diff --git a/.gitignore b/.gitignore index 5aa496774..56d4feb22 100644 --- a/.gitignore +++ b/.gitignore @@ -93,10 +93,18 @@ $RECYCLE.BIN/ unsuccessfulbuild /mk/windows/dependencies/openssl /mk/windows/dependencies/src +/mk/windows/bin /data # -/mk/linux/zetaglest -/mk/linux/zetaglest_editor -/mk/linux/zetaglest_g3dviewer source/masterserver mk/linux/tools-for-standalone-client/installer/mojosetup + +# These are generated when 'cmake . && make' is run from the root directory +CMakeFiles +CMakeCache.txt +Makefile +cmake_install.cmake +CPackConfig.cmake +CPackSourceConfig.cmake +/source/shared_lib/liblibmegaglest.a +/source/shared_lib/sources/streflop/libstreflop.a diff --git a/BUILD.md b/BUILD.md index b9c93b5db..d7955be68 100644 --- a/BUILD.md +++ b/BUILD.md @@ -68,10 +68,23 @@ libwxgtk3.0-dev libxml2-dev Follow the [instructions](https://github.com/ZetaGlest/zetaglest-source/blob/develop/mk/windows/README.md) listed in the `Windows` directory. -### *BSD +### OpenBSD -(Need editing/updating) -[Current Status](https://github.com/ZetaGlest/zetaglest-source/issues/56) +Below is a list of most of dependencies you will need. + +cmake libvorbis lua sdl2 openal wxWidgets fribidi ftgl nghttp2 gnutls glew jpeg png +miniupnpc curl freeealut + +A C++11 compiler is mandatory, so base (or package) clang++, or eg++ (from 4.9.3) + +You can install the dependencies using `pkg_add`: + +`pkg_add curl libvorbis openal sdl2 git miniupnpc cmake wxWidgets +xerces-c lua icu4c freealut` + +#### Optional + +`pkg_add g++ (either 4.9.3 or 6.2.0)` ### Arch Linux @@ -98,8 +111,10 @@ lua51 miniupnpc openal sdl2 wxgtk3 xerces-c cmake ftjam git mesa` ./build-zg.bat -### BSD +### OpenBSD -(Need editing/updating) +`cmake <-DCMAKE_CXX_COMPILER=eg++>` + +`make` ### Other (add sections) diff --git a/CMakeLists.txt b/CMakeLists.txt index e8caef397..b5eb12535 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,11 @@ CMAKE_MINIMUM_REQUIRED( VERSION 2.8.2 ) -PROJECT( MegaGlest ) +PROJECT( ZetaGlest ) #SET(CMAKE_VERBOSE_MAKEFILE ON) # # *NOTE: For now we assume some variation of GCC Compiler (or MingW for Windows binaries) -# VC++ users should not use CMake yet but rather the build-mg.bat file in mk/windows +# VC++ users should not use CMake yet but rather the build-zg.bat file in mk/windows # # build type @@ -30,11 +30,11 @@ IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") ADD_DEFINITIONS("-Wno-switch") ENDIF() -OPTION(BUILD_MEGAGLEST_MODEL_IMPORT_EXPORT_TOOLS "Build model import/export tools" ON) -OPTION(BUILD_MEGAGLEST_MODEL_VIEWER "Build model viewer" ON) -OPTION(BUILD_MEGAGLEST_MAP_EDITOR "Build map editor" ON) -OPTION(BUILD_MEGAGLEST "Build MegaGlest" ON) -OPTION(BUILD_MEGAGLEST_TESTS "Build MegaGlest Unit Tests" OFF) +OPTION(BUILD_ZETAGLEST_MODEL_IMPORT_EXPORT_TOOLS "Build model import/export tools" ON) +OPTION(BUILD_ZETAGLEST_MODEL_VIEWER "Build model viewer" ON) +OPTION(BUILD_ZETAGLEST_MAP_EDITOR "Build map editor" ON) +OPTION(BUILD_ZETAGLEST "Build ZetaGlest" ON) +OPTION(BUILD_ZETAGLEST_TESTS "Build ZetaGlest Unit Tests" OFF) OPTION(WANT_SINGLE_INSTALL_DIRECTORY "Use single install directory for everything. It is useful for example for MacOS cpack bundles." OFF) OPTION(WANT_STATIC_LIBS "Builds as many static libs as possible." OFF) OPTION(WANT_USE_VLC "Use libVLC to play videos." ON) @@ -58,37 +58,37 @@ include(${CMAKE_SOURCE_DIR}/mk/cmake/Modules/SpecialMacros.cmake) include(${CMAKE_SOURCE_DIR}/mk/cmake/Modules/ReqVersAndStaticConf.cmake) MESSAGE(STATUS "=====================================================================") -SET(PKG_NAME "megaglest") +SET(PKG_NAME "zetaglest") # read version -FILE(READ ${PROJECT_SOURCE_DIR}/source/glest_game/facilities/game_util.cpp MG_VERSION_H_CONTENTS) +FILE(READ ${PROJECT_SOURCE_DIR}/source/glest_game/facilities/game_util.cpp ZG_VERSION_H_CONTENTS) MESSAGE(STATUS "Determining version number system type to use:") SET(Begin_otv_string "string glestVersionString") -STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)(\\.)([^\\.]*)(\\.)([^\"\\.;]*)\";" _threePartMatch "${MG_VERSION_H_CONTENTS}") -STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)(\\.)([^\"\\.;]*)\";" _twoPartMatch "${MG_VERSION_H_CONTENTS}") -STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\"\\.;]*)\";" _onePartMatch "${MG_VERSION_H_CONTENTS}") +STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)(\\.)([^\\.]*)(\\.)([^\"\\.;]*)\";" _threePartMatch "${ZG_VERSION_H_CONTENTS}") +STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)(\\.)([^\"\\.;]*)\";" _twoPartMatch "${ZG_VERSION_H_CONTENTS}") +STRING(REGEX MATCH "${Begin_otv_string}[ \t]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\"\\.;]*)\";" _onePartMatch "${ZG_VERSION_H_CONTENTS}") IF(_threePartMatch) MESSAGE(STATUS "** Trying 3 part versioning ") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)\\.[^\\.]*\\..*" "\\1" VER_MAJOR "${_threePartMatch}") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*[0-9][^\\.]*\\.([^\\.]*)\\..*" "\\1" VER_MINOR "${_threePartMatch}") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*[0-9][^\\.]*\\.[^\\.]*\\.([^\"\\.;]*).*" "\\1" VER_PATCH "${_threePartMatch}") - SET(MEGAGLEST_VERSION "${VER_MAJOR}.${VER_MINOR}.${VER_PATCH}") + SET(ZETAGLEST_VERSION "${VER_MAJOR}.${VER_MINOR}.${VER_PATCH}") ELSEIF(_twoPartMatch) MESSAGE(STATUS "** Trying 2 part versioning ") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\\.]*)\\..*" "\\1" VER_MAJOR "${_twoPartMatch}") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*[0-9][^\\.]*\\.([^\"\\.;]*).*" "\\1" VER_MINOR "${_twoPartMatch}") SET(VER_PATCH "0") - SET(MEGAGLEST_VERSION "${VER_MAJOR}.${VER_MINOR}") + SET(ZETAGLEST_VERSION "${VER_MAJOR}.${VER_MINOR}") ELSEIF(_onePartMatch) MESSAGE(STATUS "** Trying 1 part versioning ") STRING(REGEX REPLACE "[^=]*=[ \t]*\"[ \t]*v[ \t]*([0-9][^\"\\.;]*).*" "\\1" VER_MAJOR "${_onePartMatch}") SET(VER_MINOR "0") SET(VER_PATCH "0") - SET(MEGAGLEST_VERSION "${VER_MAJOR}") + SET(ZETAGLEST_VERSION "${VER_MAJOR}") ENDIF() -FOREACH(ver_var VER_MAJOR;VER_MINOR;VER_PATCH;MEGAGLEST_VERSION) +FOREACH(ver_var VER_MAJOR;VER_MINOR;VER_PATCH;ZETAGLEST_VERSION) STRING(REGEX REPLACE "[ \t]" "" "${ver_var}" "${${ver_var}}") ENDFOREACH() -MESSAGE(STATUS "Detected MegaGlest Version is [v${MEGAGLEST_VERSION}] ") +MESSAGE(STATUS "Detected ZetaGlest Version is [v${ZETAGLEST_VERSION}] ") SET(PIC_FLAG "${PIC_FLAG} -fPIC") IF(WANT_STATIC_LIBS AND "${CMAKE_SIZEOF_VOID_P}" EQUAL "8") @@ -210,121 +210,79 @@ IF(CMAKE_COMPILER_IS_GNUCXX OR MINGW) SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -s") ## Strip binary ENDIF() - # see if this fixes compile issue for tomreyn: libpthread.so.0: error adding symbols: DSO missing from command line - IF(UNIX) - MESSAGE(STATUS "*NOTE: Apply fix for: libpthread.so.0: error adding symbols: DSO missing from command line") - set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") - set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -pthread") - SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -pthread") - SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -pthread") - ENDIF() - - # Get the git revision info for the binary - SET(HAS_GIT "FALSE") - SET(GIT_LIVE_REV_CMD "") - - OPTION(WANT_GIT_STAMP "use git revision stamp" ON) - IF(WANT_GIT_STAMP) - # The stuff below gets GIT Global Revision # but ONLY when calling cmake! - # the FindGit.cmake module is part of the standard distribution - include(FindGit) - - IF(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git/") - SET(HAS_GIT "TRUE") - MESSAGE(STATUS "Found GIT and using GIT version stamping...") - - # Get the current commit SHA1 - execute_process( - COMMAND git log -1 --format=%h --abbrev=7 - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_SHA1 - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - # Get the current version counter - execute_process( - COMMAND git rev-list HEAD --count - WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} - OUTPUT_VARIABLE GIT_COMMIT_COUNT - OUTPUT_STRIP_TRAILING_WHITESPACE - ) - - SET(GIT_LIVE_REV_CMD "`cd '${PROJECT_SOURCE_DIR}' && git rev-list HEAD --count`.`cd '${PROJECT_SOURCE_DIR}' && git log -1 --format=%h --abbrev=7`") - - MESSAGE(STATUS "Using GIT revision stamp: [${GIT_COMMIT_COUNT}.${GIT_SHA1}] CMD [${GIT_LIVE_REV_CMD}]") - - IF(CMAKE_GENERATOR STREQUAL Xcode) - SET(GIT_VERSION_CMD "-DGITVERSION='\\\\'${GIT_COMMIT_COUNT}.${GIT_SHA1}\\\\''") - ELSE() - SET(GIT_VERSION_CMD "-DGITVERSION='\\\"${GIT_LIVE_REV_CMD}\\\"'") - ENDIF() - ENDIF() + # see if this fixes compile issue for tomreyn: libpthread.so.0: error adding symbols: DSO missing from command line + IF(UNIX) + MESSAGE(STATUS "*NOTE: Apply fix for: libpthread.so.0: error adding symbols: DSO missing from command line") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread") + set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -pthread") + SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -pthread") + SET(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_MINSIZEREL} -pthread") ENDIF() SET(COMMON_INFO_ABOUT_PATH "(if the path is relative then is appended to the CMAKE_INSTALL_PREFIX)") IF(CMAKE_INSTALL_PREFIX STREQUAL "") MESSAGE(STATUS "*NOTE: NOT USING a Custom Data Install Path...") ELSE() - IF(WANT_SINGLE_INSTALL_DIRECTORY AND NOT MEGAGLEST_SINGLE_DIRECTORY_INSTALL_PATH) - SET(MEGAGLEST_SINGLE_DIRECTORY_INSTALL_PATH "megaglest-game/" CACHE STRING "The single directory installation path for game ${COMMON_INFO_ABOUT_PATH}") + IF(WANT_SINGLE_INSTALL_DIRECTORY AND NOT ZETAGLEST_SINGLE_DIRECTORY_INSTALL_PATH) + SET(ZETAGLEST_SINGLE_DIRECTORY_INSTALL_PATH "zetaglest-game/" CACHE STRING "The single directory installation path for game ${COMMON_INFO_ABOUT_PATH}") ENDIF() - IF(NOT MEGAGLEST_BIN_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) - SET(MEGAGLEST_BIN_INSTALL_PATH "bin/" CACHE STRING "The installation path for binaries ${COMMON_INFO_ABOUT_PATH}") + IF(NOT ZETAGLEST_BIN_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) + SET(ZETAGLEST_BIN_INSTALL_PATH "bin/" CACHE STRING "The installation path for binaries ${COMMON_INFO_ABOUT_PATH}") ENDIF() - IF(NOT MEGAGLEST_DATA_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) - SET(MEGAGLEST_DATA_INSTALL_PATH "share/megaglest/" CACHE STRING "The installation path for data files ${COMMON_INFO_ABOUT_PATH}") + IF(NOT ZETAGLEST_DATA_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) + SET(ZETAGLEST_DATA_INSTALL_PATH "share/zetaglest/" CACHE STRING "The installation path for data files ${COMMON_INFO_ABOUT_PATH}") ENDIF() - SET(MEGAGLEST_INI_INSTALL_PATH "${MEGAGLEST_DATA_INSTALL_PATH}") - IF(NOT MEGAGLEST_DESKTOP_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) - SET(MEGAGLEST_DESKTOP_INSTALL_PATH "share/applications/" CACHE STRING "The installation path for desktop files ${COMMON_INFO_ABOUT_PATH}") + SET(ZETAGLEST_INI_INSTALL_PATH "${ZETAGLEST_DATA_INSTALL_PATH}") + IF(NOT ZETAGLEST_DESKTOP_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) + SET(ZETAGLEST_DESKTOP_INSTALL_PATH "share/applications/" CACHE STRING "The installation path for desktop files ${COMMON_INFO_ABOUT_PATH}") ENDIF() - IF(NOT MEGAGLEST_ICON_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) - SET(MEGAGLEST_ICON_INSTALL_PATH "share/pixmaps/" CACHE STRING "The installation path for icon files ${COMMON_INFO_ABOUT_PATH}") + IF(NOT ZETAGLEST_ICON_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) + SET(ZETAGLEST_ICON_INSTALL_PATH "share/pixmaps/" CACHE STRING "The installation path for icon files ${COMMON_INFO_ABOUT_PATH}") ENDIF() - IF(NOT MEGAGLEST_MANPAGE_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) - SET(MEGAGLEST_MANPAGE_INSTALL_PATH "share/man/man6/" CACHE STRING "The installation path for manpage files ${COMMON_INFO_ABOUT_PATH}") + IF(NOT ZETAGLEST_MANPAGE_INSTALL_PATH AND NOT WANT_SINGLE_INSTALL_DIRECTORY) + SET(ZETAGLEST_MANPAGE_INSTALL_PATH "share/man/man6/" CACHE STRING "The installation path for manpage files ${COMMON_INFO_ABOUT_PATH}") ENDIF() IF(WANT_SINGLE_INSTALL_DIRECTORY) - FOREACH(MG_PATH BIN DATA INI DESKTOP ICON) - IF(NOT MEGAGLEST_${MG_PATH}_INSTALL_PATH) - SET(MEGAGLEST_${MG_PATH}_INSTALL_PATH "${MEGAGLEST_SINGLE_DIRECTORY_INSTALL_PATH}") + FOREACH(ZG_PATH BIN DATA INI DESKTOP ICON) + IF(NOT ZETAGLEST_${ZG_PATH}_INSTALL_PATH) + SET(ZETAGLEST_${ZG_PATH}_INSTALL_PATH "${ZETAGLEST_SINGLE_DIRECTORY_INSTALL_PATH}") ENDIF() ENDFOREACH() ENDIF() IF(NOT WANT_SINGLE_INSTALL_DIRECTORY) - FOREACH(MG_PATH DATA INI) - IF(IS_ABSOLUTE "${MEGAGLEST_${MG_PATH}_INSTALL_PATH}") - SET(MEGAGLEST_FULL_${MG_PATH}_INSTALL_PATH "${MEGAGLEST_${MG_PATH}_INSTALL_PATH}/") + FOREACH(ZG_PATH DATA INI) + IF(IS_ABSOLUTE "${ZETAGLEST_${ZG_PATH}_INSTALL_PATH}") + SET(ZETAGLEST_FULL_${ZG_PATH}_INSTALL_PATH "${ZETAGLEST_${ZG_PATH}_INSTALL_PATH}/") ELSE() - SET(MEGAGLEST_FULL_${MG_PATH}_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${MEGAGLEST_${MG_PATH}_INSTALL_PATH}/") + SET(ZETAGLEST_FULL_${ZG_PATH}_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${ZETAGLEST_${ZG_PATH}_INSTALL_PATH}/") ENDIF() - STRING(REGEX REPLACE "//+" "/" MEGAGLEST_FULL_${MG_PATH}_INSTALL_PATH "${MEGAGLEST_FULL_${MG_PATH}_INSTALL_PATH}") + STRING(REGEX REPLACE "//+" "/" ZETAGLEST_FULL_${ZG_PATH}_INSTALL_PATH "${ZETAGLEST_FULL_${ZG_PATH}_INSTALL_PATH}") ENDFOREACH() IF(NOT CUSTOM_DATA_INSTALL_PATH) - SET(CUSTOM_DATA_INSTALL_PATH "${MEGAGLEST_FULL_DATA_INSTALL_PATH}") + SET(CUSTOM_DATA_INSTALL_PATH "${ZETAGLEST_FULL_DATA_INSTALL_PATH}") # ^ someday this intermediate step may be removed ENDIF() SET(CUSTOM_INSTALL_PATHS_VALUES "-DCUSTOM_DATA_INSTALL_PATH=${CUSTOM_DATA_INSTALL_PATH}") ENDIF() #SET(PKG_DATADIR ${CUSTOM_DATA_INSTALL_PATH_VALUE}) - #SET(PKG_BINDIR ${MEGAGLEST_BIN_INSTALL_PATH}) + #SET(PKG_BINDIR ${ZETAGLEST_BIN_INSTALL_PATH}) # ^ hard to tell for what it is needed and most likely both were set to wrong values, so let's try to not use them MESSAGE(STATUS "*NOTE: Custom Data Install Path is [${CUSTOM_DATA_INSTALL_PATH}]") ENDIF() - string(TOUPPER "${CMAKE_BUILD_TYPE}" MG_BUILD_TYPE) + string(TOUPPER "${CMAKE_BUILD_TYPE}" ZG_BUILD_TYPE) IF(HAS_GIT STREQUAL "TRUE") - SET(CMAKE_CXX_FLAGS_${MG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${MG_BUILD_TYPE}} ${GIT_VERSION_CMD}") + SET(CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE}} ${GIT_VERSION_CMD}") ENDIF() - SET(CMAKE_CXX_FLAGS_${MG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${MG_BUILD_TYPE}} ${CUSTOM_INSTALL_PATHS_VALUES}") + SET(CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE}} ${CUSTOM_INSTALL_PATHS_VALUES}") # We do some funky character escaping to get the right stuff written out to # the final Makefile so we get the GIT Global Revsion # - string(REPLACE "'" "\"" CMAKE_CXX_FLAGS_${MG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${MG_BUILD_TYPE}}") + string(REPLACE "'" "\"" CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE} "${CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE}}") - message(STATUS "CMAKE_CXX_FLAGS_${MG_BUILD_TYPE}: ${CMAKE_CXX_FLAGS_${MG_BUILD_TYPE}}") + message(STATUS "CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE}: ${CMAKE_CXX_FLAGS_${ZG_BUILD_TYPE}}") # Win32 specific Compiler Flags IF(WIN32) @@ -348,10 +306,10 @@ IF(NOT WANT_DEPRECATION_WARNINGS) ENDIF() MARK_AS_ADVANCED(WANT_DEPRECATION_WARNINGS) -IF(NOT MG_CMAKE_INSTALL_PREFIX AND NOT CMAKE_INSTALL_PREFIX STREQUAL "") - SET(MG_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") +IF(NOT ZG_CMAKE_INSTALL_PREFIX AND NOT CMAKE_INSTALL_PREFIX STREQUAL "") + SET(ZG_CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") ENDIF() -MESSAGE(STATUS "**NOTE: MG_CMAKE_INSTALL_PREFIX is [${MG_CMAKE_INSTALL_PREFIX}]") +MESSAGE(STATUS "**NOTE: ZG_CMAKE_INSTALL_PREFIX is [${ZG_CMAKE_INSTALL_PREFIX}]") SET(SDL_WINDOWS_DIR_DINC "SDL-2.0.x") SET(SDL_VERSION_NAME "SDL2") @@ -359,13 +317,13 @@ SET(SDL_VERSION_SNAME "sdl") OPTION(WANT_DEV_OUTPATH "Use developer friendly output paths." OFF) IF(UNIX AND "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") - SET(MEGAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/linux/") + SET(ZETAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/linux/") ELSEIF(UNIX AND APPLE) - SET(MEGAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/macos/") + SET(ZETAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/macos/") ELSEIF(UNIX AND NOT APPLE) - SET(MEGAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/other_unix/") + SET(ZETAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/mk/other_unix/") ELSE() - SET(MEGAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/data/glest_game/") + SET(ZETAGLEST_FRIENDLY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/data/glest_game/") ENDIF() IF(WIN32) @@ -431,33 +389,33 @@ IF(NOT DEFINED CPACK_GENERATOR) ENDIF() # CPack configuration shared accross platforms SET(CPACK_PACKAGE_NAME ${PKG_NAME}) -IF(EXISTS "${PROJECT_SOURCE_DIR}/data/glest_game/others/megaglest-long-description.txt.in") - FILE(READ "${PROJECT_SOURCE_DIR}/data/glest_game/others/megaglest-long-description.txt.in" MEGAGLEST_LONG_DESCRIPTION) - SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${MEGAGLEST_LONG_DESCRIPTION}") +IF(EXISTS "${PROJECT_SOURCE_DIR}/../zetaglest-data/others/zetaglest-long-description.txt.in") + FILE(READ "${PROJECT_SOURCE_DIR}/../zetaglest-data/others/zetaglest-long-description.txt.in" ZETAGLEST_LONG_DESCRIPTION) + SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${ZETAGLEST_LONG_DESCRIPTION}") ELSE() - SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "MegaGlest") + SET(CPACK_PACKAGE_DESCRIPTION_SUMMARY "ZetaGlest") ENDIF() -SET(CPACK_PACKAGE_VENDOR "megaglest.org") +SET(CPACK_PACKAGE_VENDOR "zetaglest.github.io") #SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README") #SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING") -SET(CPACK_PACKAGE_INSTALL_DIRECTORY "megaglest") +SET(CPACK_PACKAGE_INSTALL_DIRECTORY "zetaglest") SET(CPACK_PACKAGING_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}") -SET(CPACK_PACKAGE_VERSION "${MEGAGLEST_VERSION}") +SET(CPACK_PACKAGE_VERSION "${ZETAGLEST_VERSION}") SET(CPACK_PACKAGE_VERSION_MAJOR "${VER_MAJOR}") SET(CPACK_PACKAGE_VERSION_MINOR "${VER_MINOR}") SET(CPACK_PACKAGE_VERSION_PATCH "${VER_PATCH}") IF("${CPACK_GENERATOR}" STREQUAL "DEB") - SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Mark Vejvoda") #required + SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "ZetaGlest Team") #required SET(CPACK_DEBIAN_PACKAGE_SECTION "games") SET(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON) SET(CPACK_DEBIAN_PACKAGE_SUGGESTS "p7zip-full") ENDIF() IF("${CPACK_GENERATOR}" STREQUAL "NSIS") - SET(CPACK_NSIS_DISPLAY_NAME "MegaGlest") + SET(CPACK_NSIS_DISPLAY_NAME "ZetaGlest") SET(CPACK_NSIS_MUI_ICON "${PROJECT_SOURCE_DIR}/mk/windows/zetaglest.ico") SET(CPACK_PACKAGE_ICON "${PROJECT_SOURCE_DIR}/mk/windows/zetaglest.ico") - SET(CPACK_NSIS_URL_INFO_ABOUT "http://megaglest.org") + SET(CPACK_NSIS_URL_INFO_ABOUT "https://zetaglest.github.io") ENDIF() INCLUDE(CPack) diff --git a/ChangeLog.md b/ChangeLog.md index e1d432c2c..a9614bdb2 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -5,6 +5,29 @@ This documents notable or significant changes to information about the game from which it was forked, please see the [MegaGlest home page](https://megaglest.org/). +## 2018-05-08 + +OpenBSD build instructions added + +## 2018-05-06 + +[[d1acd87](https://github.com/ZetaGlest/zetaglest-source/commit/d1acd8742395bca8b37656e9e8065022902586fe)]Lua +can now be used in scripts tag in faction tag within XML + +## 2018-03-31 + +[[bugfix:835c6ef](https://github.com/ZetaGlest/zetaglest-source/commit/835c6efd924ed7e62ccd43e064bc6b7f5cdecbfc)]Fixed +"x" key getting stuck on Windows + +## 2018-03-20 + +[[045b0b8](https://github.com/ZetaGlest/zetaglest-source/commit/045b0b88e051f7163fbef42fa0adc63a326ca186)]Removed "MG_" and "xxx" (3 digit extension) on nicknames in lobby player list + +## 2018-03-14 + +[Windows](https://github.com/ZetaGlest/zetaglest-source/commits/develop/mk/windows) build +system, dependencies, and documentation updated + ## 2018-02-22 * Some sound effects can now be customized by players. See diff --git a/README.md b/README.md index bd1a2004d..df70de339 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,19 @@ Open games and [Server list and player stats](https://zetaglest.dreamhosters.com [Detailed information about units in the game](https://zetaglest.github.io/) +## Stay Informed (Updates) + +* [ChangeLog.md](https://github.com/ZetaGlest/zetaglest-source/blob/develop/ChangeLog.md)
+* [![RSS](rss_23x23.jpeg)](https://github.com/ZetaGlest/zetaglest-source/commits/develop.atom) +[Recent Commits to zetaglest-source:develop](https://github.com/ZetaGlest/zetaglest-source/commits/develop.atom)
+* [![RSS](rss_23x23.jpeg)](https://github.com/ZetaGlest/zetaglest-data/commits/develop.atom) +[Recent Commits to zetaglest-data:develop](https://github.com/ZetaGlest/zetaglest-data/commits/develop.atom)
+* [![RSS](rss_23x23.jpeg)](https://github.com/ZetaGlest/zetaglest.github.io/commits/master.atom) +[Recent Commits to the web site](https://github.com/ZetaGlest/zetaglest.github.io/commits/master.atom) + ## Why this fork? (Project Goals) -ZetaGlest forked from MegaGlest at v3.13.0-dev +ZetaGlest forked from MegaGlest at v3.13.0-dev ([12998eb](https://github.com/ZetaGlest/zetaglest-source/commit/12998eb10f6447ac89a45d1e6d76f17946647b7a)) I noticed that there were some good feature requests in the MegaGlest forum, as well as some mods, but it seemed that implementing and @@ -99,4 +109,5 @@ for extended, important instructions and guidelines for contributing. ## Contact * [Discord](https://discord.gg/WaAaXS7) +* [Message Board](https://groups.google.com/forum/#!forum/zetaglest) * email: andy400-dev@yahoo.com diff --git a/docs/AUTHORS.source_code.txt b/deprecated_megaglest_docs/AUTHORS.source_code.txt similarity index 100% rename from docs/AUTHORS.source_code.txt rename to deprecated_megaglest_docs/AUTHORS.source_code.txt diff --git a/docs/CHANGELOG.txt b/deprecated_megaglest_docs/CHANGELOG.txt similarity index 100% rename from docs/CHANGELOG.txt rename to deprecated_megaglest_docs/CHANGELOG.txt diff --git a/docs/COPYRIGHT.source_code.txt b/deprecated_megaglest_docs/COPYRIGHT.source_code.txt similarity index 100% rename from docs/COPYRIGHT.source_code.txt rename to deprecated_megaglest_docs/COPYRIGHT.source_code.txt diff --git a/docs/README.release-management.txt b/deprecated_megaglest_docs/README.release-management.txt similarity index 100% rename from docs/README.release-management.txt rename to deprecated_megaglest_docs/README.release-management.txt diff --git a/docs/README.txt b/deprecated_megaglest_docs/README.txt similarity index 98% rename from docs/README.txt rename to deprecated_megaglest_docs/README.txt index 73cba973d..014f4c828 100644 --- a/docs/README.txt +++ b/deprecated_megaglest_docs/README.txt @@ -1,3 +1,7 @@ +The documents in this directory are here for historical purposes and pertain +to MegaGlest, not ZetaGlest. To view active documentation for ZetaGlest, see +https://github.com/ZetaGlest/zetaglest-source and https://zetaglest.github.io/docs/ + MEGAGLEST diff --git a/docs/gnu_gpl_3.0.txt b/deprecated_megaglest_docs/gnu_gpl_3.0.txt similarity index 100% rename from docs/gnu_gpl_3.0.txt rename to deprecated_megaglest_docs/gnu_gpl_3.0.txt diff --git a/docs/README.compiling-linux.txt b/docs/README.compiling-linux.txt deleted file mode 100644 index a323a4b00..000000000 --- a/docs/README.compiling-linux.txt +++ /dev/null @@ -1,174 +0,0 @@ - - MEGAGLEST - - by Titus Tscharntke and Mark Vejvoda - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Build instructions for Linux -~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Architecture ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Developed on Linux with glibc, little endian CPU. While MacIntel builds exist -(for some versions of the game), MegaGlest does not currently work on big -endian CPUs like PPC (though some unfinished patches for vanilla Glest float -around on the forums, e.g. http://forum.megaglest.org/?topic=1426#). - -~~~~~~~~~~~~~~~~~~~~~~ 2. Building and Installation ~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ---- 2.1 Prerequesites --- - -Compiling MegaGlest requires the following dependencies to be installed: - -* Standard GNU compiler and additional tools (g++ version 4.6.3 or later is - required at the moment) - -* Kitware CMake 2.8.2 or later (used as build tool) - -* X11 libraries + headers - http://x.org/ - -* SDL 2.0.0 or later - http://libsdl.org/ - -* OpenGL - http://dri.freedesktop.org/wiki/libGL - -* libvlc - http://www.videolan.org/vlc/libvlc.html - -* libcurl - http://curl.haxx.se/libcurl/ - -* wxWidgets - http://wxwidgets.org/ - -* OpenAL - http://openal.org/ - -* Ogg - http://xiph.org/ogg/ - -* Vorbis - http://xiph.org/vorbis/ - -* Xerces-C - http://xerces.apache.org/xerces-c/ - -* Lua 5.1 or later - http://www.lua.org/ - -* JPEG - http://www.ijg.org/ - -* PNG - http://www.libpng.org/ - -* Zlib - http://zlib.net/ - -* GnuTLS - http://www.gnu.org/software/gnutls/ - -* ICU - http://site.icu-project.org/ - -* libdl - -NOTE: A script which tries to install build dependencies on many Linux distros -is located in mk/linux/setupBuildDeps.sh - ---- 2.2 Building --- - -To build the game simply invoke the build script: - -../mk/linux/build-mg.sh - -This script manually calls cmake with some optional parameters. Feel free to -examine it and build manually using cmake. - ---- 2.3 Installation -- - -We provide MojoSetup based installers for Linux and NSIS based installers for -Windows. By default, the Linux installers install to your home directory. The -Windows installers install to %ProgramFiles% (global system scope). - -There are also community maintained packages available for several Linux and -BSD distributions. Please see the website, forums and wiki for details. - - -~~~~~~~~~~~~~~~~~~~~~~ 3. Troubleshooting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ---- General --- -* Make sure both the hardware and software of your system match the requirements -* If you cannot find what you are looking for on here please check the FAQs - (https://docs.megaglest.org/MG/FAQ) before contacting the developers. - ---- Compiling --- -* If CMake reports that it cannot find some of the libraries, make sure that - the relevant ...-dev(el) packages are also installed (distro-dependent). - ---- Sound/Audio --- -* If the game doesn't start because of audio/sound errors: - Make sure no other application is using your soundcard. Typical problems are - the Gnome/KDE sound dameons esd and artsd. You can kill these daemons with - the following commands: - # killall esd ; killall artsd - -* If this doesn't solve the sound problems, get an updated OpenAL from - http://openal.org or a newer repository provided by your distribution. - -* Sound is played through OpenAL - double-check the OpenAL system - configuration: http://supertux.lethargik.org/wiki/OpenAL_Configuration - ---- OpenGL --- -* If the game produces error messages regarding OpenGL or OpenGL extensions - being unavailable, look at glxinfo and make sure the system is using the - drivers you want to use. If you have a NVIDIA or AMD/ATI graphics card then - consider using the proprietary drivers, which may provide better - performance than the open source drivers most distributions use by default. - Most Intel graphics chips use an open source driver on Linux, based on Mesa - ("glxinfo | grep -i mesa"). This hardware is much slower than any dedicated - graphics cards produced during the past few years. The same holds true for - AMD APUs (the graphics chips embedded into AMD processors). - ---- Crashing --- -* Check the forums at http://forums.megaglest.org/ -* Please report any crashes and freezes that are not yet described on the forums, - preferably with a gdb backtrace from a debugging enabled build - (cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo) - - -~~~~~~~~~~~~~~~~~~~~~~~~~~ 4. More information ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -* Website - http://megaglest.org/ - -* Wiki - https://docs.megaglest.org/Main_Page - -* Forums - http://forums.megaglest.org/ - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5. Contact + Credits ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* MegaGlest is developed by: - Titus Tscharntke (info@titusgames.de) - Mark Vejvoda (www.soft-haus.com - mark_vejvoda@hotmail.com) - -* General contact: - contact@megaglest.org - -* MegaGlest is a fork of Glest: - http://glest.org/ - -* Linux port by: - Matthias Braun - - - with help from - - Karl Robillard - - -*** Please also refer to the copyright file. *** - -On Debian GNU/Linux systems please find license information in: -/usr/share/common-licenses diff --git a/docs/README.compiling-windows.txt b/docs/README.compiling-windows.txt deleted file mode 100644 index 292c19375..000000000 --- a/docs/README.compiling-windows.txt +++ /dev/null @@ -1,152 +0,0 @@ - - MEGAGLEST - - by Titus Tscharntke and Mark Vejvoda - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - Build Instructions for Windows -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Architecture ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -Developed on Windows with Microsoft Visual Studio Community 2015, -little endian CPU. Compiling using the mingw32 toolset is experimental, which -also allows for cross compiling. - -On Debian GNU/Linux systems please find license information in: -/usr/share/common-licenses -Also see Build Instructions for Linux - -~~~~~~~~~~~~~~~~~~~~~~~~ 2. Building and Installation ~~~~~~~~~~~~~~~~~~~~~~~~ - ---- 2.1 Prerequesites --- - -To build the game, the following tools and libraries must be present: - -- Pick 1: -* Microsoft Visual Studio Community 2015 - https://www.microsoft.com/en-us/download/details.aspx?id=48146 - (Experimental Microsoft Visual C++ Express 2012 projects also exist in the vc2012 - folder) -* mingw32 - http://mingw.org/ - -- Pick 1: -* MegaGlest dependencies for building with VC++, which which will be downloaded - automatically in 'Option A' (see below) -* MegaGlest Windows 32 bit dependencies for building with mingw: - https://github.com/MegaGlest/megaglest-source/releases/download/3.2.3/win32_deps_mingw.7z - -- These include: -* Xerces-C - http://xerces.apache.org/xerces-c/ -* wxWidgets - http://wxwidgets.org/ -* And many more. For a more verbose list, please inspect the archive contents and refer to the - Linux build instructions. - - ---- 2.2 Building --- - -To build and install the game proceed as follows: - -*** Option A ~ (recommended) Automated build on the command line: - -1. Open a command prompt and navigate to the root folder where you have acquired the source code. - -2. cd mk\windows - -3. build-mg-2015.bat (or build-mg32bit-2015.bat for 32 bit build) - -4. megaglest.exe --version - -5. megaglest.exe - -*** Option B ~ Using VC++ IDE: - -1. Download the dependencies archive (which one and from where you can find - out in the "code" of build-...bat script from 'Option A' approach, some line - with "wget.exe") and decompress its contents into the 'source' directory. This - should create a subdirectory called "windows_deps..." with many files and - subdirectories in it. - -2. Start the Visual C++ 2015 IDE and open the solution file: - \mk\windows\Glest_vc2015.sln - -3. Right Click on the top level 'Glest' node in Solution Explorer and select - 'Rebuild All' (choose release mode or release with debug info). - -Note: If you had no errors all binaries will be created. - Before running MegaGlest you must run CopyWindowsRuntimeDlls_2015.bat. - You should now be able to just run megaglest.exe. - - ---- 2.3 Installation --- - -We provide NSIS based installation packages. By default, these will create a -system-wide installation below %ProgramFiles% and setup Desktop icons for -simplified access. User specific configuration will be stored within the -directory tree the %AppData% environment variable points to. - - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3. Troubleshooting ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - ---- General --- -* Make sure that the hardware and software of your system match the - requirements - -* If you cannot find what you are looking for on here please check the FAQs - (https://docs.megaglest.org/MG/FAQ) before contacting. - ---- Compiling --- -* If configure fails make sure you have read the Building section above - ---- Sound/Audio errors when starting --- -* If the game doesn't start because of audio/sound errors: - Make sure no other application is using your soundcard. -* If this doesn't solve your sound problems try to get an updated OpenAL from - http://openal.org -* Sound is played through OpenAL - you might need to take a look at your - configuration: http://supertux.lethargik.org/wiki/OpenAL_Configuration - ---- The game complains that OpenGL 1.3: is not available, is missing OpenGL - extensions, or works very slowly --- -* Try to get updated graphics drivers. - --- The game crashes --- -* Check the forums at http://forums.megaglest.org/ -* Please report any other crashes and freezes that are not yet described on - the forums, preferably with a backtrace from a debugging enabled build - -~~~~~~~~~~~~~~~~~~~~~~~~~~ 4. More information ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -* Website - http://megaglest.org/ - -* Wiki - https://docs.megaglest.org/Main_Page - -* Forums - http://forums.megaglest.org/ - -~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5. Contact + Credits ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -* MegaGlest is developed by: - Titus Tscharntke (info@titusgames.de) - Mark Vejvoda (www.soft-haus.com - mark_vejvoda@hotmail.com) - -* General contact: - contact@megaglest.org - -* MegaGlest is a fork of Glest: - http://glest.org/ - -* Linux port by: - Matthias Braun - - - with help from - - Karl Robillard - - -*** Please also refer to the copyright file. *** diff --git a/mk/cmake/Modules/ReqVersAndStaticConf.cmake b/mk/cmake/Modules/ReqVersAndStaticConf.cmake index c9bc8db79..61c02805a 100644 --- a/mk/cmake/Modules/ReqVersAndStaticConf.cmake +++ b/mk/cmake/Modules/ReqVersAndStaticConf.cmake @@ -8,7 +8,7 @@ # IF(WANT_STATIC_LIBS) - IF(BUILD_MEGAGLEST_MODEL_VIEWER OR BUILD_MEGAGLEST_MAP_EDITOR OR BUILD_MEGAGLEST) + IF(BUILD_ZETAGLEST_MODEL_VIEWER OR BUILD_ZETAGLEST_MAP_EDITOR OR BUILD_ZETAGLEST) # shared lib FOREACH(STATIC_LIB OpenSSL @@ -26,7 +26,7 @@ IF(WANT_STATIC_LIBS) LIST(APPEND LIST_OF_STATIC_LIBS_MG "${STATIC_LIB}") ENDFOREACH() ENDIF() - IF(BUILD_MEGAGLEST_MODEL_VIEWER OR BUILD_MEGAGLEST_MAP_EDITOR) + IF(BUILD_ZETAGLEST_MODEL_VIEWER OR BUILD_ZETAGLEST_MAP_EDITOR) # wxWidgets for tools FOREACH(STATIC_LIB wxWidgets) @@ -38,7 +38,7 @@ IF(WANT_STATIC_LIBS) # wxWidgets by default are not available static ENDIF() ENDIF() - IF(BUILD_MEGAGLEST) + IF(BUILD_ZETAGLEST) # only libs not used by shared lib FOREACH(STATIC_LIB OGG) diff --git a/mk/linux/.gitignore b/mk/linux/.gitignore index 8ed774cfc..af011f75c 100644 --- a/mk/linux/.gitignore +++ b/mk/linux/.gitignore @@ -1,7 +1,9 @@ -/megaglest -/megaglest.6 +/zetaglest +/zetaglest.6 +/zetaglest_editor* +/zetaglest_g3dviewer* +/zetaglest_tests -/megaglest_* !/*.desktop core diff --git a/mk/linux/build-zg.sh b/mk/linux/build-zg.sh index aa6be180b..a20e92f77 100755 --- a/mk/linux/build-zg.sh +++ b/mk/linux/build-zg.sh @@ -25,7 +25,7 @@ GCC_FORCED_VERSION=0 LUA_FORCED_VERSION=0 FORCE_32BIT_CROSS_COMPILE=0 COMPILATION_WITHOUT=0 -BUILD_MEGAGLEST_TESTS="ON" +BUILD_ZETAGLEST_TESTS="ON" while getopts "c:defg:hl:mnswx" option; do case "${option}" in @@ -189,14 +189,14 @@ case $distribution in echo 'Turning ON dynamic FTGL, LUA, JPEG, PNG ... and forcing use the embedded IRCCLIENT' EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DSTATIC_FTGL=OFF -DSTATIC_LUA=OFF -DSTATIC_JPEG=OFF -DSTATIC_PNG=OFF -DSTATIC_OGG=OFF -DFORCE_USE_EMBEDDED_Ircclient=ON" fi - if [ $CLANG_FORCED = 1 ]; then BUILD_MEGAGLEST_TESTS="OFF"; fi + if [ $CLANG_FORCED = 1 ]; then BUILD_ZETAGLEST_TESTS="OFF"; fi ;; *) if [ "$WANT_STATIC_LIBS" = "-DWANT_STATIC_LIBS=ON" ]; then echo 'Turning ON dynamic OGG ... and forcing use the embedded IRCCLIENT' EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DSTATIC_OGG=OFF -DFORCE_USE_EMBEDDED_Ircclient=ON" fi - if [ $CLANG_FORCED = 1 ]; then BUILD_MEGAGLEST_TESTS="OFF"; fi + if [ $CLANG_FORCED = 1 ]; then BUILD_ZETAGLEST_TESTS="OFF"; fi # ^ may be removed ~ when default clang's version will be 3.9+ ;; esac @@ -311,12 +311,12 @@ if [ $FORCE_32BIT_CROSS_COMPILE != 0 ]; then fi if [ "$COMPILATION_WITHOUT" != "0" ] && [ "$COMPILATION_WITHOUT" != "" ]; then - EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_MEGAGLEST_MAP_EDITOR=OFF -DBUILD_MEGAGLEST_MODEL_VIEWER=OFF" + EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_ZETAGLEST_MAP_EDITOR=OFF -DBUILD_ZETAGLEST_MODEL_VIEWER=OFF" fi if [ $MAKE_ONLY = 0 ]; then echo "Calling cmake with EXTRA_CMAKE_OPTIONS = ${EXTRA_CMAKE_OPTIONS} AND WANT_STATIC_LIBS = ${WANT_STATIC_LIBS}" - cmake -DCMAKE_INSTALL_PREFIX='' -DWANT_DEV_OUTPATH=ON $WANT_STATIC_LIBS -DBUILD_MEGAGLEST_TESTS=$BUILD_MEGAGLEST_TESTS -DBREAKPAD_ROOT=$BREAKPAD_ROOT $EXTRA_CMAKE_OPTIONS ../../.. + cmake -DCMAKE_INSTALL_PREFIX='' -DWANT_DEV_OUTPATH=ON $WANT_STATIC_LIBS -DBUILD_ZETAGLEST_TESTS=$BUILD_ZETAGLEST_TESTS -DBREAKPAD_ROOT=$BREAKPAD_ROOT $EXTRA_CMAKE_OPTIONS ../../.. if [ $? -ne 0 ]; then echo 'ERROR: CMAKE failed.' >&2; exit 1 fi diff --git a/mk/macos/CMakeLists.txt b/mk/macos/CMakeLists.txt index a587c35c2..2a143a8c3 100644 --- a/mk/macos/CMakeLists.txt +++ b/mk/macos/CMakeLists.txt @@ -37,7 +37,7 @@ include (InstallRequiredSystemLibraries) # Use bundle generator (OSX has 3 other options if you feel adventurous) SET(CPACK_GENERATOR "Bundle") SET(CPACK_BUNDLE_NAME "MegaGlest") -SET(CPACK_PACKAGE_FILE_NAME "MegaGlest-game-macos-${MEGAGLEST_VERSION}") +SET(CPACK_PACKAGE_FILE_NAME "MegaGlest-game-macos-${ZETAGLEST_VERSION}") SET(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/mk/macos/bundle_resources/MegaGlest.icns") SET(CPACK_BUNDLE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/mk/macos/bundle_resources/MegaGlest.icns") SET(CPACK_BUNDLE_STARTUP_COMMAND "${PROJECT_SOURCE_DIR}/mk/macos/bundle_resources/MegaGlest.sh") diff --git a/mk/macos/build-zg.sh b/mk/macos/build-zg.sh index 70e83c601..f8a4a4343 100755 --- a/mk/macos/build-zg.sh +++ b/mk/macos/build-zg.sh @@ -191,7 +191,7 @@ if [ "$FORCE_EMBEDDED_LIBS" != "0" ] && [ "$FORCE_EMBEDDED_LIBS" != "" ]; then fi if [ "$COMPILATION_WITHOUT" != "0" ] && [ "$COMPILATION_WITHOUT" != "" ]; then - EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_MEGAGLEST_MAP_EDITOR=OFF -DBUILD_MEGAGLEST_MODEL_VIEWER=OFF" + EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_ZETAGLEST_MAP_EDITOR=OFF -DBUILD_ZETAGLEST_MODEL_VIEWER=OFF" fi if [ "$MAKE_ONLY" -eq "0" ]; then @@ -200,7 +200,7 @@ if [ "$MAKE_ONLY" -eq "0" ]; then EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DCMAKE_INSTALL_PREFIX=''" if [ "$GCC_FORCED" -ne "1" ] || [ "$USE_XCODE" -eq "1" ]; then : #^ Remove this condition when it V will start working on gcc - #EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_MEGAGLEST_TESTS=ON" + #EXTRA_CMAKE_OPTIONS="${EXTRA_CMAKE_OPTIONS} -DBUILD_ZETAGLEST_TESTS=ON" #^ Uncomment when it will start working on clang else rm -f ../zetaglest_tests diff --git a/mk/macos/bundle_resources/Info.plist.in b/mk/macos/bundle_resources/Info.plist.in index b01daa05d..9e1835cf4 100644 --- a/mk/macos/bundle_resources/Info.plist.in +++ b/mk/macos/bundle_resources/Info.plist.in @@ -7,11 +7,11 @@ CFBundleExecutable MegaGlest CFBundleGetInfoString - v${MEGAGLEST_VERSION}, © 2018 The ZetaGlest Team. + v${ZETAGLEST_VERSION}, © 2018 The ZetaGlest Team. CFBundleIconFile MegaGlest CFBundleIdentifier - org.megaglest.v${MEGAGLEST_VERSION} + org.megaglest.v${ZETAGLEST_VERSION} CFBundleInfoDictionaryVersion 6.0 CFBundleName diff --git a/mk/windows/LICENSE b/mk/windows/LICENSE deleted file mode 100644 index 94a9ed024..000000000 --- a/mk/windows/LICENSE +++ /dev/null @@ -1,674 +0,0 @@ - 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/mk/windows/README.md b/mk/windows/README.md index d0ddc03d5..891ee7000 100644 --- a/mk/windows/README.md +++ b/mk/windows/README.md @@ -1,5 +1,4 @@ -# Windows -ZetaGlest build system for Windows +# Build ZetaGlest for Windows **To build ZetaGlest for the first time:** @@ -34,16 +33,20 @@ To be able to run ZetaGlest from within Visual Studio using the debugger, you ne 2. Right-click `glest_game` -> `Properties` -> `Debugging` and change `Working Directory` to `$(SolutionDir)$(TargetName)\` Do this also for `g3d_viewer` and `map_editor`. -Configuration in Visual Studio must be left as "Release", otherwise it won't compile, but one can still use debugging options by making the following changes in the project properties: +Configuration in Visual Studio **must be left as `Release`**, and it must match the project architecture as otherwise it won't compile. The settings involved are the following: -Debug: +![VS Image](https://i.imgur.com/UFURP4x.png) + +Although the configuration *must be set as `Release`*, one can still use debugging options by making the following changes in the project properties. + +To manually emulate `Debug`: Inline Function Expansion: Disabled Enable Intrinsic Functions: No Favor Size Or Speed: Neither Preprocessor: DEBUG instead of NDEBUG -Release: +To manually emulate `Release`: Inline Function Expansion: Any Suitable Enable Intrinsic Functions: Yes diff --git a/mk/windows/build-zg.bat b/mk/windows/build-zg.bat index 46a0800e1..27dd72fe5 100644 --- a/mk/windows/build-zg.bat +++ b/mk/windows/build-zg.bat @@ -162,5 +162,13 @@ Echo Building ZetaGlest... cd .\%FOLDER_NAME%\ msbuild.exe /p:Configuration=%MSBUILD_CONFIG%;Platform=%PLATFORM%;PlatformToolset=%TOOLSET% ZetaGlest.sln cd ..\ +ECHO. +ECHO Copying binaries... +ECHO. +call .\group-binaries.bat ECHO ... Done. +ECHO. +ECHO If build was successful, then output is in bin folder, otherwise scroll up a little to view error messages. +ECHO Thanks for trying out ZetaGlest. +ECHO. pause \ No newline at end of file diff --git a/mk/windows/clean-all.bat b/mk/windows/clean-all.bat index 866e5b371..3147a121b 100644 --- a/mk/windows/clean-all.bat +++ b/mk/windows/clean-all.bat @@ -39,6 +39,12 @@ IF EXIST .\vs2017-64 ( cd ..\ ) +IF EXIST .\bin ( + cd .\bin + CALL :DELETE + cd ..\ +) + ROBOCOPY . . /S /MOVE ROBOCOPY . . /S /MOVE ROBOCOPY . . /S /MOVE diff --git a/mk/windows/clean-zg.bat b/mk/windows/clean-zg.bat index 1125120fd..f07705a42 100644 --- a/mk/windows/clean-zg.bat +++ b/mk/windows/clean-zg.bat @@ -33,6 +33,12 @@ IF EXIST .\vs2017-64 ( cd ..\ ) +IF EXIST .\bin ( + cd .\bin + CALL :DELETE + cd ..\ +) + ROBOCOPY . . /S /MOVE ROBOCOPY . . /S /MOVE ROBOCOPY . . /S /MOVE diff --git a/mk/windows/group-binaries.bat b/mk/windows/group-binaries.bat new file mode 100644 index 000000000..31b6f97b9 --- /dev/null +++ b/mk/windows/group-binaries.bat @@ -0,0 +1,37 @@ +@echo off +md bin +md bin\vs2015 +md bin\vs2017 + +copy /y 7z.dll bin\vs2015\7z.dll +copy /y 7z.dll bin\vs2017\7z.dll + +copy /y 7z.exe bin\vs2015\7z.exe +copy /y 7z.exe bin\vs2017\7z.exe + +copy /y glest.ini bin\vs2015\glest.ini +copy /y glest.ini bin\vs2017\glest.ini + +copy /y ..\shared\glestkeys.ini bin\vs2015\glestkeys.ini +copy /y ..\shared\glestkeys.ini bin\vs2017\glestkeys.ini + +copy /y ..\shared\servers.ini bin\vs2015\servers.ini +copy /y ..\shared\servers.ini bin\vs2017\servers.ini + +copy /y vs2015-32\g3dviewerWin32\Release\g3dviewer.exe bin\vs2015\g3dviewer-32.exe +copy /y vs2017-32\g3dviewerWin32\Release\g3dviewer.exe bin\vs2017\g3dviewer-32.exe + +copy /y vs2015-32\map_editorWin32\Release\map_editor.exe bin\vs2015\map_editor-32.exe +copy /y vs2017-32\map_editorWin32\Release\map_editor.exe bin\vs2017\map_editor-32.exe + +copy /y vs2015-32\zetaglest\zetaglest.exe bin\vs2015\zetaglest-32.exe +copy /y vs2017-32\zetaglest\zetaglest.exe bin\vs2017\zetaglest-32.exe + +copy /y vs2015-64\g3dviewerx64\Release\g3dviewer.exe bin\vs2015\g3dviewer-64.exe +copy /y vs2017-64\g3dviewerx64\Release\g3dviewer.exe bin\vs2017\g3dviewer-64.exe + +copy /y vs2015-64\map_editorx64\Release\map_editor.exe bin\vs2015\map_editor-64.exe +copy /y vs2017-64\map_editorx64\Release\map_editor.exe bin\vs2017\map_editor-64.exe + +copy /y vs2015-64\zetaglest\zetaglest.exe bin\vs2015\zetaglest-64.exe +copy /y vs2017-64\zetaglest\zetaglest.exe bin\vs2017\zetaglest-64.exe \ No newline at end of file diff --git a/mk/windows/vs2015-32/.gitignore b/mk/windows/vs2015-32/.gitignore index 18bbedb1a..9db351467 100644 --- a/mk/windows/vs2015-32/.gitignore +++ b/mk/windows/vs2015-32/.gitignore @@ -17,4 +17,6 @@ *.ipch *.pch *.ini -*.obj \ No newline at end of file +*.obj +*.res +*.user \ No newline at end of file diff --git a/mk/windows/vs2015-32/ZetaGlest.sln b/mk/windows/vs2015-32/ZetaGlest.sln index 69ee11b18..552cac21f 100644 --- a/mk/windows/vs2015-32/ZetaGlest.sln +++ b/mk/windows/vs2015-32/ZetaGlest.sln @@ -5,13 +5,13 @@ VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libglest", "libglest.vcxproj", "{8DAA0C24-95CD-4F66-B4C5-19ABDD771746}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glest_game", "glest_game.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zetaglest", "zetaglest.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libstreflop", "libstreflop.vcxproj", "{CDF4DDB9-945E-4D0D-9F0E-2BBEB5D22141}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g2xml", "g2xml.vcxproj", "{407355A4-D12A-4E3B-A7EB-A835E573B376}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g3d_viewer", "g3d_viewer.vcxproj", "{FE5C7C7C-F109-44F5-8329-25A4E24F162C}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g3dviewer", "g3dviewer.vcxproj", "{FE5C7C7C-F109-44F5-8329-25A4E24F162C}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "map_editor", "map_editor.vcxproj", "{5D09BE33-81EC-450B-8A7B-2E7B941ADC56}" EndProject diff --git a/mk/windows/vs2015-32/g2xml.vcxproj b/mk/windows/vs2015-32/g2xml.vcxproj index 5a0740cb7..814bab31a 100644 --- a/mk/windows/vs2015-32/g2xml.vcxproj +++ b/mk/windows/vs2015-32/g2xml.vcxproj @@ -61,7 +61,7 @@ Full true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL true @@ -70,8 +70,8 @@ ProgramDatabase true StreamingSIMDExtensions2 - AnySuitable - Speed + AnySuitable + Speed true diff --git a/mk/windows/vs2015-32/g2xml.vcxproj.user b/mk/windows/vs2015-32/g2xml.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-32/g2xml.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/g3d_viewer.vcxproj.user b/mk/windows/vs2015-32/g3d_viewer.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-32/g3d_viewer.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/g3d_viewerWin32_obj/Release/g3d_viewer.res b/mk/windows/vs2015-32/g3d_viewerWin32_obj/Release/g3d_viewer.res deleted file mode 100644 index 2e34fdc1c..000000000 Binary files a/mk/windows/vs2015-32/g3d_viewerWin32_obj/Release/g3d_viewer.res and /dev/null differ diff --git a/mk/windows/vs2015-32/g3d_viewer.vcxproj b/mk/windows/vs2015-32/g3dviewer.vcxproj similarity index 98% rename from mk/windows/vs2015-32/g3d_viewer.vcxproj rename to mk/windows/vs2015-32/g3dviewer.vcxproj index 47ee0d364..11d86e67d 100644 --- a/mk/windows/vs2015-32/g3d_viewer.vcxproj +++ b/mk/windows/vs2015-32/g3dviewer.vcxproj @@ -12,9 +12,9 @@ {FE5C7C7C-F109-44F5-8329-25A4E24F162C} - g3d_viewer + g3dviewer Win32Proj - g3d_viewer + g3dviewer 8.1 @@ -161,7 +161,7 @@ /arch:SSE2 %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -261,9 +261,9 @@ - - PreserveNewest - + + PreserveNewest + \ No newline at end of file diff --git a/mk/windows/vs2015-32/glest_game.vcxproj.user b/mk/windows/vs2015-32/glest_game.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-32/glest_game.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/libglest.vcxproj.user b/mk/windows/vs2015-32/libglest.vcxproj.user deleted file mode 100644 index 620587b76..000000000 --- a/mk/windows/vs2015-32/libglest.vcxproj.user +++ /dev/null @@ -1,8 +0,0 @@ - - - - $(SolutionDir)\..\dependencies\src\wxwidgets - lib - true - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/libstreflop.vcxproj.user b/mk/windows/vs2015-32/libstreflop.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-32/libstreflop.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/map_editor.vcxproj b/mk/windows/vs2015-32/map_editor.vcxproj index 3b5b545bd..93767f093 100644 --- a/mk/windows/vs2015-32/map_editor.vcxproj +++ b/mk/windows/vs2015-32/map_editor.vcxproj @@ -158,7 +158,7 @@ /arch:SSE2 %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;_CRT_SECURE_NO_WARNINGS;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -250,9 +250,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2015-32/map_editor.vcxproj.user b/mk/windows/vs2015-32/map_editor.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-32/map_editor.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-32/map_editorWin32_obj/Release/glest_editor.res b/mk/windows/vs2015-32/map_editorWin32_obj/Release/glest_editor.res deleted file mode 100644 index 2f0f86c39..000000000 Binary files a/mk/windows/vs2015-32/map_editorWin32_obj/Release/glest_editor.res and /dev/null differ diff --git a/mk/windows/vs2015-32/glest_game.vcxproj b/mk/windows/vs2015-32/zetaglest.vcxproj similarity index 99% rename from mk/windows/vs2015-32/glest_game.vcxproj rename to mk/windows/vs2015-32/zetaglest.vcxproj index 1c5fdaae5..66544fc0f 100644 --- a/mk/windows/vs2015-32/glest_game.vcxproj +++ b/mk/windows/vs2015-32/zetaglest.vcxproj @@ -12,8 +12,8 @@ {6B0C65F1-D031-46AF-AC0D-7C38892D2952} - glest_game - glest_game + zetaglest + zetaglest 8.1 diff --git a/mk/windows/vs2015-32/zetaglestWin32_obj/Release/glest.res b/mk/windows/vs2015-32/zetaglestWin32_obj/Release/glest.res deleted file mode 100644 index 5082ca401..000000000 Binary files a/mk/windows/vs2015-32/zetaglestWin32_obj/Release/glest.res and /dev/null differ diff --git a/mk/windows/vs2015-64/.gitignore b/mk/windows/vs2015-64/.gitignore index 18bbedb1a..9db351467 100644 --- a/mk/windows/vs2015-64/.gitignore +++ b/mk/windows/vs2015-64/.gitignore @@ -17,4 +17,6 @@ *.ipch *.pch *.ini -*.obj \ No newline at end of file +*.obj +*.res +*.user \ No newline at end of file diff --git a/mk/windows/vs2015-64/ZetaGlest.sln b/mk/windows/vs2015-64/ZetaGlest.sln index 967cf1029..bd9d397a4 100644 --- a/mk/windows/vs2015-64/ZetaGlest.sln +++ b/mk/windows/vs2015-64/ZetaGlest.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libglest", "libglest.vcxproj", "{8DAA0C24-95CD-4F66-B4C5-19ABDD771746}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glest_game", "glest_game.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zetaglest", "zetaglest.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libstreflop", "libstreflop.vcxproj", "{CDF4DDB9-945E-4D0D-9F0E-2BBEB5D22141}" EndProject diff --git a/mk/windows/vs2015-64/g2xml.vcxproj b/mk/windows/vs2015-64/g2xml.vcxproj index 333c0bbf4..96b911bea 100644 --- a/mk/windows/vs2015-64/g2xml.vcxproj +++ b/mk/windows/vs2015-64/g2xml.vcxproj @@ -83,7 +83,7 @@ Full true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL true diff --git a/mk/windows/vs2015-64/g2xml.vcxproj.user b/mk/windows/vs2015-64/g2xml.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-64/g2xml.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/g3d_viewer.vcxproj b/mk/windows/vs2015-64/g3d_viewer.vcxproj index 16aa6582f..ceb2325d6 100644 --- a/mk/windows/vs2015-64/g3d_viewer.vcxproj +++ b/mk/windows/vs2015-64/g3d_viewer.vcxproj @@ -239,7 +239,7 @@ %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - NDEBUG;_WINDOWS;WIN32;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) + NDEBUG;_WINDOWS;WIN32;_CRT_SECURE_NO_WARNINGS;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -331,9 +331,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2015-64/g3d_viewer.vcxproj.user b/mk/windows/vs2015-64/g3d_viewer.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-64/g3d_viewer.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/g3dviewerx64_obj/Release/g3d_viewer.res b/mk/windows/vs2015-64/g3dviewerx64_obj/Release/g3d_viewer.res deleted file mode 100644 index 2e34fdc1c..000000000 Binary files a/mk/windows/vs2015-64/g3dviewerx64_obj/Release/g3d_viewer.res and /dev/null differ diff --git a/mk/windows/vs2015-64/glest_game.vcxproj.user b/mk/windows/vs2015-64/glest_game.vcxproj.user deleted file mode 100644 index b5b74823e..000000000 --- a/mk/windows/vs2015-64/glest_game.vcxproj.user +++ /dev/null @@ -1,7 +0,0 @@ - - - - $(SolutionDir)$(TargetName)\ - WindowsLocalDebugger - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/libglest.vcxproj.user b/mk/windows/vs2015-64/libglest.vcxproj.user deleted file mode 100644 index 620587b76..000000000 --- a/mk/windows/vs2015-64/libglest.vcxproj.user +++ /dev/null @@ -1,8 +0,0 @@ - - - - $(SolutionDir)\..\dependencies\src\wxwidgets - lib - true - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/libstreflop.vcxproj.user b/mk/windows/vs2015-64/libstreflop.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-64/libstreflop.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/map_editor.vcxproj b/mk/windows/vs2015-64/map_editor.vcxproj index 96606b543..d419423fd 100644 --- a/mk/windows/vs2015-64/map_editor.vcxproj +++ b/mk/windows/vs2015-64/map_editor.vcxproj @@ -234,7 +234,7 @@ %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;_CRT_SECURE_NO_WARNINGS;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -324,9 +324,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2015-64/map_editor.vcxproj.user b/mk/windows/vs2015-64/map_editor.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2015-64/map_editor.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2015-64/map_editorx64_obj/Release/glest_editor.res b/mk/windows/vs2015-64/map_editorx64_obj/Release/glest_editor.res deleted file mode 100644 index 2f0f86c39..000000000 Binary files a/mk/windows/vs2015-64/map_editorx64_obj/Release/glest_editor.res and /dev/null differ diff --git a/mk/windows/vs2015-64/glest_game.vcxproj b/mk/windows/vs2015-64/zetaglest.vcxproj similarity index 99% rename from mk/windows/vs2015-64/glest_game.vcxproj rename to mk/windows/vs2015-64/zetaglest.vcxproj index 640437a2a..ea0a216b1 100644 --- a/mk/windows/vs2015-64/glest_game.vcxproj +++ b/mk/windows/vs2015-64/zetaglest.vcxproj @@ -12,8 +12,8 @@ {6B0C65F1-D031-46AF-AC0D-7C38892D2952} - glest_game - glest_game + zetaglest + zetaglest 8.1 diff --git a/mk/windows/vs2015-64/zetaglestx64_obj/Release/glest.res b/mk/windows/vs2015-64/zetaglestx64_obj/Release/glest.res deleted file mode 100644 index 5082ca401..000000000 Binary files a/mk/windows/vs2015-64/zetaglestx64_obj/Release/glest.res and /dev/null differ diff --git a/mk/windows/vs2017-32/.gitignore b/mk/windows/vs2017-32/.gitignore index 18bbedb1a..9db351467 100644 --- a/mk/windows/vs2017-32/.gitignore +++ b/mk/windows/vs2017-32/.gitignore @@ -17,4 +17,6 @@ *.ipch *.pch *.ini -*.obj \ No newline at end of file +*.obj +*.res +*.user \ No newline at end of file diff --git a/mk/windows/vs2017-32/ZetaGlest.sln b/mk/windows/vs2017-32/ZetaGlest.sln index 3c6551019..8b216b401 100644 --- a/mk/windows/vs2017-32/ZetaGlest.sln +++ b/mk/windows/vs2017-32/ZetaGlest.sln @@ -5,13 +5,13 @@ VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libglest", "libglest.vcxproj", "{8DAA0C24-95CD-4F66-B4C5-19ABDD771746}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glest_game", "glest_game.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zetaglest", "zetaglest.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libstreflop", "libstreflop.vcxproj", "{CDF4DDB9-945E-4D0D-9F0E-2BBEB5D22141}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g2xml", "g2xml.vcxproj", "{407355A4-D12A-4E3B-A7EB-A835E573B376}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g3d_viewer", "g3d_viewer.vcxproj", "{FE5C7C7C-F109-44F5-8329-25A4E24F162C}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "g3dviewer", "g3dviewer.vcxproj", "{FE5C7C7C-F109-44F5-8329-25A4E24F162C}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "map_editor", "map_editor.vcxproj", "{5D09BE33-81EC-450B-8A7B-2E7B941ADC56}" EndProject diff --git a/mk/windows/vs2017-32/g2xml.vcxproj b/mk/windows/vs2017-32/g2xml.vcxproj index c6b2b7782..142052261 100644 --- a/mk/windows/vs2017-32/g2xml.vcxproj +++ b/mk/windows/vs2017-32/g2xml.vcxproj @@ -61,7 +61,7 @@ Full true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL true diff --git a/mk/windows/vs2017-32/g2xml.vcxproj.user b/mk/windows/vs2017-32/g2xml.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-32/g2xml.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/g3d_viewer.vcxproj.user b/mk/windows/vs2017-32/g3d_viewer.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-32/g3d_viewer.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/g3d_viewerWin32_obj/Release/g3d_viewer.res b/mk/windows/vs2017-32/g3d_viewerWin32_obj/Release/g3d_viewer.res deleted file mode 100644 index 2e34fdc1c..000000000 Binary files a/mk/windows/vs2017-32/g3d_viewerWin32_obj/Release/g3d_viewer.res and /dev/null differ diff --git a/mk/windows/vs2017-32/g3d_viewer.vcxproj b/mk/windows/vs2017-32/g3dviewer.vcxproj similarity index 98% rename from mk/windows/vs2017-32/g3d_viewer.vcxproj rename to mk/windows/vs2017-32/g3dviewer.vcxproj index 8b8f17488..eaea90ea8 100644 --- a/mk/windows/vs2017-32/g3d_viewer.vcxproj +++ b/mk/windows/vs2017-32/g3dviewer.vcxproj @@ -12,9 +12,9 @@ {FE5C7C7C-F109-44F5-8329-25A4E24F162C} - g3d_viewer + g3dviewer Win32Proj - g3d_viewer + g3dviewer 8.1 @@ -161,7 +161,7 @@ /arch:SSE2 %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -256,9 +256,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2017-32/glest_game.vcxproj.user b/mk/windows/vs2017-32/glest_game.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-32/glest_game.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/libglest.vcxproj.user b/mk/windows/vs2017-32/libglest.vcxproj.user deleted file mode 100644 index 620587b76..000000000 --- a/mk/windows/vs2017-32/libglest.vcxproj.user +++ /dev/null @@ -1,8 +0,0 @@ - - - - $(SolutionDir)\..\dependencies\src\wxwidgets - lib - true - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/libstreflop.vcxproj.user b/mk/windows/vs2017-32/libstreflop.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-32/libstreflop.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/map_editor.vcxproj b/mk/windows/vs2017-32/map_editor.vcxproj index d35ced090..baa298d14 100644 --- a/mk/windows/vs2017-32/map_editor.vcxproj +++ b/mk/windows/vs2017-32/map_editor.vcxproj @@ -158,7 +158,7 @@ /arch:SSE2 %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -250,9 +250,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2017-32/map_editor.vcxproj.user b/mk/windows/vs2017-32/map_editor.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-32/map_editor.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-32/map_editorWin32_obj/Release/glest_editor.res b/mk/windows/vs2017-32/map_editorWin32_obj/Release/glest_editor.res deleted file mode 100644 index 2f0f86c39..000000000 Binary files a/mk/windows/vs2017-32/map_editorWin32_obj/Release/glest_editor.res and /dev/null differ diff --git a/mk/windows/vs2017-32/glest_game.vcxproj b/mk/windows/vs2017-32/zetaglest.vcxproj similarity index 99% rename from mk/windows/vs2017-32/glest_game.vcxproj rename to mk/windows/vs2017-32/zetaglest.vcxproj index a53a961fc..fcc027c6c 100644 --- a/mk/windows/vs2017-32/glest_game.vcxproj +++ b/mk/windows/vs2017-32/zetaglest.vcxproj @@ -12,8 +12,8 @@ {6B0C65F1-D031-46AF-AC0D-7C38892D2952} - glest_game - glest_game + zetaglest + zetaglest 8.1 diff --git a/mk/windows/vs2017-32/zetaglestWin32_obj/Release/glest.res b/mk/windows/vs2017-32/zetaglestWin32_obj/Release/glest.res deleted file mode 100644 index 5082ca401..000000000 Binary files a/mk/windows/vs2017-32/zetaglestWin32_obj/Release/glest.res and /dev/null differ diff --git a/mk/windows/vs2017-64/.gitignore b/mk/windows/vs2017-64/.gitignore index 18bbedb1a..9db351467 100644 --- a/mk/windows/vs2017-64/.gitignore +++ b/mk/windows/vs2017-64/.gitignore @@ -17,4 +17,6 @@ *.ipch *.pch *.ini -*.obj \ No newline at end of file +*.obj +*.res +*.user \ No newline at end of file diff --git a/mk/windows/vs2017-64/ZetaGlest.sln b/mk/windows/vs2017-64/ZetaGlest.sln index 0b4752b65..d48e560c2 100644 --- a/mk/windows/vs2017-64/ZetaGlest.sln +++ b/mk/windows/vs2017-64/ZetaGlest.sln @@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.27130.2036 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libglest", "libglest.vcxproj", "{8DAA0C24-95CD-4F66-B4C5-19ABDD771746}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "glest_game", "glest_game.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "zetaglest", "zetaglest.vcxproj", "{6B0C65F1-D031-46AF-AC0D-7C38892D2952}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libstreflop", "libstreflop.vcxproj", "{CDF4DDB9-945E-4D0D-9F0E-2BBEB5D22141}" EndProject diff --git a/mk/windows/vs2017-64/g2xml.vcxproj b/mk/windows/vs2017-64/g2xml.vcxproj index ac32583a6..6bb7a5f1a 100644 --- a/mk/windows/vs2017-64/g2xml.vcxproj +++ b/mk/windows/vs2017-64/g2xml.vcxproj @@ -83,7 +83,7 @@ Full true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) MultiThreadedDLL true diff --git a/mk/windows/vs2017-64/g2xml.vcxproj.user b/mk/windows/vs2017-64/g2xml.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-64/g2xml.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/g3d_viewer.vcxproj b/mk/windows/vs2017-64/g3d_viewer.vcxproj index a3d08020a..b57702cea 100644 --- a/mk/windows/vs2017-64/g3d_viewer.vcxproj +++ b/mk/windows/vs2017-64/g3d_viewer.vcxproj @@ -239,7 +239,7 @@ %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - NDEBUG;_WINDOWS;WIN32;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) + NDEBUG;_WINDOWS;WIN32;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -331,9 +331,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2017-64/g3d_viewer.vcxproj.user b/mk/windows/vs2017-64/g3d_viewer.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-64/g3d_viewer.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/g3dviewerx64_obj/Release/g3d_viewer.res b/mk/windows/vs2017-64/g3dviewerx64_obj/Release/g3d_viewer.res deleted file mode 100644 index 2e34fdc1c..000000000 Binary files a/mk/windows/vs2017-64/g3dviewerx64_obj/Release/g3d_viewer.res and /dev/null differ diff --git a/mk/windows/vs2017-64/glest_game.vcxproj.user b/mk/windows/vs2017-64/glest_game.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-64/glest_game.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/libglest.vcxproj.user b/mk/windows/vs2017-64/libglest.vcxproj.user deleted file mode 100644 index 620587b76..000000000 --- a/mk/windows/vs2017-64/libglest.vcxproj.user +++ /dev/null @@ -1,8 +0,0 @@ - - - - $(SolutionDir)\..\dependencies\src\wxwidgets - lib - true - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/libstreflop.vcxproj.user b/mk/windows/vs2017-64/libstreflop.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-64/libstreflop.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/map_editor.vcxproj b/mk/windows/vs2017-64/map_editor.vcxproj index 7ee8abb21..f3b2271a8 100644 --- a/mk/windows/vs2017-64/map_editor.vcxproj +++ b/mk/windows/vs2017-64/map_editor.vcxproj @@ -234,7 +234,7 @@ %(AdditionalOptions) ..\dependencies\src\wxwidgets\include;..\..\..\source\shared_lib\include\util;..\..\..\source\shared_lib\include\graphics\gl;..\..\..\source\shared_lib\include\graphics;../dependencies/src/wxwidgets/include;..\..\..\source\shared_lib\include\platform\sdl;../../../source/shared_lib/include/platform/common;..\dependencies\src\sdl2\include;../dependencies/src/curl/include;..\..\..\source\glest_game\graphics;..\..\..\source\shared_lib\include\xml;../dependencies/src/xerces/src;..\..\..\source\glest_game\global;..\..\..\source\glest_game\sound;..\..\..\source\shared_lib\include\sound;..\..\..\source\glest_game\game;../../../source/shared_lib/include/map;../dependencies/src/png;..\..\..\source\glest_game\facilities;../dependencies/src/glew/include;..\..\..\source\shared_lib\include\xml\rapidxml;../../../source/shared_lib/include/platform/win32;..\dependencies\src\wxwidgets\lib\vc_lib\mswu;%(AdditionalIncludeDirectories) - _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;%(PreprocessorDefinitions) + _WINDOWS;WIN32;NDEBUG;CURL_STATICLIB;UNICODE;_UNICODE;GLEW_STATIC;ZLIB_WINAPI;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) true Async MultiThreadedDLL @@ -324,9 +324,9 @@ - - PreserveNewest - + + PreserveNewest + diff --git a/mk/windows/vs2017-64/map_editor.vcxproj.user b/mk/windows/vs2017-64/map_editor.vcxproj.user deleted file mode 100644 index abe8dd896..000000000 --- a/mk/windows/vs2017-64/map_editor.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/mk/windows/vs2017-64/map_editorx64_obj/Release/glest_editor.res b/mk/windows/vs2017-64/map_editorx64_obj/Release/glest_editor.res deleted file mode 100644 index 2f0f86c39..000000000 Binary files a/mk/windows/vs2017-64/map_editorx64_obj/Release/glest_editor.res and /dev/null differ diff --git a/mk/windows/vs2017-64/glest_game.vcxproj b/mk/windows/vs2017-64/zetaglest.vcxproj similarity index 99% rename from mk/windows/vs2017-64/glest_game.vcxproj rename to mk/windows/vs2017-64/zetaglest.vcxproj index 8724fe2fe..1e3e97557 100644 --- a/mk/windows/vs2017-64/glest_game.vcxproj +++ b/mk/windows/vs2017-64/zetaglest.vcxproj @@ -12,8 +12,8 @@ {6B0C65F1-D031-46AF-AC0D-7C38892D2952} - glest_game - glest_game + zetaglest + zetaglest 8.1 diff --git a/mk/windows/vs2017-64/zetaglestx64_obj/Release/glest.res b/mk/windows/vs2017-64/zetaglestx64_obj/Release/glest.res deleted file mode 100644 index 5082ca401..000000000 Binary files a/mk/windows/vs2017-64/zetaglestx64_obj/Release/glest.res and /dev/null differ diff --git a/rss_23x23.jpeg b/rss_23x23.jpeg new file mode 100644 index 000000000..a496a9184 Binary files /dev/null and b/rss_23x23.jpeg differ diff --git a/source/g3d_viewer/CMakeLists.txt b/source/g3d_viewer/CMakeLists.txt index affc4be86..6126d20f0 100644 --- a/source/g3d_viewer/CMakeLists.txt +++ b/source/g3d_viewer/CMakeLists.txt @@ -4,7 +4,7 @@ SET(EXTERNAL_LIBS "") SET(TARGET_NAME "zetaglest_g3dviewer") SET(TARGET_NAME_MANPAGE "${TARGET_NAME}.6") -IF(BUILD_MEGAGLEST_MODEL_VIEWER) +IF(BUILD_ZETAGLEST_MODEL_VIEWER) MESSAGE(STATUS "Will try to build ZetaGlest model viewer") IF(UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND NOT "${CMAKE_SYSTEM_NAME}" STREQUAL "Linux") @@ -187,7 +187,7 @@ IF(BUILD_MEGAGLEST_MODEL_VIEWER) ENDFOREACH(DIR) IF(WANT_DEV_OUTPATH) - SET(EXECUTABLE_OUTPUT_PATH "${MEGAGLEST_FRIENDLY_OUTPUT_PATH}") + SET(EXECUTABLE_OUTPUT_PATH "${ZETAGLEST_FRIENDLY_OUTPUT_PATH}") ENDIF() SET(HELP2MAN_OUT_PATH ${EXECUTABLE_OUTPUT_PATH}) @@ -234,20 +234,20 @@ IF(BUILD_MEGAGLEST_MODEL_VIEWER) # Installation of the program INSTALL(TARGETS ${TARGET_NAME} - DESTINATION ${MEGAGLEST_BIN_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_BIN_INSTALL_PATH}) - IF(EXISTS ${HELP2MAN} AND NOT "${MEGAGLEST_MANPAGE_INSTALL_PATH}" STREQUAL "" AND NOT APPLE) - MESSAGE(STATUS "**NOTE: Will Install manpage [${TARGET_NAME_MANPAGE}] to [${MEGAGLEST_MANPAGE_INSTALL_PATH}]") + IF(EXISTS ${HELP2MAN} AND NOT "${ZETAGLEST_MANPAGE_INSTALL_PATH}" STREQUAL "" AND NOT APPLE) + MESSAGE(STATUS "**NOTE: Will Install manpage [${TARGET_NAME_MANPAGE}] to [${ZETAGLEST_MANPAGE_INSTALL_PATH}]") # Installation of the program manpage file INSTALL(FILES "${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE}" - DESTINATION ${MEGAGLEST_MANPAGE_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_MANPAGE_INSTALL_PATH}) ENDIF() # Installation of the program config and image files INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/shared/g3dviewer.ico" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) ENDIF() ENDIF() diff --git a/source/g3d_viewer/main.cpp b/source/g3d_viewer/main.cpp index eccbfaabe..ebd52ce1b 100644 --- a/source/g3d_viewer/main.cpp +++ b/source/g3d_viewer/main.cpp @@ -43,15 +43,15 @@ #endif #ifndef WIN32 - #define stricmp strcasecmp - #define strnicmp strncasecmp - #define _strnicmp strncasecmp +#define stricmp strcasecmp +#define strnicmp strncasecmp +#define _strnicmp strncasecmp #endif #if wxCHECK_VERSION(2, 9, 1) - #define WX2CHR(x) (x.mb_str()) +#define WX2CHR(x) (x.mb_str()) #else - #define WX2CHR(x) (wxConvCurrent->cWX2MB(x)) +#define WX2CHR(x) (wxConvCurrent->cWX2MB(x)) #endif using namespace Shared::Platform; @@ -73,395 +73,398 @@ const char *folderDelimiter = "/"; //int GameConstants::updateFps= 40; //int GameConstants::cameraFps= 100; -const string g3dviewerVersionString= "v0.8.01"; +const string g3dviewerVersionString = "v0.8.01"; // Because g3d should always support alpha transparency string fileFormat = "png"; -namespace Glest { namespace Game { +namespace Glest { + namespace Game { -string getGameReadWritePath(const string &lookupKey) { - string path = ""; - if(path == "" && getenv("GLESTHOME") != NULL) { - path = safeCharPtrCopy(getenv("GLESTHOME"),8096); - if(path != "" && EndsWith(path, "/") == false && EndsWith(path, "\\") == false) { - path += "/"; - } + string getGameReadWritePath(const string &lookupKey) { + string path = ""; + if (path == "" && getenv("GLESTHOME") != NULL) { + path = safeCharPtrCopy(getenv("GLESTHOME"), 8096); + if (path != "" && EndsWith(path, "/") == false && EndsWith(path, "\\") == false) { + path += "/"; + } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); - } - - return path; -} -}} - -namespace Shared{ namespace G3dViewer{ - -// =============================================== -// class Global functions -// =============================================== - -wxString ToUnicode(const char* str) { - return wxString(str, wxConvUTF8); -} - -wxString ToUnicode(const string& str) { - return wxString(str.c_str(), wxConvUTF8); -} - -const wxChar *GAME_ARGS[] = { - wxT("--help"), - wxT("--auto-screenshot"), - wxT("--load-unit"), - wxT("--load-model"), - wxT("--load-model-animation-value"), - wxT("--load-particle"), - wxT("--load-projectile-particle"), - wxT("--load-splash-particle"), - wxT("--load-particle-loop-value"), - wxT("--zoom-value"), - wxT("--rotate-x-value"), - wxT("--rotate-y-value"), - wxT("--screenshot-format"), - wxT("--verbose"), -}; - -enum GAME_ARG_TYPE { - GAME_ARG_HELP = 0, - GAME_ARG_AUTO_SCREENSHOT, - GAME_ARG_LOAD_UNIT, - GAME_ARG_LOAD_MODEL, - GAME_ARG_LOAD_MODEL_ANIMATION_VALUE, - GAME_ARG_LOAD_PARTICLE, - GAME_ARG_LOAD_PARTICLE_PROJECTILE, - GAME_ARG_LOAD_PARTICLE_SPLASH, - GAME_ARG_LOAD_PARTICLE_LOOP_VALUE, - GAME_ARG_ZOOM_VALUE, - GAME_ARG_ROTATE_X_VALUE, - GAME_ARG_ROTATE_Y_VALUE, - GAME_ARG_SCREENSHOT_FORMAT, - GAME_ARG_VERBOSE, -}; - -bool hasCommandArgument(int argc, wxChar** argv,const string &argName, - int *foundIndex=NULL, int startLookupIndex=1, - bool useArgParamLen=false) { - bool result = false; - - if(foundIndex != NULL) { - *foundIndex = -1; - } - size_t compareLen = strlen(argName.c_str()); - - for(int idx = startLookupIndex; idx < argc; idx++) { - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx]); - //printf("tmp_buf [%s]\n",(const char *)tmp_buf); - if(useArgParamLen == true) { - compareLen = strlen(tmp_buf); - } - if(_strnicmp(argName.c_str(),tmp_buf,compareLen) == 0) { - result = true; - if(foundIndex != NULL) { - *foundIndex = idx; + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); } - break; + return path; } } - return result; } -void printParameterHelp(const char *argv0, bool foundInvalidArgs) { - if(foundInvalidArgs == true) { - printf("\n"); - } +namespace Shared { + namespace G3dViewer { - // "================================================================================" - printf("\n%s %s, [Using %s]\n",extractFileFromDirectoryPath(argv0).c_str(),g3dviewerVersionString.c_str(),(const char *)wxConvCurrent->cWX2MB(wxVERSION_STRING)); + // =============================================== + // class Global functions + // =============================================== - printf("\nDisplays glest 3D-models and unit/projectile/splash particle systems.\n"); - printf("\nRotate with left mouse button. Zoom with right mouse button or mousewheel."); - printf("\nUse ctrl to load more than one particle system."); - printf("\nPress R to restart particles, this also reloads all files if they are changed."); + wxString ToUnicode(const char* str) { + return wxString(str, wxConvUTF8); + } - //printf("\n\noptionally you may use any of the following:\n"); - printf("\n\n%s [G3D FILE], usage",extractFileFromDirectoryPath(argv0).c_str()); - printf("\n\nCommandline Parameter: Description:"); - printf("\n\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); - printf("\n\n%s \t\tDisplays this help text.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_HELP])); + wxString ToUnicode(const string& str) { + return wxString(str.c_str(), wxConvUTF8); + } - // "================================================================================" - printf("\n\n%s=x \t\tAuto load the unit / skill information specified",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])); - printf("\n\n \tin path/filename x."); - printf("\n\n \tWhere x is a g3d filename to load separated with a"); - printf("\n\n \tcomma and one or more skill names to try loading."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=techs/megapack/factions/",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])); - printf("\n\n \ttech/units/battle_machine,attack_skill,stop_skill"); + const wxChar *GAME_ARGS[] = { + wxT("--help"), + wxT("--auto-screenshot"), + wxT("--load-unit"), + wxT("--load-model"), + wxT("--load-model-animation-value"), + wxT("--load-particle"), + wxT("--load-projectile-particle"), + wxT("--load-splash-particle"), + wxT("--load-particle-loop-value"), + wxT("--zoom-value"), + wxT("--rotate-x-value"), + wxT("--rotate-y-value"), + wxT("--screenshot-format"), + wxT("--verbose"), + }; - printf("\n\n%s=x \tAuto load the model specified in path/filename x.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); - printf("\n\n \tWhere x is a g3d filename to load."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=techs/megapack/factions/",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); - printf("\n\n \ttech/units/battle_machine/models/battle_machine_dying.g3d"); + enum GAME_ARG_TYPE { + GAME_ARG_HELP = 0, + GAME_ARG_AUTO_SCREENSHOT, + GAME_ARG_LOAD_UNIT, + GAME_ARG_LOAD_MODEL, + GAME_ARG_LOAD_MODEL_ANIMATION_VALUE, + GAME_ARG_LOAD_PARTICLE, + GAME_ARG_LOAD_PARTICLE_PROJECTILE, + GAME_ARG_LOAD_PARTICLE_SPLASH, + GAME_ARG_LOAD_PARTICLE_LOOP_VALUE, + GAME_ARG_ZOOM_VALUE, + GAME_ARG_ROTATE_X_VALUE, + GAME_ARG_ROTATE_Y_VALUE, + GAME_ARG_SCREENSHOT_FORMAT, + GAME_ARG_VERBOSE, + }; - printf("\n\n%s=x ",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])); - printf("\n\n \tAnimation value when loading a model."); - printf("\n\n \tWhere x is a decimal value from -1.0 to 1.0"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=0.5",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])); + bool hasCommandArgument(int argc, wxChar** argv, const string &argName, + int *foundIndex = NULL, int startLookupIndex = 1, + bool useArgParamLen = false) { + bool result = false; - // "================================================================================" - printf("\n\n%s=x \tAutomatically takes a screenshot of the items you",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])); - printf("\n\n \tare loading."); - printf("\n\n \tWhere x is a comma-delimited list of one or more"); - printf("\n\n \t of the optional settings:"); - printf("\n\n \t transparent, enable_grid, enable_wireframe,"); - printf("\n\n \t enable_normals, disable_grid, disable_wireframe,"); - printf("\n\n \t disable_normals, saveas-, resize-wxh"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=transparent,",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])); - printf("\n\n \tdisable_grid,saveas-test.png,resize-800x600"); - printf("\n\n \t%s=techs/megapack/factions/tech/units/",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); - printf("\n\n \tbattle_machine/models/battle_machine_dying.g3d"); + if (foundIndex != NULL) { + *foundIndex = -1; + } + size_t compareLen = strlen(argName.c_str()); - // "================================================================================" - printf("\n\n%s=x \tAuto load the particle specified in path/filename x.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])); - printf("\n\n \tWhere x is a Particle XML filename to load."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=techs/megapack/factions/",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])); - printf("\n\n \tpersian/units/genie/glow_particles.xml"); + for (int idx = startLookupIndex; idx < argc; idx++) { + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx]); + //printf("tmp_buf [%s]\n",(const char *)tmp_buf); + if (useArgParamLen == true) { + compareLen = strlen(tmp_buf); + } + if (_strnicmp(argName.c_str(), tmp_buf, compareLen) == 0) { + result = true; + if (foundIndex != NULL) { + *foundIndex = idx; + } - printf("\n\n%s=x ",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])); - printf("\n\n \tAuto load the projectile particle specified in"); - printf("\n\n \tpath/filename x."); - printf("\n\n \tWhere x is a Projectile Particle Definition XML"); - printf("\n\n \t filename to load."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=techs/megapack/",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])); - printf("\n\n \tfactions/persian/units/genie/particle_proj.xml"); + break; + } + } + return result; + } - printf("\n\n%s=x ",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])); - printf("\n\n \tAuto load the splash particle specified in"); - printf("\n\n \tpath/filename x."); - printf("\n\n \tWhere x is a Splash Particle Definition XML"); - printf("\n\n \t filename to load."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=techs/megapack/",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])); - printf("\n\n \tfactions/persian/units/genie/particle_splash.xml"); + void printParameterHelp(const char *argv0, bool foundInvalidArgs) { + if (foundInvalidArgs == true) { + printf("\n"); + } - printf("\n\n%s=x ",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])); - printf("\n\n \tParticle loop value when loading one or more"); - printf("\n\n \tparticles."); - printf("\n\n \tWhere x is an integer value from 1 to particle count."); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=25",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])); + // "================================================================================" + printf("\n%s %s, [Using %s]\n", extractFileFromDirectoryPath(argv0).c_str(), g3dviewerVersionString.c_str(), (const char *) wxConvCurrent->cWX2MB(wxVERSION_STRING)); - printf("\n\n%s=x \tZoom value when loading a model.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])); - printf("\n\n \tWhere x is a decimal value from 0.1 to 10.0"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=4.2",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])); + printf("\nDisplays glest 3D-models and unit/projectile/splash particle systems.\n"); + printf("\nRotate with left mouse button. Zoom with right mouse button or mousewheel."); + printf("\nUse ctrl to load more than one particle system."); + printf("\nPress R to restart particles, this also reloads all files if they are changed."); - printf("\n\n%s=x \tX Coordinate Rotation value when loading a model.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])); - printf("\n\n \tWhere x is a decimal value from -10.0 to 10.0"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=2.2",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])); + //printf("\n\noptionally you may use any of the following:\n"); + printf("\n\n%s [G3D FILE], usage", extractFileFromDirectoryPath(argv0).c_str()); + printf("\n\nCommandline Parameter: Description:"); + printf("\n\n- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"); + printf("\n\n%s \t\tDisplays this help text.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_HELP])); - printf("\n\n%s=x \tY Coordinate Rotation value when loading a model.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])); - printf("\n\n \tWhere x is a decimal value from -10.0 to 10.0"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=2.2",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])); + // "================================================================================" + printf("\n\n%s=x \t\tAuto load the unit / skill information specified", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])); + printf("\n\n \tin path/filename x."); + printf("\n\n \tWhere x is a g3d filename to load separated with a"); + printf("\n\n \tcomma and one or more skill names to try loading."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=techs/megapack/factions/", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])); + printf("\n\n \ttech/units/battle_machine,attack_skill,stop_skill"); - printf("\n\n%s=x \tSpecify which image format to use for screenshots.",(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])); - printf("\n\n \tWhere x is one of the following supported formats:"); - printf("\n\n \t png,jpg,tga,bmp"); - printf("\n\n \t*NOTE: png is the default (and supports transparency)"); - printf("\n\n \texample:"); - printf("\n\n \t%s %s=jpg",extractFileFromDirectoryPath(argv0).c_str(),(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])); + printf("\n\n%s=x \tAuto load the model specified in path/filename x.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); + printf("\n\n \tWhere x is a g3d filename to load."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=techs/megapack/factions/", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); + printf("\n\n \ttech/units/battle_machine/models/battle_machine_dying.g3d"); - printf("\n\n"); -} + printf("\n\n%s=x ", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])); + printf("\n\n \tAnimation value when loading a model."); + printf("\n\n \tWhere x is a decimal value from -1.0 to 1.0"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=0.5", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])); -bool autoScreenShotAndExit = false; -vector autoScreenShotParams; -std::pair overrideSize(0,0); + // "================================================================================" + printf("\n\n%s=x \tAutomatically takes a screenshot of the items you", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])); + printf("\n\n \tare loading."); + printf("\n\n \tWhere x is a comma-delimited list of one or more"); + printf("\n\n \t of the optional settings:"); + printf("\n\n \t transparent, enable_grid, enable_wireframe,"); + printf("\n\n \t enable_normals, disable_grid, disable_wireframe,"); + printf("\n\n \t disable_normals, saveas-, resize-wxh"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=transparent,", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])); + printf("\n\n \tdisable_grid,saveas-test.png,resize-800x600"); + printf("\n\n \t%s=techs/megapack/factions/tech/units/", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])); + printf("\n\n \tbattle_machine/models/battle_machine_dying.g3d"); -// =============================================== -// class MainWindow -// =============================================== + // "================================================================================" + printf("\n\n%s=x \tAuto load the particle specified in path/filename x.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])); + printf("\n\n \tWhere x is a Particle XML filename to load."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=techs/megapack/factions/", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])); + printf("\n\n \tpersian/units/genie/glow_particles.xml"); -const string MainWindow::winHeader= "G3D viewer " + g3dviewerVersionString; + printf("\n\n%s=x ", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])); + printf("\n\n \tAuto load the projectile particle specified in"); + printf("\n\n \tpath/filename x."); + printf("\n\n \tWhere x is a Projectile Particle Definition XML"); + printf("\n\n \t filename to load."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=techs/megapack/", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])); + printf("\n\n \tfactions/persian/units/genie/particle_proj.xml"); -const float defaultspeed = 0.025f; + printf("\n\n%s=x ", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])); + printf("\n\n \tAuto load the splash particle specified in"); + printf("\n\n \tpath/filename x."); + printf("\n\n \tWhere x is a Splash Particle Definition XML"); + printf("\n\n \t filename to load."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=techs/megapack/", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])); + printf("\n\n \tfactions/persian/units/genie/particle_splash.xml"); -MainWindow::MainWindow( std::pair > unitToLoad, - const string modelPath, - const string particlePath, - const string projectileParticlePath, - const string splashParticlePath, - float defaultAnimation, - int defaultParticleLoopStart, - float defaultZoom,float defaultXRot, float defaultYRot, - string appPath) - : wxFrame(NULL, -1, ToUnicode(winHeader), - wxPoint(Renderer::windowX, Renderer::windowY), - wxSize(Renderer::windowW, Renderer::windowH)), - glCanvas(NULL), - renderer(NULL), - timer(NULL), - menu(NULL), - fileDialog(NULL), - colorPicker(NULL), - model(NULL), - initTextureManager(true), - startupSettingsInited(false) + printf("\n\n%s=x ", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])); + printf("\n\n \tParticle loop value when loading one or more"); + printf("\n\n \tparticles."); + printf("\n\n \tWhere x is an integer value from 1 to particle count."); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=25", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])); -{ - this->appPath = appPath; - Properties::setApplicationPath(executable_path(appPath)); + printf("\n\n%s=x \tZoom value when loading a model.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])); + printf("\n\n \tWhere x is a decimal value from 0.1 to 10.0"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=4.2", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])); - lastanim = 0; - model= NULL; + printf("\n\n%s=x \tX Coordinate Rotation value when loading a model.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])); + printf("\n\n \tWhere x is a decimal value from -10.0 to 10.0"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=2.2", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])); - Config &config = Config::getInstance(); + printf("\n\n%s=x \tY Coordinate Rotation value when loading a model.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])); + printf("\n\n \tWhere x is a decimal value from -10.0 to 10.0"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=2.2", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])); - isControlKeyPressed = false; + printf("\n\n%s=x \tSpecify which image format to use for screenshots.", (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])); + printf("\n\n \tWhere x is one of the following supported formats:"); + printf("\n\n \t png,jpg,tga,bmp"); + printf("\n\n \t*NOTE: png is the default (and supports transparency)"); + printf("\n\n \texample:"); + printf("\n\n \t%s %s=jpg", extractFileFromDirectoryPath(argv0).c_str(), (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])); - initGlCanvas(); + printf("\n\n"); + } + + bool autoScreenShotAndExit = false; + vector autoScreenShotParams; + std::pair overrideSize(0, 0); + + // =============================================== + // class MainWindow + // =============================================== + + const string MainWindow::winHeader = "G3D viewer " + g3dviewerVersionString; + + const float defaultspeed = 0.025f; + + MainWindow::MainWindow(std::pair > unitToLoad, + const string modelPath, + const string particlePath, + const string projectileParticlePath, + const string splashParticlePath, + float defaultAnimation, + int defaultParticleLoopStart, + float defaultZoom, float defaultXRot, float defaultYRot, + string appPath) + : wxFrame(NULL, -1, ToUnicode(winHeader), + wxPoint(Renderer::windowX, Renderer::windowY), + wxSize(Renderer::windowW, Renderer::windowH)), + glCanvas(NULL), + renderer(NULL), + timer(NULL), + menu(NULL), + fileDialog(NULL), + colorPicker(NULL), + model(NULL), + initTextureManager(true), + startupSettingsInited(false) + + { + this->appPath = appPath; + Properties::setApplicationPath(executable_path(appPath)); + + lastanim = 0; + model = NULL; + + Config &config = Config::getInstance(); + + isControlKeyPressed = false; + + initGlCanvas(); #if wxCHECK_VERSION(2, 9, 1) #else - glCanvas->SetCurrent(); + glCanvas->SetCurrent(); #endif - unitPath = unitToLoad; + unitPath = unitToLoad; - if(modelPath != "") { - this->modelPathList.push_back(modelPath); - printf("Startup Adding model [%s] list size " MG_SIZE_T_SPECIFIER "\n",modelPath.c_str(),this->modelPathList.size()); - } - if(particlePath != "") { - this->particlePathList.push_back(particlePath); - } - if(projectileParticlePath != "") { - this->particleProjectilePathList.push_back(projectileParticlePath); - } - if(splashParticlePath != "") { - this->particleSplashPathList.push_back(splashParticlePath); - } + if (modelPath != "") { + this->modelPathList.push_back(modelPath); + printf("Startup Adding model [%s] list size " MG_SIZE_T_SPECIFIER "\n", modelPath.c_str(), this->modelPathList.size()); + } + if (particlePath != "") { + this->particlePathList.push_back(particlePath); + } + if (projectileParticlePath != "") { + this->particleProjectilePathList.push_back(projectileParticlePath); + } + if (splashParticlePath != "") { + this->particleSplashPathList.push_back(splashParticlePath); + } - resetAnimation = false; - anim = defaultAnimation; - particleLoopStart = defaultParticleLoopStart; - resetAnim = anim; - resetParticleLoopStart = particleLoopStart; - rotX= defaultXRot; - rotY= defaultYRot; - zoom= defaultZoom; - playerColor= Renderer::pcRed; + resetAnimation = false; + anim = defaultAnimation; + particleLoopStart = defaultParticleLoopStart; + resetAnim = anim; + resetParticleLoopStart = particleLoopStart; + rotX = defaultXRot; + rotY = defaultYRot; + zoom = defaultZoom; + playerColor = Renderer::pcRed; - speed= defaultspeed; + speed = defaultspeed; - //getGlPlatformExtensions(); - menu= new wxMenuBar(); + //getGlPlatformExtensions(); + menu = new wxMenuBar(); - //menu - menuFile= new wxMenu(); - menuFile->Append(miFileLoad, wxT("&Load G3d model\tCtrl+L"), wxT("Load 3D model")); - menuFile->Append(miFileLoadParticleXML, wxT("Load &Particle XML\tCtrl+P"), wxT("Press ctrl before menu for keeping current particles")); - menuFile->Append(miFileLoadProjectileParticleXML, wxT("Load Pro&jectile Particle XML\tCtrl+J"), wxT("Press ctrl before menu for keeping current projectile particles")); - menuFile->Append(miFileLoadSplashParticleXML, wxT("Load &Splash Particle XML\tCtrl+S"), wxT("Press ctrl before menu for keeping current splash particles")); - menuFile->Append(miFileClearAll, wxT("&Clear All\tCtrl+C")); - menuFile->AppendCheckItem(miFileToggleScreenshotTransparent, wxT("&Transparent Screenshots\tCtrl+T")); - menuFile->Append(miFileSaveScreenshot, wxT("Sa&ve a Screenshot\tCtrl+V")); - menuFile->AppendSeparator(); - menuFile->Append(wxID_EXIT); - menu->Append(menuFile, wxT("&File")); + //menu + menuFile = new wxMenu(); + menuFile->Append(miFileLoad, wxT("&Load G3d model\tCtrl+L"), wxT("Load 3D model")); + menuFile->Append(miFileLoadParticleXML, wxT("Load &Particle XML\tCtrl+P"), wxT("Press ctrl before menu for keeping current particles")); + menuFile->Append(miFileLoadProjectileParticleXML, wxT("Load Pro&jectile Particle XML\tCtrl+J"), wxT("Press ctrl before menu for keeping current projectile particles")); + menuFile->Append(miFileLoadSplashParticleXML, wxT("Load &Splash Particle XML\tCtrl+S"), wxT("Press ctrl before menu for keeping current splash particles")); + menuFile->Append(miFileClearAll, wxT("&Clear All\tCtrl+C")); + menuFile->AppendCheckItem(miFileToggleScreenshotTransparent, wxT("&Transparent Screenshots\tCtrl+T")); + menuFile->Append(miFileSaveScreenshot, wxT("Sa&ve a Screenshot\tCtrl+V")); + menuFile->AppendSeparator(); + menuFile->Append(wxID_EXIT); + menu->Append(menuFile, wxT("&File")); - //mode - menuMode= new wxMenu(); - menuMode->AppendCheckItem(miModeNormals, wxT("&Normals")); - menuMode->AppendCheckItem(miModeWireframe, wxT("&Wireframe")); - menuMode->AppendCheckItem(miModeGrid, wxT("&Grid")); - menu->Append(menuMode, wxT("&Mode")); + //mode + menuMode = new wxMenu(); + menuMode->AppendCheckItem(miModeNormals, wxT("&Normals")); + menuMode->AppendCheckItem(miModeWireframe, wxT("&Wireframe")); + menuMode->AppendCheckItem(miModeGrid, wxT("&Grid")); + menu->Append(menuMode, wxT("&Mode")); - //mode - menuSpeed= new wxMenu(); - menuSpeed->Append(miSpeedSlower, wxT("&Slower\t-")); - menuSpeed->Append(miSpeedFaster, wxT("&Faster\t+")); - menuSpeed->AppendSeparator(); - menuSpeed->Append(miRestart, wxT("&Restart particles\tR"), wxT("Restart particle animations, this also reloads model and particle files if they are changed")); - menu->Append(menuSpeed, wxT("&Speed")); + //mode + menuSpeed = new wxMenu(); + menuSpeed->Append(miSpeedSlower, wxT("&Slower\t-")); + menuSpeed->Append(miSpeedFaster, wxT("&Faster\t+")); + menuSpeed->AppendSeparator(); + menuSpeed->Append(miRestart, wxT("&Restart particles\tR"), wxT("Restart particle animations, this also reloads model and particle files if they are changed")); + menu->Append(menuSpeed, wxT("&Speed")); - //custom color - menuCustomColor= new wxMenu(); - menuCustomColor->Append(miChangeBackgroundColor, wxT("Change Background Color")); - menuCustomColor->AppendCheckItem(miColorRed, wxT("&Red\t0")); - menuCustomColor->AppendCheckItem(miColorBlue, wxT("&Blue\t1")); - menuCustomColor->AppendCheckItem(miColorGreen, wxT("&Green\t2")); - menuCustomColor->AppendCheckItem(miColorYellow, wxT("&Yellow\t3")); - menuCustomColor->AppendCheckItem(miColorWhite, wxT("&White\t4")); - menuCustomColor->AppendCheckItem(miColorCyan, wxT("&Cyan\t5")); - menuCustomColor->AppendCheckItem(miColorOrange, wxT("&Orange\t6")); - menuCustomColor->AppendCheckItem(miColorMagenta, wxT("&Pink\t7")); // it is called Pink everywhere else so... - menu->Append(menuCustomColor, wxT("&Custom Color")); + //custom color + menuCustomColor = new wxMenu(); + menuCustomColor->Append(miChangeBackgroundColor, wxT("Change Background Color")); + menuCustomColor->AppendCheckItem(miColorRed, wxT("&Red\t0")); + menuCustomColor->AppendCheckItem(miColorBlue, wxT("&Blue\t1")); + menuCustomColor->AppendCheckItem(miColorGreen, wxT("&Green\t2")); + menuCustomColor->AppendCheckItem(miColorYellow, wxT("&Yellow\t3")); + menuCustomColor->AppendCheckItem(miColorWhite, wxT("&White\t4")); + menuCustomColor->AppendCheckItem(miColorCyan, wxT("&Cyan\t5")); + menuCustomColor->AppendCheckItem(miColorOrange, wxT("&Orange\t6")); + menuCustomColor->AppendCheckItem(miColorMagenta, wxT("&Pink\t7")); // it is called Pink everywhere else so... + menu->Append(menuCustomColor, wxT("&Custom Color")); - menuMode->Check(miModeGrid, true); - menuCustomColor->Check(miColorRed, true); + menuMode->Check(miModeGrid, true); + menuCustomColor->Check(miColorRed, true); - SetMenuBar(menu); + SetMenuBar(menu); - //misc - backBrightness= 0.3f; - gridBrightness= 1.0f; - lightBrightness= 0.3f; - lastX= 0; - lastY= 0; + //misc + backBrightness = 0.3f; + gridBrightness = 1.0f; + lightBrightness = 0.3f; + lastX = 0; + lastY = 0; - statusbarText=""; - CreateStatusBar(); + statusbarText = ""; + CreateStatusBar(); - this->Layout(); + this->Layout(); - wxInitAllImageHandlers(); + wxInitAllImageHandlers(); #ifdef WIN32 #if defined(__MINGW32__) - wxIcon icon(ToUnicode("IDI_ICON1")); + wxIcon icon(ToUnicode("IDI_ICON1")); #else - wxIcon icon(L"IDI_ICON1"); + wxIcon icon(L"IDI_ICON1"); #endif #else - wxIcon icon; - string iniFilePath = extractDirectoryPathFromFile(config.getFileName(false)); - string icon_file = iniFilePath + "g3dviewer.ico"; - std::ifstream testFile(icon_file.c_str()); - if(testFile.good()) { - testFile.close(); - icon.LoadFile(ToUnicode(icon_file.c_str()),wxBITMAP_TYPE_ICO); - } + wxIcon icon; + string iniFilePath = extractDirectoryPathFromFile(config.getFileName(false)); + string icon_file = iniFilePath + "g3dviewer.ico"; + std::ifstream testFile(icon_file.c_str()); + if (testFile.good()) { + testFile.close(); + icon.LoadFile(ToUnicode(icon_file.c_str()), wxBITMAP_TYPE_ICO); + } #endif - SetIcon(icon); + SetIcon(icon); - fileDialog = new wxFileDialog(this); - if(modelPath != "") { - fileDialog->SetPath(ToUnicode(modelPath)); - } - string userData = config.getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - string defaultPath = userData + "techs/"; - fileDialog->SetDirectory(ToUnicode(defaultPath.c_str())); + fileDialog = new wxFileDialog(this); + if (modelPath != "") { + fileDialog->SetPath(ToUnicode(modelPath)); + } + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + string defaultPath = userData + "techs/"; + fileDialog->SetDirectory(ToUnicode(defaultPath.c_str())); - if(glCanvas != NULL) { - glCanvas->SetFocus(); - } + if (glCanvas != NULL) { + glCanvas->SetFocus(); + } - // For windows register g3d file extension to launch this app + // For windows register g3d file extension to launch this app #if defined(WIN32) && !defined(__MINGW32__) // example from: http://stackoverflow.com/questions/1387769/create-registry-entry-to-associate-file-extension-with-application-in-c //[HKEY_CURRENT_USER\Software\Classes\blergcorp.blergapp.v1\shell\open\command] @@ -470,2160 +473,2096 @@ MainWindow::MainWindow( std::pair > unitToLoad, //@="blergcorp.blergapp.v1" //Open the registry key. - wstring subKey = L"Software\\Classes\\zetaglest.g3d\\shell\\open\\command"; - HKEY keyHandle; - DWORD dwDisposition; - RegCreateKeyEx(HKEY_CURRENT_USER,subKey.c_str(),0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); - //Set the value. - auto_ptr wstr(Ansi2WideString(appPath.c_str())); + wstring subKey = L"Software\\Classes\\zetaglest.g3d\\shell\\open\\command"; + HKEY keyHandle; + DWORD dwDisposition; + RegCreateKeyEx(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); + //Set the value. + auto_ptr wstr(Ansi2WideString(appPath.c_str())); - wstring launchApp = wstring(wstr.get()) + L" \"%1\""; - DWORD len = (launchApp.size() + 1) * sizeof(wchar_t); - RegSetValueEx(keyHandle, NULL, 0, REG_SZ, (PBYTE)launchApp.c_str(), len); - RegCloseKey(keyHandle); + wstring launchApp = wstring(wstr.get()) + L" \"%1\""; + DWORD len = (launchApp.size() + 1) * sizeof(wchar_t); + RegSetValueEx(keyHandle, NULL, 0, REG_SZ, (PBYTE) launchApp.c_str(), len); + RegCloseKey(keyHandle); - subKey = L"Software\\Classes\\.g3d"; - RegCreateKeyEx(HKEY_CURRENT_USER,subKey.c_str(),0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); - //Set the value. - launchApp = L"zetaglest.g3d"; - len = (launchApp.size() + 1) * sizeof(wchar_t); - RegSetValueEx(keyHandle, NULL, 0, REG_SZ, (PBYTE)launchApp.c_str(), len); - RegCloseKey(keyHandle); + subKey = L"Software\\Classes\\.g3d"; + RegCreateKeyEx(HKEY_CURRENT_USER, subKey.c_str(), 0, NULL, 0, KEY_ALL_ACCESS, NULL, &keyHandle, &dwDisposition); + //Set the value. + launchApp = L"zetaglest.g3d"; + len = (launchApp.size() + 1) * sizeof(wchar_t); + RegSetValueEx(keyHandle, NULL, 0, REG_SZ, (PBYTE) launchApp.c_str(), len); + RegCloseKey(keyHandle); #endif -} + } -void MainWindow::setupTimer() { - timer = new wxTimer(this); - timer->Start(100); -} + void MainWindow::setupTimer() { + timer = new wxTimer(this); + timer->Start(100); + } -void MainWindow::setupStartupSettings() { + void MainWindow::setupStartupSettings() { - //printf("In setupStartupSettings #1\n"); - if(glCanvas == NULL) { - initGlCanvas(); + //printf("In setupStartupSettings #1\n"); + if (glCanvas == NULL) { + initGlCanvas(); #if wxCHECK_VERSION(2, 9, 1) #else - glCanvas->SetCurrent(); + glCanvas->SetCurrent(); #endif - } - glCanvas->setCurrentGLContext(); - //printf("In setupStartupSettings #2\n"); + } + glCanvas->setCurrentGLContext(); + //printf("In setupStartupSettings #2\n"); - GLuint err = glewInit(); - if (GLEW_OK != err) { - fprintf(stderr, "Error [main]: glewInit failed: %s\n", glewGetErrorString(err)); - //return 1; - throw std::runtime_error((char *)glewGetErrorString(err)); - } + GLuint err = glewInit(); + if (GLEW_OK != err) { + fprintf(stderr, "Error [main]: glewInit failed: %s\n", glewGetErrorString(err)); + //return 1; + throw std::runtime_error((char *) glewGetErrorString(err)); + } - renderer= Renderer::getInstance(); + renderer = Renderer::getInstance(); - for(unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { - if(autoScreenShotParams[i] == "transparent") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuFile->Check(miFileToggleScreenshotTransparent,true); - float alpha = 0.0f; - renderer->setAlphaColor(alpha); + for (unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { + if (autoScreenShotParams[i] == "transparent") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuFile->Check(miFileToggleScreenshotTransparent, true); + float alpha = 0.0f; + renderer->setAlphaColor(alpha); + } + if (autoScreenShotParams[i] == "enable_grid") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeGrid, true); + if (renderer->getGrid() == false) { + renderer->toggleGrid(); + } + } + if (autoScreenShotParams[i] == "enable_wireframe") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeWireframe, true); + if (renderer->getWireframe() == false) { + renderer->toggleWireframe(); + } + } + if (autoScreenShotParams[i] == "enable_normals") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeNormals, true); + if (renderer->getNormals() == false) { + renderer->toggleNormals(); + } + } + if (autoScreenShotParams[i] == "disable_grid") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeGrid, false); + if (renderer->getGrid() == true) { + renderer->toggleGrid(); + } + } + if (autoScreenShotParams[i] == "enable_wireframe") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeWireframe, false); + if (renderer->getWireframe() == true) { + renderer->toggleWireframe(); + } + } + if (autoScreenShotParams[i] == "enable_normals") { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + menuMode->Check(miModeNormals, false); + if (renderer->getNormals() == true) { + renderer->toggleNormals(); + } + } + } + renderer->init(); + + wxCommandEvent event; + onMenuRestart(event); } - if(autoScreenShotParams[i] == "enable_grid") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeGrid,true); - if(renderer->getGrid() == false) { - renderer->toggleGrid(); + + MainWindow::~MainWindow() { + delete timer; + timer = NULL; + + delete fileDialog; + fileDialog = NULL; + + //delete model; + //model = NULL; + if (renderer) renderer->end(); + + delete renderer; + renderer = NULL; + + if (glCanvas) { + glCanvas->Destroy(); + } + glCanvas = NULL; + + } + + void MainWindow::initGlCanvas() { + if (glCanvas == NULL) { + int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_MIN_ALPHA, 8 }; // to prevent flicker + glCanvas = new GlCanvas(this, args); } } - if(autoScreenShotParams[i] == "enable_wireframe") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeWireframe,true); - if(renderer->getWireframe() == false) { - renderer->toggleWireframe(); - } - } - if(autoScreenShotParams[i] == "enable_normals") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeNormals,true); - if(renderer->getNormals() == false) { - renderer->toggleNormals(); - } - } - if(autoScreenShotParams[i] == "disable_grid") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeGrid,false); - if(renderer->getGrid() == true) { - renderer->toggleGrid(); - } - } - if(autoScreenShotParams[i] == "enable_wireframe") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeWireframe,false); - if(renderer->getWireframe() == true) { - renderer->toggleWireframe(); - } - } - if(autoScreenShotParams[i] == "enable_normals") { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - menuMode->Check(miModeNormals,false); - if(renderer->getNormals() == true) { - renderer->toggleNormals(); - } - } - } - renderer->init(); - wxCommandEvent event; - onMenuRestart(event); -} - -MainWindow::~MainWindow(){ - delete timer; - timer = NULL; - - delete fileDialog; - fileDialog = NULL; - - //delete model; - //model = NULL; - if(renderer) renderer->end(); - - delete renderer; - renderer = NULL; - - if(glCanvas) { - glCanvas->Destroy(); - } - glCanvas = NULL; - -} - -void MainWindow::initGlCanvas(){ - if(glCanvas == NULL) { - int args[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_MIN_ALPHA, 8 }; // to prevent flicker - glCanvas = new GlCanvas(this, args); - } -} - -void MainWindow::init() { + void MainWindow::init() { #if wxCHECK_VERSION(2, 9, 3) #elif wxCHECK_VERSION(2, 9, 1) #else - glCanvas->SetCurrent(); - //printf("setcurrent #2\n"); + glCanvas->SetCurrent(); + //printf("setcurrent #2\n"); #endif -} - -void MainWindow::onPaint(wxPaintEvent &event) { - if(!IsShown()) { - event.Skip(); - return; - } - - bool isFirstWindowShownEvent = !startupSettingsInited ; - if(startupSettingsInited == false) { - startupSettingsInited = true; - setupStartupSettings(); - } - glCanvas->setCurrentGLContext(); - - static float autoScreenshotRender = -1; - if(autoScreenShotAndExit == true && autoScreenshotRender >= 0) { - anim = autoScreenshotRender; - } - // notice that we use GetSize() here and not GetClientSize() because - // the latter doesn't return correct results for the minimized windows - // (at least not under Windows) - int viewportW = GetClientSize().x; - int viewportH = GetClientSize().y; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%d x %d\n",viewportW,viewportH); - - if(viewportW == 0 && viewportH == 0) { - printf("#1 %d x %d\n",viewportW,viewportH); - - viewportW = GetSize().x; - viewportH = GetSize().y; - - if(viewportH > 0) { - //viewportH -= menu->GetSize().y; - viewportH -= 22; } - printf("#2 %d x %d\n",viewportW,viewportH); - } + void MainWindow::onPaint(wxPaintEvent &event) { + if (!IsShown()) { + event.Skip(); + return; + } + + bool isFirstWindowShownEvent = !startupSettingsInited; + if (startupSettingsInited == false) { + startupSettingsInited = true; + setupStartupSettings(); + } + glCanvas->setCurrentGLContext(); + + static float autoScreenshotRender = -1; + if (autoScreenShotAndExit == true && autoScreenshotRender >= 0) { + anim = autoScreenshotRender; + } + // notice that we use GetSize() here and not GetClientSize() because + // the latter doesn't return correct results for the minimized windows + // (at least not under Windows) + int viewportW = GetClientSize().x; + int viewportH = GetClientSize().y; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%d x %d\n", viewportW, viewportH); + + if (viewportW == 0 && viewportH == 0) { + printf("#1 %d x %d\n", viewportW, viewportH); + + viewportW = GetSize().x; + viewportH = GetSize().y; + + if (viewportH > 0) { + //viewportH -= menu->GetSize().y; + viewportH -= 22; + } + + printf("#2 %d x %d\n", viewportW, viewportH); + } #if defined(WIN32) - renderer->reset(viewportW, viewportH, playerColor); + renderer->reset(viewportW, viewportH, playerColor); #else - renderer->reset(viewportW, viewportH, playerColor); + renderer->reset(viewportW, viewportH, playerColor); #endif - renderer->transform(rotX, rotY, zoom); - renderer->renderGrid(); + renderer->transform(rotX, rotY, zoom); + renderer->renderGrid(); - //printf("anim [%f] particleLoopStart [%d]\n",anim,particleLoopStart); + //printf("anim [%f] particleLoopStart [%d]\n",anim,particleLoopStart); - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - renderer->renderTheModel(model, anim); + renderer->renderTheModel(model, anim); - int updateLoops = particleLoopStart; - particleLoopStart = 1; + int updateLoops = particleLoopStart; + particleLoopStart = 1; - if(resetAnimation == true || ((anim - lastanim) >= defaultspeed*2)) { - //printf("anim [%f] [%f] [%f]\n",anim,lastanim,speed); + if (resetAnimation == true || ((anim - lastanim) >= defaultspeed * 2)) { + //printf("anim [%f] [%f] [%f]\n",anim,lastanim,speed); - for(int i=0; i< updateLoops; ++i) { - renderer->updateParticleManager(); + for (int i = 0; i < updateLoops; ++i) { + renderer->updateParticleManager(); + } + } + + renderer->renderParticleManager(); + + if (isFirstWindowShownEvent) { + this->Refresh(); + glCanvas->Refresh(); + glCanvas->SetFocus(); + } + + bool haveLoadedParticles = (particleProjectilePathList.empty() == false || particleSplashPathList.empty() == false); + + if (autoScreenShotAndExit == true && viewportW > 0 && viewportH > 0) { + printf("Auto exiting app...\n"); + fflush(stdout); + + autoScreenShotAndExit = false; + + saveScreenshot(); + Close(); + return; + } + + glCanvas->SwapBuffers(); + + if (autoScreenShotAndExit == true && viewportW == 0 && viewportH == 0) { + autoScreenshotRender = anim; + + printf("Auto exiting desired but waiting for w x h > 0...\n"); + + return; + } + if ((modelPathList.empty() == false) && resetAnimation && haveLoadedParticles) { + if (anim >= resetAnim && resetAnim > 0) { + printf("RESETTING EVERYTHING [%f][%f]...\n", anim, resetAnim); + fflush(stdout); + + resetAnimation = false; + particleLoopStart = resetParticleLoopStart; + + wxCommandEvent event; + if (unitPath.first != "") { + //onMenuFileClearAll(event); + + modelPathList.clear(); + particlePathList.clear(); + particleProjectilePathList.clear(); + particleSplashPathList.clear(); // as above + + onMenuRestart(event); + } else { + onMenuRestart(event); + } + } + } else if (modelPathList.empty() == true && haveLoadedParticles) { + if (renderer->hasActiveParticleSystem(ParticleSystem::pst_ProjectileParticleSystem) == false && + renderer->hasActiveParticleSystem(ParticleSystem::pst_SplashParticleSystem) == false) { + + printf("RESETTING PARTICLES...\n"); + fflush(stdout); + + resetAnimation = false; + anim = 0.f; + particleLoopStart = resetParticleLoopStart; + + wxCommandEvent event; + onMenuRestart(event); + } + } + + lastanim = anim; } - } - renderer->renderParticleManager(); + void MainWindow::onClose(wxCloseEvent &event) { + // release memory first (from onMenuFileClearAll) - if(isFirstWindowShownEvent) { - this->Refresh(); - glCanvas->Refresh(); - glCanvas->SetFocus(); - } + //printf("OnClose START\n"); + //fflush(stdout); - bool haveLoadedParticles = (particleProjectilePathList.empty() == false || particleSplashPathList.empty() == false); + modelPathList.clear(); + particlePathList.clear(); + particleProjectilePathList.clear(); + particleSplashPathList.clear(); // as above - if(autoScreenShotAndExit == true && viewportW > 0 && viewportH > 0) { - printf("Auto exiting app...\n"); - fflush(stdout); + if (timer) timer->Stop(); - autoScreenShotAndExit = false; + unitParticleSystems.clear(); + unitParticleSystemTypes.clear(); - saveScreenshot(); - Close(); - return; - } + projectileParticleSystems.clear(); + projectileParticleSystemTypes.clear(); + splashParticleSystems.clear(); // as above + splashParticleSystemTypes.clear(); - glCanvas->SwapBuffers(); + //delete model; + //model = NULL; + if (renderer) renderer->end(); - if(autoScreenShotAndExit == true && viewportW == 0 && viewportH == 0) { - autoScreenshotRender = anim; + //printf("OnClose about to END\n"); + //fflush(stdout); - printf("Auto exiting desired but waiting for w x h > 0...\n"); + delete timer; + timer = NULL; - return; - } - if((modelPathList.empty() == false) && resetAnimation && haveLoadedParticles) { - if(anim >= resetAnim && resetAnim > 0) { - printf("RESETTING EVERYTHING [%f][%f]...\n",anim,resetAnim); - fflush(stdout); + //delete model; + //model = NULL; - resetAnimation = false; - particleLoopStart = resetParticleLoopStart; + delete renderer; + renderer = NULL; - wxCommandEvent event; - if(unitPath.first != "") { - //onMenuFileClearAll(event); + //delete glCanvas; + if (glCanvas) { + glCanvas->Destroy(); + } + glCanvas = NULL; + + this->Destroy(); + } + + // for the mousewheel + void MainWindow::onMouseWheelDown(wxMouseEvent &event) { + try { + wxPaintEvent paintEvent; + zoom *= 1.1f; + zoom = clamp(zoom, 0.1f, 10.0f); + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + + onPaint(paintEvent); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMouseWheelUp(wxMouseEvent &event) { + try { + wxPaintEvent paintEvent; + zoom *= 0.90909f; + zoom = clamp(zoom, 0.1f, 10.0f); + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + + onPaint(paintEvent); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + + void MainWindow::onMouseMove(wxMouseEvent &event) { + try { + int x = event.GetX(); + int y = event.GetY(); + wxPaintEvent paintEvent; + + if (event.LeftIsDown()) { + rotX += clamp(lastX - x, -10, 10); + rotY += clamp(lastY - y, -10, 10); + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + + onPaint(paintEvent); + } else if (event.RightIsDown()) { + zoom *= 1.0f + (lastX - x + lastY - y) / 100.0f; + zoom = clamp(zoom, 0.1f, 10.0f); + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + + onPaint(paintEvent); + } + + lastX = x; + lastY = y; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuFileLoad(wxCommandEvent &event) { + try { + //string fileName; + fileDialog->SetWildcard(wxT("G3D files (*.g3d)|*.g3d;*.G3D")); + fileDialog->SetMessage(wxT("Selecting Glest Model for current view.")); + + if (fileDialog->ShowModal() == wxID_OK) { + modelPathList.clear(); + string file; +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); + file = tmp_buf; + + auto_ptr wstr(Ansi2WideString(file.c_str())); + file = utf8_encode(wstr.get()); +#else + file = (const char*) wxFNCONV(fileDialog->GetPath().c_str()); +#endif + + //loadModel((const char*)wxFNCONV(fileDialog->GetPath().c_str())); + loadModel(file); + } + isControlKeyPressed = false; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuFileLoadParticleXML(wxCommandEvent &event) { + try { + //string fileName; + fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); + + if (isControlKeyPressed == true) { + fileDialog->SetMessage(wxT("Adding ZetaGlest particle to current view.")); + } else { + fileDialog->SetMessage(wxT("Selecting ZetaGlest particle for current view.")); + } + + if (fileDialog->ShowModal() == wxID_OK) { + //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); + string file; +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); + file = tmp_buf; + auto_ptr wstr(Ansi2WideString(file.c_str())); + file = utf8_encode(wstr.get()); +#else + file = (const char*) wxFNCONV(fileDialog->GetPath().c_str()); +#endif + + loadParticle(file); + } + isControlKeyPressed = false; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuFileLoadProjectileParticleXML(wxCommandEvent &event) { + try { + //string fileName; + fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); + + if (isControlKeyPressed == true) { + fileDialog->SetMessage(wxT("Adding ZetaGlest projectile particle to current view.")); + } else { + fileDialog->SetMessage(wxT("Selecting ZetaGlest projectile particle for current view.")); + } + + if (fileDialog->ShowModal() == wxID_OK) { + //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); + string file; +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); + file = tmp_buf; + auto_ptr wstr(Ansi2WideString(file.c_str())); + file = utf8_encode(wstr.get()); +#else + file = (const char*) wxFNCONV(fileDialog->GetPath().c_str()); +#endif + + loadProjectileParticle(file); + } + isControlKeyPressed = false; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuFileLoadSplashParticleXML(wxCommandEvent &event) { + try { + //string fileName; + fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); + + if (isControlKeyPressed == true) { + fileDialog->SetMessage(wxT("Adding ZetaGlest splash particle to current view.")); + } else { + fileDialog->SetMessage(wxT("Selecting ZetaGlest splash particle for current view.")); + } + + if (fileDialog->ShowModal() == wxID_OK) { + //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); + string file; +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); + file = tmp_buf; + + auto_ptr wstr(Ansi2WideString(file.c_str())); + file = utf8_encode(wstr.get()); +#else + file = (const char*) wxFNCONV(fileDialog->GetPath().c_str()); +#endif + + loadSplashParticle(file); + } + isControlKeyPressed = false; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } // is it possible to join loadParticle(), loadProjectileParticle() and loadSplashParticle() to one method? + + + void MainWindow::OnChangeColor(wxCommandEvent &event) { + try { + //wxColour color = colorPicker->GetColour(); + wxColourData data; + data.SetChooseFull(true); + for (int i = 0; i < 16; i++) { + wxColour colour(i * 16, i * 16, i * 16); + data.SetCustomColour(i, colour); + } + + wxColourDialog dialog(this, &data); + if (dialog.ShowModal() == wxID_OK) { + wxColourData retData = dialog.GetColourData(); + wxColour col = retData.GetColour(); + renderer->setBackgroundColor(col.Red() / 255.0f, col.Green() / 255.0f, col.Blue() / 255.0f); + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenumFileToggleScreenshotTransparent(wxCommandEvent &event) { + try { + float alpha = (event.IsChecked() == true ? 0.0f : 1.0f); + renderer->setAlphaColor(alpha); + //printf("alpha = %f\n",alpha); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::saveScreenshot() { + try { + int autoSaveScreenshotIndex = -1; + for (unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { + if (_strnicmp(autoScreenShotParams[i].c_str(), "saveas-", 7) == 0) { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); + autoSaveScreenshotIndex = i; + break; + } + } + if (autoSaveScreenshotIndex >= 0) { + string saveAsFilename = autoScreenShotParams[autoSaveScreenshotIndex]; + saveAsFilename.erase(0, 7); +#ifdef WIN32 + FILE*f = _wfopen(utf8_decode(saveAsFilename).c_str(), L"rb"); +#else + FILE *f = fopen(saveAsFilename.c_str(), "rb"); +#endif + if (f == NULL) { + renderer->saveScreen(saveAsFilename.c_str(), &overrideSize); + } else { + if (f) { + fclose(f); + } + } + } else { + //string screenShotsPath = extractDirectoryPathFromFile(appPath) + string("screens/"); + string userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + string screenShotsPath = userData + string("screens/"); + printf("screenShotsPath [%s]\n", screenShotsPath.c_str()); + + if (isdir(screenShotsPath.c_str()) == false) { + createDirectoryPaths(screenShotsPath); + } + + string path = screenShotsPath; + if (isdir(path.c_str()) == true) { + //Config &config= Config::getInstance(); + //string fileFormat = config.getString("ScreenShotFileType","jpg"); + + for (int i = 0; i < 5000; ++i) { + path = screenShotsPath; + path += string("screen") + intToStr(i) + string(".") + fileFormat; +#ifdef WIN32 + FILE*f = _wfopen(utf8_decode(path).c_str(), L"rb"); +#else + FILE *f = fopen(path.c_str(), "rb"); +#endif + if (f == NULL) { + renderer->saveScreen(path, &overrideSize); + break; + } else { + if (f) { + fclose(f); + } + } + } + } + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuFileSaveScreenshot(wxCommandEvent &event) { + saveScreenshot(); + } + + void MainWindow::onMenuFileClearAll(wxCommandEvent &event) { + try { + //printf("Start onMenuFileClearAll\n"); + //fflush(stdout); modelPathList.clear(); particlePathList.clear(); particleProjectilePathList.clear(); particleSplashPathList.clear(); // as above - onMenuRestart(event); - } - else { - onMenuRestart(event); + if (timer) timer->Stop(); + if (renderer) renderer->end(); + + unitParticleSystems.clear(); + unitParticleSystemTypes.clear(); + + projectileParticleSystems.clear(); + projectileParticleSystemTypes.clear(); + splashParticleSystems.clear(); // as above + splashParticleSystemTypes.clear(); + + //delete model; + //model = NULL; + if (model != NULL && renderer != NULL) renderer->endModel(rsGlobal, model); + model = NULL; + + loadUnit("", ""); + loadModel(""); + loadParticle(""); + loadProjectileParticle(""); + loadSplashParticle(""); // as above + + GetStatusBar()->SetStatusText(ToUnicode(statusbarText.c_str())); + isControlKeyPressed = false; + + //printf("END onMenuFileClearAll\n"); + //fflush(stdout); + + if (timer) timer->Start(100); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); } } - } - else if(modelPathList.empty() == true && haveLoadedParticles) { - if(renderer->hasActiveParticleSystem(ParticleSystem::pst_ProjectileParticleSystem) == false && - renderer->hasActiveParticleSystem(ParticleSystem::pst_SplashParticleSystem) == false) { - printf("RESETTING PARTICLES...\n"); - fflush(stdout); - - resetAnimation = false; - anim = 0.f; - particleLoopStart = resetParticleLoopStart; - - wxCommandEvent event; - onMenuRestart(event); + void MainWindow::onMenuFileExit(wxCommandEvent &event) { + Close(); } - } - lastanim = anim; -} + void MainWindow::loadUnit(string path, string skillName) { + if (path != "" && fileExists(path) == true) { + // std::cout << "Clearing list..." << std::endl; + this->unitPath.first = path; + this->unitPath.second.push_back(skillName); + } -void MainWindow::onClose(wxCloseEvent &event){ - // release memory first (from onMenuFileClearAll) + try { + if (this->unitPath.first != "") { + if (timer) timer->Stop(); + if (renderer) renderer->end(); - //printf("OnClose START\n"); - //fflush(stdout); + string titlestring = winHeader; - modelPathList.clear(); - particlePathList.clear(); - particleProjectilePathList.clear(); - particleSplashPathList.clear(); // as above + string unitPath = this->unitPath.first; + string dir = unitPath; + string name = lastDir(dir); + string path = dir + "/" + name + ".xml"; - if(timer) timer->Stop(); + titlestring = unitPath + " - " + titlestring; - unitParticleSystems.clear(); - unitParticleSystemTypes.clear(); + std::string unitXML = path; - projectileParticleSystems.clear(); - projectileParticleSystemTypes.clear(); - splashParticleSystems.clear(); // as above - splashParticleSystemTypes.clear(); + string skillModelFile = ""; + string skillParticleFile = ""; + string skillParticleProjectileFile = ""; + string skillParticleSplashFile = ""; + bool fileFound = fileExists(unitXML); - //delete model; - //model = NULL; - if(renderer) renderer->end(); + printf("Loading unit from file [%s] fileFound = %d\n", unitXML.c_str(), fileFound); - //printf("OnClose about to END\n"); - //fflush(stdout); + if (fileFound == true) { + XmlTree xmlTree; + xmlTree.load(unitXML, Properties::getTagReplacementValues()); + const XmlNode *unitNode = xmlTree.getRootNode(); - delete timer; - timer = NULL; + bool foundSkillName = false; + for (unsigned int skillIdx = 0; foundSkillName == false && skillIdx < this->unitPath.second.size(); ++skillIdx) { + string lookipForSkillName = this->unitPath.second[skillIdx]; - //delete model; - //model = NULL; + const XmlNode *skillsNode = unitNode->getChild("skills"); + for (unsigned int i = 0; foundSkillName == false && i < skillsNode->getChildCount(); ++i) { + const XmlNode *sn = skillsNode->getChild("skill", i); + //const XmlNode *typeNode= sn->getChild("type"); + const XmlNode *nameNode = sn->getChild("name"); + string skillXmlName = nameNode->getAttribute("value")->getRestrictedValue(); + if (skillXmlName == lookipForSkillName) { + printf("Found skill [%s]\n", lookipForSkillName.c_str()); + foundSkillName = true; - delete renderer; - renderer = NULL; + if (sn->getChild("animation") != NULL) { + skillModelFile = sn->getChild("animation")->getAttribute("path")->getRestrictedValue(unitPath + '/'); + printf("Found skill model [%s]\n", skillModelFile.c_str()); + } - //delete glCanvas; - if(glCanvas) { - glCanvas->Destroy(); - } - glCanvas = NULL; + if (sn->hasChild("particles") == true) { + const XmlNode *particlesNode = sn->getChild("particles"); + //for(int j = 0; particlesNode != NULL && particlesNode->getAttribute("value")->getRestrictedValue() == "true" && + // j < particlesNode->getChildCount(); ++j) { + if (particlesNode != NULL && particlesNode->getAttribute("value")->getRestrictedValue() == "true" && + particlesNode->hasChild("particle-file") == true) { + const XmlNode *pf = particlesNode->getChild("particle-file"); + if (pf != NULL) { + skillParticleFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); + printf("Found skill particle [%s]\n", skillParticleFile.c_str()); + } + } + } + if (sn->hasChild("projectile") == true) { + const XmlNode *particlesProjectileNode = sn->getChild("projectile"); + //for(int j = 0; particlesProjectileNode != NULL && particlesProjectileNode->getAttribute("value")->getRestrictedValue() == "true" && + // j < particlesProjectileNode->getChildCount(); ++j) { + if (particlesProjectileNode != NULL && particlesProjectileNode->getAttribute("value")->getRestrictedValue() == "true" && + particlesProjectileNode->hasChild("particle") == true) { + const XmlNode *pf = particlesProjectileNode->getChild("particle"); + if (pf != NULL && pf->getAttribute("value")->getRestrictedValue() == "true") { + skillParticleProjectileFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); + printf("Found skill skill projectile particle [%s]\n", skillParticleProjectileFile.c_str()); + } + } + } + if (sn->hasChild("splash") == true) { + const XmlNode *particlesSplashNode = sn->getChild("splash"); + //for(int j = 0; particlesSplashNode != NULL && particlesSplashNode->getAttribute("value")->getRestrictedValue() == "true" && + // j < particlesSplashNode->getChildCount(); ++j) { + if (particlesSplashNode != NULL && particlesSplashNode->getAttribute("value")->getRestrictedValue() == "true" && + particlesSplashNode->hasChild("particle") == true) { + const XmlNode *pf = particlesSplashNode->getChild("particle"); + if (pf != NULL && pf->getAttribute("value")->getRestrictedValue() == "true") { + skillParticleSplashFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); + printf("Found skill skill splash particle [%s]\n", skillParticleSplashFile.c_str()); + } + } + } + } + } + } - this->Destroy(); -} - -// for the mousewheel -void MainWindow::onMouseWheelDown(wxMouseEvent &event) { - try { - wxPaintEvent paintEvent; - zoom*= 1.1f; - zoom= clamp(zoom, 0.1f, 10.0f); - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - - onPaint(paintEvent); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMouseWheelUp(wxMouseEvent &event) { - try { - wxPaintEvent paintEvent; - zoom*= 0.90909f; - zoom= clamp(zoom, 0.1f, 10.0f); - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - - onPaint(paintEvent); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - - -void MainWindow::onMouseMove(wxMouseEvent &event){ - try { - int x= event.GetX(); - int y= event.GetY(); - wxPaintEvent paintEvent; - - if(event.LeftIsDown()){ - rotX+= clamp(lastX-x, -10, 10); - rotY+= clamp(lastY-y, -10, 10); - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - - onPaint(paintEvent); + if (skillModelFile != "") { + this->modelPathList.push_back(skillModelFile); + printf("Added skill model [%s]\n", skillModelFile.c_str()); + } + if (skillParticleFile != "") { + this->particlePathList.push_back(skillParticleFile); + printf("Added skill particle [%s]\n", skillParticleFile.c_str()); + } + if (skillParticleProjectileFile != "") { + this->particleProjectilePathList.push_back(skillParticleProjectileFile); + printf("Added skill projectile particle [%s]\n", skillParticleProjectileFile.c_str()); + } + if (skillParticleSplashFile != "") { + this->particleSplashPathList.push_back(skillParticleSplashFile); + printf("Added skill splash particle [%s]\n", skillParticleSplashFile.c_str()); + } + } + SetTitle(ToUnicode(titlestring)); + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); + } } - else if(event.RightIsDown()){ - zoom*= 1.0f+(lastX-x+lastY-y)/100.0f; - zoom= clamp(zoom, 0.1f, 10.0f); - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + void MainWindow::loadModel(string path) { + try { + if (path != "" && fileExists(path) == true) { + this->modelPathList.push_back(path); + printf("Adding model [%s] list size " MG_SIZE_T_SPECIFIER "\n", path.c_str(), this->modelPathList.size()); + } + string titlestring = winHeader; + for (unsigned int idx = 0; idx < this->modelPathList.size(); idx++) { + string modelPath = this->modelPathList[idx]; + + //printf("Loading model [%s] %u of " MG_SIZE_T_SPECIFIER "\n",modelPath.c_str(),idx, this->modelPathList.size()); + + if (timer) timer->Stop(); + //delete model; + if (model != NULL && renderer != NULL) renderer->endModel(rsGlobal, model); + model = NULL; + model = renderer ? renderer->newModel(rsGlobal, modelPath) : NULL; + + statusbarText = getModelInfo(); + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + if (timer) timer->Start(100); + titlestring = extractFileFromDirectoryPath(modelPath) + " - " + titlestring; + } + SetTitle(ToUnicode(titlestring)); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::loadParticle(string path) { + if (timer) timer->Stop(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str()); + if (path != "" && fileExists(path) == true) { + renderer->end(); + unitParticleSystems.clear(); + unitParticleSystemTypes.clear(); + + if (isControlKeyPressed == true) { + // std::cout << "Adding to list..." << std::endl; + this->particlePathList.push_back(path); + } else { + // std::cout << "Clearing list..." << std::endl; + this->particlePathList.clear(); + this->particlePathList.push_back(path); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] added file [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str()); + } + + try { + if (this->particlePathList.empty() == false) { + string titlestring = winHeader; + for (unsigned int idx = 0; idx < this->particlePathList.size(); idx++) { + string particlePath = this->particlePathList[idx]; + string dir = extractDirectoryPathFromFile(particlePath); + + size_t pos = dir.find_last_of(folderDelimiter); + if (pos == dir.length() - 1 && dir.length() > 0) { + dir.erase(dir.length() - 1); + } + + particlePath = extractFileFromDirectoryPath(particlePath); + titlestring = particlePath + " - " + titlestring; + + std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] looking for unit XML [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, unitXML.c_str()); + + //int size = -1; + //int height = -1; + int size = 0; + int height = 0; + + if (fileExists(unitXML) == true) { + { + XmlTree xmlTree; + xmlTree.load(unitXML, Properties::getTagReplacementValues()); + const XmlNode *unitNode = xmlTree.getRootNode(); + const XmlNode *parametersNode = unitNode->getChild("parameters"); + //size + size = parametersNode->getChild("size")->getAttribute("value")->getIntValue(); + //height + height = parametersNode->getChild("height")->getAttribute("value")->getIntValue(); + } + + // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] unit XML NOT FOUND [%s] using default position values\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, unitXML.c_str()); + + size = 1; + height = 1; + } + + std::map > > loadedFileList; + UnitParticleSystemType *unitParticleSystemType = new UnitParticleSystemType(); + unitParticleSystemType->load(NULL, dir, dir + folderDelimiter + particlePath, //### if we knew which particle it was, we could be more accurate + renderer, loadedFileList, "g3dviewer", ""); + unitParticleSystemTypes.push_back(unitParticleSystemType); + + for (std::vector::const_iterator it = unitParticleSystemTypes.begin(); it != unitParticleSystemTypes.end(); ++it) { + UnitParticleSystem *ups = new UnitParticleSystem(200); + + ups->setParticleType((*it)); + (*it)->setValues(ups); + if (size > 0) { + //getCurrVectorFlat() + Vec3f(0.f, type->getHeight()/2.f, 0.f); + Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); + ups->setPos(vec); + } + //ups->setFactionColor(getFaction()->getTexture()->getPixmap()->getPixel3f(0,0)); + ups->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0, 0)); + unitParticleSystems.push_back(ups); + renderer->manageParticleSystem(ups); + + ups->setVisible(true); + } + + if (path != "" && fileExists(path) == true) { + renderer->initModelManager(); + if (initTextureManager) { + renderer->initTextureManager(); + } + } + + } + SetTitle(ToUnicode(titlestring)); + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a Mega-Glest particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); + } + if (timer) timer->Start(100); + } + + void MainWindow::loadProjectileParticle(string path) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s] particleProjectilePathList.size() = " MG_SIZE_T_SPECIFIER "\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str(), this->particleProjectilePathList.size()); + + if (timer) timer->Stop(); + if (path != "" && fileExists(path) == true) { + renderer->end(); + projectileParticleSystems.clear(); + projectileParticleSystemTypes.clear(); + + if (isControlKeyPressed == true) { + // std::cout << "Adding to list..." << std::endl; + this->particleProjectilePathList.push_back(path); + } else { + // std::cout << "Clearing list..." << std::endl; + this->particleProjectilePathList.clear(); + this->particleProjectilePathList.push_back(path); + } + } + + try { + if (this->particleProjectilePathList.empty() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("this->particleProjectilePathList.size() = " MG_SIZE_T_SPECIFIER "\n", this->particleProjectilePathList.size()); + + string titlestring = winHeader; + for (unsigned int idx = 0; idx < this->particleProjectilePathList.size(); idx++) { + string particlePath = this->particleProjectilePathList[idx]; + string dir = extractDirectoryPathFromFile(particlePath); + + size_t pos = dir.find_last_of(folderDelimiter); + if (pos == dir.length() - 1) { + dir.erase(dir.length() - 1); + } + + particlePath = extractFileFromDirectoryPath(particlePath); + titlestring = particlePath + " - " + titlestring; + + std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; + + int size = 1; + int height = 1; + + if (fileExists(unitXML) == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, unitXML.c_str(), idx); + + XmlTree xmlTree; + xmlTree.load(unitXML, Properties::getTagReplacementValues()); + const XmlNode *unitNode = xmlTree.getRootNode(); + const XmlNode *parametersNode = unitNode->getChild("parameters"); + //size + size = parametersNode->getChild("size")->getAttribute("value")->getIntValue(); + //height + height = parametersNode->getChild("height")->getAttribute("value")->getIntValue(); + } + + // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; + + string particleFile = dir + folderDelimiter + particlePath; + { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, particleFile.c_str(), idx); + XmlTree xmlTree; + xmlTree.load(particleFile, Properties::getTagReplacementValues()); + //const XmlNode *particleSystemNode= xmlTree.getRootNode(); + + // std::cout << "Loaded successfully, loading values..." << std::endl; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, particleFile.c_str(), idx); + std::map > > loadedFileList; + ParticleSystemTypeProjectile *projectileParticleSystemType = new ParticleSystemTypeProjectile(); + projectileParticleSystemType->load(NULL, dir, //### we don't know if there are overrides in the unit XML + particleFile, renderer, loadedFileList, + "g3dviewer", ""); + + // std::cout << "Values loaded, about to read..." << std::endl; + + projectileParticleSystemTypes.push_back(projectileParticleSystemType); + + for (std::vector::const_iterator it = projectileParticleSystemTypes.begin(); + it != projectileParticleSystemTypes.end(); ++it) { + + ProjectileParticleSystem *ps = (*it)->create(NULL); + + if (size > 0) { + Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); + //ps->setPos(vec); + + Vec3f vec2 = Vec3f(size * 2.f, height * 2.f, height * 2.f); + ps->setPath(vec, vec2); + } + ps->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0, 0)); + + projectileParticleSystems.push_back(ps); + + ps->setVisible(true); + renderer->manageParticleSystem(ps); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loaded [%s] idx = %u\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, particleFile.c_str(), idx); + } + SetTitle(ToUnicode(titlestring)); + + if (path != "" && fileExists(path) == true) { + renderer->initModelManager(); + if (initTextureManager) { + renderer->initTextureManager(); + } + } + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest projectile particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); + } + if (timer) timer->Start(100); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] after load [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str()); + } + + void MainWindow::loadSplashParticle(string path) { // uses ParticleSystemTypeSplash::load (and own list...) + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s] particleSplashPathList.size() = " MG_SIZE_T_SPECIFIER "\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str(), this->particleSplashPathList.size()); + + if (timer) timer->Stop(); + if (path != "" && fileExists(path) == true) { + renderer->end(); + splashParticleSystems.clear(); + splashParticleSystemTypes.clear(); + + if (isControlKeyPressed == true) { + // std::cout << "Adding to list..." << std::endl; + this->particleSplashPathList.push_back(path); + } else { + // std::cout << "Clearing list..." << std::endl; + this->particleSplashPathList.clear(); + this->particleSplashPathList.push_back(path); + } + } + + try { + if (this->particleSplashPathList.empty() == false) { + string titlestring = winHeader; + for (unsigned int idx = 0; idx < this->particleSplashPathList.size(); idx++) { + string particlePath = this->particleSplashPathList[idx]; + string dir = extractDirectoryPathFromFile(particlePath); + + size_t pos = dir.find_last_of(folderDelimiter); + if (pos == dir.length() - 1) { + dir.erase(dir.length() - 1); + } + + particlePath = extractFileFromDirectoryPath(particlePath); + titlestring = particlePath + " - " + titlestring; + + std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; + + int size = 1; + //int height = 1; + + if (fileExists(unitXML) == true) { + XmlTree xmlTree; + xmlTree.load(unitXML, Properties::getTagReplacementValues()); + const XmlNode *unitNode = xmlTree.getRootNode(); + const XmlNode *parametersNode = unitNode->getChild("parameters"); + //size + size = parametersNode->getChild("size")->getAttribute("value")->getIntValue(); + //height + //height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); + } + + // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; + + { + XmlTree xmlTree; + xmlTree.load(dir + folderDelimiter + particlePath, Properties::getTagReplacementValues()); + //const XmlNode *particleSystemNode= xmlTree.getRootNode(); + // std::cout << "Loaded successfully, loading values..." << std::endl; + } + + std::map > > loadedFileList; + ParticleSystemTypeSplash *splashParticleSystemType = new ParticleSystemTypeSplash(); + splashParticleSystemType->load(NULL, dir, dir + folderDelimiter + particlePath, renderer, //### we don't know if there are overrides in the unit XML + loadedFileList, "g3dviewer", ""); // <---- only that must be splash... + + // std::cout << "Values loaded, about to read..." << std::endl; + + splashParticleSystemTypes.push_back(splashParticleSystemType); + + //ParticleSystemTypeSplash + for (std::vector::const_iterator it = splashParticleSystemTypes.begin(); it != splashParticleSystemTypes.end(); ++it) { + + SplashParticleSystem *ps = (*it)->create(NULL); + + if (size > 0) { + //Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); + //ps->setPos(vec); + + //Vec3f vec2 = Vec3f(size * 2.f, height * 2.f, height * 2.f); // <------- removed relative projectile + //ps->setPath(vec, vec2); // <------- removed relative projectile + } + ps->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0, 0)); + + splashParticleSystems.push_back(ps); + + ps->setVisible(true); + renderer->manageParticleSystem(ps); + } + } + SetTitle(ToUnicode(titlestring)); + + if (path != "" && fileExists(path) == true) { + renderer->initModelManager(); + if (initTextureManager) { + renderer->initTextureManager(); + } + } + } + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest projectile particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); + } + if (timer) timer->Start(100); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] after load [%s] particleSplashPathList.size() = " MG_SIZE_T_SPECIFIER "\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str(), this->particleSplashPathList.size()); + } + + void MainWindow::onMenuModeNormals(wxCommandEvent &event) { + try { + renderer->toggleNormals(); + menuMode->Check(miModeNormals, renderer->getNormals()); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuModeWireframe(wxCommandEvent &event) { + try { + renderer->toggleWireframe(); + menuMode->Check(miModeWireframe, renderer->getWireframe()); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuModeGrid(wxCommandEvent &event) { + try { + renderer->toggleGrid(); + menuMode->Check(miModeGrid, renderer->getGrid()); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuSpeedSlower(wxCommandEvent &event) { + try { + speed /= 1.5f; + if (speed < 0) { + speed = 0; + } + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuSpeedFaster(wxCommandEvent &event) { + try { + speed *= 1.5f; + if (speed > 1) { + speed = 1; + } + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + // set menu checkboxes to what player color is used + void MainWindow::onMenuColorRed(wxCommandEvent &event) { + try { + playerColor = Renderer::pcRed; + menuCustomColor->Check(miColorRed, true); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorBlue(wxCommandEvent &event) { + try { + playerColor = Renderer::pcBlue; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, true); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorGreen(wxCommandEvent &event) { + try { + playerColor = Renderer::pcGreen; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, true); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorYellow(wxCommandEvent &event) { + try { + playerColor = Renderer::pcYellow; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, true); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorWhite(wxCommandEvent &event) { + try { + playerColor = Renderer::pcWhite; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, true); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorCyan(wxCommandEvent &event) { + try { + playerColor = Renderer::pcCyan; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, true); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorOrange(wxCommandEvent &event) { + try { + playerColor = Renderer::pcOrange; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, true); + menuCustomColor->Check(miColorMagenta, false); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + void MainWindow::onMenuColorMagenta(wxCommandEvent &event) { + try { + playerColor = Renderer::pcMagenta; + menuCustomColor->Check(miColorRed, false); + menuCustomColor->Check(miColorBlue, false); + menuCustomColor->Check(miColorGreen, false); + menuCustomColor->Check(miColorYellow, false); + menuCustomColor->Check(miColorWhite, false); + menuCustomColor->Check(miColorCyan, false); + menuCustomColor->Check(miColorOrange, false); + menuCustomColor->Check(miColorMagenta, true); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); + } + } + + + void MainWindow::onTimer(wxTimerEvent &event) { + anim = anim + speed; + if (anim > 1.0f) { + anim -= 1.f; + resetAnimation = true; + } + wxPaintEvent paintEvent; onPaint(paintEvent); } - lastX= x; - lastY= y; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + string MainWindow::getModelInfo() { + string str; -void MainWindow::onMenuFileLoad(wxCommandEvent &event){ - try { - //string fileName; - fileDialog->SetWildcard(wxT("G3D files (*.g3d)|*.g3d;*.G3D")); - fileDialog->SetMessage(wxT("Selecting Glest Model for current view.")); - - if(fileDialog->ShowModal()==wxID_OK){ - modelPathList.clear(); - string file; -#ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); - file = tmp_buf; - - auto_ptr wstr(Ansi2WideString(file.c_str())); - file = utf8_encode(wstr.get()); -#else - file = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); -#endif - - //loadModel((const char*)wxFNCONV(fileDialog->GetPath().c_str())); - loadModel(file); - } - isControlKeyPressed = false; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuFileLoadParticleXML(wxCommandEvent &event){ - try { - //string fileName; - fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); - - if(isControlKeyPressed == true) { - fileDialog->SetMessage(wxT("Adding ZetaGlest particle to current view.")); - } - else { - fileDialog->SetMessage(wxT("Selecting ZetaGlest particle for current view.")); - } - - if(fileDialog->ShowModal()==wxID_OK){ - //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); - string file; -#ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); - file = tmp_buf; - auto_ptr wstr(Ansi2WideString(file.c_str())); - file = utf8_encode(wstr.get()); -#else - file = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); -#endif - - loadParticle(file); - } - isControlKeyPressed = false; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuFileLoadProjectileParticleXML(wxCommandEvent &event){ - try { - //string fileName; - fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); - - if(isControlKeyPressed == true) { - fileDialog->SetMessage(wxT("Adding ZetaGlest projectile particle to current view.")); - } - else { - fileDialog->SetMessage(wxT("Selecting ZetaGlest projectile particle for current view.")); - } - - if(fileDialog->ShowModal()==wxID_OK){ - //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); - string file; -#ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); - file = tmp_buf; - auto_ptr wstr(Ansi2WideString(file.c_str())); - file = utf8_encode(wstr.get()); -#else - file = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); -#endif - - loadProjectileParticle(file); - } - isControlKeyPressed = false; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuFileLoadSplashParticleXML(wxCommandEvent &event){ - try { - //string fileName; - fileDialog->SetWildcard(wxT("XML files (*.xml)|*.xml")); - - if(isControlKeyPressed == true) { - fileDialog->SetMessage(wxT("Adding ZetaGlest splash particle to current view.")); - } - else { - fileDialog->SetMessage(wxT("Selecting ZetaGlest splash particle for current view.")); - } - - if(fileDialog->ShowModal()==wxID_OK){ - //string path = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); - string file; -#ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(fileDialog->GetPath()); - file = tmp_buf; - - auto_ptr wstr(Ansi2WideString(file.c_str())); - file = utf8_encode(wstr.get()); -#else - file = (const char*)wxFNCONV(fileDialog->GetPath().c_str()); -#endif - - loadSplashParticle(file); - } - isControlKeyPressed = false; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} // is it possible to join loadParticle(), loadProjectileParticle() and loadSplashParticle() to one method? - - -void MainWindow::OnChangeColor(wxCommandEvent &event) { - try { - //wxColour color = colorPicker->GetColour(); - wxColourData data; - data.SetChooseFull(true); - for (int i = 0; i < 16; i++) - { - wxColour colour(i*16, i*16, i*16); - data.SetCustomColour(i, colour); - } - - wxColourDialog dialog(this, &data); - if (dialog.ShowModal() == wxID_OK) - { - wxColourData retData = dialog.GetColourData(); - wxColour col = retData.GetColour(); - renderer->setBackgroundColor(col.Red()/255.0f, col.Green()/255.0f, col.Blue()/255.0f); - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenumFileToggleScreenshotTransparent(wxCommandEvent &event) { - try { - float alpha = (event.IsChecked() == true ? 0.0f : 1.0f); - renderer->setAlphaColor(alpha); - //printf("alpha = %f\n",alpha); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::saveScreenshot() { - try { - int autoSaveScreenshotIndex = -1; - for(unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { - if(_strnicmp(autoScreenShotParams[i].c_str(),"saveas-",7) == 0) { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - autoSaveScreenshotIndex = i; - break; - } - } - if(autoSaveScreenshotIndex >= 0) { - string saveAsFilename = autoScreenShotParams[autoSaveScreenshotIndex]; - saveAsFilename.erase(0,7); -#ifdef WIN32 - FILE*f = _wfopen(utf8_decode(saveAsFilename).c_str(), L"rb"); -#else - FILE *f= fopen(saveAsFilename.c_str(), "rb"); -#endif - if(f == NULL) { - renderer->saveScreen(saveAsFilename.c_str(),&overrideSize); + if (model != NULL) { + str += "Meshes: " + intToStr(model->getMeshCount()); + str += ", Vertices: " + intToStr(model->getVertexCount()); + str += ", Triangles: " + intToStr(model->getTriangleCount()); + str += ", Version: " + intToStr(model->getFileVersion()); } - else { - if(f) { - fclose(f); + + return str; + } + + void MainWindow::onKeyDown(wxKeyEvent &e) { + + try { + // std::cout << "e.ControlDown() = " << e.ControlDown() << " e.GetKeyCode() = " << e.GetKeyCode() << " isCtrl = " << (e.GetKeyCode() == WXK_CONTROL) << std::endl; + + // Note: This ctrl-key handling is buggy since it never resets when ctrl is released later, so I reset it at end of loadcommands for now. + if (e.ControlDown() == true || e.GetKeyCode() == WXK_CONTROL) { + isControlKeyPressed = true; + } else { + isControlKeyPressed = false; } - } - } - else { - //string screenShotsPath = extractDirectoryPathFromFile(appPath) + string("screens/"); - string userData = Config::getInstance().getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - string screenShotsPath = userData + string("screens/"); - printf("screenShotsPath [%s]\n",screenShotsPath.c_str()); - if(isdir(screenShotsPath.c_str()) == false) { - createDirectoryPaths(screenShotsPath); - } + // std::cout << "isControlKeyPressed = " << isControlKeyPressed << std::endl; - string path = screenShotsPath; - if(isdir(path.c_str()) == true) { - //Config &config= Config::getInstance(); - //string fileFormat = config.getString("ScreenShotFileType","jpg"); - for(int i=0; i < 5000; ++i) { - path = screenShotsPath; - path += string("screen") + intToStr(i) + string(".") + fileFormat; -#ifdef WIN32 - FILE*f= _wfopen(utf8_decode(path).c_str(), L"rb"); -#else - FILE *f= fopen(path.c_str(), "rb"); -#endif - if(f == NULL) { - renderer->saveScreen(path,&overrideSize); - break; + // here also because + and - hotkeys don't work for numpad automaticly + if (e.GetKeyCode() == 388) { + speed *= 1.5f; //numpad+ + if (speed > 1.0) { + speed = 1.0; } - else { - if(f) { - fclose(f); - } + + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + + } else if (e.GetKeyCode() == 390) { + speed /= 1.5f; //numpad- + if (speed < 0) { + speed = 0; } - } - } - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); + GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); + } else if (e.GetKeyCode() == 'W') { + glClearColor(0.6f, 0.6f, 0.6f, 1.0f); //w key //backgroundcolor constant 0.3 -> 0.6 -void MainWindow::onMenuFileSaveScreenshot(wxCommandEvent &event) { - saveScreenshot(); -} - -void MainWindow::onMenuFileClearAll(wxCommandEvent &event) { - try { - //printf("Start onMenuFileClearAll\n"); - //fflush(stdout); - - modelPathList.clear(); - particlePathList.clear(); - particleProjectilePathList.clear(); - particleSplashPathList.clear(); // as above - - if(timer) timer->Stop(); - if(renderer) renderer->end(); - - unitParticleSystems.clear(); - unitParticleSystemTypes.clear(); - - projectileParticleSystems.clear(); - projectileParticleSystemTypes.clear(); - splashParticleSystems.clear(); // as above - splashParticleSystemTypes.clear(); - - //delete model; - //model = NULL; - if(model != NULL && renderer != NULL) renderer->endModel(rsGlobal, model); - model = NULL; - - loadUnit("",""); - loadModel(""); - loadParticle(""); - loadProjectileParticle(""); - loadSplashParticle(""); // as above - - GetStatusBar()->SetStatusText(ToUnicode(statusbarText.c_str())); - isControlKeyPressed = false; - - //printf("END onMenuFileClearAll\n"); - //fflush(stdout); - - if(timer) timer->Start(100); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuFileExit(wxCommandEvent &event) { - Close(); -} - -void MainWindow::loadUnit(string path, string skillName) { - if(path != "" && fileExists(path) == true) { - // std::cout << "Clearing list..." << std::endl; - this->unitPath.first = path; - this->unitPath.second.push_back(skillName); - } - - try{ - if(this->unitPath.first != "") { - if(timer) timer->Stop(); - if(renderer) renderer->end(); - - string titlestring = winHeader; - - string unitPath = this->unitPath.first; - string dir = unitPath; - string name= lastDir(dir); - string path= dir + "/" + name + ".xml"; - - titlestring = unitPath + " - "+ titlestring; - - std::string unitXML = path; - - string skillModelFile = ""; - string skillParticleFile = ""; - string skillParticleProjectileFile = ""; - string skillParticleSplashFile = ""; - bool fileFound = fileExists(unitXML); - - printf("Loading unit from file [%s] fileFound = %d\n",unitXML.c_str(),fileFound); - - if(fileFound == true) { - XmlTree xmlTree; - xmlTree.load(unitXML,Properties::getTagReplacementValues()); - const XmlNode *unitNode= xmlTree.getRootNode(); - - bool foundSkillName = false; - for(unsigned int skillIdx = 0; foundSkillName == false && skillIdx < this->unitPath.second.size(); ++skillIdx) { - string lookipForSkillName = this->unitPath.second[skillIdx]; - - const XmlNode *skillsNode= unitNode->getChild("skills"); - for(unsigned int i = 0; foundSkillName == false && i < skillsNode->getChildCount(); ++i) { - const XmlNode *sn= skillsNode->getChild("skill", i); - //const XmlNode *typeNode= sn->getChild("type"); - const XmlNode *nameNode= sn->getChild("name"); - string skillXmlName = nameNode->getAttribute("value")->getRestrictedValue(); - if(skillXmlName == lookipForSkillName) { - printf("Found skill [%s]\n",lookipForSkillName.c_str()); - foundSkillName = true; - - if(sn->getChild("animation") != NULL) { - skillModelFile = sn->getChild("animation")->getAttribute("path")->getRestrictedValue(unitPath + '/'); - printf("Found skill model [%s]\n",skillModelFile.c_str()); - } - - if(sn->hasChild("particles") == true) { - const XmlNode *particlesNode= sn->getChild("particles"); - //for(int j = 0; particlesNode != NULL && particlesNode->getAttribute("value")->getRestrictedValue() == "true" && - // j < particlesNode->getChildCount(); ++j) { - if(particlesNode != NULL && particlesNode->getAttribute("value")->getRestrictedValue() == "true" && - particlesNode->hasChild("particle-file") == true) { - const XmlNode *pf= particlesNode->getChild("particle-file"); - if(pf != NULL) { - skillParticleFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); - printf("Found skill particle [%s]\n",skillParticleFile.c_str()); - } - } - } - if(sn->hasChild("projectile") == true) { - const XmlNode *particlesProjectileNode= sn->getChild("projectile"); - //for(int j = 0; particlesProjectileNode != NULL && particlesProjectileNode->getAttribute("value")->getRestrictedValue() == "true" && - // j < particlesProjectileNode->getChildCount(); ++j) { - if(particlesProjectileNode != NULL && particlesProjectileNode->getAttribute("value")->getRestrictedValue() == "true" && - particlesProjectileNode->hasChild("particle") == true) { - const XmlNode *pf= particlesProjectileNode->getChild("particle"); - if(pf != NULL && pf->getAttribute("value")->getRestrictedValue() == "true") { - skillParticleProjectileFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); - printf("Found skill skill projectile particle [%s]\n",skillParticleProjectileFile.c_str()); - } - } - } - if(sn->hasChild("splash") == true) { - const XmlNode *particlesSplashNode= sn->getChild("splash"); - //for(int j = 0; particlesSplashNode != NULL && particlesSplashNode->getAttribute("value")->getRestrictedValue() == "true" && - // j < particlesSplashNode->getChildCount(); ++j) { - if(particlesSplashNode != NULL && particlesSplashNode->getAttribute("value")->getRestrictedValue() == "true" && - particlesSplashNode->hasChild("particle") == true) { - const XmlNode *pf= particlesSplashNode->getChild("particle"); - if(pf != NULL && pf->getAttribute("value")->getRestrictedValue() == "true") { - skillParticleSplashFile = unitPath + '/' + pf->getAttribute("path")->getRestrictedValue(); - printf("Found skill skill splash particle [%s]\n",skillParticleSplashFile.c_str()); - } - } - } - } - } - } - - if(skillModelFile != "") { - this->modelPathList.push_back(skillModelFile); - printf("Added skill model [%s]\n",skillModelFile.c_str()); - } - if(skillParticleFile != "") { - this->particlePathList.push_back(skillParticleFile); - printf("Added skill particle [%s]\n",skillParticleFile.c_str()); - } - if(skillParticleProjectileFile != "") { - this->particleProjectilePathList.push_back(skillParticleProjectileFile); - printf("Added skill projectile particle [%s]\n",skillParticleProjectileFile.c_str()); - } - if(skillParticleSplashFile != "") { - this->particleSplashPathList.push_back(skillParticleSplashFile); - printf("Added skill splash particle [%s]\n",skillParticleSplashFile.c_str()); - } - } - SetTitle(ToUnicode(titlestring)); - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::loadModel(string path) { - try { - if(path != "" && fileExists(path) == true) { - this->modelPathList.push_back(path); - printf("Adding model [%s] list size " MG_SIZE_T_SPECIFIER "\n",path.c_str(),this->modelPathList.size()); - } - - string titlestring=winHeader; - for(unsigned int idx =0; idx < this->modelPathList.size(); idx++) { - string modelPath = this->modelPathList[idx]; - - //printf("Loading model [%s] %u of " MG_SIZE_T_SPECIFIER "\n",modelPath.c_str(),idx, this->modelPathList.size()); - - if(timer) timer->Stop(); - //delete model; - if(model != NULL && renderer != NULL) renderer->endModel(rsGlobal, model); - model = NULL; - model = renderer? renderer->newModel(rsGlobal, modelPath): NULL; - - statusbarText = getModelInfo(); - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - if(timer) timer->Start(100); - titlestring = extractFileFromDirectoryPath(modelPath) + " - "+ titlestring; - } - SetTitle(ToUnicode(titlestring)); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::loadParticle(string path) { - if(timer) timer->Stop(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str()); - if(path != "" && fileExists(path) == true) { - renderer->end(); - unitParticleSystems.clear(); - unitParticleSystemTypes.clear(); - - if(isControlKeyPressed == true) { - // std::cout << "Adding to list..." << std::endl; - this->particlePathList.push_back(path); - } - else { - // std::cout << "Clearing list..." << std::endl; - this->particlePathList.clear(); - this->particlePathList.push_back(path); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] added file [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str()); - } - - try { - if(this->particlePathList.empty() == false) { - string titlestring=winHeader; - for(unsigned int idx = 0; idx < this->particlePathList.size(); idx++) { - string particlePath = this->particlePathList[idx]; - string dir= extractDirectoryPathFromFile(particlePath); - - size_t pos = dir.find_last_of(folderDelimiter); - if(pos == dir.length() - 1 && dir.length() > 0) { - dir.erase(dir.length() -1); - } - - particlePath= extractFileFromDirectoryPath(particlePath); - titlestring = particlePath + " - "+ titlestring; - - std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] looking for unit XML [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unitXML.c_str()); - - //int size = -1; - //int height = -1; - int size = 0; - int height= 0; - - if(fileExists(unitXML) == true) { - { - XmlTree xmlTree; - xmlTree.load(unitXML,Properties::getTagReplacementValues()); - const XmlNode *unitNode= xmlTree.getRootNode(); - const XmlNode *parametersNode= unitNode->getChild("parameters"); - //size - size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); - //height - height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); } - // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; - } - else { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] unit XML NOT FOUND [%s] using default position values\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unitXML.c_str()); - - size = 1; - height= 1; - } - - std::map > > loadedFileList; - UnitParticleSystemType *unitParticleSystemType = new UnitParticleSystemType(); - unitParticleSystemType->load(NULL, dir, dir + folderDelimiter + particlePath, //### if we knew which particle it was, we could be more accurate - renderer,loadedFileList,"g3dviewer",""); - unitParticleSystemTypes.push_back(unitParticleSystemType); - - for(std::vector::const_iterator it= unitParticleSystemTypes.begin(); it != unitParticleSystemTypes.end(); ++it) { - UnitParticleSystem *ups= new UnitParticleSystem(200); - - ups->setParticleType((*it)); - (*it)->setValues(ups); - if(size > 0) { - //getCurrVectorFlat() + Vec3f(0.f, type->getHeight()/2.f, 0.f); - Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); - ups->setPos(vec); - } - //ups->setFactionColor(getFaction()->getTexture()->getPixmap()->getPixel3f(0,0)); - ups->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0,0)); - unitParticleSystems.push_back(ups); - renderer->manageParticleSystem(ups); - - ups->setVisible(true); - } - - if(path != "" && fileExists(path) == true) { - renderer->initModelManager(); - if(initTextureManager) { - renderer->initTextureManager(); - } - } - - } - SetTitle(ToUnicode(titlestring)); - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a Mega-Glest particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); - } - if(timer) timer->Start(100); -} - -void MainWindow::loadProjectileParticle(string path) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s] particleProjectilePathList.size() = " MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),this->particleProjectilePathList.size()); - - if(timer) timer->Stop(); - if(path != "" && fileExists(path) == true) { - renderer->end(); - projectileParticleSystems.clear(); - projectileParticleSystemTypes.clear(); - - if(isControlKeyPressed == true) { - // std::cout << "Adding to list..." << std::endl; - this->particleProjectilePathList.push_back(path); - } - else { - // std::cout << "Clearing list..." << std::endl; - this->particleProjectilePathList.clear(); - this->particleProjectilePathList.push_back(path); - } - } - - try { - if(this->particleProjectilePathList.empty() == false) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("this->particleProjectilePathList.size() = " MG_SIZE_T_SPECIFIER "\n",this->particleProjectilePathList.size()); - - string titlestring=winHeader; - for(unsigned int idx = 0; idx < this->particleProjectilePathList.size(); idx++) { - string particlePath = this->particleProjectilePathList[idx]; - string dir= extractDirectoryPathFromFile(particlePath); - - size_t pos = dir.find_last_of(folderDelimiter); - if(pos == dir.length()-1) { - dir.erase(dir.length() -1); - } - - particlePath= extractFileFromDirectoryPath(particlePath); - titlestring = particlePath + " - "+ titlestring; - - std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; - - int size = 1; - int height = 1; - - if(fileExists(unitXML) == true) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unitXML.c_str(),idx); - - XmlTree xmlTree; - xmlTree.load(unitXML,Properties::getTagReplacementValues()); - const XmlNode *unitNode= xmlTree.getRootNode(); - const XmlNode *parametersNode= unitNode->getChild("parameters"); - //size - size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); - //height - height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); - } - - // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; - - string particleFile = dir + folderDelimiter + particlePath; - { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,particleFile.c_str(),idx); - XmlTree xmlTree; - xmlTree.load(particleFile,Properties::getTagReplacementValues()); - //const XmlNode *particleSystemNode= xmlTree.getRootNode(); - - // std::cout << "Loaded successfully, loading values..." << std::endl; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loading [%s] idx = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,particleFile.c_str(),idx); - std::map > > loadedFileList; - ParticleSystemTypeProjectile *projectileParticleSystemType= new ParticleSystemTypeProjectile(); - projectileParticleSystemType->load(NULL, dir, //### we don't know if there are overrides in the unit XML - particleFile,renderer, loadedFileList, - "g3dviewer",""); - - // std::cout << "Values loaded, about to read..." << std::endl; - - projectileParticleSystemTypes.push_back(projectileParticleSystemType); - - for(std::vector::const_iterator it= projectileParticleSystemTypes.begin(); - it != projectileParticleSystemTypes.end(); ++it) { - - ProjectileParticleSystem *ps = (*it)->create(NULL); - - if(size > 0) { - Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); - //ps->setPos(vec); - - Vec3f vec2 = Vec3f(size * 2.f, height * 2.f, height * 2.f); - ps->setPath(vec, vec2); + // some posibility to adjust brightness: + /* + else if (e.GetKeyCode() == 322) { // Ins - Grid + gridBrightness += 0.1f; if (gridBrightness >1.0) gridBrightness =1.0; } - ps->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0,0)); - - projectileParticleSystems.push_back(ps); - - ps->setVisible(true); - renderer->manageParticleSystem(ps); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] loaded [%s] idx = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,particleFile.c_str(),idx); - } - SetTitle(ToUnicode(titlestring)); - - if(path != "" && fileExists(path) == true) { - renderer->initModelManager(); - if(initTextureManager) { - renderer->initTextureManager(); - } - } - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest projectile particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); - } - if(timer) timer->Start(100); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] after load [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str()); -} - -void MainWindow::loadSplashParticle(string path) { // uses ParticleSystemTypeSplash::load (and own list...) - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] about to load [%s] particleSplashPathList.size() = " MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),this->particleSplashPathList.size()); - - if(timer) timer->Stop(); - if(path != "" && fileExists(path) == true) { - renderer->end(); - splashParticleSystems.clear(); - splashParticleSystemTypes.clear(); - - if(isControlKeyPressed == true) { - // std::cout << "Adding to list..." << std::endl; - this->particleSplashPathList.push_back(path); - } - else { - // std::cout << "Clearing list..." << std::endl; - this->particleSplashPathList.clear(); - this->particleSplashPathList.push_back(path); - } - } - - try { - if(this->particleSplashPathList.empty() == false) { - string titlestring=winHeader; - for(unsigned int idx = 0; idx < this->particleSplashPathList.size(); idx++) { - string particlePath = this->particleSplashPathList[idx]; - string dir= extractDirectoryPathFromFile(particlePath); - - size_t pos = dir.find_last_of(folderDelimiter); - if(pos == dir.length()-1) { - dir.erase(dir.length() -1); - } - - particlePath= extractFileFromDirectoryPath(particlePath); - titlestring = particlePath + " - "+ titlestring; - - std::string unitXML = dir + folderDelimiter + extractFileFromDirectoryPath(dir) + ".xml"; - - int size = 1; - //int height = 1; - - if(fileExists(unitXML) == true) { - XmlTree xmlTree; - xmlTree.load(unitXML,Properties::getTagReplacementValues()); - const XmlNode *unitNode= xmlTree.getRootNode(); - const XmlNode *parametersNode= unitNode->getChild("parameters"); - //size - size= parametersNode->getChild("size")->getAttribute("value")->getIntValue(); - //height - //height= parametersNode->getChild("height")->getAttribute("value")->getIntValue(); - } - - // std::cout << "About to load [" << particlePath << "] from [" << dir << "] unit [" << unitXML << "]" << std::endl; - - { - XmlTree xmlTree; - xmlTree.load(dir + folderDelimiter + particlePath,Properties::getTagReplacementValues()); - //const XmlNode *particleSystemNode= xmlTree.getRootNode(); - // std::cout << "Loaded successfully, loading values..." << std::endl; - } - - std::map > > loadedFileList; - ParticleSystemTypeSplash *splashParticleSystemType= new ParticleSystemTypeSplash(); - splashParticleSystemType->load(NULL, dir, dir + folderDelimiter + particlePath,renderer, //### we don't know if there are overrides in the unit XML - loadedFileList,"g3dviewer",""); // <---- only that must be splash... - - // std::cout << "Values loaded, about to read..." << std::endl; - - splashParticleSystemTypes.push_back(splashParticleSystemType); - - //ParticleSystemTypeSplash - for(std::vector::const_iterator it= splashParticleSystemTypes.begin(); it != splashParticleSystemTypes.end(); ++it) { - - SplashParticleSystem *ps = (*it)->create(NULL); - - if(size > 0) { - //Vec3f vec = Vec3f(0.f, height / 2.f, 0.f); - //ps->setPos(vec); - - //Vec3f vec2 = Vec3f(size * 2.f, height * 2.f, height * 2.f); // <------- removed relative projectile - //ps->setPath(vec, vec2); // <------- removed relative projectile + else if (e.GetKeyCode() == 127) { // Del + gridBrightness -= 0.1f; if (gridBrightness <0) gridBrightness =0; + } + */ + else if (e.GetKeyCode() == 313) { // Home - Background + backBrightness += 0.1f; if (backBrightness > 1.0) backBrightness = 1.0; + glClearColor(backBrightness, backBrightness, backBrightness, 1.0f); + } else if (e.GetKeyCode() == 312) { // End + backBrightness -= 0.1f; if (backBrightness < 0) backBrightness = 0; + glClearColor(backBrightness, backBrightness, backBrightness, 1.0f); + } else if (e.GetKeyCode() == 366) { // PgUp - Lightning of model + lightBrightness += 0.1f; if (lightBrightness > 1.0) lightBrightness = 1.0; + Vec4f ambientNEW = Vec4f(lightBrightness, lightBrightness, lightBrightness, 1.0f); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambientNEW.ptr()); + } else if (e.GetKeyCode() == 367) { // pgDn + lightBrightness -= 0.1f; if (lightBrightness < 0) lightBrightness = 0; + Vec4f ambientNEW = Vec4f(lightBrightness, lightBrightness, lightBrightness, 1.0f); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambientNEW.ptr()); } - ps->setFactionColor(renderer->getPlayerColorTexture(playerColor)->getPixmap()->getPixel3f(0,0)); - splashParticleSystems.push_back(ps); - - ps->setVisible(true); - renderer->manageParticleSystem(ps); + std::cout << "pressed " << e.GetKeyCode() << std::endl; + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); } } - SetTitle(ToUnicode(titlestring)); - if(path != "" && fileExists(path) == true) { - renderer->initModelManager(); - if(initTextureManager) { - renderer->initTextureManager(); - } - } - } - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Not a ZetaGlest projectile particle XML file, or broken"), wxOK | wxICON_ERROR).ShowModal(); - } - if(timer) timer->Start(100); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] after load [%s] particleSplashPathList.size() = " MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str(),this->particleSplashPathList.size()); -} + void MainWindow::onMenuRestart(wxCommandEvent &event) { + try { + // std::cout << "pressed R (restart particle animation)" << std::endl; + if (timer) timer->Stop(); + if (renderer) renderer->end(); -void MainWindow::onMenuModeNormals(wxCommandEvent &event){ - try { - renderer->toggleNormals(); - menuMode->Check(miModeNormals, renderer->getNormals()); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + unitParticleSystems.clear(); + unitParticleSystemTypes.clear(); + projectileParticleSystems.clear(); + projectileParticleSystemTypes.clear(); + splashParticleSystems.clear(); // as above + splashParticleSystemTypes.clear(); -void MainWindow::onMenuModeWireframe(wxCommandEvent &event){ - try { - renderer->toggleWireframe(); - menuMode->Check(miModeWireframe, renderer->getWireframe()); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + loadUnit("", ""); + loadModel(""); + loadParticle(""); + loadProjectileParticle(""); + loadSplashParticle(""); // as above -void MainWindow::onMenuModeGrid(wxCommandEvent &event){ - try { - renderer->toggleGrid(); - menuMode->Check(miModeGrid, renderer->getGrid()); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuSpeedSlower(wxCommandEvent &event){ - try { - speed /= 1.5f; - if(speed < 0) { - speed = 0; - } - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuSpeedFaster(wxCommandEvent &event){ - try { - speed *= 1.5f; - if(speed > 1) { - speed = 1; - } - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0 ) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -// set menu checkboxes to what player color is used -void MainWindow::onMenuColorRed(wxCommandEvent &event) { - try { - playerColor= Renderer::pcRed; - menuCustomColor->Check(miColorRed, true); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorBlue(wxCommandEvent &event) { - try { - playerColor= Renderer::pcBlue; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, true); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorGreen(wxCommandEvent &event) { - try { - playerColor= Renderer::pcGreen; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, true); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorYellow(wxCommandEvent &event) { - try { - playerColor= Renderer::pcYellow; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, true); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorWhite(wxCommandEvent &event) { - try { - playerColor= Renderer::pcWhite; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, true); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorCyan(wxCommandEvent &event) { - try { - playerColor= Renderer::pcCyan; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, true); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorOrange(wxCommandEvent &event) { - try { - playerColor= Renderer::pcOrange; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, true); - menuCustomColor->Check(miColorMagenta, false); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - -void MainWindow::onMenuColorMagenta(wxCommandEvent &event) { - try { - playerColor= Renderer::pcMagenta; - menuCustomColor->Check(miColorRed, false); - menuCustomColor->Check(miColorBlue, false); - menuCustomColor->Check(miColorGreen, false); - menuCustomColor->Check(miColorYellow, false); - menuCustomColor->Check(miColorWhite, false); - menuCustomColor->Check(miColorCyan, false); - menuCustomColor->Check(miColorOrange, false); - menuCustomColor->Check(miColorMagenta, true); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} - - -void MainWindow::onTimer(wxTimerEvent &event) { - anim = anim + speed; - if(anim > 1.0f){ - anim -= 1.f; - resetAnimation = true; - } - wxPaintEvent paintEvent; - onPaint(paintEvent); -} - -string MainWindow::getModelInfo() { - string str; - - if(model != NULL) { - str+= "Meshes: "+intToStr(model->getMeshCount()); - str+= ", Vertices: "+intToStr(model->getVertexCount()); - str+= ", Triangles: "+intToStr(model->getTriangleCount()); - str+= ", Version: "+intToStr(model->getFileVersion()); - } - - return str; -} - -void MainWindow::onKeyDown(wxKeyEvent &e) { - - try { - // std::cout << "e.ControlDown() = " << e.ControlDown() << " e.GetKeyCode() = " << e.GetKeyCode() << " isCtrl = " << (e.GetKeyCode() == WXK_CONTROL) << std::endl; - - // Note: This ctrl-key handling is buggy since it never resets when ctrl is released later, so I reset it at end of loadcommands for now. - if(e.ControlDown() == true || e.GetKeyCode() == WXK_CONTROL) { - isControlKeyPressed = true; - } - else { - isControlKeyPressed = false; - } - - // std::cout << "isControlKeyPressed = " << isControlKeyPressed << std::endl; - - - // here also because + and - hotkeys don't work for numpad automaticly - if (e.GetKeyCode() == 388) { - speed *= 1.5f; //numpad+ - if(speed > 1.0) { - speed = 1.0; + renderer->initModelManager(); + if (initTextureManager) { + renderer->initTextureManager(); + } + if (timer) timer->Start(100); + } catch (std::runtime_error &e) { + std::cout << e.what() << std::endl; + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); } - - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - - } - else if (e.GetKeyCode() == 390) { - speed /= 1.5f; //numpad- - if(speed < 0) { - speed = 0; - } - string statusTextValue = statusbarText + " animation speed: " + floatToStr(speed * 1000.0) + " anim value: " + floatToStr(anim) + " zoom: " + floatToStr(zoom) + " rotX: " + floatToStr(rotX) + " rotY: " + floatToStr(rotY); - GetStatusBar()->SetStatusText(ToUnicode(statusTextValue.c_str())); - } - else if (e.GetKeyCode() == 'W') { - glClearColor(0.6f, 0.6f, 0.6f, 1.0f); //w key //backgroundcolor constant 0.3 -> 0.6 - } - // some posibility to adjust brightness: - /* - else if (e.GetKeyCode() == 322) { // Ins - Grid - gridBrightness += 0.1f; if (gridBrightness >1.0) gridBrightness =1.0; - } - else if (e.GetKeyCode() == 127) { // Del - gridBrightness -= 0.1f; if (gridBrightness <0) gridBrightness =0; - } - */ - else if (e.GetKeyCode() == 313) { // Home - Background - backBrightness += 0.1f; if (backBrightness >1.0) backBrightness=1.0; - glClearColor(backBrightness, backBrightness, backBrightness, 1.0f); - } - else if (e.GetKeyCode() == 312) { // End - backBrightness -= 0.1f; if (backBrightness<0) backBrightness=0; - glClearColor(backBrightness, backBrightness, backBrightness, 1.0f); - } - else if (e.GetKeyCode() == 366) { // PgUp - Lightning of model - lightBrightness += 0.1f; if (lightBrightness >1.0) lightBrightness =1.0; - Vec4f ambientNEW= Vec4f(lightBrightness, lightBrightness, lightBrightness, 1.0f); - glLightfv(GL_LIGHT0,GL_AMBIENT, ambientNEW.ptr()); - } - else if (e.GetKeyCode() == 367) { // pgDn - lightBrightness -= 0.1f; if (lightBrightness <0) lightBrightness =0; - Vec4f ambientNEW= Vec4f(lightBrightness, lightBrightness, lightBrightness, 1.0f); - glLightfv(GL_LIGHT0,GL_AMBIENT, ambientNEW.ptr()); - } + BEGIN_EVENT_TABLE(MainWindow, wxFrame) + EVT_TIMER(-1, MainWindow::onTimer) + EVT_CLOSE(MainWindow::onClose) + EVT_MENU(miFileLoad, MainWindow::onMenuFileLoad) + EVT_MENU(miFileLoadParticleXML, MainWindow::onMenuFileLoadParticleXML) + EVT_MENU(miFileLoadProjectileParticleXML, MainWindow::onMenuFileLoadProjectileParticleXML) + EVT_MENU(miFileLoadSplashParticleXML, MainWindow::onMenuFileLoadSplashParticleXML) + EVT_MENU(miFileClearAll, MainWindow::onMenuFileClearAll) + EVT_MENU(miFileToggleScreenshotTransparent, MainWindow::onMenumFileToggleScreenshotTransparent) + EVT_MENU(miFileSaveScreenshot, MainWindow::onMenuFileSaveScreenshot) + EVT_MENU(wxID_EXIT, MainWindow::onMenuFileExit) - std::cout << "pressed " << e.GetKeyCode() << std::endl; - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + EVT_MENU(miModeWireframe, MainWindow::onMenuModeWireframe) + EVT_MENU(miModeNormals, MainWindow::onMenuModeNormals) + EVT_MENU(miModeGrid, MainWindow::onMenuModeGrid) -void MainWindow::onMenuRestart(wxCommandEvent &event) { - try { - // std::cout << "pressed R (restart particle animation)" << std::endl; - if(timer) timer->Stop(); - if(renderer) renderer->end(); + EVT_MENU(miSpeedFaster, MainWindow::onMenuSpeedFaster) + EVT_MENU(miSpeedSlower, MainWindow::onMenuSpeedSlower) + EVT_MENU(miRestart, MainWindow::onMenuRestart) - unitParticleSystems.clear(); - unitParticleSystemTypes.clear(); - projectileParticleSystems.clear(); - projectileParticleSystemTypes.clear(); - splashParticleSystems.clear(); // as above - splashParticleSystemTypes.clear(); + EVT_MENU(miColorRed, MainWindow::onMenuColorRed) + EVT_MENU(miColorBlue, MainWindow::onMenuColorBlue) + EVT_MENU(miColorGreen, MainWindow::onMenuColorGreen) + EVT_MENU(miColorYellow, MainWindow::onMenuColorYellow) + EVT_MENU(miColorWhite, MainWindow::onMenuColorWhite) + EVT_MENU(miColorCyan, MainWindow::onMenuColorCyan) + EVT_MENU(miColorOrange, MainWindow::onMenuColorOrange) + EVT_MENU(miColorMagenta, MainWindow::onMenuColorMagenta) - loadUnit("", ""); - loadModel(""); - loadParticle(""); - loadProjectileParticle(""); - loadSplashParticle(""); // as above + EVT_MENU(miChangeBackgroundColor, MainWindow::OnChangeColor) + END_EVENT_TABLE() - renderer->initModelManager(); - if(initTextureManager) { - renderer->initTextureManager(); - } - if(timer) timer->Start(100); - } - catch(std::runtime_error &e) { - std::cout << e.what() << std::endl; - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Error"), wxOK | wxICON_ERROR).ShowModal(); - } -} + // ===================================================== + // class GlCanvas + // ===================================================== -BEGIN_EVENT_TABLE(MainWindow, wxFrame) - EVT_TIMER(-1, MainWindow::onTimer) - EVT_CLOSE(MainWindow::onClose) - EVT_MENU(miFileLoad, MainWindow::onMenuFileLoad) - EVT_MENU(miFileLoadParticleXML, MainWindow::onMenuFileLoadParticleXML) - EVT_MENU(miFileLoadProjectileParticleXML, MainWindow::onMenuFileLoadProjectileParticleXML) - EVT_MENU(miFileLoadSplashParticleXML, MainWindow::onMenuFileLoadSplashParticleXML) - EVT_MENU(miFileClearAll, MainWindow::onMenuFileClearAll) - EVT_MENU(miFileToggleScreenshotTransparent, MainWindow::onMenumFileToggleScreenshotTransparent) - EVT_MENU(miFileSaveScreenshot, MainWindow::onMenuFileSaveScreenshot) - EVT_MENU(wxID_EXIT, MainWindow::onMenuFileExit) - - EVT_MENU(miModeWireframe, MainWindow::onMenuModeWireframe) - EVT_MENU(miModeNormals, MainWindow::onMenuModeNormals) - EVT_MENU(miModeGrid, MainWindow::onMenuModeGrid) - - EVT_MENU(miSpeedFaster, MainWindow::onMenuSpeedFaster) - EVT_MENU(miSpeedSlower, MainWindow::onMenuSpeedSlower) - EVT_MENU(miRestart, MainWindow::onMenuRestart) - - EVT_MENU(miColorRed, MainWindow::onMenuColorRed) - EVT_MENU(miColorBlue, MainWindow::onMenuColorBlue) - EVT_MENU(miColorGreen, MainWindow::onMenuColorGreen) - EVT_MENU(miColorYellow, MainWindow::onMenuColorYellow) - EVT_MENU(miColorWhite, MainWindow::onMenuColorWhite) - EVT_MENU(miColorCyan, MainWindow::onMenuColorCyan) - EVT_MENU(miColorOrange, MainWindow::onMenuColorOrange) - EVT_MENU(miColorMagenta, MainWindow::onMenuColorMagenta) - - EVT_MENU(miChangeBackgroundColor, MainWindow::OnChangeColor) -END_EVENT_TABLE() - -// ===================================================== -// class GlCanvas -// ===================================================== - -void translateCoords(wxWindow *wnd, int &x, int &y) { + void translateCoords(wxWindow *wnd, int &x, int &y) { #ifdef WIN32 - int cx, cy; - wnd->GetPosition(&cx, &cy); - x += cx; - y += cy; + int cx, cy; + wnd->GetPosition(&cx, &cy); + x += cx; + y += cy; #endif -} + } -// to prevent flicker -GlCanvas::GlCanvas(MainWindow * mainWindow, int *args) + // to prevent flicker + GlCanvas::GlCanvas(MainWindow * mainWindow, int *args) #if wxCHECK_VERSION(2, 9, 1) - : wxGLCanvas(mainWindow, wxID_ANY, args, wxDefaultPosition, mainWindow->GetClientSize(), wxFULL_REPAINT_ON_RESIZE, wxT("GLCanvas")) { - this->context = new wxGLContext(this); + : wxGLCanvas(mainWindow, wxID_ANY, args, wxDefaultPosition, mainWindow->GetClientSize(), wxFULL_REPAINT_ON_RESIZE, wxT("GLCanvas")) { + this->context = new wxGLContext(this); #else - : wxGLCanvas(mainWindow, -1, wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"), args) { - this->context = NULL; + : wxGLCanvas(mainWindow, -1, wxDefaultPosition, wxDefaultSize, 0, wxT("GLCanvas"), args) { + this->context = NULL; #endif - this->mainWindow = mainWindow; -} + this->mainWindow = mainWindow; + } -GlCanvas::~GlCanvas() { - if(this->context) { - delete this->context; - } - this->context = NULL; -} + GlCanvas::~GlCanvas() { + if (this->context) { + delete this->context; + } + this->context = NULL; + } -void GlCanvas::setCurrentGLContext() { + void GlCanvas::setCurrentGLContext() { #ifndef __APPLE__ #if wxCHECK_VERSION(3, 0, 0) - //printf("Setting glcontext 3x!\n"); + //printf("Setting glcontext 3x!\n"); - //if(!IsShown()) {} - if(this->context == NULL) { - //printf("Make new ctx!\n"); - this->context = new wxGLContext(this); - //printf("Set ctx [%p]\n",this->context); - } + //if(!IsShown()) {} + if (this->context == NULL) { + //printf("Make new ctx!\n"); + this->context = new wxGLContext(this); + //printf("Set ctx [%p]\n",this->context); + } #elif wxCHECK_VERSION(2, 9, 1) - //printf("Setting glcontext 29x!\n"); + //printf("Setting glcontext 29x!\n"); - //if(!IsShown()) {} - if(this->context == NULL) { - this->context = new wxGLContext(this); - //printf("Set ctx [%p]\n",this->context); - } + //if(!IsShown()) {} + if (this->context == NULL) { + this->context = new wxGLContext(this); + //printf("Set ctx [%p]\n",this->context); + } #endif - //printf("Set ctx [%p]\n",this->context); - if(this->context) { - wxGLCanvas::SetCurrent(*this->context); - //printf("Set ctx2 [%p]\n",this->context); - } + //printf("Set ctx [%p]\n",this->context); + if (this->context) { + wxGLCanvas::SetCurrent(*this->context); + //printf("Set ctx2 [%p]\n",this->context); + } #else - this->SetCurrent(); + this->SetCurrent(); #endif -} + } -// for the mousewheel -void GlCanvas::onMouseWheel(wxMouseEvent &event) { - if(event.GetWheelRotation()>0) mainWindow->onMouseWheelDown(event); - else mainWindow->onMouseWheelUp(event); -} + // for the mousewheel + void GlCanvas::onMouseWheel(wxMouseEvent &event) { + if (event.GetWheelRotation() > 0) mainWindow->onMouseWheelDown(event); + else mainWindow->onMouseWheelUp(event); + } -void GlCanvas::onMouseMove(wxMouseEvent &event){ - mainWindow->onMouseMove(event); -} + void GlCanvas::onMouseMove(wxMouseEvent &event) { + mainWindow->onMouseMove(event); + } -void GlCanvas::onKeyDown(wxKeyEvent &event) { - int x, y; - event.GetPosition(&x, &y); - translateCoords(this, x, y); - mainWindow->onKeyDown(event); -} + void GlCanvas::onKeyDown(wxKeyEvent &event) { + int x, y; + event.GetPosition(&x, &y); + translateCoords(this, x, y); + mainWindow->onKeyDown(event); + } -void GlCanvas::OnSize(wxSizeEvent&event) { + void GlCanvas::OnSize(wxSizeEvent&event) { - //printf("OnSize %dx%d\n",event.m_size.GetWidth(),event.m_size.GetHeight()); - Update(); -} + //printf("OnSize %dx%d\n",event.m_size.GetWidth(),event.m_size.GetHeight()); + Update(); + } -// EVT_SPIN_DOWN(GlCanvas::onMouseDown) -// EVT_SPIN_UP(GlCanvas::onMouseDown) -// EVT_MIDDLE_DOWN(GlCanvas::onMouseWheel) -// EVT_MIDDLE_UP(GlCanvas::onMouseWheel) + // EVT_SPIN_DOWN(GlCanvas::onMouseDown) + // EVT_SPIN_UP(GlCanvas::onMouseDown) + // EVT_MIDDLE_DOWN(GlCanvas::onMouseWheel) + // EVT_MIDDLE_UP(GlCanvas::onMouseWheel) -BEGIN_EVENT_TABLE(GlCanvas, wxGLCanvas) - EVT_MOUSEWHEEL(GlCanvas::onMouseWheel) - EVT_MOTION(GlCanvas::onMouseMove) - EVT_KEY_DOWN(GlCanvas::onKeyDown) - EVT_SIZE(GlCanvas::OnSize) -END_EVENT_TABLE() + BEGIN_EVENT_TABLE(GlCanvas, wxGLCanvas) + EVT_MOUSEWHEEL(GlCanvas::onMouseWheel) + EVT_MOTION(GlCanvas::onMouseMove) + EVT_KEY_DOWN(GlCanvas::onKeyDown) + EVT_SIZE(GlCanvas::OnSize) + END_EVENT_TABLE() -// =============================================== -// class App -// =============================================== + // =============================================== + // class App + // =============================================== -bool App::OnInit() { - SystemFlags::VERBOSE_MODE_ENABLED = false; + bool App::OnInit() { + SystemFlags::VERBOSE_MODE_ENABLED = false; #if defined(wxMAJOR_VERSION) && defined(wxMINOR_VERSION) && defined(wxRELEASE_NUMBER) && defined(wxSUBRELEASE_NUMBER) - printf("Using wxWidgets version [%d.%d.%d.%d]\n",wxMAJOR_VERSION,wxMINOR_VERSION,wxRELEASE_NUMBER,wxSUBRELEASE_NUMBER); + printf("Using wxWidgets version [%d.%d.%d.%d]\n", wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER, wxSUBRELEASE_NUMBER); #endif - string modelPath=""; - string particlePath=""; - string projectileParticlePath=""; - string splashParticlePath=""; + string modelPath = ""; + string particlePath = ""; + string projectileParticlePath = ""; + string splashParticlePath = ""; - bool foundInvalidArgs = false; - const int knownArgCount = sizeof(GAME_ARGS) / sizeof(GAME_ARGS[0]); - for(int idx = 1; idx < argc; ++idx) { + bool foundInvalidArgs = false; + const int knownArgCount = sizeof(GAME_ARGS) / sizeof(GAME_ARGS[0]); + for (int idx = 1; idx < argc; ++idx) { #if wxCHECK_VERSION(2, 9, 1) - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx].wc_str()); + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx].wc_str()); #else - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx]); + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(argv[idx]); #endif -if( hasCommandArgument(knownArgCount, (wxChar**)&GAME_ARGS[0], (const char *)tmp_buf, NULL, 0, true) == false && - argv[idx][0] == '-') { - foundInvalidArgs = true; + if (hasCommandArgument(knownArgCount, (wxChar**) &GAME_ARGS[0], (const char *) tmp_buf, NULL, 0, true) == false && + argv[idx][0] == '-') { + foundInvalidArgs = true; - printf("\nInvalid argument: %s",(const char*)tmp_buf); - } - } - - if(foundInvalidArgs == true || - hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_HELP])) == true) { - printParameterHelp(static_cast(WX2CHR(argv[0])), foundInvalidArgs); - return false; - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_VERBOSE])) == true) { - SystemFlags::VERBOSE_MODE_ENABLED = true; - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])) == true) { - autoScreenShotAndExit = true; - - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT]); - //printf("param = [%s]\n",(const char*)param); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); -#if wxCHECK_VERSION(2, 9, 1) - string options = argv[foundParamIndIndex].ToStdString(); -#else - string options = (const char *)wxConvCurrent->cWX2MB(argv[foundParamIndIndex]); -#endif - vector paramPartTokens; - Tokenize(options,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string optionsValue = paramPartTokens[1]; - - autoScreenShotParams.clear(); - Tokenize(optionsValue,autoScreenShotParams,","); - - for(unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { - -#ifdef WIN32 - auto_ptr wstr(Ansi2WideString(autoScreenShotParams[i].c_str())); - autoScreenShotParams[i] = utf8_encode(wstr.get()); -#endif - - if(_strnicmp(autoScreenShotParams[i].c_str(),"resize-",7) == 0) { - printf("Screenshot option [%s]\n",autoScreenShotParams[i].c_str()); - - string resize = autoScreenShotParams[i]; - resize = resize.erase(0,7); - vector values; - Tokenize(resize,values,"x"); - overrideSize.first = strToInt(values[0]); - overrideSize.second = strToInt(values[1]); - - Renderer::windowX= 0; - Renderer::windowY= 0; - Renderer::windowW = overrideSize.first; - Renderer::windowH = overrideSize.second + 25; + printf("\nInvalid argument: %s", (const char*) tmp_buf); } } - } - } - std::pair > unitToLoad; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT]); - //printf("param = [%s]\n",(const char*)param); + if (foundInvalidArgs == true || + hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_HELP])) == true) { + printParameterHelp(static_cast(WX2CHR(argv[0])), foundInvalidArgs); + return false; + } - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string customPath = static_cast(WX2CHR(argv[foundParamIndIndex])); - vector paramPartTokens; - Tokenize(customPath,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string customPathValue = paramPartTokens[1]; - std::vector delimitedList; - Tokenize(customPathValue,delimitedList,","); + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_VERBOSE])) == true) { + SystemFlags::VERBOSE_MODE_ENABLED = true; + } - if(delimitedList.size() >= 2) { - unitToLoad.first = delimitedList[0]; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(unitToLoad.first.c_str())); - unitToLoad.first = utf8_encode(wstr.get()); - #endif + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT])) == true) { + autoScreenShotAndExit = true; - for(unsigned int i = 1; i < delimitedList.size(); ++i) { - string value = delimitedList[i]; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(value.c_str())); - value = utf8_encode(wstr.get()); - #endif + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_AUTO_SCREENSHOT]); + //printf("param = [%s]\n",(const char*)param); - unitToLoad.second.push_back(value); - } - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])) == true && - hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])) == false) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL]); - //printf("param = [%s]\n",(const char*)param); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string customPath = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(customPath,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string customPathValue = paramPartTokens[1]; - modelPath = customPathValue; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(modelPath.c_str())); - modelPath = utf8_encode(wstr.get()); - #endif - - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n",(const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])) == true && - hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])) == false) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE]); - //printf("param = [%s]\n",(const char*)param); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string customPath = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(customPath,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string customPathValue = paramPartTokens[1]; - particlePath = customPathValue; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(particlePath.c_str())); - particlePath = utf8_encode(wstr.get()); - #endif - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE]); - //printf("param = [%s]\n",(const char*)param); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string customPath = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(customPath,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string customPathValue = paramPartTokens[1]; - projectileParticlePath = customPathValue; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(projectileParticlePath.c_str())); - projectileParticlePath = utf8_encode(wstr.get()); - #endif - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH]); - //printf("param = [%s]\n",(const char*)param); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string customPath = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(customPath,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - string customPathValue = paramPartTokens[1]; - splashParticlePath = customPathValue; - #ifdef WIN32 - auto_ptr wstr(Ansi2WideString(splashParticlePath.c_str())); - splashParticlePath = utf8_encode(wstr.get()); - #endif - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - float newAnimValue = 0.0f; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - newAnimValue = strToFloat(paramPartTokens[1]); - printf("newAnimValue = %f\n",newAnimValue); - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - int newParticleLoopValue = 1; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - newParticleLoopValue = strToInt(paramPartTokens[1]); - //printf("newParticleLoopValue = %d\n",newParticleLoopValue); - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - float newZoomValue = 1.0f; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - newZoomValue = strToFloat(paramPartTokens[1]); - //printf("newAnimValue = %f\n",newAnimValue); - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - float newXRotValue = 0.0f; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = (const char*)WX2CHR(argv[foundParamIndIndex]); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - newXRotValue = strToFloat(paramPartTokens[1]); - //printf("newAnimValue = %f\n",newAnimValue); - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*)WX2CHR(argv[foundParamIndIndex]),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - float newYRotValue = 0.0f; - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = static_cast(WX2CHR(argv[foundParamIndIndex])); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - newYRotValue = strToFloat(paramPartTokens[1]); - //printf("newAnimValue = %f\n",newAnimValue); - } - else { - printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])),(paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(hasCommandArgument(argc, argv,(const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])) == true) { - const wxWX2MBbuf param = (const char *)wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT]); - - int foundParamIndIndex = -1; - hasCommandArgument(argc, argv,string((const char*)param) + string("="),&foundParamIndIndex); - if(foundParamIndIndex < 0) { - hasCommandArgument(argc, argv,(const char*)param,&foundParamIndIndex); - } - //printf("foundParamIndIndex = %d\n",foundParamIndIndex); - string value = static_cast(WX2CHR(argv[foundParamIndIndex])); - vector paramPartTokens; - Tokenize(value,paramPartTokens,"="); - if(paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { - fileFormat = paramPartTokens[1]; - } - else { - printf("\nInvalid value specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); - printParameterHelp(WX2CHR(argv[0]),false); - return false; - } - } - - if(argc == 2 && argv[1][0] != '-') { - -//#if defined(__MINGW32__) -#ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(argv[1])); - modelPath = tmp_buf; - auto_ptr wstr(Ansi2WideString(modelPath.c_str())); - modelPath = utf8_encode(wstr.get()); + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); +#if wxCHECK_VERSION(2, 9, 1) + string options = argv[foundParamIndIndex].ToStdString(); #else - modelPath = static_cast(WX2CHR(argv[1])); + string options = (const char *) wxConvCurrent->cWX2MB(argv[foundParamIndIndex]); +#endif + vector paramPartTokens; + Tokenize(options, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string optionsValue = paramPartTokens[1]; + + autoScreenShotParams.clear(); + Tokenize(optionsValue, autoScreenShotParams, ","); + + for (unsigned int i = 0; i < autoScreenShotParams.size(); ++i) { + +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(autoScreenShotParams[i].c_str())); + autoScreenShotParams[i] = utf8_encode(wstr.get()); #endif -//#else -// modelPath = wxFNCONV(argv[1]); -//#endif + if (_strnicmp(autoScreenShotParams[i].c_str(), "resize-", 7) == 0) { + printf("Screenshot option [%s]\n", autoScreenShotParams[i].c_str()); - } + string resize = autoScreenShotParams[i]; + resize = resize.erase(0, 7); + vector values; + Tokenize(resize, values, "x"); + overrideSize.first = strToInt(values[0]); + overrideSize.second = strToInt(values[1]); -//#if defined(__MINGW32__) -// const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(argv[0])); -// appPath = tmp_buf; -//#else -// appPath = wxFNCONV(argv[0]); -//#endif -// printf("appPath [%s]\n",argv[0]); + Renderer::windowX = 0; + Renderer::windowY = 0; + Renderer::windowW = overrideSize.first; + Renderer::windowH = overrideSize.second + 25; + } + } + } + } - wxString exe_path = wxStandardPaths::Get().GetExecutablePath(); - //wxString path_separator = wxFileName::GetPathSeparator(); - //exe_path = exe_path.BeforeLast(path_separator[0]); - //exe_path += path_separator; + std::pair > unitToLoad; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_UNIT]); + //printf("param = [%s]\n",(const char*)param); -//#if defined(__MINGW32__) + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string customPath = static_cast(WX2CHR(argv[foundParamIndIndex])); + vector paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string customPathValue = paramPartTokens[1]; + std::vector delimitedList; + Tokenize(customPathValue, delimitedList, ","); + + if (delimitedList.size() >= 2) { + unitToLoad.first = delimitedList[0]; #ifdef WIN32 - const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(exe_path)); - string appPath = tmp_buf; - - auto_ptr wstr(Ansi2WideString(appPath.c_str())); - appPath = utf8_encode(wstr.get()); -#else - string appPath(static_cast(WX2CHR(exe_path))); + auto_ptr wstr(Ansi2WideString(unitToLoad.first.c_str())); + unitToLoad.first = utf8_encode(wstr.get()); #endif -//#else -// appPath = wxFNCONV(exe_path); -//#endif + for (unsigned int i = 1; i < delimitedList.size(); ++i) { + string value = delimitedList[i]; +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(value.c_str())); + value = utf8_encode(wstr.get()); +#endif -// printf("#2 appPath [%s]\n",appPath.c_str()); + unitToLoad.second.push_back(value); + } + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } - mainWindow= new MainWindow( unitToLoad, - modelPath, - particlePath, - projectileParticlePath, - splashParticlePath, - newAnimValue, - newParticleLoopValue, - newZoomValue, - newXRotValue, - newYRotValue, - appPath); - if(autoScreenShotAndExit == true) { + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL])) == true && + hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])) == false) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL]); + //printf("param = [%s]\n",(const char*)param); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string customPath = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string customPathValue = paramPartTokens[1]; + modelPath = customPathValue; +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(modelPath.c_str())); + modelPath = utf8_encode(wstr.get()); +#endif + + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE])) == true && + hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])) == false) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE]); + //printf("param = [%s]\n",(const char*)param); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string customPath = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string customPathValue = paramPartTokens[1]; + particlePath = customPathValue; +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(particlePath.c_str())); + particlePath = utf8_encode(wstr.get()); +#endif + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_PROJECTILE]); + //printf("param = [%s]\n",(const char*)param); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string customPath = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string customPathValue = paramPartTokens[1]; + projectileParticlePath = customPathValue; +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(projectileParticlePath.c_str())); + projectileParticlePath = utf8_encode(wstr.get()); +#endif + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_SPLASH]); + //printf("param = [%s]\n",(const char*)param); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string customPath = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string customPathValue = paramPartTokens[1]; + splashParticlePath = customPathValue; +#ifdef WIN32 + auto_ptr wstr(Ansi2WideString(splashParticlePath.c_str())); + splashParticlePath = utf8_encode(wstr.get()); +#endif + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + float newAnimValue = 0.0f; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_MODEL_ANIMATION_VALUE]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + newAnimValue = strToFloat(paramPartTokens[1]); + printf("newAnimValue = %f\n", newAnimValue); + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + int newParticleLoopValue = 1; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_LOAD_PARTICLE_LOOP_VALUE]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + newParticleLoopValue = strToInt(paramPartTokens[1]); + //printf("newParticleLoopValue = %d\n",newParticleLoopValue); + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + float newZoomValue = 1.0f; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ZOOM_VALUE]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + newZoomValue = strToFloat(paramPartTokens[1]); + //printf("newAnimValue = %f\n",newAnimValue); + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + float newXRotValue = 0.0f; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_X_VALUE]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = (const char*) WX2CHR(argv[foundParamIndIndex]); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + newXRotValue = strToFloat(paramPartTokens[1]); + //printf("newAnimValue = %f\n",newAnimValue); + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", (const char*) WX2CHR(argv[foundParamIndIndex]), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + float newYRotValue = 0.0f; + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_ROTATE_Y_VALUE]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = static_cast(WX2CHR(argv[foundParamIndIndex])); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + newYRotValue = strToFloat(paramPartTokens[1]); + //printf("newAnimValue = %f\n",newAnimValue); + } else { + printf("\nInvalid path specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (hasCommandArgument(argc, argv, (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT])) == true) { + const wxWX2MBbuf param = (const char *) wxConvCurrent->cWX2MB(GAME_ARGS[GAME_ARG_SCREENSHOT_FORMAT]); + + int foundParamIndIndex = -1; + hasCommandArgument(argc, argv, string((const char*) param) + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, (const char*) param, &foundParamIndIndex); + } + //printf("foundParamIndIndex = %d\n",foundParamIndIndex); + string value = static_cast(WX2CHR(argv[foundParamIndIndex])); + vector paramPartTokens; + Tokenize(value, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + fileFormat = paramPartTokens[1]; + } else { + printf("\nInvalid value specified on commandline [%s] value [%s]\n\n", static_cast(WX2CHR(argv[foundParamIndIndex])), (paramPartTokens.size() >= 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(WX2CHR(argv[0]), false); + return false; + } + } + + if (argc == 2 && argv[1][0] != '-') { + + //#if defined(__MINGW32__) +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(argv[1])); + modelPath = tmp_buf; + auto_ptr wstr(Ansi2WideString(modelPath.c_str())); + modelPath = utf8_encode(wstr.get()); +#else + modelPath = static_cast(WX2CHR(argv[1])); +#endif + + //#else + // modelPath = wxFNCONV(argv[1]); + //#endif + + } + + //#if defined(__MINGW32__) + // const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(argv[0])); + // appPath = tmp_buf; + //#else + // appPath = wxFNCONV(argv[0]); + //#endif + // printf("appPath [%s]\n",argv[0]); + + wxString exe_path = wxStandardPaths::Get().GetExecutablePath(); + //wxString path_separator = wxFileName::GetPathSeparator(); + //exe_path = exe_path.BeforeLast(path_separator[0]); + //exe_path += path_separator; + + //#if defined(__MINGW32__) +#ifdef WIN32 + const wxWX2MBbuf tmp_buf = wxConvCurrent->cWX2MB(wxFNCONV(exe_path)); + string appPath = tmp_buf; + + auto_ptr wstr(Ansi2WideString(appPath.c_str())); + appPath = utf8_encode(wstr.get()); +#else + string appPath(static_cast(WX2CHR(exe_path))); +#endif + + //#else + // appPath = wxFNCONV(exe_path); + //#endif + + // printf("#2 appPath [%s]\n",appPath.c_str()); + + mainWindow = new MainWindow(unitToLoad, + modelPath, + particlePath, + projectileParticlePath, + splashParticlePath, + newAnimValue, + newParticleLoopValue, + newZoomValue, + newXRotValue, + newYRotValue, + appPath); + if (autoScreenShotAndExit == true) { #if !defined(WIN32) - mainWindow->Iconize(true); + mainWindow->Iconize(true); #endif - } - mainWindow->Show(); - mainWindow->init(); - mainWindow->Update(); - mainWindow->setupTimer(); + } + mainWindow->Show(); + mainWindow->init(); + mainWindow->Update(); + mainWindow->setupTimer(); - return true; -} + return true; + } -int App::MainLoop(){ - try{ - return wxApp::MainLoop(); - } - catch(const exception &e){ - wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Exception"), wxOK | wxICON_ERROR).ShowModal(); - return 1; - } - return 0; -} + int App::MainLoop() { + try { + return wxApp::MainLoop(); + } catch (const exception &e) { + wxMessageDialog(NULL, ToUnicode(e.what()), ToUnicode("Exception"), wxOK | wxICON_ERROR).ShowModal(); + return 1; + } + return 0; + } -int App::OnExit(){ - SystemFlags::Close(); - SystemFlags::SHUTDOWN_PROGRAM_MODE=true; + int App::OnExit() { + SystemFlags::Close(); + SystemFlags::SHUTDOWN_PROGRAM_MODE = true; - return 0; -} + return 0; + } -}}//end namespace + } + }//end namespace IMPLEMENT_APP_CONSOLE(Shared::G3dViewer::App) diff --git a/source/g3d_viewer/main.h b/source/g3d_viewer/main.h index d1af96318..02ab8e126 100644 --- a/source/g3d_viewer/main.h +++ b/source/g3d_viewer/main.h @@ -13,8 +13,8 @@ #define _SHADER_G3DVIEWER_MAIN_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -32,203 +32,208 @@ using std::string; using namespace Glest::Game; -namespace Shared{ namespace G3dViewer{ +namespace Shared { + namespace G3dViewer { -class GlCanvas; + class GlCanvas; -// =============================== -// class MainWindow -// =============================== + // =============================== + // class MainWindow + // =============================== -class MainWindow: public wxFrame{ -private: - DECLARE_EVENT_TABLE() + class MainWindow : public wxFrame { + private: + DECLARE_EVENT_TABLE() -public: - static const string versionString; - static const string winHeader; + public: + static const string versionString; + static const string winHeader; - enum MenuId{ - miFileLoad, - miFileLoadParticleXML, - miFileLoadProjectileParticleXML, - miFileLoadSplashParticleXML, - miFileClearAll, - miFileToggleScreenshotTransparent, - miFileSaveScreenshot, - miModeWireframe, - miModeNormals, - miModeGrid, - miSpeedSlower, - miSpeedFaster, - miRestart, - miChangeBackgroundColor, - miColorRed, - miColorBlue, - miColorGreen, - miColorYellow, - miColorWhite, - miColorCyan, - miColorOrange, - miColorMagenta - }; + enum MenuId { + miFileLoad, + miFileLoadParticleXML, + miFileLoadProjectileParticleXML, + miFileLoadSplashParticleXML, + miFileClearAll, + miFileToggleScreenshotTransparent, + miFileSaveScreenshot, + miModeWireframe, + miModeNormals, + miModeGrid, + miSpeedSlower, + miSpeedFaster, + miRestart, + miChangeBackgroundColor, + miColorRed, + miColorBlue, + miColorGreen, + miColorYellow, + miColorWhite, + miColorCyan, + miColorOrange, + miColorMagenta + }; -private: - GlCanvas *glCanvas; - Renderer *renderer; + private: + GlCanvas *glCanvas; + Renderer *renderer; - wxTimer *timer; + wxTimer *timer; - wxMenuBar *menu; - wxMenu *menuFile; - wxMenu *menuMode; - wxMenu *menuSpeed; - wxMenu *menuCustomColor; - wxFileDialog *fileDialog; + wxMenuBar *menu; + wxMenu *menuFile; + wxMenu *menuMode; + wxMenu *menuSpeed; + wxMenu *menuCustomColor; + wxFileDialog *fileDialog; - wxColourDialog *colorPicker; + wxColourDialog *colorPicker; - Model *model; + Model *model; - std::pair > unitPath; - std::vector modelPathList; - std::vector particlePathList; - std::vector particleProjectilePathList; - std::vector particleSplashPathList; // as above + std::pair > unitPath; + std::vector modelPathList; + std::vector particlePathList; + std::vector particleProjectilePathList; + std::vector particleSplashPathList; // as above - bool resetAnimation; - float resetAnim; - int resetParticleLoopStart; + bool resetAnimation; + float resetAnim; + int resetParticleLoopStart; - float speed; - float anim; - float lastanim; - int particleLoopStart; - float rotX, rotY, zoom; - float backBrightness, gridBrightness, lightBrightness; - int lastX, lastY; - Renderer::PlayerColor playerColor; - bool initTextureManager; + float speed; + float anim; + float lastanim; + int particleLoopStart; + float rotX, rotY, zoom; + float backBrightness, gridBrightness, lightBrightness; + int lastX, lastY; + Renderer::PlayerColor playerColor; + bool initTextureManager; - std::vector unitParticleSystemTypes; - std::vector unitParticleSystems; + std::vector unitParticleSystemTypes; + std::vector unitParticleSystems; - std::vector projectileParticleSystemTypes; - std::vector projectileParticleSystems; - std::vector splashParticleSystemTypes; // as above - std::vector splashParticleSystems; - string statusbarText; + std::vector projectileParticleSystemTypes; + std::vector projectileParticleSystems; + std::vector splashParticleSystemTypes; // as above + std::vector splashParticleSystems; + string statusbarText; - bool isControlKeyPressed; + bool isControlKeyPressed; - string appPath; + string appPath; - bool startupSettingsInited; + bool startupSettingsInited; - void initGlCanvas(); - void loadUnit(string path, string skillName); - void loadModel(string path); - void loadParticle(string path); - void loadProjectileParticle(string path); - void loadSplashParticle(string path); + void initGlCanvas(); + void loadUnit(string path, string skillName); + void loadModel(string path); + void loadParticle(string path); + void loadProjectileParticle(string path); + void loadSplashParticle(string path); - void saveScreenshot(); + void saveScreenshot(); -public: - MainWindow( std::pair > unitToLoad, - const string modelPath,const string particlePath, - const string projectileParticlePath,const string splashParticlePath, - float defaultAnimation,int defaultParticleLoopStart, - float defaultZoom,float defaultXRot, float defaultYRot, + public: + MainWindow(std::pair > unitToLoad, + const string modelPath, const string particlePath, + const string projectileParticlePath, const string splashParticlePath, + float defaultAnimation, int defaultParticleLoopStart, + float defaultZoom, float defaultXRot, float defaultYRot, string appPath); - ~MainWindow(); - void init(); + ~MainWindow(); + void init(); - void Notify(); + void Notify(); - void onPaint(wxPaintEvent &event); - void onClose(wxCloseEvent &event); - void onMenuFileLoad(wxCommandEvent &event); - void onMenuFileLoadParticleXML(wxCommandEvent &event); - void onMenuFileLoadProjectileParticleXML(wxCommandEvent &event); - void onMenuFileLoadSplashParticleXML(wxCommandEvent &event); - void onMenuFileClearAll(wxCommandEvent &event); - void onMenumFileToggleScreenshotTransparent(wxCommandEvent &event); - void onMenuFileSaveScreenshot(wxCommandEvent &event); - void onMenuFileExit(wxCommandEvent &event); - void onMenuModeNormals(wxCommandEvent &event); - void onMenuModeWireframe(wxCommandEvent &event); - void onMenuModeGrid(wxCommandEvent &event); - void onMenuSpeedSlower(wxCommandEvent &event); - void onMenuSpeedFaster(wxCommandEvent &event); - void onMenuRestart(wxCommandEvent &event); - void OnChangeColor(wxCommandEvent &event); - void onMenuColorRed(wxCommandEvent &event); - void onMenuColorBlue(wxCommandEvent &event); - void onMenuColorGreen(wxCommandEvent &event); - void onMenuColorYellow(wxCommandEvent &event); - void onMenuColorWhite(wxCommandEvent &event); - void onMenuColorCyan(wxCommandEvent &event); - void onMenuColorOrange(wxCommandEvent &event); - void onMenuColorMagenta(wxCommandEvent &event); - void onMouseWheelDown(wxMouseEvent &event); - void onMouseWheelUp(wxMouseEvent &event); - void onMouseMove(wxMouseEvent &event); - void onTimer(wxTimerEvent &event); + void onPaint(wxPaintEvent &event); + void onClose(wxCloseEvent &event); + void onMenuFileLoad(wxCommandEvent &event); + void onMenuFileLoadParticleXML(wxCommandEvent &event); + void onMenuFileLoadProjectileParticleXML(wxCommandEvent &event); + void onMenuFileLoadSplashParticleXML(wxCommandEvent &event); + void onMenuFileClearAll(wxCommandEvent &event); + void onMenumFileToggleScreenshotTransparent(wxCommandEvent &event); + void onMenuFileSaveScreenshot(wxCommandEvent &event); + void onMenuFileExit(wxCommandEvent &event); + void onMenuModeNormals(wxCommandEvent &event); + void onMenuModeWireframe(wxCommandEvent &event); + void onMenuModeGrid(wxCommandEvent &event); + void onMenuSpeedSlower(wxCommandEvent &event); + void onMenuSpeedFaster(wxCommandEvent &event); + void onMenuRestart(wxCommandEvent &event); + void OnChangeColor(wxCommandEvent &event); + void onMenuColorRed(wxCommandEvent &event); + void onMenuColorBlue(wxCommandEvent &event); + void onMenuColorGreen(wxCommandEvent &event); + void onMenuColorYellow(wxCommandEvent &event); + void onMenuColorWhite(wxCommandEvent &event); + void onMenuColorCyan(wxCommandEvent &event); + void onMenuColorOrange(wxCommandEvent &event); + void onMenuColorMagenta(wxCommandEvent &event); + void onMouseWheelDown(wxMouseEvent &event); + void onMouseWheelUp(wxMouseEvent &event); + void onMouseMove(wxMouseEvent &event); + void onTimer(wxTimerEvent &event); - void onKeyDown(wxKeyEvent &e); + void onKeyDown(wxKeyEvent &e); - string getModelInfo(); + string getModelInfo(); - void setupTimer(); - void setupStartupSettings(); -}; + void setupTimer(); + void setupStartupSettings(); + }; -// ===================================================== -// class GlCanvas -// ===================================================== + // ===================================================== + // class GlCanvas + // ===================================================== -class GlCanvas: public wxGLCanvas { -private: - DECLARE_EVENT_TABLE() + class GlCanvas : public wxGLCanvas { + private: + DECLARE_EVENT_TABLE() -public: - GlCanvas(MainWindow *mainWindow, int *args); - ~GlCanvas(); + public: + GlCanvas(MainWindow *mainWindow, int *args); + ~GlCanvas(); - void onMouseWheel(wxMouseEvent &event); - void onMouseMove(wxMouseEvent &event); - void onPaint(wxPaintEvent &event); - void onKeyDown(wxKeyEvent &event); - void OnSize(wxSizeEvent&); - void setCurrentGLContext(); + void onMouseWheel(wxMouseEvent &event); + void onMouseMove(wxMouseEvent &event); + void onPaint(wxPaintEvent &event); + void onKeyDown(wxKeyEvent &event); + void OnSize(wxSizeEvent&); + void setCurrentGLContext(); - wxGLContext * getCtx() { return context; } -private: - MainWindow *mainWindow; - wxGLContext *context; -}; + wxGLContext * getCtx() { + return context; + } + private: + MainWindow *mainWindow; + wxGLContext *context; + }; -// =============================== -// class App -// =============================== + // =============================== + // class App + // =============================== -class App: public wxApp{ -private: - MainWindow *mainWindow; + class App : public wxApp { + private: + MainWindow *mainWindow; + + public: + App() : wxApp() { + mainWindow = NULL; + } + virtual ~App() { + } + virtual bool OnInit(); + virtual int MainLoop(); + virtual int OnExit(); + }; -public: - App() : wxApp() { - mainWindow = NULL; } - virtual ~App() {} - virtual bool OnInit(); - virtual int MainLoop(); - virtual int OnExit(); -}; - -}}//end namespace +}//end namespace DECLARE_APP(Shared::G3dViewer::App) diff --git a/source/g3d_viewer/renderer.cpp b/source/g3d_viewer/renderer.cpp index a8a09d44d..df1771e0f 100644 --- a/source/g3d_viewer/renderer.cpp +++ b/source/g3d_viewer/renderer.cpp @@ -22,534 +22,534 @@ using namespace Shared::Graphics::Gl; using namespace Glest::Game; using namespace Shared::Util; -namespace Shared{ namespace G3dViewer{ +namespace Shared { + namespace G3dViewer { -int Renderer::windowX= 100; -int Renderer::windowY= 100; -int Renderer::windowW= 640; -int Renderer::windowH= 480; + int Renderer::windowX = 100; + int Renderer::windowY = 100; + int Renderer::windowW = 640; + int Renderer::windowH = 480; -// =============================================== -// class MeshCallbackTeamColor -// =============================================== + // =============================================== + // class MeshCallbackTeamColor + // =============================================== -void MeshCallbackTeamColor::execute(const Mesh *mesh){ + void MeshCallbackTeamColor::execute(const Mesh *mesh) { - //team color - if(mesh->getCustomTexture() && teamTexture!=NULL){ - //texture 0 - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + //team color + if (mesh->getCustomTexture() && teamTexture != NULL) { + //texture 0 + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - //set color to interpolation - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); + //set color to interpolation + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - //texture 1 - glActiveTexture(GL_TEXTURE1); - glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); - glEnable(GL_TEXTURE_2D); + //texture 1 + glActiveTexture(GL_TEXTURE1); + glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); + glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(teamTexture)->getHandle()); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + glBindTexture(GL_TEXTURE_2D, static_cast(teamTexture)->getHandle()); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - glActiveTexture(GL_TEXTURE0); - } - else{ - glActiveTexture(GL_TEXTURE1); - glDisable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - } -} - -// =============================================== -// class Renderer -// =============================================== - -Renderer::Renderer() { - normals= false; - wireframe= false; - grid= true; - modelRenderer = NULL; - textureManager = NULL; - particleRenderer = NULL; - modelManager = NULL; - width=0; - height=0; - red = 0.3f; - green = 0.3f; - blue = 0.3f; - alpha = 1.0f; - - customTextureRed=NULL; - customTextureBlue=NULL; - customTextureGreen=NULL; - customTextureYellow=NULL; - customTextureWhite=NULL; - customTextureCyan=NULL; - customTextureOrange=NULL; - customTextureMagenta=NULL; - particleManager=NULL; -} - -Renderer::~Renderer() { - delete modelRenderer; - delete textureManager; - delete particleRenderer; - - //resources - delete particleManager; - delete modelManager; - - if(GraphicsInterface::getInstance().getFactory() != NULL) { - delete GraphicsInterface::getInstance().getFactory(); - GraphicsInterface::getInstance().setFactory(NULL); - } -} - -Renderer * Renderer::getInstance() { - static Renderer * renderer = new Renderer(); - return renderer; -} - -void Renderer::transform(float rotX, float rotY, float zoom) { - assertGl(); - - glMatrixMode(GL_MODELVIEW); - glRotatef(rotY, 1.0f, 0.0f, 0.0f); - glRotatef(rotX, 0.0f, 1.0f, 0.0f); - glScalef(zoom, zoom, zoom); - Vec4f pos(-8.0f, 5.0f, 10.0f, 0.0f); - glLightfv(GL_LIGHT0,GL_POSITION, pos.ptr()); - - assertGl(); -} - -void Renderer::checkGlCaps() { - - if(glActiveTexture == NULL) { - string message; - - message += "Your system supports OpenGL version \""; - message += getGlVersion() + string("\"\n"); - message += "ZetaGlest needs a version that supports\n"; - message += "glActiveTexture (OpenGL 1.3) or the ARB_multitexture extension."; - - throw megaglest_runtime_error(message.c_str()); - } - - //opengl 1.3 - //if(!isGlVersionSupported(1, 3, 0)) { - if(glewIsSupported("GL_VERSION_1_3") == false) { - string message; - - message += "Your system supports OpenGL version \""; - message += getGlVersion() + string("\"\n"); - message += "ZetaGlest needs at least version 1.3 to work\n"; - message += "You may solve this problem by installing your latest video card drivers"; - - throw megaglest_runtime_error(message.c_str()); - } - - //opengl 1.4 or extension - //if(isGlVersionSupported(1, 4, 0) == false) { - if(glewIsSupported("GL_VERSION_1_4") == false) { - checkExtension("GL_ARB_texture_env_crossbar", "ZetaGlest"); - } -} - -void Renderer::checkExtension(const string &extension, const string &msg) { - if(isGlExtensionSupported(extension.c_str()) == false) { - string str= "OpenGL extension not supported: " + extension + ", required for " + msg; - throw megaglest_runtime_error(str); - } -} - -Texture2D * Renderer::getNewTexture2D() { - Texture2D *newTexture = textureManager->newTexture2D(); - return newTexture; -} - -Model * Renderer::newModel(ResourceScope rs,const string &path,bool deletePixMapAfterLoad,std::map > > *loadedFileList, string *sourceLoader) { - return modelManager->newModel(path,deletePixMapAfterLoad,loadedFileList,sourceLoader); -} - -void Renderer::endModel(ResourceScope rs,Model *model) { - modelManager->endModel(model); -} - -void Renderer::init() { - assertGl(); - - GraphicsFactory *gf= GraphicsInterface::getInstance().getFactory(); - if(gf == NULL) { - gf= new GraphicsFactoryGl(); - GraphicsInterface::getInstance().setFactory(gf); - } - - Config &config = Config::getInstance(); - if(config.getBool("CheckGlCaps")){ - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - checkGlCaps(); - } - - if(glActiveTexture == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error: glActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!"); - throw megaglest_runtime_error(szBuf); - } - - modelRenderer= gf->newModelRenderer(); - textureManager= gf->newTextureManager(); - particleRenderer= gf->newParticleRenderer(); - - //resources - particleManager= gf->newParticleManager(); - - modelManager = gf->newModelManager(); - modelManager->setTextureManager(textureManager); - - //red tex - customTextureRed= textureManager->newTexture2D(); - customTextureRed->getPixmap()->init(1, 1, 3); - customTextureRed->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.f, 0.f)); - - //blue tex - customTextureBlue= textureManager->newTexture2D(); - customTextureBlue->getPixmap()->init(1, 1, 3); - customTextureBlue->getPixmap()->setPixel(0, 0, Vec3f(0.f, 0.f, 1.f)); - - //green tex - customTextureGreen= textureManager->newTexture2D(); - customTextureGreen->getPixmap()->init(1, 1, 3); - customTextureGreen->getPixmap()->setPixel(0, 0, Vec3f(0.f, 0.5f, 0.f)); - - //yellow tex - customTextureYellow= textureManager->newTexture2D(); - customTextureYellow->getPixmap()->init(1, 1, 3); - customTextureYellow->getPixmap()->setPixel(0, 0, Vec3f(1.f, 1.f, 0.f)); - - //white tex - customTextureWhite= textureManager->newTexture2D(); - customTextureWhite->getPixmap()->init(1, 1, 3); - customTextureWhite->getPixmap()->setPixel(0, 0, Vec3f(1.f, 1.f, 1.f)); - - //cyan tex - customTextureCyan= textureManager->newTexture2D(); - customTextureCyan->getPixmap()->init(1, 1, 3); - customTextureCyan->getPixmap()->setPixel(0, 0, Vec3f(0.f, 1.f, 0.8f)); - - //orange tex - customTextureOrange= textureManager->newTexture2D(); - customTextureOrange->getPixmap()->init(1, 1, 3); - customTextureOrange->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.5f, 0.f)); - - //magenta tex - customTextureMagenta= textureManager->newTexture2D(); - customTextureMagenta->getPixmap()->init(1, 1, 3); - customTextureMagenta->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.5f, 1.f)); - - glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 - //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - /* once the GL context is valid : */ - //GLint alpha_bits; - //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); - //printf("#1 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); - - glEnable(GL_TEXTURE_2D); - glFrontFace(GL_CW); - glEnable(GL_CULL_FACE); - glEnable(GL_DEPTH_TEST); - glEnable(GL_ALPHA_TEST); - //glAlphaFunc(GL_GREATER, 0.5f); - glAlphaFunc(GL_GREATER, 0.0f); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - - Vec4f diffuse= Vec4f(1.0f, 1.0f, 1.0f, 1.0f); - Vec4f ambient= Vec4f(0.3f, 0.3f, 0.3f, 1.0f); - Vec4f specular= Vec4f(0.1f, 0.1f, 0.1f, 1.0f); - - glLightfv(GL_LIGHT0,GL_AMBIENT, ambient.ptr()); - glLightfv(GL_LIGHT0,GL_DIFFUSE, diffuse.ptr()); - glLightfv(GL_LIGHT0,GL_SPECULAR, specular.ptr()); - - glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); - glEnable(GL_COLOR_MATERIAL); - - assertGl(); -} - -void Renderer::reset(int w, int h, PlayerColor playerColor) { - assertGl(); - - width=w; - height=h; - - //glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - /* once the GL context is valid : */ - //GLint alpha_bits; - //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); - //printf("#2 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); - - glViewport(0, 0, w, h); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(60.0f, static_cast(w)/h, 1.0f, 200.0f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glTranslatef(0, -1.5, -5); - - Texture2D *customTexture=NULL; - switch(playerColor) { - case pcRed: - customTexture= customTextureRed; - break; - case pcBlue: - customTexture= customTextureBlue; - break; - case pcGreen: - customTexture= customTextureGreen; - break; - case pcYellow: - customTexture= customTextureYellow; - break; - case pcWhite: - customTexture= customTextureWhite; - break; - case pcCyan: - customTexture= customTextureCyan; - break; - case pcOrange: - customTexture= customTextureOrange; - break; - case pcMagenta: - customTexture= customTextureMagenta; - break; - default: - assert(false); - break; - } - meshCallbackTeamColor.setTeamTexture(customTexture); - - if(wireframe) { - glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); - glDisable(GL_TEXTURE_2D); - glDisable(GL_LIGHTING); - glDisable(GL_LIGHT0); - } - else { - glEnable(GL_TEXTURE_2D); - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); - } - - assertGl(); -} - -void Renderer::renderGrid() { - if(grid) { - float i=0; - - assertGl(); - - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_TEXTURE_2D); - glDisable(GL_LIGHTING); - - glBegin(GL_LINES); - glColor3f(1.0f, 1.0f, 1.0f); // gridcolor constant - for(i=-10.0f; i<=10.0f; i+=1.0f) { - glVertex3f(i, 0.0f, 10.0f); - glVertex3f(i, 0.0f, -10.0f); + glActiveTexture(GL_TEXTURE0); + } else { + glActiveTexture(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + } } - for(i=-10.0f; i<=10.0f; i+=1.0f) { - glVertex3f(10.f, 0.0f, i); - glVertex3f(-10.f, 0.0f, i); + + // =============================================== + // class Renderer + // =============================================== + + Renderer::Renderer() { + normals = false; + wireframe = false; + grid = true; + modelRenderer = NULL; + textureManager = NULL; + particleRenderer = NULL; + modelManager = NULL; + width = 0; + height = 0; + red = 0.3f; + green = 0.3f; + blue = 0.3f; + alpha = 1.0f; + + customTextureRed = NULL; + customTextureBlue = NULL; + customTextureGreen = NULL; + customTextureYellow = NULL; + customTextureWhite = NULL; + customTextureCyan = NULL; + customTextureOrange = NULL; + customTextureMagenta = NULL; + particleManager = NULL; } - glEnd(); - glPopAttrib(); + Renderer::~Renderer() { + delete modelRenderer; + delete textureManager; + delete particleRenderer; - assertGl(); - } -} + //resources + delete particleManager; + delete modelManager; -void Renderer::toggleNormals() { - normals= normals? false: true; -} + if (GraphicsInterface::getInstance().getFactory() != NULL) { + delete GraphicsInterface::getInstance().getFactory(); + GraphicsInterface::getInstance().setFactory(NULL); + } + } -void Renderer::toggleWireframe() { - wireframe= wireframe? false: true; -} + Renderer * Renderer::getInstance() { + static Renderer * renderer = new Renderer(); + return renderer; + } -void Renderer::toggleGrid() { - grid= grid? false: true; -} + void Renderer::transform(float rotX, float rotY, float zoom) { + assertGl(); -void Renderer::renderTheModel(Model *model, float f) { - if(model != NULL){ - modelRenderer->begin(true, true, !wireframe, false, &meshCallbackTeamColor); - model->updateInterpolationData(f, true); - modelRenderer->render(model); + glMatrixMode(GL_MODELVIEW); + glRotatef(rotY, 1.0f, 0.0f, 0.0f); + glRotatef(rotX, 0.0f, 1.0f, 0.0f); + glScalef(zoom, zoom, zoom); + Vec4f pos(-8.0f, 5.0f, 10.0f, 0.0f); + glLightfv(GL_LIGHT0, GL_POSITION, pos.ptr()); - if(normals) { - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glColor3f(1.0f, 1.0f, 1.0f); //normalscolor constant - modelRenderer->renderNormalsOnly(model); + assertGl(); + } + + void Renderer::checkGlCaps() { + + if (glActiveTexture == NULL) { + string message; + + message += "Your system supports OpenGL version \""; + message += getGlVersion() + string("\"\n"); + message += "ZetaGlest needs a version that supports\n"; + message += "glActiveTexture (OpenGL 1.3) or the ARB_multitexture extension."; + + throw megaglest_runtime_error(message.c_str()); + } + + //opengl 1.3 + //if(!isGlVersionSupported(1, 3, 0)) { + if (glewIsSupported("GL_VERSION_1_3") == false) { + string message; + + message += "Your system supports OpenGL version \""; + message += getGlVersion() + string("\"\n"); + message += "ZetaGlest needs at least version 1.3 to work\n"; + message += "You may solve this problem by installing your latest video card drivers"; + + throw megaglest_runtime_error(message.c_str()); + } + + //opengl 1.4 or extension + //if(isGlVersionSupported(1, 4, 0) == false) { + if (glewIsSupported("GL_VERSION_1_4") == false) { + checkExtension("GL_ARB_texture_env_crossbar", "ZetaGlest"); + } + } + + void Renderer::checkExtension(const string &extension, const string &msg) { + if (isGlExtensionSupported(extension.c_str()) == false) { + string str = "OpenGL extension not supported: " + extension + ", required for " + msg; + throw megaglest_runtime_error(str); + } + } + + Texture2D * Renderer::getNewTexture2D() { + Texture2D *newTexture = textureManager->newTexture2D(); + return newTexture; + } + + Model * Renderer::newModel(ResourceScope rs, const string &path, bool deletePixMapAfterLoad, std::map > > *loadedFileList, string *sourceLoader) { + return modelManager->newModel(path, deletePixMapAfterLoad, loadedFileList, sourceLoader); + } + + void Renderer::endModel(ResourceScope rs, Model *model) { + modelManager->endModel(model); + } + + void Renderer::init() { + assertGl(); + + GraphicsFactory *gf = GraphicsInterface::getInstance().getFactory(); + if (gf == NULL) { + gf = new GraphicsFactoryGl(); + GraphicsInterface::getInstance().setFactory(gf); + } + + Config &config = Config::getInstance(); + if (config.getBool("CheckGlCaps")) { + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + checkGlCaps(); + } + + if (glActiveTexture == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error: glActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!"); + throw megaglest_runtime_error(szBuf); + } + + modelRenderer = gf->newModelRenderer(); + textureManager = gf->newTextureManager(); + particleRenderer = gf->newParticleRenderer(); + + //resources + particleManager = gf->newParticleManager(); + + modelManager = gf->newModelManager(); + modelManager->setTextureManager(textureManager); + + //red tex + customTextureRed = textureManager->newTexture2D(); + customTextureRed->getPixmap()->init(1, 1, 3); + customTextureRed->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.f, 0.f)); + + //blue tex + customTextureBlue = textureManager->newTexture2D(); + customTextureBlue->getPixmap()->init(1, 1, 3); + customTextureBlue->getPixmap()->setPixel(0, 0, Vec3f(0.f, 0.f, 1.f)); + + //green tex + customTextureGreen = textureManager->newTexture2D(); + customTextureGreen->getPixmap()->init(1, 1, 3); + customTextureGreen->getPixmap()->setPixel(0, 0, Vec3f(0.f, 0.5f, 0.f)); + + //yellow tex + customTextureYellow = textureManager->newTexture2D(); + customTextureYellow->getPixmap()->init(1, 1, 3); + customTextureYellow->getPixmap()->setPixel(0, 0, Vec3f(1.f, 1.f, 0.f)); + + //white tex + customTextureWhite = textureManager->newTexture2D(); + customTextureWhite->getPixmap()->init(1, 1, 3); + customTextureWhite->getPixmap()->setPixel(0, 0, Vec3f(1.f, 1.f, 1.f)); + + //cyan tex + customTextureCyan = textureManager->newTexture2D(); + customTextureCyan->getPixmap()->init(1, 1, 3); + customTextureCyan->getPixmap()->setPixel(0, 0, Vec3f(0.f, 1.f, 0.8f)); + + //orange tex + customTextureOrange = textureManager->newTexture2D(); + customTextureOrange->getPixmap()->init(1, 1, 3); + customTextureOrange->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.5f, 0.f)); + + //magenta tex + customTextureMagenta = textureManager->newTexture2D(); + customTextureMagenta->getPixmap()->init(1, 1, 3); + customTextureMagenta->getPixmap()->setPixel(0, 0, Vec3f(1.f, 0.5f, 1.f)); + + glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* once the GL context is valid : */ + //GLint alpha_bits; + //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); + //printf("#1 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); + + glEnable(GL_TEXTURE_2D); + glFrontFace(GL_CW); + glEnable(GL_CULL_FACE); + glEnable(GL_DEPTH_TEST); + glEnable(GL_ALPHA_TEST); + //glAlphaFunc(GL_GREATER, 0.5f); + glAlphaFunc(GL_GREATER, 0.0f); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + Vec4f diffuse = Vec4f(1.0f, 1.0f, 1.0f, 1.0f); + Vec4f ambient = Vec4f(0.3f, 0.3f, 0.3f, 1.0f); + Vec4f specular = Vec4f(0.1f, 0.1f, 0.1f, 1.0f); + + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient.ptr()); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse.ptr()); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular.ptr()); + + glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); + glEnable(GL_COLOR_MATERIAL); + + assertGl(); + } + + void Renderer::reset(int w, int h, PlayerColor playerColor) { + assertGl(); + + width = w; + height = h; + + //glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* once the GL context is valid : */ + //GLint alpha_bits; + //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); + //printf("#2 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); + + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0f, static_cast(w) / h, 1.0f, 200.0f); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0, -1.5, -5); + + Texture2D *customTexture = NULL; + switch (playerColor) { + case pcRed: + customTexture = customTextureRed; + break; + case pcBlue: + customTexture = customTextureBlue; + break; + case pcGreen: + customTexture = customTextureGreen; + break; + case pcYellow: + customTexture = customTextureYellow; + break; + case pcWhite: + customTexture = customTextureWhite; + break; + case pcCyan: + customTexture = customTextureCyan; + break; + case pcOrange: + customTexture = customTextureOrange; + break; + case pcMagenta: + customTexture = customTextureMagenta; + break; + default: + assert(false); + break; + } + meshCallbackTeamColor.setTeamTexture(customTexture); + + if (wireframe) { + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); + glDisable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + glDisable(GL_LIGHT0); + } else { + glEnable(GL_TEXTURE_2D); + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); + } + + assertGl(); + } + + void Renderer::renderGrid() { + if (grid) { + float i = 0; + + assertGl(); + + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_TEXTURE_2D); + glDisable(GL_LIGHTING); + + glBegin(GL_LINES); + glColor3f(1.0f, 1.0f, 1.0f); // gridcolor constant + for (i = -10.0f; i <= 10.0f; i += 1.0f) { + glVertex3f(i, 0.0f, 10.0f); + glVertex3f(i, 0.0f, -10.0f); + } + for (i = -10.0f; i <= 10.0f; i += 1.0f) { + glVertex3f(10.f, 0.0f, i); + glVertex3f(-10.f, 0.0f, i); + } + glEnd(); + + glPopAttrib(); + + assertGl(); + } + } + + void Renderer::toggleNormals() { + normals = normals ? false : true; + } + + void Renderer::toggleWireframe() { + wireframe = wireframe ? false : true; + } + + void Renderer::toggleGrid() { + grid = grid ? false : true; + } + + void Renderer::renderTheModel(Model *model, float f) { + if (model != NULL) { + modelRenderer->begin(true, true, !wireframe, false, &meshCallbackTeamColor); + model->updateInterpolationData(f, true); + modelRenderer->render(model); + + if (normals) { + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glColor3f(1.0f, 1.0f, 1.0f); //normalscolor constant + modelRenderer->renderNormalsOnly(model); + glPopAttrib(); + } + + modelRenderer->end(); + } + } + + void Renderer::manageParticleSystem(ParticleSystem *particleSystem) { + particleManager->manage(particleSystem); + } + + void Renderer::updateParticleManager() { + particleManager->update(); + } + + bool Renderer::hasActiveParticleSystem(ParticleSystem::ParticleSystemType type) const { + return particleManager->hasActiveParticleSystem(type); + } + + void Renderer::renderParticleManager() { + glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glDepthFunc(GL_LESS); + particleRenderer->renderManager(particleManager, modelRenderer); glPopAttrib(); } - modelRenderer->end(); + Texture2D * Renderer::getPlayerColorTexture(PlayerColor playerColor) { + Texture2D *customTexture = NULL; + switch (playerColor) { + case pcRed: + customTexture = customTextureRed; + break; + case pcBlue: + customTexture = customTextureBlue; + break; + case pcGreen: + customTexture = customTextureGreen; + break; + case pcYellow: + customTexture = customTextureYellow; + break; + case pcWhite: + customTexture = customTextureWhite; + break; + case pcCyan: + customTexture = customTextureCyan; + break; + case pcOrange: + customTexture = customTextureOrange; + break; + case pcMagenta: + customTexture = customTextureMagenta; + break; + default: + throw megaglest_runtime_error("Unknown playercolor: " + intToStr(playerColor)); + break; + } + + return customTexture; + } + + void Renderer::initTextureManager() { + textureManager->init(); + } + + void Renderer::initModelManager() { + modelManager->init(); + } + + void Renderer::end() { + //delete resources + //textureManager->end(); + particleManager->end(); + modelManager->end(); + } + + void Renderer::setBackgroundColor(float red, float green, float blue) { + this->red = red; + this->green = green; + this->blue = blue; + + glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* once the GL context is valid : */ + //GLint alpha_bits; + //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); + //printf("#3 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); + } + + void Renderer::setAlphaColor(float alpha) { + this->alpha = alpha; + + glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 + //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + /* once the GL context is valid : */ + //GLint alpha_bits; + //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); + //printf("#3.1 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); + } + + void Renderer::saveScreen(const string &path, std::pair *overrideSize) { + Pixmap2D *pixmapScreenShot = new Pixmap2D(width, height, 4); + //glFinish(); + + glPixelStorei(GL_PACK_ALIGNMENT, 1); + + glReadPixels(0, 0, pixmapScreenShot->getW(), pixmapScreenShot->getH(), + GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + //if(overrideSize != NULL && overrideSize->first > 0 && overrideSize->second > 0) { + // pixmapScreenShot->Scale(GL_RGBA,overrideSize->first,overrideSize->second); + //} + + pixmapScreenShot->save(path); + delete pixmapScreenShot; + } + } -} - -void Renderer::manageParticleSystem(ParticleSystem *particleSystem) { - particleManager->manage(particleSystem); -} - -void Renderer::updateParticleManager() { - particleManager->update(); -} - -bool Renderer::hasActiveParticleSystem(ParticleSystem::ParticleSystemType type) const { - return particleManager->hasActiveParticleSystem(type); -} - -void Renderer::renderParticleManager() { - glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - glDepthFunc(GL_LESS); - particleRenderer->renderManager(particleManager, modelRenderer); - glPopAttrib(); -} - -Texture2D * Renderer::getPlayerColorTexture(PlayerColor playerColor) { - Texture2D *customTexture=NULL; - switch(playerColor){ - case pcRed: - customTexture= customTextureRed; - break; - case pcBlue: - customTexture= customTextureBlue; - break; - case pcGreen: - customTexture= customTextureGreen; - break; - case pcYellow: - customTexture= customTextureYellow; - break; - case pcWhite: - customTexture= customTextureWhite; - break; - case pcCyan: - customTexture= customTextureCyan; - break; - case pcOrange: - customTexture= customTextureOrange; - break; - case pcMagenta: - customTexture= customTextureMagenta; - break; - default: - throw megaglest_runtime_error("Unknown playercolor: " + intToStr(playerColor)); - break; - } - - return customTexture; -} - -void Renderer::initTextureManager() { - textureManager->init(); -} - -void Renderer::initModelManager() { - modelManager->init(); -} - -void Renderer::end() { - //delete resources - //textureManager->end(); - particleManager->end(); - modelManager->end(); -} - -void Renderer::setBackgroundColor(float red, float green, float blue) { - this->red = red; - this->green = green; - this->blue = blue; - - glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 - //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - /* once the GL context is valid : */ - //GLint alpha_bits; - //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); - //printf("#3 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); -} - -void Renderer::setAlphaColor(float alpha) { - this->alpha= alpha; - - glClearColor(red, green, blue, alpha); //backgroundcolor constant 0.3 - //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - /* once the GL context is valid : */ - //GLint alpha_bits; - //glGetIntegerv(GL_ALPHA_BITS, &alpha_bits); - //printf("#3.1 The framebuffer uses %d bit(s) per the alpha component\n", alpha_bits); -} - -void Renderer::saveScreen(const string &path,std::pair *overrideSize) { - Pixmap2D *pixmapScreenShot = new Pixmap2D(width, height, 4); - //glFinish(); - - glPixelStorei(GL_PACK_ALIGNMENT, 1); - - glReadPixels(0, 0, pixmapScreenShot->getW(), pixmapScreenShot->getH(), - GL_RGBA, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - //if(overrideSize != NULL && overrideSize->first > 0 && overrideSize->second > 0) { - // pixmapScreenShot->Scale(GL_RGBA,overrideSize->first,overrideSize->second); - //} - - pixmapScreenShot->save(path); - delete pixmapScreenShot; -} - -}}//end namespace +}//end namespace diff --git a/source/g3d_viewer/renderer.h b/source/g3d_viewer/renderer.h index f9a881461..93a60d419 100644 --- a/source/g3d_viewer/renderer.h +++ b/source/g3d_viewer/renderer.h @@ -13,8 +13,8 @@ #define _SHADER_G3DVIEWER_RENDERER_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include "model_renderer.h" @@ -45,122 +45,134 @@ using Shared::Graphics::Texture; using namespace Shared::Graphics; -namespace Shared{ namespace G3dViewer{ +namespace Shared { + namespace G3dViewer { -// =============================================== -// class MeshCallbackTeamColor -// =============================================== + // =============================================== + // class MeshCallbackTeamColor + // =============================================== -class MeshCallbackTeamColor: public MeshCallback{ -private: - const Texture *teamTexture; + class MeshCallbackTeamColor : public MeshCallback { + private: + const Texture *teamTexture; + + public: + MeshCallbackTeamColor() : MeshCallback() { + teamTexture = NULL; + } + void setTeamTexture(const Texture *teamTexture) { + this->teamTexture = teamTexture; + } + virtual void execute(const Mesh *mesh); + }; + + // =============================== + // class Renderer + // =============================== + + class Renderer : public RendererInterface { + public: + static int windowX; + static int windowY; + static int windowW; + static int windowH; + + public: + enum PlayerColor { + pcRed, + pcBlue, + pcGreen, + pcYellow, + pcWhite, + pcCyan, + pcOrange, + pcMagenta + }; + + private: + bool wireframe; + bool normals; + bool grid; + + int width; + int height; + + ModelRenderer *modelRenderer; + TextureManager *textureManager; + ParticleRenderer *particleRenderer; + + ParticleManager *particleManager; + ModelManager *modelManager; + + Texture2D *customTextureRed; + Texture2D *customTextureBlue; + Texture2D *customTextureGreen; + Texture2D *customTextureYellow; + Texture2D *customTextureWhite; + Texture2D *customTextureCyan; + Texture2D *customTextureOrange; + Texture2D *customTextureMagenta; + MeshCallbackTeamColor meshCallbackTeamColor; + + float red; + float green; + float blue; + float alpha; + + Renderer(); + void checkGlCaps(); + void checkExtension(const string &extension, const string &msg); + + public: + virtual ~Renderer(); + static Renderer *getInstance(); + + void init(); + void reset(int w, int h, PlayerColor playerColor); + void transform(float rotX, float rotY, float zoom); + void renderGrid(); + + bool getNormals() const { + return normals; + } + bool getWireframe() const { + return wireframe; + } + bool getGrid() const { + return grid; + } + + void toggleNormals(); + void toggleWireframe(); + void toggleGrid(); + + void renderTheModel(Model *model, float f); + + void manageParticleSystem(ParticleSystem *particleSystem); + void updateParticleManager(); + void renderParticleManager(); + Texture2D *getPlayerColorTexture(PlayerColor playerColor); + + Texture2D * getNewTexture2D(); + + Model *newModel(ResourceScope rs, const string &path, bool deletePixMapAfterLoad = false, std::map > > *loadedFileList = NULL, string *sourceLoader = NULL); + void endModel(ResourceScope rs, Model *model); + Texture2D *newTexture2D(ResourceScope rs) { + return getNewTexture2D(); + } + + void initTextureManager(); + void initModelManager(); + + void end(); + + void setBackgroundColor(float red, float green, float blue); + void setAlphaColor(float alpha); + void saveScreen(const string &path, std::pair *overrideSize); + bool hasActiveParticleSystem(ParticleSystem::ParticleSystemType typeName) const; + }; -public: - MeshCallbackTeamColor() : MeshCallback() { - teamTexture = NULL; } - void setTeamTexture(const Texture *teamTexture) {this->teamTexture= teamTexture;} - virtual void execute(const Mesh *mesh); -}; - -// =============================== -// class Renderer -// =============================== - -class Renderer : public RendererInterface { -public: - static int windowX; - static int windowY; - static int windowW; - static int windowH; - -public: - enum PlayerColor{ - pcRed, - pcBlue, - pcGreen, - pcYellow, - pcWhite, - pcCyan, - pcOrange, - pcMagenta - }; - -private: - bool wireframe; - bool normals; - bool grid; - - int width; - int height; - - ModelRenderer *modelRenderer; - TextureManager *textureManager; - ParticleRenderer *particleRenderer; - - ParticleManager *particleManager; - ModelManager *modelManager; - - Texture2D *customTextureRed; - Texture2D *customTextureBlue; - Texture2D *customTextureGreen; - Texture2D *customTextureYellow; - Texture2D *customTextureWhite; - Texture2D *customTextureCyan; - Texture2D *customTextureOrange; - Texture2D *customTextureMagenta; - MeshCallbackTeamColor meshCallbackTeamColor; - - float red; - float green; - float blue; - float alpha; - - Renderer(); - void checkGlCaps(); - void checkExtension(const string &extension, const string &msg); - -public: - virtual ~Renderer(); - static Renderer *getInstance(); - - void init(); - void reset(int w, int h, PlayerColor playerColor); - void transform(float rotX, float rotY, float zoom); - void renderGrid(); - - bool getNormals() const {return normals;} - bool getWireframe() const {return wireframe;} - bool getGrid() const {return grid;} - - void toggleNormals(); - void toggleWireframe(); - void toggleGrid(); - - void renderTheModel(Model *model, float f); - - void manageParticleSystem(ParticleSystem *particleSystem); - void updateParticleManager(); - void renderParticleManager(); - Texture2D *getPlayerColorTexture(PlayerColor playerColor); - - Texture2D * getNewTexture2D(); - - Model *newModel(ResourceScope rs,const string &path,bool deletePixMapAfterLoad=false,std::map > > *loadedFileList=NULL, string *sourceLoader=NULL); - void endModel(ResourceScope rs,Model *model); - Texture2D *newTexture2D(ResourceScope rs) { return getNewTexture2D(); } - - void initTextureManager(); - void initModelManager(); - - void end(); - - void setBackgroundColor(float red, float green, float blue); - void setAlphaColor(float alpha); - void saveScreen(const string &path,std::pair *overrideSize); - bool hasActiveParticleSystem(ParticleSystem::ParticleSystemType typeName) const; -}; - -}}//end namespace +}//end namespace #endif diff --git a/source/glest_game/CMakeLists.txt b/source/glest_game/CMakeLists.txt index 3ef9f0a10..baa40b1d4 100644 --- a/source/glest_game/CMakeLists.txt +++ b/source/glest_game/CMakeLists.txt @@ -5,7 +5,7 @@ SET(EXTERNAL_LIBS "") SET(TARGET_NAME "zetaglest") SET(TARGET_NAME_MANPAGE "${TARGET_NAME}.6") -IF(BUILD_MEGAGLEST) +IF(BUILD_ZETAGLEST) MESSAGE(STATUS "Will try to build ZetaGlest game") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") @@ -112,8 +112,8 @@ IF(BUILD_MEGAGLEST) SET(EXTERNAL_LIBS ${EXTERNAL_LIBS} ${GLEW_LIBRARIES}) ENDIF() - IF(VLC_MIN_VERSION_MG) - SET(LIBVLC_MIN_VERSION "${VLC_MIN_VERSION_MG}") + IF(VLC_MIN_VERSION_ZG) + SET(LIBVLC_MIN_VERSION "${VLC_MIN_VERSION_ZG}") ELSE() SET(LIBVLC_MIN_VERSION "1.1.0") ENDIF() @@ -233,26 +233,26 @@ IF(BUILD_MEGAGLEST) SET(GLEST_LIB_INCLUDE_DIRS ${GLEST_LIB_INCLUDE_DIRS} ${GLEST_LIB_INCLUDE_ROOT}platform/unix) ENDIF() - SET(MG_SOURCE_FILES "") + SET(ZG_SOURCE_FILES "") FOREACH(DIR IN LISTS DIRS_WITH_SRC) INCLUDE_DIRECTORIES( ${DIR} ) set(SRC_DIR_TO_GLOB ${DIR}) FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.cpp) - SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) + SET(ZG_SOURCE_FILES ${ZG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.c) - SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) + SET(ZG_SOURCE_FILES ${ZG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) FILE(GLOB INCLUDE_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.h) - SET(MG_INCLUDE_FILES ${MG_INCLUDE_FILES} ${INCLUDE_FILES_FROM_THIS_DIR}) + SET(ZG_INCLUDE_FILES ${ZG_INCLUDE_FILES} ${INCLUDE_FILES_FROM_THIS_DIR}) IF(APPLE) FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.mm) #MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}") - SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) + SET(ZG_SOURCE_FILES ${ZG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) FILE(GLOB SRC_FILES_FROM_THIS_DIR ${SRC_DIR_TO_GLOB}/*.m) #MESSAGE(STATUS "SRC_FILES_FROM_THIS_DIR: ${SRC_FILES_FROM_THIS_DIR}") - SET(MG_SOURCE_FILES ${MG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) + SET(ZG_SOURCE_FILES ${ZG_SOURCE_FILES} ${SRC_FILES_FROM_THIS_DIR}) ENDIF(APPLE) ENDFOREACH(DIR) @@ -261,18 +261,18 @@ IF(BUILD_MEGAGLEST) ENDIF() IF(WANT_DEV_OUTPATH) - SET(EXECUTABLE_OUTPUT_PATH "${MEGAGLEST_FRIENDLY_OUTPUT_PATH}") + SET(EXECUTABLE_OUTPUT_PATH "${ZETAGLEST_FRIENDLY_OUTPUT_PATH}") ENDIF() MESSAGE(STATUS "***Note: The final output path for the ZetaGlest engine will be [${EXECUTABLE_OUTPUT_PATH}]") - SET_SOURCE_FILES_PROPERTIES(${MG_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1) + SET_SOURCE_FILES_PROPERTIES(${ZG_INCLUDE_FILES} PROPERTIES HEADER_FILE_ONLY 1) - ADD_EXECUTABLE(${TARGET_NAME} ${MG_SOURCE_FILES} ${MG_INCLUDE_FILES}) + ADD_EXECUTABLE(${TARGET_NAME} ${ZG_SOURCE_FILES} ${ZG_INCLUDE_FILES}) - SET(MG_STREFLOP "") + SET(ZG_STREFLOP "") IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND) - SET(MG_STREFLOP "streflop") + SET(ZG_STREFLOP "streflop") ENDIF() if(WANT_USE_GoogleBreakpad) @@ -289,12 +289,12 @@ IF(BUILD_MEGAGLEST) endif() IF(WIN32) - TARGET_LINK_LIBRARIES(${TARGET_NAME} stdc++ gcc odbc32 wsock32 winspool winmm shell32 comctl32 ctl3d32 advapi32 wsock32 opengl32 glu32 ole32 oleaut32 uuid mingw32 ddraw dsound dxguid ws2_32 iphlpapi wsock32 libogg libvorbis libvorbisfile zlib jpeg libpng xerces-c2_8_0 OpenAL32 libcurl winmm gdi32 opengl32 glu32 ${SDL_VERSION_NAME} ${SDL_VERSION_NAME}main lua5.1 ${MG_STREFLOP} libmegaglest stdc++ moldname mingwex msvcrt user32 kernel32) + TARGET_LINK_LIBRARIES(${TARGET_NAME} stdc++ gcc odbc32 wsock32 winspool winmm shell32 comctl32 ctl3d32 advapi32 wsock32 opengl32 glu32 ole32 oleaut32 uuid mingw32 ddraw dsound dxguid ws2_32 iphlpapi wsock32 libogg libvorbis libvorbisfile zlib jpeg libpng xerces-c2_8_0 OpenAL32 libcurl winmm gdi32 opengl32 glu32 ${SDL_VERSION_NAME} ${SDL_VERSION_NAME}main lua5.1 ${ZG_STREFLOP} libmegaglest stdc++ moldname mingwex msvcrt user32 kernel32) ENDIF() IF(NOT WIN32) IF(WANT_USE_STREFLOP AND NOT STREFLOP_FOUND) - TARGET_LINK_LIBRARIES(${TARGET_NAME} ${MG_STREFLOP}) + TARGET_LINK_LIBRARIES(${TARGET_NAME} ${ZG_STREFLOP}) ENDIF() TARGET_LINK_LIBRARIES(${TARGET_NAME} libmegaglest) ENDIF() @@ -307,17 +307,17 @@ IF(BUILD_MEGAGLEST) IF(EXISTS ${HELP2MAN}) MESSAGE(STATUS "***-- Found help2man: ${HELP2MAN} about to build manpage in [${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE}]") - SET(MG_MANPAGE_DESCRIPTION "3D multi-player real time strategy game") + SET(ZG_MANPAGE_DESCRIPTION "3D multi-player real time strategy game") IF(EXISTS ${XVFB_EXEC}) MESSAGE(STATUS "***-- Found xvfb-run: ${XVFB_EXEC} about to build manpage with it.") add_custom_target(${TARGET_NAME_MANPAGE} ALL - COMMAND ${XVFB_EXEC} --auto-servernum --server-num=770 ${HELP2MAN} --name="${MG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME} + COMMAND ${XVFB_EXEC} --auto-servernum --server-num=770 ${HELP2MAN} --name="${ZG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME} DEPENDS ${TARGET_NAME}) ELSE() add_custom_target(${TARGET_NAME_MANPAGE} ALL - COMMAND ${HELP2MAN} --name="${MG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME} + COMMAND ${HELP2MAN} --name="${ZG_MANPAGE_DESCRIPTION}" --section=6 -N -o ${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE} ${HELP2MAN_OUT_PATH}${TARGET_NAME} DEPENDS ${TARGET_NAME}) ENDIF() ENDIF() @@ -329,57 +329,57 @@ IF(BUILD_MEGAGLEST) # Installation of the program INSTALL(TARGETS ${TARGET_NAME} - DESTINATION ${MEGAGLEST_BIN_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_BIN_INSTALL_PATH}) - IF(EXISTS ${HELP2MAN} AND NOT "${MEGAGLEST_MANPAGE_INSTALL_PATH}" STREQUAL "") - MESSAGE(STATUS "**NOTE: Will Install manpage [${TARGET_NAME_MANPAGE}] to [${MEGAGLEST_MANPAGE_INSTALL_PATH}]") + IF(EXISTS ${HELP2MAN} AND NOT "${ZETAGLEST_MANPAGE_INSTALL_PATH}" STREQUAL "") + MESSAGE(STATUS "**NOTE: Will Install manpage [${TARGET_NAME_MANPAGE}] to [${ZETAGLEST_MANPAGE_INSTALL_PATH}]") # Installation of the program manpage file INSTALL(FILES "${HELP2MAN_OUT_PATH}${TARGET_NAME_MANPAGE}" - DESTINATION ${MEGAGLEST_MANPAGE_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_MANPAGE_INSTALL_PATH}) ENDIF() IF(UNIX) INSTALL(PROGRAMS "${PROJECT_SOURCE_DIR}/mk/linux/start_zetaglest_gameserver" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) ENDIF() INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/shared/glestkeys.ini" "${PROJECT_SOURCE_DIR}/mk/shared/servers.ini" - DESTINATION ${MEGAGLEST_DATA_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_DATA_INSTALL_PATH}) IF(WIN32) INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/windows/glest.ini" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) ELSEIF(UNIX AND NOT APPLE) INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/linux/glest.ini" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) ELSEIF(UNIX AND APPLE) INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/macos/glest.ini" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) IF(WANT_SINGLE_INSTALL_DIRECTORY AND EXISTS "${PROJECT_SOURCE_DIR}/mk/macos/p7zip/") INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/mk/macos/p7zip" - DESTINATION ${MEGAGLEST_DATA_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_DATA_INSTALL_PATH}) ENDIF() IF(WANT_SINGLE_INSTALL_DIRECTORY AND EXISTS "${PROJECT_SOURCE_DIR}/mk/macos/lib/") INSTALL(DIRECTORY "${PROJECT_SOURCE_DIR}/mk/macos/lib/" - DESTINATION "${MEGAGLEST_DATA_INSTALL_PATH}/../../Frameworks/") + DESTINATION "${ZETAGLEST_DATA_INSTALL_PATH}/../../Frameworks/") ENDIF() ENDIF() INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/shared/zetaglest.ico" - DESTINATION ${MEGAGLEST_INI_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_INI_INSTALL_PATH}) IF(UNIX AND APPLE AND NOT WANT_SINGLE_INSTALL_DIRECTORY) INSTALL(FILES "${PROJECT_SOURCE_DIR}/mk/macos/bundle_resources/MegaGlest.icns" - DESTINATION ${MEGAGLEST_ICON_INSTALL_PATH}) + DESTINATION ${ZETAGLEST_ICON_INSTALL_PATH}) ENDIF() ENDIF() ENDIF() diff --git a/source/glest_game/ai/ai.cpp b/source/glest_game/ai/ai.cpp index 207dd37f3..bcacc82a3 100644 --- a/source/glest_game/ai/ai.cpp +++ b/source/glest_game/ai/ai.cpp @@ -19,2148 +19,1896 @@ #include "leak_dumper.h" using namespace - Shared::Graphics; +Shared::Graphics; using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - - Task::Task () - { - taskClass = tcProduce; - } - -//void Task::saveGame(XmlNode *rootNode) const { -// std::map mapTagReplacements; -// XmlNode *taskNode = rootNode->addChild("Task"); -//} - -// ===================================================== -// class ProduceTask -// ===================================================== - ProduceTask::ProduceTask (): - Task () - { - taskClass = tcProduce; - unitType = NULL; - resourceType = NULL; - unitClass = ucWarrior; - } - - ProduceTask::ProduceTask (UnitClass unitClass): - Task () - { - taskClass = tcProduce; - this->unitClass = unitClass; - unitType = NULL; - resourceType = NULL; - } - - ProduceTask::ProduceTask (const UnitType * unitType): - Task () - { - taskClass = tcProduce; - this->unitType = unitType; - resourceType = NULL; - unitClass = ucWarrior; - } - - ProduceTask::ProduceTask (const ResourceType * resourceType): - Task () - { - taskClass = tcProduce; - unitType = NULL; - unitClass = ucWarrior; - this->resourceType = resourceType; - } - - string - ProduceTask::toString () const - { - string - str = "Produce "; - if (unitType != NULL) - { - str += unitType->getName (false); - } - return - str; - } - - void - ProduceTask::saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - taskNode = rootNode->addChild ("Task"); - taskNode-> - addAttribute ("taskClass", intToStr (taskClass), mapTagReplacements); - XmlNode * - produceTaskNode = taskNode->addChild ("ProduceTask"); - -// UnitClass unitClass; - produceTaskNode-> - addAttribute ("unitClass", intToStr (unitClass), mapTagReplacements); -// const UnitType *unitType; - if (unitType != NULL) - { - produceTaskNode->addAttribute ("unitType", - unitType->getName (false), - mapTagReplacements); - } -// const ResourceType *resourceType; - if (resourceType != NULL) - { - produceTaskNode->addAttribute ("resourceType", - resourceType->getName (false), - mapTagReplacements); - } - } - - ProduceTask * - ProduceTask::loadGame (const XmlNode * rootNode, Faction * faction) - { - const XmlNode * - produceTaskNode = rootNode->getChild ("ProduceTask"); - - ProduceTask * - newTask = new ProduceTask (); - // UnitClass unitClass; - newTask->unitClass = - static_cast < UnitClass > - (produceTaskNode->getAttribute ("unitClass")->getIntValue ()); - // const UnitType *unitType; - if (produceTaskNode->hasAttribute ("unitType")) - { - string - unitTypeName = - produceTaskNode->getAttribute ("unitType")->getValue (); - newTask->unitType = faction->getType ()->getUnitType (unitTypeName); - } - // const ResourceType *resourceType; - if (produceTaskNode->hasAttribute ("resourceType")) - { - string - resourceTypeName = - produceTaskNode->getAttribute ("resourceType")->getValue (); - newTask->resourceType = - faction->getTechTree ()->getResourceType (resourceTypeName); - } - - return newTask; - } - -// ===================================================== -// class BuildTask -// ===================================================== - BuildTask::BuildTask () - { - taskClass = tcBuild; - this->unitType = NULL; - resourceType = NULL; - forcePos = false; - } - - BuildTask::BuildTask (const UnitType * unitType) - { - taskClass = tcBuild; - this->unitType = unitType; - resourceType = NULL; - forcePos = false; - } - - BuildTask::BuildTask (const ResourceType * resourceType) - { - taskClass = tcBuild; - unitType = NULL; - this->resourceType = resourceType; - forcePos = false; - } - - BuildTask::BuildTask (const UnitType * unitType, const Vec2i & pos) - { - taskClass = tcBuild; - this->unitType = unitType; - resourceType = NULL; - forcePos = true; - this->pos = pos; - } - - string - BuildTask::toString () const - { - string - str = "Build "; - if (unitType != NULL) - { - str += unitType->getName (false); - } - return - str; - } - - void - BuildTask::saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - taskNode = rootNode->addChild ("Task"); - taskNode-> - addAttribute ("taskClass", intToStr (taskClass), mapTagReplacements); - XmlNode * - buildTaskNode = taskNode->addChild ("BuildTask"); - -// const UnitType *unitType; - if (unitType != NULL) - { - buildTaskNode->addAttribute ("unitType", unitType->getName (false), - mapTagReplacements); - } -// const ResourceType *resourceType; - if (resourceType != NULL) - { - buildTaskNode->addAttribute ("resourceType", - resourceType->getName (), - mapTagReplacements); - } -// bool forcePos; - buildTaskNode->addAttribute ("forcePos", intToStr (forcePos), - mapTagReplacements); -// Vec2i pos; - buildTaskNode->addAttribute ("pos", pos.getString (), - mapTagReplacements); - } - - BuildTask * - BuildTask::loadGame (const XmlNode * rootNode, Faction * faction) - { - const XmlNode * - buildTaskNode = rootNode->getChild ("BuildTask"); - - BuildTask * - newTask = new BuildTask (); - if (buildTaskNode->hasAttribute ("unitType")) - { - string - unitTypeName = - buildTaskNode->getAttribute ("unitType")->getValue (); - newTask->unitType = faction->getType ()->getUnitType (unitTypeName); - } - if (buildTaskNode->hasAttribute ("resourceType")) - { - string - resourceTypeName = - buildTaskNode->getAttribute ("resourceType")->getValue (); - newTask->resourceType = - faction->getTechTree ()->getResourceType (resourceTypeName); - } - - newTask->forcePos = - buildTaskNode->getAttribute ("forcePos")->getIntValue () != 0; - newTask->pos = - Vec2i::strToVec2 (buildTaskNode->getAttribute ("pos")->getValue ()); - - return newTask; - } - -// ===================================================== -// class UpgradeTask -// ===================================================== - UpgradeTask::UpgradeTask () - { - taskClass = tcUpgrade; - this->upgradeType = NULL; - } - - UpgradeTask::UpgradeTask (const UpgradeType * upgradeType) - { - taskClass = tcUpgrade; - this->upgradeType = upgradeType; - } - - string - UpgradeTask::toString () const - { - string - str = "Build "; - if (upgradeType != NULL) - { - str += upgradeType->getName (); - } - return - str; - } - - void - UpgradeTask::saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - taskNode = rootNode->addChild ("Task"); - taskNode-> - addAttribute ("taskClass", intToStr (taskClass), mapTagReplacements); - XmlNode * - upgradeTaskNode = taskNode->addChild ("UpgradeTask"); - - if (upgradeType != NULL) - { - //upgradeType->saveGame(upgradeTaskNode); - upgradeTaskNode->addAttribute ("upgradeType", - upgradeType->getName (), - mapTagReplacements); - } - } - - UpgradeTask * - UpgradeTask::loadGame (const XmlNode * rootNode, Faction * faction) - { - const XmlNode * - upgradeTaskNode = rootNode->getChild ("UpgradeTask"); - - UpgradeTask * - newTask = new UpgradeTask (); - if (upgradeTaskNode->hasAttribute ("upgradeType")) - { - string - upgradeTypeName = - upgradeTaskNode->getAttribute ("upgradeType")->getValue (); - newTask->upgradeType = - faction->getType ()->getUpgradeType (upgradeTypeName); - } - return newTask; - } - -// ===================================================== -// class Ai -// ===================================================== - - void - Ai::init (AiInterface * aiInterface, int useStartLocation) - { - this->aiInterface = aiInterface; - - Faction * - faction = this->aiInterface->getMyFaction (); - if (faction->getAIBehaviorStaticOverideValue (aibsvcMaxBuildRadius) != - INT_MAX) - { - maxBuildRadius = - faction->getAIBehaviorStaticOverideValue (aibsvcMaxBuildRadius); - //printf("Discovered overriden static value for AI, maxBuildRadius = %d\n",maxBuildRadius); - } - if (faction->getAIBehaviorStaticOverideValue (aibsvcMinMinWarriors) != - INT_MAX) - { - minMinWarriors = - faction->getAIBehaviorStaticOverideValue (aibsvcMinMinWarriors); - //printf("Discovered overriden static value for AI, minMinWarriors = %d\n",minMinWarriors); - } - if (faction-> - getAIBehaviorStaticOverideValue (aibsvcMinMinWarriorsExpandCpuEasy) - != INT_MAX) - { - minMinWarriorsExpandCpuEasy = - faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinMinWarriorsExpandCpuEasy); - //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuEasy = %d\n",minMinWarriorsExpandCpuEasy); - } - if (faction-> - getAIBehaviorStaticOverideValue (aibsvcMinMinWarriorsExpandCpuZeta) - != INT_MAX) - { - minMinWarriorsExpandCpuZeta = - faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinMinWarriorsExpandCpuZeta); - //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuZeta = %d\n",minMinWarriorsExpandCpuZeta); - } - if (faction-> - getAIBehaviorStaticOverideValue (aibsvcMinMinWarriorsExpandCpuUltra) - != INT_MAX) - { - minMinWarriorsExpandCpuUltra = - faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinMinWarriorsExpandCpuUltra); - //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuUltra = %d\n",minMinWarriorsExpandCpuUltra); - } - if (faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinMinWarriorsExpandCpuNormal) != INT_MAX) - { - minMinWarriorsExpandCpuNormal = - faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinMinWarriorsExpandCpuNormal); - //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuNormal = %d\n",minMinWarriorsExpandCpuNormal); - } - if (faction->getAIBehaviorStaticOverideValue (aibsvcMaxMinWarriors) != - INT_MAX) - { - maxMinWarriors = - faction->getAIBehaviorStaticOverideValue (aibsvcMaxMinWarriors); - //printf("Discovered overriden static value for AI, maxMinWarriors = %d\n",maxMinWarriors); - } - if (faction->getAIBehaviorStaticOverideValue (aibsvcMaxExpansions) != - INT_MAX) - { - maxExpansions = - faction->getAIBehaviorStaticOverideValue (aibsvcMaxExpansions); - //printf("Discovered overriden static value for AI, maxExpansions = %d\n",maxExpansions); - } - if (faction->getAIBehaviorStaticOverideValue (aibsvcVillageRadius) != - INT_MAX) - { - villageRadius = - faction->getAIBehaviorStaticOverideValue (aibsvcVillageRadius); - //printf("Discovered overriden static value for AI, villageRadius = %d\n",villageRadius); - } - if (faction-> - getAIBehaviorStaticOverideValue (aibsvcScoutResourceRange) != - INT_MAX) - { - scoutResourceRange = - faction-> - getAIBehaviorStaticOverideValue (aibsvcScoutResourceRange); - //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); - } - if (faction-> - getAIBehaviorStaticOverideValue (aibsvcMinWorkerAttackersHarvesting) - != INT_MAX) - { - minWorkerAttackersHarvesting = - faction-> - getAIBehaviorStaticOverideValue - (aibsvcMinWorkerAttackersHarvesting); - //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); - } - if (faction->getAIBehaviorStaticOverideValue (aibsvcMinBuildSpacing) != - INT_MAX) - { - minBuildSpacing = - faction->getAIBehaviorStaticOverideValue (aibsvcMinBuildSpacing); - //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); - } - - if (useStartLocation == -1) - { - startLoc = - random.randRange (0, aiInterface->getMapMaxPlayers () - 1); - } - else - { - startLoc = useStartLocation; - } - minWarriors = minMinWarriors; - randomMinWarriorsReached = false; - //add ai rules - aiRules.clear (); - aiRules.push_back (new AiRuleWorkerHarvest (this)); - aiRules.push_back (new AiRuleRefreshHarvester (this)); - aiRules.push_back (new AiRuleScoutPatrol (this)); - aiRules.push_back (new AiRuleUnBlock (this)); - aiRules.push_back (new AiRuleReturnBase (this)); - aiRules.push_back (new AiRuleMassiveAttack (this)); - aiRules.push_back (new AiRuleAddTasks (this)); - aiRules.push_back (new AiRuleProduceResourceProducer (this)); - aiRules.push_back (new AiRuleBuildOneFarm (this)); - aiRules.push_back (new AiRuleProduce (this)); - aiRules.push_back (new AiRuleBuild (this)); - aiRules.push_back (new AiRuleUpgrade (this)); - aiRules.push_back (new AiRuleExpand (this)); - aiRules.push_back (new AiRuleRepair (this)); - aiRules.push_back (new AiRuleRepair (this)); - } - - Ai::~Ai () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", - __FILE__, __FUNCTION__, __LINE__, - aiInterface); - deleteValues (tasks.begin (), tasks.end ()); - tasks.clear (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", - __FILE__, __FUNCTION__, __LINE__, - aiInterface); - - deleteValues (aiRules.begin (), aiRules.end ()); - aiRules.clear (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", - __FILE__, __FUNCTION__, __LINE__, - aiInterface); - - aiInterface = NULL; - } - - RandomGen * - Ai::getRandom () - { -// if(Thread::isCurrentThreadMainThread() == false) { -// throw megaglest_runtime_error("Invalid access to AI random from outside main thread current id = " + -// intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); -// } - return &random; - } - - void - Ai::update () - { - - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono.start (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (aiInterface->getMyFaction ()->getFirstSwitchTeamVote () != NULL) - { - const SwitchTeamVote * - vote = aiInterface->getMyFaction ()->getFirstSwitchTeamVote (); - aiInterface->getMyFaction ()-> - setCurrentSwitchTeamVoteFactionIndex (vote->factionIndex); - - factionSwitchTeamRequestCount[vote->factionIndex]++; - int - factionSwitchTeamRequestCountCurrent = - factionSwitchTeamRequestCount[vote->factionIndex]; - - //int allowJoinTeam = random.randRange(0, 100); - //srand(time(NULL) + aiInterface->getMyFaction()->getIndex()); - Chrono - seed (true); - srand ((unsigned int) seed.getCurTicks () + - aiInterface->getMyFaction ()->getIndex ()); - - int - allowJoinTeam = rand () % 100; - - SwitchTeamVote * - voteResult = - aiInterface->getMyFaction ()->getSwitchTeamVote (vote-> - factionIndex); - voteResult->voted = true; - voteResult->allowSwitchTeam = false; - - const GameSettings * - settings = aiInterface->getWorld ()->getGameSettings (); - - // If AI player already lost game they cannot vote - if (aiInterface->getWorld ()-> - factionLostGame (aiInterface->getFactionIndex ()) == true) - { - voteResult->allowSwitchTeam = true; - } - else - { - // Can only ask the AI player 2 times max per game - if (factionSwitchTeamRequestCountCurrent <= 2) - { - // x% chance the AI will answer yes - if (settings->getAiAcceptSwitchTeamPercentChance () >= 100) - { - voteResult->allowSwitchTeam = true; - } - else if (settings->getAiAcceptSwitchTeamPercentChance () <= - 0) - { - voteResult->allowSwitchTeam = false; - } - else - { - voteResult->allowSwitchTeam = - (allowJoinTeam >= - (100 - - settings->getAiAcceptSwitchTeamPercentChance ())); - } - } - } - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "AI for faction# %d voted %s [%d] CountCurrent [%d] PercentChance [%d]", - aiInterface->getMyFaction ()->getIndex (), - (voteResult->allowSwitchTeam ? "Yes" : "No"), - allowJoinTeam, factionSwitchTeamRequestCountCurrent, - settings->getAiAcceptSwitchTeamPercentChance ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] %s\n", __FILE__, - __FUNCTION__, __LINE__, szBuf); - - aiInterface->printLog (3, szBuf); - - aiInterface->giveCommandSwitchTeamVote (aiInterface-> - getMyFaction (), - voteResult); - } - - //process ai rules - for (unsigned int ruleIdx = 0; ruleIdx < aiRules.size (); ++ruleIdx) - { - AiRule * - rule = aiRules[ruleIdx]; - if (rule == NULL) - { - throw - megaglest_runtime_error ("rule == NULL"); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis (), ruleIdx); - // Determines wether to process AI rules. Whether a particular rule is processed, is weighted by getTestInterval(). - // Values returned by getTestInterval() are defined in ai_rule.h. - if ((aiInterface->getTimer () % - (rule->getTestInterval () * GameConstants::updateFps / - 1000)) == 0) - { - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, before rule->test()]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis (), ruleIdx); - - //printf("Testing AI Faction # %d RULE Name[%s]\n",aiInterface->getFactionIndex(),rule->getName().c_str()); - - // Test to see if AI can execute rule e.g. is there a worker available to for harvesting wood? - if (rule->test ()) - { - if (outputAIBehaviourToConsole ()) - printf - ("\n\nYYYYY Executing AI Faction # %d RULE Name[%s]\n\n", - aiInterface->getFactionIndex (), - rule->getName ().c_str ()); - - aiInterface->printLog (3, - intToStr (1000 * - aiInterface->getTimer () / - GameConstants::updateFps) + - ": Executing rule: " + - rule->getName () + '\n'); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, before rule->execute() [%s]]\n", - __FILE__, __FUNCTION__, - __LINE__, chrono.getMillis (), - ruleIdx, - rule->getName ().c_str ()); - // Execute the rule. - rule->execute (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, after rule->execute() [%s]]\n", - __FILE__, __FUNCTION__, - __LINE__, chrono.getMillis (), - ruleIdx, - rule->getName ().c_str ()); - } - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [END]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - } - - -// ==================== state requests ==================== - - int - Ai::getCountOfType (const UnitType * ut) - { - int - count = 0; - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - if (ut == aiInterface->getMyUnit (i)->getType ()) - { - count++; - } - } - return count; - } - - int - Ai::getCountOfClass (UnitClass uc, - UnitClass * additionalUnitClassToExcludeFromCount) - { - int - count = 0; - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - if (aiInterface->getMyUnit (i)->getType ()->isOfClass (uc)) - { - // Skip unit if it ALSO contains the exclusion unit class type - if (additionalUnitClassToExcludeFromCount != NULL) - { - if (aiInterface->getMyUnit (i)->getType ()-> - isOfClass (*additionalUnitClassToExcludeFromCount)) - { - continue; - } - } - ++count; - } - } - return count; - } - - float - Ai::getRatioOfClass (UnitClass uc, - UnitClass * additionalUnitClassToExcludeFromCount) - { - if (aiInterface->getMyUnitCount () == 0) - { - return 0; - } - else - { - //return static_cast(getCountOfClass(uc,additionalUnitClassToExcludeFromCount)) / aiInterface->getMyUnitCount(); - return truncateDecimal < float >(static_cast < - float - >(getCountOfClass - (uc, - additionalUnitClassToExcludeFromCount)) - / aiInterface->getMyUnitCount (), - 6); - } - } - - const ResourceType * - Ai::getNeededResource (int unitIndex) - { - int - amount = INT_MAX; - const ResourceType * - neededResource = NULL; - const TechTree * - tt = aiInterface->getTechTree (); - const Unit * - unit = aiInterface->getMyUnit (unitIndex); - - for (int i = 0; i < tt->getResourceTypeCount (); ++i) - { - const ResourceType * - rt = tt->getResourceType (i); - const Resource * - r = aiInterface->getResource (rt); - - if (rt->getClass () != rcStatic && rt->getClass () != rcConsumable) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Examining resource [%s] amount [%d] (previous amount [%d]", - rt->getName ().c_str (), r->getAmount (), amount); - aiInterface->printLog (3, szBuf); - } - - if (rt->getClass () != rcStatic && rt->getClass () != rcConsumable - && r->getAmount () < amount) - { - - // Only have up to x units going for this resource so we can focus - // on other needed resources for other units - const int - maxUnitsToHarvestResource = 5; - - vector < int > - unitsGettingResource = findUnitsHarvestingResourceType (rt); - if ((int) unitsGettingResource.size () <= - maxUnitsToHarvestResource) - { - // Now MAKE SURE the unit has a harvest command for this resource - // AND that the resource is within eye-sight to avoid units - // standing around doing nothing. - const HarvestCommandType * - hct = - unit->getType ()->getFirstHarvestCommand (rt, - unit-> - getFaction ()); - Vec2i - resPos; - if (hct != NULL - && aiInterface->getNearestSightedResource (rt, - aiInterface-> - getHomeLocation - (), resPos, - false)) - { - amount = r->getAmount (); - neededResource = rt; - } - } - } - } - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Unit [%d - %s] looking for resources (not static or consumable)", - unit->getId (), unit->getType ()->getName (false).c_str ()); - aiInterface->printLog (3, szBuf); - snprintf (szBuf, 8096, "[resource type count %d] Needed resource [%s].", - tt->getResourceTypeCount (), - (neededResource != - NULL ? neededResource->getName ().c_str () : "")); - aiInterface->printLog (3, szBuf); - - return neededResource; - } - - bool - Ai::beingAttacked (Vec2i & pos, Field & field, int radius) - { - const Unit * - enemy = aiInterface->getFirstOnSightEnemyUnit (pos, field, radius); - return (enemy != NULL); - } - - bool - Ai::isStableBase () - { - UnitClass - ucWorkerType = ucWorker; - if (getCountOfClass (ucWarrior, &ucWorkerType) > minWarriors) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Base is stable [minWarriors = %d found = %d]", - minWarriors, ucWorkerType); - aiInterface->printLog (4, szBuf); - - return true; - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Base is NOT stable [minWarriors = %d found = %d]", - minWarriors, ucWorkerType); - aiInterface->printLog (4, szBuf); - - return false; - } - } - - bool - Ai::findAbleUnit (int *unitIndex, CommandClass ability, bool idleOnly) - { - vector < int > - units; - - *unitIndex = -1; - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - unit = aiInterface->getMyUnit (i); - if (unit->getType ()->isCommandable () - && unit->getType ()->hasCommandClass (ability)) - { - if (!idleOnly || !unit->anyCommand () - || unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccStop) - { - units.push_back (i); - } - } - } - - if (units.empty ()) - { - return false; - } - else - { - *unitIndex = units[random.randRange (0, (int) units.size () - 1)]; - return true; - } - } - - vector < int > - Ai::findUnitsHarvestingResourceType (const ResourceType * rt) - { - vector < int > - units; - - Map * - map = aiInterface->getMap (); - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - unit = aiInterface->getMyUnit (i); - if (unit->getType ()->isCommandable ()) - { - if (unit->getType ()->hasCommandClass (ccHarvest)) - { - if (unit->anyCommand () - && unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccHarvest) - { - Command * - command = unit->getCurrCommand (); - const HarvestCommandType * - hct = dynamic_cast < const - HarvestCommandType * >(command->getCommandType ()); - if (hct != NULL) - { - const Vec2i - unitTargetPos = unit->getTargetPos (); - SurfaceCell * - sc = - map-> - getSurfaceCell (Map:: - toSurfCoords (unitTargetPos)); - Resource * - r = sc->getResource (); - if (r != NULL && r->getType () == rt) - { - units.push_back (i); - } - } - } - } - else if (unit->getType ()->hasCommandClass (ccProduce)) - { - if (unit->anyCommand () - && unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccProduce) - { - Command * - command = unit->getCurrCommand (); - const ProduceCommandType * - pct = dynamic_cast < const - ProduceCommandType * >(command->getCommandType ()); - if (pct != NULL) - { - const UnitType * - ut = pct->getProducedUnit (); - if (ut != NULL) - { - const Resource * - r = ut->getCost (rt); - if (r != NULL) - { - if (r != NULL && r->getAmount () < 0) - { - units.push_back (i); - } - } - } - } - } - } - else if (unit->getType ()->hasCommandClass (ccBuild)) - { - if (unit->anyCommand () - && unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccBuild) - { - Command * - command = unit->getCurrCommand (); - const BuildCommandType * - bct = dynamic_cast < const - BuildCommandType * >(command->getCommandType ()); - if (bct != NULL) - { - for (int j = 0; j < bct->getBuildingCount (); ++j) - { - const UnitType * - ut = bct->getBuilding (j); - if (ut != NULL) - { - const Resource * - r = ut->getCost (rt); - if (r != NULL) - { - if (r != NULL && r->getAmount () < 0) - { - units.push_back (i); - break; - } - } - } - } - } - } - } - } - } - - return units; - } - -//vector Ai::findUnitsDoingCommand(CommandClass currentCommand) { -// vector units; -// -// for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { -// const Unit *unit= aiInterface->getMyUnit(i); -// if(unit->getType()->isCommandable() && unit->getType()->hasCommandClass(currentCommand)) { -// if(unit->anyCommand() && unit->getCurrCommand()->getCommandType()->getClass() == currentCommand) { -// units.push_back(i); -// } -// } -// } -// -// return units; -//} - - bool - Ai::findAbleUnit (int *unitIndex, CommandClass ability, - CommandClass currentCommand) - { - vector < int > - units; - - *unitIndex = -1; - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - unit = aiInterface->getMyUnit (i); - if (unit->getType ()->isCommandable () - && unit->getType ()->hasCommandClass (ability)) - { - if (unit->anyCommand () - && unit->getCurrCommand ()->getCommandType ()-> - getClass () == currentCommand) - { - units.push_back (i); - } - } - } - - if (units.empty ()) - { - return false; - } - else - { - *unitIndex = units[random.randRange (0, (int) units.size () - 1)]; - return true; - } - } - - bool - Ai::findPosForBuilding (const UnitType * building, - const Vec2i & searchPos, Vec2i & outPos) - { - - for (int currRadius = 0; currRadius < maxBuildRadius; ++currRadius) - { - for (int i = searchPos.x - currRadius; i < searchPos.x + currRadius; - ++i) - { - for (int j = searchPos.y - currRadius; - j < searchPos.y + currRadius; ++j) - { - outPos = Vec2i (i, j); - if (aiInterface-> - isFreeCells (outPos - Vec2i (minBuildSpacing), - building->getAiBuildSize () + - minBuildSpacing * 2, fLand)) - { - int - aiBuildSizeDiff = - building->getAiBuildSize () - building->getSize (); - if (aiBuildSizeDiff > 0) - { - int - halfSize = aiBuildSizeDiff / 2; - outPos.x += halfSize; - outPos.y += halfSize; - } - return true; - } - } - } - } - - return false; - - } - - -// ==================== tasks ==================== - - void - Ai::addTask (const Task * task) - { - tasks.push_back (task); - aiInterface->printLog (2, "Task added: " + task->toString ()); - } - - void - Ai::addPriorityTask (const Task * task) - { - deleteValues (tasks.begin (), tasks.end ()); - tasks.clear (); - - tasks.push_back (task); - aiInterface->printLog (2, "Priority Task added: " + task->toString ()); - } - - bool - Ai::anyTask () - { - return !tasks.empty (); - } - - const Task * - Ai::getTask () const - { - if (tasks.empty ()) - { - return NULL; - } - else - { - return tasks.front (); - } - } - - void - Ai::removeTask (const Task * task) - { - aiInterface->printLog (2, "Task removed: " + task->toString ()); - tasks.remove (task); - delete - task; - } - - void - Ai::retryTask (const Task * task) - { - tasks.remove (task); - tasks.push_back (task); - } -// ==================== expansions ==================== - - void - Ai::addExpansion (const Vec2i & pos) - { - - //check if there is a nearby expansion - for (Positions::iterator it = expansionPositions.begin (); - it != expansionPositions.end (); ++it) - { - if ((*it).dist (pos) < villageRadius) - { - return; - } - } - - //add expansion - expansionPositions.push_front (pos); - - //remove expansion if queue is list is full - if ((int) expansionPositions.size () > maxExpansions) - { - expansionPositions.pop_back (); - } - } - - Vec2i - Ai::getRandomHomePosition () - { - - if (expansionPositions.empty () || random.randRange (0, 1) == 0) - { - return aiInterface->getHomeLocation (); - } - - return expansionPositions[random. - randRange (0, - (int) expansionPositions.size () - - 1)]; - } - -// ==================== actions ==================== - - void - Ai::sendScoutPatrol () - { - - Vec2i - pos; - int - unit; - bool - possibleTargetFound = false; - - bool - ultraResourceAttack = (aiInterface->getControlType () == ctCpuUltra - || aiInterface->getControlType () == - ctNetworkCpuUltra) - && random.randRange (0, 2) == 1; - bool - megaResourceAttack = (aiInterface->getControlType () == ctCpuZeta - || aiInterface->getControlType () == - ctNetworkCpuZeta) - && random.randRange (0, 1) == 1; - - if (megaResourceAttack || ultraResourceAttack) - { - Map * - map = aiInterface->getMap (); - - const TechTree * - tt = aiInterface->getTechTree (); - const ResourceType * - rt = tt->getResourceType (0); - int - tryCount = 0; - int - height = map->getH (); - int - width = map->getW (); - - for (int i = 0; i < tt->getResourceTypeCount (); ++i) - { - const ResourceType * - rt_ = tt->getResourceType (i); - //const Resource *r= aiInterface->getResource(rt); - - if (rt_->getClass () == rcTech) - { - rt = rt_; - break; - } - } - //printf("looking for resource %s\n",rt->getName().c_str()); - while (possibleTargetFound == false) - { - tryCount++; - if (tryCount == 4) - { - //printf("no target found\n"); - break; - } - pos = - Vec2i (random.randRange (2, width - 2), - random.randRange (2, height - 2)); - if (map->isInside (pos) - && map->isInsideSurface (map->toSurfCoords (pos))) - { - //printf("is inside map\n"); - // find first resource in this area - Vec2i - resPos; - if (aiInterface-> - isResourceInRegion (pos, rt, resPos, - scoutResourceRange)) - { - // found a possible target. - pos = resPos; - //printf("lets try the new target\n"); - possibleTargetFound = true; - break; - } - } - //else printf("is outside map\n"); - } - } - - std::vector < Vec2i > warningEnemyList = - aiInterface->getEnemyWarningPositionList (); - if ((possibleTargetFound == false) - && (warningEnemyList.empty () == false)) - { - //for(int i = (int)warningEnemyList.size() - 1; i <= 0; --i) { - //Vec2i &checkPos = warningEnemyList[i]; - Vec2i & checkPos = warningEnemyList[0]; - if (random.randRange (0, 1) == 1) - { - pos = checkPos; - possibleTargetFound = true; - warningEnemyList.clear (); - } - else - { - aiInterface->removeEnemyWarningPositionFromList (checkPos); - } - //break; - //} - } - - if (possibleTargetFound == false) - { - startLoc = (startLoc + 1) % aiInterface->getMapMaxPlayers (); - pos = aiInterface->getStartLocation (startLoc); - //printf("normal target used\n"); - } - - if (aiInterface->getHomeLocation () != pos) - { - if (findAbleUnit (&unit, ccAttack, false)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - aiInterface->giveCommand (unit, ccAttack, pos); - aiInterface->printLog (2, - "Scout patrol sent to: " + - intToStr (pos.x) + "," + - intToStr (pos.y) + "\n"); - } - } - } - - void - Ai::massiveAttack (const Vec2i & pos, Field field, bool ultraAttack) - { - int - producerWarriorCount = 0; - int - maxProducerWarriors = random.randRange (1, 11); - int - unitCount = aiInterface->getMyUnitCount (); - int - unitGroupCommandId = -1; - - int - attackerWorkersHarvestingCount = 0; - for (int i = 0; i < unitCount; ++i) - { - bool - isWarrior = false; - bool - productionInProgress = false; - const Unit * - unit = aiInterface->getMyUnit (i); - const AttackCommandType * - act = unit->getType ()->getFirstAttackCommand (field); - - if (aiInterface->getControlType () == ctCpuZeta || - aiInterface->getControlType () == ctNetworkCpuZeta) - { - if (producerWarriorCount > maxProducerWarriors) - { - if (unit->getCommandSize () > 0 && - unit->getCurrCommand ()->getCommandType () != NULL - && (unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccBuild - || unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccMorph - || unit->getCurrCommand ()->getCommandType ()-> - getClass () == ccProduce)) - { - productionInProgress = true; - isWarrior = false; - producerWarriorCount++; - } - else - { - isWarrior = - !unit->getType ()->hasCommandClass (ccHarvest); - } - - } - else - { - isWarrior = !unit->getType ()->hasCommandClass (ccHarvest) - && !unit->getType ()->hasCommandClass (ccProduce); - } - } - else - { - isWarrior = !unit->getType ()->hasCommandClass (ccHarvest) - && !unit->getType ()->hasCommandClass (ccProduce); - } - - bool - alreadyAttacking = - (unit->getCurrSkill ()->getClass () == scAttack); - - bool - unitSignalledToAttack = false; - if (alreadyAttacking == false - && unit->getType ()->hasSkillClass (scAttack) - && (aiInterface->getControlType () == ctCpuUltra - || aiInterface->getControlType () == ctCpuZeta - || aiInterface->getControlType () == ctNetworkCpuUltra - || aiInterface->getControlType () == ctNetworkCpuZeta)) - { - //printf("~~~~~~~~ Unit [%s - %d] checking if unit is being attacked\n",unit->getFullName().c_str(),unit->getId()); - - std::pair < bool, Unit * >beingAttacked = - aiInterface->getWorld ()->getUnitUpdater ()-> - unitBeingAttacked (unit); - if (beingAttacked.first == true) - { - Unit * - enemy = beingAttacked.second; - const AttackCommandType * - act_forenemy = - unit->getType ()->getFirstAttackCommand (enemy-> - getCurrField ()); - - //printf("~~~~~~~~ Unit [%s - %d] attacked by enemy [%s - %d] act_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),act_forenemy,enemy->getCurrField()); - - if (act_forenemy != NULL) - { - bool - shouldAttack = true; - if (unit->getType ()->hasSkillClass (scHarvest)) - { - shouldAttack = - (attackerWorkersHarvestingCount > - minWorkerAttackersHarvesting); - if (shouldAttack == false) - { - attackerWorkersHarvestingCount++; - } - } - if (shouldAttack) - { - if (unitGroupCommandId == -1) - { - unitGroupCommandId = - aiInterface->getWorld ()-> - getNextCommandGroupId (); - } - - aiInterface->giveCommand (i, act_forenemy, - beingAttacked.second-> - getPos (), - unitGroupCommandId); - unitSignalledToAttack = true; - } - } - else - { - const AttackStoppedCommandType * - asct_forenemy = - unit->getType ()-> - getFirstAttackStoppedCommand (enemy->getCurrField ()); - //printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] asct_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),asct_forenemy,enemy->getCurrField()); - if (asct_forenemy != NULL) - { - bool - shouldAttack = true; - if (unit->getType ()->hasSkillClass (scHarvest)) - { - shouldAttack = - (attackerWorkersHarvestingCount > - minWorkerAttackersHarvesting); - if (shouldAttack == false) - { - attackerWorkersHarvestingCount++; - } - } - if (shouldAttack) - { -// printf("~~~~~~~~ Unit [%s - %d] WILL AttackStoppedCommand [%s - %d]\n", unit->getFullName().c_str(), -// unit->getId(), enemy->getFullName().c_str(), enemy->getId()); - - if (unitGroupCommandId == -1) - { - unitGroupCommandId = - aiInterface->getWorld ()-> - getNextCommandGroupId (); - } - - aiInterface->giveCommand (i, asct_forenemy, - beingAttacked.second-> - getCenteredPos (), - unitGroupCommandId); - unitSignalledToAttack = true; - } - } - } - } - } - if (alreadyAttacking == false && act != NULL - && (ultraAttack || isWarrior) && unitSignalledToAttack == false) - { - bool - shouldAttack = true; - if (unit->getType ()->hasSkillClass (scHarvest)) - { - shouldAttack = - (attackerWorkersHarvestingCount > - minWorkerAttackersHarvesting); - if (shouldAttack == false) - { - attackerWorkersHarvestingCount++; - } - } - - // Zeta CPU does not send ( far away ) units which are currently producing something - if (aiInterface->getControlType () == ctCpuZeta - || aiInterface->getControlType () == ctNetworkCpuZeta) - { - if (!isWarrior) - { - if (!productionInProgress) - { - shouldAttack = false; - //printf("no attack \n "); - } - } - } - if (shouldAttack) - { - if (unitGroupCommandId == -1) - { - unitGroupCommandId = - aiInterface->getWorld ()->getNextCommandGroupId (); - } - - aiInterface->giveCommand (i, act, pos, unitGroupCommandId); - } - } - } - - if (aiInterface->getControlType () == ctCpuEasy || - aiInterface->getControlType () == ctNetworkCpuEasy) - { - minWarriors += minMinWarriorsExpandCpuEasy; - } - else if (aiInterface->getControlType () == ctCpuZeta || - aiInterface->getControlType () == ctNetworkCpuZeta) - { - minWarriors += minMinWarriorsExpandCpuZeta; - if (minWarriors > maxMinWarriors - 1 || randomMinWarriorsReached) - { - randomMinWarriorsReached = true; - minWarriors = - random.randRange (maxMinWarriors - 10, maxMinWarriors * 2); - } - } - else if (minWarriors < maxMinWarriors) - { - if (aiInterface->getControlType () == ctCpuUltra || - aiInterface->getControlType () == ctNetworkCpuUltra) - { - minWarriors += minMinWarriorsExpandCpuUltra; - } - else - { - minWarriors += minMinWarriorsExpandCpuNormal; - } - } - aiInterface->printLog (2, - "Massive attack to pos: " + intToStr (pos.x) + - ", " + intToStr (pos.y) + "\n"); - } - - void - Ai::returnBase (int unitIndex) - { - Vec2i - pos; - //std::pair r(crFailUndefined,""); - //aiInterface->getFactionIndex(); - pos = Vec2i (random.randRange (-villageRadius, villageRadius), - random.randRange (-villageRadius, villageRadius)) + - getRandomHomePosition (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - //r= aiInterface->giveCommand(unitIndex, ccMove, pos); - aiInterface->giveCommand (unitIndex, ccMove, pos); - - //aiInterface->printLog(1, "Order return to base pos:" + intToStr(pos.x)+", "+intToStr(pos.y)+": "+rrToStr(r)+"\n"); - } - - void - Ai::harvest (int unitIndex) - { - const ResourceType * - rt = getNeededResource (unitIndex); - if (rt != NULL) - { - const HarvestCommandType * - hct = - aiInterface->getMyUnit (unitIndex)->getType ()-> - getFirstHarvestCommand (rt, - aiInterface->getMyUnit (unitIndex)-> - getFaction ()); - - Vec2i - resPos; - if (hct != NULL - && aiInterface->getNearestSightedResource (rt, - aiInterface-> - getHomeLocation (), - resPos, false)) - { - resPos = - resPos + Vec2i (random.randRange (-2, 2), - random.randRange (-2, 2)); - aiInterface->giveCommand (unitIndex, hct, resPos, -1); - //aiInterface->printLog(4, "Order harvest pos:" + intToStr(resPos.x)+", "+intToStr(resPos.y)+": "+rrToStr(r)+"\n"); - } - } - } - - bool - Ai::haveBlockedUnits () - { - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono.start (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - - int - unitCount = aiInterface->getMyUnitCount (); - Map * - map = aiInterface->getMap (); - //If there is no close store - for (int j = 0; j < unitCount; ++j) - { - const Unit * - u = aiInterface->getMyUnit (j); - const UnitType * - ut = u->getType (); - - // If this building is a store - if (u->isAlive () && ut->isMobile () && u->getPath () != NULL - && (u->getPath ()->isBlocked () - || u->getPath ()->getBlockCount ())) - { - Vec2i - unitPos = u->getPosNotThreadSafe (); - - //printf("#1 AI found blocked unit [%d - %s]\n",u->getId(),u->getFullName().c_str()); - - int - failureCount = 0; - int - cellCount = 0; - - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = unitPos + Vec2i (i, j); - if (map->isInside (pos) - && map->isInsideSurface (map->toSurfCoords (pos))) - { - if (pos != unitPos) - { - bool - canUnitMoveToCell = - map->aproxCanMove (u, unitPos, pos); - if (canUnitMoveToCell == false) - { - failureCount++; - } - cellCount++; - } - } - } - } - bool - unitImmediatelyBlocked = (failureCount == cellCount); - //printf("#1 unitImmediatelyBlocked = %d, failureCount = %d, cellCount = %d\n",unitImmediatelyBlocked,failureCount,cellCount); - - if (unitImmediatelyBlocked) - { - //printf("#1 AI unit IS BLOCKED [%d - %s]\n",u->getId(),u->getFullName().c_str()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, - __LINE__, chrono.getMillis ()); - return true; - } - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - return false; - } - - bool - Ai::getAdjacentUnits (std::map < float, std::map < int, - const Unit * > >&signalAdjacentUnits, - const Unit * unit) - { - //printf("In getAdjacentUnits...\n"); - - bool - result = false; - Map * - map = aiInterface->getMap (); - Vec2i - unitPos = unit->getPosNotThreadSafe (); - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = unitPos + Vec2i (i, j); - if (map->isInside (pos) - && map->isInsideSurface (map->toSurfCoords (pos))) - { - if (pos != unitPos) - { - Unit * - adjacentUnit = - map->getCell (pos)->getUnit (unit->getCurrField ()); - if (adjacentUnit != NULL - && adjacentUnit->getFactionIndex () == - unit->getFactionIndex ()) - { - if (adjacentUnit->getType ()->isMobile () - && adjacentUnit->getPath () != NULL) - { - //signalAdjacentUnits.push_back(adjacentUnit); - float - dist = unitPos.dist (adjacentUnit->getPos ()); - - std::map < float, - std::map < int, const Unit *> >::iterator - iterFind1 = signalAdjacentUnits.find (dist); - if (iterFind1 == signalAdjacentUnits.end ()) - { - signalAdjacentUnits[dist][adjacentUnit-> - getId ()] = - adjacentUnit; - - getAdjacentUnits (signalAdjacentUnits, - adjacentUnit); - result = true; - } - else - { - std::map < int, const Unit *>::iterator - iterFind2 = - iterFind1->second.find (adjacentUnit-> - getId ()); - if (iterFind2 == iterFind1->second.end ()) - { - signalAdjacentUnits[dist][adjacentUnit-> - getId ()] = - adjacentUnit; - getAdjacentUnits (signalAdjacentUnits, - adjacentUnit); - result = true; - } - } - } - } - } - } - } - } - return result; - } - - void - Ai::unblockUnits () - { - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono.start (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - - int - unitCount = aiInterface->getMyUnitCount (); - Map * - map = aiInterface->getMap (); - // Find blocked units and move surrounding units out of the way - std::map < float, - std::map < int, const Unit *> > - signalAdjacentUnits; - for (int idx = 0; idx < unitCount; ++idx) - { - const Unit * - u = aiInterface->getMyUnit (idx); - const UnitType * - ut = u->getType (); - - // If this building is a store - if (u->isAlive () && ut->isMobile () && u->getPath () != NULL - && (u->getPath ()->isBlocked () - || u->getPath ()->getBlockCount ())) - { - Vec2i - unitPos = u->getPosNotThreadSafe (); - - //printf("#2 AI found blocked unit [%d - %s]\n",u->getId(),u->getFullName().c_str()); - - //int failureCount = 0; - //int cellCount = 0; - - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = unitPos + Vec2i (i, j); - if (map->isInside (pos) - && map->isInsideSurface (map->toSurfCoords (pos))) - { - if (pos != unitPos) - { - bool - canUnitMoveToCell = - map->aproxCanMove (u, unitPos, pos); - if (canUnitMoveToCell == false) - { - //failureCount++; - getAdjacentUnits (signalAdjacentUnits, u); - } - //cellCount++; - } - } - } - } - //bool unitImmediatelyBlocked = (failureCount == cellCount); - //printf("#2 unitImmediatelyBlocked = %d, failureCount = %d, cellCount = %d, signalAdjacentUnits.size() = %d\n",unitImmediatelyBlocked,failureCount,cellCount,signalAdjacentUnits.size()); - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (signalAdjacentUnits.empty () == false) - { - //printf("#2 AI units ARE BLOCKED about to unblock\n"); - - int - unitGroupCommandId = -1; - - for (std::map < float, std::map < int, - const Unit * > >::reverse_iterator iterMap = - signalAdjacentUnits.rbegin (); - iterMap != signalAdjacentUnits.rend (); ++iterMap) - { - - for (std::map < int, const Unit * >::iterator iterMap2 = - iterMap->second.begin (); - iterMap2 != iterMap->second.end (); ++iterMap2) - { - //int idx = iterMap2->first; - const Unit * - adjacentUnit = iterMap2->second; - if (adjacentUnit != NULL - && adjacentUnit->getType ()-> - getFirstCtOfClass (ccMove) != NULL) - { - const CommandType * - ct = - adjacentUnit->getType ()->getFirstCtOfClass (ccMove); - - for (int moveAttempt = 1; moveAttempt <= villageRadius; - ++moveAttempt) - { - Vec2i - pos = - Vec2i (random. - randRange (-villageRadius * 2, - villageRadius * 2), - random.randRange (-villageRadius * 2, - villageRadius * 2)) + - adjacentUnit->getPosNotThreadSafe (); - - bool - canUnitMoveToCell = - map->aproxCanMove (adjacentUnit, - adjacentUnit-> - getPosNotThreadSafe (), pos); - if (canUnitMoveToCell == true) - { - - if (ct != NULL) - { - if (unitGroupCommandId == -1) - { - unitGroupCommandId = - aiInterface->getWorld ()-> - getNextCommandGroupId (); - } - - //std::pair r = aiInterface->giveCommand(adjacentUnit,ct, pos, unitGroupCommandId); - aiInterface->giveCommand (adjacentUnit, ct, - pos, - unitGroupCommandId); - } - } - } - } - } - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - __FILE__, __FUNCTION__, __LINE__, - chrono.getMillis ()); - } - - bool - Ai::outputAIBehaviourToConsole () const - { - return - false; - } - - void - Ai::saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - aiNode = rootNode->addChild ("Ai"); - -// AiInterface *aiInterface; -// AiRules aiRules; -// int startLoc; - aiNode-> - addAttribute ("startLoc", intToStr (startLoc), mapTagReplacements); -// bool randomMinWarriorsReached; - aiNode-> - addAttribute ("randomMinWarriorsReached", - intToStr (randomMinWarriorsReached), mapTagReplacements); -// Tasks tasks; - for (Tasks::const_iterator it = tasks.begin (); it != tasks.end (); - ++it) - { - (*it)->saveGame (aiNode); - } -// Positions expansionPositions; - for (Positions::const_iterator it = expansionPositions.begin (); - it != expansionPositions.end (); ++it) - { - XmlNode * - expansionPositionsNode = aiNode->addChild ("expansionPositions"); - expansionPositionsNode->addAttribute ("pos", (*it).getString (), - mapTagReplacements); - } - -// RandomGen random; - aiNode->addAttribute ("random", intToStr (random.getLastNumber ()), - mapTagReplacements); -// std::map factionSwitchTeamRequestCount; - -// int maxBuildRadius; - aiNode->addAttribute ("maxBuildRadius", intToStr (maxBuildRadius), - mapTagReplacements); -// int minMinWarriors; - aiNode->addAttribute ("minMinWarriors", intToStr (minMinWarriors), - mapTagReplacements); -// int minMinWarriorsExpandCpuEasy; - aiNode->addAttribute ("minMinWarriorsExpandCpuEasy", - intToStr (minMinWarriorsExpandCpuEasy), - mapTagReplacements); -// int minMinWarriorsExpandCpuZeta; - aiNode->addAttribute ("minMinWarriorsExpandCpuZeta", - intToStr (minMinWarriorsExpandCpuZeta), - mapTagReplacements); -// int minMinWarriorsExpandCpuUltra; - aiNode->addAttribute ("minMinWarriorsExpandCpuUltra", - intToStr (minMinWarriorsExpandCpuUltra), - mapTagReplacements); -// int minMinWarriorsExpandCpuNormal; - aiNode->addAttribute ("minMinWarriorsExpandCpuNormal", - intToStr (minMinWarriorsExpandCpuNormal), - mapTagReplacements); -// int maxMinWarriors; - aiNode->addAttribute ("maxMinWarriors", intToStr (maxMinWarriors), - mapTagReplacements); -// int maxExpansions; - aiNode->addAttribute ("maxExpansions", intToStr (maxExpansions), - mapTagReplacements); -// int villageRadius; - aiNode->addAttribute ("villageRadius", intToStr (villageRadius), - mapTagReplacements); -// int scoutResourceRange; - aiNode->addAttribute ("scoutResourceRange", - intToStr (scoutResourceRange), - mapTagReplacements); -// int minWorkerAttackersHarvesting; - aiNode->addAttribute ("minWorkerAttackersHarvesting", - intToStr (minWorkerAttackersHarvesting), - mapTagReplacements); - } - - void - Ai::loadGame (const XmlNode * rootNode, Faction * faction) - { - const XmlNode * - aiNode = rootNode->getChild ("Ai"); - - startLoc = aiNode->getAttribute ("startLoc")->getIntValue (); - randomMinWarriorsReached = - aiNode->getAttribute ("randomMinWarriorsReached")->getIntValue () != - 0; - - vector < XmlNode * >taskNodeList = aiNode->getChildList ("Task"); - for (unsigned int i = 0; i < taskNodeList.size (); ++i) - { - XmlNode * - taskNode = taskNodeList[i]; - TaskClass - taskClass = - static_cast < TaskClass > - (taskNode->getAttribute ("taskClass")->getIntValue ()); - switch (taskClass) - { - case tcProduce: - { - ProduceTask * - newTask = ProduceTask::loadGame (taskNode, faction); - tasks.push_back (newTask); - } - break; - case tcBuild: - { - BuildTask * - newTask = BuildTask::loadGame (taskNode, faction); - tasks.push_back (newTask); - } - break; - case tcUpgrade: - { - UpgradeTask * - newTask = UpgradeTask::loadGame (taskNode, faction); - tasks.push_back (newTask); - } - break; - } - } - - vector < XmlNode * >expansionPositionsNodeList = - aiNode->getChildList ("expansionPositions"); - for (unsigned int i = 0; i < expansionPositionsNodeList.size (); ++i) - { - XmlNode * - expansionPositionsNode = expansionPositionsNodeList[i]; - Vec2i - pos = - Vec2i::strToVec2 (expansionPositionsNode->getAttribute ("pos")-> - getValue ()); - expansionPositions.push_back (pos); - } - - // RandomGen random; - random.setLastNumber (aiNode->getAttribute ("random")->getIntValue ()); - // std::map factionSwitchTeamRequestCount; - - // int maxBuildRadius; - maxBuildRadius = - aiNode->getAttribute ("maxBuildRadius")->getIntValue (); - // int minMinWarriors; - minMinWarriors = - aiNode->getAttribute ("minMinWarriors")->getIntValue (); - // int minMinWarriorsExpandCpuEasy; - minMinWarriorsExpandCpuEasy = - aiNode->getAttribute ("minMinWarriorsExpandCpuEasy")->getIntValue (); - // int minMinWarriorsExpandCpZeta; - minMinWarriorsExpandCpuZeta = - aiNode->getAttribute ("minMinWarriorsExpandCpuZeta")->getIntValue (); - // int minMinWarriorsExpandCpuUltra; - minMinWarriorsExpandCpuUltra = - aiNode->getAttribute ("minMinWarriorsExpandCpuUltra")->getIntValue (); - // int minMinWarriorsExpandCpuNormal; - minMinWarriorsExpandCpuNormal = - aiNode->getAttribute ("minMinWarriorsExpandCpuNormal")-> - getIntValue (); - // int maxMinWarriors; - maxMinWarriors = - aiNode->getAttribute ("maxMinWarriors")->getIntValue (); - // int maxExpansions; - maxExpansions = aiNode->getAttribute ("maxExpansions")->getIntValue (); - // int villageRadius; - villageRadius = aiNode->getAttribute ("villageRadius")->getIntValue (); - // int scoutResourceRange; - scoutResourceRange = - aiNode->getAttribute ("scoutResourceRange")->getIntValue (); - // int minWorkerAttackersHarvesting; - minWorkerAttackersHarvesting = - aiNode->getAttribute ("minWorkerAttackersHarvesting")->getIntValue (); - } - -}} //end namespace + Glest { + namespace + Game { + + Task::Task() { + taskClass = tcProduce; + } + + //void Task::saveGame(XmlNode *rootNode) const { + // std::map mapTagReplacements; + // XmlNode *taskNode = rootNode->addChild("Task"); + //} + + // ===================================================== + // class ProduceTask + // ===================================================== + ProduceTask::ProduceTask() : + Task() { + taskClass = tcProduce; + unitType = NULL; + resourceType = NULL; + unitClass = ucWarrior; + } + + ProduceTask::ProduceTask(UnitClass unitClass) : + Task() { + taskClass = tcProduce; + this->unitClass = unitClass; + unitType = NULL; + resourceType = NULL; + } + + ProduceTask::ProduceTask(const UnitType * unitType) : + Task() { + taskClass = tcProduce; + this->unitType = unitType; + resourceType = NULL; + unitClass = ucWarrior; + } + + ProduceTask::ProduceTask(const ResourceType * resourceType) : + Task() { + taskClass = tcProduce; + unitType = NULL; + unitClass = ucWarrior; + this->resourceType = resourceType; + } + + string + ProduceTask::toString() const { + string + str = "Produce "; + if (unitType != NULL) { + str += unitType->getName(false); + } + return + str; + } + + void + ProduceTask::saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + taskNode = rootNode->addChild("Task"); + taskNode-> + addAttribute("taskClass", intToStr(taskClass), mapTagReplacements); + XmlNode * + produceTaskNode = taskNode->addChild("ProduceTask"); + + // UnitClass unitClass; + produceTaskNode-> + addAttribute("unitClass", intToStr(unitClass), mapTagReplacements); + // const UnitType *unitType; + if (unitType != NULL) { + produceTaskNode->addAttribute("unitType", + unitType->getName(false), + mapTagReplacements); + } + // const ResourceType *resourceType; + if (resourceType != NULL) { + produceTaskNode->addAttribute("resourceType", + resourceType->getName(false), + mapTagReplacements); + } + } + + ProduceTask * + ProduceTask::loadGame(const XmlNode * rootNode, Faction * faction) { + const XmlNode * + produceTaskNode = rootNode->getChild("ProduceTask"); + + ProduceTask * + newTask = new ProduceTask(); + // UnitClass unitClass; + newTask->unitClass = + static_cast + (produceTaskNode->getAttribute("unitClass")->getIntValue()); + // const UnitType *unitType; + if (produceTaskNode->hasAttribute("unitType")) { + string + unitTypeName = + produceTaskNode->getAttribute("unitType")->getValue(); + newTask->unitType = faction->getType()->getUnitType(unitTypeName); + } + // const ResourceType *resourceType; + if (produceTaskNode->hasAttribute("resourceType")) { + string + resourceTypeName = + produceTaskNode->getAttribute("resourceType")->getValue(); + newTask->resourceType = + faction->getTechTree()->getResourceType(resourceTypeName); + } + + return newTask; + } + + // ===================================================== + // class BuildTask + // ===================================================== + BuildTask::BuildTask() { + taskClass = tcBuild; + this->unitType = NULL; + resourceType = NULL; + forcePos = false; + } + + BuildTask::BuildTask(const UnitType * unitType) { + taskClass = tcBuild; + this->unitType = unitType; + resourceType = NULL; + forcePos = false; + } + + BuildTask::BuildTask(const ResourceType * resourceType) { + taskClass = tcBuild; + unitType = NULL; + this->resourceType = resourceType; + forcePos = false; + } + + BuildTask::BuildTask(const UnitType * unitType, const Vec2i & pos) { + taskClass = tcBuild; + this->unitType = unitType; + resourceType = NULL; + forcePos = true; + this->pos = pos; + } + + string + BuildTask::toString() const { + string + str = "Build "; + if (unitType != NULL) { + str += unitType->getName(false); + } + return + str; + } + + void + BuildTask::saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + taskNode = rootNode->addChild("Task"); + taskNode-> + addAttribute("taskClass", intToStr(taskClass), mapTagReplacements); + XmlNode * + buildTaskNode = taskNode->addChild("BuildTask"); + + // const UnitType *unitType; + if (unitType != NULL) { + buildTaskNode->addAttribute("unitType", unitType->getName(false), + mapTagReplacements); + } + // const ResourceType *resourceType; + if (resourceType != NULL) { + buildTaskNode->addAttribute("resourceType", + resourceType->getName(), + mapTagReplacements); + } + // bool forcePos; + buildTaskNode->addAttribute("forcePos", intToStr(forcePos), + mapTagReplacements); + // Vec2i pos; + buildTaskNode->addAttribute("pos", pos.getString(), + mapTagReplacements); + } + + BuildTask * + BuildTask::loadGame(const XmlNode * rootNode, Faction * faction) { + const XmlNode * + buildTaskNode = rootNode->getChild("BuildTask"); + + BuildTask * + newTask = new BuildTask(); + if (buildTaskNode->hasAttribute("unitType")) { + string + unitTypeName = + buildTaskNode->getAttribute("unitType")->getValue(); + newTask->unitType = faction->getType()->getUnitType(unitTypeName); + } + if (buildTaskNode->hasAttribute("resourceType")) { + string + resourceTypeName = + buildTaskNode->getAttribute("resourceType")->getValue(); + newTask->resourceType = + faction->getTechTree()->getResourceType(resourceTypeName); + } + + newTask->forcePos = + buildTaskNode->getAttribute("forcePos")->getIntValue() != 0; + newTask->pos = + Vec2i::strToVec2(buildTaskNode->getAttribute("pos")->getValue()); + + return newTask; + } + + // ===================================================== + // class UpgradeTask + // ===================================================== + UpgradeTask::UpgradeTask() { + taskClass = tcUpgrade; + this->upgradeType = NULL; + } + + UpgradeTask::UpgradeTask(const UpgradeType * upgradeType) { + taskClass = tcUpgrade; + this->upgradeType = upgradeType; + } + + string + UpgradeTask::toString() const { + string + str = "Build "; + if (upgradeType != NULL) { + str += upgradeType->getName(); + } + return + str; + } + + void + UpgradeTask::saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + taskNode = rootNode->addChild("Task"); + taskNode-> + addAttribute("taskClass", intToStr(taskClass), mapTagReplacements); + XmlNode * + upgradeTaskNode = taskNode->addChild("UpgradeTask"); + + if (upgradeType != NULL) { + //upgradeType->saveGame(upgradeTaskNode); + upgradeTaskNode->addAttribute("upgradeType", + upgradeType->getName(), + mapTagReplacements); + } + } + + UpgradeTask * + UpgradeTask::loadGame(const XmlNode * rootNode, Faction * faction) { + const XmlNode * + upgradeTaskNode = rootNode->getChild("UpgradeTask"); + + UpgradeTask * + newTask = new UpgradeTask(); + if (upgradeTaskNode->hasAttribute("upgradeType")) { + string + upgradeTypeName = + upgradeTaskNode->getAttribute("upgradeType")->getValue(); + newTask->upgradeType = + faction->getType()->getUpgradeType(upgradeTypeName); + } + return newTask; + } + + // ===================================================== + // class Ai + // ===================================================== + + void + Ai::init(AiInterface * aiInterface, int useStartLocation) { + this->aiInterface = aiInterface; + + Faction * + faction = this->aiInterface->getMyFaction(); + if (faction->getAIBehaviorStaticOverideValue(aibsvcMaxBuildRadius) != + INT_MAX) { + maxBuildRadius = + faction->getAIBehaviorStaticOverideValue(aibsvcMaxBuildRadius); + //printf("Discovered overriden static value for AI, maxBuildRadius = %d\n",maxBuildRadius); + } + if (faction->getAIBehaviorStaticOverideValue(aibsvcMinMinWarriors) != + INT_MAX) { + minMinWarriors = + faction->getAIBehaviorStaticOverideValue(aibsvcMinMinWarriors); + //printf("Discovered overriden static value for AI, minMinWarriors = %d\n",minMinWarriors); + } + if (faction-> + getAIBehaviorStaticOverideValue(aibsvcMinMinWarriorsExpandCpuEasy) + != INT_MAX) { + minMinWarriorsExpandCpuEasy = + faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinMinWarriorsExpandCpuEasy); + //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuEasy = %d\n",minMinWarriorsExpandCpuEasy); + } + if (faction-> + getAIBehaviorStaticOverideValue(aibsvcMinMinWarriorsExpandCpuZeta) + != INT_MAX) { + minMinWarriorsExpandCpuZeta = + faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinMinWarriorsExpandCpuZeta); + //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuZeta = %d\n",minMinWarriorsExpandCpuZeta); + } + if (faction-> + getAIBehaviorStaticOverideValue(aibsvcMinMinWarriorsExpandCpuUltra) + != INT_MAX) { + minMinWarriorsExpandCpuUltra = + faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinMinWarriorsExpandCpuUltra); + //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuUltra = %d\n",minMinWarriorsExpandCpuUltra); + } + if (faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinMinWarriorsExpandCpuNormal) != INT_MAX) { + minMinWarriorsExpandCpuNormal = + faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinMinWarriorsExpandCpuNormal); + //printf("Discovered overriden static value for AI, minMinWarriorsExpandCpuNormal = %d\n",minMinWarriorsExpandCpuNormal); + } + if (faction->getAIBehaviorStaticOverideValue(aibsvcMaxMinWarriors) != + INT_MAX) { + maxMinWarriors = + faction->getAIBehaviorStaticOverideValue(aibsvcMaxMinWarriors); + //printf("Discovered overriden static value for AI, maxMinWarriors = %d\n",maxMinWarriors); + } + if (faction->getAIBehaviorStaticOverideValue(aibsvcMaxExpansions) != + INT_MAX) { + maxExpansions = + faction->getAIBehaviorStaticOverideValue(aibsvcMaxExpansions); + //printf("Discovered overriden static value for AI, maxExpansions = %d\n",maxExpansions); + } + if (faction->getAIBehaviorStaticOverideValue(aibsvcVillageRadius) != + INT_MAX) { + villageRadius = + faction->getAIBehaviorStaticOverideValue(aibsvcVillageRadius); + //printf("Discovered overriden static value for AI, villageRadius = %d\n",villageRadius); + } + if (faction-> + getAIBehaviorStaticOverideValue(aibsvcScoutResourceRange) != + INT_MAX) { + scoutResourceRange = + faction-> + getAIBehaviorStaticOverideValue(aibsvcScoutResourceRange); + //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); + } + if (faction-> + getAIBehaviorStaticOverideValue(aibsvcMinWorkerAttackersHarvesting) + != INT_MAX) { + minWorkerAttackersHarvesting = + faction-> + getAIBehaviorStaticOverideValue + (aibsvcMinWorkerAttackersHarvesting); + //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); + } + if (faction->getAIBehaviorStaticOverideValue(aibsvcMinBuildSpacing) != + INT_MAX) { + minBuildSpacing = + faction->getAIBehaviorStaticOverideValue(aibsvcMinBuildSpacing); + //printf("Discovered overriden static value for AI, scoutResourceRange = %d\n",scoutResourceRange); + } + + if (useStartLocation == -1) { + startLoc = + random.randRange(0, aiInterface->getMapMaxPlayers() - 1); + } else { + startLoc = useStartLocation; + } + minWarriors = minMinWarriors; + randomMinWarriorsReached = false; + //add ai rules + aiRules.clear(); + aiRules.push_back(new AiRuleWorkerHarvest(this)); + aiRules.push_back(new AiRuleRefreshHarvester(this)); + aiRules.push_back(new AiRuleScoutPatrol(this)); + aiRules.push_back(new AiRuleUnBlock(this)); + aiRules.push_back(new AiRuleReturnBase(this)); + aiRules.push_back(new AiRuleMassiveAttack(this)); + aiRules.push_back(new AiRuleAddTasks(this)); + aiRules.push_back(new AiRuleProduceResourceProducer(this)); + aiRules.push_back(new AiRuleBuildOneFarm(this)); + aiRules.push_back(new AiRuleProduce(this)); + aiRules.push_back(new AiRuleBuild(this)); + aiRules.push_back(new AiRuleUpgrade(this)); + aiRules.push_back(new AiRuleExpand(this)); + aiRules.push_back(new AiRuleRepair(this)); + aiRules.push_back(new AiRuleRepair(this)); + } + + Ai::~Ai() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", + __FILE__, __FUNCTION__, __LINE__, + aiInterface); + deleteValues(tasks.begin(), tasks.end()); + tasks.clear(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", + __FILE__, __FUNCTION__, __LINE__, + aiInterface); + + deleteValues(aiRules.begin(), aiRules.end()); + aiRules.clear(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] deleting AI aiInterface [%p]\n", + __FILE__, __FUNCTION__, __LINE__, + aiInterface); + + aiInterface = NULL; + } + + RandomGen * + Ai::getRandom() { + // if(Thread::isCurrentThreadMainThread() == false) { + // throw megaglest_runtime_error("Invalid access to AI random from outside main thread current id = " + + // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); + // } + return &random; + } + + void + Ai::update() { + + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono.start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (aiInterface->getMyFaction()->getFirstSwitchTeamVote() != NULL) { + const SwitchTeamVote * + vote = aiInterface->getMyFaction()->getFirstSwitchTeamVote(); + aiInterface->getMyFaction()-> + setCurrentSwitchTeamVoteFactionIndex(vote->factionIndex); + + factionSwitchTeamRequestCount[vote->factionIndex]++; + int + factionSwitchTeamRequestCountCurrent = + factionSwitchTeamRequestCount[vote->factionIndex]; + + //int allowJoinTeam = random.randRange(0, 100); + //srand(time(NULL) + aiInterface->getMyFaction()->getIndex()); + Chrono + seed(true); + srand((unsigned int) seed.getCurTicks() + + aiInterface->getMyFaction()->getIndex()); + + int + allowJoinTeam = rand() % 100; + + SwitchTeamVote * + voteResult = + aiInterface->getMyFaction()->getSwitchTeamVote(vote-> + factionIndex); + voteResult->voted = true; + voteResult->allowSwitchTeam = false; + + const GameSettings * + settings = aiInterface->getWorld()->getGameSettings(); + + // If AI player already lost game they cannot vote + if (aiInterface->getWorld()-> + factionLostGame(aiInterface->getFactionIndex()) == true) { + voteResult->allowSwitchTeam = true; + } else { + // Can only ask the AI player 2 times max per game + if (factionSwitchTeamRequestCountCurrent <= 2) { + // x% chance the AI will answer yes + if (settings->getAiAcceptSwitchTeamPercentChance() >= 100) { + voteResult->allowSwitchTeam = true; + } else if (settings->getAiAcceptSwitchTeamPercentChance() <= + 0) { + voteResult->allowSwitchTeam = false; + } else { + voteResult->allowSwitchTeam = + (allowJoinTeam >= + (100 - + settings->getAiAcceptSwitchTeamPercentChance())); + } + } + } + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "AI for faction# %d voted %s [%d] CountCurrent [%d] PercentChance [%d]", + aiInterface->getMyFaction()->getIndex(), + (voteResult->allowSwitchTeam ? "Yes" : "No"), + allowJoinTeam, factionSwitchTeamRequestCountCurrent, + settings->getAiAcceptSwitchTeamPercentChance()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] %s\n", __FILE__, + __FUNCTION__, __LINE__, szBuf); + + aiInterface->printLog(3, szBuf); + + aiInterface->giveCommandSwitchTeamVote(aiInterface-> + getMyFaction(), + voteResult); + } + + //process ai rules + for (unsigned int ruleIdx = 0; ruleIdx < aiRules.size(); ++ruleIdx) { + AiRule * + rule = aiRules[ruleIdx]; + if (rule == NULL) { + throw + megaglest_runtime_error("rule == NULL"); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis(), ruleIdx); + // Determines wether to process AI rules. Whether a particular rule is processed, is weighted by getTestInterval(). + // Values returned by getTestInterval() are defined in ai_rule.h. + if ((aiInterface->getTimer() % + (rule->getTestInterval() * GameConstants::updateFps / + 1000)) == 0) { + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, before rule->test()]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis(), ruleIdx); + + //printf("Testing AI Faction # %d RULE Name[%s]\n",aiInterface->getFactionIndex(),rule->getName().c_str()); + + // Test to see if AI can execute rule e.g. is there a worker available to for harvesting wood? + if (rule->test()) { + if (outputAIBehaviourToConsole()) + printf + ("\n\nYYYYY Executing AI Faction # %d RULE Name[%s]\n\n", + aiInterface->getFactionIndex(), + rule->getName().c_str()); + + aiInterface->printLog(3, + intToStr(1000 * + aiInterface->getTimer() / + GameConstants::updateFps) + + ": Executing rule: " + + rule->getName() + '\n'); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, before rule->execute() [%s]]\n", + __FILE__, __FUNCTION__, + __LINE__, chrono.getMillis(), + ruleIdx, + rule->getName().c_str()); + // Execute the rule. + rule->execute(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [ruleIdx = %d, after rule->execute() [%s]]\n", + __FILE__, __FUNCTION__, + __LINE__, chrono.getMillis(), + ruleIdx, + rule->getName().c_str()); + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [END]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + } + + + // ==================== state requests ==================== + + int + Ai::getCountOfType(const UnitType * ut) { + int + count = 0; + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + if (ut == aiInterface->getMyUnit(i)->getType()) { + count++; + } + } + return count; + } + + int + Ai::getCountOfClass(UnitClass uc, + UnitClass * additionalUnitClassToExcludeFromCount) { + int + count = 0; + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + if (aiInterface->getMyUnit(i)->getType()->isOfClass(uc)) { + // Skip unit if it ALSO contains the exclusion unit class type + if (additionalUnitClassToExcludeFromCount != NULL) { + if (aiInterface->getMyUnit(i)->getType()-> + isOfClass(*additionalUnitClassToExcludeFromCount)) { + continue; + } + } + ++count; + } + } + return count; + } + + float + Ai::getRatioOfClass(UnitClass uc, + UnitClass * additionalUnitClassToExcludeFromCount) { + if (aiInterface->getMyUnitCount() == 0) { + return 0; + } else { + //return static_cast(getCountOfClass(uc,additionalUnitClassToExcludeFromCount)) / aiInterface->getMyUnitCount(); + return truncateDecimal < float >(static_cast < + float + >(getCountOfClass + (uc, + additionalUnitClassToExcludeFromCount)) + / aiInterface->getMyUnitCount(), + 6); + } + } + + const ResourceType * + Ai::getNeededResource(int unitIndex) { + int + amount = INT_MAX; + const ResourceType * + neededResource = NULL; + const TechTree * + tt = aiInterface->getTechTree(); + const Unit * + unit = aiInterface->getMyUnit(unitIndex); + + for (int i = 0; i < tt->getResourceTypeCount(); ++i) { + const ResourceType * + rt = tt->getResourceType(i); + const Resource * + r = aiInterface->getResource(rt); + + if (rt->getClass() != rcStatic && rt->getClass() != rcConsumable) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Examining resource [%s] amount [%d] (previous amount [%d]", + rt->getName().c_str(), r->getAmount(), amount); + aiInterface->printLog(3, szBuf); + } + + if (rt->getClass() != rcStatic && rt->getClass() != rcConsumable + && r->getAmount() < amount) { + + // Only have up to x units going for this resource so we can focus + // on other needed resources for other units + const int + maxUnitsToHarvestResource = 5; + + vector < int > + unitsGettingResource = findUnitsHarvestingResourceType(rt); + if ((int) unitsGettingResource.size() <= + maxUnitsToHarvestResource) { + // Now MAKE SURE the unit has a harvest command for this resource + // AND that the resource is within eye-sight to avoid units + // standing around doing nothing. + const HarvestCommandType * + hct = + unit->getType()->getFirstHarvestCommand(rt, + unit-> + getFaction()); + Vec2i + resPos; + if (hct != NULL + && aiInterface->getNearestSightedResource(rt, + aiInterface-> + getHomeLocation + (), resPos, + false)) { + amount = r->getAmount(); + neededResource = rt; + } + } + } + } + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Unit [%d - %s] looking for resources (not static or consumable)", + unit->getId(), unit->getType()->getName(false).c_str()); + aiInterface->printLog(3, szBuf); + snprintf(szBuf, 8096, "[resource type count %d] Needed resource [%s].", + tt->getResourceTypeCount(), + (neededResource != + NULL ? neededResource->getName().c_str() : "")); + aiInterface->printLog(3, szBuf); + + return neededResource; + } + + bool + Ai::beingAttacked(Vec2i & pos, Field & field, int radius) { + const Unit * + enemy = aiInterface->getFirstOnSightEnemyUnit(pos, field, radius); + return (enemy != NULL); + } + + bool + Ai::isStableBase() { + UnitClass + ucWorkerType = ucWorker; + if (getCountOfClass(ucWarrior, &ucWorkerType) > minWarriors) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Base is stable [minWarriors = %d found = %d]", + minWarriors, ucWorkerType); + aiInterface->printLog(4, szBuf); + + return true; + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Base is NOT stable [minWarriors = %d found = %d]", + minWarriors, ucWorkerType); + aiInterface->printLog(4, szBuf); + + return false; + } + } + + bool + Ai::findAbleUnit(int *unitIndex, CommandClass ability, bool idleOnly) { + vector < int > + units; + + *unitIndex = -1; + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + unit = aiInterface->getMyUnit(i); + if (unit->getType()->isCommandable() + && unit->getType()->hasCommandClass(ability)) { + if (!idleOnly || !unit->anyCommand() + || unit->getCurrCommand()->getCommandType()-> + getClass() == ccStop) { + units.push_back(i); + } + } + } + + if (units.empty()) { + return false; + } else { + *unitIndex = units[random.randRange(0, (int) units.size() - 1)]; + return true; + } + } + + vector < int > + Ai::findUnitsHarvestingResourceType(const ResourceType * rt) { + vector < int > + units; + + Map * + map = aiInterface->getMap(); + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + unit = aiInterface->getMyUnit(i); + if (unit->getType()->isCommandable()) { + if (unit->getType()->hasCommandClass(ccHarvest)) { + if (unit->anyCommand() + && unit->getCurrCommand()->getCommandType()-> + getClass() == ccHarvest) { + Command * + command = unit->getCurrCommand(); + const HarvestCommandType * + hct = dynamic_cast (command->getCommandType()); + if (hct != NULL) { + const Vec2i + unitTargetPos = unit->getTargetPos(); + SurfaceCell * + sc = + map-> + getSurfaceCell(Map:: + toSurfCoords(unitTargetPos)); + Resource * + r = sc->getResource(); + if (r != NULL && r->getType() == rt) { + units.push_back(i); + } + } + } + } else if (unit->getType()->hasCommandClass(ccProduce)) { + if (unit->anyCommand() + && unit->getCurrCommand()->getCommandType()-> + getClass() == ccProduce) { + Command * + command = unit->getCurrCommand(); + const ProduceCommandType * + pct = dynamic_cast (command->getCommandType()); + if (pct != NULL) { + const UnitType * + ut = pct->getProducedUnit(); + if (ut != NULL) { + const Resource * + r = ut->getCost(rt); + if (r != NULL) { + if (r != NULL && r->getAmount() < 0) { + units.push_back(i); + } + } + } + } + } + } else if (unit->getType()->hasCommandClass(ccBuild)) { + if (unit->anyCommand() + && unit->getCurrCommand()->getCommandType()-> + getClass() == ccBuild) { + Command * + command = unit->getCurrCommand(); + const BuildCommandType * + bct = dynamic_cast (command->getCommandType()); + if (bct != NULL) { + for (int j = 0; j < bct->getBuildingCount(); ++j) { + const UnitType * + ut = bct->getBuilding(j); + if (ut != NULL) { + const Resource * + r = ut->getCost(rt); + if (r != NULL) { + if (r != NULL && r->getAmount() < 0) { + units.push_back(i); + break; + } + } + } + } + } + } + } + } + } + + return units; + } + + //vector Ai::findUnitsDoingCommand(CommandClass currentCommand) { + // vector units; + // + // for(int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + // const Unit *unit= aiInterface->getMyUnit(i); + // if(unit->getType()->isCommandable() && unit->getType()->hasCommandClass(currentCommand)) { + // if(unit->anyCommand() && unit->getCurrCommand()->getCommandType()->getClass() == currentCommand) { + // units.push_back(i); + // } + // } + // } + // + // return units; + //} + + bool + Ai::findAbleUnit(int *unitIndex, CommandClass ability, + CommandClass currentCommand) { + vector < int > + units; + + *unitIndex = -1; + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + unit = aiInterface->getMyUnit(i); + if (unit->getType()->isCommandable() + && unit->getType()->hasCommandClass(ability)) { + if (unit->anyCommand() + && unit->getCurrCommand()->getCommandType()-> + getClass() == currentCommand) { + units.push_back(i); + } + } + } + + if (units.empty()) { + return false; + } else { + *unitIndex = units[random.randRange(0, (int) units.size() - 1)]; + return true; + } + } + + bool + Ai::findPosForBuilding(const UnitType * building, + const Vec2i & searchPos, Vec2i & outPos) { + + for (int currRadius = 0; currRadius < maxBuildRadius; ++currRadius) { + for (int i = searchPos.x - currRadius; i < searchPos.x + currRadius; + ++i) { + for (int j = searchPos.y - currRadius; + j < searchPos.y + currRadius; ++j) { + outPos = Vec2i(i, j); + if (aiInterface-> + isFreeCells(outPos - Vec2i(minBuildSpacing), + building->getAiBuildSize() + + minBuildSpacing * 2, fLand)) { + int + aiBuildSizeDiff = + building->getAiBuildSize() - building->getSize(); + if (aiBuildSizeDiff > 0) { + int + halfSize = aiBuildSizeDiff / 2; + outPos.x += halfSize; + outPos.y += halfSize; + } + return true; + } + } + } + } + + return false; + + } + + + // ==================== tasks ==================== + + void + Ai::addTask(const Task * task) { + tasks.push_back(task); + aiInterface->printLog(2, "Task added: " + task->toString()); + } + + void + Ai::addPriorityTask(const Task * task) { + deleteValues(tasks.begin(), tasks.end()); + tasks.clear(); + + tasks.push_back(task); + aiInterface->printLog(2, "Priority Task added: " + task->toString()); + } + + bool + Ai::anyTask() { + return !tasks.empty(); + } + + const Task * + Ai::getTask() const { + if (tasks.empty()) { + return NULL; + } else { + return tasks.front(); + } + } + + void + Ai::removeTask(const Task * task) { + aiInterface->printLog(2, "Task removed: " + task->toString()); + tasks.remove(task); + delete + task; + } + + void + Ai::retryTask(const Task * task) { + tasks.remove(task); + tasks.push_back(task); + } + // ==================== expansions ==================== + + void + Ai::addExpansion(const Vec2i & pos) { + + //check if there is a nearby expansion + for (Positions::iterator it = expansionPositions.begin(); + it != expansionPositions.end(); ++it) { + if ((*it).dist(pos) < villageRadius) { + return; + } + } + + //add expansion + expansionPositions.push_front(pos); + + //remove expansion if queue is list is full + if ((int) expansionPositions.size() > maxExpansions) { + expansionPositions.pop_back(); + } + } + + Vec2i + Ai::getRandomHomePosition() { + + if (expansionPositions.empty() || random.randRange(0, 1) == 0) { + return aiInterface->getHomeLocation(); + } + + return expansionPositions[random. + randRange(0, + (int) expansionPositions.size() - + 1)]; + } + + // ==================== actions ==================== + + void + Ai::sendScoutPatrol() { + + Vec2i + pos; + int + unit; + bool + possibleTargetFound = false; + + bool + ultraResourceAttack = (aiInterface->getControlType() == ctCpuUltra + || aiInterface->getControlType() == + ctNetworkCpuUltra) + && random.randRange(0, 2) == 1; + bool + megaResourceAttack = (aiInterface->getControlType() == ctCpuZeta + || aiInterface->getControlType() == + ctNetworkCpuZeta) + && random.randRange(0, 1) == 1; + + if (megaResourceAttack || ultraResourceAttack) { + Map * + map = aiInterface->getMap(); + + const TechTree * + tt = aiInterface->getTechTree(); + const ResourceType * + rt = tt->getResourceType(0); + int + tryCount = 0; + int + height = map->getH(); + int + width = map->getW(); + + for (int i = 0; i < tt->getResourceTypeCount(); ++i) { + const ResourceType * + rt_ = tt->getResourceType(i); + //const Resource *r= aiInterface->getResource(rt); + + if (rt_->getClass() == rcTech) { + rt = rt_; + break; + } + } + //printf("looking for resource %s\n",rt->getName().c_str()); + while (possibleTargetFound == false) { + tryCount++; + if (tryCount == 4) { + //printf("no target found\n"); + break; + } + pos = + Vec2i(random.randRange(2, width - 2), + random.randRange(2, height - 2)); + if (map->isInside(pos) + && map->isInsideSurface(map->toSurfCoords(pos))) { + //printf("is inside map\n"); + // find first resource in this area + Vec2i + resPos; + if (aiInterface-> + isResourceInRegion(pos, rt, resPos, + scoutResourceRange)) { + // found a possible target. + pos = resPos; + //printf("lets try the new target\n"); + possibleTargetFound = true; + break; + } + } + //else printf("is outside map\n"); + } + } + + std::vector < Vec2i > warningEnemyList = + aiInterface->getEnemyWarningPositionList(); + if ((possibleTargetFound == false) + && (warningEnemyList.empty() == false)) { + //for(int i = (int)warningEnemyList.size() - 1; i <= 0; --i) { + //Vec2i &checkPos = warningEnemyList[i]; + Vec2i & checkPos = warningEnemyList[0]; + if (random.randRange(0, 1) == 1) { + pos = checkPos; + possibleTargetFound = true; + warningEnemyList.clear(); + } else { + aiInterface->removeEnemyWarningPositionFromList(checkPos); + } + //break; + //} + } + + if (possibleTargetFound == false) { + startLoc = (startLoc + 1) % aiInterface->getMapMaxPlayers(); + pos = aiInterface->getStartLocation(startLoc); + //printf("normal target used\n"); + } + + if (aiInterface->getHomeLocation() != pos) { + if (findAbleUnit(&unit, ccAttack, false)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + aiInterface->giveCommand(unit, ccAttack, pos); + aiInterface->printLog(2, + "Scout patrol sent to: " + + intToStr(pos.x) + "," + + intToStr(pos.y) + "\n"); + } + } + } + + void + Ai::massiveAttack(const Vec2i & pos, Field field, bool ultraAttack) { + int + producerWarriorCount = 0; + int + maxProducerWarriors = random.randRange(1, 11); + int + unitCount = aiInterface->getMyUnitCount(); + int + unitGroupCommandId = -1; + + int + attackerWorkersHarvestingCount = 0; + for (int i = 0; i < unitCount; ++i) { + bool + isWarrior = false; + bool + productionInProgress = false; + const Unit * + unit = aiInterface->getMyUnit(i); + const AttackCommandType * + act = unit->getType()->getFirstAttackCommand(field); + + if (aiInterface->getControlType() == ctCpuZeta || + aiInterface->getControlType() == ctNetworkCpuZeta) { + if (producerWarriorCount > maxProducerWarriors) { + if (unit->getCommandSize() > 0 && + unit->getCurrCommand()->getCommandType() != NULL + && (unit->getCurrCommand()->getCommandType()-> + getClass() == ccBuild + || unit->getCurrCommand()->getCommandType()-> + getClass() == ccMorph + || unit->getCurrCommand()->getCommandType()-> + getClass() == ccProduce)) { + productionInProgress = true; + isWarrior = false; + producerWarriorCount++; + } else { + isWarrior = + !unit->getType()->hasCommandClass(ccHarvest); + } + + } else { + isWarrior = !unit->getType()->hasCommandClass(ccHarvest) + && !unit->getType()->hasCommandClass(ccProduce); + } + } else { + isWarrior = !unit->getType()->hasCommandClass(ccHarvest) + && !unit->getType()->hasCommandClass(ccProduce); + } + + bool + alreadyAttacking = + (unit->getCurrSkill()->getClass() == scAttack); + + bool + unitSignalledToAttack = false; + if (alreadyAttacking == false + && unit->getType()->hasSkillClass(scAttack) + && (aiInterface->getControlType() == ctCpuUltra + || aiInterface->getControlType() == ctCpuZeta + || aiInterface->getControlType() == ctNetworkCpuUltra + || aiInterface->getControlType() == ctNetworkCpuZeta)) { + //printf("~~~~~~~~ Unit [%s - %d] checking if unit is being attacked\n",unit->getFullName().c_str(),unit->getId()); + + std::pair < bool, Unit * >beingAttacked = + aiInterface->getWorld()->getUnitUpdater()-> + unitBeingAttacked(unit); + if (beingAttacked.first == true) { + Unit * + enemy = beingAttacked.second; + const AttackCommandType * + act_forenemy = + unit->getType()->getFirstAttackCommand(enemy-> + getCurrField()); + + //printf("~~~~~~~~ Unit [%s - %d] attacked by enemy [%s - %d] act_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),act_forenemy,enemy->getCurrField()); + + if (act_forenemy != NULL) { + bool + shouldAttack = true; + if (unit->getType()->hasSkillClass(scHarvest)) { + shouldAttack = + (attackerWorkersHarvestingCount > + minWorkerAttackersHarvesting); + if (shouldAttack == false) { + attackerWorkersHarvestingCount++; + } + } + if (shouldAttack) { + if (unitGroupCommandId == -1) { + unitGroupCommandId = + aiInterface->getWorld()-> + getNextCommandGroupId(); + } + + aiInterface->giveCommand(i, act_forenemy, + beingAttacked.second-> + getPos(), + unitGroupCommandId); + unitSignalledToAttack = true; + } + } else { + const AttackStoppedCommandType * + asct_forenemy = + unit->getType()-> + getFirstAttackStoppedCommand(enemy->getCurrField()); + //printf("~~~~~~~~ Unit [%s - %d] found enemy [%s - %d] asct_forenemy [%p] enemy->getCurrField() = %d\n",unit->getFullName().c_str(),unit->getId(),enemy->getFullName().c_str(),enemy->getId(),asct_forenemy,enemy->getCurrField()); + if (asct_forenemy != NULL) { + bool + shouldAttack = true; + if (unit->getType()->hasSkillClass(scHarvest)) { + shouldAttack = + (attackerWorkersHarvestingCount > + minWorkerAttackersHarvesting); + if (shouldAttack == false) { + attackerWorkersHarvestingCount++; + } + } + if (shouldAttack) { + // printf("~~~~~~~~ Unit [%s - %d] WILL AttackStoppedCommand [%s - %d]\n", unit->getFullName().c_str(), + // unit->getId(), enemy->getFullName().c_str(), enemy->getId()); + + if (unitGroupCommandId == -1) { + unitGroupCommandId = + aiInterface->getWorld()-> + getNextCommandGroupId(); + } + + aiInterface->giveCommand(i, asct_forenemy, + beingAttacked.second-> + getCenteredPos(), + unitGroupCommandId); + unitSignalledToAttack = true; + } + } + } + } + } + if (alreadyAttacking == false && act != NULL + && (ultraAttack || isWarrior) && unitSignalledToAttack == false) { + bool + shouldAttack = true; + if (unit->getType()->hasSkillClass(scHarvest)) { + shouldAttack = + (attackerWorkersHarvestingCount > + minWorkerAttackersHarvesting); + if (shouldAttack == false) { + attackerWorkersHarvestingCount++; + } + } + + // Zeta CPU does not send ( far away ) units which are currently producing something + if (aiInterface->getControlType() == ctCpuZeta + || aiInterface->getControlType() == ctNetworkCpuZeta) { + if (!isWarrior) { + if (!productionInProgress) { + shouldAttack = false; + //printf("no attack \n "); + } + } + } + if (shouldAttack) { + if (unitGroupCommandId == -1) { + unitGroupCommandId = + aiInterface->getWorld()->getNextCommandGroupId(); + } + + aiInterface->giveCommand(i, act, pos, unitGroupCommandId); + } + } + } + + if (aiInterface->getControlType() == ctCpuEasy || + aiInterface->getControlType() == ctNetworkCpuEasy) { + minWarriors += minMinWarriorsExpandCpuEasy; + } else if (aiInterface->getControlType() == ctCpuZeta || + aiInterface->getControlType() == ctNetworkCpuZeta) { + minWarriors += minMinWarriorsExpandCpuZeta; + if (minWarriors > maxMinWarriors - 1 || randomMinWarriorsReached) { + randomMinWarriorsReached = true; + minWarriors = + random.randRange(maxMinWarriors - 10, maxMinWarriors * 2); + } + } else if (minWarriors < maxMinWarriors) { + if (aiInterface->getControlType() == ctCpuUltra || + aiInterface->getControlType() == ctNetworkCpuUltra) { + minWarriors += minMinWarriorsExpandCpuUltra; + } else { + minWarriors += minMinWarriorsExpandCpuNormal; + } + } + aiInterface->printLog(2, + "Massive attack to pos: " + intToStr(pos.x) + + ", " + intToStr(pos.y) + "\n"); + } + + void + Ai::returnBase(int unitIndex) { + Vec2i + pos; + //std::pair r(crFailUndefined,""); + //aiInterface->getFactionIndex(); + pos = Vec2i(random.randRange(-villageRadius, villageRadius), + random.randRange(-villageRadius, villageRadius)) + + getRandomHomePosition(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //r= aiInterface->giveCommand(unitIndex, ccMove, pos); + aiInterface->giveCommand(unitIndex, ccMove, pos); + + //aiInterface->printLog(1, "Order return to base pos:" + intToStr(pos.x)+", "+intToStr(pos.y)+": "+rrToStr(r)+"\n"); + } + + void + Ai::harvest(int unitIndex) { + const ResourceType * + rt = getNeededResource(unitIndex); + if (rt != NULL) { + const HarvestCommandType * + hct = + aiInterface->getMyUnit(unitIndex)->getType()-> + getFirstHarvestCommand(rt, + aiInterface->getMyUnit(unitIndex)-> + getFaction()); + + Vec2i + resPos; + if (hct != NULL + && aiInterface->getNearestSightedResource(rt, + aiInterface-> + getHomeLocation(), + resPos, false)) { + resPos = + resPos + Vec2i(random.randRange(-2, 2), + random.randRange(-2, 2)); + aiInterface->giveCommand(unitIndex, hct, resPos, -1); + //aiInterface->printLog(4, "Order harvest pos:" + intToStr(resPos.x)+", "+intToStr(resPos.y)+": "+rrToStr(r)+"\n"); + } + } + } + + bool + Ai::haveBlockedUnits() { + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono.start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + + int + unitCount = aiInterface->getMyUnitCount(); + Map * + map = aiInterface->getMap(); + //If there is no close store + for (int j = 0; j < unitCount; ++j) { + const Unit * + u = aiInterface->getMyUnit(j); + const UnitType * + ut = u->getType(); + + // If this building is a store + if (u->isAlive() && ut->isMobile() && u->getPath() != NULL + && (u->getPath()->isBlocked() + || u->getPath()->getBlockCount())) { + Vec2i + unitPos = u->getPosNotThreadSafe(); + + //printf("#1 AI found blocked unit [%d - %s]\n",u->getId(),u->getFullName().c_str()); + + int + failureCount = 0; + int + cellCount = 0; + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = unitPos + Vec2i(i, j); + if (map->isInside(pos) + && map->isInsideSurface(map->toSurfCoords(pos))) { + if (pos != unitPos) { + bool + canUnitMoveToCell = + map->aproxCanMove(u, unitPos, pos); + if (canUnitMoveToCell == false) { + failureCount++; + } + cellCount++; + } + } + } + } + bool + unitImmediatelyBlocked = (failureCount == cellCount); + //printf("#1 unitImmediatelyBlocked = %d, failureCount = %d, cellCount = %d\n",unitImmediatelyBlocked,failureCount,cellCount); + + if (unitImmediatelyBlocked) { + //printf("#1 AI unit IS BLOCKED [%d - %s]\n",u->getId(),u->getFullName().c_str()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, + __LINE__, chrono.getMillis()); + return true; + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + return false; + } + + bool + Ai::getAdjacentUnits(std::map < float, std::map < int, + const Unit * > >&signalAdjacentUnits, + const Unit * unit) { + //printf("In getAdjacentUnits...\n"); + + bool + result = false; + Map * + map = aiInterface->getMap(); + Vec2i + unitPos = unit->getPosNotThreadSafe(); + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = unitPos + Vec2i(i, j); + if (map->isInside(pos) + && map->isInsideSurface(map->toSurfCoords(pos))) { + if (pos != unitPos) { + Unit * + adjacentUnit = + map->getCell(pos)->getUnit(unit->getCurrField()); + if (adjacentUnit != NULL + && adjacentUnit->getFactionIndex() == + unit->getFactionIndex()) { + if (adjacentUnit->getType()->isMobile() + && adjacentUnit->getPath() != NULL) { + //signalAdjacentUnits.push_back(adjacentUnit); + float + dist = unitPos.dist(adjacentUnit->getPos()); + + std::map < float, + std::map < int, const Unit *> >::iterator + iterFind1 = signalAdjacentUnits.find(dist); + if (iterFind1 == signalAdjacentUnits.end()) { + signalAdjacentUnits[dist][adjacentUnit-> + getId()] = + adjacentUnit; + + getAdjacentUnits(signalAdjacentUnits, + adjacentUnit); + result = true; + } else { + std::map < int, const Unit *>::iterator + iterFind2 = + iterFind1->second.find(adjacentUnit-> + getId()); + if (iterFind2 == iterFind1->second.end()) { + signalAdjacentUnits[dist][adjacentUnit-> + getId()] = + adjacentUnit; + getAdjacentUnits(signalAdjacentUnits, + adjacentUnit); + result = true; + } + } + } + } + } + } + } + } + return result; + } + + void + Ai::unblockUnits() { + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono.start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + + int + unitCount = aiInterface->getMyUnitCount(); + Map * + map = aiInterface->getMap(); + // Find blocked units and move surrounding units out of the way + std::map < float, + std::map < int, const Unit *> > + signalAdjacentUnits; + for (int idx = 0; idx < unitCount; ++idx) { + const Unit * + u = aiInterface->getMyUnit(idx); + const UnitType * + ut = u->getType(); + + // If this building is a store + if (u->isAlive() && ut->isMobile() && u->getPath() != NULL + && (u->getPath()->isBlocked() + || u->getPath()->getBlockCount())) { + Vec2i + unitPos = u->getPosNotThreadSafe(); + + //printf("#2 AI found blocked unit [%d - %s]\n",u->getId(),u->getFullName().c_str()); + + //int failureCount = 0; + //int cellCount = 0; + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = unitPos + Vec2i(i, j); + if (map->isInside(pos) + && map->isInsideSurface(map->toSurfCoords(pos))) { + if (pos != unitPos) { + bool + canUnitMoveToCell = + map->aproxCanMove(u, unitPos, pos); + if (canUnitMoveToCell == false) { + //failureCount++; + getAdjacentUnits(signalAdjacentUnits, u); + } + //cellCount++; + } + } + } + } + //bool unitImmediatelyBlocked = (failureCount == cellCount); + //printf("#2 unitImmediatelyBlocked = %d, failureCount = %d, cellCount = %d, signalAdjacentUnits.size() = %d\n",unitImmediatelyBlocked,failureCount,cellCount,signalAdjacentUnits.size()); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (signalAdjacentUnits.empty() == false) { + //printf("#2 AI units ARE BLOCKED about to unblock\n"); + + int + unitGroupCommandId = -1; + + for (std::map < float, std::map < int, + const Unit * > >::reverse_iterator iterMap = + signalAdjacentUnits.rbegin(); + iterMap != signalAdjacentUnits.rend(); ++iterMap) { + + for (std::map < int, const Unit * >::iterator iterMap2 = + iterMap->second.begin(); + iterMap2 != iterMap->second.end(); ++iterMap2) { + //int idx = iterMap2->first; + const Unit * + adjacentUnit = iterMap2->second; + if (adjacentUnit != NULL + && adjacentUnit->getType()-> + getFirstCtOfClass(ccMove) != NULL) { + const CommandType * + ct = + adjacentUnit->getType()->getFirstCtOfClass(ccMove); + + for (int moveAttempt = 1; moveAttempt <= villageRadius; + ++moveAttempt) { + Vec2i + pos = + Vec2i(random. + randRange(-villageRadius * 2, + villageRadius * 2), + random.randRange(-villageRadius * 2, + villageRadius * 2)) + + adjacentUnit->getPosNotThreadSafe(); + + bool + canUnitMoveToCell = + map->aproxCanMove(adjacentUnit, + adjacentUnit-> + getPosNotThreadSafe(), pos); + if (canUnitMoveToCell == true) { + + if (ct != NULL) { + if (unitGroupCommandId == -1) { + unitGroupCommandId = + aiInterface->getWorld()-> + getNextCommandGroupId(); + } + + //std::pair r = aiInterface->giveCommand(adjacentUnit,ct, pos, unitGroupCommandId); + aiInterface->giveCommand(adjacentUnit, ct, + pos, + unitGroupCommandId); + } + } + } + } + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + __FILE__, __FUNCTION__, __LINE__, + chrono.getMillis()); + } + + bool + Ai::outputAIBehaviourToConsole() const { + return + false; + } + + void + Ai::saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + aiNode = rootNode->addChild("Ai"); + + // AiInterface *aiInterface; + // AiRules aiRules; + // int startLoc; + aiNode-> + addAttribute("startLoc", intToStr(startLoc), mapTagReplacements); + // bool randomMinWarriorsReached; + aiNode-> + addAttribute("randomMinWarriorsReached", + intToStr(randomMinWarriorsReached), mapTagReplacements); + // Tasks tasks; + for (Tasks::const_iterator it = tasks.begin(); it != tasks.end(); + ++it) { + (*it)->saveGame(aiNode); + } + // Positions expansionPositions; + for (Positions::const_iterator it = expansionPositions.begin(); + it != expansionPositions.end(); ++it) { + XmlNode * + expansionPositionsNode = aiNode->addChild("expansionPositions"); + expansionPositionsNode->addAttribute("pos", (*it).getString(), + mapTagReplacements); + } + + // RandomGen random; + aiNode->addAttribute("random", intToStr(random.getLastNumber()), + mapTagReplacements); + // std::map factionSwitchTeamRequestCount; + + // int maxBuildRadius; + aiNode->addAttribute("maxBuildRadius", intToStr(maxBuildRadius), + mapTagReplacements); + // int minMinWarriors; + aiNode->addAttribute("minMinWarriors", intToStr(minMinWarriors), + mapTagReplacements); + // int minMinWarriorsExpandCpuEasy; + aiNode->addAttribute("minMinWarriorsExpandCpuEasy", + intToStr(minMinWarriorsExpandCpuEasy), + mapTagReplacements); + // int minMinWarriorsExpandCpuZeta; + aiNode->addAttribute("minMinWarriorsExpandCpuZeta", + intToStr(minMinWarriorsExpandCpuZeta), + mapTagReplacements); + // int minMinWarriorsExpandCpuUltra; + aiNode->addAttribute("minMinWarriorsExpandCpuUltra", + intToStr(minMinWarriorsExpandCpuUltra), + mapTagReplacements); + // int minMinWarriorsExpandCpuNormal; + aiNode->addAttribute("minMinWarriorsExpandCpuNormal", + intToStr(minMinWarriorsExpandCpuNormal), + mapTagReplacements); + // int maxMinWarriors; + aiNode->addAttribute("maxMinWarriors", intToStr(maxMinWarriors), + mapTagReplacements); + // int maxExpansions; + aiNode->addAttribute("maxExpansions", intToStr(maxExpansions), + mapTagReplacements); + // int villageRadius; + aiNode->addAttribute("villageRadius", intToStr(villageRadius), + mapTagReplacements); + // int scoutResourceRange; + aiNode->addAttribute("scoutResourceRange", + intToStr(scoutResourceRange), + mapTagReplacements); + // int minWorkerAttackersHarvesting; + aiNode->addAttribute("minWorkerAttackersHarvesting", + intToStr(minWorkerAttackersHarvesting), + mapTagReplacements); + } + + void + Ai::loadGame(const XmlNode * rootNode, Faction * faction) { + const XmlNode * + aiNode = rootNode->getChild("Ai"); + + startLoc = aiNode->getAttribute("startLoc")->getIntValue(); + randomMinWarriorsReached = + aiNode->getAttribute("randomMinWarriorsReached")->getIntValue() != + 0; + + vector < XmlNode * >taskNodeList = aiNode->getChildList("Task"); + for (unsigned int i = 0; i < taskNodeList.size(); ++i) { + XmlNode * + taskNode = taskNodeList[i]; + TaskClass + taskClass = + static_cast + (taskNode->getAttribute("taskClass")->getIntValue()); + switch (taskClass) { + case tcProduce: + { + ProduceTask * + newTask = ProduceTask::loadGame(taskNode, faction); + tasks.push_back(newTask); + } + break; + case tcBuild: + { + BuildTask * + newTask = BuildTask::loadGame(taskNode, faction); + tasks.push_back(newTask); + } + break; + case tcUpgrade: + { + UpgradeTask * + newTask = UpgradeTask::loadGame(taskNode, faction); + tasks.push_back(newTask); + } + break; + } + } + + vector < XmlNode * >expansionPositionsNodeList = + aiNode->getChildList("expansionPositions"); + for (unsigned int i = 0; i < expansionPositionsNodeList.size(); ++i) { + XmlNode * + expansionPositionsNode = expansionPositionsNodeList[i]; + Vec2i + pos = + Vec2i::strToVec2(expansionPositionsNode->getAttribute("pos")-> + getValue()); + expansionPositions.push_back(pos); + } + + // RandomGen random; + random.setLastNumber(aiNode->getAttribute("random")->getIntValue()); + // std::map factionSwitchTeamRequestCount; + + // int maxBuildRadius; + maxBuildRadius = + aiNode->getAttribute("maxBuildRadius")->getIntValue(); + // int minMinWarriors; + minMinWarriors = + aiNode->getAttribute("minMinWarriors")->getIntValue(); + // int minMinWarriorsExpandCpuEasy; + minMinWarriorsExpandCpuEasy = + aiNode->getAttribute("minMinWarriorsExpandCpuEasy")->getIntValue(); + // int minMinWarriorsExpandCpZeta; + minMinWarriorsExpandCpuZeta = + aiNode->getAttribute("minMinWarriorsExpandCpuZeta")->getIntValue(); + // int minMinWarriorsExpandCpuUltra; + minMinWarriorsExpandCpuUltra = + aiNode->getAttribute("minMinWarriorsExpandCpuUltra")->getIntValue(); + // int minMinWarriorsExpandCpuNormal; + minMinWarriorsExpandCpuNormal = + aiNode->getAttribute("minMinWarriorsExpandCpuNormal")-> + getIntValue(); + // int maxMinWarriors; + maxMinWarriors = + aiNode->getAttribute("maxMinWarriors")->getIntValue(); + // int maxExpansions; + maxExpansions = aiNode->getAttribute("maxExpansions")->getIntValue(); + // int villageRadius; + villageRadius = aiNode->getAttribute("villageRadius")->getIntValue(); + // int scoutResourceRange; + scoutResourceRange = + aiNode->getAttribute("scoutResourceRange")->getIntValue(); + // int minWorkerAttackersHarvesting; + minWorkerAttackersHarvesting = + aiNode->getAttribute("minWorkerAttackersHarvesting")->getIntValue(); + } + + } +} //end namespace diff --git a/source/glest_game/ai/ai.h b/source/glest_game/ai/ai.h index 19b108c91..a0e891f81 100644 --- a/source/glest_game/ai/ai.h +++ b/source/glest_game/ai/ai.h @@ -22,410 +22,389 @@ # include "leak_dumper.h" using - std::deque; +std::deque; using - std::vector; +std::vector; using - std::list; +std::list; using - Shared::Util::RandomGen; +Shared::Util::RandomGen; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - class - AiInterface; - class - AiRule; + class + AiInterface; + class + AiRule; -// ===================================================== -// class Task -// -/// An action that has to be performed by the IA -// ===================================================== + // ===================================================== + // class Task + // + /// An action that has to be performed by the IA + // ===================================================== - enum TaskClass - { - tcProduce, - tcBuild, - tcUpgrade - }; + enum TaskClass { + tcProduce, + tcBuild, + tcUpgrade + }; - class - Task - { - protected: - TaskClass - taskClass; + class + Task { + protected: + TaskClass + taskClass; - public: - Task (); - virtual ~ - Task () - { - } - TaskClass - getClass () const - { - return - taskClass; - } - virtual string - toString () const = 0; + public: + Task(); + virtual ~ + Task() { + } + TaskClass + getClass() const { + return + taskClass; + } + virtual string + toString() const = 0; - virtual void - saveGame (XmlNode * rootNode) const = 0; - }; + virtual void + saveGame(XmlNode * rootNode) const = 0; + }; -// ==================== ProduceTask ==================== + // ==================== ProduceTask ==================== - class - ProduceTask: - public - Task - { - private: - UnitClass - unitClass; - const UnitType * - unitType; - const ResourceType * - resourceType; + class + ProduceTask : + public + Task { + private: + UnitClass + unitClass; + const UnitType * + unitType; + const ResourceType * + resourceType; - ProduceTask (); - public: - explicit - ProduceTask (UnitClass unitClass); - explicit - ProduceTask (const UnitType * unitType); - explicit - ProduceTask (const ResourceType * resourceType); + ProduceTask(); + public: + explicit + ProduceTask(UnitClass unitClass); + explicit + ProduceTask(const UnitType * unitType); + explicit + ProduceTask(const ResourceType * resourceType); - UnitClass - getUnitClass () const - { - return - unitClass; - } - const UnitType * - getUnitType () const - { - return - unitType; - } - const ResourceType * - getResourceType () const - { - return - resourceType; - } - virtual string - toString () const; + UnitClass + getUnitClass() const { + return + unitClass; + } + const UnitType * + getUnitType() const { + return + unitType; + } + const ResourceType * + getResourceType() const { + return + resourceType; + } + virtual string + toString() const; - virtual void - saveGame (XmlNode * rootNode) const; - static ProduceTask * - loadGame (const XmlNode * rootNode, Faction * faction); - }; + virtual void + saveGame(XmlNode * rootNode) const; + static ProduceTask * + loadGame(const XmlNode * rootNode, Faction * faction); + }; -// ==================== BuildTask ==================== + // ==================== BuildTask ==================== - class - BuildTask: - public - Task - { - private: - const UnitType * - unitType; - const ResourceType * - resourceType; - bool - forcePos; - Vec2i - pos; + class + BuildTask : + public + Task { + private: + const UnitType * + unitType; + const ResourceType * + resourceType; + bool + forcePos; + Vec2i + pos; - BuildTask (); + BuildTask(); - public: - explicit - BuildTask (const UnitType * unitType); - explicit - BuildTask (const ResourceType * resourceType); - BuildTask (const UnitType * unitType, const Vec2i & pos); + public: + explicit + BuildTask(const UnitType * unitType); + explicit + BuildTask(const ResourceType * resourceType); + BuildTask(const UnitType * unitType, const Vec2i & pos); - const UnitType * - getUnitType () const - { - return - unitType; - } - const ResourceType * - getResourceType () const - { - return - resourceType; - } - bool - getForcePos () const - { - return - forcePos; - } - Vec2i - getPos () const - { - return - pos; - } - virtual string - toString () const; + const UnitType * + getUnitType() const { + return + unitType; + } + const ResourceType * + getResourceType() const { + return + resourceType; + } + bool + getForcePos() const { + return + forcePos; + } + Vec2i + getPos() const { + return + pos; + } + virtual string + toString() const; - virtual void - saveGame (XmlNode * rootNode) const; - static BuildTask * - loadGame (const XmlNode * rootNode, Faction * faction); - }; + virtual void + saveGame(XmlNode * rootNode) const; + static BuildTask * + loadGame(const XmlNode * rootNode, Faction * faction); + }; -// ==================== UpgradeTask ==================== + // ==================== UpgradeTask ==================== - class - UpgradeTask: - public - Task - { - private: - const UpgradeType * - upgradeType; + class + UpgradeTask : + public + Task { + private: + const UpgradeType * + upgradeType; - UpgradeTask (); - public: - explicit - UpgradeTask (const UpgradeType * upgradeType); - const UpgradeType * - getUpgradeType () const - { - return - upgradeType; - } - virtual string - toString () const; + UpgradeTask(); + public: + explicit + UpgradeTask(const UpgradeType * upgradeType); + const UpgradeType * + getUpgradeType() const { + return + upgradeType; + } + virtual string + toString() const; - virtual void - saveGame (XmlNode * rootNode) const; - static UpgradeTask * - loadGame (const XmlNode * rootNode, Faction * faction); - }; + virtual void + saveGame(XmlNode * rootNode) const; + static UpgradeTask * + loadGame(const XmlNode * rootNode, Faction * faction); + }; -// =============================== -// class AI -// -/// Main AI class -// =============================== + // =============================== + // class AI + // + /// Main AI class + // =============================== - class - Ai - { - private: - int - maxBuildRadius; + class + Ai { + private: + int + maxBuildRadius; - int - minMinWarriors; - int - minMinWarriorsExpandCpuEasy; - int - minMinWarriorsExpandCpuZeta; - int - minMinWarriorsExpandCpuUltra; - int - minMinWarriorsExpandCpuNormal; - int - maxMinWarriors; + int + minMinWarriors; + int + minMinWarriorsExpandCpuEasy; + int + minMinWarriorsExpandCpuZeta; + int + minMinWarriorsExpandCpuUltra; + int + minMinWarriorsExpandCpuNormal; + int + maxMinWarriors; - int - maxExpansions; - int - villageRadius; - int - scoutResourceRange; - int - minWorkerAttackersHarvesting; - int - minBuildSpacing; + int + maxExpansions; + int + villageRadius; + int + scoutResourceRange; + int + minWorkerAttackersHarvesting; + int + minBuildSpacing; - public: - enum ResourceUsage - { - ruHarvester, - ruWarrior, - ruBuilding, - ruUpgrade - }; + public: + enum ResourceUsage { + ruHarvester, + ruWarrior, + ruBuilding, + ruUpgrade + }; - private: - typedef - vector < - AiRule * > - AiRules; - typedef - list < const Task *> - Tasks; - typedef - deque < - Vec2i > - Positions; + private: + typedef + vector < + AiRule * > + AiRules; + typedef + list < const Task *> + Tasks; + typedef + deque < + Vec2i > + Positions; - private: - AiInterface * - aiInterface; - AiRules - aiRules; - int - startLoc; - bool - randomMinWarriorsReached; - Tasks - tasks; - Positions - expansionPositions; - RandomGen - random; - std::map < int, int > - factionSwitchTeamRequestCount; - int - minWarriors; + private: + AiInterface * + aiInterface; + AiRules + aiRules; + int + startLoc; + bool + randomMinWarriorsReached; + Tasks + tasks; + Positions + expansionPositions; + RandomGen + random; + std::map < int, int > + factionSwitchTeamRequestCount; + int + minWarriors; - bool - getAdjacentUnits (std::map < float, std::map < int, - const Unit * > >&signalAdjacentUnits, - const Unit * unit); + bool + getAdjacentUnits(std::map < float, std::map < int, + const Unit * > >&signalAdjacentUnits, + const Unit * unit); - public: - Ai () - { - // Defaults that used to be static which can now be overriden - maxBuildRadius = 40; - minMinWarriors = 7; - minMinWarriorsExpandCpuEasy = 1; - minMinWarriorsExpandCpuZeta = 3; - minMinWarriorsExpandCpuUltra = 3; - minMinWarriorsExpandCpuNormal = 3; - maxMinWarriors = 20; - maxExpansions = 20; - villageRadius = 15; - scoutResourceRange = 20; - minWorkerAttackersHarvesting = 3; - minBuildSpacing = 1; + public: + Ai() { + // Defaults that used to be static which can now be overriden + maxBuildRadius = 40; + minMinWarriors = 7; + minMinWarriorsExpandCpuEasy = 1; + minMinWarriorsExpandCpuZeta = 3; + minMinWarriorsExpandCpuUltra = 3; + minMinWarriorsExpandCpuNormal = 3; + maxMinWarriors = 20; + maxExpansions = 20; + villageRadius = 15; + scoutResourceRange = 20; + minWorkerAttackersHarvesting = 3; + minBuildSpacing = 1; - aiInterface = NULL; - startLoc = -1; - randomMinWarriorsReached = false; - minWarriors = 0; - } - ~ - Ai (); + aiInterface = NULL; + startLoc = -1; + randomMinWarriorsReached = false; + minWarriors = 0; + } + ~ + Ai(); - void - init (AiInterface * aiInterface, int useStartLocation = -1); - void - update (); + void + init(AiInterface * aiInterface, int useStartLocation = -1); + void + update(); - //state requests - AiInterface * - getAiInterface () const - { - return - aiInterface; - } - RandomGen * - getRandom (); - int - getCountOfType (const UnitType * ut); + //state requests + AiInterface * + getAiInterface() const { + return + aiInterface; + } + RandomGen * + getRandom(); + int + getCountOfType(const UnitType * ut); - int - getMinWarriors () const - { - return - minWarriors; - } + int + getMinWarriors() const { + return + minWarriors; + } - int - getCountOfClass (UnitClass uc, - UnitClass * additionalUnitClassToExcludeFromCount = - NULL); - float - getRatioOfClass (UnitClass uc, - UnitClass * additionalUnitClassToExcludeFromCount = - NULL); + int + getCountOfClass(UnitClass uc, + UnitClass * additionalUnitClassToExcludeFromCount = + NULL); + float + getRatioOfClass(UnitClass uc, + UnitClass * additionalUnitClassToExcludeFromCount = + NULL); - const ResourceType * - getNeededResource (int unitIndex); - bool - isStableBase (); - bool - findPosForBuilding (const UnitType * building, const Vec2i & searchPos, - Vec2i & pos); - bool - findAbleUnit (int *unitIndex, CommandClass ability, bool idleOnly); - bool - findAbleUnit (int *unitIndex, CommandClass ability, - CommandClass currentCommand); - //vector findUnitsDoingCommand(CommandClass currentCommand); - vector < int > - findUnitsHarvestingResourceType (const ResourceType * rt); + const ResourceType * + getNeededResource(int unitIndex); + bool + isStableBase(); + bool + findPosForBuilding(const UnitType * building, const Vec2i & searchPos, + Vec2i & pos); + bool + findAbleUnit(int *unitIndex, CommandClass ability, bool idleOnly); + bool + findAbleUnit(int *unitIndex, CommandClass ability, + CommandClass currentCommand); + //vector findUnitsDoingCommand(CommandClass currentCommand); + vector < int > + findUnitsHarvestingResourceType(const ResourceType * rt); - bool - beingAttacked (Vec2i & pos, Field & field, int radius); + bool + beingAttacked(Vec2i & pos, Field & field, int radius); - //tasks - void - addTask (const Task * task); - void - addPriorityTask (const Task * task); - bool - anyTask (); - const Task * - getTask () const; - void - removeTask (const Task * task); - void - retryTask (const Task * task); + //tasks + void + addTask(const Task * task); + void + addPriorityTask(const Task * task); + bool + anyTask(); + const Task * + getTask() const; + void + removeTask(const Task * task); + void + retryTask(const Task * task); - //expansions - void - addExpansion (const Vec2i & pos); - Vec2i - getRandomHomePosition (); + //expansions + void + addExpansion(const Vec2i & pos); + Vec2i + getRandomHomePosition(); - //actions - void - sendScoutPatrol (); - void - massiveAttack (const Vec2i & pos, Field field, bool ultraAttack = - false); - void - returnBase (int unitIndex); - void - harvest (int unitIndex); - bool - haveBlockedUnits (); - void - unblockUnits (); + //actions + void + sendScoutPatrol(); + void + massiveAttack(const Vec2i & pos, Field field, bool ultraAttack = + false); + void + returnBase(int unitIndex); + void + harvest(int unitIndex); + bool + haveBlockedUnits(); + void + unblockUnits(); - bool - outputAIBehaviourToConsole () const; + bool + outputAIBehaviourToConsole() const; - void - saveGame (XmlNode * rootNode) const; - void - loadGame (const XmlNode * rootNode, Faction * faction); - }; + void + saveGame(XmlNode * rootNode) const; + void + loadGame(const XmlNode * rootNode, Faction * faction); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index cea7b00d2..17c62eb4c 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -35,1616 +35,1451 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using namespace - Shared::Graphics; +Shared::Graphics; // ===================================================== // class AiInterface // ===================================================== namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// ===================================================== -// class FactionThread -// ===================================================== + // ===================================================== + // class FactionThread + // ===================================================== - AiInterfaceThread::AiInterfaceThread (AiInterface * aiIntf): - BaseThread () - { - this->masterController = NULL; - this-> - triggerIdMutex = new Mutex (CODE_AT_LINE); - this-> - aiIntf = aiIntf; - uniqueID = "AiInterfaceThread"; - } + AiInterfaceThread::AiInterfaceThread(AiInterface * aiIntf) : + BaseThread() { + this->masterController = NULL; + this-> + triggerIdMutex = new Mutex(CODE_AT_LINE); + this-> + aiIntf = aiIntf; + uniqueID = "AiInterfaceThread"; + } - AiInterfaceThread::~ - AiInterfaceThread () - { - delete - triggerIdMutex; - triggerIdMutex = NULL; - } + AiInterfaceThread::~ + AiInterfaceThread() { + delete + triggerIdMutex; + triggerIdMutex = NULL; + } - void - AiInterfaceThread::setQuitStatus (bool value) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d value = %d\n", - __FILE__, __FUNCTION__, __LINE__, value); + void + AiInterfaceThread::setQuitStatus(bool value) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d value = %d\n", + __FILE__, __FUNCTION__, __LINE__, value); - BaseThread::setQuitStatus (value); - if (value == true) - { - signal (-1); - } + BaseThread::setQuitStatus(value); + if (value == true) { + signal(-1); + } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + } - void - AiInterfaceThread::signal (int frameIndex) - { - if (frameIndex >= 0) - { - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutex (triggerIdMutex, mutexOwnerId); - this->frameIndex.first = frameIndex; - this->frameIndex.second = false; + void + AiInterfaceThread::signal(int frameIndex) { + if (frameIndex >= 0) { + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutex(triggerIdMutex, mutexOwnerId); + this->frameIndex.first = frameIndex; + this->frameIndex.second = false; - safeMutex.ReleaseLock (); - } - semTaskSignalled.signal (); - } + safeMutex.ReleaseLock(); + } + semTaskSignalled.signal(); + } - void - AiInterfaceThread::setTaskCompleted (int frameIndex) - { - if (frameIndex >= 0) - { - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutex (triggerIdMutex, mutexOwnerId); - if (this->frameIndex.first == frameIndex) - { - this->frameIndex.second = true; - } - safeMutex.ReleaseLock (); - } - } + void + AiInterfaceThread::setTaskCompleted(int frameIndex) { + if (frameIndex >= 0) { + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutex(triggerIdMutex, mutexOwnerId); + if (this->frameIndex.first == frameIndex) { + this->frameIndex.second = true; + } + safeMutex.ReleaseLock(); + } + } - bool - AiInterfaceThread::canShutdown (bool deleteSelfIfShutdownDelayed) - { - bool - ret = (getExecutingTask () == false); - if (ret == false && deleteSelfIfShutdownDelayed == true) - { - setDeleteSelfOnExecutionDone (deleteSelfIfShutdownDelayed); - deleteSelfIfRequired (); - signalQuit (); - } + bool + AiInterfaceThread::canShutdown(bool deleteSelfIfShutdownDelayed) { + bool + ret = (getExecutingTask() == false); + if (ret == false && deleteSelfIfShutdownDelayed == true) { + setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); + deleteSelfIfRequired(); + signalQuit(); + } - return ret; - } + return ret; + } - bool - AiInterfaceThread::isSignalCompleted (int frameIndex) - { - if (getRunningStatus () == false) - { - return true; - } - static string - mutexOwnerId = string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutex (triggerIdMutex, mutexOwnerId); - //bool result = (event != NULL ? event->eventCompleted : true); - bool - result = (this->frameIndex.first == frameIndex - && this->frameIndex.second == true); + bool + AiInterfaceThread::isSignalCompleted(int frameIndex) { + if (getRunningStatus() == false) { + return true; + } + static string + mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutex(triggerIdMutex, mutexOwnerId); + //bool result = (event != NULL ? event->eventCompleted : true); + bool + result = (this->frameIndex.first == frameIndex + && this->frameIndex.second == true); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] worker thread this = %p, this->frameIndex.first = %d, this->frameIndex.second = %d\n",__FILE__,__FUNCTION__,__LINE__,this,this->frameIndex.first,this->frameIndex.second); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] worker thread this = %p, this->frameIndex.first = %d, this->frameIndex.second = %d\n",__FILE__,__FUNCTION__,__LINE__,this,this->frameIndex.first,this->frameIndex.second); - safeMutex.ReleaseLock (); - return result; - } + safeMutex.ReleaseLock(); + return result; + } - void - AiInterfaceThread::signalQuit () - { - if (this->aiIntf != NULL) - { - MutexSafeWrapper - safeMutex (this->aiIntf->getMutex (), - string (__FILE__) + "_" + intToStr (__LINE__)); - this->aiIntf = NULL; - } + void + AiInterfaceThread::signalQuit() { + if (this->aiIntf != NULL) { + MutexSafeWrapper + safeMutex(this->aiIntf->getMutex(), + string(__FILE__) + "_" + intToStr(__LINE__)); + this->aiIntf = NULL; + } - BaseThread::signalQuit (); - } - void - AiInterfaceThread::execute () - { - RunningStatusSafeWrapper - runningStatus (this); - try - { - //setRunningStatus(true); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n", - __FILE__, __FUNCTION__, __LINE__, this); + BaseThread::signalQuit(); + } + void + AiInterfaceThread::execute() { + RunningStatusSafeWrapper + runningStatus(this); + try { + //setRunningStatus(true); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n", + __FILE__, __FUNCTION__, __LINE__, this); - //bool minorDebugPerformance = false; - Chrono - chrono; + //bool minorDebugPerformance = false; + Chrono + chrono; - //unsigned int idx = 0; - for (; this->aiIntf != NULL;) - { - if (getQuitStatus () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, __FUNCTION__, __LINE__); - break; - } + //unsigned int idx = 0; + for (; this->aiIntf != NULL;) { + if (getQuitStatus() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, __FUNCTION__, __LINE__); + break; + } - semTaskSignalled.waitTillSignalled (); + semTaskSignalled.waitTillSignalled(); - static string - masterSlaveOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MasterSlaveThreadControllerSafeWrapper - safeMasterController (masterController, 20000, - masterSlaveOwnerId); + static string + masterSlaveOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MasterSlaveThreadControllerSafeWrapper + safeMasterController(masterController, 20000, + masterSlaveOwnerId); - if (getQuitStatus () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, __FUNCTION__, __LINE__); - break; - } + if (getQuitStatus() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, __FUNCTION__, __LINE__); + break; + } - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutex (triggerIdMutex, mutexOwnerId); - bool - executeTask = (frameIndex.first >= 0); + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutex(triggerIdMutex, mutexOwnerId); + bool + executeTask = (frameIndex.first >= 0); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] frameIndex = %d this = %p executeTask = %d\n",__FILE__,__FUNCTION__,__LINE__,frameIndex.first, this, executeTask); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] frameIndex = %d this = %p executeTask = %d\n",__FILE__,__FUNCTION__,__LINE__,frameIndex.first, this, executeTask); - safeMutex.ReleaseLock (); + safeMutex.ReleaseLock(); - if (executeTask == true) - { - ExecutingTaskSafeWrapper - safeExecutingTaskMutex (this); + if (executeTask == true) { + ExecutingTaskSafeWrapper + safeExecutingTaskMutex(this); - MutexSafeWrapper - safeMutex (this->aiIntf->getMutex (), - string (__FILE__) + "_" + intToStr (__LINE__)); + MutexSafeWrapper + safeMutex(this->aiIntf->getMutex(), + string(__FILE__) + "_" + intToStr(__LINE__)); - this->aiIntf->update (); + this->aiIntf->update(); - safeMutex.ReleaseLock (); + safeMutex.ReleaseLock(); - setTaskCompleted (frameIndex.first); - } + setTaskCompleted(frameIndex.first); + } - if (getQuitStatus () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, __FUNCTION__, __LINE__); - break; - } - } + if (getQuitStatus() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, __FUNCTION__, __LINE__); + break; + } + } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n", - __FILE__, __FUNCTION__, __LINE__, this); - } - catch (const exception & ex) - { - //setRunningStatus(false); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n", + __FILE__, __FUNCTION__, __LINE__, this); + } catch (const exception & ex) { + //setRunningStatus(false); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - throw - megaglest_runtime_error (ex.what ()); - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - } + throw + megaglest_runtime_error(ex.what()); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + } - AiInterface::AiInterface (Game & game, int factionIndex, int teamIndex, - int useStartLocation): - fp (NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + AiInterface::AiInterface(Game & game, int factionIndex, int teamIndex, + int useStartLocation) : + fp(NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - this->aiMutex = new Mutex (CODE_AT_LINE); - this->workerThread = NULL; - this->world = game.getWorld (); - this->commander = game.getCommander (); - this->console = game.getConsole (); - this->gameSettings = game.getGameSettings (); + this->aiMutex = new Mutex(CODE_AT_LINE); + this->workerThread = NULL; + this->world = game.getWorld(); + this->commander = game.getCommander(); + this->console = game.getConsole(); + this->gameSettings = game.getGameSettings(); - this->factionIndex = factionIndex; - this->teamIndex = teamIndex; - timer = 0; + this->factionIndex = factionIndex; + this->teamIndex = teamIndex; + timer = 0; - //init ai - ai.init (this, useStartLocation); + //init ai + ai.init(this, useStartLocation); - //config - logLevel = Config::getInstance ().getInt ("AiLog"); - redir = Config::getInstance ().getBool ("AiRedir"); + //config + logLevel = Config::getInstance().getInt("AiLog"); + redir = Config::getInstance().getBool("AiRedir"); - aiLogFile = getLogFilename (); - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - aiLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - aiLogFile; - } - else - { - string - userData = Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - aiLogFile = userData + aiLogFile; - } + aiLogFile = getLogFilename(); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + aiLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + aiLogFile; + } else { + string + userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + aiLogFile = userData + aiLogFile; + } - //clear log file - if (logLevel > 0) - { + //clear log file + if (logLevel > 0) { #ifdef WIN32 - fp = - _wfopen (::Shared::Platform::utf8_decode (aiLogFile).c_str (), - L"wt"); + fp = + _wfopen(::Shared::Platform::utf8_decode(aiLogFile).c_str(), + L"wt"); #else - fp = fopen (aiLogFile.c_str (), "wt"); + fp = fopen(aiLogFile.c_str(), "wt"); #endif - if (fp == NULL) - { - throw - megaglest_runtime_error ("Can't open file: [" + aiLogFile + - "]"); - } - fprintf (fp, - "ZetaGlest AI log file for Tech [%s] Faction [%s] #%d\n\n", - this->gameSettings->getTech ().c_str (), - this->world->getFaction (this->factionIndex)->getType ()-> - getName ().c_str (), this->factionIndex); - } - - - if (Config::getInstance ().getBool ("EnableAIWorkerThreads", "true") == - true) - { - if (workerThread != NULL) - { - workerThread->signalQuit (); - if (workerThread->shutdownAndWait () == true) - { - delete - workerThread; - } - workerThread = NULL; - } - static string - mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - this->workerThread = new AiInterfaceThread (this); - this->workerThread->setUniqueID (mutexOwnerId); - this->workerThread->start (); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - AiInterface::init () - { - world = NULL;; - commander = NULL;; - console = NULL;; - gameSettings = NULL;; - timer = 0; - factionIndex = 0; - teamIndex = 0; - redir = false; - logLevel = 0; - fp = NULL;; - aiMutex = NULL; - workerThread = NULL; - } - - AiInterface::~AiInterface () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] deleting AI factionIndex = %d, teamIndex = %d\n", - __FILE__, __FUNCTION__, __LINE__, - this->factionIndex, this->teamIndex); - cacheUnitHarvestResourceLookup.clear (); - - if (workerThread != NULL) - { - workerThread->signalQuit (); - sleep (0); - if (workerThread->canShutdown (true) == true && - workerThread->shutdownAndWait () == true) - { - delete - workerThread; - } - workerThread = NULL; - } - - if (fp) - { - fclose (fp); - fp = NULL; - } - - delete - aiMutex; - aiMutex = NULL; - } - - void - AiInterface::signalWorkerThread (int frameIndex) - { - if (workerThread != NULL) - { - workerThread->signal (frameIndex); - } - else - { - this->update (); - } - } - - bool - AiInterface::isWorkerThreadSignalCompleted (int frameIndex) - { - if (workerThread != NULL) - { - return workerThread->isSignalCompleted (frameIndex); - } - return true; - } - -// ==================== main ==================== - - void - AiInterface::update () - { - timer++; - ai.update (); - } - -// ==================== misc ==================== - - bool - AiInterface::isLogLevelEnabled (int level) - { - return (this->logLevel >= level); - } - - void - AiInterface::printLog (int logLevel, const string & s) - { - if (isLogLevelEnabled (logLevel) == true) - { - string - logString = "(" + intToStr (factionIndex) + ") " + s; - - MutexSafeWrapper - safeMutex (aiMutex, string (__FILE__) + "_" + intToStr (__LINE__)); - //print log to file - if (fp != NULL) - { - fprintf (fp, "%s\n", logString.c_str ()); - } - - //redirect to console - if (redir) - { - console->addLine (logString); - } - } - } - -// ==================== interaction ==================== - - Faction * - AiInterface::getMyFaction () - { - return world->getFaction (factionIndex); - } - - bool - AiInterface::executeCommandOverNetwork () - { - bool - enableServerControlledAI = - gameSettings->getEnableServerControlledAI (); - bool - isNetworkGame = gameSettings->isNetworkGame (); - NetworkRole - role = NetworkManager::getInstance ().getNetworkRole (); - Faction * - faction = world->getFaction (factionIndex); - return faction->getCpuControl (enableServerControlledAI, isNetworkGame, - role); - } - - std::pair < CommandResult, - string > - AiInterface::giveCommandSwitchTeamVote (const Faction * faction, - SwitchTeamVote * vote) - { - assert (this->gameSettings != NULL); - - commander->trySwitchTeamVote (faction, vote); - return std::pair < CommandResult, string > (crSuccess, ""); - } - - std::pair < CommandResult, - string > AiInterface::giveCommand (int unitIndex, - CommandClass commandClass, - const Vec2i & pos) - { - assert (this->gameSettings != NULL); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - if (executeCommandOverNetwork () == true) - { - const Unit * - unit = getMyUnit (unitIndex); - result = - commander->tryGiveCommand (unit, - unit->getType ()-> - getFirstCtOfClass (commandClass), pos, - unit->getType (), - CardinalDir (CardinalDir::NORTH)); - return result; - } - else - { - Command * - c = - new Command (world->getFaction (factionIndex)-> - getUnit (unitIndex)->getType ()-> - getFirstCtOfClass (commandClass), pos); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - result = - world->getFaction (factionIndex)->getUnit (unitIndex)-> - giveCommand (c); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - return result; - } - } - - std::pair < CommandResult, - string > AiInterface::giveCommand (const Unit * unit, - const CommandType * commandType, - const Vec2i & pos, - int unitGroupCommandId) - { - assert (this->gameSettings != NULL); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unit in AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const UnitType * - unitType = unit->getType (); - if (unitType == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unittype with unit id: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - if (commandType == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] commandType == NULL, unit id: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const CommandType * - ct = unit->getType ()->findCommandTypeById (commandType->getId ()); - if (ct == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - - std::string worldLog = world->DumpWorldToLog (); - std::string sError = - "worldLog = " + worldLog + " " + string (szBuf); - throw - megaglest_runtime_error (sError); - } - - if (executeCommandOverNetwork () == true) - { - result = commander->tryGiveCommand (unit, commandType, pos, - unit->getType (), - CardinalDir (CardinalDir:: - NORTH), false, - NULL, unitGroupCommandId); - return result; - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Faction * - faction = world->getFaction (unit->getFactionIndex ()); - Unit * - unitToCommand = faction->findUnit (unit->getId ()); - Command * - cmd = new Command (commandType, pos); - cmd->setUnitCommandGroupId (unitGroupCommandId); - result = unitToCommand->giveCommand (cmd); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - return result; - } - } - - std::pair < CommandResult, - string > AiInterface::giveCommand (int unitIndex, - const CommandType * commandType, - const Vec2i & pos, - int unitGroupCommandId) - { - assert (this->gameSettings != NULL); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - const Unit * - unit = getMyUnit (unitIndex); - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const UnitType * - unitType = unit->getType (); - if (unitType == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const CommandType * - ct = unit->getType ()->findCommandTypeById (commandType->getId ()); - if (ct == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - - std::string worldLog = world->DumpWorldToLog (); - std::string sError = - "worldLog = " + worldLog + " " + string (szBuf); - throw - megaglest_runtime_error (sError); - } - - if (executeCommandOverNetwork () == true) - { - const Unit * - unit = getMyUnit (unitIndex); - result = - commander->tryGiveCommand (unit, commandType, pos, - unit->getType (), - CardinalDir (CardinalDir::NORTH)); - return result; - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Command * - cmd = new Command (commandType, pos); - cmd->setUnitCommandGroupId (unitGroupCommandId); - result = - world->getFaction (factionIndex)->getUnit (unitIndex)-> - giveCommand (cmd); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - return result; - } - } - - std::pair < CommandResult, - string > AiInterface::giveCommand (int unitIndex, - const CommandType * commandType, - const Vec2i & pos, - const UnitType * ut) - { - assert (this->gameSettings != NULL); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - const Unit * - unit = getMyUnit (unitIndex); - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const UnitType * - unitType = unit->getType (); - if (unitType == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const CommandType * - ct = unit->getType ()->findCommandTypeById (commandType->getId ()); - if (ct == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - - std::string worldLog = world->DumpWorldToLog (); - std::string sError = - "worldLog = " + worldLog + " " + string (szBuf); - throw - megaglest_runtime_error (sError); - } - - if (executeCommandOverNetwork () == true) - { - const Unit * - unit = getMyUnit (unitIndex); - result = - commander->tryGiveCommand (unit, commandType, pos, ut, - CardinalDir (CardinalDir::NORTH)); - return result; - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - result = - world->getFaction (factionIndex)->getUnit (unitIndex)-> - giveCommand (new - Command (commandType, pos, ut, - CardinalDir (CardinalDir::NORTH))); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - return result; - } - } - - std::pair < CommandResult, - string > AiInterface::giveCommand (int unitIndex, - const CommandType * commandType, - Unit * u) - { - assert (this->gameSettings != NULL); - assert (this->commander != NULL); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - const Unit * - unit = getMyUnit (unitIndex); - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const UnitType * - unitType = unit->getType (); - if (unitType == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - factionIndex); - throw - megaglest_runtime_error (szBuf); - } - const CommandType * - ct = - (commandType != - NULL ? unit->getType ()->findCommandTypeById (commandType-> - getId ()) : NULL); - if (ct == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", - __FILE__, __FUNCTION__, __LINE__, unit->getId (), - unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - - std::string worldLog = world->DumpWorldToLog (); - std::string sError = - "worldLog = " + worldLog + " " + string (szBuf); - throw - megaglest_runtime_error (sError); - } - - if (executeCommandOverNetwork () == true) - { - Unit * - targetUnit = u; - const Unit * - unit = getMyUnit (unitIndex); - - result = - commander->tryGiveCommand (unit, commandType, Vec2i (0), - unit->getType (), - CardinalDir (CardinalDir::NORTH), - false, targetUnit); - - return result; - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - result = - world->getFaction (factionIndex)->getUnit (unitIndex)-> - giveCommand (new Command (commandType, u)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - return result; - } - } - -// ==================== get data ==================== - - int - AiInterface::getMapMaxPlayers () - { - return world->getMaxPlayers (); - } - - Vec2i - AiInterface::getHomeLocation () - { - return world->getMap ()->getStartLocation (world-> - getFaction (factionIndex)-> - getStartLocationIndex ()); - } - - Vec2i - AiInterface::getStartLocation (int loactionIndex) - { - return world->getMap ()->getStartLocation (loactionIndex); - } - - int - AiInterface::getFactionCount () - { - return world->getFactionCount (); - } - - int - AiInterface::getMyUnitCount () const - { - return - world-> - getFaction (factionIndex)-> - getUnitCount (); - } - - int - AiInterface::getMyUpgradeCount () const - { - return - world-> - getFaction (factionIndex)-> - getUpgradeManager ()-> - getUpgradeCount (); - } - -//int AiInterface::onSightUnitCount() { -// int count=0; -// Map *map= world->getMap(); -// for(int i=0; igetFactionCount(); ++i) { -// for(int j=0; jgetFaction(i)->getUnitCount(); ++j) { -// Unit *unit = world->getFaction(i)->getUnit(j); -// SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); -// bool cannotSeeUnit = (unit->getType()->hasCellMap() == true && -// unit->getType()->getAllowEmptyCellMap() == true && -// unit->getType()->hasEmptyCellMap() == true); -// if(sc->isVisible(teamIndex) && cannotSeeUnit == false) { -// count++; -// } -// } -// } -// return count; -//} - - const Resource * - AiInterface::getResource (const ResourceType * rt) - { - return world->getFaction (factionIndex)->getResource (rt); - } - - Unit * - AiInterface::getMyUnitPtr (int unitIndex) - { - if (unitIndex < 0 - || unitIndex >= world->getFaction (factionIndex)->getUnitCount ()) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] unitIndex >= world->getFaction(factionIndex)->getUnitCount(), unitIndex = %d, world->getFaction(factionIndex)->getUnitCount() = %d", - __FILE__, __FUNCTION__, __LINE__, unitIndex, - world->getFaction (factionIndex)->getUnitCount ()); - throw - megaglest_runtime_error (szBuf); - } - - return world->getFaction (factionIndex)->getUnit (unitIndex); - } - - const Unit * - AiInterface::getMyUnit (int unitIndex) - { - return getMyUnitPtr (unitIndex); - } - -//const Unit *AiInterface::getOnSightUnit(int unitIndex) { -// -// int count=0; -// Map *map= world->getMap(); -// -// for(int i=0; igetFactionCount(); ++i) { -// for(int j=0; jgetFaction(i)->getUnitCount(); ++j) { -// Unit * unit= world->getFaction(i)->getUnit(j); -// SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); -// bool cannotSeeUnit = (unit->getType()->hasCellMap() == true && -// unit->getType()->getAllowEmptyCellMap() == true && -// unit->getType()->hasEmptyCellMap() == true); -// -// if(sc->isVisible(teamIndex) && cannotSeeUnit == false) { -// if(count==unitIndex) { -// return unit; -// } -// else { -// count ++; -// } -// } -// } -// } -// return NULL; -//} - - const FactionType * - AiInterface::getMyFactionType () - { - return world->getFaction (factionIndex)->getType (); - } - - const ControlType - AiInterface::getControlType () - { - return world->getFaction (factionIndex)->getControlType (); - } - - const TechTree * - AiInterface::getTechTree () - { - return world->getTechTree (); - } - - - bool - AiInterface::isResourceInRegion (const Vec2i & pos, - const ResourceType * rt, - Vec2i & resourcePos, int range) const - { - const Map * - map = world->getMap (); - - int - xi = 1; - int - xj = 1; - - if (rand () % 2 == 1) - { - xi = -1; - } - if (rand () % 2 == 1) - { - xj = -1; - } - for (int i = -range; i <= range; ++i) - { - for (int j = -range; j <= range; ++j) - { - int - ii = xi * i; - int - jj = xj * j; - if (map->isInside (pos.x + ii, pos.y + jj)) - { - Resource * - r = - map->getSurfaceCell (map-> - toSurfCoords (Vec2i - (pos.x + ii, - pos.y + - jj)))->getResource (); - if (r != NULL) - { - if (r->getType () == rt) - { - resourcePos = pos + Vec2i (ii, jj); - return true; - } - } - } - } - } - return false; - } - - - -//returns if there is a resource next to a unit, in "resourcePos" is stored the relative position of the resource - bool - AiInterface::isResourceNear (const Vec2i & pos, const ResourceType * rt, - Vec2i & resourcePos, Faction * faction, - bool fallbackToPeersHarvestingSameResource) - const - { - const Map * - map = world->getMap (); - int - size = 1; - for (int i = -1; i <= size; ++i) - { - for (int j = -1; j <= size; ++j) - { - if (map->isInside (pos.x + i, pos.y + j)) - { - Resource * - r = - map->getSurfaceCell (map-> - toSurfCoords (Vec2i - (pos.x + i, - pos.y + - j)))->getResource (); - if (r != NULL) - { - if (r->getType () == rt) - { - resourcePos = pos + Vec2i (i, j); - - return - true; - } - } - } - } - } - - if (fallbackToPeersHarvestingSameResource == true && faction != NULL) - { - // Look for another unit that is currently harvesting the same resource - // type right now - - // Check the faction cache for a known position where we can harvest - // this resource type - Vec2i - result = faction->getClosestResourceTypeTargetFromCache (pos, rt); - if (result.x >= 0) - { - resourcePos = result; - - if (pos.dist (resourcePos) <= size) - { - return true; - } - } - } - - return false; - } - - bool - AiInterface::getNearestSightedResource (const ResourceType * rt, - const Vec2i & pos, - Vec2i & resultPos, - bool usableResourceTypeOnly) - { - Faction * - faction = world->getFaction (factionIndex); - //float tmpDist=0; - float - nearestDist = infinity; - bool - anyResource = false; - resultPos.x = -1; - resultPos.y = -1; - - bool - canUseResourceType = (usableResourceTypeOnly == false); - if (usableResourceTypeOnly == true) - { - // can any unit harvest this resource yet? - std::map < const ResourceType *, int >::iterator - iterFind = cacheUnitHarvestResourceLookup.find (rt); - - if (iterFind != cacheUnitHarvestResourceLookup.end () && - faction->findUnit (iterFind->second) != NULL) - { - canUseResourceType = true; - } - else - { - int - unitCount = getMyUnitCount (); - for (int i = 0; i < unitCount; ++i) - { - const Unit * - unit = getMyUnit (i); - const HarvestCommandType * - hct = - unit->getType ()->getFirstHarvestCommand (rt, - unit-> - getFaction ()); - if (hct != NULL) - { - canUseResourceType = true; - cacheUnitHarvestResourceLookup[rt] = unit->getId (); - break; - } - } - } - } - - if (canUseResourceType == true) - { - bool - isResourceClose = - isResourceNear (pos, rt, resultPos, faction, true); - - // Found a resource - if (isResourceClose == true || resultPos.x >= 0) - { - anyResource = true; - } - else - { - const Map * - map = world->getMap (); - for (int i = 0; i < map->getW (); ++i) - { - for (int j = 0; j < map->getH (); ++j) - { - Vec2i - resPos = Vec2i (i, j); - Vec2i - surfPos = Map::toSurfCoords (resPos); - SurfaceCell * - sc = map->getSurfaceCell (surfPos); - - //if explored cell - if (sc != NULL && sc->isExplored (teamIndex)) - { - Resource * - r = sc->getResource (); - - //if resource cell - if (r != NULL) - { - if (r->getType () == rt) - { - float - tmpDist = pos.dist (resPos); - if (tmpDist < nearestDist) - { - anyResource = true; - nearestDist = tmpDist; - resultPos = resPos; - } - } - } - } - } - } - } - } - return anyResource; - } - - bool - AiInterface::isAlly (const Unit * unit) const - { - return - world-> - getFaction (factionIndex)-> - isAlly (unit->getFaction ()); - } - bool - AiInterface::reqsOk (const RequirableType * rt) - { - return world->getFaction (factionIndex)->reqsOk (rt); - } - - bool - AiInterface::reqsOk (const CommandType * ct) - { - return world->getFaction (factionIndex)->reqsOk (ct); - } - - bool - AiInterface::checkCosts (const ProducibleType * pt, - const CommandType * ct) - { - return world->getFaction (factionIndex)->checkCosts (pt, ct); - } - - bool - AiInterface::isFreeCells (const Vec2i & pos, int size, Field field) - { - return world->getMap ()->isFreeCells (pos, size, field); - } - - void - AiInterface::removeEnemyWarningPositionFromList (Vec2i & checkPos) - { - for (int i = (int)enemyWarningPositionList.size () - 1; i >= 0; --i) - { - Vec2i & pos = enemyWarningPositionList[i]; - - if (checkPos == pos) - { - enemyWarningPositionList.erase (enemyWarningPositionList. - begin () + i); - break; - } - } - } - - const Unit * - AiInterface::getFirstOnSightEnemyUnit (Vec2i & pos, Field & field, - int radius) - { - Map * - map = world->getMap (); - - const int - CHECK_RADIUS = 12; - const int - WARNING_ENEMY_COUNT = 6; - - for (int i = 0; i < world->getFactionCount (); ++i) - { - for (int j = 0; j < world->getFaction (i)->getUnitCount (); ++j) - { - Unit * - unit = world->getFaction (i)->getUnit (j); - SurfaceCell * - sc = - map->getSurfaceCell (Map::toSurfCoords (unit->getPos ())); - bool - cannotSeeUnit = (unit->getType ()->hasCellMap () == true && - unit->getType ()->getAllowEmptyCellMap () == - true - && unit->getType ()->hasEmptyCellMap () == - true); - - if (sc->isVisible (teamIndex) && cannotSeeUnit == false && - isAlly (unit) == false && unit->isAlive () == true) - { - pos = unit->getPos (); - field = unit->getCurrField (); - if (pos.dist (getHomeLocation ()) < radius) - { - printLog (2, - "Being attacked at pos " + intToStr (pos.x) + - "," + intToStr (pos.y) + "\n"); - - // Now check if there are more than x enemies in sight and if - // so make note of the position - int - foundEnemies = 0; - std::map < int, - bool > - foundEnemyList; - for (int aiX = pos.x - CHECK_RADIUS; - aiX < pos.x + CHECK_RADIUS; ++aiX) - { - for (int aiY = pos.y - CHECK_RADIUS; - aiY < pos.y + CHECK_RADIUS; ++aiY) - { - Vec2i - checkPos (aiX, aiY); - if (map->isInside (checkPos) - && map->isInsideSurface (map-> - toSurfCoords - (checkPos))) - { - Cell * - cAI = map->getCell (checkPos); - SurfaceCell * - scAI = - map-> - getSurfaceCell (Map:: - toSurfCoords (checkPos)); - if (scAI != NULL && cAI != NULL - && cAI->getUnit (field) != NULL - && sc->isVisible (teamIndex)) - { - const Unit * - checkUnit = cAI->getUnit (field); - if (foundEnemyList. - find (checkUnit->getId ()) == - foundEnemyList.end ()) - { - bool - cannotSeeUnitAI = - (checkUnit->getType ()-> - hasCellMap () == true - && checkUnit->getType ()-> - getAllowEmptyCellMap () == true - && checkUnit->getType ()-> - hasEmptyCellMap () == true); - if (cannotSeeUnitAI == false - && isAlly (checkUnit) == false - && checkUnit->isAlive () == - true) - { - foundEnemies++; - foundEnemyList[checkUnit-> - getId ()] = true; - } - } - } - } - } - } - if (foundEnemies >= WARNING_ENEMY_COUNT) - { - if (std:: - find (enemyWarningPositionList.begin (), - enemyWarningPositionList.end (), - pos) == enemyWarningPositionList.end ()) - { - enemyWarningPositionList.push_back (pos); - } - } - return unit; - } - } - } - } - return NULL; - } - - Map * - AiInterface::getMap () - { - Map * - map = world->getMap (); - return map; - } - - bool - AiInterface::factionUsesResourceType (const FactionType * factionType, - const ResourceType * rt) - { - bool - factionUsesResourceType = factionType->factionUsesResourceType (rt); - return factionUsesResourceType; - } - - void - AiInterface::saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - aiInterfaceNode = rootNode->addChild ("AiInterface"); - -// World *world; -// Commander *commander; -// Console *console; -// GameSettings *gameSettings; -// -// Ai ai; - ai. - saveGame (aiInterfaceNode); -// int timer; - aiInterfaceNode-> - addAttribute ("timer", intToStr (timer), mapTagReplacements); -// int factionIndex; - aiInterfaceNode-> - addAttribute ("factionIndex", intToStr (factionIndex), - mapTagReplacements); -// int teamIndex; - aiInterfaceNode-> - addAttribute ("teamIndex", intToStr (teamIndex), mapTagReplacements); -// //config -// bool redir; - aiInterfaceNode-> - addAttribute ("redir", intToStr (redir), mapTagReplacements); -// int logLevel; - aiInterfaceNode-> - addAttribute ("logLevel", intToStr (logLevel), mapTagReplacements); -// std::map cacheUnitHarvestResourceLookup; - for (std::map < const ResourceType *, int >::const_iterator iterMap = - cacheUnitHarvestResourceLookup.begin (); - iterMap != cacheUnitHarvestResourceLookup.end (); ++iterMap) - { - XmlNode * - cacheUnitHarvestResourceLookupNode = - aiInterfaceNode->addChild ("cacheUnitHarvestResourceLookup"); - - cacheUnitHarvestResourceLookupNode-> - addAttribute ("key", iterMap->first->getName (), - mapTagReplacements); - cacheUnitHarvestResourceLookupNode-> - addAttribute ("value", intToStr (iterMap->second), - mapTagReplacements); - } - } - -// AiInterface::AiInterface(Game &game, int factionIndex, int teamIndex, int useStartLocation) { - void - AiInterface::loadGame (const XmlNode * rootNode, Faction * faction) - { - XmlNode * - aiInterfaceNode = NULL; - vector < XmlNode * >aiInterfaceNodeList = - rootNode->getChildList ("AiInterface"); - for (unsigned int i = 0; i < aiInterfaceNodeList.size (); ++i) - { - XmlNode * - node = aiInterfaceNodeList[i]; - if (node->getAttribute ("factionIndex")->getIntValue () == - faction->getIndex ()) - { - aiInterfaceNode = node; - break; - } - } - - if (aiInterfaceNode != NULL) - { - factionIndex = - aiInterfaceNode->getAttribute ("factionIndex")->getIntValue (); - teamIndex = - aiInterfaceNode->getAttribute ("teamIndex")->getIntValue (); - - ai.loadGame (aiInterfaceNode, faction); - //firstTime = timeflowNode->getAttribute("firstTime")->getFloatValue(); - - timer = aiInterfaceNode->getAttribute ("timer")->getIntValue (); - // int factionIndex; - //factionIndex = aiInterfaceNode->getAttribute("factionIndex")->getIntValue(); - // int teamIndex; - //teamIndex = aiInterfaceNode->getAttribute("teamIndex")->getIntValue(); - // //config - // bool redir; - redir = - aiInterfaceNode->getAttribute ("redir")->getIntValue () != 0; - // int logLevel; - logLevel = - aiInterfaceNode->getAttribute ("logLevel")->getIntValue (); - - // std::map cacheUnitHarvestResourceLookup; - // for(std::map::const_iterator iterMap = cacheUnitHarvestResourceLookup.begin(); - // iterMap != cacheUnitHarvestResourceLookup.end(); ++iterMap) { - // XmlNode *cacheUnitHarvestResourceLookupNode = aiInterfaceNode->addChild("cacheUnitHarvestResourceLookup"); - // - // cacheUnitHarvestResourceLookupNode->addAttribute("key",iterMap->first->getName(), mapTagReplacements); - // cacheUnitHarvestResourceLookupNode->addAttribute("value",intToStr(iterMap->second), mapTagReplacements); - // } - } - } - - } + if (fp == NULL) { + throw + megaglest_runtime_error("Can't open file: [" + aiLogFile + + "]"); + } + fprintf(fp, + "ZetaGlest AI log file for Tech [%s] Faction [%s] #%d\n\n", + this->gameSettings->getTech().c_str(), + this->world->getFaction(this->factionIndex)->getType()-> + getName().c_str(), this->factionIndex); + } + + + if (Config::getInstance().getBool("EnableAIWorkerThreads", "true") == + true) { + if (workerThread != NULL) { + workerThread->signalQuit(); + if (workerThread->shutdownAndWait() == true) { + delete + workerThread; + } + workerThread = NULL; + } + static string + mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + this->workerThread = new AiInterfaceThread(this); + this->workerThread->setUniqueID(mutexOwnerId); + this->workerThread->start(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + AiInterface::init() { + world = NULL;; + commander = NULL;; + console = NULL;; + gameSettings = NULL;; + timer = 0; + factionIndex = 0; + teamIndex = 0; + redir = false; + logLevel = 0; + fp = NULL;; + aiMutex = NULL; + workerThread = NULL; + } + + AiInterface::~AiInterface() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] deleting AI factionIndex = %d, teamIndex = %d\n", + __FILE__, __FUNCTION__, __LINE__, + this->factionIndex, this->teamIndex); + cacheUnitHarvestResourceLookup.clear(); + + if (workerThread != NULL) { + workerThread->signalQuit(); + sleep(0); + if (workerThread->canShutdown(true) == true && + workerThread->shutdownAndWait() == true) { + delete + workerThread; + } + workerThread = NULL; + } + + if (fp) { + fclose(fp); + fp = NULL; + } + + delete + aiMutex; + aiMutex = NULL; + } + + void + AiInterface::signalWorkerThread(int frameIndex) { + if (workerThread != NULL) { + workerThread->signal(frameIndex); + } else { + this->update(); + } + } + + bool + AiInterface::isWorkerThreadSignalCompleted(int frameIndex) { + if (workerThread != NULL) { + return workerThread->isSignalCompleted(frameIndex); + } + return true; + } + + // ==================== main ==================== + + void + AiInterface::update() { + timer++; + ai.update(); + } + + // ==================== misc ==================== + + bool + AiInterface::isLogLevelEnabled(int level) { + return (this->logLevel >= level); + } + + void + AiInterface::printLog(int logLevel, const string & s) { + if (isLogLevelEnabled(logLevel) == true) { + string + logString = "(" + intToStr(factionIndex) + ") " + s; + + MutexSafeWrapper + safeMutex(aiMutex, string(__FILE__) + "_" + intToStr(__LINE__)); + //print log to file + if (fp != NULL) { + fprintf(fp, "%s\n", logString.c_str()); + } + + //redirect to console + if (redir) { + console->addLine(logString); + } + } + } + + // ==================== interaction ==================== + + Faction * + AiInterface::getMyFaction() { + return world->getFaction(factionIndex); + } + + bool + AiInterface::executeCommandOverNetwork() { + bool + enableServerControlledAI = + gameSettings->getEnableServerControlledAI(); + bool + isNetworkGame = gameSettings->isNetworkGame(); + NetworkRole + role = NetworkManager::getInstance().getNetworkRole(); + Faction * + faction = world->getFaction(factionIndex); + return faction->getCpuControl(enableServerControlledAI, isNetworkGame, + role); + } + + std::pair < CommandResult, + string > + AiInterface::giveCommandSwitchTeamVote(const Faction * faction, + SwitchTeamVote * vote) { + assert(this->gameSettings != NULL); + + commander->trySwitchTeamVote(faction, vote); + return std::pair < CommandResult, string >(crSuccess, ""); + } + + std::pair < CommandResult, + string > AiInterface::giveCommand(int unitIndex, + CommandClass commandClass, + const Vec2i & pos) { + assert(this->gameSettings != NULL); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + if (executeCommandOverNetwork() == true) { + const Unit * + unit = getMyUnit(unitIndex); + result = + commander->tryGiveCommand(unit, + unit->getType()-> + getFirstCtOfClass(commandClass), pos, + unit->getType(), + CardinalDir(CardinalDir::NORTH)); + return result; + } else { + Command * + c = + new Command(world->getFaction(factionIndex)-> + getUnit(unitIndex)->getType()-> + getFirstCtOfClass(commandClass), pos); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + result = + world->getFaction(factionIndex)->getUnit(unitIndex)-> + giveCommand(c); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + return result; + } + } + + std::pair < CommandResult, + string > AiInterface::giveCommand(const Unit * unit, + const CommandType * commandType, + const Vec2i & pos, + int unitGroupCommandId) { + assert(this->gameSettings != NULL); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unit in AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const UnitType * + unitType = unit->getType(); + if (unitType == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unittype with unit id: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + if (commandType == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] commandType == NULL, unit id: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const CommandType * + ct = unit->getType()->findCommandTypeById(commandType->getId()); + if (ct == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = + "worldLog = " + worldLog + " " + string(szBuf); + throw + megaglest_runtime_error(sError); + } + + if (executeCommandOverNetwork() == true) { + result = commander->tryGiveCommand(unit, commandType, pos, + unit->getType(), + CardinalDir(CardinalDir:: + NORTH), false, + NULL, unitGroupCommandId); + return result; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Faction * + faction = world->getFaction(unit->getFactionIndex()); + Unit * + unitToCommand = faction->findUnit(unit->getId()); + Command * + cmd = new Command(commandType, pos); + cmd->setUnitCommandGroupId(unitGroupCommandId); + result = unitToCommand->giveCommand(cmd); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + return result; + } + } + + std::pair < CommandResult, + string > AiInterface::giveCommand(int unitIndex, + const CommandType * commandType, + const Vec2i & pos, + int unitGroupCommandId) { + assert(this->gameSettings != NULL); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + const Unit * + unit = getMyUnit(unitIndex); + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const UnitType * + unitType = unit->getType(); + if (unitType == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const CommandType * + ct = unit->getType()->findCommandTypeById(commandType->getId()); + if (ct == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = + "worldLog = " + worldLog + " " + string(szBuf); + throw + megaglest_runtime_error(sError); + } + + if (executeCommandOverNetwork() == true) { + const Unit * + unit = getMyUnit(unitIndex); + result = + commander->tryGiveCommand(unit, commandType, pos, + unit->getType(), + CardinalDir(CardinalDir::NORTH)); + return result; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Command * + cmd = new Command(commandType, pos); + cmd->setUnitCommandGroupId(unitGroupCommandId); + result = + world->getFaction(factionIndex)->getUnit(unitIndex)-> + giveCommand(cmd); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + return result; + } + } + + std::pair < CommandResult, + string > AiInterface::giveCommand(int unitIndex, + const CommandType * commandType, + const Vec2i & pos, + const UnitType * ut) { + assert(this->gameSettings != NULL); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + const Unit * + unit = getMyUnit(unitIndex); + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const UnitType * + unitType = unit->getType(); + if (unitType == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const CommandType * + ct = unit->getType()->findCommandTypeById(commandType->getId()); + if (ct == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = + "worldLog = " + worldLog + " " + string(szBuf); + throw + megaglest_runtime_error(sError); + } + + if (executeCommandOverNetwork() == true) { + const Unit * + unit = getMyUnit(unitIndex); + result = + commander->tryGiveCommand(unit, commandType, pos, ut, + CardinalDir(CardinalDir::NORTH)); + return result; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + result = + world->getFaction(factionIndex)->getUnit(unitIndex)-> + giveCommand(new + Command(commandType, pos, ut, + CardinalDir(CardinalDir::NORTH))); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + return result; + } + } + + std::pair < CommandResult, + string > AiInterface::giveCommand(int unitIndex, + const CommandType * commandType, + Unit * u) { + assert(this->gameSettings != NULL); + assert(this->commander != NULL); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + const Unit * + unit = getMyUnit(unitIndex); + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unit with index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const UnitType * + unitType = unit->getType(); + if (unitType == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find AI unittype with unit index: %d, AI factionIndex = %d. Game out of synch.", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + factionIndex); + throw + megaglest_runtime_error(szBuf); + } + const CommandType * + ct = + (commandType != + NULL ? unit->getType()->findCommandTypeById(commandType-> + getId()) : NULL); + if (ct == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nCan not find AI command type for:\nunit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__, __FUNCTION__, __LINE__, unit->getId(), + unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + + std::string worldLog = world->DumpWorldToLog(); + std::string sError = + "worldLog = " + worldLog + " " + string(szBuf); + throw + megaglest_runtime_error(sError); + } + + if (executeCommandOverNetwork() == true) { + Unit * + targetUnit = u; + const Unit * + unit = getMyUnit(unitIndex); + + result = + commander->tryGiveCommand(unit, commandType, Vec2i(0), + unit->getType(), + CardinalDir(CardinalDir::NORTH), + false, targetUnit); + + return result; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + result = + world->getFaction(factionIndex)->getUnit(unitIndex)-> + giveCommand(new Command(commandType, u)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + return result; + } + } + + // ==================== get data ==================== + + int + AiInterface::getMapMaxPlayers() { + return world->getMaxPlayers(); + } + + Vec2i + AiInterface::getHomeLocation() { + return world->getMap()->getStartLocation(world-> + getFaction(factionIndex)-> + getStartLocationIndex()); + } + + Vec2i + AiInterface::getStartLocation(int loactionIndex) { + return world->getMap()->getStartLocation(loactionIndex); + } + + int + AiInterface::getFactionCount() { + return world->getFactionCount(); + } + + int + AiInterface::getMyUnitCount() const { + return + world-> + getFaction(factionIndex)-> + getUnitCount(); + } + + int + AiInterface::getMyUpgradeCount() const { + return + world-> + getFaction(factionIndex)-> + getUpgradeManager()-> + getUpgradeCount(); + } + + //int AiInterface::onSightUnitCount() { + // int count=0; + // Map *map= world->getMap(); + // for(int i=0; igetFactionCount(); ++i) { + // for(int j=0; jgetFaction(i)->getUnitCount(); ++j) { + // Unit *unit = world->getFaction(i)->getUnit(j); + // SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); + // bool cannotSeeUnit = (unit->getType()->hasCellMap() == true && + // unit->getType()->getAllowEmptyCellMap() == true && + // unit->getType()->hasEmptyCellMap() == true); + // if(sc->isVisible(teamIndex) && cannotSeeUnit == false) { + // count++; + // } + // } + // } + // return count; + //} + + const Resource * + AiInterface::getResource(const ResourceType * rt) { + return world->getFaction(factionIndex)->getResource(rt); + } + + Unit * + AiInterface::getMyUnitPtr(int unitIndex) { + if (unitIndex < 0 + || unitIndex >= world->getFaction(factionIndex)->getUnitCount()) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] unitIndex >= world->getFaction(factionIndex)->getUnitCount(), unitIndex = %d, world->getFaction(factionIndex)->getUnitCount() = %d", + __FILE__, __FUNCTION__, __LINE__, unitIndex, + world->getFaction(factionIndex)->getUnitCount()); + throw + megaglest_runtime_error(szBuf); + } + + return world->getFaction(factionIndex)->getUnit(unitIndex); + } + + const Unit * + AiInterface::getMyUnit(int unitIndex) { + return getMyUnitPtr(unitIndex); + } + + //const Unit *AiInterface::getOnSightUnit(int unitIndex) { + // + // int count=0; + // Map *map= world->getMap(); + // + // for(int i=0; igetFactionCount(); ++i) { + // for(int j=0; jgetFaction(i)->getUnitCount(); ++j) { + // Unit * unit= world->getFaction(i)->getUnit(j); + // SurfaceCell *sc= map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); + // bool cannotSeeUnit = (unit->getType()->hasCellMap() == true && + // unit->getType()->getAllowEmptyCellMap() == true && + // unit->getType()->hasEmptyCellMap() == true); + // + // if(sc->isVisible(teamIndex) && cannotSeeUnit == false) { + // if(count==unitIndex) { + // return unit; + // } + // else { + // count ++; + // } + // } + // } + // } + // return NULL; + //} + + const FactionType * + AiInterface::getMyFactionType() { + return world->getFaction(factionIndex)->getType(); + } + + const ControlType + AiInterface::getControlType() { + return world->getFaction(factionIndex)->getControlType(); + } + + const TechTree * + AiInterface::getTechTree() { + return world->getTechTree(); + } + + + bool + AiInterface::isResourceInRegion(const Vec2i & pos, + const ResourceType * rt, + Vec2i & resourcePos, int range) const { + const Map * + map = world->getMap(); + + int + xi = 1; + int + xj = 1; + + if (rand() % 2 == 1) { + xi = -1; + } + if (rand() % 2 == 1) { + xj = -1; + } + for (int i = -range; i <= range; ++i) { + for (int j = -range; j <= range; ++j) { + int + ii = xi * i; + int + jj = xj * j; + if (map->isInside(pos.x + ii, pos.y + jj)) { + Resource * + r = + map->getSurfaceCell(map-> + toSurfCoords(Vec2i + (pos.x + ii, + pos.y + + jj)))->getResource(); + if (r != NULL) { + if (r->getType() == rt) { + resourcePos = pos + Vec2i(ii, jj); + return true; + } + } + } + } + } + return false; + } + + + + //returns if there is a resource next to a unit, in "resourcePos" is stored the relative position of the resource + bool + AiInterface::isResourceNear(const Vec2i & pos, const ResourceType * rt, + Vec2i & resourcePos, Faction * faction, + bool fallbackToPeersHarvestingSameResource) + const { + const Map * + map = world->getMap(); + int + size = 1; + for (int i = -1; i <= size; ++i) { + for (int j = -1; j <= size; ++j) { + if (map->isInside(pos.x + i, pos.y + j)) { + Resource * + r = + map->getSurfaceCell(map-> + toSurfCoords(Vec2i + (pos.x + i, + pos.y + + j)))->getResource(); + if (r != NULL) { + if (r->getType() == rt) { + resourcePos = pos + Vec2i(i, j); + + return + true; + } + } + } + } + } + + if (fallbackToPeersHarvestingSameResource == true && faction != NULL) { + // Look for another unit that is currently harvesting the same resource + // type right now + + // Check the faction cache for a known position where we can harvest + // this resource type + Vec2i + result = faction->getClosestResourceTypeTargetFromCache(pos, rt); + if (result.x >= 0) { + resourcePos = result; + + if (pos.dist(resourcePos) <= size) { + return true; + } + } + } + + return false; + } + + bool + AiInterface::getNearestSightedResource(const ResourceType * rt, + const Vec2i & pos, + Vec2i & resultPos, + bool usableResourceTypeOnly) { + Faction * + faction = world->getFaction(factionIndex); + //float tmpDist=0; + float + nearestDist = infinity; + bool + anyResource = false; + resultPos.x = -1; + resultPos.y = -1; + + bool + canUseResourceType = (usableResourceTypeOnly == false); + if (usableResourceTypeOnly == true) { + // can any unit harvest this resource yet? + std::map < const ResourceType *, int >::iterator + iterFind = cacheUnitHarvestResourceLookup.find(rt); + + if (iterFind != cacheUnitHarvestResourceLookup.end() && + faction->findUnit(iterFind->second) != NULL) { + canUseResourceType = true; + } else { + int + unitCount = getMyUnitCount(); + for (int i = 0; i < unitCount; ++i) { + const Unit * + unit = getMyUnit(i); + const HarvestCommandType * + hct = + unit->getType()->getFirstHarvestCommand(rt, + unit-> + getFaction()); + if (hct != NULL) { + canUseResourceType = true; + cacheUnitHarvestResourceLookup[rt] = unit->getId(); + break; + } + } + } + } + + if (canUseResourceType == true) { + bool + isResourceClose = + isResourceNear(pos, rt, resultPos, faction, true); + + // Found a resource + if (isResourceClose == true || resultPos.x >= 0) { + anyResource = true; + } else { + const Map * + map = world->getMap(); + for (int i = 0; i < map->getW(); ++i) { + for (int j = 0; j < map->getH(); ++j) { + Vec2i + resPos = Vec2i(i, j); + Vec2i + surfPos = Map::toSurfCoords(resPos); + SurfaceCell * + sc = map->getSurfaceCell(surfPos); + + //if explored cell + if (sc != NULL && sc->isExplored(teamIndex)) { + Resource * + r = sc->getResource(); + + //if resource cell + if (r != NULL) { + if (r->getType() == rt) { + float + tmpDist = pos.dist(resPos); + if (tmpDist < nearestDist) { + anyResource = true; + nearestDist = tmpDist; + resultPos = resPos; + } + } + } + } + } + } + } + } + return anyResource; + } + + bool + AiInterface::isAlly(const Unit * unit) const { + return + world-> + getFaction(factionIndex)-> + isAlly(unit->getFaction()); + } + bool + AiInterface::reqsOk(const RequirableType * rt) { + return world->getFaction(factionIndex)->reqsOk(rt); + } + + bool + AiInterface::reqsOk(const CommandType * ct) { + return world->getFaction(factionIndex)->reqsOk(ct); + } + + bool + AiInterface::checkCosts(const ProducibleType * pt, + const CommandType * ct) { + return world->getFaction(factionIndex)->checkCosts(pt, ct); + } + + bool + AiInterface::isFreeCells(const Vec2i & pos, int size, Field field) { + return world->getMap()->isFreeCells(pos, size, field); + } + + void + AiInterface::removeEnemyWarningPositionFromList(Vec2i & checkPos) { + for (int i = (int) enemyWarningPositionList.size() - 1; i >= 0; --i) { + Vec2i & pos = enemyWarningPositionList[i]; + + if (checkPos == pos) { + enemyWarningPositionList.erase(enemyWarningPositionList. + begin() + i); + break; + } + } + } + + const Unit * + AiInterface::getFirstOnSightEnemyUnit(Vec2i & pos, Field & field, + int radius) { + Map * + map = world->getMap(); + + const int + CHECK_RADIUS = 12; + const int + WARNING_ENEMY_COUNT = 6; + + for (int i = 0; i < world->getFactionCount(); ++i) { + for (int j = 0; j < world->getFaction(i)->getUnitCount(); ++j) { + Unit * + unit = world->getFaction(i)->getUnit(j); + SurfaceCell * + sc = + map->getSurfaceCell(Map::toSurfCoords(unit->getPos())); + bool + cannotSeeUnit = (unit->getType()->hasCellMap() == true && + unit->getType()->getAllowEmptyCellMap() == + true + && unit->getType()->hasEmptyCellMap() == + true); + + if (sc->isVisible(teamIndex) && cannotSeeUnit == false && + isAlly(unit) == false && unit->isAlive() == true) { + pos = unit->getPos(); + field = unit->getCurrField(); + if (pos.dist(getHomeLocation()) < radius) { + printLog(2, + "Being attacked at pos " + intToStr(pos.x) + + "," + intToStr(pos.y) + "\n"); + + // Now check if there are more than x enemies in sight and if + // so make note of the position + int + foundEnemies = 0; + std::map < int, + bool > + foundEnemyList; + for (int aiX = pos.x - CHECK_RADIUS; + aiX < pos.x + CHECK_RADIUS; ++aiX) { + for (int aiY = pos.y - CHECK_RADIUS; + aiY < pos.y + CHECK_RADIUS; ++aiY) { + Vec2i + checkPos(aiX, aiY); + if (map->isInside(checkPos) + && map->isInsideSurface(map-> + toSurfCoords + (checkPos))) { + Cell * + cAI = map->getCell(checkPos); + SurfaceCell * + scAI = + map-> + getSurfaceCell(Map:: + toSurfCoords(checkPos)); + if (scAI != NULL && cAI != NULL + && cAI->getUnit(field) != NULL + && sc->isVisible(teamIndex)) { + const Unit * + checkUnit = cAI->getUnit(field); + if (foundEnemyList. + find(checkUnit->getId()) == + foundEnemyList.end()) { + bool + cannotSeeUnitAI = + (checkUnit->getType()-> + hasCellMap() == true + && checkUnit->getType()-> + getAllowEmptyCellMap() == true + && checkUnit->getType()-> + hasEmptyCellMap() == true); + if (cannotSeeUnitAI == false + && isAlly(checkUnit) == false + && checkUnit->isAlive() == + true) { + foundEnemies++; + foundEnemyList[checkUnit-> + getId()] = true; + } + } + } + } + } + } + if (foundEnemies >= WARNING_ENEMY_COUNT) { + if (std:: + find(enemyWarningPositionList.begin(), + enemyWarningPositionList.end(), + pos) == enemyWarningPositionList.end()) { + enemyWarningPositionList.push_back(pos); + } + } + return unit; + } + } + } + } + return NULL; + } + + Map * + AiInterface::getMap() { + Map * + map = world->getMap(); + return map; + } + + bool + AiInterface::factionUsesResourceType(const FactionType * factionType, + const ResourceType * rt) { + bool + factionUsesResourceType = factionType->factionUsesResourceType(rt); + return factionUsesResourceType; + } + + void + AiInterface::saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + aiInterfaceNode = rootNode->addChild("AiInterface"); + + // World *world; + // Commander *commander; + // Console *console; + // GameSettings *gameSettings; + // + // Ai ai; + ai. + saveGame(aiInterfaceNode); + // int timer; + aiInterfaceNode-> + addAttribute("timer", intToStr(timer), mapTagReplacements); + // int factionIndex; + aiInterfaceNode-> + addAttribute("factionIndex", intToStr(factionIndex), + mapTagReplacements); + // int teamIndex; + aiInterfaceNode-> + addAttribute("teamIndex", intToStr(teamIndex), mapTagReplacements); + // //config + // bool redir; + aiInterfaceNode-> + addAttribute("redir", intToStr(redir), mapTagReplacements); + // int logLevel; + aiInterfaceNode-> + addAttribute("logLevel", intToStr(logLevel), mapTagReplacements); + // std::map cacheUnitHarvestResourceLookup; + for (std::map < const ResourceType *, int >::const_iterator iterMap = + cacheUnitHarvestResourceLookup.begin(); + iterMap != cacheUnitHarvestResourceLookup.end(); ++iterMap) { + XmlNode * + cacheUnitHarvestResourceLookupNode = + aiInterfaceNode->addChild("cacheUnitHarvestResourceLookup"); + + cacheUnitHarvestResourceLookupNode-> + addAttribute("key", iterMap->first->getName(), + mapTagReplacements); + cacheUnitHarvestResourceLookupNode-> + addAttribute("value", intToStr(iterMap->second), + mapTagReplacements); + } + } + + // AiInterface::AiInterface(Game &game, int factionIndex, int teamIndex, int useStartLocation) { + void + AiInterface::loadGame(const XmlNode * rootNode, Faction * faction) { + XmlNode * + aiInterfaceNode = NULL; + vector < XmlNode * >aiInterfaceNodeList = + rootNode->getChildList("AiInterface"); + for (unsigned int i = 0; i < aiInterfaceNodeList.size(); ++i) { + XmlNode * + node = aiInterfaceNodeList[i]; + if (node->getAttribute("factionIndex")->getIntValue() == + faction->getIndex()) { + aiInterfaceNode = node; + break; + } + } + + if (aiInterfaceNode != NULL) { + factionIndex = + aiInterfaceNode->getAttribute("factionIndex")->getIntValue(); + teamIndex = + aiInterfaceNode->getAttribute("teamIndex")->getIntValue(); + + ai.loadGame(aiInterfaceNode, faction); + //firstTime = timeflowNode->getAttribute("firstTime")->getFloatValue(); + + timer = aiInterfaceNode->getAttribute("timer")->getIntValue(); + // int factionIndex; + //factionIndex = aiInterfaceNode->getAttribute("factionIndex")->getIntValue(); + // int teamIndex; + //teamIndex = aiInterfaceNode->getAttribute("teamIndex")->getIntValue(); + // //config + // bool redir; + redir = + aiInterfaceNode->getAttribute("redir")->getIntValue() != 0; + // int logLevel; + logLevel = + aiInterfaceNode->getAttribute("logLevel")->getIntValue(); + + // std::map cacheUnitHarvestResourceLookup; + // for(std::map::const_iterator iterMap = cacheUnitHarvestResourceLookup.begin(); + // iterMap != cacheUnitHarvestResourceLookup.end(); ++iterMap) { + // XmlNode *cacheUnitHarvestResourceLookupNode = aiInterfaceNode->addChild("cacheUnitHarvestResourceLookup"); + // + // cacheUnitHarvestResourceLookupNode->addAttribute("key",iterMap->first->getName(), mapTagReplacements); + // cacheUnitHarvestResourceLookupNode->addAttribute("value",intToStr(iterMap->second), mapTagReplacements); + // } + } + } + + } } //end namespace diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index f37cb20d4..4639ed31b 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -33,309 +33,295 @@ # include "leak_dumper.h" using - Shared::Util::intToStr; +Shared::Util::intToStr; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// ===================================================== -// class AiInterface -// -/// The AI will interact with the game through this interface -// ===================================================== + // ===================================================== + // class AiInterface + // + /// The AI will interact with the game through this interface + // ===================================================== - class - AiInterfaceThread: - public - BaseThread, - public - SlaveThreadControllerInterface - { - protected: + class + AiInterfaceThread : + public + BaseThread, + public + SlaveThreadControllerInterface { + protected: - AiInterface * - aiIntf; - Semaphore - semTaskSignalled; - Mutex * - triggerIdMutex; - std::pair < int, - bool > - frameIndex; - MasterSlaveThreadController * - masterController; + AiInterface * + aiIntf; + Semaphore + semTaskSignalled; + Mutex * + triggerIdMutex; + std::pair < int, + bool > + frameIndex; + MasterSlaveThreadController * + masterController; - virtual void - setQuitStatus (bool value); - virtual void - setTaskCompleted (int frameIndex); + virtual void + setQuitStatus(bool value); + virtual void + setTaskCompleted(int frameIndex); - public: - explicit - AiInterfaceThread (AiInterface * aiIntf); - virtual ~ - AiInterfaceThread (); - virtual void - execute (); - void - signal (int frameIndex); - bool - isSignalCompleted (int frameIndex); + public: + explicit + AiInterfaceThread(AiInterface * aiIntf); + virtual ~ + AiInterfaceThread(); + virtual void + execute(); + void + signal(int frameIndex); + bool + isSignalCompleted(int frameIndex); - virtual void - setMasterController (MasterSlaveThreadController * master) - { - masterController = master; - } - virtual void - signalSlave (void *userdata) - { - signal (*((int *) (userdata))); - } + virtual void + setMasterController(MasterSlaveThreadController * master) { + masterController = master; + } + virtual void + signalSlave(void *userdata) { + signal(*((int *) (userdata))); + } - virtual void - signalQuit (); - virtual bool - canShutdown (bool deleteSelfIfShutdownDelayed = false); - }; + virtual void + signalQuit(); + virtual bool + canShutdown(bool deleteSelfIfShutdownDelayed = false); + }; - class - AiInterface - { - private: - World * - world; - Commander * - commander; - Console * - console; - GameSettings * - gameSettings; + class + AiInterface { + private: + World * + world; + Commander * + commander; + Console * + console; + GameSettings * + gameSettings; - Ai - ai; + Ai + ai; - int - timer; - int - factionIndex; - int - teamIndex; + int + timer; + int + factionIndex; + int + teamIndex; - //config - bool - redir; - int - logLevel; - std::string - aiLogFile; - FILE * - fp; + //config + bool + redir; + int + logLevel; + std::string + aiLogFile; + FILE * + fp; - std::map < const ResourceType *, int > - cacheUnitHarvestResourceLookup; + std::map < const ResourceType *, int > + cacheUnitHarvestResourceLookup; - Mutex * - aiMutex; + Mutex * + aiMutex; - AiInterfaceThread * - workerThread; - std::vector < - Vec2i > - enemyWarningPositionList; + AiInterfaceThread * + workerThread; + std::vector < + Vec2i > + enemyWarningPositionList; - public: - AiInterface (Game & game, int factionIndex, int teamIndex, - int useStartLocation = -1); - ~ - AiInterface (); + public: + AiInterface(Game & game, int factionIndex, int teamIndex, + int useStartLocation = -1); + ~ + AiInterface(); - AiInterface (const AiInterface & obj) - { - init (); - throw - megaglest_runtime_error ("class AiInterface is NOT safe to copy!"); - } - AiInterface & - operator= (const AiInterface & obj) - { - init (); - throw - megaglest_runtime_error ("class AiInterface is NOT safe to assign!"); - } + AiInterface(const AiInterface & obj) { + init(); + throw + megaglest_runtime_error("class AiInterface is NOT safe to copy!"); + } + AiInterface & + operator= (const AiInterface & obj) { + init(); + throw + megaglest_runtime_error("class AiInterface is NOT safe to assign!"); + } - //main - void - update (); + //main + void + update(); - std::vector < Vec2i > getEnemyWarningPositionList ()const - { - return - enemyWarningPositionList; - } - void - removeEnemyWarningPositionFromList (Vec2i & checkPos); + std::vector < Vec2i > getEnemyWarningPositionList()const { + return + enemyWarningPositionList; + } + void + removeEnemyWarningPositionFromList(Vec2i & checkPos); - inline Mutex * - getMutex () - { - return aiMutex; - } + inline Mutex * + getMutex() { + return aiMutex; + } - void - signalWorkerThread (int frameIndex); - bool - isWorkerThreadSignalCompleted (int frameIndex); - AiInterfaceThread * - getWorkerThread () - { - return workerThread; - } + void + signalWorkerThread(int frameIndex); + bool + isWorkerThreadSignalCompleted(int frameIndex); + AiInterfaceThread * + getWorkerThread() { + return workerThread; + } - bool - isLogLevelEnabled (int level); + bool + isLogLevelEnabled(int level); - //get - int - getTimer () const - { - return - timer; - } - int - getFactionIndex () const - { - return - factionIndex; - } + //get + int + getTimer() const { + return + timer; + } + int + getFactionIndex() const { + return + factionIndex; + } - //misc - void - printLog (int logLevel, const string & s); + //misc + void + printLog(int logLevel, const string & s); - //interact - std::pair < CommandResult, string > giveCommand (int unitIndex, - CommandClass - commandClass, - const Vec2i & pos = - Vec2i (0)); - std::pair < CommandResult, string > giveCommand (int unitIndex, - const CommandType * - commandType, - const Vec2i & pos, - const UnitType * - unitType); - std::pair < CommandResult, string > giveCommand (int unitIndex, - const CommandType * - commandType, - const Vec2i & pos, - int - unitGroupCommandId); - std::pair < CommandResult, string > giveCommand (int unitIndex, - const CommandType * - commandType, Unit * u = - NULL); - std::pair < CommandResult, string > giveCommand (const Unit * unit, - const CommandType * - commandType, - const Vec2i & pos, - int - unitGroupCommandId); + //interact + std::pair < CommandResult, string > giveCommand(int unitIndex, + CommandClass + commandClass, + const Vec2i & pos = + Vec2i(0)); + std::pair < CommandResult, string > giveCommand(int unitIndex, + const CommandType * + commandType, + const Vec2i & pos, + const UnitType * + unitType); + std::pair < CommandResult, string > giveCommand(int unitIndex, + const CommandType * + commandType, + const Vec2i & pos, + int + unitGroupCommandId); + std::pair < CommandResult, string > giveCommand(int unitIndex, + const CommandType * + commandType, Unit * u = + NULL); + std::pair < CommandResult, string > giveCommand(const Unit * unit, + const CommandType * + commandType, + const Vec2i & pos, + int + unitGroupCommandId); - std::pair < CommandResult, - string > giveCommandSwitchTeamVote (const Faction * faction, - SwitchTeamVote * vote); + std::pair < CommandResult, + string > giveCommandSwitchTeamVote(const Faction * faction, + SwitchTeamVote * vote); - //get data - const ControlType - getControlType (); - int - getMapMaxPlayers (); - Vec2i - getHomeLocation (); - Vec2i - getStartLocation (int locationIndex); - int - getFactionCount (); - int - getMyUnitCount () const; - int - getMyUpgradeCount () const; - //int onSightUnitCount(); - const Resource * - getResource (const ResourceType * rt); - const Unit * - getMyUnit (int unitIndex); - Unit * - getMyUnitPtr (int unitIndex); - //const Unit *getOnSightUnit(int unitIndex); - const FactionType * - getMyFactionType (); - Faction * - getMyFaction (); - const TechTree * - getTechTree (); - bool - isResourceInRegion (const Vec2i & pos, const ResourceType * rt, - Vec2i & resourcePos, int range) const; - bool - isResourceNear (const Vec2i & pos, const ResourceType * rt, - Vec2i & resourcePos, Faction * faction, - bool fallbackToPeersHarvestingSameResource) const; - bool - getNearestSightedResource (const ResourceType * rt, const Vec2i & pos, - Vec2i & resultPos, - bool usableResourceTypeOnly); - bool - isAlly (const Unit * unit) const; - bool - isAlly (int factionIndex) const; - bool - reqsOk (const RequirableType * rt); - bool - reqsOk (const CommandType * ct); - bool - checkCosts (const ProducibleType * pt, const CommandType * ct); - bool - isFreeCells (const Vec2i & pos, int size, Field field); - const Unit * - getFirstOnSightEnemyUnit (Vec2i & pos, Field & field, int radius); - Map * - getMap (); - World * - getWorld () - { - return world; - } + //get data + const ControlType + getControlType(); + int + getMapMaxPlayers(); + Vec2i + getHomeLocation(); + Vec2i + getStartLocation(int locationIndex); + int + getFactionCount(); + int + getMyUnitCount() const; + int + getMyUpgradeCount() const; + //int onSightUnitCount(); + const Resource * + getResource(const ResourceType * rt); + const Unit * + getMyUnit(int unitIndex); + Unit * + getMyUnitPtr(int unitIndex); + //const Unit *getOnSightUnit(int unitIndex); + const FactionType * + getMyFactionType(); + Faction * + getMyFaction(); + const TechTree * + getTechTree(); + bool + isResourceInRegion(const Vec2i & pos, const ResourceType * rt, + Vec2i & resourcePos, int range) const; + bool + isResourceNear(const Vec2i & pos, const ResourceType * rt, + Vec2i & resourcePos, Faction * faction, + bool fallbackToPeersHarvestingSameResource) const; + bool + getNearestSightedResource(const ResourceType * rt, const Vec2i & pos, + Vec2i & resultPos, + bool usableResourceTypeOnly); + bool + isAlly(const Unit * unit) const; + bool + isAlly(int factionIndex) const; + bool + reqsOk(const RequirableType * rt); + bool + reqsOk(const CommandType * ct); + bool + checkCosts(const ProducibleType * pt, const CommandType * ct); + bool + isFreeCells(const Vec2i & pos, int size, Field field); + const Unit * + getFirstOnSightEnemyUnit(Vec2i & pos, Field & field, int radius); + Map * + getMap(); + World * + getWorld() { + return world; + } - bool - factionUsesResourceType (const FactionType * factionType, - const ResourceType * rt); + bool + factionUsesResourceType(const FactionType * factionType, + const ResourceType * rt); - void - saveGame (XmlNode * rootNode) const; - void - loadGame (const XmlNode * rootNode, Faction * faction); + void + saveGame(XmlNode * rootNode) const; + void + loadGame(const XmlNode * rootNode, Faction * faction); - private: - string getLogFilename ()const - { - return - "ai" + - intToStr (factionIndex) + - ".log"; - } - bool - executeCommandOverNetwork (); + private: + string getLogFilename()const { + return + "ai" + + intToStr(factionIndex) + + ".log"; + } + bool + executeCommandOverNetwork(); - void - init (); - }; + void + init(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/ai/ai_rule.cpp b/source/glest_game/ai/ai_rule.cpp index 173f46eb1..1bce60de8 100644 --- a/source/glest_game/ai/ai_rule.cpp +++ b/source/glest_game/ai/ai_rule.cpp @@ -19,3646 +19,3226 @@ #include "leak_dumper.h" using - Shared::Graphics::Vec2i; +Shared::Graphics::Vec2i; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class AiRule -// ===================================================== - - AiRule::AiRule (Ai * ai) - { - this->ai = ai; - } - -// ===================================================== -// class AiRuleWorkerHarvest -// ===================================================== - - AiRuleWorkerHarvest::AiRuleWorkerHarvest (Ai * ai): - AiRule (ai) - { - stoppedWorkerIndex = -1; - } - - bool - AiRuleWorkerHarvest::test () - { - return ai->findAbleUnit (&stoppedWorkerIndex, ccHarvest, true); - } - - void - AiRuleWorkerHarvest::execute () - { - ai->harvest (stoppedWorkerIndex); - } - -// ===================================================== -// class AiRuleRefreshHarvester -// ===================================================== - - AiRuleRefreshHarvester::AiRuleRefreshHarvester (Ai * ai): - AiRule (ai) - { - workerIndex = -1; - } - - bool - AiRuleRefreshHarvester::test () - { - return ai->findAbleUnit (&workerIndex, ccHarvest, ccHarvest); - } - - void - AiRuleRefreshHarvester::execute () - { - ai->harvest (workerIndex); - } - -// ===================================================== -// class AiRuleScoutPatrol -// ===================================================== - - AiRuleScoutPatrol::AiRuleScoutPatrol (Ai * ai): - AiRule (ai) - { - } - - bool - AiRuleScoutPatrol::test () - { - return ai->isStableBase (); - } - - void - AiRuleScoutPatrol::execute () - { - ai->sendScoutPatrol (); - } -// ===================================================== -// class AiRuleRepair -// ===================================================== - - AiRuleRepair::AiRuleRepair (Ai * ai): - AiRule (ai) - { - damagedUnitIndex = 0; - damagedUnitIsCastle = false; - } - - double - AiRuleRepair::getMinCastleHpRatio () const - { - return - 0.6; - } - - int - AiRuleRepair::getMinUnitsToRepairCastle () - { - int - minUnitsRepairingCastle = 7; - if (ai->getCountOfClass (ucWorker) <= 6) - { - minUnitsRepairingCastle = 1; - } - else if (ai->getCountOfClass (ucWorker) <= 8) - { - minUnitsRepairingCastle = 2; - } - else if (ai->getCountOfClass (ucWorker) <= 10) - { - minUnitsRepairingCastle = 3; - } - else if (ai->getCountOfClass (ucWorker) <= 12) - { - minUnitsRepairingCastle = 5; - } - return minUnitsRepairingCastle; - } - - bool - AiRuleRepair::test () - { - AiInterface * - aiInterface = ai->getAiInterface (); - - int - minUnitsRepairingCastle = getMinUnitsToRepairCastle (); - const double - minCastleHpRatio = getMinCastleHpRatio (); - - // look for a damaged unit and give priority to the factions bases - // (units that produce workers and store resources) - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - u = aiInterface->getMyUnit (i); - //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - if (u->getHpRatio () < 1.f) - { - //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - - bool - unitCanProduceWorker = false; - for (int j = 0; unitCanProduceWorker == false && - j < u->getType ()->getCommandTypeCount (); ++j) - { - const CommandType * - ct = u->getType ()->getCommandType (j); - - //if the command is produce - if (ct->getClass () == ccProduce - || ct->getClass () == ccMorph) - { - const ProducibleType * - pt = ct->getProduced (); - if (pt != NULL) - { - const UnitType * - ut = dynamic_cast < const - UnitType * >(pt); - if (ut != NULL - && ut->hasCommandClass (ccHarvest) == true - && u->getType ()->getStoredResourceCount () > 0) - { - //printf("\n\n\n\n!!!!!! found candidate castle unit to repair [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); - unitCanProduceWorker = true; - } - } - } - } - - if (unitCanProduceWorker == true) - { - int - candidatedamagedUnitIndex = -1; - int - unitCountAlreadyRepairingDamagedUnit = 0; - // Now check if any other unit is able to repair this unit - for (int i1 = 0; i1 < aiInterface->getMyUnitCount (); ++i1) - { - const Unit * - u1 = aiInterface->getMyUnit (i1); - const RepairCommandType * - rct = static_cast < const - RepairCommandType * - >(u1->getType ()->getFirstCtOfClass (ccRepair)); - //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); - - if (rct != NULL) - { - //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); - - if (u1->getCurrSkill ()->getClass () == scStop - || u1->getCurrSkill ()->getClass () == scMove - || u->getHpRatio () <= minCastleHpRatio) - { - if (rct->isRepairableUnitType (u->getType ())) - { - candidatedamagedUnitIndex = i; - //return true; - } - } - else if (u1->getCurrSkill ()->getClass () == - scRepair) - { - Command * - cmd = u1->getCurrCommand (); - if (cmd != NULL - && cmd->getCommandType ()->getClass () == - ccRepair) - { - if (cmd->getUnit () != NULL - && cmd->getUnit ()->getId () == - u->getId ()) - { - //printf("\n\n\n\n^^^^^^^^^^ unit is ALREADY repairer unit [%d - %s]\n",u1->getId(),u1->getType()->getName().c_str()); - unitCountAlreadyRepairingDamagedUnit++; - } - } - } - } - } - - if (candidatedamagedUnitIndex >= 0 - && unitCountAlreadyRepairingDamagedUnit < - minUnitsRepairingCastle) - { - //printf("\n\n\n\n^^^^^^^^^^ AI test will repair damaged unit [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); - damagedUnitIndex = candidatedamagedUnitIndex; - damagedUnitIsCastle = true; - return true; - } - } - } - } - damagedUnitIsCastle = false; - damagedUnitIndex = -1; - // Normal Repair checking - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - u = aiInterface->getMyUnit (i); - //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); - if (u->getHpRatio () < 1.f) - { - // Now check if any other unit is able to repair this unit - for (int i1 = 0; i1 < aiInterface->getMyUnitCount (); ++i1) - { - const Unit * - u1 = aiInterface->getMyUnit (i1); - const RepairCommandType * - rct = static_cast < const - RepairCommandType * - >(u1->getType ()->getFirstCtOfClass (ccRepair)); - //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); - - if (rct != NULL - && (u1->getCurrSkill ()->getClass () == scStop - || u1->getCurrSkill ()->getClass () == scMove)) - { - if (rct->isRepairableUnitType (u->getType ())) - { - damagedUnitIndex = i; - //random if return or not so we get different targets from time to time - if (ai->getRandom ()->randRange (0, 1) == 0) - return true; - } - } - } - } - } - if (damagedUnitIndex != -1) - { - return true; - } - return false; - } - - void - AiRuleRepair::execute () - { - AiInterface * - aiInterface = ai->getAiInterface (); - const Unit * - damagedUnit = aiInterface->getMyUnit (damagedUnitIndex); - //printf("\n\n\n\n###^^^^^^^^^^ Looking for repairer for damaged unit [%d - %s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str()); - - int - minUnitsRepairingCastle = getMinUnitsToRepairCastle (); - const double - minCastleHpRatio = getMinCastleHpRatio (); - - if (minUnitsRepairingCastle > 2) - { - if (damagedUnit->getCurrSkill ()->getClass () == scBeBuilt) - { // if build is still be build 2 helpers are enough - minUnitsRepairingCastle = 2; - } - - if (!damagedUnitIsCastle) - { - minUnitsRepairingCastle = 2; - } - } - if (aiInterface->getControlType () == ctCpuEasy || - aiInterface->getControlType () == ctNetworkCpuEasy) - { - if (!damagedUnitIsCastle) - { - // cpu easy does not repair! - minUnitsRepairingCastle = 0; - } - } - if (aiInterface->getControlType () == ctCpu || - aiInterface->getControlType () == ctNetworkCpu) - { - if (!damagedUnitIsCastle) - { - // cpu does only repair with one unit! - minUnitsRepairingCastle = 1; - } - } - int - unitCountAlreadyRepairingDamagedUnit = 0; - //printf("team %d has damaged unit\n", damagedUnit->getTeam()); - // Now check if any other unit is able to repair this unit - for (int i1 = 0; i1 < aiInterface->getMyUnitCount (); ++i1) - { - const Unit * - u1 = aiInterface->getMyUnit (i1); - const RepairCommandType * - rct = static_cast < const - RepairCommandType * - >(u1->getType ()->getFirstCtOfClass (ccRepair)); - //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u1->getType())); - Command * - cmd = u1->getCurrCommand (); - if (cmd != NULL && cmd->getCommandType ()->getClass () == ccRepair) - { - //if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == damagedUnit->getId()) - //if(cmd->getUnit() != NULL && cmd->getPos() == damagedUnit->getPosWithCellMapSet()) - - if (rct != NULL) - { - //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); - - if (((RepairCommandType *) (cmd->getCommandType ()))-> - isRepairableUnitType (damagedUnit->getType ())) - { - //printf("^^^^test^^^^^^ unit is ALREADY repairer unit [%d - %s]\nminUnitsRepairingCastle=%d\n",u1->getId(), u1->getType()->getName().c_str(), minUnitsRepairingCastle); - unitCountAlreadyRepairingDamagedUnit++; - } - } - } - } - - if (unitCountAlreadyRepairingDamagedUnit >= minUnitsRepairingCastle) - { - return; - } - - int - unitGroupCommandId = -1; - - //find a repairer and issue command - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - const Unit * - u = aiInterface->getMyUnit (i); - const RepairCommandType * - rct = static_cast < const - RepairCommandType * - >(u->getType ()->getFirstCtOfClass (ccRepair)); - //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType())); - - if (rct != NULL) - { - //printf("\n\n\n\n^^^^^^^^^^ possible excute repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType()),damagedUnit->getHpRatio()); - - if ((u->getCurrSkill ()->getClass () == scStop - || u->getCurrSkill ()->getClass () == scMove - || damagedUnit->getHpRatio () <= minCastleHpRatio)) - { - if ((u->getCurrCommand () == NULL - || (u->getCurrCommand ()->getCommandType ()-> - getClass () != ccBuild - && u->getCurrCommand ()->getCommandType ()-> - getClass () != ccProduce)) - && rct->isRepairableUnitType (damagedUnit->getType ())) - { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("\n\n\n\n^^^^^^^^^^ AI execute will repair damaged unit [%d - %s] at pos [%s] cellmapPos [%s] using unit [%d -%s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str(),damagedUnit->getPos().getString().c_str(),damagedUnit->getPosWithCellMapSet().getString().c_str(),u->getId(),u->getType()->getName().c_str()); - - /* - Map *map= aiInterface->getWorld()->getMap(); - Cell *cell = map->getCell(damagedUnit->getPosWithCellMapSet()); - if(cell != NULL) { - printf("\n\n\n\n^^^^^^^^^^ cell is ok\n"); - - Unit *cellUnit = cell->getUnit(damagedUnit->getCurrField()); - if(cellUnit != NULL) { - printf("\n\n\n\n^^^^^^^^^^ cell unit [%d - %s] at pos [%s]\n",cellUnit->getId(),cellUnit->getType()->getName().c_str(),cellUnit->getPos().getString().c_str()); - } - } - */ - - //aiInterface->giveCommand(i, rct, damagedUnit->getPos()); - if (unitCountAlreadyRepairingDamagedUnit < - minUnitsRepairingCastle) - { - - if (unitGroupCommandId == -1) - { - unitGroupCommandId = - aiInterface->getWorld ()-> - getNextCommandGroupId (); - } - - aiInterface->giveCommand (i, rct, - damagedUnit-> - getPosWithCellMapSet (), - unitGroupCommandId); - if (aiInterface->isLogLevelEnabled (3) == true) - aiInterface->printLog (3, - "Repairing order issued"); - unitCountAlreadyRepairingDamagedUnit++; - // printf( - // "^^^^^^^^^^adding one unit to repair ... unitCountAlreadyRepairingDamagedUnit/minUnitsRepairingCastle=%d/%d\n", - // unitCountAlreadyRepairingDamagedUnit, minUnitsRepairingCastle); - } - - if (!damagedUnitIsCastle - || unitCountAlreadyRepairingDamagedUnit >= - minUnitsRepairingCastle) - { - return; - } - } - } - } - } - } - -// ===================================================== -// class AiRuleReturnBase -// ===================================================== - - AiRuleReturnBase::AiRuleReturnBase (Ai * ai): - AiRule (ai) - { - stoppedUnitIndex = -1; - } - - bool - AiRuleReturnBase::test () - { - return ai->findAbleUnit (&stoppedUnitIndex, ccMove, true); - } - - void - AiRuleReturnBase::execute () - { - ai->returnBase (stoppedUnitIndex); - } - -// ===================================================== -// class AiRuleMassiveAttack -// ===================================================== - - AiRuleMassiveAttack::AiRuleMassiveAttack (Ai * ai): - AiRule (ai) - { - ultraAttack = false; - field = fLand; - } - - bool - AiRuleMassiveAttack::test () - { - - if (ai->isStableBase ()) - { - ultraAttack = false; - return ai->beingAttacked (attackPos, field, INT_MAX); - } - else - { - ultraAttack = true; - return ai->beingAttacked (attackPos, field, baseRadius); - } - } - - void - AiRuleMassiveAttack::execute () - { - ai->massiveAttack (attackPos, field, ultraAttack); - } -// ===================================================== -// class AiRuleAddTasks -// ===================================================== - - AiRuleAddTasks::AiRuleAddTasks (Ai * ai): - AiRule (ai) - { - } - - bool - AiRuleAddTasks::test () - { - return !ai->anyTask () || ai->getCountOfClass (ucWorker) < 4; - } - -// This function is what triggers the AI to create new units. - void - AiRuleAddTasks::execute () - { - int - buildingCount = ai->getCountOfClass (ucBuilding); - UnitClass - ucWorkerType = ucWorker; - int - warriorCount = ai->getCountOfClass (ucWarrior, &ucWorkerType); - int - workerCount = ai->getCountOfClass (ucWorker); - int - upgradeCount = ai->getAiInterface ()->getMyUpgradeCount (); - - float - buildingRatio = ai->getRatioOfClass (ucBuilding); - float - warriorRatio = ai->getRatioOfClass (ucWarrior); - float - workerRatio = ai->getRatioOfClass (ucWorker); - - //standard tasks - if (ai->outputAIBehaviourToConsole ()) - printf - ("Add a TASK - AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", - workerCount, this->getName ().c_str ()); - - //emergency workers - if (workerCount < 4) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("AAA AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", - workerCount, this->getName ().c_str ()); - - ai->addPriorityTask (new ProduceTask (ucWorker)); - } - // The following rules are specific creation rules for different AI. - else - { - if (ai->getAiInterface ()->getControlType () == ctCpuZeta || - ai->getAiInterface ()->getControlType () == ctNetworkCpuZeta) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("AAA AiRuleAddTasks adding #1 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount, workerRatio, buildingCount, buildingRatio, - warriorCount, warriorRatio, upgradeCount, - this->getName ().c_str ()); - - //workers - if (workerCount < 5) - ai->addTask (new ProduceTask (ucWorker)); - if (workerCount < 100) - ai->addTask (new ProduceTask (ucWorker)); - if (workerRatio < 0.20) - ai->addTask (new ProduceTask (ucWorker)); - if (workerRatio < 0.30) - ai->addTask (new ProduceTask (ucWorker)); - - //warriors - if (warriorCount < 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.20) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.30) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 15) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorCount < ai->getMinWarriors () + 2) - { - ai->addTask (new ProduceTask (ucWarrior)); - if (buildingCount > 9) - { - ai->addTask (new ProduceTask (ucWarrior)); - ai->addTask (new ProduceTask (ucWarrior)); - } - if (buildingCount > 12) - { - ai->addTask (new ProduceTask (ucWarrior)); - ai->addTask (new ProduceTask (ucWarrior)); - } - } - - //buildings - if (buildingCount < 6 || buildingRatio < 0.20) - ai->addTask (new BuildTask ((UnitType *) NULL)); - if (buildingCount < 10 && workerCount > 12) - ai->addTask (new BuildTask ((UnitType *) NULL)); - //upgrades - if (upgradeCount == 0 && workerCount > 5) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 1 && workerCount > 10) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 2 && workerCount > 15) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (ai->isStableBase ()) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - } - else if (ai->getAiInterface ()->getControlType () == ctCpuEasy || - ai->getAiInterface ()->getControlType () == - ctNetworkCpuEasy) - { // Easy CPU - - if (ai->outputAIBehaviourToConsole ()) - printf - ("AAA AiRuleAddTasks adding #2 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount, workerRatio, buildingCount, buildingRatio, - warriorCount, warriorRatio, upgradeCount, - this->getName ().c_str ()); - - //workers - if (workerCount < buildingCount + 2) - ai->addTask (new ProduceTask (ucWorker)); - if (workerCount > 5 && workerRatio < 0.20) - ai->addTask (new ProduceTask (ucWorker)); - - //warriors - if (warriorCount < 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.20) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.30) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 15) - ai->addTask (new ProduceTask (ucWarrior)); - - //buildings - if (buildingCount < 6 || buildingRatio < 0.20) - ai->addTask (new BuildTask ((UnitType *) NULL)); - if (buildingCount < 10 && ai->isStableBase ()) - ai->addTask (new BuildTask ((UnitType *) NULL)); - - //upgrades - if (upgradeCount == 0 && workerCount > 6) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 1 && workerCount > 7) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 2 && workerCount > 9) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - //if(ai->isStableBase()) ai->addTask(new UpgradeTask()); - } - else - { // normal CPU / UltraCPU ... - if (ai->outputAIBehaviourToConsole ()) - printf - ("AAA AiRuleAddTasks adding #3 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", - workerCount, workerRatio, buildingCount, buildingRatio, - warriorCount, warriorRatio, upgradeCount, - this->getName ().c_str ()); - - //workers - if (workerCount < 5) - ai->addTask (new ProduceTask (ucWorker)); - if (workerCount < 10) - ai->addTask (new ProduceTask (ucWorker)); - if (workerRatio < 0.20) - ai->addTask (new ProduceTask (ucWorker)); - if (workerRatio < 0.30) - ai->addTask (new ProduceTask (ucWorker)); - - //warriors - if (warriorCount < 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.20) - ai->addTask (new ProduceTask (ucWarrior)); - if (warriorRatio < 0.30) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 10) - ai->addTask (new ProduceTask (ucWarrior)); - if (workerCount >= 15) - ai->addTask (new ProduceTask (ucWarrior)); - - //buildings - if (buildingCount < 6 || buildingRatio < 0.20) - ai->addTask (new BuildTask ((UnitType *) NULL)); - if (buildingCount < 10 && workerCount > 12) - ai->addTask (new BuildTask ((UnitType *) NULL)); - - //upgrades - if (upgradeCount == 0 && workerCount > 5) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 1 && workerCount > 10) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (upgradeCount == 2 && workerCount > 15) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - if (ai->isStableBase ()) - ai->addTask (new UpgradeTask ((const UpgradeType *) NULL)); - } - } - } - -// ===================================================== -// class AiRuleBuildOneFarm -// ===================================================== - - AiRuleBuildOneFarm::AiRuleBuildOneFarm (Ai * ai): - AiRule (ai) - { - farm = NULL; - } - - bool - AiRuleBuildOneFarm::test () - { - AiInterface * - aiInterface = ai->getAiInterface (); - - //for all units - for (int i = 0; - i < aiInterface->getMyFactionType ()->getUnitTypeCount (); ++i) - { - const UnitType * - ut = aiInterface->getMyFactionType ()->getUnitType (i); - - //for all production commands - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - if (ct->getClass () == ccProduce) - { - const UnitType * - producedType = static_cast < const - ProduceCommandType * >(ct)-> - getProducedUnit (); - - //for all resources - for (int k = 0; k < producedType->getCostCount (); ++k) - { - const Resource * - r = producedType->getCost (k); - - //find a food producer in the farm produced units - if (r->getAmount () < 0 - && r->getType ()->getClass () == rcConsumable - && ai->getCountOfType (ut) == 0) - { - if (aiInterface->reqsOk (ct) - && aiInterface->getMyFaction ()-> - canCreateUnit (ut, true, true, true) == true) - { - farm = ut; - //printf("AiRuleBuildOneFarm returning true, RULE Name[%s] ut [%s] producedType [%s]\n",this->getName().c_str(),ut->getName().c_str(),producedType->getName().c_str()); - - if (ai->outputAIBehaviourToConsole ()) - printf - ("AiRuleBuildOneFarm returning true, RULE Name[%s]\n", - this->getName ().c_str ()); - - return true; - } - } - } - } - } - } - return false; - } - - void - AiRuleBuildOneFarm::execute () - { - ai->addPriorityTask (new BuildTask (farm)); - } - -// ===================================================== -// class AiRuleProduceResourceProducer -// ===================================================== - - AiRuleProduceResourceProducer::AiRuleProduceResourceProducer (Ai * ai): - AiRule (ai) - { - interval = shortInterval; - rt = NULL; - newResourceBehaviour = - Config::getInstance ().getBool ("NewResourceBehaviour", "false");; - } - - bool - AiRuleProduceResourceProducer::test () - { - //emergency tasks: resource buildings - AiInterface * - aiInterface = ai->getAiInterface (); - - //consumables first - for (int i = 0; - i < aiInterface->getTechTree ()->getResourceTypeCount (); ++i) - { - rt = aiInterface->getTechTree ()->getResourceType (i); - const Resource * - r = aiInterface->getResource (rt); - - if (ai->outputAIBehaviourToConsole ()) - printf ("CONSUMABLE [%s][%d] Testing AI RULE Name[%s]\n", - rt->getName ().c_str (), r->getBalance (), - this->getName ().c_str ()); - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "CONSUMABLE [%s][%d] Testing AI RULE Name[%s]", - rt->getName ().c_str (), r->getBalance (), - this->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - bool - factionUsesResourceType = - aiInterface->factionUsesResourceType (aiInterface-> - getMyFactionType (), rt); - if (factionUsesResourceType == true - && rt->getClass () == rcConsumable) - { - // The consumable balance is negative - if (r->getBalance () < 0) - { - if (newResourceBehaviour == true) - { - interval = shortInterval; - } - else - { - interval = longInterval; - } - - return true; - } - // If the consumable balance is down to 1/3 of what we need - else - { - if (r->getBalance () * 3 + r->getAmount () < 0) - { - if (newResourceBehaviour == true) - { - interval = shortInterval; - } - else - { - interval = longInterval; - } - - return true; - } - } - } - } - - int - targetStaticResourceCount = minStaticResources; - if (aiInterface->getMyFactionType ()-> - getAIBehaviorStaticOverideValue (aibsvcMinStaticResourceCount) != - INT_MAX) - { - targetStaticResourceCount = - aiInterface->getMyFactionType ()-> - getAIBehaviorStaticOverideValue (aibsvcMinStaticResourceCount); - } - - //statics second - for (int i = 0; - i < aiInterface->getTechTree ()->getResourceTypeCount (); ++i) - { - rt = aiInterface->getTechTree ()->getResourceType (i); - const Resource * - r = aiInterface->getResource (rt); - - if (ai->outputAIBehaviourToConsole ()) - printf ("STATIC [%s][%d] [min %d] Testing AI RULE Name[%s]\n", - rt->getName ().c_str (), r->getAmount (), - targetStaticResourceCount, this->getName ().c_str ()); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "STATIC resource check [%s][%d] [min %d] Testing AI RULE Name[%s]", - rt->getName ().c_str (), r->getAmount (), - targetStaticResourceCount, this->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - if (rt->getClass () == rcStatic - && r->getAmount () < targetStaticResourceCount) - { - bool - factionUsesResourceType = - aiInterface->factionUsesResourceType (aiInterface-> - getMyFactionType (), - rt); - if (factionUsesResourceType == true) - { - if (newResourceBehaviour == true) - interval = shortInterval; - else - interval = longInterval; - return true; - } - } - } - - if (ai->outputAIBehaviourToConsole ()) - printf ("STATIC returning FALSE\n"); - if (aiInterface->isLogLevelEnabled (4) == true) - aiInterface->printLog (4, "Static Resource check returning FALSE"); - - if (newResourceBehaviour == true) - interval = longInterval; - else - interval = shortInterval; - return false; - } - - void - AiRuleProduceResourceProducer::execute () - { - ai->addPriorityTask (new ProduceTask (rt)); - ai->addTask (new BuildTask (rt)); - } - -// ===================================================== -// class AiRuleProduce -// ===================================================== - - AiRuleProduce::AiRuleProduce (Ai * ai): - AiRule (ai) - { - produceTask = NULL; - newResourceBehaviour = - Config::getInstance ().getBool ("NewResourceBehaviour", "false"); - } - - bool - AiRuleProduce::test () - { - const Task * - task = ai->getTask (); - - if (task == NULL || task->getClass () != tcProduce) - { - return false; - } - - produceTask = static_cast < const ProduceTask *>(task); - return true; - } - - void - AiRuleProduce::execute () - { - AiInterface * - aiInterface = ai->getAiInterface (); - if (produceTask != NULL) - { - - if (ai->outputAIBehaviourToConsole ()) - printf ("AiRuleProduce producing [%s]\n", - (produceTask->getUnitType () != - NULL ? produceTask->getUnitType ()->getName (false). - c_str () : "null")); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "AiRuleProduce producing [%s]", - (produceTask->getUnitType () != - NULL ? produceTask->getUnitType ()->getName (false). - c_str () : "null")); - aiInterface->printLog (4, szBuf); - } - - //generic produce task, produce random unit that has the skill or produces the resource - if (produceTask->getUnitType () == NULL) - { - if (newResourceBehaviour) - { - produceGenericNew (produceTask); - } - else - produceGeneric (produceTask); - } - - //specific produce task, produce if possible, retry if not enough resources - else - { - produceSpecific (produceTask); - } - - //remove the task - ai->removeTask (produceTask); - } - } - - bool - AiRuleProduce::canUnitTypeOfferResourceType (const UnitType * ut, - const ResourceType * rt) - { - bool - unitTypeOffersResourceType = false; - - AiInterface * - aiInterface = ai->getAiInterface (); - - if (ut != NULL && rt != NULL && aiInterface != NULL - && aiInterface->reqsOk (ut)) - { - // Check of the unit 'gives' the resource - // if the unit produces the resource - const Resource * - r = ut->getCost (rt); - if (r != NULL) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("#2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", - r->getDescription (false).c_str (), r->getAmount (), - this->getName ().c_str ()); - } - - if (r != NULL && r->getAmount () < 0) - { - unitTypeOffersResourceType = true; - } - else - { - // for each command check if we produce a unit that handles the resource - for (int commandIndex = 0; - commandIndex < ut->getCommandTypeCount (); ++commandIndex) - { - const CommandType * - ct = ut->getCommandType (commandIndex); - - //if the command is produce - if (ct->getClass () == ccProduce - || ct->getClass () == ccMorph) - { - const UnitType * - producedUnit = static_cast < const - UnitType * >(ct->getProduced ()); - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric [%p] Testing AI RULE Name[%s]\n", - rt, this->getName ().c_str ()); - - //if the unit produces the resource - const Resource * - r = producedUnit->getCost (rt); - if (r != NULL) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", - r->getDescription (false).c_str (), - r->getAmount (), this->getName ().c_str ()); - } - - if (r != NULL && r->getAmount () < 0) - { - unitTypeOffersResourceType = true; - break; - } - } - } - } - } - - if (aiInterface != NULL && aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "canUnitTypeOfferResourceType for unit type [%s] for resource type [%s] returned: %d", - (ut != NULL ? ut->getName (false).c_str () : "n/a"), - (rt != NULL ? rt->getName (false).c_str () : "n/a"), - unitTypeOffersResourceType); - aiInterface->printLog (4, szBuf); - } - - return unitTypeOffersResourceType; - } - - bool - AiRuleProduce::setAIProduceTaskForResourceType (const ProduceTask * pt, - AiInterface * aiInterface) - { - bool - taskAdded = false; - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - const UnitType * - ut = priorityUnit.first; - if (ai->getCountOfType (ut) < priorityUnit.second && - canUnitTypeOfferResourceType (ut, - pt->getResourceType ()) == - true - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - ai->addTask (new ProduceTask (priorityUnit.first)); - taskAdded = true; - break; - } - } - } - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "setAIProduceTaskForResourceType for resource type [%s] returned: %d", - pt->getResourceType ()->getName (false).c_str (), - taskAdded); - aiInterface->printLog (4, szBuf); - } - - return taskAdded; - } - - void - AiRuleProduce::addUnitTypeToCandidates (const UnitType * producedUnit, - UnitTypes & ableUnits, - UnitTypesGiveBack & - ableUnitsGiveBack, - bool unitCanGiveBackResource) - { - // if the unit is not already on the list - if (find (ableUnits.begin (), ableUnits.end (), producedUnit) == - ableUnits.end ()) - { - ableUnits.push_back (producedUnit); - ableUnitsGiveBack.push_back (unitCanGiveBackResource); - - AiInterface * - aiInterface = ai->getAiInterface (); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "addUnitTypeToCandidates for unit type [%s] unitCanGiveBackResource = %d", - producedUnit->getName (false).c_str (), - unitCanGiveBackResource); - aiInterface->printLog (4, szBuf); - } - - } - } - - void - AiRuleProduce::produceGenericNew (const ProduceTask * pt) - { - UnitTypes - ableUnits; - UnitTypesGiveBack - ableUnitsGiveBack; - - AiInterface * - aiInterface = ai->getAiInterface (); - if (pt->getResourceType () != NULL) - { - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "****START: produceGeneric for resource type [%s]", - pt->getResourceType ()->getName (false).c_str ()); - aiInterface->printLog (4, szBuf); - } - - if (setAIProduceTaskForResourceType (pt, aiInterface) == true) - { - return; - } - } - else if (pt->getUnitClass () == ucWorker) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWorkerUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWorkerUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - ai->addTask (new ProduceTask (priorityUnit.first)); - return; - } - } - } - } - else if (pt->getUnitClass () == ucWarrior) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWarriorUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWarriorUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - ai->addTask (new ProduceTask (priorityUnit.first)); - return; - } - } - } - } - - //for each unit, produce it if possible - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - if (aiInterface->getMyUnit (i)->getCurrCommand () != NULL - && aiInterface->getMyUnit (i)->getCurrCommand ()-> - getCommandType ()->getClass () == ccBuild) - { - //skip this units as it is currently building something - continue; - } - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - - //bool produceIt= false; - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is produce - //bool produceIt= false; - if (ct->getClass () == ccProduce || ct->getClass () == ccMorph) - { - const UnitType * - producedUnit = static_cast < const - UnitType * >(ct->getProduced ()); - - if (ai->outputAIBehaviourToConsole ()) - printf ("produceGeneric [%p] Testing AI RULE Name[%s]\n", - pt->getResourceType (), - this->getName ().c_str ()); - - //if the unit produces the resource - if (pt->getResourceType () != NULL) - { - const Resource * - r = producedUnit->getCost (pt->getResourceType ()); - if (r != NULL) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", - r->getDescription (false).c_str (), - r->getAmount (), this->getName ().c_str ()); - } - - if (r != NULL && r->getAmount () < 0) - { - if (aiInterface->reqsOk (ct) - && aiInterface->reqsOk (producedUnit)) - { - //produceIt= true; - addUnitTypeToCandidates (producedUnit, - ableUnits, - ableUnitsGiveBack, - false); - } - } - } - else - { - //if the unit is from the right class - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n", - producedUnit->isOfClass (pt->getUnitClass ()), - this->getName ().c_str ()); - - if (producedUnit->isOfClass (pt->getUnitClass ())) - { - if (aiInterface->reqsOk (ct) - && aiInterface->reqsOk (producedUnit)) - { - //produceIt= true; - addUnitTypeToCandidates (producedUnit, - ableUnits, - ableUnitsGiveBack, - false); - } - } - } - } - } - // Now check of the unit 'gives' the resource -// This is likely a unit that it BUILT by another and that is handled by a different AI task type: Build -// if(produceIt == false && pt->getResourceType() != NULL) { -// const Resource *r= ut->getCost(pt->getResourceType()); -// if(r != NULL) { -// if(ai->outputAIBehaviourToConsole()) printf("#2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n",r->getDescription(false).c_str(),r->getAmount(), this->getName().c_str()); -// } -// -// if(r != NULL && r->getAmount() < 0) { -// if(aiInterface->reqsOk(ut)){ -// produceIt= true; -// addUnitTypeToCandidates(ut, ableUnits,ableUnitsGiveBack, true); -// } -// } -// } - - } - - //add specific produce task - if (ableUnits.empty () == false) - { - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n", - (int) ableUnits.size (), this->getName ().c_str ()); - - // Now check if we already have at least 2 produce or morph - // resource based units, if so prefer units that give back the resource - if (pt->getResourceType () != NULL && ableUnits.size () > 1) - { - //priority for non produced units - UnitTypes - newAbleUnits; - bool - haveEnoughProducers = true; - bool - haveNonProducers = false; - for (unsigned int i = 0; i < ableUnits.size (); ++i) - { - const UnitType * - ut = ableUnits[i]; - bool - givesBack = ableUnitsGiveBack[i]; - if (givesBack == false && ai->getCountOfType (ut) < 2) - { - haveEnoughProducers = false; - } - else if (givesBack == true) - { - haveNonProducers = true; - newAbleUnits.push_back (ut); - } - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In produceGeneric for unit type [%s] givesBack: %d count of unit type: %d", - ut->getName (false).c_str (), givesBack, - ai->getCountOfType (ut)); - aiInterface->printLog (4, szBuf); - } - - } - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "haveEnoughProducers [%d] haveNonProducers [%d]", - haveEnoughProducers, haveNonProducers); - aiInterface->printLog (4, szBuf); - - for (unsigned int i = 0; i < ableUnits.size (); ++i) - { - const UnitType * - ut = ableUnits[i]; - snprintf (szBuf, 8096, "i: %u unit type [%s]", i, - ut->getName (false).c_str ()); - aiInterface->printLog (4, szBuf); - } - for (unsigned int i = 0; i < newAbleUnits.size (); ++i) - { - const UnitType * - ut = newAbleUnits[i]; - snprintf (szBuf, 8096, "i: %u new unit type [%s]", i, - ut->getName (false).c_str ()); - aiInterface->printLog (4, szBuf); - } - } - - if (haveEnoughProducers == true && haveNonProducers == true) - { - ableUnits = newAbleUnits; - } - } - - //priority for non produced units - for (unsigned int i = 0; i < ableUnits.size (); ++i) - { - if (ai->getCountOfType (ableUnits[i]) == 0) - { - if (ai->getRandom ()->randRange (0, 1) == 0) - { - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In produceGeneric priority adding produce task: %d of " - MG_SIZE_T_SPECIFIER " for unit type [%s]", - i, ableUnits.size (), - ableUnits[i]->getName (false).c_str ()); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new ProduceTask (ableUnits[i])); - return; - } - } - } - - //normal case - int - randomUnitTypeIndex = - ai->getRandom ()->randRange (0, (int) ableUnits.size () - 1); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In produceGeneric randomUnitTypeIndex = %d of " - MG_SIZE_T_SPECIFIER " equals unit type [%s]", - randomUnitTypeIndex, ableUnits.size () - 1, - ableUnits[randomUnitTypeIndex]->getName (false). - c_str ()); - aiInterface->printLog (4, szBuf); - } - - const UnitType * - ut = ableUnits[randomUnitTypeIndex]; - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "== END In produceGeneric normal adding produce task for unit type [%s]", - ut->getName (false).c_str ()); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new ProduceTask (ut)); - } - } - - void - AiRuleProduce::produceGeneric (const ProduceTask * pt) - { - typedef - vector < const UnitType *> - UnitTypes; - UnitTypes - ableUnits; - AiInterface * - aiInterface = ai->getAiInterface (); - - if (pt->getResourceType () != NULL) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - ai->addTask (new ProduceTask (priorityUnit.first)); - return; - //} - } - } - } - } - else if (pt->getUnitClass () == ucWorker) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWorkerUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWorkerUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - ai->addTask (new ProduceTask (priorityUnit.first)); - return; - //} - } - } - } - } - else if (pt->getUnitClass () == ucWarrior) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWarriorUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcWarriorUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, false, true, - true) == true) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - ai->addTask (new ProduceTask (priorityUnit.first)); - return; - //} - } - } - } - } - - //for each unit, produce it if possible - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - if (aiInterface->getMyUnit (i)->getCurrCommand () != NULL - && aiInterface->getMyUnit (i)->getCurrCommand ()-> - getCommandType ()->getClass () == ccBuild) - { - //skip this units as it is currently building something - continue; - } - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is produce - if (ct->getClass () == ccProduce || ct->getClass () == ccMorph) - { - - const UnitType * - producedUnit = static_cast < const - UnitType * >(ct->getProduced ()); - bool - produceIt = false; - - if (ai->outputAIBehaviourToConsole ()) - printf ("produceGeneric [%p] Testing AI RULE Name[%s]\n", - pt->getResourceType (), - this->getName ().c_str ()); - - //if the unit produces the resource - if (pt->getResourceType () != NULL) - { - const Resource * - r = producedUnit->getCost (pt->getResourceType ()); - - if (r != NULL) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", - r->getDescription (false).c_str (), - r->getAmount (), this->getName ().c_str ()); - } - - if (r != NULL && r->getAmount () < 0) - { - produceIt = true; - } - } - - else - { - //if the unit is from the right class - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n", - producedUnit->isOfClass (pt->getUnitClass ()), - this->getName ().c_str ()); - - if (producedUnit->isOfClass (pt->getUnitClass ())) - { - if (aiInterface->reqsOk (ct) - && aiInterface->reqsOk (producedUnit)) - { - produceIt = true; - } - } - } - - if (produceIt) - { - //if the unit is not already on the list - if (find - (ableUnits.begin (), ableUnits.end (), - producedUnit) == ableUnits.end ()) - { - ableUnits.push_back (producedUnit); - } - } - } - } - } - - //add specific produce task - if (ableUnits.empty () == false) - { - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n", - (int) ableUnits.size (), this->getName ().c_str ()); - - //priority for non produced units - for (unsigned int i = 0; i < ableUnits.size (); ++i) - { - if (ai->getCountOfType (ableUnits[i]) == 0) - { - if (ai->getRandom ()->randRange (0, 1) == 0) - { - ai->addTask (new ProduceTask (ableUnits[i])); - return; - } - } - } - - //normal case - ai-> - addTask (new - ProduceTask (ableUnits - [ai->getRandom ()-> - randRange (0, - (int) ableUnits.size () - 1)])); - } - } - - void - AiRuleProduce::produceSpecific (const ProduceTask * pt) - { - - AiInterface * - aiInterface = ai->getAiInterface (); - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s]\n", - pt->getUnitType ()->getName ().c_str (), - aiInterface->reqsOk (pt->getUnitType ()), - this->getName ().c_str ()); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "== START produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s]", - pt->getUnitType ()->getName ().c_str (), - aiInterface->reqsOk (pt->getUnitType ()), - this->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - //if unit meets requirements - if (aiInterface->reqsOk (pt->getUnitType ())) - { - - const CommandType * - ctypeForCostCheck = NULL; - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - //don't use units which are currently building - if (aiInterface->getMyUnit (i)->getCurrCommand () != NULL - && aiInterface->getMyUnit (i)->getCurrCommand ()-> - getCommandType ()->getClass () == ccBuild) - { - //skip this units as it is currently building something - continue; - } - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is produce - if (ct->getClass () == ccProduce - || ct->getClass () == ccMorph) - { - const UnitType * - producedUnit = static_cast < const - UnitType * >(ct->getProduced ()); - - //if units match - if (producedUnit == pt->getUnitType ()) - { - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n", - aiInterface->reqsOk (ct), - this->getName ().c_str ()); - - if (aiInterface->reqsOk (ct)) - { - if (ctypeForCostCheck == NULL - || ct->getClass () == ccMorph) - { - if (ctypeForCostCheck != NULL - && ct->getClass () == ccMorph) - { - const MorphCommandType * - mct = dynamic_cast < const - MorphCommandType * >(ct); - if (mct == NULL) - { - throw - megaglest_runtime_error - ("mct == NULL"); - } - if (mct-> - getIgnoreResourceRequirements () == - true) - { - ctypeForCostCheck = ct; - } - } - else - { - ctypeForCostCheck = ct; - } - } - } - } - } - } - } - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n", - aiInterface->checkCosts (pt->getUnitType (), - ctypeForCostCheck), - this->getName ().c_str ()); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]", - aiInterface->checkCosts (pt->getUnitType (), - ctypeForCostCheck), - this->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - //if unit doesnt meet resources retry - if (aiInterface-> - checkCosts (pt->getUnitType (), ctypeForCostCheck) == false) - { - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "Check costs FAILED."); - aiInterface->printLog (4, szBuf); - } - - ai->retryTask (pt); - return; - } - - //produce specific unit - vector < int > - producers; - // Hold a list of units which can produce or morph - // then a list of commandtypes for each unit - map < int, - vector < const CommandType *> > - producersDefaultCommandType; - const CommandType * - defCt = NULL; - - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - if (aiInterface->getMyUnit (i)->getCurrCommand () != NULL - && aiInterface->getMyUnit (i)->getCurrCommand ()-> - getCommandType ()->getClass () == ccBuild) - { - //skip this units as it is currently building something - continue; - } - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is produce - if (ct->getClass () == ccProduce - || ct->getClass () == ccMorph) - { - const UnitType * - producedUnit = static_cast < const - UnitType * >(ct->getProduced ()); - - //if units match - if (producedUnit == pt->getUnitType ()) - { - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n", - aiInterface->reqsOk (ct), - this->getName ().c_str ()); - - if (aiInterface->reqsOk (ct)) - { - //defCt= ct; - producers.push_back (i); - producersDefaultCommandType[i].push_back (ct); - } - } - } - } - } - - //produce from random producer - if (producers.empty () == false) - { - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]\n", - producers.empty (), this->getName ().c_str ()); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]", - producers.empty (), this->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - // Narrow down producers list to those who are not busy if possible - vector < int > - idle_producers; - for (unsigned int i = 0; i < producers.size (); ++i) - { - int - currentProducerIndex = producers[i]; - if (currentProducerIndex >= aiInterface->getMyUnitCount ()) - { - char - szBuf[8096] = ""; - printf - ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, - __LINE__, currentProducerIndex, - aiInterface->getMyUnitCount (), i, - producers.size ()); - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER "", __FILE__, - __FUNCTION__, __LINE__, currentProducerIndex, - aiInterface->getMyUnitCount (), i, - producers.size ()); - throw - megaglest_runtime_error (szBuf); - } - - const Unit * - unit = aiInterface->getMyUnit (currentProducerIndex); - if (unit->anyCommand () == false) - { - idle_producers.push_back (currentProducerIndex); - } - } - if (idle_producers.empty () == false) - { - producers = idle_producers; - } - - if (aiInterface->getControlType () == ctCpuZeta || - aiInterface->getControlType () == ctNetworkCpuZeta) - { // zeta cpu trys to balance the commands to the producers - int - randomstart = - ai->getRandom ()->randRange (0, - (int) producers.size () - 1); - int - lowestCommandCount = 1000000; - int - currentProducerIndex = producers[randomstart]; - int - bestIndex = -1; - //int besti=0; - int - currentCommandCount = 0; - for (unsigned int i = randomstart; - i < producers.size () + randomstart; i++) - { - int - prIndex = i; - if (i >= producers.size ()) - { - prIndex = (i - (int) producers.size ()); - } - currentProducerIndex = producers[prIndex]; - - if (currentProducerIndex >= - aiInterface->getMyUnitCount ()) - { - char - szBuf[8096] = ""; - printf - ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, - __LINE__, currentProducerIndex, - aiInterface->getMyUnitCount (), i, - producers.size ()); - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER "", __FILE__, - __FUNCTION__, __LINE__, - currentProducerIndex, - aiInterface->getMyUnitCount (), i, - producers.size ()); - throw - megaglest_runtime_error (szBuf); - } - if (prIndex >= (int) producers.size ()) - { - char - szBuf[8096] = ""; - printf - ("In [%s::%s Line: %d] prIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER " \n", __FILE__, - __FUNCTION__, __LINE__, prIndex, i, - producers.size ()); - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] currentProducerIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " - MG_SIZE_T_SPECIFIER "", __FILE__, - __FUNCTION__, __LINE__, - currentProducerIndex, i, - producers.size ()); - throw - megaglest_runtime_error (szBuf); - } - - currentCommandCount = - aiInterface->getMyUnit (currentProducerIndex)-> - getCommandSize (); - if (currentCommandCount == 1 - && aiInterface->getMyUnit (currentProducerIndex)-> - getCurrCommand ()->getCommandType ()->getClass () == - ccStop) - { // special for non buildings - currentCommandCount = 0; - } - if (lowestCommandCount > currentCommandCount) - { - lowestCommandCount = - aiInterface->getMyUnit (currentProducerIndex)-> - getCommandSize (); - bestIndex = currentProducerIndex; - //besti=i%(producers.size()); - } - } - if (bestIndex >= 0) - { - if (aiInterface->getMyUnit (bestIndex)-> - getCommandSize () > 2) - { - // maybe we need another producer of this kind if possible! - if (aiInterface-> - reqsOk (aiInterface->getMyUnit (bestIndex)-> - getType ())) - { - if (ai->getCountOfClass (ucBuilding) > 5) - { - ai-> - addTask (new - BuildTask (aiInterface-> - getMyUnit - (bestIndex)-> - getType ())); - } - } - // need to calculate another producer, maybe its better to produce another warrior with another producer - vector < int > - backupProducers; - // find another producer unit which is free and produce any kind of warrior. - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); - ++i) - { - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - //for each command - for (int j = 0; j < ut->getCommandTypeCount (); - ++j) - { - const CommandType * - ct = ut->getCommandType (j); - //if the command is produce - if (ct->getClass () == ccProduce) - { - const UnitType * - unitType = static_cast < const - UnitType * >(ct->getProduced ()); - if (unitType->hasSkillClass (scAttack) - && !unitType-> - hasCommandClass (ccHarvest) - && aiInterface->reqsOk (ct)) - { //this can produce a warrior - backupProducers.push_back (i); - } - } - } - } - if (!backupProducers.empty ()) - { - int - randomstart = - ai->getRandom ()->randRange (0, - (int) - backupProducers. - size () - 1); - int - lowestCommandCount = 1000000; - int - currentProducerIndex = - backupProducers[randomstart]; - int - bestIndex = -1; - for (unsigned int i = randomstart; - i < backupProducers.size () + randomstart; - i++) - { - int - prIndex = i; - if (i >= backupProducers.size ()) - { - prIndex = - (i - (int) backupProducers.size ()); - } - - currentProducerIndex = - backupProducers[prIndex]; - - if (currentProducerIndex >= - aiInterface->getMyUnitCount ()) - { - char - szBuf[8096] = ""; - printf - ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " - MG_SIZE_T_SPECIFIER "\n", __FILE__, - __FUNCTION__, __LINE__, - currentProducerIndex, - aiInterface->getMyUnitCount (), i, - backupProducers.size ()); - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " - MG_SIZE_T_SPECIFIER "", - __FILE__, __FUNCTION__, - __LINE__, - currentProducerIndex, - aiInterface-> - getMyUnitCount (), i, - backupProducers.size ()); - throw - megaglest_runtime_error (szBuf); - } - if (prIndex >= - (int) backupProducers.size ()) - { - char - szBuf[8096] = ""; - printf - ("In [%s::%s Line: %d] prIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " - MG_SIZE_T_SPECIFIER " \n", __FILE__, - __FUNCTION__, __LINE__, prIndex, i, - backupProducers.size ()); - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] currentProducerIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " - MG_SIZE_T_SPECIFIER "", - __FILE__, __FUNCTION__, - __LINE__, - currentProducerIndex, i, - backupProducers.size ()); - throw - megaglest_runtime_error (szBuf); - } - - int - currentCommandCount = - aiInterface-> - getMyUnit (currentProducerIndex)-> - getCommandSize (); - if (currentCommandCount == 1 - && aiInterface-> - getMyUnit (currentProducerIndex)-> - getCurrCommand ()->getCommandType ()-> - getClass () == ccStop) - { // special for non buildings - currentCommandCount = 0; - } - if (lowestCommandCount > - currentCommandCount) - { - lowestCommandCount = - currentCommandCount; - bestIndex = currentProducerIndex; - if (lowestCommandCount == 0) - break; - } - } - // a good producer is found, lets choose a warrior production - vector < int > - productionCommandIndexes; - if (bestIndex >= 0) - { - const UnitType * - ut = - aiInterface->getMyUnit (bestIndex)-> - getType (); - for (int j = 0; - j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is produce - if (ct->getClass () == ccProduce) - { - const UnitType * - unitType = static_cast < const - UnitType * >(ct->getProduced ()); - if (unitType-> - hasSkillClass (scAttack) - && !unitType-> - hasCommandClass (ccHarvest) - && aiInterface->reqsOk (ct)) - { //this can produce a warrior - productionCommandIndexes. - push_back (j); - } - } - } - - int - commandIndex = - productionCommandIndexes[ai-> - getRandom ()-> - randRange (0, - (int) - productionCommandIndexes. - size - () - - 1)]; - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, - __FUNCTION__, - __LINE__); - - if (ai->outputAIBehaviourToConsole ()) - printf - ("zeta #1 produceSpecific giveCommand to unit [%s] commandType [%s]\n", - aiInterface->getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - ut->getCommandType (commandIndex)-> - getName ().c_str ()); - if (aiInterface->isLogLevelEnabled (4) == - true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "zeta #1 produceSpecific giveCommand to unit [%s] commandType [%s]", - aiInterface-> - getMyUnit (bestIndex)-> - getType ()->getName (). - c_str (), - ut-> - getCommandType - (commandIndex)->getName (). - c_str ()); - aiInterface->printLog (4, szBuf); - } - - aiInterface->giveCommand (bestIndex, - ut-> - getCommandType - (commandIndex)); - } - } - else - { // do it like normal CPU - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, - __FUNCTION__, - __LINE__); - defCt = NULL; - if (producersDefaultCommandType. - find (bestIndex) != - producersDefaultCommandType.end ()) - { - int - bestCommandTypeCount = - (int) - producersDefaultCommandType[bestIndex]. - size (); - int - bestCommandTypeIndex = - ai->getRandom ()->randRange (0, - bestCommandTypeCount - - 1); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - bestCommandTypeIndex, - bestCommandTypeCount); - - defCt = - producersDefaultCommandType[bestIndex] - [bestCommandTypeIndex]; - } - - if (ai->outputAIBehaviourToConsole ()) - printf - ("zeta #2 produceSpecific giveCommand to unit [%s] commandType [%s]\n", - aiInterface->getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "n/a")); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "zeta #2 produceSpecific giveCommand to unit [%s] commandType [%s]", - aiInterface-> - getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "n/a")); - aiInterface->printLog (4, szBuf); - } - aiInterface->giveCommand (bestIndex, defCt); - } - } - else - { - if (currentCommandCount == 0) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, - __FUNCTION__, - __LINE__); - defCt = NULL; - if (producersDefaultCommandType. - find (bestIndex) != - producersDefaultCommandType.end ()) - { - //defCt = producersDefaultCommandType[bestIndex]; - int - bestCommandTypeCount = - (int) - producersDefaultCommandType[bestIndex]. - size (); - int - bestCommandTypeIndex = - ai->getRandom ()->randRange (0, - bestCommandTypeCount - - 1); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - bestCommandTypeIndex, - bestCommandTypeCount); - - defCt = - producersDefaultCommandType[bestIndex] - [bestCommandTypeIndex]; - } - - if (ai->outputAIBehaviourToConsole ()) - printf - ("zeta #3 produceSpecific giveCommand to unit [%s] commandType [%s]\n", - aiInterface->getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "n/a")); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "zeta #3 produceSpecific giveCommand to unit [%s] commandType [%s]", - aiInterface-> - getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "n/a")); - aiInterface->printLog (4, szBuf); - } - - aiInterface->giveCommand (bestIndex, defCt); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, __FUNCTION__, - __LINE__); - defCt = NULL; - if (producersDefaultCommandType.find (bestIndex) != - producersDefaultCommandType.end ()) - { - //defCt = producersDefaultCommandType[bestIndex]; - int - bestCommandTypeCount = - (int) producersDefaultCommandType[bestIndex]. - size (); - int - bestCommandTypeIndex = - ai->getRandom ()->randRange (0, - bestCommandTypeCount - - 1); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - bestCommandTypeIndex, - bestCommandTypeCount); - - defCt = - producersDefaultCommandType[bestIndex] - [bestCommandTypeIndex]; - } - if (ai->outputAIBehaviourToConsole ()) - printf - ("zeta #4 produceSpecific giveCommand to unit [%s] commandType [%s]\n", - aiInterface->getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName ().c_str () : "n/a")); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "zeta #4 produceSpecific giveCommand to unit [%s] commandType [%s]", - aiInterface->getMyUnit (bestIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "n/a")); - aiInterface->printLog (4, szBuf); - } - - aiInterface->giveCommand (bestIndex, defCt); - } - } - } - else - { - int - pIndex = - ai->getRandom ()->randRange (0, - (int) producers.size () - 1); - int - producerIndex = producers[pIndex]; - defCt = NULL; - if (producersDefaultCommandType.find (producerIndex) != - producersDefaultCommandType.end ()) - { - //defCt = producersDefaultCommandType[producerIndex]; - int - bestCommandTypeCount = - (int) producersDefaultCommandType[producerIndex]. - size (); - int - bestCommandTypeIndex = - ai->getRandom ()->randRange (0, - bestCommandTypeCount - - 1); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", - __FILE__, __FUNCTION__, - __LINE__, - bestCommandTypeIndex, - bestCommandTypeCount); - - defCt = - producersDefaultCommandType[producerIndex] - [bestCommandTypeIndex]; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] producers.size() = %d, producerIndex = %d, pIndex = %d, producersDefaultCommandType.size() = %d\n", - __FILE__, __FUNCTION__, - __LINE__, producers.size (), - producerIndex, pIndex, - producersDefaultCommandType. - size ()); - - if (ai->outputAIBehaviourToConsole ()) - printf - ("produceSpecific giveCommand to unit [%s] commandType [%s]\n", - aiInterface->getMyUnit (producerIndex)->getType ()-> - getName ().c_str (), - (defCt != NULL ? defCt->getName ().c_str () : "n/a")); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "produceSpecific giveCommand to unit [%s] commandType [%s]", - aiInterface->getMyUnit (producerIndex)-> - getType ()->getName ().c_str (), - (defCt != - NULL ? defCt->getName (). - c_str () : "(null)")); - aiInterface->printLog (4, szBuf); - } - aiInterface->giveCommand (producerIndex, defCt); - } - } - } - } - -// ======================================== -// class AiRuleBuild -// ======================================== - - AiRuleBuild::AiRuleBuild (Ai * ai): - AiRule (ai) - { - buildTask = NULL; - } - - bool - AiRuleBuild::test () - { - const Task * - task = ai->getTask (); - - if (task == NULL || task->getClass () != tcBuild) - { - return false; - } - - buildTask = static_cast < const BuildTask *>(task); - return true; - } - - - void - AiRuleBuild::execute () - { - if (buildTask != NULL) - { - if (ai->outputAIBehaviourToConsole ()) - printf ("BUILD AiRuleBuild Unit Name[%s]\n", - (buildTask->getUnitType () != - NULL ? buildTask->getUnitType ()->getName (false). - c_str () : "null")); - - //generic build task, build random building that can be built - if (buildTask->getUnitType () == NULL) - { - buildGeneric (buildTask); - } - //specific building task, build if possible, retry if not enough resources or not position - else - { - buildSpecific (buildTask); - } - - //remove the task - ai->removeTask (buildTask); - } - } - - void - AiRuleBuild::buildGeneric (const BuildTask * bt) - { - - //find buildings that can be built - AiInterface * - aiInterface = ai->getAiInterface (); - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "== START: buildGeneric for resource type [%s]", - (bt->getResourceType () != - NULL ? bt->getResourceType ()->getName (). - c_str () : "null")); - aiInterface->printLog (4, szBuf); - } - - typedef - vector < const UnitType *> - UnitTypes; - UnitTypes - buildings; - - if (bt->getResourceType () != NULL) - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcResourceProducerUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, true, false, - false) == true) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildGeneric for resource type [%s] aibcResourceProducerUnits = " - MG_SIZE_T_SPECIFIER - " priorityUnit.first: [%s]\n", - bt->getResourceType ()->getName (). - c_str (), unitList.size (), - priorityUnit.first->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (priorityUnit.first)); - return; - //} - } - } - } - } - else - { - if (aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcBuildingUnits).size () > 0) - { - const - std::vector < - FactionType::PairPUnitTypeInt > & - unitList = - aiInterface->getMyFactionType ()-> - getAIBehaviorUnits (aibcBuildingUnits); - for (unsigned int i = 0; i < unitList.size (); ++i) - { - const - FactionType::PairPUnitTypeInt & - priorityUnit = unitList[i]; - if (ai->getCountOfType (priorityUnit.first) < - priorityUnit.second - && aiInterface->getMyFaction ()-> - canCreateUnit (priorityUnit.first, true, false, - false) == true) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildGeneric for resource type [%s] aibcBuildingUnits = " - MG_SIZE_T_SPECIFIER - " priorityUnit.first: [%s]\n", - bt->getResourceType ()->getName (). - c_str (), unitList.size (), - priorityUnit.first->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (priorityUnit.first)); - return; - //} - } - } - } - } - - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is build - if (ct->getClass () == ccBuild) - { - const BuildCommandType * - bct = static_cast < const - BuildCommandType * >(ct); - - //for each building - for (int k = 0; k < bct->getBuildingCount (); ++k) - { - const UnitType * - building = bct->getBuilding (k); - if (aiInterface->reqsOk (bct) - && aiInterface->reqsOk (building)) - { - - //if any building, or produces resource - const ResourceType * - rt = bt->getResourceType (); - const Resource * - cost = building->getCost (rt); - if (rt == NULL - || (cost != NULL && cost->getAmount () < 0)) - { - if (find - (buildings.begin (), buildings.end (), - building) == buildings.end ()) - { - buildings.push_back (building); - } - } - } - } - } - } - } - - if (aiInterface->isLogLevelEnabled (4) == true) - { - for (int i = 0; i < (int) buildings.size (); ++i) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildGeneric i = %d unit type: [%s]\n", i, - buildings[i]->getName ().c_str ()); - aiInterface->printLog (4, szBuf); - } - } - - //add specific build task - buildBestBuilding (buildings); - } - - void - AiRuleBuild::buildBestBuilding (const vector < - const UnitType * >&buildings) - { - - AiInterface * - aiInterface = ai->getAiInterface (); - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "==> START buildBestBuilding buildings.size = " - MG_SIZE_T_SPECIFIER "\n", buildings.size ()); - aiInterface->printLog (4, szBuf); - } - - if (!buildings.empty ()) - { - - //build the least built building - bool - buildingFound = false; - for (int i = 0; i < 10 && !buildingFound; ++i) - { - - if (i > 0) - { - - //Defensive buildings have priority - for (int j = 0; - j < (int) buildings.size () && buildingFound == false; - ++j) - { - const UnitType * - building = buildings[j]; - if (ai->getCountOfType (building) <= i + 1 - && isDefensive (building)) - { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildBestBuilding defensive building unit type: [%s] i = %d j = %d\n", - building->getName ().c_str (), i, j); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (building)); - buildingFound = true; - } - } - - //Warrior producers next - for (unsigned int j = 0; - j < buildings.size () && !buildingFound; ++j) - { - const UnitType * - building = buildings[j]; - if (ai->getCountOfType (building) <= i + 1 - && isWarriorProducer (building)) - { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildBestBuilding warriorproducer building unit type: [%s] i = %d j = %u\n", - building->getName ().c_str (), i, j); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (building)); - buildingFound = true; - } - } - - //Resource producers next - for (unsigned int j = 0; - j < buildings.size () && !buildingFound; ++j) - { - const UnitType * - building = buildings[j]; - if (ai->getCountOfType (building) <= i + 1 - && isResourceProducer (building)) - { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildBestBuilding resourceproducer building unit type: [%s] i = %d j = %u\n", - building->getName ().c_str (), i, j); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (building)); - buildingFound = true; - } - } - } - - //Any building - for (unsigned int j = 0; - j < buildings.size () && !buildingFound; ++j) - { - const UnitType * - building = buildings[j]; - if (ai->getCountOfType (building) <= i) - { - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildBestBuilding ANY building unit type: [%s] i = %d j = %u\n", - building->getName ().c_str (), i, j); - aiInterface->printLog (4, szBuf); - } - - ai->addTask (new BuildTask (building)); - buildingFound = true; - } - } - } - } - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "==> END buildBestBuilding buildings.size = " - MG_SIZE_T_SPECIFIER "\n", buildings.size ()); - aiInterface->printLog (4, szBuf); - } - } - - void - AiRuleBuild::buildSpecific (const BuildTask * bt) - { - AiInterface * - aiInterface = ai->getAiInterface (); - - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "== START: buildSpecific for resource type [%s] bt->getUnitType() [%s]", - (bt->getResourceType () != - NULL ? bt->getResourceType ()->getName (). - c_str () : "null"), - (bt->getUnitType () != - NULL ? bt->getUnitType ()->getName (false). - c_str () : "null")); - aiInterface->printLog (4, szBuf); - } - - //if reqs ok - if (aiInterface->reqsOk (bt->getUnitType ())) - { - - //retry if not enough resources - if (aiInterface->checkCosts (bt->getUnitType (), NULL) == false) - { - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildSpecific for resource type [%s] checkcosts == false RETRYING", - (bt->getResourceType () != - NULL ? bt->getResourceType ()->getName (). - c_str () : "null")); - aiInterface->printLog (4, szBuf); - } - - ai->retryTask (bt); - return; - } - - vector < int > - builders; - // Hold a list of units which can build - // then a list of build commandtypes for each unit - map < int, - vector < const BuildCommandType *> > - buildersDefaultCommandType; - const BuildCommandType * - defBct = NULL; - - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - - //if the unit is not going to build - const Unit * - u = aiInterface->getMyUnit (i); - if (u->anyCommand () == false - || u->getCurrCommand ()->getCommandType ()->getClass () != - ccBuild) - { - - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is build - if (ct->getClass () == ccBuild) - { - const BuildCommandType * - bct = static_cast < const - BuildCommandType * >(ct); - - //for each building - for (int k = 0; k < bct->getBuildingCount (); ++k) - { - const UnitType * - building = bct->getBuilding (k); - - //if building match - if (bt->getUnitType () == building) - { - if (aiInterface->reqsOk (bct)) - { - builders.push_back (i); - buildersDefaultCommandType[i]. - push_back (bct); - //defBct= bct; - } - } - } - } - } - } - } - - //use random builder to build - if (builders.empty () == false) - { - int - bIndex = - ai->getRandom ()->randRange (0, (int) builders.size () - 1); - int - builderIndex = builders[bIndex]; - Vec2i - pos; - Vec2i - searchPos = - bt->getForcePos ()? bt->getPos () : ai-> - getRandomHomePosition (); - if (bt->getForcePos () == false) - { - const int - enemySightDistanceToAvoid = 18; - vector < Unit * >enemies; - ai->getAiInterface ()->getWorld ()->getUnitUpdater ()-> - findEnemiesForCell (searchPos, - bt->getUnitType ()->getSize (), - enemySightDistanceToAvoid, - ai->getAiInterface ()-> - getMyFaction (), enemies, true); - if (enemies.empty () == false) - { - for (int i1 = 0; i1 < 25 && enemies.empty () == false; - ++i1) - { - for (int j1 = 0; - j1 < 25 && enemies.empty () == false; ++j1) - { - Vec2i - tryPos = searchPos + Vec2i (i1, j1); - - const int - spacing = 1; - if (ai->getAiInterface ()-> - isFreeCells (tryPos - Vec2i (spacing), - bt->getUnitType ()-> - getSize () + spacing * 2, - fLand)) - { - enemies.clear (); - ai->getAiInterface ()->getWorld ()-> - getUnitUpdater ()-> - findEnemiesForCell (tryPos, - bt->getUnitType ()-> - getSize (), - enemySightDistanceToAvoid, - ai-> - getAiInterface ()-> - getMyFaction (), - enemies, true); - if (enemies.empty () == true) - { - searchPos = tryPos; - } - } - } - } - } - if (enemies.empty () == false) - { - for (int i1 = -1; - i1 >= -25 && enemies.empty () == false; --i1) - { - for (int j1 = -1; - j1 >= -25 && enemies.empty () == false; --j1) - { - Vec2i - tryPos = searchPos + Vec2i (i1, j1); - - const int - spacing = 1; - if (ai->getAiInterface ()-> - isFreeCells (tryPos - Vec2i (spacing), - bt->getUnitType ()-> - getSize () + spacing * 2, - fLand)) - { - enemies.clear (); - ai->getAiInterface ()->getWorld ()-> - getUnitUpdater ()-> - findEnemiesForCell (tryPos, - bt->getUnitType ()-> - getSize (), - enemySightDistanceToAvoid, - ai-> - getAiInterface ()-> - getMyFaction (), - enemies, true); - if (enemies.empty () == true) - { - searchPos = tryPos; - } - } - } - } - } - } - - //if free pos give command, else retry - if (ai->findPosForBuilding (bt->getUnitType (), searchPos, pos)) - { - defBct = NULL; - if (buildersDefaultCommandType.find (builderIndex) != - buildersDefaultCommandType.end ()) - { - //defBct = buildersDefaultCommandType[builderIndex]; - int - bestCommandTypeCount = - (int) buildersDefaultCommandType[builderIndex]. - size (); - int - bestCommandTypeIndex = - ai->getRandom ()->randRange (0, - bestCommandTypeCount - - 1); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", - __FILE__, __FUNCTION__, - __LINE__, - bestCommandTypeIndex, - bestCommandTypeCount); - - defBct = - buildersDefaultCommandType[builderIndex] - [bestCommandTypeIndex]; - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n", - __FILE__, __FUNCTION__, - __LINE__, builderIndex, bIndex, - defBct); - - aiInterface->giveCommand (builderIndex, defBct, pos, - bt->getUnitType ()); - } - else - { - ai->retryTask (bt); - return; - } - } - } - else - { - if (aiInterface->isLogLevelEnabled (4) == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In buildSpecific for resource type [%s] reqsok == false", - (bt->getResourceType () != - NULL ? bt->getResourceType ()->getName (). - c_str () : "null")); - aiInterface->printLog (4, szBuf); - } - - } - } - - bool - AiRuleBuild::isDefensive (const UnitType * building) - { - if (ai->outputAIBehaviourToConsole ()) - printf ("BUILD isDefensive check for Unit Name[%s] result = %d\n", - building->getName (false).c_str (), - building->hasSkillClass (scAttack)); - - return building->hasSkillClass (scAttack); - } - - bool - AiRuleBuild::isResourceProducer (const UnitType * building) - { - for (int i = 0; i < building->getCostCount (); i++) - { - if (building->getCost (i)->getAmount () < 0) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("BUILD isResourceProducer check for Unit Name[%s] result = true\n", - building->getName (false).c_str ()); - - return true; - } - } - if (ai->outputAIBehaviourToConsole ()) - printf - ("BUILD isResourceProducer check for Unit Name[%s] result = false\n", - building->getName (false).c_str ()); - - return false; - } - - bool - AiRuleBuild::isWarriorProducer (const UnitType * building) - { - for (int i = 0; i < building->getCommandTypeCount (); i++) - { - const CommandType * - ct = building->getCommandType (i); - if (ct->getClass () == ccProduce) - { - const UnitType * - ut = static_cast < const - ProduceCommandType * >(ct)-> - getProducedUnit (); - - if (ut->isOfClass (ucWarrior)) - { - if (ai->outputAIBehaviourToConsole ()) - printf - ("BUILD isWarriorProducer check for Unit Name[%s] result = true\n", - building->getName (false).c_str ()); - - return true; - } - } - } - if (ai->outputAIBehaviourToConsole ()) - printf - ("BUILD isWarriorProducer check for Unit Name[%s] result = false\n", - building->getName (false).c_str ()); - - return false; - } - -// ======================================== -// class AiRuleUpgrade -// ======================================== - - AiRuleUpgrade::AiRuleUpgrade (Ai * ai): - AiRule (ai) - { - upgradeTask = NULL; - } - - bool - AiRuleUpgrade::test () - { - const Task * - task = ai->getTask (); - - if (task == NULL || task->getClass () != tcUpgrade) - { - return false; - } - - upgradeTask = static_cast < const UpgradeTask *>(task); - return true; - } - - void - AiRuleUpgrade::execute () - { - - //upgrade any upgrade - if (upgradeTask->getUpgradeType () == NULL) - { - upgradeGeneric (upgradeTask); - } - //upgrade specific upgrade - else - { - upgradeSpecific (upgradeTask); - } - - //remove the task - ai->removeTask (upgradeTask); - } - - void - AiRuleUpgrade::upgradeGeneric (const UpgradeTask * upgt) - { - - typedef - vector < const UpgradeType *> - UpgradeTypes; - AiInterface * - aiInterface = ai->getAiInterface (); - - //find upgrades that can be upgraded - UpgradeTypes - upgrades; - - if (aiInterface->getMyFactionType ()->getAIBehaviorUpgrades ().size () > - 0) - { - const - std::vector < const UpgradeType *>& - upgradeList = - aiInterface->getMyFactionType ()->getAIBehaviorUpgrades (); - for (unsigned int i = 0; i < upgradeList.size (); ++i) - { - const UpgradeType * - priorityUpgrade = upgradeList[i]; - - //for each upgrade, upgrade it if possible - for (int k = 0; k < aiInterface->getMyUnitCount (); ++k) - { - - //for each command - const UnitType * - ut = aiInterface->getMyUnit (k)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is upgrade - if (ct->getClass () == ccUpgrade) - { - const UpgradeCommandType * - upgct = dynamic_cast < const - UpgradeCommandType * >(ct); - if (upgct != NULL) - { - const UpgradeType * - upgrade = upgct->getProducedUpgrade (); - if (upgrade == priorityUpgrade) - { - if (aiInterface->reqsOk (upgct) == true && - aiInterface->getMyFaction ()-> - getUpgradeManager ()-> - isUpgradingOrUpgraded (priorityUpgrade) - == false) - { - //if(ai->getRandom()->randRange(0, 1)==0) { - ai-> - addTask (new - UpgradeTask - (priorityUpgrade)); - return; - //} - } - } - } - } - } - } - } - } - - //for each upgrade, upgrade it if possible - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is upgrade - if (ct->getClass () == ccUpgrade) - { - const UpgradeCommandType * - upgct = static_cast < const - UpgradeCommandType * >(ct); - const UpgradeType * - upgrade = upgct->getProducedUpgrade (); - if (aiInterface->reqsOk (upgct)) - { - upgrades.push_back (upgrade); - } - } - } - } - - //add specific upgrade task - if (!upgrades.empty ()) - { - ai-> - addTask (new - UpgradeTask (upgrades - [ai->getRandom ()-> - randRange (0, - (int) upgrades.size () - 1)])); - } - } - - void - AiRuleUpgrade::upgradeSpecific (const UpgradeTask * upgt) - { - - AiInterface * - aiInterface = ai->getAiInterface (); - - //if reqs ok - if (aiInterface->reqsOk (upgt->getUpgradeType ())) - { - - //if resources dont meet retry - if (!aiInterface->checkCosts (upgt->getUpgradeType (), NULL)) - { - ai->retryTask (upgt); - return; - } - - //for each unit - for (int i = 0; i < aiInterface->getMyUnitCount (); ++i) - { - - //for each command - const UnitType * - ut = aiInterface->getMyUnit (i)->getType (); - for (int j = 0; j < ut->getCommandTypeCount (); ++j) - { - const CommandType * - ct = ut->getCommandType (j); - - //if the command is upgrade - if (ct->getClass () == ccUpgrade) - { - const UpgradeCommandType * - uct = static_cast < const - UpgradeCommandType * >(ct); - const UpgradeType * - producedUpgrade = uct->getProducedUpgrade (); - - //if upgrades match - if (producedUpgrade == upgt->getUpgradeType ()) - { - if (aiInterface->reqsOk (uct)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugSystem, - "In [%s::%s Line: %d]\n", - __FILE__, - __FUNCTION__, - __LINE__); - aiInterface->giveCommand (i, uct); - } - } - } - } - } - - } - } - -// ======================================== -// class AiRuleExpand -// ======================================== - - AiRuleExpand::AiRuleExpand (Ai * ai): - AiRule (ai) - { - storeType = NULL; - } - - bool - AiRuleExpand::test () - { - AiInterface * - aiInterface = ai->getAiInterface (); - - int - unitCount = aiInterface->getMyUnitCount (); - for (int i = 0; - i < aiInterface->getTechTree ()->getResourceTypeCount (); ++i) - { - const ResourceType * - rt = aiInterface->getTechTree ()->getResourceType (i); - if (rt->getClass () == rcTech) - { - bool - factionUsesResourceType = - aiInterface->factionUsesResourceType (aiInterface-> - getMyFactionType (), - rt); - if (factionUsesResourceType == true) - { - // If any resource sighted - if (aiInterface-> - getNearestSightedResource (rt, - aiInterface-> - getHomeLocation (), - expandPos, true)) - { - int - minDistance = INT_MAX; - storeType = NULL; - - //If there is no close store - for (int j = 0; j < unitCount; ++j) - { - const Unit * - u = aiInterface->getMyUnit (j); - const UnitType * - ut = u->getType (); - - // If this building is a store - if (ut->getStore (rt) > 0) - { - storeType = ut; - int - distance = - static_cast < - int >(u->getPosNotThreadSafe (). - dist (expandPos)); - if (distance < minDistance) - { - minDistance = distance; - } - } - } - - if (minDistance > expandDistance) - { - return true; - } - } - else - { - // send patrol to look for resource - ai->sendScoutPatrol (); - } - } - } - } - - return false; - } - - void - AiRuleExpand::execute () - { - ai->addExpansion (expandPos); - ai->addPriorityTask (new BuildTask (storeType, expandPos)); - } - - -// ======================================== -// class AiRuleUnBlock -// ======================================== - - AiRuleUnBlock::AiRuleUnBlock (Ai * ai): - AiRule (ai) - { - - } - - bool - AiRuleUnBlock::test () - { - return ai->haveBlockedUnits (); - } - - void - AiRuleUnBlock::execute () - { - ai->unblockUnits (); - } - -}} //end namespace + Glest { + namespace + Game { + + // ===================================================== + // class AiRule + // ===================================================== + + AiRule::AiRule(Ai * ai) { + this->ai = ai; + } + + // ===================================================== + // class AiRuleWorkerHarvest + // ===================================================== + + AiRuleWorkerHarvest::AiRuleWorkerHarvest(Ai * ai) : + AiRule(ai) { + stoppedWorkerIndex = -1; + } + + bool + AiRuleWorkerHarvest::test() { + return ai->findAbleUnit(&stoppedWorkerIndex, ccHarvest, true); + } + + void + AiRuleWorkerHarvest::execute() { + ai->harvest(stoppedWorkerIndex); + } + + // ===================================================== + // class AiRuleRefreshHarvester + // ===================================================== + + AiRuleRefreshHarvester::AiRuleRefreshHarvester(Ai * ai) : + AiRule(ai) { + workerIndex = -1; + } + + bool + AiRuleRefreshHarvester::test() { + return ai->findAbleUnit(&workerIndex, ccHarvest, ccHarvest); + } + + void + AiRuleRefreshHarvester::execute() { + ai->harvest(workerIndex); + } + + // ===================================================== + // class AiRuleScoutPatrol + // ===================================================== + + AiRuleScoutPatrol::AiRuleScoutPatrol(Ai * ai) : + AiRule(ai) { + } + + bool + AiRuleScoutPatrol::test() { + return ai->isStableBase(); + } + + void + AiRuleScoutPatrol::execute() { + ai->sendScoutPatrol(); + } + // ===================================================== + // class AiRuleRepair + // ===================================================== + + AiRuleRepair::AiRuleRepair(Ai * ai) : + AiRule(ai) { + damagedUnitIndex = 0; + damagedUnitIsCastle = false; + } + + double + AiRuleRepair::getMinCastleHpRatio() const { + return + 0.6; + } + + int + AiRuleRepair::getMinUnitsToRepairCastle() { + int + minUnitsRepairingCastle = 7; + if (ai->getCountOfClass(ucWorker) <= 6) { + minUnitsRepairingCastle = 1; + } else if (ai->getCountOfClass(ucWorker) <= 8) { + minUnitsRepairingCastle = 2; + } else if (ai->getCountOfClass(ucWorker) <= 10) { + minUnitsRepairingCastle = 3; + } else if (ai->getCountOfClass(ucWorker) <= 12) { + minUnitsRepairingCastle = 5; + } + return minUnitsRepairingCastle; + } + + bool + AiRuleRepair::test() { + AiInterface * + aiInterface = ai->getAiInterface(); + + int + minUnitsRepairingCastle = getMinUnitsToRepairCastle(); + const double + minCastleHpRatio = getMinCastleHpRatio(); + + // look for a damaged unit and give priority to the factions bases + // (units that produce workers and store resources) + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + u = aiInterface->getMyUnit(i); + //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); + if (u->getHpRatio() < 1.f) { + //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); + + bool + unitCanProduceWorker = false; + for (int j = 0; unitCanProduceWorker == false && + j < u->getType()->getCommandTypeCount(); ++j) { + const CommandType * + ct = u->getType()->getCommandType(j); + + //if the command is produce + if (ct->getClass() == ccProduce + || ct->getClass() == ccMorph) { + const ProducibleType * + pt = ct->getProduced(); + if (pt != NULL) { + const UnitType * + ut = dynamic_cast (pt); + if (ut != NULL + && ut->hasCommandClass(ccHarvest) == true + && u->getType()->getStoredResourceCount() > 0) { + //printf("\n\n\n\n!!!!!! found candidate castle unit to repair [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); + unitCanProduceWorker = true; + } + } + } + } + + if (unitCanProduceWorker == true) { + int + candidatedamagedUnitIndex = -1; + int + unitCountAlreadyRepairingDamagedUnit = 0; + // Now check if any other unit is able to repair this unit + for (int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + const Unit * + u1 = aiInterface->getMyUnit(i1); + const RepairCommandType * + rct = static_cast (u1->getType()->getFirstCtOfClass(ccRepair)); + //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); + + if (rct != NULL) { + //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); + + if (u1->getCurrSkill()->getClass() == scStop + || u1->getCurrSkill()->getClass() == scMove + || u->getHpRatio() <= minCastleHpRatio) { + if (rct->isRepairableUnitType(u->getType())) { + candidatedamagedUnitIndex = i; + //return true; + } + } else if (u1->getCurrSkill()->getClass() == + scRepair) { + Command * + cmd = u1->getCurrCommand(); + if (cmd != NULL + && cmd->getCommandType()->getClass() == + ccRepair) { + if (cmd->getUnit() != NULL + && cmd->getUnit()->getId() == + u->getId()) { + //printf("\n\n\n\n^^^^^^^^^^ unit is ALREADY repairer unit [%d - %s]\n",u1->getId(),u1->getType()->getName().c_str()); + unitCountAlreadyRepairingDamagedUnit++; + } + } + } + } + } + + if (candidatedamagedUnitIndex >= 0 + && unitCountAlreadyRepairingDamagedUnit < + minUnitsRepairingCastle) { + //printf("\n\n\n\n^^^^^^^^^^ AI test will repair damaged unit [%d - %s]\n",u->getId(),u->getType()->getName().c_str()); + damagedUnitIndex = candidatedamagedUnitIndex; + damagedUnitIsCastle = true; + return true; + } + } + } + } + damagedUnitIsCastle = false; + damagedUnitIndex = -1; + // Normal Repair checking + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + u = aiInterface->getMyUnit(i); + //printf("\n\n\n\n!!!!!! Is damaged unit [%d - %s] u->getHpRatio() = %f, hp = %d, mapHp = %d\n",u->getId(),u->getType()->getName().c_str(),u->getHpRatio(),u->getHp(),u->getType()->getTotalMaxHp(u->getTotalUpgrade())); + if (u->getHpRatio() < 1.f) { + // Now check if any other unit is able to repair this unit + for (int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + const Unit * + u1 = aiInterface->getMyUnit(i1); + const RepairCommandType * + rct = static_cast (u1->getType()->getFirstCtOfClass(ccRepair)); + //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType())); + + if (rct != NULL + && (u1->getCurrSkill()->getClass() == scStop + || u1->getCurrSkill()->getClass() == scMove)) { + if (rct->isRepairableUnitType(u->getType())) { + damagedUnitIndex = i; + //random if return or not so we get different targets from time to time + if (ai->getRandom()->randRange(0, 1) == 0) + return true; + } + } + } + } + } + if (damagedUnitIndex != -1) { + return true; + } + return false; + } + + void + AiRuleRepair::execute() { + AiInterface * + aiInterface = ai->getAiInterface(); + const Unit * + damagedUnit = aiInterface->getMyUnit(damagedUnitIndex); + //printf("\n\n\n\n###^^^^^^^^^^ Looking for repairer for damaged unit [%d - %s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str()); + + int + minUnitsRepairingCastle = getMinUnitsToRepairCastle(); + const double + minCastleHpRatio = getMinCastleHpRatio(); + + if (minUnitsRepairingCastle > 2) { + if (damagedUnit->getCurrSkill()->getClass() == scBeBuilt) { // if build is still be build 2 helpers are enough + minUnitsRepairingCastle = 2; + } + + if (!damagedUnitIsCastle) { + minUnitsRepairingCastle = 2; + } + } + if (aiInterface->getControlType() == ctCpuEasy || + aiInterface->getControlType() == ctNetworkCpuEasy) { + if (!damagedUnitIsCastle) { + // cpu easy does not repair! + minUnitsRepairingCastle = 0; + } + } + if (aiInterface->getControlType() == ctCpu || + aiInterface->getControlType() == ctNetworkCpu) { + if (!damagedUnitIsCastle) { + // cpu does only repair with one unit! + minUnitsRepairingCastle = 1; + } + } + int + unitCountAlreadyRepairingDamagedUnit = 0; + //printf("team %d has damaged unit\n", damagedUnit->getTeam()); + // Now check if any other unit is able to repair this unit + for (int i1 = 0; i1 < aiInterface->getMyUnitCount(); ++i1) { + const Unit * + u1 = aiInterface->getMyUnit(i1); + const RepairCommandType * + rct = static_cast (u1->getType()->getFirstCtOfClass(ccRepair)); + //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u1->getType())); + Command * + cmd = u1->getCurrCommand(); + if (cmd != NULL && cmd->getCommandType()->getClass() == ccRepair) { + //if(cmd->getUnit() != NULL && cmd->getUnit()->getId() == damagedUnit->getId()) + //if(cmd->getUnit() != NULL && cmd->getPos() == damagedUnit->getPosWithCellMapSet()) + + if (rct != NULL) { + //printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u1->getId(),u1->getType()->getName().c_str(),u1->getCurrSkill()->getClass(),rct->isRepairableUnitType(u->getType()),u->getHpRatio()); + + if (((RepairCommandType *) (cmd->getCommandType()))-> + isRepairableUnitType(damagedUnit->getType())) { + //printf("^^^^test^^^^^^ unit is ALREADY repairer unit [%d - %s]\nminUnitsRepairingCastle=%d\n",u1->getId(), u1->getType()->getName().c_str(), minUnitsRepairingCastle); + unitCountAlreadyRepairingDamagedUnit++; + } + } + } + } + + if (unitCountAlreadyRepairingDamagedUnit >= minUnitsRepairingCastle) { + return; + } + + int + unitGroupCommandId = -1; + + //find a repairer and issue command + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + const Unit * + u = aiInterface->getMyUnit(i); + const RepairCommandType * + rct = static_cast (u->getType()->getFirstCtOfClass(ccRepair)); + //if(rct) printf("\n\n\n\n^^^^^^^^^^ possible repairer unit [%d - %s] current skill [%d] can reapir damaged unit [%d]\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType())); + + if (rct != NULL) { + //printf("\n\n\n\n^^^^^^^^^^ possible excute repairer unit [%d - %s] current skill [%d] can repair damaged unit [%d] Castles hp-ratio = %f\n",u->getId(),u->getType()->getName().c_str(),u->getCurrSkill()->getClass(),rct->isRepairableUnitType(damagedUnit->getType()),damagedUnit->getHpRatio()); + + if ((u->getCurrSkill()->getClass() == scStop + || u->getCurrSkill()->getClass() == scMove + || damagedUnit->getHpRatio() <= minCastleHpRatio)) { + if ((u->getCurrCommand() == NULL + || (u->getCurrCommand()->getCommandType()-> + getClass() != ccBuild + && u->getCurrCommand()->getCommandType()-> + getClass() != ccProduce)) + && rct->isRepairableUnitType(damagedUnit->getType())) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("\n\n\n\n^^^^^^^^^^ AI execute will repair damaged unit [%d - %s] at pos [%s] cellmapPos [%s] using unit [%d -%s]\n",damagedUnit->getId(),damagedUnit->getType()->getName().c_str(),damagedUnit->getPos().getString().c_str(),damagedUnit->getPosWithCellMapSet().getString().c_str(),u->getId(),u->getType()->getName().c_str()); + + /* + Map *map= aiInterface->getWorld()->getMap(); + Cell *cell = map->getCell(damagedUnit->getPosWithCellMapSet()); + if(cell != NULL) { + printf("\n\n\n\n^^^^^^^^^^ cell is ok\n"); + + Unit *cellUnit = cell->getUnit(damagedUnit->getCurrField()); + if(cellUnit != NULL) { + printf("\n\n\n\n^^^^^^^^^^ cell unit [%d - %s] at pos [%s]\n",cellUnit->getId(),cellUnit->getType()->getName().c_str(),cellUnit->getPos().getString().c_str()); + } + } + */ + + //aiInterface->giveCommand(i, rct, damagedUnit->getPos()); + if (unitCountAlreadyRepairingDamagedUnit < + minUnitsRepairingCastle) { + + if (unitGroupCommandId == -1) { + unitGroupCommandId = + aiInterface->getWorld()-> + getNextCommandGroupId(); + } + + aiInterface->giveCommand(i, rct, + damagedUnit-> + getPosWithCellMapSet(), + unitGroupCommandId); + if (aiInterface->isLogLevelEnabled(3) == true) + aiInterface->printLog(3, + "Repairing order issued"); + unitCountAlreadyRepairingDamagedUnit++; + // printf( + // "^^^^^^^^^^adding one unit to repair ... unitCountAlreadyRepairingDamagedUnit/minUnitsRepairingCastle=%d/%d\n", + // unitCountAlreadyRepairingDamagedUnit, minUnitsRepairingCastle); + } + + if (!damagedUnitIsCastle + || unitCountAlreadyRepairingDamagedUnit >= + minUnitsRepairingCastle) { + return; + } + } + } + } + } + } + + // ===================================================== + // class AiRuleReturnBase + // ===================================================== + + AiRuleReturnBase::AiRuleReturnBase(Ai * ai) : + AiRule(ai) { + stoppedUnitIndex = -1; + } + + bool + AiRuleReturnBase::test() { + return ai->findAbleUnit(&stoppedUnitIndex, ccMove, true); + } + + void + AiRuleReturnBase::execute() { + ai->returnBase(stoppedUnitIndex); + } + + // ===================================================== + // class AiRuleMassiveAttack + // ===================================================== + + AiRuleMassiveAttack::AiRuleMassiveAttack(Ai * ai) : + AiRule(ai) { + ultraAttack = false; + field = fLand; + } + + bool + AiRuleMassiveAttack::test() { + + if (ai->isStableBase()) { + ultraAttack = false; + return ai->beingAttacked(attackPos, field, INT_MAX); + } else { + ultraAttack = true; + return ai->beingAttacked(attackPos, field, baseRadius); + } + } + + void + AiRuleMassiveAttack::execute() { + ai->massiveAttack(attackPos, field, ultraAttack); + } + // ===================================================== + // class AiRuleAddTasks + // ===================================================== + + AiRuleAddTasks::AiRuleAddTasks(Ai * ai) : + AiRule(ai) { + } + + bool + AiRuleAddTasks::test() { + return !ai->anyTask() || ai->getCountOfClass(ucWorker) < 4; + } + + // This function is what triggers the AI to create new units. + void + AiRuleAddTasks::execute() { + int + buildingCount = ai->getCountOfClass(ucBuilding); + UnitClass + ucWorkerType = ucWorker; + int + warriorCount = ai->getCountOfClass(ucWarrior, &ucWorkerType); + int + workerCount = ai->getCountOfClass(ucWorker); + int + upgradeCount = ai->getAiInterface()->getMyUpgradeCount(); + + float + buildingRatio = ai->getRatioOfClass(ucBuilding); + float + warriorRatio = ai->getRatioOfClass(ucWarrior); + float + workerRatio = ai->getRatioOfClass(ucWorker); + + //standard tasks + if (ai->outputAIBehaviourToConsole()) + printf + ("Add a TASK - AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", + workerCount, this->getName().c_str()); + + //emergency workers + if (workerCount < 4) { + if (ai->outputAIBehaviourToConsole()) + printf + ("AAA AiRuleAddTasks adding ProduceTask(ucWorker) workerCount = %d, RULE Name[%s]\n", + workerCount, this->getName().c_str()); + + ai->addPriorityTask(new ProduceTask(ucWorker)); + } + // The following rules are specific creation rules for different AI. + else { + if (ai->getAiInterface()->getControlType() == ctCpuZeta || + ai->getAiInterface()->getControlType() == ctNetworkCpuZeta) { + if (ai->outputAIBehaviourToConsole()) + printf + ("AAA AiRuleAddTasks adding #1 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, + warriorCount, warriorRatio, upgradeCount, + this->getName().c_str()); + + //workers + if (workerCount < 5) + ai->addTask(new ProduceTask(ucWorker)); + if (workerCount < 100) + ai->addTask(new ProduceTask(ucWorker)); + if (workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); + if (workerRatio < 0.30) + ai->addTask(new ProduceTask(ucWorker)); + + //warriors + if (warriorCount < 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorCount < ai->getMinWarriors() + 2) { + ai->addTask(new ProduceTask(ucWarrior)); + if (buildingCount > 9) { + ai->addTask(new ProduceTask(ucWarrior)); + ai->addTask(new ProduceTask(ucWarrior)); + } + if (buildingCount > 12) { + ai->addTask(new ProduceTask(ucWarrior)); + ai->addTask(new ProduceTask(ucWarrior)); + } + } + + //buildings + if (buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask((UnitType *) NULL)); + if (buildingCount < 10 && workerCount > 12) + ai->addTask(new BuildTask((UnitType *) NULL)); + //upgrades + if (upgradeCount == 0 && workerCount > 5) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 1 && workerCount > 10) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 2 && workerCount > 15) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (ai->isStableBase()) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + } else if (ai->getAiInterface()->getControlType() == ctCpuEasy || + ai->getAiInterface()->getControlType() == + ctNetworkCpuEasy) { // Easy CPU + + if (ai->outputAIBehaviourToConsole()) + printf + ("AAA AiRuleAddTasks adding #2 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, + warriorCount, warriorRatio, upgradeCount, + this->getName().c_str()); + + //workers + if (workerCount < buildingCount + 2) + ai->addTask(new ProduceTask(ucWorker)); + if (workerCount > 5 && workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); + + //warriors + if (warriorCount < 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); + + //buildings + if (buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask((UnitType *) NULL)); + if (buildingCount < 10 && ai->isStableBase()) + ai->addTask(new BuildTask((UnitType *) NULL)); + + //upgrades + if (upgradeCount == 0 && workerCount > 6) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 1 && workerCount > 7) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 2 && workerCount > 9) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + //if(ai->isStableBase()) ai->addTask(new UpgradeTask()); + } else { // normal CPU / UltraCPU ... + if (ai->outputAIBehaviourToConsole()) + printf + ("AAA AiRuleAddTasks adding #3 workerCount = %d[%.2f], buildingCount = %d[%.2f] warriorCount = %d[%.2f] upgradeCount = %d RULE Name[%s]\n", + workerCount, workerRatio, buildingCount, buildingRatio, + warriorCount, warriorRatio, upgradeCount, + this->getName().c_str()); + + //workers + if (workerCount < 5) + ai->addTask(new ProduceTask(ucWorker)); + if (workerCount < 10) + ai->addTask(new ProduceTask(ucWorker)); + if (workerRatio < 0.20) + ai->addTask(new ProduceTask(ucWorker)); + if (workerRatio < 0.30) + ai->addTask(new ProduceTask(ucWorker)); + + //warriors + if (warriorCount < 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.20) + ai->addTask(new ProduceTask(ucWarrior)); + if (warriorRatio < 0.30) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 10) + ai->addTask(new ProduceTask(ucWarrior)); + if (workerCount >= 15) + ai->addTask(new ProduceTask(ucWarrior)); + + //buildings + if (buildingCount < 6 || buildingRatio < 0.20) + ai->addTask(new BuildTask((UnitType *) NULL)); + if (buildingCount < 10 && workerCount > 12) + ai->addTask(new BuildTask((UnitType *) NULL)); + + //upgrades + if (upgradeCount == 0 && workerCount > 5) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 1 && workerCount > 10) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (upgradeCount == 2 && workerCount > 15) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + if (ai->isStableBase()) + ai->addTask(new UpgradeTask((const UpgradeType *) NULL)); + } + } + } + + // ===================================================== + // class AiRuleBuildOneFarm + // ===================================================== + + AiRuleBuildOneFarm::AiRuleBuildOneFarm(Ai * ai) : + AiRule(ai) { + farm = NULL; + } + + bool + AiRuleBuildOneFarm::test() { + AiInterface * + aiInterface = ai->getAiInterface(); + + //for all units + for (int i = 0; + i < aiInterface->getMyFactionType()->getUnitTypeCount(); ++i) { + const UnitType * + ut = aiInterface->getMyFactionType()->getUnitType(i); + + //for all production commands + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + if (ct->getClass() == ccProduce) { + const UnitType * + producedType = static_cast (ct)-> + getProducedUnit(); + + //for all resources + for (int k = 0; k < producedType->getCostCount(); ++k) { + const Resource * + r = producedType->getCost(k); + + //find a food producer in the farm produced units + if (r->getAmount() < 0 + && r->getType()->getClass() == rcConsumable + && ai->getCountOfType(ut) == 0) { + if (aiInterface->reqsOk(ct) + && aiInterface->getMyFaction()-> + canCreateUnit(ut, true, true, true) == true) { + farm = ut; + //printf("AiRuleBuildOneFarm returning true, RULE Name[%s] ut [%s] producedType [%s]\n",this->getName().c_str(),ut->getName().c_str(),producedType->getName().c_str()); + + if (ai->outputAIBehaviourToConsole()) + printf + ("AiRuleBuildOneFarm returning true, RULE Name[%s]\n", + this->getName().c_str()); + + return true; + } + } + } + } + } + } + return false; + } + + void + AiRuleBuildOneFarm::execute() { + ai->addPriorityTask(new BuildTask(farm)); + } + + // ===================================================== + // class AiRuleProduceResourceProducer + // ===================================================== + + AiRuleProduceResourceProducer::AiRuleProduceResourceProducer(Ai * ai) : + AiRule(ai) { + interval = shortInterval; + rt = NULL; + newResourceBehaviour = + Config::getInstance().getBool("NewResourceBehaviour", "false");; + } + + bool + AiRuleProduceResourceProducer::test() { + //emergency tasks: resource buildings + AiInterface * + aiInterface = ai->getAiInterface(); + + //consumables first + for (int i = 0; + i < aiInterface->getTechTree()->getResourceTypeCount(); ++i) { + rt = aiInterface->getTechTree()->getResourceType(i); + const Resource * + r = aiInterface->getResource(rt); + + if (ai->outputAIBehaviourToConsole()) + printf("CONSUMABLE [%s][%d] Testing AI RULE Name[%s]\n", + rt->getName().c_str(), r->getBalance(), + this->getName().c_str()); + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "CONSUMABLE [%s][%d] Testing AI RULE Name[%s]", + rt->getName().c_str(), r->getBalance(), + this->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + bool + factionUsesResourceType = + aiInterface->factionUsesResourceType(aiInterface-> + getMyFactionType(), rt); + if (factionUsesResourceType == true + && rt->getClass() == rcConsumable) { + // The consumable balance is negative + if (r->getBalance() < 0) { + if (newResourceBehaviour == true) { + interval = shortInterval; + } else { + interval = longInterval; + } + + return true; + } + // If the consumable balance is down to 1/3 of what we need + else { + if (r->getBalance() * 3 + r->getAmount() < 0) { + if (newResourceBehaviour == true) { + interval = shortInterval; + } else { + interval = longInterval; + } + + return true; + } + } + } + } + + int + targetStaticResourceCount = minStaticResources; + if (aiInterface->getMyFactionType()-> + getAIBehaviorStaticOverideValue(aibsvcMinStaticResourceCount) != + INT_MAX) { + targetStaticResourceCount = + aiInterface->getMyFactionType()-> + getAIBehaviorStaticOverideValue(aibsvcMinStaticResourceCount); + } + + //statics second + for (int i = 0; + i < aiInterface->getTechTree()->getResourceTypeCount(); ++i) { + rt = aiInterface->getTechTree()->getResourceType(i); + const Resource * + r = aiInterface->getResource(rt); + + if (ai->outputAIBehaviourToConsole()) + printf("STATIC [%s][%d] [min %d] Testing AI RULE Name[%s]\n", + rt->getName().c_str(), r->getAmount(), + targetStaticResourceCount, this->getName().c_str()); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "STATIC resource check [%s][%d] [min %d] Testing AI RULE Name[%s]", + rt->getName().c_str(), r->getAmount(), + targetStaticResourceCount, this->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + if (rt->getClass() == rcStatic + && r->getAmount() < targetStaticResourceCount) { + bool + factionUsesResourceType = + aiInterface->factionUsesResourceType(aiInterface-> + getMyFactionType(), + rt); + if (factionUsesResourceType == true) { + if (newResourceBehaviour == true) + interval = shortInterval; + else + interval = longInterval; + return true; + } + } + } + + if (ai->outputAIBehaviourToConsole()) + printf("STATIC returning FALSE\n"); + if (aiInterface->isLogLevelEnabled(4) == true) + aiInterface->printLog(4, "Static Resource check returning FALSE"); + + if (newResourceBehaviour == true) + interval = longInterval; + else + interval = shortInterval; + return false; + } + + void + AiRuleProduceResourceProducer::execute() { + ai->addPriorityTask(new ProduceTask(rt)); + ai->addTask(new BuildTask(rt)); + } + + // ===================================================== + // class AiRuleProduce + // ===================================================== + + AiRuleProduce::AiRuleProduce(Ai * ai) : + AiRule(ai) { + produceTask = NULL; + newResourceBehaviour = + Config::getInstance().getBool("NewResourceBehaviour", "false"); + } + + bool + AiRuleProduce::test() { + const Task * + task = ai->getTask(); + + if (task == NULL || task->getClass() != tcProduce) { + return false; + } + + produceTask = static_cast (task); + return true; + } + + void + AiRuleProduce::execute() { + AiInterface * + aiInterface = ai->getAiInterface(); + if (produceTask != NULL) { + + if (ai->outputAIBehaviourToConsole()) + printf("AiRuleProduce producing [%s]\n", + (produceTask->getUnitType() != + NULL ? produceTask->getUnitType()->getName(false). + c_str() : "null")); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "AiRuleProduce producing [%s]", + (produceTask->getUnitType() != + NULL ? produceTask->getUnitType()->getName(false). + c_str() : "null")); + aiInterface->printLog(4, szBuf); + } + + //generic produce task, produce random unit that has the skill or produces the resource + if (produceTask->getUnitType() == NULL) { + if (newResourceBehaviour) { + produceGenericNew(produceTask); + } else + produceGeneric(produceTask); + } + + //specific produce task, produce if possible, retry if not enough resources + else { + produceSpecific(produceTask); + } + + //remove the task + ai->removeTask(produceTask); + } + } + + bool + AiRuleProduce::canUnitTypeOfferResourceType(const UnitType * ut, + const ResourceType * rt) { + bool + unitTypeOffersResourceType = false; + + AiInterface * + aiInterface = ai->getAiInterface(); + + if (ut != NULL && rt != NULL && aiInterface != NULL + && aiInterface->reqsOk(ut)) { + // Check of the unit 'gives' the resource + // if the unit produces the resource + const Resource * + r = ut->getCost(rt); + if (r != NULL) { + if (ai->outputAIBehaviourToConsole()) + printf + ("#2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", + r->getDescription(false).c_str(), r->getAmount(), + this->getName().c_str()); + } + + if (r != NULL && r->getAmount() < 0) { + unitTypeOffersResourceType = true; + } else { + // for each command check if we produce a unit that handles the resource + for (int commandIndex = 0; + commandIndex < ut->getCommandTypeCount(); ++commandIndex) { + const CommandType * + ct = ut->getCommandType(commandIndex); + + //if the command is produce + if (ct->getClass() == ccProduce + || ct->getClass() == ccMorph) { + const UnitType * + producedUnit = static_cast (ct->getProduced()); + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric [%p] Testing AI RULE Name[%s]\n", + rt, this->getName().c_str()); + + //if the unit produces the resource + const Resource * + r = producedUnit->getCost(rt); + if (r != NULL) { + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", + r->getDescription(false).c_str(), + r->getAmount(), this->getName().c_str()); + } + + if (r != NULL && r->getAmount() < 0) { + unitTypeOffersResourceType = true; + break; + } + } + } + } + } + + if (aiInterface != NULL && aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "canUnitTypeOfferResourceType for unit type [%s] for resource type [%s] returned: %d", + (ut != NULL ? ut->getName(false).c_str() : "n/a"), + (rt != NULL ? rt->getName(false).c_str() : "n/a"), + unitTypeOffersResourceType); + aiInterface->printLog(4, szBuf); + } + + return unitTypeOffersResourceType; + } + + bool + AiRuleProduce::setAIProduceTaskForResourceType(const ProduceTask * pt, + AiInterface * aiInterface) { + bool + taskAdded = false; + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + const UnitType * + ut = priorityUnit.first; + if (ai->getCountOfType(ut) < priorityUnit.second && + canUnitTypeOfferResourceType(ut, + pt->getResourceType()) == + true + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + ai->addTask(new ProduceTask(priorityUnit.first)); + taskAdded = true; + break; + } + } + } + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "setAIProduceTaskForResourceType for resource type [%s] returned: %d", + pt->getResourceType()->getName(false).c_str(), + taskAdded); + aiInterface->printLog(4, szBuf); + } + + return taskAdded; + } + + void + AiRuleProduce::addUnitTypeToCandidates(const UnitType * producedUnit, + UnitTypes & ableUnits, + UnitTypesGiveBack & + ableUnitsGiveBack, + bool unitCanGiveBackResource) { + // if the unit is not already on the list + if (find(ableUnits.begin(), ableUnits.end(), producedUnit) == + ableUnits.end()) { + ableUnits.push_back(producedUnit); + ableUnitsGiveBack.push_back(unitCanGiveBackResource); + + AiInterface * + aiInterface = ai->getAiInterface(); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "addUnitTypeToCandidates for unit type [%s] unitCanGiveBackResource = %d", + producedUnit->getName(false).c_str(), + unitCanGiveBackResource); + aiInterface->printLog(4, szBuf); + } + + } + } + + void + AiRuleProduce::produceGenericNew(const ProduceTask * pt) { + UnitTypes + ableUnits; + UnitTypesGiveBack + ableUnitsGiveBack; + + AiInterface * + aiInterface = ai->getAiInterface(); + if (pt->getResourceType() != NULL) { + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "****START: produceGeneric for resource type [%s]", + pt->getResourceType()->getName(false).c_str()); + aiInterface->printLog(4, szBuf); + } + + if (setAIProduceTaskForResourceType(pt, aiInterface) == true) { + return; + } + } else if (pt->getUnitClass() == ucWorker) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWorkerUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWorkerUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + ai->addTask(new ProduceTask(priorityUnit.first)); + return; + } + } + } + } else if (pt->getUnitClass() == ucWarrior) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWarriorUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWarriorUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + ai->addTask(new ProduceTask(priorityUnit.first)); + return; + } + } + } + } + + //for each unit, produce it if possible + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + if (aiInterface->getMyUnit(i)->getCurrCommand() != NULL + && aiInterface->getMyUnit(i)->getCurrCommand()-> + getCommandType()->getClass() == ccBuild) { + //skip this units as it is currently building something + continue; + } + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + + //bool produceIt= false; + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is produce + //bool produceIt= false; + if (ct->getClass() == ccProduce || ct->getClass() == ccMorph) { + const UnitType * + producedUnit = static_cast (ct->getProduced()); + + if (ai->outputAIBehaviourToConsole()) + printf("produceGeneric [%p] Testing AI RULE Name[%s]\n", + pt->getResourceType(), + this->getName().c_str()); + + //if the unit produces the resource + if (pt->getResourceType() != NULL) { + const Resource * + r = producedUnit->getCost(pt->getResourceType()); + if (r != NULL) { + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", + r->getDescription(false).c_str(), + r->getAmount(), this->getName().c_str()); + } + + if (r != NULL && r->getAmount() < 0) { + if (aiInterface->reqsOk(ct) + && aiInterface->reqsOk(producedUnit)) { + //produceIt= true; + addUnitTypeToCandidates(producedUnit, + ableUnits, + ableUnitsGiveBack, + false); + } + } + } else { + //if the unit is from the right class + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n", + producedUnit->isOfClass(pt->getUnitClass()), + this->getName().c_str()); + + if (producedUnit->isOfClass(pt->getUnitClass())) { + if (aiInterface->reqsOk(ct) + && aiInterface->reqsOk(producedUnit)) { + //produceIt= true; + addUnitTypeToCandidates(producedUnit, + ableUnits, + ableUnitsGiveBack, + false); + } + } + } + } + } + // Now check of the unit 'gives' the resource + // This is likely a unit that it BUILT by another and that is handled by a different AI task type: Build + // if(produceIt == false && pt->getResourceType() != NULL) { + // const Resource *r= ut->getCost(pt->getResourceType()); + // if(r != NULL) { + // if(ai->outputAIBehaviourToConsole()) printf("#2 produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n",r->getDescription(false).c_str(),r->getAmount(), this->getName().c_str()); + // } + // + // if(r != NULL && r->getAmount() < 0) { + // if(aiInterface->reqsOk(ut)){ + // produceIt= true; + // addUnitTypeToCandidates(ut, ableUnits,ableUnitsGiveBack, true); + // } + // } + // } + + } + + //add specific produce task + if (ableUnits.empty() == false) { + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n", + (int) ableUnits.size(), this->getName().c_str()); + + // Now check if we already have at least 2 produce or morph + // resource based units, if so prefer units that give back the resource + if (pt->getResourceType() != NULL && ableUnits.size() > 1) { + //priority for non produced units + UnitTypes + newAbleUnits; + bool + haveEnoughProducers = true; + bool + haveNonProducers = false; + for (unsigned int i = 0; i < ableUnits.size(); ++i) { + const UnitType * + ut = ableUnits[i]; + bool + givesBack = ableUnitsGiveBack[i]; + if (givesBack == false && ai->getCountOfType(ut) < 2) { + haveEnoughProducers = false; + } else if (givesBack == true) { + haveNonProducers = true; + newAbleUnits.push_back(ut); + } + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In produceGeneric for unit type [%s] givesBack: %d count of unit type: %d", + ut->getName(false).c_str(), givesBack, + ai->getCountOfType(ut)); + aiInterface->printLog(4, szBuf); + } + + } + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "haveEnoughProducers [%d] haveNonProducers [%d]", + haveEnoughProducers, haveNonProducers); + aiInterface->printLog(4, szBuf); + + for (unsigned int i = 0; i < ableUnits.size(); ++i) { + const UnitType * + ut = ableUnits[i]; + snprintf(szBuf, 8096, "i: %u unit type [%s]", i, + ut->getName(false).c_str()); + aiInterface->printLog(4, szBuf); + } + for (unsigned int i = 0; i < newAbleUnits.size(); ++i) { + const UnitType * + ut = newAbleUnits[i]; + snprintf(szBuf, 8096, "i: %u new unit type [%s]", i, + ut->getName(false).c_str()); + aiInterface->printLog(4, szBuf); + } + } + + if (haveEnoughProducers == true && haveNonProducers == true) { + ableUnits = newAbleUnits; + } + } + + //priority for non produced units + for (unsigned int i = 0; i < ableUnits.size(); ++i) { + if (ai->getCountOfType(ableUnits[i]) == 0) { + if (ai->getRandom()->randRange(0, 1) == 0) { + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In produceGeneric priority adding produce task: %d of " + MG_SIZE_T_SPECIFIER " for unit type [%s]", + i, ableUnits.size(), + ableUnits[i]->getName(false).c_str()); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new ProduceTask(ableUnits[i])); + return; + } + } + } + + //normal case + int + randomUnitTypeIndex = + ai->getRandom()->randRange(0, (int) ableUnits.size() - 1); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In produceGeneric randomUnitTypeIndex = %d of " + MG_SIZE_T_SPECIFIER " equals unit type [%s]", + randomUnitTypeIndex, ableUnits.size() - 1, + ableUnits[randomUnitTypeIndex]->getName(false). + c_str()); + aiInterface->printLog(4, szBuf); + } + + const UnitType * + ut = ableUnits[randomUnitTypeIndex]; + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "== END In produceGeneric normal adding produce task for unit type [%s]", + ut->getName(false).c_str()); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new ProduceTask(ut)); + } + } + + void + AiRuleProduce::produceGeneric(const ProduceTask * pt) { + typedef + vector < const UnitType *> + UnitTypes; + UnitTypes + ableUnits; + AiInterface * + aiInterface = ai->getAiInterface(); + + if (pt->getResourceType() != NULL) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + //if(ai->getRandom()->randRange(0, 1)==0) { + ai->addTask(new ProduceTask(priorityUnit.first)); + return; + //} + } + } + } + } else if (pt->getUnitClass() == ucWorker) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWorkerUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWorkerUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + //if(ai->getRandom()->randRange(0, 1)==0) { + ai->addTask(new ProduceTask(priorityUnit.first)); + return; + //} + } + } + } + } else if (pt->getUnitClass() == ucWarrior) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWarriorUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcWarriorUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, false, true, + true) == true) { + //if(ai->getRandom()->randRange(0, 1)==0) { + ai->addTask(new ProduceTask(priorityUnit.first)); + return; + //} + } + } + } + } + + //for each unit, produce it if possible + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + if (aiInterface->getMyUnit(i)->getCurrCommand() != NULL + && aiInterface->getMyUnit(i)->getCurrCommand()-> + getCommandType()->getClass() == ccBuild) { + //skip this units as it is currently building something + continue; + } + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is produce + if (ct->getClass() == ccProduce || ct->getClass() == ccMorph) { + + const UnitType * + producedUnit = static_cast (ct->getProduced()); + bool + produceIt = false; + + if (ai->outputAIBehaviourToConsole()) + printf("produceGeneric [%p] Testing AI RULE Name[%s]\n", + pt->getResourceType(), + this->getName().c_str()); + + //if the unit produces the resource + if (pt->getResourceType() != NULL) { + const Resource * + r = producedUnit->getCost(pt->getResourceType()); + + if (r != NULL) { + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric r = [%s][%d] Testing AI RULE Name[%s]\n", + r->getDescription(false).c_str(), + r->getAmount(), this->getName().c_str()); + } + + if (r != NULL && r->getAmount() < 0) { + produceIt = true; + } + } + + else { + //if the unit is from the right class + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric right class = [%d] Testing AI RULE Name[%s]\n", + producedUnit->isOfClass(pt->getUnitClass()), + this->getName().c_str()); + + if (producedUnit->isOfClass(pt->getUnitClass())) { + if (aiInterface->reqsOk(ct) + && aiInterface->reqsOk(producedUnit)) { + produceIt = true; + } + } + } + + if (produceIt) { + //if the unit is not already on the list + if (find + (ableUnits.begin(), ableUnits.end(), + producedUnit) == ableUnits.end()) { + ableUnits.push_back(producedUnit); + } + } + } + } + } + + //add specific produce task + if (ableUnits.empty() == false) { + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceGeneric !ableUnits.empty(), ableUnits.size() = [%d] Testing AI RULE Name[%s]\n", + (int) ableUnits.size(), this->getName().c_str()); + + //priority for non produced units + for (unsigned int i = 0; i < ableUnits.size(); ++i) { + if (ai->getCountOfType(ableUnits[i]) == 0) { + if (ai->getRandom()->randRange(0, 1) == 0) { + ai->addTask(new ProduceTask(ableUnits[i])); + return; + } + } + } + + //normal case + ai-> + addTask(new + ProduceTask(ableUnits + [ai->getRandom()-> + randRange(0, + (int) ableUnits.size() - 1)])); + } + } + + void + AiRuleProduce::produceSpecific(const ProduceTask * pt) { + + AiInterface * + aiInterface = ai->getAiInterface(); + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s]\n", + pt->getUnitType()->getName().c_str(), + aiInterface->reqsOk(pt->getUnitType()), + this->getName().c_str()); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "== START produceSpecific aiInterface->reqsOk(pt->getUnitType()) = [%s][%d] Testing AI RULE Name[%s]", + pt->getUnitType()->getName().c_str(), + aiInterface->reqsOk(pt->getUnitType()), + this->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + //if unit meets requirements + if (aiInterface->reqsOk(pt->getUnitType())) { + + const CommandType * + ctypeForCostCheck = NULL; + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + //don't use units which are currently building + if (aiInterface->getMyUnit(i)->getCurrCommand() != NULL + && aiInterface->getMyUnit(i)->getCurrCommand()-> + getCommandType()->getClass() == ccBuild) { + //skip this units as it is currently building something + continue; + } + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is produce + if (ct->getClass() == ccProduce + || ct->getClass() == ccMorph) { + const UnitType * + producedUnit = static_cast (ct->getProduced()); + + //if units match + if (producedUnit == pt->getUnitType()) { + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->reqsOk(ct), + this->getName().c_str()); + + if (aiInterface->reqsOk(ct)) { + if (ctypeForCostCheck == NULL + || ct->getClass() == ccMorph) { + if (ctypeForCostCheck != NULL + && ct->getClass() == ccMorph) { + const MorphCommandType * + mct = dynamic_cast (ct); + if (mct == NULL) { + throw + megaglest_runtime_error + ("mct == NULL"); + } + if (mct-> + getIgnoreResourceRequirements() == + true) { + ctypeForCostCheck = ct; + } + } else { + ctypeForCostCheck = ct; + } + } + } + } + } + } + } + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->checkCosts(pt->getUnitType(), + ctypeForCostCheck), + this->getName().c_str()); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "produceSpecific aiInterface->checkCosts(pt->getUnitType()) = [%d] Testing AI RULE Name[%s]", + aiInterface->checkCosts(pt->getUnitType(), + ctypeForCostCheck), + this->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + //if unit doesnt meet resources retry + if (aiInterface-> + checkCosts(pt->getUnitType(), ctypeForCostCheck) == false) { + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "Check costs FAILED."); + aiInterface->printLog(4, szBuf); + } + + ai->retryTask(pt); + return; + } + + //produce specific unit + vector < int > + producers; + // Hold a list of units which can produce or morph + // then a list of commandtypes for each unit + map < int, + vector < const CommandType *> > + producersDefaultCommandType; + const CommandType * + defCt = NULL; + + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + if (aiInterface->getMyUnit(i)->getCurrCommand() != NULL + && aiInterface->getMyUnit(i)->getCurrCommand()-> + getCommandType()->getClass() == ccBuild) { + //skip this units as it is currently building something + continue; + } + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is produce + if (ct->getClass() == ccProduce + || ct->getClass() == ccMorph) { + const UnitType * + producedUnit = static_cast (ct->getProduced()); + + //if units match + if (producedUnit == pt->getUnitType()) { + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific aiInterface->reqsOk(ct) = [%d] Testing AI RULE Name[%s]\n", + aiInterface->reqsOk(ct), + this->getName().c_str()); + + if (aiInterface->reqsOk(ct)) { + //defCt= ct; + producers.push_back(i); + producersDefaultCommandType[i].push_back(ct); + } + } + } + } + } + + //produce from random producer + if (producers.empty() == false) { + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]\n", + producers.empty(), this->getName().c_str()); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "produceSpecific producers.empty() = [%d] Testing AI RULE Name[%s]", + producers.empty(), this->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + // Narrow down producers list to those who are not busy if possible + vector < int > + idle_producers; + for (unsigned int i = 0; i < producers.size(); ++i) { + int + currentProducerIndex = producers[i]; + if (currentProducerIndex >= aiInterface->getMyUnitCount()) { + char + szBuf[8096] = ""; + printf + ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, + __LINE__, currentProducerIndex, + aiInterface->getMyUnitCount(), i, + producers.size()); + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER "", __FILE__, + __FUNCTION__, __LINE__, currentProducerIndex, + aiInterface->getMyUnitCount(), i, + producers.size()); + throw + megaglest_runtime_error(szBuf); + } + + const Unit * + unit = aiInterface->getMyUnit(currentProducerIndex); + if (unit->anyCommand() == false) { + idle_producers.push_back(currentProducerIndex); + } + } + if (idle_producers.empty() == false) { + producers = idle_producers; + } + + if (aiInterface->getControlType() == ctCpuZeta || + aiInterface->getControlType() == ctNetworkCpuZeta) { // zeta cpu trys to balance the commands to the producers + int + randomstart = + ai->getRandom()->randRange(0, + (int) producers.size() - 1); + int + lowestCommandCount = 1000000; + int + currentProducerIndex = producers[randomstart]; + int + bestIndex = -1; + //int besti=0; + int + currentCommandCount = 0; + for (unsigned int i = randomstart; + i < producers.size() + randomstart; i++) { + int + prIndex = i; + if (i >= producers.size()) { + prIndex = (i - (int) producers.size()); + } + currentProducerIndex = producers[prIndex]; + + if (currentProducerIndex >= + aiInterface->getMyUnitCount()) { + char + szBuf[8096] = ""; + printf + ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, + __LINE__, currentProducerIndex, + aiInterface->getMyUnitCount(), i, + producers.size()); + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %u, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER "", __FILE__, + __FUNCTION__, __LINE__, + currentProducerIndex, + aiInterface->getMyUnitCount(), i, + producers.size()); + throw + megaglest_runtime_error(szBuf); + } + if (prIndex >= (int) producers.size()) { + char + szBuf[8096] = ""; + printf + ("In [%s::%s Line: %d] prIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER " \n", __FILE__, + __FUNCTION__, __LINE__, prIndex, i, + producers.size()); + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] currentProducerIndex >= producers.size(), currentProducerIndex = %d, i = %u,producers.size() = " + MG_SIZE_T_SPECIFIER "", __FILE__, + __FUNCTION__, __LINE__, + currentProducerIndex, i, + producers.size()); + throw + megaglest_runtime_error(szBuf); + } + + currentCommandCount = + aiInterface->getMyUnit(currentProducerIndex)-> + getCommandSize(); + if (currentCommandCount == 1 + && aiInterface->getMyUnit(currentProducerIndex)-> + getCurrCommand()->getCommandType()->getClass() == + ccStop) { // special for non buildings + currentCommandCount = 0; + } + if (lowestCommandCount > currentCommandCount) { + lowestCommandCount = + aiInterface->getMyUnit(currentProducerIndex)-> + getCommandSize(); + bestIndex = currentProducerIndex; + //besti=i%(producers.size()); + } + } + if (bestIndex >= 0) { + if (aiInterface->getMyUnit(bestIndex)-> + getCommandSize() > 2) { + // maybe we need another producer of this kind if possible! + if (aiInterface-> + reqsOk(aiInterface->getMyUnit(bestIndex)-> + getType())) { + if (ai->getCountOfClass(ucBuilding) > 5) { + ai-> + addTask(new + BuildTask(aiInterface-> + getMyUnit + (bestIndex)-> + getType())); + } + } + // need to calculate another producer, maybe its better to produce another warrior with another producer + vector < int > + backupProducers; + // find another producer unit which is free and produce any kind of warrior. + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); + ++i) { + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + //for each command + for (int j = 0; j < ut->getCommandTypeCount(); + ++j) { + const CommandType * + ct = ut->getCommandType(j); + //if the command is produce + if (ct->getClass() == ccProduce) { + const UnitType * + unitType = static_cast (ct->getProduced()); + if (unitType->hasSkillClass(scAttack) + && !unitType-> + hasCommandClass(ccHarvest) + && aiInterface->reqsOk(ct)) { //this can produce a warrior + backupProducers.push_back(i); + } + } + } + } + if (!backupProducers.empty()) { + int + randomstart = + ai->getRandom()->randRange(0, + (int) + backupProducers. + size() - 1); + int + lowestCommandCount = 1000000; + int + currentProducerIndex = + backupProducers[randomstart]; + int + bestIndex = -1; + for (unsigned int i = randomstart; + i < backupProducers.size() + randomstart; + i++) { + int + prIndex = i; + if (i >= backupProducers.size()) { + prIndex = + (i - (int) backupProducers.size()); + } + + currentProducerIndex = + backupProducers[prIndex]; + + if (currentProducerIndex >= + aiInterface->getMyUnitCount()) { + char + szBuf[8096] = ""; + printf + ("In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " + MG_SIZE_T_SPECIFIER "\n", __FILE__, + __FUNCTION__, __LINE__, + currentProducerIndex, + aiInterface->getMyUnitCount(), i, + backupProducers.size()); + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] currentProducerIndex >= aiInterface->getMyUnitCount(), currentProducerIndex = %d, aiInterface->getMyUnitCount() = %d, i = %u,backupProducers.size() = " + MG_SIZE_T_SPECIFIER "", + __FILE__, __FUNCTION__, + __LINE__, + currentProducerIndex, + aiInterface-> + getMyUnitCount(), i, + backupProducers.size()); + throw + megaglest_runtime_error(szBuf); + } + if (prIndex >= + (int) backupProducers.size()) { + char + szBuf[8096] = ""; + printf + ("In [%s::%s Line: %d] prIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " + MG_SIZE_T_SPECIFIER " \n", __FILE__, + __FUNCTION__, __LINE__, prIndex, i, + backupProducers.size()); + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] currentProducerIndex >= backupProducers.size(), currentProducerIndex = %d, i = %u,backupProducers.size() = " + MG_SIZE_T_SPECIFIER "", + __FILE__, __FUNCTION__, + __LINE__, + currentProducerIndex, i, + backupProducers.size()); + throw + megaglest_runtime_error(szBuf); + } + + int + currentCommandCount = + aiInterface-> + getMyUnit(currentProducerIndex)-> + getCommandSize(); + if (currentCommandCount == 1 + && aiInterface-> + getMyUnit(currentProducerIndex)-> + getCurrCommand()->getCommandType()-> + getClass() == ccStop) { // special for non buildings + currentCommandCount = 0; + } + if (lowestCommandCount > + currentCommandCount) { + lowestCommandCount = + currentCommandCount; + bestIndex = currentProducerIndex; + if (lowestCommandCount == 0) + break; + } + } + // a good producer is found, lets choose a warrior production + vector < int > + productionCommandIndexes; + if (bestIndex >= 0) { + const UnitType * + ut = + aiInterface->getMyUnit(bestIndex)-> + getType(); + for (int j = 0; + j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is produce + if (ct->getClass() == ccProduce) { + const UnitType * + unitType = static_cast (ct->getProduced()); + if (unitType-> + hasSkillClass(scAttack) + && !unitType-> + hasCommandClass(ccHarvest) + && aiInterface->reqsOk(ct)) { //this can produce a warrior + productionCommandIndexes. + push_back(j); + } + } + } + + int + commandIndex = + productionCommandIndexes[ai-> + getRandom()-> + randRange(0, + (int) + productionCommandIndexes. + size + () - + 1)]; + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, + __FUNCTION__, + __LINE__); + + if (ai->outputAIBehaviourToConsole()) + printf + ("zeta #1 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)-> + getType()->getName().c_str(), + ut->getCommandType(commandIndex)-> + getName().c_str()); + if (aiInterface->isLogLevelEnabled(4) == + true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "zeta #1 produceSpecific giveCommand to unit [%s] commandType [%s]", + aiInterface-> + getMyUnit(bestIndex)-> + getType()->getName(). + c_str(), + ut-> + getCommandType + (commandIndex)->getName(). + c_str()); + aiInterface->printLog(4, szBuf); + } + + aiInterface->giveCommand(bestIndex, + ut-> + getCommandType + (commandIndex)); + } + } else { // do it like normal CPU + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, + __FUNCTION__, + __LINE__); + defCt = NULL; + if (producersDefaultCommandType. + find(bestIndex) != + producersDefaultCommandType.end()) { + int + bestCommandTypeCount = + (int) + producersDefaultCommandType[bestIndex]. + size(); + int + bestCommandTypeIndex = + ai->getRandom()->randRange(0, + bestCommandTypeCount + - 1); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + bestCommandTypeIndex, + bestCommandTypeCount); + + defCt = + producersDefaultCommandType[bestIndex] + [bestCommandTypeIndex]; + } + + if (ai->outputAIBehaviourToConsole()) + printf + ("zeta #2 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "n/a")); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "zeta #2 produceSpecific giveCommand to unit [%s] commandType [%s]", + aiInterface-> + getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "n/a")); + aiInterface->printLog(4, szBuf); + } + aiInterface->giveCommand(bestIndex, defCt); + } + } else { + if (currentCommandCount == 0) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, + __FUNCTION__, + __LINE__); + defCt = NULL; + if (producersDefaultCommandType. + find(bestIndex) != + producersDefaultCommandType.end()) { + //defCt = producersDefaultCommandType[bestIndex]; + int + bestCommandTypeCount = + (int) + producersDefaultCommandType[bestIndex]. + size(); + int + bestCommandTypeIndex = + ai->getRandom()->randRange(0, + bestCommandTypeCount + - 1); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + bestCommandTypeIndex, + bestCommandTypeCount); + + defCt = + producersDefaultCommandType[bestIndex] + [bestCommandTypeIndex]; + } + + if (ai->outputAIBehaviourToConsole()) + printf + ("zeta #3 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "n/a")); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "zeta #3 produceSpecific giveCommand to unit [%s] commandType [%s]", + aiInterface-> + getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "n/a")); + aiInterface->printLog(4, szBuf); + } + + aiInterface->giveCommand(bestIndex, defCt); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, __FUNCTION__, + __LINE__); + defCt = NULL; + if (producersDefaultCommandType.find(bestIndex) != + producersDefaultCommandType.end()) { + //defCt = producersDefaultCommandType[bestIndex]; + int + bestCommandTypeCount = + (int) producersDefaultCommandType[bestIndex]. + size(); + int + bestCommandTypeIndex = + ai->getRandom()->randRange(0, + bestCommandTypeCount + - 1); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + bestCommandTypeIndex, + bestCommandTypeCount); + + defCt = + producersDefaultCommandType[bestIndex] + [bestCommandTypeIndex]; + } + if (ai->outputAIBehaviourToConsole()) + printf + ("zeta #4 produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName().c_str() : "n/a")); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "zeta #4 produceSpecific giveCommand to unit [%s] commandType [%s]", + aiInterface->getMyUnit(bestIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "n/a")); + aiInterface->printLog(4, szBuf); + } + + aiInterface->giveCommand(bestIndex, defCt); + } + } + } else { + int + pIndex = + ai->getRandom()->randRange(0, + (int) producers.size() - 1); + int + producerIndex = producers[pIndex]; + defCt = NULL; + if (producersDefaultCommandType.find(producerIndex) != + producersDefaultCommandType.end()) { + //defCt = producersDefaultCommandType[producerIndex]; + int + bestCommandTypeCount = + (int) producersDefaultCommandType[producerIndex]. + size(); + int + bestCommandTypeIndex = + ai->getRandom()->randRange(0, + bestCommandTypeCount - + 1); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", + __FILE__, __FUNCTION__, + __LINE__, + bestCommandTypeIndex, + bestCommandTypeCount); + + defCt = + producersDefaultCommandType[producerIndex] + [bestCommandTypeIndex]; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] producers.size() = %d, producerIndex = %d, pIndex = %d, producersDefaultCommandType.size() = %d\n", + __FILE__, __FUNCTION__, + __LINE__, producers.size(), + producerIndex, pIndex, + producersDefaultCommandType. + size()); + + if (ai->outputAIBehaviourToConsole()) + printf + ("produceSpecific giveCommand to unit [%s] commandType [%s]\n", + aiInterface->getMyUnit(producerIndex)->getType()-> + getName().c_str(), + (defCt != NULL ? defCt->getName().c_str() : "n/a")); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "produceSpecific giveCommand to unit [%s] commandType [%s]", + aiInterface->getMyUnit(producerIndex)-> + getType()->getName().c_str(), + (defCt != + NULL ? defCt->getName(). + c_str() : "(null)")); + aiInterface->printLog(4, szBuf); + } + aiInterface->giveCommand(producerIndex, defCt); + } + } + } + } + + // ======================================== + // class AiRuleBuild + // ======================================== + + AiRuleBuild::AiRuleBuild(Ai * ai) : + AiRule(ai) { + buildTask = NULL; + } + + bool + AiRuleBuild::test() { + const Task * + task = ai->getTask(); + + if (task == NULL || task->getClass() != tcBuild) { + return false; + } + + buildTask = static_cast (task); + return true; + } + + + void + AiRuleBuild::execute() { + if (buildTask != NULL) { + if (ai->outputAIBehaviourToConsole()) + printf("BUILD AiRuleBuild Unit Name[%s]\n", + (buildTask->getUnitType() != + NULL ? buildTask->getUnitType()->getName(false). + c_str() : "null")); + + //generic build task, build random building that can be built + if (buildTask->getUnitType() == NULL) { + buildGeneric(buildTask); + } + //specific building task, build if possible, retry if not enough resources or not position + else { + buildSpecific(buildTask); + } + + //remove the task + ai->removeTask(buildTask); + } + } + + void + AiRuleBuild::buildGeneric(const BuildTask * bt) { + + //find buildings that can be built + AiInterface * + aiInterface = ai->getAiInterface(); + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "== START: buildGeneric for resource type [%s]", + (bt->getResourceType() != + NULL ? bt->getResourceType()->getName(). + c_str() : "null")); + aiInterface->printLog(4, szBuf); + } + + typedef + vector < const UnitType *> + UnitTypes; + UnitTypes + buildings; + + if (bt->getResourceType() != NULL) { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcResourceProducerUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, true, false, + false) == true) { + //if(ai->getRandom()->randRange(0, 1)==0) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildGeneric for resource type [%s] aibcResourceProducerUnits = " + MG_SIZE_T_SPECIFIER + " priorityUnit.first: [%s]\n", + bt->getResourceType()->getName(). + c_str(), unitList.size(), + priorityUnit.first->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(priorityUnit.first)); + return; + //} + } + } + } + } else { + if (aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcBuildingUnits).size() > 0) { + const + std::vector < + FactionType::PairPUnitTypeInt > & + unitList = + aiInterface->getMyFactionType()-> + getAIBehaviorUnits(aibcBuildingUnits); + for (unsigned int i = 0; i < unitList.size(); ++i) { + const + FactionType::PairPUnitTypeInt & + priorityUnit = unitList[i]; + if (ai->getCountOfType(priorityUnit.first) < + priorityUnit.second + && aiInterface->getMyFaction()-> + canCreateUnit(priorityUnit.first, true, false, + false) == true) { + //if(ai->getRandom()->randRange(0, 1)==0) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildGeneric for resource type [%s] aibcBuildingUnits = " + MG_SIZE_T_SPECIFIER + " priorityUnit.first: [%s]\n", + bt->getResourceType()->getName(). + c_str(), unitList.size(), + priorityUnit.first->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(priorityUnit.first)); + return; + //} + } + } + } + } + + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is build + if (ct->getClass() == ccBuild) { + const BuildCommandType * + bct = static_cast (ct); + + //for each building + for (int k = 0; k < bct->getBuildingCount(); ++k) { + const UnitType * + building = bct->getBuilding(k); + if (aiInterface->reqsOk(bct) + && aiInterface->reqsOk(building)) { + + //if any building, or produces resource + const ResourceType * + rt = bt->getResourceType(); + const Resource * + cost = building->getCost(rt); + if (rt == NULL + || (cost != NULL && cost->getAmount() < 0)) { + if (find + (buildings.begin(), buildings.end(), + building) == buildings.end()) { + buildings.push_back(building); + } + } + } + } + } + } + } + + if (aiInterface->isLogLevelEnabled(4) == true) { + for (int i = 0; i < (int) buildings.size(); ++i) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildGeneric i = %d unit type: [%s]\n", i, + buildings[i]->getName().c_str()); + aiInterface->printLog(4, szBuf); + } + } + + //add specific build task + buildBestBuilding(buildings); + } + + void + AiRuleBuild::buildBestBuilding(const vector < + const UnitType * >&buildings) { + + AiInterface * + aiInterface = ai->getAiInterface(); + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "==> START buildBestBuilding buildings.size = " + MG_SIZE_T_SPECIFIER "\n", buildings.size()); + aiInterface->printLog(4, szBuf); + } + + if (!buildings.empty()) { + + //build the least built building + bool + buildingFound = false; + for (int i = 0; i < 10 && !buildingFound; ++i) { + + if (i > 0) { + + //Defensive buildings have priority + for (int j = 0; + j < (int) buildings.size() && buildingFound == false; + ++j) { + const UnitType * + building = buildings[j]; + if (ai->getCountOfType(building) <= i + 1 + && isDefensive(building)) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildBestBuilding defensive building unit type: [%s] i = %d j = %d\n", + building->getName().c_str(), i, j); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(building)); + buildingFound = true; + } + } + + //Warrior producers next + for (unsigned int j = 0; + j < buildings.size() && !buildingFound; ++j) { + const UnitType * + building = buildings[j]; + if (ai->getCountOfType(building) <= i + 1 + && isWarriorProducer(building)) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildBestBuilding warriorproducer building unit type: [%s] i = %d j = %u\n", + building->getName().c_str(), i, j); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(building)); + buildingFound = true; + } + } + + //Resource producers next + for (unsigned int j = 0; + j < buildings.size() && !buildingFound; ++j) { + const UnitType * + building = buildings[j]; + if (ai->getCountOfType(building) <= i + 1 + && isResourceProducer(building)) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildBestBuilding resourceproducer building unit type: [%s] i = %d j = %u\n", + building->getName().c_str(), i, j); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(building)); + buildingFound = true; + } + } + } + + //Any building + for (unsigned int j = 0; + j < buildings.size() && !buildingFound; ++j) { + const UnitType * + building = buildings[j]; + if (ai->getCountOfType(building) <= i) { + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildBestBuilding ANY building unit type: [%s] i = %d j = %u\n", + building->getName().c_str(), i, j); + aiInterface->printLog(4, szBuf); + } + + ai->addTask(new BuildTask(building)); + buildingFound = true; + } + } + } + } + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "==> END buildBestBuilding buildings.size = " + MG_SIZE_T_SPECIFIER "\n", buildings.size()); + aiInterface->printLog(4, szBuf); + } + } + + void + AiRuleBuild::buildSpecific(const BuildTask * bt) { + AiInterface * + aiInterface = ai->getAiInterface(); + + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "== START: buildSpecific for resource type [%s] bt->getUnitType() [%s]", + (bt->getResourceType() != + NULL ? bt->getResourceType()->getName(). + c_str() : "null"), + (bt->getUnitType() != + NULL ? bt->getUnitType()->getName(false). + c_str() : "null")); + aiInterface->printLog(4, szBuf); + } + + //if reqs ok + if (aiInterface->reqsOk(bt->getUnitType())) { + + //retry if not enough resources + if (aiInterface->checkCosts(bt->getUnitType(), NULL) == false) { + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildSpecific for resource type [%s] checkcosts == false RETRYING", + (bt->getResourceType() != + NULL ? bt->getResourceType()->getName(). + c_str() : "null")); + aiInterface->printLog(4, szBuf); + } + + ai->retryTask(bt); + return; + } + + vector < int > + builders; + // Hold a list of units which can build + // then a list of build commandtypes for each unit + map < int, + vector < const BuildCommandType *> > + buildersDefaultCommandType; + const BuildCommandType * + defBct = NULL; + + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + + //if the unit is not going to build + const Unit * + u = aiInterface->getMyUnit(i); + if (u->anyCommand() == false + || u->getCurrCommand()->getCommandType()->getClass() != + ccBuild) { + + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is build + if (ct->getClass() == ccBuild) { + const BuildCommandType * + bct = static_cast (ct); + + //for each building + for (int k = 0; k < bct->getBuildingCount(); ++k) { + const UnitType * + building = bct->getBuilding(k); + + //if building match + if (bt->getUnitType() == building) { + if (aiInterface->reqsOk(bct)) { + builders.push_back(i); + buildersDefaultCommandType[i]. + push_back(bct); + //defBct= bct; + } + } + } + } + } + } + } + + //use random builder to build + if (builders.empty() == false) { + int + bIndex = + ai->getRandom()->randRange(0, (int) builders.size() - 1); + int + builderIndex = builders[bIndex]; + Vec2i + pos; + Vec2i + searchPos = + bt->getForcePos() ? bt->getPos() : ai-> + getRandomHomePosition(); + if (bt->getForcePos() == false) { + const int + enemySightDistanceToAvoid = 18; + vector < Unit * >enemies; + ai->getAiInterface()->getWorld()->getUnitUpdater()-> + findEnemiesForCell(searchPos, + bt->getUnitType()->getSize(), + enemySightDistanceToAvoid, + ai->getAiInterface()-> + getMyFaction(), enemies, true); + if (enemies.empty() == false) { + for (int i1 = 0; i1 < 25 && enemies.empty() == false; + ++i1) { + for (int j1 = 0; + j1 < 25 && enemies.empty() == false; ++j1) { + Vec2i + tryPos = searchPos + Vec2i(i1, j1); + + const int + spacing = 1; + if (ai->getAiInterface()-> + isFreeCells(tryPos - Vec2i(spacing), + bt->getUnitType()-> + getSize() + spacing * 2, + fLand)) { + enemies.clear(); + ai->getAiInterface()->getWorld()-> + getUnitUpdater()-> + findEnemiesForCell(tryPos, + bt->getUnitType()-> + getSize(), + enemySightDistanceToAvoid, + ai-> + getAiInterface()-> + getMyFaction(), + enemies, true); + if (enemies.empty() == true) { + searchPos = tryPos; + } + } + } + } + } + if (enemies.empty() == false) { + for (int i1 = -1; + i1 >= -25 && enemies.empty() == false; --i1) { + for (int j1 = -1; + j1 >= -25 && enemies.empty() == false; --j1) { + Vec2i + tryPos = searchPos + Vec2i(i1, j1); + + const int + spacing = 1; + if (ai->getAiInterface()-> + isFreeCells(tryPos - Vec2i(spacing), + bt->getUnitType()-> + getSize() + spacing * 2, + fLand)) { + enemies.clear(); + ai->getAiInterface()->getWorld()-> + getUnitUpdater()-> + findEnemiesForCell(tryPos, + bt->getUnitType()-> + getSize(), + enemySightDistanceToAvoid, + ai-> + getAiInterface()-> + getMyFaction(), + enemies, true); + if (enemies.empty() == true) { + searchPos = tryPos; + } + } + } + } + } + } + + //if free pos give command, else retry + if (ai->findPosForBuilding(bt->getUnitType(), searchPos, pos)) { + defBct = NULL; + if (buildersDefaultCommandType.find(builderIndex) != + buildersDefaultCommandType.end()) { + //defBct = buildersDefaultCommandType[builderIndex]; + int + bestCommandTypeCount = + (int) buildersDefaultCommandType[builderIndex]. + size(); + int + bestCommandTypeIndex = + ai->getRandom()->randRange(0, + bestCommandTypeCount - + 1); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] bestCommandTypeIndex = %d, bestCommandTypeCount = %d\n", + __FILE__, __FUNCTION__, + __LINE__, + bestCommandTypeIndex, + bestCommandTypeCount); + + defBct = + buildersDefaultCommandType[builderIndex] + [bestCommandTypeIndex]; + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] builderIndex = %d, bIndex = %d, defBct = %p\n", + __FILE__, __FUNCTION__, + __LINE__, builderIndex, bIndex, + defBct); + + aiInterface->giveCommand(builderIndex, defBct, pos, + bt->getUnitType()); + } else { + ai->retryTask(bt); + return; + } + } + } else { + if (aiInterface->isLogLevelEnabled(4) == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In buildSpecific for resource type [%s] reqsok == false", + (bt->getResourceType() != + NULL ? bt->getResourceType()->getName(). + c_str() : "null")); + aiInterface->printLog(4, szBuf); + } + + } + } + + bool + AiRuleBuild::isDefensive(const UnitType * building) { + if (ai->outputAIBehaviourToConsole()) + printf("BUILD isDefensive check for Unit Name[%s] result = %d\n", + building->getName(false).c_str(), + building->hasSkillClass(scAttack)); + + return building->hasSkillClass(scAttack); + } + + bool + AiRuleBuild::isResourceProducer(const UnitType * building) { + for (int i = 0; i < building->getCostCount(); i++) { + if (building->getCost(i)->getAmount() < 0) { + if (ai->outputAIBehaviourToConsole()) + printf + ("BUILD isResourceProducer check for Unit Name[%s] result = true\n", + building->getName(false).c_str()); + + return true; + } + } + if (ai->outputAIBehaviourToConsole()) + printf + ("BUILD isResourceProducer check for Unit Name[%s] result = false\n", + building->getName(false).c_str()); + + return false; + } + + bool + AiRuleBuild::isWarriorProducer(const UnitType * building) { + for (int i = 0; i < building->getCommandTypeCount(); i++) { + const CommandType * + ct = building->getCommandType(i); + if (ct->getClass() == ccProduce) { + const UnitType * + ut = static_cast (ct)-> + getProducedUnit(); + + if (ut->isOfClass(ucWarrior)) { + if (ai->outputAIBehaviourToConsole()) + printf + ("BUILD isWarriorProducer check for Unit Name[%s] result = true\n", + building->getName(false).c_str()); + + return true; + } + } + } + if (ai->outputAIBehaviourToConsole()) + printf + ("BUILD isWarriorProducer check for Unit Name[%s] result = false\n", + building->getName(false).c_str()); + + return false; + } + + // ======================================== + // class AiRuleUpgrade + // ======================================== + + AiRuleUpgrade::AiRuleUpgrade(Ai * ai) : + AiRule(ai) { + upgradeTask = NULL; + } + + bool + AiRuleUpgrade::test() { + const Task * + task = ai->getTask(); + + if (task == NULL || task->getClass() != tcUpgrade) { + return false; + } + + upgradeTask = static_cast (task); + return true; + } + + void + AiRuleUpgrade::execute() { + + //upgrade any upgrade + if (upgradeTask->getUpgradeType() == NULL) { + upgradeGeneric(upgradeTask); + } + //upgrade specific upgrade + else { + upgradeSpecific(upgradeTask); + } + + //remove the task + ai->removeTask(upgradeTask); + } + + void + AiRuleUpgrade::upgradeGeneric(const UpgradeTask * upgt) { + + typedef + vector < const UpgradeType *> + UpgradeTypes; + AiInterface * + aiInterface = ai->getAiInterface(); + + //find upgrades that can be upgraded + UpgradeTypes + upgrades; + + if (aiInterface->getMyFactionType()->getAIBehaviorUpgrades().size() > + 0) { + const + std::vector < const UpgradeType *>& + upgradeList = + aiInterface->getMyFactionType()->getAIBehaviorUpgrades(); + for (unsigned int i = 0; i < upgradeList.size(); ++i) { + const UpgradeType * + priorityUpgrade = upgradeList[i]; + + //for each upgrade, upgrade it if possible + for (int k = 0; k < aiInterface->getMyUnitCount(); ++k) { + + //for each command + const UnitType * + ut = aiInterface->getMyUnit(k)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is upgrade + if (ct->getClass() == ccUpgrade) { + const UpgradeCommandType * + upgct = dynamic_cast (ct); + if (upgct != NULL) { + const UpgradeType * + upgrade = upgct->getProducedUpgrade(); + if (upgrade == priorityUpgrade) { + if (aiInterface->reqsOk(upgct) == true && + aiInterface->getMyFaction()-> + getUpgradeManager()-> + isUpgradingOrUpgraded(priorityUpgrade) + == false) { + //if(ai->getRandom()->randRange(0, 1)==0) { + ai-> + addTask(new + UpgradeTask + (priorityUpgrade)); + return; + //} + } + } + } + } + } + } + } + } + + //for each upgrade, upgrade it if possible + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is upgrade + if (ct->getClass() == ccUpgrade) { + const UpgradeCommandType * + upgct = static_cast (ct); + const UpgradeType * + upgrade = upgct->getProducedUpgrade(); + if (aiInterface->reqsOk(upgct)) { + upgrades.push_back(upgrade); + } + } + } + } + + //add specific upgrade task + if (!upgrades.empty()) { + ai-> + addTask(new + UpgradeTask(upgrades + [ai->getRandom()-> + randRange(0, + (int) upgrades.size() - 1)])); + } + } + + void + AiRuleUpgrade::upgradeSpecific(const UpgradeTask * upgt) { + + AiInterface * + aiInterface = ai->getAiInterface(); + + //if reqs ok + if (aiInterface->reqsOk(upgt->getUpgradeType())) { + + //if resources dont meet retry + if (!aiInterface->checkCosts(upgt->getUpgradeType(), NULL)) { + ai->retryTask(upgt); + return; + } + + //for each unit + for (int i = 0; i < aiInterface->getMyUnitCount(); ++i) { + + //for each command + const UnitType * + ut = aiInterface->getMyUnit(i)->getType(); + for (int j = 0; j < ut->getCommandTypeCount(); ++j) { + const CommandType * + ct = ut->getCommandType(j); + + //if the command is upgrade + if (ct->getClass() == ccUpgrade) { + const UpgradeCommandType * + uct = static_cast (ct); + const UpgradeType * + producedUpgrade = uct->getProducedUpgrade(); + + //if upgrades match + if (producedUpgrade == upgt->getUpgradeType()) { + if (aiInterface->reqsOk(uct)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugSystem, + "In [%s::%s Line: %d]\n", + __FILE__, + __FUNCTION__, + __LINE__); + aiInterface->giveCommand(i, uct); + } + } + } + } + } + + } + } + + // ======================================== + // class AiRuleExpand + // ======================================== + + AiRuleExpand::AiRuleExpand(Ai * ai) : + AiRule(ai) { + storeType = NULL; + } + + bool + AiRuleExpand::test() { + AiInterface * + aiInterface = ai->getAiInterface(); + + int + unitCount = aiInterface->getMyUnitCount(); + for (int i = 0; + i < aiInterface->getTechTree()->getResourceTypeCount(); ++i) { + const ResourceType * + rt = aiInterface->getTechTree()->getResourceType(i); + if (rt->getClass() == rcTech) { + bool + factionUsesResourceType = + aiInterface->factionUsesResourceType(aiInterface-> + getMyFactionType(), + rt); + if (factionUsesResourceType == true) { + // If any resource sighted + if (aiInterface-> + getNearestSightedResource(rt, + aiInterface-> + getHomeLocation(), + expandPos, true)) { + int + minDistance = INT_MAX; + storeType = NULL; + + //If there is no close store + for (int j = 0; j < unitCount; ++j) { + const Unit * + u = aiInterface->getMyUnit(j); + const UnitType * + ut = u->getType(); + + // If this building is a store + if (ut->getStore(rt) > 0) { + storeType = ut; + int + distance = + static_cast < + int>(u->getPosNotThreadSafe(). + dist(expandPos)); + if (distance < minDistance) { + minDistance = distance; + } + } + } + + if (minDistance > expandDistance) { + return true; + } + } else { + // send patrol to look for resource + ai->sendScoutPatrol(); + } + } + } + } + + return false; + } + + void + AiRuleExpand::execute() { + ai->addExpansion(expandPos); + ai->addPriorityTask(new BuildTask(storeType, expandPos)); + } + + + // ======================================== + // class AiRuleUnBlock + // ======================================== + + AiRuleUnBlock::AiRuleUnBlock(Ai * ai) : + AiRule(ai) { + + } + + bool + AiRuleUnBlock::test() { + return ai->haveBlockedUnits(); + } + + void + AiRuleUnBlock::execute() { + ai->unblockUnits(); + } + + } +} //end namespace diff --git a/source/glest_game/ai/ai_rule.h b/source/glest_game/ai/ai_rule.h index c1757fabe..d96f2ea91 100644 --- a/source/glest_game/ai/ai_rule.h +++ b/source/glest_game/ai/ai_rule.h @@ -23,642 +23,597 @@ # include "leak_dumper.h" using - std::string; +std::string; using - Shared::Graphics::Vec2i; +Shared::Graphics::Vec2i; namespace - Glest -{ - namespace - Game - { - - class - Ai; - class - AiInterface; - class - Unit; - class - UnitType; - class - ProduceTask; - class - BuildTask; - class - UpgradeTask; - class - ResourceType; - -// ===================================================== -// class AiRule -// -/// An action that the AI will perform periodically -/// if the test succeeds -// ===================================================== - -// The general structure of the rules for an AI to do a task. - class - AiRule - { - protected: - Ai * - ai; - - public: - explicit - AiRule (Ai * ai); - virtual ~ - AiRule () - { - } - - virtual int - getTestInterval () const = 0; //in milliseconds - virtual string - getName () const = 0; - - virtual bool - test () = 0; - virtual void - execute () = 0; - }; - -// ===================================================== -// class AiRuleWorkerHarvest -// ===================================================== - -// The rules for AI tasks inherit from the generic AiRule class. - class - AiRuleWorkerHarvest: - public - AiRule - { - private: - int - stoppedWorkerIndex; - - public: - explicit - AiRuleWorkerHarvest (Ai * ai); - // This value returned byt getTestInterval() - // is called in ai.cpp to determine the chance the rule is executed. - virtual int - getTestInterval () const - { - return - 1000; - } - virtual string - getName () const - { - return - "Worker stopped => Order worker to harvest"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleRefreshHarvester -// ===================================================== - - class - AiRuleRefreshHarvester: - public - AiRule - { - private: - int - workerIndex; - - public: - explicit - AiRuleRefreshHarvester (Ai * ai); - - virtual int - getTestInterval () const - { - return - 20000; - } - virtual string - getName () const - { - return - "Worker reassigned to needed resource"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleScoutPatrol -// ===================================================== - - class - AiRuleScoutPatrol: - public - AiRule - { - public: - explicit - AiRuleScoutPatrol (Ai * ai); - - virtual int - getTestInterval () const - { - return - 10000; - } - virtual string - getName () const - { - return - "Base is stable => Send scout patrol"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleRepair -// ===================================================== - - class - AiRuleRepair: - public - AiRule - { - private: - int - damagedUnitIndex; - bool - damagedUnitIsCastle; - - int - getMinUnitsToRepairCastle (); - double - getMinCastleHpRatio () const; - - public: - explicit - AiRuleRepair (Ai * ai); - - virtual int - getTestInterval () const - { - return - 10000; - } - virtual string - getName () const - { - return - "Building Damaged => Repair"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleReturnBase -// ===================================================== - - class - AiRuleReturnBase: - public - AiRule - { - private: - int - stoppedUnitIndex; - public: - explicit - AiRuleReturnBase (Ai * ai); - - virtual int - getTestInterval () const - { - return - 5000; - } - virtual string - getName () const - { - return - "Stopped unit => Order return base"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleMassiveAttack -// ===================================================== - - class - AiRuleMassiveAttack: - public - AiRule - { - private: - static const int - baseRadius = 25; - - private: - Vec2i - attackPos; - Field - field; - bool - ultraAttack; - - public: - explicit - AiRuleMassiveAttack (Ai * ai); - - virtual int - getTestInterval () const - { - return - 1000; - } - virtual string - getName () const - { - return - "Unit under attack => Order massive attack"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleAddTasks -// ===================================================== - - class - AiRuleAddTasks: - public - AiRule - { - public: - explicit - AiRuleAddTasks (Ai * ai); - - virtual int - getTestInterval () const - { - return - 5000; - } - virtual string - getName () const - { - return - "Tasks empty => Add tasks"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleBuildOneFarm -// ===================================================== - - class - AiRuleBuildOneFarm: - public - AiRule - { - private: - const UnitType * - farm; - - public: - explicit - AiRuleBuildOneFarm (Ai * ai); - - virtual int - getTestInterval () const - { - return - 10000; - } - virtual string - getName () const - { - return - "No farms => Build one"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleProduceResourceProducer -// ===================================================== - - class - AiRuleProduceResourceProducer: - public - AiRule - { - private: - static const int - minStaticResources = 20; - static const int - longInterval = 60000; - static const int - shortInterval = 5000; - const ResourceType * - rt; - int - interval; - bool - newResourceBehaviour; - - public: - explicit - AiRuleProduceResourceProducer (Ai * ai); - - virtual int - getTestInterval () const - { - return - interval; - } - virtual string - getName () const - { - return - "No resources => Build Resource Producer"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleProduce -// ===================================================== - - class - AiRuleProduce: - public - AiRule - { - private: - const ProduceTask * - produceTask; - - typedef - vector < const UnitType *> - UnitTypes; - typedef - vector < - bool > - UnitTypesGiveBack; - bool - newResourceBehaviour; - - public: - explicit - AiRuleProduce (Ai * ai); - - virtual int - getTestInterval () const - { - return - 100; - } - virtual string - getName () const - { - return - "Performing produce task"; - } - - virtual bool - test (); - virtual void - execute (); - - private: - void - produceGeneric (const ProduceTask * pt); - void - produceGenericNew (const ProduceTask * pt); - void - produceSpecific (const ProduceTask * pt); - bool - canUnitTypeOfferResourceType (const UnitType * ut, - const ResourceType * rt); - bool - setAIProduceTaskForResourceType (const ProduceTask * pt, - AiInterface * aiInterface); - void - addUnitTypeToCandidates (const UnitType * producedUnit, - UnitTypes & ableUnits, - UnitTypesGiveBack & ableUnitsGiveBack, - bool unitCanGiveBackResource); - }; -// ===================================================== -// class AiRuleBuild -// ===================================================== - - class - AiRuleBuild: - public - AiRule - { - private: - const BuildTask * - buildTask; - - public: - explicit - AiRuleBuild (Ai * ai); - - virtual int - getTestInterval () const - { - return - 2000; - } - virtual string - getName () const - { - return - "Performing build task"; - } - - virtual bool - test (); - virtual void - execute (); - - private: - void - buildGeneric (const BuildTask * bt); - void - buildSpecific (const BuildTask * bt); - void - buildBestBuilding (const vector < const UnitType * >&buildings); - - bool - isDefensive (const UnitType * building); - bool - isResourceProducer (const UnitType * building); - bool - isWarriorProducer (const UnitType * building); - }; - -// ===================================================== -// class AiRuleUpgrade -// ===================================================== - - class - AiRuleUpgrade: - public - AiRule - { - private: - const UpgradeTask * - upgradeTask; - - public: - explicit - AiRuleUpgrade (Ai * ai); - - virtual int - getTestInterval () const - { - return - 2000; - } - virtual string - getName () const - { - return - "Performing upgrade task"; - } - - virtual bool - test (); - virtual void - execute (); - - private: - void - upgradeSpecific (const UpgradeTask * upgt); - void - upgradeGeneric (const UpgradeTask * upgt); - }; - -// ===================================================== -// class AiRuleExpand -// ===================================================== - - class - AiRuleExpand: - public - AiRule - { - private: - static const int - expandDistance = 30; - - private: - Vec2i - expandPos; - const UnitType * - storeType; - - public: - explicit - AiRuleExpand (Ai * ai); - - virtual int - getTestInterval () const - { - return - 30000; - } - virtual string - getName () const - { - return - "Expanding"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -// ===================================================== -// class AiRuleUnBlock -// ===================================================== - - class - AiRuleUnBlock: - public - AiRule - { - public: - explicit - AiRuleUnBlock (Ai * ai); - - virtual int - getTestInterval () const - { - return - 3000; - } - virtual string - getName () const - { - return - "Blocked Units => Move surrounding units"; - } - - virtual bool - test (); - virtual void - execute (); - }; - -}} //end namespace + Glest { + namespace + Game { + + class + Ai; + class + AiInterface; + class + Unit; + class + UnitType; + class + ProduceTask; + class + BuildTask; + class + UpgradeTask; + class + ResourceType; + + // ===================================================== + // class AiRule + // + /// An action that the AI will perform periodically + /// if the test succeeds + // ===================================================== + + // The general structure of the rules for an AI to do a task. + class + AiRule { + protected: + Ai * + ai; + + public: + explicit + AiRule(Ai * ai); + virtual ~ + AiRule() { + } + + virtual int + getTestInterval() const = 0; //in milliseconds + virtual string + getName() const = 0; + + virtual bool + test() = 0; + virtual void + execute() = 0; + }; + + // ===================================================== + // class AiRuleWorkerHarvest + // ===================================================== + + // The rules for AI tasks inherit from the generic AiRule class. + class + AiRuleWorkerHarvest : + public + AiRule { + private: + int + stoppedWorkerIndex; + + public: + explicit + AiRuleWorkerHarvest(Ai * ai); + // This value returned byt getTestInterval() + // is called in ai.cpp to determine the chance the rule is executed. + virtual int + getTestInterval() const { + return + 1000; + } + virtual string + getName() const { + return + "Worker stopped => Order worker to harvest"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleRefreshHarvester + // ===================================================== + + class + AiRuleRefreshHarvester : + public + AiRule { + private: + int + workerIndex; + + public: + explicit + AiRuleRefreshHarvester(Ai * ai); + + virtual int + getTestInterval() const { + return + 20000; + } + virtual string + getName() const { + return + "Worker reassigned to needed resource"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleScoutPatrol + // ===================================================== + + class + AiRuleScoutPatrol : + public + AiRule { + public: + explicit + AiRuleScoutPatrol(Ai * ai); + + virtual int + getTestInterval() const { + return + 10000; + } + virtual string + getName() const { + return + "Base is stable => Send scout patrol"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleRepair + // ===================================================== + + class + AiRuleRepair : + public + AiRule { + private: + int + damagedUnitIndex; + bool + damagedUnitIsCastle; + + int + getMinUnitsToRepairCastle(); + double + getMinCastleHpRatio() const; + + public: + explicit + AiRuleRepair(Ai * ai); + + virtual int + getTestInterval() const { + return + 10000; + } + virtual string + getName() const { + return + "Building Damaged => Repair"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleReturnBase + // ===================================================== + + class + AiRuleReturnBase : + public + AiRule { + private: + int + stoppedUnitIndex; + public: + explicit + AiRuleReturnBase(Ai * ai); + + virtual int + getTestInterval() const { + return + 5000; + } + virtual string + getName() const { + return + "Stopped unit => Order return base"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleMassiveAttack + // ===================================================== + + class + AiRuleMassiveAttack : + public + AiRule { + private: + static const int + baseRadius = 25; + + private: + Vec2i + attackPos; + Field + field; + bool + ultraAttack; + + public: + explicit + AiRuleMassiveAttack(Ai * ai); + + virtual int + getTestInterval() const { + return + 1000; + } + virtual string + getName() const { + return + "Unit under attack => Order massive attack"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleAddTasks + // ===================================================== + + class + AiRuleAddTasks : + public + AiRule { + public: + explicit + AiRuleAddTasks(Ai * ai); + + virtual int + getTestInterval() const { + return + 5000; + } + virtual string + getName() const { + return + "Tasks empty => Add tasks"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleBuildOneFarm + // ===================================================== + + class + AiRuleBuildOneFarm : + public + AiRule { + private: + const UnitType * + farm; + + public: + explicit + AiRuleBuildOneFarm(Ai * ai); + + virtual int + getTestInterval() const { + return + 10000; + } + virtual string + getName() const { + return + "No farms => Build one"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleProduceResourceProducer + // ===================================================== + + class + AiRuleProduceResourceProducer : + public + AiRule { + private: + static const int + minStaticResources = 20; + static const int + longInterval = 60000; + static const int + shortInterval = 5000; + const ResourceType * + rt; + int + interval; + bool + newResourceBehaviour; + + public: + explicit + AiRuleProduceResourceProducer(Ai * ai); + + virtual int + getTestInterval() const { + return + interval; + } + virtual string + getName() const { + return + "No resources => Build Resource Producer"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleProduce + // ===================================================== + + class + AiRuleProduce : + public + AiRule { + private: + const ProduceTask * + produceTask; + + typedef + vector < const UnitType *> + UnitTypes; + typedef + vector < + bool > + UnitTypesGiveBack; + bool + newResourceBehaviour; + + public: + explicit + AiRuleProduce(Ai * ai); + + virtual int + getTestInterval() const { + return + 2000; + } + virtual string + getName() const { + return + "Performing produce task"; + } + + virtual bool + test(); + virtual void + execute(); + + private: + void + produceGeneric(const ProduceTask * pt); + void + produceGenericNew(const ProduceTask * pt); + void + produceSpecific(const ProduceTask * pt); + bool + canUnitTypeOfferResourceType(const UnitType * ut, + const ResourceType * rt); + bool + setAIProduceTaskForResourceType(const ProduceTask * pt, + AiInterface * aiInterface); + void + addUnitTypeToCandidates(const UnitType * producedUnit, + UnitTypes & ableUnits, + UnitTypesGiveBack & ableUnitsGiveBack, + bool unitCanGiveBackResource); + }; + // ===================================================== + // class AiRuleBuild + // ===================================================== + + class + AiRuleBuild : + public + AiRule { + private: + const BuildTask * + buildTask; + + public: + explicit + AiRuleBuild(Ai * ai); + + virtual int + getTestInterval() const { + return + 1000; + } + virtual string + getName() const { + return + "Performing build task"; + } + + virtual bool + test(); + virtual void + execute(); + + private: + void + buildGeneric(const BuildTask * bt); + void + buildSpecific(const BuildTask * bt); + void + buildBestBuilding(const vector < const UnitType * >&buildings); + + bool + isDefensive(const UnitType * building); + bool + isResourceProducer(const UnitType * building); + bool + isWarriorProducer(const UnitType * building); + }; + + // ===================================================== + // class AiRuleUpgrade + // ===================================================== + + class + AiRuleUpgrade : + public + AiRule { + private: + const UpgradeTask * + upgradeTask; + + public: + explicit + AiRuleUpgrade(Ai * ai); + + virtual int + getTestInterval() const { + return + 2000; + } + virtual string + getName() const { + return + "Performing upgrade task"; + } + + virtual bool + test(); + virtual void + execute(); + + private: + void + upgradeSpecific(const UpgradeTask * upgt); + void + upgradeGeneric(const UpgradeTask * upgt); + }; + + // ===================================================== + // class AiRuleExpand + // ===================================================== + + class + AiRuleExpand : + public + AiRule { + private: + static const int + expandDistance = 30; + + private: + Vec2i + expandPos; + const UnitType * + storeType; + + public: + explicit + AiRuleExpand(Ai * ai); + + virtual int + getTestInterval() const { + return + 30000; + } + virtual string + getName() const { + return + "Expanding"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + // ===================================================== + // class AiRuleUnBlock + // ===================================================== + + class + AiRuleUnBlock : + public + AiRule { + public: + explicit + AiRuleUnBlock(Ai * ai); + + virtual int + getTestInterval() const { + return + 3000; + } + virtual string + getName() const { + return + "Blocked Units => Move surrounding units"; + } + + virtual bool + test(); + virtual void + execute(); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/ai/path_finder.cpp b/source/glest_game/ai/path_finder.cpp index c383dc71b..d03016ec7 100644 --- a/source/glest_game/ai/path_finder.cpp +++ b/source/glest_game/ai/path_finder.cpp @@ -24,2096 +24,1852 @@ using namespace std; using namespace - Shared::Graphics; +Shared::Graphics; using namespace - Shared::Util; +Shared::Util; using namespace - Shared::PlatformCommon; +Shared::PlatformCommon; using - Shared::Util::RandomGen; +Shared::Util::RandomGen; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class PathFinder -// ===================================================== - -// ===================== PUBLIC ======================== - - const int - PathFinder::maxFreeSearchRadius = 10; - - int - PathFinder::pathFindNodesAbsoluteMax = 900; - int - PathFinder::pathFindNodesMax = 2000; - const int - PathFinder::pathFindBailoutRadius = 20; - const int - PathFinder::pathFindExtendRefreshForNodeCount = 25; - const int - PathFinder::pathFindExtendRefreshNodeCountMin = 40; - const int - PathFinder::pathFindExtendRefreshNodeCountMax = 40; - - PathFinder::PathFinder () - { - minorDebugPathfinder = false; - map = NULL; - } - - int - PathFinder::getPathFindExtendRefreshNodeCount (FactionState & faction) - { - //int refreshNodeCount = faction.random.randRange(PathFinder::pathFindExtendRefreshNodeCountMin,PathFinder::pathFindExtendRefreshNodeCountMax); - //return refreshNodeCount; - return PathFinder::pathFindExtendRefreshNodeCountMin; - } - - PathFinder::PathFinder (const Map * map) - { - minorDebugPathfinder = false; - - map = NULL; - init (map); - } - - void - PathFinder::init (const Map * map) - { - for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; - ++factionIndex) - { - FactionState & faction = factions.getFactionState (factionIndex); - - faction.nodePool.resize (pathFindNodesAbsoluteMax); - faction.useMaxNodeCount = PathFinder::pathFindNodesMax; - } - this->map = map; - } - - void - PathFinder::init () - { - minorDebugPathfinder = false; - map = NULL; - } - - PathFinder::~PathFinder () - { - for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; - ++factionIndex) - { - FactionState & faction = factions.getFactionState (factionIndex); - - faction.nodePool.clear (); - } - factions.clear (); - map = NULL; - } - - void - PathFinder::clearCaches () - { - for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; - ++factionIndex) - { - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - FactionState & faction = factions.getFactionState (factionIndex); - MutexSafeWrapper - safeMutex (faction.getMutexPreCache (), mutexOwnerId); - - faction.precachedTravelState.clear (); - faction.precachedPath.clear (); - } - } - - void - PathFinder::clearUnitPrecache (Unit * unit) - { - if (unit != NULL && factions.size () > unit->getFactionIndex ()) - { - int - factionIndex = unit->getFactionIndex (); - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - FactionState & faction = factions.getFactionState (factionIndex); - MutexSafeWrapper - safeMutex (faction.getMutexPreCache (), mutexOwnerId); - - faction.precachedTravelState[unit->getId ()] = tsImpossible; - faction.precachedPath[unit->getId ()].clear (); - } - } - - void - PathFinder::removeUnitPrecache (Unit * unit) - { - if (unit != NULL && factions.size () > unit->getFactionIndex ()) - { - int - factionIndex = unit->getFactionIndex (); - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - FactionState & faction = factions.getFactionState (factionIndex); - MutexSafeWrapper - safeMutex (faction.getMutexPreCache (), mutexOwnerId); - - if (faction.precachedTravelState.find (unit->getId ()) != - faction.precachedTravelState.end ()) - { - faction.precachedTravelState.erase (unit->getId ()); - } - if (faction.precachedPath.find (unit->getId ()) != - faction.precachedPath.end ()) - { - faction.precachedPath.erase (unit->getId ()); - } - } - } - - TravelState - PathFinder::findPath (Unit * unit, const Vec2i & finalPos, - bool * wasStuck, int frameIndex) - { - TravelState - ts = tsImpossible; - - try - { - - int - factionIndex = unit->getFactionIndex (); - FactionState & faction = factions.getFactionState (factionIndex); - static string - mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexPrecache (faction.getMutexPreCache (), mutexOwnerId); - - if (map == NULL) - { - throw - megaglest_runtime_error ("map == NULL"); - } - - unit->setCurrentPathFinderDesiredFinalPos (finalPos); - - - if (frameIndex >= 0) - { - clearUnitPrecache (unit); - } - if (unit->getFaction ()->canUnitsPathfind () == true) - { - unit->getFaction ()->addUnitToPathfindingList (unit->getId ()); - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "canUnitsPathfind() == false"); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - return tsBlocked; - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[findPath] unit->getPos() [%s] finalPos [%s]", - unit->getPos ().getString ().c_str (), - finalPos.getString ().c_str ()); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - //route cache - if (finalPos == unit->getPos ()) - { - if (frameIndex < 0) - { - //if arrived - unit->setCurrSkill (scStop); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Unit finalPos [%s] == unit->getPos() [%s]", - finalPos.getString ().c_str (), - unit->getPos ().getString ().c_str ()); - unit-> - logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPathFinder).enabled == - true) - { - string - commandDesc = "none"; - Command * - command = unit->getCurrCommand (); - if (command != NULL && command->getCommandType () != NULL) - { - commandDesc = - command->getCommandType ()->toString (false); - } - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "State: arrived#1 at pos: %s, command [%s]", - finalPos.getString ().c_str (), - commandDesc.c_str ()); - unit->setCurrentUnitTitle (szBuf); - } - - return tsArrived; - } - - UnitPathInterface * - path = unit->getPath (); - if (path->isEmpty () == false) - { - UnitPathBasic * - basic_path = dynamic_cast < UnitPathBasic * >(path); - if (basic_path != NULL) - { - //route cache - Vec2i - pos = basic_path->pop (frameIndex < 0); - - if (map->canMove (unit, unit->getPos (), pos)) - { - if (frameIndex < 0) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch).enabled == - true - && SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "#1 map->canMove to pos [%s] from [%s]", - pos.getString ().c_str (), - unit->getPos ().getString ().c_str ()); - unit-> - logSynchData (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - unit->setTargetPos (pos, frameIndex < 0); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch).enabled == - true - && SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "#2 map->canMove to pos [%s] from [%s]", - pos.getString ().c_str (), - unit->getPos ().getString ().c_str ()); - unit-> - logSynchData (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - } - - return tsMoving; - } - } - else if (dynamic_cast < UnitPath * >(path) != NULL) - { - UnitPath * - advPath = dynamic_cast < UnitPath * >(path); - //route cache - Vec2i - pos = advPath->peek (); - if (map->canMove (unit, unit->getPos (), pos)) - { - if (frameIndex < 0) - { - advPath->pop (); - unit->setTargetPos (pos, frameIndex < 0); - } - - return tsMoving; - } - } - else - { - throw - megaglest_runtime_error - ("unsupported or missing path finder detected!"); - } - } - - if (path->isStuck () == true && - (unit->getLastStuckPos () == finalPos - || path->getBlockCount () > 500) - && unit-> - isLastStuckFrameWithinCurrentFrameTolerance (frameIndex >= 0) == - true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "path->isStuck() == true unit->getLastStuckPos() [%s] finalPos [%s] path->getBlockCount() [%d]", - unit->getLastStuckPos ().getString ().c_str (), - finalPos.getString ().c_str (), - path->getBlockCount ()); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - return tsBlocked; - } - - //route cache miss - int - maxNodeCount = -1; - if (unit->getUsePathfinderExtendedMaxNodes () == true) - { - - maxNodeCount = PathFinder::pathFindNodesAbsoluteMax; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "maxNodeCount: %d", maxNodeCount); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - } - - bool - minorDebugPathfinderPerformance = false; - Chrono - chrono; - if (minorDebugPathfinderPerformance) - chrono.start (); - - uint32 - searched_node_count = 0; - minorDebugPathfinder = false; - if (minorDebugPathfinder) - printf - ("Legacy Pathfind Unit [%d - %s] from = %s to = %s frameIndex = %d\n", - unit->getId (), unit->getType ()->getName (false).c_str (), - unit->getPos ().getString ().c_str (), - finalPos.getString ().c_str (), frameIndex); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "calling aStar()"); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - ts = - aStar (unit, finalPos, false, frameIndex, maxNodeCount, - &searched_node_count); - //post actions - switch (ts) - { - case tsBlocked: - case tsArrived: - // The unit is stuck (not only blocked but unable to go anywhere for a while) - // We will try to bail out of the immediate area - if (ts == tsBlocked && unit->getInBailOutAttempt () == false && - path->isStuck () == true) - { - - if (minorDebugPathfinder) - printf - ("Pathfind Unit [%d - %s] START BAILOUT ATTEMPT frameIndex = %d\n", - unit->getId (), - unit->getType ()->getName (false).c_str (), frameIndex); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[attempting to BAIL OUT] finalPos [%s] ts [%d]", - finalPos.getString ().c_str (), ts); - unit-> - logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - if (wasStuck != NULL) - { - *wasStuck = true; - } - unit->setInBailOutAttempt (true); - - bool - unitImmediatelyBlocked = false; - - // First check if unit currently blocked all around them, if so don't try to pathfind - const Vec2i - unitPos = unit->getPos (); - int - failureCount = 0; - int - cellCount = 0; - - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = unitPos + Vec2i (i, j); - if (pos != unitPos) - { - bool - canUnitMoveToCell = - map->aproxCanMove (unit, unitPos, pos); - if (canUnitMoveToCell == false) - { - failureCount++; - } - cellCount++; - } - } - } - unitImmediatelyBlocked = (failureCount == cellCount); - if (unitImmediatelyBlocked == false) - { - - int - factionIndex = unit->getFactionIndex (); - FactionState & faction = - factions.getFactionState (factionIndex); - - //if(Thread::isCurrentThreadMainThread() == false) { - // throw megaglest_runtime_error("#2 Invalid access to FactionState random from outside main thread current id = " + - // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); - //} - - int - tryRadius = faction.random.randRange (1, 2); - //int tryRadius = faction.random.IRandomX(1,2); - //int tryRadius = 1; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In astar bailout() tryRadius %d", - tryRadius); - - if (frameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, - szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - // Try to bail out up to PathFinder::pathFindBailoutRadius cells away - if (tryRadius == 2) - { - for (int bailoutX = - -PathFinder::pathFindBailoutRadius; - bailoutX <= PathFinder::pathFindBailoutRadius - && ts == tsBlocked; ++bailoutX) - { - for (int bailoutY = - -PathFinder::pathFindBailoutRadius; - bailoutY <= PathFinder::pathFindBailoutRadius - && ts == tsBlocked; ++bailoutY) - { - const Vec2i - newFinalPos = - finalPos + Vec2i (bailoutX, bailoutY); - bool - canUnitMove = - map->canMove (unit, unit->getPos (), - newFinalPos); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[attempting to BAIL OUT] finalPos [%s] newFinalPos [%s] ts [%d] canUnitMove [%d]", - finalPos.getString ().c_str (), - newFinalPos.getString (). - c_str (), ts, canUnitMove); - unit-> - logSynchData - (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, szBuf); - } - - if (canUnitMove) - { - - int - maxBailoutNodeCount = - (PathFinder::pathFindBailoutRadius * 2); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "calling aStar()"); - unit-> - logSynchData - (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - ts = - aStar (unit, newFinalPos, true, - frameIndex, maxBailoutNodeCount, - &searched_node_count); - } - } - } - } - else - { - for (int bailoutX = PathFinder::pathFindBailoutRadius; - bailoutX >= -PathFinder::pathFindBailoutRadius - && ts == tsBlocked; --bailoutX) - { - for (int bailoutY = - PathFinder::pathFindBailoutRadius; - bailoutY >= - -PathFinder::pathFindBailoutRadius - && ts == tsBlocked; --bailoutY) - { - const Vec2i - newFinalPos = - finalPos + Vec2i (bailoutX, bailoutY); - bool - canUnitMove = - map->canMove (unit, unit->getPos (), - newFinalPos); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[attempting to BAIL OUT] finalPos [%s] newFinalPos [%s] ts [%d] canUnitMove [%d]", - finalPos.getString ().c_str (), - newFinalPos.getString (). - c_str (), ts, canUnitMove); - unit-> - logSynchData - (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, szBuf); - } - - if (canUnitMove) - { - int - maxBailoutNodeCount = - (PathFinder::pathFindBailoutRadius * 2); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "calling aStar()"); - unit-> - logSynchData - (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - ts = - aStar (unit, newFinalPos, true, - frameIndex, maxBailoutNodeCount, - &searched_node_count); - } - } - } - } - } - unit->setInBailOutAttempt (false); - - if (ts == tsBlocked) - { - unit->setLastStuckFrameToCurrentFrame (); - unit->setLastStuckPos (finalPos); - } - } - if (ts == tsArrived || ts == tsBlocked) - { - if (frameIndex < 0) - { - unit->setCurrSkill (scStop); - } - } - break; - case tsMoving: - { - if (dynamic_cast < UnitPathBasic * >(path) != NULL) - { - UnitPathBasic * - basicPath = dynamic_cast < UnitPathBasic * >(path); - Vec2i - pos; - if (frameIndex < 0 && basicPath != NULL) - { - pos = basicPath->pop (frameIndex < 0); - } - else - { - - if (faction.precachedPath[unit->getId ()].size () <= 0) - { - throw - megaglest_runtime_error - ("factions[unit->getFactionIndex()].precachedPath[unit->getId()].size() <= 0!"); - } - - pos = faction.precachedPath[unit->getId ()][0]; - - } - - if (map->canMove (unit, unit->getPos (), pos)) - { - if (frameIndex < 0) - { - unit->setTargetPos (pos, frameIndex < 0); - } - } - else - { - if (frameIndex < 0) - { - unit->setCurrSkill (scStop); - } - - if (minorDebugPathfinderPerformance - && chrono.getMillis () >= 1) - printf - ("Unit [%d - %s] astar #2 took [%lld] msecs, ts = %d searched_node_count = %d.\n", - unit->getId (), - unit->getType ()->getName (false).c_str (), - (long long int) chrono.getMillis (), ts, - searched_node_count); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "tsBlocked"); - unit-> - logSynchData (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - return tsBlocked; - } - } - else if (dynamic_cast < UnitPath * >(path) != NULL) - { - UnitPath * - advPath = dynamic_cast < UnitPath * >(path); - Vec2i - pos = advPath->peek (); - if (map->canMove (unit, unit->getPos (), pos)) - { - if (frameIndex < 0) - { - advPath->pop (); - unit->setTargetPos (pos, frameIndex < 0); - } - } - else - { - if (frameIndex < 0) - { - unit->setCurrSkill (scStop); - } - - if (minorDebugPathfinder) - printf - ("Pathfind Unit [%d - %s] INT BAILOUT ATTEMPT BLOCKED frameIndex = %d\n", - unit->getId (), - unit->getType ()->getName (false).c_str (), - frameIndex); - - if (minorDebugPathfinderPerformance - && chrono.getMillis () >= 1) - printf - ("Unit [%d - %s] astar #3 took [%lld] msecs, ts = %d searched_node_count = %d.\n", - unit->getId (), - unit->getType ()->getName (false).c_str (), - (long long int) chrono.getMillis (), ts, - searched_node_count); - return tsBlocked; - } - } - else - { - throw - megaglest_runtime_error - ("unsupported or missing path finder detected!"); - } - } - break; - - default: - break; - } - if (minorDebugPathfinderPerformance && chrono.getMillis () >= 1) - printf - ("Unit [%d - %s] astar took [%lld] msecs, ts = %d searched_node_count = %d.\n", - unit->getId (), unit->getType ()->getName (false).c_str (), - (long long int) chrono.getMillis (), ts, searched_node_count); - - } - catch (const exception & ex) - { - //setRunningStatus(false); - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - throw - megaglest_runtime_error (ex.what ()); - } - catch ( ...) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, - __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - - return ts; - } - -// ==================== PRIVATE ==================== - -//route a unit using A* algorithm - TravelState - PathFinder::aStar (Unit * unit, const Vec2i & targetPos, bool inBailout, - int frameIndex, int maxNodeCount, - uint32 * searched_node_count) - { - TravelState - ts = tsImpossible; - - try - { - - int - unitFactionIndex = unit->getFactionIndex (); - int - factionIndex = unit->getFactionIndex (); - FactionState & faction = factions.getFactionState (factionIndex); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex >= 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In aStar()"); - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono.start (); - - if (map == NULL) - { - throw - megaglest_runtime_error ("map == NULL"); - } - - - if (maxNodeCount < 0) - { - - int - factionIndex = unit->getFactionIndex (); - FactionState & faction = factions.getFactionState (factionIndex); - - maxNodeCount = faction.useMaxNodeCount; - } - - if (maxNodeCount >= 1 - && unit->getPathfindFailedConsecutiveFrameCount () >= 3) - { - maxNodeCount = 200; - } - - UnitPathInterface * - path = unit->getPath (); - - faction.nodePoolCount = 0; - faction.openNodesList.clear (); - faction.openPosList.clear (); - faction.closedNodesList.clear (); - - // check the pre-cache to see if we can re-use a cached path - if (frameIndex < 0) - { - - bool - foundPrecacheTravelState = - (faction.precachedTravelState.find (unit->getId ()) != - faction.precachedTravelState.end ()); - if (foundPrecacheTravelState == true) - { - -// if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true && frameIndex < 0) { -// char szBuf[8096]=""; -// snprintf(szBuf,8096,"factions[unitFactionIndex].precachedTravelState[unit->getId()]: %d",faction.precachedTravelState[unit->getId()]); -// unit->logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,szBuf); -// } - - bool - foundPrecacheTravelStateIsMoving = - (faction.precachedTravelState[unit->getId ()] == tsMoving); - if (foundPrecacheTravelStateIsMoving == true) - { - bool - canMoveToCells = true; - - Vec2i - lastPos = unit->getPos (); - - int - unitPrecachePathSize = - (int) faction.precachedPath[unit->getId ()].size (); - - for (int i = 0; i < unitPrecachePathSize; i++) - { - - Vec2i - nodePos = faction.precachedPath[unit->getId ()][i]; - - if (map->isInside (nodePos) == false - || map->isInsideSurface (map-> - toSurfCoords (nodePos)) - == false) - { - throw - megaglest_runtime_error - ("Pathfinder invalid node path position = " + - nodePos.getString () + " i = " + intToStr (i)); - } - - if (i < unit->getPathFindRefreshCellCount () || - (unitPrecachePathSize >= - pathFindExtendRefreshForNodeCount - && i < - getPathFindExtendRefreshNodeCount (faction))) - { - - if (canUnitMoveSoon (unit, lastPos, nodePos) == - false) - { - canMoveToCells = false; - break; - } - lastPos = nodePos; - } - else - { - break; - } - } - - if (canMoveToCells == true) - { - path->clear (); - //UnitPathBasic *basicPathFinder = dynamic_cast(path); - - int - unitPrecachePathSize = - (int) faction.precachedPath[unit->getId ()].size (); - - for (int i = 0; i < unitPrecachePathSize; i++) - { - - Vec2i - nodePos = - faction.precachedPath[unit->getId ()][i]; - - if (map->isInside (nodePos) == false - || map->isInsideSurface (map-> - toSurfCoords - (nodePos)) == false) - { - throw - megaglest_runtime_error - ("Pathfinder invalid node path position = " - + nodePos.getString () + " i = " + - intToStr (i)); - } - - //if(i < pathFindRefresh || - if (i < unit->getPathFindRefreshCellCount () || - (unitPrecachePathSize >= - pathFindExtendRefreshForNodeCount - && i < - getPathFindExtendRefreshNodeCount (faction))) - { - path->add (nodePos); - } - } - unit->setUsePathfinderExtendedMaxNodes (false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugWorldSynch).enabled == - true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "return factions[unitFactionIndex].precachedTravelState[unit->getId()];"); - unit-> - logSynchData (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - - return faction.precachedTravelState[unit->getId ()]; - } - else - { - clearUnitPrecache (unit); - } - } - else - { - - bool - foundPrecacheTravelStateIsBlocked = - (faction.precachedTravelState[unit->getId ()] == - tsBlocked); - - if (foundPrecacheTravelStateIsBlocked == true) - { - path->incBlockCount (); - unit->setUsePathfinderExtendedMaxNodes (false); - -// if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true && frameIndex < 0) { -// char szBuf[8096]=""; -// snprintf(szBuf,8096,"return factions[unitFactionIndex].precachedTravelState[unit->getId()];"); -// unit->logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,szBuf); -// } - - return faction.precachedTravelState[unit->getId ()]; - } - } - } - } - else - { - clearUnitPrecache (unit); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "[clearUnitPrecache]"); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - } - - const Vec2i - unitPos = unit->getPos (); - const Vec2i - finalPos = computeNearestFreePos (unit, targetPos); - - float - dist = unitPos.dist (finalPos); - - faction.useMaxNodeCount = PathFinder::pathFindNodesMax; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - //path find algorithm - - //a) push starting pos into openNodes - Node * - firstNode = newNode (faction, maxNodeCount); - if (firstNode == NULL) - { - throw - megaglest_runtime_error ("firstNode == NULL"); - } - - firstNode->next = NULL; - firstNode->prev = NULL; - firstNode->pos = unitPos; - firstNode->heuristic = heuristic (unitPos, finalPos); - firstNode->exploredCell = true; - if (faction.openNodesList.find (firstNode->heuristic) == - faction.openNodesList.end ()) - { - faction.openNodesList[firstNode->heuristic].clear (); - } - faction.openNodesList[firstNode->heuristic].push_back (firstNode); - faction.openPosList[firstNode->pos] = true; - - //b) loop - bool - pathFound = true; - bool - nodeLimitReached = false; - Node * - node = NULL; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - // First check if unit currently blocked all around them, if so don't try to pathfind - if (inBailout == false && unitPos != finalPos) - { - int - failureCount = 0; - int - cellCount = 0; - - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = unitPos + Vec2i (i, j); - if (pos != unitPos) - { - bool - canUnitMoveToCell = - canUnitMoveSoon (unit, unitPos, pos); - if (canUnitMoveToCell == false) - { - failureCount++; - } - cellCount++; - } - } - } - nodeLimitReached = (failureCount == cellCount); - pathFound = !nodeLimitReached; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "nodeLimitReached: %d failureCount: %d cellCount: %d", - nodeLimitReached, failureCount, cellCount); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 1) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] **Check if dest blocked, distance for unit [%d - %s] from [%s] to [%s] is %.2f took msecs: %lld nodeLimitReached = %d, failureCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, unit->getId (), - unit->getFullName (false).c_str (), - unitPos.getString ().c_str (), - finalPos.getString ().c_str (), dist, - (long long int) chrono.getMillis (), - nodeLimitReached, failureCount); - - if (nodeLimitReached == false) - { - // First check if final destination blocked - failureCount = 0; - cellCount = 0; - - for (int i = -1; i <= 1; ++i) - { - for (int j = -1; j <= 1; ++j) - { - Vec2i - pos = finalPos + Vec2i (i, j); - if (pos != finalPos) - { - bool - canUnitMoveToCell = - canUnitMoveSoon (unit, pos, finalPos); - if (canUnitMoveToCell == false) - { - failureCount++; - } - cellCount++; - } - } - } - nodeLimitReached = (failureCount == cellCount); - pathFound = !nodeLimitReached; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "nodeLimitReached: %d failureCount: %d cellCount: %d", - nodeLimitReached, failureCount, cellCount); - unit-> - logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 1) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] **Check if dest blocked, distance for unit [%d - %s] from [%s] to [%s] is %.2f took msecs: %lld nodeLimitReached = %d, failureCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, unit->getId (), - unit->getFullName (false). - c_str (), - unitPos.getString ().c_str (), - finalPos.getString ().c_str (), - dist, - (long long int) chrono. - getMillis (), nodeLimitReached, - failureCount); - } - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "inBailout: %d unitPos: [%s] finalPos [%s]", - inBailout, unitPos.getString ().c_str (), - finalPos.getString ().c_str ()); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - } - // - - // START - std::map < std::pair < Vec2i, Vec2i >, bool > canAddNode; - std::map < Vec2i, bool > closedNodes; - std::map < Vec2i, Vec2i > cameFrom; - cameFrom[unitPos] = Vec2i (-1, -1); - - // Do the a-star base pathfind work if required - int - whileLoopCount = 0; - if (nodeLimitReached == false) - { - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Calling doAStarPathSearch nodeLimitReached: %d whileLoopCount: %d unitFactionIndex: %d pathFound: %d finalPos [%s] maxNodeCount: %d frameIndex: %d", - nodeLimitReached, whileLoopCount, unitFactionIndex, - pathFound, finalPos.getString ().c_str (), - maxNodeCount, frameIndex); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - doAStarPathSearch (nodeLimitReached, whileLoopCount, - unitFactionIndex, pathFound, node, finalPos, - closedNodes, cameFrom, canAddNode, unit, - maxNodeCount, frameIndex); - - if (searched_node_count != NULL) - { - *searched_node_count = whileLoopCount; - } - - // Now see if the unit is eligible for pathfind max nodes boost? - if (nodeLimitReached == true) - { - unit->incrementPathfindFailedConsecutiveFrameCount (); - } - else - { - unit->resetPathfindFailedConsecutiveFrameCount (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Calling doAStarPathSearch nodeLimitReached: %d whileLoopCount: %d unitFactionIndex: %d pathFound: %d finalPos [%s] maxNodeCount: %d pathFindNodesAbsoluteMax: %d frameIndex: %d", - nodeLimitReached, whileLoopCount, unitFactionIndex, - pathFound, finalPos.getString ().c_str (), - maxNodeCount, pathFindNodesAbsoluteMax, frameIndex); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - if (nodeLimitReached == true - && maxNodeCount != pathFindNodesAbsoluteMax) - { - if (unit-> - isLastPathfindFailedFrameWithinCurrentFrameTolerance () == - true) - { - if (frameIndex < 0) - { - unit->setLastPathfindFailedFrameToCurrentFrame (); - unit->setLastPathfindFailedPos (finalPos); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "calling aStar()"); - unit-> - logSynchData (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, szBuf); - } - - return aStar (unit, targetPos, false, frameIndex, - pathFindNodesAbsoluteMax); - } - } - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "nodeLimitReached: %d", - nodeLimitReached); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - } - - - Node * - lastNode = node; - - //if consumed all nodes find best node (to avoid strange behaviour) - if (nodeLimitReached == true) - { - - if (faction.closedNodesList.empty () == false) - { - float - bestHeuristic = - truncateDecimal < - float >(faction.closedNodesList.begin ()->first, 6); - if (lastNode != NULL && bestHeuristic < lastNode->heuristic) - { - lastNode = - faction.closedNodesList.begin ()->second.front (); - } - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - //check results of path finding - ts = tsImpossible; - if (pathFound == false || lastNode == firstNode) - { - if (minorDebugPathfinder) - printf - ("Legacy Pathfind Unit [%d - %s] NOT FOUND PATH count = %d frameIndex = %d\n", - unit->getId (), unit->getType ()->getName ().c_str (), - whileLoopCount, frameIndex); - - //blocked - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPathFinder).enabled == - true) - { - string - commandDesc = "none"; - Command * - command = unit->getCurrCommand (); - if (command != NULL && command->getCommandType () != NULL) - { - commandDesc = - command->getCommandType ()->toString (false); - } - - std::pair < Vec2i, int > - lastHarvest = unit->getLastHarvestResourceTarget (); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "State: blocked, cmd [%s] pos: [%s], dest pos: [%s], lastHarvest = [%s - %d], reason A= %d, B= %d, C= %d, D= %d, E= %d, F = %d", - commandDesc.c_str (), - unit->getPos ().getString ().c_str (), - targetPos.getString ().c_str (), - lastHarvest.first.getString ().c_str (), - lastHarvest.second, pathFound, - (lastNode == firstNode), path->getBlockCount (), - path->isBlocked (), nodeLimitReached, - path->isStuck ()); - unit->setCurrentUnitTitle (szBuf); - } - - if (frameIndex < 0) - { - unit->setUsePathfinderExtendedMaxNodes (false); - } - - ts = tsBlocked; - if (frameIndex < 0) - { - path->incBlockCount (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - } - else - { - if (minorDebugPathfinder) - printf - ("Legacy Pathfind Unit [%d - %s] FOUND PATH count = %d frameIndex = %d\n", - unit->getId (), unit->getType ()->getName ().c_str (), - whileLoopCount, frameIndex); - //on the way - ts = tsMoving; - - //build next pointers - Node * - currNode = lastNode; - while (currNode->prev != NULL) - { - currNode->prev->next = currNode; - currNode = currNode->prev; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - - if (frameIndex < 0) - { - if (maxNodeCount == pathFindNodesAbsoluteMax) - { - unit->setUsePathfinderExtendedMaxNodes (true); - } - else - { - unit->setUsePathfinderExtendedMaxNodes (false); - } - } - - //store path - if (frameIndex < 0) - { - path->clear (); - } - - //UnitPathBasic *basicPathFinder = dynamic_cast(path); - - currNode = firstNode; - - for (int i = 0; currNode->next != NULL; - currNode = currNode->next, i++) - { - Vec2i - nodePos = currNode->next->pos; - if (map->isInside (nodePos) == false - || map->isInsideSurface (map->toSurfCoords (nodePos)) == - false) - { - throw - megaglest_runtime_error - ("Pathfinder invalid node path position = " + - nodePos.getString () + " i = " + intToStr (i)); - } - - if (minorDebugPathfinder) - printf ("nodePos [%s]\n", nodePos.getString ().c_str ()); - - if (frameIndex >= 0) - { - faction.precachedPath[unit->getId ()].push_back (nodePos); - } - else - { - if (i < unit->getPathFindRefreshCellCount () || - (whileLoopCount >= pathFindExtendRefreshForNodeCount - && i < getPathFindExtendRefreshNodeCount (faction))) - { - path->add (nodePos); - } - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - - string - pathToTake = ""; - if (frameIndex < 0) - { - vector < Vec2i > pathQueue = path->getQueue (); - for (unsigned int index = 0; index < pathQueue.size (); - ++index) - { - Vec2i & pos = pathQueue[index]; - if (pathToTake != "") - { - pathToTake += ", "; - } - pathToTake += pos.getString (); - } - } - else - { - for (unsigned int index = 0; - index < - faction.precachedPath[unit->getId ()].size (); - ++index) - { - Vec2i & pos = - faction.precachedPath[unit->getId ()][index]; - if (pathToTake != "") - { - pathToTake += ", "; - } - pathToTake += pos.getString (); - } - } - snprintf (szBuf, 8096, "Path for unit to take = %s", - pathToTake.c_str ()); - if (frameIndex < 0) - { - unit-> - logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - else - { - unit-> - logSynchDataThreaded (extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__, - szBuf); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPathFinder).enabled == - true) - { - string - commandDesc = "none"; - Command * - command = unit->getCurrCommand (); - if (command != NULL && command->getCommandType () != NULL) - { - commandDesc = - command->getCommandType ()->toString (false); - } - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "State: moving, cmd [%s] pos: %s dest pos: %s, Queue= %d", - commandDesc.c_str (), - unit->getPos ().getString ().c_str (), - targetPos.getString ().c_str (), - path->getQueueCount ()); - unit->setCurrentUnitTitle (szBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - } - - - faction.openNodesList.clear (); - faction.openPosList.clear (); - faction.closedNodesList.clear (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled == true && chrono.getMillis () > 4) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (frameIndex >= 0) - { - - FactionState & faction = factions.getFactionState (factionIndex); - faction.precachedTravelState[unit->getId ()] = ts; - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED && chrono.getMillis () >= 5) - printf - ("In [%s::%s Line: %d] astar took [%lld] msecs, ts = %d.\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, (long long int) chrono.getMillis (), - ts); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true && frameIndex < 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "return ts: %d", ts); - unit->logSynchData (extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__, szBuf); - } - - } - catch (const exception & ex) - { - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - throw - megaglest_runtime_error (ex.what ()); - } - catch ( ...) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, - __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - - return ts; - } - - void - PathFinder::processNearestFreePos (const Vec2i & finalPos, int i, int j, - int size, Field field, int teamIndex, - Vec2i unitPos, Vec2i & nearestPos, - float &nearestDist) - { - - try - { - Vec2i - currPos = finalPos + Vec2i (i, j); - - if (map->isAproxFreeCells (currPos, size, field, teamIndex)) - { - float - dist = currPos.dist (finalPos); - - //if nearer from finalPos - if (dist < nearestDist) - { - nearestPos = currPos; - nearestDist = dist; - } - //if the distance is the same compare distance to unit - else if (dist == nearestDist) - { - if (currPos.dist (unitPos) < nearestPos.dist (unitPos)) - { - nearestPos = currPos; - } - } - } - } - catch (const exception & ex) - { - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - throw - megaglest_runtime_error (ex.what ()); - } - catch ( ...) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, - __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - - } - - Vec2i - PathFinder::computeNearestFreePos (const Unit * unit, - const Vec2i & finalPos) - { - - Vec2i - nearestPos (0, 0); - try - { - - if (map == NULL) - { - throw - megaglest_runtime_error ("map == NULL"); - } - - //unit data - int - size = unit->getType ()->getSize (); - Field - field = unit->getCurrField (); - int - teamIndex = unit->getTeam (); - - //if finalPos is free return it - if (map->isAproxFreeCells (finalPos, size, field, teamIndex)) - { - return finalPos; - } - - //find nearest pos - Vec2i - unitPos = unit->getPosNotThreadSafe (); - nearestPos = unitPos; - - float - nearestDist = unitPos.dist (finalPos); - - for (int i = -maxFreeSearchRadius; i <= maxFreeSearchRadius; ++i) - { - for (int j = -maxFreeSearchRadius; j <= maxFreeSearchRadius; ++j) - { - processNearestFreePos (finalPos, i, j, size, field, teamIndex, - unitPos, nearestPos, nearestDist); - } - } - - } - catch (const exception & ex) - { - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - throw - megaglest_runtime_error (ex.what ()); - } - catch ( ...) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, - __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - - return nearestPos; - } - - int - PathFinder::findNodeIndex (Node * node, Nodes & nodeList) - { - int - index = -1; - if (node != NULL) - { - for (unsigned int i = 0; i < nodeList.size (); ++i) - { - Node * - curnode = nodeList[i]; - if (node == curnode) - { - index = i; - break; - } - } - } - return index; - } - - int - PathFinder::findNodeIndex (Node * node, std::vector < Node > &nodeList) - { - int - index = -1; - if (node != NULL) - { - for (unsigned int i = 0; i < nodeList.size (); ++i) - { - Node & curnode = nodeList[i]; - if (node == &curnode) - { - index = i; - break; - } - } - } - return index; - } - -//bool PathFinder::unitCannotMove(Unit *unit) { -// bool unitImmediatelyBlocked = false; -// -// try { -// // First check if unit currently blocked all around them, if so don't try to pathfind -// const Vec2i unitPos = unit->getPos(); -// int failureCount = 0; -// int cellCount = 0; -// -// for(int i = -1; i <= 1; ++i) { -// for(int j = -1; j <= 1; ++j) { -// Vec2i pos = unitPos + Vec2i(i, j); -// if(pos != unitPos) { -// bool canUnitMoveToCell = map->aproxCanMove(unit, unitPos, pos); -// if(canUnitMoveToCell == false) { -// failureCount++; -// } -// cellCount++; -// } -// } -// } -// unitImmediatelyBlocked = (failureCount == cellCount); -// -// } -// catch(const exception &ex) { -// //setRunningStatus(false); -// -// SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -// -// throw megaglest_runtime_error(ex.what()); -// } -// catch(...) { -// char szBuf[8096]=""; -// snprintf(szBuf,8096,"In [%s::%s %d] UNKNOWN error\n",__FILE__,__FUNCTION__,__LINE__); -// SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); -// throw megaglest_runtime_error(szBuf); -// } -// -// return unitImmediatelyBlocked; -//} - - void - PathFinder::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - pathfinderNode = rootNode->addChild ("PathFinder"); - - pathfinderNode->addAttribute ("pathFindNodesMax", - intToStr (pathFindNodesMax), - mapTagReplacements); - pathfinderNode->addAttribute ("pathFindNodesAbsoluteMax", - intToStr (pathFindNodesAbsoluteMax), - mapTagReplacements); - for (unsigned int i = 0; i < (unsigned int) factions.size (); ++i) - { - FactionState & factionState = factions.getFactionState (i); - XmlNode * - factionsNode = pathfinderNode->addChild ("factions"); - - for (unsigned int j = 0; - j < (unsigned int) factionState.nodePool.size (); ++j) - { - Node * - curNode = &factionState.nodePool[j]; - XmlNode * - nodePoolNode = factionsNode->addChild ("nodePool"); - - nodePoolNode->addAttribute ("pos", curNode->pos.getString (), - mapTagReplacements); - int - nextIdx = - findNodeIndex (curNode->next, factionState.nodePool); - nodePoolNode->addAttribute ("next", intToStr (nextIdx), - mapTagReplacements); - int - prevIdx = - findNodeIndex (curNode->prev, factionState.nodePool); - nodePoolNode->addAttribute ("prev", intToStr (prevIdx), - mapTagReplacements); - nodePoolNode->addAttribute ("heuristic", - floatToStr (curNode->heuristic, 6), - mapTagReplacements); - nodePoolNode->addAttribute ("exploredCell", - intToStr (curNode->exploredCell), - mapTagReplacements); - } - - factionsNode->addAttribute ("nodePoolCount", - intToStr (factionState.nodePoolCount), - mapTagReplacements); - factionsNode->addAttribute ("random", - intToStr (factionState.random. - getLastNumber ()), - mapTagReplacements); - factionsNode->addAttribute ("useMaxNodeCount", - intToStr (factionState.useMaxNodeCount), - mapTagReplacements); - } - } - - void - PathFinder::loadGame (const XmlNode * rootNode) - { - const XmlNode * - pathfinderNode = rootNode->getChild ("PathFinder"); - - vector < XmlNode * >factionsNodeList = - pathfinderNode->getChildList ("factions"); - for (unsigned int i = 0; i < (unsigned int) factionsNodeList.size (); - ++i) - { - XmlNode * - factionsNode = factionsNodeList[i]; - - FactionState & factionState = factions.getFactionState (i); - vector < XmlNode * >nodePoolListNode = - factionsNode->getChildList ("nodePool"); - for (unsigned int j = 0; - j < (unsigned int) nodePoolListNode.size () - && j < (unsigned int) pathFindNodesAbsoluteMax; ++j) - { - XmlNode * - nodePoolNode = nodePoolListNode[j]; - - Node * - curNode = &factionState.nodePool[j]; - curNode->pos = - Vec2i::strToVec2 (nodePoolNode->getAttribute ("pos")-> - getValue ()); - int - nextNode = - nodePoolNode->getAttribute ("next")->getIntValue (); - if (nextNode >= 0) - { - curNode->next = &factionState.nodePool[nextNode]; - } - else - { - curNode->next = NULL; - } - - int - prevNode = - nodePoolNode->getAttribute ("prev")->getIntValue (); - if (prevNode >= 0) - { - curNode->prev = &factionState.nodePool[prevNode]; - } - else - { - curNode->prev = NULL; - } - curNode->heuristic = - nodePoolNode->getAttribute ("heuristic")->getFloatValue (); - curNode->exploredCell = - nodePoolNode->getAttribute ("exploredCell")->getIntValue () != - 0; - } - - factionState.nodePoolCount = - factionsNode->getAttribute ("nodePoolCount")->getIntValue (); - factionState.random.setLastNumber (factionsNode-> - getAttribute ("random")-> - getIntValue ()); - factionState.useMaxNodeCount = PathFinder::pathFindNodesMax; - } - } - - } + Glest { + namespace + Game { + + // ===================================================== + // class PathFinder + // ===================================================== + + // ===================== PUBLIC ======================== + + const int + PathFinder::maxFreeSearchRadius = 10; + + int + PathFinder::pathFindNodesAbsoluteMax = 900; + int + PathFinder::pathFindNodesMax = 2000; + const int + PathFinder::pathFindBailoutRadius = 20; + const int + PathFinder::pathFindExtendRefreshForNodeCount = 25; + const int + PathFinder::pathFindExtendRefreshNodeCountMin = 40; + const int + PathFinder::pathFindExtendRefreshNodeCountMax = 40; + + PathFinder::PathFinder() { + minorDebugPathfinder = false; + map = NULL; + } + + int + PathFinder::getPathFindExtendRefreshNodeCount(FactionState & faction) { + //int refreshNodeCount = faction.random.randRange(PathFinder::pathFindExtendRefreshNodeCountMin,PathFinder::pathFindExtendRefreshNodeCountMax); + //return refreshNodeCount; + return PathFinder::pathFindExtendRefreshNodeCountMin; + } + + PathFinder::PathFinder(const Map * map) { + minorDebugPathfinder = false; + + map = NULL; + init(map); + } + + void + PathFinder::init(const Map * map) { + for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; + ++factionIndex) { + FactionState & faction = factions.getFactionState(factionIndex); + + faction.nodePool.resize(pathFindNodesAbsoluteMax); + faction.useMaxNodeCount = PathFinder::pathFindNodesMax; + } + this->map = map; + } + + void + PathFinder::init() { + minorDebugPathfinder = false; + map = NULL; + } + + PathFinder::~PathFinder() { + for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; + ++factionIndex) { + FactionState & faction = factions.getFactionState(factionIndex); + + faction.nodePool.clear(); + } + factions.clear(); + map = NULL; + } + + void + PathFinder::clearCaches() { + for (int factionIndex = 0; factionIndex < GameConstants::maxPlayers; + ++factionIndex) { + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + FactionState & faction = factions.getFactionState(factionIndex); + MutexSafeWrapper + safeMutex(faction.getMutexPreCache(), mutexOwnerId); + + faction.precachedTravelState.clear(); + faction.precachedPath.clear(); + } + } + + void + PathFinder::clearUnitPrecache(Unit * unit) { + if (unit != NULL && factions.size() > unit->getFactionIndex()) { + int + factionIndex = unit->getFactionIndex(); + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + FactionState & faction = factions.getFactionState(factionIndex); + MutexSafeWrapper + safeMutex(faction.getMutexPreCache(), mutexOwnerId); + + faction.precachedTravelState[unit->getId()] = tsImpossible; + faction.precachedPath[unit->getId()].clear(); + } + } + + void + PathFinder::removeUnitPrecache(Unit * unit) { + if (unit != NULL && factions.size() > unit->getFactionIndex()) { + int + factionIndex = unit->getFactionIndex(); + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + FactionState & faction = factions.getFactionState(factionIndex); + MutexSafeWrapper + safeMutex(faction.getMutexPreCache(), mutexOwnerId); + + if (faction.precachedTravelState.find(unit->getId()) != + faction.precachedTravelState.end()) { + faction.precachedTravelState.erase(unit->getId()); + } + if (faction.precachedPath.find(unit->getId()) != + faction.precachedPath.end()) { + faction.precachedPath.erase(unit->getId()); + } + } + } + + TravelState + PathFinder::findPath(Unit * unit, const Vec2i & finalPos, + bool * wasStuck, int frameIndex) { + TravelState + ts = tsImpossible; + + try { + + int + factionIndex = unit->getFactionIndex(); + FactionState & faction = factions.getFactionState(factionIndex); + static string + mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexPrecache(faction.getMutexPreCache(), mutexOwnerId); + + if (map == NULL) { + throw + megaglest_runtime_error("map == NULL"); + } + + unit->setCurrentPathFinderDesiredFinalPos(finalPos); + + + if (frameIndex >= 0) { + clearUnitPrecache(unit); + } + if (unit->getFaction()->canUnitsPathfind() == true) { + unit->getFaction()->addUnitToPathfindingList(unit->getId()); + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "canUnitsPathfind() == false"); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + return tsBlocked; + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[findPath] unit->getPos() [%s] finalPos [%s]", + unit->getPos().getString().c_str(), + finalPos.getString().c_str()); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + //route cache + if (finalPos == unit->getPos()) { + if (frameIndex < 0) { + //if arrived + unit->setCurrSkill(scStop); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Unit finalPos [%s] == unit->getPos() [%s]", + finalPos.getString().c_str(), + unit->getPos().getString().c_str()); + unit-> + logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPathFinder).enabled == + true) { + string + commandDesc = "none"; + Command * + command = unit->getCurrCommand(); + if (command != NULL && command->getCommandType() != NULL) { + commandDesc = + command->getCommandType()->toString(false); + } + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "State: arrived#1 at pos: %s, command [%s]", + finalPos.getString().c_str(), + commandDesc.c_str()); + unit->setCurrentUnitTitle(szBuf); + } + + return tsArrived; + } + + UnitPathInterface * + path = unit->getPath(); + if (path->isEmpty() == false) { + UnitPathBasic * + basic_path = dynamic_cast (path); + if (basic_path != NULL) { + //route cache + Vec2i + pos = basic_path->pop(frameIndex < 0); + + if (map->canMove(unit, unit->getPos(), pos)) { + if (frameIndex < 0) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch).enabled == + true + && SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "#1 map->canMove to pos [%s] from [%s]", + pos.getString().c_str(), + unit->getPos().getString().c_str()); + unit-> + logSynchData(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + unit->setTargetPos(pos, frameIndex < 0); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch).enabled == + true + && SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "#2 map->canMove to pos [%s] from [%s]", + pos.getString().c_str(), + unit->getPos().getString().c_str()); + unit-> + logSynchData(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + } + + return tsMoving; + } + } else if (dynamic_cast (path) != NULL) { + UnitPath * + advPath = dynamic_cast (path); + //route cache + Vec2i + pos = advPath->peek(); + if (map->canMove(unit, unit->getPos(), pos)) { + if (frameIndex < 0) { + advPath->pop(); + unit->setTargetPos(pos, frameIndex < 0); + } + + return tsMoving; + } + } else { + throw + megaglest_runtime_error + ("unsupported or missing path finder detected!"); + } + } + + if (path->isStuck() == true && + (unit->getLastStuckPos() == finalPos + || path->getBlockCount() > 500) + && unit-> + isLastStuckFrameWithinCurrentFrameTolerance(frameIndex >= 0) == + true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "path->isStuck() == true unit->getLastStuckPos() [%s] finalPos [%s] path->getBlockCount() [%d]", + unit->getLastStuckPos().getString().c_str(), + finalPos.getString().c_str(), + path->getBlockCount()); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + return tsBlocked; + } + + //route cache miss + int + maxNodeCount = -1; + if (unit->getUsePathfinderExtendedMaxNodes() == true) { + + maxNodeCount = PathFinder::pathFindNodesAbsoluteMax; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "maxNodeCount: %d", maxNodeCount); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + } + + bool + minorDebugPathfinderPerformance = false; + Chrono + chrono; + if (minorDebugPathfinderPerformance) + chrono.start(); + + uint32 + searched_node_count = 0; + minorDebugPathfinder = false; + if (minorDebugPathfinder) + printf + ("Legacy Pathfind Unit [%d - %s] from = %s to = %s frameIndex = %d\n", + unit->getId(), unit->getType()->getName(false).c_str(), + unit->getPos().getString().c_str(), + finalPos.getString().c_str(), frameIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "calling aStar()"); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + ts = + aStar(unit, finalPos, false, frameIndex, maxNodeCount, + &searched_node_count); + //post actions + switch (ts) { + case tsBlocked: + case tsArrived: + // The unit is stuck (not only blocked but unable to go anywhere for a while) + // We will try to bail out of the immediate area + if (ts == tsBlocked && unit->getInBailOutAttempt() == false && + path->isStuck() == true) { + + if (minorDebugPathfinder) + printf + ("Pathfind Unit [%d - %s] START BAILOUT ATTEMPT frameIndex = %d\n", + unit->getId(), + unit->getType()->getName(false).c_str(), frameIndex); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[attempting to BAIL OUT] finalPos [%s] ts [%d]", + finalPos.getString().c_str(), ts); + unit-> + logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + if (wasStuck != NULL) { + *wasStuck = true; + } + unit->setInBailOutAttempt(true); + + bool + unitImmediatelyBlocked = false; + + // First check if unit currently blocked all around them, if so don't try to pathfind + const Vec2i + unitPos = unit->getPos(); + int + failureCount = 0; + int + cellCount = 0; + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = unitPos + Vec2i(i, j); + if (pos != unitPos) { + bool + canUnitMoveToCell = + map->aproxCanMove(unit, unitPos, pos); + if (canUnitMoveToCell == false) { + failureCount++; + } + cellCount++; + } + } + } + unitImmediatelyBlocked = (failureCount == cellCount); + if (unitImmediatelyBlocked == false) { + + int + factionIndex = unit->getFactionIndex(); + FactionState & faction = + factions.getFactionState(factionIndex); + + //if(Thread::isCurrentThreadMainThread() == false) { + // throw megaglest_runtime_error("#2 Invalid access to FactionState random from outside main thread current id = " + + // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); + //} + + int + tryRadius = faction.random.randRange(1, 2); + //int tryRadius = faction.random.IRandomX(1,2); + //int tryRadius = 1; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In astar bailout() tryRadius %d", + tryRadius); + + if (frameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, + szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + // Try to bail out up to PathFinder::pathFindBailoutRadius cells away + if (tryRadius == 2) { + for (int bailoutX = + -PathFinder::pathFindBailoutRadius; + bailoutX <= PathFinder::pathFindBailoutRadius + && ts == tsBlocked; ++bailoutX) { + for (int bailoutY = + -PathFinder::pathFindBailoutRadius; + bailoutY <= PathFinder::pathFindBailoutRadius + && ts == tsBlocked; ++bailoutY) { + const Vec2i + newFinalPos = + finalPos + Vec2i(bailoutX, bailoutY); + bool + canUnitMove = + map->canMove(unit, unit->getPos(), + newFinalPos); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[attempting to BAIL OUT] finalPos [%s] newFinalPos [%s] ts [%d] canUnitMove [%d]", + finalPos.getString().c_str(), + newFinalPos.getString(). + c_str(), ts, canUnitMove); + unit-> + logSynchData + (extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, szBuf); + } + + if (canUnitMove) { + + int + maxBailoutNodeCount = + (PathFinder::pathFindBailoutRadius * 2); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "calling aStar()"); + unit-> + logSynchData + (extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + ts = + aStar(unit, newFinalPos, true, + frameIndex, maxBailoutNodeCount, + &searched_node_count); + } + } + } + } else { + for (int bailoutX = PathFinder::pathFindBailoutRadius; + bailoutX >= -PathFinder::pathFindBailoutRadius + && ts == tsBlocked; --bailoutX) { + for (int bailoutY = + PathFinder::pathFindBailoutRadius; + bailoutY >= + -PathFinder::pathFindBailoutRadius + && ts == tsBlocked; --bailoutY) { + const Vec2i + newFinalPos = + finalPos + Vec2i(bailoutX, bailoutY); + bool + canUnitMove = + map->canMove(unit, unit->getPos(), + newFinalPos); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[attempting to BAIL OUT] finalPos [%s] newFinalPos [%s] ts [%d] canUnitMove [%d]", + finalPos.getString().c_str(), + newFinalPos.getString(). + c_str(), ts, canUnitMove); + unit-> + logSynchData + (extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, szBuf); + } + + if (canUnitMove) { + int + maxBailoutNodeCount = + (PathFinder::pathFindBailoutRadius * 2); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "calling aStar()"); + unit-> + logSynchData + (extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + ts = + aStar(unit, newFinalPos, true, + frameIndex, maxBailoutNodeCount, + &searched_node_count); + } + } + } + } + } + unit->setInBailOutAttempt(false); + + if (ts == tsBlocked) { + unit->setLastStuckFrameToCurrentFrame(); + unit->setLastStuckPos(finalPos); + } + } + if (ts == tsArrived || ts == tsBlocked) { + if (frameIndex < 0) { + unit->setCurrSkill(scStop); + } + } + break; + case tsMoving: + { + if (dynamic_cast (path) != NULL) { + UnitPathBasic * + basicPath = dynamic_cast (path); + Vec2i + pos; + if (frameIndex < 0 && basicPath != NULL) { + pos = basicPath->pop(frameIndex < 0); + } else { + + if (faction.precachedPath[unit->getId()].size() <= 0) { + throw + megaglest_runtime_error + ("factions[unit->getFactionIndex()].precachedPath[unit->getId()].size() <= 0!"); + } + + pos = faction.precachedPath[unit->getId()][0]; + + } + + if (map->canMove(unit, unit->getPos(), pos)) { + if (frameIndex < 0) { + unit->setTargetPos(pos, frameIndex < 0); + } + } else { + if (frameIndex < 0) { + unit->setCurrSkill(scStop); + } + + if (minorDebugPathfinderPerformance + && chrono.getMillis() >= 1) + printf + ("Unit [%d - %s] astar #2 took [%lld] msecs, ts = %d searched_node_count = %d.\n", + unit->getId(), + unit->getType()->getName(false).c_str(), + (long long int) chrono.getMillis(), ts, + searched_node_count); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "tsBlocked"); + unit-> + logSynchData(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + return tsBlocked; + } + } else if (dynamic_cast (path) != NULL) { + UnitPath * + advPath = dynamic_cast (path); + Vec2i + pos = advPath->peek(); + if (map->canMove(unit, unit->getPos(), pos)) { + if (frameIndex < 0) { + advPath->pop(); + unit->setTargetPos(pos, frameIndex < 0); + } + } else { + if (frameIndex < 0) { + unit->setCurrSkill(scStop); + } + + if (minorDebugPathfinder) + printf + ("Pathfind Unit [%d - %s] INT BAILOUT ATTEMPT BLOCKED frameIndex = %d\n", + unit->getId(), + unit->getType()->getName(false).c_str(), + frameIndex); + + if (minorDebugPathfinderPerformance + && chrono.getMillis() >= 1) + printf + ("Unit [%d - %s] astar #3 took [%lld] msecs, ts = %d searched_node_count = %d.\n", + unit->getId(), + unit->getType()->getName(false).c_str(), + (long long int) chrono.getMillis(), ts, + searched_node_count); + return tsBlocked; + } + } else { + throw + megaglest_runtime_error + ("unsupported or missing path finder detected!"); + } + } + break; + + default: + break; + } + if (minorDebugPathfinderPerformance && chrono.getMillis() >= 1) + printf + ("Unit [%d - %s] astar took [%lld] msecs, ts = %d searched_node_count = %d.\n", + unit->getId(), unit->getType()->getName(false).c_str(), + (long long int) chrono.getMillis(), ts, searched_node_count); + + } catch (const exception & ex) { + //setRunningStatus(false); + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + throw + megaglest_runtime_error(ex.what()); + } catch (...) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, + __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + + return ts; + } + + // ==================== PRIVATE ==================== + + //route a unit using A* algorithm + TravelState + PathFinder::aStar(Unit * unit, const Vec2i & targetPos, bool inBailout, + int frameIndex, int maxNodeCount, + uint32 * searched_node_count) { + TravelState + ts = tsImpossible; + + try { + + int + unitFactionIndex = unit->getFactionIndex(); + int + factionIndex = unit->getFactionIndex(); + FactionState & faction = factions.getFactionState(factionIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex >= 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In aStar()"); + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } + + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono.start(); + + if (map == NULL) { + throw + megaglest_runtime_error("map == NULL"); + } + + + if (maxNodeCount < 0) { + + int + factionIndex = unit->getFactionIndex(); + FactionState & faction = factions.getFactionState(factionIndex); + + maxNodeCount = faction.useMaxNodeCount; + } + + if (maxNodeCount >= 1 + && unit->getPathfindFailedConsecutiveFrameCount() >= 3) { + maxNodeCount = 200; + } + + UnitPathInterface * + path = unit->getPath(); + + faction.nodePoolCount = 0; + faction.openNodesList.clear(); + faction.openPosList.clear(); + faction.closedNodesList.clear(); + + // check the pre-cache to see if we can re-use a cached path + if (frameIndex < 0) { + + bool + foundPrecacheTravelState = + (faction.precachedTravelState.find(unit->getId()) != + faction.precachedTravelState.end()); + if (foundPrecacheTravelState == true) { + + // if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true && frameIndex < 0) { + // char szBuf[8096]=""; + // snprintf(szBuf,8096,"factions[unitFactionIndex].precachedTravelState[unit->getId()]: %d",faction.precachedTravelState[unit->getId()]); + // unit->logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,szBuf); + // } + + bool + foundPrecacheTravelStateIsMoving = + (faction.precachedTravelState[unit->getId()] == tsMoving); + if (foundPrecacheTravelStateIsMoving == true) { + bool + canMoveToCells = true; + + Vec2i + lastPos = unit->getPos(); + + int + unitPrecachePathSize = + (int) faction.precachedPath[unit->getId()].size(); + + for (int i = 0; i < unitPrecachePathSize; i++) { + + Vec2i + nodePos = faction.precachedPath[unit->getId()][i]; + + if (map->isInside(nodePos) == false + || map->isInsideSurface(map-> + toSurfCoords(nodePos)) + == false) { + throw + megaglest_runtime_error + ("Pathfinder invalid node path position = " + + nodePos.getString() + " i = " + intToStr(i)); + } + + if (i < unit->getPathFindRefreshCellCount() || + (unitPrecachePathSize >= + pathFindExtendRefreshForNodeCount + && i < + getPathFindExtendRefreshNodeCount(faction))) { + + if (canUnitMoveSoon(unit, lastPos, nodePos) == + false) { + canMoveToCells = false; + break; + } + lastPos = nodePos; + } else { + break; + } + } + + if (canMoveToCells == true) { + path->clear(); + //UnitPathBasic *basicPathFinder = dynamic_cast(path); + + int + unitPrecachePathSize = + (int) faction.precachedPath[unit->getId()].size(); + + for (int i = 0; i < unitPrecachePathSize; i++) { + + Vec2i + nodePos = + faction.precachedPath[unit->getId()][i]; + + if (map->isInside(nodePos) == false + || map->isInsideSurface(map-> + toSurfCoords + (nodePos)) == false) { + throw + megaglest_runtime_error + ("Pathfinder invalid node path position = " + + nodePos.getString() + " i = " + + intToStr(i)); + } + + //if(i < pathFindRefresh || + if (i < unit->getPathFindRefreshCellCount() || + (unitPrecachePathSize >= + pathFindExtendRefreshForNodeCount + && i < + getPathFindExtendRefreshNodeCount(faction))) { + path->add(nodePos); + } + } + unit->setUsePathfinderExtendedMaxNodes(false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugWorldSynch).enabled == + true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "return factions[unitFactionIndex].precachedTravelState[unit->getId()];"); + unit-> + logSynchData(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + + return faction.precachedTravelState[unit->getId()]; + } else { + clearUnitPrecache(unit); + } + } else { + + bool + foundPrecacheTravelStateIsBlocked = + (faction.precachedTravelState[unit->getId()] == + tsBlocked); + + if (foundPrecacheTravelStateIsBlocked == true) { + path->incBlockCount(); + unit->setUsePathfinderExtendedMaxNodes(false); + + // if(SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true && frameIndex < 0) { + // char szBuf[8096]=""; + // snprintf(szBuf,8096,"return factions[unitFactionIndex].precachedTravelState[unit->getId()];"); + // unit->logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,szBuf); + // } + + return faction.precachedTravelState[unit->getId()]; + } + } + } + } else { + clearUnitPrecache(unit); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "[clearUnitPrecache]"); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + } + + const Vec2i + unitPos = unit->getPos(); + const Vec2i + finalPos = computeNearestFreePos(unit, targetPos); + + float + dist = unitPos.dist(finalPos); + + faction.useMaxNodeCount = PathFinder::pathFindNodesMax; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + //path find algorithm + + //a) push starting pos into openNodes + Node * + firstNode = newNode(faction, maxNodeCount); + if (firstNode == NULL) { + throw + megaglest_runtime_error("firstNode == NULL"); + } + + firstNode->next = NULL; + firstNode->prev = NULL; + firstNode->pos = unitPos; + firstNode->heuristic = heuristic(unitPos, finalPos); + firstNode->exploredCell = true; + if (faction.openNodesList.find(firstNode->heuristic) == + faction.openNodesList.end()) { + faction.openNodesList[firstNode->heuristic].clear(); + } + faction.openNodesList[firstNode->heuristic].push_back(firstNode); + faction.openPosList[firstNode->pos] = true; + + //b) loop + bool + pathFound = true; + bool + nodeLimitReached = false; + Node * + node = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + // First check if unit currently blocked all around them, if so don't try to pathfind + if (inBailout == false && unitPos != finalPos) { + int + failureCount = 0; + int + cellCount = 0; + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = unitPos + Vec2i(i, j); + if (pos != unitPos) { + bool + canUnitMoveToCell = + canUnitMoveSoon(unit, unitPos, pos); + if (canUnitMoveToCell == false) { + failureCount++; + } + cellCount++; + } + } + } + nodeLimitReached = (failureCount == cellCount); + pathFound = !nodeLimitReached; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "nodeLimitReached: %d failureCount: %d cellCount: %d", + nodeLimitReached, failureCount, cellCount); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 1) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] **Check if dest blocked, distance for unit [%d - %s] from [%s] to [%s] is %.2f took msecs: %lld nodeLimitReached = %d, failureCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, unit->getId(), + unit->getFullName(false).c_str(), + unitPos.getString().c_str(), + finalPos.getString().c_str(), dist, + (long long int) chrono.getMillis(), + nodeLimitReached, failureCount); + + if (nodeLimitReached == false) { + // First check if final destination blocked + failureCount = 0; + cellCount = 0; + + for (int i = -1; i <= 1; ++i) { + for (int j = -1; j <= 1; ++j) { + Vec2i + pos = finalPos + Vec2i(i, j); + if (pos != finalPos) { + bool + canUnitMoveToCell = + canUnitMoveSoon(unit, pos, finalPos); + if (canUnitMoveToCell == false) { + failureCount++; + } + cellCount++; + } + } + } + nodeLimitReached = (failureCount == cellCount); + pathFound = !nodeLimitReached; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "nodeLimitReached: %d failureCount: %d cellCount: %d", + nodeLimitReached, failureCount, cellCount); + unit-> + logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 1) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] **Check if dest blocked, distance for unit [%d - %s] from [%s] to [%s] is %.2f took msecs: %lld nodeLimitReached = %d, failureCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, unit->getId(), + unit->getFullName(false). + c_str(), + unitPos.getString().c_str(), + finalPos.getString().c_str(), + dist, + (long long int) chrono. + getMillis(), nodeLimitReached, + failureCount); + } + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "inBailout: %d unitPos: [%s] finalPos [%s]", + inBailout, unitPos.getString().c_str(), + finalPos.getString().c_str()); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + } + // + + // START + std::map < std::pair < Vec2i, Vec2i >, bool > canAddNode; + std::map < Vec2i, bool > closedNodes; + std::map < Vec2i, Vec2i > cameFrom; + cameFrom[unitPos] = Vec2i(-1, -1); + + // Do the a-star base pathfind work if required + int + whileLoopCount = 0; + if (nodeLimitReached == false) { + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Calling doAStarPathSearch nodeLimitReached: %d whileLoopCount: %d unitFactionIndex: %d pathFound: %d finalPos [%s] maxNodeCount: %d frameIndex: %d", + nodeLimitReached, whileLoopCount, unitFactionIndex, + pathFound, finalPos.getString().c_str(), + maxNodeCount, frameIndex); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + doAStarPathSearch(nodeLimitReached, whileLoopCount, + unitFactionIndex, pathFound, node, finalPos, + closedNodes, cameFrom, canAddNode, unit, + maxNodeCount, frameIndex); + + if (searched_node_count != NULL) { + *searched_node_count = whileLoopCount; + } + + // Now see if the unit is eligible for pathfind max nodes boost? + if (nodeLimitReached == true) { + unit->incrementPathfindFailedConsecutiveFrameCount(); + } else { + unit->resetPathfindFailedConsecutiveFrameCount(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Calling doAStarPathSearch nodeLimitReached: %d whileLoopCount: %d unitFactionIndex: %d pathFound: %d finalPos [%s] maxNodeCount: %d pathFindNodesAbsoluteMax: %d frameIndex: %d", + nodeLimitReached, whileLoopCount, unitFactionIndex, + pathFound, finalPos.getString().c_str(), + maxNodeCount, pathFindNodesAbsoluteMax, frameIndex); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + if (nodeLimitReached == true + && maxNodeCount != pathFindNodesAbsoluteMax) { + if (unit-> + isLastPathfindFailedFrameWithinCurrentFrameTolerance() == + true) { + if (frameIndex < 0) { + unit->setLastPathfindFailedFrameToCurrentFrame(); + unit->setLastPathfindFailedPos(finalPos); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "calling aStar()"); + unit-> + logSynchData(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, szBuf); + } + + return aStar(unit, targetPos, false, frameIndex, + pathFindNodesAbsoluteMax); + } + } + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "nodeLimitReached: %d", + nodeLimitReached); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + } + + + Node * + lastNode = node; + + //if consumed all nodes find best node (to avoid strange behaviour) + if (nodeLimitReached == true) { + + if (faction.closedNodesList.empty() == false) { + float + bestHeuristic = + truncateDecimal < + float >(faction.closedNodesList.begin()->first, 6); + if (lastNode != NULL && bestHeuristic < lastNode->heuristic) { + lastNode = + faction.closedNodesList.begin()->second.front(); + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + //check results of path finding + ts = tsImpossible; + if (pathFound == false || lastNode == firstNode) { + if (minorDebugPathfinder) + printf + ("Legacy Pathfind Unit [%d - %s] NOT FOUND PATH count = %d frameIndex = %d\n", + unit->getId(), unit->getType()->getName().c_str(), + whileLoopCount, frameIndex); + + //blocked + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPathFinder).enabled == + true) { + string + commandDesc = "none"; + Command * + command = unit->getCurrCommand(); + if (command != NULL && command->getCommandType() != NULL) { + commandDesc = + command->getCommandType()->toString(false); + } + + std::pair < Vec2i, int > + lastHarvest = unit->getLastHarvestResourceTarget(); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "State: blocked, cmd [%s] pos: [%s], dest pos: [%s], lastHarvest = [%s - %d], reason A= %d, B= %d, C= %d, D= %d, E= %d, F = %d", + commandDesc.c_str(), + unit->getPos().getString().c_str(), + targetPos.getString().c_str(), + lastHarvest.first.getString().c_str(), + lastHarvest.second, pathFound, + (lastNode == firstNode), path->getBlockCount(), + path->isBlocked(), nodeLimitReached, + path->isStuck()); + unit->setCurrentUnitTitle(szBuf); + } + + if (frameIndex < 0) { + unit->setUsePathfinderExtendedMaxNodes(false); + } + + ts = tsBlocked; + if (frameIndex < 0) { + path->incBlockCount(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + } else { + if (minorDebugPathfinder) + printf + ("Legacy Pathfind Unit [%d - %s] FOUND PATH count = %d frameIndex = %d\n", + unit->getId(), unit->getType()->getName().c_str(), + whileLoopCount, frameIndex); + //on the way + ts = tsMoving; + + //build next pointers + Node * + currNode = lastNode; + while (currNode->prev != NULL) { + currNode->prev->next = currNode; + currNode = currNode->prev; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + + if (frameIndex < 0) { + if (maxNodeCount == pathFindNodesAbsoluteMax) { + unit->setUsePathfinderExtendedMaxNodes(true); + } else { + unit->setUsePathfinderExtendedMaxNodes(false); + } + } + + //store path + if (frameIndex < 0) { + path->clear(); + } + + //UnitPathBasic *basicPathFinder = dynamic_cast(path); + + currNode = firstNode; + + for (int i = 0; currNode->next != NULL; + currNode = currNode->next, i++) { + Vec2i + nodePos = currNode->next->pos; + if (map->isInside(nodePos) == false + || map->isInsideSurface(map->toSurfCoords(nodePos)) == + false) { + throw + megaglest_runtime_error + ("Pathfinder invalid node path position = " + + nodePos.getString() + " i = " + intToStr(i)); + } + + if (minorDebugPathfinder) + printf("nodePos [%s]\n", nodePos.getString().c_str()); + + if (frameIndex >= 0) { + faction.precachedPath[unit->getId()].push_back(nodePos); + } else { + if (i < unit->getPathFindRefreshCellCount() || + (whileLoopCount >= pathFindExtendRefreshForNodeCount + && i < getPathFindExtendRefreshNodeCount(faction))) { + path->add(nodePos); + } + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + + string + pathToTake = ""; + if (frameIndex < 0) { + vector < Vec2i > pathQueue = path->getQueue(); + for (unsigned int index = 0; index < pathQueue.size(); + ++index) { + Vec2i & pos = pathQueue[index]; + if (pathToTake != "") { + pathToTake += ", "; + } + pathToTake += pos.getString(); + } + } else { + for (unsigned int index = 0; + index < + faction.precachedPath[unit->getId()].size(); + ++index) { + Vec2i & pos = + faction.precachedPath[unit->getId()][index]; + if (pathToTake != "") { + pathToTake += ", "; + } + pathToTake += pos.getString(); + } + } + snprintf(szBuf, 8096, "Path for unit to take = %s", + pathToTake.c_str()); + if (frameIndex < 0) { + unit-> + logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } else { + unit-> + logSynchDataThreaded(extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__, + szBuf); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPathFinder).enabled == + true) { + string + commandDesc = "none"; + Command * + command = unit->getCurrCommand(); + if (command != NULL && command->getCommandType() != NULL) { + commandDesc = + command->getCommandType()->toString(false); + } + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "State: moving, cmd [%s] pos: %s dest pos: %s, Queue= %d", + commandDesc.c_str(), + unit->getPos().getString().c_str(), + targetPos.getString().c_str(), + path->getQueueCount()); + unit->setCurrentUnitTitle(szBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + } + + + faction.openNodesList.clear(); + faction.openPosList.clear(); + faction.closedNodesList.clear(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled == true && chrono.getMillis() > 4) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld --------------------------- [END OF METHOD] ---------------------------\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (frameIndex >= 0) { + + FactionState & faction = factions.getFactionState(factionIndex); + faction.precachedTravelState[unit->getId()] = ts; + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED && chrono.getMillis() >= 5) + printf + ("In [%s::%s Line: %d] astar took [%lld] msecs, ts = %d.\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, (long long int) chrono.getMillis(), + ts); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true && frameIndex < 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "return ts: %d", ts); + unit->logSynchData(extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__, szBuf); + } + + } catch (const exception & ex) { + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + throw + megaglest_runtime_error(ex.what()); + } catch (...) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, + __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + + return ts; + } + + void + PathFinder::processNearestFreePos(const Vec2i & finalPos, int i, int j, + int size, Field field, int teamIndex, + Vec2i unitPos, Vec2i & nearestPos, + float &nearestDist) { + + try { + Vec2i + currPos = finalPos + Vec2i(i, j); + + if (map->isAproxFreeCells(currPos, size, field, teamIndex)) { + float + dist = currPos.dist(finalPos); + + //if nearer from finalPos + if (dist < nearestDist) { + nearestPos = currPos; + nearestDist = dist; + } + //if the distance is the same compare distance to unit + else if (dist == nearestDist) { + if (currPos.dist(unitPos) < nearestPos.dist(unitPos)) { + nearestPos = currPos; + } + } + } + } catch (const exception & ex) { + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + throw + megaglest_runtime_error(ex.what()); + } catch (...) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, + __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + + } + + Vec2i + PathFinder::computeNearestFreePos(const Unit * unit, + const Vec2i & finalPos) { + + Vec2i + nearestPos(0, 0); + try { + + if (map == NULL) { + throw + megaglest_runtime_error("map == NULL"); + } + + //unit data + int + size = unit->getType()->getSize(); + Field + field = unit->getCurrField(); + int + teamIndex = unit->getTeam(); + + //if finalPos is free return it + if (map->isAproxFreeCells(finalPos, size, field, teamIndex)) { + return finalPos; + } + + //find nearest pos + Vec2i + unitPos = unit->getPosNotThreadSafe(); + nearestPos = unitPos; + + float + nearestDist = unitPos.dist(finalPos); + + for (int i = -maxFreeSearchRadius; i <= maxFreeSearchRadius; ++i) { + for (int j = -maxFreeSearchRadius; j <= maxFreeSearchRadius; ++j) { + processNearestFreePos(finalPos, i, j, size, field, teamIndex, + unitPos, nearestPos, nearestDist); + } + } + + } catch (const exception & ex) { + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + throw + megaglest_runtime_error(ex.what()); + } catch (...) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, + __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + + return nearestPos; + } + + int + PathFinder::findNodeIndex(Node * node, Nodes & nodeList) { + int + index = -1; + if (node != NULL) { + for (unsigned int i = 0; i < nodeList.size(); ++i) { + Node * + curnode = nodeList[i]; + if (node == curnode) { + index = i; + break; + } + } + } + return index; + } + + int + PathFinder::findNodeIndex(Node * node, std::vector < Node > &nodeList) { + int + index = -1; + if (node != NULL) { + for (unsigned int i = 0; i < nodeList.size(); ++i) { + Node & curnode = nodeList[i]; + if (node == &curnode) { + index = i; + break; + } + } + } + return index; + } + + //bool PathFinder::unitCannotMove(Unit *unit) { + // bool unitImmediatelyBlocked = false; + // + // try { + // // First check if unit currently blocked all around them, if so don't try to pathfind + // const Vec2i unitPos = unit->getPos(); + // int failureCount = 0; + // int cellCount = 0; + // + // for(int i = -1; i <= 1; ++i) { + // for(int j = -1; j <= 1; ++j) { + // Vec2i pos = unitPos + Vec2i(i, j); + // if(pos != unitPos) { + // bool canUnitMoveToCell = map->aproxCanMove(unit, unitPos, pos); + // if(canUnitMoveToCell == false) { + // failureCount++; + // } + // cellCount++; + // } + // } + // } + // unitImmediatelyBlocked = (failureCount == cellCount); + // + // } + // catch(const exception &ex) { + // //setRunningStatus(false); + // + // SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + // + // throw megaglest_runtime_error(ex.what()); + // } + // catch(...) { + // char szBuf[8096]=""; + // snprintf(szBuf,8096,"In [%s::%s %d] UNKNOWN error\n",__FILE__,__FUNCTION__,__LINE__); + // SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + // throw megaglest_runtime_error(szBuf); + // } + // + // return unitImmediatelyBlocked; + //} + + void + PathFinder::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + pathfinderNode = rootNode->addChild("PathFinder"); + + pathfinderNode->addAttribute("pathFindNodesMax", + intToStr(pathFindNodesMax), + mapTagReplacements); + pathfinderNode->addAttribute("pathFindNodesAbsoluteMax", + intToStr(pathFindNodesAbsoluteMax), + mapTagReplacements); + for (unsigned int i = 0; i < (unsigned int) factions.size(); ++i) { + FactionState & factionState = factions.getFactionState(i); + XmlNode * + factionsNode = pathfinderNode->addChild("factions"); + + for (unsigned int j = 0; + j < (unsigned int) factionState.nodePool.size(); ++j) { + Node * + curNode = &factionState.nodePool[j]; + XmlNode * + nodePoolNode = factionsNode->addChild("nodePool"); + + nodePoolNode->addAttribute("pos", curNode->pos.getString(), + mapTagReplacements); + int + nextIdx = + findNodeIndex(curNode->next, factionState.nodePool); + nodePoolNode->addAttribute("next", intToStr(nextIdx), + mapTagReplacements); + int + prevIdx = + findNodeIndex(curNode->prev, factionState.nodePool); + nodePoolNode->addAttribute("prev", intToStr(prevIdx), + mapTagReplacements); + nodePoolNode->addAttribute("heuristic", + floatToStr(curNode->heuristic, 6), + mapTagReplacements); + nodePoolNode->addAttribute("exploredCell", + intToStr(curNode->exploredCell), + mapTagReplacements); + } + + factionsNode->addAttribute("nodePoolCount", + intToStr(factionState.nodePoolCount), + mapTagReplacements); + factionsNode->addAttribute("random", + intToStr(factionState.random. + getLastNumber()), + mapTagReplacements); + factionsNode->addAttribute("useMaxNodeCount", + intToStr(factionState.useMaxNodeCount), + mapTagReplacements); + } + } + + void + PathFinder::loadGame(const XmlNode * rootNode) { + const XmlNode * + pathfinderNode = rootNode->getChild("PathFinder"); + + vector < XmlNode * >factionsNodeList = + pathfinderNode->getChildList("factions"); + for (unsigned int i = 0; i < (unsigned int) factionsNodeList.size(); + ++i) { + XmlNode * + factionsNode = factionsNodeList[i]; + + FactionState & factionState = factions.getFactionState(i); + vector < XmlNode * >nodePoolListNode = + factionsNode->getChildList("nodePool"); + for (unsigned int j = 0; + j < (unsigned int) nodePoolListNode.size() + && j < (unsigned int) pathFindNodesAbsoluteMax; ++j) { + XmlNode * + nodePoolNode = nodePoolListNode[j]; + + Node * + curNode = &factionState.nodePool[j]; + curNode->pos = + Vec2i::strToVec2(nodePoolNode->getAttribute("pos")-> + getValue()); + int + nextNode = + nodePoolNode->getAttribute("next")->getIntValue(); + if (nextNode >= 0) { + curNode->next = &factionState.nodePool[nextNode]; + } else { + curNode->next = NULL; + } + + int + prevNode = + nodePoolNode->getAttribute("prev")->getIntValue(); + if (prevNode >= 0) { + curNode->prev = &factionState.nodePool[prevNode]; + } else { + curNode->prev = NULL; + } + curNode->heuristic = + nodePoolNode->getAttribute("heuristic")->getFloatValue(); + curNode->exploredCell = + nodePoolNode->getAttribute("exploredCell")->getIntValue() != + 0; + } + + factionState.nodePoolCount = + factionsNode->getAttribute("nodePoolCount")->getIntValue(); + factionState.random.setLastNumber(factionsNode-> + getAttribute("random")-> + getIntValue()); + factionState.useMaxNodeCount = PathFinder::pathFindNodesMax; + } + } + + } } //end namespace diff --git a/source/glest_game/ai/path_finder.h b/source/glest_game/ai/path_finder.h index 7e8c1ebf1..4ef765a56 100644 --- a/source/glest_game/ai/path_finder.h +++ b/source/glest_game/ai/path_finder.h @@ -28,730 +28,637 @@ # include "leak_dumper.h" using - std::vector; +std::vector; using - Shared::Graphics::Vec2i; +Shared::Graphics::Vec2i; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class PathFinder -// -/// Finds paths for units using a modification of the A* algorithm -// ===================================================== - - class - PathFinder - { - public: - class - BadUnitNodeList - { - public: - BadUnitNodeList () - { - unitSize = -1; - field = fLand; - } - int - unitSize; - Field - field; - std::map < - Vec2i, - std::map < - Vec2i, - bool > > - badPosList; - - inline bool - isPosBad (const Vec2i & pos1, const Vec2i & pos2) - { - bool - result = false; - - std::map < - Vec2i, - std::map < - Vec2i, - bool > >::iterator - iterFind = badPosList.find (pos1); - if (iterFind != badPosList.end ()) - { - std::map < Vec2i, bool >::iterator iterFind2 = - iterFind->second.find (pos2); - if (iterFind2 != iterFind->second.end ()) - { - result = true; - } - } - - return - result; - } - }; - - class - Node - { - public: - Node () - { - clear (); - } - void - clear () - { - pos.x = 0; - pos.y = 0; - next = NULL; - prev = NULL; - heuristic = 0.0; - exploredCell = false; - } - Vec2i - pos; - Node * - next; - Node * - prev; - float - heuristic; - bool - exploredCell; - }; - typedef - vector < - Node * > - Nodes; - - class - FactionState - { - protected: - Mutex * - factionMutexPrecache; - public: - explicit - FactionState (int factionIndex): - //factionMutexPrecache(new Mutex) { - factionMutexPrecache (NULL) - { //, random(factionIndex) { - - openPosList.clear (); - openNodesList. - clear (); - closedNodesList. - clear (); - nodePool. - clear (); - nodePoolCount = 0; - this-> - factionIndex = factionIndex; - useMaxNodeCount = 0; - - precachedTravelState. - clear (); - precachedPath. - clear (); - } - ~ - FactionState () - { - - delete - factionMutexPrecache; - factionMutexPrecache = NULL; - } - Mutex * - getMutexPreCache () - { - return factionMutexPrecache; - } - - std::map < Vec2i, bool > openPosList; - std::map < float, - Nodes > - openNodesList; - std::map < float, - Nodes > - closedNodesList; - std::vector < Node > nodePool; - - int - nodePoolCount; - int - factionIndex; - RandomGen - random; - //CRandomMersenne random; - int - useMaxNodeCount; - - std::map < int, - TravelState > - precachedTravelState; - std::map < int, - std::vector < - Vec2i > > - precachedPath; - }; - - class - FactionStateManager - { - protected: - typedef - vector < - FactionState * > - FactionStateList; - FactionStateList - factions; - - void - init () - { - for (int index = 0; index < GameConstants::maxPlayers; ++index) - { - factions.push_back (new FactionState (index)); - } - } - - public: - FactionStateManager () - { - init (); - } - ~FactionStateManager () - { - clear (); - } - - FactionState & getFactionState (int index) - { - FactionState * - faction = factions[index]; - return *faction; - } - void - clear () - { - for (unsigned int index = 0; - index < (unsigned int) factions.size (); ++index) - { - delete - factions[index]; - } - - factions.clear (); - } - int - size () - { - return (int) factions.size (); - } - }; - - public: - static const int - maxFreeSearchRadius; - - static const int - pathFindBailoutRadius; - static const int - pathFindExtendRefreshForNodeCount; - static const int - pathFindExtendRefreshNodeCountMin; - static const int - pathFindExtendRefreshNodeCountMax; - - private: - - static int - pathFindNodesMax; - static int - pathFindNodesAbsoluteMax; - - - FactionStateManager - factions; - const Map * - map; - bool - minorDebugPathfinder; - - public: - PathFinder (); - explicit - PathFinder (const Map * map); - ~PathFinder (); - - PathFinder (const PathFinder & obj) - { - init (); - throw - megaglest_runtime_error ("class PathFinder is NOT safe to copy!"); - } - PathFinder & operator= (const PathFinder & obj) - { - init (); - throw - megaglest_runtime_error ("class PathFinder is NOT safe to assign!"); - } - - void - init (const Map * map); - TravelState - findPath (Unit * unit, const Vec2i & finalPos, bool * wasStuck = - NULL, int frameIndex = -1); - void - clearUnitPrecache (Unit * unit); - void - removeUnitPrecache (Unit * unit); - void - clearCaches (); - - //bool unitCannotMove(Unit *unit); - - int - findNodeIndex (Node * node, Nodes & nodeList); - int - findNodeIndex (Node * node, std::vector < Node > &nodeList); - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - - private: - void - init (); - - TravelState - aStar (Unit * unit, const Vec2i & finalPos, bool inBailout, - int frameIndex, int maxNodeCount = - -1, uint32 * searched_node_count = NULL); - inline static Node * - newNode (FactionState & faction, int maxNodeCount) - { - if (faction.nodePoolCount < (int) faction.nodePool.size () && - faction.nodePoolCount < maxNodeCount) - { - Node * - node = &(faction.nodePool[faction.nodePoolCount]); - node->clear (); - faction.nodePoolCount++; - return node; - } - return NULL; - } - - Vec2i - computeNearestFreePos (const Unit * unit, const Vec2i & targetPos); - - inline static float - heuristic (const Vec2i & pos, const Vec2i & finalPos) - { - return pos.dist (finalPos); - } - - inline static bool - openPos (const Vec2i & sucPos, FactionState & faction) - { - if (faction.openPosList.find (sucPos) == faction.openPosList.end ()) - { - return false; - } - return true; - } - - inline static Node * - minHeuristicFastLookup (FactionState & faction) - { - if (faction.openNodesList.empty () == true) - { - throw - megaglest_runtime_error ("openNodesList.empty() == true"); - } - - Node * - result = faction.openNodesList.begin ()->second.front (); - faction.openNodesList.begin ()->second.erase (faction.openNodesList. - begin ()->second. - begin ()); - if (faction.openNodesList.begin ()->second.empty ()) - { - faction.openNodesList.erase (faction.openNodesList.begin ()); - } - return result; - } - - inline bool - processNode (Unit * unit, Node * node, const Vec2i finalPos, - int x, int y, bool & nodeLimitReached, int maxNodeCount) - { - bool - result = false; - Vec2i - sucPos = node->pos + Vec2i (x, y); - - int - unitFactionIndex = unit->getFactionIndex (); - FactionState & faction = factions.getFactionState (unitFactionIndex); - - bool - foundOpenPosForPos = openPos (sucPos, faction); - bool - allowUnitMoveSoon = canUnitMoveSoon (unit, node->pos, sucPos); - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In processNode() nodeLimitReached %d unitFactionIndex %d foundOpenPosForPos %d allowUnitMoveSoon %d maxNodeCount %d node->pos = %s finalPos = %s sucPos = %s faction.openPosList.size() %lu closedNodesList.size() %lu", - nodeLimitReached, unitFactionIndex, foundOpenPosForPos, - allowUnitMoveSoon, maxNodeCount, - node->pos.getString ().c_str (), - finalPos.getString ().c_str (), - sucPos.getString ().c_str (), - faction.openPosList.size (), - faction.closedNodesList.size ()); - - if (Thread::isCurrentThreadMainThread () == false) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - if (foundOpenPosForPos == false && allowUnitMoveSoon) - { - //if node is not open and canMove then generate another node - Node * - sucNode = newNode (faction, maxNodeCount); - if (sucNode != NULL) - { - sucNode->pos = sucPos; - sucNode->heuristic = heuristic (sucNode->pos, finalPos); - sucNode->prev = node; - sucNode->next = NULL; - sucNode->exploredCell = - map->getSurfaceCell (Map::toSurfCoords (sucPos))-> - isExplored (unit->getTeam ()); - if (faction.openNodesList.find (sucNode->heuristic) == - faction.openNodesList.end ()) - { - faction.openNodesList[sucNode->heuristic].clear (); - } - faction.openNodesList[sucNode->heuristic].push_back (sucNode); - faction.openPosList[sucNode->pos] = true; - - result = true; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In processNode() sucPos = %s", - sucPos.getString ().c_str ()); - - if (Thread::isCurrentThreadMainThread () == false) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, - szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - } - else - { - nodeLimitReached = true; - } - } - - return result; - } - - void - processNearestFreePos (const Vec2i & finalPos, int i, int j, int size, - Field field, int teamIndex, Vec2i unitPos, - Vec2i & nearestPos, float &nearestDist); - int - getPathFindExtendRefreshNodeCount (FactionState & faction); - - inline bool - canUnitMoveSoon (Unit * unit, const Vec2i & pos1, const Vec2i & pos2) - { - bool - result = map->aproxCanMoveSoon (unit, pos1, pos2); - return result; - } - - inline void - doAStarPathSearch (bool & nodeLimitReached, int &whileLoopCount, - int &unitFactionIndex, bool & pathFound, - Node * &node, const Vec2i & finalPos, - const std::map < Vec2i, bool > &closedNodes, - const std::map < Vec2i, Vec2i > &cameFrom, - const std::map < std::pair < Vec2i, Vec2i >, - bool > &canAddNode, Unit * &unit, int &maxNodeCount, - int curFrameIndex) - { - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", - nodeLimitReached, whileLoopCount, unitFactionIndex, - pathFound, maxNodeCount); - - if (curFrameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - FactionState & faction = factions.getFactionState (unitFactionIndex); - - while (nodeLimitReached == false) - { - whileLoopCount++; - if (faction.openNodesList.empty () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", - nodeLimitReached, whileLoopCount, - unitFactionIndex, pathFound, maxNodeCount); - - if (curFrameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, - szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - pathFound = false; - break; - } - node = minHeuristicFastLookup (faction); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d node->pos = %s finalPos = %s node->exploredCell = %d", - nodeLimitReached, whileLoopCount, unitFactionIndex, - pathFound, maxNodeCount, - node->pos.getString ().c_str (), - finalPos.getString ().c_str (), node->exploredCell); - - if (curFrameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - if (node->pos == finalPos || node->exploredCell == false) - { - pathFound = true; - break; - } - - if (faction.closedNodesList.find (node->heuristic) == - faction.closedNodesList.end ()) - { - faction.closedNodesList[node->heuristic].clear (); - } - faction.closedNodesList[node->heuristic].push_back (node); - faction.openPosList[node->pos] = true; - - int - failureCount = 0; - int - cellCount = 0; - -// if(Thread::isCurrentThreadMainThread() == false) { -// throw megaglest_runtime_error("#1 Invalid access to FactionState random from outside main thread current id = " + -// intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); -// } - - //int tryDirection = 1; - //int tryDirection = faction.random.IRandomX(1, 4); - int - tryDirection = faction.random.randRange (1, 4); - //int tryDirection = unit->getRandom(true)->randRange(1, 4); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In doAStarPathSearch() tryDirection %d", - tryDirection); - - if (curFrameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - if (tryDirection == 4) - { - for (int i = 1; i >= -1 && nodeLimitReached == false; --i) - { - for (int j = -1; j <= 1 && nodeLimitReached == false; ++j) - { - if (processNode - (unit, node, finalPos, i, j, nodeLimitReached, - maxNodeCount) == false) - { - failureCount++; - } - cellCount++; - } - } - } - else if (tryDirection == 3) - { - for (int i = -1; i <= 1 && nodeLimitReached == false; ++i) - { - for (int j = 1; j >= -1 && nodeLimitReached == false; --j) - { - if (processNode - (unit, node, finalPos, i, j, nodeLimitReached, - maxNodeCount) == false) - { - failureCount++; - } - cellCount++; - } - } - } - else if (tryDirection == 2) - { - for (int i = -1; i <= 1 && nodeLimitReached == false; ++i) - { - for (int j = -1; j <= 1 && nodeLimitReached == false; ++j) - { - if (processNode - (unit, node, finalPos, i, j, nodeLimitReached, - maxNodeCount) == false) - { - failureCount++; - } - cellCount++; - } - } - } - else - { - for (int i = 1; i >= -1 && nodeLimitReached == false; --i) - { - for (int j = 1; j >= -1 && nodeLimitReached == false; --j) - { - if (processNode - (unit, node, finalPos, i, j, nodeLimitReached, - maxNodeCount) == false) - { - failureCount++; - } - cellCount++; - } - } - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true - && SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynchMax). - enabled == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", - nodeLimitReached, whileLoopCount, unitFactionIndex, - pathFound, maxNodeCount); - - if (curFrameIndex >= 0) - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - } - - } - - }; - - } + Glest { + namespace + Game { + + // ===================================================== + // class PathFinder + // + /// Finds paths for units using a modification of the A* algorithm + // ===================================================== + + class + PathFinder { + public: + class + BadUnitNodeList { + public: + BadUnitNodeList() { + unitSize = -1; + field = fLand; + } + int + unitSize; + Field + field; + std::map < + Vec2i, + std::map < + Vec2i, + bool > > + badPosList; + + inline bool + isPosBad(const Vec2i & pos1, const Vec2i & pos2) { + bool + result = false; + + std::map < + Vec2i, + std::map < + Vec2i, + bool > >::iterator + iterFind = badPosList.find(pos1); + if (iterFind != badPosList.end()) { + std::map < Vec2i, bool >::iterator iterFind2 = + iterFind->second.find(pos2); + if (iterFind2 != iterFind->second.end()) { + result = true; + } + } + + return + result; + } + }; + + class + Node { + public: + Node() { + clear(); + } + void + clear() { + pos.x = 0; + pos.y = 0; + next = NULL; + prev = NULL; + heuristic = 0.0; + exploredCell = false; + } + Vec2i + pos; + Node * + next; + Node * + prev; + float + heuristic; + bool + exploredCell; + }; + typedef + vector < + Node * > + Nodes; + + class + FactionState { + protected: + Mutex * + factionMutexPrecache; + public: + explicit + FactionState(int factionIndex) : + //factionMutexPrecache(new Mutex) { + factionMutexPrecache(NULL) { //, random(factionIndex) { + + openPosList.clear(); + openNodesList. + clear(); + closedNodesList. + clear(); + nodePool. + clear(); + nodePoolCount = 0; + this-> + factionIndex = factionIndex; + useMaxNodeCount = 0; + + precachedTravelState. + clear(); + precachedPath. + clear(); + } + ~ + FactionState() { + + delete + factionMutexPrecache; + factionMutexPrecache = NULL; + } + Mutex * + getMutexPreCache() { + return factionMutexPrecache; + } + + std::map < Vec2i, bool > openPosList; + std::map < float, + Nodes > + openNodesList; + std::map < float, + Nodes > + closedNodesList; + std::vector < Node > nodePool; + + int + nodePoolCount; + int + factionIndex; + RandomGen + random; + //CRandomMersenne random; + int + useMaxNodeCount; + + std::map < int, + TravelState > + precachedTravelState; + std::map < int, + std::vector < + Vec2i > > + precachedPath; + }; + + class + FactionStateManager { + protected: + typedef + vector < + FactionState * > + FactionStateList; + FactionStateList + factions; + + void + init() { + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + factions.push_back(new FactionState(index)); + } + } + + public: + FactionStateManager() { + init(); + } + ~FactionStateManager() { + clear(); + } + + FactionState & getFactionState(int index) { + FactionState * + faction = factions[index]; + return *faction; + } + void + clear() { + for (unsigned int index = 0; + index < (unsigned int) factions.size(); ++index) { + delete + factions[index]; + } + + factions.clear(); + } + int + size() { + return (int) factions.size(); + } + }; + + public: + static const int + maxFreeSearchRadius; + + static const int + pathFindBailoutRadius; + static const int + pathFindExtendRefreshForNodeCount; + static const int + pathFindExtendRefreshNodeCountMin; + static const int + pathFindExtendRefreshNodeCountMax; + + private: + + static int + pathFindNodesMax; + static int + pathFindNodesAbsoluteMax; + + + FactionStateManager + factions; + const Map * + map; + bool + minorDebugPathfinder; + + public: + PathFinder(); + explicit + PathFinder(const Map * map); + ~PathFinder(); + + PathFinder(const PathFinder & obj) { + init(); + throw + megaglest_runtime_error("class PathFinder is NOT safe to copy!"); + } + PathFinder & operator= (const PathFinder & obj) { + init(); + throw + megaglest_runtime_error("class PathFinder is NOT safe to assign!"); + } + + void + init(const Map * map); + TravelState + findPath(Unit * unit, const Vec2i & finalPos, bool * wasStuck = + NULL, int frameIndex = -1); + void + clearUnitPrecache(Unit * unit); + void + removeUnitPrecache(Unit * unit); + void + clearCaches(); + + //bool unitCannotMove(Unit *unit); + + int + findNodeIndex(Node * node, Nodes & nodeList); + int + findNodeIndex(Node * node, std::vector < Node > &nodeList); + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + + private: + void + init(); + + TravelState + aStar(Unit * unit, const Vec2i & finalPos, bool inBailout, + int frameIndex, int maxNodeCount = + -1, uint32 * searched_node_count = NULL); + inline static Node * + newNode(FactionState & faction, int maxNodeCount) { + if (faction.nodePoolCount < (int) faction.nodePool.size() && + faction.nodePoolCount < maxNodeCount) { + Node * + node = &(faction.nodePool[faction.nodePoolCount]); + node->clear(); + faction.nodePoolCount++; + return node; + } + return NULL; + } + + Vec2i + computeNearestFreePos(const Unit * unit, const Vec2i & targetPos); + + inline static float + heuristic(const Vec2i & pos, const Vec2i & finalPos) { + return pos.dist(finalPos); + } + + inline static bool + openPos(const Vec2i & sucPos, FactionState & faction) { + if (faction.openPosList.find(sucPos) == faction.openPosList.end()) { + return false; + } + return true; + } + + inline static Node * + minHeuristicFastLookup(FactionState & faction) { + if (faction.openNodesList.empty() == true) { + throw + megaglest_runtime_error("openNodesList.empty() == true"); + } + + Node * + result = faction.openNodesList.begin()->second.front(); + faction.openNodesList.begin()->second.erase(faction.openNodesList. + begin()->second. + begin()); + if (faction.openNodesList.begin()->second.empty()) { + faction.openNodesList.erase(faction.openNodesList.begin()); + } + return result; + } + + inline bool + processNode(Unit * unit, Node * node, const Vec2i finalPos, + int x, int y, bool & nodeLimitReached, int maxNodeCount) { + bool + result = false; + Vec2i + sucPos = node->pos + Vec2i(x, y); + + int + unitFactionIndex = unit->getFactionIndex(); + FactionState & faction = factions.getFactionState(unitFactionIndex); + + bool + foundOpenPosForPos = openPos(sucPos, faction); + bool + allowUnitMoveSoon = canUnitMoveSoon(unit, node->pos, sucPos); + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In processNode() nodeLimitReached %d unitFactionIndex %d foundOpenPosForPos %d allowUnitMoveSoon %d maxNodeCount %d node->pos = %s finalPos = %s sucPos = %s faction.openPosList.size() %lu closedNodesList.size() %lu", + nodeLimitReached, unitFactionIndex, foundOpenPosForPos, + allowUnitMoveSoon, maxNodeCount, + node->pos.getString().c_str(), + finalPos.getString().c_str(), + sucPos.getString().c_str(), + faction.openPosList.size(), + faction.closedNodesList.size()); + + if (Thread::isCurrentThreadMainThread() == false) { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + if (foundOpenPosForPos == false && allowUnitMoveSoon) { + //if node is not open and canMove then generate another node + Node * + sucNode = newNode(faction, maxNodeCount); + if (sucNode != NULL) { + sucNode->pos = sucPos; + sucNode->heuristic = heuristic(sucNode->pos, finalPos); + sucNode->prev = node; + sucNode->next = NULL; + sucNode->exploredCell = + map->getSurfaceCell(Map::toSurfCoords(sucPos))-> + isExplored(unit->getTeam()); + if (faction.openNodesList.find(sucNode->heuristic) == + faction.openNodesList.end()) { + faction.openNodesList[sucNode->heuristic].clear(); + } + faction.openNodesList[sucNode->heuristic].push_back(sucNode); + faction.openPosList[sucNode->pos] = true; + + result = true; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In processNode() sucPos = %s", + sucPos.getString().c_str()); + + if (Thread::isCurrentThreadMainThread() == false) { + unit->logSynchDataThreaded(__FILE__, __LINE__, + szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + } else { + nodeLimitReached = true; + } + } + + return result; + } + + void + processNearestFreePos(const Vec2i & finalPos, int i, int j, int size, + Field field, int teamIndex, Vec2i unitPos, + Vec2i & nearestPos, float &nearestDist); + int + getPathFindExtendRefreshNodeCount(FactionState & faction); + + inline bool + canUnitMoveSoon(Unit * unit, const Vec2i & pos1, const Vec2i & pos2) { + bool + result = map->aproxCanMoveSoon(unit, pos1, pos2); + return result; + } + + inline void + doAStarPathSearch(bool & nodeLimitReached, int &whileLoopCount, + int &unitFactionIndex, bool & pathFound, + Node * &node, const Vec2i & finalPos, + const std::map < Vec2i, bool > &closedNodes, + const std::map < Vec2i, Vec2i > &cameFrom, + const std::map < std::pair < Vec2i, Vec2i >, + bool > &canAddNode, Unit * &unit, int &maxNodeCount, + int curFrameIndex) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", + nodeLimitReached, whileLoopCount, unitFactionIndex, + pathFound, maxNodeCount); + + if (curFrameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + FactionState & faction = factions.getFactionState(unitFactionIndex); + + while (nodeLimitReached == false) { + whileLoopCount++; + if (faction.openNodesList.empty() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", + nodeLimitReached, whileLoopCount, + unitFactionIndex, pathFound, maxNodeCount); + + if (curFrameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, + szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + pathFound = false; + break; + } + node = minHeuristicFastLookup(faction); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d node->pos = %s finalPos = %s node->exploredCell = %d", + nodeLimitReached, whileLoopCount, unitFactionIndex, + pathFound, maxNodeCount, + node->pos.getString().c_str(), + finalPos.getString().c_str(), node->exploredCell); + + if (curFrameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + if (node->pos == finalPos || node->exploredCell == false) { + pathFound = true; + break; + } + + if (faction.closedNodesList.find(node->heuristic) == + faction.closedNodesList.end()) { + faction.closedNodesList[node->heuristic].clear(); + } + faction.closedNodesList[node->heuristic].push_back(node); + faction.openPosList[node->pos] = true; + + int + failureCount = 0; + int + cellCount = 0; + + // if(Thread::isCurrentThreadMainThread() == false) { + // throw megaglest_runtime_error("#1 Invalid access to FactionState random from outside main thread current id = " + + // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); + // } + + //int tryDirection = 1; + //int tryDirection = faction.random.IRandomX(1, 4); + int + tryDirection = faction.random.randRange(1, 4); + //int tryDirection = unit->getRandom(true)->randRange(1, 4); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In doAStarPathSearch() tryDirection %d", + tryDirection); + + if (curFrameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + if (tryDirection == 4) { + for (int i = 1; i >= -1 && nodeLimitReached == false; --i) { + for (int j = -1; j <= 1 && nodeLimitReached == false; ++j) { + if (processNode + (unit, node, finalPos, i, j, nodeLimitReached, + maxNodeCount) == false) { + failureCount++; + } + cellCount++; + } + } + } else if (tryDirection == 3) { + for (int i = -1; i <= 1 && nodeLimitReached == false; ++i) { + for (int j = 1; j >= -1 && nodeLimitReached == false; --j) { + if (processNode + (unit, node, finalPos, i, j, nodeLimitReached, + maxNodeCount) == false) { + failureCount++; + } + cellCount++; + } + } + } else if (tryDirection == 2) { + for (int i = -1; i <= 1 && nodeLimitReached == false; ++i) { + for (int j = -1; j <= 1 && nodeLimitReached == false; ++j) { + if (processNode + (unit, node, finalPos, i, j, nodeLimitReached, + maxNodeCount) == false) { + failureCount++; + } + cellCount++; + } + } + } else { + for (int i = 1; i >= -1 && nodeLimitReached == false; --i) { + for (int j = 1; j >= -1 && nodeLimitReached == false; --j) { + if (processNode + (unit, node, finalPos, i, j, nodeLimitReached, + maxNodeCount) == false) { + failureCount++; + } + cellCount++; + } + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true + && SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynchMax). + enabled == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In doAStarPathSearch() nodeLimitReached %d whileLoopCount %d unitFactionIndex %d pathFound %d maxNodeCount %d", + nodeLimitReached, whileLoopCount, unitFactionIndex, + pathFound, maxNodeCount); + + if (curFrameIndex >= 0) { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } + } + + } + + }; + + } } //end namespace #endif diff --git a/source/glest_game/facilities/auto_test.cpp b/source/glest_game/facilities/auto_test.cpp index ff2167f02..28fdb3a72 100644 --- a/source/glest_game/facilities/auto_test.cpp +++ b/source/glest_game/facilities/auto_test.cpp @@ -23,89 +23,90 @@ #include "leak_dumper.h" -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class AutoTest -// ===================================================== + // ===================================================== + // class AutoTest + // ===================================================== -const time_t AutoTest::invalidTime = -1; -time_t AutoTest::gameTime = 60 * 20; -bool AutoTest::wantExitGame = false; + const time_t AutoTest::invalidTime = -1; + time_t AutoTest::gameTime = 60 * 20; + bool AutoTest::wantExitGame = false; -GameSettings AutoTest::gameSettings; -string AutoTest::loadGameSettingsFile = ""; + GameSettings AutoTest::gameSettings; + string AutoTest::loadGameSettingsFile = ""; -// ===================== PUBLIC ======================== + // ===================== PUBLIC ======================== -AutoTest::AutoTest() { - exitGame = false; - gameStartTime = invalidTime; - random.init(time(NULL)); -} - -AutoTest & AutoTest::getInstance() { - static AutoTest autoTest; - return autoTest; -} - -void AutoTest::updateIntro(Program *program) { - program->setState(new MainMenu(program)); -} - -void AutoTest::updateRoot(Program *program, MainMenu *mainMenu) { - mainMenu->setState(new MenuStateNewGame(program, mainMenu)); -} - -void AutoTest::updateNewGame(Program *program, MainMenu *mainMenu) { - if(loadGameSettingsFile != "") { - gameStartTime = invalidTime; - bool fileFound = CoreData::getInstance().loadGameSettingsFromFile( - loadGameSettingsFile, &gameSettings); - - if(fileFound == false) { - throw megaglest_runtime_error("Specified game settings file [" + loadGameSettingsFile + "] was NOT found!"); + AutoTest::AutoTest() { + exitGame = false; + gameStartTime = invalidTime; + random.init(time(NULL)); } - //printf("Got settings:\n%s",gameSettings.toString().c_str()); - mainMenu->setState(new MenuStateCustomGame(program, mainMenu, false, pNewGame, true, &gameSettings)); - } - else { - mainMenu->setState(new MenuStateScenario(program, mainMenu, false, - Config::getInstance().getPathListForType(ptScenarios))); - } -} -void AutoTest::updateScenario(MenuStateScenario *menuStateScenario) { - gameStartTime = invalidTime; - - int scenarioIndex = random.randRange(0, menuStateScenario->getScenarioCount()-1); - menuStateScenario->setScenario(scenarioIndex); - - menuStateScenario->launchGame(); -} - -bool AutoTest::updateGame(Game *game) { - // record start time - if(gameStartTime == invalidTime) { - gameStartTime = time(NULL); - } - - // quit if we've espend enough time in the game - if(difftime(time(NULL),gameStartTime) > gameTime) { - Program *program = game->getProgram(); - Stats endStats = game->quitGame(); - if(AutoTest::wantExitGame == true) { - exitGame = true; + AutoTest & AutoTest::getInstance() { + static AutoTest autoTest; + return autoTest; } - Game::exitGameState(program, endStats); - return true; + + void AutoTest::updateIntro(Program *program) { + program->setState(new MainMenu(program)); + } + + void AutoTest::updateRoot(Program *program, MainMenu *mainMenu) { + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + } + + void AutoTest::updateNewGame(Program *program, MainMenu *mainMenu) { + if (loadGameSettingsFile != "") { + gameStartTime = invalidTime; + bool fileFound = CoreData::getInstance().loadGameSettingsFromFile( + loadGameSettingsFile, &gameSettings); + + if (fileFound == false) { + throw megaglest_runtime_error("Specified game settings file [" + loadGameSettingsFile + "] was NOT found!"); + } + //printf("Got settings:\n%s",gameSettings.toString().c_str()); + mainMenu->setState(new MenuStateCustomGame(program, mainMenu, false, pNewGame, true, &gameSettings)); + } else { + mainMenu->setState(new MenuStateScenario(program, mainMenu, false, + Config::getInstance().getPathListForType(ptScenarios))); + } + } + + void AutoTest::updateScenario(MenuStateScenario *menuStateScenario) { + gameStartTime = invalidTime; + + int scenarioIndex = random.randRange(0, menuStateScenario->getScenarioCount() - 1); + menuStateScenario->setScenario(scenarioIndex); + + menuStateScenario->launchGame(); + } + + bool AutoTest::updateGame(Game *game) { + // record start time + if (gameStartTime == invalidTime) { + gameStartTime = time(NULL); + } + + // quit if we've espend enough time in the game + if (difftime(time(NULL), gameStartTime) > gameTime) { + Program *program = game->getProgram(); + Stats endStats = game->quitGame(); + if (AutoTest::wantExitGame == true) { + exitGame = true; + } + Game::exitGameState(program, endStats); + return true; + } + + return false; + } + + void AutoTest::updateBattleEnd(Program *program) { + program->setState(new MainMenu(program)); + } + } - - return false; -} - -void AutoTest::updateBattleEnd(Program *program) { - program->setState(new MainMenu(program)); -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/facilities/auto_test.h b/source/glest_game/facilities/auto_test.h index 73b878fc4..da6d8c891 100644 --- a/source/glest_game/facilities/auto_test.h +++ b/source/glest_game/facilities/auto_test.h @@ -13,8 +13,8 @@ #define _SHARED_UTIL_AUTO_TEST_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -26,51 +26,63 @@ using namespace std; using Shared::Util::RandomGen; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class Program; -class MainMenu; -class MenuStateScenario; -class Game; + class Program; + class MainMenu; + class MenuStateScenario; + class Game; -// ===================================================== -// class AutoTest -// -/// Interface to write log files -// ===================================================== + // ===================================================== + // class AutoTest + // + /// Interface to write log files + // ===================================================== -class AutoTest{ -private: - int gameStartTime; - RandomGen random; - bool exitGame; - static bool wantExitGame; + class AutoTest { + private: + int gameStartTime; + RandomGen random; + bool exitGame; + static bool wantExitGame; - static GameSettings gameSettings; - static string loadGameSettingsFile; + static GameSettings gameSettings; + static string loadGameSettingsFile; - static const time_t invalidTime; - static time_t gameTime; + static const time_t invalidTime; + static time_t gameTime; -public: - static AutoTest & getInstance(); - AutoTest(); + public: + static AutoTest & getInstance(); + AutoTest(); - static void setMaxGameTime(time_t value) { gameTime = value; } - static void setWantExitGameWhenDone(bool value) { wantExitGame = value; } - static string getLoadGameSettingsFile() { return loadGameSettingsFile; } - static void setLoadGameSettingsFile(const string &filename) { loadGameSettingsFile = filename; } + static void setMaxGameTime(time_t value) { + gameTime = value; + } + static void setWantExitGameWhenDone(bool value) { + wantExitGame = value; + } + static string getLoadGameSettingsFile() { + return loadGameSettingsFile; + } + static void setLoadGameSettingsFile(const string &filename) { + loadGameSettingsFile = filename; + } - bool mustExitGame() const { return exitGame; } + bool mustExitGame() const { + return exitGame; + } - void updateIntro(Program *program); - void updateRoot(Program *program, MainMenu *mainMenu); - void updateNewGame(Program *program, MainMenu *mainMenu); - void updateScenario(MenuStateScenario *menuStateScenario); - bool updateGame(Game *game); - void updateBattleEnd(Program *program); -}; + void updateIntro(Program *program); + void updateRoot(Program *program, MainMenu *mainMenu); + void updateNewGame(Program *program, MainMenu *mainMenu); + void updateScenario(MenuStateScenario *menuStateScenario); + bool updateGame(Game *game); + void updateBattleEnd(Program *program); + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/facilities/components.cpp b/source/glest_game/facilities/components.cpp index fe581fa74..607624d57 100644 --- a/source/glest_game/facilities/components.cpp +++ b/source/glest_game/facilities/components.cpp @@ -26,1155 +26,1136 @@ using namespace std; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class GraphicComponent -// ===================================================== + // ===================================================== + // class GraphicComponent + // ===================================================== -float GraphicComponent::anim= 0.f; -float GraphicComponent::fade= 0.f; -const float GraphicComponent::animSpeed= 0.02f; -const float GraphicComponent::fadeSpeed= 0.01f; -// WHITE -Vec3f GraphicComponent::customTextColor = Vec3f(1.0,1.0,1.0); + float GraphicComponent::anim = 0.f; + float GraphicComponent::fade = 0.f; + const float GraphicComponent::animSpeed = 0.02f; + const float GraphicComponent::fadeSpeed = 0.01f; + // WHITE + Vec3f GraphicComponent::customTextColor = Vec3f(1.0, 1.0, 1.0); -std::map > GraphicComponent::registeredGraphicComponentList; + std::map > GraphicComponent::registeredGraphicComponentList; -GraphicComponent::GraphicComponent(const std::string &containerName, const std::string &objName, bool registerControl) { - this->containerName = containerName; - this->instanceName = ""; - if(containerName == "" || objName == "") { - //char szBuf[8096]=""; - //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",containerName.c_str(),objName.c_str()); - //throw megaglest_runtime_error(szBuf); - } - if(objName != "" && registerControl) { - registerGraphicComponent(containerName,objName); - } - else { - this->instanceName = objName; - } - this->fontCallbackName = objName + "_" + getNewUUD(); - CoreData::getInstance().registerFontChangedCallback(this->getFontCallbackName(), this); + GraphicComponent::GraphicComponent(const std::string &containerName, const std::string &objName, bool registerControl) { + this->containerName = containerName; + this->instanceName = ""; + if (containerName == "" || objName == "") { + //char szBuf[8096]=""; + //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",containerName.c_str(),objName.c_str()); + //throw megaglest_runtime_error(szBuf); + } + if (objName != "" && registerControl) { + registerGraphicComponent(containerName, objName); + } else { + this->instanceName = objName; + } + this->fontCallbackName = objName + "_" + getNewUUD(); + CoreData::getInstance().registerFontChangedCallback(this->getFontCallbackName(), this); - enabled = true; - editable = true; - visible = true; - x = 0; - y = 0; - w = 0; - h = 0; - text = ""; - font = NULL; - font3D = NULL; - textNativeTranslation = ""; -} - -string GraphicComponent::getNewUUD() { - char uuid_str[38]; - get_uuid_string(uuid_str,sizeof(uuid_str)); - return string(uuid_str); -} - -GraphicComponent::~GraphicComponent() { - CoreData::getInstance().unRegisterFontChangedCallback(this->getFontCallbackName()); -} - -void GraphicComponent::clearRegisteredComponents(std::string containerName) { - if(containerName == "") { - GraphicComponent::registeredGraphicComponentList.clear(); - } - else { - GraphicComponent::registeredGraphicComponentList[containerName].clear(); - } -} - -void GraphicComponent::clearRegisterGraphicComponent(std::string containerName, std::string objName) { - GraphicComponent *obj = findRegisteredComponent(containerName, objName); - if(obj) { - GraphicComponent::registeredGraphicComponentList[containerName].erase(objName); - } -} - -void GraphicComponent::clearRegisterGraphicComponent(std::string containerName, std::vector objNameList) { - for(int idx = 0; idx < (int)objNameList.size(); ++idx) { - GraphicComponent::clearRegisterGraphicComponent(containerName, objNameList[idx]); - } -} - -void GraphicComponent::registerGraphicComponent(std::string containerName, std::string objName) { - // unregistered old name if we have been renamed - if(this->getInstanceName() != "") { - //printf("RENAME Register Callback detected calling: Control old [%s] new [%s]\n",this->getInstanceName().c_str(),objName.c_str()); - clearRegisterGraphicComponent(this->containerName, this->getInstanceName()); - } - else { - //printf("NEW Register Callback detected calling: Control container [%s] name [%s]\n",containerName.c_str(),objName.c_str()); - } - - if(containerName == "" || objName == "") { - //char szBuf[8096]=""; - //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",this->containerName.c_str(),objName.c_str()); - //throw megaglest_runtime_error(szBuf); - } - - this->containerName = containerName; - this->instanceName = objName; - registeredGraphicComponentList[containerName][objName] = this; - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] registered [%s] [%s] count = %d\n",__FILE__,__FUNCTION__,__LINE__,containerName.c_str(),instanceName.c_str(),registeredGraphicComponentList[containerName].size()); -} - -void GraphicComponent::registerGraphicComponentOnlyFontCallbacks(std::string containerName, std::string objName) { - if(this->getInstanceName() != "") { - //printf("(FONT ONLY) RENAME Register Callback detected calling: Control old [%s] new [%s]\n",this->getInstanceName().c_str(),objName.c_str()); - clearRegisterGraphicComponent(this->containerName, this->getInstanceName()); - } - else { - //printf("(FONT ONLY) NEW Register Callback detected calling: Control container [%s] name [%s]\n",containerName.c_str(),objName.c_str()); - } - - if(containerName == "" || objName == "") { - //char szBuf[8096]=""; - //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",this->containerName.c_str(),objName.c_str()); - //throw megaglest_runtime_error(szBuf); - } - - this->containerName = containerName; - this->instanceName = objName; -} - -GraphicComponent * GraphicComponent::findRegisteredComponent(std::string containerName, std::string objName) { - GraphicComponent *result = NULL; - - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - std::map::iterator iterFind2 = iterFind1->second.find(objName); - if(iterFind2 != iterFind1->second.end()) { - result = iterFind2->second; + enabled = true; + editable = true; + visible = true; + x = 0; + y = 0; + w = 0; + h = 0; + text = ""; + font = NULL; + font3D = NULL; + textNativeTranslation = ""; } - } - return result; -} -void GraphicComponent::applyAllCustomProperties(std::string containerName) { - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - for(std::map::iterator iterFind2 = iterFind1->second.begin(); - iterFind2 != iterFind1->second.end(); ++iterFind2) { - iterFind2->second->applyCustomProperties(containerName); + string GraphicComponent::getNewUUD() { + char uuid_str[38]; + get_uuid_string(uuid_str, sizeof(uuid_str)); + return string(uuid_str); } - } -} -void GraphicComponent::applyCustomProperties(std::string containerName) { - if(instanceName != "") { - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - std::map::iterator iterFind2 = iterFind1->second.find(instanceName); - if(iterFind2 != iterFind1->second.end()) { - Config &config = Config::getInstance(); + GraphicComponent::~GraphicComponent() { + CoreData::getInstance().unRegisterFontChangedCallback(this->getFontCallbackName()); + } - //string languageToken = config.getString("Lang"); - string languageToken = Lang::getInstance().getLanguage(); - - //if(dynamic_cast(iterFind2->second) != NULL) { - GraphicComponent *ctl = dynamic_cast(iterFind2->second); - - // First check default overrides - ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x",intToStr(ctl->x).c_str()); - ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y",intToStr(ctl->y).c_str()); - ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w",intToStr(ctl->w).c_str()); - ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h",intToStr(ctl->h).c_str()); - ctl->visible = config.getBool(containerName + "_" + iterFind2->first + "_visible",boolToStr(ctl->visible).c_str()); - - // Now check language specific overrides - ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x_" + languageToken, intToStr(ctl->x).c_str()); - ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y_" + languageToken, intToStr(ctl->y).c_str()); - ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w_" + languageToken, intToStr(ctl->w).c_str()); - ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h_" + languageToken, intToStr(ctl->h).c_str()); - ctl->visible = config.getBool(containerName + "_" + iterFind2->first + "_visible_" + languageToken,boolToStr(ctl->visible).c_str()); + void GraphicComponent::clearRegisteredComponents(std::string containerName) { + if (containerName == "") { + GraphicComponent::registeredGraphicComponentList.clear(); + } else { + GraphicComponent::registeredGraphicComponentList[containerName].clear(); } } - } -} -bool GraphicComponent::saveAllCustomProperties(std::string containerName) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] registered [%s] count = %d\n",__FILE__,__FUNCTION__,__LINE__,containerName.c_str(),registeredGraphicComponentList[containerName].size()); - - bool foundPropertiesToSave = false; - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - for(std::map::iterator iterFind2 = iterFind1->second.begin(); - iterFind2 != iterFind1->second.end(); ++iterFind2) { - bool saved = iterFind2->second->saveCustomProperties(containerName); - foundPropertiesToSave = (saved || foundPropertiesToSave); - } - } - - if(foundPropertiesToSave == true) { - Config &config = Config::getInstance(); - config.save(); - } - - return foundPropertiesToSave; -} - -bool GraphicComponent::saveCustomProperties(std::string containerName) { - bool savedChange = false; - if(instanceName != "") { - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] looking for [%s] [%s]\n",__FILE__,__FUNCTION__,__LINE__,containerName.c_str(),instanceName.c_str()); - - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] looking for [%s]\n",__FILE__,__FUNCTION__,__LINE__,instanceName.c_str()); - - std::map::iterator iterFind2 = iterFind1->second.find(instanceName); - if(iterFind2 != iterFind1->second.end()) { - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] FOUND [%s]\n",__FILE__,__FUNCTION__,__LINE__,instanceName.c_str()); - - Config &config = Config::getInstance(); - - //string languageToken = config.getString("Lang"); - - //if(dynamic_cast(iterFind2->second) != NULL) { - GraphicComponent *ctl = dynamic_cast(iterFind2->second); - - // First check default overrides - config.setInt(containerName + "_" + iterFind2->first + "_x",ctl->x); - config.setInt(containerName + "_" + iterFind2->first + "_y",ctl->y); - config.setInt(containerName + "_" + iterFind2->first + "_w",ctl->w); - config.setInt(containerName + "_" + iterFind2->first + "_h",ctl->h); - - savedChange = true; - // Now check language specific overrides - //ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x_" + languageToken, intToStr(ctl->x).c_str()); - //ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y_" + languageToken, intToStr(ctl->y).c_str()); - //ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w_" + languageToken, intToStr(ctl->w).c_str()); - //ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h_" + languageToken, intToStr(ctl->h).c_str()); - - //} + void GraphicComponent::clearRegisterGraphicComponent(std::string containerName, std::string objName) { + GraphicComponent *obj = findRegisteredComponent(containerName, objName); + if (obj) { + GraphicComponent::registeredGraphicComponentList[containerName].erase(objName); } } - } - return savedChange; -} - -void GraphicComponent::setFont(Font2D *font) { - this->font = font; - if (this->font != NULL) { - this->font2DUniqueId = font->getFontUniqueId(); - } - else { - this->font2DUniqueId = ""; - } -} - -void GraphicComponent::setFont3D(Font3D *font) { - this->font3D = font; - if (this->font3D != NULL) { - this->font3DUniqueId = font->getFontUniqueId(); - } - else { - this->font3DUniqueId = ""; - } -} - -void GraphicComponent::FontChangedCallback(std::string fontUniqueId, Font *font) { - //printf("In FontChanged for [%s] font [%p] Control 2D [%s] 3D [%s]\n", fontUniqueId.c_str(),font,this->font2DUniqueId.c_str(),this->font3DUniqueId.c_str()); - if (fontUniqueId != "") { - if (fontUniqueId == this->font2DUniqueId) { - if (font != NULL) { - this->font = (Font2D *)font; - } - else { - this->font = NULL; + void GraphicComponent::clearRegisterGraphicComponent(std::string containerName, std::vector objNameList) { + for (int idx = 0; idx < (int) objNameList.size(); ++idx) { + GraphicComponent::clearRegisterGraphicComponent(containerName, objNameList[idx]); } } - else if (fontUniqueId == this->font3DUniqueId) { - if (font != NULL) { - this->font3D = (Font3D *)font; - } - else { - this->font3D = NULL; - } - } - } -} -void GraphicComponent::reloadFonts() { - setFont(CoreData::getInstance().getMenuFontNormal()); - setFont3D(CoreData::getInstance().getMenuFontNormal3D()); -} - -void GraphicComponent::reloadFontsForRegisterGraphicComponents(std::string containerName) { - std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); - if(iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { - for(std::map::iterator iterFind2 = iterFind1->second.begin(); - iterFind2 != iterFind1->second.end(); ++iterFind2) { - GraphicComponent *ctl = dynamic_cast(iterFind2->second); - if(ctl) { - ctl->reloadFonts(); - } - } - } -} - -void GraphicComponent::init(int x, int y, int w, int h) { - this->x= x; - this->y= y; - this->w= w; - this->h= h; - reloadFonts(); - enabled= true; -} - -bool GraphicComponent::mouseMove(int x, int y) { - if(this->getVisible() == false) { - return false; - } - - return - x > this->x && - y > this->y && - x < this->x + w && - y < this->y + h; -} - -bool GraphicComponent::mouseClick(int x, int y){ - if(getVisible() && getEnabled() && getEditable()) - return mouseMove(x, y); - else - return false; -} - -void GraphicComponent::update(){ - fade+= fadeSpeed; - anim+= animSpeed; - if(fade>1.f) fade= 1.f; - if(anim>1.f) anim= 0.f; -} - -void GraphicComponent::resetFade(){ - fade= 0.f; -} - -// ===================================================== -// class GraphicLabel -// ===================================================== - -const int GraphicLabel::defH= 20; -const int GraphicLabel::defW= 70; - -GraphicLabel::GraphicLabel(const std::string &containerName, const std::string &objName, bool registerControl) : - GraphicComponent(containerName, objName, registerControl) { - centered = false; - wordWrap = false; - centeredW = -1; - centeredH = 1; - editable = false; - editModeEnabled = false; - maxEditWidth = -1; - maxEditRenderWidth = -1; - renderBackground = false; - backgroundColor=Vec4f(0.2f,0.2f,0.2f,0.6f); - isPassword = false; - texture = NULL; -} - -void GraphicLabel::init(int x, int y, int w, int h, bool centered, Vec3f textColor, bool wordWrap) { - GraphicComponent::init(x, y, w, h); - this->centered= centered; - this->textColor=textColor; - this->wordWrap = wordWrap; -} - -bool GraphicLabel::mouseMove(int x, int y) { - if(this->getVisible() == false) { - return false; - } - - int useWidth = w; - if(text.length() > 0 && font3D != NULL) { - float lineWidth = (font3D->getTextHandler()->Advance(text.c_str()) * Shared::Graphics::Font::scaleFontValue); - useWidth = (int)lineWidth; - } - - if(editable && useWidth this->x && - y > this->y && - x < this->x + useWidth && - y < this->y + h; -} - -bool GraphicLabel::getCenteredW() const { - bool result = (centered || centeredW == 1); - return result; -} -//void GraphicLabel::setCenteredW(bool centered) { -// centeredW = (centered ? 1 : 0); -//} - -bool GraphicLabel::getCenteredH() const { - bool result = (centered || centeredH == 1); - return result; -} -//void GraphicLabel::setCenteredH(bool centered) { -// centeredH = (centered ? 1 : 0); -//} - -// ===================================================== -// class GraphicButton -// ===================================================== - -const int GraphicButton::defH= 22; -const int GraphicButton::defW= 90; - -GraphicButton::GraphicButton(const std::string &containerName, const std::string &objName, bool registerControl) : - GraphicComponent(containerName,objName,registerControl) { - - lighted = false; - alwaysLighted = false; - useCustomTexture = false; - customTexture = NULL; -} - -void GraphicButton::init(int x, int y, int w, int h){ - GraphicComponent::init(x, y, w, h); - lighted= false; -} - -bool GraphicButton::mouseMove(int x, int y){ - if(this->getVisible() == false) { - return false; - } - - bool b= GraphicComponent::mouseMove(x, y); - lighted= b; - return b; -} - -// ===================================================== -// class GraphicListBox -// ===================================================== - -const int GraphicListBox::defH= 22; -const int GraphicListBox::defW= 140; - -GraphicListBox::GraphicListBox(const std::string &containerName, const std::string &objName) -: GraphicComponent(containerName, objName), graphButton1(containerName, objName + "_button1"), - graphButton2(containerName, objName + "_button2") { - selectedItemIndex = 0; - lighted = false; - leftControlled = false; -} - -void GraphicListBox::init(int x, int y, int w, int h, Vec3f textColor){ - GraphicComponent::init(x, y, w, h); - - this->textColor=textColor; - graphButton1.init(x, y, 22, h); - graphButton2.init(x+w-22, y, 22, h); - graphButton1.setText("<"); - graphButton2.setText(">"); - selectedItemIndex=-1; - lighted=false; -} - -const string & GraphicListBox::getTextNativeTranslation() { - if(this->translated_items.empty() == true || - this->selectedItemIndex < 0 || - this->selectedItemIndex >= (int)this->translated_items.size() || - this->items.size() != this->translated_items.size()) { - return this->text; - } - else { - return this->translated_items[this->selectedItemIndex]; - } -} - -//queryes -void GraphicListBox::pushBackItem(string item, string translated_item){ - items.push_back(item); - translated_items.push_back(translated_item); - setSelectedItemIndex(0); -} - -void GraphicListBox::setItems(const vector &items, const vector translated_items){ - this->items= items; - this->translated_items = translated_items; - if(items.empty() == false) { - setSelectedItemIndex(0); - } - else { - selectedItemIndex=-1; - setText(""); - } -} - -void GraphicListBox::setSelectedItemIndex(int index, bool errorOnMissing){ - if(errorOnMissing == true && (index < 0 || index >= (int)items.size())) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Index not found in listbox name: [%s] value index: %d size: %lu",this->instanceName.c_str(),index,(unsigned long)items.size()); - throw megaglest_runtime_error(szBuf); - } - selectedItemIndex= index; - setText(getSelectedItem()); -} - -void GraphicListBox::setLeftControlled(bool leftControlled) { - if(this->leftControlled!=leftControlled){ - this->leftControlled= leftControlled; - if(leftControlled==true) { - graphButton2.setX(x+graphButton1.getW()-4); - graphButton2.setH(graphButton2.getH()-4); - graphButton2.setW(graphButton2.getW()-4); - graphButton1.setH(graphButton1.getH()-4); - graphButton1.setW(graphButton1.getW()-4); - graphButton2.setY(graphButton2.getY()+2); - graphButton1.setY(graphButton1.getY()+2); - } - else { - graphButton2.setX(x+w-graphButton2.getW()+4); - graphButton2.setH(graphButton2.getH()+4); - graphButton2.setW(graphButton2.getW()+4); - graphButton1.setH(graphButton1.getH()+4); - graphButton1.setW(graphButton1.getW()+4); - graphButton2.setY(graphButton2.getY()-2); - graphButton1.setY(graphButton1.getY()-2); - } - } -} - -void GraphicListBox::setX(int x) { - this->x= x; - graphButton1.setX(x); - if(leftControlled==true) { - graphButton2.setX(x+graphButton1.getW()); - } - else { - graphButton2.setX(x+w-graphButton2.getW()); - } -} - -void GraphicListBox::setY(int y) { - this->y= y; - graphButton1.setY(y); - graphButton2.setY(y); -} - -void GraphicListBox::setEditable(bool editable){ - graphButton1.setEditable(editable); - graphButton2.setEditable(editable); - GraphicComponent::setEditable(editable); -} - -bool GraphicListBox::hasItem(string item) const { - bool result = false; - vector::const_iterator iter= find(items.begin(), items.end(), item); - if(iter != items.end()) { - result = true; - } - - return result; -} - -void GraphicListBox::setSelectedItem(string item, bool errorOnMissing){ - vector::iterator iter; - - iter= find(items.begin(), items.end(), item); - - if(iter==items.end()) { - if(errorOnMissing == true) { - for(int idx = 0; idx < (int)items.size(); idx++) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\ninstanceName [%s] idx = %d items[idx] = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,instanceName.c_str(),idx,items[idx].c_str()); + void GraphicComponent::registerGraphicComponent(std::string containerName, std::string objName) { + // unregistered old name if we have been renamed + if (this->getInstanceName() != "") { + //printf("RENAME Register Callback detected calling: Control old [%s] new [%s]\n",this->getInstanceName().c_str(),objName.c_str()); + clearRegisterGraphicComponent(this->containerName, this->getInstanceName()); + } else { + //printf("NEW Register Callback detected calling: Control container [%s] name [%s]\n",containerName.c_str(),objName.c_str()); } - char szBuf[8096]=""; - snprintf(szBuf,8096,"Value not found in listbox name: [%s] value: %s",this->instanceName.c_str(),item.c_str()); - throw megaglest_runtime_error(szBuf); + if (containerName == "" || objName == "") { + //char szBuf[8096]=""; + //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",this->containerName.c_str(),objName.c_str()); + //throw megaglest_runtime_error(szBuf); + } + + this->containerName = containerName; + this->instanceName = objName; + registeredGraphicComponentList[containerName][objName] = this; + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] registered [%s] [%s] count = %d\n",__FILE__,__FUNCTION__,__LINE__,containerName.c_str(),instanceName.c_str(),registeredGraphicComponentList[containerName].size()); } - } - else { - setSelectedItemIndex(iter-items.begin()); - } -} + void GraphicComponent::registerGraphicComponentOnlyFontCallbacks(std::string containerName, std::string objName) { + if (this->getInstanceName() != "") { + //printf("(FONT ONLY) RENAME Register Callback detected calling: Control old [%s] new [%s]\n",this->getInstanceName().c_str(),objName.c_str()); + clearRegisterGraphicComponent(this->containerName, this->getInstanceName()); + } else { + //printf("(FONT ONLY) NEW Register Callback detected calling: Control container [%s] name [%s]\n",containerName.c_str(),objName.c_str()); + } -bool GraphicListBox::mouseMove(int x, int y){ - if(this->getVisible() == false) { - return false; - } + if (containerName == "" || objName == "") { + //char szBuf[8096]=""; + //snprintf(szBuf,8096,"Control not properly registered Container [%s] Control [%s]\n",this->containerName.c_str(),objName.c_str()); + //throw megaglest_runtime_error(szBuf); + } - return - graphButton1.mouseMove(x, y) || - graphButton2.mouseMove(x, y); -} + this->containerName = containerName; + this->instanceName = objName; + } -bool GraphicListBox::mouseClick(int x, int y,string advanceToItemStartingWith) { - if(this->getVisible() == false) { - return false; - } + GraphicComponent * GraphicComponent::findRegisteredComponent(std::string containerName, std::string objName) { + GraphicComponent *result = NULL; - if(!items.empty()) { - bool b1= graphButton1.mouseClick(x, y); - bool b2= graphButton2.mouseClick(x, y); + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + std::map::iterator iterFind2 = iterFind1->second.find(objName); + if (iterFind2 != iterFind1->second.end()) { + result = iterFind2->second; + } + } + return result; + } - if(b1) { - bool bFound = false; - if(advanceToItemStartingWith != "") { - for(int i = selectedItemIndex - 1; i >= 0; --i) { - string item = items[i]; - if((int)translated_items.size() > i) item = translated_items[i]; - if(StartsWith(toLower(item),toLower(advanceToItemStartingWith)) == true) { - bFound = true; - selectedItemIndex = i; - break; + void GraphicComponent::applyAllCustomProperties(std::string containerName) { + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + for (std::map::iterator iterFind2 = iterFind1->second.begin(); + iterFind2 != iterFind1->second.end(); ++iterFind2) { + iterFind2->second->applyCustomProperties(containerName); + } + } + } + + void GraphicComponent::applyCustomProperties(std::string containerName) { + if (instanceName != "") { + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + std::map::iterator iterFind2 = iterFind1->second.find(instanceName); + if (iterFind2 != iterFind1->second.end()) { + Config &config = Config::getInstance(); + + //string languageToken = config.getString("Lang"); + string languageToken = Lang::getInstance().getLanguage(); + + //if(dynamic_cast(iterFind2->second) != NULL) { + GraphicComponent *ctl = dynamic_cast(iterFind2->second); + + // First check default overrides + ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x", intToStr(ctl->x).c_str()); + ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y", intToStr(ctl->y).c_str()); + ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w", intToStr(ctl->w).c_str()); + ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h", intToStr(ctl->h).c_str()); + ctl->visible = config.getBool(containerName + "_" + iterFind2->first + "_visible", boolToStr(ctl->visible).c_str()); + + // Now check language specific overrides + ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x_" + languageToken, intToStr(ctl->x).c_str()); + ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y_" + languageToken, intToStr(ctl->y).c_str()); + ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w_" + languageToken, intToStr(ctl->w).c_str()); + ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h_" + languageToken, intToStr(ctl->h).c_str()); + ctl->visible = config.getBool(containerName + "_" + iterFind2->first + "_visible_" + languageToken, boolToStr(ctl->visible).c_str()); } } - if(bFound == false) { - for(int i = (int)items.size() - 1; i >= selectedItemIndex; --i) { - string item = items[i]; - if((int)translated_items.size() > i) item = translated_items[i]; - //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); - if(StartsWith(toLower(item),toLower(advanceToItemStartingWith)) == true) { - bFound = true; - selectedItemIndex = i; - break; + } + } + + bool GraphicComponent::saveAllCustomProperties(std::string containerName) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] registered [%s] count = %d\n", __FILE__, __FUNCTION__, __LINE__, containerName.c_str(), registeredGraphicComponentList[containerName].size()); + + bool foundPropertiesToSave = false; + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + for (std::map::iterator iterFind2 = iterFind1->second.begin(); + iterFind2 != iterFind1->second.end(); ++iterFind2) { + bool saved = iterFind2->second->saveCustomProperties(containerName); + foundPropertiesToSave = (saved || foundPropertiesToSave); + } + } + + if (foundPropertiesToSave == true) { + Config &config = Config::getInstance(); + config.save(); + } + + return foundPropertiesToSave; + } + + bool GraphicComponent::saveCustomProperties(std::string containerName) { + bool savedChange = false; + if (instanceName != "") { + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] looking for [%s] [%s]\n", __FILE__, __FUNCTION__, __LINE__, containerName.c_str(), instanceName.c_str()); + + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] looking for [%s]\n", __FILE__, __FUNCTION__, __LINE__, instanceName.c_str()); + + std::map::iterator iterFind2 = iterFind1->second.find(instanceName); + if (iterFind2 != iterFind1->second.end()) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] FOUND [%s]\n", __FILE__, __FUNCTION__, __LINE__, instanceName.c_str()); + + Config &config = Config::getInstance(); + + //string languageToken = config.getString("Lang"); + + //if(dynamic_cast(iterFind2->second) != NULL) { + GraphicComponent *ctl = dynamic_cast(iterFind2->second); + + // First check default overrides + config.setInt(containerName + "_" + iterFind2->first + "_x", ctl->x); + config.setInt(containerName + "_" + iterFind2->first + "_y", ctl->y); + config.setInt(containerName + "_" + iterFind2->first + "_w", ctl->w); + config.setInt(containerName + "_" + iterFind2->first + "_h", ctl->h); + + savedChange = true; + // Now check language specific overrides + //ctl->x = config.getInt(containerName + "_" + iterFind2->first + "_x_" + languageToken, intToStr(ctl->x).c_str()); + //ctl->y = config.getInt(containerName + "_" + iterFind2->first + "_y_" + languageToken, intToStr(ctl->y).c_str()); + //ctl->w = config.getInt(containerName + "_" + iterFind2->first + "_w_" + languageToken, intToStr(ctl->w).c_str()); + //ctl->h = config.getInt(containerName + "_" + iterFind2->first + "_h_" + languageToken, intToStr(ctl->h).c_str()); + + //} + } + } + } + + return savedChange; + } + + void GraphicComponent::setFont(Font2D *font) { + this->font = font; + if (this->font != NULL) { + this->font2DUniqueId = font->getFontUniqueId(); + } else { + this->font2DUniqueId = ""; + } + } + + void GraphicComponent::setFont3D(Font3D *font) { + this->font3D = font; + if (this->font3D != NULL) { + this->font3DUniqueId = font->getFontUniqueId(); + } else { + this->font3DUniqueId = ""; + } + } + + void GraphicComponent::FontChangedCallback(std::string fontUniqueId, Font *font) { + //printf("In FontChanged for [%s] font [%p] Control 2D [%s] 3D [%s]\n", fontUniqueId.c_str(),font,this->font2DUniqueId.c_str(),this->font3DUniqueId.c_str()); + if (fontUniqueId != "") { + if (fontUniqueId == this->font2DUniqueId) { + if (font != NULL) { + this->font = (Font2D *) font; + } else { + this->font = NULL; + } + } else if (fontUniqueId == this->font3DUniqueId) { + if (font != NULL) { + this->font3D = (Font3D *) font; + } else { + this->font3D = NULL; + } + } + } + } + + void GraphicComponent::reloadFonts() { + setFont(CoreData::getInstance().getMenuFontNormal()); + setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + } + + void GraphicComponent::reloadFontsForRegisterGraphicComponents(std::string containerName) { + std::map >::iterator iterFind1 = GraphicComponent::registeredGraphicComponentList.find(containerName); + if (iterFind1 != GraphicComponent::registeredGraphicComponentList.end()) { + for (std::map::iterator iterFind2 = iterFind1->second.begin(); + iterFind2 != iterFind1->second.end(); ++iterFind2) { + GraphicComponent *ctl = dynamic_cast(iterFind2->second); + if (ctl) { + ctl->reloadFonts(); + } + } + } + } + + void GraphicComponent::init(int x, int y, int w, int h) { + this->x = x; + this->y = y; + this->w = w; + this->h = h; + reloadFonts(); + enabled = true; + } + + bool GraphicComponent::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + return + x > this->x && + y > this->y && + x < this->x + w && + y < this->y + h; + } + + bool GraphicComponent::mouseClick(int x, int y) { + if (getVisible() && getEnabled() && getEditable()) + return mouseMove(x, y); + else + return false; + } + + void GraphicComponent::update() { + fade += fadeSpeed; + anim += animSpeed; + if (fade > 1.f) fade = 1.f; + if (anim > 1.f) anim = 0.f; + } + + void GraphicComponent::resetFade() { + fade = 0.f; + } + + // ===================================================== + // class GraphicLabel + // ===================================================== + + const int GraphicLabel::defH = 20; + const int GraphicLabel::defW = 70; + + GraphicLabel::GraphicLabel(const std::string &containerName, const std::string &objName, bool registerControl) : + GraphicComponent(containerName, objName, registerControl) { + centered = false; + wordWrap = false; + centeredW = -1; + centeredH = 1; + editable = false; + editModeEnabled = false; + maxEditWidth = -1; + maxEditRenderWidth = -1; + renderBackground = false; + backgroundColor = Vec4f(0.2f, 0.2f, 0.2f, 0.6f); + isPassword = false; + texture = NULL; + } + + void GraphicLabel::init(int x, int y, int w, int h, bool centered, Vec3f textColor, bool wordWrap) { + GraphicComponent::init(x, y, w, h); + this->centered = centered; + this->textColor = textColor; + this->wordWrap = wordWrap; + } + + bool GraphicLabel::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + int useWidth = w; + if (text.length() > 0 && font3D != NULL) { + float lineWidth = (font3D->getTextHandler()->Advance(text.c_str()) * Shared::Graphics::Font::scaleFontValue); + useWidth = (int) lineWidth; + } + + if (editable && useWidth < getMaxEditRenderWidth()) { + useWidth = getMaxEditRenderWidth(); + } + + return + x > this->x && + y > this->y && + x < this->x + useWidth && + y < this->y + h; + } + + bool GraphicLabel::getCenteredW() const { + bool result = (centered || centeredW == 1); + return result; + } + //void GraphicLabel::setCenteredW(bool centered) { + // centeredW = (centered ? 1 : 0); + //} + + bool GraphicLabel::getCenteredH() const { + bool result = (centered || centeredH == 1); + return result; + } + //void GraphicLabel::setCenteredH(bool centered) { + // centeredH = (centered ? 1 : 0); + //} + + // ===================================================== + // class GraphicButton + // ===================================================== + + const int GraphicButton::defH = 22; + const int GraphicButton::defW = 90; + + GraphicButton::GraphicButton(const std::string &containerName, const std::string &objName, bool registerControl) : + GraphicComponent(containerName, objName, registerControl) { + + lighted = false; + alwaysLighted = false; + useCustomTexture = false; + customTexture = NULL; + } + + void GraphicButton::init(int x, int y, int w, int h) { + GraphicComponent::init(x, y, w, h); + lighted = false; + } + + bool GraphicButton::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + bool b = GraphicComponent::mouseMove(x, y); + lighted = b; + return b; + } + + // ===================================================== + // class GraphicListBox + // ===================================================== + + const int GraphicListBox::defH = 22; + const int GraphicListBox::defW = 140; + + GraphicListBox::GraphicListBox(const std::string &containerName, const std::string &objName) + : GraphicComponent(containerName, objName), graphButton1(containerName, objName + "_button1"), + graphButton2(containerName, objName + "_button2") { + selectedItemIndex = 0; + lighted = false; + leftControlled = false; + } + + void GraphicListBox::init(int x, int y, int w, int h, Vec3f textColor) { + GraphicComponent::init(x, y, w, h); + + this->textColor = textColor; + graphButton1.init(x, y, 22, h); + graphButton2.init(x + w - 22, y, 22, h); + graphButton1.setText("<"); + graphButton2.setText(">"); + selectedItemIndex = -1; + lighted = false; + } + + const string & GraphicListBox::getTextNativeTranslation() { + if (this->translated_items.empty() == true || + this->selectedItemIndex < 0 || + this->selectedItemIndex >= (int) this->translated_items.size() || + this->items.size() != this->translated_items.size()) { + return this->text; + } else { + return this->translated_items[this->selectedItemIndex]; + } + } + + //queryes + void GraphicListBox::pushBackItem(string item, string translated_item) { + items.push_back(item); + translated_items.push_back(translated_item); + setSelectedItemIndex(0); + } + + void GraphicListBox::setItems(const vector &items, const vector translated_items) { + this->items = items; + this->translated_items = translated_items; + if (items.empty() == false) { + setSelectedItemIndex(0); + } else { + selectedItemIndex = -1; + setText(""); + } + } + + void GraphicListBox::setSelectedItemIndex(int index, bool errorOnMissing) { + if (errorOnMissing == true && (index < 0 || index >= (int) items.size())) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Index not found in listbox name: [%s] value index: %d size: %lu", this->instanceName.c_str(), index, (unsigned long) items.size()); + throw megaglest_runtime_error(szBuf); + } + selectedItemIndex = index; + setText(getSelectedItem()); + } + + void GraphicListBox::setLeftControlled(bool leftControlled) { + if (this->leftControlled != leftControlled) { + this->leftControlled = leftControlled; + if (leftControlled == true) { + graphButton2.setX(x + graphButton1.getW() - 4); + graphButton2.setH(graphButton2.getH() - 4); + graphButton2.setW(graphButton2.getW() - 4); + graphButton1.setH(graphButton1.getH() - 4); + graphButton1.setW(graphButton1.getW() - 4); + graphButton2.setY(graphButton2.getY() + 2); + graphButton1.setY(graphButton1.getY() + 2); + } else { + graphButton2.setX(x + w - graphButton2.getW() + 4); + graphButton2.setH(graphButton2.getH() + 4); + graphButton2.setW(graphButton2.getW() + 4); + graphButton1.setH(graphButton1.getH() + 4); + graphButton1.setW(graphButton1.getW() + 4); + graphButton2.setY(graphButton2.getY() - 2); + graphButton1.setY(graphButton1.getY() - 2); + } + } + } + + void GraphicListBox::setX(int x) { + this->x = x; + graphButton1.setX(x); + if (leftControlled == true) { + graphButton2.setX(x + graphButton1.getW()); + } else { + graphButton2.setX(x + w - graphButton2.getW()); + } + } + + void GraphicListBox::setY(int y) { + this->y = y; + graphButton1.setY(y); + graphButton2.setY(y); + } + + void GraphicListBox::setEditable(bool editable) { + graphButton1.setEditable(editable); + graphButton2.setEditable(editable); + GraphicComponent::setEditable(editable); + } + + bool GraphicListBox::hasItem(string item) const { + bool result = false; + vector::const_iterator iter = find(items.begin(), items.end(), item); + if (iter != items.end()) { + result = true; + } + + return result; + } + + void GraphicListBox::setSelectedItem(string item, bool errorOnMissing) { + vector::iterator iter; + + iter = find(items.begin(), items.end(), item); + + if (iter == items.end()) { + if (errorOnMissing == true) { + for (int idx = 0; idx < (int) items.size(); idx++) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\ninstanceName [%s] idx = %d items[idx] = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, instanceName.c_str(), idx, items[idx].c_str()); + } + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Value not found in listbox name: [%s] value: %s", this->instanceName.c_str(), item.c_str()); + throw megaglest_runtime_error(szBuf); + } + } else { + setSelectedItemIndex(iter - items.begin()); + } + + } + + bool GraphicListBox::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + return + graphButton1.mouseMove(x, y) || + graphButton2.mouseMove(x, y); + } + + bool GraphicListBox::mouseClick(int x, int y, string advanceToItemStartingWith) { + if (this->getVisible() == false) { + return false; + } + + if (!items.empty()) { + bool b1 = graphButton1.mouseClick(x, y); + bool b2 = graphButton2.mouseClick(x, y); + + if (b1) { + bool bFound = false; + if (advanceToItemStartingWith != "") { + for (int i = selectedItemIndex - 1; i >= 0; --i) { + string item = items[i]; + if ((int) translated_items.size() > i) item = translated_items[i]; + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + if (bFound == false) { + for (int i = (int) items.size() - 1; i >= selectedItemIndex; --i) { + string item = items[i]; + if ((int) translated_items.size() > i) item = translated_items[i]; + //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } } } - } - } - if(bFound == false) { - selectedItemIndex--; - } - if(selectedItemIndex<0){ - selectedItemIndex = (int)items.size()-1; - } - } - else if(b2) { - bool bFound = false; - if(advanceToItemStartingWith != "") { - for(int i = selectedItemIndex + 1; i < (int)items.size(); ++i) { - string item = items[i]; - if((int)translated_items.size() > i) item = translated_items[i]; - //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); - if(StartsWith(toLower(item),toLower(advanceToItemStartingWith)) == true) { - bFound = true; - selectedItemIndex = i; - break; + if (bFound == false) { + selectedItemIndex--; } - } - if(bFound == false) { - for(int i = 0; i <= selectedItemIndex; ++i) { - string item = items[i]; - if((int)translated_items.size() > i) item = translated_items[i]; - //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); - if(StartsWith(toLower(item),toLower(advanceToItemStartingWith)) == true) { - bFound = true; - selectedItemIndex = i; - break; + if (selectedItemIndex < 0) { + selectedItemIndex = (int) items.size() - 1; + } + } else if (b2) { + bool bFound = false; + if (advanceToItemStartingWith != "") { + for (int i = selectedItemIndex + 1; i < (int) items.size(); ++i) { + string item = items[i]; + if ((int) translated_items.size() > i) item = translated_items[i]; + //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } + if (bFound == false) { + for (int i = 0; i <= selectedItemIndex; ++i) { + string item = items[i]; + if ((int) translated_items.size() > i) item = translated_items[i]; + //printf("Trying to match [%s] with item [%s]\n",advanceToItemStartingWith.c_str(),item.c_str()); + if (StartsWith(toLower(item), toLower(advanceToItemStartingWith)) == true) { + bFound = true; + selectedItemIndex = i; + break; + } + } } } + if (bFound == false) { + selectedItemIndex++; + } + if (selectedItemIndex >= (int) items.size()) { + selectedItemIndex = 0; + } + } + setText(getSelectedItem()); + + return b1 || b2; + } + return false; + } + + // ===================================================== + // class GraphicMessageBox + // ===================================================== + + const int GraphicMessageBox::defH = 280; + const int GraphicMessageBox::defW = 350; + + GraphicMessageBox::GraphicMessageBox(const std::string &containerName, const std::string &objName) : + GraphicComponent(containerName, objName) { + header = ""; + autoWordWrap = true; + } + + GraphicMessageBox::~GraphicMessageBox() { + removeButtons(); + } + + void GraphicMessageBox::removeButtons() { + while (!buttons.empty()) { + delete buttons.back(); + buttons.pop_back(); + } + } + + void GraphicMessageBox::init(const string &button1Str, const string &button2Str, int newWidth, int newHeight) { + init(button1Str, newWidth, newHeight); + addButton(button2Str); + } + + void GraphicMessageBox::init(const string &button1Str, int newWidth, int newHeight) { + init(newWidth, newHeight); + removeButtons(); + addButton(button1Str); + } + + void GraphicMessageBox::init(int newWidth, int newHeight) { + setFont(CoreData::getInstance().getMenuFontNormal()); + setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + h = (newHeight >= 0 ? newHeight : defH); + w = (newWidth >= 0 ? newWidth : defW); + + const Metrics &metrics = Metrics::getInstance(); + + x = (metrics.getVirtualW() - w) / 2; + y = (metrics.getVirtualH() - h) / 2; + } + + void GraphicMessageBox::addButton(const string &buttonStr, int width, int height) { + GraphicButton *newButton = new GraphicButton(containerName, instanceName + "_Button_" + buttonStr); + newButton->init(0, 0); + newButton->setText(buttonStr); + if (width != -1) { + newButton->setW(width); + } + if (height != -1) { + newButton->setH(height); + } + buttons.push_back(newButton); + alignButtons(); + } + + void GraphicMessageBox::alignButtons() { + int currXPos = 0; + int totalbuttonListLength = 0; + int buttonOffset = 5; + for (int i = 0; i < getButtonCount(); i++) { + GraphicButton *button = getButton(i); + totalbuttonListLength += button->getW(); + } + totalbuttonListLength += (getButtonCount() - 1)*buttonOffset; + currXPos = x + w / 2 - totalbuttonListLength / 2; + for (int i = 0; i < getButtonCount(); i++) { + GraphicButton *button = getButton(i); + button->setY(y + 25); + button->setX(currXPos); + currXPos += button->getW() + buttonOffset; + } + } + + void GraphicMessageBox::setX(int x) { + this->x = x; + alignButtons(); + } + + void GraphicMessageBox::setY(int y) { + this->y = y; + alignButtons(); + } + + bool GraphicMessageBox::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + for (int i = 0; i < getButtonCount(); i++) { + if (getButton(i)->mouseMove(x, y)) { + return true; } } - if(bFound == false) { - selectedItemIndex++; + return false; + } + + bool GraphicMessageBox::mouseClick(int x, int y) { + if (this->getVisible() == false) { + return false; } - if(selectedItemIndex >= (int)items.size()) { - selectedItemIndex=0; - } - } - setText(getSelectedItem()); - return b1 || b2; - } - return false; -} - -// ===================================================== -// class GraphicMessageBox -// ===================================================== - -const int GraphicMessageBox::defH= 280; -const int GraphicMessageBox::defW= 350; - -GraphicMessageBox::GraphicMessageBox(const std::string &containerName, const std::string &objName) : - GraphicComponent(containerName, objName) { - header= ""; - autoWordWrap=true; -} - -GraphicMessageBox::~GraphicMessageBox(){ - removeButtons(); -} - -void GraphicMessageBox::removeButtons(){ - while(!buttons.empty()){ - delete buttons.back(); - buttons.pop_back(); - } -} - -void GraphicMessageBox::init(const string &button1Str, const string &button2Str, int newWidth, int newHeight){ - init(button1Str, newWidth, newHeight); - addButton(button2Str); -} - -void GraphicMessageBox::init(const string &button1Str, int newWidth, int newHeight){ - init(newWidth,newHeight); - removeButtons(); - addButton(button1Str); -} - -void GraphicMessageBox::init(int newWidth, int newHeight) { - setFont(CoreData::getInstance().getMenuFontNormal()); - setFont3D(CoreData::getInstance().getMenuFontNormal3D()); - - h= (newHeight >= 0 ? newHeight : defH); - w= (newWidth >= 0 ? newWidth : defW); - - const Metrics &metrics= Metrics::getInstance(); - - x= (metrics.getVirtualW() - w) / 2; - y= (metrics.getVirtualH() - h) / 2; -} - -void GraphicMessageBox::addButton(const string &buttonStr, int width, int height){ - GraphicButton *newButton= new GraphicButton(containerName, instanceName + "_Button_" + buttonStr); - newButton->init(0, 0); - newButton->setText(buttonStr); - if(width != -1){ - newButton->setW(width); - } - if(height != -1){ - newButton->setH(height); - } - buttons.push_back(newButton); - alignButtons(); -} - -void GraphicMessageBox::alignButtons(){ - int currXPos= 0; - int totalbuttonListLength=0; - int buttonOffset=5; - for(int i= 0; i < getButtonCount(); i++){ - GraphicButton *button= getButton(i); - totalbuttonListLength+=button->getW(); - } - totalbuttonListLength+=(getButtonCount()-1)*buttonOffset; - currXPos=x+w/2-totalbuttonListLength/2; - for(int i= 0; i < getButtonCount(); i++){ - GraphicButton *button= getButton(i); - button->setY(y + 25); - button->setX(currXPos); - currXPos+=button->getW()+buttonOffset; - } -} - -void GraphicMessageBox::setX(int x){ - this->x= x; - alignButtons(); -} - -void GraphicMessageBox::setY(int y){ - this->y= y; - alignButtons(); -} - -bool GraphicMessageBox::mouseMove(int x, int y){ - if(this->getVisible() == false){ - return false; - } - for(int i= 0; i < getButtonCount(); i++){ - if(getButton(i)->mouseMove(x, y)){ - return true; - } - } - return false; -} - -bool GraphicMessageBox::mouseClick(int x, int y){ - if(this->getVisible() == false){ - return false; - } - - for(int i= 0; i < getButtonCount(); i++){ - if(getButton(i)->mouseClick(x, y)){ - return true; - } - } - return false; -} - -bool GraphicMessageBox::mouseClick(int x, int y, int &clickedButton){ - if(this->getVisible() == false){ - return false; - } - - for(int i= 0; i < getButtonCount(); i++){ - if(getButton(i)->mouseClick(x, y)){ - clickedButton=i; - return true; - } - } - return false; -} - -// ===================================================== -// class GraphicLine -// ===================================================== - -const int GraphicLine::defH= 5; -const int GraphicLine::defW= 1000; - -GraphicLine::GraphicLine(const std::string &containerName, const std::string &objName) -: GraphicComponent(containerName, objName) { - horizontal = false; -} - -void GraphicLine::init(int x, int y, int w, int h){ - GraphicComponent::init(x, y, w, h); - horizontal=true; -} - -// ===================================================== -// class GraphicCheckBox -// ===================================================== - -const int GraphicCheckBox::defH= 22; -const int GraphicCheckBox::defW= 22; - -GraphicCheckBox::GraphicCheckBox(const std::string &containerName, const std::string &objName) -: GraphicComponent(containerName, objName) { - value = false; - lighted = false; -} - -void GraphicCheckBox::init(int x, int y, int w, int h){ - GraphicComponent::init(x, y, w, h); - value=true; - lighted= false; -} - -bool GraphicCheckBox::mouseMove(int x, int y){ - if(this->getVisible() == false) { - return false; - } - - bool b= GraphicComponent::mouseMove(x, y); - lighted= b; - return b; -} - -bool GraphicCheckBox::mouseClick(int x, int y){ - bool result=GraphicComponent::mouseClick( x, y); - if(result == true) { - if(value) { - value=false; - } - else { - value=true; - } - } - return result; -} - -// ===================================================== -// class GraphicScrollBar -// ===================================================== - -const int GraphicScrollBar::defThickness=20; -const int GraphicScrollBar::defLength= 200; - -GraphicScrollBar::GraphicScrollBar(const std::string &containerName, const std::string &objName) -: GraphicComponent(containerName, objName) { - lighted = false; - activated = false; - horizontal = false; - elementCount = 0; - visibleSize = 0; - visibleStart = 0; - - // position on component for renderer - visibleCompPosStart = 0; - visibleCompPosEnd = 0; -} - -void GraphicScrollBar::init(int x, int y, bool horizontal,int length, int thickness){ - GraphicComponent::init(x, y, horizontal?length:thickness,horizontal?thickness:length ); - this->horizontal=horizontal; - this->elementCount=1; - this->visibleSize=1; - this->visibleStart=0; - this->visibleCompPosStart=0; - this->visibleCompPosEnd=length; - activated = false; - lighted = false; -} - -bool GraphicScrollBar::mouseDown(int x, int y) { - if(getVisible() && getEnabled() && getEditable()) - { - if(activated && elementCount>0) - { - if( elementCount>visibleSize) { - int pos; - if(horizontal){ - pos=x-this->x; + for (int i = 0; i < getButtonCount(); i++) { + if (getButton(i)->mouseClick(x, y)) { + return true; } - else { - // invert the clicked point ( y is from bottom to top normally ) - pos=getLength()-(y-this->y); + } + return false; + } + + bool GraphicMessageBox::mouseClick(int x, int y, int &clickedButton) { + if (this->getVisible() == false) { + return false; + } + + for (int i = 0; i < getButtonCount(); i++) { + if (getButton(i)->mouseClick(x, y)) { + clickedButton = i; + return true; } - float partSize=(float)getLength()/(float)elementCount; - float visiblePartSize=partSize*(float)visibleSize; - float startPos=((float)pos)-visiblePartSize/2; - - visibleStart=startPos/partSize; - setVisibleStart(visibleStart); - } + return false; } - } - return false; -} -void GraphicScrollBar::mouseUp(int x, int y) { - activated = false; - lighted = false; -} + // ===================================================== + // class GraphicLine + // ===================================================== -void GraphicScrollBar::setVisibleStart(int vs){ - visibleStart=vs; + const int GraphicLine::defH = 5; + const int GraphicLine::defW = 1000; - if(visibleStart>elementCount-visibleSize) { - visibleStart=elementCount-visibleSize; - } - if(visibleStart<0) { - visibleStart=0; - } - float partSize = 0.f; - if(elementCount > 0) { - partSize = (float)getLength()/(float)elementCount; - } - visibleCompPosStart=visibleStart*partSize; - visibleCompPosEnd=visibleStart*partSize+visibleSize*partSize; - if(visibleCompPosEnd>getLength()) { - visibleCompPosEnd=getLength(); - } - if(!horizontal) { - // invert the display ( y is from bottom to top normally ) - visibleCompPosStart=getLength()-visibleCompPosStart; - visibleCompPosEnd=getLength()-visibleCompPosEnd; - } -} + GraphicLine::GraphicLine(const std::string &containerName, const std::string &objName) + : GraphicComponent(containerName, objName) { + horizontal = false; + } -void GraphicScrollBar::setElementCount(int elementCount){ - this->elementCount=elementCount; - setVisibleStart(getVisibleStart()); -} + void GraphicLine::init(int x, int y, int w, int h) { + GraphicComponent::init(x, y, w, h); + horizontal = true; + } -void GraphicScrollBar::setVisibleSize(int visibleSize){ - this->visibleSize=visibleSize; - setVisibleStart(getVisibleStart()); -} + // ===================================================== + // class GraphicCheckBox + // ===================================================== + const int GraphicCheckBox::defH = 22; + const int GraphicCheckBox::defW = 22; -bool GraphicScrollBar::mouseClick(int x, int y){ - bool result=GraphicComponent::mouseClick( x, y); - if(result) { - activated = true; - lighted = true; - mouseDown( x, y); - } - return result; -} + GraphicCheckBox::GraphicCheckBox(const std::string &containerName, const std::string &objName) + : GraphicComponent(containerName, objName) { + value = false; + lighted = false; + } + void GraphicCheckBox::init(int x, int y, int w, int h) { + GraphicComponent::init(x, y, w, h); + value = true; + lighted = false; + } -bool GraphicScrollBar::mouseMove(int x, int y){ - if(this->getVisible() == false) { - return false; - } - - bool inScrollBar = GraphicComponent::mouseMove(x, y); - if (activated) { - lighted = true; - } else { - lighted = inScrollBar; - } - return inScrollBar; -} - -int GraphicScrollBar::getLength() const { - return horizontal?getW():getH(); -} - -//int GraphicScrollBar::getThickness() const { -// return horizontal?getH():getW(); -//} - -void GraphicScrollBar::arrangeComponents(vector &gcs) { - if(getElementCount()!=0 ) { - for(int i = getVisibleStart(); i <= getVisibleEnd(); ++i) { - if(horizontal){ - gcs[i]->setX(getX()+getLength()-gcs[i]->getW()-gcs[i]->getW()*(i-getVisibleStart())); - } - else { - gcs[i]->setY(getY()+getLength()-gcs[i]->getH()-gcs[i]->getH()*(i-getVisibleStart())); - } - } - } -} -// =========================================================== -// class PopupMenu -// =========================================================== - -const int PopupMenu::defH= 240; -const int PopupMenu::defW= 350; - -PopupMenu::PopupMenu(const std::string &containerName, const std::string &objName) : - GraphicComponent(containerName, objName, false) { - registerGraphicComponentOnlyFontCallbacks(containerName,objName); - - h= defH; - w= defW; -} - -PopupMenu::~PopupMenu() { - -} - -void PopupMenu::init(string menuHeader,std::vector menuItems) { - header = menuHeader; - - setFont(CoreData::getInstance().getMenuFontNormal()); - setFont3D(CoreData::getInstance().getMenuFontNormal3D()); - - buttons.clear(); - - const Metrics &metrics= Metrics::getInstance(); - - x= (metrics.getVirtualW()-w)/2; - y= (metrics.getVirtualH()-h)/2; - - int textHeight = GraphicButton::defH; - int textHeightSpacing = 6; - - int maxButtonWidth = -1; - for(unsigned int i = 0; i < menuItems.size(); ++i) { - int currentButtonWidth = -1; - if(font3D != NULL && Shared::Graphics::Font::forceLegacyFonts == false) { - FontMetrics *fontMetrics= font3D->getMetrics(); - if(fontMetrics) { - currentButtonWidth = fontMetrics->getTextWidth(menuItems[i]); + bool GraphicCheckBox::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; } + + bool b = GraphicComponent::mouseMove(x, y); + lighted = b; + return b; } - else if(font) { - FontMetrics *fontMetrics= font->getMetrics(); - if(fontMetrics) { - currentButtonWidth = fontMetrics->getTextWidth(menuItems[i]); + + bool GraphicCheckBox::mouseClick(int x, int y) { + bool result = GraphicComponent::mouseClick(x, y); + if (result == true) { + if (value) { + value = false; + } else { + value = true; + } + } + return result; + } + + // ===================================================== + // class GraphicScrollBar + // ===================================================== + + const int GraphicScrollBar::defThickness = 20; + const int GraphicScrollBar::defLength = 200; + + GraphicScrollBar::GraphicScrollBar(const std::string &containerName, const std::string &objName) + : GraphicComponent(containerName, objName) { + lighted = false; + activated = false; + horizontal = false; + elementCount = 0; + visibleSize = 0; + visibleStart = 0; + + // position on component for renderer + visibleCompPosStart = 0; + visibleCompPosEnd = 0; + } + + void GraphicScrollBar::init(int x, int y, bool horizontal, int length, int thickness) { + GraphicComponent::init(x, y, horizontal ? length : thickness, horizontal ? thickness : length); + this->horizontal = horizontal; + this->elementCount = 1; + this->visibleSize = 1; + this->visibleStart = 0; + this->visibleCompPosStart = 0; + this->visibleCompPosEnd = length; + activated = false; + lighted = false; + } + + bool GraphicScrollBar::mouseDown(int x, int y) { + if (getVisible() && getEnabled() && getEditable()) { + if (activated && elementCount > 0) { + if (elementCount > visibleSize) { + int pos; + if (horizontal) { + pos = x - this->x; + } else { + // invert the clicked point ( y is from bottom to top normally ) + pos = getLength() - (y - this->y); + } + float partSize = (float) getLength() / (float) elementCount; + float visiblePartSize = partSize * (float) visibleSize; + float startPos = ((float) pos) - visiblePartSize / 2; + + visibleStart = startPos / partSize; + setVisibleStart(visibleStart); + + } + } + } + return false; + } + + void GraphicScrollBar::mouseUp(int x, int y) { + activated = false; + lighted = false; + } + + void GraphicScrollBar::setVisibleStart(int vs) { + visibleStart = vs; + + if (visibleStart > elementCount - visibleSize) { + visibleStart = elementCount - visibleSize; + } + if (visibleStart < 0) { + visibleStart = 0; + } + float partSize = 0.f; + if (elementCount > 0) { + partSize = (float) getLength() / (float) elementCount; + } + visibleCompPosStart = visibleStart * partSize; + visibleCompPosEnd = visibleStart * partSize + visibleSize * partSize; + if (visibleCompPosEnd > getLength()) { + visibleCompPosEnd = getLength(); + } + if (!horizontal) { + // invert the display ( y is from bottom to top normally ) + visibleCompPosStart = getLength() - visibleCompPosStart; + visibleCompPosEnd = getLength() - visibleCompPosEnd; } } - if(maxButtonWidth < 0 || currentButtonWidth > maxButtonWidth) { - maxButtonWidth = currentButtonWidth + textHeightSpacing; + void GraphicScrollBar::setElementCount(int elementCount) { + this->elementCount = elementCount; + setVisibleStart(getVisibleStart()); } - } - int yStartOffset = y + h - (textHeight * 2); - - if(maxButtonWidth >= w) { - w = maxButtonWidth + textHeightSpacing; - x= (metrics.getVirtualW()-w)/2; - } - - int offsetH = (yStartOffset - y); - int maxH = (offsetH + (((int)menuItems.size() -1 ) * (textHeight + textHeightSpacing))); - if(maxH >= h) { - h = maxH; - y= (metrics.getVirtualH()-h)/2; - yStartOffset = y + h - (textHeight * 2); - } - - for(unsigned int i = 0; i < menuItems.size(); ++i) { - GraphicButton button(containerName, instanceName + "_Popup_Button_" + menuItems[i],false); - button.registerGraphicComponentOnlyFontCallbacks(containerName, instanceName + "_Popup_Button_" + menuItems[i]); - button.init(x+(w-maxButtonWidth)/2, yStartOffset - (i*(textHeight + textHeightSpacing))); - button.setText(menuItems[i]); - button.setW(maxButtonWidth); - - buttons.push_back(button); - } -} - -void PopupMenu::setX(int x) { - this->x= x; - - for(unsigned int i = 0; i < buttons.size(); ++i) { - GraphicButton &button = buttons[i]; - button.init(x+(w-GraphicButton::defW)/4, y+25 + (i*25)); - } -} - -void PopupMenu::setY(int y) { - this->y= y; - - for(unsigned int i = 0; i < buttons.size(); ++i) { - GraphicButton &button = buttons[i]; - button.init(x+(w-GraphicButton::defW)/4, y+25 + (i*25)); - } -} - -bool PopupMenu::mouseMove(int x, int y){ - if(this->getVisible() == false) { - return false; - } - - for(unsigned int i = 0; i < buttons.size(); ++i) { - GraphicButton &button = buttons[i]; - if(button.mouseMove(x, y)) { - return true; + void GraphicScrollBar::setVisibleSize(int visibleSize) { + this->visibleSize = visibleSize; + setVisibleStart(getVisibleStart()); } - } - return false; -} -bool PopupMenu::mouseClick(int x, int y) { - if(this->getVisible() == false) { - return false; - } - - for(unsigned int i = 0; i < buttons.size(); ++i) { - GraphicButton &button = buttons[i]; - if(button.mouseClick(x, y)) { - return true; + bool GraphicScrollBar::mouseClick(int x, int y) { + bool result = GraphicComponent::mouseClick(x, y); + if (result) { + activated = true; + lighted = true; + mouseDown(x, y); + } + return result; } - } - return false; -} -std::pair PopupMenu::mouseClickedMenuItem(int x, int y) { - std::pair result; - for(unsigned int i = 0; i < buttons.size(); ++i) { - GraphicButton &button = buttons[i]; - if(button.mouseClick(x, y)) { - result.first = i; - result.second = buttons[i].getText(); - break; + + bool GraphicScrollBar::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + bool inScrollBar = GraphicComponent::mouseMove(x, y); + if (activated) { + lighted = true; + } else { + lighted = inScrollBar; + } + return inScrollBar; } + + int GraphicScrollBar::getLength() const { + return horizontal ? getW() : getH(); + } + + //int GraphicScrollBar::getThickness() const { + // return horizontal?getH():getW(); + //} + + void GraphicScrollBar::arrangeComponents(vector &gcs) { + if (getElementCount() != 0) { + for (int i = getVisibleStart(); i <= getVisibleEnd(); ++i) { + if (horizontal) { + gcs[i]->setX(getX() + getLength() - gcs[i]->getW() - gcs[i]->getW()*(i - getVisibleStart())); + } else { + gcs[i]->setY(getY() + getLength() - gcs[i]->getH() - gcs[i]->getH()*(i - getVisibleStart())); + } + } + } + } + // =========================================================== + // class PopupMenu + // =========================================================== + + const int PopupMenu::defH = 240; + const int PopupMenu::defW = 350; + + PopupMenu::PopupMenu(const std::string &containerName, const std::string &objName) : + GraphicComponent(containerName, objName, false) { + registerGraphicComponentOnlyFontCallbacks(containerName, objName); + + h = defH; + w = defW; + } + + PopupMenu::~PopupMenu() { + + } + + void PopupMenu::init(string menuHeader, std::vector menuItems) { + header = menuHeader; + + setFont(CoreData::getInstance().getMenuFontNormal()); + setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + buttons.clear(); + + const Metrics &metrics = Metrics::getInstance(); + + x = (metrics.getVirtualW() - w) / 2; + y = (metrics.getVirtualH() - h) / 2; + + int textHeight = GraphicButton::defH; + int textHeightSpacing = 6; + + int maxButtonWidth = -1; + for (unsigned int i = 0; i < menuItems.size(); ++i) { + int currentButtonWidth = -1; + if (font3D != NULL && Shared::Graphics::Font::forceLegacyFonts == false) { + FontMetrics *fontMetrics = font3D->getMetrics(); + if (fontMetrics) { + currentButtonWidth = fontMetrics->getTextWidth(menuItems[i]); + } + } else if (font) { + FontMetrics *fontMetrics = font->getMetrics(); + if (fontMetrics) { + currentButtonWidth = fontMetrics->getTextWidth(menuItems[i]); + } + } + + if (maxButtonWidth < 0 || currentButtonWidth > maxButtonWidth) { + maxButtonWidth = currentButtonWidth + textHeightSpacing; + } + } + + int yStartOffset = y + h - (textHeight * 2); + + if (maxButtonWidth >= w) { + w = maxButtonWidth + textHeightSpacing; + x = (metrics.getVirtualW() - w) / 2; + } + + int offsetH = (yStartOffset - y); + int maxH = (offsetH + (((int) menuItems.size() - 1) * (textHeight + textHeightSpacing))); + if (maxH >= h) { + h = maxH; + y = (metrics.getVirtualH() - h) / 2; + yStartOffset = y + h - (textHeight * 2); + } + + for (unsigned int i = 0; i < menuItems.size(); ++i) { + GraphicButton button(containerName, instanceName + "_Popup_Button_" + menuItems[i], false); + button.registerGraphicComponentOnlyFontCallbacks(containerName, instanceName + "_Popup_Button_" + menuItems[i]); + button.init(x + (w - maxButtonWidth) / 2, yStartOffset - (i*(textHeight + textHeightSpacing))); + button.setText(menuItems[i]); + button.setW(maxButtonWidth); + + buttons.push_back(button); + } + } + + void PopupMenu::setX(int x) { + this->x = x; + + for (unsigned int i = 0; i < buttons.size(); ++i) { + GraphicButton &button = buttons[i]; + button.init(x + (w - GraphicButton::defW) / 4, y + 25 + (i * 25)); + } + } + + void PopupMenu::setY(int y) { + this->y = y; + + for (unsigned int i = 0; i < buttons.size(); ++i) { + GraphicButton &button = buttons[i]; + button.init(x + (w - GraphicButton::defW) / 4, y + 25 + (i * 25)); + } + } + + bool PopupMenu::mouseMove(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + for (unsigned int i = 0; i < buttons.size(); ++i) { + GraphicButton &button = buttons[i]; + if (button.mouseMove(x, y)) { + return true; + } + } + + return false; + } + + bool PopupMenu::mouseClick(int x, int y) { + if (this->getVisible() == false) { + return false; + } + + for (unsigned int i = 0; i < buttons.size(); ++i) { + GraphicButton &button = buttons[i]; + if (button.mouseClick(x, y)) { + return true; + } + } + return false; + } + + std::pair PopupMenu::mouseClickedMenuItem(int x, int y) { + std::pair result; + for (unsigned int i = 0; i < buttons.size(); ++i) { + GraphicButton &button = buttons[i]; + if (button.mouseClick(x, y)) { + result.first = i; + result.second = buttons[i].getText(); + break; + } + } + + return result; + } + } - - return result; -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/facilities/components.h b/source/glest_game/facilities/components.h index 937de0d7f..7fa5a8ac1 100644 --- a/source/glest_game/facilities/components.h +++ b/source/glest_game/facilities/components.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_GRAPHCOMPONENT_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -34,435 +34,631 @@ using Shared::Graphics::Font3D; using namespace Shared::Graphics; using Shared::Graphics::Vec3f; -namespace Glest{ namespace Game{ - -class GraphicComponent; - -// =========================================================== -// class GraphicComponent -// -// OpenGL renderer GUI components -// =========================================================== - -class GraphicComponent : public FontChangedCallbackInterface { -public: - static const float animSpeed; - static const float fadeSpeed; - - static std::map > registeredGraphicComponentList; - -protected: - int x, y, w, h; - string text; - string textNativeTranslation; - Font2D *font; - Font3D *font3D; - string font2DUniqueId; - string font3DUniqueId; - bool enabled; - bool editable; - bool visible; - - static float anim; - static float fade; - static Vec3f customTextColor; - - string containerName; - string instanceName; - string fontCallbackName; - - string getNewUUD(); - virtual void FontChangedCallback(std::string fontUniqueId, Font *font); - -public: - GraphicComponent(const std::string &containerName="", const std::string &objName="", bool registerControl=true); - virtual ~GraphicComponent(); - - static void setCustomTextColor(Vec3f value) { customTextColor = value; } - static Vec3f getCustomTextColor() { return customTextColor; } - - static void clearRegisteredComponents(std::string containerName=""); - static void clearRegisterGraphicComponent(std::string containerName, std::string objName); - static void clearRegisterGraphicComponent(std::string containerName, std::vector objNameList); - virtual void registerGraphicComponent(std::string containerName, std::string objName); - virtual void registerGraphicComponentOnlyFontCallbacks(std::string containerName, std::string objName); - static GraphicComponent * findRegisteredComponent(std::string containerName, std::string objName); - static void applyAllCustomProperties(std::string containerName); - virtual void applyCustomProperties(std::string containerName); - - static bool saveAllCustomProperties(std::string containerName); - virtual bool saveCustomProperties(std::string containerName); - - void init(int x, int y, int w, int h); - - string getContainerName() const { return containerName; } - string getInstanceName() const { return instanceName; } - string getFontCallbackName() const { return fontCallbackName; } - void setInstanceName(string value) { instanceName = value; } - - virtual int getX() const {return x;} - virtual int getY() const {return y;} - virtual int getW() const {return w;} - virtual int getH() const {return h;} - virtual const string &getText() const {return text;} - virtual const string &getTextNativeTranslation() { return this->textNativeTranslation;} - virtual Font2D *getFont() {return font;} - virtual Font3D *getFont3D() {return font3D;} - virtual bool getEnabled() const {return enabled;} - virtual bool getEditable() const {return editable;} - virtual bool getVisible() const {return visible;} - - virtual void setX(int x) {this->x= x;} - virtual void setY(int y) {this->y= y;} - virtual void setW(int w) {this->w= w;} - virtual void setH(int h) {this->h= h;} - virtual void setText(const string &text) {this->text= text;} - virtual void setTextNativeTranslation(const string &text) {this->textNativeTranslation= text;} - virtual void setFont(Font2D *font); - virtual void setFont3D(Font3D *font); - virtual void setEnabled(bool enabled) {this->enabled= enabled;} - virtual void setEditable(bool editable) {this->editable= editable;} - virtual void setVisible(bool value) {this->visible = value;} - - virtual void reloadFonts(); - static void reloadFontsForRegisterGraphicComponents(std::string containerName); - - virtual bool mouseMove(int x, int y); - bool mouseClick(int x, int y); - - static void update(); - static void resetFade(); - static float getAnim() {return anim;} - static float getFade() {return fade;} -}; - -// =========================================================== -// class GraphicLabel -// =========================================================== - -class GraphicLabel: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - bool centered; - Vec3f textColor; - bool wordWrap; - - int centeredW; - int centeredH; - - bool editModeEnabled; - int maxEditWidth; - int maxEditRenderWidth; - bool renderBackground; - Vec4f backgroundColor; - - vector textCharLength; - bool isPassword; - Texture2D *texture; - -public: - GraphicLabel(const std::string &containerName="", const std::string &objName="", bool registerControl=true); - void init(int x, int y, int w=defW, int h=defH, bool centered= false, Vec3f textColor=GraphicComponent::customTextColor, bool wordWrap=false); - - virtual bool mouseMove(int x, int y); - - vector getTextCharLengthList() const { return textCharLength; } - void setTextCharLengthList(vector value) { textCharLength = value; } - void clearTextCharLengthList() { textCharLength.clear(); } - void addTextCharLengthToList(int length) { textCharLength.push_back(length); } - void deleteTextCharLengthFromList() { textCharLength.pop_back(); } - - bool getIsPassword() const { return isPassword; } - void setIsPassword(bool value) { isPassword = value; } - - bool getCentered() const {return centered;} - void setCentered(bool centered) {this->centered= centered;} - - bool getCenteredW() const; - //void setCenteredW(bool centered); - - bool getCenteredH() const; - //void setCenteredH(bool centered); - - Vec3f getTextColor() const {return textColor;} - void setTextColor(Vec3f color) {this->textColor= color;} - - bool getWordWrap() const { return wordWrap; } - void setWordWrap(bool value) { wordWrap = value; } - - void setEditModeEnabled(bool value) { editModeEnabled = value; } - bool getEditModeEnabled() const { return editModeEnabled; } - - void setMaxEditWidth(int value) { maxEditWidth = value; } - int getMaxEditWidth() const { return maxEditWidth; } - - void setRenderBackground(bool value) { renderBackground = value; } - bool getRenderBackground() const { return renderBackground; } - Vec4f getBackgroundColor() const {return backgroundColor;} - void setBackgroundColor(Vec4f color) {this->backgroundColor= color;} - - void setMaxEditRenderWidth(int value) { maxEditRenderWidth = value; } - int getMaxEditRenderWidth() const { return maxEditRenderWidth; } - - void setTexture(Texture2D *value) { texture = value; } - Texture2D *getTexture() const { return texture; } -}; - -// =========================================================== -// class GraphicButton -// =========================================================== - -class GraphicButton: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - bool lighted; - bool alwaysLighted; - - bool useCustomTexture; - Texture *customTexture; - -public: - GraphicButton(const std::string &containerName="", const std::string &objName="", bool registerControl=true); - void init(int x, int y, int w=defW, int h=defH); - - bool getUseCustomTexture() const { return useCustomTexture; } - Texture *getCustomTexture() const { return customTexture; } - - void setUseCustomTexture(bool value) { useCustomTexture=value; } - void setCustomTexture(Texture *value) { customTexture=value; } - - bool getLighted() const {return lighted||alwaysLighted;} - void setLighted(bool lighted) {this->lighted= lighted;} - bool getAlwaysLighted() const {return alwaysLighted;} - void setAlwaysLighted(bool value) {this->alwaysLighted= value;} - virtual bool mouseMove(int x, int y); -}; - -// =========================================================== -// class GraphicListBox -// =========================================================== - -class GraphicListBox: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - GraphicButton graphButton1, graphButton2; - vector items; - vector translated_items; - int selectedItemIndex; - bool lighted; - Vec3f textColor; - bool leftControlled; - -public: - GraphicListBox(const std::string &containerName="", const std::string &objName=""); - void init(int x, int y, int w=defW, int h=defH, Vec3f textColor=GraphicComponent::customTextColor); - - int getItemCount() const {return (int)items.size();} - string getItem(int index) const {return items[index];} - int getSelectedItemIndex() const {return selectedItemIndex;} - string getSelectedItem() const {return items[selectedItemIndex];} - GraphicButton *getButton1() {return &graphButton1;} - GraphicButton *getButton2() {return &graphButton2;} - bool getLighted() const {return lighted;} - void setLighted(bool lighted) {this->lighted= lighted;} - bool getLeftControlled() const {return leftControlled;} - void setLeftControlled(bool leftControlled); - Vec3f getTextColor() const {return textColor;} - void setTextColor(Vec3f color) {this->textColor= color;} - - void pushBackItem(string item, string translated_item=""); - void setItems(const vector &items, const vector translated_items=vector()); - void setSelectedItemIndex(int index, bool errorOnMissing=true); - void setSelectedItem(string item, bool errorOnMissing=true); - void setEditable(bool editable); - - bool hasItem(string item) const; - - virtual void setX(int x); - virtual void setY(int y); - - virtual bool mouseMove(int x, int y); - virtual bool mouseClick(int x, int y, string advanceToItemStartingWith=""); - - virtual const string &getTextNativeTranslation(); -}; - -// =========================================================== -// class GraphicMessageBox -// =========================================================== -typedef vector GraphicButtons; -class GraphicMessageBox: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - GraphicButtons buttons; - string header; - bool autoWordWrap; - -private: - void alignButtons(); -public: - GraphicMessageBox(const std::string &containerName="", const std::string &objName=""); - virtual ~GraphicMessageBox(); - void init(const string &button1Str, const string &button2Str, int newWidth=-1,int newHeight=-1); - void init(const string &button1Str, int newWidth=-1,int newHeight=-1); - void init(int newWidth=-1,int newHeight=-1); - void removeButtons(); - void addButton(const string &buttonStr, int width=-1,int height=-1); - - bool getAutoWordWrap() const { return autoWordWrap; } - void setAutoWordWrap(bool value) { autoWordWrap = value; } - - int getButtonCount() const {return (int)buttons.size();} - GraphicButton *getButton(int index) {return buttons[index];} - string getHeader() const {return header;} - - virtual void setX(int x); - virtual void setY(int y); - - void setHeader(string header) {this->header= header;} - - virtual bool mouseMove(int x, int y); - virtual bool mouseClick(int x, int y); - bool mouseClick(int x, int y, int &clickedButton); -}; - -// =========================================================== -// class GraphicLine -// =========================================================== - -class GraphicLine: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - bool horizontal; - -public: - GraphicLine(const std::string &containerName="", const std::string &objName=""); - void init(int x, int y, int w=defW, int h=defH); - bool getHorizontal() const {return horizontal;} - void setHorizontal(bool horizontal) {this->horizontal= horizontal;} -}; - -// =========================================================== -// class GraphicCheckBox -// =========================================================== - -class GraphicCheckBox: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - bool value; - bool lighted; - -public: - GraphicCheckBox(const std::string &containerName="", const std::string &objName=""); - void init(int x, int y, int w=defW, int h=defH); - bool getValue() const {return value;} - void setValue(bool value) {this->value= value;} - bool getLighted() const {return lighted;} - void setLighted(bool lighted) {this->lighted= lighted;} - virtual bool mouseMove(int x, int y); - virtual bool mouseClick(int x, int y); -}; - -// =========================================================== -// class GraphicScrollBar -// =========================================================== - -class GraphicScrollBar: public GraphicComponent { -public: - static const int defLength; - static const int defThickness; - -private: - bool activated; - bool lighted; - bool horizontal; - int elementCount; - int visibleSize; - int visibleStart; - - // position on component for renderer - int visibleCompPosStart; - int visibleCompPosEnd; - -public: - GraphicScrollBar(const std::string &containerName="", const std::string &objName=""); - void init(int x, int y, bool horizontal,int length=defLength, int thickness=defThickness); - virtual bool mouseDown(int x, int y); - virtual bool mouseMove(int x, int y); - virtual void mouseUp(int x, int y); - virtual bool mouseClick(int x, int y); - - - bool getHorizontal() const {return horizontal;} - int getLength() const; - void setLength(int length) {horizontal?setW(length):setH(length);} - //int getThickness() const; - - - bool getLighted() const {return lighted;} - void setLighted(bool lighted) {this->lighted= lighted;} - - int getElementCount() const {return elementCount;} - void setElementCount(int elementCount); - int getVisibleSize() const {return visibleSize;} - void setVisibleSize(int visibleSize); - int getVisibleStart() const {return visibleStart;} - int getVisibleEnd() const {return visibleStart+visibleSize>elementCount-1?elementCount-1: visibleStart+visibleSize-1;} - void setVisibleStart(int visibleStart); - - int getVisibleCompPosStart() const {return visibleCompPosStart;} - int getVisibleCompPosEnd() const {return visibleCompPosEnd;} - void arrangeComponents(vector &gcs); -}; - -// =========================================================== -// class PopupMenu -// =========================================================== - -class PopupMenu: public GraphicComponent { -public: - static const int defH; - static const int defW; - -private: - std::vector buttons; - string header; - -public: - PopupMenu(const std::string &containerName="", const std::string &objName=""); - virtual ~PopupMenu(); - void init(string menuHeader, std::vector menuItems); - - std::vector & getMenuItems() {return buttons;} - string getHeader() const {return header;} - - virtual void setX(int x); - virtual void setY(int y); - - void setHeader(string header) {this->header= header;} - - virtual bool mouseMove(int x, int y); - virtual bool mouseClick(int x, int y); - std::pair mouseClickedMenuItem(int x, int y); -}; - -}}//end namespace +namespace Glest { + namespace Game { + + class GraphicComponent; + + // =========================================================== + // class GraphicComponent + // + // OpenGL renderer GUI components + // =========================================================== + + class GraphicComponent : public FontChangedCallbackInterface { + public: + static const float animSpeed; + static const float fadeSpeed; + + static std::map > registeredGraphicComponentList; + + protected: + int x, y, w, h; + string text; + string textNativeTranslation; + Font2D *font; + Font3D *font3D; + string font2DUniqueId; + string font3DUniqueId; + bool enabled; + bool editable; + bool visible; + + static float anim; + static float fade; + static Vec3f customTextColor; + + string containerName; + string instanceName; + string fontCallbackName; + + string getNewUUD(); + virtual void FontChangedCallback(std::string fontUniqueId, Font *font); + + public: + GraphicComponent(const std::string &containerName = "", const std::string &objName = "", bool registerControl = true); + virtual ~GraphicComponent(); + + static void setCustomTextColor(Vec3f value) { + customTextColor = value; + } + static Vec3f getCustomTextColor() { + return customTextColor; + } + + static void clearRegisteredComponents(std::string containerName = ""); + static void clearRegisterGraphicComponent(std::string containerName, std::string objName); + static void clearRegisterGraphicComponent(std::string containerName, std::vector objNameList); + virtual void registerGraphicComponent(std::string containerName, std::string objName); + virtual void registerGraphicComponentOnlyFontCallbacks(std::string containerName, std::string objName); + static GraphicComponent * findRegisteredComponent(std::string containerName, std::string objName); + static void applyAllCustomProperties(std::string containerName); + virtual void applyCustomProperties(std::string containerName); + + static bool saveAllCustomProperties(std::string containerName); + virtual bool saveCustomProperties(std::string containerName); + + void init(int x, int y, int w, int h); + + string getContainerName() const { + return containerName; + } + string getInstanceName() const { + return instanceName; + } + string getFontCallbackName() const { + return fontCallbackName; + } + void setInstanceName(string value) { + instanceName = value; + } + + virtual int getX() const { + return x; + } + virtual int getY() const { + return y; + } + virtual int getW() const { + return w; + } + virtual int getH() const { + return h; + } + virtual const string &getText() const { + return text; + } + virtual const string &getTextNativeTranslation() { + return this->textNativeTranslation; + } + virtual Font2D *getFont() { + return font; + } + virtual Font3D *getFont3D() { + return font3D; + } + virtual bool getEnabled() const { + return enabled; + } + virtual bool getEditable() const { + return editable; + } + virtual bool getVisible() const { + return visible; + } + + virtual void setX(int x) { + this->x = x; + } + virtual void setY(int y) { + this->y = y; + } + virtual void setW(int w) { + this->w = w; + } + virtual void setH(int h) { + this->h = h; + } + virtual void setText(const string &text) { + this->text = text; + } + virtual void setTextNativeTranslation(const string &text) { + this->textNativeTranslation = text; + } + virtual void setFont(Font2D *font); + virtual void setFont3D(Font3D *font); + virtual void setEnabled(bool enabled) { + this->enabled = enabled; + } + virtual void setEditable(bool editable) { + this->editable = editable; + } + virtual void setVisible(bool value) { + this->visible = value; + } + + virtual void reloadFonts(); + static void reloadFontsForRegisterGraphicComponents(std::string containerName); + + virtual bool mouseMove(int x, int y); + bool mouseClick(int x, int y); + + static void update(); + static void resetFade(); + static float getAnim() { + return anim; + } + static float getFade() { + return fade; + } + }; + + // =========================================================== + // class GraphicLabel + // =========================================================== + + class GraphicLabel : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + bool centered; + Vec3f textColor; + bool wordWrap; + + int centeredW; + int centeredH; + + bool editModeEnabled; + int maxEditWidth; + int maxEditRenderWidth; + bool renderBackground; + Vec4f backgroundColor; + + vector textCharLength; + bool isPassword; + Texture2D *texture; + + public: + GraphicLabel(const std::string &containerName = "", const std::string &objName = "", bool registerControl = true); + void init(int x, int y, int w = defW, int h = defH, bool centered = false, Vec3f textColor = GraphicComponent::customTextColor, bool wordWrap = false); + + virtual bool mouseMove(int x, int y); + + vector getTextCharLengthList() const { + return textCharLength; + } + void setTextCharLengthList(vector value) { + textCharLength = value; + } + void clearTextCharLengthList() { + textCharLength.clear(); + } + void addTextCharLengthToList(int length) { + textCharLength.push_back(length); + } + void deleteTextCharLengthFromList() { + textCharLength.pop_back(); + } + + bool getIsPassword() const { + return isPassword; + } + void setIsPassword(bool value) { + isPassword = value; + } + + bool getCentered() const { + return centered; + } + void setCentered(bool centered) { + this->centered = centered; + } + + bool getCenteredW() const; + //void setCenteredW(bool centered); + + bool getCenteredH() const; + //void setCenteredH(bool centered); + + Vec3f getTextColor() const { + return textColor; + } + void setTextColor(Vec3f color) { + this->textColor = color; + } + + bool getWordWrap() const { + return wordWrap; + } + void setWordWrap(bool value) { + wordWrap = value; + } + + void setEditModeEnabled(bool value) { + editModeEnabled = value; + } + bool getEditModeEnabled() const { + return editModeEnabled; + } + + void setMaxEditWidth(int value) { + maxEditWidth = value; + } + int getMaxEditWidth() const { + return maxEditWidth; + } + + void setRenderBackground(bool value) { + renderBackground = value; + } + bool getRenderBackground() const { + return renderBackground; + } + Vec4f getBackgroundColor() const { + return backgroundColor; + } + void setBackgroundColor(Vec4f color) { + this->backgroundColor = color; + } + + void setMaxEditRenderWidth(int value) { + maxEditRenderWidth = value; + } + int getMaxEditRenderWidth() const { + return maxEditRenderWidth; + } + + void setTexture(Texture2D *value) { + texture = value; + } + Texture2D *getTexture() const { + return texture; + } + }; + + // =========================================================== + // class GraphicButton + // =========================================================== + + class GraphicButton : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + bool lighted; + bool alwaysLighted; + + bool useCustomTexture; + Texture *customTexture; + + public: + GraphicButton(const std::string &containerName = "", const std::string &objName = "", bool registerControl = true); + void init(int x, int y, int w = defW, int h = defH); + + bool getUseCustomTexture() const { + return useCustomTexture; + } + Texture *getCustomTexture() const { + return customTexture; + } + + void setUseCustomTexture(bool value) { + useCustomTexture = value; + } + void setCustomTexture(Texture *value) { + customTexture = value; + } + + bool getLighted() const { + return lighted || alwaysLighted; + } + void setLighted(bool lighted) { + this->lighted = lighted; + } + bool getAlwaysLighted() const { + return alwaysLighted; + } + void setAlwaysLighted(bool value) { + this->alwaysLighted = value; + } + virtual bool mouseMove(int x, int y); + }; + + // =========================================================== + // class GraphicListBox + // =========================================================== + + class GraphicListBox : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + GraphicButton graphButton1, graphButton2; + vector items; + vector translated_items; + int selectedItemIndex; + bool lighted; + Vec3f textColor; + bool leftControlled; + + public: + GraphicListBox(const std::string &containerName = "", const std::string &objName = ""); + void init(int x, int y, int w = defW, int h = defH, Vec3f textColor = GraphicComponent::customTextColor); + + int getItemCount() const { + return (int) items.size(); + } + string getItem(int index) const { + return items[index]; + } + int getSelectedItemIndex() const { + return selectedItemIndex; + } + string getSelectedItem() const { + return items[selectedItemIndex]; + } + GraphicButton *getButton1() { + return &graphButton1; + } + GraphicButton *getButton2() { + return &graphButton2; + } + bool getLighted() const { + return lighted; + } + void setLighted(bool lighted) { + this->lighted = lighted; + } + bool getLeftControlled() const { + return leftControlled; + } + void setLeftControlled(bool leftControlled); + Vec3f getTextColor() const { + return textColor; + } + void setTextColor(Vec3f color) { + this->textColor = color; + } + + void pushBackItem(string item, string translated_item = ""); + void setItems(const vector &items, const vector translated_items = vector()); + void setSelectedItemIndex(int index, bool errorOnMissing = true); + void setSelectedItem(string item, bool errorOnMissing = true); + void setEditable(bool editable); + + bool hasItem(string item) const; + + virtual void setX(int x); + virtual void setY(int y); + + virtual bool mouseMove(int x, int y); + virtual bool mouseClick(int x, int y, string advanceToItemStartingWith = ""); + + virtual const string &getTextNativeTranslation(); + }; + + // =========================================================== + // class GraphicMessageBox + // =========================================================== + typedef vector GraphicButtons; + class GraphicMessageBox : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + GraphicButtons buttons; + string header; + bool autoWordWrap; + + private: + void alignButtons(); + public: + GraphicMessageBox(const std::string &containerName = "", const std::string &objName = ""); + virtual ~GraphicMessageBox(); + void init(const string &button1Str, const string &button2Str, int newWidth = -1, int newHeight = -1); + void init(const string &button1Str, int newWidth = -1, int newHeight = -1); + void init(int newWidth = -1, int newHeight = -1); + void removeButtons(); + void addButton(const string &buttonStr, int width = -1, int height = -1); + + bool getAutoWordWrap() const { + return autoWordWrap; + } + void setAutoWordWrap(bool value) { + autoWordWrap = value; + } + + int getButtonCount() const { + return (int) buttons.size(); + } + GraphicButton *getButton(int index) { + return buttons[index]; + } + string getHeader() const { + return header; + } + + virtual void setX(int x); + virtual void setY(int y); + + void setHeader(string header) { + this->header = header; + } + + virtual bool mouseMove(int x, int y); + virtual bool mouseClick(int x, int y); + bool mouseClick(int x, int y, int &clickedButton); + }; + + // =========================================================== + // class GraphicLine + // =========================================================== + + class GraphicLine : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + bool horizontal; + + public: + GraphicLine(const std::string &containerName = "", const std::string &objName = ""); + void init(int x, int y, int w = defW, int h = defH); + bool getHorizontal() const { + return horizontal; + } + void setHorizontal(bool horizontal) { + this->horizontal = horizontal; + } + }; + + // =========================================================== + // class GraphicCheckBox + // =========================================================== + + class GraphicCheckBox : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + bool value; + bool lighted; + + public: + GraphicCheckBox(const std::string &containerName = "", const std::string &objName = ""); + void init(int x, int y, int w = defW, int h = defH); + bool getValue() const { + return value; + } + void setValue(bool value) { + this->value = value; + } + bool getLighted() const { + return lighted; + } + void setLighted(bool lighted) { + this->lighted = lighted; + } + virtual bool mouseMove(int x, int y); + virtual bool mouseClick(int x, int y); + }; + + // =========================================================== + // class GraphicScrollBar + // =========================================================== + + class GraphicScrollBar : public GraphicComponent { + public: + static const int defLength; + static const int defThickness; + + private: + bool activated; + bool lighted; + bool horizontal; + int elementCount; + int visibleSize; + int visibleStart; + + // position on component for renderer + int visibleCompPosStart; + int visibleCompPosEnd; + + public: + GraphicScrollBar(const std::string &containerName = "", const std::string &objName = ""); + void init(int x, int y, bool horizontal, int length = defLength, int thickness = defThickness); + virtual bool mouseDown(int x, int y); + virtual bool mouseMove(int x, int y); + virtual void mouseUp(int x, int y); + virtual bool mouseClick(int x, int y); + + + bool getHorizontal() const { + return horizontal; + } + int getLength() const; + void setLength(int length) { + horizontal ? setW(length) : setH(length); + } + //int getThickness() const; + + + bool getLighted() const { + return lighted; + } + void setLighted(bool lighted) { + this->lighted = lighted; + } + + int getElementCount() const { + return elementCount; + } + void setElementCount(int elementCount); + int getVisibleSize() const { + return visibleSize; + } + void setVisibleSize(int visibleSize); + int getVisibleStart() const { + return visibleStart; + } + int getVisibleEnd() const { + return visibleStart + visibleSize > elementCount - 1 ? elementCount - 1 : visibleStart + visibleSize - 1; + } + void setVisibleStart(int visibleStart); + + int getVisibleCompPosStart() const { + return visibleCompPosStart; + } + int getVisibleCompPosEnd() const { + return visibleCompPosEnd; + } + void arrangeComponents(vector &gcs); + }; + + // =========================================================== + // class PopupMenu + // =========================================================== + + class PopupMenu : public GraphicComponent { + public: + static const int defH; + static const int defW; + + private: + std::vector buttons; + string header; + + public: + PopupMenu(const std::string &containerName = "", const std::string &objName = ""); + virtual ~PopupMenu(); + void init(string menuHeader, std::vector menuItems); + + std::vector & getMenuItems() { + return buttons; + } + string getHeader() const { + return header; + } + + virtual void setX(int x); + virtual void setY(int y); + + void setHeader(string header) { + this->header = header; + } + + virtual bool mouseMove(int x, int y); + virtual bool mouseClick(int x, int y); + std::pair mouseClickedMenuItem(int x, int y); + }; + + } +}//end namespace #endif diff --git a/source/glest_game/facilities/game_util.cpp b/source/glest_game/facilities/game_util.cpp index bb0252ec6..d7e4c1230 100644 --- a/source/glest_game/facilities/game_util.cpp +++ b/source/glest_game/facilities/game_util.cpp @@ -37,108 +37,110 @@ using namespace Shared::Util; using namespace Shared::Platform; namespace Glest { - namespace Game { + namespace Game { - const char *mailString = " https://github.com/ZetaGlest"; + const char *mailString = " https://github.com/ZetaGlest"; -// !! Use minor versions !! Only major and minor version control compatibility! -// typical version numbers look like this: v3.13-beta1.0 v3.12-dev v3.12.1 -// don't forget to update file: source/version.txt - const string glestVersionString = "v0.8.01-dev"; - const string lastCompatibleSaveGameVersionString = "v3.11.1"; + // !! Use minor versions !! Only major and minor version control compatibility! + // typical version numbers look like this: v3.13-beta1.0 v3.12-dev v3.12.1 + // don't forget to update file: source/version.txt + const string glestVersionString = "v0.8.01-dev"; + const string lastCompatibleSaveGameVersionString = "v3.11.1"; - string getCrashDumpFileName() {return "zetaglest" + glestVersionString + ".dmp";} - string getPlatformTypeNameString() { - static string platform; - if (platform == "") { + string getCrashDumpFileName() { + return "zetaglest" + glestVersionString + ".dmp"; + } + string getPlatformTypeNameString() { + static string platform; + if (platform == "") { #if defined(WIN32) # if defined(__MINGW32__) - platform = "W-Ming32"; + platform = "W-Ming32"; # else - platform = "Windows"; + platform = "Windows"; # endif #elif defined(__FreeBSD__) - platform = "FreeBSD"; + platform = "FreeBSD"; #elif defined(__NetBSD__) - platform = "NetBSD"; + platform = "NetBSD"; #elif defined(__OpenBSD__) - platform = "OpenBSD"; + platform = "OpenBSD"; #elif defined(__APPLE__) - platform = "MacOS"; + platform = "MacOS"; #elif defined(_AIX) - platform = "AIX"; + platform = "AIX"; #elif defined(__ANDROID__) - platform = "Android"; + platform = "Android"; #elif defined(__BEOS__) - platform = "BEOS"; + platform = "BEOS"; #elif defined(__gnu_linux__) - platform = "Linux"; + platform = "Linux"; #elif defined(__sun) - platform = "Solaris"; + platform = "Solaris"; #elif defined(__GNUC__) # if defined(__MINGW32__) - platform = "L-Ming32"; + platform = "L-Ming32"; # else - platform = "GNU"; + platform = "GNU"; # endif #else - platform = "???"; + platform = "???"; #endif - } - return platform; - } + } + return platform; + } - string getPlatformArchTypeNameString() { - static string platform; - if (platform == "") { + string getPlatformArchTypeNameString() { + static string platform; + if (platform == "") { #if defined(_M_X64) || defined(_M_AMD64) || defined(__x86_64__) || defined(_WIN64) - platform = "-X64"; + platform = "-X64"; #elif defined(_M_ALPHA) || defined(__alpha__) - platform = "-ALPHA"; + platform = "-ALPHA"; #elif defined(_M_IA64) || defined(__ia64__) - platform = "-IA64"; + platform = "-IA64"; #elif defined(_M_MRX000) || defined(__mips__) - platform = "-MIPS"; + platform = "-MIPS"; #elif defined(_M_PPC) || defined(__powerpc__) - platform = "-POWERPC"; + platform = "-POWERPC"; #elif defined(__sparc__) - platform = "-SPARC"; + platform = "-SPARC"; #elif defined(_M_ARM_FP) || defined(__arm__) || defined(_M_ARM) - platform = "-ARM"; + platform = "-ARM"; #endif - } - return platform; - } + } + return platform; + } - string getPlatformNameString() { - static string platform; - if (platform == "") { - platform = - getPlatformTypeNameString() + getPlatformArchTypeNameString(); - } - return platform; - } + string getPlatformNameString() { + static string platform; + if (platform == "") { + platform = + getPlatformTypeNameString() + getPlatformArchTypeNameString(); + } + return platform; + } - string getCompilerNameString() { - static string version = ""; - if (version == "") { + string getCompilerNameString() { + static string version = ""; + if (version == "") { #if defined(WIN32) && defined(_MSC_VER) - version = "VC++: " + intToStr(_MSC_VER); + version = "VC++: " + intToStr(_MSC_VER); #elif defined(__clang__) - version = - "Clang: " + intToStr(__clang_major__) + "." + - intToStr(__clang_minor__) + "." + - intToStr(__clang_patchlevel__); + version = + "Clang: " + intToStr(__clang_major__) + "." + + intToStr(__clang_minor__) + "." + + intToStr(__clang_patchlevel__); #elif defined(__GNUC__) # if defined(__GNUC__) @@ -151,304 +153,304 @@ namespace Glest { + __GNUC_MINOR__ * 100) # endif # endif - version = "GNUC"; + version = "GNUC"; # if defined(__MINGW32__) - version += "-MINGW"; + version += "-MINGW"; # endif - version += ": " + intToStr(__GNUC_VERSION__); + version += ": " + intToStr(__GNUC_VERSION__); #else - version = "???"; + version = "???"; #endif #if defined(DEBUG) || defined(_DEBUG) - version += " [DEBUG]"; + version += " [DEBUG]"; #endif #if defined(_M_X64) || defined(_M_IA64) || defined(_M_AMD64) || defined(__x86_64__) || defined(_WIN64) - version += " [64bit]"; + version += " [64bit]"; #endif - } - return version; - } + } + return version; + } - string getNetworkVersionString() { - static string version = ""; - if (version == "") { - version = - glestVersionString + "-" + getCompilerNameString() + "-" + - getCompileDateTime(); - } - return version; - } + string getNetworkVersionString() { + static string version = ""; + if (version == "") { + version = + glestVersionString + "-" + getCompilerNameString() + "-" + + getCompileDateTime(); + } + return version; + } - string getNetworkVersionGITString() { - static string version = ""; - if (version == "") { - version = - glestVersionString + getCompilerNameString(); - } - return version; - } + string getNetworkVersionGITString() { + static string version = ""; + if (version == "") { + version = + glestVersionString + getCompilerNameString(); + } + return version; + } - string getCompileDateTime() { - static string result = ""; -// if(result == "") { -// result = string(__DATE__) + " " + string(__TIME__); -// } - return result; - } + string getCompileDateTime() { + static string result = ""; + // if(result == "") { + // result = string(__DATE__) + " " + string(__TIME__); + // } + return result; + } - string getNetworkPlatformFreeVersionString() { - return glestVersionString; - } + string getNetworkPlatformFreeVersionString() { + return glestVersionString; + } - string getAboutString1(int i) { - //case 1: return "Built: " + string(__DATE__) + " " + GIT_Rev; - switch (i) { - case 0: - return "ZetaGlest " + glestVersionString + " (" + - "Shared Library " + sharedLibVersionString + ")"; - case 1: - return glestVersionString; - case 2: - return "Copyright 2001-2010 The Glest Team"; - case 3: - return "Copyright 2018 The ZetaGlest Team"; - } - return ""; - } + string getAboutString1(int i) { + //case 1: return "Built: " + string(__DATE__) + " " + GIT_Rev; + switch (i) { + case 0: + return "ZetaGlest " + glestVersionString + " (" + + "Shared Library " + sharedLibVersionString + ")"; + case 1: + return glestVersionString; + case 2: + return "Copyright 2001-2010 The Glest Team"; + case 3: + return "Copyright 2018 The ZetaGlest Team"; + } + return ""; + } - string getAboutString2(int i) { - switch (i) { - case 0: - return "Web: https://github.com/ZetaGlest"; - case 1: - return "Bug reports: " + string(mailString); - case 2: - return "IRC: irc://irc.freenode.net/zegaglest-dev"; - } - return ""; - } + string getAboutString2(int i) { + switch (i) { + case 0: + return "Web: https://github.com/ZetaGlest"; + case 1: + return "Bug reports: " + string(mailString); + case 2: + return "IRC: irc://irc.freenode.net/zegaglest-dev"; + } + return ""; + } - string getTeammateName(int i) { - switch (i) { - case 0: - return "Martiño Figueroa"; - //case 0: return "Martino Figueroa"; - case 1: - return "José Luis González"; - //case 1: return "Jose Luis Gonzalez"; - case 2: - return "Tucho Fernández"; - //case 2: return "Tucho Fernandez"; - case 3: - return "José Zanni"; - //case 3: return "Jose Zanni"; - case 4: - return "Félix Menéndez"; - //case 4: return "Felix Menendez"; - case 5: - return "Marcos Caruncho"; - case 6: - return "Matthias Braun"; - case 7: - return "Titus Tscharntke"; - case 8: - return "Mark Vejvoda"; - } - return ""; - } + string getTeammateName(int i) { + switch (i) { + case 0: + return "Martiño Figueroa"; + //case 0: return "Martino Figueroa"; + case 1: + return "José Luis González"; + //case 1: return "Jose Luis Gonzalez"; + case 2: + return "Tucho Fernández"; + //case 2: return "Tucho Fernandez"; + case 3: + return "José Zanni"; + //case 3: return "Jose Zanni"; + case 4: + return "Félix Menéndez"; + //case 4: return "Felix Menendez"; + case 5: + return "Marcos Caruncho"; + case 6: + return "Matthias Braun"; + case 7: + return "Titus Tscharntke"; + case 8: + return "Mark Vejvoda"; + } + return ""; + } - string getTeammateRole(int i) { - Lang & lang = Lang::getInstance(); + string getTeammateRole(int i) { + Lang & lang = Lang::getInstance(); - switch (i) { - case 0: - return lang.getString("Programming"); - case 1: - return lang.getString("SoundAndMusic"); - case 2: - return lang.getString("3dAnd2dArt"); - case 3: - return lang.getString("2dArtAndWeb"); - case 4: - return lang.getString("Animation"); - case 5: - return lang.getString("3dArt"); - case 6: - return lang.getString("LinuxPort"); - case 7: - return lang.getString("Megaglest3d2dProgramming"); - case 8: - return lang.getString("MegaglestProgramming"); - } - return ""; - } + switch (i) { + case 0: + return lang.getString("Programming"); + case 1: + return lang.getString("SoundAndMusic"); + case 2: + return lang.getString("3dAnd2dArt"); + case 3: + return lang.getString("2dArtAndWeb"); + case 4: + return lang.getString("Animation"); + case 5: + return lang.getString("3dArt"); + case 6: + return lang.getString("LinuxPort"); + case 7: + return lang.getString("Megaglest3d2dProgramming"); + case 8: + return lang.getString("MegaglestProgramming"); + } + return ""; + } - string formatString(string str) { - string outStr = str; + string formatString(string str) { + string outStr = str; - if (!outStr.empty()) { - outStr[0] = toupper(outStr[0]); - } + if (!outStr.empty()) { + outStr[0] = toupper(outStr[0]); + } - bool afterSeparator = false; - for (int i = 0; i < (int) str.size(); ++i) { - if (outStr[i] == '_') { - outStr[i] = ' '; - } else if (afterSeparator) { - outStr[i] = toupper(outStr[i]); - afterSeparator = false; - } - if (outStr[i] == '\n' || outStr[i] == '(' || outStr[i] == ' ') { - afterSeparator = true; - } - } - return outStr; - } + bool afterSeparator = false; + for (int i = 0; i < (int) str.size(); ++i) { + if (outStr[i] == '_') { + outStr[i] = ' '; + } else if (afterSeparator) { + outStr[i] = toupper(outStr[i]); + afterSeparator = false; + } + if (outStr[i] == '\n' || outStr[i] == '(' || outStr[i] == ' ') { + afterSeparator = true; + } + } + return outStr; + } - string getGameCustomCoreDataPath(string originalBasePath, - string uniqueFilePath) { - // original file path setup - if (originalBasePath != "") { - endPathWithSlash(originalBasePath); - } - // + string getGameCustomCoreDataPath(string originalBasePath, + string uniqueFilePath) { + // original file path setup + if (originalBasePath != "") { + endPathWithSlash(originalBasePath); + } + // - // mydata user data override - Config & config = Config::getInstance(); - string data_path = config.getString("UserData_Root", ""); - if (data_path != "") { - endPathWithSlash(data_path); - } - // + // mydata user data override + Config & config = Config::getInstance(); + string data_path = config.getString("UserData_Root", ""); + if (data_path != "") { + endPathWithSlash(data_path); + } + // - // if set this is the current active mod - string custom_mod_path = - config. - getCustomRuntimeProperty(Config::ACTIVE_MOD_PROPERTY_NAME); - if (custom_mod_path != "") { - endPathWithSlash(custom_mod_path); - } - // + // if set this is the current active mod + string custom_mod_path = + config. + getCustomRuntimeProperty(Config::ACTIVE_MOD_PROPERTY_NAME); + if (custom_mod_path != "") { + endPathWithSlash(custom_mod_path); + } + // - // decide which file to use - string result = ""; + // decide which file to use + string result = ""; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf("Looking for [%s] in\n#1: [%s]\n#2: [%s]\n#3: [%s]\n", - uniqueFilePath.c_str(), custom_mod_path.c_str(), - data_path.c_str(), originalBasePath.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Looking for [%s] in\n#1: [%s]\n#2: [%s]\n#3: [%s]\n", + uniqueFilePath.c_str(), custom_mod_path.c_str(), + data_path.c_str(), originalBasePath.c_str()); - if (custom_mod_path != "" && - (uniqueFilePath == "" - || fileExists(custom_mod_path + uniqueFilePath) == true)) { - result = custom_mod_path + uniqueFilePath; - } else if (data_path != "" && - (uniqueFilePath == "" - || fileExists(data_path + uniqueFilePath) == true)) { - result = data_path + uniqueFilePath; - } else { - result = originalBasePath + uniqueFilePath; - } + if (custom_mod_path != "" && + (uniqueFilePath == "" + || fileExists(custom_mod_path + uniqueFilePath) == true)) { + result = custom_mod_path + uniqueFilePath; + } else if (data_path != "" && + (uniqueFilePath == "" + || fileExists(data_path + uniqueFilePath) == true)) { + result = data_path + uniqueFilePath; + } else { + result = originalBasePath + uniqueFilePath; + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf("result [%s]\n", result.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("result [%s]\n", result.c_str()); - return result; - } + return result; + } - string getGameReadWritePath(const string & lookupKey) { - string path = ""; + string getGameReadWritePath(const string & lookupKey) { + string path = ""; - if (lookupKey != "") { - std::map < string, string > &pathCache = - CacheManager::getCachedItem < std::map < string, - string > >(GameConstants::pathCacheLookupKey); - std::map < string, string >::const_iterator iterFind = - pathCache.find(lookupKey); - if (iterFind != pathCache.end()) { - path = iterFind->second; + if (lookupKey != "") { + std::map < string, string > &pathCache = + CacheManager::getCachedItem < std::map < string, + string > >(GameConstants::pathCacheLookupKey); + std::map < string, string >::const_iterator iterFind = + pathCache.find(lookupKey); + if (iterFind != pathCache.end()) { + path = iterFind->second; - if (path != "" && EndsWith(path, "/") == false - && EndsWith(path, "\\") == false) { - path += "/"; - } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for [%s] files [%s]\n",__FILE__,__FUNCTION__,__LINE__,lookupKey.c_str(),path.c_str()); - } - } + if (path != "" && EndsWith(path, "/") == false + && EndsWith(path, "\\") == false) { + path += "/"; + } + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for [%s] files [%s]\n",__FILE__,__FUNCTION__,__LINE__,lookupKey.c_str(),path.c_str()); + } + } - if (path == "") { - path = safeCharPtrCopy(getenv("GLESTHOME"), 8095); - if (path != "" && EndsWith(path, "/") == false - && EndsWith(path, "\\") == false) { - path += "/"; - } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); - } + if (path == "") { + path = safeCharPtrCopy(getenv("GLESTHOME"), 8095); + if (path != "" && EndsWith(path, "/") == false + && EndsWith(path, "\\") == false) { + path += "/"; + } + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] path to be used for read/write files [%s]\n",__FILE__,__FUNCTION__,__LINE__,path.c_str()); + } - return path; - } + return path; + } - void initSpecialStrings() { - getCrashDumpFileName(); - getPlatformNameString(); - getCompilerNameString(); - getNetworkVersionString(); - getNetworkVersionGITString(); - getNetworkPlatformFreeVersionString(); - getCompileDateTime(); - } + void initSpecialStrings() { + getCrashDumpFileName(); + getPlatformNameString(); + getCompilerNameString(); + getNetworkVersionString(); + getNetworkVersionGITString(); + getNetworkPlatformFreeVersionString(); + getCompileDateTime(); + } - bool upgradeFilesInTemp() { - // Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != - "") { - tempFilePath = - getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } else { - Config & config = Config::getInstance(); - string userData = config.getString("UserData_Root", ""); - if (userData != "") { - endPathWithSlash(userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf("Temp files path [%s]\n", tempFilePath.c_str()); + bool upgradeFilesInTemp() { + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + Config & config = Config::getInstance(); + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); - // Move all files into binary folder - bool anyFailures = false; - vector < string > fileList = - getFolderTreeContentsListRecursively(tempFilePath, "", false, - NULL); - for (unsigned int index = 0; index < fileList.size(); ++index) { - string fileName = fileList[index]; - string newFileName = - Properties::getApplicationPath() + - extractFileFromDirectoryPath(fileName); - bool result = renameFile(fileName, newFileName); - if (result == false) { - printf("FAILED Rename: [%s] to [%s] result = %d errno = %d\n", - fileName.c_str(), newFileName.c_str(), result, errno); + // Move all files into binary folder + bool anyFailures = false; + vector < string > fileList = + getFolderTreeContentsListRecursively(tempFilePath, "", false, + NULL); + for (unsigned int index = 0; index < fileList.size(); ++index) { + string fileName = fileList[index]; + string newFileName = + Properties::getApplicationPath() + + extractFileFromDirectoryPath(fileName); + bool result = renameFile(fileName, newFileName); + if (result == false) { + printf("FAILED Rename: [%s] to [%s] result = %d errno = %d\n", + fileName.c_str(), newFileName.c_str(), result, errno); - anyFailures = true; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf("Rename: [%s] to [%s] result = %d\n", fileName.c_str(), - newFileName.c_str(), result); - } + anyFailures = true; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Rename: [%s] to [%s] result = %d\n", fileName.c_str(), + newFileName.c_str(), result); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf("Successfully updated!\n"); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Successfully updated!\n"); - return (fileList.size() > 0 && anyFailures == false); - } + return (fileList.size() > 0 && anyFailures == false); + } - } + } } //end namespace diff --git a/source/glest_game/facilities/game_util.h b/source/glest_game/facilities/game_util.h index 6bf25bebb..b46f64de8 100644 --- a/source/glest_game/facilities/game_util.h +++ b/source/glest_game/facilities/game_util.h @@ -37,35 +37,36 @@ using std::string; using Shared::Util::sharedLibVersionString; namespace Glest { - namespace Game { + namespace Game { - extern const char *mailString; - extern const string glestVersionString; - extern const string lastCompatibleSaveGameVersionString; - extern const string networkVersionString; + extern const char *mailString; + extern const string glestVersionString; + extern const string lastCompatibleSaveGameVersionString; + extern const string networkVersionString; - void initSpecialStrings(); - string getCrashDumpFileName(); - string getPlatformTypeNameString(); - string getPlatformArchTypeNameString(); - string getPlatformNameString(); - string getCompilerNameString(); - string getNetworkVersionString(); - string getNetworkVersionGITString(); - string getNetworkPlatformFreeVersionString(); - string getAboutString1(int i); - string getAboutString2(int i); - string getTeammateName(int i); - string getTeammateRole(int i); - string getCompileDateTime(); + void initSpecialStrings(); + string getCrashDumpFileName(); + string getPlatformTypeNameString(); + string getPlatformArchTypeNameString(); + string getPlatformNameString(); + string getCompilerNameString(); + string getNetworkVersionString(); + string getNetworkVersionGITString(); + string getNetworkPlatformFreeVersionString(); + string getAboutString1(int i); + string getAboutString2(int i); + string getTeammateName(int i); + string getTeammateRole(int i); + string getCompileDateTime(); - string formatString(string str); + string formatString(string str); - string getGameReadWritePath(const string & lookupKey = ""); - string getGameCustomCoreDataPath(string originalBasePath, - string uniqueFilePath); + string getGameReadWritePath(const string & lookupKey = ""); + string getGameCustomCoreDataPath(string originalBasePath, + string uniqueFilePath); - bool upgradeFilesInTemp(); + bool upgradeFilesInTemp(); -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/facilities/logger.cpp b/source/glest_game/facilities/logger.cpp index b93e15493..b92e02c59 100644 --- a/source/glest_game/facilities/logger.cpp +++ b/source/glest_game/facilities/logger.cpp @@ -28,327 +28,320 @@ using namespace std; using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Logger -// ===================================================== + // ===================================================== + // class Logger + // ===================================================== -const int Logger::logLineCount= 15; + const int Logger::logLineCount = 15; -// ===================== PUBLIC ======================== + // ===================== PUBLIC ======================== -Logger::Logger() : buttonCancel("Logger","buttonCancel"), buttonNextHint("Logger","buttonCancel") { - progress = 0; - string logs_path = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey); - if(logs_path != "") { - fileName= logs_path + "log.txt"; - } - else { - string userData = Config::getInstance().getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - fileName= userData + "log.txt"; - } - loadingTexture=NULL; - gameHintToShow=""; - showProgressBar = false; + Logger::Logger() : buttonCancel("Logger", "buttonCancel"), buttonNextHint("Logger", "buttonCancel") { + progress = 0; + string logs_path = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey); + if (logs_path != "") { + fileName = logs_path + "log.txt"; + } else { + string userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + fileName = userData + "log.txt"; + } + loadingTexture = NULL; + gameHintToShow = ""; + showProgressBar = false; - displayColor=Vec4f(1.f,1.f,1.f,0.1f); + displayColor = Vec4f(1.f, 1.f, 1.f, 0.1f); - cancelSelected = false; - buttonCancel.setEnabled(false); + cancelSelected = false; + buttonCancel.setEnabled(false); - buttonNextHint.setEnabled(false); -} + buttonNextHint.setEnabled(false); + } -Logger::~Logger() { -} + Logger::~Logger() { + } -Logger & Logger::getInstance() { - static Logger logger; - return logger; -} + Logger & Logger::getInstance() { + static Logger logger; + return logger; + } -void Logger::add(const string str, bool renderScreen, const string statusText) { + void Logger::add(const string str, bool renderScreen, const string statusText) { #ifdef WIN32 - FILE *f= _wfopen(utf8_decode(fileName).c_str(), L"at+"); + FILE *f = _wfopen(utf8_decode(fileName).c_str(), L"at+"); #else - FILE *f = fopen(fileName.c_str(), "at+"); + FILE *f = fopen(fileName.c_str(), "at+"); #endif - if(f != NULL){ - fprintf(f, "%s\n", str.c_str()); - fclose(f); - } - this->current= str; - this->statusText = statusText; + if (f != NULL) { + fprintf(f, "%s\n", str.c_str()); + fclose(f); + } + this->current = str; + this->statusText = statusText; - if(renderScreen == true && GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - renderLoadingScreen(); - } -} + if (renderScreen == true && GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + renderLoadingScreen(); + } + } -void Logger::clear() { - string s = "Log file\n"; + void Logger::clear() { + string s = "Log file\n"; #ifdef WIN32 - FILE *f= _wfopen(utf8_decode(fileName).c_str(), L"wt+"); + FILE *f = _wfopen(utf8_decode(fileName).c_str(), L"wt+"); #else - FILE *f= fopen(fileName.c_str(), "wt+"); + FILE *f = fopen(fileName.c_str(), "wt+"); #endif - if(f == NULL){ - throw megaglest_runtime_error("Error opening log file" + fileName); - } + if (f == NULL) { + throw megaglest_runtime_error("Error opening log file" + fileName); + } - fprintf(f, "%s", s.c_str()); - fprintf(f, "\n"); + fprintf(f, "%s", s.c_str()); + fprintf(f, "\n"); - fclose(f); -} - -void Logger::loadLoadingScreen(string filepath) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if(filepath == "") { - loadingTexture = NULL; - } - else { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] filepath = [%s]\n",__FILE__,__FUNCTION__,__LINE__,filepath.c_str()); - loadingTexture = Renderer::findTexture(filepath); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - - Lang &lang = Lang::getInstance(); - buttonCancel.setText(lang.getString("Cancel")); -} - -void Logger::loadGameHints(string filePathEnglish,string filePathTranslation,bool clearList) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if((filePathEnglish == "") || (filePathTranslation == "")) { - return; - } - else { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] filePathEnglish = [%s]\n filePathTranslation = [%s]\n",__FILE__,__FUNCTION__,__LINE__,filePathEnglish.c_str(),filePathTranslation.c_str()); - gameHints.load(filePathEnglish,clearList); - gameHintsTranslation.load(filePathTranslation,clearList); - showNextHint(); - - Lang &lang = Lang::getInstance(); - buttonNextHint.setText(lang.getString("ShowNextHint","",true)); - buttonCancel.setText(lang.getString("Cancel")); - - GraphicComponent::applyAllCustomProperties("Loading"); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } -} - -void Logger::showNextHint() { - string key=gameHints.getRandomKey(true); - string tmpString=gameHintsTranslation.getString(key,""); - if(tmpString!=""){ - gameHintToShow=tmpString; - } - else { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] key [%s] not found for [%s] hint translation\n",__FILE__,__FUNCTION__,__LINE__,key.c_str(),Lang::getInstance().getLanguage().c_str()); - tmpString=gameHints.getString(key,""); - if(tmpString!=""){ - gameHintToShow=tmpString; - } - else { - gameHintToShow="Problems to resolve hint key '"+key+"'"; - } - } - replaceAll(gameHintToShow, "\\n", "\n"); - - Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); - - vector > mergedKeySettings = configKeys.getMergedProperties(); - for(unsigned int j = 0; j < mergedKeySettings.size(); ++j) { - pair &property = mergedKeySettings[j]; - replaceAll(gameHintToShow, "#"+property.first+"#", property.second); - } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - -void Logger::clearHints() { - gameHintToShow=""; - gameHints.clear(); - gameHintsTranslation.clear(); -} - -void Logger::handleMouseClick(int x, int y) { - if(buttonCancel.getEnabled() == true) { - if(buttonCancel.mouseClick(x, y)) { - cancelSelected = true; - } - } - if(buttonNextHint.getEnabled() == true && buttonNextHint.mouseClick(x,y) == true) { - showNextHint(); - //buttonNextHint.setLighted(false); - SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - CoreData &coreData= CoreData::getInstance(); - soundRenderer.playFx(coreData.getClickSoundC()); - } -} - -// ==================== PRIVATE ==================== - -void Logger::renderLoadingScreen() { - - Renderer &renderer= Renderer::getInstance(); - CoreData &coreData= CoreData::getInstance(); - const Metrics &metrics= Metrics::getInstance(); - - //3d - //renderer.reset3d(); - //renderer.clearZBuffer(); - - renderer.reset2d(); - renderer.clearBuffers(); - if(loadingTexture == NULL) { - renderer.renderBackground(CoreData::getInstance().getBackgroundTexture()); - } - else { - renderer.renderBackground(loadingTexture); - } - - if(showProgressBar == true) { - if(Renderer::renderText3DEnabled) { - renderer.renderProgressBar3D( - progress, - metrics.getVirtualW() / 4, - 59 * metrics.getVirtualH() / 100, - coreData.getDisplayFontSmall3D(), - 500,""); // no string here, because it has to be language specific and does not give much information - } - else { - renderer.renderProgressBar( - progress, - metrics.getVirtualW() / 4, - 59 * metrics.getVirtualH() / 100, - coreData.getDisplayFontSmall(), - 500,""); // no string here, because it has to be language specific and does not give much information - } - } - - int xLocation = metrics.getVirtualW() / 4; - if(Renderer::renderText3DEnabled) { - - renderer.renderTextShadow3D( - state, coreData.getMenuFontBig3D(), displayColor, - xLocation, - 65 * metrics.getVirtualH() / 100, false); - - renderer.renderTextShadow3D( - current, coreData.getMenuFontNormal3D(), displayColor, - xLocation, - 62 * metrics.getVirtualH() / 100, false); - - if(this->statusText != "") { - renderer.renderTextShadow3D( - this->statusText, coreData.getMenuFontNormal3D(), displayColor, - xLocation, - 56 * metrics.getVirtualH() / 100, false); - } - } - else { - renderer.renderTextShadow( - state, coreData.getMenuFontBig(), displayColor, - xLocation, - 65 * metrics.getVirtualH() / 100, false); - - renderer.renderTextShadow( - current, coreData.getMenuFontNormal(), displayColor, - xLocation, - 62 * metrics.getVirtualH() / 100, false); - - if(this->statusText != "") { - renderer.renderTextShadow( - this->statusText, coreData.getMenuFontNormal(), displayColor, - xLocation, - 56 * metrics.getVirtualH() / 100, false); - } - } - - if(gameHintToShow != "") { - Lang &lang = Lang::getInstance(); - string hintText = lang.getString("Hint","",true); - char szBuf[8096]=""; - snprintf(szBuf,8096,hintText.c_str(),gameHintToShow.c_str()); - hintText = szBuf; - - if(Renderer::renderText3DEnabled) { - int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig3D()->getMetrics()->getTextWidth(hintText) / 2); - - renderer.renderTextShadow3D( - hintText, coreData.getMenuFontBig3D(), displayColor, - //xLocation*1.5f, - xLocationHint, - 90 * metrics.getVirtualH() / 100, false); - } - else { - int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig()->getMetrics()->getTextWidth(hintText) / 2); - - renderer.renderTextShadow( - hintText, coreData.getMenuFontBig(), displayColor, - //xLocation*1.5f, - xLocationHint, - 90 * metrics.getVirtualH() / 100, false); - - } - //Show next Hint - if(buttonNextHint.getEnabled() == false) { - buttonNextHint.init((metrics.getVirtualW() / 2) - (175 / 2), 90 * metrics.getVirtualH() / 100 + 20, 175); - buttonNextHint.setText(lang.getString("ShowNextHint","",true)); - buttonNextHint.setEnabled(true); - buttonNextHint.setVisible(true); - buttonNextHint.setEditable(true); + fclose(f); } - renderer.renderButton(&buttonNextHint); + void Logger::loadLoadingScreen(string filepath) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); -/* - if(Renderer::renderText3DEnabled) { - int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig3D()->getMetrics()->getTextWidth(hintText) / 2); + if (filepath == "") { + loadingTexture = NULL; + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] filepath = [%s]\n", __FILE__, __FUNCTION__, __LINE__, filepath.c_str()); + loadingTexture = Renderer::findTexture(filepath); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } - renderer.renderText3D( - lang.getString("ShowNextHint","",true), coreData.getMenuFontNormal3D(), nextHintTitleColor, - //xLocation*1.5f, - xLocationHint, - 93 * metrics.getVirtualH() / 100, false); + Lang &lang = Lang::getInstance(); + buttonCancel.setText(lang.getString("Cancel")); } - else { - int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig()->getMetrics()->getTextWidth(hintText) / 2); - renderer.renderText( - lang.getString("ShowNextHint","",true), coreData.getMenuFontNormal(), nextHintTitleColor, - //xLocation*1.5f, - xLocationHint, - 93 * metrics.getVirtualH() / 100, false); + void Logger::loadGameHints(string filePathEnglish, string filePathTranslation, bool clearList) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if ((filePathEnglish == "") || (filePathTranslation == "")) { + return; + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] filePathEnglish = [%s]\n filePathTranslation = [%s]\n", __FILE__, __FUNCTION__, __LINE__, filePathEnglish.c_str(), filePathTranslation.c_str()); + gameHints.load(filePathEnglish, clearList); + gameHintsTranslation.load(filePathTranslation, clearList); + showNextHint(); + + Lang &lang = Lang::getInstance(); + buttonNextHint.setText(lang.getString("ShowNextHint", "", true)); + buttonCancel.setText(lang.getString("Cancel")); + + GraphicComponent::applyAllCustomProperties("Loading"); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + } + + void Logger::showNextHint() { + string key = gameHints.getRandomKey(true); + string tmpString = gameHintsTranslation.getString(key, ""); + if (tmpString != "") { + gameHintToShow = tmpString; + } else { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] key [%s] not found for [%s] hint translation\n", __FILE__, __FUNCTION__, __LINE__, key.c_str(), Lang::getInstance().getLanguage().c_str()); + tmpString = gameHints.getString(key, ""); + if (tmpString != "") { + gameHintToShow = tmpString; + } else { + gameHintToShow = "Problems to resolve hint key '" + key + "'"; + } + } + replaceAll(gameHintToShow, "\\n", "\n"); + + Config &configKeys = Config::getInstance(std::pair(cfgMainKeys, cfgUserKeys)); + + vector > mergedKeySettings = configKeys.getMergedProperties(); + for (unsigned int j = 0; j < mergedKeySettings.size(); ++j) { + pair &property = mergedKeySettings[j]; + replaceAll(gameHintToShow, "#" + property.first + "#", property.second); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + void Logger::clearHints() { + gameHintToShow = ""; + gameHints.clear(); + gameHintsTranslation.clear(); + } + + void Logger::handleMouseClick(int x, int y) { + if (buttonCancel.getEnabled() == true) { + if (buttonCancel.mouseClick(x, y)) { + cancelSelected = true; + } + } + if (buttonNextHint.getEnabled() == true && buttonNextHint.mouseClick(x, y) == true) { + showNextHint(); + //buttonNextHint.setLighted(false); + SoundRenderer &soundRenderer = SoundRenderer::getInstance(); + CoreData &coreData = CoreData::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + } + } + + // ==================== PRIVATE ==================== + + void Logger::renderLoadingScreen() { + + Renderer &renderer = Renderer::getInstance(); + CoreData &coreData = CoreData::getInstance(); + const Metrics &metrics = Metrics::getInstance(); + + //3d + //renderer.reset3d(); + //renderer.clearZBuffer(); + + renderer.reset2d(); + renderer.clearBuffers(); + if (loadingTexture == NULL) { + renderer.renderBackground(CoreData::getInstance().getBackgroundTexture()); + } else { + renderer.renderBackground(loadingTexture); + } + + if (showProgressBar == true) { + if (Renderer::renderText3DEnabled) { + renderer.renderProgressBar3D( + progress, + metrics.getVirtualW() / 4, + 59 * metrics.getVirtualH() / 100, + coreData.getDisplayFontSmall3D(), + 500, ""); // no string here, because it has to be language specific and does not give much information + } else { + renderer.renderProgressBar( + progress, + metrics.getVirtualW() / 4, + 59 * metrics.getVirtualH() / 100, + coreData.getDisplayFontSmall(), + 500, ""); // no string here, because it has to be language specific and does not give much information + } + } + + int xLocation = metrics.getVirtualW() / 4; + if (Renderer::renderText3DEnabled) { + + renderer.renderTextShadow3D( + state, coreData.getMenuFontBig3D(), displayColor, + xLocation, + 65 * metrics.getVirtualH() / 100, false); + + renderer.renderTextShadow3D( + current, coreData.getMenuFontNormal3D(), displayColor, + xLocation, + 62 * metrics.getVirtualH() / 100, false); + + if (this->statusText != "") { + renderer.renderTextShadow3D( + this->statusText, coreData.getMenuFontNormal3D(), displayColor, + xLocation, + 56 * metrics.getVirtualH() / 100, false); + } + } else { + renderer.renderTextShadow( + state, coreData.getMenuFontBig(), displayColor, + xLocation, + 65 * metrics.getVirtualH() / 100, false); + + renderer.renderTextShadow( + current, coreData.getMenuFontNormal(), displayColor, + xLocation, + 62 * metrics.getVirtualH() / 100, false); + + if (this->statusText != "") { + renderer.renderTextShadow( + this->statusText, coreData.getMenuFontNormal(), displayColor, + xLocation, + 56 * metrics.getVirtualH() / 100, false); + } + } + + if (gameHintToShow != "") { + Lang &lang = Lang::getInstance(); + string hintText = lang.getString("Hint", "", true); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, hintText.c_str(), gameHintToShow.c_str()); + hintText = szBuf; + + if (Renderer::renderText3DEnabled) { + int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig3D()->getMetrics()->getTextWidth(hintText) / 2); + + renderer.renderTextShadow3D( + hintText, coreData.getMenuFontBig3D(), displayColor, + //xLocation*1.5f, + xLocationHint, + 90 * metrics.getVirtualH() / 100, false); + } else { + int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig()->getMetrics()->getTextWidth(hintText) / 2); + + renderer.renderTextShadow( + hintText, coreData.getMenuFontBig(), displayColor, + //xLocation*1.5f, + xLocationHint, + 90 * metrics.getVirtualH() / 100, false); + + } + //Show next Hint + if (buttonNextHint.getEnabled() == false) { + buttonNextHint.init((metrics.getVirtualW() / 2) - (175 / 2), 90 * metrics.getVirtualH() / 100 + 20, 175); + buttonNextHint.setText(lang.getString("ShowNextHint", "", true)); + buttonNextHint.setEnabled(true); + buttonNextHint.setVisible(true); + buttonNextHint.setEditable(true); + } + + renderer.renderButton(&buttonNextHint); + + /* + if(Renderer::renderText3DEnabled) { + int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig3D()->getMetrics()->getTextWidth(hintText) / 2); + + renderer.renderText3D( + lang.getString("ShowNextHint","",true), coreData.getMenuFontNormal3D(), nextHintTitleColor, + //xLocation*1.5f, + xLocationHint, + 93 * metrics.getVirtualH() / 100, false); + } + else { + int xLocationHint = (metrics.getVirtualW() / 2) - (coreData.getMenuFontBig()->getMetrics()->getTextWidth(hintText) / 2); + + renderer.renderText( + lang.getString("ShowNextHint","",true), coreData.getMenuFontNormal(), nextHintTitleColor, + //xLocation*1.5f, + xLocationHint, + 93 * metrics.getVirtualH() / 100, false); + + } + */ + + } + + if (buttonCancel.getEnabled() == true) { + renderer.renderButton(&buttonCancel); + } + + renderer.swapBuffers(); + } + + void Logger::setCancelLoadingEnabled(bool value) { + Lang &lang = Lang::getInstance(); + const Metrics &metrics = Metrics::getInstance(); + //string containerName = "logger"; + //buttonCancel.registerGraphicComponent(containerName,"buttonCancel"); + buttonCancel.init((metrics.getVirtualW() / 2) - (125 / 2), 50 * metrics.getVirtualH() / 100, 125); + buttonCancel.setText(lang.getString("Cancel")); + buttonCancel.setEnabled(value); + //GraphicComponent::applyAllCustomProperties(containerName); } -*/ } - - if(buttonCancel.getEnabled() == true) { - renderer.renderButton(&buttonCancel); - } - - renderer.swapBuffers(); -} - -void Logger::setCancelLoadingEnabled(bool value) { - Lang &lang= Lang::getInstance(); - const Metrics &metrics= Metrics::getInstance(); - //string containerName = "logger"; - //buttonCancel.registerGraphicComponent(containerName,"buttonCancel"); - buttonCancel.init((metrics.getVirtualW() / 2) - (125 / 2), 50 * metrics.getVirtualH() / 100, 125); - buttonCancel.setText(lang.getString("Cancel")); - buttonCancel.setEnabled(value); - //GraphicComponent::applyAllCustomProperties(containerName); -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/facilities/logger.h b/source/glest_game/facilities/logger.h index 7728934f0..cd7940753 100644 --- a/source/glest_game/facilities/logger.h +++ b/source/glest_game/facilities/logger.h @@ -13,8 +13,8 @@ #define _SHARED_UTIL_LOGGER_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -30,74 +30,94 @@ using std::deque; using Shared::Graphics::Texture2D; using Shared::Util::Properties; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Logger -// -/// Interface to write log files -// ===================================================== + // ===================================================== + // class Logger + // + /// Interface to write log files + // ===================================================== -class Logger { -private: - static const int logLineCount; + class Logger { + private: + static const int logLineCount; -private: - typedef deque Strings; + private: + typedef deque Strings; -private: - string fileName; - string state; - string subtitle; - string current; - Texture2D *loadingTexture; - Properties gameHints; - Properties gameHintsTranslation; - string gameHintToShow; - int progress; - bool showProgressBar; + private: + string fileName; + string state; + string subtitle; + string current; + Texture2D *loadingTexture; + Properties gameHints; + Properties gameHintsTranslation; + string gameHintToShow; + int progress; + bool showProgressBar; - string statusText; - bool cancelSelected; - GraphicButton buttonCancel; - Vec4f displayColor; - GraphicButton buttonNextHint; + string statusText; + bool cancelSelected; + GraphicButton buttonCancel; + Vec4f displayColor; + GraphicButton buttonNextHint; -private: - Logger(); - ~Logger(); + private: + Logger(); + ~Logger(); -public: - static Logger & getInstance(); + public: + static Logger & getInstance(); - //void setMasterserverMode(bool value) { masterserverMode = value; } + //void setMasterserverMode(bool value) { masterserverMode = value; } - void setFile(const string &fileName) {this->fileName= fileName;} - void setState(const string &state) {this->state= state;} - void setSubtitle(const string &subtitle) {this->subtitle= subtitle;} - void setProgress(int value) { this->progress = value; } - int getProgress() const {return progress;} - void showProgress() { showProgressBar = true;} - void hideProgress() { showProgressBar = false;} + void setFile(const string &fileName) { + this->fileName = fileName; + } + void setState(const string &state) { + this->state = state; + } + void setSubtitle(const string &subtitle) { + this->subtitle = subtitle; + } + void setProgress(int value) { + this->progress = value; + } + int getProgress() const { + return progress; + } + void showProgress() { + showProgressBar = true; + } + void hideProgress() { + showProgressBar = false; + } - void add(const string str, bool renderScreen= false, const string statusText=""); - void loadLoadingScreen(string filepath); - void loadGameHints(string filePathEnglish,string filePathTranslation,bool clearList); - void renderLoadingScreen(); + void add(const string str, bool renderScreen = false, const string statusText = ""); + void loadLoadingScreen(string filepath); + void loadGameHints(string filePathEnglish, string filePathTranslation, bool clearList); + void renderLoadingScreen(); - void setCancelLoadingEnabled(bool value); - bool getCancelLoading() const { return cancelSelected; } - void setCancelLoading(bool value) { cancelSelected = value; } - void handleMouseClick(int x, int y); - void clearHints(); + void setCancelLoadingEnabled(bool value); + bool getCancelLoading() const { + return cancelSelected; + } + void setCancelLoading(bool value) { + cancelSelected = value; + } + void handleMouseClick(int x, int y); + void clearHints(); - void clear(); + void clear(); -private: - void showNextHint(); + private: + void showNextHint(); -}; + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/game/chat_manager.cpp b/source/glest_game/game/chat_manager.cpp index 52ca58182..ceff370e3 100644 --- a/source/glest_game/game/chat_manager.cpp +++ b/source/glest_game/game/chat_manager.cpp @@ -37,808 +37,684 @@ using namespace std; using namespace - Shared::Platform; +Shared::Platform; using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class ChatManager -// ===================================================== - - ChatManager::ChatManager () - { - console = NULL; - editEnabled = false; - teamMode = false; - thisTeamIndex = -1; - disableTeamMode = false; - xPos = 75; - yPos = 155; - maxTextLenght = 90; - textCharLength. - clear (); - text = ""; - font = CoreData::getInstance ().getConsoleFont (); - font3D = CoreData::getInstance ().getConsoleFont3D (); - inMenu = false; - customCB = NULL; - this-> - maxCustomTextLength = maxTextLenght; - } - - void - ChatManager::init (Console * console, int thisTeamIndex, - const bool inMenu, string manualPlayerNameOverride) - { - this->console = console; - this-> - thisTeamIndex = thisTeamIndex; - this-> - disableTeamMode = false; - this-> - inMenu = inMenu; - this-> - manualPlayerNameOverride = manualPlayerNameOverride; - } - - void - ChatManager::setDisableTeamMode (bool value) - { - disableTeamMode = value; - - if (disableTeamMode == true) - { - teamMode = false; - } - } - - void - ChatManager::keyUp (SDL_KeyboardEvent key) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - try - { - if (editEnabled == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - if (isKeyPressed (SDLK_ESCAPE, key, false) == true) - { - text.clear (); - textCharLength.clear (); - editEnabled = false; - - if (customCB != NULL) - { - customCB->processInputText (text, true); - customCB = NULL; - } - } - } - } - catch (const exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - bool - ChatManager::textInput (std::string inputText) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] inputText [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - inputText.c_str ()); - - int - maxTextLenAllowed = - (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); - string - textToAdd = - getTextWithLengthCheck (inputText, textCharLength.size (), - maxTextLenAllowed); - - if (editEnabled && (int) textCharLength.size () < maxTextLenAllowed - && textToAdd.size () > 0) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - appendText (textToAdd, true, true); - updateAutoCompleteBuffer (); - return true; - } - return false; - } - - string - ChatManager::getTextWithLengthCheck (string addText, int currentLength, - int maxLength) - { - string - resultText = ""; - if (addText.empty () == false) - { - int - utf8CharsAdded = 0; - for (unsigned int index = 0; index < addText.size ();) - { - int - len = getUTF8_Width (&addText[index]); - utf8CharsAdded++; - if (currentLength + utf8CharsAdded > maxLength) - { - break; - } - resultText += addText.substr (index, len); - index += len; - } - } - return resultText; - } - - void - ChatManager::keyDown (SDL_KeyboardEvent key) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - try - { - Lang & lang = Lang::getInstance (); - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - //toggle team mode - if (editEnabled == false && - isKeyPressed (configKeys.getSDLKey ("ChatTeamMode"), key) == true) - { - if (disableTeamMode == true) - { - if (!inMenu) - { - console->addLine (lang. - getString - ("ChatModeDisabledToAvoidCheating")); - } - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - if (!inMenu) - { - if (teamMode == true) - { - teamMode = false; - console->addLine (lang.getString ("ChatMode") + ": " + - lang.getString ("All")); - } - else - { - teamMode = true; - console->addLine (lang.getString ("ChatMode") + ": " + - lang.getString ("Team")); - } - } - } - } - - - if (isKeyPressed (SDLK_RETURN, key, false) == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - SDL_keysym - keystate = key.keysym; - if (keystate.mod & (KMOD_LALT | KMOD_RALT)) - { - // alt+enter is ignored - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - if (editEnabled == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, - __LINE__, key.keysym.sym, - key.keysym.sym); - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance (). - getGameNetworkInterface (); - if (text.empty () == false) - { - - if (customCB == NULL) - { - //string playerName = gameNetworkInterface->getHumanPlayerName(); - int - playerIndex = - gameNetworkInterface->getHumanPlayerIndex (); - - if (this->manualPlayerNameOverride != "") - { - console->addLine (text, false, - this-> - manualPlayerNameOverride, - Vec3f (1.f, 1.f, 1.f), - teamMode); - } - else - { - console->addLine (text, false, playerIndex, - Vec3f (1.f, 1.f, 1.f), - teamMode); - } - - gameNetworkInterface->sendTextMessage ("*" + text, - teamMode ? - thisTeamIndex - : -1, - false, ""); - if (inMenu == false - && Config::getInstance (). - getBool ("ChatStaysActive", "false") == false) - { - editEnabled = false; - } - } - } - else - { - editEnabled = false; - } - - if (customCB != NULL) - { - customCB->processInputText (text, false); - editEnabled = false; - customCB = NULL; - } - - text.clear (); - textCharLength.clear (); - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, - __LINE__, key.keysym.sym, - key.keysym.sym); - - switchOnEdit (); - } - } - } - else if (isKeyPressed (SDLK_TAB, key, false) == true) - { - if (text.empty () == false) - { - // First find the prefix characters to auto-complete - string - currentAutoCompleteName = ""; - - int - startPos = -1; - for (int i = (int)text.size () - 1; i >= 0; --i) - { - if (text[i] != ' ') - { - startPos = i; - } - else - { - break; - } - } - - if (startPos >= 0) - { - currentAutoCompleteName = text.substr (startPos); - } - - //printf("TAB currentAutoCompleteName [%s] lastAutoCompleteSearchText [%s]\n",currentAutoCompleteName.c_str(),lastAutoCompleteSearchText.c_str()); - string - autoCompleteName = lastAutoCompleteSearchText; - - // Now lookup the prefix for a match in playernames - string - autoCompleteResult = ""; - - int - replaceCurrentAutoCompleteName = -1; - vector < int > - matchedIndexes; - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - const GameSettings * - settings = gameNetworkInterface->getGameSettings (); - for (unsigned int factionIndex = 0; - factionIndex < - (unsigned int) settings->getFactionCount (); - ++factionIndex) - { - string - playerName = - settings->getNetworkPlayerName (factionIndex); - if (playerName.length () > autoCompleteName.length () - && StartsWith (toLower (playerName), - toLower (autoCompleteName)) == true) - { - if (toLower (playerName) == - toLower (currentAutoCompleteName)) - { - replaceCurrentAutoCompleteName = factionIndex; - } - else - { - autoCompleteResult = - playerName.substr (autoCompleteName.length ()); - matchedIndexes.push_back (factionIndex); - } - } - } - if (matchedIndexes.empty () == false) - { - int - newMatchedIndex = -1; - for (unsigned int index = 0; - index < (unsigned int) matchedIndexes.size (); - ++index) - { - int - possibleMatchIndex = matchedIndexes[index]; - if (replaceCurrentAutoCompleteName < 0 - || possibleMatchIndex > - replaceCurrentAutoCompleteName) - { - newMatchedIndex = possibleMatchIndex; - break; - } - } - if (newMatchedIndex < 0) - { - for (unsigned int index = 0; - index < (unsigned int) matchedIndexes.size (); - ++index) - { - int - possibleMatchIndex = matchedIndexes[index]; - if (replaceCurrentAutoCompleteName < 0 - || possibleMatchIndex > - replaceCurrentAutoCompleteName) - { - newMatchedIndex = possibleMatchIndex; - break; - } - } - } - - if (newMatchedIndex >= 0) - { - autoCompleteResult = - settings->getNetworkPlayerName (newMatchedIndex). - substr (autoCompleteName.length ()); - } - } - - if (autoCompleteResult == "") - { - replaceCurrentAutoCompleteName = -1; - matchedIndexes.clear (); - for (unsigned int index = 0; - index < (unsigned int) autoCompleteTextList.size (); - ++index) - { - string - autoText = autoCompleteTextList[index]; - - //printf("CHECKING #2 autoText.length() = %d [%s] autoCompleteName.length() = %d [%s]\n",autoText.length(),autoText.c_str(),autoCompleteName.length(),currentAutoCompleteName.c_str()); - - if (autoText.length () > autoCompleteName.length () && - StartsWith (toLower (autoText), - toLower (autoCompleteName)) == true) - { - - if (toLower (autoText) == - toLower (currentAutoCompleteName)) - { - replaceCurrentAutoCompleteName = index; - //printf("CHECKING #2 REPLACE\n"); - } - else - { - autoCompleteResult = - autoText.substr (autoCompleteName. - length ()); - //printf("CHECKING #2 autoCompleteResult [%s] autoCompleteName [%s]\n",autoCompleteResult.c_str(),autoCompleteName.c_str()); - matchedIndexes.push_back (index); - } - } - } - if (matchedIndexes.empty () == false) - { - int - newMatchedIndex = -1; - for (unsigned int index = 0; - index < (unsigned int) matchedIndexes.size (); - ++index) - { - int - possibleMatchIndex = matchedIndexes[index]; - if (replaceCurrentAutoCompleteName < 0 - || possibleMatchIndex > - replaceCurrentAutoCompleteName) - { - newMatchedIndex = possibleMatchIndex; - break; - } - } - if (newMatchedIndex < 0) - { - for (unsigned int index = 0; - index < - (unsigned int) matchedIndexes.size (); - ++index) - { - int - possibleMatchIndex = matchedIndexes[index]; - if (replaceCurrentAutoCompleteName < 0 - || possibleMatchIndex > - replaceCurrentAutoCompleteName) - { - newMatchedIndex = possibleMatchIndex; - break; - } - } - } - - if (newMatchedIndex >= 0) - { - autoCompleteResult = - autoCompleteTextList[newMatchedIndex]. - substr (autoCompleteName.length ()); - } - } - } - - if (autoCompleteResult != "") - { - if (replaceCurrentAutoCompleteName >= 0) - { - deleteText ((int) currentAutoCompleteName.length (), - false); - - autoCompleteResult = - autoCompleteName + autoCompleteResult; - - //printf("REPLACE: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str()); - } - else - { - //printf("ADD: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str()); - } - appendText (autoCompleteResult, false, false); - } - } - } - else if (isKeyPressed (SDLK_BACKSPACE, key, false) == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - deleteText (1); - } - - } - catch (const exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - ChatManager::keyPress (SDL_KeyboardEvent c) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - c.keysym.sym, c.keysym.sym); - -// no more textinput with keyPress in SDL2! -// int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); -// if(editEnabled && (int)text.size() < maxTextLenAllowed) { -// SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,c.keysym.sym,c.keysym.sym); -// //space is the first meaningful code -// wchar_t key = extractKeyPressedUnicode(c); -// wchar_t textAppend[] = { key, 0 }; -// appendText(textAppend); -// } - } - - void - ChatManager::switchOnEdit (CustomInputCallbackInterface * customCB, - int maxCustomTextLength) - { - editEnabled = true; - text.clear (); - textCharLength.clear (); - this->customCB = customCB; - if (maxCustomTextLength > 0) - { - this->maxCustomTextLength = maxCustomTextLength; - } - else - { - this->maxCustomTextLength = maxTextLenght; - } - } - - void - ChatManager::deleteText (int deleteCount, bool addToAutoCompleteBuffer) - { - if (text.empty () == false && deleteCount >= 0) - { - for (unsigned int i = 0; i < (unsigned int) deleteCount; ++i) - { - if (textCharLength.empty () == false) - { - //printf("BEFORE DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); - - if (textCharLength[textCharLength.size () - 1] > - (int) text.length ()) - { - textCharLength[(int) textCharLength.size () - 1] = - (int) text.length (); - } - for (unsigned int i = 0; - i < - (unsigned int) textCharLength[textCharLength.size () - - 1]; ++i) - { - text.erase (text.end () - 1); - } - //printf("AFTER DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); - textCharLength.pop_back (); - - if (addToAutoCompleteBuffer == true) - { - updateAutoCompleteBuffer (); - } - } - } - } - - } - - void - ChatManager::appendText (string addText, bool validateChars, - bool addToAutoCompleteBuffer) - { - - for (unsigned int index = 0; index < addText.size ();) - { - int - len = getUTF8_Width (&addText[index]); - textCharLength.push_back (len); - text += addText.substr (index, len); - index += len; - - if (addToAutoCompleteBuffer == true) - { - updateAutoCompleteBuffer (); - } - } - } - - void - ChatManager::updateAutoCompleteBuffer () - { - if (text.empty () == false) - { - int - startPos = -1; - for (int i = (int)text.size () - 1; i >= 0; --i) - { - if (text[i] != ' ') - { - startPos = i; - } - else - { - break; - } - } - - if (startPos >= 0) - { - lastAutoCompleteSearchText = text.substr (startPos); - } - } - } - - void - ChatManager::addText (string text) - { - int - maxTextLenAllowed = - (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); - if (editEnabled - && (int) text.size () + (int) this->text.size () <= - maxTextLenAllowed) - { - this->text += text; - for (int i = 0; i < (int) text.size (); i++) - { - textCharLength.push_back (1); - } - } - } - - void - ChatManager::updateNetwork () - { - try - { - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - //string text; - //string sender; - //Config &config= Config::getInstance(); - - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameNetworkInterface->getChatText() [%s]\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface->getChatText().c_str()); - - if (gameNetworkInterface != NULL && - gameNetworkInterface->getChatTextList (false).empty () == false) - { - Lang & lang = Lang::getInstance (); - - std::vector < ChatMsgInfo > chatList = - gameNetworkInterface->getChatTextList (true); - for (int idx = 0; idx < (int) chatList.size (); idx++) - { - const ChatMsgInfo - msg = chatList[idx]; - int - teamIndex = msg.chatTeamIndex; - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] got nmtText [%s] for team = %d\n", - __FILE__, __FUNCTION__, - msg.chatText.c_str (), teamIndex); - - if (teamIndex == -1 || teamIndex == thisTeamIndex) - { - if (msg.targetLanguage == "" - || lang.isLanguageLocal (msg.targetLanguage) == true) - { - bool - teamMode = (teamIndex != -1 - && teamIndex == thisTeamIndex); - string - playerName = - gameNetworkInterface->getHumanPlayerName (); - if (this->manualPlayerNameOverride != "") - { - playerName = this->manualPlayerNameOverride; - } - - //printf("Network chat msg from: [%d - %s] [%s]\n",msg.chatPlayerIndex,gameNetworkInterface->getHumanPlayerName().c_str(),this->manualPlayerNameOverride.c_str()); - - if (StartsWith (msg.chatText, "*")) - { - if (msg.chatText.find (playerName) != - string::npos) - { - static PlaySoundClip snd; - SoundRenderer & soundRenderer = - SoundRenderer::getInstance (); - soundRenderer.playFx (snd.getSound (snd.sfxHighlight), - true); - } - console->addLine (msg.chatText. - substr (1, - msg.chatText.size ()), - true, msg.chatPlayerIndex, - Vec3f (1.f, 1.f, 1.f), - teamMode); - } - else - { - console->addLine (msg.chatText, true, - msg.chatPlayerIndex, Vec3f (1.f, - 1.f, - 1.f), - teamMode); - } - - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Added text to console\n", - __FILE__, __FUNCTION__); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - //gameNetworkInterface->clearChatInfo(); - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw - megaglest_runtime_error (szBuf); - } - } - -}} //end namespace + Glest { + namespace + Game { + + // ===================================================== + // class ChatManager + // ===================================================== + + ChatManager::ChatManager() { + console = NULL; + editEnabled = false; + teamMode = false; + thisTeamIndex = -1; + disableTeamMode = false; + xPos = 75; + yPos = 155; + maxTextLenght = 90; + textCharLength. + clear(); + text = ""; + font = CoreData::getInstance().getConsoleFont(); + font3D = CoreData::getInstance().getConsoleFont3D(); + inMenu = false; + customCB = NULL; + this-> + maxCustomTextLength = maxTextLenght; + } + + void + ChatManager::init(Console * console, int thisTeamIndex, + const bool inMenu, string manualPlayerNameOverride) { + this->console = console; + this-> + thisTeamIndex = thisTeamIndex; + this-> + disableTeamMode = false; + this-> + inMenu = inMenu; + this-> + manualPlayerNameOverride = manualPlayerNameOverride; + } + + void + ChatManager::setDisableTeamMode(bool value) { + disableTeamMode = value; + + if (disableTeamMode == true) { + teamMode = false; + } + } + + void + ChatManager::keyUp(SDL_KeyboardEvent key) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + try { + if (editEnabled == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + if (isKeyPressed(SDLK_ESCAPE, key, false) == true) { + text.clear(); + textCharLength.clear(); + editEnabled = false; + + if (customCB != NULL) { + customCB->processInputText(text, true); + customCB = NULL; + } + } + } + } catch (const exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + bool + ChatManager::textInput(std::string inputText) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] inputText [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + inputText.c_str()); + + int + maxTextLenAllowed = + (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); + string + textToAdd = + getTextWithLengthCheck(inputText, textCharLength.size(), + maxTextLenAllowed); + + if (editEnabled && (int) textCharLength.size() < maxTextLenAllowed + && textToAdd.size() > 0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + appendText(textToAdd, true, true); + updateAutoCompleteBuffer(); + return true; + } + return false; + } + + string + ChatManager::getTextWithLengthCheck(string addText, int currentLength, + int maxLength) { + string + resultText = ""; + if (addText.empty() == false) { + int + utf8CharsAdded = 0; + for (unsigned int index = 0; index < addText.size();) { + int + len = getUTF8_Width(&addText[index]); + utf8CharsAdded++; + if (currentLength + utf8CharsAdded > maxLength) { + break; + } + resultText += addText.substr(index, len); + index += len; + } + } + return resultText; + } + + void + ChatManager::keyDown(SDL_KeyboardEvent key) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + try { + Lang & lang = Lang::getInstance(); + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + //toggle team mode + if (editEnabled == false && + isKeyPressed(configKeys.getSDLKey("ChatTeamMode"), key) == true) { + if (disableTeamMode == true) { + if (!inMenu) { + console->addLine(lang. + getString + ("ChatModeDisabledToAvoidCheating")); + } + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + if (!inMenu) { + if (teamMode == true) { + teamMode = false; + console->addLine(lang.getString("ChatMode") + ": " + + lang.getString("All")); + } else { + teamMode = true; + console->addLine(lang.getString("ChatMode") + ": " + + lang.getString("Team")); + } + } + } + } + + + if (isKeyPressed(SDLK_RETURN, key, false) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + SDL_keysym + keystate = key.keysym; + if (keystate.mod & (KMOD_LALT | KMOD_RALT)) { + // alt+enter is ignored + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + if (editEnabled == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, + __LINE__, key.keysym.sym, + key.keysym.sym); + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance(). + getGameNetworkInterface(); + if (text.empty() == false) { + + if (customCB == NULL) { + //string playerName = gameNetworkInterface->getHumanPlayerName(); + int + playerIndex = + gameNetworkInterface->getHumanPlayerIndex(); + + if (this->manualPlayerNameOverride != "") { + console->addLine(text, false, + this-> + manualPlayerNameOverride, + Vec3f(1.f, 1.f, 1.f), + teamMode); + } else { + console->addLine(text, false, playerIndex, + Vec3f(1.f, 1.f, 1.f), + teamMode); + } + + gameNetworkInterface->sendTextMessage("*" + text, + teamMode ? + thisTeamIndex + : -1, + false, ""); + if (inMenu == false + && Config::getInstance(). + getBool("ChatStaysActive", "false") == false) { + editEnabled = false; + } + } + } else { + editEnabled = false; + } + + if (customCB != NULL) { + customCB->processInputText(text, false); + editEnabled = false; + customCB = NULL; + } + + text.clear(); + textCharLength.clear(); + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, + __LINE__, key.keysym.sym, + key.keysym.sym); + + switchOnEdit(); + } + } + } else if (isKeyPressed(SDLK_TAB, key, false) == true) { + if (text.empty() == false) { + // First find the prefix characters to auto-complete + string + currentAutoCompleteName = ""; + + int + startPos = -1; + for (int i = (int) text.size() - 1; i >= 0; --i) { + if (text[i] != ' ') { + startPos = i; + } else { + break; + } + } + + if (startPos >= 0) { + currentAutoCompleteName = text.substr(startPos); + } + + //printf("TAB currentAutoCompleteName [%s] lastAutoCompleteSearchText [%s]\n",currentAutoCompleteName.c_str(),lastAutoCompleteSearchText.c_str()); + string + autoCompleteName = lastAutoCompleteSearchText; + + // Now lookup the prefix for a match in playernames + string + autoCompleteResult = ""; + + int + replaceCurrentAutoCompleteName = -1; + vector < int > + matchedIndexes; + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + const GameSettings * + settings = gameNetworkInterface->getGameSettings(); + for (unsigned int factionIndex = 0; + factionIndex < + (unsigned int) settings->getFactionCount(); + ++factionIndex) { + string + playerName = + settings->getNetworkPlayerName(factionIndex); + if (playerName.length() > autoCompleteName.length() + && StartsWith(toLower(playerName), + toLower(autoCompleteName)) == true) { + if (toLower(playerName) == + toLower(currentAutoCompleteName)) { + replaceCurrentAutoCompleteName = factionIndex; + } else { + autoCompleteResult = + playerName.substr(autoCompleteName.length()); + matchedIndexes.push_back(factionIndex); + } + } + } + if (matchedIndexes.empty() == false) { + int + newMatchedIndex = -1; + for (unsigned int index = 0; + index < (unsigned int) matchedIndexes.size(); + ++index) { + int + possibleMatchIndex = matchedIndexes[index]; + if (replaceCurrentAutoCompleteName < 0 + || possibleMatchIndex > + replaceCurrentAutoCompleteName) { + newMatchedIndex = possibleMatchIndex; + break; + } + } + if (newMatchedIndex < 0) { + for (unsigned int index = 0; + index < (unsigned int) matchedIndexes.size(); + ++index) { + int + possibleMatchIndex = matchedIndexes[index]; + if (replaceCurrentAutoCompleteName < 0 + || possibleMatchIndex > + replaceCurrentAutoCompleteName) { + newMatchedIndex = possibleMatchIndex; + break; + } + } + } + + if (newMatchedIndex >= 0) { + autoCompleteResult = + settings->getNetworkPlayerName(newMatchedIndex). + substr(autoCompleteName.length()); + } + } + + if (autoCompleteResult == "") { + replaceCurrentAutoCompleteName = -1; + matchedIndexes.clear(); + for (unsigned int index = 0; + index < (unsigned int) autoCompleteTextList.size(); + ++index) { + string + autoText = autoCompleteTextList[index]; + + //printf("CHECKING #2 autoText.length() = %d [%s] autoCompleteName.length() = %d [%s]\n",autoText.length(),autoText.c_str(),autoCompleteName.length(),currentAutoCompleteName.c_str()); + + if (autoText.length() > autoCompleteName.length() && + StartsWith(toLower(autoText), + toLower(autoCompleteName)) == true) { + + if (toLower(autoText) == + toLower(currentAutoCompleteName)) { + replaceCurrentAutoCompleteName = index; + //printf("CHECKING #2 REPLACE\n"); + } else { + autoCompleteResult = + autoText.substr(autoCompleteName. + length()); + //printf("CHECKING #2 autoCompleteResult [%s] autoCompleteName [%s]\n",autoCompleteResult.c_str(),autoCompleteName.c_str()); + matchedIndexes.push_back(index); + } + } + } + if (matchedIndexes.empty() == false) { + int + newMatchedIndex = -1; + for (unsigned int index = 0; + index < (unsigned int) matchedIndexes.size(); + ++index) { + int + possibleMatchIndex = matchedIndexes[index]; + if (replaceCurrentAutoCompleteName < 0 + || possibleMatchIndex > + replaceCurrentAutoCompleteName) { + newMatchedIndex = possibleMatchIndex; + break; + } + } + if (newMatchedIndex < 0) { + for (unsigned int index = 0; + index < + (unsigned int) matchedIndexes.size(); + ++index) { + int + possibleMatchIndex = matchedIndexes[index]; + if (replaceCurrentAutoCompleteName < 0 + || possibleMatchIndex > + replaceCurrentAutoCompleteName) { + newMatchedIndex = possibleMatchIndex; + break; + } + } + } + + if (newMatchedIndex >= 0) { + autoCompleteResult = + autoCompleteTextList[newMatchedIndex]. + substr(autoCompleteName.length()); + } + } + } + + if (autoCompleteResult != "") { + if (replaceCurrentAutoCompleteName >= 0) { + deleteText((int) currentAutoCompleteName.length(), + false); + + autoCompleteResult = + autoCompleteName + autoCompleteResult; + + //printf("REPLACE: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str()); + } else { + //printf("ADD: currentAutoCompleteName [%s] autoCompleteResult [%s] text [%s]\n",currentAutoCompleteName.c_str(),autoCompleteResult.c_str(),text.c_str()); + } + appendText(autoCompleteResult, false, false); + } + } + } else if (isKeyPressed(SDLK_BACKSPACE, key, false) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + deleteText(1); + } + + } catch (const exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + ChatManager::keyPress(SDL_KeyboardEvent c) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + c.keysym.sym, c.keysym.sym); + + // no more textinput with keyPress in SDL2! + // int maxTextLenAllowed = (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); + // if(editEnabled && (int)text.size() < maxTextLenAllowed) { + // SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] key = [%c] [%d]\n",__FILE__,__FUNCTION__,__LINE__,c.keysym.sym,c.keysym.sym); + // //space is the first meaningful code + // wchar_t key = extractKeyPressedUnicode(c); + // wchar_t textAppend[] = { key, 0 }; + // appendText(textAppend); + // } + } + + void + ChatManager::switchOnEdit(CustomInputCallbackInterface * customCB, + int maxCustomTextLength) { + editEnabled = true; + text.clear(); + textCharLength.clear(); + this->customCB = customCB; + if (maxCustomTextLength > 0) { + this->maxCustomTextLength = maxCustomTextLength; + } else { + this->maxCustomTextLength = maxTextLenght; + } + } + + void + ChatManager::deleteText(int deleteCount, bool addToAutoCompleteBuffer) { + if (text.empty() == false && deleteCount >= 0) { + for (unsigned int i = 0; i < (unsigned int) deleteCount; ++i) { + if (textCharLength.empty() == false) { + //printf("BEFORE DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); + + if (textCharLength[textCharLength.size() - 1] > + (int) text.length()) { + textCharLength[(int) textCharLength.size() - 1] = + (int) text.length(); + } + for (unsigned int i = 0; + i < + (unsigned int) textCharLength[textCharLength.size() - + 1]; ++i) { + text.erase(text.end() - 1); + } + //printf("AFTER DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); + textCharLength.pop_back(); + + if (addToAutoCompleteBuffer == true) { + updateAutoCompleteBuffer(); + } + } + } + } + + } + + void + ChatManager::appendText(string addText, bool validateChars, + bool addToAutoCompleteBuffer) { + + for (unsigned int index = 0; index < addText.size();) { + int + len = getUTF8_Width(&addText[index]); + textCharLength.push_back(len); + text += addText.substr(index, len); + index += len; + + if (addToAutoCompleteBuffer == true) { + updateAutoCompleteBuffer(); + } + } + } + + void + ChatManager::updateAutoCompleteBuffer() { + if (text.empty() == false) { + int + startPos = -1; + for (int i = (int) text.size() - 1; i >= 0; --i) { + if (text[i] != ' ') { + startPos = i; + } else { + break; + } + } + + if (startPos >= 0) { + lastAutoCompleteSearchText = text.substr(startPos); + } + } + } + + void + ChatManager::addText(string text) { + int + maxTextLenAllowed = + (customCB != NULL ? this->maxCustomTextLength : maxTextLenght); + if (editEnabled + && (int) text.size() + (int) this->text.size() <= + maxTextLenAllowed) { + this->text += text; + for (int i = 0; i < (int) text.size(); i++) { + textCharLength.push_back(1); + } + } + } + + void + ChatManager::updateNetwork() { + try { + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + //string text; + //string sender; + //Config &config= Config::getInstance(); + + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] gameNetworkInterface->getChatText() [%s]\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface->getChatText().c_str()); + + if (gameNetworkInterface != NULL && + gameNetworkInterface->getChatTextList(false).empty() == false) { + Lang & lang = Lang::getInstance(); + + std::vector < ChatMsgInfo > chatList = + gameNetworkInterface->getChatTextList(true); + for (int idx = 0; idx < (int) chatList.size(); idx++) { + const ChatMsgInfo + msg = chatList[idx]; + int + teamIndex = msg.chatTeamIndex; + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] got nmtText [%s] for team = %d\n", + __FILE__, __FUNCTION__, + msg.chatText.c_str(), teamIndex); + + if (teamIndex == -1 || teamIndex == thisTeamIndex) { + if (msg.targetLanguage == "" + || lang.isLanguageLocal(msg.targetLanguage) == true) { + bool + teamMode = (teamIndex != -1 + && teamIndex == thisTeamIndex); + string + playerName = + gameNetworkInterface->getHumanPlayerName(); + if (this->manualPlayerNameOverride != "") { + playerName = this->manualPlayerNameOverride; + } + + //printf("Network chat msg from: [%d - %s] [%s]\n",msg.chatPlayerIndex,gameNetworkInterface->getHumanPlayerName().c_str(),this->manualPlayerNameOverride.c_str()); + + if (StartsWith(msg.chatText, "*")) { + if (msg.chatText.find(playerName) != + string::npos) { + static PlaySoundClip snd; + SoundRenderer & soundRenderer = + SoundRenderer::getInstance(); + soundRenderer.playFx(snd.getSound(snd.sfxHighlight), + true); + } + console->addLine(msg.chatText. + substr(1, + msg.chatText.size()), + true, msg.chatPlayerIndex, + Vec3f(1.f, 1.f, 1.f), + teamMode); + } else { + console->addLine(msg.chatText, true, + msg.chatPlayerIndex, Vec3f(1.f, + 1.f, + 1.f), + teamMode); + } + + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Added text to console\n", + __FILE__, __FUNCTION__); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //gameNetworkInterface->clearChatInfo(); + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw + megaglest_runtime_error(szBuf); + } + } + + } +} //end namespace diff --git a/source/glest_game/game/chat_manager.h b/source/glest_game/game/chat_manager.h index 1b0b32ee5..8a58358fb 100644 --- a/source/glest_game/game/chat_manager.h +++ b/source/glest_game/game/chat_manager.h @@ -36,225 +36,203 @@ # include "leak_dumper.h" using - std::string; +std::string; using - std::vector; +std::vector; using - Shared::Graphics::Font2D; +Shared::Graphics::Font2D; using - Shared::Graphics::Font3D; +Shared::Graphics::Font3D; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - class - Console; + class + Console; -// -// This interface describes the methods a callback object must implement -// - class - CustomInputCallbackInterface - { - public: - virtual void - processInputText (string text, bool cancelled) = 0; - virtual ~ - CustomInputCallbackInterface () - { - } - }; + // + // This interface describes the methods a callback object must implement + // + class + CustomInputCallbackInterface { + public: + virtual void + processInputText(string text, bool cancelled) = 0; + virtual ~ + CustomInputCallbackInterface() { + } + }; -// ===================================================== -// class ChatManager -// ===================================================== + // ===================================================== + // class ChatManager + // ===================================================== - class - ChatManager - { + class + ChatManager { - private: - bool - editEnabled; - bool - teamMode; - bool - disableTeamMode; - Console * - console; - string - text; - vector < int > - textCharLength; - int - thisTeamIndex; - bool - inMenu; - string - manualPlayerNameOverride; - int - xPos; - int - yPos; - int - maxTextLenght; - Font2D * - font; - Font3D * - font3D; + private: + bool + editEnabled; + bool + teamMode; + bool + disableTeamMode; + Console * + console; + string + text; + vector < int > + textCharLength; + int + thisTeamIndex; + bool + inMenu; + string + manualPlayerNameOverride; + int + xPos; + int + yPos; + int + maxTextLenght; + Font2D * + font; + Font3D * + font3D; - string - lastAutoCompleteSearchText; - vector < - string > - autoCompleteTextList; + string + lastAutoCompleteSearchText; + vector < + string > + autoCompleteTextList; - CustomInputCallbackInterface * - customCB; - int - maxCustomTextLength; + CustomInputCallbackInterface * + customCB; + int + maxCustomTextLength; - string - getTextWithLengthCheck (string text, int currentLength, int maxLength); - void - appendText (string addText, bool validateChars = - true, bool addToAutoCompleteBuffer = true); - void - deleteText (int deleteCount, bool addToAutoCompleteBuffer = true); - void - updateAutoCompleteBuffer (); + string + getTextWithLengthCheck(string text, int currentLength, int maxLength); + void + appendText(string addText, bool validateChars = + true, bool addToAutoCompleteBuffer = true); + void + deleteText(int deleteCount, bool addToAutoCompleteBuffer = true); + void + updateAutoCompleteBuffer(); - public: - ChatManager (); - void - init (Console * console, int thisTeamIndex, const bool inMenu = - false, string manualPlayerNameOverride = ""); + public: + ChatManager(); + void + init(Console * console, int thisTeamIndex, const bool inMenu = + false, string manualPlayerNameOverride = ""); - bool - textInput (std::string text); - void - keyDown (SDL_KeyboardEvent key); - void - keyUp (SDL_KeyboardEvent key); - void - keyPress (SDL_KeyboardEvent c); - void - updateNetwork (); + bool + textInput(std::string text); + void + keyDown(SDL_KeyboardEvent key); + void + keyUp(SDL_KeyboardEvent key); + void + keyPress(SDL_KeyboardEvent c); + void + updateNetwork(); - bool - getEditEnabled () const - { - return - editEnabled; - } - bool - getTeamMode () const - { - return - teamMode; - } - bool - getInMenu () const - { - return - inMenu; - } - string - getText () const - { - return - text; - } - int - getXPos () const - { - return - xPos; - } - void - setXPos (int xPos) - { - this->xPos = xPos; - } - int - getYPos () const - { - return - yPos; - } - void - setYPos (int yPos) - { - this->yPos = yPos; - } - int - getMaxTextLenght () const - { - return - maxTextLenght; - } - void - setMaxTextLenght (int maxTextLenght) - { - this->maxTextLenght = maxTextLenght; - } - Font2D * - getFont () const - { - return - font; - } - Font3D * - getFont3D () const - { - return - font3D; - } - void - setFont (Font2D * font) - { - this->font = font; - } - void - setFont3D (Font3D * font) - { - this->font3D = font; - } - void - addText (string text); - void - switchOnEdit (CustomInputCallbackInterface * customCB = - NULL, int maxCustomTextLength = -1); + bool + getEditEnabled() const { + return + editEnabled; + } + bool + getTeamMode() const { + return + teamMode; + } + bool + getInMenu() const { + return + inMenu; + } + string + getText() const { + return + text; + } + int + getXPos() const { + return + xPos; + } + void + setXPos(int xPos) { + this->xPos = xPos; + } + int + getYPos() const { + return + yPos; + } + void + setYPos(int yPos) { + this->yPos = yPos; + } + int + getMaxTextLenght() const { + return + maxTextLenght; + } + void + setMaxTextLenght(int maxTextLenght) { + this->maxTextLenght = maxTextLenght; + } + Font2D * + getFont() const { + return + font; + } + Font3D * + getFont3D() const { + return + font3D; + } + void + setFont(Font2D * font) { + this->font = font; + } + void + setFont3D(Font3D * font) { + this->font3D = font; + } + void + addText(string text); + void + switchOnEdit(CustomInputCallbackInterface * customCB = + NULL, int maxCustomTextLength = -1); - bool - getDisableTeamMode () const - { - return - disableTeamMode; - } - void - setDisableTeamMode (bool value); + bool + getDisableTeamMode() const { + return + disableTeamMode; + } + void + setDisableTeamMode(bool value); - void - setAutoCompleteTextList (const vector < string > &list) - { - autoCompleteTextList = list; - } + void + setAutoCompleteTextList(const vector < string > &list) { + autoCompleteTextList = list; + } - bool - isInCustomInputMode () const - { - return - customCB != - NULL; - }; - }; + bool + isInCustomInputMode() const { + return + customCB != + NULL; + }; + }; - } + } } //end namespace #endif diff --git a/source/glest_game/game/commander.cpp b/source/glest_game/game/commander.cpp index 1f2337848..3b30e7b32 100644 --- a/source/glest_game/game/commander.cpp +++ b/source/glest_game/game/commander.cpp @@ -26,1993 +26,1827 @@ #include "game.h" using namespace - Shared::Graphics; +Shared::Graphics; using namespace - Shared::Util; +Shared::Util; using namespace - Shared::Platform; +Shared::Platform; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class Commander -// ===================================================== - Commander::Commander () - { - this->world = NULL; - this-> - pauseNetworkCommands = false; - } - - Commander::~ - Commander () - { - } - - void - Commander::init (World * world) - { - this->world = world; - } - - bool - Commander::canSubmitCommandType (const Unit * unit, - const CommandType * commandType) const - { - bool - canSubmitCommand = true; - const MorphCommandType * - mct = dynamic_cast < const - MorphCommandType * >(commandType); - if (mct && unit->getCommandSize () > 0) - { - Command * - cur_command = unit->getCurrCommand (); - if (cur_command != NULL) - { - const MorphCommandType * - cur_mct = dynamic_cast < const - MorphCommandType * >(cur_command->getCommandType ()); - if (cur_mct && unit->getCurrSkill () - && unit->getCurrSkill ()->getClass () == scMorph) - { - const UnitType * - morphUnitType = mct->getMorphUnit (); - const UnitType * - cur_morphUnitType = cur_mct->getMorphUnit (); - - if (morphUnitType != NULL && cur_morphUnitType != NULL - && morphUnitType->getId () == - cur_morphUnitType->getId ()) - { - canSubmitCommand = false; - } - } - } - } - return - canSubmitCommand; - } - - std::pair < CommandResult, - string > Commander::tryGiveCommand (const Selection * selection, - const CommandType * commandType, - const Vec2i & pos, - const UnitType * unitType, - CardinalDir facing, bool tryQueue, - Unit * targetUnit) const - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->pauseNetworkCommands == true) - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - std::pair < - CommandResult, - string > - result (crFailUndefined, ""); - if (selection->isEmpty () == false && commandType != NULL) - { - Vec2i - refPos; - CommandResultContainer - results; - - refPos = world->getMap ()->computeRefPos (selection); - - const Unit * - builderUnit = - world->getMap ()->findClosestUnitToPos (selection, pos, unitType); - - int - builderUnitId = builderUnit->getId (); - CommandStateType - commandStateType = cst_None; - int - commandStateValue = -1; - - int - unitCommandGroupId = -1; - if (selection->getCount () > 1) - { - unitCommandGroupId = world->getNextCommandGroupId (); - } - - //give orders to all selected units - for (int i = 0; i < selection->getCount (); ++i) - { - const Unit * - unit = selection->getUnit (i); - - - std::pair < CommandResult, string > resultCur (crFailUndefined, - ""); - bool - canSubmitCommand = canSubmitCommandType (unit, commandType); - if (canSubmitCommand == true) - { - int - unitId = unit->getId (); - Vec2i - currPos = - world->getMap ()->computeDestPos (refPos, - unit-> - getPosNotThreadSafe (), - pos); - - Vec2i - usePos = currPos; - const CommandType * - useCommandtype = commandType; - if (dynamic_cast < - const BuildCommandType * >(commandType) != NULL) - { - usePos = pos; - if (builderUnit->getId () != unitId) - { - useCommandtype = - unit->getType ()-> - getFirstRepairCommand (unitType); - commandStateType = cst_linkedUnit; - commandStateValue = builderUnitId; - } - else - { - commandStateType = cst_None; - commandStateValue = -1; - } - } - - if (useCommandtype != NULL) - { - NetworkCommand - networkCommand (this->world, nctGiveCommand, unitId, - useCommandtype->getId (), usePos, - unitType->getId (), - (targetUnit != - NULL ? targetUnit->getId () : -1), - facing, tryQueue, commandStateType, - commandStateValue, unitCommandGroupId); - - //every unit is ordered to a the position - resultCur = pushNetworkCommand (&networkCommand); - } - } - - results.push_back (resultCur); - } - - return computeResult (results); - } - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - std::pair < CommandResult, - string > Commander::tryGiveCommand (const Unit * unit, - const CommandType * commandType, - const Vec2i & pos, - const UnitType * unitType, - CardinalDir facing, bool tryQueue, - Unit * targetUnit, - int unitGroupCommandId) const - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->pauseNetworkCommands == true) - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono.start (); - - assert (this->world != NULL); - assert (unit != NULL); - assert (commandType != NULL); - assert (unitType != NULL); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - bool - canSubmitCommand = canSubmitCommandType (unit, commandType); - if (canSubmitCommand == true) - { - NetworkCommand - networkCommand (this->world, nctGiveCommand, unit->getId (), - commandType->getId (), pos, unitType->getId (), - (targetUnit != NULL ? targetUnit->getId () : -1), - facing, tryQueue, cst_None, -1, unitGroupCommandId); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - result = pushNetworkCommand (&networkCommand); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - return result; - } - - std::pair < CommandResult, - string > Commander::tryGiveCommand (const Selection * selection, - CommandClass commandClass, - const Vec2i & pos, - const Unit * targetUnit, - bool tryQueue) const - { - - if (this->pauseNetworkCommands == true) - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - std::pair < - CommandResult, - string > - result (crFailUndefined, ""); - if (selection->isEmpty () == false) - { - Vec2i - refPos, - currPos; - CommandResultContainer - results; - - refPos = world->getMap ()->computeRefPos (selection); - - int - unitCommandGroupId = -1; - if (selection->getCount () > 1) - { - unitCommandGroupId = world->getNextCommandGroupId (); - } - - //give orders to all selected units - for (int i = 0; i < selection->getCount (); ++i) - { - const Unit * - unit = selection->getUnit (i); - const CommandType * - ct = unit->getType ()->getFirstCtOfClass (commandClass); - if (ct != NULL) - { - std::pair < CommandResult, - string > resultCur (crFailUndefined, ""); - - bool - canSubmitCommand = canSubmitCommandType (unit, ct); - if (canSubmitCommand == true) - { - - int - targetId = - targetUnit == - NULL ? Unit::invalidId : targetUnit->getId (); - int - unitId = selection->getUnit (i)->getId (); - Vec2i - currPos = world->getMap ()->computeDestPos (refPos, - selection-> - getUnit - (i)-> - getPosNotThreadSafe - (), pos); - NetworkCommand - networkCommand (this->world, nctGiveCommand, - unitId, ct->getId (), currPos, -1, - targetId, -1, tryQueue, cst_None, -1, - unitCommandGroupId); - - //every unit is ordered to a different pos - resultCur = pushNetworkCommand (&networkCommand); - } - results.push_back (resultCur); - } - else - { - results.push_back (std::pair < CommandResult, - string > (crFailUndefined, "")); - } - } - return computeResult (results); - } - else - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - } - - std::pair < CommandResult, - string > Commander::tryGiveCommand (const Selection * selection, - const CommandType * commandType, - const Vec2i & pos, - const Unit * targetUnit, - bool tryQueue) const - { - - if (this->pauseNetworkCommands == true) - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - std::pair < - CommandResult, - string > - result (crFailUndefined, ""); - - if (!selection->isEmpty () && commandType != NULL) - { - Vec2i - refPos; - CommandResultContainer - results; - - refPos = world->getMap ()->computeRefPos (selection); - - int - unitCommandGroupId = -1; - if (selection->getCount () > 1) - { - unitCommandGroupId = world->getNextCommandGroupId (); - } - - //give orders to all selected units - for (int i = 0; i < selection->getCount (); ++i) - { - const Unit * - unit = selection->getUnit (i); - assert (unit != NULL); - - std::pair < CommandResult, string > resultCur (crFailUndefined, - ""); - - bool - canSubmitCommand = canSubmitCommandType (unit, commandType); - if (canSubmitCommand == true) - { - int - targetId = - targetUnit == - NULL ? Unit::invalidId : targetUnit->getId (); - int - unitId = unit->getId (); - Vec2i - currPos = - world->getMap ()->computeDestPos (refPos, - unit-> - getPosNotThreadSafe (), - pos); - NetworkCommand - networkCommand (this->world, nctGiveCommand, unitId, - commandType->getId (), currPos, -1, - targetId, -1, tryQueue, cst_None, -1, - unitCommandGroupId); - - //every unit is ordered to a different position - resultCur = pushNetworkCommand (&networkCommand); - } - results.push_back (resultCur); - } - - return computeResult (results); - } - else - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - } - -//auto command - std::pair < CommandResult, - string > Commander::tryGiveCommand (const Selection * selection, - const Vec2i & pos, - const Unit * targetUnit, - bool tryQueue, - int unitCommandGroupId) const - { - - if (this->pauseNetworkCommands == true) - { - return std::pair < CommandResult, string > (crFailUndefined, ""); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - - if (selection->isEmpty () == false) - { - Vec2i - refPos, - currPos; - CommandResultContainer - results; - - if (unitCommandGroupId == -1 && selection->getCount () > 1) - { - unitCommandGroupId = world->getNextCommandGroupId (); - } - - //give orders to all selected units - refPos = world->getMap ()->computeRefPos (selection); - for (int i = 0; i < selection->getCount (); ++i) - { - //every unit is ordered to a different pos - const Unit * - unit = selection->getUnit (i); - assert (unit != NULL); - - currPos = - world->getMap ()->computeDestPos (refPos, - unit-> - getPosNotThreadSafe (), - pos); - - //get command type - const CommandType * - commandType = unit->computeCommandType (pos, targetUnit); - - //give commands - if (commandType != NULL) - { - int - targetId = - targetUnit == - NULL ? Unit::invalidId : targetUnit->getId (); - int - unitId = unit->getId (); - - std::pair < CommandResult, - string > resultCur (crFailUndefined, ""); - - bool - canSubmitCommand = - canSubmitCommandType (unit, commandType); - if (canSubmitCommand == true) - { - NetworkCommand - networkCommand (this->world, nctGiveCommand, - unitId, commandType->getId (), currPos, - -1, targetId, -1, tryQueue, cst_None, - -1, unitCommandGroupId); - resultCur = pushNetworkCommand (&networkCommand); - } - results.push_back (resultCur); - } - else if (unit->isMeetingPointSettable () == true) - { - NetworkCommand - command (this->world, nctSetMeetingPoint, - unit->getId (), -1, currPos, -1, -1, -1, false, - cst_None, -1, unitCommandGroupId); - - std::pair < CommandResult, string > resultCur = - pushNetworkCommand (&command); - results.push_back (resultCur); - } - else - { - results.push_back (std::pair < CommandResult, - string > (crFailUndefined, "")); - } - } - result = computeResult (results); - } - - return result; - } - - CommandResult - Commander::tryCancelCommand (const Selection * selection) const - { - if (this->pauseNetworkCommands == true) - { - return crFailUndefined; - } - - int - unitCommandGroupId = -1; - if (selection->getCount () > 1) - { - unitCommandGroupId = world->getNextCommandGroupId (); - } - - for (int i = 0; i < selection->getCount (); ++i) - { - NetworkCommand - command (this->world, nctCancelCommand, - selection->getUnit (i)->getId (), -1, Vec2i (0), -1, -1, - -1, false, cst_None, -1, unitCommandGroupId); - pushNetworkCommand (&command); - } - - return crSuccess; - } - - void - Commander::trySetMeetingPoint (const Unit * unit, const Vec2i & pos) const - { - if (this->pauseNetworkCommands == true) - { - return; - } - - NetworkCommand - command (this->world, nctSetMeetingPoint, unit->getId (), -1, pos); - pushNetworkCommand (&command); - } - - void - Commander::trySwitchTeam (const Faction * faction, int teamIndex) const - { - if (this->pauseNetworkCommands == true) - { - return; - } - - NetworkCommand - command (this->world, nctSwitchTeam, faction->getIndex (), teamIndex); - pushNetworkCommand (&command); - } - - void - Commander::trySwitchTeamVote (const Faction * faction, - SwitchTeamVote * vote) const - { - if (this->pauseNetworkCommands == true) - { - return; - } - - NetworkCommand - command (this->world, nctSwitchTeamVote, faction->getIndex (), - vote->factionIndex, Vec2i (0), vote->allowSwitchTeam); - pushNetworkCommand (&command); - } - - void - Commander::tryDisconnectNetworkPlayer (const Faction * faction, - int playerIndex) const - { - NetworkCommand - command (this->world, nctDisconnectNetworkPlayer, faction->getIndex (), - playerIndex); - pushNetworkCommand (&command); - } - - void - Commander::tryPauseGame (bool joinNetworkGame, bool clearCaches) const - { - NetworkCommand - command (this->world, nctPauseResume, 1); - command. - commandTypeId = (clearCaches == true ? 1 : 0); - command. - unitTypeId = (joinNetworkGame == true ? 1 : 0); - pushNetworkCommand (&command); - } - - void - Commander::tryResumeGame (bool joinNetworkGame, bool clearCaches) const - { - NetworkCommand - command (this->world, nctPauseResume, 0); - command. - commandTypeId = (clearCaches == true ? 1 : 0); - command. - unitTypeId = (joinNetworkGame == true ? 1 : 0); - pushNetworkCommand (&command); - } - - void - Commander::tryNetworkPlayerDisconnected (int factionIndex) const - { - //printf("tryNetworkPlayerDisconnected factionIndex: %d\n",factionIndex); - - //if(this->pauseNetworkCommands == true) { - // return; - //} - - NetworkCommand - command (this->world, nctPlayerStatusChange, factionIndex, - npst_Disconnected); - pushNetworkCommand (&command); - } - -// ==================== PRIVATE ==================== - - std::pair < - CommandResult, - string > - Commander::computeResult (const CommandResultContainer & results) const - { - std::pair < - CommandResult, - string > - result (crFailUndefined, ""); - switch (results.size ()) - { - case 0: - return std::pair < CommandResult, string > (crFailUndefined, ""); - case - 1: - return - results. - front (); - default:for (int i = 0; i < (int) results.size (); ++i) - { - if (results[i].first != crSuccess) - { - return std::pair < CommandResult, string > (crSomeFailed, - results[i]. - second); - } - } - break; - } - return std::pair < CommandResult, string > (crSuccess, ""); - } - - std::pair < CommandResult, - string > - Commander::pushNetworkCommand (const NetworkCommand * - networkCommand) const - { - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - std::pair < - CommandResult, - string > - result (crSuccess, ""); - - //validate unit - const Unit * - unit = NULL; - if (networkCommand->getNetworkCommandType () != nctSwitchTeam && - networkCommand->getNetworkCommandType () != nctSwitchTeamVote && - networkCommand->getNetworkCommandType () != nctPauseResume && - networkCommand->getNetworkCommandType () != nctPlayerStatusChange && - networkCommand->getNetworkCommandType () != - nctDisconnectNetworkPlayer) - { - unit = world->findUnitById (networkCommand->getUnitId ()); - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s - %d] Command refers to non existent unit id = %d. Game out of synch.", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, networkCommand->getUnitId ()); - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - char - szMsg[8096] = ""; - snprintf (szMsg, 8096, - "Player detected an error: Command refers to non existent unit id = %d. Game out of synch.", - networkCommand->getUnitId ()); - gameNetworkInterface-> - sendTextMessage (szMsg, -1, true, ""); - } - throw - megaglest_runtime_error (szBuf); - } - } - - //add the command to the interface - gameNetworkInterface->requestCommand (networkCommand); - - //calculate the result of the command - if (unit != NULL - && networkCommand->getNetworkCommandType () == nctGiveCommand) - { - //printf("In [%s::%s Line: %d] result.first = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,result.first); - Command * - command = buildCommand (networkCommand); - result = unit->checkCommand (command); - delete - command; - } - return result; - } - - void - Commander::signalNetworkUpdate (Game * game) - { - updateNetwork (game); - } - - bool - Commander::getReplayCommandListForFrame (int worldFrameCount) - { - bool - haveReplyCommands = false; - if (replayCommandList.empty () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("worldFrameCount = %d replayCommandList.size() = " - MG_SIZE_T_SPECIFIER "\n", worldFrameCount, - replayCommandList.size ()); - - std::vector < NetworkCommand > replayList; - for (unsigned int i = 0; i < replayCommandList.size (); ++i) - { - std::pair < int, - NetworkCommand > & - cmd = replayCommandList[i]; - if (cmd.first <= worldFrameCount) - { - replayList.push_back (cmd.second); - haveReplyCommands = true; - } - } - if (haveReplyCommands == true) - { - replayCommandList.erase (replayCommandList.begin (), - replayCommandList.begin () + - replayList.size ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("worldFrameCount = %d GIVING COMMANDS replayList.size() = " - MG_SIZE_T_SPECIFIER "\n", worldFrameCount, - replayList.size ()); - for (int i = 0; i < (int) replayList.size (); ++i) - { - giveNetworkCommand (&replayList[i]); - } - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - gameNetworkInterface->setKeyframe (worldFrameCount); - } - } - return haveReplyCommands; - } - - bool - Commander::hasReplayCommandListForFrame () const - { - return (replayCommandList.empty () == false); - } - - int - Commander::getReplayCommandListForFrameCount () const - { - return (int) - replayCommandList. - size (); - } - - void - Commander::updateNetwork (Game * game) - { - if (world == NULL) - { - return; - } - NetworkManager & networkManager = NetworkManager::getInstance (); - - //check that this is a keyframe - if (game != NULL) - { - GameSettings * - gameSettings = game->getGameSettings (); - if (networkManager.isNetworkGame () == false || - (world->getFrameCount () % - gameSettings->getNetworkFramePeriod ()) == 0) - { - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] networkManager.isNetworkGame() = %d,world->getFrameCount() = %d, gameSettings->getNetworkFramePeriod() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - networkManager.isNetworkGame (), - world->getFrameCount (), - gameSettings-> - getNetworkFramePeriod ()); - - if (getReplayCommandListForFrame (world->getFrameCount ()) == - false) - { - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled) - perfTimer.start (); - //update the keyframe - gameNetworkInterface->updateKeyframe (world-> - getFrameCount ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && perfTimer.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] gameNetworkInterface->updateKeyframe for %d took %lld msecs\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - world->getFrameCount (), - perfTimer.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled) - perfTimer.start (); - //give pending commands - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("START process: %d network commands in frame: %d\n", - gameNetworkInterface->getPendingCommandCount (), - this->world->getFrameCount ()); - for (int i = 0; - i < gameNetworkInterface->getPendingCommandCount (); - ++i) - { - giveNetworkCommand (gameNetworkInterface-> - getPendingCommand (i)); - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("END process: %d network commands in frame: %d\n", - gameNetworkInterface->getPendingCommandCount (), - this->world->getFrameCount ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && perfTimer.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] giveNetworkCommand took %lld msecs, PendingCommandCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - perfTimer.getMillis (), - gameNetworkInterface-> - getPendingCommandCount ()); - gameNetworkInterface->clearPendingCommands (); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Cleared network commands in frame: %d\n", - this->world->getFrameCount ()); - } - } - } - } - - void - Commander::addToReplayCommandList (NetworkCommand & command, - int worldFrameCount) - { - replayCommandList.push_back (make_pair (worldFrameCount, command)); - } - - void - Commander::giveNetworkCommand (NetworkCommand * networkCommand) const - { - Chrono - chrono; - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - chrono. - start (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [START]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - - world-> - getGame ()-> - addNetworkCommandToReplayList (networkCommand, world->getFrameCount ()); - - networkCommand-> - preprocessNetworkCommand (this->world); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after networkCommand->preprocessNetworkCommand]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - bool - commandWasHandled = false; - // Handle special commands first (that just use network command members as placeholders) - switch (networkCommand->getNetworkCommandType ()) - { - case nctSwitchTeam: - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSwitchTeam\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - commandWasHandled = true; - int - factionIndex = networkCommand->getUnitId (); - int - newTeam = networkCommand->getCommandTypeId (); - - // Auto join empty team or ask players to join - bool - autoJoinTeam = true; - for (int i = 0; i < world->getFactionCount (); ++i) - { - if (newTeam == world->getFaction (i)->getTeam ()) - { - autoJoinTeam = false; - break; - } - } - - if (autoJoinTeam == true) - { - Faction * - faction = world->getFaction (factionIndex); - int - oldTeam = faction->getTeam (); - faction->setTeam (newTeam); - GameSettings * - settings = world->getGameSettingsPtr (); - settings->setTeam (factionIndex, newTeam); - world->getStats ()->setTeam (factionIndex, newTeam); - - if (factionIndex == world->getThisFactionIndex ()) - { - world->setThisTeamIndex (newTeam); - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance (). - getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - settings->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); - ++i) - { - char - szMsg[8096] = ""; - if (lang. - hasString ("PlayerSwitchedTeam", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang. - getString ("PlayerSwitchedTeam", - languageList[i]). - c_str (), - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, newTeam); - } - else - { - snprintf (szMsg, 8096, - "Player %s switched from team# %d to team# %d.", - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, newTeam); - } - bool - localEcho = - lang.isLanguageLocal (languageList[i]); - gameNetworkInterface->sendTextMessage (szMsg, -1, - localEcho, - languageList - [i]); - } - } - world->getGame ()->reInitGUI (); - } - } - else - { - for (int i = 0; i < world->getFactionCount (); ++i) - { - if (newTeam == world->getFaction (i)->getTeam ()) - { - Faction * - faction = world->getFaction (factionIndex); - - SwitchTeamVote - vote; - vote.factionIndex = factionIndex; - vote.allowSwitchTeam = false; - vote.oldTeam = faction->getTeam (); - vote.newTeam = newTeam; - vote.voted = false; - - world->getFaction (i)->setSwitchTeamVote (vote); - } - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSetMeetingPoint\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - break; - - case nctSwitchTeamVote: - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSwitchTeamVote\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - commandWasHandled = true; - - int - votingFactionIndex = networkCommand->getUnitId (); - int - factionIndex = networkCommand->getCommandTypeId (); - bool - allowSwitchTeam = networkCommand->getUnitTypeId () != 0; - - Faction * - faction = world->getFaction (votingFactionIndex); - - SwitchTeamVote * - vote = faction->getSwitchTeamVote (factionIndex); - if (vote == NULL) - { - throw - megaglest_runtime_error ("vote == NULL"); - } - vote->voted = true; - vote->allowSwitchTeam = allowSwitchTeam; - - // Join the new team if > 50 % said yes - int - newTeamTotalMemberCount = 0; - int - newTeamVotedYes = 0; - int - newTeamVotedNo = 0; - - for (int i = 0; i < world->getFactionCount (); ++i) - { - if (vote->newTeam == world->getFaction (i)->getTeam ()) - { - newTeamTotalMemberCount++; - - SwitchTeamVote * - teamVote = - world->getFaction (i)->getSwitchTeamVote (factionIndex); - if (teamVote != NULL && teamVote->voted == true) - { - if (teamVote->allowSwitchTeam == true) - { - newTeamVotedYes++; - } - else - { - newTeamVotedNo++; - } - } - } - } - - // If > 50% of team vote yes, switch th eplayers team - if (newTeamTotalMemberCount > 0 && newTeamVotedYes > 0 && - static_cast < float >(newTeamVotedYes) / static_cast < - float >(newTeamTotalMemberCount) > 0.5) - { - Faction * - faction = world->getFaction (factionIndex); - int - oldTeam = faction->getTeam (); - faction->setTeam (vote->newTeam); - GameSettings * - settings = world->getGameSettingsPtr (); - settings->setTeam (factionIndex, vote->newTeam); - world->getStats ()->setTeam (factionIndex, vote->newTeam); - - if (factionIndex == world->getThisFactionIndex ()) - { - world->setThisTeamIndex (vote->newTeam); - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance (). - getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - settings->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); - ++i) - { - char - szMsg[8096] = ""; - if (lang. - hasString ("PlayerSwitchedTeam", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang. - getString ("PlayerSwitchedTeam", - languageList[i]). - c_str (), - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, vote->newTeam); - } - else - { - snprintf (szMsg, 8096, - "Player %s switched from team# %d to team# %d.", - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, vote->newTeam); - } - bool - localEcho = - lang.isLanguageLocal (languageList[i]); - gameNetworkInterface->sendTextMessage (szMsg, -1, - localEcho, - languageList - [i]); - } - } - world->getGame ()->reInitGUI (); - } - } - else if (newTeamTotalMemberCount == - (newTeamVotedYes + newTeamVotedNo)) - { - if (factionIndex == world->getThisFactionIndex ()) - { - GameSettings * - settings = world->getGameSettingsPtr (); - Faction * - faction = world->getFaction (factionIndex); - int - oldTeam = faction->getTeam (); - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance (). - getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - settings->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); - ++i) - { - char - szMsg[8096] = ""; - if (lang. - hasString ("PlayerSwitchedTeamDenied", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang. - getString - ("PlayerSwitchedTeamDenied", - languageList[i]).c_str (), - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, vote->newTeam); - } - else - { - snprintf (szMsg, 8096, - "Player %s was denied the request to switch from team# %d to team# %d.", - settings-> - getNetworkPlayerName (factionIndex). - c_str (), oldTeam, vote->newTeam); - } - bool - localEcho = - lang.isLanguageLocal (languageList[i]); - gameNetworkInterface->sendTextMessage (szMsg, -1, - localEcho, - languageList - [i]); - } - } - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSetMeetingPoint\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - break; - - case nctDisconnectNetworkPlayer: - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctDisconnectNetworkPlayer\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - commandWasHandled = true; - - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole - role = networkManager.getNetworkRole (); - //GameSettings *settings = world->getGameSettingsPtr(); - - if (role == nrServer) - { - //int factionIndex = networkCommand->getUnitId(); - int - playerIndex = networkCommand->getCommandTypeId (); - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - ServerInterface * - server = networkManager.getServerInterface (); - if (server != NULL - && server->isClientConnected (playerIndex) == true) - { - - MutexSafeWrapper - safeMutex (server->getSlotMutex (playerIndex), - CODE_AT_LINE); - ConnectionSlot * - slot = server->getSlot (playerIndex, false); - if (slot != NULL) - { - safeMutex.ReleaseLock (); - NetworkMessageQuit - networkMessageQuit; - slot->sendMessage (&networkMessageQuit); - sleep (5); - - //printf("Sending nctDisconnectNetworkPlayer\n"); - server = - networkManager.getServerInterface (false); - if (server != NULL) - { - MutexSafeWrapper - safeMutex2 (server-> - getSlotMutex (playerIndex), - CODE_AT_LINE); - slot = server->getSlot (playerIndex, false); - if (slot != NULL) - { - safeMutex2.ReleaseLock (); - slot->close (); - } - } - } - } - } - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctDisconnectNetworkPlayer\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - break; - - case nctPauseResume: - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctPauseResume\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - commandWasHandled = true; - - bool - pauseGame = networkCommand->getUnitId () != 0; - bool - clearCaches = (networkCommand->getCommandTypeId () == 1); - bool - joinNetworkGame = (networkCommand->getUnitTypeId () == 1); - Game * - game = this->world->getGame (); - - //printf("nctPauseResume pauseGame = %d\n",pauseGame); - game->setPaused (pauseGame, true, clearCaches, joinNetworkGame); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctPauseResume\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - break; - - case nctPlayerStatusChange: - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctPlayerStatusChange\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - commandWasHandled = true; - - int - factionIndex = networkCommand->getUnitId (); - int - playerStatus = networkCommand->getCommandTypeId (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "nctPlayerStatusChange factionIndex = %d playerStatus = %d\n", - factionIndex, playerStatus); - - //printf("#1 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus); - - GameSettings * - settings = world->getGameSettingsPtr (); - if (playerStatus == npst_Disconnected) - { - //printf("Commander nctPlayerStatusChange factionIndex: %d\n",factionIndex); - - settings->setNetworkPlayerStatuses (factionIndex, - npst_Disconnected); - - //printf("nctPlayerStatusChange -> faction->getPersonalityType() = %d index [%d] control [%d] networkstatus [%d]\n", - // world->getFaction(factionIndex)->getPersonalityType(),world->getFaction(factionIndex)->getIndex(),world->getFaction(factionIndex)->getControlType(),settings->getNetworkPlayerStatuses(factionIndex)); - - //printf("#2 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus); - settings->setFactionControl (factionIndex, ctCpuUltra); - settings->setResourceMultiplierIndex (factionIndex, - settings-> - getFallbackCpuMultiplier - ()); - //Game *game = this->world->getGame(); - //game->get - Faction * - faction = this->world->getFaction (factionIndex); - faction->setControlType (ctCpuUltra); - - if (!world->getGame ()->getGameOver () - && !this->world->getGame ()-> - factionLostGame (factionIndex)) - { - // use the fallback multiplier here - - // mark player as "leaver" - this->world->getStats ()-> - setPlayerLeftBeforeEnd (factionIndex, true); - // set disconnect time for endgame stats - this->world->getStats ()->setTimePlayerLeft (factionIndex, - this->world-> - getStats ()-> - getFramesToCalculatePlaytime - ()); - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctPlayerStatusChange\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - break; - - } - - if (commandWasHandled == false) - { - Unit * - unit = world->findUnitById (networkCommand->getUnitId ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after world->findUnitById]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Running command NetworkCommandType = %d, unitid = %d [%p] factionindex = %d\n", - networkCommand->getNetworkCommandType (), - networkCommand->getUnitId (), unit, - (unit != NULL ? unit->getFactionIndex () : -1)); - //execute command, if unit is still alive - if (unit != NULL) - { - switch (networkCommand->getNetworkCommandType ()) - { - case nctGiveCommand: - { - assert (networkCommand->getCommandTypeId () != - CommandType::invalidId); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - networkCommand->getUnitId ()); - - Command * - command = buildCommand (networkCommand); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after buildCommand]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] command = %p\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - command); - - unit->giveCommand (command, - (networkCommand->getWantQueue () != - 0)); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after unit->giveCommand]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - networkCommand->getUnitId ()); - } - break; - case nctCancelCommand: - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctCancelCommand\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - unit->cancelCommand (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after unit->cancelCommand]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctCancelCommand\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - break; - case nctSetMeetingPoint: - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSetMeetingPoint\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - unit->setMeetingPos (networkCommand->getPosition ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found nctSetMeetingPoint\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - break; - - default: - assert (false); - break; - } - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] NULL Unit for id = %d, networkCommand->getNetworkCommandType() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - networkCommand->getUnitId (), - networkCommand-> - getNetworkCommandType ()); - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld [END]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - } - -// Reconstruct a network command received. - Command * - Commander::buildCommand (const NetworkCommand * networkCommand) const - { - // Check a new command is actually being given (and not a cancel command, switch team etc.). - assert (networkCommand->getNetworkCommandType () == nctGiveCommand); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] networkCommand [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - networkCommand->toString ().c_str ()); - - // Check there is a world. - if (world == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] world == NULL for unit with id: %d", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, networkCommand->getUnitId ()); - throw - megaglest_runtime_error (szBuf); - } - - // Get the unit. - Unit * - target = NULL; - const CommandType * - ct = NULL; - const Unit * - unit = world->findUnitById (networkCommand->getUnitId ()); - - // Validate unit is in game. - if (unit == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Can not find unit with id: %d. Game out of synch.", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, networkCommand->getUnitId ()); - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - char - szMsg[8096] = ""; - snprintf (szMsg, 8096, - "Player detected an error: Can not find unit with id: %d. Game out of synch.", - networkCommand->getUnitId ()); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - } - - throw - megaglest_runtime_error (szBuf); - } - - // Get the command type for the unit. - ct = - unit->getType ()->findCommandTypeById (networkCommand-> - getCommandTypeId ()); - - // Check that the unit from the network command is the same faction as the unit in the local game. - if (unit->getFaction ()->getIndex () != - networkCommand->getUnitFactionIndex ()) - { - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nUnit / Faction mismatch for network command = [%s]\n%s\nfor unit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", - __FILE__, __FUNCTION__, __LINE__, - networkCommand->toString ().c_str (), - unit->getType ()->getCommandTypeListDesc ().c_str (), - unit->getId (), unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - szBuf); - //std::string worldLog = world->DumpWorldToLog(); - world->DumpWorldToLog (); - - // Broadcast the error if player is still connected and print locally. - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL - && gameNetworkInterface->isConnected () == true) - { - char - szMsg[8096] = ""; - snprintf (szMsg, 8096, - "Player detected an error: Unit / Faction mismatch for unitId: %d", - networkCommand->getUnitId ()); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - snprintf (szMsg, 8096, - "Local faction index = %d, remote index = %d. Game out of synch.", - unit->getFaction ()->getIndex (), - networkCommand->getUnitFactionIndex ()); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - - } - - // Else if it's a network game but the user disconnected, print the error locally only. - else if (gameNetworkInterface != NULL) - { - char - szMsg[8096] = ""; - snprintf (szMsg, 8096, - "Player detected an error: Connection lost, possible Unit / Faction mismatch for unitId: %d", - networkCommand->getUnitId ()); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - snprintf (szMsg, 8096, - "Local faction index = %d, remote index = %d. Game out of synch.", - unit->getFaction ()->getIndex (), - networkCommand->getUnitFactionIndex ()); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - } - - // Kill the game. - std::string sError = - "Error [#1]: Game is out of sync (Unit / Faction mismatch)\nplease check log files for details."; - throw - megaglest_runtime_error (sError); - } - - const UnitType * - unitType = - world->findUnitTypeById (unit->getFaction ()->getType (), - networkCommand->getUnitTypeId ()); - - // debug test! - //throw megaglest_runtime_error("Test missing command type!"); - - //validate command type - - // !!!Test out of synch behaviour - //ct = NULL; - - // Check if the command was for the unit before it morphed, if so cancel it. - bool - isCancelPreMorphCommand = false; - if (ct == NULL && unit->getPreMorphType () != NULL) - { - const CommandType * - ctPreMorph = - unit->getPreMorphType ()->findCommandTypeById (networkCommand-> - getCommandTypeId - ()); - if (ctPreMorph != NULL) - { - ct = unit->getType ()->getFirstCtOfClass (ccStop); - isCancelPreMorphCommand = true; - } - } - - // Throw an error if a valid command for the unit is still not found. - if (ct == NULL) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nCan not find command type for network command = [%s]\n%s\nfor unit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nUnit Type Info:\n[%s]\nNetwork unit type:\n[%s]\nisCancelPreMorphCommand: %d\nGame out of synch.", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, - networkCommand->toString ().c_str (), - unit->getType ()->getCommandTypeListDesc ().c_str (), - unit->getId (), unit->getFullName (false).c_str (), - unit->getDesc (false).c_str (), - unit->getFaction ()->getIndex (), - unit->getType ()->toString ().c_str (), - (unitType != - NULL ? unitType->getName (false).c_str () : "null"), - isCancelPreMorphCommand); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", szBuf); - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", szBuf); - world->DumpWorldToLog (); - - GameNetworkInterface * - gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - char - szMsg[8096] = ""; - snprintf (szMsg, 8096, - "Player detected an error: Can not find command type: %d for unitId: %d [%s]. isCancelPreMorphCommand: %d Game out of synch.", - networkCommand->getCommandTypeId (), - networkCommand->getUnitId (), - (unitType != - NULL ? unitType->getName (false).c_str () : "null"), - isCancelPreMorphCommand); - gameNetworkInterface->sendTextMessage (szMsg, -1, true, ""); - } - - std::string sError = - "Error [#3]: Game is out of sync, please check log files for details."; - //abort(); - throw - megaglest_runtime_error (sError); - } - - CardinalDir - facing; - // Get direction of the command target unit. - if (isCancelPreMorphCommand == false) - { - // If target is a building. - if (ct->getClass () == ccBuild) - { - // Check the target building ID is valid. If not, throw an error. - /// TODO: What is happening here? The error returned does not match the condition. Why is there a constant of 4? - if (networkCommand->getTargetId () < 0 - || networkCommand->getTargetId () >= 4) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "networkCommand->getTargetId() >= 0 && networkCommand->getTargetId() < 4, [%s]", - networkCommand->toString ().c_str ()); - throw - megaglest_runtime_error (szBuf); - } - facing = CardinalDir (networkCommand->getTargetId ()); - } - // Get the target unit if the ID is valid. - else if (networkCommand->getTargetId () != Unit::invalidId) - { - target = world->findUnitById (networkCommand->getTargetId ()); - } - } - - // Create the command. - Command * - command = NULL; - if (isCancelPreMorphCommand == false) - { - if (unitType != NULL) - { - command = - new Command (ct, networkCommand->getPosition (), unitType, - facing); - } - else if (target == NULL) - { - command = new Command (ct, networkCommand->getPosition ()); - } - else - { - command = new Command (ct, target); - } - } - else - { - command = new Command (ct, NULL); - } - - // Add in any special state - CommandStateType - commandStateType = networkCommand->getCommandStateType (); - int - commandStateValue = networkCommand->getCommandStateValue (); - - command->setStateType (commandStateType); - command->setStateValue (commandStateValue); - command->setUnitCommandGroupId (networkCommand-> - getUnitCommandGroupId ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - //issue command - return command; - } - - } + Glest { + namespace + Game { + + // ===================================================== + // class Commander + // ===================================================== + Commander::Commander() { + this->world = NULL; + this-> + pauseNetworkCommands = false; + } + + Commander::~ + Commander() { + } + + void + Commander::init(World * world) { + this->world = world; + } + + bool + Commander::canSubmitCommandType(const Unit * unit, + const CommandType * commandType) const { + bool + canSubmitCommand = true; + const MorphCommandType * + mct = dynamic_cast (commandType); + if (mct && unit->getCommandSize() > 0) { + Command * + cur_command = unit->getCurrCommand(); + if (cur_command != NULL) { + const MorphCommandType * + cur_mct = dynamic_cast (cur_command->getCommandType()); + if (cur_mct && unit->getCurrSkill() + && unit->getCurrSkill()->getClass() == scMorph) { + const UnitType * + morphUnitType = mct->getMorphUnit(); + const UnitType * + cur_morphUnitType = cur_mct->getMorphUnit(); + + if (morphUnitType != NULL && cur_morphUnitType != NULL + && morphUnitType->getId() == + cur_morphUnitType->getId()) { + canSubmitCommand = false; + } + } + } + } + return + canSubmitCommand; + } + + std::pair < CommandResult, + string > Commander::tryGiveCommand(const Selection * selection, + const CommandType * commandType, + const Vec2i & pos, + const UnitType * unitType, + CardinalDir facing, bool tryQueue, + Unit * targetUnit) const { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->pauseNetworkCommands == true) { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + std::pair < + CommandResult, + string > + result(crFailUndefined, ""); + if (selection->isEmpty() == false && commandType != NULL) { + Vec2i + refPos; + CommandResultContainer + results; + + refPos = world->getMap()->computeRefPos(selection); + + const Unit * + builderUnit = + world->getMap()->findClosestUnitToPos(selection, pos, unitType); + + int + builderUnitId = builderUnit->getId(); + CommandStateType + commandStateType = cst_None; + int + commandStateValue = -1; + + int + unitCommandGroupId = -1; + if (selection->getCount() > 1) { + unitCommandGroupId = world->getNextCommandGroupId(); + } + + //give orders to all selected units + for (int i = 0; i < selection->getCount(); ++i) { + const Unit * + unit = selection->getUnit(i); + + + std::pair < CommandResult, string > resultCur(crFailUndefined, + ""); + bool + canSubmitCommand = canSubmitCommandType(unit, commandType); + if (canSubmitCommand == true) { + int + unitId = unit->getId(); + Vec2i + currPos = + world->getMap()->computeDestPos(refPos, + unit-> + getPosNotThreadSafe(), + pos); + + Vec2i + usePos = currPos; + const CommandType * + useCommandtype = commandType; + if (dynamic_cast < + const BuildCommandType *>(commandType) != NULL) { + usePos = pos; + if (builderUnit->getId() != unitId) { + useCommandtype = + unit->getType()-> + getFirstRepairCommand(unitType); + commandStateType = cst_linkedUnit; + commandStateValue = builderUnitId; + } else { + commandStateType = cst_None; + commandStateValue = -1; + } + } + + if (useCommandtype != NULL) { + NetworkCommand + networkCommand(this->world, nctGiveCommand, unitId, + useCommandtype->getId(), usePos, + unitType->getId(), + (targetUnit != + NULL ? targetUnit->getId() : -1), + facing, tryQueue, commandStateType, + commandStateValue, unitCommandGroupId); + + //every unit is ordered to a the position + resultCur = pushNetworkCommand(&networkCommand); + } + } + + results.push_back(resultCur); + } + + return computeResult(results); + } + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + std::pair < CommandResult, + string > Commander::tryGiveCommand(const Unit * unit, + const CommandType * commandType, + const Vec2i & pos, + const UnitType * unitType, + CardinalDir facing, bool tryQueue, + Unit * targetUnit, + int unitGroupCommandId) const { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->pauseNetworkCommands == true) { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono.start(); + + assert(this->world != NULL); + assert(unit != NULL); + assert(commandType != NULL); + assert(unitType != NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + bool + canSubmitCommand = canSubmitCommandType(unit, commandType); + if (canSubmitCommand == true) { + NetworkCommand + networkCommand(this->world, nctGiveCommand, unit->getId(), + commandType->getId(), pos, unitType->getId(), + (targetUnit != NULL ? targetUnit->getId() : -1), + facing, tryQueue, cst_None, -1, unitGroupCommandId); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + result = pushNetworkCommand(&networkCommand); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + return result; + } + + std::pair < CommandResult, + string > Commander::tryGiveCommand(const Selection * selection, + CommandClass commandClass, + const Vec2i & pos, + const Unit * targetUnit, + bool tryQueue) const { + + if (this->pauseNetworkCommands == true) { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + std::pair < + CommandResult, + string > + result(crFailUndefined, ""); + if (selection->isEmpty() == false) { + Vec2i + refPos, + currPos; + CommandResultContainer + results; + + refPos = world->getMap()->computeRefPos(selection); + + int + unitCommandGroupId = -1; + if (selection->getCount() > 1) { + unitCommandGroupId = world->getNextCommandGroupId(); + } + + //give orders to all selected units + for (int i = 0; i < selection->getCount(); ++i) { + const Unit * + unit = selection->getUnit(i); + const CommandType * + ct = unit->getType()->getFirstCtOfClass(commandClass); + if (ct != NULL) { + std::pair < CommandResult, + string > resultCur(crFailUndefined, ""); + + bool + canSubmitCommand = canSubmitCommandType(unit, ct); + if (canSubmitCommand == true) { + + int + targetId = + targetUnit == + NULL ? Unit::invalidId : targetUnit->getId(); + int + unitId = selection->getUnit(i)->getId(); + Vec2i + currPos = world->getMap()->computeDestPos(refPos, + selection-> + getUnit + (i)-> + getPosNotThreadSafe + (), pos); + NetworkCommand + networkCommand(this->world, nctGiveCommand, + unitId, ct->getId(), currPos, -1, + targetId, -1, tryQueue, cst_None, -1, + unitCommandGroupId); + + //every unit is ordered to a different pos + resultCur = pushNetworkCommand(&networkCommand); + } + results.push_back(resultCur); + } else { + results.push_back(std::pair < CommandResult, + string >(crFailUndefined, "")); + } + } + return computeResult(results); + } else { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + } + + std::pair < CommandResult, + string > Commander::tryGiveCommand(const Selection * selection, + const CommandType * commandType, + const Vec2i & pos, + const Unit * targetUnit, + bool tryQueue) const { + + if (this->pauseNetworkCommands == true) { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + std::pair < + CommandResult, + string > + result(crFailUndefined, ""); + + if (!selection->isEmpty() && commandType != NULL) { + Vec2i + refPos; + CommandResultContainer + results; + + refPos = world->getMap()->computeRefPos(selection); + + int + unitCommandGroupId = -1; + if (selection->getCount() > 1) { + unitCommandGroupId = world->getNextCommandGroupId(); + } + + //give orders to all selected units + for (int i = 0; i < selection->getCount(); ++i) { + const Unit * + unit = selection->getUnit(i); + assert(unit != NULL); + + std::pair < CommandResult, string > resultCur(crFailUndefined, + ""); + + bool + canSubmitCommand = canSubmitCommandType(unit, commandType); + if (canSubmitCommand == true) { + int + targetId = + targetUnit == + NULL ? Unit::invalidId : targetUnit->getId(); + int + unitId = unit->getId(); + Vec2i + currPos = + world->getMap()->computeDestPos(refPos, + unit-> + getPosNotThreadSafe(), + pos); + NetworkCommand + networkCommand(this->world, nctGiveCommand, unitId, + commandType->getId(), currPos, -1, + targetId, -1, tryQueue, cst_None, -1, + unitCommandGroupId); + + //every unit is ordered to a different position + resultCur = pushNetworkCommand(&networkCommand); + } + results.push_back(resultCur); + } + + return computeResult(results); + } else { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + } + + //auto command + std::pair < CommandResult, + string > Commander::tryGiveCommand(const Selection * selection, + const Vec2i & pos, + const Unit * targetUnit, + bool tryQueue, + int unitCommandGroupId) const { + + if (this->pauseNetworkCommands == true) { + return std::pair < CommandResult, string >(crFailUndefined, ""); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + + if (selection->isEmpty() == false) { + Vec2i + refPos, + currPos; + CommandResultContainer + results; + + if (unitCommandGroupId == -1 && selection->getCount() > 1) { + unitCommandGroupId = world->getNextCommandGroupId(); + } + + //give orders to all selected units + refPos = world->getMap()->computeRefPos(selection); + for (int i = 0; i < selection->getCount(); ++i) { + //every unit is ordered to a different pos + const Unit * + unit = selection->getUnit(i); + assert(unit != NULL); + + currPos = + world->getMap()->computeDestPos(refPos, + unit-> + getPosNotThreadSafe(), + pos); + + //get command type + const CommandType * + commandType = unit->computeCommandType(pos, targetUnit); + + //give commands + if (commandType != NULL) { + int + targetId = + targetUnit == + NULL ? Unit::invalidId : targetUnit->getId(); + int + unitId = unit->getId(); + + std::pair < CommandResult, + string > resultCur(crFailUndefined, ""); + + bool + canSubmitCommand = + canSubmitCommandType(unit, commandType); + if (canSubmitCommand == true) { + NetworkCommand + networkCommand(this->world, nctGiveCommand, + unitId, commandType->getId(), currPos, + -1, targetId, -1, tryQueue, cst_None, + -1, unitCommandGroupId); + resultCur = pushNetworkCommand(&networkCommand); + } + results.push_back(resultCur); + } else if (unit->isMeetingPointSettable() == true) { + NetworkCommand + command(this->world, nctSetMeetingPoint, + unit->getId(), -1, currPos, -1, -1, -1, false, + cst_None, -1, unitCommandGroupId); + + std::pair < CommandResult, string > resultCur = + pushNetworkCommand(&command); + results.push_back(resultCur); + } else { + results.push_back(std::pair < CommandResult, + string >(crFailUndefined, "")); + } + } + result = computeResult(results); + } + + return result; + } + + CommandResult + Commander::tryCancelCommand(const Selection * selection) const { + if (this->pauseNetworkCommands == true) { + return crFailUndefined; + } + + int + unitCommandGroupId = -1; + if (selection->getCount() > 1) { + unitCommandGroupId = world->getNextCommandGroupId(); + } + + for (int i = 0; i < selection->getCount(); ++i) { + NetworkCommand + command(this->world, nctCancelCommand, + selection->getUnit(i)->getId(), -1, Vec2i(0), -1, -1, + -1, false, cst_None, -1, unitCommandGroupId); + pushNetworkCommand(&command); + } + + return crSuccess; + } + + void + Commander::trySetMeetingPoint(const Unit * unit, const Vec2i & pos) const { + if (this->pauseNetworkCommands == true) { + return; + } + + NetworkCommand + command(this->world, nctSetMeetingPoint, unit->getId(), -1, pos); + pushNetworkCommand(&command); + } + + void + Commander::trySwitchTeam(const Faction * faction, int teamIndex) const { + if (this->pauseNetworkCommands == true) { + return; + } + + NetworkCommand + command(this->world, nctSwitchTeam, faction->getIndex(), teamIndex); + pushNetworkCommand(&command); + } + + void + Commander::trySwitchTeamVote(const Faction * faction, + SwitchTeamVote * vote) const { + if (this->pauseNetworkCommands == true) { + return; + } + + NetworkCommand + command(this->world, nctSwitchTeamVote, faction->getIndex(), + vote->factionIndex, Vec2i(0), vote->allowSwitchTeam); + pushNetworkCommand(&command); + } + + void + Commander::tryDisconnectNetworkPlayer(const Faction * faction, + int playerIndex) const { + NetworkCommand + command(this->world, nctDisconnectNetworkPlayer, faction->getIndex(), + playerIndex); + pushNetworkCommand(&command); + } + + void + Commander::tryPauseGame(bool joinNetworkGame, bool clearCaches) const { + NetworkCommand + command(this->world, nctPauseResume, 1); + command. + commandTypeId = (clearCaches == true ? 1 : 0); + command. + unitTypeId = (joinNetworkGame == true ? 1 : 0); + pushNetworkCommand(&command); + } + + void + Commander::tryResumeGame(bool joinNetworkGame, bool clearCaches) const { + NetworkCommand + command(this->world, nctPauseResume, 0); + command. + commandTypeId = (clearCaches == true ? 1 : 0); + command. + unitTypeId = (joinNetworkGame == true ? 1 : 0); + pushNetworkCommand(&command); + } + + void + Commander::tryNetworkPlayerDisconnected(int factionIndex) const { + //printf("tryNetworkPlayerDisconnected factionIndex: %d\n",factionIndex); + + //if(this->pauseNetworkCommands == true) { + // return; + //} + + NetworkCommand + command(this->world, nctPlayerStatusChange, factionIndex, + npst_Disconnected); + pushNetworkCommand(&command); + } + + // ==================== PRIVATE ==================== + + std::pair < + CommandResult, + string > + Commander::computeResult(const CommandResultContainer & results) const { + std::pair < + CommandResult, + string > + result(crFailUndefined, ""); + switch (results.size()) { + case 0: + return std::pair < CommandResult, string >(crFailUndefined, ""); + case + 1: + return + results. + front(); + default:for (int i = 0; i < (int) results.size(); ++i) { + if (results[i].first != crSuccess) { + return std::pair < CommandResult, string >(crSomeFailed, + results[i]. + second); + } + } + break; + } + return std::pair < CommandResult, string >(crSuccess, ""); + } + + std::pair < CommandResult, + string > + Commander::pushNetworkCommand(const NetworkCommand * + networkCommand) const { + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + std::pair < + CommandResult, + string > + result(crSuccess, ""); + + //validate unit + const Unit * + unit = NULL; + if (networkCommand->getNetworkCommandType() != nctSwitchTeam && + networkCommand->getNetworkCommandType() != nctSwitchTeamVote && + networkCommand->getNetworkCommandType() != nctPauseResume && + networkCommand->getNetworkCommandType() != nctPlayerStatusChange && + networkCommand->getNetworkCommandType() != + nctDisconnectNetworkPlayer) { + unit = world->findUnitById(networkCommand->getUnitId()); + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s - %d] Command refers to non existent unit id = %d. Game out of synch.", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, networkCommand->getUnitId()); + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + char + szMsg[8096] = ""; + snprintf(szMsg, 8096, + "Player detected an error: Command refers to non existent unit id = %d. Game out of synch.", + networkCommand->getUnitId()); + gameNetworkInterface-> + sendTextMessage(szMsg, -1, true, ""); + } + throw + megaglest_runtime_error(szBuf); + } + } + + //add the command to the interface + gameNetworkInterface->requestCommand(networkCommand); + + //calculate the result of the command + if (unit != NULL + && networkCommand->getNetworkCommandType() == nctGiveCommand) { + //printf("In [%s::%s Line: %d] result.first = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,result.first); + Command * + command = buildCommand(networkCommand); + result = unit->checkCommand(command); + delete + command; + } + return result; + } + + void + Commander::signalNetworkUpdate(Game * game) { + updateNetwork(game); + } + + bool + Commander::getReplayCommandListForFrame(int worldFrameCount) { + bool + haveReplyCommands = false; + if (replayCommandList.empty() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("worldFrameCount = %d replayCommandList.size() = " + MG_SIZE_T_SPECIFIER "\n", worldFrameCount, + replayCommandList.size()); + + std::vector < NetworkCommand > replayList; + for (unsigned int i = 0; i < replayCommandList.size(); ++i) { + std::pair < int, + NetworkCommand > & + cmd = replayCommandList[i]; + if (cmd.first <= worldFrameCount) { + replayList.push_back(cmd.second); + haveReplyCommands = true; + } + } + if (haveReplyCommands == true) { + replayCommandList.erase(replayCommandList.begin(), + replayCommandList.begin() + + replayList.size()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("worldFrameCount = %d GIVING COMMANDS replayList.size() = " + MG_SIZE_T_SPECIFIER "\n", worldFrameCount, + replayList.size()); + for (int i = 0; i < (int) replayList.size(); ++i) { + giveNetworkCommand(&replayList[i]); + } + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + gameNetworkInterface->setKeyframe(worldFrameCount); + } + } + return haveReplyCommands; + } + + bool + Commander::hasReplayCommandListForFrame() const { + return (replayCommandList.empty() == false); + } + + int + Commander::getReplayCommandListForFrameCount() const { + return (int) + replayCommandList. + size(); + } + + void + Commander::updateNetwork(Game * game) { + if (world == NULL) { + return; + } + NetworkManager & networkManager = NetworkManager::getInstance(); + + //check that this is a keyframe + if (game != NULL) { + GameSettings * + gameSettings = game->getGameSettings(); + if (networkManager.isNetworkGame() == false || + (world->getFrameCount() % + gameSettings->getNetworkFramePeriod()) == 0) { + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] networkManager.isNetworkGame() = %d,world->getFrameCount() = %d, gameSettings->getNetworkFramePeriod() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + networkManager.isNetworkGame(), + world->getFrameCount(), + gameSettings-> + getNetworkFramePeriod()); + + if (getReplayCommandListForFrame(world->getFrameCount()) == + false) { + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled) + perfTimer.start(); + //update the keyframe + gameNetworkInterface->updateKeyframe(world-> + getFrameCount()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && perfTimer.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] gameNetworkInterface->updateKeyframe for %d took %lld msecs\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + world->getFrameCount(), + perfTimer.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled) + perfTimer.start(); + //give pending commands + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("START process: %d network commands in frame: %d\n", + gameNetworkInterface->getPendingCommandCount(), + this->world->getFrameCount()); + for (int i = 0; + i < gameNetworkInterface->getPendingCommandCount(); + ++i) { + giveNetworkCommand(gameNetworkInterface-> + getPendingCommand(i)); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("END process: %d network commands in frame: %d\n", + gameNetworkInterface->getPendingCommandCount(), + this->world->getFrameCount()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && perfTimer.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] giveNetworkCommand took %lld msecs, PendingCommandCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + perfTimer.getMillis(), + gameNetworkInterface-> + getPendingCommandCount()); + gameNetworkInterface->clearPendingCommands(); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Cleared network commands in frame: %d\n", + this->world->getFrameCount()); + } + } + } + } + + void + Commander::addToReplayCommandList(NetworkCommand & command, + int worldFrameCount) { + replayCommandList.push_back(make_pair(worldFrameCount, command)); + } + + void + Commander::giveNetworkCommand(NetworkCommand * networkCommand) const { + Chrono + chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + chrono. + start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [START]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + + world-> + getGame()-> + addNetworkCommandToReplayList(networkCommand, world->getFrameCount()); + + networkCommand-> + preprocessNetworkCommand(this->world); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after networkCommand->preprocessNetworkCommand]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + bool + commandWasHandled = false; + // Handle special commands first (that just use network command members as placeholders) + switch (networkCommand->getNetworkCommandType()) { + case nctSwitchTeam: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSwitchTeam\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + commandWasHandled = true; + int + factionIndex = networkCommand->getUnitId(); + int + newTeam = networkCommand->getCommandTypeId(); + + // Auto join empty team or ask players to join + bool + autoJoinTeam = true; + for (int i = 0; i < world->getFactionCount(); ++i) { + if (newTeam == world->getFaction(i)->getTeam()) { + autoJoinTeam = false; + break; + } + } + + if (autoJoinTeam == true) { + Faction * + faction = world->getFaction(factionIndex); + int + oldTeam = faction->getTeam(); + faction->setTeam(newTeam); + GameSettings * + settings = world->getGameSettingsPtr(); + settings->setTeam(factionIndex, newTeam); + world->getStats()->setTeam(factionIndex, newTeam); + + if (factionIndex == world->getThisFactionIndex()) { + world->setThisTeamIndex(newTeam); + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance(). + getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + settings->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); + ++i) { + char + szMsg[8096] = ""; + if (lang. + hasString("PlayerSwitchedTeam", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang. + getString("PlayerSwitchedTeam", + languageList[i]). + c_str(), + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, newTeam); + } else { + snprintf(szMsg, 8096, + "Player %s switched from team# %d to team# %d.", + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, newTeam); + } + bool + localEcho = + lang.isLanguageLocal(languageList[i]); + gameNetworkInterface->sendTextMessage(szMsg, -1, + localEcho, + languageList + [i]); + } + } + world->getGame()->reInitGUI(); + } + } else { + for (int i = 0; i < world->getFactionCount(); ++i) { + if (newTeam == world->getFaction(i)->getTeam()) { + Faction * + faction = world->getFaction(factionIndex); + + SwitchTeamVote + vote; + vote.factionIndex = factionIndex; + vote.allowSwitchTeam = false; + vote.oldTeam = faction->getTeam(); + vote.newTeam = newTeam; + vote.voted = false; + + world->getFaction(i)->setSwitchTeamVote(vote); + } + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSetMeetingPoint\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + break; + + case nctSwitchTeamVote: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSwitchTeamVote\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + commandWasHandled = true; + + int + votingFactionIndex = networkCommand->getUnitId(); + int + factionIndex = networkCommand->getCommandTypeId(); + bool + allowSwitchTeam = networkCommand->getUnitTypeId() != 0; + + Faction * + faction = world->getFaction(votingFactionIndex); + + SwitchTeamVote * + vote = faction->getSwitchTeamVote(factionIndex); + if (vote == NULL) { + throw + megaglest_runtime_error("vote == NULL"); + } + vote->voted = true; + vote->allowSwitchTeam = allowSwitchTeam; + + // Join the new team if > 50 % said yes + int + newTeamTotalMemberCount = 0; + int + newTeamVotedYes = 0; + int + newTeamVotedNo = 0; + + for (int i = 0; i < world->getFactionCount(); ++i) { + if (vote->newTeam == world->getFaction(i)->getTeam()) { + newTeamTotalMemberCount++; + + SwitchTeamVote * + teamVote = + world->getFaction(i)->getSwitchTeamVote(factionIndex); + if (teamVote != NULL && teamVote->voted == true) { + if (teamVote->allowSwitchTeam == true) { + newTeamVotedYes++; + } else { + newTeamVotedNo++; + } + } + } + } + + // If > 50% of team vote yes, switch th eplayers team + if (newTeamTotalMemberCount > 0 && newTeamVotedYes > 0 && + static_cast (newTeamVotedYes) / static_cast < + float>(newTeamTotalMemberCount) > 0.5) { + Faction * + faction = world->getFaction(factionIndex); + int + oldTeam = faction->getTeam(); + faction->setTeam(vote->newTeam); + GameSettings * + settings = world->getGameSettingsPtr(); + settings->setTeam(factionIndex, vote->newTeam); + world->getStats()->setTeam(factionIndex, vote->newTeam); + + if (factionIndex == world->getThisFactionIndex()) { + world->setThisTeamIndex(vote->newTeam); + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance(). + getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + settings->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); + ++i) { + char + szMsg[8096] = ""; + if (lang. + hasString("PlayerSwitchedTeam", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang. + getString("PlayerSwitchedTeam", + languageList[i]). + c_str(), + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, vote->newTeam); + } else { + snprintf(szMsg, 8096, + "Player %s switched from team# %d to team# %d.", + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, vote->newTeam); + } + bool + localEcho = + lang.isLanguageLocal(languageList[i]); + gameNetworkInterface->sendTextMessage(szMsg, -1, + localEcho, + languageList + [i]); + } + } + world->getGame()->reInitGUI(); + } + } else if (newTeamTotalMemberCount == + (newTeamVotedYes + newTeamVotedNo)) { + if (factionIndex == world->getThisFactionIndex()) { + GameSettings * + settings = world->getGameSettingsPtr(); + Faction * + faction = world->getFaction(factionIndex); + int + oldTeam = faction->getTeam(); + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance(). + getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + settings->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); + ++i) { + char + szMsg[8096] = ""; + if (lang. + hasString("PlayerSwitchedTeamDenied", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang. + getString + ("PlayerSwitchedTeamDenied", + languageList[i]).c_str(), + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, vote->newTeam); + } else { + snprintf(szMsg, 8096, + "Player %s was denied the request to switch from team# %d to team# %d.", + settings-> + getNetworkPlayerName(factionIndex). + c_str(), oldTeam, vote->newTeam); + } + bool + localEcho = + lang.isLanguageLocal(languageList[i]); + gameNetworkInterface->sendTextMessage(szMsg, -1, + localEcho, + languageList + [i]); + } + } + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSetMeetingPoint\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + break; + + case nctDisconnectNetworkPlayer: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctDisconnectNetworkPlayer\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + commandWasHandled = true; + + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole + role = networkManager.getNetworkRole(); + //GameSettings *settings = world->getGameSettingsPtr(); + + if (role == nrServer) { + //int factionIndex = networkCommand->getUnitId(); + int + playerIndex = networkCommand->getCommandTypeId(); + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + ServerInterface * + server = networkManager.getServerInterface(); + if (server != NULL + && server->isClientConnected(playerIndex) == true) { + + MutexSafeWrapper + safeMutex(server->getSlotMutex(playerIndex), + CODE_AT_LINE); + ConnectionSlot * + slot = server->getSlot(playerIndex, false); + if (slot != NULL) { + safeMutex.ReleaseLock(); + NetworkMessageQuit + networkMessageQuit; + slot->sendMessage(&networkMessageQuit); + sleep(5); + + //printf("Sending nctDisconnectNetworkPlayer\n"); + server = + networkManager.getServerInterface(false); + if (server != NULL) { + MutexSafeWrapper + safeMutex2(server-> + getSlotMutex(playerIndex), + CODE_AT_LINE); + slot = server->getSlot(playerIndex, false); + if (slot != NULL) { + safeMutex2.ReleaseLock(); + slot->close(); + } + } + } + } + } + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctDisconnectNetworkPlayer\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + break; + + case nctPauseResume: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctPauseResume\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + commandWasHandled = true; + + bool + pauseGame = networkCommand->getUnitId() != 0; + bool + clearCaches = (networkCommand->getCommandTypeId() == 1); + bool + joinNetworkGame = (networkCommand->getUnitTypeId() == 1); + Game * + game = this->world->getGame(); + + //printf("nctPauseResume pauseGame = %d\n",pauseGame); + game->setPaused(pauseGame, true, clearCaches, joinNetworkGame); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctPauseResume\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + break; + + case nctPlayerStatusChange: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctPlayerStatusChange\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + commandWasHandled = true; + + int + factionIndex = networkCommand->getUnitId(); + int + playerStatus = networkCommand->getCommandTypeId(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "nctPlayerStatusChange factionIndex = %d playerStatus = %d\n", + factionIndex, playerStatus); + + //printf("#1 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus); + + GameSettings * + settings = world->getGameSettingsPtr(); + if (playerStatus == npst_Disconnected) { + //printf("Commander nctPlayerStatusChange factionIndex: %d\n",factionIndex); + + settings->setNetworkPlayerStatuses(factionIndex, + npst_Disconnected); + + //printf("nctPlayerStatusChange -> faction->getPersonalityType() = %d index [%d] control [%d] networkstatus [%d]\n", + // world->getFaction(factionIndex)->getPersonalityType(),world->getFaction(factionIndex)->getIndex(),world->getFaction(factionIndex)->getControlType(),settings->getNetworkPlayerStatuses(factionIndex)); + + //printf("#2 nctPlayerStatusChange factionIndex = %d playerStatus = %d\n",factionIndex,playerStatus); + settings->setFactionControl(factionIndex, ctCpuUltra); + settings->setResourceMultiplierIndex(factionIndex, + settings-> + getFallbackCpuMultiplier + ()); + //Game *game = this->world->getGame(); + //game->get + Faction * + faction = this->world->getFaction(factionIndex); + faction->setControlType(ctCpuUltra); + + if (!world->getGame()->getGameOver() + && !this->world->getGame()-> + factionLostGame(factionIndex)) { + // use the fallback multiplier here + + // mark player as "leaver" + this->world->getStats()-> + setPlayerLeftBeforeEnd(factionIndex, true); + // set disconnect time for endgame stats + this->world->getStats()->setTimePlayerLeft(factionIndex, + this->world-> + getStats()-> + getFramesToCalculatePlaytime + ()); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctPlayerStatusChange\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + break; + + } + + if (commandWasHandled == false) { + Unit * + unit = world->findUnitById(networkCommand->getUnitId()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after world->findUnitById]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Running command NetworkCommandType = %d, unitid = %d [%p] factionindex = %d\n", + networkCommand->getNetworkCommandType(), + networkCommand->getUnitId(), unit, + (unit != NULL ? unit->getFactionIndex() : -1)); + //execute command, if unit is still alive + if (unit != NULL) { + switch (networkCommand->getNetworkCommandType()) { + case nctGiveCommand: + { + assert(networkCommand->getCommandTypeId() != + CommandType::invalidId); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + networkCommand->getUnitId()); + + Command * + command = buildCommand(networkCommand); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after buildCommand]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] command = %p\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + command); + + unit->giveCommand(command, + (networkCommand->getWantQueue() != + 0)); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after unit->giveCommand]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctGiveCommand networkCommand->getUnitId() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + networkCommand->getUnitId()); + } + break; + case nctCancelCommand: + { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctCancelCommand\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__); + + unit->cancelCommand(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after unit->cancelCommand]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctCancelCommand\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + break; + case nctSetMeetingPoint: + { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSetMeetingPoint\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__); + + unit->setMeetingPos(networkCommand->getPosition()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [after unit->setMeetingPos]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found nctSetMeetingPoint\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + break; + + default: + assert(false); + break; + } + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] NULL Unit for id = %d, networkCommand->getNetworkCommandType() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + networkCommand->getUnitId(), + networkCommand-> + getNetworkCommandType()); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld [END]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + } + + // Reconstruct a network command received. + Command * + Commander::buildCommand(const NetworkCommand * networkCommand) const { + // Check a new command is actually being given (and not a cancel command, switch team etc.). + assert(networkCommand->getNetworkCommandType() == nctGiveCommand); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] networkCommand [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + networkCommand->toString().c_str()); + + // Check there is a world. + if (world == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] world == NULL for unit with id: %d", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, networkCommand->getUnitId()); + throw + megaglest_runtime_error(szBuf); + } + + // Get the unit. + Unit * + target = NULL; + const CommandType * + ct = NULL; + const Unit * + unit = world->findUnitById(networkCommand->getUnitId()); + + // Validate unit is in game. + if (unit == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Can not find unit with id: %d. Game out of synch.", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, networkCommand->getUnitId()); + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + char + szMsg[8096] = ""; + snprintf(szMsg, 8096, + "Player detected an error: Can not find unit with id: %d. Game out of synch.", + networkCommand->getUnitId()); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + } + + throw + megaglest_runtime_error(szBuf); + } + + // Get the command type for the unit. + ct = + unit->getType()->findCommandTypeById(networkCommand-> + getCommandTypeId()); + + // Check that the unit from the network command is the same faction as the unit in the local game. + if (unit->getFaction()->getIndex() != + networkCommand->getUnitFactionIndex()) { + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nUnit / Faction mismatch for network command = [%s]\n%s\nfor unit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nGame out of synch.", + __FILE__, __FUNCTION__, __LINE__, + networkCommand->toString().c_str(), + unit->getType()->getCommandTypeListDesc().c_str(), + unit->getId(), unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex()); + + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + szBuf); + //std::string worldLog = world->DumpWorldToLog(); + world->DumpWorldToLog(); + + // Broadcast the error if player is still connected and print locally. + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL + && gameNetworkInterface->isConnected() == true) { + char + szMsg[8096] = ""; + snprintf(szMsg, 8096, + "Player detected an error: Unit / Faction mismatch for unitId: %d", + networkCommand->getUnitId()); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + snprintf(szMsg, 8096, + "Local faction index = %d, remote index = %d. Game out of synch.", + unit->getFaction()->getIndex(), + networkCommand->getUnitFactionIndex()); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + + } + + // Else if it's a network game but the user disconnected, print the error locally only. + else if (gameNetworkInterface != NULL) { + char + szMsg[8096] = ""; + snprintf(szMsg, 8096, + "Player detected an error: Connection lost, possible Unit / Faction mismatch for unitId: %d", + networkCommand->getUnitId()); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + snprintf(szMsg, 8096, + "Local faction index = %d, remote index = %d. Game out of synch.", + unit->getFaction()->getIndex(), + networkCommand->getUnitFactionIndex()); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + } + + // Kill the game. + std::string sError = + "Error [#1]: Game is out of sync (Unit / Faction mismatch)\nplease check log files for details."; + throw + megaglest_runtime_error(sError); + } + + const UnitType * + unitType = + world->findUnitTypeById(unit->getFaction()->getType(), + networkCommand->getUnitTypeId()); + + // debug test! + //throw megaglest_runtime_error("Test missing command type!"); + + //validate command type + + // !!!Test out of synch behaviour + //ct = NULL; + + // Check if the command was for the unit before it morphed, if so cancel it. + bool + isCancelPreMorphCommand = false; + if (ct == NULL && unit->getPreMorphType() != NULL) { + const CommandType * + ctPreMorph = + unit->getPreMorphType()->findCommandTypeById(networkCommand-> + getCommandTypeId + ()); + if (ctPreMorph != NULL) { + ct = unit->getType()->getFirstCtOfClass(ccStop); + isCancelPreMorphCommand = true; + } + } + + // Throw an error if a valid command for the unit is still not found. + if (ct == NULL) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nCan not find command type for network command = [%s]\n%s\nfor unit = %d\n[%s]\n[%s]\nactual local factionIndex = %d.\nUnit Type Info:\n[%s]\nNetwork unit type:\n[%s]\nisCancelPreMorphCommand: %d\nGame out of synch.", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, + networkCommand->toString().c_str(), + unit->getType()->getCommandTypeListDesc().c_str(), + unit->getId(), unit->getFullName(false).c_str(), + unit->getDesc(false).c_str(), + unit->getFaction()->getIndex(), + unit->getType()->toString().c_str(), + (unitType != + NULL ? unitType->getName(false).c_str() : "null"), + isCancelPreMorphCommand); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", szBuf); + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", szBuf); + world->DumpWorldToLog(); + + GameNetworkInterface * + gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + char + szMsg[8096] = ""; + snprintf(szMsg, 8096, + "Player detected an error: Can not find command type: %d for unitId: %d [%s]. isCancelPreMorphCommand: %d Game out of synch.", + networkCommand->getCommandTypeId(), + networkCommand->getUnitId(), + (unitType != + NULL ? unitType->getName(false).c_str() : "null"), + isCancelPreMorphCommand); + gameNetworkInterface->sendTextMessage(szMsg, -1, true, ""); + } + + std::string sError = + "Error [#3]: Game is out of sync, please check log files for details."; + //abort(); + throw + megaglest_runtime_error(sError); + } + + CardinalDir + facing; + // Get direction of the command target unit. + if (isCancelPreMorphCommand == false) { + // If target is a building. + if (ct->getClass() == ccBuild) { + // Check the target building ID is valid. If not, throw an error. + /// TODO: What is happening here? The error returned does not match the condition. Why is there a constant of 4? + if (networkCommand->getTargetId() < 0 + || networkCommand->getTargetId() >= 4) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "networkCommand->getTargetId() >= 0 && networkCommand->getTargetId() < 4, [%s]", + networkCommand->toString().c_str()); + throw + megaglest_runtime_error(szBuf); + } + facing = CardinalDir(networkCommand->getTargetId()); + } + // Get the target unit if the ID is valid. + else if (networkCommand->getTargetId() != Unit::invalidId) { + target = world->findUnitById(networkCommand->getTargetId()); + } + } + + // Create the command. + Command * + command = NULL; + if (isCancelPreMorphCommand == false) { + if (unitType != NULL) { + command = + new Command(ct, networkCommand->getPosition(), unitType, + facing); + } else if (target == NULL) { + command = new Command(ct, networkCommand->getPosition()); + } else { + command = new Command(ct, target); + } + } else { + command = new Command(ct, NULL); + } + + // Add in any special state + CommandStateType + commandStateType = networkCommand->getCommandStateType(); + int + commandStateValue = networkCommand->getCommandStateValue(); + + command->setStateType(commandStateType); + command->setStateValue(commandStateValue); + command->setUnitCommandGroupId(networkCommand-> + getUnitCommandGroupId()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + //issue command + return command; + } + + } } //end namespace diff --git a/source/glest_game/game/commander.h b/source/glest_game/game/commander.h index c2f9a530d..e7821a71d 100644 --- a/source/glest_game/game/commander.h +++ b/source/glest_game/game/commander.h @@ -26,174 +26,170 @@ # include "leak_dumper.h" using - std::vector; +std::vector; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - using - Shared::Graphics::Vec2i; - using - Shared::PlatformCommon::Chrono; + using + Shared::Graphics::Vec2i; + using + Shared::PlatformCommon::Chrono; - class - World; - class - Unit; - class - Command; - class - CommandType; - class - NetworkCommand; - class - Game; - class - SwitchTeamVote; + class + World; + class + Unit; + class + Command; + class + CommandType; + class + NetworkCommand; + class + Game; + class + SwitchTeamVote; -// ===================================================== -// class Commander -// -/// Gives commands to the units -// ===================================================== - class - Commander - { - private: - typedef - vector < - std::pair < - CommandResult, - string > > - CommandResultContainer; + // ===================================================== + // class Commander + // + /// Gives commands to the units + // ===================================================== + class + Commander { + private: + typedef + vector < + std::pair < + CommandResult, + string > > + CommandResultContainer; - private: - World * - world; - Chrono - perfTimer; + private: + World * + world; + Chrono + perfTimer; - std::vector < - std::pair < int, - NetworkCommand > > - replayCommandList; + std::vector < + std::pair < int, + NetworkCommand > > + replayCommandList; - bool - pauseNetworkCommands; + bool + pauseNetworkCommands; - public: - Commander (); - virtual ~ - Commander (); + public: + Commander(); + virtual ~ + Commander(); - bool - getPauseNetworkCommands () const - { - return - this-> - pauseNetworkCommands; - } - void - setPauseNetworkCommands (bool pause) - { - this->pauseNetworkCommands = pause; - } + bool + getPauseNetworkCommands() const { + return + this-> + pauseNetworkCommands; + } + void + setPauseNetworkCommands(bool pause) { + this->pauseNetworkCommands = pause; + } - void - signalNetworkUpdate (Game * game); - void - init (World * world); - void - updateNetwork (Game * game); + void + signalNetworkUpdate(Game * game); + void + init(World * world); + void + updateNetwork(Game * game); - void - addToReplayCommandList (NetworkCommand & command, int worldFrameCount); - bool - getReplayCommandListForFrame (int worldFrameCount); - bool - hasReplayCommandListForFrame () const; - int - getReplayCommandListForFrameCount () const; + void + addToReplayCommandList(NetworkCommand & command, int worldFrameCount); + bool + getReplayCommandListForFrame(int worldFrameCount); + bool + hasReplayCommandListForFrame() const; + int + getReplayCommandListForFrameCount() const; - std::pair < - CommandResult, - string > - tryGiveCommand (const Selection * selection, - const CommandType * commandType, const Vec2i & pos, - const UnitType * unitType, CardinalDir facing, - bool tryQueue, Unit * targetUnit = NULL) const; + std::pair < + CommandResult, + string > + tryGiveCommand(const Selection * selection, + const CommandType * commandType, const Vec2i & pos, + const UnitType * unitType, CardinalDir facing, + bool tryQueue, Unit * targetUnit = NULL) const; - std::pair < - CommandResult, - string > - tryGiveCommand (const Unit * unit, const CommandType * commandType, - const Vec2i & pos, const UnitType * unitType, - CardinalDir facing, bool tryQueue = - false, Unit * targetUnit = - NULL, int unitGroupCommandId = -1) const; - std::pair < - CommandResult, - string > - tryGiveCommand (const Selection * selection, CommandClass commandClass, - const Vec2i & pos = Vec2i (0), const Unit * targetUnit = - NULL, bool tryQueue = false) const; - std::pair < - CommandResult, - string > - tryGiveCommand (const Selection * selection, - const CommandType * commandType, const Vec2i & pos = - Vec2i (0), const Unit * targetUnit = - NULL, bool tryQueue = false) const; - std::pair < - CommandResult, - string > - tryGiveCommand (const Selection * selection, const Vec2i & pos, - const Unit * targetUnit = NULL, bool tryQueue = - false, int unitCommandGroupId = -1) const; - CommandResult - tryCancelCommand (const Selection * selection) const; - void - trySetMeetingPoint (const Unit * unit, const Vec2i & pos) const; - void - trySwitchTeam (const Faction * faction, int teamIndex) const; - void - trySwitchTeamVote (const Faction * faction, - SwitchTeamVote * vote) const; - void - tryDisconnectNetworkPlayer (const Faction * faction, - int playerIndex) const; + std::pair < + CommandResult, + string > + tryGiveCommand(const Unit * unit, const CommandType * commandType, + const Vec2i & pos, const UnitType * unitType, + CardinalDir facing, bool tryQueue = + false, Unit * targetUnit = + NULL, int unitGroupCommandId = -1) const; + std::pair < + CommandResult, + string > + tryGiveCommand(const Selection * selection, CommandClass commandClass, + const Vec2i & pos = Vec2i(0), const Unit * targetUnit = + NULL, bool tryQueue = false) const; + std::pair < + CommandResult, + string > + tryGiveCommand(const Selection * selection, + const CommandType * commandType, const Vec2i & pos = + Vec2i(0), const Unit * targetUnit = + NULL, bool tryQueue = false) const; + std::pair < + CommandResult, + string > + tryGiveCommand(const Selection * selection, const Vec2i & pos, + const Unit * targetUnit = NULL, bool tryQueue = + false, int unitCommandGroupId = -1) const; + CommandResult + tryCancelCommand(const Selection * selection) const; + void + trySetMeetingPoint(const Unit * unit, const Vec2i & pos) const; + void + trySwitchTeam(const Faction * faction, int teamIndex) const; + void + trySwitchTeamVote(const Faction * faction, + SwitchTeamVote * vote) const; + void + tryDisconnectNetworkPlayer(const Faction * faction, + int playerIndex) const; - void - tryPauseGame (bool joinNetworkGame, bool clearCaches) const; - void - tryResumeGame (bool joinNetworkGame, bool clearCaches) const; + void + tryPauseGame(bool joinNetworkGame, bool clearCaches) const; + void + tryResumeGame(bool joinNetworkGame, bool clearCaches) const; - void - tryNetworkPlayerDisconnected (int factionIndex) const; + void + tryNetworkPlayerDisconnected(int factionIndex) const; - Command * - buildCommand (const NetworkCommand * networkCommand) const; + Command * + buildCommand(const NetworkCommand * networkCommand) const; - private: - std::pair < - CommandResult, - string > - pushNetworkCommand (const NetworkCommand * networkCommand) const; - std::pair < - CommandResult, - string > - computeResult (const CommandResultContainer & results) const; - void - giveNetworkCommand (NetworkCommand * networkCommand) const; - bool - canSubmitCommandType (const Unit * unit, - const CommandType * commandType) const; - }; + private: + std::pair < + CommandResult, + string > + pushNetworkCommand(const NetworkCommand * networkCommand) const; + std::pair < + CommandResult, + string > + computeResult(const CommandResultContainer & results) const; + void + giveNetworkCommand(NetworkCommand * networkCommand) const; + bool + canSubmitCommandType(const Unit * unit, + const CommandType * commandType) const; + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/game/console.cpp b/source/glest_game/game/console.cpp index 2d77c230e..c77db77a0 100644 --- a/source/glest_game/game/console.cpp +++ b/source/glest_game/game/console.cpp @@ -24,323 +24,257 @@ using namespace std; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Console -// ===================================================== + // ===================================================== + // class Console + // ===================================================== - Console::Console () - { - registerGraphicComponent ("Console", "Generic-Console"); - this->fontCallbackName = getInstanceName () + "_" + getNewUUD (); - CoreData::getInstance ().registerFontChangedCallback (this-> - getFontCallbackName - (), this); + Console::Console() { + registerGraphicComponent("Console", "Generic-Console"); + this->fontCallbackName = getInstanceName() + "_" + getNewUUD(); + CoreData::getInstance().registerFontChangedCallback(this-> + getFontCallbackName + (), this); - maxLines = Config::getInstance ().getInt ("ConsoleMaxLines"); - maxStoredLines = - Config::getInstance ().getInt ("ConsoleMaxLinesStored"); - timeout = Config::getInstance ().getInt ("ConsoleTimeout"); - timeElapsed = 0.0f; - xPos = 20; - yPos = 20; - lineHeight = - Config::getInstance ().getInt ("FontConsoleBaseSize", "18") + 2; - setFont (CoreData::getInstance ().getConsoleFont ()); - setFont3D (CoreData::getInstance ().getConsoleFont3D ()); + maxLines = Config::getInstance().getInt("ConsoleMaxLines"); + maxStoredLines = + Config::getInstance().getInt("ConsoleMaxLinesStored"); + timeout = Config::getInstance().getInt("ConsoleTimeout"); + timeElapsed = 0.0f; + xPos = 20; + yPos = 20; + lineHeight = + Config::getInstance().getInt("FontConsoleBaseSize", "18") + 2; + setFont(CoreData::getInstance().getConsoleFont()); + setFont3D(CoreData::getInstance().getConsoleFont3D()); - stringToHighlight = ""; - onlyChatMessagesInStoredLines = true; - } + stringToHighlight = ""; + onlyChatMessagesInStoredLines = true; + } - string Console::getNewUUD () - { - char uuid_str[38]; - get_uuid_string (uuid_str, sizeof (uuid_str)); - return string (uuid_str); - } + string Console::getNewUUD() { + char uuid_str[38]; + get_uuid_string(uuid_str, sizeof(uuid_str)); + return string(uuid_str); + } - Console::~Console () - { - CoreData::getInstance ().unRegisterFontChangedCallback (this-> - getFontCallbackName - ()); - } + Console::~Console() { + CoreData::getInstance().unRegisterFontChangedCallback(this-> + getFontCallbackName + ()); + } - void Console::setFont (Font2D * font) - { - this->font = font; - if (this->font != NULL) - { - this->font2DUniqueId = font->getFontUniqueId (); - } - else - { - this->font2DUniqueId = ""; - } - } + void Console::setFont(Font2D * font) { + this->font = font; + if (this->font != NULL) { + this->font2DUniqueId = font->getFontUniqueId(); + } else { + this->font2DUniqueId = ""; + } + } - void Console::setFont3D (Font3D * font) - { - this->font3D = font; - if (this->font3D != NULL) - { - this->font3DUniqueId = font->getFontUniqueId (); - } - else - { - this->font3DUniqueId = ""; - } - } + void Console::setFont3D(Font3D * font) { + this->font3D = font; + if (this->font3D != NULL) { + this->font3DUniqueId = font->getFontUniqueId(); + } else { + this->font3DUniqueId = ""; + } + } - void Console::registerGraphicComponent (const std::string & containerName, - const std::string & objName) - { - this->instanceName = objName; - } + void Console::registerGraphicComponent(const std::string & containerName, + const std::string & objName) { + this->instanceName = objName; + } - void Console::FontChangedCallback (std::string fontUniqueId, Font * font) - { - if (fontUniqueId != "") - { - if (fontUniqueId == this->font2DUniqueId) - { - if (font != NULL) - { - this->font = (Font2D *) font; - } - else - { - this->font = NULL; - } - } - else if (fontUniqueId == this->font3DUniqueId) - { - if (font != NULL) - { - this->font3D = (Font3D *) font; - } - else - { - this->font3D = NULL; - } - } - } - } + void Console::FontChangedCallback(std::string fontUniqueId, Font * font) { + if (fontUniqueId != "") { + if (fontUniqueId == this->font2DUniqueId) { + if (font != NULL) { + this->font = (Font2D *) font; + } else { + this->font = NULL; + } + } else if (fontUniqueId == this->font3DUniqueId) { + if (font != NULL) { + this->font3D = (Font3D *) font; + } else { + this->font3D = NULL; + } + } + } + } - void Console::resetFonts () - { - setFont (CoreData::getInstance ().getConsoleFont ()); - setFont3D (CoreData::getInstance ().getConsoleFont3D ()); - } + void Console::resetFonts() { + setFont(CoreData::getInstance().getConsoleFont()); + setFont3D(CoreData::getInstance().getConsoleFont3D()); + } - void Console::addStdMessage (const string & s, bool clearOtherLines) - { - if (clearOtherLines == true) - { - addLineOnly (Lang::getInstance ().getString (s)); - } - else - { - addLine (Lang::getInstance ().getString (s)); - } - } + void Console::addStdMessage(const string & s, bool clearOtherLines) { + if (clearOtherLines == true) { + addLineOnly(Lang::getInstance().getString(s)); + } else { + addLine(Lang::getInstance().getString(s)); + } + } - void Console::addStdMessage (const string & s, const string & failText, - bool clearOtherLines) - { - if (clearOtherLines == true) - { - addLineOnly (Lang::getInstance ().getString (s) + failText); - } - else - { - addLine (Lang::getInstance ().getString (s) + failText); - } - } + void Console::addStdMessage(const string & s, const string & failText, + bool clearOtherLines) { + if (clearOtherLines == true) { + addLineOnly(Lang::getInstance().getString(s) + failText); + } else { + addLine(Lang::getInstance().getString(s) + failText); + } + } - void Console::addStdScenarioMessage (const string & s, - bool clearOtherLines) - { - if (clearOtherLines == true) - { - addLineOnly (Lang::getInstance ().getScenarioString (s)); - } - else - { - addLine (Lang::getInstance ().getScenarioString (s)); - } - } + void Console::addStdScenarioMessage(const string & s, + bool clearOtherLines) { + if (clearOtherLines == true) { + addLineOnly(Lang::getInstance().getScenarioString(s)); + } else { + addLine(Lang::getInstance().getScenarioString(s)); + } + } - void Console::addLineOnly (const string & line) - { - addLine (line, false, -1, Vec3f (1.f, 1.f, 1.f), false, true); - } + void Console::addLineOnly(const string & line) { + addLine(line, false, -1, Vec3f(1.f, 1.f, 1.f), false, true); + } - void Console::addLine (const string & line, bool playSound, - int playerIndex, Vec3f textColor, bool teamMode, - bool clearOtherLines) - { - try - { - if (playSound == true) - { - SoundRenderer::getInstance ().playFx (CoreData::getInstance (). - getClickSoundA ()); - } - ConsoleLineInfo info; - info.text = line; - info.timeStamp = timeElapsed; - info.PlayerIndex = playerIndex; - info.originalPlayerName = ""; - info.color = textColor; - info.teamMode = teamMode; - if (playerIndex >= 0) - { - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - info.originalPlayerName = - gameNetworkInterface->getGameSettings ()-> - getNetworkPlayerNameByPlayerIndex (playerIndex); - //for(int i = 0; i < GameConstants::maxPlayers; ++i) { - // printf("i = %d, playerName = [%s]\n",i,gameNetworkInterface->getGameSettings()->getNetworkPlayerName(i).c_str()); - //} - } - } - //printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str()); + void Console::addLine(const string & line, bool playSound, + int playerIndex, Vec3f textColor, bool teamMode, + bool clearOtherLines) { + try { + if (playSound == true) { + SoundRenderer::getInstance().playFx(CoreData::getInstance(). + getClickSoundA()); + } + ConsoleLineInfo info; + info.text = line; + info.timeStamp = timeElapsed; + info.PlayerIndex = playerIndex; + info.originalPlayerName = ""; + info.color = textColor; + info.teamMode = teamMode; + if (playerIndex >= 0) { + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + info.originalPlayerName = + gameNetworkInterface->getGameSettings()-> + getNetworkPlayerNameByPlayerIndex(playerIndex); + //for(int i = 0; i < GameConstants::maxPlayers; ++i) { + // printf("i = %d, playerName = [%s]\n",i,gameNetworkInterface->getGameSettings()->getNetworkPlayerName(i).c_str()); + //} + } + } + //printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str()); - if (clearOtherLines == true) - { - lines.clear (); - storedLines.clear (); - } - lines.insert (lines.begin (), info); - if ((int) lines.size () > maxLines) - { - lines.pop_back (); - } - if (onlyChatMessagesInStoredLines == false || info.PlayerIndex != -1) - { - storedLines.insert (storedLines.begin (), info); - if ((int) storedLines.size () > maxStoredLines) - { - storedLines.pop_back (); - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } + if (clearOtherLines == true) { + lines.clear(); + storedLines.clear(); + } + lines.insert(lines.begin(), info); + if ((int) lines.size() > maxLines) { + lines.pop_back(); + } + if (onlyChatMessagesInStoredLines == false || info.PlayerIndex != -1) { + storedLines.insert(storedLines.begin(), info); + if ((int) storedLines.size() > maxStoredLines) { + storedLines.pop_back(); + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } - void Console::addLine (const string & line, bool playSound, - const string & playerName, Vec3f textColor, - bool teamMode) - { - try - { - if (playSound == true) - { - SoundRenderer::getInstance ().playFx (CoreData::getInstance (). - getClickSoundA ()); - } - ConsoleLineInfo info; - info.text = line; - info.timeStamp = timeElapsed; - info.PlayerIndex = -1; - info.originalPlayerName = ""; - info.color = textColor; - info.teamMode = teamMode; - if (playerName != "") - { - info.originalPlayerName = playerName; - } - //printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str()); + void Console::addLine(const string & line, bool playSound, + const string & playerName, Vec3f textColor, + bool teamMode) { + try { + if (playSound == true) { + SoundRenderer::getInstance().playFx(CoreData::getInstance(). + getClickSoundA()); + } + ConsoleLineInfo info; + info.text = line; + info.timeStamp = timeElapsed; + info.PlayerIndex = -1; + info.originalPlayerName = ""; + info.color = textColor; + info.teamMode = teamMode; + if (playerName != "") { + info.originalPlayerName = playerName; + } + //printf("info.PlayerIndex = %d, line [%s]\n",info.PlayerIndex,info.originalPlayerName.c_str()); - lines.insert (lines.begin (), info); - if ((int) lines.size () > maxLines) - { - lines.pop_back (); - } - storedLines.insert (storedLines.begin (), info); - if ((int) storedLines.size () > maxStoredLines) - { - storedLines.pop_back (); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } + lines.insert(lines.begin(), info); + if ((int) lines.size() > maxLines) { + lines.pop_back(); + } + storedLines.insert(storedLines.begin(), info); + if ((int) storedLines.size() > maxStoredLines) { + storedLines.pop_back(); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } - void Console::clearStoredLines () - { - while (storedLines.empty () == false) - { - storedLines.pop_back (); - } - } + void Console::clearStoredLines() { + while (storedLines.empty() == false) { + storedLines.pop_back(); + } + } - void Console::update () - { - timeElapsed += 1.f / GameConstants::updateFps; + void Console::update() { + timeElapsed += 1.f / GameConstants::updateFps; - if (lines.empty () == false) - { - if (lines.back ().timeStamp < (timeElapsed - timeout)) - { - lines.pop_back (); - } - } - } + if (lines.empty() == false) { + if (lines.back().timeStamp < (timeElapsed - timeout)) { + lines.pop_back(); + } + } + } - bool Console::isEmpty () - { - return lines.empty (); - } + bool Console::isEmpty() { + return lines.empty(); + } -//string Console::getLine(int i) const { -// if(i < 0 || i >= (int)lines.size()) -// throw megaglest_runtime_error("i >= Lines.size()"); -// return lines[i].text; -//} + //string Console::getLine(int i) const { + // if(i < 0 || i >= (int)lines.size()) + // throw megaglest_runtime_error("i >= Lines.size()"); + // return lines[i].text; + //} -//string Console::getStoredLine(int i) const { -// if(i < 0 || i >= (int)storedLines.size()) -// throw megaglest_runtime_error("i >= storedLines.size()"); -// return storedLines[i].text; -//} + //string Console::getStoredLine(int i) const { + // if(i < 0 || i >= (int)storedLines.size()) + // throw megaglest_runtime_error("i >= storedLines.size()"); + // return storedLines[i].text; + //} - ConsoleLineInfo Console::getLineItem (int i) const - { - if (i < 0 || i >= (int) lines.size ()) - throw megaglest_runtime_error ("i >= Lines.size()"); - return lines[i]; - } + ConsoleLineInfo Console::getLineItem(int i) const { + if (i < 0 || i >= (int) lines.size()) + throw megaglest_runtime_error("i >= Lines.size()"); + return lines[i]; + } - ConsoleLineInfo Console::getStoredLineItem (int i) const - { - if (i < 0 || i >= (int) storedLines.size ()) - throw megaglest_runtime_error ("i >= storedLines.size()"); - return storedLines[i]; - } + ConsoleLineInfo Console::getStoredLineItem(int i) const { + if (i < 0 || i >= (int) storedLines.size()) + throw megaglest_runtime_error("i >= storedLines.size()"); + return storedLines[i]; + } - } + } } //end namespace diff --git a/source/glest_game/game/console.h b/source/glest_game/game/console.h index 6fcdb3265..959d8fcb2 100644 --- a/source/glest_game/game/console.h +++ b/source/glest_game/game/console.h @@ -26,275 +26,254 @@ # include "vec.h" using - std::string; +std::string; using - std::vector; +std::vector; using - std::pair; +std::pair; using namespace - std; +std; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - using - Shared::Graphics::Font; - using - Shared::Graphics::Font2D; - using - Shared::Graphics::Font3D; - using - Shared::Graphics::FontChangedCallbackInterface; - using - Shared::Graphics::Vec3f; -// ===================================================== -// class Console -// -// In-game console that shows various types of messages -// ===================================================== + using + Shared::Graphics::Font; + using + Shared::Graphics::Font2D; + using + Shared::Graphics::Font3D; + using + Shared::Graphics::FontChangedCallbackInterface; + using + Shared::Graphics::Vec3f; + // ===================================================== + // class Console + // + // In-game console that shows various types of messages + // ===================================================== - class - ConsoleLineInfo - { - public: - string - text; - float - timeStamp; - int - PlayerIndex; - string - originalPlayerName; - Vec3f - color; - bool - teamMode; - }; + class + ConsoleLineInfo { + public: + string + text; + float + timeStamp; + int + PlayerIndex; + string + originalPlayerName; + Vec3f + color; + bool + teamMode; + }; - class - Console: - public - FontChangedCallbackInterface - { - private: - static const int - consoleLines = 5; + class + Console : + public + FontChangedCallbackInterface { + private: + static const int + consoleLines = 5; - public: + public: - typedef - vector < - ConsoleLineInfo > - Lines; - typedef - Lines::const_iterator - LineIterator; + typedef + vector < + ConsoleLineInfo > + Lines; + typedef + Lines::const_iterator + LineIterator; - private: - float - timeElapsed; - Lines - lines; - Lines - storedLines; - string - stringToHighlight; + private: + float + timeElapsed; + Lines + lines; + Lines + storedLines; + string + stringToHighlight; - //config - int - maxLines; - int - maxStoredLines; - float - timeout; - int - xPos; - int - yPos; - int - lineHeight; - Font2D * - font; - Font3D * - font3D; - string - font2DUniqueId; - string - font3DUniqueId; + //config + int + maxLines; + int + maxStoredLines; + float + timeout; + int + xPos; + int + yPos; + int + lineHeight; + Font2D * + font; + Font3D * + font3D; + string + font2DUniqueId; + string + font3DUniqueId; - bool - onlyChatMessagesInStoredLines; + bool + onlyChatMessagesInStoredLines; - string - instanceName; - string - fontCallbackName; + string + instanceName; + string + fontCallbackName; - string - getNewUUD (); + string + getNewUUD(); - public: + public: - Console (); - virtual ~ - Console (); + Console(); + virtual ~ + Console(); - void - registerGraphicComponent (const std::string & containerName, - const std::string & objName); - string - getInstanceName () const - { - return - instanceName; - } - void - setInstanceName (const string & value) - { - instanceName = value; - } - string - getFontCallbackName () const - { - return - fontCallbackName; - } + void + registerGraphicComponent(const std::string & containerName, + const std::string & objName); + string + getInstanceName() const { + return + instanceName; + } + void + setInstanceName(const string & value) { + instanceName = value; + } + string + getFontCallbackName() const { + return + fontCallbackName; + } - int - getStoredLineCount () const - { - return (int) - storedLines. - size (); - } - int - getLineCount () const - { - return (int) - lines. - size (); - } - bool - getOnlyChatMessagesInStoredLines () const - { - return - onlyChatMessagesInStoredLines; - } - void - setOnlyChatMessagesInStoredLines (bool value) - { - this->onlyChatMessagesInStoredLines = value; - } + int + getStoredLineCount() const { + return (int) + storedLines. + size(); + } + int + getLineCount() const { + return (int) + lines. + size(); + } + bool + getOnlyChatMessagesInStoredLines() const { + return + onlyChatMessagesInStoredLines; + } + void + setOnlyChatMessagesInStoredLines(bool value) { + this->onlyChatMessagesInStoredLines = value; + } - int - getXPos () const - { - return - xPos; - } - void - setXPos (int xPos) - { - this->xPos = xPos; - } - int - getYPos () const - { - return - yPos; - } - void - setYPos (int yPos) - { - this->yPos = yPos; - } - int - getLineHeight () const - { - return - lineHeight; - } - void - setLineHeight (int lineHeight) - { - this->lineHeight = lineHeight; - } - Font2D * - getFont () const - { - return - font; - } - Font3D * - getFont3D () const - { - return - font3D; - } - void - setFont (Font2D * font); - void - setFont3D (Font3D * font); - string - getStringToHighlight () const - { - return - stringToHighlight; - } - void - setStringToHighlight (const string & stringToHighlight) - { - this->stringToHighlight = stringToHighlight; - } - void - resetFonts (); + int + getXPos() const { + return + xPos; + } + void + setXPos(int xPos) { + this->xPos = xPos; + } + int + getYPos() const { + return + yPos; + } + void + setYPos(int yPos) { + this->yPos = yPos; + } + int + getLineHeight() const { + return + lineHeight; + } + void + setLineHeight(int lineHeight) { + this->lineHeight = lineHeight; + } + Font2D * + getFont() const { + return + font; + } + Font3D * + getFont3D() const { + return + font3D; + } + void + setFont(Font2D * font); + void + setFont3D(Font3D * font); + string + getStringToHighlight() const { + return + stringToHighlight; + } + void + setStringToHighlight(const string & stringToHighlight) { + this->stringToHighlight = stringToHighlight; + } + void + resetFonts(); - //string getLine(int i) const; - //string getStoredLine(int i) const; - ConsoleLineInfo - getLineItem (int i) const; - ConsoleLineInfo - getStoredLineItem (int i) const; + //string getLine(int i) const; + //string getStoredLine(int i) const; + ConsoleLineInfo + getLineItem(int i) const; + ConsoleLineInfo + getStoredLineItem(int i) const; - void - clearStoredLines (); - void - addStdMessage (const string & s, bool clearOtherLines = false); - void - addStdMessage (const string & s, const string & failText, - bool clearOtherLines = false); + void + clearStoredLines(); + void + addStdMessage(const string & s, bool clearOtherLines = false); + void + addStdMessage(const string & s, const string & failText, + bool clearOtherLines = false); - void - addStdScenarioMessage (const string & s, bool clearOtherLines = false); - void - addLineOnly (const string & line); - void - addLine (const string & line, bool playSound = false, int playerIndex = - -1, Vec3f textColor = Vec3f (1.f, 1.f, 1.f), bool teamMode = - false, bool clearOtherLines = false); - void - addLine (const string & line, bool playSound, const string & playerName, - Vec3f textColor = Vec3f (1.f, 1.f, 1.f), bool teamMode = - false); - void - addLine (const string & line, bool playSound, Vec3f textColor) - { - addLine (line, playSound, "", textColor, false); - } - void - update (); - bool - isEmpty (); + void + addStdScenarioMessage(const string & s, bool clearOtherLines = false); + void + addLineOnly(const string & line); + void + addLine(const string & line, bool playSound = false, int playerIndex = + -1, Vec3f textColor = Vec3f(1.f, 1.f, 1.f), bool teamMode = + false, bool clearOtherLines = false); + void + addLine(const string & line, bool playSound, const string & playerName, + Vec3f textColor = Vec3f(1.f, 1.f, 1.f), bool teamMode = + false); + void + addLine(const string & line, bool playSound, Vec3f textColor) { + addLine(line, playSound, "", textColor, false); + } + void + update(); + bool + isEmpty(); - virtual void - FontChangedCallback (std::string fontUniqueId, Font * font); - }; + virtual void + FontChangedCallback(std::string fontUniqueId, Font * font); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index 3e96e1e82..a51ff28d8 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -49,10554 +49,9377 @@ using namespace Shared::Util; using namespace Shared::Platform; using namespace Shared::CompressionUtil; -namespace Glest -{ - namespace Game - { - - string GameSettings::playerDisconnectedText = ""; - Game *thisGamePtr = NULL; - -// ===================================================== -// class Game -// ===================================================== - -// ===================== PUBLIC ======================== - - const float PHOTO_MODE_MAXHEIGHT = 500.0; - - const int CREATE_NEW_TEAM = -100; - const int CANCEL_SWITCH_TEAM = -1; - - const int CANCEL_DISCONNECT_PLAYER = -1; - - const float Game::highlightTime = 0.5f; - - int fadeMusicMilliseconds = 3500; - -// Check every x seconds if we should switch disconnected players to AI - const int NETWORK_PLAYER_CONNECTION_CHECK_SECONDS = 5; - - int GAME_STATS_DUMP_INTERVAL = 60 * 10; - - Game::Game (): - ProgramState (NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - originalDisplayMsgCallback = NULL; - aiInterfaces.clear (); - videoPlayer = NULL; - playingStaticVideo = false; - - mouse2d = 0; - mouseX = 0; - mouseY = 0; - updateFps = 0; - lastUpdateFps = 0; - avgUpdateFps = 0; - framesToCatchUpAsClient = 0; - framesToSlowDownAsClient = 0; - totalRenderFps = 0; - renderFps = 0; - lastRenderFps = 0; - avgRenderFps = 0; - currentAvgRenderFpsTotal = 0; - paused = false; - networkPauseGameForLaggedClientsRequested = false; - networkResumeGameForLaggedClientsRequested = false; - pausedForJoinGame = false; - pausedBeforeJoinGame = false; - pauseRequestSent = false; - resumeRequestSent = false; - pauseStateChanged = false; - gameOver = false; - renderNetworkStatus = false; - renderInGamePerformance = false; - showFullConsole = false; - setMarker = false; - cameraDragAllowed = false; - mouseMoved = false; - scrollSpeed = 0; - camLeftButtonDown = false; - camRightButtonDown = false; - camUpButtonDown = false; - camDownButtonDown = false; - this->speed = 1; - weatherParticleSystem = NULL; - isFirstRender = false; - quitTriggeredIndicator = false; - quitPendingIndicator = false; - original_updateFps = 0; - original_cameraFps = 0; - captureAvgTestStatus = false; - updateFpsAvgTest = 0; - renderFpsAvgTest = 0; - renderExtraTeamColor = 0; - photoModeEnabled = false; - healthbarMode = hbvUndefined; - visibleHUD = false; - timeDisplay = false; - withRainEffect = false; - program = NULL; - gameStarted = false; - this->initialResumeSpeedLoops = false; - - highlightCellTexture = NULL; - lastMasterServerGameStatsDump = 0; - lastMaxUnitCalcTime = 0; - lastRenderLog2d = 0; - playerIndexDisconnect = 0; - tickCount = 0; - currentCameraFollowUnit = NULL; - - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - - popupMenuSwitchTeams.setEnabled (false); - popupMenuSwitchTeams.setVisible (false); - - popupMenuDisconnectPlayer.setEnabled (false); - popupMenuDisconnectPlayer.setVisible (false); - - switchTeamConfirmMessageBox.setEnabled (false); - disconnectPlayerConfirmMessageBox.setEnabled (false); - - disconnectPlayerIndexMap.clear (); - - exitGamePopupMenuIndex = -1; - joinTeamPopupMenuIndex = -1; - pauseGamePopupMenuIndex = -1; - saveGamePopupMenuIndex = -1; - loadGamePopupMenuIndex = -1; - keyboardSetupPopupMenuIndex = -1; - disconnectPlayerPopupMenuIndex = -1; - - isMarkCellEnabled = false; - isMarkCellTextEnabled = false; - - markCellTexture = NULL; - isUnMarkCellEnabled = false; - unmarkCellTexture = NULL; - - masterserverMode = false; - currentUIState = NULL; - currentAmbientSound = NULL; - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - - loadGameNode = NULL; - lastworldFrameCountForReplay = -1; - lastNetworkPlayerConnectionCheck = time (NULL); - inJoinGameLoading = false; - quitGameCalled = false; - disableSpeedChange = false; - - for (int i = 0; i < GameConstants::networkSmoothInterval; i++) - { - receivedTooEarlyInFrames[i] = -1; - framesNeededToWaitForServerMessage[i] = -1; - } - - fadeMusicMilliseconds = - Config::getInstance ().getInt ("GameStartStopFadeSoundMilliseconds", - intToStr (fadeMusicMilliseconds).c_str - ()); - GAME_STATS_DUMP_INTERVAL = - Config::getInstance ().getInt ("GameStatsDumpIntervalSeconds", - intToStr - (GAME_STATS_DUMP_INTERVAL).c_str ()); - } - - void Game::resetMembers () - { - Unit::setGame (this); - gameStarted = false; - this->initialResumeSpeedLoops = false; - - original_updateFps = GameConstants::updateFps; - original_cameraFps = GameConstants::cameraFps; - GameConstants::updateFps = 40; - GameConstants::cameraFps = 100; - captureAvgTestStatus = false; - updateFpsAvgTest = 0; - renderFpsAvgTest = 0; - lastRenderLog2d = 0; - playerIndexDisconnect = 0; - lastMasterServerGameStatsDump = 0; - highlightCellTexture = NULL; - totalRenderFps = 0; - lastMaxUnitCalcTime = 0; - renderExtraTeamColor = 0; - - mouseMoved = false; - quitTriggeredIndicator = false; - quitPendingIndicator = false; - originalDisplayMsgCallback = NULL; - thisGamePtr = this; - - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - - popupMenuSwitchTeams.setEnabled (false); - popupMenuSwitchTeams.setVisible (false); - - popupMenuDisconnectPlayer.setEnabled (false); - popupMenuDisconnectPlayer.setVisible (false); - - switchTeamConfirmMessageBox.setEnabled (false); - disconnectPlayerConfirmMessageBox.setEnabled (false); - - disconnectPlayerIndexMap.clear (); - - exitGamePopupMenuIndex = -1; - joinTeamPopupMenuIndex = -1; - pauseGamePopupMenuIndex = -1; - saveGamePopupMenuIndex = -1; - loadGamePopupMenuIndex = -1; - keyboardSetupPopupMenuIndex = -1; - disconnectPlayerPopupMenuIndex = -1; - - isMarkCellEnabled = false; - isMarkCellTextEnabled = false; - - markCellTexture = NULL; - isUnMarkCellEnabled = false; - unmarkCellTexture = NULL; - - currentUIState = NULL; - - scrollSpeed = Config::getInstance ().getFloat ("UiScrollSpeed", "1.5"); - photoModeEnabled = - Config::getInstance ().getBool ("PhotoMode", "false"); - healthbarMode = Config::getInstance ().getInt ("HealthBarMode", "4"); - visibleHUD = Config::getInstance ().getBool ("VisibleHud", "true"); - timeDisplay = Config::getInstance ().getBool ("TimeDisplay", "true"); - withRainEffect = Config::getInstance ().getBool ("RainEffect", "true"); - //MIN_RENDER_FPS_ALLOWED = Config::getInstance().getInt("MIN_RENDER_FPS_ALLOWED",intToStr(MIN_RENDER_FPS_ALLOWED).c_str()); - - mouseX = 0; - mouseY = 0; - mouse2d = 0; - loadingText = ""; - weatherParticleSystem = NULL; - updateFps = 0; - renderFps = 0; - lastUpdateFps = 0; - framesToCatchUpAsClient = 0; - framesToSlowDownAsClient = 0; - lastRenderFps = -1; - avgUpdateFps = -1; - avgRenderFps = -1; - currentAvgRenderFpsTotal = 0; - tickCount = 0; - paused = false; - networkPauseGameForLaggedClientsRequested = false; - networkResumeGameForLaggedClientsRequested = false; - pausedForJoinGame = false; - pausedBeforeJoinGame = false; - resumeRequestSent = false; - pauseRequestSent = false; - pauseStateChanged = false; - gameOver = false; - renderNetworkStatus = false; - renderInGamePerformance = false; - this->speed = 1; - showFullConsole = false; - setMarker = false; - cameraDragAllowed = false; - camLeftButtonDown = false; - camRightButtonDown = false; - camUpButtonDown = false; - camDownButtonDown = false; - - currentCameraFollowUnit = NULL; - currentAmbientSound = NULL; - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - - loadGameNode = NULL; - lastworldFrameCountForReplay = -1; - - lastNetworkPlayerConnectionCheck = time (NULL); - - inJoinGameLoading = false; - quitGameCalled = false; - disableSpeedChange = false; - - for (int i = 0; i < GameConstants::networkSmoothInterval; i++) - { - receivedTooEarlyInFrames[i] = -1; - framesNeededToWaitForServerMessage[i] = -1; - } - - - fadeMusicMilliseconds = - Config::getInstance ().getInt ("GameStartStopFadeSoundMilliseconds", - intToStr (fadeMusicMilliseconds).c_str - ()); - GAME_STATS_DUMP_INTERVAL = - Config::getInstance ().getInt ("GameStatsDumpIntervalSeconds", - intToStr - (GAME_STATS_DUMP_INTERVAL).c_str ()); - - Logger & logger = Logger::getInstance (); - logger.showProgress (); - } - - Game::Game (Program * program, const GameSettings * gameSettings, - bool masterserverMode):ProgramState (program), - lastMousePos (0), isFirstRender (true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->masterserverMode = masterserverMode; - videoPlayer = NULL; - playingStaticVideo = false; - highlightCellTexture = NULL; - playerIndexDisconnect = 0; - updateFpsAvgTest = 0; - renderFpsAvgTest = 0; - cameraDragAllowed = false; - - if (this->masterserverMode == true) - { - printf ("Starting a new game...\n"); - } - - this->program = program; - resetMembers (); - this->gameSettings = *gameSettings; - - Lang::getInstance ().setAllowNativeLanguageTechtree (this-> - gameSettings.getNetworkAllowNativeLanguageTechtree - ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void Game::endGame () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - quitGame (); - sleep (0); - - Object::setStateCallback (NULL); - thisGamePtr = NULL; - if (originalDisplayMsgCallback != NULL) - { - NetworkInterface::setDisplayMessageFunction - (originalDisplayMsgCallback); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Logger & logger = Logger::getInstance (); - Renderer & renderer = Renderer::getInstance (); - - logger.clearHints (); - logger.loadLoadingScreen (""); - logger.setState (Lang::getInstance ().getString ("Deleting")); - //logger.add("Game", true); - logger. - add (Lang:: - getInstance ().getString ("LogScreenGameLoading", "", true), - false); - logger.hideProgress (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // Cannot Fade because sound files will be deleted below - SoundRenderer::getInstance ().stopAllSounds (fadeMusicMilliseconds); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// deleteValues(aiInterfaces.begin(), aiInterfaces.end()); -// aiInterfaces.clear(); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - gui.end (); //selection must be cleared before deleting units - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// world.end(); //must die before selection because of referencers - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] aiInterfaces.size() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - aiInterfaces.size ()); - - // MUST DO THIS LAST!!!! Because objects above have pointers to things like - // unit particles and fade them out etc and this end method deletes the original - // object pointers. - renderer.endGame (false); - - GameConstants::updateFps = original_updateFps; - GameConstants::cameraFps = original_cameraFps; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Unit::setGame (NULL); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ==== END GAME ==== getCurrentPixelByteCount() = " - MG_SIZE_T_SPECIFIER "\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderer.getCurrentPixelByteCount ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "==== END GAME ====\n"); - - FileCRCPreCacheThread *&preCacheCRCThreadPtr = - CacheManager::getCachedItem < - FileCRCPreCacheThread * - >(GameConstants::preCacheThreadCacheLookupKey); - if (preCacheCRCThreadPtr != NULL) - { - preCacheCRCThreadPtr->setPauseForGame (false); - } - - //this->program->reInitGl(); - //renderer.reinitAll(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - - Game::~Game () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - quitGame (); - - Object::setStateCallback (NULL); - thisGamePtr = NULL; - if (originalDisplayMsgCallback != NULL) - { - NetworkInterface::setDisplayMessageFunction - (originalDisplayMsgCallback); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Logger & logger = Logger::getInstance (); - Renderer & renderer = Renderer::getInstance (); - - logger.loadLoadingScreen (""); - logger.setState (Lang::getInstance ().getString ("Deleting")); - //logger.add("Game", true); - logger. - add (Lang:: - getInstance ().getString ("LogScreenGameLoading", "", true), - false); - logger.hideProgress (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // Cannot Fade because sound files will be deleted below - SoundRenderer::getInstance ().stopAllSounds (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - masterController.clearSlaves (true); - deleteValues (aiInterfaces.begin (), aiInterfaces.end ()); - aiInterfaces.clear (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - gui.end (); //selection must be cleared before deleting units - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - world.end (); //must die before selection because of referencers - - BaseColorPickEntity::resetUniqueColors (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] aiInterfaces.size() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - aiInterfaces.size ()); - - delete videoPlayer; - videoPlayer = NULL; - playingStaticVideo = false; - - // MUST DO THIS LAST!!!! Because objects above have pointers to things like - // unit particles and fade them out etc and this end method deletes the original - // object pointers. - renderer.endGame (true); - BaseColorPickEntity::cleanupPBO (); - - GameConstants::updateFps = original_updateFps; - GameConstants::cameraFps = original_cameraFps; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Unit::setGame (NULL); - - Lang::getInstance ().setAllowNativeLanguageTechtree (true); - - FileCRCPreCacheThread *&preCacheCRCThreadPtr = - CacheManager::getCachedItem < - FileCRCPreCacheThread * - >(GameConstants::preCacheThreadCacheLookupKey); - if (preCacheCRCThreadPtr != NULL) - { - preCacheCRCThreadPtr->setPauseForGame (false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ==== END GAME ==== getCurrentPixelByteCount() = " - MG_SIZE_T_SPECIFIER "\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderer.getCurrentPixelByteCount ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "==== END GAME ====\n"); - - //this->program->reInitGl(); - //renderer.reinitAll(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - - bool Game::showTranslatedTechTree ()const - { - return this->gameSettings.getNetworkAllowNativeLanguageTechtree (); - } - - bool Game::quitTriggered () - { - return quitTriggeredIndicator; - } - - Stats Game::quitAndToggleState () - { - //quitGame(); - //Program *program = game->getProgram(); - return quitGame (); - //Game::exitGameState(program, endStats); - } - -// ==================== init and load ==================== - - int Game::ErrorDisplayMessage (const char *msg, bool exitApp) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - msg); - - if (thisGamePtr != NULL) - { - string text = msg; - thisGamePtr->showErrorMessageBox (text, "Error detected", false); - } - - return 0; - } - -//Texture2D * Game::findFactionLogoTexture(const GameSettings *settings, Logger *logger,string factionLogoFilter, bool useTechDefaultIfFilterNotFound) { -// Texture2D *result = NULL; -// string logoFilename = Game::findFactionLogoFile(settings, logger,factionLogoFilter); -// if(logoFilename == "" && factionLogoFilter != "" && useTechDefaultIfFilterNotFound == true) { -// logoFilename = Game::findFactionLogoFile(settings, logger); -// } -// -// result = Renderer::findTexture(logoFilename); -// -// return result; -//} - - string - Game::extractScenarioLogoFile (const GameSettings * settings, - string & result, bool & loadingImageUsed, - Logger * logger, - string factionLogoFilter) - { - string scenarioDir = ""; - if (settings->getScenarioDir () != "") - { - scenarioDir = settings->getScenarioDir (); - if (EndsWith (scenarioDir, ".xml") == true) - { - scenarioDir = scenarioDir.erase (scenarioDir.size () - 4, 4); - scenarioDir = - scenarioDir.erase (scenarioDir.size () - - settings->getScenario ().size (), - settings->getScenario ().size () + 1); - } - - //printf("!!! extractScenarioLogoFile scenarioDir [%s] factionLogoFilter [%s]\n",scenarioDir.c_str(),factionLogoFilter.c_str()); - - vector < string > loadScreenList; - string logoFullPathFilter = scenarioDir + factionLogoFilter; - findAll (logoFullPathFilter, loadScreenList, false, false); - if (loadScreenList.empty () == false) - { - int bestLogoIndex = 0; - - if (loadScreenList.size () > 1 - && EndsWith (factionLogoFilter, ".xml") == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\nLooking for best logo from a list of: " - MG_SIZE_T_SPECIFIER " using filter: [%s]\n", - loadScreenList.size (), logoFullPathFilter.c_str ()); - - int bestMinWidthDiff = INT_MAX; - int bestMinHeightDiff = INT_MAX; - // Now find the best texture for our screen - // Texture2D *result = preloadTexture(logoFilename); - for (unsigned int logoIndex = 0; - logoIndex < loadScreenList.size (); ++logoIndex) - { - string - senarioLogo = scenarioDir + loadScreenList[bestLogoIndex]; - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, senarioLogo.c_str ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Looking for best logo: %u [%s]\n", logoIndex, - senarioLogo.c_str ()); - - if (fileExists (senarioLogo) == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, senarioLogo.c_str ()); - - Texture2D *checkLogo = Renderer::preloadTexture (senarioLogo); - if (checkLogo != NULL) - { - const Metrics & metrics = Metrics::getInstance (); - int - minWidthDifference = - abs (metrics.getScreenW () - - checkLogo->getPixmapConst ()->getW ()); - int - minHeightDifference = - abs (metrics.getScreenH () - - checkLogo->getPixmapConst ()->getH ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Logo info: %d x %d (%d,%d)\n", - checkLogo->getPixmapConst ()->getW (), - checkLogo->getPixmapConst ()->getH (), - minWidthDifference, minHeightDifference); - - if (minWidthDifference < bestMinWidthDiff) - { - bestMinWidthDiff = minWidthDifference; - - bestLogoIndex = logoIndex; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 New best logo is [%s]\n", - senarioLogo.c_str ()); - } - else if (minWidthDifference == bestMinWidthDiff - && minHeightDifference < bestMinHeightDiff) - { - bestMinHeightDiff = minHeightDifference; - - bestLogoIndex = logoIndex; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 New best logo is [%s]\n", - senarioLogo.c_str ()); - } - } - } - } - } - - string senarioLogo = scenarioDir + loadScreenList[bestLogoIndex]; - if (fileExists (senarioLogo) == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] found scenario loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - senarioLogo.c_str ()); - - result = senarioLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - } - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - settings->getScenarioDir ().c_str (), - settings->getScenario ().c_str (), - scenarioDir.c_str ()); - } - return scenarioDir; - } - - string - Game::extractFactionLogoFile (bool & loadingImageUsed, - const string & factionName, - string scenarioDir, - const string & techName, Logger * logger, - string factionLogoFilter) - { - string result = ""; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Searching for faction loading screen\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (factionName == formatString (GameConstants::OBSERVER_SLOTNAME)) - { - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - const string - factionLogo = data_path + "data/core/misc_textures/observer.jpg"; - //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,factionLogo.c_str()); - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - factionLogo.c_str ()); - - result = factionLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - } - } - //else if(settings->getFactionTypeName(i) == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { - else if (factionName == - formatString (GameConstants::RANDOMFACTION_SLOTNAME)) - { - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - const string - factionLogo = data_path + "data/core/misc_textures/random.jpg"; - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - factionLogo.c_str ()); - - result = factionLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - } - } - else - { - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptTechs, scenarioDir); - for (int idx = 0; idx < (int) pathList.size (); idx++) - { - string currentPath = pathList[idx]; - endPathWithSlash (currentPath); - //string path = currentPath + techName + "/" + "factions" + "/" + settings->getFactionTypeName(i); - string - path = - currentPath + techName + "/" + "factions" + "/" + factionName; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] possible loading screen dir '%s'\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - path.c_str ()); - if (isdir (path.c_str ()) == true) - { - endPathWithSlash (path); - - vector < string > loadScreenList; - string logoFullPathFilter = path + factionLogoFilter; - findAll (logoFullPathFilter, loadScreenList, false, false); - if (loadScreenList.empty () == false) - { - int bestLogoIndex = 0; - - if (loadScreenList.size () > 1 - && EndsWith (factionLogoFilter, ".xml") == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\nLooking for best logo from a list of: " - MG_SIZE_T_SPECIFIER - " using filter: [%s]\n", - loadScreenList.size (), - logoFullPathFilter.c_str ()); - - - int bestMinWidthDiff = INT_MAX; - int bestMinHeightDiff = INT_MAX; - // Now find the best texture for our screen - // Texture2D *result = preloadTexture(logoFilename); - for (unsigned int logoIndex = 0; - logoIndex < - (unsigned int) loadScreenList.size (); ++logoIndex) - { - string factionLogo = path + loadScreenList[logoIndex]; - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - factionLogo.c_str ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Looking for best logo: %u [%s]\n", - logoIndex, factionLogo.c_str ()); - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - factionLogo.c_str ()); - - Texture2D *checkLogo = - Renderer::preloadTexture (factionLogo); - if (checkLogo != NULL) - { - const Metrics & metrics = Metrics::getInstance (); - int - minWidthDifference = - abs (metrics.getScreenW () - - checkLogo->getPixmapConst ()->getW ()); - int - minHeightDifference = - abs (metrics.getScreenH () - - checkLogo->getPixmapConst ()->getH ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Logo info: %d x %d (%d,%d)\n", - checkLogo->getPixmapConst ()->getW (), - checkLogo->getPixmapConst ()->getH (), - minWidthDifference, minHeightDifference); - - if (minWidthDifference < bestMinWidthDiff) - { - bestMinWidthDiff = minWidthDifference; - - bestLogoIndex = logoIndex; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("#1 New best logo is [%s]\n", - factionLogo.c_str ()); - } - else if (minWidthDifference == - bestMinWidthDiff - && minHeightDifference < bestMinHeightDiff) - { - bestMinHeightDiff = minHeightDifference; - - bestLogoIndex = logoIndex; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("#2 New best logo is [%s]\n", - factionLogo.c_str ()); - } - } - } - } - } - - string factionLogo = path + loadScreenList[bestLogoIndex]; - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, factionLogo.c_str ()); - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, factionLogo.c_str ()); - - result = factionLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - break; - } - } - // Check if this is a linked faction - else - { - //!!! - string factionXMLFile = path + factionName + ".xml"; - - //printf("A factionXMLFile [%s]\n",factionXMLFile.c_str()); - - if (fileExists (factionXMLFile) == true) - { - XmlTree xmlTreeFaction (XML_RAPIDXML_ENGINE); - std::map < string, string > mapExtraTagReplacementValues; - xmlTreeFaction.load (factionXMLFile, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues), - true, true); - - const XmlNode *rootNode = xmlTreeFaction.getRootNode (); - - //printf("B factionXMLFile [%s] root name [%s] root first child name [%s]\n",factionXMLFile.c_str(),rootNode->getName().c_str(),rootNode->getChild(0)->getName().c_str()); - //printf("B factionXMLFile [%s] root name [%s]\n",factionXMLFile.c_str(),rootNode->getName().c_str()); - if (rootNode->hasChild ("link") == true) - { - rootNode = rootNode->getChild ("link"); - } - if (rootNode->getName () == "link" - && rootNode->hasChild ("techtree") == true) - { - const XmlNode *linkNode = rootNode; - - //printf("C factionXMLFile [%s]\n",factionXMLFile.c_str()); - - //if(linkNode->hasChild("techtree") == true) { - const XmlNode *techtreeNode = - linkNode->getChild ("techtree"); - - string - linkedTechTreeName = - techtreeNode->getAttribute ("name")->getValue (); - - //printf("D factionXMLFile [%s] linkedTechTreeName [%s]\n",factionXMLFile.c_str(),linkedTechTreeName.c_str()); - - if (linkedTechTreeName != "") - { - - string - linkedTechTreePath = - TechTree::findPath (linkedTechTreeName, pathList); - string techTreePath = linkedTechTreePath; - endPathWithSlash (techTreePath); - - string - linkedCurrentPath = - techTreePath + "factions/" + factionName; - endPathWithSlash (linkedCurrentPath); - //string linkedTmppath= linkedCurrentPath + factionName +".xml"; - - path = linkedCurrentPath; - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] possible loading screen dir '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - path.c_str ()); - - //printf("D1 idx = %d\ncurrentPath [%s]\npath [%s]\npathList[idx] [%s]\n",idx,currentPath.c_str(),path.c_str(),pathList[idx].c_str()); - - if (isdir (path.c_str ()) == true) - { - endPathWithSlash (path); - - //printf("E path [%s]\n",path.c_str()); - - loadScreenList.clear (); - findAll (path + factionLogoFilter, - loadScreenList, false, false); - if (loadScreenList.empty () == false) - { - string factionLogo = path + loadScreenList[0]; - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__, - factionLogo.c_str ()); - - //printf("F factionLogo [%s]\n",factionLogo.c_str()); - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug - (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath (__FILE__).c_str - (), __FUNCTION__, __LINE__, - factionLogo.c_str ()); - - result = factionLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - break; - } - } - } - } - //} - } - } - } - } - - if (loadingImageUsed == true) - { - break; - } - } - } - //break; - //} - //} - return result; - } - - string - Game::extractTechLogoFile (string scenarioDir, const string & techName, - bool & loadingImageUsed, Logger * logger, - const string & factionLogoFilter) - { - string result = ""; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Searching for tech loading screen\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptTechs, scenarioDir); - for (int idx = 0; idx < (int) pathList.size (); idx++) - { - string currentPath = pathList[idx]; - endPathWithSlash (currentPath); - string path = currentPath + techName; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] possible loading screen dir '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, path.c_str ()); - if (isdir (path.c_str ()) == true) - { - endPathWithSlash (path); - - vector < string > loadScreenList; - findAll (path + factionLogoFilter, loadScreenList, false, false); - if (loadScreenList.empty () == false) - { - string factionLogo = path + loadScreenList[0]; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, factionLogo.c_str ()); - - if (fileExists (factionLogo) == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found loading screen '%s'\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, factionLogo.c_str ()); - - result = factionLogo; - if (logger != NULL) - { - logger->loadLoadingScreen (result); - } - loadingImageUsed = true; - break; - } - } - } - if (loadingImageUsed == true) - { - break; - } - } - - return result; - } - - void Game::loadHudTexture (const GameSettings * settings) - { - string factionName = ""; - string techName = settings->getTech (); - string - scenarioDir = - extractDirectoryPathFromFile (settings->getScenarioDir ()); - //printf("In loadHudTexture, scenarioDir [%s]\n",scenarioDir.c_str()); - - for (int i = 0; i < settings->getFactionCount (); ++i) - { - if ((settings->getFactionControl (i) == ctHuman) - || (settings->getFactionControl (i) == ctNetwork - && settings->getThisFactionIndex () == i)) - { - factionName = settings->getFactionTypeName (i); - break; - } - } - if (factionName != "") - { - bool hudFound = false; - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptTechs, scenarioDir); - for (int idx = 0; hudFound == false && idx < (int) pathList.size (); - idx++) - { - string currentPath = pathList[idx]; - endPathWithSlash (currentPath); - - vector < string > hudList; - string - path = - currentPath + techName + "/" + "factions" + "/" + factionName; - endPathWithSlash (path); - findAll (path + GameConstants::HUD_SCREEN_FILE_FILTER, hudList, - false, false); - if (hudList.empty () == false) - { - for (unsigned int hudIdx = 0; - hudFound == false - && hudIdx < (unsigned int) hudList.size (); ++hudIdx) - { - string hudImageFileName = path + hudList[hudIdx]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] looking for a HUD [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, hudImageFileName.c_str ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] looking for a HUD [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - hudImageFileName.c_str ()); - - if (fileExists (hudImageFileName) == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] found HUD image [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, hudImageFileName.c_str ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] found HUD image [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - hudImageFileName.c_str ()); - - Texture2D *texture = Renderer::findTexture (hudImageFileName); - gui.setHudTexture (texture); - hudFound = true; - //printf("Hud texture found! \n"); - break; - } - } - } - } - } - } - - string - Game::findFactionLogoFile (const GameSettings * settings, - Logger * logger, - const string & factionLogoFilter) - { - string result = ""; - if (settings == NULL) - { - result = ""; - } - else - { - string mapName = settings->getMap (); - string tilesetName = settings->getTileset (); - string techName = settings->getTech (); - string scenarioName = settings->getScenario (); - bool loadingImageUsed = false; - - if (logger != NULL) - { - logger->setState (Lang::getInstance ().getString ("Loading")); - - if (scenarioName.empty ()) - { - string - scenarioDir = - extractDirectoryPathFromFile (settings->getScenarioDir ()); - TechTree - techTree (Config:: - getInstance ().getPathListForType (ptTechs, - scenarioDir)); - - logger->setSubtitle (formatString (mapName) + " - " + - formatString (tilesetName) + " - " + - formatString (techTree.getTranslatedName - (techName))); - } - else - { - logger->setSubtitle (formatString (scenarioName)); - } - } - - //string scenarioDir = ""; - //bool skipCustomLoadScreen = false; - //if(skipCustomLoadScreen == false) { - string - scenarioDir = - extractScenarioLogoFile (settings, result, loadingImageUsed, - logger, factionLogoFilter); - //} - // try to use a faction related loading screen - //if(skipCustomLoadScreen == false && loadingImageUsed == false) { - if (loadingImageUsed == false) - { - for (int i = 0; i < settings->getFactionCount (); ++i) - { - if (settings->getFactionControl (i) == ctHuman || - (settings->getFactionControl (i) == ctNetwork - && settings->getThisFactionIndex () == i)) - { - - result = - extractFactionLogoFile (loadingImageUsed, - settings->getFactionTypeName (i), - scenarioDir, techName, logger, - factionLogoFilter); - break; - } - } - } - - // try to use a tech related loading screen - //if(skipCustomLoadScreen == false && loadingImageUsed == false){ - if (loadingImageUsed == false) - { - result = extractTechLogoFile (scenarioDir, techName, - loadingImageUsed, logger, - factionLogoFilter); - } - } - return result; - } - - vector < Texture2D * >Game::processTech (string techName) - { - vector < Texture2D * >logoFiles; - bool - enableFactionTexturePreview = - Config::getInstance ().getBool ("FactionPreview", "true"); - if (enableFactionTexturePreview) - { - //string currentTechName_factionPreview = techName; - - vector < string > factions; - vector < string > techPaths = - Config::getInstance ().getPathListForType (ptTechs); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - findAll (techPath + techName + "/factions/*.", factions, false, - false); - - if (factions.empty () == false) - { - for (unsigned int factionIdx = 0; - factionIdx < (unsigned int) factions.size (); ++factionIdx) - { - bool loadingImageUsed = false; - string currentFactionName_factionPreview = factions[factionIdx]; - - string - factionLogo = - Game::extractFactionLogoFile (loadingImageUsed, - currentFactionName_factionPreview, - "", - techName, - NULL, - GameConstants::PREVIEW_SCREEN_FILE_FILTER); - - if (factionLogo == "") - { - factionLogo = - Game::extractFactionLogoFile (loadingImageUsed, - currentFactionName_factionPreview, - "", techName, NULL, - GameConstants::LOADING_SCREEN_FILE_FILTER); - } - if (factionLogo != "") - { - Texture2D *texture = Renderer::preloadTexture (factionLogo); - logoFiles.push_back (texture); - } - } - } - } - } - - return logoFiles; - } - - void Game::load () - { - load (lgt_All); - } - - void Game::load (int loadTypes) - { - bool - showPerfStats = - Config::getInstance ().getBool ("ShowPerfStats", "false"); - Chrono chronoPerf; - if (showPerfStats) - chronoPerf.start (); - char perfBuf[8096] = ""; - std::vector < string > perfList; - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - FileCRCPreCacheThread *&preCacheCRCThreadPtr = - CacheManager::getCachedItem < - FileCRCPreCacheThread * - >(GameConstants::preCacheThreadCacheLookupKey); - if (preCacheCRCThreadPtr != NULL) - { - preCacheCRCThreadPtr->setPauseForGame (true); - } - - std::map < string, vector < pair < string, string > > >loadedFileList; - originalDisplayMsgCallback = - NetworkInterface::getDisplayMessageFunction (); - NetworkInterface::setDisplayMessageFunction (ErrorDisplayMessage); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] loadTypes = %d, gameSettings = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - loadTypes, - this->gameSettings.toString ().c_str ()); - - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (fadeMusicMilliseconds); - - BaseColorPickEntity::resetUniqueColors (); - - Config & config = Config::getInstance (); - Logger & logger = Logger::getInstance (); - - string mapName = gameSettings.getMap (); - string tilesetName = gameSettings.getTileset (); - string techName = gameSettings.getTech (); - string scenarioName = gameSettings.getScenario (); - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - // loadHints - - if (data_path != "") - { - endPathWithSlash (data_path); - } - - string user_data_path = config.getString ("UserData_Root", ""); - if (user_data_path != "") - { - endPathWithSlash (user_data_path); - } - - string - englishFile = - getGameCustomCoreDataPath (data_path, - "data/lang/hint/hint_" + - Lang:: - getInstance ().getDefaultLanguage () + - ".lng"); - string languageFile = - getGameCustomCoreDataPath (data_path, - "data/lang/hint/hint_" + - Lang::getInstance ().getLanguage () + - ".lng"); - string languageFileUserData = - user_data_path + "data/lang/hint/hint_" + - Lang::getInstance ().getLanguage () + ".lng"; - - if (fileExists (languageFileUserData) == true) - { - languageFile = languageFileUserData; - } - if (fileExists (languageFile) == false) - { - // if there is no language specific file use english instead - languageFile = englishFile; - } - if (fileExists (englishFile) == false) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] file [%s] not found\n", - __FILE__, __FUNCTION__, __LINE__, - englishFile.c_str ()); - } - else - { - logger.loadGameHints (englishFile, languageFile, true); - - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - } - - if ((loadTypes & lgt_FactionPreview) == lgt_FactionPreview) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - Game::findFactionLogoFile (&gameSettings, &logger); - - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - loadHudTexture (&gameSettings); - - const string - markCellTextureFilename = - data_path + "data/core/misc_textures/mark_cell.png"; - markCellTexture = Renderer::findTexture (markCellTextureFilename); - const string - unmarkCellTextureFilename = - data_path + "data/core/misc_textures/unmark_cell.png"; - unmarkCellTexture = Renderer::findTexture (unmarkCellTextureFilename); - const string - highlightCellTextureFilename = - data_path + "data/core/misc_textures/pointer.png"; - highlightCellTexture = - Renderer::findTexture (highlightCellTextureFilename); - - string scenarioDir = ""; - if (gameSettings.getScenarioDir () != "") - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - scenarioDir = gameSettings.getScenarioDir (); - if (EndsWith (scenarioDir, ".xml") == true) - { - scenarioDir = scenarioDir.erase (scenarioDir.size () - 4, 4); - scenarioDir = - scenarioDir.erase (scenarioDir.size () - - gameSettings.getScenario ().size (), - gameSettings.getScenario ().size () + 1); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //tileset - if ((loadTypes & lgt_TileSet) == lgt_TileSet) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - world. - loadTileset (config.getPathListForType (ptTilesets, scenarioDir), - tilesetName, &checksum, loadedFileList); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - set < string > factions; - for (int i = 0; i < gameSettings.getFactionCount (); ++i) - { - factions.insert (gameSettings.getFactionTypeName (i)); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if ((loadTypes & lgt_TechTree) == lgt_TechTree) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //tech, load before map because of resources - world.loadTech (config.getPathListForType (ptTechs, scenarioDir), - techName, factions, &checksum, loadedFileList); - - if (world.getTechTree () == NULL - || world.getTechTree ()->getNameUntranslated () == "") - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref: %d, ERROR: Cannot find techtree: [%s]", - __LINE__, techName.c_str ()); - - throw megaglest_runtime_error (szBuf, true); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //map - if ((loadTypes & lgt_Map) == lgt_Map) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - world.loadMap (Config::getMapPath (mapName, scenarioDir), &checksum); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //scenario - if ((loadTypes & lgt_Scenario) == lgt_Scenario) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - if (scenarioName.empty () == false) - { - - bool - isTutorial = - Scenario::isGameTutorial (gameSettings.getScenarioDir ()); - //printf("Loading scenario gameSettings.getScenarioDir() [%s] scenarioName [%s] isTutorial: %d\n",gameSettings.getScenarioDir().c_str(),scenarioName.c_str(),isTutorial); - - Lang::getInstance (). - loadScenarioStrings (gameSettings.getScenarioDir (), scenarioName, - isTutorial); - - //printf("In [%s::%s Line: %d] rootNode [%p][%s]\n",__FILE__,__FUNCTION__,__LINE__,loadGameNode,(loadGameNode != NULL ? loadGameNode->getName().c_str() : "none")); - world.loadScenario (gameSettings.getScenarioDir (), &checksum, - false, loadGameNode); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - SDL_PumpEvents (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if (showPerfStats && chronoPerf.getMillis () >= 50) - { - for (unsigned int x = 0; x < perfList.size (); ++x) - { - printf ("%s", perfList[x].c_str ()); - } - } - } - - void Game::init () - { - init (false); - } - - void Game::init (bool initForPreviewOnly) - { - bool - showPerfStats = - Config::getInstance ().getBool ("ShowPerfStats", "false"); - Chrono chronoPerf; - if (showPerfStats) - chronoPerf.start (); - char perfBuf[8096] = ""; - std::vector < string > perfList; - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] initForPreviewOnly = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - initForPreviewOnly); - - Lang & lang = Lang::getInstance (); - Logger & logger = Logger::getInstance (); - CoreData & coreData = CoreData::getInstance (); - Renderer & renderer = Renderer::getInstance (); - Map *map = world.getMap (); - NetworkManager & networkManager = NetworkManager::getInstance (); - - GameSettings::playerDisconnectedText = - "*" + lang.getString ("AI") + "* "; - - if (map == NULL) - { - throw megaglest_runtime_error ("map == NULL"); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (initForPreviewOnly == false) - { - logger.setState (lang.getString ("Initializing")); - - //message box - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - mainMessageBox.setEnabled (false); - - //message box - errorMessageBox.init (lang.getString ("Ok")); - errorMessageBox.setEnabled (false); - errorMessageBox.setY (20); - - - //init world, and place camera - commander.init (&world); - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - } - - try - { - world.init (this, gameSettings.getDefaultUnits ()); - } - catch (const megaglest_runtime_error & ex) - { - string sErrBuf = ""; - if (ex.wantStackTrace () == true) - { - char szErrBuf[8096] = ""; - snprintf (szErrBuf, 8096, "In [%s::%s %d]", __FILE__, - __FUNCTION__, __LINE__); - sErrBuf = - string (szErrBuf) + string ("\nerror [") + string (ex.what ()) + - string ("]\n"); - } - else - { - sErrBuf = ex.what (); - } - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - sErrBuf.c_str ()); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - } - catch (const exception & ex) - { - char szErrBuf[8096] = ""; - snprintf (szErrBuf, 8096, "In [%s::%s %d]", __FILE__, __FUNCTION__, - __LINE__); - string - sErrBuf = - string (szErrBuf) + string ("\nerror [") + string (ex.what ()) + - string ("]\n"); - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - sErrBuf.c_str ()); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (loadGameNode != NULL) - { - //world.getMapPtr()->loadGame(loadGameNode,&world); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (initForPreviewOnly == false) - { - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - gui.init (this); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - //SDL_PumpEvents(); - - chatManager.init (&console, world.getThisTeamIndex ()); - console.clearStoredLines (); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (this->loadGameNode == NULL) - { - initCamera (map); - } - else - { - gui.loadGame (loadGameNode, &world); - - if (inJoinGameLoading == true) - { - initCamera (map); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - NetworkRole role = nrIdle; - if (initForPreviewOnly == false) - { - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - scriptManager.init (&world, &gameCamera, loadGameNode); - - //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] creating AI's\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //create AIs - - bool - enableServerControlledAI = - this->gameSettings.getEnableServerControlledAI (); - bool isNetworkGame = this->gameSettings.isNetworkGame (); - role = networkManager.getNetworkRole (); - - masterController.clearSlaves (true); - deleteValues (aiInterfaces.begin (), aiInterfaces.end ()); - - std::vector < SlaveThreadControllerInterface * >slaveThreadList; - aiInterfaces.resize (world.getFactionCount ()); - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - //printf("Controltype = %d for index = %d\n",faction->getControlType(),i); - - if (faction->getCpuControl (enableServerControlledAI, isNetworkGame, - role) == true) - { - //printf("** Loading AI player for Controltype = %d for index = %d\n",faction->getControlType(),i); - - aiInterfaces[i] = new AiInterface (*this, i, faction->getTeam ()); - if (loadGameNode != NULL) - { - aiInterfaces[i]->loadGame (loadGameNode, faction); - } - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - Lang::getInstance ().getString - ("LogScreenGameLoadingCreatingAIFaction", "", - true).c_str (), i); - logger.add (szBuf, true); - - slaveThreadList.push_back (aiInterfaces[i]->getWorkerThread ()); - } - else - { - aiInterfaces[i] = NULL; - } - } - if (Config:: - getInstance ().getBool ("EnableNewThreadManager", - "false") == true) - { - masterController.setSlaves (slaveThreadList); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - if (world.getFactionCount () == 1 - && world.getFaction (0)->getPersonalityType () == fpt_Observer) - { - withRainEffect = false; - } - - if (withRainEffect) - { - //weather particle systems - if (world.getTileset ()->getWeather () == wRainy) - { - logger.add (Lang::getInstance ().getString - ("LogScreenGameLoadingCreatingRainParticles", - "", true), true); - weatherParticleSystem = new RainParticleSystem (); - weatherParticleSystem->setSpeed (12.f / GameConstants::updateFps); - weatherParticleSystem->setPos (gameCamera.getPos ()); - renderer.manageParticleSystem (weatherParticleSystem, rsGame); - } - else if (world.getTileset ()->getWeather () == wSnowy) - { - logger.add (Lang::getInstance ().getString - ("LogScreenGameLoadingCreatingSnowParticles", - "", true), true); - weatherParticleSystem = new SnowParticleSystem (1200); - weatherParticleSystem->setSpeed (1.5f / GameConstants::updateFps); - weatherParticleSystem->setPos (gameCamera.getPos ()); - weatherParticleSystem->setTexture (coreData.getSnowTexture ()); - renderer.manageParticleSystem (weatherParticleSystem, rsGame); - } - } - else if (world.getTileset ()->getWeather () == wRainy) - { - world.getTileset ()->setWeather (wSunny); - } - - renderer.manageDeferredParticleSystems (); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //init renderer state - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Initializing renderer\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__); - logger. - add (Lang:: - getInstance ().getString ("LogScreenGameLoadingInitRenderer", "", - true), true); - - //printf("Before renderer.initGame\n"); - renderer.initGame (this, this->getGameCameraPtr ()); - //printf("After renderer.initGame\n"); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - if (faction != NULL) - { - faction->deletePixels (); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (initForPreviewOnly == false) - { - //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - // give CPU time to update other things to avoid apperance of hanging - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Waiting for network\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__); - logger. - add (Lang:: - getInstance ().getString - ("LogScreenGameLoadingWaitForNetworkPlayers", "", true), true); - networkManager.getGameNetworkInterface ()->waitUntilReady (&checksum); - - //std::string worldLog = world.DumpWorldToLog(true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Starting music stream\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - logger. - add (Lang:: - getInstance ().getString ("LogScreenGameLoadingStartingMusic", - "", true), true); - - if (this->masterserverMode == false) - { - if (world.getThisFaction () == NULL) - { - throw megaglest_runtime_error ("world.getThisFaction() == NULL"); - } - if (world.getThisFaction ()->getType () == NULL) - { - throw - megaglest_runtime_error - ("world.getThisFaction()->getType() == NULL"); - } - //if(world.getThisFaction()->getType()->getMusic() == NULL) { - // throw megaglest_runtime_error("world.getThisFaction()->getType()->getMusic() == NULL"); - //} - } - - //sounds - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (fadeMusicMilliseconds); - soundRenderer = SoundRenderer::getInstance (); - - Tileset *tileset = world.getTileset (); - AmbientSounds *ambientSounds = tileset->getAmbientSounds (); - - //rain - if (tileset->getWeather () == wRainy - && ambientSounds->isEnabledRain ()) - { - logger. - add (Lang:: - getInstance ().getString - ("LogScreenGameLoadingStartingAmbient", "", true), true); - currentAmbientSound = ambientSounds->getRain (); - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - soundRenderer.playAmbient (currentAmbientSound); - } - - //snow - if (tileset->getWeather () == wSnowy - && ambientSounds->isEnabledSnow ()) - { - logger. - add (Lang:: - getInstance ().getString - ("LogScreenGameLoadingStartingAmbient", "", true), true); - currentAmbientSound = ambientSounds->getSnow (); - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - soundRenderer.playAmbient (currentAmbientSound); - } - - if (this->masterserverMode == false) - { - StrSound *gameMusic = - world.getThisFaction ()->getType ()->getMusic (); - soundRenderer.playMusic (gameMusic); - } - - logger. - add (Lang:: - getInstance ().getString ("LogScreenGameLoadingLaunchGame", "", - true)); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //throw "test"; - - logger.setCancelLoadingEnabled (false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "================ STARTING GAME ================\n"); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPathFinder).enabled) - SystemFlags::OutputDebug (SystemFlags::debugPathFinder, - "================ STARTING GAME ================\n"); - setupPopupMenus (false); - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - //printf("Check for team switch to observer i = %d, team = %d [%d]\n",i,faction->getTeam(),(GameConstants::maxPlayers -1 + fpt_Observer)); - if (faction != NULL - && faction->getTeam () == - GameConstants::maxPlayers - 1 + fpt_Observer) - { - faction->setPersonalityType (fpt_Observer); - world.getStats ()->setPersonalityType (i, - faction->getPersonalityType - ()); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (role == nrClient) - { - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - if (clientInterface != NULL - && clientInterface->getResumeInGameJoin () == true) - { - - //printf("Client sending resume message to server...\n"); - - clientInterface->sendResumeGameMessage (); - //this->initialResumeSpeedLoops = true; - } - } - - printf ("Game unique identifier is: %s\n", - this->gameSettings.getGameUUID ().c_str ()); - - gameStarted = true; - - if (this->masterserverMode == true) - { - world.getStats ()->setIsMasterserverMode (true); - - printf ("New game has started...\n"); - } - - if (isFlagType1BitEnabled (ft1_network_synch_checks_verbose) == true) - { - printf ("*Note: Monitoring Network CRC VERBOSE synchronization...\n"); - } - else if (isFlagType1BitEnabled (ft1_network_synch_checks) == true) - { - printf ("*Note: Monitoring Network CRC NORMAL synchronization...\n"); - } - - //NetworkRole role = networkManager.getNetworkRole(); - if (role == nrServer) - { - networkManager.initServerInterfaces (this); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ==== START GAME ==== getCurrentPixelByteCount() = " - MG_SIZE_T_SPECIFIER "\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderer.getCurrentPixelByteCount ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "=============================================\n"); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "==== START GAME ====\n"); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "=============================================\n"); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "Starting framecount: %d\n", - world.getFrameCount ()); - - if (showPerfStats && chronoPerf.getMillis () >= 50) - { - for (unsigned int x = 0; x < perfList.size (); ++x) - { - printf ("%s", perfList[x].c_str ()); - } - } - } - - void Game::initCamera (Map * map) - { - gameCamera.init (map->getW (), map->getH ()); - - // camera default height calculation - if (map->getCameraHeight () > 0 - && gameCamera.getCalculatedDefault () < map->getCameraHeight ()) - { - gameCamera.setCalculatedDefault (map->getCameraHeight ()); - } - else if (gameCamera.getCalculatedDefault () < - map->getMaxMapHeight () + 13.0f) - { - gameCamera.setCalculatedDefault (map->getMaxMapHeight () + 13.0f); - } - - if (world.getThisFaction () != NULL) - { - const - Vec2i & - v = - map->getStartLocation (world. - getThisFaction ()->getStartLocationIndex ()); - // This args are set in map.cpp - Map::getStartLocation() - gameCamera.setPos (Vec2f - (v.x, - v.y + gameCamera.getCalculatedDefault () / 2)); - // - // for issue 13: observer mode (wip) - // This sets the camera position the same for each player. - // The goal is to set the camera position to this for observers only - // since they don't have a StartLocationIndex - // gameCamera.setPos(Vec2f(10, 10)); - - } - } - -// ==================== update ==================== - - void Game::reInitGUI () - { - gui.init (this); - } - - void Game::setupPopupMenus (bool checkClientAdminOverrideOnly) - { - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - ClientInterface *clientInterface = NULL; - ServerInterface *serverInterface = NULL; - - bool allowAdminMenuItems = false; - bool forceJoinInProgressUpdate = false; - if (role == nrServer) - { - allowAdminMenuItems = true; - - if (disconnectPlayerPopupMenuIndex == -1) - { - serverInterface = - dynamic_cast < - ServerInterface * >(networkManager.getServerInterface ()); - if (serverInterface != NULL && checkClientAdminOverrideOnly == true) - { - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (serverInterface->getSlotMutex - (faction->getStartLocationIndex ()), CODE_AT_LINE); - ConnectionSlot *slot = - serverInterface->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getConnectHasHandshaked () == true - && slot->getCurrentFrameCount () <= 0) - { - //printf("Connected slot can be disconnected: %d\n",slot->getPlayerIndex()); - - forceJoinInProgressUpdate = true; - break; - } - } - } - } - } - else if (role == nrClient) - { - clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - - if (clientInterface != NULL && - (gameSettings.getMasterserver_admin () == - clientInterface->getSessionKey () - || clientInterface->isMasterServerAdminOverride () == true)) - { - allowAdminMenuItems = true; - } - } - - if (checkClientAdminOverrideOnly == false || - forceJoinInProgressUpdate == true || - (clientInterface != NULL && - (gameSettings.getMasterserver_admin () != - clientInterface->getSessionKey () - && clientInterface->isMasterServerAdminOverride () == true))) - { - exitGamePopupMenuIndex = -1; - joinTeamPopupMenuIndex = -1; - pauseGamePopupMenuIndex = -1; - saveGamePopupMenuIndex = -1; - loadGamePopupMenuIndex = -1; - keyboardSetupPopupMenuIndex = -1; - disconnectPlayerPopupMenuIndex = -1; - - if (checkClientAdminOverrideOnly == true && clientInterface != NULL) - { - gameSettings. - setMasterserver_admin (clientInterface->getSessionKey ()); - gameSettings.setMasterserver_admin_faction_index - (clientInterface->getPlayerIndex ()); - } - //PopupMenu popupMenu; - std::vector < string > menuItems; - menuItems.push_back (" " + lang.getString ("ExitGameMenu") + " "); - exitGamePopupMenuIndex = (int) menuItems.size () - 1; - - if ((gameSettings.getFlagTypes1 () & ft1_allow_team_switching) == - ft1_allow_team_switching && world.getThisFaction () != NULL - && world.getThisFaction ()->getPersonalityType () != fpt_Observer) - { - menuItems.push_back (" " + lang.getString ("JoinOtherTeam") + - " "); - joinTeamPopupMenuIndex = (int) menuItems.size () - 1; - } - - if (allowAdminMenuItems == true) - { - menuItems.push_back (" " + lang.getString ("PauseResumeGame") + - " "); - pauseGamePopupMenuIndex = (int) menuItems.size () - 1; - - if (gameSettings.isNetworkGame () == false - || gameSettings.getScenario () != "") - { - menuItems.push_back (" " + lang.getString ("SaveGame") + " "); - saveGamePopupMenuIndex = (int) menuItems.size () - 1; - } - - if (gameSettings.isNetworkGame () == true) - { - menuItems.push_back (" " + - lang.getString ("DisconnectNetorkPlayer") + - " "); - disconnectPlayerPopupMenuIndex = (int) menuItems.size () - 1; - } - } - menuItems.push_back (" " + lang.getString ("KeyboardsetupL") + " "); - keyboardSetupPopupMenuIndex = (int) menuItems.size () - 1; - - menuItems.push_back (" " + lang.getString ("Cancel") + " "); - - popupMenu.setW (100); - popupMenu.setH (100); - popupMenu.init (" " + lang.getString ("GameMenuTitle") + " ", - menuItems); - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - - popupMenuSwitchTeams.setEnabled (false); - popupMenuSwitchTeams.setVisible (false); - - popupMenuDisconnectPlayer.setEnabled (false); - popupMenuDisconnectPlayer.setVisible (false); - } - } - - void Game::processNetworkSynchChecksIfRequired () - { - bool isNetworkGame = this->gameSettings.isNetworkGame (); - if (isNetworkGame == true - && NetworkManager::getInstance ().getGameNetworkInterface () != - NULL) - { - GameSettings *settings = world.getGameSettingsPtr (); - if (settings != NULL) - { - bool calculateNetworkCRC = false; - - if (isFlagType1BitEnabled (ft1_network_synch_checks) == true || - isFlagType1BitEnabled (ft1_network_synch_checks_verbose) == - true) - { - calculateNetworkCRC = true; - } - - if (calculateNetworkCRC == true) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - - NetworkInterface *netIntf = - networkManager.getGameNetworkInterface (); - for (int index = 0; index < GameConstants::maxPlayers; ++index) - { - if (index < world.getFactionCount ()) - { - Faction *faction = world.getFaction (index); - netIntf->setNetworkPlayerFactionCRC (index, - faction->getCRC ().getSum - ()); - - if (settings != NULL) - { - if (isFlagType1BitEnabled - (ft1_network_synch_checks_verbose) == true) - { - faction->addCRC_DetailsForWorldFrame (world.getFrameCount - (), - role == nrServer); - } - else - if (isFlagType1BitEnabled - (ft1_network_synch_checks) == true - && world.getFrameCount () % 20 == 0) - { - faction->addCRC_DetailsForWorldFrame (world.getFrameCount - (), - role == nrServer); - } - } - } - else - { - netIntf->setNetworkPlayerFactionCRC (index, 0); - } - } - } - } - } - } - -//update - void Game::update () - { - try - { - if (currentUIState != NULL) - { - currentUIState->update (); - } - - bool - showPerfStats = - Config::getInstance ().getBool ("ShowPerfStats", "false"); - Chrono chronoPerf; - char perfBuf[8096] = ""; - std::vector < string > perfList; - if (showPerfStats) - chronoPerf.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "=============== FRAME: %d In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", world.getFrameCount (), - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - Chrono chronoGamePerformanceCounts; - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - - // a) Updates non dependent on speed - - // set game stats for host - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - if (role == nrServer) - { - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (false); - if (server != NULL) - { - server->setGameStats (world.getStats ()); - } - } - - bool - pendingQuitError = (quitPendingIndicator == true || - (NetworkManager:: - getInstance ().getGameNetworkInterface () != - NULL - && NetworkManager:: - getInstance ().getGameNetworkInterface ()-> - getQuit ())); - - if (pendingQuitError == true && - (this->masterserverMode == true || - (mainMessageBox.getEnabled () == false - && errorMessageBox.getEnabled () == false))) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - quitTriggeredIndicator = true; - return; - } - - if (this->masterserverMode == false) - { - if (world.getFactionCount () > 0 - && world.getThisFaction ()->getFirstSwitchTeamVote () != NULL) - { - const SwitchTeamVote *vote = - world.getThisFaction ()->getFirstSwitchTeamVote (); - GameSettings *settings = world.getGameSettingsPtr (); - - Lang & lang = Lang::getInstance (); - - char szBuf[8096] = ""; - if (lang.hasString ("AllowPlayerJoinTeam") == true) - { - snprintf (szBuf, 8096, - lang.getString ("AllowPlayerJoinTeam").c_str (), - settings-> - getNetworkPlayerName (vote->factionIndex).c_str (), - vote->oldTeam, vote->newTeam); - } - else - { - snprintf (szBuf, 8096, - "Allow player [%s] to join your team\n(changing from team# %d to team# %d)?", - settings-> - getNetworkPlayerName (vote->factionIndex).c_str (), - vote->oldTeam, vote->newTeam); - } - - switchTeamConfirmMessageBox.setText (szBuf); - switchTeamConfirmMessageBox.init (lang.getString ("Yes"), - lang.getString ("No")); - switchTeamConfirmMessageBox.setEnabled (true); - - world. - getThisFactionPtr ()->setCurrentSwitchTeamVoteFactionIndex - (vote->factionIndex); - } - } - - //misc - updateFps++; - mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; - - //console - console.update (); - - // b) Updates depandant on speed - int updateLoops = getUpdateLoops (); - - // Temp speed boost when player first joins an in progress game - if (this->initialResumeSpeedLoops == true) - { - printf ("Resume In Progress Game: %d\n", __LINE__); - - this->initialResumeSpeedLoops = false; - //updateLoops = 80; - } - - chronoGamePerformanceCounts.start (); - bool - enableServerControlledAI = - this->gameSettings.getEnableServerControlledAI (); - - if (role == nrClient && updateLoops == 1 - && world.getFrameCount () >= - (gameSettings.getNetworkFramePeriod () * 2)) - { - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - if (clientInterface != NULL) - { - uint64 - lastNetworkFrameFromServer = - clientInterface->getCachedLastPendingFrameCount (); - - ///////////////////////////////// - // TTTT new attempt to make things smoother: - /////////////// - - //////////////////////////////////////////// - //get stats of received/waiting for packages - //////////////////////////////////////////// - // calculate current receive Index slot: - int - index = ((world.getFrameCount () - - (world.getFrameCount () - % gameSettings.getNetworkFramePeriod ())) - / gameSettings.getNetworkFramePeriod ()) - % GameConstants::networkSmoothInterval; - - // clean the next frame slot - receivedTooEarlyInFrames[(index + - 1) % - GameConstants::networkSmoothInterval] = - -1; - framesNeededToWaitForServerMessage[(index + - 1) % - GameConstants::networkSmoothInterval] - = -1; - - if (receivedTooEarlyInFrames[index] == -1) - { - // we need to check if we already received something for next frame - if (lastNetworkFrameFromServer > 0 - && lastNetworkFrameFromServer > - (uint64) world.getFrameCount ()) - { - receivedTooEarlyInFrames[index] = - lastNetworkFrameFromServer - world.getFrameCount (); - } - } - if (framesNeededToWaitForServerMessage[index] == -1) - { - // calc time waiting for message in milliseconds to frames - int64 - timeClientWaitedForLastMessage = - clientInterface->getTimeClientWaitedForLastMessage (); - if (timeClientWaitedForLastMessage > 0) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("world.getFrameCount():%d index %d Client waited:%d ms\n", - world.getFrameCount (), index, - (int) timeClientWaitedForLastMessage); - framesNeededToWaitForServerMessage[index] = - timeClientWaitedForLastMessage * - GameConstants::updateFps / 1000; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("ClienttimeClientWaitedForLastMessage:%d ms which is %d frames \n", - (int) timeClientWaitedForLastMessage, - framesNeededToWaitForServerMessage[index]); - } - else - { - framesNeededToWaitForServerMessage[index] = 0; - } - } - - //////////////////////////////////////////// - //use the recorded stats of received/waiting for packages - //////////////////////////////////////////// - //lets see if the client is in front and had to wait for messages ... - - //lets see if all last recorded frames where received too early - int minimum = 0; - int allowedMaxFallback = 5; - int countOfMessagesReceivedTooEarly = 0; - int countOfMessagesReceivedTooLate = 0; - int sumOfTooLateFrames = 0; - bool cleanupStats = false; - - for (int i = 0; i < GameConstants::networkSmoothInterval; i++) - { - if (receivedTooEarlyInFrames[i] > allowedMaxFallback) - { - countOfMessagesReceivedTooEarly++; - if (minimum == 0 || minimum > receivedTooEarlyInFrames[i]) - { - minimum = receivedTooEarlyInFrames[i]; - } - } - if (framesNeededToWaitForServerMessage[i] > 0) - { - countOfMessagesReceivedTooLate++; - sumOfTooLateFrames += framesNeededToWaitForServerMessage[i]; - } - } - - if (countOfMessagesReceivedTooEarly == GameConstants::networkSmoothInterval - 1) // -1 because slot for next frame is already initialized - { // all packages where too early - // we catch up the minimum-catchupInterval of what we recorded - framesToCatchUpAsClient = minimum - allowedMaxFallback; - framesToSlowDownAsClient = 0; - cleanupStats = true; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Worldframe %d : Client will speed up: %d frames\n", - world.getFrameCount (), framesToCatchUpAsClient); - } - else if (countOfMessagesReceivedTooLate > 3) - { - framesToSlowDownAsClient = - sumOfTooLateFrames / countOfMessagesReceivedTooLate; - framesToCatchUpAsClient = 0; - cleanupStats = true; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Worldframe %d : Client will slow down: %d frames\n", - world.getFrameCount (), framesToSlowDownAsClient); - } - - if (cleanupStats == true) - { - // Once we decided to use the stats to do some correction, we reset/cleanup our recorded stats - for (int i = 0; i < GameConstants::networkSmoothInterval; i++) - { - receivedTooEarlyInFrames[i] = -1; - framesNeededToWaitForServerMessage[i] = -1; - } - } - } - } - // if game is paused don't try to catch up - if (updateLoops > 0) - { - // we catch up a bit smoother with updateLoops = 2 - if (framesToCatchUpAsClient > 0) - { - updateLoops = 2; - framesToCatchUpAsClient = framesToCatchUpAsClient - 1; - } - if (framesToSlowDownAsClient > 0) - { // slowdown still the hard way. - updateLoops = 0; - framesToSlowDownAsClient = framesToSlowDownAsClient - 1; - } - } - - addPerformanceCount ("CalculateNetworkUpdateLoops", - chronoGamePerformanceCounts.getMillis ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [before ReplaceDisconnectedNetworkPlayersWithAI]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // Check to see if we are playing a network game and if any players - // have disconnected? - bool isNetworkGame = this->gameSettings.isNetworkGame (); - - chronoGamePerformanceCounts.start (); - - ReplaceDisconnectedNetworkPlayersWithAI (isNetworkGame, role); - - addPerformanceCount ("ReplaceDisconnectedNetworkPlayersWithAI", - chronoGamePerformanceCounts.getMillis ()); - - setupPopupMenus (true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [after ReplaceDisconnectedNetworkPlayersWithAI]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (updateLoops > 0) - { - // update the frame based timer in the stats with at least one step - world.getStats ()->addFramesToCalculatePlaytime (); - - //update - Chrono chronoReplay; - int64 lastReplaySecond = -1; - int replayCommandsPlayed = 0; - int replayTotal = commander.getReplayCommandListForFrameCount (); - if (replayTotal > 0) - { - chronoReplay.start (); - } - - do - { - if (replayTotal > 0) - { - replayCommandsPlayed = - (replayTotal - - commander.getReplayCommandListForFrameCount ()); - } - for (int i = 0; i < updateLoops; ++i) - { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //AiInterface - if (commander.hasReplayCommandListForFrame () == false) - { - chronoGamePerformanceCounts.start (); - - processNetworkSynchChecksIfRequired (); - - addPerformanceCount ("CalculateNetworkCRCSynchChecks", - chronoGamePerformanceCounts.getMillis - ()); - - const bool - newThreadManager = - Config::getInstance ().getBool ("EnableNewThreadManager", - "false"); - if (newThreadManager == true) - { - int currentFrameCount = world.getFrameCount (); - masterController.signalSlaves (¤tFrameCount); - //bool slavesCompleted = masterController.waitTillSlavesTrigger(20000); - masterController.waitTillSlavesTrigger (20000); - } - else - { - // Signal the faction threads to do any pre-processing - chronoGamePerformanceCounts.start (); - - bool hasAIPlayer = false; - for (int j = 0; j < world.getFactionCount (); ++j) - { - Faction *faction = world.getFaction (j); - - //printf("Faction Index = %d enableServerControlledAI = %d, isNetworkGame = %d, role = %d isCPU player = %d scriptManager.getPlayerModifiers(j)->getAiEnabled() = %d\n",j,enableServerControlledAI,isNetworkGame,role,faction->getCpuControl(enableServerControlledAI,isNetworkGame,role),scriptManager.getPlayerModifiers(j)->getAiEnabled()); - - if (faction->getCpuControl (enableServerControlledAI, - isNetworkGame, - role) == true - && scriptManager. - getPlayerModifiers (j)->getAiEnabled () == true) - { - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags:: - OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] [i = %d] faction = %d, factionCount = %d, took msecs: %lld [before AI updates]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, j, - world.getFactionCount (), - chrono.getMillis ()); - aiInterfaces[j]->signalWorkerThread (world.getFrameCount - ()); - hasAIPlayer = true; - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (hasAIPlayer == true) - { - //sleep(0); - - Chrono chronoAI; - chronoAI.start (); - - const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000; - for (; - chronoAI.getMillis () < - MAX_FACTION_THREAD_WAIT_MILLISECONDS;) - { - bool workThreadsFinished = true; - for (int j = 0; j < world.getFactionCount (); ++j) - { - Faction *faction = world.getFaction (j); - if (faction == NULL) - { - throw megaglest_runtime_error ("faction == NULL"); - } - if (faction->getCpuControl - (enableServerControlledAI, - isNetworkGame, role) == true - && - scriptManager.getPlayerModifiers (j)->getAiEnabled - () == true) - { - if (aiInterfaces[j]->isWorkerThreadSignalCompleted - (world.getFrameCount ()) == false) - { - workThreadsFinished = false; - break; - } - } - } - if (workThreadsFinished == false) - { - //sleep(0); - } - else - { - break; - } - } - } - - addPerformanceCount ("ProcessAIWorkerThreads", - chronoGamePerformanceCounts.getMillis - ()); - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - } - else - { - // Simply show a progress message while replaying commands - if (lastReplaySecond < chronoReplay.getSeconds ()) - { - lastReplaySecond = chronoReplay.getSeconds (); - Renderer & renderer = Renderer::getInstance (); - renderer.clearBuffers (); - renderer.clearZBuffer (); - renderer.reset2d (); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Please wait, loading game with replay [%d / %d]...", - replayCommandsPlayed, replayTotal); - string text = szBuf; - if (Renderer::renderText3DEnabled) - { - Font3D *font = - CoreData::getInstance ().getMenuFontBig3D (); - const Metrics & metrics = Metrics::getInstance (); - int w = metrics.getVirtualW (); - int - renderX = - (w / 2) - - (font->getMetrics ()->getTextWidth (text) / 2); - int h = metrics.getVirtualH (); - int - renderY = - (h / 2) + (font->getMetrics ()->getHeight (text) / 2); - - renderer.renderText3D (text, font, - Vec3f (1.f, 1.f, 0.f), - renderX, renderY, false); - } - else - { - Font2D *font = CoreData::getInstance ().getMenuFontBig (); - const Metrics & metrics = Metrics::getInstance (); - int w = metrics.getVirtualW (); - int renderX = (w / 2); - int h = metrics.getVirtualH (); - int renderY = (h / 2); - - renderer.renderText (text, font, - Vec3f (1.f, 1.f, 0.f), - renderX, renderY, true); - } - - renderer.swapBuffers (); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [AI updates]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //World - chronoGamePerformanceCounts.start (); - - if (pendingQuitError == false) - world.update (); - - addPerformanceCount ("ProcessWorldUpdate", - chronoGamePerformanceCounts.getMillis ()); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [world update i = %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis (), i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (currentCameraFollowUnit != NULL) - { - Vec3f c = currentCameraFollowUnit->getCurrMidHeightVector (); - int rotation = currentCameraFollowUnit->getRotation (); - float angle = rotation + 180; - - c.z = c.z + 4 * std::cos (degToRad (angle)); - c.x = c.x + 4 * std::sin (degToRad (angle)); - - c.y = - c.y + - currentCameraFollowUnit->getType ()->getHeight () / - 2.f + 2.0f; - - getGameCameraPtr ()->setPos (c); - - rotation = (540 - rotation) % 360; - getGameCameraPtr ()->rotateToVH (18.0f, rotation); - - if (currentCameraFollowUnit->isAlive () == false) - { - currentCameraFollowUnit = NULL; - getGameCameraPtr ()->setState (GameCamera::sGame); - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - // Commander - chronoGamePerformanceCounts.start (); - - if (pendingQuitError == false) - { - commander.signalNetworkUpdate (this); - } - - addPerformanceCount ("ProcessNetworkUpdate", - chronoGamePerformanceCounts.getMillis ()); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [commander updateNetwork i = %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis (), i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //Gui - chronoGamePerformanceCounts.start (); - - gui.update (); - - addPerformanceCount ("ProcessGUIUpdate", - chronoGamePerformanceCounts.getMillis ()); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [gui updating i = %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis (), i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //Particle systems - if (weatherParticleSystem != NULL) - { - weatherParticleSystem->setPos (gameCamera.getPos ()); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [weather particle updating i = %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis (), i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - Renderer & renderer = Renderer::getInstance (); - - chronoGamePerformanceCounts.start (); - - renderer.updateParticleManager (rsGame, avgRenderFps); - - addPerformanceCount ("ProcessParticleManager", - chronoGamePerformanceCounts.getMillis ()); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [particle manager updating i = %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis (), i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " - MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - while (commander.hasReplayCommandListForFrame () == true); - } - //else if(role == nrClient) { - else - { - if (pendingQuitError == false) - { - commander.signalNetworkUpdate (this); - } - - if (playingStaticVideo == true) - { - if (videoPlayer->isPlaying () == false) - { - playingStaticVideo = false; - tryPauseToggle (false); - } - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - chronoGamePerformanceCounts.start (); - - //call the chat manager - chatManager.updateNetwork (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld [chatManager.updateNetwork]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - updateNetworkMarkedCells (); - updateNetworkUnMarkedCells (); - updateNetworkHighligtedCells (); - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - //check for quiting status - if (NetworkManager::getInstance ().getGameNetworkInterface () != NULL - && NetworkManager::getInstance (). - getGameNetworkInterface ()->getQuit () - && mainMessageBox.getEnabled () == false - && errorMessageBox.getEnabled () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - quitTriggeredIndicator = true; - return; - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - addPerformanceCount ("ProcessMiscNetwork", - chronoGamePerformanceCounts.getMillis ()); - - // START - Handle joining in progress games - if (role == nrServer) - { - - if (this->networkPauseGameForLaggedClientsRequested == true) - { - this->networkPauseGameForLaggedClientsRequested = false; - - if (getPaused () == false) - { - - printf - ("[CMDR] Pausing game for lagging client(s), current world frame [%d]\n", - world.getFrameCount ()); - commander.tryPauseGame (false, false); - } - } - else if (this->networkResumeGameForLaggedClientsRequested == true) - { - this->networkResumeGameForLaggedClientsRequested = false; - - if (getPaused () == true) - { - - printf - ("[CMDR] Resuming game after Pause for lagging client(s), current world frame [%d]\n", - world.getFrameCount ()); - commander.tryResumeGame (false, false); - } - } - - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (); - if (server->getPauseForInGameConnection () == true) - { - - bool clientNeedsGameSetup = false; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getPauseForInGameConnection () == true) - { - clientNeedsGameSetup = true; - break; - } - } - - if (pausedForJoinGame == false || clientNeedsGameSetup == true) - { - //printf("================= Switching player pausing game\n"); - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - //printf("Switching player check %d from: %d connected: %d, startindex = %d, connected #2: %d\n",i,faction->getControlType(),server->isClientConnected(faction->getStartLocationIndex()),faction->getStartLocationIndex(),server->isClientConnected(i)); - //printf("Slot: %d faction name: %s\n",i,faction->getType()->getName().c_str()); - - if (faction->getControlType () != ctNetwork && - faction->getControlType () != ctHuman && - server->isClientConnected (faction->getStartLocationIndex - ()) == true) - { - - //printf("Switching player %d from: %d to %d\n",i,faction->getControlType(),ctNetwork); - //printf("Slot: %d faction name: %s GS faction: %s\n",i,faction->getType()->getName().c_str(),server->gameSettings.getFactionTypeName(i).c_str()); - - server->gameSettings.setFactionControl (i, ctNetwork); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), - CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - server->gameSettings.setNetworkPlayerName (i, - slot->getName - ()); - server->gameSettings.setNetworkPlayerUUID (i, - slot->getUUID - ()); - server->gameSettings.setNetworkPlayerPlatform (i, - slot->getPlatform - ()); - safeMutex.ReleaseLock (); - server->gameSettings.setNetworkPlayerStatuses (i, - npst_None); - - this->gameSettings.setFactionControl (i, ctNetwork); - this->gameSettings.setNetworkPlayerName (i, - server->gameSettings.getNetworkPlayerName - (i)); - this->gameSettings.setNetworkPlayerUUID (i, - server->gameSettings.getNetworkPlayerUUID - (i)); - this->gameSettings.setNetworkPlayerPlatform (i, - server->gameSettings.getNetworkPlayerPlatform - (i)); - this->gameSettings.setNetworkPlayerStatuses (i, npst_None); - } - } - //printf("#1 Data synch: lmap %u ltile: %d ltech: %u\n",gameSettings.getMapCRC(),gameSettings.getTilesetCRC(),gameSettings.getTechCRC()); - //printf("#2 Data synch: lmap %u ltile: %d ltech: %u\n",server->gameSettings.getMapCRC(),server->gameSettings.getTilesetCRC(),server->gameSettings.getTechCRC()); - server->broadcastGameSetup (&server->gameSettings, true); - } - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getPauseForInGameConnection () == true) - { - slot->setPauseForInGameConnection (false); - } - } - } - else if (server->getStartInGameConnectionLaunch () == true) - { - //printf("^^^ getStartInGameConnectionLaunch triggered!\n"); - - //server->setStartInGameConnectionLaunch(false); - - //this->speed = 1; - - //Lang &lang= Lang::getInstance(); - bool pauseAndSaveGameForNewClient = false; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - - if (slot != NULL - && slot->getStartInGameConnectionLaunch () == true) - { - //slot->setStartInGameConnectionLaunch(false); - pauseAndSaveGameForNewClient = true; - } - if (slot != NULL && slot->getJoinGameInProgress () == true) - { - //printf("$$$ signalling client to start game [deleting AI player] factionIndex: %d slot: %d startlocation: %d!\n",i,slot->getPlayerIndex(),faction->getStartLocationIndex()); - safeMutex.ReleaseLock (); - - this->gameSettings.setFactionControl (i, ctNetwork); - this->gameSettings.setNetworkPlayerName (i, - server->gameSettings.getNetworkPlayerName - (i)); - this->gameSettings.setNetworkPlayerUUID (i, - server->gameSettings.getNetworkPlayerUUID - (i)); - this->gameSettings.setNetworkPlayerPlatform (i, - server->gameSettings.getNetworkPlayerPlatform - (i)); - - if (this->gameSettings.getNetworkPlayerStatuses (i) == - npst_Disconnected) - { - this->gameSettings.setNetworkPlayerStatuses (i, npst_None); - } - - //printf("START Purging AI player for index: %d\n",i); - masterController.clearSlaves (true); - delete aiInterfaces[i]; - aiInterfaces[i] = NULL; - //printf("END Purging AI player for index: %d\n",i); - - Faction *faction = world.getFaction (i); - faction->setControlType (ctNetwork); - //pauseAndSaveGameForNewClient = true; - } - else if ((slot == NULL || slot->isConnected () == false) - && this->gameSettings.getFactionControl (i) == - ctNetwork && aiInterfaces[i] == NULL) - { - - safeMutex.ReleaseLock (); - faction->setFactionDisconnectHandled (false); - //this->gameSettings.setNetworkPlayerName(i,lang.getString("AI") + intToStr(i+1)); - //server->gameSettings.setNetworkPlayerName(i,lang.getString("AI") + intToStr(i+1)); - } - else - { - safeMutex.ReleaseLock (); - } - } - - if (pauseAndSaveGameForNewClient == true - && pausedForJoinGame == false && pauseRequestSent == false) - { - //printf("Pausing game for join in progress game...\n"); - - commander.tryPauseGame (true, true); - pauseRequestSent = true; - return; - } - } - //else if(server->getPauseForInGameConnection() == true && paused == true && - if (pausedForJoinGame == true) - { - if (pauseStateChanged == true) - { - pauseStateChanged = false; - } - - if (server->getUnPauseForInGameConnection () == true) - { - //printf("^^^ getUnPauseForInGameConnection triggered!\n"); - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), - CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getUnPauseForInGameConnection () == true) - { - slot->setUnPauseForInGameConnection (false); - faction->setFactionDisconnectHandled (false); - } - } - //printf("Resuming game for join in progress game resumeRequestSent: %d...\n",resumeRequestSent); - - if (pausedBeforeJoinGame == false && resumeRequestSent == false) - { - commander.tryResumeGame (true, true); - resumeRequestSent = true; - } - } - else if (server->getStartInGameConnectionLaunch () == true) - { - bool saveNetworkGame = false; - - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (); - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), - CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getJoinGameInProgress () == true - && slot->getStartInGameConnectionLaunch () == true - && slot->getSentSavedGameInfo () == false) - { - slot->setStartInGameConnectionLaunch (false); - - saveNetworkGame = true; - break; - } - } - - if (saveNetworkGame == true) - { - //printf("Saved network game to disk\n"); - - string - file = - this->saveGame (GameConstants::saveNetworkGameFileServer, - "temp/"); - - string saveGameFilePath = "temp/"; - string - saveGameFileCompressed = - saveGameFilePath + - string (GameConstants::saveNetworkGameFileServerCompressed); - if (getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) != "") - { - saveGameFilePath = - getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) + - saveGameFilePath; - saveGameFileCompressed = - saveGameFilePath + - string - (GameConstants::saveNetworkGameFileServerCompressed); - } - else - { - string - userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameFilePath = userData + saveGameFilePath; - saveGameFileCompressed = - saveGameFilePath + - string - (GameConstants::saveNetworkGameFileServerCompressed); - } - - bool - compressed_result = - compressFileToZIPFile (file, saveGameFileCompressed); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Saved game [%s] compressed to [%s] returned: %d\n", - file.c_str (), saveGameFileCompressed.c_str (), - compressed_result); - - char szBuf[8096] = ""; - Lang & lang = Lang::getInstance (); - snprintf (szBuf, 8096, - lang.getString ("GameSaved", "", - true).c_str (), file.c_str ()); - console.addLine (szBuf); - - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), - CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getJoinGameInProgress () == true - && slot->getSentSavedGameInfo () == false) - { - - safeMutex.ReleaseLock (); - NetworkMessageReady networkMessageReady (0); - slot->sendMessage (&networkMessageReady); - - slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL) - { - slot->setSentSavedGameInfo (true); - } - } - } - } - } - } - //else { - // handle setting changes from clients - Map *map = world.getMap (); - //printf("switchSetupRequests != NULL\n"); - - bool - switchRequested = - switchSetupForSlots (server, 0, map->getMaxPlayers (), false); - switchRequested = switchRequested - || switchSetupForSlots (server, map->getMaxPlayers (), - GameConstants::maxPlayers, true); - - if (switchRequested == true) - { - //printf("Send new game setup from switch: %d\n",switchRequested); - - //for(int i= 0; i < gameSettings.getFactionCount(); ++i) { - //printf("#1 Faction Index: %d control: %d startlocation: %d\n",i,gameSettings.getFactionControl(i),gameSettings.getStartLocationIndex(i)); - - //printf("#2 Faction Index: %d control: %d startlocation: %d\n",i,server->gameSettings.getFactionControl(i),server->gameSettings.getStartLocationIndex(i)); - //} - - server->broadcastGameSetup (&server->gameSettings, true); - } - //} - - // Make the server wait a bit for clients to start. - if (pausedForJoinGame == false && resumeRequestSent == true) - { - resumeRequestSent = false; - //sleep(500); - } - } - // END - Handle joining in progress games - - //update auto test - if (Config::getInstance ().getBool ("AutoTest")) - { - AutoTest::getInstance ().updateGame (this); - return; - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (world.getQueuedScenario () != "") - { - string name = world.getQueuedScenario (); - bool keepFactions = world.getQueuedScenarioKeepFactions (); - world.setQueuedScenario ("", false); - - //vector results; - const - vector < - string > & - dirList = Config::getInstance ().getPathListForType (ptScenarios); - string scenarioFile = Scenario::getScenarioPath (dirList, name); - - - try - { - gameStarted = false; - - //printf("\nname [%s] scenarioFile [%s] results.size() = " MG_SIZE_T_SPECIFIER "\n",name.c_str(),scenarioFile.c_str(),results.size()); - //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - bool isTutorial = Scenario::isGameTutorial (scenarioFile); - ScenarioInfo scenarioInfo; - Scenario::loadScenarioInfo (scenarioFile, &scenarioInfo, - isTutorial); - - //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - GameSettings gameSettings; - Scenario::loadGameSettings (dirList, &scenarioInfo, - &gameSettings, scenarioFile); - - //Program *program = world->getGame()->getProgram(); - //program->setState(new Game(program, &gameSettings, false)); - - //world->end(); - - //world->getMapPtr()->end(); - //world.end(); - - if (keepFactions == false) - { - world.end (); - - world.cleanup (); - world.clearTileset (); - - SoundRenderer::getInstance ().stopAllSounds (); - - masterController.clearSlaves (true); - deleteValues (aiInterfaces.begin (), aiInterfaces.end ()); - aiInterfaces.clear (); - gui.end (); //selection must be cleared before deleting units - world.end (); //must die before selection because of referencers - - BaseColorPickEntity::resetUniqueColors (); - // MUST DO THIS LAST!!!! Because objects above have pointers to things like - // unit particles and fade them out etc and this end method deletes the original - // object pointers. - Renderer & renderer = Renderer::getInstance (); - renderer.endGame (true); - - GameConstants::updateFps = original_updateFps; - GameConstants::cameraFps = original_cameraFps; - - this->setGameSettings (&gameSettings); - this->resetMembers (); - this->load (); - this->init (); - } - else - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - if (currentAmbientSound) - { - soundRenderer.stopAmbient (currentAmbientSound); - } - //soundRenderer.stopAllSounds(); - soundRenderer.stopAllSounds (fadeMusicMilliseconds); - - world.endScenario (); - BaseColorPickEntity::resetUniqueColors (); - - Renderer & renderer = Renderer::getInstance (); - renderer.endScenario (); - world.clearTileset (); - this->setGameSettings (&gameSettings); - this->load (lgt_FactionPreview | lgt_TileSet | lgt_Map | - lgt_Scenario); - try - { - world.init (this, gameSettings.getDefaultUnits (), false); - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - } - - world.initUnitsForScenario (); - Map *map = world.getMap (); - gameCamera.init (map->getW (), map->getH ()); - - // camera default height calculation - if (map->getCameraHeight () > 0 - && gameCamera.getCalculatedDefault () < - map->getCameraHeight ()) - { - gameCamera.setCalculatedDefault (map->getCameraHeight ()); - } - else if (gameCamera.getCalculatedDefault () < - map->getMaxMapHeight () + 13.0f) - { - gameCamera.setCalculatedDefault (map->getMaxMapHeight () + - 13.0f); - } - - scriptManager.init (&world, &gameCamera, loadGameNode); - renderer.initGame (this, this->getGameCameraPtr ()); - - //sounds - //soundRenderer.stopAllSounds(fadeMusicMilliseconds); - //soundRenderer.stopAllSounds(); - //soundRenderer= SoundRenderer::getInstance(); - - Tileset *tileset = world.getTileset (); - AmbientSounds *ambientSounds = tileset->getAmbientSounds (); - - //rain - if (tileset->getWeather () == wRainy - && ambientSounds->isEnabledRain ()) - { - //logger.add("Starting ambient stream", true); - currentAmbientSound = ambientSounds->getRain (); - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - soundRenderer.playAmbient (currentAmbientSound); - } - - //snow - if (tileset->getWeather () == wSnowy - && ambientSounds->isEnabledSnow ()) - { - //logger.add("Starting ambient stream", true); - currentAmbientSound = ambientSounds->getSnow (); - //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); - soundRenderer.playAmbient (currentAmbientSound); - } - - if (this->masterserverMode == false) - { - StrSound *gameMusic = - world.getThisFaction ()->getType ()->getMusic (); - soundRenderer.playMusic (gameMusic); - } - - gameStarted = true; - } - //this->init(); - - //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //Checksum checksum; - //world->loadScenario(scenarioFile, &checksum, true); - } +namespace Glest { + namespace Game { + + string GameSettings::playerDisconnectedText = ""; + Game *thisGamePtr = NULL; + + // ===================================================== + // class Game + // ===================================================== + + // ===================== PUBLIC ======================== + + const float PHOTO_MODE_MAXHEIGHT = 500.0; + + const int CREATE_NEW_TEAM = -100; + const int CANCEL_SWITCH_TEAM = -1; + + const int CANCEL_DISCONNECT_PLAYER = -1; + + const float Game::highlightTime = 0.5f; + + int fadeMusicMilliseconds = 3500; + + // Check every x seconds if we should switch disconnected players to AI + const int NETWORK_PLAYER_CONNECTION_CHECK_SECONDS = 5; + + int GAME_STATS_DUMP_INTERVAL = 60 * 10; + + Game::Game() : + ProgramState(NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + originalDisplayMsgCallback = NULL; + aiInterfaces.clear(); + videoPlayer = NULL; + playingStaticVideo = false; + + mouse2d = 0; + mouseX = 0; + mouseY = 0; + updateFps = 0; + lastUpdateFps = 0; + avgUpdateFps = 0; + framesToCatchUpAsClient = 0; + framesToSlowDownAsClient = 0; + totalRenderFps = 0; + renderFps = 0; + lastRenderFps = 0; + avgRenderFps = 0; + currentAvgRenderFpsTotal = 0; + paused = false; + networkPauseGameForLaggedClientsRequested = false; + networkResumeGameForLaggedClientsRequested = false; + pausedForJoinGame = false; + pausedBeforeJoinGame = false; + pauseRequestSent = false; + resumeRequestSent = false; + pauseStateChanged = false; + gameOver = false; + renderNetworkStatus = false; + renderInGamePerformance = false; + showFullConsole = false; + setMarker = false; + cameraDragAllowed = false; + mouseMoved = false; + scrollSpeed = 0; + camLeftButtonDown = false; + camRightButtonDown = false; + camUpButtonDown = false; + camDownButtonDown = false; + this->speed = 1; + weatherParticleSystem = NULL; + isFirstRender = false; + quitTriggeredIndicator = false; + quitPendingIndicator = false; + original_updateFps = 0; + original_cameraFps = 0; + captureAvgTestStatus = false; + updateFpsAvgTest = 0; + renderFpsAvgTest = 0; + renderExtraTeamColor = 0; + photoModeEnabled = false; + healthbarMode = hbvUndefined; + visibleHUD = false; + timeDisplay = false; + withRainEffect = false; + program = NULL; + gameStarted = false; + this->initialResumeSpeedLoops = false; + + highlightCellTexture = NULL; + lastMasterServerGameStatsDump = 0; + lastMaxUnitCalcTime = 0; + lastRenderLog2d = 0; + playerIndexDisconnect = 0; + tickCount = 0; + currentCameraFollowUnit = NULL; + + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + popupMenuSwitchTeams.setEnabled(false); + popupMenuSwitchTeams.setVisible(false); + + popupMenuDisconnectPlayer.setEnabled(false); + popupMenuDisconnectPlayer.setVisible(false); + + switchTeamConfirmMessageBox.setEnabled(false); + disconnectPlayerConfirmMessageBox.setEnabled(false); + + disconnectPlayerIndexMap.clear(); + + exitGamePopupMenuIndex = -1; + joinTeamPopupMenuIndex = -1; + pauseGamePopupMenuIndex = -1; + saveGamePopupMenuIndex = -1; + loadGamePopupMenuIndex = -1; + keyboardSetupPopupMenuIndex = -1; + disconnectPlayerPopupMenuIndex = -1; + + isMarkCellEnabled = false; + isMarkCellTextEnabled = false; + + markCellTexture = NULL; + isUnMarkCellEnabled = false; + unmarkCellTexture = NULL; + + masterserverMode = false; + currentUIState = NULL; + currentAmbientSound = NULL; + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + + loadGameNode = NULL; + lastworldFrameCountForReplay = -1; + lastNetworkPlayerConnectionCheck = time(NULL); + inJoinGameLoading = false; + quitGameCalled = false; + disableSpeedChange = false; + + for (int i = 0; i < GameConstants::networkSmoothInterval; i++) { + receivedTooEarlyInFrames[i] = -1; + framesNeededToWaitForServerMessage[i] = -1; + } + + fadeMusicMilliseconds = + Config::getInstance().getInt("GameStartStopFadeSoundMilliseconds", + intToStr(fadeMusicMilliseconds).c_str + ()); + GAME_STATS_DUMP_INTERVAL = + Config::getInstance().getInt("GameStatsDumpIntervalSeconds", + intToStr + (GAME_STATS_DUMP_INTERVAL).c_str()); + } + + void Game::resetMembers() { + Unit::setGame(this); + gameStarted = false; + this->initialResumeSpeedLoops = false; + + original_updateFps = GameConstants::updateFps; + original_cameraFps = GameConstants::cameraFps; + GameConstants::updateFps = 40; + GameConstants::cameraFps = 100; + captureAvgTestStatus = false; + updateFpsAvgTest = 0; + renderFpsAvgTest = 0; + lastRenderLog2d = 0; + playerIndexDisconnect = 0; + lastMasterServerGameStatsDump = 0; + highlightCellTexture = NULL; + totalRenderFps = 0; + lastMaxUnitCalcTime = 0; + renderExtraTeamColor = 0; + + mouseMoved = false; + quitTriggeredIndicator = false; + quitPendingIndicator = false; + originalDisplayMsgCallback = NULL; + thisGamePtr = this; + + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + popupMenuSwitchTeams.setEnabled(false); + popupMenuSwitchTeams.setVisible(false); + + popupMenuDisconnectPlayer.setEnabled(false); + popupMenuDisconnectPlayer.setVisible(false); + + switchTeamConfirmMessageBox.setEnabled(false); + disconnectPlayerConfirmMessageBox.setEnabled(false); + + disconnectPlayerIndexMap.clear(); + + exitGamePopupMenuIndex = -1; + joinTeamPopupMenuIndex = -1; + pauseGamePopupMenuIndex = -1; + saveGamePopupMenuIndex = -1; + loadGamePopupMenuIndex = -1; + keyboardSetupPopupMenuIndex = -1; + disconnectPlayerPopupMenuIndex = -1; + + isMarkCellEnabled = false; + isMarkCellTextEnabled = false; + + markCellTexture = NULL; + isUnMarkCellEnabled = false; + unmarkCellTexture = NULL; + + currentUIState = NULL; + + scrollSpeed = Config::getInstance().getFloat("UiScrollSpeed", "1.5"); + photoModeEnabled = + Config::getInstance().getBool("PhotoMode", "false"); + healthbarMode = Config::getInstance().getInt("HealthBarMode", "4"); + visibleHUD = Config::getInstance().getBool("VisibleHud", "true"); + timeDisplay = Config::getInstance().getBool("TimeDisplay", "true"); + withRainEffect = Config::getInstance().getBool("RainEffect", "true"); + //MIN_RENDER_FPS_ALLOWED = Config::getInstance().getInt("MIN_RENDER_FPS_ALLOWED",intToStr(MIN_RENDER_FPS_ALLOWED).c_str()); + + mouseX = 0; + mouseY = 0; + mouse2d = 0; + loadingText = ""; + weatherParticleSystem = NULL; + updateFps = 0; + renderFps = 0; + lastUpdateFps = 0; + framesToCatchUpAsClient = 0; + framesToSlowDownAsClient = 0; + lastRenderFps = -1; + avgUpdateFps = -1; + avgRenderFps = -1; + currentAvgRenderFpsTotal = 0; + tickCount = 0; + paused = false; + networkPauseGameForLaggedClientsRequested = false; + networkResumeGameForLaggedClientsRequested = false; + pausedForJoinGame = false; + pausedBeforeJoinGame = false; + resumeRequestSent = false; + pauseRequestSent = false; + pauseStateChanged = false; + gameOver = false; + renderNetworkStatus = false; + renderInGamePerformance = false; + this->speed = 1; + showFullConsole = false; + setMarker = false; + cameraDragAllowed = false; + camLeftButtonDown = false; + camRightButtonDown = false; + camUpButtonDown = false; + camDownButtonDown = false; + + currentCameraFollowUnit = NULL; + currentAmbientSound = NULL; + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + + loadGameNode = NULL; + lastworldFrameCountForReplay = -1; + + lastNetworkPlayerConnectionCheck = time(NULL); + + inJoinGameLoading = false; + quitGameCalled = false; + disableSpeedChange = false; + + for (int i = 0; i < GameConstants::networkSmoothInterval; i++) { + receivedTooEarlyInFrames[i] = -1; + framesNeededToWaitForServerMessage[i] = -1; + } + + + fadeMusicMilliseconds = + Config::getInstance().getInt("GameStartStopFadeSoundMilliseconds", + intToStr(fadeMusicMilliseconds).c_str + ()); + GAME_STATS_DUMP_INTERVAL = + Config::getInstance().getInt("GameStatsDumpIntervalSeconds", + intToStr + (GAME_STATS_DUMP_INTERVAL).c_str()); + + Logger & logger = Logger::getInstance(); + logger.showProgress(); + } + + Game::Game(Program * program, const GameSettings * gameSettings, + bool masterserverMode) :ProgramState(program), + lastMousePos(0), isFirstRender(true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->masterserverMode = masterserverMode; + videoPlayer = NULL; + playingStaticVideo = false; + highlightCellTexture = NULL; + playerIndexDisconnect = 0; + updateFpsAvgTest = 0; + renderFpsAvgTest = 0; + cameraDragAllowed = false; + + if (this->masterserverMode == true) { + printf("Starting a new game...\n"); + } + + this->program = program; + resetMembers(); + this->gameSettings = *gameSettings; + + Lang::getInstance().setAllowNativeLanguageTechtree(this-> + gameSettings.getNetworkAllowNativeLanguageTechtree + ()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void Game::endGame() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + quitGame(); + sleep(0); + + Object::setStateCallback(NULL); + thisGamePtr = NULL; + if (originalDisplayMsgCallback != NULL) { + NetworkInterface::setDisplayMessageFunction + (originalDisplayMsgCallback); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Logger & logger = Logger::getInstance(); + Renderer & renderer = Renderer::getInstance(); + + logger.clearHints(); + logger.loadLoadingScreen(""); + logger.setState(Lang::getInstance().getString("Deleting")); + //logger.add("Game", true); + logger. + add(Lang:: + getInstance().getString("LogScreenGameLoading", "", true), + false); + logger.hideProgress(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Cannot Fade because sound files will be deleted below + SoundRenderer::getInstance().stopAllSounds(fadeMusicMilliseconds); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // deleteValues(aiInterfaces.begin(), aiInterfaces.end()); + // aiInterfaces.clear(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + gui.end(); //selection must be cleared before deleting units + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // world.end(); //must die before selection because of referencers + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] aiInterfaces.size() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + aiInterfaces.size()); + + // MUST DO THIS LAST!!!! Because objects above have pointers to things like + // unit particles and fade them out etc and this end method deletes the original + // object pointers. + renderer.endGame(false); + + GameConstants::updateFps = original_updateFps; + GameConstants::cameraFps = original_cameraFps; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Unit::setGame(NULL); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ==== END GAME ==== getCurrentPixelByteCount() = " + MG_SIZE_T_SPECIFIER "\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderer.getCurrentPixelByteCount()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "==== END GAME ====\n"); + + FileCRCPreCacheThread *&preCacheCRCThreadPtr = + CacheManager::getCachedItem < + FileCRCPreCacheThread * + >(GameConstants::preCacheThreadCacheLookupKey); + if (preCacheCRCThreadPtr != NULL) { + preCacheCRCThreadPtr->setPauseForGame(false); + } + + //this->program->reInitGl(); + //renderer.reinitAll(); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + + Game::~Game() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + quitGame(); + + Object::setStateCallback(NULL); + thisGamePtr = NULL; + if (originalDisplayMsgCallback != NULL) { + NetworkInterface::setDisplayMessageFunction + (originalDisplayMsgCallback); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Logger & logger = Logger::getInstance(); + Renderer & renderer = Renderer::getInstance(); + + logger.loadLoadingScreen(""); + logger.setState(Lang::getInstance().getString("Deleting")); + //logger.add("Game", true); + logger. + add(Lang:: + getInstance().getString("LogScreenGameLoading", "", true), + false); + logger.hideProgress(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Cannot Fade because sound files will be deleted below + SoundRenderer::getInstance().stopAllSounds(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + masterController.clearSlaves(true); + deleteValues(aiInterfaces.begin(), aiInterfaces.end()); + aiInterfaces.clear(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + gui.end(); //selection must be cleared before deleting units + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + world.end(); //must die before selection because of referencers + + BaseColorPickEntity::resetUniqueColors(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] aiInterfaces.size() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + aiInterfaces.size()); + + delete videoPlayer; + videoPlayer = NULL; + playingStaticVideo = false; + + // MUST DO THIS LAST!!!! Because objects above have pointers to things like + // unit particles and fade them out etc and this end method deletes the original + // object pointers. + renderer.endGame(true); + BaseColorPickEntity::cleanupPBO(); + + GameConstants::updateFps = original_updateFps; + GameConstants::cameraFps = original_cameraFps; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Unit::setGame(NULL); + + Lang::getInstance().setAllowNativeLanguageTechtree(true); + + FileCRCPreCacheThread *&preCacheCRCThreadPtr = + CacheManager::getCachedItem < + FileCRCPreCacheThread * + >(GameConstants::preCacheThreadCacheLookupKey); + if (preCacheCRCThreadPtr != NULL) { + preCacheCRCThreadPtr->setPauseForGame(false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ==== END GAME ==== getCurrentPixelByteCount() = " + MG_SIZE_T_SPECIFIER "\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderer.getCurrentPixelByteCount()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "==== END GAME ====\n"); + + //this->program->reInitGl(); + //renderer.reinitAll(); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + + bool Game::showTranslatedTechTree()const { + return this->gameSettings.getNetworkAllowNativeLanguageTechtree(); + } + + bool Game::quitTriggered() { + return quitTriggeredIndicator; + } + + Stats Game::quitAndToggleState() { + //quitGame(); + //Program *program = game->getProgram(); + return quitGame(); + //Game::exitGameState(program, endStats); + } + + // ==================== init and load ==================== + + int Game::ErrorDisplayMessage(const char *msg, bool exitApp) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + msg); + + if (thisGamePtr != NULL) { + string text = msg; + thisGamePtr->showErrorMessageBox(text, "Error detected", false); + } + + return 0; + } + + //Texture2D * Game::findFactionLogoTexture(const GameSettings *settings, Logger *logger,string factionLogoFilter, bool useTechDefaultIfFilterNotFound) { + // Texture2D *result = NULL; + // string logoFilename = Game::findFactionLogoFile(settings, logger,factionLogoFilter); + // if(logoFilename == "" && factionLogoFilter != "" && useTechDefaultIfFilterNotFound == true) { + // logoFilename = Game::findFactionLogoFile(settings, logger); + // } + // + // result = Renderer::findTexture(logoFilename); + // + // return result; + //} + + string + Game::extractScenarioLogoFile(const GameSettings * settings, + string & result, bool & loadingImageUsed, + Logger * logger, + string factionLogoFilter) { + string scenarioDir = ""; + if (settings->getScenarioDir() != "") { + scenarioDir = settings->getScenarioDir(); + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); + scenarioDir = + scenarioDir.erase(scenarioDir.size() - + settings->getScenario().size(), + settings->getScenario().size() + 1); + } + + //printf("!!! extractScenarioLogoFile scenarioDir [%s] factionLogoFilter [%s]\n",scenarioDir.c_str(),factionLogoFilter.c_str()); + + vector < string > loadScreenList; + string logoFullPathFilter = scenarioDir + factionLogoFilter; + findAll(logoFullPathFilter, loadScreenList, false, false); + if (loadScreenList.empty() == false) { + int bestLogoIndex = 0; + + if (loadScreenList.size() > 1 + && EndsWith(factionLogoFilter, ".xml") == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\nLooking for best logo from a list of: " + MG_SIZE_T_SPECIFIER " using filter: [%s]\n", + loadScreenList.size(), logoFullPathFilter.c_str()); + + int bestMinWidthDiff = INT_MAX; + int bestMinHeightDiff = INT_MAX; + // Now find the best texture for our screen + // Texture2D *result = preloadTexture(logoFilename); + for (unsigned int logoIndex = 0; + logoIndex < loadScreenList.size(); ++logoIndex) { + string + senarioLogo = scenarioDir + loadScreenList[bestLogoIndex]; + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, senarioLogo.c_str()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Looking for best logo: %u [%s]\n", logoIndex, + senarioLogo.c_str()); + + if (fileExists(senarioLogo) == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, senarioLogo.c_str()); + + Texture2D *checkLogo = Renderer::preloadTexture(senarioLogo); + if (checkLogo != NULL) { + const Metrics & metrics = Metrics::getInstance(); + int + minWidthDifference = + abs(metrics.getScreenW() - + checkLogo->getPixmapConst()->getW()); + int + minHeightDifference = + abs(metrics.getScreenH() - + checkLogo->getPixmapConst()->getH()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Logo info: %d x %d (%d,%d)\n", + checkLogo->getPixmapConst()->getW(), + checkLogo->getPixmapConst()->getH(), + minWidthDifference, minHeightDifference); + + if (minWidthDifference < bestMinWidthDiff) { + bestMinWidthDiff = minWidthDifference; + + bestLogoIndex = logoIndex; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 New best logo is [%s]\n", + senarioLogo.c_str()); + } else if (minWidthDifference == bestMinWidthDiff + && minHeightDifference < bestMinHeightDiff) { + bestMinHeightDiff = minHeightDifference; + + bestLogoIndex = logoIndex; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 New best logo is [%s]\n", + senarioLogo.c_str()); + } + } + } + } + } + + string senarioLogo = scenarioDir + loadScreenList[bestLogoIndex]; + if (fileExists(senarioLogo) == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] found scenario loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + senarioLogo.c_str()); + + result = senarioLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + } + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + settings->getScenarioDir().c_str(), + settings->getScenario().c_str(), + scenarioDir.c_str()); + } + return scenarioDir; + } + + string + Game::extractFactionLogoFile(bool & loadingImageUsed, + const string & factionName, + string scenarioDir, + const string & techName, Logger * logger, + string factionLogoFilter) { + string result = ""; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Searching for faction loading screen\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (factionName == formatString(GameConstants::OBSERVER_SLOTNAME)) { + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + const string + factionLogo = data_path + "data/core/misc_textures/observer.jpg"; + //printf("In [%s::%s Line: %d] looking for loading screen '%s'\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,factionLogo.c_str()); + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + factionLogo.c_str()); + + result = factionLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + } + } + //else if(settings->getFactionTypeName(i) == formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + else if (factionName == + formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + const string + factionLogo = data_path + "data/core/misc_textures/random.jpg"; + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + factionLogo.c_str()); + + result = factionLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + } + } else { + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptTechs, scenarioDir); + for (int idx = 0; idx < (int) pathList.size(); idx++) { + string currentPath = pathList[idx]; + endPathWithSlash(currentPath); + //string path = currentPath + techName + "/" + "factions" + "/" + settings->getFactionTypeName(i); + string + path = + currentPath + techName + "/" + "factions" + "/" + factionName; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] possible loading screen dir '%s'\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + path.c_str()); + if (isdir(path.c_str()) == true) { + endPathWithSlash(path); + + vector < string > loadScreenList; + string logoFullPathFilter = path + factionLogoFilter; + findAll(logoFullPathFilter, loadScreenList, false, false); + if (loadScreenList.empty() == false) { + int bestLogoIndex = 0; + + if (loadScreenList.size() > 1 + && EndsWith(factionLogoFilter, ".xml") == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\nLooking for best logo from a list of: " + MG_SIZE_T_SPECIFIER + " using filter: [%s]\n", + loadScreenList.size(), + logoFullPathFilter.c_str()); + + + int bestMinWidthDiff = INT_MAX; + int bestMinHeightDiff = INT_MAX; + // Now find the best texture for our screen + // Texture2D *result = preloadTexture(logoFilename); + for (unsigned int logoIndex = 0; + logoIndex < + (unsigned int) loadScreenList.size(); ++logoIndex) { + string factionLogo = path + loadScreenList[logoIndex]; + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + factionLogo.c_str()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Looking for best logo: %u [%s]\n", + logoIndex, factionLogo.c_str()); + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + factionLogo.c_str()); + + Texture2D *checkLogo = + Renderer::preloadTexture(factionLogo); + if (checkLogo != NULL) { + const Metrics & metrics = Metrics::getInstance(); + int + minWidthDifference = + abs(metrics.getScreenW() - + checkLogo->getPixmapConst()->getW()); + int + minHeightDifference = + abs(metrics.getScreenH() - + checkLogo->getPixmapConst()->getH()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Logo info: %d x %d (%d,%d)\n", + checkLogo->getPixmapConst()->getW(), + checkLogo->getPixmapConst()->getH(), + minWidthDifference, minHeightDifference); + + if (minWidthDifference < bestMinWidthDiff) { + bestMinWidthDiff = minWidthDifference; + + bestLogoIndex = logoIndex; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("#1 New best logo is [%s]\n", + factionLogo.c_str()); + } else if (minWidthDifference == + bestMinWidthDiff + && minHeightDifference < bestMinHeightDiff) { + bestMinHeightDiff = minHeightDifference; + + bestLogoIndex = logoIndex; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("#2 New best logo is [%s]\n", + factionLogo.c_str()); + } + } + } + } + } + + string factionLogo = path + loadScreenList[bestLogoIndex]; + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, factionLogo.c_str()); + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, factionLogo.c_str()); + + result = factionLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + break; + } + } + // Check if this is a linked faction + else { + //!!! + string factionXMLFile = path + factionName + ".xml"; + + //printf("A factionXMLFile [%s]\n",factionXMLFile.c_str()); + + if (fileExists(factionXMLFile) == true) { + XmlTree xmlTreeFaction(XML_RAPIDXML_ENGINE); + std::map < string, string > mapExtraTagReplacementValues; + xmlTreeFaction.load(factionXMLFile, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues), + true, true); + + const XmlNode *rootNode = xmlTreeFaction.getRootNode(); + + //printf("B factionXMLFile [%s] root name [%s] root first child name [%s]\n",factionXMLFile.c_str(),rootNode->getName().c_str(),rootNode->getChild(0)->getName().c_str()); + //printf("B factionXMLFile [%s] root name [%s]\n",factionXMLFile.c_str(),rootNode->getName().c_str()); + if (rootNode->hasChild("link") == true) { + rootNode = rootNode->getChild("link"); + } + if (rootNode->getName() == "link" + && rootNode->hasChild("techtree") == true) { + const XmlNode *linkNode = rootNode; + + //printf("C factionXMLFile [%s]\n",factionXMLFile.c_str()); + + //if(linkNode->hasChild("techtree") == true) { + const XmlNode *techtreeNode = + linkNode->getChild("techtree"); + + string + linkedTechTreeName = + techtreeNode->getAttribute("name")->getValue(); + + //printf("D factionXMLFile [%s] linkedTechTreeName [%s]\n",factionXMLFile.c_str(),linkedTechTreeName.c_str()); + + if (linkedTechTreeName != "") { + + string + linkedTechTreePath = + TechTree::findPath(linkedTechTreeName, pathList); + string techTreePath = linkedTechTreePath; + endPathWithSlash(techTreePath); + + string + linkedCurrentPath = + techTreePath + "factions/" + factionName; + endPathWithSlash(linkedCurrentPath); + //string linkedTmppath= linkedCurrentPath + factionName +".xml"; + + path = linkedCurrentPath; + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] possible loading screen dir '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + path.c_str()); + + //printf("D1 idx = %d\ncurrentPath [%s]\npath [%s]\npathList[idx] [%s]\n",idx,currentPath.c_str(),path.c_str(),pathList[idx].c_str()); + + if (isdir(path.c_str()) == true) { + endPathWithSlash(path); + + //printf("E path [%s]\n",path.c_str()); + + loadScreenList.clear(); + findAll(path + factionLogoFilter, + loadScreenList, false, false); + if (loadScreenList.empty() == false) { + string factionLogo = path + loadScreenList[0]; + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__, + factionLogo.c_str()); + + //printf("F factionLogo [%s]\n",factionLogo.c_str()); + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug + (SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath(__FILE__).c_str + (), __FUNCTION__, __LINE__, + factionLogo.c_str()); + + result = factionLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + break; + } + } + } + } + //} + } + } + } + } + + if (loadingImageUsed == true) { + break; + } + } + } + //break; + //} + //} + return result; + } + + string + Game::extractTechLogoFile(string scenarioDir, const string & techName, + bool & loadingImageUsed, Logger * logger, + const string & factionLogoFilter) { + string result = ""; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Searching for tech loading screen\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptTechs, scenarioDir); + for (int idx = 0; idx < (int) pathList.size(); idx++) { + string currentPath = pathList[idx]; + endPathWithSlash(currentPath); + string path = currentPath + techName; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] possible loading screen dir '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, path.c_str()); + if (isdir(path.c_str()) == true) { + endPathWithSlash(path); + + vector < string > loadScreenList; + findAll(path + factionLogoFilter, loadScreenList, false, false); + if (loadScreenList.empty() == false) { + string factionLogo = path + loadScreenList[0]; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, factionLogo.c_str()); + + if (fileExists(factionLogo) == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found loading screen '%s'\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, factionLogo.c_str()); + + result = factionLogo; + if (logger != NULL) { + logger->loadLoadingScreen(result); + } + loadingImageUsed = true; + break; + } + } + } + if (loadingImageUsed == true) { + break; + } + } + + return result; + } + + void Game::loadHudTexture(const GameSettings * settings) { + string factionName = ""; + string techName = settings->getTech(); + string + scenarioDir = + extractDirectoryPathFromFile(settings->getScenarioDir()); + //printf("In loadHudTexture, scenarioDir [%s]\n",scenarioDir.c_str()); + + for (int i = 0; i < settings->getFactionCount(); ++i) { + if ((settings->getFactionControl(i) == ctHuman) + || (settings->getFactionControl(i) == ctNetwork + && settings->getThisFactionIndex() == i)) { + factionName = settings->getFactionTypeName(i); + break; + } + } + if (factionName != "") { + bool hudFound = false; + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptTechs, scenarioDir); + for (int idx = 0; hudFound == false && idx < (int) pathList.size(); + idx++) { + string currentPath = pathList[idx]; + endPathWithSlash(currentPath); + + vector < string > hudList; + string + path = + currentPath + techName + "/" + "factions" + "/" + factionName; + endPathWithSlash(path); + findAll(path + GameConstants::HUD_SCREEN_FILE_FILTER, hudList, + false, false); + if (hudList.empty() == false) { + for (unsigned int hudIdx = 0; + hudFound == false + && hudIdx < (unsigned int) hudList.size(); ++hudIdx) { + string hudImageFileName = path + hudList[hudIdx]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] looking for a HUD [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, hudImageFileName.c_str()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] looking for a HUD [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + hudImageFileName.c_str()); + + if (fileExists(hudImageFileName) == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] found HUD image [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, hudImageFileName.c_str()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] found HUD image [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + hudImageFileName.c_str()); + + Texture2D *texture = Renderer::findTexture(hudImageFileName); + gui.setHudTexture(texture); + hudFound = true; + //printf("Hud texture found! \n"); + break; + } + } + } + } + } + } + + string + Game::findFactionLogoFile(const GameSettings * settings, + Logger * logger, + const string & factionLogoFilter) { + string result = ""; + if (settings == NULL) { + result = ""; + } else { + string mapName = settings->getMap(); + string tilesetName = settings->getTileset(); + string techName = settings->getTech(); + string scenarioName = settings->getScenario(); + bool loadingImageUsed = false; + + if (logger != NULL) { + logger->setState(Lang::getInstance().getString("Loading")); + + if (scenarioName.empty()) { + string + scenarioDir = + extractDirectoryPathFromFile(settings->getScenarioDir()); + TechTree + techTree(Config:: + getInstance().getPathListForType(ptTechs, + scenarioDir)); + + logger->setSubtitle(formatString(mapName) + " - " + + formatString(tilesetName) + " - " + + formatString(techTree.getTranslatedName + (techName))); + } else { + logger->setSubtitle(formatString(scenarioName)); + } + } + + //string scenarioDir = ""; + //bool skipCustomLoadScreen = false; + //if(skipCustomLoadScreen == false) { + string + scenarioDir = + extractScenarioLogoFile(settings, result, loadingImageUsed, + logger, factionLogoFilter); + //} + // try to use a faction related loading screen + //if(skipCustomLoadScreen == false && loadingImageUsed == false) { + if (loadingImageUsed == false) { + for (int i = 0; i < settings->getFactionCount(); ++i) { + if (settings->getFactionControl(i) == ctHuman || + (settings->getFactionControl(i) == ctNetwork + && settings->getThisFactionIndex() == i)) { + + result = + extractFactionLogoFile(loadingImageUsed, + settings->getFactionTypeName(i), + scenarioDir, techName, logger, + factionLogoFilter); + break; + } + } + } + + // try to use a tech related loading screen + //if(skipCustomLoadScreen == false && loadingImageUsed == false){ + if (loadingImageUsed == false) { + result = extractTechLogoFile(scenarioDir, techName, + loadingImageUsed, logger, + factionLogoFilter); + } + } + return result; + } + + vector < Texture2D * >Game::processTech(string techName) { + vector < Texture2D * >logoFiles; + bool + enableFactionTexturePreview = + Config::getInstance().getBool("FactionPreview", "true"); + if (enableFactionTexturePreview) { + //string currentTechName_factionPreview = techName; + + vector < string > factions; + vector < string > techPaths = + Config::getInstance().getPathListForType(ptTechs); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + findAll(techPath + techName + "/factions/*.", factions, false, + false); + + if (factions.empty() == false) { + for (unsigned int factionIdx = 0; + factionIdx < (unsigned int) factions.size(); ++factionIdx) { + bool loadingImageUsed = false; + string currentFactionName_factionPreview = factions[factionIdx]; + + string + factionLogo = + Game::extractFactionLogoFile(loadingImageUsed, + currentFactionName_factionPreview, + "", + techName, + NULL, + GameConstants::PREVIEW_SCREEN_FILE_FILTER); + + if (factionLogo == "") { + factionLogo = + Game::extractFactionLogoFile(loadingImageUsed, + currentFactionName_factionPreview, + "", techName, NULL, + GameConstants::LOADING_SCREEN_FILE_FILTER); + } + if (factionLogo != "") { + Texture2D *texture = Renderer::preloadTexture(factionLogo); + logoFiles.push_back(texture); + } + } + } + } + } + + return logoFiles; + } + + void Game::load() { + load(lgt_All); + } + + void Game::load(int loadTypes) { + bool + showPerfStats = + Config::getInstance().getBool("ShowPerfStats", "false"); + Chrono chronoPerf; + if (showPerfStats) + chronoPerf.start(); + char perfBuf[8096] = ""; + std::vector < string > perfList; + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + FileCRCPreCacheThread *&preCacheCRCThreadPtr = + CacheManager::getCachedItem < + FileCRCPreCacheThread * + >(GameConstants::preCacheThreadCacheLookupKey); + if (preCacheCRCThreadPtr != NULL) { + preCacheCRCThreadPtr->setPauseForGame(true); + } + + std::map < string, vector < pair < string, string > > >loadedFileList; + originalDisplayMsgCallback = + NetworkInterface::getDisplayMessageFunction(); + NetworkInterface::setDisplayMessageFunction(ErrorDisplayMessage); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] loadTypes = %d, gameSettings = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + loadTypes, + this->gameSettings.toString().c_str()); + + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); + + BaseColorPickEntity::resetUniqueColors(); + + Config & config = Config::getInstance(); + Logger & logger = Logger::getInstance(); + + string mapName = gameSettings.getMap(); + string tilesetName = gameSettings.getTileset(); + string techName = gameSettings.getTech(); + string scenarioName = gameSettings.getScenario(); + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + // loadHints + + if (data_path != "") { + endPathWithSlash(data_path); + } + + string user_data_path = config.getString("UserData_Root", ""); + if (user_data_path != "") { + endPathWithSlash(user_data_path); + } + + string + englishFile = + getGameCustomCoreDataPath(data_path, + "data/lang/hint/hint_" + + Lang:: + getInstance().getDefaultLanguage() + + ".lng"); + string languageFile = + getGameCustomCoreDataPath(data_path, + "data/lang/hint/hint_" + + Lang::getInstance().getLanguage() + + ".lng"); + string languageFileUserData = + user_data_path + "data/lang/hint/hint_" + + Lang::getInstance().getLanguage() + ".lng"; + + if (fileExists(languageFileUserData) == true) { + languageFile = languageFileUserData; + } + if (fileExists(languageFile) == false) { + // if there is no language specific file use english instead + languageFile = englishFile; + } + if (fileExists(englishFile) == false) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] file [%s] not found\n", + __FILE__, __FUNCTION__, __LINE__, + englishFile.c_str()); + } else { + logger.loadGameHints(englishFile, languageFile, true); + + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + } + + if ((loadTypes & lgt_FactionPreview) == lgt_FactionPreview) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + Game::findFactionLogoFile(&gameSettings, &logger); + + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + loadHudTexture(&gameSettings); + + const string + markCellTextureFilename = + data_path + "data/core/misc_textures/mark_cell.png"; + markCellTexture = Renderer::findTexture(markCellTextureFilename); + const string + unmarkCellTextureFilename = + data_path + "data/core/misc_textures/unmark_cell.png"; + unmarkCellTexture = Renderer::findTexture(unmarkCellTextureFilename); + const string + highlightCellTextureFilename = + data_path + "data/core/misc_textures/pointer.png"; + highlightCellTexture = + Renderer::findTexture(highlightCellTextureFilename); + + string scenarioDir = ""; + if (gameSettings.getScenarioDir() != "") { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + scenarioDir = gameSettings.getScenarioDir(); + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); + scenarioDir = + scenarioDir.erase(scenarioDir.size() - + gameSettings.getScenario().size(), + gameSettings.getScenario().size() + 1); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //tileset + if ((loadTypes & lgt_TileSet) == lgt_TileSet) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + world. + loadTileset(config.getPathListForType(ptTilesets, scenarioDir), + tilesetName, &checksum, loadedFileList); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + set < string > factions; + for (int i = 0; i < gameSettings.getFactionCount(); ++i) { + factions.insert(gameSettings.getFactionTypeName(i)); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if ((loadTypes & lgt_TechTree) == lgt_TechTree) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //tech, load before map because of resources + world.loadTech(config.getPathListForType(ptTechs, scenarioDir), + techName, factions, &checksum, loadedFileList); + + if (world.getTechTree() == NULL + || world.getTechTree()->getNameUntranslated() == "") { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref: %d, ERROR: Cannot find techtree: [%s]", + __LINE__, techName.c_str()); + + throw megaglest_runtime_error(szBuf, true); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //map + if ((loadTypes & lgt_Map) == lgt_Map) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + world.loadMap(Config::getMapPath(mapName, scenarioDir), &checksum); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //scenario + if ((loadTypes & lgt_Scenario) == lgt_Scenario) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + if (scenarioName.empty() == false) { + + bool + isTutorial = + Scenario::isGameTutorial(gameSettings.getScenarioDir()); + //printf("Loading scenario gameSettings.getScenarioDir() [%s] scenarioName [%s] isTutorial: %d\n",gameSettings.getScenarioDir().c_str(),scenarioName.c_str(),isTutorial); + + Lang::getInstance(). + loadScenarioStrings(gameSettings.getScenarioDir(), scenarioName, + isTutorial); + + //printf("In [%s::%s Line: %d] rootNode [%p][%s]\n",__FILE__,__FUNCTION__,__LINE__,loadGameNode,(loadGameNode != NULL ? loadGameNode->getName().c_str() : "none")); + world.loadScenario(gameSettings.getScenarioDir(), &checksum, + false, loadGameNode); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + SDL_PumpEvents(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if (showPerfStats && chronoPerf.getMillis() >= 50) { + for (unsigned int x = 0; x < perfList.size(); ++x) { + printf("%s", perfList[x].c_str()); + } + } + } + + void Game::init() { + init(false); + } + + void Game::init(bool initForPreviewOnly) { + bool + showPerfStats = + Config::getInstance().getBool("ShowPerfStats", "false"); + Chrono chronoPerf; + if (showPerfStats) + chronoPerf.start(); + char perfBuf[8096] = ""; + std::vector < string > perfList; + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] initForPreviewOnly = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + initForPreviewOnly); + + Lang & lang = Lang::getInstance(); + Logger & logger = Logger::getInstance(); + CoreData & coreData = CoreData::getInstance(); + Renderer & renderer = Renderer::getInstance(); + Map *map = world.getMap(); + NetworkManager & networkManager = NetworkManager::getInstance(); + + GameSettings::playerDisconnectedText = + "*" + lang.getString("AI") + "* "; + + if (map == NULL) { + throw megaglest_runtime_error("map == NULL"); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (initForPreviewOnly == false) { + logger.setState(lang.getString("Initializing")); + + //message box + mainMessageBox.init(lang.getString("Yes"), lang.getString("No")); + mainMessageBox.setEnabled(false); + + //message box + errorMessageBox.init(lang.getString("Ok")); + errorMessageBox.setEnabled(false); + errorMessageBox.setY(20); + + + //init world, and place camera + commander.init(&world); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + } + + try { + world.init(this, gameSettings.getDefaultUnits()); + } catch (const megaglest_runtime_error & ex) { + string sErrBuf = ""; + if (ex.wantStackTrace() == true) { + char szErrBuf[8096] = ""; + snprintf(szErrBuf, 8096, "In [%s::%s %d]", __FILE__, + __FUNCTION__, __LINE__); + sErrBuf = + string(szErrBuf) + string("\nerror [") + string(ex.what()) + + string("]\n"); + } else { + sErrBuf = ex.what(); + } + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + sErrBuf.c_str()); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + } catch (const exception & ex) { + char szErrBuf[8096] = ""; + snprintf(szErrBuf, 8096, "In [%s::%s %d]", __FILE__, __FUNCTION__, + __LINE__); + string + sErrBuf = + string(szErrBuf) + string("\nerror [") + string(ex.what()) + + string("]\n"); + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + sErrBuf.c_str()); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (loadGameNode != NULL) { + //world.getMapPtr()->loadGame(loadGameNode,&world); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (initForPreviewOnly == false) { + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + gui.init(this); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + //SDL_PumpEvents(); + + chatManager.init(&console, world.getThisTeamIndex()); + console.clearStoredLines(); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (this->loadGameNode == NULL) { + initCamera(map); + } else { + gui.loadGame(loadGameNode, &world); + + if (inJoinGameLoading == true) { + initCamera(map); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + NetworkRole role = nrIdle; + if (initForPreviewOnly == false) { + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + scriptManager.init(&world, &gameCamera, loadGameNode); + + //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] creating AI's\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //create AIs + + bool + enableServerControlledAI = + this->gameSettings.getEnableServerControlledAI(); + bool isNetworkGame = this->gameSettings.isNetworkGame(); + role = networkManager.getNetworkRole(); + + masterController.clearSlaves(true); + deleteValues(aiInterfaces.begin(), aiInterfaces.end()); + + std::vector < SlaveThreadControllerInterface * >slaveThreadList; + aiInterfaces.resize(world.getFactionCount()); + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + //printf("Controltype = %d for index = %d\n",faction->getControlType(),i); + + if (faction->getCpuControl(enableServerControlledAI, isNetworkGame, + role) == true) { + //printf("** Loading AI player for Controltype = %d for index = %d\n",faction->getControlType(),i); + + aiInterfaces[i] = new AiInterface(*this, i, faction->getTeam()); + if (loadGameNode != NULL) { + aiInterfaces[i]->loadGame(loadGameNode, faction); + } + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + Lang::getInstance().getString + ("LogScreenGameLoadingCreatingAIFaction", "", + true).c_str(), i); + logger.add(szBuf, true); + + slaveThreadList.push_back(aiInterfaces[i]->getWorkerThread()); + } else { + aiInterfaces[i] = NULL; + } + } + if (Config:: + getInstance().getBool("EnableNewThreadManager", + "false") == true) { + masterController.setSlaves(slaveThreadList); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + if (world.getFactionCount() == 1 + && world.getFaction(0)->getPersonalityType() == fpt_Observer) { + withRainEffect = false; + } + + if (withRainEffect) { + //weather particle systems + if (world.getTileset()->getWeather() == wRainy) { + logger.add(Lang::getInstance().getString + ("LogScreenGameLoadingCreatingRainParticles", + "", true), true); + weatherParticleSystem = new RainParticleSystem(); + weatherParticleSystem->setSpeed(12.f / GameConstants::updateFps); + weatherParticleSystem->setPos(gameCamera.getPos()); + renderer.manageParticleSystem(weatherParticleSystem, rsGame); + } else if (world.getTileset()->getWeather() == wSnowy) { + logger.add(Lang::getInstance().getString + ("LogScreenGameLoadingCreatingSnowParticles", + "", true), true); + weatherParticleSystem = new SnowParticleSystem(1200); + weatherParticleSystem->setSpeed(1.5f / GameConstants::updateFps); + weatherParticleSystem->setPos(gameCamera.getPos()); + weatherParticleSystem->setTexture(coreData.getSnowTexture()); + renderer.manageParticleSystem(weatherParticleSystem, rsGame); + } + } else if (world.getTileset()->getWeather() == wRainy) { + world.getTileset()->setWeather(wSunny); + } + + renderer.manageDeferredParticleSystems(); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //init renderer state + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Initializing renderer\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__); + logger. + add(Lang:: + getInstance().getString("LogScreenGameLoadingInitRenderer", "", + true), true); + + //printf("Before renderer.initGame\n"); + renderer.initGame(this, this->getGameCameraPtr()); + //printf("After renderer.initGame\n"); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + if (faction != NULL) { + faction->deletePixels(); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (initForPreviewOnly == false) { + //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + // give CPU time to update other things to avoid apperance of hanging + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Waiting for network\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__); + logger. + add(Lang:: + getInstance().getString + ("LogScreenGameLoadingWaitForNetworkPlayers", "", true), true); + networkManager.getGameNetworkInterface()->waitUntilReady(&checksum); + + //std::string worldLog = world.DumpWorldToLog(true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Starting music stream\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + logger. + add(Lang:: + getInstance().getString("LogScreenGameLoadingStartingMusic", + "", true), true); + + if (this->masterserverMode == false) { + if (world.getThisFaction() == NULL) { + throw megaglest_runtime_error("world.getThisFaction() == NULL"); + } + if (world.getThisFaction()->getType() == NULL) { + throw + megaglest_runtime_error + ("world.getThisFaction()->getType() == NULL"); + } + //if(world.getThisFaction()->getType()->getMusic() == NULL) { + // throw megaglest_runtime_error("world.getThisFaction()->getType()->getMusic() == NULL"); + //} + } + + //sounds + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); + soundRenderer = SoundRenderer::getInstance(); + + Tileset *tileset = world.getTileset(); + AmbientSounds *ambientSounds = tileset->getAmbientSounds(); + + //rain + if (tileset->getWeather() == wRainy + && ambientSounds->isEnabledRain()) { + logger. + add(Lang:: + getInstance().getString + ("LogScreenGameLoadingStartingAmbient", "", true), true); + currentAmbientSound = ambientSounds->getRain(); + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + soundRenderer.playAmbient(currentAmbientSound); + } + + //snow + if (tileset->getWeather() == wSnowy + && ambientSounds->isEnabledSnow()) { + logger. + add(Lang:: + getInstance().getString + ("LogScreenGameLoadingStartingAmbient", "", true), true); + currentAmbientSound = ambientSounds->getSnow(); + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + soundRenderer.playAmbient(currentAmbientSound); + } + + if (this->masterserverMode == false) { + StrSound *gameMusic = + world.getThisFaction()->getType()->getMusic(); + soundRenderer.playMusic(gameMusic); + } + + logger. + add(Lang:: + getInstance().getString("LogScreenGameLoadingLaunchGame", "", + true)); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //throw "test"; + + logger.setCancelLoadingEnabled(false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "================ STARTING GAME ================\n"); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPathFinder).enabled) + SystemFlags::OutputDebug(SystemFlags::debugPathFinder, + "================ STARTING GAME ================\n"); + setupPopupMenus(false); + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + //printf("Check for team switch to observer i = %d, team = %d [%d]\n",i,faction->getTeam(),(GameConstants::maxPlayers -1 + fpt_Observer)); + if (faction != NULL + && faction->getTeam() == + GameConstants::maxPlayers - 1 + fpt_Observer) { + faction->setPersonalityType(fpt_Observer); + world.getStats()->setPersonalityType(i, + faction->getPersonalityType + ()); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (role == nrClient) { + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + if (clientInterface != NULL + && clientInterface->getResumeInGameJoin() == true) { + + //printf("Client sending resume message to server...\n"); + + clientInterface->sendResumeGameMessage(); + //this->initialResumeSpeedLoops = true; + } + } + + printf("Game unique identifier is: %s\n", + this->gameSettings.getGameUUID().c_str()); + + gameStarted = true; + + if (this->masterserverMode == true) { + world.getStats()->setIsMasterserverMode(true); + + printf("New game has started...\n"); + } + + if (isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == true) { + printf("*Note: Monitoring Network CRC VERBOSE synchronization...\n"); + } else if (isFlagType1BitEnabled(ft1_network_synch_checks) == true) { + printf("*Note: Monitoring Network CRC NORMAL synchronization...\n"); + } + + //NetworkRole role = networkManager.getNetworkRole(); + if (role == nrServer) { + networkManager.initServerInterfaces(this); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ==== START GAME ==== getCurrentPixelByteCount() = " + MG_SIZE_T_SPECIFIER "\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderer.getCurrentPixelByteCount()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "=============================================\n"); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "==== START GAME ====\n"); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "=============================================\n"); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "Starting framecount: %d\n", + world.getFrameCount()); + + if (showPerfStats && chronoPerf.getMillis() >= 50) { + for (unsigned int x = 0; x < perfList.size(); ++x) { + printf("%s", perfList[x].c_str()); + } + } + } + + void Game::initCamera(Map * map) { + gameCamera.init(map->getW(), map->getH()); + + // camera default height calculation + if (map->getCameraHeight() > 0 + && gameCamera.getCalculatedDefault() < map->getCameraHeight()) { + gameCamera.setCalculatedDefault(map->getCameraHeight()); + } else if (gameCamera.getCalculatedDefault() < + map->getMaxMapHeight() + 13.0f) { + gameCamera.setCalculatedDefault(map->getMaxMapHeight() + 13.0f); + } + + if (world.getThisFaction() != NULL) { + const + Vec2i & + v = + map->getStartLocation(world. + getThisFaction()->getStartLocationIndex()); + // This args are set in map.cpp - Map::getStartLocation() + gameCamera.setPos(Vec2f + (v.x, + v.y + gameCamera.getCalculatedDefault() / 2)); + // + // for issue 13: observer mode (wip) + // This sets the camera position the same for each player. + // The goal is to set the camera position to this for observers only + // since they don't have a StartLocationIndex + // gameCamera.setPos(Vec2f(10, 10)); + + } + } + + // ==================== update ==================== + + void Game::reInitGUI() { + gui.init(this); + } + + void Game::setupPopupMenus(bool checkClientAdminOverrideOnly) { + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + ClientInterface *clientInterface = NULL; + ServerInterface *serverInterface = NULL; + + bool allowAdminMenuItems = false; + bool forceJoinInProgressUpdate = false; + if (role == nrServer) { + allowAdminMenuItems = true; + + if (disconnectPlayerPopupMenuIndex == -1) { + serverInterface = + dynamic_cast < + ServerInterface *>(networkManager.getServerInterface()); + if (serverInterface != NULL && checkClientAdminOverrideOnly == true) { + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(serverInterface->getSlotMutex + (faction->getStartLocationIndex()), CODE_AT_LINE); + ConnectionSlot *slot = + serverInterface->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getConnectHasHandshaked() == true + && slot->getCurrentFrameCount() <= 0) { + //printf("Connected slot can be disconnected: %d\n",slot->getPlayerIndex()); + + forceJoinInProgressUpdate = true; + break; + } + } + } + } + } else if (role == nrClient) { + clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + + if (clientInterface != NULL && + (gameSettings.getMasterserver_admin() == + clientInterface->getSessionKey() + || clientInterface->isMasterServerAdminOverride() == true)) { + allowAdminMenuItems = true; + } + } + + if (checkClientAdminOverrideOnly == false || + forceJoinInProgressUpdate == true || + (clientInterface != NULL && + (gameSettings.getMasterserver_admin() != + clientInterface->getSessionKey() + && clientInterface->isMasterServerAdminOverride() == true))) { + exitGamePopupMenuIndex = -1; + joinTeamPopupMenuIndex = -1; + pauseGamePopupMenuIndex = -1; + saveGamePopupMenuIndex = -1; + loadGamePopupMenuIndex = -1; + keyboardSetupPopupMenuIndex = -1; + disconnectPlayerPopupMenuIndex = -1; + + if (checkClientAdminOverrideOnly == true && clientInterface != NULL) { + gameSettings. + setMasterserver_admin(clientInterface->getSessionKey()); + gameSettings.setMasterserver_admin_faction_index + (clientInterface->getPlayerIndex()); + } + //PopupMenu popupMenu; + std::vector < string > menuItems; + menuItems.push_back(" " + lang.getString("ExitGameMenu") + " "); + exitGamePopupMenuIndex = (int) menuItems.size() - 1; + + if ((gameSettings.getFlagTypes1() & ft1_allow_team_switching) == + ft1_allow_team_switching && world.getThisFaction() != NULL + && world.getThisFaction()->getPersonalityType() != fpt_Observer) { + menuItems.push_back(" " + lang.getString("JoinOtherTeam") + + " "); + joinTeamPopupMenuIndex = (int) menuItems.size() - 1; + } + + if (allowAdminMenuItems == true) { + menuItems.push_back(" " + lang.getString("PauseResumeGame") + + " "); + pauseGamePopupMenuIndex = (int) menuItems.size() - 1; + + if (gameSettings.isNetworkGame() == false + || gameSettings.getScenario() != "") { + menuItems.push_back(" " + lang.getString("SaveGame") + " "); + saveGamePopupMenuIndex = (int) menuItems.size() - 1; + } + + if (gameSettings.isNetworkGame() == true) { + menuItems.push_back(" " + + lang.getString("DisconnectNetorkPlayer") + + " "); + disconnectPlayerPopupMenuIndex = (int) menuItems.size() - 1; + } + } + menuItems.push_back(" " + lang.getString("KeyboardsetupL") + " "); + keyboardSetupPopupMenuIndex = (int) menuItems.size() - 1; + + menuItems.push_back(" " + lang.getString("Cancel") + " "); + + popupMenu.setW(100); + popupMenu.setH(100); + popupMenu.init(" " + lang.getString("GameMenuTitle") + " ", + menuItems); + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + popupMenuSwitchTeams.setEnabled(false); + popupMenuSwitchTeams.setVisible(false); + + popupMenuDisconnectPlayer.setEnabled(false); + popupMenuDisconnectPlayer.setVisible(false); + } + } + + void Game::processNetworkSynchChecksIfRequired() { + bool isNetworkGame = this->gameSettings.isNetworkGame(); + if (isNetworkGame == true + && NetworkManager::getInstance().getGameNetworkInterface() != + NULL) { + GameSettings *settings = world.getGameSettingsPtr(); + if (settings != NULL) { + bool calculateNetworkCRC = false; + + if (isFlagType1BitEnabled(ft1_network_synch_checks) == true || + isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == + true) { + calculateNetworkCRC = true; + } + + if (calculateNetworkCRC == true) { + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + + NetworkInterface *netIntf = + networkManager.getGameNetworkInterface(); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + if (index < world.getFactionCount()) { + Faction *faction = world.getFaction(index); + netIntf->setNetworkPlayerFactionCRC(index, + faction->getCRC().getSum + ()); + + if (settings != NULL) { + if (isFlagType1BitEnabled + (ft1_network_synch_checks_verbose) == true) { + faction->addCRC_DetailsForWorldFrame(world.getFrameCount + (), + role == nrServer); + } else + if (isFlagType1BitEnabled + (ft1_network_synch_checks) == true + && world.getFrameCount() % 20 == 0) { + faction->addCRC_DetailsForWorldFrame(world.getFrameCount + (), + role == nrServer); + } + } + } else { + netIntf->setNetworkPlayerFactionCRC(index, 0); + } + } + } + } + } + } + + //update + void Game::update() { + try { + if (currentUIState != NULL) { + currentUIState->update(); + } + + bool + showPerfStats = + Config::getInstance().getBool("ShowPerfStats", "false"); + Chrono chronoPerf; + char perfBuf[8096] = ""; + std::vector < string > perfList; + if (showPerfStats) + chronoPerf.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "=============== FRAME: %d In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", world.getFrameCount(), + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + Chrono chronoGamePerformanceCounts; + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + // a) Updates non dependent on speed + + // set game stats for host + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + if (role == nrServer) { + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(false); + if (server != NULL) { + server->setGameStats(world.getStats()); + } + } + + bool + pendingQuitError = (quitPendingIndicator == true || + (NetworkManager:: + getInstance().getGameNetworkInterface() != + NULL + && NetworkManager:: + getInstance().getGameNetworkInterface()-> + getQuit())); + + if (pendingQuitError == true && + (this->masterserverMode == true || + (mainMessageBox.getEnabled() == false + && errorMessageBox.getEnabled() == false))) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + quitTriggeredIndicator = true; + return; + } + + if (this->masterserverMode == false) { + if (world.getFactionCount() > 0 + && world.getThisFaction()->getFirstSwitchTeamVote() != NULL) { + const SwitchTeamVote *vote = + world.getThisFaction()->getFirstSwitchTeamVote(); + GameSettings *settings = world.getGameSettingsPtr(); + + Lang & lang = Lang::getInstance(); + + char szBuf[8096] = ""; + if (lang.hasString("AllowPlayerJoinTeam") == true) { + snprintf(szBuf, 8096, + lang.getString("AllowPlayerJoinTeam").c_str(), + settings-> + getNetworkPlayerName(vote->factionIndex).c_str(), + vote->oldTeam, vote->newTeam); + } else { + snprintf(szBuf, 8096, + "Allow player [%s] to join your team\n(changing from team# %d to team# %d)?", + settings-> + getNetworkPlayerName(vote->factionIndex).c_str(), + vote->oldTeam, vote->newTeam); + } + + switchTeamConfirmMessageBox.setText(szBuf); + switchTeamConfirmMessageBox.init(lang.getString("Yes"), + lang.getString("No")); + switchTeamConfirmMessageBox.setEnabled(true); + + world. + getThisFactionPtr()->setCurrentSwitchTeamVoteFactionIndex + (vote->factionIndex); + } + } + + //misc + updateFps++; + mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; + + //console + console.update(); + + // b) Updates depandant on speed + int updateLoops = getUpdateLoops(); + + // Temp speed boost when player first joins an in progress game + if (this->initialResumeSpeedLoops == true) { + printf("Resume In Progress Game: %d\n", __LINE__); + + this->initialResumeSpeedLoops = false; + //updateLoops = 80; + } + + chronoGamePerformanceCounts.start(); + bool + enableServerControlledAI = + this->gameSettings.getEnableServerControlledAI(); + + if (role == nrClient && updateLoops == 1 + && world.getFrameCount() >= + (gameSettings.getNetworkFramePeriod() * 2)) { + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + if (clientInterface != NULL) { + uint64 + lastNetworkFrameFromServer = + clientInterface->getCachedLastPendingFrameCount(); + + ///////////////////////////////// + // TTTT new attempt to make things smoother: + /////////////// + + //////////////////////////////////////////// + //get stats of received/waiting for packages + //////////////////////////////////////////// + // calculate current receive Index slot: + int + index = ((world.getFrameCount() + - (world.getFrameCount() + % gameSettings.getNetworkFramePeriod())) + / gameSettings.getNetworkFramePeriod()) + % GameConstants::networkSmoothInterval; + + // clean the next frame slot + receivedTooEarlyInFrames[(index + + 1) % + GameConstants::networkSmoothInterval] = + -1; + framesNeededToWaitForServerMessage[(index + + 1) % + GameConstants::networkSmoothInterval] + = -1; + + if (receivedTooEarlyInFrames[index] == -1) { + // we need to check if we already received something for next frame + if (lastNetworkFrameFromServer > 0 + && lastNetworkFrameFromServer > + (uint64) world.getFrameCount()) { + receivedTooEarlyInFrames[index] = + lastNetworkFrameFromServer - world.getFrameCount(); + } + } + if (framesNeededToWaitForServerMessage[index] == -1) { + // calc time waiting for message in milliseconds to frames + int64 + timeClientWaitedForLastMessage = + clientInterface->getTimeClientWaitedForLastMessage(); + if (timeClientWaitedForLastMessage > 0) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("world.getFrameCount():%d index %d Client waited:%d ms\n", + world.getFrameCount(), index, + (int) timeClientWaitedForLastMessage); + framesNeededToWaitForServerMessage[index] = + timeClientWaitedForLastMessage * + GameConstants::updateFps / 1000; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("ClienttimeClientWaitedForLastMessage:%d ms which is %d frames \n", + (int) timeClientWaitedForLastMessage, + framesNeededToWaitForServerMessage[index]); + } else { + framesNeededToWaitForServerMessage[index] = 0; + } + } + + //////////////////////////////////////////// + //use the recorded stats of received/waiting for packages + //////////////////////////////////////////// + //lets see if the client is in front and had to wait for messages ... + + //lets see if all last recorded frames where received too early + int minimum = 0; + int allowedMaxFallback = 5; + int countOfMessagesReceivedTooEarly = 0; + int countOfMessagesReceivedTooLate = 0; + int sumOfTooLateFrames = 0; + bool cleanupStats = false; + + for (int i = 0; i < GameConstants::networkSmoothInterval; i++) { + if (receivedTooEarlyInFrames[i] > allowedMaxFallback) { + countOfMessagesReceivedTooEarly++; + if (minimum == 0 || minimum > receivedTooEarlyInFrames[i]) { + minimum = receivedTooEarlyInFrames[i]; + } + } + if (framesNeededToWaitForServerMessage[i] > 0) { + countOfMessagesReceivedTooLate++; + sumOfTooLateFrames += framesNeededToWaitForServerMessage[i]; + } + } + + if (countOfMessagesReceivedTooEarly == GameConstants::networkSmoothInterval - 1) // -1 because slot for next frame is already initialized + { // all packages where too early + // we catch up the minimum-catchupInterval of what we recorded + framesToCatchUpAsClient = minimum - allowedMaxFallback; + framesToSlowDownAsClient = 0; + cleanupStats = true; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Worldframe %d : Client will speed up: %d frames\n", + world.getFrameCount(), framesToCatchUpAsClient); + } else if (countOfMessagesReceivedTooLate > 3) { + framesToSlowDownAsClient = + sumOfTooLateFrames / countOfMessagesReceivedTooLate; + framesToCatchUpAsClient = 0; + cleanupStats = true; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Worldframe %d : Client will slow down: %d frames\n", + world.getFrameCount(), framesToSlowDownAsClient); + } + + if (cleanupStats == true) { + // Once we decided to use the stats to do some correction, we reset/cleanup our recorded stats + for (int i = 0; i < GameConstants::networkSmoothInterval; i++) { + receivedTooEarlyInFrames[i] = -1; + framesNeededToWaitForServerMessage[i] = -1; + } + } + } + } + // if game is paused don't try to catch up + if (updateLoops > 0) { + // we catch up a bit smoother with updateLoops = 2 + if (framesToCatchUpAsClient > 0) { + updateLoops = 2; + framesToCatchUpAsClient = framesToCatchUpAsClient - 1; + } + if (framesToSlowDownAsClient > 0) { // slowdown still the hard way. + updateLoops = 0; + framesToSlowDownAsClient = framesToSlowDownAsClient - 1; + } + } + + addPerformanceCount("CalculateNetworkUpdateLoops", + chronoGamePerformanceCounts.getMillis()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [before ReplaceDisconnectedNetworkPlayersWithAI]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // Check to see if we are playing a network game and if any players + // have disconnected? + bool isNetworkGame = this->gameSettings.isNetworkGame(); + + chronoGamePerformanceCounts.start(); + + ReplaceDisconnectedNetworkPlayersWithAI(isNetworkGame, role); + + addPerformanceCount("ReplaceDisconnectedNetworkPlayersWithAI", + chronoGamePerformanceCounts.getMillis()); + + setupPopupMenus(true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [after ReplaceDisconnectedNetworkPlayersWithAI]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (updateLoops > 0) { + // update the frame based timer in the stats with at least one step + world.getStats()->addFramesToCalculatePlaytime(); + + //update + Chrono chronoReplay; + int64 lastReplaySecond = -1; + int replayCommandsPlayed = 0; + int replayTotal = commander.getReplayCommandListForFrameCount(); + if (replayTotal > 0) { + chronoReplay.start(); + } + + do { + if (replayTotal > 0) { + replayCommandsPlayed = + (replayTotal - + commander.getReplayCommandListForFrameCount()); + } + for (int i = 0; i < updateLoops; ++i) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //AiInterface + if (commander.hasReplayCommandListForFrame() == false) { + chronoGamePerformanceCounts.start(); + + processNetworkSynchChecksIfRequired(); + + addPerformanceCount("CalculateNetworkCRCSynchChecks", + chronoGamePerformanceCounts.getMillis + ()); + + const bool + newThreadManager = + Config::getInstance().getBool("EnableNewThreadManager", + "false"); + if (newThreadManager == true) { + int currentFrameCount = world.getFrameCount(); + masterController.signalSlaves(¤tFrameCount); + //bool slavesCompleted = masterController.waitTillSlavesTrigger(20000); + masterController.waitTillSlavesTrigger(20000); + } else { + // Signal the faction threads to do any pre-processing + chronoGamePerformanceCounts.start(); + + bool hasAIPlayer = false; + for (int j = 0; j < world.getFactionCount(); ++j) { + Faction *faction = world.getFaction(j); + + //printf("Faction Index = %d enableServerControlledAI = %d, isNetworkGame = %d, role = %d isCPU player = %d scriptManager.getPlayerModifiers(j)->getAiEnabled() = %d\n",j,enableServerControlledAI,isNetworkGame,role,faction->getCpuControl(enableServerControlledAI,isNetworkGame,role),scriptManager.getPlayerModifiers(j)->getAiEnabled()); + + if (faction->getCpuControl(enableServerControlledAI, + isNetworkGame, + role) == true + && scriptManager. + getPlayerModifiers(j)->getAiEnabled() == true) { + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags:: + OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] [i = %d] faction = %d, factionCount = %d, took msecs: %lld [before AI updates]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, j, + world.getFactionCount(), + chrono.getMillis()); + aiInterfaces[j]->signalWorkerThread(world.getFrameCount + ()); + hasAIPlayer = true; + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (hasAIPlayer == true) { + //sleep(0); + + Chrono chronoAI; + chronoAI.start(); + + const int MAX_FACTION_THREAD_WAIT_MILLISECONDS = 20000; + for (; + chronoAI.getMillis() < + MAX_FACTION_THREAD_WAIT_MILLISECONDS;) { + bool workThreadsFinished = true; + for (int j = 0; j < world.getFactionCount(); ++j) { + Faction *faction = world.getFaction(j); + if (faction == NULL) { + throw megaglest_runtime_error("faction == NULL"); + } + if (faction->getCpuControl + (enableServerControlledAI, + isNetworkGame, role) == true + && + scriptManager.getPlayerModifiers(j)->getAiEnabled + () == true) { + if (aiInterfaces[j]->isWorkerThreadSignalCompleted + (world.getFrameCount()) == false) { + workThreadsFinished = false; + break; + } + } + } + if (workThreadsFinished == false) { + //sleep(0); + } else { + break; + } + } + } + + addPerformanceCount("ProcessAIWorkerThreads", + chronoGamePerformanceCounts.getMillis + ()); + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + } else { + // Simply show a progress message while replaying commands + if (lastReplaySecond < chronoReplay.getSeconds()) { + lastReplaySecond = chronoReplay.getSeconds(); + Renderer & renderer = Renderer::getInstance(); + renderer.clearBuffers(); + renderer.clearZBuffer(); + renderer.reset2d(); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Please wait, loading game with replay [%d / %d]...", + replayCommandsPlayed, replayTotal); + string text = szBuf; + if (Renderer::renderText3DEnabled) { + Font3D *font = + CoreData::getInstance().getMenuFontBig3D(); + const Metrics & metrics = Metrics::getInstance(); + int w = metrics.getVirtualW(); + int + renderX = + (w / 2) - + (font->getMetrics()->getTextWidth(text) / 2); + int h = metrics.getVirtualH(); + int + renderY = + (h / 2) + (font->getMetrics()->getHeight(text) / 2); + + renderer.renderText3D(text, font, + Vec3f(1.f, 1.f, 0.f), + renderX, renderY, false); + } else { + Font2D *font = CoreData::getInstance().getMenuFontBig(); + const Metrics & metrics = Metrics::getInstance(); + int w = metrics.getVirtualW(); + int renderX = (w / 2); + int h = metrics.getVirtualH(); + int renderY = (h / 2); + + renderer.renderText(text, font, + Vec3f(1.f, 1.f, 0.f), + renderX, renderY, true); + } + + renderer.swapBuffers(); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [AI updates]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //World + chronoGamePerformanceCounts.start(); + + if (pendingQuitError == false) + world.update(); + + addPerformanceCount("ProcessWorldUpdate", + chronoGamePerformanceCounts.getMillis()); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [world update i = %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis(), i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (currentCameraFollowUnit != NULL) { + Vec3f c = currentCameraFollowUnit->getCurrMidHeightVector(); + int rotation = currentCameraFollowUnit->getRotation(); + float angle = rotation + 180; + + c.z = c.z + 4 * std::cos(degToRad(angle)); + c.x = c.x + 4 * std::sin(degToRad(angle)); + + c.y = + c.y + + currentCameraFollowUnit->getType()->getHeight() / + 2.f + 2.0f; + + getGameCameraPtr()->setPos(c); + + rotation = (540 - rotation) % 360; + getGameCameraPtr()->rotateToVH(18.0f, rotation); + + if (currentCameraFollowUnit->isAlive() == false) { + currentCameraFollowUnit = NULL; + getGameCameraPtr()->setState(GameCamera::sGame); + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + // Commander + chronoGamePerformanceCounts.start(); + + if (pendingQuitError == false) { + commander.signalNetworkUpdate(this); + } + + addPerformanceCount("ProcessNetworkUpdate", + chronoGamePerformanceCounts.getMillis()); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [commander updateNetwork i = %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis(), i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //Gui + chronoGamePerformanceCounts.start(); + + gui.update(); + + addPerformanceCount("ProcessGUIUpdate", + chronoGamePerformanceCounts.getMillis()); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [gui updating i = %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis(), i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //Particle systems + if (weatherParticleSystem != NULL) { + weatherParticleSystem->setPos(gameCamera.getPos()); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [weather particle updating i = %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis(), i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + Renderer & renderer = Renderer::getInstance(); + + chronoGamePerformanceCounts.start(); + + renderer.updateParticleManager(rsGame, avgRenderFps); + + addPerformanceCount("ProcessParticleManager", + chronoGamePerformanceCounts.getMillis()); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [particle manager updating i = %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis(), i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " + MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //good_fpu_control_registers(NULL,extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } + } while (commander.hasReplayCommandListForFrame() == true); + } + //else if(role == nrClient) { + else { + if (pendingQuitError == false) { + commander.signalNetworkUpdate(this); + } + + if (playingStaticVideo == true) { + if (videoPlayer->isPlaying() == false) { + playingStaticVideo = false; + tryPauseToggle(false); + } + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + chronoGamePerformanceCounts.start(); + + //call the chat manager + chatManager.updateNetwork(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld [chatManager.updateNetwork]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + updateNetworkMarkedCells(); + updateNetworkUnMarkedCells(); + updateNetworkHighligtedCells(); + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + //check for quiting status + if (NetworkManager::getInstance().getGameNetworkInterface() != NULL + && NetworkManager::getInstance(). + getGameNetworkInterface()->getQuit() + && mainMessageBox.getEnabled() == false + && errorMessageBox.getEnabled() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + quitTriggeredIndicator = true; + return; + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + addPerformanceCount("ProcessMiscNetwork", + chronoGamePerformanceCounts.getMillis()); + + // START - Handle joining in progress games + if (role == nrServer) { + + if (this->networkPauseGameForLaggedClientsRequested == true) { + this->networkPauseGameForLaggedClientsRequested = false; + + if (getPaused() == false) { + + printf + ("[CMDR] Pausing game for lagging client(s), current world frame [%d]\n", + world.getFrameCount()); + commander.tryPauseGame(false, false); + } + } else if (this->networkResumeGameForLaggedClientsRequested == true) { + this->networkResumeGameForLaggedClientsRequested = false; + + if (getPaused() == true) { + + printf + ("[CMDR] Resuming game after Pause for lagging client(s), current world frame [%d]\n", + world.getFrameCount()); + commander.tryResumeGame(false, false); + } + } + + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(); + if (server->getPauseForInGameConnection() == true) { + + bool clientNeedsGameSetup = false; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getPauseForInGameConnection() == true) { + clientNeedsGameSetup = true; + break; + } + } + + if (pausedForJoinGame == false || clientNeedsGameSetup == true) { + //printf("================= Switching player pausing game\n"); + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + //printf("Switching player check %d from: %d connected: %d, startindex = %d, connected #2: %d\n",i,faction->getControlType(),server->isClientConnected(faction->getStartLocationIndex()),faction->getStartLocationIndex(),server->isClientConnected(i)); + //printf("Slot: %d faction name: %s\n",i,faction->getType()->getName().c_str()); + + if (faction->getControlType() != ctNetwork && + faction->getControlType() != ctHuman && + server->isClientConnected(faction->getStartLocationIndex + ()) == true) { + + //printf("Switching player %d from: %d to %d\n",i,faction->getControlType(),ctNetwork); + //printf("Slot: %d faction name: %s GS faction: %s\n",i,faction->getType()->getName().c_str(),server->gameSettings.getFactionTypeName(i).c_str()); + + server->gameSettings.setFactionControl(i, ctNetwork); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), + CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + server->gameSettings.setNetworkPlayerName(i, + slot->getName + ()); + server->gameSettings.setNetworkPlayerUUID(i, + slot->getUUID + ()); + server->gameSettings.setNetworkPlayerPlatform(i, + slot->getPlatform + ()); + safeMutex.ReleaseLock(); + server->gameSettings.setNetworkPlayerStatuses(i, + npst_None); + + this->gameSettings.setFactionControl(i, ctNetwork); + this->gameSettings.setNetworkPlayerName(i, + server->gameSettings.getNetworkPlayerName + (i)); + this->gameSettings.setNetworkPlayerUUID(i, + server->gameSettings.getNetworkPlayerUUID + (i)); + this->gameSettings.setNetworkPlayerPlatform(i, + server->gameSettings.getNetworkPlayerPlatform + (i)); + this->gameSettings.setNetworkPlayerStatuses(i, npst_None); + } + } + //printf("#1 Data synch: lmap %u ltile: %d ltech: %u\n",gameSettings.getMapCRC(),gameSettings.getTilesetCRC(),gameSettings.getTechCRC()); + //printf("#2 Data synch: lmap %u ltile: %d ltech: %u\n",server->gameSettings.getMapCRC(),server->gameSettings.getTilesetCRC(),server->gameSettings.getTechCRC()); + server->broadcastGameSetup(&server->gameSettings, true); + } + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getPauseForInGameConnection() == true) { + slot->setPauseForInGameConnection(false); + } + } + } else if (server->getStartInGameConnectionLaunch() == true) { + //printf("^^^ getStartInGameConnectionLaunch triggered!\n"); + + //server->setStartInGameConnectionLaunch(false); + + //this->speed = 1; + + //Lang &lang= Lang::getInstance(); + bool pauseAndSaveGameForNewClient = false; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + + if (slot != NULL + && slot->getStartInGameConnectionLaunch() == true) { + //slot->setStartInGameConnectionLaunch(false); + pauseAndSaveGameForNewClient = true; + } + if (slot != NULL && slot->getJoinGameInProgress() == true) { + //printf("$$$ signalling client to start game [deleting AI player] factionIndex: %d slot: %d startlocation: %d!\n",i,slot->getPlayerIndex(),faction->getStartLocationIndex()); + safeMutex.ReleaseLock(); + + this->gameSettings.setFactionControl(i, ctNetwork); + this->gameSettings.setNetworkPlayerName(i, + server->gameSettings.getNetworkPlayerName + (i)); + this->gameSettings.setNetworkPlayerUUID(i, + server->gameSettings.getNetworkPlayerUUID + (i)); + this->gameSettings.setNetworkPlayerPlatform(i, + server->gameSettings.getNetworkPlayerPlatform + (i)); + + if (this->gameSettings.getNetworkPlayerStatuses(i) == + npst_Disconnected) { + this->gameSettings.setNetworkPlayerStatuses(i, npst_None); + } + + //printf("START Purging AI player for index: %d\n",i); + masterController.clearSlaves(true); + delete aiInterfaces[i]; + aiInterfaces[i] = NULL; + //printf("END Purging AI player for index: %d\n",i); + + Faction *faction = world.getFaction(i); + faction->setControlType(ctNetwork); + //pauseAndSaveGameForNewClient = true; + } else if ((slot == NULL || slot->isConnected() == false) + && this->gameSettings.getFactionControl(i) == + ctNetwork && aiInterfaces[i] == NULL) { + + safeMutex.ReleaseLock(); + faction->setFactionDisconnectHandled(false); + //this->gameSettings.setNetworkPlayerName(i,lang.getString("AI") + intToStr(i+1)); + //server->gameSettings.setNetworkPlayerName(i,lang.getString("AI") + intToStr(i+1)); + } else { + safeMutex.ReleaseLock(); + } + } + + if (pauseAndSaveGameForNewClient == true + && pausedForJoinGame == false && pauseRequestSent == false) { + //printf("Pausing game for join in progress game...\n"); + + commander.tryPauseGame(true, true); + pauseRequestSent = true; + return; + } + } + //else if(server->getPauseForInGameConnection() == true && paused == true && + if (pausedForJoinGame == true) { + if (pauseStateChanged == true) { + pauseStateChanged = false; + } + + if (server->getUnPauseForInGameConnection() == true) { + //printf("^^^ getUnPauseForInGameConnection triggered!\n"); + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), + CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getUnPauseForInGameConnection() == true) { + slot->setUnPauseForInGameConnection(false); + faction->setFactionDisconnectHandled(false); + } + } + //printf("Resuming game for join in progress game resumeRequestSent: %d...\n",resumeRequestSent); + + if (pausedBeforeJoinGame == false && resumeRequestSent == false) { + commander.tryResumeGame(true, true); + resumeRequestSent = true; + } + } else if (server->getStartInGameConnectionLaunch() == true) { + bool saveNetworkGame = false; + + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(); + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), + CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getJoinGameInProgress() == true + && slot->getStartInGameConnectionLaunch() == true + && slot->getSentSavedGameInfo() == false) { + slot->setStartInGameConnectionLaunch(false); + + saveNetworkGame = true; + break; + } + } + + if (saveNetworkGame == true) { + //printf("Saved network game to disk\n"); + + string + file = + this->saveGame(GameConstants::saveNetworkGameFileServer, + "temp/"); + + string saveGameFilePath = "temp/"; + string + saveGameFileCompressed = + saveGameFilePath + + string(GameConstants::saveNetworkGameFileServerCompressed); + if (getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) != "") { + saveGameFilePath = + getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) + + saveGameFilePath; + saveGameFileCompressed = + saveGameFilePath + + string + (GameConstants::saveNetworkGameFileServerCompressed); + } else { + string + userData = + Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + saveGameFilePath = userData + saveGameFilePath; + saveGameFileCompressed = + saveGameFilePath + + string + (GameConstants::saveNetworkGameFileServerCompressed); + } + + bool + compressed_result = + compressFileToZIPFile(file, saveGameFileCompressed); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Saved game [%s] compressed to [%s] returned: %d\n", + file.c_str(), saveGameFileCompressed.c_str(), + compressed_result); + + char szBuf[8096] = ""; + Lang & lang = Lang::getInstance(); + snprintf(szBuf, 8096, + lang.getString("GameSaved", "", + true).c_str(), file.c_str()); + console.addLine(szBuf); + + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), + CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getJoinGameInProgress() == true + && slot->getSentSavedGameInfo() == false) { + + safeMutex.ReleaseLock(); + NetworkMessageReady networkMessageReady(0); + slot->sendMessage(&networkMessageReady); + + slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL) { + slot->setSentSavedGameInfo(true); + } + } + } + } + } + } + //else { + // handle setting changes from clients + Map *map = world.getMap(); + //printf("switchSetupRequests != NULL\n"); + + bool + switchRequested = + switchSetupForSlots(server, 0, map->getMaxPlayers(), false); + switchRequested = switchRequested + || switchSetupForSlots(server, map->getMaxPlayers(), + GameConstants::maxPlayers, true); + + if (switchRequested == true) { + //printf("Send new game setup from switch: %d\n",switchRequested); + + //for(int i= 0; i < gameSettings.getFactionCount(); ++i) { + //printf("#1 Faction Index: %d control: %d startlocation: %d\n",i,gameSettings.getFactionControl(i),gameSettings.getStartLocationIndex(i)); + + //printf("#2 Faction Index: %d control: %d startlocation: %d\n",i,server->gameSettings.getFactionControl(i),server->gameSettings.getStartLocationIndex(i)); + //} + + server->broadcastGameSetup(&server->gameSettings, true); + } + //} + + // Make the server wait a bit for clients to start. + if (pausedForJoinGame == false && resumeRequestSent == true) { + resumeRequestSent = false; + //sleep(500); + } + } + // END - Handle joining in progress games + + //update auto test + if (Config::getInstance().getBool("AutoTest")) { + AutoTest::getInstance().updateGame(this); + return; + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (world.getQueuedScenario() != "") { + string name = world.getQueuedScenario(); + bool keepFactions = world.getQueuedScenarioKeepFactions(); + world.setQueuedScenario("", false); + + //vector results; + const + vector < + string > & + dirList = Config::getInstance().getPathListForType(ptScenarios); + string scenarioFile = Scenario::getScenarioPath(dirList, name); + + + try { + gameStarted = false; + + //printf("\nname [%s] scenarioFile [%s] results.size() = " MG_SIZE_T_SPECIFIER "\n",name.c_str(),scenarioFile.c_str(),results.size()); + //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + bool isTutorial = Scenario::isGameTutorial(scenarioFile); + ScenarioInfo scenarioInfo; + Scenario::loadScenarioInfo(scenarioFile, &scenarioInfo, + isTutorial); + + //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + GameSettings gameSettings; + Scenario::loadGameSettings(dirList, &scenarioInfo, + &gameSettings, scenarioFile); + + //Program *program = world->getGame()->getProgram(); + //program->setState(new Game(program, &gameSettings, false)); + + //world->end(); + + //world->getMapPtr()->end(); + //world.end(); + + if (keepFactions == false) { + world.end(); + + world.cleanup(); + world.clearTileset(); + + SoundRenderer::getInstance().stopAllSounds(); + + masterController.clearSlaves(true); + deleteValues(aiInterfaces.begin(), aiInterfaces.end()); + aiInterfaces.clear(); + gui.end(); //selection must be cleared before deleting units + world.end(); //must die before selection because of referencers + + BaseColorPickEntity::resetUniqueColors(); + // MUST DO THIS LAST!!!! Because objects above have pointers to things like + // unit particles and fade them out etc and this end method deletes the original + // object pointers. + Renderer & renderer = Renderer::getInstance(); + renderer.endGame(true); + + GameConstants::updateFps = original_updateFps; + GameConstants::cameraFps = original_cameraFps; + + this->setGameSettings(&gameSettings); + this->resetMembers(); + this->load(); + this->init(); + } else { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + if (currentAmbientSound) { + soundRenderer.stopAmbient(currentAmbientSound); + } + //soundRenderer.stopAllSounds(); + soundRenderer.stopAllSounds(fadeMusicMilliseconds); + + world.endScenario(); + BaseColorPickEntity::resetUniqueColors(); + + Renderer & renderer = Renderer::getInstance(); + renderer.endScenario(); + world.clearTileset(); + this->setGameSettings(&gameSettings); + this->load(lgt_FactionPreview | lgt_TileSet | lgt_Map | + lgt_Scenario); + try { + world.init(this, gameSettings.getDefaultUnits(), false); + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + } + + world.initUnitsForScenario(); + Map *map = world.getMap(); + gameCamera.init(map->getW(), map->getH()); + + // camera default height calculation + if (map->getCameraHeight() > 0 + && gameCamera.getCalculatedDefault() < + map->getCameraHeight()) { + gameCamera.setCalculatedDefault(map->getCameraHeight()); + } else if (gameCamera.getCalculatedDefault() < + map->getMaxMapHeight() + 13.0f) { + gameCamera.setCalculatedDefault(map->getMaxMapHeight() + + 13.0f); + } + + scriptManager.init(&world, &gameCamera, loadGameNode); + renderer.initGame(this, this->getGameCameraPtr()); + + //sounds + //soundRenderer.stopAllSounds(fadeMusicMilliseconds); + //soundRenderer.stopAllSounds(); + //soundRenderer= SoundRenderer::getInstance(); + + Tileset *tileset = world.getTileset(); + AmbientSounds *ambientSounds = tileset->getAmbientSounds(); + + //rain + if (tileset->getWeather() == wRainy + && ambientSounds->isEnabledRain()) { + //logger.add("Starting ambient stream", true); + currentAmbientSound = ambientSounds->getRain(); + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + soundRenderer.playAmbient(currentAmbientSound); + } + + //snow + if (tileset->getWeather() == wSnowy + && ambientSounds->isEnabledSnow()) { + //logger.add("Starting ambient stream", true); + currentAmbientSound = ambientSounds->getSnow(); + //printf("In [%s:%s] Line: %d currentAmbientSound = [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentAmbientSound); + soundRenderer.playAmbient(currentAmbientSound); + } + + if (this->masterserverMode == false) { + StrSound *gameMusic = + world.getThisFaction()->getType()->getMusic(); + soundRenderer.playMusic(gameMusic); + } + + gameStarted = true; + } + //this->init(); + + //printf("[%s:%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //Checksum checksum; + //world->loadScenario(scenarioFile, &checksum, true); + } #if defined(WIN32) - catch (const exception) - { + catch (const exception) { #else - catch (const exception & ex) - { + catch (const exception & ex) { #endif - gameStarted = true; - totalRenderFps++; - - throw; - } - } - - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } - - if (showPerfStats && chronoPerf.getMillis () >= 50) - { - for (unsigned int x = 0; x < (unsigned int) perfList.size (); ++x) - { - printf ("%s", perfList[x].c_str ()); - } - } - } - catch (const exception & ex) - { - quitPendingIndicator = true; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - //printf("#100 quitPendingIndicator = %d, errorMessageBox.getEnabled() = %d\n",quitPendingIndicator,errorMessageBox.getEnabled()); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - } - } - - void Game::addPerformanceCount (string key, int64 value) - { - gamePerformanceCounts[key] = value + gamePerformanceCounts[key] / 2; - } - - string Game::getGamePerformanceCounts (bool displayWarnings) const - { - if (gamePerformanceCounts.empty () == true) - { - return ""; - } - - bool displayWarningHeader = true; - bool - WARN_TO_CONSOLE = - Config::getInstance ().getBool ("PerformanceWarningEnabled", "false"); - int - WARNING_MILLIS = - Config::getInstance ().getInt ("PerformanceWarningMillis", "7"); - int - WARNING_RENDER_MILLIS = - Config::getInstance ().getInt ("PerformanceWarningRenderMillis", - "40"); - - string result = ""; - for (std::map < string, int64 >::const_iterator iterMap = - gamePerformanceCounts.begin (); - iterMap != gamePerformanceCounts.end (); ++iterMap) - { - if (iterMap->first == ProgramState::MAIN_PROGRAM_RENDER_KEY) - { - if (iterMap->second < WARNING_RENDER_MILLIS) - { - continue; - } - //else { - // printf("iterMap->second: " MG_I64_SPECIFIER " WARNING_RENDER_MILLIS = %d\n",iterMap->second,WARNING_RENDER_MILLIS); - //} - } - else if (iterMap->second < WARNING_MILLIS) - { - continue; - } - - if (result != "") - { - result += "\n"; - } - string - perfStat = - iterMap->first + " = avg millis: " + intToStr (iterMap->second); - - if (displayWarnings == true && WARN_TO_CONSOLE == true) - { - if (displayWarningHeader == true) - { - displayWarningHeader = false; - printf - ("=====================================\nPERFORMANCE WARNINGS for World Frame: %d\n", - world.getFrameCount ()); - } - - printf ("*PERFORMANCE WARNING* %s\n", perfStat.c_str ()); - } - - result += perfStat; - } - - return result; - } - - bool - Game::switchSetupForSlots (ServerInterface * &serverInterface, - int startIndex, int endIndex, - bool onlyNetworkUnassigned) - { - bool switchRequested = false; - if (serverInterface == NULL) - { - return switchRequested; - } - - MutexSafeWrapper - safeMutex (serverInterface->getSwitchSetupRequestsMutex (), - CODE_AT_LINE); - SwitchSetupRequest **switchSetupRequests = - serverInterface->getSwitchSetupRequests (); - if (switchSetupRequests == NULL) - { - return switchRequested; - } - - Map *map = world.getMap (); - for (int i = startIndex; i < endIndex; ++i) - { - if (switchSetupRequests[i] != NULL) - { - //printf("Faction Index: %d Switch slot = %d to = %d current control = %d\n",i,switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex(),gameSettings.getFactionControl(i)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - switchSetupRequests[i]->getSwitchFlags - ()); - - if (onlyNetworkUnassigned == true - && gameSettings.getFactionControl (i) != ctNetworkUnassigned) - { - if (i < map->getMaxPlayers () - || (i >= map->getMaxPlayers () - && gameSettings.getFactionControl (i) != ctNetwork)) - { - continue; - } - } - - if (gameSettings.getFactionControl (i) == ctNetwork || - gameSettings.getFactionControl (i) == ctNetworkUnassigned || - //(gameSettings.getFactionControl(i) != ctClosed && gameSettings.getFactionControl(i) != ctHuman)) { - (gameSettings.getFactionControl (i) != ctHuman)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - switchSetupRequests[i]->getToSlotIndex - ()); - - if (switchSetupRequests[i]->getToSlotIndex () != -1) - { - int newSlotIdx = switchSetupRequests[i]->getToSlotIndex (); - - //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex()); - - int - switchSlotIdx = - switchSetupRequests[i]->getCurrentSlotIndex (); - if (serverInterface->switchSlot (switchSlotIdx, newSlotIdx)) - { - //printf("switchSlot returned true\n"); - switchRequested = true; - - int - oldFactionIndex = - gameSettings.getFactionIndexForStartLocation - (switchSlotIdx); - int newFactionIndex = - gameSettings.getFactionIndexForStartLocation (newSlotIdx); - - //printf("Switching faction for index %d [%d] to %d\n",newSlotIdx,switchSlotIdx,gameSettings.getFactionControl(newFactionIndex)); - - gameSettings.setNetworkPlayerName (oldFactionIndex, ""); - serverInterface-> - gameSettings.setNetworkPlayerName (oldFactionIndex, ""); - gameSettings.setNetworkPlayerUUID (oldFactionIndex, ""); - serverInterface-> - gameSettings.setNetworkPlayerUUID (oldFactionIndex, ""); - - gameSettings.setNetworkPlayerPlatform (oldFactionIndex, ""); - serverInterface-> - gameSettings.setNetworkPlayerPlatform (oldFactionIndex, ""); - - gameSettings.setFactionControl (newFactionIndex, ctNetwork); - serverInterface-> - gameSettings.setFactionControl (newFactionIndex, ctNetwork); - - //printf("#1a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,gameSettings.getFactionControl(newFactionIndex),gameSettings.getStartLocationIndex(newFactionIndex)); - //printf("#2a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,serverInterface->gameSettings.getFactionControl(newFactionIndex),serverInterface->gameSettings.getStartLocationIndex(newFactionIndex)); - - try - { - //if(switchSetupRequests[i]->getSelectedFactionName() != "") { - // listBoxFactions[newFactionIdx].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); - //} - //if(switchSetupRequests[i]->getToTeam() != -1) { - // listBoxTeams[newFactionIdx].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); - //} - if (switchSetupRequests[i]->getNetworkPlayerName () != "") - { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,labelPlayerNames[newFactionIdx].getText().c_str(),switchSetupRequests[i]->getNetworkPlayerName().c_str()); - gameSettings.setNetworkPlayerName (newFactionIndex, - switchSetupRequests - [i]->getNetworkPlayerName - ()); - serverInterface-> - gameSettings.setNetworkPlayerName (newFactionIndex, - switchSetupRequests - [i]->getNetworkPlayerName - ()); - } - -// if(gameSettings.getFactionControl(switchFactionIdx) == ctNetworkUnassigned) { -// serverInterface->removeSlot(switchFactionIdx); -// //listBoxControls[switchFactionIdx].setSelectedItemIndex(ctClosed); -// gameSettings.getFactionControl(switchFactionIdx) -// -// labelPlayers[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); -// labelPlayerNames[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); -// listBoxControls[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); -// listBoxFactions[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); -// listBoxTeams[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); -// labelNetStatus[switchSlotIdx].setVisible(switchSlotIdx+1 <= mapInfo.players); -// } - } - catch (const runtime_error & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - e.what ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] caught exception error = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - e.what ()); - } - } - //printf("AFTER switchSlot returned\n"); - } - else - { - try - { - //if(switchSetupRequests[i]->getSelectedFactionName() != "") { - // listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); - //} - //if(switchSetupRequests[i]->getToTeam() != -1) { - // listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); - //} - - if ((switchSetupRequests[i]->getSwitchFlags () & - ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) - { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,switchSetupRequests[i]->getSwitchFlags(),switchSetupRequests[i]->getNetworkPlayerName().c_str(),labelPlayerNames[i].getText().c_str()); - - if (switchSetupRequests[i]->getNetworkPlayerName () != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - //labelPlayerNames[i].setText(switchSetupRequests[i]->getNetworkPlayerName()); - gameSettings.setNetworkPlayerName (i, - switchSetupRequests - [i]->getNetworkPlayerName - ()); - serverInterface->gameSettings.setNetworkPlayerName (i, - switchSetupRequests - [i]->getNetworkPlayerName - ()); - switchRequested = true; - } - else - { - //labelPlayerNames[i].setText(""); - gameSettings.setNetworkPlayerName (i, ""); - serverInterface->gameSettings.setNetworkPlayerName (i, - ""); - switchRequested = true; - } - //SetActivePlayerNameEditor(); - //switchSetupRequests[i]->clearSwitchFlag(ssrft_NetworkPlayerName); - } - } - catch (const runtime_error & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, e.what ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] caught exception error = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, e.what ()); - } - } - } - - delete switchSetupRequests[i]; - switchSetupRequests[i] = NULL; - } - } - - return switchRequested; - } - - void Game::updateNetworkMarkedCells () - { - try - { - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - if (gameNetworkInterface != NULL && - gameNetworkInterface->getMarkedCellList (false).empty () == false) - { - - std::vector < MarkedCell > chatList = - gameNetworkInterface->getMarkedCellList (true); - for (int idx = 0; idx < (int) chatList.size (); idx++) - { - MarkedCell mc = chatList[idx]; - if (mc.getFactionIndex () >= 0) - { - mc. - setFaction ((const Faction *) - world.getFaction (mc.getFactionIndex ())); - } - - Map *map = world.getMap (); - Vec2i surfaceCellPos = map->toSurfCoords (mc.getTargetPos ()); - mapMarkedCellList[surfaceCellPos] = mc; - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - void Game::updateNetworkUnMarkedCells () - { - try - { - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - if (gameNetworkInterface != NULL && - gameNetworkInterface->getUnMarkedCellList (false).empty () == - false) - { - //Lang &lang= Lang::getInstance(); - - std::vector < UnMarkedCell > chatList = - gameNetworkInterface->getUnMarkedCellList (true); - for (int idx = 0; idx < (int) chatList.size (); idx++) - { - UnMarkedCell mc = chatList[idx]; - mc. - setFaction ((const Faction *) - world.getFaction (mc.getFactionIndex ())); - - Map *map = world.getMap (); - Vec2i surfaceCellPos = map->toSurfCoords (mc.getTargetPos ()); - mapMarkedCellList.erase (surfaceCellPos); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - - void Game::updateNetworkHighligtedCells () - { - try - { - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - //update the current entries - for (int idx = (int)highlightedCells.size () - 1; idx >= 0; idx--) - { - MarkedCell *mc = &highlightedCells[idx]; - mc->decrementAliveCount (); - if (mc->getAliveCount () < 0) - { - highlightedCells.erase (highlightedCells.begin () + idx); - } - } - - if (gameNetworkInterface != NULL && - gameNetworkInterface->getHighlightedCellList (false).empty () == - false) - { - //Lang &lang= Lang::getInstance(); - std::vector < MarkedCell > highlighList = - gameNetworkInterface->getHighlightedCellList (true); - for (int idx = 0; idx < (int) highlighList.size (); idx++) - { - MarkedCell mc = highlighList[idx]; // I want a copy here - if (mc.getFactionIndex () >= 0) - { - mc.setFaction ((const Faction *) world.getFaction (mc.getFactionIndex ())); // set faction pointer - } - addOrReplaceInHighlightedCells (mc); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - void Game::addOrReplaceInHighlightedCells (MarkedCell mc) - { - if (mc.getFactionIndex () >= 0) - { - for (int i = (int)highlightedCells.size () - 1; i >= 0; i--) - { - MarkedCell *currentMc = &highlightedCells[i]; - if (currentMc->getFactionIndex () == mc.getFactionIndex ()) - { - highlightedCells.erase (highlightedCells.begin () + i); - } - } - } - if (mc.getAliveCount () <= 0) - { - mc.setAliveCount (200); - } - highlightedCells.push_back (mc); - - if (this->masterserverMode == false) - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - const Faction *faction = mc.getFaction (); - if (getWorld ()->getThisFaction () == NULL) - { - throw - megaglest_runtime_error ("getWorld()->getThisFaction() == NULL"); - } - //printf("faction [%p][%s]\n",faction,(faction != NULL ? faction->getType()->getName().c_str() : "")); - if ((faction == NULL) || - (faction->getTeam () == - getWorld ()->getThisFaction ()->getTeam ())) - { - static PlaySoundClip snd; - soundRenderer.playFx (snd.getSound (snd.sfxMarker), true); - } - } - } - - void - Game::ReplaceDisconnectedNetworkPlayersWithAI (bool isNetworkGame, - NetworkRole role) - { - if (role == nrServer && isNetworkGame == true && - difftime ((long int) time (NULL), - lastNetworkPlayerConnectionCheck) >= - NETWORK_PLAYER_CONNECTION_CHECK_SECONDS) - { - lastNetworkPlayerConnectionCheck = time (NULL); - Logger & logger = Logger::getInstance (); - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (); - - bool newAIPlayerCreated = false; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - if (faction->getFactionDisconnectHandled () == false && - (faction->getControlType () == ctNetwork || - faction->getControlType () == ctNetworkCpuEasy || - faction->getControlType () == ctNetworkCpu || - faction->getControlType () == ctNetworkCpuUltra || - faction->getControlType () == ctNetworkCpuZeta)) - { - - if (aiInterfaces[i] == NULL && - server-> - isClientConnected (faction->getStartLocationIndex ()) == - false) - { - - if (faction->getPersonalityType () != fpt_Observer) - { - DumpCRCWorldLogIfRequired ("_faction_" + intToStr (i)); - } - - faction->setFactionDisconnectHandled (true); - - Lang & lang = Lang::getInstance (); - - bool isPlayerObserver = false; - char szBuf[8096] = ""; - if (faction->getPersonalityType () != fpt_Observer) - { - aiInterfaces[i] = - new AiInterface (*this, i, faction->getTeam (), - faction->getStartLocationIndex ()); - - snprintf (szBuf, 8096, - Lang::getInstance ().getString - ("LogScreenGameLoadingCreatingAIFaction", - "", true).c_str (), i); - logger.add (szBuf, true); - - commander.tryNetworkPlayerDisconnected (i); - - newAIPlayerCreated = true; - } - else - { - isPlayerObserver = true; - - } - - const - vector < - string > - languageList = - this->gameSettings.getUniqueNetworkPlayerLanguages (); - for (unsigned int j = 0; - j < (unsigned int) languageList.size (); ++j) - { - if (isPlayerObserver == false) - { - string - msg = - "Player #%d [%s] has disconnected, switching player to AI mode!"; - if (lang.hasString ("GameSwitchPlayerToAI", - languageList[j], true)) - { - msg = - lang.getString ("GameSwitchPlayerToAI", - languageList[j], true); - } - snprintf (szBuf, 8096, msg.c_str (), i + 1, - this->gameSettings.getNetworkPlayerName (i). - c_str ()); - } - else - { - string - msg = - "Player #%d [%s] has disconnected, but player was only an observer!"; - if (lang.hasString ("GameSwitchPlayerObserverToAI", - languageList[j], true)) - { - msg = - lang.getString ("GameSwitchPlayerObserverToAI", - languageList[j], true); - } - snprintf (szBuf, 8096, msg.c_str (), i + 1, - this->gameSettings.getNetworkPlayerName (i). - c_str ()); - } - bool localEcho = (languageList[j] == lang.getLanguage ()); - server->sendTextMessage (szBuf, -1, localEcho, - languageList[j]); - } - } - } - } - - if (newAIPlayerCreated == true - && Config::getInstance ().getBool ("EnableNewThreadManager", - "false") == true) - { - bool - enableServerControlledAI = - this->gameSettings.getEnableServerControlledAI (); - - masterController.clearSlaves (true); - - std::vector < SlaveThreadControllerInterface * >slaveThreadList; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - if (faction->getCpuControl - (enableServerControlledAI, isNetworkGame, role) == true) - { - slaveThreadList.push_back (aiInterfaces[i]->getWorkerThread ()); - } - } - masterController.setSlaves (slaveThreadList); - } - } - } - - void Game::updateCamera () - { - if (currentUIState != NULL) - { - currentUIState->updateCamera (); - return; - } - gameCamera.update (); - } - - -// ==================== render ==================== - -//render - void Game::render () - { - // Ensure the camera starts in the right position - if (isFirstRender == true) - { - isFirstRender = false; - - if (this->loadGameNode == NULL) - { - gameCamera.setState (GameCamera::sGame); - this->restoreToStartXY (); - } - } - - canRender (); - incrementFps (); - - renderFps++; - totalRenderFps++; - - updateWorldStats (); - - //NetworkManager &networkManager= NetworkManager::getInstance(); - if (this->masterserverMode == false) - { - renderWorker (); - } - else - { - // Titi, uncomment this to watch the game on the masterserver - //renderWorker(); - - // In masterserver mode quit game if no network players left - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (); - int connectedClients = 0; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - if (server->isClientConnected (faction->getStartLocationIndex ()) == - true) - { - connectedClients++; - } - } - - if (connectedClients == 0) - { - quitTriggeredIndicator = true; - } - else - { - string str = ""; - std::map < int, string > factionDebugInfo; - - if (difftime - ((long int) time (NULL), - lastMasterServerGameStatsDump) >= GAME_STATS_DUMP_INTERVAL) - { - lastMasterServerGameStatsDump = time (NULL); - str = getDebugStats (factionDebugInfo); - - printf ("== Current in-game stats (interval %d) ==\n%s\n", - GAME_STATS_DUMP_INTERVAL, str.c_str ()); - } - } - } - } - - void Game::renderWorker () - { - if (currentUIState != NULL) - { -// Renderer &renderer= Renderer::getInstance(); -// renderer.clearBuffers(); -// -// //3d -// renderer.reset3dMenu(); -// -// renderer.clearZBuffer(); -// //renderer.loadCameraMatrix(menuBackground.getCamera()); -// //renderer.renderMenuBackground(&menuBackground); -// renderer.renderParticleManager(rsMenu); -// -// //2d -// renderer.reset2d(); -// -// currentUIState->render(); -// -// if(renderer.isMasterserverMode() == false) { -// renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); -// renderer.renderFPSWhenEnabled(lastFps); -// renderer.swapBuffers(); -// } - - currentUIState->render (); - return; - } - else - { - Renderer & renderer = Renderer::getInstance (); - if (renderer.getCustom3dMenu () != NULL) - { - renderer.setCustom3dMenu (NULL); - } - } - - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - - render3d (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %d [render3d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - render2d (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %d [render2d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - Renderer::getInstance ().swapBuffers (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %d [swap buffers]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - } - -// ==================== tick ==================== - - void Game::removeUnitFromSelection (const Unit * unit) - { - try - { - Selection *selection = gui.getSelectionPtr (); - for (int i = 0; i < selection->getCount (); ++i) - { - const Unit *currentUnit = selection->getUnit (i); - if (currentUnit == unit) - { - selection->unSelect (i); - break; - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - - //abort(); - } - } - - bool Game::addUnitToSelection (Unit * unit) - { - bool result = false; - try - { - Selection *selection = gui.getSelectionPtr (); - if (selection != NULL) - { - result = selection->select (unit, true); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - - //abort(); - } - - return result; - } - - void Game::addUnitToGroupSelection (Unit * unit, int groupIndex) - { - try - { - Selection *selection = gui.getSelectionPtr (); - if (selection != NULL) - { - selection->addUnitToGroup (groupIndex, unit); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - - //abort(); - } - } - - void Game::removeUnitFromGroupSelection (int unitId, int groupIndex) - { - try - { - Selection *selection = gui.getSelectionPtr (); - if (selection != NULL) - { - selection->removeUnitFromGroup (groupIndex, unitId); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - - //abort(); - } - } - - void Game::recallGroupSelection (int groupIndex) - { - try - { - Selection *selection = gui.getSelectionPtr (); - if (selection != NULL) - { - selection->recallGroup (groupIndex); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - if (errorMessageBox.getEnabled () == false) - { - ErrorDisplayMessage (ex.what (), true); - } - - //abort(); - } - } - - void Game::tick () - { - ProgramState::tick (); - - tickCount++; - - if (avgUpdateFps == -1) - { - avgUpdateFps = updateFps; - } - else - { - avgUpdateFps = (avgUpdateFps + updateFps) / 2; - } - currentAvgRenderFpsTotal += renderFps; - - if (avgRenderFps == -1) - { - avgRenderFps = renderFps; - } - // Update the average every x game ticks - const int CHECK_AVG_FPS_EVERY_X_TICKS = 15; - if (tickCount % CHECK_AVG_FPS_EVERY_X_TICKS == 0) - { - avgRenderFps = currentAvgRenderFpsTotal / CHECK_AVG_FPS_EVERY_X_TICKS; - currentAvgRenderFpsTotal = 0; - } - - if (captureAvgTestStatus == true) - { - if (updateFpsAvgTest == -1) - { - updateFpsAvgTest = updateFps; - } - else - { - updateFpsAvgTest = (updateFpsAvgTest + updateFps) / 2; - } - if (renderFpsAvgTest == -1) - { - renderFpsAvgTest = renderFps; - } - else - { - renderFpsAvgTest = (renderFpsAvgTest + renderFps) / 2; - } - } - - lastUpdateFps = updateFps; - lastRenderFps = renderFps; - updateFps = 0; - renderFps = 0; - - //Win/lose check - checkWinner (); - gui.tick (); - } - - -// ==================== events ==================== - - int Game::getFirstUnusedTeamNumber () - { - std::map < int, bool > uniqueTeamNumbersUsed; - for (unsigned int i = 0; i < (unsigned int) world.getFactionCount (); - ++i) - { - Faction *faction = world.getFaction (i); - uniqueTeamNumbersUsed[faction->getTeam ()] = true; - } - - int result = -1; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (uniqueTeamNumbersUsed.find (i) == uniqueTeamNumbersUsed.end ()) - { - result = i; - break; - } - } - - return result; - } - - void Game::setupRenderForVideo () - { - Renderer & renderer = Renderer::getInstance (); - //renderer.clearBuffers(); - //3d - //renderer.reset3dMenu(); - //renderer.clearZBuffer(); - //2d - //renderer.reset2d(); - renderer.setupRenderForVideo (); - } - - void Game::tryPauseToggle (bool pauseValue) - { - bool allowAdminMenuItems = false; - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - if (role == nrServer) - { - allowAdminMenuItems = true; - } - else if (role == nrClient) - { - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - - if (clientInterface != NULL && - gameSettings.getMasterserver_admin () == - clientInterface->getSessionKey ()) - { - allowAdminMenuItems = true; - } - } - - bool isNetworkGame = this->gameSettings.isNetworkGame (); - //printf("Try Pause allowAdminMenuItems = %d, pauseValue = %d\n",allowAdminMenuItems,pauseValue); - - if (allowAdminMenuItems) - { - if (pauseValue == true) - { - commander.tryPauseGame (false, false); - } - else - { - if (isNetworkGame == false) - { - setPaused (pauseValue, true, false, false); - } - else - { - commander.tryResumeGame (false, false); - } - } - } - } - - void Game::startMarkCell () - { - int totalMarkedCellsForPlayer = 0; - if (world.getThisFaction () != NULL) - { - for (std::map < Vec2i, MarkedCell >::iterator iterMap = - mapMarkedCellList.begin (); - iterMap != mapMarkedCellList.end (); ++iterMap) - { - MarkedCell & bm = iterMap->second; - if (bm.getPlayerIndex () == - world.getThisFaction ()->getStartLocationIndex ()) - { - totalMarkedCellsForPlayer++; - } - } - } - - const int MAX_MARKER_COUNT = 5; - if (totalMarkedCellsForPlayer < MAX_MARKER_COUNT) - { - isMarkCellEnabled = true; - } - else - { - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("MaxMarkerCount") + " " + - intToStr (MAX_MARKER_COUNT)); - } - } - - void Game::processInputText (string text, bool cancelled) - { - isMarkCellTextEnabled = false; - - if (cancelled == false) - { - //printf("Note [%s]\n",text.c_str()); - - cellMarkedData.setNote (text); - addCellMarker (cellMarkedPos, cellMarkedData); - -// if(text.find("\\n") != text.npos) { -// replaceAll(text, "\\n", "\n"); -// } -// if(text.find("\\t") != text.npos) { -// replaceAll(text, "\\t", "\t"); -// } -// -// cellMarkedData.setNote(text); -// mapMarkedCellList[cellMarkedPos] = cellMarkedData; -// -// GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); -// -// int factionIndex = -1; -// int playerIndex = -1; -// if(cellMarkedData.getFaction() != NULL) { -// factionIndex = cellMarkedData.getFaction()->getIndex(); -// playerIndex = cellMarkedData.getFaction()->getStartLocationIndex(); -// } -// gameNetworkInterface->sendMarkCellMessage( -// cellMarkedData.getTargetPos(), -// factionIndex, -// cellMarkedData.getNote(), -// playerIndex); -// -// Renderer &renderer= Renderer::getInstance(); -// renderer.forceQuadCacheUpdate(); - } - } - - void Game::addCellMarker (Vec2i cellPos, MarkedCell cellData) - { - //printf("Note [%s]\n",text.c_str()); - - string text = cellData.getNote (); - if (text.find ("\\n") != text.npos) - { - replaceAll (text, "\\n", "\n"); - } - if (text.find ("\\t") != text.npos) - { - replaceAll (text, "\\t", "\t"); - } - - cellData.setNote (text); - mapMarkedCellList[cellPos] = cellData; - - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - int factionIndex = -1; - int playerIndex = -1; - if (cellData.getFaction () != NULL) - { - factionIndex = cellData.getFaction ()->getIndex (); - playerIndex = cellData.getFaction ()->getStartLocationIndex (); - } - - //printf("Adding Cell marker pos [%s] factionIndex [%d] note [%s] playerIndex = %d\n",cellData.getTargetPos().getString().c_str(),factionIndex,cellData.getNote().c_str(),playerIndex); - - gameNetworkInterface->sendMarkCellMessage (cellData.getTargetPos (), - factionIndex, - cellData.getNote (), - playerIndex); - - Renderer & renderer = Renderer::getInstance (); - renderer.forceQuadCacheUpdate (); - } - - void - Game::removeCellMarker (Vec2i surfaceCellPos, const Faction * faction) - { - //Vec2i surfaceCellPos = map->toSurfCoords(Vec2i(xCell,yCell)); - Map *map = world.getMap (); - SurfaceCell *sc = map->getSurfaceCell (surfaceCellPos); - Vec3f vertex = sc->getVertex (); - Vec2i targetPos (vertex.x, vertex.z); - - //printf("Remove Cell marker lookup pos [%s] factionIndex [%d]\n",surfaceCellPos.getString().c_str(),(faction != NULL ? faction->getIndex() : -1)); - - if (mapMarkedCellList.find (surfaceCellPos) != mapMarkedCellList.end ()) - { - MarkedCell mc = mapMarkedCellList[surfaceCellPos]; - if (mc.getFaction () == faction) - { - mapMarkedCellList.erase (surfaceCellPos); - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - - int factionIndex = (faction != NULL ? faction->getIndex () : -1); - - //printf("Remvoing Cell marker pos [%s] factionIndex [%d] note [%s]\n",mc.getTargetPos().getString().c_str(),factionIndex,mc.getNote().c_str()); - - gameNetworkInterface->sendUnMarkCellMessage (mc.getTargetPos (), - factionIndex); - } - } - //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); - - //isUnMarkCellEnabled = false; - - Renderer & renderer = Renderer::getInstance (); - //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); - renderer.forceQuadCacheUpdate (); - } - void Game::showMarker (Vec2i cellPos, MarkedCell cellData) - { - //setMarker = true; - //if(setMarker) { - //Vec2i targetPos = cellData.targetPos; - //Vec2i screenPos(x,y-60); - //Renderer &renderer= Renderer::getInstance(); - //renderer.computePosition(screenPos, targetPos); - //Vec2i surfaceCellPos = map->toSurfCoords(targetPos); - - //MarkedCell mc(targetPos,world.getThisFaction(),"none",world.getThisFaction()->getStartLocationIndex()); - addOrReplaceInHighlightedCells (cellData); - - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - gameNetworkInterface-> - sendHighlightCellMessage (cellData.getTargetPos (), - cellData.getFactionIndex ()); - //} - } - - void Game::mouseDownLeft (int x, int y) - { - if (this->masterserverMode == true) - { - return; - } - cameraDragAllowed = false; - if (currentUIState != NULL) - { - currentUIState->mouseDownLeft (x, y); - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - Logger::getInstance ().handleMouseClick (x, y); - return; - } - - Map *map = world.getMap (); - const Metrics & metrics = Metrics::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - bool messageBoxClick = false; - bool originalIsMarkCellEnabled = isMarkCellEnabled; - bool originalIsUnMarkCellEnabled = isUnMarkCellEnabled; - - if (popupMenu.mouseClick (x, y)) - { - std::pair < int, - string > result = popupMenu.mouseClickedMenuItem (x, y); - //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); - - //printf("popupMenu.mouseClick == true result.first = %d disconnectPlayerPopupMenuIndex = %d\n",result.first,disconnectPlayerPopupMenuIndex); - - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - - // Exit game - if (result.first == exitGamePopupMenuIndex) - { - showMessageBox (Lang:: - getInstance ().getString ("ExitBattleQuestion"), - "", true); - } - else if (result.first == joinTeamPopupMenuIndex) - { - - Lang & lang = Lang::getInstance (); - switchTeamIndexMap.clear (); - std::map < int, bool > uniqueTeamNumbersUsed; - std::vector < string > menuItems; - for (unsigned int i = 0; - i < (unsigned int) world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - if (faction->getPersonalityType () != fpt_Observer && - uniqueTeamNumbersUsed.find (faction->getTeam ()) == - uniqueTeamNumbersUsed.end ()) - { - uniqueTeamNumbersUsed[faction->getTeam ()] = true; - } - - if (faction->getPersonalityType () != fpt_Observer && - world.getThisFaction ()->getIndex () != - faction->getIndex () - && world.getThisFaction ()->getTeam () != - faction->getTeam ()) - { - char szBuf[8096] = ""; - if (lang.hasString ("JoinPlayerTeam") == true) - { - snprintf (szBuf, 8096, - (" " + - lang.getString ("JoinPlayerTeam") + - " ").c_str (), faction->getIndex (), - this->gameSettings.getNetworkPlayerName (i). - c_str (), faction->getTeam ()); - } - else - { - snprintf (szBuf, 8096, - " Join player #%d - %s on Team: %d ", - faction->getIndex (), - this->gameSettings.getNetworkPlayerName (i). - c_str (), faction->getTeam ()); - } - - menuItems.push_back (szBuf); - - switchTeamIndexMap[(int) menuItems.size () - 1] = - faction->getTeam (); - } - } - - if ((int) uniqueTeamNumbersUsed.size () < 8) - { - menuItems.push_back (" " + - lang.getString ("CreateNewTeam") + " "); - switchTeamIndexMap[(int) menuItems.size () - 1] = - CREATE_NEW_TEAM; - } - menuItems.push_back (" " + lang.getString ("Cancel") + " "); - switchTeamIndexMap[(int) menuItems.size () - 1] = - CANCEL_SWITCH_TEAM; - - popupMenuSwitchTeams.setW (100); - popupMenuSwitchTeams.setH (100); - popupMenuSwitchTeams.init (" " + - lang.getString ("SwitchTeams") + - " ", menuItems); - popupMenuSwitchTeams.setEnabled (true); - popupMenuSwitchTeams.setVisible (true); - } - else if (result.first == disconnectPlayerPopupMenuIndex) - { - Lang & lang = Lang::getInstance (); - - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - ServerInterface *serverInterface = NULL; - if (role == nrServer) - { - serverInterface = - dynamic_cast < - ServerInterface * >(networkManager.getServerInterface ()); - } - disconnectPlayerIndexMap.clear (); - std::vector < string > menuItems; - for (unsigned int i = 0; - i < (unsigned int) world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - //printf("faction->getPersonalityType() = %d index [%d,%d] control [%d] networkstatus [%d]\n",faction->getPersonalityType(),world.getThisFaction()->getIndex(),faction->getIndex(),faction->getControlType(),this->gameSettings.getNetworkPlayerStatuses(i)); - - bool isSlotJoinInProgressClient = false; - if (serverInterface != NULL) - { - - MutexSafeWrapper - safeMutex (serverInterface->getSlotMutex - (faction->getStartLocationIndex ()), - CODE_AT_LINE); - ConnectionSlot *slot = - serverInterface->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL - && slot->getConnectHasHandshaked () == true - && slot->getCurrentFrameCount () <= 0) - { - isSlotJoinInProgressClient = true; - } - } - - //printf("isSlotJoinInProgressClient: %d [%d] [%d][%d] [%d] [%d] [%d]\n", - // isSlotJoinInProgressClient,faction->getPersonalityType(),faction->getIndex(),world.getThisFaction()->getIndex(),faction->getControlType(),this->gameSettings.getNetworkPlayerStatuses(i),i); - - if (isSlotJoinInProgressClient == true || - (faction->getPersonalityType () != fpt_Observer && - world.getThisFaction ()->getIndex () != - faction->getIndex () - && faction->getControlType () == ctNetwork - && this->gameSettings.getNetworkPlayerStatuses (i) != - npst_Disconnected)) - { - - char szBuf[8096] = ""; - if (lang.hasString ("DisconnectNetorkPlayerIndex") == true) - { - snprintf (szBuf, 8096, - (" " + - lang.getString - ("DisconnectNetorkPlayerIndex") + - " ").c_str (), - faction->getIndex () + 1, - this->gameSettings.getNetworkPlayerName (i). - c_str ()); - } - else - { - snprintf (szBuf, 8096, - " Disconnect player #%d - %s: ", - faction->getIndex () + 1, - this->gameSettings.getNetworkPlayerName (i). - c_str ()); - } - - menuItems.push_back (szBuf); - - //disconnectPlayerIndexMap[menuItems.size()-1] = faction->getStartLocationIndex(); - disconnectPlayerIndexMap[(int) menuItems.size () - - 1] = faction->getIndex (); - } - } - - menuItems.push_back (" " + lang.getString ("Cancel") + " "); - disconnectPlayerIndexMap[(int) menuItems.size () - 1] = - CANCEL_DISCONNECT_PLAYER; - - popupMenuDisconnectPlayer.setW (100); - popupMenuDisconnectPlayer.setH (100); - popupMenuDisconnectPlayer.init (" " + - lang.getString - ("DisconnectNetorkPlayer") + - " ", menuItems); - popupMenuDisconnectPlayer.setEnabled (true); - popupMenuDisconnectPlayer.setVisible (true); - } - else if (result.first == keyboardSetupPopupMenuIndex) - { - MainMenu *newMenu = new MainMenu (program); // open keyboard shortcuts setup screen - currentUIState = newMenu; - Renderer & renderer = Renderer::getInstance (); - renderer.setCustom3dMenu (newMenu); - //currentUIState->load(); - currentUIState->init (); - - // open keyboard shortcuts setup screen - newMenu->setState (new - MenuStateKeysetup (program, newMenu, - ¤tUIState)); - } - else if (result.first == pauseGamePopupMenuIndex) - { - //this->setPaused(!paused); - //printf("popup paused = %d\n",paused); - - bool allowAdminMenuItems = false; - NetworkRole role = networkManager.getNetworkRole (); - if (role == nrServer) - { - allowAdminMenuItems = true; - } - else if (role == nrClient) - { - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - - if (clientInterface != NULL && - gameSettings.getMasterserver_admin () == - clientInterface->getSessionKey ()) - { - allowAdminMenuItems = true; - } - } - - if (allowAdminMenuItems) - { - if (getPaused () == false) - { - commander.tryPauseGame (false, false); - } - else - { - commander.tryResumeGame (false, false); - } - } - } - else if (result.first == saveGamePopupMenuIndex) - { - saveGame (); - } - //else if(result.first == markCellPopupMenuIndex) { - // startMarkCell(); - //} - //else if(result.first == unmarkCellPopupMenuIndex) { - // isUnMarkCellEnabled = true; - //} - } - else if (popupMenuSwitchTeams.mouseClick (x, y)) - { - //popupMenuSwitchTeams - std::pair < int, - string > - result = popupMenuSwitchTeams.mouseClickedMenuItem (x, y); - //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); - - popupMenuSwitchTeams.setEnabled (false); - popupMenuSwitchTeams.setVisible (false); - - //bool isNetworkGame = this->gameSettings.isNetworkGame(); - - int teamIndex = switchTeamIndexMap[result.first]; - switch (teamIndex) - { - case CREATE_NEW_TEAM: - { - int newTeam = getFirstUnusedTeamNumber (); - //if(isNetworkGame == true) { - const Faction *faction = world.getThisFaction (); - commander.trySwitchTeam (faction, newTeam); - //} - //else { - // const Faction *faction = world.getThisFaction(); - // commander.trySwitchTeam(faction,newTeam); - //} - } - break; - case CANCEL_SWITCH_TEAM: - break; - default: - //if(isNetworkGame == true) { - const Faction *faction = world.getThisFaction (); - commander.trySwitchTeam (faction, teamIndex); - //} - //else { - // const Faction *faction = world.getThisFaction(); - // commander.trySwitchTeam(faction,teamIndex); - //} - - break; - } - } - else if (popupMenuDisconnectPlayer.mouseClick (x, y)) - { - //popupMenuSwitchTeams - std::pair < int, - string > - result = popupMenuDisconnectPlayer.mouseClickedMenuItem (x, y); - //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); - - popupMenuDisconnectPlayer.setEnabled (false); - popupMenuDisconnectPlayer.setVisible (false); - - //bool isNetworkGame = this->gameSettings.isNetworkGame(); - - //int playerIndex = disconnectPlayerIndexMap[result.first]; - int factionIndex = disconnectPlayerIndexMap[result.first]; - switch (factionIndex) - { - case CANCEL_DISCONNECT_PLAYER: - break; - default: -// if(isNetworkGame == true) { -// const Faction *faction = world.getThisFaction(); -// commander.trySwitchTeam(faction,teamIndex); -// } -// else { -// const Faction *faction = world.getThisFaction(); -// commander.trySwitchTeam(faction,teamIndex); -// } - - - GameSettings * settings = world.getGameSettingsPtr (); - Lang & lang = Lang::getInstance (); - - char szBuf[8096] = ""; - if (lang.hasString ("DisconnectNetorkPlayerIndexConfirm") == true) - { - snprintf (szBuf, 8096, - (" " + - lang.getString - ("DisconnectNetorkPlayerIndexConfirm") + - " ").c_str (), factionIndex + 1, - settings-> - getNetworkPlayerName (factionIndex).c_str ()); - } - else - { - snprintf (szBuf, 8096, - " Confirm disconnection for player #%d - %s? ", - factionIndex + 1, - settings-> - getNetworkPlayerName (factionIndex).c_str ()); - } - - disconnectPlayerConfirmMessageBox.setText (szBuf); - disconnectPlayerConfirmMessageBox.init (lang.getString ("Yes"), - lang.getString ("No")); - disconnectPlayerConfirmMessageBox.setEnabled (true); - - playerIndexDisconnect = - world.getFaction (factionIndex)->getStartLocationIndex (); - - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - if (gameNetworkInterface != NULL) - { - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = settings->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; - i < (unsigned int) languageList.size (); ++i) - { - char szMsg[8096] = ""; - if (lang.hasString ("DisconnectNetorkPlayerIndexConfirmed", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("DisconnectNetorkPlayerIndexConfirmed", - languageList[i]).c_str (), - factionIndex + 1, - settings->getNetworkPlayerName - (factionIndex).c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Notice - Admin is warning to disconnect player #%d - %s!", - factionIndex + 1, - settings->getNetworkPlayerName - (factionIndex).c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - gameNetworkInterface->sendTextMessage (szMsg, -1, - localEcho, - languageList[i]); - } - - sleep (10); - } - - break; - } - } - - if (switchTeamConfirmMessageBox.getEnabled () == true) - { - int button = -1; - if (switchTeamConfirmMessageBox.mouseClick (x, y, button)) - { - switchTeamConfirmMessageBox.setEnabled (false); - - SwitchTeamVote *vote = - world.getThisFactionPtr ()-> - getSwitchTeamVote (world.getThisFaction - ()->getCurrentSwitchTeamVoteFactionIndex ()); - vote->voted = true; - vote->allowSwitchTeam = (button == 0); - - const Faction *faction = world.getThisFaction (); - commander.trySwitchTeamVote (faction, vote); - } - } - else if (disconnectPlayerConfirmMessageBox.getEnabled () == true) - { - int button = -1; - if (disconnectPlayerConfirmMessageBox.mouseClick (x, y, button)) - { - disconnectPlayerConfirmMessageBox.setEnabled (false); - - if (button == 0) - { - const Faction *faction = world.getThisFaction (); - commander.tryDisconnectNetworkPlayer (faction, - playerIndexDisconnect); - } - } - } - - //scrip message box, only if the exit box is not enabled - if (mainMessageBox.getEnabled () == false && - errorMessageBox.getEnabled () == false && - scriptManager.getMessageBox ()->getEnabled ()) - { - int button = 0; - if (scriptManager.getMessageBox ()->mouseClick (x, y, button)) - { - scriptManager.onMessageBoxOk (); - messageBoxClick = true; - } - } - - //minimap panel - if (messageBoxClick == false) - { - if (metrics.isInMinimap (x, y)) - { - int xm = x - metrics.getMinimapX (); - int ym = y - metrics.getMinimapY (); - int - xCell = - static_cast < - int >(xm * - (static_cast < - float >(map->getW ()) / metrics.getMinimapW ())); - int - yCell = - static_cast < - int >(map->getH () - - ym * (static_cast < - float >(map->getH ()) / metrics.getMinimapH ())); - - if (map->isInside (xCell, yCell) - && map->isInsideSurface (map->toSurfCoords (Vec2i - (xCell, yCell)))) - { - if (gui.isSelectingPos ()) - { - gui.mouseDownLeftGraphics (xCell, yCell, true); - } - else - { - if (!setMarker) - { - cameraDragAllowed = true; - gameCamera.setPos (Vec2f - (static_cast < float >(xCell), - static_cast < float >(yCell))); - } - } - - if (setMarker) - { - Vec2i - surfaceCellPos = map->toSurfCoords (Vec2i (xCell, yCell)); - SurfaceCell *sc = map->getSurfaceCell (surfaceCellPos); - Vec3f vertex = sc->getVertex (); - Vec2i targetPos (vertex.x, vertex.z); - - MarkedCell - mc (targetPos, world.getThisFaction (), "none", - world.getThisFaction ()->getStartLocationIndex ()); - addOrReplaceInHighlightedCells (mc); - - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - gameNetworkInterface-> - sendHighlightCellMessage (mc.getTargetPos (), - mc.getFaction ()->getIndex ()); - } - - - if (originalIsMarkCellEnabled == true - && isMarkCellEnabled == true) - { - Vec2i - surfaceCellPos = map->toSurfCoords (Vec2i (xCell, yCell)); - SurfaceCell *sc = map->getSurfaceCell (surfaceCellPos); - Vec3f vertex = sc->getVertex (); - Vec2i targetPos (vertex.x, vertex.z); - - MarkedCell - mc (targetPos, world.getThisFaction (), - "placeholder for note", - world.getThisFaction ()->getStartLocationIndex ()); - - //GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); - //gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); - - //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); - - isMarkCellEnabled = false; - cellMarkedData = mc; - cellMarkedPos = surfaceCellPos; - isMarkCellTextEnabled = true; - chatManager.switchOnEdit (this, 500); - - Renderer & renderer = Renderer::getInstance (); - //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); - renderer.forceQuadCacheUpdate (); - } - if (originalIsUnMarkCellEnabled == true - && isUnMarkCellEnabled == true) - { - Vec2i - surfaceCellPos = map->toSurfCoords (Vec2i (xCell, yCell)); - SurfaceCell *sc = map->getSurfaceCell (surfaceCellPos); - Vec3f vertex = sc->getVertex (); - Vec2i targetPos (vertex.x, vertex.z); - -// if(mapMarkedCellList.find(surfaceCellPos) != mapMarkedCellList.end()) { -// MarkedCell mc = mapMarkedCellList[surfaceCellPos]; -// if(mc.getFaction() == world.getThisFaction()) { -// mapMarkedCellList.erase(surfaceCellPos); -// GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); -// gameNetworkInterface->sendUnMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex()); -// } -// } - - isUnMarkCellEnabled = false; - - removeCellMarker (surfaceCellPos, world.getThisFaction ()); - //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); - - Renderer & renderer = Renderer::getInstance (); - //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); - renderer.forceQuadCacheUpdate (); - } - } - } - //display panel - else if (metrics.isInDisplay (x, y) && !gui.isSelectingPos ()) - { - int xd = x - metrics.getDisplayX (); - int yd = y - metrics.getDisplayY (); - if (gui.mouseValid (xd, yd)) - { - gui.mouseDownLeftDisplay (xd, yd); - } - else - { - gui.mouseDownLeftGraphics (x, y, false); - } - } - //graphics panel - else - { - gui.mouseDownLeftGraphics (x, y, false); - - if (setMarker) - { - Vec2i targetPos; - Vec2i screenPos (x, y - 60); - targetPos = getMouseCellPos (); - //Vec2i surfaceCellPos = map->toSurfCoords(targetPos); - - - MarkedCell - mc (targetPos, world.getThisFaction (), "none", - world.getThisFaction ()->getStartLocationIndex ()); - addOrReplaceInHighlightedCells (mc); - - GameNetworkInterface *gameNetworkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - gameNetworkInterface->sendHighlightCellMessage (mc.getTargetPos - (), - mc.getFaction - ()->getIndex - ()); - } - - if (originalIsMarkCellEnabled == true - && isMarkCellEnabled == true) - { - Vec2i targetPos; - Vec2i screenPos (x, y - 60); - targetPos = getMouseCellPos (); - Vec2i surfaceCellPos = map->toSurfCoords (targetPos); - - MarkedCell - mc (targetPos, world.getThisFaction (), - "placeholder for note", - world.getThisFaction ()->getStartLocationIndex ()); - - //printf("#2 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); - - isMarkCellEnabled = false; - cellMarkedData = mc; - cellMarkedPos = surfaceCellPos; - isMarkCellTextEnabled = true; - chatManager.switchOnEdit (this, 500); - - //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); - Renderer::getInstance ().forceQuadCacheUpdate (); - } - - if (originalIsUnMarkCellEnabled == true - && isUnMarkCellEnabled == true) - { - Vec2i targetPos; - Vec2i screenPos (x, y - 35); - targetPos = getMouseCellPos (); - Vec2i surfaceCellPos = map->toSurfCoords (targetPos); - -// if(mapMarkedCellList.find(surfaceCellPos) != mapMarkedCellList.end()) { -// MarkedCell mc = mapMarkedCellList[surfaceCellPos]; -// if(mc.getFaction() == world.getThisFaction()) { -// mapMarkedCellList.erase(surfaceCellPos); -// GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); -// gameNetworkInterface->sendUnMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex()); -// } -// } - - isUnMarkCellEnabled = false; - removeCellMarker (surfaceCellPos, world.getThisFaction ()); - //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); - - //Renderer &renderer= Renderer::getInstance(); - //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); - Renderer::getInstance ().forceQuadCacheUpdate (); - } - } - } - - //exit message box, has to be the last thing to do in this function - if (errorMessageBox.getEnabled () == true) - { - if (errorMessageBox.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - //close message box - errorMessageBox.setEnabled (false); - } - } - if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - if (button == 0) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - if (networkManager.getGameNetworkInterface () != NULL) - { - networkManager.getGameNetworkInterface ()->quitGame (true); - } - quitTriggeredIndicator = true; - return; - } - else - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - //close message box - mainMessageBox.setEnabled (false); - } - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::mouseDownRight (int x, int y) - { - if (this->masterserverMode == true) - { - return; - } - - if (currentUIState != NULL) - { - currentUIState->mouseDownRight (x, y); - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - Logger::getInstance ().handleMouseClick (x, y); - return; - } - - Map *map = world.getMap (); - const Metrics & metrics = Metrics::getInstance (); - - if (metrics.isInMinimap (x, y)) - { - int xm = x - metrics.getMinimapX (); - int ym = y - metrics.getMinimapY (); - int - xCell = - static_cast < - int >(xm * - (static_cast < - float >(map->getW ()) / metrics.getMinimapW ())); - int - yCell = - static_cast < - int >(map->getH () - - ym * (static_cast < - float >(map->getH ()) / metrics.getMinimapH ())); - - if (map->isInside (xCell, yCell) - && map-> - isInsideSurface (map->toSurfCoords (Vec2i (xCell, yCell)))) - { - gui.mouseDownRightGraphics (xCell, yCell, true); - } - } - else - { - Vec2i targetPos; - Vec2i screenPos (x, y); - targetPos = getMouseCellPos (); - if (isValidMouseCellPos () == true && - map->isInsideSurface (map->toSurfCoords (targetPos)) == true) - { - gui.mouseDownRightGraphics (x, y, false); - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Error [%s] x = %d y = %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what (), x, y); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::mouseUpCenter (int x, int y) - { - if (this->masterserverMode == true) - { - return; - } - - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - - if (currentUIState != NULL) - { - currentUIState->mouseUpCenter (x, y); - return; - } - - if (mouseMoved == false) - { - gameCamera.setState (GameCamera::sGame); - } - else - { - mouseMoved = false; - } - } - - void Game::mouseUpLeft (int x, int y) - { - if (this->masterserverMode == true) - { - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - - if (currentUIState != NULL) - { - currentUIState->mouseUpLeft (x, y); - return; - } - - gui.mouseUpLeftGraphics (x, y); - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::mouseDoubleClickLeft (int x, int y) - { - if (this->masterserverMode == true) - { - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - if (currentUIState != NULL) - { - currentUIState->mouseDoubleClickLeft (x, y); - return; - } - - const Metrics & metrics = Metrics::getInstance (); - - if (metrics.isInMinimap (x, y)) - { - // no double click on minimap - } - else - { - //display panel - if (metrics.isInDisplay (x, y) && !gui.isSelectingPos ()) - { - int xd = x - metrics.getDisplayX (); - int yd = y - metrics.getDisplayY (); - if (gui.mouseValid (xd, yd)) - { - return; - } - } - //graphics panel - gui.mouseDoubleClickLeftGraphics (x, y); - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::mouseMove (int x, int y, const MouseState * ms) - { - if (this->masterserverMode == true) - { - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - if (currentUIState != NULL) - { - currentUIState->mouseMove (x, y, ms); - return; - } - - popupMenu.mouseMove (x, y); - popupMenuSwitchTeams.mouseMove (x, y); - popupMenuDisconnectPlayer.mouseMove (x, y); - - const Metrics & metrics = Metrics::getInstance (); - - mouseX = x; - mouseY = y; - - if (ms->get (mbCenter)) - { - mouseMoved = true; - if (currentCameraFollowUnit == NULL) - { - float ymult = 0.2f; - float xmult = 0.2f; - - Vec2i oldPos =::Shared::Platform::Window::getOldMousePos (); - int - oldx = - (oldPos.x * metrics.getVirtualW () / metrics.getScreenW ()); - int - oldy = - ((metrics.getScreenH () - - oldPos.y) * metrics.getVirtualH () / metrics.getScreenH ()); - lastMousePos.x = oldx; - lastMousePos.y = oldy; - gameCamera.transitionVH (-(y - oldy) * ymult, (oldx - x) * xmult); - } - mouseX = lastMousePos.x; - mouseY = lastMousePos.y; - ::Shared::Platform::Window::revertMousePos (); - - return; - } - else if (currentCameraFollowUnit == NULL) - { - //if(Window::isKeyDown() == false) - if (!camLeftButtonDown && !camRightButtonDown && !camUpButtonDown - && !camDownButtonDown) - { - if (ms->get (mbLeft) && metrics.isInMinimap (x, y)) - { - int xm = x - metrics.getMinimapX (); - int ym = y - metrics.getMinimapY (); - - Map *map = world.getMap (); - int - xCell = - static_cast < - int >(xm * - (static_cast < - float >(map->getW ()) / metrics.getMinimapW ())); - int - yCell = - static_cast < - int >(map->getH () - - ym * (static_cast < - float >(map->getH ()) / metrics.getMinimapH ())); - - if (map->isInside (xCell, yCell) - && map->isInsideSurface (map->toSurfCoords (Vec2i - (xCell, - yCell)))) - { - if (gui.isSelectingPos ()) - { - gui.mouseDownLeftGraphics (xCell, yCell, true); - } - else - { - if (cameraDragAllowed == true) - { - gameCamera.setPos (Vec2f - (static_cast < float >(xCell), - static_cast < float >(yCell))); - } - } - } - } - else - { - bool - mouseMoveScrollsWorld = - Config::getInstance ().getBool ("MouseMoveScrollsWorld", - "true"); - if (mouseMoveScrollsWorld == true) - { - if (y < 10) - { - gameCamera.setMoveZ (-scrollSpeed); - } - else if (y > metrics.getVirtualH () - 10) - { - gameCamera.setMoveZ (scrollSpeed); - } - else - { - gameCamera.setMoveZ (0); - } - - if (x < 10) - { - gameCamera.setMoveX (-scrollSpeed); - } - else if (x > metrics.getVirtualW () - 10) - { - gameCamera.setMoveX (scrollSpeed); - } - else - { - gameCamera.setMoveX (0); - } - } - } - } - - if (switchTeamConfirmMessageBox.getEnabled () == true) - { - switchTeamConfirmMessageBox.mouseMove (x, y); - } - - if (disconnectPlayerConfirmMessageBox.getEnabled () == true) - { - disconnectPlayerConfirmMessageBox.mouseMove (x, y); - } - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - if (errorMessageBox.getEnabled ()) - { - errorMessageBox.mouseMove (x, y); - } - if (scriptManager.getMessageBox ()->getEnabled ()) - { - scriptManager.getMessageBox ()->mouseMove (x, y); - } - //else if (saveBox) { - // saveBox->mouseMove(x, y); - //} else { - // //graphics - gui.mouseMoveGraphics (x, y); - //} - } - - //display - if (!gui.isSelecting () && !gui.isSelectingPos ()) - { - if (!gui.isSelectingPos ()) - { - if (metrics.isInDisplay (x, y)) - { - gui.mouseMoveDisplay (x - metrics.getDisplayX (), - y - metrics.getDisplayY ()); - } - else - { - gui.mouseMoveOutsideDisplay (); - } - } - } - - lastMousePos.x = mouseX; - lastMousePos.y = mouseY; - - Renderer & renderer = Renderer::getInstance (); - renderer.ccomputePosition (Vec2i (mouseX, mouseY), mouseCellPos); - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - bool Game::isValidMouseCellPos ()const - { - if (world.getMap () == NULL) - { - return false; - } - else - { - return world.getMap ()->isInside (mouseCellPos); - } - } - - void Game::eventMouseWheel (int x, int y, int zDelta) - { - if (this->masterserverMode == true) - { - return; - } - - if (currentUIState != NULL) - { - currentUIState->eventMouseWheel (x, y, zDelta); - return; - } - - try - { - gameCamera.zoom ((float) zDelta / 60.0f); - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::startCameraFollowUnit () - { - Selection *selection = gui.getSelectionPtr (); - if (selection->getCount () == 1) - { - Unit *currentUnit = selection->getUnitPtr (0); - if (currentUnit != NULL) - { - currentCameraFollowUnit = currentUnit; - getGameCameraPtr ()->setState (GameCamera::sUnit); - getGameCameraPtr ()-> - setPos (currentCameraFollowUnit->getCurrMidHeightVector ()); - - int rotation = currentCameraFollowUnit->getRotation (); - getGameCameraPtr ()->stop (); - getGameCameraPtr ()->rotateToVH (0.0f, (540 - rotation) % 360); - getGameCameraPtr ()->setHAng ((540 - rotation) % 360); - getGameCameraPtr ()->setVAng (0.0f); - } - } - else - { - if (currentCameraFollowUnit != NULL) - { - currentCameraFollowUnit = NULL; - } - } - } - - bool Game::textInput (std::string text) - { - if (chatManager.getEditEnabled () == true) - { - return chatManager.textInput (text); - } - return false; - } - - bool Game::sdlKeyDown (SDL_KeyboardEvent key) - { - if (this->masterserverMode == true) - { - return false; - } - if (gameStarted == false || totalRenderFps <= 0) - { - return false; - } - - if (chatManager.getEditEnabled () == true) - { - return false; - } - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //group - for (int idx = 1; idx <= Selection::maxGroups; idx++) - { - string keyName = "GroupUnitsKey" + intToStr (idx); - - SDL_Keycode groupHotKey = configKeys.getSDLKey (keyName.c_str ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] keyName [%s] group index = %d, key = [%c] [%d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, keyName.c_str (), idx, - groupHotKey, groupHotKey); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = %d (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,key.keysym.unicode,keyName.c_str(),isKeyPressed(groupHotKey,key)); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("input.keysym.mod = %d groupHotKey = %d key = (%d) [%s] isgroup = %d\n", - key.keysym.mod, groupHotKey, key.keysym.sym, keyName.c_str (), - isKeyPressed (groupHotKey, key)); - //printf(" group key check %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); - if (key.keysym.sym == groupHotKey) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - //printf("IS GROUP KEY %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); - gui.groupKey (idx - 1); - return true; - } - } - return false; - } - - void Game::keyDown (SDL_KeyboardEvent key) - { - if (this->masterserverMode == true) - { - return; - } - - //printf("In game checking keypress for key [%d]\n",key.keysym.sym); - - try - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d] gameStarted [%d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, key.keysym.sym, key.keysym.sym, - gameStarted); - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - if (currentUIState != NULL) - { - currentUIState->keyDown (key); - return; - } - - Lang & lang = Lang::getInstance (); - bool formerChatState = chatManager.getEditEnabled (); - //send key to the chat manager - chatManager.keyDown (key); - - if (formerChatState == false && chatManager.getEditEnabled ()) - { - camUpButtonDown = false; - camDownButtonDown = false; - camLeftButtonDown = false; - camRightButtonDown = false; - - gameCamera.stopMove (); - } - - //printf("GAME KEYDOWN #1\n"); - - if (chatManager.getEditEnabled () == false) - { - //printf("GAME KEYDOWN #2\n"); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%d - %c]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] key = [%d - %c] pausegame [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,key,configKeys.getCharKey("PauseGame")); - - //printf("SDL [%d] key [%d][%d]\n",configKeys.getSDLKey("SetMarker"),key.keysym.unicode,key.keysym.sym); - bool setMarkerKeyAllowsModifier = false; - if (configKeys.getSDLKey ("SetMarker") == SDLK_RALT || - configKeys.getSDLKey ("SetMarker") == SDLK_LALT) - { - setMarkerKeyAllowsModifier = true; - } - - //printf("In game checking keypress for key [%d] camera left [%d]\n",key.keysym.sym,configKeys.getSDLKey("CameraModeLeft")); - - if (isKeyPressed - (configKeys.getSDLKey ("RenderInGamePerformance"), key, - false) == true) - { - renderInGamePerformance = !renderInGamePerformance; - - Config::getInstance ().setBool ("PerformanceWarningEnabled", - renderInGamePerformance, true); - } - //if(key == configKeys.getCharKey("RenderNetworkStatus")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("RenderNetworkStatus"), key, - false) == true) - { - renderNetworkStatus = !renderNetworkStatus; - } - //else if(key == configKeys.getCharKey("ShowFullConsole")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("ShowFullConsole"), key, - false) == true) - { - showFullConsole = true; - } - else - if (isKeyPressed - (configKeys.getSDLKey ("SetMarker"), key, - setMarkerKeyAllowsModifier) == true) - { - setMarker = true; - printf("%d\n", key.keysym.scancode); - } - //else if(key == configKeys.getCharKey("TogglePhotoMode")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("TogglePhotoMode"), key, - false) == true) - { - photoModeEnabled = !photoModeEnabled; - if (photoModeEnabled == true && - this->gameSettings.isNetworkGame () == false) - { - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - else if (photoModeEnabled == false) - { - gameCamera.setMaxHeight (-1); - } - - } - //Toggle Healthbars - else - if (isKeyPressed - (configKeys.getSDLKey ("ToggleHealthbars"), key, - false) == true) - { - switch (healthbarMode) - { - case hbvUndefined: - healthbarMode = hbvOff; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsOff")); - break; - case hbvOff: - healthbarMode = hbvAlways; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsAlways")); - break; - case hbvAlways: - healthbarMode = hbvIfNeeded; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsIfNeeded")); - break; - case hbvIfNeeded: - healthbarMode = hbvSelected; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsSelected")); - break; - case hbvSelected: - healthbarMode = hbvSelected | hbvIfNeeded; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsSelectedOrNeeded")); - break; - case (hbvSelected | hbvIfNeeded): - healthbarMode = hbvUndefined; - console.addLine (lang.getString ("Healthbar2") + ": " + - lang.getString ("HealthbarsFactionDefault")); - break; - default: - printf - ("In [%s::%s Line: %d] Toggle Healthbars Hotkey - Invalid Value. Setting to default.\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - healthbarMode = hbvUndefined; - break; - } - } - //Toggle music - //else if(key == configKeys.getCharKey("ToggleMusic")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("ToggleMusic"), key, false) == true) - { - - if (this->masterserverMode == false) - { - Config & config = Config::getInstance (); - StrSound *gameMusic = - world.getThisFaction ()->getType ()->getMusic (); - if (gameMusic != NULL) - { - float - configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - float currentVolume = gameMusic->getVolume (); - if (currentVolume > 0) - { - gameMusic->setVolume (0); - console.addLine (lang.getString ("GameMusic") + - " " + lang.getString ("Off")); - } - else - { - //If the config says zero, use the default music volume - gameMusic->setVolume (configVolume ? configVolume : 0.9); - console.addLine (lang.getString ("GameMusic")); - } - } - } - } - //move camera left - //else if(key == configKeys.getCharKey("CameraModeLeft")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraModeLeft"), key, false) == true) - { - gameCamera.setMoveX (-1); - camLeftButtonDown = true; - } - //move camera right - //else if(key == configKeys.getCharKey("CameraModeRight")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraModeRight"), key, - false) == true) - { - gameCamera.setMoveX (1); - camRightButtonDown = true; - } - //move camera up - //else if(key == configKeys.getCharKey("CameraModeUp")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraModeUp"), key, false) == true) - { - gameCamera.setMoveZ (1); - camUpButtonDown = true; - } - //move camera down - //else if(key == configKeys.getCharKey("CameraModeDown")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraModeDown"), key, false) == true) - { - gameCamera.setMoveZ (-1); - camDownButtonDown = true; - } - //change camera mode - //else if(key == configKeys.getCharKey("FreeCameraMode")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("FreeCameraMode"), key, false) == true) - { - if (gameCamera.getState () == GameCamera::sFree) - { - gameCamera.setState (GameCamera::sGame); - string - stateString = - gameCamera.getState () == - GameCamera:: - sGame ? lang.getString ("GameCamera") : - lang.getString ("FreeCamera"); - console.addLine (lang.getString ("CameraModeSet") + " " + - stateString); - } - else if (gameCamera.getState () == GameCamera::sGame) - { - gameCamera.setState (GameCamera::sFree); - string - stateString = - gameCamera.getState () == - GameCamera:: - sGame ? lang.getString ("GameCamera") : - lang.getString ("FreeCamera"); - console.addLine (lang.getString ("CameraModeSet") + " " + - stateString); - } - //else ignore! - } - //reset camera mode to normal - //else if(key == configKeys.getCharKey("ResetCameraMode")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("ResetCameraMode"), key, - false) == true) - { - if (currentCameraFollowUnit != NULL) - { - currentCameraFollowUnit = NULL; - } - gameCamera.setState (GameCamera::sGame); - } - //pause - //else if(key == configKeys.getCharKey("PauseGame")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("PauseGame"), key, false) == true) - { - //printf("Toggle pause paused = %d\n",paused); - //setPaused(!paused); - - bool allowAdminMenuItems = false; - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - if (role == nrServer) - { - allowAdminMenuItems = true; - } - else if (role == nrClient) - { - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - - if (clientInterface != NULL && - gameSettings.getMasterserver_admin () == - clientInterface->getSessionKey ()) - { - allowAdminMenuItems = true; - } - } - - if (allowAdminMenuItems) - { - if (getPaused () == false) - { - commander.tryPauseGame (false, false); - } - else - { - commander.tryResumeGame (false, false); - } - } - } - else - if (isKeyPressed - (configKeys.getSDLKey ("ExtraTeamColorMarker"), key, - false) == true) - { - //printf("Toggle ExtraTeamColorMarker\n"); - toggleTeamColorMarker (); - } - //switch display color - //else if(key == configKeys.getCharKey("ChangeFontColor")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("ChangeFontColor"), key, - false) == true) - { - gui.switchToNextDisplayColor (); - } - //increment speed - //else if(key == configKeys.getCharKey("GameSpeedIncrease")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("GameSpeedIncrease"), key, - false) == true) - { - bool - speedChangesAllowed = - !NetworkManager:: - getInstance ().isNetworkGameWithConnectedClients (); - if (speedChangesAllowed) - { - incSpeed (); - } - } - //decrement speed - //else if(key == configKeys.getCharKey("GameSpeedDecrease")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("GameSpeedDecrease"), key, - false) == true) - { - bool - speedChangesAllowed = - !NetworkManager:: - getInstance ().isNetworkGameWithConnectedClients (); - if (speedChangesAllowed) - { - decSpeed (); - } - } - else - if (isKeyPressed - (configKeys.getSDLKey ("BookmarkAdd"), key, false) == true) - { - startMarkCell (); - } - else - if (isKeyPressed - (configKeys.getSDLKey ("BookmarkRemove"), key, false) == true) - { - isUnMarkCellEnabled = true; - } - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraFollowSelectedUnit"), key, - false) == true) - { - startCameraFollowUnit (); - } - //exit - else - if (isKeyPressed (configKeys.getSDLKey ("ExitKey"), key, false) - == true) - { - popupMenu.setEnabled (!popupMenu.getEnabled ()); - popupMenu.setVisible (popupMenu.getEnabled ()); - } - - //hotkeys - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] gameCamera.getState() = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - gameCamera.getState ()); - - if (gameCamera.getState () != GameCamera::sFree) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, key); - - gui.hotKey (key); - } - else - { - //rotate camera leftt - //if(key == configKeys.getCharKey("CameraRotateLeft")) { - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateLeft"), key) == true) - { - gameCamera.setRotate (-1); - } - //rotate camera right - //else if(key == configKeys.getCharKey("CameraRotateRight")){ - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateRight"), key) == true) - { - gameCamera.setRotate (1); - } - //camera up - //else if(key == configKeys.getCharKey("CameraRotateUp")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateUp"), key) == true) - { - gameCamera.setMoveY (1); - } - //camera down - //else if(key == configKeys.getCharKey("CameraRotateDown")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateDown"), key) == true) - { - gameCamera.setMoveY (-1); - } - } - - if (isKeyPressed (configKeys.getSDLKey ("SaveGame"), key) == true) - { - saveGame (); - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } - - void Game::keyUp (SDL_KeyboardEvent key) - { - if (this->masterserverMode == true) - { - return; - } - - try - { - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - if (currentUIState != NULL) - { - currentUIState->keyUp (key); - return; - } - - if (chatManager.getEditEnabled ()) - { - //send key to the chat manager - chatManager.keyUp (key); - } - else - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - //if(key == configKeys.getCharKey("ShowFullConsole")) { - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) - == true) - { - showFullConsole = false; - } - else if (isKeyPressed (configKeys.getSDLKey ("SetMarker"), key) == - true + gameStarted = true; + totalRenderFps++; + + throw; + } + } + + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } + + if (showPerfStats && chronoPerf.getMillis() >= 50) { + for (unsigned int x = 0; x < (unsigned int) perfList.size(); ++x) { + printf("%s", perfList[x].c_str()); + } + } + } catch (const exception & ex) { + quitPendingIndicator = true; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + //printf("#100 quitPendingIndicator = %d, errorMessageBox.getEnabled() = %d\n",quitPendingIndicator,errorMessageBox.getEnabled()); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + } + } + + void Game::addPerformanceCount(string key, int64 value) { + gamePerformanceCounts[key] = value + gamePerformanceCounts[key] / 2; + } + + string Game::getGamePerformanceCounts(bool displayWarnings) const { + if (gamePerformanceCounts.empty() == true) { + return ""; + } + + bool displayWarningHeader = true; + bool + WARN_TO_CONSOLE = + Config::getInstance().getBool("PerformanceWarningEnabled", "false"); + int + WARNING_MILLIS = + Config::getInstance().getInt("PerformanceWarningMillis", "7"); + int + WARNING_RENDER_MILLIS = + Config::getInstance().getInt("PerformanceWarningRenderMillis", + "40"); + + string result = ""; + for (std::map < string, int64 >::const_iterator iterMap = + gamePerformanceCounts.begin(); + iterMap != gamePerformanceCounts.end(); ++iterMap) { + if (iterMap->first == ProgramState::MAIN_PROGRAM_RENDER_KEY) { + if (iterMap->second < WARNING_RENDER_MILLIS) { + continue; + } + //else { + // printf("iterMap->second: " MG_I64_SPECIFIER " WARNING_RENDER_MILLIS = %d\n",iterMap->second,WARNING_RENDER_MILLIS); + //} + } else if (iterMap->second < WARNING_MILLIS) { + continue; + } + + if (result != "") { + result += "\n"; + } + string + perfStat = + iterMap->first + " = avg millis: " + intToStr(iterMap->second); + + if (displayWarnings == true && WARN_TO_CONSOLE == true) { + if (displayWarningHeader == true) { + displayWarningHeader = false; + printf + ("=====================================\nPERFORMANCE WARNINGS for World Frame: %d\n", + world.getFrameCount()); + } + + printf("*PERFORMANCE WARNING* %s\n", perfStat.c_str()); + } + + result += perfStat; + } + + return result; + } + + bool + Game::switchSetupForSlots(ServerInterface * &serverInterface, + int startIndex, int endIndex, + bool onlyNetworkUnassigned) { + bool switchRequested = false; + if (serverInterface == NULL) { + return switchRequested; + } + + MutexSafeWrapper + safeMutex(serverInterface->getSwitchSetupRequestsMutex(), + CODE_AT_LINE); + SwitchSetupRequest **switchSetupRequests = + serverInterface->getSwitchSetupRequests(); + if (switchSetupRequests == NULL) { + return switchRequested; + } + + Map *map = world.getMap(); + for (int i = startIndex; i < endIndex; ++i) { + if (switchSetupRequests[i] != NULL) { + //printf("Faction Index: %d Switch slot = %d to = %d current control = %d\n",i,switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex(),gameSettings.getFactionControl(i)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + switchSetupRequests[i]->getSwitchFlags + ()); + + if (onlyNetworkUnassigned == true + && gameSettings.getFactionControl(i) != ctNetworkUnassigned) { + if (i < map->getMaxPlayers() + || (i >= map->getMaxPlayers() + && gameSettings.getFactionControl(i) != ctNetwork)) { + continue; + } + } + + if (gameSettings.getFactionControl(i) == ctNetwork || + gameSettings.getFactionControl(i) == ctNetworkUnassigned || + //(gameSettings.getFactionControl(i) != ctClosed && gameSettings.getFactionControl(i) != ctHuman)) { + (gameSettings.getFactionControl(i) != ctHuman)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + switchSetupRequests[i]->getToSlotIndex + ()); + + if (switchSetupRequests[i]->getToSlotIndex() != -1) { + int newSlotIdx = switchSetupRequests[i]->getToSlotIndex(); + + //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex()); + + int + switchSlotIdx = + switchSetupRequests[i]->getCurrentSlotIndex(); + if (serverInterface->switchSlot(switchSlotIdx, newSlotIdx)) { + //printf("switchSlot returned true\n"); + switchRequested = true; + + int + oldFactionIndex = + gameSettings.getFactionIndexForStartLocation + (switchSlotIdx); + int newFactionIndex = + gameSettings.getFactionIndexForStartLocation(newSlotIdx); + + //printf("Switching faction for index %d [%d] to %d\n",newSlotIdx,switchSlotIdx,gameSettings.getFactionControl(newFactionIndex)); + + gameSettings.setNetworkPlayerName(oldFactionIndex, ""); + serverInterface-> + gameSettings.setNetworkPlayerName(oldFactionIndex, ""); + gameSettings.setNetworkPlayerUUID(oldFactionIndex, ""); + serverInterface-> + gameSettings.setNetworkPlayerUUID(oldFactionIndex, ""); + + gameSettings.setNetworkPlayerPlatform(oldFactionIndex, ""); + serverInterface-> + gameSettings.setNetworkPlayerPlatform(oldFactionIndex, ""); + + gameSettings.setFactionControl(newFactionIndex, ctNetwork); + serverInterface-> + gameSettings.setFactionControl(newFactionIndex, ctNetwork); + + //printf("#1a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,gameSettings.getFactionControl(newFactionIndex),gameSettings.getStartLocationIndex(newFactionIndex)); + //printf("#2a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,serverInterface->gameSettings.getFactionControl(newFactionIndex),serverInterface->gameSettings.getStartLocationIndex(newFactionIndex)); + + try { + //if(switchSetupRequests[i]->getSelectedFactionName() != "") { + // listBoxFactions[newFactionIdx].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + //} + //if(switchSetupRequests[i]->getToTeam() != -1) { + // listBoxTeams[newFactionIdx].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + //} + if (switchSetupRequests[i]->getNetworkPlayerName() != "") { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,labelPlayerNames[newFactionIdx].getText().c_str(),switchSetupRequests[i]->getNetworkPlayerName().c_str()); + gameSettings.setNetworkPlayerName(newFactionIndex, + switchSetupRequests + [i]->getNetworkPlayerName + ()); + serverInterface-> + gameSettings.setNetworkPlayerName(newFactionIndex, + switchSetupRequests + [i]->getNetworkPlayerName + ()); + } + + // if(gameSettings.getFactionControl(switchFactionIdx) == ctNetworkUnassigned) { + // serverInterface->removeSlot(switchFactionIdx); + // //listBoxControls[switchFactionIdx].setSelectedItemIndex(ctClosed); + // gameSettings.getFactionControl(switchFactionIdx) + // + // labelPlayers[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); + // labelPlayerNames[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); + // listBoxControls[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); + // listBoxFactions[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); + // listBoxTeams[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); + // labelNetStatus[switchSlotIdx].setVisible(switchSlotIdx+1 <= mapInfo.players); + // } + } catch (const runtime_error & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + e.what()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] caught exception error = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + e.what()); + } + } + //printf("AFTER switchSlot returned\n"); + } else { + try { + //if(switchSetupRequests[i]->getSelectedFactionName() != "") { + // listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + //} + //if(switchSetupRequests[i]->getToTeam() != -1) { + // listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + //} + + if ((switchSetupRequests[i]->getSwitchFlags() & + ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,switchSetupRequests[i]->getSwitchFlags(),switchSetupRequests[i]->getNetworkPlayerName().c_str(),labelPlayerNames[i].getText().c_str()); + + if (switchSetupRequests[i]->getNetworkPlayerName() != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + //labelPlayerNames[i].setText(switchSetupRequests[i]->getNetworkPlayerName()); + gameSettings.setNetworkPlayerName(i, + switchSetupRequests + [i]->getNetworkPlayerName + ()); + serverInterface->gameSettings.setNetworkPlayerName(i, + switchSetupRequests + [i]->getNetworkPlayerName + ()); + switchRequested = true; + } else { + //labelPlayerNames[i].setText(""); + gameSettings.setNetworkPlayerName(i, ""); + serverInterface->gameSettings.setNetworkPlayerName(i, + ""); + switchRequested = true; + } + //SetActivePlayerNameEditor(); + //switchSetupRequests[i]->clearSwitchFlag(ssrft_NetworkPlayerName); + } + } catch (const runtime_error & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, e.what()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] caught exception error = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, e.what()); + } + } + } + + delete switchSetupRequests[i]; + switchSetupRequests[i] = NULL; + } + } + + return switchRequested; + } + + void Game::updateNetworkMarkedCells() { + try { + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + if (gameNetworkInterface != NULL && + gameNetworkInterface->getMarkedCellList(false).empty() == false) { + + std::vector < MarkedCell > chatList = + gameNetworkInterface->getMarkedCellList(true); + for (int idx = 0; idx < (int) chatList.size(); idx++) { + MarkedCell mc = chatList[idx]; + if (mc.getFactionIndex() >= 0) { + mc. + setFaction((const Faction *) + world.getFaction(mc.getFactionIndex())); + } + + Map *map = world.getMap(); + Vec2i surfaceCellPos = map->toSurfCoords(mc.getTargetPos()); + mapMarkedCellList[surfaceCellPos] = mc; + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + void Game::updateNetworkUnMarkedCells() { + try { + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + if (gameNetworkInterface != NULL && + gameNetworkInterface->getUnMarkedCellList(false).empty() == + false) { + //Lang &lang= Lang::getInstance(); + + std::vector < UnMarkedCell > chatList = + gameNetworkInterface->getUnMarkedCellList(true); + for (int idx = 0; idx < (int) chatList.size(); idx++) { + UnMarkedCell mc = chatList[idx]; + mc. + setFaction((const Faction *) + world.getFaction(mc.getFactionIndex())); + + Map *map = world.getMap(); + Vec2i surfaceCellPos = map->toSurfCoords(mc.getTargetPos()); + mapMarkedCellList.erase(surfaceCellPos); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + + void Game::updateNetworkHighligtedCells() { + try { + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + //update the current entries + for (int idx = (int) highlightedCells.size() - 1; idx >= 0; idx--) { + MarkedCell *mc = &highlightedCells[idx]; + mc->decrementAliveCount(); + if (mc->getAliveCount() < 0) { + highlightedCells.erase(highlightedCells.begin() + idx); + } + } + + if (gameNetworkInterface != NULL && + gameNetworkInterface->getHighlightedCellList(false).empty() == + false) { + //Lang &lang= Lang::getInstance(); + std::vector < MarkedCell > highlighList = + gameNetworkInterface->getHighlightedCellList(true); + for (int idx = 0; idx < (int) highlighList.size(); idx++) { + MarkedCell mc = highlighList[idx]; // I want a copy here + if (mc.getFactionIndex() >= 0) { + mc.setFaction((const Faction *) world.getFaction(mc.getFactionIndex())); // set faction pointer + } + addOrReplaceInHighlightedCells(mc); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + void Game::addOrReplaceInHighlightedCells(MarkedCell mc) { + if (mc.getFactionIndex() >= 0) { + for (int i = (int) highlightedCells.size() - 1; i >= 0; i--) { + MarkedCell *currentMc = &highlightedCells[i]; + if (currentMc->getFactionIndex() == mc.getFactionIndex()) { + highlightedCells.erase(highlightedCells.begin() + i); + } + } + } + if (mc.getAliveCount() <= 0) { + mc.setAliveCount(200); + } + highlightedCells.push_back(mc); + + if (this->masterserverMode == false) { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + const Faction *faction = mc.getFaction(); + if (getWorld()->getThisFaction() == NULL) { + throw + megaglest_runtime_error("getWorld()->getThisFaction() == NULL"); + } + //printf("faction [%p][%s]\n",faction,(faction != NULL ? faction->getType()->getName().c_str() : "")); + if ((faction == NULL) || + (faction->getTeam() == + getWorld()->getThisFaction()->getTeam())) { + static PlaySoundClip snd; + soundRenderer.playFx(snd.getSound(snd.sfxMarker), true); + } + } + } + + void + Game::ReplaceDisconnectedNetworkPlayersWithAI(bool isNetworkGame, + NetworkRole role) { + if (role == nrServer && isNetworkGame == true && + difftime((long int) time(NULL), + lastNetworkPlayerConnectionCheck) >= + NETWORK_PLAYER_CONNECTION_CHECK_SECONDS) { + lastNetworkPlayerConnectionCheck = time(NULL); + Logger & logger = Logger::getInstance(); + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(); + + bool newAIPlayerCreated = false; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + if (faction->getFactionDisconnectHandled() == false && + (faction->getControlType() == ctNetwork || + faction->getControlType() == ctNetworkCpuEasy || + faction->getControlType() == ctNetworkCpu || + faction->getControlType() == ctNetworkCpuUltra || + faction->getControlType() == ctNetworkCpuZeta)) { + + if (aiInterfaces[i] == NULL && + server-> + isClientConnected(faction->getStartLocationIndex()) == + false) { + + if (faction->getPersonalityType() != fpt_Observer) { + DumpCRCWorldLogIfRequired("_faction_" + intToStr(i)); + } + + faction->setFactionDisconnectHandled(true); + + Lang & lang = Lang::getInstance(); + + bool isPlayerObserver = false; + char szBuf[8096] = ""; + if (faction->getPersonalityType() != fpt_Observer) { + aiInterfaces[i] = + new AiInterface(*this, i, faction->getTeam(), + faction->getStartLocationIndex()); + + snprintf(szBuf, 8096, + Lang::getInstance().getString + ("LogScreenGameLoadingCreatingAIFaction", + "", true).c_str(), i); + logger.add(szBuf, true); + + commander.tryNetworkPlayerDisconnected(i); + + newAIPlayerCreated = true; + } else { + isPlayerObserver = true; + + } + + const + vector < + string > + languageList = + this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int j = 0; + j < (unsigned int) languageList.size(); ++j) { + if (isPlayerObserver == false) { + string + msg = + "Player #%d [%s] has disconnected, switching player to AI mode!"; + if (lang.hasString("GameSwitchPlayerToAI", + languageList[j], true)) { + msg = + lang.getString("GameSwitchPlayerToAI", + languageList[j], true); + } + snprintf(szBuf, 8096, msg.c_str(), i + 1, + this->gameSettings.getNetworkPlayerName(i). + c_str()); + } else { + string + msg = + "Player #%d [%s] has disconnected, but player was only an observer!"; + if (lang.hasString("GameSwitchPlayerObserverToAI", + languageList[j], true)) { + msg = + lang.getString("GameSwitchPlayerObserverToAI", + languageList[j], true); + } + snprintf(szBuf, 8096, msg.c_str(), i + 1, + this->gameSettings.getNetworkPlayerName(i). + c_str()); + } + bool localEcho = (languageList[j] == lang.getLanguage()); + server->sendTextMessage(szBuf, -1, localEcho, + languageList[j]); + } + } + } + } + + if (newAIPlayerCreated == true + && Config::getInstance().getBool("EnableNewThreadManager", + "false") == true) { + bool + enableServerControlledAI = + this->gameSettings.getEnableServerControlledAI(); + + masterController.clearSlaves(true); + + std::vector < SlaveThreadControllerInterface * >slaveThreadList; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + if (faction->getCpuControl + (enableServerControlledAI, isNetworkGame, role) == true) { + slaveThreadList.push_back(aiInterfaces[i]->getWorkerThread()); + } + } + masterController.setSlaves(slaveThreadList); + } + } + } + + void Game::updateCamera() { + if (currentUIState != NULL) { + currentUIState->updateCamera(); + return; + } + gameCamera.update(); + } + + + // ==================== render ==================== + + //render + void Game::render() { + // Ensure the camera starts in the right position + if (isFirstRender == true) { + isFirstRender = false; + + if (this->loadGameNode == NULL) { + gameCamera.setState(GameCamera::sGame); + this->restoreToStartXY(); + } + } + + canRender(); + incrementFps(); + + renderFps++; + totalRenderFps++; + + updateWorldStats(); + + //NetworkManager &networkManager= NetworkManager::getInstance(); + if (this->masterserverMode == false) { + renderWorker(); + } else { + // Titi, uncomment this to watch the game on the masterserver + //renderWorker(); + + // In masterserver mode quit game if no network players left + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(); + int connectedClients = 0; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + if (server->isClientConnected(faction->getStartLocationIndex()) == + true) { + connectedClients++; + } + } + + if (connectedClients == 0) { + quitTriggeredIndicator = true; + } else { + string str = ""; + std::map < int, string > factionDebugInfo; + + if (difftime + ((long int) time(NULL), + lastMasterServerGameStatsDump) >= GAME_STATS_DUMP_INTERVAL) { + lastMasterServerGameStatsDump = time(NULL); + str = getDebugStats(factionDebugInfo); + + printf("== Current in-game stats (interval %d) ==\n%s\n", + GAME_STATS_DUMP_INTERVAL, str.c_str()); + } + } + } + } + + void Game::renderWorker() { + if (currentUIState != NULL) { + // Renderer &renderer= Renderer::getInstance(); + // renderer.clearBuffers(); + // + // //3d + // renderer.reset3dMenu(); + // + // renderer.clearZBuffer(); + // //renderer.loadCameraMatrix(menuBackground.getCamera()); + // //renderer.renderMenuBackground(&menuBackground); + // renderer.renderParticleManager(rsMenu); + // + // //2d + // renderer.reset2d(); + // + // currentUIState->render(); + // + // if(renderer.isMasterserverMode() == false) { + // renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + // renderer.renderFPSWhenEnabled(lastFps); + // renderer.swapBuffers(); + // } + + currentUIState->render(); + return; + } else { + Renderer & renderer = Renderer::getInstance(); + if (renderer.getCustom3dMenu() != NULL) { + renderer.setCustom3dMenu(NULL); + } + } + + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + render3d(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %d [render3d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + render2d(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %d [render2d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + Renderer::getInstance().swapBuffers(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %d [swap buffers]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + } + + // ==================== tick ==================== + + void Game::removeUnitFromSelection(const Unit * unit) { + try { + Selection *selection = gui.getSelectionPtr(); + for (int i = 0; i < selection->getCount(); ++i) { + const Unit *currentUnit = selection->getUnit(i); + if (currentUnit == unit) { + selection->unSelect(i); + break; + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + + //abort(); + } + } + + bool Game::addUnitToSelection(Unit * unit) { + bool result = false; + try { + Selection *selection = gui.getSelectionPtr(); + if (selection != NULL) { + result = selection->select(unit, true); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + + //abort(); + } + + return result; + } + + void Game::addUnitToGroupSelection(Unit * unit, int groupIndex) { + try { + Selection *selection = gui.getSelectionPtr(); + if (selection != NULL) { + selection->addUnitToGroup(groupIndex, unit); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + + //abort(); + } + } + + void Game::removeUnitFromGroupSelection(int unitId, int groupIndex) { + try { + Selection *selection = gui.getSelectionPtr(); + if (selection != NULL) { + selection->removeUnitFromGroup(groupIndex, unitId); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + + //abort(); + } + } + + void Game::recallGroupSelection(int groupIndex) { + try { + Selection *selection = gui.getSelectionPtr(); + if (selection != NULL) { + selection->recallGroup(groupIndex); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + if (errorMessageBox.getEnabled() == false) { + ErrorDisplayMessage(ex.what(), true); + } + + //abort(); + } + } + + void Game::tick() { + ProgramState::tick(); + + tickCount++; + + if (avgUpdateFps == -1) { + avgUpdateFps = updateFps; + } else { + avgUpdateFps = (avgUpdateFps + updateFps) / 2; + } + currentAvgRenderFpsTotal += renderFps; + + if (avgRenderFps == -1) { + avgRenderFps = renderFps; + } + // Update the average every x game ticks + const int CHECK_AVG_FPS_EVERY_X_TICKS = 15; + if (tickCount % CHECK_AVG_FPS_EVERY_X_TICKS == 0) { + avgRenderFps = currentAvgRenderFpsTotal / CHECK_AVG_FPS_EVERY_X_TICKS; + currentAvgRenderFpsTotal = 0; + } + + if (captureAvgTestStatus == true) { + if (updateFpsAvgTest == -1) { + updateFpsAvgTest = updateFps; + } else { + updateFpsAvgTest = (updateFpsAvgTest + updateFps) / 2; + } + if (renderFpsAvgTest == -1) { + renderFpsAvgTest = renderFps; + } else { + renderFpsAvgTest = (renderFpsAvgTest + renderFps) / 2; + } + } + + lastUpdateFps = updateFps; + lastRenderFps = renderFps; + updateFps = 0; + renderFps = 0; + + //Win/lose check + checkWinner(); + gui.tick(); + } + + + // ==================== events ==================== + + int Game::getFirstUnusedTeamNumber() { + std::map < int, bool > uniqueTeamNumbersUsed; + for (unsigned int i = 0; i < (unsigned int) world.getFactionCount(); + ++i) { + Faction *faction = world.getFaction(i); + uniqueTeamNumbersUsed[faction->getTeam()] = true; + } + + int result = -1; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (uniqueTeamNumbersUsed.find(i) == uniqueTeamNumbersUsed.end()) { + result = i; + break; + } + } + + return result; + } + + void Game::setupRenderForVideo() { + Renderer & renderer = Renderer::getInstance(); + //renderer.clearBuffers(); + //3d + //renderer.reset3dMenu(); + //renderer.clearZBuffer(); + //2d + //renderer.reset2d(); + renderer.setupRenderForVideo(); + } + + void Game::tryPauseToggle(bool pauseValue) { + bool allowAdminMenuItems = false; + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + if (role == nrServer) { + allowAdminMenuItems = true; + } else if (role == nrClient) { + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + + if (clientInterface != NULL && + gameSettings.getMasterserver_admin() == + clientInterface->getSessionKey()) { + allowAdminMenuItems = true; + } + } + + bool isNetworkGame = this->gameSettings.isNetworkGame(); + //printf("Try Pause allowAdminMenuItems = %d, pauseValue = %d\n",allowAdminMenuItems,pauseValue); + + if (allowAdminMenuItems) { + if (pauseValue == true) { + commander.tryPauseGame(false, false); + } else { + if (isNetworkGame == false) { + setPaused(pauseValue, true, false, false); + } else { + commander.tryResumeGame(false, false); + } + } + } + } + + void Game::startMarkCell() { + int totalMarkedCellsForPlayer = 0; + if (world.getThisFaction() != NULL) { + for (std::map < Vec2i, MarkedCell >::iterator iterMap = + mapMarkedCellList.begin(); + iterMap != mapMarkedCellList.end(); ++iterMap) { + MarkedCell & bm = iterMap->second; + if (bm.getPlayerIndex() == + world.getThisFaction()->getStartLocationIndex()) { + totalMarkedCellsForPlayer++; + } + } + } + + const int MAX_MARKER_COUNT = 5; + if (totalMarkedCellsForPlayer < MAX_MARKER_COUNT) { + isMarkCellEnabled = true; + } else { + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("MaxMarkerCount") + " " + + intToStr(MAX_MARKER_COUNT)); + } + } + + void Game::processInputText(string text, bool cancelled) { + isMarkCellTextEnabled = false; + + if (cancelled == false) { + //printf("Note [%s]\n",text.c_str()); + + cellMarkedData.setNote(text); + addCellMarker(cellMarkedPos, cellMarkedData); + + // if(text.find("\\n") != text.npos) { + // replaceAll(text, "\\n", "\n"); + // } + // if(text.find("\\t") != text.npos) { + // replaceAll(text, "\\t", "\t"); + // } + // + // cellMarkedData.setNote(text); + // mapMarkedCellList[cellMarkedPos] = cellMarkedData; + // + // GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + // + // int factionIndex = -1; + // int playerIndex = -1; + // if(cellMarkedData.getFaction() != NULL) { + // factionIndex = cellMarkedData.getFaction()->getIndex(); + // playerIndex = cellMarkedData.getFaction()->getStartLocationIndex(); + // } + // gameNetworkInterface->sendMarkCellMessage( + // cellMarkedData.getTargetPos(), + // factionIndex, + // cellMarkedData.getNote(), + // playerIndex); + // + // Renderer &renderer= Renderer::getInstance(); + // renderer.forceQuadCacheUpdate(); + } + } + + void Game::addCellMarker(Vec2i cellPos, MarkedCell cellData) { + //printf("Note [%s]\n",text.c_str()); + + string text = cellData.getNote(); + if (text.find("\\n") != text.npos) { + replaceAll(text, "\\n", "\n"); + } + if (text.find("\\t") != text.npos) { + replaceAll(text, "\\t", "\t"); + } + + cellData.setNote(text); + mapMarkedCellList[cellPos] = cellData; + + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + int factionIndex = -1; + int playerIndex = -1; + if (cellData.getFaction() != NULL) { + factionIndex = cellData.getFaction()->getIndex(); + playerIndex = cellData.getFaction()->getStartLocationIndex(); + } + + //printf("Adding Cell marker pos [%s] factionIndex [%d] note [%s] playerIndex = %d\n",cellData.getTargetPos().getString().c_str(),factionIndex,cellData.getNote().c_str(),playerIndex); + + gameNetworkInterface->sendMarkCellMessage(cellData.getTargetPos(), + factionIndex, + cellData.getNote(), + playerIndex); + + Renderer & renderer = Renderer::getInstance(); + renderer.forceQuadCacheUpdate(); + } + + void + Game::removeCellMarker(Vec2i surfaceCellPos, const Faction * faction) { + //Vec2i surfaceCellPos = map->toSurfCoords(Vec2i(xCell,yCell)); + Map *map = world.getMap(); + SurfaceCell *sc = map->getSurfaceCell(surfaceCellPos); + Vec3f vertex = sc->getVertex(); + Vec2i targetPos(vertex.x, vertex.z); + + //printf("Remove Cell marker lookup pos [%s] factionIndex [%d]\n",surfaceCellPos.getString().c_str(),(faction != NULL ? faction->getIndex() : -1)); + + if (mapMarkedCellList.find(surfaceCellPos) != mapMarkedCellList.end()) { + MarkedCell mc = mapMarkedCellList[surfaceCellPos]; + if (mc.getFaction() == faction) { + mapMarkedCellList.erase(surfaceCellPos); + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + + int factionIndex = (faction != NULL ? faction->getIndex() : -1); + + //printf("Remvoing Cell marker pos [%s] factionIndex [%d] note [%s]\n",mc.getTargetPos().getString().c_str(),factionIndex,mc.getNote().c_str()); + + gameNetworkInterface->sendUnMarkCellMessage(mc.getTargetPos(), + factionIndex); + } + } + //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); + + //isUnMarkCellEnabled = false; + + Renderer & renderer = Renderer::getInstance(); + //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); + renderer.forceQuadCacheUpdate(); + } + void Game::showMarker(Vec2i cellPos, MarkedCell cellData) { + //setMarker = true; + //if(setMarker) { + //Vec2i targetPos = cellData.targetPos; + //Vec2i screenPos(x,y-60); + //Renderer &renderer= Renderer::getInstance(); + //renderer.computePosition(screenPos, targetPos); + //Vec2i surfaceCellPos = map->toSurfCoords(targetPos); + + //MarkedCell mc(targetPos,world.getThisFaction(),"none",world.getThisFaction()->getStartLocationIndex()); + addOrReplaceInHighlightedCells(cellData); + + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + gameNetworkInterface-> + sendHighlightCellMessage(cellData.getTargetPos(), + cellData.getFactionIndex()); + //} + } + + void Game::mouseDownLeft(int x, int y) { + if (this->masterserverMode == true) { + return; + } + cameraDragAllowed = false; + if (currentUIState != NULL) { + currentUIState->mouseDownLeft(x, y); + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + Logger::getInstance().handleMouseClick(x, y); + return; + } + + Map *map = world.getMap(); + const Metrics & metrics = Metrics::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + bool messageBoxClick = false; + bool originalIsMarkCellEnabled = isMarkCellEnabled; + bool originalIsUnMarkCellEnabled = isUnMarkCellEnabled; + + if (popupMenu.mouseClick(x, y)) { + std::pair < int, + string > result = popupMenu.mouseClickedMenuItem(x, y); + //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); + + //printf("popupMenu.mouseClick == true result.first = %d disconnectPlayerPopupMenuIndex = %d\n",result.first,disconnectPlayerPopupMenuIndex); + + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + // Exit game + if (result.first == exitGamePopupMenuIndex) { + showMessageBox(Lang:: + getInstance().getString("ExitBattleQuestion"), + "", true); + } else if (result.first == joinTeamPopupMenuIndex) { + + Lang & lang = Lang::getInstance(); + switchTeamIndexMap.clear(); + std::map < int, bool > uniqueTeamNumbersUsed; + std::vector < string > menuItems; + for (unsigned int i = 0; + i < (unsigned int) world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + if (faction->getPersonalityType() != fpt_Observer && + uniqueTeamNumbersUsed.find(faction->getTeam()) == + uniqueTeamNumbersUsed.end()) { + uniqueTeamNumbersUsed[faction->getTeam()] = true; + } + + if (faction->getPersonalityType() != fpt_Observer && + world.getThisFaction()->getIndex() != + faction->getIndex() + && world.getThisFaction()->getTeam() != + faction->getTeam()) { + char szBuf[8096] = ""; + if (lang.hasString("JoinPlayerTeam") == true) { + snprintf(szBuf, 8096, + (" " + + lang.getString("JoinPlayerTeam") + + " ").c_str(), faction->getIndex(), + this->gameSettings.getNetworkPlayerName(i). + c_str(), faction->getTeam()); + } else { + snprintf(szBuf, 8096, + " Join player #%d - %s on Team: %d ", + faction->getIndex(), + this->gameSettings.getNetworkPlayerName(i). + c_str(), faction->getTeam()); + } + + menuItems.push_back(szBuf); + + switchTeamIndexMap[(int) menuItems.size() - 1] = + faction->getTeam(); + } + } + + if ((int) uniqueTeamNumbersUsed.size() < 8) { + menuItems.push_back(" " + + lang.getString("CreateNewTeam") + " "); + switchTeamIndexMap[(int) menuItems.size() - 1] = + CREATE_NEW_TEAM; + } + menuItems.push_back(" " + lang.getString("Cancel") + " "); + switchTeamIndexMap[(int) menuItems.size() - 1] = + CANCEL_SWITCH_TEAM; + + popupMenuSwitchTeams.setW(100); + popupMenuSwitchTeams.setH(100); + popupMenuSwitchTeams.init(" " + + lang.getString("SwitchTeams") + + " ", menuItems); + popupMenuSwitchTeams.setEnabled(true); + popupMenuSwitchTeams.setVisible(true); + } else if (result.first == disconnectPlayerPopupMenuIndex) { + Lang & lang = Lang::getInstance(); + + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + ServerInterface *serverInterface = NULL; + if (role == nrServer) { + serverInterface = + dynamic_cast < + ServerInterface *>(networkManager.getServerInterface()); + } + disconnectPlayerIndexMap.clear(); + std::vector < string > menuItems; + for (unsigned int i = 0; + i < (unsigned int) world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + //printf("faction->getPersonalityType() = %d index [%d,%d] control [%d] networkstatus [%d]\n",faction->getPersonalityType(),world.getThisFaction()->getIndex(),faction->getIndex(),faction->getControlType(),this->gameSettings.getNetworkPlayerStatuses(i)); + + bool isSlotJoinInProgressClient = false; + if (serverInterface != NULL) { + + MutexSafeWrapper + safeMutex(serverInterface->getSlotMutex + (faction->getStartLocationIndex()), + CODE_AT_LINE); + ConnectionSlot *slot = + serverInterface->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL + && slot->getConnectHasHandshaked() == true + && slot->getCurrentFrameCount() <= 0) { + isSlotJoinInProgressClient = true; + } + } + + //printf("isSlotJoinInProgressClient: %d [%d] [%d][%d] [%d] [%d] [%d]\n", + // isSlotJoinInProgressClient,faction->getPersonalityType(),faction->getIndex(),world.getThisFaction()->getIndex(),faction->getControlType(),this->gameSettings.getNetworkPlayerStatuses(i),i); + + if (isSlotJoinInProgressClient == true || + (faction->getPersonalityType() != fpt_Observer && + world.getThisFaction()->getIndex() != + faction->getIndex() + && faction->getControlType() == ctNetwork + && this->gameSettings.getNetworkPlayerStatuses(i) != + npst_Disconnected)) { + + char szBuf[8096] = ""; + if (lang.hasString("DisconnectNetorkPlayerIndex") == true) { + snprintf(szBuf, 8096, + (" " + + lang.getString + ("DisconnectNetorkPlayerIndex") + + " ").c_str(), + faction->getIndex() + 1, + this->gameSettings.getNetworkPlayerName(i). + c_str()); + } else { + snprintf(szBuf, 8096, + " Disconnect player #%d - %s: ", + faction->getIndex() + 1, + this->gameSettings.getNetworkPlayerName(i). + c_str()); + } + + menuItems.push_back(szBuf); + + //disconnectPlayerIndexMap[menuItems.size()-1] = faction->getStartLocationIndex(); + disconnectPlayerIndexMap[(int) menuItems.size() - + 1] = faction->getIndex(); + } + } + + menuItems.push_back(" " + lang.getString("Cancel") + " "); + disconnectPlayerIndexMap[(int) menuItems.size() - 1] = + CANCEL_DISCONNECT_PLAYER; + + popupMenuDisconnectPlayer.setW(100); + popupMenuDisconnectPlayer.setH(100); + popupMenuDisconnectPlayer.init(" " + + lang.getString + ("DisconnectNetorkPlayer") + + " ", menuItems); + popupMenuDisconnectPlayer.setEnabled(true); + popupMenuDisconnectPlayer.setVisible(true); + } else if (result.first == keyboardSetupPopupMenuIndex) { + MainMenu *newMenu = new MainMenu(program); // open keyboard shortcuts setup screen + currentUIState = newMenu; + Renderer & renderer = Renderer::getInstance(); + renderer.setCustom3dMenu(newMenu); + //currentUIState->load(); + currentUIState->init(); + + // open keyboard shortcuts setup screen + newMenu->setState(new + MenuStateKeysetup(program, newMenu, + ¤tUIState)); + } else if (result.first == pauseGamePopupMenuIndex) { + //this->setPaused(!paused); + //printf("popup paused = %d\n",paused); + + bool allowAdminMenuItems = false; + NetworkRole role = networkManager.getNetworkRole(); + if (role == nrServer) { + allowAdminMenuItems = true; + } else if (role == nrClient) { + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + + if (clientInterface != NULL && + gameSettings.getMasterserver_admin() == + clientInterface->getSessionKey()) { + allowAdminMenuItems = true; + } + } + + if (allowAdminMenuItems) { + if (getPaused() == false) { + commander.tryPauseGame(false, false); + } else { + commander.tryResumeGame(false, false); + } + } + } else if (result.first == saveGamePopupMenuIndex) { + saveGame(); + } + //else if(result.first == markCellPopupMenuIndex) { + // startMarkCell(); + //} + //else if(result.first == unmarkCellPopupMenuIndex) { + // isUnMarkCellEnabled = true; + //} + } else if (popupMenuSwitchTeams.mouseClick(x, y)) { + //popupMenuSwitchTeams + std::pair < int, + string > + result = popupMenuSwitchTeams.mouseClickedMenuItem(x, y); + //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); + + popupMenuSwitchTeams.setEnabled(false); + popupMenuSwitchTeams.setVisible(false); + + //bool isNetworkGame = this->gameSettings.isNetworkGame(); + + int teamIndex = switchTeamIndexMap[result.first]; + switch (teamIndex) { + case CREATE_NEW_TEAM: + { + int newTeam = getFirstUnusedTeamNumber(); + //if(isNetworkGame == true) { + const Faction *faction = world.getThisFaction(); + commander.trySwitchTeam(faction, newTeam); + //} + //else { + // const Faction *faction = world.getThisFaction(); + // commander.trySwitchTeam(faction,newTeam); + //} + } + break; + case CANCEL_SWITCH_TEAM: + break; + default: + //if(isNetworkGame == true) { + const Faction *faction = world.getThisFaction(); + commander.trySwitchTeam(faction, teamIndex); + //} + //else { + // const Faction *faction = world.getThisFaction(); + // commander.trySwitchTeam(faction,teamIndex); + //} + + break; + } + } else if (popupMenuDisconnectPlayer.mouseClick(x, y)) { + //popupMenuSwitchTeams + std::pair < int, + string > + result = popupMenuDisconnectPlayer.mouseClickedMenuItem(x, y); + //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); + + popupMenuDisconnectPlayer.setEnabled(false); + popupMenuDisconnectPlayer.setVisible(false); + + //bool isNetworkGame = this->gameSettings.isNetworkGame(); + + //int playerIndex = disconnectPlayerIndexMap[result.first]; + int factionIndex = disconnectPlayerIndexMap[result.first]; + switch (factionIndex) { + case CANCEL_DISCONNECT_PLAYER: + break; + default: + // if(isNetworkGame == true) { + // const Faction *faction = world.getThisFaction(); + // commander.trySwitchTeam(faction,teamIndex); + // } + // else { + // const Faction *faction = world.getThisFaction(); + // commander.trySwitchTeam(faction,teamIndex); + // } + + + GameSettings * settings = world.getGameSettingsPtr(); + Lang & lang = Lang::getInstance(); + + char szBuf[8096] = ""; + if (lang.hasString("DisconnectNetorkPlayerIndexConfirm") == true) { + snprintf(szBuf, 8096, + (" " + + lang.getString + ("DisconnectNetorkPlayerIndexConfirm") + + " ").c_str(), factionIndex + 1, + settings-> + getNetworkPlayerName(factionIndex).c_str()); + } else { + snprintf(szBuf, 8096, + " Confirm disconnection for player #%d - %s? ", + factionIndex + 1, + settings-> + getNetworkPlayerName(factionIndex).c_str()); + } + + disconnectPlayerConfirmMessageBox.setText(szBuf); + disconnectPlayerConfirmMessageBox.init(lang.getString("Yes"), + lang.getString("No")); + disconnectPlayerConfirmMessageBox.setEnabled(true); + + playerIndexDisconnect = + world.getFaction(factionIndex)->getStartLocationIndex(); + + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetworkInterface != NULL) { + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = settings->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; + i < (unsigned int) languageList.size(); ++i) { + char szMsg[8096] = ""; + if (lang.hasString("DisconnectNetorkPlayerIndexConfirmed", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("DisconnectNetorkPlayerIndexConfirmed", + languageList[i]).c_str(), + factionIndex + 1, + settings->getNetworkPlayerName + (factionIndex).c_str()); + } else { + snprintf(szMsg, 8096, + "Notice - Admin is warning to disconnect player #%d - %s!", + factionIndex + 1, + settings->getNetworkPlayerName + (factionIndex).c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + gameNetworkInterface->sendTextMessage(szMsg, -1, + localEcho, + languageList[i]); + } + + sleep(10); + } + + break; + } + } + + if (switchTeamConfirmMessageBox.getEnabled() == true) { + int button = -1; + if (switchTeamConfirmMessageBox.mouseClick(x, y, button)) { + switchTeamConfirmMessageBox.setEnabled(false); + + SwitchTeamVote *vote = + world.getThisFactionPtr()-> + getSwitchTeamVote(world.getThisFaction + ()->getCurrentSwitchTeamVoteFactionIndex()); + vote->voted = true; + vote->allowSwitchTeam = (button == 0); + + const Faction *faction = world.getThisFaction(); + commander.trySwitchTeamVote(faction, vote); + } + } else if (disconnectPlayerConfirmMessageBox.getEnabled() == true) { + int button = -1; + if (disconnectPlayerConfirmMessageBox.mouseClick(x, y, button)) { + disconnectPlayerConfirmMessageBox.setEnabled(false); + + if (button == 0) { + const Faction *faction = world.getThisFaction(); + commander.tryDisconnectNetworkPlayer(faction, + playerIndexDisconnect); + } + } + } + + //scrip message box, only if the exit box is not enabled + if (mainMessageBox.getEnabled() == false && + errorMessageBox.getEnabled() == false && + scriptManager.getMessageBox()->getEnabled()) { + int button = 0; + if (scriptManager.getMessageBox()->mouseClick(x, y, button)) { + scriptManager.onMessageBoxOk(); + messageBoxClick = true; + } + } + + //minimap panel + if (messageBoxClick == false) { + if (metrics.isInMinimap(x, y)) { + int xm = x - metrics.getMinimapX(); + int ym = y - metrics.getMinimapY(); + int + xCell = + static_cast < + int>(xm * + (static_cast < + float>(map->getW()) / metrics.getMinimapW())); + int + yCell = + static_cast < + int>(map->getH() - + ym * (static_cast < + float>(map->getH()) / metrics.getMinimapH())); + + if (map->isInside(xCell, yCell) + && map->isInsideSurface(map->toSurfCoords(Vec2i + (xCell, yCell)))) { + if (gui.isSelectingPos()) { + gui.mouseDownLeftGraphics(xCell, yCell, true); + } else { + if (!setMarker) { + cameraDragAllowed = true; + gameCamera.setPos(Vec2f + (static_cast (xCell), + static_cast (yCell))); + } + } + + if (setMarker) { + Vec2i + surfaceCellPos = map->toSurfCoords(Vec2i(xCell, yCell)); + SurfaceCell *sc = map->getSurfaceCell(surfaceCellPos); + Vec3f vertex = sc->getVertex(); + Vec2i targetPos(vertex.x, vertex.z); + + MarkedCell + mc(targetPos, world.getThisFaction(), "none", + world.getThisFaction()->getStartLocationIndex()); + addOrReplaceInHighlightedCells(mc); + + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + gameNetworkInterface-> + sendHighlightCellMessage(mc.getTargetPos(), + mc.getFaction()->getIndex()); + } + + + if (originalIsMarkCellEnabled == true + && isMarkCellEnabled == true) { + Vec2i + surfaceCellPos = map->toSurfCoords(Vec2i(xCell, yCell)); + SurfaceCell *sc = map->getSurfaceCell(surfaceCellPos); + Vec3f vertex = sc->getVertex(); + Vec2i targetPos(vertex.x, vertex.z); + + MarkedCell + mc(targetPos, world.getThisFaction(), + "placeholder for note", + world.getThisFaction()->getStartLocationIndex()); + + //GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + //gameNetworkInterface->sendMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex(),mc.getNote()); + + //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); + + isMarkCellEnabled = false; + cellMarkedData = mc; + cellMarkedPos = surfaceCellPos; + isMarkCellTextEnabled = true; + chatManager.switchOnEdit(this, 500); + + Renderer & renderer = Renderer::getInstance(); + //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); + renderer.forceQuadCacheUpdate(); + } + if (originalIsUnMarkCellEnabled == true + && isUnMarkCellEnabled == true) { + Vec2i + surfaceCellPos = map->toSurfCoords(Vec2i(xCell, yCell)); + SurfaceCell *sc = map->getSurfaceCell(surfaceCellPos); + Vec3f vertex = sc->getVertex(); + Vec2i targetPos(vertex.x, vertex.z); + + // if(mapMarkedCellList.find(surfaceCellPos) != mapMarkedCellList.end()) { + // MarkedCell mc = mapMarkedCellList[surfaceCellPos]; + // if(mc.getFaction() == world.getThisFaction()) { + // mapMarkedCellList.erase(surfaceCellPos); + // GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + // gameNetworkInterface->sendUnMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex()); + // } + // } + + isUnMarkCellEnabled = false; + + removeCellMarker(surfaceCellPos, world.getThisFaction()); + //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); + + Renderer & renderer = Renderer::getInstance(); + //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); + renderer.forceQuadCacheUpdate(); + } + } + } + //display panel + else if (metrics.isInDisplay(x, y) && !gui.isSelectingPos()) { + int xd = x - metrics.getDisplayX(); + int yd = y - metrics.getDisplayY(); + if (gui.mouseValid(xd, yd)) { + gui.mouseDownLeftDisplay(xd, yd); + } else { + gui.mouseDownLeftGraphics(x, y, false); + } + } + //graphics panel + else { + gui.mouseDownLeftGraphics(x, y, false); + + if (setMarker) { + Vec2i targetPos; + Vec2i screenPos(x, y - 60); + targetPos = getMouseCellPos(); + //Vec2i surfaceCellPos = map->toSurfCoords(targetPos); + + + MarkedCell + mc(targetPos, world.getThisFaction(), "none", + world.getThisFaction()->getStartLocationIndex()); + addOrReplaceInHighlightedCells(mc); + + GameNetworkInterface *gameNetworkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + gameNetworkInterface->sendHighlightCellMessage(mc.getTargetPos + (), + mc.getFaction + ()->getIndex + ()); + } + + if (originalIsMarkCellEnabled == true + && isMarkCellEnabled == true) { + Vec2i targetPos; + Vec2i screenPos(x, y - 60); + targetPos = getMouseCellPos(); + Vec2i surfaceCellPos = map->toSurfCoords(targetPos); + + MarkedCell + mc(targetPos, world.getThisFaction(), + "placeholder for note", + world.getThisFaction()->getStartLocationIndex()); + + //printf("#2 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); + + isMarkCellEnabled = false; + cellMarkedData = mc; + cellMarkedPos = surfaceCellPos; + isMarkCellTextEnabled = true; + chatManager.switchOnEdit(this, 500); + + //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); + Renderer::getInstance().forceQuadCacheUpdate(); + } + + if (originalIsUnMarkCellEnabled == true + && isUnMarkCellEnabled == true) { + Vec2i targetPos; + Vec2i screenPos(x, y - 35); + targetPos = getMouseCellPos(); + Vec2i surfaceCellPos = map->toSurfCoords(targetPos); + + // if(mapMarkedCellList.find(surfaceCellPos) != mapMarkedCellList.end()) { + // MarkedCell mc = mapMarkedCellList[surfaceCellPos]; + // if(mc.getFaction() == world.getThisFaction()) { + // mapMarkedCellList.erase(surfaceCellPos); + // GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); + // gameNetworkInterface->sendUnMarkCellMessage(mc.getTargetPos(),mc.getFaction()->getIndex()); + // } + // } + + isUnMarkCellEnabled = false; + removeCellMarker(surfaceCellPos, world.getThisFaction()); + //printf("#1 ADDED in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",surfaceCellPos.getString().c_str(),mapMarkedCellList.size()); + + //Renderer &renderer= Renderer::getInstance(); + //renderer.updateMarkedCellScreenPosQuadCache(surfaceCellPos); + Renderer::getInstance().forceQuadCacheUpdate(); + } + } + } + + //exit message box, has to be the last thing to do in this function + if (errorMessageBox.getEnabled() == true) { + if (errorMessageBox.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + //close message box + errorMessageBox.setEnabled(false); + } + } + if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + if (button == 0) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + if (networkManager.getGameNetworkInterface() != NULL) { + networkManager.getGameNetworkInterface()->quitGame(true); + } + quitTriggeredIndicator = true; + return; + } else { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + //close message box + mainMessageBox.setEnabled(false); + } + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::mouseDownRight(int x, int y) { + if (this->masterserverMode == true) { + return; + } + + if (currentUIState != NULL) { + currentUIState->mouseDownRight(x, y); + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + Logger::getInstance().handleMouseClick(x, y); + return; + } + + Map *map = world.getMap(); + const Metrics & metrics = Metrics::getInstance(); + + if (metrics.isInMinimap(x, y)) { + int xm = x - metrics.getMinimapX(); + int ym = y - metrics.getMinimapY(); + int + xCell = + static_cast < + int>(xm * + (static_cast < + float>(map->getW()) / metrics.getMinimapW())); + int + yCell = + static_cast < + int>(map->getH() - + ym * (static_cast < + float>(map->getH()) / metrics.getMinimapH())); + + if (map->isInside(xCell, yCell) + && map-> + isInsideSurface(map->toSurfCoords(Vec2i(xCell, yCell)))) { + gui.mouseDownRightGraphics(xCell, yCell, true); + } + } else { + Vec2i targetPos; + Vec2i screenPos(x, y); + targetPos = getMouseCellPos(); + if (isValidMouseCellPos() == true && + map->isInsideSurface(map->toSurfCoords(targetPos)) == true) { + gui.mouseDownRightGraphics(x, y, false); + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Error [%s] x = %d y = %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what(), x, y); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::mouseUpCenter(int x, int y) { + if (this->masterserverMode == true) { + return; + } + + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + + if (currentUIState != NULL) { + currentUIState->mouseUpCenter(x, y); + return; + } + + if (mouseMoved == false) { + gameCamera.setState(GameCamera::sGame); + } else { + mouseMoved = false; + } + } + + void Game::mouseUpLeft(int x, int y) { + if (this->masterserverMode == true) { + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + + if (currentUIState != NULL) { + currentUIState->mouseUpLeft(x, y); + return; + } + + gui.mouseUpLeftGraphics(x, y); + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::mouseDoubleClickLeft(int x, int y) { + if (this->masterserverMode == true) { + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + if (currentUIState != NULL) { + currentUIState->mouseDoubleClickLeft(x, y); + return; + } + + const Metrics & metrics = Metrics::getInstance(); + + if (metrics.isInMinimap(x, y)) { + // no double click on minimap + } else { + //display panel + if (metrics.isInDisplay(x, y) && !gui.isSelectingPos()) { + int xd = x - metrics.getDisplayX(); + int yd = y - metrics.getDisplayY(); + if (gui.mouseValid(xd, yd)) { + return; + } + } + //graphics panel + gui.mouseDoubleClickLeftGraphics(x, y); + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::mouseMove(int x, int y, const MouseState * ms) { + if (this->masterserverMode == true) { + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + if (currentUIState != NULL) { + currentUIState->mouseMove(x, y, ms); + return; + } + + popupMenu.mouseMove(x, y); + popupMenuSwitchTeams.mouseMove(x, y); + popupMenuDisconnectPlayer.mouseMove(x, y); + + const Metrics & metrics = Metrics::getInstance(); + + mouseX = x; + mouseY = y; + + if (ms->get(mbCenter)) { + mouseMoved = true; + if (currentCameraFollowUnit == NULL) { + float ymult = 0.2f; + float xmult = 0.2f; + + Vec2i oldPos = ::Shared::Platform::Window::getOldMousePos(); + int + oldx = + (oldPos.x * metrics.getVirtualW() / metrics.getScreenW()); + int + oldy = + ((metrics.getScreenH() - + oldPos.y) * metrics.getVirtualH() / metrics.getScreenH()); + lastMousePos.x = oldx; + lastMousePos.y = oldy; + gameCamera.transitionVH(-(y - oldy) * ymult, (oldx - x) * xmult); + } + mouseX = lastMousePos.x; + mouseY = lastMousePos.y; + ::Shared::Platform::Window::revertMousePos(); + + return; + } else if (currentCameraFollowUnit == NULL) { + //if(Window::isKeyDown() == false) + if (!camLeftButtonDown && !camRightButtonDown && !camUpButtonDown + && !camDownButtonDown) { + if (ms->get(mbLeft) && metrics.isInMinimap(x, y)) { + int xm = x - metrics.getMinimapX(); + int ym = y - metrics.getMinimapY(); + + Map *map = world.getMap(); + int + xCell = + static_cast < + int>(xm * + (static_cast < + float>(map->getW()) / metrics.getMinimapW())); + int + yCell = + static_cast < + int>(map->getH() - + ym * (static_cast < + float>(map->getH()) / metrics.getMinimapH())); + + if (map->isInside(xCell, yCell) + && map->isInsideSurface(map->toSurfCoords(Vec2i + (xCell, + yCell)))) { + if (gui.isSelectingPos()) { + gui.mouseDownLeftGraphics(xCell, yCell, true); + } else { + if (cameraDragAllowed == true) { + gameCamera.setPos(Vec2f + (static_cast (xCell), + static_cast (yCell))); + } + } + } + } else { + bool + mouseMoveScrollsWorld = + Config::getInstance().getBool("MouseMoveScrollsWorld", + "true"); + if (mouseMoveScrollsWorld == true) { + if (y < 10) { + gameCamera.setMoveZ(-scrollSpeed); + } else if (y > metrics.getVirtualH() - 10) { + gameCamera.setMoveZ(scrollSpeed); + } else { + gameCamera.setMoveZ(0); + } + + if (x < 10) { + gameCamera.setMoveX(-scrollSpeed); + } else if (x > metrics.getVirtualW() - 10) { + gameCamera.setMoveX(scrollSpeed); + } else { + gameCamera.setMoveX(0); + } + } + } + } + + if (switchTeamConfirmMessageBox.getEnabled() == true) { + switchTeamConfirmMessageBox.mouseMove(x, y); + } + + if (disconnectPlayerConfirmMessageBox.getEnabled() == true) { + disconnectPlayerConfirmMessageBox.mouseMove(x, y); + } + + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + if (errorMessageBox.getEnabled()) { + errorMessageBox.mouseMove(x, y); + } + if (scriptManager.getMessageBox()->getEnabled()) { + scriptManager.getMessageBox()->mouseMove(x, y); + } + //else if (saveBox) { + // saveBox->mouseMove(x, y); + //} else { + // //graphics + gui.mouseMoveGraphics(x, y); + //} + } + + //display + if (!gui.isSelecting() && !gui.isSelectingPos()) { + if (!gui.isSelectingPos()) { + if (metrics.isInDisplay(x, y)) { + gui.mouseMoveDisplay(x - metrics.getDisplayX(), + y - metrics.getDisplayY()); + } else { + gui.mouseMoveOutsideDisplay(); + } + } + } + + lastMousePos.x = mouseX; + lastMousePos.y = mouseY; + + Renderer & renderer = Renderer::getInstance(); + renderer.ccomputePosition(Vec2i(mouseX, mouseY), mouseCellPos); + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + bool Game::isValidMouseCellPos()const { + if (world.getMap() == NULL) { + return false; + } else { + return world.getMap()->isInside(mouseCellPos); + } + } + + void Game::eventMouseWheel(int x, int y, int zDelta) { + if (this->masterserverMode == true) { + return; + } + + if (currentUIState != NULL) { + currentUIState->eventMouseWheel(x, y, zDelta); + return; + } + + try { + gameCamera.zoom((float) zDelta / 60.0f); + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::startCameraFollowUnit() { + Selection *selection = gui.getSelectionPtr(); + if (selection->getCount() == 1) { + Unit *currentUnit = selection->getUnitPtr(0); + if (currentUnit != NULL) { + currentCameraFollowUnit = currentUnit; + getGameCameraPtr()->setState(GameCamera::sUnit); + getGameCameraPtr()-> + setPos(currentCameraFollowUnit->getCurrMidHeightVector()); + + int rotation = currentCameraFollowUnit->getRotation(); + getGameCameraPtr()->stop(); + getGameCameraPtr()->rotateToVH(0.0f, (540 - rotation) % 360); + getGameCameraPtr()->setHAng((540 - rotation) % 360); + getGameCameraPtr()->setVAng(0.0f); + } + } else { + if (currentCameraFollowUnit != NULL) { + currentCameraFollowUnit = NULL; + } + } + } + + bool Game::textInput(std::string text) { + if (chatManager.getEditEnabled() == true) { + return chatManager.textInput(text); + } + return false; + } + + bool Game::sdlKeyDown(SDL_KeyboardEvent key) { + if (this->masterserverMode == true) { + return false; + } + if (gameStarted == false || totalRenderFps <= 0) { + return false; + } + + if (chatManager.getEditEnabled() == true) { + return false; + } + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //group + for (int idx = 1; idx <= Selection::maxGroups; idx++) { + string keyName = "GroupUnitsKey" + intToStr(idx); + + SDL_Keycode groupHotKey = configKeys.getSDLKey(keyName.c_str()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] keyName [%s] group index = %d, key = [%c] [%d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, keyName.c_str(), idx, + groupHotKey, groupHotKey); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("input.keysym.mod = %d groupHotKey = %d key = %d (%d) [%s] isgroup = %d\n",key.keysym.mod,groupHotKey,key.keysym.sym,key.keysym.unicode,keyName.c_str(),isKeyPressed(groupHotKey,key)); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("input.keysym.mod = %d groupHotKey = %d key = (%d) [%s] isgroup = %d\n", + key.keysym.mod, groupHotKey, key.keysym.sym, keyName.c_str(), + isKeyPressed(groupHotKey, key)); + //printf(" group key check %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); + if (key.keysym.sym == groupHotKey) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + //printf("IS GROUP KEY %d scancode:%d sym:%d groupHotKey=%d \n",idx,key.keysym.scancode,key.keysym.sym,groupHotKey); + gui.groupKey(idx - 1); + return true; + } + } + return false; + } + + void Game::keyDown(SDL_KeyboardEvent key) { + if (this->masterserverMode == true) { + return; + } + + //printf("In game checking keypress for key [%d]\n",key.keysym.sym); + + try { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d] gameStarted [%d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, key.keysym.sym, key.keysym.sym, + gameStarted); + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + if (currentUIState != NULL) { + currentUIState->keyDown(key); + return; + } + + Lang & lang = Lang::getInstance(); + bool formerChatState = chatManager.getEditEnabled(); + //send key to the chat manager + chatManager.keyDown(key); + + if (formerChatState == false && chatManager.getEditEnabled()) { + camUpButtonDown = false; + camDownButtonDown = false; + camLeftButtonDown = false; + camRightButtonDown = false; + + gameCamera.stopMove(); + } + + //printf("GAME KEYDOWN #1\n"); + + if (chatManager.getEditEnabled() == false) { + //printf("GAME KEYDOWN #2\n"); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%d - %c]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] key = [%d - %c] pausegame [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key,key,configKeys.getCharKey("PauseGame")); + + //printf("SDL [%d] key [%d][%d]\n",configKeys.getSDLKey("SetMarker"),key.keysym.unicode,key.keysym.sym); + bool setMarkerKeyAllowsModifier = false; + if (configKeys.getSDLKey("SetMarker") == SDLK_RALT || + configKeys.getSDLKey("SetMarker") == SDLK_LALT) { + setMarkerKeyAllowsModifier = true; + } + + //printf("In game checking keypress for key [%d] camera left [%d]\n",key.keysym.sym,configKeys.getSDLKey("CameraModeLeft")); + + if (isKeyPressed + (configKeys.getSDLKey("RenderInGamePerformance"), key, + false) == true) { + renderInGamePerformance = !renderInGamePerformance; + + Config::getInstance().setBool("PerformanceWarningEnabled", + renderInGamePerformance, true); + } + //if(key == configKeys.getCharKey("RenderNetworkStatus")) { + else + if (isKeyPressed + (configKeys.getSDLKey("RenderNetworkStatus"), key, + false) == true) { + renderNetworkStatus = !renderNetworkStatus; + } + //else if(key == configKeys.getCharKey("ShowFullConsole")) { + else + if (isKeyPressed + (configKeys.getSDLKey("ShowFullConsole"), key, + false) == true) { + showFullConsole = true; + } else + if (isKeyPressed + (configKeys.getSDLKey("SetMarker"), key, + setMarkerKeyAllowsModifier) == true) { + setMarker = true; + printf("%d\n", key.keysym.scancode); + } + //else if(key == configKeys.getCharKey("TogglePhotoMode")) { + else + if (isKeyPressed + (configKeys.getSDLKey("TogglePhotoMode"), key, + false) == true) { + photoModeEnabled = !photoModeEnabled; + if (photoModeEnabled == true && + this->gameSettings.isNetworkGame() == false) { + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } else if (photoModeEnabled == false) { + gameCamera.setMaxHeight(-1); + } + + } + //Toggle Healthbars + else + if (isKeyPressed + (configKeys.getSDLKey("ToggleHealthbars"), key, + false) == true) { + switch (healthbarMode) { + case hbvUndefined: + healthbarMode = hbvOff; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsOff")); + break; + case hbvOff: + healthbarMode = hbvAlways; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsAlways")); + break; + case hbvAlways: + healthbarMode = hbvIfNeeded; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsIfNeeded")); + break; + case hbvIfNeeded: + healthbarMode = hbvSelected; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsSelected")); + break; + case hbvSelected: + healthbarMode = hbvSelected | hbvIfNeeded; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsSelectedOrNeeded")); + break; + case (hbvSelected | hbvIfNeeded): + healthbarMode = hbvUndefined; + console.addLine(lang.getString("Healthbar2") + ": " + + lang.getString("HealthbarsFactionDefault")); + break; + default: + printf + ("In [%s::%s Line: %d] Toggle Healthbars Hotkey - Invalid Value. Setting to default.\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + healthbarMode = hbvUndefined; + break; + } + } + //Toggle music + //else if(key == configKeys.getCharKey("ToggleMusic")) { + else + if (isKeyPressed + (configKeys.getSDLKey("ToggleMusic"), key, false) == true) { + + if (this->masterserverMode == false) { + Config & config = Config::getInstance(); + StrSound *gameMusic = + world.getThisFaction()->getType()->getMusic(); + if (gameMusic != NULL) { + float + configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + float currentVolume = gameMusic->getVolume(); + if (currentVolume > 0) { + gameMusic->setVolume(0); + console.addLine(lang.getString("GameMusic") + + " " + lang.getString("Off")); + } else { + //If the config says zero, use the default music volume + gameMusic->setVolume(configVolume ? configVolume : 0.9); + console.addLine(lang.getString("GameMusic")); + } + } + } + } + //move camera left + //else if(key == configKeys.getCharKey("CameraModeLeft")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraModeLeft"), key, false) == true) { + gameCamera.setMoveX(-1); + camLeftButtonDown = true; + } + //move camera right + //else if(key == configKeys.getCharKey("CameraModeRight")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraModeRight"), key, + false) == true) { + gameCamera.setMoveX(1); + camRightButtonDown = true; + } + //move camera up + //else if(key == configKeys.getCharKey("CameraModeUp")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraModeUp"), key, false) == true) { + gameCamera.setMoveZ(1); + camUpButtonDown = true; + } + //move camera down + //else if(key == configKeys.getCharKey("CameraModeDown")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraModeDown"), key, false) == true) { + gameCamera.setMoveZ(-1); + camDownButtonDown = true; + } + //change camera mode + //else if(key == configKeys.getCharKey("FreeCameraMode")) { + else + if (isKeyPressed + (configKeys.getSDLKey("FreeCameraMode"), key, false) == true) { + if (gameCamera.getState() == GameCamera::sFree) { + gameCamera.setState(GameCamera::sGame); + string + stateString = + gameCamera.getState() == + GameCamera:: + sGame ? lang.getString("GameCamera") : + lang.getString("FreeCamera"); + console.addLine(lang.getString("CameraModeSet") + " " + + stateString); + } else if (gameCamera.getState() == GameCamera::sGame) { + gameCamera.setState(GameCamera::sFree); + string + stateString = + gameCamera.getState() == + GameCamera:: + sGame ? lang.getString("GameCamera") : + lang.getString("FreeCamera"); + console.addLine(lang.getString("CameraModeSet") + " " + + stateString); + } + //else ignore! + } + //reset camera mode to normal + //else if(key == configKeys.getCharKey("ResetCameraMode")) { + else + if (isKeyPressed + (configKeys.getSDLKey("ResetCameraMode"), key, + false) == true) { + if (currentCameraFollowUnit != NULL) { + currentCameraFollowUnit = NULL; + } + gameCamera.setState(GameCamera::sGame); + } + //pause + //else if(key == configKeys.getCharKey("PauseGame")) { + else + if (isKeyPressed + (configKeys.getSDLKey("PauseGame"), key, false) == true) { + //printf("Toggle pause paused = %d\n",paused); + //setPaused(!paused); + + bool allowAdminMenuItems = false; + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + if (role == nrServer) { + allowAdminMenuItems = true; + } else if (role == nrClient) { + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + + if (clientInterface != NULL && + gameSettings.getMasterserver_admin() == + clientInterface->getSessionKey()) { + allowAdminMenuItems = true; + } + } + + if (allowAdminMenuItems) { + if (getPaused() == false) { + commander.tryPauseGame(false, false); + } else { + commander.tryResumeGame(false, false); + } + } + } else + if (isKeyPressed + (configKeys.getSDLKey("ExtraTeamColorMarker"), key, + false) == true) { + //printf("Toggle ExtraTeamColorMarker\n"); + toggleTeamColorMarker(); + } + //switch display color + //else if(key == configKeys.getCharKey("ChangeFontColor")) { + else + if (isKeyPressed + (configKeys.getSDLKey("ChangeFontColor"), key, + false) == true) { + gui.switchToNextDisplayColor(); + } + //increment speed + //else if(key == configKeys.getCharKey("GameSpeedIncrease")) { + else + if (isKeyPressed + (configKeys.getSDLKey("GameSpeedIncrease"), key, + false) == true) { + bool + speedChangesAllowed = + !NetworkManager:: + getInstance().isNetworkGameWithConnectedClients(); + if (speedChangesAllowed) { + incSpeed(); + } + } + //decrement speed + //else if(key == configKeys.getCharKey("GameSpeedDecrease")) { + else + if (isKeyPressed + (configKeys.getSDLKey("GameSpeedDecrease"), key, + false) == true) { + bool + speedChangesAllowed = + !NetworkManager:: + getInstance().isNetworkGameWithConnectedClients(); + if (speedChangesAllowed) { + decSpeed(); + } + } else + if (isKeyPressed + (configKeys.getSDLKey("BookmarkAdd"), key, false) == true) { + startMarkCell(); + } else + if (isKeyPressed + (configKeys.getSDLKey("BookmarkRemove"), key, false) == true) { + isUnMarkCellEnabled = true; + } else + if (isKeyPressed + (configKeys.getSDLKey("CameraFollowSelectedUnit"), key, + false) == true) { + startCameraFollowUnit(); + } + //exit + else + if (isKeyPressed(configKeys.getSDLKey("ExitKey"), key, false) + == true) { + popupMenu.setEnabled(!popupMenu.getEnabled()); + popupMenu.setVisible(popupMenu.getEnabled()); + } + + //hotkeys + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] gameCamera.getState() = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + gameCamera.getState()); + + if (gameCamera.getState() != GameCamera::sFree) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, key); + + gui.hotKey(key); + } else { + //rotate camera leftt + //if(key == configKeys.getCharKey("CameraRotateLeft")) { + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateLeft"), key) == true) { + gameCamera.setRotate(-1); + } + //rotate camera right + //else if(key == configKeys.getCharKey("CameraRotateRight")){ + else + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateRight"), key) == true) { + gameCamera.setRotate(1); + } + //camera up + //else if(key == configKeys.getCharKey("CameraRotateUp")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateUp"), key) == true) { + gameCamera.setMoveY(1); + } + //camera down + //else if(key == configKeys.getCharKey("CameraRotateDown")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateDown"), key) == true) { + gameCamera.setMoveY(-1); + } + } + + if (isKeyPressed(configKeys.getSDLKey("SaveGame"), key) == true) { + saveGame(); + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } + + void Game::keyUp(SDL_KeyboardEvent key) { + if (this->masterserverMode == true) { + return; + } + + try { + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + if (currentUIState != NULL) { + currentUIState->keyUp(key); + return; + } + + if (chatManager.getEditEnabled()) { + //send key to the chat manager + chatManager.keyUp(key); + } else { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + //if(key == configKeys.getCharKey("ShowFullConsole")) { + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) + == true) { + showFullConsole = false; + } else if (isKeyPressed(configKeys.getSDLKey("SetMarker"), key) == + true #ifdef WIN32 - || key.keysym.scancode == 5 + || key.keysym.scancode == 5 #endif - ) - { - setMarker = false; - } - //else if(key == configKeys.getCharKey("CameraRotateLeft") || - // key == configKeys.getCharKey("CameraRotateRight")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateLeft"), key) == true - || isKeyPressed (configKeys.getSDLKey ("CameraRotateRight"), - key) == true) - { - gameCamera.setRotate (0); - } - //else if(key == configKeys.getCharKey("CameraRotateDown") || - // key == configKeys.getCharKey("CameraRotateUp")) { - else - if (isKeyPressed - (configKeys.getSDLKey ("CameraRotateDown"), key) == true - || isKeyPressed (configKeys.getSDLKey ("CameraRotateUp"), - key) == true) - { + ) { + setMarker = false; + } + //else if(key == configKeys.getCharKey("CameraRotateLeft") || + // key == configKeys.getCharKey("CameraRotateRight")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateLeft"), key) == true + || isKeyPressed(configKeys.getSDLKey("CameraRotateRight"), + key) == true) { + gameCamera.setRotate(0); + } + //else if(key == configKeys.getCharKey("CameraRotateDown") || + // key == configKeys.getCharKey("CameraRotateUp")) { + else + if (isKeyPressed + (configKeys.getSDLKey("CameraRotateDown"), key) == true + || isKeyPressed(configKeys.getSDLKey("CameraRotateUp"), + key) == true) { - gameCamera.setMoveY (0); - } - //else if(key == configKeys.getCharKey("CameraModeUp")){ - else if (isKeyPressed (configKeys.getSDLKey ("CameraModeUp"), key) - == true) - { - gameCamera.setMoveZ (0); - camUpButtonDown = false; - calcCameraMoveZ (); - } - //else if(key == configKeys.getCharKey("CameraModeDown")){ - else - if (isKeyPressed (configKeys.getSDLKey ("CameraModeDown"), key) - == true) - { - gameCamera.setMoveZ (0); - camDownButtonDown = false; - calcCameraMoveZ (); - } - //else if(key == configKeys.getCharKey("CameraModeLeft")){ - else - if (isKeyPressed (configKeys.getSDLKey ("CameraModeLeft"), key) - == true) - { - gameCamera.setMoveX (0); - camLeftButtonDown = false; - calcCameraMoveX (); - } - //else if(key == configKeys.getCharKey("CameraModeRight")){ - else - if (isKeyPressed (configKeys.getSDLKey ("CameraModeRight"), key) - == true) - { - gameCamera.setMoveX (0); - camRightButtonDown = false; - calcCameraMoveX (); - } - } - } - catch (const exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); + gameCamera.setMoveY(0); + } + //else if(key == configKeys.getCharKey("CameraModeUp")){ + else if (isKeyPressed(configKeys.getSDLKey("CameraModeUp"), key) + == true) { + gameCamera.setMoveZ(0); + camUpButtonDown = false; + calcCameraMoveZ(); + } + //else if(key == configKeys.getCharKey("CameraModeDown")){ + else + if (isKeyPressed(configKeys.getSDLKey("CameraModeDown"), key) + == true) { + gameCamera.setMoveZ(0); + camDownButtonDown = false; + calcCameraMoveZ(); + } + //else if(key == configKeys.getCharKey("CameraModeLeft")){ + else + if (isKeyPressed(configKeys.getSDLKey("CameraModeLeft"), key) + == true) { + gameCamera.setMoveX(0); + camLeftButtonDown = false; + calcCameraMoveX(); + } + //else if(key == configKeys.getCharKey("CameraModeRight")){ + else + if (isKeyPressed(configKeys.getSDLKey("CameraModeRight"), key) + == true) { + gameCamera.setMoveX(0); + camRightButtonDown = false; + calcCameraMoveX(); + } + } + } catch (const exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.getGameNetworkInterface () != NULL) - { - GameNetworkInterface *networkInterface = - NetworkManager::getInstance ().getGameNetworkInterface (); - networkInterface->sendTextMessage (szBuf, -1, true, ""); - sleep (10); - networkManager.getGameNetworkInterface ()->quitGame (true); - } - ErrorDisplayMessage (ex.what (), true); - } - } + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.getGameNetworkInterface() != NULL) { + GameNetworkInterface *networkInterface = + NetworkManager::getInstance().getGameNetworkInterface(); + networkInterface->sendTextMessage(szBuf, -1, true, ""); + sleep(10); + networkManager.getGameNetworkInterface()->quitGame(true); + } + ErrorDisplayMessage(ex.what(), true); + } + } - void Game::calcCameraMoveX () - { - //move camera left - if (camLeftButtonDown == true) - { - gameCamera.setMoveX (-1); - } - //move camera right - else if (camRightButtonDown == true) - { - gameCamera.setMoveX (1); - } - } - void Game::calcCameraMoveZ () - { - //move camera up - if (camUpButtonDown == true) - { - gameCamera.setMoveZ (1); - } - //move camera down - else if (camDownButtonDown == true) - { - gameCamera.setMoveZ (-1); - } + void Game::calcCameraMoveX() { + //move camera left + if (camLeftButtonDown == true) { + gameCamera.setMoveX(-1); + } + //move camera right + else if (camRightButtonDown == true) { + gameCamera.setMoveX(1); + } + } + void Game::calcCameraMoveZ() { + //move camera up + if (camUpButtonDown == true) { + gameCamera.setMoveZ(1); + } + //move camera down + else if (camDownButtonDown == true) { + gameCamera.setMoveZ(-1); + } - } + } - void Game::keyPress (SDL_KeyboardEvent c) - { - if (this->masterserverMode == true) - { - return; - } + void Game::keyPress(SDL_KeyboardEvent c) { + if (this->masterserverMode == true) { + return; + } - if (gameStarted == false || totalRenderFps <= 0) - { - return; - } - if (currentUIState != NULL) - { - currentUIState->keyPress (c); - return; - } + if (gameStarted == false || totalRenderFps <= 0) { + return; + } + if (currentUIState != NULL) { + currentUIState->keyPress(c); + return; + } - chatManager.keyPress (c); - } + chatManager.keyPress(c); + } - Stats Game::getEndGameStats () - { - Stats endStats; - endStats = *(world.getStats ()); - //NetworkManager &networkManager= NetworkManager::getInstance(); - if (this->masterserverMode == true) - { - endStats.setIsMasterserverMode (true); - } - return endStats; - } + Stats Game::getEndGameStats() { + Stats endStats; + endStats = *(world.getStats()); + //NetworkManager &networkManager= NetworkManager::getInstance(); + if (this->masterserverMode == true) { + endStats.setIsMasterserverMode(true); + } + return endStats; + } - Stats Game::quitGame () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + Stats Game::quitGame() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); - if (quitGameCalled == true) - { - Stats endStats = getEndGameStats (); - return endStats; - } - quitGameCalled = true; + if (quitGameCalled == true) { + Stats endStats = getEndGameStats(); + return endStats; + } + quitGameCalled = true; - NetworkManager & networkManager = NetworkManager::getInstance (); - NetworkRole role = networkManager.getNetworkRole (); - string suffix = "_client"; - if (role == nrServer) - { - suffix = "_server"; - } - this->DumpCRCWorldLogIfRequired (suffix); + NetworkManager & networkManager = NetworkManager::getInstance(); + NetworkRole role = networkManager.getNetworkRole(); + string suffix = "_client"; + if (role == nrServer) { + suffix = "_server"; + } + this->DumpCRCWorldLogIfRequired(suffix); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled == true) - { - world.DumpWorldToLog (); - } - //printf("Check savegame\n"); - //printf("Saving...\n"); - if (Config::getInstance ().getBool ("AutoTest")) - { - this->saveGame (GameConstants::saveGameFileAutoTestDefault); - } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled == true) { + world.DumpWorldToLog(); + } + //printf("Check savegame\n"); + //printf("Saving...\n"); + if (Config::getInstance().getBool("AutoTest")) { + this->saveGame(GameConstants::saveGameFileAutoTestDefault); + } - Stats endStats = getEndGameStats (); + Stats endStats = getEndGameStats(); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); - //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - NetworkManager::getInstance ().end (); - //sleep(0); + //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + NetworkManager::getInstance().end(); + //sleep(0); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); - return endStats; - } + return endStats; + } - void Game::DumpCRCWorldLogIfRequired (string fileSuffix) - { - bool isNetworkGame = this->gameSettings.isNetworkGame (); - if (isNetworkGame == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Check save world CRC to log. isNetworkGame = %d fileSuffix = %s\n", - isNetworkGame, fileSuffix.c_str ()); + void Game::DumpCRCWorldLogIfRequired(string fileSuffix) { + bool isNetworkGame = this->gameSettings.isNetworkGame(); + if (isNetworkGame == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Check save world CRC to log. isNetworkGame = %d fileSuffix = %s\n", + isNetworkGame, fileSuffix.c_str()); - GameSettings *settings = world.getGameSettingsPtr (); - if (settings != NULL && - (isFlagType1BitEnabled (ft1_network_synch_checks_verbose) == - true - || isFlagType1BitEnabled (ft1_network_synch_checks) == true)) - { - string - debugCRCWorldLogFile = - Config::getInstance ().getString ("DebugCRCWorldLogFile", - "debugCRCWorld.log"); - debugCRCWorldLogFile += fileSuffix; + GameSettings *settings = world.getGameSettingsPtr(); + if (settings != NULL && + (isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == + true + || isFlagType1BitEnabled(ft1_network_synch_checks) == true)) { + string + debugCRCWorldLogFile = + Config::getInstance().getString("DebugCRCWorldLogFile", + "debugCRCWorld.log"); + debugCRCWorldLogFile += fileSuffix; - if (getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) != "") - { - debugCRCWorldLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugCRCWorldLogFile; - } - else - { - string - userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - debugCRCWorldLogFile = userData + debugCRCWorldLogFile; - } + if (getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) != "") { + debugCRCWorldLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugCRCWorldLogFile; + } else { + string + userData = + Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + debugCRCWorldLogFile = userData + debugCRCWorldLogFile; + } - printf ("Save to log debugCRCWorldLogFile = %s\n", - debugCRCWorldLogFile.c_str ()); + printf("Save to log debugCRCWorldLogFile = %s\n", + debugCRCWorldLogFile.c_str()); #if defined(WIN32) && !defined(__MINGW32__) - FILE *fp = - _wfopen (utf8_decode (debugCRCWorldLogFile).c_str (), L"w"); - std::ofstream logFile (fp); + FILE *fp = + _wfopen(utf8_decode(debugCRCWorldLogFile).c_str(), L"w"); + std::ofstream logFile(fp); #else - std::ofstream logFile; - logFile.open (debugCRCWorldLogFile.c_str (), - ios_base::out | ios_base::trunc); + std::ofstream logFile; + logFile.open(debugCRCWorldLogFile.c_str(), + ios_base::out | ios_base::trunc); #endif - logFile << "World CRC debug information:" << std::endl; - logFile << "============================" << std::endl; - logFile << "Software version: " << glestVersionString << "-" << - getCompilerNameString () << std::endl; - logFile << "Maximum framecount: " << world. - getFaction (0)->getCRC_DetailsForWorldFrameCount () << std::endl; + logFile << "World CRC debug information:" << std::endl; + logFile << "============================" << std::endl; + logFile << "Software version: " << glestVersionString << "-" << + getCompilerNameString() << std::endl; + logFile << "Maximum framecount: " << world. + getFaction(0)->getCRC_DetailsForWorldFrameCount() << std::endl; - for (unsigned int worldFrameIndex = 0; - worldFrameIndex < - world.getFaction (0)->getCRC_DetailsForWorldFrameCount (); - ++worldFrameIndex) - { - //factions (and their related info) - for (int i = 0; i < world.getFactionCount (); ++i) - { - logFile << "Faction detail for index: " << i << std::endl; - logFile << "--------------------------" << std::endl; + for (unsigned int worldFrameIndex = 0; + worldFrameIndex < + world.getFaction(0)->getCRC_DetailsForWorldFrameCount(); + ++worldFrameIndex) { + //factions (and their related info) + for (int i = 0; i < world.getFactionCount(); ++i) { + logFile << "Faction detail for index: " << i << std::endl; + logFile << "--------------------------" << std::endl; - std::pair < int, - string > - details = - world. - getFaction (i)->getCRC_DetailsForWorldFrameIndex - (worldFrameIndex); + std::pair < int, + string > + details = + world. + getFaction(i)->getCRC_DetailsForWorldFrameIndex + (worldFrameIndex); - logFile << string ("** world frame: ") << details.first << std:: - endl; - logFile << details.second << std::endl; - } - } + logFile << string("** world frame: ") << details.first << std:: + endl; + logFile << details.second << std::endl; + } + } - logFile.close (); + logFile.close(); #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - { - fclose (fp); - } + if (fp) { + fclose(fp); + } #endif - } - } - } - - void saveStatsToSteam (Game * game, Stats & endStats) - { - Steam *steamInstance = - CacheManager::getCachedItem < - Steam * >(GameConstants::steamCacheInstanceKey); - if (steamInstance != NULL) - { - printf ("\nSTEAM detected, writing out end game stats for player!\n"); - - // Write out stats here - if (NetworkManager::getInstance ().isNetworkGame ()) - { - //printf("\nSTEAM Refresh Stats!\n"); - steamInstance->requestRefreshStats (); - for (int factionIndex = 0; - factionIndex < game->getWorld ()->getFactionCount (); - ++factionIndex) - { - if (factionIndex == game->getWorld ()->getThisFactionIndex ()) - { - //printf("\nWriting out game stats for Faction Index: %d!\n",factionIndex); - if (endStats.getVictory (factionIndex)) - { - steamInstance->setStatAsInt (EnumParser < - SteamStatName >::getString - (stat_online_wins).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName >::getString - (stat_online_wins).c_str ()) + - 1); - } - else - { - steamInstance->setStatAsInt (EnumParser < - SteamStatName >::getString - (stat_online_loses).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName >::getString - (stat_online_loses).c_str ()) + - 1); - } - steamInstance->setStatAsInt (EnumParser < - SteamStatName >::getString - (stat_online_kills).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_kills).c_str ()) + - endStats.getKills (factionIndex)); - steamInstance->setStatAsInt (EnumParser < - SteamStatName - >::getString - (stat_online_kills_enemy).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_kills_enemy).c_str - ()) + - endStats.getEnemyKills - (factionIndex)); - steamInstance->setStatAsInt (EnumParser < - SteamStatName - >::getString - (stat_online_deaths).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_deaths).c_str ()) + - endStats.getDeaths (factionIndex)); - steamInstance->setStatAsInt (EnumParser < - SteamStatName - >::getString - (stat_online_units).c_str (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_units).c_str ()) + - endStats.getUnitsProduced - (factionIndex)); - steamInstance->setStatAsInt (EnumParser < - SteamStatName - >::getString - (stat_online_resources_harvested).c_str - (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_resources_harvested).c_str - ()) + - endStats.getResourcesHarvested - (factionIndex)); - if (endStats.getPlayerLeftBeforeEnd (factionIndex)) - { - steamInstance->setStatAsInt (EnumParser < - SteamStatName >::getString - (stat_online_quit_before_end).c_str - (), - steamInstance->getStatAsInt - (EnumParser < - SteamStatName - >::getString - (stat_online_quit_before_end).c_str - ()) + 1); - } - steamInstance->setStatAsDouble (EnumParser < - SteamStatName >::getString - (stat_online_minutes_played).c_str - (), - steamInstance->getStatAsDouble - (EnumParser < - SteamStatName - >::getString - (stat_online_minutes_played).c_str - ()) + - getTimeDuationMinutes - (endStats.getFramesToCalculatePlaytime - (), GameConstants::updateFps)); - } - } - } - - // Write out achievements here - for (int factionIndex = 0; - factionIndex < game->getWorld ()->getFactionCount (); - ++factionIndex) - { - if (factionIndex == game->getWorld ()->getThisFactionIndex ()) - { - //printf("\nWriting out game stats for Faction Index: %d won status: %d\n",factionIndex,endStats.getVictory(factionIndex)); - if (endStats.getVictory (factionIndex)) - { - if (steamInstance->isUnlocked (EnumParser < - SteamAchievementName - >::getString - (ACH_WIN_ONE_GAME).c_str ()) == - false) - { - steamInstance->unlock (EnumParser < - SteamAchievementName - >::getString (ACH_WIN_ONE_GAME).c_str - ()); - } - if (NetworkManager::getInstance ().isNetworkGame ()) - { - if (steamInstance->isUnlocked (EnumParser < - SteamAchievementName - >::getString - (ACH_WIN_ONE_GAME_ONLINE).c_str - ()) == false) - { - steamInstance->unlock (EnumParser < - SteamAchievementName >::getString - (ACH_WIN_ONE_GAME_ONLINE).c_str ()); - } - } - } - } - } - - //printf("\nSTEAM Store Stats!\n"); - steamInstance->storeStats (); - //printf("\nSTEAM Refresh Stats!\n"); - steamInstance->requestRefreshStats (); - } - } - - void Game::exitGameState (Program * program, Stats & endStats) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Game *game = dynamic_cast < Game * >(program->getState ()); - - //printf("game = %p\n",game); - - if (game) - { - game->setEndGameTeamWinnersAndLosers (); - game->endGame (); - } - - if ((game != NULL && game->isMasterserverMode () == true) || - Config::getInstance ().getBool ("AutoTest") == true) - { - printf ("Game ending with stats:\n"); - printf ("-----------------------\n"); - - string gameStats = endStats.getStats (); - printf ("%s", gameStats.c_str ()); - - printf ("-----------------------\n"); - } - - saveStatsToSteam (game, endStats); - - ProgramState *newState = new BattleEnd (program, &endStats, game); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - program->setState (newState, false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - -// ==================== PRIVATE ==================== - - void - Game::highlightUnit (int unitId, float radius, float thickness, - Vec4f color) - { - HighlightSpecialUnitInfo info; - info.radius = radius; - info.thickness = thickness; - info.color = color; - unitHighlightList[unitId] = info; - } - - void Game::unhighlightUnit (int unitId) - { - unitHighlightList.erase (unitId); - } - -// ==================== render ==================== - - void Game::render3d () - { - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - - Renderer & renderer = Renderer::getInstance (); - - //init - renderer.reset3d (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [reset3d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -// renderer.computeVisibleQuad(); -// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [computeVisibleQuad]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); -// if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - renderer.loadGameCameraMatrix (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [loadGameCameraMatrix]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - renderer.computeVisibleQuad (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [computeVisibleQuad]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - renderer.setupLighting (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [setupLighting]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //shadow map - renderer.renderShadowsToTexture (avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderShadowsToTexture]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //clear buffers - renderer.clearBuffers (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //surface - renderer.renderSurface (avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSurface]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //selection circles - renderer.renderSelectionEffects (healthbarMode); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSelectionEffects]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - // renderTeamColorCircle - if ((renderExtraTeamColor & renderTeamColorCircleBit) > 0) - { - renderer.renderTeamColorCircle (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, renderFps, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - chrono.start (); - } - - renderer.renderSpecialHighlightUnits (unitHighlightList); - - // renderTeamColorCircle - renderer.renderMorphEffects (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //objects - renderer.renderObjects (avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //ground units - renderer.renderUnits (false, avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //water - renderer.renderWater (); - renderer.renderWaterEffects (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderWater]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //air units - renderer.renderUnits (true, avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //particles - renderer.renderParticleManager (rsGame); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderParticleManager]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - //renderOnTopBars (aka Healthbars) - if (photoModeEnabled == false) - { - renderer.renderHealthBars (healthbarMode); - } - - // renderTeamColorPlane - if ((renderExtraTeamColor & renderTeamColorPlaneBit) > 0) - { - renderer.renderTeamColorPlane (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, renderFps, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled && chrono.getMillis () > 0) - chrono.start (); - } - - //mouse 3d - renderer.renderMouse3d (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderMouse3d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - - renderer.renderUnitsToBuild (avgRenderFps); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnitsToBuild]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - renderFps, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - renderer.setLastRenderFps (lastRenderFps); - } - - void Game::updateWorldStats () - { - world.getStats ()->setWorldTimeElapsed (world. - getTimeFlow ()->getTime ()); - - if (difftime ((long int) time (NULL), lastMaxUnitCalcTime) >= 1) - { - lastMaxUnitCalcTime = time (NULL); - - int totalUnitcount = 0; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - totalUnitcount += faction->getUnitCount (); - } - - if (world.getStats ()->getMaxConcurrentUnitCount () < totalUnitcount) - { - world.getStats ()->setMaxConcurrentUnitCount (totalUnitcount); - } - world. - getStats ()->setTotalEndGameConcurrentUnitCount (totalUnitcount); - world.getStats ()->setFramesPlayed (world.getFrameCount ()); - } - } - - string Game::getDebugStats (std::map < int, string > &factionDebugInfo) - { - string str = ""; - - if (this->masterserverMode == false) - { - str += - "MouseXY: " + intToStr (mouseX) + "," + intToStr (mouseY) + "\n"; - - if (world. - getMap ()->isInsideSurface (world. - getMap ()->toSurfCoords - (mouseCellPos)) == true) - { - str += - "MouseXY cell coords: " + intToStr (mouseCellPos.x) + "," + - intToStr (mouseCellPos.y) + "\n"; - } - - str += - "PosObjWord: " + intToStr (gui.getPosObjWorld ().x) + "," + - intToStr (gui.getPosObjWorld ().y) + "\n"; - } - - str += - "Render FPS: " + intToStr (lastRenderFps) + "[" + - intToStr (avgRenderFps) + "]\n"; - str += - "Update FPS: " + intToStr (lastUpdateFps) + "[" + - intToStr (avgUpdateFps) + "]\n"; - - if (this->masterserverMode == false) - { - str += - "GameCamera pos: " + floatToStr (gameCamera.getPos ().x) + "," + - floatToStr (gameCamera.getPos ().y) + "," + - floatToStr (gameCamera.getPos ().z) + "\n"; - //str+= "Cached surfacedata: " + intToStr(renderer.getCachedSurfaceDataSize())+"\n"; - } - - //intToStr(stats.getFramesToCalculatePlaytime()/GameConstants::updateFps/60 - str += - "Time: " + floatToStr (world.getTimeFlow ()->getTime (), - 2) + " [" + - floatToStr ((double) world. - getStats ()->getFramesToCalculatePlaytime () / - (float) GameConstants::updateFps / 60.0, 2) + "]\n"; - - if (SystemFlags::getThreadedLoggerRunning () == true) - { - str += - "Log buffer count: " + - intToStr (SystemFlags::getLogEntryBufferCount ()) + "\n"; - } - - str += - "UnitRangeCellsLookupItemCache: " + - world.getUnitUpdater ()->getUnitRangeCellsLookupItemCacheStats () + - "\n"; - str += - "ExploredCellsLookupItemCache: " + - world.getExploredCellsLookupItemCacheStats () + "\n"; - str += - "FowAlphaCellsLookupItemCache: " + - world.getFowAlphaCellsLookupItemCacheStats () + "\n"; - - const string - selectionType = - toLower (Config:: - getInstance ().getString ("SelectionType", - Config::colorPicking)); - str += "Selection type: " + toLower (selectionType) + "\n"; - - if (selectionType == Config::colorPicking) - { - str += - "Color picking used color list size: " + - intToStr (BaseColorPickEntity::getUsedColorIDListSize ()) + "\n"; - } - - //str+= "AllFactionsCacheStats: " + world.getAllFactionsCacheStats()+"\n"; - //str+= "AttackWarningCount: " + intToStr(world.getUnitUpdater()->getAttackWarningCount()) + "\n"; - - str += "Map: " + gameSettings.getMap () + "\n"; - str += "Tileset: " + gameSettings.getTileset () + "\n"; - str += "Techtree: " + gameSettings.getTech () + "\n"; - - if (this->masterserverMode == false) - { - Renderer & renderer = Renderer::getInstance (); - str += - "Triangle count: " + intToStr (renderer.getTriangleCount ()) + "\n"; - str += "Vertex count: " + intToStr (renderer.getPointCount ()) + "\n"; - } - - str += "Frame count:" + intToStr (world.getFrameCount ()) + "\n"; - - //visible quad - if (this->masterserverMode == false) - { - Renderer & renderer = Renderer::getInstance (); - Quad2i visibleQuad = renderer.getVisibleQuad (); - //Quad2i visibleQuadCamera= renderer.getVisibleQuadFromCamera(); - - str += "Visible quad: "; - for (int i = 0; i < 4; ++i) - { - str += - "(" + intToStr (visibleQuad.p[i].x) + "," + - intToStr (visibleQuad.p[i].y) + ") "; - } - // str+= "\n"; - // str+= "Visible quad camera: "; - // for(int i= 0; i<4; ++i){ - // str+= "(" + intToStr(visibleQuadCamera.p[i].x) + "," +intToStr(visibleQuadCamera.p[i].y) + ") "; - // } - str += "\n"; - - str += - "Visible quad area: " + floatToStr (visibleQuad.area ()) + - "\n"; - // str+= "Visible quad camera area: " + floatToStr(visibleQuadCamera.area()) +"\n"; - - // Rect2i boundingRect= visibleQuad.computeBoundingRect(); - // Rect2i scaledRect= boundingRect/Map::cellScale; - // scaledRect.clamp(0, 0, world.getMap()->getSurfaceW()-1, world.getMap()->getSurfaceH()-1); - // renderer.renderText3D("#1", coreData.getMenuFontNormal3D(), Vec3f(1.0f), scaledRect.p[0].x, scaledRect.p[0].y, false); - // renderer.renderText3D("#2", coreData.getMenuFontNormal3D(), Vec3f(1.0f), scaledRect.p[1].x, scaledRect.p[1].y, false); - } - - int totalUnitcount = 0; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - totalUnitcount += faction->getUnitCount (); - } - - if (this->masterserverMode == false) - { - Renderer & renderer = Renderer::getInstance (); - VisibleQuadContainerCache & qCache = renderer.getQuadCache (); - int visibleUnitCount = (int) qCache.visibleQuadUnitList.size (); - str += - "Visible unit count: " + intToStr (visibleUnitCount) + - " total: " + intToStr (totalUnitcount) + "\n"; - - int visibleObjectCount = (int) qCache.visibleObjectList.size (); - str += - "Visible object count: " + intToStr (visibleObjectCount) + "\n"; - } - else - { - str += "Total unit count: " + intToStr (totalUnitcount) + "\n"; - } - - // resources - for (int i = 0; i < world.getFactionCount (); ++i) - { - string factionInfo = this->gameSettings.getNetworkPlayerName (i); - //factionInfo += " [" + this->gameSettings.getNetworkPlayerUUID(i) + "]"; - float multi = world.getStats ()->getResourceMultiplier (i); - string multiplier = "[" + floatToStr (multi, 1) + "]"; - switch (this->gameSettings.getFactionControl (i)) - { - case ctCpuEasy: - factionInfo += " CPU Easy" + multiplier; - break; - case ctCpu: - factionInfo += " CPU Normal" + multiplier; - break; - case ctCpuUltra: - factionInfo += " CPU Ultra" + multiplier; - break; - case ctCpuZeta: - factionInfo += " CPU Zeta" + multiplier; - break; - } - - factionInfo += - " [" + formatString (this->gameSettings.getFactionTypeName (i)) + - " team: " + intToStr (this->gameSettings.getTeam (i)) + "]"; - -// bool showResourceDebugInfo = true; -// if(showResourceDebugInfo == true) { -// factionInfo +=" res: "; -// for(int j = 0; j < world.getTechTree()->getResourceTypeCount(); ++j) { -// factionInfo += world.getFaction(i)->getResource(j)->getType()->getName()+":"+intToStr(world.getFaction(i)->getResource(j)->getAmount()); -// factionInfo += " "; -// } -// } - - factionDebugInfo[i] = factionInfo; - } - - return str; - } - - void Game::render2d () - { - Renderer & renderer = Renderer::getInstance (); - //Config &config= Config::getInstance(); - CoreData & coreData = CoreData::getInstance (); - - //init - renderer.reset2d (); - - //HUD - if (visibleHUD == true && photoModeEnabled == false) - { - renderer.renderHud (); - } - //display - renderer.renderDisplay (); - - //minimap - if (photoModeEnabled == false) - { - renderer.renderMinimap (); - } - - renderer.renderVisibleMarkedCells (); - renderer.renderVisibleMarkedCells (true, lastMousePos.x, - lastMousePos.y); - - //selection - renderer.renderSelectionQuad (); - - //highlighted Cells - renderer.renderHighlightedCellsOnMinimap (); - - if (switchTeamConfirmMessageBox.getEnabled () == true) - { - renderer.renderMessageBox (&switchTeamConfirmMessageBox); - } - - if (disconnectPlayerConfirmMessageBox.getEnabled () == true) - { - renderer.renderMessageBox (&disconnectPlayerConfirmMessageBox); - } - - //exit message box - if (errorMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&errorMessageBox); - } - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - - //script message box - if (mainMessageBox.getEnabled () == false && - errorMessageBox.getEnabled () == false && - scriptManager.getMessageBoxEnabled ()) - { - renderer.renderMessageBox (scriptManager.getMessageBox ()); - } - - //script display text - if (!scriptManager.getDisplayText ().empty () - && !scriptManager.getMessageBoxEnabled ()) - { - Vec4f fontColor = getGui ()->getDisplay ()->getColor (); - - if (Renderer::renderText3DEnabled == true) - { - renderer.renderText3D (scriptManager.getDisplayText (), - coreData.getMenuFontNormal3D (), - Vec3f (fontColor.x, fontColor.y, - fontColor.z), 200, 660, false); - } - else - { - renderer.renderText (scriptManager.getDisplayText (), - coreData.getMenuFontNormal (), - Vec3f (fontColor.x, fontColor.y, - fontColor.z), 200, 660, false); - } - } - - renderVideoPlayer (); - - renderer.renderPopupMenu (&popupMenu); - renderer.renderPopupMenu (&popupMenuSwitchTeams); - renderer.renderPopupMenu (&popupMenuDisconnectPlayer); - - if (program != NULL) - program->renderProgramMsgBox (); - - renderer.renderChatManager (&chatManager); - - //debug info - bool perfLogging = false; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled == true - || SystemFlags::getSystemSettingType (SystemFlags:: - debugWorldSynch).enabled == - true) - { - perfLogging = true; - } - - string str = ""; - std::map < int, string > factionDebugInfo; - - if (renderer.getShowDebugUI () == true || - (perfLogging == true - && difftime ((long int) time (NULL), lastRenderLog2d) >= 1)) - { - str = getDebugStats (factionDebugInfo); - } - - if (this->getRenderInGamePerformance () == true) - { - renderer.renderPerformanceStats (); - } - else - { - static time_t lastGamePerfCheck = time (NULL); - if (difftime ((long int) time (NULL), lastGamePerfCheck) > 3) - { - lastGamePerfCheck = time (NULL); - - getGamePerformanceCounts (true); - } - } - - if (renderer.getShowDebugUI () == true) - { - const Metrics & metrics = Metrics::getInstance (); - int mh = metrics.getMinimapH (); - - if (this->getRenderInGamePerformance () == true) - { - mh = mh + ((int) gamePerformanceCounts.size () * 14); - } - - const Vec4f fontColor = getGui ()->getDisplay ()->getColor (); - - if (Renderer::renderText3DEnabled == true) - { - renderer.renderTextShadow3D (str, - coreData.getMenuFontNormal3D (), - fontColor, 10, - metrics.getVirtualH () - mh - 60, - false); - } - else - { - renderer.renderTextShadow (str, coreData.getMenuFontNormal (), - fontColor, 10, - metrics.getVirtualH () - mh - 60, false); - } - - vector < string > lineTokens; - Tokenize (str, lineTokens, "\n"); - int - fontHeightNormal = - (Renderer::renderText3DEnabled == - true ? coreData.getMenuFontNormal3D ()-> - getMetrics ()->getHeight ("W") : coreData.getMenuFontNormal ()-> - getMetrics ()->getHeight ("W")); - int fontHeightBig = - (Renderer::renderText3DEnabled == - true ? coreData.getMenuFontBig3D ()-> - getMetrics ()->getHeight ("W") : coreData.getMenuFontBig ()-> - getMetrics ()->getHeight ("W")); - int playerPosY = (int) lineTokens.size () * fontHeightNormal; - - //printf("lineTokens.size() = %d\n",lineTokens.size()); - - for (int i = 0; i < world.getFactionCount (); ++i) - { - string factionInfo = factionDebugInfo[i]; - Vec3f - playerColor = - world.getFaction (i)->getTexture ()-> - getPixmapConst ()->getPixel3f (0, 0); - - if (Renderer::renderText3DEnabled == true) - { - renderer.renderText3D (factionInfo, - coreData.getMenuFontBig3D (), - Vec4f (playerColor.x, playerColor.y, - playerColor.z, 1.0), 10, - //metrics.getVirtualH() - mh - 90 - 280 - (i * 16), - metrics.getVirtualH () - mh - 60 - - playerPosY - fontHeightBig - - (fontHeightBig * i), false); - } - else - { - renderer.renderText (factionInfo, - coreData.getMenuFontBig (), - Vec4f (playerColor.x, playerColor.y, - playerColor.z, 1.0), 10, - //metrics.getVirtualH() - mh - 90 - 280 - (i * 16), - metrics.getVirtualH () - mh - 60 - - playerPosY - fontHeightBig - - (fontHeightBig * i), false); - } - } - - if ((renderer.getShowDebugUILevel () & debugui_unit_titles) == - debugui_unit_titles) - { - if (renderer.getAllowRenderUnitTitles () == false) - { - renderer.setAllowRenderUnitTitles (true); - } - - if (Renderer::renderText3DEnabled == true) - { - renderer.renderUnitTitles3D (coreData.getMenuFontNormal3D (), - Vec3f (1.0f)); - } - else - { - renderer.renderUnitTitles (coreData.getMenuFontNormal (), - Vec3f (1.0f)); - } - } - } - - //network status - if (renderNetworkStatus == true) - { - if (NetworkManager::getInstance ().getGameNetworkInterface () != NULL) - { - const Metrics & metrics = Metrics::getInstance (); - int mx = metrics.getMinimapX (); - //int my= metrics.getMinimapY(); - int mw = metrics.getMinimapW (); - //int mh= metrics.getMinimapH(); - const Vec4f fontColor = getGui ()->getDisplay ()->getColor (); - - if (Renderer::renderText3DEnabled == true) - { - renderer. - renderTextShadow3D (NetworkManager:: - getInstance ().getGameNetworkInterface - ()->getNetworkStatus (), - coreData.getMenuFontNormal3D (), fontColor, - mx + mw + 5, - metrics.getVirtualH () - 30 - 20, false); - } - else - { - renderer. - renderTextShadow (NetworkManager:: - getInstance ().getGameNetworkInterface - ()->getNetworkStatus (), - coreData.getMenuFontNormal (), fontColor, - mx + mw + 5, metrics.getVirtualH () - 30 - 20, - false); - } - } - } - - // clock - if (photoModeEnabled == false && timeDisplay == true) - { - renderer.renderClock (); - } - - //resource info - if (photoModeEnabled == false) - { - if (this->masterserverMode == false) - { - renderer.renderResourceStatus (); - } - renderer.renderConsole (&console, - showFullConsole ? consoleFull : - consoleNormal); - } - - //2d mouse - renderer.renderMouse2d (mouseX, mouseY, mouse2d, - gui.isSelectingPos ()? 1.f : 0.f); - - if (perfLogging == true - && difftime ((long int) time (NULL), lastRenderLog2d) >= 1) - { - lastRenderLog2d = time (NULL); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] Statistics: %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, str.c_str ()); - } - } - - -// ==================== misc ==================== - - void Game::checkWinner () - { - // lookup int is team #, value is players alive on team - std::map < int, int > teamsAlive = getTeamsAlive (); - - if (gameOver == false || teamsAlive.size () > 1) - { - if (gameSettings.getDefaultVictoryConditions ()) - { - checkWinnerStandard (); - } - else - { - checkWinnerScripted (); - } - } - } - - void Game::setEndGameTeamWinnersAndLosers () - { - //bool lose= false; - bool - checkTeamIndex = !(this->masterserverMode == false - && world. - getThisFaction ()->getPersonalityType () != - fpt_Observer); - - // lookup int is team #, value is players alive on team - std::map < int, int > teamsAlive; - for (int i = 0; i < world.getFactionCount (); ++i) - { - if (checkTeamIndex == false || i != world.getThisFactionIndex ()) - { - if (factionLostGame (world.getFaction (i)) == false) - { - teamsAlive[world.getFaction (i)->getTeam ()] = - teamsAlive[world.getFaction (i)->getTeam ()] + 1; - } - } - } - - // did some team win - if (teamsAlive.size () <= 1) - { - for (int i = 0; i < world.getFactionCount (); ++i) - { - if (checkTeamIndex == false || i != world.getThisFactionIndex ()) - { - if (teamsAlive.find (world.getFaction (i)->getTeam ()) != - teamsAlive.end ()) - { - world.getStats ()->setVictorious (i); - } - else if (i == world.getThisFactionIndex ()) - { - //lose= true; - } - } - } - bool firstGameOverTrigger = false; - if (gameOver == false) - { - firstGameOverTrigger = true; - gameOver = true; - } - if (this->gameSettings.isNetworkGame () == false || - this->gameSettings.getEnableObserverModeAtEndGame () == true) - { - // Let the happy winner view everything left in the world - - // This caused too much LAG for network games - if (this->gameSettings.isNetworkGame () == false) - { - Renderer::getInstance ().setPhotoMode (true); - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - // END - } - - if (firstGameOverTrigger == true) - { - scriptManager.onGameOver (true); - } - - if (world.getFactionCount () == 1 - && world.getFaction (0)->getPersonalityType () == fpt_Observer) - { - //printf("!!!!!!!!!!!!!!!!!!!!"); - - //gameCamera.setMoveY(100.0); - gameCamera.zoom (-300); - //gameCamera.update(); - } -// else { -// if(lose == true) { -// showLoseMessageBox(); -// } -// else { -// showWinMessageBox(); -// } -// } - } - } - - std::map < int, int > Game::getTeamsAlive () - { - std::map < int, int > teamsAlive; - for (int factionIndex = 0; factionIndex < world.getFactionCount (); - ++factionIndex) - { - if (factionIndex != world.getThisFactionIndex ()) - { - if (factionLostGame (world.getFaction (factionIndex)) == false) - { - teamsAlive[world.getFaction (factionIndex)->getTeam ()] = - teamsAlive[world.getFaction (factionIndex)->getTeam ()] + 1; - } - } - } - // did some team win - return teamsAlive; - } - - void Game::checkWinnerStandardHeadlessOrObserver () - { - // lookup int is team #, value is players alive on team - std::map < int, int > teamsAlive = getTeamsAlive (); - - // did some team win - if (teamsAlive.size () <= 1) - { - if (this->masterserverMode == true) - { - printf ("Game finished...\n"); - } - for (int factionIndex = 0; factionIndex < world.getFactionCount (); - ++factionIndex) - { - - Faction *faction = world.getFaction (factionIndex); - if (factionIndex != world.getThisFactionIndex () && - teamsAlive.find (faction->getTeam ()) != teamsAlive.end ()) - { - - world.getStats ()->setVictorious (factionIndex); - if (this->masterserverMode == true) - { - - printf ("Player: %s is on the winning team #: %d\n", - this->gameSettings.getNetworkPlayerName (factionIndex). - c_str (), faction->getTeam ()); - } - } - } - bool wasGameOverAlready = gameOver; - gameOver = true; - - // Only need to process this once - if (wasGameOverAlready == false) - { - if (this->gameSettings.isNetworkGame () == false || - this->gameSettings.getEnableObserverModeAtEndGame () == true) - { - - // Let the happy winner view everything left in the world - // This caused too much LAG for network games - if (this->gameSettings.isNetworkGame () == false) - { - - Renderer::getInstance ().setPhotoMode (true); - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - // END - } - scriptManager.onGameOver (true); - if (world.getFactionCount () == 1 && - world.getFaction (0)->getPersonalityType () == fpt_Observer) - { - gameCamera.zoom (-300); - } - else - { - showWinMessageBox (); - } - } - } - } - - void Game::checkWinnerStandardPlayer () - { - //lose - bool lose = false; - if (factionLostGame (world.getThisFaction ()) == true) - { - - bool playerLostGame = true; - // Team Shared units enabled? - if (isFlagType1BitEnabled (ft1_allow_shared_team_units) == true) - { - - // Check if all team members have lost? - for (int factionIndex = 0; - factionIndex < world.getFactionCount (); ++factionIndex) - { - - if (world.getFaction (factionIndex)->getPersonalityType () != - fpt_Observer) - { - if (world. - getFaction (factionIndex)->isAlly (world. - getThisFaction ()) == - true - && factionLostGame (world.getFaction (factionIndex)) == - false) - { - - playerLostGame = false; - break; - } - } - } - } - - if (playerLostGame == true) - { - lose = true; - for (int factionIndex = 0; - factionIndex < world.getFactionCount (); ++factionIndex) - { - - if (world.getFaction (factionIndex)->getPersonalityType () != - fpt_Observer) - { - if (world. - getFaction (factionIndex)->isAlly (world. - getThisFaction ()) == - false - && factionLostGame (world.getFaction (factionIndex)) == - false) - { - - world.getStats ()->setVictorious (factionIndex); - } - } - } - bool wasGameOverAlready = gameOver; - gameOver = true; - - // Only need to process losing once - if (wasGameOverAlready == false) - { - if (this->gameSettings.isNetworkGame () == false || - this->gameSettings.getEnableObserverModeAtEndGame () == true) - { - // Let the poor user watch everything unfold - // This caused too much LAG for network games - if (this->gameSettings.isNetworkGame () == false) - { - Renderer::getInstance ().setPhotoMode (true); - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - // END - // but don't let him cheat via teamchat - chatManager.setDisableTeamMode (true); - } - scriptManager.onGameOver (!lose); - showLoseMessageBox (); - } - } - } - - //win - if (lose == false) - { - bool win = true; - for (int factionIndex = 0; factionIndex < world.getFactionCount (); - ++factionIndex) - { - - if (factionIndex != world.getThisFactionIndex ()) - { - if (world.getFaction (factionIndex)->getPersonalityType () != - fpt_Observer) - { - - if (factionLostGame (world.getFaction (factionIndex)) == - false - && world.getFaction (factionIndex)-> - isAlly (world.getThisFaction ()) == false) - { - - win = false; - } - } - } - } - - if (win) - { - for (int factionIndex = 0; - factionIndex < world.getFactionCount (); ++factionIndex) - { - if (world.getFaction (factionIndex)->getPersonalityType () != - fpt_Observer) - { - if (world. - getFaction (factionIndex)->isAlly (world.getThisFaction ())) - { - - world.getStats ()->setVictorious (factionIndex); - } - } - } - - bool wasGameOverAlready = gameOver; - gameOver = true; - - // Only need to process winning once - if (wasGameOverAlready == false) - { - if (this->gameSettings.isNetworkGame () == false || - this->gameSettings.getEnableObserverModeAtEndGame () == true) - { - // Let the happy winner view everything left in the world - - // This caused too much LAG for network games - if (this->gameSettings.isNetworkGame () == false) - { - Renderer::getInstance ().setPhotoMode (true); - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - // END - } - scriptManager.onGameOver (win); - showWinMessageBox (); - } - } - } - } - - void Game::checkWinnerStandard () - { - if (world.getFactionCount () <= 0) - { - return; - } - if (this->masterserverMode == true || - world.getThisFaction ()->getPersonalityType () == fpt_Observer) - { - checkWinnerStandardHeadlessOrObserver (); - } - else - { - checkWinnerStandardPlayer (); - } - } - - void Game::checkWinnerScripted () - { - if (scriptManager.getIsGameOver ()) - { - bool wasGameOverAlready = gameOver; - gameOver = true; - - - for (int index = 0; index < world.getFactionCount (); ++index) - { - if (scriptManager.getPlayerModifiers (index)->getWinner ()) - { - - world.getStats ()->setVictorious (index); - } - } - - // Only need to process winning once - if (wasGameOverAlready == false) - { - if (this->gameSettings.isNetworkGame () == false || - this->gameSettings.getEnableObserverModeAtEndGame () == true) - { - - // Let the happy winner view everything left in the world - // This caused too much LAG for network games - if (this->gameSettings.isNetworkGame () == false) - { - Renderer::getInstance ().setPhotoMode (true); - gameCamera.setMaxHeight (PHOTO_MODE_MAXHEIGHT); - } - // END - } - - if (this->masterserverMode == true || - world.getThisFaction ()->getPersonalityType () == fpt_Observer) - { - showWinMessageBox (); - } - else - { - scriptManager. - onGameOver (scriptManager.getPlayerModifiers - (world.getThisFactionIndex ())->getWinner ()); - - if (scriptManager.getPlayerModifiers - (world.getThisFactionIndex ())->getWinner ()) - { - showWinMessageBox (); - } - else - { - showLoseMessageBox (); - } - } - } - } - } - - bool Game::isFlagType1BitEnabled (FlagTypes1 type) const - { - return ((gameSettings.getFlagTypes1 () & (uint32) type) == - (uint32) type); - } - - bool Game::factionLostGame (int factionIndex) - { - return factionLostGame (world.getFaction (factionIndex)); - } - - bool Game::factionLostGame (const Faction * faction) - { - if (faction != NULL) - { - for (int factionIndex = 0; factionIndex < faction->getUnitCount (); - ++factionIndex) - { - const UnitType *ut = faction->getUnit (factionIndex)->getType (); - if (ut->getCountInVictoryConditions () == ucvcNotSet) - { - if (faction->getUnit (factionIndex)-> - getType ()->hasSkillClass (scBeBuilt)) - { - return false; - } - } - else if (ut->getCountInVictoryConditions () == ucvcTrue) - { - return false; - } - } - } - return true; - } - -//bool Game::hasBuilding(const Faction *faction) { -// if(faction != NULL) { -// for(int i=0; igetUnitCount(); ++i) { -// if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { -// return true; -// } -// } -// } -// return false; -//} - - void Game::incSpeed () - { - if (disableSpeedChange == true) - { - return; - } - - Lang & lang = Lang::getInstance (); - - if (this->speed < Config::getInstance ().getInt ("FastSpeedLoops")) - { - if (this->speed == 0) - { - this->speed = 1; - } - else - { - this->speed++; - } - console.addLine (lang.getString ("GameSpeedSet") + " " + - ((this->speed == - 0) ? lang.getString ("Slow") : (this->speed == - 1) ? - lang.getString ("Normal") : "x" + - intToStr (this->speed))); - } - } - - void Game::decSpeed () - { - if (disableSpeedChange == true) - { - return; - } - - Lang & lang = Lang::getInstance (); - if (this->speed > 0) - { - this->speed--; - console.addLine (lang.getString ("GameSpeedSet") + " " + - ((this->speed == - 0) ? lang.getString ("Slow") : (this->speed == - 1) ? - lang.getString ("Normal") : "x" + - intToStr (this->speed))); - } - } - - void - Game::setPaused (bool value, bool forceAllowPauseStateChange, - bool clearCaches, bool joinNetworkGame) - { - bool - speedChangesAllowed = - !NetworkManager::getInstance ().isNetworkGame (); - //printf("Toggle pause value = %d, speedChangesAllowed = %d, forceAllowPauseStateChange = %d\n",value,speedChangesAllowed,forceAllowPauseStateChange); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "game.cpp line: %d setPaused value: %d clearCaches: %d forceAllowPauseStateChange: %d speedChangesAllowed: %d pausedForJoinGame: %d joinNetworkGame: %d\n", - __LINE__, value, clearCaches, - forceAllowPauseStateChange, - speedChangesAllowed, pausedForJoinGame, - joinNetworkGame); - //printf("Line: %d setPaused value: %d clearCaches: %d forceAllowPauseStateChange: %d speedChangesAllowed: %d pausedForJoinGame: %d joinNetworkGame: %d\n",__LINE__,value,clearCaches,forceAllowPauseStateChange,speedChangesAllowed,pausedForJoinGame,joinNetworkGame); - - if (forceAllowPauseStateChange == true || speedChangesAllowed == true) - { - //printf("setPaused paused = %d, value = %d\n",paused,value); - - NetworkManager & networkManager = NetworkManager::getInstance (); - - // Cannot change pause state while client is joining in progress game - if (pausedForJoinGame == true && joinNetworkGame == false && - networkManager.getNetworkRole () == nrServer) - { - - ServerInterface *server = - NetworkManager::getInstance ().getServerInterface (); - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - this->gameSettings.getUniqueNetworkPlayerLanguages (); - - bool haveClientConnectedButNoReady = false; - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - - MutexSafeWrapper - safeMutex (server->getSlotMutex - (faction->getStartLocationIndex ()), CODE_AT_LINE); - ConnectionSlot *slot = - server->getSlot (faction->getStartLocationIndex (), - false); - if (slot != NULL && slot->isConnected () == true - && slot->isReady () == false) - { - for (unsigned int i = 0; i < languageList.size (); ++i) - { - - char szMsg[8096] = ""; - if (lang.hasString ("JoinPlayerToCurrentGameLaunch", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("JoinPlayerToCurrentGameLaunch", - languageList[i]).c_str (), - slot->getName ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is about to join the game, please wait...", - slot->getName ().c_str ()); - } - - safeMutex.ReleaseLock (); - - bool localEcho = lang.isLanguageLocal (languageList[i]); - server->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - sleep (1); - - haveClientConnectedButNoReady = true; - } - } - } - - if (haveClientConnectedButNoReady == true) - { - return; - } - } - - Lang & lang = Lang::getInstance (); - if (value == false) - { - console.addLine (lang.getString ("GameResumed")); - - bool wasPausedForJoinGame = pausedForJoinGame; - paused = false; - pausedForJoinGame = false; - pausedBeforeJoinGame = false; - pauseStateChanged = true; - - if (clearCaches == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "game.cpp line: %d Clear Caches for resume in progress game\n", - __LINE__); - //printf("Line: %d Clear Caches for resume in progress game\n",__LINE__); - - world.clearCaches (); - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - faction->clearCaches (); - } - world.refreshAllUnitExplorations (); - } - setupPopupMenus (false); - - if (networkManager.getNetworkRole () == nrClient && - wasPausedForJoinGame == true) - { - initialResumeSpeedLoops = true; - } - - commander.setPauseNetworkCommands (false); - } - else - { - console.addLine (lang.getString ("GamePaused")); - - if (joinNetworkGame == true) - { - pausedBeforeJoinGame = paused; - } - paused = true; - pausedForJoinGame = joinNetworkGame; - pauseStateChanged = true; - - if (clearCaches == true) - { - //printf("Line: %d Clear Caches for resume in progress game\n",__LINE__); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugWorldSynch).enabled) - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "game.cpp line: %d Clear Caches for resume in progress game\n", - __LINE__); - - world.clearCaches (); - for (int i = 0; i < world.getFactionCount (); ++i) - { - Faction *faction = world.getFaction (i); - faction->clearCaches (); - } - world.refreshAllUnitExplorations (); - } - pauseRequestSent = false; - - commander.setPauseNetworkCommands (joinNetworkGame); - } - //printf("setPaused new paused = %d\n",paused); - } - } - - bool Game::getPaused () - { - bool - speedChangesAllowed = - !NetworkManager::getInstance ().isNetworkGame (); - if (speedChangesAllowed) - { - if (popupMenu.getVisible () == true - || popupMenuSwitchTeams.getVisible () == true) - { - return true; - } -// if(mainMessageBox.getEnabled() == true || errorMessageBox.getEnabled() == true){ -// return true; -// } - if (currentUIState != NULL) - { - return true; - } - } - return paused; - } - - int Game::getUpdateLoops () - { - if (commander.hasReplayCommandListForFrame () == true) - { - return 1; - } - - if (getPaused ()) - { - return 0; - } - else if (this->speed == 0) - { - return updateFps % 2 == 0 ? 1 : 0; - } - else - return this->speed; - } - - void Game::showLoseMessageBox () - { - Lang & lang = Lang::getInstance (); - - NetworkManager & networkManager = NetworkManager::getInstance (); - if (networkManager.isNetworkGame () == true - && networkManager.getNetworkRole () == nrServer) - { - showMessageBox (lang.getString ("YouLose") + " " + - lang.getString ("ExitBattleServerQuestion"), - lang.getString ("BattleOver"), false); - } - else - { - showMessageBox (lang.getString ("YouLose") + " " + - lang.getString ("ExitBattleQuestion"), - lang.getString ("BattleOver"), false); - } - } - - void Game::showWinMessageBox () - { - Lang & lang = Lang::getInstance (); - - if (this->masterserverMode == true - || world.getThisFaction ()->getPersonalityType () == fpt_Observer) - { - showMessageBox (lang.getString ("GameOver") + " " + - lang.getString ("ExitBattleQuestion"), - lang.getString ("BattleOver"), false); - } - else - { - showMessageBox (lang.getString ("YouWin") + " " + - lang.getString ("ExitBattleQuestion"), - lang.getString ("BattleOver"), false); - } - } - - void - Game::showMessageBox (const string & text, const string & header, - bool toggle) - { - if (toggle == false) - { - mainMessageBox.setEnabled (false); - } - - if (mainMessageBox.getEnabled () == false) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void - Game::showErrorMessageBox (const string & text, const string & header, - bool toggle) - { - if (toggle == false) - { - errorMessageBox.setEnabled (false); - } - - if (errorMessageBox.getEnabled () == false) - { - errorMessageBox.setText (text); - errorMessageBox.setHeader (header); - errorMessageBox.setEnabled (true); - } - else - { - errorMessageBox.setEnabled (false); - } - } - - void Game::startPerformanceTimer () - { - captureAvgTestStatus = true; - updateFpsAvgTest = -1; - renderFpsAvgTest = -1; - } - - void Game::endPerformanceTimer () - { - captureAvgTestStatus = false; - } - - Vec2i Game::getPerformanceTimerResults () - { - Vec2i results (this->updateFpsAvgTest, this->renderFpsAvgTest); - return results; - } - - void Game::consoleAddLine (string line) - { - console.addLine (line); - } - void Game::toggleTeamColorMarker () - { - renderExtraTeamColor++; - renderExtraTeamColor = renderExtraTeamColor % 4; - } - - void - Game::addNetworkCommandToReplayList (NetworkCommand * networkCommand, - int worldFrameCount) - { - Config & config = Config::getInstance (); - if (config.getBool ("SaveCommandsForReplay", "false") == true) - { - replayCommandList.push_back (make_pair - (worldFrameCount, *networkCommand)); - } - } - - void Game::renderVideoPlayer () - { - if (videoPlayer != NULL) - { - if (videoPlayer->isPlaying () == true) - { - videoPlayer->playFrame (false); - } - else - { - delete videoPlayer; - videoPlayer = NULL; - } - } - } - - void Game::playStaticVideo (const string & playVideo) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == true) - { - - //togglePauseGame(true,true); - tryPauseToggle (true); - setupRenderForVideo (); - - -// Context *c= GraphicsInterface::getInstance().getCurrentContext(); -// SDL_Surface *screen = static_cast(c)->getPlatformContextGlPtr()->getScreen(); -// -// string vlcPluginsPath = Config::getInstance().getString("VideoPlayerPluginsPath",""); -// //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); -// Shared::Graphics::VideoPlayer player(playVideo.c_str(), -// screen, -// 0,0, -// screen->w, -// screen->h, -// screen->format->BitsPerPixel, -// vlcPluginsPath, -// SystemFlags::VERBOSE_MODE_ENABLED); -// player.PlayVideo(); - //} - //tryPauseToggle(false); - - playStreamingVideo (playVideo); - playingStaticVideo = true; - } - } - void Game::playStreamingVideo (const string & playVideo) - { - if (videoPlayer == NULL) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - Context *c = GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl *glCtx = - static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window *window = glCtx->getScreenWindow (); - SDL_Surface *screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); - - videoPlayer = - new::Shared::Graphics::VideoPlayer (&Renderer::getInstance (), - playVideo, "", window, 0, - 0, screen->w, screen->h, - screen->format->BitsPerPixel, - false, vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - } - } - else - { - if (videoPlayer->isPlaying () == false) - { - delete videoPlayer; - videoPlayer = NULL; - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - Context *c = - GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl *glCtx = - static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window *window = glCtx->getScreenWindow (); - SDL_Surface *screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); - - videoPlayer = - new::Shared::Graphics::VideoPlayer (&Renderer::getInstance (), - playVideo, "", window, - 0, 0, screen->w, - screen->h, - screen-> - format->BitsPerPixel, false, - vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - } - } - } - - if (videoPlayer != NULL) - { - videoPlayer->initPlayer (); - } - } - void Game::stopStreamingVideo (const string & playVideo) - { - if (videoPlayer != NULL) - { - videoPlayer->StopVideo (); - } - } - - void Game::stopAllVideo () - { - if (videoPlayer != NULL) - { - videoPlayer->StopVideo (); - } - } - - bool - Game::clientLagHandler (int slotIndex, - bool networkPauseGameForLaggedClients) - { - if (networkPauseGameForLaggedClients == true) - { - printf - ("**WARNING** Detected lag from client: %d networkPauseGameForLaggedClients: %d\n", - slotIndex, networkPauseGameForLaggedClients); - } - else - { - printf - ("==> Requested Resume Game after Pause for lagging client(s)...\n"); - } - - this->networkPauseGameForLaggedClientsRequested = - networkPauseGameForLaggedClients; - this->networkResumeGameForLaggedClientsRequested = - !networkPauseGameForLaggedClients; - return true; - } - - void Game::saveGame () - { - string file = this->saveGame (GameConstants::saveGameFilePattern); - char szBuf[8096] = ""; - Lang & lang = Lang::getInstance (); - snprintf (szBuf, 8096, lang.getString ("GameSaved", "", true).c_str (), - file.c_str ()); - console.addLine (szBuf); - - Config & config = Config::getInstance (); - config.setString ("LastSavedGame", file); - config.save (); - } - - string Game::saveGame (string name, const string & path) - { - Config & config = Config::getInstance (); - // auto name file if using saved file pattern string - if (name == GameConstants::saveGameFilePattern) - { - //time_t curTime = time(NULL); - //struct tm *loctime = localtime (&curTime); - struct tm loctime = threadsafe_localtime (systemtime_now ()); - char szBuf2[100] = ""; - strftime (szBuf2, 100, "%Y%m%d_%H%M%S", &loctime); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, name.c_str (), szBuf2); - name = szBuf; - } - else if (name == GameConstants::saveGameFileAutoTestDefault) - { - //time_t curTime = time(NULL); - //struct tm *loctime = localtime (&curTime); - struct tm loctime = threadsafe_localtime (systemtime_now ()); - char szBuf2[100] = ""; - strftime (szBuf2, 100, "%Y%m%d_%H%M%S", &loctime); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, name.c_str (), szBuf2); - name = szBuf; - } - - // Save the file now - string saveGameFile = path + name; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - saveGameFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - saveGameFile; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameFile = userData + saveGameFile; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Saving game to [%s]\n", saveGameFile.c_str ()); - - // This condition will re-play all the commands from a replay file - // INSTEAD of saving from a saved game. - if (config.getBool ("SaveCommandsForReplay", "false") == true) - { - std::map < string, string > mapTagReplacements; - XmlTree xmlTreeSaveGame (XML_RAPIDXML_ENGINE); - - xmlTreeSaveGame.init ("zetaglest-saved-game"); - XmlNode *rootNodeReplay = xmlTreeSaveGame.getRootNode (); - - //std::map mapTagReplacements; - //time_t now = time(NULL); - //struct tm *loctime = localtime (&now); - struct tm loctime = threadsafe_localtime (systemtime_now ()); - char szBuf[4096] = ""; - strftime (szBuf, 4095, "%Y-%m-%d %H:%M:%S", &loctime); - - rootNodeReplay->addAttribute ("version", glestVersionString, - mapTagReplacements); - rootNodeReplay->addAttribute ("timestamp", szBuf, mapTagReplacements); - - XmlNode *gameNodeReplay = rootNodeReplay->addChild ("Game"); - gameSettings.saveGame (gameNodeReplay); - - gameNodeReplay->addAttribute ("LastWorldFrameCount", - intToStr (world.getFrameCount ()), - mapTagReplacements); - - for (unsigned int i = 0; i < replayCommandList.size (); ++i) - { - std::pair < int, NetworkCommand > & cmd = replayCommandList[i]; - XmlNode *networkCommandNode = cmd.second.saveGame (gameNodeReplay); - networkCommandNode->addAttribute ("worldFrameCount", - intToStr (cmd.first), - mapTagReplacements); - } - - string replayFile = saveGameFile + ".replay"; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Saving game replay commands to [%s]\n", - replayFile.c_str ()); - xmlTreeSaveGame.save (replayFile); - } - - XmlTree xmlTree; - xmlTree.init ("zetaglest-saved-game"); - XmlNode *rootNode = xmlTree.getRootNode (); - - std::map < string, string > mapTagReplacements; - //time_t now = time(NULL); - //struct tm *loctime = localtime (&now); - struct tm loctime = threadsafe_localtime (systemtime_now ()); - char szBuf[4096] = ""; - strftime (szBuf, 4095, "%Y-%m-%d %H:%M:%S", &loctime); - - rootNode->addAttribute ("version", glestVersionString, - mapTagReplacements); - rootNode->addAttribute ("timestamp", szBuf, mapTagReplacements); - - XmlNode *gameNode = rootNode->addChild ("Game"); - //World world; - world.saveGame (gameNode); - //AiInterfaces aiInterfaces; - for (unsigned int i = 0; i < aiInterfaces.size (); ++i) - { - AiInterface *aiIntf = aiInterfaces[i]; - if (aiIntf != NULL) - { - aiIntf->saveGame (gameNode); - } - } - //Gui gui; - gui.saveGame (gameNode); - //GameCamera gameCamera; - gameCamera.saveGame (gameNode); - //Commander commander; - //Console console; - //ChatManager chatManager; - //ScriptManager scriptManager; - scriptManager.saveGame (gameNode); - - //misc - //Checksum checksum; - gameNode->addAttribute ("checksum", intToStr (checksum.getSum ()), - mapTagReplacements); - //string loadingText; -// int mouse2d; - gameNode->addAttribute ("mouse2d", intToStr (mouse2d), - mapTagReplacements); -// int mouseX; - gameNode->addAttribute ("mouseX", intToStr (mouseX), - mapTagReplacements); -// int mouseY; //coords win32Api - gameNode->addAttribute ("mouseY", intToStr (mouseY), - mapTagReplacements); -// Vec2i mouseCellPos; - gameNode->addAttribute ("mouseCellPos", mouseCellPos.getString (), - mapTagReplacements); -// int updateFps, lastUpdateFps, avgUpdateFps; -// int totalRenderFps, renderFps, lastRenderFps, avgRenderFps,currentAvgRenderFpsTotal; - //Uint64 tickCount; - gameNode->addAttribute ("tickCount", intToStr (tickCount), - mapTagReplacements); - - //bool paused; - gameNode->addAttribute ("paused", intToStr (paused), - mapTagReplacements); - //bool gameOver; - gameNode->addAttribute ("gameOver", intToStr (gameOver), - mapTagReplacements); - //bool renderNetworkStatus; - //bool showFullConsole; - //bool mouseMoved; - //float scrollSpeed; - gameNode->addAttribute ("scrollSpeed", floatToStr (scrollSpeed, 6), - mapTagReplacements); - //bool camLeftButtonDown; - //bool camRightButtonDown; - //bool camUpButtonDown; - //bool camDownButtonDown; - - //Speed speed; - gameNode->addAttribute ("speed", intToStr (speed), mapTagReplacements); - - //GraphicMessageBox mainMessageBox; - //GraphicMessageBox errorMessageBox; - - //misc ptr - //ParticleSystem *weatherParticleSystem; - if (weatherParticleSystem != NULL) - { - weatherParticleSystem->saveGame (gameNode); - } - //GameSettings gameSettings; - gameSettings.saveGame (gameNode); - //Vec2i lastMousePos; - gameNode->addAttribute ("lastMousePos", lastMousePos.getString (), - mapTagReplacements); - //time_t lastRenderLog2d; - gameNode->addAttribute ("lastRenderLog2d", intToStr (lastRenderLog2d), - mapTagReplacements); - //DisplayMessageFunction originalDisplayMsgCallback; - //bool isFirstRender; - gameNode->addAttribute ("isFirstRender", intToStr (isFirstRender), - mapTagReplacements); - - //bool quitTriggeredIndicator; - //int original_updateFps; - gameNode->addAttribute ("original_updateFps", - intToStr (original_updateFps), - mapTagReplacements); - //int original_cameraFps; - gameNode->addAttribute ("original_cameraFps", - intToStr (original_cameraFps), - mapTagReplacements); - - //bool captureAvgTestStatus; - gameNode->addAttribute ("captureAvgTestStatus", - intToStr (captureAvgTestStatus), - mapTagReplacements); - //int updateFpsAvgTest; - gameNode->addAttribute ("updateFpsAvgTest", intToStr (updateFpsAvgTest), - mapTagReplacements); - //int renderFpsAvgTest; - gameNode->addAttribute ("renderFpsAvgTest", intToStr (renderFpsAvgTest), - mapTagReplacements); - - //int renderExtraTeamColor; - gameNode->addAttribute ("renderExtraTeamColor", - intToStr (renderExtraTeamColor), - mapTagReplacements); - - //static const int renderTeamColorCircleBit=1; - //static const int renderTeamColorPlaneBit=2; - - //bool photoModeEnabled; - gameNode->addAttribute ("photoModeEnabled", intToStr (photoModeEnabled), - mapTagReplacements); - //bool visibleHUD; - gameNode->addAttribute ("visibleHUD", intToStr (visibleHUD), - mapTagReplacements); - - //bool timeDisplay - gameNode->addAttribute ("timeDisplay", intToStr (timeDisplay), - mapTagReplacements); - - //bool withRainEffect; - gameNode->addAttribute ("withRainEffect", intToStr (withRainEffect), - mapTagReplacements); - //Program *program; - - //bool gameStarted; - gameNode->addAttribute ("gameStarted", intToStr (gameStarted), - mapTagReplacements); - - //time_t lastMaxUnitCalcTime; - gameNode->addAttribute ("lastMaxUnitCalcTime", - intToStr (lastMaxUnitCalcTime), - mapTagReplacements); - - //PopupMenu popupMenu; - //PopupMenu popupMenuSwitchTeams; - - //std::map switchTeamIndexMap; - //GraphicMessageBox switchTeamConfirmMessageBox; - - //int exitGamePopupMenuIndex; - //int joinTeamPopupMenuIndex; - //int pauseGamePopupMenuIndex; - //int keyboardSetupPopupMenuIndex; - //GLuint statelist3dMenu; - //ProgramState *currentUIState; - - //bool masterserverMode; - - //StrSound *currentAmbientSound; - - //time_t lastNetworkPlayerConnectionCheck; - gameNode->addAttribute ("lastNetworkPlayerConnectionCheck", - intToStr (lastNetworkPlayerConnectionCheck), - mapTagReplacements); - - //time_t lastMasterServerGameStatsDump; - gameNode->addAttribute ("lastMasterServerGameStatsDump", - intToStr (lastMasterServerGameStatsDump), - mapTagReplacements); - - XmlNode *unitHighlightListNode = - gameNode->addChild ("unitHighlightList"); - //for(unsigned int i = 0; i < unitHighlightList.size(); ++i) { - for (std::map < int, HighlightSpecialUnitInfo >::iterator iterMap = - unitHighlightList.begin (); iterMap != unitHighlightList.end (); - ++iterMap) - { - HighlightSpecialUnitInfo & info = iterMap->second; - XmlNode *infoNode = unitHighlightListNode->addChild ("info"); - infoNode->addAttribute ("unitid", intToStr (iterMap->first), - mapTagReplacements); - infoNode->addAttribute ("radius", floatToStr (info.radius, 6), - mapTagReplacements); - infoNode->addAttribute ("thickness", floatToStr (info.thickness, 6), - mapTagReplacements); - infoNode->addAttribute ("color", info.color.getString (), - mapTagReplacements); - } - - gameNode->addAttribute ("disableSpeedChange", - intToStr (disableSpeedChange), - mapTagReplacements); - - xmlTree.save (saveGameFile); - - if (masterserverMode == false) - { - // take Screenshot - string jpgFileName = saveGameFile + ".jpg"; - // menu is already disabled, last rendered screen is still with enabled one. Lets render again: - render3d (); - render2d (); - Renderer::getInstance ().saveScreen (jpgFileName, - config.getInt - ("SaveGameScreenshotWidth", - "800"), - config.getInt - ("SaveGameScreenshotHeight", - "600")); - } - - return saveGameFile; - } - - void - Game::loadGame (string name, Program * programPtr, - bool isMasterserverMode, - const GameSettings * joinGameSettings) - { - Config & config = Config::getInstance (); - // This condition will re-play all the commands from a replay file - // INSTEAD of saving from a saved game. - if (joinGameSettings == NULL - && config.getBool ("SaveCommandsForReplay", "false") == true) - { - XmlTree xmlTreeReplay (XML_RAPIDXML_ENGINE); - std::map < string, string > mapExtraTagReplacementValues; - xmlTreeReplay.load (name + ".replay", - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues), true); - - const XmlNode *rootNode = xmlTreeReplay.getRootNode (); - - if (rootNode->hasChild ("zetaglest-saved-game") == true) - { - rootNode = rootNode->getChild ("zetaglest-saved-game"); - } - - //const XmlNode *versionNode= rootNode->getChild("zetaglest-saved-game"); - const XmlNode *versionNode = rootNode; - - Lang & lang = Lang::getInstance (); - string gameVer = versionNode->getAttribute ("version")->getValue (); - if (gameVer != glestVersionString - && checkVersionComptability (gameVer, - glestVersionString) == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("SavedGameBadVersion").c_str (), - gameVer.c_str (), glestVersionString.c_str ()); - throw megaglest_runtime_error (szBuf, true); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Found saved game version that matches your application version: [%s] --> [%s]\n", - gameVer.c_str (), glestVersionString.c_str ()); - - XmlNode *gameNode = rootNode->getChild ("Game"); - - GameSettings newGameSettingsReplay; - newGameSettingsReplay.loadGame (gameNode); - //printf("Loading scenario [%s]\n",newGameSettingsReplay.getScenarioDir().c_str()); - if (newGameSettingsReplay.getScenarioDir () != "" - && fileExists (newGameSettingsReplay.getScenarioDir ()) == false) - { - newGameSettingsReplay.setScenarioDir (Scenario::getScenarioPath - (Config:: - getInstance - ().getPathListForType - (ptScenarios), - newGameSettingsReplay.getScenario - ())); - - //printf("Loading scenario #2 [%s]\n",newGameSettingsReplay.getScenarioDir().c_str()); - } - - //GameSettings newGameSettings; - //newGameSettings.loadGame(gameNode); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Game settings loaded\n"); - - NetworkManager & networkManager = NetworkManager::getInstance (); - networkManager.end (); - networkManager.init (nrServer, true); - - Game *newGame = - new Game (programPtr, &newGameSettingsReplay, isMasterserverMode); - newGame->lastworldFrameCountForReplay = - gameNode->getAttribute ("LastWorldFrameCount")->getIntValue (); - - vector < XmlNode * >networkCommandNodeList = - gameNode->getChildList ("NetworkCommand"); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("networkCommandNodeList.size() = " MG_SIZE_T_SPECIFIER - "\n", networkCommandNodeList.size ()); - for (unsigned int i = 0; i < networkCommandNodeList.size (); ++i) - { - XmlNode *node = networkCommandNodeList[i]; - int - worldFrameCount = - node->getAttribute ("worldFrameCount")->getIntValue (); - NetworkCommand command; - command.loadGame (node); - newGame->commander.addToReplayCommandList (command, - worldFrameCount); - } - - programPtr->setState (newGame); - return; - } - - XmlTree xmlTree (XML_RAPIDXML_ENGINE); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Before load of XML\n"); - std::map < string, string > mapExtraTagReplacementValues; - xmlTree.load (name, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues), true); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("After load of XML\n"); - - const XmlNode *rootNode = xmlTree.getRootNode (); - if (rootNode->hasChild ("zetaglest-saved-game") == true) - { - rootNode = rootNode->getChild ("zetaglest-saved-game"); - } - - //const XmlNode *versionNode= rootNode->getChild("zetaglest-saved-game"); - const XmlNode *versionNode = rootNode; - - Lang & lang = Lang::getInstance (); - string gameVer = versionNode->getAttribute ("version")->getValue (); - // this is the version check for loading normal save games from menu_state_load_game - if (gameVer != glestVersionString - && - (compareMajorMinorVersion - (gameVer, lastCompatibleSaveGameVersionString) < 0 - || compareMajorMinorVersion (glestVersionString, gameVer) < 0)) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("SavedGameBadVersion").c_str (), - gameVer.c_str (), glestVersionString.c_str ()); - throw megaglest_runtime_error (szBuf, true); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Found saved game version that matches your application version: [%s] --> [%s]\n", - gameVer.c_str (), glestVersionString.c_str ()); - - XmlNode *gameNode = rootNode->getChild ("Game"); - GameSettings newGameSettings; - if (joinGameSettings != NULL) - { - newGameSettings = *joinGameSettings; - - XmlNode *worldNode = gameNode->getChild ("World"); - XmlNode *guiNode = gameNode->getChild ("Gui"); - XmlNode *selectionNode = guiNode->getChild ("Selection"); - XmlNode *statsNode = worldNode->getChild ("Stats"); - XmlNode *minimapNode = worldNode->getChild ("Minimap"); - - if (gameVer != glestVersionString - && checkVersionComptability (gameVer, - glestVersionString) == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("SavedGameBadVersion").c_str (), - gameVer.c_str (), glestVersionString.c_str ()); - throw megaglest_runtime_error (szBuf, true); - } - // This is explored fog of war for the host player, clear it - minimapNode->clearChild ("fowPixmap1"); - - NetworkManager & networkManager = NetworkManager::getInstance (); - //NetworkRole role = networkManager.getNetworkRole(); - ClientInterface *clientInterface = - dynamic_cast < - ClientInterface * >(networkManager.getClientInterface ()); - - for (int i = 0; i < newGameSettings.getFactionCount (); ++i) - { - //replace by network - if (newGameSettings.getFactionControl (i) == ctHuman) - { - newGameSettings.setFactionControl (i, ctNetwork); - } - - //set the faction index - if (newGameSettings.getStartLocationIndex (i) == - clientInterface->getPlayerIndex ()) - { - newGameSettings.setThisFactionIndex (i); - newGameSettings.setFactionControl (i, ctNetwork); - - worldNode-> - getAttribute ("thisFactionIndex")->setValue (intToStr (i)); - //worldNode->getAttribute("thisTeamIndex")->setValue(intToStr(newGameSettings.getTeam(i))); - - XmlNode *factionNode = worldNode->getChild ("Faction", i); - factionNode-> - getAttribute ("thisFaction")->setValue (intToStr (i)); - factionNode-> - getAttribute ("control")->setValue (intToStr (ctNetwork)); - - selectionNode-> - getAttribute ("factionIndex")->setValue (intToStr (i)); - //selectionNode->getAttribute("teamIndex")->setValue(intToStr(newGameSettings.getTeam(i))); - - statsNode-> - getAttribute ("thisFactionIndex")->setValue (intToStr (i)); - } - else - { - XmlNode *factionNode = worldNode->getChild ("Faction", i); - factionNode-> - getAttribute ("thisFaction")->setValue (intToStr (0)); - } - } - } - else - { - newGameSettings.loadGame (gameNode); - //printf("Loading scenario [%s]\n",newGameSettings.getScenarioDir().c_str()); - if (newGameSettings.getScenarioDir () != "" - && fileExists (newGameSettings.getScenarioDir ()) == false) - { - newGameSettings.setScenarioDir (Scenario::getScenarioPath - (Config:: - getInstance ().getPathListForType - (ptScenarios), - newGameSettings.getScenario ())); - - //printf("Loading scenario #2 [%s]\n",newGameSettings.getScenarioDir().c_str()); - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Game settings loaded\n"); - - if (joinGameSettings == NULL) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - networkManager.end (); - networkManager.init (nrServer, true); - } - - Game *newGame = - new Game (programPtr, &newGameSettings, isMasterserverMode); - - newGame->loadGameNode = gameNode; - newGame->inJoinGameLoading = (joinGameSettings != NULL); - -// newGame->mouse2d = gameNode->getAttribute("mouse2d")->getIntValue(); -// int mouseX; -// newGame->mouseX = gameNode->getAttribute("mouseX")->getIntValue(); -// int mouseY; //coords win32Api -// newGame->mouseY = gameNode->getAttribute("mouseY")->getIntValue(); -// Vec2i mouseCellPos; -// newGame->mouseCellPos = Vec2i::strToVec2(gameNode->getAttribute("mouseCellPos")->getValue()); -// int updateFps, lastUpdateFps, avgUpdateFps; -// int totalRenderFps, renderFps, lastRenderFps, avgRenderFps,currentAvgRenderFpsTotal; - //Uint64 tickCount; - newGame->tickCount = - gameNode->getAttribute ("tickCount")->getIntValue (); - - //bool paused; - if (newGame->inJoinGameLoading == true) - { - newGame->paused = - gameNode->getAttribute ("paused")->getIntValue () != 0; - } - else - { - //newGame->paused = gameNode->getAttribute("paused")->getIntValue() != 0; - newGame->paused = true; - } - if (newGame->paused) - newGame->console.addLine (lang.getString ("GamePaused")); - //bool gameOver; - newGame->gameOver = - gameNode->getAttribute ("gameOver")->getIntValue () != 0; - //bool renderNetworkStatus; - //bool showFullConsole; - //bool mouseMoved; - //float scrollSpeed; -// newGame->scrollSpeed = gameNode->getAttribute("scrollSpeed")->getFloatValue(); - //bool camLeftButtonDown; - //bool camRightButtonDown; - //bool camUpButtonDown; - //bool camDownButtonDown; - - //Speed speed; - //gameNode->addAttribute("speed",intToStr(speed), mapTagReplacements); - - //GraphicMessageBox mainMessageBox; - //GraphicMessageBox errorMessageBox; - - //misc ptr - //ParticleSystem *weatherParticleSystem; -// if(weatherParticleSystem != NULL) { -// weatherParticleSystem->saveGame(gameNode); -// } - //GameSettings gameSettings; -// gameSettings.saveGame(gameNode); - //Vec2i lastMousePos; -// gameNode->addAttribute("lastMousePos",lastMousePos.getString(), mapTagReplacements); - //time_t lastRenderLog2d; -// gameNode->addAttribute("lastRenderLog2d",intToStr(lastRenderLog2d), mapTagReplacements); - //DisplayMessageFunction originalDisplayMsgCallback; - //bool isFirstRender; -// gameNode->addAttribute("isFirstRender",intToStr(isFirstRender), mapTagReplacements); - - //bool quitTriggeredIndicator; - //int original_updateFps; -// gameNode->addAttribute("original_updateFps",intToStr(original_updateFps), mapTagReplacements); - //int original_cameraFps; -// gameNode->addAttribute("original_cameraFps",intToStr(original_cameraFps), mapTagReplacements); - - //bool captureAvgTestStatus; -// gameNode->addAttribute("captureAvgTestStatus",intToStr(captureAvgTestStatus), mapTagReplacements); - //int updateFpsAvgTest; -// gameNode->addAttribute("updateFpsAvgTest",intToStr(updateFpsAvgTest), mapTagReplacements); - //int renderFpsAvgTest; -// gameNode->addAttribute("renderFpsAvgTest",intToStr(renderFpsAvgTest), mapTagReplacements); - - //int renderExtraTeamColor; - newGame->renderExtraTeamColor = - gameNode->getAttribute ("renderExtraTeamColor")->getIntValue (); - - //static const int renderTeamColorCircleBit=1; - //static const int renderTeamColorPlaneBit=2; - - //bool photoModeEnabled; - //gameNode->addAttribute("photoModeEnabled",intToStr(photoModeEnabled), mapTagReplacements); - newGame->photoModeEnabled = - gameNode->getAttribute ("photoModeEnabled")->getIntValue () != 0; - //bool visibleHUD; - //gameNode->addAttribute("visibleHUD",intToStr(visibleHUD), mapTagReplacements); - newGame->visibleHUD = - gameNode->getAttribute ("visibleHUD")->getIntValue () != 0; - newGame->timeDisplay = - gameNode->getAttribute ("timeDisplay")->getIntValue () != 0; - //bool withRainEffect; - //gameNode->addAttribute("withRainEffect",intToStr(withRainEffect), mapTagReplacements); - newGame->withRainEffect = - gameNode->getAttribute ("withRainEffect")->getIntValue () != 0; - //Program *program; - - if (joinGameSettings == NULL) - { - if (gameNode->hasChild ("unitHighlightList") == true) - { - XmlNode *unitHighlightListNode = - gameNode->getChild ("unitHighlightList"); - vector < XmlNode * >infoNodeList = - unitHighlightListNode->getChildList ("info"); - for (unsigned int i = 0; i < infoNodeList.size (); ++i) - { - XmlNode *infoNode = infoNodeList[i]; - - int unitId = infoNode->getAttribute ("radius")->getIntValue (); - HighlightSpecialUnitInfo info; - info.radius = infoNode->getAttribute ("radius")->getFloatValue (); - info.thickness = - infoNode->getAttribute ("thickness")->getFloatValue (); - info.color = - Vec4f::strToVec4 (infoNode-> - getAttribute ("color")->getValue ()); - - newGame->unitHighlightList[unitId] = info; - } - } - } - - newGame->timeDisplay = - gameNode->getAttribute ("timeDisplay")->getIntValue () != 0; - - if (gameNode->hasAttribute ("disableSpeedChange") == true) - { - newGame->disableSpeedChange = - gameNode->getAttribute ("disableSpeedChange")->getIntValue () != 0; - } - - //bool gameStarted; - //gameNode->addAttribute("gameStarted",intToStr(gameStarted), mapTagReplacements); -// newGame->gameStarted = gameNode->getAttribute("gameStarted")->getIntValue(); - - //time_t lastMaxUnitCalcTime; - //gameNode->addAttribute("lastMaxUnitCalcTime",intToStr(lastMaxUnitCalcTime), mapTagReplacements); - - //PopupMenu popupMenu; - //PopupMenu popupMenuSwitchTeams; - - //std::map switchTeamIndexMap; - //GraphicMessageBox switchTeamConfirmMessageBox; - - //int exitGamePopupMenuIndex; - //int joinTeamPopupMenuIndex; - //int pauseGamePopupMenuIndex; - //int keyboardSetupPopupMenuIndex; - //GLuint statelist3dMenu; - //ProgramState *currentUIState; - - //bool masterserverMode; - - //StrSound *currentAmbientSound; - - //time_t lastNetworkPlayerConnectionCheck; - //gameNode->addAttribute("lastNetworkPlayerConnectionCheck",intToStr(lastNetworkPlayerConnectionCheck), mapTagReplacements); - - //time_t lastMasterServerGameStatsDump; - //gameNode->addAttribute("lastMasterServerGameStatsDump",intToStr(lastMasterServerGameStatsDump), mapTagReplacements); - - if (joinGameSettings == NULL) - { - newGame->gameCamera.loadGame (gameNode); - } - - const XmlNode *worldNode = gameNode->getChild ("World"); - newGame->world.loadGame (worldNode); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Starting Game ...\n"); - programPtr->setState (newGame); - } - - } -} //end namespace + } + } + } + + void saveStatsToSteam(Game * game, Stats & endStats) { + Steam *steamInstance = + CacheManager::getCachedItem < + Steam * >(GameConstants::steamCacheInstanceKey); + if (steamInstance != NULL) { + printf("\nSTEAM detected, writing out end game stats for player!\n"); + + // Write out stats here + if (NetworkManager::getInstance().isNetworkGame()) { + //printf("\nSTEAM Refresh Stats!\n"); + steamInstance->requestRefreshStats(); + for (int factionIndex = 0; + factionIndex < game->getWorld()->getFactionCount(); + ++factionIndex) { + if (factionIndex == game->getWorld()->getThisFactionIndex()) { + //printf("\nWriting out game stats for Faction Index: %d!\n",factionIndex); + if (endStats.getVictory(factionIndex)) { + steamInstance->setStatAsInt(EnumParser < + SteamStatName >::getString + (stat_online_wins).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName >::getString + (stat_online_wins).c_str()) + + 1); + } else { + steamInstance->setStatAsInt(EnumParser < + SteamStatName >::getString + (stat_online_loses).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName >::getString + (stat_online_loses).c_str()) + + 1); + } + steamInstance->setStatAsInt(EnumParser < + SteamStatName >::getString + (stat_online_kills).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_kills).c_str()) + + endStats.getKills(factionIndex)); + steamInstance->setStatAsInt(EnumParser < + SteamStatName + >::getString + (stat_online_kills_enemy).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_kills_enemy).c_str + ()) + + endStats.getEnemyKills + (factionIndex)); + steamInstance->setStatAsInt(EnumParser < + SteamStatName + >::getString + (stat_online_deaths).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_deaths).c_str()) + + endStats.getDeaths(factionIndex)); + steamInstance->setStatAsInt(EnumParser < + SteamStatName + >::getString + (stat_online_units).c_str(), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_units).c_str()) + + endStats.getUnitsProduced + (factionIndex)); + steamInstance->setStatAsInt(EnumParser < + SteamStatName + >::getString + (stat_online_resources_harvested).c_str + (), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_resources_harvested).c_str + ()) + + endStats.getResourcesHarvested + (factionIndex)); + if (endStats.getPlayerLeftBeforeEnd(factionIndex)) { + steamInstance->setStatAsInt(EnumParser < + SteamStatName >::getString + (stat_online_quit_before_end).c_str + (), + steamInstance->getStatAsInt + (EnumParser < + SteamStatName + >::getString + (stat_online_quit_before_end).c_str + ()) + 1); + } + steamInstance->setStatAsDouble(EnumParser < + SteamStatName >::getString + (stat_online_minutes_played).c_str + (), + steamInstance->getStatAsDouble + (EnumParser < + SteamStatName + >::getString + (stat_online_minutes_played).c_str + ()) + + getTimeDuationMinutes + (endStats.getFramesToCalculatePlaytime + (), GameConstants::updateFps)); + } + } + } + + // Write out achievements here + for (int factionIndex = 0; + factionIndex < game->getWorld()->getFactionCount(); + ++factionIndex) { + if (factionIndex == game->getWorld()->getThisFactionIndex()) { + //printf("\nWriting out game stats for Faction Index: %d won status: %d\n",factionIndex,endStats.getVictory(factionIndex)); + if (endStats.getVictory(factionIndex)) { + if (steamInstance->isUnlocked(EnumParser < + SteamAchievementName + >::getString + (ACH_WIN_ONE_GAME).c_str()) == + false) { + steamInstance->unlock(EnumParser < + SteamAchievementName + >::getString(ACH_WIN_ONE_GAME).c_str + ()); + } + if (NetworkManager::getInstance().isNetworkGame()) { + if (steamInstance->isUnlocked(EnumParser < + SteamAchievementName + >::getString + (ACH_WIN_ONE_GAME_ONLINE).c_str + ()) == false) { + steamInstance->unlock(EnumParser < + SteamAchievementName >::getString + (ACH_WIN_ONE_GAME_ONLINE).c_str()); + } + } + } + } + } + + //printf("\nSTEAM Store Stats!\n"); + steamInstance->storeStats(); + //printf("\nSTEAM Refresh Stats!\n"); + steamInstance->requestRefreshStats(); + } + } + + void Game::exitGameState(Program * program, Stats & endStats) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Game *game = dynamic_cast (program->getState()); + + //printf("game = %p\n",game); + + if (game) { + game->setEndGameTeamWinnersAndLosers(); + game->endGame(); + } + + if ((game != NULL && game->isMasterserverMode() == true) || + Config::getInstance().getBool("AutoTest") == true) { + printf("Game ending with stats:\n"); + printf("-----------------------\n"); + + string gameStats = endStats.getStats(); + printf("%s", gameStats.c_str()); + + printf("-----------------------\n"); + } + + saveStatsToSteam(game, endStats); + + ProgramState *newState = new BattleEnd(program, &endStats, game); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + program->setState(newState, false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + // ==================== PRIVATE ==================== + + void + Game::highlightUnit(int unitId, float radius, float thickness, + Vec4f color) { + HighlightSpecialUnitInfo info; + info.radius = radius; + info.thickness = thickness; + info.color = color; + unitHighlightList[unitId] = info; + } + + void Game::unhighlightUnit(int unitId) { + unitHighlightList.erase(unitId); + } + + // ==================== render ==================== + + void Game::render3d() { + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + Renderer & renderer = Renderer::getInstance(); + + //init + renderer.reset3d(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [reset3d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // renderer.computeVisibleQuad(); + // if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] renderFps = %d took msecs: %lld [computeVisibleQuad]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,renderFps,chrono.getMillis()); + // if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + renderer.loadGameCameraMatrix(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [loadGameCameraMatrix]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + renderer.computeVisibleQuad(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [computeVisibleQuad]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + renderer.setupLighting(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [setupLighting]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //shadow map + renderer.renderShadowsToTexture(avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderShadowsToTexture]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //clear buffers + renderer.clearBuffers(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d renderFps = %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //surface + renderer.renderSurface(avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSurface]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //selection circles + renderer.renderSelectionEffects(healthbarMode); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderSelectionEffects]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // renderTeamColorCircle + if ((renderExtraTeamColor & renderTeamColorCircleBit) > 0) { + renderer.renderTeamColorCircle(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, renderFps, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + chrono.start(); + } + + renderer.renderSpecialHighlightUnits(unitHighlightList); + + // renderTeamColorCircle + renderer.renderMorphEffects(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //objects + renderer.renderObjects(avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //ground units + renderer.renderUnits(false, avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //water + renderer.renderWater(); + renderer.renderWaterEffects(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderWater]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //air units + renderer.renderUnits(true, avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnits]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //particles + renderer.renderParticleManager(rsGame); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderParticleManager]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //renderOnTopBars (aka Healthbars) + if (photoModeEnabled == false) { + renderer.renderHealthBars(healthbarMode); + } + + // renderTeamColorPlane + if ((renderExtraTeamColor & renderTeamColorPlaneBit) > 0) { + renderer.renderTeamColorPlane(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderObjects]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, renderFps, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled && chrono.getMillis() > 0) + chrono.start(); + } + + //mouse 3d + renderer.renderMouse3d(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderMouse3d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + + renderer.renderUnitsToBuild(avgRenderFps); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] renderFps = %d took msecs: %lld [renderUnitsToBuild]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + renderFps, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + renderer.setLastRenderFps(lastRenderFps); + } + + void Game::updateWorldStats() { + world.getStats()->setWorldTimeElapsed(world. + getTimeFlow()->getTime()); + + if (difftime((long int) time(NULL), lastMaxUnitCalcTime) >= 1) { + lastMaxUnitCalcTime = time(NULL); + + int totalUnitcount = 0; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + totalUnitcount += faction->getUnitCount(); + } + + if (world.getStats()->getMaxConcurrentUnitCount() < totalUnitcount) { + world.getStats()->setMaxConcurrentUnitCount(totalUnitcount); + } + world. + getStats()->setTotalEndGameConcurrentUnitCount(totalUnitcount); + world.getStats()->setFramesPlayed(world.getFrameCount()); + } + } + + string Game::getDebugStats(std::map < int, string > &factionDebugInfo) { + string str = ""; + + if (this->masterserverMode == false) { + str += + "MouseXY: " + intToStr(mouseX) + "," + intToStr(mouseY) + "\n"; + + if (world. + getMap()->isInsideSurface(world. + getMap()->toSurfCoords + (mouseCellPos)) == true) { + str += + "MouseXY cell coords: " + intToStr(mouseCellPos.x) + "," + + intToStr(mouseCellPos.y) + "\n"; + } + + str += + "PosObjWord: " + intToStr(gui.getPosObjWorld().x) + "," + + intToStr(gui.getPosObjWorld().y) + "\n"; + } + + str += + "Render FPS: " + intToStr(lastRenderFps) + "[" + + intToStr(avgRenderFps) + "]\n"; + str += + "Update FPS: " + intToStr(lastUpdateFps) + "[" + + intToStr(avgUpdateFps) + "]\n"; + + if (this->masterserverMode == false) { + str += + "GameCamera pos: " + floatToStr(gameCamera.getPos().x) + "," + + floatToStr(gameCamera.getPos().y) + "," + + floatToStr(gameCamera.getPos().z) + "\n"; + //str+= "Cached surfacedata: " + intToStr(renderer.getCachedSurfaceDataSize())+"\n"; + } + + //intToStr(stats.getFramesToCalculatePlaytime()/GameConstants::updateFps/60 + str += + "Time: " + floatToStr(world.getTimeFlow()->getTime(), + 2) + " [" + + floatToStr((double) world. + getStats()->getFramesToCalculatePlaytime() / + (float) GameConstants::updateFps / 60.0, 2) + "]\n"; + + if (SystemFlags::getThreadedLoggerRunning() == true) { + str += + "Log buffer count: " + + intToStr(SystemFlags::getLogEntryBufferCount()) + "\n"; + } + + str += + "UnitRangeCellsLookupItemCache: " + + world.getUnitUpdater()->getUnitRangeCellsLookupItemCacheStats() + + "\n"; + str += + "ExploredCellsLookupItemCache: " + + world.getExploredCellsLookupItemCacheStats() + "\n"; + str += + "FowAlphaCellsLookupItemCache: " + + world.getFowAlphaCellsLookupItemCacheStats() + "\n"; + + const string + selectionType = + toLower(Config:: + getInstance().getString("SelectionType", + Config::colorPicking)); + str += "Selection type: " + toLower(selectionType) + "\n"; + + if (selectionType == Config::colorPicking) { + str += + "Color picking used color list size: " + + intToStr(BaseColorPickEntity::getUsedColorIDListSize()) + "\n"; + } + + //str+= "AllFactionsCacheStats: " + world.getAllFactionsCacheStats()+"\n"; + //str+= "AttackWarningCount: " + intToStr(world.getUnitUpdater()->getAttackWarningCount()) + "\n"; + + str += "Map: " + gameSettings.getMap() + "\n"; + str += "Tileset: " + gameSettings.getTileset() + "\n"; + str += "Techtree: " + gameSettings.getTech() + "\n"; + + if (this->masterserverMode == false) { + Renderer & renderer = Renderer::getInstance(); + str += + "Triangle count: " + intToStr(renderer.getTriangleCount()) + "\n"; + str += "Vertex count: " + intToStr(renderer.getPointCount()) + "\n"; + } + + str += "Frame count:" + intToStr(world.getFrameCount()) + "\n"; + + //visible quad + if (this->masterserverMode == false) { + Renderer & renderer = Renderer::getInstance(); + Quad2i visibleQuad = renderer.getVisibleQuad(); + //Quad2i visibleQuadCamera= renderer.getVisibleQuadFromCamera(); + + str += "Visible quad: "; + for (int i = 0; i < 4; ++i) { + str += + "(" + intToStr(visibleQuad.p[i].x) + "," + + intToStr(visibleQuad.p[i].y) + ") "; + } + // str+= "\n"; + // str+= "Visible quad camera: "; + // for(int i= 0; i<4; ++i){ + // str+= "(" + intToStr(visibleQuadCamera.p[i].x) + "," +intToStr(visibleQuadCamera.p[i].y) + ") "; + // } + str += "\n"; + + str += + "Visible quad area: " + floatToStr(visibleQuad.area()) + + "\n"; + // str+= "Visible quad camera area: " + floatToStr(visibleQuadCamera.area()) +"\n"; + + // Rect2i boundingRect= visibleQuad.computeBoundingRect(); + // Rect2i scaledRect= boundingRect/Map::cellScale; + // scaledRect.clamp(0, 0, world.getMap()->getSurfaceW()-1, world.getMap()->getSurfaceH()-1); + // renderer.renderText3D("#1", coreData.getMenuFontNormal3D(), Vec3f(1.0f), scaledRect.p[0].x, scaledRect.p[0].y, false); + // renderer.renderText3D("#2", coreData.getMenuFontNormal3D(), Vec3f(1.0f), scaledRect.p[1].x, scaledRect.p[1].y, false); + } + + int totalUnitcount = 0; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + totalUnitcount += faction->getUnitCount(); + } + + if (this->masterserverMode == false) { + Renderer & renderer = Renderer::getInstance(); + VisibleQuadContainerCache & qCache = renderer.getQuadCache(); + int visibleUnitCount = (int) qCache.visibleQuadUnitList.size(); + str += + "Visible unit count: " + intToStr(visibleUnitCount) + + " total: " + intToStr(totalUnitcount) + "\n"; + + int visibleObjectCount = (int) qCache.visibleObjectList.size(); + str += + "Visible object count: " + intToStr(visibleObjectCount) + "\n"; + } else { + str += "Total unit count: " + intToStr(totalUnitcount) + "\n"; + } + + // resources + for (int i = 0; i < world.getFactionCount(); ++i) { + string factionInfo = this->gameSettings.getNetworkPlayerName(i); + //factionInfo += " [" + this->gameSettings.getNetworkPlayerUUID(i) + "]"; + float multi = world.getStats()->getResourceMultiplier(i); + string multiplier = "[" + floatToStr(multi, 1) + "]"; + switch (this->gameSettings.getFactionControl(i)) { + case ctCpuEasy: + factionInfo += " CPU Easy" + multiplier; + break; + case ctCpu: + factionInfo += " CPU Normal" + multiplier; + break; + case ctCpuUltra: + factionInfo += " CPU Ultra" + multiplier; + break; + case ctCpuZeta: + factionInfo += " CPU Zeta" + multiplier; + break; + } + + factionInfo += + " [" + formatString(this->gameSettings.getFactionTypeName(i)) + + " team: " + intToStr(this->gameSettings.getTeam(i)) + "]"; + + // bool showResourceDebugInfo = true; + // if(showResourceDebugInfo == true) { + // factionInfo +=" res: "; + // for(int j = 0; j < world.getTechTree()->getResourceTypeCount(); ++j) { + // factionInfo += world.getFaction(i)->getResource(j)->getType()->getName()+":"+intToStr(world.getFaction(i)->getResource(j)->getAmount()); + // factionInfo += " "; + // } + // } + + factionDebugInfo[i] = factionInfo; + } + + return str; + } + + void Game::render2d() { + Renderer & renderer = Renderer::getInstance(); + //Config &config= Config::getInstance(); + CoreData & coreData = CoreData::getInstance(); + + //init + renderer.reset2d(); + + //HUD + if (visibleHUD == true && photoModeEnabled == false) { + renderer.renderHud(); + } + //display + renderer.renderDisplay(); + + //minimap + if (photoModeEnabled == false) { + renderer.renderMinimap(); + } + + renderer.renderVisibleMarkedCells(); + renderer.renderVisibleMarkedCells(true, lastMousePos.x, + lastMousePos.y); + + //selection + renderer.renderSelectionQuad(); + + //highlighted Cells + renderer.renderHighlightedCellsOnMinimap(); + + if (switchTeamConfirmMessageBox.getEnabled() == true) { + renderer.renderMessageBox(&switchTeamConfirmMessageBox); + } + + if (disconnectPlayerConfirmMessageBox.getEnabled() == true) { + renderer.renderMessageBox(&disconnectPlayerConfirmMessageBox); + } + + //exit message box + if (errorMessageBox.getEnabled()) { + renderer.renderMessageBox(&errorMessageBox); + } + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } + + //script message box + if (mainMessageBox.getEnabled() == false && + errorMessageBox.getEnabled() == false && + scriptManager.getMessageBoxEnabled()) { + renderer.renderMessageBox(scriptManager.getMessageBox()); + } + + //script display text + if (!scriptManager.getDisplayText().empty() + && !scriptManager.getMessageBoxEnabled()) { + Vec4f fontColor = getGui()->getDisplay()->getColor(); + + if (Renderer::renderText3DEnabled == true) { + renderer.renderText3D(scriptManager.getDisplayText(), + coreData.getMenuFontNormal3D(), + Vec3f(fontColor.x, fontColor.y, + fontColor.z), 200, 660, false); + } else { + renderer.renderText(scriptManager.getDisplayText(), + coreData.getMenuFontNormal(), + Vec3f(fontColor.x, fontColor.y, + fontColor.z), 200, 660, false); + } + } + + renderVideoPlayer(); + + renderer.renderPopupMenu(&popupMenu); + renderer.renderPopupMenu(&popupMenuSwitchTeams); + renderer.renderPopupMenu(&popupMenuDisconnectPlayer); + + if (program != NULL) + program->renderProgramMsgBox(); + + renderer.renderChatManager(&chatManager); + + //debug info + bool perfLogging = false; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled == true + || SystemFlags::getSystemSettingType(SystemFlags:: + debugWorldSynch).enabled == + true) { + perfLogging = true; + } + + string str = ""; + std::map < int, string > factionDebugInfo; + + if (renderer.getShowDebugUI() == true || + (perfLogging == true + && difftime((long int) time(NULL), lastRenderLog2d) >= 1)) { + str = getDebugStats(factionDebugInfo); + } + + if (this->getRenderInGamePerformance() == true) { + renderer.renderPerformanceStats(); + } else { + static time_t lastGamePerfCheck = time(NULL); + if (difftime((long int) time(NULL), lastGamePerfCheck) > 3) { + lastGamePerfCheck = time(NULL); + + getGamePerformanceCounts(true); + } + } + + if (renderer.getShowDebugUI() == true) { + const Metrics & metrics = Metrics::getInstance(); + int mh = metrics.getMinimapH(); + + if (this->getRenderInGamePerformance() == true) { + mh = mh + ((int) gamePerformanceCounts.size() * 14); + } + + const Vec4f fontColor = getGui()->getDisplay()->getColor(); + + if (Renderer::renderText3DEnabled == true) { + renderer.renderTextShadow3D(str, + coreData.getMenuFontNormal3D(), + fontColor, 10, + metrics.getVirtualH() - mh - 60, + false); + } else { + renderer.renderTextShadow(str, coreData.getMenuFontNormal(), + fontColor, 10, + metrics.getVirtualH() - mh - 60, false); + } + + vector < string > lineTokens; + Tokenize(str, lineTokens, "\n"); + int + fontHeightNormal = + (Renderer::renderText3DEnabled == + true ? coreData.getMenuFontNormal3D()-> + getMetrics()->getHeight("W") : coreData.getMenuFontNormal()-> + getMetrics()->getHeight("W")); + int fontHeightBig = + (Renderer::renderText3DEnabled == + true ? coreData.getMenuFontBig3D()-> + getMetrics()->getHeight("W") : coreData.getMenuFontBig()-> + getMetrics()->getHeight("W")); + int playerPosY = (int) lineTokens.size() * fontHeightNormal; + + //printf("lineTokens.size() = %d\n",lineTokens.size()); + + for (int i = 0; i < world.getFactionCount(); ++i) { + string factionInfo = factionDebugInfo[i]; + Vec3f + playerColor = + world.getFaction(i)->getTexture()-> + getPixmapConst()->getPixel3f(0, 0); + + if (Renderer::renderText3DEnabled == true) { + renderer.renderText3D(factionInfo, + coreData.getMenuFontBig3D(), + Vec4f(playerColor.x, playerColor.y, + playerColor.z, 1.0), 10, + //metrics.getVirtualH() - mh - 90 - 280 - (i * 16), + metrics.getVirtualH() - mh - 60 - + playerPosY - fontHeightBig - + (fontHeightBig * i), false); + } else { + renderer.renderText(factionInfo, + coreData.getMenuFontBig(), + Vec4f(playerColor.x, playerColor.y, + playerColor.z, 1.0), 10, + //metrics.getVirtualH() - mh - 90 - 280 - (i * 16), + metrics.getVirtualH() - mh - 60 - + playerPosY - fontHeightBig - + (fontHeightBig * i), false); + } + } + + if ((renderer.getShowDebugUILevel() & debugui_unit_titles) == + debugui_unit_titles) { + if (renderer.getAllowRenderUnitTitles() == false) { + renderer.setAllowRenderUnitTitles(true); + } + + if (Renderer::renderText3DEnabled == true) { + renderer.renderUnitTitles3D(coreData.getMenuFontNormal3D(), + Vec3f(1.0f)); + } else { + renderer.renderUnitTitles(coreData.getMenuFontNormal(), + Vec3f(1.0f)); + } + } + } + + //network status + if (renderNetworkStatus == true) { + if (NetworkManager::getInstance().getGameNetworkInterface() != NULL) { + const Metrics & metrics = Metrics::getInstance(); + int mx = metrics.getMinimapX(); + //int my= metrics.getMinimapY(); + int mw = metrics.getMinimapW(); + //int mh= metrics.getMinimapH(); + const Vec4f fontColor = getGui()->getDisplay()->getColor(); + + if (Renderer::renderText3DEnabled == true) { + renderer. + renderTextShadow3D(NetworkManager:: + getInstance().getGameNetworkInterface + ()->getNetworkStatus(), + coreData.getMenuFontNormal3D(), fontColor, + mx + mw + 5, + metrics.getVirtualH() - 30 - 20, false); + } else { + renderer. + renderTextShadow(NetworkManager:: + getInstance().getGameNetworkInterface + ()->getNetworkStatus(), + coreData.getMenuFontNormal(), fontColor, + mx + mw + 5, metrics.getVirtualH() - 30 - 20, + false); + } + } + } + + // clock + if (photoModeEnabled == false && timeDisplay == true) { + renderer.renderClock(); + } + + //resource info + if (photoModeEnabled == false) { + if (this->masterserverMode == false) { + renderer.renderResourceStatus(); + } + renderer.renderConsole(&console, + showFullConsole ? consoleFull : + consoleNormal); + } + + //2d mouse + renderer.renderMouse2d(mouseX, mouseY, mouse2d, + gui.isSelectingPos() ? 1.f : 0.f); + + if (perfLogging == true + && difftime((long int) time(NULL), lastRenderLog2d) >= 1) { + lastRenderLog2d = time(NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] Statistics: %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, str.c_str()); + } + } + + + // ==================== misc ==================== + + void Game::checkWinner() { + // lookup int is team #, value is players alive on team + std::map < int, int > teamsAlive = getTeamsAlive(); + + if (gameOver == false || teamsAlive.size() > 1) { + if (gameSettings.getDefaultVictoryConditions()) { + checkWinnerStandard(); + } else { + checkWinnerScripted(); + } + } + } + + void Game::setEndGameTeamWinnersAndLosers() { + //bool lose= false; + bool + checkTeamIndex = !(this->masterserverMode == false + && world. + getThisFaction()->getPersonalityType() != + fpt_Observer); + + // lookup int is team #, value is players alive on team + std::map < int, int > teamsAlive; + for (int i = 0; i < world.getFactionCount(); ++i) { + if (checkTeamIndex == false || i != world.getThisFactionIndex()) { + if (factionLostGame(world.getFaction(i)) == false) { + teamsAlive[world.getFaction(i)->getTeam()] = + teamsAlive[world.getFaction(i)->getTeam()] + 1; + } + } + } + + // did some team win + if (teamsAlive.size() <= 1) { + for (int i = 0; i < world.getFactionCount(); ++i) { + if (checkTeamIndex == false || i != world.getThisFactionIndex()) { + if (teamsAlive.find(world.getFaction(i)->getTeam()) != + teamsAlive.end()) { + world.getStats()->setVictorious(i); + } else if (i == world.getThisFactionIndex()) { + //lose= true; + } + } + } + bool firstGameOverTrigger = false; + if (gameOver == false) { + firstGameOverTrigger = true; + gameOver = true; + } + if (this->gameSettings.isNetworkGame() == false || + this->gameSettings.getEnableObserverModeAtEndGame() == true) { + // Let the happy winner view everything left in the world + + // This caused too much LAG for network games + if (this->gameSettings.isNetworkGame() == false) { + Renderer::getInstance().setPhotoMode(true); + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } + // END + } + + if (firstGameOverTrigger == true) { + scriptManager.onGameOver(true); + } + + if (world.getFactionCount() == 1 + && world.getFaction(0)->getPersonalityType() == fpt_Observer) { + //printf("!!!!!!!!!!!!!!!!!!!!"); + + //gameCamera.setMoveY(100.0); + gameCamera.zoom(-300); + //gameCamera.update(); + } + // else { + // if(lose == true) { + // showLoseMessageBox(); + // } + // else { + // showWinMessageBox(); + // } + // } + } + } + + std::map < int, int > Game::getTeamsAlive() { + std::map < int, int > teamsAlive; + for (int factionIndex = 0; factionIndex < world.getFactionCount(); + ++factionIndex) { + if (factionIndex != world.getThisFactionIndex()) { + if (factionLostGame(world.getFaction(factionIndex)) == false) { + teamsAlive[world.getFaction(factionIndex)->getTeam()] = + teamsAlive[world.getFaction(factionIndex)->getTeam()] + 1; + } + } + } + // did some team win + return teamsAlive; + } + + void Game::checkWinnerStandardHeadlessOrObserver() { + // lookup int is team #, value is players alive on team + std::map < int, int > teamsAlive = getTeamsAlive(); + + // did some team win + if (teamsAlive.size() <= 1) { + if (this->masterserverMode == true) { + printf("Game finished...\n"); + } + for (int factionIndex = 0; factionIndex < world.getFactionCount(); + ++factionIndex) { + + Faction *faction = world.getFaction(factionIndex); + if (factionIndex != world.getThisFactionIndex() && + teamsAlive.find(faction->getTeam()) != teamsAlive.end()) { + + world.getStats()->setVictorious(factionIndex); + if (this->masterserverMode == true) { + + printf("Player: %s is on the winning team #: %d\n", + this->gameSettings.getNetworkPlayerName(factionIndex). + c_str(), faction->getTeam()); + } + } + } + bool wasGameOverAlready = gameOver; + gameOver = true; + + // Only need to process this once + if (wasGameOverAlready == false) { + if (this->gameSettings.isNetworkGame() == false || + this->gameSettings.getEnableObserverModeAtEndGame() == true) { + + // Let the happy winner view everything left in the world + // This caused too much LAG for network games + if (this->gameSettings.isNetworkGame() == false) { + + Renderer::getInstance().setPhotoMode(true); + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } + // END + } + scriptManager.onGameOver(true); + if (world.getFactionCount() == 1 && + world.getFaction(0)->getPersonalityType() == fpt_Observer) { + gameCamera.zoom(-300); + } else { + showWinMessageBox(); + } + } + } + } + + void Game::checkWinnerStandardPlayer() { + //lose + bool lose = false; + if (factionLostGame(world.getThisFaction()) == true) { + + bool playerLostGame = true; + // Team Shared units enabled? + if (isFlagType1BitEnabled(ft1_allow_shared_team_units) == true) { + + // Check if all team members have lost? + for (int factionIndex = 0; + factionIndex < world.getFactionCount(); ++factionIndex) { + + if (world.getFaction(factionIndex)->getPersonalityType() != + fpt_Observer) { + if (world. + getFaction(factionIndex)->isAlly(world. + getThisFaction()) == + true + && factionLostGame(world.getFaction(factionIndex)) == + false) { + + playerLostGame = false; + break; + } + } + } + } + + if (playerLostGame == true) { + lose = true; + for (int factionIndex = 0; + factionIndex < world.getFactionCount(); ++factionIndex) { + + if (world.getFaction(factionIndex)->getPersonalityType() != + fpt_Observer) { + if (world. + getFaction(factionIndex)->isAlly(world. + getThisFaction()) == + false + && factionLostGame(world.getFaction(factionIndex)) == + false) { + + world.getStats()->setVictorious(factionIndex); + } + } + } + bool wasGameOverAlready = gameOver; + gameOver = true; + + // Only need to process losing once + if (wasGameOverAlready == false) { + if (this->gameSettings.isNetworkGame() == false || + this->gameSettings.getEnableObserverModeAtEndGame() == true) { + // Let the poor user watch everything unfold + // This caused too much LAG for network games + if (this->gameSettings.isNetworkGame() == false) { + Renderer::getInstance().setPhotoMode(true); + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } + // END + // but don't let him cheat via teamchat + chatManager.setDisableTeamMode(true); + } + scriptManager.onGameOver(!lose); + showLoseMessageBox(); + } + } + } + + //win + if (lose == false) { + bool win = true; + for (int factionIndex = 0; factionIndex < world.getFactionCount(); + ++factionIndex) { + + if (factionIndex != world.getThisFactionIndex()) { + if (world.getFaction(factionIndex)->getPersonalityType() != + fpt_Observer) { + + if (factionLostGame(world.getFaction(factionIndex)) == + false + && world.getFaction(factionIndex)-> + isAlly(world.getThisFaction()) == false) { + + win = false; + } + } + } + } + + if (win) { + for (int factionIndex = 0; + factionIndex < world.getFactionCount(); ++factionIndex) { + if (world.getFaction(factionIndex)->getPersonalityType() != + fpt_Observer) { + if (world. + getFaction(factionIndex)->isAlly(world.getThisFaction())) { + + world.getStats()->setVictorious(factionIndex); + } + } + } + + bool wasGameOverAlready = gameOver; + gameOver = true; + + // Only need to process winning once + if (wasGameOverAlready == false) { + if (this->gameSettings.isNetworkGame() == false || + this->gameSettings.getEnableObserverModeAtEndGame() == true) { + // Let the happy winner view everything left in the world + + // This caused too much LAG for network games + if (this->gameSettings.isNetworkGame() == false) { + Renderer::getInstance().setPhotoMode(true); + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } + // END + } + scriptManager.onGameOver(win); + showWinMessageBox(); + } + } + } + } + + void Game::checkWinnerStandard() { + if (world.getFactionCount() <= 0) { + return; + } + if (this->masterserverMode == true || + world.getThisFaction()->getPersonalityType() == fpt_Observer) { + checkWinnerStandardHeadlessOrObserver(); + } else { + checkWinnerStandardPlayer(); + } + } + + void Game::checkWinnerScripted() { + if (scriptManager.getIsGameOver()) { + bool wasGameOverAlready = gameOver; + gameOver = true; + + + for (int index = 0; index < world.getFactionCount(); ++index) { + if (scriptManager.getPlayerModifiers(index)->getWinner()) { + + world.getStats()->setVictorious(index); + } + } + + // Only need to process winning once + if (wasGameOverAlready == false) { + if (this->gameSettings.isNetworkGame() == false || + this->gameSettings.getEnableObserverModeAtEndGame() == true) { + + // Let the happy winner view everything left in the world + // This caused too much LAG for network games + if (this->gameSettings.isNetworkGame() == false) { + Renderer::getInstance().setPhotoMode(true); + gameCamera.setMaxHeight(PHOTO_MODE_MAXHEIGHT); + } + // END + } + + if (this->masterserverMode == true || + world.getThisFaction()->getPersonalityType() == fpt_Observer) { + showWinMessageBox(); + } else { + scriptManager. + onGameOver(scriptManager.getPlayerModifiers + (world.getThisFactionIndex())->getWinner()); + + if (scriptManager.getPlayerModifiers + (world.getThisFactionIndex())->getWinner()) { + showWinMessageBox(); + } else { + showLoseMessageBox(); + } + } + } + } + } + + bool Game::isFlagType1BitEnabled(FlagTypes1 type) const { + return ((gameSettings.getFlagTypes1() & (uint32) type) == + (uint32) type); + } + + bool Game::factionLostGame(int factionIndex) { + return factionLostGame(world.getFaction(factionIndex)); + } + + bool Game::factionLostGame(const Faction * faction) { + if (faction != NULL) { + for (int factionIndex = 0; factionIndex < faction->getUnitCount(); + ++factionIndex) { + const UnitType *ut = faction->getUnit(factionIndex)->getType(); + if (ut->getCountInVictoryConditions() == ucvcNotSet) { + if (faction->getUnit(factionIndex)-> + getType()->hasSkillClass(scBeBuilt)) { + return false; + } + } else if (ut->getCountInVictoryConditions() == ucvcTrue) { + return false; + } + } + } + return true; + } + + //bool Game::hasBuilding(const Faction *faction) { + // if(faction != NULL) { + // for(int i=0; igetUnitCount(); ++i) { + // if(faction->getUnit(i)->getType()->hasSkillClass(scBeBuilt)) { + // return true; + // } + // } + // } + // return false; + //} + + void Game::incSpeed() { + if (disableSpeedChange == true) { + return; + } + + Lang & lang = Lang::getInstance(); + + if (this->speed < Config::getInstance().getInt("FastSpeedLoops")) { + if (this->speed == 0) { + this->speed = 1; + } else { + this->speed++; + } + console.addLine(lang.getString("GameSpeedSet") + " " + + ((this->speed == + 0) ? lang.getString("Slow") : (this->speed == + 1) ? + lang.getString("Normal") : "x" + + intToStr(this->speed))); + } + } + + void Game::decSpeed() { + if (disableSpeedChange == true) { + return; + } + + Lang & lang = Lang::getInstance(); + if (this->speed > 0) { + this->speed--; + console.addLine(lang.getString("GameSpeedSet") + " " + + ((this->speed == + 0) ? lang.getString("Slow") : (this->speed == + 1) ? + lang.getString("Normal") : "x" + + intToStr(this->speed))); + } + } + + void + Game::setPaused(bool value, bool forceAllowPauseStateChange, + bool clearCaches, bool joinNetworkGame) { + bool + speedChangesAllowed = + !NetworkManager::getInstance().isNetworkGame(); + //printf("Toggle pause value = %d, speedChangesAllowed = %d, forceAllowPauseStateChange = %d\n",value,speedChangesAllowed,forceAllowPauseStateChange); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "game.cpp line: %d setPaused value: %d clearCaches: %d forceAllowPauseStateChange: %d speedChangesAllowed: %d pausedForJoinGame: %d joinNetworkGame: %d\n", + __LINE__, value, clearCaches, + forceAllowPauseStateChange, + speedChangesAllowed, pausedForJoinGame, + joinNetworkGame); + //printf("Line: %d setPaused value: %d clearCaches: %d forceAllowPauseStateChange: %d speedChangesAllowed: %d pausedForJoinGame: %d joinNetworkGame: %d\n",__LINE__,value,clearCaches,forceAllowPauseStateChange,speedChangesAllowed,pausedForJoinGame,joinNetworkGame); + + if (forceAllowPauseStateChange == true || speedChangesAllowed == true) { + //printf("setPaused paused = %d, value = %d\n",paused,value); + + NetworkManager & networkManager = NetworkManager::getInstance(); + + // Cannot change pause state while client is joining in progress game + if (pausedForJoinGame == true && joinNetworkGame == false && + networkManager.getNetworkRole() == nrServer) { + + ServerInterface *server = + NetworkManager::getInstance().getServerInterface(); + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + this->gameSettings.getUniqueNetworkPlayerLanguages(); + + bool haveClientConnectedButNoReady = false; + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + + MutexSafeWrapper + safeMutex(server->getSlotMutex + (faction->getStartLocationIndex()), CODE_AT_LINE); + ConnectionSlot *slot = + server->getSlot(faction->getStartLocationIndex(), + false); + if (slot != NULL && slot->isConnected() == true + && slot->isReady() == false) { + for (unsigned int i = 0; i < languageList.size(); ++i) { + + char szMsg[8096] = ""; + if (lang.hasString("JoinPlayerToCurrentGameLaunch", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("JoinPlayerToCurrentGameLaunch", + languageList[i]).c_str(), + slot->getName().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is about to join the game, please wait...", + slot->getName().c_str()); + } + + safeMutex.ReleaseLock(); + + bool localEcho = lang.isLanguageLocal(languageList[i]); + server->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + sleep(1); + + haveClientConnectedButNoReady = true; + } + } + } + + if (haveClientConnectedButNoReady == true) { + return; + } + } + + Lang & lang = Lang::getInstance(); + if (value == false) { + console.addLine(lang.getString("GameResumed")); + + bool wasPausedForJoinGame = pausedForJoinGame; + paused = false; + pausedForJoinGame = false; + pausedBeforeJoinGame = false; + pauseStateChanged = true; + + if (clearCaches == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "game.cpp line: %d Clear Caches for resume in progress game\n", + __LINE__); + //printf("Line: %d Clear Caches for resume in progress game\n",__LINE__); + + world.clearCaches(); + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + faction->clearCaches(); + } + world.refreshAllUnitExplorations(); + } + setupPopupMenus(false); + + if (networkManager.getNetworkRole() == nrClient && + wasPausedForJoinGame == true) { + initialResumeSpeedLoops = true; + } + + commander.setPauseNetworkCommands(false); + } else { + console.addLine(lang.getString("GamePaused")); + + if (joinNetworkGame == true) { + pausedBeforeJoinGame = paused; + } + paused = true; + pausedForJoinGame = joinNetworkGame; + pauseStateChanged = true; + + if (clearCaches == true) { + //printf("Line: %d Clear Caches for resume in progress game\n",__LINE__); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugWorldSynch).enabled) + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "game.cpp line: %d Clear Caches for resume in progress game\n", + __LINE__); + + world.clearCaches(); + for (int i = 0; i < world.getFactionCount(); ++i) { + Faction *faction = world.getFaction(i); + faction->clearCaches(); + } + world.refreshAllUnitExplorations(); + } + pauseRequestSent = false; + + commander.setPauseNetworkCommands(joinNetworkGame); + } + //printf("setPaused new paused = %d\n",paused); + } + } + + bool Game::getPaused() { + bool + speedChangesAllowed = + !NetworkManager::getInstance().isNetworkGame(); + if (speedChangesAllowed) { + if (popupMenu.getVisible() == true + || popupMenuSwitchTeams.getVisible() == true) { + return true; + } + // if(mainMessageBox.getEnabled() == true || errorMessageBox.getEnabled() == true){ + // return true; + // } + if (currentUIState != NULL) { + return true; + } + } + return paused; + } + + int Game::getUpdateLoops() { + if (commander.hasReplayCommandListForFrame() == true) { + return 1; + } + + if (getPaused()) { + return 0; + } else if (this->speed == 0) { + return updateFps % 2 == 0 ? 1 : 0; + } else + return this->speed; + } + + void Game::showLoseMessageBox() { + Lang & lang = Lang::getInstance(); + + NetworkManager & networkManager = NetworkManager::getInstance(); + if (networkManager.isNetworkGame() == true + && networkManager.getNetworkRole() == nrServer) { + showMessageBox(lang.getString("YouLose") + " " + + lang.getString("ExitBattleServerQuestion"), + lang.getString("BattleOver"), false); + } else { + showMessageBox(lang.getString("YouLose") + " " + + lang.getString("ExitBattleQuestion"), + lang.getString("BattleOver"), false); + } + } + + void Game::showWinMessageBox() { + Lang & lang = Lang::getInstance(); + + if (this->masterserverMode == true + || world.getThisFaction()->getPersonalityType() == fpt_Observer) { + showMessageBox(lang.getString("GameOver") + " " + + lang.getString("ExitBattleQuestion"), + lang.getString("BattleOver"), false); + } else { + showMessageBox(lang.getString("YouWin") + " " + + lang.getString("ExitBattleQuestion"), + lang.getString("BattleOver"), false); + } + } + + void + Game::showMessageBox(const string & text, const string & header, + bool toggle) { + if (toggle == false) { + mainMessageBox.setEnabled(false); + } + + if (mainMessageBox.getEnabled() == false) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void + Game::showErrorMessageBox(const string & text, const string & header, + bool toggle) { + if (toggle == false) { + errorMessageBox.setEnabled(false); + } + + if (errorMessageBox.getEnabled() == false) { + errorMessageBox.setText(text); + errorMessageBox.setHeader(header); + errorMessageBox.setEnabled(true); + } else { + errorMessageBox.setEnabled(false); + } + } + + void Game::startPerformanceTimer() { + captureAvgTestStatus = true; + updateFpsAvgTest = -1; + renderFpsAvgTest = -1; + } + + void Game::endPerformanceTimer() { + captureAvgTestStatus = false; + } + + Vec2i Game::getPerformanceTimerResults() { + Vec2i results(this->updateFpsAvgTest, this->renderFpsAvgTest); + return results; + } + + void Game::consoleAddLine(string line) { + console.addLine(line); + } + void Game::toggleTeamColorMarker() { + renderExtraTeamColor++; + renderExtraTeamColor = renderExtraTeamColor % 4; + } + + void + Game::addNetworkCommandToReplayList(NetworkCommand * networkCommand, + int worldFrameCount) { + Config & config = Config::getInstance(); + if (config.getBool("SaveCommandsForReplay", "false") == true) { + replayCommandList.push_back(make_pair + (worldFrameCount, *networkCommand)); + } + } + + void Game::renderVideoPlayer() { + if (videoPlayer != NULL) { + if (videoPlayer->isPlaying() == true) { + videoPlayer->playFrame(false); + } else { + delete videoPlayer; + videoPlayer = NULL; + } + } + } + + void Game::playStaticVideo(const string & playVideo) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true) { + + //togglePauseGame(true,true); + tryPauseToggle(true); + setupRenderForVideo(); + + + // Context *c= GraphicsInterface::getInstance().getCurrentContext(); + // SDL_Surface *screen = static_cast(c)->getPlatformContextGlPtr()->getScreen(); + // + // string vlcPluginsPath = Config::getInstance().getString("VideoPlayerPluginsPath",""); + // //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + // Shared::Graphics::VideoPlayer player(playVideo.c_str(), + // screen, + // 0,0, + // screen->w, + // screen->h, + // screen->format->BitsPerPixel, + // vlcPluginsPath, + // SystemFlags::VERBOSE_MODE_ENABLED); + // player.PlayVideo(); + //} + //tryPauseToggle(false); + + playStreamingVideo(playVideo); + playingStaticVideo = true; + } + } + void Game::playStreamingVideo(const string & playVideo) { + if (videoPlayer == NULL) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + Context *c = GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl *glCtx = + static_cast (c)->getPlatformContextGlPtr(); + SDL_Window *window = glCtx->getScreenWindow(); + SDL_Surface *screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + + videoPlayer = + new::Shared::Graphics::VideoPlayer(&Renderer::getInstance(), + playVideo, "", window, 0, + 0, screen->w, screen->h, + screen->format->BitsPerPixel, + false, vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + } + } else { + if (videoPlayer->isPlaying() == false) { + delete videoPlayer; + videoPlayer = NULL; + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + Context *c = + GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl *glCtx = + static_cast (c)->getPlatformContextGlPtr(); + SDL_Window *window = glCtx->getScreenWindow(); + SDL_Surface *screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + + videoPlayer = + new::Shared::Graphics::VideoPlayer(&Renderer::getInstance(), + playVideo, "", window, + 0, 0, screen->w, + screen->h, + screen-> + format->BitsPerPixel, false, + vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + } + } + } + + if (videoPlayer != NULL) { + videoPlayer->initPlayer(); + } + } + void Game::stopStreamingVideo(const string & playVideo) { + if (videoPlayer != NULL) { + videoPlayer->StopVideo(); + } + } + + void Game::stopAllVideo() { + if (videoPlayer != NULL) { + videoPlayer->StopVideo(); + } + } + + bool + Game::clientLagHandler(int slotIndex, + bool networkPauseGameForLaggedClients) { + if (networkPauseGameForLaggedClients == true) { + printf + ("**WARNING** Detected lag from client: %d networkPauseGameForLaggedClients: %d\n", + slotIndex, networkPauseGameForLaggedClients); + } else { + printf + ("==> Requested Resume Game after Pause for lagging client(s)...\n"); + } + + this->networkPauseGameForLaggedClientsRequested = + networkPauseGameForLaggedClients; + this->networkResumeGameForLaggedClientsRequested = + !networkPauseGameForLaggedClients; + return true; + } + + void Game::saveGame() { + string file = this->saveGame(GameConstants::saveGameFilePattern); + char szBuf[8096] = ""; + Lang & lang = Lang::getInstance(); + snprintf(szBuf, 8096, lang.getString("GameSaved", "", true).c_str(), + file.c_str()); + console.addLine(szBuf); + + Config & config = Config::getInstance(); + config.setString("LastSavedGame", file); + config.save(); + } + + string Game::saveGame(string name, const string & path) { + Config & config = Config::getInstance(); + // auto name file if using saved file pattern string + if (name == GameConstants::saveGameFilePattern) { + //time_t curTime = time(NULL); + //struct tm *loctime = localtime (&curTime); + struct tm loctime = threadsafe_localtime(systemtime_now()); + char szBuf2[100] = ""; + strftime(szBuf2, 100, "%Y%m%d_%H%M%S", &loctime); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, name.c_str(), szBuf2); + name = szBuf; + } else if (name == GameConstants::saveGameFileAutoTestDefault) { + //time_t curTime = time(NULL); + //struct tm *loctime = localtime (&curTime); + struct tm loctime = threadsafe_localtime(systemtime_now()); + char szBuf2[100] = ""; + strftime(szBuf2, 100, "%Y%m%d_%H%M%S", &loctime); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, name.c_str(), szBuf2); + name = szBuf; + } + + // Save the file now + string saveGameFile = path + name; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + saveGameFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + saveGameFile; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + saveGameFile = userData + saveGameFile; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Saving game to [%s]\n", saveGameFile.c_str()); + + // This condition will re-play all the commands from a replay file + // INSTEAD of saving from a saved game. + if (config.getBool("SaveCommandsForReplay", "false") == true) { + std::map < string, string > mapTagReplacements; + XmlTree xmlTreeSaveGame(XML_RAPIDXML_ENGINE); + + xmlTreeSaveGame.init("zetaglest-saved-game"); + XmlNode *rootNodeReplay = xmlTreeSaveGame.getRootNode(); + + //std::map mapTagReplacements; + //time_t now = time(NULL); + //struct tm *loctime = localtime (&now); + struct tm loctime = threadsafe_localtime(systemtime_now()); + char szBuf[4096] = ""; + strftime(szBuf, 4095, "%Y-%m-%d %H:%M:%S", &loctime); + + rootNodeReplay->addAttribute("version", glestVersionString, + mapTagReplacements); + rootNodeReplay->addAttribute("timestamp", szBuf, mapTagReplacements); + + XmlNode *gameNodeReplay = rootNodeReplay->addChild("Game"); + gameSettings.saveGame(gameNodeReplay); + + gameNodeReplay->addAttribute("LastWorldFrameCount", + intToStr(world.getFrameCount()), + mapTagReplacements); + + for (unsigned int i = 0; i < replayCommandList.size(); ++i) { + std::pair < int, NetworkCommand > & cmd = replayCommandList[i]; + XmlNode *networkCommandNode = cmd.second.saveGame(gameNodeReplay); + networkCommandNode->addAttribute("worldFrameCount", + intToStr(cmd.first), + mapTagReplacements); + } + + string replayFile = saveGameFile + ".replay"; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Saving game replay commands to [%s]\n", + replayFile.c_str()); + xmlTreeSaveGame.save(replayFile); + } + + XmlTree xmlTree; + xmlTree.init("zetaglest-saved-game"); + XmlNode *rootNode = xmlTree.getRootNode(); + + std::map < string, string > mapTagReplacements; + //time_t now = time(NULL); + //struct tm *loctime = localtime (&now); + struct tm loctime = threadsafe_localtime(systemtime_now()); + char szBuf[4096] = ""; + strftime(szBuf, 4095, "%Y-%m-%d %H:%M:%S", &loctime); + + rootNode->addAttribute("version", glestVersionString, + mapTagReplacements); + rootNode->addAttribute("timestamp", szBuf, mapTagReplacements); + + XmlNode *gameNode = rootNode->addChild("Game"); + //World world; + world.saveGame(gameNode); + //AiInterfaces aiInterfaces; + for (unsigned int i = 0; i < aiInterfaces.size(); ++i) { + AiInterface *aiIntf = aiInterfaces[i]; + if (aiIntf != NULL) { + aiIntf->saveGame(gameNode); + } + } + //Gui gui; + gui.saveGame(gameNode); + //GameCamera gameCamera; + gameCamera.saveGame(gameNode); + //Commander commander; + //Console console; + //ChatManager chatManager; + //ScriptManager scriptManager; + scriptManager.saveGame(gameNode); + + //misc + //Checksum checksum; + gameNode->addAttribute("checksum", intToStr(checksum.getSum()), + mapTagReplacements); + //string loadingText; + // int mouse2d; + gameNode->addAttribute("mouse2d", intToStr(mouse2d), + mapTagReplacements); + // int mouseX; + gameNode->addAttribute("mouseX", intToStr(mouseX), + mapTagReplacements); + // int mouseY; //coords win32Api + gameNode->addAttribute("mouseY", intToStr(mouseY), + mapTagReplacements); + // Vec2i mouseCellPos; + gameNode->addAttribute("mouseCellPos", mouseCellPos.getString(), + mapTagReplacements); + // int updateFps, lastUpdateFps, avgUpdateFps; + // int totalRenderFps, renderFps, lastRenderFps, avgRenderFps,currentAvgRenderFpsTotal; + //Uint64 tickCount; + gameNode->addAttribute("tickCount", intToStr(tickCount), + mapTagReplacements); + + //bool paused; + gameNode->addAttribute("paused", intToStr(paused), + mapTagReplacements); + //bool gameOver; + gameNode->addAttribute("gameOver", intToStr(gameOver), + mapTagReplacements); + //bool renderNetworkStatus; + //bool showFullConsole; + //bool mouseMoved; + //float scrollSpeed; + gameNode->addAttribute("scrollSpeed", floatToStr(scrollSpeed, 6), + mapTagReplacements); + //bool camLeftButtonDown; + //bool camRightButtonDown; + //bool camUpButtonDown; + //bool camDownButtonDown; + + //Speed speed; + gameNode->addAttribute("speed", intToStr(speed), mapTagReplacements); + + //GraphicMessageBox mainMessageBox; + //GraphicMessageBox errorMessageBox; + + //misc ptr + //ParticleSystem *weatherParticleSystem; + if (weatherParticleSystem != NULL) { + weatherParticleSystem->saveGame(gameNode); + } + //GameSettings gameSettings; + gameSettings.saveGame(gameNode); + //Vec2i lastMousePos; + gameNode->addAttribute("lastMousePos", lastMousePos.getString(), + mapTagReplacements); + //time_t lastRenderLog2d; + gameNode->addAttribute("lastRenderLog2d", intToStr(lastRenderLog2d), + mapTagReplacements); + //DisplayMessageFunction originalDisplayMsgCallback; + //bool isFirstRender; + gameNode->addAttribute("isFirstRender", intToStr(isFirstRender), + mapTagReplacements); + + //bool quitTriggeredIndicator; + //int original_updateFps; + gameNode->addAttribute("original_updateFps", + intToStr(original_updateFps), + mapTagReplacements); + //int original_cameraFps; + gameNode->addAttribute("original_cameraFps", + intToStr(original_cameraFps), + mapTagReplacements); + + //bool captureAvgTestStatus; + gameNode->addAttribute("captureAvgTestStatus", + intToStr(captureAvgTestStatus), + mapTagReplacements); + //int updateFpsAvgTest; + gameNode->addAttribute("updateFpsAvgTest", intToStr(updateFpsAvgTest), + mapTagReplacements); + //int renderFpsAvgTest; + gameNode->addAttribute("renderFpsAvgTest", intToStr(renderFpsAvgTest), + mapTagReplacements); + + //int renderExtraTeamColor; + gameNode->addAttribute("renderExtraTeamColor", + intToStr(renderExtraTeamColor), + mapTagReplacements); + + //static const int renderTeamColorCircleBit=1; + //static const int renderTeamColorPlaneBit=2; + + //bool photoModeEnabled; + gameNode->addAttribute("photoModeEnabled", intToStr(photoModeEnabled), + mapTagReplacements); + //bool visibleHUD; + gameNode->addAttribute("visibleHUD", intToStr(visibleHUD), + mapTagReplacements); + + //bool timeDisplay + gameNode->addAttribute("timeDisplay", intToStr(timeDisplay), + mapTagReplacements); + + //bool withRainEffect; + gameNode->addAttribute("withRainEffect", intToStr(withRainEffect), + mapTagReplacements); + //Program *program; + + //bool gameStarted; + gameNode->addAttribute("gameStarted", intToStr(gameStarted), + mapTagReplacements); + + //time_t lastMaxUnitCalcTime; + gameNode->addAttribute("lastMaxUnitCalcTime", + intToStr(lastMaxUnitCalcTime), + mapTagReplacements); + + //PopupMenu popupMenu; + //PopupMenu popupMenuSwitchTeams; + + //std::map switchTeamIndexMap; + //GraphicMessageBox switchTeamConfirmMessageBox; + + //int exitGamePopupMenuIndex; + //int joinTeamPopupMenuIndex; + //int pauseGamePopupMenuIndex; + //int keyboardSetupPopupMenuIndex; + //GLuint statelist3dMenu; + //ProgramState *currentUIState; + + //bool masterserverMode; + + //StrSound *currentAmbientSound; + + //time_t lastNetworkPlayerConnectionCheck; + gameNode->addAttribute("lastNetworkPlayerConnectionCheck", + intToStr(lastNetworkPlayerConnectionCheck), + mapTagReplacements); + + //time_t lastMasterServerGameStatsDump; + gameNode->addAttribute("lastMasterServerGameStatsDump", + intToStr(lastMasterServerGameStatsDump), + mapTagReplacements); + + XmlNode *unitHighlightListNode = + gameNode->addChild("unitHighlightList"); + //for(unsigned int i = 0; i < unitHighlightList.size(); ++i) { + for (std::map < int, HighlightSpecialUnitInfo >::iterator iterMap = + unitHighlightList.begin(); iterMap != unitHighlightList.end(); + ++iterMap) { + HighlightSpecialUnitInfo & info = iterMap->second; + XmlNode *infoNode = unitHighlightListNode->addChild("info"); + infoNode->addAttribute("unitid", intToStr(iterMap->first), + mapTagReplacements); + infoNode->addAttribute("radius", floatToStr(info.radius, 6), + mapTagReplacements); + infoNode->addAttribute("thickness", floatToStr(info.thickness, 6), + mapTagReplacements); + infoNode->addAttribute("color", info.color.getString(), + mapTagReplacements); + } + + gameNode->addAttribute("disableSpeedChange", + intToStr(disableSpeedChange), + mapTagReplacements); + + xmlTree.save(saveGameFile); + + if (masterserverMode == false) { + // take Screenshot + string jpgFileName = saveGameFile + ".jpg"; + // menu is already disabled, last rendered screen is still with enabled one. Lets render again: + render3d(); + render2d(); + Renderer::getInstance().saveScreen(jpgFileName, + config.getInt + ("SaveGameScreenshotWidth", + "800"), + config.getInt + ("SaveGameScreenshotHeight", + "600")); + } + + return saveGameFile; + } + + void + Game::loadGame(string name, Program * programPtr, + bool isMasterserverMode, + const GameSettings * joinGameSettings) { + Config & config = Config::getInstance(); + // This condition will re-play all the commands from a replay file + // INSTEAD of saving from a saved game. + if (joinGameSettings == NULL + && config.getBool("SaveCommandsForReplay", "false") == true) { + XmlTree xmlTreeReplay(XML_RAPIDXML_ENGINE); + std::map < string, string > mapExtraTagReplacementValues; + xmlTreeReplay.load(name + ".replay", + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues), true); + + const XmlNode *rootNode = xmlTreeReplay.getRootNode(); + + if (rootNode->hasChild("zetaglest-saved-game") == true) { + rootNode = rootNode->getChild("zetaglest-saved-game"); + } + + //const XmlNode *versionNode= rootNode->getChild("zetaglest-saved-game"); + const XmlNode *versionNode = rootNode; + + Lang & lang = Lang::getInstance(); + string gameVer = versionNode->getAttribute("version")->getValue(); + if (gameVer != glestVersionString + && checkVersionComptability(gameVer, + glestVersionString) == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("SavedGameBadVersion").c_str(), + gameVer.c_str(), glestVersionString.c_str()); + throw megaglest_runtime_error(szBuf, true); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Found saved game version that matches your application version: [%s] --> [%s]\n", + gameVer.c_str(), glestVersionString.c_str()); + + XmlNode *gameNode = rootNode->getChild("Game"); + + GameSettings newGameSettingsReplay; + newGameSettingsReplay.loadGame(gameNode); + //printf("Loading scenario [%s]\n",newGameSettingsReplay.getScenarioDir().c_str()); + if (newGameSettingsReplay.getScenarioDir() != "" + && fileExists(newGameSettingsReplay.getScenarioDir()) == false) { + newGameSettingsReplay.setScenarioDir(Scenario::getScenarioPath + (Config:: + getInstance + ().getPathListForType + (ptScenarios), + newGameSettingsReplay.getScenario + ())); + + //printf("Loading scenario #2 [%s]\n",newGameSettingsReplay.getScenarioDir().c_str()); + } + + //GameSettings newGameSettings; + //newGameSettings.loadGame(gameNode); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Game settings loaded\n"); + + NetworkManager & networkManager = NetworkManager::getInstance(); + networkManager.end(); + networkManager.init(nrServer, true); + + Game *newGame = + new Game(programPtr, &newGameSettingsReplay, isMasterserverMode); + newGame->lastworldFrameCountForReplay = + gameNode->getAttribute("LastWorldFrameCount")->getIntValue(); + + vector < XmlNode * >networkCommandNodeList = + gameNode->getChildList("NetworkCommand"); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("networkCommandNodeList.size() = " MG_SIZE_T_SPECIFIER + "\n", networkCommandNodeList.size()); + for (unsigned int i = 0; i < networkCommandNodeList.size(); ++i) { + XmlNode *node = networkCommandNodeList[i]; + int + worldFrameCount = + node->getAttribute("worldFrameCount")->getIntValue(); + NetworkCommand command; + command.loadGame(node); + newGame->commander.addToReplayCommandList(command, + worldFrameCount); + } + + programPtr->setState(newGame); + return; + } + + XmlTree xmlTree(XML_RAPIDXML_ENGINE); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Before load of XML\n"); + std::map < string, string > mapExtraTagReplacementValues; + xmlTree.load(name, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues), true); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("After load of XML\n"); + + const XmlNode *rootNode = xmlTree.getRootNode(); + if (rootNode->hasChild("zetaglest-saved-game") == true) { + rootNode = rootNode->getChild("zetaglest-saved-game"); + } + + //const XmlNode *versionNode= rootNode->getChild("zetaglest-saved-game"); + const XmlNode *versionNode = rootNode; + + Lang & lang = Lang::getInstance(); + string gameVer = versionNode->getAttribute("version")->getValue(); + // this is the version check for loading normal save games from menu_state_load_game + if (gameVer != glestVersionString + && + (compareMajorMinorVersion + (gameVer, lastCompatibleSaveGameVersionString) < 0 + || compareMajorMinorVersion(glestVersionString, gameVer) < 0)) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("SavedGameBadVersion").c_str(), + gameVer.c_str(), glestVersionString.c_str()); + throw megaglest_runtime_error(szBuf, true); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Found saved game version that matches your application version: [%s] --> [%s]\n", + gameVer.c_str(), glestVersionString.c_str()); + + XmlNode *gameNode = rootNode->getChild("Game"); + GameSettings newGameSettings; + if (joinGameSettings != NULL) { + newGameSettings = *joinGameSettings; + + XmlNode *worldNode = gameNode->getChild("World"); + XmlNode *guiNode = gameNode->getChild("Gui"); + XmlNode *selectionNode = guiNode->getChild("Selection"); + XmlNode *statsNode = worldNode->getChild("Stats"); + XmlNode *minimapNode = worldNode->getChild("Minimap"); + + if (gameVer != glestVersionString + && checkVersionComptability(gameVer, + glestVersionString) == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("SavedGameBadVersion").c_str(), + gameVer.c_str(), glestVersionString.c_str()); + throw megaglest_runtime_error(szBuf, true); + } + // This is explored fog of war for the host player, clear it + minimapNode->clearChild("fowPixmap1"); + + NetworkManager & networkManager = NetworkManager::getInstance(); + //NetworkRole role = networkManager.getNetworkRole(); + ClientInterface *clientInterface = + dynamic_cast < + ClientInterface *>(networkManager.getClientInterface()); + + for (int i = 0; i < newGameSettings.getFactionCount(); ++i) { + //replace by network + if (newGameSettings.getFactionControl(i) == ctHuman) { + newGameSettings.setFactionControl(i, ctNetwork); + } + + //set the faction index + if (newGameSettings.getStartLocationIndex(i) == + clientInterface->getPlayerIndex()) { + newGameSettings.setThisFactionIndex(i); + newGameSettings.setFactionControl(i, ctNetwork); + + worldNode-> + getAttribute("thisFactionIndex")->setValue(intToStr(i)); + //worldNode->getAttribute("thisTeamIndex")->setValue(intToStr(newGameSettings.getTeam(i))); + + XmlNode *factionNode = worldNode->getChild("Faction", i); + factionNode-> + getAttribute("thisFaction")->setValue(intToStr(i)); + factionNode-> + getAttribute("control")->setValue(intToStr(ctNetwork)); + + selectionNode-> + getAttribute("factionIndex")->setValue(intToStr(i)); + //selectionNode->getAttribute("teamIndex")->setValue(intToStr(newGameSettings.getTeam(i))); + + statsNode-> + getAttribute("thisFactionIndex")->setValue(intToStr(i)); + } else { + XmlNode *factionNode = worldNode->getChild("Faction", i); + factionNode-> + getAttribute("thisFaction")->setValue(intToStr(0)); + } + } + } else { + newGameSettings.loadGame(gameNode); + //printf("Loading scenario [%s]\n",newGameSettings.getScenarioDir().c_str()); + if (newGameSettings.getScenarioDir() != "" + && fileExists(newGameSettings.getScenarioDir()) == false) { + newGameSettings.setScenarioDir(Scenario::getScenarioPath + (Config:: + getInstance().getPathListForType + (ptScenarios), + newGameSettings.getScenario())); + + //printf("Loading scenario #2 [%s]\n",newGameSettings.getScenarioDir().c_str()); + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Game settings loaded\n"); + + if (joinGameSettings == NULL) { + NetworkManager & networkManager = NetworkManager::getInstance(); + networkManager.end(); + networkManager.init(nrServer, true); + } + + Game *newGame = + new Game(programPtr, &newGameSettings, isMasterserverMode); + + newGame->loadGameNode = gameNode; + newGame->inJoinGameLoading = (joinGameSettings != NULL); + + // newGame->mouse2d = gameNode->getAttribute("mouse2d")->getIntValue(); + // int mouseX; + // newGame->mouseX = gameNode->getAttribute("mouseX")->getIntValue(); + // int mouseY; //coords win32Api + // newGame->mouseY = gameNode->getAttribute("mouseY")->getIntValue(); + // Vec2i mouseCellPos; + // newGame->mouseCellPos = Vec2i::strToVec2(gameNode->getAttribute("mouseCellPos")->getValue()); + // int updateFps, lastUpdateFps, avgUpdateFps; + // int totalRenderFps, renderFps, lastRenderFps, avgRenderFps,currentAvgRenderFpsTotal; + //Uint64 tickCount; + newGame->tickCount = + gameNode->getAttribute("tickCount")->getIntValue(); + + //bool paused; + if (newGame->inJoinGameLoading == true) { + newGame->paused = + gameNode->getAttribute("paused")->getIntValue() != 0; + } else { + //newGame->paused = gameNode->getAttribute("paused")->getIntValue() != 0; + newGame->paused = true; + } + if (newGame->paused) + newGame->console.addLine(lang.getString("GamePaused")); + //bool gameOver; + newGame->gameOver = + gameNode->getAttribute("gameOver")->getIntValue() != 0; + //bool renderNetworkStatus; + //bool showFullConsole; + //bool mouseMoved; + //float scrollSpeed; + // newGame->scrollSpeed = gameNode->getAttribute("scrollSpeed")->getFloatValue(); + //bool camLeftButtonDown; + //bool camRightButtonDown; + //bool camUpButtonDown; + //bool camDownButtonDown; + + //Speed speed; + //gameNode->addAttribute("speed",intToStr(speed), mapTagReplacements); + + //GraphicMessageBox mainMessageBox; + //GraphicMessageBox errorMessageBox; + + //misc ptr + //ParticleSystem *weatherParticleSystem; + // if(weatherParticleSystem != NULL) { + // weatherParticleSystem->saveGame(gameNode); + // } + //GameSettings gameSettings; + // gameSettings.saveGame(gameNode); + //Vec2i lastMousePos; + // gameNode->addAttribute("lastMousePos",lastMousePos.getString(), mapTagReplacements); + //time_t lastRenderLog2d; + // gameNode->addAttribute("lastRenderLog2d",intToStr(lastRenderLog2d), mapTagReplacements); + //DisplayMessageFunction originalDisplayMsgCallback; + //bool isFirstRender; + // gameNode->addAttribute("isFirstRender",intToStr(isFirstRender), mapTagReplacements); + + //bool quitTriggeredIndicator; + //int original_updateFps; + // gameNode->addAttribute("original_updateFps",intToStr(original_updateFps), mapTagReplacements); + //int original_cameraFps; + // gameNode->addAttribute("original_cameraFps",intToStr(original_cameraFps), mapTagReplacements); + + //bool captureAvgTestStatus; + // gameNode->addAttribute("captureAvgTestStatus",intToStr(captureAvgTestStatus), mapTagReplacements); + //int updateFpsAvgTest; + // gameNode->addAttribute("updateFpsAvgTest",intToStr(updateFpsAvgTest), mapTagReplacements); + //int renderFpsAvgTest; + // gameNode->addAttribute("renderFpsAvgTest",intToStr(renderFpsAvgTest), mapTagReplacements); + + //int renderExtraTeamColor; + newGame->renderExtraTeamColor = + gameNode->getAttribute("renderExtraTeamColor")->getIntValue(); + + //static const int renderTeamColorCircleBit=1; + //static const int renderTeamColorPlaneBit=2; + + //bool photoModeEnabled; + //gameNode->addAttribute("photoModeEnabled",intToStr(photoModeEnabled), mapTagReplacements); + newGame->photoModeEnabled = + gameNode->getAttribute("photoModeEnabled")->getIntValue() != 0; + //bool visibleHUD; + //gameNode->addAttribute("visibleHUD",intToStr(visibleHUD), mapTagReplacements); + newGame->visibleHUD = + gameNode->getAttribute("visibleHUD")->getIntValue() != 0; + newGame->timeDisplay = + gameNode->getAttribute("timeDisplay")->getIntValue() != 0; + //bool withRainEffect; + //gameNode->addAttribute("withRainEffect",intToStr(withRainEffect), mapTagReplacements); + newGame->withRainEffect = + gameNode->getAttribute("withRainEffect")->getIntValue() != 0; + //Program *program; + + if (joinGameSettings == NULL) { + if (gameNode->hasChild("unitHighlightList") == true) { + XmlNode *unitHighlightListNode = + gameNode->getChild("unitHighlightList"); + vector < XmlNode * >infoNodeList = + unitHighlightListNode->getChildList("info"); + for (unsigned int i = 0; i < infoNodeList.size(); ++i) { + XmlNode *infoNode = infoNodeList[i]; + + int unitId = infoNode->getAttribute("radius")->getIntValue(); + HighlightSpecialUnitInfo info; + info.radius = infoNode->getAttribute("radius")->getFloatValue(); + info.thickness = + infoNode->getAttribute("thickness")->getFloatValue(); + info.color = + Vec4f::strToVec4(infoNode-> + getAttribute("color")->getValue()); + + newGame->unitHighlightList[unitId] = info; + } + } + } + + newGame->timeDisplay = + gameNode->getAttribute("timeDisplay")->getIntValue() != 0; + + if (gameNode->hasAttribute("disableSpeedChange") == true) { + newGame->disableSpeedChange = + gameNode->getAttribute("disableSpeedChange")->getIntValue() != 0; + } + + //bool gameStarted; + //gameNode->addAttribute("gameStarted",intToStr(gameStarted), mapTagReplacements); + // newGame->gameStarted = gameNode->getAttribute("gameStarted")->getIntValue(); + + //time_t lastMaxUnitCalcTime; + //gameNode->addAttribute("lastMaxUnitCalcTime",intToStr(lastMaxUnitCalcTime), mapTagReplacements); + + //PopupMenu popupMenu; + //PopupMenu popupMenuSwitchTeams; + + //std::map switchTeamIndexMap; + //GraphicMessageBox switchTeamConfirmMessageBox; + + //int exitGamePopupMenuIndex; + //int joinTeamPopupMenuIndex; + //int pauseGamePopupMenuIndex; + //int keyboardSetupPopupMenuIndex; + //GLuint statelist3dMenu; + //ProgramState *currentUIState; + + //bool masterserverMode; + + //StrSound *currentAmbientSound; + + //time_t lastNetworkPlayerConnectionCheck; + //gameNode->addAttribute("lastNetworkPlayerConnectionCheck",intToStr(lastNetworkPlayerConnectionCheck), mapTagReplacements); + + //time_t lastMasterServerGameStatsDump; + //gameNode->addAttribute("lastMasterServerGameStatsDump",intToStr(lastMasterServerGameStatsDump), mapTagReplacements); + + if (joinGameSettings == NULL) { + newGame->gameCamera.loadGame(gameNode); + } + + const XmlNode *worldNode = gameNode->getChild("World"); + newGame->world.loadGame(worldNode); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Starting Game ...\n"); + programPtr->setState(newGame); + } + + } + } //end namespace diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index 0f37268b2..ad1e03a27 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -50,543 +50,506 @@ using std::vector; using namespace Shared::Platform; using namespace Shared::PlatformCommon; -namespace Shared -{ - namespace Graphics - { - class VideoPlayer; - } +namespace Shared { + namespace Graphics { + class VideoPlayer; + } }; -namespace Glest -{ - namespace Game - { - - class GraphicMessageBox; - class ServerInterface; - - enum LoadGameItem - { - lgt_FactionPreview = 0x01, - lgt_TileSet = 0x02, - lgt_TechTree = 0x04, - lgt_Map = 0x08, - lgt_Scenario = 0x10, - - lgt_All = - (lgt_FactionPreview | lgt_TileSet | lgt_TechTree | lgt_Map | - lgt_Scenario) - }; - -// ===================================================== -// class Game -// -// Main game class -// ===================================================== - class Game: - public - ProgramState, - public - FileCRCPreCacheThreadCallbackInterface, - public CustomInputCallbackInterface, public ClientLagCallbackInterface - { - public: - static const float highlightTime; - - private: - typedef vector < Ai * > Ais; - typedef vector < AiInterface * > AiInterfaces; - - private: - //main data - World world; - AiInterfaces aiInterfaces; - Gui gui; - GameCamera gameCamera; - Commander commander; - Console console; - ChatManager chatManager; - ScriptManager scriptManager; - - //misc - Checksum checksum; - string loadingText; - int mouse2d; - int mouseX; - int mouseY; //coords win32Api - Vec2i mouseCellPos; - - int updateFps, lastUpdateFps, avgUpdateFps; - int framesToCatchUpAsClient; - int framesToSlowDownAsClient; - int receivedTooEarlyInFrames[GameConstants::networkSmoothInterval]; - int - framesNeededToWaitForServerMessage[GameConstants:: - networkSmoothInterval]; - int - totalRenderFps, - renderFps, lastRenderFps, avgRenderFps, currentAvgRenderFpsTotal; - uint64 tickCount; - bool paused; - bool pauseRequestSent; - bool resumeRequestSent; - bool pauseStateChanged; - bool pausedForJoinGame; - bool pausedBeforeJoinGame; - - bool gameOver; - bool renderNetworkStatus; - bool renderInGamePerformance; - bool showFullConsole; - bool setMarker; - bool cameraDragAllowed; - bool mouseMoved; - float scrollSpeed; - bool camLeftButtonDown; - bool camRightButtonDown; - bool camUpButtonDown; - bool camDownButtonDown; - - int speed; - GraphicMessageBox mainMessageBox; - GraphicMessageBox errorMessageBox; - - //misc ptr - ParticleSystem *weatherParticleSystem; - GameSettings gameSettings; - Vec2i lastMousePos; - time_t lastRenderLog2d; - DisplayMessageFunction originalDisplayMsgCallback; - bool isFirstRender; - - bool quitTriggeredIndicator; - bool quitPendingIndicator; - - int original_updateFps; - int original_cameraFps; - - bool captureAvgTestStatus; - int updateFpsAvgTest; - int renderFpsAvgTest; - - int renderExtraTeamColor; - static const int renderTeamColorCircleBit = 1; - static const int renderTeamColorPlaneBit = 2; - - bool photoModeEnabled; - int healthbarMode; - bool visibleHUD; - bool timeDisplay; - bool withRainEffect; - Program *program; - - bool gameStarted; - - time_t lastMaxUnitCalcTime; - - PopupMenu popupMenu; - PopupMenu popupMenuSwitchTeams; - PopupMenu popupMenuDisconnectPlayer; - - std::map < int, int > switchTeamIndexMap; - GraphicMessageBox switchTeamConfirmMessageBox; - - std::map < int, int > disconnectPlayerIndexMap; - int playerIndexDisconnect; - GraphicMessageBox disconnectPlayerConfirmMessageBox; - - int exitGamePopupMenuIndex; - int joinTeamPopupMenuIndex; - int pauseGamePopupMenuIndex; - int saveGamePopupMenuIndex; - int loadGamePopupMenuIndex; - //int markCellPopupMenuIndex; - //int unmarkCellPopupMenuIndex; - int keyboardSetupPopupMenuIndex; - int disconnectPlayerPopupMenuIndex; - //GLuint statelist3dMenu; - ProgramState *currentUIState; - - bool isMarkCellEnabled; - Vec2i cellMarkedPos; - MarkedCell cellMarkedData; - bool isMarkCellTextEnabled; - - Texture2D *markCellTexture; - bool isUnMarkCellEnabled; - Texture2D *unmarkCellTexture; - std::map < Vec2i, MarkedCell > mapMarkedCellList; - Texture2D *highlightCellTexture; - std::vector < MarkedCell > highlightedCells; - bool masterserverMode; - - StrSound *currentAmbientSound; - - time_t lastNetworkPlayerConnectionCheck; - - time_t lastMasterServerGameStatsDump; - - XmlNode *loadGameNode; - int lastworldFrameCountForReplay; - std::vector < std::pair < int, NetworkCommand > > replayCommandList; - - std::vector < string > streamingVideos; - ::Shared::Graphics::VideoPlayer * videoPlayer; - bool playingStaticVideo; - - Unit *currentCameraFollowUnit; - - std::map < int, HighlightSpecialUnitInfo > unitHighlightList; - - MasterSlaveThreadController masterController; - - bool inJoinGameLoading; - bool initialResumeSpeedLoops; - - bool quitGameCalled; - bool disableSpeedChange; - - std::map < int, FowAlphaCellsLookupItem > teamFowAlphaCellsLookupItem; - std::map < string, int64 > gamePerformanceCounts; - - bool networkPauseGameForLaggedClientsRequested; - bool networkResumeGameForLaggedClientsRequested; - - public: - Game (); - Game (Program * program, const GameSettings * gameSettings, - bool masterserverMode); - ~Game (); - - void reInitGUI (); - bool isFlagType1BitEnabled (FlagTypes1 type) const; - - bool isMarkCellMode ()const - { - return isMarkCellEnabled; - } - const Texture2D *getMarkCellTexture () const - { - return markCellTexture; - } - bool isUnMarkCellMode () const - { - return isUnMarkCellEnabled; - } - const Texture2D *getUnMarkCellTexture () const - { - return unmarkCellTexture; - } - - std::map < Vec2i, MarkedCell > getMapMarkedCellList () const - { - return mapMarkedCellList; - } - - const Texture2D *getHighlightCellTexture () const - { - return highlightCellTexture; - } - const std::vector < MarkedCell > *getHighlightedCells () const - { - return &highlightedCells; - } - void addOrReplaceInHighlightedCells (MarkedCell mc); - - bool isMasterserverMode ()const - { - return masterserverMode; - } - //get - GameSettings *getGameSettings () - { - return &gameSettings; - } - void setGameSettings (GameSettings * settings) - { - gameSettings = *settings; - } - const GameSettings *getReadOnlyGameSettings () const - { - return &gameSettings; - } - void setQuitPendingIndicator () - { - quitPendingIndicator = true; - } - - const GameCamera *getGameCamera () const - { - return &gameCamera; - } - GameCamera *getGameCameraPtr () - { - return &gameCamera; - } - const Commander *getCommander () const - { - return &commander; - } - Gui *getGuiPtr () - { - return &gui; - } - const Gui *getGui () const - { - return &gui; - } - Commander *getCommander () - { - return &commander; - } - Console *getConsole () - { - return &console; - } - ScriptManager *getScriptManager () - { - return &scriptManager; - } - World *getWorld () - { - return &world; - } - const World *getWorld () const - { - return &world; - } - - Program *getProgram () - { - return program; - } - - Vec2i getMouseCellPos ()const - { - return mouseCellPos; - } - bool isValidMouseCellPos () const; - - void removeUnitFromSelection (const Unit * unit); - bool addUnitToSelection (Unit * unit); - void addUnitToGroupSelection (Unit * unit, int groupIndex); - void removeUnitFromGroupSelection (int unitId, int groupIndex); - void recallGroupSelection (int groupIndex); - - Uint64 getTickCount () - { - return tickCount; - } - bool getPaused (); - void - setPaused (bool value, bool forceAllowPauseStateChange, - bool clearCaches, bool joinNetworkGame); - void tryPauseToggle (bool pause); - void setupRenderForVideo (); - void saveGame (); - const int getTotalRenderFps () const - { - return totalRenderFps; - } - - void toggleTeamColorMarker (); - //init - void resetMembers (); - virtual void load (int loadTypes); - virtual void load (); - virtual void init (); - virtual void init (bool initForPreviewOnly); - virtual void update (); - virtual void updateCamera (); - virtual void render (); - virtual void tick (); - - //event managing - virtual bool textInput (std::string text); - virtual bool sdlKeyDown (SDL_KeyboardEvent key); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyUp (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - virtual void mouseDownLeft (int x, int y); - virtual void mouseDownRight (int x, int y); - virtual void mouseUpCenter (int x, int y); - virtual void mouseUpLeft (int x, int y); - virtual void mouseDoubleClickLeft (int x, int y); - virtual void eventMouseWheel (int x, int y, int zDelta); - virtual void mouseMove (int x, int y, const MouseState * mouseState); - - virtual bool isInSpecialKeyCaptureEvent () - { - return chatManager.getEditEnabled (); - } - - virtual bool quitTriggered (); - virtual Stats quitAndToggleState (); - Stats quitGame (); - static void exitGameState (Program * program, Stats & endStats); - - void startPerformanceTimer (); - void endPerformanceTimer (); - Vec2i getPerformanceTimerResults (); - - //static Texture2D * findFactionLogoTexture(const GameSettings *settings, Logger *logger=NULL,string factionLogoFilter=GameConstants::LOADING_SCREEN_FILE_FILTER, bool useTechDefaultIfFilterNotFound=true); - static string - findFactionLogoFile (const GameSettings * settings, Logger * logger = - NULL, const string & factionLogoFilter = - GameConstants::LOADING_SCREEN_FILE_FILTER); - static string - extractScenarioLogoFile (const GameSettings * settings, - string & result, bool & loadingImageUsed, - Logger * logger = - NULL, string factionLogoFilter = - GameConstants::LOADING_SCREEN_FILE_FILTER); - static string extractFactionLogoFile (bool & loadingImageUsed, - const string & factionName, - string scenarioDir, - const string & techName, - Logger * logger = - NULL, string factionLogoFilter = - GameConstants:: - LOADING_SCREEN_FILE_FILTER); - static string extractTechLogoFile (string scenarioDir, - const string & techName, - bool & loadingImageUsed, - Logger * logger = - NULL, - const string & factionLogoFilter = - GameConstants:: - LOADING_SCREEN_FILE_FILTER); - - void loadHudTexture (const GameSettings * settings); - - bool getGameOver () - { - return gameOver; - } - bool hasGameStarted () - { - return gameStarted; - } - virtual vector < Texture2D * >processTech (string techName); - virtual void consoleAddLine (string line); - - void endGame (); - - void playStaticVideo (const string & playVideo); - void playStreamingVideo (const string & playVideo); - void stopStreamingVideo (const string & playVideo); - void stopAllVideo (); - - string saveGame (string name, const string & path = "saved/"); - static void - loadGame (string name, Program * programPtr, bool isMasterserverMode, - const GameSettings * joinGameSettings = NULL); - - void - addNetworkCommandToReplayList (NetworkCommand * networkCommand, - int worldFrameCount); - - bool factionLostGame (int factionIndex); - - void addCellMarker (Vec2i cellPos, MarkedCell cellData); - void removeCellMarker (Vec2i surfaceCellPos, const Faction * faction); - void showMarker (Vec2i cellPos, MarkedCell cellData); - - void - highlightUnit (int unitId, float radius, float thickness, - Vec4f color); - void unhighlightUnit (int unitId); - - bool showTranslatedTechTree ()const; - - void DumpCRCWorldLogIfRequired (string fileSuffix = ""); - - bool getDisableSpeedChange ()const - { - return disableSpeedChange; - } - void setDisableSpeedChange (bool value) - { - disableSpeedChange = value; - } - - string getGamePerformanceCounts (bool displayWarnings) const; - virtual void addPerformanceCount (string key, int64 value); - bool getRenderInGamePerformance ()const - { - return renderInGamePerformance; - } - - private: - //render - void render3d (); - void render2d (); - - //misc - void checkWinner (); - void checkWinnerStandard (); - void checkWinnerScripted (); - void setEndGameTeamWinnersAndLosers (); - - //bool hasBuilding(const Faction *faction); - bool factionLostGame (const Faction * faction); - void incSpeed (); - void decSpeed (); - int getUpdateLoops (); - - void showLoseMessageBox (); - void showWinMessageBox (); - void - showMessageBox (const string & text, const string & header, - bool toggle); - void - showErrorMessageBox (const string & text, const string & header, - bool toggle); - - void renderWorker (); - static int ErrorDisplayMessage (const char *msg, bool exitApp); - - void - ReplaceDisconnectedNetworkPlayersWithAI (bool isNetworkGame, - NetworkRole role); - void calcCameraMoveX (); - void calcCameraMoveZ (); - - int getFirstUnusedTeamNumber (); - void updateWorldStats (); - - void setupPopupMenus (bool checkClientAdminOverrideOnly); - - string getDebugStats (std::map < int, string > &factionDebugInfo); - - void renderVideoPlayer (); - - void updateNetworkMarkedCells (); - void updateNetworkUnMarkedCells (); - void updateNetworkHighligtedCells (); - - virtual void processInputText (string text, bool cancelled); - - void startMarkCell (); - void startCameraFollowUnit (); - - bool - switchSetupForSlots (ServerInterface * &serverInterface, - int startIndex, int endIndex, - bool onlyNetworkUnassigned); - void processNetworkSynchChecksIfRequired (); - Stats getEndGameStats (); - void checkWinnerStandardHeadlessOrObserver (); - void checkWinnerStandardPlayer (); - std::map < int, int > getTeamsAlive (); - void initCamera (Map * map); - - virtual bool - clientLagHandler (int slotIndex, - bool networkPauseGameForLaggedClients); - }; - -}} //end namespace +namespace Glest { + namespace Game { + + class GraphicMessageBox; + class ServerInterface; + + enum LoadGameItem { + lgt_FactionPreview = 0x01, + lgt_TileSet = 0x02, + lgt_TechTree = 0x04, + lgt_Map = 0x08, + lgt_Scenario = 0x10, + + lgt_All = + (lgt_FactionPreview | lgt_TileSet | lgt_TechTree | lgt_Map | + lgt_Scenario) + }; + + // ===================================================== + // class Game + // + // Main game class + // ===================================================== + class Game : + public + ProgramState, + public + FileCRCPreCacheThreadCallbackInterface, + public CustomInputCallbackInterface, public ClientLagCallbackInterface { + public: + static const float highlightTime; + + private: + typedef vector < Ai * > Ais; + typedef vector < AiInterface * > AiInterfaces; + + private: + //main data + World world; + AiInterfaces aiInterfaces; + Gui gui; + GameCamera gameCamera; + Commander commander; + Console console; + ChatManager chatManager; + ScriptManager scriptManager; + + //misc + Checksum checksum; + string loadingText; + int mouse2d; + int mouseX; + int mouseY; //coords win32Api + Vec2i mouseCellPos; + + int updateFps, lastUpdateFps, avgUpdateFps; + int framesToCatchUpAsClient; + int framesToSlowDownAsClient; + int receivedTooEarlyInFrames[GameConstants::networkSmoothInterval]; + int + framesNeededToWaitForServerMessage[GameConstants:: + networkSmoothInterval]; + int + totalRenderFps, + renderFps, lastRenderFps, avgRenderFps, currentAvgRenderFpsTotal; + uint64 tickCount; + bool paused; + bool pauseRequestSent; + bool resumeRequestSent; + bool pauseStateChanged; + bool pausedForJoinGame; + bool pausedBeforeJoinGame; + + bool gameOver; + bool renderNetworkStatus; + bool renderInGamePerformance; + bool showFullConsole; + bool setMarker; + bool cameraDragAllowed; + bool mouseMoved; + float scrollSpeed; + bool camLeftButtonDown; + bool camRightButtonDown; + bool camUpButtonDown; + bool camDownButtonDown; + + int speed; + GraphicMessageBox mainMessageBox; + GraphicMessageBox errorMessageBox; + + //misc ptr + ParticleSystem *weatherParticleSystem; + GameSettings gameSettings; + Vec2i lastMousePos; + time_t lastRenderLog2d; + DisplayMessageFunction originalDisplayMsgCallback; + bool isFirstRender; + + bool quitTriggeredIndicator; + bool quitPendingIndicator; + + int original_updateFps; + int original_cameraFps; + + bool captureAvgTestStatus; + int updateFpsAvgTest; + int renderFpsAvgTest; + + int renderExtraTeamColor; + static const int renderTeamColorCircleBit = 1; + static const int renderTeamColorPlaneBit = 2; + + bool photoModeEnabled; + int healthbarMode; + bool visibleHUD; + bool timeDisplay; + bool withRainEffect; + Program *program; + + bool gameStarted; + + time_t lastMaxUnitCalcTime; + + PopupMenu popupMenu; + PopupMenu popupMenuSwitchTeams; + PopupMenu popupMenuDisconnectPlayer; + + std::map < int, int > switchTeamIndexMap; + GraphicMessageBox switchTeamConfirmMessageBox; + + std::map < int, int > disconnectPlayerIndexMap; + int playerIndexDisconnect; + GraphicMessageBox disconnectPlayerConfirmMessageBox; + + int exitGamePopupMenuIndex; + int joinTeamPopupMenuIndex; + int pauseGamePopupMenuIndex; + int saveGamePopupMenuIndex; + int loadGamePopupMenuIndex; + //int markCellPopupMenuIndex; + //int unmarkCellPopupMenuIndex; + int keyboardSetupPopupMenuIndex; + int disconnectPlayerPopupMenuIndex; + //GLuint statelist3dMenu; + ProgramState *currentUIState; + + bool isMarkCellEnabled; + Vec2i cellMarkedPos; + MarkedCell cellMarkedData; + bool isMarkCellTextEnabled; + + Texture2D *markCellTexture; + bool isUnMarkCellEnabled; + Texture2D *unmarkCellTexture; + std::map < Vec2i, MarkedCell > mapMarkedCellList; + Texture2D *highlightCellTexture; + std::vector < MarkedCell > highlightedCells; + bool masterserverMode; + + StrSound *currentAmbientSound; + + time_t lastNetworkPlayerConnectionCheck; + + time_t lastMasterServerGameStatsDump; + + XmlNode *loadGameNode; + int lastworldFrameCountForReplay; + std::vector < std::pair < int, NetworkCommand > > replayCommandList; + + std::vector < string > streamingVideos; + ::Shared::Graphics::VideoPlayer * videoPlayer; + bool playingStaticVideo; + + Unit *currentCameraFollowUnit; + + std::map < int, HighlightSpecialUnitInfo > unitHighlightList; + + MasterSlaveThreadController masterController; + + bool inJoinGameLoading; + bool initialResumeSpeedLoops; + + bool quitGameCalled; + bool disableSpeedChange; + + std::map < int, FowAlphaCellsLookupItem > teamFowAlphaCellsLookupItem; + std::map < string, int64 > gamePerformanceCounts; + + bool networkPauseGameForLaggedClientsRequested; + bool networkResumeGameForLaggedClientsRequested; + + public: + Game(); + Game(Program * program, const GameSettings * gameSettings, + bool masterserverMode); + ~Game(); + + void reInitGUI(); + bool isFlagType1BitEnabled(FlagTypes1 type) const; + + bool isMarkCellMode()const { + return isMarkCellEnabled; + } + const Texture2D *getMarkCellTexture() const { + return markCellTexture; + } + bool isUnMarkCellMode() const { + return isUnMarkCellEnabled; + } + const Texture2D *getUnMarkCellTexture() const { + return unmarkCellTexture; + } + + std::map < Vec2i, MarkedCell > getMapMarkedCellList() const { + return mapMarkedCellList; + } + + const Texture2D *getHighlightCellTexture() const { + return highlightCellTexture; + } + const std::vector < MarkedCell > *getHighlightedCells() const { + return &highlightedCells; + } + void addOrReplaceInHighlightedCells(MarkedCell mc); + + bool isMasterserverMode()const { + return masterserverMode; + } + //get + GameSettings *getGameSettings() { + return &gameSettings; + } + void setGameSettings(GameSettings * settings) { + gameSettings = *settings; + } + const GameSettings *getReadOnlyGameSettings() const { + return &gameSettings; + } + void setQuitPendingIndicator() { + quitPendingIndicator = true; + } + + const GameCamera *getGameCamera() const { + return &gameCamera; + } + GameCamera *getGameCameraPtr() { + return &gameCamera; + } + const Commander *getCommander() const { + return &commander; + } + Gui *getGuiPtr() { + return &gui; + } + const Gui *getGui() const { + return &gui; + } + Commander *getCommander() { + return &commander; + } + Console *getConsole() { + return &console; + } + ScriptManager *getScriptManager() { + return &scriptManager; + } + World *getWorld() { + return &world; + } + const World *getWorld() const { + return &world; + } + + Program *getProgram() { + return program; + } + + Vec2i getMouseCellPos()const { + return mouseCellPos; + } + bool isValidMouseCellPos() const; + + void removeUnitFromSelection(const Unit * unit); + bool addUnitToSelection(Unit * unit); + void addUnitToGroupSelection(Unit * unit, int groupIndex); + void removeUnitFromGroupSelection(int unitId, int groupIndex); + void recallGroupSelection(int groupIndex); + + Uint64 getTickCount() { + return tickCount; + } + bool getPaused(); + void + setPaused(bool value, bool forceAllowPauseStateChange, + bool clearCaches, bool joinNetworkGame); + void tryPauseToggle(bool pause); + void setupRenderForVideo(); + void saveGame(); + const int getTotalRenderFps() const { + return totalRenderFps; + } + + void toggleTeamColorMarker(); + //init + void resetMembers(); + virtual void load(int loadTypes); + virtual void load(); + virtual void init(); + virtual void init(bool initForPreviewOnly); + virtual void update(); + virtual void updateCamera(); + virtual void render(); + virtual void tick(); + + //event managing + virtual bool textInput(std::string text); + virtual bool sdlKeyDown(SDL_KeyboardEvent key); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyUp(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + virtual void mouseDownLeft(int x, int y); + virtual void mouseDownRight(int x, int y); + virtual void mouseUpCenter(int x, int y); + virtual void mouseUpLeft(int x, int y); + virtual void mouseDoubleClickLeft(int x, int y); + virtual void eventMouseWheel(int x, int y, int zDelta); + virtual void mouseMove(int x, int y, const MouseState * mouseState); + + virtual bool isInSpecialKeyCaptureEvent() { + return chatManager.getEditEnabled(); + } + + virtual bool quitTriggered(); + virtual Stats quitAndToggleState(); + Stats quitGame(); + static void exitGameState(Program * program, Stats & endStats); + + void startPerformanceTimer(); + void endPerformanceTimer(); + Vec2i getPerformanceTimerResults(); + + //static Texture2D * findFactionLogoTexture(const GameSettings *settings, Logger *logger=NULL,string factionLogoFilter=GameConstants::LOADING_SCREEN_FILE_FILTER, bool useTechDefaultIfFilterNotFound=true); + static string + findFactionLogoFile(const GameSettings * settings, Logger * logger = + NULL, const string & factionLogoFilter = + GameConstants::LOADING_SCREEN_FILE_FILTER); + static string + extractScenarioLogoFile(const GameSettings * settings, + string & result, bool & loadingImageUsed, + Logger * logger = + NULL, string factionLogoFilter = + GameConstants::LOADING_SCREEN_FILE_FILTER); + static string extractFactionLogoFile(bool & loadingImageUsed, + const string & factionName, + string scenarioDir, + const string & techName, + Logger * logger = + NULL, string factionLogoFilter = + GameConstants:: + LOADING_SCREEN_FILE_FILTER); + static string extractTechLogoFile(string scenarioDir, + const string & techName, + bool & loadingImageUsed, + Logger * logger = + NULL, + const string & factionLogoFilter = + GameConstants:: + LOADING_SCREEN_FILE_FILTER); + + void loadHudTexture(const GameSettings * settings); + + bool getGameOver() { + return gameOver; + } + bool hasGameStarted() { + return gameStarted; + } + virtual vector < Texture2D * >processTech(string techName); + virtual void consoleAddLine(string line); + + void endGame(); + + void playStaticVideo(const string & playVideo); + void playStreamingVideo(const string & playVideo); + void stopStreamingVideo(const string & playVideo); + void stopAllVideo(); + + string saveGame(string name, const string & path = "saved/"); + static void + loadGame(string name, Program * programPtr, bool isMasterserverMode, + const GameSettings * joinGameSettings = NULL); + + void + addNetworkCommandToReplayList(NetworkCommand * networkCommand, + int worldFrameCount); + + bool factionLostGame(int factionIndex); + + void addCellMarker(Vec2i cellPos, MarkedCell cellData); + void removeCellMarker(Vec2i surfaceCellPos, const Faction * faction); + void showMarker(Vec2i cellPos, MarkedCell cellData); + + void + highlightUnit(int unitId, float radius, float thickness, + Vec4f color); + void unhighlightUnit(int unitId); + + bool showTranslatedTechTree()const; + + void DumpCRCWorldLogIfRequired(string fileSuffix = ""); + + bool getDisableSpeedChange()const { + return disableSpeedChange; + } + void setDisableSpeedChange(bool value) { + disableSpeedChange = value; + } + + string getGamePerformanceCounts(bool displayWarnings) const; + virtual void addPerformanceCount(string key, int64 value); + bool getRenderInGamePerformance()const { + return renderInGamePerformance; + } + + private: + //render + void render3d(); + void render2d(); + + //misc + void checkWinner(); + void checkWinnerStandard(); + void checkWinnerScripted(); + void setEndGameTeamWinnersAndLosers(); + + //bool hasBuilding(const Faction *faction); + bool factionLostGame(const Faction * faction); + void incSpeed(); + void decSpeed(); + int getUpdateLoops(); + + void showLoseMessageBox(); + void showWinMessageBox(); + void + showMessageBox(const string & text, const string & header, + bool toggle); + void + showErrorMessageBox(const string & text, const string & header, + bool toggle); + + void renderWorker(); + static int ErrorDisplayMessage(const char *msg, bool exitApp); + + void + ReplaceDisconnectedNetworkPlayersWithAI(bool isNetworkGame, + NetworkRole role); + void calcCameraMoveX(); + void calcCameraMoveZ(); + + int getFirstUnusedTeamNumber(); + void updateWorldStats(); + + void setupPopupMenus(bool checkClientAdminOverrideOnly); + + string getDebugStats(std::map < int, string > &factionDebugInfo); + + void renderVideoPlayer(); + + void updateNetworkMarkedCells(); + void updateNetworkUnMarkedCells(); + void updateNetworkHighligtedCells(); + + virtual void processInputText(string text, bool cancelled); + + void startMarkCell(); + void startCameraFollowUnit(); + + bool + switchSetupForSlots(ServerInterface * &serverInterface, + int startIndex, int endIndex, + bool onlyNetworkUnassigned); + void processNetworkSynchChecksIfRequired(); + Stats getEndGameStats(); + void checkWinnerStandardHeadlessOrObserver(); + void checkWinnerStandardPlayer(); + std::map < int, int > getTeamsAlive(); + void initCamera(Map * map); + + virtual bool + clientLagHandler(int slotIndex, + bool networkPauseGameForLaggedClients); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/game/game_camera.cpp b/source/glest_game/game/game_camera.cpp index d84c58f18..da23e1e02 100644 --- a/source/glest_game/game/game_camera.cpp +++ b/source/glest_game/game/game_camera.cpp @@ -23,821 +23,733 @@ using namespace - Shared::Graphics; +Shared::Graphics; using - Shared::Xml::XmlNode; +Shared::Xml::XmlNode; using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class GameCamera -// ===================================================== - -//static std::map > > cacheVisibleQuad; - -// ================== PUBLIC ===================== - - const float - GameCamera::startingVAng = -60.f; - const float - GameCamera::startingHAng = 0.f; - const float - GameCamera::vTransitionMult = 0.125f; - const float - GameCamera::hTransitionMult = 0.125f; - const float - GameCamera::defaultHeight = 20.f; - const float - GameCamera::centerOffsetZ = 8.0f; - const float - GameCamera::shakeDist = 50.f; - -// ================= Constructor ================= - - GameCamera::GameCamera (): - pos (0.f, defaultHeight, 0.f), - destPos (0.f, defaultHeight, 0.f), - destAng (startingVAng, startingHAng) - { - //Config &config = Config::getInstance(); - calculatedDefault = defaultHeight; - state = sGame; - - cacheVisibleQuad. - clear (); - //MaxVisibleQuadItemCache = config.getInt("MaxVisibleQuadItemCache",intToStr(-1).c_str()); - MaxVisibleQuadItemCache = -1; - //if(Config::getInstance().getBool("DisableCaching","false") == true) { - // MaxVisibleQuadItemCache = 0; - //} - - //config - speed = - Config::getInstance ().getFloat ("CameraMoveSpeed", - "15") / GameConstants::cameraFps; - clampBounds = !Config::getInstance ().getBool ("PhotoMode"); - clampDisable = false; - - vAng = startingVAng; - hAng = startingHAng; - - rotate = 0; - - move = Vec3f (0.f); - - shakeDecrement = 0.f; - currentShakeIntensity = 0; - shakeOffset = Vec2f (0.f); - - //maxRenderDistance = Config::getInstance().getFloat("RenderDistanceMax","64"); - maxHeight = Config::getInstance ().getFloat ("CameraMaxDistance", "20"); - minHeight = Config::getInstance ().getFloat ("CameraMinDistance", "7"); - //maxCameraDist = maxHeight; - //minCameraDist = minHeight; - minVAng = -Config::getInstance ().getFloat ("CameraMaxYaw", "77.5"); - maxVAng = -Config::getInstance ().getFloat ("CameraMinYaw", "20"); - fov = Config::getInstance ().getFloat ("CameraFov", "45"); - - lastHAng = 0; - lastVAng = 0; - limitX = 0; - limitY = 0; - } - - GameCamera::~ - GameCamera () - { - cacheVisibleQuad.clear (); - } - - std::string - GameCamera::getCameraMovementKey () const - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s_%f_%f_%f_%s,%f", pos.getString ().c_str (), - hAng, vAng, rotate, move.getString ().c_str (), fov); - return - szBuf; - } - - void - GameCamera::setMaxHeight (float value) - { - if (value < 0) - { - maxHeight = - Config::getInstance ().getFloat ("CameraMaxDistance", "20"); - } - else - { - maxHeight = value; - } - } - - void - GameCamera::setCalculatedDefault (float calculatedDefault) - { - this->calculatedDefault = calculatedDefault; - if (maxHeight > 0 && maxHeight < calculatedDefault) - { - setMaxHeight (calculatedDefault); - } - resetPosition (); - } - - void - GameCamera::init (int limitX, int limitY) - { - this->limitX = limitX; - this->limitY = limitY; - } - -// ==================== Misc ===================== - - void - GameCamera::setPos (Vec2f pos) - { - this->pos = Vec3f (pos.x, this->pos.y, pos.y); - clampPosXZ (0.0f, (float) limitX, 0.0f, (float) limitY); - destPos.x = pos.x; - destPos.z = pos.y; - } - - void - GameCamera::setPos (Vec3f pos) - { - this->pos = pos; - //clampPosXZ(0.0f, (float)limitX, 0.0f, (float)limitY); - destPos.x = pos.x; - destPos.y = pos.y; - destPos.z = pos.z; - } - - void - GameCamera::shake (int shakeDuration, int shakeStartIntensity, - bool cameraDistanceAffected, Vec3f unitVector) - { - float - currentDurationLeft = 0; - float - incomingShakeIntensity = ((float) shakeStartIntensity) / 1000; - - // calculate the shake duration which is left - if (this->currentShakeIntensity > 0.f && this->shakeDecrement != 0.f) - { - currentDurationLeft = - this->currentShakeIntensity / this->shakeDecrement; - } - - // reduce new shake effect camera distance related - if (cameraDistanceAffected) - { - incomingShakeIntensity = - incomingShakeIntensity * (1.f - - unitVector.dist (getPos ()) / - GameCamera::shakeDist); - } - - // add camera shake effect to current one ( if exsists ). - if (this->currentShakeIntensity > 0) - { - this->currentShakeIntensity = - this->currentShakeIntensity + incomingShakeIntensity; - } - else - { - this->currentShakeIntensity = incomingShakeIntensity; - } - - // use bigger shakeDuration to calculate new shakeDecrement - if (currentDurationLeft < shakeDuration) - { - this->shakeDecrement = - currentShakeIntensity / ((float) shakeDuration); - } - else if (currentDurationLeft != 0.0f) - { - this->shakeDecrement = - currentShakeIntensity / ((float) currentDurationLeft); - } - } - - void - GameCamera::shakeCamera () - { - //RandomGen random; - if (currentShakeIntensity > 0.f) - { -// pos.x += (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; -// pos.z += (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; - - shakeOffset.x = - (((float) (rand () % 50)) / 50.f - 0.5f) * currentShakeIntensity; - shakeOffset.y = - (((float) (rand () % 50)) / 50.f - 0.5f) * currentShakeIntensity; - currentShakeIntensity -= shakeDecrement; - } - } - - void - GameCamera::update () - { - //move XZ - if (move.z) - { - moveForwardH (speed * move.z, 0.9f); - } - if (move.x) - { - moveSideH (speed * move.x, 0.9f); - } - - //free state - if (state == sFree) - { - if (std::fabs (rotate) == 1) - { - rotateHV (speed * 5 * rotate, 0); - } - if (move.y > 0) - { - moveUp (speed * move.y); - if (clampDisable == false && clampBounds && pos.y < maxHeight) - { - rotateHV (0.f, -speed * 1.7f * move.y); - } - } - if (move.y < 0) - { - moveUp (speed * move.y); - if (clampDisable == false && clampBounds && pos.y > minHeight) - { - rotateHV (0.f, -speed * 1.7f * move.y); - } - } - } - - //game state - if (abs (destAng.x - vAng) > 0.01f) - { - vAng += (destAng.x - vAng) * hTransitionMult; - } - if (abs (destAng.y - hAng) > 0.01f) - { - if (abs (destAng.y - hAng) > 180) - { - if (destAng.y > hAng) - { - hAng += (destAng.y - hAng - 360) * vTransitionMult; - } - else - { - hAng += (destAng.y - hAng + 360) * vTransitionMult; - } - } - else - { - hAng += (destAng.y - hAng) * vTransitionMult; - } - } - if (abs (destPos.x - pos.x) > 0.01f) - { - pos.x += (destPos.x - pos.x) / 32.0f; - } - if (abs (destPos.y - pos.y) > 0.01f) - { - pos.y += (destPos.y - pos.y) / 32.0f; - } - if (abs (destPos.z - pos.z) > 0.01f) - { - pos.z += (destPos.z - pos.z) / 32.0f; - } - clampAng (); - shakeCamera (); - - if (clampDisable == false && clampBounds) - { - clampPosXYZ (0.0f, (float) limitX, minHeight, maxHeight, 0.0f, - (float) limitY); - } - } - - Quad2i - GameCamera::computeVisibleQuad () - { - //printf("\n@@@ hAng [%f] vAng [%f] fov [%f]\n",hAng,vAng,fov); - - if (MaxVisibleQuadItemCache != 0) - { - std::map < float, - std::map < float, - std::map < - Vec3f, - Quad2i > > >::const_iterator - iterFind = cacheVisibleQuad.find (fov); - if (iterFind != cacheVisibleQuad.end ()) - { - std::map < float, - std::map < - Vec3f, - Quad2i > >::const_iterator - iterFind2 = iterFind->second.find (hAng); - if (iterFind2 != iterFind->second.end ()) - { - std::map < Vec3f, Quad2i >::const_iterator iterFind3 = - iterFind2->second.find (pos); - if (iterFind3 != iterFind2->second.end ()) - { - return iterFind3->second; - } - } - } - } - - float - nearDist = 15.f; - float - dist = pos.y > nearDist ? pos.y * 1.2f : nearDist; - float - farDist = 90.f * (pos.y > nearDist ? pos.y / 15.f : 1.f); - const float - viewDegree = 180.f; - - Vec2f - v (std::sin (degToRad (viewDegree - hAng)), - std::cos (degToRad (viewDegree - hAng))); - Vec2f - v1 (std::sin (degToRad (viewDegree - hAng - fov)), - std::cos (degToRad (viewDegree - hAng - fov))); - Vec2f - v2 (std::sin (degToRad (viewDegree - hAng + fov)), - std::cos (degToRad (viewDegree - hAng + fov))); - - v.normalize (); - v1.normalize (); - v2.normalize (); - - Vec2f - p = Vec2f (pos.x, pos.z) - v * dist; - Vec2i - p1 (static_cast < int >(p.x + v1.x * nearDist), - static_cast < int >(p.y + v1.y * nearDist)); - Vec2i - p2 (static_cast < int >(p.x + v1.x * farDist), - static_cast < int >(p.y + v1.y * farDist)); - Vec2i - p3 (static_cast < int >(p.x + v2.x * nearDist), - static_cast < int >(p.y + v2.y * nearDist)); - Vec2i - p4 (static_cast < int >(p.x + v2.x * farDist), - static_cast < int >(p.y + v2.y * farDist)); - - const bool - debug = false; - - Quad2i - result; - if (hAng >= 135 && hAng <= 225) - { - if (debug) - printf ("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); - - result = Quad2i (p1, p2, p3, p4); - if (MaxVisibleQuadItemCache != 0 && - (MaxVisibleQuadItemCache < 0 - || (int) cacheVisibleQuad[fov][hAng].size () <= - MaxVisibleQuadItemCache)) - { - cacheVisibleQuad[fov][hAng][pos] = result; - } - } - else if (hAng >= 45 && hAng <= 135) - { - if (debug) - printf ("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); - - result = Quad2i (p3, p1, p4, p2); - if (MaxVisibleQuadItemCache != 0 && - (MaxVisibleQuadItemCache < 0 - || (int) cacheVisibleQuad[fov][hAng].size () <= - MaxVisibleQuadItemCache)) - { - cacheVisibleQuad[fov][hAng][pos] = result; - } - } - else if (hAng >= 225 && hAng <= 315) - { - if (debug) - printf ("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); - - result = Quad2i (p2, p4, p1, p3); - if (MaxVisibleQuadItemCache != 0 && - (MaxVisibleQuadItemCache < 0 - || (int) cacheVisibleQuad[fov][hAng].size () <= - MaxVisibleQuadItemCache)) - { - cacheVisibleQuad[fov][hAng][pos] = result; - } - } - else - { - if (debug) - printf ("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); - - result = Quad2i (p4, p3, p2, p1); - if (MaxVisibleQuadItemCache != 0 && - (MaxVisibleQuadItemCache < 0 - || (int) cacheVisibleQuad[fov][hAng].size () <= - MaxVisibleQuadItemCache)) - { - cacheVisibleQuad[fov][hAng][pos] = Quad2i (p4, p3, p2, p1); - } - } - - return result; - } - - void - GameCamera::setState (State s) - { - if (s == sGame) - { - state = sGame; - setClampDisabled (false); - resetPosition (); - } - else if (s == sUnit) - { - state = sUnit; - setClampDisabled (true); - } - else if (s == sFree) - { - state = sFree; - setClampDisabled (false); - resetPosition (); - } - else - { - abort (); //"unknown camera state" - } - } - - void - GameCamera::resetPosition () - { - destAng.x = startingVAng; - destAng.y = startingHAng; - destPos.y = calculatedDefault; - } - - void - GameCamera::centerXZ (float x, float z) - { - destPos.x = pos.x = x; - destPos.z = pos.z = z + centerOffsetZ; - } - -//void GameCamera::transitionXYZ(float x, float y, float z) { -// destPos.x += x; -// destPos.y += y; -// destPos.z += z; -// clampPosXYZ(0.0f, (float)limitX, minHeight, maxHeight, 0.0f, (float)limitY); -//} - - void - GameCamera::transitionVH (float v, float h) - { - destAng.x -= v; - //destPos.y -= v * destPos.y / 100.f; - destAng.y -= h; - clampAng (); - } - - void - GameCamera::rotateToVH (float v, float h) - { - destAng.x = v; - destAng.y = h; - clampAng (); - } - - void - GameCamera::zoom (float dist) - { - float - flatDist = dist * std::cos (degToRad (vAng)); - Vec3f - offset (flatDist * std::sin (degToRad (hAng)), - dist * std::sin (degToRad (vAng)), - flatDist * -std::cos (degToRad (hAng))); - - destPos += offset; - } - - void - GameCamera::load (const XmlNode * node) - { - //destPos = node->getChildVec3fValue("pos"); - //destAng = node->getChildVec2fValue("angle"); - } - - void - GameCamera::save (XmlNode * node) const - { - //node->addChild("pos", pos); - //node->addChild("angle", Vec2f(vAng, hAng)); - } - -// ==================== PRIVATE ==================== - - void - GameCamera::clampPosXZ (float x1, float x2, float z1, float z2) - { - if (clampDisable == true) - { - return; - } - - if (pos.x < x1) - pos.x = x1; - if (destPos.x < x1) - destPos.x = x1; - if (pos.z < z1) - pos.z = z1; - if (destPos.z < z1) - destPos.z = z1; - if (pos.x > x2) - pos.x = x2; - if (destPos.x > x2) - destPos.x = x2; - if (pos.z > z2) - pos.z = z2; - if (destPos.z > z2) - destPos.z = z2; - } - - void - GameCamera::clampPosXYZ (float x1, float x2, float y1, float y2, float z1, - float z2) - { - if (clampDisable == true) - { - return; - } - - if (pos.x < x1) - pos.x = x1; - if (destPos.x < x1) - destPos.x = x1; - if (pos.y < y1) - pos.y = y1; - if (destPos.y < y1) - destPos.y = y1; - if (pos.z < z1) - pos.z = z1; - if (destPos.z < z1) - destPos.z = z1; - if (pos.x > x2) - pos.x = x2; - if (destPos.x > x2) - destPos.x = x2; - if (pos.y > y2) - pos.y = y2; - if (destPos.y > y2) - destPos.y = y2; - if (pos.z > z2) - pos.z = z2; - if (destPos.z > z2) - destPos.z = z2; - } - - void - GameCamera::rotateHV (float h, float v) - { - destAng.x = vAng += v; - destAng.y = hAng += h; - clampAng (); - } - - void - GameCamera::clampAng () - { - if (clampDisable == true && state != sUnit) - { - return; - } - - if (vAng > maxVAng) - vAng = maxVAng; - if (destAng.x > maxVAng) - destAng.x = maxVAng; - if (vAng < minVAng) - vAng = minVAng; - if (destAng.x < minVAng) - destAng.x = minVAng; - if (hAng > 360.f) - hAng -= 360.f; - if (destAng.y > 360.f) - destAng.y -= 360.f; - if (hAng < 0.f) - hAng += 360.f; - if (destAng.y < 0.f) - destAng.y = 360.f; - } - -//move camera forwad but never change heightFactor - void - GameCamera::moveForwardH (float d, float response) - { - Vec3f - offset (std::sin (degToRad (hAng)) * d, 0.f, - -std::cos (degToRad (hAng)) * d); - destPos += offset; - pos.x += offset.x * response; - pos.z += offset.z * response; - } - -//move camera to a side but never change heightFactor - void - GameCamera::moveSideH (float d, float response) - { - Vec3f - offset (std::sin (degToRad (hAng + 90)) * d, 0.f, - -std::cos (degToRad (hAng + 90)) * d); - destPos += offset; - pos.x += (destPos.x - pos.x) * response; - pos.z += (destPos.z - pos.z) * response; - } - - void - GameCamera::moveUp (float d) - { -// pos.y+= d; - destPos.y += d; - } - - void - GameCamera::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - gamecameraNode = rootNode->addChild ("GameCamera"); - -// Vec3f pos; - gamecameraNode->addAttribute ("pos", pos.getString (), - mapTagReplacements); -// Vec3f destPos; - gamecameraNode->addAttribute ("destPos", destPos.getString (), - mapTagReplacements); -// -// float hAng; //YZ plane positive -Z axis - gamecameraNode->addAttribute ("hAng", floatToStr (hAng, 6), - mapTagReplacements); -// float vAng; //XZ plane positive +Z axis - gamecameraNode->addAttribute ("vAng", floatToStr (vAng, 6), - mapTagReplacements); -// float lastHAng; - gamecameraNode->addAttribute ("lastHAng", floatToStr (lastHAng, 6), - mapTagReplacements); - -// float lastVAng; - gamecameraNode->addAttribute ("lastVAng", floatToStr (lastVAng, 6), - mapTagReplacements); -// Vec2f destAng; - gamecameraNode->addAttribute ("destAng", destAng.getString (), - mapTagReplacements); -// float rotate; - gamecameraNode->addAttribute ("rotate", floatToStr (rotate, 6), - mapTagReplacements); -// Vec3f move; - gamecameraNode->addAttribute ("move", move.getString (), - mapTagReplacements); -// State state; - gamecameraNode->addAttribute ("state", intToStr (state), - mapTagReplacements); -// int limitX; - gamecameraNode->addAttribute ("limitX", intToStr (limitX), - mapTagReplacements); -// int limitY; - gamecameraNode->addAttribute ("limitY", intToStr (limitY), - mapTagReplacements); -// //config -// float speed; - gamecameraNode->addAttribute ("speed", floatToStr (speed, 6), - mapTagReplacements); -// bool clampBounds; - gamecameraNode->addAttribute ("clampBounds", intToStr (clampBounds), - mapTagReplacements); -// //float maxRenderDistance; -// float maxHeight; - gamecameraNode->addAttribute ("maxHeight", floatToStr (maxHeight, 6), - mapTagReplacements); -// float minHeight; - gamecameraNode->addAttribute ("minHeight", floatToStr (minHeight, 6), - mapTagReplacements); -// //float maxCameraDist; -// //float minCameraDist; -// float minVAng; - gamecameraNode->addAttribute ("minVAng", floatToStr (minVAng, 6), - mapTagReplacements); -// float maxVAng; - gamecameraNode->addAttribute ("maxVAng", floatToStr (maxVAng, 6), - mapTagReplacements); -// float fov; - gamecameraNode->addAttribute ("fov", floatToStr (fov, 6), - mapTagReplacements); -// float calculatedDefault; - gamecameraNode->addAttribute ("calculatedDefault", - floatToStr (calculatedDefault, 6), - mapTagReplacements); -// std::map > > cacheVisibleQuad; -// int MaxVisibleQuadItemCache; - gamecameraNode->addAttribute ("MaxVisibleQuadItemCache", - intToStr (MaxVisibleQuadItemCache), - mapTagReplacements); - } - - void - GameCamera::loadGame (const XmlNode * rootNode) - { - const XmlNode * - gamecameraNode = rootNode->getChild ("GameCamera"); - - //firstTime = timeflowNode->getAttribute("firstTime")->getFloatValue(); - - // Vec3f pos; - pos = - Vec3f::strToVec3 (gamecameraNode->getAttribute ("pos")->getValue ()); - // Vec3f destPos; - destPos = - Vec3f::strToVec3 (gamecameraNode->getAttribute ("destPos")-> - getValue ()); - // - // float hAng; //YZ plane positive -Z axis - hAng = gamecameraNode->getAttribute ("hAng")->getFloatValue (); - // float vAng; //XZ plane positive +Z axis - vAng = gamecameraNode->getAttribute ("vAng")->getFloatValue (); - // float lastHAng; - lastHAng = gamecameraNode->getAttribute ("lastHAng")->getFloatValue (); - - // float lastVAng; - lastVAng = gamecameraNode->getAttribute ("lastVAng")->getFloatValue (); - // Vec2f destAng; - destAng = - Vec2f::strToVec2 (gamecameraNode->getAttribute ("destAng")-> - getValue ()); - // float rotate; - rotate = gamecameraNode->getAttribute ("rotate")->getFloatValue (); - // Vec3f move; - move = - Vec3f::strToVec3 (gamecameraNode->getAttribute ("move")->getValue ()); - // State state; - state = - static_cast < State > - (gamecameraNode->getAttribute ("state")->getIntValue ()); - // int limitX; - limitX = gamecameraNode->getAttribute ("limitX")->getIntValue (); - // int limitY; - limitY = gamecameraNode->getAttribute ("limitY")->getIntValue (); - // //config - // float speed; - speed = gamecameraNode->getAttribute ("speed")->getFloatValue (); - // bool clampBounds; - clampBounds = - gamecameraNode->getAttribute ("clampBounds")->getIntValue () != 0; - // //float maxRenderDistance; - // float maxHeight; - maxHeight = - gamecameraNode->getAttribute ("maxHeight")->getFloatValue (); - // float minHeight; - minHeight = - gamecameraNode->getAttribute ("minHeight")->getFloatValue (); - // //float maxCameraDist; - // //float minCameraDist; - // float minVAng; - minVAng = gamecameraNode->getAttribute ("minVAng")->getFloatValue (); - // float maxVAng; - maxVAng = gamecameraNode->getAttribute ("maxVAng")->getFloatValue (); - // float fov; - fov = gamecameraNode->getAttribute ("fov")->getFloatValue (); - // float calculatedDefault; - calculatedDefault = - gamecameraNode->getAttribute ("calculatedDefault")->getFloatValue (); - // std::map > > cacheVisibleQuad; - // int MaxVisibleQuadItemCache; - MaxVisibleQuadItemCache = - gamecameraNode->getAttribute ("MaxVisibleQuadItemCache")-> - getIntValue (); - - } - -}} //end namespace + Glest { + namespace + Game { + + // ===================================================== + // class GameCamera + // ===================================================== + + //static std::map > > cacheVisibleQuad; + + // ================== PUBLIC ===================== + + const float + GameCamera::startingVAng = -60.f; + const float + GameCamera::startingHAng = 0.f; + const float + GameCamera::vTransitionMult = 0.125f; + const float + GameCamera::hTransitionMult = 0.125f; + const float + GameCamera::defaultHeight = 20.f; + const float + GameCamera::centerOffsetZ = 8.0f; + const float + GameCamera::shakeDist = 50.f; + + // ================= Constructor ================= + + GameCamera::GameCamera() : + pos(0.f, defaultHeight, 0.f), + destPos(0.f, defaultHeight, 0.f), + destAng(startingVAng, startingHAng) { + //Config &config = Config::getInstance(); + calculatedDefault = defaultHeight; + state = sGame; + + cacheVisibleQuad. + clear(); + //MaxVisibleQuadItemCache = config.getInt("MaxVisibleQuadItemCache",intToStr(-1).c_str()); + MaxVisibleQuadItemCache = -1; + //if(Config::getInstance().getBool("DisableCaching","false") == true) { + // MaxVisibleQuadItemCache = 0; + //} + + //config + speed = + Config::getInstance().getFloat("CameraMoveSpeed", + "15") / GameConstants::cameraFps; + clampBounds = !Config::getInstance().getBool("PhotoMode"); + clampDisable = false; + + vAng = startingVAng; + hAng = startingHAng; + + rotate = 0; + + move = Vec3f(0.f); + + shakeDecrement = 0.f; + currentShakeIntensity = 0; + shakeOffset = Vec2f(0.f); + + //maxRenderDistance = Config::getInstance().getFloat("RenderDistanceMax","64"); + maxHeight = Config::getInstance().getFloat("CameraMaxDistance", "20"); + minHeight = Config::getInstance().getFloat("CameraMinDistance", "7"); + //maxCameraDist = maxHeight; + //minCameraDist = minHeight; + minVAng = -Config::getInstance().getFloat("CameraMaxYaw", "77.5"); + maxVAng = -Config::getInstance().getFloat("CameraMinYaw", "20"); + fov = Config::getInstance().getFloat("CameraFov", "45"); + + lastHAng = 0; + lastVAng = 0; + limitX = 0; + limitY = 0; + } + + GameCamera::~ + GameCamera() { + cacheVisibleQuad.clear(); + } + + std::string + GameCamera::getCameraMovementKey() const { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s_%f_%f_%f_%s,%f", pos.getString().c_str(), + hAng, vAng, rotate, move.getString().c_str(), fov); + return + szBuf; + } + + void + GameCamera::setMaxHeight(float value) { + if (value < 0) { + maxHeight = + Config::getInstance().getFloat("CameraMaxDistance", "20"); + } else { + maxHeight = value; + } + } + + void + GameCamera::setCalculatedDefault(float calculatedDefault) { + this->calculatedDefault = calculatedDefault; + if (maxHeight > 0 && maxHeight < calculatedDefault) { + setMaxHeight(calculatedDefault); + } + resetPosition(); + } + + void + GameCamera::init(int limitX, int limitY) { + this->limitX = limitX; + this->limitY = limitY; + } + + // ==================== Misc ===================== + + void + GameCamera::setPos(Vec2f pos) { + this->pos = Vec3f(pos.x, this->pos.y, pos.y); + clampPosXZ(0.0f, (float) limitX, 0.0f, (float) limitY); + destPos.x = pos.x; + destPos.z = pos.y; + } + + void + GameCamera::setPos(Vec3f pos) { + this->pos = pos; + //clampPosXZ(0.0f, (float)limitX, 0.0f, (float)limitY); + destPos.x = pos.x; + destPos.y = pos.y; + destPos.z = pos.z; + } + + void + GameCamera::shake(int shakeDuration, int shakeStartIntensity, + bool cameraDistanceAffected, Vec3f unitVector) { + float + currentDurationLeft = 0; + float + incomingShakeIntensity = ((float) shakeStartIntensity) / 1000; + + // calculate the shake duration which is left + if (this->currentShakeIntensity > 0.f && this->shakeDecrement != 0.f) { + currentDurationLeft = + this->currentShakeIntensity / this->shakeDecrement; + } + + // reduce new shake effect camera distance related + if (cameraDistanceAffected) { + incomingShakeIntensity = + incomingShakeIntensity * (1.f - + unitVector.dist(getPos()) / + GameCamera::shakeDist); + } + + // add camera shake effect to current one ( if exsists ). + if (this->currentShakeIntensity > 0) { + this->currentShakeIntensity = + this->currentShakeIntensity + incomingShakeIntensity; + } else { + this->currentShakeIntensity = incomingShakeIntensity; + } + + // use bigger shakeDuration to calculate new shakeDecrement + if (currentDurationLeft < shakeDuration) { + this->shakeDecrement = + currentShakeIntensity / ((float) shakeDuration); + } else if (currentDurationLeft != 0.0f) { + this->shakeDecrement = + currentShakeIntensity / ((float) currentDurationLeft); + } + } + + void + GameCamera::shakeCamera() { + //RandomGen random; + if (currentShakeIntensity > 0.f) { + // pos.x += (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; + // pos.z += (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; + + shakeOffset.x = + (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; + shakeOffset.y = + (((float) (rand() % 50)) / 50.f - 0.5f) * currentShakeIntensity; + currentShakeIntensity -= shakeDecrement; + } + } + + void + GameCamera::update() { + //move XZ + if (move.z) { + moveForwardH(speed * move.z, 0.9f); + } + if (move.x) { + moveSideH(speed * move.x, 0.9f); + } + + //free state + if (state == sFree) { + if (std::fabs(rotate) == 1) { + rotateHV(speed * 5 * rotate, 0); + } + if (move.y > 0) { + moveUp(speed * move.y); + if (clampDisable == false && clampBounds && pos.y < maxHeight) { + rotateHV(0.f, -speed * 1.7f * move.y); + } + } + if (move.y < 0) { + moveUp(speed * move.y); + if (clampDisable == false && clampBounds && pos.y > minHeight) { + rotateHV(0.f, -speed * 1.7f * move.y); + } + } + } + + //game state + if (abs(destAng.x - vAng) > 0.01f) { + vAng += (destAng.x - vAng) * hTransitionMult; + } + if (abs(destAng.y - hAng) > 0.01f) { + if (abs(destAng.y - hAng) > 180) { + if (destAng.y > hAng) { + hAng += (destAng.y - hAng - 360) * vTransitionMult; + } else { + hAng += (destAng.y - hAng + 360) * vTransitionMult; + } + } else { + hAng += (destAng.y - hAng) * vTransitionMult; + } + } + if (abs(destPos.x - pos.x) > 0.01f) { + pos.x += (destPos.x - pos.x) / 32.0f; + } + if (abs(destPos.y - pos.y) > 0.01f) { + pos.y += (destPos.y - pos.y) / 32.0f; + } + if (abs(destPos.z - pos.z) > 0.01f) { + pos.z += (destPos.z - pos.z) / 32.0f; + } + clampAng(); + shakeCamera(); + + if (clampDisable == false && clampBounds) { + clampPosXYZ(0.0f, (float) limitX, minHeight, maxHeight, 0.0f, + (float) limitY); + } + } + + Quad2i + GameCamera::computeVisibleQuad() { + //printf("\n@@@ hAng [%f] vAng [%f] fov [%f]\n",hAng,vAng,fov); + + if (MaxVisibleQuadItemCache != 0) { + std::map < float, + std::map < float, + std::map < + Vec3f, + Quad2i > > >::const_iterator + iterFind = cacheVisibleQuad.find(fov); + if (iterFind != cacheVisibleQuad.end()) { + std::map < float, + std::map < + Vec3f, + Quad2i > >::const_iterator + iterFind2 = iterFind->second.find(hAng); + if (iterFind2 != iterFind->second.end()) { + std::map < Vec3f, Quad2i >::const_iterator iterFind3 = + iterFind2->second.find(pos); + if (iterFind3 != iterFind2->second.end()) { + return iterFind3->second; + } + } + } + } + + float + nearDist = 15.f; + float + dist = pos.y > nearDist ? pos.y * 1.2f : nearDist; + float + farDist = 90.f * (pos.y > nearDist ? pos.y / 15.f : 1.f); + const float + viewDegree = 180.f; + + Vec2f + v(std::sin(degToRad(viewDegree - hAng)), + std::cos(degToRad(viewDegree - hAng))); + Vec2f + v1(std::sin(degToRad(viewDegree - hAng - fov)), + std::cos(degToRad(viewDegree - hAng - fov))); + Vec2f + v2(std::sin(degToRad(viewDegree - hAng + fov)), + std::cos(degToRad(viewDegree - hAng + fov))); + + v.normalize(); + v1.normalize(); + v2.normalize(); + + Vec2f + p = Vec2f(pos.x, pos.z) - v * dist; + Vec2i + p1(static_cast (p.x + v1.x * nearDist), + static_cast (p.y + v1.y * nearDist)); + Vec2i + p2(static_cast (p.x + v1.x * farDist), + static_cast (p.y + v1.y * farDist)); + Vec2i + p3(static_cast (p.x + v2.x * nearDist), + static_cast (p.y + v2.y * nearDist)); + Vec2i + p4(static_cast (p.x + v2.x * farDist), + static_cast (p.y + v2.y * farDist)); + + const bool + debug = false; + + Quad2i + result; + if (hAng >= 135 && hAng <= 225) { + if (debug) + printf("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); + + result = Quad2i(p1, p2, p3, p4); + if (MaxVisibleQuadItemCache != 0 && + (MaxVisibleQuadItemCache < 0 + || (int) cacheVisibleQuad[fov][hAng].size() <= + MaxVisibleQuadItemCache)) { + cacheVisibleQuad[fov][hAng][pos] = result; + } + } else if (hAng >= 45 && hAng <= 135) { + if (debug) + printf("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); + + result = Quad2i(p3, p1, p4, p2); + if (MaxVisibleQuadItemCache != 0 && + (MaxVisibleQuadItemCache < 0 + || (int) cacheVisibleQuad[fov][hAng].size() <= + MaxVisibleQuadItemCache)) { + cacheVisibleQuad[fov][hAng][pos] = result; + } + } else if (hAng >= 225 && hAng <= 315) { + if (debug) + printf("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); + + result = Quad2i(p2, p4, p1, p3); + if (MaxVisibleQuadItemCache != 0 && + (MaxVisibleQuadItemCache < 0 + || (int) cacheVisibleQuad[fov][hAng].size() <= + MaxVisibleQuadItemCache)) { + cacheVisibleQuad[fov][hAng][pos] = result; + } + } else { + if (debug) + printf("Line %d hAng [%f] fov [%f]\n", __LINE__, hAng, fov); + + result = Quad2i(p4, p3, p2, p1); + if (MaxVisibleQuadItemCache != 0 && + (MaxVisibleQuadItemCache < 0 + || (int) cacheVisibleQuad[fov][hAng].size() <= + MaxVisibleQuadItemCache)) { + cacheVisibleQuad[fov][hAng][pos] = Quad2i(p4, p3, p2, p1); + } + } + + return result; + } + + void + GameCamera::setState(State s) { + if (s == sGame) { + state = sGame; + setClampDisabled(false); + resetPosition(); + } else if (s == sUnit) { + state = sUnit; + setClampDisabled(true); + } else if (s == sFree) { + state = sFree; + setClampDisabled(false); + resetPosition(); + } else { + abort(); //"unknown camera state" + } + } + + void + GameCamera::resetPosition() { + destAng.x = startingVAng; + destAng.y = startingHAng; + destPos.y = calculatedDefault; + } + + void + GameCamera::centerXZ(float x, float z) { + destPos.x = pos.x = x; + destPos.z = pos.z = z + centerOffsetZ; + } + + //void GameCamera::transitionXYZ(float x, float y, float z) { + // destPos.x += x; + // destPos.y += y; + // destPos.z += z; + // clampPosXYZ(0.0f, (float)limitX, minHeight, maxHeight, 0.0f, (float)limitY); + //} + + void + GameCamera::transitionVH(float v, float h) { + destAng.x -= v; + //destPos.y -= v * destPos.y / 100.f; + destAng.y -= h; + clampAng(); + } + + void + GameCamera::rotateToVH(float v, float h) { + destAng.x = v; + destAng.y = h; + clampAng(); + } + + void + GameCamera::zoom(float dist) { + float + flatDist = dist * std::cos(degToRad(vAng)); + Vec3f + offset(flatDist * std::sin(degToRad(hAng)), + dist * std::sin(degToRad(vAng)), + flatDist * -std::cos(degToRad(hAng))); + + destPos += offset; + } + + void + GameCamera::load(const XmlNode * node) { + //destPos = node->getChildVec3fValue("pos"); + //destAng = node->getChildVec2fValue("angle"); + } + + void + GameCamera::save(XmlNode * node) const { + //node->addChild("pos", pos); + //node->addChild("angle", Vec2f(vAng, hAng)); + } + + // ==================== PRIVATE ==================== + + void + GameCamera::clampPosXZ(float x1, float x2, float z1, float z2) { + if (clampDisable == true) { + return; + } + + if (pos.x < x1) + pos.x = x1; + if (destPos.x < x1) + destPos.x = x1; + if (pos.z < z1) + pos.z = z1; + if (destPos.z < z1) + destPos.z = z1; + if (pos.x > x2) + pos.x = x2; + if (destPos.x > x2) + destPos.x = x2; + if (pos.z > z2) + pos.z = z2; + if (destPos.z > z2) + destPos.z = z2; + } + + void + GameCamera::clampPosXYZ(float x1, float x2, float y1, float y2, float z1, + float z2) { + if (clampDisable == true) { + return; + } + + if (pos.x < x1) + pos.x = x1; + if (destPos.x < x1) + destPos.x = x1; + if (pos.y < y1) + pos.y = y1; + if (destPos.y < y1) + destPos.y = y1; + if (pos.z < z1) + pos.z = z1; + if (destPos.z < z1) + destPos.z = z1; + if (pos.x > x2) + pos.x = x2; + if (destPos.x > x2) + destPos.x = x2; + if (pos.y > y2) + pos.y = y2; + if (destPos.y > y2) + destPos.y = y2; + if (pos.z > z2) + pos.z = z2; + if (destPos.z > z2) + destPos.z = z2; + } + + void + GameCamera::rotateHV(float h, float v) { + destAng.x = vAng += v; + destAng.y = hAng += h; + clampAng(); + } + + void + GameCamera::clampAng() { + if (clampDisable == true && state != sUnit) { + return; + } + + if (vAng > maxVAng) + vAng = maxVAng; + if (destAng.x > maxVAng) + destAng.x = maxVAng; + if (vAng < minVAng) + vAng = minVAng; + if (destAng.x < minVAng) + destAng.x = minVAng; + if (hAng > 360.f) + hAng -= 360.f; + if (destAng.y > 360.f) + destAng.y -= 360.f; + if (hAng < 0.f) + hAng += 360.f; + if (destAng.y < 0.f) + destAng.y = 360.f; + } + + //move camera forwad but never change heightFactor + void + GameCamera::moveForwardH(float d, float response) { + Vec3f + offset(std::sin(degToRad(hAng)) * d, 0.f, + -std::cos(degToRad(hAng)) * d); + destPos += offset; + pos.x += offset.x * response; + pos.z += offset.z * response; + } + + //move camera to a side but never change heightFactor + void + GameCamera::moveSideH(float d, float response) { + Vec3f + offset(std::sin(degToRad(hAng + 90)) * d, 0.f, + -std::cos(degToRad(hAng + 90)) * d); + destPos += offset; + pos.x += (destPos.x - pos.x) * response; + pos.z += (destPos.z - pos.z) * response; + } + + void + GameCamera::moveUp(float d) { + // pos.y+= d; + destPos.y += d; + } + + void + GameCamera::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + gamecameraNode = rootNode->addChild("GameCamera"); + + // Vec3f pos; + gamecameraNode->addAttribute("pos", pos.getString(), + mapTagReplacements); + // Vec3f destPos; + gamecameraNode->addAttribute("destPos", destPos.getString(), + mapTagReplacements); + // + // float hAng; //YZ plane positive -Z axis + gamecameraNode->addAttribute("hAng", floatToStr(hAng, 6), + mapTagReplacements); + // float vAng; //XZ plane positive +Z axis + gamecameraNode->addAttribute("vAng", floatToStr(vAng, 6), + mapTagReplacements); + // float lastHAng; + gamecameraNode->addAttribute("lastHAng", floatToStr(lastHAng, 6), + mapTagReplacements); + + // float lastVAng; + gamecameraNode->addAttribute("lastVAng", floatToStr(lastVAng, 6), + mapTagReplacements); + // Vec2f destAng; + gamecameraNode->addAttribute("destAng", destAng.getString(), + mapTagReplacements); + // float rotate; + gamecameraNode->addAttribute("rotate", floatToStr(rotate, 6), + mapTagReplacements); + // Vec3f move; + gamecameraNode->addAttribute("move", move.getString(), + mapTagReplacements); + // State state; + gamecameraNode->addAttribute("state", intToStr(state), + mapTagReplacements); + // int limitX; + gamecameraNode->addAttribute("limitX", intToStr(limitX), + mapTagReplacements); + // int limitY; + gamecameraNode->addAttribute("limitY", intToStr(limitY), + mapTagReplacements); + // //config + // float speed; + gamecameraNode->addAttribute("speed", floatToStr(speed, 6), + mapTagReplacements); + // bool clampBounds; + gamecameraNode->addAttribute("clampBounds", intToStr(clampBounds), + mapTagReplacements); + // //float maxRenderDistance; + // float maxHeight; + gamecameraNode->addAttribute("maxHeight", floatToStr(maxHeight, 6), + mapTagReplacements); + // float minHeight; + gamecameraNode->addAttribute("minHeight", floatToStr(minHeight, 6), + mapTagReplacements); + // //float maxCameraDist; + // //float minCameraDist; + // float minVAng; + gamecameraNode->addAttribute("minVAng", floatToStr(minVAng, 6), + mapTagReplacements); + // float maxVAng; + gamecameraNode->addAttribute("maxVAng", floatToStr(maxVAng, 6), + mapTagReplacements); + // float fov; + gamecameraNode->addAttribute("fov", floatToStr(fov, 6), + mapTagReplacements); + // float calculatedDefault; + gamecameraNode->addAttribute("calculatedDefault", + floatToStr(calculatedDefault, 6), + mapTagReplacements); + // std::map > > cacheVisibleQuad; + // int MaxVisibleQuadItemCache; + gamecameraNode->addAttribute("MaxVisibleQuadItemCache", + intToStr(MaxVisibleQuadItemCache), + mapTagReplacements); + } + + void + GameCamera::loadGame(const XmlNode * rootNode) { + const XmlNode * + gamecameraNode = rootNode->getChild("GameCamera"); + + //firstTime = timeflowNode->getAttribute("firstTime")->getFloatValue(); + + // Vec3f pos; + pos = + Vec3f::strToVec3(gamecameraNode->getAttribute("pos")->getValue()); + // Vec3f destPos; + destPos = + Vec3f::strToVec3(gamecameraNode->getAttribute("destPos")-> + getValue()); + // + // float hAng; //YZ plane positive -Z axis + hAng = gamecameraNode->getAttribute("hAng")->getFloatValue(); + // float vAng; //XZ plane positive +Z axis + vAng = gamecameraNode->getAttribute("vAng")->getFloatValue(); + // float lastHAng; + lastHAng = gamecameraNode->getAttribute("lastHAng")->getFloatValue(); + + // float lastVAng; + lastVAng = gamecameraNode->getAttribute("lastVAng")->getFloatValue(); + // Vec2f destAng; + destAng = + Vec2f::strToVec2(gamecameraNode->getAttribute("destAng")-> + getValue()); + // float rotate; + rotate = gamecameraNode->getAttribute("rotate")->getFloatValue(); + // Vec3f move; + move = + Vec3f::strToVec3(gamecameraNode->getAttribute("move")->getValue()); + // State state; + state = + static_cast + (gamecameraNode->getAttribute("state")->getIntValue()); + // int limitX; + limitX = gamecameraNode->getAttribute("limitX")->getIntValue(); + // int limitY; + limitY = gamecameraNode->getAttribute("limitY")->getIntValue(); + // //config + // float speed; + speed = gamecameraNode->getAttribute("speed")->getFloatValue(); + // bool clampBounds; + clampBounds = + gamecameraNode->getAttribute("clampBounds")->getIntValue() != 0; + // //float maxRenderDistance; + // float maxHeight; + maxHeight = + gamecameraNode->getAttribute("maxHeight")->getFloatValue(); + // float minHeight; + minHeight = + gamecameraNode->getAttribute("minHeight")->getFloatValue(); + // //float maxCameraDist; + // //float minCameraDist; + // float minVAng; + minVAng = gamecameraNode->getAttribute("minVAng")->getFloatValue(); + // float maxVAng; + maxVAng = gamecameraNode->getAttribute("maxVAng")->getFloatValue(); + // float fov; + fov = gamecameraNode->getAttribute("fov")->getFloatValue(); + // float calculatedDefault; + calculatedDefault = + gamecameraNode->getAttribute("calculatedDefault")->getFloatValue(); + // std::map > > cacheVisibleQuad; + // int MaxVisibleQuadItemCache; + MaxVisibleQuadItemCache = + gamecameraNode->getAttribute("MaxVisibleQuadItemCache")-> + getIntValue(); + + } + + } +} //end namespace diff --git a/source/glest_game/game/game_camera.h b/source/glest_game/game/game_camera.h index bccd96e9c..9607bfcdd 100644 --- a/source/glest_game/game/game_camera.h +++ b/source/glest_game/game/game_camera.h @@ -23,242 +23,216 @@ # include # include "leak_dumper.h" -namespace Shared -{ - namespace Xml - { - class XmlNode; -}} +namespace Shared { + namespace Xml { + class XmlNode; + } +} -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using::Shared::Graphics::Quad2i; - using::Shared::Graphics::Vec3f; - using::Shared::Graphics::Vec2f; - using::Shared::Xml::XmlNode; + using::Shared::Graphics::Quad2i; + using::Shared::Graphics::Vec3f; + using::Shared::Graphics::Vec2f; + using::Shared::Xml::XmlNode; - class Config; + class Config; -// ===================================================== -// class GameCamera -// -/// A basic camera that holds information about the game view -// ===================================================== + // ===================================================== + // class GameCamera + // + /// A basic camera that holds information about the game view + // ===================================================== - class GameCamera - { - public: - static const float startingVAng; - static const float startingHAng; - static const float vTransitionMult; - static const float hTransitionMult; - static const float defaultHeight; - static const float centerOffsetZ; - static const float shakeDist; + class GameCamera { + public: + static const float startingVAng; + static const float startingHAng; + static const float vTransitionMult; + static const float hTransitionMult; + static const float defaultHeight; + static const float centerOffsetZ; + static const float shakeDist; - public: - enum State - { - sGame, - sFree, - sUnit - }; + public: + enum State { + sGame, + sFree, + sUnit + }; - private: - Vec3f pos; - Vec3f destPos; - Vec2f shakeOffset; + private: + Vec3f pos; + Vec3f destPos; + Vec2f shakeOffset; - float hAng; //YZ plane positive -Z axis - float vAng; //XZ plane positive +Z axis - float lastHAng; - float lastVAng; - Vec2f destAng; + float hAng; //YZ plane positive -Z axis + float vAng; //XZ plane positive +Z axis + float lastHAng; + float lastVAng; + Vec2f destAng; - float rotate; + float rotate; - Vec3f move; + Vec3f move; - float shakeDecrement; - float currentShakeIntensity; - State state; + float shakeDecrement; + float currentShakeIntensity; + State state; - int limitX; - int limitY; + int limitX; + int limitY; - //config - float speed; - bool clampBounds; - bool clampDisable; - //float maxRenderDistance; - float maxHeight; - float minHeight; - //float maxCameraDist; - //float minCameraDist; - float minVAng; - float maxVAng; - float fov; + //config + float speed; + bool clampBounds; + bool clampDisable; + //float maxRenderDistance; + float maxHeight; + float minHeight; + //float maxCameraDist; + //float minCameraDist; + float minVAng; + float maxVAng; + float fov; - float calculatedDefault; + float calculatedDefault; - std::map < float, std::map < float, std::map < Vec3f, - Quad2i > > >cacheVisibleQuad; - int MaxVisibleQuadItemCache; + std::map < float, std::map < float, std::map < Vec3f, + Quad2i > > >cacheVisibleQuad; + int MaxVisibleQuadItemCache; - public: - GameCamera (); - ~GameCamera (); + public: + GameCamera(); + ~GameCamera(); - void init (int limitX, int limitY); + void init(int limitX, int limitY); - //get - float getHAng () const - { - return hAng; - }; - float getVAng () const - { - return vAng; - } - State getState () const - { - return state; - } - void setState (State s); - void resetCamera () - { - setState (sGame); - } + //get + float getHAng() const { + return hAng; + }; + float getVAng() const { + return vAng; + } + State getState() const { + return state; + } + void setState(State s); + void resetCamera() { + setState(sGame); + } - const Vec3f & getPos () const - { - return pos; - } - float getFov () const - { - return fov; - } - //set - void setRotate (float rotate) - { - this->rotate = rotate; - } - void setPos (Vec2f pos); - void setPos (Vec3f pos); + const Vec3f & getPos() const { + return pos; + } + float getFov() const { + return fov; + } + //set + void setRotate(float rotate) { + this->rotate = rotate; + } + void setPos(Vec2f pos); + void setPos(Vec3f pos); - const Vec2f & getShakeOffset () const - { - return shakeOffset; - } - void shake (int shakeDuration, int shakeStartIntensity, - bool cameraDistanceAffected, Vec3f unitVector); + const Vec2f & getShakeOffset() const { + return shakeOffset; + } + void shake(int shakeDuration, int shakeStartIntensity, + bool cameraDistanceAffected, Vec3f unitVector); - void setMoveX (float f) - { - this->move.x = f; - } - void setMoveY (float f) - { - this->move.y = f; - } - void setMoveZ (float f) - { - this->move.z = f; - } + void setMoveX(float f) { + this->move.x = f; + } + void setMoveY(float f) { + this->move.y = f; + } + void setMoveZ(float f) { + this->move.z = f; + } - inline bool isMoving () const - { - return (this->move.x != 0.0 || this->move.y != 0.0 - || this->move.z != 0.0); - } - inline void stopMove () - { - this->move.x = 0.0; - this->move.y = 0.0; - this->move.z = 0.0; - } + inline bool isMoving() const { + return (this->move.x != 0.0 || this->move.y != 0.0 + || this->move.z != 0.0); + } + inline void stopMove() { + this->move.x = 0.0; + this->move.y = 0.0; + this->move.z = 0.0; + } - void stop () - { - destPos = pos; - destAng.x = vAng; - destAng.y = hAng; - } + void stop() { + destPos = pos; + destAng.x = vAng; + destAng.y = hAng; + } - std::string getCameraMovementKey ()const; + std::string getCameraMovementKey()const; - //other - void update (); - Quad2i computeVisibleQuad (); + //other + void update(); + Quad2i computeVisibleQuad(); - void centerXZ (float x, float z); - //void transitionXYZ(float x, float y, float z); - void transitionVH (float v, float h); - void rotateToVH (float v, float h); + void centerXZ(float x, float z); + //void transitionXYZ(float x, float y, float z); + void transitionVH(float v, float h); + void rotateToVH(float v, float h); - void zoom (float dist); - void moveForwardH (float dist, float response); // response: 1.0 for immediate, 0 for full inertia - void moveSideH (float dist, float response); + void zoom(float dist); + void moveForwardH(float dist, float response); // response: 1.0 for immediate, 0 for full inertia + void moveSideH(float dist, float response); - void load (const XmlNode * node); - void save (XmlNode * node) const; + void load(const XmlNode * node); + void save(XmlNode * node) const; - void setMaxHeight (float value); - float getCalculatedDefault () const - { - return calculatedDefault; - } - void setCalculatedDefault (float calculatedDefault); + void setMaxHeight(float value); + float getCalculatedDefault() const { + return calculatedDefault; + } + void setCalculatedDefault(float calculatedDefault); - float getMaxHeight () const - { - return maxHeight; - } - void setFov (float value) - { - fov = value; - } - void setMinVAng (float value) - { - minVAng = value; - } - void setMaxVAng (float value) - { - maxVAng = value; - } + float getMaxHeight() const { + return maxHeight; + } + void setFov(float value) { + fov = value; + } + void setMinVAng(float value) { + minVAng = value; + } + void setMaxVAng(float value) { + maxVAng = value; + } - void setHAng (float value) - { - hAng = value; - }; - void setVAng (float value) - { - vAng = value; - } + void setHAng(float value) { + hAng = value; + }; + void setVAng(float value) { + vAng = value; + } - void saveGame (XmlNode * rootNode); - void loadGame (const XmlNode * rootNode); + void saveGame(XmlNode * rootNode); + void loadGame(const XmlNode * rootNode); - private: - //void setClampBounds(bool value) { clampBounds = value; } - void resetPosition (); - void setClampDisabled (bool value) - { - clampDisable = value; - }; - void clampPosXYZ (float x1, float x2, float y1, float y2, float z1, - float z2); - void clampPosXZ (float x1, float x2, float z1, float z2); - void clampAng (); - void moveUp (float dist); - void rotateHV (float h, float v); - void shakeCamera (); - }; + private: + //void setClampBounds(bool value) { clampBounds = value; } + void resetPosition(); + void setClampDisabled(bool value) { + clampDisable = value; + }; + void clampPosXYZ(float x1, float x2, float y1, float y2, float z1, + float z2); + void clampPosXZ(float x1, float x2, float z1, float z2); + void clampAng(); + void moveUp(float dist); + void rotateHV(float h, float v); + void shakeCamera(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/game/game_constants.h b/source/glest_game/game/game_constants.h index 37c4ac51a..d2d8dbcb2 100644 --- a/source/glest_game/game/game_constants.h +++ b/source/glest_game/game/game_constants.h @@ -33,353 +33,328 @@ # include using namespace - Shared::Graphics; +Shared::Graphics; using namespace - std; +std; using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - template < - typename - T > - class - EnumParser - { - private: - typedef - map < - string, - T > - enumMapType; - typedef typename - enumMapType::const_iterator - enumMapTypeIter; - EnumParser (); + template < + typename + T > + class + EnumParser { + private: + typedef + map < + string, + T > + enumMapType; + typedef typename + enumMapType::const_iterator + enumMapTypeIter; + EnumParser(); - enumMapType - enumMap; + enumMapType + enumMap; - public: + public: - static T - getEnum (const string & value) - { - static - EnumParser < - T > - parser; - enumMapTypeIter - iValue = parser.enumMap.find (value); - if (iValue == parser.enumMap.end ()) - { - throw - std::runtime_error ("unknown enum lookup [" + value + "]"); - } - return - iValue-> - second; - } - static string - getString (const T & value) - { - static - EnumParser < - T > - parser; - for (enumMapTypeIter iValue = parser.enumMap.begin (); - iValue != parser.enumMap.end (); ++iValue) - { - if (iValue->second == value) - { - return iValue->first; - } - } - throw - std::runtime_error ("unknown enum lookup [" + intToStr (value) + "]"); - } - static int - getCount () - { - static - EnumParser < - T > - parser; - return parser.enumMap.size (); - } - }; + static T + getEnum(const string & value) { + static + EnumParser < + T > + parser; + enumMapTypeIter + iValue = parser.enumMap.find(value); + if (iValue == parser.enumMap.end()) { + throw + std::runtime_error("unknown enum lookup [" + value + "]"); + } + return + iValue-> + second; + } + static string + getString(const T & value) { + static + EnumParser < + T > + parser; + for (enumMapTypeIter iValue = parser.enumMap.begin(); + iValue != parser.enumMap.end(); ++iValue) { + if (iValue->second == value) { + return iValue->first; + } + } + throw + std::runtime_error("unknown enum lookup [" + intToStr(value) + "]"); + } + static int + getCount() { + static + EnumParser < + T > + parser; + return parser.enumMap.size(); + } + }; -// ===================================================== -// class GameConstants -// ===================================================== + // ===================================================== + // class GameConstants + // ===================================================== - const Vec4f - BLACK (0.0f, 0.0f, 0.0f, 1.0f); - const Vec4f - RED (1.0f, 0.0f, 0.0f, 1.0f); - const Vec4f - GREEN (0.0f, 1.0f, 0.0f, 1.0f); - const Vec4f - BLUE (0.0f, 0.0f, 1.0f, 1.0f); - const Vec4f - GLASS (1.0f, 1.0f, 1.0f, 0.3f); - const Vec4f - CYAN (0.0f, 1.0f, 1.0f, 1.0f); - const Vec4f - YELLOW (1.0f, 1.0f, 0.0f, 1.0f); - const Vec4f - MAGENTA (1.0f, 0.0f, 1.0f, 1.0f); - const Vec4f - WHITE (1.0f, 1.0f, 1.0f, 1.0f); - const Vec4f - ORANGE (1.0f, 0.7f, 0.0f, 1.0f); + const Vec4f + BLACK(0.0f, 0.0f, 0.0f, 1.0f); + const Vec4f + RED(1.0f, 0.0f, 0.0f, 1.0f); + const Vec4f + GREEN(0.0f, 1.0f, 0.0f, 1.0f); + const Vec4f + BLUE(0.0f, 0.0f, 1.0f, 1.0f); + const Vec4f + GLASS(1.0f, 1.0f, 1.0f, 0.3f); + const Vec4f + CYAN(0.0f, 1.0f, 1.0f, 1.0f); + const Vec4f + YELLOW(1.0f, 1.0f, 0.0f, 1.0f); + const Vec4f + MAGENTA(1.0f, 0.0f, 1.0f, 1.0f); + const Vec4f + WHITE(1.0f, 1.0f, 1.0f, 1.0f); + const Vec4f + ORANGE(1.0f, 0.7f, 0.0f, 1.0f); - enum PathFinderType - { - pfBasic - }; + enum PathFinderType { + pfBasic + }; - enum TravelState - { - tsArrived, - tsMoving, - tsBlocked, - tsImpossible - }; + enum TravelState { + tsArrived, + tsMoving, + tsBlocked, + tsImpossible + }; - enum ControlType - { - ctClosed, - ctCpuEasy, - ctCpu, - ctCpuUltra, - ctCpuZeta, - ctNetwork, - ctNetworkUnassigned, - ctHuman, + enum ControlType { + ctClosed, + ctCpuEasy, + ctCpu, + ctCpuUltra, + ctCpuZeta, + ctNetwork, + ctNetworkUnassigned, + ctHuman, - ctNetworkCpuEasy, - ctNetworkCpu, - ctNetworkCpuUltra, - ctNetworkCpuZeta - }; + ctNetworkCpuEasy, + ctNetworkCpu, + ctNetworkCpuUltra, + ctNetworkCpuZeta + }; - enum NetworkRole - { - nrServer, - nrClient, - nrIdle - }; + enum NetworkRole { + nrServer, + nrClient, + nrIdle + }; - enum FactionPersonalityType - { - fpt_Normal, - fpt_Observer, + enum FactionPersonalityType { + fpt_Normal, + fpt_Observer, - fpt_EndCount - }; + fpt_EndCount + }; - enum MasterServerGameStatusType - { - game_status_waiting_for_players = 0, - game_status_waiting_for_start = 1, - game_status_in_progress = 2, - game_status_finished = 3 - }; + enum MasterServerGameStatusType { + game_status_waiting_for_players = 0, + game_status_waiting_for_start = 1, + game_status_in_progress = 2, + game_status_finished = 3 + }; - class - GameConstants - { - public: - static const int - specialFactions = fpt_EndCount - 1; - static const int - maxPlayers = 10; - static const int - serverPort = 61357; - static const int - serverAdminPort = 61355; - static int - updateFps; - static int - cameraFps; + class + GameConstants { + public: + static const int + specialFactions = fpt_EndCount - 1; + static const int + maxPlayers = 10; + static const int + serverPort = 61357; + static const int + serverAdminPort = 61355; + static int + updateFps; + static int + cameraFps; - static int - networkFramePeriod; - static const int - networkPingInterval = 5; - static const int - networkSmoothInterval = 30; - static const int - maxClientConnectHandshakeSecs = 10; + static int + networkFramePeriod; + static const int + networkPingInterval = 5; + static const int + networkSmoothInterval = 30; + static const int + maxClientConnectHandshakeSecs = 10; - static const int - cellScale = 2; - static const int - clusterSize = 16; + static const int + cellScale = 2; + static const int + clusterSize = 16; - static const char * - folder_path_maps; - static const char * - folder_path_scenarios; - static const char * - folder_path_techs; - static const char * - folder_path_tilesets; - static const char * - folder_path_tutorials; + static const char * + folder_path_maps; + static const char * + folder_path_scenarios; + static const char * + folder_path_techs; + static const char * + folder_path_tilesets; + static const char * + folder_path_tutorials; - static const char * - NETWORK_SLOT_UNCONNECTED_SLOTNAME; - static const char * - NETWORK_SLOT_CLOSED_SLOTNAME; + static const char * + NETWORK_SLOT_UNCONNECTED_SLOTNAME; + static const char * + NETWORK_SLOT_CLOSED_SLOTNAME; - static const char * - folder_path_screenshots; + static const char * + folder_path_screenshots; - static const char * - OBSERVER_SLOTNAME; - static const char * - RANDOMFACTION_SLOTNAME; + static const char * + OBSERVER_SLOTNAME; + static const char * + RANDOMFACTION_SLOTNAME; - static const char * - steamCacheInstanceKey; - static const char * - preCacheThreadCacheLookupKey; - static const char * - playerTextureCacheLookupKey; - static const char * - ircClientCacheLookupKey; - static const char * - factionPreviewTextureCacheLookupKey; - static const char * - characterMenuScreenPositionListCacheLookupKey; - static const char * - pathCacheLookupKey; - static const char * - path_data_CacheLookupKey; - static const char * - path_ini_CacheLookupKey; - static const char * - path_logs_CacheLookupKey; + static const char * + steamCacheInstanceKey; + static const char * + preCacheThreadCacheLookupKey; + static const char * + playerTextureCacheLookupKey; + static const char * + ircClientCacheLookupKey; + static const char * + factionPreviewTextureCacheLookupKey; + static const char * + characterMenuScreenPositionListCacheLookupKey; + static const char * + pathCacheLookupKey; + static const char * + path_data_CacheLookupKey; + static const char * + path_ini_CacheLookupKey; + static const char * + path_logs_CacheLookupKey; - static const char * - application_name; + static const char * + application_name; - static const char * - saveNetworkGameFileServerCompressed; - static const char * - saveNetworkGameFileServer; - static const char * - saveNetworkGameFileClientCompressed; - static const char * - saveNetworkGameFileClient; - static const char * - saveGameFileDefault; - static const char * - saveGameFileAutoTestDefault; - static const char * - saveGameFilePattern; + static const char * + saveNetworkGameFileServerCompressed; + static const char * + saveNetworkGameFileServer; + static const char * + saveNetworkGameFileClientCompressed; + static const char * + saveNetworkGameFileClient; + static const char * + saveGameFileDefault; + static const char * + saveGameFileAutoTestDefault; + static const char * + saveGameFilePattern; - // VC++ Chokes on init of non integral static types - static const float - normalMultiplier; - static const float - easyMultiplier; - static const float - ultraMultiplier; - static const float - megaMultiplier; - // + // VC++ Chokes on init of non integral static types + static const float + normalMultiplier; + static const float + easyMultiplier; + static const float + ultraMultiplier; + static const float + megaMultiplier; + // - static const char * - LOADING_SCREEN_FILE; - static const char * - LOADING_SCREEN_FILE_FILTER; - static const char * - PREVIEW_SCREEN_FILE; - static const char * - PREVIEW_SCREEN_FILE_FILTER; - static const char * - HUD_SCREEN_FILE; - static const char * - HUD_SCREEN_FILE_FILTER; - }; + static const char * + LOADING_SCREEN_FILE; + static const char * + LOADING_SCREEN_FILE_FILTER; + static const char * + PREVIEW_SCREEN_FILE; + static const char * + PREVIEW_SCREEN_FILE_FILTER; + static const char * + HUD_SCREEN_FILE; + static const char * + HUD_SCREEN_FILE_FILTER; + }; - enum PathType - { - ptMaps, - ptScenarios, - ptTechs, - ptTilesets, - ptTutorials - }; + enum PathType { + ptMaps, + ptScenarios, + ptTechs, + ptTilesets, + ptTutorials + }; - struct CardinalDir - { - public: - enum Enum - { - NORTH, - EAST, - SOUTH, - WEST, - COUNT }; + struct CardinalDir { + public: + enum Enum { + NORTH, + EAST, + SOUTH, + WEST, + COUNT + }; - CardinalDir (): - value (NORTH) - { - } - explicit - CardinalDir (Enum v): - value (v) - { - } - explicit - CardinalDir (int v) - { - assertDirValid (v); - value = static_cast < Enum > (v); - } - operator - Enum () const - { - return - value; - } - int - asInt () const - { - return (int) - value; - } + CardinalDir() : + value(NORTH) { + } + explicit + CardinalDir(Enum v) : + value(v) { + } + explicit + CardinalDir(int v) { + assertDirValid(v); + value = static_cast (v); + } + operator + Enum () const { + return + value; + } + int + asInt() const { + return (int) + value; + } - static void - assertDirValid (int v) - { - assert (v >= 0 && v < 4); - } - void - operator++ () - { - value = static_cast < Enum > ((value + 1) % 4); - } - void - operator-- () - { // mod with negative numbers is a 'grey area', hence the +3 rather than -1 - value = static_cast < Enum > ((value + 3) % 4); - } + static void + assertDirValid(int v) { + assert(v >= 0 && v < 4); + } + void + operator++ () { + value = static_cast ((value + 1) % 4); + } + void + operator-- () { // mod with negative numbers is a 'grey area', hence the +3 rather than -1 + value = static_cast ((value + 3) % 4); + } - private: - Enum value; - }; + private: + Enum value; + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/game/game_settings.h b/source/glest_game/game/game_settings.h index 891d7cf49..be1d61510 100644 --- a/source/glest_game/game/game_settings.h +++ b/source/glest_game/game/game_settings.h @@ -23,1741 +23,1569 @@ # include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using namespace - Shared::PlatformCommon; +Shared::PlatformCommon; using namespace - Shared::Platform; +Shared::Platform; using - Shared::Xml::XmlNode; +Shared::Xml::XmlNode; namespace - Glest -{ - namespace - Game - { - - enum ModType - { - mt_None, - mt_Map, - mt_Tileset, - mt_Techtree, - mt_Scenario - }; - -// =============================== -// class ModInfo -// =============================== - - class - ModInfo - { - public: - string - name; - string - url; - string - imageUrl; - string - description; - string - count; // used for faction count for example - string - crc; - string - localCRC; - ModType - type; - public: - ModInfo (); - }; - -// ===================================================== -// class GameSettings -// ===================================================== - - enum FlagTypes1 - { - ft1_none = 0x00, - ft1_show_map_resources = 0x01, - ft1_allow_team_switching = 0x02, - ft1_allow_in_game_joining = 0x04, - ft1_network_synch_checks_verbose = 0x08, - ft1_network_synch_checks = 0x10, - ft1_allow_shared_team_units = 0x20, - ft1_allow_shared_team_resources = 0x40 - //ft1_xxx = 0x80 - }; - - inline static bool - isFlagType1BitEnabled (uint32 flagValue, FlagTypes1 type) - { - return ((flagValue & (uint32) type) == (uint32) type); - } - - enum NetworkPlayerStatusType - { - npst_None = 0, - npst_PickSettings = 1, - npst_BeRightBack = 2, - npst_Ready = 3, - npst_Disconnected = 4 - }; - - class - GameSettings - { - private: - string - gameName; - string - description; - string - map; - string - tileset; - string - tech; - string - scenario; - string - scenarioDir; - string - factionTypeNames[GameConstants::maxPlayers]; //faction names - string - networkPlayerNames[GameConstants::maxPlayers]; - string - networkPlayerPlatform[GameConstants::maxPlayers]; - int - networkPlayerStatuses[GameConstants::maxPlayers]; - string - networkPlayerLanguages[GameConstants::maxPlayers]; - int - networkPlayerGameStatus[GameConstants::maxPlayers]; - - ControlType - factionControls[GameConstants::maxPlayers]; - int - resourceMultiplierIndex[GameConstants::maxPlayers]; - string - networkPlayerUUID[GameConstants::maxPlayers]; - - - int - thisFactionIndex; - int - factionCount; - int - teams[GameConstants::maxPlayers]; - int - startLocationIndex[GameConstants::maxPlayers]; - int - mapFilter; - - int - fallbackCpuMultiplier; - bool - defaultUnits; - bool - defaultResources; - bool - defaultVictoryConditions; - - bool - fogOfWar; - bool - allowObservers; - bool - enableObserverModeAtEndGame; - bool - enableServerControlledAI; - int - networkFramePeriod; - bool - networkPauseGameForLaggedClients; - PathFinderType - pathFinderType; - - uint32 - flagTypes1; - - uint32 - mapCRC; - uint32 - tilesetCRC; - uint32 - techCRC; - vector < - pair < - string, - uint32 > > - factionCRCList; - - int - aiAcceptSwitchTeamPercentChance; - int - masterserver_admin; - - int - masterserver_admin_factionIndex; - - bool - networkAllowNativeLanguageTechtree; - - string - gameUUID; - - public: - - static string - playerDisconnectedText; - - GameSettings () - { - defaultUnits = false; - defaultResources = false; - defaultVictoryConditions = false; - mapFilter = 0; - factionCount = 0; - thisFactionIndex = 0; - fogOfWar = true; - allowObservers = false; - enableObserverModeAtEndGame = false; - enableServerControlledAI = false; - networkFramePeriod = GameConstants::networkFramePeriod; - networkPauseGameForLaggedClients = false; - pathFinderType = pfBasic; - - static const string - DEFAULT_LANG = "english"; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - factionTypeNames[i] = ""; - networkPlayerNames[i] = ""; - networkPlayerPlatform[i] = ""; - networkPlayerStatuses[i] = npst_None; - networkPlayerLanguages[i] = DEFAULT_LANG; - factionControls[i] = ctClosed; - resourceMultiplierIndex[i] = 1.0f; - teams[i] = 0; - startLocationIndex[i] = i; - networkPlayerGameStatus[i] = 0; - - networkPlayerUUID[i] = ""; - } - - flagTypes1 = ft1_none; - - mapCRC = 0; - tilesetCRC = 0; - techCRC = 0; - factionCRCList. - clear (); - aiAcceptSwitchTeamPercentChance = 30; - masterserver_admin = -1; - masterserver_admin_factionIndex = -1; - fallbackCpuMultiplier = 1.0f; - networkAllowNativeLanguageTechtree = true; - } - - // default copy constructor will do fine, and will maintain itself ;) - - //get - const - string & - getGameName () const - { - return - gameName; - } - const - string & - getDescription () const - { - return - description; - } - const - string & - getMap () const - { - return - map; - } - const - string & - getTileset () const - { - return - tileset; - } - const - string & - getTech () const - { - return - tech; - } - const - string & - getScenario () const - { - return - scenario; - } - const - string & - getScenarioDir () const - { - return - scenarioDir; - } - const - string & - getFactionTypeName (int factionIndex) const - { - if (factionIndex == -1) - { - static string - HEADLESS_FACTION = "headless-server"; - return - HEADLESS_FACTION; - } - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - return factionTypeNames[factionIndex]; - } - string - getNetworkPlayerName (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - string - result = networkPlayerNames[factionIndex]; - if (networkPlayerStatuses[factionIndex] == npst_Disconnected) - { - result = playerDisconnectedText + result; - } - return result; - } - string - getNetworkPlayerPlatform (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - string - result = networkPlayerPlatform[factionIndex]; - return result; - } - - const int - getNetworkPlayerStatuses (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - networkPlayerStatuses[factionIndex]; - } - const string - getNetworkPlayerLanguages (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - networkPlayerLanguages[factionIndex]; - } - - const int - getNetworkPlayerGameStatus (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - networkPlayerGameStatus[factionIndex]; - } - - const - vector < - string > - getUniqueNetworkPlayerLanguages () const - { - vector < - string > - languageList; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (networkPlayerLanguages[i] != "") - { - if (std:: - find (languageList.begin (), languageList.end (), - networkPlayerLanguages[i]) == languageList.end ()) - { - languageList.push_back (networkPlayerLanguages[i]); - } - } - } - if (languageList.empty () == true) - { - languageList.push_back (""); - } - return languageList; - } - - const string - getNetworkPlayerNameByPlayerIndex (int playerIndex) const - { - string - result = ""; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (startLocationIndex[i] == playerIndex) - { - result = networkPlayerNames[i]; - break; - } - } - return - result; - } - const string - getNetworkPlayerPlatformByPlayerIndex (int playerIndex) const - { - string - result = ""; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (startLocationIndex[i] == playerIndex) - { - result = networkPlayerPlatform[i]; - break; - } - } - return - result; - } - - ControlType - getFactionControl (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - factionControls[factionIndex]; - } - int - getResourceMultiplierIndex (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - resourceMultiplierIndex[factionIndex]; - } - - const - string & - getNetworkPlayerUUID (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - return - networkPlayerUUID[factionIndex]; - } - - bool - isNetworkGame () const - { - bool - result = false; - for (int idx = 0; idx < GameConstants::maxPlayers; ++idx) - { - if (factionControls[idx] == ctNetwork - || factionControls[idx] == ctNetworkUnassigned - || networkPlayerStatuses[idx] == npst_Disconnected) - { - result = true; - break; - } - } - return - result; - } - int - getThisFactionIndex () const - { - return - thisFactionIndex; - } - int - getFactionCount () const - { - return - factionCount; - } - int - getTeam (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - teams[factionIndex]; - } - - int - getStartLocationIndex (int factionIndex) const - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - return - startLocationIndex[factionIndex]; - } - int - getFactionIndexForStartLocation (int startIndex) const - { - if (startIndex < 0 || startIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid startIndex = %d\n", - __FUNCTION__, startIndex); - throw - megaglest_runtime_error (szBuf); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (startLocationIndex[i] == startIndex) - { - return i; - } - } - return -1; - } - - int - getMapFilter () const - { - return - mapFilter; - } - - bool - getDefaultUnits () const - { - return - defaultUnits; - } - bool - getDefaultResources () const - { - return - defaultResources; - } - bool - getDefaultVictoryConditions () const - { - return - defaultVictoryConditions; - } - - bool - getFogOfWar () const - { - return - fogOfWar; - } - bool - getAllowObservers () const - { - return - allowObservers; - } - bool - getEnableObserverModeAtEndGame () const - { - return - enableObserverModeAtEndGame; - } - bool - getEnableServerControlledAI () const - { - return - enableServerControlledAI; - } - int - getNetworkFramePeriod () const - { - return - networkFramePeriod; - } - bool - getNetworkPauseGameForLaggedClients () const - { - return - networkPauseGameForLaggedClients; - } - PathFinderType - getPathFinderType () const - { - return - pathFinderType; - } - uint32 - getFlagTypes1 () const - { - return - flagTypes1; - } - - uint32 - getMapCRC () const - { - return - mapCRC; - } - uint32 - getTilesetCRC () const - { - return - tilesetCRC; - } - uint32 - getTechCRC () const - { - return - techCRC; - } - vector < - pair < - string, - uint32 > > - getFactionCRCList () const - { - return - factionCRCList; - } - - const - string & - getGameUUID () const - { - return - gameUUID; - } - - //set - void - setGameName (const string & gameName) - { - this->gameName = gameName; - } - void - setDescription (const string & description) - { - this->description = description; - } - void - setMap (const string & map) - { - this->map = map; - } - void - setTileset (const string & tileset) - { - this->tileset = tileset; - } - void - setTech (const string & tech) - { - this->tech = tech; - } - void - setScenario (const string & scenario) - { - this->scenario = scenario; - } - void - setScenarioDir (const string & scenarioDir) - { - this->scenarioDir = scenarioDir; - } - - void - setFactionTypeName (int factionIndex, const string & factionTypeName) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->factionTypeNames[factionIndex] = factionTypeName; - } - void - setNetworkPlayerName (int factionIndex, const string & playername) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerNames[factionIndex] = playername; - } - void - setNetworkPlayerPlatform (int factionIndex, const string & platform) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerPlatform[factionIndex] = platform; - } - - void - setNetworkPlayerStatuses (int factionIndex, int status) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerStatuses[factionIndex] = status; - } - - void - setNetworkPlayerGameStatus (int factionIndex, int status) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerGameStatus[factionIndex] = status; - } - void - setNetworkPlayerLanguages (int factionIndex, const string & language) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerLanguages[factionIndex] = language; - } - - void - setFactionControl (int factionIndex, ControlType controller) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->factionControls[factionIndex] = controller; - } - void - setResourceMultiplierIndex (int factionIndex, int multiplierIndex) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - //if(multiplierIndex >= 45) { - // printf("gamesettings Line: %d multiplier index: %d factionIndex: %d\n",__LINE__,multiplierIndex,factionIndex); - //} - - this->resourceMultiplierIndex[factionIndex] = multiplierIndex; - } - - void - setNetworkPlayerUUID (int factionIndex, const string & uuid) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->networkPlayerUUID[factionIndex] = uuid; - } - - void - setThisFactionIndex (int thisFactionIndex) - { - this->thisFactionIndex = thisFactionIndex; - } - void - setFactionCount (int factionCount) - { - this->factionCount = factionCount; - } - void - setTeam (int factionIndex, int team) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->teams[factionIndex] = team; - } - void - setStartLocationIndex (int factionIndex, int startLocationIndex) - { - if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", - __FUNCTION__, factionIndex); - throw - megaglest_runtime_error (szBuf); - } - - this->startLocationIndex[factionIndex] = startLocationIndex; - } - void - setMapFilter (int mapFilter) - { - this->mapFilter = mapFilter; - } - - void - setDefaultUnits (bool defaultUnits) - { - this->defaultUnits = defaultUnits; - } - void - setDefaultResources (bool defaultResources) - { - this->defaultResources = defaultResources; - } - void - setDefaultVictoryConditions (bool defaultVictoryConditions) - { - this->defaultVictoryConditions = defaultVictoryConditions; - } - - void - setFogOfWar (bool fogOfWar) - { - this->fogOfWar = fogOfWar; - } - void - setAllowObservers (bool value) - { - this->allowObservers = value; - } - void - setEnableObserverModeAtEndGame (bool value) - { - this->enableObserverModeAtEndGame = value; - } - void - setEnableServerControlledAI (bool value) - { - this->enableServerControlledAI = value; - } - void - setNetworkFramePeriod (int value) - { - this->networkFramePeriod = value; - } - void - setNetworkPauseGameForLaggedClients (bool value) - { - this->networkPauseGameForLaggedClients = value; - } - void - setPathFinderType (PathFinderType value) - { - this->pathFinderType = value; - } - - void - setFlagTypes1 (uint32 value) - { - this->flagTypes1 = value; - } - - void - setMapCRC (uint32 value) - { - mapCRC = value; - } - void - setTilesetCRC (uint32 value) - { - tilesetCRC = value; - } - void - setTechCRC (uint32 value) - { - techCRC = value; - } - - void - setFactionCRCList (const vector < pair < string, uint32 > >&value) - { - factionCRCList = value; - } - - int - getAiAcceptSwitchTeamPercentChance () const - { - return - aiAcceptSwitchTeamPercentChance; - } - void - setAiAcceptSwitchTeamPercentChance (int value) - { - aiAcceptSwitchTeamPercentChance = value; - } - - int - getFallbackCpuMultiplier () const - { - return - fallbackCpuMultiplier; - } - void - setFallbackCpuMultiplier (int value) - { - fallbackCpuMultiplier = value; - } - - int - getMasterserver_admin () const - { - return - masterserver_admin; - } - void - setMasterserver_admin (int value) - { - masterserver_admin = value; - } - - int - getMasterserver_admin_faction_index () const - { - return - masterserver_admin_factionIndex; - } - void - setMasterserver_admin_faction_index (int value) - { - masterserver_admin_factionIndex = value; - } - - bool - getNetworkAllowNativeLanguageTechtree () const - { - return - networkAllowNativeLanguageTechtree; - } - void - setNetworkAllowNativeLanguageTechtree (bool value) - { - networkAllowNativeLanguageTechtree = value; - } - - void - setGameUUID (const string & gameUUID) - { - this->gameUUID = gameUUID; - } - - string - toString () const - { - string - result = ""; - - result += - "Game ID = " + - gameUUID + - "\n"; - result += - "gameName = " + - gameName + - "\n"; - result += - "description = " + - description + - "\n"; - result += - "mapFilterIndex = " + - intToStr (mapFilter) + - "\n"; - result += - "map = " + - map + - "\n"; - result += - "tileset = " + - tileset + - "\n"; - result += - "tech = " + - tech + - "\n"; - result += - "scenario = " + - scenario + - "\n"; - result += - "scenarioDir = " + - scenarioDir + - "\n"; - - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - result += "player index = " + intToStr (idx) + "\n"; - result += - "factionTypeName = " + - factionTypeNames[idx] + - "\n"; - result += - "networkPlayerName = " + - networkPlayerNames[idx] + - "\n"; - result += - "networkPlayerPlatform = " + - networkPlayerPlatform[idx] + - "\n"; - result += - "networkPlayerLanguage = " + - networkPlayerLanguages[idx] + - "\n"; - - result += - "factionControl = " + - intToStr (factionControls[idx]) + - "\n"; - result += - "resourceMultiplierIndex = " + - intToStr (resourceMultiplierIndex[idx]) + - "\n"; - result += - "team = " + - intToStr (teams[idx]) + - "\n"; - result += - "startLocationIndex = " + - intToStr (startLocationIndex[idx]) + - "\n"; - result += - "networkPlayerUUID = " + - networkPlayerUUID[idx] + - "\n"; - } - - result += - "thisFactionIndex = " + - intToStr (thisFactionIndex) + - "\n"; - result += "factionCount = " + intToStr (factionCount) + "\n"; - result += "defaultUnits = " + intToStr (defaultUnits) + "\n"; - result += "defaultResources = " + intToStr (defaultResources) + "\n"; - result += - "defaultVictoryConditions = " + - intToStr (defaultVictoryConditions) + "\n"; - result += "fogOfWar = " + intToStr (fogOfWar) + "\n"; - result += "allowObservers = " + intToStr (allowObservers) + "\n"; - result += - "enableObserverModeAtEndGame = " + - intToStr (enableObserverModeAtEndGame) + "\n"; - result += - "enableServerControlledAI = " + - intToStr (enableServerControlledAI) + "\n"; - result += - "networkFramePeriod = " + intToStr (networkFramePeriod) + "\n"; - result += - "networkPauseGameForLaggedClients = " + - intToStr (networkPauseGameForLaggedClients) + "\n"; - result += "pathFinderType = " + intToStr (pathFinderType) + "\n"; - result += "flagTypes1 = " + uIntToStr (flagTypes1) + "\n"; - result += "mapCRC = " + uIntToStr (mapCRC) + "\n"; - result += "tilesetCRC = " + uIntToStr (tilesetCRC) + "\n"; - result += "techCRC = " + uIntToStr (techCRC) + "\n"; - - for (unsigned int i = 0; i < factionCRCList.size (); ++i) - { - result += - "factionCRCList name [" + factionCRCList[i].first + "] CRC = " + - uIntToStr (factionCRCList[i].second) + "\n"; - } - - result += - "aiAcceptSwitchTeamPercentChance = " + - intToStr (aiAcceptSwitchTeamPercentChance) + "\n"; - result += - "masterserver_admin = " + intToStr (masterserver_admin) + "\n"; - result += - "masterserver_admin_factionIndex = " + - intToStr (masterserver_admin_factionIndex) + "\n"; - - result += - "networkAllowNativeLanguageTechtree = " + - intToStr (networkAllowNativeLanguageTechtree) + "\n"; - return result; - } - - void - saveGame (XmlNode * rootNode) const - { - std::map < - string, - string > - mapTagReplacements; - XmlNode * - gameSettingsNode = rootNode->addChild ("GameSettings"); - - gameSettingsNode-> - addAttribute ("gameUUID", gameUUID, mapTagReplacements); - -// string gameName; - gameSettingsNode-> - addAttribute ("gameName", gameName, mapTagReplacements); -// string description; - gameSettingsNode-> - addAttribute ("description", description, mapTagReplacements); -// string map; - gameSettingsNode-> - addAttribute ("map", map, mapTagReplacements); -// string tileset; - gameSettingsNode-> - addAttribute ("tileset", tileset, mapTagReplacements); -// string tech; - gameSettingsNode-> - addAttribute ("tech", tech, mapTagReplacements); -// string scenario; - gameSettingsNode-> - addAttribute ("scenario", scenario, mapTagReplacements); -// string scenarioDir; - gameSettingsNode-> - addAttribute ("scenarioDir", scenarioDir, mapTagReplacements); -// string factionTypeNames[GameConstants::maxPlayers]; //faction names - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - factionTypeNamesNode = - gameSettingsNode->addChild ("factionTypeNames"); - factionTypeNamesNode-> - addAttribute ("name", factionTypeNames[idx], mapTagReplacements); - } - -// string networkPlayerNames[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerNamesNode = - gameSettingsNode->addChild ("networkPlayerNames"); - networkPlayerNamesNode->addAttribute ("name", - networkPlayerNames[idx], - mapTagReplacements); - } - - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerNamesNode = - gameSettingsNode->addChild ("networkPlayerPlatform"); - networkPlayerNamesNode->addAttribute ("name", - networkPlayerPlatform[idx], - mapTagReplacements); - } - -// int networkPlayerStatuses[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerStatusesNode = - gameSettingsNode->addChild ("networkPlayerStatuses"); - networkPlayerStatusesNode->addAttribute ("status", - intToStr - (networkPlayerStatuses - [idx]), - mapTagReplacements); - } - - // int networkPlayerStatuses[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerStatusesNode = - gameSettingsNode->addChild ("networkPlayerGameStatus"); - networkPlayerStatusesNode->addAttribute ("game_status", - intToStr - (networkPlayerGameStatus - [idx]), - mapTagReplacements); - } - -// string networkPlayerLanguages[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerLanguagesNode = - gameSettingsNode->addChild ("networkPlayerLanguages"); - networkPlayerLanguagesNode->addAttribute ("name", - networkPlayerLanguages - [idx], - mapTagReplacements); - } - -// ControlType factionControls[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - factionControlsNode = - gameSettingsNode->addChild ("factionControls"); - factionControlsNode->addAttribute ("control", - intToStr (factionControls - [idx]), - mapTagReplacements); - } - -// int resourceMultiplierIndex[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - resourceMultiplierIndexNode = - gameSettingsNode->addChild ("resourceMultiplierIndex"); - resourceMultiplierIndexNode->addAttribute ("multiplier", - intToStr - (resourceMultiplierIndex - [idx]), - mapTagReplacements); - } - -// int thisFactionIndex; - gameSettingsNode->addAttribute ("thisFactionIndex", - intToStr (thisFactionIndex), - mapTagReplacements); -// int factionCount; - gameSettingsNode->addAttribute ("factionCount", - intToStr (factionCount), - mapTagReplacements); -// int teams[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - teamsNode = gameSettingsNode->addChild ("teams"); - teamsNode->addAttribute ("team", intToStr (teams[idx]), - mapTagReplacements); - } - -// int startLocationIndex[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - startLocationIndexNode = - gameSettingsNode->addChild ("startLocationIndex"); - startLocationIndexNode->addAttribute ("location", - intToStr (startLocationIndex - [idx]), - mapTagReplacements); - } - - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - XmlNode * - networkPlayerUUIDNode = - gameSettingsNode->addChild ("networkPlayerUUID"); - networkPlayerUUIDNode->addAttribute ("value", - networkPlayerUUID[idx], - mapTagReplacements); - } - -// int mapFilterIndex; - gameSettingsNode->addAttribute ("mapFilterIndex", - intToStr (mapFilter), - mapTagReplacements); -// -// -// bool defaultUnits; - gameSettingsNode->addAttribute ("defaultUnits", - intToStr (defaultUnits), - mapTagReplacements); -// bool defaultResources; - gameSettingsNode->addAttribute ("defaultResources", - intToStr (defaultResources), - mapTagReplacements); -// bool defaultVictoryConditions; - gameSettingsNode->addAttribute ("defaultVictoryConditions", - intToStr (defaultVictoryConditions), - mapTagReplacements); -// bool fogOfWar; - gameSettingsNode->addAttribute ("fogOfWar", intToStr (fogOfWar), - mapTagReplacements); -// bool allowObservers; - gameSettingsNode->addAttribute ("allowObservers", - intToStr (allowObservers), - mapTagReplacements); -// bool enableObserverModeAtEndGame; - gameSettingsNode->addAttribute ("enableObserverModeAtEndGame", - intToStr - (enableObserverModeAtEndGame), - mapTagReplacements); -// bool enableServerControlledAI; - gameSettingsNode->addAttribute ("enableServerControlledAI", - intToStr (enableServerControlledAI), - mapTagReplacements); -// int networkFramePeriod; - gameSettingsNode->addAttribute ("networkFramePeriod", - intToStr (networkFramePeriod), - mapTagReplacements); -// bool networkPauseGameForLaggedClients; - gameSettingsNode->addAttribute ("networkPauseGameForLaggedClients", - intToStr - (networkPauseGameForLaggedClients), - mapTagReplacements); -// PathFinderType pathFinderType; - gameSettingsNode->addAttribute ("pathFinderType", - intToStr (pathFinderType), - mapTagReplacements); -// uint32 flagTypes1; - gameSettingsNode->addAttribute ("flagTypes1", uIntToStr (flagTypes1), - mapTagReplacements); -// int32 mapCRC; - gameSettingsNode->addAttribute ("mapCRC", uIntToStr (mapCRC), - mapTagReplacements); -// int32 tilesetCRC; - gameSettingsNode->addAttribute ("tilesetCRC", uIntToStr (tilesetCRC), - mapTagReplacements); -// int32 techCRC; - gameSettingsNode->addAttribute ("techCRC", uIntToStr (techCRC), - mapTagReplacements); -// vector > factionCRCList; - for (unsigned int i = 0; i < factionCRCList.size (); ++i) - { - const - pair < - string, - uint32 > & - item = factionCRCList[i]; - - XmlNode * - factionCRCListNode = - gameSettingsNode->addChild ("factionCRCList"); - factionCRCListNode->addAttribute ("key", item.first, - mapTagReplacements); - factionCRCListNode->addAttribute ("value", - uIntToStr (item.second), - mapTagReplacements); - } -// int aiAcceptSwitchTeamPercentChance; - gameSettingsNode->addAttribute ("aiAcceptSwitchTeamPercentChance", - intToStr - (aiAcceptSwitchTeamPercentChance), - mapTagReplacements); -// int masterserver_admin; - gameSettingsNode->addAttribute ("masterserver_admin", - intToStr (masterserver_admin), - mapTagReplacements); - - gameSettingsNode->addAttribute ("masterserver_admin_factionIndex", - intToStr - (masterserver_admin_factionIndex), - mapTagReplacements); - - gameSettingsNode->addAttribute ("networkAllowNativeLanguageTechtree", - intToStr - (networkAllowNativeLanguageTechtree), - mapTagReplacements); - } - - void - loadGame (const XmlNode * rootNode) - { - const XmlNode * - gameSettingsNode = rootNode->getChild ("GameSettings"); - - if (gameSettingsNode->hasAttribute ("gameUUID") == true) - { - gameUUID = - gameSettingsNode->getAttribute ("gameUUID")->getValue (); - } - -// string gameName; - if (gameSettingsNode->hasAttribute ("gameName") == true) - { - gameName = - gameSettingsNode->getAttribute ("gameName")->getValue (); - } - else - { - gameName = "oldSavegame"; - } -// string description; - description = - gameSettingsNode->getAttribute ("description")->getValue (); -// string map; - map = gameSettingsNode->getAttribute ("map")->getValue (); -// string tileset; - tileset = gameSettingsNode->getAttribute ("tileset")->getValue (); -// string tech; - tech = gameSettingsNode->getAttribute ("tech")->getValue (); -// string scenario; - scenario = gameSettingsNode->getAttribute ("scenario")->getValue (); -// string scenarioDir; - scenarioDir = - gameSettingsNode->getAttribute ("scenarioDir")->getValue (); - if (fileExists (scenarioDir) == false) - { - scenarioDir = Config::findValidLocalFileFromPath (scenarioDir); - } - -// string factionTypeNames[GameConstants::maxPlayers]; //faction names - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - factionTypeNamesNode = - gameSettingsNode->getChild ("factionTypeNames", idx); - factionTypeNames[idx] = - factionTypeNamesNode->getAttribute ("name")->getValue (); - } - -// string networkPlayerNames[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - networkPlayerNamesNode = - gameSettingsNode->getChild ("networkPlayerNames", idx); - networkPlayerNames[idx] = - networkPlayerNamesNode->getAttribute ("name")->getValue (); - } - -// int networkPlayerStatuses[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - networkPlayerStatusesNode = - gameSettingsNode->getChild ("networkPlayerStatuses", idx); - networkPlayerStatuses[idx] = - networkPlayerStatusesNode->getAttribute ("status")-> - getIntValue (); - } - - // int networkPlayerStatuses[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - if (gameSettingsNode-> - hasChildAtIndex ("networkPlayerGameStatus", idx) == true) - { - const XmlNode * - networkPlayerGameStatusNode = - gameSettingsNode->getChild ("networkPlayerGameStatus", idx); - networkPlayerGameStatus[idx] = - networkPlayerGameStatusNode->getAttribute ("game_status")-> - getIntValue (); - } - } - -// string networkPlayerLanguages[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - networkPlayerLanguagesNode = - gameSettingsNode->getChild ("networkPlayerLanguages", idx); - networkPlayerLanguages[idx] = - networkPlayerLanguagesNode->getAttribute ("name")->getValue (); - } - -// ControlType factionControls[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - factionControlsNode = - gameSettingsNode->getChild ("factionControls", idx); - factionControls[idx] = - static_cast < ControlType > - (factionControlsNode->getAttribute ("control")->getIntValue ()); - } - -// int resourceMultiplierIndex[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - resourceMultiplierIndexNode = - gameSettingsNode->getChild ("resourceMultiplierIndex", idx); - resourceMultiplierIndex[idx] = - resourceMultiplierIndexNode->getAttribute ("multiplier")-> - getIntValue (); - } - - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - if (gameSettingsNode-> - hasChildAtIndex ("networkPlayerUUID", idx) == true) - { - const XmlNode * - networkPlayerUUIDNode = - gameSettingsNode->getChild ("networkPlayerUUID", idx); - networkPlayerUUID[idx] = - networkPlayerUUIDNode->getAttribute ("value")->getValue (); - } - } - -// int thisFactionIndex; - thisFactionIndex = - gameSettingsNode->getAttribute ("thisFactionIndex")->getIntValue (); -// int factionCount; - factionCount = - gameSettingsNode->getAttribute ("factionCount")->getIntValue (); -// int teams[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - teamsNode = gameSettingsNode->getChild ("teams", idx); - teams[idx] = teamsNode->getAttribute ("team")->getIntValue (); - } - -// int startLocationIndex[GameConstants::maxPlayers]; - for (int idx = 0; idx < GameConstants::maxPlayers; idx++) - { - const XmlNode * - startLocationIndexNode = - gameSettingsNode->getChild ("startLocationIndex", idx); - startLocationIndex[idx] = - startLocationIndexNode->getAttribute ("location")-> - getIntValue (); - } - -// int mapFilterIndex; - mapFilter = - gameSettingsNode->getAttribute ("mapFilterIndex")->getIntValue (); -// -// -// bool defaultUnits; - defaultUnits = - gameSettingsNode->getAttribute ("defaultUnits")->getIntValue () != - 0; -// bool defaultResources; - defaultResources = - gameSettingsNode->getAttribute ("defaultResources")-> - getIntValue () != 0; -// bool defaultVictoryConditions; - defaultVictoryConditions = - gameSettingsNode->getAttribute ("defaultVictoryConditions")-> - getIntValue () != 0; -// bool fogOfWar; - fogOfWar = - gameSettingsNode->getAttribute ("fogOfWar")->getIntValue () != 0; -// bool allowObservers; - allowObservers = - gameSettingsNode->getAttribute ("allowObservers")->getIntValue () != - 0; -// bool enableObserverModeAtEndGame; - enableObserverModeAtEndGame = - gameSettingsNode->getAttribute ("enableObserverModeAtEndGame")-> - getIntValue () != 0; -// bool enableServerControlledAI; - enableServerControlledAI = - gameSettingsNode->getAttribute ("enableServerControlledAI")-> - getIntValue () != 0; -// int networkFramePeriod; - networkFramePeriod = - gameSettingsNode->getAttribute ("networkFramePeriod")-> - getIntValue (); -// bool networkPauseGameForLaggedClients; - networkPauseGameForLaggedClients = - gameSettingsNode-> - getAttribute ("networkPauseGameForLaggedClients")->getIntValue () != - 0; -// PathFinderType pathFinderType; - pathFinderType = - static_cast < PathFinderType > - (gameSettingsNode->getAttribute ("pathFinderType")->getIntValue ()); -// uint32 flagTypes1; - flagTypes1 = - gameSettingsNode->getAttribute ("flagTypes1")->getUIntValue (); -// int32 mapCRC; - mapCRC = gameSettingsNode->getAttribute ("mapCRC")->getUIntValue (); -// int32 tilesetCRC; - tilesetCRC = - gameSettingsNode->getAttribute ("tilesetCRC")->getUIntValue (); -// int32 techCRC; - techCRC = gameSettingsNode->getAttribute ("techCRC")->getUIntValue (); -// vector > factionCRCList; -// for(unsigned int i = 0; i < factionCRCList.size(); ++i) { -// const pair &item = factionCRCList[i]; -// -// XmlNode *factionCRCListNode = gameSettingsNode->addChild("factionCRCList"); -// factionCRCListNode->addAttribute("key",item.first, mapTagReplacements); -// factionCRCListNode->addAttribute("value",intToStr(item.second), mapTagReplacements); -// } -// int aiAcceptSwitchTeamPercentChance; - aiAcceptSwitchTeamPercentChance = - gameSettingsNode->getAttribute ("aiAcceptSwitchTeamPercentChance")-> - getIntValue (); -// int masterserver_admin; - masterserver_admin = - gameSettingsNode->getAttribute ("masterserver_admin")-> - getIntValue (); - - if (gameSettingsNode-> - hasAttribute ("masterserver_admin_factionIndex") == true) - { - masterserver_admin_factionIndex = - gameSettingsNode-> - getAttribute ("masterserver_admin_factionIndex")-> - getIntValue (); - } - - if (gameSettingsNode-> - hasAttribute ("networkAllowNativeLanguageTechtree") == true) - { - networkAllowNativeLanguageTechtree = - gameSettingsNode-> - getAttribute ("networkAllowNativeLanguageTechtree")-> - getIntValue () != 0; - } - } - - }; - - } + Glest { + namespace + Game { + + enum ModType { + mt_None, + mt_Map, + mt_Tileset, + mt_Techtree, + mt_Scenario + }; + + // =============================== + // class ModInfo + // =============================== + + class + ModInfo { + public: + string + name; + string + url; + string + imageUrl; + string + description; + string + count; // used for faction count for example + string + crc; + string + localCRC; + ModType + type; + public: + ModInfo(); + }; + + // ===================================================== + // class GameSettings + // ===================================================== + + enum FlagTypes1 { + ft1_none = 0x00, + ft1_show_map_resources = 0x01, + ft1_allow_team_switching = 0x02, + ft1_allow_in_game_joining = 0x04, + ft1_network_synch_checks_verbose = 0x08, + ft1_network_synch_checks = 0x10, + ft1_allow_shared_team_units = 0x20, + ft1_allow_shared_team_resources = 0x40 + //ft1_xxx = 0x80 + }; + + inline static bool + isFlagType1BitEnabled(uint32 flagValue, FlagTypes1 type) { + return ((flagValue & (uint32) type) == (uint32) type); + } + + enum NetworkPlayerStatusType { + npst_None = 0, + npst_PickSettings = 1, + npst_BeRightBack = 2, + npst_Ready = 3, + npst_Disconnected = 4 + }; + + class + GameSettings { + private: + string + gameName; + string + description; + string + map; + string + tileset; + string + tech; + string + scenario; + string + scenarioDir; + string + factionTypeNames[GameConstants::maxPlayers]; //faction names + string + networkPlayerNames[GameConstants::maxPlayers]; + string + networkPlayerPlatform[GameConstants::maxPlayers]; + int + networkPlayerStatuses[GameConstants::maxPlayers]; + string + networkPlayerLanguages[GameConstants::maxPlayers]; + int + networkPlayerGameStatus[GameConstants::maxPlayers]; + + ControlType + factionControls[GameConstants::maxPlayers]; + int + resourceMultiplierIndex[GameConstants::maxPlayers]; + string + networkPlayerUUID[GameConstants::maxPlayers]; + + + int + thisFactionIndex; + int + factionCount; + int + teams[GameConstants::maxPlayers]; + int + startLocationIndex[GameConstants::maxPlayers]; + int + mapFilter; + + int + fallbackCpuMultiplier; + bool + defaultUnits; + bool + defaultResources; + bool + defaultVictoryConditions; + + bool + fogOfWar; + bool + allowObservers; + bool + enableObserverModeAtEndGame; + bool + enableServerControlledAI; + int + networkFramePeriod; + bool + networkPauseGameForLaggedClients; + PathFinderType + pathFinderType; + + uint32 + flagTypes1; + + uint32 + mapCRC; + uint32 + tilesetCRC; + uint32 + techCRC; + vector < + pair < + string, + uint32 > > + factionCRCList; + + int + aiAcceptSwitchTeamPercentChance; + int + masterserver_admin; + + int + masterserver_admin_factionIndex; + + bool + networkAllowNativeLanguageTechtree; + + string + gameUUID; + + public: + + static string + playerDisconnectedText; + + GameSettings() { + defaultUnits = false; + defaultResources = false; + defaultVictoryConditions = false; + mapFilter = 0; + factionCount = 0; + thisFactionIndex = 0; + fogOfWar = true; + allowObservers = false; + enableObserverModeAtEndGame = false; + enableServerControlledAI = false; + networkFramePeriod = GameConstants::networkFramePeriod; + networkPauseGameForLaggedClients = false; + pathFinderType = pfBasic; + + static const string + DEFAULT_LANG = "english"; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + factionTypeNames[i] = ""; + networkPlayerNames[i] = ""; + networkPlayerPlatform[i] = ""; + networkPlayerStatuses[i] = npst_None; + networkPlayerLanguages[i] = DEFAULT_LANG; + factionControls[i] = ctClosed; + resourceMultiplierIndex[i] = 1.0f; + teams[i] = 0; + startLocationIndex[i] = i; + networkPlayerGameStatus[i] = 0; + + networkPlayerUUID[i] = ""; + } + + flagTypes1 = ft1_none; + + mapCRC = 0; + tilesetCRC = 0; + techCRC = 0; + factionCRCList. + clear(); + aiAcceptSwitchTeamPercentChance = 30; + masterserver_admin = -1; + masterserver_admin_factionIndex = -1; + fallbackCpuMultiplier = 1.0f; + networkAllowNativeLanguageTechtree = true; + } + + // default copy constructor will do fine, and will maintain itself ;) + + //get + const + string & + getGameName() const { + return + gameName; + } + const + string & + getDescription() const { + return + description; + } + const + string & + getMap() const { + return + map; + } + const + string & + getTileset() const { + return + tileset; + } + const + string & + getTech() const { + return + tech; + } + const + string & + getScenario() const { + return + scenario; + } + const + string & + getScenarioDir() const { + return + scenarioDir; + } + const + string & + getFactionTypeName(int factionIndex) const { + if (factionIndex == -1) { + static string + HEADLESS_FACTION = "headless-server"; + return + HEADLESS_FACTION; + } + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + return factionTypeNames[factionIndex]; + } + string + getNetworkPlayerName(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + string + result = networkPlayerNames[factionIndex]; + if (networkPlayerStatuses[factionIndex] == npst_Disconnected) { + result = playerDisconnectedText + result; + } + return result; + } + string + getNetworkPlayerPlatform(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + string + result = networkPlayerPlatform[factionIndex]; + return result; + } + + const int + getNetworkPlayerStatuses(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + networkPlayerStatuses[factionIndex]; + } + const string + getNetworkPlayerLanguages(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + networkPlayerLanguages[factionIndex]; + } + + const int + getNetworkPlayerGameStatus(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + networkPlayerGameStatus[factionIndex]; + } + + const + vector < + string > + getUniqueNetworkPlayerLanguages() const { + vector < + string > + languageList; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (networkPlayerLanguages[i] != "") { + if (std:: + find(languageList.begin(), languageList.end(), + networkPlayerLanguages[i]) == languageList.end()) { + languageList.push_back(networkPlayerLanguages[i]); + } + } + } + if (languageList.empty() == true) { + languageList.push_back(""); + } + return languageList; + } + + const string + getNetworkPlayerNameByPlayerIndex(int playerIndex) const { + string + result = ""; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (startLocationIndex[i] == playerIndex) { + result = networkPlayerNames[i]; + break; + } + } + return + result; + } + const string + getNetworkPlayerPlatformByPlayerIndex(int playerIndex) const { + string + result = ""; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (startLocationIndex[i] == playerIndex) { + result = networkPlayerPlatform[i]; + break; + } + } + return + result; + } + + ControlType + getFactionControl(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + factionControls[factionIndex]; + } + int + getResourceMultiplierIndex(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + resourceMultiplierIndex[factionIndex]; + } + + const + string & + getNetworkPlayerUUID(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + return + networkPlayerUUID[factionIndex]; + } + + bool + isNetworkGame() const { + bool + result = false; + for (int idx = 0; idx < GameConstants::maxPlayers; ++idx) { + if (factionControls[idx] == ctNetwork + || factionControls[idx] == ctNetworkUnassigned + || networkPlayerStatuses[idx] == npst_Disconnected) { + result = true; + break; + } + } + return + result; + } + int + getThisFactionIndex() const { + return + thisFactionIndex; + } + int + getFactionCount() const { + return + factionCount; + } + int + getTeam(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + teams[factionIndex]; + } + + int + getStartLocationIndex(int factionIndex) const { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + return + startLocationIndex[factionIndex]; + } + int + getFactionIndexForStartLocation(int startIndex) const { + if (startIndex < 0 || startIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid startIndex = %d\n", + __FUNCTION__, startIndex); + throw + megaglest_runtime_error(szBuf); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (startLocationIndex[i] == startIndex) { + return i; + } + } + return -1; + } + + int + getMapFilter() const { + return + mapFilter; + } + + bool + getDefaultUnits() const { + return + defaultUnits; + } + bool + getDefaultResources() const { + return + defaultResources; + } + bool + getDefaultVictoryConditions() const { + return + defaultVictoryConditions; + } + + bool + getFogOfWar() const { + return + fogOfWar; + } + bool + getAllowObservers() const { + return + allowObservers; + } + bool + getEnableObserverModeAtEndGame() const { + return + enableObserverModeAtEndGame; + } + bool + getEnableServerControlledAI() const { + return + enableServerControlledAI; + } + int + getNetworkFramePeriod() const { + return + networkFramePeriod; + } + bool + getNetworkPauseGameForLaggedClients() const { + return + networkPauseGameForLaggedClients; + } + PathFinderType + getPathFinderType() const { + return + pathFinderType; + } + uint32 + getFlagTypes1() const { + return + flagTypes1; + } + + uint32 + getMapCRC() const { + return + mapCRC; + } + uint32 + getTilesetCRC() const { + return + tilesetCRC; + } + uint32 + getTechCRC() const { + return + techCRC; + } + vector < + pair < + string, + uint32 > > + getFactionCRCList() const { + return + factionCRCList; + } + + const + string & + getGameUUID() const { + return + gameUUID; + } + + //set + void + setGameName(const string & gameName) { + this->gameName = gameName; + } + void + setDescription(const string & description) { + this->description = description; + } + void + setMap(const string & map) { + this->map = map; + } + void + setTileset(const string & tileset) { + this->tileset = tileset; + } + void + setTech(const string & tech) { + this->tech = tech; + } + void + setScenario(const string & scenario) { + this->scenario = scenario; + } + void + setScenarioDir(const string & scenarioDir) { + this->scenarioDir = scenarioDir; + } + + void + setFactionTypeName(int factionIndex, const string & factionTypeName) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->factionTypeNames[factionIndex] = factionTypeName; + } + void + setNetworkPlayerName(int factionIndex, const string & playername) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerNames[factionIndex] = playername; + } + void + setNetworkPlayerPlatform(int factionIndex, const string & platform) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerPlatform[factionIndex] = platform; + } + + void + setNetworkPlayerStatuses(int factionIndex, int status) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerStatuses[factionIndex] = status; + } + + void + setNetworkPlayerGameStatus(int factionIndex, int status) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerGameStatus[factionIndex] = status; + } + void + setNetworkPlayerLanguages(int factionIndex, const string & language) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerLanguages[factionIndex] = language; + } + + void + setFactionControl(int factionIndex, ControlType controller) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->factionControls[factionIndex] = controller; + } + void + setResourceMultiplierIndex(int factionIndex, int multiplierIndex) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + //if(multiplierIndex >= 45) { + // printf("gamesettings Line: %d multiplier index: %d factionIndex: %d\n",__LINE__,multiplierIndex,factionIndex); + //} + + this->resourceMultiplierIndex[factionIndex] = multiplierIndex; + } + + void + setNetworkPlayerUUID(int factionIndex, const string & uuid) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->networkPlayerUUID[factionIndex] = uuid; + } + + void + setThisFactionIndex(int thisFactionIndex) { + this->thisFactionIndex = thisFactionIndex; + } + void + setFactionCount(int factionCount) { + this->factionCount = factionCount; + } + void + setTeam(int factionIndex, int team) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->teams[factionIndex] = team; + } + void + setStartLocationIndex(int factionIndex, int startLocationIndex) { + if (factionIndex < 0 || factionIndex >= GameConstants::maxPlayers) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s] Invalid factionIndex = %d\n", + __FUNCTION__, factionIndex); + throw + megaglest_runtime_error(szBuf); + } + + this->startLocationIndex[factionIndex] = startLocationIndex; + } + void + setMapFilter(int mapFilter) { + this->mapFilter = mapFilter; + } + + void + setDefaultUnits(bool defaultUnits) { + this->defaultUnits = defaultUnits; + } + void + setDefaultResources(bool defaultResources) { + this->defaultResources = defaultResources; + } + void + setDefaultVictoryConditions(bool defaultVictoryConditions) { + this->defaultVictoryConditions = defaultVictoryConditions; + } + + void + setFogOfWar(bool fogOfWar) { + this->fogOfWar = fogOfWar; + } + void + setAllowObservers(bool value) { + this->allowObservers = value; + } + void + setEnableObserverModeAtEndGame(bool value) { + this->enableObserverModeAtEndGame = value; + } + void + setEnableServerControlledAI(bool value) { + this->enableServerControlledAI = value; + } + void + setNetworkFramePeriod(int value) { + this->networkFramePeriod = value; + } + void + setNetworkPauseGameForLaggedClients(bool value) { + this->networkPauseGameForLaggedClients = value; + } + void + setPathFinderType(PathFinderType value) { + this->pathFinderType = value; + } + + void + setFlagTypes1(uint32 value) { + this->flagTypes1 = value; + } + + void + setMapCRC(uint32 value) { + mapCRC = value; + } + void + setTilesetCRC(uint32 value) { + tilesetCRC = value; + } + void + setTechCRC(uint32 value) { + techCRC = value; + } + + void + setFactionCRCList(const vector < pair < string, uint32 > >&value) { + factionCRCList = value; + } + + int + getAiAcceptSwitchTeamPercentChance() const { + return + aiAcceptSwitchTeamPercentChance; + } + void + setAiAcceptSwitchTeamPercentChance(int value) { + aiAcceptSwitchTeamPercentChance = value; + } + + int + getFallbackCpuMultiplier() const { + return + fallbackCpuMultiplier; + } + void + setFallbackCpuMultiplier(int value) { + fallbackCpuMultiplier = value; + } + + int + getMasterserver_admin() const { + return + masterserver_admin; + } + void + setMasterserver_admin(int value) { + masterserver_admin = value; + } + + int + getMasterserver_admin_faction_index() const { + return + masterserver_admin_factionIndex; + } + void + setMasterserver_admin_faction_index(int value) { + masterserver_admin_factionIndex = value; + } + + bool + getNetworkAllowNativeLanguageTechtree() const { + return + networkAllowNativeLanguageTechtree; + } + void + setNetworkAllowNativeLanguageTechtree(bool value) { + networkAllowNativeLanguageTechtree = value; + } + + void + setGameUUID(const string & gameUUID) { + this->gameUUID = gameUUID; + } + + string + toString() const { + string + result = ""; + + result += + "Game ID = " + + gameUUID + + "\n"; + result += + "gameName = " + + gameName + + "\n"; + result += + "description = " + + description + + "\n"; + result += + "mapFilterIndex = " + + intToStr(mapFilter) + + "\n"; + result += + "map = " + + map + + "\n"; + result += + "tileset = " + + tileset + + "\n"; + result += + "tech = " + + tech + + "\n"; + result += + "scenario = " + + scenario + + "\n"; + result += + "scenarioDir = " + + scenarioDir + + "\n"; + + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + result += "player index = " + intToStr(idx) + "\n"; + result += + "factionTypeName = " + + factionTypeNames[idx] + + "\n"; + result += + "networkPlayerName = " + + networkPlayerNames[idx] + + "\n"; + result += + "networkPlayerPlatform = " + + networkPlayerPlatform[idx] + + "\n"; + result += + "networkPlayerLanguage = " + + networkPlayerLanguages[idx] + + "\n"; + + result += + "factionControl = " + + intToStr(factionControls[idx]) + + "\n"; + result += + "resourceMultiplierIndex = " + + intToStr(resourceMultiplierIndex[idx]) + + "\n"; + result += + "team = " + + intToStr(teams[idx]) + + "\n"; + result += + "startLocationIndex = " + + intToStr(startLocationIndex[idx]) + + "\n"; + result += + "networkPlayerUUID = " + + networkPlayerUUID[idx] + + "\n"; + } + + result += + "thisFactionIndex = " + + intToStr(thisFactionIndex) + + "\n"; + result += "factionCount = " + intToStr(factionCount) + "\n"; + result += "defaultUnits = " + intToStr(defaultUnits) + "\n"; + result += "defaultResources = " + intToStr(defaultResources) + "\n"; + result += + "defaultVictoryConditions = " + + intToStr(defaultVictoryConditions) + "\n"; + result += "fogOfWar = " + intToStr(fogOfWar) + "\n"; + result += "allowObservers = " + intToStr(allowObservers) + "\n"; + result += + "enableObserverModeAtEndGame = " + + intToStr(enableObserverModeAtEndGame) + "\n"; + result += + "enableServerControlledAI = " + + intToStr(enableServerControlledAI) + "\n"; + result += + "networkFramePeriod = " + intToStr(networkFramePeriod) + "\n"; + result += + "networkPauseGameForLaggedClients = " + + intToStr(networkPauseGameForLaggedClients) + "\n"; + result += "pathFinderType = " + intToStr(pathFinderType) + "\n"; + result += "flagTypes1 = " + uIntToStr(flagTypes1) + "\n"; + result += "mapCRC = " + uIntToStr(mapCRC) + "\n"; + result += "tilesetCRC = " + uIntToStr(tilesetCRC) + "\n"; + result += "techCRC = " + uIntToStr(techCRC) + "\n"; + + for (unsigned int i = 0; i < factionCRCList.size(); ++i) { + result += + "factionCRCList name [" + factionCRCList[i].first + "] CRC = " + + uIntToStr(factionCRCList[i].second) + "\n"; + } + + result += + "aiAcceptSwitchTeamPercentChance = " + + intToStr(aiAcceptSwitchTeamPercentChance) + "\n"; + result += + "masterserver_admin = " + intToStr(masterserver_admin) + "\n"; + result += + "masterserver_admin_factionIndex = " + + intToStr(masterserver_admin_factionIndex) + "\n"; + + result += + "networkAllowNativeLanguageTechtree = " + + intToStr(networkAllowNativeLanguageTechtree) + "\n"; + return result; + } + + void + saveGame(XmlNode * rootNode) const { + std::map < + string, + string > + mapTagReplacements; + XmlNode * + gameSettingsNode = rootNode->addChild("GameSettings"); + + gameSettingsNode-> + addAttribute("gameUUID", gameUUID, mapTagReplacements); + + // string gameName; + gameSettingsNode-> + addAttribute("gameName", gameName, mapTagReplacements); + // string description; + gameSettingsNode-> + addAttribute("description", description, mapTagReplacements); + // string map; + gameSettingsNode-> + addAttribute("map", map, mapTagReplacements); + // string tileset; + gameSettingsNode-> + addAttribute("tileset", tileset, mapTagReplacements); + // string tech; + gameSettingsNode-> + addAttribute("tech", tech, mapTagReplacements); + // string scenario; + gameSettingsNode-> + addAttribute("scenario", scenario, mapTagReplacements); + // string scenarioDir; + gameSettingsNode-> + addAttribute("scenarioDir", scenarioDir, mapTagReplacements); + // string factionTypeNames[GameConstants::maxPlayers]; //faction names + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + factionTypeNamesNode = + gameSettingsNode->addChild("factionTypeNames"); + factionTypeNamesNode-> + addAttribute("name", factionTypeNames[idx], mapTagReplacements); + } + + // string networkPlayerNames[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerNamesNode = + gameSettingsNode->addChild("networkPlayerNames"); + networkPlayerNamesNode->addAttribute("name", + networkPlayerNames[idx], + mapTagReplacements); + } + + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerNamesNode = + gameSettingsNode->addChild("networkPlayerPlatform"); + networkPlayerNamesNode->addAttribute("name", + networkPlayerPlatform[idx], + mapTagReplacements); + } + + // int networkPlayerStatuses[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerStatusesNode = + gameSettingsNode->addChild("networkPlayerStatuses"); + networkPlayerStatusesNode->addAttribute("status", + intToStr + (networkPlayerStatuses + [idx]), + mapTagReplacements); + } + + // int networkPlayerStatuses[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerStatusesNode = + gameSettingsNode->addChild("networkPlayerGameStatus"); + networkPlayerStatusesNode->addAttribute("game_status", + intToStr + (networkPlayerGameStatus + [idx]), + mapTagReplacements); + } + + // string networkPlayerLanguages[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerLanguagesNode = + gameSettingsNode->addChild("networkPlayerLanguages"); + networkPlayerLanguagesNode->addAttribute("name", + networkPlayerLanguages + [idx], + mapTagReplacements); + } + + // ControlType factionControls[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + factionControlsNode = + gameSettingsNode->addChild("factionControls"); + factionControlsNode->addAttribute("control", + intToStr(factionControls + [idx]), + mapTagReplacements); + } + + // int resourceMultiplierIndex[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + resourceMultiplierIndexNode = + gameSettingsNode->addChild("resourceMultiplierIndex"); + resourceMultiplierIndexNode->addAttribute("multiplier", + intToStr + (resourceMultiplierIndex + [idx]), + mapTagReplacements); + } + + // int thisFactionIndex; + gameSettingsNode->addAttribute("thisFactionIndex", + intToStr(thisFactionIndex), + mapTagReplacements); + // int factionCount; + gameSettingsNode->addAttribute("factionCount", + intToStr(factionCount), + mapTagReplacements); + // int teams[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + teamsNode = gameSettingsNode->addChild("teams"); + teamsNode->addAttribute("team", intToStr(teams[idx]), + mapTagReplacements); + } + + // int startLocationIndex[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + startLocationIndexNode = + gameSettingsNode->addChild("startLocationIndex"); + startLocationIndexNode->addAttribute("location", + intToStr(startLocationIndex + [idx]), + mapTagReplacements); + } + + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + XmlNode * + networkPlayerUUIDNode = + gameSettingsNode->addChild("networkPlayerUUID"); + networkPlayerUUIDNode->addAttribute("value", + networkPlayerUUID[idx], + mapTagReplacements); + } + + // int mapFilterIndex; + gameSettingsNode->addAttribute("mapFilterIndex", + intToStr(mapFilter), + mapTagReplacements); + // + // + // bool defaultUnits; + gameSettingsNode->addAttribute("defaultUnits", + intToStr(defaultUnits), + mapTagReplacements); + // bool defaultResources; + gameSettingsNode->addAttribute("defaultResources", + intToStr(defaultResources), + mapTagReplacements); + // bool defaultVictoryConditions; + gameSettingsNode->addAttribute("defaultVictoryConditions", + intToStr(defaultVictoryConditions), + mapTagReplacements); + // bool fogOfWar; + gameSettingsNode->addAttribute("fogOfWar", intToStr(fogOfWar), + mapTagReplacements); + // bool allowObservers; + gameSettingsNode->addAttribute("allowObservers", + intToStr(allowObservers), + mapTagReplacements); + // bool enableObserverModeAtEndGame; + gameSettingsNode->addAttribute("enableObserverModeAtEndGame", + intToStr + (enableObserverModeAtEndGame), + mapTagReplacements); + // bool enableServerControlledAI; + gameSettingsNode->addAttribute("enableServerControlledAI", + intToStr(enableServerControlledAI), + mapTagReplacements); + // int networkFramePeriod; + gameSettingsNode->addAttribute("networkFramePeriod", + intToStr(networkFramePeriod), + mapTagReplacements); + // bool networkPauseGameForLaggedClients; + gameSettingsNode->addAttribute("networkPauseGameForLaggedClients", + intToStr + (networkPauseGameForLaggedClients), + mapTagReplacements); + // PathFinderType pathFinderType; + gameSettingsNode->addAttribute("pathFinderType", + intToStr(pathFinderType), + mapTagReplacements); + // uint32 flagTypes1; + gameSettingsNode->addAttribute("flagTypes1", uIntToStr(flagTypes1), + mapTagReplacements); + // int32 mapCRC; + gameSettingsNode->addAttribute("mapCRC", uIntToStr(mapCRC), + mapTagReplacements); + // int32 tilesetCRC; + gameSettingsNode->addAttribute("tilesetCRC", uIntToStr(tilesetCRC), + mapTagReplacements); + // int32 techCRC; + gameSettingsNode->addAttribute("techCRC", uIntToStr(techCRC), + mapTagReplacements); + // vector > factionCRCList; + for (unsigned int i = 0; i < factionCRCList.size(); ++i) { + const + pair < + string, + uint32 > & + item = factionCRCList[i]; + + XmlNode * + factionCRCListNode = + gameSettingsNode->addChild("factionCRCList"); + factionCRCListNode->addAttribute("key", item.first, + mapTagReplacements); + factionCRCListNode->addAttribute("value", + uIntToStr(item.second), + mapTagReplacements); + } + // int aiAcceptSwitchTeamPercentChance; + gameSettingsNode->addAttribute("aiAcceptSwitchTeamPercentChance", + intToStr + (aiAcceptSwitchTeamPercentChance), + mapTagReplacements); + // int masterserver_admin; + gameSettingsNode->addAttribute("masterserver_admin", + intToStr(masterserver_admin), + mapTagReplacements); + + gameSettingsNode->addAttribute("masterserver_admin_factionIndex", + intToStr + (masterserver_admin_factionIndex), + mapTagReplacements); + + gameSettingsNode->addAttribute("networkAllowNativeLanguageTechtree", + intToStr + (networkAllowNativeLanguageTechtree), + mapTagReplacements); + } + + void + loadGame(const XmlNode * rootNode) { + const XmlNode * + gameSettingsNode = rootNode->getChild("GameSettings"); + + if (gameSettingsNode->hasAttribute("gameUUID") == true) { + gameUUID = + gameSettingsNode->getAttribute("gameUUID")->getValue(); + } + + // string gameName; + if (gameSettingsNode->hasAttribute("gameName") == true) { + gameName = + gameSettingsNode->getAttribute("gameName")->getValue(); + } else { + gameName = "oldSavegame"; + } + // string description; + description = + gameSettingsNode->getAttribute("description")->getValue(); + // string map; + map = gameSettingsNode->getAttribute("map")->getValue(); + // string tileset; + tileset = gameSettingsNode->getAttribute("tileset")->getValue(); + // string tech; + tech = gameSettingsNode->getAttribute("tech")->getValue(); + // string scenario; + scenario = gameSettingsNode->getAttribute("scenario")->getValue(); + // string scenarioDir; + scenarioDir = + gameSettingsNode->getAttribute("scenarioDir")->getValue(); + if (fileExists(scenarioDir) == false) { + scenarioDir = Config::findValidLocalFileFromPath(scenarioDir); + } + + // string factionTypeNames[GameConstants::maxPlayers]; //faction names + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + factionTypeNamesNode = + gameSettingsNode->getChild("factionTypeNames", idx); + factionTypeNames[idx] = + factionTypeNamesNode->getAttribute("name")->getValue(); + } + + // string networkPlayerNames[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + networkPlayerNamesNode = + gameSettingsNode->getChild("networkPlayerNames", idx); + networkPlayerNames[idx] = + networkPlayerNamesNode->getAttribute("name")->getValue(); + } + + // int networkPlayerStatuses[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + networkPlayerStatusesNode = + gameSettingsNode->getChild("networkPlayerStatuses", idx); + networkPlayerStatuses[idx] = + networkPlayerStatusesNode->getAttribute("status")-> + getIntValue(); + } + + // int networkPlayerStatuses[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + if (gameSettingsNode-> + hasChildAtIndex("networkPlayerGameStatus", idx) == true) { + const XmlNode * + networkPlayerGameStatusNode = + gameSettingsNode->getChild("networkPlayerGameStatus", idx); + networkPlayerGameStatus[idx] = + networkPlayerGameStatusNode->getAttribute("game_status")-> + getIntValue(); + } + } + + // string networkPlayerLanguages[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + networkPlayerLanguagesNode = + gameSettingsNode->getChild("networkPlayerLanguages", idx); + networkPlayerLanguages[idx] = + networkPlayerLanguagesNode->getAttribute("name")->getValue(); + } + + // ControlType factionControls[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + factionControlsNode = + gameSettingsNode->getChild("factionControls", idx); + factionControls[idx] = + static_cast + (factionControlsNode->getAttribute("control")->getIntValue()); + } + + // int resourceMultiplierIndex[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + resourceMultiplierIndexNode = + gameSettingsNode->getChild("resourceMultiplierIndex", idx); + resourceMultiplierIndex[idx] = + resourceMultiplierIndexNode->getAttribute("multiplier")-> + getIntValue(); + } + + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + if (gameSettingsNode-> + hasChildAtIndex("networkPlayerUUID", idx) == true) { + const XmlNode * + networkPlayerUUIDNode = + gameSettingsNode->getChild("networkPlayerUUID", idx); + networkPlayerUUID[idx] = + networkPlayerUUIDNode->getAttribute("value")->getValue(); + } + } + + // int thisFactionIndex; + thisFactionIndex = + gameSettingsNode->getAttribute("thisFactionIndex")->getIntValue(); + // int factionCount; + factionCount = + gameSettingsNode->getAttribute("factionCount")->getIntValue(); + // int teams[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + teamsNode = gameSettingsNode->getChild("teams", idx); + teams[idx] = teamsNode->getAttribute("team")->getIntValue(); + } + + // int startLocationIndex[GameConstants::maxPlayers]; + for (int idx = 0; idx < GameConstants::maxPlayers; idx++) { + const XmlNode * + startLocationIndexNode = + gameSettingsNode->getChild("startLocationIndex", idx); + startLocationIndex[idx] = + startLocationIndexNode->getAttribute("location")-> + getIntValue(); + } + + // int mapFilterIndex; + mapFilter = + gameSettingsNode->getAttribute("mapFilterIndex")->getIntValue(); + // + // + // bool defaultUnits; + defaultUnits = + gameSettingsNode->getAttribute("defaultUnits")->getIntValue() != + 0; + // bool defaultResources; + defaultResources = + gameSettingsNode->getAttribute("defaultResources")-> + getIntValue() != 0; + // bool defaultVictoryConditions; + defaultVictoryConditions = + gameSettingsNode->getAttribute("defaultVictoryConditions")-> + getIntValue() != 0; + // bool fogOfWar; + fogOfWar = + gameSettingsNode->getAttribute("fogOfWar")->getIntValue() != 0; + // bool allowObservers; + allowObservers = + gameSettingsNode->getAttribute("allowObservers")->getIntValue() != + 0; + // bool enableObserverModeAtEndGame; + enableObserverModeAtEndGame = + gameSettingsNode->getAttribute("enableObserverModeAtEndGame")-> + getIntValue() != 0; + // bool enableServerControlledAI; + enableServerControlledAI = + gameSettingsNode->getAttribute("enableServerControlledAI")-> + getIntValue() != 0; + // int networkFramePeriod; + networkFramePeriod = + gameSettingsNode->getAttribute("networkFramePeriod")-> + getIntValue(); + // bool networkPauseGameForLaggedClients; + networkPauseGameForLaggedClients = + gameSettingsNode-> + getAttribute("networkPauseGameForLaggedClients")->getIntValue() != + 0; + // PathFinderType pathFinderType; + pathFinderType = + static_cast + (gameSettingsNode->getAttribute("pathFinderType")->getIntValue()); + // uint32 flagTypes1; + flagTypes1 = + gameSettingsNode->getAttribute("flagTypes1")->getUIntValue(); + // int32 mapCRC; + mapCRC = gameSettingsNode->getAttribute("mapCRC")->getUIntValue(); + // int32 tilesetCRC; + tilesetCRC = + gameSettingsNode->getAttribute("tilesetCRC")->getUIntValue(); + // int32 techCRC; + techCRC = gameSettingsNode->getAttribute("techCRC")->getUIntValue(); + // vector > factionCRCList; + // for(unsigned int i = 0; i < factionCRCList.size(); ++i) { + // const pair &item = factionCRCList[i]; + // + // XmlNode *factionCRCListNode = gameSettingsNode->addChild("factionCRCList"); + // factionCRCListNode->addAttribute("key",item.first, mapTagReplacements); + // factionCRCListNode->addAttribute("value",intToStr(item.second), mapTagReplacements); + // } + // int aiAcceptSwitchTeamPercentChance; + aiAcceptSwitchTeamPercentChance = + gameSettingsNode->getAttribute("aiAcceptSwitchTeamPercentChance")-> + getIntValue(); + // int masterserver_admin; + masterserver_admin = + gameSettingsNode->getAttribute("masterserver_admin")-> + getIntValue(); + + if (gameSettingsNode-> + hasAttribute("masterserver_admin_factionIndex") == true) { + masterserver_admin_factionIndex = + gameSettingsNode-> + getAttribute("masterserver_admin_factionIndex")-> + getIntValue(); + } + + if (gameSettingsNode-> + hasAttribute("networkAllowNativeLanguageTechtree") == true) { + networkAllowNativeLanguageTechtree = + gameSettingsNode-> + getAttribute("networkAllowNativeLanguageTechtree")-> + getIntValue() != 0; + } + } + + }; + + } } //end namespace #endif diff --git a/source/glest_game/game/script_manager.cpp b/source/glest_game/game/script_manager.cpp index ffda30484..09ddc2c49 100644 --- a/source/glest_game/game/script_manager.cpp +++ b/source/glest_game/game/script_manager.cpp @@ -20,7232 +20,6334 @@ #include "leak_dumper.h" using namespace - Shared::Platform; +Shared::Platform; using namespace - Shared::Lua; +Shared::Lua; using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - - ScriptManagerMessage::ScriptManagerMessage (): - text (""), - header ("") - { - this->factionIndex = -1; - this-> - teamIndex = -1; - this-> - messageNotTranslated = true; - } - - ScriptManagerMessage::ScriptManagerMessage (const string & textIn, - const string & headerIn, - int factionIndex, - int teamIndex, - bool messageNotTranslated): - text (textIn), - header (headerIn) - { - this->factionIndex = factionIndex; - this-> - teamIndex = teamIndex; - this-> - messageNotTranslated = messageNotTranslated; - } - - void - ScriptManagerMessage::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - scriptManagerMessageNode = - rootNode->addChild ("ScriptManagerMessage"); - - //string text; - scriptManagerMessageNode->addAttribute ("text", text, - mapTagReplacements); - //string header; - scriptManagerMessageNode->addAttribute ("header", header, - mapTagReplacements); - scriptManagerMessageNode->addAttribute ("factionIndex", - intToStr (factionIndex), - mapTagReplacements); - scriptManagerMessageNode->addAttribute ("teamIndex", - intToStr (teamIndex), - mapTagReplacements); - scriptManagerMessageNode->addAttribute ("messageNotTranslated", - intToStr (messageNotTranslated), - mapTagReplacements); - } - - void - ScriptManagerMessage::loadGame (const XmlNode * rootNode) - { - const XmlNode * - scriptManagerMessageNode = rootNode; - - text = scriptManagerMessageNode->getAttribute ("text")->getValue (); - header = scriptManagerMessageNode->getAttribute ("header")->getValue (); - factionIndex = - scriptManagerMessageNode->getAttribute ("factionIndex")-> - getIntValue (); - teamIndex = - scriptManagerMessageNode->getAttribute ("teamIndex")->getIntValue (); - - messageNotTranslated = true; - if (scriptManagerMessageNode->hasAttribute ("messageNotTranslated") == - true) - { - messageNotTranslated = - (scriptManagerMessageNode->getAttribute ("messageNotTranslated")-> - getIntValue () != 0); - } - } - -// ===================================================== -// class PlayerModifiers -// ===================================================== - - PlayerModifiers::PlayerModifiers () - { - winner = false; - aiEnabled = true; - consumeEnabled = true; - } - - void - PlayerModifiers::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - playerModifiersNode = rootNode->addChild ("PlayerModifiers"); - - //bool winner; - playerModifiersNode->addAttribute ("winner", intToStr (winner), - mapTagReplacements); - //bool aiEnabled; - playerModifiersNode->addAttribute ("aiEnabled", intToStr (aiEnabled), - mapTagReplacements); - //bool consumeEnabled; - playerModifiersNode->addAttribute ("consumeEnabled", - intToStr (consumeEnabled), - mapTagReplacements); - } - - void - PlayerModifiers::loadGame (const XmlNode * rootNode) - { - const XmlNode * - playerModifiersNode = rootNode; - - winner = - playerModifiersNode->getAttribute ("winner")->getIntValue () != 0; - aiEnabled = - playerModifiersNode->getAttribute ("aiEnabled")->getIntValue () != 0; - consumeEnabled = - playerModifiersNode->getAttribute ("consumeEnabled")-> - getIntValue () != 0; - } - - CellTriggerEvent::CellTriggerEvent () - { - type = ctet_Unit; - sourceId = 0; - destId = 0; - //Vec2i destPos; - - triggerCount = 0; - } - - void - CellTriggerEvent::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - cellTriggerEventNode = rootNode->addChild ("CellTriggerEvent"); - -// CellTriggerEventType type; - cellTriggerEventNode->addAttribute ("type", intToStr (type), - mapTagReplacements); -// int sourceId; - cellTriggerEventNode->addAttribute ("sourceId", intToStr (sourceId), - mapTagReplacements); -// int destId; - cellTriggerEventNode->addAttribute ("destId", intToStr (destId), - mapTagReplacements); -// Vec2i destPos; - cellTriggerEventNode->addAttribute ("destPos", destPos.getString (), - mapTagReplacements); -// int triggerCount; - cellTriggerEventNode->addAttribute ("triggerCount", - intToStr (triggerCount), - mapTagReplacements); - -// Vec2i destPosEnd; - cellTriggerEventNode->addAttribute ("destPosEnd", - destPosEnd.getString (), - mapTagReplacements); - } - - void - CellTriggerEvent::loadGame (const XmlNode * rootNode) - { - const XmlNode * - cellTriggerEventNode = rootNode->getChild ("CellTriggerEvent"); - - type = - static_cast < CellTriggerEventType > - (cellTriggerEventNode->getAttribute ("type")->getIntValue ()); - sourceId = - cellTriggerEventNode->getAttribute ("sourceId")->getIntValue (); - destId = cellTriggerEventNode->getAttribute ("destId")->getIntValue (); - destPos = - Vec2i::strToVec2 (cellTriggerEventNode->getAttribute ("destPos")-> - getValue ()); - triggerCount = - cellTriggerEventNode->getAttribute ("triggerCount")->getIntValue (); - - if (cellTriggerEventNode->hasAttribute ("destPosEnd") == true) - { - destPosEnd = - Vec2i::strToVec2 (cellTriggerEventNode-> - getAttribute ("destPosEnd")->getValue ()); - } - } - - TimerTriggerEvent::TimerTriggerEvent () - { - running = false; - startFrame = 0; - endFrame = 0; - triggerSecondsElapsed = 0; - } - - void - TimerTriggerEvent::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - timerTriggerEventNode = rootNode->addChild ("TimerTriggerEvent"); - -// bool running; - timerTriggerEventNode->addAttribute ("running", intToStr (running), - mapTagReplacements); -// //time_t startTime; -// //time_t endTime; -// int startFrame; - timerTriggerEventNode->addAttribute ("startFrame", - intToStr (startFrame), - mapTagReplacements); -// int endFrame; - timerTriggerEventNode->addAttribute ("endFrame", intToStr (endFrame), - mapTagReplacements); - - if (triggerSecondsElapsed > 0) - { - timerTriggerEventNode->addAttribute ("triggerSecondsElapsed", - intToStr - (triggerSecondsElapsed), - mapTagReplacements); - } - } - - void - TimerTriggerEvent::loadGame (const XmlNode * rootNode) - { - const XmlNode * - timerTriggerEventNode = rootNode->getChild ("TimerTriggerEvent"); - - running = - timerTriggerEventNode->getAttribute ("running")->getIntValue () != 0; - startFrame = - timerTriggerEventNode->getAttribute ("startFrame")->getIntValue (); - endFrame = - timerTriggerEventNode->getAttribute ("endFrame")->getIntValue (); - if (timerTriggerEventNode->hasAttribute ("triggerSecondsElapsed") == - true) - { - triggerSecondsElapsed = - timerTriggerEventNode->getAttribute ("triggerSecondsElapsed")-> - getIntValue (); - } - } - -// ===================================================== -// class ScriptManager -// ===================================================== - ScriptManager * - ScriptManager::thisScriptManager = NULL; - const int - ScriptManager::messageWrapCount = 35; - const int - ScriptManager::displayTextWrapCount = 64; - - ScriptManager::ScriptManager () - { - world = NULL; - gameCamera = NULL; - lastCreatedUnitId = -1; - lastDeadUnitId = 0; - lastDeadUnitCauseOfDeath = 0; - lastDeadUnitKillerId = 0; - lastAttackedUnitId = 0; - lastAttackingUnitId = 0; - gameOver = false; - gameWon = false; - currentTimerTriggeredEventId = 0; - currentCellTriggeredEventId = 0; - currentCellTriggeredEventUnitId = 0; - currentEventId = 0; - inCellTriggerEvent = false; - rootNode = NULL; - currentCellTriggeredEventAreaEntryUnitId = 0; - currentCellTriggeredEventAreaExitUnitId = 0; - lastDayNightTriggerStatus = 0; - registeredDayNightEvent = false; - errorCount = 0; - - lastUnitTriggerEventUnitId = -1; - lastUnitTriggerEventType = utet_None; - } - - ScriptManager::~ScriptManager () - { - - } - - void - ScriptManager::init (World * world, GameCamera * gameCamera, - const XmlNode * rootNode) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - //printf("In [%s::%s Line: %d] rootNode [%p][%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rootNode,(rootNode != NULL ? rootNode->getName().c_str() : "none")); - this->rootNode = rootNode; - const Scenario * - scenario = world->getScenario (); - - this->world = world; - this->gameCamera = gameCamera; - - //set static instance - thisScriptManager = this; - - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - currentEventId = 1; - CellTriggerEventList.clear (); - TimerTriggerEventList.clear (); - - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //register functions - luaScript.registerFunction (networkShowMessageForFaction, - "networkShowMessageForFaction"); - luaScript.registerFunction (networkShowMessageForTeam, - "networkShowMessageForTeam"); - - luaScript.registerFunction (networkSetCameraPositionForFaction, - "networkSetCameraPositionForFaction"); - luaScript.registerFunction (networkSetCameraPositionForTeam, - "networkSetCameraPositionForTeam"); - - luaScript.registerFunction (showMessage, "showMessage"); - luaScript.registerFunction (setDisplayText, "setDisplayText"); - luaScript.registerFunction (addConsoleText, "addConsoleText"); - luaScript.registerFunction (addConsoleLangText, "addConsoleLangText"); - luaScript.registerFunction (DisplayFormattedText, - "DisplayFormattedText"); - luaScript.registerFunction (DisplayFormattedText, - "displayFormattedText"); - luaScript.registerFunction (DisplayFormattedLangText, - "DisplayFormattedLangText"); - luaScript.registerFunction (DisplayFormattedLangText, - "displayFormattedLangText"); - luaScript.registerFunction (clearDisplayText, "clearDisplayText"); - luaScript.registerFunction (setCameraPosition, "setCameraPosition"); - luaScript.registerFunction (shakeCamera, "shakeCamera"); - luaScript.registerFunction (shakeCameraOnUnit, "shakeCameraOnUnit"); - luaScript.registerFunction (createUnit, "createUnit"); - luaScript.registerFunction (createUnitNoSpacing, "createUnitNoSpacing"); - luaScript.registerFunction (setLockedUnitForFaction, - "setLockedUnitForFaction"); - luaScript.registerFunction (destroyUnit, "destroyUnit"); - luaScript.registerFunction (giveKills, "giveKills"); - luaScript.registerFunction (morphToUnit, "morphToUnit"); - luaScript.registerFunction (moveToUnit, "moveToUnit"); - - luaScript.registerFunction (playStaticSound, "playStaticSound"); - luaScript.registerFunction (playStreamingSound, "playStreamingSound"); - luaScript.registerFunction (stopStreamingSound, "stopStreamingSound"); - luaScript.registerFunction (stopAllSound, "stopAllSound"); - luaScript.registerFunction (togglePauseGame, "togglePauseGame"); - - luaScript.registerFunction (playStaticVideo, "playStaticVideo"); - //luaScript.registerFunction(playStreamingVideo, "playStreamingVideo"); - //luaScript.registerFunction(stopStreamingVideo, "stopStreamingVideo"); - luaScript.registerFunction (stopAllVideo, "stopAllVideo"); - - luaScript.registerFunction (giveResource, "giveResource"); - luaScript.registerFunction (givePositionCommand, "givePositionCommand"); - luaScript.registerFunction (giveProductionCommand, - "giveProductionCommand"); - luaScript.registerFunction (giveAttackCommand, "giveAttackCommand"); - luaScript.registerFunction (giveUpgradeCommand, "giveUpgradeCommand"); - luaScript.registerFunction (giveAttackStoppedCommand, - "giveAttackStoppedCommand"); - luaScript.registerFunction (disableAi, "disableAi"); - luaScript.registerFunction (enableAi, "enableAi"); - luaScript.registerFunction (getAiEnabled, "getAiEnabled"); - luaScript.registerFunction (disableConsume, "disableConsume"); - luaScript.registerFunction (enableConsume, "enableConsume"); - luaScript.registerFunction (getConsumeEnabled, "getConsumeEnabled"); - luaScript.registerFunction (setPlayerAsWinner, "setPlayerAsWinner"); - luaScript.registerFunction (endGame, "endGame"); - - luaScript.registerFunction (startPerformanceTimer, - "startPerformanceTimer"); - luaScript.registerFunction (endPerformanceTimer, "endPerformanceTimer"); - luaScript.registerFunction (getPerformanceTimerResults, - "getPerformanceTimerResults"); - - luaScript.registerFunction (registerCellTriggerEventForUnitToUnit, - "registerCellTriggerEventForUnitToUnit"); - luaScript.registerFunction (registerCellTriggerEventForUnitToLocation, - "registerCellTriggerEventForUnitToLocation"); - luaScript.registerFunction (registerCellTriggerEventForFactionToUnit, - "registerCellTriggerEventForFactionToUnit"); - luaScript. - registerFunction (registerCellTriggerEventForFactionToLocation, - "registerCellTriggerEventForFactionToLocation"); - - luaScript. - registerFunction (registerCellAreaTriggerEventForUnitToLocation, - "registerCellAreaTriggerEventForUnitToLocation"); - luaScript. - registerFunction (registerCellAreaTriggerEventForFactionToLocation, - "registerCellAreaTriggerEventForFactionToLocation"); - luaScript.registerFunction (registerCellAreaTriggerEvent, - "registerCellAreaTriggerEvent"); - - luaScript.registerFunction (getCellTriggerEventCount, - "getCellTriggerEventCount"); - luaScript.registerFunction (unregisterCellTriggerEvent, - "unregisterCellTriggerEvent"); - luaScript.registerFunction (startTimerEvent, "startTimerEvent"); - luaScript.registerFunction (startEfficientTimerEvent, - "startEfficientTimerEvent"); - luaScript.registerFunction (resetTimerEvent, "resetTimerEvent"); - luaScript.registerFunction (stopTimerEvent, "stopTimerEvent"); - luaScript.registerFunction (getTimerEventSecondsElapsed, - "timerEventSecondsElapsed"); - luaScript.registerFunction (getCellTriggeredEventId, - "triggeredCellEventId"); - luaScript.registerFunction (getTimerTriggeredEventId, - "triggeredTimerEventId"); - - luaScript.registerFunction (getCellTriggeredEventAreaEntryUnitId, - "triggeredEventAreaEntryUnitId"); - luaScript.registerFunction (getCellTriggeredEventAreaExitUnitId, - "triggeredEventAreaExitUnitId"); - - luaScript.registerFunction (getCellTriggeredEventUnitId, - "triggeredCellEventUnitId"); - - luaScript.registerFunction (setRandomGenInit, "setRandomGenInit"); - luaScript.registerFunction (getRandomGen, "getRandomGen"); - luaScript.registerFunction (getWorldFrameCount, "getWorldFrameCount"); - - luaScript.registerFunction (getStartLocation, "startLocation"); - luaScript.registerFunction (getIsUnitAlive, "isUnitAlive"); - luaScript.registerFunction (getUnitPosition, "unitPosition"); - luaScript.registerFunction (setUnitPosition, "setUnitPosition"); - - luaScript.registerFunction (addCellMarker, "addCellMarker"); - luaScript.registerFunction (removeCellMarker, "removeCellMarker"); - luaScript.registerFunction (showMarker, "showMarker"); - - luaScript.registerFunction (getUnitFaction, "unitFaction"); - luaScript.registerFunction (getUnitName, "unitName"); - luaScript.registerFunction (getResourceAmount, "resourceAmount"); - - luaScript.registerFunction (getLastCreatedUnitName, - "lastCreatedUnitName"); - luaScript.registerFunction (getLastCreatedUnitId, "lastCreatedUnit"); - - luaScript.registerFunction (getLastDeadUnitName, "lastDeadUnitName"); - luaScript.registerFunction (getLastDeadUnitId, "lastDeadUnit"); - luaScript.registerFunction (getLastDeadUnitCauseOfDeath, - "lastDeadUnitCauseOfDeath"); - luaScript.registerFunction (getLastDeadUnitKillerName, - "lastDeadUnitKillerName"); - luaScript.registerFunction (getLastDeadUnitKillerId, - "lastDeadUnitKiller"); - - luaScript.registerFunction (getLastAttackedUnitName, - "lastAttackedUnitName"); - luaScript.registerFunction (getLastAttackedUnitId, "lastAttackedUnit"); - - luaScript.registerFunction (getLastAttackingUnitName, - "lastAttackingUnitName"); - luaScript.registerFunction (getLastAttackingUnitId, - "lastAttackingUnit"); - - luaScript.registerFunction (getUnitCount, "unitCount"); - luaScript.registerFunction (getUnitCountOfType, "unitCountOfType"); - - luaScript.registerFunction (getIsGameOver, "isGameOver"); - luaScript.registerFunction (getGameWon, "gameWon"); - - luaScript.registerFunction (getSystemMacroValue, "getSystemMacroValue"); - luaScript.registerFunction (scenarioDir, "scenarioDir"); - luaScript.registerFunction (getPlayerName, "getPlayerName"); - luaScript.registerFunction (getPlayerName, "playerName"); - - luaScript.registerFunction (loadScenario, "loadScenario"); - - luaScript.registerFunction (getUnitsForFaction, "getUnitsForFaction"); - luaScript.registerFunction (getUnitCurrentField, "getUnitCurrentField"); - - luaScript.registerFunction (isFreeCellsOrHasUnit, - "isFreeCellsOrHasUnit"); - luaScript.registerFunction (isFreeCells, "isFreeCells"); - - luaScript.registerFunction (getHumanFactionId, "humanFaction"); - - luaScript.registerFunction (highlightUnit, "highlightUnit"); - luaScript.registerFunction (unhighlightUnit, "unhighlightUnit"); - - luaScript.registerFunction (giveStopCommand, "giveStopCommand"); - luaScript.registerFunction (selectUnit, "selectUnit"); - luaScript.registerFunction (unselectUnit, "unselectUnit"); - luaScript.registerFunction (addUnitToGroupSelection, - "addUnitToGroupSelection"); - luaScript.registerFunction (recallGroupSelection, - "recallGroupSelection"); - luaScript.registerFunction (removeUnitFromGroupSelection, - "removeUnitFromGroupSelection"); - luaScript.registerFunction (setAttackWarningsEnabled, - "setAttackWarningsEnabled"); - luaScript.registerFunction (getAttackWarningsEnabled, - "getAttackWarningsEnabled"); - - luaScript.registerFunction (getIsDayTime, "getIsDayTime"); - luaScript.registerFunction (getIsNightTime, "getIsNightTime"); - luaScript.registerFunction (getTimeOfDay, "getTimeOfDay"); - luaScript.registerFunction (registerDayNightEvent, - "registerDayNightEvent"); - luaScript.registerFunction (unregisterDayNightEvent, - "unregisterDayNightEvent"); - - luaScript.registerFunction (registerUnitTriggerEvent, - "registerUnitTriggerEvent"); - luaScript.registerFunction (unregisterUnitTriggerEvent, - "unregisterUnitTriggerEvent"); - luaScript.registerFunction (getLastUnitTriggerEventUnitId, - "lastUnitTriggerEventUnit"); - luaScript.registerFunction (getLastUnitTriggerEventType, - "lastUnitTriggerEventType"); - luaScript.registerFunction (getUnitProperty, "getUnitProperty"); - luaScript.registerFunction (getUnitPropertyName, "getUnitPropertyName"); - luaScript.registerFunction (disableSpeedChange, "disableSpeedChange"); - luaScript.registerFunction (enableSpeedChange, "enableSpeedChange"); - luaScript.registerFunction (getSpeedChangeEnabled, - "getSpeedChangeEnabled"); - - luaScript.registerFunction (storeSaveGameData, "storeSaveGameData"); - luaScript.registerFunction (loadSaveGameData, "loadSaveGameData"); - - luaScript.registerFunction (getFactionPlayerType, - "getFactionPlayerType"); - - //load code - for (int i = 0; i < scenario->getScriptCount (); ++i) - { - const Script * - script = scenario->getScript (i); - luaScript.loadCode ("function " + script->getName () + "()" + - script->getCode () + "end\n", - script->getName ()); - } - - - //!!! -// string data_path= getGameReadWritePath(GameConstants::path_data_CacheLookupKey); -// if(data_path != ""){ -// endPathWithSlash(data_path); -// } -// string sandboxScriptFilename = data_path + "data/core/scripts/sandbox.lua"; -// string sandboxLuaCode = getFileTextContents(sandboxScriptFilename); -// -// //luaScript.loadCode(sandboxLuaCode + "\n", "megaglest_lua_sandbox"); -// luaScript.setSandboxWrapperFunctionName("runsandboxed"); -// luaScript.setSandboxCode(sandboxLuaCode); -// luaScript.runCode(sandboxLuaCode); - -// // Setup the lua security sandbox here -// luaScript.beginCall("megaglest_lua_sandbox"); -// luaScript.endCall(); - - //setup message box - messageBox.init (Lang::getInstance ().getString ("Ok")); - messageBox.setEnabled (false); - //messageBox.setAutoWordWrap(false); - - //last created unit - lastCreatedUnitId = -1; - lastDeadUnitName = ""; - lastDeadUnitId = -1; - lastDeadUnitCauseOfDeath = ucodNone; - lastDeadUnitKillerName = ""; - lastDeadUnitKillerId = -1; - - lastAttackedUnitName = ""; - lastAttackedUnitId = -1; - lastAttackingUnitName = ""; - lastAttackingUnitId = -1; - - gameOver = false; - gameWon = false; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - // Setup global functions and vars here - luaScript.beginCall ("global"); - luaScript.endCall (); - - //call startup function - if (this->rootNode == NULL) - { - luaScript.beginCall ("startup"); - luaScript.endCall (); - } - else - { - loadGame (this->rootNode); - this->rootNode = NULL; - - if (LuaScript::getDebugModeEnabled () == true) - printf ("Calling onLoad luaSavedGameData.size() = %d\n", - (int) luaSavedGameData.size ()); - - luaScript.beginCall ("onLoad"); - luaScript.endCall (); - } - } - catch (const megaglest_runtime_error & ex) - { - //string sErrBuf = ""; - //if(ex.wantStackTrace() == true) { - char - szErrBuf[8096] = ""; - //snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - string - sErrBuf = - string (szErrBuf) + - string ("The game may no longer be stable!\n\n\t [") + - string (ex.what ()) + string ("]\n"); - //} - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - sErrBuf.c_str ()); - - thisScriptManager-> - addMessageToQueue (ScriptManagerMessage - (sErrBuf.c_str (), "error", -1, -1, true)); - thisScriptManager->onMessageBoxOk (false); - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - -// ========================== events =============================================== - - void - ScriptManager::onMessageBoxOk (bool popFront) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - - for (int index = 0; messageQueue.empty () == false; ++index) - { - //printf("i = %d messageQueue.size() = %d popFront = %d\n",i,messageQueue.size(),popFront); - if (popFront == true) - { - messageQueue.pop_front (); - } - if (messageQueue.empty () == false) - { -// printf("onMessageBoxOk [%s] factionIndex = %d [%d] teamIndex = %d [%d][%d]\n", -// wrapString(lang.getScenarioString(messageQueue.front().getText()), messageWrapCount).c_str(), -// messageQueue.front().getFactionIndex(), this->world->getThisFactionIndex(), -// messageQueue.front().getTeamIndex(),this->world->getThisTeamIndex(),this->world->getThisFaction()->getTeam()); - - if ((messageQueue.front ().getFactionIndex () < 0 - && messageQueue.front ().getTeamIndex () < 0) - || messageQueue.front ().getFactionIndex () == - this->world->getThisFactionIndex () - || messageQueue.front ().getTeamIndex () == - this->world->getThisTeamIndex ()) - { - - messageBox.setEnabled (true); - - string - msgText = messageQueue.front ().getText (); - string - msgHeader = messageQueue.front ().getHeader (); - if (messageQueue.front ().getMessageNotTranslated () == - false) - { - - msgText = - lang.getScenarioString (messageQueue.front (). - getText ()); - msgHeader = - lang.getScenarioString (messageQueue.front (). - getHeader ()); - } - messageBox.setText (msgText); - messageBox.setHeader (msgHeader); - break; - } - popFront = true; - } - } - } - - void - ScriptManager::onResourceHarvested () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->rootNode == NULL) - { - luaScript.beginCall ("resourceHarvested"); - luaScript.endCall (); - } - } - - void - ScriptManager::onUnitCreated (const Unit * unit) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->rootNode == NULL) - { - lastCreatedUnitName = unit->getType ()->getName (false); - lastCreatedUnitId = unit->getId (); - luaScript.beginCall ("unitCreated"); - luaScript.endCall (); - luaScript.beginCall ("unitCreatedOfType_" + - unit->getType ()->getName ()); - luaScript.endCall (); - } - } - - void - ScriptManager::onUnitDied (const Unit * unit) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->rootNode == NULL) - { - if (unit->getLastAttackerUnitId () >= 0) - { - Unit * - killer = world->findUnitById (unit->getLastAttackerUnitId ()); - - if (killer != NULL) - { - lastAttackingUnitName = killer->getType ()->getName (false); - lastAttackingUnitId = killer->getId (); - - lastDeadUnitKillerName = - killer->getType ()->getName (false); - lastDeadUnitKillerId = killer->getId (); - } - else - { - lastDeadUnitKillerName = ""; - lastDeadUnitKillerId = -1; - } - } - - lastAttackedUnitName = unit->getType ()->getName (false); - lastAttackedUnitId = unit->getId (); - - lastDeadUnitName = unit->getType ()->getName (false); - lastDeadUnitId = unit->getId (); - lastDeadUnitCauseOfDeath = unit->getCauseOfDeath (); - - luaScript.beginCall ("unitDied"); - luaScript.endCall (); - } - } - - void - ScriptManager::onUnitAttacked (const Unit * unit) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->rootNode == NULL) - { - lastAttackedUnitName = unit->getType ()->getName (false); - lastAttackedUnitId = unit->getId (); - luaScript.beginCall ("unitAttacked"); - luaScript.endCall (); - } - } - - void - ScriptManager::onUnitAttacking (const Unit * unit) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->rootNode == NULL) - { - lastAttackingUnitName = unit->getType ()->getName (false); - lastAttackingUnitId = unit->getId (); - luaScript.beginCall ("unitAttacking"); - luaScript.endCall (); - } - } - - void - ScriptManager::onGameOver (bool won) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - gameWon = won; - luaScript.beginCall ("gameOver"); - luaScript.endCall (); - } - - void - ScriptManager::onTimerTriggerEvent () - { - if (TimerTriggerEventList.empty () == true) - { - return; - } - if (this->rootNode != NULL) - { - return; - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - TimerTriggerEventList.size ()); - - for (std::map < int, TimerTriggerEvent >::iterator iterMap = - TimerTriggerEventList.begin (); - iterMap != TimerTriggerEventList.end (); ++iterMap) - { - - TimerTriggerEvent & event = iterMap->second; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] event.running = %d, event.startTime = %lld, event.endTime = %lld, diff = %f\n", - __FILE__, __FUNCTION__, __LINE__, - event.running, - (long long int) event.startFrame, - (long long int) event.endFrame, - (event.endFrame - event.startFrame)); - - if (event.running == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // If using an efficient timer, check if its time to trigger - // on the elapsed check - if (event.triggerSecondsElapsed > 0) - { - int - elapsed = - (world->getFrameCount () - - event.startFrame) / GameConstants::updateFps; - if (elapsed < event.triggerSecondsElapsed) - { - continue; - } - } - currentTimerTriggeredEventId = iterMap->first; - luaScript.beginCall ("timerTriggerEvent"); - luaScript.endCall (); - - if (event.triggerSecondsElapsed > 0) - { - int - timerId = iterMap->first; - stopTimerEvent (timerId); - } - } - } - } - - void - ScriptManager::onCellTriggerEvent (Unit * movingUnit) - { - if (CellTriggerEventList.empty () == true) - { - return; - } - if (this->rootNode != NULL) - { - return; - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %p, CellTriggerEventList.size() = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - movingUnit, CellTriggerEventList.size ()); - - // remove any delayed removals - unregisterCellTriggerEvent (-1); - - inCellTriggerEvent = true; - if (movingUnit != NULL) - { - //ScenarioInfo scenarioInfoStart = world->getScenario()->getInfo(); - - for (std::map < int, CellTriggerEvent >::iterator iterMap = - CellTriggerEventList.begin (); - iterMap != CellTriggerEventList.end (); ++iterMap) - { - CellTriggerEvent & event = iterMap->second; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s\n", - __FILE__, __FUNCTION__, __LINE__, - movingUnit->getId (), event.type, - movingUnit->getPos ().getString (). - c_str (), event.sourceId, - event.destId, - event.destPos.getString (). - c_str ()); - - bool - triggerEvent = false; - currentCellTriggeredEventAreaEntryUnitId = 0; - currentCellTriggeredEventAreaExitUnitId = 0; - currentCellTriggeredEventUnitId = 0; - - switch (event.type) - { - case ctet_Unit: - { - Unit * - destUnit = world->findUnitById (event.destId); - if (destUnit != NULL) - { - if (movingUnit->getId () == event.sourceId) - { - bool - srcInDst = - world->getMap ()->isInUnitTypeCells (destUnit-> - getType (), - destUnit-> - getPos (), - movingUnit-> - getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - movingUnit->getId (), - event.type, - movingUnit->getPos (). - getString ().c_str (), - event.sourceId, - event.destId, - event.destPos. - getString ().c_str (), - destUnit->getPos (). - getString ().c_str (), - srcInDst); - - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__). - c_str (), - __FUNCTION__, - __LINE__); - } - else - { - srcInDst = - world->getMap ()-> - isNextToUnitTypeCells (destUnit->getType (), - destUnit->getPos (), - movingUnit-> - getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - movingUnit-> - getId (), - event.type, - movingUnit-> - getPos (). - getString (). - c_str (), - event.sourceId, - event.destId, - event.destPos. - getString (). - c_str (), - destUnit-> - getPos (). - getString (). - c_str (), - srcInDst); - } - triggerEvent = srcInDst; - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - } - break; - case ctet_UnitPos: - { - if (movingUnit->getId () == event.sourceId) - { - bool - srcInDst = - world->getMap ()->isInUnitTypeCells (movingUnit-> - getType (), - event.destPos, - movingUnit-> - getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", - __FILE__, __FUNCTION__, - __LINE__, - movingUnit->getId (), - event.type, - movingUnit->getPos (). - getString ().c_str (), - event.sourceId, - event.destId, - event.destPos. - getString ().c_str (), - srcInDst); - - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__); - } - triggerEvent = srcInDst; - - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - break; - - case ctet_UnitAreaPos: - { - if (movingUnit->getId () == event.sourceId) - { - bool - srcInDst = false; - - // Cache area lookup so for each unitsize and pos its done only once - bool - foundInCache = false; - std::map < int, - std::map < - Vec2i, - bool > >::iterator - iterFind1 = - event.eventLookupCache.find (movingUnit-> - getType ()-> - getSize ()); - if (iterFind1 != event.eventLookupCache.end ()) - { - std::map < Vec2i, bool >::iterator iterFind2 = - iterFind1->second.find (movingUnit->getPos ()); - if (iterFind2 != iterFind1->second.end ()) - { - foundInCache = true; - srcInDst = iterFind2->second; - } - } - - if (foundInCache == false) - { - for (int x = event.destPos.x; - srcInDst == false && x <= event.destPosEnd.x; - ++x) - { - for (int y = event.destPos.y; - srcInDst == false - && y <= event.destPosEnd.y; ++y) - { - srcInDst = - world->getMap ()-> - isInUnitTypeCells (movingUnit-> - getType (), Vec2i (x, - y), - movingUnit-> - getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - movingUnit-> - getId (), - event.type, - movingUnit-> - getPos (). - getString (). - c_str (), - event. - sourceId, - event.destId, - Vec2i (x, - y). - getString (). - c_str (), - srcInDst); - } - } - - event.eventLookupCache[movingUnit->getType ()-> - getSize ()][movingUnit-> - getPos ()] = - srcInDst; - } - - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__); - } - triggerEvent = srcInDst; - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - break; - - case ctet_Faction: - { - Unit * - destUnit = world->findUnitById (event.destId); - if (destUnit != NULL && - movingUnit->getFactionIndex () == event.sourceId) - { - bool - srcInDst = - world->getMap ()->isInUnitTypeCells (destUnit-> - getType (), - destUnit-> - getPos (), - movingUnit-> - getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", - __FILE__, __FUNCTION__, - __LINE__, - movingUnit->getId (), - event.type, - movingUnit->getPos (). - getString ().c_str (), - event.sourceId, - event.destId, - event.destPos. - getString ().c_str (), - srcInDst); - - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__); - } - else - { - srcInDst = - world->getMap ()-> - isNextToUnitTypeCells (destUnit->getType (), - destUnit->getPos (), - movingUnit->getPos ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", - __FILE__, - __FUNCTION__, - __LINE__, - movingUnit->getId (), - event.type, - movingUnit->getPos (). - getString ().c_str (), - event.sourceId, - event.destId, - event.destPos. - getString ().c_str (), - destUnit->getPos (). - getString ().c_str (), - srcInDst); - } - triggerEvent = srcInDst; - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - break; - - case ctet_FactionPos: - { - if (movingUnit->getFactionIndex () == event.sourceId) - { - //printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s]\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str()); - - bool - srcInDst = - world->getMap ()->isInUnitTypeCells (movingUnit-> - getType (), - event.destPos, - movingUnit-> - getPos ()); - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__); - } - triggerEvent = srcInDst; - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - break; - - case ctet_FactionAreaPos: - { - if (movingUnit->getFactionIndex () == event.sourceId) - { - //if(event.sourceId == 1) printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s] Unit id = %d\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str(),movingUnit->getId()); - - bool - srcInDst = false; - - // Cache area lookup so for each unitsize and pos its done only once - bool - foundInCache = false; - std::map < int, - std::map < - Vec2i, - bool > >::iterator - iterFind1 = - event.eventLookupCache.find (movingUnit-> - getType ()-> - getSize ()); - if (iterFind1 != event.eventLookupCache.end ()) - { - std::map < Vec2i, bool >::iterator iterFind2 = - iterFind1->second.find (movingUnit->getPos ()); - if (iterFind2 != iterFind1->second.end ()) - { - foundInCache = true; - srcInDst = iterFind2->second; - } - } - - if (foundInCache == false) - { - for (int x = event.destPos.x; - srcInDst == false && x <= event.destPosEnd.x; - ++x) - { - for (int y = event.destPos.y; - srcInDst == false - && y <= event.destPosEnd.y; ++y) - { - - srcInDst = - world->getMap ()-> - isInUnitTypeCells (movingUnit-> - getType (), Vec2i (x, - y), - movingUnit-> - getPos ()); - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType - (SystemFlags::debugLUA).enabled) - SystemFlags:: - OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__); - } - } - } - - event.eventLookupCache[movingUnit->getType ()-> - getSize ()][movingUnit-> - getPos ()] = - srcInDst; - } - - triggerEvent = srcInDst; - if (triggerEvent == true) - { - //printf("!!!UNIT IN AREA!!! Faction area pos, moving unit faction= %d, trigger faction = %d, unit id = %d\n",movingUnit->getFactionIndex(),event.sourceId,movingUnit->getId()); - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - } - break; - - case ctet_AreaPos: - { - // Is the unit already in the cell range? If no check if they are entering it - if (event.eventStateInfo.find (movingUnit->getId ()) == - event.eventStateInfo.end ()) - { - //printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s]\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str()); - - bool - srcInDst = false; - for (int x = event.destPos.x; - srcInDst == false && x <= event.destPosEnd.x; - ++x) - { - for (int y = event.destPos.y; - srcInDst == false && y <= event.destPosEnd.y; - ++y) - { - - srcInDst = - world->getMap ()-> - isInUnitTypeCells (movingUnit->getType (), - Vec2i (x, y), - movingUnit->getPos ()); - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__). - c_str (), - __FUNCTION__, - __LINE__); - - currentCellTriggeredEventAreaEntryUnitId = - movingUnit->getId (); - event.eventStateInfo[movingUnit-> - getId ()] = - Vec2i (x, y).getString (); - } - } - } - triggerEvent = srcInDst; - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - } - // If unit is already in cell range check if they are leaving? - else - { - bool - srcInDst = false; - for (int x = event.destPos.x; - srcInDst == false && x <= event.destPosEnd.x; - ++x) - { - for (int y = event.destPos.y; - srcInDst == false && y <= event.destPosEnd.y; - ++y) - { - - srcInDst = - world->getMap ()-> - isInUnitTypeCells (movingUnit->getType (), - Vec2i (x, y), - movingUnit->getPos ()); - if (srcInDst == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags:: - debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__). - c_str (), - __FUNCTION__, - __LINE__); - - //event.eventStateInfo[movingUnit->getId()] = Vec2i(x,y); - } - } - } - triggerEvent = (srcInDst == false); - if (triggerEvent == true) - { - currentCellTriggeredEventUnitId = - movingUnit->getId (); - } - - if (triggerEvent == true) - { - currentCellTriggeredEventAreaExitUnitId = - movingUnit->getId (); - - event.eventStateInfo.erase (movingUnit->getId ()); - } - } - } - break; - - } - - if (triggerEvent == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - currentCellTriggeredEventId = iterMap->first; - event.triggerCount++; - - luaScript.beginCall ("cellTriggerEvent"); - luaScript.endCall (); - } - -// ScenarioInfo scenarioInfoEnd = world->getScenario()->getInfo(); -// if(scenarioInfoStart.file != scenarioInfoEnd.file) { -// break; -// } - } - } - - inCellTriggerEvent = false; - } - -// ========================== lua wrappers =============================================== - - string - ScriptManager::wrapString (const string & str, int wrapCount) - { - string - returnString; - - int - letterCount = 0; - for (int i = 0; i < (int) str.size (); ++i) - { - if (letterCount > wrapCount && str[i] == ' ') - { - returnString += '\n'; - letterCount = 0; - } - else - { - returnString += str[i]; - } - ++letterCount; - } - - return returnString; - } - - void - ScriptManager::networkShowMessageForFaction (const string & text, - const string & header, - int factionIndex) - { - messageQueue. - push_back (ScriptManagerMessage (text, header, factionIndex)); - thisScriptManager->onMessageBoxOk (false); - } - void - ScriptManager::networkShowMessageForTeam (const string & text, - const string & header, - int teamIndex) - { - // Team indexes are 0 based internally (but 1 based in the lua script) so convert - teamIndex--; - messageQueue. - push_back (ScriptManagerMessage (text, header, -1, teamIndex)); - thisScriptManager->onMessageBoxOk (false); - } - - void - ScriptManager::networkSetCameraPositionForFaction (int factionIndex, - const Vec2i & pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (factionIndex == this->world->getThisFactionIndex ()) - { - gameCamera->centerXZ (pos.x, pos.y); - } - } - - void - ScriptManager::networkSetCameraPositionForTeam (int teamIndex, - const Vec2i & pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (teamIndex == this->world->getThisTeamIndex ()) - { - gameCamera->centerXZ (pos.x, pos.y); - } - } - - void - ScriptManager::showMessage (const string & text, const string & header) - { - messageQueue.push_back (ScriptManagerMessage (text, header)); - thisScriptManager->onMessageBoxOk (false); - } - - void - ScriptManager::clearDisplayText () - { - displayText = ""; - } - - void - ScriptManager::setDisplayText (const string & text) - { - displayText = - wrapString (Lang::getInstance ().getScenarioString (text), - displayTextWrapCount); - } - - void - ScriptManager::addConsoleText (const string & text) - { - world->addConsoleText (text); - } - void - ScriptManager::addConsoleLangText (const char *fmt, ...) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - va_list - argList; - va_start (argList, fmt); - - const int - max_debug_buffer_size = 8096; - char - szBuf[max_debug_buffer_size] = ""; - vsnprintf (szBuf, max_debug_buffer_size - 1, fmt, argList); - - world->addConsoleTextWoLang (szBuf); - va_end (argList); - - } - - void - ScriptManager::DisplayFormattedText (const char *fmt, ...) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - va_list - argList; - va_start (argList, fmt); - - const int - max_debug_buffer_size = 8096; - char - szBuf[max_debug_buffer_size] = ""; - vsnprintf (szBuf, max_debug_buffer_size - 1, fmt, argList); - - displayText = szBuf; - - va_end (argList); - } - void - ScriptManager::DisplayFormattedLangText (const char *fmt, ...) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - va_list - argList; - va_start (argList, fmt); - - const int - max_debug_buffer_size = 8096; - char - szBuf[max_debug_buffer_size] = ""; - vsnprintf (szBuf, max_debug_buffer_size - 1, fmt, argList); - - displayText = szBuf; - - va_end (argList); - } - void - ScriptManager::setCameraPosition (const Vec2i & pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - gameCamera->centerXZ (pos.x, pos.y); - } - - void - ScriptManager::shakeCamera (int shakeIntensity, int shakeDuration, - bool cameraDistanceAffected, int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (cameraDistanceAffected) - { - Unit * - unit = world->findUnitById (unitId); - if (unit) - { - gameCamera->shake (shakeDuration, shakeIntensity, - cameraDistanceAffected, - unit->getCurrMidHeightVector ()); - } - } - else - { - gameCamera->shake (shakeDuration, shakeIntensity, - cameraDistanceAffected, Vec3f (0.f, 0.f, 0.f)); - } - } - - void - ScriptManager::createUnit (const string & unitName, int factionIndex, - Vec2i pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - unitName.c_str (), factionIndex); - world->createUnit (unitName, factionIndex, pos); - } - - void - ScriptManager::createUnitNoSpacing (const string & unitName, - int factionIndex, Vec2i pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - unitName.c_str (), factionIndex); - world->createUnit (unitName, factionIndex, pos, false); - } - - void - ScriptManager::setLockedUnitForFaction (const string & unitName, - int factionIndex, bool lock) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - unitName.c_str (), factionIndex); - if (world->getFactionCount () > factionIndex) - { - const UnitType * - ut = - world->getFaction (factionIndex)->getType ()-> - getUnitType (unitName); - world->getFaction (factionIndex)->setLockedUnitForFaction (ut, - lock); - } - else - { - throw - megaglest_runtime_error - ("Invalid faction index in setLockedUnitForFaction: " + - intToStr (factionIndex), true); - } - } - - void - ScriptManager::destroyUnit (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, unitId); - Unit * - unit = world->findUnitById (unitId); - if (unit != NULL) - { - // Make sure they die - bool - unit_dead = unit->decHp (unit->getHp () * unit->getHp ()); - if (unit_dead == false) - { - throw - megaglest_runtime_error ("unit_dead == false", true); - } - unit->kill (); - // If called from an existing die event we get a stack overflow - //onUnitDied(unit); - } - } - void - ScriptManager::giveKills (int unitId, int amount) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, unitId); - Unit * - unit = world->findUnitById (unitId); - if (unit != NULL) - { - for (int index = 1; index <= amount; ++index) - { - unit->incKills (-1); - } - } - } - - void - ScriptManager::playStaticSound (const string & playSound) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playSound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playSound.c_str ()); - world->playStaticSound (playSound); - } - void - ScriptManager::playStreamingSound (const string & playSound) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playSound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playSound.c_str ()); - world->playStreamingSound (playSound); - } - - void - ScriptManager::stopStreamingSound (const string & playSound) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playSound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playSound.c_str ()); - world->stopStreamingSound (playSound); - } - - void - ScriptManager::stopAllSound () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - world->stopAllSound (); - } - - void - ScriptManager::playStaticVideo (const string & playVideo) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playVideo [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playVideo.c_str ()); - world->playStaticVideo (playVideo); - } - void - ScriptManager::playStreamingVideo (const string & playVideo) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playVideo [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playVideo.c_str ()); - world->playStreamingVideo (playVideo); - } - - void - ScriptManager::stopStreamingVideo (const string & playVideo) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] playVideo [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - playVideo.c_str ()); - world->stopStreamingVideo (playVideo); - } - - void - ScriptManager::stopAllVideo () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - world->stopAllVideo (); - } - - void - ScriptManager::togglePauseGame (int pauseStatus) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] pauseStatus = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - pauseStatus); - world->togglePauseGame ((pauseStatus != 0), true); - } - - void - ScriptManager::morphToUnit (int unitId, const string & morphName, - int ignoreRequirements) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgradesIfRequired = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, unitId, - morphName.c_str (), ignoreRequirements); - - world->morphToUnit (unitId, morphName, (ignoreRequirements == 1)); - } - - void - ScriptManager::moveToUnit (int unitId, int destUnitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d] destUnitId [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, unitId, - destUnitId); - - world->moveToUnit (unitId, destUnitId); - } - - void - ScriptManager::giveResource (const string & resourceName, - int factionIndex, int amount) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveResource (resourceName, factionIndex, amount); - } - - void - ScriptManager::givePositionCommand (int unitId, - const string & commandName, - const Vec2i & pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->givePositionCommand (unitId, commandName, pos); - } - - void - ScriptManager::giveAttackCommand (int unitId, int unitToAttackId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveAttackCommand (unitId, unitToAttackId); - } - - void - ScriptManager::giveProductionCommand (int unitId, - const string & producedName) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveProductionCommand (unitId, producedName); - } - - void - ScriptManager::giveUpgradeCommand (int unitId, - const string & producedName) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveUpgradeCommand (unitId, producedName); - } - - void - ScriptManager::giveAttackStoppedCommand (int unitId, - const string & itemName, - int ignoreRequirements) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveAttackStoppedCommand (unitId, itemName, - (ignoreRequirements == 1)); - } - - void - ScriptManager::disableAi (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - playerModifiers[factionIndex].disableAi (); - disableConsume (factionIndex); // by this we stay somehow compatible with old behaviour - } - } - - void - ScriptManager::enableAi (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - playerModifiers[factionIndex].enableAi (); - } - } - - bool - ScriptManager::getAiEnabled (int factionIndex) - { - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - return playerModifiers[factionIndex].getAiEnabled (); - } - return false; - } - - void - ScriptManager::disableConsume (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - playerModifiers[factionIndex].disableConsume (); - } - } - - void - ScriptManager::enableConsume (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - playerModifiers[factionIndex].enableConsume (); - } - } - - bool - ScriptManager::getConsumeEnabled (int factionIndex) - { - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - return playerModifiers[factionIndex].getConsumeEnabled (); - } - return false; - } - - int - ScriptManager::registerCellTriggerEventForUnitToUnit (int sourceUnitId, - int destUnitId) - { - CellTriggerEvent - trigger; - trigger.type = ctet_Unit; - trigger.sourceId = sourceUnitId; - trigger.destId = destUnitId; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching unit: %d, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceUnitId, destUnitId, eventId); - - return eventId; - } - - int - ScriptManager:: - registerCellTriggerEventForUnitToLocation (int sourceUnitId, - const Vec2i & pos) - { - CellTriggerEvent - trigger; - trigger.type = ctet_UnitPos; - trigger.sourceId = sourceUnitId; - trigger.destPos = pos; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching pos: %s, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceUnitId, pos.getString ().c_str (), - eventId); - - return eventId; - } - - int - ScriptManager:: - registerCellAreaTriggerEventForUnitToLocation (int sourceUnitId, - const Vec4i & pos) - { - CellTriggerEvent - trigger; - trigger.type = ctet_UnitAreaPos; - trigger.sourceId = sourceUnitId; - trigger.destPos.x = pos.x; - trigger.destPos.y = pos.y; - trigger.destPosEnd.x = pos.z; - trigger.destPosEnd.y = pos.w; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching pos: %s, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceUnitId, pos.getString ().c_str (), - eventId); - - return eventId; - } - - int - ScriptManager:: - registerCellTriggerEventForFactionToUnit (int sourceFactionId, - int destUnitId) - { - CellTriggerEvent - trigger; - trigger.type = ctet_Faction; - trigger.sourceId = sourceFactionId; - trigger.destId = destUnitId; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] Faction: %d will trigger cell event when reaching unit: %d, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceFactionId, destUnitId, eventId); - - return eventId; - } - - int - ScriptManager:: - registerCellTriggerEventForFactionToLocation (int sourceFactionId, - const Vec2i & pos) - { - CellTriggerEvent - trigger; - trigger.type = ctet_FactionPos; - trigger.sourceId = sourceFactionId; - trigger.destPos = pos; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]Faction: %d will trigger cell event when reaching pos: %s, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceFactionId, pos.getString ().c_str (), - eventId); - - return eventId; - } - - int - ScriptManager:: - registerCellAreaTriggerEventForFactionToLocation (int sourceFactionId, - const Vec4i & pos) - { - CellTriggerEvent - trigger; - trigger.type = ctet_FactionAreaPos; - trigger.sourceId = sourceFactionId; - trigger.destPos.x = pos.x; - trigger.destPos.y = pos.y; - trigger.destPosEnd.x = pos.z; - trigger.destPosEnd.y = pos.w; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]Faction: %d will trigger cell event when reaching pos: %s, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - sourceFactionId, pos.getString ().c_str (), - eventId); - - return eventId; - } - - int - ScriptManager::registerCellAreaTriggerEvent (const Vec4i & pos) - { - CellTriggerEvent - trigger; - trigger.type = ctet_AreaPos; - trigger.sourceId = -1; - trigger.destPos.x = pos.x; - trigger.destPos.y = pos.y; - trigger.destPosEnd.x = pos.z; - trigger.destPosEnd.y = pos.w; - - int - eventId = currentEventId++; - CellTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] trigger cell event when reaching pos: %s, eventId = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - pos.getString ().c_str (), eventId); - - return eventId; - } - - int - ScriptManager::getCellTriggerEventCount (int eventId) - { - int - result = 0; - if (CellTriggerEventList.find (eventId) != CellTriggerEventList.end ()) - { - result = CellTriggerEventList[eventId].triggerCount; - } - - return result; - } - - void - ScriptManager::unregisterCellTriggerEvent (int eventId) - { - if (CellTriggerEventList.find (eventId) != CellTriggerEventList.end ()) - { - if (inCellTriggerEvent == false) - { - CellTriggerEventList.erase (eventId); - } - else - { - unRegisterCellTriggerEventList.push_back (eventId); - } - } - - if (inCellTriggerEvent == false) - { - if (unRegisterCellTriggerEventList.empty () == false) - { - for (int i = 0; - i < (int) unRegisterCellTriggerEventList.size (); ++i) - { - int - delayedEventId = unRegisterCellTriggerEventList[i]; - CellTriggerEventList.erase (delayedEventId); - } - unRegisterCellTriggerEventList.clear (); - } - } - } - - int - ScriptManager::startTimerEvent () - { - TimerTriggerEvent - trigger; - trigger.running = true; - //trigger.startTime = time(NULL); - trigger.startFrame = world->getFrameCount (); - //trigger.endTime = 0; - trigger.endFrame = 0; - - int - eventId = currentEventId++; - TimerTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - TimerTriggerEventList.size (), eventId, - (long long int) trigger.startFrame, - (long long int) trigger.endFrame); - - return eventId; - } - - int - ScriptManager::startEfficientTimerEvent (int triggerSecondsElapsed) - { - TimerTriggerEvent - trigger; - trigger.running = true; - //trigger.startTime = time(NULL); - trigger.startFrame = world->getFrameCount (); - //trigger.endTime = 0; - trigger.endFrame = 0; - trigger.triggerSecondsElapsed = triggerSecondsElapsed; - - int - eventId = currentEventId++; - TimerTriggerEventList[eventId] = trigger; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - TimerTriggerEventList.size (), eventId, - (long long int) trigger.startFrame, - (long long int) trigger.endFrame); - - return eventId; - } - - int - ScriptManager::resetTimerEvent (int eventId) - { - int - result = 0; - if (TimerTriggerEventList.find (eventId) != - TimerTriggerEventList.end ()) - { - TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; - result = getTimerEventSecondsElapsed (eventId); - - //trigger.startTime = time(NULL); - trigger.startFrame = world->getFrameCount (); - //trigger.endTime = 0; - trigger.endFrame = 0; - trigger.running = true; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld, result = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - TimerTriggerEventList.size (), eventId, - (long long int) trigger.startFrame, - (long long int) trigger.endFrame, - result); - } - return result; - } - - int - ScriptManager::stopTimerEvent (int eventId) - { - int - result = 0; - if (TimerTriggerEventList.find (eventId) != - TimerTriggerEventList.end ()) - { - TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; - //trigger.endTime = time(NULL); - trigger.endFrame = world->getFrameCount (); - trigger.running = false; - result = getTimerEventSecondsElapsed (eventId); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld, result = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - TimerTriggerEventList.size (), eventId, - (long long int) trigger.startFrame, - (long long int) trigger.endFrame, - result); - } - - return result; - } - - int - ScriptManager::getTimerEventSecondsElapsed (int eventId) - { - int - result = 0; - if (TimerTriggerEventList.find (eventId) != - TimerTriggerEventList.end ()) - { - TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; - if (trigger.running) - { - //result = (int)difftime(time(NULL),trigger.startTime); - result = - (world->getFrameCount () - - trigger.startFrame) / GameConstants::updateFps; - } - else - { - //result = (int)difftime(trigger.endTime,trigger.startTime); - result = - (trigger.endFrame - - trigger.startFrame) / GameConstants::updateFps; - } - - //printf("Timer event id = %d seconmds elapsed = %d\n",eventId,result); - } - - return result; - } - - void - ScriptManager::setPlayerAsWinner (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) - { - playerModifiers[factionIndex].setAsWinner (); - } - } - - void - ScriptManager::endGame () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - gameOver = true; - } - - void - ScriptManager::startPerformanceTimer () - { - - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#1 world->getGame() == NULL", true); - } - world->getGame ()->startPerformanceTimer (); - - } - - void - ScriptManager::endPerformanceTimer () - { - - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#2 world->getGame() == NULL", true); - } - world->getGame ()->endPerformanceTimer (); - - } - - Vec2i - ScriptManager::getPerformanceTimerResults () - { - - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#3 world->getGame() == NULL", true); - } - return world->getGame ()->getPerformanceTimerResults (); - } - - Vec2i - ScriptManager::getStartLocation (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getStartLocation (factionIndex); - } - - - Vec2i - ScriptManager::getUnitPosition (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - Vec2i - result = world->getUnitPosition (unitId); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s] unitId = %d, pos [%s]\n", - __FUNCTION__, unitId, - result.getString ().c_str ()); - - return result; - } - - void - ScriptManager::setUnitPosition (int unitId, Vec2i pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->setUnitPosition (unitId, pos); - } - - void - ScriptManager::addCellMarker (Vec2i pos, int factionIndex, - const string & note, - const string & textureFile) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->addCellMarker (pos, factionIndex, note, textureFile); - } - - void - ScriptManager::removeCellMarker (Vec2i pos, int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->removeCellMarker (pos, factionIndex); - } - - void - ScriptManager::showMarker (Vec2i pos, int factionIndex, - const string & note, - const string & textureFile, int flashCount) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->showMarker (pos, factionIndex, note, textureFile, flashCount); - } - - int - ScriptManager::getIsUnitAlive (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getIsUnitAlive (unitId); - } - - int - ScriptManager::getUnitFaction (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getUnitFactionIndex (unitId); - } - const string - ScriptManager::getUnitName (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - string - result = ""; - Unit * - unit = world->findUnitById (unitId); - if (unit) - { - result = world->findUnitById (unitId)->getType ()->getName (false); - } - return result; - } - - const string - ScriptManager::getUnitDisplayName (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getUnitName (unitId); - } - - int - ScriptManager::getResourceAmount (const string & resourceName, - int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getResourceAmount (resourceName, factionIndex); - } - - const - string & - ScriptManager::getLastCreatedUnitName () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastCreatedUnitName; - } - - int - ScriptManager::getCellTriggeredEventId () - { - - return currentCellTriggeredEventId; - } - - int - ScriptManager::getTimerTriggeredEventId () - { - - return currentTimerTriggeredEventId; - } - - int - ScriptManager::getCellTriggeredEventAreaEntryUnitId () - { - - return currentCellTriggeredEventAreaEntryUnitId; - } - - int - ScriptManager::getCellTriggeredEventAreaExitUnitId () - { - - return currentCellTriggeredEventAreaExitUnitId; - } - - int - ScriptManager::getCellTriggeredEventUnitId () - { - - return currentCellTriggeredEventUnitId; - } - - void - ScriptManager::setRandomGenInit (int seed) - { - - random.init (seed); - } - - int - ScriptManager::getRandomGen (int minVal, int maxVal) - { - - return random.randRange (minVal, maxVal); - } - - int - ScriptManager::getWorldFrameCount () - { - - return world->getFrameCount (); - } - - bool - ScriptManager::getGameWon () const - { - - return - gameWon; - } - - bool - ScriptManager::getIsGameOver () const - { - - return - gameOver; - } - - int - ScriptManager::getLastCreatedUnitId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastCreatedUnitId; - } - - const - string & - ScriptManager::getLastDeadUnitName () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastDeadUnitName; - } - - int - ScriptManager::getLastDeadUnitId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastDeadUnitId; - } - - int - ScriptManager::getLastDeadUnitCauseOfDeath () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastDeadUnitCauseOfDeath; - } - - const - string & - ScriptManager::getLastDeadUnitKillerName () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastDeadUnitKillerName; - } - - int - ScriptManager::getLastDeadUnitKillerId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastDeadUnitKillerId; - } - - const - string & - ScriptManager::getLastAttackedUnitName () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastAttackedUnitName; - } - - int - ScriptManager::getLastAttackedUnitId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastAttackedUnitId; - } - - const - string & - ScriptManager::getLastAttackingUnitName () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastAttackingUnitName; - } - - int - ScriptManager::getLastAttackingUnitId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return lastAttackingUnitId; - } - - int - ScriptManager::getUnitCount (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getUnitCount (factionIndex); - } - - int - ScriptManager::getUnitCountOfType (int factionIndex, - const string & typeName) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getUnitCountOfType (factionIndex, typeName); - } - - const string - ScriptManager::getSystemMacroValue (const string & key) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getSystemMacroValue (key); - } - - const string - ScriptManager::getPlayerName (int factionIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getPlayerName (factionIndex); - } - - void - ScriptManager::loadScenario (const string & name, bool keepFactions) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - world->setQueuedScenario (name, keepFactions); - } - - vector < int > - ScriptManager::getUnitsForFaction (int factionIndex, - const string & commandTypeName, - int field) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - return world->getUnitsForFaction (factionIndex, commandTypeName, field); - } - - int - ScriptManager::getUnitCurrentField (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - return world->getUnitCurrentField (unitId); - } - - int - ScriptManager::isFreeCellsOrHasUnit (int field, int unitId, Vec2i pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - Unit * - unit = world->findUnitById (unitId); - if (unit == NULL) - { - throw - megaglest_runtime_error ("unit == NULL", true); - } - int - result = - world->getMap ()->isFreeCellsOrHasUnit (pos, - unit->getType ()->getSize (), - static_cast < Field > (field), - unit); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s] unitId = %d, [%s] pos [%s] field = %d result = %d\n", - __FUNCTION__, unitId, - unit->getType ()->getName (false).c_str (), - pos.getString ().c_str (), field, result); - - return result; - } - - int - ScriptManager::isFreeCells (int unitSize, int field, Vec2i pos) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - int - result = - world->getMap ()->isFreeCellsOrHasUnit (pos, unitSize, - static_cast < Field > (field), - NULL); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s] unitSize = %d, pos [%s] field = %d result = %d\n", - __FUNCTION__, unitSize, - pos.getString ().c_str (), field, result); - - return result; - } - - int - ScriptManager::getHumanFactionId () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return this->world->getThisFactionIndex (); - } - - void - ScriptManager::highlightUnit (int unitId, float radius, float thickness, - Vec4f color) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - world->highlightUnit (unitId, radius, thickness, color); - } - - void - ScriptManager::unhighlightUnit (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - world->unhighlightUnit (unitId); - } - - void - ScriptManager::giveStopCommand (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->giveStopCommand (unitId); - } - - bool - ScriptManager::selectUnit (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->selectUnit (unitId); - } - - void - ScriptManager::unselectUnit (int unitId) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->unselectUnit (unitId); - } - - void - ScriptManager::addUnitToGroupSelection (int unitId, int groupIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->addUnitToGroupSelection (unitId, groupIndex); - } - void - ScriptManager::recallGroupSelection (int groupIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->recallGroupSelection (groupIndex); - } - void - ScriptManager::removeUnitFromGroupSelection (int unitId, int groupIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->removeUnitFromGroupSelection (unitId, groupIndex); - } - - void - ScriptManager::setAttackWarningsEnabled (bool enabled) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - world->setAttackWarningsEnabled (enabled); - } - - bool - ScriptManager::getAttackWarningsEnabled () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - return world->getAttackWarningsEnabled (); - } - - void - ScriptManager::registerUnitTriggerEvent (int unitId) - { - UnitTriggerEventList[unitId] = utet_None; - } - - void - ScriptManager::unregisterUnitTriggerEvent (int unitId) - { - UnitTriggerEventList.erase (unitId); - } - - int - ScriptManager::getLastUnitTriggerEventUnitId () - { - return lastUnitTriggerEventUnitId; - } - UnitTriggerEventType - ScriptManager::getLastUnitTriggerEventType () - { - return lastUnitTriggerEventType; - } - - int - ScriptManager::getUnitProperty (int unitId, UnitTriggerEventType type) - { - int - result = -1; - - //printf("File: %s line: %d type: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,type); - - Unit * - unit = world->findUnitById (unitId); - if (unit != NULL) - { - switch (type) - { - case utet_None: - result = -2; - break; - case utet_HPChanged: - result = unit->getHp (); - break; - case utet_EPChanged: - result = unit->getEp (); - break; - case utet_LevelChanged: - result = -3; - if (unit->getLevel () != NULL) - { - result = unit->getLevel ()->getKills (); - } - break; - case utet_FieldChanged: - result = unit->getCurrField (); - break; - case utet_SkillChanged: - result = -4; - if (unit->getCurrSkill () != NULL) - { - result = unit->getCurrSkill ()->getClass (); - } - break; - default: - result = -1000; - break; - } - } - //printf("File: %s line: %d result: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,result); - return result; - } - const string - ScriptManager::getUnitPropertyName (int unitId, UnitTriggerEventType type) - { - string - result = ""; - Unit * - unit = world->findUnitById (unitId); - if (unit != NULL) - { - switch (type) - { - case utet_None: - result = ""; - break; - case utet_HPChanged: - result = ""; - break; - case utet_EPChanged: - result = ""; - break; - case utet_LevelChanged: - result = ""; - if (unit->getLevel () != NULL) - { - result = unit->getLevel ()->getName (false); - } - break; - case utet_FieldChanged: - result = ""; - break; - case utet_SkillChanged: - result = ""; - if (unit->getCurrSkill () != NULL) - { - result = unit->getCurrSkill ()->getName (); - } - break; - default: - result = "???"; - break; - } - } - return result; - } - - void - ScriptManager::onUnitTriggerEvent (const Unit * unit, - UnitTriggerEventType event) - { - //static bool inEvent = false; - //if(inEvent == true) { - // printf("\n\n!!!!!!!!!!!!!!! File: %s line: %d unit [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,unit->getId(),unit->getType()->getName().c_str()); - // return; - //} - //inEvent = true; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - if (UnitTriggerEventList.empty () == false) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - std::map < int, - UnitTriggerEventType >::iterator - iterFind = UnitTriggerEventList.find (unit->getId ()); - if (iterFind != UnitTriggerEventList.end ()) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - lastUnitTriggerEventUnitId = unit->getId (); - lastUnitTriggerEventType = event; - - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - luaScript.beginCall ("unitTriggerEvent"); - luaScript.endCall (); - - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - } - //inEvent = false; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - - void - ScriptManager::registerDayNightEvent () - { - registeredDayNightEvent = true; - } - - void - ScriptManager::unregisterDayNightEvent () - { - registeredDayNightEvent = false; - } - - void - ScriptManager::onDayNightTriggerEvent () - { - if (registeredDayNightEvent == true) - { - bool - isDay = (this->getIsDayTime () == 1); - if ((lastDayNightTriggerStatus != 1 && isDay == true) || - (lastDayNightTriggerStatus != 2 && isDay == false)) - { - if (isDay == true) - { - lastDayNightTriggerStatus = 1; - } - else - { - lastDayNightTriggerStatus = 2; - } - - printf ("Triggering daynight event isDay: %d [%f]\n", isDay, - getTimeOfDay ()); - - luaScript.beginCall ("dayNightTriggerEvent"); - luaScript.endCall (); - } - } - } - - int - ScriptManager::getIsDayTime () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - const TimeFlow * - tf = world->getTimeFlow (); - if (tf == NULL) - { - throw - megaglest_runtime_error ("#1 tf == NULL", true); - } - return tf->isDay (); - } - int - ScriptManager::getIsNightTime () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - const TimeFlow * - tf = world->getTimeFlow (); - if (tf == NULL) - { - throw - megaglest_runtime_error ("#2 tf == NULL", true); - } - return tf->isNight (); - } - float - ScriptManager::getTimeOfDay () - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - - const TimeFlow * - tf = world->getTimeFlow (); - if (tf == NULL) - { - throw - megaglest_runtime_error ("#3 tf == NULL", true); - } - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - return tf->getTime (); - } - - void - ScriptManager::disableSpeedChange () - { - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#4 world->getGame() == NULL"); - } - world->getGame ()->setDisableSpeedChange (true); - } - void - ScriptManager::enableSpeedChange () - { - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#5 world->getGame() == NULL"); - } - world->getGame ()->setDisableSpeedChange (false); - } - - bool - ScriptManager::getSpeedChangeEnabled () - { - if (world->getGame () == NULL) - { - throw - megaglest_runtime_error ("#6 world->getGame() == NULL"); - } - return world->getGame ()->getDisableSpeedChange (); - } - - void - ScriptManager::addMessageToQueue (ScriptManagerMessage msg) - { - messageQueue.push_back (msg); - } - - void - ScriptManager::storeSaveGameData (string name, string value) - { - - if (LuaScript::getDebugModeEnabled () == true) - printf ("storeSaveGameData name [%s] value [%s]\n", name.c_str (), - value.c_str ()); - - luaSavedGameData[name] = value; - } - - string - ScriptManager::loadSaveGameData (string name) - { - string - value = luaSavedGameData[name]; - - if (LuaScript::getDebugModeEnabled () == true) - printf ("loadSaveGameData result name [%s] value [%s]\n", - name.c_str (), value.c_str ()); - - return value; - } - - ControlType - ScriptManager::getFactionPlayerType (int factionIndex) - { - Faction * - faction = world->getFaction (factionIndex); - if (faction != NULL) - { - return faction->getControlType (); - } - return ctClosed; - } -// ========================== lua callbacks =============================================== - - int - ScriptManager::showMessage (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - thisScriptManager->showMessage (luaArguments.getString (-2), - luaArguments.getString (-1)); - return luaArguments.getReturnCount (); - } - - int - ScriptManager::networkShowMessageForFaction (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->networkShowMessageForFaction (luaArguments. - getString (-3), - luaArguments. - getString (-2), - luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::networkShowMessageForTeam (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->networkShowMessageForTeam (luaArguments. - getString (-3), - luaArguments. - getString (-2), - luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::networkSetCameraPositionForFaction (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->networkSetCameraPositionForFaction (luaArguments. - getInt (-2), - luaArguments. - getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::networkSetCameraPositionForTeam (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->networkSetCameraPositionForTeam (luaArguments. - getInt (-2), - luaArguments. - getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - - int - ScriptManager::setDisplayText (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->setDisplayText (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::addConsoleText (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->addConsoleText (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::clearDisplayText (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->clearDisplayText (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setCameraPosition (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager-> - setCameraPosition (Vec2i (luaArguments.getVec2i (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::shakeCamera (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->shakeCamera (luaArguments.getInt (-2), - luaArguments.getInt (-1), false, 0); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::shakeCameraOnUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - thisScriptManager->shakeCamera (luaArguments.getInt (-3), - luaArguments.getInt (-2), true, - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::createUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-3).c_str (), - luaArguments.getInt (-2)); - - try - { - thisScriptManager->createUnit (luaArguments.getString (-3), - luaArguments.getInt (-2), - luaArguments.getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - void - ScriptManager::error (LuaHandle * luaHandle, - const megaglest_runtime_error * mgErr, - const char *file, const char *function, int line) - { - char - szErrBuf[8096] = ""; - char - szErrBuf2[8096] = ""; - - int - luaLine = -1; - const char * - luaSource = ""; - - if (luaHandle != NULL) - { - lua_Debug - ar; - lua_getstack (luaHandle, 1, &ar); - lua_getinfo (luaHandle, "nSl", &ar); - - luaLine = ar.currentline; - luaSource = ar.source; - - } - snprintf (szErrBuf, 8096, "in %s::%s %d ", - extractFileFromDirectoryPath (file).c_str (), function, line); - snprintf (szErrBuf2, 8096, "Lua: tag=<%s> line=%d ", luaSource, - luaLine - 1); - string - sErrBuf = string ("Error! The game may no longer be stable!\n\n") - + string (szErrBuf) + "\n" + string (szErrBuf2) + "\n\n" - + string (mgErr->what ()); - - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, sErrBuf.c_str ()); - - thisScriptManager-> - addMessageToQueue (ScriptManagerMessage - (sErrBuf.c_str (), - "error " + - intToStr (thisScriptManager->errorCount), -1, -1, - true)); - thisScriptManager->errorCount++; - thisScriptManager->onMessageBoxOk (false); - } - - int - ScriptManager::createUnitNoSpacing (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-3).c_str (), - luaArguments.getInt (-2)); - - try - { - thisScriptManager->createUnitNoSpacing (luaArguments.getString (-3), - luaArguments.getInt (-2), - luaArguments.getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::destroyUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getInt (-1)); - - try - { - thisScriptManager->destroyUnit (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setLockedUnitForFaction (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-3).c_str (), - luaArguments.getInt (-2)); - - try - { - thisScriptManager->setLockedUnitForFaction (luaArguments. - getString (-3), - luaArguments.getInt (-2), - (luaArguments. - getInt (-1) == - 0 ? false : true)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveKills (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getInt (-1)); - - try - { - thisScriptManager->giveKills (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::morphToUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgrade = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getInt (-3), - luaArguments.getString (-2).c_str (), - luaArguments.getInt (-1)); - - try - { - thisScriptManager->morphToUnit (luaArguments.getInt (-3), - luaArguments.getString (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::moveToUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] unit [%d] dest unit [%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getInt (-2), - luaArguments.getInt (-1)); - - try - { - thisScriptManager->moveToUnit (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::playStaticSound (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->playStaticSound (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::playStreamingSound (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->playStreamingSound (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::stopStreamingSound (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->stopStreamingSound (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::stopAllSound (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - thisScriptManager->stopAllSound (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::playStaticVideo (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->playStaticVideo (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::playStreamingVideo (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->playStreamingVideo (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::stopStreamingVideo (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] sound [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getString (-1).c_str ()); - - try - { - thisScriptManager->stopStreamingVideo (luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::stopAllVideo (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - thisScriptManager->stopAllVideo (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::togglePauseGame (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] value = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - luaArguments.getInt (-1)); - - try - { - thisScriptManager->togglePauseGame (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::giveResource (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveResource (luaArguments.getString (-3), - luaArguments.getInt (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::givePositionCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->givePositionCommand (luaArguments.getInt (-3), - luaArguments.getString (-2), - luaArguments.getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveAttackCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveAttackCommand (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveProductionCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveProductionCommand (luaArguments.getInt (-2), - luaArguments. - getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveUpgradeCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveUpgradeCommand (luaArguments.getInt (-2), - luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveAttackStoppedCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveAttackStoppedCommand (luaArguments.getInt (-3), - luaArguments. - getString (-2), - luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::disableAi (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->disableAi (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::enableAi (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->enableAi (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getAiEnabled (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - bool - result = thisScriptManager->getAiEnabled (luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::disableConsume (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->disableConsume (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::enableConsume (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->enableConsume (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getConsumeEnabled (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - bool - result = - thisScriptManager->getConsumeEnabled (luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellTriggerEventForUnitToUnit (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellTriggerEventForUnitToUnit (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellTriggerEventForUnitToLocation (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellTriggerEventForUnitToLocation (luaArguments.getInt (-2), - luaArguments. - getVec2i (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellAreaTriggerEventForUnitToLocation (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellAreaTriggerEventForUnitToLocation (luaArguments. - getInt (-2), - luaArguments. - getVec4i (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellTriggerEventForFactionToUnit (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellTriggerEventForFactionToUnit (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellTriggerEventForFactionToLocation (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellTriggerEventForFactionToLocation (luaArguments. - getInt (-2), - luaArguments. - getVec2i (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager:: - registerCellAreaTriggerEventForFactionToLocation (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager-> - registerCellAreaTriggerEventForFactionToLocation (luaArguments. - getInt (-2), - luaArguments. - getVec4i (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerCellAreaTriggerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->registerCellAreaTriggerEvent (luaArguments. - getVec4i (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getCellTriggerEventCount (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->getCellTriggerEventCount (luaArguments. - getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::unregisterCellTriggerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->unregisterCellTriggerEvent (luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::startTimerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = thisScriptManager->startTimerEvent (); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::startEfficientTimerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->startEfficientTimerEvent (luaArguments. - getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::stopTimerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->stopTimerEvent (luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::resetTimerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->resetTimerEvent (luaArguments.getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getTimerEventSecondsElapsed (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - result = - thisScriptManager->getTimerEventSecondsElapsed (luaArguments. - getInt (-1)); - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setPlayerAsWinner (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->setPlayerAsWinner (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::endGame (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->endGame (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::startPerformanceTimer (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->startPerformanceTimer (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::endPerformanceTimer (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->endPerformanceTimer (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getPerformanceTimerResults (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - Vec2i - results = thisScriptManager->getPerformanceTimerResults (); - luaArguments.returnVec2i (results); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getStartLocation (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - Vec2i - pos = - thisScriptManager->getStartLocation (luaArguments.getInt (-1)); - luaArguments.returnVec2i (pos); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitPosition (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - Vec2i - pos = thisScriptManager->getUnitPosition (luaArguments.getInt (-1)); - luaArguments.returnVec2i (pos); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setUnitPosition (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->setUnitPosition (luaArguments.getInt (-2), - luaArguments.getVec2i (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::addCellMarker (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - //printf("LUA addCellMarker --> START\n"); - - int - factionIndex = luaArguments.getInt (-4); - - //printf("LUA addCellMarker --> START 1\n"); - - Vec2i - pos = luaArguments.getVec2i (-1); - - //printf("LUA addCellMarker --> START 2\n"); - - string - note = luaArguments.getString (-3); - - //printf("LUA addCellMarker --> START 3\n"); - - string - texture = luaArguments.getString (-2); - - //printf("LUA addCellMarker --> faction [%d] pos [%s] note [%s] texture [%s]\n",factionIndex,pos.getString().c_str(),note.c_str(),texture.c_str()); - - thisScriptManager->addCellMarker (pos, factionIndex, note, texture); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::removeCellMarker (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - int - factionIndex = luaArguments.getInt (-2); - Vec2i - pos = luaArguments.getVec2i (-1); - - thisScriptManager->removeCellMarker (pos, factionIndex); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::showMarker (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - int - flashCount = luaArguments.getInt (-5); - //printf("LUA addCellMarker --> START\n"); - - int - factionIndex = luaArguments.getInt (-4); - - //printf("LUA addCellMarker --> START 1\n"); - - Vec2i - pos = luaArguments.getVec2i (-1); - - //printf("LUA addCellMarker --> START 2\n"); - - string - note = luaArguments.getString (-3); - - //printf("LUA addCellMarker --> START 3\n"); - - string - texture = luaArguments.getString (-2); - - //printf("LUA addCellMarker --> faction [%d] pos [%s] note [%s] texture [%s]\n",factionIndex,pos.getString().c_str(),note.c_str(),texture.c_str()); - - thisScriptManager->showMarker (pos, factionIndex, note, texture, - flashCount); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitFaction (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - factionIndex = - thisScriptManager->getUnitFaction (luaArguments.getInt (-1)); - luaArguments.returnInt (factionIndex); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - const string - unitname = - thisScriptManager->getUnitName (luaArguments.getInt (-1)); - luaArguments.returnString (unitname); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitDisplayName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - const string - unitname = - thisScriptManager->getUnitDisplayName (luaArguments.getInt (-1)); - luaArguments.returnString (unitname); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - - int - ScriptManager::getResourceAmount (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getResourceAmount (luaArguments. - getString (-2), - luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastCreatedUnitName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getLastCreatedUnitName ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastCreatedUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getLastCreatedUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getCellTriggeredEventId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getCellTriggeredEventId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getTimerTriggeredEventId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getTimerTriggeredEventId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getCellTriggeredEventAreaEntryUnitId (LuaHandle * - luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getCellTriggeredEventAreaEntryUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getCellTriggeredEventAreaExitUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getCellTriggeredEventAreaExitUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getCellTriggeredEventUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getCellTriggeredEventUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setRandomGenInit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->setRandomGenInit (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getRandomGen (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getRandomGen (luaArguments.getInt (-2), - luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getWorldFrameCount (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getWorldFrameCount ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastDeadUnitName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager->getLastDeadUnitName ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastDeadUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getLastDeadUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastDeadUnitCauseOfDeath (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getLastDeadUnitCauseOfDeath ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastDeadUnitKillerName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getLastDeadUnitKillerName ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastDeadUnitKillerId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getLastDeadUnitKillerId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastAttackedUnitName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getLastAttackedUnitName ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastAttackedUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getLastAttackedUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastAttackingUnitName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getLastAttackingUnitName ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getLastAttackingUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getLastAttackingUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getSystemMacroValue (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getSystemMacroValue (luaArguments. - getString (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::scenarioDir (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getSystemMacroValue ("$SCENARIO_PATH")); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getPlayerName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - getPlayerName (luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitCount (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getUnitCount (luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitCountOfType (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getUnitCountOfType (luaArguments.getInt (-2), - luaArguments. - getString (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::DisplayFormattedText (LuaHandle * luaHandle) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - //const char *ret; - //lua_lock(luaHandle); - //luaC_checkGC(luaHandle); - - int - args = lua_gettop (luaHandle); - if (lua_checkstack (luaHandle, args + 1)) - { - LuaArguments - luaArguments (luaHandle); - string - fmt = luaArguments.getString (-args); - //va_list argList; - //va_start(argList, fmt.c_str() ); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "DisplayFormattedText args = %d!\n", - args); - - const int - max_args_allowed = 8; - if (args == 1) - { - thisScriptManager->DisplayFormattedText (fmt.c_str ()); - } - else if (args == 2) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + - 1)); - } - else if (args == 3) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + - 2)); - } - else if (args == 4) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + 2), - luaArguments. - getGenericData (-args - + - 3)); - } - else if (args == 5) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + 2), - luaArguments. - getGenericData (-args - + 3), - luaArguments. - getGenericData (-args - + - 4)); - } - else if (args == 6) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + 2), - luaArguments. - getGenericData (-args - + 3), - luaArguments. - getGenericData (-args - + 4), - luaArguments. - getGenericData (-args - + - 5)); - } - else if (args == 7) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + 2), - luaArguments. - getGenericData (-args - + 3), - luaArguments. - getGenericData (-args - + 4), - luaArguments. - getGenericData (-args - + 5), - luaArguments. - getGenericData (-args - + - 6)); - } - else if (args == max_args_allowed) - { - thisScriptManager->DisplayFormattedText (fmt.c_str (), - luaArguments. - getGenericData (-args - + 1), - luaArguments. - getGenericData (-args - + 2), - luaArguments. - getGenericData (-args - + 3), - luaArguments. - getGenericData (-args - + 4), - luaArguments. - getGenericData (-args - + 5), - luaArguments. - getGenericData (-args - + 6), - luaArguments. - getGenericData (-args - + - 7)); - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", - __FUNCTION__, args, max_args_allowed); - throw - megaglest_runtime_error (szBuf); - } - - //va_end(argList); - } - //lua_unlock(luaHandle); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return 1; - -/* - int args=lua_gettop(luaHandle); - if(lua_checkstack(luaHandle, args+1)) - { - va_list argList; - int i; - //lua_getfield(luaHandle, LUA_GLOBALSINDEX, "print"); - for(i = 1;i <= args; i++) { - lua_pushvalue(luaHandle, i); - } - lua_call(luaHandle, args, 0); - } - else - { - return luaL_error(luaHandle, "cannot grow stack"); - } -*/ - -/* - luax_getfunction(L, mod, fn); - for (int i = 0; i < n; i++) { - lua_pushvalue(L, idxs[i]); // The arguments. + Glest { + namespace + Game { + + ScriptManagerMessage::ScriptManagerMessage() : + text(""), + header("") { + this->factionIndex = -1; + this-> + teamIndex = -1; + this-> + messageNotTranslated = true; } - lua_call(L, n, 1); // Call the function, n args, one return value. - lua_replace(L, idxs[0]); // Replace the initial argument with the new object. - return 0; -*/ - } - - int - ScriptManager::addConsoleLangText (LuaHandle * luaHandle) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - //const char *ret; - //lua_lock(luaHandle); - //luaC_checkGC(luaHandle); - - int - args = lua_gettop (luaHandle); - if (lua_checkstack (luaHandle, args + 1)) - { - LuaArguments - luaArguments (luaHandle); - string - fmt = luaArguments.getString (-args); - //va_list argList; - //va_start(argList, fmt.c_str() ); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "DisplayFormattedText args = %d!\n", - args); - - const int - max_args_allowed = 8; - if (args == 1) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str ()); - } - else if (args == 2) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1)); - } - else if (args == 3) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2)); - } - else if (args == 4) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2), - luaArguments. - getGenericData (-args + - 3)); - } - else if (args == 5) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2), - luaArguments. - getGenericData (-args + - 3), - luaArguments. - getGenericData (-args + - 4)); - } - else if (args == 6) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2), - luaArguments. - getGenericData (-args + - 3), - luaArguments. - getGenericData (-args + - 4), - luaArguments. - getGenericData (-args + - 5)); - } - else if (args == 7) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2), - luaArguments. - getGenericData (-args + - 3), - luaArguments. - getGenericData (-args + - 4), - luaArguments. - getGenericData (-args + - 5), - luaArguments. - getGenericData (-args + - 6)); - } - else if (args == max_args_allowed) - { - thisScriptManager->addConsoleLangText (Lang::getInstance (). - getScenarioString - (fmt).c_str (), - luaArguments. - getGenericData (-args + - 1), - luaArguments. - getGenericData (-args + - 2), - luaArguments. - getGenericData (-args + - 3), - luaArguments. - getGenericData (-args + - 4), - luaArguments. - getGenericData (-args + - 5), - luaArguments. - getGenericData (-args + - 6), - luaArguments. - getGenericData (-args + - 7)); - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", - __FUNCTION__, args, max_args_allowed); - throw - megaglest_runtime_error (szBuf); - } - - //va_end(argList); - } - //lua_unlock(luaHandle); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return 1; - } - - int - ScriptManager::DisplayFormattedLangText (LuaHandle * luaHandle) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - try - { - //const char *ret; - //lua_lock(luaHandle); - //luaC_checkGC(luaHandle); - - int - args = lua_gettop (luaHandle); - if (lua_checkstack (luaHandle, args + 1)) - { - LuaArguments - luaArguments (luaHandle); - string - fmt = luaArguments.getString (-args); - //va_list argList; - //va_start(argList, fmt.c_str() ); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "DisplayFormattedText args = %d!\n", - args); - - const int - max_args_allowed = 8; - if (args == 1) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str ()); - } - else if (args == 2) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1)); - } - else if (args == 3) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2)); - } - else if (args == 4) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2), - luaArguments. - getGenericData (-args + 3)); - } - else if (args == 5) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2), - luaArguments. - getGenericData (-args + 3), - luaArguments. - getGenericData (-args + 4)); - } - else if (args == 6) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2), - luaArguments. - getGenericData (-args + 3), - luaArguments. - getGenericData (-args + 4), - luaArguments. - getGenericData (-args + 5)); - } - else if (args == 7) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2), - luaArguments. - getGenericData (-args + 3), - luaArguments. - getGenericData (-args + 4), - luaArguments. - getGenericData (-args + 5), - luaArguments. - getGenericData (-args + 6)); - } - else if (args == max_args_allowed) - { - thisScriptManager-> - DisplayFormattedLangText (Lang::getInstance (). - getScenarioString (fmt).c_str (), - luaArguments. - getGenericData (-args + 1), - luaArguments. - getGenericData (-args + 2), - luaArguments. - getGenericData (-args + 3), - luaArguments. - getGenericData (-args + 4), - luaArguments. - getGenericData (-args + 5), - luaArguments. - getGenericData (-args + 6), - luaArguments. - getGenericData (-args + 7)); - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", - __FUNCTION__, args, max_args_allowed); - throw - megaglest_runtime_error (szBuf); - } - - //va_end(argList); - } - //lua_unlock(luaHandle); - - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return 1; - } - - int - ScriptManager::getGameWon (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getGameWon ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getIsGameOver (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getIsGameOver ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::loadScenario (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->loadScenario (luaArguments.getString (-2), - luaArguments.getInt (-1) != 0); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitsForFaction (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - vector < int > - units = - thisScriptManager->getUnitsForFaction (luaArguments.getInt (-3), - luaArguments.getString (-2), - luaArguments.getInt (-1)); - luaArguments.returnVectorInt (units); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - - } - - int - ScriptManager::getUnitCurrentField (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getUnitCurrentField (luaArguments. - getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getIsUnitAlive (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getIsUnitAlive (luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::isFreeCellsOrHasUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - int - result = - thisScriptManager->isFreeCellsOrHasUnit (luaArguments.getInt (-3), - luaArguments.getInt (-2), - luaArguments. - getVec2i (-1)); - - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::isFreeCells (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - - try - { - int - result = thisScriptManager->isFreeCells (luaArguments.getInt (-3), - luaArguments.getInt (-2), - luaArguments. - getVec2i (-1)); - - luaArguments.returnInt (result); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getHumanFactionId (LuaHandle * luaHandle) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getHumanFactionId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::highlightUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->highlightUnit (luaArguments.getInt (-4), - luaArguments.getFloat (-3), - luaArguments.getFloat (-2), - luaArguments.getVec4f (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::unhighlightUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->unhighlightUnit (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::giveStopCommand (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->giveStopCommand (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::selectUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - selectUnit (luaArguments.getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::unselectUnit (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->unselectUnit (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::addUnitToGroupSelection (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->addUnitToGroupSelection (luaArguments.getInt (-2), - luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::recallGroupSelection (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->recallGroupSelection (luaArguments.getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::removeUnitFromGroupSelection (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->removeUnitFromGroupSelection (luaArguments. - getInt (-2), - luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::setAttackWarningsEnabled (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager-> - setAttackWarningsEnabled ((luaArguments.getInt (-1) == - 0 ? false : true)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getAttackWarningsEnabled (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getAttackWarningsEnabled ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getIsDayTime (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getIsDayTime ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getIsNightTime (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getIsNightTime ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getTimeOfDay (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnFloat (thisScriptManager->getTimeOfDay ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerDayNightEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->registerDayNightEvent (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::unregisterDayNightEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->unregisterDayNightEvent (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::registerUnitTriggerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->registerUnitTriggerEvent (luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::unregisterUnitTriggerEvent (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->unregisterUnitTriggerEvent (luaArguments. - getInt (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getLastUnitTriggerEventUnitId (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getLastUnitTriggerEventUnitId ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getLastUnitTriggerEventType (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (static_cast < - int >(thisScriptManager-> - getLastUnitTriggerEventType ())); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getUnitProperty (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - int - value = - thisScriptManager->getUnitProperty (luaArguments.getInt (-2), - static_cast < - UnitTriggerEventType > - (luaArguments.getInt (-1))); - luaArguments.returnInt (value); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getUnitPropertyName (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - const string - unitname = - thisScriptManager->getUnitPropertyName (luaArguments.getInt (-2), - static_cast < - UnitTriggerEventType > - (luaArguments.getInt (-1))); - luaArguments.returnString (unitname); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::disableSpeedChange (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->disableSpeedChange (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::enableSpeedChange (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->enableSpeedChange (); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - int - ScriptManager::getSpeedChangeEnabled (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager->getSpeedChangeEnabled ()); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::storeSaveGameData (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - thisScriptManager->storeSaveGameData (luaArguments.getString (-2), - luaArguments.getString (-1)); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::loadSaveGameData (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnString (thisScriptManager-> - loadSaveGameData (luaArguments. - getString (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - } - - int - ScriptManager::getFactionPlayerType (LuaHandle * luaHandle) - { - LuaArguments - luaArguments (luaHandle); - try - { - luaArguments.returnInt (thisScriptManager-> - getFactionPlayerType (luaArguments. - getInt (-1))); - } - catch (const megaglest_runtime_error & ex) - { - error (luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); - } - - return luaArguments.getReturnCount (); - - } - - void - ScriptManager::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode * - scriptManagerNode = rootNode->addChild ("ScriptManager"); - - //lua -// string code; - scriptManagerNode->addAttribute ("code", code, mapTagReplacements); -// LuaScript luaScript; - - luaScript.beginCall ("onSave"); - luaScript.endCall (); - - if (LuaScript::getDebugModeEnabled () == true) - printf ("After onSave luaSavedGameData.size() = %d\n", - (int) luaSavedGameData.size ()); - for (std::map < string, string >::iterator iterMap = - luaSavedGameData.begin (); iterMap != luaSavedGameData.end (); - ++iterMap) - { - - XmlNode * - savedGameDataItemNode = - scriptManagerNode->addChild ("SavedGameDataItem"); - - savedGameDataItemNode->addAttribute ("key", iterMap->first, - mapTagReplacements); - savedGameDataItemNode->addAttribute ("value", iterMap->second, - mapTagReplacements); - } - -// //world -// World *world; -// GameCamera *gameCamera; -// -// //misc -// MessageQueue messageQueue; - for (std::list < ScriptManagerMessage >::iterator it = - messageQueue.begin (); it != messageQueue.end (); ++it) - { - (*it).saveGame (scriptManagerNode); - } - - //printf("==== ScriptManager Savegame messageBox [%d][%s][%s] displayText [%s]\n",messageBox.getEnabled(),messageBox.getText().c_str(),messageBox.getHeader().c_str(),displayText.c_str()); - -// GraphicMessageBox messageBox; - scriptManagerNode->addAttribute ("messageBox_enabled", - intToStr (messageBox.getEnabled ()), - mapTagReplacements); - scriptManagerNode->addAttribute ("messageBox_text", - messageBox.getText (), - mapTagReplacements); - scriptManagerNode->addAttribute ("messageBox_header", - messageBox.getHeader (), - mapTagReplacements); - -// string displayText; - scriptManagerNode->addAttribute ("displayText", displayText, - mapTagReplacements); -// -// //last created unit -// string lastCreatedUnitName; - scriptManagerNode->addAttribute ("lastCreatedUnitName", - lastCreatedUnitName, - mapTagReplacements); -// int lastCreatedUnitId; - scriptManagerNode->addAttribute ("lastCreatedUnitId", - intToStr (lastCreatedUnitId), - mapTagReplacements); -// -// //last dead unit -// string lastDeadUnitName; - scriptManagerNode->addAttribute ("lastDeadUnitName", lastDeadUnitName, - mapTagReplacements); -// int lastDeadUnitId; - scriptManagerNode->addAttribute ("lastDeadUnitId", - intToStr (lastDeadUnitId), - mapTagReplacements); -// int lastDeadUnitCauseOfDeath; - scriptManagerNode->addAttribute ("lastDeadUnitCauseOfDeath", - intToStr (lastDeadUnitCauseOfDeath), - mapTagReplacements); -// -// //last dead unit's killer -// string lastDeadUnitKillerName; - scriptManagerNode->addAttribute ("lastDeadUnitKillerName", - lastDeadUnitKillerName, - mapTagReplacements); -// int lastDeadUnitKillerId; - scriptManagerNode->addAttribute ("lastDeadUnitKillerId", - intToStr (lastDeadUnitKillerId), - mapTagReplacements); -// -// //last attacked unit -// string lastAttackedUnitName; - scriptManagerNode->addAttribute ("lastAttackedUnitName", - lastAttackedUnitName, - mapTagReplacements); -// int lastAttackedUnitId; - scriptManagerNode->addAttribute ("lastAttackedUnitId", - intToStr (lastAttackedUnitId), - mapTagReplacements); -// -// //last attacking unit -// string lastAttackingUnitName; - scriptManagerNode->addAttribute ("lastAttackingUnitName", - lastAttackingUnitName, - mapTagReplacements); -// int lastAttackingUnitId; - scriptManagerNode->addAttribute ("lastAttackingUnitId", - intToStr (lastAttackingUnitId), - mapTagReplacements); -// -// // end game state -// bool gameOver; - scriptManagerNode->addAttribute ("gameOver", intToStr (gameOver), - mapTagReplacements); -// bool gameWon; - scriptManagerNode->addAttribute ("gameWon", intToStr (gameWon), - mapTagReplacements); -// PlayerModifiers playerModifiers[GameConstants::maxPlayers]; - for (unsigned int i = 0; i < (unsigned int) GameConstants::maxPlayers; - ++i) - { - PlayerModifiers & player = playerModifiers[i]; - player.saveGame (scriptManagerNode); - } -// int currentTimerTriggeredEventId; - scriptManagerNode->addAttribute ("currentTimerTriggeredEventId", - intToStr - (currentTimerTriggeredEventId), - mapTagReplacements); -// int currentCellTriggeredEventId; - scriptManagerNode->addAttribute ("currentCellTriggeredEventId", - intToStr (currentCellTriggeredEventId), - mapTagReplacements); -// int currentEventId; - scriptManagerNode->addAttribute ("currentEventId", - intToStr (currentEventId), - mapTagReplacements); -// std::map CellTriggerEventList; - for (std::map < int, CellTriggerEvent >::iterator iterMap = - CellTriggerEventList.begin (); - iterMap != CellTriggerEventList.end (); ++iterMap) - { - XmlNode * - cellTriggerEventListNode = - scriptManagerNode->addChild ("CellTriggerEventList"); - - cellTriggerEventListNode->addAttribute ("key", - intToStr (iterMap->first), - mapTagReplacements); - iterMap->second.saveGame (cellTriggerEventListNode); - } -// std::map TimerTriggerEventList; - for (std::map < int, TimerTriggerEvent >::iterator iterMap = - TimerTriggerEventList.begin (); - iterMap != TimerTriggerEventList.end (); ++iterMap) - { - XmlNode * - timerTriggerEventListNode = - scriptManagerNode->addChild ("TimerTriggerEventList"); - - timerTriggerEventListNode->addAttribute ("key", - intToStr (iterMap->first), - mapTagReplacements); - iterMap->second.saveGame (timerTriggerEventListNode); - } - -// bool inCellTriggerEvent; - scriptManagerNode->addAttribute ("inCellTriggerEvent", - intToStr (inCellTriggerEvent), - mapTagReplacements); -// std::vector unRegisterCellTriggerEventList; - for (unsigned int i = 0; i < unRegisterCellTriggerEventList.size (); - ++i) - { - XmlNode * - unRegisterCellTriggerEventListNode = - scriptManagerNode->addChild ("unRegisterCellTriggerEventList"); - unRegisterCellTriggerEventListNode->addAttribute ("eventId", - intToStr - (unRegisterCellTriggerEventList - [i]), - mapTagReplacements); - } - - scriptManagerNode->addAttribute ("registeredDayNightEvent", - intToStr (registeredDayNightEvent), - mapTagReplacements); - scriptManagerNode->addAttribute ("lastDayNightTriggerStatus", - intToStr (lastDayNightTriggerStatus), - mapTagReplacements); - - for (std::map < int, UnitTriggerEventType >::iterator iterMap = - UnitTriggerEventList.begin (); - iterMap != UnitTriggerEventList.end (); ++iterMap) - { - XmlNode * - unitTriggerEventListNode = - scriptManagerNode->addChild ("UnitTriggerEventList"); - - unitTriggerEventListNode->addAttribute ("unitId", - intToStr (iterMap->first), - mapTagReplacements); - unitTriggerEventListNode->addAttribute ("eventType", - intToStr (iterMap->second), - mapTagReplacements); - } - scriptManagerNode->addAttribute ("lastUnitTriggerEventUnitId", - intToStr (lastUnitTriggerEventUnitId), - mapTagReplacements); - scriptManagerNode->addAttribute ("lastUnitTriggerEventType", - intToStr (lastUnitTriggerEventType), - mapTagReplacements); - - luaScript.saveGame (scriptManagerNode); - } - - void - ScriptManager::loadGame (const XmlNode * rootNode) - { - const XmlNode * - scriptManagerNode = rootNode->getChild ("ScriptManager"); - -// string code; - code = scriptManagerNode->getAttribute ("code")->getValue (); -// LuaScript luaScript; - - vector < XmlNode * >savedGameDataItemNodeList = - scriptManagerNode->getChildList ("SavedGameDataItem"); - - if (LuaScript::getDebugModeEnabled () == true) - printf ("In loadGame savedGameDataItemNodeList.size() = %d\n", - (int) savedGameDataItemNodeList.size ()); - - for (unsigned int i = 0; i < savedGameDataItemNodeList.size (); ++i) - { - XmlNode * - node = savedGameDataItemNodeList[i]; - string - key = node->getAttribute ("key")->getValue (); - string - value = node->getAttribute ("value")->getValue (); - - luaSavedGameData[key] = value; - } - -// //world -// World *world; -// GameCamera *gameCamera; -// -// //misc -// MessageQueue messageQueue; - messageQueue.clear (); - vector < XmlNode * >messageQueueNodeList = - scriptManagerNode->getChildList ("ScriptManagerMessage"); - for (unsigned int i = 0; i < messageQueueNodeList.size (); ++i) - { - XmlNode * - node = messageQueueNodeList[i]; - ScriptManagerMessage - msg; - msg.loadGame (node); - messageQueue.push_back (msg); - } - -// GraphicMessageBox messageBox; - messageBox.setEnabled (scriptManagerNode-> - getAttribute ("messageBox_enabled")-> - getIntValue () != 0); - messageBox.setText (scriptManagerNode-> - getAttribute ("messageBox_text")->getValue ()); - messageBox.setHeader (scriptManagerNode-> - getAttribute ("messageBox_header")->getValue ()); - -// string displayText; - displayText = - scriptManagerNode->getAttribute ("displayText")->getValue (); -// -// //last created unit -// string lastCreatedUnitName; - lastCreatedUnitName = - scriptManagerNode->getAttribute ("lastCreatedUnitName")->getValue (); -// int lastCreatedUnitId; - lastCreatedUnitId = - scriptManagerNode->getAttribute ("lastCreatedUnitId")->getIntValue (); -// -// //last dead unit -// string lastDeadUnitName; - lastDeadUnitName = - scriptManagerNode->getAttribute ("lastDeadUnitName")->getValue (); -// int lastDeadUnitId; - lastDeadUnitId = - scriptManagerNode->getAttribute ("lastDeadUnitId")->getIntValue (); -// int lastDeadUnitCauseOfDeath; - lastDeadUnitCauseOfDeath = - scriptManagerNode->getAttribute ("lastDeadUnitCauseOfDeath")-> - getIntValue (); -// -// //last dead unit's killer -// string lastDeadUnitKillerName; - lastDeadUnitKillerName = - scriptManagerNode->getAttribute ("lastDeadUnitKillerName")-> - getValue (); -// int lastDeadUnitKillerId; - lastDeadUnitKillerId = - scriptManagerNode->getAttribute ("lastDeadUnitKillerId")-> - getIntValue (); -// -// //last attacked unit -// string lastAttackedUnitName; - lastAttackedUnitName = - scriptManagerNode->getAttribute ("lastAttackedUnitName")->getValue (); -// int lastAttackedUnitId; - lastAttackedUnitId = - scriptManagerNode->getAttribute ("lastAttackedUnitId")-> - getIntValue (); -// -// //last attacking unit -// string lastAttackingUnitName; - lastAttackingUnitName = - scriptManagerNode->getAttribute ("lastAttackingUnitName")-> - getValue (); -// int lastAttackingUnitId; - lastAttackingUnitId = - scriptManagerNode->getAttribute ("lastAttackingUnitId")-> - getIntValue (); -// -// // end game state -// bool gameOver; - gameOver = - scriptManagerNode->getAttribute ("gameOver")->getIntValue () != 0; -// bool gameWon; - gameWon = - scriptManagerNode->getAttribute ("gameWon")->getIntValue () != 0; -// PlayerModifiers playerModifiers[GameConstants::maxPlayers]; - vector < XmlNode * >playerModifiersNodeList = - scriptManagerNode->getChildList ("PlayerModifiers"); - for (unsigned int i = 0; i < playerModifiersNodeList.size (); ++i) - { - XmlNode * - node = playerModifiersNodeList[i]; - playerModifiers[i].loadGame (node); - } -// int currentTimerTriggeredEventId; - currentTimerTriggeredEventId = - scriptManagerNode->getAttribute ("currentTimerTriggeredEventId")-> - getIntValue (); -// int currentCellTriggeredEventId; - currentCellTriggeredEventId = - scriptManagerNode->getAttribute ("currentCellTriggeredEventId")-> - getIntValue (); -// int currentEventId; - currentEventId = - scriptManagerNode->getAttribute ("currentEventId")->getIntValue (); -// std::map CellTriggerEventList; - vector < XmlNode * >cellTriggerEventListNodeList = - scriptManagerNode->getChildList ("CellTriggerEventList"); - for (unsigned int i = 0; i < cellTriggerEventListNodeList.size (); ++i) - { - XmlNode * - node = cellTriggerEventListNodeList[i]; - CellTriggerEvent - event; - event.loadGame (node); - CellTriggerEventList[node->getAttribute ("key")->getIntValue ()] = - event; - } - -// std::map TimerTriggerEventList; - vector < XmlNode * >timerTriggerEventListNodeList = - scriptManagerNode->getChildList ("TimerTriggerEventList"); - for (unsigned int i = 0; i < timerTriggerEventListNodeList.size (); ++i) - { - XmlNode * - node = timerTriggerEventListNodeList[i]; - - TimerTriggerEvent - event; - event.loadGame (node); - TimerTriggerEventList[node->getAttribute ("key")->getIntValue ()] = - event; - } - -// bool inCellTriggerEvent; - inCellTriggerEvent = - scriptManagerNode->getAttribute ("inCellTriggerEvent")-> - getIntValue () != 0; -// std::vector unRegisterCellTriggerEventList; - vector < XmlNode * >unRegisterCellTriggerEventListNodeList = - scriptManagerNode->getChildList ("unRegisterCellTriggerEventList"); - for (unsigned int i = 0; - i < unRegisterCellTriggerEventListNodeList.size (); ++i) - { - XmlNode * - node = unRegisterCellTriggerEventListNodeList[i]; - unRegisterCellTriggerEventList.push_back (node-> - getAttribute ("eventId")-> - getIntValue ()); - } - - if (scriptManagerNode->hasAttribute ("registeredDayNightEvent") == true) - { - registeredDayNightEvent = - scriptManagerNode->getAttribute ("registeredDayNightEvent")-> - getIntValue () != 0; - } - if (scriptManagerNode->hasAttribute ("lastDayNightTriggerStatus") == - true) - { - lastDayNightTriggerStatus = - scriptManagerNode->getAttribute ("lastDayNightTriggerStatus")-> - getIntValue (); - } - - vector < XmlNode * >unitTriggerEventListNodeList = - scriptManagerNode->getChildList ("UnitTriggerEventList"); - for (unsigned int i = 0; i < unitTriggerEventListNodeList.size (); ++i) - { - XmlNode * - node = unitTriggerEventListNodeList[i]; - - UnitTriggerEventType - eventType = utet_None; - int - unitId = node->getAttribute ("unitId")->getIntValue (); - if (node->hasAttribute ("eventType") == true) - { - eventType = - static_cast < UnitTriggerEventType > - (node->getAttribute ("eventType")->getIntValue ()); - } - else if (node->hasAttribute ("evenType") == true) - { - eventType = - static_cast < UnitTriggerEventType > - (node->getAttribute ("evenType")->getIntValue ()); - } - UnitTriggerEventList[unitId] = eventType; - } - if (scriptManagerNode->hasAttribute ("lastUnitTriggerEventUnitId") == - true) - { - lastUnitTriggerEventUnitId = - scriptManagerNode->getAttribute ("lastUnitTriggerEventUnitId")-> - getIntValue (); - } - if (scriptManagerNode->hasAttribute ("lastUnitTriggerEventType") == - true) - { - lastUnitTriggerEventType = - static_cast < UnitTriggerEventType > - (scriptManagerNode->getAttribute ("lastUnitTriggerEventType")-> - getIntValue ()); - } - - luaScript.loadGame (scriptManagerNode); - } - - - } + + ScriptManagerMessage::ScriptManagerMessage(const string & textIn, + const string & headerIn, + int factionIndex, + int teamIndex, + bool messageNotTranslated) : + text(textIn), + header(headerIn) { + this->factionIndex = factionIndex; + this-> + teamIndex = teamIndex; + this-> + messageNotTranslated = messageNotTranslated; + } + + void + ScriptManagerMessage::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + scriptManagerMessageNode = + rootNode->addChild("ScriptManagerMessage"); + + //string text; + scriptManagerMessageNode->addAttribute("text", text, + mapTagReplacements); + //string header; + scriptManagerMessageNode->addAttribute("header", header, + mapTagReplacements); + scriptManagerMessageNode->addAttribute("factionIndex", + intToStr(factionIndex), + mapTagReplacements); + scriptManagerMessageNode->addAttribute("teamIndex", + intToStr(teamIndex), + mapTagReplacements); + scriptManagerMessageNode->addAttribute("messageNotTranslated", + intToStr(messageNotTranslated), + mapTagReplacements); + } + + void + ScriptManagerMessage::loadGame(const XmlNode * rootNode) { + const XmlNode * + scriptManagerMessageNode = rootNode; + + text = scriptManagerMessageNode->getAttribute("text")->getValue(); + header = scriptManagerMessageNode->getAttribute("header")->getValue(); + factionIndex = + scriptManagerMessageNode->getAttribute("factionIndex")-> + getIntValue(); + teamIndex = + scriptManagerMessageNode->getAttribute("teamIndex")->getIntValue(); + + messageNotTranslated = true; + if (scriptManagerMessageNode->hasAttribute("messageNotTranslated") == + true) { + messageNotTranslated = + (scriptManagerMessageNode->getAttribute("messageNotTranslated")-> + getIntValue() != 0); + } + } + + // ===================================================== + // class PlayerModifiers + // ===================================================== + + PlayerModifiers::PlayerModifiers() { + winner = false; + aiEnabled = true; + consumeEnabled = true; + } + + void + PlayerModifiers::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + playerModifiersNode = rootNode->addChild("PlayerModifiers"); + + //bool winner; + playerModifiersNode->addAttribute("winner", intToStr(winner), + mapTagReplacements); + //bool aiEnabled; + playerModifiersNode->addAttribute("aiEnabled", intToStr(aiEnabled), + mapTagReplacements); + //bool consumeEnabled; + playerModifiersNode->addAttribute("consumeEnabled", + intToStr(consumeEnabled), + mapTagReplacements); + } + + void + PlayerModifiers::loadGame(const XmlNode * rootNode) { + const XmlNode * + playerModifiersNode = rootNode; + + winner = + playerModifiersNode->getAttribute("winner")->getIntValue() != 0; + aiEnabled = + playerModifiersNode->getAttribute("aiEnabled")->getIntValue() != 0; + consumeEnabled = + playerModifiersNode->getAttribute("consumeEnabled")-> + getIntValue() != 0; + } + + CellTriggerEvent::CellTriggerEvent() { + type = ctet_Unit; + sourceId = 0; + destId = 0; + //Vec2i destPos; + + triggerCount = 0; + } + + void + CellTriggerEvent::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + cellTriggerEventNode = rootNode->addChild("CellTriggerEvent"); + + // CellTriggerEventType type; + cellTriggerEventNode->addAttribute("type", intToStr(type), + mapTagReplacements); + // int sourceId; + cellTriggerEventNode->addAttribute("sourceId", intToStr(sourceId), + mapTagReplacements); + // int destId; + cellTriggerEventNode->addAttribute("destId", intToStr(destId), + mapTagReplacements); + // Vec2i destPos; + cellTriggerEventNode->addAttribute("destPos", destPos.getString(), + mapTagReplacements); + // int triggerCount; + cellTriggerEventNode->addAttribute("triggerCount", + intToStr(triggerCount), + mapTagReplacements); + + // Vec2i destPosEnd; + cellTriggerEventNode->addAttribute("destPosEnd", + destPosEnd.getString(), + mapTagReplacements); + } + + void + CellTriggerEvent::loadGame(const XmlNode * rootNode) { + const XmlNode * + cellTriggerEventNode = rootNode->getChild("CellTriggerEvent"); + + type = + static_cast + (cellTriggerEventNode->getAttribute("type")->getIntValue()); + sourceId = + cellTriggerEventNode->getAttribute("sourceId")->getIntValue(); + destId = cellTriggerEventNode->getAttribute("destId")->getIntValue(); + destPos = + Vec2i::strToVec2(cellTriggerEventNode->getAttribute("destPos")-> + getValue()); + triggerCount = + cellTriggerEventNode->getAttribute("triggerCount")->getIntValue(); + + if (cellTriggerEventNode->hasAttribute("destPosEnd") == true) { + destPosEnd = + Vec2i::strToVec2(cellTriggerEventNode-> + getAttribute("destPosEnd")->getValue()); + } + } + + TimerTriggerEvent::TimerTriggerEvent() { + running = false; + startFrame = 0; + endFrame = 0; + triggerSecondsElapsed = 0; + } + + void + TimerTriggerEvent::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + timerTriggerEventNode = rootNode->addChild("TimerTriggerEvent"); + + // bool running; + timerTriggerEventNode->addAttribute("running", intToStr(running), + mapTagReplacements); + // //time_t startTime; + // //time_t endTime; + // int startFrame; + timerTriggerEventNode->addAttribute("startFrame", + intToStr(startFrame), + mapTagReplacements); + // int endFrame; + timerTriggerEventNode->addAttribute("endFrame", intToStr(endFrame), + mapTagReplacements); + + if (triggerSecondsElapsed > 0) { + timerTriggerEventNode->addAttribute("triggerSecondsElapsed", + intToStr + (triggerSecondsElapsed), + mapTagReplacements); + } + } + + void + TimerTriggerEvent::loadGame(const XmlNode * rootNode) { + const XmlNode * + timerTriggerEventNode = rootNode->getChild("TimerTriggerEvent"); + + running = + timerTriggerEventNode->getAttribute("running")->getIntValue() != 0; + startFrame = + timerTriggerEventNode->getAttribute("startFrame")->getIntValue(); + endFrame = + timerTriggerEventNode->getAttribute("endFrame")->getIntValue(); + if (timerTriggerEventNode->hasAttribute("triggerSecondsElapsed") == + true) { + triggerSecondsElapsed = + timerTriggerEventNode->getAttribute("triggerSecondsElapsed")-> + getIntValue(); + } + } + + // ===================================================== + // class ScriptManager + // ===================================================== + ScriptManager * + ScriptManager::thisScriptManager = NULL; + const int + ScriptManager::messageWrapCount = 35; + const int + ScriptManager::displayTextWrapCount = 64; + + ScriptManager::ScriptManager() { + world = NULL; + gameCamera = NULL; + lastCreatedUnitId = -1; + lastDeadUnitId = 0; + lastDeadUnitCauseOfDeath = 0; + lastDeadUnitKillerId = 0; + lastAttackedUnitId = 0; + lastAttackingUnitId = 0; + gameOver = false; + gameWon = false; + currentTimerTriggeredEventId = 0; + currentCellTriggeredEventId = 0; + currentCellTriggeredEventUnitId = 0; + currentEventId = 0; + inCellTriggerEvent = false; + rootNode = NULL; + currentCellTriggeredEventAreaEntryUnitId = 0; + currentCellTriggeredEventAreaExitUnitId = 0; + lastDayNightTriggerStatus = 0; + registeredDayNightEvent = false; + errorCount = 0; + + lastUnitTriggerEventUnitId = -1; + lastUnitTriggerEventType = utet_None; + } + + ScriptManager::~ScriptManager() { + + } + + void + ScriptManager::init(World * world, GameCamera * gameCamera, + const XmlNode * rootNode) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + //printf("In [%s::%s Line: %d] rootNode [%p][%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rootNode,(rootNode != NULL ? rootNode->getName().c_str() : "none")); + this->rootNode = rootNode; + const Scenario * + scenario = world->getScenario(); + + this->world = world; + this->gameCamera = gameCamera; + + //set static instance + thisScriptManager = this; + + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + currentEventId = 1; + CellTriggerEventList.clear(); + TimerTriggerEventList.clear(); + + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //register functions + luaScript.registerFunction(networkShowMessageForFaction, + "networkShowMessageForFaction"); + luaScript.registerFunction(networkShowMessageForTeam, + "networkShowMessageForTeam"); + + luaScript.registerFunction(networkSetCameraPositionForFaction, + "networkSetCameraPositionForFaction"); + luaScript.registerFunction(networkSetCameraPositionForTeam, + "networkSetCameraPositionForTeam"); + + luaScript.registerFunction(showMessage, "showMessage"); + luaScript.registerFunction(setDisplayText, "setDisplayText"); + luaScript.registerFunction(addConsoleText, "addConsoleText"); + luaScript.registerFunction(addConsoleLangText, "addConsoleLangText"); + luaScript.registerFunction(DisplayFormattedText, + "DisplayFormattedText"); + luaScript.registerFunction(DisplayFormattedText, + "displayFormattedText"); + luaScript.registerFunction(DisplayFormattedLangText, + "DisplayFormattedLangText"); + luaScript.registerFunction(DisplayFormattedLangText, + "displayFormattedLangText"); + luaScript.registerFunction(clearDisplayText, "clearDisplayText"); + luaScript.registerFunction(setCameraPosition, "setCameraPosition"); + luaScript.registerFunction(shakeCamera, "shakeCamera"); + luaScript.registerFunction(shakeCameraOnUnit, "shakeCameraOnUnit"); + luaScript.registerFunction(createUnit, "createUnit"); + luaScript.registerFunction(createUnitNoSpacing, "createUnitNoSpacing"); + luaScript.registerFunction(setLockedUnitForFaction, + "setLockedUnitForFaction"); + luaScript.registerFunction(destroyUnit, "destroyUnit"); + luaScript.registerFunction(giveKills, "giveKills"); + luaScript.registerFunction(morphToUnit, "morphToUnit"); + luaScript.registerFunction(moveToUnit, "moveToUnit"); + + luaScript.registerFunction(playStaticSound, "playStaticSound"); + luaScript.registerFunction(playStreamingSound, "playStreamingSound"); + luaScript.registerFunction(stopStreamingSound, "stopStreamingSound"); + luaScript.registerFunction(stopAllSound, "stopAllSound"); + luaScript.registerFunction(togglePauseGame, "togglePauseGame"); + + luaScript.registerFunction(playStaticVideo, "playStaticVideo"); + //luaScript.registerFunction(playStreamingVideo, "playStreamingVideo"); + //luaScript.registerFunction(stopStreamingVideo, "stopStreamingVideo"); + luaScript.registerFunction(stopAllVideo, "stopAllVideo"); + + luaScript.registerFunction(giveResource, "giveResource"); + luaScript.registerFunction(givePositionCommand, "givePositionCommand"); + luaScript.registerFunction(giveProductionCommand, + "giveProductionCommand"); + luaScript.registerFunction(giveAttackCommand, "giveAttackCommand"); + luaScript.registerFunction(giveUpgradeCommand, "giveUpgradeCommand"); + luaScript.registerFunction(giveAttackStoppedCommand, + "giveAttackStoppedCommand"); + luaScript.registerFunction(disableAi, "disableAi"); + luaScript.registerFunction(enableAi, "enableAi"); + luaScript.registerFunction(getAiEnabled, "getAiEnabled"); + luaScript.registerFunction(disableConsume, "disableConsume"); + luaScript.registerFunction(enableConsume, "enableConsume"); + luaScript.registerFunction(getConsumeEnabled, "getConsumeEnabled"); + luaScript.registerFunction(setPlayerAsWinner, "setPlayerAsWinner"); + luaScript.registerFunction(endGame, "endGame"); + + luaScript.registerFunction(startPerformanceTimer, + "startPerformanceTimer"); + luaScript.registerFunction(endPerformanceTimer, "endPerformanceTimer"); + luaScript.registerFunction(getPerformanceTimerResults, + "getPerformanceTimerResults"); + + luaScript.registerFunction(registerCellTriggerEventForUnitToUnit, + "registerCellTriggerEventForUnitToUnit"); + luaScript.registerFunction(registerCellTriggerEventForUnitToLocation, + "registerCellTriggerEventForUnitToLocation"); + luaScript.registerFunction(registerCellTriggerEventForFactionToUnit, + "registerCellTriggerEventForFactionToUnit"); + luaScript. + registerFunction(registerCellTriggerEventForFactionToLocation, + "registerCellTriggerEventForFactionToLocation"); + + luaScript. + registerFunction(registerCellAreaTriggerEventForUnitToLocation, + "registerCellAreaTriggerEventForUnitToLocation"); + luaScript. + registerFunction(registerCellAreaTriggerEventForFactionToLocation, + "registerCellAreaTriggerEventForFactionToLocation"); + luaScript.registerFunction(registerCellAreaTriggerEvent, + "registerCellAreaTriggerEvent"); + + luaScript.registerFunction(getCellTriggerEventCount, + "getCellTriggerEventCount"); + luaScript.registerFunction(unregisterCellTriggerEvent, + "unregisterCellTriggerEvent"); + luaScript.registerFunction(startTimerEvent, "startTimerEvent"); + luaScript.registerFunction(startEfficientTimerEvent, + "startEfficientTimerEvent"); + luaScript.registerFunction(resetTimerEvent, "resetTimerEvent"); + luaScript.registerFunction(stopTimerEvent, "stopTimerEvent"); + luaScript.registerFunction(getTimerEventSecondsElapsed, + "timerEventSecondsElapsed"); + luaScript.registerFunction(getCellTriggeredEventId, + "triggeredCellEventId"); + luaScript.registerFunction(getTimerTriggeredEventId, + "triggeredTimerEventId"); + + luaScript.registerFunction(getCellTriggeredEventAreaEntryUnitId, + "triggeredEventAreaEntryUnitId"); + luaScript.registerFunction(getCellTriggeredEventAreaExitUnitId, + "triggeredEventAreaExitUnitId"); + + luaScript.registerFunction(getCellTriggeredEventUnitId, + "triggeredCellEventUnitId"); + + luaScript.registerFunction(setRandomGenInit, "setRandomGenInit"); + luaScript.registerFunction(getRandomGen, "getRandomGen"); + luaScript.registerFunction(getWorldFrameCount, "getWorldFrameCount"); + + luaScript.registerFunction(getStartLocation, "startLocation"); + luaScript.registerFunction(getIsUnitAlive, "isUnitAlive"); + luaScript.registerFunction(getUnitPosition, "unitPosition"); + luaScript.registerFunction(setUnitPosition, "setUnitPosition"); + + luaScript.registerFunction(addCellMarker, "addCellMarker"); + luaScript.registerFunction(removeCellMarker, "removeCellMarker"); + luaScript.registerFunction(showMarker, "showMarker"); + + luaScript.registerFunction(getUnitFaction, "unitFaction"); + luaScript.registerFunction(getUnitName, "unitName"); + luaScript.registerFunction(getResourceAmount, "resourceAmount"); + + luaScript.registerFunction(getLastCreatedUnitName, + "lastCreatedUnitName"); + luaScript.registerFunction(getLastCreatedUnitId, "lastCreatedUnit"); + + luaScript.registerFunction(getLastDeadUnitName, "lastDeadUnitName"); + luaScript.registerFunction(getLastDeadUnitId, "lastDeadUnit"); + luaScript.registerFunction(getLastDeadUnitCauseOfDeath, + "lastDeadUnitCauseOfDeath"); + luaScript.registerFunction(getLastDeadUnitKillerName, + "lastDeadUnitKillerName"); + luaScript.registerFunction(getLastDeadUnitKillerId, + "lastDeadUnitKiller"); + + luaScript.registerFunction(getLastAttackedUnitName, + "lastAttackedUnitName"); + luaScript.registerFunction(getLastAttackedUnitId, "lastAttackedUnit"); + + luaScript.registerFunction(getLastAttackingUnitName, + "lastAttackingUnitName"); + luaScript.registerFunction(getLastAttackingUnitId, + "lastAttackingUnit"); + + luaScript.registerFunction(getUnitCount, "unitCount"); + luaScript.registerFunction(getUnitCountOfType, "unitCountOfType"); + + luaScript.registerFunction(getIsGameOver, "isGameOver"); + luaScript.registerFunction(getGameWon, "gameWon"); + + luaScript.registerFunction(getSystemMacroValue, "getSystemMacroValue"); + luaScript.registerFunction(scenarioDir, "scenarioDir"); + luaScript.registerFunction(getPlayerName, "getPlayerName"); + luaScript.registerFunction(getPlayerName, "playerName"); + + luaScript.registerFunction(loadScenario, "loadScenario"); + + luaScript.registerFunction(getUnitsForFaction, "getUnitsForFaction"); + luaScript.registerFunction(getUnitCurrentField, "getUnitCurrentField"); + + luaScript.registerFunction(isFreeCellsOrHasUnit, + "isFreeCellsOrHasUnit"); + luaScript.registerFunction(isFreeCells, "isFreeCells"); + + luaScript.registerFunction(getHumanFactionId, "humanFaction"); + + luaScript.registerFunction(highlightUnit, "highlightUnit"); + luaScript.registerFunction(unhighlightUnit, "unhighlightUnit"); + + luaScript.registerFunction(giveStopCommand, "giveStopCommand"); + luaScript.registerFunction(selectUnit, "selectUnit"); + luaScript.registerFunction(unselectUnit, "unselectUnit"); + luaScript.registerFunction(addUnitToGroupSelection, + "addUnitToGroupSelection"); + luaScript.registerFunction(recallGroupSelection, + "recallGroupSelection"); + luaScript.registerFunction(removeUnitFromGroupSelection, + "removeUnitFromGroupSelection"); + luaScript.registerFunction(setAttackWarningsEnabled, + "setAttackWarningsEnabled"); + luaScript.registerFunction(getAttackWarningsEnabled, + "getAttackWarningsEnabled"); + + luaScript.registerFunction(getIsDayTime, "getIsDayTime"); + luaScript.registerFunction(getIsNightTime, "getIsNightTime"); + luaScript.registerFunction(getTimeOfDay, "getTimeOfDay"); + luaScript.registerFunction(registerDayNightEvent, + "registerDayNightEvent"); + luaScript.registerFunction(unregisterDayNightEvent, + "unregisterDayNightEvent"); + + luaScript.registerFunction(registerUnitTriggerEvent, + "registerUnitTriggerEvent"); + luaScript.registerFunction(unregisterUnitTriggerEvent, + "unregisterUnitTriggerEvent"); + luaScript.registerFunction(getLastUnitTriggerEventUnitId, + "lastUnitTriggerEventUnit"); + luaScript.registerFunction(getLastUnitTriggerEventType, + "lastUnitTriggerEventType"); + luaScript.registerFunction(getUnitProperty, "getUnitProperty"); + luaScript.registerFunction(getUnitPropertyName, "getUnitPropertyName"); + luaScript.registerFunction(disableSpeedChange, "disableSpeedChange"); + luaScript.registerFunction(enableSpeedChange, "enableSpeedChange"); + luaScript.registerFunction(getSpeedChangeEnabled, + "getSpeedChangeEnabled"); + + luaScript.registerFunction(storeSaveGameData, "storeSaveGameData"); + luaScript.registerFunction(loadSaveGameData, "loadSaveGameData"); + + luaScript.registerFunction(getFactionPlayerType, + "getFactionPlayerType"); + + //load code + for (int i = 0; i < scenario->getScriptCount(); ++i) { + const Script * + script = scenario->getScript(i); + luaScript.loadCode("function " + script->getName() + "()" + + script->getCode() + "end\n", + script->getName()); + } + //load code + for (int i = 0; i < world->getFactionCount(); ++i) { + FactionType const* type = world->getFaction(i)->getType(); + for (int j = 0; j < type->getScriptCount(); j++) { + const Script* script = type->getScript(j); + luaScript.loadCode("function " + script->getName() + "()" + + script->getCode() + "end\n", + script->getName()); + } + } + + //!!! + // string data_path= getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + // if(data_path != ""){ + // endPathWithSlash(data_path); + // } + // string sandboxScriptFilename = data_path + "data/core/scripts/sandbox.lua"; + // string sandboxLuaCode = getFileTextContents(sandboxScriptFilename); + // + // //luaScript.loadCode(sandboxLuaCode + "\n", "megaglest_lua_sandbox"); + // luaScript.setSandboxWrapperFunctionName("runsandboxed"); + // luaScript.setSandboxCode(sandboxLuaCode); + // luaScript.runCode(sandboxLuaCode); + + // // Setup the lua security sandbox here + // luaScript.beginCall("megaglest_lua_sandbox"); + // luaScript.endCall(); + + //setup message box + messageBox.init(Lang::getInstance().getString("Ok")); + messageBox.setEnabled(false); + //messageBox.setAutoWordWrap(false); + + //last created unit + lastCreatedUnitId = -1; + lastDeadUnitName = ""; + lastDeadUnitId = -1; + lastDeadUnitCauseOfDeath = ucodNone; + lastDeadUnitKillerName = ""; + lastDeadUnitKillerId = -1; + + lastAttackedUnitName = ""; + lastAttackedUnitId = -1; + lastAttackingUnitName = ""; + lastAttackingUnitId = -1; + + gameOver = false; + gameWon = false; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + // Setup global functions and vars here + luaScript.beginCall("global"); + luaScript.endCall(); + + //call startup function + if (this->rootNode == NULL) { + luaScript.beginCall("startup"); + luaScript.endCall(); + } else { + loadGame(this->rootNode); + this->rootNode = NULL; + + if (LuaScript::getDebugModeEnabled() == true) + printf("Calling onLoad luaSavedGameData.size() = %d\n", + (int) luaSavedGameData.size()); + + luaScript.beginCall("onLoad"); + luaScript.endCall(); + } + } catch (const megaglest_runtime_error & ex) { + //string sErrBuf = ""; + //if(ex.wantStackTrace() == true) { + char + szErrBuf[8096] = ""; + //snprintf(szErrBuf,8096,"In [%s::%s %d]",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string + sErrBuf = + string(szErrBuf) + + string("The game may no longer be stable!\n\n\t [") + + string(ex.what()) + string("]\n"); + //} + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + sErrBuf.c_str()); + + thisScriptManager-> + addMessageToQueue(ScriptManagerMessage + (sErrBuf.c_str(), "error", -1, -1, true)); + thisScriptManager->onMessageBoxOk(false); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + // ========================== events =============================================== + + void + ScriptManager::onMessageBoxOk(bool popFront) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + + for (int index = 0; messageQueue.empty() == false; ++index) { + //printf("i = %d messageQueue.size() = %d popFront = %d\n",i,messageQueue.size(),popFront); + if (popFront == true) { + messageQueue.pop_front(); + } + if (messageQueue.empty() == false) { + // printf("onMessageBoxOk [%s] factionIndex = %d [%d] teamIndex = %d [%d][%d]\n", + // wrapString(lang.getScenarioString(messageQueue.front().getText()), messageWrapCount).c_str(), + // messageQueue.front().getFactionIndex(), this->world->getThisFactionIndex(), + // messageQueue.front().getTeamIndex(),this->world->getThisTeamIndex(),this->world->getThisFaction()->getTeam()); + + if ((messageQueue.front().getFactionIndex() < 0 + && messageQueue.front().getTeamIndex() < 0) + || messageQueue.front().getFactionIndex() == + this->world->getThisFactionIndex() + || messageQueue.front().getTeamIndex() == + this->world->getThisTeamIndex()) { + + messageBox.setEnabled(true); + + string + msgText = messageQueue.front().getText(); + string + msgHeader = messageQueue.front().getHeader(); + if (messageQueue.front().getMessageNotTranslated() == + false) { + + msgText = + lang.getScenarioString(messageQueue.front(). + getText()); + msgHeader = + lang.getScenarioString(messageQueue.front(). + getHeader()); + } + messageBox.setText(msgText); + messageBox.setHeader(msgHeader); + break; + } + popFront = true; + } + } + } + + void + ScriptManager::onResourceHarvested() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->rootNode == NULL) { + luaScript.beginCall("resourceHarvested"); + luaScript.endCall(); + } + } + + void + ScriptManager::onUnitCreated(const Unit * unit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->rootNode == NULL) { + lastCreatedUnitName = unit->getType()->getName(false); + lastCreatedUnitId = unit->getId(); + luaScript.beginCall("unitCreated"); + luaScript.endCall(); + luaScript.beginCall("unitCreatedOfType_" + + unit->getType()->getName()); + luaScript.endCall(); + } + } + + void + ScriptManager::onUnitDied(const Unit * unit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->rootNode == NULL) { + if (unit->getLastAttackerUnitId() >= 0) { + Unit * + killer = world->findUnitById(unit->getLastAttackerUnitId()); + + if (killer != NULL) { + lastAttackingUnitName = killer->getType()->getName(false); + lastAttackingUnitId = killer->getId(); + + lastDeadUnitKillerName = + killer->getType()->getName(false); + lastDeadUnitKillerId = killer->getId(); + } else { + lastDeadUnitKillerName = ""; + lastDeadUnitKillerId = -1; + } + } + + lastAttackedUnitName = unit->getType()->getName(false); + lastAttackedUnitId = unit->getId(); + + lastDeadUnitName = unit->getType()->getName(false); + lastDeadUnitId = unit->getId(); + lastDeadUnitCauseOfDeath = unit->getCauseOfDeath(); + + luaScript.beginCall("unitDied"); + luaScript.endCall(); + } + } + + void + ScriptManager::onUnitAttacked(const Unit * unit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->rootNode == NULL) { + lastAttackedUnitName = unit->getType()->getName(false); + lastAttackedUnitId = unit->getId(); + luaScript.beginCall("unitAttacked"); + luaScript.endCall(); + } + } + + void + ScriptManager::onUnitAttacking(const Unit * unit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->rootNode == NULL) { + lastAttackingUnitName = unit->getType()->getName(false); + lastAttackingUnitId = unit->getId(); + luaScript.beginCall("unitAttacking"); + luaScript.endCall(); + } + } + + void + ScriptManager::onGameOver(bool won) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + gameWon = won; + luaScript.beginCall("gameOver"); + luaScript.endCall(); + } + + void + ScriptManager::onTimerTriggerEvent() { + if (TimerTriggerEventList.empty() == true) { + return; + } + if (this->rootNode != NULL) { + return; + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + TimerTriggerEventList.size()); + + for (std::map < int, TimerTriggerEvent >::iterator iterMap = + TimerTriggerEventList.begin(); + iterMap != TimerTriggerEventList.end(); ++iterMap) { + + TimerTriggerEvent & event = iterMap->second; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] event.running = %d, event.startTime = %lld, event.endTime = %lld, diff = %f\n", + __FILE__, __FUNCTION__, __LINE__, + event.running, + (long long int) event.startFrame, + (long long int) event.endFrame, + (event.endFrame - event.startFrame)); + + if (event.running == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // If using an efficient timer, check if its time to trigger + // on the elapsed check + if (event.triggerSecondsElapsed > 0) { + int + elapsed = + (world->getFrameCount() - + event.startFrame) / GameConstants::updateFps; + if (elapsed < event.triggerSecondsElapsed) { + continue; + } + } + currentTimerTriggeredEventId = iterMap->first; + luaScript.beginCall("timerTriggerEvent"); + luaScript.endCall(); + + if (event.triggerSecondsElapsed > 0) { + int + timerId = iterMap->first; + stopTimerEvent(timerId); + } + } + } + } + + void + ScriptManager::onCellTriggerEvent(Unit * movingUnit) { + if (CellTriggerEventList.empty() == true) { + return; + } + if (this->rootNode != NULL) { + return; + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %p, CellTriggerEventList.size() = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + movingUnit, CellTriggerEventList.size()); + + // remove any delayed removals + unregisterCellTriggerEvent(-1); + + inCellTriggerEvent = true; + if (movingUnit != NULL) { + //ScenarioInfo scenarioInfoStart = world->getScenario()->getInfo(); + + for (std::map < int, CellTriggerEvent >::iterator iterMap = + CellTriggerEventList.begin(); + iterMap != CellTriggerEventList.end(); ++iterMap) { + CellTriggerEvent & event = iterMap->second; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s\n", + __FILE__, __FUNCTION__, __LINE__, + movingUnit->getId(), event.type, + movingUnit->getPos().getString(). + c_str(), event.sourceId, + event.destId, + event.destPos.getString(). + c_str()); + + bool + triggerEvent = false; + currentCellTriggeredEventAreaEntryUnitId = 0; + currentCellTriggeredEventAreaExitUnitId = 0; + currentCellTriggeredEventUnitId = 0; + + switch (event.type) { + case ctet_Unit: + { + Unit * + destUnit = world->findUnitById(event.destId); + if (destUnit != NULL) { + if (movingUnit->getId() == event.sourceId) { + bool + srcInDst = + world->getMap()->isInUnitTypeCells(destUnit-> + getType(), + destUnit-> + getPos(), + movingUnit-> + getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + movingUnit->getId(), + event.type, + movingUnit->getPos(). + getString().c_str(), + event.sourceId, + event.destId, + event.destPos. + getString().c_str(), + destUnit->getPos(). + getString().c_str(), + srcInDst); + + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__). + c_str(), + __FUNCTION__, + __LINE__); + } else { + srcInDst = + world->getMap()-> + isNextToUnitTypeCells(destUnit->getType(), + destUnit->getPos(), + movingUnit-> + getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + movingUnit-> + getId(), + event.type, + movingUnit-> + getPos(). + getString(). + c_str(), + event.sourceId, + event.destId, + event.destPos. + getString(). + c_str(), + destUnit-> + getPos(). + getString(). + c_str(), + srcInDst); + } + triggerEvent = srcInDst; + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + } + break; + case ctet_UnitPos: + { + if (movingUnit->getId() == event.sourceId) { + bool + srcInDst = + world->getMap()->isInUnitTypeCells(movingUnit-> + getType(), + event.destPos, + movingUnit-> + getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", + __FILE__, __FUNCTION__, + __LINE__, + movingUnit->getId(), + event.type, + movingUnit->getPos(). + getString().c_str(), + event.sourceId, + event.destId, + event.destPos. + getString().c_str(), + srcInDst); + + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__); + } + triggerEvent = srcInDst; + + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + break; + + case ctet_UnitAreaPos: + { + if (movingUnit->getId() == event.sourceId) { + bool + srcInDst = false; + + // Cache area lookup so for each unitsize and pos its done only once + bool + foundInCache = false; + std::map < int, + std::map < + Vec2i, + bool > >::iterator + iterFind1 = + event.eventLookupCache.find(movingUnit-> + getType()-> + getSize()); + if (iterFind1 != event.eventLookupCache.end()) { + std::map < Vec2i, bool >::iterator iterFind2 = + iterFind1->second.find(movingUnit->getPos()); + if (iterFind2 != iterFind1->second.end()) { + foundInCache = true; + srcInDst = iterFind2->second; + } + } + + if (foundInCache == false) { + for (int x = event.destPos.x; + srcInDst == false && x <= event.destPosEnd.x; + ++x) { + for (int y = event.destPos.y; + srcInDst == false + && y <= event.destPosEnd.y; ++y) { + srcInDst = + world->getMap()-> + isInUnitTypeCells(movingUnit-> + getType(), Vec2i(x, + y), + movingUnit-> + getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + movingUnit-> + getId(), + event.type, + movingUnit-> + getPos(). + getString(). + c_str(), + event. + sourceId, + event.destId, + Vec2i(x, + y). + getString(). + c_str(), + srcInDst); + } + } + + event.eventLookupCache[movingUnit->getType()-> + getSize()][movingUnit-> + getPos()] = + srcInDst; + } + + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__); + } + triggerEvent = srcInDst; + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + break; + + case ctet_Faction: + { + Unit * + destUnit = world->findUnitById(event.destId); + if (destUnit != NULL && + movingUnit->getFactionIndex() == event.sourceId) { + bool + srcInDst = + world->getMap()->isInUnitTypeCells(destUnit-> + getType(), + destUnit-> + getPos(), + movingUnit-> + getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, srcInDst = %d\n", + __FILE__, __FUNCTION__, + __LINE__, + movingUnit->getId(), + event.type, + movingUnit->getPos(). + getString().c_str(), + event.sourceId, + event.destId, + event.destPos. + getString().c_str(), + srcInDst); + + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__); + } else { + srcInDst = + world->getMap()-> + isNextToUnitTypeCells(destUnit->getType(), + destUnit->getPos(), + movingUnit->getPos()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] movingUnit = %d, event.type = %d, movingUnit->getPos() = %s, event.sourceId = %d, event.destId = %d, event.destPos = %s, destUnit->getPos() = %s, srcInDst = %d\n", + __FILE__, + __FUNCTION__, + __LINE__, + movingUnit->getId(), + event.type, + movingUnit->getPos(). + getString().c_str(), + event.sourceId, + event.destId, + event.destPos. + getString().c_str(), + destUnit->getPos(). + getString().c_str(), + srcInDst); + } + triggerEvent = srcInDst; + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + break; + + case ctet_FactionPos: + { + if (movingUnit->getFactionIndex() == event.sourceId) { + //printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s]\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str()); + + bool + srcInDst = + world->getMap()->isInUnitTypeCells(movingUnit-> + getType(), + event.destPos, + movingUnit-> + getPos()); + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__); + } + triggerEvent = srcInDst; + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + break; + + case ctet_FactionAreaPos: + { + if (movingUnit->getFactionIndex() == event.sourceId) { + //if(event.sourceId == 1) printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s] Unit id = %d\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str(),movingUnit->getId()); + + bool + srcInDst = false; + + // Cache area lookup so for each unitsize and pos its done only once + bool + foundInCache = false; + std::map < int, + std::map < + Vec2i, + bool > >::iterator + iterFind1 = + event.eventLookupCache.find(movingUnit-> + getType()-> + getSize()); + if (iterFind1 != event.eventLookupCache.end()) { + std::map < Vec2i, bool >::iterator iterFind2 = + iterFind1->second.find(movingUnit->getPos()); + if (iterFind2 != iterFind1->second.end()) { + foundInCache = true; + srcInDst = iterFind2->second; + } + } + + if (foundInCache == false) { + for (int x = event.destPos.x; + srcInDst == false && x <= event.destPosEnd.x; + ++x) { + for (int y = event.destPos.y; + srcInDst == false + && y <= event.destPosEnd.y; ++y) { + + srcInDst = + world->getMap()-> + isInUnitTypeCells(movingUnit-> + getType(), Vec2i(x, + y), + movingUnit-> + getPos()); + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType + (SystemFlags::debugLUA).enabled) + SystemFlags:: + OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__); + } + } + } + + event.eventLookupCache[movingUnit->getType()-> + getSize()][movingUnit-> + getPos()] = + srcInDst; + } + + triggerEvent = srcInDst; + if (triggerEvent == true) { + //printf("!!!UNIT IN AREA!!! Faction area pos, moving unit faction= %d, trigger faction = %d, unit id = %d\n",movingUnit->getFactionIndex(),event.sourceId,movingUnit->getId()); + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + } + break; + + case ctet_AreaPos: + { + // Is the unit already in the cell range? If no check if they are entering it + if (event.eventStateInfo.find(movingUnit->getId()) == + event.eventStateInfo.end()) { + //printf("ctet_FactionPos event.destPos = [%s], movingUnit->getPos() [%s]\n",event.destPos.getString().c_str(),movingUnit->getPos().getString().c_str()); + + bool + srcInDst = false; + for (int x = event.destPos.x; + srcInDst == false && x <= event.destPosEnd.x; + ++x) { + for (int y = event.destPos.y; + srcInDst == false && y <= event.destPosEnd.y; + ++y) { + + srcInDst = + world->getMap()-> + isInUnitTypeCells(movingUnit->getType(), + Vec2i(x, y), + movingUnit->getPos()); + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__). + c_str(), + __FUNCTION__, + __LINE__); + + currentCellTriggeredEventAreaEntryUnitId = + movingUnit->getId(); + event.eventStateInfo[movingUnit-> + getId()] = + Vec2i(x, y).getString(); + } + } + } + triggerEvent = srcInDst; + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + } + // If unit is already in cell range check if they are leaving? + else { + bool + srcInDst = false; + for (int x = event.destPos.x; + srcInDst == false && x <= event.destPosEnd.x; + ++x) { + for (int y = event.destPos.y; + srcInDst == false && y <= event.destPosEnd.y; + ++y) { + + srcInDst = + world->getMap()-> + isInUnitTypeCells(movingUnit->getType(), + Vec2i(x, y), + movingUnit->getPos()); + if (srcInDst == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags:: + debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__). + c_str(), + __FUNCTION__, + __LINE__); + + //event.eventStateInfo[movingUnit->getId()] = Vec2i(x,y); + } + } + } + triggerEvent = (srcInDst == false); + if (triggerEvent == true) { + currentCellTriggeredEventUnitId = + movingUnit->getId(); + } + + if (triggerEvent == true) { + currentCellTriggeredEventAreaExitUnitId = + movingUnit->getId(); + + event.eventStateInfo.erase(movingUnit->getId()); + } + } + } + break; + + } + + if (triggerEvent == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__); + + currentCellTriggeredEventId = iterMap->first; + event.triggerCount++; + + luaScript.beginCall("cellTriggerEvent"); + luaScript.endCall(); + } + + // ScenarioInfo scenarioInfoEnd = world->getScenario()->getInfo(); + // if(scenarioInfoStart.file != scenarioInfoEnd.file) { + // break; + // } + } + } + + inCellTriggerEvent = false; + } + + // ========================== lua wrappers =============================================== + + string + ScriptManager::wrapString(const string & str, int wrapCount) { + string + returnString; + + int + letterCount = 0; + for (int i = 0; i < (int) str.size(); ++i) { + if (letterCount > wrapCount && str[i] == ' ') { + returnString += '\n'; + letterCount = 0; + } else { + returnString += str[i]; + } + ++letterCount; + } + + return returnString; + } + + void + ScriptManager::networkShowMessageForFaction(const string & text, + const string & header, + int factionIndex) { + messageQueue. + push_back(ScriptManagerMessage(text, header, factionIndex)); + thisScriptManager->onMessageBoxOk(false); + } + void + ScriptManager::networkShowMessageForTeam(const string & text, + const string & header, + int teamIndex) { + // Team indexes are 0 based internally (but 1 based in the lua script) so convert + teamIndex--; + messageQueue. + push_back(ScriptManagerMessage(text, header, -1, teamIndex)); + thisScriptManager->onMessageBoxOk(false); + } + + void + ScriptManager::networkSetCameraPositionForFaction(int factionIndex, + const Vec2i & pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (factionIndex == this->world->getThisFactionIndex()) { + gameCamera->centerXZ(pos.x, pos.y); + } + } + + void + ScriptManager::networkSetCameraPositionForTeam(int teamIndex, + const Vec2i & pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (teamIndex == this->world->getThisTeamIndex()) { + gameCamera->centerXZ(pos.x, pos.y); + } + } + + void + ScriptManager::showMessage(const string & text, const string & header) { + messageQueue.push_back(ScriptManagerMessage(text, header)); + thisScriptManager->onMessageBoxOk(false); + } + + void + ScriptManager::clearDisplayText() { + displayText = ""; + } + + void + ScriptManager::setDisplayText(const string & text) { + displayText = + wrapString(Lang::getInstance().getScenarioString(text), + displayTextWrapCount); + } + + void + ScriptManager::addConsoleText(const string & text) { + world->addConsoleText(text); + } + void + ScriptManager::addConsoleLangText(const char *fmt, ...) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + va_list + argList; + va_start(argList, fmt); + + const int + max_debug_buffer_size = 8096; + char + szBuf[max_debug_buffer_size] = ""; + vsnprintf(szBuf, max_debug_buffer_size - 1, fmt, argList); + + world->addConsoleTextWoLang(szBuf); + va_end(argList); + + } + + void + ScriptManager::DisplayFormattedText(const char *fmt, ...) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + va_list + argList; + va_start(argList, fmt); + + const int + max_debug_buffer_size = 8096; + char + szBuf[max_debug_buffer_size] = ""; + vsnprintf(szBuf, max_debug_buffer_size - 1, fmt, argList); + + displayText = szBuf; + + va_end(argList); + } + void + ScriptManager::DisplayFormattedLangText(const char *fmt, ...) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + va_list + argList; + va_start(argList, fmt); + + const int + max_debug_buffer_size = 8096; + char + szBuf[max_debug_buffer_size] = ""; + vsnprintf(szBuf, max_debug_buffer_size - 1, fmt, argList); + + displayText = szBuf; + + va_end(argList); + } + void + ScriptManager::setCameraPosition(const Vec2i & pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + gameCamera->centerXZ(pos.x, pos.y); + } + + void + ScriptManager::shakeCamera(int shakeIntensity, int shakeDuration, + bool cameraDistanceAffected, int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (cameraDistanceAffected) { + Unit * + unit = world->findUnitById(unitId); + if (unit) { + gameCamera->shake(shakeDuration, shakeIntensity, + cameraDistanceAffected, + unit->getCurrMidHeightVector()); + } + } else { + gameCamera->shake(shakeDuration, shakeIntensity, + cameraDistanceAffected, Vec3f(0.f, 0.f, 0.f)); + } + } + + void + ScriptManager::createUnit(const string & unitName, int factionIndex, + Vec2i pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + unitName.c_str(), factionIndex); + world->createUnit(unitName, factionIndex, pos); + } + + void + ScriptManager::createUnitNoSpacing(const string & unitName, + int factionIndex, Vec2i pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + unitName.c_str(), factionIndex); + world->createUnit(unitName, factionIndex, pos, false); + } + + void + ScriptManager::setLockedUnitForFaction(const string & unitName, + int factionIndex, bool lock) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + unitName.c_str(), factionIndex); + if (world->getFactionCount() > factionIndex) { + const UnitType * + ut = + world->getFaction(factionIndex)->getType()-> + getUnitType(unitName); + world->getFaction(factionIndex)->setLockedUnitForFaction(ut, + lock); + } else { + throw + megaglest_runtime_error + ("Invalid faction index in setLockedUnitForFaction: " + + intToStr(factionIndex), true); + } + } + + void + ScriptManager::destroyUnit(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, unitId); + Unit * + unit = world->findUnitById(unitId); + if (unit != NULL) { + // Make sure they die + bool + unit_dead = unit->decHp(unit->getHp() * unit->getHp()); + if (unit_dead == false) { + throw + megaglest_runtime_error("unit_dead == false", true); + } + unit->kill(); + // If called from an existing die event we get a stack overflow + //onUnitDied(unit); + } + } + void + ScriptManager::giveKills(int unitId, int amount) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, unitId); + Unit * + unit = world->findUnitById(unitId); + if (unit != NULL) { + for (int index = 1; index <= amount; ++index) { + unit->incKills(-1); + } + } + } + + void + ScriptManager::playStaticSound(const string & playSound) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playSound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playSound.c_str()); + world->playStaticSound(playSound); + } + void + ScriptManager::playStreamingSound(const string & playSound) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playSound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playSound.c_str()); + world->playStreamingSound(playSound); + } + + void + ScriptManager::stopStreamingSound(const string & playSound) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playSound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playSound.c_str()); + world->stopStreamingSound(playSound); + } + + void + ScriptManager::stopAllSound() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + world->stopAllSound(); + } + + void + ScriptManager::playStaticVideo(const string & playVideo) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playVideo [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playVideo.c_str()); + world->playStaticVideo(playVideo); + } + void + ScriptManager::playStreamingVideo(const string & playVideo) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playVideo [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playVideo.c_str()); + world->playStreamingVideo(playVideo); + } + + void + ScriptManager::stopStreamingVideo(const string & playVideo) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] playVideo [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + playVideo.c_str()); + world->stopStreamingVideo(playVideo); + } + + void + ScriptManager::stopAllVideo() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + world->stopAllVideo(); + } + + void + ScriptManager::togglePauseGame(int pauseStatus) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] pauseStatus = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + pauseStatus); + world->togglePauseGame((pauseStatus != 0), true); + } + + void + ScriptManager::morphToUnit(int unitId, const string & morphName, + int ignoreRequirements) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgradesIfRequired = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, unitId, + morphName.c_str(), ignoreRequirements); + + world->morphToUnit(unitId, morphName, (ignoreRequirements == 1)); + } + + void + ScriptManager::moveToUnit(int unitId, int destUnitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d] destUnitId [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, unitId, + destUnitId); + + world->moveToUnit(unitId, destUnitId); + } + + void + ScriptManager::giveResource(const string & resourceName, + int factionIndex, int amount) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveResource(resourceName, factionIndex, amount); + } + + void + ScriptManager::givePositionCommand(int unitId, + const string & commandName, + const Vec2i & pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->givePositionCommand(unitId, commandName, pos); + } + + void + ScriptManager::giveAttackCommand(int unitId, int unitToAttackId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveAttackCommand(unitId, unitToAttackId); + } + + void + ScriptManager::giveProductionCommand(int unitId, + const string & producedName) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveProductionCommand(unitId, producedName); + } + + void + ScriptManager::giveUpgradeCommand(int unitId, + const string & producedName) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveUpgradeCommand(unitId, producedName); + } + + void + ScriptManager::giveAttackStoppedCommand(int unitId, + const string & itemName, + int ignoreRequirements) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveAttackStoppedCommand(unitId, itemName, + (ignoreRequirements == 1)); + } + + void + ScriptManager::disableAi(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + playerModifiers[factionIndex].disableAi(); + disableConsume(factionIndex); // by this we stay somehow compatible with old behaviour + } + } + + void + ScriptManager::enableAi(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + playerModifiers[factionIndex].enableAi(); + } + } + + bool + ScriptManager::getAiEnabled(int factionIndex) { + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + return playerModifiers[factionIndex].getAiEnabled(); + } + return false; + } + + void + ScriptManager::disableConsume(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + playerModifiers[factionIndex].disableConsume(); + } + } + + void + ScriptManager::enableConsume(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + playerModifiers[factionIndex].enableConsume(); + } + } + + bool + ScriptManager::getConsumeEnabled(int factionIndex) { + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + return playerModifiers[factionIndex].getConsumeEnabled(); + } + return false; + } + + int + ScriptManager::registerCellTriggerEventForUnitToUnit(int sourceUnitId, + int destUnitId) { + CellTriggerEvent + trigger; + trigger.type = ctet_Unit; + trigger.sourceId = sourceUnitId; + trigger.destId = destUnitId; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching unit: %d, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceUnitId, destUnitId, eventId); + + return eventId; + } + + int + ScriptManager:: + registerCellTriggerEventForUnitToLocation(int sourceUnitId, + const Vec2i & pos) { + CellTriggerEvent + trigger; + trigger.type = ctet_UnitPos; + trigger.sourceId = sourceUnitId; + trigger.destPos = pos; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching pos: %s, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceUnitId, pos.getString().c_str(), + eventId); + + return eventId; + } + + int + ScriptManager:: + registerCellAreaTriggerEventForUnitToLocation(int sourceUnitId, + const Vec4i & pos) { + CellTriggerEvent + trigger; + trigger.type = ctet_UnitAreaPos; + trigger.sourceId = sourceUnitId; + trigger.destPos.x = pos.x; + trigger.destPos.y = pos.y; + trigger.destPosEnd.x = pos.z; + trigger.destPosEnd.y = pos.w; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] Unit: %d will trigger cell event when reaching pos: %s, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceUnitId, pos.getString().c_str(), + eventId); + + return eventId; + } + + int + ScriptManager:: + registerCellTriggerEventForFactionToUnit(int sourceFactionId, + int destUnitId) { + CellTriggerEvent + trigger; + trigger.type = ctet_Faction; + trigger.sourceId = sourceFactionId; + trigger.destId = destUnitId; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] Faction: %d will trigger cell event when reaching unit: %d, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceFactionId, destUnitId, eventId); + + return eventId; + } + + int + ScriptManager:: + registerCellTriggerEventForFactionToLocation(int sourceFactionId, + const Vec2i & pos) { + CellTriggerEvent + trigger; + trigger.type = ctet_FactionPos; + trigger.sourceId = sourceFactionId; + trigger.destPos = pos; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]Faction: %d will trigger cell event when reaching pos: %s, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceFactionId, pos.getString().c_str(), + eventId); + + return eventId; + } + + int + ScriptManager:: + registerCellAreaTriggerEventForFactionToLocation(int sourceFactionId, + const Vec4i & pos) { + CellTriggerEvent + trigger; + trigger.type = ctet_FactionAreaPos; + trigger.sourceId = sourceFactionId; + trigger.destPos.x = pos.x; + trigger.destPos.y = pos.y; + trigger.destPosEnd.x = pos.z; + trigger.destPosEnd.y = pos.w; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]Faction: %d will trigger cell event when reaching pos: %s, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + sourceFactionId, pos.getString().c_str(), + eventId); + + return eventId; + } + + int + ScriptManager::registerCellAreaTriggerEvent(const Vec4i & pos) { + CellTriggerEvent + trigger; + trigger.type = ctet_AreaPos; + trigger.sourceId = -1; + trigger.destPos.x = pos.x; + trigger.destPos.y = pos.y; + trigger.destPosEnd.x = pos.z; + trigger.destPosEnd.y = pos.w; + + int + eventId = currentEventId++; + CellTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] trigger cell event when reaching pos: %s, eventId = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + pos.getString().c_str(), eventId); + + return eventId; + } + + int + ScriptManager::getCellTriggerEventCount(int eventId) { + int + result = 0; + if (CellTriggerEventList.find(eventId) != CellTriggerEventList.end()) { + result = CellTriggerEventList[eventId].triggerCount; + } + + return result; + } + + void + ScriptManager::unregisterCellTriggerEvent(int eventId) { + if (CellTriggerEventList.find(eventId) != CellTriggerEventList.end()) { + if (inCellTriggerEvent == false) { + CellTriggerEventList.erase(eventId); + } else { + unRegisterCellTriggerEventList.push_back(eventId); + } + } + + if (inCellTriggerEvent == false) { + if (unRegisterCellTriggerEventList.empty() == false) { + for (int i = 0; + i < (int) unRegisterCellTriggerEventList.size(); ++i) { + int + delayedEventId = unRegisterCellTriggerEventList[i]; + CellTriggerEventList.erase(delayedEventId); + } + unRegisterCellTriggerEventList.clear(); + } + } + } + + int + ScriptManager::startTimerEvent() { + TimerTriggerEvent + trigger; + trigger.running = true; + //trigger.startTime = time(NULL); + trigger.startFrame = world->getFrameCount(); + //trigger.endTime = 0; + trigger.endFrame = 0; + + int + eventId = currentEventId++; + TimerTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + TimerTriggerEventList.size(), eventId, + (long long int) trigger.startFrame, + (long long int) trigger.endFrame); + + return eventId; + } + + int + ScriptManager::startEfficientTimerEvent(int triggerSecondsElapsed) { + TimerTriggerEvent + trigger; + trigger.running = true; + //trigger.startTime = time(NULL); + trigger.startFrame = world->getFrameCount(); + //trigger.endTime = 0; + trigger.endFrame = 0; + trigger.triggerSecondsElapsed = triggerSecondsElapsed; + + int + eventId = currentEventId++; + TimerTriggerEventList[eventId] = trigger; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + TimerTriggerEventList.size(), eventId, + (long long int) trigger.startFrame, + (long long int) trigger.endFrame); + + return eventId; + } + + int + ScriptManager::resetTimerEvent(int eventId) { + int + result = 0; + if (TimerTriggerEventList.find(eventId) != + TimerTriggerEventList.end()) { + TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; + result = getTimerEventSecondsElapsed(eventId); + + //trigger.startTime = time(NULL); + trigger.startFrame = world->getFrameCount(); + //trigger.endTime = 0; + trigger.endFrame = 0; + trigger.running = true; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld, result = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + TimerTriggerEventList.size(), eventId, + (long long int) trigger.startFrame, + (long long int) trigger.endFrame, + result); + } + return result; + } + + int + ScriptManager::stopTimerEvent(int eventId) { + int + result = 0; + if (TimerTriggerEventList.find(eventId) != + TimerTriggerEventList.end()) { + TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; + //trigger.endTime = time(NULL); + trigger.endFrame = world->getFrameCount(); + trigger.running = false; + result = getTimerEventSecondsElapsed(eventId); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] TimerTriggerEventList.size() = %d, eventId = %d, trigger.startTime = %lld, trigger.endTime = %lld, result = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + TimerTriggerEventList.size(), eventId, + (long long int) trigger.startFrame, + (long long int) trigger.endFrame, + result); + } + + return result; + } + + int + ScriptManager::getTimerEventSecondsElapsed(int eventId) { + int + result = 0; + if (TimerTriggerEventList.find(eventId) != + TimerTriggerEventList.end()) { + TimerTriggerEvent & trigger = TimerTriggerEventList[eventId]; + if (trigger.running) { + //result = (int)difftime(time(NULL),trigger.startTime); + result = + (world->getFrameCount() - + trigger.startFrame) / GameConstants::updateFps; + } else { + //result = (int)difftime(trigger.endTime,trigger.startTime); + result = + (trigger.endFrame - + trigger.startFrame) / GameConstants::updateFps; + } + + //printf("Timer event id = %d seconmds elapsed = %d\n",eventId,result); + } + + return result; + } + + void + ScriptManager::setPlayerAsWinner(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + if (factionIndex >= 0 && factionIndex < GameConstants::maxPlayers) { + playerModifiers[factionIndex].setAsWinner(); + } + } + + void + ScriptManager::endGame() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + gameOver = true; + } + + void + ScriptManager::startPerformanceTimer() { + + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#1 world->getGame() == NULL", true); + } + world->getGame()->startPerformanceTimer(); + + } + + void + ScriptManager::endPerformanceTimer() { + + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#2 world->getGame() == NULL", true); + } + world->getGame()->endPerformanceTimer(); + + } + + Vec2i + ScriptManager::getPerformanceTimerResults() { + + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#3 world->getGame() == NULL", true); + } + return world->getGame()->getPerformanceTimerResults(); + } + + Vec2i + ScriptManager::getStartLocation(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getStartLocation(factionIndex); + } + + + Vec2i + ScriptManager::getUnitPosition(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + Vec2i + result = world->getUnitPosition(unitId); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s] unitId = %d, pos [%s]\n", + __FUNCTION__, unitId, + result.getString().c_str()); + + return result; + } + + void + ScriptManager::setUnitPosition(int unitId, Vec2i pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->setUnitPosition(unitId, pos); + } + + void + ScriptManager::addCellMarker(Vec2i pos, int factionIndex, + const string & note, + const string & textureFile) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->addCellMarker(pos, factionIndex, note, textureFile); + } + + void + ScriptManager::removeCellMarker(Vec2i pos, int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->removeCellMarker(pos, factionIndex); + } + + void + ScriptManager::showMarker(Vec2i pos, int factionIndex, + const string & note, + const string & textureFile, int flashCount) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->showMarker(pos, factionIndex, note, textureFile, flashCount); + } + + int + ScriptManager::getIsUnitAlive(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getIsUnitAlive(unitId); + } + + int + ScriptManager::getUnitFaction(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getUnitFactionIndex(unitId); + } + const string + ScriptManager::getUnitName(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + string + result = ""; + Unit * + unit = world->findUnitById(unitId); + if (unit) { + result = world->findUnitById(unitId)->getType()->getName(false); + } + return result; + } + + const string + ScriptManager::getUnitDisplayName(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getUnitName(unitId); + } + + int + ScriptManager::getResourceAmount(const string & resourceName, + int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getResourceAmount(resourceName, factionIndex); + } + + const + string & + ScriptManager::getLastCreatedUnitName() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastCreatedUnitName; + } + + int + ScriptManager::getCellTriggeredEventId() { + + return currentCellTriggeredEventId; + } + + int + ScriptManager::getTimerTriggeredEventId() { + + return currentTimerTriggeredEventId; + } + + int + ScriptManager::getCellTriggeredEventAreaEntryUnitId() { + + return currentCellTriggeredEventAreaEntryUnitId; + } + + int + ScriptManager::getCellTriggeredEventAreaExitUnitId() { + + return currentCellTriggeredEventAreaExitUnitId; + } + + int + ScriptManager::getCellTriggeredEventUnitId() { + + return currentCellTriggeredEventUnitId; + } + + void + ScriptManager::setRandomGenInit(int seed) { + + random.init(seed); + } + + int + ScriptManager::getRandomGen(int minVal, int maxVal) { + + return random.randRange(minVal, maxVal); + } + + int + ScriptManager::getWorldFrameCount() { + + return world->getFrameCount(); + } + + bool + ScriptManager::getGameWon() const { + + return + gameWon; + } + + bool + ScriptManager::getIsGameOver() const { + + return + gameOver; + } + + int + ScriptManager::getLastCreatedUnitId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastCreatedUnitId; + } + + const + string & + ScriptManager::getLastDeadUnitName() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastDeadUnitName; + } + + int + ScriptManager::getLastDeadUnitId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastDeadUnitId; + } + + int + ScriptManager::getLastDeadUnitCauseOfDeath() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastDeadUnitCauseOfDeath; + } + + const + string & + ScriptManager::getLastDeadUnitKillerName() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastDeadUnitKillerName; + } + + int + ScriptManager::getLastDeadUnitKillerId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastDeadUnitKillerId; + } + + const + string & + ScriptManager::getLastAttackedUnitName() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastAttackedUnitName; + } + + int + ScriptManager::getLastAttackedUnitId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastAttackedUnitId; + } + + const + string & + ScriptManager::getLastAttackingUnitName() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastAttackingUnitName; + } + + int + ScriptManager::getLastAttackingUnitId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return lastAttackingUnitId; + } + + int + ScriptManager::getUnitCount(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getUnitCount(factionIndex); + } + + int + ScriptManager::getUnitCountOfType(int factionIndex, + const string & typeName) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getUnitCountOfType(factionIndex, typeName); + } + + const string + ScriptManager::getSystemMacroValue(const string & key) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getSystemMacroValue(key); + } + + const string + ScriptManager::getPlayerName(int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getPlayerName(factionIndex); + } + + void + ScriptManager::loadScenario(const string & name, bool keepFactions) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + world->setQueuedScenario(name, keepFactions); + } + + vector < int > + ScriptManager::getUnitsForFaction(int factionIndex, + const string & commandTypeName, + int field) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + return world->getUnitsForFaction(factionIndex, commandTypeName, field); + } + + int + ScriptManager::getUnitCurrentField(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + return world->getUnitCurrentField(unitId); + } + + int + ScriptManager::isFreeCellsOrHasUnit(int field, int unitId, Vec2i pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + Unit * + unit = world->findUnitById(unitId); + if (unit == NULL) { + throw + megaglest_runtime_error("unit == NULL", true); + } + int + result = + world->getMap()->isFreeCellsOrHasUnit(pos, + unit->getType()->getSize(), + static_cast (field), + unit); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s] unitId = %d, [%s] pos [%s] field = %d result = %d\n", + __FUNCTION__, unitId, + unit->getType()->getName(false).c_str(), + pos.getString().c_str(), field, result); + + return result; + } + + int + ScriptManager::isFreeCells(int unitSize, int field, Vec2i pos) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + int + result = + world->getMap()->isFreeCellsOrHasUnit(pos, unitSize, + static_cast (field), + NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s] unitSize = %d, pos [%s] field = %d result = %d\n", + __FUNCTION__, unitSize, + pos.getString().c_str(), field, result); + + return result; + } + + int + ScriptManager::getHumanFactionId() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return this->world->getThisFactionIndex(); + } + + void + ScriptManager::highlightUnit(int unitId, float radius, float thickness, + Vec4f color) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + world->highlightUnit(unitId, radius, thickness, color); + } + + void + ScriptManager::unhighlightUnit(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + world->unhighlightUnit(unitId); + } + + void + ScriptManager::giveStopCommand(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->giveStopCommand(unitId); + } + + bool + ScriptManager::selectUnit(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->selectUnit(unitId); + } + + void + ScriptManager::unselectUnit(int unitId) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->unselectUnit(unitId); + } + + void + ScriptManager::addUnitToGroupSelection(int unitId, int groupIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->addUnitToGroupSelection(unitId, groupIndex); + } + void + ScriptManager::recallGroupSelection(int groupIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->recallGroupSelection(groupIndex); + } + void + ScriptManager::removeUnitFromGroupSelection(int unitId, int groupIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->removeUnitFromGroupSelection(unitId, groupIndex); + } + + void + ScriptManager::setAttackWarningsEnabled(bool enabled) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + world->setAttackWarningsEnabled(enabled); + } + + bool + ScriptManager::getAttackWarningsEnabled() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + return world->getAttackWarningsEnabled(); + } + + void + ScriptManager::registerUnitTriggerEvent(int unitId) { + UnitTriggerEventList[unitId] = utet_None; + } + + void + ScriptManager::unregisterUnitTriggerEvent(int unitId) { + UnitTriggerEventList.erase(unitId); + } + + int + ScriptManager::getLastUnitTriggerEventUnitId() { + return lastUnitTriggerEventUnitId; + } + UnitTriggerEventType + ScriptManager::getLastUnitTriggerEventType() { + return lastUnitTriggerEventType; + } + + int + ScriptManager::getUnitProperty(int unitId, UnitTriggerEventType type) { + int + result = -1; + + //printf("File: %s line: %d type: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,type); + + Unit * + unit = world->findUnitById(unitId); + if (unit != NULL) { + switch (type) { + case utet_None: + result = -2; + break; + case utet_HPChanged: + result = unit->getHp(); + break; + case utet_EPChanged: + result = unit->getEp(); + break; + case utet_LevelChanged: + result = -3; + if (unit->getLevel() != NULL) { + result = unit->getLevel()->getKills(); + } + break; + case utet_FieldChanged: + result = unit->getCurrField(); + break; + case utet_SkillChanged: + result = -4; + if (unit->getCurrSkill() != NULL) { + result = unit->getCurrSkill()->getClass(); + } + break; + default: + result = -1000; + break; + } + } + //printf("File: %s line: %d result: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,result); + return result; + } + const string + ScriptManager::getUnitPropertyName(int unitId, UnitTriggerEventType type) { + string + result = ""; + Unit * + unit = world->findUnitById(unitId); + if (unit != NULL) { + switch (type) { + case utet_None: + result = ""; + break; + case utet_HPChanged: + result = ""; + break; + case utet_EPChanged: + result = ""; + break; + case utet_LevelChanged: + result = ""; + if (unit->getLevel() != NULL) { + result = unit->getLevel()->getName(false); + } + break; + case utet_FieldChanged: + result = ""; + break; + case utet_SkillChanged: + result = ""; + if (unit->getCurrSkill() != NULL) { + result = unit->getCurrSkill()->getName(); + } + break; + default: + result = "???"; + break; + } + } + return result; + } + + void + ScriptManager::onUnitTriggerEvent(const Unit * unit, + UnitTriggerEventType event) { + //static bool inEvent = false; + //if(inEvent == true) { + // printf("\n\n!!!!!!!!!!!!!!! File: %s line: %d unit [%d - %s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__,unit->getId(),unit->getType()->getName().c_str()); + // return; + //} + //inEvent = true; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + if (UnitTriggerEventList.empty() == false) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + std::map < int, + UnitTriggerEventType >::iterator + iterFind = UnitTriggerEventList.find(unit->getId()); + if (iterFind != UnitTriggerEventList.end()) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + + lastUnitTriggerEventUnitId = unit->getId(); + lastUnitTriggerEventType = event; + + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + + luaScript.beginCall("unitTriggerEvent"); + luaScript.endCall(); + + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + } + //inEvent = false; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + + void + ScriptManager::registerDayNightEvent() { + registeredDayNightEvent = true; + } + + void + ScriptManager::unregisterDayNightEvent() { + registeredDayNightEvent = false; + } + + void + ScriptManager::onDayNightTriggerEvent() { + if (registeredDayNightEvent == true) { + bool + isDay = (this->getIsDayTime() == 1); + if ((lastDayNightTriggerStatus != 1 && isDay == true) || + (lastDayNightTriggerStatus != 2 && isDay == false)) { + if (isDay == true) { + lastDayNightTriggerStatus = 1; + } else { + lastDayNightTriggerStatus = 2; + } + + printf("Triggering daynight event isDay: %d [%f]\n", isDay, + getTimeOfDay()); + + luaScript.beginCall("dayNightTriggerEvent"); + luaScript.endCall(); + } + } + } + + int + ScriptManager::getIsDayTime() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + const TimeFlow * + tf = world->getTimeFlow(); + if (tf == NULL) { + throw + megaglest_runtime_error("#1 tf == NULL", true); + } + return tf->isDay(); + } + int + ScriptManager::getIsNightTime() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + const TimeFlow * + tf = world->getTimeFlow(); + if (tf == NULL) { + throw + megaglest_runtime_error("#2 tf == NULL", true); + } + return tf->isNight(); + } + float + ScriptManager::getTimeOfDay() { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + + const TimeFlow * + tf = world->getTimeFlow(); + if (tf == NULL) { + throw + megaglest_runtime_error("#3 tf == NULL", true); + } + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + return tf->getTime(); + } + + void + ScriptManager::disableSpeedChange() { + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#4 world->getGame() == NULL"); + } + world->getGame()->setDisableSpeedChange(true); + } + void + ScriptManager::enableSpeedChange() { + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#5 world->getGame() == NULL"); + } + world->getGame()->setDisableSpeedChange(false); + } + + bool + ScriptManager::getSpeedChangeEnabled() { + if (world->getGame() == NULL) { + throw + megaglest_runtime_error("#6 world->getGame() == NULL"); + } + return world->getGame()->getDisableSpeedChange(); + } + + void + ScriptManager::addMessageToQueue(ScriptManagerMessage msg) { + messageQueue.push_back(msg); + } + + void + ScriptManager::storeSaveGameData(string name, string value) { + + if (LuaScript::getDebugModeEnabled() == true) + printf("storeSaveGameData name [%s] value [%s]\n", name.c_str(), + value.c_str()); + + luaSavedGameData[name] = value; + } + + string + ScriptManager::loadSaveGameData(string name) { + string + value = luaSavedGameData[name]; + + if (LuaScript::getDebugModeEnabled() == true) + printf("loadSaveGameData result name [%s] value [%s]\n", + name.c_str(), value.c_str()); + + return value; + } + + ControlType + ScriptManager::getFactionPlayerType(int factionIndex) { + Faction * + faction = world->getFaction(factionIndex); + if (faction != NULL) { + return faction->getControlType(); + } + return ctClosed; + } + // ========================== lua callbacks =============================================== + + int + ScriptManager::showMessage(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + thisScriptManager->showMessage(luaArguments.getString(-2), + luaArguments.getString(-1)); + return luaArguments.getReturnCount(); + } + + int + ScriptManager::networkShowMessageForFaction(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->networkShowMessageForFaction(luaArguments. + getString(-3), + luaArguments. + getString(-2), + luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::networkShowMessageForTeam(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->networkShowMessageForTeam(luaArguments. + getString(-3), + luaArguments. + getString(-2), + luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::networkSetCameraPositionForFaction(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->networkSetCameraPositionForFaction(luaArguments. + getInt(-2), + luaArguments. + getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::networkSetCameraPositionForTeam(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->networkSetCameraPositionForTeam(luaArguments. + getInt(-2), + luaArguments. + getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + + int + ScriptManager::setDisplayText(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->setDisplayText(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::addConsoleText(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->addConsoleText(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::clearDisplayText(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->clearDisplayText(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setCameraPosition(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager-> + setCameraPosition(Vec2i(luaArguments.getVec2i(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::shakeCamera(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->shakeCamera(luaArguments.getInt(-2), + luaArguments.getInt(-1), false, 0); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::shakeCameraOnUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + thisScriptManager->shakeCamera(luaArguments.getInt(-3), + luaArguments.getInt(-2), true, + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::createUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-3).c_str(), + luaArguments.getInt(-2)); + + try { + thisScriptManager->createUnit(luaArguments.getString(-3), + luaArguments.getInt(-2), + luaArguments.getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + void + ScriptManager::error(LuaHandle * luaHandle, + const megaglest_runtime_error * mgErr, + const char *file, const char *function, int line) { + char + szErrBuf[8096] = ""; + char + szErrBuf2[8096] = ""; + + int + luaLine = -1; + const char * + luaSource = ""; + + if (luaHandle != NULL) { + lua_Debug + ar; + lua_getstack(luaHandle, 1, &ar); + lua_getinfo(luaHandle, "nSl", &ar); + + luaLine = ar.currentline; + luaSource = ar.source; + + } + snprintf(szErrBuf, 8096, "in %s::%s %d ", + extractFileFromDirectoryPath(file).c_str(), function, line); + snprintf(szErrBuf2, 8096, "Lua: tag=<%s> line=%d ", luaSource, + luaLine - 1); + string + sErrBuf = string("Error! The game may no longer be stable!\n\n") + + string(szErrBuf) + "\n" + string(szErrBuf2) + "\n\n" + + string(mgErr->what()); + + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, sErrBuf.c_str()); + + thisScriptManager-> + addMessageToQueue(ScriptManagerMessage + (sErrBuf.c_str(), + "error " + + intToStr(thisScriptManager->errorCount), -1, -1, + true)); + thisScriptManager->errorCount++; + thisScriptManager->onMessageBoxOk(false); + } + + int + ScriptManager::createUnitNoSpacing(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-3).c_str(), + luaArguments.getInt(-2)); + + try { + thisScriptManager->createUnitNoSpacing(luaArguments.getString(-3), + luaArguments.getInt(-2), + luaArguments.getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::destroyUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getInt(-1)); + + try { + thisScriptManager->destroyUnit(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setLockedUnitForFaction(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%s] factionIndex = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-3).c_str(), + luaArguments.getInt(-2)); + + try { + thisScriptManager->setLockedUnitForFaction(luaArguments. + getString(-3), + luaArguments.getInt(-2), + (luaArguments. + getInt(-1) == + 0 ? false : true)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveKills(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getInt(-1)); + + try { + thisScriptManager->giveKills(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::morphToUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d] morphName [%s] forceUpgrade = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getInt(-3), + luaArguments.getString(-2).c_str(), + luaArguments.getInt(-1)); + + try { + thisScriptManager->morphToUnit(luaArguments.getInt(-3), + luaArguments.getString(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::moveToUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] unit [%d] dest unit [%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getInt(-2), + luaArguments.getInt(-1)); + + try { + thisScriptManager->moveToUnit(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::playStaticSound(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->playStaticSound(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::playStreamingSound(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->playStreamingSound(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::stopStreamingSound(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->stopStreamingSound(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::stopAllSound(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + thisScriptManager->stopAllSound(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::playStaticVideo(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->playStaticVideo(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::playStreamingVideo(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->playStreamingVideo(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::stopStreamingVideo(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] sound [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getString(-1).c_str()); + + try { + thisScriptManager->stopStreamingVideo(luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::stopAllVideo(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + thisScriptManager->stopAllVideo(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::togglePauseGame(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] value = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + luaArguments.getInt(-1)); + + try { + thisScriptManager->togglePauseGame(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::giveResource(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveResource(luaArguments.getString(-3), + luaArguments.getInt(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::givePositionCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->givePositionCommand(luaArguments.getInt(-3), + luaArguments.getString(-2), + luaArguments.getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveAttackCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveAttackCommand(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveProductionCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveProductionCommand(luaArguments.getInt(-2), + luaArguments. + getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveUpgradeCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveUpgradeCommand(luaArguments.getInt(-2), + luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveAttackStoppedCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveAttackStoppedCommand(luaArguments.getInt(-3), + luaArguments. + getString(-2), + luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::disableAi(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->disableAi(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::enableAi(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->enableAi(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getAiEnabled(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + bool + result = thisScriptManager->getAiEnabled(luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::disableConsume(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->disableConsume(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::enableConsume(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->enableConsume(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getConsumeEnabled(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + bool + result = + thisScriptManager->getConsumeEnabled(luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellTriggerEventForUnitToUnit(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellTriggerEventForUnitToUnit(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellTriggerEventForUnitToLocation(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellTriggerEventForUnitToLocation(luaArguments.getInt(-2), + luaArguments. + getVec2i(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellAreaTriggerEventForUnitToLocation(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellAreaTriggerEventForUnitToLocation(luaArguments. + getInt(-2), + luaArguments. + getVec4i(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellTriggerEventForFactionToUnit(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellTriggerEventForFactionToUnit(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellTriggerEventForFactionToLocation(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellTriggerEventForFactionToLocation(luaArguments. + getInt(-2), + luaArguments. + getVec2i(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager:: + registerCellAreaTriggerEventForFactionToLocation(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager-> + registerCellAreaTriggerEventForFactionToLocation(luaArguments. + getInt(-2), + luaArguments. + getVec4i(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerCellAreaTriggerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->registerCellAreaTriggerEvent(luaArguments. + getVec4i(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getCellTriggerEventCount(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->getCellTriggerEventCount(luaArguments. + getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::unregisterCellTriggerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->unregisterCellTriggerEvent(luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::startTimerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = thisScriptManager->startTimerEvent(); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::startEfficientTimerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->startEfficientTimerEvent(luaArguments. + getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::stopTimerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->stopTimerEvent(luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::resetTimerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->resetTimerEvent(luaArguments.getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getTimerEventSecondsElapsed(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + result = + thisScriptManager->getTimerEventSecondsElapsed(luaArguments. + getInt(-1)); + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setPlayerAsWinner(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->setPlayerAsWinner(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::endGame(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->endGame(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::startPerformanceTimer(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->startPerformanceTimer(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::endPerformanceTimer(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->endPerformanceTimer(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getPerformanceTimerResults(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + Vec2i + results = thisScriptManager->getPerformanceTimerResults(); + luaArguments.returnVec2i(results); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getStartLocation(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + Vec2i + pos = + thisScriptManager->getStartLocation(luaArguments.getInt(-1)); + luaArguments.returnVec2i(pos); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitPosition(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + Vec2i + pos = thisScriptManager->getUnitPosition(luaArguments.getInt(-1)); + luaArguments.returnVec2i(pos); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setUnitPosition(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->setUnitPosition(luaArguments.getInt(-2), + luaArguments.getVec2i(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::addCellMarker(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + //printf("LUA addCellMarker --> START\n"); + + int + factionIndex = luaArguments.getInt(-4); + + //printf("LUA addCellMarker --> START 1\n"); + + Vec2i + pos = luaArguments.getVec2i(-1); + + //printf("LUA addCellMarker --> START 2\n"); + + string + note = luaArguments.getString(-3); + + //printf("LUA addCellMarker --> START 3\n"); + + string + texture = luaArguments.getString(-2); + + //printf("LUA addCellMarker --> faction [%d] pos [%s] note [%s] texture [%s]\n",factionIndex,pos.getString().c_str(),note.c_str(),texture.c_str()); + + thisScriptManager->addCellMarker(pos, factionIndex, note, texture); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::removeCellMarker(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + int + factionIndex = luaArguments.getInt(-2); + Vec2i + pos = luaArguments.getVec2i(-1); + + thisScriptManager->removeCellMarker(pos, factionIndex); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::showMarker(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + int + flashCount = luaArguments.getInt(-5); + //printf("LUA addCellMarker --> START\n"); + + int + factionIndex = luaArguments.getInt(-4); + + //printf("LUA addCellMarker --> START 1\n"); + + Vec2i + pos = luaArguments.getVec2i(-1); + + //printf("LUA addCellMarker --> START 2\n"); + + string + note = luaArguments.getString(-3); + + //printf("LUA addCellMarker --> START 3\n"); + + string + texture = luaArguments.getString(-2); + + //printf("LUA addCellMarker --> faction [%d] pos [%s] note [%s] texture [%s]\n",factionIndex,pos.getString().c_str(),note.c_str(),texture.c_str()); + + thisScriptManager->showMarker(pos, factionIndex, note, texture, + flashCount); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitFaction(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + factionIndex = + thisScriptManager->getUnitFaction(luaArguments.getInt(-1)); + luaArguments.returnInt(factionIndex); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + const string + unitname = + thisScriptManager->getUnitName(luaArguments.getInt(-1)); + luaArguments.returnString(unitname); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitDisplayName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + const string + unitname = + thisScriptManager->getUnitDisplayName(luaArguments.getInt(-1)); + luaArguments.returnString(unitname); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + + int + ScriptManager::getResourceAmount(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getResourceAmount(luaArguments. + getString(-2), + luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastCreatedUnitName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getLastCreatedUnitName()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastCreatedUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getLastCreatedUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getCellTriggeredEventId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getCellTriggeredEventId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getTimerTriggeredEventId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getTimerTriggeredEventId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getCellTriggeredEventAreaEntryUnitId(LuaHandle * + luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getCellTriggeredEventAreaEntryUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getCellTriggeredEventAreaExitUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getCellTriggeredEventAreaExitUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getCellTriggeredEventUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getCellTriggeredEventUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setRandomGenInit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->setRandomGenInit(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getRandomGen(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getRandomGen(luaArguments.getInt(-2), + luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getWorldFrameCount(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getWorldFrameCount()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastDeadUnitName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager->getLastDeadUnitName()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastDeadUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getLastDeadUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastDeadUnitCauseOfDeath(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getLastDeadUnitCauseOfDeath()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastDeadUnitKillerName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getLastDeadUnitKillerName()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastDeadUnitKillerId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getLastDeadUnitKillerId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastAttackedUnitName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getLastAttackedUnitName()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastAttackedUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getLastAttackedUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastAttackingUnitName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getLastAttackingUnitName()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getLastAttackingUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getLastAttackingUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getSystemMacroValue(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getSystemMacroValue(luaArguments. + getString(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::scenarioDir(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getSystemMacroValue("$SCENARIO_PATH")); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getPlayerName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + getPlayerName(luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitCount(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getUnitCount(luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitCountOfType(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getUnitCountOfType(luaArguments.getInt(-2), + luaArguments. + getString(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::DisplayFormattedText(LuaHandle * luaHandle) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + //const char *ret; + //lua_lock(luaHandle); + //luaC_checkGC(luaHandle); + + int + args = lua_gettop(luaHandle); + if (lua_checkstack(luaHandle, args + 1)) { + LuaArguments + luaArguments(luaHandle); + string + fmt = luaArguments.getString(-args); + //va_list argList; + //va_start(argList, fmt.c_str() ); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "DisplayFormattedText args = %d!\n", + args); + + const int + max_args_allowed = 8; + if (args == 1) { + thisScriptManager->DisplayFormattedText(fmt.c_str()); + } else if (args == 2) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + + 1)); + } else if (args == 3) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + + 2)); + } else if (args == 4) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + + 3)); + } else if (args == 5) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + + 4)); + } else if (args == 6) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + + 5)); + } else if (args == 7) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + 5), + luaArguments. + getGenericData(-args + + + 6)); + } else if (args == max_args_allowed) { + thisScriptManager->DisplayFormattedText(fmt.c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + 5), + luaArguments. + getGenericData(-args + + 6), + luaArguments. + getGenericData(-args + + + 7)); + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", + __FUNCTION__, args, max_args_allowed); + throw + megaglest_runtime_error(szBuf); + } + + //va_end(argList); + } + //lua_unlock(luaHandle); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return 1; + + /* + int args=lua_gettop(luaHandle); + if(lua_checkstack(luaHandle, args+1)) + { + va_list argList; + int i; + //lua_getfield(luaHandle, LUA_GLOBALSINDEX, "print"); + for(i = 1;i <= args; i++) { + lua_pushvalue(luaHandle, i); + } + lua_call(luaHandle, args, 0); + } + else + { + return luaL_error(luaHandle, "cannot grow stack"); + } + */ + + /* + luax_getfunction(L, mod, fn); + for (int i = 0; i < n; i++) { + lua_pushvalue(L, idxs[i]); // The arguments. + } + lua_call(L, n, 1); // Call the function, n args, one return value. + lua_replace(L, idxs[0]); // Replace the initial argument with the new object. + return 0; + */ + } + + int + ScriptManager::addConsoleLangText(LuaHandle * luaHandle) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + //const char *ret; + //lua_lock(luaHandle); + //luaC_checkGC(luaHandle); + + int + args = lua_gettop(luaHandle); + if (lua_checkstack(luaHandle, args + 1)) { + LuaArguments + luaArguments(luaHandle); + string + fmt = luaArguments.getString(-args); + //va_list argList; + //va_start(argList, fmt.c_str() ); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "DisplayFormattedText args = %d!\n", + args); + + const int + max_args_allowed = 8; + if (args == 1) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str()); + } else if (args == 2) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1)); + } else if (args == 3) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2)); + } else if (args == 4) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3)); + } else if (args == 5) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4)); + } else if (args == 6) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + 5)); + } else if (args == 7) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + 5), + luaArguments. + getGenericData(-args + + 6)); + } else if (args == max_args_allowed) { + thisScriptManager->addConsoleLangText(Lang::getInstance(). + getScenarioString + (fmt).c_str(), + luaArguments. + getGenericData(-args + + 1), + luaArguments. + getGenericData(-args + + 2), + luaArguments. + getGenericData(-args + + 3), + luaArguments. + getGenericData(-args + + 4), + luaArguments. + getGenericData(-args + + 5), + luaArguments. + getGenericData(-args + + 6), + luaArguments. + getGenericData(-args + + 7)); + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", + __FUNCTION__, args, max_args_allowed); + throw + megaglest_runtime_error(szBuf); + } + + //va_end(argList); + } + //lua_unlock(luaHandle); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return 1; + } + + int + ScriptManager::DisplayFormattedLangText(LuaHandle * luaHandle) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + try { + //const char *ret; + //lua_lock(luaHandle); + //luaC_checkGC(luaHandle); + + int + args = lua_gettop(luaHandle); + if (lua_checkstack(luaHandle, args + 1)) { + LuaArguments + luaArguments(luaHandle); + string + fmt = luaArguments.getString(-args); + //va_list argList; + //va_start(argList, fmt.c_str() ); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "DisplayFormattedText args = %d!\n", + args); + + const int + max_args_allowed = 8; + if (args == 1) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str()); + } else if (args == 2) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1)); + } else if (args == 3) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2)); + } else if (args == 4) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2), + luaArguments. + getGenericData(-args + 3)); + } else if (args == 5) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2), + luaArguments. + getGenericData(-args + 3), + luaArguments. + getGenericData(-args + 4)); + } else if (args == 6) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2), + luaArguments. + getGenericData(-args + 3), + luaArguments. + getGenericData(-args + 4), + luaArguments. + getGenericData(-args + 5)); + } else if (args == 7) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2), + luaArguments. + getGenericData(-args + 3), + luaArguments. + getGenericData(-args + 4), + luaArguments. + getGenericData(-args + 5), + luaArguments. + getGenericData(-args + 6)); + } else if (args == max_args_allowed) { + thisScriptManager-> + DisplayFormattedLangText(Lang::getInstance(). + getScenarioString(fmt).c_str(), + luaArguments. + getGenericData(-args + 1), + luaArguments. + getGenericData(-args + 2), + luaArguments. + getGenericData(-args + 3), + luaArguments. + getGenericData(-args + 4), + luaArguments. + getGenericData(-args + 5), + luaArguments. + getGenericData(-args + 6), + luaArguments. + getGenericData(-args + 7)); + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]", + __FUNCTION__, args, max_args_allowed); + throw + megaglest_runtime_error(szBuf); + } + + //va_end(argList); + } + //lua_unlock(luaHandle); + + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return 1; + } + + int + ScriptManager::getGameWon(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getGameWon()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getIsGameOver(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getIsGameOver()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::loadScenario(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->loadScenario(luaArguments.getString(-2), + luaArguments.getInt(-1) != 0); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitsForFaction(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + vector < int > + units = + thisScriptManager->getUnitsForFaction(luaArguments.getInt(-3), + luaArguments.getString(-2), + luaArguments.getInt(-1)); + luaArguments.returnVectorInt(units); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + + } + + int + ScriptManager::getUnitCurrentField(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getUnitCurrentField(luaArguments. + getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getIsUnitAlive(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getIsUnitAlive(luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::isFreeCellsOrHasUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + int + result = + thisScriptManager->isFreeCellsOrHasUnit(luaArguments.getInt(-3), + luaArguments.getInt(-2), + luaArguments. + getVec2i(-1)); + + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::isFreeCells(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + + try { + int + result = thisScriptManager->isFreeCells(luaArguments.getInt(-3), + luaArguments.getInt(-2), + luaArguments. + getVec2i(-1)); + + luaArguments.returnInt(result); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getHumanFactionId(LuaHandle * luaHandle) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getHumanFactionId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::highlightUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->highlightUnit(luaArguments.getInt(-4), + luaArguments.getFloat(-3), + luaArguments.getFloat(-2), + luaArguments.getVec4f(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::unhighlightUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->unhighlightUnit(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::giveStopCommand(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->giveStopCommand(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::selectUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + selectUnit(luaArguments.getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::unselectUnit(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->unselectUnit(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::addUnitToGroupSelection(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->addUnitToGroupSelection(luaArguments.getInt(-2), + luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::recallGroupSelection(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->recallGroupSelection(luaArguments.getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::removeUnitFromGroupSelection(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->removeUnitFromGroupSelection(luaArguments. + getInt(-2), + luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::setAttackWarningsEnabled(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager-> + setAttackWarningsEnabled((luaArguments.getInt(-1) == + 0 ? false : true)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getAttackWarningsEnabled(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getAttackWarningsEnabled()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getIsDayTime(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getIsDayTime()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getIsNightTime(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getIsNightTime()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getTimeOfDay(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnFloat(thisScriptManager->getTimeOfDay()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerDayNightEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->registerDayNightEvent(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::unregisterDayNightEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->unregisterDayNightEvent(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::registerUnitTriggerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->registerUnitTriggerEvent(luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::unregisterUnitTriggerEvent(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->unregisterUnitTriggerEvent(luaArguments. + getInt(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getLastUnitTriggerEventUnitId(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getLastUnitTriggerEventUnitId()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getLastUnitTriggerEventType(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(static_cast < + int>(thisScriptManager-> + getLastUnitTriggerEventType())); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getUnitProperty(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + int + value = + thisScriptManager->getUnitProperty(luaArguments.getInt(-2), + static_cast < + UnitTriggerEventType> + (luaArguments.getInt(-1))); + luaArguments.returnInt(value); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getUnitPropertyName(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + const string + unitname = + thisScriptManager->getUnitPropertyName(luaArguments.getInt(-2), + static_cast < + UnitTriggerEventType> + (luaArguments.getInt(-1))); + luaArguments.returnString(unitname); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::disableSpeedChange(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->disableSpeedChange(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::enableSpeedChange(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->enableSpeedChange(); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + int + ScriptManager::getSpeedChangeEnabled(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager->getSpeedChangeEnabled()); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::storeSaveGameData(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + thisScriptManager->storeSaveGameData(luaArguments.getString(-2), + luaArguments.getString(-1)); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::loadSaveGameData(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnString(thisScriptManager-> + loadSaveGameData(luaArguments. + getString(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + } + + int + ScriptManager::getFactionPlayerType(LuaHandle * luaHandle) { + LuaArguments + luaArguments(luaHandle); + try { + luaArguments.returnInt(thisScriptManager-> + getFactionPlayerType(luaArguments. + getInt(-1))); + } catch (const megaglest_runtime_error & ex) { + error(luaHandle, &ex, __FILE__, __FUNCTION__, __LINE__); + } + + return luaArguments.getReturnCount(); + + } + + void + ScriptManager::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode * + scriptManagerNode = rootNode->addChild("ScriptManager"); + + //lua + // string code; + scriptManagerNode->addAttribute("code", code, mapTagReplacements); + // LuaScript luaScript; + + luaScript.beginCall("onSave"); + luaScript.endCall(); + + if (LuaScript::getDebugModeEnabled() == true) + printf("After onSave luaSavedGameData.size() = %d\n", + (int) luaSavedGameData.size()); + for (std::map < string, string >::iterator iterMap = + luaSavedGameData.begin(); iterMap != luaSavedGameData.end(); + ++iterMap) { + + XmlNode * + savedGameDataItemNode = + scriptManagerNode->addChild("SavedGameDataItem"); + + savedGameDataItemNode->addAttribute("key", iterMap->first, + mapTagReplacements); + savedGameDataItemNode->addAttribute("value", iterMap->second, + mapTagReplacements); + } + + // //world + // World *world; + // GameCamera *gameCamera; + // + // //misc + // MessageQueue messageQueue; + for (std::list < ScriptManagerMessage >::iterator it = + messageQueue.begin(); it != messageQueue.end(); ++it) { + (*it).saveGame(scriptManagerNode); + } + + //printf("==== ScriptManager Savegame messageBox [%d][%s][%s] displayText [%s]\n",messageBox.getEnabled(),messageBox.getText().c_str(),messageBox.getHeader().c_str(),displayText.c_str()); + + // GraphicMessageBox messageBox; + scriptManagerNode->addAttribute("messageBox_enabled", + intToStr(messageBox.getEnabled()), + mapTagReplacements); + scriptManagerNode->addAttribute("messageBox_text", + messageBox.getText(), + mapTagReplacements); + scriptManagerNode->addAttribute("messageBox_header", + messageBox.getHeader(), + mapTagReplacements); + + // string displayText; + scriptManagerNode->addAttribute("displayText", displayText, + mapTagReplacements); + // + // //last created unit + // string lastCreatedUnitName; + scriptManagerNode->addAttribute("lastCreatedUnitName", + lastCreatedUnitName, + mapTagReplacements); + // int lastCreatedUnitId; + scriptManagerNode->addAttribute("lastCreatedUnitId", + intToStr(lastCreatedUnitId), + mapTagReplacements); + // + // //last dead unit + // string lastDeadUnitName; + scriptManagerNode->addAttribute("lastDeadUnitName", lastDeadUnitName, + mapTagReplacements); + // int lastDeadUnitId; + scriptManagerNode->addAttribute("lastDeadUnitId", + intToStr(lastDeadUnitId), + mapTagReplacements); + // int lastDeadUnitCauseOfDeath; + scriptManagerNode->addAttribute("lastDeadUnitCauseOfDeath", + intToStr(lastDeadUnitCauseOfDeath), + mapTagReplacements); + // + // //last dead unit's killer + // string lastDeadUnitKillerName; + scriptManagerNode->addAttribute("lastDeadUnitKillerName", + lastDeadUnitKillerName, + mapTagReplacements); + // int lastDeadUnitKillerId; + scriptManagerNode->addAttribute("lastDeadUnitKillerId", + intToStr(lastDeadUnitKillerId), + mapTagReplacements); + // + // //last attacked unit + // string lastAttackedUnitName; + scriptManagerNode->addAttribute("lastAttackedUnitName", + lastAttackedUnitName, + mapTagReplacements); + // int lastAttackedUnitId; + scriptManagerNode->addAttribute("lastAttackedUnitId", + intToStr(lastAttackedUnitId), + mapTagReplacements); + // + // //last attacking unit + // string lastAttackingUnitName; + scriptManagerNode->addAttribute("lastAttackingUnitName", + lastAttackingUnitName, + mapTagReplacements); + // int lastAttackingUnitId; + scriptManagerNode->addAttribute("lastAttackingUnitId", + intToStr(lastAttackingUnitId), + mapTagReplacements); + // + // // end game state + // bool gameOver; + scriptManagerNode->addAttribute("gameOver", intToStr(gameOver), + mapTagReplacements); + // bool gameWon; + scriptManagerNode->addAttribute("gameWon", intToStr(gameWon), + mapTagReplacements); + // PlayerModifiers playerModifiers[GameConstants::maxPlayers]; + for (unsigned int i = 0; i < (unsigned int) GameConstants::maxPlayers; + ++i) { + PlayerModifiers & player = playerModifiers[i]; + player.saveGame(scriptManagerNode); + } + // int currentTimerTriggeredEventId; + scriptManagerNode->addAttribute("currentTimerTriggeredEventId", + intToStr + (currentTimerTriggeredEventId), + mapTagReplacements); + // int currentCellTriggeredEventId; + scriptManagerNode->addAttribute("currentCellTriggeredEventId", + intToStr(currentCellTriggeredEventId), + mapTagReplacements); + // int currentEventId; + scriptManagerNode->addAttribute("currentEventId", + intToStr(currentEventId), + mapTagReplacements); + // std::map CellTriggerEventList; + for (std::map < int, CellTriggerEvent >::iterator iterMap = + CellTriggerEventList.begin(); + iterMap != CellTriggerEventList.end(); ++iterMap) { + XmlNode * + cellTriggerEventListNode = + scriptManagerNode->addChild("CellTriggerEventList"); + + cellTriggerEventListNode->addAttribute("key", + intToStr(iterMap->first), + mapTagReplacements); + iterMap->second.saveGame(cellTriggerEventListNode); + } + // std::map TimerTriggerEventList; + for (std::map < int, TimerTriggerEvent >::iterator iterMap = + TimerTriggerEventList.begin(); + iterMap != TimerTriggerEventList.end(); ++iterMap) { + XmlNode * + timerTriggerEventListNode = + scriptManagerNode->addChild("TimerTriggerEventList"); + + timerTriggerEventListNode->addAttribute("key", + intToStr(iterMap->first), + mapTagReplacements); + iterMap->second.saveGame(timerTriggerEventListNode); + } + + // bool inCellTriggerEvent; + scriptManagerNode->addAttribute("inCellTriggerEvent", + intToStr(inCellTriggerEvent), + mapTagReplacements); + // std::vector unRegisterCellTriggerEventList; + for (unsigned int i = 0; i < unRegisterCellTriggerEventList.size(); + ++i) { + XmlNode * + unRegisterCellTriggerEventListNode = + scriptManagerNode->addChild("unRegisterCellTriggerEventList"); + unRegisterCellTriggerEventListNode->addAttribute("eventId", + intToStr + (unRegisterCellTriggerEventList + [i]), + mapTagReplacements); + } + + scriptManagerNode->addAttribute("registeredDayNightEvent", + intToStr(registeredDayNightEvent), + mapTagReplacements); + scriptManagerNode->addAttribute("lastDayNightTriggerStatus", + intToStr(lastDayNightTriggerStatus), + mapTagReplacements); + + for (std::map < int, UnitTriggerEventType >::iterator iterMap = + UnitTriggerEventList.begin(); + iterMap != UnitTriggerEventList.end(); ++iterMap) { + XmlNode * + unitTriggerEventListNode = + scriptManagerNode->addChild("UnitTriggerEventList"); + + unitTriggerEventListNode->addAttribute("unitId", + intToStr(iterMap->first), + mapTagReplacements); + unitTriggerEventListNode->addAttribute("eventType", + intToStr(iterMap->second), + mapTagReplacements); + } + scriptManagerNode->addAttribute("lastUnitTriggerEventUnitId", + intToStr(lastUnitTriggerEventUnitId), + mapTagReplacements); + scriptManagerNode->addAttribute("lastUnitTriggerEventType", + intToStr(lastUnitTriggerEventType), + mapTagReplacements); + + luaScript.saveGame(scriptManagerNode); + } + + void + ScriptManager::loadGame(const XmlNode * rootNode) { + const XmlNode * + scriptManagerNode = rootNode->getChild("ScriptManager"); + + // string code; + code = scriptManagerNode->getAttribute("code")->getValue(); + // LuaScript luaScript; + + vector < XmlNode * >savedGameDataItemNodeList = + scriptManagerNode->getChildList("SavedGameDataItem"); + + if (LuaScript::getDebugModeEnabled() == true) + printf("In loadGame savedGameDataItemNodeList.size() = %d\n", + (int) savedGameDataItemNodeList.size()); + + for (unsigned int i = 0; i < savedGameDataItemNodeList.size(); ++i) { + XmlNode * + node = savedGameDataItemNodeList[i]; + string + key = node->getAttribute("key")->getValue(); + string + value = node->getAttribute("value")->getValue(); + + luaSavedGameData[key] = value; + } + + // //world + // World *world; + // GameCamera *gameCamera; + // + // //misc + // MessageQueue messageQueue; + messageQueue.clear(); + vector < XmlNode * >messageQueueNodeList = + scriptManagerNode->getChildList("ScriptManagerMessage"); + for (unsigned int i = 0; i < messageQueueNodeList.size(); ++i) { + XmlNode * + node = messageQueueNodeList[i]; + ScriptManagerMessage + msg; + msg.loadGame(node); + messageQueue.push_back(msg); + } + + // GraphicMessageBox messageBox; + messageBox.setEnabled(scriptManagerNode-> + getAttribute("messageBox_enabled")-> + getIntValue() != 0); + messageBox.setText(scriptManagerNode-> + getAttribute("messageBox_text")->getValue()); + messageBox.setHeader(scriptManagerNode-> + getAttribute("messageBox_header")->getValue()); + + // string displayText; + displayText = + scriptManagerNode->getAttribute("displayText")->getValue(); + // + // //last created unit + // string lastCreatedUnitName; + lastCreatedUnitName = + scriptManagerNode->getAttribute("lastCreatedUnitName")->getValue(); + // int lastCreatedUnitId; + lastCreatedUnitId = + scriptManagerNode->getAttribute("lastCreatedUnitId")->getIntValue(); + // + // //last dead unit + // string lastDeadUnitName; + lastDeadUnitName = + scriptManagerNode->getAttribute("lastDeadUnitName")->getValue(); + // int lastDeadUnitId; + lastDeadUnitId = + scriptManagerNode->getAttribute("lastDeadUnitId")->getIntValue(); + // int lastDeadUnitCauseOfDeath; + lastDeadUnitCauseOfDeath = + scriptManagerNode->getAttribute("lastDeadUnitCauseOfDeath")-> + getIntValue(); + // + // //last dead unit's killer + // string lastDeadUnitKillerName; + lastDeadUnitKillerName = + scriptManagerNode->getAttribute("lastDeadUnitKillerName")-> + getValue(); + // int lastDeadUnitKillerId; + lastDeadUnitKillerId = + scriptManagerNode->getAttribute("lastDeadUnitKillerId")-> + getIntValue(); + // + // //last attacked unit + // string lastAttackedUnitName; + lastAttackedUnitName = + scriptManagerNode->getAttribute("lastAttackedUnitName")->getValue(); + // int lastAttackedUnitId; + lastAttackedUnitId = + scriptManagerNode->getAttribute("lastAttackedUnitId")-> + getIntValue(); + // + // //last attacking unit + // string lastAttackingUnitName; + lastAttackingUnitName = + scriptManagerNode->getAttribute("lastAttackingUnitName")-> + getValue(); + // int lastAttackingUnitId; + lastAttackingUnitId = + scriptManagerNode->getAttribute("lastAttackingUnitId")-> + getIntValue(); + // + // // end game state + // bool gameOver; + gameOver = + scriptManagerNode->getAttribute("gameOver")->getIntValue() != 0; + // bool gameWon; + gameWon = + scriptManagerNode->getAttribute("gameWon")->getIntValue() != 0; + // PlayerModifiers playerModifiers[GameConstants::maxPlayers]; + vector < XmlNode * >playerModifiersNodeList = + scriptManagerNode->getChildList("PlayerModifiers"); + for (unsigned int i = 0; i < playerModifiersNodeList.size(); ++i) { + XmlNode * + node = playerModifiersNodeList[i]; + playerModifiers[i].loadGame(node); + } + // int currentTimerTriggeredEventId; + currentTimerTriggeredEventId = + scriptManagerNode->getAttribute("currentTimerTriggeredEventId")-> + getIntValue(); + // int currentCellTriggeredEventId; + currentCellTriggeredEventId = + scriptManagerNode->getAttribute("currentCellTriggeredEventId")-> + getIntValue(); + // int currentEventId; + currentEventId = + scriptManagerNode->getAttribute("currentEventId")->getIntValue(); + // std::map CellTriggerEventList; + vector < XmlNode * >cellTriggerEventListNodeList = + scriptManagerNode->getChildList("CellTriggerEventList"); + for (unsigned int i = 0; i < cellTriggerEventListNodeList.size(); ++i) { + XmlNode * + node = cellTriggerEventListNodeList[i]; + CellTriggerEvent + event; + event.loadGame(node); + CellTriggerEventList[node->getAttribute("key")->getIntValue()] = + event; + } + + // std::map TimerTriggerEventList; + vector < XmlNode * >timerTriggerEventListNodeList = + scriptManagerNode->getChildList("TimerTriggerEventList"); + for (unsigned int i = 0; i < timerTriggerEventListNodeList.size(); ++i) { + XmlNode * + node = timerTriggerEventListNodeList[i]; + + TimerTriggerEvent + event; + event.loadGame(node); + TimerTriggerEventList[node->getAttribute("key")->getIntValue()] = + event; + } + + // bool inCellTriggerEvent; + inCellTriggerEvent = + scriptManagerNode->getAttribute("inCellTriggerEvent")-> + getIntValue() != 0; + // std::vector unRegisterCellTriggerEventList; + vector < XmlNode * >unRegisterCellTriggerEventListNodeList = + scriptManagerNode->getChildList("unRegisterCellTriggerEventList"); + for (unsigned int i = 0; + i < unRegisterCellTriggerEventListNodeList.size(); ++i) { + XmlNode * + node = unRegisterCellTriggerEventListNodeList[i]; + unRegisterCellTriggerEventList.push_back(node-> + getAttribute("eventId")-> + getIntValue()); + } + + if (scriptManagerNode->hasAttribute("registeredDayNightEvent") == true) { + registeredDayNightEvent = + scriptManagerNode->getAttribute("registeredDayNightEvent")-> + getIntValue() != 0; + } + if (scriptManagerNode->hasAttribute("lastDayNightTriggerStatus") == + true) { + lastDayNightTriggerStatus = + scriptManagerNode->getAttribute("lastDayNightTriggerStatus")-> + getIntValue(); + } + + vector < XmlNode * >unitTriggerEventListNodeList = + scriptManagerNode->getChildList("UnitTriggerEventList"); + for (unsigned int i = 0; i < unitTriggerEventListNodeList.size(); ++i) { + XmlNode * + node = unitTriggerEventListNodeList[i]; + + UnitTriggerEventType + eventType = utet_None; + int + unitId = node->getAttribute("unitId")->getIntValue(); + if (node->hasAttribute("eventType") == true) { + eventType = + static_cast + (node->getAttribute("eventType")->getIntValue()); + } else if (node->hasAttribute("evenType") == true) { + eventType = + static_cast + (node->getAttribute("evenType")->getIntValue()); + } + UnitTriggerEventList[unitId] = eventType; + } + if (scriptManagerNode->hasAttribute("lastUnitTriggerEventUnitId") == + true) { + lastUnitTriggerEventUnitId = + scriptManagerNode->getAttribute("lastUnitTriggerEventUnitId")-> + getIntValue(); + } + if (scriptManagerNode->hasAttribute("lastUnitTriggerEventType") == + true) { + lastUnitTriggerEventType = + static_cast + (scriptManagerNode->getAttribute("lastUnitTriggerEventType")-> + getIntValue()); + } + + luaScript.loadGame(scriptManagerNode); + } + + + } } //end namespace diff --git a/source/glest_game/game/script_manager.h b/source/glest_game/game/script_manager.h index 5783d53fb..4929a78c7 100644 --- a/source/glest_game/game/script_manager.h +++ b/source/glest_game/game/script_manager.h @@ -29,1081 +29,1056 @@ # include "platform_util.h" using - std::string; +std::string; using - std::list; +std::list; using - Shared::Graphics::Vec2i; +Shared::Graphics::Vec2i; using - Shared::Lua::LuaScript; +Shared::Lua::LuaScript; using - Shared::Lua::LuaHandle; +Shared::Lua::LuaHandle; using - Shared::Xml::XmlNode; +Shared::Xml::XmlNode; using - Shared::Util::RandomGen; +Shared::Util::RandomGen; namespace - Glest -{ - namespace - Game - { - - class - World; - class - Unit; - class - GameCamera; - -// ===================================================== -// class ScriptManagerMessage -// ===================================================== - - class - ScriptManagerMessage - { - private: - string - text; - string - header; - int - factionIndex; - int - teamIndex; - bool - messageNotTranslated; - - public: - ScriptManagerMessage (); - ScriptManagerMessage (const string & text, const string & header, - int factionIndex = -1, int teamIndex = - -1, bool messageNotTranslated = false); - const - string & - getText () const - { - return - text; - } - const - string & - getHeader () const - { - return - header; - } - int - getFactionIndex () const - { - return - factionIndex; - } - int - getTeamIndex () const - { - return - teamIndex; - } - bool - getMessageNotTranslated () const - { - return - messageNotTranslated; - } - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - }; - - class - PlayerModifiers - { - public: - PlayerModifiers (); - - void - disableAi () - { - aiEnabled = false; - } - void - enableAi () - { - aiEnabled = true; - } - - void - setAsWinner () - { - winner = true; - } - - void - disableConsume () - { - consumeEnabled = false; - } - void - enableConsume () - { - consumeEnabled = true; - } - - bool - getWinner () const - { - return - winner; - } - - bool - getAiEnabled () const - { - return - aiEnabled; - } - bool - getConsumeEnabled () const - { - return - consumeEnabled; - } - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - - private: - - bool winner; - bool - aiEnabled; - bool - consumeEnabled; - }; - -// ===================================================== -// class ScriptManager -// ===================================================== - - enum UnitTriggerEventType - { - utet_None = 0, - utet_HPChanged = 1, - utet_EPChanged, - utet_LevelChanged, - utet_FieldChanged, - utet_SkillChanged - }; - - enum CellTriggerEventType - { - ctet_Unit, - ctet_UnitPos, - ctet_Faction, - ctet_FactionPos, - ctet_UnitAreaPos, - ctet_FactionAreaPos, - ctet_AreaPos - }; - - class - CellTriggerEvent - { - public: - CellTriggerEvent (); - CellTriggerEventType - type; - int - sourceId; - int - destId; - Vec2i - destPos; - Vec2i - destPosEnd; - - int - triggerCount; - - std::map < int, - string > - eventStateInfo; - - std::map < int, - std::map < - Vec2i, - bool > > - eventLookupCache; - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - }; - - class - TimerTriggerEvent - { - public: - TimerTriggerEvent (); - bool - running; - //time_t startTime; - //time_t endTime; - int - startFrame; - int - endFrame; - - int - triggerSecondsElapsed; - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - }; - - class - ScriptManager - { - private: - typedef - list < - ScriptManagerMessage > - MessageQueue; - - private: - - //lua - string - code; - LuaScript - luaScript; - - //world - World * - world; - GameCamera * - gameCamera; - - //misc - MessageQueue - messageQueue; - GraphicMessageBox - messageBox; - string - displayText; - int - errorCount; - - //last created unit - string - lastCreatedUnitName; - int - lastCreatedUnitId; - - //last dead unit - string - lastDeadUnitName; - int - lastDeadUnitId; - int - lastDeadUnitCauseOfDeath; - - //last dead unit's killer - string - lastDeadUnitKillerName; - int - lastDeadUnitKillerId; - - //last attacked unit - string - lastAttackedUnitName; - int - lastAttackedUnitId; - - //last attacking unit - string - lastAttackingUnitName; - int - lastAttackingUnitId; - - // end game state - bool - gameOver; - bool - gameWon; - PlayerModifiers - playerModifiers[GameConstants::maxPlayers]; - - int - currentTimerTriggeredEventId; - int - currentCellTriggeredEventId; - int - currentCellTriggeredEventUnitId; - - int - currentCellTriggeredEventAreaEntryUnitId; - int - currentCellTriggeredEventAreaExitUnitId; - - int - currentEventId; - std::map < int, - CellTriggerEvent > - CellTriggerEventList; - std::map < int, - TimerTriggerEvent > - TimerTriggerEventList; - bool - inCellTriggerEvent; - std::vector < int > - unRegisterCellTriggerEventList; - - bool - registeredDayNightEvent; - int - lastDayNightTriggerStatus; - - std::map < int, - UnitTriggerEventType > - UnitTriggerEventList; - int - lastUnitTriggerEventUnitId; - UnitTriggerEventType - lastUnitTriggerEventType; - - RandomGen - random; - const XmlNode * - rootNode; - - std::map < - string, - string > - luaSavedGameData; - - private: - static ScriptManager * - thisScriptManager; - - private: - static const int - messageWrapCount; - static const int - displayTextWrapCount; - - public: - - ScriptManager (); - ~ - ScriptManager (); - void - init (World * world, GameCamera * gameCamera, const XmlNode * rootNode); - - //message box functions - void - addMessageToQueue (ScriptManagerMessage msg); - bool - getMessageBoxEnabled () const - { - return ! - messageQueue. - empty (); - } - GraphicMessageBox * - getMessageBox () - { - return &messageBox; - } - string - getDisplayText () const - { - return - displayText; - } - const PlayerModifiers * - getPlayerModifiers (int factionIndex) const - { - return & - playerModifiers[factionIndex]; - } - - //events - void - onMessageBoxOk (bool popFront = true); - void - onResourceHarvested (); - void - onUnitCreated (const Unit * unit); - void - onUnitDied (const Unit * unit); - void - onUnitAttacked (const Unit * unit); - void - onUnitAttacking (const Unit * unit); - void - onGameOver (bool won); - - void - onCellTriggerEvent (Unit * movingUnit); - void - onTimerTriggerEvent (); - void - onDayNightTriggerEvent (); - void - onUnitTriggerEvent (const Unit * unit, UnitTriggerEventType event); - - bool - getGameWon () const; - bool - getIsGameOver () const; - - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - - private: - string wrapString (const string & str, int wrapCount); - - //wrappers, commands - void - networkShowMessageForFaction (const string & text, - const string & header, int factionIndex); - void - networkShowMessageForTeam (const string & text, const string & header, - int teamIndex); - - void - networkSetCameraPositionForFaction (int factionIndex, - const Vec2i & pos); - void - networkSetCameraPositionForTeam (int teamIndex, const Vec2i & pos); - - void - showMessage (const string & text, const string & header); - void - clearDisplayText (); - void - setDisplayText (const string & text); - void - addConsoleText (const string & text); - void - addConsoleLangText (const char *fmt, ...); - void - DisplayFormattedText (const char *fmt, ...); - void - DisplayFormattedLangText (const char *fmt, ...); - void - setCameraPosition (const Vec2i & pos); - void - shakeCamera (int shakeIntensity, int shakeDuration, - bool cameraDistanceAffected, int unitId); - void - createUnit (const string & unitName, int factionIndex, Vec2i pos); - void - createUnitNoSpacing (const string & unitName, int factionIndex, - Vec2i pos); - - void - setLockedUnitForFaction (const string & unitName, int factionIndex, - bool lock); - void - destroyUnit (int unitId); - void - giveKills (int unitId, int amount); - void - morphToUnit (int unitId, const string & morphName, - int ignoreRequirements); - void - moveToUnit (int unitId, int destUnitId); - void - giveAttackStoppedCommand (int unitId, const string & valueName, - int ignoreRequirements); - void - playStaticSound (const string & playSound); - void - playStreamingSound (const string & playSound); - void - stopStreamingSound (const string & playSound); - void - stopAllSound (); - void - togglePauseGame (int pauseStatus); - - void - playStaticVideo (const string & playVideo); - void - playStreamingVideo (const string & playVideo); - void - stopStreamingVideo (const string & playVideo); - void - stopAllVideo (); - - void - giveResource (const string & resourceName, int factionIndex, - int amount); - void - givePositionCommand (int unitId, const string & producedName, - const Vec2i & pos); - void - giveProductionCommand (int unitId, const string & producedName); - void - giveAttackCommand (int unitId, int unitToAttackId); - void - giveUpgradeCommand (int unitId, const string & upgradeName); - void - giveStopCommand (int unitId); - - void - disableAi (int factionIndex); - void - enableAi (int factionIndex); - void - disableConsume (int factionIndex); - void - enableConsume (int factionIndex); - - int - registerCellTriggerEventForUnitToUnit (int sourceUnitId, - int destUnitId); - int - registerCellTriggerEventForUnitToLocation (int sourceUnitId, - const Vec2i & pos); - int - registerCellTriggerEventForFactionToUnit (int sourceFactionId, - int destUnitId); - int - registerCellTriggerEventForFactionToLocation (int sourceFactionId, - const Vec2i & pos); - - int - registerCellAreaTriggerEventForUnitToLocation (int sourceUnitId, - const Vec4i & pos); - int - registerCellAreaTriggerEventForFactionToLocation (int sourceFactionId, - const Vec4i & pos); - - int - registerCellAreaTriggerEvent (const Vec4i & pos); - - int - getCellTriggerEventCount (int eventId); - void - unregisterCellTriggerEvent (int eventId); - int - startTimerEvent (); - int - startEfficientTimerEvent (int triggerSecondsElapsed); - int - resetTimerEvent (int eventId); - int - stopTimerEvent (int eventId); - int - getTimerEventSecondsElapsed (int eventId); - - int - getCellTriggeredEventId (); - int - getTimerTriggeredEventId (); - - int - getCellTriggeredEventAreaEntryUnitId (); - int - getCellTriggeredEventAreaExitUnitId (); - - int - getCellTriggeredEventUnitId (); - - void - setRandomGenInit (int seed); - int - getRandomGen (int minVal, int maxVal); - int - getWorldFrameCount (); - - bool - getAiEnabled (int factionIndex); - bool - getConsumeEnabled (int factionIndex); - - void - setPlayerAsWinner (int factionIndex); - void - endGame (); - - void - startPerformanceTimer (); - void - endPerformanceTimer (); - Vec2i - getPerformanceTimerResults (); - - //wrappers, queries - int - getIsUnitAlive (int unitId); - Vec2i - getStartLocation (int factionIndex); - Vec2i - getUnitPosition (int unitId); - int - getUnitFaction (int unitId); - const string - getUnitName (int unitId); - const string - getUnitDisplayName (int unitId); - int - getResourceAmount (const string & resourceName, int factionIndex); - const - string & - getLastCreatedUnitName (); - int - getLastCreatedUnitId (); - - void - setUnitPosition (int unitId, Vec2i pos); - - void - addCellMarker (Vec2i pos, int factionIndex, const string & note, - const string & textureFile); - void - removeCellMarker (Vec2i pos, int factionIndex); - - void - showMarker (Vec2i pos, int factionIndex, const string & note, - const string & textureFile, int flashCount); - - const - string & - getLastDeadUnitName (); - int - getLastDeadUnitId (); - int - getLastDeadUnitCauseOfDeath (); - const - string & - getLastDeadUnitKillerName (); - int - getLastDeadUnitKillerId (); - - const - string & - getLastAttackedUnitName (); - int - getLastAttackedUnitId (); - - const - string & - getLastAttackingUnitName (); - int - getLastAttackingUnitId (); - - int - getUnitCount (int factionIndex); - int - getUnitCountOfType (int factionIndex, const string & typeName); - - const string - getSystemMacroValue (const string & key); - const string - getPlayerName (int factionIndex); - - vector < int > - getUnitsForFaction (int factionIndex, const string & commandTypeName, - int field); - int - getUnitCurrentField (int unitId); - - void - loadScenario (const string & name, bool keepFactions); - - int - isFreeCellsOrHasUnit (int field, int unitId, Vec2i pos); - int - isFreeCells (int unitSize, int field, Vec2i pos); - - int - getHumanFactionId (); - void - highlightUnit (int unitId, float radius, float thickness, Vec4f color); - void - unhighlightUnit (int unitId); - - bool - selectUnit (int unitId); - void - unselectUnit (int unitId); - void - addUnitToGroupSelection (int unitId, int groupIndex); - void - recallGroupSelection (int groupIndex); - void - removeUnitFromGroupSelection (int unitId, int group); - void - setAttackWarningsEnabled (bool enabled); - bool - getAttackWarningsEnabled (); - - int - getIsDayTime (); - int - getIsNightTime (); - float - getTimeOfDay (); - void - registerDayNightEvent (); - void - unregisterDayNightEvent (); - - void - registerUnitTriggerEvent (int unitId); - void - unregisterUnitTriggerEvent (int unitId); - int - getLastUnitTriggerEventUnitId (); - UnitTriggerEventType - getLastUnitTriggerEventType (); - int - getUnitProperty (int unitId, UnitTriggerEventType type); - const string - getUnitPropertyName (int unitId, UnitTriggerEventType type); - - void - disableSpeedChange (); - void - enableSpeedChange (); - bool - getSpeedChangeEnabled (); - void - storeSaveGameData (string name, string value); - string - loadSaveGameData (string name); - - ControlType - getFactionPlayerType (int factionIndex); - // ----------------------------------------------------------------------- - - static void - error (LuaHandle * luaHandle, const megaglest_runtime_error * mgErr, - const char *file, const char *function, int line); - - //callbacks, commands - static int - networkShowMessageForFaction (LuaHandle * luaHandle); - static int - networkShowMessageForTeam (LuaHandle * luaHandle); - - static int - networkSetCameraPositionForFaction (LuaHandle * luaHandle); - static int - networkSetCameraPositionForTeam (LuaHandle * luaHandle); - - static int - showMessage (LuaHandle * luaHandle); - static int - setDisplayText (LuaHandle * luaHandle); - static int - addConsoleText (LuaHandle * luaHandle); - static int - addConsoleLangText (LuaHandle * luaHandle); - static int - DisplayFormattedText (LuaHandle * luaHandle); - static int - DisplayFormattedLangText (LuaHandle * luaHandle); - static int - clearDisplayText (LuaHandle * luaHandle); - static int - setCameraPosition (LuaHandle * luaHandle); - static int - shakeCamera (LuaHandle * luaHandle); - static int - shakeCameraOnUnit (LuaHandle * luaHandle); - static int - createUnit (LuaHandle * luaHandle); - static int - createUnitNoSpacing (LuaHandle * luaHandle); - - static int - setLockedUnitForFaction (LuaHandle * luaHandle); - static int - destroyUnit (LuaHandle * luaHandle); - static int - giveKills (LuaHandle * luaHandle); - static int - morphToUnit (LuaHandle * luaHandle); - static int - moveToUnit (LuaHandle * luaHandle); - static int - giveAttackStoppedCommand (LuaHandle * luaHandle); - static int - playStaticSound (LuaHandle * luaHandle); - static int - playStreamingSound (LuaHandle * luaHandle); - static int - stopStreamingSound (LuaHandle * luaHandle); - static int - stopAllSound (LuaHandle * luaHandle); - static int - togglePauseGame (LuaHandle * luaHandle); - - static int - playStaticVideo (LuaHandle * luaHandle); - static int - playStreamingVideo (LuaHandle * luaHandle); - static int - stopStreamingVideo (LuaHandle * luaHandle); - static int - stopAllVideo (LuaHandle * luaHandle); - - static int - giveResource (LuaHandle * luaHandle); - static int - givePositionCommand (LuaHandle * luaHandle); - static int - giveProductionCommand (LuaHandle * luaHandle); - static int - giveAttackCommand (LuaHandle * luaHandle); - static int - giveUpgradeCommand (LuaHandle * luaHandle); - - static int - disableAi (LuaHandle * luaHandle); - static int - enableAi (LuaHandle * luaHandle); - - static int - disableConsume (LuaHandle * luaHandle); - static int - enableConsume (LuaHandle * luaHandle); - - static int - getAiEnabled (LuaHandle * luaHandle); - static int - getConsumeEnabled (LuaHandle * luaHandle); - - static int - registerCellTriggerEventForUnitToUnit (LuaHandle * luaHandle); - static int - registerCellTriggerEventForUnitToLocation (LuaHandle * luaHandle); - static int - registerCellTriggerEventForFactionToUnit (LuaHandle * luaHandle); - static int - registerCellTriggerEventForFactionToLocation (LuaHandle * luaHandle); - - static int - registerCellAreaTriggerEventForUnitToLocation (LuaHandle * luaHandle); - static int - registerCellAreaTriggerEventForFactionToLocation (LuaHandle * - luaHandle); - - static int - registerCellAreaTriggerEvent (LuaHandle * luaHandle); - - static int - getCellTriggerEventCount (LuaHandle * luaHandle); - static int - unregisterCellTriggerEvent (LuaHandle * luaHandle); - static int - startTimerEvent (LuaHandle * luaHandle); - static int - startEfficientTimerEvent (LuaHandle * luaHandle); - static int - resetTimerEvent (LuaHandle * luaHandle); - static int - stopTimerEvent (LuaHandle * luaHandle); - static int - getTimerEventSecondsElapsed (LuaHandle * luaHandle); - - static int - getCellTriggeredEventId (LuaHandle * luaHandle); - static int - getTimerTriggeredEventId (LuaHandle * luaHandle); - - static int - getCellTriggeredEventAreaEntryUnitId (LuaHandle * luaHandle); - static int - getCellTriggeredEventAreaExitUnitId (LuaHandle * luaHandle); - - static int - getCellTriggeredEventUnitId (LuaHandle * luaHandle); - - static int - setRandomGenInit (LuaHandle * luaHandle); - static int - getRandomGen (LuaHandle * luaHandle); - static int - getWorldFrameCount (LuaHandle * luaHandle); - - static int - setPlayerAsWinner (LuaHandle * luaHandle); - static int - endGame (LuaHandle * luaHandle); - - static int - startPerformanceTimer (LuaHandle * luaHandle); - static int - endPerformanceTimer (LuaHandle * luaHandle); - static int - getPerformanceTimerResults (LuaHandle * luaHandle); - - //callbacks, queries - static int - getIsUnitAlive (LuaHandle * luaHandle); - static int - getStartLocation (LuaHandle * luaHandle); - static int - getUnitPosition (LuaHandle * luaHandle); - static int - getUnitFaction (LuaHandle * luaHandle); - static int - getUnitName (LuaHandle * luaHandle); - static int - getUnitDisplayName (LuaHandle * luaHandle); - static int - getResourceAmount (LuaHandle * luaHandle); - static int - getLastCreatedUnitName (LuaHandle * luaHandle); - static int - getLastCreatedUnitId (LuaHandle * luaHandle); - - static int - setUnitPosition (LuaHandle * luaHandle); - - static int - addCellMarker (LuaHandle * luaHandle); - static int - removeCellMarker (LuaHandle * luaHandle); - - static int - showMarker (LuaHandle * luaHandle); - - static int - getLastDeadUnitName (LuaHandle * luaHandle); - static int - getLastDeadUnitId (LuaHandle * luaHandle); - static int - getLastDeadUnitCauseOfDeath (LuaHandle * luaHandle); - static int - getLastDeadUnitKillerName (LuaHandle * luaHandle); - static int - getLastDeadUnitKillerId (LuaHandle * luaHandle); - - static int - getLastAttackedUnitName (LuaHandle * luaHandle); - static int - getLastAttackedUnitId (LuaHandle * luaHandle); - - static int - getLastAttackingUnitName (LuaHandle * luaHandle); - static int - getLastAttackingUnitId (LuaHandle * luaHandle); - - static int - getUnitCount (LuaHandle * luaHandle); - static int - getUnitCountOfType (LuaHandle * luaHandle); - - static int - getGameWon (LuaHandle * luaHandle); - static int - getIsGameOver (LuaHandle * luaHandle); - - static int - getSystemMacroValue (LuaHandle * luaHandle); - static int - getPlayerName (LuaHandle * luaHandle); - static int - scenarioDir (LuaHandle * luaHandle); - - static int - loadScenario (LuaHandle * luaHandle); - - static int - getUnitsForFaction (LuaHandle * luaHandle); - static int - getUnitCurrentField (LuaHandle * luaHandle); - - static int - isFreeCellsOrHasUnit (LuaHandle * luaHandle); - static int - isFreeCells (LuaHandle * luaHandle); - - static int - getHumanFactionId (LuaHandle * luaHandle); - - static int - highlightUnit (LuaHandle * luaHandle); - static int - unhighlightUnit (LuaHandle * luaHandle); - - static int - giveStopCommand (LuaHandle * luaHandle); - static int - selectUnit (LuaHandle * luaHandle); - static int - unselectUnit (LuaHandle * luaHandle); - static int - addUnitToGroupSelection (LuaHandle * luaHandle); - static int - recallGroupSelection (LuaHandle * luaHandle); - static int - removeUnitFromGroupSelection (LuaHandle * luaHandle); - static int - setAttackWarningsEnabled (LuaHandle * luaHandle); - static int - getAttackWarningsEnabled (LuaHandle * luaHandle); - - static int - getIsDayTime (LuaHandle * luaHandle); - static int - getIsNightTime (LuaHandle * luaHandle); - static int - getTimeOfDay (LuaHandle * luaHandle); - static int - registerDayNightEvent (LuaHandle * luaHandle); - static int - unregisterDayNightEvent (LuaHandle * luaHandle); - - static int - registerUnitTriggerEvent (LuaHandle * luaHandle); - static int - unregisterUnitTriggerEvent (LuaHandle * luaHandle); - static int - getLastUnitTriggerEventUnitId (LuaHandle * luaHandle); - static int - getLastUnitTriggerEventType (LuaHandle * luaHandle); - static int - getUnitProperty (LuaHandle * luaHandle); - static int - getUnitPropertyName (LuaHandle * luaHandle); - - static int - disableSpeedChange (LuaHandle * luaHandle); - static int - enableSpeedChange (LuaHandle * luaHandle); - static int - getSpeedChangeEnabled (LuaHandle * luaHandle); - - static int - storeSaveGameData (LuaHandle * luaHandle); - static int - loadSaveGameData (LuaHandle * luaHandle); - - static int - getFactionPlayerType (LuaHandle * luaHandle); - }; - -}} //end namespace + Glest { + namespace + Game { + + class + World; + class + Unit; + class + GameCamera; + + // ===================================================== + // class ScriptManagerMessage + // ===================================================== + + class + ScriptManagerMessage { + private: + string + text; + string + header; + int + factionIndex; + int + teamIndex; + bool + messageNotTranslated; + + public: + ScriptManagerMessage(); + ScriptManagerMessage(const string & text, const string & header, + int factionIndex = -1, int teamIndex = + -1, bool messageNotTranslated = false); + const + string & + getText() const { + return + text; + } + const + string & + getHeader() const { + return + header; + } + int + getFactionIndex() const { + return + factionIndex; + } + int + getTeamIndex() const { + return + teamIndex; + } + bool + getMessageNotTranslated() const { + return + messageNotTranslated; + } + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + }; + + class + PlayerModifiers { + public: + PlayerModifiers(); + + void + disableAi() { + aiEnabled = false; + } + void + enableAi() { + aiEnabled = true; + } + + void + setAsWinner() { + winner = true; + } + + void + disableConsume() { + consumeEnabled = false; + } + void + enableConsume() { + consumeEnabled = true; + } + + bool + getWinner() const { + return + winner; + } + + bool + getAiEnabled() const { + return + aiEnabled; + } + bool + getConsumeEnabled() const { + return + consumeEnabled; + } + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + + private: + + bool winner; + bool + aiEnabled; + bool + consumeEnabled; + }; + + // ===================================================== + // class ScriptManager + // ===================================================== + + enum UnitTriggerEventType { + utet_None = 0, + utet_HPChanged = 1, + utet_EPChanged, + utet_LevelChanged, + utet_FieldChanged, + utet_SkillChanged + }; + + enum CellTriggerEventType { + ctet_Unit, + ctet_UnitPos, + ctet_Faction, + ctet_FactionPos, + ctet_UnitAreaPos, + ctet_FactionAreaPos, + ctet_AreaPos + }; + + class + CellTriggerEvent { + public: + CellTriggerEvent(); + CellTriggerEventType + type; + int + sourceId; + int + destId; + Vec2i + destPos; + Vec2i + destPosEnd; + + int + triggerCount; + + std::map < int, + string > + eventStateInfo; + + std::map < int, + std::map < + Vec2i, + bool > > + eventLookupCache; + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + }; + + class + TimerTriggerEvent { + public: + TimerTriggerEvent(); + bool + running; + //time_t startTime; + //time_t endTime; + int + startFrame; + int + endFrame; + + int + triggerSecondsElapsed; + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + }; + + class + ScriptManager { + private: + typedef + list < + ScriptManagerMessage > + MessageQueue; + + private: + + //lua + string + code; + LuaScript + luaScript; + + //world + World * + world; + GameCamera * + gameCamera; + + //misc + MessageQueue + messageQueue; + GraphicMessageBox + messageBox; + string + displayText; + int + errorCount; + + //last created unit + string + lastCreatedUnitName; + int + lastCreatedUnitId; + + //last dead unit + string + lastDeadUnitName; + int + lastDeadUnitId; + int + lastDeadUnitCauseOfDeath; + + //last dead unit's killer + string + lastDeadUnitKillerName; + int + lastDeadUnitKillerId; + + //last attacked unit + string + lastAttackedUnitName; + int + lastAttackedUnitId; + + //last attacking unit + string + lastAttackingUnitName; + int + lastAttackingUnitId; + + // end game state + bool + gameOver; + bool + gameWon; + PlayerModifiers + playerModifiers[GameConstants::maxPlayers]; + + int + currentTimerTriggeredEventId; + int + currentCellTriggeredEventId; + int + currentCellTriggeredEventUnitId; + + int + currentCellTriggeredEventAreaEntryUnitId; + int + currentCellTriggeredEventAreaExitUnitId; + + int + currentEventId; + std::map < int, + CellTriggerEvent > + CellTriggerEventList; + std::map < int, + TimerTriggerEvent > + TimerTriggerEventList; + bool + inCellTriggerEvent; + std::vector < int > + unRegisterCellTriggerEventList; + + bool + registeredDayNightEvent; + int + lastDayNightTriggerStatus; + + std::map < int, + UnitTriggerEventType > + UnitTriggerEventList; + int + lastUnitTriggerEventUnitId; + UnitTriggerEventType + lastUnitTriggerEventType; + + RandomGen + random; + const XmlNode * + rootNode; + + std::map < + string, + string > + luaSavedGameData; + + private: + static ScriptManager * + thisScriptManager; + + private: + static const int + messageWrapCount; + static const int + displayTextWrapCount; + + public: + + ScriptManager(); + ~ + ScriptManager(); + void + init(World * world, GameCamera * gameCamera, const XmlNode * rootNode); + + //message box functions + void + addMessageToQueue(ScriptManagerMessage msg); + bool + getMessageBoxEnabled() const { + return ! + messageQueue. + empty(); + } + GraphicMessageBox * + getMessageBox() { + return &messageBox; + } + string + getDisplayText() const { + return + displayText; + } + const PlayerModifiers * + getPlayerModifiers(int factionIndex) const { + return & + playerModifiers[factionIndex]; + } + + //events + void + onMessageBoxOk(bool popFront = true); + void + onResourceHarvested(); + void + onUnitCreated(const Unit * unit); + void + onUnitDied(const Unit * unit); + void + onUnitAttacked(const Unit * unit); + void + onUnitAttacking(const Unit * unit); + void + onGameOver(bool won); + + void + onCellTriggerEvent(Unit * movingUnit); + void + onTimerTriggerEvent(); + void + onDayNightTriggerEvent(); + void + onUnitTriggerEvent(const Unit * unit, UnitTriggerEventType event); + + bool + getGameWon() const; + bool + getIsGameOver() const; + + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + + private: + string wrapString(const string & str, int wrapCount); + + //wrappers, commands + void + networkShowMessageForFaction(const string & text, + const string & header, int factionIndex); + void + networkShowMessageForTeam(const string & text, const string & header, + int teamIndex); + + void + networkSetCameraPositionForFaction(int factionIndex, + const Vec2i & pos); + void + networkSetCameraPositionForTeam(int teamIndex, const Vec2i & pos); + + void + showMessage(const string & text, const string & header); + void + clearDisplayText(); + void + setDisplayText(const string & text); + void + addConsoleText(const string & text); + void + addConsoleLangText(const char *fmt, ...); + void + DisplayFormattedText(const char *fmt, ...); + void + DisplayFormattedLangText(const char *fmt, ...); + void + setCameraPosition(const Vec2i & pos); + void + shakeCamera(int shakeIntensity, int shakeDuration, + bool cameraDistanceAffected, int unitId); + void + createUnit(const string & unitName, int factionIndex, Vec2i pos); + void + createUnitNoSpacing(const string & unitName, int factionIndex, + Vec2i pos); + + void + setLockedUnitForFaction(const string & unitName, int factionIndex, + bool lock); + void + destroyUnit(int unitId); + void + giveKills(int unitId, int amount); + void + morphToUnit(int unitId, const string & morphName, + int ignoreRequirements); + void + moveToUnit(int unitId, int destUnitId); + void + giveAttackStoppedCommand(int unitId, const string & valueName, + int ignoreRequirements); + void + playStaticSound(const string & playSound); + void + playStreamingSound(const string & playSound); + void + stopStreamingSound(const string & playSound); + void + stopAllSound(); + void + togglePauseGame(int pauseStatus); + + void + playStaticVideo(const string & playVideo); + void + playStreamingVideo(const string & playVideo); + void + stopStreamingVideo(const string & playVideo); + void + stopAllVideo(); + + void + giveResource(const string & resourceName, int factionIndex, + int amount); + void + givePositionCommand(int unitId, const string & producedName, + const Vec2i & pos); + void + giveProductionCommand(int unitId, const string & producedName); + void + giveAttackCommand(int unitId, int unitToAttackId); + void + giveUpgradeCommand(int unitId, const string & upgradeName); + void + giveStopCommand(int unitId); + + void + disableAi(int factionIndex); + void + enableAi(int factionIndex); + void + disableConsume(int factionIndex); + void + enableConsume(int factionIndex); + + int + registerCellTriggerEventForUnitToUnit(int sourceUnitId, + int destUnitId); + int + registerCellTriggerEventForUnitToLocation(int sourceUnitId, + const Vec2i & pos); + int + registerCellTriggerEventForFactionToUnit(int sourceFactionId, + int destUnitId); + int + registerCellTriggerEventForFactionToLocation(int sourceFactionId, + const Vec2i & pos); + + int + registerCellAreaTriggerEventForUnitToLocation(int sourceUnitId, + const Vec4i & pos); + int + registerCellAreaTriggerEventForFactionToLocation(int sourceFactionId, + const Vec4i & pos); + + int + registerCellAreaTriggerEvent(const Vec4i & pos); + + int + getCellTriggerEventCount(int eventId); + void + unregisterCellTriggerEvent(int eventId); + int + startTimerEvent(); + int + startEfficientTimerEvent(int triggerSecondsElapsed); + int + resetTimerEvent(int eventId); + int + stopTimerEvent(int eventId); + int + getTimerEventSecondsElapsed(int eventId); + + int + getCellTriggeredEventId(); + int + getTimerTriggeredEventId(); + + int + getCellTriggeredEventAreaEntryUnitId(); + int + getCellTriggeredEventAreaExitUnitId(); + + int + getCellTriggeredEventUnitId(); + + void + setRandomGenInit(int seed); + int + getRandomGen(int minVal, int maxVal); + int + getWorldFrameCount(); + + bool + getAiEnabled(int factionIndex); + bool + getConsumeEnabled(int factionIndex); + + void + setPlayerAsWinner(int factionIndex); + void + endGame(); + + void + startPerformanceTimer(); + void + endPerformanceTimer(); + Vec2i + getPerformanceTimerResults(); + + //wrappers, queries + int + getIsUnitAlive(int unitId); + Vec2i + getStartLocation(int factionIndex); + Vec2i + getUnitPosition(int unitId); + int + getUnitFaction(int unitId); + const string + getUnitName(int unitId); + const string + getUnitDisplayName(int unitId); + int + getResourceAmount(const string & resourceName, int factionIndex); + const + string & + getLastCreatedUnitName(); + int + getLastCreatedUnitId(); + + void + setUnitPosition(int unitId, Vec2i pos); + + void + addCellMarker(Vec2i pos, int factionIndex, const string & note, + const string & textureFile); + void + removeCellMarker(Vec2i pos, int factionIndex); + + void + showMarker(Vec2i pos, int factionIndex, const string & note, + const string & textureFile, int flashCount); + + const + string & + getLastDeadUnitName(); + int + getLastDeadUnitId(); + int + getLastDeadUnitCauseOfDeath(); + const + string & + getLastDeadUnitKillerName(); + int + getLastDeadUnitKillerId(); + + const + string & + getLastAttackedUnitName(); + int + getLastAttackedUnitId(); + + const + string & + getLastAttackingUnitName(); + int + getLastAttackingUnitId(); + + int + getUnitCount(int factionIndex); + int + getUnitCountOfType(int factionIndex, const string & typeName); + + const string + getSystemMacroValue(const string & key); + const string + getPlayerName(int factionIndex); + + vector < int > + getUnitsForFaction(int factionIndex, const string & commandTypeName, + int field); + int + getUnitCurrentField(int unitId); + + void + loadScenario(const string & name, bool keepFactions); + + int + isFreeCellsOrHasUnit(int field, int unitId, Vec2i pos); + int + isFreeCells(int unitSize, int field, Vec2i pos); + + int + getHumanFactionId(); + void + highlightUnit(int unitId, float radius, float thickness, Vec4f color); + void + unhighlightUnit(int unitId); + + bool + selectUnit(int unitId); + void + unselectUnit(int unitId); + void + addUnitToGroupSelection(int unitId, int groupIndex); + void + recallGroupSelection(int groupIndex); + void + removeUnitFromGroupSelection(int unitId, int group); + void + setAttackWarningsEnabled(bool enabled); + bool + getAttackWarningsEnabled(); + + int + getIsDayTime(); + int + getIsNightTime(); + float + getTimeOfDay(); + void + registerDayNightEvent(); + void + unregisterDayNightEvent(); + + void + registerUnitTriggerEvent(int unitId); + void + unregisterUnitTriggerEvent(int unitId); + int + getLastUnitTriggerEventUnitId(); + UnitTriggerEventType + getLastUnitTriggerEventType(); + int + getUnitProperty(int unitId, UnitTriggerEventType type); + const string + getUnitPropertyName(int unitId, UnitTriggerEventType type); + + void + disableSpeedChange(); + void + enableSpeedChange(); + bool + getSpeedChangeEnabled(); + void + storeSaveGameData(string name, string value); + string + loadSaveGameData(string name); + + ControlType + getFactionPlayerType(int factionIndex); + // ----------------------------------------------------------------------- + + static void + error(LuaHandle * luaHandle, const megaglest_runtime_error * mgErr, + const char *file, const char *function, int line); + + //callbacks, commands + static int + networkShowMessageForFaction(LuaHandle * luaHandle); + static int + networkShowMessageForTeam(LuaHandle * luaHandle); + + static int + networkSetCameraPositionForFaction(LuaHandle * luaHandle); + static int + networkSetCameraPositionForTeam(LuaHandle * luaHandle); + + static int + showMessage(LuaHandle * luaHandle); + static int + setDisplayText(LuaHandle * luaHandle); + static int + addConsoleText(LuaHandle * luaHandle); + static int + addConsoleLangText(LuaHandle * luaHandle); + static int + DisplayFormattedText(LuaHandle * luaHandle); + static int + DisplayFormattedLangText(LuaHandle * luaHandle); + static int + clearDisplayText(LuaHandle * luaHandle); + static int + setCameraPosition(LuaHandle * luaHandle); + static int + shakeCamera(LuaHandle * luaHandle); + static int + shakeCameraOnUnit(LuaHandle * luaHandle); + static int + createUnit(LuaHandle * luaHandle); + static int + createUnitNoSpacing(LuaHandle * luaHandle); + + static int + setLockedUnitForFaction(LuaHandle * luaHandle); + static int + destroyUnit(LuaHandle * luaHandle); + static int + giveKills(LuaHandle * luaHandle); + static int + morphToUnit(LuaHandle * luaHandle); + static int + moveToUnit(LuaHandle * luaHandle); + static int + giveAttackStoppedCommand(LuaHandle * luaHandle); + static int + playStaticSound(LuaHandle * luaHandle); + static int + playStreamingSound(LuaHandle * luaHandle); + static int + stopStreamingSound(LuaHandle * luaHandle); + static int + stopAllSound(LuaHandle * luaHandle); + static int + togglePauseGame(LuaHandle * luaHandle); + + static int + playStaticVideo(LuaHandle * luaHandle); + static int + playStreamingVideo(LuaHandle * luaHandle); + static int + stopStreamingVideo(LuaHandle * luaHandle); + static int + stopAllVideo(LuaHandle * luaHandle); + + static int + giveResource(LuaHandle * luaHandle); + static int + givePositionCommand(LuaHandle * luaHandle); + static int + giveProductionCommand(LuaHandle * luaHandle); + static int + giveAttackCommand(LuaHandle * luaHandle); + static int + giveUpgradeCommand(LuaHandle * luaHandle); + + static int + disableAi(LuaHandle * luaHandle); + static int + enableAi(LuaHandle * luaHandle); + + static int + disableConsume(LuaHandle * luaHandle); + static int + enableConsume(LuaHandle * luaHandle); + + static int + getAiEnabled(LuaHandle * luaHandle); + static int + getConsumeEnabled(LuaHandle * luaHandle); + + static int + registerCellTriggerEventForUnitToUnit(LuaHandle * luaHandle); + static int + registerCellTriggerEventForUnitToLocation(LuaHandle * luaHandle); + static int + registerCellTriggerEventForFactionToUnit(LuaHandle * luaHandle); + static int + registerCellTriggerEventForFactionToLocation(LuaHandle * luaHandle); + + static int + registerCellAreaTriggerEventForUnitToLocation(LuaHandle * luaHandle); + static int + registerCellAreaTriggerEventForFactionToLocation(LuaHandle * + luaHandle); + + static int + registerCellAreaTriggerEvent(LuaHandle * luaHandle); + + static int + getCellTriggerEventCount(LuaHandle * luaHandle); + static int + unregisterCellTriggerEvent(LuaHandle * luaHandle); + static int + startTimerEvent(LuaHandle * luaHandle); + static int + startEfficientTimerEvent(LuaHandle * luaHandle); + static int + resetTimerEvent(LuaHandle * luaHandle); + static int + stopTimerEvent(LuaHandle * luaHandle); + static int + getTimerEventSecondsElapsed(LuaHandle * luaHandle); + + static int + getCellTriggeredEventId(LuaHandle * luaHandle); + static int + getTimerTriggeredEventId(LuaHandle * luaHandle); + + static int + getCellTriggeredEventAreaEntryUnitId(LuaHandle * luaHandle); + static int + getCellTriggeredEventAreaExitUnitId(LuaHandle * luaHandle); + + static int + getCellTriggeredEventUnitId(LuaHandle * luaHandle); + + static int + setRandomGenInit(LuaHandle * luaHandle); + static int + getRandomGen(LuaHandle * luaHandle); + static int + getWorldFrameCount(LuaHandle * luaHandle); + + static int + setPlayerAsWinner(LuaHandle * luaHandle); + static int + endGame(LuaHandle * luaHandle); + + static int + startPerformanceTimer(LuaHandle * luaHandle); + static int + endPerformanceTimer(LuaHandle * luaHandle); + static int + getPerformanceTimerResults(LuaHandle * luaHandle); + + //callbacks, queries + static int + getIsUnitAlive(LuaHandle * luaHandle); + static int + getStartLocation(LuaHandle * luaHandle); + static int + getUnitPosition(LuaHandle * luaHandle); + static int + getUnitFaction(LuaHandle * luaHandle); + static int + getUnitName(LuaHandle * luaHandle); + static int + getUnitDisplayName(LuaHandle * luaHandle); + static int + getResourceAmount(LuaHandle * luaHandle); + static int + getLastCreatedUnitName(LuaHandle * luaHandle); + static int + getLastCreatedUnitId(LuaHandle * luaHandle); + + static int + setUnitPosition(LuaHandle * luaHandle); + + static int + addCellMarker(LuaHandle * luaHandle); + static int + removeCellMarker(LuaHandle * luaHandle); + + static int + showMarker(LuaHandle * luaHandle); + + static int + getLastDeadUnitName(LuaHandle * luaHandle); + static int + getLastDeadUnitId(LuaHandle * luaHandle); + static int + getLastDeadUnitCauseOfDeath(LuaHandle * luaHandle); + static int + getLastDeadUnitKillerName(LuaHandle * luaHandle); + static int + getLastDeadUnitKillerId(LuaHandle * luaHandle); + + static int + getLastAttackedUnitName(LuaHandle * luaHandle); + static int + getLastAttackedUnitId(LuaHandle * luaHandle); + + static int + getLastAttackingUnitName(LuaHandle * luaHandle); + static int + getLastAttackingUnitId(LuaHandle * luaHandle); + + static int + getUnitCount(LuaHandle * luaHandle); + static int + getUnitCountOfType(LuaHandle * luaHandle); + + static int + getGameWon(LuaHandle * luaHandle); + static int + getIsGameOver(LuaHandle * luaHandle); + + static int + getSystemMacroValue(LuaHandle * luaHandle); + static int + getPlayerName(LuaHandle * luaHandle); + static int + scenarioDir(LuaHandle * luaHandle); + + static int + loadScenario(LuaHandle * luaHandle); + + static int + getUnitsForFaction(LuaHandle * luaHandle); + static int + getUnitCurrentField(LuaHandle * luaHandle); + + static int + isFreeCellsOrHasUnit(LuaHandle * luaHandle); + static int + isFreeCells(LuaHandle * luaHandle); + + static int + getHumanFactionId(LuaHandle * luaHandle); + + static int + highlightUnit(LuaHandle * luaHandle); + static int + unhighlightUnit(LuaHandle * luaHandle); + + static int + giveStopCommand(LuaHandle * luaHandle); + static int + selectUnit(LuaHandle * luaHandle); + static int + unselectUnit(LuaHandle * luaHandle); + static int + addUnitToGroupSelection(LuaHandle * luaHandle); + static int + recallGroupSelection(LuaHandle * luaHandle); + static int + removeUnitFromGroupSelection(LuaHandle * luaHandle); + static int + setAttackWarningsEnabled(LuaHandle * luaHandle); + static int + getAttackWarningsEnabled(LuaHandle * luaHandle); + + static int + getIsDayTime(LuaHandle * luaHandle); + static int + getIsNightTime(LuaHandle * luaHandle); + static int + getTimeOfDay(LuaHandle * luaHandle); + static int + registerDayNightEvent(LuaHandle * luaHandle); + static int + unregisterDayNightEvent(LuaHandle * luaHandle); + + static int + registerUnitTriggerEvent(LuaHandle * luaHandle); + static int + unregisterUnitTriggerEvent(LuaHandle * luaHandle); + static int + getLastUnitTriggerEventUnitId(LuaHandle * luaHandle); + static int + getLastUnitTriggerEventType(LuaHandle * luaHandle); + static int + getUnitProperty(LuaHandle * luaHandle); + static int + getUnitPropertyName(LuaHandle * luaHandle); + + static int + disableSpeedChange(LuaHandle * luaHandle); + static int + enableSpeedChange(LuaHandle * luaHandle); + static int + getSpeedChangeEnabled(LuaHandle * luaHandle); + + static int + storeSaveGameData(LuaHandle * luaHandle); + static int + loadSaveGameData(LuaHandle * luaHandle); + + static int + getFactionPlayerType(LuaHandle * luaHandle); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/game/stats.cpp b/source/glest_game/game/stats.cpp index be28499b4..8094f5043 100644 --- a/source/glest_game/game/stats.cpp +++ b/source/glest_game/game/stats.cpp @@ -13,361 +13,335 @@ #include "lang.h" #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - PlayerStats::PlayerStats () - { - control = ctClosed; - resourceMultiplier = 1.0f; - //factionTypeName = ""; - personalityType = fpt_Normal; - teamIndex = 0; - victory = false; - kills = 0; - enemykills = 0; - deaths = 0; - unitsProduced = 0; - resourcesHarvested = 0; - //playerName = ""; - playerLeftBeforeEnd = false; - timePlayerLeft = -1; - playerColor = Vec3f (0, 0, 0); - } + PlayerStats::PlayerStats() { + control = ctClosed; + resourceMultiplier = 1.0f; + //factionTypeName = ""; + personalityType = fpt_Normal; + teamIndex = 0; + victory = false; + kills = 0; + enemykills = 0; + deaths = 0; + unitsProduced = 0; + resourcesHarvested = 0; + //playerName = ""; + playerLeftBeforeEnd = false; + timePlayerLeft = -1; + playerColor = Vec3f(0, 0, 0); + } - string PlayerStats::getStats () const - { - string result = ""; + string PlayerStats::getStats() const { + string result = ""; - Lang & lang = Lang::getInstance (); - string controlString = ""; + Lang & lang = Lang::getInstance(); + string controlString = ""; - if (personalityType == fpt_Observer) - { - controlString = GameConstants::OBSERVER_SLOTNAME; - } - else - { - switch (control) - { - case ctCpuEasy: - controlString = lang.getString ("CpuEasy"); - break; - case ctCpu:controlString = lang.getString ("Cpu"); - break; - case ctCpuUltra:controlString = lang.getString ("CpuUltra"); - break; - case ctCpuZeta:controlString = lang.getString ("CpuZeta"); - break; - case ctNetwork:controlString = lang.getString ("Network"); - break; - case ctHuman:controlString = lang.getString ("Human"); - break; + if (personalityType == fpt_Observer) { + controlString = GameConstants::OBSERVER_SLOTNAME; + } else { + switch (control) { + case ctCpuEasy: + controlString = lang.getString("CpuEasy"); + break; + case ctCpu:controlString = lang.getString("Cpu"); + break; + case ctCpuUltra:controlString = lang.getString("CpuUltra"); + break; + case ctCpuZeta:controlString = lang.getString("CpuZeta"); + break; + case ctNetwork:controlString = lang.getString("Network"); + break; + case ctHuman:controlString = lang.getString("Human"); + break; - case ctNetworkCpuEasy:controlString = - lang.getString ("NetworkCpuEasy"); - break; - case ctNetworkCpu:controlString = lang.getString ("NetworkCpu"); - break; - case ctNetworkCpuUltra:controlString = - lang.getString ("NetworkCpuUltra"); - break; - case ctNetworkCpuZeta:controlString = - lang.getString ("NetworkCpuZeta"); - break; + case ctNetworkCpuEasy:controlString = + lang.getString("NetworkCpuEasy"); + break; + case ctNetworkCpu:controlString = lang.getString("NetworkCpu"); + break; + case ctNetworkCpuUltra:controlString = + lang.getString("NetworkCpuUltra"); + break; + case ctNetworkCpuZeta:controlString = + lang.getString("NetworkCpuZeta"); + break; - default:printf ("Error control = %d\n", control); - assert (false); - break; - }; - } + default:printf("Error control = %d\n", control); + assert(false); + break; + }; + } - if ((control != ctHuman && control != ctNetwork) || - (control == ctNetwork && playerLeftBeforeEnd == true)) - { - controlString += " x " + floatToStr (resourceMultiplier, 1); - } + if ((control != ctHuman && control != ctNetwork) || + (control == ctNetwork && playerLeftBeforeEnd == true)) { + controlString += " x " + floatToStr(resourceMultiplier, 1); + } - result += playerName + " (" + controlString + ") "; - if (control == ctNetwork && playerLeftBeforeEnd == true) - { - result += "player left before end "; - } - result += "faction: " + factionTypeName + " "; - result += "Team: " + intToStr (teamIndex) + " "; - result += "victory: " + boolToStr (victory) + " "; - result += "# kills: " + intToStr (kills) + " "; - result += "# enemy kills: " + intToStr (enemykills) + " "; - result += "# deaths: " + intToStr (deaths) + "\n"; - result += "# units produced: " + intToStr (unitsProduced) + " "; - result += "# resources harvested: " + intToStr (resourcesHarvested); + result += playerName + " (" + controlString + ") "; + if (control == ctNetwork && playerLeftBeforeEnd == true) { + result += "player left before end "; + } + result += "faction: " + factionTypeName + " "; + result += "Team: " + intToStr(teamIndex) + " "; + result += "victory: " + boolToStr(victory) + " "; + result += "# kills: " + intToStr(kills) + " "; + result += "# enemy kills: " + intToStr(enemykills) + " "; + result += "# deaths: " + intToStr(deaths) + "\n"; + result += "# units produced: " + intToStr(unitsProduced) + " "; + result += "# resources harvested: " + intToStr(resourcesHarvested); - return result; - } + return result; + } -// ===================================================== -// class Stats -// ===================================================== + // ===================================================== + // class Stats + // ===================================================== - void Stats::init (int factionCount, int thisFactionIndex, - const string & description, const string & techName) - { - this->thisFactionIndex = thisFactionIndex; - this->factionCount = factionCount; - this->description = description; - this->techName = techName; - } + void Stats::init(int factionCount, int thisFactionIndex, + const string & description, const string & techName) { + this->thisFactionIndex = thisFactionIndex; + this->factionCount = factionCount; + this->description = description; + this->techName = techName; + } - void Stats::setVictorious (int playerIndex) - { - playerStats[playerIndex].victory = true; - } + void Stats::setVictorious(int playerIndex) { + playerStats[playerIndex].victory = true; + } - void Stats::kill (int killerFactionIndex, int killedFactionIndex, - bool isEnemy, bool isDeathCounted, bool isKillCounted) - { - if (isKillCounted == true) - { - playerStats[killerFactionIndex].kills++; - } - if (isDeathCounted == true) - { - playerStats[killedFactionIndex].deaths++; - } - if (isEnemy == true && isKillCounted == true) - { - playerStats[killerFactionIndex].enemykills++; - } - } + void Stats::kill(int killerFactionIndex, int killedFactionIndex, + bool isEnemy, bool isDeathCounted, bool isKillCounted) { + if (isKillCounted == true) { + playerStats[killerFactionIndex].kills++; + } + if (isDeathCounted == true) { + playerStats[killedFactionIndex].deaths++; + } + if (isEnemy == true && isKillCounted == true) { + playerStats[killerFactionIndex].enemykills++; + } + } - void Stats::die (int diedFactionIndex, bool isDeathCounted) - { - if (isDeathCounted == true) - { - playerStats[diedFactionIndex].deaths++; - } - } + void Stats::die(int diedFactionIndex, bool isDeathCounted) { + if (isDeathCounted == true) { + playerStats[diedFactionIndex].deaths++; + } + } - void Stats::produce (int producerFactionIndex, bool isProductionCounted) - { - if (isProductionCounted == true) - { - playerStats[producerFactionIndex].unitsProduced++; - } - } + void Stats::produce(int producerFactionIndex, bool isProductionCounted) { + if (isProductionCounted == true) { + playerStats[producerFactionIndex].unitsProduced++; + } + } - void Stats::harvest (int harvesterFactionIndex, int amount) - { - playerStats[harvesterFactionIndex].resourcesHarvested += amount; - } + void Stats::harvest(int harvesterFactionIndex, int amount) { + playerStats[harvesterFactionIndex].resourcesHarvested += amount; + } - string Stats::getStats () const - { - string result = ""; + string Stats::getStats() const { + string result = ""; - result += "Description: " + description + "\n"; - result += "Faction count: " + intToStr (factionCount) + "\n"; + result += "Description: " + description + "\n"; + result += "Faction count: " + intToStr(factionCount) + "\n"; - result += - "Game duration (mins): " + - intToStr (getFramesToCalculatePlaytime () / GameConstants::updateFps / - 60) + "\n"; - result += - "Max Concurrent Units: " + intToStr (maxConcurrentUnitCount) + "\n"; - result += - "Total EndGame Concurrent Unit Count: " + - intToStr (totalEndGameConcurrentUnitCount) + "\n"; + result += + "Game duration (mins): " + + intToStr(getFramesToCalculatePlaytime() / GameConstants::updateFps / + 60) + "\n"; + result += + "Max Concurrent Units: " + intToStr(maxConcurrentUnitCount) + "\n"; + result += + "Total EndGame Concurrent Unit Count: " + + intToStr(totalEndGameConcurrentUnitCount) + "\n"; - for (unsigned int i = 0; i < (unsigned int) factionCount; ++i) - { - const PlayerStats & player = playerStats[i]; + for (unsigned int i = 0; i < (unsigned int) factionCount; ++i) { + const PlayerStats & player = playerStats[i]; - result += - "player #" + uIntToStr (i) + " " + player.getStats () + "\n"; - } + result += + "player #" + uIntToStr(i) + " " + player.getStats() + "\n"; + } - return result; - } + return result; + } - void Stats::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *statsNode = rootNode->addChild ("Stats"); + void Stats::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *statsNode = rootNode->addChild("Stats"); -// PlayerStats playerStats[GameConstants::maxPlayers]; - for (unsigned int i = 0; i < (unsigned int) GameConstants::maxPlayers; - ++i) - { - PlayerStats & stat = playerStats[i]; + // PlayerStats playerStats[GameConstants::maxPlayers]; + for (unsigned int i = 0; i < (unsigned int) GameConstants::maxPlayers; + ++i) { + PlayerStats & stat = playerStats[i]; - XmlNode *statsNodePlayer = statsNode->addChild ("Player"); + XmlNode *statsNodePlayer = statsNode->addChild("Player"); -// ControlType control; - statsNodePlayer->addAttribute ("control", intToStr (stat.control), - mapTagReplacements); -// float resourceMultiplier; - statsNodePlayer->addAttribute ("resourceMultiplier", - floatToStr (stat.resourceMultiplier, - 6), mapTagReplacements); -// string factionTypeName; - statsNodePlayer->addAttribute ("factionTypeName", - stat.factionTypeName, - mapTagReplacements); -// FactionPersonalityType personalityType; - statsNodePlayer->addAttribute ("personalityType", - intToStr (stat.personalityType), - mapTagReplacements); -// int teamIndex; - statsNodePlayer->addAttribute ("teamIndex", - intToStr (stat.teamIndex), - mapTagReplacements); -// bool victory; - statsNodePlayer->addAttribute ("victory", intToStr (stat.victory), - mapTagReplacements); -// int kills; - statsNodePlayer->addAttribute ("kills", intToStr (stat.kills), - mapTagReplacements); -// int enemykills; - statsNodePlayer->addAttribute ("enemykills", - intToStr (stat.enemykills), - mapTagReplacements); -// int deaths; - statsNodePlayer->addAttribute ("deaths", intToStr (stat.deaths), - mapTagReplacements); -// int unitsProduced; - statsNodePlayer->addAttribute ("unitsProduced", - intToStr (stat.unitsProduced), - mapTagReplacements); -// int resourcesHarvested; - statsNodePlayer->addAttribute ("resourcesHarvested", - intToStr (stat.resourcesHarvested), - mapTagReplacements); -// string playerName; - statsNodePlayer->addAttribute ("playerName", stat.playerName, - mapTagReplacements); -// Vec3f playerColor; - statsNodePlayer->addAttribute ("playerColor", - stat.playerColor.getString (), - mapTagReplacements); - } -// string description; - statsNode->addAttribute ("description", description, - mapTagReplacements); -// int factionCount; - statsNode->addAttribute ("factionCount", intToStr (factionCount), - mapTagReplacements); -// int thisFactionIndex; - statsNode->addAttribute ("thisFactionIndex", - intToStr (thisFactionIndex), - mapTagReplacements); -// -// float worldTimeElapsed; - statsNode->addAttribute ("worldTimeElapsed", - floatToStr (worldTimeElapsed, 6), - mapTagReplacements); -// int framesPlayed; - statsNode->addAttribute ("framesPlayed", intToStr (framesPlayed), - mapTagReplacements); -// int framesToCalculatePlaytime; - statsNode->addAttribute ("framesToCalculatePlaytime", - intToStr (framesToCalculatePlaytime), - mapTagReplacements); -// int maxConcurrentUnitCount; - statsNode->addAttribute ("maxConcurrentUnitCount", - intToStr (maxConcurrentUnitCount), - mapTagReplacements); -// int totalEndGameConcurrentUnitCount; - statsNode->addAttribute ("totalEndGameConcurrentUnitCount", - intToStr (totalEndGameConcurrentUnitCount), - mapTagReplacements); -// bool isMasterserverMode; - } + // ControlType control; + statsNodePlayer->addAttribute("control", intToStr(stat.control), + mapTagReplacements); + // float resourceMultiplier; + statsNodePlayer->addAttribute("resourceMultiplier", + floatToStr(stat.resourceMultiplier, + 6), mapTagReplacements); + // string factionTypeName; + statsNodePlayer->addAttribute("factionTypeName", + stat.factionTypeName, + mapTagReplacements); + // FactionPersonalityType personalityType; + statsNodePlayer->addAttribute("personalityType", + intToStr(stat.personalityType), + mapTagReplacements); + // int teamIndex; + statsNodePlayer->addAttribute("teamIndex", + intToStr(stat.teamIndex), + mapTagReplacements); + // bool victory; + statsNodePlayer->addAttribute("victory", intToStr(stat.victory), + mapTagReplacements); + // int kills; + statsNodePlayer->addAttribute("kills", intToStr(stat.kills), + mapTagReplacements); + // int enemykills; + statsNodePlayer->addAttribute("enemykills", + intToStr(stat.enemykills), + mapTagReplacements); + // int deaths; + statsNodePlayer->addAttribute("deaths", intToStr(stat.deaths), + mapTagReplacements); + // int unitsProduced; + statsNodePlayer->addAttribute("unitsProduced", + intToStr(stat.unitsProduced), + mapTagReplacements); + // int resourcesHarvested; + statsNodePlayer->addAttribute("resourcesHarvested", + intToStr(stat.resourcesHarvested), + mapTagReplacements); + // string playerName; + statsNodePlayer->addAttribute("playerName", stat.playerName, + mapTagReplacements); + // Vec3f playerColor; + statsNodePlayer->addAttribute("playerColor", + stat.playerColor.getString(), + mapTagReplacements); + } + // string description; + statsNode->addAttribute("description", description, + mapTagReplacements); + // int factionCount; + statsNode->addAttribute("factionCount", intToStr(factionCount), + mapTagReplacements); + // int thisFactionIndex; + statsNode->addAttribute("thisFactionIndex", + intToStr(thisFactionIndex), + mapTagReplacements); + // + // float worldTimeElapsed; + statsNode->addAttribute("worldTimeElapsed", + floatToStr(worldTimeElapsed, 6), + mapTagReplacements); + // int framesPlayed; + statsNode->addAttribute("framesPlayed", intToStr(framesPlayed), + mapTagReplacements); + // int framesToCalculatePlaytime; + statsNode->addAttribute("framesToCalculatePlaytime", + intToStr(framesToCalculatePlaytime), + mapTagReplacements); + // int maxConcurrentUnitCount; + statsNode->addAttribute("maxConcurrentUnitCount", + intToStr(maxConcurrentUnitCount), + mapTagReplacements); + // int totalEndGameConcurrentUnitCount; + statsNode->addAttribute("totalEndGameConcurrentUnitCount", + intToStr(totalEndGameConcurrentUnitCount), + mapTagReplacements); + // bool isMasterserverMode; + } - void Stats::loadGame (const XmlNode * rootNode) - { - const XmlNode *statsNode = rootNode->getChild ("Stats"); + void Stats::loadGame(const XmlNode * rootNode) { + const XmlNode *statsNode = rootNode->getChild("Stats"); - // PlayerStats playerStats[GameConstants::maxPlayers]; + // PlayerStats playerStats[GameConstants::maxPlayers]; - vector < XmlNode * >statsNodePlayerList = - statsNode->getChildList ("Player"); - for (unsigned int i = 0; i < statsNodePlayerList.size (); ++i) - { - XmlNode *statsNodePlayer = statsNodePlayerList[i]; - PlayerStats & stat = playerStats[i]; + vector < XmlNode * >statsNodePlayerList = + statsNode->getChildList("Player"); + for (unsigned int i = 0; i < statsNodePlayerList.size(); ++i) { + XmlNode *statsNodePlayer = statsNodePlayerList[i]; + PlayerStats & stat = playerStats[i]; - // ControlType control; - stat.control = - static_cast < ControlType > - (statsNodePlayer->getAttribute ("control")->getIntValue ()); - // float resourceMultiplier; - stat.resourceMultiplier = - statsNodePlayer->getAttribute ("resourceMultiplier")-> - getFloatValue (); - // string factionTypeName; - stat.factionTypeName = - statsNodePlayer->getAttribute ("factionTypeName")->getValue (); - // FactionPersonalityType personalityType; - stat.personalityType = - static_cast < FactionPersonalityType > - (statsNodePlayer->getAttribute ("personalityType")-> - getIntValue ()); - // int teamIndex; - stat.teamIndex = - statsNodePlayer->getAttribute ("teamIndex")->getIntValue (); - // bool victory; - stat.victory = - statsNodePlayer->getAttribute ("victory")->getIntValue () != 0; - // int kills; - stat.kills = - statsNodePlayer->getAttribute ("kills")->getIntValue (); - // int enemykills; - stat.enemykills = - statsNodePlayer->getAttribute ("enemykills")->getIntValue (); - // int deaths; - stat.deaths = - statsNodePlayer->getAttribute ("deaths")->getIntValue (); - // int unitsProduced; - stat.unitsProduced = - statsNodePlayer->getAttribute ("unitsProduced")->getIntValue (); - // int resourcesHarvested; - stat.resourcesHarvested = - statsNodePlayer->getAttribute ("resourcesHarvested")-> - getIntValue (); - // string playerName; - stat.playerName = - statsNodePlayer->getAttribute ("playerName")->getValue (); - // Vec3f playerColor; - stat.playerColor = - Vec3f::strToVec3 (statsNodePlayer->getAttribute ("playerColor")-> - getValue ()); - } - // string description; - //statsNode->addAttribute("description",description, mapTagReplacements); - description = statsNode->getAttribute ("description")->getValue (); - // int factionCount; - factionCount = statsNode->getAttribute ("factionCount")->getIntValue (); - // int thisFactionIndex; - thisFactionIndex = - statsNode->getAttribute ("thisFactionIndex")->getIntValue (); - // - // float worldTimeElapsed; - worldTimeElapsed = - statsNode->getAttribute ("worldTimeElapsed")->getFloatValue (); - // int framesPlayed; - framesPlayed = statsNode->getAttribute ("framesPlayed")->getIntValue (); - // int framesToCalculatePlaytime; - framesToCalculatePlaytime = - statsNode->getAttribute ("framesToCalculatePlaytime")->getIntValue (); - // int maxConcurrentUnitCount; - maxConcurrentUnitCount = - statsNode->getAttribute ("maxConcurrentUnitCount")->getIntValue (); - // int totalEndGameConcurrentUnitCount; - totalEndGameConcurrentUnitCount = - statsNode->getAttribute ("totalEndGameConcurrentUnitCount")-> - getIntValue (); - // bool isMasterserverMode; - } -}} //end namespace + // ControlType control; + stat.control = + static_cast + (statsNodePlayer->getAttribute("control")->getIntValue()); + // float resourceMultiplier; + stat.resourceMultiplier = + statsNodePlayer->getAttribute("resourceMultiplier")-> + getFloatValue(); + // string factionTypeName; + stat.factionTypeName = + statsNodePlayer->getAttribute("factionTypeName")->getValue(); + // FactionPersonalityType personalityType; + stat.personalityType = + static_cast + (statsNodePlayer->getAttribute("personalityType")-> + getIntValue()); + // int teamIndex; + stat.teamIndex = + statsNodePlayer->getAttribute("teamIndex")->getIntValue(); + // bool victory; + stat.victory = + statsNodePlayer->getAttribute("victory")->getIntValue() != 0; + // int kills; + stat.kills = + statsNodePlayer->getAttribute("kills")->getIntValue(); + // int enemykills; + stat.enemykills = + statsNodePlayer->getAttribute("enemykills")->getIntValue(); + // int deaths; + stat.deaths = + statsNodePlayer->getAttribute("deaths")->getIntValue(); + // int unitsProduced; + stat.unitsProduced = + statsNodePlayer->getAttribute("unitsProduced")->getIntValue(); + // int resourcesHarvested; + stat.resourcesHarvested = + statsNodePlayer->getAttribute("resourcesHarvested")-> + getIntValue(); + // string playerName; + stat.playerName = + statsNodePlayer->getAttribute("playerName")->getValue(); + // Vec3f playerColor; + stat.playerColor = + Vec3f::strToVec3(statsNodePlayer->getAttribute("playerColor")-> + getValue()); + } + // string description; + //statsNode->addAttribute("description",description, mapTagReplacements); + description = statsNode->getAttribute("description")->getValue(); + // int factionCount; + factionCount = statsNode->getAttribute("factionCount")->getIntValue(); + // int thisFactionIndex; + thisFactionIndex = + statsNode->getAttribute("thisFactionIndex")->getIntValue(); + // + // float worldTimeElapsed; + worldTimeElapsed = + statsNode->getAttribute("worldTimeElapsed")->getFloatValue(); + // int framesPlayed; + framesPlayed = statsNode->getAttribute("framesPlayed")->getIntValue(); + // int framesToCalculatePlaytime; + framesToCalculatePlaytime = + statsNode->getAttribute("framesToCalculatePlaytime")->getIntValue(); + // int maxConcurrentUnitCount; + maxConcurrentUnitCount = + statsNode->getAttribute("maxConcurrentUnitCount")->getIntValue(); + // int totalEndGameConcurrentUnitCount; + totalEndGameConcurrentUnitCount = + statsNode->getAttribute("totalEndGameConcurrentUnitCount")-> + getIntValue(); + // bool isMasterserverMode; + } + } +} //end namespace diff --git a/source/glest_game/game/stats.h b/source/glest_game/game/stats.h index 5da4a51c2..0f2b81153 100644 --- a/source/glest_game/game/stats.h +++ b/source/glest_game/game/stats.h @@ -25,395 +25,349 @@ # include "leak_dumper.h" using - std::string; +std::string; using namespace - Shared::Graphics; +Shared::Graphics; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - class - PlayerStats - { - public: - PlayerStats (); + class + PlayerStats { + public: + PlayerStats(); - ControlType - control; - float - resourceMultiplier; - string - factionTypeName; - FactionPersonalityType - personalityType; - int - teamIndex; - bool - victory; - int - kills; - int - enemykills; - int - deaths; - int - unitsProduced; - int - resourcesHarvested; - string - playerName; - bool - playerLeftBeforeEnd; - int - timePlayerLeft; - Vec3f - playerColor; + ControlType + control; + float + resourceMultiplier; + string + factionTypeName; + FactionPersonalityType + personalityType; + int + teamIndex; + bool + victory; + int + kills; + int + enemykills; + int + deaths; + int + unitsProduced; + int + resourcesHarvested; + string + playerName; + bool + playerLeftBeforeEnd; + int + timePlayerLeft; + Vec3f + playerColor; - string - getStats () const; - }; + string + getStats() const; + }; -// ===================================================== -// class Stats -// -/// Player statistics that are shown after the game ends -// ===================================================== + // ===================================================== + // class Stats + // + /// Player statistics that are shown after the game ends + // ===================================================== - class - Stats - { - private: - PlayerStats - playerStats[GameConstants::maxPlayers]; + class + Stats { + private: + PlayerStats + playerStats[GameConstants::maxPlayers]; - string - description; - int - factionCount; - int - thisFactionIndex; + string + description; + int + factionCount; + int + thisFactionIndex; - float - worldTimeElapsed; - int - framesPlayed; - int - framesToCalculatePlaytime; - int - maxConcurrentUnitCount; - int - totalEndGameConcurrentUnitCount; - bool - isMasterserverMode; - string - techName; + float + worldTimeElapsed; + int + framesPlayed; + int + framesToCalculatePlaytime; + int + maxConcurrentUnitCount; + int + totalEndGameConcurrentUnitCount; + bool + isMasterserverMode; + string + techName; - public: + public: - Stats () - { - //description = ""; - factionCount = 0; - thisFactionIndex = 0; + Stats() { + //description = ""; + factionCount = 0; + thisFactionIndex = 0; - worldTimeElapsed = 0.0; - framesPlayed = 0; - framesToCalculatePlaytime = 0; - maxConcurrentUnitCount = 0; - totalEndGameConcurrentUnitCount = 0; - isMasterserverMode = false; - //techName = ""; - } + worldTimeElapsed = 0.0; + framesPlayed = 0; + framesToCalculatePlaytime = 0; + maxConcurrentUnitCount = 0; + totalEndGameConcurrentUnitCount = 0; + isMasterserverMode = false; + //techName = ""; + } - void - init (int factionCount, int thisFactionIndex, - const string & description, const string & techName); + void + init(int factionCount, int thisFactionIndex, + const string & description, const string & techName); - string - getDescription () const - { - return - description; - } - int - getThisFactionIndex () const - { - return - thisFactionIndex; - } - int - getFactionCount () const - { - return - factionCount; - } + string + getDescription() const { + return + description; + } + int + getThisFactionIndex() const { + return + thisFactionIndex; + } + int + getFactionCount() const { + return + factionCount; + } - float - getWorldTimeElapsed () const - { - return - worldTimeElapsed; - } - int - getFramesPlayed () const - { - return - framesPlayed; - } - int - getFramesToCalculatePlaytime () const - { - return - framesToCalculatePlaytime; - } - int - getMaxConcurrentUnitCount () const - { - return - maxConcurrentUnitCount; - } - int - getTotalEndGameConcurrentUnitCount () const - { - return - totalEndGameConcurrentUnitCount; - } + float + getWorldTimeElapsed() const { + return + worldTimeElapsed; + } + int + getFramesPlayed() const { + return + framesPlayed; + } + int + getFramesToCalculatePlaytime() const { + return + framesToCalculatePlaytime; + } + int + getMaxConcurrentUnitCount() const { + return + maxConcurrentUnitCount; + } + int + getTotalEndGameConcurrentUnitCount() const { + return + totalEndGameConcurrentUnitCount; + } - const - string & - getFactionTypeName (int factionIndex) const - { - return - playerStats[factionIndex]. - factionTypeName; - } - FactionPersonalityType - getPersonalityType (int factionIndex) const - { - return - playerStats[factionIndex]. - personalityType; - } - ControlType - getControl (int factionIndex) const - { - return - playerStats[factionIndex]. - control; - } - float - getResourceMultiplier (int factionIndex) const - { - return - playerStats[factionIndex]. - resourceMultiplier; - } - bool - getVictory (int factionIndex) const - { - return - playerStats[factionIndex]. - victory; - } - int - getTeam (int factionIndex) const - { - return - playerStats[factionIndex]. - teamIndex; - } - int - getKills (int factionIndex) const - { - return - playerStats[factionIndex]. - kills; - } - int - getEnemyKills (int factionIndex) const - { - return - playerStats[factionIndex]. - enemykills; - } - int - getDeaths (int factionIndex) const - { - return - playerStats[factionIndex]. - deaths; - } - int - getUnitsProduced (int factionIndex) const - { - return - playerStats[factionIndex]. - unitsProduced; - } - int - getResourcesHarvested (int factionIndex) const - { - return - playerStats[factionIndex]. - resourcesHarvested; - } - string - getPlayerName (int factionIndex) const - { - return - playerStats[factionIndex]. - playerName; - } - bool - getPlayerLeftBeforeEnd (int factionIndex) const - { - return - playerStats[factionIndex]. - playerLeftBeforeEnd; - } - void - setPlayerLeftBeforeEnd (int factionIndex, bool value) - { - playerStats[factionIndex].playerLeftBeforeEnd = value; - } - Vec3f - getPlayerColor (int factionIndex) const - { - return - playerStats[factionIndex]. - playerColor; - } + const + string & + getFactionTypeName(int factionIndex) const { + return + playerStats[factionIndex]. + factionTypeName; + } + FactionPersonalityType + getPersonalityType(int factionIndex) const { + return + playerStats[factionIndex]. + personalityType; + } + ControlType + getControl(int factionIndex) const { + return + playerStats[factionIndex]. + control; + } + float + getResourceMultiplier(int factionIndex) const { + return + playerStats[factionIndex]. + resourceMultiplier; + } + bool + getVictory(int factionIndex) const { + return + playerStats[factionIndex]. + victory; + } + int + getTeam(int factionIndex) const { + return + playerStats[factionIndex]. + teamIndex; + } + int + getKills(int factionIndex) const { + return + playerStats[factionIndex]. + kills; + } + int + getEnemyKills(int factionIndex) const { + return + playerStats[factionIndex]. + enemykills; + } + int + getDeaths(int factionIndex) const { + return + playerStats[factionIndex]. + deaths; + } + int + getUnitsProduced(int factionIndex) const { + return + playerStats[factionIndex]. + unitsProduced; + } + int + getResourcesHarvested(int factionIndex) const { + return + playerStats[factionIndex]. + resourcesHarvested; + } + string + getPlayerName(int factionIndex) const { + return + playerStats[factionIndex]. + playerName; + } + bool + getPlayerLeftBeforeEnd(int factionIndex) const { + return + playerStats[factionIndex]. + playerLeftBeforeEnd; + } + void + setPlayerLeftBeforeEnd(int factionIndex, bool value) { + playerStats[factionIndex].playerLeftBeforeEnd = value; + } + Vec3f + getPlayerColor(int factionIndex) const { + return + playerStats[factionIndex]. + playerColor; + } - int - getTimePlayerLeft (int factionIndex) const - { - return - playerStats[factionIndex]. - timePlayerLeft; - } - void - setTimePlayerLeft (int factionIndex, int value) - { - playerStats[factionIndex].timePlayerLeft = value; - } + int + getTimePlayerLeft(int factionIndex) const { + return + playerStats[factionIndex]. + timePlayerLeft; + } + void + setTimePlayerLeft(int factionIndex, int value) { + playerStats[factionIndex].timePlayerLeft = value; + } - bool - getIsMasterserverMode () const - { - return - isMasterserverMode; - } - void - setIsMasterserverMode (bool value) - { - isMasterserverMode = value; - } + bool + getIsMasterserverMode() const { + return + isMasterserverMode; + } + void + setIsMasterserverMode(bool value) { + isMasterserverMode = value; + } - void - setDescription (const string & description) - { - this->description = description; - } - void - setWorldTimeElapsed (float value) - { - this->worldTimeElapsed = value; - } - void - setFramesPlayed (int value) - { - this->framesPlayed = value; - } - void - setMaxConcurrentUnitCount (int value) - { - this->maxConcurrentUnitCount = value; - } - void - setTotalEndGameConcurrentUnitCount (int value) - { - this->totalEndGameConcurrentUnitCount = value; - } + void + setDescription(const string & description) { + this->description = description; + } + void + setWorldTimeElapsed(float value) { + this->worldTimeElapsed = value; + } + void + setFramesPlayed(int value) { + this->framesPlayed = value; + } + void + setMaxConcurrentUnitCount(int value) { + this->maxConcurrentUnitCount = value; + } + void + setTotalEndGameConcurrentUnitCount(int value) { + this->totalEndGameConcurrentUnitCount = value; + } - void - setFactionTypeName (int playerIndex, const string & factionTypeName) - { - playerStats[playerIndex].factionTypeName = factionTypeName; - } - void - setPersonalityType (int playerIndex, FactionPersonalityType value) - { - playerStats[playerIndex].personalityType = value; - } - void - setControl (int playerIndex, ControlType control) - { - playerStats[playerIndex].control = control; - } - void - setResourceMultiplier (int playerIndex, float resourceMultiplier) - { - playerStats[playerIndex].resourceMultiplier = resourceMultiplier; - } - void - setTeam (int playerIndex, int teamIndex) - { - playerStats[playerIndex].teamIndex = teamIndex; - } - void - setVictorious (int playerIndex); - void - kill (int killerFactionIndex, int killedFactionIndex, bool isEnemy, - bool isDeathCounted, bool isKillCounted); - void - die (int diedFactionIndex, bool isDeathCounted); - void - produce (int producerFactionIndex, bool isProductionCounted); - void - harvest (int harvesterFactionIndex, int amount); - void - setPlayerName (int playerIndex, const string & value) - { - playerStats[playerIndex].playerName = value; - } - void - setPlayerColor (int playerIndex, Vec3f value) - { - playerStats[playerIndex].playerColor = value; - } + void + setFactionTypeName(int playerIndex, const string & factionTypeName) { + playerStats[playerIndex].factionTypeName = factionTypeName; + } + void + setPersonalityType(int playerIndex, FactionPersonalityType value) { + playerStats[playerIndex].personalityType = value; + } + void + setControl(int playerIndex, ControlType control) { + playerStats[playerIndex].control = control; + } + void + setResourceMultiplier(int playerIndex, float resourceMultiplier) { + playerStats[playerIndex].resourceMultiplier = resourceMultiplier; + } + void + setTeam(int playerIndex, int teamIndex) { + playerStats[playerIndex].teamIndex = teamIndex; + } + void + setVictorious(int playerIndex); + void + kill(int killerFactionIndex, int killedFactionIndex, bool isEnemy, + bool isDeathCounted, bool isKillCounted); + void + die(int diedFactionIndex, bool isDeathCounted); + void + produce(int producerFactionIndex, bool isProductionCounted); + void + harvest(int harvesterFactionIndex, int amount); + void + setPlayerName(int playerIndex, const string & value) { + playerStats[playerIndex].playerName = value; + } + void + setPlayerColor(int playerIndex, Vec3f value) { + playerStats[playerIndex].playerColor = value; + } - void - addFramesToCalculatePlaytime () - { - this->framesToCalculatePlaytime++; - } + void + addFramesToCalculatePlaytime() { + this->framesToCalculatePlaytime++; + } - void - setTechName (const string & name) - { - techName = name; - } - string - getTechName () const - { - return - techName; - } + void + setTechName(const string & name) { + techName = name; + } + string + getTechName() const { + return + techName; + } - string - getStats () const; + string + getStats() const; - void - saveGame (XmlNode * rootNode); - void - loadGame (const XmlNode * rootNode); - }; + void + saveGame(XmlNode * rootNode); + void + loadGame(const XmlNode * rootNode); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/global/config.cpp b/source/glest_game/global/config.cpp index c3a72d51a..10686ba4b 100644 --- a/source/glest_game/global/config.cpp +++ b/source/glest_game/global/config.cpp @@ -37,1236 +37,1045 @@ using namespace Shared::Platform; using namespace Shared::Util; using namespace std; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - int GameConstants::networkFramePeriod = 20; - int GameConstants::updateFps = 40; - int GameConstants::cameraFps = 100; + int GameConstants::networkFramePeriod = 20; + int GameConstants::updateFps = 40; + int GameConstants::cameraFps = 100; - const float GameConstants::normalMultiplier = 1.0f; - const float GameConstants::easyMultiplier = 0.5f; - const float GameConstants::ultraMultiplier = 1.5f; - const float GameConstants::megaMultiplier = 2.5f; + const float GameConstants::normalMultiplier = 1.0f; + const float GameConstants::easyMultiplier = 0.5f; + const float GameConstants::ultraMultiplier = 1.5f; + const float GameConstants::megaMultiplier = 2.5f; - const char *GameConstants::folder_path_maps = "maps"; - const char *GameConstants::folder_path_scenarios = "scenarios"; - const char *GameConstants::folder_path_techs = "techs"; - const char *GameConstants::folder_path_tilesets = "tilesets"; - const char *GameConstants::folder_path_tutorials = "tutorials"; + const char *GameConstants::folder_path_maps = "maps"; + const char *GameConstants::folder_path_scenarios = "scenarios"; + const char *GameConstants::folder_path_techs = "techs"; + const char *GameConstants::folder_path_tilesets = "tilesets"; + const char *GameConstants::folder_path_tutorials = "tutorials"; - const char *GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME = "???"; - const char *GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME = "Closed"; + const char *GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME = "???"; + const char *GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME = "Closed"; - const char *GameConstants::folder_path_screenshots = "screens/"; + const char *GameConstants::folder_path_screenshots = "screens/"; - const char *GameConstants::OBSERVER_SLOTNAME = "*Observer*"; - const char *GameConstants::RANDOMFACTION_SLOTNAME = "*Random*"; + const char *GameConstants::OBSERVER_SLOTNAME = "*Observer*"; + const char *GameConstants::RANDOMFACTION_SLOTNAME = "*Random*"; - const char *GameConstants::steamCacheInstanceKey = "steamInstanceCache"; - const char *GameConstants::preCacheThreadCacheLookupKey = - "preCacheThreadCache"; - const char *GameConstants::ircClientCacheLookupKey = "ircClientCache"; - const char *GameConstants::playerTextureCacheLookupKey = - "playerTextureCache"; - const char *GameConstants::factionPreviewTextureCacheLookupKey = - "factionPreviewTextureCache"; - const char *GameConstants::characterMenuScreenPositionListCacheLookupKey = - "characterMenuScreenPositionListCache"; - const char *GameConstants::application_name = "ZetaGlest"; + const char *GameConstants::steamCacheInstanceKey = "steamInstanceCache"; + const char *GameConstants::preCacheThreadCacheLookupKey = + "preCacheThreadCache"; + const char *GameConstants::ircClientCacheLookupKey = "ircClientCache"; + const char *GameConstants::playerTextureCacheLookupKey = + "playerTextureCache"; + const char *GameConstants::factionPreviewTextureCacheLookupKey = + "factionPreviewTextureCache"; + const char *GameConstants::characterMenuScreenPositionListCacheLookupKey = + "characterMenuScreenPositionListCache"; + const char *GameConstants::application_name = "ZetaGlest"; - const char *GameConstants::LOADING_SCREEN_FILE = "loading_screen"; - const char *GameConstants::LOADING_SCREEN_FILE_FILTER = - "loading_screen*.*"; - const char *GameConstants::PREVIEW_SCREEN_FILE = "preview_screen"; - const char *GameConstants::PREVIEW_SCREEN_FILE_FILTER = - "preview_screen*.*"; - const char *GameConstants::HUD_SCREEN_FILE = "hud"; - const char *GameConstants::HUD_SCREEN_FILE_FILTER = "hud*.*"; + const char *GameConstants::LOADING_SCREEN_FILE = "loading_screen"; + const char *GameConstants::LOADING_SCREEN_FILE_FILTER = + "loading_screen*.*"; + const char *GameConstants::PREVIEW_SCREEN_FILE = "preview_screen"; + const char *GameConstants::PREVIEW_SCREEN_FILE_FILTER = + "preview_screen*.*"; + const char *GameConstants::HUD_SCREEN_FILE = "hud"; + const char *GameConstants::HUD_SCREEN_FILE_FILTER = "hud*.*"; - const char *GameConstants::pathCacheLookupKey = "pathCache_"; - const char *GameConstants::path_data_CacheLookupKey = "data"; - const char *GameConstants::path_ini_CacheLookupKey = "ini"; - const char *GameConstants::path_logs_CacheLookupKey = "logs"; + const char *GameConstants::pathCacheLookupKey = "pathCache_"; + const char *GameConstants::path_data_CacheLookupKey = "data"; + const char *GameConstants::path_ini_CacheLookupKey = "ini"; + const char *GameConstants::path_logs_CacheLookupKey = "logs"; - const char *GameConstants::saveNetworkGameFileServer = - "zetaglest-saved-server.xml"; - const char *GameConstants::saveNetworkGameFileServerCompressed = - "zetaglest-saved-server.zip"; + const char *GameConstants::saveNetworkGameFileServer = + "zetaglest-saved-server.xml"; + const char *GameConstants::saveNetworkGameFileServerCompressed = + "zetaglest-saved-server.zip"; - const char *GameConstants::saveNetworkGameFileClient = - "zetaglest-saved-client.xml"; - const char *GameConstants::saveNetworkGameFileClientCompressed = - "zetaglest-saved-client.zip"; + const char *GameConstants::saveNetworkGameFileClient = + "zetaglest-saved-client.xml"; + const char *GameConstants::saveNetworkGameFileClientCompressed = + "zetaglest-saved-client.zip"; - const char *GameConstants::saveGameFileDefault = "zetaglest-saved.xml"; - const char *GameConstants::saveGameFileAutoTestDefault = - "zetaglest-auto-saved_%s.xml"; - const char *GameConstants::saveGameFilePattern = "zetaglest-saved_%s.xml"; + const char *GameConstants::saveGameFileDefault = "zetaglest-saved.xml"; + const char *GameConstants::saveGameFileAutoTestDefault = + "zetaglest-auto-saved_%s.xml"; + const char *GameConstants::saveGameFilePattern = "zetaglest-saved_%s.xml"; - const char *Config::glest_ini_filename = "glest.ini"; - const char *Config::glestuser_ini_filename = "glestuser.ini"; + const char *Config::glest_ini_filename = "glest.ini"; + const char *Config::glestuser_ini_filename = "glestuser.ini"; - const char *Config::glestkeys_ini_filename = "glestkeys.ini"; - const char *Config::glestuserkeys_ini_filename = "glestuserkeys.ini"; + const char *Config::glestkeys_ini_filename = "glestkeys.ini"; + const char *Config::glestuserkeys_ini_filename = "glestuserkeys.ini"; - const char *Config::ACTIVE_MOD_PROPERTY_NAME = "current_mod_name"; + const char *Config::ACTIVE_MOD_PROPERTY_NAME = "current_mod_name"; - const char *Config::colorPicking = "color"; - const char *Config::selectBufPicking = "selectbuf"; - const char *Config::frustumPicking = "frustum"; + const char *Config::colorPicking = "color"; + const char *Config::selectBufPicking = "selectbuf"; + const char *Config::frustumPicking = "frustum"; - map < string, string > Config::customRuntimeProperties; + map < string, string > Config::customRuntimeProperties; -// ===================================================== -// class Config -// ===================================================== + // ===================================================== + // class Config + // ===================================================== - const string defaultNotFoundValue = "~~NOT FOUND~~"; + const string defaultNotFoundValue = "~~NOT FOUND~~"; - map < ConfigType, Config > Config::configList; + map < ConfigType, Config > Config::configList; - Config::Config () - { - fileLoaded.first = false; - fileLoaded.second = false; - cfgType.first = cfgMainGame; - cfgType.second = cfgUserGame; - fileName.first = ""; - fileName.second = ""; - fileNameParameter.first = ""; - fileNameParameter.second = ""; - custom_path_parameter = ""; - } + Config::Config() { + fileLoaded.first = false; + fileLoaded.second = false; + cfgType.first = cfgMainGame; + cfgType.second = cfgUserGame; + fileName.first = ""; + fileName.second = ""; + fileNameParameter.first = ""; + fileNameParameter.second = ""; + custom_path_parameter = ""; + } - bool Config::tryCustomPath (std::pair < ConfigType, ConfigType > &type, - std::pair < string, string > &file, - string custom_path) - { - bool wasFound = false; - if ((type.first == cfgMainGame && type.second == cfgUserGame && - file.first == glest_ini_filename - && file.second == glestuser_ini_filename) - || (type.first == cfgMainKeys && type.second == cfgUserKeys - && file.first == glestkeys_ini_filename - && file.second == glestuserkeys_ini_filename)) - { + bool Config::tryCustomPath(std::pair < ConfigType, ConfigType > &type, + std::pair < string, string > &file, + string custom_path) { + bool wasFound = false; + if ((type.first == cfgMainGame && type.second == cfgUserGame && + file.first == glest_ini_filename + && file.second == glestuser_ini_filename) + || (type.first == cfgMainKeys && type.second == cfgUserKeys + && file.first == glestkeys_ini_filename + && file.second == glestuserkeys_ini_filename)) { - string linuxPath = custom_path; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("-=-=-=-=-=-=-= looking for file in possible location [%s]\n", - linuxPath.c_str ()); + string linuxPath = custom_path; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("-=-=-=-=-=-=-= looking for file in possible location [%s]\n", + linuxPath.c_str()); #if defined(__linux__) - string nixFile = linuxPath + "linux_" + file.first; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("-=-=-=-=-=-=-= looking for linux specific file in possible location [%s]\n", - nixFile.c_str ()); + string nixFile = linuxPath + "linux_" + file.first; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("-=-=-=-=-=-=-= looking for linux specific file in possible location [%s]\n", + nixFile.c_str()); - if (wasFound == false && fileExists (nixFile) == true) - { - file.first = nixFile; - file.second = linuxPath + file.second; - wasFound = true; - } + if (wasFound == false && fileExists(nixFile) == true) { + file.first = nixFile; + file.second = linuxPath + file.second; + wasFound = true; + } #elif defined(__WIN32__) - string winFile = linuxPath + "windows_" + file.first; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("-=-=-=-=-=-=-= looking for windows specific file in possible location [%s]\n", - winFile.c_str ()); + string winFile = linuxPath + "windows_" + file.first; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("-=-=-=-=-=-=-= looking for windows specific file in possible location [%s]\n", + winFile.c_str()); - if (wasFound == false && fileExists (winFile) == true) - { - file.first = winFile; - file.second = linuxPath + file.second; - wasFound = true; - } + if (wasFound == false && fileExists(winFile) == true) { + file.first = winFile; + file.second = linuxPath + file.second; + wasFound = true; + } #endif - if (wasFound == false && fileExists (linuxPath + file.first) == true) - { - file.first = linuxPath + file.first; - file.second = linuxPath + file.second; - wasFound = true; - } - } - return wasFound; - } + if (wasFound == false && fileExists(linuxPath + file.first) == true) { + file.first = linuxPath + file.first; + file.second = linuxPath + file.second; + wasFound = true; + } + } + return wasFound; + } - Config::Config (std::pair < ConfigType, ConfigType > type, - std::pair < string, string > file, std::pair < bool, - bool > fileMustExist, string custom_path) - { - fileLoaded.first = false; - fileLoaded.second = false; - cfgType = type; - fileName = file; - fileNameParameter = file; - custom_path_parameter = custom_path; + Config::Config(std::pair < ConfigType, ConfigType > type, + std::pair < string, string > file, std::pair < bool, + bool > fileMustExist, string custom_path) { + fileLoaded.first = false; + fileLoaded.second = false; + cfgType = type; + fileName = file; + fileNameParameter = file; + custom_path_parameter = custom_path; - if (getGameReadWritePath (GameConstants::path_ini_CacheLookupKey) != "") - { - fileName.first = - getGameReadWritePath (GameConstants::path_ini_CacheLookupKey) + - fileName.first; - fileName.second = - getGameReadWritePath (GameConstants::path_ini_CacheLookupKey) + - fileName.second; - } + if (getGameReadWritePath(GameConstants::path_ini_CacheLookupKey) != "") { + fileName.first = + getGameReadWritePath(GameConstants::path_ini_CacheLookupKey) + + fileName.first; + fileName.second = + getGameReadWritePath(GameConstants::path_ini_CacheLookupKey) + + fileName.second; + } - bool foundPath = false; - string currentpath = custom_path; + bool foundPath = false; + string currentpath = custom_path; - if (custom_path != "") - { - foundPath = tryCustomPath (cfgType, fileName, custom_path); - } + if (custom_path != "") { + foundPath = tryCustomPath(cfgType, fileName, custom_path); + } - if (foundPath == false) - { - currentpath = - extractDirectoryPathFromFile (Properties::getApplicationPath ()); - foundPath = tryCustomPath (cfgType, fileName, currentpath); - } + if (foundPath == false) { + currentpath = + extractDirectoryPathFromFile(Properties::getApplicationPath()); + foundPath = tryCustomPath(cfgType, fileName, currentpath); + } #if defined(CUSTOM_DATA_INSTALL_PATH) - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, - formatPath (TOSTRING (CUSTOM_DATA_INSTALL_PATH))); - } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, + formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH))); + } #endif #if defined(MG_CMAKE_INSTALL_PREFIX) - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, - formatPath (TOSTRING (MG_CMAKE_INSTALL_PREFIX))); - } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, + formatPath(TOSTRING(MG_CMAKE_INSTALL_PREFIX))); + } #endif -// Look in standard linux shared paths for ini files + // Look in standard linux shared paths for ini files #if defined(__linux__) - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, "/usr/share/zetaglest/"); - } - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, "/usr/share/games/zetaglest/"); - } - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, "/usr/local/share/zetaglest/"); - } - if (foundPath == false) - { - foundPath = - tryCustomPath (cfgType, fileName, - "/usr/local/share/games/zetaglest/"); - } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, "/usr/share/zetaglest/"); + } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, "/usr/share/games/zetaglest/"); + } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, "/usr/local/share/zetaglest/"); + } + if (foundPath == false) { + foundPath = + tryCustomPath(cfgType, fileName, + "/usr/local/share/games/zetaglest/"); + } #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("foundPath = [%d]\n", foundPath); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("foundPath = [%d]\n", foundPath); - if (fileMustExist.first == true && fileExists (fileName.first) == false) - { - //string currentpath = extractDirectoryPathFromFile(Properties::getApplicationPath()); - fileName.first = currentpath + fileName.first; - } + if (fileMustExist.first == true && fileExists(fileName.first) == false) { + //string currentpath = extractDirectoryPathFromFile(Properties::getApplicationPath()); + fileName.first = currentpath + fileName.first; + } #if defined(WIN32) - //string test = "C:\\Code\\zetaglest\\mk\\windows\\.\\..\\..\\data\\glest_game\\glest.ini"; - //updatePathClimbingParts(test); + //string test = "C:\\Code\\zetaglest\\mk\\windows\\.\\..\\..\\data\\glest_game\\glest.ini"; + //updatePathClimbingParts(test); - updatePathClimbingParts (fileName.first); + updatePathClimbingParts(fileName.first); #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("-=-=-=-=-=-=-= About to load fileName.first = [%s]\n", - fileName.first.c_str ()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("-=-=-=-=-=-=-= About to load fileName.first = [%s]\n", + fileName.first.c_str()); - if (fileMustExist.first == true || - (fileMustExist.first == false - && fileExists (fileName.first) == true)) - { - properties.first.load (fileName.first); - fileLoaded.first = true; - } + if (fileMustExist.first == true || + (fileMustExist.first == false + && fileExists(fileName.first) == true)) { + properties.first.load(fileName.first); + fileLoaded.first = true; + } - if (cfgType.first == cfgMainGame) - { - if (properties.first. - getString ("UserData_Root", - defaultNotFoundValue.c_str ()) != defaultNotFoundValue) - { - string userData = properties.first.getString ("UserData_Root"); - if (userData != "") - { - endPathWithSlash (userData); - } - fileName.second = userData + fileNameParameter.second; - } - else if (properties.first. - getString ("UserOverrideFile", - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - string userData = properties.first.getString ("UserOverrideFile"); - if (userData != "") - { - endPathWithSlash (userData); - } - fileName.second = userData + fileNameParameter.second; - } + if (cfgType.first == cfgMainGame) { + if (properties.first. + getString("UserData_Root", + defaultNotFoundValue.c_str()) != defaultNotFoundValue) { + string userData = properties.first.getString("UserData_Root"); + if (userData != "") { + endPathWithSlash(userData); + } + fileName.second = userData + fileNameParameter.second; + } else if (properties.first. + getString("UserOverrideFile", + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + string userData = properties.first.getString("UserOverrideFile"); + if (userData != "") { + endPathWithSlash(userData); + } + fileName.second = userData + fileNameParameter.second; + } #if defined(WIN32) - updatePathClimbingParts (fileName.second); + updatePathClimbingParts(fileName.second); #endif - } - else if (cfgType.first == cfgMainKeys) - { - Config & mainCfg = Config::getInstance (); - if (mainCfg. - getString ("UserData_Root", - defaultNotFoundValue.c_str ()) != defaultNotFoundValue) - { - string userData = mainCfg.getString ("UserData_Root"); - if (userData != "") - { - endPathWithSlash (userData); - } - fileName.second = userData + fileNameParameter.second; - } - else if (mainCfg. - getString ("UserOverrideFile", - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - string userData = mainCfg.getString ("UserOverrideFile"); - if (userData != "") - { - endPathWithSlash (userData); - } - fileName.second = userData + fileNameParameter.second; - } + } else if (cfgType.first == cfgMainKeys) { + Config & mainCfg = Config::getInstance(); + if (mainCfg. + getString("UserData_Root", + defaultNotFoundValue.c_str()) != defaultNotFoundValue) { + string userData = mainCfg.getString("UserData_Root"); + if (userData != "") { + endPathWithSlash(userData); + } + fileName.second = userData + fileNameParameter.second; + } else if (mainCfg. + getString("UserOverrideFile", + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + string userData = mainCfg.getString("UserOverrideFile"); + if (userData != "") { + endPathWithSlash(userData); + } + fileName.second = userData + fileNameParameter.second; + } #if defined(WIN32) - updatePathClimbingParts (fileName.second); + updatePathClimbingParts(fileName.second); #endif - } + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("-=-=-=-=-=-=-= About to load fileName.second = [%s]\n", - fileName.second.c_str ()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("-=-=-=-=-=-=-= About to load fileName.second = [%s]\n", + fileName.second.c_str()); - if (fileMustExist.second == true || - (fileMustExist.second == false - && fileExists (fileName.second) == true)) - { - properties.second.load (fileName.second); - fileLoaded.second = true; - } + if (fileMustExist.second == true || + (fileMustExist.second == false + && fileExists(fileName.second) == true)) { + properties.second.load(fileName.second); + fileLoaded.second = true; + } - try - { - if (fileName.second != "" && fileExists (fileName.second) == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] attempting to auto-create cfgFile.second = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.second.c_str ()); + try { + if (fileName.second != "" && fileExists(fileName.second) == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] attempting to auto-create cfgFile.second = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.second.c_str()); #if defined(WIN32) && !defined(__MINGW32__) - wstring wstr = utf8_decode (fileName.second); - FILE *fp = _wfopen (wstr.c_str (), L"w"); - std::ofstream userFile (fp); + wstring wstr = utf8_decode(fileName.second); + FILE *fp = _wfopen(wstr.c_str(), L"w"); + std::ofstream userFile(fp); #else - std::ofstream userFile; - userFile.open (fileName.second.c_str (), - ios_base::out | ios_base::trunc); + std::ofstream userFile; + userFile.open(fileName.second.c_str(), + ios_base::out | ios_base::trunc); #endif - userFile.close (); + userFile.close(); #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - { - fclose (fp); - } + if (fp) { + fclose(fp); + } #endif - fileLoaded.second = true; - properties.second.load (fileName.second); - } - } - catch (const exception & ex) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ERROR trying to auto-create cfgFile.second = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.second.c_str ()); - } - } - - Config & Config::getInstance (std::pair < ConfigType, ConfigType > type, - std::pair < string, string > file, - std::pair < bool, bool > fileMustExist, - string custom_path) - { - if (configList.find (type.first) == configList.end ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Config config (type, file, fileMustExist, custom_path); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - configList.insert (map < ConfigType, - Config >::value_type (type.first, config)); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - return configList.find (type.first)->second; - } - - void Config::CopyAll (Config * src, Config * dest) - { - - dest->properties = src->properties; - dest->cfgType = src->cfgType; - dest->fileName = src->fileName; - dest->fileNameParameter = src->fileNameParameter; - dest->fileLoaded = src->fileLoaded; - } - - void Config::reload () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - std::pair < ConfigType, ConfigType > type = - std::make_pair (cfgType.first, cfgType.second); - Config newconfig (type, - std::make_pair (fileNameParameter.first, - fileNameParameter.second), - std::make_pair (true, false), custom_path_parameter); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Config & oldconfig = configList.find (type.first)->second; - CopyAll (&newconfig, &oldconfig); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void Config::save (const string & path) - { - if (fileLoaded.second == true) - { - if (path != "") - { - fileName.second = path; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] save file [%s]\n", __FILE__, - __FUNCTION__, __LINE__, fileName.second.c_str ()); - properties.second.save (fileName.second); - return; - } - - if (path != "") - { - fileName.first = path; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] save file [%s]\n", __FILE__, - __FUNCTION__, __LINE__, fileName.first.c_str ()); - properties.first.save (fileName.first); - } - - int Config::getInt (const char *key, const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getInt (key, defaultValueIfNotFound); - } - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getInt (key, defaultValueIfNotFound); - } - return properties.first.getInt (key, defaultValueIfNotFound); - } - - bool Config::getBool (const char *key, const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getBool (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getBool (key, defaultValueIfNotFound); - } - - return properties.first.getBool (key, defaultValueIfNotFound); - } - - float Config::getFloat (const char *key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getFloat (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getFloat (key, defaultValueIfNotFound); - } - - return properties.first.getFloat (key, defaultValueIfNotFound); - } - - const string Config::getString (const char *key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getString (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getString (key, defaultValueIfNotFound); - } - - return properties.first.getString (key, defaultValueIfNotFound); - } - - int Config::getInt (const string & key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getInt (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getInt (key, defaultValueIfNotFound); - } - - return properties.first.getInt (key, defaultValueIfNotFound); - } - - bool Config::getBool (const string & key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getBool (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getBool (key, defaultValueIfNotFound); - } - - return properties.first.getBool (key, defaultValueIfNotFound); - } - - float Config::getFloat (const string & key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getFloat (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getFloat (key, defaultValueIfNotFound); - } - - return properties.first.getFloat (key, defaultValueIfNotFound); - } - - const string Config::getString (const string & key, - const char *defaultValueIfNotFound) const - { - if (tempProperties.getString (key, defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return tempProperties.getString (key, defaultValueIfNotFound); - } - - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - return properties.second.getString (key, defaultValueIfNotFound); - } - - return properties.first.getString (key, defaultValueIfNotFound); - } - - SDL_Keycode Config::translateStringToSDLKey (const string & value) const - { - SDL_Keycode result = SDLK_UNKNOWN; - - if (IsNumeric (value.c_str ()) == true) - { - result = (SDL_Keycode) strToInt (value); - } - else if (value.substr (0, 2) == "vk") - { - if (value == "vkLeft") - { - result = SDLK_LEFT; - } - else if (value == "vkRight") - { - result = SDLK_RIGHT; - } - else if (value == "vkUp") - { - result = SDLK_UP; - } - else if (value == "vkDown") - { - result = SDLK_DOWN; - } - else if (value == "vkAdd") - { - result = SDLK_PLUS; - } - else if (value == "vkSubtract") - { - result = SDLK_MINUS; - } - else if (value == "vkEscape") - { - result = SDLK_ESCAPE; - } - else if (value == "vkF1") - { - result = SDLK_F1; - } - else if (value == "vkF2") - { - result = SDLK_F2; - } - else if (value == "vkF3") - { - result = SDLK_F3; - } - else if (value == "vkF4") - { - result = SDLK_F4; - } - else if (value == "vkF5") - { - result = SDLK_F5; - } - else if (value == "vkF6") - { - result = SDLK_F6; - } - else if (value == "vkF7") - { - result = SDLK_F7; - } - else if (value == "vkF8") - { - result = SDLK_F8; - } - else if (value == "vkF9") - { - result = SDLK_F9; - } - else if (value == "vkF10") - { - result = SDLK_F10; - } - else if (value == "vkF11") - { - result = SDLK_F11; - } - else if (value == "vkF12") - { - result = SDLK_F12; - } - else if (value == "vkPrint") - { - result = SDLK_PRINTSCREEN; - } - else if (value == "vkPause") - { - result = SDLK_PAUSE; - } - else - { - string sError = "Unsupported key translation [" + value + "]"; - throw megaglest_runtime_error (sError.c_str ()); - } - } - else if (value.length () >= 1) - { - if (value.length () == 3 && value[0] == '\'' && value[2] == '\'') - { - result = (SDL_Keycode) value[1]; - } - else - { - bool foundKey = false; - if (value.length () > 1) - { - SDL_Keycode lookup = SDL_GetKeyFromName (value.c_str ()); - if (lookup != SDLK_UNKNOWN) - { - result = lookup; - foundKey = true; - } - } - - if (foundKey == false) - { - result = (SDL_Keycode) value[0]; - } - } - } - else - { - string sError = "Unsupported key translation" + value; - throw megaglest_runtime_error (sError.c_str ()); - } - - // Because SDL is based on lower Ascii - //result = tolower(result); - return result; - } - - SDL_Keycode Config::getSDLKey (const char *key) const - { - if (fileLoaded.second == true && - properties.second.getString (key, - defaultNotFoundValue.c_str ()) != - defaultNotFoundValue) - { - - string value = properties.second.getString (key); - return translateStringToSDLKey (value); - } - string value = properties.first.getString (key); - return translateStringToSDLKey (value); - } - -//char Config::getCharKey(const char *key) const { -// if(fileLoaded.second == true && -// properties.second.getString(key, defaultNotFoundValue.c_str()) != defaultNotFoundValue) { -// -// string value = properties.second.getString(key); -// return translateStringToCharKey(value); -// } -// string value = properties.first.getString(key); -// return translateStringToCharKey(value); -//} - - void Config::setInt (const string & key, int value, bool tempBuffer) - { - if (tempBuffer == true) - { - tempProperties.setInt (key, value); - return; - } - if (fileLoaded.second == true) - { - properties.second.setInt (key, value); - return; - } - properties.first.setInt (key, value); - } - - void Config::setBool (const string & key, bool value, bool tempBuffer) - { - if (tempBuffer == true) - { - tempProperties.setBool (key, value); - return; - } - - if (fileLoaded.second == true) - { - properties.second.setBool (key, value); - return; - } - - properties.first.setBool (key, value); - } - - void Config::setFloat (const string & key, float value, bool tempBuffer) - { - if (tempBuffer == true) - { - tempProperties.setFloat (key, value); - return; - } - - if (fileLoaded.second == true) - { - properties.second.setFloat (key, value); - return; - } - - properties.first.setFloat (key, value); - } - - void Config::setString (const string & key, const string & value, - bool tempBuffer) - { - if (tempBuffer == true) - { - tempProperties.setString (key, value); - return; - } - - if (fileLoaded.second == true) - { - properties.second.setString (key, value); - return; - } - - properties.first.setString (key, value); - } - - vector < pair < string, - string > - >Config:: - getPropertiesFromContainer (const Properties & propertiesObj) const - { - vector < pair < string, string > >result; - - int count = propertiesObj.getPropertyCount (); - for (int i = 0; i < count; ++i) - { - pair < string, string > property; - property.first = propertiesObj.getKey (i); - property.second = propertiesObj.getString (i); - result.push_back (property); - } - - return result; - } - - vector < pair < string, string > >Config::getMergedProperties ()const - { - vector < pair < string, string > >result = getMasterProperties (); - vector < pair < string, string > >resultUser = getUserProperties (); - for (unsigned int i = 0; i < resultUser.size (); ++i) - { - const pair < string, string > &propertyUser = resultUser[i]; - bool overrideProperty = false; - for (unsigned int j = 0; j < result.size (); ++j) - { - pair < string, string > &property = result[j]; - // Take the user property and override the original value - if (property.first == propertyUser.first) - { - overrideProperty = true; - property.second = propertyUser.second; - break; - } - } - if (overrideProperty == false) - { - result.push_back (propertyUser); - } - } - - return result; - } - - vector < pair < string, string > >Config::getMasterProperties ()const - { - return getPropertiesFromContainer (properties.first); - } - - vector < pair < string, string > >Config::getUserProperties () const - { - return getPropertiesFromContainer (properties.second); - } - - void Config::setUserProperties (const vector < pair < string, - string > >&valueList) - { - Properties & propertiesObj = properties.second; - - for (unsigned int idx = 0; idx < valueList.size (); ++idx) - { - const pair < string, string > &nameValuePair = valueList[idx]; - propertiesObj.setString (nameValuePair.first, nameValuePair.second); - } - } - - string Config::getFileName (bool userFilename) const - { - string result = fileName.second; - if (userFilename == false) - { - result = fileName.first; - } - - return result; - } - - string Config::toString () - { - return properties.first.toString (); - } - - vector < string > Config::getPathListForType (PathType type, - string scenarioDir) - { - vector < string > pathList; - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - string userData = getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - //if(data_path == "") { - // userData = userData; - //} - //else { - // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("extractLastDirectoryFromPath(userData) [%s] from userData [%s]\n",extractLastDirectoryFromPath(userData).c_str(),userData.c_str()); - // userData = data_path + extractLastDirectoryFromPath(userData); - //} - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] userData path [%s]\n",__FILE__,__FUNCTION__,__LINE__,userData.c_str()); - - if (isdir (userData.c_str ()) == false) - { - createDirectoryPaths (userData); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userData.c_str ()); - } - - string userDataMaps = userData + GameConstants::folder_path_maps; - if (isdir (userDataMaps.c_str ()) == false) - { - createDirectoryPaths (userDataMaps); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userDataMaps.c_str ()); - } - string userDataScenarios = - userData + GameConstants::folder_path_scenarios; - if (isdir (userDataScenarios.c_str ()) == false) - { - createDirectoryPaths (userDataScenarios); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userDataScenarios.c_str ()); - } - string userDataTechs = userData + GameConstants::folder_path_techs; - if (isdir (userDataTechs.c_str ()) == false) - { - createDirectoryPaths (userDataTechs); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userDataTechs.c_str ()); - } - string userDataTilesets = - userData + GameConstants::folder_path_tilesets; - if (isdir (userDataTilesets.c_str ()) == false) - { - createDirectoryPaths (userDataTilesets); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userDataTilesets.c_str ()); - } - string userDataTutorials = - userData + GameConstants::folder_path_tutorials; - if (isdir (userDataTutorials.c_str ()) == false) - { - createDirectoryPaths (userDataTutorials); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, - __FUNCTION__, __LINE__, userDataTutorials.c_str ()); - } - } - if (scenarioDir != "") - { - if (EndsWith (scenarioDir, ".xml") == true) - { - scenarioDir = extractDirectoryPathFromFile (scenarioDir); - } - - //string scenarioLocation = data_path + scenarioDir; - string scenarioLocation = scenarioDir; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Scenario path [%s]\n", scenarioLocation.c_str ()); - pathList.push_back (scenarioLocation); - } - - switch (type) - { - case ptMaps: - pathList.push_back (data_path + GameConstants::folder_path_maps); - if (userData != "") - { - pathList.push_back (userData + - string (GameConstants::folder_path_maps)); - } - break; - case ptScenarios: - pathList.push_back (data_path + GameConstants::folder_path_scenarios); - if (userData != "") - { - pathList.push_back (userData + - string (GameConstants::folder_path_scenarios)); - } - break; - case ptTechs: - pathList.push_back (data_path + GameConstants::folder_path_techs); - if (userData != "") - { - pathList.push_back (userData + - string (GameConstants::folder_path_techs)); - } - break; - case ptTilesets: - pathList.push_back (data_path + GameConstants::folder_path_tilesets); - if (userData != "") - { - pathList.push_back (userData + - string (GameConstants::folder_path_tilesets)); - } - break; - case ptTutorials: - pathList.push_back (data_path + GameConstants::folder_path_tutorials); - if (userData != "") - { - pathList.push_back (userData + - string (GameConstants::folder_path_tutorials)); - } - break; - } - - return pathList; - } - - bool Config::replaceFileWithLocalFile (const vector < string > &dirList, - string fileNamePart, - string & resultToReplace) - { - bool found = false; - for (unsigned int i = 0; i < dirList.size (); ++i) - { - string path = dirList[i]; - endPathWithSlash (path); - string newFileName = path + fileNamePart; - if (fileExists (newFileName) == true) - { - resultToReplace = newFileName; - found = true; - break; - } - } - return found; - } - - - - string Config::findValidLocalFileFromPath (string fileName) - { - string result = fileName; - - if (fileName.find ("maps/") != fileName.npos) - { - size_t pos = fileName.find ("maps/"); - string fileNamePart = fileName.substr (pos + 5); - Config & config = Config::getInstance (); - vector < string > dirList = config.getPathListForType (ptMaps); - replaceFileWithLocalFile (dirList, fileNamePart, result); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Found file [%s] @ " MG_SIZE_T_SPECIFIER - " [%s]\nNew File [%s]\n", fileName.c_str (), pos, - fileNamePart.c_str (), result.c_str ()); - } - else if (fileName.find ("tilesets/") != fileName.npos) - { - size_t pos = fileName.find ("tilesets/"); - string fileNamePart = fileName.substr (pos + 9); - Config & config = Config::getInstance (); - vector < string > dirList = config.getPathListForType (ptTilesets); - replaceFileWithLocalFile (dirList, fileNamePart, result); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Found file [%s] @ " MG_SIZE_T_SPECIFIER - " [%s]\nNew File [%s]\n", fileName.c_str (), pos, - fileNamePart.c_str (), result.c_str ()); - } - else if (fileName.find ("techs/") != fileName.npos) - { - size_t pos = fileName.find ("techs/"); - string fileNamePart = fileName.substr (pos + 6); - Config & config = Config::getInstance (); - vector < string > dirList = config.getPathListForType (ptTechs); - replaceFileWithLocalFile (dirList, fileNamePart, result); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Found file [%s] @ " MG_SIZE_T_SPECIFIER - " [%s]\nNew File [%s]\n", fileName.c_str (), pos, - fileNamePart.c_str (), result.c_str ()); - } - else if (fileName.find ("scenarios/") != fileName.npos) - { - size_t pos = fileName.find ("scenarios/"); - string fileNamePart = fileName.substr (pos + 10); - Config & config = Config::getInstance (); - vector < string > dirList = config.getPathListForType (ptScenarios); - replaceFileWithLocalFile (dirList, fileNamePart, result); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Found file [%s] @ " MG_SIZE_T_SPECIFIER - " [%s]\nNew File [%s]\n", fileName.c_str (), pos, - fileNamePart.c_str (), result.c_str ()); - } - else if (fileName.find ("tutorials/") != fileName.npos) - { - size_t pos = fileName.find ("tutorials/"); - string fileNamePart = fileName.substr (pos + 10); - Config & config = Config::getInstance (); - vector < string > dirList = config.getPathListForType (ptTutorials); - replaceFileWithLocalFile (dirList, fileNamePart, result); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Found file [%s] @ " MG_SIZE_T_SPECIFIER - " [%s]\nNew File [%s]\n", fileName.c_str (), pos, - fileNamePart.c_str (), result.c_str ()); - } - - return result; - } - - -// static - string Config::getMapPath (const string & mapName, string scenarioDir, - bool errorOnNotFound) - { - - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptMaps, scenarioDir); - - for (int idx = 0; idx < (int) pathList.size (); idx++) - { - string map_path = pathList[idx]; - endPathWithSlash (map_path); - - const string mega = map_path + mapName + ".mgm"; - const string glest = map_path + mapName + ".gbm"; - if (fileExists (mega)) - { - return mega; - } - else if (fileExists (glest)) - { - return glest; - } - } - - if (errorOnNotFound == true) - { - //abort(); - throw megaglest_runtime_error ("Map not found [" + mapName + - "]\nScenario [" + scenarioDir + "]"); - } - - return ""; - } - - } + fileLoaded.second = true; + properties.second.load(fileName.second); + } + } catch (const exception & ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ERROR trying to auto-create cfgFile.second = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.second.c_str()); + } + } + + Config & Config::getInstance(std::pair < ConfigType, ConfigType > type, + std::pair < string, string > file, + std::pair < bool, bool > fileMustExist, + string custom_path) { + if (configList.find(type.first) == configList.end()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Config config(type, file, fileMustExist, custom_path); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + configList.insert(map < ConfigType, + Config >::value_type(type.first, config)); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + return configList.find(type.first)->second; + } + + void Config::CopyAll(Config * src, Config * dest) { + + dest->properties = src->properties; + dest->cfgType = src->cfgType; + dest->fileName = src->fileName; + dest->fileNameParameter = src->fileNameParameter; + dest->fileLoaded = src->fileLoaded; + } + + void Config::reload() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + std::pair < ConfigType, ConfigType > type = + std::make_pair(cfgType.first, cfgType.second); + Config newconfig(type, + std::make_pair(fileNameParameter.first, + fileNameParameter.second), + std::make_pair(true, false), custom_path_parameter); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Config & oldconfig = configList.find(type.first)->second; + CopyAll(&newconfig, &oldconfig); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void Config::save(const string & path) { + if (fileLoaded.second == true) { + if (path != "") { + fileName.second = path; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] save file [%s]\n", __FILE__, + __FUNCTION__, __LINE__, fileName.second.c_str()); + properties.second.save(fileName.second); + return; + } + + if (path != "") { + fileName.first = path; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] save file [%s]\n", __FILE__, + __FUNCTION__, __LINE__, fileName.first.c_str()); + properties.first.save(fileName.first); + } + + int Config::getInt(const char *key, const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getInt(key, defaultValueIfNotFound); + } + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getInt(key, defaultValueIfNotFound); + } + return properties.first.getInt(key, defaultValueIfNotFound); + } + + bool Config::getBool(const char *key, const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getBool(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getBool(key, defaultValueIfNotFound); + } + + return properties.first.getBool(key, defaultValueIfNotFound); + } + + float Config::getFloat(const char *key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getFloat(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getFloat(key, defaultValueIfNotFound); + } + + return properties.first.getFloat(key, defaultValueIfNotFound); + } + + const string Config::getString(const char *key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getString(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getString(key, defaultValueIfNotFound); + } + + return properties.first.getString(key, defaultValueIfNotFound); + } + + int Config::getInt(const string & key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getInt(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getInt(key, defaultValueIfNotFound); + } + + return properties.first.getInt(key, defaultValueIfNotFound); + } + + bool Config::getBool(const string & key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getBool(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getBool(key, defaultValueIfNotFound); + } + + return properties.first.getBool(key, defaultValueIfNotFound); + } + + float Config::getFloat(const string & key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getFloat(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getFloat(key, defaultValueIfNotFound); + } + + return properties.first.getFloat(key, defaultValueIfNotFound); + } + + const string Config::getString(const string & key, + const char *defaultValueIfNotFound) const { + if (tempProperties.getString(key, defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return tempProperties.getString(key, defaultValueIfNotFound); + } + + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + return properties.second.getString(key, defaultValueIfNotFound); + } + + return properties.first.getString(key, defaultValueIfNotFound); + } + + SDL_Keycode Config::translateStringToSDLKey(const string & value) const { + SDL_Keycode result = SDLK_UNKNOWN; + + if (IsNumeric(value.c_str()) == true) { + result = (SDL_Keycode) strToInt(value); + } else if (value.substr(0, 2) == "vk") { + if (value == "vkLeft") { + result = SDLK_LEFT; + } else if (value == "vkRight") { + result = SDLK_RIGHT; + } else if (value == "vkUp") { + result = SDLK_UP; + } else if (value == "vkDown") { + result = SDLK_DOWN; + } else if (value == "vkAdd") { + result = SDLK_PLUS; + } else if (value == "vkSubtract") { + result = SDLK_MINUS; + } else if (value == "vkEscape") { + result = SDLK_ESCAPE; + } else if (value == "vkF1") { + result = SDLK_F1; + } else if (value == "vkF2") { + result = SDLK_F2; + } else if (value == "vkF3") { + result = SDLK_F3; + } else if (value == "vkF4") { + result = SDLK_F4; + } else if (value == "vkF5") { + result = SDLK_F5; + } else if (value == "vkF6") { + result = SDLK_F6; + } else if (value == "vkF7") { + result = SDLK_F7; + } else if (value == "vkF8") { + result = SDLK_F8; + } else if (value == "vkF9") { + result = SDLK_F9; + } else if (value == "vkF10") { + result = SDLK_F10; + } else if (value == "vkF11") { + result = SDLK_F11; + } else if (value == "vkF12") { + result = SDLK_F12; + } else if (value == "vkPrint") { + result = SDLK_PRINTSCREEN; + } else if (value == "vkPause") { + result = SDLK_PAUSE; + } else { + string sError = "Unsupported key translation [" + value + "]"; + throw megaglest_runtime_error(sError.c_str()); + } + } else if (value.length() >= 1) { + if (value.length() == 3 && value[0] == '\'' && value[2] == '\'') { + result = (SDL_Keycode) value[1]; + } else { + bool foundKey = false; + if (value.length() > 1) { + SDL_Keycode lookup = SDL_GetKeyFromName(value.c_str()); + if (lookup != SDLK_UNKNOWN) { + result = lookup; + foundKey = true; + } + } + + if (foundKey == false) { + result = (SDL_Keycode) value[0]; + } + } + } else { + string sError = "Unsupported key translation" + value; + throw megaglest_runtime_error(sError.c_str()); + } + + // Because SDL is based on lower Ascii + //result = tolower(result); + return result; + } + + SDL_Keycode Config::getSDLKey(const char *key) const { + if (fileLoaded.second == true && + properties.second.getString(key, + defaultNotFoundValue.c_str()) != + defaultNotFoundValue) { + + string value = properties.second.getString(key); + return translateStringToSDLKey(value); + } + string value = properties.first.getString(key); + return translateStringToSDLKey(value); + } + + //char Config::getCharKey(const char *key) const { + // if(fileLoaded.second == true && + // properties.second.getString(key, defaultNotFoundValue.c_str()) != defaultNotFoundValue) { + // + // string value = properties.second.getString(key); + // return translateStringToCharKey(value); + // } + // string value = properties.first.getString(key); + // return translateStringToCharKey(value); + //} + + void Config::setInt(const string & key, int value, bool tempBuffer) { + if (tempBuffer == true) { + tempProperties.setInt(key, value); + return; + } + if (fileLoaded.second == true) { + properties.second.setInt(key, value); + return; + } + properties.first.setInt(key, value); + } + + void Config::setBool(const string & key, bool value, bool tempBuffer) { + if (tempBuffer == true) { + tempProperties.setBool(key, value); + return; + } + + if (fileLoaded.second == true) { + properties.second.setBool(key, value); + return; + } + + properties.first.setBool(key, value); + } + + void Config::setFloat(const string & key, float value, bool tempBuffer) { + if (tempBuffer == true) { + tempProperties.setFloat(key, value); + return; + } + + if (fileLoaded.second == true) { + properties.second.setFloat(key, value); + return; + } + + properties.first.setFloat(key, value); + } + + void Config::setString(const string & key, const string & value, + bool tempBuffer) { + if (tempBuffer == true) { + tempProperties.setString(key, value); + return; + } + + if (fileLoaded.second == true) { + properties.second.setString(key, value); + return; + } + + properties.first.setString(key, value); + } + + vector < pair < string, + string > + >Config:: + getPropertiesFromContainer(const Properties & propertiesObj) const { + vector < pair < string, string > >result; + + int count = propertiesObj.getPropertyCount(); + for (int i = 0; i < count; ++i) { + pair < string, string > property; + property.first = propertiesObj.getKey(i); + property.second = propertiesObj.getString(i); + result.push_back(property); + } + + return result; + } + + vector < pair < string, string > >Config::getMergedProperties()const { + vector < pair < string, string > >result = getMasterProperties(); + vector < pair < string, string > >resultUser = getUserProperties(); + for (unsigned int i = 0; i < resultUser.size(); ++i) { + const pair < string, string > &propertyUser = resultUser[i]; + bool overrideProperty = false; + for (unsigned int j = 0; j < result.size(); ++j) { + pair < string, string > &property = result[j]; + // Take the user property and override the original value + if (property.first == propertyUser.first) { + overrideProperty = true; + property.second = propertyUser.second; + break; + } + } + if (overrideProperty == false) { + result.push_back(propertyUser); + } + } + + return result; + } + + vector < pair < string, string > >Config::getMasterProperties()const { + return getPropertiesFromContainer(properties.first); + } + + vector < pair < string, string > >Config::getUserProperties() const { + return getPropertiesFromContainer(properties.second); + } + + void Config::setUserProperties(const vector < pair < string, + string > >&valueList) { + Properties & propertiesObj = properties.second; + + for (unsigned int idx = 0; idx < valueList.size(); ++idx) { + const pair < string, string > &nameValuePair = valueList[idx]; + propertiesObj.setString(nameValuePair.first, nameValuePair.second); + } + } + + string Config::getFileName(bool userFilename) const { + string result = fileName.second; + if (userFilename == false) { + result = fileName.first; + } + + return result; + } + + string Config::toString() { + return properties.first.toString(); + } + + vector < string > Config::getPathListForType(PathType type, + string scenarioDir) { + vector < string > pathList; + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + string userData = getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + //if(data_path == "") { + // userData = userData; + //} + //else { + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("extractLastDirectoryFromPath(userData) [%s] from userData [%s]\n",extractLastDirectoryFromPath(userData).c_str(),userData.c_str()); + // userData = data_path + extractLastDirectoryFromPath(userData); + //} + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] userData path [%s]\n",__FILE__,__FUNCTION__,__LINE__,userData.c_str()); + + if (isdir(userData.c_str()) == false) { + createDirectoryPaths(userData); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userData.c_str()); + } + + string userDataMaps = userData + GameConstants::folder_path_maps; + if (isdir(userDataMaps.c_str()) == false) { + createDirectoryPaths(userDataMaps); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userDataMaps.c_str()); + } + string userDataScenarios = + userData + GameConstants::folder_path_scenarios; + if (isdir(userDataScenarios.c_str()) == false) { + createDirectoryPaths(userDataScenarios); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userDataScenarios.c_str()); + } + string userDataTechs = userData + GameConstants::folder_path_techs; + if (isdir(userDataTechs.c_str()) == false) { + createDirectoryPaths(userDataTechs); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userDataTechs.c_str()); + } + string userDataTilesets = + userData + GameConstants::folder_path_tilesets; + if (isdir(userDataTilesets.c_str()) == false) { + createDirectoryPaths(userDataTilesets); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userDataTilesets.c_str()); + } + string userDataTutorials = + userData + GameConstants::folder_path_tutorials; + if (isdir(userDataTutorials.c_str()) == false) { + createDirectoryPaths(userDataTutorials); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] creating path [%s]\n", __FILE__, + __FUNCTION__, __LINE__, userDataTutorials.c_str()); + } + } + if (scenarioDir != "") { + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = extractDirectoryPathFromFile(scenarioDir); + } + + //string scenarioLocation = data_path + scenarioDir; + string scenarioLocation = scenarioDir; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Scenario path [%s]\n", scenarioLocation.c_str()); + pathList.push_back(scenarioLocation); + } + + switch (type) { + case ptMaps: + pathList.push_back(data_path + GameConstants::folder_path_maps); + if (userData != "") { + pathList.push_back(userData + + string(GameConstants::folder_path_maps)); + } + break; + case ptScenarios: + pathList.push_back(data_path + GameConstants::folder_path_scenarios); + if (userData != "") { + pathList.push_back(userData + + string(GameConstants::folder_path_scenarios)); + } + break; + case ptTechs: + pathList.push_back(data_path + GameConstants::folder_path_techs); + if (userData != "") { + pathList.push_back(userData + + string(GameConstants::folder_path_techs)); + } + break; + case ptTilesets: + pathList.push_back(data_path + GameConstants::folder_path_tilesets); + if (userData != "") { + pathList.push_back(userData + + string(GameConstants::folder_path_tilesets)); + } + break; + case ptTutorials: + pathList.push_back(data_path + GameConstants::folder_path_tutorials); + if (userData != "") { + pathList.push_back(userData + + string(GameConstants::folder_path_tutorials)); + } + break; + } + + return pathList; + } + + bool Config::replaceFileWithLocalFile(const vector < string > &dirList, + string fileNamePart, + string & resultToReplace) { + bool found = false; + for (unsigned int i = 0; i < dirList.size(); ++i) { + string path = dirList[i]; + endPathWithSlash(path); + string newFileName = path + fileNamePart; + if (fileExists(newFileName) == true) { + resultToReplace = newFileName; + found = true; + break; + } + } + return found; + } + + + + string Config::findValidLocalFileFromPath(string fileName) { + string result = fileName; + + if (fileName.find("maps/") != fileName.npos) { + size_t pos = fileName.find("maps/"); + string fileNamePart = fileName.substr(pos + 5); + Config & config = Config::getInstance(); + vector < string > dirList = config.getPathListForType(ptMaps); + replaceFileWithLocalFile(dirList, fileNamePart, result); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Found file [%s] @ " MG_SIZE_T_SPECIFIER + " [%s]\nNew File [%s]\n", fileName.c_str(), pos, + fileNamePart.c_str(), result.c_str()); + } else if (fileName.find("tilesets/") != fileName.npos) { + size_t pos = fileName.find("tilesets/"); + string fileNamePart = fileName.substr(pos + 9); + Config & config = Config::getInstance(); + vector < string > dirList = config.getPathListForType(ptTilesets); + replaceFileWithLocalFile(dirList, fileNamePart, result); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Found file [%s] @ " MG_SIZE_T_SPECIFIER + " [%s]\nNew File [%s]\n", fileName.c_str(), pos, + fileNamePart.c_str(), result.c_str()); + } else if (fileName.find("techs/") != fileName.npos) { + size_t pos = fileName.find("techs/"); + string fileNamePart = fileName.substr(pos + 6); + Config & config = Config::getInstance(); + vector < string > dirList = config.getPathListForType(ptTechs); + replaceFileWithLocalFile(dirList, fileNamePart, result); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Found file [%s] @ " MG_SIZE_T_SPECIFIER + " [%s]\nNew File [%s]\n", fileName.c_str(), pos, + fileNamePart.c_str(), result.c_str()); + } else if (fileName.find("scenarios/") != fileName.npos) { + size_t pos = fileName.find("scenarios/"); + string fileNamePart = fileName.substr(pos + 10); + Config & config = Config::getInstance(); + vector < string > dirList = config.getPathListForType(ptScenarios); + replaceFileWithLocalFile(dirList, fileNamePart, result); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Found file [%s] @ " MG_SIZE_T_SPECIFIER + " [%s]\nNew File [%s]\n", fileName.c_str(), pos, + fileNamePart.c_str(), result.c_str()); + } else if (fileName.find("tutorials/") != fileName.npos) { + size_t pos = fileName.find("tutorials/"); + string fileNamePart = fileName.substr(pos + 10); + Config & config = Config::getInstance(); + vector < string > dirList = config.getPathListForType(ptTutorials); + replaceFileWithLocalFile(dirList, fileNamePart, result); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Found file [%s] @ " MG_SIZE_T_SPECIFIER + " [%s]\nNew File [%s]\n", fileName.c_str(), pos, + fileNamePart.c_str(), result.c_str()); + } + + return result; + } + + + // static + string Config::getMapPath(const string & mapName, string scenarioDir, + bool errorOnNotFound) { + + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptMaps, scenarioDir); + + for (int idx = 0; idx < (int) pathList.size(); idx++) { + string map_path = pathList[idx]; + endPathWithSlash(map_path); + + const string mega = map_path + mapName + ".mgm"; + const string glest = map_path + mapName + ".gbm"; + if (fileExists(mega)) { + return mega; + } else if (fileExists(glest)) { + return glest; + } + } + + if (errorOnNotFound == true) { + //abort(); + throw megaglest_runtime_error("Map not found [" + mapName + + "]\nScenario [" + scenarioDir + "]"); + } + + return ""; + } + + } } // end namespace diff --git a/source/glest_game/global/config.h b/source/glest_game/global/config.h index 752d0ec44..df48c41ae 100644 --- a/source/glest_game/global/config.h +++ b/source/glest_game/global/config.h @@ -34,148 +34,143 @@ # include # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using Shared::Util::Properties; + using Shared::Util::Properties; -// ===================================================== -// class Config -// -// Game configuration -// ===================================================== + // ===================================================== + // class Config + // + // Game configuration + // ===================================================== - enum ConfigType - { - cfgMainGame, - cfgUserGame, - cfgTempGame, - cfgMainKeys, - cfgUserKeys, - cfgTempKeys - }; + enum ConfigType { + cfgMainGame, + cfgUserGame, + cfgTempGame, + cfgMainKeys, + cfgUserKeys, + cfgTempKeys + }; - class Config - { - private: + class Config { + private: - std::pair < Properties, Properties > properties; - Properties tempProperties; - std::pair < ConfigType, ConfigType > cfgType; - std::pair < string, string > fileNameParameter; - std::pair < string, string > fileName; - std::pair < bool, bool > fileLoaded; - string custom_path_parameter; + std::pair < Properties, Properties > properties; + Properties tempProperties; + std::pair < ConfigType, ConfigType > cfgType; + std::pair < string, string > fileNameParameter; + std::pair < string, string > fileName; + std::pair < bool, bool > fileLoaded; + string custom_path_parameter; - static map < ConfigType, Config > configList; + static map < ConfigType, Config > configList; - static const char *glest_ini_filename; - static const char *glestuser_ini_filename; + static const char *glest_ini_filename; + static const char *glestuser_ini_filename; - static map < string, string > customRuntimeProperties; + static map < string, string > customRuntimeProperties; - public: + public: - static const char *glestkeys_ini_filename; - static const char *glestuserkeys_ini_filename; + static const char *glestkeys_ini_filename; + static const char *glestuserkeys_ini_filename; - static const char *ACTIVE_MOD_PROPERTY_NAME; + static const char *ACTIVE_MOD_PROPERTY_NAME; - static const char *colorPicking; - static const char *selectBufPicking; - static const char *frustumPicking; + static const char *colorPicking; + static const char *selectBufPicking; + static const char *frustumPicking; - protected: + protected: - Config (); - Config (std::pair < ConfigType, ConfigType > type, std::pair < string, - string > file, std::pair < bool, bool > fileMustExist, - string custom_path = ""); - bool tryCustomPath (std::pair < ConfigType, ConfigType > &type, - std::pair < string, string > &file, - string custom_path); - static void CopyAll (Config * src, Config * dest); - vector < pair < string, - string > - >getPropertiesFromContainer (const Properties & propertiesObj) const; - static bool replaceFileWithLocalFile (const vector < string > &dirList, - string fileNamePart, - string & resultToReplace); + Config(); + Config(std::pair < ConfigType, ConfigType > type, std::pair < string, + string > file, std::pair < bool, bool > fileMustExist, + string custom_path = ""); + bool tryCustomPath(std::pair < ConfigType, ConfigType > &type, + std::pair < string, string > &file, + string custom_path); + static void CopyAll(Config * src, Config * dest); + vector < pair < string, + string > + >getPropertiesFromContainer(const Properties & propertiesObj) const; + static bool replaceFileWithLocalFile(const vector < string > &dirList, + string fileNamePart, + string & resultToReplace); - public: + public: - static Config & getInstance (std::pair < ConfigType, ConfigType > type = - std::make_pair (cfgMainGame, cfgUserGame), - std::pair < string, string > file = - std::make_pair (glest_ini_filename, - glestuser_ini_filename), - std::pair < bool, bool > fileMustExist = - std::make_pair (true, false), - string custom_path = ""); - void save (const string & path = ""); - void reload (); + static Config & getInstance(std::pair < ConfigType, ConfigType > type = + std::make_pair(cfgMainGame, cfgUserGame), + std::pair < string, string > file = + std::make_pair(glest_ini_filename, + glestuser_ini_filename), + std::pair < bool, bool > fileMustExist = + std::make_pair(true, false), + string custom_path = ""); + void save(const string & path = ""); + void reload(); - int getInt (const string & key, const char *defaultValueIfNotFound = - NULL) const; - bool getBool (const string & key, const char *defaultValueIfNotFound = - NULL) const; - float getFloat (const string & key, const char *defaultValueIfNotFound = - NULL) const; - const string getString (const string & key, - const char *defaultValueIfNotFound = - NULL) const; + int getInt(const string & key, const char *defaultValueIfNotFound = + NULL) const; + bool getBool(const string & key, const char *defaultValueIfNotFound = + NULL) const; + float getFloat(const string & key, const char *defaultValueIfNotFound = + NULL) const; + const string getString(const string & key, + const char *defaultValueIfNotFound = + NULL) const; - int getInt (const char *key, const char *defaultValueIfNotFound = - NULL) const; - bool getBool (const char *key, const char *defaultValueIfNotFound = - NULL) const; - float getFloat (const char *key, const char *defaultValueIfNotFound = - NULL) const; - const string getString (const char *key, - const char *defaultValueIfNotFound = - NULL) const; - //char getCharKey(const char *key) const; - SDL_Keycode getSDLKey (const char *key) const; + int getInt(const char *key, const char *defaultValueIfNotFound = + NULL) const; + bool getBool(const char *key, const char *defaultValueIfNotFound = + NULL) const; + float getFloat(const char *key, const char *defaultValueIfNotFound = + NULL) const; + const string getString(const char *key, + const char *defaultValueIfNotFound = + NULL) const; + //char getCharKey(const char *key) const; + SDL_Keycode getSDLKey(const char *key) const; - void setInt (const string & key, int value, bool tempBuffer = false); - void setBool (const string & key, bool value, bool tempBuffer = false); - void setFloat (const string & key, float value, bool tempBuffer = - false); - void setString (const string & key, const string & value, - bool tempBuffer = false); + void setInt(const string & key, int value, bool tempBuffer = false); + void setBool(const string & key, bool value, bool tempBuffer = false); + void setFloat(const string & key, float value, bool tempBuffer = + false); + void setString(const string & key, const string & value, + bool tempBuffer = false); - vector < string > getPathListForType (PathType type, - string scenarioDir = ""); + vector < string > getPathListForType(PathType type, + string scenarioDir = ""); - vector < pair < string, string > >getMergedProperties () const; - vector < pair < string, string > >getMasterProperties () const; - vector < pair < string, string > >getUserProperties () const; - void setUserProperties (const vector < pair < string, - string > >&valueList); + vector < pair < string, string > >getMergedProperties() const; + vector < pair < string, string > >getMasterProperties() const; + vector < pair < string, string > >getUserProperties() const; + void setUserProperties(const vector < pair < string, + string > >&valueList); - string getFileName (bool userFilename) const; + string getFileName(bool userFilename) const; - SDL_Keycode translateStringToSDLKey (const string & value) const; + SDL_Keycode translateStringToSDLKey(const string & value) const; - string toString (); + string toString(); - static string getCustomRuntimeProperty (string key) - { - return customRuntimeProperties[key]; - } - static void setCustomRuntimeProperty (string key, string value) - { - customRuntimeProperties[key] = value; - } + static string getCustomRuntimeProperty(string key) { + return customRuntimeProperties[key]; + } + static void setCustomRuntimeProperty(string key, string value) { + customRuntimeProperties[key] = value; + } - static string findValidLocalFileFromPath (string fileName); + static string findValidLocalFileFromPath(string fileName); - static string getMapPath (const string & mapName, string scenarioDir = - "", bool errorOnNotFound = true); - }; + static string getMapPath(const string & mapName, string scenarioDir = + "", bool errorOnNotFound = true); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/global/core_data.cpp b/source/glest_game/global/core_data.cpp index 70d2a4555..cc304cef2 100644 --- a/source/glest_game/global/core_data.cpp +++ b/source/glest_game/global/core_data.cpp @@ -38,1959 +38,1772 @@ using namespace Shared::Sound; using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest -{ - namespace Game - { - -// ===================================================== -// class CoreData -// ===================================================== - - static string tempDataLocation = getUserHome (); -// ===================== PUBLIC ======================== - - static const string CORE_PATH = "data/core/"; - static const string CORE_MISC_TEXTURES_PATH = - CORE_PATH + "misc_textures/"; - - static const string CORE_MENU_PATH = CORE_PATH + "menu/"; - static const string CORE_MENU_TEXTURES_PATH = - CORE_MENU_PATH + "textures/"; - static const string CORE_MENU_SOUND_PATH = CORE_MENU_PATH + "sound/"; - static const string CORE_MENU_MUSIC_PATH = CORE_MENU_PATH + "music/"; - static const string CORE_MENU_VIDEOS_PATH = CORE_MENU_PATH + "videos/"; - - static const string CORE_WATER_SOUNDS_PATH = CORE_PATH + "/water_sounds/"; - - // Sound effects - // These variables are specified in the ini file - const string PlaySoundClip::sfxAttention = "PlaySoundAttention"; - const string PlaySoundClip::sfxHighlight = "PlaySoundHighlight"; - const string PlaySoundClip::sfxNewServer = "PlaySoundNewServer"; - const string PlaySoundClip::sfxMarker = "PlaySoundMarker"; - const string PlaySoundClip::sfxMenuClickA = "PlaySoundMenuClickA"; - const string PlaySoundClip::sfxMenuClickB = "PlaySoundMenuClickB"; - const string PlaySoundClip::sfxMenuClickC = "PlaySoundMenuClickC"; - - CoreData & CoreData::getInstance () - { - static CoreData coreData; - return coreData; - } - - CoreData::CoreData () - { - logoTexture = NULL; - logoTextureList.clear (); - backgroundTexture = NULL; - fireTexture = NULL; - teamColorTexture = NULL; - snowTexture = NULL; - waterSplashTexture = NULL; - customTexture = NULL; - buttonSmallTexture = NULL; - buttonBigTexture = NULL; - horizontalLineTexture = NULL; - verticalLineTexture = NULL; - checkBoxTexture = NULL; - checkedCheckBoxTexture = NULL; - gameWinnerTexture = NULL; - notOnServerTexture = NULL; - onServerDifferentTexture = NULL; - onServerTexture = NULL; - onServerInstalledTexture = NULL; - statusReadyTexture = NULL; - statusNotReadyTexture = NULL; - statusBRBTexture = NULL; - - healthbarTexture = NULL; - healthbarBackgroundTexture = NULL; - - miscTextureList.clear (); - - displayFont = NULL; - menuFontNormal = NULL; - displayFontSmall = NULL; - menuFontBig = NULL; - menuFontVeryBig = NULL; - consoleFont = NULL; - - displayFont3D = NULL; - menuFontNormal3D = NULL; - displayFontSmall3D = NULL; - menuFontBig3D = NULL; - menuFontVeryBig3D = NULL; - consoleFont3D = NULL; - - introVideoFilename = ""; - mainMenuVideoFilename = ""; - - battleEndWinVideoFilename = ""; - battleEndWinVideoFilenameFallback = ""; - battleEndWinMusicFilename = ""; - battleEndLoseVideoFilename = ""; - battleEndLoseVideoFilenameFallback = ""; - battleEndLoseMusicFilename = ""; - } - - CoreData::~CoreData () - { - cleanup (); - } - - void CoreData::cleanup () - { - deleteValues (waterSounds.getSoundsPtr ()->begin (), - waterSounds.getSoundsPtr ()->end ()); - waterSounds.getSoundsPtr ()->clear (); - } - - Texture2D *CoreData::getTextureBySystemId (TextureSystemType type) - { - Texture2D *result = NULL; - switch (type) - { - case tsyst_logoTexture: - result = getLogoTexture (); - break; - //std::vector logoTextureList; - case tsyst_backgroundTexture: - result = getBackgroundTexture (); - break; - case tsyst_fireTexture: - result = getFireTexture (); - break; - case tsyst_teamColorTexture: - result = getTeamColorTexture (); - break; - case tsyst_snowTexture: - result = getSnowTexture (); - break; - case tsyst_waterSplashTexture: - result = getWaterSplashTexture (); - break; - case tsyst_customTexture: - result = getCustomTexture (); - break; - case tsyst_buttonSmallTexture: - result = buttonSmallTexture; - break; - case tsyst_buttonBigTexture: - result = buttonBigTexture; - break; - case tsyst_horizontalLineTexture: - result = horizontalLineTexture; - break; - case tsyst_verticalLineTexture: - result = verticalLineTexture; - break; - case tsyst_checkBoxTexture: - result = checkBoxTexture; - break; - case tsyst_checkedCheckBoxTexture: - result = checkedCheckBoxTexture; - break; - case tsyst_gameWinnerTexture: - result = gameWinnerTexture; - break; - case tsyst_notOnServerTexture: - result = notOnServerTexture; - break; - case tsyst_onServerDifferentTexture: - result = onServerDifferentTexture; - break; - case tsyst_onServerTexture: - result = onServerTexture; - break; - case tsyst_onServerInstalledTexture: - result = onServerInstalledTexture; - break; - case tsyst_statusReadyTexture: - result = statusReadyTexture; - break; - case tsyst_statusNotReadyTexture: - result = statusNotReadyTexture; - break; - case tsyst_statusBRBTexture: - result = statusBRBTexture; - break; - case tsyst_healthbarTexture: - result = healthbarTexture; - break; - case tsyst_healthbarBackgroundTexture: - result = healthbarBackgroundTexture; - break; - - //std::vector miscTextureList; - } - return result; - } - - void CoreData::cleanupTexture (Texture2D ** texture) - { - Renderer & renderer = Renderer::getInstance (); - renderer.endTexture (rsGlobal, *texture); - *texture = NULL; - } - - void CoreData::loadTextureIfRequired (Texture2D ** tex, string data_path, - string uniqueFilePath, - int texSystemId, bool setMipMap, - bool setAlpha, - bool loadUniqueFilePath, - bool compressionDisabled) - { - if (*tex == NULL) - { - bool attemptToLoadTexture = (texSystemId == tsyst_NONE); - if (attemptToLoadTexture == false && - itemLoadAttempted.find (texSystemId) == itemLoadAttempted.end ()) - { - - attemptToLoadTexture = true; - itemLoadAttempted[texSystemId] = true; - } - - if (attemptToLoadTexture == true) - { - Renderer & renderer = Renderer::getInstance (); - *tex = renderer.newTexture2D (rsGlobal); - if (*tex) - { - - (*tex)->setForceCompressionDisabled (compressionDisabled); - (*tex)->setMipmap (setMipMap); - if (setAlpha == true) - { - - (*tex)->setFormat (Texture::fAlpha); - (*tex)->getPixmap ()->init (1); - } - - try - { - string fileToLoad = uniqueFilePath; - if (loadUniqueFilePath == false) - { - - fileToLoad = - getGameCustomCoreDataPath (data_path, uniqueFilePath); - } - (*tex)->getPixmap ()->load (fileToLoad); - (*tex)->setTextureSystemId (texSystemId); - - renderer.initTexture (rsGlobal, *tex); - } - catch (const megaglest_runtime_error & ex) - { - message (ex.what (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - cleanupTexture (tex); - } - } - } - } - } - - string CoreData::getDataPath () - { - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - if (data_path != "") - { - endPathWithSlash (data_path); - } - return data_path; - } - - Texture2D *CoreData::getBackgroundTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&backgroundTexture, getDataPath (), - CORE_MENU_TEXTURES_PATH + "back.tga", - tsyst_backgroundTexture, false, false, false); - - return backgroundTexture; - } - Texture2D *CoreData::getFireTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&fireTexture, data_path, - CORE_MISC_TEXTURES_PATH + "fire_particle.tga", - tsyst_fireTexture, true, true, false); - - return fireTexture; - } - Texture2D *CoreData::getTeamColorTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&teamColorTexture, data_path, - CORE_MISC_TEXTURES_PATH + - "team_color_texture.tga", tsyst_teamColorTexture, - true, true, false); - - return teamColorTexture; - } - Texture2D *CoreData::getSnowTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&snowTexture, data_path, - CORE_MISC_TEXTURES_PATH + "snow_particle.tga", - tsyst_snowTexture, false, true, false); - - return snowTexture; - } - Texture2D *CoreData::getLogoTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&logoTexture, data_path, - CORE_MENU_TEXTURES_PATH + "logo.tga", - tsyst_logoTexture, false, false, false); - - return logoTexture; - } - Texture2D *CoreData::getWaterSplashTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&waterSplashTexture, data_path, - CORE_MISC_TEXTURES_PATH + "water_splash.tga", - tsyst_waterSplashTexture, true, true, false); - - return waterSplashTexture; - } - Texture2D *CoreData::getCustomTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&customTexture, data_path, - CORE_MENU_TEXTURES_PATH + "custom_texture.tga", - tsyst_customTexture, true, false, false); - - return customTexture; - } - Texture2D *CoreData::getButtonSmallTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&buttonSmallTexture, data_path, - CORE_MENU_TEXTURES_PATH + "button_small.tga", - tsyst_buttonSmallTexture, true, false, false, - true); - - return buttonSmallTexture; - } - Texture2D *CoreData::getButtonBigTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&buttonBigTexture, data_path, - CORE_MENU_TEXTURES_PATH + "button_big.tga", - tsyst_buttonBigTexture, true, false, false, - true); - - return buttonBigTexture; - } - Texture2D *CoreData::getHorizontalLineTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&horizontalLineTexture, data_path, - CORE_MENU_TEXTURES_PATH + "line_horizontal.tga", - tsyst_horizontalLineTexture, true, false, false, - true); - - return horizontalLineTexture; - } - Texture2D *CoreData::getVerticalLineTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&verticalLineTexture, data_path, - CORE_MENU_TEXTURES_PATH + "line_vertical.tga", - tsyst_verticalLineTexture, true, false, false, - true); - - return verticalLineTexture; - } - Texture2D *CoreData::getCheckBoxTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&checkBoxTexture, data_path, - CORE_MENU_TEXTURES_PATH + "checkbox.tga", - tsyst_checkBoxTexture, true, false, false, true); - - return checkBoxTexture; - } - Texture2D *CoreData::getCheckedCheckBoxTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&checkedCheckBoxTexture, data_path, - CORE_MENU_TEXTURES_PATH + "checkbox_checked.tga", - tsyst_checkedCheckBoxTexture, true, false, false, - true); - - return checkedCheckBoxTexture; - } - Texture2D *CoreData::getNotOnServerTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (¬OnServerTexture, data_path, - CORE_MENU_TEXTURES_PATH + "not_on_server.tga", - tsyst_notOnServerTexture, true, false, false); - - return notOnServerTexture; - } - Texture2D *CoreData::getOnServerDifferentTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&onServerDifferentTexture, data_path, - CORE_MENU_TEXTURES_PATH + - "on_server_different.tga", - tsyst_onServerDifferentTexture, true, false, - false); - - return onServerDifferentTexture; - } - Texture2D *CoreData::getOnServerTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&onServerTexture, data_path, - CORE_MENU_TEXTURES_PATH + "on_server.tga", - tsyst_onServerTexture, true, false, false); - - return onServerTexture; - } - Texture2D *CoreData::getOnServerInstalledTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&onServerInstalledTexture, data_path, - CORE_MENU_TEXTURES_PATH + - "on_server_installed.tga", - tsyst_onServerInstalledTexture, true, false, - false); - - return onServerInstalledTexture; - } - Texture2D *CoreData::getStatusReadyTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&statusReadyTexture, data_path, - CORE_MENU_TEXTURES_PATH + "status_ready.png", - tsyst_statusReadyTexture, true, false, false); - - return statusReadyTexture; - } - Texture2D *CoreData::getStatusNotReadyTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&statusNotReadyTexture, data_path, - CORE_MENU_TEXTURES_PATH + "status_notready.png", - tsyst_statusNotReadyTexture, true, false, false); - - return statusNotReadyTexture; - } - Texture2D *CoreData::getStatusBRBTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&statusBRBTexture, data_path, - CORE_MENU_TEXTURES_PATH + "status_brb.png", - tsyst_statusBRBTexture, true, false, false); - - return statusBRBTexture; - } - Texture2D *CoreData::getGameWinnerTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&gameWinnerTexture, data_path, - CORE_MISC_TEXTURES_PATH + "game_winner.png", - tsyst_gameWinnerTexture, true, false, false, - true); - - return gameWinnerTexture; - } - - Texture2D *CoreData::getHealthbarTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&healthbarTexture, data_path, - CORE_MISC_TEXTURES_PATH + "healthbar.png", - tsyst_healthbarTexture, true, false, false, - true); - - return healthbarTexture; - } - - Texture2D *CoreData::getHealthbarBackgroundTexture () - { - string data_path = getDataPath (); - loadTextureIfRequired (&healthbarBackgroundTexture, data_path, - CORE_MISC_TEXTURES_PATH + - "healthbarBackground.png", - tsyst_healthbarBackgroundTexture, true, false, - false, true); - - return healthbarBackgroundTexture; - } - - void CoreData::loadLogoTextureExtraIfRequired () - { - int loadAttemptLookupKey = tsyst_COUNT + 1; - if (itemLoadAttempted.find (loadAttemptLookupKey) == - itemLoadAttempted.end ()) - { - - itemLoadAttempted[loadAttemptLookupKey] = true; - - string data_path = getDataPath (); - logoTextureList.clear (); - string logosPath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_TEXTURES_PATH + "logo*.*"; - vector < string > logoFilenames; - findAll (logosPath, logoFilenames, false, false); - for (int index = 0; index < (int) logoFilenames.size (); ++index) - { - - string logo = logoFilenames[index]; - if (strcmp ("logo.tga", logo.c_str ()) != 0) - { - - Texture2D *logoTextureExtra = NULL; - loadTextureIfRequired (&logoTextureExtra, data_path, - getGameCustomCoreDataPath (data_path, "") + - CORE_MENU_TEXTURES_PATH + logo, tsyst_NONE, - true, false, true); - logoTextureList.push_back (logoTextureExtra); - } - } - if (logoTextureList.empty () == true) - { - - logosPath = data_path + CORE_MENU_TEXTURES_PATH + "logo*.*"; - vector < string > logoFilenames; - findAll (logosPath, logoFilenames, false, false); - for (int index = 0; index < (int) logoFilenames.size (); ++index) - { - - string logo = logoFilenames[index]; - if (strcmp ("logo.tga", logo.c_str ()) != 0) - { - - Texture2D *logoTextureExtra = NULL; - loadTextureIfRequired (&logoTextureExtra, data_path, - data_path + CORE_MENU_TEXTURES_PATH + - logo, tsyst_NONE, true, false, true); - logoTextureList.push_back (logoTextureExtra); - } - } - } - } - } - size_t CoreData::getLogoTextureExtraCount () - { - loadLogoTextureExtraIfRequired (); - return logoTextureList.size (); - } - Texture2D *CoreData::getLogoTextureExtra (int idx) - { - loadLogoTextureExtraIfRequired (); - return logoTextureList[idx]; - } - - void CoreData::loadMiscTextureListIfRequired () - { - int loadAttemptLookupKey = tsyst_COUNT + 2; - if (itemLoadAttempted.find (loadAttemptLookupKey) == - itemLoadAttempted.end ()) - { - - itemLoadAttempted[loadAttemptLookupKey] = true; - - string data_path = getDataPath (); - - miscTextureList.clear (); - string introPath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_TEXTURES_PATH + "intro*.*"; - vector < string > introFilenames; - findAll (introPath, introFilenames, false, false); - for (int i = 0; i < (int) introFilenames.size (); ++i) - { - string logo = introFilenames[i]; - - Texture2D *logoTextureExtra = NULL; - loadTextureIfRequired (&logoTextureExtra, data_path, - getGameCustomCoreDataPath (data_path, - "") + - CORE_MENU_TEXTURES_PATH + logo, tsyst_NONE, - true, false, true); - miscTextureList.push_back (logoTextureExtra); - } - if (miscTextureList.empty () == true) - { - introPath = data_path + CORE_MENU_TEXTURES_PATH + "intro*.*"; - vector < string > introFilenames; - findAll (introPath, introFilenames, false, false); - for (int i = 0; i < (int) introFilenames.size (); ++i) - { - string logo = introFilenames[i]; - - Texture2D *logoTextureExtra = NULL; - loadTextureIfRequired (&logoTextureExtra, data_path, - data_path + CORE_MENU_TEXTURES_PATH + logo, - tsyst_NONE, true, false, true); - miscTextureList.push_back (logoTextureExtra); - } - } - - } - } - - std::vector < Texture2D * >&CoreData::getMiscTextureList () - { - loadMiscTextureListIfRequired (); - return miscTextureList; - } - - void CoreData::loadTextures (string data_path) - { - // Required to be loaded at program startup as they may be accessed in - // threads or some other dangerous way so lazy loading is not an option - getCustomTexture (); - } - - StaticSound *CoreData::getClickSoundA () - { - static PlaySoundClip snd; - return snd.getSound (snd.sfxMenuClickA); - } - - StaticSound *CoreData::getClickSoundB () - { - static PlaySoundClip snd; - return snd.getSound (snd.sfxMenuClickB); - - } - StaticSound *CoreData::getClickSoundC () - { - static PlaySoundClip snd; - return snd.getSound (snd.sfxMenuClickC); - } - - void CoreData::loadWaterSoundsIfRequired () - { - int loadAttemptLookupKey = tsyst_COUNT + 9; - if (itemLoadAttempted.find (loadAttemptLookupKey) == - itemLoadAttempted.end ()) - { - - itemLoadAttempted[loadAttemptLookupKey] = true; - - string data_path = getDataPath (); - cleanup (); - waterSounds.resize (6); - - for (int index = 0; index < 6; ++index) - { - waterSounds[index] = new StaticSound (); - if (waterSounds[index] != NULL) - { - try - { - waterSounds[index]->load (getGameCustomCoreDataPath (data_path, - CORE_WATER_SOUNDS_PATH - + "water" + - intToStr - (index) + - ".wav")); - } - catch (const megaglest_runtime_error & ex) - { - message (ex.what (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } - } - } - } - } - - StaticSound *CoreData::getWaterSound () - { - loadWaterSoundsIfRequired (); - return waterSounds.getRandSound (); - } - - void CoreData::loadSounds (string data_path) - { - // sounds -// try { -// clickSoundA.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "click_a.wav")); -// clickSoundB.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "click_b.wav")); -// clickSoundC.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "click_c.wav")); -// attentionSound.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "attention.wav")); -// highlightSound.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "highlight.wav")); -// markerSound.load( -// getGameCustomCoreDataPath(data_path, -// CORE_MENU_SOUND_PATH + "sonar.wav")); -// } -// catch (const megaglest_runtime_error& ex) { -// message(ex.what(), GlobalStaticFlags::getIsNonGraphicalModeEnabled(), -// tempDataLocation); -// } - -// cleanup(); -// waterSounds.resize(6); -// -// for (int i = 0; i < 6; ++i) { -// waterSounds[i] = new StaticSound(); -// if (waterSounds[i]) { -// try { -// waterSounds[i]->load( -// getGameCustomCoreDataPath(data_path, -// CORE_WATER_SOUNDS_PATH + "water" + intToStr(i) -// + ".wav")); -// } catch (const megaglest_runtime_error& ex) { -// message(ex.what(), -// GlobalStaticFlags::getIsNonGraphicalModeEnabled(), -// tempDataLocation); -// } -// } -// } - } - - - void CoreData::loadMusicIfRequired () - { - int loadAttemptLookupKey = tsyst_COUNT + 10; - if (itemLoadAttempted.find (loadAttemptLookupKey) == - itemLoadAttempted.end ()) - { - - itemLoadAttempted[loadAttemptLookupKey] = true; - - string data_path = getDataPath (); - - XmlTree xmlTree; - xmlTree. - load (getGameCustomCoreDataPath - (data_path, CORE_MENU_PATH + "menu.xml"), - Properties::getTagReplacementValues ()); - const XmlNode *menuNode = xmlTree.getRootNode (); - string menuMusicPath = "/menu/music/"; - string menuIntroMusicFile = "intro_music.ogg"; - string menuMusicFile = "menu_music.ogg"; - if (menuNode->hasChild ("intro") == true) - { - const XmlNode *introNode = menuNode->getChild ("intro"); - // intro info - const XmlNode *menuPathNode = - introNode->getChild ("menu-music-path"); - menuMusicPath = - menuPathNode->getAttribute ("value")->getRestrictedValue (); - const XmlNode *menuIntroMusicNode = - introNode->getChild ("menu-intro-music"); - menuIntroMusicFile = - menuIntroMusicNode->getAttribute ("value")->getRestrictedValue (); - const XmlNode *menuMusicNode = introNode->getChild ("menu-music"); - menuMusicFile = - menuMusicNode->getAttribute ("value")->getRestrictedValue (); - } - try - { - introMusic.open (getGameCustomCoreDataPath (data_path, - CORE_PATH + - menuMusicPath + - menuIntroMusicFile)); - introMusic.setNext (&menuMusic); - menuMusic.open (getGameCustomCoreDataPath (data_path, - CORE_PATH + - menuMusicPath + - menuMusicFile)); - menuMusic.setNext (&menuMusic); - } - catch (const megaglest_runtime_error & ex) - { - message (ex.what (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } - } - } - - StrSound *CoreData::getIntroMusic () - { - loadMusicIfRequired (); - return &introMusic; - } - - StrSound *CoreData::getMenuMusic () - { - loadMusicIfRequired (); - return &menuMusic; - } - - void CoreData::loadMusic (string data_path) - { - } - - void CoreData::loadIntroMedia (string data_path) - { - Config & config = Config::getInstance (); - - introVideoFilename = config.getString ("IntroVideoURL", ""); - introVideoFilenameFallback = - config.getString ("IntroVideoURLFallback", ""); - if (introVideoFilename == "") - { - string introVideoPath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + "intro.*"; - vector < string > introVideos; - findAll (introVideoPath, introVideos, false, false); - for (int i = 0; i < (int) introVideos.size (); ++i) - { - string video = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + introVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if intro video [%s] exists\n", video.c_str ()); - - if (fileExists (video)) - { - introVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND intro video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - if (introVideoFilename == "") - { - introVideoPath = data_path + CORE_MENU_VIDEOS_PATH + "intro.*"; - introVideos.clear (); - findAll (introVideoPath, introVideos, false, false); - for (int i = 0; i < (int) introVideos.size (); ++i) - { - string video = data_path + CORE_MENU_VIDEOS_PATH + introVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if intro video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - introVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND intro video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - } - } - } - - void CoreData::loadMainMenuMedia (string data_path) - { - Config & config = Config::getInstance (); - - mainMenuVideoFilename = config.getString ("MainMenuVideoURL", ""); - mainMenuVideoFilenameFallback = - config.getString ("MainMenuVideoURLFallback", ""); - if (mainMenuVideoFilename == "") - { - string mainVideoPath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + "main.*"; - vector < string > mainVideos; - findAll (mainVideoPath, mainVideos, false, false); - for (int i = 0; i < (int) mainVideos.size (); ++i) - { - string video = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + mainVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if mainmenu video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - mainMenuVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND mainmenu video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - if (mainMenuVideoFilename == "") - { - mainVideoPath = data_path + CORE_MENU_VIDEOS_PATH + "main.*"; - mainVideos.clear (); - findAll (mainVideoPath, mainVideos, false, false); - for (int i = 0; i < (int) mainVideos.size (); ++i) - { - string video = data_path + CORE_MENU_VIDEOS_PATH + mainVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if mainmenu video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - mainMenuVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND mainmenu video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - } - } - } - - void CoreData::loadBattleEndMedia (string data_path) - { - Config & config = Config::getInstance (); - - battleEndWinVideoFilename = - config.getString ("BattleEndWinVideoURL", ""); - battleEndWinVideoFilenameFallback = - config.getString ("BattleEndWinVideoURLFallback", ""); - if (battleEndWinVideoFilename == "") - { - string battleEndWinVideoPath = - getGameCustomCoreDataPath (data_path, - "") + CORE_MENU_VIDEOS_PATH + - "battle_end_win.*"; - vector < string > battleEndWinVideos; - findAll (battleEndWinVideoPath, battleEndWinVideos, false, false); - for (int i = 0; i < (int) battleEndWinVideos.size (); ++i) - { - string video = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + battleEndWinVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end win video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - battleEndWinVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND battle end win video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - if (battleEndWinVideoFilename == "") - { - battleEndWinVideoPath = data_path + CORE_MENU_VIDEOS_PATH - + "battle_end_win.*"; - battleEndWinVideos.clear (); - findAll (battleEndWinVideoPath, battleEndWinVideos, false, false); - for (int i = 0; i < (int) battleEndWinVideos.size (); ++i) - { - string video = data_path + CORE_MENU_VIDEOS_PATH - + battleEndWinVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end win video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - battleEndWinVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("FOUND battle end video win [%s] will use this file\n", - video.c_str ()); - - break; - } - } - } - } - battleEndWinMusicFilename = - config.getString ("BattleEndWinMusicFilename", ""); - if (battleEndWinMusicFilename == "") - { - string battleEndWinPath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_MUSIC_PATH + "battle_end_win.*"; - vector < string > battleEndWinMusic; - findAll (battleEndWinPath, battleEndWinMusic, false, false); - for (int i = 0; i < (int) battleEndWinMusic.size (); ++i) - { - string music = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_MUSIC_PATH + battleEndWinMusic[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end win music [%s] exists\n", - music.c_str ()); - - if (fileExists (music)) - { - battleEndWinMusicFilename = music; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND battle end win music [%s] will use this file\n", - music.c_str ()); - - break; - } - } - if (battleEndWinMusicFilename == "") - { - battleEndWinPath = data_path + CORE_MENU_MUSIC_PATH - + "battle_end_win.*"; - battleEndWinMusic.clear (); - findAll (battleEndWinPath, battleEndWinMusic, false, false); - for (int i = 0; i < (int) battleEndWinMusic.size (); ++i) - { - string music = data_path + CORE_MENU_MUSIC_PATH - + battleEndWinMusic[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end win music [%s] exists\n", - music.c_str ()); - - if (fileExists (music)) - { - battleEndWinMusicFilename = music; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("FOUND battle end music win [%s] will use this file\n", - music.c_str ()); - - break; - } - } - } - } - battleEndLoseVideoFilename = - config.getString ("BattleEndLoseVideoURL", ""); - battleEndLoseVideoFilenameFallback = - config.getString ("BattleEndLoseVideoURLFallback", ""); - if (battleEndLoseVideoFilename == "") - { - string battleEndLoseVideoPath = - getGameCustomCoreDataPath (data_path, - "") + CORE_MENU_VIDEOS_PATH + - "battle_end_lose.*"; - vector < string > battleEndLoseVideos; - findAll (battleEndLoseVideoPath, battleEndLoseVideos, false, false); - for (int i = 0; i < (int) battleEndLoseVideos.size (); ++i) - { - string video = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_VIDEOS_PATH + battleEndLoseVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end lose video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - battleEndLoseVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND battle end lose video [%s] will use this file\n", - video.c_str ()); - - break; - } - } - if (battleEndLoseVideoFilename == "") - { - battleEndLoseVideoPath = data_path + CORE_MENU_VIDEOS_PATH - + "battle_end_lose.*"; - battleEndLoseVideos.clear (); - findAll (battleEndLoseVideoPath, battleEndLoseVideos, false, false); - for (int i = 0; i < (int) battleEndLoseVideos.size (); ++i) - { - string video = data_path + CORE_MENU_VIDEOS_PATH - + battleEndLoseVideos[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end lose video [%s] exists\n", - video.c_str ()); - - if (fileExists (video)) - { - battleEndLoseVideoFilename = video; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("FOUND battle end video lose [%s] will use this file\n", - video.c_str ()); - - break; - } - } - } - } - battleEndLoseMusicFilename = - config.getString ("BattleEndLoseMusicFilename", ""); - if (battleEndLoseMusicFilename == "") - { - string battleEndLosePath = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_MUSIC_PATH + "battle_end_lose.*"; - vector < string > battleEndLoseMusic; - findAll (battleEndLosePath, battleEndLoseMusic, false, false); - for (int i = 0; i < (int) battleEndLoseMusic.size (); ++i) - { - string music = getGameCustomCoreDataPath (data_path, "") - + CORE_MENU_MUSIC_PATH + battleEndLoseMusic[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end lose music [%s] exists\n", - music.c_str ()); - - if (fileExists (music)) - { - battleEndLoseMusicFilename = music; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("FOUND battle end lose music [%s] will use this file\n", - music.c_str ()); - - break; - } - } - if (battleEndLoseMusicFilename == "") - { - battleEndLosePath = data_path + CORE_MENU_MUSIC_PATH - + "battle_end_lose.*"; - battleEndLoseMusic.clear (); - findAll (battleEndLosePath, battleEndLoseMusic, false, false); - for (int i = 0; i < (int) battleEndLoseMusic.size (); ++i) - { - string music = data_path + CORE_MENU_MUSIC_PATH - + battleEndLoseMusic[i]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Checking if battle end lose music [%s] exists\n", - music.c_str ()); - - if (fileExists (music)) - { - battleEndLoseMusicFilename = music; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("FOUND battle end music lose [%s] will use this file\n", - music.c_str ()); - - break; - } - } - } - } - } - - void CoreData::load () - { - string data_path = CoreData::getDataPath (); - - Logger::getInstance ().add (Lang::getInstance (). - getString ("LogScreenCoreDataLoading", "", - true)); - - // textures - loadTextures (data_path); - - // fonts - loadFonts (); - - // sounds - loadSounds (data_path); - - // music - loadMusic (data_path); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false && - Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == true) - { - - loadIntroMedia (data_path); - - loadMainMenuMedia (data_path); - - loadBattleEndMedia (data_path); - } - } - - bool CoreData::hasIntroVideoFilename () const - { - bool result = (introVideoFilename != ""); - return result; - } - - bool CoreData::hasMainMenuVideoFilename () const - { - bool result = (mainMenuVideoFilename != ""); - return result; - } - -//bool CoreData::hasBattleEndVideoFilename(bool won) const { -// bool result = false; -// if(won == true) { -// result =(battleEndWinVideoFilename != ""); -// } -// else { -// result =(battleEndLoseVideoFilename != ""); -// } -// return result; -//} - - void CoreData::registerFontChangedCallback (std::string entityName, - FontChangedCallbackInterface * - cb) - { - if (entityName == "") - { - printf ("Register Font Callback detected a blank entityName!\n"); - throw - megaglest_runtime_error - ("Register Font Callback detected a blank entityName!"); - } - if (entityName != "") - { - registeredFontChangedCallbacks[entityName].push_back (cb); - } - } - void CoreData::unRegisterFontChangedCallback (std::string entityName) - { - if (entityName == "") - { - printf ("UnRegister Font Callback detected a blank entityName!\n"); - throw - megaglest_runtime_error - ("UnRegister Font Callback detected a blank entityName!"); - } - if (entityName != "") - { - registeredFontChangedCallbacks.erase (entityName); - } - } - void CoreData::triggerFontChangedCallbacks (std::string fontUniqueId, - Font * font) - { - for (std::map < std::string, - std::vector < - FontChangedCallbackInterface * > >::const_iterator iterMap = - registeredFontChangedCallbacks.begin (); - iterMap != registeredFontChangedCallbacks.end (); ++iterMap) - { - for (unsigned int index = 0; index < iterMap->second.size (); ++index) - { - //printf("Font Callback detected calling: Control [%s] for Font: [%s] value [%p]\n",iterMap->first.c_str(),fontUniqueId.c_str(),font); - FontChangedCallbackInterface *cb = iterMap->second[index]; - cb->FontChangedCallback (fontUniqueId, font); - } - } - } - void CoreData::loadFonts () - { - Lang & lang = Lang::getInstance (); - - //display font - Config & config = Config::getInstance (); - - string displayFontNamePrefix = config.getString ("FontDisplayPrefix"); - string displayFontNamePostfix = config.getString ("FontDisplayPostfix"); - int displayFontSize = - computeFontSize (config.getInt ("FontDisplayBaseSize")); - - if (lang.hasString ("FontDisplayPrefix") == true) - { - displayFontNamePrefix = lang.getString ("FontDisplayPrefix"); - } - if (lang.hasString ("FontDisplayPostfix") == true) - { - displayFontNamePostfix = lang.getString ("FontDisplayPostfix"); - } - if (lang.hasString ("FontDisplayBaseSize") == true) - { - displayFontSize = - computeFontSize (strToInt (lang.getString ("FontDisplayBaseSize"))); - } - string displayFontName = - displayFontNamePrefix + intToStr (displayFontSize) + - displayFontNamePostfix; - - displayFont = - loadFont < Font2D > (displayFont, displayFontName, displayFontSize, - "FontDisplay", "FontDisplayFamily", - "displayFont"); - displayFont3D = - loadFont < Font3D > (displayFont3D, displayFontName, displayFontSize, - "FontDisplay", "FontDisplayFamily", - "displayFont3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] displayFontName = [%s] displayFontSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - displayFontName.c_str (), displayFontSize); - - //menu fonts - string displayFontNameSmallPrefix = - config.getString ("FontDisplayPrefix"); - string displayFontNameSmallPostfix = - config.getString ("FontDisplayPostfix"); - int displayFontNameSmallSize = - computeFontSize (config.getInt ("FontDisplaySmallBaseSize")); - - if (lang.hasString ("FontDisplayPrefix") == true) - { - displayFontNameSmallPrefix = lang.getString ("FontDisplayPrefix"); - } - if (lang.hasString ("FontDisplayPostfix") == true) - { - displayFontNameSmallPostfix = lang.getString ("FontDisplayPostfix"); - } - if (lang.hasString ("FontDisplaySmallBaseSize") == true) - { - displayFontNameSmallSize = - computeFontSize (strToInt - (lang.getString ("FontDisplaySmallBaseSize"))); - } - string displayFontNameSmall = - displayFontNameSmallPrefix + intToStr (displayFontNameSmallSize) + - displayFontNameSmallPostfix; - - displayFontSmall = - loadFont < Font2D > (displayFontSmall, displayFontNameSmall, - displayFontNameSmallSize, "FontSmallDisplay", - "FontSmallDisplayFamily", "displayFontSmall"); - displayFontSmall3D = - loadFont < Font3D > (displayFontSmall3D, displayFontNameSmall, - displayFontNameSmallSize, "FontSmallDisplay", - "FontSmallDisplayFamily", "displayFontSmall3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] displayFontSmallName = [%s] displayFontSmallNameSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - displayFontNameSmall.c_str (), - displayFontNameSmallSize); - - string menuFontNameNormalPrefix = - config.getString ("FontMenuNormalPrefix"); - string menuFontNameNormalPostfix = - config.getString ("FontMenuNormalPostfix"); - int menuFontNameNormalSize = - computeFontSize (config.getInt ("FontMenuNormalBaseSize")); - if (lang.hasString ("FontMenuNormalPrefix") == true) - { - menuFontNameNormalPrefix = lang.getString ("FontMenuNormalPrefix"); - } - if (lang.hasString ("FontMenuNormalPostfix") == true) - { - menuFontNameNormalPostfix = lang.getString ("FontMenuNormalPostfix"); - } - if (lang.hasString ("FontMenuNormalBaseSize") == true) - { - menuFontNameNormalSize = - computeFontSize (strToInt - (lang.getString ("FontMenuNormalBaseSize"))); - } - string menuFontNameNormal = - menuFontNameNormalPrefix + intToStr (menuFontNameNormalSize) + - menuFontNameNormalPostfix; - - menuFontNormal = - loadFont < Font2D > (menuFontNormal, menuFontNameNormal, - menuFontNameNormalSize, "FontMenuNormal", - "FontMenuNormalFamily", "menuFontNormal"); - menuFontNormal3D = - loadFont < Font3D > (menuFontNormal3D, menuFontNameNormal, - menuFontNameNormalSize, "FontMenuNormal", - "FontMenuNormalFamily", "menuFontNormal3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] menuFontNormalName = [%s] menuFontNormalNameSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - menuFontNameNormal.c_str (), - menuFontNameNormalSize); - - string menuFontNameBigPrefix = config.getString ("FontMenuBigPrefix"); - string menuFontNameBigPostfix = config.getString ("FontMenuBigPostfix"); - int menuFontNameBigSize = - computeFontSize (config.getInt ("FontMenuBigBaseSize")); - - if (lang.hasString ("FontMenuBigPrefix") == true) - { - menuFontNameBigPrefix = lang.getString ("FontMenuBigPrefix"); - } - if (lang.hasString ("FontMenuBigPostfix") == true) - { - menuFontNameBigPostfix = lang.getString ("FontMenuBigPostfix"); - } - if (lang.hasString ("FontMenuBigBaseSize") == true) - { - menuFontNameBigSize = - computeFontSize (strToInt (lang.getString ("FontMenuBigBaseSize"))); - } - string menuFontNameBig = - menuFontNameBigPrefix + intToStr (menuFontNameBigSize) + - menuFontNameBigPostfix; - - menuFontBig = - loadFont < Font2D > (menuFontBig, menuFontNameBig, - menuFontNameBigSize, "FontMenuBig", - "FontMenuBigFamily", "menuFontBig"); - menuFontBig3D = - loadFont < Font3D > (menuFontBig3D, menuFontNameBig, - menuFontNameBigSize, "FontMenuBig", - "FontMenuBigFamily", "menuFontBig3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] menuFontNameBig = [%s] menuFontNameBigSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - menuFontNameBig.c_str (), - menuFontNameBigSize); - - string menuFontNameVeryBigPrefix = - config.getString ("FontMenuBigPrefix"); - string menuFontNameVeryBigPostfix = - config.getString ("FontMenuBigPostfix"); - int menuFontNameVeryBigSize = - computeFontSize (config.getInt ("FontMenuVeryBigBaseSize")); - - if (lang.hasString ("FontMenuBigPrefix") == true) - { - menuFontNameVeryBigPrefix = lang.getString ("FontMenuBigPrefix"); - } - if (lang.hasString ("FontMenuBigPostfix") == true) - { - menuFontNameVeryBigPostfix = lang.getString ("FontMenuBigPostfix"); - } - if (lang.hasString ("FontMenuVeryBigBaseSize") == true) - { - menuFontNameVeryBigSize = - computeFontSize (strToInt - (lang.getString ("FontMenuVeryBigBaseSize"))); - } - string menuFontNameVeryBig = - menuFontNameVeryBigPrefix + intToStr (menuFontNameVeryBigSize) + - menuFontNameVeryBigPostfix; - - menuFontVeryBig = - loadFont < Font2D > (menuFontVeryBig, menuFontNameVeryBig, - menuFontNameVeryBigSize, "FontMenuVeryBig", - "FontMenuVeryBigFamily", "menuFontVeryBig"); - menuFontVeryBig3D = - loadFont < Font3D > (menuFontVeryBig3D, menuFontNameVeryBig, - menuFontNameVeryBigSize, "FontMenuVeryBig", - "FontMenuVeryBigFamily", "menuFontVeryBig3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] menuFontNameVeryBig = [%s] menuFontNameVeryBigSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - menuFontNameVeryBig.c_str (), - menuFontNameVeryBigSize); - - //console font - string consoleFontNamePrefix = config.getString ("FontConsolePrefix"); - string consoleFontNamePostfix = config.getString ("FontConsolePostfix"); - int consoleFontNameSize = - computeFontSize (config.getInt ("FontConsoleBaseSize")); - - if (lang.hasString ("FontConsolePrefix") == true) - { - consoleFontNamePrefix = lang.getString ("FontConsolePrefix"); - } - if (lang.hasString ("FontConsolePostfix") == true) - { - consoleFontNamePostfix = lang.getString ("FontConsolePostfix"); - } - if (lang.hasString ("FontConsoleBaseSize") == true) - { - consoleFontNameSize = - computeFontSize (strToInt (lang.getString ("FontConsoleBaseSize"))); - } - string consoleFontName = - consoleFontNamePrefix + intToStr (consoleFontNameSize) + - consoleFontNamePostfix; - - consoleFont = - loadFont < Font2D > (consoleFont, consoleFontName, - consoleFontNameSize, "FontConsole", - "FontConsoleFamily", "consoleFont"); - consoleFont3D = - loadFont < Font3D > (consoleFont3D, consoleFontName, - consoleFontNameSize, "FontConsole", - "FontConsoleFamily", "consoleFont3D"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] consoleFontName = [%s] consoleFontNameSize = %d\n", - __FILE__, __FUNCTION__, __LINE__, - consoleFontName.c_str (), - consoleFontNameSize); - } - - - template < typename T > T * CoreData::loadFont (Font * menuFont, - string menuFontName, - int menuFontNameSize, - string fontType, - string fontTypeFamily, - string fontUniqueKey) - { - Renderer & renderer = Renderer::getInstance (); - if (menuFont) - { - string fontUniqueId = menuFont->getFontUniqueId (); - renderer.endFont (menuFont, rsGlobal); - menuFont = NULL; - triggerFontChangedCallbacks (fontUniqueId, menuFont); - } - if (Renderer::renderText3DEnabled == false) - { - menuFont = renderer.newFont (rsGlobal); - } - else - { - menuFont = renderer.newFont3D (rsGlobal); - } - if (menuFont) - { - Config & config = Config::getInstance (); - menuFont->setType (menuFontName, config.getString (fontType, ""), - config.getString (fontTypeFamily, "")); - menuFont->setSize (menuFontNameSize); - menuFont->setWidth (Font::wBold); - menuFont->setFontUniqueId (fontUniqueKey); - triggerFontChangedCallbacks (menuFont->getFontUniqueId (), menuFont); - } - return (T *) menuFont; - } - - int CoreData::computeFontSize (int size) - { - int rs = size; - Config & config = Config::getInstance (); - if (Font::forceLegacyFonts == true) - { - int screenH = config.getInt ("ScreenHeight"); - rs = size * screenH / 1024; - } - //FontSizeAdjustment - rs += config.getInt ("FontSizeAdjustment"); - if (Font::forceLegacyFonts == false) - { - rs += Font::baseSize; //basesize only for new font system - } - if (Font::forceLegacyFonts == true) - { - if (rs < 10) - { - rs = 10; - } - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] fontsize original %d calculated:%d \n", - __FILE__, __FUNCTION__, __LINE__, size, rs); - return rs; - } - - void CoreData::saveGameSettingsToFile (std::string fileName, - GameSettings * gameSettings, - int advancedIndex) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - - Config & config = Config::getInstance (); - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - fileName = userData + fileName; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] fileName = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.c_str ()); +namespace Glest { + namespace Game { + + // ===================================================== + // class CoreData + // ===================================================== + + static string tempDataLocation = getUserHome(); + // ===================== PUBLIC ======================== + + static const string CORE_PATH = "data/core/"; + static const string CORE_MISC_TEXTURES_PATH = + CORE_PATH + "misc_textures/"; + + static const string CORE_MENU_PATH = CORE_PATH + "menu/"; + static const string CORE_MENU_TEXTURES_PATH = + CORE_MENU_PATH + "textures/"; + static const string CORE_MENU_SOUND_PATH = CORE_MENU_PATH + "sound/"; + static const string CORE_MENU_MUSIC_PATH = CORE_MENU_PATH + "music/"; + static const string CORE_MENU_VIDEOS_PATH = CORE_MENU_PATH + "videos/"; + + static const string CORE_WATER_SOUNDS_PATH = CORE_PATH + "/water_sounds/"; + + // Sound effects + // These variables are specified in the ini file + const string PlaySoundClip::sfxAttention = "PlaySoundAttention"; + const string PlaySoundClip::sfxHighlight = "PlaySoundHighlight"; + const string PlaySoundClip::sfxNewServer = "PlaySoundNewServer"; + const string PlaySoundClip::sfxMarker = "PlaySoundMarker"; + const string PlaySoundClip::sfxMenuClickA = "PlaySoundMenuClickA"; + const string PlaySoundClip::sfxMenuClickB = "PlaySoundMenuClickB"; + const string PlaySoundClip::sfxMenuClickC = "PlaySoundMenuClickC"; + + CoreData & CoreData::getInstance() { + static CoreData coreData; + return coreData; + } + + CoreData::CoreData() { + logoTexture = NULL; + logoTextureList.clear(); + backgroundTexture = NULL; + fireTexture = NULL; + teamColorTexture = NULL; + snowTexture = NULL; + waterSplashTexture = NULL; + customTexture = NULL; + buttonSmallTexture = NULL; + buttonBigTexture = NULL; + horizontalLineTexture = NULL; + verticalLineTexture = NULL; + checkBoxTexture = NULL; + checkedCheckBoxTexture = NULL; + gameWinnerTexture = NULL; + notOnServerTexture = NULL; + onServerDifferentTexture = NULL; + onServerTexture = NULL; + onServerInstalledTexture = NULL; + statusReadyTexture = NULL; + statusNotReadyTexture = NULL; + statusBRBTexture = NULL; + + healthbarTexture = NULL; + healthbarBackgroundTexture = NULL; + + miscTextureList.clear(); + + displayFont = NULL; + menuFontNormal = NULL; + displayFontSmall = NULL; + menuFontBig = NULL; + menuFontVeryBig = NULL; + consoleFont = NULL; + + displayFont3D = NULL; + menuFontNormal3D = NULL; + displayFontSmall3D = NULL; + menuFontBig3D = NULL; + menuFontVeryBig3D = NULL; + consoleFont3D = NULL; + + introVideoFilename = ""; + mainMenuVideoFilename = ""; + + battleEndWinVideoFilename = ""; + battleEndWinVideoFilenameFallback = ""; + battleEndWinMusicFilename = ""; + battleEndLoseVideoFilename = ""; + battleEndLoseVideoFilenameFallback = ""; + battleEndLoseMusicFilename = ""; + } + + CoreData::~CoreData() { + cleanup(); + } + + void CoreData::cleanup() { + deleteValues(waterSounds.getSoundsPtr()->begin(), + waterSounds.getSoundsPtr()->end()); + waterSounds.getSoundsPtr()->clear(); + } + + Texture2D *CoreData::getTextureBySystemId(TextureSystemType type) { + Texture2D *result = NULL; + switch (type) { + case tsyst_logoTexture: + result = getLogoTexture(); + break; + //std::vector logoTextureList; + case tsyst_backgroundTexture: + result = getBackgroundTexture(); + break; + case tsyst_fireTexture: + result = getFireTexture(); + break; + case tsyst_teamColorTexture: + result = getTeamColorTexture(); + break; + case tsyst_snowTexture: + result = getSnowTexture(); + break; + case tsyst_waterSplashTexture: + result = getWaterSplashTexture(); + break; + case tsyst_customTexture: + result = getCustomTexture(); + break; + case tsyst_buttonSmallTexture: + result = buttonSmallTexture; + break; + case tsyst_buttonBigTexture: + result = buttonBigTexture; + break; + case tsyst_horizontalLineTexture: + result = horizontalLineTexture; + break; + case tsyst_verticalLineTexture: + result = verticalLineTexture; + break; + case tsyst_checkBoxTexture: + result = checkBoxTexture; + break; + case tsyst_checkedCheckBoxTexture: + result = checkedCheckBoxTexture; + break; + case tsyst_gameWinnerTexture: + result = gameWinnerTexture; + break; + case tsyst_notOnServerTexture: + result = notOnServerTexture; + break; + case tsyst_onServerDifferentTexture: + result = onServerDifferentTexture; + break; + case tsyst_onServerTexture: + result = onServerTexture; + break; + case tsyst_onServerInstalledTexture: + result = onServerInstalledTexture; + break; + case tsyst_statusReadyTexture: + result = statusReadyTexture; + break; + case tsyst_statusNotReadyTexture: + result = statusNotReadyTexture; + break; + case tsyst_statusBRBTexture: + result = statusBRBTexture; + break; + case tsyst_healthbarTexture: + result = healthbarTexture; + break; + case tsyst_healthbarBackgroundTexture: + result = healthbarBackgroundTexture; + break; + + //std::vector miscTextureList; + } + return result; + } + + void CoreData::cleanupTexture(Texture2D ** texture) { + Renderer & renderer = Renderer::getInstance(); + renderer.endTexture(rsGlobal, *texture); + *texture = NULL; + } + + void CoreData::loadTextureIfRequired(Texture2D ** tex, string data_path, + string uniqueFilePath, + int texSystemId, bool setMipMap, + bool setAlpha, + bool loadUniqueFilePath, + bool compressionDisabled) { + if (*tex == NULL) { + bool attemptToLoadTexture = (texSystemId == tsyst_NONE); + if (attemptToLoadTexture == false && + itemLoadAttempted.find(texSystemId) == itemLoadAttempted.end()) { + + attemptToLoadTexture = true; + itemLoadAttempted[texSystemId] = true; + } + + if (attemptToLoadTexture == true) { + Renderer & renderer = Renderer::getInstance(); + *tex = renderer.newTexture2D(rsGlobal); + if (*tex) { + + (*tex)->setForceCompressionDisabled(compressionDisabled); + (*tex)->setMipmap(setMipMap); + if (setAlpha == true) { + + (*tex)->setFormat(Texture::fAlpha); + (*tex)->getPixmap()->init(1); + } + + try { + string fileToLoad = uniqueFilePath; + if (loadUniqueFilePath == false) { + + fileToLoad = + getGameCustomCoreDataPath(data_path, uniqueFilePath); + } + (*tex)->getPixmap()->load(fileToLoad); + (*tex)->setTextureSystemId(texSystemId); + + renderer.initTexture(rsGlobal, *tex); + } catch (const megaglest_runtime_error & ex) { + message(ex.what(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + cleanupTexture(tex); + } + } + } + } + } + + string CoreData::getDataPath() { + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if (data_path != "") { + endPathWithSlash(data_path); + } + return data_path; + } + + Texture2D *CoreData::getBackgroundTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&backgroundTexture, getDataPath(), + CORE_MENU_TEXTURES_PATH + "back.tga", + tsyst_backgroundTexture, false, false, false); + + return backgroundTexture; + } + Texture2D *CoreData::getFireTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&fireTexture, data_path, + CORE_MISC_TEXTURES_PATH + "fire_particle.tga", + tsyst_fireTexture, true, true, false); + + return fireTexture; + } + Texture2D *CoreData::getTeamColorTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&teamColorTexture, data_path, + CORE_MISC_TEXTURES_PATH + + "team_color_texture.tga", tsyst_teamColorTexture, + true, true, false); + + return teamColorTexture; + } + Texture2D *CoreData::getSnowTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&snowTexture, data_path, + CORE_MISC_TEXTURES_PATH + "snow_particle.tga", + tsyst_snowTexture, false, true, false); + + return snowTexture; + } + Texture2D *CoreData::getLogoTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&logoTexture, data_path, + CORE_MENU_TEXTURES_PATH + "logo.tga", + tsyst_logoTexture, false, false, false); + + return logoTexture; + } + Texture2D *CoreData::getWaterSplashTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&waterSplashTexture, data_path, + CORE_MISC_TEXTURES_PATH + "water_splash.tga", + tsyst_waterSplashTexture, true, true, false); + + return waterSplashTexture; + } + Texture2D *CoreData::getCustomTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&customTexture, data_path, + CORE_MENU_TEXTURES_PATH + "custom_texture.tga", + tsyst_customTexture, true, false, false); + + return customTexture; + } + Texture2D *CoreData::getButtonSmallTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&buttonSmallTexture, data_path, + CORE_MENU_TEXTURES_PATH + "button_small.tga", + tsyst_buttonSmallTexture, true, false, false, + true); + + return buttonSmallTexture; + } + Texture2D *CoreData::getButtonBigTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&buttonBigTexture, data_path, + CORE_MENU_TEXTURES_PATH + "button_big.tga", + tsyst_buttonBigTexture, true, false, false, + true); + + return buttonBigTexture; + } + Texture2D *CoreData::getHorizontalLineTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&horizontalLineTexture, data_path, + CORE_MENU_TEXTURES_PATH + "line_horizontal.tga", + tsyst_horizontalLineTexture, true, false, false, + true); + + return horizontalLineTexture; + } + Texture2D *CoreData::getVerticalLineTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&verticalLineTexture, data_path, + CORE_MENU_TEXTURES_PATH + "line_vertical.tga", + tsyst_verticalLineTexture, true, false, false, + true); + + return verticalLineTexture; + } + Texture2D *CoreData::getCheckBoxTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&checkBoxTexture, data_path, + CORE_MENU_TEXTURES_PATH + "checkbox.tga", + tsyst_checkBoxTexture, true, false, false, true); + + return checkBoxTexture; + } + Texture2D *CoreData::getCheckedCheckBoxTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&checkedCheckBoxTexture, data_path, + CORE_MENU_TEXTURES_PATH + "checkbox_checked.tga", + tsyst_checkedCheckBoxTexture, true, false, false, + true); + + return checkedCheckBoxTexture; + } + Texture2D *CoreData::getNotOnServerTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(¬OnServerTexture, data_path, + CORE_MENU_TEXTURES_PATH + "not_on_server.tga", + tsyst_notOnServerTexture, true, false, false); + + return notOnServerTexture; + } + Texture2D *CoreData::getOnServerDifferentTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&onServerDifferentTexture, data_path, + CORE_MENU_TEXTURES_PATH + + "on_server_different.tga", + tsyst_onServerDifferentTexture, true, false, + false); + + return onServerDifferentTexture; + } + Texture2D *CoreData::getOnServerTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&onServerTexture, data_path, + CORE_MENU_TEXTURES_PATH + "on_server.tga", + tsyst_onServerTexture, true, false, false); + + return onServerTexture; + } + Texture2D *CoreData::getOnServerInstalledTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&onServerInstalledTexture, data_path, + CORE_MENU_TEXTURES_PATH + + "on_server_installed.tga", + tsyst_onServerInstalledTexture, true, false, + false); + + return onServerInstalledTexture; + } + Texture2D *CoreData::getStatusReadyTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&statusReadyTexture, data_path, + CORE_MENU_TEXTURES_PATH + "status_ready.png", + tsyst_statusReadyTexture, true, false, false); + + return statusReadyTexture; + } + Texture2D *CoreData::getStatusNotReadyTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&statusNotReadyTexture, data_path, + CORE_MENU_TEXTURES_PATH + "status_notready.png", + tsyst_statusNotReadyTexture, true, false, false); + + return statusNotReadyTexture; + } + Texture2D *CoreData::getStatusBRBTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&statusBRBTexture, data_path, + CORE_MENU_TEXTURES_PATH + "status_brb.png", + tsyst_statusBRBTexture, true, false, false); + + return statusBRBTexture; + } + Texture2D *CoreData::getGameWinnerTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&gameWinnerTexture, data_path, + CORE_MISC_TEXTURES_PATH + "game_winner.png", + tsyst_gameWinnerTexture, true, false, false, + true); + + return gameWinnerTexture; + } + + Texture2D *CoreData::getHealthbarTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&healthbarTexture, data_path, + CORE_MISC_TEXTURES_PATH + "healthbar.png", + tsyst_healthbarTexture, true, false, false, + true); + + return healthbarTexture; + } + + Texture2D *CoreData::getHealthbarBackgroundTexture() { + string data_path = getDataPath(); + loadTextureIfRequired(&healthbarBackgroundTexture, data_path, + CORE_MISC_TEXTURES_PATH + + "healthbarBackground.png", + tsyst_healthbarBackgroundTexture, true, false, + false, true); + + return healthbarBackgroundTexture; + } + + void CoreData::loadLogoTextureExtraIfRequired() { + int loadAttemptLookupKey = tsyst_COUNT + 1; + if (itemLoadAttempted.find(loadAttemptLookupKey) == + itemLoadAttempted.end()) { + + itemLoadAttempted[loadAttemptLookupKey] = true; + + string data_path = getDataPath(); + logoTextureList.clear(); + string logosPath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_TEXTURES_PATH + "logo*.*"; + vector < string > logoFilenames; + findAll(logosPath, logoFilenames, false, false); + for (int index = 0; index < (int) logoFilenames.size(); ++index) { + + string logo = logoFilenames[index]; + if (strcmp("logo.tga", logo.c_str()) != 0) { + + Texture2D *logoTextureExtra = NULL; + loadTextureIfRequired(&logoTextureExtra, data_path, + getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_TEXTURES_PATH + logo, tsyst_NONE, + true, false, true); + logoTextureList.push_back(logoTextureExtra); + } + } + if (logoTextureList.empty() == true) { + + logosPath = data_path + CORE_MENU_TEXTURES_PATH + "logo*.*"; + vector < string > logoFilenames; + findAll(logosPath, logoFilenames, false, false); + for (int index = 0; index < (int) logoFilenames.size(); ++index) { + + string logo = logoFilenames[index]; + if (strcmp("logo.tga", logo.c_str()) != 0) { + + Texture2D *logoTextureExtra = NULL; + loadTextureIfRequired(&logoTextureExtra, data_path, + data_path + CORE_MENU_TEXTURES_PATH + + logo, tsyst_NONE, true, false, true); + logoTextureList.push_back(logoTextureExtra); + } + } + } + } + } + size_t CoreData::getLogoTextureExtraCount() { + loadLogoTextureExtraIfRequired(); + return logoTextureList.size(); + } + Texture2D *CoreData::getLogoTextureExtra(int idx) { + loadLogoTextureExtraIfRequired(); + return logoTextureList[idx]; + } + + void CoreData::loadMiscTextureListIfRequired() { + int loadAttemptLookupKey = tsyst_COUNT + 2; + if (itemLoadAttempted.find(loadAttemptLookupKey) == + itemLoadAttempted.end()) { + + itemLoadAttempted[loadAttemptLookupKey] = true; + + string data_path = getDataPath(); + + miscTextureList.clear(); + string introPath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_TEXTURES_PATH + "intro*.*"; + vector < string > introFilenames; + findAll(introPath, introFilenames, false, false); + for (int i = 0; i < (int) introFilenames.size(); ++i) { + string logo = introFilenames[i]; + + Texture2D *logoTextureExtra = NULL; + loadTextureIfRequired(&logoTextureExtra, data_path, + getGameCustomCoreDataPath(data_path, + "") + + CORE_MENU_TEXTURES_PATH + logo, tsyst_NONE, + true, false, true); + miscTextureList.push_back(logoTextureExtra); + } + if (miscTextureList.empty() == true) { + introPath = data_path + CORE_MENU_TEXTURES_PATH + "intro*.*"; + vector < string > introFilenames; + findAll(introPath, introFilenames, false, false); + for (int i = 0; i < (int) introFilenames.size(); ++i) { + string logo = introFilenames[i]; + + Texture2D *logoTextureExtra = NULL; + loadTextureIfRequired(&logoTextureExtra, data_path, + data_path + CORE_MENU_TEXTURES_PATH + logo, + tsyst_NONE, true, false, true); + miscTextureList.push_back(logoTextureExtra); + } + } + + } + } + + std::vector < Texture2D * >&CoreData::getMiscTextureList() { + loadMiscTextureListIfRequired(); + return miscTextureList; + } + + void CoreData::loadTextures(string data_path) { + // Required to be loaded at program startup as they may be accessed in + // threads or some other dangerous way so lazy loading is not an option + getCustomTexture(); + } + + StaticSound *CoreData::getClickSoundA() { + static PlaySoundClip snd; + return snd.getSound(snd.sfxMenuClickA); + } + + StaticSound *CoreData::getClickSoundB() { + static PlaySoundClip snd; + return snd.getSound(snd.sfxMenuClickB); + + } + StaticSound *CoreData::getClickSoundC() { + static PlaySoundClip snd; + return snd.getSound(snd.sfxMenuClickC); + } + + void CoreData::loadWaterSoundsIfRequired() { + int loadAttemptLookupKey = tsyst_COUNT + 9; + if (itemLoadAttempted.find(loadAttemptLookupKey) == + itemLoadAttempted.end()) { + + itemLoadAttempted[loadAttemptLookupKey] = true; + + string data_path = getDataPath(); + cleanup(); + waterSounds.resize(6); + + for (int index = 0; index < 6; ++index) { + waterSounds[index] = new StaticSound(); + if (waterSounds[index] != NULL) { + try { + waterSounds[index]->load(getGameCustomCoreDataPath(data_path, + CORE_WATER_SOUNDS_PATH + + "water" + + intToStr + (index) + + ".wav")); + } catch (const megaglest_runtime_error & ex) { + message(ex.what(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } + } + } + } + } + + StaticSound *CoreData::getWaterSound() { + loadWaterSoundsIfRequired(); + return waterSounds.getRandSound(); + } + + void CoreData::loadSounds(string data_path) { + // sounds + // try { + // clickSoundA.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "click_a.wav")); + // clickSoundB.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "click_b.wav")); + // clickSoundC.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "click_c.wav")); + // attentionSound.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "attention.wav")); + // highlightSound.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "highlight.wav")); + // markerSound.load( + // getGameCustomCoreDataPath(data_path, + // CORE_MENU_SOUND_PATH + "sonar.wav")); + // } + // catch (const megaglest_runtime_error& ex) { + // message(ex.what(), GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + // tempDataLocation); + // } + + // cleanup(); + // waterSounds.resize(6); + // + // for (int i = 0; i < 6; ++i) { + // waterSounds[i] = new StaticSound(); + // if (waterSounds[i]) { + // try { + // waterSounds[i]->load( + // getGameCustomCoreDataPath(data_path, + // CORE_WATER_SOUNDS_PATH + "water" + intToStr(i) + // + ".wav")); + // } catch (const megaglest_runtime_error& ex) { + // message(ex.what(), + // GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + // tempDataLocation); + // } + // } + // } + } + + + void CoreData::loadMusicIfRequired() { + int loadAttemptLookupKey = tsyst_COUNT + 10; + if (itemLoadAttempted.find(loadAttemptLookupKey) == + itemLoadAttempted.end()) { + + itemLoadAttempted[loadAttemptLookupKey] = true; + + string data_path = getDataPath(); + + XmlTree xmlTree; + xmlTree. + load(getGameCustomCoreDataPath + (data_path, CORE_MENU_PATH + "menu.xml"), + Properties::getTagReplacementValues()); + const XmlNode *menuNode = xmlTree.getRootNode(); + string menuMusicPath = "/menu/music/"; + string menuIntroMusicFile = "intro_music.ogg"; + string menuMusicFile = "menu_music.ogg"; + if (menuNode->hasChild("intro") == true) { + const XmlNode *introNode = menuNode->getChild("intro"); + // intro info + const XmlNode *menuPathNode = + introNode->getChild("menu-music-path"); + menuMusicPath = + menuPathNode->getAttribute("value")->getRestrictedValue(); + const XmlNode *menuIntroMusicNode = + introNode->getChild("menu-intro-music"); + menuIntroMusicFile = + menuIntroMusicNode->getAttribute("value")->getRestrictedValue(); + const XmlNode *menuMusicNode = introNode->getChild("menu-music"); + menuMusicFile = + menuMusicNode->getAttribute("value")->getRestrictedValue(); + } + try { + introMusic.open(getGameCustomCoreDataPath(data_path, + CORE_PATH + + menuMusicPath + + menuIntroMusicFile)); + introMusic.setNext(&menuMusic); + menuMusic.open(getGameCustomCoreDataPath(data_path, + CORE_PATH + + menuMusicPath + + menuMusicFile)); + menuMusic.setNext(&menuMusic); + } catch (const megaglest_runtime_error & ex) { + message(ex.what(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } + } + } + + StrSound *CoreData::getIntroMusic() { + loadMusicIfRequired(); + return &introMusic; + } + + StrSound *CoreData::getMenuMusic() { + loadMusicIfRequired(); + return &menuMusic; + } + + void CoreData::loadMusic(string data_path) { + } + + void CoreData::loadIntroMedia(string data_path) { + Config & config = Config::getInstance(); + + introVideoFilename = config.getString("IntroVideoURL", ""); + introVideoFilenameFallback = + config.getString("IntroVideoURLFallback", ""); + if (introVideoFilename == "") { + string introVideoPath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + "intro.*"; + vector < string > introVideos; + findAll(introVideoPath, introVideos, false, false); + for (int i = 0; i < (int) introVideos.size(); ++i) { + string video = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + introVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if intro video [%s] exists\n", video.c_str()); + + if (fileExists(video)) { + introVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND intro video [%s] will use this file\n", + video.c_str()); + + break; + } + } + if (introVideoFilename == "") { + introVideoPath = data_path + CORE_MENU_VIDEOS_PATH + "intro.*"; + introVideos.clear(); + findAll(introVideoPath, introVideos, false, false); + for (int i = 0; i < (int) introVideos.size(); ++i) { + string video = data_path + CORE_MENU_VIDEOS_PATH + introVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if intro video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + introVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND intro video [%s] will use this file\n", + video.c_str()); + + break; + } + } + } + } + } + + void CoreData::loadMainMenuMedia(string data_path) { + Config & config = Config::getInstance(); + + mainMenuVideoFilename = config.getString("MainMenuVideoURL", ""); + mainMenuVideoFilenameFallback = + config.getString("MainMenuVideoURLFallback", ""); + if (mainMenuVideoFilename == "") { + string mainVideoPath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + "main.*"; + vector < string > mainVideos; + findAll(mainVideoPath, mainVideos, false, false); + for (int i = 0; i < (int) mainVideos.size(); ++i) { + string video = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + mainVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if mainmenu video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + mainMenuVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND mainmenu video [%s] will use this file\n", + video.c_str()); + + break; + } + } + if (mainMenuVideoFilename == "") { + mainVideoPath = data_path + CORE_MENU_VIDEOS_PATH + "main.*"; + mainVideos.clear(); + findAll(mainVideoPath, mainVideos, false, false); + for (int i = 0; i < (int) mainVideos.size(); ++i) { + string video = data_path + CORE_MENU_VIDEOS_PATH + mainVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if mainmenu video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + mainMenuVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND mainmenu video [%s] will use this file\n", + video.c_str()); + + break; + } + } + } + } + } + + void CoreData::loadBattleEndMedia(string data_path) { + Config & config = Config::getInstance(); + + battleEndWinVideoFilename = + config.getString("BattleEndWinVideoURL", ""); + battleEndWinVideoFilenameFallback = + config.getString("BattleEndWinVideoURLFallback", ""); + if (battleEndWinVideoFilename == "") { + string battleEndWinVideoPath = + getGameCustomCoreDataPath(data_path, + "") + CORE_MENU_VIDEOS_PATH + + "battle_end_win.*"; + vector < string > battleEndWinVideos; + findAll(battleEndWinVideoPath, battleEndWinVideos, false, false); + for (int i = 0; i < (int) battleEndWinVideos.size(); ++i) { + string video = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + battleEndWinVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end win video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + battleEndWinVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND battle end win video [%s] will use this file\n", + video.c_str()); + + break; + } + } + if (battleEndWinVideoFilename == "") { + battleEndWinVideoPath = data_path + CORE_MENU_VIDEOS_PATH + + "battle_end_win.*"; + battleEndWinVideos.clear(); + findAll(battleEndWinVideoPath, battleEndWinVideos, false, false); + for (int i = 0; i < (int) battleEndWinVideos.size(); ++i) { + string video = data_path + CORE_MENU_VIDEOS_PATH + + battleEndWinVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end win video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + battleEndWinVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("FOUND battle end video win [%s] will use this file\n", + video.c_str()); + + break; + } + } + } + } + battleEndWinMusicFilename = + config.getString("BattleEndWinMusicFilename", ""); + if (battleEndWinMusicFilename == "") { + string battleEndWinPath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_MUSIC_PATH + "battle_end_win.*"; + vector < string > battleEndWinMusic; + findAll(battleEndWinPath, battleEndWinMusic, false, false); + for (int i = 0; i < (int) battleEndWinMusic.size(); ++i) { + string music = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_MUSIC_PATH + battleEndWinMusic[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end win music [%s] exists\n", + music.c_str()); + + if (fileExists(music)) { + battleEndWinMusicFilename = music; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND battle end win music [%s] will use this file\n", + music.c_str()); + + break; + } + } + if (battleEndWinMusicFilename == "") { + battleEndWinPath = data_path + CORE_MENU_MUSIC_PATH + + "battle_end_win.*"; + battleEndWinMusic.clear(); + findAll(battleEndWinPath, battleEndWinMusic, false, false); + for (int i = 0; i < (int) battleEndWinMusic.size(); ++i) { + string music = data_path + CORE_MENU_MUSIC_PATH + + battleEndWinMusic[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end win music [%s] exists\n", + music.c_str()); + + if (fileExists(music)) { + battleEndWinMusicFilename = music; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("FOUND battle end music win [%s] will use this file\n", + music.c_str()); + + break; + } + } + } + } + battleEndLoseVideoFilename = + config.getString("BattleEndLoseVideoURL", ""); + battleEndLoseVideoFilenameFallback = + config.getString("BattleEndLoseVideoURLFallback", ""); + if (battleEndLoseVideoFilename == "") { + string battleEndLoseVideoPath = + getGameCustomCoreDataPath(data_path, + "") + CORE_MENU_VIDEOS_PATH + + "battle_end_lose.*"; + vector < string > battleEndLoseVideos; + findAll(battleEndLoseVideoPath, battleEndLoseVideos, false, false); + for (int i = 0; i < (int) battleEndLoseVideos.size(); ++i) { + string video = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_VIDEOS_PATH + battleEndLoseVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end lose video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + battleEndLoseVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND battle end lose video [%s] will use this file\n", + video.c_str()); + + break; + } + } + if (battleEndLoseVideoFilename == "") { + battleEndLoseVideoPath = data_path + CORE_MENU_VIDEOS_PATH + + "battle_end_lose.*"; + battleEndLoseVideos.clear(); + findAll(battleEndLoseVideoPath, battleEndLoseVideos, false, false); + for (int i = 0; i < (int) battleEndLoseVideos.size(); ++i) { + string video = data_path + CORE_MENU_VIDEOS_PATH + + battleEndLoseVideos[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end lose video [%s] exists\n", + video.c_str()); + + if (fileExists(video)) { + battleEndLoseVideoFilename = video; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("FOUND battle end video lose [%s] will use this file\n", + video.c_str()); + + break; + } + } + } + } + battleEndLoseMusicFilename = + config.getString("BattleEndLoseMusicFilename", ""); + if (battleEndLoseMusicFilename == "") { + string battleEndLosePath = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_MUSIC_PATH + "battle_end_lose.*"; + vector < string > battleEndLoseMusic; + findAll(battleEndLosePath, battleEndLoseMusic, false, false); + for (int i = 0; i < (int) battleEndLoseMusic.size(); ++i) { + string music = getGameCustomCoreDataPath(data_path, "") + + CORE_MENU_MUSIC_PATH + battleEndLoseMusic[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end lose music [%s] exists\n", + music.c_str()); + + if (fileExists(music)) { + battleEndLoseMusicFilename = music; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("FOUND battle end lose music [%s] will use this file\n", + music.c_str()); + + break; + } + } + if (battleEndLoseMusicFilename == "") { + battleEndLosePath = data_path + CORE_MENU_MUSIC_PATH + + "battle_end_lose.*"; + battleEndLoseMusic.clear(); + findAll(battleEndLosePath, battleEndLoseMusic, false, false); + for (int i = 0; i < (int) battleEndLoseMusic.size(); ++i) { + string music = data_path + CORE_MENU_MUSIC_PATH + + battleEndLoseMusic[i]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Checking if battle end lose music [%s] exists\n", + music.c_str()); + + if (fileExists(music)) { + battleEndLoseMusicFilename = music; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("FOUND battle end music lose [%s] will use this file\n", + music.c_str()); + + break; + } + } + } + } + } + + void CoreData::load() { + string data_path = CoreData::getDataPath(); + + Logger::getInstance().add(Lang::getInstance(). + getString("LogScreenCoreDataLoading", "", + true)); + + // textures + loadTextures(data_path); + + // fonts + loadFonts(); + + // sounds + loadSounds(data_path); + + // music + loadMusic(data_path); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false && + Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true) { + + loadIntroMedia(data_path); + + loadMainMenuMedia(data_path); + + loadBattleEndMedia(data_path); + } + } + + bool CoreData::hasIntroVideoFilename() const { + bool result = (introVideoFilename != ""); + return result; + } + + bool CoreData::hasMainMenuVideoFilename() const { + bool result = (mainMenuVideoFilename != ""); + return result; + } + + //bool CoreData::hasBattleEndVideoFilename(bool won) const { + // bool result = false; + // if(won == true) { + // result =(battleEndWinVideoFilename != ""); + // } + // else { + // result =(battleEndLoseVideoFilename != ""); + // } + // return result; + //} + + void CoreData::registerFontChangedCallback(std::string entityName, + FontChangedCallbackInterface * + cb) { + if (entityName == "") { + printf("Register Font Callback detected a blank entityName!\n"); + throw + megaglest_runtime_error + ("Register Font Callback detected a blank entityName!"); + } + if (entityName != "") { + registeredFontChangedCallbacks[entityName].push_back(cb); + } + } + void CoreData::unRegisterFontChangedCallback(std::string entityName) { + if (entityName == "") { + printf("UnRegister Font Callback detected a blank entityName!\n"); + throw + megaglest_runtime_error + ("UnRegister Font Callback detected a blank entityName!"); + } + if (entityName != "") { + registeredFontChangedCallbacks.erase(entityName); + } + } + void CoreData::triggerFontChangedCallbacks(std::string fontUniqueId, + Font * font) { + for (std::map < std::string, + std::vector < + FontChangedCallbackInterface * > >::const_iterator iterMap = + registeredFontChangedCallbacks.begin(); + iterMap != registeredFontChangedCallbacks.end(); ++iterMap) { + for (unsigned int index = 0; index < iterMap->second.size(); ++index) { + //printf("Font Callback detected calling: Control [%s] for Font: [%s] value [%p]\n",iterMap->first.c_str(),fontUniqueId.c_str(),font); + FontChangedCallbackInterface *cb = iterMap->second[index]; + cb->FontChangedCallback(fontUniqueId, font); + } + } + } + void CoreData::loadFonts() { + Lang & lang = Lang::getInstance(); + + //display font + Config & config = Config::getInstance(); + + string displayFontNamePrefix = config.getString("FontDisplayPrefix"); + string displayFontNamePostfix = config.getString("FontDisplayPostfix"); + int displayFontSize = + computeFontSize(config.getInt("FontDisplayBaseSize")); + + if (lang.hasString("FontDisplayPrefix") == true) { + displayFontNamePrefix = lang.getString("FontDisplayPrefix"); + } + if (lang.hasString("FontDisplayPostfix") == true) { + displayFontNamePostfix = lang.getString("FontDisplayPostfix"); + } + if (lang.hasString("FontDisplayBaseSize") == true) { + displayFontSize = + computeFontSize(strToInt(lang.getString("FontDisplayBaseSize"))); + } + string displayFontName = + displayFontNamePrefix + intToStr(displayFontSize) + + displayFontNamePostfix; + + displayFont = + loadFont < Font2D >(displayFont, displayFontName, displayFontSize, + "FontDisplay", "FontDisplayFamily", + "displayFont"); + displayFont3D = + loadFont < Font3D >(displayFont3D, displayFontName, displayFontSize, + "FontDisplay", "FontDisplayFamily", + "displayFont3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] displayFontName = [%s] displayFontSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + displayFontName.c_str(), displayFontSize); + + //menu fonts + string displayFontNameSmallPrefix = + config.getString("FontDisplayPrefix"); + string displayFontNameSmallPostfix = + config.getString("FontDisplayPostfix"); + int displayFontNameSmallSize = + computeFontSize(config.getInt("FontDisplaySmallBaseSize")); + + if (lang.hasString("FontDisplayPrefix") == true) { + displayFontNameSmallPrefix = lang.getString("FontDisplayPrefix"); + } + if (lang.hasString("FontDisplayPostfix") == true) { + displayFontNameSmallPostfix = lang.getString("FontDisplayPostfix"); + } + if (lang.hasString("FontDisplaySmallBaseSize") == true) { + displayFontNameSmallSize = + computeFontSize(strToInt + (lang.getString("FontDisplaySmallBaseSize"))); + } + string displayFontNameSmall = + displayFontNameSmallPrefix + intToStr(displayFontNameSmallSize) + + displayFontNameSmallPostfix; + + displayFontSmall = + loadFont < Font2D >(displayFontSmall, displayFontNameSmall, + displayFontNameSmallSize, "FontSmallDisplay", + "FontSmallDisplayFamily", "displayFontSmall"); + displayFontSmall3D = + loadFont < Font3D >(displayFontSmall3D, displayFontNameSmall, + displayFontNameSmallSize, "FontSmallDisplay", + "FontSmallDisplayFamily", "displayFontSmall3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] displayFontSmallName = [%s] displayFontSmallNameSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + displayFontNameSmall.c_str(), + displayFontNameSmallSize); + + string menuFontNameNormalPrefix = + config.getString("FontMenuNormalPrefix"); + string menuFontNameNormalPostfix = + config.getString("FontMenuNormalPostfix"); + int menuFontNameNormalSize = + computeFontSize(config.getInt("FontMenuNormalBaseSize")); + if (lang.hasString("FontMenuNormalPrefix") == true) { + menuFontNameNormalPrefix = lang.getString("FontMenuNormalPrefix"); + } + if (lang.hasString("FontMenuNormalPostfix") == true) { + menuFontNameNormalPostfix = lang.getString("FontMenuNormalPostfix"); + } + if (lang.hasString("FontMenuNormalBaseSize") == true) { + menuFontNameNormalSize = + computeFontSize(strToInt + (lang.getString("FontMenuNormalBaseSize"))); + } + string menuFontNameNormal = + menuFontNameNormalPrefix + intToStr(menuFontNameNormalSize) + + menuFontNameNormalPostfix; + + menuFontNormal = + loadFont < Font2D >(menuFontNormal, menuFontNameNormal, + menuFontNameNormalSize, "FontMenuNormal", + "FontMenuNormalFamily", "menuFontNormal"); + menuFontNormal3D = + loadFont < Font3D >(menuFontNormal3D, menuFontNameNormal, + menuFontNameNormalSize, "FontMenuNormal", + "FontMenuNormalFamily", "menuFontNormal3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] menuFontNormalName = [%s] menuFontNormalNameSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + menuFontNameNormal.c_str(), + menuFontNameNormalSize); + + string menuFontNameBigPrefix = config.getString("FontMenuBigPrefix"); + string menuFontNameBigPostfix = config.getString("FontMenuBigPostfix"); + int menuFontNameBigSize = + computeFontSize(config.getInt("FontMenuBigBaseSize")); + + if (lang.hasString("FontMenuBigPrefix") == true) { + menuFontNameBigPrefix = lang.getString("FontMenuBigPrefix"); + } + if (lang.hasString("FontMenuBigPostfix") == true) { + menuFontNameBigPostfix = lang.getString("FontMenuBigPostfix"); + } + if (lang.hasString("FontMenuBigBaseSize") == true) { + menuFontNameBigSize = + computeFontSize(strToInt(lang.getString("FontMenuBigBaseSize"))); + } + string menuFontNameBig = + menuFontNameBigPrefix + intToStr(menuFontNameBigSize) + + menuFontNameBigPostfix; + + menuFontBig = + loadFont < Font2D >(menuFontBig, menuFontNameBig, + menuFontNameBigSize, "FontMenuBig", + "FontMenuBigFamily", "menuFontBig"); + menuFontBig3D = + loadFont < Font3D >(menuFontBig3D, menuFontNameBig, + menuFontNameBigSize, "FontMenuBig", + "FontMenuBigFamily", "menuFontBig3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] menuFontNameBig = [%s] menuFontNameBigSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + menuFontNameBig.c_str(), + menuFontNameBigSize); + + string menuFontNameVeryBigPrefix = + config.getString("FontMenuBigPrefix"); + string menuFontNameVeryBigPostfix = + config.getString("FontMenuBigPostfix"); + int menuFontNameVeryBigSize = + computeFontSize(config.getInt("FontMenuVeryBigBaseSize")); + + if (lang.hasString("FontMenuBigPrefix") == true) { + menuFontNameVeryBigPrefix = lang.getString("FontMenuBigPrefix"); + } + if (lang.hasString("FontMenuBigPostfix") == true) { + menuFontNameVeryBigPostfix = lang.getString("FontMenuBigPostfix"); + } + if (lang.hasString("FontMenuVeryBigBaseSize") == true) { + menuFontNameVeryBigSize = + computeFontSize(strToInt + (lang.getString("FontMenuVeryBigBaseSize"))); + } + string menuFontNameVeryBig = + menuFontNameVeryBigPrefix + intToStr(menuFontNameVeryBigSize) + + menuFontNameVeryBigPostfix; + + menuFontVeryBig = + loadFont < Font2D >(menuFontVeryBig, menuFontNameVeryBig, + menuFontNameVeryBigSize, "FontMenuVeryBig", + "FontMenuVeryBigFamily", "menuFontVeryBig"); + menuFontVeryBig3D = + loadFont < Font3D >(menuFontVeryBig3D, menuFontNameVeryBig, + menuFontNameVeryBigSize, "FontMenuVeryBig", + "FontMenuVeryBigFamily", "menuFontVeryBig3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] menuFontNameVeryBig = [%s] menuFontNameVeryBigSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + menuFontNameVeryBig.c_str(), + menuFontNameVeryBigSize); + + //console font + string consoleFontNamePrefix = config.getString("FontConsolePrefix"); + string consoleFontNamePostfix = config.getString("FontConsolePostfix"); + int consoleFontNameSize = + computeFontSize(config.getInt("FontConsoleBaseSize")); + + if (lang.hasString("FontConsolePrefix") == true) { + consoleFontNamePrefix = lang.getString("FontConsolePrefix"); + } + if (lang.hasString("FontConsolePostfix") == true) { + consoleFontNamePostfix = lang.getString("FontConsolePostfix"); + } + if (lang.hasString("FontConsoleBaseSize") == true) { + consoleFontNameSize = + computeFontSize(strToInt(lang.getString("FontConsoleBaseSize"))); + } + string consoleFontName = + consoleFontNamePrefix + intToStr(consoleFontNameSize) + + consoleFontNamePostfix; + + consoleFont = + loadFont < Font2D >(consoleFont, consoleFontName, + consoleFontNameSize, "FontConsole", + "FontConsoleFamily", "consoleFont"); + consoleFont3D = + loadFont < Font3D >(consoleFont3D, consoleFontName, + consoleFontNameSize, "FontConsole", + "FontConsoleFamily", "consoleFont3D"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] consoleFontName = [%s] consoleFontNameSize = %d\n", + __FILE__, __FUNCTION__, __LINE__, + consoleFontName.c_str(), + consoleFontNameSize); + } + + + template < typename T > T * CoreData::loadFont(Font * menuFont, + string menuFontName, + int menuFontNameSize, + string fontType, + string fontTypeFamily, + string fontUniqueKey) { + Renderer & renderer = Renderer::getInstance(); + if (menuFont) { + string fontUniqueId = menuFont->getFontUniqueId(); + renderer.endFont(menuFont, rsGlobal); + menuFont = NULL; + triggerFontChangedCallbacks(fontUniqueId, menuFont); + } + if (Renderer::renderText3DEnabled == false) { + menuFont = renderer.newFont(rsGlobal); + } else { + menuFont = renderer.newFont3D(rsGlobal); + } + if (menuFont) { + Config & config = Config::getInstance(); + menuFont->setType(menuFontName, config.getString(fontType, ""), + config.getString(fontTypeFamily, "")); + menuFont->setSize(menuFontNameSize); + menuFont->setWidth(Font::wBold); + menuFont->setFontUniqueId(fontUniqueKey); + triggerFontChangedCallbacks(menuFont->getFontUniqueId(), menuFont); + } + return (T *) menuFont; + } + + int CoreData::computeFontSize(int size) { + int rs = size; + Config & config = Config::getInstance(); + if (Font::forceLegacyFonts == true) { + int screenH = config.getInt("ScreenHeight"); + rs = size * screenH / 1024; + } + //FontSizeAdjustment + rs += config.getInt("FontSizeAdjustment"); + if (Font::forceLegacyFonts == false) { + rs += Font::baseSize; //basesize only for new font system + } + if (Font::forceLegacyFonts == true) { + if (rs < 10) { + rs = 10; + } + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] fontsize original %d calculated:%d \n", + __FILE__, __FUNCTION__, __LINE__, size, rs); + return rs; + } + + void CoreData::saveGameSettingsToFile(std::string fileName, + GameSettings * gameSettings, + int advancedIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + + Config & config = Config::getInstance(); + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + fileName = userData + fileName; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] fileName = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.c_str()); #if defined(WIN32) && !defined(__MINGW32__) - FILE *fp = _wfopen (utf8_decode (fileName).c_str (), L"w"); - std::ofstream saveGameFile (fp); + FILE *fp = _wfopen(utf8_decode(fileName).c_str(), L"w"); + std::ofstream saveGameFile(fp); #else - std::ofstream saveGameFile; - saveGameFile.open (fileName.c_str (), ios_base::out | ios_base::trunc); + std::ofstream saveGameFile; + saveGameFile.open(fileName.c_str(), ios_base::out | ios_base::trunc); #endif - saveGameFile << "Description=" << gameSettings-> - getDescription () << std::endl; - saveGameFile << "MapFilterIndex=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getMapFilter ()) << std::endl; - saveGameFile << "Map=" << gameSettings->getMap () << std::endl; - saveGameFile << "Tileset=" << gameSettings->getTileset () << std::endl; - saveGameFile << "TechTree=" << gameSettings->getTech () << std::endl; - saveGameFile << "DefaultUnits=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getDefaultUnits ()) << std::endl; - saveGameFile << "DefaultResources=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getDefaultResources ()) << std::endl; - saveGameFile << "DefaultVictoryConditions=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getDefaultVictoryConditions ()) << - std::endl; - saveGameFile << "FogOfWar=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getFogOfWar ()) << std::endl; - saveGameFile << "AdvancedIndex=" << Shared::PlatformByteOrder:: - toCommonEndian (advancedIndex) << std::endl; - saveGameFile << "AllowObservers=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getAllowObservers ()) << std::endl; - saveGameFile << "FlagTypes1=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getFlagTypes1 ()) << std::endl; - saveGameFile << "EnableObserverModeAtEndGame=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getEnableObserverModeAtEndGame ()) - << std::endl; - saveGameFile << "AiAcceptSwitchTeamPercentChance=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getAiAcceptSwitchTeamPercentChance - ()) << std::endl; - saveGameFile << "FallbackCpuMultiplier=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings-> - getFallbackCpuMultiplier ()) << std::endl; - saveGameFile << "PathFinderType=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getPathFinderType ()) << std::endl; - saveGameFile << "EnableServerControlledAI=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getEnableServerControlledAI ()) << - std::endl; - saveGameFile << "NetworkFramePeriod=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getNetworkFramePeriod ()) << std::endl; - saveGameFile << "NetworkPauseGameForLaggedClients=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getNetworkPauseGameForLaggedClients - ()) << std::endl; + saveGameFile << "Description=" << gameSettings-> + getDescription() << std::endl; + saveGameFile << "MapFilterIndex=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getMapFilter()) << std::endl; + saveGameFile << "Map=" << gameSettings->getMap() << std::endl; + saveGameFile << "Tileset=" << gameSettings->getTileset() << std::endl; + saveGameFile << "TechTree=" << gameSettings->getTech() << std::endl; + saveGameFile << "DefaultUnits=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getDefaultUnits()) << std::endl; + saveGameFile << "DefaultResources=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getDefaultResources()) << std::endl; + saveGameFile << "DefaultVictoryConditions=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getDefaultVictoryConditions()) << + std::endl; + saveGameFile << "FogOfWar=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getFogOfWar()) << std::endl; + saveGameFile << "AdvancedIndex=" << Shared::PlatformByteOrder:: + toCommonEndian(advancedIndex) << std::endl; + saveGameFile << "AllowObservers=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getAllowObservers()) << std::endl; + saveGameFile << "FlagTypes1=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getFlagTypes1()) << std::endl; + saveGameFile << "EnableObserverModeAtEndGame=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getEnableObserverModeAtEndGame()) + << std::endl; + saveGameFile << "AiAcceptSwitchTeamPercentChance=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getAiAcceptSwitchTeamPercentChance + ()) << std::endl; + saveGameFile << "FallbackCpuMultiplier=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings-> + getFallbackCpuMultiplier()) << std::endl; + saveGameFile << "PathFinderType=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getPathFinderType()) << std::endl; + saveGameFile << "EnableServerControlledAI=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getEnableServerControlledAI()) << + std::endl; + saveGameFile << "NetworkFramePeriod=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getNetworkFramePeriod()) << std::endl; + saveGameFile << "NetworkPauseGameForLaggedClients=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getNetworkPauseGameForLaggedClients + ()) << std::endl; - saveGameFile << "FactionThisFactionIndex=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getThisFactionIndex ()) << std:: - endl; - saveGameFile << "FactionCount=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getFactionCount ()) << std::endl; + saveGameFile << "FactionThisFactionIndex=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getThisFactionIndex()) << std:: + endl; + saveGameFile << "FactionCount=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getFactionCount()) << std::endl; - saveGameFile << "NetworkAllowNativeLanguageTechtree=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getNetworkAllowNativeLanguageTechtree - ()) << std::endl; + saveGameFile << "NetworkAllowNativeLanguageTechtree=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getNetworkAllowNativeLanguageTechtree + ()) << std::endl; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - int slotIndex = gameSettings->getStartLocationIndex (i); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + int slotIndex = gameSettings->getStartLocationIndex(i); - saveGameFile << "FactionControlForIndex" << Shared:: - PlatformByteOrder::toCommonEndian (slotIndex) << "=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getFactionControl (i)) << std:: - endl; - saveGameFile << "ResourceMultiplierIndex" << Shared:: - PlatformByteOrder::toCommonEndian (slotIndex) << "=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getResourceMultiplierIndex (i)) - << std::endl; - saveGameFile << "FactionTeamForIndex" << Shared::PlatformByteOrder:: - toCommonEndian (slotIndex) << "=" << Shared::PlatformByteOrder:: - toCommonEndian (gameSettings->getTeam (i)) << std::endl; - saveGameFile << "FactionStartLocationForIndex" << Shared:: - PlatformByteOrder::toCommonEndian (slotIndex) << "=" << Shared:: - PlatformByteOrder::toCommonEndian (gameSettings-> - getStartLocationIndex (i)) << - std::endl; - saveGameFile << "FactionTypeNameForIndex" << Shared:: - PlatformByteOrder:: - toCommonEndian (slotIndex) << "=" << gameSettings-> - getFactionTypeName (i) << std::endl; - saveGameFile << "FactionPlayerNameForIndex" << Shared:: - PlatformByteOrder:: - toCommonEndian (slotIndex) << "=" << gameSettings-> - getNetworkPlayerName (i) << std::endl; + saveGameFile << "FactionControlForIndex" << Shared:: + PlatformByteOrder::toCommonEndian(slotIndex) << "=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getFactionControl(i)) << std:: + endl; + saveGameFile << "ResourceMultiplierIndex" << Shared:: + PlatformByteOrder::toCommonEndian(slotIndex) << "=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getResourceMultiplierIndex(i)) + << std::endl; + saveGameFile << "FactionTeamForIndex" << Shared::PlatformByteOrder:: + toCommonEndian(slotIndex) << "=" << Shared::PlatformByteOrder:: + toCommonEndian(gameSettings->getTeam(i)) << std::endl; + saveGameFile << "FactionStartLocationForIndex" << Shared:: + PlatformByteOrder::toCommonEndian(slotIndex) << "=" << Shared:: + PlatformByteOrder::toCommonEndian(gameSettings-> + getStartLocationIndex(i)) << + std::endl; + saveGameFile << "FactionTypeNameForIndex" << Shared:: + PlatformByteOrder:: + toCommonEndian(slotIndex) << "=" << gameSettings-> + getFactionTypeName(i) << std::endl; + saveGameFile << "FactionPlayerNameForIndex" << Shared:: + PlatformByteOrder:: + toCommonEndian(slotIndex) << "=" << gameSettings-> + getNetworkPlayerName(i) << std::endl; - saveGameFile << "FactionPlayerUUIDForIndex" << Shared:: - PlatformByteOrder:: - toCommonEndian (slotIndex) << "=" << gameSettings-> - getNetworkPlayerUUID (i) << std::endl; - } + saveGameFile << "FactionPlayerUUIDForIndex" << Shared:: + PlatformByteOrder:: + toCommonEndian(slotIndex) << "=" << gameSettings-> + getNetworkPlayerUUID(i) << std::endl; + } #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - fclose (fp); + if (fp) + fclose(fp); #endif - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + } - bool CoreData::loadGameSettingsFromFile (std::string fileName, - GameSettings * gameSettings) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); + bool CoreData::loadGameSettingsFromFile(std::string fileName, + GameSettings * gameSettings) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); - bool fileWasFound = false; - Config & config = Config::getInstance (); - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - if (fileExists (userData + fileName) == true) - { - fileName = userData + fileName; - fileWasFound = true; - } + bool fileWasFound = false; + Config & config = Config::getInstance(); + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + if (fileExists(userData + fileName) == true) { + fileName = userData + fileName; + fileWasFound = true; + } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] fileName = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.c_str ()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] fileName = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.c_str()); - if (fileExists (fileName) == false) - { - return false; - } + if (fileExists(fileName) == false) { + return false; + } - fileWasFound = true; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] fileName = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.c_str ()); + fileWasFound = true; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] fileName = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.c_str()); - Properties properties; - properties.load (fileName); + Properties properties; + properties.load(fileName); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] fileName = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - fileName.c_str ()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] fileName = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + fileName.c_str()); - gameSettings->setMapFilter (properties.getInt ("MapFilterIndex", "0")); - gameSettings->setDescription (properties.getString ("Description")); - gameSettings->setMap (properties.getString ("Map")); - gameSettings->setTileset (properties.getString ("Tileset")); - gameSettings->setTech (properties.getString ("TechTree")); - gameSettings->setDefaultUnits (properties.getBool ("DefaultUnits")); - gameSettings->setDefaultResources (properties. - getBool ("DefaultResources")); - gameSettings->setDefaultVictoryConditions (properties. - getBool - ("DefaultVictoryConditions")); - gameSettings->setFogOfWar (properties.getBool ("FogOfWar")); - //listBoxAdvanced.setSelectedItemIndex(properties.getInt("AdvancedIndex","0")); + gameSettings->setMapFilter(properties.getInt("MapFilterIndex", "0")); + gameSettings->setDescription(properties.getString("Description")); + gameSettings->setMap(properties.getString("Map")); + gameSettings->setTileset(properties.getString("Tileset")); + gameSettings->setTech(properties.getString("TechTree")); + gameSettings->setDefaultUnits(properties.getBool("DefaultUnits")); + gameSettings->setDefaultResources(properties. + getBool("DefaultResources")); + gameSettings->setDefaultVictoryConditions(properties. + getBool + ("DefaultVictoryConditions")); + gameSettings->setFogOfWar(properties.getBool("FogOfWar")); + //listBoxAdvanced.setSelectedItemIndex(properties.getInt("AdvancedIndex","0")); - gameSettings->setAllowObservers (properties. - getBool ("AllowObservers", "false")); - gameSettings->setFlagTypes1 (properties.getInt ("FlagTypes1", "0")); + gameSettings->setAllowObservers(properties. + getBool("AllowObservers", "false")); + gameSettings->setFlagTypes1(properties.getInt("FlagTypes1", "0")); - uint32 valueFlags1 = gameSettings->getFlagTypes1 (); - if (Config::getInstance (). - getBool ("EnableNetworkGameSynchChecks", "false") == true) - { - //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); + uint32 valueFlags1 = gameSettings->getFlagTypes1(); + if (Config::getInstance(). + getBool("EnableNetworkGameSynchChecks", "false") == true) { + //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); - valueFlags1 |= ft1_network_synch_checks_verbose; - gameSettings->setFlagTypes1 (valueFlags1); + valueFlags1 |= ft1_network_synch_checks_verbose; + gameSettings->setFlagTypes1(valueFlags1); - } - else - { - valueFlags1 &= ~ft1_network_synch_checks_verbose; - gameSettings->setFlagTypes1 (valueFlags1); + } else { + valueFlags1 &= ~ft1_network_synch_checks_verbose; + gameSettings->setFlagTypes1(valueFlags1); - } - if (Config::getInstance (). - getBool ("EnableNetworkGameSynchMonitor", "false") == true) - { - //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); + } + if (Config::getInstance(). + getBool("EnableNetworkGameSynchMonitor", "false") == true) { + //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); - valueFlags1 |= ft1_network_synch_checks; - gameSettings->setFlagTypes1 (valueFlags1); + valueFlags1 |= ft1_network_synch_checks; + gameSettings->setFlagTypes1(valueFlags1); - } - else - { - valueFlags1 &= ~ft1_network_synch_checks; - gameSettings->setFlagTypes1 (valueFlags1); + } else { + valueFlags1 &= ~ft1_network_synch_checks; + gameSettings->setFlagTypes1(valueFlags1); - } + } - gameSettings->setEnableObserverModeAtEndGame (properties. - getBool - ("EnableObserverModeAtEndGame")); - gameSettings->setAiAcceptSwitchTeamPercentChance (properties. - getInt - ("AiAcceptSwitchTeamPercentChance", - "30")); - gameSettings->setFallbackCpuMultiplier (properties. - getInt ("FallbackCpuMultiplier", - "5")); + gameSettings->setEnableObserverModeAtEndGame(properties. + getBool + ("EnableObserverModeAtEndGame")); + gameSettings->setAiAcceptSwitchTeamPercentChance(properties. + getInt + ("AiAcceptSwitchTeamPercentChance", + "30")); + gameSettings->setFallbackCpuMultiplier(properties. + getInt("FallbackCpuMultiplier", + "5")); - gameSettings->setPathFinderType (static_cast < PathFinderType > - (properties. - getInt ("PathFinderType", - intToStr (pfBasic). - c_str ()))); - gameSettings->setEnableServerControlledAI (properties. - getBool - ("EnableServerControlledAI", - "true")); - gameSettings->setNetworkFramePeriod (properties. - getInt ("NetworkFramePeriod", - intToStr (GameConstants:: - networkFramePeriod). - c_str ())); - gameSettings->setNetworkPauseGameForLaggedClients (properties. - getBool - ("NetworkPauseGameForLaggedClients", - "false")); + gameSettings->setPathFinderType(static_cast + (properties. + getInt("PathFinderType", + intToStr(pfBasic). + c_str()))); + gameSettings->setEnableServerControlledAI(properties. + getBool + ("EnableServerControlledAI", + "true")); + gameSettings->setNetworkFramePeriod(properties. + getInt("NetworkFramePeriod", + intToStr(GameConstants:: + networkFramePeriod). + c_str())); + gameSettings->setNetworkPauseGameForLaggedClients(properties. + getBool + ("NetworkPauseGameForLaggedClients", + "false")); - gameSettings->setThisFactionIndex (properties. - getInt ("FactionThisFactionIndex")); - gameSettings->setFactionCount (properties.getInt ("FactionCount")); + gameSettings->setThisFactionIndex(properties. + getInt("FactionThisFactionIndex")); + gameSettings->setFactionCount(properties.getInt("FactionCount")); - if (properties.hasString ("NetworkAllowNativeLanguageTechtree") == true) - { - gameSettings->setNetworkAllowNativeLanguageTechtree (properties. - getBool - ("NetworkAllowNativeLanguageTechtree")); - } - else - { - gameSettings->setNetworkAllowNativeLanguageTechtree (false); - } + if (properties.hasString("NetworkAllowNativeLanguageTechtree") == true) { + gameSettings->setNetworkAllowNativeLanguageTechtree(properties. + getBool + ("NetworkAllowNativeLanguageTechtree")); + } else { + gameSettings->setNetworkAllowNativeLanguageTechtree(false); + } - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - gameSettings->setFactionControl (i, - (ControlType) properties. - getInt (string - ("FactionControlForIndex") + - intToStr (i), - intToStr (ctClosed). - c_str ())); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + gameSettings->setFactionControl(i, + (ControlType) properties. + getInt(string + ("FactionControlForIndex") + + intToStr(i), + intToStr(ctClosed). + c_str())); - if (gameSettings->getFactionControl (i) == ctNetworkUnassigned) - { - gameSettings->setFactionControl (i, ctNetwork); - } + if (gameSettings->getFactionControl(i) == ctNetworkUnassigned) { + gameSettings->setFactionControl(i, ctNetwork); + } - gameSettings->setResourceMultiplierIndex (i, - properties. - getInt (string - ("ResourceMultiplierIndex") - + intToStr (i), - "5")); - gameSettings->setTeam (i, - properties. - getInt (string ("FactionTeamForIndex") + - intToStr (i), "0")); - gameSettings->setStartLocationIndex (i, - properties. - getInt (string - ("FactionStartLocationForIndex") - + intToStr (i), - intToStr (i).c_str ())); - gameSettings->setFactionTypeName (i, - properties. - getString (string - ("FactionTypeNameForIndex") - + intToStr (i), "?")); + gameSettings->setResourceMultiplierIndex(i, + properties. + getInt(string + ("ResourceMultiplierIndex") + + intToStr(i), + "5")); + gameSettings->setTeam(i, + properties. + getInt(string("FactionTeamForIndex") + + intToStr(i), "0")); + gameSettings->setStartLocationIndex(i, + properties. + getInt(string + ("FactionStartLocationForIndex") + + intToStr(i), + intToStr(i).c_str())); + gameSettings->setFactionTypeName(i, + properties. + getString(string + ("FactionTypeNameForIndex") + + intToStr(i), "?")); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, factionTypeName [%s]\n", - __FILE__, __FUNCTION__, __LINE__, i, - gameSettings->getFactionTypeName (i). - c_str ()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, factionTypeName [%s]\n", + __FILE__, __FUNCTION__, __LINE__, i, + gameSettings->getFactionTypeName(i). + c_str()); - if (gameSettings->getFactionControl (i) == ctHuman) - { - gameSettings->setNetworkPlayerName (i, - properties. - getString (string - ("FactionPlayerNameForIndex") - + intToStr (i), "")); - } - else - { - gameSettings->setNetworkPlayerName (i, ""); - } + if (gameSettings->getFactionControl(i) == ctHuman) { + gameSettings->setNetworkPlayerName(i, + properties. + getString(string + ("FactionPlayerNameForIndex") + + intToStr(i), "")); + } else { + gameSettings->setNetworkPlayerName(i, ""); + } - gameSettings->setNetworkPlayerUUID (i, - properties. - getString (string - ("FactionPlayerUUIDForIndex") - + intToStr (i), "")); - } + gameSettings->setNetworkPlayerUUID(i, + properties. + getString(string + ("FactionPlayerUUIDForIndex") + + intToStr(i), "")); + } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); - return fileWasFound; - } + return fileWasFound; + } - PlaySoundClip::PlaySoundClip (void) {}; + PlaySoundClip::PlaySoundClip(void) { + }; - PlaySoundClip::~PlaySoundClip (void) {}; + PlaySoundClip::~PlaySoundClip(void) { + }; - StaticSound *PlaySoundClip::getSound (const std::string& iniPlaySoundVal) - { - CoreData coreData; - int loadAttemptLookupKey = coreData.tsyst_COUNT + 6; - if (coreData.itemLoadAttempted.find (loadAttemptLookupKey) == - coreData.itemLoadAttempted.end ()) - { + StaticSound *PlaySoundClip::getSound(const std::string& iniPlaySoundVal) { + CoreData coreData; + int loadAttemptLookupKey = coreData.tsyst_COUNT + 6; + if (coreData.itemLoadAttempted.find(loadAttemptLookupKey) == + coreData.itemLoadAttempted.end()) { - coreData.itemLoadAttempted[loadAttemptLookupKey] = true; + coreData.itemLoadAttempted[loadAttemptLookupKey] = true; - try - { - static Config & config = Config::getInstance (); - iniPlaySound.load (config.getString (iniPlaySoundVal, "")); - } - catch (const megaglest_runtime_error & ex) - { - message (ex.what (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } - } + try { + static Config & config = Config::getInstance(); + iniPlaySound.load(config.getString(iniPlaySoundVal, "")); + } catch (const megaglest_runtime_error & ex) { + message(ex.what(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } + } - return &iniPlaySound; - } + return &iniPlaySound; + } -// ================== PRIVATE ======================== + // ================== PRIVATE ======================== - } + } } //end namespace diff --git a/source/glest_game/global/core_data.h b/source/glest_game/global/core_data.h index 6865f0d4b..57fea5d8b 100644 --- a/source/glest_game/global/core_data.h +++ b/source/glest_game/global/core_data.h @@ -33,347 +33,324 @@ # include "sound_container.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using::Shared::Graphics::Texture2D; - using::Shared::Graphics::Texture3D; - using::Shared::Graphics::Font; - using::Shared::Graphics::Font2D; - using::Shared::Graphics::Font3D; - using::Shared::Graphics::FontChangedCallbackInterface; - using::Shared::Sound::StrSound; - using::Shared::Sound::StaticSound; + using::Shared::Graphics::Texture2D; + using::Shared::Graphics::Texture3D; + using::Shared::Graphics::Font; + using::Shared::Graphics::Font2D; + using::Shared::Graphics::Font3D; + using::Shared::Graphics::FontChangedCallbackInterface; + using::Shared::Sound::StrSound; + using::Shared::Sound::StaticSound; -// ===================================================== -// class CoreData -// -/// Data shared among all the ProgramStates -// ===================================================== + // ===================================================== + // class CoreData + // + /// Data shared among all the ProgramStates + // ===================================================== - class GameSettings; + class GameSettings; - class CoreData - { - friend class PlaySoundClip; - private: - std::map < int, bool > itemLoadAttempted; + class CoreData { + friend class PlaySoundClip; + private: + std::map < int, bool > itemLoadAttempted; - StrSound introMusic; - StrSound menuMusic; - SoundContainer waterSounds; + StrSound introMusic; + StrSound menuMusic; + SoundContainer waterSounds; - Texture2D *logoTexture; - std::vector < Texture2D * >logoTextureList; - Texture2D *backgroundTexture; - Texture2D *fireTexture; - Texture2D *teamColorTexture; - Texture2D *snowTexture; - Texture2D *waterSplashTexture; - Texture2D *customTexture; - Texture2D *buttonSmallTexture; - Texture2D *buttonBigTexture; - Texture2D *horizontalLineTexture; - Texture2D *verticalLineTexture; - Texture2D *checkBoxTexture; - Texture2D *checkedCheckBoxTexture; - Texture2D *gameWinnerTexture; - Texture2D *notOnServerTexture; - Texture2D *onServerDifferentTexture; - Texture2D *onServerTexture; - Texture2D *onServerInstalledTexture; - Texture2D *statusReadyTexture; - Texture2D *statusNotReadyTexture; - Texture2D *statusBRBTexture; + Texture2D *logoTexture; + std::vector < Texture2D * >logoTextureList; + Texture2D *backgroundTexture; + Texture2D *fireTexture; + Texture2D *teamColorTexture; + Texture2D *snowTexture; + Texture2D *waterSplashTexture; + Texture2D *customTexture; + Texture2D *buttonSmallTexture; + Texture2D *buttonBigTexture; + Texture2D *horizontalLineTexture; + Texture2D *verticalLineTexture; + Texture2D *checkBoxTexture; + Texture2D *checkedCheckBoxTexture; + Texture2D *gameWinnerTexture; + Texture2D *notOnServerTexture; + Texture2D *onServerDifferentTexture; + Texture2D *onServerTexture; + Texture2D *onServerInstalledTexture; + Texture2D *statusReadyTexture; + Texture2D *statusNotReadyTexture; + Texture2D *statusBRBTexture; - Texture2D *healthbarTexture; - Texture2D *healthbarBackgroundTexture; + Texture2D *healthbarTexture; + Texture2D *healthbarBackgroundTexture; - std::vector < Texture2D * >miscTextureList; + std::vector < Texture2D * >miscTextureList; - Font2D *displayFont; - Font2D *menuFontNormal; - Font2D *displayFontSmall; - Font2D *menuFontBig; - Font2D *menuFontVeryBig; - Font2D *consoleFont; + Font2D *displayFont; + Font2D *menuFontNormal; + Font2D *displayFontSmall; + Font2D *menuFontBig; + Font2D *menuFontVeryBig; + Font2D *consoleFont; - Font3D *displayFont3D; - Font3D *menuFontNormal3D; - Font3D *displayFontSmall3D; - Font3D *menuFontBig3D; - Font3D *menuFontVeryBig3D; - Font3D *consoleFont3D; + Font3D *displayFont3D; + Font3D *menuFontNormal3D; + Font3D *displayFontSmall3D; + Font3D *menuFontBig3D; + Font3D *menuFontVeryBig3D; + Font3D *consoleFont3D; - string introVideoFilename; - string introVideoFilenameFallback; + string introVideoFilename; + string introVideoFilenameFallback; - string mainMenuVideoFilename; - string mainMenuVideoFilenameFallback; + string mainMenuVideoFilename; + string mainMenuVideoFilenameFallback; - string battleEndWinVideoFilename; - string battleEndWinVideoFilenameFallback; - string battleEndWinMusicFilename; + string battleEndWinVideoFilename; + string battleEndWinVideoFilenameFallback; + string battleEndWinMusicFilename; - string battleEndLoseVideoFilename; - string battleEndLoseVideoFilenameFallback; - string battleEndLoseMusicFilename; + string battleEndLoseVideoFilename; + string battleEndLoseVideoFilenameFallback; + string battleEndLoseMusicFilename; - std::map < std::string, - std::vector < - FontChangedCallbackInterface * > >registeredFontChangedCallbacks; - public: + std::map < std::string, + std::vector < + FontChangedCallbackInterface * > >registeredFontChangedCallbacks; + public: - enum TextureSystemType - { - tsyst_NONE, - tsyst_logoTexture, - tsyst_backgroundTexture, - tsyst_fireTexture, - tsyst_teamColorTexture, - tsyst_snowTexture, - tsyst_waterSplashTexture, - tsyst_customTexture, - tsyst_buttonSmallTexture, - tsyst_buttonBigTexture, - tsyst_horizontalLineTexture, - tsyst_verticalLineTexture, - tsyst_checkBoxTexture, - tsyst_checkedCheckBoxTexture, - tsyst_gameWinnerTexture, - tsyst_notOnServerTexture, - tsyst_onServerDifferentTexture, - tsyst_onServerTexture, - tsyst_onServerInstalledTexture, - tsyst_statusReadyTexture, - tsyst_statusNotReadyTexture, - tsyst_statusBRBTexture, - tsyst_healthbarTexture, - tsyst_healthbarBackgroundTexture, + enum TextureSystemType { + tsyst_NONE, + tsyst_logoTexture, + tsyst_backgroundTexture, + tsyst_fireTexture, + tsyst_teamColorTexture, + tsyst_snowTexture, + tsyst_waterSplashTexture, + tsyst_customTexture, + tsyst_buttonSmallTexture, + tsyst_buttonBigTexture, + tsyst_horizontalLineTexture, + tsyst_verticalLineTexture, + tsyst_checkBoxTexture, + tsyst_checkedCheckBoxTexture, + tsyst_gameWinnerTexture, + tsyst_notOnServerTexture, + tsyst_onServerDifferentTexture, + tsyst_onServerTexture, + tsyst_onServerInstalledTexture, + tsyst_statusReadyTexture, + tsyst_statusNotReadyTexture, + tsyst_statusBRBTexture, + tsyst_healthbarTexture, + tsyst_healthbarBackgroundTexture, - tsyst_COUNT - }; + tsyst_COUNT + }; - public: + public: - ~CoreData (); - static CoreData & getInstance (); + ~CoreData(); + static CoreData & getInstance(); - void load (); - void cleanup (); - void loadFonts (); + void load(); + void cleanup(); + void loadFonts(); - // Textures - Texture2D *getTextureBySystemId (TextureSystemType type); + // Textures + Texture2D *getTextureBySystemId(TextureSystemType type); - Texture2D *getBackgroundTexture (); - Texture2D *getFireTexture (); - Texture2D *getTeamColorTexture (); - Texture2D *getSnowTexture (); - Texture2D *getLogoTexture (); - Texture2D *getWaterSplashTexture (); - Texture2D *getCustomTexture (); - Texture2D *getButtonSmallTexture (); - Texture2D *getButtonBigTexture (); - Texture2D *getHorizontalLineTexture (); - Texture2D *getVerticalLineTexture (); - Texture2D *getCheckBoxTexture (); - Texture2D *getCheckedCheckBoxTexture (); - Texture2D *getNotOnServerTexture (); - Texture2D *getOnServerDifferentTexture (); - Texture2D *getOnServerTexture (); - Texture2D *getOnServerInstalledTexture (); - Texture2D *getStatusReadyTexture (); - Texture2D *getStatusNotReadyTexture (); - Texture2D *getStatusBRBTexture (); - Texture2D *getGameWinnerTexture (); + Texture2D *getBackgroundTexture(); + Texture2D *getFireTexture(); + Texture2D *getTeamColorTexture(); + Texture2D *getSnowTexture(); + Texture2D *getLogoTexture(); + Texture2D *getWaterSplashTexture(); + Texture2D *getCustomTexture(); + Texture2D *getButtonSmallTexture(); + Texture2D *getButtonBigTexture(); + Texture2D *getHorizontalLineTexture(); + Texture2D *getVerticalLineTexture(); + Texture2D *getCheckBoxTexture(); + Texture2D *getCheckedCheckBoxTexture(); + Texture2D *getNotOnServerTexture(); + Texture2D *getOnServerDifferentTexture(); + Texture2D *getOnServerTexture(); + Texture2D *getOnServerInstalledTexture(); + Texture2D *getStatusReadyTexture(); + Texture2D *getStatusNotReadyTexture(); + Texture2D *getStatusBRBTexture(); + Texture2D *getGameWinnerTexture(); - Texture2D *getHealthbarTexture (); - Texture2D *getHealthbarBackgroundTexture (); + Texture2D *getHealthbarTexture(); + Texture2D *getHealthbarBackgroundTexture(); - size_t getLogoTextureExtraCount (); - Texture2D *getLogoTextureExtra (int idx); + size_t getLogoTextureExtraCount(); + Texture2D *getLogoTextureExtra(int idx); - std::vector < Texture2D * >&getMiscTextureList (); + std::vector < Texture2D * >&getMiscTextureList(); - // Sounds and Music - StrSound *getIntroMusic (); - StrSound *getMenuMusic (); + // Sounds and Music + StrSound *getIntroMusic(); + StrSound *getMenuMusic(); - // When issue #63 was done - // - // and the PlaySoundFile class was created, - // these functions were kept because they were being called from many - // different places in the code base. Rather than trying to change - // all those locations, I made these three into "wrapper" functions. - // -andy5995 2018-02-22 - StaticSound *getClickSoundA (); - StaticSound *getClickSoundB (); - StaticSound *getClickSoundC (); + // When issue #63 was done + // + // and the PlaySoundFile class was created, + // these functions were kept because they were being called from many + // different places in the code base. Rather than trying to change + // all those locations, I made these three into "wrapper" functions. + // -andy5995 2018-02-22 + StaticSound *getClickSoundA(); + StaticSound *getClickSoundB(); + StaticSound *getClickSoundC(); - StaticSound *getWaterSound (); + StaticSound *getWaterSound(); - // Fonts - Font2D *getDisplayFont () const - { - return displayFont; - } - Font2D *getDisplayFontSmall () const - { - return displayFontSmall; - } - Font2D *getMenuFontNormal () const - { - return menuFontNormal; - } - Font2D *getMenuFontBig () const - { - return menuFontBig; - } - Font2D *getMenuFontVeryBig () const - { - return menuFontVeryBig; - } - Font2D *getConsoleFont () const - { - return consoleFont; - } + // Fonts + Font2D *getDisplayFont() const { + return displayFont; + } + Font2D *getDisplayFontSmall() const { + return displayFontSmall; + } + Font2D *getMenuFontNormal() const { + return menuFontNormal; + } + Font2D *getMenuFontBig() const { + return menuFontBig; + } + Font2D *getMenuFontVeryBig() const { + return menuFontVeryBig; + } + Font2D *getConsoleFont() const { + return consoleFont; + } - Font3D *getDisplayFont3D () const - { - return displayFont3D; - } - Font3D *getDisplayFontSmall3D () const - { - return displayFontSmall3D; - } - Font3D *getMenuFontNormal3D () const - { - return menuFontNormal3D; - } - Font3D *getMenuFontBig3D () const - { - return menuFontBig3D; - } - Font3D *getMenuFontVeryBig3D () const - { - return menuFontVeryBig3D; - } - Font3D *getConsoleFont3D () const - { - return consoleFont3D; - } + Font3D *getDisplayFont3D() const { + return displayFont3D; + } + Font3D *getDisplayFontSmall3D() const { + return displayFontSmall3D; + } + Font3D *getMenuFontNormal3D() const { + return menuFontNormal3D; + } + Font3D *getMenuFontBig3D() const { + return menuFontBig3D; + } + Font3D *getMenuFontVeryBig3D() const { + return menuFontVeryBig3D; + } + Font3D *getConsoleFont3D() const { + return consoleFont3D; + } - // Helper functions - string getMainMenuVideoFilename () const - { - return mainMenuVideoFilename; - } - bool hasMainMenuVideoFilename () const; - string getMainMenuVideoFilenameFallback () const - { - return mainMenuVideoFilenameFallback; - } - bool hasMainMenuVideoFilenameFallback () const; + // Helper functions + string getMainMenuVideoFilename() const { + return mainMenuVideoFilename; + } + bool hasMainMenuVideoFilename() const; + string getMainMenuVideoFilenameFallback() const { + return mainMenuVideoFilenameFallback; + } + bool hasMainMenuVideoFilenameFallback() const; - string getIntroVideoFilename () const - { - return introVideoFilename; - } - bool hasIntroVideoFilename () const; - string getIntroVideoFilenameFallback () const - { - return introVideoFilenameFallback; - } - bool hasIntroVideoFilenameFallback () const; + string getIntroVideoFilename() const { + return introVideoFilename; + } + bool hasIntroVideoFilename() const; + string getIntroVideoFilenameFallback() const { + return introVideoFilenameFallback; + } + bool hasIntroVideoFilenameFallback() const; - string getBattleEndVideoFilename (bool won) const - { - return won == - true ? battleEndWinVideoFilename : battleEndLoseVideoFilename; - } - //bool hasBattleEndVideoFilename(bool won) const; - string getBattleEndVideoFilenameFallback (bool won) const - { - return won == - true ? battleEndWinVideoFilenameFallback : - battleEndLoseVideoFilenameFallback; - } - bool hasBattleEndVideoFilenameFallback (bool won) const; + string getBattleEndVideoFilename(bool won) const { + return won == + true ? battleEndWinVideoFilename : battleEndLoseVideoFilename; + } + //bool hasBattleEndVideoFilename(bool won) const; + string getBattleEndVideoFilenameFallback(bool won) const { + return won == + true ? battleEndWinVideoFilenameFallback : + battleEndLoseVideoFilenameFallback; + } + bool hasBattleEndVideoFilenameFallback(bool won) const; - string getBattleEndMusicFilename (bool won) const - { - return won == - true ? battleEndWinMusicFilename : battleEndLoseMusicFilename; - } + string getBattleEndMusicFilename(bool won) const { + return won == + true ? battleEndWinMusicFilename : battleEndLoseMusicFilename; + } - void saveGameSettingsToFile (std::string fileName, - GameSettings * gameSettings, - int advancedIndex = 0); - bool loadGameSettingsFromFile (std::string fileName, - GameSettings * gameSettings); + void saveGameSettingsToFile(std::string fileName, + GameSettings * gameSettings, + int advancedIndex = 0); + bool loadGameSettingsFromFile(std::string fileName, + GameSettings * gameSettings); - void registerFontChangedCallback (std::string entityName, - FontChangedCallbackInterface * cb); - void unRegisterFontChangedCallback (std::string entityName); - private: + void registerFontChangedCallback(std::string entityName, + FontChangedCallbackInterface * cb); + void unRegisterFontChangedCallback(std::string entityName); + private: - CoreData (); + CoreData(); - int computeFontSize (int size); - void cleanupTexture (Texture2D ** texture); - void loadTextures (string data_path); - void loadSounds (string data_path); - void loadMusic (string data_path); - void loadIntroMedia (string data_path); - void loadMainMenuMedia (string data_path); - void loadBattleEndMedia (string data_path); + int computeFontSize(int size); + void cleanupTexture(Texture2D ** texture); + void loadTextures(string data_path); + void loadSounds(string data_path); + void loadMusic(string data_path); + void loadIntroMedia(string data_path); + void loadMainMenuMedia(string data_path); + void loadBattleEndMedia(string data_path); - string getDataPath (); - void loadTextureIfRequired (Texture2D ** tex, string data_path, - string uniqueFilePath, int texSystemId, - bool setMipMap, bool setAlpha, - bool loadUniqueFilePath, - bool compressionDisabled = false); + string getDataPath(); + void loadTextureIfRequired(Texture2D ** tex, string data_path, + string uniqueFilePath, int texSystemId, + bool setMipMap, bool setAlpha, + bool loadUniqueFilePath, + bool compressionDisabled = false); - void loadLogoTextureExtraIfRequired (); - void loadMiscTextureListIfRequired (); + void loadLogoTextureExtraIfRequired(); + void loadMiscTextureListIfRequired(); - void loadWaterSoundsIfRequired (); - void loadMusicIfRequired (); + void loadWaterSoundsIfRequired(); + void loadMusicIfRequired(); - void triggerFontChangedCallbacks (std::string fontUniqueId, - Font * font); - template < typename T > T * loadFont (Font * menuFontNormal, - string menuFontNameNormal, - int menuFontNameNormalSize, - string fontType, - string fontTypeFamily, - string fontUniqueKey); + void triggerFontChangedCallbacks(std::string fontUniqueId, + Font * font); + template < typename T > T * loadFont(Font * menuFontNormal, + string menuFontNameNormal, + int menuFontNameNormalSize, + string fontType, + string fontTypeFamily, + string fontUniqueKey); - }; + }; - class PlaySoundClip - { - private: - StaticSound iniPlaySound; - public: - StaticSound *getSound (const std::string& iniPlaySoundVal); - static const string sfxAttention; - static const string sfxHighlight; - static const string sfxNewServer; - static const string sfxMarker; - static const string sfxMenuClickA; - static const string sfxMenuClickB; - static const string sfxMenuClickC; - PlaySoundClip(); - ~PlaySoundClip(); + class PlaySoundClip { + private: + StaticSound iniPlaySound; + public: + StaticSound * getSound(const std::string& iniPlaySoundVal); + static const string sfxAttention; + static const string sfxHighlight; + static const string sfxNewServer; + static const string sfxMarker; + static const string sfxMenuClickA; + static const string sfxMenuClickB; + static const string sfxMenuClickC; + PlaySoundClip(); + ~PlaySoundClip(); - }; -}} //end namespace + }; + } +} //end namespace #endif diff --git a/source/glest_game/global/lang.cpp b/source/glest_game/global/lang.cpp index c90dc4dec..ea2ef291a 100644 --- a/source/glest_game/global/lang.cpp +++ b/source/glest_game/global/lang.cpp @@ -32,1048 +32,879 @@ using namespace std; using namespace Shared::Util; using namespace Shared::Platform; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - const char *DEFAULT_LANGUAGE = "english"; + const char *DEFAULT_LANGUAGE = "english"; -// ===================================================== -// class Lang -// ===================================================== + // ===================================================== + // class Lang + // ===================================================== - Lang::Lang () - { - language = ""; - is_utf8_language = false; - allowNativeLanguageTechtree = true; - techNameLoaded = ""; - } + Lang::Lang() { + language = ""; + is_utf8_language = false; + allowNativeLanguageTechtree = true; + techNameLoaded = ""; + } - Lang & Lang::getInstance () - { - static Lang lang; - return lang; - } + Lang & Lang::getInstance() { + static Lang lang; + return lang; + } - string Lang::getDefaultLanguage () const - { - return DEFAULT_LANGUAGE; - } + string Lang::getDefaultLanguage() const { + return DEFAULT_LANGUAGE; + } - void Lang::loadGameStrings (string uselanguage, bool loadFonts, - bool fallbackToDefault) - { - if (uselanguage.length () == 2 - || (uselanguage.length () == 5 && uselanguage[2] == '-')) - { - uselanguage = getLanguageFile (uselanguage); - } - bool languageChanged = (uselanguage != this->language); - this->language = uselanguage; - loadGameStringProperties (uselanguage, - gameStringsAllLanguages[this->language], true, - fallbackToDefault); + void Lang::loadGameStrings(string uselanguage, bool loadFonts, + bool fallbackToDefault) { + if (uselanguage.length() == 2 + || (uselanguage.length() == 5 && uselanguage[2] == '-')) { + uselanguage = getLanguageFile(uselanguage); + } + bool languageChanged = (uselanguage != this->language); + this->language = uselanguage; + loadGameStringProperties(uselanguage, + gameStringsAllLanguages[this->language], true, + fallbackToDefault); - if (languageChanged == true) - { - Font::resetToDefaults (); - Lang & lang = Lang::getInstance (); - if (lang.hasString ("FONT_BASE_SIZE")) - { - Font::baseSize = strToInt (lang.getString ("FONT_BASE_SIZE")); - } + if (languageChanged == true) { + Font::resetToDefaults(); + Lang & lang = Lang::getInstance(); + if (lang.hasString("FONT_BASE_SIZE")) { + Font::baseSize = strToInt(lang.getString("FONT_BASE_SIZE")); + } - if (lang.hasString ("FONT_SCALE_SIZE")) - { - Font::scaleFontValue = - strToFloat (lang.getString ("FONT_SCALE_SIZE")); - } - if (lang.hasString ("FONT_SCALE_CENTERH_FACTOR")) - { - Font::scaleFontValueCenterHFactor = - strToFloat (lang.getString ("FONT_SCALE_CENTERH_FACTOR")); - } + if (lang.hasString("FONT_SCALE_SIZE")) { + Font::scaleFontValue = + strToFloat(lang.getString("FONT_SCALE_SIZE")); + } + if (lang.hasString("FONT_SCALE_CENTERH_FACTOR")) { + Font::scaleFontValueCenterHFactor = + strToFloat(lang.getString("FONT_SCALE_CENTERH_FACTOR")); + } - if (lang.hasString ("FONT_CHARCOUNT")) - { - // 256 for English - // 30000 for Chinese - Font::charCount = strToInt (lang.getString ("FONT_CHARCOUNT")); - } - if (lang.hasString ("FONT_TYPENAME")) - { - Font::fontTypeName = lang.getString ("FONT_TYPENAME"); - } - if (lang.hasString ("FONT_CHARSET")) - { - // Example values: - // DEFAULT_CHARSET (English) = 1 - // GB2312_CHARSET (Chinese) = 134 - Shared::Platform::PlatformContextGl::charSet = - strToInt (lang.getString ("FONT_CHARSET")); - } - if (lang.hasString ("FONT_MULTIBYTE")) - { - Font::fontIsMultibyte = - strToBool (lang.getString ("FONT_MULTIBYTE")); - } + if (lang.hasString("FONT_CHARCOUNT")) { + // 256 for English + // 30000 for Chinese + Font::charCount = strToInt(lang.getString("FONT_CHARCOUNT")); + } + if (lang.hasString("FONT_TYPENAME")) { + Font::fontTypeName = lang.getString("FONT_TYPENAME"); + } + if (lang.hasString("FONT_CHARSET")) { + // Example values: + // DEFAULT_CHARSET (English) = 1 + // GB2312_CHARSET (Chinese) = 134 + Shared::Platform::PlatformContextGl::charSet = + strToInt(lang.getString("FONT_CHARSET")); + } + if (lang.hasString("FONT_MULTIBYTE")) { + Font::fontIsMultibyte = + strToBool(lang.getString("FONT_MULTIBYTE")); + } - if (lang.hasString ("FONT_RIGHTTOLEFT")) - { - Font::fontIsRightToLeft = - strToBool (lang.getString ("FONT_RIGHTTOLEFT")); - } + if (lang.hasString("FONT_RIGHTTOLEFT")) { + Font::fontIsRightToLeft = + strToBool(lang.getString("FONT_RIGHTTOLEFT")); + } - if (lang.hasString ("FONT_RIGHTTOLEFT_MIXED_SUPPORT")) - { - Font::fontSupportMixedRightToLeft = - strToBool (lang.getString ("FONT_RIGHTTOLEFT_MIXED_SUPPORT")); - } + if (lang.hasString("FONT_RIGHTTOLEFT_MIXED_SUPPORT")) { + Font::fontSupportMixedRightToLeft = + strToBool(lang.getString("FONT_RIGHTTOLEFT_MIXED_SUPPORT")); + } - if (lang.hasString ("MEGAGLEST_FONT")) - { - //setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese + if (lang.hasString("MEGAGLEST_FONT")) { + //setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese #if defined(WIN32) - string newEnvValue = - "MEGAGLEST_FONT=" + lang.getString ("MEGAGLEST_FONT"); - _putenv (newEnvValue.c_str ()); + string newEnvValue = + "MEGAGLEST_FONT=" + lang.getString("MEGAGLEST_FONT"); + _putenv(newEnvValue.c_str()); #else - setenv ("MEGAGLEST_FONT", - lang.getString ("MEGAGLEST_FONT").c_str (), 0); + setenv("MEGAGLEST_FONT", + lang.getString("MEGAGLEST_FONT").c_str(), 0); #endif - } + } - if (lang.hasString ("MEGAGLEST_FONT_FAMILY")) - { + if (lang.hasString("MEGAGLEST_FONT_FAMILY")) { #if defined(WIN32) - string newEnvValue = - "MEGAGLEST_FONT_FAMILY=" + - lang.getString ("MEGAGLEST_FONT_FAMILY"); - _putenv (newEnvValue.c_str ()); + string newEnvValue = + "MEGAGLEST_FONT_FAMILY=" + + lang.getString("MEGAGLEST_FONT_FAMILY"); + _putenv(newEnvValue.c_str()); #else - setenv ("MEGAGLEST_FONT_FAMILY", - lang.getString ("MEGAGLEST_FONT_FAMILY").c_str (), 0); + setenv("MEGAGLEST_FONT_FAMILY", + lang.getString("MEGAGLEST_FONT_FAMILY").c_str(), 0); #endif - } + } #if defined(WIN32) - // Win32 overrides for fonts (just in case they must be different) + // Win32 overrides for fonts (just in case they must be different) - if (lang.hasString ("FONT_BASE_SIZE_WINDOWS")) - { - // 256 for English - // 30000 for Chinese - Font::baseSize = - strToInt (lang.getString ("FONT_BASE_SIZE_WINDOWS")); - } + if (lang.hasString("FONT_BASE_SIZE_WINDOWS")) { + // 256 for English + // 30000 for Chinese + Font::baseSize = + strToInt(lang.getString("FONT_BASE_SIZE_WINDOWS")); + } - if (lang.hasString ("FONT_SCALE_SIZE_WINDOWS")) - { - Font::scaleFontValue = - strToFloat (lang.getString ("FONT_SCALE_SIZE_WINDOWS")); - } - if (lang.hasString ("FONT_SCALE_CENTERH_FACTOR_WINDOWS")) - { - Font::scaleFontValueCenterHFactor = - strToFloat (lang.getString ("FONT_SCALE_CENTERH_FACTOR_WINDOWS")); - } + if (lang.hasString("FONT_SCALE_SIZE_WINDOWS")) { + Font::scaleFontValue = + strToFloat(lang.getString("FONT_SCALE_SIZE_WINDOWS")); + } + if (lang.hasString("FONT_SCALE_CENTERH_FACTOR_WINDOWS")) { + Font::scaleFontValueCenterHFactor = + strToFloat(lang.getString("FONT_SCALE_CENTERH_FACTOR_WINDOWS")); + } - if (lang.hasString ("FONT_HEIGHT_TEXT_WINDOWS")) - { - Font::langHeightText = - lang.getString ("FONT_HEIGHT_TEXT_WINDOWS", - Font::langHeightText.c_str ()); - } + if (lang.hasString("FONT_HEIGHT_TEXT_WINDOWS")) { + Font::langHeightText = + lang.getString("FONT_HEIGHT_TEXT_WINDOWS", + Font::langHeightText.c_str()); + } - if (lang.hasString ("FONT_CHARCOUNT_WINDOWS")) - { - // 256 for English - // 30000 for Chinese - Font::charCount = - strToInt (lang.getString ("FONT_CHARCOUNT_WINDOWS")); - } - if (lang.hasString ("FONT_TYPENAME_WINDOWS")) - { - Font::fontTypeName = lang.getString ("FONT_TYPENAME_WINDOWS"); - } - if (lang.hasString ("FONT_CHARSET_WINDOWS")) - { - // Example values: - // DEFAULT_CHARSET (English) = 1 - // GB2312_CHARSET (Chinese) = 134 - Shared::Platform::PlatformContextGl::charSet = - strToInt (lang.getString ("FONT_CHARSET_WINDOWS")); - } - if (lang.hasString ("FONT_MULTIBYTE_WINDOWS")) - { - Font::fontIsMultibyte = - strToBool (lang.getString ("FONT_MULTIBYTE_WINDOWS")); - } - if (lang.hasString ("FONT_RIGHTTOLEFT_WINDOWS")) - { - Font::fontIsRightToLeft = - strToBool (lang.getString ("FONT_RIGHTTOLEFT_WINDOWS")); - } + if (lang.hasString("FONT_CHARCOUNT_WINDOWS")) { + // 256 for English + // 30000 for Chinese + Font::charCount = + strToInt(lang.getString("FONT_CHARCOUNT_WINDOWS")); + } + if (lang.hasString("FONT_TYPENAME_WINDOWS")) { + Font::fontTypeName = lang.getString("FONT_TYPENAME_WINDOWS"); + } + if (lang.hasString("FONT_CHARSET_WINDOWS")) { + // Example values: + // DEFAULT_CHARSET (English) = 1 + // GB2312_CHARSET (Chinese) = 134 + Shared::Platform::PlatformContextGl::charSet = + strToInt(lang.getString("FONT_CHARSET_WINDOWS")); + } + if (lang.hasString("FONT_MULTIBYTE_WINDOWS")) { + Font::fontIsMultibyte = + strToBool(lang.getString("FONT_MULTIBYTE_WINDOWS")); + } + if (lang.hasString("FONT_RIGHTTOLEFT_WINDOWS")) { + Font::fontIsRightToLeft = + strToBool(lang.getString("FONT_RIGHTTOLEFT_WINDOWS")); + } - if (lang.hasString ("MEGAGLEST_FONT_WINDOWS")) - { - //setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese - string newEnvValue = - "MEGAGLEST_FONT=" + lang.getString ("MEGAGLEST_FONT_WINDOWS"); - _putenv (newEnvValue.c_str ()); - } + if (lang.hasString("MEGAGLEST_FONT_WINDOWS")) { + //setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese + string newEnvValue = + "MEGAGLEST_FONT=" + lang.getString("MEGAGLEST_FONT_WINDOWS"); + _putenv(newEnvValue.c_str()); + } - // end win32 + // end win32 #endif - if (lang.hasString ("ALLOWED_SPECIAL_KEYS", "", false)) - { - string allowedKeys = lang.getString ("ALLOWED_SPECIAL_KEYS"); - Window::addAllowedKeys (allowedKeys); - } - else - { - Window::clearAllowedKeys (); - } - - if (loadFonts == true) - { - CoreData & coreData = CoreData::getInstance (); - coreData.loadFonts (); - } - } - } - - void Lang::loadGameStringProperties (string uselanguage, - Properties & properties, - bool fileMustExist, - bool fallbackToDefault) - { - properties.clear (); - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - //string languageFile = data_path + "data/lang/" + uselanguage + ".lng"; - string languageFile = - getGameCustomCoreDataPath (data_path, - "data/lang/" + uselanguage + ".lng"); - if (fileMustExist == false && fileExists (languageFile) == false) - { - return; - } - else if (fileExists (languageFile) == false - && fallbackToDefault == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - languageFile.c_str ()); - //throw megaglest_runtime_error("File NOT FOUND, can't open file: [" + languageFile + "]"); - printf - ("Language file NOT FOUND, can't open file: [%s] switching to default language: %s\n", - languageFile.c_str (), DEFAULT_LANGUAGE); - - languageFile = - getGameCustomCoreDataPath (data_path, - "data/lang/" + - string (DEFAULT_LANGUAGE) + ".lng"); - } - is_utf8_language = valid_utf8_file (languageFile.c_str ()); - properties.load (languageFile); - } - -//bool Lang::isUTF8Language() const { -// return is_utf8_language; -//} - - void Lang::loadScenarioStrings (string scenarioDir, string scenarioName, - bool isTutorial) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] scenarioDir = [%s] scenarioName = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - scenarioDir.c_str (), - scenarioName.c_str ()); - - //printf("Loading scenario scenarioDir [%s] scenarioName [%s]\n",scenarioDir.c_str(),scenarioName.c_str()); - - // First try to find scenario lng file in userdata - Config & config = Config::getInstance (); - vector < string > scenarioPaths; - if (isTutorial == false) - { - scenarioPaths = config.getPathListForType (ptScenarios); - } - else - { - scenarioPaths = config.getPathListForType (ptTutorials); - } - if (scenarioPaths.size () > 1) - { - string & scenarioPath = scenarioPaths[1]; - endPathWithSlash (scenarioPath); - - string currentPath = scenarioPath; - endPathWithSlash (currentPath); - string scenarioFolder = currentPath + scenarioName + "/"; - string path = scenarioFolder + scenarioName + "_" + language + ".lng"; - - //try to load the current language first - if (fileExists (path)) - { - scenarioDir = scenarioPath; - } - } - - string currentPath = scenarioDir; - endPathWithSlash (currentPath); - string scenarioFolder = currentPath + scenarioName + "/"; - string path = scenarioFolder + scenarioName + "_" + language + ".lng"; - if (EndsWith (scenarioDir, ".xml") == true) - { - scenarioFolder = extractDirectoryPathFromFile (scenarioDir); - path = scenarioFolder + scenarioName + "_" + language + ".lng"; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - } - - scenarioStrings.clear (); - - //try to load the current language first - if (fileExists (path)) - { - //printf("#2 Loading scenario path [%s]\n",path.c_str()); - - scenarioStrings.load (path); - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path not found [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - //try english otherwise - path = scenarioFolder + scenarioName + "_english.lng"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - if (fileExists (path)) - { - //printf("#3 Loading scenario path [%s]\n",path.c_str()); - - scenarioStrings.load (path); - } - } - } - - bool Lang::loadTechTreeStrings (string techTree, bool forceLoad) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] techTree = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - techTree.c_str ()); - - //printf("Line: %d techTree = %s forceLoad = %d\n",__LINE__,techTree.c_str(),forceLoad); - - if (forceLoad == false && techTree == techNameLoaded) - { - return true; - } - - bool foundTranslation = false; - - string currentPath = ""; - Config & config = Config::getInstance (); - vector < string > techPaths = config.getPathListForType (ptTechs); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - - if (folderExists (techPath + techTree) == true) - { - currentPath = techPath; - endPathWithSlash (currentPath); - break; - } - } - - string techTreeFolder = currentPath + techTree + "/"; - string path = - techTreeFolder + "lang/" + techTree + "_" + language + ".lng"; - string pathDefault = - techTreeFolder + "lang/" + techTree + "_default.lng"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s] pathDefault = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str (), pathDefault.c_str ()); - - //techTreeStrings.clear(); - //techTreeStringsDefault.clear(); - - //printf("Line: %d techTree = %s this->language = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),this->language.c_str(),forceLoad,path.c_str()); - - //try to load the current language first - if (fileExists (path)) - { - foundTranslation = true; - if (forceLoad == true || - path != - techTreeStringsAllLanguages[techTree][this->language].getpath ()) - { - - //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); - - techTreeStringsAllLanguages[techTree][this->language].load (path); - techNameLoaded = techTree; - } - else if (path == - techTreeStringsAllLanguages[techTree][this->language]. - getpath () && techTree != techNameLoaded) - { - techNameLoaded = techTree; - } - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path not found [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - //try english otherwise - string default_language = "english"; - path = - techTreeFolder + "lang/" + techTree + "_" + default_language + - ".lng"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); - - if (fileExists (path)) - { - foundTranslation = true; - if (forceLoad == true || - path != - techTreeStringsAllLanguages[techTree][default_language]. - getpath ()) - { - //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); - - techTreeStringsAllLanguages[techTree][default_language]. - load (path); - techNameLoaded = techTree; - } - else if (path == - techTreeStringsAllLanguages[techTree][default_language]. - getpath () && techTree != techNameLoaded) - { - techNameLoaded = techTree; - } - } - } - - if (fileExists (pathDefault)) - { - foundTranslation = true; - string default_language = "default"; - - //printf("Line: %d techTree = %s forceLoad = %d default_language = %s\n",__LINE__,techTree.c_str(),forceLoad,default_language.c_str()); - - if (forceLoad == true || - pathDefault != - techTreeStringsAllLanguages[techTree][default_language]. - getpath ()) - { - //printf("Line: %d techTree = %s forceLoad = %d pathDefault = %s\n",__LINE__,techTree.c_str(),forceLoad,pathDefault.c_str()); - - techTreeStringsAllLanguages[techTree][default_language]. - load (pathDefault); - techNameLoaded = techTree; - } - else if (pathDefault == - techTreeStringsAllLanguages[techTree][default_language]. - getpath () && techTree != techNameLoaded) - { - techNameLoaded = techTree; - } - } - - return foundTranslation; - } - - void Lang::loadTilesetStrings (string tileset) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] tileset = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - tileset.c_str ()); - - string currentPath = ""; - Config & config = Config::getInstance (); - vector < string > tilesetPaths = config.getPathListForType (ptTilesets); - for (int idx = 0; idx < (int) tilesetPaths.size (); idx++) - { - string & tilesetPath = tilesetPaths[idx]; - endPathWithSlash (tilesetPath); - - //printf("tilesetPath [%s]\n",tilesetPath.c_str()); - - if (folderExists (tilesetPath + tileset) == true) - { - currentPath = tilesetPath; - endPathWithSlash (currentPath); - break; - } - } - - string tilesetFolder = currentPath + tileset + "/"; - string path = - tilesetFolder + "lang/" + tileset + "_" + language + ".lng"; - string pathDefault = tilesetFolder + "lang/" + tileset + "_default.lng"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s] pathDefault = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str (), pathDefault.c_str ()); - - tilesetStrings.clear (); - tilesetStringsDefault.clear (); - - //try to load the current language first - if (fileExists (path)) - { - tilesetStrings.load (path); - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path not found [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - //try english otherwise - path = tilesetFolder + "lang/" + tileset + "_english.lng"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] path = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - path.c_str ()); - - if (fileExists (path)) - { - tilesetStrings.load (path); - } - } - - if (fileExists (pathDefault)) - { - tilesetStringsDefault.load (pathDefault); - } - } - - bool Lang::hasString (const string & s, string uselanguage, - bool fallbackToDefault) - { - bool result = false; - try - { - if (uselanguage != "") - { - //printf("#a fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); - if (gameStringsAllLanguages.find (uselanguage) == - gameStringsAllLanguages.end ()) - { - loadGameStringProperties (uselanguage, - gameStringsAllLanguages[uselanguage], - false); - } - //string result2 = otherLanguageStrings[uselanguage].getString(s); - gameStringsAllLanguages[uselanguage].getString (s); - //printf("#b result2 [%s]\n",result2.c_str()); - - result = true; - } - else - { - //string result2 = strings.getString(s); - gameStringsAllLanguages[this->language].getString (s); - result = true; - } - } - catch (exception & ex) - { - if (gameStringsAllLanguages[this->language].getpath () != "") - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s] for uselanguage [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what (), uselanguage.c_str ()); - } - - //printf("#1 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); - if (fallbackToDefault == true && uselanguage != DEFAULT_LANGUAGE - && this->language != DEFAULT_LANGUAGE) - { - result = hasString (s, DEFAULT_LANGUAGE, false); - } - else - { - - } - } - return result; - } - - bool Lang::isLanguageLocal (string compareLanguage) const - { - return (compareLanguage == language); - } - - string Lang::parseResult (const string & key, const string & value) - { - if (value != "$USE_DEFAULT_LANGUAGE_VALUE") - { - return value; - } - string result = Lang::getString (key, DEFAULT_LANGUAGE); - return result; - } - string Lang::getString (const string & s, string uselanguage, - bool fallbackToDefault) - { - try - { - string result = ""; - - if (uselanguage != "") - { - if (gameStringsAllLanguages.find (uselanguage) == - gameStringsAllLanguages.end ()) - { - loadGameStringProperties (uselanguage, - gameStringsAllLanguages[uselanguage], - false); - } - result = gameStringsAllLanguages[uselanguage].getString (s); - replaceAll (result, "\\n", "\n"); - } - else - { - result = gameStringsAllLanguages[this->language].getString (s); - replaceAll (result, "\\n", "\n"); - } - - return parseResult (s, result);; - } - catch (exception & ex) - { - if (gameStringsAllLanguages[this->language].getpath () != "") - { - if (fallbackToDefault == false || SystemFlags::VERBOSE_MODE_ENABLED) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s] uselanguage [%s] text [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what (), uselanguage.c_str (), - s.c_str ()); - } - } - } - - //printf("#2 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); - - //if(fallbackToDefault == true && uselanguage != DEFAULT_LANGUAGE && this->language != DEFAULT_LANGUAGE) { - if (uselanguage != DEFAULT_LANGUAGE - && this->language != DEFAULT_LANGUAGE) - { - return getString (s, DEFAULT_LANGUAGE, false); - } - - //return "???" + s + "???"; - } - return "???" + s + "???"; - } - - string Lang::getScenarioString (const string & s) - { - try - { - string result = scenarioStrings.getString (s); - replaceAll (result, "\\n", "\n"); - return result; - } - catch (exception & ex) - { - if (scenarioStrings.getpath () != "") - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - } - return "???" + s + "???"; - } - } - - bool Lang::hasScenarioString (const string & s) - { - bool result = false; - try - { - scenarioStrings.getString (s); - result = true; - } - catch (exception & ex) - { - if (scenarioStrings.getpath () != "") - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - } - } - return result; - } - - string Lang::getTechTreeString (const string & s, - const char *defaultValue) - { - try - { - string result = ""; - string default_language = "default"; - - //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); - - if (allowNativeLanguageTechtree == true && - (techTreeStringsAllLanguages[techNameLoaded][this->language]. - hasString (s) == true || defaultValue == NULL)) - { - if (techTreeStringsAllLanguages[techNameLoaded][this->language]. - hasString (s) == false - && - techTreeStringsAllLanguages[techNameLoaded][default_language]. - hasString (s) == true) - { - - //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); - - result = - techTreeStringsAllLanguages[techNameLoaded][default_language]. - getString (s); - } - else - { - - //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); - - result = - techTreeStringsAllLanguages[techNameLoaded][this->language]. - getString (s); - } - } - else if (allowNativeLanguageTechtree == true && - techTreeStringsAllLanguages[techNameLoaded] - [default_language].hasString (s) == true) - { - - //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); - - result = - techTreeStringsAllLanguages[techNameLoaded][default_language]. - getString (s); - } - else if (defaultValue != NULL) - { - result = defaultValue; - } - replaceAll (result, "\\n", "\n"); - return result; - } - catch (exception & ex) - { - if (techTreeStringsAllLanguages[techNameLoaded][this->language]. - getpath () != "") - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - } - //return "???" + s + "???"; - } - return "???" + s + "???"; - } - - string Lang::getTilesetString (const string & s, const char *defaultValue) - { - try - { - string result = ""; - - if (tilesetStrings.hasString (s) == true || defaultValue == NULL) - { - if (tilesetStrings.hasString (s) == false - && tilesetStringsDefault.hasString (s) == true) - { - result = tilesetStringsDefault.getString (s); - } - else - { - result = tilesetStrings.getString (s); - } - } - else if (tilesetStringsDefault.hasString (s) == true) - { - result = tilesetStringsDefault.getString (s); - } - else if (defaultValue != NULL) - { - result = defaultValue; - } - replaceAll (result, "\\n", "\n"); - return result; - } - catch (exception & ex) - { - if (tilesetStrings.getpath () != "") - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - ex.what ()); - } - //return "???" + s + "???"; - } - return "???" + s + "???"; - } - - bool Lang::fileMatchesISO630Code (string uselanguage, - string testLanguageFile) - { - bool result = false; - Properties stringsTest; - stringsTest.load (testLanguageFile); - - try - { - string iso639 = stringsTest.getString ("ISO639-1"); - if (iso639 == uselanguage) - { - result = true; - } - } - //catch(const exception &ex) { - catch ( ...) - { - } - - return result; - } - - string Lang::getLanguageFile (string uselanguage) - { - bool foundMatch = false; - string result = uselanguage; - - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - vector < string > langResults; - string userDataPath = getGameCustomCoreDataPath (data_path, ""); - findAll (userDataPath + "data/lang/*.lng", langResults, true, false); - for (unsigned int i = 0; i < langResults.size () && foundMatch == false; - ++i) - { - string testLanguageFile = - userDataPath + "data/lang/" + langResults[i] + ".lng"; - foundMatch = fileMatchesISO630Code (uselanguage, testLanguageFile); - if (foundMatch == true) - { - result = langResults[i]; - } - } - if (foundMatch == false) - { - langResults.clear (); - findAll (data_path + "data/lang/*.lng", langResults, true); - for (unsigned int i = 0; - i < langResults.size () && foundMatch == false; ++i) - { - string testLanguageFile = - data_path + "data/lang/" + langResults[i] + ".lng"; - foundMatch = fileMatchesISO630Code (uselanguage, testLanguageFile); - if (foundMatch == true) - { - result = langResults[i]; - } - } - } - return result; - } - - string Lang::getNativeLanguageName (string uselanguage, - string testLanguageFile) - { - string result = uselanguage; - - static map < string, string > cachedNativeLanguageNames; - if (cachedNativeLanguageNames.find (testLanguageFile) != - cachedNativeLanguageNames.end ()) - { - result = cachedNativeLanguageNames[testLanguageFile]; - } - else - { - Properties stringsTest; - stringsTest.load (testLanguageFile); - - try - { - result = stringsTest.getString ("NativeLanguageName"); - cachedNativeLanguageNames[testLanguageFile] = result; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Caching native language name for [%s] = [%s]\n", - testLanguageFile.c_str (), result.c_str ()); - } - catch (const exception & ex) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("ERROR Caching native language name for [%s] msg: [%s]\n", - testLanguageFile.c_str (), ex.what ()); - } - catch ( ...) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("ERROR Caching native language name for [%s] msg: [UNKNOWN]\n", - testLanguageFile.c_str ()); - } - } - - return result; - } - - pair < string, - string > Lang::getNavtiveNameFromLanguageName (string langName) - { - pair < string, string > result; - - //printf("looking for language [%s]\n",langName.c_str()); - - map < string, string > nativeList = - Lang::getDiscoveredLanguageList (true); - map < string, string >::iterator iterMap = nativeList.find (langName); - if (iterMap != nativeList.end ()) - { - result = make_pair (iterMap->second, iterMap->first); - } - return result; - } - - map < string, - string > Lang::getDiscoveredLanguageList (bool searchKeyIsLangName) - { - map < string, string > result; - - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - string userDataPath = getGameCustomCoreDataPath (data_path, ""); - - vector < string > langResults; - findAll (userDataPath + "data/lang/*.lng", langResults, true, false); - for (unsigned int i = 0; i < langResults.size (); ++i) - { - string testLanguage = langResults[i]; - string testLanguageFile = - userDataPath + "data/lang/" + testLanguage + ".lng"; - string nativeName = - getNativeLanguageName (testLanguage, testLanguageFile); - if (searchKeyIsLangName == false) - { - result[nativeName] = testLanguage; - } - else - { - result[testLanguage] = nativeName; - } - } - - vector < string > langResults2; - findAll (data_path + "data/lang/*.lng", langResults2, true); - if (langResults2.empty () && langResults.empty ()) - { - throw megaglest_runtime_error ("There are no lang files"); - } - for (unsigned int i = 0; i < langResults2.size (); ++i) - { - string testLanguage = langResults2[i]; - if (std:: - find (langResults.begin (), langResults.end (), - testLanguage) == langResults.end ()) - { - langResults.push_back (testLanguage); - - string testLanguageFile = - data_path + "data/lang/" + testLanguage + ".lng"; - string nativeName = - getNativeLanguageName (testLanguage, testLanguageFile); - if (searchKeyIsLangName == false) - { - result[nativeName] = testLanguage; - } - else - { - result[testLanguage] = nativeName; - } - } - } - - return result; - } - - } + if (lang.hasString("ALLOWED_SPECIAL_KEYS", "", false)) { + string allowedKeys = lang.getString("ALLOWED_SPECIAL_KEYS"); + Window::addAllowedKeys(allowedKeys); + } else { + Window::clearAllowedKeys(); + } + + if (loadFonts == true) { + CoreData & coreData = CoreData::getInstance(); + coreData.loadFonts(); + } + } + } + + void Lang::loadGameStringProperties(string uselanguage, + Properties & properties, + bool fileMustExist, + bool fallbackToDefault) { + properties.clear(); + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + //string languageFile = data_path + "data/lang/" + uselanguage + ".lng"; + string languageFile = + getGameCustomCoreDataPath(data_path, + "data/lang/" + uselanguage + ".lng"); + if (fileMustExist == false && fileExists(languageFile) == false) { + return; + } else if (fileExists(languageFile) == false + && fallbackToDefault == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + languageFile.c_str()); + //throw megaglest_runtime_error("File NOT FOUND, can't open file: [" + languageFile + "]"); + printf + ("Language file NOT FOUND, can't open file: [%s] switching to default language: %s\n", + languageFile.c_str(), DEFAULT_LANGUAGE); + + languageFile = + getGameCustomCoreDataPath(data_path, + "data/lang/" + + string(DEFAULT_LANGUAGE) + ".lng"); + } + is_utf8_language = valid_utf8_file(languageFile.c_str()); + properties.load(languageFile); + } + + //bool Lang::isUTF8Language() const { + // return is_utf8_language; + //} + + void Lang::loadScenarioStrings(string scenarioDir, string scenarioName, + bool isTutorial) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] scenarioDir = [%s] scenarioName = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + scenarioDir.c_str(), + scenarioName.c_str()); + + //printf("Loading scenario scenarioDir [%s] scenarioName [%s]\n",scenarioDir.c_str(),scenarioName.c_str()); + + // First try to find scenario lng file in userdata + Config & config = Config::getInstance(); + vector < string > scenarioPaths; + if (isTutorial == false) { + scenarioPaths = config.getPathListForType(ptScenarios); + } else { + scenarioPaths = config.getPathListForType(ptTutorials); + } + if (scenarioPaths.size() > 1) { + string & scenarioPath = scenarioPaths[1]; + endPathWithSlash(scenarioPath); + + string currentPath = scenarioPath; + endPathWithSlash(currentPath); + string scenarioFolder = currentPath + scenarioName + "/"; + string path = scenarioFolder + scenarioName + "_" + language + ".lng"; + + //try to load the current language first + if (fileExists(path)) { + scenarioDir = scenarioPath; + } + } + + string currentPath = scenarioDir; + endPathWithSlash(currentPath); + string scenarioFolder = currentPath + scenarioName + "/"; + string path = scenarioFolder + scenarioName + "_" + language + ".lng"; + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioFolder = extractDirectoryPathFromFile(scenarioDir); + path = scenarioFolder + scenarioName + "_" + language + ".lng"; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + } + + scenarioStrings.clear(); + + //try to load the current language first + if (fileExists(path)) { + //printf("#2 Loading scenario path [%s]\n",path.c_str()); + + scenarioStrings.load(path); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path not found [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + //try english otherwise + path = scenarioFolder + scenarioName + "_english.lng"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + if (fileExists(path)) { + //printf("#3 Loading scenario path [%s]\n",path.c_str()); + + scenarioStrings.load(path); + } + } + } + + bool Lang::loadTechTreeStrings(string techTree, bool forceLoad) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] techTree = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + techTree.c_str()); + + //printf("Line: %d techTree = %s forceLoad = %d\n",__LINE__,techTree.c_str(),forceLoad); + + if (forceLoad == false && techTree == techNameLoaded) { + return true; + } + + bool foundTranslation = false; + + string currentPath = ""; + Config & config = Config::getInstance(); + vector < string > techPaths = config.getPathListForType(ptTechs); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + + if (folderExists(techPath + techTree) == true) { + currentPath = techPath; + endPathWithSlash(currentPath); + break; + } + } + + string techTreeFolder = currentPath + techTree + "/"; + string path = + techTreeFolder + "lang/" + techTree + "_" + language + ".lng"; + string pathDefault = + techTreeFolder + "lang/" + techTree + "_default.lng"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s] pathDefault = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str(), pathDefault.c_str()); + + //techTreeStrings.clear(); + //techTreeStringsDefault.clear(); + + //printf("Line: %d techTree = %s this->language = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),this->language.c_str(),forceLoad,path.c_str()); + + //try to load the current language first + if (fileExists(path)) { + foundTranslation = true; + if (forceLoad == true || + path != + techTreeStringsAllLanguages[techTree][this->language].getpath()) { + + //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); + + techTreeStringsAllLanguages[techTree][this->language].load(path); + techNameLoaded = techTree; + } else if (path == + techTreeStringsAllLanguages[techTree][this->language]. + getpath() && techTree != techNameLoaded) { + techNameLoaded = techTree; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path not found [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + //try english otherwise + string default_language = "english"; + path = + techTreeFolder + "lang/" + techTree + "_" + default_language + + ".lng"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); + + if (fileExists(path)) { + foundTranslation = true; + if (forceLoad == true || + path != + techTreeStringsAllLanguages[techTree][default_language]. + getpath()) { + //printf("Line: %d techTree = %s forceLoad = %d path = %s\n",__LINE__,techTree.c_str(),forceLoad,path.c_str()); + + techTreeStringsAllLanguages[techTree][default_language]. + load(path); + techNameLoaded = techTree; + } else if (path == + techTreeStringsAllLanguages[techTree][default_language]. + getpath() && techTree != techNameLoaded) { + techNameLoaded = techTree; + } + } + } + + if (fileExists(pathDefault)) { + foundTranslation = true; + string default_language = "default"; + + //printf("Line: %d techTree = %s forceLoad = %d default_language = %s\n",__LINE__,techTree.c_str(),forceLoad,default_language.c_str()); + + if (forceLoad == true || + pathDefault != + techTreeStringsAllLanguages[techTree][default_language]. + getpath()) { + //printf("Line: %d techTree = %s forceLoad = %d pathDefault = %s\n",__LINE__,techTree.c_str(),forceLoad,pathDefault.c_str()); + + techTreeStringsAllLanguages[techTree][default_language]. + load(pathDefault); + techNameLoaded = techTree; + } else if (pathDefault == + techTreeStringsAllLanguages[techTree][default_language]. + getpath() && techTree != techNameLoaded) { + techNameLoaded = techTree; + } + } + + return foundTranslation; + } + + void Lang::loadTilesetStrings(string tileset) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] tileset = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + tileset.c_str()); + + string currentPath = ""; + Config & config = Config::getInstance(); + vector < string > tilesetPaths = config.getPathListForType(ptTilesets); + for (int idx = 0; idx < (int) tilesetPaths.size(); idx++) { + string & tilesetPath = tilesetPaths[idx]; + endPathWithSlash(tilesetPath); + + //printf("tilesetPath [%s]\n",tilesetPath.c_str()); + + if (folderExists(tilesetPath + tileset) == true) { + currentPath = tilesetPath; + endPathWithSlash(currentPath); + break; + } + } + + string tilesetFolder = currentPath + tileset + "/"; + string path = + tilesetFolder + "lang/" + tileset + "_" + language + ".lng"; + string pathDefault = tilesetFolder + "lang/" + tileset + "_default.lng"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s] pathDefault = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str(), pathDefault.c_str()); + + tilesetStrings.clear(); + tilesetStringsDefault.clear(); + + //try to load the current language first + if (fileExists(path)) { + tilesetStrings.load(path); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path not found [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + //try english otherwise + path = tilesetFolder + "lang/" + tileset + "_english.lng"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] path = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + path.c_str()); + + if (fileExists(path)) { + tilesetStrings.load(path); + } + } + + if (fileExists(pathDefault)) { + tilesetStringsDefault.load(pathDefault); + } + } + + bool Lang::hasString(const string & s, string uselanguage, + bool fallbackToDefault) { + bool result = false; + try { + if (uselanguage != "") { + //printf("#a fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); + if (gameStringsAllLanguages.find(uselanguage) == + gameStringsAllLanguages.end()) { + loadGameStringProperties(uselanguage, + gameStringsAllLanguages[uselanguage], + false); + } + //string result2 = otherLanguageStrings[uselanguage].getString(s); + gameStringsAllLanguages[uselanguage].getString(s); + //printf("#b result2 [%s]\n",result2.c_str()); + + result = true; + } else { + //string result2 = strings.getString(s); + gameStringsAllLanguages[this->language].getString(s); + result = true; + } + } catch (exception & ex) { + if (gameStringsAllLanguages[this->language].getpath() != "") { + if (SystemFlags::VERBOSE_MODE_ENABLED) + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s] for uselanguage [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what(), uselanguage.c_str()); + } + + //printf("#1 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); + if (fallbackToDefault == true && uselanguage != DEFAULT_LANGUAGE + && this->language != DEFAULT_LANGUAGE) { + result = hasString(s, DEFAULT_LANGUAGE, false); + } else { + + } + } + return result; + } + + bool Lang::isLanguageLocal(string compareLanguage) const { + return (compareLanguage == language); + } + + string Lang::parseResult(const string & key, const string & value) { + if (value != "$USE_DEFAULT_LANGUAGE_VALUE") { + return value; + } + string result = Lang::getString(key, DEFAULT_LANGUAGE); + return result; + } + string Lang::getString(const string & s, string uselanguage, + bool fallbackToDefault) { + try { + string result = ""; + + if (uselanguage != "") { + if (gameStringsAllLanguages.find(uselanguage) == + gameStringsAllLanguages.end()) { + loadGameStringProperties(uselanguage, + gameStringsAllLanguages[uselanguage], + false); + } + result = gameStringsAllLanguages[uselanguage].getString(s); + replaceAll(result, "\\n", "\n"); + } else { + result = gameStringsAllLanguages[this->language].getString(s); + replaceAll(result, "\\n", "\n"); + } + + return parseResult(s, result);; + } catch (exception & ex) { + if (gameStringsAllLanguages[this->language].getpath() != "") { + if (fallbackToDefault == false || SystemFlags::VERBOSE_MODE_ENABLED) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s] uselanguage [%s] text [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what(), uselanguage.c_str(), + s.c_str()); + } + } + } + + //printf("#2 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str()); + + //if(fallbackToDefault == true && uselanguage != DEFAULT_LANGUAGE && this->language != DEFAULT_LANGUAGE) { + if (uselanguage != DEFAULT_LANGUAGE + && this->language != DEFAULT_LANGUAGE) { + return getString(s, DEFAULT_LANGUAGE, false); + } + + //return "???" + s + "???"; + } + return "???" + s + "???"; + } + + string Lang::getScenarioString(const string & s) { + try { + string result = scenarioStrings.getString(s); + replaceAll(result, "\\n", "\n"); + return result; + } catch (exception & ex) { + if (scenarioStrings.getpath() != "") { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + } + return "???" + s + "???"; + } + } + + bool Lang::hasScenarioString(const string & s) { + bool result = false; + try { + scenarioStrings.getString(s); + result = true; + } catch (exception & ex) { + if (scenarioStrings.getpath() != "") { + if (SystemFlags::VERBOSE_MODE_ENABLED) + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + } + } + return result; + } + + string Lang::getTechTreeString(const string & s, + const char *defaultValue) { + try { + string result = ""; + string default_language = "default"; + + //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); + + if (allowNativeLanguageTechtree == true && + (techTreeStringsAllLanguages[techNameLoaded][this->language]. + hasString(s) == true || defaultValue == NULL)) { + if (techTreeStringsAllLanguages[techNameLoaded][this->language]. + hasString(s) == false + && + techTreeStringsAllLanguages[techNameLoaded][default_language]. + hasString(s) == true) { + + //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); + + result = + techTreeStringsAllLanguages[techNameLoaded][default_language]. + getString(s); + } else { + + //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); + + result = + techTreeStringsAllLanguages[techNameLoaded][this->language]. + getString(s); + } + } else if (allowNativeLanguageTechtree == true && + techTreeStringsAllLanguages[techNameLoaded] + [default_language].hasString(s) == true) { + + //printf("Line: %d techNameLoaded = %s s = %s this->language = %s\n",__LINE__,techNameLoaded.c_str(),s.c_str(),this->language.c_str()); + + result = + techTreeStringsAllLanguages[techNameLoaded][default_language]. + getString(s); + } else if (defaultValue != NULL) { + result = defaultValue; + } + replaceAll(result, "\\n", "\n"); + return result; + } catch (exception & ex) { + if (techTreeStringsAllLanguages[techNameLoaded][this->language]. + getpath() != "") { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + } + //return "???" + s + "???"; + } + return "???" + s + "???"; + } + + string Lang::getTilesetString(const string & s, const char *defaultValue) { + try { + string result = ""; + + if (tilesetStrings.hasString(s) == true || defaultValue == NULL) { + if (tilesetStrings.hasString(s) == false + && tilesetStringsDefault.hasString(s) == true) { + result = tilesetStringsDefault.getString(s); + } else { + result = tilesetStrings.getString(s); + } + } else if (tilesetStringsDefault.hasString(s) == true) { + result = tilesetStringsDefault.getString(s); + } else if (defaultValue != NULL) { + result = defaultValue; + } + replaceAll(result, "\\n", "\n"); + return result; + } catch (exception & ex) { + if (tilesetStrings.getpath() != "") { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + ex.what()); + } + //return "???" + s + "???"; + } + return "???" + s + "???"; + } + + bool Lang::fileMatchesISO630Code(string uselanguage, + string testLanguageFile) { + bool result = false; + Properties stringsTest; + stringsTest.load(testLanguageFile); + + try { + string iso639 = stringsTest.getString("ISO639-1"); + if (iso639 == uselanguage) { + result = true; + } + } + //catch(const exception &ex) { + catch (...) { + } + + return result; + } + + string Lang::getLanguageFile(string uselanguage) { + bool foundMatch = false; + string result = uselanguage; + + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + vector < string > langResults; + string userDataPath = getGameCustomCoreDataPath(data_path, ""); + findAll(userDataPath + "data/lang/*.lng", langResults, true, false); + for (unsigned int i = 0; i < langResults.size() && foundMatch == false; + ++i) { + string testLanguageFile = + userDataPath + "data/lang/" + langResults[i] + ".lng"; + foundMatch = fileMatchesISO630Code(uselanguage, testLanguageFile); + if (foundMatch == true) { + result = langResults[i]; + } + } + if (foundMatch == false) { + langResults.clear(); + findAll(data_path + "data/lang/*.lng", langResults, true); + for (unsigned int i = 0; + i < langResults.size() && foundMatch == false; ++i) { + string testLanguageFile = + data_path + "data/lang/" + langResults[i] + ".lng"; + foundMatch = fileMatchesISO630Code(uselanguage, testLanguageFile); + if (foundMatch == true) { + result = langResults[i]; + } + } + } + return result; + } + + string Lang::getNativeLanguageName(string uselanguage, + string testLanguageFile) { + string result = uselanguage; + + static map < string, string > cachedNativeLanguageNames; + if (cachedNativeLanguageNames.find(testLanguageFile) != + cachedNativeLanguageNames.end()) { + result = cachedNativeLanguageNames[testLanguageFile]; + } else { + Properties stringsTest; + stringsTest.load(testLanguageFile); + + try { + result = stringsTest.getString("NativeLanguageName"); + cachedNativeLanguageNames[testLanguageFile] = result; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Caching native language name for [%s] = [%s]\n", + testLanguageFile.c_str(), result.c_str()); + } catch (const exception & ex) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("ERROR Caching native language name for [%s] msg: [%s]\n", + testLanguageFile.c_str(), ex.what()); + } catch (...) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("ERROR Caching native language name for [%s] msg: [UNKNOWN]\n", + testLanguageFile.c_str()); + } + } + + return result; + } + + pair < string, + string > Lang::getNavtiveNameFromLanguageName(string langName) { + pair < string, string > result; + + //printf("looking for language [%s]\n",langName.c_str()); + + map < string, string > nativeList = + Lang::getDiscoveredLanguageList(true); + map < string, string >::iterator iterMap = nativeList.find(langName); + if (iterMap != nativeList.end()) { + result = make_pair(iterMap->second, iterMap->first); + } + return result; + } + + map < string, + string > Lang::getDiscoveredLanguageList(bool searchKeyIsLangName) { + map < string, string > result; + + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + string userDataPath = getGameCustomCoreDataPath(data_path, ""); + + vector < string > langResults; + findAll(userDataPath + "data/lang/*.lng", langResults, true, false); + for (unsigned int i = 0; i < langResults.size(); ++i) { + string testLanguage = langResults[i]; + string testLanguageFile = + userDataPath + "data/lang/" + testLanguage + ".lng"; + string nativeName = + getNativeLanguageName(testLanguage, testLanguageFile); + if (searchKeyIsLangName == false) { + result[nativeName] = testLanguage; + } else { + result[testLanguage] = nativeName; + } + } + + vector < string > langResults2; + findAll(data_path + "data/lang/*.lng", langResults2, true); + if (langResults2.empty() && langResults.empty()) { + throw megaglest_runtime_error("There are no lang files"); + } + for (unsigned int i = 0; i < langResults2.size(); ++i) { + string testLanguage = langResults2[i]; + if (std:: + find(langResults.begin(), langResults.end(), + testLanguage) == langResults.end()) { + langResults.push_back(testLanguage); + + string testLanguageFile = + data_path + "data/lang/" + testLanguage + ".lng"; + string nativeName = + getNativeLanguageName(testLanguage, testLanguageFile); + if (searchKeyIsLangName == false) { + result[nativeName] = testLanguage; + } else { + result[testLanguage] = nativeName; + } + } + } + + return result; + } + + } } //end namespace diff --git a/source/glest_game/global/lang.h b/source/glest_game/global/lang.h index d5f684fcc..fde16b132 100644 --- a/source/glest_game/global/lang.h +++ b/source/glest_game/global/lang.h @@ -20,101 +20,95 @@ # include "properties.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using Shared::Util::Properties; + using Shared::Util::Properties; -// ===================================================== -// class Lang -// -// String table -// ===================================================== + // ===================================================== + // class Lang + // + // String table + // ===================================================== - class Lang - { - private: + class Lang { + private: - bool is_utf8_language; - string language; - std::map < string, Properties > gameStringsAllLanguages; + bool is_utf8_language; + string language; + std::map < string, Properties > gameStringsAllLanguages; - Properties scenarioStrings; + Properties scenarioStrings; - Properties tilesetStrings; - Properties tilesetStringsDefault; + Properties tilesetStrings; + Properties tilesetStringsDefault; - std::map < string, std::map < string, - Properties > >techTreeStringsAllLanguages; - string techNameLoaded; - bool allowNativeLanguageTechtree; + std::map < string, std::map < string, + Properties > >techTreeStringsAllLanguages; + string techNameLoaded; + bool allowNativeLanguageTechtree; - private: - Lang (); - void loadGameStringProperties (string language, Properties & properties, - bool fileMustExist, - bool fallbackToDefault = false); - bool fileMatchesISO630Code (string uselanguage, - string testLanguageFile); - string getNativeLanguageName (string uselanguage, - string testLanguageFile); + private: + Lang(); + void loadGameStringProperties(string language, Properties & properties, + bool fileMustExist, + bool fallbackToDefault = false); + bool fileMatchesISO630Code(string uselanguage, + string testLanguageFile); + string getNativeLanguageName(string uselanguage, + string testLanguageFile); - string parseResult (const string & key, const string & value); + string parseResult(const string & key, const string & value); - public: - static Lang & getInstance (); + public: + static Lang & getInstance(); - string getTechNameLoaded () const - { - return techNameLoaded; - } - bool getAllowNativeLanguageTechtree () const - { - return allowNativeLanguageTechtree; - } - void setAllowNativeLanguageTechtree (bool value) - { - allowNativeLanguageTechtree = value; - } + string getTechNameLoaded() const { + return techNameLoaded; + } + bool getAllowNativeLanguageTechtree() const { + return allowNativeLanguageTechtree; + } + void setAllowNativeLanguageTechtree(bool value) { + allowNativeLanguageTechtree = value; + } - void loadGameStrings (string uselanguage, bool loadFonts = - true, bool fallbackToDefault = false); - void loadScenarioStrings (string scenarioDir, string scenarioName, - bool isTutorial); - bool loadTechTreeStrings (string techTree, bool forceLoad = false); - void loadTilesetStrings (string tileset); + void loadGameStrings(string uselanguage, bool loadFonts = + true, bool fallbackToDefault = false); + void loadScenarioStrings(string scenarioDir, string scenarioName, + bool isTutorial); + bool loadTechTreeStrings(string techTree, bool forceLoad = false); + void loadTilesetStrings(string tileset); - string getString (const string & s, string uselanguage = - "", bool fallbackToDefault = false); - bool hasString (const string & s, string uselanguage = - "", bool fallbackToDefault = false); + string getString(const string & s, string uselanguage = + "", bool fallbackToDefault = false); + bool hasString(const string & s, string uselanguage = + "", bool fallbackToDefault = false); - string getScenarioString (const string & s); - bool hasScenarioString (const string & s); + string getScenarioString(const string & s); + bool hasScenarioString(const string & s); - string getTechTreeString (const string & s, const char *defaultValue = - NULL); - string getTilesetString (const string & s, const char *defaultValue = - NULL); + string getTechTreeString(const string & s, const char *defaultValue = + NULL); + string getTilesetString(const string & s, const char *defaultValue = + NULL); - string getLanguage () const - { - return language; - } - bool isLanguageLocal (string compareLanguage) const; - //bool isUTF8Language() const; - string getDefaultLanguage () const; + string getLanguage() const { + return language; + } + bool isLanguageLocal(string compareLanguage) const; + //bool isUTF8Language() const; + string getDefaultLanguage() const; - map < string, - string > getDiscoveredLanguageList (bool searchKeyIsLangName = false); - pair < string, - string > getNavtiveNameFromLanguageName (string langName); + map < string, + string > getDiscoveredLanguageList(bool searchKeyIsLangName = false); + pair < string, + string > getNavtiveNameFromLanguageName(string langName); - string getLanguageFile (string uselanguage); - }; + string getLanguageFile(string uselanguage); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/global/metrics.cpp b/source/glest_game/global/metrics.cpp index 34bf853a8..315a2fe11 100644 --- a/source/glest_game/global/metrics.cpp +++ b/source/glest_game/global/metrics.cpp @@ -16,117 +16,96 @@ using namespace std; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Metrics -// ===================================================== + // ===================================================== + // class Metrics + // ===================================================== - Metrics::Metrics () - { - reloadData (); - } + Metrics::Metrics() { + reloadData(); + } - void Metrics::reload (int resWidth, int resHeight) - { - Metrics *metrics = getInstancePtr (); - metrics->reloadData (resWidth, resHeight); - } + void Metrics::reload(int resWidth, int resHeight) { + Metrics *metrics = getInstancePtr(); + metrics->reloadData(resWidth, resHeight); + } - void Metrics::reloadData (int resWidth, int resHeight) - { - Config & config = Config::getInstance (); + void Metrics::reloadData(int resWidth, int resHeight) { + Config & config = Config::getInstance(); - virtualW = 1000; - virtualH = 750; + virtualW = 1000; + virtualH = 750; - if (resWidth > 0) - { - screenW = resWidth; - } - else - { - screenW = config.getInt ("ScreenWidth"); - } - if (resHeight > 0) - { - screenH = resHeight; - } - else - { - screenH = config.getInt ("ScreenHeight"); - } + if (resWidth > 0) { + screenW = resWidth; + } else { + screenW = config.getInt("ScreenWidth"); + } + if (resHeight > 0) { + screenH = resHeight; + } else { + screenH = config.getInt("ScreenHeight"); + } - minimapX = 10; - minimapY = 750 - 128 - 30 + 16; - minimapW = 128; - minimapH = 128; + minimapX = 10; + minimapY = 750 - 128 - 30 + 16; + minimapW = 128; + minimapH = 128; - displayX = 800; - displayY = 250; - displayW = 128; - displayH = 480; - } + displayX = 800; + displayY = 250; + displayW = 128; + displayH = 480; + } - Metrics *Metrics::getInstancePtr () - { - static Metrics metrics; - return &metrics; - } + Metrics *Metrics::getInstancePtr() { + static Metrics metrics; + return &metrics; + } - const Metrics & Metrics::getInstance () - { - Metrics *metrics = getInstancePtr (); - return *metrics; - } + const Metrics & Metrics::getInstance() { + Metrics *metrics = getInstancePtr(); + return *metrics; + } - float Metrics::getAspectRatio () const - { - if (screenH == 0) - { - throw megaglest_runtime_error ("div by 0 screenH == 0"); - } - return static_cast < float >(screenW) / screenH; - } + float Metrics::getAspectRatio() const { + if (screenH == 0) { + throw megaglest_runtime_error("div by 0 screenH == 0"); + } + return static_cast (screenW) / screenH; + } - int Metrics::toVirtualX (int w) const - { - if (screenW == 0) - { - throw megaglest_runtime_error ("div by 0 screenW == 0"); - } - return w * virtualW / screenW; - } + int Metrics::toVirtualX(int w) const { + if (screenW == 0) { + throw megaglest_runtime_error("div by 0 screenW == 0"); + } + return w * virtualW / screenW; + } - int Metrics::toVirtualY (int h) const - { - if (screenH == 0) - { - throw megaglest_runtime_error ("div by 0 screenH == 0"); - } + int Metrics::toVirtualY(int h) const { + if (screenH == 0) { + throw megaglest_runtime_error("div by 0 screenH == 0"); + } - //printf("h [%d] virtualH [%d] screenH [%d] result = %d\n",h,virtualH,screenH,(h*virtualH/screenH)); + //printf("h [%d] virtualH [%d] screenH [%d] result = %d\n",h,virtualH,screenH,(h*virtualH/screenH)); - return h * virtualH / screenH; - } + return h * virtualH / screenH; + } - bool Metrics::isInDisplay (int x, int y) const - { - return - x > displayX && - y > displayY && x < displayX + displayW && y < displayY + displayH; - } + bool Metrics::isInDisplay(int x, int y) const { + return + x > displayX && + y > displayY && x < displayX + displayW && y < displayY + displayH; + } - bool Metrics::isInMinimap (int x, int y) const - { - return - x > minimapX && - y > minimapY && x < minimapX + minimapW && y < minimapY + minimapH; - } + bool Metrics::isInMinimap(int x, int y) const { + return + x > minimapX && + y > minimapY && x < minimapX + minimapW && y < minimapY + minimapH; + } - } + } } // end namespace diff --git a/source/glest_game/global/metrics.h b/source/glest_game/global/metrics.h index 8a938bc1b..ec20d52f0 100644 --- a/source/glest_game/global/metrics.h +++ b/source/glest_game/global/metrics.h @@ -20,98 +20,84 @@ # include "config.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Metrics -// ===================================================== + // ===================================================== + // class Metrics + // ===================================================== - class Metrics - { - private: - int virtualW; - int virtualH; - int screenW; - int screenH; - int minimapX; - int minimapY; - int minimapW; - int minimapH; - int displayX; - int displayY; - int displayH; - int displayW; + class Metrics { + private: + int virtualW; + int virtualH; + int screenW; + int screenH; + int minimapX; + int minimapY; + int minimapW; + int minimapH; + int displayX; + int displayY; + int displayH; + int displayW; - private: - Metrics (); - static Metrics *getInstancePtr (); - void reloadData (int resWidth = -1, int resHeight = -1); + private: + Metrics(); + static Metrics *getInstancePtr(); + void reloadData(int resWidth = -1, int resHeight = -1); - public: - static const Metrics & getInstance (); + public: + static const Metrics & getInstance(); - int getVirtualW () const - { - return virtualW; - } - int getVirtualH () const - { - return virtualH; - } - int getScreenW () const - { - return screenW; - } - int getScreenH () const - { - return screenH; - } - int getMinimapX () const - { - return minimapX; - } - int getMinimapY () const - { - return minimapY; - } - int getMinimapW () const - { - return minimapW; - } - int getMinimapH () const - { - return minimapH; - } - int getDisplayX () const - { - return displayX; - } - int getDisplayY () const - { - return displayY; - } - int getDisplayH () const - { - return displayH; - } - int getDisplayW () const - { - return displayW; - } - float getAspectRatio () const; + int getVirtualW() const { + return virtualW; + } + int getVirtualH() const { + return virtualH; + } + int getScreenW() const { + return screenW; + } + int getScreenH() const { + return screenH; + } + int getMinimapX() const { + return minimapX; + } + int getMinimapY() const { + return minimapY; + } + int getMinimapW() const { + return minimapW; + } + int getMinimapH() const { + return minimapH; + } + int getDisplayX() const { + return displayX; + } + int getDisplayY() const { + return displayY; + } + int getDisplayH() const { + return displayH; + } + int getDisplayW() const { + return displayW; + } + float getAspectRatio() const; - int toVirtualX (int w) const; - int toVirtualY (int h) const; + int toVirtualX(int w) const; + int toVirtualY(int h) const; - bool isInDisplay (int x, int y) const; - bool isInMinimap (int x, int y) const; + bool isInDisplay(int x, int y) const; + bool isInMinimap(int x, int y) const; - static void reload (int resWidth = -1, int resHeight = -1); - }; + static void reload(int resWidth = -1, int resHeight = -1); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/graphics/particle_type.cpp b/source/glest_game/graphics/particle_type.cpp index 12caa3560..f2abd326f 100644 --- a/source/glest_game/graphics/particle_type.cpp +++ b/source/glest_game/graphics/particle_type.cpp @@ -28,600 +28,594 @@ using namespace Shared::Graphics; using namespace Shared::Util; using namespace Shared::PlatformCommon; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class ParticleSystemType -// ===================================================== -const bool checkMemory = false; -static map memoryObjectList; + // ===================================================== + // class ParticleSystemType + // ===================================================== + const bool checkMemory = false; + static map memoryObjectList; -ParticleSystemType::ParticleSystemType() { - if(checkMemory) { - printf("++ Create ParticleSystemType [%p]\n",this); - memoryObjectList[this]++; - } - - teamcolorNoEnergy=false; - teamcolorEnergy=false; - alternations=0; - particleSystemStartDelay=0; - texture=NULL; - model=NULL; - minmaxEnabled=false; - minHp=0; - maxHp=0; - minmaxIsPercent=false; - - modelCycle=0; - size=0; - sizeNoEnergy=0; - speed=0; - speedUpRelative=0; - speedUpConstant=0; - gravity=0; - emissionRate=0; - energyMax=0; - energyVar=0; - -} - -ParticleSystemType::ParticleSystemType(const ParticleSystemType &src) { - if(checkMemory) { - printf("++ Create ParticleSystemType #2 [%p]\n",this); - memoryObjectList[this]++; - } - - copyAll(src); -} - -ParticleSystemType & ParticleSystemType::operator=(const ParticleSystemType &src) { - if(checkMemory) { - printf("++ Create ParticleSystemType #3 [%p]\n",this); - memoryObjectList[this]++; - } - - copyAll(src); - return *this; -} - -ParticleSystemType::~ParticleSystemType() { - if(checkMemory) { - printf("-- Delete ParticleSystemType [%p] type = [%s]\n",this,type.c_str()); - memoryObjectList[this]--; - assert(memoryObjectList[this] == 0); - } - for(Children::iterator it = children.begin(); it != children.end(); ++it) { - delete *it; - } - children.clear(); -} - -void ParticleSystemType::copyAll(const ParticleSystemType &src) { - this->type = src.type; - this->texture = src.texture; - this->model = src.model; - this->modelCycle = src.modelCycle; - this->primitive = src.primitive; - this->offset = src.offset; - this->color = src.color; - this->colorNoEnergy = src.colorNoEnergy; - this->size = src.size; - this->sizeNoEnergy = src.sizeNoEnergy; - this->speed = src.speed; - this->speedUpRelative = src.speedUpRelative; - this->speedUpConstant = src.speedUpConstant; - this->gravity = src.gravity; - this->emissionRate = src.emissionRate; - this->energyMax = src.energyMax; - this->energyVar = src.energyVar; - this->mode = src.mode; - this->teamcolorNoEnergy = src.teamcolorNoEnergy; - this->teamcolorEnergy = src.teamcolorEnergy; - this->alternations = src.alternations; - this->particleSystemStartDelay= src.particleSystemStartDelay; - for(Children::iterator it = children.begin(); it != children.end(); ++it) { - UnitParticleSystemType *child = *it; - - // Deep copy the child particles - UnitParticleSystemType *newCopy = new UnitParticleSystemType(); - *newCopy = *child; - children.push_back(newCopy); - } - - this->minmaxEnabled = src.minmaxEnabled; - this->minHp = src.minHp; - this->maxHp = src.maxHp; - this->minmaxIsPercent = src.minmaxIsPercent; -} - -void ParticleSystemType::load(const XmlNode *particleSystemNode, const string &dir, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath) { - - //texture - const XmlNode *textureNode= particleSystemNode->getChild("texture"); - bool textureEnabled= textureNode->getAttribute("value")->getBoolValue(); - - if(textureEnabled){ - texture= renderer->newTexture2D(rsGame); - if(texture) { - if(textureNode->getAttribute("luminance")->getBoolValue()){ - texture->setFormat(Texture::fAlpha); - texture->getPixmap()->init(1); + ParticleSystemType::ParticleSystemType() { + if (checkMemory) { + printf("++ Create ParticleSystemType [%p]\n", this); + memoryObjectList[this]++; } - else{ - texture->getPixmap()->init(4); + + teamcolorNoEnergy = false; + teamcolorEnergy = false; + alternations = 0; + particleSystemStartDelay = 0; + texture = NULL; + model = NULL; + minmaxEnabled = false; + minHp = 0; + maxHp = 0; + minmaxIsPercent = false; + + modelCycle = 0; + size = 0; + sizeNoEnergy = 0; + speed = 0; + speedUpRelative = 0; + speedUpConstant = 0; + gravity = 0; + emissionRate = 0; + energyMax = 0; + energyVar = 0; + + } + + ParticleSystemType::ParticleSystemType(const ParticleSystemType &src) { + if (checkMemory) { + printf("++ Create ParticleSystemType #2 [%p]\n", this); + memoryObjectList[this]++; + } + + copyAll(src); + } + + ParticleSystemType & ParticleSystemType::operator=(const ParticleSystemType &src) { + if (checkMemory) { + printf("++ Create ParticleSystemType #3 [%p]\n", this); + memoryObjectList[this]++; + } + + copyAll(src); + return *this; + } + + ParticleSystemType::~ParticleSystemType() { + if (checkMemory) { + printf("-- Delete ParticleSystemType [%p] type = [%s]\n", this, type.c_str()); + memoryObjectList[this]--; + assert(memoryObjectList[this] == 0); + } + for (Children::iterator it = children.begin(); it != children.end(); ++it) { + delete *it; + } + children.clear(); + } + + void ParticleSystemType::copyAll(const ParticleSystemType &src) { + this->type = src.type; + this->texture = src.texture; + this->model = src.model; + this->modelCycle = src.modelCycle; + this->primitive = src.primitive; + this->offset = src.offset; + this->color = src.color; + this->colorNoEnergy = src.colorNoEnergy; + this->size = src.size; + this->sizeNoEnergy = src.sizeNoEnergy; + this->speed = src.speed; + this->speedUpRelative = src.speedUpRelative; + this->speedUpConstant = src.speedUpConstant; + this->gravity = src.gravity; + this->emissionRate = src.emissionRate; + this->energyMax = src.energyMax; + this->energyVar = src.energyVar; + this->mode = src.mode; + this->teamcolorNoEnergy = src.teamcolorNoEnergy; + this->teamcolorEnergy = src.teamcolorEnergy; + this->alternations = src.alternations; + this->particleSystemStartDelay = src.particleSystemStartDelay; + for (Children::iterator it = children.begin(); it != children.end(); ++it) { + UnitParticleSystemType *child = *it; + + // Deep copy the child particles + UnitParticleSystemType *newCopy = new UnitParticleSystemType(); + *newCopy = *child; + children.push_back(newCopy); + } + + this->minmaxEnabled = src.minmaxEnabled; + this->minHp = src.minHp; + this->maxHp = src.maxHp; + this->minmaxIsPercent = src.minmaxIsPercent; + } + + void ParticleSystemType::load(const XmlNode *particleSystemNode, const string &dir, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath) { + + //texture + const XmlNode *textureNode = particleSystemNode->getChild("texture"); + bool textureEnabled = textureNode->getAttribute("value")->getBoolValue(); + + if (textureEnabled) { + texture = renderer->newTexture2D(rsGame); + if (texture) { + if (textureNode->getAttribute("luminance")->getBoolValue()) { + texture->setFormat(Texture::fAlpha); + texture->getPixmap()->init(1); + } else { + texture->getPixmap()->init(4); + } + } + string currentPath = dir; + endPathWithSlash(currentPath); + if (texture) { + texture->load(textureNode->getAttribute("path")->getRestrictedValue(currentPath)); + } + loadedFileList[textureNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(parentLoader, textureNode->getAttribute("path")->getRestrictedValue())); + } else { + texture = NULL; + } + + //model + if (particleSystemNode->hasChild("model")) { + const XmlNode *modelNode = particleSystemNode->getChild("model"); + bool modelEnabled = modelNode->getAttribute("value")->getBoolValue(); + if (modelEnabled) { + string currentPath = dir; + endPathWithSlash(currentPath); + + string path = modelNode->getAttribute("path")->getRestrictedValue(currentPath); + model = renderer->newModel(rsGame, path, false, &loadedFileList, &parentLoader); + loadedFileList[path].push_back(make_pair(parentLoader, modelNode->getAttribute("path")->getRestrictedValue())); + + if (modelNode->hasChild("cycles")) { + modelCycle = modelNode->getChild("cycles")->getAttribute("value")->getFloatValue(); + if (modelCycle < 0.0) + throw megaglest_runtime_error("negative model cycle value is bad"); + } + } + } else { + model = NULL; + } + + //primitive + const XmlNode *primitiveNode = particleSystemNode->getChild("primitive"); + primitive = primitiveNode->getAttribute("value")->getRestrictedValue(); + + //offset + const XmlNode *offsetNode = particleSystemNode->getChild("offset"); + offset.x = offsetNode->getAttribute("x")->getFloatValue(); + offset.y = offsetNode->getAttribute("y")->getFloatValue(); + offset.z = offsetNode->getAttribute("z")->getFloatValue(); + + //color + const XmlNode *colorNode = particleSystemNode->getChild("color"); + color.x = colorNode->getAttribute("red")->getFloatValue(0.f, 1.0f); + color.y = colorNode->getAttribute("green")->getFloatValue(0.f, 1.0f); + color.z = colorNode->getAttribute("blue")->getFloatValue(0.f, 1.0f); + color.w = colorNode->getAttribute("alpha")->getFloatValue(0.f, 1.0f); + + //color + const XmlNode *colorNoEnergyNode = particleSystemNode->getChild("color-no-energy"); + colorNoEnergy.x = colorNoEnergyNode->getAttribute("red")->getFloatValue(0.f, 1.0f); + colorNoEnergy.y = colorNoEnergyNode->getAttribute("green")->getFloatValue(0.f, 1.0f); + colorNoEnergy.z = colorNoEnergyNode->getAttribute("blue")->getFloatValue(0.f, 1.0f); + colorNoEnergy.w = colorNoEnergyNode->getAttribute("alpha")->getFloatValue(0.f, 1.0f); + + //size + const XmlNode *sizeNode = particleSystemNode->getChild("size"); + size = sizeNode->getAttribute("value")->getFloatValue(); + + //sizeNoEnergy + const XmlNode *sizeNoEnergyNode = particleSystemNode->getChild("size-no-energy"); + sizeNoEnergy = sizeNoEnergyNode->getAttribute("value")->getFloatValue(); + + //speed + const XmlNode *speedNode = particleSystemNode->getChild("speed"); + speed = speedNode->getAttribute("value")->getFloatValue() / GameConstants::updateFps; + + //speedUp + if (particleSystemNode->hasChild("speedUp")) { + const XmlNode *speedUpNode = particleSystemNode->getChild("speedUp"); + if (speedUpNode->hasAttribute("relative")) { + speedUpRelative = speedUpNode->getAttribute("relative")->getFloatValue(); + } + if (speedUpNode->hasAttribute("constant")) { + speedUpConstant = speedUpNode->getAttribute("constant")->getFloatValue(); + } + } + + //gravity + const XmlNode *gravityNode = particleSystemNode->getChild("gravity"); + gravity = gravityNode->getAttribute("value")->getFloatValue() / GameConstants::updateFps; + + //emission rate + const XmlNode *emissionRateNode = particleSystemNode->getChild("emission-rate"); + emissionRate = emissionRateNode->getAttribute("value")->getFloatValue(); + + //energy max + const XmlNode *energyMaxNode = particleSystemNode->getChild("energy-max"); + energyMax = energyMaxNode->getAttribute("value")->getIntValue(); + + //speed + const XmlNode *energyVarNode = particleSystemNode->getChild("energy-var"); + energyVar = energyVarNode->getAttribute("value")->getIntValue(); + + //teamcolorNoEnergy + if (particleSystemNode->hasChild("teamcolorNoEnergy")) { + const XmlNode *teamcolorNoEnergyNode = particleSystemNode->getChild("teamcolorNoEnergy"); + teamcolorNoEnergy = teamcolorNoEnergyNode->getAttribute("value")->getBoolValue(); + } + //teamcolorEnergy + if (particleSystemNode->hasChild("teamcolorEnergy")) { + const XmlNode *teamcolorEnergyNode = particleSystemNode->getChild("teamcolorEnergy"); + teamcolorEnergy = teamcolorEnergyNode->getAttribute("value")->getBoolValue(); + } + //alternations + if (particleSystemNode->hasChild("alternations")) { + const XmlNode *alternatingNode = particleSystemNode->getChild("alternations"); + alternations = alternatingNode->getAttribute("value")->getIntValue(); + } + //particleSystemStartDelay + if (particleSystemNode->hasChild("particleSystemStartDelay")) { + const XmlNode *node = particleSystemNode->getChild("particleSystemStartDelay"); + particleSystemStartDelay = node->getAttribute("value")->getIntValue(); + } + //mode + if (particleSystemNode->hasChild("mode")) { + const XmlNode *modeNode = particleSystemNode->getChild("mode"); + mode = modeNode->getAttribute("value")->getRestrictedValue(); + } else { + mode = "normal"; + } + + // child particles + if (particleSystemNode->hasChild("child-particles")) { + const XmlNode *childrenNode = particleSystemNode->getChild("child-particles"); + if (childrenNode->getAttribute("value")->getBoolValue()) { + for (unsigned int i = 0; i < childrenNode->getChildCount(); ++i) { + const XmlNode *particleFileNode = childrenNode->getChild("particle-file", i); + string path = particleFileNode->getAttribute("path")->getRestrictedValue(); + UnitParticleSystemType *unitParticleSystemType = new UnitParticleSystemType(); + string childPath = dir; + endPathWithSlash(childPath); + childPath += path; + string childDir = extractDirectoryPathFromFile(childPath); + unitParticleSystemType->load(particleFileNode, childDir, childPath, renderer, loadedFileList, parentLoader, techtreePath); + loadedFileList[childPath].push_back(make_pair(parentLoader, path)); + children.push_back(unitParticleSystemType); + } + } } } - string currentPath = dir; - endPathWithSlash(currentPath); - if(texture) { - texture->load(textureNode->getAttribute("path")->getRestrictedValue(currentPath)); + + void ParticleSystemType::setValues(AttackParticleSystem *ats) { + // add instances of all children; some settings will cascade to all children + for (Children::iterator i = children.begin(); i != children.end(); ++i) { + UnitParticleSystem *child = new UnitParticleSystem(); + child->setParticleOwner(ats->getParticleOwner()); + child->setParticleType((*i)); + (*i)->setValues(child); + ats->addChild(child); + child->setState(ParticleSystem::sPlay); + } + ats->setTexture(texture); + ats->setPrimitive(AttackParticleSystem::strToPrimitive(primitive)); + ats->setOffset(offset); + ats->setColor(color); + ats->setColorNoEnergy(colorNoEnergy); + ats->setSpeed(speed); + ats->setSpeedUpRelative(speedUpRelative); + ats->setSpeedUpConstant(speedUpConstant); + ats->setGravity(gravity); + ats->setParticleSize(size); + ats->setSizeNoEnergy(sizeNoEnergy); + ats->setEmissionRate(emissionRate); + ats->setMaxParticleEnergy(energyMax); + ats->setVarParticleEnergy(energyVar); + ats->setModel(model); + ats->setModelCycle(modelCycle); + ats->setTeamcolorNoEnergy(teamcolorNoEnergy); + ats->setTeamcolorEnergy(teamcolorEnergy); + ats->setAlternations(alternations); + ats->setParticleSystemStartDelay(particleSystemStartDelay); + ats->setBlendMode(ParticleSystem::strToBlendMode(mode)); } - loadedFileList[textureNode->getAttribute("path")->getRestrictedValue(currentPath)].push_back(make_pair(parentLoader,textureNode->getAttribute("path")->getRestrictedValue())); - } - else { - texture= NULL; - } - //model - if(particleSystemNode->hasChild("model")){ - const XmlNode *modelNode= particleSystemNode->getChild("model"); - bool modelEnabled= modelNode->getAttribute("value")->getBoolValue(); - if(modelEnabled) { - string currentPath = dir; - endPathWithSlash(currentPath); + void ParticleSystemType::loadGame(const XmlNode *rootNode) { + const XmlNode *particleSystemTypeNode = rootNode->getChild("ParticleSystemType"); - string path= modelNode->getAttribute("path")->getRestrictedValue(currentPath); - model= renderer->newModel(rsGame,path, false, &loadedFileList, &parentLoader); - loadedFileList[path].push_back(make_pair(parentLoader,modelNode->getAttribute("path")->getRestrictedValue())); + type = particleSystemTypeNode->getAttribute("type")->getIntValue(); - if(modelNode->hasChild("cycles")) { - modelCycle = modelNode->getChild("cycles")->getAttribute("value")->getFloatValue(); - if(modelCycle < 0.0) - throw megaglest_runtime_error("negative model cycle value is bad"); + modelCycle = particleSystemTypeNode->getAttribute("modelCycle")->getFloatValue(); + primitive = particleSystemTypeNode->getAttribute("primitive")->getValue(); + offset = Vec3f::strToVec3(particleSystemTypeNode->getAttribute("offset")->getValue()); + color = Vec4f::strToVec4(particleSystemTypeNode->getAttribute("color")->getValue()); + colorNoEnergy = Vec4f::strToVec4(particleSystemTypeNode->getAttribute("colorNoEnergy")->getValue()); + size = particleSystemTypeNode->getAttribute("size")->getFloatValue(); + sizeNoEnergy = particleSystemTypeNode->getAttribute("sizeNoEnergy")->getFloatValue(); + speed = particleSystemTypeNode->getAttribute("speed")->getFloatValue(); + if (particleSystemTypeNode->hasAttribute("speedUpRelative")) { + speedUpRelative = particleSystemTypeNode->getAttribute("speedUpRelative")->getFloatValue(); + } + if (particleSystemTypeNode->hasAttribute("speedUpConstant")) { + speedUpConstant = particleSystemTypeNode->getAttribute("speedUpConstant")->getFloatValue(); + } + gravity = particleSystemTypeNode->getAttribute("gravity")->getFloatValue(); + emissionRate = particleSystemTypeNode->getAttribute("emissionRate")->getFloatValue(); + energyMax = particleSystemTypeNode->getAttribute("energyMax")->getIntValue(); + energyVar = particleSystemTypeNode->getAttribute("energyVar")->getIntValue(); + mode = particleSystemTypeNode->getAttribute("mode")->getValue(); + teamcolorNoEnergy = (particleSystemTypeNode->getAttribute("teamcolorNoEnergy")->getIntValue() != 0); + teamcolorEnergy = (particleSystemTypeNode->getAttribute("teamcolorEnergy")->getIntValue() != 0); + alternations = particleSystemTypeNode->getAttribute("alternations")->getIntValue(); + particleSystemStartDelay = particleSystemTypeNode->getAttribute("particleSystemStartDelay")->getIntValue(); + + if (particleSystemTypeNode->hasChild("UnitParticleSystemType")) { + vector particleSystemTypeNodeList = particleSystemTypeNode->getChildList("UnitParticleSystemType"); + for (unsigned int i = 0; i < particleSystemTypeNodeList.size(); ++i) { + XmlNode *node = particleSystemTypeNodeList[i]; + + UnitParticleSystemType *child = new UnitParticleSystemType(); + child->loadGame(node); + children.push_back(child); + } + } + minmaxEnabled = (particleSystemTypeNode->getAttribute("minmaxEnabled")->getIntValue() != 0); + minHp = particleSystemTypeNode->getAttribute("minHp")->getIntValue(); + maxHp = particleSystemTypeNode->getAttribute("maxHp")->getIntValue(); + minmaxIsPercent = (particleSystemTypeNode->getAttribute("minmaxIsPercent")->getIntValue() != 0); + } + + void ParticleSystemType::saveGame(XmlNode *rootNode) { + std::map mapTagReplacements; + XmlNode *particleSystemTypeNode = rootNode->addChild("ParticleSystemType"); + + // string type; + particleSystemTypeNode->addAttribute("type", type, mapTagReplacements); + // Texture2D *texture; + // Model *model; + // float modelCycle; + particleSystemTypeNode->addAttribute("modelCycle", floatToStr(modelCycle, 6), mapTagReplacements); + // string primitive; + particleSystemTypeNode->addAttribute("primitive", primitive, mapTagReplacements); + // Vec3f offset; + particleSystemTypeNode->addAttribute("offset", offset.getString(), mapTagReplacements); + // Vec4f color; + particleSystemTypeNode->addAttribute("color", color.getString(), mapTagReplacements); + // Vec4f colorNoEnergy; + particleSystemTypeNode->addAttribute("colorNoEnergy", colorNoEnergy.getString(), mapTagReplacements); + // float size; + particleSystemTypeNode->addAttribute("size", floatToStr(size, 6), mapTagReplacements); + // float sizeNoEnergy; + particleSystemTypeNode->addAttribute("sizeNoEnergy", floatToStr(sizeNoEnergy, 6), mapTagReplacements); + // float speed; + particleSystemTypeNode->addAttribute("speed", floatToStr(speed, 6), mapTagReplacements); + // float speedUpRelative; + particleSystemTypeNode->addAttribute("speedUpRelative", floatToStr(speedUpRelative, 6), mapTagReplacements); + // float speedUpConstant; + particleSystemTypeNode->addAttribute("speedUpConstant", floatToStr(speedUpConstant, 6), mapTagReplacements); + // float gravity; + particleSystemTypeNode->addAttribute("gravity", floatToStr(gravity, 6), mapTagReplacements); + // float emissionRate; + particleSystemTypeNode->addAttribute("emissionRate", floatToStr(emissionRate, 6), mapTagReplacements); + // int energyMax; + particleSystemTypeNode->addAttribute("energyMax", intToStr(energyMax), mapTagReplacements); + // int energyVar; + particleSystemTypeNode->addAttribute("energyVar", intToStr(energyVar), mapTagReplacements); + // string mode; + particleSystemTypeNode->addAttribute("mode", mode, mapTagReplacements); + // bool teamcolorNoEnergy; + particleSystemTypeNode->addAttribute("teamcolorNoEnergy", intToStr(teamcolorNoEnergy), mapTagReplacements); + // bool teamcolorEnergy; + particleSystemTypeNode->addAttribute("teamcolorEnergy", intToStr(teamcolorEnergy), mapTagReplacements); + // int alternations; + particleSystemTypeNode->addAttribute("alternations", intToStr(alternations), mapTagReplacements); + // int particleSystemStartDelay; + particleSystemTypeNode->addAttribute("particleSystemStartDelay", intToStr(particleSystemStartDelay), mapTagReplacements); + // typedef std::list Children; + // Children children; + for (Children::iterator it = children.begin(); it != children.end(); ++it) { + (*it)->saveGame(particleSystemTypeNode); + } + // bool minmaxEnabled; + particleSystemTypeNode->addAttribute("minmaxEnabled", intToStr(minmaxEnabled), mapTagReplacements); + // int minHp; + particleSystemTypeNode->addAttribute("minHp", intToStr(minHp), mapTagReplacements); + // int maxHp; + particleSystemTypeNode->addAttribute("maxHp", intToStr(maxHp), mapTagReplacements); + // bool minmaxIsPercent; + particleSystemTypeNode->addAttribute("minmaxIsPercent", intToStr(minmaxIsPercent), mapTagReplacements); + } + + // =========================================================== + // class ParticleSystemTypeProjectile + // =========================================================== + + ParticleSystemTypeProjectile::ParticleSystemTypeProjectile() : ParticleSystemType() { + trajectorySpeed = 0.0f; + trajectoryScale = 0.0f; + trajectoryFrequency = 0.0f; + } + + void ParticleSystemTypeProjectile::load(const XmlNode* particleFileNode, const string &dir, const string &path, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath) { + + try { + XmlTree xmlTree; + + std::map mapExtraTagReplacementValues; + mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; + xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); + loadedFileList[path].push_back(make_pair(parentLoader, parentLoader)); + + const XmlNode *particleSystemNode = xmlTree.getRootNode(); + + if (particleFileNode) { + // immediate children in the particleFileNode will override the particleSystemNode + particleFileNode->setSuper(particleSystemNode); + particleSystemNode = particleFileNode; + } + + ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList, parentLoader, techtreePath); + + //trajectory values + const XmlNode *tajectoryNode = particleSystemNode->getChild("trajectory"); + trajectory = tajectoryNode->getAttribute("type")->getRestrictedValue(); + + //trajectory speed + const XmlNode *tajectorySpeedNode = tajectoryNode->getChild("speed"); + trajectorySpeed = tajectorySpeedNode->getAttribute("value")->getFloatValue() / (float) GameConstants::updateFps; + //printf("[%s] trajectorySpeed = %f\n",path.c_str(),trajectorySpeed); + + if (trajectory == "parabolic" || trajectory == "spiral") { + //trajectory scale + const XmlNode *tajectoryScaleNode = tajectoryNode->getChild("scale"); + trajectoryScale = tajectoryScaleNode->getAttribute("value")->getFloatValue(); + } else { + trajectoryScale = 1.0f; + } + + if (trajectory == "spiral") { + //trajectory frequency + const XmlNode *tajectoryFrequencyNode = tajectoryNode->getChild("frequency"); + trajectoryFrequency = tajectoryFrequencyNode->getAttribute("value")->getFloatValue(); + } else { + trajectoryFrequency = 1.0f; + } + } catch (const exception &e) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, e.what()); + throw megaglest_runtime_error("Error loading ParticleSystem: " + path + "\n" + e.what()); } } - } - else { - model= NULL; - } - //primitive - const XmlNode *primitiveNode= particleSystemNode->getChild("primitive"); - primitive= primitiveNode->getAttribute("value")->getRestrictedValue(); + ProjectileParticleSystem *ParticleSystemTypeProjectile::create(ParticleOwner *owner) { + ProjectileParticleSystem *ps = new ProjectileParticleSystem(); + ps->setParticleOwner(owner); + ParticleSystemType::setValues(ps); - //offset - const XmlNode *offsetNode= particleSystemNode->getChild("offset"); - offset.x= offsetNode->getAttribute("x")->getFloatValue(); - offset.y= offsetNode->getAttribute("y")->getFloatValue(); - offset.z= offsetNode->getAttribute("z")->getFloatValue(); + ps->setTrajectory(ProjectileParticleSystem::strToTrajectory(trajectory)); - //color - const XmlNode *colorNode= particleSystemNode->getChild("color"); - color.x= colorNode->getAttribute("red")->getFloatValue(0.f, 1.0f); - color.y= colorNode->getAttribute("green")->getFloatValue(0.f, 1.0f); - color.z= colorNode->getAttribute("blue")->getFloatValue(0.f, 1.0f); - color.w= colorNode->getAttribute("alpha")->getFloatValue(0.f, 1.0f); + //printf("Setting trajectorySpeed = %f\n",trajectorySpeed); + ps->setTrajectorySpeed(trajectorySpeed); + ps->setTrajectoryScale(trajectoryScale); + ps->setTrajectoryFrequency(trajectoryFrequency); - //color - const XmlNode *colorNoEnergyNode= particleSystemNode->getChild("color-no-energy"); - colorNoEnergy.x= colorNoEnergyNode->getAttribute("red")->getFloatValue(0.f, 1.0f); - colorNoEnergy.y= colorNoEnergyNode->getAttribute("green")->getFloatValue(0.f, 1.0f); - colorNoEnergy.z= colorNoEnergyNode->getAttribute("blue")->getFloatValue(0.f, 1.0f); - colorNoEnergy.w= colorNoEnergyNode->getAttribute("alpha")->getFloatValue(0.f, 1.0f); + ps->initParticleSystem(); - //size - const XmlNode *sizeNode= particleSystemNode->getChild("size"); - size= sizeNode->getAttribute("value")->getFloatValue(); + return ps; + } - //sizeNoEnergy - const XmlNode *sizeNoEnergyNode= particleSystemNode->getChild("size-no-energy"); - sizeNoEnergy= sizeNoEnergyNode->getAttribute("value")->getFloatValue(); + void ParticleSystemTypeProjectile::saveGame(XmlNode *rootNode) { + ParticleSystemType::saveGame(rootNode); - //speed - const XmlNode *speedNode= particleSystemNode->getChild("speed"); - speed= speedNode->getAttribute("value")->getFloatValue()/GameConstants::updateFps; + std::map mapTagReplacements; + XmlNode *particleSystemTypeProjectileNode = rootNode->addChild("ParticleSystemTypeProjectile"); - //speedUp - if(particleSystemNode->hasChild("speedUp")){ - const XmlNode *speedUpNode= particleSystemNode->getChild("speedUp"); - if(speedUpNode->hasAttribute("relative")){ - speedUpRelative= speedUpNode->getAttribute("relative")->getFloatValue(); - } - if(speedUpNode->hasAttribute("constant")){ - speedUpConstant= speedUpNode->getAttribute("constant")->getFloatValue(); - } - } + // string trajectory; + particleSystemTypeProjectileNode->addAttribute("trajectory", trajectory, mapTagReplacements); + // float trajectorySpeed; + particleSystemTypeProjectileNode->addAttribute("trajectorySpeed", floatToStr(trajectorySpeed, 6), mapTagReplacements); + // float trajectoryScale; + particleSystemTypeProjectileNode->addAttribute("trajectoryScale", floatToStr(trajectoryScale, 6), mapTagReplacements); + // float trajectoryFrequency; + particleSystemTypeProjectileNode->addAttribute("trajectoryFrequency", floatToStr(trajectoryFrequency, 6), mapTagReplacements); + } - //gravity - const XmlNode *gravityNode= particleSystemNode->getChild("gravity"); - gravity= gravityNode->getAttribute("value")->getFloatValue()/GameConstants::updateFps; + // =========================================================== + // class ParticleSystemTypeSplash + // =========================================================== - //emission rate - const XmlNode *emissionRateNode= particleSystemNode->getChild("emission-rate"); - emissionRate= emissionRateNode->getAttribute("value")->getFloatValue(); + ParticleSystemTypeSplash::ParticleSystemTypeSplash() { + emissionRateFade = 0.0f; + verticalSpreadA = 0.0f; + verticalSpreadB = 0.0f; + horizontalSpreadA = 0.0f; + horizontalSpreadB = 0.0f; + } - //energy max - const XmlNode *energyMaxNode= particleSystemNode->getChild("energy-max"); - energyMax= energyMaxNode->getAttribute("value")->getIntValue(); + void ParticleSystemTypeSplash::load(const XmlNode* particleFileNode, const string &dir, const string &path, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath) { - //speed - const XmlNode *energyVarNode= particleSystemNode->getChild("energy-var"); - energyVar= energyVarNode->getAttribute("value")->getIntValue(); + try { + XmlTree xmlTree; - //teamcolorNoEnergy - if(particleSystemNode->hasChild("teamcolorNoEnergy")){ - const XmlNode *teamcolorNoEnergyNode= particleSystemNode->getChild("teamcolorNoEnergy"); - teamcolorNoEnergy= teamcolorNoEnergyNode->getAttribute("value")->getBoolValue(); - } - //teamcolorEnergy - if(particleSystemNode->hasChild("teamcolorEnergy")){ - const XmlNode *teamcolorEnergyNode= particleSystemNode->getChild("teamcolorEnergy"); - teamcolorEnergy= teamcolorEnergyNode->getAttribute("value")->getBoolValue(); - } - //alternations - if(particleSystemNode->hasChild("alternations")){ - const XmlNode *alternatingNode= particleSystemNode->getChild("alternations"); - alternations= alternatingNode->getAttribute("value")->getIntValue(); - } - //particleSystemStartDelay - if(particleSystemNode->hasChild("particleSystemStartDelay")){ - const XmlNode *node= particleSystemNode->getChild("particleSystemStartDelay"); - particleSystemStartDelay= node->getAttribute("value")->getIntValue(); - } - //mode - if(particleSystemNode->hasChild("mode")) { - const XmlNode *modeNode= particleSystemNode->getChild("mode"); - mode= modeNode->getAttribute("value")->getRestrictedValue(); - } - else { - mode="normal"; - } + std::map mapExtraTagReplacementValues; + mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; + xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); + loadedFileList[path].push_back(make_pair(parentLoader, parentLoader)); - // child particles - if(particleSystemNode->hasChild("child-particles")) { - const XmlNode *childrenNode= particleSystemNode->getChild("child-particles"); - if(childrenNode->getAttribute("value")->getBoolValue()) { - for(unsigned int i = 0; i < childrenNode->getChildCount(); ++i) { - const XmlNode *particleFileNode= childrenNode->getChild("particle-file",i); - string path= particleFileNode->getAttribute("path")->getRestrictedValue(); - UnitParticleSystemType *unitParticleSystemType= new UnitParticleSystemType(); - string childPath= dir; - endPathWithSlash(childPath); - childPath += path; - string childDir = extractDirectoryPathFromFile(childPath); - unitParticleSystemType->load(particleFileNode,childDir,childPath,renderer,loadedFileList,parentLoader,techtreePath); - loadedFileList[childPath].push_back(make_pair(parentLoader,path)); - children.push_back(unitParticleSystemType); + const XmlNode *particleSystemNode = xmlTree.getRootNode(); + + if (particleFileNode) { + // immediate children in the particleFileNode will override the particleSystemNode + particleFileNode->setSuper(particleSystemNode); + particleSystemNode = particleFileNode; + } + + ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList, parentLoader, techtreePath); + + //emission rate fade + const XmlNode *emissionRateFadeNode = particleSystemNode->getChild("emission-rate-fade"); + emissionRateFade = emissionRateFadeNode->getAttribute("value")->getFloatValue(); + + //spread values + const XmlNode *verticalSpreadNode = particleSystemNode->getChild("vertical-spread"); + verticalSpreadA = verticalSpreadNode->getAttribute("a")->getFloatValue(0.0f, 1.0f); + verticalSpreadB = verticalSpreadNode->getAttribute("b")->getFloatValue(-1.0f, 1.0f); + + const XmlNode *horizontalSpreadNode = particleSystemNode->getChild("horizontal-spread"); + horizontalSpreadA = horizontalSpreadNode->getAttribute("a")->getFloatValue(0.0f, 1.0f); + horizontalSpreadB = horizontalSpreadNode->getAttribute("b")->getFloatValue(-1.0f, 1.0f); + } catch (const exception &e) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, e.what()); + throw megaglest_runtime_error("Error loading ParticleSystem: " + path + "\n" + e.what()); } } - } -} -void ParticleSystemType::setValues(AttackParticleSystem *ats){ - // add instances of all children; some settings will cascade to all children - for(Children::iterator i=children.begin(); i!=children.end(); ++i){ - UnitParticleSystem *child = new UnitParticleSystem(); - child->setParticleOwner(ats->getParticleOwner()); - child->setParticleType((*i)); - (*i)->setValues(child); - ats->addChild(child); - child->setState(ParticleSystem::sPlay); - } - ats->setTexture(texture); - ats->setPrimitive(AttackParticleSystem::strToPrimitive(primitive)); - ats->setOffset(offset); - ats->setColor(color); - ats->setColorNoEnergy(colorNoEnergy); - ats->setSpeed(speed); - ats->setSpeedUpRelative(speedUpRelative); - ats->setSpeedUpConstant(speedUpConstant); - ats->setGravity(gravity); - ats->setParticleSize(size); - ats->setSizeNoEnergy(sizeNoEnergy); - ats->setEmissionRate(emissionRate); - ats->setMaxParticleEnergy(energyMax); - ats->setVarParticleEnergy(energyVar); - ats->setModel(model); - ats->setModelCycle(modelCycle); - ats->setTeamcolorNoEnergy(teamcolorNoEnergy); - ats->setTeamcolorEnergy(teamcolorEnergy); - ats->setAlternations(alternations); - ats->setParticleSystemStartDelay(particleSystemStartDelay); - ats->setBlendMode(ParticleSystem::strToBlendMode(mode)); -} + SplashParticleSystem *ParticleSystemTypeSplash::create(ParticleOwner *owner) { + SplashParticleSystem *ps = new SplashParticleSystem(); + ps->setParticleOwner(owner); + ParticleSystemType::setValues(ps); -void ParticleSystemType::loadGame(const XmlNode *rootNode) { - const XmlNode *particleSystemTypeNode = rootNode->getChild("ParticleSystemType"); + ps->setEmissionRateFade(emissionRateFade); + ps->setVerticalSpreadA(verticalSpreadA); + ps->setVerticalSpreadB(verticalSpreadB); + ps->setHorizontalSpreadA(horizontalSpreadA); + ps->setHorizontalSpreadB(horizontalSpreadB); - type = particleSystemTypeNode->getAttribute("type")->getIntValue(); + ps->initParticleSystem(); - modelCycle = particleSystemTypeNode->getAttribute("modelCycle")->getFloatValue(); - primitive = particleSystemTypeNode->getAttribute("primitive")->getValue(); - offset = Vec3f::strToVec3(particleSystemTypeNode->getAttribute("offset")->getValue()); - color = Vec4f::strToVec4(particleSystemTypeNode->getAttribute("color")->getValue()); - colorNoEnergy = Vec4f::strToVec4(particleSystemTypeNode->getAttribute("colorNoEnergy")->getValue()); - size = particleSystemTypeNode->getAttribute("size")->getFloatValue(); - sizeNoEnergy = particleSystemTypeNode->getAttribute("sizeNoEnergy")->getFloatValue(); - speed = particleSystemTypeNode->getAttribute("speed")->getFloatValue(); - if(particleSystemTypeNode->hasAttribute("speedUpRelative")){ - speedUpRelative = particleSystemTypeNode->getAttribute("speedUpRelative")->getFloatValue(); - } - if(particleSystemTypeNode->hasAttribute("speedUpConstant")){ - speedUpConstant = particleSystemTypeNode->getAttribute("speedUpConstant")->getFloatValue(); - } - gravity = particleSystemTypeNode->getAttribute("gravity")->getFloatValue(); - emissionRate = particleSystemTypeNode->getAttribute("emissionRate")->getFloatValue(); - energyMax = particleSystemTypeNode->getAttribute("energyMax")->getIntValue(); - energyVar = particleSystemTypeNode->getAttribute("energyVar")->getIntValue(); - mode = particleSystemTypeNode->getAttribute("mode")->getValue(); - teamcolorNoEnergy = (particleSystemTypeNode->getAttribute("teamcolorNoEnergy")->getIntValue() != 0); - teamcolorEnergy = (particleSystemTypeNode->getAttribute("teamcolorEnergy")->getIntValue() != 0); - alternations = particleSystemTypeNode->getAttribute("alternations")->getIntValue(); - particleSystemStartDelay = particleSystemTypeNode->getAttribute("particleSystemStartDelay")->getIntValue(); - - if(particleSystemTypeNode->hasChild("UnitParticleSystemType")) { - vector particleSystemTypeNodeList = particleSystemTypeNode->getChildList("UnitParticleSystemType"); - for(unsigned int i = 0; i < particleSystemTypeNodeList.size(); ++i) { - XmlNode *node = particleSystemTypeNodeList[i]; - - UnitParticleSystemType *child = new UnitParticleSystemType(); - child->loadGame(node); - children.push_back(child); - } - } - minmaxEnabled = (particleSystemTypeNode->getAttribute("minmaxEnabled")->getIntValue() != 0); - minHp = particleSystemTypeNode->getAttribute("minHp")->getIntValue(); - maxHp = particleSystemTypeNode->getAttribute("maxHp")->getIntValue(); - minmaxIsPercent = (particleSystemTypeNode->getAttribute("minmaxIsPercent")->getIntValue() != 0); -} - -void ParticleSystemType::saveGame(XmlNode *rootNode) { - std::map mapTagReplacements; - XmlNode *particleSystemTypeNode = rootNode->addChild("ParticleSystemType"); - -// string type; - particleSystemTypeNode->addAttribute("type",type, mapTagReplacements); -// Texture2D *texture; -// Model *model; -// float modelCycle; - particleSystemTypeNode->addAttribute("modelCycle",floatToStr(modelCycle,6), mapTagReplacements); -// string primitive; - particleSystemTypeNode->addAttribute("primitive",primitive, mapTagReplacements); -// Vec3f offset; - particleSystemTypeNode->addAttribute("offset",offset.getString(), mapTagReplacements); -// Vec4f color; - particleSystemTypeNode->addAttribute("color",color.getString(), mapTagReplacements); -// Vec4f colorNoEnergy; - particleSystemTypeNode->addAttribute("colorNoEnergy",colorNoEnergy.getString(), mapTagReplacements); -// float size; - particleSystemTypeNode->addAttribute("size",floatToStr(size,6), mapTagReplacements); -// float sizeNoEnergy; - particleSystemTypeNode->addAttribute("sizeNoEnergy",floatToStr(sizeNoEnergy,6), mapTagReplacements); -// float speed; - particleSystemTypeNode->addAttribute("speed",floatToStr(speed,6), mapTagReplacements); -// float speedUpRelative; - particleSystemTypeNode->addAttribute("speedUpRelative",floatToStr(speedUpRelative,6), mapTagReplacements); -// float speedUpConstant; - particleSystemTypeNode->addAttribute("speedUpConstant",floatToStr(speedUpConstant,6), mapTagReplacements); -// float gravity; - particleSystemTypeNode->addAttribute("gravity",floatToStr(gravity,6), mapTagReplacements); -// float emissionRate; - particleSystemTypeNode->addAttribute("emissionRate",floatToStr(emissionRate,6), mapTagReplacements); -// int energyMax; - particleSystemTypeNode->addAttribute("energyMax",intToStr(energyMax), mapTagReplacements); -// int energyVar; - particleSystemTypeNode->addAttribute("energyVar",intToStr(energyVar), mapTagReplacements); -// string mode; - particleSystemTypeNode->addAttribute("mode",mode, mapTagReplacements); -// bool teamcolorNoEnergy; - particleSystemTypeNode->addAttribute("teamcolorNoEnergy",intToStr(teamcolorNoEnergy), mapTagReplacements); -// bool teamcolorEnergy; - particleSystemTypeNode->addAttribute("teamcolorEnergy",intToStr(teamcolorEnergy), mapTagReplacements); -// int alternations; - particleSystemTypeNode->addAttribute("alternations",intToStr(alternations), mapTagReplacements); -// int particleSystemStartDelay; - particleSystemTypeNode->addAttribute("particleSystemStartDelay",intToStr(particleSystemStartDelay), mapTagReplacements); -// typedef std::list Children; -// Children children; - for(Children::iterator it = children.begin(); it != children.end(); ++it) { - (*it)->saveGame(particleSystemTypeNode); - } -// bool minmaxEnabled; - particleSystemTypeNode->addAttribute("minmaxEnabled",intToStr(minmaxEnabled), mapTagReplacements); -// int minHp; - particleSystemTypeNode->addAttribute("minHp",intToStr(minHp), mapTagReplacements); -// int maxHp; - particleSystemTypeNode->addAttribute("maxHp",intToStr(maxHp), mapTagReplacements); -// bool minmaxIsPercent; - particleSystemTypeNode->addAttribute("minmaxIsPercent",intToStr(minmaxIsPercent), mapTagReplacements); -} - -// =========================================================== -// class ParticleSystemTypeProjectile -// =========================================================== - -ParticleSystemTypeProjectile::ParticleSystemTypeProjectile() : ParticleSystemType() { - trajectorySpeed = 0.0f; - trajectoryScale = 0.0f; - trajectoryFrequency = 0.0f; -} - -void ParticleSystemTypeProjectile::load(const XmlNode* particleFileNode, const string &dir, const string &path, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath) { - - try{ - XmlTree xmlTree; - - std::map mapExtraTagReplacementValues; - mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; - xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); - loadedFileList[path].push_back(make_pair(parentLoader,parentLoader)); - - const XmlNode *particleSystemNode= xmlTree.getRootNode(); - - if(particleFileNode){ - // immediate children in the particleFileNode will override the particleSystemNode - particleFileNode->setSuper(particleSystemNode); - particleSystemNode= particleFileNode; + return ps; } - ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList,parentLoader, techtreePath); + void ParticleSystemTypeSplash::saveGame(XmlNode *rootNode) { + ParticleSystemType::saveGame(rootNode); - //trajectory values - const XmlNode *tajectoryNode= particleSystemNode->getChild("trajectory"); - trajectory= tajectoryNode->getAttribute("type")->getRestrictedValue(); + std::map mapTagReplacements; + XmlNode *particleSystemTypeSplashNode = rootNode->addChild("ParticleSystemTypeSplash"); - //trajectory speed - const XmlNode *tajectorySpeedNode= tajectoryNode->getChild("speed"); - trajectorySpeed= tajectorySpeedNode->getAttribute("value")->getFloatValue() / (float)GameConstants::updateFps; - //printf("[%s] trajectorySpeed = %f\n",path.c_str(),trajectorySpeed); - - if(trajectory=="parabolic" || trajectory=="spiral"){ - //trajectory scale - const XmlNode *tajectoryScaleNode= tajectoryNode->getChild("scale"); - trajectoryScale= tajectoryScaleNode->getAttribute("value")->getFloatValue(); - } - else{ - trajectoryScale= 1.0f; + // float emissionRateFade; + particleSystemTypeSplashNode->addAttribute("emissionRateFade", floatToStr(emissionRateFade, 6), mapTagReplacements); + // float verticalSpreadA; + particleSystemTypeSplashNode->addAttribute("verticalSpreadA", floatToStr(verticalSpreadA, 6), mapTagReplacements); + // float verticalSpreadB; + particleSystemTypeSplashNode->addAttribute("verticalSpreadB", floatToStr(verticalSpreadB, 6), mapTagReplacements); + // float horizontalSpreadA; + particleSystemTypeSplashNode->addAttribute("horizontalSpreadA", floatToStr(horizontalSpreadA, 6), mapTagReplacements); + // float horizontalSpreadB; + particleSystemTypeSplashNode->addAttribute("horizontalSpreadB", floatToStr(horizontalSpreadB, 6), mapTagReplacements); } - if(trajectory=="spiral"){ - //trajectory frequency - const XmlNode *tajectoryFrequencyNode= tajectoryNode->getChild("frequency"); - trajectoryFrequency= tajectoryFrequencyNode->getAttribute("value")->getFloatValue(); - } - else{ - trajectoryFrequency= 1.0f; - } } - catch(const exception &e){ - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); - throw megaglest_runtime_error("Error loading ParticleSystem: "+ path + "\n" +e.what()); - } -} - -ProjectileParticleSystem *ParticleSystemTypeProjectile::create(ParticleOwner *owner) { - ProjectileParticleSystem *ps= new ProjectileParticleSystem(); - ps->setParticleOwner(owner); - ParticleSystemType::setValues(ps); - - ps->setTrajectory(ProjectileParticleSystem::strToTrajectory(trajectory)); - - //printf("Setting trajectorySpeed = %f\n",trajectorySpeed); - ps->setTrajectorySpeed(trajectorySpeed); - ps->setTrajectoryScale(trajectoryScale); - ps->setTrajectoryFrequency(trajectoryFrequency); - - ps->initParticleSystem(); - - return ps; -} - -void ParticleSystemTypeProjectile::saveGame(XmlNode *rootNode) { - ParticleSystemType::saveGame(rootNode); - - std::map mapTagReplacements; - XmlNode *particleSystemTypeProjectileNode = rootNode->addChild("ParticleSystemTypeProjectile"); - -// string trajectory; - particleSystemTypeProjectileNode->addAttribute("trajectory",trajectory, mapTagReplacements); -// float trajectorySpeed; - particleSystemTypeProjectileNode->addAttribute("trajectorySpeed",floatToStr(trajectorySpeed,6), mapTagReplacements); -// float trajectoryScale; - particleSystemTypeProjectileNode->addAttribute("trajectoryScale",floatToStr(trajectoryScale,6), mapTagReplacements); -// float trajectoryFrequency; - particleSystemTypeProjectileNode->addAttribute("trajectoryFrequency",floatToStr(trajectoryFrequency,6), mapTagReplacements); -} - -// =========================================================== -// class ParticleSystemTypeSplash -// =========================================================== - -ParticleSystemTypeSplash::ParticleSystemTypeSplash() { - emissionRateFade = 0.0f; - verticalSpreadA = 0.0f; - verticalSpreadB = 0.0f; - horizontalSpreadA = 0.0f; - horizontalSpreadB = 0.0f; -} - -void ParticleSystemTypeSplash::load(const XmlNode* particleFileNode, const string &dir, const string &path, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath) { - - try{ - XmlTree xmlTree; - - std::map mapExtraTagReplacementValues; - mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; - xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); - loadedFileList[path].push_back(make_pair(parentLoader,parentLoader)); - - const XmlNode *particleSystemNode= xmlTree.getRootNode(); - - if(particleFileNode){ - // immediate children in the particleFileNode will override the particleSystemNode - particleFileNode->setSuper(particleSystemNode); - particleSystemNode= particleFileNode; - } - - ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList, parentLoader, techtreePath); - - //emission rate fade - const XmlNode *emissionRateFadeNode= particleSystemNode->getChild("emission-rate-fade"); - emissionRateFade= emissionRateFadeNode->getAttribute("value")->getFloatValue(); - - //spread values - const XmlNode *verticalSpreadNode= particleSystemNode->getChild("vertical-spread"); - verticalSpreadA= verticalSpreadNode->getAttribute("a")->getFloatValue(0.0f, 1.0f); - verticalSpreadB= verticalSpreadNode->getAttribute("b")->getFloatValue(-1.0f, 1.0f); - - const XmlNode *horizontalSpreadNode= particleSystemNode->getChild("horizontal-spread"); - horizontalSpreadA= horizontalSpreadNode->getAttribute("a")->getFloatValue(0.0f, 1.0f); - horizontalSpreadB= horizontalSpreadNode->getAttribute("b")->getFloatValue(-1.0f, 1.0f); - } - catch(const exception &e){ - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); - throw megaglest_runtime_error("Error loading ParticleSystem: "+ path + "\n" +e.what()); - } -} - -SplashParticleSystem *ParticleSystemTypeSplash::create(ParticleOwner *owner) { - SplashParticleSystem *ps= new SplashParticleSystem(); - ps->setParticleOwner(owner); - ParticleSystemType::setValues(ps); - - ps->setEmissionRateFade(emissionRateFade); - ps->setVerticalSpreadA(verticalSpreadA); - ps->setVerticalSpreadB(verticalSpreadB); - ps->setHorizontalSpreadA(horizontalSpreadA); - ps->setHorizontalSpreadB(horizontalSpreadB); - - ps->initParticleSystem(); - - return ps; -} - -void ParticleSystemTypeSplash::saveGame(XmlNode *rootNode) { - ParticleSystemType::saveGame(rootNode); - - std::map mapTagReplacements; - XmlNode *particleSystemTypeSplashNode = rootNode->addChild("ParticleSystemTypeSplash"); - -// float emissionRateFade; - particleSystemTypeSplashNode->addAttribute("emissionRateFade",floatToStr(emissionRateFade,6), mapTagReplacements); -// float verticalSpreadA; - particleSystemTypeSplashNode->addAttribute("verticalSpreadA",floatToStr(verticalSpreadA,6), mapTagReplacements); -// float verticalSpreadB; - particleSystemTypeSplashNode->addAttribute("verticalSpreadB",floatToStr(verticalSpreadB,6), mapTagReplacements); -// float horizontalSpreadA; - particleSystemTypeSplashNode->addAttribute("horizontalSpreadA",floatToStr(horizontalSpreadA,6), mapTagReplacements); -// float horizontalSpreadB; - particleSystemTypeSplashNode->addAttribute("horizontalSpreadB",floatToStr(horizontalSpreadB,6), mapTagReplacements); -} - -}}//end mamespace +}//end mamespace diff --git a/source/glest_game/graphics/particle_type.h b/source/glest_game/graphics/particle_type.h index 0291cbbf7..d8b99df78 100644 --- a/source/glest_game/graphics/particle_type.h +++ b/source/glest_game/graphics/particle_type.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_PARTICLETYPE_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -30,139 +30,163 @@ using std::string; using namespace Shared::Graphics; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -using Shared::Graphics::ParticleSystem; -using Shared::Graphics::UnitParticleSystem; -using Shared::Graphics::AttackParticleSystem; -using Shared::Graphics::ProjectileParticleSystem; -using Shared::Graphics::SplashParticleSystem; -using Shared::Graphics::Texture2D; -using Shared::Graphics::Vec3f; -using Shared::Graphics::Vec4f; -using Shared::Graphics::Model; -using Shared::Util::MultiFactory; -using Shared::Xml::XmlNode; + using Shared::Graphics::ParticleSystem; + using Shared::Graphics::UnitParticleSystem; + using Shared::Graphics::AttackParticleSystem; + using Shared::Graphics::ProjectileParticleSystem; + using Shared::Graphics::SplashParticleSystem; + using Shared::Graphics::Texture2D; + using Shared::Graphics::Vec3f; + using Shared::Graphics::Vec4f; + using Shared::Graphics::Model; + using Shared::Util::MultiFactory; + using Shared::Xml::XmlNode; -class UnitParticleSystemType; + class UnitParticleSystemType; -// =========================================================== -// class ParticleSystemType -// -/// A type of particle system -// =========================================================== + // =========================================================== + // class ParticleSystemType + // + /// A type of particle system + // =========================================================== -class ParticleSystemType : public ParticleSystemTypeInterface { -protected: - string type; - Texture2D *texture; - Model *model; - float modelCycle; - string primitive; - Vec3f offset; - Vec4f color; - Vec4f colorNoEnergy; - float size; - float sizeNoEnergy; - float speed; - float speedUpRelative; - float speedUpConstant; - float gravity; - float emissionRate; - int energyMax; - int energyVar; - string mode; - bool teamcolorNoEnergy; - bool teamcolorEnergy; - int alternations; - int particleSystemStartDelay; - typedef std::list Children; - Children children; + class ParticleSystemType : public ParticleSystemTypeInterface { + protected: + string type; + Texture2D *texture; + Model *model; + float modelCycle; + string primitive; + Vec3f offset; + Vec4f color; + Vec4f colorNoEnergy; + float size; + float sizeNoEnergy; + float speed; + float speedUpRelative; + float speedUpConstant; + float gravity; + float emissionRate; + int energyMax; + int energyVar; + string mode; + bool teamcolorNoEnergy; + bool teamcolorEnergy; + int alternations; + int particleSystemStartDelay; + typedef std::list Children; + Children children; - bool minmaxEnabled; - int minHp; - int maxHp; - bool minmaxIsPercent; + bool minmaxEnabled; + int minHp; + int maxHp; + bool minmaxIsPercent; - void copyAll(const ParticleSystemType &src); -public: + void copyAll(const ParticleSystemType &src); + public: - ParticleSystemType(); - virtual ~ParticleSystemType(); + ParticleSystemType(); + virtual ~ParticleSystemType(); - ParticleSystemType & operator=(const ParticleSystemType &src); - ParticleSystemType(const ParticleSystemType &src); + ParticleSystemType & operator=(const ParticleSystemType &src); + ParticleSystemType(const ParticleSystemType &src); - void load(const XmlNode *particleSystemNode, const string &dir, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath); - void setValues(AttackParticleSystem *ats); - bool hasTexture() const { return(texture != NULL); } - bool hasModel() const { return(model != NULL); } + void load(const XmlNode *particleSystemNode, const string &dir, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath); + void setValues(AttackParticleSystem *ats); + bool hasTexture() const { + return(texture != NULL); + } + bool hasModel() const { + return(model != NULL); + } - bool getMinmaxEnabled() const { return minmaxEnabled;} - int getMinHp() const { return minHp;} - int getMaxHp() const { return maxHp;} - bool getMinmaxIsPercent() const { return minmaxIsPercent; } + bool getMinmaxEnabled() const { + return minmaxEnabled; + } + int getMinHp() const { + return minHp; + } + int getMaxHp() const { + return maxHp; + } + bool getMinmaxIsPercent() const { + return minmaxIsPercent; + } - void setMinmaxEnabled(bool value) { minmaxEnabled=value;} - void setMinHp(int value) { minHp=value;} - void setMaxHp(int value) { maxHp=value;} - void setMinmaxIsPercent(bool value) { minmaxIsPercent=value; } + void setMinmaxEnabled(bool value) { + minmaxEnabled = value; + } + void setMinHp(int value) { + minHp = value; + } + void setMaxHp(int value) { + maxHp = value; + } + void setMinmaxIsPercent(bool value) { + minmaxIsPercent = value; + } - string getType() const { return type; }; + string getType() const { + return type; + }; - virtual void saveGame(XmlNode *rootNode); - virtual void loadGame(const XmlNode *rootNode); + virtual void saveGame(XmlNode *rootNode); + virtual void loadGame(const XmlNode *rootNode); -protected: + protected: -}; + }; -// =========================================================== -// class ParticleSystemTypeProjectile -// =========================================================== + // =========================================================== + // class ParticleSystemTypeProjectile + // =========================================================== -class ParticleSystemTypeProjectile: public ParticleSystemType{ -private: - string trajectory; - float trajectorySpeed; - float trajectoryScale; - float trajectoryFrequency; + class ParticleSystemTypeProjectile : public ParticleSystemType { + private: + string trajectory; + float trajectorySpeed; + float trajectoryScale; + float trajectoryFrequency; -public: - ParticleSystemTypeProjectile(); - void load(const XmlNode *particleFileNode, const string &dir, const string &path, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath); - ProjectileParticleSystem *create(ParticleOwner *owner); + public: + ParticleSystemTypeProjectile(); + void load(const XmlNode *particleFileNode, const string &dir, const string &path, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath); + ProjectileParticleSystem *create(ParticleOwner *owner); - virtual void saveGame(XmlNode *rootNode); -}; + virtual void saveGame(XmlNode *rootNode); + }; -// =========================================================== -// class ParticleSystemTypeSplash -// =========================================================== + // =========================================================== + // class ParticleSystemTypeSplash + // =========================================================== -class ParticleSystemTypeSplash: public ParticleSystemType { -public: + class ParticleSystemTypeSplash : public ParticleSystemType { + public: - ParticleSystemTypeSplash(); - void load(const XmlNode *particleFileNode, const string &dir, const string &path, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath); - SplashParticleSystem *create(ParticleOwner *owner); + ParticleSystemTypeSplash(); + void load(const XmlNode *particleFileNode, const string &dir, const string &path, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath); + SplashParticleSystem *create(ParticleOwner *owner); - virtual void saveGame(XmlNode *rootNode); + virtual void saveGame(XmlNode *rootNode); -private: - float emissionRateFade; - float verticalSpreadA; - float verticalSpreadB; - float horizontalSpreadA; - float horizontalSpreadB; -}; + private: + float emissionRateFade; + float verticalSpreadA; + float verticalSpreadB; + float horizontalSpreadA; + float horizontalSpreadB; + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index d1539b82e..21553fef6 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -36,5315 +36,4818 @@ using namespace Shared::Graphics::Gl; using namespace Shared::Util; using namespace Shared::Graphics; -namespace Glest { namespace Game{ +namespace Glest { + namespace Game { -uint32 Renderer::SurfaceData::nextUniqueId = 1; -bool Renderer::renderText3DEnabled = true; + uint32 Renderer::SurfaceData::nextUniqueId = 1; + bool Renderer::renderText3DEnabled = true; -//const float SKIP_INTERPOLATION_DISTANCE = 20.0f; -const string DEFAULT_CHAR_FOR_WIDTH_CALC = "V"; + //const float SKIP_INTERPOLATION_DISTANCE = 20.0f; + const string DEFAULT_CHAR_FOR_WIDTH_CALC = "V"; -enum PROJECTION_TO_INFINITY { - pti_D_IS_ZERO, - pti_N_OVER_D_IS_OUTSIDE -}; - -// ===================================================== -// class MeshCallbackTeamColor -// ===================================================== - -bool MeshCallbackTeamColor::noTeamColors = false; - -void MeshCallbackTeamColor::execute(const Mesh *mesh) { - //team color - if( mesh->getCustomTexture() && teamTexture != NULL && - MeshCallbackTeamColor::noTeamColors == false) { - //texture 0 - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - //set color to interpolation - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); - - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - //texture 1 - glActiveTexture(GL_TEXTURE1); - glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); - glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, static_cast(teamTexture)->getHandle()); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - glActiveTexture(GL_TEXTURE0); - } - else { - glActiveTexture(GL_TEXTURE1); - glDisable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - } -} - -// =========================================================== -// class Renderer -// =========================================================== - -// ===================== PUBLIC ======================== - -const int Renderer::maxProgressBar= 100; -const Vec4f Renderer::progressBarBack1= Vec4f(0.7f, 0.7f, 0.7f, 0.7f); -const Vec4f Renderer::progressBarBack2= Vec4f(0.7f, 0.7f, 0.7f, 1.f); -const Vec4f Renderer::progressBarFront1= Vec4f(0.f, 0.5f, 0.f, 1.f); -const Vec4f Renderer::progressBarFront2= Vec4f(0.f, 0.1f, 0.f, 1.f); - -const float Renderer::sunDist= 10e6; -const float Renderer::moonDist= 10e6; -const float Renderer::lightAmbFactor= 0.4f; - -const int Renderer::maxMouse2dAnim= 100; - -const GLenum Renderer::baseTexUnit= GL_TEXTURE0; -const GLenum Renderer::fowTexUnit= GL_TEXTURE1; -const GLenum Renderer::shadowTexUnit= GL_TEXTURE2; - -const float Renderer::selectionCircleRadius= 0.7f; -const float Renderer::magicCircleRadius= 1.f; - -//perspective values -const float Renderer::perspFov= 60.f; -const float Renderer::perspNearPlane= 1.f; -//const float Renderer::perspFarPlane= 50.f; -float Renderer::perspFarPlane= 1000000.f; - -const float Renderer::ambFactor= 0.7f; -const Vec4f Renderer::fowColor= Vec4f(0.0f, 0.0f, 0.0f, 1.0f); -const Vec4f Renderer::defSpecularColor= Vec4f(0.8f, 0.8f, 0.8f, 1.f); -const Vec4f Renderer::defDiffuseColor= Vec4f(1.f, 1.f, 1.f, 1.f); -const Vec4f Renderer::defAmbientColor= Vec4f(1.f * ambFactor, 1.f * ambFactor, 1.f * ambFactor, 1.f); -const Vec4f Renderer::defColor= Vec4f(1.f, 1.f, 1.f, 1.f); - -//const float Renderer::maxLightDist= 100.f; -const float Renderer::maxLightDist= 100.f; - -bool Renderer::rendererEnded = true; - -const int MIN_FPS_NORMAL_RENDERING = 15; -const int MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD = 25; - -const int OBJECT_SELECT_OFFSET=100000000; - -bool VisibleQuadContainerCache::enableFrustumCalcs = true; - -// ==================== constructor and destructor ==================== - -Renderer::Renderer() : BaseRenderer(), saveScreenShotThreadAccessor(new Mutex(CODE_AT_LINE)) { - //this->masterserverMode = masterserverMode; - //printf("this->masterserverMode = %d\n",this->masterserverMode); - //assert(0==1); - - Renderer::rendererEnded = false; - shadowIntensity = 0; - shadowFrameSkip = 0; - triangleCount = 0; - smoothedRenderFps = 0; - shadowTextureSize = 0; - shadows = sDisabled; - shadowMapFrame = 0; - textures3D = false; - photoMode = false; - focusArrows = false; - pointCount = 0; - maxLights = 0; - waterAnim = 0; - - this->allowRenderUnitTitles = false; - this->menu = NULL; - this->game = NULL; - this->gameCamera = NULL; - showDebugUI = false; - showDebugUILevel = debugui_fps; - modelRenderer = NULL; - textRenderer = NULL; - textRenderer3D = NULL; - particleRenderer = NULL; - saveScreenShotThread = NULL; - mapSurfaceData.clear(); - visibleFrameUnitList.clear(); - visibleFrameUnitListCameraKey = ""; - - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustumData(); - - lastRenderFps=MIN_FPS_NORMAL_RENDERING; - shadowsOffDueToMinRender=false; - shadowMapHandle=0; - shadowMapHandleValid=false; - - //list3d=0; - //list3dValid=false; - //list2d=0; - //list2dValid=false; - //list3dMenu=0; - //list3dMenuValid=false; - //customlist3dMenu=NULL; - //this->mm3d = NULL; - this->custom_mm3d = NULL; - - this->program = NULL; - - //resources - for(int i=0; i < rsCount; ++i) { - modelManager[i] = NULL; - textureManager[i] = NULL; - particleManager[i] = NULL; - fontManager[i] = NULL; - } - - Config &config= Config::getInstance(); - - Renderer::perspFarPlane = config.getFloat("PerspectiveFarPlane",floatToStr(Renderer::perspFarPlane).c_str()); - this->no2DMouseRendering = config.getBool("No2DMouseRendering","false"); - this->maxConsoleLines= config.getInt("ConsoleMaxLines"); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Renderer::perspFarPlane [%f] this->no2DMouseRendering [%d] this->maxConsoleLines [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,Renderer::perspFarPlane,this->no2DMouseRendering,this->maxConsoleLines); - - GraphicsInterface &gi= GraphicsInterface::getInstance(); - FactoryRepository &fr= FactoryRepository::getInstance(); - gi.setFactory(fr.getGraphicsFactory(config.getString("FactoryGraphics"))); - GraphicsFactory *graphicsFactory= GraphicsInterface::getInstance().getFactory(); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - modelRenderer= graphicsFactory->newModelRenderer(); - textRenderer= graphicsFactory->newTextRenderer2D(); - textRenderer3D = graphicsFactory->newTextRenderer3D(); - particleRenderer= graphicsFactory->newParticleRenderer(); - } - - //resources - for(int i=0; i< rsCount; ++i) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - modelManager[i]= graphicsFactory->newModelManager(); - textureManager[i]= graphicsFactory->newTextureManager(); - modelManager[i]->setTextureManager(textureManager[i]); - fontManager[i]= graphicsFactory->newFontManager(); - } - particleManager[i]= graphicsFactory->newParticleManager(); - } - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - saveScreenShotThread = new SimpleTaskThread(this,0,25); - saveScreenShotThread->setUniqueID(mutexOwnerId); - saveScreenShotThread->start(); - } -} - -void Renderer::cleanupScreenshotThread() { - if(saveScreenShotThread) { - saveScreenShotThread->signalQuit(); -// for(time_t elapsed = time(NULL); -// getSaveScreenQueueSize() > 0 && difftime((long int)time(NULL),elapsed) <= 7;) { -// sleep(0); -// } -// if(saveScreenShotThread->canShutdown(true) == true && -// saveScreenShotThread->shutdownAndWait() == true) { -// //printf("IN MenuStateCustomGame cleanup - C\n"); -// delete saveScreenShotThread; -// } -// saveScreenShotThread = NULL; - if(saveScreenShotThread->shutdownAndWait() == true) { - delete saveScreenShotThread; - } - saveScreenShotThread = NULL; - - - if(getSaveScreenQueueSize() > 0) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] FORCING MEMORY CLEANUP and NOT SAVING screenshots, saveScreenQueue.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,saveScreenQueue.size()); - - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor,mutexOwnerId); - for(std::list >::iterator iter = saveScreenQueue.begin(); - iter != saveScreenQueue.end(); ++iter) { - delete iter->second; - } - saveScreenQueue.clear(); - } - } -} - -Renderer::~Renderer() { - try{ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - delete modelRenderer; - modelRenderer = NULL; - delete textRenderer; - textRenderer = NULL; - delete textRenderer3D; - textRenderer3D = NULL; - delete particleRenderer; - particleRenderer = NULL; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //resources - for(int i=0; imenu = NULL; - this->game = NULL; - this->gameCamera = NULL; - - delete saveScreenShotThreadAccessor; - saveScreenShotThreadAccessor = NULL; - } - catch(const exception &e) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - - throw megaglest_runtime_error(szBuf); - } -} - -void Renderer::simpleTask(BaseThread *callingThread,void *userdata) { - // This code reads pixmaps from a queue and saves them to disk - Pixmap2D *savePixMapBuffer=NULL; - string path=""; - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor,mutexOwnerId); - if(saveScreenQueue.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] saveScreenQueue.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,saveScreenQueue.size()); - - savePixMapBuffer = saveScreenQueue.front().second; - path = saveScreenQueue.front().first; - - saveScreenQueue.pop_front(); - } - safeMutex.ReleaseLock(); - - if(savePixMapBuffer != NULL) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] about to save [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,path.c_str()); - - savePixMapBuffer->save(path); - delete savePixMapBuffer; - } -} - -bool Renderer::isEnded() { - return Renderer::rendererEnded; -} - -Renderer &Renderer::getInstance() { - static Renderer renderer; - return renderer; -} - -void Renderer::reinitAll() { - //resources - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - for(int i=0; iinit(); - textureManager[i]->init(true); - //particleManager[i]->init(); - //fontManager[i]->init(); - } -} -// ==================== init ==================== - -void Renderer::init() { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - Config &config= Config::getInstance(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - loadConfig(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(config.getBool("CheckGlCaps")){ - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - checkGlCaps(); - } - - if(glActiveTexture == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error: glActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!"); - throw megaglest_runtime_error(szBuf); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(config.getBool("FirstTime")){ - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - config.setBool("FirstTime", false); - autoConfig(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - config.save(); - } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - modelManager[rsGlobal]->init(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - textureManager[rsGlobal]->init(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - fontManager[rsGlobal]->init(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - init2dList(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - glHint(GL_FOG_HINT, GL_FASTEST); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST); - glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); - //glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST); - - //glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); - glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - -} - -void Renderer::initGame(const Game *game, GameCamera *gameCamera) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - this->gameCamera = gameCamera; - VisibleQuadContainerCache::enableFrustumCalcs = Config::getInstance().getBool("EnableFrustrumCalcs","true"); - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustumData(); - - SurfaceData::nextUniqueId = 1; - mapSurfaceData.clear(); - this->game= game; - worldToScreenPosCache.clear(); - - //vars - shadowMapFrame= 0; - waterAnim= 0; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //check gl caps - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - checkGlOptionalCaps(); - - //shadows - if(shadows == sProjected || shadows == sShadowMapping) { - static_cast(modelRenderer)->setSecondaryTexCoordUnit(2); - Config &config= Config::getInstance(); - - glGenTextures(1, &shadowMapHandle); - shadowMapHandleValid=true; - - shadowIntensity= config.getFloat("ShadowIntensity","1.0"); - if(game!=NULL){ - shadowIntensity=shadowIntensity*game->getWorld()->getTileset()->getShadowIntense(); - if(shadowIntensity > 1.0f){ - shadowIntensity=1.0f; - } - } - - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - if(shadows == sShadowMapping) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //shadow mapping - glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); - //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); - //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, 1.0f-shadowAlpha); - - glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, - shadowTextureSize, shadowTextureSize, - 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //projected - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, - shadowTextureSize, shadowTextureSize, - 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); - } - - shadowMapFrame= -1; - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - IF_DEBUG_EDITION( getDebugRenderer().init(); ) - - //texture init - modelManager[rsGame]->init(); - textureManager[rsGame]->init(); - fontManager[rsGame]->init(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - init3dList(); -} - -void Renderer::manageDeferredParticleSystems() { - -// if(deferredParticleSystems.empty() == false) { -// printf("deferredParticleSystems.size() = %d\n",(int)deferredParticleSystems.size()); -// } - - for(unsigned int i = 0; i < deferredParticleSystems.size(); ++i) { - std::pair &deferredParticleSystem = deferredParticleSystems[i]; - ParticleSystem *ps = deferredParticleSystem.first; - ResourceScope rs = deferredParticleSystem.second; - if(ps->getTextureFileLoadDeferred() != "" && ps->getTexture() == NULL) { - CoreData::TextureSystemType textureSystemId = - static_cast( - ps->getTextureFileLoadDeferredSystemId()); - - //printf("Load DEFERRED particle i = %d textureSystemId = %d\n",i,textureSystemId); - - if(textureSystemId != CoreData::tsyst_NONE) { - Texture2D *texture= CoreData::getInstance().getTextureBySystemId(textureSystemId); - //printf("Loading texture from system [%d] [%p]\n",textureSystemId,texture); - ps->setTexture(texture); - - //printf("#2 Load DEFERRED particle i = %d textureSystemId = %d, texture = %p\n",i,textureSystemId,texture); - } - else { - Texture2D *texture= newTexture2D(rs); - if(texture) { - texture->setFormat(ps->getTextureFileLoadDeferredFormat()); - texture->getPixmap()->init(ps->getTextureFileLoadDeferredComponents()); - } - if(texture) { - string textureFile = ps->getTextureFileLoadDeferred(); - if(fileExists(textureFile) == false) { - textureFile = Config::findValidLocalFileFromPath(textureFile); - } - texture->load(textureFile); - ps->setTexture(texture); - } - //printf("#3 Load DEFERRED particle i = %d textureSystemId = %d, texture = %p\n",i,textureSystemId,texture); - } - } - if(dynamic_cast(ps) != NULL) { - GameParticleSystem *gps = dynamic_cast(ps); - if(gps != NULL && gps->getModelFileLoadDeferred() != "" && gps->getModel() == NULL) { - std::map > > loadedFileList; - Model *model= newModel(rsGame, gps->getModelFileLoadDeferred(), false, &loadedFileList, NULL); - if(model) - gps->setModel(model); - } - } - manageParticleSystem(ps, rs); - //printf("Managing ps [%p]\n",ps); - } - deferredParticleSystems.clear(); - //printf("After deferredParticleSystems.size() = %d\n",deferredParticleSystems.size()); -} - -void Renderer::initMenu(const MainMenu *mm) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - this->menu = mm; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - modelManager[rsMenu]->init(); - textureManager[rsMenu]->init(); - fontManager[rsMenu]->init(); - //modelRenderer->setCustomTexture(CoreData::getInstance().getCustomTexture()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //init3dListMenu(mm); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void Renderer::reset3d() { - assertGl(); - glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); - //glCallList(list3d); - render3dSetup(); - - pointCount= 0; - triangleCount= 0; - assertGl(); -} - -void Renderer::reset2d() { - assertGl(); - glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR); - //glCallList(list2d); - render2dMenuSetup(); - assertGl(); -} - -void Renderer::reset3dMenu() { - assertGl(); - glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR); - - //printf("In [%s::%s Line: %d] this->custom_mm3d [%p] this->mm3d [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->custom_mm3d,this->mm3d); - - if(this->custom_mm3d != NULL) { - render3dMenuSetup(this->custom_mm3d); - //glCallList(*this->customlist3dMenu); - } - else { - render3dMenuSetup(this->menu); - //render3dMenuSetup(this->mm3d); - //glCallList(list3dMenu); - } - - assertGl(); -} - -// ==================== end ==================== - -void Renderer::end() { - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustumData(); - - if(Renderer::rendererEnded == true) { - return; - } - std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); - crcFactionPreviewTextureCache.clear(); - - // Wait for the queue to become empty or timeout the thread at 7 seconds - cleanupScreenshotThread(); - - mapSurfaceData.clear(); - - //delete resources - if(modelManager[rsGlobal]) { - modelManager[rsGlobal]->end(); - } - if(textureManager[rsGlobal]) { - textureManager[rsGlobal]->end(); - } - if(fontManager[rsGlobal]) { - fontManager[rsGlobal]->end(); - } - if(particleManager[rsGlobal]) { - particleManager[rsGlobal]->end(); - } - - //delete 2d list - //if(list2dValid == true) { - // glDeleteLists(list2d, 1); - // list2dValid=false; - //} - - Renderer::rendererEnded = true; -} - -void Renderer::endScenario() { - this->game= NULL; - this->gameCamera = NULL; - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustumData(); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //delete resources - //modelManager[rsGame]->end(); - //textureManager[rsGame]->end(); - //fontManager[rsGame]->end(); - //particleManager[rsGame]->end(); - - if(shadowMapHandleValid == true && - (shadows == sProjected || shadows == sShadowMapping)) { - glDeleteTextures(1, &shadowMapHandle); - shadowMapHandleValid=false; - } - - //if(list3dValid == true) { - // glDeleteLists(list3d, 1); - // list3dValid=false; - //} - - worldToScreenPosCache.clear(); - ReleaseSurfaceVBOs(); - mapSurfaceData.clear(); -} - -void Renderer::endGame(bool isFinalEnd) { - this->game= NULL; - this->gameCamera = NULL; - Config &config= Config::getInstance(); - - try { - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustumData(); - } - catch(const exception &e) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - - abort(); - } - - if(isFinalEnd) { - //delete resources - if(modelManager[rsGame] != NULL) { - modelManager[rsGame]->end(); - } - if(textureManager[rsGame] != NULL) { - textureManager[rsGame]->end(); - } - if(fontManager[rsGame] != NULL) { - fontManager[rsGame]->end(); - } - if(particleManager[rsGame] != NULL) { - particleManager[rsGame]->end(); - } - } - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(shadowMapHandleValid == true && - (shadows == sProjected || shadows == sShadowMapping)) { - glDeleteTextures(1, &shadowMapHandle); - shadowMapHandleValid=false; - } - shadowIntensity= config.getFloat("ShadowIntensity","1.0"); - - //if(list3dValid == true) { - // glDeleteLists(list3d, 1); - // list3dValid=false; - //} - - worldToScreenPosCache.clear(); - ReleaseSurfaceVBOs(); - mapSurfaceData.clear(); -} - -void Renderer::endMenu() { - this->menu = NULL; - - //delete resources - if(modelManager[rsMenu]) { - modelManager[rsMenu]->end(); - } - if(textureManager[rsMenu]) { - textureManager[rsMenu]->end(); - } - if(fontManager[rsMenu]) { - fontManager[rsMenu]->end(); - } - if(particleManager[rsMenu]) { - particleManager[rsMenu]->end(); - } - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //if(this->customlist3dMenu != NULL) { - // glDeleteLists(*this->customlist3dMenu,1); - //} - //else { - // glDeleteLists(list3dMenu, 1); - //} -} - -//void Renderer::reloadResources() { -// if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { -// return; -// } -// -// for(int i=0; iend(); -// textureManager[i]->end(); -// fontManager[i]->end(); -// } -// -// for(int i=0; iinit(); -// textureManager[i]->init(); -// fontManager[i]->init(); -// } -//} - -// ==================== engine interface ==================== - -void Renderer::initTexture(ResourceScope rs, Texture *texture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - textureManager[rs]->initTexture(texture); -} - -void Renderer::endTexture(ResourceScope rs, Texture *texture, bool mustExistInList) { - string textureFilename = texture->getPath(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] free texture from manager [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,textureFilename.c_str()); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - textureManager[rs]->endTexture(texture,mustExistInList); - - if(rs == rsGlobal) { - std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); - if(crcFactionPreviewTextureCache.find(textureFilename) != crcFactionPreviewTextureCache.end()) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] textureFilename [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,textureFilename.c_str()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] free texture from cache [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,textureFilename.c_str()); - - crcFactionPreviewTextureCache.erase(textureFilename); - } - } -} -void Renderer::endLastTexture(ResourceScope rs, bool mustExistInList) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - textureManager[rs]->endLastTexture(mustExistInList); -} - -Model *Renderer::newModel(ResourceScope rs,const string &path,bool deletePixMapAfterLoad,std::map > > *loadedFileList, string *sourceLoader){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return NULL; - } - - return modelManager[rs]->newModel(path,deletePixMapAfterLoad,loadedFileList,sourceLoader); -} - -void Renderer::endModel(ResourceScope rs, Model *model,bool mustExistInList) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - modelManager[rs]->endModel(model,mustExistInList); -} -void Renderer::endLastModel(ResourceScope rs, bool mustExistInList) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - modelManager[rs]->endLastModel(mustExistInList); -} - -Texture2D *Renderer::newTexture2D(ResourceScope rs){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return NULL; - } - - return textureManager[rs]->newTexture2D(); -} - -Texture3D *Renderer::newTexture3D(ResourceScope rs){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return NULL; - } - - return textureManager[rs]->newTexture3D(); -} - -Font2D *Renderer::newFont(ResourceScope rs){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return NULL; - } - - return fontManager[rs]->newFont2D(); -} - -Font3D *Renderer::newFont3D(ResourceScope rs){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return NULL; - } - - return fontManager[rs]->newFont3D(); -} - -void Renderer::endFont(::Shared::Graphics::Font *font, ResourceScope rs, bool mustExistInList) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - fontManager[rs]->endFont(font,mustExistInList); -} - -//void Renderer::resetFontManager(ResourceScope rs) { -// if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { -// return; -// } -// fontManager[rs]->end(); -// fontManager[rsGlobal]->init(); -//} - -void Renderer::addToDeferredParticleSystemList(std::pair deferredParticleSystem) { - deferredParticleSystems.push_back(deferredParticleSystem); -} - -void Renderer::manageParticleSystem(ParticleSystem *particleSystem, ResourceScope rs){ - particleManager[rs]->manage(particleSystem); -} - -bool Renderer::validateParticleSystemStillExists(ParticleSystem * particleSystem,ResourceScope rs) const { - return particleManager[rs]->validateParticleSystemStillExists(particleSystem); -} - -void Renderer::removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner,ResourceScope rs) { - particleManager[rs]->removeParticleSystemsForParticleOwner(particleOwner); -} - -void Renderer::cleanupParticleSystems(vector &particleSystems, ResourceScope rs) { - particleManager[rs]->cleanupParticleSystems(particleSystems); -} - -void Renderer::cleanupUnitParticleSystems(vector &particleSystems, ResourceScope rs) { - particleManager[rs]->cleanupUnitParticleSystems(particleSystems); -} - -void Renderer::updateParticleManager(ResourceScope rs, int renderFps) { - particleManager[rs]->update(renderFps); -} - -void Renderer::renderParticleManager(ResourceScope rs){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); - glDepthFunc(GL_LESS); - particleRenderer->renderManager(particleManager[rs], modelRenderer); - glPopAttrib(); -} - -void Renderer::swapBuffers() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - //glFlush(); // should not be required - http://www.opengl.org/wiki/Common_Mistakes - //glFlush(); - - GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); -} - -// ==================== lighting ==================== - -//places all the opengl lights -void Renderer::setupLighting() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - int lightCount= 0; - const World *world= game->getWorld(); - const GameCamera *gameCamera= game->getGameCamera(); - const TimeFlow *timeFlow= world->getTimeFlow(); - float time= timeFlow->getTime(); - - assertGl(); - - //sun/moon light - Vec3f lightColor= timeFlow->computeLightColor(); - Vec3f fogColor= world->getTileset()->getFogColor(); - Vec4f lightPos= timeFlow->isDay()? computeSunPos(time): computeMoonPos(time); - nearestLightPos= lightPos; - - glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); - glLightfv(GL_LIGHT0, GL_AMBIENT, Vec4f(lightColor*lightAmbFactor, 1.f).ptr()); - glLightfv(GL_LIGHT0, GL_DIFFUSE, Vec4f(lightColor, 1.f).ptr()); - glLightfv(GL_LIGHT0, GL_SPECULAR, Vec4f(0.0f, 0.0f, 0.f, 1.f).ptr()); - - glFogfv(GL_FOG_COLOR, Vec4f(fogColor*lightColor, 1.f).ptr()); - - lightCount++; - - //disable all secondary lights - for(int i= 1; i < maxLights; ++i) { - glDisable(GL_LIGHT0 + i); - } - - //unit lights (not projectiles) - - if(timeFlow->isTotalNight()) { - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - //bool modelRenderStarted = false; - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size() && lightCount < maxLights; - ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - - if(world->toRenderUnit(unit) && - unit->getCurrMidHeightVector().dist(gameCamera->getPos()) < maxLightDist && - unit->getType()->getLight() && unit->isOperative()) { - //printf("$$$ Show light for faction: %s # %d / %d for Unit [%d - %s]\n",world->getFaction(i)->getType()->getName().c_str(),lightCount,maxLights,unit->getId(),unit->getFullName().c_str()); - - Vec4f pos= Vec4f(unit->getCurrMidHeightVector()); - pos.y+=4.f; - - GLenum lightEnum= GL_LIGHT0 + lightCount; - - glEnable(lightEnum); - glLightfv(lightEnum, GL_POSITION, pos.ptr()); - glLightfv(lightEnum, GL_AMBIENT, Vec4f(unit->getType()->getLightColor()).ptr()); - glLightfv(lightEnum, GL_DIFFUSE, Vec4f(unit->getType()->getLightColor()).ptr()); - glLightfv(lightEnum, GL_SPECULAR, Vec4f(unit->getType()->getLightColor()*0.3f).ptr()); - glLightf(lightEnum, GL_QUADRATIC_ATTENUATION, 0.05f); - - ++lightCount; - - const GameCamera *gameCamera= game->getGameCamera(); - - if(Vec3f(pos).dist(gameCamera->getPos())getPos())){ - nearestLightPos= pos; - } - } - } - } - } - - assertGl(); -} - -//void Renderer::setupLightingForRotatedModel() { -// if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { -// return; -// } -// -// const World *world= game->getWorld(); -// //const GameCamera *gameCamera= game->getGameCamera(); -// const TimeFlow *timeFlow= world->getTimeFlow(); -// float time= timeFlow->getTime(); -// -// assertGl(); -// -// //sun/moon light -// Vec3f lightColor= timeFlow->computeLightColor(); -// Vec3f fogColor= world->getTileset()->getFogColor(); -// Vec4f lightPos= timeFlow->isDay()? computeSunPos(time): computeMoonPos(time); -// //nearestLightPos= lightPos; -// -// glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); -// glLightfv(GL_LIGHT0, GL_AMBIENT, Vec4f(lightColor*lightAmbFactor, 1.f).ptr()); -// glLightfv(GL_LIGHT0, GL_DIFFUSE, Vec4f(lightColor, 1.f).ptr()); -// glLightfv(GL_LIGHT0, GL_SPECULAR, Vec4f(0.0f, 0.0f, 0.f, 1.f).ptr()); -// -// glFogfv(GL_FOG_COLOR, Vec4f(fogColor*lightColor, 1.f).ptr()); -// -// assertGl(); -//} - -void Renderer::loadGameCameraMatrix() { - const GameCamera *gameCamera= game->getGameCamera(); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - if(gameCamera != NULL) { - glRotatef(gameCamera->getVAng(), -1, 0, 0); - glRotatef(gameCamera->getHAng(), 0, 1, 0); - glTranslatef(-(gameCamera->getPos().x + gameCamera->getShakeOffset().x), - -gameCamera->getPos().y, - -(gameCamera->getPos().z + gameCamera->getShakeOffset().y)); - } -} - -void Renderer::loadCameraMatrix(const Camera *camera) { - const Vec3f &position= camera->getConstPosition(); - Quaternion orientation= camera->getOrientation().conjugate(); - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glMultMatrixf(orientation.toMatrix4().ptr()); - glTranslatef(-position.x, -position.y, -position.z); -} - -static Vec2i _unprojectMap(const Vec2i& pt,const GLdouble* model,const GLdouble* projection,const GLint* viewport,const char* label=NULL) { - Vec3d a,b; - /* note viewport[3] is height of window in pixels */ - GLint realy = viewport[3] - (GLint) pt.y; - gluUnProject(pt.x,realy,0,model,projection,viewport,&a.x,&a.y,&a.z); - gluUnProject(pt.x,realy,1,model,projection,viewport,&b.x,&b.y,&b.z); - - // junk values if you were looking parallel to the XZ plane; this shouldn't happen as the camera can't do this? - const Vec3f - start(a.x,a.y,a.z), - stop(b.x,b.y,b.z), - plane(0,0,0), - norm(0,1,0), - u = stop-start, - w = start-plane; - const float d = norm.x*u.x + norm.y*u.y + norm.z*u.z; - if(std::fabs(d) < 0.00001) - throw pti_D_IS_ZERO; - - const float nd = -(norm.x*w.x + norm.y*w.y + norm.z*w.z) / d; - if(nd < 0.0 || nd >= 1.0) - throw pti_N_OVER_D_IS_OUTSIDE; - - const Vec3f i = start + u*nd; - //const Vec2i pos(i.x,i.z); - - Vec2i pos; - if(strcmp(label,"tl") == 0) { - pos = Vec2i(std::floor(i.x),std::floor(i.z)); - } - else if(strcmp(label,"tr") == 0) { - pos = Vec2i(std::ceil(i.x),std::floor(i.z)); - } - else if(strcmp(label,"bl") == 0) { - pos = Vec2i(std::floor(i.x),std::ceil(i.z)); - } - else if(strcmp(label,"br") == 0) { - pos = Vec2i(std::ceil(i.x),std::ceil(i.z)); - } - - if(false) { // print debug info - if(label) printf("%s ",label); - printf("%d,%d -> %f,%f,%f -> %f,%f,%f -> %f,%f,%f -> %d,%d\n", - pt.x,pt.y, - start.x,start.y,start.z, - stop.x,stop.y,stop.z, - i.x,i.y,i.z, - pos.x,pos.y); - } - return pos; - -} - -bool Renderer::ExtractFrustum(VisibleQuadContainerCache &quadCacheItem) { - bool frustumChanged = false; - vector proj(16,0); - vector modl(16,0); - - /* Get the current PROJECTION matrix from OpenGL */ - glGetFloatv( GL_PROJECTION_MATRIX, &proj[0] ); - - /* Get the current MODELVIEW matrix from OpenGL */ - glGetFloatv( GL_MODELVIEW_MATRIX, &modl[0] ); - -// for(unsigned int i = 0; i < proj.size(); ++i) { -// //printf("\ni = %d proj [%f][%f] modl [%f][%f]\n",i,proj[i],quadCacheItem.proj[i],modl[i],quadCacheItem.modl[i]); -// if(proj[i] != quadCacheItem.proj[i]) { -// frustumChanged = true; -// break; -// } -// if(modl[i] != quadCacheItem.modl[i]) { -// frustumChanged = true; -// break; -// } -// } - - // Check the frustum cache - const bool useFrustumCache = Config::getInstance().getBool("EnableFrustrumCache","false"); - pair,vector > lookupKey; - if(useFrustumCache == true) { - lookupKey = make_pair(proj,modl); - map,vector >, vector > >::iterator iterFind = quadCacheItem.frustumDataCache.find(lookupKey); - if(iterFind != quadCacheItem.frustumDataCache.end()) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum found in cache\n"); - - quadCacheItem.frustumData = iterFind->second; - frustumChanged = (quadCacheItem.proj != proj || quadCacheItem.modl != modl); - if(frustumChanged == true) { - quadCacheItem.proj = proj; - quadCacheItem.modl = modl; - } - - return frustumChanged; - } - } - - if(quadCacheItem.proj != proj || quadCacheItem.modl != modl) { - //if(frustumChanged == true) { - frustumChanged = true; - vector > &frustum = quadCacheItem.frustumData; - //assert(frustum.size() == 6); - //assert(frustum[0].size() == 4); - - quadCacheItem.proj = proj; - quadCacheItem.modl = modl; - - float clip[16]; - float t=0; - - /* Combine the two matrices (multiply projection by modelview) */ - clip[ 0] = modl[ 0] * proj[ 0] + modl[ 1] * proj[ 4] + modl[ 2] * proj[ 8] + modl[ 3] * proj[12]; - clip[ 1] = modl[ 0] * proj[ 1] + modl[ 1] * proj[ 5] + modl[ 2] * proj[ 9] + modl[ 3] * proj[13]; - clip[ 2] = modl[ 0] * proj[ 2] + modl[ 1] * proj[ 6] + modl[ 2] * proj[10] + modl[ 3] * proj[14]; - clip[ 3] = modl[ 0] * proj[ 3] + modl[ 1] * proj[ 7] + modl[ 2] * proj[11] + modl[ 3] * proj[15]; - - clip[ 4] = modl[ 4] * proj[ 0] + modl[ 5] * proj[ 4] + modl[ 6] * proj[ 8] + modl[ 7] * proj[12]; - clip[ 5] = modl[ 4] * proj[ 1] + modl[ 5] * proj[ 5] + modl[ 6] * proj[ 9] + modl[ 7] * proj[13]; - clip[ 6] = modl[ 4] * proj[ 2] + modl[ 5] * proj[ 6] + modl[ 6] * proj[10] + modl[ 7] * proj[14]; - clip[ 7] = modl[ 4] * proj[ 3] + modl[ 5] * proj[ 7] + modl[ 6] * proj[11] + modl[ 7] * proj[15]; - - clip[ 8] = modl[ 8] * proj[ 0] + modl[ 9] * proj[ 4] + modl[10] * proj[ 8] + modl[11] * proj[12]; - clip[ 9] = modl[ 8] * proj[ 1] + modl[ 9] * proj[ 5] + modl[10] * proj[ 9] + modl[11] * proj[13]; - clip[10] = modl[ 8] * proj[ 2] + modl[ 9] * proj[ 6] + modl[10] * proj[10] + modl[11] * proj[14]; - clip[11] = modl[ 8] * proj[ 3] + modl[ 9] * proj[ 7] + modl[10] * proj[11] + modl[11] * proj[15]; - - clip[12] = modl[12] * proj[ 0] + modl[13] * proj[ 4] + modl[14] * proj[ 8] + modl[15] * proj[12]; - clip[13] = modl[12] * proj[ 1] + modl[13] * proj[ 5] + modl[14] * proj[ 9] + modl[15] * proj[13]; - clip[14] = modl[12] * proj[ 2] + modl[13] * proj[ 6] + modl[14] * proj[10] + modl[15] * proj[14]; - clip[15] = modl[12] * proj[ 3] + modl[13] * proj[ 7] + modl[14] * proj[11] + modl[15] * proj[15]; - - /* Extract the numbers for the RIGHT plane */ - frustum[0][0] = clip[ 3] - clip[ 0]; - frustum[0][1] = clip[ 7] - clip[ 4]; - frustum[0][2] = clip[11] - clip[ 8]; - frustum[0][3] = clip[15] - clip[12]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",0,frustum[0][0],frustum[0][1],frustum[0][2],frustum[0][3]); - - /* Normalize the result */ - t = std::sqrt( frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2] ); - if(t != 0.0) { - frustum[0][0] /= t; - frustum[0][1] /= t; - frustum[0][2] /= t; - frustum[0][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",0,frustum[0][0],frustum[0][1],frustum[0][2],frustum[0][3],t); - } - - /* Extract the numbers for the LEFT plane */ - frustum[1][0] = clip[ 3] + clip[ 0]; - frustum[1][1] = clip[ 7] + clip[ 4]; - frustum[1][2] = clip[11] + clip[ 8]; - frustum[1][3] = clip[15] + clip[12]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",1,frustum[1][0],frustum[1][1],frustum[1][2],frustum[1][3]); - - /* Normalize the result */ - t = std::sqrt( frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2] ); - if(t != 0.0) { - frustum[1][0] /= t; - frustum[1][1] /= t; - frustum[1][2] /= t; - frustum[1][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",1,frustum[1][0],frustum[1][1],frustum[1][2],frustum[1][3],t); - } - - /* Extract the BOTTOM plane */ - frustum[2][0] = clip[ 3] + clip[ 1]; - frustum[2][1] = clip[ 7] + clip[ 5]; - frustum[2][2] = clip[11] + clip[ 9]; - frustum[2][3] = clip[15] + clip[13]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",2,frustum[2][0],frustum[2][1],frustum[2][2],frustum[2][3]); - - /* Normalize the result */ - - t = std::sqrt( frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2] ); - if(t != 0.0) { - frustum[2][0] /= t; - frustum[2][1] /= t; - frustum[2][2] /= t; - frustum[2][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",2,frustum[2][0],frustum[2][1],frustum[2][2],frustum[2][3],t); - } - - /* Extract the TOP plane */ - frustum[3][0] = clip[ 3] - clip[ 1]; - frustum[3][1] = clip[ 7] - clip[ 5]; - frustum[3][2] = clip[11] - clip[ 9]; - frustum[3][3] = clip[15] - clip[13]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",3,frustum[3][0],frustum[3][1],frustum[3][2],frustum[3][3]); - - /* Normalize the result */ - - t = std::sqrt( frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2] ); - if(t != 0.0) { - frustum[3][0] /= t; - frustum[3][1] /= t; - frustum[3][2] /= t; - frustum[3][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",3,frustum[3][0],frustum[3][1],frustum[3][2],frustum[3][3],t); - } - - /* Extract the FAR plane */ - frustum[4][0] = clip[ 3] - clip[ 2]; - frustum[4][1] = clip[ 7] - clip[ 6]; - frustum[4][2] = clip[11] - clip[10]; - frustum[4][3] = clip[15] - clip[14]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",4,frustum[4][0],frustum[4][1],frustum[4][2],frustum[4][3]); - - /* Normalize the result */ - - t = std::sqrt( frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2] ); - - if(t != 0.0) { - frustum[4][0] /= t; - frustum[4][1] /= t; - frustum[4][2] /= t; - frustum[4][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",4,frustum[4][0],frustum[4][1],frustum[4][2],frustum[4][3],t); - } - - /* Extract the NEAR plane */ - frustum[5][0] = clip[ 3] + clip[ 2]; - frustum[5][1] = clip[ 7] + clip[ 6]; - frustum[5][2] = clip[11] + clip[10]; - frustum[5][3] = clip[15] + clip[14]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n",5,frustum[5][0],frustum[5][1],frustum[5][2],frustum[5][3]); - - /* Normalize the result */ - - t = std::sqrt( frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2] ); - - if(t != 0.0) { - frustum[5][0] /= t; - frustum[5][1] /= t; - frustum[5][2] /= t; - frustum[5][3] /= t; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n",5,frustum[5][0],frustum[5][1],frustum[5][2],frustum[5][3],t); - } - - if(useFrustumCache == true) { - quadCacheItem.frustumDataCache[lookupKey] = frustum; - } - } - return frustumChanged; -} - -//bool Renderer::PointInFrustum(vector > &frustum, float x, float y, float z ) { -// unsigned int p=0; -// -// for( p = 0; p < frustum.size(); p++ ) { -// if( frustum[p][0] * x + frustum[p][1] * y + frustum[p][2] * z + frustum[p][3] <= 0 ) { -// return false; -// } -// } -// return true; -//} -// -//bool Renderer::SphereInFrustum(vector > &frustum, float x, float y, float z, float radius) { -// // Go through all the sides of the frustum -// for(int i = 0; i < (int)frustum.size(); i++ ) { -// // If the center of the sphere is farther away from the plane than the radius -// if(frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] <= -radius ) { -// // The distance was greater than the radius so the sphere is outside of the frustum -// return false; -// } -// } -// -// // The sphere was inside of the frustum! -// return true; -//} - -bool Renderer::CubeInFrustum(vector > &frustum, float x, float y, float z, float size ) { - unsigned int p=0; - - for( p = 0; p < frustum.size(); p++ ) { - if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + frustum[p][2] * (z - size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + frustum[p][2] * (z - size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + frustum[p][2] * (z - size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + frustum[p][2] * (z - size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + frustum[p][2] * (z + size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + frustum[p][2] * (z + size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + frustum[p][2] * (z + size) + frustum[p][3] > 0 ) - continue; - if( frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + frustum[p][2] * (z + size) + frustum[p][3] > 0 ) - continue; - return false; - } - return true; -} - -void Renderer::computeVisibleQuad() { - visibleQuad = this->gameCamera->computeVisibleQuad(); - - bool frustumChanged = false; - if(VisibleQuadContainerCache::enableFrustumCalcs == true) { - frustumChanged = ExtractFrustum(quadCache); - } - - if(frustumChanged && SystemFlags::VERBOSE_MODE_ENABLED) { - printf("\nCamera: %d,%d %d,%d %d,%d %d,%d\n", - visibleQuad.p[0].x,visibleQuad.p[0].y, - visibleQuad.p[1].x,visibleQuad.p[1].y, - visibleQuad.p[2].x,visibleQuad.p[2].y, - visibleQuad.p[3].x,visibleQuad.p[3].y); - - for(unsigned int i = 0; i < quadCache.frustumData.size(); ++i) { - printf("\nFrustum #%u [" MG_SIZE_T_SPECIFIER "]: ",i,quadCache.frustumData.size()); - vector &frustumDataInner = quadCache.frustumData[i]; - for(unsigned int j = 0; j < frustumDataInner.size(); ++j) { - printf("[%f]",quadCache.frustumData[i][j]); - } - } - - printf("\nEND\n"); - } - - const bool newVisibleQuadCalc = false; - if(newVisibleQuadCalc) { - const bool debug = false; - try { - if(debug) { - visibleQuad = gameCamera->computeVisibleQuad(); - printf("Camera: %d,%d %d,%d %d,%d %d,%d\n", - visibleQuad.p[0].x,visibleQuad.p[0].y, - visibleQuad.p[1].x,visibleQuad.p[1].y, - visibleQuad.p[2].x,visibleQuad.p[2].y, - visibleQuad.p[3].x,visibleQuad.p[3].y); - } - - - // compute the four corners using OpenGL - GLdouble model[16], projection[16]; - GLint viewport[4]; - glGetDoublev(GL_MODELVIEW_MATRIX,model); - glGetDoublev(GL_PROJECTION_MATRIX,projection); - glGetIntegerv(GL_VIEWPORT,viewport); - Vec2i - tl = _unprojectMap(Vec2i(0,0),model,projection,viewport,"tl"), - tr = _unprojectMap(Vec2i(viewport[2],0),model,projection,viewport,"tr"), - br = _unprojectMap(Vec2i(viewport[2],viewport[3]),model,projection,viewport,"br"), - bl = _unprojectMap(Vec2i(0,viewport[3]),model,projection,viewport,"bl"); - - - // orientate it for map iterator - //bool swapRequiredX = false; - bool swapRequiredY = false; - int const cellBuffer = 4; - if((tl.x > tr.x) || (bl.x > br.x)) { - if(debug) printf("Swap X???\n"); - - //std::swap(tl,bl); - //std::swap(tr,br); - if(tl.x > tr.x) { - if(debug) printf("Swap X1???\n"); - - tr.x += cellBuffer; - tl.x -= cellBuffer; - - std::swap(tl.x,tr.x); - //swapRequiredX = true; - } - else { - tl.x += cellBuffer; - tr.x -= cellBuffer; - } - if(bl.x > br.x) { - if(debug) printf("Swap X2???\n"); - - bl.x += cellBuffer; - br.x -= cellBuffer; - - std::swap(bl.x,br.x); - //swapRequiredX = true; - } - else { - br.x += cellBuffer; - bl.x -= cellBuffer; - } - } - - if((tl.y > bl.y) || (tr.y > br.y)) { - visibleQuad = this->gameCamera->computeVisibleQuad(); - - if(debug) printf("Swap Y???\n"); - - if(tl.y > bl.y) { - if(debug) printf("Swap Y1???\n"); - - tl.y += cellBuffer; - bl.y -= cellBuffer; - - std::swap(tl.y,bl.y); - swapRequiredY = true; - } - else { - bl.y += cellBuffer; - tl.y -= cellBuffer; - } - if(tr.y > br.y) { - if(debug) printf("Swap Y2???\n"); - - tr.y += cellBuffer; - br.y -= cellBuffer; - - std::swap(tr.y,br.y); - swapRequiredY = true; - } - else { - br.y += cellBuffer; - tr.y -= cellBuffer; - } - - - //std::swap(tl,tr); - //std::swap(bl,br); - } - if(swapRequiredY == false) { - tl.y -= cellBuffer; - tr.y -= cellBuffer; - bl.y += cellBuffer; - br.y += cellBuffer; - } - - // set it as the frustum - visibleQuad = Quad2i(tl,bl,tr,br); // strange order - if(debug) { - printf("Will: %d,%d %d,%d %d,%d %d,%d\n", - visibleQuad.p[0].x,visibleQuad.p[0].y, - visibleQuad.p[1].x,visibleQuad.p[1].y, - visibleQuad.p[2].x,visibleQuad.p[2].y, - visibleQuad.p[3].x,visibleQuad.p[3].y); - } - } - catch(PROJECTION_TO_INFINITY &e) { - if(debug) printf("hmm staring at the horizon %d\n",(int)e); - // use historic code solution - visibleQuad = this->gameCamera->computeVisibleQuad(); - } - } -} - -// ======================================= -// basic rendering -// ======================================= - -void Renderer::renderMouse2d(int x, int y, int anim, float fade) { - if(no2DMouseRendering == true) { - return; - } - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } -// float blue=0.0f; -// float green=0.4f; - if(game != NULL && game->getGui() != NULL) { - const Gui *gui=game->getGui(); - const Display *display=gui->getDisplay(); - int downPos= display->getDownSelectedPos(); - if(downPos != Display::invalidPos){ - // in state of doing something - const Texture2D *texture= display->getDownImage(downPos); - renderTextureQuad(x+18,y-50,32,32,texture,0.8f); - } -// else { -// // Display current commandtype -// const Unit *unit=NULL; -// if(gui->getSelection()->isEmpty()){ -// blue=0.0f; -// green=0.1f; -// } -// else{ -// unit=gui->getSelection()->getFrontUnit(); -// if(unit->getCurrCommand()!=NULL && unit->getCurrCommand()->getCommandType()->getImage()!=NULL){ -// const Texture2D *texture = unit->getCurrCommand()->getCommandType()->getImage(); -// renderTextureQuad(x+18,y-50,32,32,texture,0.2f); -// } -// } -// } - - if(game->isMarkCellMode() == true) { - const Texture2D *texture= game->getMarkCellTexture(); - renderTextureQuad(x,y,texture->getTextureWidth(),texture->getTextureHeight(),texture,0.8f); - } - if(game->isUnMarkCellMode() == true) { - const Texture2D *texture= game->getUnMarkCellTexture(); - renderTextureQuad(x,y,texture->getTextureWidth(),texture->getTextureHeight(),texture,0.8f); - } - } - - float fadeFactor = fade + 1.f; - - anim= anim * 2 - maxMouse2dAnim; - - float color2= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.4f; - float color1= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.8f; - - glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT); - glEnable(GL_BLEND); - - //inside - glColor4f(0.4f*fadeFactor, 0.2f*fadeFactor, 0.2f*fadeFactor, 0.5f*fadeFactor); - glBegin(GL_TRIANGLES); - glVertex2i(x, y); - glVertex2i(x+20, y-10); - glVertex2i(x+10, y-20); - glEnd(); - - //border - glLineWidth(2); - glBegin(GL_LINE_LOOP); - glColor4f(1.f, 0.2f, 0, color1); - glVertex2i(x, y); - glColor4f(1.f, 0.4f, 0, color2); - glVertex2i(x+20, y-10); - glColor4f(1.f, 0.4f, 0, color2); - glVertex2i(x+10, y-20); - glEnd(); - glPopAttrib(); - - -/* - if(no2DMouseRendering == true) { - return; - } - float color1 = 0.0, color2 = 0.0; - - float fadeFactor = fade + 1.f; - - anim= anim * 2 - maxMouse2dAnim; - - color2= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.4f; - color1= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.8f; - - glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT); - glEnable(GL_BLEND); - - //inside - Vec2i vertices[3]; - vertices[0] = Vec2i(x, y); - vertices[1] = Vec2i(x+20, y-10); - vertices[2] = Vec2i(x+10, y-20); - - glColor4f(0.4f*fadeFactor, 0.2f*fadeFactor, 0.2f*fadeFactor, 0.5f*fadeFactor); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(2, GL_INT, 0, &vertices[0]); - glDrawArrays(GL_TRIANGLES, 0, 3); - glDisableClientState(GL_VERTEX_ARRAY); - - //border - vertices[0] = Vec2i(x, y); - vertices[1] = Vec2i(x+20, y-10); - vertices[2] = Vec2i(x+10, y-20); - - Vec4f colors[4]; - colors[0] = Vec4f(1.f, 0.2f, 0, color1); - colors[1] = Vec4f(1.f, 0.4f, 0, color2); - colors[2] = Vec4f(1.f, 0.4f, 0, color2); - - glLineWidth(2); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(2, GL_INT, 0, &vertices[0]); - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(4, GL_FLOAT, 0, &colors[0]); - glDrawArrays(GL_LINE_LOOP, 0, 3); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - glPopAttrib(); -*/ -} - -void Renderer::renderMouse3d() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("RecordMode","false") == true) { - return; - } - - if(game == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d game == NULL",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - else if(game->getGui() == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d game->getGui() == NULL",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - else if(game->getGui()->getMouse3d() == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d game->getGui()->getMouse3d() == NULL",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - - const Gui *gui= game->getGui(); - const Mouse3d *mouse3d= gui->getMouse3d(); - const Map *map= game->getWorld()->getMap(); - if(map == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d map == NULL",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - - assertGl(); - - if((mouse3d->isEnabled() || gui->isPlacingBuilding()) && gui->isValidPosObjWorld()) { - const Vec2i &pos= gui->getPosObjWorld(); - - glMatrixMode(GL_MODELVIEW); - - glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_BLEND); - glDisable(GL_STENCIL_TEST); - glDepthFunc(GL_LESS); - glEnable(GL_COLOR_MATERIAL); - glDepthMask(GL_FALSE); - - if(gui->isPlacingBuilding()) { - - modelRenderer->begin(true, true, false, false); - - const UnitType *building= gui->getBuilding(); - const Gui *gui= game->getGui(); - renderGhostModel(building, pos, gui->getSelectedFacing()); - - modelRenderer->end(); - - glDisable(GL_COLOR_MATERIAL); - glPopAttrib(); - } - else { - glPushMatrix(); - Vec3f pos3f= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); - Vec4f color; - GLUquadricObj *cilQuadric; - //standard mouse - glDisable(GL_TEXTURE_2D); - glDisable(GL_CULL_FACE); - color= Vec4f(1.f, 0.f, 0.f, 1.f-mouse3d->getFade()); - glColor4fv(color.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color.ptr()); - - glTranslatef(pos3f.x, pos3f.y+2.f, pos3f.z); - glRotatef(90.f, 1.f, 0.f, 0.f); - glRotatef(static_cast(mouse3d->getRot()), 0.f, 0.f, 1.f); - - cilQuadric= gluNewQuadric(); - gluQuadricDrawStyle(cilQuadric, GLU_FILL); - gluCylinder(cilQuadric, 0.5f, 0.f, 2.f, 4, 1); - gluCylinder(cilQuadric, 0.5f, 0.f, 0.f, 4, 1); - glTranslatef(0.f, 0.f, 1.f); - gluCylinder(cilQuadric, 0.7f, 0.f, 1.f, 4, 1); - gluCylinder(cilQuadric, 0.7f, 0.f, 0.f, 4, 1); - gluDeleteQuadric(cilQuadric); - - glPopAttrib(); - glPopMatrix(); - } - } - -} - -void Renderer::renderBackground(const Texture2D *texture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const Metrics &metrics= Metrics::getInstance(); - - assertGl(); - - glPushAttrib(GL_ENABLE_BIT); - - glDisable(GL_LIGHTING); - glEnable(GL_TEXTURE_2D); - - renderQuad(0, 0, metrics.getVirtualW(), metrics.getVirtualH(), texture); - - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderTextureQuad(int x, int y, int w, int h, const Texture2D *texture, float alpha,const Vec3f *color) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - assertGl(); - - glPushAttrib(GL_ENABLE_BIT); - - glDisable(GL_LIGHTING); - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - if(color != NULL) { - Vec4f newColor(*color); - newColor.w = alpha; - glColor4fv(newColor.ptr()); - } - else { - glColor4f(1.f, 1.f, 1.f, alpha); - } - renderQuad(x, y, w, h, texture); - - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderConsoleLine3D(int lineIndex, int xPosition, int yPosition, int lineHeight, - Font3D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Vec4f fontColor; - Lang &lang= Lang::getInstance(); - //const Metrics &metrics= Metrics::getInstance(); - FontMetrics *fontMetrics= font->getMetrics(); - - if(game != NULL) { - fontColor = game->getGui()->getDisplay()->getColor(); - } - else { - // white shadowed is default ( in the menu for example ) - //fontColor=Vec4f(1.f, 1.f, 1.f, 0.0f); - fontColor=Vec4f(lineInfo->color.x,lineInfo->color.y,lineInfo->color.z, 0.0f); - } - - Vec4f defaultFontColor = fontColor; - - if(lineInfo->PlayerIndex >= 0) { - std::map &crcPlayerTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::playerTextureCacheLookupKey); - Vec3f playerColor = crcPlayerTextureCache[lineInfo->PlayerIndex]->getPixmap()->getPixel3f(0, 0); - fontColor.x = playerColor.x; - fontColor.y = playerColor.y; - fontColor.z = playerColor.z; - - GameNetworkInterface *gameNetInterface = NetworkManager::getInstance().getGameNetworkInterface(); - if(gameNetInterface != NULL && gameNetInterface->getGameSettings() != NULL) { - const GameSettings *gameSettings = gameNetInterface->getGameSettings(); - string playerName = gameSettings->getNetworkPlayerNameByPlayerIndex(lineInfo->PlayerIndex); - if(playerName != lineInfo->originalPlayerName && lineInfo->originalPlayerName != "") { - playerName = lineInfo->originalPlayerName; - } - if(playerName == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { - playerName = lang.getString("SystemUser"); - } - //printf("playerName [%s], line [%s]\n",playerName.c_str(),line.c_str()); - - //string headerLine = "*" + playerName + ":"; - //string headerLine = playerName + ": "; - string headerLine = playerName; - if(lineInfo->teamMode == true) { - headerLine += " (" + lang.getString("Team") + ")"; - } - headerLine += ": "; - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL"); - } - - renderTextShadow3D( - headerLine, - font, - fontColor, - xPosition, lineIndex * lineHeight + yPosition); - - fontColor = defaultFontColor; - //xPosition += (8 * (playerName.length() + 2)); - // Proper font spacing after username portion of chat text rendering - - //xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); - xPosition += fontMetrics->getTextWidth(headerLine); - } - } - else if(lineInfo->originalPlayerName != "") { - string playerName = lineInfo->originalPlayerName; - //string headerLine = playerName + ": "; - string headerLine = playerName; - if(lineInfo->teamMode == true) { - headerLine += " (" + lang.getString("Team") + ")"; - } - headerLine += ": "; - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL"); - } - - renderTextShadow3D( - headerLine, - font, - fontColor, - xPosition, lineIndex * lineHeight + yPosition); - - fontColor = defaultFontColor; - //xPosition += (8 * (playerName.length() + 2)); - // Proper font spacing after username portion of chat text rendering - //xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); - xPosition += fontMetrics->getTextWidth(headerLine); - } - else { - fontColor = defaultFontColor; - } - - if(stringToHightlight!="" && lineInfo->text.find(stringToHightlight)!=string::npos){ - fontColor=Vec4f(1.f, 0.5f, 0.5f, 0.0f); - } - renderTextShadow3D( - lineInfo->text, - font, - fontColor, - xPosition, (lineIndex * lineHeight) + yPosition); -} - -void Renderer::renderConsoleLine(int lineIndex, int xPosition, int yPosition, int lineHeight, - Font2D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Vec4f fontColor; - Lang &lang= Lang::getInstance(); - - const Metrics &metrics= Metrics::getInstance(); - FontMetrics *fontMetrics= font->getMetrics(); - - if(game != NULL) { - fontColor = game->getGui()->getDisplay()->getColor(); - } - else { - // white shadowed is default ( in the menu for example ) - //fontColor=Vec4f(1.f, 1.f, 1.f, 0.0f); - fontColor=Vec4f(lineInfo->color.x,lineInfo->color.y,lineInfo->color.z, 0.0f); - } - - Vec4f defaultFontColor = fontColor; - - if(lineInfo->PlayerIndex >= 0) { - std::map &crcPlayerTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::playerTextureCacheLookupKey); - Vec3f playerColor = crcPlayerTextureCache[lineInfo->PlayerIndex]->getPixmap()->getPixel3f(0, 0); - fontColor.x = playerColor.x; - fontColor.y = playerColor.y; - fontColor.z = playerColor.z; - - GameNetworkInterface *gameNetInterface = NetworkManager::getInstance().getGameNetworkInterface(); - if(gameNetInterface != NULL && gameNetInterface->getGameSettings() != NULL) { - const GameSettings *gameSettings = gameNetInterface->getGameSettings(); - string playerName = gameSettings->getNetworkPlayerNameByPlayerIndex(lineInfo->PlayerIndex); - if(playerName != lineInfo->originalPlayerName && lineInfo->originalPlayerName != "") { - playerName = lineInfo->originalPlayerName; - } - //printf("playerName [%s], line [%s]\n",playerName.c_str(),line.c_str()); - - //string headerLine = "*" + playerName + ":"; - //string headerLine = playerName + ": "; - string headerLine = playerName; - if(lineInfo->teamMode == true) { - headerLine += " (" + lang.getString("Team") + ")"; - } - headerLine += ": "; - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL"); - } - - renderTextShadow( - headerLine, - font, - fontColor, - xPosition, lineIndex * lineHeight + yPosition); - - fontColor = defaultFontColor; - //xPosition += (8 * (playerName.length() + 2)); - // Proper font spacing after username portion of chat text rendering - xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); - } - } - else if(lineInfo->originalPlayerName != "") { - string playerName = lineInfo->originalPlayerName; - //string headerLine = playerName + ": "; - string headerLine = playerName; - if(lineInfo->teamMode == true) { - headerLine += " (" + lang.getString("Team") + ")"; - } - headerLine += ": "; - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL"); - } - - renderTextShadow( - headerLine, - font, - fontColor, - xPosition, lineIndex * lineHeight + yPosition); - - fontColor = defaultFontColor; - //xPosition += (8 * (playerName.length() + 2)); - // Proper font spacing after username portion of chat text rendering - xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); - } - else { - fontColor = defaultFontColor; - } - - if(stringToHightlight!="" && lineInfo->text.find(stringToHightlight)!=string::npos){ - fontColor=Vec4f(1.f, 0.5f, 0.5f, 0.0f); - } - renderTextShadow( - lineInfo->text, - font, - fontColor, - xPosition, (lineIndex * lineHeight) + yPosition); -} - -void Renderer::renderConsole(const Console *console, ConsoleMode mode , int overrideMaxConsoleLines){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(console == NULL) { - throw megaglest_runtime_error("console == NULL"); - } - - glPushAttrib(GL_ENABLE_BIT); - glEnable(GL_BLEND); - - if(mode==consoleFull) { - int x= console->getXPos()-5; - int y= console->getYPos()-5; - int h= console->getLineHeight()*console->getStoredLineCount(); - - if(h > 0) { - int w= 1000; - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(0.0f, 0.0f, 0.0f, 0.8f) ; - - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y); - glVertex2i(x, y+h); - glVertex2i(x+w, y); - glVertex2i(x+w, y+h); - glEnd(); - glPopAttrib(); - } - for(int i = 0; i < console->getStoredLineCount(); ++i) { - const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); - if(renderText3DEnabled == true) { - renderConsoleLine3D(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont3D(), - console->getStringToHighlight(), &lineInfo); - } - else { - renderConsoleLine(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont(), - console->getStringToHighlight(), &lineInfo); - } - } - } - else if(mode==consoleStoredOnly) { - int allowedMaxLines = (overrideMaxConsoleLines >= 0 ? overrideMaxConsoleLines : maxConsoleLines); - for(int i = 0; i < console->getStoredLineCount() && i < allowedMaxLines; ++i) { - const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); - if(renderText3DEnabled == true) { - renderConsoleLine3D(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); - } - else { - renderConsoleLine(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); - } - } - } - else if(mode==consoleStoredAndNormal) { - int allowedMaxLines = (overrideMaxConsoleLines >= 0 ? overrideMaxConsoleLines : maxConsoleLines); - float starttimestamp=-1; - int consoleIndex=0; - for(int i = 0; i < console->getLineCount() && i < allowedMaxLines; ++i) { - const ConsoleLineInfo &lineInfo = console->getLineItem(i); - if(starttimestamp>lineInfo.timeStamp || starttimestamp==-1) starttimestamp=lineInfo.timeStamp; - if(renderText3DEnabled == true) { - renderConsoleLine3D(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); - } - else { - renderConsoleLine(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); - } - consoleIndex++; - } - for(int i = 0; i < console->getStoredLineCount() && consoleIndex < allowedMaxLines; ++i) { - const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); - if( lineInfo.timeStampgetXPos(), console->getYPos(), - console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); - } - else { - renderConsoleLine(consoleIndex, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); - } - consoleIndex++; - } - } - } - else if(mode==consoleNormal) { - for(int i = 0; i < console->getLineCount(); ++i) { - const ConsoleLineInfo &lineInfo = console->getLineItem(i); - if(renderText3DEnabled == true) { - renderConsoleLine3D(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); - } - else { - renderConsoleLine(i, console->getXPos(), console->getYPos(), - console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); - } - } - } - glPopAttrib(); -} - -void Renderer::renderChatManager(const ChatManager *chatManager) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Vec4f fontColor; - Lang &lang= Lang::getInstance(); - - if(chatManager->getEditEnabled()) { - Vec4f color=Vec4f(0.0f,0.0f,0.0f,0.6f); - string text=""; - - if(chatManager->isInCustomInputMode() == true) { - text += lang.getString("CellHint"); - } - else if(chatManager->getInMenu()) { - text += lang.getString("Chat"); - } - else if(chatManager->getTeamMode()) { - text += lang.getString("Team"); - } - else { - text += lang.getString("All"); - } - text += ": " + chatManager->getText() + "_"; - - if(game != NULL) { - fontColor = game->getGui()->getDisplay()->getColor(); - } - else { - // white shadowed is default ( in the menu for example ) - fontColor=Vec4f(1.f, 1.f, 1.f, 0.0f); - } - - // render Background - int x=chatManager->getXPos(); - int y=chatManager->getYPos()-6; - int h=22; - int w=830; - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(color.x, color.y, color.z, color.w) ; - - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y); - glVertex2i(x, y+h); - glVertex2i(x+w, y); - glVertex2i(x+w, y+h); - glEnd(); - glPopAttrib(); - - if(renderText3DEnabled == true) { - renderTextShadow3D( - text, - chatManager->getFont3D(), - fontColor, - chatManager->getXPos(), chatManager->getYPos()); - } - else { - renderTextShadow( - text, - chatManager->getFont(), - fontColor, - chatManager->getXPos(), chatManager->getYPos()); - } - } - else - { - if (chatManager->getInMenu()) { - string text = "\t\t\t\t\t>> "+lang.getString("PressEnterToChat")+" <<"; - fontColor = Vec4f(0.5f, 0.5f, 0.5f, 0.5f); - - if(renderText3DEnabled == true) { - renderTextShadow3D(text, chatManager->getFont3D(), fontColor, - chatManager->getXPos(), chatManager->getYPos()); - } - else { - renderTextShadow(text, chatManager->getFont(), fontColor, - chatManager->getXPos(), chatManager->getYPos()); - } - } - } -} - - -void Renderer::renderPerformanceStats() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const Metrics &metrics = Metrics::getInstance(); - const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); - - char szBuf[200]=""; - snprintf(szBuf,200,"Frame: %d",game->getWorld()->getFrameCount() / 20); - string str = string(szBuf) + string("\n"); - - static time_t lastGamePerfCheck = time(NULL); - static string gamePerfStats = ""; - if(difftime((long int)time(NULL),lastGamePerfCheck) > 3) { - lastGamePerfCheck = time(NULL); - gamePerfStats = game->getGamePerformanceCounts(true); - } - - if(gamePerfStats != "") { - str += gamePerfStats + "\n"; - } - - if(renderText3DEnabled == true) { - renderTextShadow3D( - str, CoreData::getInstance().getDisplayFontSmall3D(), - fontColor, - 10, metrics.getVirtualH()-180, false); - } - else { - renderTextShadow( - str, CoreData::getInstance().getDisplayFontSmall(), - fontColor, - 10, metrics.getVirtualH()-180, false); - } -} - -void Renderer::renderClock() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("InGameClock","true") == false && - config.getBool("InGameLocalClock","true") == false && - config.getBool("InGameFrameCounter","false") == false) { - return; - } - - string str = ""; - const Metrics &metrics = Metrics::getInstance(); - const World *world = game->getWorld(); - const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); - - if(config.getBool("InGameClock","true") == true) { - Lang &lang= Lang::getInstance(); - char szBuf[501]=""; - - //int hours = world->getTimeFlow()->getTime(); - //int minutes = (world->getTimeFlow()->getTime() - hours) * 100 * 0.6; // scale 100 to 60 - //snprintf(szBuf,200,"%s %.2d:%.2d",lang.getString("GameTime","",true).c_str(),hours,minutes); - // string header2 = lang.getString("GameDurationTime","",true) + ": " + getTimeString(stats.getFramesToCalculatePlaytime()); - snprintf(szBuf,500,"%s %s",lang.getString("GameDurationTime","",true).c_str(),getTimeDuationString(world->getFrameCount(),GameConstants::updateFps).c_str()); - if(str != "") { - str += " "; - } - str += szBuf; - } - - if(config.getBool("InGameLocalClock","true") == true) { - //time_t nowTime = time(NULL); - //struct tm *loctime = localtime(&nowTime); - struct tm loctime = threadsafe_localtime(systemtime_now()); - char szBuf2[100]=""; - strftime(szBuf2,100,"%H:%M",&loctime); - - Lang &lang= Lang::getInstance(); - char szBuf[200]=""; - snprintf(szBuf,200,"%s %s",lang.getString("LocalTime","",true).c_str(),szBuf2); - if(str != "") { - str += " "; - } - str += szBuf; - } - - if(config.getBool("InGameFrameCounter","false") == true) { - char szBuf[200]=""; - snprintf(szBuf,200,"Frame: %d",game->getWorld()->getFrameCount() / 20); - if(str != "") { - str += " "; - } - str += szBuf; - } - - //string str = szBuf; - - if(renderText3DEnabled == true) { - renderTextShadow3D( - str, CoreData::getInstance().getDisplayFontSmall3D(), - fontColor, - 10, metrics.getVirtualH()-160, false); - } - else { - renderTextShadow( - str, CoreData::getInstance().getDisplayFontSmall(), - fontColor, - 10, metrics.getVirtualH()-160, false); - } -} - -void Renderer::renderResourceStatus() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const World *world = game->getWorld(); - Config &config= Config::getInstance(); - - if(world->getThisFactionIndex() < 0 || - world->getThisFactionIndex() >= world->getFactionCount()) { - return; - } - - const Faction *thisFaction = world->getFaction(world->getThisFactionIndex()); - - assertGl(); - glPushAttrib(GL_ENABLE_BIT); - - int rowsRendered = 0; - int resourceCountRendered = 0; - bool twoRessourceLines=false; - - bool sharedTeamUnits = game != NULL && game->getGui() != NULL - && game->isFlagType1BitEnabled(ft1_allow_shared_team_units) - == true; - bool sharedTeamResources = game != NULL && game->getGui() != NULL - && game->isFlagType1BitEnabled( - ft1_allow_shared_team_resources) == true; - - bool renderSharedTeamResources=false; - bool renderSharedTeamUnits=false; - bool renderLocalFactionResources=false; - - if(config.getBool("TwoLineTeamResourceRendering","false") == true) { - if( sharedTeamResources == true || sharedTeamUnits == true){ - twoRessourceLines=true; - } - if(sharedTeamResources == true){ - renderSharedTeamResources=true; - renderSharedTeamUnits=true; - } - else if(sharedTeamUnits == true){ - renderSharedTeamUnits=true; - renderLocalFactionResources=true; - } - else{ - renderLocalFactionResources=true; - } - } - else { - if(sharedTeamResources == true) - renderSharedTeamResources=true; - else if(sharedTeamUnits == true) - renderSharedTeamUnits=true; - else - renderLocalFactionResources=true; - } - - if(renderSharedTeamResources == true) { - resourceCountRendered = 0; - for(int techTreeResourceTypeIndex = 0; - techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); - ++techTreeResourceTypeIndex) { - - const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); - - if ( rt->getDisplayInHud() == false ) { - continue; - } - - bool showResource = world->showResourceTypeForTeam(rt, thisFaction->getTeam()); - if(showResource == true) { - rowsRendered = renderResource(thisFaction, - false, twoRessourceLines, rt, 0, - resourceCountRendered); - } - } - if(resourceCountRendered > 0) { - rowsRendered++; - } - } - - if(renderLocalFactionResources == true){ - resourceCountRendered = 0; - - const Faction *factionForResourceView = thisFaction; - bool localFactionResourcesOnly = true; - - for(int techTreeResourceTypeIndex = 0; - techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); - ++techTreeResourceTypeIndex) { - const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); - if ( rt->getDisplayInHud() == false ) { - continue; - } - - //if any unit produces the resource - bool showResource; - if (twoRessourceLines) - showResource = world->showResourceTypeForTeam(rt, - factionForResourceView->getTeam()); - else - showResource = world->showResourceTypeForFaction(rt, - factionForResourceView); - if(showResource == true) { - renderResource(factionForResourceView, localFactionResourcesOnly, - twoRessourceLines, rt, rowsRendered, resourceCountRendered); - } - } - if(resourceCountRendered > 0) { - rowsRendered++; - } - } - - if(renderSharedTeamUnits == true){ - resourceCountRendered = 0; - - const Faction *factionForResourceView = thisFaction; - bool localFactionResourcesOnly = true; - - const Gui *gui = game->getGui(); - if(gui != NULL) { - const Selection *selection = gui->getSelection(); - if(selection != NULL && selection->getCount() > 0 && selection->getFrontUnit() != NULL) { - const Unit *selectedUnit = selection->getFrontUnit(); - if(selectedUnit != NULL && selectedUnit->getFaction()->isAlly(thisFaction) == true) { - factionForResourceView = selectedUnit->getFaction(); - } - } - } - - for(int techTreeResourceTypeIndex = 0; - techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); - ++techTreeResourceTypeIndex) { - const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); - if ( rt->getDisplayInHud() == false ) { - continue; - } - - //if any unit produces the resource - bool showResource; - if (twoRessourceLines) - showResource = world->showResourceTypeForTeam(rt, - factionForResourceView->getTeam()); - else - showResource = world->showResourceTypeForFaction(rt, - factionForResourceView); - - if(showResource == true) { - renderResource(factionForResourceView, localFactionResourcesOnly, - twoRessourceLines, rt, rowsRendered, resourceCountRendered); - } - } - //if(resourceCountRendered > 0) { - // rowsRendered++; - //} - } - - glPopAttrib(); - - assertGl(); -} - -int Renderer::renderResource(const Faction *factionForResourceView,bool localFactionResourcesOnly, - bool twoResourceLines, const ResourceType *rt, int startRow, int &resourceCountRendered) { - - const Metrics &metrics = Metrics::getInstance(); - const int MAX_RESOURCES_PER_ROW = 6; - - int resourceRowHeigth=30; - int resourceYStart=metrics.getVirtualH()-30; - if(twoResourceLines){ - // we need to save some space - resourceYStart=metrics.getVirtualH()-22; - resourceRowHeigth=16; - } - - //draw resource status - if(localFactionResourcesOnly == true) { - Vec4f resourceFontColor = Vec4f(factionForResourceView->getTexture()->getPixmapConst()->getPixel3f(0,0)); - int resourceCol = 0; - int resourceRow = startRow; - - int x=resourceCol * 100 + 190; - int y=resourceYStart - (resourceRowHeigth * resourceRow); - int h=16; - int w=8; - glColor3f(resourceFontColor.x,resourceFontColor.y,resourceFontColor.z); - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y+h); - glVertex2i(x, y); - glVertex2i(x+w, y+h/2); - glEnd(); - } - - const Resource *r = factionForResourceView->getResource(rt,localFactionResourcesOnly); - string str = intToStr(r->getAmount()); - - glEnable(GL_TEXTURE_2D); - - const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); - Vec4f resourceFontColor = fontColor; - - bool isNegativeConsumableDisplayCycle = false; - if(rt->getClass() == rcConsumable) { - // Show in yellow/orange/red font if negative - if(r->getBalance() * 5 + r->getAmount() < 0) { - if(time(NULL) % 2 == 0) { - - isNegativeConsumableDisplayCycle = true; - if(r->getBalance() * 1 + r->getAmount() < 0) { - glColor3f(RED.x,RED.y,RED.z); - resourceFontColor = RED; - } - else if(r->getBalance() * 3 + r->getAmount() < 0) { - glColor3f(ORANGE.x,ORANGE.y,ORANGE.z); - resourceFontColor = ORANGE; - } - else if(r->getBalance() * 5 + r->getAmount() < 0) { - glColor3f(YELLOW.x,YELLOW.y,YELLOW.z); - resourceFontColor = YELLOW; - } - } - } - } - - if(isNegativeConsumableDisplayCycle == false) { - glColor3f(1.f, 1.f, 1.f); - } - - int resourceRow = startRow + (resourceCountRendered > 0 ? resourceCountRendered / MAX_RESOURCES_PER_ROW : 0); - int resourceCol = resourceCountRendered % MAX_RESOURCES_PER_ROW; - - renderQuad(resourceCol * 100 + 200, resourceYStart - (resourceRowHeigth * resourceRow), 16, 16, rt->getImage()); - - if(rt->getClass() != rcStatic) { - str+= "/" + intToStr(factionForResourceView->getStoreAmount(rt,localFactionResourcesOnly)); - } - if(rt->getClass() == rcConsumable) { - str+= "("; - if(r->getBalance() > 0) { - str+= "+"; - } - str+= intToStr(r->getBalance()) + ")"; - } - - glDisable(GL_TEXTURE_2D); - - if(renderText3DEnabled == true) { - renderTextShadow3D( - str, CoreData::getInstance().getDisplayFontSmall3D(), - resourceFontColor, - resourceCol * 100 + 220, resourceYStart - (resourceRowHeigth * resourceRow), false); - } - else { - renderTextShadow( - str, CoreData::getInstance().getDisplayFontSmall(), - resourceFontColor, - resourceCol * 100 + 220, resourceYStart - (resourceRowHeigth * resourceRow), false); - } - ++resourceCountRendered; - - return resourceRow; -} - -void Renderer::renderSelectionQuad() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("RecordMode","false") == true) { - return; - } - - const Gui *gui= game->getGui(); - const SelectionQuad *sq= gui->getSelectionQuad(); - - Vec2i down= sq->getPosDown(); - Vec2i up= sq->getPosUp(); - - if(gui->isSelecting()) { - glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT); - - Vec2i vertices[4]; - vertices[0] = Vec2i(down.x, down.y); - vertices[1] = Vec2i(up.x, down.y); - vertices[2] = Vec2i(up.x, up.y); - vertices[3] = Vec2i(down.x, up.y); - - glColor3f(0,1,0); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(2, GL_INT, 0, &vertices[0]); - glDrawArrays(GL_LINE_LOOP, 0, 4); - glDisableClientState(GL_VERTEX_ARRAY); - -/* - glColor3f(0,1,0); - glBegin(GL_LINE_LOOP); - glVertex2i(down.x, down.y); - glVertex2i(up.x, down.y); - glVertex2i(up.x, up.y); - glVertex2i(down.x, up.y); - glEnd(); -*/ - glPopAttrib(); - } -} - -Vec2i computeCenteredPos(const string &text, Font2D *font, int x, int y) { - if(font == NULL) { - //abort(); - throw megaglest_runtime_error("font == NULL (1) text = " + text); - } - const Metrics &metrics= Metrics::getInstance(); - FontMetrics *fontMetrics= font->getMetrics(); - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL (1) text = " + text); - } - - int virtualX = (fontMetrics->getTextWidth(text) > 0 ? static_cast(fontMetrics->getTextWidth(text)/2.f) : 5); - int virtualY = (fontMetrics->getHeight(text) > 0 ? static_cast(fontMetrics->getHeight(text)/2.f) : 5); - - Vec2i textPos( - x-metrics.toVirtualX(virtualX), - y-metrics.toVirtualY(virtualY)); - - //printf("text [%s] x = %d y = %d virtualX = %d virtualY = %d fontMetrics->getHeight() = %f\n",text.c_str(),x,y,virtualX,virtualY,fontMetrics->getHeight()); - - return textPos; -} - -Vec2i computeCenteredPos(const string &text, Font3D *font, int x, int y) { - if(font == NULL) { - throw megaglest_runtime_error("font == NULL (2) text = " + text); - } - const Metrics &metrics= Metrics::getInstance(); - FontMetrics *fontMetrics= font->getMetrics(); - - if(fontMetrics == NULL) { - throw megaglest_runtime_error("fontMetrics == NULL (2) text = " + text); - } - - int virtualX = (fontMetrics->getTextWidth(text) > 0 ? static_cast(fontMetrics->getTextWidth(text) / 2.f) : 5); - int virtualY = (fontMetrics->getHeight(text) > 0 ? static_cast(fontMetrics->getHeight(text) / 2.f) : 5); - - Vec2i textPos( - x-metrics.toVirtualX(virtualX), - y-metrics.toVirtualY(virtualY)); - - return textPos; -} - -void Renderer::renderTextSurroundingBox(int x, int y, int w, int h, - int maxEditWidth, int maxEditRenderWidth) { - //glColor4fv(color.ptr()); - //glBegin(GL_QUADS); // Start drawing a quad primitive - - //printf("A w = %d maxEditWidth = %d maxEditRenderWidth = %d\n",w,maxEditWidth,maxEditRenderWidth); - if(maxEditWidth >= 0 || maxEditRenderWidth >= 0) { - //printf("B w = %d maxEditWidth = %d maxEditRenderWidth = %d\n",w,maxEditWidth,maxEditRenderWidth); - if(maxEditRenderWidth >= 0) { - w = maxEditRenderWidth; - } - else { - w = maxEditWidth; - } - } - //printf("HI!!!\n"); - glPointSize(20.0f); - - int margin = 4; - //glBegin(GL_POINTS); // Start drawing a point primitive - glBegin(GL_LINE_LOOP); // Start drawing a line primitive - - glVertex3f(x, y+h, 0.0f); // The bottom left corner - glVertex3f(x, y-margin, 0.0f); // The top left corner - glVertex3f(x+w, y-margin, 0.0f); // The top right corner - glVertex3f(x+w, y+h, 0.0f); // The bottom right corner - glEnd(); -} - -void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, - float alpha, int x, int y, int w, int h, bool centeredW, bool centeredH, - bool editModeEnabled,int maxEditWidth, int maxEditRenderWidth) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); - - Vec2f pos= Vec2f(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - if(centeredW == true || centeredH == true) { - getCentered3DPos(text, font, pos, w, h, centeredW, centeredH); - } - - if(editModeEnabled) { - if(maxEditWidth >= 0 || maxEditRenderWidth >= 0) { - int useWidth = maxEditWidth; - string temp = ""; - for(int i = 0; i < useWidth; ++i) { - temp += DEFAULT_CHAR_FOR_WIDTH_CALC; - } - float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); - useWidth = (int)lineWidth; - - maxEditWidth = useWidth; - } - - renderTextSurroundingBox(pos.x, pos.y, w, h,maxEditWidth,maxEditRenderWidth); - } - glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y); - safeTextRender.end(); - - glDisable(GL_BLEND); - glPopAttrib(); -} - -void Renderer::renderText3D(const string &text, Font3D *font, float alpha, int x, int y, bool centered) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); - - Vec2i pos= Vec2i(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y, centered); - //textRenderer3D->end(); - safeTextRender.end(); - - glDisable(GL_BLEND); - glPopAttrib(); -} - -void Renderer::renderText(const string &text, Font2D *font, float alpha, int x, int y, bool centered) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); - - Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer,font); - textRenderer->render(text, pos.x, pos.y); - safeTextRender.end(); - - glPopAttrib(); -} - -Vec2f Renderer::getCentered3DPos(const string &text, Font3D *font, Vec2f &pos, int w, int h,bool centeredW, bool centeredH) { - if(centeredW == true) { - if(font == NULL) { - //abort(); - throw megaglest_runtime_error("font == NULL (5) text = " + text); - } - else if(font->getTextHandler() == NULL) { - char szBuf[8096] = ""; - snprintf(szBuf, 8096, "font->getTextHandler() == NULL(5) text = [%s] FontPtr = [%p]\n", text.c_str(),font); - throw megaglest_runtime_error(szBuf); - } - - float lineWidth = (font->getTextHandler()->Advance(text.c_str()) * ::Shared::Graphics::Font::scaleFontValue); - if(lineWidth < w) { - pos.x += ((w / 2.f) - (lineWidth / 2.f)); - } - } - - if(centeredH) { - if(font == NULL) { - throw megaglest_runtime_error("font == NULL (6) text = " + text); - } - else if(font->getTextHandler() == NULL) { - throw megaglest_runtime_error("font->getTextHandler() == NULL (6) text = " + text); - } - - //const Metrics &metrics= Metrics::getInstance(); - //float lineHeight = (font->getTextHandler()->LineHeight(text.c_str()) * Font::scaleFontValue); - float lineHeight = (font->getTextHandler()->LineHeight(text.c_str()) * ::Shared::Graphics::Font::scaleFontValue); - //lineHeight=metrics.toVirtualY(lineHeight); - //lineHeight= lineHeight / (2.f + 0.2f * FontMetrics::DEFAULT_Y_OFFSET_FACTOR); - //pos.y += (h / 2.f) - (lineHeight / 2.f); - //pos.y += (h / 2.f) - (lineHeight); - //pos.y += (lineHeight / 2.f); // y starts at the middle of the render position, so only move up 1/2 the font height - - if(lineHeight < h) { - //printf("line %d, lineHeight [%f] h [%d] text [%s]\n",__LINE__,lineHeight,h,text.c_str()); - - //if(Font::forceFTGLFonts == true) { - // First go to top of bounding box - pos.y += (h - lineHeight); - pos.y -= ((h - lineHeight) / ::Shared::Graphics::Font::scaleFontValueCenterHFactor); -// } -// else { -// pos.y += (float)(((float)h) / 2.0); -// float heightGap = (float)(((float)h - lineHeight) / 2.0); -// pos.y -= heightGap; -// -// //printf("h = %d lineHeight = %f heightGap = %f\n",h,lineHeight,heightGap); -// -// // Now calculate till we get text to middle -// //pos.y -= (realHeight / 2); -// //pos.y += (lineHeight / 2); -// } - } - else if(lineHeight > h) { - //printf("line %d, lineHeight [%f] h [%d] text [%s]\n",__LINE__,lineHeight,h,text.c_str()); - - pos.y += (std::ceil(lineHeight - h)); - } - } - return pos; -} - -void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, - const Vec3f &color, int x, int y, int w, int h, bool centeredW, - bool centeredH, bool editModeEnabled,int maxEditWidth, int maxEditRenderWidth) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_CURRENT_BIT); - glColor3fv(color.ptr()); - - Vec2f pos= Vec2f(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - if(centeredW == true || centeredH == true) { - getCentered3DPos(text, font, pos, w, h,centeredW,centeredH); - } - - if(editModeEnabled) { - if(maxEditWidth >= 0 || maxEditRenderWidth >= 0) { - int useWidth = maxEditWidth; - string temp = ""; - for(int i = 0; i < useWidth; ++i) { - temp += DEFAULT_CHAR_FOR_WIDTH_CALC; - } - float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); - useWidth = (int)lineWidth; - - maxEditWidth = useWidth; - } - - renderTextSurroundingBox(pos.x, pos.y, w, h,maxEditWidth,maxEditRenderWidth); - } - glColor3fv(color.ptr()); - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y); - safeTextRender.end(); - - glPopAttrib(); -} - -void Renderer::renderText3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, bool centered) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_CURRENT_BIT); - glColor3fv(color.ptr()); - - Vec2i pos= Vec2i(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y, centered); - safeTextRender.end(); - - glPopAttrib(); -} - -void Renderer::renderText(const string &text, Font2D *font, const Vec3f &color, int x, int y, bool centered){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_CURRENT_BIT); - glColor3fv(color.ptr()); - - Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer,font); - textRenderer->render(text, pos.x, pos.y); - safeTextRender.end(); - - glPopAttrib(); -} - -void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, - const Vec4f &color, int x, int y, int w, int h, bool centeredW, - bool centeredH, bool editModeEnabled,int maxEditWidth, int maxEditRenderWidth) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(color.ptr()); - - Vec2f pos= Vec2f(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - if(centeredW == true || centeredH == true) { - getCentered3DPos(text, font, pos, w, h,centeredW,centeredH); - } - - if(editModeEnabled) { - if(maxEditWidth >= 0 || maxEditRenderWidth >= 0) { - int useWidth = maxEditWidth; - string temp = ""; - for(int i = 0; i < useWidth; ++i) { - temp += DEFAULT_CHAR_FOR_WIDTH_CALC; - } - float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); - useWidth = (int)lineWidth; - - maxEditWidth = useWidth; - } - - renderTextSurroundingBox(pos.x, pos.y, w, h,maxEditWidth,maxEditRenderWidth); - } - glColor4fv(color.ptr()); - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y); - safeTextRender.end(); - - glDisable(GL_BLEND); - glPopAttrib(); -} - -void Renderer::renderText3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(color.ptr()); - - Vec2i pos= Vec2i(x, y); - //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - textRenderer3D->render(text, pos.x, pos.y, centered); - safeTextRender.end(); - - glDisable(GL_BLEND); - glPopAttrib(); -} - -void Renderer::renderText(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glColor4fv(color.ptr()); - - Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer,font); - textRenderer->render(text, pos.x, pos.y); - safeTextRender.end(); - - glPopAttrib(); -} - -void Renderer::renderTextShadow3D(const string &text, Font3D *font,const Vec4f &color, int x, int y, bool centered) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(font == NULL) { - throw megaglest_runtime_error("font == NULL (3) text = " + text); - } - - glPushAttrib(GL_CURRENT_BIT); - - Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer3D,font); - if(color.w < 0.5) { - glColor3f(0.0f, 0.0f, 0.0f); - - textRenderer3D->render(text, pos.x-1.0f, pos.y-1.0f); - } - glColor3f(color.x,color.y,color.z); - - textRenderer3D->render(text, pos.x, pos.y); - //textRenderer3D->end(); - safeTextRender.end(); - - glPopAttrib(); -} - -void Renderer::renderTextShadow(const string &text, Font2D *font,const Vec4f &color, int x, int y, bool centered){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(font == NULL) { - throw megaglest_runtime_error("font == NULL (4) text = " + text); - } - - glPushAttrib(GL_CURRENT_BIT); - - Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); - - TextRendererSafeWrapper safeTextRender(textRenderer,font); - if(color.w < 0.5) { - glColor3f(0.0f, 0.0f, 0.0f); - - textRenderer->render(text, pos.x-1.0f, pos.y-1.0f); - } - glColor3f(color.x,color.y,color.z); - - textRenderer->render(text, pos.x, pos.y); - //textRenderer->end(); - safeTextRender.end(); - - glPopAttrib(); -} - -// ============= COMPONENTS ============================= - -void Renderer::renderLabel(GraphicLabel *label) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(label->getEditable() && label->getMaxEditRenderWidth()>0) - { - int x= label->getX(); - int y= label->getY(); - int h= label->getH(); - int w= label->getMaxEditRenderWidth(); - if(h>0){ - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(0.2f, 0.2f, 0.2f, 0.6f*label->getFade()) ; - - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y); - glVertex2i(x, y+h); - glVertex2i(x+w, y); - glVertex2i(x+w, y+h); - glEnd(); - glPopAttrib(); - } - } - - if(label->getRenderBackground()) - { - int x= label->getX(); - int y= label->getY(); - int h= label->getH(); - int w= label->getW(); - if(label->getMaxEditRenderWidth()>0){ - w= label->getMaxEditRenderWidth(); - } - Vec4f color=label->getBackgroundColor(); - if(h>0){ - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(color.x, color.y, color.z, color.w*label->getFade()) ; - - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y); - glVertex2i(x, y+h); - glVertex2i(x+w, y); - glVertex2i(x+w, y+h); - glEnd(); - glPopAttrib(); - } - } - - if(label->getTexture()!=NULL ) - { - int x= label->getX(); - int y= label->getY(); - int h= label->getH(); - int w= label->getW(); - if(h>0){ - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast( label->getTexture())->getHandle()); - glColor4f(1.0f, 1.0f, 1.0f, 1.0f*label->getFade()) ; - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - glEnd(); - glDisable(GL_TEXTURE_2D); - glPopAttrib(); - } - } - Vec3f labelColor=label->getTextColor(); - Vec4f colorWithAlpha = Vec4f(labelColor.x,labelColor.y,labelColor.z,GraphicComponent::getFade()); - renderLabel(label,&colorWithAlpha); -} - -void Renderer::renderLabel(GraphicLabel *label,const Vec3f *color) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(color != NULL) { - Vec4f colorWithAlpha = Vec4f(*color); - colorWithAlpha.w = GraphicComponent::getFade(); - renderLabel(label,&colorWithAlpha); - } - else { - Vec4f *colorWithAlpha = NULL; - renderLabel(label,colorWithAlpha); - } -} - -void Renderer::renderLabel(GraphicLabel *label,const Vec4f *color) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(label->getVisible() == false) { - return; - } - try { - glPushAttrib(GL_ENABLE_BIT); - glEnable(GL_BLEND); - - vector lines; - string renderTextString = (label->getTextNativeTranslation() != "" ? label->getTextNativeTranslation() : label->getText()); - if(label->getWordWrap() == true) { - Tokenize(renderTextString,lines,"\n"); - } - else { - lines.push_back(renderTextString); - } - - for(unsigned int i = 0; i < lines.size(); ++i) { - Vec2i textPos; - int x= label->getX(); - int y= label->getY() - (i * label->getH()); - int h= label->getH(); - int w= label->getW(); - //if(label->getInstanceName() == "modDescrLabel") printf("~~~ lines.size() [%u] i = %d lines[i] [%s] y = %d\n",lines.size(),i,lines[i].c_str(),y); - - if(label->getCentered()) { - textPos= Vec2i(x+w/2, y+h/2); - } - else { - textPos= Vec2i(x, y+h/4); - } - - string renderTextStr = lines[i]; - if(label->getIsPassword() == true) { - if(renderTextStr != "") { - renderTextStr = "*****"; - } - } - - if(color != NULL) { - if(renderText3DEnabled == true) { - //renderText3D(lines[i], label->getFont3D(), (*color), textPos.x, textPos.y, label->getCentered()); - //printf("Text Render3D [%s] font3d [%p]\n",lines[i].c_str(),label->getFont3D()); - //printf("Label render C\n"); - - renderTextBoundingBox3D(renderTextStr, label->getFont3D(), (*color), - x, y, w, h, label->getCenteredW(),label->getCenteredH(), - label->getEditModeEnabled(),label->getMaxEditWidth(), - label->getMaxEditRenderWidth()); - } - else { - //printf("Label render D\n"); - renderText(renderTextStr, label->getFont(), (*color), textPos.x, textPos.y, label->getCentered()); - } - } - else { - if(renderText3DEnabled == true) { - //renderText3D(lines[i], label->getFont3D(), GraphicComponent::getFade(), textPos.x, textPos.y, label->getCentered()); - //printf("Text Render3D [%s] font3d [%p]\n",lines[i].c_str(),label->getFont3D()); - //printf("Label render E\n"); - renderTextBoundingBox3D(renderTextStr, label->getFont3D(), - GraphicComponent::getFade(), x, y, w, h, - label->getCenteredW(),label->getCenteredH(), - label->getEditModeEnabled(),label->getMaxEditWidth(), - label->getMaxEditRenderWidth()); - } - else { - //printf("Label render F\n"); - renderText(renderTextStr, label->getFont(), GraphicComponent::getFade(), textPos.x, textPos.y, label->getCentered()); - } - } - } - glPopAttrib(); - } - catch(const exception &e) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s] For Control [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),label->getInstanceName().c_str()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - - throw megaglest_runtime_error(szBuf); - } - -} - -void Renderer::renderButton(GraphicButton *button, const Vec4f *fontColorOverride, bool *lightedOverride) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(button->getVisible() == false) { - return; - } - - try { - - //char szBuf[8096]=""; - //snprintf(szBuf,8096,"In [%s::%s Line: %d]\n For Control container [%s] name [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,button->getContainerName().c_str(), button->getInstanceName().c_str()); - //printf(szBuf); - - int x= button->getX(); - int y= button->getY(); - int h= button->getH(); - int w= button->getW(); - - const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); - - //background - CoreData &coreData= CoreData::getInstance(); - Texture2D *backTexture = NULL; - - if(button->getUseCustomTexture() == true) { - backTexture = dynamic_cast(button->getCustomTexture()); - } - else { - backTexture = w > 3 * h / 2 ? coreData.getButtonBigTexture(): coreData.getButtonSmallTexture(); - } - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - if(backTexture != NULL) { - glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); - } - else { - glBindTexture(GL_TEXTURE_2D, 0); - } - - //button - Vec4f fontColor(GraphicComponent::getCustomTextColor()); - - if(fontColorOverride != NULL) { - fontColor= *fontColorOverride; - } - else { - // white shadowed is default ( in the menu for example ) - fontColor.w = GraphicComponent::getFade(); - } - - //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); - Vec4f color= fontColor; - glColor4fv(color.ptr()); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - - glEnd(); - - glDisable(GL_TEXTURE_2D); - - //lighting - float anim= GraphicComponent::getAnim(); - if(anim>0.5f) anim= 1.f-anim; - - bool renderLighted = (button->getLighted() && button->getEditable()); - if(lightedOverride != NULL) { - renderLighted = *lightedOverride; - } - if(renderLighted) { - const int lightSize= 0; - const Vec4f color1= Vec4f(color.x, color.y, color.z, 0.1f+anim*0.5f); - const Vec4f color2= Vec4f(color.x, color.y, color.z, 0.3f+anim); - - glBegin(GL_TRIANGLE_FAN); - - glColor4fv(color2.ptr()); - glVertex2f(x+w/2, y+h/2); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glEnd(); - } - - //Vec2i textPos= Vec2i(x + w / 2, y + h / 2); - - if(button->getEditable()) { - if(renderText3DEnabled == true) { - //renderText3D(button->getText(), button->getFont3D(), color,x + (w / 2), y + (h / 2), true); - renderTextBoundingBox3D(button->getText(), button->getFont3D(), - color, x, y, w, h, true, true,false,-1,-1); - } - else { - renderText(button->getText(), button->getFont(), color,x + (w / 2), y + (h / 2), true); - } - } - else { - if(renderText3DEnabled == true) { - //renderText3D(button->getText(), button->getFont3D(),disabledTextColor, - // x + (w / 2), y + (h / 2), true); - renderTextBoundingBox3D(button->getText(), button->getFont3D(),disabledTextColor, - x, y, w, h, true, true,false,-1,-1); - } - else { - renderText(button->getText(), button->getFont(),disabledTextColor, - x + (w / 2), y + (h / 2), true); - } - } - - glPopAttrib(); - } - catch(const exception &e) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s] For Control container [%s] name [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what(),button->getContainerName().c_str(), button->getInstanceName().c_str()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - - throw megaglest_runtime_error(szBuf); - } -} - -void Renderer::renderCheckBox(const GraphicCheckBox *box) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(box->getVisible() == false) { - return; - } - - int x= box->getX(); - int y= box->getY(); - int h= box->getH(); - int w= box->getW(); - - //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); - - //background - CoreData &coreData= CoreData::getInstance(); - Texture2D *backTexture= box->getValue()? coreData.getCheckedCheckBoxTexture(): coreData.getCheckBoxTexture(); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); - - //box - Vec4f fontColor; - //if(game!=NULL){ - // fontColor=game->getGui()->getDisplay()->getColor(); - // fontColor.w = GraphicComponent::getFade(); - //} - //else { - // white shadowed is default ( in the menu for example ) - fontColor=Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); - //} - - //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); - Vec4f color= fontColor; - glColor4fv(color.ptr()); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - - glEnd(); - - glDisable(GL_TEXTURE_2D); - - //lighting - float anim= GraphicComponent::getAnim(); - if(anim > 0.5f) { - anim = 1.f - anim; - } - - if(box->getLighted() && box->getEditable()) { - const int lightSize= 0; - const Vec4f color1= Vec4f(color.x, color.y, color.z, 0.1f+anim*0.5f); - const Vec4f color2= Vec4f(color.x, color.y, color.z, 0.3f+anim); - - glBegin(GL_TRIANGLE_FAN); - - glColor4fv(color2.ptr()); - glVertex2f(x+w/2, y+h/2); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glEnd(); - } - - glPopAttrib(); -} - - -void Renderer::renderLine(const GraphicLine *line) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(line->getVisible() == false) { - return; - } - - int x= line->getX(); - int y= line->getY(); - int h= line->getH(); - int w= line->getW(); - - //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); - - //background - CoreData &coreData= CoreData::getInstance(); - Texture2D *backTexture= line->getHorizontal()? coreData.getHorizontalLineTexture(): coreData.getVerticalLineTexture(); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - - glEnd(); - - glDisable(GL_TEXTURE_2D); - glPopAttrib(); -} - -void Renderer::renderScrollBar(const GraphicScrollBar *sb) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(sb->getVisible() == false) { - return; - } - - int x= sb->getX(); - int y= sb->getY(); - int h= sb->getH(); - int w= sb->getW(); - - //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); - ///////////////////// - //background - //////////////////// - CoreData &coreData= CoreData::getInstance(); - Texture2D *backTexture= coreData.getHorizontalLineTexture(); - - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - - glEnd(); - - //////////////////// - // selectBlock - //////////////////// - - x= sb->getX(); - y= sb->getY(); - h= sb->getH(); - w= sb->getW(); - - if( sb->getHorizontal()) { - x=x+sb->getVisibleCompPosStart(); - w=sb->getVisibleCompPosEnd()-sb->getVisibleCompPosStart(); - } - else { - y=y+sb->getVisibleCompPosStart(); - h=sb->getVisibleCompPosEnd()-sb->getVisibleCompPosStart(); - } - - Texture2D *selectTexture= coreData.getButtonBigTexture(); - assert(selectTexture != NULL); - - glBindTexture(GL_TEXTURE_2D, static_cast(selectTexture)->getHandle()); - - //button - Vec4f fontColor; - //if(game!=NULL){ - // fontColor=game->getGui()->getDisplay()->getColor(); - // fontColor.w = GraphicComponent::getFade(); - //} - //else { - // white shadowed is default ( in the menu for example ) - fontColor=Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); - //} - - //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); - Vec4f color= fontColor; - glColor4fv(color.ptr()); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 0.f); - glVertex2f(x, y); - - glTexCoord2f(0.f, 1.f); - glVertex2f(x, y+h); - - glTexCoord2f(1.f, 0.f); - glVertex2f(x+w, y); - - glTexCoord2f(1.f, 1.f); - glVertex2f(x+w, y+h); - - glEnd(); - - glDisable(GL_TEXTURE_2D); - - //lighting - float anim= GraphicComponent::getAnim(); - if(anim>0.5f) anim= 1.f-anim; - - if(sb->getLighted() && sb->getEditable()){ - const int lightSize= 0; - const Vec4f color1= Vec4f(color.x, color.y, color.z, 0.1f+anim*0.5f); - const Vec4f color2= Vec4f(color.x, color.y, color.z, 0.3f+anim); - - glBegin(GL_TRIANGLE_FAN); - - glColor4fv(color2.ptr()); - glVertex2f(x+w/2, y+h/2); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glEnd(); - } - - glPopAttrib(); -} - -void Renderer::renderListBox(GraphicListBox *listBox) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(listBox->getVisible() == false) { - return; - } - //if(listBox->getLeftControlled()==true) - { - int x= listBox->getX(); - int y= listBox->getY(); - int h= listBox->getH(); - int w= listBox->getW(); - if(h>0){ - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(0.0f, 0.0f, 0.0f, 0.6f*listBox->getFade()) ; - - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(x, y); - glVertex2i(x, y+h); - glVertex2i(x+w, y); - glVertex2i(x+w, y+h); - glEnd(); - glPopAttrib(); - } - } - renderButton(listBox->getButton1()); - renderButton(listBox->getButton2()); - - glPushAttrib(GL_ENABLE_BIT); - glEnable(GL_BLEND); - - GraphicLabel label("ListBox_render_label","label",false); - //label.setInstanceName("ListBox_render_label"); - if(listBox->getLeftControlled()==true){ - label.init(listBox->getX()+listBox->getButton1()->getW()+listBox->getButton2()->getW()+2, listBox->getY(), listBox->getW(), listBox->getH(), false,listBox->getTextColor()); - } - else { - label.init(listBox->getX(), listBox->getY(), listBox->getW(), listBox->getH(), true,listBox->getTextColor()); - } - label.setText(listBox->getText()); - label.setTextNativeTranslation(listBox->getTextNativeTranslation()); - label.setFont(listBox->getFont()); - label.setFont3D(listBox->getFont3D()); - renderLabel(&label); - - - //lighting - - bool renderLighted= (listBox->getLighted()); - - - if(renderLighted) { - float anim= GraphicComponent::getAnim(); - if(anim>0.5f) anim= 1.f-anim; - - Vec3f color=listBox->getTextColor(); - int x= listBox->getX()+listBox->getButton1()->getW(); - int y= listBox->getY(); - int h= listBox->getH(); - int w= listBox->getW()-listBox->getButton1()->getW()-listBox->getButton2()->getW(); - - const int lightSize= 0; - const Vec4f color1= Vec4f(color.x, color.y, color.z, 0.1f+anim*0.5f); - const Vec4f color2= Vec4f(color.x, color.y, color.z, 0.3f+anim); - - glBegin(GL_TRIANGLE_FAN); - - glColor4fv(color2.ptr()); - glVertex2f(x+w/2, y+h/2); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y-lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x+w+lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y+h+lightSize); - - glColor4fv(color1.ptr()); - glVertex2f(x-lightSize, y-lightSize); - - glEnd(); - } - - glPopAttrib(); -} - -void Renderer::renderMessageBox(GraphicMessageBox *messageBox) { - const int headerHeight=25; - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - try { - if(messageBox->getVisible() == false) { - return; - } - - if((renderText3DEnabled == false && messageBox->getFont() == NULL) || - (renderText3DEnabled == true && messageBox->getFont3D() == NULL)) { - messageBox->setFont(CoreData::getInstance().getMenuFontNormal()); - messageBox->setFont3D(CoreData::getInstance().getMenuFontNormal3D()); - } - - string wrappedText = messageBox->getText(); - if(messageBox->getAutoWordWrap() == true) { - if(renderText3DEnabled == false) { - wrappedText = messageBox->getFont()->getMetrics()->wordWrapText(wrappedText,messageBox->getW() * 0.90); - } - else { - wrappedText = messageBox->getFont3D()->getMetrics()->wordWrapText(wrappedText,messageBox->getW() * 0.90); - } - } - - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(0.0f, 0.0f, 0.0f, 0.8f) ; - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(messageBox->getX(), messageBox->getY()+messageBox->getH()-headerHeight); - glVertex2i(messageBox->getX(), messageBox->getY()); - glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH()-headerHeight); - glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY()); - glEnd(); - - glColor4f(0.0f, 0.0f, 0.0f, 0.8f) ; - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(messageBox->getX(), messageBox->getY()+messageBox->getH()); - glVertex2i(messageBox->getX(), messageBox->getY()+messageBox->getH()-headerHeight); - glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH()); - glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY()+messageBox->getH()-headerHeight); - glEnd(); - - glBegin(GL_LINE_LOOP); - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(messageBox->getX(), messageBox->getY()); - - glColor4f(0.0f, 0.0f, 0.0f, 0.25f) ; - glVertex2i(messageBox->getX()+ messageBox->getW(), messageBox->getY()); - - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(messageBox->getX()+ messageBox->getW(), messageBox->getY() + messageBox->getH()); - - glColor4f(0.25f, 0.25f, 0.25f, 0.25f) ; - glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH()); - glEnd(); - - glBegin(GL_LINE_STRIP); - glColor4f(1.0f, 1.0f, 1.0f, 0.25f) ; - glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH()-headerHeight); - - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(messageBox->getX()+ messageBox->getW(), messageBox->getY() + messageBox->getH()-headerHeight); - glEnd(); - - glPopAttrib(); - - - //buttons - for(int i=0; igetButtonCount();i++){ - - if((renderText3DEnabled == false && messageBox->getButton(i)->getFont() == NULL) || - (renderText3DEnabled == true && messageBox->getButton(i)->getFont3D() == NULL)) { - messageBox->getButton(i)->setFont(CoreData::getInstance().getMenuFontNormal()); - messageBox->getButton(i)->setFont3D(CoreData::getInstance().getMenuFontNormal3D()); - } - - renderButton(messageBox->getButton(i)); - } - - Vec4f fontColor; - //if(game!=NULL){ - // fontColor=game->getGui()->getDisplay()->getColor(); - //} - //else { - // white shadowed is default ( in the menu for example ) - fontColor=Vec4f(1.f, 1.f, 1.f, 1.0f); - //} - - if(renderText3DEnabled == true) { - //text - renderTextShadow3D( - wrappedText, messageBox->getFont3D(), fontColor, - messageBox->getX()+15, messageBox->getY()+messageBox->getH()-headerHeight*2, - false ); - - renderTextShadow3D( - messageBox->getHeader(), messageBox->getFont3D(),fontColor, - messageBox->getX()+15, messageBox->getY()+messageBox->getH()-headerHeight+8, - false ); - - } - else { - //text - renderTextShadow( - wrappedText, messageBox->getFont(), fontColor, - messageBox->getX()+15, messageBox->getY()+messageBox->getH()-headerHeight*2, - false ); - - renderTextShadow( - messageBox->getHeader(), messageBox->getFont(),fontColor, - messageBox->getX()+15, messageBox->getY()+messageBox->getH()-headerHeight+8, - false ); - } - } - catch(const exception &e) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - - throw megaglest_runtime_error(szBuf); - } -} - -// ==================== complex rendering ==================== - -//VisibleQuadContainerVBOCache * Renderer::GetSurfaceVBOs(SurfaceData *cellData) { -// std::map::iterator iterFind = mapSurfaceVBOCache.find(cellData->uniqueId); -// if(iterFind == mapSurfaceVBOCache.end()) { -// Vec2f *texCoords = &cellData->texCoords[0]; -// Vec2f *texCoordsSurface = &cellData->texCoordsSurface[0]; -// Vec3f *vertices = &cellData->vertices[0]; -// Vec3f *normals = &cellData->normals[0]; -// -// VisibleQuadContainerVBOCache vboCache; -// -// // Generate And Bind The Vertex Buffer -// glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOVertices ); // Get A Valid Name -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOVertices ); // Bind The Buffer -// // Load The Data -// glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f) * cellData->bufferCount, vertices, GL_STATIC_DRAW_ARB ); -// glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); -// -// assertGl(); -// // Generate And Bind The Texture Coordinate Buffer -// glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOFowTexCoords ); // Get A Valid Name -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOFowTexCoords ); // Bind The Buffer -// // Load The Data -// glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f) * cellData->bufferCount, texCoords, GL_STATIC_DRAW_ARB ); -// glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); -// -// assertGl(); -// // Generate And Bind The Texture Coordinate Buffer -// glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOSurfaceTexCoords ); // Get A Valid Name -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOSurfaceTexCoords ); // Bind The Buffer -// // Load The Data -// glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f) * cellData->bufferCount, texCoordsSurface, GL_STATIC_DRAW_ARB ); -// glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); -// -// assertGl(); -// // Generate And Bind The Normal Buffer -// glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBONormals ); // Get A Valid Name -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBONormals ); // Bind The Buffer -// // Load The Data -// glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f) * cellData->bufferCount, normals, GL_STATIC_DRAW_ARB ); -// glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); -// -// vboCache.hasBuiltVBOs = true; -// -// mapSurfaceVBOCache[cellData->uniqueId] = vboCache; -// -// // don't need the data in computer RAM anymore its in the GPU now -// cellData->texCoords.clear(); -// cellData->texCoordsSurface.clear(); -// cellData->vertices.clear(); -// cellData->normals.clear(); -// } -// -// return &mapSurfaceVBOCache[cellData->uniqueId]; -//} - -void Renderer::ReleaseSurfaceVBOs() { - for(std::map::iterator iterFind = mapSurfaceVBOCache.begin(); - iterFind != mapSurfaceVBOCache.end(); ++iterFind) { - - VisibleQuadContainerVBOCache &item = iterFind->second; - if(item.hasBuiltVBOs == true) { - glDeleteBuffersARB( 1, (GLuint*)&item.m_nVBOVertices ); // Get A Valid Name - glDeleteBuffersARB( 1, (GLuint*)&item.m_nVBOFowTexCoords ); // Get A Valid Name - glDeleteBuffersARB( 1, (GLuint*)&item.m_nVBOSurfaceTexCoords ); // Get A Valid Name - glDeleteBuffersARB( 1, (GLuint*)&item.m_nVBONormals ); // Get A Valid Name - //glDeleteBuffersARB( 1, &item.m_nVBOIndexes ); // Get A Valid Name - } - } - - mapSurfaceVBOCache.clear(); -} - -Renderer::MapRenderer::Layer::~Layer() { - if(vbo_vertices) glDeleteBuffersARB(1,&vbo_vertices); - if(vbo_normals) glDeleteBuffersARB(1,&vbo_normals); - if(vbo_fowTexCoords) glDeleteBuffersARB(1,&vbo_fowTexCoords); - if(vbo_surfTexCoords) glDeleteBuffersARB(1,&vbo_surfTexCoords); - if(vbo_indices) glDeleteBuffersARB(1,&vbo_indices); - -} - -template void _loadVBO(GLuint &vbo,std::vector buf,int target=GL_ARRAY_BUFFER_ARB) { - assert(buf.size()); - if(true /* vbo enabled? */) { - glGenBuffersARB(1,&vbo); - assert(vbo); - glBindBufferARB(target,vbo); - glBufferDataARB(target,sizeof(T)*buf.size(),&buf[0],GL_STATIC_DRAW_ARB); - glBindBufferARB(target,0); - assertGl(); - buf.clear(); - } -} - -void Renderer::MapRenderer::Layer::load_vbos(bool vboEnabled) { - indexCount = (int)indices.size(); - if(vboEnabled) { - _loadVBO(vbo_vertices,vertices); - _loadVBO(vbo_normals,normals); - _loadVBO(vbo_fowTexCoords,fowTexCoords); - _loadVBO(vbo_surfTexCoords,surfTexCoords); - - _loadVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB); - } - else { - vbo_vertices = 0; - vbo_normals = 0; - vbo_fowTexCoords = 0; - vbo_surfTexCoords = 0; - vbo_indices = 0; - } -} - -void Renderer::MapRenderer::loadVisibleLayers(float coordStep,VisibleQuadContainerCache &qCache) { - int totalCellCount = 0; - // we create a layer for each visible texture in the map - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - - totalCellCount++; - - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); - SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); - SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); - - const Vec2f &surfCoord= tc00->getSurfTexCoord(); - - SurfaceCell *tc[4] = { - tc00, - tc10, - tc01, - tc11 + enum PROJECTION_TO_INFINITY { + pti_D_IS_ZERO, + pti_N_OVER_D_IS_OUTSIDE }; - int textureHandle = static_cast(tc[0]->getSurfaceTexture())->getHandle(); - string texturePath = static_cast(tc[0]->getSurfaceTexture())->getPath(); - //int32 textureCRC = CalculatePixelsCRC(static_cast(tc[0]->getSurfaceTexture())); - Layer* layer = NULL; - for(Layers::iterator it= layers.begin(); it!= layers.end(); ++it) { - if((*it)->textureHandle == textureHandle) { - //if((*it)->texturePath == texturePath) { - //if((*it)->textureCRC == textureCRC) { - layer = *it; - break; - } - } - if(!layer) { - layer = new Layer(textureHandle); - layer->texturePath = texturePath; - //layer->textureCRC = textureCRC; - layers.push_back(layer); - //printf("Ading new unique texture [%s]\n",texturePath.c_str()); - } - // we'll be super-lazy and re-emit all four corners just because its easier - int index[4]; - int loopIndexes[4] = { 2,0,3,1 }; - for(int i=0; i < 4; i++) { - index[i] = (int)layer->vertices.size(); - SurfaceCell *corner = tc[loopIndexes[i]]; + // ===================================================== + // class MeshCallbackTeamColor + // ===================================================== - layer->vertices.push_back(corner->getVertex()); - layer->normals.push_back(corner->getNormal()); - layer->fowTexCoords.push_back(corner->getFowTexCoord()); - } + bool MeshCallbackTeamColor::noTeamColors = false; - layer->surfTexCoords.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); - layer->surfTexCoords.push_back(Vec2f(surfCoord.x, surfCoord.y)); - layer->surfTexCoords.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep)); - layer->surfTexCoords.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y)); + void MeshCallbackTeamColor::execute(const Mesh *mesh) { + //team color + if (mesh->getCustomTexture() && teamTexture != NULL && + MeshCallbackTeamColor::noTeamColors == false) { + //texture 0 + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - // and make two triangles (no strip, we may be disjoint) - layer->indices.push_back(index[0]); - layer->indices.push_back(index[1]); - layer->indices.push_back(index[2]); - layer->indices.push_back(index[1]); - layer->indices.push_back(index[3]); - layer->indices.push_back(index[2]); + //set color to interpolation + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); - } - // turn them into vbos (actually this method will just calc the index count) - for(Layers::iterator layer= layers.begin(); layer!= layers.end(); ++layer){ - (*layer)->load_vbos(false); - } + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - //printf("Total # of layers for this map = %d totalCellCount = %d overall render reduction ratio = %d times\n",layers.size(),totalCellCount,(totalCellCount / layers.size())); -} + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); -void Renderer::MapRenderer::load(float coordStep) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); - int totalCellCount = 0; - // we create a layer for each texture in the map - for(int y=0; ygetSurfaceH()-1; y++) { - for(int x=0; xgetSurfaceW()-1; x++) { - totalCellCount++; + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - SurfaceCell *tc[4] = { - map->getSurfaceCell(x,y), - map->getSurfaceCell(x+1,y), - map->getSurfaceCell(x,y+1), - map->getSurfaceCell(x+1,y+1) - }; - int textureHandle = static_cast(tc[0]->getSurfaceTexture())->getHandle(); - string texturePath = static_cast(tc[0]->getSurfaceTexture())->getPath(); - //int32 textureCRC = CalculatePixelsCRC(static_cast(tc[0]->getSurfaceTexture())); - Layer* layer = NULL; - for(Layers::iterator it= layers.begin(); it!= layers.end(); ++it) { - if((*it)->textureHandle == textureHandle) { - //if((*it)->texturePath == texturePath) { - //if((*it)->textureCRC == textureCRC) { - layer = *it; - break; - } - } - if(!layer) { - layer = new Layer(textureHandle); - layer->texturePath = texturePath; - //layer->textureCRC = textureCRC; - layers.push_back(layer); - - //printf("Ading new unique texture [%s]\n",texturePath.c_str()); - } - // we'll be super-lazy and re-emit all four corners just because its easier - int index[4]; - int loopIndexes[4] = { 2,0,3,1 }; - for(int i=0; i < 4; i++) { - index[i] = (int)layer->vertices.size(); - SurfaceCell *corner = tc[loopIndexes[i]]; - layer->vertices.push_back(corner->getVertex()); - layer->normals.push_back(corner->getNormal()); - } - - // the texture coords are all on the current texture obviously - layer->fowTexCoords.push_back(tc[loopIndexes[0]]->getFowTexCoord()); - layer->fowTexCoords.push_back(tc[loopIndexes[1]]->getFowTexCoord()); - layer->fowTexCoords.push_back(tc[loopIndexes[2]]->getFowTexCoord()); - layer->fowTexCoords.push_back(tc[loopIndexes[3]]->getFowTexCoord()); - - layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord()+Vec2f(0,coordStep)); - layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord()+Vec2f(0,0)); - layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord()+Vec2f(coordStep,coordStep)); - layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord()+Vec2f(coordStep,0)); - - layer->cellToIndicesMap[Vec2i(x,y)] = (int)layer->indices.size(); - - // and make two triangles (no strip, we may be disjoint) - layer->indices.push_back(index[0]); - layer->indices.push_back(index[1]); - layer->indices.push_back(index[2]); - layer->indices.push_back(index[1]); - layer->indices.push_back(index[3]); - layer->indices.push_back(index[2]); - } - } - // turn them into vbos - for(Layers::iterator layer= layers.begin(); layer!= layers.end(); ++layer){ - (*layer)->load_vbos(true); - } - - //printf("Total # of layers for this map = %d totalCellCount = %d overall render reduction ratio = %d times\n",layers.size(),totalCellCount,(totalCellCount / layers.size())); -} - -template void* _bindVBO(GLuint vbo,std::vector &buf,int target=GL_ARRAY_BUFFER_ARB) { - void* result = NULL; - if(vbo) { - glBindBuffer(target,vbo); - } - else { - result = &buf[0]; - } - return result; -} - -void Renderer::MapRenderer::Layer::renderVisibleLayer() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); - glClientActiveTexture(Renderer::fowTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0,&fowTexCoords[0]); - - glBindTexture(GL_TEXTURE_2D, textureHandle); - glClientActiveTexture(Renderer::baseTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, &surfTexCoords[0]); - - glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); - glNormalPointer(GL_FLOAT, 0, &normals[0]); - - //glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()); - //unsigned short faceIndices[4] = {0, 1, 2, 3}; - //glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, &faceIndices[0]); - glDrawElements(GL_TRIANGLES,indexCount,GL_UNSIGNED_INT,&indices[0]); - - glClientActiveTexture(Renderer::fowTexUnit); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(Renderer::baseTexUnit); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); -} - -void Renderer::MapRenderer::Layer::render(VisibleQuadContainerCache &qCache) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - -// const bool renderOnlyVisibleQuad = true; -// -// if(renderOnlyVisibleQuad == true) { - vector > rowsToRender; - - if(rowsToRenderCache.find(qCache.lastVisibleQuad) != rowsToRenderCache.end()) { - rowsToRender = rowsToRenderCache[qCache.lastVisibleQuad]; - } - else { - int startIndex = -1; - int lastValidIndex = -1; - - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - - if(cellToIndicesMap.find(pos) != cellToIndicesMap.end()) { - //printf("Layer Render, visibleindex = %d pos [%s] cellToIndicesMap[pos] = %d lastValidIndex = %d\n",visibleIndex,pos.getString().c_str(),cellToIndicesMap[pos],lastValidIndex); - - if(startIndex < 0 || cellToIndicesMap[pos] == lastValidIndex + 6) { - lastValidIndex = cellToIndicesMap[pos]; - if(startIndex < 0) { - startIndex = lastValidIndex; - } - } - else if(startIndex >= 0) { - rowsToRender.push_back(make_pair(startIndex,lastValidIndex)); - - lastValidIndex = cellToIndicesMap[pos]; - startIndex = lastValidIndex; - } - } - } - if(startIndex >= 0) { - rowsToRender.push_back(make_pair(startIndex,lastValidIndex)); - } - - rowsToRenderCache[qCache.lastVisibleQuad] = rowsToRender; - } - - if(rowsToRender.empty() == false) { - //printf("Layer has %d rows in visible quad, visible quad has %d cells\n",rowsToRender.size(),qCache.visibleScaledCellList.size()); - - glVertexPointer(3,GL_FLOAT,0,_bindVBO(vbo_vertices,vertices)); - glNormalPointer(GL_FLOAT,0,_bindVBO(vbo_normals,normals)); - - glClientActiveTexture(Renderer::fowTexUnit); - glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_fowTexCoords,fowTexCoords)); - - glClientActiveTexture(Renderer::baseTexUnit); - glBindTexture(GL_TEXTURE_2D,textureHandle); - glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_surfTexCoords,surfTexCoords)); - - for(unsigned int i = 0; i < rowsToRender.size(); ++i) { - //glDrawElements(GL_TRIANGLES,indexCount,GL_UNSIGNED_INT,_bindVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB)); - glDrawRangeElements(GL_TRIANGLES,rowsToRender[i].first,rowsToRender[i].second,indexCount,GL_UNSIGNED_INT,_bindVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB)); - } - } -// } -// else { -// glVertexPointer(3,GL_FLOAT,0,_bindVBO(vbo_vertices,vertices)); -// glNormalPointer(GL_FLOAT,0,_bindVBO(vbo_normals,normals)); -// -// glClientActiveTexture(Renderer::fowTexUnit); -// glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_fowTexCoords,fowTexCoords)); -// -// glClientActiveTexture(Renderer::baseTexUnit); -// glBindTexture(GL_TEXTURE_2D,textureHandle); -// glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_surfTexCoords,surfTexCoords)); -// -// glDrawElements(GL_TRIANGLES,indexCount,GL_UNSIGNED_INT,_bindVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB)); -// } -} - -void Renderer::MapRenderer::renderVisibleLayers(const Map* map,float coordStep,VisibleQuadContainerCache &qCache) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(map != this->map) { - //printf("New Map loading\n"); - destroy(); // clear any previous map data - this->map = map; - loadVisibleLayers(coordStep,qCache); - } - else if(lastVisibleQuad != qCache.lastVisibleQuad) { - //printf("New Visible Quad loading\n"); - destroy(); // clear any previous map data - this->map = map; - loadVisibleLayers(coordStep,qCache); - } - - lastVisibleQuad = qCache.lastVisibleQuad; - //printf("About to render %d layers\n",layers.size()); - - glClientActiveTexture(fowTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(baseTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - for(Layers::iterator layer= layers.begin(); layer!= layers.end(); ++layer) - (*layer)->renderVisibleLayer(); - glDisableClientState(GL_VERTEX_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER_ARB,0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,0); - glDisableClientState(GL_NORMAL_ARRAY); - glClientActiveTexture(fowTexUnit); - glBindTexture(GL_TEXTURE_2D,0); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(baseTexUnit); - glBindTexture(GL_TEXTURE_2D,0); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - assertGl(); -} - -void Renderer::MapRenderer::render(const Map* map,float coordStep,VisibleQuadContainerCache &qCache) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(map != this->map) { - destroy(); // clear any previous map data - this->map = map; - load(coordStep); - } - - //printf("About to render %d layers\n",layers.size()); - - glClientActiveTexture(fowTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(baseTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - for(Layers::iterator layer= layers.begin(); layer!= layers.end(); ++layer) - (*layer)->render(qCache); - glDisableClientState(GL_VERTEX_ARRAY); - glBindBuffer(GL_ARRAY_BUFFER_ARB,0); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB,0); - glDisableClientState(GL_NORMAL_ARRAY); - glClientActiveTexture(fowTexUnit); - glBindTexture(GL_TEXTURE_2D,0); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(baseTexUnit); - glBindTexture(GL_TEXTURE_2D,0); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - assertGl(); -} - -void Renderer::MapRenderer::destroy() { - while(layers.empty() == false) { - delete layers.back(); - layers.pop_back(); - } - map = NULL; -} - -void Renderer::renderSurface(const int renderFps) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - IF_DEBUG_EDITION( - if (getDebugRenderer().willRenderSurface()) { - getDebugRenderer().renderSurface(visibleQuad / Map::cellScale); - } else { - ) - assertGl(); - - const World *world= game->getWorld(); - const Map *map= world->getMap(); - float coordStep= world->getTileset()->getSurfaceAtlas()->getCoordStep(); - - const Texture2D *fowTex= world->getMinimap()->getFowTexture(); - if(fowTex == NULL) { - return; - } - - glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_TEXTURE_BIT); - - glEnable(GL_BLEND); - glEnable(GL_COLOR_MATERIAL); - glDisable(GL_ALPHA_TEST); - glEnable(GL_CULL_FACE); - - //fog of war tex unit - glActiveTexture(fowTexUnit); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); - - glTexSubImage2D( - GL_TEXTURE_2D, 0, 0, 0, - fowTex->getPixmapConst()->getW(), fowTex->getPixmapConst()->getH(), - GL_ALPHA, GL_UNSIGNED_BYTE, fowTex->getPixmapConst()->getPixels()); - - if(shadowsOffDueToMinRender == false) { - //shadow texture - if(shadows == sProjected || shadows == sShadowMapping) { - glActiveTexture(shadowTexUnit); - glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - - static_cast(modelRenderer)->setDuplicateTexCoords(true); - enableProjectiveTexturing(); - } - } - - const Rect2i mapBounds(0, 0, map->getSurfaceW()-1, map->getSurfaceH()-1); - - glActiveTexture(baseTexUnit); - - VisibleQuadContainerCache &qCache = getQuadCache(); - - bool useVBORendering = getVBOSupported(); - if(useVBORendering == true) { - VisibleQuadContainerCache &qCache = getQuadCache(); - //mapRenderer.render(map,coordStep,qCache); - mapRenderer.renderVisibleLayers(map,coordStep,qCache); - } - else if(qCache.visibleScaledCellList.empty() == false) { - - int lastTex=-1; - //int currTex=-1; - - //Quad2i snapshotOfvisibleQuad = visibleQuad; - - //bool useVertexArrayRendering = getVBOSupported(); - //bool useVertexArrayRendering = false; - //if(useVertexArrayRendering == false) { - //printf("\LEGACY qCache.visibleScaledCellList.size() = %d \n",qCache.visibleScaledCellList.size()); - - Vec2f texCoords[4]; - Vec2f texCoordsSurface[4]; - Vec3f vertices[4]; - Vec3f normals[4]; - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - - std::map uniqueVisibleTextures; - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - int cellTex= static_cast(tc00->getSurfaceTexture())->getHandle(); - - uniqueVisibleTextures[cellTex]++; - } - - //printf("Current renders = %d possible = %d\n",qCache.visibleScaledCellList.size(),uniqueVisibleTextures.size()); - - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { - Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; - - SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); - SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); - SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); - SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); - - if(tc00 == NULL) { - throw megaglest_runtime_error("tc00 == NULL"); - } - if(tc10 == NULL) { - throw megaglest_runtime_error("tc10 == NULL"); - } - if(tc01 == NULL) { - throw megaglest_runtime_error("tc01 == NULL"); - } - if(tc11 == NULL) { - throw megaglest_runtime_error("tc11 == NULL"); - } - - triangleCount+= 2; - pointCount+= 4; - - //set texture - if(tc00->getSurfaceTexture() == NULL) { - throw megaglest_runtime_error("tc00->getSurfaceTexture() == NULL"); - } - int currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); - if(currTex != lastTex) { - lastTex = currTex; - //glBindTexture(GL_TEXTURE_2D, lastTex); - } - - const Vec2f &surfCoord= tc00->getSurfTexCoord(); - - texCoords[0] = tc01->getFowTexCoord(); - texCoordsSurface[0] = Vec2f(surfCoord.x, surfCoord.y + coordStep); - vertices[0] = tc01->getVertex(); - normals[0] = tc01->getNormal();; - - texCoords[1] = tc00->getFowTexCoord(); - texCoordsSurface[1] = Vec2f(surfCoord.x, surfCoord.y); - vertices[1] = tc00->getVertex(); - normals[1] = tc00->getNormal(); - - texCoords[2] = tc11->getFowTexCoord(); - texCoordsSurface[2] = Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep); - vertices[2] = tc11->getVertex(); - normals[2] = tc11->getNormal(); - - texCoords[3] = tc10->getFowTexCoord(); - texCoordsSurface[3] = Vec2f(surfCoord.x+coordStep, surfCoord.y); - vertices[3] = tc10->getVertex(); - normals[3] = tc10->getNormal(); - - //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); - glClientActiveTexture(fowTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0,&texCoords[0]); - - glBindTexture(GL_TEXTURE_2D, lastTex); - glClientActiveTexture(baseTexUnit); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glTexCoordPointer(2, GL_FLOAT, 0, &texCoordsSurface[0]); - - glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); - glNormalPointer(GL_FLOAT, 0, &normals[0]); - - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - //unsigned short faceIndices[4] = {0, 1, 2, 3}; - //glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, &faceIndices[0]); - - glClientActiveTexture(fowTexUnit); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glClientActiveTexture(baseTexUnit); - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - -/* - glBegin(GL_TRIANGLE_STRIP); - - //draw quad using immediate mode - glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); - glNormal3fv(tc01->getNormal().ptr()); - glVertex3fv(tc01->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); - glNormal3fv(tc00->getNormal().ptr()); - glVertex3fv(tc00->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); - glNormal3fv(tc11->getNormal().ptr()); - glVertex3fv(tc11->getVertex().ptr()); - - glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); - glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); - glNormal3fv(tc10->getNormal().ptr()); - glVertex3fv(tc10->getVertex().ptr()); - - glEnd(); -*/ - } - - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - -// } -// else { -// const bool useVBOs = false; -// const bool useSurfaceCache = false; -// -// std::vector surfaceData; -// bool recalcSurface = false; -// -// if(useSurfaceCache == true) { -// std::map > >::iterator iterFind = mapSurfaceData.find(snapshotOfvisibleQuad.getString()); -// if(iterFind == mapSurfaceData.end()) { -// recalcSurface = true; -// //printf("#1 Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); -// } -///* -// else if(iterFind->second.first.getMillis() >= 250) { -// recalcSurface = true; -// mapSurfaceData.erase(snapshotOfvisibleQuad.getString()); -// //printf("#2 RE-Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); -// } -//*/ -// } -// else { -// recalcSurface = true; -// } -// -// if(recalcSurface == true) { -// //printf("Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); -// -// std::vector *surface = &surfaceData; -// if(useSurfaceCache == true) { -// std::pair > &surfaceCacheEntity = mapSurfaceData[snapshotOfvisibleQuad.getString()]; -// surface = &surfaceCacheEntity.second; -// //surface.reserve(qCache.visibleScaledCellList.size()); -// } -// surface->reserve(qCache.visibleScaledCellList.size()); -// -// int lastSurfaceDataIndex = -1; -// for(int visibleIndex = 0; -// visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { -// Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; -// -// SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); -// SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); -// SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); -// SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); -// -// if(tc00 == NULL) { -// throw megaglest_runtime_error("tc00 == NULL"); -// } -// if(tc10 == NULL) { -// throw megaglest_runtime_error("tc10 == NULL"); -// } -// if(tc01 == NULL) { -// throw megaglest_runtime_error("tc01 == NULL"); -// } -// if(tc11 == NULL) { -// throw megaglest_runtime_error("tc11 == NULL"); -// } -// -// triangleCount+= 2; -// pointCount+= 4; -// -// //set texture -// if(tc00->getSurfaceTexture() == NULL) { -// throw megaglest_runtime_error("tc00->getSurfaceTexture() == NULL"); -// } -// -// int surfaceDataIndex = -1; -// currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); -// if(currTex != lastTex) { -// lastTex = currTex; -// } -// else { -// surfaceDataIndex = lastSurfaceDataIndex; -// } -// -// if(surfaceDataIndex < 0) { -// SurfaceData newData; -// newData.uniqueId = SurfaceData::nextUniqueId; -// SurfaceData::nextUniqueId++; -// newData.bufferCount=0; -// newData.textureHandle = currTex; -// surface->push_back(newData); -// -// surfaceDataIndex = (int)surface->size() - 1; -// } -// -// lastSurfaceDataIndex = surfaceDataIndex; -// -// SurfaceData *cellData = &(*surface)[surfaceDataIndex]; -// -// const Vec2f &surfCoord= tc00->getSurfTexCoord(); -// -// cellData->texCoords.push_back(tc01->getFowTexCoord()); -// cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); -// cellData->vertices.push_back(tc01->getVertex()); -// cellData->normals.push_back(tc01->getNormal()); -// cellData->bufferCount++; -// -// cellData->texCoords.push_back(tc00->getFowTexCoord()); -// cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y)); -// cellData->vertices.push_back(tc00->getVertex()); -// cellData->normals.push_back(tc00->getNormal()); -// cellData->bufferCount++; -// -// cellData->texCoords.push_back(tc11->getFowTexCoord()); -// cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep)); -// cellData->vertices.push_back(tc11->getVertex()); -// cellData->normals.push_back(tc11->getNormal()); -// cellData->bufferCount++; -// -// cellData->texCoords.push_back(tc10->getFowTexCoord()); -// cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y)); -// cellData->vertices.push_back(tc10->getVertex()); -// cellData->normals.push_back(tc10->getNormal()); -// cellData->bufferCount++; -// } -// } -// -// std::vector *surface = &surfaceData; -// if(useSurfaceCache == true) { -// std::pair > &surfaceCacheEntity = mapSurfaceData[snapshotOfvisibleQuad.getString()]; -// surface = &surfaceCacheEntity.second; -// } -// -// glEnableClientState(GL_VERTEX_ARRAY); -// glEnableClientState(GL_NORMAL_ARRAY); -// -// for(int i = 0; i < (int)surface->size(); ++i) { -// SurfaceData &data = (*surface)[i]; -// -// if(useVBOs == true) { -// VisibleQuadContainerVBOCache *vboCache = GetSurfaceVBOs(&data); -// -// //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); -// glClientActiveTexture(fowTexUnit); -// glEnableClientState(GL_TEXTURE_COORD_ARRAY); -// -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOFowTexCoords); -// glTexCoordPointer(2, GL_FLOAT, 0,(char *) NULL); -// -// glBindTexture(GL_TEXTURE_2D, data.textureHandle); -// glClientActiveTexture(baseTexUnit); -// glEnableClientState(GL_TEXTURE_COORD_ARRAY); -// -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOSurfaceTexCoords); -// glTexCoordPointer(2, GL_FLOAT, 0, (char *) NULL); -// -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOVertices); -// glVertexPointer(3, GL_FLOAT, 0, (char *) NULL); -// -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBONormals); -// glNormalPointer(GL_FLOAT, 0, (char *) NULL); -// -// glDrawArrays(GL_TRIANGLE_STRIP, 0, data.bufferCount); -// -// glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); -// -// glClientActiveTexture(fowTexUnit); -// glDisableClientState(GL_TEXTURE_COORD_ARRAY); -// glClientActiveTexture(baseTexUnit); -// glDisableClientState(GL_TEXTURE_COORD_ARRAY); -// -// } -// else { -// Vec2f *texCoords = &data.texCoords[0]; -// Vec2f *texCoordsSurface = &data.texCoordsSurface[0]; -// Vec3f *vertices = &data.vertices[0]; -// Vec3f *normals = &data.normals[0]; -// -// //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); -// glClientActiveTexture(fowTexUnit); -// glEnableClientState(GL_TEXTURE_COORD_ARRAY); -// glTexCoordPointer(2, GL_FLOAT, 0,texCoords); -// -// glBindTexture(GL_TEXTURE_2D, data.textureHandle); -// glClientActiveTexture(baseTexUnit); -// glEnableClientState(GL_TEXTURE_COORD_ARRAY); -// glTexCoordPointer(2, GL_FLOAT, 0, texCoordsSurface); -// -// glVertexPointer(3, GL_FLOAT, 0, vertices); -// glNormalPointer(GL_FLOAT, 0, normals); -// -// glDrawArrays(GL_TRIANGLE_STRIP, 0, data.bufferCount); -// -// glClientActiveTexture(fowTexUnit); -// glDisableClientState(GL_TEXTURE_COORD_ARRAY); -// glClientActiveTexture(baseTexUnit); -// glDisableClientState(GL_TEXTURE_COORD_ARRAY); -// } -// } -// -// glDisableClientState(GL_NORMAL_ARRAY); -// glDisableClientState(GL_VERTEX_ARRAY); -// -// //printf("Surface Render before [%d] after [%d]\n",qCache.visibleScaledCellList.size(),surface.size()); -// } - } - - //Restore - static_cast(modelRenderer)->setDuplicateTexCoords(false); - - glDisable(GL_TEXTURE_2D); - - glPopAttrib(); - - //assert - GLenum glresult = glGetError(); //remove when first mtex problem solved - if(glresult) { - assertGl(); - } - assertGl(); - - IF_DEBUG_EDITION( - } // end else, if not renderering debug textures instead of regular terrain - getDebugRenderer().renderEffects(visibleQuad / Map::cellScale); - ) -} - -void Renderer::renderObjects(const int renderFps) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const World *world= game->getWorld(); - //const Map *map= world->getMap(); - - Config &config= Config::getInstance(); - int tilesetObjectsToAnimate=config.getInt("AnimatedTilesetObjects","-1"); - - assertGl(); - - const Texture2D *fowTex = world->getMinimap()->getFowTexture(); - const Pixmap2D *fowTexPixmap = fowTex->getPixmapConst(); - Vec3f baseFogColor = world->getTileset()->getFogColor() * world->getTimeFlow()->computeLightColor(); - - bool modelRenderStarted = false; - - VisibleQuadContainerCache &qCache = getQuadCache(); - - // for(int visibleIndex = 0; - // visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { - // render from last to first object so animated objects which are on bottom of screen are - // rendered first which looks better for limited number of animated tileset objects - for(int visibleIndex = (int)qCache.visibleObjectList.size()-1; - visibleIndex >= 0 ; --visibleIndex) { - Object *o = qCache.visibleObjectList[visibleIndex]; - - Model *objModel= o->getModelPtr(); - //objModel->updateInterpolationData(o->getAnimProgress(), true); - const Vec3f v= o->getConstPos(); - - if(modelRenderStarted == false) { - modelRenderStarted = true; - - glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); - - if(shadowsOffDueToMinRender == false && - shadows == sShadowMapping) { - glActiveTexture(shadowTexUnit); + //texture 1 + glActiveTexture(GL_TEXTURE1); + glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(teamTexture)->getHandle()); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + glActiveTexture(GL_TEXTURE0); + } else { + glActiveTexture(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + } + } + + // =========================================================== + // class Renderer + // =========================================================== + + // ===================== PUBLIC ======================== + + const int Renderer::maxProgressBar = 100; + const Vec4f Renderer::progressBarBack1 = Vec4f(0.7f, 0.7f, 0.7f, 0.7f); + const Vec4f Renderer::progressBarBack2 = Vec4f(0.7f, 0.7f, 0.7f, 1.f); + const Vec4f Renderer::progressBarFront1 = Vec4f(0.f, 0.5f, 0.f, 1.f); + const Vec4f Renderer::progressBarFront2 = Vec4f(0.f, 0.1f, 0.f, 1.f); + + const float Renderer::sunDist = 10e6; + const float Renderer::moonDist = 10e6; + const float Renderer::lightAmbFactor = 0.4f; + + const int Renderer::maxMouse2dAnim = 100; + + const GLenum Renderer::baseTexUnit = GL_TEXTURE0; + const GLenum Renderer::fowTexUnit = GL_TEXTURE1; + const GLenum Renderer::shadowTexUnit = GL_TEXTURE2; + + const float Renderer::selectionCircleRadius = 0.7f; + const float Renderer::magicCircleRadius = 1.f; + + //perspective values + const float Renderer::perspFov = 60.f; + const float Renderer::perspNearPlane = 1.f; + //const float Renderer::perspFarPlane= 50.f; + float Renderer::perspFarPlane = 1000000.f; + + const float Renderer::ambFactor = 0.7f; + const Vec4f Renderer::fowColor = Vec4f(0.0f, 0.0f, 0.0f, 1.0f); + const Vec4f Renderer::defSpecularColor = Vec4f(0.8f, 0.8f, 0.8f, 1.f); + const Vec4f Renderer::defDiffuseColor = Vec4f(1.f, 1.f, 1.f, 1.f); + const Vec4f Renderer::defAmbientColor = Vec4f(1.f * ambFactor, 1.f * ambFactor, 1.f * ambFactor, 1.f); + const Vec4f Renderer::defColor = Vec4f(1.f, 1.f, 1.f, 1.f); + + //const float Renderer::maxLightDist= 100.f; + const float Renderer::maxLightDist = 100.f; + + bool Renderer::rendererEnded = true; + + const int MIN_FPS_NORMAL_RENDERING = 15; + const int MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD = 25; + + const int OBJECT_SELECT_OFFSET = 100000000; + + bool VisibleQuadContainerCache::enableFrustumCalcs = true; + + // ==================== constructor and destructor ==================== + + Renderer::Renderer() : BaseRenderer(), saveScreenShotThreadAccessor(new Mutex(CODE_AT_LINE)) { + //this->masterserverMode = masterserverMode; + //printf("this->masterserverMode = %d\n",this->masterserverMode); + //assert(0==1); + + Renderer::rendererEnded = false; + shadowIntensity = 0; + shadowFrameSkip = 0; + triangleCount = 0; + smoothedRenderFps = 0; + shadowTextureSize = 0; + shadows = sDisabled; + shadowMapFrame = 0; + textures3D = false; + photoMode = false; + focusArrows = false; + pointCount = 0; + maxLights = 0; + waterAnim = 0; + + this->allowRenderUnitTitles = false; + this->menu = NULL; + this->game = NULL; + this->gameCamera = NULL; + showDebugUI = false; + showDebugUILevel = debugui_fps; + modelRenderer = NULL; + textRenderer = NULL; + textRenderer3D = NULL; + particleRenderer = NULL; + saveScreenShotThread = NULL; + mapSurfaceData.clear(); + visibleFrameUnitList.clear(); + visibleFrameUnitListCameraKey = ""; + + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + + lastRenderFps = MIN_FPS_NORMAL_RENDERING; + shadowsOffDueToMinRender = false; + shadowMapHandle = 0; + shadowMapHandleValid = false; + + //list3d=0; + //list3dValid=false; + //list2d=0; + //list2dValid=false; + //list3dMenu=0; + //list3dMenuValid=false; + //customlist3dMenu=NULL; + //this->mm3d = NULL; + this->custom_mm3d = NULL; + + this->program = NULL; + + //resources + for (int i = 0; i < rsCount; ++i) { + modelManager[i] = NULL; + textureManager[i] = NULL; + particleManager[i] = NULL; + fontManager[i] = NULL; + } + + Config &config = Config::getInstance(); + + Renderer::perspFarPlane = config.getFloat("PerspectiveFarPlane", floatToStr(Renderer::perspFarPlane).c_str()); + this->no2DMouseRendering = config.getBool("No2DMouseRendering", "false"); + this->maxConsoleLines = config.getInt("ConsoleMaxLines"); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] Renderer::perspFarPlane [%f] this->no2DMouseRendering [%d] this->maxConsoleLines [%d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, Renderer::perspFarPlane, this->no2DMouseRendering, this->maxConsoleLines); + + GraphicsInterface &gi = GraphicsInterface::getInstance(); + FactoryRepository &fr = FactoryRepository::getInstance(); + gi.setFactory(fr.getGraphicsFactory(config.getString("FactoryGraphics"))); + GraphicsFactory *graphicsFactory = GraphicsInterface::getInstance().getFactory(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + modelRenderer = graphicsFactory->newModelRenderer(); + textRenderer = graphicsFactory->newTextRenderer2D(); + textRenderer3D = graphicsFactory->newTextRenderer3D(); + particleRenderer = graphicsFactory->newParticleRenderer(); + } + + //resources + for (int i = 0; i < rsCount; ++i) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + modelManager[i] = graphicsFactory->newModelManager(); + textureManager[i] = graphicsFactory->newTextureManager(); + modelManager[i]->setTextureManager(textureManager[i]); + fontManager[i] = graphicsFactory->newFontManager(); + } + particleManager[i] = graphicsFactory->newParticleManager(); + } + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + saveScreenShotThread = new SimpleTaskThread(this, 0, 25); + saveScreenShotThread->setUniqueID(mutexOwnerId); + saveScreenShotThread->start(); + } + } + + void Renderer::cleanupScreenshotThread() { + if (saveScreenShotThread) { + saveScreenShotThread->signalQuit(); + // for(time_t elapsed = time(NULL); + // getSaveScreenQueueSize() > 0 && difftime((long int)time(NULL),elapsed) <= 7;) { + // sleep(0); + // } + // if(saveScreenShotThread->canShutdown(true) == true && + // saveScreenShotThread->shutdownAndWait() == true) { + // //printf("IN MenuStateCustomGame cleanup - C\n"); + // delete saveScreenShotThread; + // } + // saveScreenShotThread = NULL; + if (saveScreenShotThread->shutdownAndWait() == true) { + delete saveScreenShotThread; + } + saveScreenShotThread = NULL; + + + if (getSaveScreenQueueSize() > 0) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line %d] FORCING MEMORY CLEANUP and NOT SAVING screenshots, saveScreenQueue.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, saveScreenQueue.size()); + + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor, mutexOwnerId); + for (std::list >::iterator iter = saveScreenQueue.begin(); + iter != saveScreenQueue.end(); ++iter) { + delete iter->second; + } + saveScreenQueue.clear(); + } + } + } + + Renderer::~Renderer() { + try { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + delete modelRenderer; + modelRenderer = NULL; + delete textRenderer; + textRenderer = NULL; + delete textRenderer3D; + textRenderer3D = NULL; + delete particleRenderer; + particleRenderer = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //resources + for (int i = 0; i < rsCount; ++i) { + delete modelManager[i]; + modelManager[i] = NULL; + delete textureManager[i]; + textureManager[i] = NULL; + delete particleManager[i]; + particleManager[i] = NULL; + delete fontManager[i]; + fontManager[i] = NULL; + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + // Wait for the queue to become empty or timeout the thread at 7 seconds + cleanupScreenshotThread(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + mapSurfaceData.clear(); + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + this->menu = NULL; + this->game = NULL; + this->gameCamera = NULL; + + delete saveScreenShotThreadAccessor; + saveScreenShotThreadAccessor = NULL; + } catch (const exception &e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + throw megaglest_runtime_error(szBuf); + } + } + + void Renderer::simpleTask(BaseThread *callingThread, void *userdata) { + // This code reads pixmaps from a queue and saves them to disk + Pixmap2D *savePixMapBuffer = NULL; + string path = ""; + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor, mutexOwnerId); + if (saveScreenQueue.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line %d] saveScreenQueue.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, saveScreenQueue.size()); + + savePixMapBuffer = saveScreenQueue.front().second; + path = saveScreenQueue.front().first; + + saveScreenQueue.pop_front(); + } + safeMutex.ReleaseLock(); + + if (savePixMapBuffer != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line %d] about to save [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, path.c_str()); + + savePixMapBuffer->save(path); + delete savePixMapBuffer; + } + } + + bool Renderer::isEnded() { + return Renderer::rendererEnded; + } + + Renderer &Renderer::getInstance() { + static Renderer renderer; + return renderer; + } + + void Renderer::reinitAll() { + //resources + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + for (int i = 0; i < rsCount; ++i) { + //modelManager[i]->init(); + textureManager[i]->init(true); + //particleManager[i]->init(); + //fontManager[i]->init(); + } + } + // ==================== init ==================== + + void Renderer::init() { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + Config &config = Config::getInstance(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + loadConfig(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (config.getBool("CheckGlCaps")) { + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + checkGlCaps(); + } + + if (glActiveTexture == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error: glActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!"); + throw megaglest_runtime_error(szBuf); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (config.getBool("FirstTime")) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + config.setBool("FirstTime", false); + autoConfig(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + config.save(); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + modelManager[rsGlobal]->init(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + textureManager[rsGlobal]->init(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + fontManager[rsGlobal]->init(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + init2dList(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + glHint(GL_FOG_HINT, GL_FASTEST); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + //glHint(GL_GENERATE_MIPMAP_HINT, GL_FASTEST); + glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST); + //glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST); + + //glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST); + glHint(GL_TEXTURE_COMPRESSION_HINT, GL_FASTEST); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + } + + void Renderer::initGame(const Game *game, GameCamera *gameCamera) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + this->gameCamera = gameCamera; + VisibleQuadContainerCache::enableFrustumCalcs = Config::getInstance().getBool("EnableFrustrumCalcs", "true"); + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + + SurfaceData::nextUniqueId = 1; + mapSurfaceData.clear(); + this->game = game; + worldToScreenPosCache.clear(); + + //vars + shadowMapFrame = 0; + waterAnim = 0; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //check gl caps + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + checkGlOptionalCaps(); + + //shadows + if (shadows == sProjected || shadows == sShadowMapping) { + static_cast(modelRenderer)->setSecondaryTexCoordUnit(2); + Config &config = Config::getInstance(); + + glGenTextures(1, &shadowMapHandle); + shadowMapHandleValid = true; + + shadowIntensity = config.getFloat("ShadowIntensity", "1.0"); + if (game != NULL) { + shadowIntensity = shadowIntensity * game->getWorld()->getTileset()->getShadowIntense(); + if (shadowIntensity > 1.0f) { + shadowIntensity = 1.0f; + } + } + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - static_cast(modelRenderer)->setDuplicateTexCoords(true); - enableProjectiveTexturing(); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + if (shadows == sShadowMapping) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //shadow mapping + glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE); + //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); + //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FAIL_VALUE_ARB, 1.0f-shadowAlpha); + + glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, + shadowTextureSize, shadowTextureSize, + 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //projected + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, + shadowTextureSize, shadowTextureSize, + 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, NULL); + } + + shadowMapFrame = -1; } - glActiveTexture(baseTexUnit); - glEnable(GL_COLOR_MATERIAL); - glAlphaFunc(GL_GREATER, 0.5f); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - modelRenderer->begin(true, true, false, false); - } - //ambient and diffuse color is taken from cell color + IF_DEBUG_EDITION(getDebugRenderer().init(); ) - float fowFactor= fowTexPixmap->getPixelf(o->getMapPos().x / Map::cellScale, o->getMapPos().y / Map::cellScale); - Vec4f color= Vec4f(Vec3f(fowFactor), 1.f); - glColor4fv(color.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color * ambFactor).ptr()); - glFogfv(GL_FOG_COLOR, (baseFogColor * fowFactor).ptr()); + //texture init + modelManager[rsGame]->init(); + textureManager[rsGame]->init(); + fontManager[rsGame]->init(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glTranslatef(v.x, v.y, v.z); - glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - //We use OpenGL Lights so no manual action is needed here. In fact this call did bad things on lighting big rocks for example - // if(o->getRotation() != 0.0) { - // setupLightingForRotatedModel(); - // } - - //objModel->updateInterpolationData(0.f, true); - //if(this->gameCamera->getPos().dist(o->getPos()) <= SKIP_INTERPOLATION_DISTANCE) { - - - if (tilesetObjectsToAnimate == -1) { - objModel->updateInterpolationData(o->getAnimProgress(), true); - } else if (tilesetObjectsToAnimate > 0 && o->isAnimated()) { - tilesetObjectsToAnimate--; - objModel->updateInterpolationData(o->getAnimProgress(), true); - } else { - objModel->updateInterpolationData(0, true); + init3dList(); } -// objModel->updateInterpolationData(o->getAnimProgress(), true); + void Renderer::manageDeferredParticleSystems() { + + // if(deferredParticleSystems.empty() == false) { + // printf("deferredParticleSystems.size() = %d\n",(int)deferredParticleSystems.size()); + // } + + for (unsigned int i = 0; i < deferredParticleSystems.size(); ++i) { + std::pair &deferredParticleSystem = deferredParticleSystems[i]; + ParticleSystem *ps = deferredParticleSystem.first; + ResourceScope rs = deferredParticleSystem.second; + if (ps->getTextureFileLoadDeferred() != "" && ps->getTexture() == NULL) { + CoreData::TextureSystemType textureSystemId = + static_cast( + ps->getTextureFileLoadDeferredSystemId()); + + //printf("Load DEFERRED particle i = %d textureSystemId = %d\n",i,textureSystemId); + + if (textureSystemId != CoreData::tsyst_NONE) { + Texture2D *texture = CoreData::getInstance().getTextureBySystemId(textureSystemId); + //printf("Loading texture from system [%d] [%p]\n",textureSystemId,texture); + ps->setTexture(texture); + + //printf("#2 Load DEFERRED particle i = %d textureSystemId = %d, texture = %p\n",i,textureSystemId,texture); + } else { + Texture2D *texture = newTexture2D(rs); + if (texture) { + texture->setFormat(ps->getTextureFileLoadDeferredFormat()); + texture->getPixmap()->init(ps->getTextureFileLoadDeferredComponents()); + } + if (texture) { + string textureFile = ps->getTextureFileLoadDeferred(); + if (fileExists(textureFile) == false) { + textureFile = Config::findValidLocalFileFromPath(textureFile); + } + texture->load(textureFile); + ps->setTexture(texture); + } + //printf("#3 Load DEFERRED particle i = %d textureSystemId = %d, texture = %p\n",i,textureSystemId,texture); + } + } + if (dynamic_cast(ps) != NULL) { + GameParticleSystem *gps = dynamic_cast(ps); + if (gps != NULL && gps->getModelFileLoadDeferred() != "" && gps->getModel() == NULL) { + std::map > > loadedFileList; + Model *model = newModel(rsGame, gps->getModelFileLoadDeferred(), false, &loadedFileList, NULL); + if (model) + gps->setModel(model); + } + } + manageParticleSystem(ps, rs); + //printf("Managing ps [%p]\n",ps); + } + deferredParticleSystems.clear(); + //printf("After deferredParticleSystems.size() = %d\n",deferredParticleSystems.size()); + } + + void Renderer::initMenu(const MainMenu *mm) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + this->menu = mm; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + modelManager[rsMenu]->init(); + textureManager[rsMenu]->init(); + fontManager[rsMenu]->init(); + //modelRenderer->setCustomTexture(CoreData::getInstance().getCustomTexture()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //init3dListMenu(mm); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void Renderer::reset3d() { + assertGl(); + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); + //glCallList(list3d); + render3dSetup(); + + pointCount = 0; + triangleCount = 0; + assertGl(); + } + + void Renderer::reset2d() { + assertGl(); + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR); + //glCallList(list2d); + render2dMenuSetup(); + assertGl(); + } + + void Renderer::reset3dMenu() { + assertGl(); + glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR); + + //printf("In [%s::%s Line: %d] this->custom_mm3d [%p] this->mm3d [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->custom_mm3d,this->mm3d); + + if (this->custom_mm3d != NULL) { + render3dMenuSetup(this->custom_mm3d); + //glCallList(*this->customlist3dMenu); + } else { + render3dMenuSetup(this->menu); + //render3dMenuSetup(this->mm3d); + //glCallList(list3dMenu); + } + + assertGl(); + } + + // ==================== end ==================== + + void Renderer::end() { + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + + if (Renderer::rendererEnded == true) { + return; + } + std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); + crcFactionPreviewTextureCache.clear(); + + // Wait for the queue to become empty or timeout the thread at 7 seconds + cleanupScreenshotThread(); + + mapSurfaceData.clear(); + + //delete resources + if (modelManager[rsGlobal]) { + modelManager[rsGlobal]->end(); + } + if (textureManager[rsGlobal]) { + textureManager[rsGlobal]->end(); + } + if (fontManager[rsGlobal]) { + fontManager[rsGlobal]->end(); + } + if (particleManager[rsGlobal]) { + particleManager[rsGlobal]->end(); + } + + //delete 2d list + //if(list2dValid == true) { + // glDeleteLists(list2d, 1); + // list2dValid=false; + //} + + Renderer::rendererEnded = true; + } + + void Renderer::endScenario() { + this->game = NULL; + this->gameCamera = NULL; + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //delete resources + //modelManager[rsGame]->end(); + //textureManager[rsGame]->end(); + //fontManager[rsGame]->end(); + //particleManager[rsGame]->end(); + + if (shadowMapHandleValid == true && + (shadows == sProjected || shadows == sShadowMapping)) { + glDeleteTextures(1, &shadowMapHandle); + shadowMapHandleValid = false; + } + + //if(list3dValid == true) { + // glDeleteLists(list3d, 1); + // list3dValid=false; + //} + + worldToScreenPosCache.clear(); + ReleaseSurfaceVBOs(); + mapSurfaceData.clear(); + } + + void Renderer::endGame(bool isFinalEnd) { + this->game = NULL; + this->gameCamera = NULL; + Config &config = Config::getInstance(); + + try { + quadCache = VisibleQuadContainerCache(); + quadCache.clearFrustumData(); + } catch (const exception &e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + abort(); + } + + if (isFinalEnd) { + //delete resources + if (modelManager[rsGame] != NULL) { + modelManager[rsGame]->end(); + } + if (textureManager[rsGame] != NULL) { + textureManager[rsGame]->end(); + } + if (fontManager[rsGame] != NULL) { + fontManager[rsGame]->end(); + } + if (particleManager[rsGame] != NULL) { + particleManager[rsGame]->end(); + } + } + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (shadowMapHandleValid == true && + (shadows == sProjected || shadows == sShadowMapping)) { + glDeleteTextures(1, &shadowMapHandle); + shadowMapHandleValid = false; + } + shadowIntensity = config.getFloat("ShadowIntensity", "1.0"); + + //if(list3dValid == true) { + // glDeleteLists(list3d, 1); + // list3dValid=false; + //} + + worldToScreenPosCache.clear(); + ReleaseSurfaceVBOs(); + mapSurfaceData.clear(); + } + + void Renderer::endMenu() { + this->menu = NULL; + + //delete resources + if (modelManager[rsMenu]) { + modelManager[rsMenu]->end(); + } + if (textureManager[rsMenu]) { + textureManager[rsMenu]->end(); + } + if (fontManager[rsMenu]) { + fontManager[rsMenu]->end(); + } + if (particleManager[rsMenu]) { + particleManager[rsMenu]->end(); + } + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //if(this->customlist3dMenu != NULL) { + // glDeleteLists(*this->customlist3dMenu,1); + //} + //else { + // glDeleteLists(list3dMenu, 1); + //} + } + + //void Renderer::reloadResources() { + // if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + // return; + // } + // + // for(int i=0; iend(); + // textureManager[i]->end(); + // fontManager[i]->end(); + // } + // + // for(int i=0; iinit(); + // textureManager[i]->init(); + // fontManager[i]->init(); + // } //} - modelRenderer->render(objModel); - triangleCount+= objModel->getTriangleCount(); - pointCount+= objModel->getVertexCount(); + // ==================== engine interface ==================== - glPopMatrix(); - } + void Renderer::initTexture(ResourceScope rs, Texture *texture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); - - assertGl(); -} - -void Renderer::renderWater() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const World *world= game->getWorld(); - const Map *map= world->getMap(); - - const Texture2D *fowTex= world->getMinimap()->getFowTexture(); - if(fowTex == NULL) { - return; - } - - float waterAnim= world->getWaterEffects()->getAmin(); - - //assert - assertGl(); - - glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT); - - //water texture nit - glDisable(GL_TEXTURE_2D); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - if(textures3D) { - Texture3D *waterTex= world->getTileset()->getWaterTex(); - if(waterTex == NULL) { - throw megaglest_runtime_error("waterTex == NULL"); + textureManager[rs]->initTexture(texture); } - glEnable(GL_TEXTURE_3D); - glBindTexture(GL_TEXTURE_3D, static_cast(waterTex)->getHandle()); - } - else{ - glEnable(GL_COLOR_MATERIAL); - glColor4f(0.5f, 0.5f, 1.0f, 0.5f); - glBindTexture(GL_TEXTURE_3D, 0); - } - assertGl(); + void Renderer::endTexture(ResourceScope rs, Texture *texture, bool mustExistInList) { + string textureFilename = texture->getPath(); - //fog of War texture Unit - //const Texture2D *fowTex= world->getMinimap()->getFowTexture(); - glActiveTexture(fowTexUnit); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); - glActiveTexture(baseTexUnit); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] free texture from manager [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, textureFilename.c_str()); - assertGl(); - - int thisTeamIndex= world->getThisTeamIndex(); - bool cellExplored = world->showWorldForPlayer(world->getThisFactionIndex()); - bool closed= false; - - Rect2i boundingRect= visibleQuad.computeBoundingRect(); - Rect2i scaledRect= boundingRect/Map::cellScale; - scaledRect.clamp(0, 0, map->getSurfaceW()-1, map->getSurfaceH()-1); - - float waterLevel= world->getMap()->getWaterLevel(); - for(int j=scaledRect.p[0].y; jgetSurfaceCell(i, j); - SurfaceCell *tc1= map->getSurfaceCell(i, j+1); - if(tc0 == NULL) { - throw megaglest_runtime_error("tc0 == NULL"); - } - if(tc1 == NULL) { - throw megaglest_runtime_error("tc1 == NULL"); + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; } - if(cellExplored == false) { - cellExplored = (tc0->isExplored(thisTeamIndex) || tc1->isExplored(thisTeamIndex)); - } + textureManager[rs]->endTexture(texture, mustExistInList); - if(cellExplored == true && tc0->getNearSubmerged()) { - glNormal3f(0.f, 1.f, 0.f); - closed= false; + if (rs == rsGlobal) { + std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); + if (crcFactionPreviewTextureCache.find(textureFilename) != crcFactionPreviewTextureCache.end()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] textureFilename [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, textureFilename.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] free texture from cache [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, textureFilename.c_str()); - triangleCount+= 2; - pointCount+= 2; + crcFactionPreviewTextureCache.erase(textureFilename); + } + } + } + void Renderer::endLastTexture(ResourceScope rs, bool mustExistInList) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - //vertex 1 - glMaterialfv( - GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, - computeWaterColor(waterLevel, tc1->getHeight()).ptr()); - glMultiTexCoord2fv(GL_TEXTURE1, tc1->getFowTexCoord().ptr()); - glTexCoord3f(i, 1.f, waterAnim); - glVertex3f( - static_cast(i)*Map::mapScale, - waterLevel, - static_cast(j+1)*Map::mapScale); + textureManager[rs]->endLastTexture(mustExistInList); + } - //vertex 2 - glMaterialfv( - GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, - computeWaterColor(waterLevel, tc0->getHeight()).ptr()); - glMultiTexCoord2fv(GL_TEXTURE1, tc0->getFowTexCoord().ptr()); - glTexCoord3f(i, 0.f, waterAnim); - glVertex3f( - static_cast(i)*Map::mapScale, - waterLevel, - static_cast(j)*Map::mapScale); + Model *Renderer::newModel(ResourceScope rs, const string &path, bool deletePixMapAfterLoad, std::map > > *loadedFileList, string *sourceLoader) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return NULL; + } - } - else{ - if(closed == false) { - pointCount+= 2; + return modelManager[rs]->newModel(path, deletePixMapAfterLoad, loadedFileList, sourceLoader); + } - //vertex 1 - glMaterialfv( - GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, - computeWaterColor(waterLevel, tc1->getHeight()).ptr()); - glMultiTexCoord2fv(GL_TEXTURE1, tc1->getFowTexCoord().ptr()); - glTexCoord3f(i, 1.f, waterAnim); - glVertex3f( - static_cast(i)*Map::mapScale, - waterLevel, - static_cast(j+1)*Map::mapScale); + void Renderer::endModel(ResourceScope rs, Model *model, bool mustExistInList) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - //vertex 2 - glMaterialfv( - GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, - computeWaterColor(waterLevel, tc0->getHeight()).ptr()); - glMultiTexCoord2fv(GL_TEXTURE1, tc0->getFowTexCoord().ptr()); - glTexCoord3f(i, 0.f, waterAnim); - glVertex3f( - static_cast(i)*Map::mapScale, - waterLevel, - static_cast(j)*Map::mapScale); + modelManager[rs]->endModel(model, mustExistInList); + } + void Renderer::endLastModel(ResourceScope rs, bool mustExistInList) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + modelManager[rs]->endLastModel(mustExistInList); + } + + Texture2D *Renderer::newTexture2D(ResourceScope rs) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return NULL; + } + + return textureManager[rs]->newTexture2D(); + } + + Texture3D *Renderer::newTexture3D(ResourceScope rs) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return NULL; + } + + return textureManager[rs]->newTexture3D(); + } + + Font2D *Renderer::newFont(ResourceScope rs) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return NULL; + } + + return fontManager[rs]->newFont2D(); + } + + Font3D *Renderer::newFont3D(ResourceScope rs) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return NULL; + } + + return fontManager[rs]->newFont3D(); + } + + void Renderer::endFont(::Shared::Graphics::Font *font, ResourceScope rs, bool mustExistInList) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + fontManager[rs]->endFont(font, mustExistInList); + } + + //void Renderer::resetFontManager(ResourceScope rs) { + // if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + // return; + // } + // fontManager[rs]->end(); + // fontManager[rsGlobal]->init(); + //} + + void Renderer::addToDeferredParticleSystemList(std::pair deferredParticleSystem) { + deferredParticleSystems.push_back(deferredParticleSystem); + } + + void Renderer::manageParticleSystem(ParticleSystem *particleSystem, ResourceScope rs) { + particleManager[rs]->manage(particleSystem); + } + + bool Renderer::validateParticleSystemStillExists(ParticleSystem * particleSystem, ResourceScope rs) const { + return particleManager[rs]->validateParticleSystemStillExists(particleSystem); + } + + void Renderer::removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner, ResourceScope rs) { + particleManager[rs]->removeParticleSystemsForParticleOwner(particleOwner); + } + + void Renderer::cleanupParticleSystems(vector &particleSystems, ResourceScope rs) { + particleManager[rs]->cleanupParticleSystems(particleSystems); + } + + void Renderer::cleanupUnitParticleSystems(vector &particleSystems, ResourceScope rs) { + particleManager[rs]->cleanupUnitParticleSystems(particleSystems); + } + + void Renderer::updateParticleManager(ResourceScope rs, int renderFps) { + particleManager[rs]->update(renderFps); + } + + void Renderer::renderParticleManager(ResourceScope rs) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); + glDepthFunc(GL_LESS); + particleRenderer->renderManager(particleManager[rs], modelRenderer); + glPopAttrib(); + } + + void Renderer::swapBuffers() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + //glFlush(); // should not be required - http://www.opengl.org/wiki/Common_Mistakes + //glFlush(); + + GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); + } + + // ==================== lighting ==================== + + //places all the opengl lights + void Renderer::setupLighting() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + int lightCount = 0; + const World *world = game->getWorld(); + const GameCamera *gameCamera = game->getGameCamera(); + const TimeFlow *timeFlow = world->getTimeFlow(); + float time = timeFlow->getTime(); + + assertGl(); + + //sun/moon light + Vec3f lightColor = timeFlow->computeLightColor(); + Vec3f fogColor = world->getTileset()->getFogColor(); + Vec4f lightPos = timeFlow->isDay() ? computeSunPos(time) : computeMoonPos(time); + nearestLightPos = lightPos; + + glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); + glLightfv(GL_LIGHT0, GL_AMBIENT, Vec4f(lightColor*lightAmbFactor, 1.f).ptr()); + glLightfv(GL_LIGHT0, GL_DIFFUSE, Vec4f(lightColor, 1.f).ptr()); + glLightfv(GL_LIGHT0, GL_SPECULAR, Vec4f(0.0f, 0.0f, 0.f, 1.f).ptr()); + + glFogfv(GL_FOG_COLOR, Vec4f(fogColor*lightColor, 1.f).ptr()); + + lightCount++; + + //disable all secondary lights + for (int i = 1; i < maxLights; ++i) { + glDisable(GL_LIGHT0 + i); + } + + //unit lights (not projectiles) + + if (timeFlow->isTotalNight()) { + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + //bool modelRenderStarted = false; + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size() && lightCount < maxLights; + ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + + if (world->toRenderUnit(unit) && + unit->getCurrMidHeightVector().dist(gameCamera->getPos()) < maxLightDist && + unit->getType()->getLight() && unit->isOperative()) { + //printf("$$$ Show light for faction: %s # %d / %d for Unit [%d - %s]\n",world->getFaction(i)->getType()->getName().c_str(),lightCount,maxLights,unit->getId(),unit->getFullName().c_str()); + + Vec4f pos = Vec4f(unit->getCurrMidHeightVector()); + pos.y += 4.f; + + GLenum lightEnum = GL_LIGHT0 + lightCount; + + glEnable(lightEnum); + glLightfv(lightEnum, GL_POSITION, pos.ptr()); + glLightfv(lightEnum, GL_AMBIENT, Vec4f(unit->getType()->getLightColor()).ptr()); + glLightfv(lightEnum, GL_DIFFUSE, Vec4f(unit->getType()->getLightColor()).ptr()); + glLightfv(lightEnum, GL_SPECULAR, Vec4f(unit->getType()->getLightColor()*0.3f).ptr()); + glLightf(lightEnum, GL_QUADRATIC_ATTENUATION, 0.05f); + + ++lightCount; + + const GameCamera *gameCamera = game->getGameCamera(); + + if (Vec3f(pos).dist(gameCamera->getPos()) < Vec3f(nearestLightPos).dist(gameCamera->getPos())) { + nearestLightPos = pos; + } + } + } + } + } + + assertGl(); + } + + //void Renderer::setupLightingForRotatedModel() { + // if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + // return; + // } + // + // const World *world= game->getWorld(); + // //const GameCamera *gameCamera= game->getGameCamera(); + // const TimeFlow *timeFlow= world->getTimeFlow(); + // float time= timeFlow->getTime(); + // + // assertGl(); + // + // //sun/moon light + // Vec3f lightColor= timeFlow->computeLightColor(); + // Vec3f fogColor= world->getTileset()->getFogColor(); + // Vec4f lightPos= timeFlow->isDay()? computeSunPos(time): computeMoonPos(time); + // //nearestLightPos= lightPos; + // + // glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); + // glLightfv(GL_LIGHT0, GL_AMBIENT, Vec4f(lightColor*lightAmbFactor, 1.f).ptr()); + // glLightfv(GL_LIGHT0, GL_DIFFUSE, Vec4f(lightColor, 1.f).ptr()); + // glLightfv(GL_LIGHT0, GL_SPECULAR, Vec4f(0.0f, 0.0f, 0.f, 1.f).ptr()); + // + // glFogfv(GL_FOG_COLOR, Vec4f(fogColor*lightColor, 1.f).ptr()); + // + // assertGl(); + //} + + void Renderer::loadGameCameraMatrix() { + const GameCamera *gameCamera = game->getGameCamera(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + if (gameCamera != NULL) { + glRotatef(gameCamera->getVAng(), -1, 0, 0); + glRotatef(gameCamera->getHAng(), 0, 1, 0); + glTranslatef(-(gameCamera->getPos().x + gameCamera->getShakeOffset().x), + -gameCamera->getPos().y, + -(gameCamera->getPos().z + gameCamera->getShakeOffset().y)); + } + } + + void Renderer::loadCameraMatrix(const Camera *camera) { + const Vec3f &position = camera->getConstPosition(); + Quaternion orientation = camera->getOrientation().conjugate(); + + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glMultMatrixf(orientation.toMatrix4().ptr()); + glTranslatef(-position.x, -position.y, -position.z); + } + + static Vec2i _unprojectMap(const Vec2i& pt, const GLdouble* model, const GLdouble* projection, const GLint* viewport, const char* label = NULL) { + Vec3d a, b; + /* note viewport[3] is height of window in pixels */ + GLint realy = viewport[3] - (GLint) pt.y; + gluUnProject(pt.x, realy, 0, model, projection, viewport, &a.x, &a.y, &a.z); + gluUnProject(pt.x, realy, 1, model, projection, viewport, &b.x, &b.y, &b.z); + + // junk values if you were looking parallel to the XZ plane; this shouldn't happen as the camera can't do this? + const Vec3f + start(a.x, a.y, a.z), + stop(b.x, b.y, b.z), + plane(0, 0, 0), + norm(0, 1, 0), + u = stop - start, + w = start - plane; + const float d = norm.x*u.x + norm.y*u.y + norm.z*u.z; + if (std::fabs(d) < 0.00001) + throw pti_D_IS_ZERO; + + const float nd = -(norm.x*w.x + norm.y*w.y + norm.z*w.z) / d; + if (nd < 0.0 || nd >= 1.0) + throw pti_N_OVER_D_IS_OUTSIDE; + + const Vec3f i = start + u * nd; + //const Vec2i pos(i.x,i.z); + + Vec2i pos; + if (strcmp(label, "tl") == 0) { + pos = Vec2i(std::floor(i.x), std::floor(i.z)); + } else if (strcmp(label, "tr") == 0) { + pos = Vec2i(std::ceil(i.x), std::floor(i.z)); + } else if (strcmp(label, "bl") == 0) { + pos = Vec2i(std::floor(i.x), std::ceil(i.z)); + } else if (strcmp(label, "br") == 0) { + pos = Vec2i(std::ceil(i.x), std::ceil(i.z)); + } + + if (false) { // print debug info + if (label) printf("%s ", label); + printf("%d,%d -> %f,%f,%f -> %f,%f,%f -> %f,%f,%f -> %d,%d\n", + pt.x, pt.y, + start.x, start.y, start.z, + stop.x, stop.y, stop.z, + i.x, i.y, i.z, + pos.x, pos.y); + } + return pos; + + } + + bool Renderer::ExtractFrustum(VisibleQuadContainerCache &quadCacheItem) { + bool frustumChanged = false; + vector proj(16, 0); + vector modl(16, 0); + + /* Get the current PROJECTION matrix from OpenGL */ + glGetFloatv(GL_PROJECTION_MATRIX, &proj[0]); + + /* Get the current MODELVIEW matrix from OpenGL */ + glGetFloatv(GL_MODELVIEW_MATRIX, &modl[0]); + + // for(unsigned int i = 0; i < proj.size(); ++i) { + // //printf("\ni = %d proj [%f][%f] modl [%f][%f]\n",i,proj[i],quadCacheItem.proj[i],modl[i],quadCacheItem.modl[i]); + // if(proj[i] != quadCacheItem.proj[i]) { + // frustumChanged = true; + // break; + // } + // if(modl[i] != quadCacheItem.modl[i]) { + // frustumChanged = true; + // break; + // } + // } + + // Check the frustum cache + const bool useFrustumCache = Config::getInstance().getBool("EnableFrustrumCache", "false"); + pair, vector > lookupKey; + if (useFrustumCache == true) { + lookupKey = make_pair(proj, modl); + map, vector >, vector > >::iterator iterFind = quadCacheItem.frustumDataCache.find(lookupKey); + if (iterFind != quadCacheItem.frustumDataCache.end()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum found in cache\n"); + + quadCacheItem.frustumData = iterFind->second; + frustumChanged = (quadCacheItem.proj != proj || quadCacheItem.modl != modl); + if (frustumChanged == true) { + quadCacheItem.proj = proj; + quadCacheItem.modl = modl; + } + + return frustumChanged; + } + } + + if (quadCacheItem.proj != proj || quadCacheItem.modl != modl) { + //if(frustumChanged == true) { + frustumChanged = true; + vector > &frustum = quadCacheItem.frustumData; + //assert(frustum.size() == 6); + //assert(frustum[0].size() == 4); + + quadCacheItem.proj = proj; + quadCacheItem.modl = modl; + + float clip[16]; + float t = 0; + + /* Combine the two matrices (multiply projection by modelview) */ + clip[0] = modl[0] * proj[0] + modl[1] * proj[4] + modl[2] * proj[8] + modl[3] * proj[12]; + clip[1] = modl[0] * proj[1] + modl[1] * proj[5] + modl[2] * proj[9] + modl[3] * proj[13]; + clip[2] = modl[0] * proj[2] + modl[1] * proj[6] + modl[2] * proj[10] + modl[3] * proj[14]; + clip[3] = modl[0] * proj[3] + modl[1] * proj[7] + modl[2] * proj[11] + modl[3] * proj[15]; + + clip[4] = modl[4] * proj[0] + modl[5] * proj[4] + modl[6] * proj[8] + modl[7] * proj[12]; + clip[5] = modl[4] * proj[1] + modl[5] * proj[5] + modl[6] * proj[9] + modl[7] * proj[13]; + clip[6] = modl[4] * proj[2] + modl[5] * proj[6] + modl[6] * proj[10] + modl[7] * proj[14]; + clip[7] = modl[4] * proj[3] + modl[5] * proj[7] + modl[6] * proj[11] + modl[7] * proj[15]; + + clip[8] = modl[8] * proj[0] + modl[9] * proj[4] + modl[10] * proj[8] + modl[11] * proj[12]; + clip[9] = modl[8] * proj[1] + modl[9] * proj[5] + modl[10] * proj[9] + modl[11] * proj[13]; + clip[10] = modl[8] * proj[2] + modl[9] * proj[6] + modl[10] * proj[10] + modl[11] * proj[14]; + clip[11] = modl[8] * proj[3] + modl[9] * proj[7] + modl[10] * proj[11] + modl[11] * proj[15]; + + clip[12] = modl[12] * proj[0] + modl[13] * proj[4] + modl[14] * proj[8] + modl[15] * proj[12]; + clip[13] = modl[12] * proj[1] + modl[13] * proj[5] + modl[14] * proj[9] + modl[15] * proj[13]; + clip[14] = modl[12] * proj[2] + modl[13] * proj[6] + modl[14] * proj[10] + modl[15] * proj[14]; + clip[15] = modl[12] * proj[3] + modl[13] * proj[7] + modl[14] * proj[11] + modl[15] * proj[15]; + + /* Extract the numbers for the RIGHT plane */ + frustum[0][0] = clip[3] - clip[0]; + frustum[0][1] = clip[7] - clip[4]; + frustum[0][2] = clip[11] - clip[8]; + frustum[0][3] = clip[15] - clip[12]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 0, frustum[0][0], frustum[0][1], frustum[0][2], frustum[0][3]); + + /* Normalize the result */ + t = std::sqrt(frustum[0][0] * frustum[0][0] + frustum[0][1] * frustum[0][1] + frustum[0][2] * frustum[0][2]); + if (t != 0.0) { + frustum[0][0] /= t; + frustum[0][1] /= t; + frustum[0][2] /= t; + frustum[0][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 0, frustum[0][0], frustum[0][1], frustum[0][2], frustum[0][3], t); + } + + /* Extract the numbers for the LEFT plane */ + frustum[1][0] = clip[3] + clip[0]; + frustum[1][1] = clip[7] + clip[4]; + frustum[1][2] = clip[11] + clip[8]; + frustum[1][3] = clip[15] + clip[12]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 1, frustum[1][0], frustum[1][1], frustum[1][2], frustum[1][3]); + + /* Normalize the result */ + t = std::sqrt(frustum[1][0] * frustum[1][0] + frustum[1][1] * frustum[1][1] + frustum[1][2] * frustum[1][2]); + if (t != 0.0) { + frustum[1][0] /= t; + frustum[1][1] /= t; + frustum[1][2] /= t; + frustum[1][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 1, frustum[1][0], frustum[1][1], frustum[1][2], frustum[1][3], t); + } + + /* Extract the BOTTOM plane */ + frustum[2][0] = clip[3] + clip[1]; + frustum[2][1] = clip[7] + clip[5]; + frustum[2][2] = clip[11] + clip[9]; + frustum[2][3] = clip[15] + clip[13]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 2, frustum[2][0], frustum[2][1], frustum[2][2], frustum[2][3]); + + /* Normalize the result */ + + t = std::sqrt(frustum[2][0] * frustum[2][0] + frustum[2][1] * frustum[2][1] + frustum[2][2] * frustum[2][2]); + if (t != 0.0) { + frustum[2][0] /= t; + frustum[2][1] /= t; + frustum[2][2] /= t; + frustum[2][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 2, frustum[2][0], frustum[2][1], frustum[2][2], frustum[2][3], t); + } + + /* Extract the TOP plane */ + frustum[3][0] = clip[3] - clip[1]; + frustum[3][1] = clip[7] - clip[5]; + frustum[3][2] = clip[11] - clip[9]; + frustum[3][3] = clip[15] - clip[13]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 3, frustum[3][0], frustum[3][1], frustum[3][2], frustum[3][3]); + + /* Normalize the result */ + + t = std::sqrt(frustum[3][0] * frustum[3][0] + frustum[3][1] * frustum[3][1] + frustum[3][2] * frustum[3][2]); + if (t != 0.0) { + frustum[3][0] /= t; + frustum[3][1] /= t; + frustum[3][2] /= t; + frustum[3][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 3, frustum[3][0], frustum[3][1], frustum[3][2], frustum[3][3], t); + } + + /* Extract the FAR plane */ + frustum[4][0] = clip[3] - clip[2]; + frustum[4][1] = clip[7] - clip[6]; + frustum[4][2] = clip[11] - clip[10]; + frustum[4][3] = clip[15] - clip[14]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 4, frustum[4][0], frustum[4][1], frustum[4][2], frustum[4][3]); + + /* Normalize the result */ + + t = std::sqrt(frustum[4][0] * frustum[4][0] + frustum[4][1] * frustum[4][1] + frustum[4][2] * frustum[4][2]); + + if (t != 0.0) { + frustum[4][0] /= t; + frustum[4][1] /= t; + frustum[4][2] /= t; + frustum[4][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 4, frustum[4][0], frustum[4][1], frustum[4][2], frustum[4][3], t); + } + + /* Extract the NEAR plane */ + frustum[5][0] = clip[3] + clip[2]; + frustum[5][1] = clip[7] + clip[6]; + frustum[5][2] = clip[11] + clip[10]; + frustum[5][3] = clip[15] + clip[14]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%da: [%f][%f][%f][%f]\n", 5, frustum[5][0], frustum[5][1], frustum[5][2], frustum[5][3]); + + /* Normalize the result */ + + t = std::sqrt(frustum[5][0] * frustum[5][0] + frustum[5][1] * frustum[5][1] + frustum[5][2] * frustum[5][2]); + + if (t != 0.0) { + frustum[5][0] /= t; + frustum[5][1] /= t; + frustum[5][2] /= t; + frustum[5][3] /= t; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nCalc Frustum #%db: [%f][%f][%f][%f] t = %f\n", 5, frustum[5][0], frustum[5][1], frustum[5][2], frustum[5][3], t); + } + + if (useFrustumCache == true) { + quadCacheItem.frustumDataCache[lookupKey] = frustum; + } + } + return frustumChanged; + } + + //bool Renderer::PointInFrustum(vector > &frustum, float x, float y, float z ) { + // unsigned int p=0; + // + // for( p = 0; p < frustum.size(); p++ ) { + // if( frustum[p][0] * x + frustum[p][1] * y + frustum[p][2] * z + frustum[p][3] <= 0 ) { + // return false; + // } + // } + // return true; + //} + // + //bool Renderer::SphereInFrustum(vector > &frustum, float x, float y, float z, float radius) { + // // Go through all the sides of the frustum + // for(int i = 0; i < (int)frustum.size(); i++ ) { + // // If the center of the sphere is farther away from the plane than the radius + // if(frustum[i][0] * x + frustum[i][1] * y + frustum[i][2] * z + frustum[i][3] <= -radius ) { + // // The distance was greater than the radius so the sphere is outside of the frustum + // return false; + // } + // } + // + // // The sphere was inside of the frustum! + // return true; + //} + + bool Renderer::CubeInFrustum(vector > &frustum, float x, float y, float z, float size) { + unsigned int p = 0; + + for (p = 0; p < frustum.size(); p++) { + if (frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + frustum[p][2] * (z - size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + frustum[p][2] * (z - size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + frustum[p][2] * (z - size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + frustum[p][2] * (z - size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x - size) + frustum[p][1] * (y - size) + frustum[p][2] * (z + size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x + size) + frustum[p][1] * (y - size) + frustum[p][2] * (z + size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x - size) + frustum[p][1] * (y + size) + frustum[p][2] * (z + size) + frustum[p][3] > 0) + continue; + if (frustum[p][0] * (x + size) + frustum[p][1] * (y + size) + frustum[p][2] * (z + size) + frustum[p][3] > 0) + continue; + return false; + } + return true; + } + + void Renderer::computeVisibleQuad() { + visibleQuad = this->gameCamera->computeVisibleQuad(); + + bool frustumChanged = false; + if (VisibleQuadContainerCache::enableFrustumCalcs == true) { + frustumChanged = ExtractFrustum(quadCache); + } + + if (frustumChanged && SystemFlags::VERBOSE_MODE_ENABLED) { + printf("\nCamera: %d,%d %d,%d %d,%d %d,%d\n", + visibleQuad.p[0].x, visibleQuad.p[0].y, + visibleQuad.p[1].x, visibleQuad.p[1].y, + visibleQuad.p[2].x, visibleQuad.p[2].y, + visibleQuad.p[3].x, visibleQuad.p[3].y); + + for (unsigned int i = 0; i < quadCache.frustumData.size(); ++i) { + printf("\nFrustum #%u [" MG_SIZE_T_SPECIFIER "]: ", i, quadCache.frustumData.size()); + vector &frustumDataInner = quadCache.frustumData[i]; + for (unsigned int j = 0; j < frustumDataInner.size(); ++j) { + printf("[%f]", quadCache.frustumData[i][j]); + } + } + + printf("\nEND\n"); + } + + const bool newVisibleQuadCalc = false; + if (newVisibleQuadCalc) { + const bool debug = false; + try { + if (debug) { + visibleQuad = gameCamera->computeVisibleQuad(); + printf("Camera: %d,%d %d,%d %d,%d %d,%d\n", + visibleQuad.p[0].x, visibleQuad.p[0].y, + visibleQuad.p[1].x, visibleQuad.p[1].y, + visibleQuad.p[2].x, visibleQuad.p[2].y, + visibleQuad.p[3].x, visibleQuad.p[3].y); + } + + + // compute the four corners using OpenGL + GLdouble model[16], projection[16]; + GLint viewport[4]; + glGetDoublev(GL_MODELVIEW_MATRIX, model); + glGetDoublev(GL_PROJECTION_MATRIX, projection); + glGetIntegerv(GL_VIEWPORT, viewport); + Vec2i + tl = _unprojectMap(Vec2i(0, 0), model, projection, viewport, "tl"), + tr = _unprojectMap(Vec2i(viewport[2], 0), model, projection, viewport, "tr"), + br = _unprojectMap(Vec2i(viewport[2], viewport[3]), model, projection, viewport, "br"), + bl = _unprojectMap(Vec2i(0, viewport[3]), model, projection, viewport, "bl"); + + + // orientate it for map iterator + //bool swapRequiredX = false; + bool swapRequiredY = false; + int const cellBuffer = 4; + if ((tl.x > tr.x) || (bl.x > br.x)) { + if (debug) printf("Swap X???\n"); + + //std::swap(tl,bl); + //std::swap(tr,br); + if (tl.x > tr.x) { + if (debug) printf("Swap X1???\n"); + + tr.x += cellBuffer; + tl.x -= cellBuffer; + + std::swap(tl.x, tr.x); + //swapRequiredX = true; + } else { + tl.x += cellBuffer; + tr.x -= cellBuffer; + } + if (bl.x > br.x) { + if (debug) printf("Swap X2???\n"); + + bl.x += cellBuffer; + br.x -= cellBuffer; + + std::swap(bl.x, br.x); + //swapRequiredX = true; + } else { + br.x += cellBuffer; + bl.x -= cellBuffer; + } + } + + if ((tl.y > bl.y) || (tr.y > br.y)) { + visibleQuad = this->gameCamera->computeVisibleQuad(); + + if (debug) printf("Swap Y???\n"); + + if (tl.y > bl.y) { + if (debug) printf("Swap Y1???\n"); + + tl.y += cellBuffer; + bl.y -= cellBuffer; + + std::swap(tl.y, bl.y); + swapRequiredY = true; + } else { + bl.y += cellBuffer; + tl.y -= cellBuffer; + } + if (tr.y > br.y) { + if (debug) printf("Swap Y2???\n"); + + tr.y += cellBuffer; + br.y -= cellBuffer; + + std::swap(tr.y, br.y); + swapRequiredY = true; + } else { + br.y += cellBuffer; + tr.y -= cellBuffer; + } + + + //std::swap(tl,tr); + //std::swap(bl,br); + } + if (swapRequiredY == false) { + tl.y -= cellBuffer; + tr.y -= cellBuffer; + bl.y += cellBuffer; + br.y += cellBuffer; + } + + // set it as the frustum + visibleQuad = Quad2i(tl, bl, tr, br); // strange order + if (debug) { + printf("Will: %d,%d %d,%d %d,%d %d,%d\n", + visibleQuad.p[0].x, visibleQuad.p[0].y, + visibleQuad.p[1].x, visibleQuad.p[1].y, + visibleQuad.p[2].x, visibleQuad.p[2].y, + visibleQuad.p[3].x, visibleQuad.p[3].y); + } + } catch (PROJECTION_TO_INFINITY &e) { + if (debug) printf("hmm staring at the horizon %d\n", (int) e); + // use historic code solution + visibleQuad = this->gameCamera->computeVisibleQuad(); + } + } + } + + // ======================================= + // basic rendering + // ======================================= + + void Renderer::renderMouse2d(int x, int y, int anim, float fade) { + if (no2DMouseRendering == true) { + return; + } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + // float blue=0.0f; + // float green=0.4f; + if (game != NULL && game->getGui() != NULL) { + const Gui *gui = game->getGui(); + const Display *display = gui->getDisplay(); + int downPos = display->getDownSelectedPos(); + if (downPos != Display::invalidPos) { + // in state of doing something + const Texture2D *texture = display->getDownImage(downPos); + renderTextureQuad(x + 18, y - 50, 32, 32, texture, 0.8f); + } + // else { + // // Display current commandtype + // const Unit *unit=NULL; + // if(gui->getSelection()->isEmpty()){ + // blue=0.0f; + // green=0.1f; + // } + // else{ + // unit=gui->getSelection()->getFrontUnit(); + // if(unit->getCurrCommand()!=NULL && unit->getCurrCommand()->getCommandType()->getImage()!=NULL){ + // const Texture2D *texture = unit->getCurrCommand()->getCommandType()->getImage(); + // renderTextureQuad(x+18,y-50,32,32,texture,0.2f); + // } + // } + // } + + if (game->isMarkCellMode() == true) { + const Texture2D *texture = game->getMarkCellTexture(); + renderTextureQuad(x, y, texture->getTextureWidth(), texture->getTextureHeight(), texture, 0.8f); + } + if (game->isUnMarkCellMode() == true) { + const Texture2D *texture = game->getUnMarkCellTexture(); + renderTextureQuad(x, y, texture->getTextureWidth(), texture->getTextureHeight(), texture, 0.8f); + } + } + + float fadeFactor = fade + 1.f; + + anim = anim * 2 - maxMouse2dAnim; + + float color2 = (abs(anim*(int) fadeFactor) / static_cast(maxMouse2dAnim)) / 2.f + 0.4f; + float color1 = (abs(anim*(int) fadeFactor) / static_cast(maxMouse2dAnim)) / 2.f + 0.8f; + + glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT); + glEnable(GL_BLEND); + + //inside + glColor4f(0.4f*fadeFactor, 0.2f*fadeFactor, 0.2f*fadeFactor, 0.5f*fadeFactor); + glBegin(GL_TRIANGLES); + glVertex2i(x, y); + glVertex2i(x + 20, y - 10); + glVertex2i(x + 10, y - 20); + glEnd(); + + //border + glLineWidth(2); + glBegin(GL_LINE_LOOP); + glColor4f(1.f, 0.2f, 0, color1); + glVertex2i(x, y); + glColor4f(1.f, 0.4f, 0, color2); + glVertex2i(x + 20, y - 10); + glColor4f(1.f, 0.4f, 0, color2); + glVertex2i(x + 10, y - 20); + glEnd(); + glPopAttrib(); + + + /* + if(no2DMouseRendering == true) { + return; + } + float color1 = 0.0, color2 = 0.0; + + float fadeFactor = fade + 1.f; + + anim= anim * 2 - maxMouse2dAnim; + + color2= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.4f; + color1= (abs(anim*(int)fadeFactor)/static_cast(maxMouse2dAnim))/2.f+0.8f; + + glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT); + glEnable(GL_BLEND); + + //inside + Vec2i vertices[3]; + vertices[0] = Vec2i(x, y); + vertices[1] = Vec2i(x+20, y-10); + vertices[2] = Vec2i(x+10, y-20); + + glColor4f(0.4f*fadeFactor, 0.2f*fadeFactor, 0.2f*fadeFactor, 0.5f*fadeFactor); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_INT, 0, &vertices[0]); + glDrawArrays(GL_TRIANGLES, 0, 3); + glDisableClientState(GL_VERTEX_ARRAY); + + //border + vertices[0] = Vec2i(x, y); + vertices[1] = Vec2i(x+20, y-10); + vertices[2] = Vec2i(x+10, y-20); + + Vec4f colors[4]; + colors[0] = Vec4f(1.f, 0.2f, 0, color1); + colors[1] = Vec4f(1.f, 0.4f, 0, color2); + colors[2] = Vec4f(1.f, 0.4f, 0, color2); + + glLineWidth(2); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_INT, 0, &vertices[0]); + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(4, GL_FLOAT, 0, &colors[0]); + glDrawArrays(GL_LINE_LOOP, 0, 3); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + glPopAttrib(); + */ + } + + void Renderer::renderMouse3d() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("RecordMode", "false") == true) { + return; + } + + if (game == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d game == NULL", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); + } else if (game->getGui() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d game->getGui() == NULL", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); + } else if (game->getGui()->getMouse3d() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d game->getGui()->getMouse3d() == NULL", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); + } + + const Gui *gui = game->getGui(); + const Mouse3d *mouse3d = gui->getMouse3d(); + const Map *map = game->getWorld()->getMap(); + if (map == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d map == NULL", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); + } + + assertGl(); + + if ((mouse3d->isEnabled() || gui->isPlacingBuilding()) && gui->isValidPosObjWorld()) { + const Vec2i &pos = gui->getPosObjWorld(); + + glMatrixMode(GL_MODELVIEW); + + glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_BLEND); + glDisable(GL_STENCIL_TEST); + glDepthFunc(GL_LESS); + glEnable(GL_COLOR_MATERIAL); + glDepthMask(GL_FALSE); + + if (gui->isPlacingBuilding()) { + + modelRenderer->begin(true, true, false, false); + + const UnitType *building = gui->getBuilding(); + const Gui *gui = game->getGui(); + renderGhostModel(building, pos, gui->getSelectedFacing()); + + modelRenderer->end(); + + glDisable(GL_COLOR_MATERIAL); + glPopAttrib(); + } else { + glPushMatrix(); + Vec3f pos3f = Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); + Vec4f color; + GLUquadricObj *cilQuadric; + //standard mouse + glDisable(GL_TEXTURE_2D); + glDisable(GL_CULL_FACE); + color = Vec4f(1.f, 0.f, 0.f, 1.f - mouse3d->getFade()); + glColor4fv(color.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color.ptr()); + + glTranslatef(pos3f.x, pos3f.y + 2.f, pos3f.z); + glRotatef(90.f, 1.f, 0.f, 0.f); + glRotatef(static_cast(mouse3d->getRot()), 0.f, 0.f, 1.f); + + cilQuadric = gluNewQuadric(); + gluQuadricDrawStyle(cilQuadric, GLU_FILL); + gluCylinder(cilQuadric, 0.5f, 0.f, 2.f, 4, 1); + gluCylinder(cilQuadric, 0.5f, 0.f, 0.f, 4, 1); + glTranslatef(0.f, 0.f, 1.f); + gluCylinder(cilQuadric, 0.7f, 0.f, 1.f, 4, 1); + gluCylinder(cilQuadric, 0.7f, 0.f, 0.f, 4, 1); + gluDeleteQuadric(cilQuadric); + + glPopAttrib(); + glPopMatrix(); + } + } + + } + + void Renderer::renderBackground(const Texture2D *texture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const Metrics &metrics = Metrics::getInstance(); + + assertGl(); + + glPushAttrib(GL_ENABLE_BIT); + + glDisable(GL_LIGHTING); + glEnable(GL_TEXTURE_2D); + + renderQuad(0, 0, metrics.getVirtualW(), metrics.getVirtualH(), texture); + + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderTextureQuad(int x, int y, int w, int h, const Texture2D *texture, float alpha, const Vec3f *color) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + assertGl(); + + glPushAttrib(GL_ENABLE_BIT); + + glDisable(GL_LIGHTING); + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + if (color != NULL) { + Vec4f newColor(*color); + newColor.w = alpha; + glColor4fv(newColor.ptr()); + } else { + glColor4f(1.f, 1.f, 1.f, alpha); + } + renderQuad(x, y, w, h, texture); + + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderConsoleLine3D(int lineIndex, int xPosition, int yPosition, int lineHeight, + Font3D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Vec4f fontColor; + Lang &lang = Lang::getInstance(); + //const Metrics &metrics= Metrics::getInstance(); + FontMetrics *fontMetrics = font->getMetrics(); + + if (game != NULL) { + fontColor = game->getGui()->getDisplay()->getColor(); + } else { + // white shadowed is default ( in the menu for example ) + //fontColor=Vec4f(1.f, 1.f, 1.f, 0.0f); + fontColor = Vec4f(lineInfo->color.x, lineInfo->color.y, lineInfo->color.z, 0.0f); + } + + Vec4f defaultFontColor = fontColor; + + if (lineInfo->PlayerIndex >= 0) { + std::map &crcPlayerTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::playerTextureCacheLookupKey); + Vec3f playerColor = crcPlayerTextureCache[lineInfo->PlayerIndex]->getPixmap()->getPixel3f(0, 0); + fontColor.x = playerColor.x; + fontColor.y = playerColor.y; + fontColor.z = playerColor.z; + + GameNetworkInterface *gameNetInterface = NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetInterface != NULL && gameNetInterface->getGameSettings() != NULL) { + const GameSettings *gameSettings = gameNetInterface->getGameSettings(); + string playerName = gameSettings->getNetworkPlayerNameByPlayerIndex(lineInfo->PlayerIndex); + if (playerName != lineInfo->originalPlayerName && lineInfo->originalPlayerName != "") { + playerName = lineInfo->originalPlayerName; + } + if (playerName == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + playerName = lang.getString("SystemUser"); + } + //printf("playerName [%s], line [%s]\n",playerName.c_str(),line.c_str()); + + //string headerLine = "*" + playerName + ":"; + //string headerLine = playerName + ": "; + string headerLine = playerName; + if (lineInfo->teamMode == true) { + headerLine += " (" + lang.getString("Team") + ")"; + } + headerLine += ": "; + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + + renderTextShadow3D( + headerLine, + font, + fontColor, + xPosition, lineIndex * lineHeight + yPosition); + + fontColor = defaultFontColor; + //xPosition += (8 * (playerName.length() + 2)); + // Proper font spacing after username portion of chat text rendering + + //xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); + xPosition += fontMetrics->getTextWidth(headerLine); + } + } else if (lineInfo->originalPlayerName != "") { + string playerName = lineInfo->originalPlayerName; + //string headerLine = playerName + ": "; + string headerLine = playerName; + if (lineInfo->teamMode == true) { + headerLine += " (" + lang.getString("Team") + ")"; + } + headerLine += ": "; + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + + renderTextShadow3D( + headerLine, + font, + fontColor, + xPosition, lineIndex * lineHeight + yPosition); + + fontColor = defaultFontColor; + //xPosition += (8 * (playerName.length() + 2)); + // Proper font spacing after username portion of chat text rendering + //xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); + xPosition += fontMetrics->getTextWidth(headerLine); + } else { + fontColor = defaultFontColor; + } + + if (stringToHightlight != "" && lineInfo->text.find(stringToHightlight) != string::npos) { + fontColor = Vec4f(1.f, 0.5f, 0.5f, 0.0f); + } + renderTextShadow3D( + lineInfo->text, + font, + fontColor, + xPosition, (lineIndex * lineHeight) + yPosition); + } + + void Renderer::renderConsoleLine(int lineIndex, int xPosition, int yPosition, int lineHeight, + Font2D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Vec4f fontColor; + Lang &lang = Lang::getInstance(); + + const Metrics &metrics = Metrics::getInstance(); + FontMetrics *fontMetrics = font->getMetrics(); + + if (game != NULL) { + fontColor = game->getGui()->getDisplay()->getColor(); + } else { + // white shadowed is default ( in the menu for example ) + //fontColor=Vec4f(1.f, 1.f, 1.f, 0.0f); + fontColor = Vec4f(lineInfo->color.x, lineInfo->color.y, lineInfo->color.z, 0.0f); + } + + Vec4f defaultFontColor = fontColor; + + if (lineInfo->PlayerIndex >= 0) { + std::map &crcPlayerTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::playerTextureCacheLookupKey); + Vec3f playerColor = crcPlayerTextureCache[lineInfo->PlayerIndex]->getPixmap()->getPixel3f(0, 0); + fontColor.x = playerColor.x; + fontColor.y = playerColor.y; + fontColor.z = playerColor.z; + + GameNetworkInterface *gameNetInterface = NetworkManager::getInstance().getGameNetworkInterface(); + if (gameNetInterface != NULL && gameNetInterface->getGameSettings() != NULL) { + const GameSettings *gameSettings = gameNetInterface->getGameSettings(); + string playerName = gameSettings->getNetworkPlayerNameByPlayerIndex(lineInfo->PlayerIndex); + if (playerName != lineInfo->originalPlayerName && lineInfo->originalPlayerName != "") { + playerName = lineInfo->originalPlayerName; + } + //printf("playerName [%s], line [%s]\n",playerName.c_str(),line.c_str()); + + //string headerLine = "*" + playerName + ":"; + //string headerLine = playerName + ": "; + string headerLine = playerName; + if (lineInfo->teamMode == true) { + headerLine += " (" + lang.getString("Team") + ")"; + } + headerLine += ": "; + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + + renderTextShadow( + headerLine, + font, + fontColor, + xPosition, lineIndex * lineHeight + yPosition); + + fontColor = defaultFontColor; + //xPosition += (8 * (playerName.length() + 2)); + // Proper font spacing after username portion of chat text rendering + xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); + } + } else if (lineInfo->originalPlayerName != "") { + string playerName = lineInfo->originalPlayerName; + //string headerLine = playerName + ": "; + string headerLine = playerName; + if (lineInfo->teamMode == true) { + headerLine += " (" + lang.getString("Team") + ")"; + } + headerLine += ": "; + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + + renderTextShadow( + headerLine, + font, + fontColor, + xPosition, lineIndex * lineHeight + yPosition); + + fontColor = defaultFontColor; + //xPosition += (8 * (playerName.length() + 2)); + // Proper font spacing after username portion of chat text rendering + xPosition += (metrics.toVirtualX(fontMetrics->getTextWidth(headerLine))); + } else { + fontColor = defaultFontColor; + } + + if (stringToHightlight != "" && lineInfo->text.find(stringToHightlight) != string::npos) { + fontColor = Vec4f(1.f, 0.5f, 0.5f, 0.0f); + } + renderTextShadow( + lineInfo->text, + font, + fontColor, + xPosition, (lineIndex * lineHeight) + yPosition); + } + + void Renderer::renderConsole(const Console *console, ConsoleMode mode, int overrideMaxConsoleLines) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (console == NULL) { + throw megaglest_runtime_error("console == NULL"); + } + + glPushAttrib(GL_ENABLE_BIT); + glEnable(GL_BLEND); + + if (mode == consoleFull) { + int x = console->getXPos() - 5; + int y = console->getYPos() - 5; + int h = console->getLineHeight()*console->getStoredLineCount(); + + if (h > 0) { + int w = 1000; + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(0.0f, 0.0f, 0.0f, 0.8f); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x, y); + glVertex2i(x, y + h); + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glEnd(); + glPopAttrib(); + } + for (int i = 0; i < console->getStoredLineCount(); ++i) { + const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); + if (renderText3DEnabled == true) { + renderConsoleLine3D(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont3D(), + console->getStringToHighlight(), &lineInfo); + } else { + renderConsoleLine(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont(), + console->getStringToHighlight(), &lineInfo); + } + } + } else if (mode == consoleStoredOnly) { + int allowedMaxLines = (overrideMaxConsoleLines >= 0 ? overrideMaxConsoleLines : maxConsoleLines); + for (int i = 0; i < console->getStoredLineCount() && i < allowedMaxLines; ++i) { + const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); + if (renderText3DEnabled == true) { + renderConsoleLine3D(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); + } else { + renderConsoleLine(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); + } + } + } else if (mode == consoleStoredAndNormal) { + int allowedMaxLines = (overrideMaxConsoleLines >= 0 ? overrideMaxConsoleLines : maxConsoleLines); + float starttimestamp = -1; + int consoleIndex = 0; + for (int i = 0; i < console->getLineCount() && i < allowedMaxLines; ++i) { + const ConsoleLineInfo &lineInfo = console->getLineItem(i); + if (starttimestamp > lineInfo.timeStamp || starttimestamp == -1) starttimestamp = lineInfo.timeStamp; + if (renderText3DEnabled == true) { + renderConsoleLine3D(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); + } else { + renderConsoleLine(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); + } + consoleIndex++; + } + for (int i = 0; i < console->getStoredLineCount() && consoleIndex < allowedMaxLines; ++i) { + const ConsoleLineInfo &lineInfo = console->getStoredLineItem(i); + if (lineInfo.timeStamp < starttimestamp || starttimestamp == -1) { + if (renderText3DEnabled == true) { + renderConsoleLine3D(consoleIndex, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); + } else { + renderConsoleLine(consoleIndex, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); + } + consoleIndex++; + } + } + } else if (mode == consoleNormal) { + for (int i = 0; i < console->getLineCount(); ++i) { + const ConsoleLineInfo &lineInfo = console->getLineItem(i); + if (renderText3DEnabled == true) { + renderConsoleLine3D(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont3D(), console->getStringToHighlight(), &lineInfo); + } else { + renderConsoleLine(i, console->getXPos(), console->getYPos(), + console->getLineHeight(), console->getFont(), console->getStringToHighlight(), &lineInfo); + } + } + } + glPopAttrib(); + } + + void Renderer::renderChatManager(const ChatManager *chatManager) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Vec4f fontColor; + Lang &lang = Lang::getInstance(); + + if (chatManager->getEditEnabled()) { + Vec4f color = Vec4f(0.0f, 0.0f, 0.0f, 0.6f); + string text = ""; + + if (chatManager->isInCustomInputMode() == true) { + text += lang.getString("CellHint"); + } else if (chatManager->getInMenu()) { + text += lang.getString("Chat"); + } else if (chatManager->getTeamMode()) { + text += lang.getString("Team"); + } else { + text += lang.getString("All"); + } + text += ": " + chatManager->getText() + "_"; + + if (game != NULL) { + fontColor = game->getGui()->getDisplay()->getColor(); + } else { + // white shadowed is default ( in the menu for example ) + fontColor = Vec4f(1.f, 1.f, 1.f, 0.0f); + } + + // render Background + int x = chatManager->getXPos(); + int y = chatManager->getYPos() - 6; + int h = 22; + int w = 830; + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(color.x, color.y, color.z, color.w); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x, y); + glVertex2i(x, y + h); + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glEnd(); + glPopAttrib(); + + if (renderText3DEnabled == true) { + renderTextShadow3D( + text, + chatManager->getFont3D(), + fontColor, + chatManager->getXPos(), chatManager->getYPos()); + } else { + renderTextShadow( + text, + chatManager->getFont(), + fontColor, + chatManager->getXPos(), chatManager->getYPos()); + } + } else { + if (chatManager->getInMenu()) { + string text = "\t\t\t\t\t>> " + lang.getString("PressEnterToChat") + " <<"; + fontColor = Vec4f(0.5f, 0.5f, 0.5f, 0.5f); + + if (renderText3DEnabled == true) { + renderTextShadow3D(text, chatManager->getFont3D(), fontColor, + chatManager->getXPos(), chatManager->getYPos()); + } else { + renderTextShadow(text, chatManager->getFont(), fontColor, + chatManager->getXPos(), chatManager->getYPos()); + } + } + } + } + + + void Renderer::renderPerformanceStats() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const Metrics &metrics = Metrics::getInstance(); + const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); + + char szBuf[200] = ""; + snprintf(szBuf, 200, "Frame: %d", game->getWorld()->getFrameCount() / 20); + string str = string(szBuf) + string("\n"); + + static time_t lastGamePerfCheck = time(NULL); + static string gamePerfStats = ""; + if (difftime((long int) time(NULL), lastGamePerfCheck) > 3) { + lastGamePerfCheck = time(NULL); + gamePerfStats = game->getGamePerformanceCounts(true); + } + + if (gamePerfStats != "") { + str += gamePerfStats + "\n"; + } + + if (renderText3DEnabled == true) { + renderTextShadow3D( + str, CoreData::getInstance().getDisplayFontSmall3D(), + fontColor, + 10, metrics.getVirtualH() - 180, false); + } else { + renderTextShadow( + str, CoreData::getInstance().getDisplayFontSmall(), + fontColor, + 10, metrics.getVirtualH() - 180, false); + } + } + + void Renderer::renderClock() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("InGameClock", "true") == false && + config.getBool("InGameLocalClock", "true") == false && + config.getBool("InGameFrameCounter", "false") == false) { + return; + } + + string str = ""; + const Metrics &metrics = Metrics::getInstance(); + const World *world = game->getWorld(); + const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); + + if (config.getBool("InGameClock", "true") == true) { + Lang &lang = Lang::getInstance(); + char szBuf[501] = ""; + + //int hours = world->getTimeFlow()->getTime(); + //int minutes = (world->getTimeFlow()->getTime() - hours) * 100 * 0.6; // scale 100 to 60 + //snprintf(szBuf,200,"%s %.2d:%.2d",lang.getString("GameTime","",true).c_str(),hours,minutes); + // string header2 = lang.getString("GameDurationTime","",true) + ": " + getTimeString(stats.getFramesToCalculatePlaytime()); + snprintf(szBuf, 500, "%s %s", lang.getString("GameDurationTime", "", true).c_str(), getTimeDuationString(world->getFrameCount(), GameConstants::updateFps).c_str()); + if (str != "") { + str += " "; + } + str += szBuf; + } + + if (config.getBool("InGameLocalClock", "true") == true) { + //time_t nowTime = time(NULL); + //struct tm *loctime = localtime(&nowTime); + struct tm loctime = threadsafe_localtime(systemtime_now()); + char szBuf2[100] = ""; + strftime(szBuf2, 100, "%H:%M", &loctime); + + Lang &lang = Lang::getInstance(); + char szBuf[200] = ""; + snprintf(szBuf, 200, "%s %s", lang.getString("LocalTime", "", true).c_str(), szBuf2); + if (str != "") { + str += " "; + } + str += szBuf; + } + + if (config.getBool("InGameFrameCounter", "false") == true) { + char szBuf[200] = ""; + snprintf(szBuf, 200, "Frame: %d", game->getWorld()->getFrameCount() / 20); + if (str != "") { + str += " "; + } + str += szBuf; + } + + //string str = szBuf; + + if (renderText3DEnabled == true) { + renderTextShadow3D( + str, CoreData::getInstance().getDisplayFontSmall3D(), + fontColor, + 10, metrics.getVirtualH() - 160, false); + } else { + renderTextShadow( + str, CoreData::getInstance().getDisplayFontSmall(), + fontColor, + 10, metrics.getVirtualH() - 160, false); + } + } + + void Renderer::renderResourceStatus() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const World *world = game->getWorld(); + Config &config = Config::getInstance(); + + if (world->getThisFactionIndex() < 0 || + world->getThisFactionIndex() >= world->getFactionCount()) { + return; + } + + const Faction *thisFaction = world->getFaction(world->getThisFactionIndex()); + + assertGl(); + glPushAttrib(GL_ENABLE_BIT); + + int rowsRendered = 0; + int resourceCountRendered = 0; + bool twoRessourceLines = false; + + bool sharedTeamUnits = game != NULL && game->getGui() != NULL + && game->isFlagType1BitEnabled(ft1_allow_shared_team_units) + == true; + bool sharedTeamResources = game != NULL && game->getGui() != NULL + && game->isFlagType1BitEnabled( + ft1_allow_shared_team_resources) == true; + + bool renderSharedTeamResources = false; + bool renderSharedTeamUnits = false; + bool renderLocalFactionResources = false; + + if (config.getBool("TwoLineTeamResourceRendering", "false") == true) { + if (sharedTeamResources == true || sharedTeamUnits == true) { + twoRessourceLines = true; + } + if (sharedTeamResources == true) { + renderSharedTeamResources = true; + renderSharedTeamUnits = true; + } else if (sharedTeamUnits == true) { + renderSharedTeamUnits = true; + renderLocalFactionResources = true; + } else { + renderLocalFactionResources = true; + } + } else { + if (sharedTeamResources == true) + renderSharedTeamResources = true; + else if (sharedTeamUnits == true) + renderSharedTeamUnits = true; + else + renderLocalFactionResources = true; + } + + if (renderSharedTeamResources == true) { + resourceCountRendered = 0; + for (int techTreeResourceTypeIndex = 0; + techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); + ++techTreeResourceTypeIndex) { + + const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); + + if (rt->getDisplayInHud() == false) { + continue; + } + + bool showResource = world->showResourceTypeForTeam(rt, thisFaction->getTeam()); + if (showResource == true) { + rowsRendered = renderResource(thisFaction, + false, twoRessourceLines, rt, 0, + resourceCountRendered); + } + } + if (resourceCountRendered > 0) { + rowsRendered++; + } + } + + if (renderLocalFactionResources == true) { + resourceCountRendered = 0; + + const Faction *factionForResourceView = thisFaction; + bool localFactionResourcesOnly = true; + + for (int techTreeResourceTypeIndex = 0; + techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); + ++techTreeResourceTypeIndex) { + const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); + if (rt->getDisplayInHud() == false) { + continue; + } + + //if any unit produces the resource + bool showResource; + if (twoRessourceLines) + showResource = world->showResourceTypeForTeam(rt, + factionForResourceView->getTeam()); + else + showResource = world->showResourceTypeForFaction(rt, + factionForResourceView); + if (showResource == true) { + renderResource(factionForResourceView, localFactionResourcesOnly, + twoRessourceLines, rt, rowsRendered, resourceCountRendered); + } + } + if (resourceCountRendered > 0) { + rowsRendered++; + } + } + + if (renderSharedTeamUnits == true) { + resourceCountRendered = 0; + + const Faction *factionForResourceView = thisFaction; + bool localFactionResourcesOnly = true; + + const Gui *gui = game->getGui(); + if (gui != NULL) { + const Selection *selection = gui->getSelection(); + if (selection != NULL && selection->getCount() > 0 && selection->getFrontUnit() != NULL) { + const Unit *selectedUnit = selection->getFrontUnit(); + if (selectedUnit != NULL && selectedUnit->getFaction()->isAlly(thisFaction) == true) { + factionForResourceView = selectedUnit->getFaction(); + } + } + } + + for (int techTreeResourceTypeIndex = 0; + techTreeResourceTypeIndex < world->getTechTree()->getResourceTypeCount(); + ++techTreeResourceTypeIndex) { + const ResourceType *rt = world->getTechTree()->getResourceType(techTreeResourceTypeIndex); + if (rt->getDisplayInHud() == false) { + continue; + } + + //if any unit produces the resource + bool showResource; + if (twoRessourceLines) + showResource = world->showResourceTypeForTeam(rt, + factionForResourceView->getTeam()); + else + showResource = world->showResourceTypeForFaction(rt, + factionForResourceView); + + if (showResource == true) { + renderResource(factionForResourceView, localFactionResourcesOnly, + twoRessourceLines, rt, rowsRendered, resourceCountRendered); + } + } + //if(resourceCountRendered > 0) { + // rowsRendered++; + //} + } + + glPopAttrib(); + + assertGl(); + } + + int Renderer::renderResource(const Faction *factionForResourceView, bool localFactionResourcesOnly, + bool twoResourceLines, const ResourceType *rt, int startRow, int &resourceCountRendered) { + + const Metrics &metrics = Metrics::getInstance(); + const int MAX_RESOURCES_PER_ROW = 6; + + int resourceRowHeigth = 30; + int resourceYStart = metrics.getVirtualH() - 30; + if (twoResourceLines) { + // we need to save some space + resourceYStart = metrics.getVirtualH() - 22; + resourceRowHeigth = 16; + } + + //draw resource status + if (localFactionResourcesOnly == true) { + Vec4f resourceFontColor = Vec4f(factionForResourceView->getTexture()->getPixmapConst()->getPixel3f(0, 0)); + int resourceCol = 0; + int resourceRow = startRow; + + int x = resourceCol * 100 + 190; + int y = resourceYStart - (resourceRowHeigth * resourceRow); + int h = 16; + int w = 8; + glColor3f(resourceFontColor.x, resourceFontColor.y, resourceFontColor.z); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x, y + h); + glVertex2i(x, y); + glVertex2i(x + w, y + h / 2); + glEnd(); + } + + const Resource *r = factionForResourceView->getResource(rt, localFactionResourcesOnly); + string str = intToStr(r->getAmount()); + + glEnable(GL_TEXTURE_2D); + + const Vec4f fontColor = game->getGui()->getDisplay()->getColor(); + Vec4f resourceFontColor = fontColor; + + bool isNegativeConsumableDisplayCycle = false; + if (rt->getClass() == rcConsumable) { + // Show in yellow/orange/red font if negative + if (r->getBalance() * 5 + r->getAmount() < 0) { + if (time(NULL) % 2 == 0) { + + isNegativeConsumableDisplayCycle = true; + if (r->getBalance() * 1 + r->getAmount() < 0) { + glColor3f(RED.x, RED.y, RED.z); + resourceFontColor = RED; + } else if (r->getBalance() * 3 + r->getAmount() < 0) { + glColor3f(ORANGE.x, ORANGE.y, ORANGE.z); + resourceFontColor = ORANGE; + } else if (r->getBalance() * 5 + r->getAmount() < 0) { + glColor3f(YELLOW.x, YELLOW.y, YELLOW.z); + resourceFontColor = YELLOW; + } + } + } + } + + if (isNegativeConsumableDisplayCycle == false) { + glColor3f(1.f, 1.f, 1.f); + } + + int resourceRow = startRow + (resourceCountRendered > 0 ? resourceCountRendered / MAX_RESOURCES_PER_ROW : 0); + int resourceCol = resourceCountRendered % MAX_RESOURCES_PER_ROW; + + renderQuad(resourceCol * 100 + 200, resourceYStart - (resourceRowHeigth * resourceRow), 16, 16, rt->getImage()); + + if (rt->getClass() != rcStatic) { + str += "/" + intToStr(factionForResourceView->getStoreAmount(rt, localFactionResourcesOnly)); + } + if (rt->getClass() == rcConsumable) { + str += "("; + if (r->getBalance() > 0) { + str += "+"; + } + str += intToStr(r->getBalance()) + ")"; + } + + glDisable(GL_TEXTURE_2D); + + if (renderText3DEnabled == true) { + renderTextShadow3D( + str, CoreData::getInstance().getDisplayFontSmall3D(), + resourceFontColor, + resourceCol * 100 + 220, resourceYStart - (resourceRowHeigth * resourceRow), false); + } else { + renderTextShadow( + str, CoreData::getInstance().getDisplayFontSmall(), + resourceFontColor, + resourceCol * 100 + 220, resourceYStart - (resourceRowHeigth * resourceRow), false); + } + ++resourceCountRendered; + + return resourceRow; + } + + void Renderer::renderSelectionQuad() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("RecordMode", "false") == true) { + return; + } + + const Gui *gui = game->getGui(); + const SelectionQuad *sq = gui->getSelectionQuad(); + + Vec2i down = sq->getPosDown(); + Vec2i up = sq->getPosUp(); + + if (gui->isSelecting()) { + glPushAttrib(GL_CURRENT_BIT | GL_LINE_BIT); + + Vec2i vertices[4]; + vertices[0] = Vec2i(down.x, down.y); + vertices[1] = Vec2i(up.x, down.y); + vertices[2] = Vec2i(up.x, up.y); + vertices[3] = Vec2i(down.x, up.y); + + glColor3f(0, 1, 0); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_INT, 0, &vertices[0]); + glDrawArrays(GL_LINE_LOOP, 0, 4); + glDisableClientState(GL_VERTEX_ARRAY); + + /* + glColor3f(0,1,0); + glBegin(GL_LINE_LOOP); + glVertex2i(down.x, down.y); + glVertex2i(up.x, down.y); + glVertex2i(up.x, up.y); + glVertex2i(down.x, up.y); + glEnd(); + */ + glPopAttrib(); + } + } + + Vec2i computeCenteredPos(const string &text, Font2D *font, int x, int y) { + if (font == NULL) { + //abort(); + throw megaglest_runtime_error("font == NULL (1) text = " + text); + } + const Metrics &metrics = Metrics::getInstance(); + FontMetrics *fontMetrics = font->getMetrics(); + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL (1) text = " + text); + } + + int virtualX = (fontMetrics->getTextWidth(text) > 0 ? static_cast(fontMetrics->getTextWidth(text) / 2.f) : 5); + int virtualY = (fontMetrics->getHeight(text) > 0 ? static_cast(fontMetrics->getHeight(text) / 2.f) : 5); + + Vec2i textPos( + x - metrics.toVirtualX(virtualX), + y - metrics.toVirtualY(virtualY)); + + //printf("text [%s] x = %d y = %d virtualX = %d virtualY = %d fontMetrics->getHeight() = %f\n",text.c_str(),x,y,virtualX,virtualY,fontMetrics->getHeight()); + + return textPos; + } + + Vec2i computeCenteredPos(const string &text, Font3D *font, int x, int y) { + if (font == NULL) { + throw megaglest_runtime_error("font == NULL (2) text = " + text); + } + const Metrics &metrics = Metrics::getInstance(); + FontMetrics *fontMetrics = font->getMetrics(); + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL (2) text = " + text); + } + + int virtualX = (fontMetrics->getTextWidth(text) > 0 ? static_cast(fontMetrics->getTextWidth(text) / 2.f) : 5); + int virtualY = (fontMetrics->getHeight(text) > 0 ? static_cast(fontMetrics->getHeight(text) / 2.f) : 5); + + Vec2i textPos( + x - metrics.toVirtualX(virtualX), + y - metrics.toVirtualY(virtualY)); + + return textPos; + } + + void Renderer::renderTextSurroundingBox(int x, int y, int w, int h, + int maxEditWidth, int maxEditRenderWidth) { + //glColor4fv(color.ptr()); + //glBegin(GL_QUADS); // Start drawing a quad primitive + + //printf("A w = %d maxEditWidth = %d maxEditRenderWidth = %d\n",w,maxEditWidth,maxEditRenderWidth); + if (maxEditWidth >= 0 || maxEditRenderWidth >= 0) { + //printf("B w = %d maxEditWidth = %d maxEditRenderWidth = %d\n",w,maxEditWidth,maxEditRenderWidth); + if (maxEditRenderWidth >= 0) { + w = maxEditRenderWidth; + } else { + w = maxEditWidth; + } + } + //printf("HI!!!\n"); + glPointSize(20.0f); + + int margin = 4; + //glBegin(GL_POINTS); // Start drawing a point primitive + glBegin(GL_LINE_LOOP); // Start drawing a line primitive + + glVertex3f(x, y + h, 0.0f); // The bottom left corner + glVertex3f(x, y - margin, 0.0f); // The top left corner + glVertex3f(x + w, y - margin, 0.0f); // The top right corner + glVertex3f(x + w, y + h, 0.0f); // The bottom right corner + glEnd(); + } + + void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, + float alpha, int x, int y, int w, int h, bool centeredW, bool centeredH, + bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); + + Vec2f pos = Vec2f(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + if (centeredW == true || centeredH == true) { + getCentered3DPos(text, font, pos, w, h, centeredW, centeredH); + } + + if (editModeEnabled) { + if (maxEditWidth >= 0 || maxEditRenderWidth >= 0) { + int useWidth = maxEditWidth; + string temp = ""; + for (int i = 0; i < useWidth; ++i) { + temp += DEFAULT_CHAR_FOR_WIDTH_CALC; + } + float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); + useWidth = (int) lineWidth; + + maxEditWidth = useWidth; + } + + renderTextSurroundingBox(pos.x, pos.y, w, h, maxEditWidth, maxEditRenderWidth); + } + glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y); + safeTextRender.end(); + + glDisable(GL_BLEND); + glPopAttrib(); + } + + void Renderer::renderText3D(const string &text, Font3D *font, float alpha, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); + + Vec2i pos = Vec2i(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y, centered); + //textRenderer3D->end(); + safeTextRender.end(); + + glDisable(GL_BLEND); + glPopAttrib(); + } + + void Renderer::renderText(const string &text, Font2D *font, float alpha, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(Vec4f(1.f, 1.f, 1.f, alpha).ptr()); + + Vec2i pos = centered ? computeCenteredPos(text, font, x, y) : Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer, font); + textRenderer->render(text, pos.x, pos.y); + safeTextRender.end(); + + glPopAttrib(); + } + + Vec2f Renderer::getCentered3DPos(const string &text, Font3D *font, Vec2f &pos, int w, int h, bool centeredW, bool centeredH) { + if (centeredW == true) { + if (font == NULL) { + //abort(); + throw megaglest_runtime_error("font == NULL (5) text = " + text); + } else if (font->getTextHandler() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "font->getTextHandler() == NULL(5) text = [%s] FontPtr = [%p]\n", text.c_str(), font); + throw megaglest_runtime_error(szBuf); + } + + float lineWidth = (font->getTextHandler()->Advance(text.c_str()) * ::Shared::Graphics::Font::scaleFontValue); + if (lineWidth < w) { + pos.x += ((w / 2.f) - (lineWidth / 2.f)); + } + } + + if (centeredH) { + if (font == NULL) { + throw megaglest_runtime_error("font == NULL (6) text = " + text); + } else if (font->getTextHandler() == NULL) { + throw megaglest_runtime_error("font->getTextHandler() == NULL (6) text = " + text); + } + + //const Metrics &metrics= Metrics::getInstance(); + //float lineHeight = (font->getTextHandler()->LineHeight(text.c_str()) * Font::scaleFontValue); + float lineHeight = (font->getTextHandler()->LineHeight(text.c_str()) * ::Shared::Graphics::Font::scaleFontValue); + //lineHeight=metrics.toVirtualY(lineHeight); + //lineHeight= lineHeight / (2.f + 0.2f * FontMetrics::DEFAULT_Y_OFFSET_FACTOR); + //pos.y += (h / 2.f) - (lineHeight / 2.f); + //pos.y += (h / 2.f) - (lineHeight); + //pos.y += (lineHeight / 2.f); // y starts at the middle of the render position, so only move up 1/2 the font height + + if (lineHeight < h) { + //printf("line %d, lineHeight [%f] h [%d] text [%s]\n",__LINE__,lineHeight,h,text.c_str()); + + //if(Font::forceFTGLFonts == true) { + // First go to top of bounding box + pos.y += (h - lineHeight); + pos.y -= ((h - lineHeight) / ::Shared::Graphics::Font::scaleFontValueCenterHFactor); + // } + // else { + // pos.y += (float)(((float)h) / 2.0); + // float heightGap = (float)(((float)h - lineHeight) / 2.0); + // pos.y -= heightGap; + // + // //printf("h = %d lineHeight = %f heightGap = %f\n",h,lineHeight,heightGap); + // + // // Now calculate till we get text to middle + // //pos.y -= (realHeight / 2); + // //pos.y += (lineHeight / 2); + // } + } else if (lineHeight > h) { + //printf("line %d, lineHeight [%f] h [%d] text [%s]\n",__LINE__,lineHeight,h,text.c_str()); + + pos.y += (std::ceil(lineHeight - h)); + } + } + return pos; + } + + void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, + const Vec3f &color, int x, int y, int w, int h, bool centeredW, + bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_CURRENT_BIT); + glColor3fv(color.ptr()); + + Vec2f pos = Vec2f(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + + if (centeredW == true || centeredH == true) { + getCentered3DPos(text, font, pos, w, h, centeredW, centeredH); + } + + if (editModeEnabled) { + if (maxEditWidth >= 0 || maxEditRenderWidth >= 0) { + int useWidth = maxEditWidth; + string temp = ""; + for (int i = 0; i < useWidth; ++i) { + temp += DEFAULT_CHAR_FOR_WIDTH_CALC; + } + float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); + useWidth = (int) lineWidth; + + maxEditWidth = useWidth; + } + + renderTextSurroundingBox(pos.x, pos.y, w, h, maxEditWidth, maxEditRenderWidth); + } + glColor3fv(color.ptr()); + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y); + safeTextRender.end(); + + glPopAttrib(); + } + + void Renderer::renderText3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_CURRENT_BIT); + glColor3fv(color.ptr()); + + Vec2i pos = Vec2i(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y, centered); + safeTextRender.end(); + + glPopAttrib(); + } + + void Renderer::renderText(const string &text, Font2D *font, const Vec3f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_CURRENT_BIT); + glColor3fv(color.ptr()); + + Vec2i pos = centered ? computeCenteredPos(text, font, x, y) : Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer, font); + textRenderer->render(text, pos.x, pos.y); + safeTextRender.end(); + + glPopAttrib(); + } + + void Renderer::renderTextBoundingBox3D(const string &text, Font3D *font, + const Vec4f &color, int x, int y, int w, int h, bool centeredW, + bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(color.ptr()); + + Vec2f pos = Vec2f(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + + if (centeredW == true || centeredH == true) { + getCentered3DPos(text, font, pos, w, h, centeredW, centeredH); + } + + if (editModeEnabled) { + if (maxEditWidth >= 0 || maxEditRenderWidth >= 0) { + int useWidth = maxEditWidth; + string temp = ""; + for (int i = 0; i < useWidth; ++i) { + temp += DEFAULT_CHAR_FOR_WIDTH_CALC; + } + float lineWidth = (font->getTextHandler()->Advance(temp.c_str()) * ::Shared::Graphics::Font::scaleFontValue); + useWidth = (int) lineWidth; + + maxEditWidth = useWidth; + } + + renderTextSurroundingBox(pos.x, pos.y, w, h, maxEditWidth, maxEditRenderWidth); + } + glColor4fv(color.ptr()); + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y); + safeTextRender.end(); + + glDisable(GL_BLEND); + glPopAttrib(); + } + + void Renderer::renderText3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(color.ptr()); + + Vec2i pos = Vec2i(x, y); + //Vec2i pos= centered? computeCenteredPos(text, font, x, y): Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + textRenderer3D->render(text, pos.x, pos.y, centered); + safeTextRender.end(); + + glDisable(GL_BLEND); + glPopAttrib(); + } + + void Renderer::renderText(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glColor4fv(color.ptr()); + + Vec2i pos = centered ? computeCenteredPos(text, font, x, y) : Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer, font); + textRenderer->render(text, pos.x, pos.y); + safeTextRender.end(); + + glPopAttrib(); + } + + void Renderer::renderTextShadow3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (font == NULL) { + throw megaglest_runtime_error("font == NULL (3) text = " + text); + } + + glPushAttrib(GL_CURRENT_BIT); + + Vec2i pos = centered ? computeCenteredPos(text, font, x, y) : Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer3D, font); + if (color.w < 0.5) { + glColor3f(0.0f, 0.0f, 0.0f); + + textRenderer3D->render(text, pos.x - 1.0f, pos.y - 1.0f); + } + glColor3f(color.x, color.y, color.z); + + textRenderer3D->render(text, pos.x, pos.y); + //textRenderer3D->end(); + safeTextRender.end(); + + glPopAttrib(); + } + + void Renderer::renderTextShadow(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (font == NULL) { + throw megaglest_runtime_error("font == NULL (4) text = " + text); + } + + glPushAttrib(GL_CURRENT_BIT); + + Vec2i pos = centered ? computeCenteredPos(text, font, x, y) : Vec2i(x, y); + + TextRendererSafeWrapper safeTextRender(textRenderer, font); + if (color.w < 0.5) { + glColor3f(0.0f, 0.0f, 0.0f); + + textRenderer->render(text, pos.x - 1.0f, pos.y - 1.0f); + } + glColor3f(color.x, color.y, color.z); + + textRenderer->render(text, pos.x, pos.y); + //textRenderer->end(); + safeTextRender.end(); + + glPopAttrib(); + } + + // ============= COMPONENTS ============================= + + void Renderer::renderLabel(GraphicLabel *label) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (label->getEditable() && label->getMaxEditRenderWidth() > 0) { + int x = label->getX(); + int y = label->getY(); + int h = label->getH(); + int w = label->getMaxEditRenderWidth(); + if (h > 0) { + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(0.2f, 0.2f, 0.2f, 0.6f*label->getFade()); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x, y); + glVertex2i(x, y + h); + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glEnd(); + glPopAttrib(); + } + } + + if (label->getRenderBackground()) { + int x = label->getX(); + int y = label->getY(); + int h = label->getH(); + int w = label->getW(); + if (label->getMaxEditRenderWidth() > 0) { + w = label->getMaxEditRenderWidth(); + } + Vec4f color = label->getBackgroundColor(); + if (h > 0) { + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(color.x, color.y, color.z, color.w*label->getFade()); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(x, y); + glVertex2i(x, y + h); + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glEnd(); + glPopAttrib(); + } + } + + if (label->getTexture() != NULL) { + int x = label->getX(); + int y = label->getY(); + int h = label->getH(); + int w = label->getW(); + if (h > 0) { + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(label->getTexture())->getHandle()); + glColor4f(1.0f, 1.0f, 1.0f, 1.0f*label->getFade()); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + glEnd(); + glDisable(GL_TEXTURE_2D); + glPopAttrib(); + } + } + Vec3f labelColor = label->getTextColor(); + Vec4f colorWithAlpha = Vec4f(labelColor.x, labelColor.y, labelColor.z, GraphicComponent::getFade()); + renderLabel(label, &colorWithAlpha); + } + + void Renderer::renderLabel(GraphicLabel *label, const Vec3f *color) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (color != NULL) { + Vec4f colorWithAlpha = Vec4f(*color); + colorWithAlpha.w = GraphicComponent::getFade(); + renderLabel(label, &colorWithAlpha); + } else { + Vec4f *colorWithAlpha = NULL; + renderLabel(label, colorWithAlpha); + } + } + + void Renderer::renderLabel(GraphicLabel *label, const Vec4f *color) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (label->getVisible() == false) { + return; + } + try { + glPushAttrib(GL_ENABLE_BIT); + glEnable(GL_BLEND); + + vector lines; + string renderTextString = (label->getTextNativeTranslation() != "" ? label->getTextNativeTranslation() : label->getText()); + if (label->getWordWrap() == true) { + Tokenize(renderTextString, lines, "\n"); + } else { + lines.push_back(renderTextString); + } + + for (unsigned int i = 0; i < lines.size(); ++i) { + Vec2i textPos; + int x = label->getX(); + int y = label->getY() - (i * label->getH()); + int h = label->getH(); + int w = label->getW(); + //if(label->getInstanceName() == "modDescrLabel") printf("~~~ lines.size() [%u] i = %d lines[i] [%s] y = %d\n",lines.size(),i,lines[i].c_str(),y); + + if (label->getCentered()) { + textPos = Vec2i(x + w / 2, y + h / 2); + } else { + textPos = Vec2i(x, y + h / 4); + } + + string renderTextStr = lines[i]; + if (label->getIsPassword() == true) { + if (renderTextStr != "") { + renderTextStr = "*****"; + } + } + + if (color != NULL) { + if (renderText3DEnabled == true) { + //renderText3D(lines[i], label->getFont3D(), (*color), textPos.x, textPos.y, label->getCentered()); + //printf("Text Render3D [%s] font3d [%p]\n",lines[i].c_str(),label->getFont3D()); + //printf("Label render C\n"); + + renderTextBoundingBox3D(renderTextStr, label->getFont3D(), (*color), + x, y, w, h, label->getCenteredW(), label->getCenteredH(), + label->getEditModeEnabled(), label->getMaxEditWidth(), + label->getMaxEditRenderWidth()); + } else { + //printf("Label render D\n"); + renderText(renderTextStr, label->getFont(), (*color), textPos.x, textPos.y, label->getCentered()); + } + } else { + if (renderText3DEnabled == true) { + //renderText3D(lines[i], label->getFont3D(), GraphicComponent::getFade(), textPos.x, textPos.y, label->getCentered()); + //printf("Text Render3D [%s] font3d [%p]\n",lines[i].c_str(),label->getFont3D()); + //printf("Label render E\n"); + renderTextBoundingBox3D(renderTextStr, label->getFont3D(), + GraphicComponent::getFade(), x, y, w, h, + label->getCenteredW(), label->getCenteredH(), + label->getEditModeEnabled(), label->getMaxEditWidth(), + label->getMaxEditRenderWidth()); + } else { + //printf("Label render F\n"); + renderText(renderTextStr, label->getFont(), GraphicComponent::getFade(), textPos.x, textPos.y, label->getCentered()); + } + } + } + glPopAttrib(); + } catch (const exception &e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s] For Control [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what(), label->getInstanceName().c_str()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + throw megaglest_runtime_error(szBuf); + } + + } + + void Renderer::renderButton(GraphicButton *button, const Vec4f *fontColorOverride, bool *lightedOverride) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (button->getVisible() == false) { + return; + } + + try { + + //char szBuf[8096]=""; + //snprintf(szBuf,8096,"In [%s::%s Line: %d]\n For Control container [%s] name [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,button->getContainerName().c_str(), button->getInstanceName().c_str()); + //printf(szBuf); + + int x = button->getX(); + int y = button->getY(); + int h = button->getH(); + int w = button->getW(); + + const Vec3f disabledTextColor = Vec3f(0.25f, 0.25f, 0.25f); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); + + //background + CoreData &coreData = CoreData::getInstance(); + Texture2D *backTexture = NULL; + + if (button->getUseCustomTexture() == true) { + backTexture = dynamic_cast(button->getCustomTexture()); + } else { + backTexture = w > 3 * h / 2 ? coreData.getButtonBigTexture() : coreData.getButtonSmallTexture(); + } + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + if (backTexture != NULL) { + glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); + } else { + glBindTexture(GL_TEXTURE_2D, 0); + } + + //button + Vec4f fontColor(GraphicComponent::getCustomTextColor()); + + if (fontColorOverride != NULL) { + fontColor = *fontColorOverride; + } else { + // white shadowed is default ( in the menu for example ) + fontColor.w = GraphicComponent::getFade(); + } + + //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); + Vec4f color = fontColor; + glColor4fv(color.ptr()); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + + glEnd(); + + glDisable(GL_TEXTURE_2D); + + //lighting + float anim = GraphicComponent::getAnim(); + if (anim > 0.5f) anim = 1.f - anim; + + bool renderLighted = (button->getLighted() && button->getEditable()); + if (lightedOverride != NULL) { + renderLighted = *lightedOverride; + } + if (renderLighted) { + const int lightSize = 0; + const Vec4f color1 = Vec4f(color.x, color.y, color.z, 0.1f + anim * 0.5f); + const Vec4f color2 = Vec4f(color.x, color.y, color.z, 0.3f + anim); + + glBegin(GL_TRIANGLE_FAN); + + glColor4fv(color2.ptr()); + glVertex2f(x + w / 2, y + h / 2); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); glEnd(); + } + + //Vec2i textPos= Vec2i(x + w / 2, y + h / 2); + + if (button->getEditable()) { + if (renderText3DEnabled == true) { + //renderText3D(button->getText(), button->getFont3D(), color,x + (w / 2), y + (h / 2), true); + renderTextBoundingBox3D(button->getText(), button->getFont3D(), + color, x, y, w, h, true, true, false, -1, -1); + } else { + renderText(button->getText(), button->getFont(), color, x + (w / 2), y + (h / 2), true); + } + } else { + if (renderText3DEnabled == true) { + //renderText3D(button->getText(), button->getFont3D(),disabledTextColor, + // x + (w / 2), y + (h / 2), true); + renderTextBoundingBox3D(button->getText(), button->getFont3D(), disabledTextColor, + x, y, w, h, true, true, false, -1, -1); + } else { + renderText(button->getText(), button->getFont(), disabledTextColor, + x + (w / 2), y + (h / 2), true); + } + } + + glPopAttrib(); + } catch (const exception &e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s] For Control container [%s] name [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what(), button->getContainerName().c_str(), button->getInstanceName().c_str()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + throw megaglest_runtime_error(szBuf); + } + } + + void Renderer::renderCheckBox(const GraphicCheckBox *box) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (box->getVisible() == false) { + return; + } + + int x = box->getX(); + int y = box->getY(); + int h = box->getH(); + int w = box->getW(); + + //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); + + //background + CoreData &coreData = CoreData::getInstance(); + Texture2D *backTexture = box->getValue() ? coreData.getCheckedCheckBoxTexture() : coreData.getCheckBoxTexture(); + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); + + //box + Vec4f fontColor; + //if(game!=NULL){ + // fontColor=game->getGui()->getDisplay()->getColor(); + // fontColor.w = GraphicComponent::getFade(); + //} + //else { + // white shadowed is default ( in the menu for example ) + fontColor = Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); + //} + + //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); + Vec4f color = fontColor; + glColor4fv(color.ptr()); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + + glEnd(); + + glDisable(GL_TEXTURE_2D); + + //lighting + float anim = GraphicComponent::getAnim(); + if (anim > 0.5f) { + anim = 1.f - anim; + } + + if (box->getLighted() && box->getEditable()) { + const int lightSize = 0; + const Vec4f color1 = Vec4f(color.x, color.y, color.z, 0.1f + anim * 0.5f); + const Vec4f color2 = Vec4f(color.x, color.y, color.z, 0.3f + anim); + + glBegin(GL_TRIANGLE_FAN); + + glColor4fv(color2.ptr()); + glVertex2f(x + w / 2, y + h / 2); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glEnd(); + } + + glPopAttrib(); + } + + + void Renderer::renderLine(const GraphicLine *line) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (line->getVisible() == false) { + return; + } + + int x = line->getX(); + int y = line->getY(); + int h = line->getH(); + int w = line->getW(); + + //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); + + //background + CoreData &coreData = CoreData::getInstance(); + Texture2D *backTexture = line->getHorizontal() ? coreData.getHorizontalLineTexture() : coreData.getVerticalLineTexture(); + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + + glEnd(); + + glDisable(GL_TEXTURE_2D); + glPopAttrib(); + } + + void Renderer::renderScrollBar(const GraphicScrollBar *sb) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (sb->getVisible() == false) { + return; + } + + int x = sb->getX(); + int y = sb->getY(); + int h = sb->getH(); + int w = sb->getW(); + + //const Vec3f disabledTextColor= Vec3f(0.25f,0.25f,0.25f); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT); + ///////////////////// + //background + //////////////////// + CoreData &coreData = CoreData::getInstance(); + Texture2D *backTexture = coreData.getHorizontalLineTexture(); + + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + glBindTexture(GL_TEXTURE_2D, static_cast(backTexture)->getHandle()); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + + glEnd(); + + //////////////////// + // selectBlock + //////////////////// + + x = sb->getX(); + y = sb->getY(); + h = sb->getH(); + w = sb->getW(); + + if (sb->getHorizontal()) { + x = x + sb->getVisibleCompPosStart(); + w = sb->getVisibleCompPosEnd() - sb->getVisibleCompPosStart(); + } else { + y = y + sb->getVisibleCompPosStart(); + h = sb->getVisibleCompPosEnd() - sb->getVisibleCompPosStart(); + } + + Texture2D *selectTexture = coreData.getButtonBigTexture(); + assert(selectTexture != NULL); + + glBindTexture(GL_TEXTURE_2D, static_cast(selectTexture)->getHandle()); + + //button + Vec4f fontColor; + //if(game!=NULL){ + // fontColor=game->getGui()->getDisplay()->getColor(); + // fontColor.w = GraphicComponent::getFade(); + //} + //else { + // white shadowed is default ( in the menu for example ) + fontColor = Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); + //} + + //Vec4f color= Vec4f(1.f, 1.f, 1.f, GraphicComponent::getFade()); + Vec4f color = fontColor; + glColor4fv(color.ptr()); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 0.f); + glVertex2f(x, y); + + glTexCoord2f(0.f, 1.f); + glVertex2f(x, y + h); + + glTexCoord2f(1.f, 0.f); + glVertex2f(x + w, y); + + glTexCoord2f(1.f, 1.f); + glVertex2f(x + w, y + h); + + glEnd(); + + glDisable(GL_TEXTURE_2D); + + //lighting + float anim = GraphicComponent::getAnim(); + if (anim > 0.5f) anim = 1.f - anim; + + if (sb->getLighted() && sb->getEditable()) { + const int lightSize = 0; + const Vec4f color1 = Vec4f(color.x, color.y, color.z, 0.1f + anim * 0.5f); + const Vec4f color2 = Vec4f(color.x, color.y, color.z, 0.3f + anim); + + glBegin(GL_TRIANGLE_FAN); + + glColor4fv(color2.ptr()); + glVertex2f(x + w / 2, y + h / 2); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glEnd(); + } + + glPopAttrib(); + } + + void Renderer::renderListBox(GraphicListBox *listBox) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (listBox->getVisible() == false) { + return; + } + //if(listBox->getLeftControlled()==true) + { + int x = listBox->getX(); + int y = listBox->getY(); + int h = listBox->getH(); + int w = listBox->getW(); + if (h > 0) { + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(0.0f, 0.0f, 0.0f, 0.6f*listBox->getFade()); + glBegin(GL_TRIANGLE_STRIP); - closed= true; + glVertex2i(x, y); + glVertex2i(x, y + h); + glVertex2i(x + w, y); + glVertex2i(x + w, y + h); + glEnd(); + glPopAttrib(); } - } - } - glEnd(); - } - - //restore - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderTeamColorCircle(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glDepthFunc(GL_ALWAYS); - glDisable(GL_STENCIL_TEST); - glDisable(GL_CULL_FACE); - glEnable(GL_BLEND); - glLineWidth(2.f); - - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - Vec3f currVec= unit->getCurrVectorFlat(); - Vec3f color=unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0); - glColor4f(color.x, color.y, color.z, 0.7f); - renderSelectionCircle(currVec, unit->getType()->getSize(), 0.8f, 0.05f); } - glPopAttrib(); - } -} + renderButton(listBox->getButton1()); + renderButton(listBox->getButton2()); -void Renderer::renderSpecialHighlightUnits(std::map unitHighlightList) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true || unitHighlightList.empty() == true) { - return; - } + glPushAttrib(GL_ENABLE_BIT); + glEnable(GL_BLEND); - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { + GraphicLabel label("ListBox_render_label", "label", false); + //label.setInstanceName("ListBox_render_label"); + if (listBox->getLeftControlled() == true) { + label.init(listBox->getX() + listBox->getButton1()->getW() + listBox->getButton2()->getW() + 2, listBox->getY(), listBox->getW(), listBox->getH(), false, listBox->getTextColor()); + } else { + label.init(listBox->getX(), listBox->getY(), listBox->getW(), listBox->getH(), true, listBox->getTextColor()); + } + label.setText(listBox->getText()); + label.setTextNativeTranslation(listBox->getTextNativeTranslation()); + label.setFont(listBox->getFont()); + label.setFont3D(listBox->getFont3D()); + renderLabel(&label); - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glDepthFunc(GL_ALWAYS); - glDisable(GL_STENCIL_TEST); - glDisable(GL_CULL_FACE); - glEnable(GL_BLEND); - glLineWidth(2.f); - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + //lighting - std::map::iterator iterFindSpecialUnit = unitHighlightList.find(unit->getId()); - if(iterFindSpecialUnit != unitHighlightList.end()) { - Vec3f color=unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0); - float radius = 1.0f; - float thickness = 0.1f; - float alpha = 0.65f; + bool renderLighted = (listBox->getLighted()); - HighlightSpecialUnitInfo &specialInfo = iterFindSpecialUnit->second; - if(specialInfo.color.x >= 0) { - color.x = specialInfo.color.x; - color.y = specialInfo.color.y; - color.z = specialInfo.color.z; - } - if(specialInfo.color.w >= 0) { - alpha = specialInfo.color.w; - } - if(specialInfo.radius > 0) { - radius = specialInfo.radius; - } - if(specialInfo.thickness > 0) { - thickness = specialInfo.thickness; - } - glColor4f(color.x, color.y, color.z, alpha); + if (renderLighted) { + float anim = GraphicComponent::getAnim(); + if (anim > 0.5f) anim = 1.f - anim; - Vec3f currVec= unit->getCurrVectorFlat(); - renderSelectionCircle(currVec, unit->getType()->getSize(), radius, thickness); - } + Vec3f color = listBox->getTextColor(); + int x = listBox->getX() + listBox->getButton1()->getW(); + int y = listBox->getY(); + int h = listBox->getH(); + int w = listBox->getW() - listBox->getButton1()->getW() - listBox->getButton2()->getW(); + + const int lightSize = 0; + const Vec4f color1 = Vec4f(color.x, color.y, color.z, 0.1f + anim * 0.5f); + const Vec4f color2 = Vec4f(color.x, color.y, color.z, 0.3f + anim); + + glBegin(GL_TRIANGLE_FAN); + + glColor4fv(color2.ptr()); + glVertex2f(x + w / 2, y + h / 2); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y - lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x + w + lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y + h + lightSize); + + glColor4fv(color1.ptr()); + glVertex2f(x - lightSize, y - lightSize); + + glEnd(); + } + + glPopAttrib(); } - glPopAttrib(); - } -} -void Renderer::renderTeamColorPlane(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } + void Renderer::renderMessageBox(GraphicMessageBox *messageBox) { + const int headerHeight = 25; + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false){ - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_LIGHTING); - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glEnable(GL_COLOR_MATERIAL); - const Texture2D *texture=CoreData::getInstance().getTeamColorTexture(); - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex){ - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if( unit->isAlive()){ - Vec3f currVec= unit->getCurrVectorFlat(); - renderTeamColorEffect(currVec,visibleUnitIndex,unit->getType()->getSize(), - unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0),texture); + try { + if (messageBox->getVisible() == false) { + return; + } + + if ((renderText3DEnabled == false && messageBox->getFont() == NULL) || + (renderText3DEnabled == true && messageBox->getFont3D() == NULL)) { + messageBox->setFont(CoreData::getInstance().getMenuFontNormal()); + messageBox->setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + } + + string wrappedText = messageBox->getText(); + if (messageBox->getAutoWordWrap() == true) { + if (renderText3DEnabled == false) { + wrappedText = messageBox->getFont()->getMetrics()->wordWrapText(wrappedText, messageBox->getW() * 0.90); + } else { + wrappedText = messageBox->getFont3D()->getMetrics()->wordWrapText(wrappedText, messageBox->getW() * 0.90); + } + } + + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(0.0f, 0.0f, 0.0f, 0.8f); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH() - headerHeight); + glVertex2i(messageBox->getX(), messageBox->getY()); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH() - headerHeight); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY()); + glEnd(); + + glColor4f(0.0f, 0.0f, 0.0f, 0.8f); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH()); + glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH() - headerHeight); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH()); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH() - headerHeight); + glEnd(); + + glBegin(GL_LINE_LOOP); + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(messageBox->getX(), messageBox->getY()); + + glColor4f(0.0f, 0.0f, 0.0f, 0.25f); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY()); + + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH()); + + glColor4f(0.25f, 0.25f, 0.25f, 0.25f); + glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH()); + glEnd(); + + glBegin(GL_LINE_STRIP); + glColor4f(1.0f, 1.0f, 1.0f, 0.25f); + glVertex2i(messageBox->getX(), messageBox->getY() + messageBox->getH() - headerHeight); + + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(messageBox->getX() + messageBox->getW(), messageBox->getY() + messageBox->getH() - headerHeight); + glEnd(); + + glPopAttrib(); + + + //buttons + for (int i = 0; i < messageBox->getButtonCount(); i++) { + + if ((renderText3DEnabled == false && messageBox->getButton(i)->getFont() == NULL) || + (renderText3DEnabled == true && messageBox->getButton(i)->getFont3D() == NULL)) { + messageBox->getButton(i)->setFont(CoreData::getInstance().getMenuFontNormal()); + messageBox->getButton(i)->setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + } + + renderButton(messageBox->getButton(i)); + } + + Vec4f fontColor; + //if(game!=NULL){ + // fontColor=game->getGui()->getDisplay()->getColor(); + //} + //else { + // white shadowed is default ( in the menu for example ) + fontColor = Vec4f(1.f, 1.f, 1.f, 1.0f); + //} + + if (renderText3DEnabled == true) { + //text + renderTextShadow3D( + wrappedText, messageBox->getFont3D(), fontColor, + messageBox->getX() + 15, messageBox->getY() + messageBox->getH() - headerHeight * 2, + false); + + renderTextShadow3D( + messageBox->getHeader(), messageBox->getFont3D(), fontColor, + messageBox->getX() + 15, messageBox->getY() + messageBox->getH() - headerHeight + 8, + false); + + } else { + //text + renderTextShadow( + wrappedText, messageBox->getFont(), fontColor, + messageBox->getX() + 15, messageBox->getY() + messageBox->getH() - headerHeight * 2, + false); + + renderTextShadow( + messageBox->getHeader(), messageBox->getFont(), fontColor, + messageBox->getX() + 15, messageBox->getY() + messageBox->getH() - headerHeight + 8, + false); + } + } catch (const exception &e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + + throw megaglest_runtime_error(szBuf); } } - glDisable(GL_COLOR_MATERIAL); - glPopAttrib(); - } -} -void Renderer::renderGhostModel(const UnitType *building, const Vec2i pos,CardinalDir facing, Vec4f *forceColor) { - //const UnitType *building= gui->getBuilding(); - //const Vec2i &pos= gui->getPosObjWorld(); + // ==================== complex rendering ==================== - //const Gui *gui= game->getGui(); - //const Mouse3d *mouse3d= gui->getMouse3d(); - const Map *map= game->getWorld()->getMap(); - if(map == NULL) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d map == NULL",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - - glPushMatrix(); - Vec3f pos3f= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); - - //selection building placement - float offset= building->getSize()/2.f-0.5f; - glTranslatef(pos3f.x+offset, pos3f.y, pos3f.z+offset); - - //choose color - Vec4f color; - if(forceColor != NULL) { - color = *forceColor; - } - else { - if(map->isFreeCells(pos, building->getSize(), fLand)) { - color= Vec4f(1.f, 1.f, 1.f, 0.5f); - } - else { -// Uint64 tc=game->getTickCount(); -// float red=0.49f+((tc%4*1.0f)/2); - color= Vec4f(1.0f, 0.f, 0.f, 0.5f); - } - } - - glColor4fv(color.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color.ptr()); - Model *buildingModel= building->getFirstStOfClass(scStop)->getAnimation(); - - if(facing != CardinalDir::NORTH) { - float rotateAmount = facing * 90.f; - if(rotateAmount > 0) { - glRotatef(rotateAmount, 0.f, 1.f, 0.f); - } - } - - buildingModel->updateInterpolationData(0.f, false); - modelRenderer->render(buildingModel); - - glPopMatrix(); -} - -void Renderer::renderUnits(bool airUnits, const int renderFps) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //Unit *unit=NULL; - //const World *world= game->getWorld(); - MeshCallbackTeamColor meshCallbackTeamColor; - - //assert - assertGl(); - - if(visibleFrameUnitList.empty() == false) { - visibleFrameUnitList.clear(); - //visibleFrameUnitListCameraKey = ""; - //if(visibleFrameUnitListCameraKey != game->getGameCamera()->getCameraMovementKey()) { - // worldToScreenPosCache.clear(); + //VisibleQuadContainerVBOCache * Renderer::GetSurfaceVBOs(SurfaceData *cellData) { + // std::map::iterator iterFind = mapSurfaceVBOCache.find(cellData->uniqueId); + // if(iterFind == mapSurfaceVBOCache.end()) { + // Vec2f *texCoords = &cellData->texCoords[0]; + // Vec2f *texCoordsSurface = &cellData->texCoordsSurface[0]; + // Vec3f *vertices = &cellData->vertices[0]; + // Vec3f *normals = &cellData->normals[0]; + // + // VisibleQuadContainerVBOCache vboCache; + // + // // Generate And Bind The Vertex Buffer + // glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOVertices ); // Get A Valid Name + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOVertices ); // Bind The Buffer + // // Load The Data + // glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f) * cellData->bufferCount, vertices, GL_STATIC_DRAW_ARB ); + // glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + // + // assertGl(); + // // Generate And Bind The Texture Coordinate Buffer + // glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOFowTexCoords ); // Get A Valid Name + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOFowTexCoords ); // Bind The Buffer + // // Load The Data + // glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f) * cellData->bufferCount, texCoords, GL_STATIC_DRAW_ARB ); + // glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + // + // assertGl(); + // // Generate And Bind The Texture Coordinate Buffer + // glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBOSurfaceTexCoords ); // Get A Valid Name + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBOSurfaceTexCoords ); // Bind The Buffer + // // Load The Data + // glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec2f) * cellData->bufferCount, texCoordsSurface, GL_STATIC_DRAW_ARB ); + // glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + // + // assertGl(); + // // Generate And Bind The Normal Buffer + // glGenBuffersARB( 1, (GLuint*)&vboCache.m_nVBONormals ); // Get A Valid Name + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache.m_nVBONormals ); // Bind The Buffer + // // Load The Data + // glBufferDataARB( GL_ARRAY_BUFFER_ARB, sizeof(Vec3f) * cellData->bufferCount, normals, GL_STATIC_DRAW_ARB ); + // glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); + // + // vboCache.hasBuiltVBOs = true; + // + // mapSurfaceVBOCache[cellData->uniqueId] = vboCache; + // + // // don't need the data in computer RAM anymore its in the GPU now + // cellData->texCoords.clear(); + // cellData->texCoordsSurface.clear(); + // cellData->vertices.clear(); + // cellData->normals.clear(); + // } + // + // return &mapSurfaceVBOCache[cellData->uniqueId]; //} - } - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - bool modelRenderStarted = false; - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + void Renderer::ReleaseSurfaceVBOs() { + for (std::map::iterator iterFind = mapSurfaceVBOCache.begin(); + iterFind != mapSurfaceVBOCache.end(); ++iterFind) { - if(( airUnits==false && unit->getType()->getField()==fAir) || ( airUnits==true && unit->getType()->getField()!=fAir)){ - continue; + VisibleQuadContainerVBOCache &item = iterFind->second; + if (item.hasBuiltVBOs == true) { + glDeleteBuffersARB(1, (GLuint*) &item.m_nVBOVertices); // Get A Valid Name + glDeleteBuffersARB(1, (GLuint*) &item.m_nVBOFowTexCoords); // Get A Valid Name + glDeleteBuffersARB(1, (GLuint*) &item.m_nVBOSurfaceTexCoords); // Get A Valid Name + glDeleteBuffersARB(1, (GLuint*) &item.m_nVBONormals); // Get A Valid Name + //glDeleteBuffersARB( 1, &item.m_nVBOIndexes ); // Get A Valid Name + } } - meshCallbackTeamColor.setTeamTexture(unit->getFaction()->getTexture()); - if(modelRenderStarted == false) { - modelRenderStarted = true; + mapSurfaceVBOCache.clear(); + } - glPushAttrib(GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); - glEnable(GL_COLOR_MATERIAL); + Renderer::MapRenderer::Layer::~Layer() { + if (vbo_vertices) glDeleteBuffersARB(1, &vbo_vertices); + if (vbo_normals) glDeleteBuffersARB(1, &vbo_normals); + if (vbo_fowTexCoords) glDeleteBuffersARB(1, &vbo_fowTexCoords); + if (vbo_surfTexCoords) glDeleteBuffersARB(1, &vbo_surfTexCoords); + if (vbo_indices) glDeleteBuffersARB(1, &vbo_indices); - if(!shadowsOffDueToMinRender) { - if(shadows == sShadowMapping) { + } + + template void _loadVBO(GLuint &vbo, std::vector buf, int target = GL_ARRAY_BUFFER_ARB) { + assert(buf.size()); + if (true /* vbo enabled? */) { + glGenBuffersARB(1, &vbo); + assert(vbo); + glBindBufferARB(target, vbo); + glBufferDataARB(target, sizeof(T)*buf.size(), &buf[0], GL_STATIC_DRAW_ARB); + glBindBufferARB(target, 0); + assertGl(); + buf.clear(); + } + } + + void Renderer::MapRenderer::Layer::load_vbos(bool vboEnabled) { + indexCount = (int) indices.size(); + if (vboEnabled) { + _loadVBO(vbo_vertices, vertices); + _loadVBO(vbo_normals, normals); + _loadVBO(vbo_fowTexCoords, fowTexCoords); + _loadVBO(vbo_surfTexCoords, surfTexCoords); + + _loadVBO(vbo_indices, indices, GL_ELEMENT_ARRAY_BUFFER_ARB); + } else { + vbo_vertices = 0; + vbo_normals = 0; + vbo_fowTexCoords = 0; + vbo_surfTexCoords = 0; + vbo_indices = 0; + } + } + + void Renderer::MapRenderer::loadVisibleLayers(float coordStep, VisibleQuadContainerCache &qCache) { + int totalCellCount = 0; + // we create a layer for each visible texture in the map + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + + totalCellCount++; + + SurfaceCell *tc00 = map->getSurfaceCell(pos.x, pos.y); + SurfaceCell *tc10 = map->getSurfaceCell(pos.x + 1, pos.y); + SurfaceCell *tc01 = map->getSurfaceCell(pos.x, pos.y + 1); + SurfaceCell *tc11 = map->getSurfaceCell(pos.x + 1, pos.y + 1); + + const Vec2f &surfCoord = tc00->getSurfTexCoord(); + + SurfaceCell *tc[4] = { + tc00, + tc10, + tc01, + tc11 + }; + int textureHandle = static_cast(tc[0]->getSurfaceTexture())->getHandle(); + string texturePath = static_cast(tc[0]->getSurfaceTexture())->getPath(); + //int32 textureCRC = CalculatePixelsCRC(static_cast(tc[0]->getSurfaceTexture())); + Layer* layer = NULL; + for (Layers::iterator it = layers.begin(); it != layers.end(); ++it) { + if ((*it)->textureHandle == textureHandle) { + //if((*it)->texturePath == texturePath) { + //if((*it)->textureCRC == textureCRC) { + layer = *it; + break; + } + } + if (!layer) { + layer = new Layer(textureHandle); + layer->texturePath = texturePath; + //layer->textureCRC = textureCRC; + layers.push_back(layer); + + //printf("Ading new unique texture [%s]\n",texturePath.c_str()); + } + // we'll be super-lazy and re-emit all four corners just because its easier + int index[4]; + int loopIndexes[4] = { 2,0,3,1 }; + for (int i = 0; i < 4; i++) { + index[i] = (int) layer->vertices.size(); + SurfaceCell *corner = tc[loopIndexes[i]]; + + layer->vertices.push_back(corner->getVertex()); + layer->normals.push_back(corner->getNormal()); + layer->fowTexCoords.push_back(corner->getFowTexCoord()); + } + + layer->surfTexCoords.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); + layer->surfTexCoords.push_back(Vec2f(surfCoord.x, surfCoord.y)); + layer->surfTexCoords.push_back(Vec2f(surfCoord.x + coordStep, surfCoord.y + coordStep)); + layer->surfTexCoords.push_back(Vec2f(surfCoord.x + coordStep, surfCoord.y)); + + // and make two triangles (no strip, we may be disjoint) + layer->indices.push_back(index[0]); + layer->indices.push_back(index[1]); + layer->indices.push_back(index[2]); + layer->indices.push_back(index[1]); + layer->indices.push_back(index[3]); + layer->indices.push_back(index[2]); + + } + // turn them into vbos (actually this method will just calc the index count) + for (Layers::iterator layer = layers.begin(); layer != layers.end(); ++layer) { + (*layer)->load_vbos(false); + } + + //printf("Total # of layers for this map = %d totalCellCount = %d overall render reduction ratio = %d times\n",layers.size(),totalCellCount,(totalCellCount / layers.size())); + } + + void Renderer::MapRenderer::load(float coordStep) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + int totalCellCount = 0; + // we create a layer for each texture in the map + for (int y = 0; y < map->getSurfaceH() - 1; y++) { + for (int x = 0; x < map->getSurfaceW() - 1; x++) { + totalCellCount++; + + SurfaceCell *tc[4] = { + map->getSurfaceCell(x,y), + map->getSurfaceCell(x + 1,y), + map->getSurfaceCell(x,y + 1), + map->getSurfaceCell(x + 1,y + 1) + }; + int textureHandle = static_cast(tc[0]->getSurfaceTexture())->getHandle(); + string texturePath = static_cast(tc[0]->getSurfaceTexture())->getPath(); + //int32 textureCRC = CalculatePixelsCRC(static_cast(tc[0]->getSurfaceTexture())); + Layer* layer = NULL; + for (Layers::iterator it = layers.begin(); it != layers.end(); ++it) { + if ((*it)->textureHandle == textureHandle) { + //if((*it)->texturePath == texturePath) { + //if((*it)->textureCRC == textureCRC) { + layer = *it; + break; + } + } + if (!layer) { + layer = new Layer(textureHandle); + layer->texturePath = texturePath; + //layer->textureCRC = textureCRC; + layers.push_back(layer); + + //printf("Ading new unique texture [%s]\n",texturePath.c_str()); + } + // we'll be super-lazy and re-emit all four corners just because its easier + int index[4]; + int loopIndexes[4] = { 2,0,3,1 }; + for (int i = 0; i < 4; i++) { + index[i] = (int) layer->vertices.size(); + SurfaceCell *corner = tc[loopIndexes[i]]; + layer->vertices.push_back(corner->getVertex()); + layer->normals.push_back(corner->getNormal()); + } + + // the texture coords are all on the current texture obviously + layer->fowTexCoords.push_back(tc[loopIndexes[0]]->getFowTexCoord()); + layer->fowTexCoords.push_back(tc[loopIndexes[1]]->getFowTexCoord()); + layer->fowTexCoords.push_back(tc[loopIndexes[2]]->getFowTexCoord()); + layer->fowTexCoords.push_back(tc[loopIndexes[3]]->getFowTexCoord()); + + layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord() + Vec2f(0, coordStep)); + layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord() + Vec2f(0, 0)); + layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord() + Vec2f(coordStep, coordStep)); + layer->surfTexCoords.push_back(tc[0]->getSurfTexCoord() + Vec2f(coordStep, 0)); + + layer->cellToIndicesMap[Vec2i(x, y)] = (int) layer->indices.size(); + + // and make two triangles (no strip, we may be disjoint) + layer->indices.push_back(index[0]); + layer->indices.push_back(index[1]); + layer->indices.push_back(index[2]); + layer->indices.push_back(index[1]); + layer->indices.push_back(index[3]); + layer->indices.push_back(index[2]); + } + } + // turn them into vbos + for (Layers::iterator layer = layers.begin(); layer != layers.end(); ++layer) { + (*layer)->load_vbos(true); + } + + //printf("Total # of layers for this map = %d totalCellCount = %d overall render reduction ratio = %d times\n",layers.size(),totalCellCount,(totalCellCount / layers.size())); + } + + template void* _bindVBO(GLuint vbo, std::vector &buf, int target = GL_ARRAY_BUFFER_ARB) { + void* result = NULL; + if (vbo) { + glBindBuffer(target, vbo); + } else { + result = &buf[0]; + } + return result; + } + + void Renderer::MapRenderer::Layer::renderVisibleLayer() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + glClientActiveTexture(Renderer::fowTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, &fowTexCoords[0]); + + glBindTexture(GL_TEXTURE_2D, textureHandle); + glClientActiveTexture(Renderer::baseTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, &surfTexCoords[0]); + + glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); + glNormalPointer(GL_FLOAT, 0, &normals[0]); + + //glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size()); + //unsigned short faceIndices[4] = {0, 1, 2, 3}; + //glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, &faceIndices[0]); + glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, &indices[0]); + + glClientActiveTexture(Renderer::fowTexUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(Renderer::baseTexUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + } + + void Renderer::MapRenderer::Layer::render(VisibleQuadContainerCache &qCache) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + // const bool renderOnlyVisibleQuad = true; + // + // if(renderOnlyVisibleQuad == true) { + vector > rowsToRender; + + if (rowsToRenderCache.find(qCache.lastVisibleQuad) != rowsToRenderCache.end()) { + rowsToRender = rowsToRenderCache[qCache.lastVisibleQuad]; + } else { + int startIndex = -1; + int lastValidIndex = -1; + + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + + if (cellToIndicesMap.find(pos) != cellToIndicesMap.end()) { + //printf("Layer Render, visibleindex = %d pos [%s] cellToIndicesMap[pos] = %d lastValidIndex = %d\n",visibleIndex,pos.getString().c_str(),cellToIndicesMap[pos],lastValidIndex); + + if (startIndex < 0 || cellToIndicesMap[pos] == lastValidIndex + 6) { + lastValidIndex = cellToIndicesMap[pos]; + if (startIndex < 0) { + startIndex = lastValidIndex; + } + } else if (startIndex >= 0) { + rowsToRender.push_back(make_pair(startIndex, lastValidIndex)); + + lastValidIndex = cellToIndicesMap[pos]; + startIndex = lastValidIndex; + } + } + } + if (startIndex >= 0) { + rowsToRender.push_back(make_pair(startIndex, lastValidIndex)); + } + + rowsToRenderCache[qCache.lastVisibleQuad] = rowsToRender; + } + + if (rowsToRender.empty() == false) { + //printf("Layer has %d rows in visible quad, visible quad has %d cells\n",rowsToRender.size(),qCache.visibleScaledCellList.size()); + + glVertexPointer(3, GL_FLOAT, 0, _bindVBO(vbo_vertices, vertices)); + glNormalPointer(GL_FLOAT, 0, _bindVBO(vbo_normals, normals)); + + glClientActiveTexture(Renderer::fowTexUnit); + glTexCoordPointer(2, GL_FLOAT, 0, _bindVBO(vbo_fowTexCoords, fowTexCoords)); + + glClientActiveTexture(Renderer::baseTexUnit); + glBindTexture(GL_TEXTURE_2D, textureHandle); + glTexCoordPointer(2, GL_FLOAT, 0, _bindVBO(vbo_surfTexCoords, surfTexCoords)); + + for (unsigned int i = 0; i < rowsToRender.size(); ++i) { + //glDrawElements(GL_TRIANGLES,indexCount,GL_UNSIGNED_INT,_bindVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB)); + glDrawRangeElements(GL_TRIANGLES, rowsToRender[i].first, rowsToRender[i].second, indexCount, GL_UNSIGNED_INT, _bindVBO(vbo_indices, indices, GL_ELEMENT_ARRAY_BUFFER_ARB)); + } + } + // } + // else { + // glVertexPointer(3,GL_FLOAT,0,_bindVBO(vbo_vertices,vertices)); + // glNormalPointer(GL_FLOAT,0,_bindVBO(vbo_normals,normals)); + // + // glClientActiveTexture(Renderer::fowTexUnit); + // glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_fowTexCoords,fowTexCoords)); + // + // glClientActiveTexture(Renderer::baseTexUnit); + // glBindTexture(GL_TEXTURE_2D,textureHandle); + // glTexCoordPointer(2,GL_FLOAT,0,_bindVBO(vbo_surfTexCoords,surfTexCoords)); + // + // glDrawElements(GL_TRIANGLES,indexCount,GL_UNSIGNED_INT,_bindVBO(vbo_indices,indices,GL_ELEMENT_ARRAY_BUFFER_ARB)); + // } + } + + void Renderer::MapRenderer::renderVisibleLayers(const Map* map, float coordStep, VisibleQuadContainerCache &qCache) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (map != this->map) { + //printf("New Map loading\n"); + destroy(); // clear any previous map data + this->map = map; + loadVisibleLayers(coordStep, qCache); + } else if (lastVisibleQuad != qCache.lastVisibleQuad) { + //printf("New Visible Quad loading\n"); + destroy(); // clear any previous map data + this->map = map; + loadVisibleLayers(coordStep, qCache); + } + + lastVisibleQuad = qCache.lastVisibleQuad; + //printf("About to render %d layers\n",layers.size()); + + glClientActiveTexture(fowTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(baseTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + for (Layers::iterator layer = layers.begin(); layer != layers.end(); ++layer) + (*layer)->renderVisibleLayer(); + glDisableClientState(GL_VERTEX_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + glDisableClientState(GL_NORMAL_ARRAY); + glClientActiveTexture(fowTexUnit); + glBindTexture(GL_TEXTURE_2D, 0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(baseTexUnit); + glBindTexture(GL_TEXTURE_2D, 0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + assertGl(); + } + + void Renderer::MapRenderer::render(const Map* map, float coordStep, VisibleQuadContainerCache &qCache) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (map != this->map) { + destroy(); // clear any previous map data + this->map = map; + load(coordStep); + } + + //printf("About to render %d layers\n",layers.size()); + + glClientActiveTexture(fowTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(baseTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + for (Layers::iterator layer = layers.begin(); layer != layers.end(); ++layer) + (*layer)->render(qCache); + glDisableClientState(GL_VERTEX_ARRAY); + glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); + glDisableClientState(GL_NORMAL_ARRAY); + glClientActiveTexture(fowTexUnit); + glBindTexture(GL_TEXTURE_2D, 0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(baseTexUnit); + glBindTexture(GL_TEXTURE_2D, 0); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + assertGl(); + } + + void Renderer::MapRenderer::destroy() { + while (layers.empty() == false) { + delete layers.back(); + layers.pop_back(); + } + map = NULL; + } + + void Renderer::renderSurface(const int renderFps) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + IF_DEBUG_EDITION( + if (getDebugRenderer().willRenderSurface()) { + getDebugRenderer().renderSurface(visibleQuad / Map::cellScale); + } else { + ) + assertGl(); + + const World *world = game->getWorld(); + const Map *map = world->getMap(); + float coordStep = world->getTileset()->getSurfaceAtlas()->getCoordStep(); + + const Texture2D *fowTex = world->getMinimap()->getFowTexture(); + if (fowTex == NULL) { + return; + } + + glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_FOG_BIT | GL_TEXTURE_BIT); + + glEnable(GL_BLEND); + glEnable(GL_COLOR_MATERIAL); + glDisable(GL_ALPHA_TEST); + glEnable(GL_CULL_FACE); + + //fog of war tex unit + glActiveTexture(fowTexUnit); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + + glTexSubImage2D( + GL_TEXTURE_2D, 0, 0, 0, + fowTex->getPixmapConst()->getW(), fowTex->getPixmapConst()->getH(), + GL_ALPHA, GL_UNSIGNED_BYTE, fowTex->getPixmapConst()->getPixels()); + + if (shadowsOffDueToMinRender == false) { + //shadow texture + if (shadows == sProjected || shadows == sShadowMapping) { + glActiveTexture(shadowTexUnit); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + + static_cast(modelRenderer)->setDuplicateTexCoords(true); + enableProjectiveTexturing(); + } + } + + const Rect2i mapBounds(0, 0, map->getSurfaceW() - 1, map->getSurfaceH() - 1); + + glActiveTexture(baseTexUnit); + + VisibleQuadContainerCache &qCache = getQuadCache(); + + bool useVBORendering = getVBOSupported(); + if (useVBORendering == true) { + VisibleQuadContainerCache &qCache = getQuadCache(); + //mapRenderer.render(map,coordStep,qCache); + mapRenderer.renderVisibleLayers(map, coordStep, qCache); + } else if (qCache.visibleScaledCellList.empty() == false) { + + int lastTex = -1; + //int currTex=-1; + + //Quad2i snapshotOfvisibleQuad = visibleQuad; + + //bool useVertexArrayRendering = getVBOSupported(); + //bool useVertexArrayRendering = false; + //if(useVertexArrayRendering == false) { + //printf("\LEGACY qCache.visibleScaledCellList.size() = %d \n",qCache.visibleScaledCellList.size()); + + Vec2f texCoords[4]; + Vec2f texCoordsSurface[4]; + Vec3f vertices[4]; + Vec3f normals[4]; + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + + std::map uniqueVisibleTextures; + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + SurfaceCell *tc00 = map->getSurfaceCell(pos.x, pos.y); + int cellTex = static_cast(tc00->getSurfaceTexture())->getHandle(); + + uniqueVisibleTextures[cellTex]++; + } + + //printf("Current renders = %d possible = %d\n",qCache.visibleScaledCellList.size(),uniqueVisibleTextures.size()); + + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleScaledCellList.size(); ++visibleIndex) { + Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + + SurfaceCell *tc00 = map->getSurfaceCell(pos.x, pos.y); + SurfaceCell *tc10 = map->getSurfaceCell(pos.x + 1, pos.y); + SurfaceCell *tc01 = map->getSurfaceCell(pos.x, pos.y + 1); + SurfaceCell *tc11 = map->getSurfaceCell(pos.x + 1, pos.y + 1); + + if (tc00 == NULL) { + throw megaglest_runtime_error("tc00 == NULL"); + } + if (tc10 == NULL) { + throw megaglest_runtime_error("tc10 == NULL"); + } + if (tc01 == NULL) { + throw megaglest_runtime_error("tc01 == NULL"); + } + if (tc11 == NULL) { + throw megaglest_runtime_error("tc11 == NULL"); + } + + triangleCount += 2; + pointCount += 4; + + //set texture + if (tc00->getSurfaceTexture() == NULL) { + throw megaglest_runtime_error("tc00->getSurfaceTexture() == NULL"); + } + int currTex = static_cast(tc00->getSurfaceTexture())->getHandle(); + if (currTex != lastTex) { + lastTex = currTex; + //glBindTexture(GL_TEXTURE_2D, lastTex); + } + + const Vec2f &surfCoord = tc00->getSurfTexCoord(); + + texCoords[0] = tc01->getFowTexCoord(); + texCoordsSurface[0] = Vec2f(surfCoord.x, surfCoord.y + coordStep); + vertices[0] = tc01->getVertex(); + normals[0] = tc01->getNormal();; + + texCoords[1] = tc00->getFowTexCoord(); + texCoordsSurface[1] = Vec2f(surfCoord.x, surfCoord.y); + vertices[1] = tc00->getVertex(); + normals[1] = tc00->getNormal(); + + texCoords[2] = tc11->getFowTexCoord(); + texCoordsSurface[2] = Vec2f(surfCoord.x + coordStep, surfCoord.y + coordStep); + vertices[2] = tc11->getVertex(); + normals[2] = tc11->getNormal(); + + texCoords[3] = tc10->getFowTexCoord(); + texCoordsSurface[3] = Vec2f(surfCoord.x + coordStep, surfCoord.y); + vertices[3] = tc10->getVertex(); + normals[3] = tc10->getNormal(); + + //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + glClientActiveTexture(fowTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, &texCoords[0]); + + glBindTexture(GL_TEXTURE_2D, lastTex); + glClientActiveTexture(baseTexUnit); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + glTexCoordPointer(2, GL_FLOAT, 0, &texCoordsSurface[0]); + + glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); + glNormalPointer(GL_FLOAT, 0, &normals[0]); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + //unsigned short faceIndices[4] = {0, 1, 2, 3}; + //glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, &faceIndices[0]); + + glClientActiveTexture(fowTexUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glClientActiveTexture(baseTexUnit); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + + /* + glBegin(GL_TRIANGLE_STRIP); + + //draw quad using immediate mode + glMultiTexCoord2fv(fowTexUnit, tc01->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y + coordStep); + glNormal3fv(tc01->getNormal().ptr()); + glVertex3fv(tc01->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc00->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x, surfCoord.y); + glNormal3fv(tc00->getNormal().ptr()); + glVertex3fv(tc00->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc11->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x+coordStep, surfCoord.y+coordStep); + glNormal3fv(tc11->getNormal().ptr()); + glVertex3fv(tc11->getVertex().ptr()); + + glMultiTexCoord2fv(fowTexUnit, tc10->getFowTexCoord().ptr()); + glMultiTexCoord2f(baseTexUnit, surfCoord.x + coordStep, surfCoord.y); + glNormal3fv(tc10->getNormal().ptr()); + glVertex3fv(tc10->getVertex().ptr()); + + glEnd(); + */ + } + + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + // } + // else { + // const bool useVBOs = false; + // const bool useSurfaceCache = false; + // + // std::vector surfaceData; + // bool recalcSurface = false; + // + // if(useSurfaceCache == true) { + // std::map > >::iterator iterFind = mapSurfaceData.find(snapshotOfvisibleQuad.getString()); + // if(iterFind == mapSurfaceData.end()) { + // recalcSurface = true; + // //printf("#1 Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); + // } + ///* + // else if(iterFind->second.first.getMillis() >= 250) { + // recalcSurface = true; + // mapSurfaceData.erase(snapshotOfvisibleQuad.getString()); + // //printf("#2 RE-Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); + // } + //*/ + // } + // else { + // recalcSurface = true; + // } + // + // if(recalcSurface == true) { + // //printf("Calculating surface for Rendering using VA's [%s]\n",snapshotOfvisibleQuad.getString().c_str()); + // + // std::vector *surface = &surfaceData; + // if(useSurfaceCache == true) { + // std::pair > &surfaceCacheEntity = mapSurfaceData[snapshotOfvisibleQuad.getString()]; + // surface = &surfaceCacheEntity.second; + // //surface.reserve(qCache.visibleScaledCellList.size()); + // } + // surface->reserve(qCache.visibleScaledCellList.size()); + // + // int lastSurfaceDataIndex = -1; + // for(int visibleIndex = 0; + // visibleIndex < (int)qCache.visibleScaledCellList.size(); ++visibleIndex) { + // Vec2i &pos = qCache.visibleScaledCellList[visibleIndex]; + // + // SurfaceCell *tc00= map->getSurfaceCell(pos.x, pos.y); + // SurfaceCell *tc10= map->getSurfaceCell(pos.x+1, pos.y); + // SurfaceCell *tc01= map->getSurfaceCell(pos.x, pos.y+1); + // SurfaceCell *tc11= map->getSurfaceCell(pos.x+1, pos.y+1); + // + // if(tc00 == NULL) { + // throw megaglest_runtime_error("tc00 == NULL"); + // } + // if(tc10 == NULL) { + // throw megaglest_runtime_error("tc10 == NULL"); + // } + // if(tc01 == NULL) { + // throw megaglest_runtime_error("tc01 == NULL"); + // } + // if(tc11 == NULL) { + // throw megaglest_runtime_error("tc11 == NULL"); + // } + // + // triangleCount+= 2; + // pointCount+= 4; + // + // //set texture + // if(tc00->getSurfaceTexture() == NULL) { + // throw megaglest_runtime_error("tc00->getSurfaceTexture() == NULL"); + // } + // + // int surfaceDataIndex = -1; + // currTex= static_cast(tc00->getSurfaceTexture())->getHandle(); + // if(currTex != lastTex) { + // lastTex = currTex; + // } + // else { + // surfaceDataIndex = lastSurfaceDataIndex; + // } + // + // if(surfaceDataIndex < 0) { + // SurfaceData newData; + // newData.uniqueId = SurfaceData::nextUniqueId; + // SurfaceData::nextUniqueId++; + // newData.bufferCount=0; + // newData.textureHandle = currTex; + // surface->push_back(newData); + // + // surfaceDataIndex = (int)surface->size() - 1; + // } + // + // lastSurfaceDataIndex = surfaceDataIndex; + // + // SurfaceData *cellData = &(*surface)[surfaceDataIndex]; + // + // const Vec2f &surfCoord= tc00->getSurfTexCoord(); + // + // cellData->texCoords.push_back(tc01->getFowTexCoord()); + // cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y + coordStep)); + // cellData->vertices.push_back(tc01->getVertex()); + // cellData->normals.push_back(tc01->getNormal()); + // cellData->bufferCount++; + // + // cellData->texCoords.push_back(tc00->getFowTexCoord()); + // cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x, surfCoord.y)); + // cellData->vertices.push_back(tc00->getVertex()); + // cellData->normals.push_back(tc00->getNormal()); + // cellData->bufferCount++; + // + // cellData->texCoords.push_back(tc11->getFowTexCoord()); + // cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y+coordStep)); + // cellData->vertices.push_back(tc11->getVertex()); + // cellData->normals.push_back(tc11->getNormal()); + // cellData->bufferCount++; + // + // cellData->texCoords.push_back(tc10->getFowTexCoord()); + // cellData->texCoordsSurface.push_back(Vec2f(surfCoord.x+coordStep, surfCoord.y)); + // cellData->vertices.push_back(tc10->getVertex()); + // cellData->normals.push_back(tc10->getNormal()); + // cellData->bufferCount++; + // } + // } + // + // std::vector *surface = &surfaceData; + // if(useSurfaceCache == true) { + // std::pair > &surfaceCacheEntity = mapSurfaceData[snapshotOfvisibleQuad.getString()]; + // surface = &surfaceCacheEntity.second; + // } + // + // glEnableClientState(GL_VERTEX_ARRAY); + // glEnableClientState(GL_NORMAL_ARRAY); + // + // for(int i = 0; i < (int)surface->size(); ++i) { + // SurfaceData &data = (*surface)[i]; + // + // if(useVBOs == true) { + // VisibleQuadContainerVBOCache *vboCache = GetSurfaceVBOs(&data); + // + // //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + // glClientActiveTexture(fowTexUnit); + // glEnableClientState(GL_TEXTURE_COORD_ARRAY); + // + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOFowTexCoords); + // glTexCoordPointer(2, GL_FLOAT, 0,(char *) NULL); + // + // glBindTexture(GL_TEXTURE_2D, data.textureHandle); + // glClientActiveTexture(baseTexUnit); + // glEnableClientState(GL_TEXTURE_COORD_ARRAY); + // + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOSurfaceTexCoords); + // glTexCoordPointer(2, GL_FLOAT, 0, (char *) NULL); + // + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBOVertices); + // glVertexPointer(3, GL_FLOAT, 0, (char *) NULL); + // + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, vboCache->m_nVBONormals); + // glNormalPointer(GL_FLOAT, 0, (char *) NULL); + // + // glDrawArrays(GL_TRIANGLE_STRIP, 0, data.bufferCount); + // + // glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); + // + // glClientActiveTexture(fowTexUnit); + // glDisableClientState(GL_TEXTURE_COORD_ARRAY); + // glClientActiveTexture(baseTexUnit); + // glDisableClientState(GL_TEXTURE_COORD_ARRAY); + // + // } + // else { + // Vec2f *texCoords = &data.texCoords[0]; + // Vec2f *texCoordsSurface = &data.texCoordsSurface[0]; + // Vec3f *vertices = &data.vertices[0]; + // Vec3f *normals = &data.normals[0]; + // + // //glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + // glClientActiveTexture(fowTexUnit); + // glEnableClientState(GL_TEXTURE_COORD_ARRAY); + // glTexCoordPointer(2, GL_FLOAT, 0,texCoords); + // + // glBindTexture(GL_TEXTURE_2D, data.textureHandle); + // glClientActiveTexture(baseTexUnit); + // glEnableClientState(GL_TEXTURE_COORD_ARRAY); + // glTexCoordPointer(2, GL_FLOAT, 0, texCoordsSurface); + // + // glVertexPointer(3, GL_FLOAT, 0, vertices); + // glNormalPointer(GL_FLOAT, 0, normals); + // + // glDrawArrays(GL_TRIANGLE_STRIP, 0, data.bufferCount); + // + // glClientActiveTexture(fowTexUnit); + // glDisableClientState(GL_TEXTURE_COORD_ARRAY); + // glClientActiveTexture(baseTexUnit); + // glDisableClientState(GL_TEXTURE_COORD_ARRAY); + // } + // } + // + // glDisableClientState(GL_NORMAL_ARRAY); + // glDisableClientState(GL_VERTEX_ARRAY); + // + // //printf("Surface Render before [%d] after [%d]\n",qCache.visibleScaledCellList.size(),surface.size()); + // } + } + + //Restore + static_cast(modelRenderer)->setDuplicateTexCoords(false); + + glDisable(GL_TEXTURE_2D); + + glPopAttrib(); + + //assert + GLenum glresult = glGetError(); //remove when first mtex problem solved + if (glresult) { + assertGl(); + } + assertGl(); + + IF_DEBUG_EDITION( + } // end else, if not renderering debug textures instead of regular terrain + getDebugRenderer().renderEffects(visibleQuad / Map::cellScale); + ) + } + + void Renderer::renderObjects(const int renderFps) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const World *world = game->getWorld(); + //const Map *map= world->getMap(); + + Config &config = Config::getInstance(); + int tilesetObjectsToAnimate = config.getInt("AnimatedTilesetObjects", "-1"); + + assertGl(); + + const Texture2D *fowTex = world->getMinimap()->getFowTexture(); + const Pixmap2D *fowTexPixmap = fowTex->getPixmapConst(); + Vec3f baseFogColor = world->getTileset()->getFogColor() * world->getTimeFlow()->computeLightColor(); + + bool modelRenderStarted = false; + + VisibleQuadContainerCache &qCache = getQuadCache(); + + // for(int visibleIndex = 0; + // visibleIndex < qCache.visibleObjectList.size(); ++visibleIndex) { + // render from last to first object so animated objects which are on bottom of screen are + // rendered first which looks better for limited number of animated tileset objects + for (int visibleIndex = (int) qCache.visibleObjectList.size() - 1; + visibleIndex >= 0; --visibleIndex) { + Object *o = qCache.visibleObjectList[visibleIndex]; + + Model *objModel = o->getModelPtr(); + //objModel->updateInterpolationData(o->getAnimProgress(), true); + const Vec3f v = o->getConstPos(); + + if (modelRenderStarted == false) { + modelRenderStarted = true; + + glPushAttrib(GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); + + if (shadowsOffDueToMinRender == false && + shadows == sShadowMapping) { glActiveTexture(shadowTexUnit); glEnable(GL_TEXTURE_2D); @@ -5353,4770 +4856,5139 @@ void Renderer::renderUnits(bool airUnits, const int renderFps) { static_cast(modelRenderer)->setDuplicateTexCoords(true); enableProjectiveTexturing(); } + + glActiveTexture(baseTexUnit); + glEnable(GL_COLOR_MATERIAL); + glAlphaFunc(GL_GREATER, 0.5f); + + modelRenderer->begin(true, true, false, false); } - glActiveTexture(baseTexUnit); + //ambient and diffuse color is taken from cell color - modelRenderer->begin(true, true, true, false, &meshCallbackTeamColor); - } - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - //translate - Vec3f currVec= unit->getCurrVectorFlat(); - glTranslatef(currVec.x, currVec.y, currVec.z); - - //rotate - float zrot=unit->getRotationZ(); - float xrot=unit->getRotationX(); - if(zrot!=.0f){ - glRotatef(zrot, 0.f, 0.f, 1.f); - } - if(xrot!=.0f){ - glRotatef(xrot, 1.f, 0.f, 0.f); - } - glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); - - //dead alpha - const SkillType *st= unit->getCurrSkill(); - if(st->getClass() == scDie && static_cast(st)->getFade()) { - float alpha= 1.0f - unit->getAnimProgressAsFloat(); - glDisable(GL_COLOR_MATERIAL); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); - } - else { - glEnable(GL_COLOR_MATERIAL); - // we cut off a tiny bit here to avoid problems with fully transparent texture parts cutting units in background rendered later. - glAlphaFunc(GL_GREATER, 0.02f); - } - - //render - Model *model= unit->getCurrentModelPtr(); - //printf("Rendering model [%d - %s]\n[%s]\nCamera [%s]\nDistance: %f\n",unit->getId(),unit->getType()->getName().c_str(),unit->getCurrVector().getString().c_str(),this->gameCamera->getPos().getString().c_str(),this->gameCamera->getPos().dist(unit->getCurrVector())); - - //if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) { - model->updateInterpolationData(unit->getAnimProgressAsFloat(), unit->isAlive() && !unit->isAnimProgressBound()); - //} - - modelRenderer->render(model); - triangleCount+= model->getTriangleCount(); - pointCount+= model->getVertexCount(); - - glPopMatrix(); - unit->setVisible(true); - - if( showDebugUI == true && - (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { - - unit->setScreenPos(computeScreenPosition(currVec)); - visibleFrameUnitList.push_back(unit); - visibleFrameUnitListCameraKey = game->getGameCamera()->getCameraMovementKey(); - } - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - glPopAttrib(); - } - } - - //restore - static_cast(modelRenderer)->setDuplicateTexCoords(true); - - // reset alpha - glAlphaFunc(GL_GREATER, 0.0f); - //assert - assertGl(); - -} - -void Renderer::renderUnitsToBuild(const int renderFps) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //assert - assertGl(); - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitBuildList.empty() == false) { - - glMatrixMode(GL_MODELVIEW); - glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_BLEND); - glDisable(GL_STENCIL_TEST); - glDepthFunc(GL_LESS); - glEnable(GL_COLOR_MATERIAL); - glDepthMask(GL_FALSE); - - modelRenderer->begin(true, true, false, false); - - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitBuildList.size(); ++visibleUnitIndex) { - const UnitBuildInfo &buildUnit = qCache.visibleQuadUnitBuildList[visibleUnitIndex]; - //Vec4f modelColor= Vec4f(0.f, 1.f, 0.f, 0.5f); - const Vec3f teamColor = buildUnit.unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0); - Vec4f modelColor= Vec4f(teamColor.x,teamColor.y,teamColor.z,0.4f); - renderGhostModel(buildUnit.buildUnit, buildUnit.pos, buildUnit.facing, &modelColor); - - //printf("Rendering to build unit index = %d\n",visibleUnitIndex); - } - - modelRenderer->end(); - - glDisable(GL_COLOR_MATERIAL); - glPopAttrib(); - } - - //assert - assertGl(); - -} - -void Renderer::renderTeamColorEffect(Vec3f &v, int heigth, int size, Vec3f color, const Texture2D *texture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //GLUquadricObj *disc; - float halfSize=size; - //halfSize=halfSize; - float heigthoffset=0.5+heigth%25*0.004; - glPushMatrix(); - glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); - glColor4f(color.x, color.y, color.z, 1.0f); - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(0, 1); - glVertex3f(v.x-halfSize,v.y+heigthoffset,v.z+halfSize); - glTexCoord2i(0, 0); - glVertex3f(v.x-halfSize,v.y+heigthoffset, v.z-halfSize); - glTexCoord2i(1, 1); - - glVertex3f(v.x+halfSize,v.y+heigthoffset, v.z+halfSize); - glTexCoord2i(1, 0); - glVertex3f(v.x+halfSize,v.y+heigthoffset, v.z-halfSize); - glEnd(); - glPopMatrix(); - -} - -void Renderer::renderMorphEffects(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - bool initialized=false; - int frameCycle=0; - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(unit->getCurrSkill() != NULL && unit->getCurrSkill()->getClass() == scMorph) { - Command *command= unit->getCurrCommand(); - if(command != NULL && command->getCommandType()->commandTypeClass == ccMorph){ - const MorphCommandType *mct= static_cast(command->getCommandType()); - const UnitType* mType=mct->getMorphUnit(); - - if(mType->getSize()>unit->getType()->getSize() || - mType->getField()!=unit->getType()->getField()){ - if(!initialized){ - const World *world= game->getWorld(); - frameCycle=world->getFrameCount() % 40; - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glDepthFunc(GL_ALWAYS); - glDisable(GL_STENCIL_TEST); - glDisable(GL_CULL_FACE); - glEnable(GL_BLEND); - glLineWidth(2.f); - initialized=true; - } - - Vec3f currVec= unit->getCurrVectorFlat(); - currVec=Vec3f(currVec.x,currVec.y+0.3f,currVec.z); - if(mType->getField() == fAir && unit->getType()->getField()== fLand) { - currVec=Vec3f(currVec.x,currVec.y+game->getWorld()->getTileset()->getAirHeight(),currVec.z); - } - if(mType->getField() == fLand && unit->getType()->getField()== fAir) { - currVec=Vec3f(currVec.x,currVec.y-game->getWorld()->getTileset()->getAirHeight(),currVec.z); - } - - float color=frameCycle*0.4f/40; - glColor4f(color,color, 0.4f, 0.4f); - renderSelectionCircle(currVec, mType->getSize(), frameCycle*0.85f/40, 0.2f); - } - } - } - } - if(initialized) { - glPopAttrib(); - } - } -} - - - -void Renderer::renderSelectionEffects(int healthbarMode) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("RecordMode","false") == true) { - return; - } - - const World *world= game->getWorld(); - const Map *map= world->getMap(); - const Selection *selection= game->getGui()->getSelection(); - const Object *selectedResourceObject= game->getGui()->getSelectedResourceObject(); - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glDepthFunc(GL_ALWAYS); - glDisable(GL_STENCIL_TEST); - glDisable(GL_CULL_FACE); - glEnable(GL_BLEND); - glLineWidth(2.f); - - //units - for(int i=0; igetCount(); ++i){ - - const Unit *unit= selection->getUnit(i); - if(unit != NULL) { - //translate - Vec3f currVec= unit->getCurrVectorFlat(); - currVec.y+= 0.3f; - - //selection circle - int finalHealthbarMode = hbvUndefined; - if(healthbarMode == hbvUndefined) { - finalHealthbarMode = unit->getFaction()->getType()->getHealthbarVisible(); - } - else { - finalHealthbarMode = healthbarMode; - } - bool healthbarsVisible =((finalHealthbarMode & hbvAlways) || - (finalHealthbarMode & hbvSelected) || - (finalHealthbarMode & hbvIfNeeded)); - float selectionCircleThickness = 0.2f; - float hpRatio = unit->getHpRatio(); - if(healthbarsVisible) { - selectionCircleThickness = 0.05f; - hpRatio = 1.0f; - } - - if(world->getThisFactionIndex() == unit->getFactionIndex()) { - if( showDebugUI == true && - ((showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) && - unit->getCommandSize() > 0 && - dynamic_cast(unit->getCurrCommand()->getCommandType()) != NULL) { - glColor4f(hpRatio, hpRatio, hpRatio, 0.3f); - } - else { - glColor4f(0, hpRatio, 0, 0.3f); - } - } - else if ( world->getThisTeamIndex() == unit->getTeam()) { - glColor4f(hpRatio, hpRatio, 0, 0.3f); - } - else { - glColor4f(hpRatio, 0, 0, 0.3f); - } - renderSelectionCircle(currVec, unit->getType()->getSize(), selectionCircleRadius,selectionCircleThickness); - - if( showDebugUI == true && - (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { - - const UnitPathInterface *path= unit->getPath(); - const UnitPathBasic *pathfinder = (path == NULL ? NULL : dynamic_cast(path)); - if(pathfinder != NULL) { - vector pathList = pathfinder->getQueue(); - - Vec2i lastPosValue; - for(int i = 0; i < (int)pathList.size(); ++i) { - Vec2i curPosValue = pathList[i]; - if(i == 0) { - lastPosValue = curPosValue; - } - Vec3f currVec2 = unit->getVectorFlat(lastPosValue,curPosValue); - currVec2.y+= 0.3f; - renderSelectionCircle(currVec2, 1, selectionCircleRadius); - } - } - } - - //magic circle - if(!healthbarsVisible && world->getThisFactionIndex() == unit->getFactionIndex() && unit->getType()->getMaxEp() > 0) { - glColor4f(unit->getEpRatio()/2.f, unit->getEpRatio(), unit->getEpRatio(), 0.5f); - renderSelectionCircle(currVec, unit->getType()->getSize(), magicCircleRadius); - } - - // Render Attack-boost circles - if(showDebugUI == true) { - //const std::pair > ¤tAttackBoostUnits = unit->getCurrentAttackBoostUnits(); - const UnitAttackBoostEffectOriginator &effect = unit->getAttackBoostOriginatorEffect(); - - if(effect.skillType->isAttackBoostEnabled() == true) { - glColor4f(MAGENTA.x,MAGENTA.y,MAGENTA.z,MAGENTA.w); - renderSelectionCircle(currVec, 1, effect.skillType->getAttackBoost()->radius, .25f/effect.skillType->getAttackBoost()->radius); - - for(unsigned int i = 0; i < effect.currentAttackBoostUnits.size(); ++i) { - // Remove attack boost upgrades from unit - int findUnitId = effect.currentAttackBoostUnits[i]; - Unit *affectedUnit = game->getWorld()->findUnitById(findUnitId); - if(affectedUnit != NULL) { - Vec3f currVecBoost = affectedUnit->getCurrVectorFlat(); - currVecBoost.y += 0.3f; - - renderSelectionCircle(currVecBoost, affectedUnit->getType()->getSize(), 1.f); - } - } - } - } - - //meeting point arrow - if(unit->getType()->getMeetingPoint()) { - Vec2i pos= unit->getMeetingPos(); - map->clampPos(pos); - - Vec3f arrowTarget= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); - renderArrow(unit->getCurrVectorFlat(), arrowTarget, Vec3f(0.f, 0.f, 1.f), 0.3f); - } - } - } - - if(selectedResourceObject != NULL && selectedResourceObject->getResource() != NULL && selection->getCount() < 1) { - Resource *r= selectedResourceObject->getResource(); - int defaultValue= r->getType()->getDefResPerPatch(); - float colorValue=static_cast(r->getAmount())/static_cast(defaultValue); - glColor4f(0.1f, 0.1f , colorValue, 0.4f); - renderSelectionCircle(selectedResourceObject->getPos(),2, selectionCircleRadius); - } - //target arrow - if(selection->getCount() == 1) { - const Unit *unit= selection->getUnit(0); - if(unit != NULL) { - //comand arrow - if(focusArrows && unit->anyCommand()) { - const CommandType *ct= unit->getCurrCommand()->getCommandType(); - if(ct->getClicks() != cOne){ - - //arrow color - Vec3f arrowColor; - switch(ct->getClass()) { - case ccMove: - arrowColor= Vec3f(0.f, 1.f, 0.f); - break; - case ccAttack: - case ccAttackStopped: - arrowColor= Vec3f(1.f, 0.f, 0.f); - break; - default: - arrowColor= Vec3f(1.f, 1.f, 0.f); - break; - } - - //arrow target - Vec3f arrowTarget; - Command *c= unit->getCurrCommand(); - if(c->getUnit() != NULL) { - arrowTarget= c->getUnit()->getCurrVectorFlat(); - } - else { - Vec2i pos= c->getPos(); - map->clampPos(pos); - - arrowTarget= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); - } - - renderArrow(unit->getCurrVectorFlat(), arrowTarget, arrowColor, 0.3f); - } - } - } - } - - //render selection hightlights - if(game->getGui()->getHighlightedUnit() != NULL) { - const Unit *unit=game->getGui()->getHighlightedUnit() ; - - if(unit->isHighlighted()) { - float highlight= unit->getHightlight(); - if(game->getWorld()->getThisFactionIndex() == unit->getFactionIndex()) { - glColor4f(0.f, 1.f, 0.f, highlight); - } - else{ - glColor4f(1.f, 0.f, 0.f, highlight); - } - - Vec3f v= unit->getCurrVectorFlat(); - v.y+= 0.3f; - renderSelectionCircle(v, unit->getType()->getSize(), 0.5f+0.4f*highlight ); - } - } -// old inefficient way to render highlights -// for(int i=0; i < world->getFactionCount(); ++i) { -// for(int j=0; j < world->getFaction(i)->getUnitCount(); ++j) { -// const Unit *unit= world->getFaction(i)->getUnit(j); -// -// if(unit->isHighlighted()) { -// float highlight= unit->getHightlight(); -// if(game->getWorld()->getThisFactionIndex() == unit->getFactionIndex()) { -// glColor4f(0.f, 1.f, 0.f, highlight); -// } -// else{ -// glColor4f(1.f, 0.f, 0.f, highlight); -// } -// -// Vec3f v= unit->getCurrVectorFlat(); -// v.y+= 0.3f; -// renderSelectionCircle(v, unit->getType()->getSize(), 0.5f+0.4f*highlight ); -// } -// } -// } - //render resource selection highlight - if(game->getGui()->getHighlightedResourceObject() != NULL) { - const Object* object=game->getGui()->getHighlightedResourceObject(); - if(object->isHighlighted()) { - float highlight= object->getHightlight(); - glColor4f(0.1f, 0.1f , 1.0f, highlight); - Vec3f v= object->getPos(); - v.y+= 0.3f; - renderSelectionCircle(v, 2, 0.4f+0.4f*highlight ); - } - } - - glPopAttrib(); -} - -void Renderer::renderHealthBars(int healthbarMode){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("RecordMode","false") == true) { - return; - } - - if(config.getBool("PhotoMode")) { - return; - } - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - glDisable(GL_LIGHTING); - glDisable(GL_TEXTURE_2D); - glDepthFunc(GL_ALWAYS); - glDisable(GL_STENCIL_TEST); - glDisable(GL_CULL_FACE); - glEnable(GL_BLEND); - glLineWidth(2.f); - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(isHealthBarVisible(unit,healthbarMode)) { - float healthbarheight; - float healthbarthickness; - const Texture2D *healthbarTexture; - const Texture2D *healthbarBackgroundTexture; - bool healthbarLineBorder; - - //get settings of the faction - healthbarheight=unit->getFaction()->getType()->getHealthbarHeight(); - healthbarthickness=unit->getFaction()->getType()->getHealthbarThickness(); - healthbarLineBorder=unit->getFaction()->getType()->isHealthbarLineBorder(); - CoreData &coreData= CoreData::getInstance(); - //First try faction texture then use core Texture - if(unit->getFaction()->getType()->isHealthbarBorderTextureEnabled()) { - healthbarTexture=unit->getFaction()->getType()->getHealthbarTexture(); - if(healthbarTexture==NULL) { - healthbarTexture=coreData.getHealthbarTexture(); - } - } else { - healthbarTexture=NULL; - } - if(unit->getFaction()->getType()->isHealthbarBackgroundTextureEnabled()) { - healthbarBackgroundTexture=unit->getFaction()->getType()->getHealthbarBackgroundTexture(); - if(healthbarBackgroundTexture==NULL) { - healthbarBackgroundTexture=coreData.getHealthbarBackgroundTexture(); - } - } else { - healthbarBackgroundTexture=NULL; - } - - //replace them by the ones from the unit if existent - if(unit->getType()->getHealthbarVisible()!=hbvOff && unit->getType()->getHealthbarVisible()!=hbvUndefined) { - if(unit->getType()->getHealthbarHeight()!=-100.0f) { - healthbarheight=unit->getType()->getHealthbarHeight(); - } - if(unit->getType()->getHealthbarThickness()!=-1.0f) { - healthbarthickness=unit->getType()->getHealthbarThickness(); - } - } - - Vec3f currVec= unit->getCurrVectorFlat(); - if(healthbarheight==-100.0f) { - currVec.y+=unit->getType()->getHeight(); - } else { - currVec.y+=healthbarheight; - } - renderHealthBar(currVec,unit,healthbarthickness,healthbarLineBorder,healthbarTexture,healthbarBackgroundTexture); - } - } - } - glPopAttrib(); -} - -bool Renderer::isHealthBarVisible(const Unit *unit,int healthbarMode){ - int healthbarVisible=hbvUndefined; - //check options (hotkey) - if(healthbarMode==hbvUndefined) { - healthbarVisible=unit->getFaction()->getType()->getHealthbarVisible(); - } else { - healthbarVisible=healthbarMode; - } - - //replace them by the ones from the unit if existent - if(unit->getType()->getHealthbarVisible()!=hbvOff && unit->getType()->getHealthbarVisible()!=hbvUndefined) { - if(healthbarMode==hbvUndefined) { //don't override the visible setting when hotkey is not hbvUndefined - healthbarVisible=unit->getType()->getHealthbarVisible(); - } - } - - bool settingsWantToRenderThem=!(healthbarVisible==hbvUndefined || (healthbarVisible&hbvOff)) - && ((healthbarVisible&hbvAlways) - || ((healthbarVisible&hbvIfNeeded) && unit->getHp()getType()->getMaxHp()+unit->getTotalUpgrade()->getMaxHp()) - || ((healthbarVisible&hbvIfNeeded) && unit->getType()->getMaxEp() > 0 && unit->getEp()getType()->getMaxEp()+unit->getTotalUpgrade()->getMaxEp()) - || ((healthbarVisible&hbvIfNeeded) && unit->getProductionPercent() > 0) - || ((healthbarVisible&hbvSelected) && game->getGui()->isSelected(unit))); - - if(unit->isAlive() && (settingsWantToRenderThem)) { - return true; - } - return false; -} - -void Renderer::renderWaterEffects(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const World *world= game->getWorld(); - const WaterEffects *we= world->getWaterEffects(); - const Map *map= world->getMap(); - CoreData &coreData= CoreData::getInstance(); - float height= map->getWaterLevel()+0.001f; - - assertGl(); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); - glEnable(GL_BLEND); - glDisable(GL_ALPHA_TEST); - glDepthMask(GL_FALSE); - glDepthFunc(GL_LEQUAL); - glEnable(GL_COLOR_MATERIAL); - - //glNormal3f(0.f, 1.f, 0.f); - - //splashes - glBindTexture(GL_TEXTURE_2D, static_cast(coreData.getWaterSplashTexture())->getHandle()); - - //!!! - Vec2f texCoords[4]; - Vec3f vertices[4]; - Vec3f normals[4]; - - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_NORMAL_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - - for(int i=0; igetWaterSplashCount(); ++i){ - const WaterSplash *ws= we->getWaterSplash(i); - - //render only if enabled - if(ws->getEnabled()){ - - //render only if visible - Vec2i intPos= Vec2i(static_cast(ws->getPos().x), static_cast(ws->getPos().y)); - const Vec2i &mapPos = Map::toSurfCoords(intPos); - - bool visible = map->getSurfaceCell(mapPos)->isVisible(world->getThisTeamIndex()); - if(visible == false && world->showWorldForPlayer(world->getThisFactionIndex()) == true) { - visible = true; - } - - if(visible == true) { - float scale= ws->getAnim()*ws->getSize(); - texCoords[0] = Vec2f(0.f, 1.f); - vertices[0] = Vec3f(ws->getPos().x-scale, height, ws->getPos().y+scale); - normals[0] = Vec3f(0.f, 1.f, 0.f); - - texCoords[1] = Vec2f(0.f, 0.f); - vertices[1] = Vec3f(ws->getPos().x-scale, height, ws->getPos().y-scale); - normals[1] = Vec3f(0.f, 1.f, 0.f); - - texCoords[2] = Vec2f(1.f, 1.f); - vertices[2] = Vec3f(ws->getPos().x+scale, height, ws->getPos().y+scale); - normals[2] = Vec3f(0.f, 1.f, 0.f); - - texCoords[3] = Vec2f(1.f, 0.f); - vertices[3] = Vec3f(ws->getPos().x+scale, height, ws->getPos().y-scale); - normals[3] = Vec3f(0.f, 1.f, 0.f); - - glColor4f(1.f, 1.f, 1.f, 1.f - ws->getAnim()); - glTexCoordPointer(2, GL_FLOAT, 0,&texCoords[0]); - glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); - glNormalPointer(GL_FLOAT, 0, &normals[0]); - glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - - -/* - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 1.f); - glVertex3f(ws->getPos().x-scale, height, ws->getPos().y+scale); - glTexCoord2f(0.f, 0.f); - glVertex3f(ws->getPos().x-scale, height, ws->getPos().y-scale); - glTexCoord2f(1.f, 1.f); - glVertex3f(ws->getPos().x+scale, height, ws->getPos().y+scale); - glTexCoord2f(1.f, 0.f); - glVertex3f(ws->getPos().x+scale, height, ws->getPos().y-scale); - glEnd(); -*/ - } - } - } - - glDisableClientState(GL_TEXTURE_COORD_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderHud(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Texture2D *hudTexture=game->getGui()->getHudTexture(); - if(hudTexture!=NULL){ - const Metrics &metrics= Metrics::getInstance(); - renderTextureQuad(0, 0, metrics.getVirtualW(), metrics.getVirtualH(),hudTexture,1.0f); - } -} - -void Renderer::renderMinimap(){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - const World *world= game->getWorld(); - const Minimap *minimap= world->getMinimap(); - - if(minimap == NULL || minimap->getTexture() == NULL) { - return; - } - - const GameCamera *gameCamera= game->getGameCamera(); - const Pixmap2D *pixmap= minimap->getTexture()->getPixmapConst(); - const Metrics &metrics= Metrics::getInstance(); - const WaterEffects *attackEffects= world->getAttackEffects(); - - int mx= metrics.getMinimapX(); - int my= metrics.getMinimapY(); - int mw= metrics.getMinimapW(); - int mh= metrics.getMinimapH(); - - Vec2f zoom= Vec2f( - static_cast(mw)/ pixmap->getW(), - static_cast(mh)/ pixmap->getH()); - - assertGl(); - - // render minimap border - Vec4f col= game->getGui()->getDisplay()->getColor(); - glColor4f(col.x*0.5f,col.y*0.5f,col.z*0.5f,1.0 ); - - int borderWidth=2; - glBegin(GL_QUADS); - glVertex2i(mx-borderWidth, my-borderWidth); - glVertex2i(mx-borderWidth, my); - glVertex2i(mx+mw+borderWidth, my); - glVertex2i(mx+mw+borderWidth, my-borderWidth); - glEnd(); - - glBegin(GL_QUADS); - glVertex2i(mx-borderWidth, my+mh+borderWidth); - glVertex2i(mx-borderWidth, my+mh); - glVertex2i(mx+mw+borderWidth, my+mh); - glVertex2i(mx+mw+borderWidth, my+mh+borderWidth); - glEnd(); - - glBegin(GL_QUADS); - glVertex2i(mx-borderWidth, my); - glVertex2i(mx-borderWidth, my+mh); - glVertex2i(mx, my+mh); - glVertex2i(mx, my); - glEnd(); - - glBegin(GL_QUADS); - glVertex2i(mx+mw, my); - glVertex2i(mx+mw, my+mh); - glVertex2i(mx+mw+borderWidth, my+mh); - glVertex2i(mx+mw+borderWidth, my); - glEnd(); - - assertGl(); - - glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_TEXTURE_BIT); - - //draw map - glEnable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - - glActiveTexture(fowTexUnit); - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(minimap->getFowTexture())->getHandle()); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE); - - glActiveTexture(baseTexUnit); - glBindTexture(GL_TEXTURE_2D, static_cast(minimap->getTexture())->getHandle()); - - glColor4f(0.5f, 0.5f, 0.5f, 0.2f); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.0f, 1.0f); - glMultiTexCoord2f(fowTexUnit, 0.0f, 1.0f); - glVertex2i(mx, my); - - glTexCoord2f(0.0f, 0.0f); - glMultiTexCoord2f(fowTexUnit, 0.0f, 0.0f); - glVertex2i(mx, my+mh); - - glTexCoord2f(1.0f, 1.0f); - glMultiTexCoord2f(fowTexUnit, 1.0f, 1.0f); - glVertex2i(mx+mw, my); - - glTexCoord2f(1.0f, 0.0f); - glMultiTexCoord2f(fowTexUnit, 1.0f, 0.0f); - glVertex2i(mx+mw, my+mh); - glEnd(); - - glDisable(GL_BLEND); - - glActiveTexture(fowTexUnit); - glDisable(GL_TEXTURE_2D); - glActiveTexture(baseTexUnit); - glDisable(GL_TEXTURE_2D); - - glEnable(GL_BLEND); - - const int itemCount = attackEffects->getWaterSplashCount() * 12; - if(itemCount > 0) { - vector vertices; - vertices.resize(itemCount); - vector colors; - colors.resize(itemCount); - - // draw attack alarm - int vertexIndex = 0; - for(int i = 0; i < attackEffects->getWaterSplashCount(); ++i) { - const WaterSplash *ws = attackEffects->getWaterSplash(i); - float scale= (1/ws->getAnim()*ws->getSize())*5; - //glColor4f(1.f, 1.f, 0.f, 1.f-ws->getAnim()); - float alpha=(1.f-ws->getAnim())*0.01f; - Vec2f pos= ws->getPos()/Map::cellScale; - float attackX=mx +pos.x*zoom.x; - float attackY=my +mh -pos.y*zoom.y; - if(ws->getEnabled()){ - // glBegin(GL_QUADS); - // glVertex2f(attackX-scale, attackY-scale); - // glVertex2f(attackX-scale, attackY+scale); - // glVertex2f(attackX+scale, attackY+scale); - // glVertex2f(attackX+scale, attackY-scale); - // glEnd(); - - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX-scale, attackY-scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX-scale, attackY+scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); - vertices[vertexIndex] = Vec2f(attackX, attackY); - vertexIndex++; - - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX+scale, attackY+scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX-scale, attackY+scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); - vertices[vertexIndex] = Vec2f(attackX, attackY); - vertexIndex++; - - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX+scale, attackY+scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX+scale, attackY-scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); - vertices[vertexIndex] = Vec2f(attackX, attackY); - vertexIndex++; - - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX+scale, attackY-scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); - vertices[vertexIndex] = Vec2f(attackX-scale, attackY-scale); - vertexIndex++; - colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); - vertices[vertexIndex] = Vec2f(attackX, attackY); - vertexIndex++; - - /* - glBegin(GL_TRIANGLES); - glColor4f(1.f, 1.f, 0.f, alpha); - glVertex2f(attackX-scale, attackY-scale); - glVertex2f(attackX-scale, attackY+scale); - glColor4f(1.f, 1.f, 0.f, 0.8f); - glVertex2f(attackX, attackY); - glEnd(); - glBegin(GL_TRIANGLES); - glColor4f(1.f, 1.f, 0.f, alpha); - glVertex2f(attackX-scale, attackY+scale); - glVertex2f(attackX+scale, attackY+scale); - glColor4f(1.f, 1.f, 0.f, 0.8f); - glVertex2f(attackX, attackY); - glEnd(); - glBegin(GL_TRIANGLES); - glColor4f(1.f, 1.f, 0.f, alpha); - glVertex2f(attackX+scale, attackY+scale); - glVertex2f(attackX+scale, attackY-scale); - glColor4f(1.f, 1.f, 0.f, 0.8f); - glVertex2f(attackX, attackY); - glEnd(); - glBegin(GL_TRIANGLES); - glColor4f(1.f, 1.f, 0.f, alpha); - glVertex2f(attackX+scale, attackY-scale); - glVertex2f(attackX-scale, attackY-scale); - glColor4f(1.f, 1.f, 0.f, 0.8f); - glVertex2f(attackX, attackY); - glEnd(); - */ - - } - } - - if(vertexIndex > 0) { - glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glColorPointer(4,GL_FLOAT, 0, &colors[0]); - glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); - glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexIndex); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - } - } - - glDisable(GL_BLEND); - - //draw units - VisibleQuadContainerCache &qCache = getQuadCache(); - std::vector visibleUnitList = qCache.visibleUnitList; - - const bool showAllUnitsInMinimap = Config::getInstance().getBool("DebugGameSynchUI","false"); - if(showAllUnitsInMinimap == true) { - visibleUnitList.clear(); - - const World *world= game->getWorld(); - for(unsigned int i = 0; i < (unsigned int)world->getFactionCount(); ++i) { - const Faction *faction = world->getFaction(i); - for(unsigned int j = 0; j < (unsigned int)faction->getUnitCount(); ++j) { - Unit *unit = faction->getUnit(j); - visibleUnitList.push_back(unit); - } - } - } - - if(visibleUnitList.empty() == false) { - uint32 unitIdx=0; - vector unit_vertices; - unit_vertices.resize(visibleUnitList.size()*4); - vector unit_colors; - unit_colors.resize(visibleUnitList.size()*4); - - for(int visibleIndex = 0; - visibleIndex < (int)visibleUnitList.size(); ++visibleIndex) { - Unit *unit = visibleUnitList[visibleIndex]; - if (unit->isAlive() == false) { - continue; - } - - Vec2i pos= unit->getPos() / Map::cellScale; - int size= unit->getType()->getSize(); - Vec3f color= unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); - - unit_colors[unitIdx] = color; - unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = color; - unit_vertices[unitIdx] = Vec2f(mx + (pos.x+1)*zoom.x+size, my + mh - (pos.y*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = color; - unit_vertices[unitIdx] = Vec2f(mx + (pos.x+1)*zoom.x+size, my + mh - ((pos.y+size)*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = color; - unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - ((pos.y+size)*zoom.y)); - unitIdx++; - } - - if(unitIdx > 0) { - glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glColorPointer(3,GL_FLOAT, 0, &unit_colors[0]); - glVertexPointer(2, GL_FLOAT, 0, &unit_vertices[0]); - glDrawArrays(GL_QUADS, 0, unitIdx); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - } - - } - - renderMarkedCellsOnMinimap(); - - //draw camera - float wRatio= static_cast(metrics.getMinimapW()) / world->getMap()->getW(); - float hRatio= static_cast(metrics.getMinimapH()) / world->getMap()->getH(); - - int x= static_cast(gameCamera->getPos().x * wRatio); - int y= static_cast(gameCamera->getPos().z * hRatio); - - float ang= degToRad(gameCamera->getHAng()); - - glEnable(GL_BLEND); - - int x1; - int y1; - x1 = mx + x + static_cast(20*std::sin(ang-pi/5)); - y1 = my + mh - (y-static_cast(20*std::cos(ang-pi/5))); - - int x2; - int y2; - x2 = mx + x + static_cast(20*std::sin(ang+pi/5)); - y2 = my + mh - (y-static_cast(20*std::cos(ang+pi/5))); - - glColor4f(1.f, 1.f, 1.f, 1.f); - glBegin(GL_TRIANGLES); - - glVertex2i(mx+x, my+mh-y); - glColor4f(1.f, 1.f, 1.f, 0.0f); - glVertex2i(x1,y1); - glColor4f(1.f, 1.f, 1.f, 0.0f); - glVertex2i(x2,y2); - - glEnd(); - - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderHighlightedCellsOnMinimap() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - // Draw marked cells - const std::vector *highlightedCells = game->getHighlightedCells(); - if(highlightedCells->empty() == false) { - //const Map *map= game->getWorld()->getMap(); - const World *world= game->getWorld(); - const Minimap *minimap= world->getMinimap(); - int pointersize=10; - if(minimap == NULL || minimap->getTexture() == NULL) { - return; - } - - //const GameCamera *gameCamera= game->getGameCamera(); - const Pixmap2D *pixmap= minimap->getTexture()->getPixmapConst(); - const Metrics &metrics= Metrics::getInstance(); - - - //int mx= metrics.getMinimapX(); - int my= metrics.getMinimapY(); - int mw= metrics.getMinimapW(); - int mh= metrics.getMinimapH(); - - Vec2f zoom= Vec2f( - static_cast(mw)/ pixmap->getW()/2, - static_cast(mh)/ pixmap->getH()/2); - - for(int i = 0;i < (int)highlightedCells->size(); i++) { - const MarkedCell *mc=&highlightedCells->at(i); - if(mc->getFaction() == NULL || (mc->getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { - const Texture2D *texture= game->getHighlightCellTexture(); - Vec3f color(MarkedCell::static_system_marker_color); - if(mc->getFaction() != NULL) { - color= mc->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); - } - int lighting=(mc->getAliveCount()%15); - Vec3f myColor=Vec3f(color.x/2+.5f/lighting,color.y/2+.5f/lighting,color.z/2+.5f/lighting); - - Vec2i pos=mc->getTargetPos(); - if(texture != NULL) { - //float alpha = 0.49f+0.5f/(mc->getAliveCount()%15); - float alpha=1.0f; - renderTextureQuad((int)(pos.x*zoom.x)+pointersize, my + mh-(int)(pos.y*zoom.y), pointersize, pointersize, texture, alpha,&myColor); - } - } - } - } -} - -void Renderer::renderMarkedCellsOnMinimap() { - // Draw marked cells - std::map markedCells = game->getMapMarkedCellList(); - if(markedCells.empty() == false) { - //const Map *map= game->getWorld()->getMap(); - const World *world= game->getWorld(); - const Minimap *minimap= world->getMinimap(); - - if(minimap == NULL || minimap->getTexture() == NULL) { - return; - } - - //const GameCamera *gameCamera= game->getGameCamera(); - const Pixmap2D *pixmap= minimap->getTexture()->getPixmapConst(); - const Metrics &metrics= Metrics::getInstance(); - //const WaterEffects *attackEffects= world->getAttackEffects(); - - int mx= metrics.getMinimapX(); - int my= metrics.getMinimapY(); - int mw= metrics.getMinimapW(); - int mh= metrics.getMinimapH(); - - Vec2f zoom= Vec2f( - static_cast(mw)/ pixmap->getW(), - static_cast(mh)/ pixmap->getH()); - - uint32 unitIdx=0; - vector unit_vertices; - unit_vertices.resize(markedCells.size()*4); - vector unit_colors; - unit_colors.resize(markedCells.size()*4); - - for(std::map::iterator iterMap =markedCells.begin(); - iterMap != markedCells.end(); ++iterMap) { - MarkedCell &bm = iterMap->second; - if(bm.getPlayerIndex() < 0 || - (bm.getFaction() != NULL && - bm.getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { - Vec2i pos= bm.getTargetPos() / Map::cellScale; - float size= 0.5f; - - Vec3f color(MarkedCell::static_system_marker_color); - if(bm.getFaction() != NULL) { - color= bm.getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); - } - float alpha = 0.65f; - - unit_colors[unitIdx] = Vec4f(color.x,color.y,color.z,alpha); - unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = Vec4f(color.x,color.y,color.z,alpha); - unit_vertices[unitIdx] = Vec2f(mx + (pos.x+1)*zoom.x+size, my + mh - (pos.y*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = Vec4f(color.x,color.y,color.z,alpha); - unit_vertices[unitIdx] = Vec2f(mx + (pos.x+1)*zoom.x+size, my + mh - ((pos.y+size)*zoom.y)); - unitIdx++; - - unit_colors[unitIdx] = Vec4f(color.x,color.y,color.z,alpha); - unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - ((pos.y+size)*zoom.y)); - unitIdx++; - } - } - - if(unitIdx > 0) { - glEnable(GL_BLEND); - - glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_VERTEX_ARRAY); - glColorPointer(4,GL_FLOAT, 0, &unit_colors[0]); - glVertexPointer(2, GL_FLOAT, 0, &unit_vertices[0]); - glDrawArrays(GL_QUADS, 0, unitIdx); - //glDrawArrays(GL_TRIANGLE_STRIP, 0, unitIdx); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - glDisable(GL_BLEND); - } - } -} -void Renderer::renderVisibleMarkedCells(bool renderTextHint,int x, int y) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - // Draw marked cells - std::map markedCells = game->getMapMarkedCellList(); - if(markedCells.empty() == false) { - const Texture2D *texture= game->getMarkCellTexture(); - const int yOffset = -40; - - for(std::map::iterator iterMap =markedCells.begin(); - iterMap != markedCells.end(); ++iterMap) { - MarkedCell &bm = iterMap->second; - if(bm.getPlayerIndex() < 0 || - (bm.getFaction() != NULL && - bm.getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { - const Map *map= game->getWorld()->getMap(); - std::pair bmVisible = posInCellQuadCache( - map->toSurfCoords(bm.getTargetPos())); - if(bmVisible.first == true) { - if(renderTextHint == true) { - if(bm.getNote() != "") { - bool validPosObjWorld= x > bmVisible.second.x && - y > bmVisible.second.y + yOffset && - x < bmVisible.second.x + texture->getTextureWidth() && - y < bmVisible.second.y + yOffset + texture->getTextureHeight(); - - if(validPosObjWorld) { - //printf("Checking for hint text render mouse [%d,%d] marker pos [%d,%d] validPosObjWorld = %d, hint [%s]\n",x,y,bm.getTargetPos().x,bm.getTargetPos().y,validPosObjWorld,bm.getNote().c_str()); - - //Lang &lang= Lang::getInstance(); - Vec4f fontColor = Vec4f(1.0f, 1.0f, 1.0f, 0.25f); - - if(renderText3DEnabled == true) { - renderTextShadow3D(bm.getNote(), CoreData::getInstance().getConsoleFont3D(), fontColor, - bmVisible.second.x, bmVisible.second.y); - } - else { - renderTextShadow(bm.getNote(), CoreData::getInstance().getConsoleFont(), fontColor, - bmVisible.second.x, bmVisible.second.y); - } - } - } - } - else { - - -/* - //texture 0 - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - //set color to interpolation - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); - - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - //texture 1 - glActiveTexture(GL_TEXTURE1); - glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); - glEnable(GL_TEXTURE_2D); - - glBindTexture(GL_TEXTURE_2D, static_cast(bm.getFaction()->getTexture())->getHandle()); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); - - //set alpha to 1 - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - - glActiveTexture(GL_TEXTURE0); -*/ - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - Vec3f color(MarkedCell::static_system_marker_color); - if(bm.getFaction() != NULL) { - color = bm.getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0); - } - - renderTextureQuad( - bmVisible.second.x,bmVisible.second.y + yOffset, - texture->getTextureWidth(),texture->getTextureHeight(),texture,0.8f,&color); - -/* - glActiveTexture(GL_TEXTURE1); - glDisable(GL_TEXTURE_2D); - glActiveTexture(GL_TEXTURE0); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); -*/ - } - } - } - } - } -} - -void Renderer::renderDisplay() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - CoreData &coreData= CoreData::getInstance(); - const Metrics &metrics= Metrics::getInstance(); - const Display *display= game->getGui()->getDisplay(); - - glPushAttrib(GL_ENABLE_BIT); - - if(renderText3DEnabled == true) { - //infoString - renderTextShadow3D( - display->getInfoText().c_str(), - coreData.getDisplayFont3D(), - display->getColor(), - metrics.getDisplayX(), - metrics.getDisplayY()+Display::infoStringY); - - //title - renderTextShadow3D( - display->getTitle().c_str(), - coreData.getDisplayFont3D(), - display->getColor(), - metrics.getDisplayX()+40, - metrics.getDisplayY() + metrics.getDisplayH() - 20); - - glColor3f(0.0f, 0.0f, 0.0f); - - //text - renderTextShadow3D( - display->getText().c_str(), - coreData.getDisplayFont3D(), - display->getColor(), - metrics.getDisplayX() -1, - metrics.getDisplayY() + metrics.getDisplayH() - 56); - - //progress Bar - if(display->getProgressBar() != -1) { - renderProgressBar3D( - display->getProgressBar(), - metrics.getDisplayX(), - metrics.getDisplayY() + metrics.getDisplayH()-50, - coreData.getDisplayFontSmall3D(), 175); - } - } - else { - //infoString - renderTextShadow( - display->getInfoText().c_str(), - coreData.getDisplayFont(), - display->getColor(), - metrics.getDisplayX(), - metrics.getDisplayY()+Display::infoStringY); - - //title - renderTextShadow( - display->getTitle().c_str(), - coreData.getDisplayFont(), - display->getColor(), - metrics.getDisplayX()+40, - metrics.getDisplayY() + metrics.getDisplayH() - 20); - - glColor3f(0.0f, 0.0f, 0.0f); - - //text - renderTextShadow( - display->getText().c_str(), - coreData.getDisplayFont(), - display->getColor(), - metrics.getDisplayX() -1, - metrics.getDisplayY() + metrics.getDisplayH() - 56); - - //progress Bar - if(display->getProgressBar()!=-1){ - renderProgressBar( - display->getProgressBar(), - metrics.getDisplayX(), - metrics.getDisplayY() + metrics.getDisplayH()-50, - coreData.getDisplayFontSmall()); - } - } - - //up images - glEnable(GL_TEXTURE_2D); - - glColor3f(1.f, 1.f, 1.f); - for(int i=0; igetUpImage(i)!=NULL){ - renderQuad( - metrics.getDisplayX()+display->computeUpX(i), - metrics.getDisplayY()+display->computeUpY(i), - display->getUpImageSize(), display->getUpImageSize(), display->getUpImage(i)); - } - } - - //down images - for(int i=0; igetDownImage(i)!=NULL){ - if(display->getDownLighted(i)){ - glColor3f(1.f, 1.f, 1.f); - } - else{ - glColor3f(0.3f, 0.3f, 0.3f); - } - - int x= metrics.getDisplayX()+display->computeDownX(i); - int y= metrics.getDisplayY()+display->computeDownY(i); - int size= Display::imageSize; - - if(display->getDownSelectedPos()==i){ - x-= 3; - y-= 3; - size+= 6; - } - - renderQuad(x, y, size, size, display->getDownImage(i)); - } - } - - //selection - int downPos= display->getDownSelectedPos(); - if(downPos!=Display::invalidPos){ - const Texture2D *texture= display->getDownImage(downPos); - if(texture!=NULL){ - int x= metrics.getDisplayX()+display->computeDownX(downPos)-3; - int y= metrics.getDisplayY()+display->computeDownY(downPos)-3; - int size= Display::imageSize+6; - renderQuad(x, y, size, size, display->getDownImage(downPos)); - } - } - - glPopAttrib(); -} - -void Renderer::renderMenuBackground(const MenuBackground *menuBackground) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - assertGl(); - - const Vec3f &cameraPosition= menuBackground->getCamera()->getConstPosition(); - - glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - - //clear - Vec4f fogColor= Vec4f(0.4f, 0.4f, 0.4f, 1.f) * menuBackground->getFade(); - glClearColor(fogColor.x, fogColor.y, fogColor.z, 1.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glFogfv(GL_FOG_COLOR, fogColor.ptr()); - - //light - Vec4f lightPos= Vec4f(10.f, 10.f, 10.f, 1.f)* menuBackground->getFade(); - Vec4f diffLight= Vec4f(0.9f, 0.9f, 0.9f, 1.f)* menuBackground->getFade(); - Vec4f ambLight= Vec4f(0.3f, 0.3f, 0.3f, 1.f)* menuBackground->getFade(); - Vec4f specLight= Vec4f(0.1f, 0.1f, 0.1f, 1.f)* menuBackground->getFade(); - - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight.ptr()); - glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight.ptr()); - glLightfv(GL_LIGHT0, GL_SPECULAR, specLight.ptr()); - - //main model - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_GREATER, 0.5f); - modelRenderer->begin(true, true, true, false); - menuBackground->getMainModelPtr()->updateInterpolationData(menuBackground->getAnim(), true); - modelRenderer->render(menuBackground->getMainModelPtr()); - modelRenderer->end(); - glDisable(GL_ALPHA_TEST); - - //characters - float dist= menuBackground->getAboutPosition().dist(cameraPosition); - float minDist= 3.f; - if(dist < minDist) { - - glAlphaFunc(GL_GREATER, 0.0f); - float alpha= clamp((minDist-dist) / minDist, 0.f, 1.f); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); - - std::vector &characterMenuScreenPositionListCache = - CacheManager::getCachedItem< std::vector >(GameConstants::characterMenuScreenPositionListCacheLookupKey); - characterMenuScreenPositionListCache.clear(); - - modelRenderer->begin(true, true, false, false); - - for(int i=0; i < MenuBackground::characterCount; ++i) { - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - - Vec3f worldPos(i*2.f-4.f, -1.4f, -7.5f); - glTranslatef(worldPos.x,worldPos.y,worldPos.z); - - // - // Get the screen coordinates for each character model - START - //std::vector projection(16); - //std::vector modelview(16); - //std::vector screen_coords(3); - - //glGetDoublev(GL_PROJECTION_MATRIX, projection.data()); - //glGetDoublev(GL_MODELVIEW_MATRIX, modelview.data()); - - const Metrics &metrics= Metrics::getInstance(); - GLint viewport[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - - //get matrices - GLdouble projection[16]; - glGetDoublev(GL_PROJECTION_MATRIX, projection); - - GLdouble modelview[16]; - glGetDoublev(GL_MODELVIEW_MATRIX, modelview); - - //get the screen coordinates - GLdouble screen_coords[3]; - - gluProject(worldPos.x, worldPos.y, worldPos.z, - modelview, projection, viewport, - &screen_coords[0], &screen_coords[1], &screen_coords[2]); - characterMenuScreenPositionListCache.push_back(Vec3f(screen_coords[0],screen_coords[1],screen_coords[2])); - // Get the screen coordinates for each character model - END - // - - menuBackground->getCharacterModelPtr(i)->updateInterpolationData(menuBackground->getAnim(), true); - modelRenderer->render(menuBackground->getCharacterModelPtr(i)); - - glPopMatrix(); - } - modelRenderer->end(); - } - - //water - if(menuBackground->getWater()) { - - //water surface - const int waterTesselation= 10; - const int waterSize= 250; - const int waterQuadSize= 2*waterSize/waterTesselation; - const float waterHeight= menuBackground->getWaterHeight(); - - glEnable(GL_BLEND); - - glNormal3f(0.f, 1.f, 0.f); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.f, 1.f, 1.f, 1.f).ptr()); - GLuint waterHandle= static_cast(menuBackground->getWaterTexture())->getHandle(); - glBindTexture(GL_TEXTURE_2D, waterHandle); - for(int i=1; i < waterTesselation; ++i) { - glBegin(GL_TRIANGLE_STRIP); - for(int j=1; j < waterTesselation; ++j) { - glTexCoord2i(1, 2 % j); - glVertex3f(-waterSize+i*waterQuadSize, waterHeight, -waterSize+j*waterQuadSize); - glTexCoord2i(0, 2 % j); - glVertex3f(-waterSize+(i+1)*waterQuadSize, waterHeight, -waterSize+j*waterQuadSize); - } - glEnd(); - } - glDisable(GL_BLEND); - - //raindrops - if(menuBackground->getRain()) { - const float maxRaindropAlpha= 0.5f; - - glEnable(GL_BLEND); - glDisable(GL_LIGHTING); - glDisable(GL_ALPHA_TEST); - glDepthMask(GL_FALSE); - - //splashes - CoreData &coreData= CoreData::getInstance(); - glBindTexture(GL_TEXTURE_2D, static_cast(coreData.getWaterSplashTexture())->getHandle()); - for(int i=0; i< MenuBackground::raindropCount; ++i) { - - Vec2f pos= menuBackground->getRaindropPos(i); - float scale= menuBackground->getRaindropState(i); - float alpha= maxRaindropAlpha-scale*maxRaindropAlpha; - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - glColor4f(1.f, 1.f, 1.f, alpha); - glTranslatef(pos.x, waterHeight+0.01f, pos.y); - - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(0.f, 1.f); - glVertex3f(-scale, 0, scale); - glTexCoord2f(0.f, 0.f); - glVertex3f(-scale, 0, -scale); - glTexCoord2f(1.f, 1.f); - glVertex3f(scale, 0, scale); - glTexCoord2f(1.f, 0.f); - glVertex3f(scale, 0, -scale); - glEnd(); - - glPopMatrix(); - } - } - } - - glPopAttrib(); - - assertGl(); -} - -void Renderer::renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels,const Vec3f characterPosition, float anim) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - assertGl(); - - const Vec3f &cameraPosition= camera->getConstPosition(); - - glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); - - //clear - //Vec4f fogColor= Vec4f(0.4f, 0.4f, 0.4f, 1.f) * fade; - // Show black bacground - Vec4f fogColor= Vec4f(0.f, 0.f, 0.f, 1.f); - glClearColor(fogColor.x, fogColor.y, fogColor.z, 1.f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glFogfv(GL_FOG_COLOR, fogColor.ptr()); - - //light - Vec4f lightPos= Vec4f(10.f, 10.f, 10.f, 1.f) * fade; - Vec4f diffLight= Vec4f(0.9f, 0.9f, 0.9f, 1.f) * fade; - Vec4f ambLight= Vec4f(0.3f, 0.3f, 0.3f, 1.f) * fade; - Vec4f specLight= Vec4f(0.1f, 0.1f, 0.1f, 1.f) * fade; - - glEnable(GL_LIGHT0); - glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); - glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight.ptr()); - glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight.ptr()); - glLightfv(GL_LIGHT0, GL_SPECULAR, specLight.ptr()); - - //main model - if(mainModel) { - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_GREATER, 0.5f); - modelRenderer->begin(true, true, true, false); - mainModel->updateInterpolationData(anim, true); - modelRenderer->render(mainModel); - modelRenderer->end(); - glDisable(GL_ALPHA_TEST); - } - - //characters - if(characterModels.empty() == false) { - float dist= characterPosition.dist(cameraPosition); - float minDist= 3.f; - if(dist < minDist) { - glAlphaFunc(GL_GREATER, 0.0f); - float alpha= clamp((minDist-dist) / minDist, 0.f, 1.f); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); - modelRenderer->begin(true, true, false, false); - - for(unsigned int i = 0; i < characterModels.size(); ++i) { - if(characterModels[i]) { - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glTranslatef(i*2.f-4.f, -1.4f, -7.5f); - characterModels[i]->updateInterpolationData(anim, true); - modelRenderer->render(characterModels[i]); - glPopMatrix(); - } - } - modelRenderer->end(); - } - } - - - glPopAttrib(); - - assertGl(); -} - -// ==================== computing ==================== - -bool Renderer::ccomputePosition(const Vec2i &screenPos, Vec2i &worldPos, bool exactCoords) { - assertGl(); - const Map* map= game->getWorld()->getMap(); - const Metrics &metrics= Metrics::getInstance(); - float depth= 0.0f; - GLdouble modelviewMatrix[16]; - GLdouble projectionMatrix[16]; - GLint viewport[4]= {0, 0, metrics.getScreenW(), metrics.getScreenH()}; - GLdouble worldX; - GLdouble worldY; - GLdouble worldZ; - GLint screenX= (screenPos.x * metrics.getScreenW() / metrics.getVirtualW()); - GLint screenY= (screenPos.y * metrics.getScreenH() / metrics.getVirtualH()); - - //get the depth in the cursor pixel - glReadPixels(screenX, screenY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); - - //load matrices - loadProjectionMatrix(); - loadGameCameraMatrix(); - - //get matrices - glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); - glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); - - //get the world coordinates - gluUnProject( - screenX, screenY, depth, - modelviewMatrix, projectionMatrix, viewport, - &worldX, &worldY, &worldZ); - - //conver coords to int - if(exactCoords == true) { - worldPos= Vec2i(static_cast(worldX), static_cast(worldZ)); - } - else { - worldPos= Vec2i(static_cast(worldX+0.5f), static_cast(worldZ+0.5f)); - } - - //clamp coords to map size - return map->isInside(worldPos); -} - -// This method takes world co-ordinates and translates them to screen co-ords -Vec3f Renderer::computeScreenPosition(const Vec3f &worldPos) { - if(worldToScreenPosCache.find(worldPos) != worldToScreenPosCache.end()) { - return worldToScreenPosCache[worldPos]; - } - assertGl(); - - const Metrics &metrics= Metrics::getInstance(); - GLint viewport[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - GLdouble worldX = worldPos.x; - GLdouble worldY = worldPos.y; - GLdouble worldZ = worldPos.z; - - //load matrices - loadProjectionMatrix(); - loadGameCameraMatrix(); - - //get matrices - GLdouble modelviewMatrix[16]; - glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); - GLdouble projectionMatrix[16]; - glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); - - //get the screen coordinates - GLdouble screenX; - GLdouble screenY; - GLdouble screenZ; - gluProject(worldX, worldY, worldZ, - modelviewMatrix, projectionMatrix, viewport, - &screenX, &screenY, &screenZ); - - Vec3f screenPos(screenX,screenY,screenZ); - worldToScreenPosCache[worldPos]=screenPos; - - return screenPos; -} - -void Renderer::computeSelected( Selection::UnitContainer &units, const Object *&obj, - const bool withObjectSelection, - const Vec2i &posDown, const Vec2i &posUp) { - const string selectionType=toLower(Config::getInstance().getString("SelectionType",Config::colorPicking)); - - if(selectionType==Config::colorPicking) { - selectUsingColorPicking(units,obj, withObjectSelection,posDown, posUp); - } - /// Frustum approach --> Currently not accurate enough - else if(selectionType==Config::frustumPicking) { - selectUsingFrustumSelection(units,obj, withObjectSelection,posDown, posUp); - } - else { - selectUsingSelectionBuffer(units,obj, withObjectSelection,posDown, posUp); - } -} - -void Renderer::selectUsingFrustumSelection(Selection::UnitContainer &units, - const Object *&obj, const bool withObjectSelection, - const Vec2i &posDown, const Vec2i &posUp) { - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - - const Metrics &metrics= Metrics::getInstance(); - GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - - //compute center and dimensions of selection rectangle - int x = (posDown.x+posUp.x) / 2; - int y = (posDown.y+posUp.y) / 2; - int w = abs(posDown.x-posUp.x); - int h = abs(posDown.y-posUp.y); - if(w < 2) { - w = 2; - } - if(h < 2) { - h = 2; - } - - gluPickMatrix(x, y, w, h, view); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); - loadGameCameraMatrix(); - - VisibleQuadContainerCache quadSelectionCacheItem; - ExtractFrustum(quadSelectionCacheItem); - - //pop matrices - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - if(unit != NULL && unit->isAlive()) { - Vec3f unitPos = unit->getCurrMidHeightVector(); - bool insideQuad = CubeInFrustum(quadSelectionCacheItem.frustumData, - unitPos.x, unitPos.y, unitPos.z, unit->getType()->getRenderSize()); - if(insideQuad == true) { - units.push_back(unit); - } - } - } - } - - if(withObjectSelection == true) { - if(qCache.visibleObjectList.empty() == false) { - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleObjectList.size(); ++visibleIndex) { - Object *object = qCache.visibleObjectList[visibleIndex]; - if(object != NULL) { - bool insideQuad = CubeInFrustum(quadSelectionCacheItem.frustumData, - object->getPos().x, object->getPos().y, object->getPos().z, 1); - if(insideQuad == true) { - obj = object; - //if(withObjectSelection == true) { - break; - //} - } - } - } - } - } -} - -void Renderer::selectUsingSelectionBuffer(Selection::UnitContainer &units, - const Object *&obj, const bool withObjectSelection, - const Vec2i &posDown, const Vec2i &posUp) { - //compute center and dimensions of selection rectangle - int x = (posDown.x+posUp.x) / 2; - int y = (posDown.y+posUp.y) / 2; - int w = abs(posDown.x-posUp.x); - int h = abs(posDown.y-posUp.y); - if(w < 2) { - w = 2; - } - if(h < 2) { - h = 2; - } - - //declarations - GLuint selectBuffer[Gui::maxSelBuff]; - - //setup matrices - glSelectBuffer(Gui::maxSelBuff, selectBuffer); - //glMatrixMode(GL_PROJECTION); - //glPushMatrix(); - - GLint renderModeResult = glRenderMode(GL_SELECT); - if(renderModeResult < 0) { - const char *errorString= reinterpret_cast(gluErrorString(renderModeResult)); - char szBuf[8096]=""; - snprintf(szBuf,8096,"OpenGL error #%d [0x%X] : [%s] at file: [%s], line: %d",renderModeResult,renderModeResult,errorString,extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - printf("%s\n",szBuf); - } - - - glPushMatrix(); - glMatrixMode(GL_PROJECTION); - - glLoadIdentity(); - - const Metrics &metrics= Metrics::getInstance(); - GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - //GLint view[4]; - //glGetIntegerv(GL_VIEWPORT, view); - - gluPickMatrix(x, y, w, h, view); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); - //gluPerspective(perspFov, metrics.getAspectRatio(), 0.0001, 1000.0); - //gluPerspective(perspFov, (float)view[2]/(float)view[3], perspNearPlane, perspFarPlane); - loadGameCameraMatrix(); - - //render units to find which ones should be selected - renderUnitsFast(); - if(withObjectSelection == true) { - renderObjectsFast(false,true); - } - - //pop matrices - glPopMatrix(); - - // Added this to ensure all the selection calls are done now - // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) - //glFlush(); - - //select units by checking the selected buffer - int selCount= glRenderMode(GL_RENDER); - if(selCount > 0) { - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int i = 1; i <= selCount; ++i) { - int index = selectBuffer[i*4-1]; - if(index >= OBJECT_SELECT_OFFSET) { - Object *object = qCache.visibleObjectList[index - OBJECT_SELECT_OFFSET]; - if(object != NULL) { - obj = object; - if(withObjectSelection == true) { - break; - } - } - } - else { - Unit *unit = qCache.visibleQuadUnitList[index]; - if(unit != NULL && unit->isAlive()) { - units.push_back(unit); - } - } - } - } - else if(selCount < 0) { - const char *errorString= reinterpret_cast(gluErrorString(selCount)); - char szBuf[8096]=""; - snprintf(szBuf,8096,"OpenGL error #%d [0x%X] : [%s] at file: [%s], line: %d",selCount,selCount,errorString,extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - printf("%s\n",szBuf); - } -} - -void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, - const Object *&obj, const bool withObjectSelection, - const Vec2i &posDown, const Vec2i &posUp) { - int x1 = posDown.x; - int y1 = posDown.y; - int x2 = posUp.x; - int y2 = posUp.y; - - int x = min(x1,x2); - int y = min(y1,y2); - int w = max(x1,x2) - min(x1,x2); - int h = max(y1,y2) - min(y1,y2); - if(w < 2) { - w = 2; - } - if(h < 2) { - h = 2; - } - - const Metrics &metrics= Metrics::getInstance(); - x= (x * metrics.getScreenW() / metrics.getVirtualW()); - y= (y * metrics.getScreenH() / metrics.getVirtualH()); - - w= (w * metrics.getScreenW() / metrics.getVirtualW()); - h= (h * metrics.getScreenH() / metrics.getVirtualH()); - - PixelBufferWrapper::begin(); - - - glPushMatrix(); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - //GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; - //gluPickMatrix(x, y, w, h, view); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); - loadGameCameraMatrix(); - - //render units to find which ones should be selected - //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - vector rendererUnits = renderUnitsFast(false, true); - //printf("In [%s::%s] Line: %d rendererUnits = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rendererUnits.size()); - - - // Added this to ensure all the selection calls are done now - // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) - //glFlush(); - - //GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); - - PixelBufferWrapper::end(); - - vector unitsVector; - bool unitFound=false; - - if(rendererUnits.empty() == false) { - copy(rendererUnits.begin(), rendererUnits.end(), std::inserter(unitsVector, unitsVector.begin())); - } - - if(unitsVector.empty() == false) { - vector pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, unitsVector); - //printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size()); - - if(pickedList.empty() == false) { - units.reserve(pickedList.size()); - for(unsigned int i = 0; i < pickedList.size(); ++i) { - int index = pickedList[i]; - //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size()); - - if(rendererUnits.empty() == false && index < (int)rendererUnits.size()) { - Unit *unit = rendererUnits[index]; - if(unit != NULL && unit->isAlive()) { - unitFound=true; - units.push_back(unit); - } - } - } - } - } - - if(withObjectSelection == true && unitFound==false) { - vector rendererObjects; - vector objectsVector; - rendererObjects = renderObjectsFast(false,true,true); - if(rendererObjects.empty() == false) { - copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(objectsVector, objectsVector.begin())); - } - - if(objectsVector.empty() == false) { - vector pickedList = BaseColorPickEntity::getPickedList(x,y,w,h, objectsVector); - //printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size()); - - if(pickedList.empty() == false) { - for(unsigned int i = 0; i < pickedList.size(); ++i) { - int index = pickedList[i]; - //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size()); - - if(rendererObjects.empty() == false && index < (int)rendererObjects.size()) { - Object *object = rendererObjects[index]; - //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,object); - - if(object != NULL) { - obj = object; - break; - } - } - } - } - } - } - //pop matrices - glPopMatrix(); -} - -// ==================== shadows ==================== - -void Renderer::renderShadowsToTexture(const int renderFps){ - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(shadowsOffDueToMinRender == false && - (shadows == sProjected || shadows == sShadowMapping)) { - - shadowMapFrame= (shadowMapFrame + 1) % (shadowFrameSkip + 1); - - if(shadowMapFrame == 0){ - assertGl(); - - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT | GL_POLYGON_BIT); - - if(shadows == sShadowMapping) { - glClear(GL_DEPTH_BUFFER_BIT); - } - else { - float color= 1.0f-shadowIntensity; - glColor3f(color, color, color); - glClearColor(1.f, 1.f, 1.f, 1.f); - glDisable(GL_DEPTH_TEST); - glClear(GL_COLOR_BUFFER_BIT); - } - - //assertGl(); - - //clear color buffer - // - //set viewport, we leave one texel always in white to avoid problems - glViewport(1, 1, shadowTextureSize-2, shadowTextureSize-2); - - //assertGl(); - - if(nearestLightPos.w == 0.f) { - //directional light - - //light pos - assert(game != NULL); - assert(game->getWorld() != NULL); - const TimeFlow *tf= game->getWorld()->getTimeFlow(); - assert(tf != NULL); - float ang= tf->isDay()? computeSunAngle(tf->getTime()): computeMoonAngle(tf->getTime()); - ang= radToDeg(ang); - - //push and set projection - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - - //assertGl(); - - if(game->getGameCamera()->getState()==GameCamera::sGame){ - //glOrtho(-35, 5, -15, 15, -1000, 1000); - //glOrtho(-30, 30, -20, 20, -1000, 1000); - glOrtho(-30, 5, -20, 20, -1000, 1000); - } - else{ - glOrtho(-30, 30, -20, 20, -1000, 1000); - } - - //assertGl(); - - //push and set modelview - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - - glRotatef(15, 0, 1, 0); - - glRotatef(ang, 1, 0, 0); - glRotatef(90, 0, 1, 0); - const Vec3f &pos= game->getGameCamera()->getPos(); - - glTranslatef(static_cast(-pos.x), 0, static_cast(-pos.z)); - - //assertGl(); - } - else { - //non directional light - - //push projection - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - - //assertGl(); - - gluPerspective(perspFov, 1.f, perspNearPlane, perspFarPlane); - //const Metrics &metrics= Metrics::getInstance(); - //gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); - - - assertGl(); - - //push modelview - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); - glRotatef(-90, -1, 0, 0); - glTranslatef(-nearestLightPos.x, -nearestLightPos.y-2, -nearestLightPos.z); - - //assertGl(); - } - - if(shadows == sShadowMapping) { - glEnable(GL_POLYGON_OFFSET_FILL); - glPolygonOffset(1.0f, 16.0f); - - //assertGl(); - } - - //render 3d - renderUnitsFast(true); - renderObjectsFast(true,false); - - //assertGl(); - - //read color buffer - glBindTexture(GL_TEXTURE_2D, shadowMapHandle); - assertGl(); - glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowTextureSize, shadowTextureSize); - GLenum error = glGetError(); - // This error can happen when a Linux user switches from an X session - // back to a running game, and 'seems' to be safe to ignore it - if(error != GL_INVALID_OPERATION) { - assertGlWithErrorNumber(error); - } - - //get elemental matrices - static Matrix4f matrix1; - static bool matrix1Populate = true; - if(matrix1Populate == true) { - matrix1Populate = false; - matrix1[0]= 0.5f; matrix1[4]= 0.f; matrix1[8]= 0.f; matrix1[12]= 0.5f; - matrix1[1]= 0.f; matrix1[5]= 0.5f; matrix1[9]= 0.f; matrix1[13]= 0.5f; - matrix1[2]= 0.f; matrix1[6]= 0.f; matrix1[10]= 0.5f; matrix1[14]= 0.5f; - matrix1[3]= 0.f; matrix1[7]= 0.f; matrix1[11]= 0.f; matrix1[15]= 1.f; - } - Matrix4f matrix2; - glGetFloatv(GL_PROJECTION_MATRIX, matrix2.ptr()); - - //assertGl(); - - Matrix4f matrix3; - glGetFloatv(GL_MODELVIEW_MATRIX, matrix3.ptr()); - - //pop both matrices - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - - //assertGl(); - - //compute texture matrix - glLoadMatrixf(matrix1.ptr()); - glMultMatrixf(matrix2.ptr()); - glMultMatrixf(matrix3.ptr()); - glGetFloatv(GL_TRANSPOSE_PROJECTION_MATRIX_ARB, shadowMapMatrix.ptr()); - - //assertGl(); - - //if(shadows == sShadowMapping) { - // glDisable(GL_POLYGON_OFFSET_FILL); - // glPolygonOffset(0.0f, 0.0f); - //} - - //pop - glPopMatrix(); - - //assertGl(); - - glPopAttrib(); - - assertGl(); - } - } -} - - -// ==================== gl wrap ==================== - -string Renderer::getGlInfo(){ - string infoStr=""; - Lang &lang= Lang::getInstance(); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - infoStr+= lang.getString("OpenGlInfo")+":\n"; - infoStr+= " "+lang.getString("OpenGlVersion")+": "; - infoStr+= string((getGlVersion() != NULL ? getGlVersion() : "?"))+"\n"; - infoStr+= " "+lang.getString("OpenGlRenderer")+": "; - infoStr+= string((getGlVersion() != NULL ? getGlVersion() : "?"))+"\n"; - infoStr+= " "+lang.getString("OpenGlVendor")+": "; - infoStr+= string((getGlVendor() != NULL ? getGlVendor() : "?"))+"\n"; - infoStr+= " "+lang.getString("OpenGlMaxLights")+": "; - infoStr+= intToStr(getGlMaxLights())+"\n"; - infoStr+= " "+lang.getString("OpenGlMaxTextureSize")+": "; - infoStr+= intToStr(getGlMaxTextureSize())+"\n"; - infoStr+= " "+lang.getString("OpenGlMaxTextureUnits")+": "; - infoStr+= intToStr(getGlMaxTextureUnits())+"\n"; - infoStr+= " "+lang.getString("OpenGlModelviewStack")+": "; - infoStr+= intToStr(getGlModelviewMatrixStackDepth())+"\n"; - infoStr+= " "+lang.getString("OpenGlProjectionStack")+": "; - infoStr+= intToStr(getGlProjectionMatrixStackDepth())+"\n"; - } - return infoStr; -} - -string Renderer::getGlMoreInfo(){ - string infoStr=""; - Lang &lang= Lang::getInstance(); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - //gl extensions - infoStr+= lang.getString("OpenGlExtensions")+":\n "; - - string extensions= getGlExtensions(); - int charCount= 0; - for(int i = 0; i < (int)extensions.size(); ++i) { - infoStr+= extensions[i]; - if(charCount>120 && extensions[i]==' '){ - infoStr+= "\n "; - charCount= 0; - } - ++charCount; - } - - //platform extensions - infoStr+= "\n\n"; - infoStr+= lang.getString("OpenGlPlatformExtensions")+":\n "; - - charCount= 0; - string platformExtensions= getGlPlatformExtensions(); - for(int i = 0; i < (int)platformExtensions.size(); ++i) { - infoStr+= platformExtensions[i]; - if(charCount>120 && platformExtensions[i]==' '){ - infoStr+= "\n "; - charCount= 0; - } - ++charCount; - } - } - - return infoStr; -} - -void Renderer::autoConfig() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - Config &config= Config::getInstance(); - - bool nvidiaCard= toLower(getGlVendor()).find("nvidia")!=string::npos; - bool atiCard= toLower(getGlVendor()).find("ati")!=string::npos; - //bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow") && isGlExtensionSupported("GL_ARB_shadow_ambient"); - bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow"); - - //3D textures - config.setBool("Textures3D", isGlExtensionSupported("GL_EXT_texture3D")); - - //shadows - string shadows=""; - if(getGlMaxTextureUnits()>=3){ - if(nvidiaCard && shadowExtensions){ - shadows= shadowsToStr(sShadowMapping); - } - else{ - shadows= shadowsToStr(sProjected); - } - } - else{ - shadows=shadowsToStr(sDisabled); - } - config.setString("Shadows", shadows); - - //lights - config.setInt("MaxLights", atiCard? 1: 4); - - //filter - config.setString("Filter", "Bilinear"); - } -} - -void Renderer::clearBuffers() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); -} - -void Renderer::clearZBuffer() { - glClear(GL_DEPTH_BUFFER_BIT); -} - -void Renderer::loadConfig() { - Config &config= Config::getInstance(); - - //cache most used config params - maxLights= config.getInt("MaxLights"); - photoMode= config.getBool("PhotoMode"); - focusArrows= config.getBool("FocusArrows"); - textures3D= config.getBool("Textures3D"); - float gammaValue=config.getFloat("GammaValue","0.0"); - if(this->program == NULL) { - throw megaglest_runtime_error("this->program == NULL"); - } - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - //if(this->program != NULL) { - if(gammaValue != 0.0) { - this->program->getWindow()->setGamma(gammaValue); - } - //} - } - //load shadows - shadows= strToShadows(config.getString("Shadows")); - if(shadows==sProjected || shadows==sShadowMapping){ - shadowTextureSize= config.getInt("ShadowTextureSize"); - shadowFrameSkip= config.getInt("ShadowFrameSkip"); - shadowIntensity= config.getFloat("ShadowIntensity","1.0"); - } - - //load filter settings - Texture2D::Filter textureFilter= strToTextureFilter(config.getString("Filter")); - int maxAnisotropy= config.getInt("FilterMaxAnisotropy"); - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { - for(int i=0; isetFilter(textureFilter); - textureManager[i]->setMaxAnisotropy(maxAnisotropy); - } - } -} - -//Texture2D *Renderer::saveScreenToTexture(int x, int y, int width, int height) { -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// Config &config= Config::getInstance(); -// Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); -// int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); -// -// Texture2D *texture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); -// texture->setForceCompressionDisabled(true); -// texture->setMipmap(false); -// Pixmap2D *pixmapScreenShot = texture->getPixmap(); -// pixmapScreenShot->init(width, height, 3); -// texture->init(textureFilter,maxAnisotropy); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// //glFinish(); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// glReadPixels(x, y, pixmapScreenShot->getW(), pixmapScreenShot->getH(), -// GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// return texture; -//} - -void Renderer::saveScreen(const string &path,int w, int h) { - const Metrics &sm= Metrics::getInstance(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - Pixmap2D *pixmapScreenShot = new Pixmap2D(sm.getScreenW(),sm.getScreenH(), 3); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //glFinish(); - - glPixelStorei(GL_PACK_ALIGNMENT, 1); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - glReadPixels(0, 0, pixmapScreenShot->getW(), pixmapScreenShot->getH(), - GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); - - if(w==0 || h==0){ - h=sm.getScreenH(); - w=sm.getScreenW(); - } - else{ - pixmapScreenShot->Scale(GL_RGB,w,h); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - // Signal the threads queue to add a screenshot save request - MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor,string(extractFileFromDirectoryPath(__FILE__).c_str()) + "_" + intToStr(__LINE__)); - saveScreenQueue.push_back(make_pair(path,pixmapScreenShot)); - safeMutex.ReleaseLock(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -unsigned int Renderer::getSaveScreenQueueSize() { - MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor,string(extractFileFromDirectoryPath(__FILE__).c_str()) + "_" + intToStr(__LINE__)); - int queueSize = (int)saveScreenQueue.size(); - safeMutex.ReleaseLock(); - - return queueSize; -} - -// ==================== PRIVATE ==================== - -float Renderer::computeSunAngle(float time) { - - float dayTime= TimeFlow::dusk-TimeFlow::dawn; - float fTime= (time-TimeFlow::dawn)/dayTime; - return clamp(fTime*pi, pi/8.f, 7.f*pi/8.f); -} - -float Renderer::computeMoonAngle(float time) { - float nightTime= 24-(TimeFlow::dusk-TimeFlow::dawn); - - if(time Renderer::renderUnitsFast(bool renderingShadows, bool colorPickingSelection) { - vector unitsList; - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return unitsList; - } - - assert(game != NULL); - //const World *world= game->getWorld(); - //assert(world != NULL); - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleQuadUnitList.empty() == false) { - if(colorPickingSelection == true) { - unitsList.reserve(qCache.visibleQuadUnitList.size()); - } - - bool modelRenderStarted = false; - bool renderOnlyBuildings=true; - for(int k=0; k<2 ;k++) { - if(k==0){ - //glDisable(GL_DEPTH_TEST); - renderOnlyBuildings=true; - } - else { - if(colorPickingSelection == true){ - // clear depth buffer to get units behind buildings rendered in front of them - glClear(GL_DEPTH_BUFFER_BIT); - } - //glEnable(GL_DEPTH_TEST); - renderOnlyBuildings=false; - } - for(int visibleUnitIndex = 0; - visibleUnitIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { - Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; - - if(renderingShadows==false && unit->isAlive()==false){ - // no need to render dead units for selection - continue; - } - - if(renderOnlyBuildings==true && unit->getType()->hasSkillClass(scMove)){ - continue; - } - - if(renderOnlyBuildings==false && !unit->getType()->hasSkillClass(scMove)){ - continue; - } - - if(modelRenderStarted == false) { - modelRenderStarted = true; - - if(colorPickingSelection == false) { - //glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); - glDisable(GL_LIGHTING); - if (renderingShadows == false) { - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_TEXTURE_2D); - } - else { - glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); - glEnable(GL_TEXTURE_2D); - glAlphaFunc(GL_GREATER, 0.4f); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - //set color to the texture alpha - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - //set alpha to the texture alpha - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - } - } - - modelRenderer->begin(false, renderingShadows, false, colorPickingSelection); - - if(colorPickingSelection == false) { - glInitNames(); - } - } - - if(colorPickingSelection == false) { - glPushName(visibleUnitIndex); - } - - //assertGl(); - - glMatrixMode(GL_MODELVIEW); - //debuxar modelo - glPushMatrix(); - - //translate - Vec3f currVec= unit->getCurrVectorFlat(); - glTranslatef(currVec.x, currVec.y, currVec.z); - - //rotate - glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); - - //render - Model *model= unit->getCurrentModelPtr(); - //if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) { - - // ***MV don't think this is needed below 2013/01/11 - model->updateInterpolationVertices(unit->getAnimProgressAsFloat(), unit->isAlive() && !unit->isAnimProgressBound()); - - //} - - if(colorPickingSelection == true) { - unit->setUniquePickingColor(); - unitsList.push_back(unit); - } - - modelRenderer->render(model,rmSelection); - - glPopMatrix(); - - if(colorPickingSelection == false) { - glPopName(); - } - } - } - - if(modelRenderStarted == true) { - modelRenderer->end(); - if(colorPickingSelection == false) { - glPopAttrib(); - } - } - } - //glDisable(GL_DEPTH_TEST); - return unitsList; -} - -//render objects for selection purposes -vector Renderer::renderObjectsFast(bool renderingShadows, bool resourceOnly, - bool colorPickingSelection) { - vector objectList; - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return objectList; - } - - VisibleQuadContainerCache &qCache = getQuadCache(); - if(qCache.visibleObjectList.empty() == false) { - if(colorPickingSelection == true) { - objectList.reserve(qCache.visibleObjectList.size()); - } - - bool modelRenderStarted = false; - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleObjectList.size(); ++visibleIndex) { - Object *o = qCache.visibleObjectList[visibleIndex]; - - if(modelRenderStarted == false) { - modelRenderStarted = true; - - if(colorPickingSelection == false) { - glDisable(GL_LIGHTING); - - if (renderingShadows == false){ - glPushAttrib(GL_ENABLE_BIT); - glDisable(GL_TEXTURE_2D); - } - else { - glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); - glAlphaFunc(GL_GREATER, 0.5f); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); - - //set color to the texture alpha - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); - - //set alpha to the texture alpha - glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); - glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); - glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); - } - } - - modelRenderer->begin(false, renderingShadows, false, colorPickingSelection); - - if(colorPickingSelection == false) { - glInitNames(); - } - } - - if(resourceOnly == false || o->getResource()!= NULL) { - Model *objModel= o->getModelPtr(); - //if(this->gameCamera->getPos().dist(o->getPos()) <= SKIP_INTERPOLATION_DISTANCE) { - - // ***MV don't think this is needed below 2013/01/11 - //objModel->updateInterpolationData(o->getAnimProgress(), true); - - //} - const Vec3f v= o->getConstPos(); - - if(colorPickingSelection == false) { - glPushName(OBJECT_SELECT_OFFSET+visibleIndex); - } + float fowFactor = fowTexPixmap->getPixelf(o->getMapPos().x / Map::cellScale, o->getMapPos().y / Map::cellScale); + Vec4f color = Vec4f(Vec3f(fowFactor), 1.f); + glColor4fv(color.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (color * ambFactor).ptr()); + glFogfv(GL_FOG_COLOR, (baseFogColor * fowFactor).ptr()); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef(v.x, v.y, v.z); glRotatef(o->getRotation(), 0.f, 1.f, 0.f); - if(colorPickingSelection == true) { - o->setUniquePickingColor(); - objectList.push_back(o); + //We use OpenGL Lights so no manual action is needed here. In fact this call did bad things on lighting big rocks for example + // if(o->getRotation() != 0.0) { + // setupLightingForRotatedModel(); + // } + + //objModel->updateInterpolationData(0.f, true); + //if(this->gameCamera->getPos().dist(o->getPos()) <= SKIP_INTERPOLATION_DISTANCE) { + + + if (tilesetObjectsToAnimate == -1) { + objModel->updateInterpolationData(o->getAnimProgress(), true); + } else if (tilesetObjectsToAnimate > 0 && o->isAnimated()) { + tilesetObjectsToAnimate--; + objModel->updateInterpolationData(o->getAnimProgress(), true); + } else { + objModel->updateInterpolationData(0, true); } - modelRenderer->render(objModel,resourceOnly?rmSelection:rmNormal); + // objModel->updateInterpolationData(o->getAnimProgress(), true); + //} + modelRenderer->render(objModel); + + triangleCount += objModel->getTriangleCount(); + pointCount += objModel->getVertexCount(); glPopMatrix(); - - if(colorPickingSelection == false) { - glPopName(); - } } + + if (modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); + } + + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + + assertGl(); } - if(modelRenderStarted == true) { - modelRenderer->end(); + void Renderer::renderWater() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - if(colorPickingSelection == false) { + const World *world = game->getWorld(); + const Map *map = world->getMap(); + + const Texture2D *fowTex = world->getMinimap()->getFowTexture(); + if (fowTex == NULL) { + return; + } + + float waterAnim = world->getWaterEffects()->getAmin(); + + //assert + assertGl(); + + glPushAttrib(GL_TEXTURE_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT); + + //water texture nit + glDisable(GL_TEXTURE_2D); + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + if (textures3D) { + Texture3D *waterTex = world->getTileset()->getWaterTex(); + if (waterTex == NULL) { + throw megaglest_runtime_error("waterTex == NULL"); + } + glEnable(GL_TEXTURE_3D); + glBindTexture(GL_TEXTURE_3D, static_cast(waterTex)->getHandle()); + } else { + glEnable(GL_COLOR_MATERIAL); + glColor4f(0.5f, 0.5f, 1.0f, 0.5f); + glBindTexture(GL_TEXTURE_3D, 0); + } + + assertGl(); + + //fog of War texture Unit + //const Texture2D *fowTex= world->getMinimap()->getFowTexture(); + glActiveTexture(fowTexUnit); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(fowTex)->getHandle()); + glActiveTexture(baseTexUnit); + + assertGl(); + + int thisTeamIndex = world->getThisTeamIndex(); + bool cellExplored = world->showWorldForPlayer(world->getThisFactionIndex()); + bool closed = false; + + Rect2i boundingRect = visibleQuad.computeBoundingRect(); + Rect2i scaledRect = boundingRect / Map::cellScale; + scaledRect.clamp(0, 0, map->getSurfaceW() - 1, map->getSurfaceH() - 1); + + float waterLevel = world->getMap()->getWaterLevel(); + for (int j = scaledRect.p[0].y; j < scaledRect.p[1].y; ++j) { + glBegin(GL_TRIANGLE_STRIP); + + for (int i = scaledRect.p[0].x; i <= scaledRect.p[1].x; ++i) { + SurfaceCell *tc0 = map->getSurfaceCell(i, j); + SurfaceCell *tc1 = map->getSurfaceCell(i, j + 1); + if (tc0 == NULL) { + throw megaglest_runtime_error("tc0 == NULL"); + } + if (tc1 == NULL) { + throw megaglest_runtime_error("tc1 == NULL"); + } + + if (cellExplored == false) { + cellExplored = (tc0->isExplored(thisTeamIndex) || tc1->isExplored(thisTeamIndex)); + } + + if (cellExplored == true && tc0->getNearSubmerged()) { + glNormal3f(0.f, 1.f, 0.f); + closed = false; + + triangleCount += 2; + pointCount += 2; + + //vertex 1 + glMaterialfv( + GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, + computeWaterColor(waterLevel, tc1->getHeight()).ptr()); + glMultiTexCoord2fv(GL_TEXTURE1, tc1->getFowTexCoord().ptr()); + glTexCoord3f(i, 1.f, waterAnim); + glVertex3f( + static_cast(i)*Map::mapScale, + waterLevel, + static_cast(j + 1)*Map::mapScale); + + //vertex 2 + glMaterialfv( + GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, + computeWaterColor(waterLevel, tc0->getHeight()).ptr()); + glMultiTexCoord2fv(GL_TEXTURE1, tc0->getFowTexCoord().ptr()); + glTexCoord3f(i, 0.f, waterAnim); + glVertex3f( + static_cast(i)*Map::mapScale, + waterLevel, + static_cast(j)*Map::mapScale); + + } else { + if (closed == false) { + pointCount += 2; + + //vertex 1 + glMaterialfv( + GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, + computeWaterColor(waterLevel, tc1->getHeight()).ptr()); + glMultiTexCoord2fv(GL_TEXTURE1, tc1->getFowTexCoord().ptr()); + glTexCoord3f(i, 1.f, waterAnim); + glVertex3f( + static_cast(i)*Map::mapScale, + waterLevel, + static_cast(j + 1)*Map::mapScale); + + //vertex 2 + glMaterialfv( + GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, + computeWaterColor(waterLevel, tc0->getHeight()).ptr()); + glMultiTexCoord2fv(GL_TEXTURE1, tc0->getFowTexCoord().ptr()); + glTexCoord3f(i, 0.f, waterAnim); + glVertex3f( + static_cast(i)*Map::mapScale, + waterLevel, + static_cast(j)*Map::mapScale); + + glEnd(); + glBegin(GL_TRIANGLE_STRIP); + closed = true; + } + } + } + glEnd(); + } + + //restore + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderTeamColorCircle() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + Vec3f currVec = unit->getCurrVectorFlat(); + Vec3f color = unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + glColor4f(color.x, color.y, color.z, 0.7f); + renderSelectionCircle(currVec, unit->getType()->getSize(), 0.8f, 0.05f); + } glPopAttrib(); } } - } - return objectList; -} - -// ==================== gl caps ==================== - -void Renderer::checkGlCaps() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(glActiveTexture == NULL) { - string message; - - message += "Your system supports OpenGL version ["; - message += getGlVersion() + string("]\n"); - message += "ZetaGlest needs a version that supports\n"; - message += "glActiveTexture (OpenGL 1.3) or the ARB_multitexture extension."; - - throw megaglest_runtime_error(message.c_str(),true); - } - - //opengl 2.1 - if(glewIsSupported("GL_VERSION_2_1") == false) { - string message; - - message += "Your system supports OpenGL version ["; - message += getGlVersion() + string("]\n"); - message += "ZetaGlest needs at least version 2.1 to work\n"; - message += "You may solve this problem by installing your latest video card drivers"; - - throw megaglest_runtime_error(message.c_str(),true); - } - - //opengl 1.4 or extension - //if(!isGlVersionSupported(1, 4, 0)){ - if(glewIsSupported("GL_VERSION_1_4") == false) { - checkExtension("GL_ARB_texture_env_crossbar", "ZetaGlest"); - } -} - -void Renderer::checkGlOptionalCaps() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //shadows - if(shadows == sProjected || shadows == sShadowMapping) { - if(getGlMaxTextureUnits() < 3) { - throw megaglest_runtime_error("Your system doesn't support 3 texture units, required for shadows"); - } - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //shadow mapping - if(shadows == sShadowMapping) { - checkExtension("GL_ARB_shadow", "Shadow Mapping"); - //checkExtension("GL_ARB_shadow_ambient", "Shadow Mapping"); - //checkExtension("GL_ARB_depth_texture", "Shadow Mapping"); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void Renderer::checkExtension(const string &extension, const string &msg) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - if(!isGlExtensionSupported(extension.c_str())) { - string str= "OpenGL extension not supported: " + extension + ", required for " + msg; - throw megaglest_runtime_error(str); - } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -// ==================== init 3d lists ==================== - -void Renderer::init3dList() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - render3dSetup(); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //const Metrics &metrics= Metrics::getInstance(); - - //assertGl(); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //list3d= glGenLists(1); - //assertGl(); - //list3dValid=true; - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //glNewList(list3d, GL_COMPILE_AND_EXECUTE); - //need to execute, because if not gluPerspective takes no effect and gluLoadMatrix is wrong - //render3dSetup(); - //glEndList(); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //assert - //assertGl(); -} - -void Renderer::render3dSetup() { - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - const Metrics &metrics= Metrics::getInstance(); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //misc - glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); - glClearColor(fowColor.x, fowColor.y, fowColor.z, fowColor.w); - glFrontFace(GL_CW); - glEnable(GL_CULL_FACE); - loadProjectionMatrix(); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //texture state - glActiveTexture(shadowTexUnit); - glDisable(GL_TEXTURE_2D); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - glActiveTexture(fowTexUnit); - glDisable(GL_TEXTURE_2D); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - glActiveTexture(baseTexUnit); - glEnable(GL_TEXTURE_2D); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //material state - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - glColor4fv(defColor.ptr()); - - //blend state - glDisable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //alpha test state - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_GREATER, 0.f); - - //depth test state - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - glDepthFunc(GL_LESS); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //lighting state - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - - //matrix mode - glMatrixMode(GL_MODELVIEW); - - //stencil test - glDisable(GL_STENCIL_TEST); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //fog - const Tileset *tileset= NULL; - if(game != NULL && game->getWorld() != NULL) { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - tileset = game->getWorld()->getTileset(); - } - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(tileset != NULL && tileset->getFog()) { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - glEnable(GL_FOG); - if(tileset->getFogMode()==fmExp) { - glFogi(GL_FOG_MODE, GL_EXP); - } - else { - glFogi(GL_FOG_MODE, GL_EXP2); - } - - glFogf(GL_FOG_DENSITY, tileset->getFogDensity()); - glFogfv(GL_FOG_COLOR, tileset->getFogColor().ptr()); - } - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void Renderer::init2dList() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - -// //this list sets the state for the 2d rendering -// list2d= glGenLists(1); -// assertGl(); -// list2dValid=true; -// -// glNewList(list2d, GL_COMPILE); -// render2dMenuSetup(); -// glEndList(); -// -// assertGl(); -} - -void Renderer::render2dMenuSetup() { - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - const Metrics &metrics= Metrics::getInstance(); - //projection - glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, metrics.getVirtualW(), 0, metrics.getVirtualH(), 0, 1); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //modelview - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //disable everything - glDisable(GL_BLEND); - glDisable(GL_LIGHTING); - glDisable(GL_ALPHA_TEST); - glDisable(GL_DEPTH_TEST); - glDisable(GL_STENCIL_TEST); - glDisable(GL_FOG); - glDisable(GL_CULL_FACE); - glFrontFace(GL_CCW); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(glActiveTexture != NULL) { - glActiveTexture(baseTexUnit); - } - else { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s] Line: %d\nglActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - throw megaglest_runtime_error(szBuf); - } - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - glDisable(GL_TEXTURE_2D); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //blend func - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //color - glColor4f(1.f, 1.f, 1.f, 1.f); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -//void Renderer::init3dListMenu(const MainMenu *mm) { -// if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { -// return; -// } -// -// //this->mm3d = mm; -// //printf("In [%s::%s Line: %d] this->custom_mm3d [%p] this->mm3d [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->custom_mm3d,this->mm3d); -// -///* -// assertGl(); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// const Metrics &metrics= Metrics::getInstance(); -// //const MenuBackground *mb= mm->getConstMenuBackground(); -// const MenuBackground *mb = NULL; -// if(mm != NULL) { -// mb = mm->getConstMenuBackground(); -// } -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// if(this->customlist3dMenu != NULL) { -// *this->customlist3dMenu = glGenLists(1); -// assertGl(); -// } -// else { -// list3dMenu= glGenLists(1); -// assertGl(); -// list3dMenuValid=true; -// } -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// if(this->customlist3dMenu != NULL) { -// glNewList(*this->customlist3dMenu, GL_COMPILE); -// } -// else { -// glNewList(list3dMenu, GL_COMPILE); -// } -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// //misc -// glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); -// glClearColor(0.4f, 0.4f, 0.4f, 1.f); -// glFrontFace(GL_CW); -// glEnable(GL_CULL_FACE); -// glMatrixMode(GL_PROJECTION); -// glLoadIdentity(); -// gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, 1000000); -// -// //texture state -// glEnable(GL_TEXTURE_2D); -// glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); -// -// //material state -// glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); -// glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); -// glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); -// glColor4fv(defColor.ptr()); -// glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); -// -// //blend state -// glDisable(GL_BLEND); -// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); -// -// //alpha test state -// glEnable(GL_ALPHA_TEST); -// glAlphaFunc(GL_GREATER, 0.f); -// -// //depth test state -// glEnable(GL_DEPTH_TEST); -// glDepthMask(GL_TRUE); -// glDepthFunc(GL_LESS); -// -// //lighting state -// glEnable(GL_LIGHTING); -// -// //matrix mode -// glMatrixMode(GL_MODELVIEW); -// -// //stencil test -// glDisable(GL_STENCIL_TEST); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// //fog -// if(mb != NULL && mb->getFog()){ -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// glEnable(GL_FOG); -// glFogi(GL_FOG_MODE, GL_EXP2); -// glFogf(GL_FOG_DENSITY, mb->getFogDensity()); -// } -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// glEndList(); -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -// -// //assert -// assertGl(); -//*/ -//} - -void Renderer::render3dMenuSetup(const MainMenu *mm) { - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - const Metrics &metrics= Metrics::getInstance(); - const MenuBackground *mb = NULL; - if(mm != NULL) { - mb = mm->getConstMenuBackground(); - } - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //misc - glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); - glClearColor(0.4f, 0.4f, 0.4f, 1.f); - glFrontFace(GL_CW); - glEnable(GL_CULL_FACE); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, 1000000); - - //texture state - glEnable(GL_TEXTURE_2D); - glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); - - //material state - glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); - glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); - glColor4fv(defColor.ptr()); - glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); - - //blend state - glDisable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //alpha test state - glEnable(GL_ALPHA_TEST); - glAlphaFunc(GL_GREATER, 0.f); - - //depth test state - glEnable(GL_DEPTH_TEST); - glDepthMask(GL_TRUE); - glDepthFunc(GL_LESS); - - //lighting state - glEnable(GL_LIGHTING); - - //matrix mode - glMatrixMode(GL_MODELVIEW); - - //stencil test - glDisable(GL_STENCIL_TEST); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //fog - if(mb != NULL && mb->getFog()) { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - glEnable(GL_FOG); - glFogi(GL_FOG_MODE, GL_EXP2); - glFogf(GL_FOG_DENSITY, mb->getFogDensity()); - } - - //assert - assertGl(); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} -// ==================== misc ==================== - -void Renderer::loadProjectionMatrix() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - GLdouble clipping; - const Metrics &metrics= Metrics::getInstance(); - - assertGl(); - - clipping= photoMode ? perspFarPlane*100 : perspFarPlane; - - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, clipping); - - assertGl(); -} - -void Renderer::enableProjectiveTexturing() { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - glTexGenfv(GL_S, GL_EYE_PLANE, &shadowMapMatrix[0]); - glTexGenfv(GL_T, GL_EYE_PLANE, &shadowMapMatrix[4]); - glTexGenfv(GL_R, GL_EYE_PLANE, &shadowMapMatrix[8]); - glTexGenfv(GL_Q, GL_EYE_PLANE, &shadowMapMatrix[12]); - glEnable(GL_TEXTURE_GEN_S); - glEnable(GL_TEXTURE_GEN_T); - glEnable(GL_TEXTURE_GEN_R); - glEnable(GL_TEXTURE_GEN_Q); -} - -// ==================== private aux drawing ==================== -void Renderer::renderHealthBar(Vec3f v, Unit *unit, float height, bool lineBorder, const Texture2D *texture, const Texture2D *backgroundTexture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - int numberOfBars=1; - int barCount=0; - float hp=unit->getHpRatio(); - float ep=-1.f; - if(unit->getType()->getTotalMaxEp(unit->getTotalUpgrade()) !=0 ) { - ep=unit->getEpRatio(); - numberOfBars++; - } - int productionPercent=unit->getProductionPercent(); - if(productionPercent!=-1) { - numberOfBars++; - } - int size=unit->getType()->getSize(); - - - Vec3f rightVector; - Vec3f upVector; - Vec3f rightVectorTexture; - Vec3f upVectorTexture; - v.y+=1; - float modelview[16]; - float width=(float)size/6+0.25f; - float red; - float green; - float brightness=0.8f; - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - // get the current modelview state - glGetFloatv(GL_MODELVIEW_MATRIX , modelview); - rightVector= Vec3f(modelview[0], modelview[4], modelview[8]); - upVector= Vec3f(modelview[1], modelview[5], modelview[9]); - rightVectorTexture=rightVector*2; - upVectorTexture=upVector*4; - - //from green to yellow to red - - if(hp >= 0.75f) { - green=1; - red=1-((2*hp-1)-0.5f); - } else { - red=1; - green=0.5f+(2*hp-1); - } - - if(red>1.0f) red=1.0f; - if(green>1.0f) green=1.0f; - float yOffset=(float)numberOfBars/2.f; - - if(backgroundTexture!=NULL) { - //backgroundTexture - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(backgroundTexture)->getHandle()); - glColor4f(1.f,1.f,1.f,1.f); - //glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); - glBegin(GL_QUADS); - glTexCoord2i(0,1); - glVertex3fv((v - (rightVectorTexture*width - upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(0,0); - glVertex3fv((v - (rightVectorTexture*width + upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(1,0); - glVertex3fv((v + (rightVectorTexture*width - upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(1,1); - glVertex3fv((v + (rightVectorTexture*width + upVectorTexture*height*yOffset)).ptr()); - glEnd(); - glDisable(GL_TEXTURE_2D); - } - - //healthbar - glColor4f(red*brightness,green*brightness,0.0f,0.4f); - //hpbar - barCount++; - internalRenderHp(numberOfBars,barCount,hp,v,width,height,rightVector,upVector); - - - if(ep > -1.0f) { - //epbar - barCount++; - //glColor4f(brightness,0,brightness,0.5f); - glColor4f(.15f*brightness,0.3f*brightness,0.8f*brightness,0.7f); - internalRenderHp(numberOfBars,barCount,ep,v,width,height,rightVector,upVector); - } - if(productionPercent!=-1) { - barCount++; - glColor4f(brightness,0,brightness,0.6f); - //glColor4f(0.0f*brightness,0.4f*brightness,0.2f*brightness,0.8f); - internalRenderHp(numberOfBars,barCount,(float)productionPercent/100,v,width,height,rightVector,upVector); - } - - -// glBegin(GL_QUADS); -// if(ep < -2.0f) { -// //hpbar -// glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); -// glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); -// glVertex3fv((v + (rightVector*hp*width - upVector*height)).ptr()); -// glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); -// -// } else { -// //hpbar -// glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); -// glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); -// glVertex3fv((v + (rightVector*hp*width - upVector*height*0.0f)).ptr()); -// glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); -// //epbar -// glColor4f(brightness,0,brightness,0.4f); -// glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); -// glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); -// glVertex3fv((v + (rightVector*ep*width - upVector*height)).ptr()); -// glVertex3fv((v + (rightVector*ep*width - upVector*height*0.0f)).ptr()); -// } -// glEnd(); - - if(lineBorder) { - //border - glColor4f(red*brightness,green*brightness,0.1f*brightness,0.5f); - glBegin(GL_LINE_LOOP); - glVertex3fv((v - (rightVector*width - upVector*height*yOffset)).ptr()); - glVertex3fv((v - (rightVector*width + upVector*height*yOffset)).ptr()); - glVertex3fv((v + (rightVector*width - upVector*height*yOffset)).ptr()); - glVertex3fv((v + (rightVector*width + upVector*height*yOffset)).ptr()); - glEnd(); - } - - if(texture!=NULL) { - //BorderTexture - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); - glColor4f(1.f,1.f,1.f,1.f); - //glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); - glBegin(GL_QUADS); - glTexCoord2i(0,1); - glVertex3fv((v - (rightVectorTexture*width - upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(0,0); - glVertex3fv((v - (rightVectorTexture*width + upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(1,0); - glVertex3fv((v + (rightVectorTexture*width - upVectorTexture*height*yOffset)).ptr()); - glTexCoord2i(1,1); - glVertex3fv((v + (rightVectorTexture*width + upVectorTexture*height*yOffset)).ptr()); - glEnd(); - glDisable(GL_TEXTURE_2D); - } - - glPopMatrix(); -} - -void Renderer::internalRenderHp(int numberOfBars, int barNumber, float hp, - Vec3f posVector, float width, float singleHPheight, Vec3f rightVector, Vec3f upVector) { - - float yOffset=(float)numberOfBars*singleHPheight/2; - float offsetTop=yOffset-singleHPheight*(barNumber-1); - float offsetBottom=yOffset-singleHPheight*barNumber; - offsetBottom=offsetBottom*-1; - hp=hp*2-1; - - glBegin(GL_QUADS); - glVertex3fv((posVector - (rightVector*width - upVector*offsetTop)).ptr()); - glVertex3fv((posVector - (rightVector*width + upVector*offsetBottom)).ptr()); - glVertex3fv((posVector + (rightVector*hp*width - upVector*offsetBottom)).ptr()); - glVertex3fv((posVector + (rightVector*hp*width + upVector*offsetTop)).ptr()); - glEnd(); -} - -void Renderer::renderSelectionCircle(Vec3f v, int size, float radius, float thickness) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - GLUquadricObj *disc; - - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - glTranslatef(v.x, v.y, v.z); - glRotatef(90.f, 1.f, 0.f, 0.f); - disc= gluNewQuadric(); - gluQuadricDrawStyle(disc, GLU_FILL); - gluCylinder(disc, radius*(size-thickness), radius*size, thickness, 30, 1); - gluDeleteQuadric(disc); - - glPopMatrix(); - // glBegin (GL_QUAD_STRIP); - // for (float k = 0; k <= 180; k=k+1) { - // float j=degToRad(k); - // glVertex3f(v.x+std::cos(j)*.9*radius*size, v.y+thickness, v.z+std::sin(j)*.9*radius*size); - // glVertex3f(v.x+std::cos(j)*radius*size, v.y, v.z+std::sin(j)*radius*size); - // } - // glEnd(); -} - -void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, - const Vec3f &color, float width) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - Config &config= Config::getInstance(); - if(config.getBool("RecordMode","false") == true) { - return; - } - - const int tesselation= 3; - const float arrowEndSize= 0.4f; - const float maxlen= 25; - const float blendDelay= 5.f; - - Vec3f dir= Vec3f(pos2-pos1); - float len= dir.length(); - - if(len>maxlen) { - return; - } - float alphaFactor= clamp((maxlen-len)/blendDelay, 0.f, 1.f); - - dir.normalize(); - Vec3f normal= dir.cross(Vec3f(0, 1, 0)); - - Vec3f pos2Left= pos2 + normal*(width-0.05f) - dir*arrowEndSize*width; - Vec3f pos2Right= pos2 - normal*(width-0.05f) - dir*arrowEndSize*width; - Vec3f pos1Left= pos1 + normal*(width+0.05f); - Vec3f pos1Right= pos1 - normal*(width+0.05f); - - //arrow body - glBegin(GL_TRIANGLE_STRIP); - for(int i=0; i<=tesselation; ++i) { - float t= static_cast(i)/tesselation; - Vec3f a= pos1Left.lerp(t, pos2Left); - Vec3f b= pos1Right.lerp(t, pos2Right); - Vec4f c= Vec4f(color, t*0.25f*alphaFactor); - - glColor4fv(c.ptr()); - - glVertex3fv(a.ptr()); - glVertex3fv(b.ptr()); - - } - - glEnd(); - //arrow end - glBegin(GL_TRIANGLES); - glVertex3fv((pos2Left + normal*(arrowEndSize-0.1f)).ptr()); - glVertex3fv((pos2Right - normal*(arrowEndSize-0.1f)).ptr()); - glVertex3fv((pos2 + dir*(arrowEndSize-0.1f)).ptr()); - glEnd(); -} - -void Renderer::renderProgressBar3D(int size, int x, int y, Font3D *font, int customWidth, - string prefixLabel,bool centeredText,int customHeight) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - // Makiong this smaller than 14 is a bad idea (since the font size is never smaller than that) - int progressbarHeight = (customHeight > 0 ? customHeight : 14); - int currentSize = size; - int maxSize = maxProgressBar; - string renderText = intToStr(static_cast(size)) + "%"; - if(customWidth > 0) { - if(size > 0) { - currentSize = (int)((double)customWidth * ((double)size / 100.0)); - } - maxSize = customWidth; - //if(maxSize <= 0) { - // maxSize = maxProgressBar; - //} - } - if(prefixLabel != "") { - renderText = prefixLabel + renderText; - } - - //bar - glBegin(GL_QUADS); - glColor4fv(progressBarFront2.ptr()); - glVertex2i(x, y); - glVertex2i(x, y + progressbarHeight); - glColor4fv(progressBarFront1.ptr()); - glVertex2i(x + currentSize, y + progressbarHeight); - glVertex2i(x + currentSize, y); - glEnd(); - - //transp bar - glEnable(GL_BLEND); - glBegin(GL_QUADS); - glColor4fv(progressBarBack2.ptr()); - glVertex2i(x + currentSize, y); - glVertex2i(x + currentSize, y + progressbarHeight); - glColor4fv(progressBarBack1.ptr()); - glVertex2i(x + maxSize, y + progressbarHeight); - glVertex2i(x + maxSize, y); - glEnd(); - glDisable(GL_BLEND); - - //text - //glColor3fv(defColor.ptr()); - //printf("Render progress bar3d renderText [%s] y = %d, centeredText = %d\n",renderText.c_str(),y, centeredText); - - renderTextBoundingBox3D(renderText, font, defColor, x, y, maxSize, - progressbarHeight, true, true, false,-1,-1); -} - -void Renderer::renderProgressBar(int size, int x, int y, Font2D *font, int customWidth, - string prefixLabel,bool centeredText) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - int currentSize = size; - int maxSize = maxProgressBar; - string renderText = intToStr(static_cast(size)) + "%"; - if(customWidth > 0) { - if(size > 0) { - currentSize = (int)((double)customWidth * ((double)size / 100.0)); - } - maxSize = customWidth; - //if(maxSize <= 0) { - // maxSize = maxProgressBar; - //} - } - if(prefixLabel != "") { - renderText = prefixLabel + renderText; - } - - //bar - glBegin(GL_QUADS); - glColor4fv(progressBarFront2.ptr()); - glVertex2i(x, y); - glVertex2i(x, y+10); - glColor4fv(progressBarFront1.ptr()); - glVertex2i(x + currentSize, y+10); - glVertex2i(x + currentSize, y); - glEnd(); - - //transp bar - glEnable(GL_BLEND); - glBegin(GL_QUADS); - glColor4fv(progressBarBack2.ptr()); - glVertex2i(x + currentSize, y); - glVertex2i(x + currentSize, y+10); - glColor4fv(progressBarBack1.ptr()); - glVertex2i(x + maxSize, y+10); - glVertex2i(x + maxSize, y); - glEnd(); - glDisable(GL_BLEND); - - //text - glColor3fv(defColor.ptr()); - - //textRenderer->begin(font); - TextRendererSafeWrapper safeTextRender(textRenderer,font); - if(centeredText == true) { - textRenderer->render(renderText.c_str(), x + maxSize / 2, y, centeredText); - } - else { - textRenderer->render(renderText.c_str(), x, y, centeredText); - } - //textRenderer->end(); - safeTextRender.end(); -} - - -//void Renderer::renderTile(const Vec2i &pos) { -// if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { -// return; -// } -// -// const Map *map= game->getWorld()->getMap(); -// Vec2i scaledPos= pos * Map::cellScale; -// -// glMatrixMode(GL_MODELVIEW); -// glPushMatrix(); -// glTranslatef(-0.5f, 0.f, -0.5f); -// -// glInitNames(); -// for(int i=0; i < Map::cellScale; ++i) { -// for(int j=0; j < Map::cellScale; ++j) { -// -// Vec2i renderPos= scaledPos + Vec2i(i, j); -// -// glPushName(renderPos.y); -// glPushName(renderPos.x); -// -// glDisable(GL_CULL_FACE); -// -// float h1 = map->getCell(renderPos.x, renderPos.y)->getHeight(); -// float h2 = map->getCell(renderPos.x, renderPos.y+1)->getHeight(); -// float h3 = map->getCell(renderPos.x+1, renderPos.y)->getHeight(); -// float h4 = map->getCell(renderPos.x+1, renderPos.y+1)->getHeight(); -// -// glBegin(GL_TRIANGLE_STRIP); -// glVertex3f( -// static_cast(renderPos.x), -// h1, -// static_cast(renderPos.y)); -// glVertex3f( -// static_cast(renderPos.x), -// h2, -// static_cast(renderPos.y+1)); -// glVertex3f( -// static_cast(renderPos.x+1), -// h3, -// static_cast(renderPos.y)); -// glVertex3f( -// static_cast(renderPos.x+1), -// h4, -// static_cast(renderPos.y+1)); -// glEnd(); -// -// glPopName(); -// glPopName(); -// } -// } -// -// glPopMatrix(); -//} - -void Renderer::renderQuad(int x, int y, int w, int h, const Texture2D *texture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - if(texture == NULL) { - printf("\n**WARNING** detected a null texture to render in renderQuad!\n"); - return; - } - - if(w < 0) { - w = texture->getPixmapConst()->getW(); - } - if(h < 0) { - h = texture->getPixmapConst()->getH(); - } - - glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2i(0, 1); - glVertex2i(x, y+h); - glTexCoord2i(0, 0); - glVertex2i(x, y); - glTexCoord2i(1, 1); - glVertex2i(x+w, y+h); - glTexCoord2i(1, 0); - glVertex2i(x+w, y); - glEnd(); -} - -Renderer::Shadows Renderer::strToShadows(const string &s){ - if(s=="Projected"){ - return sProjected; - } - else if(s=="ShadowMapping"){ - return sShadowMapping; - } - return sDisabled; -} - -string Renderer::shadowsToStr(Shadows shadows){ - switch(shadows){ - case sDisabled: - return "Disabled2"; - case sProjected: - return "Projected"; - case sShadowMapping: - return "ShadowMapping"; - default: - assert(false); - return ""; - } -} - -Texture2D::Filter Renderer::strToTextureFilter(const string &s){ - if(s=="Bilinear"){ - return Texture2D::fBilinear; - } - else if(s=="Trilinear"){ - return Texture2D::fTrilinear; - } - - throw megaglest_runtime_error("Error converting from string to FilterType, found: "+s); -} - -void Renderer::setAllowRenderUnitTitles(bool value) { - allowRenderUnitTitles = value; -} - -// This method renders titles for units -void Renderer::renderUnitTitles3D(Font3D *font, Vec3f color) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //std::map unitRenderedList; - - if(visibleFrameUnitList.empty() == false) { - //printf("Render Unit titles ON\n"); - - for(int idx = 0; idx < (int)visibleFrameUnitList.size(); idx++) { - const Unit *unit = visibleFrameUnitList[idx]; - if(unit != NULL) { - if(unit->getVisible() == true) { - if(unit->getCurrentUnitTitle() != "") { - //get the screen coordinates - Vec3f screenPos = unit->getScreenPos(); - renderText3D(unit->getCurrentUnitTitle(), font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); - //unitRenderedList[unit->getId()] = true; - } - else { - string str = unit->getFullName(unit->showTranslatedTechTree()) + " - " + intToStr(unit->getId()) + " [" + unit->getPosNotThreadSafe().getString() + "]"; - Vec3f screenPos = unit->getScreenPos(); - renderText3D(str, font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + void Renderer::renderSpecialHighlightUnits(std::map unitHighlightList) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true || unitHighlightList.empty() == true) { + return; + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + + std::map::iterator iterFindSpecialUnit = unitHighlightList.find(unit->getId()); + if (iterFindSpecialUnit != unitHighlightList.end()) { + Vec3f color = unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + float radius = 1.0f; + float thickness = 0.1f; + float alpha = 0.65f; + + HighlightSpecialUnitInfo &specialInfo = iterFindSpecialUnit->second; + if (specialInfo.color.x >= 0) { + color.x = specialInfo.color.x; + color.y = specialInfo.color.y; + color.z = specialInfo.color.z; + } + if (specialInfo.color.w >= 0) { + alpha = specialInfo.color.w; + } + if (specialInfo.radius > 0) { + radius = specialInfo.radius; + } + if (specialInfo.thickness > 0) { + thickness = specialInfo.thickness; + } + + glColor4f(color.x, color.y, color.z, alpha); + + Vec3f currVec = unit->getCurrVectorFlat(); + renderSelectionCircle(currVec, unit->getType()->getSize(), radius, thickness); } } + glPopAttrib(); } } - visibleFrameUnitList.clear(); - } - /* - if(renderUnitTitleList.empty() == false) { - for(int idx = 0; idx < renderUnitTitleList.size(); idx++) { - std::pair &unitInfo = renderUnitTitleList[idx]; - Unit *unit = unitInfo.first; - - const World *world= game->getWorld(); - Unit *validUnit = world->findUnitById(unit->getId()); - - if(validUnit != NULL && unitRenderedList.find(validUnit->getId()) == unitRenderedList.end()) { - string str = validUnit->getFullName() + " - " + intToStr(validUnit->getId()); - //get the screen coordinates - Vec3f &screenPos = unitInfo.second; - renderText(str, font, color, fabs(screenPos.x) + 5, fabs(screenPos.y) + 5, false); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] screenPos.x = %f, screenPos.y = %f, screenPos.z = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,screenPos.x,screenPos.y,screenPos.z); + void Renderer::renderTeamColorPlane() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; } - } - renderUnitTitleList.clear(); - } - */ -} -// This method renders titles for units -void Renderer::renderUnitTitles(Font2D *font, Vec3f color) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - //std::map unitRenderedList; - - if(visibleFrameUnitList.empty() == false) { - //printf("Render Unit titles ON\n"); - - for(int idx = 0; idx < (int)visibleFrameUnitList.size(); idx++) { - const Unit *unit = visibleFrameUnitList[idx]; - if(unit != NULL) { - if(unit->getCurrentUnitTitle() != "") { - //get the screen coordinates - Vec3f screenPos = unit->getScreenPos(); - renderText(unit->getCurrentUnitTitle(), font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); - - //unitRenderedList[unit->getId()] = true; - } - else { - string str = unit->getFullName(unit->showTranslatedTechTree()) + " - " + intToStr(unit->getId()) + " [" + unit->getPosNotThreadSafe().getString() + "]"; - Vec3f screenPos = unit->getScreenPos(); - renderText(str, font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_LIGHTING); + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + glEnable(GL_COLOR_MATERIAL); + const Texture2D *texture = CoreData::getInstance().getTeamColorTexture(); + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + if (unit->isAlive()) { + Vec3f currVec = unit->getCurrVectorFlat(); + renderTeamColorEffect(currVec, visibleUnitIndex, unit->getType()->getSize(), + unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0), texture); + } } + glDisable(GL_COLOR_MATERIAL); + glPopAttrib(); } } - visibleFrameUnitList.clear(); - } - /* - if(renderUnitTitleList.empty() == false) { - for(int idx = 0; idx < renderUnitTitleList.size(); idx++) { - std::pair &unitInfo = renderUnitTitleList[idx]; - Unit *unit = unitInfo.first; + void Renderer::renderGhostModel(const UnitType *building, const Vec2i pos, CardinalDir facing, Vec4f *forceColor) { + //const UnitType *building= gui->getBuilding(); + //const Vec2i &pos= gui->getPosObjWorld(); - const World *world= game->getWorld(); - Unit *validUnit = world->findUnitById(unit->getId()); - - if(validUnit != NULL && unitRenderedList.find(validUnit->getId()) == unitRenderedList.end()) { - string str = validUnit->getFullName() + " - " + intToStr(validUnit->getId()); - //get the screen coordinates - Vec3f &screenPos = unitInfo.second; - renderText(str, font, color, fabs(screenPos.x) + 5, fabs(screenPos.y) + 5, false); - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] screenPos.x = %f, screenPos.y = %f, screenPos.z = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,screenPos.x,screenPos.y,screenPos.z); + //const Gui *gui= game->getGui(); + //const Mouse3d *mouse3d= gui->getMouse3d(); + const Map *map = game->getWorld()->getMap(); + if (map == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d map == NULL", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); } - } - renderUnitTitleList.clear(); - } - */ -} -void Renderer::removeObjectFromQuadCache(const Object *o) { - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleObjectList.size(); ++visibleIndex) { - Object *currentObj = qCache.visibleObjectList[visibleIndex]; - if(currentObj == o) { - qCache.visibleObjectList.erase(qCache.visibleObjectList.begin() + visibleIndex); - break; - } - } -} + glPushMatrix(); + Vec3f pos3f = Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); -void Renderer::removeUnitFromQuadCache(const Unit *unit) { - VisibleQuadContainerCache &qCache = getQuadCache(); - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleQuadUnitList.size(); ++visibleIndex) { - Unit *currentUnit = qCache.visibleQuadUnitList[visibleIndex]; - if(currentUnit == unit) { - qCache.visibleQuadUnitList.erase(qCache.visibleQuadUnitList.begin() + visibleIndex); - break; - } - } - for(int visibleIndex = 0; - visibleIndex < (int)qCache.visibleUnitList.size(); ++visibleIndex) { - Unit *currentUnit = qCache.visibleUnitList[visibleIndex]; - if(currentUnit == unit) { - qCache.visibleUnitList.erase(qCache.visibleUnitList.begin() + visibleIndex); - break; - } - } -} + //selection building placement + float offset = building->getSize() / 2.f - 0.5f; + glTranslatef(pos3f.x + offset, pos3f.y, pos3f.z + offset); -VisibleQuadContainerCache & Renderer::getQuadCache( bool updateOnDirtyFrame, - bool forceNew) { - //forceNew = true; - if(game != NULL && game->getWorld() != NULL) { - const World *world= game->getWorld(); - - if(forceNew == true || - (updateOnDirtyFrame == true && - (world->getFrameCount() != quadCache.cacheFrame || - visibleQuad != quadCache.lastVisibleQuad))) { - - // Dump cached info - //if(forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { - //quadCache.clearCacheData(); - //} - //else { - quadCache.clearVolatileCacheData(); - worldToScreenPosCache.clear(); - //} - - // Unit calculations - for(int i = 0; i < world->getFactionCount(); ++i) { - const Faction *faction = world->getFaction(i); - for(int j = 0; j < faction->getUnitCount(); ++j) { - Unit *unit= faction->getUnit(j); - - bool unitCheckedForRender = false; - if(VisibleQuadContainerCache::enableFrustumCalcs == true) { - //bool insideQuad = PointInFrustum(quadCache.frustumData, unit->getCurrVector().x, unit->getCurrVector().y, unit->getCurrVector().z ); - bool insideQuad = CubeInFrustum(quadCache.frustumData, unit->getCurrMidHeightVector().x, unit->getCurrMidHeightVector().y, unit->getCurrMidHeightVector().z, unit->getType()->getRenderSize()); - bool renderInMap = world->toRenderUnit(unit); - if(insideQuad == false || renderInMap == false) { - unit->setVisible(false); - if(renderInMap == true) { - quadCache.visibleUnitList.push_back(unit); - } - unitCheckedForRender = true; // no more need to check any further; - // Currently don't need this list - //quadCache.inVisibleUnitList.push_back(unit); - } - } - if(unitCheckedForRender == false) { - bool insideQuad = visibleQuad.isInside(unit->getPos()); - bool renderInMap = world->toRenderUnit(unit); - if(insideQuad == true && renderInMap == true) { - quadCache.visibleQuadUnitList.push_back(unit); - } - else { - unit->setVisible(false); - // Currently don't need this list - //quadCache.inVisibleUnitList.push_back(unit); - } - - if(renderInMap == true) { - quadCache.visibleUnitList.push_back(unit); - } - } - - bool unitBuildPending = unit->isBuildCommandPending(); - if(unitBuildPending == true) { - const UnitBuildInfo &pendingUnit = unit->getBuildCommandPendingInfo(); - const Vec2i &pos = pendingUnit.pos; - const Map *map= world->getMap(); - - bool unitBuildCheckedForRender = false; - - //printf("#1 Unit is about to build another unit\n"); - - if(VisibleQuadContainerCache::enableFrustumCalcs == true) { - Vec3f pos3f= Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); - //bool insideQuad = PointInFrustum(quadCache.frustumData, unit->getCurrVector().x, unit->getCurrVector().y, unit->getCurrVector().z ); - bool insideQuad = CubeInFrustum(quadCache.frustumData, pos3f.x, pos3f.y, pos3f.z, pendingUnit.buildUnit->getRenderSize()); - bool renderInMap = world->toRenderUnit(pendingUnit); - if(insideQuad == false || renderInMap == false) { - if(renderInMap == true) { - quadCache.visibleQuadUnitBuildList.push_back(pendingUnit); - } - unitBuildCheckedForRender = true; // no more need to check any further; - // Currently don't need this list - //quadCache.inVisibleUnitList.push_back(unit); - } - - //printf("#2 Unit build added? insideQuad = %d, renderInMap = %d\n",insideQuad,renderInMap); - } - - if(unitBuildCheckedForRender == false) { - bool insideQuad = visibleQuad.isInside(pos); - bool renderInMap = world->toRenderUnit(pendingUnit); - if(insideQuad == true && renderInMap == true) { - quadCache.visibleQuadUnitBuildList.push_back(pendingUnit); - } - else { - //unit->setVisible(false); - // Currently don't need this list - //quadCache.inVisibleUnitList.push_back(unit); - } - - //printf("#3 Unit build added? insideQuad = %d, renderInMap = %d\n",insideQuad,renderInMap); - } - - //printf("#4 quadCache.visibleQuadUnitBuildList.size() = %d\n",quadCache.visibleQuadUnitBuildList.size()); - } + //choose color + Vec4f color; + if (forceColor != NULL) { + color = *forceColor; + } else { + if (map->isFreeCells(pos, building->getSize(), fLand)) { + color = Vec4f(1.f, 1.f, 1.f, 0.5f); + } else { + // Uint64 tc=game->getTickCount(); + // float red=0.49f+((tc%4*1.0f)/2); + color = Vec4f(1.0f, 0.f, 0.f, 0.5f); } } - if(forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { - // Object calculations - const Map *map= world->getMap(); - // clear visibility of old objects - for(int visibleIndex = 0; - visibleIndex < (int)quadCache.visibleObjectList.size(); ++visibleIndex){ - quadCache.visibleObjectList[visibleIndex]->setVisible(false); + glColor4fv(color.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color.ptr()); + Model *buildingModel = building->getFirstStOfClass(scStop)->getAnimation(); + + if (facing != CardinalDir::NORTH) { + float rotateAmount = facing * 90.f; + if (rotateAmount > 0) { + glRotatef(rotateAmount, 0.f, 1.f, 0.f); } - quadCache.clearNonVolatileCacheData(); + } - //int loops1=0; - PosQuadIterator pqi(map,visibleQuad, Map::cellScale); - while(pqi.next()) { - const Vec2i &pos= pqi.getPos(); - if(map->isInside(pos)) { - //loops1++; - const Vec2i &mapPos = Map::toSurfCoords(pos); + buildingModel->updateInterpolationData(0.f, false); + modelRenderer->render(buildingModel); - //quadCache.visibleCellList.push_back(mapPos); + glPopMatrix(); + } - SurfaceCell *sc = map->getSurfaceCell(mapPos); - Object *o = sc->getObject(); + void Renderer::renderUnits(bool airUnits, const int renderFps) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } - if(VisibleQuadContainerCache::enableFrustumCalcs == true) { - if(o != NULL) { - //bool insideQuad = PointInFrustum(quadCache.frustumData, o->getPos().x, o->getPos().y, o->getPos().z ); - bool insideQuad = CubeInFrustum(quadCache.frustumData, o->getPos().x, o->getPos().y, o->getPos().z, 1); - if(insideQuad == false) { - o->setVisible(false); - continue; - } + //Unit *unit=NULL; + //const World *world= game->getWorld(); + MeshCallbackTeamColor meshCallbackTeamColor; + + //assert + assertGl(); + + if (visibleFrameUnitList.empty() == false) { + visibleFrameUnitList.clear(); + //visibleFrameUnitListCameraKey = ""; + //if(visibleFrameUnitListCameraKey != game->getGameCamera()->getCameraMovementKey()) { + // worldToScreenPosCache.clear(); + //} + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + bool modelRenderStarted = false; + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + + if ((airUnits == false && unit->getType()->getField() == fAir) || (airUnits == true && unit->getType()->getField() != fAir)) { + continue; + } + meshCallbackTeamColor.setTeamTexture(unit->getFaction()->getTexture()); + + if (modelRenderStarted == false) { + modelRenderStarted = true; + + glPushAttrib(GL_ENABLE_BIT | GL_FOG_BIT | GL_LIGHTING_BIT | GL_TEXTURE_BIT); + glEnable(GL_COLOR_MATERIAL); + + if (!shadowsOffDueToMinRender) { + if (shadows == sShadowMapping) { + glActiveTexture(shadowTexUnit); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + + static_cast(modelRenderer)->setDuplicateTexCoords(true); + enableProjectiveTexturing(); } } + glActiveTexture(baseTexUnit); - bool cellExplored = world->showWorldForPlayer(world->getThisFactionIndex()); - if(cellExplored == false) { - cellExplored = sc->isExplored(world->getThisTeamIndex()); - } + modelRenderer->begin(true, true, true, false, &meshCallbackTeamColor); + } - bool isExplored = (cellExplored == true && o != NULL); - //bool isVisible = (sc->isVisible(world->getThisTeamIndex()) && o != NULL); - bool isVisible = true; + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); - if(isExplored == true && isVisible == true) { - quadCache.visibleObjectList.push_back(o); - o->setVisible(true); - } + //translate + Vec3f currVec = unit->getCurrVectorFlat(); + glTranslatef(currVec.x, currVec.y, currVec.z); + + //rotate + float zrot = unit->getRotationZ(); + float xrot = unit->getRotationX(); + if (zrot != .0f) { + glRotatef(zrot, 0.f, 0.f, 1.f); + } + if (xrot != .0f) { + glRotatef(xrot, 1.f, 0.f, 0.f); + } + glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); + + //dead alpha + const SkillType *st = unit->getCurrSkill(); + if (st->getClass() == scDie && static_cast(st)->getFade()) { + float alpha = 1.0f - unit->getAnimProgressAsFloat(); + glDisable(GL_COLOR_MATERIAL); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); + } else { + glEnable(GL_COLOR_MATERIAL); + // we cut off a tiny bit here to avoid problems with fully transparent texture parts cutting units in background rendered later. + glAlphaFunc(GL_GREATER, 0.02f); + } + + //render + Model *model = unit->getCurrentModelPtr(); + //printf("Rendering model [%d - %s]\n[%s]\nCamera [%s]\nDistance: %f\n",unit->getId(),unit->getType()->getName().c_str(),unit->getCurrVector().getString().c_str(),this->gameCamera->getPos().getString().c_str(),this->gameCamera->getPos().dist(unit->getCurrVector())); + + //if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) { + model->updateInterpolationData(unit->getAnimProgressAsFloat(), unit->isAlive() && !unit->isAnimProgressBound()); + //} + + modelRenderer->render(model); + triangleCount += model->getTriangleCount(); + pointCount += model->getVertexCount(); + + glPopMatrix(); + unit->setVisible(true); + + if (showDebugUI == true && + (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { + + unit->setScreenPos(computeScreenPosition(currVec)); + visibleFrameUnitList.push_back(unit); + visibleFrameUnitListCameraKey = game->getGameCamera()->getCameraMovementKey(); } } - //printf("Frame # = %d loops1 = %d\n",world->getFrameCount(),loops1); - - //int loops2=0; - - std::map markedCells = game->getMapMarkedCellList(); - - const Rect2i mapBounds(0, 0, map->getSurfaceW()-1, map->getSurfaceH()-1); - Quad2i scaledQuad = visibleQuad / Map::cellScale; - PosQuadIterator pqis(map,scaledQuad); - while(pqis.next()) { - const Vec2i &pos= pqis.getPos(); - if(mapBounds.isInside(pos)) { - //loops2++; - if(VisibleQuadContainerCache::enableFrustumCalcs == false) { - quadCache.visibleScaledCellList.push_back(pos); - - if(markedCells.empty() == false) { - if(markedCells.find(pos) != markedCells.end()) { - //printf("#1 ******** VISIBLE SCALED CELL FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); - //if(markedCells.empty() == false) { - //SurfaceCell *sc = map->getSurfaceCell(pos); - //quadCache.visibleScaledCellToScreenPosList[pos]=computeScreenPosition(sc->getVertex()); - updateMarkedCellScreenPosQuadCache(pos); - } - else { - //printf("#1 VISIBLE SCALED CELL NOT FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); - } - } - } - else { - //bool insideQuad = false; - - //if( !insideQuad) { - SurfaceCell *sc = map->getSurfaceCell(pos.x, pos.y); - bool insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); - //} - if( !insideQuad) { - SurfaceCell *sc = map->getSurfaceCell(pos.x+1, pos.y); - insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); - } - if( !insideQuad) { - SurfaceCell *sc = map->getSurfaceCell(pos.x, pos.y+1); - insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); - } - if( !insideQuad) { - SurfaceCell *sc = map->getSurfaceCell(pos.x+1, pos.y+1); - insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); - } - - if(insideQuad == true) { - quadCache.visibleScaledCellList.push_back(pos); - - if(markedCells.empty() == false) { - if(markedCells.find(pos) != markedCells.end()) { - //printf("#2 ******** VISIBLE SCALED CELL FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); - //if(markedCells.empty() == false) { - //quadCache.visibleScaledCellToScreenPosList[pos]=computeScreenPosition(sc->getVertex()); - updateMarkedCellScreenPosQuadCache(pos); - } - else { - //printf("#2 VISIBLE SCALED CELL NOT FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); - } - } - } - } - } + if (modelRenderStarted == true) { + modelRenderer->end(); + glPopAttrib(); } - //printf("Frame # = %d loops2 = %d\n",world->getFrameCount(),loops2); - } - quadCache.cacheFrame = world->getFrameCount(); - quadCache.lastVisibleQuad = visibleQuad; - } - } - - return quadCache; -} - -void Renderer::updateMarkedCellScreenPosQuadCache(Vec2i pos) { - const World *world= game->getWorld(); - const Map *map= world->getMap(); - - SurfaceCell *sc = map->getSurfaceCell(pos); - quadCache.visibleScaledCellToScreenPosList[pos]=computeScreenPosition(sc->getVertex()); -} - -void Renderer::forceQuadCacheUpdate() { - quadCache.cacheFrame = -1; - - Vec2i clearPos(-1,-1); - quadCache.lastVisibleQuad.p[0] = clearPos; - quadCache.lastVisibleQuad.p[1] = clearPos; - quadCache.lastVisibleQuad.p[2] = clearPos; - quadCache.lastVisibleQuad.p[3] = clearPos; -} - -std::pair Renderer::posInCellQuadCache(Vec2i pos) { - std::pair result = make_pair(false,Vec3f()); - if(std::find( - quadCache.visibleScaledCellList.begin(), - quadCache.visibleScaledCellList.end(), - pos) != quadCache.visibleScaledCellList.end()) { - result.first = true; - result.second = quadCache.visibleScaledCellToScreenPosList[pos]; - } - return result; -} - -//Vec3f Renderer::getMarkedCellScreenPosQuadCache(Vec2i pos) { -// Vec3f result(-1,-1,-1); -// if(std::find( -// quadCache.visibleScaledCellList.begin(), -// quadCache.visibleScaledCellList.end(), -// pos) != quadCache.visibleScaledCellList.end()) { -// result = quadCache.visibleScaledCellToScreenPosList[pos]; -// } -// return result; -//} - -void Renderer::beginRenderToTexture(Texture2D **renderToTexture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); - - if(supportFBOs == true && renderToTexture != NULL) { - Config &config= Config::getInstance(); - Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); - int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); - - const Metrics &metrics = Metrics::getInstance(); - - *renderToTexture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); - Texture2DGl *texture = static_cast(*renderToTexture); - texture->setMipmap(false); - Pixmap2D *pixmapScreenShot = texture->getPixmap(); - pixmapScreenShot->init(metrics.getScreenW(), metrics.getScreenH(), 4); - texture->setForceCompressionDisabled(true); - texture->init(textureFilter,maxAnisotropy); - texture->setup_FBO_RBO(); - - assertGl(); - - if(texture->checkFrameBufferStatus() == false) { - //printf("******************** WARNING CANNOT Attach to FBO!\n"); - texture->end(); - delete texture; - *renderToTexture=NULL; - } - } -} - -void Renderer::endRenderToTexture(Texture2D **renderToTexture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); - - if(supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { - Texture2DGl *texture = static_cast(*renderToTexture); - if(texture != NULL) { - texture->dettachFrameBufferFromTexture(); - } - - assertGl(); - } -} - -void Renderer::renderMapPreview( const MapPreview *map, bool renderAll, - int screenPosX, int screenPosY, - Texture2D **renderToTexture) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); - if(Config::getInstance().getBool("LegacyMapPreviewRendering","false") == true) { - supportFBOs = false; - } - - //static bool supportFBOs = false; - const Metrics &metrics= Metrics::getInstance(); - - float alt = 0; - float showWater = 0; - int renderMapHeight = 64; - int renderMapWidth = 64; - float cellSize = 2; - float playerCrossSize = 2; - float clientW = renderMapWidth * cellSize; - float clientH = renderMapHeight * cellSize;; - float minDimension = std::min(metrics.getVirtualW(), metrics.getVirtualH()); - - // stretch small maps to 128x128 - if(map->getW() < map->getH()) { - cellSize = cellSize * renderMapHeight / map->getH(); - } - else { - cellSize = cellSize * renderMapWidth / map->getW(); - } - - assertGl(); - - if(supportFBOs == true && renderToTexture != NULL) { - Config &config= Config::getInstance(); - Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); - int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); - - *renderToTexture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); - Texture2DGl *texture = static_cast(*renderToTexture); - texture->setMipmap(false); - Pixmap2D *pixmapScreenShot = texture->getPixmap(); - pixmapScreenShot->init(minDimension, minDimension, 4); - texture->setForceCompressionDisabled(true); - texture->init(textureFilter,maxAnisotropy); - texture->setup_FBO_RBO(); - - assertGl(); - - if(texture->checkFrameBufferStatus() == false) { - //printf("******************** WARNING CANNOT Attach to FBO!\n"); - texture->end(); - delete texture; - *renderToTexture=NULL; - } - } - - if(supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { - cellSize =1; - clientW = minDimension; - clientH = minDimension; - int mapMaxDimensionSize = std::max(map->getW(),map->getH()); - switch(mapMaxDimensionSize) { - case 8: - cellSize = 96; - break; - case 16: - cellSize = 48; - break; - case 32: - cellSize = 24; - break; - case 64: - cellSize = 12; - break; - case 128: - cellSize = 6; - break; - case 256: - cellSize = 3; - break; - case 512: - cellSize = 2; - break; - } - } - - glFrontFace(GL_CW); - glEnable(GL_CULL_FACE); - - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - - assertGl(); - - GLint viewport[4]; // Where The original Viewport Values Will Be Stored - - if(supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { - glGetIntegerv(GL_VIEWPORT, viewport); - glOrtho(0, clientW, 0, clientH, 0, 1); - glViewport(0, 0, clientW, clientH); - } - else { - glOrtho(0, metrics.getVirtualW(), 0, metrics.getVirtualH(), 0, 1); - } - - glMatrixMode(GL_MODELVIEW); - - glPushMatrix(); - glLoadIdentity(); - - if(supportFBOs == false || renderToTexture == NULL || *renderToTexture == NULL) { - glTranslatef(static_cast(screenPosX),static_cast(screenPosY)-clientH,0.0f); - } - - assertGl(); - - glPushAttrib(GL_CURRENT_BIT); - glLineWidth(1); - glColor3f(0, 0, 0); - - for (int j = 0; j < map->getH(); j++) { - for (int i = 0; i < map->getW(); i++) { - //surface - alt = map->getHeight(i, j) / 20.f; - showWater = map->getWaterLevel()/ 20.f - alt; - showWater = (showWater > 0)? showWater:0; - Vec3f surfColor; - switch (map->getSurface(i, j)) { - case st_Grass: - surfColor = Vec3f(0.0, 0.8f * alt, 0.f + showWater); - break; - case st_Secondary_Grass: - surfColor = Vec3f(0.4f * alt, 0.6f * alt, 0.f + showWater); - break; - case st_Road: - surfColor = Vec3f(0.6f * alt, 0.3f * alt, 0.f + showWater); - break; - case st_Stone: - surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); - break; - case st_Ground: - surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); - break; } - glColor3fv(surfColor.ptr()); + //restore + static_cast(modelRenderer)->setDuplicateTexCoords(true); + // reset alpha + glAlphaFunc(GL_GREATER, 0.0f); + //assert + assertGl(); + + } + + void Renderer::renderUnitsToBuild(const int renderFps) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //assert + assertGl(); + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitBuildList.empty() == false) { + + glMatrixMode(GL_MODELVIEW); + glPushAttrib(GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_BLEND); + glDisable(GL_STENCIL_TEST); + glDepthFunc(GL_LESS); + glEnable(GL_COLOR_MATERIAL); + glDepthMask(GL_FALSE); + + modelRenderer->begin(true, true, false, false); + + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitBuildList.size(); ++visibleUnitIndex) { + const UnitBuildInfo &buildUnit = qCache.visibleQuadUnitBuildList[visibleUnitIndex]; + //Vec4f modelColor= Vec4f(0.f, 1.f, 0.f, 0.5f); + const Vec3f teamColor = buildUnit.unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + Vec4f modelColor = Vec4f(teamColor.x, teamColor.y, teamColor.z, 0.4f); + renderGhostModel(buildUnit.buildUnit, buildUnit.pos, buildUnit.facing, &modelColor); + + //printf("Rendering to build unit index = %d\n",visibleUnitIndex); + } + + modelRenderer->end(); + + glDisable(GL_COLOR_MATERIAL); + glPopAttrib(); + } + + //assert + assertGl(); + + } + + void Renderer::renderTeamColorEffect(Vec3f &v, int heigth, int size, Vec3f color, const Texture2D *texture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //GLUquadricObj *disc; + float halfSize = size; + //halfSize=halfSize; + float heigthoffset = 0.5 + heigth % 25 * 0.004; + glPushMatrix(); + glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); + glColor4f(color.x, color.y, color.z, 1.0f); glBegin(GL_TRIANGLE_STRIP); - glVertex2f(i * cellSize, clientH - j * cellSize - cellSize); - glVertex2f(i * cellSize, clientH - j * cellSize); - glVertex2f(i * cellSize + cellSize, clientH - j * cellSize - cellSize); - glVertex2f(i * cellSize + cellSize, clientH - j * cellSize); + glTexCoord2i(0, 1); + glVertex3f(v.x - halfSize, v.y + heigthoffset, v.z + halfSize); + glTexCoord2i(0, 0); + glVertex3f(v.x - halfSize, v.y + heigthoffset, v.z - halfSize); + glTexCoord2i(1, 1); + + glVertex3f(v.x + halfSize, v.y + heigthoffset, v.z + halfSize); + glTexCoord2i(1, 0); + glVertex3f(v.x + halfSize, v.y + heigthoffset, v.z - halfSize); + glEnd(); + glPopMatrix(); + + } + + void Renderer::renderMorphEffects() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + bool initialized = false; + int frameCycle = 0; + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + if (unit->getCurrSkill() != NULL && unit->getCurrSkill()->getClass() == scMorph) { + Command *command = unit->getCurrCommand(); + if (command != NULL && command->getCommandType()->commandTypeClass == ccMorph) { + const MorphCommandType *mct = static_cast(command->getCommandType()); + const UnitType* mType = mct->getMorphUnit(); + + if (mType->getSize() > unit->getType()->getSize() || + mType->getField() != unit->getType()->getField()) { + if (!initialized) { + const World *world = game->getWorld(); + frameCycle = world->getFrameCount() % 40; + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + initialized = true; + } + + Vec3f currVec = unit->getCurrVectorFlat(); + currVec = Vec3f(currVec.x, currVec.y + 0.3f, currVec.z); + if (mType->getField() == fAir && unit->getType()->getField() == fLand) { + currVec = Vec3f(currVec.x, currVec.y + game->getWorld()->getTileset()->getAirHeight(), currVec.z); + } + if (mType->getField() == fLand && unit->getType()->getField() == fAir) { + currVec = Vec3f(currVec.x, currVec.y - game->getWorld()->getTileset()->getAirHeight(), currVec.z); + } + + float color = frameCycle * 0.4f / 40; + glColor4f(color, color, 0.4f, 0.4f); + renderSelectionCircle(currVec, mType->getSize(), frameCycle*0.85f / 40, 0.2f); + } + } + } + } + if (initialized) { + glPopAttrib(); + } + } + } + + + + void Renderer::renderSelectionEffects(int healthbarMode) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("RecordMode", "false") == true) { + return; + } + + const World *world = game->getWorld(); + const Map *map = world->getMap(); + const Selection *selection = game->getGui()->getSelection(); + const Object *selectedResourceObject = game->getGui()->getSelectedResourceObject(); + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + //units + for (int i = 0; i < selection->getCount(); ++i) { + + const Unit *unit = selection->getUnit(i); + if (unit != NULL) { + //translate + Vec3f currVec = unit->getCurrVectorFlat(); + currVec.y += 0.3f; + + //selection circle + int finalHealthbarMode = hbvUndefined; + if (healthbarMode == hbvUndefined) { + finalHealthbarMode = unit->getFaction()->getType()->getHealthbarVisible(); + } else { + finalHealthbarMode = healthbarMode; + } + bool healthbarsVisible = ((finalHealthbarMode & hbvAlways) || + (finalHealthbarMode & hbvSelected) || + (finalHealthbarMode & hbvIfNeeded)); + float selectionCircleThickness = 0.2f; + float hpRatio = unit->getHpRatio(); + if (healthbarsVisible) { + selectionCircleThickness = 0.05f; + hpRatio = 1.0f; + } + + if (world->getThisFactionIndex() == unit->getFactionIndex()) { + if (showDebugUI == true && + ((showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) && + unit->getCommandSize() > 0 && + dynamic_cast(unit->getCurrCommand()->getCommandType()) != NULL) { + glColor4f(hpRatio, hpRatio, hpRatio, 0.3f); + } else { + glColor4f(0, hpRatio, 0, 0.3f); + } + } else if (world->getThisTeamIndex() == unit->getTeam()) { + glColor4f(hpRatio, hpRatio, 0, 0.3f); + } else { + glColor4f(hpRatio, 0, 0, 0.3f); + } + renderSelectionCircle(currVec, unit->getType()->getSize(), selectionCircleRadius, selectionCircleThickness); + + if (showDebugUI == true && + (showDebugUILevel & debugui_unit_titles) == debugui_unit_titles) { + + const UnitPathInterface *path = unit->getPath(); + const UnitPathBasic *pathfinder = (path == NULL ? NULL : dynamic_cast(path)); + if (pathfinder != NULL) { + vector pathList = pathfinder->getQueue(); + + Vec2i lastPosValue; + for (int i = 0; i < (int) pathList.size(); ++i) { + Vec2i curPosValue = pathList[i]; + if (i == 0) { + lastPosValue = curPosValue; + } + Vec3f currVec2 = unit->getVectorFlat(lastPosValue, curPosValue); + currVec2.y += 0.3f; + renderSelectionCircle(currVec2, 1, selectionCircleRadius); + } + } + } + + //magic circle + if (!healthbarsVisible && world->getThisFactionIndex() == unit->getFactionIndex() && unit->getType()->getMaxEp() > 0) { + glColor4f(unit->getEpRatio() / 2.f, unit->getEpRatio(), unit->getEpRatio(), 0.5f); + renderSelectionCircle(currVec, unit->getType()->getSize(), magicCircleRadius); + } + + // Render Attack-boost circles + if (showDebugUI == true) { + //const std::pair > ¤tAttackBoostUnits = unit->getCurrentAttackBoostUnits(); + const UnitAttackBoostEffectOriginator &effect = unit->getAttackBoostOriginatorEffect(); + + if (effect.skillType->isAttackBoostEnabled() == true) { + glColor4f(MAGENTA.x, MAGENTA.y, MAGENTA.z, MAGENTA.w); + renderSelectionCircle(currVec, 1, effect.skillType->getAttackBoost()->radius, .25f / effect.skillType->getAttackBoost()->radius); + + for (unsigned int i = 0; i < effect.currentAttackBoostUnits.size(); ++i) { + // Remove attack boost upgrades from unit + int findUnitId = effect.currentAttackBoostUnits[i]; + Unit *affectedUnit = game->getWorld()->findUnitById(findUnitId); + if (affectedUnit != NULL) { + Vec3f currVecBoost = affectedUnit->getCurrVectorFlat(); + currVecBoost.y += 0.3f; + + renderSelectionCircle(currVecBoost, affectedUnit->getType()->getSize(), 1.f); + } + } + } + } + + //meeting point arrow + if (unit->getType()->getMeetingPoint()) { + Vec2i pos = unit->getMeetingPos(); + map->clampPos(pos); + + Vec3f arrowTarget = Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); + renderArrow(unit->getCurrVectorFlat(), arrowTarget, Vec3f(0.f, 0.f, 1.f), 0.3f); + } + } + } + + if (selectedResourceObject != NULL && selectedResourceObject->getResource() != NULL && selection->getCount() < 1) { + Resource *r = selectedResourceObject->getResource(); + int defaultValue = r->getType()->getDefResPerPatch(); + float colorValue = static_cast(r->getAmount()) / static_cast(defaultValue); + glColor4f(0.1f, 0.1f, colorValue, 0.4f); + renderSelectionCircle(selectedResourceObject->getPos(), 2, selectionCircleRadius); + } + //target arrow + if (selection->getCount() == 1) { + const Unit *unit = selection->getUnit(0); + if (unit != NULL) { + //comand arrow + if (focusArrows && unit->anyCommand()) { + const CommandType *ct = unit->getCurrCommand()->getCommandType(); + if (ct->getClicks() != cOne) { + + //arrow color + Vec3f arrowColor; + switch (ct->getClass()) { + case ccMove: + arrowColor = Vec3f(0.f, 1.f, 0.f); + break; + case ccAttack: + case ccAttackStopped: + arrowColor = Vec3f(1.f, 0.f, 0.f); + break; + default: + arrowColor = Vec3f(1.f, 1.f, 0.f); + break; + } + + //arrow target + Vec3f arrowTarget; + Command *c = unit->getCurrCommand(); + if (c->getUnit() != NULL) { + arrowTarget = c->getUnit()->getCurrVectorFlat(); + } else { + Vec2i pos = c->getPos(); + map->clampPos(pos); + + arrowTarget = Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); + } + + renderArrow(unit->getCurrVectorFlat(), arrowTarget, arrowColor, 0.3f); + } + } + } + } + + //render selection hightlights + if (game->getGui()->getHighlightedUnit() != NULL) { + const Unit *unit = game->getGui()->getHighlightedUnit(); + + if (unit->isHighlighted()) { + float highlight = unit->getHightlight(); + if (game->getWorld()->getThisFactionIndex() == unit->getFactionIndex()) { + glColor4f(0.f, 1.f, 0.f, highlight); + } else { + glColor4f(1.f, 0.f, 0.f, highlight); + } + + Vec3f v = unit->getCurrVectorFlat(); + v.y += 0.3f; + renderSelectionCircle(v, unit->getType()->getSize(), 0.5f + 0.4f*highlight); + } + } + // old inefficient way to render highlights + // for(int i=0; i < world->getFactionCount(); ++i) { + // for(int j=0; j < world->getFaction(i)->getUnitCount(); ++j) { + // const Unit *unit= world->getFaction(i)->getUnit(j); + // + // if(unit->isHighlighted()) { + // float highlight= unit->getHightlight(); + // if(game->getWorld()->getThisFactionIndex() == unit->getFactionIndex()) { + // glColor4f(0.f, 1.f, 0.f, highlight); + // } + // else{ + // glColor4f(1.f, 0.f, 0.f, highlight); + // } + // + // Vec3f v= unit->getCurrVectorFlat(); + // v.y+= 0.3f; + // renderSelectionCircle(v, unit->getType()->getSize(), 0.5f+0.4f*highlight ); + // } + // } + // } + //render resource selection highlight + if (game->getGui()->getHighlightedResourceObject() != NULL) { + const Object* object = game->getGui()->getHighlightedResourceObject(); + if (object->isHighlighted()) { + float highlight = object->getHightlight(); + glColor4f(0.1f, 0.1f, 1.0f, highlight); + Vec3f v = object->getPos(); + v.y += 0.3f; + renderSelectionCircle(v, 2, 0.4f + 0.4f*highlight); + } + } + + glPopAttrib(); + } + + void Renderer::renderHealthBars(int healthbarMode) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("RecordMode", "false") == true) { + return; + } + + if (config.getBool("PhotoMode")) { + return; + } + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + glDisable(GL_LIGHTING); + glDisable(GL_TEXTURE_2D); + glDepthFunc(GL_ALWAYS); + glDisable(GL_STENCIL_TEST); + glDisable(GL_CULL_FACE); + glEnable(GL_BLEND); + glLineWidth(2.f); + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + if (isHealthBarVisible(unit, healthbarMode)) { + float healthbarheight; + float healthbarthickness; + const Texture2D *healthbarTexture; + const Texture2D *healthbarBackgroundTexture; + bool healthbarLineBorder; + + //get settings of the faction + healthbarheight = unit->getFaction()->getType()->getHealthbarHeight(); + healthbarthickness = unit->getFaction()->getType()->getHealthbarThickness(); + healthbarLineBorder = unit->getFaction()->getType()->isHealthbarLineBorder(); + CoreData &coreData = CoreData::getInstance(); + //First try faction texture then use core Texture + if (unit->getFaction()->getType()->isHealthbarBorderTextureEnabled()) { + healthbarTexture = unit->getFaction()->getType()->getHealthbarTexture(); + if (healthbarTexture == NULL) { + healthbarTexture = coreData.getHealthbarTexture(); + } + } else { + healthbarTexture = NULL; + } + if (unit->getFaction()->getType()->isHealthbarBackgroundTextureEnabled()) { + healthbarBackgroundTexture = unit->getFaction()->getType()->getHealthbarBackgroundTexture(); + if (healthbarBackgroundTexture == NULL) { + healthbarBackgroundTexture = coreData.getHealthbarBackgroundTexture(); + } + } else { + healthbarBackgroundTexture = NULL; + } + + //replace them by the ones from the unit if existent + if (unit->getType()->getHealthbarVisible() != hbvOff && unit->getType()->getHealthbarVisible() != hbvUndefined) { + if (unit->getType()->getHealthbarHeight() != -100.0f) { + healthbarheight = unit->getType()->getHealthbarHeight(); + } + if (unit->getType()->getHealthbarThickness() != -1.0f) { + healthbarthickness = unit->getType()->getHealthbarThickness(); + } + } + + Vec3f currVec = unit->getCurrVectorFlat(); + if (healthbarheight == -100.0f) { + currVec.y += unit->getType()->getHeight(); + } else { + currVec.y += healthbarheight; + } + renderHealthBar(currVec, unit, healthbarthickness, healthbarLineBorder, healthbarTexture, healthbarBackgroundTexture); + } + } + } + glPopAttrib(); + } + + bool Renderer::isHealthBarVisible(const Unit *unit, int healthbarMode) { + int healthbarVisible = hbvUndefined; + //check options (hotkey) + if (healthbarMode == hbvUndefined) { + healthbarVisible = unit->getFaction()->getType()->getHealthbarVisible(); + } else { + healthbarVisible = healthbarMode; + } + + //replace them by the ones from the unit if existent + if (unit->getType()->getHealthbarVisible() != hbvOff && unit->getType()->getHealthbarVisible() != hbvUndefined) { + if (healthbarMode == hbvUndefined) { //don't override the visible setting when hotkey is not hbvUndefined + healthbarVisible = unit->getType()->getHealthbarVisible(); + } + } + + bool settingsWantToRenderThem = !(healthbarVisible == hbvUndefined || (healthbarVisible&hbvOff)) + && ((healthbarVisible&hbvAlways) + || ((healthbarVisible&hbvIfNeeded) && unit->getHp() < unit->getType()->getMaxHp() + unit->getTotalUpgrade()->getMaxHp()) + || ((healthbarVisible&hbvIfNeeded) && unit->getType()->getMaxEp() > 0 && unit->getEp() < unit->getType()->getMaxEp() + unit->getTotalUpgrade()->getMaxEp()) + || ((healthbarVisible&hbvIfNeeded) && unit->getProductionPercent() > 0) + || ((healthbarVisible&hbvSelected) && game->getGui()->isSelected(unit))); + + if (unit->isAlive() && (settingsWantToRenderThem)) { + return true; + } + return false; + } + + void Renderer::renderWaterEffects() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const World *world = game->getWorld(); + const WaterEffects *we = world->getWaterEffects(); + const Map *map = world->getMap(); + CoreData &coreData = CoreData::getInstance(); + float height = map->getWaterLevel() + 0.001f; + + assertGl(); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT); + glEnable(GL_BLEND); + glDisable(GL_ALPHA_TEST); + glDepthMask(GL_FALSE); + glDepthFunc(GL_LEQUAL); + glEnable(GL_COLOR_MATERIAL); + + //glNormal3f(0.f, 1.f, 0.f); + + //splashes + glBindTexture(GL_TEXTURE_2D, static_cast(coreData.getWaterSplashTexture())->getHandle()); + + //!!! + Vec2f texCoords[4]; + Vec3f vertices[4]; + Vec3f normals[4]; + + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_NORMAL_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + for (int i = 0; i < we->getWaterSplashCount(); ++i) { + const WaterSplash *ws = we->getWaterSplash(i); + + //render only if enabled + if (ws->getEnabled()) { + + //render only if visible + Vec2i intPos = Vec2i(static_cast(ws->getPos().x), static_cast(ws->getPos().y)); + const Vec2i &mapPos = Map::toSurfCoords(intPos); + + bool visible = map->getSurfaceCell(mapPos)->isVisible(world->getThisTeamIndex()); + if (visible == false && world->showWorldForPlayer(world->getThisFactionIndex()) == true) { + visible = true; + } + + if (visible == true) { + float scale = ws->getAnim()*ws->getSize(); + texCoords[0] = Vec2f(0.f, 1.f); + vertices[0] = Vec3f(ws->getPos().x - scale, height, ws->getPos().y + scale); + normals[0] = Vec3f(0.f, 1.f, 0.f); + + texCoords[1] = Vec2f(0.f, 0.f); + vertices[1] = Vec3f(ws->getPos().x - scale, height, ws->getPos().y - scale); + normals[1] = Vec3f(0.f, 1.f, 0.f); + + texCoords[2] = Vec2f(1.f, 1.f); + vertices[2] = Vec3f(ws->getPos().x + scale, height, ws->getPos().y + scale); + normals[2] = Vec3f(0.f, 1.f, 0.f); + + texCoords[3] = Vec2f(1.f, 0.f); + vertices[3] = Vec3f(ws->getPos().x + scale, height, ws->getPos().y - scale); + normals[3] = Vec3f(0.f, 1.f, 0.f); + + glColor4f(1.f, 1.f, 1.f, 1.f - ws->getAnim()); + glTexCoordPointer(2, GL_FLOAT, 0, &texCoords[0]); + glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); + glNormalPointer(GL_FLOAT, 0, &normals[0]); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + + /* + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 1.f); + glVertex3f(ws->getPos().x-scale, height, ws->getPos().y+scale); + glTexCoord2f(0.f, 0.f); + glVertex3f(ws->getPos().x-scale, height, ws->getPos().y-scale); + glTexCoord2f(1.f, 1.f); + glVertex3f(ws->getPos().x+scale, height, ws->getPos().y+scale); + glTexCoord2f(1.f, 0.f); + glVertex3f(ws->getPos().x+scale, height, ws->getPos().y-scale); + glEnd(); + */ + } + } + } + + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderHud() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Texture2D *hudTexture = game->getGui()->getHudTexture(); + if (hudTexture != NULL) { + const Metrics &metrics = Metrics::getInstance(); + renderTextureQuad(0, 0, metrics.getVirtualW(), metrics.getVirtualH(), hudTexture, 1.0f); + } + } + + void Renderer::renderMinimap() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + const World *world = game->getWorld(); + const Minimap *minimap = world->getMinimap(); + + if (minimap == NULL || minimap->getTexture() == NULL) { + return; + } + + const GameCamera *gameCamera = game->getGameCamera(); + const Pixmap2D *pixmap = minimap->getTexture()->getPixmapConst(); + const Metrics &metrics = Metrics::getInstance(); + const WaterEffects *attackEffects = world->getAttackEffects(); + + int mx = metrics.getMinimapX(); + int my = metrics.getMinimapY(); + int mw = metrics.getMinimapW(); + int mh = metrics.getMinimapH(); + + Vec2f zoom = Vec2f( + static_cast(mw) / pixmap->getW(), + static_cast(mh) / pixmap->getH()); + + assertGl(); + + // render minimap border + Vec4f col = game->getGui()->getDisplay()->getColor(); + glColor4f(col.x*0.5f, col.y*0.5f, col.z*0.5f, 1.0); + + int borderWidth = 2; + glBegin(GL_QUADS); + glVertex2i(mx - borderWidth, my - borderWidth); + glVertex2i(mx - borderWidth, my); + glVertex2i(mx + mw + borderWidth, my); + glVertex2i(mx + mw + borderWidth, my - borderWidth); glEnd(); - //objects - if(renderAll == true) { - switch (map->getObject(i, j)) { - case 0: - glColor3f(0.f, 0.f, 0.f); - break; - case 1: - glColor3f(1.f, 0.f, 0.f); - break; - case 2: - glColor3f(1.f, 1.f, 1.f); - break; - case 3: - glColor3f(0.5f, 0.5f, 1.f); - break; - case 4: - glColor3f(0.f, 0.f, 1.f); - break; - case 5: - glColor3f(0.5f, 0.5f, 0.5f); - break; - case 6: - glColor3f(1.f, 0.8f, 0.5f); - break; - case 7: - glColor3f(0.f, 1.f, 1.f); - break; - case 8: - glColor3f(0.7f, 0.1f, 0.3f); - break; - case 9: - glColor3f(0.5f, 1.f, 0.1f); - break; - case 10: - glColor3f(1.f, 0.2f, 0.8f); - break;// we don't render unvisible blocking objects + glBegin(GL_QUADS); + glVertex2i(mx - borderWidth, my + mh + borderWidth); + glVertex2i(mx - borderWidth, my + mh); + glVertex2i(mx + mw + borderWidth, my + mh); + glVertex2i(mx + mw + borderWidth, my + mh + borderWidth); + glEnd(); + + glBegin(GL_QUADS); + glVertex2i(mx - borderWidth, my); + glVertex2i(mx - borderWidth, my + mh); + glVertex2i(mx, my + mh); + glVertex2i(mx, my); + glEnd(); + + glBegin(GL_QUADS); + glVertex2i(mx + mw, my); + glVertex2i(mx + mw, my + mh); + glVertex2i(mx + mw + borderWidth, my + mh); + glVertex2i(mx + mw + borderWidth, my); + glEnd(); + + assertGl(); + + glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_TEXTURE_BIT); + + //draw map + glEnable(GL_TEXTURE_2D); + glEnable(GL_BLEND); + + glActiveTexture(fowTexUnit); + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(minimap->getFowTexture())->getHandle()); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); + + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_ALPHA, GL_TEXTURE); + + glActiveTexture(baseTexUnit); + glBindTexture(GL_TEXTURE_2D, static_cast(minimap->getTexture())->getHandle()); + + glColor4f(0.5f, 0.5f, 0.5f, 0.2f); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.0f, 1.0f); + glMultiTexCoord2f(fowTexUnit, 0.0f, 1.0f); + glVertex2i(mx, my); + + glTexCoord2f(0.0f, 0.0f); + glMultiTexCoord2f(fowTexUnit, 0.0f, 0.0f); + glVertex2i(mx, my + mh); + + glTexCoord2f(1.0f, 1.0f); + glMultiTexCoord2f(fowTexUnit, 1.0f, 1.0f); + glVertex2i(mx + mw, my); + + glTexCoord2f(1.0f, 0.0f); + glMultiTexCoord2f(fowTexUnit, 1.0f, 0.0f); + glVertex2i(mx + mw, my + mh); + glEnd(); + + glDisable(GL_BLEND); + + glActiveTexture(fowTexUnit); + glDisable(GL_TEXTURE_2D); + glActiveTexture(baseTexUnit); + glDisable(GL_TEXTURE_2D); + + glEnable(GL_BLEND); + + const int itemCount = attackEffects->getWaterSplashCount() * 12; + if (itemCount > 0) { + vector vertices; + vertices.resize(itemCount); + vector colors; + colors.resize(itemCount); + + // draw attack alarm + int vertexIndex = 0; + for (int i = 0; i < attackEffects->getWaterSplashCount(); ++i) { + const WaterSplash *ws = attackEffects->getWaterSplash(i); + float scale = (1 / ws->getAnim()*ws->getSize()) * 5; + //glColor4f(1.f, 1.f, 0.f, 1.f-ws->getAnim()); + float alpha = (1.f - ws->getAnim())*0.01f; + Vec2f pos = ws->getPos() / Map::cellScale; + float attackX = mx + pos.x*zoom.x; + float attackY = my + mh - pos.y*zoom.y; + if (ws->getEnabled()) { + // glBegin(GL_QUADS); + // glVertex2f(attackX-scale, attackY-scale); + // glVertex2f(attackX-scale, attackY+scale); + // glVertex2f(attackX+scale, attackY+scale); + // glVertex2f(attackX+scale, attackY-scale); + // glEnd(); + + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX - scale, attackY - scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX - scale, attackY + scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); + vertices[vertexIndex] = Vec2f(attackX, attackY); + vertexIndex++; + + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX + scale, attackY + scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX - scale, attackY + scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); + vertices[vertexIndex] = Vec2f(attackX, attackY); + vertexIndex++; + + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX + scale, attackY + scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX + scale, attackY - scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); + vertices[vertexIndex] = Vec2f(attackX, attackY); + vertexIndex++; + + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX + scale, attackY - scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, alpha); + vertices[vertexIndex] = Vec2f(attackX - scale, attackY - scale); + vertexIndex++; + colors[vertexIndex] = Vec4f(1.f, 1.f, 0.f, 0.8f); + vertices[vertexIndex] = Vec2f(attackX, attackY); + vertexIndex++; + + /* + glBegin(GL_TRIANGLES); + glColor4f(1.f, 1.f, 0.f, alpha); + glVertex2f(attackX-scale, attackY-scale); + glVertex2f(attackX-scale, attackY+scale); + glColor4f(1.f, 1.f, 0.f, 0.8f); + glVertex2f(attackX, attackY); + glEnd(); + glBegin(GL_TRIANGLES); + glColor4f(1.f, 1.f, 0.f, alpha); + glVertex2f(attackX-scale, attackY+scale); + glVertex2f(attackX+scale, attackY+scale); + glColor4f(1.f, 1.f, 0.f, 0.8f); + glVertex2f(attackX, attackY); + glEnd(); + glBegin(GL_TRIANGLES); + glColor4f(1.f, 1.f, 0.f, alpha); + glVertex2f(attackX+scale, attackY+scale); + glVertex2f(attackX+scale, attackY-scale); + glColor4f(1.f, 1.f, 0.f, 0.8f); + glVertex2f(attackX, attackY); + glEnd(); + glBegin(GL_TRIANGLES); + glColor4f(1.f, 1.f, 0.f, alpha); + glVertex2f(attackX+scale, attackY-scale); + glVertex2f(attackX-scale, attackY-scale); + glColor4f(1.f, 1.f, 0.f, 0.8f); + glVertex2f(attackX, attackY); + glEnd(); + */ + + } } - if ( renderAll && (map->getObject(i, j) != 0) && (map->getObject(i, j) != 10) ) { - glPointSize(cellSize / 2.f); - glBegin(GL_POINTS); - glVertex2f(i * cellSize + cellSize / 2.f, clientH - j * cellSize - cellSize / 2.f); - glEnd(); + if (vertexIndex > 0) { + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glColorPointer(4, GL_FLOAT, 0, &colors[0]); + glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); + glDrawArrays(GL_TRIANGLE_STRIP, 0, vertexIndex); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); } } - // bool found = false; + glDisable(GL_BLEND); - //height lines - // if (!found) { + //draw units + VisibleQuadContainerCache &qCache = getQuadCache(); + std::vector visibleUnitList = qCache.visibleUnitList; - //left - if (i > 0 && map->getHeight(i - 1, j) > map->getHeight(i, j)) { - glColor3fv((surfColor*0.5f).ptr()); - glBegin(GL_LINES); - glVertex2f(i * cellSize, clientH - (j + 1) * cellSize); - glVertex2f(i * cellSize, clientH - j * cellSize); - glEnd(); - } - //down - if (j > 0 && map->getHeight(i, j - 1) > map->getHeight(i, j)) { - glColor3fv((surfColor*0.5f).ptr()); - glBegin(GL_LINES); - glVertex2f(i * cellSize, clientH - j * cellSize); - glVertex2f((i + 1) * cellSize, clientH - j * cellSize); - glEnd(); + const bool showAllUnitsInMinimap = Config::getInstance().getBool("DebugGameSynchUI", "false"); + if (showAllUnitsInMinimap == true) { + visibleUnitList.clear(); + + const World *world = game->getWorld(); + for (unsigned int i = 0; i < (unsigned int) world->getFactionCount(); ++i) { + const Faction *faction = world->getFaction(i); + for (unsigned int j = 0; j < (unsigned int) faction->getUnitCount(); ++j) { + Unit *unit = faction->getUnit(j); + visibleUnitList.push_back(unit); + } } + } + if (visibleUnitList.empty() == false) { + uint32 unitIdx = 0; + vector unit_vertices; + unit_vertices.resize(visibleUnitList.size() * 4); + vector unit_colors; + unit_colors.resize(visibleUnitList.size() * 4); - //left - if (i > 0 && map->getHeight(i - 1, j) < map->getHeight(i, j)) { - glColor3fv((surfColor*2.f).ptr()); - glBegin(GL_LINES); - glVertex2f(i * cellSize, clientH - (j + 1) * cellSize); - glVertex2f(i * cellSize, clientH - j * cellSize); - glEnd(); - } - if (j > 0 && map->getHeight(i, j - 1) < map->getHeight(i, j)) { - glColor3fv((surfColor*2.f).ptr()); - glBegin(GL_LINES); - glVertex2f(i * cellSize, clientH - j * cellSize); - glVertex2f((i + 1) * cellSize, clientH - j * cellSize); - glEnd(); - } - // } - - //resources - if(renderAll == true) { - switch (map->getResource(i, j)) { - case 1: glColor3f(1.f, 1.f, 0.f); break; - case 2: glColor3f(0.5f, 0.5f, 0.5f); break; - case 3: glColor3f(1.f, 0.f, 0.f); break; - case 4: glColor3f(0.f, 0.f, 1.f); break; - case 5: glColor3f(0.5f, 0.5f, 1.f); break; + for (int visibleIndex = 0; + visibleIndex < (int) visibleUnitList.size(); ++visibleIndex) { + Unit *unit = visibleUnitList[visibleIndex]; + if (unit->isAlive() == false) { + continue; } - if (renderAll && map->getResource(i, j) != 0) { + Vec2i pos = unit->getPos() / Map::cellScale; + int size = unit->getType()->getSize(); + Vec3f color = unit->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + + unit_colors[unitIdx] = color; + unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = color; + unit_vertices[unitIdx] = Vec2f(mx + (pos.x + 1)*zoom.x + size, my + mh - (pos.y*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = color; + unit_vertices[unitIdx] = Vec2f(mx + (pos.x + 1)*zoom.x + size, my + mh - ((pos.y + size)*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = color; + unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - ((pos.y + size)*zoom.y)); + unitIdx++; + } + + if (unitIdx > 0) { + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glColorPointer(3, GL_FLOAT, 0, &unit_colors[0]); + glVertexPointer(2, GL_FLOAT, 0, &unit_vertices[0]); + glDrawArrays(GL_QUADS, 0, unitIdx); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + } + + } + + renderMarkedCellsOnMinimap(); + + //draw camera + float wRatio = static_cast(metrics.getMinimapW()) / world->getMap()->getW(); + float hRatio = static_cast(metrics.getMinimapH()) / world->getMap()->getH(); + + int x = static_cast(gameCamera->getPos().x * wRatio); + int y = static_cast(gameCamera->getPos().z * hRatio); + + float ang = degToRad(gameCamera->getHAng()); + + glEnable(GL_BLEND); + + int x1; + int y1; + x1 = mx + x + static_cast(20 * std::sin(ang - pi / 5)); + y1 = my + mh - (y - static_cast(20 * std::cos(ang - pi / 5))); + + int x2; + int y2; + x2 = mx + x + static_cast(20 * std::sin(ang + pi / 5)); + y2 = my + mh - (y - static_cast(20 * std::cos(ang + pi / 5))); + + glColor4f(1.f, 1.f, 1.f, 1.f); + glBegin(GL_TRIANGLES); + + glVertex2i(mx + x, my + mh - y); + glColor4f(1.f, 1.f, 1.f, 0.0f); + glVertex2i(x1, y1); + glColor4f(1.f, 1.f, 1.f, 0.0f); + glVertex2i(x2, y2); + + glEnd(); + + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderHighlightedCellsOnMinimap() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + // Draw marked cells + const std::vector *highlightedCells = game->getHighlightedCells(); + if (highlightedCells->empty() == false) { + //const Map *map= game->getWorld()->getMap(); + const World *world = game->getWorld(); + const Minimap *minimap = world->getMinimap(); + int pointersize = 10; + if (minimap == NULL || minimap->getTexture() == NULL) { + return; + } + + //const GameCamera *gameCamera= game->getGameCamera(); + const Pixmap2D *pixmap = minimap->getTexture()->getPixmapConst(); + const Metrics &metrics = Metrics::getInstance(); + + + //int mx= metrics.getMinimapX(); + int my = metrics.getMinimapY(); + int mw = metrics.getMinimapW(); + int mh = metrics.getMinimapH(); + + Vec2f zoom = Vec2f( + static_cast(mw) / pixmap->getW() / 2, + static_cast(mh) / pixmap->getH() / 2); + + for (int i = 0; i < (int) highlightedCells->size(); i++) { + const MarkedCell *mc = &highlightedCells->at(i); + if (mc->getFaction() == NULL || (mc->getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { + const Texture2D *texture = game->getHighlightCellTexture(); + Vec3f color(MarkedCell::static_system_marker_color); + if (mc->getFaction() != NULL) { + color = mc->getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + } + int lighting = (mc->getAliveCount() % 15); + Vec3f myColor = Vec3f(color.x / 2 + .5f / lighting, color.y / 2 + .5f / lighting, color.z / 2 + .5f / lighting); + + Vec2i pos = mc->getTargetPos(); + if (texture != NULL) { + //float alpha = 0.49f+0.5f/(mc->getAliveCount()%15); + float alpha = 1.0f; + renderTextureQuad((int) (pos.x*zoom.x) + pointersize, my + mh - (int) (pos.y*zoom.y), pointersize, pointersize, texture, alpha, &myColor); + } + } + } + } + } + + void Renderer::renderMarkedCellsOnMinimap() { + // Draw marked cells + std::map markedCells = game->getMapMarkedCellList(); + if (markedCells.empty() == false) { + //const Map *map= game->getWorld()->getMap(); + const World *world = game->getWorld(); + const Minimap *minimap = world->getMinimap(); + + if (minimap == NULL || minimap->getTexture() == NULL) { + return; + } + + //const GameCamera *gameCamera= game->getGameCamera(); + const Pixmap2D *pixmap = minimap->getTexture()->getPixmapConst(); + const Metrics &metrics = Metrics::getInstance(); + //const WaterEffects *attackEffects= world->getAttackEffects(); + + int mx = metrics.getMinimapX(); + int my = metrics.getMinimapY(); + int mw = metrics.getMinimapW(); + int mh = metrics.getMinimapH(); + + Vec2f zoom = Vec2f( + static_cast(mw) / pixmap->getW(), + static_cast(mh) / pixmap->getH()); + + uint32 unitIdx = 0; + vector unit_vertices; + unit_vertices.resize(markedCells.size() * 4); + vector unit_colors; + unit_colors.resize(markedCells.size() * 4); + + for (std::map::iterator iterMap = markedCells.begin(); + iterMap != markedCells.end(); ++iterMap) { + MarkedCell &bm = iterMap->second; + if (bm.getPlayerIndex() < 0 || + (bm.getFaction() != NULL && + bm.getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { + Vec2i pos = bm.getTargetPos() / Map::cellScale; + float size = 0.5f; + + Vec3f color(MarkedCell::static_system_marker_color); + if (bm.getFaction() != NULL) { + color = bm.getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + } + float alpha = 0.65f; + + unit_colors[unitIdx] = Vec4f(color.x, color.y, color.z, alpha); + unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - (pos.y*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = Vec4f(color.x, color.y, color.z, alpha); + unit_vertices[unitIdx] = Vec2f(mx + (pos.x + 1)*zoom.x + size, my + mh - (pos.y*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = Vec4f(color.x, color.y, color.z, alpha); + unit_vertices[unitIdx] = Vec2f(mx + (pos.x + 1)*zoom.x + size, my + mh - ((pos.y + size)*zoom.y)); + unitIdx++; + + unit_colors[unitIdx] = Vec4f(color.x, color.y, color.z, alpha); + unit_vertices[unitIdx] = Vec2f(mx + pos.x*zoom.x, my + mh - ((pos.y + size)*zoom.y)); + unitIdx++; + } + } + + if (unitIdx > 0) { + glEnable(GL_BLEND); + + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_VERTEX_ARRAY); + glColorPointer(4, GL_FLOAT, 0, &unit_colors[0]); + glVertexPointer(2, GL_FLOAT, 0, &unit_vertices[0]); + glDrawArrays(GL_QUADS, 0, unitIdx); + //glDrawArrays(GL_TRIANGLE_STRIP, 0, unitIdx); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + glDisable(GL_BLEND); + } + } + } + void Renderer::renderVisibleMarkedCells(bool renderTextHint, int x, int y) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + // Draw marked cells + std::map markedCells = game->getMapMarkedCellList(); + if (markedCells.empty() == false) { + const Texture2D *texture = game->getMarkCellTexture(); + const int yOffset = -40; + + for (std::map::iterator iterMap = markedCells.begin(); + iterMap != markedCells.end(); ++iterMap) { + MarkedCell &bm = iterMap->second; + if (bm.getPlayerIndex() < 0 || + (bm.getFaction() != NULL && + bm.getFaction()->getTeam() == game->getWorld()->getThisFaction()->getTeam())) { + const Map *map = game->getWorld()->getMap(); + std::pair bmVisible = posInCellQuadCache( + map->toSurfCoords(bm.getTargetPos())); + if (bmVisible.first == true) { + if (renderTextHint == true) { + if (bm.getNote() != "") { + bool validPosObjWorld = x > bmVisible.second.x && + y > bmVisible.second.y + yOffset && + x < bmVisible.second.x + texture->getTextureWidth() && + y < bmVisible.second.y + yOffset + texture->getTextureHeight(); + + if (validPosObjWorld) { + //printf("Checking for hint text render mouse [%d,%d] marker pos [%d,%d] validPosObjWorld = %d, hint [%s]\n",x,y,bm.getTargetPos().x,bm.getTargetPos().y,validPosObjWorld,bm.getNote().c_str()); + + //Lang &lang= Lang::getInstance(); + Vec4f fontColor = Vec4f(1.0f, 1.0f, 1.0f, 0.25f); + + if (renderText3DEnabled == true) { + renderTextShadow3D(bm.getNote(), CoreData::getInstance().getConsoleFont3D(), fontColor, + bmVisible.second.x, bmVisible.second.y); + } else { + renderTextShadow(bm.getNote(), CoreData::getInstance().getConsoleFont(), fontColor, + bmVisible.second.x, bmVisible.second.y); + } + } + } + } else { + + + /* + //texture 0 + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + //set color to interpolation + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_INTERPOLATE); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE1); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); + + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + //texture 1 + glActiveTexture(GL_TEXTURE1); + glMultiTexCoord2f(GL_TEXTURE1, 0.f, 0.f); + glEnable(GL_TEXTURE_2D); + + glBindTexture(GL_TEXTURE_2D, static_cast(bm.getFaction()->getTexture())->getHandle()); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_MODULATE); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_PREVIOUS); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB, GL_SRC_COLOR); + + //set alpha to 1 + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + + glActiveTexture(GL_TEXTURE0); + */ + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + Vec3f color(MarkedCell::static_system_marker_color); + if (bm.getFaction() != NULL) { + color = bm.getFaction()->getTexture()->getPixmapConst()->getPixel3f(0, 0); + } + + renderTextureQuad( + bmVisible.second.x, bmVisible.second.y + yOffset, + texture->getTextureWidth(), texture->getTextureHeight(), texture, 0.8f, &color); + + /* + glActiveTexture(GL_TEXTURE1); + glDisable(GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + */ + } + } + } + } + } + } + + void Renderer::renderDisplay() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + CoreData &coreData = CoreData::getInstance(); + const Metrics &metrics = Metrics::getInstance(); + const Display *display = game->getGui()->getDisplay(); + + glPushAttrib(GL_ENABLE_BIT); + + if (renderText3DEnabled == true) { + //infoString + renderTextShadow3D( + display->getInfoText().c_str(), + coreData.getDisplayFont3D(), + display->getColor(), + metrics.getDisplayX(), + metrics.getDisplayY() + Display::infoStringY); + + //title + renderTextShadow3D( + display->getTitle().c_str(), + coreData.getDisplayFont3D(), + display->getColor(), + metrics.getDisplayX() + 40, + metrics.getDisplayY() + metrics.getDisplayH() - 20); + + glColor3f(0.0f, 0.0f, 0.0f); + + //text + renderTextShadow3D( + display->getText().c_str(), + coreData.getDisplayFont3D(), + display->getColor(), + metrics.getDisplayX() - 1, + metrics.getDisplayY() + metrics.getDisplayH() - 56); + + //progress Bar + if (display->getProgressBar() != -1) { + renderProgressBar3D( + display->getProgressBar(), + metrics.getDisplayX(), + metrics.getDisplayY() + metrics.getDisplayH() - 50, + coreData.getDisplayFontSmall3D(), 175); + } + } else { + //infoString + renderTextShadow( + display->getInfoText().c_str(), + coreData.getDisplayFont(), + display->getColor(), + metrics.getDisplayX(), + metrics.getDisplayY() + Display::infoStringY); + + //title + renderTextShadow( + display->getTitle().c_str(), + coreData.getDisplayFont(), + display->getColor(), + metrics.getDisplayX() + 40, + metrics.getDisplayY() + metrics.getDisplayH() - 20); + + glColor3f(0.0f, 0.0f, 0.0f); + + //text + renderTextShadow( + display->getText().c_str(), + coreData.getDisplayFont(), + display->getColor(), + metrics.getDisplayX() - 1, + metrics.getDisplayY() + metrics.getDisplayH() - 56); + + //progress Bar + if (display->getProgressBar() != -1) { + renderProgressBar( + display->getProgressBar(), + metrics.getDisplayX(), + metrics.getDisplayY() + metrics.getDisplayH() - 50, + coreData.getDisplayFontSmall()); + } + } + + //up images + glEnable(GL_TEXTURE_2D); + + glColor3f(1.f, 1.f, 1.f); + for (int i = 0; i < Display::upCellCount; ++i) { + if (display->getUpImage(i) != NULL) { + renderQuad( + metrics.getDisplayX() + display->computeUpX(i), + metrics.getDisplayY() + display->computeUpY(i), + display->getUpImageSize(), display->getUpImageSize(), display->getUpImage(i)); + } + } + + //down images + for (int i = 0; i < Display::downCellCount; ++i) { + if (display->getDownImage(i) != NULL) { + if (display->getDownLighted(i)) { + glColor3f(1.f, 1.f, 1.f); + } else { + glColor3f(0.3f, 0.3f, 0.3f); + } + + int x = metrics.getDisplayX() + display->computeDownX(i); + int y = metrics.getDisplayY() + display->computeDownY(i); + int size = Display::imageSize; + + if (display->getDownSelectedPos() == i) { + x -= 3; + y -= 3; + size += 6; + } + + renderQuad(x, y, size, size, display->getDownImage(i)); + } + } + + //selection + int downPos = display->getDownSelectedPos(); + if (downPos != Display::invalidPos) { + const Texture2D *texture = display->getDownImage(downPos); + if (texture != NULL) { + int x = metrics.getDisplayX() + display->computeDownX(downPos) - 3; + int y = metrics.getDisplayY() + display->computeDownY(downPos) - 3; + int size = Display::imageSize + 6; + renderQuad(x, y, size, size, display->getDownImage(downPos)); + } + } + + glPopAttrib(); + } + + void Renderer::renderMenuBackground(const MenuBackground *menuBackground) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + assertGl(); + + const Vec3f &cameraPosition = menuBackground->getCamera()->getConstPosition(); + + glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + + //clear + Vec4f fogColor = Vec4f(0.4f, 0.4f, 0.4f, 1.f) * menuBackground->getFade(); + glClearColor(fogColor.x, fogColor.y, fogColor.z, 1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glFogfv(GL_FOG_COLOR, fogColor.ptr()); + + //light + Vec4f lightPos = Vec4f(10.f, 10.f, 10.f, 1.f)* menuBackground->getFade(); + Vec4f diffLight = Vec4f(0.9f, 0.9f, 0.9f, 1.f)* menuBackground->getFade(); + Vec4f ambLight = Vec4f(0.3f, 0.3f, 0.3f, 1.f)* menuBackground->getFade(); + Vec4f specLight = Vec4f(0.1f, 0.1f, 0.1f, 1.f)* menuBackground->getFade(); + + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight.ptr()); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight.ptr()); + glLightfv(GL_LIGHT0, GL_SPECULAR, specLight.ptr()); + + //main model + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.5f); + modelRenderer->begin(true, true, true, false); + menuBackground->getMainModelPtr()->updateInterpolationData(menuBackground->getAnim(), true); + modelRenderer->render(menuBackground->getMainModelPtr()); + modelRenderer->end(); + glDisable(GL_ALPHA_TEST); + + //characters + float dist = menuBackground->getAboutPosition().dist(cameraPosition); + float minDist = 3.f; + if (dist < minDist) { + + glAlphaFunc(GL_GREATER, 0.0f); + float alpha = clamp((minDist - dist) / minDist, 0.f, 1.f); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); + + std::vector &characterMenuScreenPositionListCache = + CacheManager::getCachedItem< std::vector >(GameConstants::characterMenuScreenPositionListCacheLookupKey); + characterMenuScreenPositionListCache.clear(); + + modelRenderer->begin(true, true, false, false); + + for (int i = 0; i < MenuBackground::characterCount; ++i) { + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + Vec3f worldPos(i*2.f - 4.f, -1.4f, -7.5f); + glTranslatef(worldPos.x, worldPos.y, worldPos.z); + + // + // Get the screen coordinates for each character model - START + //std::vector projection(16); + //std::vector modelview(16); + //std::vector screen_coords(3); + + //glGetDoublev(GL_PROJECTION_MATRIX, projection.data()); + //glGetDoublev(GL_MODELVIEW_MATRIX, modelview.data()); + + const Metrics &metrics = Metrics::getInstance(); + GLint viewport[] = { 0, 0, metrics.getVirtualW(), metrics.getVirtualH() }; + + //get matrices + GLdouble projection[16]; + glGetDoublev(GL_PROJECTION_MATRIX, projection); + + GLdouble modelview[16]; + glGetDoublev(GL_MODELVIEW_MATRIX, modelview); + + //get the screen coordinates + GLdouble screen_coords[3]; + + gluProject(worldPos.x, worldPos.y, worldPos.z, + modelview, projection, viewport, + &screen_coords[0], &screen_coords[1], &screen_coords[2]); + characterMenuScreenPositionListCache.push_back(Vec3f(screen_coords[0], screen_coords[1], screen_coords[2])); + // Get the screen coordinates for each character model - END + // + + menuBackground->getCharacterModelPtr(i)->updateInterpolationData(menuBackground->getAnim(), true); + modelRenderer->render(menuBackground->getCharacterModelPtr(i)); + + glPopMatrix(); + } + modelRenderer->end(); + } + + //water + if (menuBackground->getWater()) { + + //water surface + const int waterTesselation = 10; + const int waterSize = 250; + const int waterQuadSize = 2 * waterSize / waterTesselation; + const float waterHeight = menuBackground->getWaterHeight(); + + glEnable(GL_BLEND); + + glNormal3f(0.f, 1.f, 0.f); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.f, 1.f, 1.f, 1.f).ptr()); + GLuint waterHandle = static_cast(menuBackground->getWaterTexture())->getHandle(); + glBindTexture(GL_TEXTURE_2D, waterHandle); + for (int i = 1; i < waterTesselation; ++i) { + glBegin(GL_TRIANGLE_STRIP); + for (int j = 1; j < waterTesselation; ++j) { + glTexCoord2i(1, 2 % j); + glVertex3f(-waterSize + i * waterQuadSize, waterHeight, -waterSize + j * waterQuadSize); + glTexCoord2i(0, 2 % j); + glVertex3f(-waterSize + (i + 1)*waterQuadSize, waterHeight, -waterSize + j * waterQuadSize); + } + glEnd(); + } + glDisable(GL_BLEND); + + //raindrops + if (menuBackground->getRain()) { + const float maxRaindropAlpha = 0.5f; + + glEnable(GL_BLEND); + glDisable(GL_LIGHTING); + glDisable(GL_ALPHA_TEST); + glDepthMask(GL_FALSE); + + //splashes + CoreData &coreData = CoreData::getInstance(); + glBindTexture(GL_TEXTURE_2D, static_cast(coreData.getWaterSplashTexture())->getHandle()); + for (int i = 0; i < MenuBackground::raindropCount; ++i) { + + Vec2f pos = menuBackground->getRaindropPos(i); + float scale = menuBackground->getRaindropState(i); + float alpha = maxRaindropAlpha - scale * maxRaindropAlpha; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glColor4f(1.f, 1.f, 1.f, alpha); + glTranslatef(pos.x, waterHeight + 0.01f, pos.y); + + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(0.f, 1.f); + glVertex3f(-scale, 0, scale); + glTexCoord2f(0.f, 0.f); + glVertex3f(-scale, 0, -scale); + glTexCoord2f(1.f, 1.f); + glVertex3f(scale, 0, scale); + glTexCoord2f(1.f, 0.f); + glVertex3f(scale, 0, -scale); + glEnd(); + + glPopMatrix(); + } + } + } + + glPopAttrib(); + + assertGl(); + } + + void Renderer::renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels, const Vec3f characterPosition, float anim) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + assertGl(); + + const Vec3f &cameraPosition = camera->getConstPosition(); + + glPushAttrib(GL_LIGHTING_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT); + + //clear + //Vec4f fogColor= Vec4f(0.4f, 0.4f, 0.4f, 1.f) * fade; + // Show black bacground + Vec4f fogColor = Vec4f(0.f, 0.f, 0.f, 1.f); + glClearColor(fogColor.x, fogColor.y, fogColor.z, 1.f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glFogfv(GL_FOG_COLOR, fogColor.ptr()); + + //light + Vec4f lightPos = Vec4f(10.f, 10.f, 10.f, 1.f) * fade; + Vec4f diffLight = Vec4f(0.9f, 0.9f, 0.9f, 1.f) * fade; + Vec4f ambLight = Vec4f(0.3f, 0.3f, 0.3f, 1.f) * fade; + Vec4f specLight = Vec4f(0.1f, 0.1f, 0.1f, 1.f) * fade; + + glEnable(GL_LIGHT0); + glLightfv(GL_LIGHT0, GL_POSITION, lightPos.ptr()); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffLight.ptr()); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambLight.ptr()); + glLightfv(GL_LIGHT0, GL_SPECULAR, specLight.ptr()); + + //main model + if (mainModel) { + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.5f); + modelRenderer->begin(true, true, true, false); + mainModel->updateInterpolationData(anim, true); + modelRenderer->render(mainModel); + modelRenderer->end(); + glDisable(GL_ALPHA_TEST); + } + + //characters + if (characterModels.empty() == false) { + float dist = characterPosition.dist(cameraPosition); + float minDist = 3.f; + if (dist < minDist) { + glAlphaFunc(GL_GREATER, 0.0f); + float alpha = clamp((minDist - dist) / minDist, 0.f, 1.f); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, Vec4f(1.0f, 1.0f, 1.0f, alpha).ptr()); + modelRenderer->begin(true, true, false, false); + + for (unsigned int i = 0; i < characterModels.size(); ++i) { + if (characterModels[i]) { + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glTranslatef(i*2.f - 4.f, -1.4f, -7.5f); + characterModels[i]->updateInterpolationData(anim, true); + modelRenderer->render(characterModels[i]); + glPopMatrix(); + } + } + modelRenderer->end(); + } + } + + + glPopAttrib(); + + assertGl(); + } + + // ==================== computing ==================== + + bool Renderer::ccomputePosition(const Vec2i &screenPos, Vec2i &worldPos, bool exactCoords) { + assertGl(); + const Map* map = game->getWorld()->getMap(); + const Metrics &metrics = Metrics::getInstance(); + float depth = 0.0f; + GLdouble modelviewMatrix[16]; + GLdouble projectionMatrix[16]; + GLint viewport[4] = { 0, 0, metrics.getScreenW(), metrics.getScreenH() }; + GLdouble worldX; + GLdouble worldY; + GLdouble worldZ; + GLint screenX = (screenPos.x * metrics.getScreenW() / metrics.getVirtualW()); + GLint screenY = (screenPos.y * metrics.getScreenH() / metrics.getVirtualH()); + + //get the depth in the cursor pixel + glReadPixels(screenX, screenY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth); + + //load matrices + loadProjectionMatrix(); + loadGameCameraMatrix(); + + //get matrices + glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); + glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); + + //get the world coordinates + gluUnProject( + screenX, screenY, depth, + modelviewMatrix, projectionMatrix, viewport, + &worldX, &worldY, &worldZ); + + //conver coords to int + if (exactCoords == true) { + worldPos = Vec2i(static_cast(worldX), static_cast(worldZ)); + } else { + worldPos = Vec2i(static_cast(worldX + 0.5f), static_cast(worldZ + 0.5f)); + } + + //clamp coords to map size + return map->isInside(worldPos); + } + + // This method takes world co-ordinates and translates them to screen co-ords + Vec3f Renderer::computeScreenPosition(const Vec3f &worldPos) { + if (worldToScreenPosCache.find(worldPos) != worldToScreenPosCache.end()) { + return worldToScreenPosCache[worldPos]; + } + assertGl(); + + const Metrics &metrics = Metrics::getInstance(); + GLint viewport[] = { 0, 0, metrics.getVirtualW(), metrics.getVirtualH() }; + GLdouble worldX = worldPos.x; + GLdouble worldY = worldPos.y; + GLdouble worldZ = worldPos.z; + + //load matrices + loadProjectionMatrix(); + loadGameCameraMatrix(); + + //get matrices + GLdouble modelviewMatrix[16]; + glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix); + GLdouble projectionMatrix[16]; + glGetDoublev(GL_PROJECTION_MATRIX, projectionMatrix); + + //get the screen coordinates + GLdouble screenX; + GLdouble screenY; + GLdouble screenZ; + gluProject(worldX, worldY, worldZ, + modelviewMatrix, projectionMatrix, viewport, + &screenX, &screenY, &screenZ); + + Vec3f screenPos(screenX, screenY, screenZ); + worldToScreenPosCache[worldPos] = screenPos; + + return screenPos; + } + + void Renderer::computeSelected(Selection::UnitContainer &units, const Object *&obj, + const bool withObjectSelection, + const Vec2i &posDown, const Vec2i &posUp) { + const string selectionType = toLower(Config::getInstance().getString("SelectionType", Config::colorPicking)); + + if (selectionType == Config::colorPicking) { + selectUsingColorPicking(units, obj, withObjectSelection, posDown, posUp); + } + /// Frustum approach --> Currently not accurate enough + else if (selectionType == Config::frustumPicking) { + selectUsingFrustumSelection(units, obj, withObjectSelection, posDown, posUp); + } else { + selectUsingSelectionBuffer(units, obj, withObjectSelection, posDown, posUp); + } + } + + void Renderer::selectUsingFrustumSelection(Selection::UnitContainer &units, + const Object *&obj, const bool withObjectSelection, + const Vec2i &posDown, const Vec2i &posUp) { + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + const Metrics &metrics = Metrics::getInstance(); + GLint view[] = { 0, 0, metrics.getVirtualW(), metrics.getVirtualH() }; + + //compute center and dimensions of selection rectangle + int x = (posDown.x + posUp.x) / 2; + int y = (posDown.y + posUp.y) / 2; + int w = abs(posDown.x - posUp.x); + int h = abs(posDown.y - posUp.y); + if (w < 2) { + w = 2; + } + if (h < 2) { + h = 2; + } + + gluPickMatrix(x, y, w, h, view); + gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); + loadGameCameraMatrix(); + + VisibleQuadContainerCache quadSelectionCacheItem; + ExtractFrustum(quadSelectionCacheItem); + + //pop matrices + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + if (unit != NULL && unit->isAlive()) { + Vec3f unitPos = unit->getCurrMidHeightVector(); + bool insideQuad = CubeInFrustum(quadSelectionCacheItem.frustumData, + unitPos.x, unitPos.y, unitPos.z, unit->getType()->getRenderSize()); + if (insideQuad == true) { + units.push_back(unit); + } + } + } + } + + if (withObjectSelection == true) { + if (qCache.visibleObjectList.empty() == false) { + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleObjectList.size(); ++visibleIndex) { + Object *object = qCache.visibleObjectList[visibleIndex]; + if (object != NULL) { + bool insideQuad = CubeInFrustum(quadSelectionCacheItem.frustumData, + object->getPos().x, object->getPos().y, object->getPos().z, 1); + if (insideQuad == true) { + obj = object; + //if(withObjectSelection == true) { + break; + //} + } + } + } + } + } + } + + void Renderer::selectUsingSelectionBuffer(Selection::UnitContainer &units, + const Object *&obj, const bool withObjectSelection, + const Vec2i &posDown, const Vec2i &posUp) { + //compute center and dimensions of selection rectangle + int x = (posDown.x + posUp.x) / 2; + int y = (posDown.y + posUp.y) / 2; + int w = abs(posDown.x - posUp.x); + int h = abs(posDown.y - posUp.y); + if (w < 2) { + w = 2; + } + if (h < 2) { + h = 2; + } + + //declarations + GLuint selectBuffer[Gui::maxSelBuff]; + + //setup matrices + glSelectBuffer(Gui::maxSelBuff, selectBuffer); + //glMatrixMode(GL_PROJECTION); + //glPushMatrix(); + + GLint renderModeResult = glRenderMode(GL_SELECT); + if (renderModeResult < 0) { + const char *errorString = reinterpret_cast(gluErrorString(renderModeResult)); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "OpenGL error #%d [0x%X] : [%s] at file: [%s], line: %d", renderModeResult, renderModeResult, errorString, extractFileFromDirectoryPath(__FILE__).c_str(), __LINE__); + + printf("%s\n", szBuf); + } + + + glPushMatrix(); + glMatrixMode(GL_PROJECTION); + + glLoadIdentity(); + + const Metrics &metrics = Metrics::getInstance(); + GLint view[] = { 0, 0, metrics.getVirtualW(), metrics.getVirtualH() }; + //GLint view[4]; + //glGetIntegerv(GL_VIEWPORT, view); + + gluPickMatrix(x, y, w, h, view); + gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); + //gluPerspective(perspFov, metrics.getAspectRatio(), 0.0001, 1000.0); + //gluPerspective(perspFov, (float)view[2]/(float)view[3], perspNearPlane, perspFarPlane); + loadGameCameraMatrix(); + + //render units to find which ones should be selected + renderUnitsFast(); + if (withObjectSelection == true) { + renderObjectsFast(false, true); + } + + //pop matrices + glPopMatrix(); + + // Added this to ensure all the selection calls are done now + // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) + //glFlush(); + + //select units by checking the selected buffer + int selCount = glRenderMode(GL_RENDER); + if (selCount > 0) { + VisibleQuadContainerCache &qCache = getQuadCache(); + for (int i = 1; i <= selCount; ++i) { + int index = selectBuffer[i * 4 - 1]; + if (index >= OBJECT_SELECT_OFFSET) { + Object *object = qCache.visibleObjectList[index - OBJECT_SELECT_OFFSET]; + if (object != NULL) { + obj = object; + if (withObjectSelection == true) { + break; + } + } + } else { + Unit *unit = qCache.visibleQuadUnitList[index]; + if (unit != NULL && unit->isAlive()) { + units.push_back(unit); + } + } + } + } else if (selCount < 0) { + const char *errorString = reinterpret_cast(gluErrorString(selCount)); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "OpenGL error #%d [0x%X] : [%s] at file: [%s], line: %d", selCount, selCount, errorString, extractFileFromDirectoryPath(__FILE__).c_str(), __LINE__); + + printf("%s\n", szBuf); + } + } + + void Renderer::selectUsingColorPicking(Selection::UnitContainer &units, + const Object *&obj, const bool withObjectSelection, + const Vec2i &posDown, const Vec2i &posUp) { + int x1 = posDown.x; + int y1 = posDown.y; + int x2 = posUp.x; + int y2 = posUp.y; + + int x = min(x1, x2); + int y = min(y1, y2); + int w = max(x1, x2) - min(x1, x2); + int h = max(y1, y2) - min(y1, y2); + if (w < 2) { + w = 2; + } + if (h < 2) { + h = 2; + } + + const Metrics &metrics = Metrics::getInstance(); + x = (x * metrics.getScreenW() / metrics.getVirtualW()); + y = (y * metrics.getScreenH() / metrics.getVirtualH()); + + w = (w * metrics.getScreenW() / metrics.getVirtualW()); + h = (h * metrics.getScreenH() / metrics.getVirtualH()); + + PixelBufferWrapper::begin(); + + + glPushMatrix(); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + //GLint view[]= {0, 0, metrics.getVirtualW(), metrics.getVirtualH()}; + //gluPickMatrix(x, y, w, h, view); + gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); + loadGameCameraMatrix(); + + //render units to find which ones should be selected + //printf("In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + vector rendererUnits = renderUnitsFast(false, true); + //printf("In [%s::%s] Line: %d rendererUnits = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,rendererUnits.size()); + + + // Added this to ensure all the selection calls are done now + // (see http://www.unknownroad.com/rtfm/graphics/glselection.html section: [0x4]) + //glFlush(); + + //GraphicsInterface::getInstance().getCurrentContext()->swapBuffers(); + + PixelBufferWrapper::end(); + + vector unitsVector; + bool unitFound = false; + + if (rendererUnits.empty() == false) { + copy(rendererUnits.begin(), rendererUnits.end(), std::inserter(unitsVector, unitsVector.begin())); + } + + if (unitsVector.empty() == false) { + vector pickedList = BaseColorPickEntity::getPickedList(x, y, w, h, unitsVector); + //printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size()); + + if (pickedList.empty() == false) { + units.reserve(pickedList.size()); + for (unsigned int i = 0; i < pickedList.size(); ++i) { + int index = pickedList[i]; + //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size()); + + if (rendererUnits.empty() == false && index < (int) rendererUnits.size()) { + Unit *unit = rendererUnits[index]; + if (unit != NULL && unit->isAlive()) { + unitFound = true; + units.push_back(unit); + } + } + } + } + } + + if (withObjectSelection == true && unitFound == false) { + vector rendererObjects; + vector objectsVector; + rendererObjects = renderObjectsFast(false, true, true); + if (rendererObjects.empty() == false) { + copy(rendererObjects.begin(), rendererObjects.end(), std::inserter(objectsVector, objectsVector.begin())); + } + + if (objectsVector.empty() == false) { + vector pickedList = BaseColorPickEntity::getPickedList(x, y, w, h, objectsVector); + //printf("In [%s::%s] Line: %d pickedList = %d models rendered = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,pickedList.size(),rendererModels.size()); + + if (pickedList.empty() == false) { + for (unsigned int i = 0; i < pickedList.size(); ++i) { + int index = pickedList[i]; + //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d units = %d objects = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,rendererUnits.size(),rendererObjects.size()); + + if (rendererObjects.empty() == false && index < (int) rendererObjects.size()) { + Object *object = rendererObjects[index]; + //printf("In [%s::%s] Line: %d searching for selected object i = %d index = %d [%p]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,index,object); + + if (object != NULL) { + obj = object; + break; + } + } + } + } + } + } + //pop matrices + glPopMatrix(); + } + + // ==================== shadows ==================== + + void Renderer::renderShadowsToTexture(const int renderFps) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (shadowsOffDueToMinRender == false && + (shadows == sProjected || shadows == sShadowMapping)) { + + shadowMapFrame = (shadowMapFrame + 1) % (shadowFrameSkip + 1); + + if (shadowMapFrame == 0) { + assertGl(); + + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_VIEWPORT_BIT | GL_POLYGON_BIT); + + if (shadows == sShadowMapping) { + glClear(GL_DEPTH_BUFFER_BIT); + } else { + float color = 1.0f - shadowIntensity; + glColor3f(color, color, color); + glClearColor(1.f, 1.f, 1.f, 1.f); + glDisable(GL_DEPTH_TEST); + glClear(GL_COLOR_BUFFER_BIT); + } + + //assertGl(); + + //clear color buffer + // + //set viewport, we leave one texel always in white to avoid problems + glViewport(1, 1, shadowTextureSize - 2, shadowTextureSize - 2); + + //assertGl(); + + if (nearestLightPos.w == 0.f) { + //directional light + + //light pos + assert(game != NULL); + assert(game->getWorld() != NULL); + const TimeFlow *tf = game->getWorld()->getTimeFlow(); + assert(tf != NULL); + float ang = tf->isDay() ? computeSunAngle(tf->getTime()) : computeMoonAngle(tf->getTime()); + ang = radToDeg(ang); + + //push and set projection + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + //assertGl(); + + if (game->getGameCamera()->getState() == GameCamera::sGame) { + //glOrtho(-35, 5, -15, 15, -1000, 1000); + //glOrtho(-30, 30, -20, 20, -1000, 1000); + glOrtho(-30, 5, -20, 20, -1000, 1000); + } else { + glOrtho(-30, 30, -20, 20, -1000, 1000); + } + + //assertGl(); + + //push and set modelview + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + + glRotatef(15, 0, 1, 0); + + glRotatef(ang, 1, 0, 0); + glRotatef(90, 0, 1, 0); + const Vec3f &pos = game->getGameCamera()->getPos(); + + glTranslatef(static_cast(-pos.x), 0, static_cast(-pos.z)); + + //assertGl(); + } else { + //non directional light + + //push projection + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + //assertGl(); + + gluPerspective(perspFov, 1.f, perspNearPlane, perspFarPlane); + //const Metrics &metrics= Metrics::getInstance(); + //gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, perspFarPlane); + + + assertGl(); + + //push modelview + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glLoadIdentity(); + glRotatef(-90, -1, 0, 0); + glTranslatef(-nearestLightPos.x, -nearestLightPos.y - 2, -nearestLightPos.z); + + //assertGl(); + } + + if (shadows == sShadowMapping) { + glEnable(GL_POLYGON_OFFSET_FILL); + glPolygonOffset(1.0f, 16.0f); + + //assertGl(); + } + + //render 3d + renderUnitsFast(true); + renderObjectsFast(true, false); + + //assertGl(); + + //read color buffer + glBindTexture(GL_TEXTURE_2D, shadowMapHandle); + assertGl(); + glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowTextureSize, shadowTextureSize); + GLenum error = glGetError(); + // This error can happen when a Linux user switches from an X session + // back to a running game, and 'seems' to be safe to ignore it + if (error != GL_INVALID_OPERATION) { + assertGlWithErrorNumber(error); + } + + //get elemental matrices + static Matrix4f matrix1; + static bool matrix1Populate = true; + if (matrix1Populate == true) { + matrix1Populate = false; + matrix1[0] = 0.5f; matrix1[4] = 0.f; matrix1[8] = 0.f; matrix1[12] = 0.5f; + matrix1[1] = 0.f; matrix1[5] = 0.5f; matrix1[9] = 0.f; matrix1[13] = 0.5f; + matrix1[2] = 0.f; matrix1[6] = 0.f; matrix1[10] = 0.5f; matrix1[14] = 0.5f; + matrix1[3] = 0.f; matrix1[7] = 0.f; matrix1[11] = 0.f; matrix1[15] = 1.f; + } + Matrix4f matrix2; + glGetFloatv(GL_PROJECTION_MATRIX, matrix2.ptr()); + + //assertGl(); + + Matrix4f matrix3; + glGetFloatv(GL_MODELVIEW_MATRIX, matrix3.ptr()); + + //pop both matrices + glPopMatrix(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + + //assertGl(); + + //compute texture matrix + glLoadMatrixf(matrix1.ptr()); + glMultMatrixf(matrix2.ptr()); + glMultMatrixf(matrix3.ptr()); + glGetFloatv(GL_TRANSPOSE_PROJECTION_MATRIX_ARB, shadowMapMatrix.ptr()); + + //assertGl(); + + //if(shadows == sShadowMapping) { + // glDisable(GL_POLYGON_OFFSET_FILL); + // glPolygonOffset(0.0f, 0.0f); + //} + + //pop + glPopMatrix(); + + //assertGl(); + + glPopAttrib(); + + assertGl(); + } + } + } + + + // ==================== gl wrap ==================== + + string Renderer::getGlInfo() { + string infoStr = ""; + Lang &lang = Lang::getInstance(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + infoStr += lang.getString("OpenGlInfo") + ":\n"; + infoStr += " " + lang.getString("OpenGlVersion") + ": "; + infoStr += string((getGlVersion() != NULL ? getGlVersion() : "?")) + "\n"; + infoStr += " " + lang.getString("OpenGlRenderer") + ": "; + infoStr += string((getGlVersion() != NULL ? getGlVersion() : "?")) + "\n"; + infoStr += " " + lang.getString("OpenGlVendor") + ": "; + infoStr += string((getGlVendor() != NULL ? getGlVendor() : "?")) + "\n"; + infoStr += " " + lang.getString("OpenGlMaxLights") + ": "; + infoStr += intToStr(getGlMaxLights()) + "\n"; + infoStr += " " + lang.getString("OpenGlMaxTextureSize") + ": "; + infoStr += intToStr(getGlMaxTextureSize()) + "\n"; + infoStr += " " + lang.getString("OpenGlMaxTextureUnits") + ": "; + infoStr += intToStr(getGlMaxTextureUnits()) + "\n"; + infoStr += " " + lang.getString("OpenGlModelviewStack") + ": "; + infoStr += intToStr(getGlModelviewMatrixStackDepth()) + "\n"; + infoStr += " " + lang.getString("OpenGlProjectionStack") + ": "; + infoStr += intToStr(getGlProjectionMatrixStackDepth()) + "\n"; + } + return infoStr; + } + + string Renderer::getGlMoreInfo() { + string infoStr = ""; + Lang &lang = Lang::getInstance(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + //gl extensions + infoStr += lang.getString("OpenGlExtensions") + ":\n "; + + string extensions = getGlExtensions(); + int charCount = 0; + for (int i = 0; i < (int) extensions.size(); ++i) { + infoStr += extensions[i]; + if (charCount > 120 && extensions[i] == ' ') { + infoStr += "\n "; + charCount = 0; + } + ++charCount; + } + + //platform extensions + infoStr += "\n\n"; + infoStr += lang.getString("OpenGlPlatformExtensions") + ":\n "; + + charCount = 0; + string platformExtensions = getGlPlatformExtensions(); + for (int i = 0; i < (int) platformExtensions.size(); ++i) { + infoStr += platformExtensions[i]; + if (charCount > 120 && platformExtensions[i] == ' ') { + infoStr += "\n "; + charCount = 0; + } + ++charCount; + } + } + + return infoStr; + } + + void Renderer::autoConfig() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + Config &config = Config::getInstance(); + + bool nvidiaCard = toLower(getGlVendor()).find("nvidia") != string::npos; + bool atiCard = toLower(getGlVendor()).find("ati") != string::npos; + //bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow") && isGlExtensionSupported("GL_ARB_shadow_ambient"); + bool shadowExtensions = isGlExtensionSupported("GL_ARB_shadow"); + + //3D textures + config.setBool("Textures3D", isGlExtensionSupported("GL_EXT_texture3D")); + + //shadows + string shadows = ""; + if (getGlMaxTextureUnits() >= 3) { + if (nvidiaCard && shadowExtensions) { + shadows = shadowsToStr(sShadowMapping); + } else { + shadows = shadowsToStr(sProjected); + } + } else { + shadows = shadowsToStr(sDisabled); + } + config.setString("Shadows", shadows); + + //lights + config.setInt("MaxLights", atiCard ? 1 : 4); + + //filter + config.setString("Filter", "Bilinear"); + } + } + + void Renderer::clearBuffers() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + void Renderer::clearZBuffer() { + glClear(GL_DEPTH_BUFFER_BIT); + } + + void Renderer::loadConfig() { + Config &config = Config::getInstance(); + + //cache most used config params + maxLights = config.getInt("MaxLights"); + photoMode = config.getBool("PhotoMode"); + focusArrows = config.getBool("FocusArrows"); + textures3D = config.getBool("Textures3D"); + float gammaValue = config.getFloat("GammaValue", "0.0"); + if (this->program == NULL) { + throw megaglest_runtime_error("this->program == NULL"); + } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + //if(this->program != NULL) { + if (gammaValue != 0.0) { + this->program->getWindow()->setGamma(gammaValue); + } + //} + } + //load shadows + shadows = strToShadows(config.getString("Shadows")); + if (shadows == sProjected || shadows == sShadowMapping) { + shadowTextureSize = config.getInt("ShadowTextureSize"); + shadowFrameSkip = config.getInt("ShadowFrameSkip"); + shadowIntensity = config.getFloat("ShadowIntensity", "1.0"); + } + + //load filter settings + Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); + int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + for (int i = 0; i < rsCount; ++i) { + textureManager[i]->setFilter(textureFilter); + textureManager[i]->setMaxAnisotropy(maxAnisotropy); + } + } + } + + //Texture2D *Renderer::saveScreenToTexture(int x, int y, int width, int height) { + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // Config &config= Config::getInstance(); + // Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); + // int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); + // + // Texture2D *texture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); + // texture->setForceCompressionDisabled(true); + // texture->setMipmap(false); + // Pixmap2D *pixmapScreenShot = texture->getPixmap(); + // pixmapScreenShot->init(width, height, 3); + // texture->init(textureFilter,maxAnisotropy); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // //glFinish(); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // glReadPixels(x, y, pixmapScreenShot->getW(), pixmapScreenShot->getH(), + // GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // return texture; + //} + + void Renderer::saveScreen(const string &path, int w, int h) { + const Metrics &sm = Metrics::getInstance(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + Pixmap2D *pixmapScreenShot = new Pixmap2D(sm.getScreenW(), sm.getScreenH(), 3); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + //glFinish(); + + glPixelStorei(GL_PACK_ALIGNMENT, 1); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + glReadPixels(0, 0, pixmapScreenShot->getW(), pixmapScreenShot->getH(), + GL_RGB, GL_UNSIGNED_BYTE, pixmapScreenShot->getPixels()); + + if (w == 0 || h == 0) { + h = sm.getScreenH(); + w = sm.getScreenW(); + } else { + pixmapScreenShot->Scale(GL_RGB, w, h); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + + // Signal the threads queue to add a screenshot save request + MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor, string(extractFileFromDirectoryPath(__FILE__).c_str()) + "_" + intToStr(__LINE__)); + saveScreenQueue.push_back(make_pair(path, pixmapScreenShot)); + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + unsigned int Renderer::getSaveScreenQueueSize() { + MutexSafeWrapper safeMutex(saveScreenShotThreadAccessor, string(extractFileFromDirectoryPath(__FILE__).c_str()) + "_" + intToStr(__LINE__)); + int queueSize = (int) saveScreenQueue.size(); + safeMutex.ReleaseLock(); + + return queueSize; + } + + // ==================== PRIVATE ==================== + + float Renderer::computeSunAngle(float time) { + + float dayTime = TimeFlow::dusk - TimeFlow::dawn; + float fTime = (time - TimeFlow::dawn) / dayTime; + return clamp(fTime*pi, pi / 8.f, 7.f*pi / 8.f); + } + + float Renderer::computeMoonAngle(float time) { + float nightTime = 24 - (TimeFlow::dusk - TimeFlow::dawn); + + if (time < TimeFlow::dawn) { + time += 24.f; + } + + float fTime = (time - TimeFlow::dusk) / nightTime; + return clamp((1.0f - fTime)*pi, pi / 8.f, 7.f*pi / 8.f); + } + + Vec4f Renderer::computeSunPos(float time) { + float ang = computeSunAngle(time); + return Vec4f(-std::cos(ang)*sunDist, std::sin(ang)*sunDist, 0.f, 0.f); + } + + Vec4f Renderer::computeMoonPos(float time) { + float ang = computeMoonAngle(time); + return Vec4f(-std::cos(ang)*moonDist, std::sin(ang)*moonDist, 0.f, 0.f); + } + + // ==================== fast render ==================== + + //render units for selection purposes + vector Renderer::renderUnitsFast(bool renderingShadows, bool colorPickingSelection) { + vector unitsList; + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return unitsList; + } + + assert(game != NULL); + //const World *world= game->getWorld(); + //assert(world != NULL); + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleQuadUnitList.empty() == false) { + if (colorPickingSelection == true) { + unitsList.reserve(qCache.visibleQuadUnitList.size()); + } + + bool modelRenderStarted = false; + bool renderOnlyBuildings = true; + for (int k = 0; k < 2; k++) { + if (k == 0) { + //glDisable(GL_DEPTH_TEST); + renderOnlyBuildings = true; + } else { + if (colorPickingSelection == true) { + // clear depth buffer to get units behind buildings rendered in front of them + glClear(GL_DEPTH_BUFFER_BIT); + } + //glEnable(GL_DEPTH_TEST); + renderOnlyBuildings = false; + } + for (int visibleUnitIndex = 0; + visibleUnitIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleUnitIndex) { + Unit *unit = qCache.visibleQuadUnitList[visibleUnitIndex]; + + if (renderingShadows == false && unit->isAlive() == false) { + // no need to render dead units for selection + continue; + } + + if (renderOnlyBuildings == true && unit->getType()->hasSkillClass(scMove)) { + continue; + } + + if (renderOnlyBuildings == false && !unit->getType()->hasSkillClass(scMove)) { + continue; + } + + if (modelRenderStarted == false) { + modelRenderStarted = true; + + if (colorPickingSelection == false) { + //glPushAttrib(GL_ENABLE_BIT| GL_TEXTURE_BIT); + glDisable(GL_LIGHTING); + if (renderingShadows == false) { + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_TEXTURE_2D); + } else { + glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT); + glEnable(GL_TEXTURE_2D); + glAlphaFunc(GL_GREATER, 0.4f); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + //set color to the texture alpha + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + + //set alpha to the texture alpha + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + } + } + + modelRenderer->begin(false, renderingShadows, false, colorPickingSelection); + + if (colorPickingSelection == false) { + glInitNames(); + } + } + + if (colorPickingSelection == false) { + glPushName(visibleUnitIndex); + } + + //assertGl(); + + glMatrixMode(GL_MODELVIEW); + //debuxar modelo + glPushMatrix(); + + //translate + Vec3f currVec = unit->getCurrVectorFlat(); + glTranslatef(currVec.x, currVec.y, currVec.z); + + //rotate + glRotatef(unit->getRotation(), 0.f, 1.f, 0.f); + + //render + Model *model = unit->getCurrentModelPtr(); + //if(this->gameCamera->getPos().dist(unit->getCurrVector()) <= SKIP_INTERPOLATION_DISTANCE) { + + // ***MV don't think this is needed below 2013/01/11 + model->updateInterpolationVertices(unit->getAnimProgressAsFloat(), unit->isAlive() && !unit->isAnimProgressBound()); + + //} + + if (colorPickingSelection == true) { + unit->setUniquePickingColor(); + unitsList.push_back(unit); + } + + modelRenderer->render(model, rmSelection); + + glPopMatrix(); + + if (colorPickingSelection == false) { + glPopName(); + } + } + } + + if (modelRenderStarted == true) { + modelRenderer->end(); + if (colorPickingSelection == false) { + glPopAttrib(); + } + } + } + //glDisable(GL_DEPTH_TEST); + return unitsList; + } + + //render objects for selection purposes + vector Renderer::renderObjectsFast(bool renderingShadows, bool resourceOnly, + bool colorPickingSelection) { + vector objectList; + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return objectList; + } + + VisibleQuadContainerCache &qCache = getQuadCache(); + if (qCache.visibleObjectList.empty() == false) { + if (colorPickingSelection == true) { + objectList.reserve(qCache.visibleObjectList.size()); + } + + bool modelRenderStarted = false; + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleObjectList.size(); ++visibleIndex) { + Object *o = qCache.visibleObjectList[visibleIndex]; + + if (modelRenderStarted == false) { + modelRenderStarted = true; + + if (colorPickingSelection == false) { + glDisable(GL_LIGHTING); + + if (renderingShadows == false) { + glPushAttrib(GL_ENABLE_BIT); + glDisable(GL_TEXTURE_2D); + } else { + glPushAttrib(GL_ENABLE_BIT | GL_TEXTURE_BIT); + glAlphaFunc(GL_GREATER, 0.5f); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); + + //set color to the texture alpha + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PRIMARY_COLOR); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB, GL_SRC_COLOR); + + //set alpha to the texture alpha + glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_REPLACE); + glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE); + glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA, GL_SRC_ALPHA); + } + } + + modelRenderer->begin(false, renderingShadows, false, colorPickingSelection); + + if (colorPickingSelection == false) { + glInitNames(); + } + } + + if (resourceOnly == false || o->getResource() != NULL) { + Model *objModel = o->getModelPtr(); + //if(this->gameCamera->getPos().dist(o->getPos()) <= SKIP_INTERPOLATION_DISTANCE) { + + // ***MV don't think this is needed below 2013/01/11 + //objModel->updateInterpolationData(o->getAnimProgress(), true); + + //} + const Vec3f v = o->getConstPos(); + + if (colorPickingSelection == false) { + glPushName(OBJECT_SELECT_OFFSET + visibleIndex); + } + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glTranslatef(v.x, v.y, v.z); + glRotatef(o->getRotation(), 0.f, 1.f, 0.f); + + if (colorPickingSelection == true) { + o->setUniquePickingColor(); + objectList.push_back(o); + } + + modelRenderer->render(objModel, resourceOnly ? rmSelection : rmNormal); + + glPopMatrix(); + + if (colorPickingSelection == false) { + glPopName(); + } + } + } + + if (modelRenderStarted == true) { + modelRenderer->end(); + + if (colorPickingSelection == false) { + glPopAttrib(); + } + } + } + + return objectList; + } + + // ==================== gl caps ==================== + + void Renderer::checkGlCaps() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (glActiveTexture == NULL) { + string message; + + message += "Your system supports OpenGL version ["; + message += getGlVersion() + string("]\n"); + message += "ZetaGlest needs a version that supports\n"; + message += "glActiveTexture (OpenGL 1.3) or the ARB_multitexture extension."; + + throw megaglest_runtime_error(message.c_str(), true); + } + + //opengl 2.1 + if (glewIsSupported("GL_VERSION_2_1") == false) { + string message; + + message += "Your system supports OpenGL version ["; + message += getGlVersion() + string("]\n"); + message += "ZetaGlest needs at least version 2.1 to work\n"; + message += "You may solve this problem by installing your latest video card drivers"; + + throw megaglest_runtime_error(message.c_str(), true); + } + + //opengl 1.4 or extension + //if(!isGlVersionSupported(1, 4, 0)){ + if (glewIsSupported("GL_VERSION_1_4") == false) { + checkExtension("GL_ARB_texture_env_crossbar", "ZetaGlest"); + } + } + + void Renderer::checkGlOptionalCaps() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + //shadows + if (shadows == sProjected || shadows == sShadowMapping) { + if (getGlMaxTextureUnits() < 3) { + throw megaglest_runtime_error("Your system doesn't support 3 texture units, required for shadows"); + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + //shadow mapping + if (shadows == sShadowMapping) { + checkExtension("GL_ARB_shadow", "Shadow Mapping"); + //checkExtension("GL_ARB_shadow_ambient", "Shadow Mapping"); + //checkExtension("GL_ARB_depth_texture", "Shadow Mapping"); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void Renderer::checkExtension(const string &extension, const string &msg) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + if (!isGlExtensionSupported(extension.c_str())) { + string str = "OpenGL extension not supported: " + extension + ", required for " + msg; + throw megaglest_runtime_error(str); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + // ==================== init 3d lists ==================== + + void Renderer::init3dList() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + render3dSetup(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //const Metrics &metrics= Metrics::getInstance(); + + //assertGl(); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //list3d= glGenLists(1); + //assertGl(); + //list3dValid=true; + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //glNewList(list3d, GL_COMPILE_AND_EXECUTE); + //need to execute, because if not gluPerspective takes no effect and gluLoadMatrix is wrong + //render3dSetup(); + //glEndList(); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //assert + //assertGl(); + } + + void Renderer::render3dSetup() { + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + const Metrics &metrics = Metrics::getInstance(); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //misc + glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); + glClearColor(fowColor.x, fowColor.y, fowColor.z, fowColor.w); + glFrontFace(GL_CW); + glEnable(GL_CULL_FACE); + loadProjectionMatrix(); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //texture state + glActiveTexture(shadowTexUnit); + glDisable(GL_TEXTURE_2D); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + glActiveTexture(fowTexUnit); + glDisable(GL_TEXTURE_2D); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + glActiveTexture(baseTexUnit); + glEnable(GL_TEXTURE_2D); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //material state + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); + glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); + glColor4fv(defColor.ptr()); + + //blend state + glDisable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //alpha test state + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.f); + + //depth test state + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(GL_LESS); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //lighting state + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + + //matrix mode + glMatrixMode(GL_MODELVIEW); + + //stencil test + glDisable(GL_STENCIL_TEST); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //fog + const Tileset *tileset = NULL; + if (game != NULL && game->getWorld() != NULL) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + tileset = game->getWorld()->getTileset(); + } + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if (tileset != NULL && tileset->getFog()) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + glEnable(GL_FOG); + if (tileset->getFogMode() == fmExp) { + glFogi(GL_FOG_MODE, GL_EXP); + } else { + glFogi(GL_FOG_MODE, GL_EXP2); + } + + glFogf(GL_FOG_DENSITY, tileset->getFogDensity()); + glFogfv(GL_FOG_COLOR, tileset->getFogColor().ptr()); + } + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } + + void Renderer::init2dList() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + // //this list sets the state for the 2d rendering + // list2d= glGenLists(1); + // assertGl(); + // list2dValid=true; + // + // glNewList(list2d, GL_COMPILE); + // render2dMenuSetup(); + // glEndList(); + // + // assertGl(); + } + + void Renderer::render2dMenuSetup() { + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + const Metrics &metrics = Metrics::getInstance(); + //projection + glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, metrics.getVirtualW(), 0, metrics.getVirtualH(), 0, 1); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //modelview + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //disable everything + glDisable(GL_BLEND); + glDisable(GL_LIGHTING); + glDisable(GL_ALPHA_TEST); + glDisable(GL_DEPTH_TEST); + glDisable(GL_STENCIL_TEST); + glDisable(GL_FOG); + glDisable(GL_CULL_FACE); + glFrontFace(GL_CCW); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if (glActiveTexture != NULL) { + glActiveTexture(baseTexUnit); + } else { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s] Line: %d\nglActiveTexture == NULL\nglActiveTexture is only supported if the GL version is 1.3 or greater,\nor if the ARB_multitexture extension is supported!", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + throw megaglest_runtime_error(szBuf); + } + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + glDisable(GL_TEXTURE_2D); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //blend func + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + //color + glColor4f(1.f, 1.f, 1.f, 1.f); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } + + //void Renderer::init3dListMenu(const MainMenu *mm) { + // if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + // return; + // } + // + // //this->mm3d = mm; + // //printf("In [%s::%s Line: %d] this->custom_mm3d [%p] this->mm3d [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->custom_mm3d,this->mm3d); + // + ///* + // assertGl(); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // const Metrics &metrics= Metrics::getInstance(); + // //const MenuBackground *mb= mm->getConstMenuBackground(); + // const MenuBackground *mb = NULL; + // if(mm != NULL) { + // mb = mm->getConstMenuBackground(); + // } + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // if(this->customlist3dMenu != NULL) { + // *this->customlist3dMenu = glGenLists(1); + // assertGl(); + // } + // else { + // list3dMenu= glGenLists(1); + // assertGl(); + // list3dMenuValid=true; + // } + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // if(this->customlist3dMenu != NULL) { + // glNewList(*this->customlist3dMenu, GL_COMPILE); + // } + // else { + // glNewList(list3dMenu, GL_COMPILE); + // } + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // //misc + // glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); + // glClearColor(0.4f, 0.4f, 0.4f, 1.f); + // glFrontFace(GL_CW); + // glEnable(GL_CULL_FACE); + // glMatrixMode(GL_PROJECTION); + // glLoadIdentity(); + // gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, 1000000); + // + // //texture state + // glEnable(GL_TEXTURE_2D); + // glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + // + // //material state + // glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); + // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); + // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); + // glColor4fv(defColor.ptr()); + // glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); + // + // //blend state + // glDisable(GL_BLEND); + // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // + // //alpha test state + // glEnable(GL_ALPHA_TEST); + // glAlphaFunc(GL_GREATER, 0.f); + // + // //depth test state + // glEnable(GL_DEPTH_TEST); + // glDepthMask(GL_TRUE); + // glDepthFunc(GL_LESS); + // + // //lighting state + // glEnable(GL_LIGHTING); + // + // //matrix mode + // glMatrixMode(GL_MODELVIEW); + // + // //stencil test + // glDisable(GL_STENCIL_TEST); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // //fog + // if(mb != NULL && mb->getFog()){ + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // glEnable(GL_FOG); + // glFogi(GL_FOG_MODE, GL_EXP2); + // glFogf(GL_FOG_DENSITY, mb->getFogDensity()); + // } + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // glEndList(); + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // + // //assert + // assertGl(); + //*/ + //} + + void Renderer::render3dMenuSetup(const MainMenu *mm) { + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + const Metrics &metrics = Metrics::getInstance(); + const MenuBackground *mb = NULL; + if (mm != NULL) { + mb = mm->getConstMenuBackground(); + } + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //misc + glViewport(0, 0, metrics.getScreenW(), metrics.getScreenH()); + glClearColor(0.4f, 0.4f, 0.4f, 1.f); + glFrontFace(GL_CW); + glEnable(GL_CULL_FACE); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, 1000000); + + //texture state + glEnable(GL_TEXTURE_2D); + glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); + + //material state + glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, defSpecularColor.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, defAmbientColor.ptr()); + glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, defDiffuseColor.ptr()); + glColor4fv(defColor.ptr()); + glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); + + //blend state + glDisable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + //alpha test state + glEnable(GL_ALPHA_TEST); + glAlphaFunc(GL_GREATER, 0.f); + + //depth test state + glEnable(GL_DEPTH_TEST); + glDepthMask(GL_TRUE); + glDepthFunc(GL_LESS); + + //lighting state + glEnable(GL_LIGHTING); + + //matrix mode + glMatrixMode(GL_MODELVIEW); + + //stencil test + glDisable(GL_STENCIL_TEST); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //fog + if (mb != NULL && mb->getFog()) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + glEnable(GL_FOG); + glFogi(GL_FOG_MODE, GL_EXP2); + glFogf(GL_FOG_DENSITY, mb->getFogDensity()); + } + + //assert + assertGl(); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } + // ==================== misc ==================== + + void Renderer::loadProjectionMatrix() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + GLdouble clipping; + const Metrics &metrics = Metrics::getInstance(); + + assertGl(); + + clipping = photoMode ? perspFarPlane * 100 : perspFarPlane; + + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(perspFov, metrics.getAspectRatio(), perspNearPlane, clipping); + + assertGl(); + } + + void Renderer::enableProjectiveTexturing() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + glTexGenfv(GL_S, GL_EYE_PLANE, &shadowMapMatrix[0]); + glTexGenfv(GL_T, GL_EYE_PLANE, &shadowMapMatrix[4]); + glTexGenfv(GL_R, GL_EYE_PLANE, &shadowMapMatrix[8]); + glTexGenfv(GL_Q, GL_EYE_PLANE, &shadowMapMatrix[12]); + glEnable(GL_TEXTURE_GEN_S); + glEnable(GL_TEXTURE_GEN_T); + glEnable(GL_TEXTURE_GEN_R); + glEnable(GL_TEXTURE_GEN_Q); + } + + // ==================== private aux drawing ==================== + void Renderer::renderHealthBar(Vec3f v, Unit *unit, float height, bool lineBorder, const Texture2D *texture, const Texture2D *backgroundTexture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + int numberOfBars = 1; + int barCount = 0; + float hp = unit->getHpRatio(); + float ep = -1.f; + if (unit->getType()->getTotalMaxEp(unit->getTotalUpgrade()) != 0) { + ep = unit->getEpRatio(); + numberOfBars++; + } + int productionPercent = unit->getProductionPercent(); + if (productionPercent != -1) { + numberOfBars++; + } + int size = unit->getType()->getSize(); + + + Vec3f rightVector; + Vec3f upVector; + Vec3f rightVectorTexture; + Vec3f upVectorTexture; + v.y += 1; + float modelview[16]; + float width = (float) size / 6 + 0.25f; + float red; + float green; + float brightness = 0.8f; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + // get the current modelview state + glGetFloatv(GL_MODELVIEW_MATRIX, modelview); + rightVector = Vec3f(modelview[0], modelview[4], modelview[8]); + upVector = Vec3f(modelview[1], modelview[5], modelview[9]); + rightVectorTexture = rightVector * 2; + upVectorTexture = upVector * 4; + + //from green to yellow to red + + if (hp >= 0.75f) { + green = 1; + red = 1 - ((2 * hp - 1) - 0.5f); + } else { + red = 1; + green = 0.5f + (2 * hp - 1); + } + + if (red > 1.0f) red = 1.0f; + if (green > 1.0f) green = 1.0f; + float yOffset = (float) numberOfBars / 2.f; + + if (backgroundTexture != NULL) { + //backgroundTexture + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(backgroundTexture)->getHandle()); + glColor4f(1.f, 1.f, 1.f, 1.f); + //glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); + glBegin(GL_QUADS); + glTexCoord2i(0, 1); + glVertex3fv((v - (rightVectorTexture*width - upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(0, 0); + glVertex3fv((v - (rightVectorTexture*width + upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(1, 0); + glVertex3fv((v + (rightVectorTexture*width - upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(1, 1); + glVertex3fv((v + (rightVectorTexture*width + upVectorTexture * height*yOffset)).ptr()); + glEnd(); + glDisable(GL_TEXTURE_2D); + } + + //healthbar + glColor4f(red*brightness, green*brightness, 0.0f, 0.4f); + //hpbar + barCount++; + internalRenderHp(numberOfBars, barCount, hp, v, width, height, rightVector, upVector); + + + if (ep > -1.0f) { + //epbar + barCount++; + //glColor4f(brightness,0,brightness,0.5f); + glColor4f(.15f*brightness, 0.3f*brightness, 0.8f*brightness, 0.7f); + internalRenderHp(numberOfBars, barCount, ep, v, width, height, rightVector, upVector); + } + if (productionPercent != -1) { + barCount++; + glColor4f(brightness, 0, brightness, 0.6f); + //glColor4f(0.0f*brightness,0.4f*brightness,0.2f*brightness,0.8f); + internalRenderHp(numberOfBars, barCount, (float) productionPercent / 100, v, width, height, rightVector, upVector); + } + + + // glBegin(GL_QUADS); + // if(ep < -2.0f) { + // //hpbar + // glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); + // glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); + // glVertex3fv((v + (rightVector*hp*width - upVector*height)).ptr()); + // glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); + // + // } else { + // //hpbar + // glVertex3fv((v - (rightVector*width - upVector*height)).ptr()); + // glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); + // glVertex3fv((v + (rightVector*hp*width - upVector*height*0.0f)).ptr()); + // glVertex3fv((v + (rightVector*hp*width + upVector*height)).ptr()); + // //epbar + // glColor4f(brightness,0,brightness,0.4f); + // glVertex3fv((v - (rightVector*width + upVector*height*0.0f)).ptr()); + // glVertex3fv((v - (rightVector*width + upVector*height)).ptr()); + // glVertex3fv((v + (rightVector*ep*width - upVector*height)).ptr()); + // glVertex3fv((v + (rightVector*ep*width - upVector*height*0.0f)).ptr()); + // } + // glEnd(); + + if (lineBorder) { + //border + glColor4f(red*brightness, green*brightness, 0.1f*brightness, 0.5f); + glBegin(GL_LINE_LOOP); + glVertex3fv((v - (rightVector*width - upVector * height*yOffset)).ptr()); + glVertex3fv((v - (rightVector*width + upVector * height*yOffset)).ptr()); + glVertex3fv((v + (rightVector*width - upVector * height*yOffset)).ptr()); + glVertex3fv((v + (rightVector*width + upVector * height*yOffset)).ptr()); + glEnd(); + } + + if (texture != NULL) { + //BorderTexture + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); + glColor4f(1.f, 1.f, 1.f, 1.f); + //glColor4f(red+0.1f,green+0.1f,0.1f,0.5f); + glBegin(GL_QUADS); + glTexCoord2i(0, 1); + glVertex3fv((v - (rightVectorTexture*width - upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(0, 0); + glVertex3fv((v - (rightVectorTexture*width + upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(1, 0); + glVertex3fv((v + (rightVectorTexture*width - upVectorTexture * height*yOffset)).ptr()); + glTexCoord2i(1, 1); + glVertex3fv((v + (rightVectorTexture*width + upVectorTexture * height*yOffset)).ptr()); + glEnd(); + glDisable(GL_TEXTURE_2D); + } + + glPopMatrix(); + } + + void Renderer::internalRenderHp(int numberOfBars, int barNumber, float hp, + Vec3f posVector, float width, float singleHPheight, Vec3f rightVector, Vec3f upVector) { + + float yOffset = (float) numberOfBars*singleHPheight / 2; + float offsetTop = yOffset - singleHPheight * (barNumber - 1); + float offsetBottom = yOffset - singleHPheight * barNumber; + offsetBottom = offsetBottom * -1; + hp = hp * 2 - 1; + + glBegin(GL_QUADS); + glVertex3fv((posVector - (rightVector*width - upVector * offsetTop)).ptr()); + glVertex3fv((posVector - (rightVector*width + upVector * offsetBottom)).ptr()); + glVertex3fv((posVector + (rightVector*hp*width - upVector * offsetBottom)).ptr()); + glVertex3fv((posVector + (rightVector*hp*width + upVector * offsetTop)).ptr()); + glEnd(); + } + + void Renderer::renderSelectionCircle(Vec3f v, int size, float radius, float thickness) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + GLUquadricObj *disc; + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + + glTranslatef(v.x, v.y, v.z); + glRotatef(90.f, 1.f, 0.f, 0.f); + disc = gluNewQuadric(); + gluQuadricDrawStyle(disc, GLU_FILL); + gluCylinder(disc, radius*(size - thickness), radius*size, thickness, 30, 1); + gluDeleteQuadric(disc); + + glPopMatrix(); + // glBegin (GL_QUAD_STRIP); + // for (float k = 0; k <= 180; k=k+1) { + // float j=degToRad(k); + // glVertex3f(v.x+std::cos(j)*.9*radius*size, v.y+thickness, v.z+std::sin(j)*.9*radius*size); + // glVertex3f(v.x+std::cos(j)*radius*size, v.y, v.z+std::sin(j)*radius*size); + // } + // glEnd(); + } + + void Renderer::renderArrow(const Vec3f &pos1, const Vec3f &pos2, + const Vec3f &color, float width) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + Config &config = Config::getInstance(); + if (config.getBool("RecordMode", "false") == true) { + return; + } + + const int tesselation = 3; + const float arrowEndSize = 0.4f; + const float maxlen = 25; + const float blendDelay = 5.f; + + Vec3f dir = Vec3f(pos2 - pos1); + float len = dir.length(); + + if (len > maxlen) { + return; + } + float alphaFactor = clamp((maxlen - len) / blendDelay, 0.f, 1.f); + + dir.normalize(); + Vec3f normal = dir.cross(Vec3f(0, 1, 0)); + + Vec3f pos2Left = pos2 + normal * (width - 0.05f) - dir * arrowEndSize*width; + Vec3f pos2Right = pos2 - normal * (width - 0.05f) - dir * arrowEndSize*width; + Vec3f pos1Left = pos1 + normal * (width + 0.05f); + Vec3f pos1Right = pos1 - normal * (width + 0.05f); + + //arrow body + glBegin(GL_TRIANGLE_STRIP); + for (int i = 0; i <= tesselation; ++i) { + float t = static_cast(i) / tesselation; + Vec3f a = pos1Left.lerp(t, pos2Left); + Vec3f b = pos1Right.lerp(t, pos2Right); + Vec4f c = Vec4f(color, t*0.25f*alphaFactor); + + glColor4fv(c.ptr()); + + glVertex3fv(a.ptr()); + glVertex3fv(b.ptr()); + + } + + glEnd(); + //arrow end + glBegin(GL_TRIANGLES); + glVertex3fv((pos2Left + normal * (arrowEndSize - 0.1f)).ptr()); + glVertex3fv((pos2Right - normal * (arrowEndSize - 0.1f)).ptr()); + glVertex3fv((pos2 + dir * (arrowEndSize - 0.1f)).ptr()); + glEnd(); + } + + void Renderer::renderProgressBar3D(int size, int x, int y, Font3D *font, int customWidth, + string prefixLabel, bool centeredText, int customHeight) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + // Makiong this smaller than 14 is a bad idea (since the font size is never smaller than that) + int progressbarHeight = (customHeight > 0 ? customHeight : 14); + int currentSize = size; + int maxSize = maxProgressBar; + string renderText = intToStr(static_cast(size)) + "%"; + if (customWidth > 0) { + if (size > 0) { + currentSize = (int) ((double) customWidth * ((double) size / 100.0)); + } + maxSize = customWidth; + //if(maxSize <= 0) { + // maxSize = maxProgressBar; + //} + } + if (prefixLabel != "") { + renderText = prefixLabel + renderText; + } + + //bar + glBegin(GL_QUADS); + glColor4fv(progressBarFront2.ptr()); + glVertex2i(x, y); + glVertex2i(x, y + progressbarHeight); + glColor4fv(progressBarFront1.ptr()); + glVertex2i(x + currentSize, y + progressbarHeight); + glVertex2i(x + currentSize, y); + glEnd(); + + //transp bar + glEnable(GL_BLEND); + glBegin(GL_QUADS); + glColor4fv(progressBarBack2.ptr()); + glVertex2i(x + currentSize, y); + glVertex2i(x + currentSize, y + progressbarHeight); + glColor4fv(progressBarBack1.ptr()); + glVertex2i(x + maxSize, y + progressbarHeight); + glVertex2i(x + maxSize, y); + glEnd(); + glDisable(GL_BLEND); + + //text + //glColor3fv(defColor.ptr()); + //printf("Render progress bar3d renderText [%s] y = %d, centeredText = %d\n",renderText.c_str(),y, centeredText); + + renderTextBoundingBox3D(renderText, font, defColor, x, y, maxSize, + progressbarHeight, true, true, false, -1, -1); + } + + void Renderer::renderProgressBar(int size, int x, int y, Font2D *font, int customWidth, + string prefixLabel, bool centeredText) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + int currentSize = size; + int maxSize = maxProgressBar; + string renderText = intToStr(static_cast(size)) + "%"; + if (customWidth > 0) { + if (size > 0) { + currentSize = (int) ((double) customWidth * ((double) size / 100.0)); + } + maxSize = customWidth; + //if(maxSize <= 0) { + // maxSize = maxProgressBar; + //} + } + if (prefixLabel != "") { + renderText = prefixLabel + renderText; + } + + //bar + glBegin(GL_QUADS); + glColor4fv(progressBarFront2.ptr()); + glVertex2i(x, y); + glVertex2i(x, y + 10); + glColor4fv(progressBarFront1.ptr()); + glVertex2i(x + currentSize, y + 10); + glVertex2i(x + currentSize, y); + glEnd(); + + //transp bar + glEnable(GL_BLEND); + glBegin(GL_QUADS); + glColor4fv(progressBarBack2.ptr()); + glVertex2i(x + currentSize, y); + glVertex2i(x + currentSize, y + 10); + glColor4fv(progressBarBack1.ptr()); + glVertex2i(x + maxSize, y + 10); + glVertex2i(x + maxSize, y); + glEnd(); + glDisable(GL_BLEND); + + //text + glColor3fv(defColor.ptr()); + + //textRenderer->begin(font); + TextRendererSafeWrapper safeTextRender(textRenderer, font); + if (centeredText == true) { + textRenderer->render(renderText.c_str(), x + maxSize / 2, y, centeredText); + } else { + textRenderer->render(renderText.c_str(), x, y, centeredText); + } + //textRenderer->end(); + safeTextRender.end(); + } + + + //void Renderer::renderTile(const Vec2i &pos) { + // if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + // return; + // } + // + // const Map *map= game->getWorld()->getMap(); + // Vec2i scaledPos= pos * Map::cellScale; + // + // glMatrixMode(GL_MODELVIEW); + // glPushMatrix(); + // glTranslatef(-0.5f, 0.f, -0.5f); + // + // glInitNames(); + // for(int i=0; i < Map::cellScale; ++i) { + // for(int j=0; j < Map::cellScale; ++j) { + // + // Vec2i renderPos= scaledPos + Vec2i(i, j); + // + // glPushName(renderPos.y); + // glPushName(renderPos.x); + // + // glDisable(GL_CULL_FACE); + // + // float h1 = map->getCell(renderPos.x, renderPos.y)->getHeight(); + // float h2 = map->getCell(renderPos.x, renderPos.y+1)->getHeight(); + // float h3 = map->getCell(renderPos.x+1, renderPos.y)->getHeight(); + // float h4 = map->getCell(renderPos.x+1, renderPos.y+1)->getHeight(); + // + // glBegin(GL_TRIANGLE_STRIP); + // glVertex3f( + // static_cast(renderPos.x), + // h1, + // static_cast(renderPos.y)); + // glVertex3f( + // static_cast(renderPos.x), + // h2, + // static_cast(renderPos.y+1)); + // glVertex3f( + // static_cast(renderPos.x+1), + // h3, + // static_cast(renderPos.y)); + // glVertex3f( + // static_cast(renderPos.x+1), + // h4, + // static_cast(renderPos.y+1)); + // glEnd(); + // + // glPopName(); + // glPopName(); + // } + // } + // + // glPopMatrix(); + //} + + void Renderer::renderQuad(int x, int y, int w, int h, const Texture2D *texture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + if (texture == NULL) { + printf("\n**WARNING** detected a null texture to render in renderQuad!\n"); + return; + } + + if (w < 0) { + w = texture->getPixmapConst()->getW(); + } + if (h < 0) { + h = texture->getPixmapConst()->getH(); + } + + glBindTexture(GL_TEXTURE_2D, static_cast(texture)->getHandle()); + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2i(0, 1); + glVertex2i(x, y + h); + glTexCoord2i(0, 0); + glVertex2i(x, y); + glTexCoord2i(1, 1); + glVertex2i(x + w, y + h); + glTexCoord2i(1, 0); + glVertex2i(x + w, y); + glEnd(); + } + + Renderer::Shadows Renderer::strToShadows(const string &s) { + if (s == "Projected") { + return sProjected; + } else if (s == "ShadowMapping") { + return sShadowMapping; + } + return sDisabled; + } + + string Renderer::shadowsToStr(Shadows shadows) { + switch (shadows) { + case sDisabled: + return "Disabled2"; + case sProjected: + return "Projected"; + case sShadowMapping: + return "ShadowMapping"; + default: + assert(false); + return ""; + } + } + + Texture2D::Filter Renderer::strToTextureFilter(const string &s) { + if (s == "Bilinear") { + return Texture2D::fBilinear; + } else if (s == "Trilinear") { + return Texture2D::fTrilinear; + } + + throw megaglest_runtime_error("Error converting from string to FilterType, found: " + s); + } + + void Renderer::setAllowRenderUnitTitles(bool value) { + allowRenderUnitTitles = value; + } + + // This method renders titles for units + void Renderer::renderUnitTitles3D(Font3D *font, Vec3f color) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //std::map unitRenderedList; + + if (visibleFrameUnitList.empty() == false) { + //printf("Render Unit titles ON\n"); + + for (int idx = 0; idx < (int) visibleFrameUnitList.size(); idx++) { + const Unit *unit = visibleFrameUnitList[idx]; + if (unit != NULL) { + if (unit->getVisible() == true) { + if (unit->getCurrentUnitTitle() != "") { + //get the screen coordinates + Vec3f screenPos = unit->getScreenPos(); + renderText3D(unit->getCurrentUnitTitle(), font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + //unitRenderedList[unit->getId()] = true; + } else { + string str = unit->getFullName(unit->showTranslatedTechTree()) + " - " + intToStr(unit->getId()) + " [" + unit->getPosNotThreadSafe().getString() + "]"; + Vec3f screenPos = unit->getScreenPos(); + renderText3D(str, font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + } + } + } + } + visibleFrameUnitList.clear(); + } + + /* + if(renderUnitTitleList.empty() == false) { + for(int idx = 0; idx < renderUnitTitleList.size(); idx++) { + std::pair &unitInfo = renderUnitTitleList[idx]; + Unit *unit = unitInfo.first; + + const World *world= game->getWorld(); + Unit *validUnit = world->findUnitById(unit->getId()); + + if(validUnit != NULL && unitRenderedList.find(validUnit->getId()) == unitRenderedList.end()) { + string str = validUnit->getFullName() + " - " + intToStr(validUnit->getId()); + //get the screen coordinates + Vec3f &screenPos = unitInfo.second; + renderText(str, font, color, fabs(screenPos.x) + 5, fabs(screenPos.y) + 5, false); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] screenPos.x = %f, screenPos.y = %f, screenPos.z = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,screenPos.x,screenPos.y,screenPos.z); + } + } + renderUnitTitleList.clear(); + } + */ + } + + // This method renders titles for units + void Renderer::renderUnitTitles(Font2D *font, Vec3f color) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + //std::map unitRenderedList; + + if (visibleFrameUnitList.empty() == false) { + //printf("Render Unit titles ON\n"); + + for (int idx = 0; idx < (int) visibleFrameUnitList.size(); idx++) { + const Unit *unit = visibleFrameUnitList[idx]; + if (unit != NULL) { + if (unit->getCurrentUnitTitle() != "") { + //get the screen coordinates + Vec3f screenPos = unit->getScreenPos(); + renderText(unit->getCurrentUnitTitle(), font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + + //unitRenderedList[unit->getId()] = true; + } else { + string str = unit->getFullName(unit->showTranslatedTechTree()) + " - " + intToStr(unit->getId()) + " [" + unit->getPosNotThreadSafe().getString() + "]"; + Vec3f screenPos = unit->getScreenPos(); + renderText(str, font, color, std::fabs(screenPos.x) + 5, std::fabs(screenPos.y) + 5, false); + } + } + } + visibleFrameUnitList.clear(); + } + + /* + if(renderUnitTitleList.empty() == false) { + for(int idx = 0; idx < renderUnitTitleList.size(); idx++) { + std::pair &unitInfo = renderUnitTitleList[idx]; + Unit *unit = unitInfo.first; + + const World *world= game->getWorld(); + Unit *validUnit = world->findUnitById(unit->getId()); + + if(validUnit != NULL && unitRenderedList.find(validUnit->getId()) == unitRenderedList.end()) { + string str = validUnit->getFullName() + " - " + intToStr(validUnit->getId()); + //get the screen coordinates + Vec3f &screenPos = unitInfo.second; + renderText(str, font, color, fabs(screenPos.x) + 5, fabs(screenPos.y) + 5, false); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] screenPos.x = %f, screenPos.y = %f, screenPos.z = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,screenPos.x,screenPos.y,screenPos.z); + } + } + renderUnitTitleList.clear(); + } + */ + } + + void Renderer::removeObjectFromQuadCache(const Object *o) { + VisibleQuadContainerCache &qCache = getQuadCache(); + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleObjectList.size(); ++visibleIndex) { + Object *currentObj = qCache.visibleObjectList[visibleIndex]; + if (currentObj == o) { + qCache.visibleObjectList.erase(qCache.visibleObjectList.begin() + visibleIndex); + break; + } + } + } + + void Renderer::removeUnitFromQuadCache(const Unit *unit) { + VisibleQuadContainerCache &qCache = getQuadCache(); + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleQuadUnitList.size(); ++visibleIndex) { + Unit *currentUnit = qCache.visibleQuadUnitList[visibleIndex]; + if (currentUnit == unit) { + qCache.visibleQuadUnitList.erase(qCache.visibleQuadUnitList.begin() + visibleIndex); + break; + } + } + for (int visibleIndex = 0; + visibleIndex < (int) qCache.visibleUnitList.size(); ++visibleIndex) { + Unit *currentUnit = qCache.visibleUnitList[visibleIndex]; + if (currentUnit == unit) { + qCache.visibleUnitList.erase(qCache.visibleUnitList.begin() + visibleIndex); + break; + } + } + } + + VisibleQuadContainerCache & Renderer::getQuadCache(bool updateOnDirtyFrame, + bool forceNew) { + //forceNew = true; + if (game != NULL && game->getWorld() != NULL) { + const World *world = game->getWorld(); + + if (forceNew == true || + (updateOnDirtyFrame == true && + (world->getFrameCount() != quadCache.cacheFrame || + visibleQuad != quadCache.lastVisibleQuad))) { + + // Dump cached info + //if(forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { + //quadCache.clearCacheData(); + //} + //else { + quadCache.clearVolatileCacheData(); + worldToScreenPosCache.clear(); + //} + + // Unit calculations + for (int i = 0; i < world->getFactionCount(); ++i) { + const Faction *faction = world->getFaction(i); + for (int j = 0; j < faction->getUnitCount(); ++j) { + Unit *unit = faction->getUnit(j); + + bool unitCheckedForRender = false; + if (VisibleQuadContainerCache::enableFrustumCalcs == true) { + //bool insideQuad = PointInFrustum(quadCache.frustumData, unit->getCurrVector().x, unit->getCurrVector().y, unit->getCurrVector().z ); + bool insideQuad = CubeInFrustum(quadCache.frustumData, unit->getCurrMidHeightVector().x, unit->getCurrMidHeightVector().y, unit->getCurrMidHeightVector().z, unit->getType()->getRenderSize()); + bool renderInMap = world->toRenderUnit(unit); + if (insideQuad == false || renderInMap == false) { + unit->setVisible(false); + if (renderInMap == true) { + quadCache.visibleUnitList.push_back(unit); + } + unitCheckedForRender = true; // no more need to check any further; + // Currently don't need this list + //quadCache.inVisibleUnitList.push_back(unit); + } + } + if (unitCheckedForRender == false) { + bool insideQuad = visibleQuad.isInside(unit->getPos()); + bool renderInMap = world->toRenderUnit(unit); + if (insideQuad == true && renderInMap == true) { + quadCache.visibleQuadUnitList.push_back(unit); + } else { + unit->setVisible(false); + // Currently don't need this list + //quadCache.inVisibleUnitList.push_back(unit); + } + + if (renderInMap == true) { + quadCache.visibleUnitList.push_back(unit); + } + } + + bool unitBuildPending = unit->isBuildCommandPending(); + if (unitBuildPending == true) { + const UnitBuildInfo &pendingUnit = unit->getBuildCommandPendingInfo(); + const Vec2i &pos = pendingUnit.pos; + const Map *map = world->getMap(); + + bool unitBuildCheckedForRender = false; + + //printf("#1 Unit is about to build another unit\n"); + + if (VisibleQuadContainerCache::enableFrustumCalcs == true) { + Vec3f pos3f = Vec3f(pos.x, map->getCell(pos)->getHeight(), pos.y); + //bool insideQuad = PointInFrustum(quadCache.frustumData, unit->getCurrVector().x, unit->getCurrVector().y, unit->getCurrVector().z ); + bool insideQuad = CubeInFrustum(quadCache.frustumData, pos3f.x, pos3f.y, pos3f.z, pendingUnit.buildUnit->getRenderSize()); + bool renderInMap = world->toRenderUnit(pendingUnit); + if (insideQuad == false || renderInMap == false) { + if (renderInMap == true) { + quadCache.visibleQuadUnitBuildList.push_back(pendingUnit); + } + unitBuildCheckedForRender = true; // no more need to check any further; + // Currently don't need this list + //quadCache.inVisibleUnitList.push_back(unit); + } + + //printf("#2 Unit build added? insideQuad = %d, renderInMap = %d\n",insideQuad,renderInMap); + } + + if (unitBuildCheckedForRender == false) { + bool insideQuad = visibleQuad.isInside(pos); + bool renderInMap = world->toRenderUnit(pendingUnit); + if (insideQuad == true && renderInMap == true) { + quadCache.visibleQuadUnitBuildList.push_back(pendingUnit); + } else { + //unit->setVisible(false); + // Currently don't need this list + //quadCache.inVisibleUnitList.push_back(unit); + } + + //printf("#3 Unit build added? insideQuad = %d, renderInMap = %d\n",insideQuad,renderInMap); + } + + //printf("#4 quadCache.visibleQuadUnitBuildList.size() = %d\n",quadCache.visibleQuadUnitBuildList.size()); + } + } + } + + if (forceNew == true || visibleQuad != quadCache.lastVisibleQuad) { + // Object calculations + const Map *map = world->getMap(); + // clear visibility of old objects + for (int visibleIndex = 0; + visibleIndex < (int) quadCache.visibleObjectList.size(); ++visibleIndex) { + quadCache.visibleObjectList[visibleIndex]->setVisible(false); + } + quadCache.clearNonVolatileCacheData(); + + //int loops1=0; + PosQuadIterator pqi(map, visibleQuad, Map::cellScale); + while (pqi.next()) { + const Vec2i &pos = pqi.getPos(); + if (map->isInside(pos)) { + //loops1++; + const Vec2i &mapPos = Map::toSurfCoords(pos); + + //quadCache.visibleCellList.push_back(mapPos); + + SurfaceCell *sc = map->getSurfaceCell(mapPos); + Object *o = sc->getObject(); + + if (VisibleQuadContainerCache::enableFrustumCalcs == true) { + if (o != NULL) { + //bool insideQuad = PointInFrustum(quadCache.frustumData, o->getPos().x, o->getPos().y, o->getPos().z ); + bool insideQuad = CubeInFrustum(quadCache.frustumData, o->getPos().x, o->getPos().y, o->getPos().z, 1); + if (insideQuad == false) { + o->setVisible(false); + continue; + } + } + } + + bool cellExplored = world->showWorldForPlayer(world->getThisFactionIndex()); + if (cellExplored == false) { + cellExplored = sc->isExplored(world->getThisTeamIndex()); + } + + bool isExplored = (cellExplored == true && o != NULL); + //bool isVisible = (sc->isVisible(world->getThisTeamIndex()) && o != NULL); + bool isVisible = true; + + if (isExplored == true && isVisible == true) { + quadCache.visibleObjectList.push_back(o); + o->setVisible(true); + } + } + } + + //printf("Frame # = %d loops1 = %d\n",world->getFrameCount(),loops1); + + //int loops2=0; + + std::map markedCells = game->getMapMarkedCellList(); + + const Rect2i mapBounds(0, 0, map->getSurfaceW() - 1, map->getSurfaceH() - 1); + Quad2i scaledQuad = visibleQuad / Map::cellScale; + PosQuadIterator pqis(map, scaledQuad); + while (pqis.next()) { + const Vec2i &pos = pqis.getPos(); + if (mapBounds.isInside(pos)) { + //loops2++; + if (VisibleQuadContainerCache::enableFrustumCalcs == false) { + quadCache.visibleScaledCellList.push_back(pos); + + if (markedCells.empty() == false) { + if (markedCells.find(pos) != markedCells.end()) { + //printf("#1 ******** VISIBLE SCALED CELL FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); + //if(markedCells.empty() == false) { + //SurfaceCell *sc = map->getSurfaceCell(pos); + //quadCache.visibleScaledCellToScreenPosList[pos]=computeScreenPosition(sc->getVertex()); + updateMarkedCellScreenPosQuadCache(pos); + } else { + //printf("#1 VISIBLE SCALED CELL NOT FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); + } + } + } else { + //bool insideQuad = false; + + //if( !insideQuad) { + SurfaceCell *sc = map->getSurfaceCell(pos.x, pos.y); + bool insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); + //} + if (!insideQuad) { + SurfaceCell *sc = map->getSurfaceCell(pos.x + 1, pos.y); + insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); + } + if (!insideQuad) { + SurfaceCell *sc = map->getSurfaceCell(pos.x, pos.y + 1); + insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); + } + if (!insideQuad) { + SurfaceCell *sc = map->getSurfaceCell(pos.x + 1, pos.y + 1); + insideQuad = CubeInFrustum(quadCache.frustumData, sc->getVertex().x, sc->getVertex().y, sc->getVertex().z, 0); + } + + if (insideQuad == true) { + quadCache.visibleScaledCellList.push_back(pos); + + if (markedCells.empty() == false) { + if (markedCells.find(pos) != markedCells.end()) { + //printf("#2 ******** VISIBLE SCALED CELL FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); + //if(markedCells.empty() == false) { + //quadCache.visibleScaledCellToScreenPosList[pos]=computeScreenPosition(sc->getVertex()); + updateMarkedCellScreenPosQuadCache(pos); + } else { + //printf("#2 VISIBLE SCALED CELL NOT FOUND in marked list pos [%s] markedCells.size() = " MG_SIZE_T_SPECIFIER "\n",pos.getString().c_str(),markedCells.size()); + } + } + } + } + } + } + //printf("Frame # = %d loops2 = %d\n",world->getFrameCount(),loops2); + } + quadCache.cacheFrame = world->getFrameCount(); + quadCache.lastVisibleQuad = visibleQuad; + } + } + + return quadCache; + } + + void Renderer::updateMarkedCellScreenPosQuadCache(Vec2i pos) { + const World *world = game->getWorld(); + const Map *map = world->getMap(); + + SurfaceCell *sc = map->getSurfaceCell(pos); + quadCache.visibleScaledCellToScreenPosList[pos] = computeScreenPosition(sc->getVertex()); + } + + void Renderer::forceQuadCacheUpdate() { + quadCache.cacheFrame = -1; + + Vec2i clearPos(-1, -1); + quadCache.lastVisibleQuad.p[0] = clearPos; + quadCache.lastVisibleQuad.p[1] = clearPos; + quadCache.lastVisibleQuad.p[2] = clearPos; + quadCache.lastVisibleQuad.p[3] = clearPos; + } + + std::pair Renderer::posInCellQuadCache(Vec2i pos) { + std::pair result = make_pair(false, Vec3f()); + if (std::find( + quadCache.visibleScaledCellList.begin(), + quadCache.visibleScaledCellList.end(), + pos) != quadCache.visibleScaledCellList.end()) { + result.first = true; + result.second = quadCache.visibleScaledCellToScreenPosList[pos]; + } + return result; + } + + //Vec3f Renderer::getMarkedCellScreenPosQuadCache(Vec2i pos) { + // Vec3f result(-1,-1,-1); + // if(std::find( + // quadCache.visibleScaledCellList.begin(), + // quadCache.visibleScaledCellList.end(), + // pos) != quadCache.visibleScaledCellList.end()) { + // result = quadCache.visibleScaledCellToScreenPosList[pos]; + // } + // return result; + //} + + void Renderer::beginRenderToTexture(Texture2D **renderToTexture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); + + if (supportFBOs == true && renderToTexture != NULL) { + Config &config = Config::getInstance(); + Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); + int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); + + const Metrics &metrics = Metrics::getInstance(); + + *renderToTexture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); + Texture2DGl *texture = static_cast(*renderToTexture); + texture->setMipmap(false); + Pixmap2D *pixmapScreenShot = texture->getPixmap(); + pixmapScreenShot->init(metrics.getScreenW(), metrics.getScreenH(), 4); + texture->setForceCompressionDisabled(true); + texture->init(textureFilter, maxAnisotropy); + texture->setup_FBO_RBO(); + + assertGl(); + + if (texture->checkFrameBufferStatus() == false) { + //printf("******************** WARNING CANNOT Attach to FBO!\n"); + texture->end(); + delete texture; + *renderToTexture = NULL; + } + } + } + + void Renderer::endRenderToTexture(Texture2D **renderToTexture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); + + if (supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { + Texture2DGl *texture = static_cast(*renderToTexture); + if (texture != NULL) { + texture->dettachFrameBufferFromTexture(); + } + + assertGl(); + } + } + + void Renderer::renderMapPreview(const MapPreview *map, bool renderAll, + int screenPosX, int screenPosY, + Texture2D **renderToTexture) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + static bool supportFBOs = Texture2DGl().supports_FBO_RBO(); + if (Config::getInstance().getBool("LegacyMapPreviewRendering", "false") == true) { + supportFBOs = false; + } + + //static bool supportFBOs = false; + const Metrics &metrics = Metrics::getInstance(); + + float alt = 0; + float showWater = 0; + int renderMapHeight = 64; + int renderMapWidth = 64; + float cellSize = 2; + float playerCrossSize = 2; + float clientW = renderMapWidth * cellSize; + float clientH = renderMapHeight * cellSize;; + float minDimension = std::min(metrics.getVirtualW(), metrics.getVirtualH()); + + // stretch small maps to 128x128 + if (map->getW() < map->getH()) { + cellSize = cellSize * renderMapHeight / map->getH(); + } else { + cellSize = cellSize * renderMapWidth / map->getW(); + } + + assertGl(); + + if (supportFBOs == true && renderToTexture != NULL) { + Config &config = Config::getInstance(); + Texture2D::Filter textureFilter = strToTextureFilter(config.getString("Filter")); + int maxAnisotropy = config.getInt("FilterMaxAnisotropy"); + + *renderToTexture = GraphicsInterface::getInstance().getFactory()->newTexture2D(); + Texture2DGl *texture = static_cast(*renderToTexture); + texture->setMipmap(false); + Pixmap2D *pixmapScreenShot = texture->getPixmap(); + pixmapScreenShot->init(minDimension, minDimension, 4); + texture->setForceCompressionDisabled(true); + texture->init(textureFilter, maxAnisotropy); + texture->setup_FBO_RBO(); + + assertGl(); + + if (texture->checkFrameBufferStatus() == false) { + //printf("******************** WARNING CANNOT Attach to FBO!\n"); + texture->end(); + delete texture; + *renderToTexture = NULL; + } + } + + if (supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { + cellSize = 1; + clientW = minDimension; + clientH = minDimension; + int mapMaxDimensionSize = std::max(map->getW(), map->getH()); + switch (mapMaxDimensionSize) { + case 8: + cellSize = 96; + break; + case 16: + cellSize = 48; + break; + case 32: + cellSize = 24; + break; + case 64: + cellSize = 12; + break; + case 128: + cellSize = 6; + break; + case 256: + cellSize = 3; + break; + case 512: + cellSize = 2; + break; + } + } + + glFrontFace(GL_CW); + glEnable(GL_CULL_FACE); + + glMatrixMode(GL_PROJECTION); + glPushMatrix(); + glLoadIdentity(); + + assertGl(); + + GLint viewport[4]; // Where The original Viewport Values Will Be Stored + + if (supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { + glGetIntegerv(GL_VIEWPORT, viewport); + glOrtho(0, clientW, 0, clientH, 0, 1); + glViewport(0, 0, clientW, clientH); + } else { + glOrtho(0, metrics.getVirtualW(), 0, metrics.getVirtualH(), 0, 1); + } + + glMatrixMode(GL_MODELVIEW); + + glPushMatrix(); + glLoadIdentity(); + + if (supportFBOs == false || renderToTexture == NULL || *renderToTexture == NULL) { + glTranslatef(static_cast(screenPosX), static_cast(screenPosY) - clientH, 0.0f); + } + + assertGl(); + + glPushAttrib(GL_CURRENT_BIT); + glLineWidth(1); + glColor3f(0, 0, 0); + + for (int j = 0; j < map->getH(); j++) { + for (int i = 0; i < map->getW(); i++) { + //surface + alt = map->getHeight(i, j) / 20.f; + showWater = map->getWaterLevel() / 20.f - alt; + showWater = (showWater > 0) ? showWater : 0; + Vec3f surfColor; + switch (map->getSurface(i, j)) { + case st_Grass: + surfColor = Vec3f(0.0, 0.8f * alt, 0.f + showWater); + break; + case st_Secondary_Grass: + surfColor = Vec3f(0.4f * alt, 0.6f * alt, 0.f + showWater); + break; + case st_Road: + surfColor = Vec3f(0.6f * alt, 0.3f * alt, 0.f + showWater); + break; + case st_Stone: + surfColor = Vec3f(0.7f * alt, 0.7f * alt, 0.7f * alt + showWater); + break; + case st_Ground: + surfColor = Vec3f(0.7f * alt, 0.5f * alt, 0.3f * alt + showWater); + break; + } + + glColor3fv(surfColor.ptr()); + + glBegin(GL_TRIANGLE_STRIP); + glVertex2f(i * cellSize, clientH - j * cellSize - cellSize); + glVertex2f(i * cellSize, clientH - j * cellSize); + glVertex2f(i * cellSize + cellSize, clientH - j * cellSize - cellSize); + glVertex2f(i * cellSize + cellSize, clientH - j * cellSize); + glEnd(); + + //objects + if (renderAll == true) { + switch (map->getObject(i, j)) { + case 0: + glColor3f(0.f, 0.f, 0.f); + break; + case 1: + glColor3f(1.f, 0.f, 0.f); + break; + case 2: + glColor3f(1.f, 1.f, 1.f); + break; + case 3: + glColor3f(0.5f, 0.5f, 1.f); + break; + case 4: + glColor3f(0.f, 0.f, 1.f); + break; + case 5: + glColor3f(0.5f, 0.5f, 0.5f); + break; + case 6: + glColor3f(1.f, 0.8f, 0.5f); + break; + case 7: + glColor3f(0.f, 1.f, 1.f); + break; + case 8: + glColor3f(0.7f, 0.1f, 0.3f); + break; + case 9: + glColor3f(0.5f, 1.f, 0.1f); + break; + case 10: + glColor3f(1.f, 0.2f, 0.8f); + break;// we don't render unvisible blocking objects + } + + if (renderAll && (map->getObject(i, j) != 0) && (map->getObject(i, j) != 10)) { + glPointSize(cellSize / 2.f); + glBegin(GL_POINTS); + glVertex2f(i * cellSize + cellSize / 2.f, clientH - j * cellSize - cellSize / 2.f); + glEnd(); + } + } + + // bool found = false; + + //height lines + // if (!found) { + + //left + if (i > 0 && map->getHeight(i - 1, j) > map->getHeight(i, j)) { + glColor3fv((surfColor*0.5f).ptr()); glBegin(GL_LINES); - glVertex2f(i * cellSize, clientH - j * cellSize - cellSize); - glVertex2f(i * cellSize + cellSize, clientH - j * cellSize); + glVertex2f(i * cellSize, clientH - (j + 1) * cellSize); glVertex2f(i * cellSize, clientH - j * cellSize); - glVertex2f(i * cellSize + cellSize, clientH - j * cellSize - cellSize); glEnd(); } + //down + if (j > 0 && map->getHeight(i, j - 1) > map->getHeight(i, j)) { + glColor3fv((surfColor*0.5f).ptr()); + glBegin(GL_LINES); + glVertex2f(i * cellSize, clientH - j * cellSize); + glVertex2f((i + 1) * cellSize, clientH - j * cellSize); + glEnd(); + } + + + //left + if (i > 0 && map->getHeight(i - 1, j) < map->getHeight(i, j)) { + glColor3fv((surfColor*2.f).ptr()); + glBegin(GL_LINES); + glVertex2f(i * cellSize, clientH - (j + 1) * cellSize); + glVertex2f(i * cellSize, clientH - j * cellSize); + glEnd(); + } + if (j > 0 && map->getHeight(i, j - 1) < map->getHeight(i, j)) { + glColor3fv((surfColor*2.f).ptr()); + glBegin(GL_LINES); + glVertex2f(i * cellSize, clientH - j * cellSize); + glVertex2f((i + 1) * cellSize, clientH - j * cellSize); + glEnd(); + } + // } + + //resources + if (renderAll == true) { + switch (map->getResource(i, j)) { + case 1: glColor3f(1.f, 1.f, 0.f); break; + case 2: glColor3f(0.5f, 0.5f, 0.5f); break; + case 3: glColor3f(1.f, 0.f, 0.f); break; + case 4: glColor3f(0.f, 0.f, 1.f); break; + case 5: glColor3f(0.5f, 0.5f, 1.f); break; + } + + if (renderAll && map->getResource(i, j) != 0) { + glBegin(GL_LINES); + glVertex2f(i * cellSize, clientH - j * cellSize - cellSize); + glVertex2f(i * cellSize + cellSize, clientH - j * cellSize); + glVertex2f(i * cellSize, clientH - j * cellSize); + glVertex2f(i * cellSize + cellSize, clientH - j * cellSize - cellSize); + glEnd(); + } + } } - } - } - - //start locations - glLineWidth(3); - - assertGl(); - - if(supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { - glLineWidth(14); - playerCrossSize = 24; - } - else { - // force playerCrossSize to be at least of size 4 - if(cellSize < 4) { - playerCrossSize = 4; - } - else { - playerCrossSize = cellSize; - } - } - - assertGl(); - - Vec2f *vertices = new Vec2f[map->getMaxFactions() * 4]; - Vec3f *colors = new Vec3f[map->getMaxFactions() * 4]; - - for (int i = 0; i < map->getMaxFactions(); i++) { - Vec3f color; - switch (i) { - case 0: - color = Vec3f(1.f, 0.f, 0.f); - break; - case 1: - color = Vec3f(0.f, 0.f, 1.f); - break; - case 2: - color = Vec3f(0.f, 1.f, 0.f); - break; - case 3: - color = Vec3f(1.f, 1.f, 0.f); - break; - case 4: - color = Vec3f(1.f, 1.f, 1.f); - break; - case 5: - color = Vec3f(0.f, 1.f, 0.8f); - break; - case 6: - color = Vec3f(1.f, 0.5f, 0.f); - break; - case 7: - color = Vec3f(1.f, 0.5f, 1.f); - break; - } - - colors[i*4] = color; - colors[(i*4)+1] = color; - colors[(i*4)+2] = color; - colors[(i*4)+3] = color; - - vertices[i*4] = Vec2f((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); - vertices[(i*4)+1] = Vec2f((map->getStartLocationX(i) + 1) * cellSize + playerCrossSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - playerCrossSize); - vertices[(i*4)+2] = Vec2f((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - playerCrossSize); - vertices[(i*4)+3] = Vec2f((map->getStartLocationX(i) + 1) * cellSize + playerCrossSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); - } - - glEnableClientState(GL_COLOR_ARRAY); - glColorPointer(3, GL_FLOAT, 0, &colors[0]); - glEnableClientState(GL_VERTEX_ARRAY); - glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); - glDrawArrays(GL_LINES, 0, 4 * map->getMaxFactions()); - glDisableClientState(GL_COLOR_ARRAY); - glDisableClientState(GL_VERTEX_ARRAY); - - assertGl(); - - glLineWidth(1); - - glPopMatrix(); - glPopAttrib(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - - if(supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { - Texture2DGl *texture = static_cast(*renderToTexture); - if(texture != NULL) { - texture->dettachFrameBufferFromTexture(); - } - - glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); - - assertGl(); - } - - delete [] vertices; - delete [] colors; - - assertGl(); -} - -// setLastRenderFps and calculate shadowsOffDueToMinRender -void Renderer::setLastRenderFps(int value) { - lastRenderFps = value; - smoothedRenderFps=(MIN_FPS_NORMAL_RENDERING*smoothedRenderFps+lastRenderFps)/(MIN_FPS_NORMAL_RENDERING+1.0f); - - if(smoothedRenderFps>=MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD){ - shadowsOffDueToMinRender=false; - } - if(smoothedRenderFps<=MIN_FPS_NORMAL_RENDERING){ - shadowsOffDueToMinRender=true; - } -} - -std::size_t Renderer::getCurrentPixelByteCount(ResourceScope rs) const { - std::size_t result = 0; - for(int i = (rs == rsCount ? 0 : rs); i < rsCount; ++i) { - if(textureManager[i] != NULL) { - const ::Shared::Graphics::TextureContainer &textures = textureManager[i]->getTextures(); - for(int j = 0; j < (int)textures.size(); ++j) { - const Texture *texture = textures[j]; - result += texture->getPixelByteCount(); - } - if(rs != rsCount) { - break; - } - } - } - - return result; -} - -Texture2D * Renderer::preloadTexture(string logoFilename) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); - - Texture2D *result = NULL; - if(logoFilename != "") { - // Cache faction preview textures - string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); - std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); - - if(crcFactionPreviewTextureCache.find(logoFilename) != crcFactionPreviewTextureCache.end()) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] load texture from cache [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); - - result = crcFactionPreviewTextureCache[logoFilename]; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); - Renderer &renderer= Renderer::getInstance(); - result = renderer.newTexture2D(rsGlobal); - if(result) { - result->setMipmap(true); - result->load(logoFilename); - //renderer.initTexture(rsGlobal,result); } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] add texture to manager and cache [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); + //start locations + glLineWidth(3); - crcFactionPreviewTextureCache[logoFilename] = result; - } - } + assertGl(); - return result; -} - -Texture2D * Renderer::findTexture(string logoFilename) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] logoFilename [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,logoFilename.c_str()); - - Texture2D *result = preloadTexture(logoFilename); - if(result != NULL && result->getInited() == false) { - Renderer &renderer= Renderer::getInstance(); - renderer.initTexture(rsGlobal,result); - } - - return result; -} - -void Renderer::cycleShowDebugUILevel() { - //printf("#1 showDebugUILevel = %d, debugui_fps = %d, debugui_unit_titles = %d\n",showDebugUILevel,debugui_fps,debugui_unit_titles); - - //if(showDebugUI == false) { - // showDebugUI = true; - //} - if((showDebugUILevel & debugui_fps) != debugui_fps || - (showDebugUILevel & debugui_unit_titles) != debugui_unit_titles) { - showDebugUILevel |= debugui_fps; - showDebugUILevel |= debugui_unit_titles; - } - else { - showDebugUILevel = debugui_fps; - } - - //printf("#2 showDebugUILevel = %d, debugui_fps = %d, debugui_unit_titles = %d\n",showDebugUILevel,debugui_fps,debugui_unit_titles); -} - -void Renderer::renderFPSWhenEnabled(int lastFps) { - if(getShowDebugUI() == true) { - CoreData &coreData= CoreData::getInstance(); - if(Renderer::renderText3DEnabled) { - renderText3D( - "FPS: " + intToStr(lastFps), - coreData.getMenuFontNormal3D(), Vec3f(1.f), 10, 10, false); - } - else { - renderText( - "FPS: " + intToStr(lastFps), - coreData.getMenuFontNormal(), Vec3f(1.f), 10, 10, false); - } - } -} - -void Renderer::renderPopupMenu(PopupMenu *menu) { - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - return; - } - - if(menu->getVisible() == false || menu->getEnabled() == false) { - return; - } - - //background - glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); - glEnable(GL_BLEND); - - glColor4f(0.0f, 0.0f, 0.0f, 0.8f) ; - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(menu->getX(), menu->getY() + 9 * menu->getH() / 10); - glVertex2i(menu->getX(), menu->getY()); - glVertex2i(menu->getX() + menu->getW(), menu->getY() + 9 * menu->getH() / 10); - glVertex2i(menu->getX() + menu->getW(), menu->getY()); - glEnd(); - - glColor4f(0.0f, 0.0f, 0.0f, 0.8f) ; - glBegin(GL_TRIANGLE_STRIP); - glVertex2i(menu->getX(), menu->getY() + menu->getH()); - glVertex2i(menu->getX(), menu->getY() + 9 * menu->getH() / 10); - glVertex2i(menu->getX() + menu->getW(), menu->getY() + menu->getH()); - glVertex2i(menu->getX() + menu->getW(), menu->getY() + 9 * menu->getH() / 10); - glEnd(); - - glBegin(GL_LINE_LOOP); - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(menu->getX(), menu->getY()); - - glColor4f(0.0f, 0.0f, 0.0f, 0.25f) ; - glVertex2i(menu->getX() + menu->getW(), menu->getY()); - - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(menu->getX() + menu->getW(), menu->getY() + menu->getH()); - - glColor4f(0.25f, 0.25f, 0.25f, 0.25f) ; - glVertex2i(menu->getX(), menu->getY() + menu->getH()); - glEnd(); - - glBegin(GL_LINE_STRIP); - glColor4f(1.0f, 1.0f, 1.0f, 0.25f) ; - glVertex2i(menu->getX(), menu->getY() + 90*menu->getH()/100); - - glColor4f(0.5f, 0.5f, 0.5f, 0.25f) ; - glVertex2i(menu->getX()+ menu->getW(), menu->getY() + 90*menu->getH()/100); - glEnd(); - - glPopAttrib(); - - Vec4f fontColor; - //if(game!=NULL){ - // fontColor=game->getGui()->getDisplay()->getColor(); - //} - //else { - // white shadowed is default ( in the menu for example ) - fontColor=Vec4f(1.f, 1.f, 1.f, 1.0f); - //} - - if(renderText3DEnabled == true) { - //text - renderTextBoundingBox3D( - menu->getHeader(), menu->getFont3D(),fontColor, - menu->getX(), menu->getY()+93*menu->getH()/100,menu->getW(),0, - true,false, false,-1,-1); - - } - else { - //text - int renderX = (menu->getX() + (menu->getW() / 2)); - //int renderY = (menu->getY() + (menu->getH() / 2)); - FontMetrics *fontMetrics= menu->getFont()->getMetrics(); - int renderY = menu->getY() + menu->getH() - fontMetrics->getHeight(menu->getHeader()); - renderTextShadow( - menu->getHeader(), menu->getFont(),fontColor, - renderX, renderY, - true); - - //renderText(button->getText(), button->getFont(), color,x + (w / 2), y + (h / 2), true); - } - - //buttons -// int maxButtonWidth = -1; - std::vector &menuItems = menu->getMenuItems(); -// for(unsigned int i = 0; i < menuItems.size(); ++i) { -// GraphicButton *button = &menuItems[i]; -// int currentButtonWidth = -1; -// if(renderText3DEnabled == true) { -// FontMetrics *fontMetrics= menu->getFont3D()->getMetrics(); -// currentButtonWidth = fontMetrics->getTextWidth(button->getText()); -// } -// else { -// FontMetrics *fontMetrics= menu->getFont()->getMetrics(); -// currentButtonWidth = fontMetrics->getTextWidth(button->getText()); -// } -// -// if(maxButtonWidth < 0 || currentButtonWidth > maxButtonWidth) { -// maxButtonWidth = currentButtonWidth + 5; -// } -// } - - for(unsigned int i = 0; i < menuItems.size(); ++i) { - GraphicButton *button = &menuItems[i]; - - //button->setW(maxButtonWidth); - renderButton(button); - } - -} - -void Renderer::setupRenderForVideo() { - clearBuffers(); - //3d - reset3dMenu(); - clearZBuffer(); - //2d - reset2d(); - glClearColor(0.f, 0.f, 0.f, 1.f); -} - -void Renderer::renderVideoLoading(int progressPercent) { - //printf("Rendering progress progressPercent = %d\n",progressPercent); - setupRenderForVideo(); - - Lang &lang= Lang::getInstance(); - string textToRender = lang.getString("PleaseWait"); - const Metrics &metrics= Metrics::getInstance(); - - static Chrono cycle(true); - static float anim = 0.0f; - - if(CoreData::getInstance().getMenuFontBig3D() != NULL) { - - int w= metrics.getVirtualW(); - int renderX = (w / 2) - (CoreData::getInstance().getMenuFontBig3D()->getMetrics()->getTextWidth(textToRender) / 2); - int h= metrics.getVirtualH(); - int renderY = (h / 2) + (CoreData::getInstance().getMenuFontBig3D()->getMetrics()->getHeight(textToRender) / 2); - - renderText3D( - textToRender, - CoreData::getInstance().getMenuFontBig3D(), - Vec4f(1.f, 1.f, 0.f,anim), - renderX, renderY, false); - } - else { - renderText( - textToRender, - CoreData::getInstance().getMenuFontBig(), - Vec4f(1.f, 1.f, 0.f,anim), (metrics.getScreenW() / 2), - (metrics.getScreenH() / 2), true); - } - swapBuffers(); - - if(cycle.getCurMillis() % 50 == 0) { - static bool animCycleUp = true; - if(animCycleUp == true) { - anim += 0.1f; - if(anim > 1.f) { - anim= 1.f; - cycle.reset(); - animCycleUp = false; + if (supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { + glLineWidth(14); + playerCrossSize = 24; + } else { + // force playerCrossSize to be at least of size 4 + if (cellSize < 4) { + playerCrossSize = 4; + } else { + playerCrossSize = cellSize; + } } - } - else { - anim -= 0.1f; - if(anim < 0.f) { - anim= 0.f; - cycle.reset(); - animCycleUp = true; - } - } - } -} -}}//end namespace + assertGl(); + + Vec2f *vertices = new Vec2f[map->getMaxFactions() * 4]; + Vec3f *colors = new Vec3f[map->getMaxFactions() * 4]; + + for (int i = 0; i < map->getMaxFactions(); i++) { + Vec3f color; + switch (i) { + case 0: + color = Vec3f(1.f, 0.f, 0.f); + break; + case 1: + color = Vec3f(0.f, 0.f, 1.f); + break; + case 2: + color = Vec3f(0.f, 1.f, 0.f); + break; + case 3: + color = Vec3f(1.f, 1.f, 0.f); + break; + case 4: + color = Vec3f(1.f, 1.f, 1.f); + break; + case 5: + color = Vec3f(0.f, 1.f, 0.8f); + break; + case 6: + color = Vec3f(1.f, 0.5f, 0.f); + break; + case 7: + color = Vec3f(1.f, 0.5f, 1.f); + break; + } + + colors[i * 4] = color; + colors[(i * 4) + 1] = color; + colors[(i * 4) + 2] = color; + colors[(i * 4) + 3] = color; + + vertices[i * 4] = Vec2f((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); + vertices[(i * 4) + 1] = Vec2f((map->getStartLocationX(i) + 1) * cellSize + playerCrossSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - playerCrossSize); + vertices[(i * 4) + 2] = Vec2f((map->getStartLocationX(i) - 1) * cellSize, clientH - (map->getStartLocationY(i) + 1) * cellSize - playerCrossSize); + vertices[(i * 4) + 3] = Vec2f((map->getStartLocationX(i) + 1) * cellSize + playerCrossSize, clientH - (map->getStartLocationY(i) - 1) * cellSize); + } + + glEnableClientState(GL_COLOR_ARRAY); + glColorPointer(3, GL_FLOAT, 0, &colors[0]); + glEnableClientState(GL_VERTEX_ARRAY); + glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); + glDrawArrays(GL_LINES, 0, 4 * map->getMaxFactions()); + glDisableClientState(GL_COLOR_ARRAY); + glDisableClientState(GL_VERTEX_ARRAY); + + assertGl(); + + glLineWidth(1); + + glPopMatrix(); + glPopAttrib(); + glMatrixMode(GL_PROJECTION); + glPopMatrix(); + + if (supportFBOs == true && renderToTexture != NULL && *renderToTexture != NULL) { + Texture2DGl *texture = static_cast(*renderToTexture); + if (texture != NULL) { + texture->dettachFrameBufferFromTexture(); + } + + glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); + + assertGl(); + } + + delete[] vertices; + delete[] colors; + + assertGl(); + } + + // setLastRenderFps and calculate shadowsOffDueToMinRender + void Renderer::setLastRenderFps(int value) { + lastRenderFps = value; + smoothedRenderFps = (MIN_FPS_NORMAL_RENDERING*smoothedRenderFps + lastRenderFps) / (MIN_FPS_NORMAL_RENDERING + 1.0f); + + if (smoothedRenderFps >= MIN_FPS_NORMAL_RENDERING_TOP_THRESHOLD) { + shadowsOffDueToMinRender = false; + } + if (smoothedRenderFps <= MIN_FPS_NORMAL_RENDERING) { + shadowsOffDueToMinRender = true; + } + } + + std::size_t Renderer::getCurrentPixelByteCount(ResourceScope rs) const { + std::size_t result = 0; + for (int i = (rs == rsCount ? 0 : rs); i < rsCount; ++i) { + if (textureManager[i] != NULL) { + const ::Shared::Graphics::TextureContainer &textures = textureManager[i]->getTextures(); + for (int j = 0; j < (int) textures.size(); ++j) { + const Texture *texture = textures[j]; + result += texture->getPixelByteCount(); + } + if (rs != rsCount) { + break; + } + } + } + + return result; + } + + Texture2D * Renderer::preloadTexture(string logoFilename) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] logoFilename [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + + Texture2D *result = NULL; + if (logoFilename != "") { + // Cache faction preview textures + string data_path = getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + std::map &crcFactionPreviewTextureCache = CacheManager::getCachedItem< std::map >(GameConstants::factionPreviewTextureCacheLookupKey); + + if (crcFactionPreviewTextureCache.find(logoFilename) != crcFactionPreviewTextureCache.end()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] logoFilename [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] load texture from cache [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + + result = crcFactionPreviewTextureCache[logoFilename]; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] logoFilename [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + Renderer &renderer = Renderer::getInstance(); + result = renderer.newTexture2D(rsGlobal); + if (result) { + result->setMipmap(true); + result->load(logoFilename); + //renderer.initTexture(rsGlobal,result); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] add texture to manager and cache [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + + crcFactionPreviewTextureCache[logoFilename] = result; + } + } + + return result; + } + + Texture2D * Renderer::findTexture(string logoFilename) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] logoFilename [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, logoFilename.c_str()); + + Texture2D *result = preloadTexture(logoFilename); + if (result != NULL && result->getInited() == false) { + Renderer &renderer = Renderer::getInstance(); + renderer.initTexture(rsGlobal, result); + } + + return result; + } + + void Renderer::cycleShowDebugUILevel() { + //printf("#1 showDebugUILevel = %d, debugui_fps = %d, debugui_unit_titles = %d\n",showDebugUILevel,debugui_fps,debugui_unit_titles); + + //if(showDebugUI == false) { + // showDebugUI = true; + //} + if ((showDebugUILevel & debugui_fps) != debugui_fps || + (showDebugUILevel & debugui_unit_titles) != debugui_unit_titles) { + showDebugUILevel |= debugui_fps; + showDebugUILevel |= debugui_unit_titles; + } else { + showDebugUILevel = debugui_fps; + } + + //printf("#2 showDebugUILevel = %d, debugui_fps = %d, debugui_unit_titles = %d\n",showDebugUILevel,debugui_fps,debugui_unit_titles); + } + + void Renderer::renderFPSWhenEnabled(int lastFps) { + if (getShowDebugUI() == true) { + CoreData &coreData = CoreData::getInstance(); + if (Renderer::renderText3DEnabled) { + renderText3D( + "FPS: " + intToStr(lastFps), + coreData.getMenuFontNormal3D(), Vec3f(1.f), 10, 10, false); + } else { + renderText( + "FPS: " + intToStr(lastFps), + coreData.getMenuFontNormal(), Vec3f(1.f), 10, 10, false); + } + } + } + + void Renderer::renderPopupMenu(PopupMenu *menu) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + if (menu->getVisible() == false || menu->getEnabled() == false) { + return; + } + + //background + glPushAttrib(GL_ENABLE_BIT | GL_CURRENT_BIT); + glEnable(GL_BLEND); + + glColor4f(0.0f, 0.0f, 0.0f, 0.8f); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(menu->getX(), menu->getY() + 9 * menu->getH() / 10); + glVertex2i(menu->getX(), menu->getY()); + glVertex2i(menu->getX() + menu->getW(), menu->getY() + 9 * menu->getH() / 10); + glVertex2i(menu->getX() + menu->getW(), menu->getY()); + glEnd(); + + glColor4f(0.0f, 0.0f, 0.0f, 0.8f); + glBegin(GL_TRIANGLE_STRIP); + glVertex2i(menu->getX(), menu->getY() + menu->getH()); + glVertex2i(menu->getX(), menu->getY() + 9 * menu->getH() / 10); + glVertex2i(menu->getX() + menu->getW(), menu->getY() + menu->getH()); + glVertex2i(menu->getX() + menu->getW(), menu->getY() + 9 * menu->getH() / 10); + glEnd(); + + glBegin(GL_LINE_LOOP); + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(menu->getX(), menu->getY()); + + glColor4f(0.0f, 0.0f, 0.0f, 0.25f); + glVertex2i(menu->getX() + menu->getW(), menu->getY()); + + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(menu->getX() + menu->getW(), menu->getY() + menu->getH()); + + glColor4f(0.25f, 0.25f, 0.25f, 0.25f); + glVertex2i(menu->getX(), menu->getY() + menu->getH()); + glEnd(); + + glBegin(GL_LINE_STRIP); + glColor4f(1.0f, 1.0f, 1.0f, 0.25f); + glVertex2i(menu->getX(), menu->getY() + 90 * menu->getH() / 100); + + glColor4f(0.5f, 0.5f, 0.5f, 0.25f); + glVertex2i(menu->getX() + menu->getW(), menu->getY() + 90 * menu->getH() / 100); + glEnd(); + + glPopAttrib(); + + Vec4f fontColor; + //if(game!=NULL){ + // fontColor=game->getGui()->getDisplay()->getColor(); + //} + //else { + // white shadowed is default ( in the menu for example ) + fontColor = Vec4f(1.f, 1.f, 1.f, 1.0f); + //} + + if (renderText3DEnabled == true) { + //text + renderTextBoundingBox3D( + menu->getHeader(), menu->getFont3D(), fontColor, + menu->getX(), menu->getY() + 93 * menu->getH() / 100, menu->getW(), 0, + true, false, false, -1, -1); + + } else { + //text + int renderX = (menu->getX() + (menu->getW() / 2)); + //int renderY = (menu->getY() + (menu->getH() / 2)); + FontMetrics *fontMetrics = menu->getFont()->getMetrics(); + int renderY = menu->getY() + menu->getH() - fontMetrics->getHeight(menu->getHeader()); + renderTextShadow( + menu->getHeader(), menu->getFont(), fontColor, + renderX, renderY, + true); + + //renderText(button->getText(), button->getFont(), color,x + (w / 2), y + (h / 2), true); + } + + //buttons + // int maxButtonWidth = -1; + std::vector &menuItems = menu->getMenuItems(); + // for(unsigned int i = 0; i < menuItems.size(); ++i) { + // GraphicButton *button = &menuItems[i]; + // int currentButtonWidth = -1; + // if(renderText3DEnabled == true) { + // FontMetrics *fontMetrics= menu->getFont3D()->getMetrics(); + // currentButtonWidth = fontMetrics->getTextWidth(button->getText()); + // } + // else { + // FontMetrics *fontMetrics= menu->getFont()->getMetrics(); + // currentButtonWidth = fontMetrics->getTextWidth(button->getText()); + // } + // + // if(maxButtonWidth < 0 || currentButtonWidth > maxButtonWidth) { + // maxButtonWidth = currentButtonWidth + 5; + // } + // } + + for (unsigned int i = 0; i < menuItems.size(); ++i) { + GraphicButton *button = &menuItems[i]; + + //button->setW(maxButtonWidth); + renderButton(button); + } + + } + + void Renderer::setupRenderForVideo() { + clearBuffers(); + //3d + reset3dMenu(); + clearZBuffer(); + //2d + reset2d(); + glClearColor(0.f, 0.f, 0.f, 1.f); + } + + void Renderer::renderVideoLoading(int progressPercent) { + //printf("Rendering progress progressPercent = %d\n",progressPercent); + setupRenderForVideo(); + + Lang &lang = Lang::getInstance(); + string textToRender = lang.getString("PleaseWait"); + const Metrics &metrics = Metrics::getInstance(); + + static Chrono cycle(true); + static float anim = 0.0f; + + if (CoreData::getInstance().getMenuFontBig3D() != NULL) { + + int w = metrics.getVirtualW(); + int renderX = (w / 2) - (CoreData::getInstance().getMenuFontBig3D()->getMetrics()->getTextWidth(textToRender) / 2); + int h = metrics.getVirtualH(); + int renderY = (h / 2) + (CoreData::getInstance().getMenuFontBig3D()->getMetrics()->getHeight(textToRender) / 2); + + renderText3D( + textToRender, + CoreData::getInstance().getMenuFontBig3D(), + Vec4f(1.f, 1.f, 0.f, anim), + renderX, renderY, false); + } else { + renderText( + textToRender, + CoreData::getInstance().getMenuFontBig(), + Vec4f(1.f, 1.f, 0.f, anim), (metrics.getScreenW() / 2), + (metrics.getScreenH() / 2), true); + } + swapBuffers(); + + if (cycle.getCurMillis() % 50 == 0) { + static bool animCycleUp = true; + if (animCycleUp == true) { + anim += 0.1f; + if (anim > 1.f) { + anim = 1.f; + cycle.reset(); + animCycleUp = false; + } + } else { + anim -= 0.1f; + if (anim < 0.f) { + anim = 0.f; + cycle.reset(); + animCycleUp = true; + } + } + } + } + + } +}//end namespace diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 3eea90fac..6a02cbccb 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_RENDERER_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include "vec.h" @@ -49,675 +49,723 @@ #include "leak_dumper.h" enum DebugUILevelType { - debugui_fps = 0x01, + debugui_fps = 0x01, debugui_unit_titles = 0x02 }; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -using namespace ::Shared::Graphics; -using namespace ::Shared::PlatformCommon; + using namespace ::Shared::Graphics; + using namespace ::Shared::PlatformCommon; -//non shared classes -class Config; -class Game; -class GameCamera; -class MainMenu; -class Console; -class MenuBackground; -class ChatManager; -class Object; -class ConsoleLineInfo; -class SurfaceCell; -class Program; -// ===================================================== -// class MeshCallbackTeamColor -// ===================================================== + //non shared classes + class Config; + class Game; + class GameCamera; + class MainMenu; + class Console; + class MenuBackground; + class ChatManager; + class Object; + class ConsoleLineInfo; + class SurfaceCell; + class Program; + // ===================================================== + // class MeshCallbackTeamColor + // ===================================================== -class MeshCallbackTeamColor: public MeshCallback { -private: - const Texture *teamTexture; + class MeshCallbackTeamColor : public MeshCallback { + private: + const Texture *teamTexture; -public: - MeshCallbackTeamColor() : MeshCallback() { - teamTexture = NULL; - } - void setTeamTexture(const Texture *teamTexture) {this->teamTexture= teamTexture;} - virtual void execute(const Mesh *mesh); + public: + MeshCallbackTeamColor() : MeshCallback() { + teamTexture = NULL; + } + void setTeamTexture(const Texture *teamTexture) { + this->teamTexture = teamTexture; + } + virtual void execute(const Mesh *mesh); - static bool noTeamColors; -}; + static bool noTeamColors; + }; -// =========================================================== -// class Renderer -// -/// OpenGL renderer, uses the shared library -// =========================================================== + // =========================================================== + // class Renderer + // + /// OpenGL renderer, uses the shared library + // =========================================================== -class VisibleQuadContainerCache { -protected: + class VisibleQuadContainerCache { + protected: - inline void CopyAll(const VisibleQuadContainerCache &obj) { - cacheFrame = obj.cacheFrame; - visibleObjectList = obj.visibleObjectList; - visibleUnitList = obj.visibleUnitList; - visibleQuadUnitList = obj.visibleQuadUnitList; - visibleQuadUnitBuildList = obj.visibleQuadUnitBuildList; - visibleScaledCellList = obj.visibleScaledCellList; - visibleScaledCellToScreenPosList = obj.visibleScaledCellToScreenPosList; - lastVisibleQuad = obj.lastVisibleQuad; - frustumData = obj.frustumData; - proj = obj.proj; - modl = obj.modl; - frustumDataCache = obj.frustumDataCache; - } - -public: - - inline VisibleQuadContainerCache() { - cacheFrame = 0; - clearFrustumData(); - clearCacheData(); - } - inline VisibleQuadContainerCache(const VisibleQuadContainerCache &obj) { - CopyAll(obj); - } - inline VisibleQuadContainerCache & operator=(const VisibleQuadContainerCache &obj) { - CopyAll(obj); - return *this; - } - - inline void clearCacheData() { - clearVolatileCacheData(); - clearNonVolatileCacheData(); - } - inline void clearVolatileCacheData() { - visibleUnitList.clear(); - visibleQuadUnitList.clear(); - visibleQuadUnitBuildList.clear(); - //inVisibleUnitList.clear(); - - visibleUnitList.reserve(500); - visibleQuadUnitList.reserve(500); - visibleQuadUnitBuildList.reserve(100); - } - inline void clearNonVolatileCacheData() { - visibleObjectList.clear(); - visibleScaledCellList.clear(); - visibleScaledCellToScreenPosList.clear(); - - visibleObjectList.reserve(500); - visibleScaledCellList.reserve(500); - } - inline void clearFrustumData() { - frustumData = vector >(6,vector(4,0)); - proj = vector(16,0); - modl = vector(16,0); - frustumDataCache.clear(); - } - int cacheFrame; - Quad2i lastVisibleQuad; - std::vector visibleObjectList; - std::vector visibleQuadUnitList; - std::vector visibleQuadUnitBuildList; - std::vector visibleUnitList; - std::vector visibleScaledCellList; - std::map visibleScaledCellToScreenPosList; - - static bool enableFrustumCalcs; - vector > frustumData; - vector proj; - vector modl; - map,vector >, vector > > frustumDataCache; - -}; - -class VisibleQuadContainerVBOCache { -public: - // Vertex Buffer Object Names - bool hasBuiltVBOs; - uint32 m_nVBOVertices; // Vertex VBO Name - uint32 m_nVBOFowTexCoords; // Texture Coordinate VBO Name for fog of war texture coords - uint32 m_nVBOSurfaceTexCoords; // Texture Coordinate VBO Name for surface texture coords - uint32 m_nVBONormals; // Normal VBO Name - //uint32 m_nVBOIndexes; // Indexes VBO Name -}; - -enum ConsoleMode { - consoleOff, - consoleNormal, - consoleFull, - consoleStoredOnly, - consoleStoredAndNormal, - - consoleCount -}; - -class Renderer : public RendererInterface, - public BaseRenderer, - // This is for screen saver thread - public SimpleTaskCallbackInterface, - public VideoLoadingCallbackInterface { -public: - //progress bar - static const int maxProgressBar; - static const Vec4f progressBarBack1; - static const Vec4f progressBarBack2; - static const Vec4f progressBarFront1; - static const Vec4f progressBarFront2; - - //sun and moon - static const float sunDist; - static const float moonDist; - static const float lightAmbFactor; - - //mouse - static const int maxMouse2dAnim; - - //texture units - static const GLenum baseTexUnit; - static const GLenum fowTexUnit; - static const GLenum shadowTexUnit; - - //selection - static const float selectionCircleRadius; - static const float magicCircleRadius; - - //perspective values - static const float perspFov; - static const float perspNearPlane; - static float perspFarPlane; - - //default values - static const float ambFactor; - static const Vec4f defSpecularColor; - static const Vec4f defDiffuseColor; - static const Vec4f defAmbientColor; - static const Vec4f defColor; - static const Vec4f fowColor; - - //light - static const float maxLightDist; - - static bool renderText3DEnabled; - -public: - enum Shadows { - sDisabled, - sProjected, - sShadowMapping, - - sCount - }; - -private: - - //config - int maxLights; - bool photoMode; - int shadowTextureSize; - int shadowFrameSkip; - float shadowIntensity; - bool focusArrows; - bool textures3D; - Shadows shadows; - int maxConsoleLines; - - //game - const Game *game; - GameCamera *gameCamera; - const MainMenu *menu; - Program *program; - - //misc - int triangleCount; - int pointCount; - Quad2i visibleQuad; - Quad2i visibleQuadFromCamera; - Vec4f nearestLightPos; - VisibleQuadContainerCache quadCache; - VisibleQuadContainerCache quadCacheSelection; - - //renderers - ModelRenderer *modelRenderer; - TextRenderer2D *textRenderer; - TextRenderer3D *textRenderer3D; - ParticleRenderer *particleRenderer; - - //texture managers - ModelManager *modelManager[rsCount]; - TextureManager *textureManager[rsCount]; - FontManager *fontManager[rsCount]; - ParticleManager *particleManager[rsCount]; - - //state lists - //GLuint list3d; - //bool list3dValid; - //GLuint list2d; - //bool list2dValid; - //GLuint list3dMenu; - //bool list3dMenuValid; - //GLuint *customlist3dMenu; - //const MainMenu *mm3d; - const MainMenu *custom_mm3d; - - //shadows - GLuint shadowMapHandle; - bool shadowMapHandleValid; - - Matrix4f shadowMapMatrix; - int shadowMapFrame; - - //water - float waterAnim; - - bool allowRenderUnitTitles; - //std::vector > renderUnitTitleList; - std::vector visibleFrameUnitList; - string visibleFrameUnitListCameraKey; - - bool no2DMouseRendering; - bool showDebugUI; - int showDebugUILevel; - - int lastRenderFps; - float smoothedRenderFps; - bool shadowsOffDueToMinRender; - - std::vector > deferredParticleSystems; - - SimpleTaskThread *saveScreenShotThread; - Mutex *saveScreenShotThreadAccessor; - std::list > saveScreenQueue; - - std::map worldToScreenPosCache; - - //bool masterserverMode; - - std::map mapSurfaceVBOCache; - - class SurfaceData { - public: - inline SurfaceData() { - uniqueId=0; - bufferCount=0; - textureHandle=0; - } - static uint32 nextUniqueId; - uint32 uniqueId; - int bufferCount; - int textureHandle; - vector texCoords; - vector texCoordsSurface; - vector vertices; - vector normals; - }; - - //VisibleQuadContainerVBOCache * GetSurfaceVBOs(SurfaceData *cellData); - void ReleaseSurfaceVBOs(); - std::map > > mapSurfaceData; - static bool rendererEnded; - - class MapRenderer { - public: - inline MapRenderer(): map(NULL) {} - inline ~MapRenderer() { destroy(); } - void render(const Map* map,float coordStep,VisibleQuadContainerCache &qCache); - void renderVisibleLayers(const Map* map,float coordStep,VisibleQuadContainerCache &qCache); - void destroy(); - private: - void load(float coordStep); - void loadVisibleLayers(float coordStep,VisibleQuadContainerCache &qCache); - - const Map* map; - struct Layer { - inline explicit Layer(int th): - vbo_vertices(0), vbo_normals(0), - vbo_fowTexCoords(0), vbo_surfTexCoords(0), - vbo_indices(0), indexCount(0), - textureHandle(th),textureCRC(0) {} - - inline explicit Layer(Layer &obj) { - *this = obj; + inline void CopyAll(const VisibleQuadContainerCache &obj) { + cacheFrame = obj.cacheFrame; + visibleObjectList = obj.visibleObjectList; + visibleUnitList = obj.visibleUnitList; + visibleQuadUnitList = obj.visibleQuadUnitList; + visibleQuadUnitBuildList = obj.visibleQuadUnitBuildList; + visibleScaledCellList = obj.visibleScaledCellList; + visibleScaledCellToScreenPosList = obj.visibleScaledCellToScreenPosList; + lastVisibleQuad = obj.lastVisibleQuad; + frustumData = obj.frustumData; + proj = obj.proj; + modl = obj.modl; + frustumDataCache = obj.frustumDataCache; } - inline Layer & operator=(Layer &obj) { - this->vertices = obj.vertices; - this->normals = obj.normals; - this->fowTexCoords = obj.fowTexCoords; - this->surfTexCoords = obj.surfTexCoords; - this->indices = obj.indices; - this->cellToIndicesMap = obj.cellToIndicesMap; - this->rowsToRenderCache = obj.rowsToRenderCache; - this->vbo_vertices = obj.vbo_vertices; - this->vbo_normals = obj.vbo_normals; - this->vbo_fowTexCoords = obj.vbo_fowTexCoords; - this->vbo_surfTexCoords = obj.vbo_surfTexCoords; - this->vbo_indices = obj.vbo_indices; - this->indexCount = obj.indexCount; - this->textureHandle = obj.textureHandle; - this->texturePath = obj.texturePath; - this->textureCRC = obj.textureCRC; + public: + inline VisibleQuadContainerCache() { + cacheFrame = 0; + clearFrustumData(); + clearCacheData(); + } + inline VisibleQuadContainerCache(const VisibleQuadContainerCache &obj) { + CopyAll(obj); + } + inline VisibleQuadContainerCache & operator=(const VisibleQuadContainerCache &obj) { + CopyAll(obj); return *this; } - ~Layer(); - void load_vbos(bool vboEnabled); - void render(VisibleQuadContainerCache &qCache); - void renderVisibleLayer(); + inline void clearCacheData() { + clearVolatileCacheData(); + clearNonVolatileCacheData(); + } + inline void clearVolatileCacheData() { + visibleUnitList.clear(); + visibleQuadUnitList.clear(); + visibleQuadUnitBuildList.clear(); + //inVisibleUnitList.clear(); - std::vector vertices, normals; - std::vector fowTexCoords, surfTexCoords; - std::vector indices; - std::map cellToIndicesMap; - std::map > > rowsToRenderCache; + visibleUnitList.reserve(500); + visibleQuadUnitList.reserve(500); + visibleQuadUnitBuildList.reserve(100); + } + inline void clearNonVolatileCacheData() { + visibleObjectList.clear(); + visibleScaledCellList.clear(); + visibleScaledCellToScreenPosList.clear(); + + visibleObjectList.reserve(500); + visibleScaledCellList.reserve(500); + } + inline void clearFrustumData() { + frustumData = vector >(6, vector(4, 0)); + proj = vector(16, 0); + modl = vector(16, 0); + frustumDataCache.clear(); + } + int cacheFrame; + Quad2i lastVisibleQuad; + std::vector visibleObjectList; + std::vector visibleQuadUnitList; + std::vector visibleQuadUnitBuildList; + std::vector visibleUnitList; + std::vector visibleScaledCellList; + std::map visibleScaledCellToScreenPosList; + + static bool enableFrustumCalcs; + vector > frustumData; + vector proj; + vector modl; + map, vector >, vector > > frustumDataCache; - GLuint vbo_vertices, vbo_normals, - vbo_fowTexCoords, vbo_surfTexCoords, - vbo_indices; - int indexCount; - int textureHandle; - string texturePath; - uint32 textureCRC; }; - typedef std::vector Layers; - Layers layers; - Quad2i lastVisibleQuad; - } mapRenderer; - bool ExtractFrustum(VisibleQuadContainerCache &quadCacheItem); - //bool PointInFrustum(vector > &frustum, float x, float y, float z ); - //bool SphereInFrustum(vector > &frustum, float x, float y, float z, float radius); - bool CubeInFrustum(vector > &frustum, float x, float y, float z, float size ); + class VisibleQuadContainerVBOCache { + public: + // Vertex Buffer Object Names + bool hasBuiltVBOs; + uint32 m_nVBOVertices; // Vertex VBO Name + uint32 m_nVBOFowTexCoords; // Texture Coordinate VBO Name for fog of war texture coords + uint32 m_nVBOSurfaceTexCoords; // Texture Coordinate VBO Name for surface texture coords + uint32 m_nVBONormals; // Normal VBO Name + //uint32 m_nVBOIndexes; // Indexes VBO Name + }; -private: - Renderer(); - ~Renderer(); + enum ConsoleMode { + consoleOff, + consoleNormal, + consoleFull, + consoleStoredOnly, + consoleStoredAndNormal, -public: - static Renderer &getInstance(); - static bool isEnded(); - //bool isMasterserverMode() const { return masterserverMode; } + consoleCount + }; - void addToDeferredParticleSystemList(std::pair deferredParticleSystem); - void manageDeferredParticleSystems(); + class Renderer : public RendererInterface, + public BaseRenderer, + // This is for screen saver thread + public SimpleTaskCallbackInterface, + public VideoLoadingCallbackInterface { + public: + //progress bar + static const int maxProgressBar; + static const Vec4f progressBarBack1; + static const Vec4f progressBarBack2; + static const Vec4f progressBarFront1; + static const Vec4f progressBarFront2; - void reinitAll(); + //sun and moon + static const float sunDist; + static const float moonDist; + static const float lightAmbFactor; - //init - void init(); - void initGame(const Game *game, GameCamera *gameCamera); - void initMenu(const MainMenu *mm); - void reset3d(); - void reset2d(); - void reset3dMenu(); + //mouse + static const int maxMouse2dAnim; - //end - void end(); - void endScenario(); - void endMenu(); - void endGame(bool isFinalEnd); + //texture units + static const GLenum baseTexUnit; + static const GLenum fowTexUnit; + static const GLenum shadowTexUnit; - //get - inline int getTriangleCount() const {return triangleCount;} - inline int getPointCount() const {return pointCount;} + //selection + static const float selectionCircleRadius; + static const float magicCircleRadius; - //misc - //void reloadResources(); + //perspective values + static const float perspFov; + static const float perspNearPlane; + static float perspFarPlane; - //engine interface - void initTexture(ResourceScope rs, Texture *texture); - void endTexture(ResourceScope rs, Texture *texture,bool mustExistInList=false); - void endLastTexture(ResourceScope rs, bool mustExistInList=false); + //default values + static const float ambFactor; + static const Vec4f defSpecularColor; + static const Vec4f defDiffuseColor; + static const Vec4f defAmbientColor; + static const Vec4f defColor; + static const Vec4f fowColor; - Model *newModel(ResourceScope rs,const string &path,bool deletePixMapAfterLoad=false,std::map > > *loadedFileList=NULL, string *sourceLoader=NULL); - void endModel(ResourceScope rs, Model *model, bool mustExistInList=false); - void endLastModel(ResourceScope rs, bool mustExistInList=false); + //light + static const float maxLightDist; - Texture2D *newTexture2D(ResourceScope rs); - Texture3D *newTexture3D(ResourceScope rs); - Font2D *newFont(ResourceScope rs); - Font3D *newFont3D(ResourceScope rs); - void endFont(::Shared::Graphics::Font *font, ResourceScope rs, bool mustExistInList=false); - //void resetFontManager(ResourceScope rs); + static bool renderText3DEnabled; - inline TextRenderer2D *getTextRenderer() const {return textRenderer;} - inline TextRenderer3D *getTextRenderer3D() const {return textRenderer3D;} + public: + enum Shadows { + sDisabled, + sProjected, + sShadowMapping, - void manageParticleSystem(ParticleSystem *particleSystem, ResourceScope rs); - void cleanupParticleSystems(vector &particleSystems,ResourceScope rs); - void cleanupUnitParticleSystems(vector &particleSystems,ResourceScope rs); - bool validateParticleSystemStillExists(ParticleSystem * particleSystem,ResourceScope rs) const; - void removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner,ResourceScope rs); - void updateParticleManager(ResourceScope rs,int renderFps=-1); - void renderParticleManager(ResourceScope rs); - void swapBuffers(); + sCount + }; - //lights and camera - void setupLighting(); - //void setupLightingForRotatedModel(); - void loadGameCameraMatrix(); - void loadCameraMatrix(const Camera *camera); - void computeVisibleQuad(); + private: - //basic rendering - void renderMouse2d(int mouseX, int mouseY, int anim, float fade= 0.f); - void renderMouse3d(); + //config + int maxLights; + bool photoMode; + int shadowTextureSize; + int shadowFrameSkip; + float shadowIntensity; + bool focusArrows; + bool textures3D; + Shadows shadows; + int maxConsoleLines; - void renderGhostModel(const UnitType *building, const Vec2i pos,CardinalDir facing,Vec4f *forceColor=NULL); + //game + const Game *game; + GameCamera *gameCamera; + const MainMenu *menu; + Program *program; - void renderBackground(const Texture2D *texture); - void renderTextureQuad(int x, int y, int w, int h, const Texture2D *texture, float alpha=1.f,const Vec3f *color=NULL); - void renderConsole(const Console *console, ConsoleMode mode=consoleNormal, int overrideMaxConsoleLines=-1); - void renderConsoleLine3D(int lineIndex, int xPosition, int yPosition, int lineHeight, Font3D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo); - void renderConsoleLine(int lineIndex, int xPosition, int yPosition, int lineHeight, Font2D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo); + //misc + int triangleCount; + int pointCount; + Quad2i visibleQuad; + Quad2i visibleQuadFromCamera; + Vec4f nearestLightPos; + VisibleQuadContainerCache quadCache; + VisibleQuadContainerCache quadCacheSelection; - void renderChatManager(const ChatManager *chatManager); - void renderClock(); - void renderPerformanceStats(); - void renderResourceStatus(); - void renderSelectionQuad(); - void renderText(const string &text, Font2D *font, float alpha, int x, int y, bool centered= false); - void renderText(const string &text, Font2D *font, const Vec3f &color, int x, int y, bool centered= false); - void renderText(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered=false); - void renderTextShadow(const string &text, Font2D *font,const Vec4f &color, int x, int y, bool centered= false); + //renderers + ModelRenderer *modelRenderer; + TextRenderer2D *textRenderer; + TextRenderer3D *textRenderer3D; + ParticleRenderer *particleRenderer; - void renderText3D(const string &text, Font3D *font, float alpha, int x, int y, bool centered); - void renderText3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, bool centered); - void renderText3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered); - void renderTextShadow3D(const string &text, Font3D *font,const Vec4f &color, int x, int y, bool centered=false); - void renderProgressBar3D(int size, int x, int y, Font3D *font, int customWidth=-1, string prefixLabel="", bool centeredText=true,int customHeight=-1); + //texture managers + ModelManager *modelManager[rsCount]; + TextureManager *textureManager[rsCount]; + FontManager *fontManager[rsCount]; + ParticleManager *particleManager[rsCount]; - Vec2f getCentered3DPos(const string &text, Font3D *font, Vec2f &pos, int w, int h, bool centeredW, bool centeredH); - void renderTextBoundingBox3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth); - void renderTextBoundingBox3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled,int maxEditWidth, int maxEditRenderWidth); - void renderTextBoundingBox3D(const string &text, Font3D *font, float alpha, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled,int maxEditWidth, int maxEditRenderWidth); + //state lists + //GLuint list3d; + //bool list3dValid; + //GLuint list2d; + //bool list2dValid; + //GLuint list3dMenu; + //bool list3dMenuValid; + //GLuint *customlist3dMenu; + //const MainMenu *mm3d; + const MainMenu *custom_mm3d; - void renderTextSurroundingBox(int x, int y, int w, int h,int maxEditWidth, int maxEditRenderWidth); + //shadows + GLuint shadowMapHandle; + bool shadowMapHandleValid; - void beginRenderToTexture(Texture2D **renderToTexture); - void endRenderToTexture(Texture2D **renderToTexture); + Matrix4f shadowMapMatrix; + int shadowMapFrame; - void renderFPSWhenEnabled(int lastFps); + //water + float waterAnim; - //components - void renderLabel(GraphicLabel *label); - void renderLabel(GraphicLabel *label,const Vec3f *color); - void renderLabel(GraphicLabel *label,const Vec4f *color); - void renderButton(GraphicButton *button,const Vec4f *fontColorOverride=NULL,bool *lightedOverride=NULL); - void renderCheckBox(const GraphicCheckBox *box); - void renderLine(const GraphicLine *line); - void renderScrollBar(const GraphicScrollBar *sb); - void renderListBox(GraphicListBox *listBox); - void renderMessageBox(GraphicMessageBox *listBox); - void renderPopupMenu(PopupMenu *menu); + bool allowRenderUnitTitles; + //std::vector > renderUnitTitleList; + std::vector visibleFrameUnitList; + string visibleFrameUnitListCameraKey; - //complex rendering - void renderSurface(const int renderFps); - void renderObjects(const int renderFps); + bool no2DMouseRendering; + bool showDebugUI; + int showDebugUILevel; - void renderWater(); - void renderUnits(bool airUnits, const int renderFps); - void renderUnitsToBuild(const int renderFps); + int lastRenderFps; + float smoothedRenderFps; + bool shadowsOffDueToMinRender; - void renderSelectionEffects(int healthbarMode); - void renderHealthBars(int healthbarMode); - void renderWaterEffects(); - void renderHud(); - void renderMinimap(); - void renderDisplay(); - void renderMenuBackground(const MenuBackground *menuBackground); - void renderMapPreview(const MapPreview *map, bool renderAll, int screenX, int screenY,Texture2D **renderToTexture=NULL); - void renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels,const Vec3f characterPosition, float anim); + std::vector > deferredParticleSystems; - //computing - bool ccomputePosition(const Vec2i &screenPos, Vec2i &worldPos,bool exactCoords=false); - void computeSelected(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp); - void selectUsingColorPicking(Selection::UnitContainer &units, const Object *&obj,const bool withObjectSelection,const Vec2i &posDown, const Vec2i &posUp); - void selectUsingSelectionBuffer(Selection::UnitContainer &units,const Object *&obj, const bool withObjectSelection,const Vec2i &posDown, const Vec2i &posUp); - void selectUsingFrustumSelection(Selection::UnitContainer &units,const Object *&obj, const bool withObjectSelection,const Vec2i &posDown, const Vec2i &posUp); + SimpleTaskThread *saveScreenShotThread; + Mutex *saveScreenShotThreadAccessor; + std::list > saveScreenQueue; + + std::map worldToScreenPosCache; + + //bool masterserverMode; + + std::map mapSurfaceVBOCache; + + class SurfaceData { + public: + inline SurfaceData() { + uniqueId = 0; + bufferCount = 0; + textureHandle = 0; + } + static uint32 nextUniqueId; + uint32 uniqueId; + int bufferCount; + int textureHandle; + vector texCoords; + vector texCoordsSurface; + vector vertices; + vector normals; + }; + + //VisibleQuadContainerVBOCache * GetSurfaceVBOs(SurfaceData *cellData); + void ReleaseSurfaceVBOs(); + std::map > > mapSurfaceData; + static bool rendererEnded; + + class MapRenderer { + public: + inline MapRenderer() : map(NULL) { + } + inline ~MapRenderer() { + destroy(); + } + void render(const Map* map, float coordStep, VisibleQuadContainerCache &qCache); + void renderVisibleLayers(const Map* map, float coordStep, VisibleQuadContainerCache &qCache); + void destroy(); + private: + void load(float coordStep); + void loadVisibleLayers(float coordStep, VisibleQuadContainerCache &qCache); + + const Map* map; + struct Layer { + inline explicit Layer(int th) : + vbo_vertices(0), vbo_normals(0), + vbo_fowTexCoords(0), vbo_surfTexCoords(0), + vbo_indices(0), indexCount(0), + textureHandle(th), textureCRC(0) { + } + + inline explicit Layer(Layer &obj) { + *this = obj; + } + + inline Layer & operator=(Layer &obj) { + this->vertices = obj.vertices; + this->normals = obj.normals; + this->fowTexCoords = obj.fowTexCoords; + this->surfTexCoords = obj.surfTexCoords; + this->indices = obj.indices; + this->cellToIndicesMap = obj.cellToIndicesMap; + this->rowsToRenderCache = obj.rowsToRenderCache; + this->vbo_vertices = obj.vbo_vertices; + this->vbo_normals = obj.vbo_normals; + this->vbo_fowTexCoords = obj.vbo_fowTexCoords; + this->vbo_surfTexCoords = obj.vbo_surfTexCoords; + this->vbo_indices = obj.vbo_indices; + this->indexCount = obj.indexCount; + this->textureHandle = obj.textureHandle; + this->texturePath = obj.texturePath; + this->textureCRC = obj.textureCRC; + + return *this; + } + + ~Layer(); + void load_vbos(bool vboEnabled); + void render(VisibleQuadContainerCache &qCache); + void renderVisibleLayer(); + + std::vector vertices, normals; + std::vector fowTexCoords, surfTexCoords; + std::vector indices; + std::map cellToIndicesMap; + std::map > > rowsToRenderCache; + + GLuint vbo_vertices, vbo_normals, + vbo_fowTexCoords, vbo_surfTexCoords, + vbo_indices; + int indexCount; + int textureHandle; + string texturePath; + uint32 textureCRC; + }; + typedef std::vector Layers; + Layers layers; + Quad2i lastVisibleQuad; + } mapRenderer; + + bool ExtractFrustum(VisibleQuadContainerCache &quadCacheItem); + //bool PointInFrustum(vector > &frustum, float x, float y, float z ); + //bool SphereInFrustum(vector > &frustum, float x, float y, float z, float radius); + bool CubeInFrustum(vector > &frustum, float x, float y, float z, float size); + + private: + Renderer(); + ~Renderer(); + + public: + static Renderer &getInstance(); + static bool isEnded(); + //bool isMasterserverMode() const { return masterserverMode; } + + void addToDeferredParticleSystemList(std::pair deferredParticleSystem); + void manageDeferredParticleSystems(); + + void reinitAll(); + + //init + void init(); + void initGame(const Game *game, GameCamera *gameCamera); + void initMenu(const MainMenu *mm); + void reset3d(); + void reset2d(); + void reset3dMenu(); + + //end + void end(); + void endScenario(); + void endMenu(); + void endGame(bool isFinalEnd); + + //get + inline int getTriangleCount() const { + return triangleCount; + } + inline int getPointCount() const { + return pointCount; + } + + //misc + //void reloadResources(); + + //engine interface + void initTexture(ResourceScope rs, Texture *texture); + void endTexture(ResourceScope rs, Texture *texture, bool mustExistInList = false); + void endLastTexture(ResourceScope rs, bool mustExistInList = false); + + Model *newModel(ResourceScope rs, const string &path, bool deletePixMapAfterLoad = false, std::map > > *loadedFileList = NULL, string *sourceLoader = NULL); + void endModel(ResourceScope rs, Model *model, bool mustExistInList = false); + void endLastModel(ResourceScope rs, bool mustExistInList = false); + + Texture2D *newTexture2D(ResourceScope rs); + Texture3D *newTexture3D(ResourceScope rs); + Font2D *newFont(ResourceScope rs); + Font3D *newFont3D(ResourceScope rs); + void endFont(::Shared::Graphics::Font *font, ResourceScope rs, bool mustExistInList = false); + //void resetFontManager(ResourceScope rs); + + inline TextRenderer2D *getTextRenderer() const { + return textRenderer; + } + inline TextRenderer3D *getTextRenderer3D() const { + return textRenderer3D; + } + + void manageParticleSystem(ParticleSystem *particleSystem, ResourceScope rs); + void cleanupParticleSystems(vector &particleSystems, ResourceScope rs); + void cleanupUnitParticleSystems(vector &particleSystems, ResourceScope rs); + bool validateParticleSystemStillExists(ParticleSystem * particleSystem, ResourceScope rs) const; + void removeParticleSystemsForParticleOwner(ParticleOwner * particleOwner, ResourceScope rs); + void updateParticleManager(ResourceScope rs, int renderFps = -1); + void renderParticleManager(ResourceScope rs); + void swapBuffers(); + + //lights and camera + void setupLighting(); + //void setupLightingForRotatedModel(); + void loadGameCameraMatrix(); + void loadCameraMatrix(const Camera *camera); + void computeVisibleQuad(); + + //basic rendering + void renderMouse2d(int mouseX, int mouseY, int anim, float fade = 0.f); + void renderMouse3d(); + + void renderGhostModel(const UnitType *building, const Vec2i pos, CardinalDir facing, Vec4f *forceColor = NULL); + + void renderBackground(const Texture2D *texture); + void renderTextureQuad(int x, int y, int w, int h, const Texture2D *texture, float alpha = 1.f, const Vec3f *color = NULL); + void renderConsole(const Console *console, ConsoleMode mode = consoleNormal, int overrideMaxConsoleLines = -1); + void renderConsoleLine3D(int lineIndex, int xPosition, int yPosition, int lineHeight, Font3D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo); + void renderConsoleLine(int lineIndex, int xPosition, int yPosition, int lineHeight, Font2D* font, string stringToHightlight, const ConsoleLineInfo *lineInfo); + + void renderChatManager(const ChatManager *chatManager); + void renderClock(); + void renderPerformanceStats(); + void renderResourceStatus(); + void renderSelectionQuad(); + void renderText(const string &text, Font2D *font, float alpha, int x, int y, bool centered = false); + void renderText(const string &text, Font2D *font, const Vec3f &color, int x, int y, bool centered = false); + void renderText(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered = false); + void renderTextShadow(const string &text, Font2D *font, const Vec4f &color, int x, int y, bool centered = false); + + void renderText3D(const string &text, Font3D *font, float alpha, int x, int y, bool centered); + void renderText3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, bool centered); + void renderText3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered); + void renderTextShadow3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, bool centered = false); + void renderProgressBar3D(int size, int x, int y, Font3D *font, int customWidth = -1, string prefixLabel = "", bool centeredText = true, int customHeight = -1); + + Vec2f getCentered3DPos(const string &text, Font3D *font, Vec2f &pos, int w, int h, bool centeredW, bool centeredH); + void renderTextBoundingBox3D(const string &text, Font3D *font, const Vec4f &color, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth); + void renderTextBoundingBox3D(const string &text, Font3D *font, const Vec3f &color, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth); + void renderTextBoundingBox3D(const string &text, Font3D *font, float alpha, int x, int y, int w, int h, bool centeredW, bool centeredH, bool editModeEnabled, int maxEditWidth, int maxEditRenderWidth); + + void renderTextSurroundingBox(int x, int y, int w, int h, int maxEditWidth, int maxEditRenderWidth); + + void beginRenderToTexture(Texture2D **renderToTexture); + void endRenderToTexture(Texture2D **renderToTexture); + + void renderFPSWhenEnabled(int lastFps); + + //components + void renderLabel(GraphicLabel *label); + void renderLabel(GraphicLabel *label, const Vec3f *color); + void renderLabel(GraphicLabel *label, const Vec4f *color); + void renderButton(GraphicButton *button, const Vec4f *fontColorOverride = NULL, bool *lightedOverride = NULL); + void renderCheckBox(const GraphicCheckBox *box); + void renderLine(const GraphicLine *line); + void renderScrollBar(const GraphicScrollBar *sb); + void renderListBox(GraphicListBox *listBox); + void renderMessageBox(GraphicMessageBox *listBox); + void renderPopupMenu(PopupMenu *menu); + + //complex rendering + void renderSurface(const int renderFps); + void renderObjects(const int renderFps); + + void renderWater(); + void renderUnits(bool airUnits, const int renderFps); + void renderUnitsToBuild(const int renderFps); + + void renderSelectionEffects(int healthbarMode); + void renderHealthBars(int healthbarMode); + void renderWaterEffects(); + void renderHud(); + void renderMinimap(); + void renderDisplay(); + void renderMenuBackground(const MenuBackground *menuBackground); + void renderMapPreview(const MapPreview *map, bool renderAll, int screenX, int screenY, Texture2D **renderToTexture = NULL); + void renderMenuBackground(Camera *camera, float fade, Model *mainModel, vector characterModels, const Vec3f characterPosition, float anim); + + //computing + bool ccomputePosition(const Vec2i &screenPos, Vec2i &worldPos, bool exactCoords = false); + void computeSelected(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp); + void selectUsingColorPicking(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp); + void selectUsingSelectionBuffer(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp); + void selectUsingFrustumSelection(Selection::UnitContainer &units, const Object *&obj, const bool withObjectSelection, const Vec2i &posDown, const Vec2i &posUp); - //gl wrap - string getGlInfo(); - string getGlMoreInfo(); - void autoConfig(); + //gl wrap + string getGlInfo(); + string getGlMoreInfo(); + void autoConfig(); - //clear - void clearBuffers(); - void clearZBuffer(); + //clear + void clearBuffers(); + void clearZBuffer(); - //shadows - void renderShadowsToTexture(const int renderFps); + //shadows + void renderShadowsToTexture(const int renderFps); - //misc - void loadConfig(); - void saveScreen(const string &path,int w=0, int h=0); - inline Quad2i getVisibleQuad() const {return visibleQuad;} - inline Quad2i getVisibleQuadFromCamera() const {return visibleQuadFromCamera;} - void renderTeamColorPlane(); - void renderSpecialHighlightUnits(std::map unitHighlightList); - void renderTeamColorCircle(); - void renderMorphEffects(); + //misc + void loadConfig(); + void saveScreen(const string &path, int w = 0, int h = 0); + inline Quad2i getVisibleQuad() const { + return visibleQuad; + } + inline Quad2i getVisibleQuadFromCamera() const { + return visibleQuadFromCamera; + } + void renderTeamColorPlane(); + void renderSpecialHighlightUnits(std::map unitHighlightList); + void renderTeamColorCircle(); + void renderMorphEffects(); - //static - static Shadows strToShadows(const string &s); - static string shadowsToStr(Shadows shadows); + //static + static Shadows strToShadows(const string &s); + static string shadowsToStr(Shadows shadows); - inline const Game * getGame() { return game; } + inline const Game * getGame() { + return game; + } - void setAllowRenderUnitTitles(bool value); - inline bool getAllowRenderUnitTitles() { return allowRenderUnitTitles; } - void renderUnitTitles(Font2D *font, Vec3f color); - void renderUnitTitles3D(Font3D *font, Vec3f color); - Vec3f computeScreenPosition(const Vec3f &worldPos); + void setAllowRenderUnitTitles(bool value); + inline bool getAllowRenderUnitTitles() { + return allowRenderUnitTitles; + } + void renderUnitTitles(Font2D *font, Vec3f color); + void renderUnitTitles3D(Font3D *font, Vec3f color); + Vec3f computeScreenPosition(const Vec3f &worldPos); - void setPhotoMode(bool value) { photoMode = value; } + void setPhotoMode(bool value) { + photoMode = value; + } - inline bool getNo2DMouseRendering() const { return no2DMouseRendering; } - void setNo2DMouseRendering(bool value) { no2DMouseRendering = value; } + inline bool getNo2DMouseRendering() const { + return no2DMouseRendering; + } + void setNo2DMouseRendering(bool value) { + no2DMouseRendering = value; + } - inline bool getShowDebugUI() const { return showDebugUI; } - void setShowDebugUI(bool value) { showDebugUI = value; } + inline bool getShowDebugUI() const { + return showDebugUI; + } + void setShowDebugUI(bool value) { + showDebugUI = value; + } - inline int getShowDebugUILevel() const { return showDebugUILevel; } - void setShowDebugUILevel(int value) { showDebugUILevel=value; } - void cycleShowDebugUILevel(); + inline int getShowDebugUILevel() const { + return showDebugUILevel; + } + void setShowDebugUILevel(int value) { + showDebugUILevel = value; + } + void cycleShowDebugUILevel(); - void setLastRenderFps(int value); - inline int getLastRenderFps() const { return lastRenderFps;} + void setLastRenderFps(int value); + inline int getLastRenderFps() const { + return lastRenderFps; + } - VisibleQuadContainerCache & getQuadCache(bool updateOnDirtyFrame=true,bool forceNew=false); - std::pair posInCellQuadCache(Vec2i pos); - //Vec3f getMarkedCellScreenPosQuadCache(Vec2i pos); - void updateMarkedCellScreenPosQuadCache(Vec2i pos); - void forceQuadCacheUpdate(); - void renderVisibleMarkedCells(bool renderTextHint=false,int x=-1, int y=-1); - void renderMarkedCellsOnMinimap(); + VisibleQuadContainerCache & getQuadCache(bool updateOnDirtyFrame = true, bool forceNew = false); + std::pair posInCellQuadCache(Vec2i pos); + //Vec3f getMarkedCellScreenPosQuadCache(Vec2i pos); + void updateMarkedCellScreenPosQuadCache(Vec2i pos); + void forceQuadCacheUpdate(); + void renderVisibleMarkedCells(bool renderTextHint = false, int x = -1, int y = -1); + void renderMarkedCellsOnMinimap(); - void renderHighlightedCellsOnMinimap(); + void renderHighlightedCellsOnMinimap(); - void removeObjectFromQuadCache(const Object *o); - void removeUnitFromQuadCache(const Unit *unit); + void removeObjectFromQuadCache(const Object *o); + void removeUnitFromQuadCache(const Unit *unit); - std::size_t getCurrentPixelByteCount(ResourceScope rs=rsGame) const; - unsigned int getSaveScreenQueueSize(); + std::size_t getCurrentPixelByteCount(ResourceScope rs = rsGame) const; + unsigned int getSaveScreenQueueSize(); - //Texture2D *saveScreenToTexture(int x, int y, int width, int height); + //Texture2D *saveScreenToTexture(int x, int y, int width, int height); - void renderProgressBar(int size, int x, int y, Font2D *font,int customWidth=-1, string prefixLabel="", bool centeredText=true); + void renderProgressBar(int size, int x, int y, Font2D *font, int customWidth = -1, string prefixLabel = "", bool centeredText = true); - static Texture2D * findTexture(string logoFilename); - static Texture2D * preloadTexture(string logoFilename); - inline int getCachedSurfaceDataSize() const { return (int)mapSurfaceData.size(); } + static Texture2D * findTexture(string logoFilename); + static Texture2D * preloadTexture(string logoFilename); + inline int getCachedSurfaceDataSize() const { + return (int) mapSurfaceData.size(); + } - //void setCustom3dMenuList(GLuint *customlist3dMenu) { this->customlist3dMenu = customlist3dMenu; } - //inline GLuint * getCustom3dMenuList() const { return this->customlist3dMenu; } - void setCustom3dMenu(const MainMenu *mm) { this->custom_mm3d = mm; } - const MainMenu * getCustom3dMenu() { return this->custom_mm3d; } + //void setCustom3dMenuList(GLuint *customlist3dMenu) { this->customlist3dMenu = customlist3dMenu; } + //inline GLuint * getCustom3dMenuList() const { return this->customlist3dMenu; } + void setCustom3dMenu(const MainMenu *mm) { + this->custom_mm3d = mm; + } + const MainMenu * getCustom3dMenu() { + return this->custom_mm3d; + } - //void init3dListMenu(const MainMenu *mm); + //void init3dListMenu(const MainMenu *mm); - void setProgram(Program *program) { this->program = program; } + void setProgram(Program *program) { + this->program = program; + } - void setupRenderForVideo(); - virtual void renderVideoLoading(int progressPercent); + void setupRenderForVideo(); + virtual void renderVideoLoading(int progressPercent); + + private: + //private misc + float computeSunAngle(float time); + float computeMoonAngle(float time); + Vec4f computeSunPos(float time); + Vec4f computeMoonPos(float time); + inline Vec4f computeWaterColor(float waterLevel, float cellHeight) { + const float waterFactor = 1.5f; + return Vec4f(1.f, 1.f, 1.f, clamp((waterLevel - cellHeight) * waterFactor, 0.f, 1.f)); + } + void checkExtension(const string &extension, const string &msg); + + //selection render + vector renderObjectsFast(bool renderingShadows = false, bool resourceOnly = false, bool colorPickingSelection = false); + vector renderUnitsFast(bool renderingShadows = false, bool colorPickingSelection = false); + + //gl requirements + void checkGlCaps(); + void checkGlOptionalCaps(); + + //gl init + void init3dList(); + void init2dList(); + + //misc + void loadProjectionMatrix(); + void enableProjectiveTexturing(); + + //private aux drawing + void renderSelectionCircle(Vec3f v, int size, float radius, float thickness = 0.2f); + bool isHealthBarVisible(const Unit *unit, int healthbarMode); + void renderHealthBar(Vec3f v, Unit *unit, float height, bool lineBorder, const Texture2D *texture = NULL, const Texture2D *backgroundTexture = NULL); + void internalRenderHp(int numberOfBars, int barNumber, float hp, Vec3f posVector, float width, float singleHPheight, Vec3f rightVector, Vec3f upVector); + void renderTeamColorEffect(Vec3f &v, int heigth, int size, Vec3f color, const Texture2D *texture); + void renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &color, float width); + //void renderTile(const Vec2i &pos); + void renderQuad(int x, int y, int w, int h, const Texture2D *texture); + + void simpleTask(BaseThread *callingThread, void *userdata); + + //static + static Texture2D::Filter strToTextureFilter(const string &s); + void cleanupScreenshotThread(); + + void render2dMenuSetup(); + void render3dSetup(); + void render3dMenuSetup(const MainMenu *mm); + + int renderResource(const Faction *factionForResourceView, + bool localFactionResourcesOnly, bool twoResourceLines, const ResourceType *rt, + int startRow, int &resourceCountRendered); + + }; -private: - //private misc - float computeSunAngle(float time); - float computeMoonAngle(float time); - Vec4f computeSunPos(float time); - Vec4f computeMoonPos(float time); - inline Vec4f computeWaterColor(float waterLevel, float cellHeight) { - const float waterFactor= 1.5f; - return Vec4f(1.f, 1.f, 1.f, clamp((waterLevel-cellHeight) * waterFactor, 0.f, 1.f)); } - void checkExtension(const string &extension, const string &msg); - - //selection render - vector renderObjectsFast(bool renderingShadows = false, bool resourceOnly = false, bool colorPickingSelection = false); - vector renderUnitsFast(bool renderingShadows = false, bool colorPickingSelection = false); - - //gl requirements - void checkGlCaps(); - void checkGlOptionalCaps(); - - //gl init - void init3dList(); - void init2dList(); - - //misc - void loadProjectionMatrix(); - void enableProjectiveTexturing(); - - //private aux drawing - void renderSelectionCircle(Vec3f v, int size, float radius, float thickness=0.2f); - bool isHealthBarVisible(const Unit *unit,int healthbarMode); - void renderHealthBar(Vec3f v, Unit *unit, float height, bool lineBorder, const Texture2D *texture=NULL, const Texture2D *backgroundTexture=NULL); - void internalRenderHp(int numberOfBars, int barNumber, float hp, Vec3f posVector, float width, float singleHPheight, Vec3f rightVector, Vec3f upVector); - void renderTeamColorEffect(Vec3f &v, int heigth, int size, Vec3f color, const Texture2D *texture); - void renderArrow(const Vec3f &pos1, const Vec3f &pos2, const Vec3f &color, float width); - //void renderTile(const Vec2i &pos); - void renderQuad(int x, int y, int w, int h, const Texture2D *texture); - - void simpleTask(BaseThread *callingThread,void *userdata); - - //static - static Texture2D::Filter strToTextureFilter(const string &s); - void cleanupScreenshotThread(); - - void render2dMenuSetup(); - void render3dSetup(); - void render3dMenuSetup(const MainMenu *mm); - - int renderResource(const Faction *factionForResourceView, - bool localFactionResourcesOnly,bool twoResourceLines, const ResourceType *rt, - int startRow, int &resourceCountRendered); - -}; - -}} //end namespace +} //end namespace #endif diff --git a/source/glest_game/graphics/unit_particle_type.cpp b/source/glest_game/graphics/unit_particle_type.cpp index 0deeefd3f..08d39ff0b 100644 --- a/source/glest_game/graphics/unit_particle_type.cpp +++ b/source/glest_game/graphics/unit_particle_type.cpp @@ -25,360 +25,353 @@ using namespace Shared::Graphics; using namespace Shared::Util; using namespace Shared::PlatformCommon; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class UnitParticleSystemType -// ===================================================== -UnitParticleSystemType::UnitParticleSystemType() : ParticleSystemType() { - shape = UnitParticleSystem::sLinear; - angle = 0; - radius = 0; - minRadius = 0; - emissionRateFade = 0; - relative = false; - meshName = ""; - relativeDirection = false; - fixed = false; - staticParticleCount = 0; - isVisibleAtNight = true; - isVisibleAtDay = true; - isDaylightAffected = false; - radiusBasedStartenergy = false; - delay = 0; - lifetime = 0; - startTime = 0; - endTime = 1; -} - -void UnitParticleSystemType::load(const XmlNode *particleSystemNode, const string &dir, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath) { - - ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList, - parentLoader,techtreePath); - - //shape - angle= 0.0f; - if(particleSystemNode->hasChild("shape")){ - const XmlNode *shapeNode= particleSystemNode->getChild("shape"); - shape= UnitParticleSystem::strToShape(shapeNode->getAttribute("value")->getRestrictedValue()); - if(shape == UnitParticleSystem::sConical){ - angle= shapeNode->getChild("angle")->getAttribute("value")->getFloatValue(); + // ===================================================== + // class UnitParticleSystemType + // ===================================================== + UnitParticleSystemType::UnitParticleSystemType() : ParticleSystemType() { + shape = UnitParticleSystem::sLinear; + angle = 0; + radius = 0; + minRadius = 0; + emissionRateFade = 0; + relative = false; + meshName = ""; + relativeDirection = false; + fixed = false; + staticParticleCount = 0; + isVisibleAtNight = true; + isVisibleAtDay = true; + isDaylightAffected = false; + radiusBasedStartenergy = false; + delay = 0; + lifetime = 0; + startTime = 0; + endTime = 1; } - } else { - shape = UnitParticleSystem::sLinear; - } - if(shape != UnitParticleSystem::sSpherical){ - //direction - const XmlNode *directionNode= particleSystemNode->getChild("direction"); - direction.x= directionNode->getAttribute("x")->getFloatValue(); - direction.y= directionNode->getAttribute("y")->getFloatValue(); - direction.z= directionNode->getAttribute("z")->getFloatValue(); - if((shape == UnitParticleSystem::sConical) && (0.0 == direction.length())) - throw megaglest_runtime_error("direction cannot be zero"); - // ought to warn about 0 directions generally - } - //emission rate fade - if(particleSystemNode->hasChild("emission-rate-fade")){ - const XmlNode *emissionRateFadeNode= particleSystemNode->getChild("emission-rate-fade"); - emissionRateFade= emissionRateFadeNode->getAttribute("value")->getFloatValue(); - } else { - emissionRateFade = 0; - } - - //radius - const XmlNode *radiusNode= particleSystemNode->getChild("radius"); - radius= radiusNode->getAttribute("value")->getFloatValue(); - - // min radius - if(particleSystemNode->hasChild("min-radius")){ - const XmlNode *minRadiusNode= particleSystemNode->getChild("min-radius"); - minRadius= minRadiusNode->getAttribute("value")->getFloatValue(); - if(minRadius > radius) - throw megaglest_runtime_error("min-radius cannot be bigger than radius"); - } else { - minRadius = 0; - } - if((minRadius == 0) && (shape == UnitParticleSystem::sConical)) { - minRadius = 0.001f; // fudge it so we aren't generating particles that are exactly centred - if(minRadius > radius) - radius = minRadius; - } + void UnitParticleSystemType::load(const XmlNode *particleSystemNode, const string &dir, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath) { - //relative - const XmlNode *relativeNode= particleSystemNode->getChild("relative"); - relative= relativeNode->getAttribute("value")->getBoolValue(); - + ParticleSystemType::load(particleSystemNode, dir, renderer, loadedFileList, + parentLoader, techtreePath); - //meshName - if(particleSystemNode->hasChild("meshName")){ - const XmlNode *tmpNode= particleSystemNode->getChild("meshName"); - meshName= tmpNode->getAttribute("value")->getValue(); - } - else{ - meshName=""; - } - - //relativeDirection - if(particleSystemNode->hasChild("relativeDirection")){ - const XmlNode *relativeDirectionNode= particleSystemNode->getChild("relativeDirection"); - relativeDirection= relativeDirectionNode->getAttribute("value")->getBoolValue(); - } - else{ - relativeDirection=true; - } + //shape + angle = 0.0f; + if (particleSystemNode->hasChild("shape")) { + const XmlNode *shapeNode = particleSystemNode->getChild("shape"); + shape = UnitParticleSystem::strToShape(shapeNode->getAttribute("value")->getRestrictedValue()); + if (shape == UnitParticleSystem::sConical) { + angle = shapeNode->getChild("angle")->getAttribute("value")->getFloatValue(); + } + } else { + shape = UnitParticleSystem::sLinear; + } + if (shape != UnitParticleSystem::sSpherical) { + //direction + const XmlNode *directionNode = particleSystemNode->getChild("direction"); + direction.x = directionNode->getAttribute("x")->getFloatValue(); + direction.y = directionNode->getAttribute("y")->getFloatValue(); + direction.z = directionNode->getAttribute("z")->getFloatValue(); + if ((shape == UnitParticleSystem::sConical) && (0.0 == direction.length())) + throw megaglest_runtime_error("direction cannot be zero"); + // ought to warn about 0 directions generally + } - if(particleSystemNode->hasChild("static-particle-count")){ - //staticParticleCount - const XmlNode *staticParticleCountNode= particleSystemNode->getChild("static-particle-count"); - staticParticleCount= staticParticleCountNode->getAttribute("value")->getIntValue(); - } - else{ - staticParticleCount=0; - } - - //isVisibleAtNight - if(particleSystemNode->hasChild("isVisibleAtNight")){ - const XmlNode *isVisibleAtNightNode= particleSystemNode->getChild("isVisibleAtNight"); - isVisibleAtNight= isVisibleAtNightNode->getAttribute("value")->getBoolValue(); - } - else { - isVisibleAtNight=true; - } + //emission rate fade + if (particleSystemNode->hasChild("emission-rate-fade")) { + const XmlNode *emissionRateFadeNode = particleSystemNode->getChild("emission-rate-fade"); + emissionRateFade = emissionRateFadeNode->getAttribute("value")->getFloatValue(); + } else { + emissionRateFade = 0; + } - //isVisibleAtDay - if(particleSystemNode->hasChild("isVisibleAtDay")){ - const XmlNode *isVisibleAtDayNode= particleSystemNode->getChild("isVisibleAtDay"); - isVisibleAtDay= isVisibleAtDayNode->getAttribute("value")->getBoolValue(); - } - else { - isVisibleAtDay=true; - } + //radius + const XmlNode *radiusNode = particleSystemNode->getChild("radius"); + radius = radiusNode->getAttribute("value")->getFloatValue(); - //isDaylightAffected - if(particleSystemNode->hasChild("isDaylightAffected")){ - const XmlNode *node= particleSystemNode->getChild("isDaylightAffected"); - isDaylightAffected= node->getAttribute("value")->getBoolValue(); - } - else { - isDaylightAffected=false; - } + // min radius + if (particleSystemNode->hasChild("min-radius")) { + const XmlNode *minRadiusNode = particleSystemNode->getChild("min-radius"); + minRadius = minRadiusNode->getAttribute("value")->getFloatValue(); + if (minRadius > radius) + throw megaglest_runtime_error("min-radius cannot be bigger than radius"); + } else { + minRadius = 0; + } + if ((minRadius == 0) && (shape == UnitParticleSystem::sConical)) { + minRadius = 0.001f; // fudge it so we aren't generating particles that are exactly centred + if (minRadius > radius) + radius = minRadius; + } - //radiusBasedStartenergy - if(particleSystemNode->hasChild("radiusBasedStartenergy")){ - const XmlNode *isVisibleAtDayNode= particleSystemNode->getChild("radiusBasedStartenergy"); - radiusBasedStartenergy= isVisibleAtDayNode->getAttribute("value")->getBoolValue(); - } - else{ - radiusBasedStartenergy= true; - } + //relative + const XmlNode *relativeNode = particleSystemNode->getChild("relative"); + relative = relativeNode->getAttribute("value")->getBoolValue(); - //fixed - const XmlNode *fixedNode= particleSystemNode->getChild("fixed"); - fixed= fixedNode->getAttribute("value")->getBoolValue(); - // delay - if(particleSystemNode->hasChild("delay")) { - const XmlNode* delayNode = particleSystemNode->getChild("delay"); - const float delay_secs = delayNode->getAttribute("value")->getFloatValue(); - if(delay_secs < 0) - throw megaglest_runtime_error("particle effect delay cannot be negative"); - delay = (int)delay_secs * GameConstants::updateFps; - } else{ - delay= 0; - } + //meshName + if (particleSystemNode->hasChild("meshName")) { + const XmlNode *tmpNode = particleSystemNode->getChild("meshName"); + meshName = tmpNode->getAttribute("value")->getValue(); + } else { + meshName = ""; + } - // lifetime - if(particleSystemNode->hasChild("lifetime")) { - const XmlNode* lifetimeNode = particleSystemNode->getChild("lifetime"); - const float lifetime_secs = lifetimeNode->getAttribute("value")->getFloatValue(); - if(lifetime_secs < 0 && lifetime_secs != -1) - throw megaglest_runtime_error("particle effect lifetime cannot be negative (-1 means inherited from parent particle)"); - lifetime = (int)lifetime_secs * GameConstants::updateFps; - } else{ - lifetime= -1; //default - } -} + //relativeDirection + if (particleSystemNode->hasChild("relativeDirection")) { + const XmlNode *relativeDirectionNode = particleSystemNode->getChild("relativeDirection"); + relativeDirection = relativeDirectionNode->getAttribute("value")->getBoolValue(); + } else { + relativeDirection = true; + } -ObjectParticleSystemType::ObjectParticleSystemType() : UnitParticleSystemType() { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s Line: %d] NEW [%p]\n",__FUNCTION__,__LINE__,this); -} -ObjectParticleSystemType::~ObjectParticleSystemType() { - if(SystemFlags::VERBOSE_MODE_ENABLED) { - printf("In [%s Line: %d] NEW [%p]\n",__FUNCTION__,__LINE__,this); - //printf("%s\n",PlatformExceptionHandler::getStackTrace().c_str()); - } -} + if (particleSystemNode->hasChild("static-particle-count")) { + //staticParticleCount + const XmlNode *staticParticleCountNode = particleSystemNode->getChild("static-particle-count"); + staticParticleCount = staticParticleCountNode->getAttribute("value")->getIntValue(); + } else { + staticParticleCount = 0; + } -const void UnitParticleSystemType::setValues(UnitParticleSystem *ups){ - // whilst we extend ParticleSystemType we don't use ParticleSystemType::setValues() - // add instances of all children; some settings will cascade to all children - for(Children::iterator i=children.begin(); i!=children.end(); ++i){ - UnitParticleSystem *child = new UnitParticleSystem(); - child->setParticleOwner(ups->getParticleOwner()); - child->setParticleType((*i)); - (*i)->setValues(child); - ups->addChild(child); - } - // set values - ups->setModel(model); - ups->setModelCycle(modelCycle); - ups->setTexture(texture); - ups->setPrimitive(UnitParticleSystem::strToPrimitive(primitive)); - ups->setOffset(offset); - ups->setShape(shape); - ups->setAngle(angle); - ups->setDirection(direction); - ups->setColor(color); - ups->setColorNoEnergy(colorNoEnergy); - ups->setSpeed(speed); - ups->setSpeedUpRelative(speedUpRelative); - ups->setSpeedUpConstant(speedUpConstant); - ups->setGravity(gravity); - ups->setParticleSize(size); - ups->setSizeNoEnergy(sizeNoEnergy); - ups->setEmissionRate(emissionRate); - ups->setMaxParticleEnergy(energyMax); - ups->setVarParticleEnergy(energyVar); - ups->setFixed(fixed); - ups->setRelative(relative); - ups->setMeshName(meshName); - ups->setRelativeDirection(relativeDirection); - ups->setDelay(delay); - ups->setLifetime(lifetime); - ups->setEmissionRateFade(emissionRateFade); - ups->setTeamcolorNoEnergy(teamcolorNoEnergy); - ups->setTeamcolorEnergy(teamcolorEnergy); - ups->setAlternations(alternations); - ups->setParticleSystemStartDelay(particleSystemStartDelay); + //isVisibleAtNight + if (particleSystemNode->hasChild("isVisibleAtNight")) { + const XmlNode *isVisibleAtNightNode = particleSystemNode->getChild("isVisibleAtNight"); + isVisibleAtNight = isVisibleAtNightNode->getAttribute("value")->getBoolValue(); + } else { + isVisibleAtNight = true; + } - ups->setIsVisibleAtNight(isVisibleAtNight); - ups->setIsVisibleAtDay(isVisibleAtDay); - ups->setIsDaylightAffected(isDaylightAffected); - ups->setStaticParticleCount(staticParticleCount); - ups->setRadius(radius); - ups->setMinRadius(minRadius); - ups->setBlendMode(ParticleSystem::strToBlendMode(mode)); - ups->setRadiusBasedStartenergy(radiusBasedStartenergy); - //prepare system for given staticParticleCount - if(staticParticleCount>0) - { - ups->setEmissionRate(0.0f); - ups->setSpeed(0); - direction.x= 0.0f; - direction.y= 0.0f; - direction.z= 0.0f; - ups->setDirection(direction); - } + //isVisibleAtDay + if (particleSystemNode->hasChild("isVisibleAtDay")) { + const XmlNode *isVisibleAtDayNode = particleSystemNode->getChild("isVisibleAtDay"); + isVisibleAtDay = isVisibleAtDayNode->getAttribute("value")->getBoolValue(); + } else { + isVisibleAtDay = true; + } - ups->setStartTime(startTime); - ups->setEndTime(endTime); -} + //isDaylightAffected + if (particleSystemNode->hasChild("isDaylightAffected")) { + const XmlNode *node = particleSystemNode->getChild("isDaylightAffected"); + isDaylightAffected = node->getAttribute("value")->getBoolValue(); + } else { + isDaylightAffected = false; + } -void UnitParticleSystemType::load(const XmlNode *particleFileNode, const string &dir, const string &path, - RendererInterface *renderer, std::map > > &loadedFileList, - string parentLoader, string techtreePath) { + //radiusBasedStartenergy + if (particleSystemNode->hasChild("radiusBasedStartenergy")) { + const XmlNode *isVisibleAtDayNode = particleSystemNode->getChild("radiusBasedStartenergy"); + radiusBasedStartenergy = isVisibleAtDayNode->getAttribute("value")->getBoolValue(); + } else { + radiusBasedStartenergy = true; + } - try{ - XmlTree xmlTree; + //fixed + const XmlNode *fixedNode = particleSystemNode->getChild("fixed"); + fixed = fixedNode->getAttribute("value")->getBoolValue(); - std::map mapExtraTagReplacementValues; - mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; - xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); - loadedFileList[path].push_back(make_pair(parentLoader,parentLoader)); - const XmlNode *particleSystemNode= xmlTree.getRootNode(); - - if(particleFileNode){ - // immediate children in the particleFileNode will override the particleSystemNode - particleFileNode->setSuper(particleSystemNode); - particleSystemNode= particleFileNode; + // delay + if (particleSystemNode->hasChild("delay")) { + const XmlNode* delayNode = particleSystemNode->getChild("delay"); + const float delay_secs = delayNode->getAttribute("value")->getFloatValue(); + if (delay_secs < 0) + throw megaglest_runtime_error("particle effect delay cannot be negative"); + delay = (int) delay_secs * GameConstants::updateFps; + } else { + delay = 0; + } + + // lifetime + if (particleSystemNode->hasChild("lifetime")) { + const XmlNode* lifetimeNode = particleSystemNode->getChild("lifetime"); + const float lifetime_secs = lifetimeNode->getAttribute("value")->getFloatValue(); + if (lifetime_secs < 0 && lifetime_secs != -1) + throw megaglest_runtime_error("particle effect lifetime cannot be negative (-1 means inherited from parent particle)"); + lifetime = (int) lifetime_secs * GameConstants::updateFps; + } else { + lifetime = -1; //default + } } - - UnitParticleSystemType::load(particleSystemNode, dir, renderer, - loadedFileList, parentLoader, techtreePath); + + ObjectParticleSystemType::ObjectParticleSystemType() : UnitParticleSystemType() { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s Line: %d] NEW [%p]\n", __FUNCTION__, __LINE__, this); + } + ObjectParticleSystemType::~ObjectParticleSystemType() { + if (SystemFlags::VERBOSE_MODE_ENABLED) { + printf("In [%s Line: %d] NEW [%p]\n", __FUNCTION__, __LINE__, this); + //printf("%s\n",PlatformExceptionHandler::getStackTrace().c_str()); + } + } + + const void UnitParticleSystemType::setValues(UnitParticleSystem *ups) { + // whilst we extend ParticleSystemType we don't use ParticleSystemType::setValues() + // add instances of all children; some settings will cascade to all children + for (Children::iterator i = children.begin(); i != children.end(); ++i) { + UnitParticleSystem *child = new UnitParticleSystem(); + child->setParticleOwner(ups->getParticleOwner()); + child->setParticleType((*i)); + (*i)->setValues(child); + ups->addChild(child); + } + // set values + ups->setModel(model); + ups->setModelCycle(modelCycle); + ups->setTexture(texture); + ups->setPrimitive(UnitParticleSystem::strToPrimitive(primitive)); + ups->setOffset(offset); + ups->setShape(shape); + ups->setAngle(angle); + ups->setDirection(direction); + ups->setColor(color); + ups->setColorNoEnergy(colorNoEnergy); + ups->setSpeed(speed); + ups->setSpeedUpRelative(speedUpRelative); + ups->setSpeedUpConstant(speedUpConstant); + ups->setGravity(gravity); + ups->setParticleSize(size); + ups->setSizeNoEnergy(sizeNoEnergy); + ups->setEmissionRate(emissionRate); + ups->setMaxParticleEnergy(energyMax); + ups->setVarParticleEnergy(energyVar); + ups->setFixed(fixed); + ups->setRelative(relative); + ups->setMeshName(meshName); + ups->setRelativeDirection(relativeDirection); + ups->setDelay(delay); + ups->setLifetime(lifetime); + ups->setEmissionRateFade(emissionRateFade); + ups->setTeamcolorNoEnergy(teamcolorNoEnergy); + ups->setTeamcolorEnergy(teamcolorEnergy); + ups->setAlternations(alternations); + ups->setParticleSystemStartDelay(particleSystemStartDelay); + + ups->setIsVisibleAtNight(isVisibleAtNight); + ups->setIsVisibleAtDay(isVisibleAtDay); + ups->setIsDaylightAffected(isDaylightAffected); + ups->setStaticParticleCount(staticParticleCount); + ups->setRadius(radius); + ups->setMinRadius(minRadius); + ups->setBlendMode(ParticleSystem::strToBlendMode(mode)); + ups->setRadiusBasedStartenergy(radiusBasedStartenergy); + //prepare system for given staticParticleCount + if (staticParticleCount > 0) { + ups->setEmissionRate(0.0f); + ups->setSpeed(0); + direction.x = 0.0f; + direction.y = 0.0f; + direction.z = 0.0f; + ups->setDirection(direction); + } + + ups->setStartTime(startTime); + ups->setEndTime(endTime); + } + + void UnitParticleSystemType::load(const XmlNode *particleFileNode, const string &dir, const string &path, + RendererInterface *renderer, std::map > > &loadedFileList, + string parentLoader, string techtreePath) { + + try { + XmlTree xmlTree; + + std::map mapExtraTagReplacementValues; + mapExtraTagReplacementValues["$COMMONDATAPATH"] = techtreePath + "/commondata/"; + xmlTree.load(path, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); + loadedFileList[path].push_back(make_pair(parentLoader, parentLoader)); + const XmlNode *particleSystemNode = xmlTree.getRootNode(); + + if (particleFileNode) { + // immediate children in the particleFileNode will override the particleSystemNode + particleFileNode->setSuper(particleSystemNode); + particleSystemNode = particleFileNode; + } + + UnitParticleSystemType::load(particleSystemNode, dir, renderer, + loadedFileList, parentLoader, techtreePath); + } catch (const exception &e) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, e.what()); + throw megaglest_runtime_error("Error loading ParticleSystem: " + path + "\n" + e.what()); + } + } + + void UnitParticleSystemType::loadGame(const XmlNode *rootNode) { + ParticleSystemType::loadGame(rootNode); + + const XmlNode *unitParticleSystemTypeNode = rootNode->getChild("UnitParticleSystemType"); + + shape = static_cast(unitParticleSystemTypeNode->getAttribute("shape")->getIntValue()); + angle = unitParticleSystemTypeNode->getAttribute("angle")->getFloatValue(); + radius = unitParticleSystemTypeNode->getAttribute("radius")->getFloatValue(); + minRadius = unitParticleSystemTypeNode->getAttribute("minRadius")->getFloatValue(); + emissionRateFade = unitParticleSystemTypeNode->getAttribute("emissionRateFade")->getFloatValue(); + direction = Vec3f::strToVec3(unitParticleSystemTypeNode->getAttribute("direction")->getValue()); + relative = (unitParticleSystemTypeNode->getAttribute("relative")->getIntValue() != 0); + if (unitParticleSystemTypeNode->hasAttribute("meshName")) { + meshName = unitParticleSystemTypeNode->getAttribute("meshName")->getValue(); + } else { + meshName = ""; + } + relativeDirection = (unitParticleSystemTypeNode->getAttribute("relativeDirection")->getIntValue() != 0); + fixed = (unitParticleSystemTypeNode->getAttribute("fixed")->getIntValue() != 0); + staticParticleCount = unitParticleSystemTypeNode->getAttribute("staticParticleCount")->getIntValue(); + isVisibleAtNight = (unitParticleSystemTypeNode->getAttribute("isVisibleAtNight")->getIntValue() != 0); + isVisibleAtDay = (unitParticleSystemTypeNode->getAttribute("isVisibleAtDay")->getIntValue() != 0); + isDaylightAffected = (unitParticleSystemTypeNode->getAttribute("isDaylightAffected")->getIntValue() != 0); + radiusBasedStartenergy = (unitParticleSystemTypeNode->getAttribute("radiusBasedStartenergy")->getIntValue() != 0); + delay = unitParticleSystemTypeNode->getAttribute("delay")->getIntValue(); + lifetime = unitParticleSystemTypeNode->getAttribute("lifetime")->getIntValue(); + startTime = unitParticleSystemTypeNode->getAttribute("startTime")->getFloatValue(); + endTime = unitParticleSystemTypeNode->getAttribute("endTime")->getFloatValue(); + } + + void UnitParticleSystemType::saveGame(XmlNode *rootNode) { + ParticleSystemType::saveGame(rootNode); + + std::map mapTagReplacements; + XmlNode *unitParticleSystemTypeNode = rootNode->addChild("UnitParticleSystemType"); + + // UnitParticleSystem::Shape shape; + unitParticleSystemTypeNode->addAttribute("shape", intToStr(shape), mapTagReplacements); + // float angle; + unitParticleSystemTypeNode->addAttribute("angle", floatToStr(angle, 6), mapTagReplacements); + // float radius; + unitParticleSystemTypeNode->addAttribute("radius", floatToStr(radius, 6), mapTagReplacements); + // float minRadius; + unitParticleSystemTypeNode->addAttribute("minRadius", floatToStr(minRadius, 6), mapTagReplacements); + // float emissionRateFade; + unitParticleSystemTypeNode->addAttribute("emissionRateFade", floatToStr(emissionRateFade, 6), mapTagReplacements); + // Vec3f direction; + unitParticleSystemTypeNode->addAttribute("direction", direction.getString(), mapTagReplacements); + // bool relative; + unitParticleSystemTypeNode->addAttribute("relative", intToStr(relative), mapTagReplacements); + // string meshName; + unitParticleSystemTypeNode->addAttribute("meshName", meshName, mapTagReplacements); + // bool relativeDirection; + unitParticleSystemTypeNode->addAttribute("relativeDirection", intToStr(relativeDirection), mapTagReplacements); + // bool fixed; + unitParticleSystemTypeNode->addAttribute("fixed", intToStr(fixed), mapTagReplacements); + // int staticParticleCount; + unitParticleSystemTypeNode->addAttribute("staticParticleCount", intToStr(staticParticleCount), mapTagReplacements); + // bool isVisibleAtNight; + unitParticleSystemTypeNode->addAttribute("isVisibleAtNight", intToStr(isVisibleAtNight), mapTagReplacements); + // bool isVisibleAtDay; + unitParticleSystemTypeNode->addAttribute("isVisibleAtDay", intToStr(isVisibleAtDay), mapTagReplacements); + // bool isDaylightAffected; + unitParticleSystemTypeNode->addAttribute("isDaylightAffected", intToStr(isDaylightAffected), mapTagReplacements); + // bool radiusBasedStartenergy; + unitParticleSystemTypeNode->addAttribute("radiusBasedStartenergy", intToStr(radiusBasedStartenergy), mapTagReplacements); + // int delay; + unitParticleSystemTypeNode->addAttribute("delay", intToStr(delay), mapTagReplacements); + // int lifetime; + unitParticleSystemTypeNode->addAttribute("lifetime", intToStr(lifetime), mapTagReplacements); + // float startTime; + unitParticleSystemTypeNode->addAttribute("startTime", floatToStr(startTime, 6), mapTagReplacements); + // float endTime; + unitParticleSystemTypeNode->addAttribute("endTime", floatToStr(endTime, 6), mapTagReplacements); + } + } - catch(const exception &e){ - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,e.what()); - throw megaglest_runtime_error("Error loading ParticleSystem: "+ path + "\n" +e.what()); - } -} - -void UnitParticleSystemType::loadGame(const XmlNode *rootNode) { - ParticleSystemType::loadGame(rootNode); - - const XmlNode *unitParticleSystemTypeNode = rootNode->getChild("UnitParticleSystemType"); - - shape = static_cast(unitParticleSystemTypeNode->getAttribute("shape")->getIntValue()); - angle = unitParticleSystemTypeNode->getAttribute("angle")->getFloatValue(); - radius = unitParticleSystemTypeNode->getAttribute("radius")->getFloatValue(); - minRadius = unitParticleSystemTypeNode->getAttribute("minRadius")->getFloatValue(); - emissionRateFade = unitParticleSystemTypeNode->getAttribute("emissionRateFade")->getFloatValue(); - direction = Vec3f::strToVec3(unitParticleSystemTypeNode->getAttribute("direction")->getValue()); - relative = (unitParticleSystemTypeNode->getAttribute("relative")->getIntValue() != 0); - if(unitParticleSystemTypeNode->hasAttribute("meshName")){ - meshName = unitParticleSystemTypeNode->getAttribute("meshName")->getValue();} - else { - meshName = ""; - } - relativeDirection = (unitParticleSystemTypeNode->getAttribute("relativeDirection")->getIntValue() != 0); - fixed = (unitParticleSystemTypeNode->getAttribute("fixed")->getIntValue() != 0); - staticParticleCount = unitParticleSystemTypeNode->getAttribute("staticParticleCount")->getIntValue(); - isVisibleAtNight = (unitParticleSystemTypeNode->getAttribute("isVisibleAtNight")->getIntValue() != 0); - isVisibleAtDay = (unitParticleSystemTypeNode->getAttribute("isVisibleAtDay")->getIntValue() != 0); - isDaylightAffected = (unitParticleSystemTypeNode->getAttribute("isDaylightAffected")->getIntValue() != 0); - radiusBasedStartenergy = (unitParticleSystemTypeNode->getAttribute("radiusBasedStartenergy")->getIntValue() != 0); - delay = unitParticleSystemTypeNode->getAttribute("delay")->getIntValue(); - lifetime = unitParticleSystemTypeNode->getAttribute("lifetime")->getIntValue(); - startTime = unitParticleSystemTypeNode->getAttribute("startTime")->getFloatValue(); - endTime = unitParticleSystemTypeNode->getAttribute("endTime")->getFloatValue(); -} - -void UnitParticleSystemType::saveGame(XmlNode *rootNode) { - ParticleSystemType::saveGame(rootNode); - - std::map mapTagReplacements; - XmlNode *unitParticleSystemTypeNode = rootNode->addChild("UnitParticleSystemType"); - -// UnitParticleSystem::Shape shape; - unitParticleSystemTypeNode->addAttribute("shape",intToStr(shape), mapTagReplacements); -// float angle; - unitParticleSystemTypeNode->addAttribute("angle",floatToStr(angle,6), mapTagReplacements); -// float radius; - unitParticleSystemTypeNode->addAttribute("radius",floatToStr(radius,6), mapTagReplacements); -// float minRadius; - unitParticleSystemTypeNode->addAttribute("minRadius",floatToStr(minRadius,6), mapTagReplacements); -// float emissionRateFade; - unitParticleSystemTypeNode->addAttribute("emissionRateFade",floatToStr(emissionRateFade,6), mapTagReplacements); -// Vec3f direction; - unitParticleSystemTypeNode->addAttribute("direction",direction.getString(), mapTagReplacements); -// bool relative; - unitParticleSystemTypeNode->addAttribute("relative",intToStr(relative), mapTagReplacements); -// string meshName; - unitParticleSystemTypeNode->addAttribute("meshName",meshName, mapTagReplacements); -// bool relativeDirection; - unitParticleSystemTypeNode->addAttribute("relativeDirection",intToStr(relativeDirection), mapTagReplacements); -// bool fixed; - unitParticleSystemTypeNode->addAttribute("fixed",intToStr(fixed), mapTagReplacements); -// int staticParticleCount; - unitParticleSystemTypeNode->addAttribute("staticParticleCount",intToStr(staticParticleCount), mapTagReplacements); -// bool isVisibleAtNight; - unitParticleSystemTypeNode->addAttribute("isVisibleAtNight",intToStr(isVisibleAtNight), mapTagReplacements); -// bool isVisibleAtDay; - unitParticleSystemTypeNode->addAttribute("isVisibleAtDay",intToStr(isVisibleAtDay), mapTagReplacements); -// bool isDaylightAffected; - unitParticleSystemTypeNode->addAttribute("isDaylightAffected",intToStr(isDaylightAffected), mapTagReplacements); -// bool radiusBasedStartenergy; - unitParticleSystemTypeNode->addAttribute("radiusBasedStartenergy",intToStr(radiusBasedStartenergy), mapTagReplacements); -// int delay; - unitParticleSystemTypeNode->addAttribute("delay",intToStr(delay), mapTagReplacements); -// int lifetime; - unitParticleSystemTypeNode->addAttribute("lifetime",intToStr(lifetime), mapTagReplacements); -// float startTime; - unitParticleSystemTypeNode->addAttribute("startTime",floatToStr(startTime,6), mapTagReplacements); -// float endTime; - unitParticleSystemTypeNode->addAttribute("endTime",floatToStr(endTime,6), mapTagReplacements); -} - -}}//end mamespace +}//end mamespace diff --git a/source/glest_game/graphics/unit_particle_type.h b/source/glest_game/graphics/unit_particle_type.h index 159d96a87..2c5582e59 100644 --- a/source/glest_game/graphics/unit_particle_type.h +++ b/source/glest_game/graphics/unit_particle_type.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_UNITPARTICLETYPE_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -32,74 +32,87 @@ using std::string; using namespace Shared::Graphics; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -using Shared::Graphics::ParticleManager; -using Shared::Graphics::ParticleSystem; -using Shared::Graphics::UnitParticleSystem; -using Shared::Graphics::Texture2D; -using Shared::Graphics::Vec3f; -using Shared::Graphics::Vec4f; -using Shared::Util::MultiFactory; -using Shared::Xml::XmlNode; + using Shared::Graphics::ParticleManager; + using Shared::Graphics::ParticleSystem; + using Shared::Graphics::UnitParticleSystem; + using Shared::Graphics::Texture2D; + using Shared::Graphics::Vec3f; + using Shared::Graphics::Vec4f; + using Shared::Util::MultiFactory; + using Shared::Xml::XmlNode; -// =========================================================== -// class ParticleSystemType -// -/// A type of particle system -// =========================================================== + // =========================================================== + // class ParticleSystemType + // + /// A type of particle system + // =========================================================== -class UnitParticleSystemType: public ParticleSystemType { -protected: - UnitParticleSystem::Shape shape; - float angle; - float radius; - float minRadius; - float emissionRateFade; - Vec3f direction; - bool relative; - string meshName; - bool relativeDirection; - bool fixed; - int staticParticleCount; - bool isVisibleAtNight; - bool isVisibleAtDay; - bool isDaylightAffected; - bool radiusBasedStartenergy; - int delay; - int lifetime; - float startTime; - float endTime; + class UnitParticleSystemType : public ParticleSystemType { + protected: + UnitParticleSystem::Shape shape; + float angle; + float radius; + float minRadius; + float emissionRateFade; + Vec3f direction; + bool relative; + string meshName; + bool relativeDirection; + bool fixed; + int staticParticleCount; + bool isVisibleAtNight; + bool isVisibleAtDay; + bool isDaylightAffected; + bool radiusBasedStartenergy; + int delay; + int lifetime; + float startTime; + float endTime; -public: - UnitParticleSystemType(); - virtual ~UnitParticleSystemType() {}; + public: + UnitParticleSystemType(); + virtual ~UnitParticleSystemType() { + }; - void load(const XmlNode *particleSystemNode, const string &dir, - RendererInterface *newTexture, std::map > > &loadedFileList, - string parentLoader, string techtreePath); - void load(const XmlNode *particleFileNode, const string &dir, const string &path, RendererInterface *newTexture, - std::map > > &loadedFileList,string parentLoader, - string techtreePath); + void load(const XmlNode *particleSystemNode, const string &dir, + RendererInterface *newTexture, std::map > > &loadedFileList, + string parentLoader, string techtreePath); + void load(const XmlNode *particleFileNode, const string &dir, const string &path, RendererInterface *newTexture, + std::map > > &loadedFileList, string parentLoader, + string techtreePath); - void setStartTime(float startTime) { this->startTime = startTime; } - float getStartTime() const { return this->startTime; } - void setEndTime(float endTime) { this->endTime = endTime; } - float getEndTime() const { return this->endTime; } + void setStartTime(float startTime) { + this->startTime = startTime; + } + float getStartTime() const { + return this->startTime; + } + void setEndTime(float endTime) { + this->endTime = endTime; + } + float getEndTime() const { + return this->endTime; + } - const void setValues (UnitParticleSystem *uts); - bool hasTexture() const { return(texture != NULL); } - virtual void saveGame(XmlNode *rootNode); - virtual void loadGame(const XmlNode *rootNode); -}; + const void setValues(UnitParticleSystem *uts); + bool hasTexture() const { + return(texture != NULL); + } + virtual void saveGame(XmlNode *rootNode); + virtual void loadGame(const XmlNode *rootNode); + }; -class ObjectParticleSystemType: public UnitParticleSystemType { -public: - ObjectParticleSystemType(); - virtual ~ObjectParticleSystemType(); -}; + class ObjectParticleSystemType : public UnitParticleSystemType { + public: + ObjectParticleSystemType(); + virtual ~ObjectParticleSystemType(); + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/gui/display.cpp b/source/glest_game/gui/display.cpp index e66fdf74c..0884534a0 100644 --- a/source/glest_game/gui/display.cpp +++ b/source/glest_game/gui/display.cpp @@ -19,166 +19,167 @@ using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Display -// ===================================================== + // ===================================================== + // class Display + // ===================================================== -Display::Display(){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + Display::Display() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - colors[0]= Vec4f(1.f, 1.f, 1.f, 0.0f); - colors[1]= Vec4f(1.f, 0.5f, 0.5f, 0.0f); - colors[2]= Vec4f(0.5f, 0.5f, 1.0f, 0.0f); - colors[3]= Vec4f(0.5f, 1.0f, 0.5f, 0.0f); - colors[4]= Vec4f(0.0f, 0.0f, 0.0f, 1.0f); - colors[5]= Vec4f(0.0f, 0.0f, 1.0f, 1.0f); - colors[6]= Vec4f(1.0f, 0.0f, 0.0f, 1.0f); - colors[7]= Vec4f(0.0f, 1.0f, 0.0f, 1.0f); - colors[8]= Vec4f(1.0f, 1.0f, 1.0f, 1.0f); + colors[0] = Vec4f(1.f, 1.f, 1.f, 0.0f); + colors[1] = Vec4f(1.f, 0.5f, 0.5f, 0.0f); + colors[2] = Vec4f(0.5f, 0.5f, 1.0f, 0.0f); + colors[3] = Vec4f(0.5f, 1.0f, 0.5f, 0.0f); + colors[4] = Vec4f(0.0f, 0.0f, 0.0f, 1.0f); + colors[5] = Vec4f(0.0f, 0.0f, 1.0f, 1.0f); + colors[6] = Vec4f(1.0f, 0.0f, 0.0f, 1.0f); + colors[7] = Vec4f(0.0f, 1.0f, 0.0f, 1.0f); + colors[8] = Vec4f(1.0f, 1.0f, 1.0f, 1.0f); - currentColor= 0; - clear(); + currentColor = 0; + clear(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - -void Display::calculateUpDimensions(int index) { - if(index>maxUpIndex){ - maxUpIndex=index; - if(maxUpIndex+1>upCellSideCount*upCellSideCount){ - upCellSideCount=upCellSideCount+1; - upImageSize = static_cast(imageSize) * static_cast(cellSideCount) / static_cast(upCellSideCount) + 0.9f; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); } + + void Display::calculateUpDimensions(int index) { + if (index > maxUpIndex) { + maxUpIndex = index; + if (maxUpIndex + 1 > upCellSideCount*upCellSideCount) { + upCellSideCount = upCellSideCount + 1; + upImageSize = static_cast(imageSize) * static_cast(cellSideCount) / static_cast(upCellSideCount) + 0.9f; + } + } + } + + Vec4f Display::getColor() const { + if (currentColor < 0 || currentColor >= colorCount) { + throw megaglest_runtime_error("currentColor >= colorCount"); + } + return colors[currentColor]; + } + + void Display::setUpImage(int i, const Texture2D *image) { + if (i >= upCellCount) throw megaglest_runtime_error("i>=upCellCount in Display::setUpImage"); + upImages[i] = image; + calculateUpDimensions(i); + } + + //misc + void Display::clear() { + downSelectedPos = invalidPos; + for (int i = 0; i < upCellCount; ++i) { + upImages[i] = NULL; + } + + for (int i = 0; i < downCellCount; ++i) { + downImages[i] = NULL; + downLighted[i] = true; + commandTypes[i] = NULL; + commandClasses[i] = ccNull; + } + + title.clear(); + text.clear(); + progressBar = -1; + + upCellSideCount = cellSideCount; + upImageSize = imageSize; + maxUpIndex = 0; + } + void Display::switchColor() { + currentColor = (currentColor + 1) % colorCount; + } + + int Display::computeDownIndex(int x, int y) const { + y = y - (downY - cellSideCount * imageSize); + + if (y > imageSize*cellSideCount || y < 0) { + return invalidPos; + } + + int cellX = x / imageSize; + int cellY = (y / imageSize) % cellSideCount; + int index = (cellSideCount - cellY - 1)*cellSideCount + cellX;; + + if (index < 0 || index >= downCellCount || downImages[index] == NULL) { + index = invalidPos; + } + + return index; + } + + int Display::computeDownX(int index) const { + return (index % cellSideCount) * imageSize; + } + + int Display::computeDownY(int index) const { + return Display::downY - (index / cellSideCount)*imageSize - imageSize; + } + + int Display::computeUpX(int index) const { + return (index % upCellSideCount) * upImageSize; + } + + int Display::computeUpY(int index) const { + return Metrics::getInstance().getDisplayH() - (index / upCellSideCount)*upImageSize - upImageSize; + } + + void Display::saveGame(XmlNode *rootNode) const { + std::map mapTagReplacements; + XmlNode *displayNode = rootNode->addChild("Display"); + + // string title; + displayNode->addAttribute("title", title, mapTagReplacements); + // string text; + displayNode->addAttribute("text", text, mapTagReplacements); + // string infoText; + displayNode->addAttribute("infoText", infoText, mapTagReplacements); + // const Texture2D *upImages[upCellCount]; + // const Texture2D *downImages[downCellCount]; + // bool downLighted[downCellCount]; + // const CommandType *commandTypes[downCellCount]; + // CommandClass commandClasses[downCellCount]; + // int progressBar; + displayNode->addAttribute("progressBar", intToStr(progressBar), mapTagReplacements); + // int downSelectedPos; + displayNode->addAttribute("downSelectedPos", intToStr(downSelectedPos), mapTagReplacements); + // Vec4f colors[colorCount]; + // int currentColor; + displayNode->addAttribute("currentColor", intToStr(currentColor), mapTagReplacements); + // int upCellSideCount; + // int upImageSize; + // int maxUpIndex; + } + + void Display::loadGame(const XmlNode *rootNode) { + const XmlNode *displayNode = rootNode->getChild("Display"); + + // string title; + title = displayNode->getAttribute("title")->getValue(); + // string text; + text = displayNode->getAttribute("text")->getValue(); + // string infoText; + infoText = displayNode->getAttribute("infoText")->getValue(); + // const Texture2D *upImages[upCellCount]; + // const Texture2D *downImages[downCellCount]; + // bool downLighted[downCellCount]; + // const CommandType *commandTypes[downCellCount]; + // CommandClass commandClasses[downCellCount]; + // int progressBar; + progressBar = displayNode->getAttribute("progressBar")->getIntValue(); + // int downSelectedPos; + //displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements); + // Vec4f colors[colorCount]; + // int currentColor; + //currentColor = displayNode->getAttribute("progressBar")->getIntValue(); + // int upCellSideCount; + // int upImageSize; + // int maxUpIndex; + } + } -} - -Vec4f Display::getColor() const { - if(currentColor < 0 || currentColor >= colorCount) { - throw megaglest_runtime_error("currentColor >= colorCount"); - } - return colors[currentColor]; -} - -void Display::setUpImage(int i, const Texture2D *image) -{ - if(i>=upCellCount) throw megaglest_runtime_error("i>=upCellCount in Display::setUpImage"); - upImages[i]= image; - calculateUpDimensions(i); -} - -//misc -void Display::clear(){ - downSelectedPos= invalidPos; - for(int i=0; iimageSize*cellSideCount || y < 0){ - return invalidPos; - } - - int cellX= x/imageSize; - int cellY= (y/imageSize) % cellSideCount; - int index= (cellSideCount-cellY-1)*cellSideCount+cellX;; - - if(index<0 || index>=downCellCount || downImages[index]==NULL){ - index= invalidPos; - } - - return index; -} - -int Display::computeDownX(int index) const{ - return (index % cellSideCount) * imageSize; -} - -int Display::computeDownY(int index) const{ - return Display::downY - (index/cellSideCount)*imageSize - imageSize; -} - -int Display::computeUpX(int index) const{ - return (index % upCellSideCount) * upImageSize; -} - -int Display::computeUpY(int index) const{ - return Metrics::getInstance().getDisplayH() - (index/upCellSideCount)*upImageSize - upImageSize; -} - -void Display::saveGame(XmlNode *rootNode) const { - std::map mapTagReplacements; - XmlNode *displayNode = rootNode->addChild("Display"); - -// string title; - displayNode->addAttribute("title",title, mapTagReplacements); -// string text; - displayNode->addAttribute("text",text, mapTagReplacements); -// string infoText; - displayNode->addAttribute("infoText",infoText, mapTagReplacements); -// const Texture2D *upImages[upCellCount]; -// const Texture2D *downImages[downCellCount]; -// bool downLighted[downCellCount]; -// const CommandType *commandTypes[downCellCount]; -// CommandClass commandClasses[downCellCount]; -// int progressBar; - displayNode->addAttribute("progressBar",intToStr(progressBar), mapTagReplacements); -// int downSelectedPos; - displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements); -// Vec4f colors[colorCount]; -// int currentColor; - displayNode->addAttribute("currentColor",intToStr(currentColor), mapTagReplacements); -// int upCellSideCount; -// int upImageSize; -// int maxUpIndex; -} - -void Display::loadGame(const XmlNode *rootNode) { - const XmlNode *displayNode = rootNode->getChild("Display"); - - // string title; - title = displayNode->getAttribute("title")->getValue(); - // string text; - text = displayNode->getAttribute("text")->getValue(); - // string infoText; - infoText = displayNode->getAttribute("infoText")->getValue(); - // const Texture2D *upImages[upCellCount]; - // const Texture2D *downImages[downCellCount]; - // bool downLighted[downCellCount]; - // const CommandType *commandTypes[downCellCount]; - // CommandClass commandClasses[downCellCount]; - // int progressBar; - progressBar = displayNode->getAttribute("progressBar")->getIntValue(); - // int downSelectedPos; - //displayNode->addAttribute("downSelectedPos",intToStr(downSelectedPos), mapTagReplacements); - // Vec4f colors[colorCount]; - // int currentColor; - //currentColor = displayNode->getAttribute("progressBar")->getIntValue(); - // int upCellSideCount; - // int upImageSize; - // int maxUpIndex; -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/gui/display.h b/source/glest_game/gui/display.h index 257eb5169..94393c5cc 100644 --- a/source/glest_game/gui/display.h +++ b/source/glest_game/gui/display.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_DISPLAY_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -30,90 +30,134 @@ using Shared::Graphics::Texture2D; using Shared::Graphics::Vec4f; using Shared::Util::replaceBy; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Display -// -/// Display for unit commands, and unit selection -// ===================================================== + // ===================================================== + // class Display + // + /// Display for unit commands, and unit selection + // ===================================================== -class Display{ -public: - static const int cellSideCount= 4; - static const int upCellCount= 36; - static const int downCellCount= cellSideCount*cellSideCount; - static const int colorCount= 9; - static const int imageSize= 32; - static const int invalidPos= -1; - static const int downY = imageSize*9; - static const int infoStringY= imageSize*4; + class Display { + public: + static const int cellSideCount = 4; + static const int upCellCount = 36; + static const int downCellCount = cellSideCount * cellSideCount; + static const int colorCount = 9; + static const int imageSize = 32; + static const int invalidPos = -1; + static const int downY = imageSize * 9; + static const int infoStringY = imageSize * 4; -private: - string title; - string text; - string infoText; - const Texture2D *upImages[upCellCount]; - const Texture2D *downImages[downCellCount]; - bool downLighted[downCellCount]; - const CommandType *commandTypes[downCellCount]; - CommandClass commandClasses[downCellCount]; - int progressBar; - int downSelectedPos; - Vec4f colors[colorCount]; - int currentColor; + private: + string title; + string text; + string infoText; + const Texture2D *upImages[upCellCount]; + const Texture2D *downImages[downCellCount]; + bool downLighted[downCellCount]; + const CommandType *commandTypes[downCellCount]; + CommandClass commandClasses[downCellCount]; + int progressBar; + int downSelectedPos; + Vec4f colors[colorCount]; + int currentColor; - int upCellSideCount; - int upImageSize; - int maxUpIndex; - + int upCellSideCount; + int upImageSize; + int maxUpIndex; -public: - Display(); - //get - string getTitle() const {return title;} - string getText() const {return text;} - string getInfoText() const {return infoText;} - const Texture2D *getUpImage(int index) const {return upImages[index];} - const Texture2D *getDownImage(int index) const {return downImages[index];} - bool getDownLighted(int index) const {return downLighted[index];} - const CommandType *getCommandType(int i) const {return commandTypes[i];} - CommandClass getCommandClass(int i) const {return commandClasses[i];} - Vec4f getColor() const; - int getProgressBar() const {return progressBar;} - int getDownSelectedPos() const {return downSelectedPos;} - int getUpCellSideCount() const {return upCellSideCount;} - int getUpImageSize() const {return upImageSize;} - - //set - void setTitle(const string title) {this->title= formatString(title);} - void setText(const string &text) {this->text= formatString(text);} - void setInfoText(const string infoText) {this->infoText= formatString(infoText);} - void setUpImage(int i, const Texture2D *image); - void setDownImage(int i, const Texture2D *image) {downImages[i]= image;} - void setCommandType(int i, const CommandType *ct) {commandTypes[i]= ct;} - void setCommandClass(int i, const CommandClass cc) {commandClasses[i]= cc;} - void setDownLighted(int i, bool lighted) {downLighted[i]= lighted;} - void setProgressBar(int i) {progressBar= i;} - void setDownSelectedPos(int i) {downSelectedPos= i;} - - //misc - void clear(); - void switchColor(); - int computeDownIndex(int x, int y) const; - int computeDownX(int index) const; - int computeDownY(int index) const; - int computeUpX(int index) const; - int computeUpY(int index) const; + public: + Display(); - void saveGame(XmlNode *rootNode) const; - void loadGame(const XmlNode *rootNode); + //get + string getTitle() const { + return title; + } + string getText() const { + return text; + } + string getInfoText() const { + return infoText; + } + const Texture2D *getUpImage(int index) const { + return upImages[index]; + } + const Texture2D *getDownImage(int index) const { + return downImages[index]; + } + bool getDownLighted(int index) const { + return downLighted[index]; + } + const CommandType *getCommandType(int i) const { + return commandTypes[i]; + } + CommandClass getCommandClass(int i) const { + return commandClasses[i]; + } + Vec4f getColor() const; + int getProgressBar() const { + return progressBar; + } + int getDownSelectedPos() const { + return downSelectedPos; + } + int getUpCellSideCount() const { + return upCellSideCount; + } + int getUpImageSize() const { + return upImageSize; + } -private: - void calculateUpDimensions(int index); -}; + //set + void setTitle(const string title) { + this->title = formatString(title); + } + void setText(const string &text) { + this->text = formatString(text); + } + void setInfoText(const string infoText) { + this->infoText = formatString(infoText); + } + void setUpImage(int i, const Texture2D *image); + void setDownImage(int i, const Texture2D *image) { + downImages[i] = image; + } + void setCommandType(int i, const CommandType *ct) { + commandTypes[i] = ct; + } + void setCommandClass(int i, const CommandClass cc) { + commandClasses[i] = cc; + } + void setDownLighted(int i, bool lighted) { + downLighted[i] = lighted; + } + void setProgressBar(int i) { + progressBar = i; + } + void setDownSelectedPos(int i) { + downSelectedPos = i; + } -}}//end namespace + //misc + void clear(); + void switchColor(); + int computeDownIndex(int x, int y) const; + int computeDownX(int index) const; + int computeDownY(int index) const; + int computeUpX(int index) const; + int computeUpY(int index) const; + + void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode); + + private: + void calculateUpDimensions(int index); + }; + + } +}//end namespace #endif diff --git a/source/glest_game/gui/gui.cpp b/source/glest_game/gui/gui.cpp index 60e390921..f91f292d2 100644 --- a/source/glest_game/gui/gui.cpp +++ b/source/glest_game/gui/gui.cpp @@ -35,1370 +35,1321 @@ using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Mouse3d -// ===================================================== + // ===================================================== + // class Mouse3d + // ===================================================== -const float Mouse3d::fadeSpeed= 1.f/50.f; + const float Mouse3d::fadeSpeed = 1.f / 50.f; -static const int queueCommandKey = vkShift; + static const int queueCommandKey = vkShift; -Mouse3d::Mouse3d(){ - enabled= false; - rot= 0; - fade= 0.f; -} - -void Mouse3d::enable(){ - enabled= true; - fade= 0.f; -} - -void Mouse3d::update(){ - if(enabled){ - rot= (rot + 3) % 360; - fade+= fadeSpeed; - if(fade>1.f) fade= 1.f; - } -} - -// =============================== -// class SelectionQuad -// =============================== - -SelectionQuad::SelectionQuad(){ - enabled= false; - posDown= Vec2i(0); - posUp= Vec2i(0); -} - -void SelectionQuad::setPosDown(const Vec2i &posDown){ - enabled= true; - this->posDown= posDown; - this->posUp= posDown; -} - -void SelectionQuad::setPosUp(const Vec2i &posUp){ - this->posUp= posUp; -} - -void SelectionQuad::disable(){ - enabled= false; -} - -// ===================================================== -// class Gui -// ===================================================== - -//constructor -Gui::Gui(){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] START\n",__FILE__,__FUNCTION__); - - lastGroupRecall = -1; - posObjWorld= Vec2i(54, 14); - validPosObjWorld= false; - activeCommandType= NULL; - activeCommandClass= ccStop; - selectingBuilding= false; - selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); - selectingPos= false; - selectingMeetingPoint= false; - activePos= invalidPos; - lastPosDisplay= invalidPos; - lastQuadCalcFrame=0; - selectionCalculationFrameSkip=10; - minQuadSize=20; - selectedResourceObjectPos=Vec2i(-1,-1); - highlightedResourceObjectPos=Vec2i(-1,-1); - highlightedUnitId=-1; - hudTexture=NULL; - commander=NULL; - world=NULL; - game=NULL; - gameCamera=NULL; - console=NULL; - choosenBuildingType=NULL; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] END\n",__FILE__,__FUNCTION__); -} - -void Gui::init(Game *game) { - - this->commander = game->getCommander(); - this->gameCamera = game->getGameCameraPtr(); - this->console = game->getConsole(); - this->world = game->getWorld(); - this->game = game; - - selection.init(this, - world->getThisFactionIndex(), - world->getThisTeamIndex(), - game->isFlagType1BitEnabled(ft1_allow_shared_team_units)); -} - -void Gui::end(){ - selection.clear(); - if(hudTexture != NULL) { - Renderer::getInstance().endTexture(rsGlobal, hudTexture, false); - } - hudTexture = NULL; -} - -// ==================== get ==================== - -const UnitType *Gui::getBuilding() const{ - assert(selectingBuilding); - return choosenBuildingType; -} -const Object *Gui::getSelectedResourceObject() const{ - if(selectedResourceObjectPos.x==-1){ - return NULL; - } - else { - return world->getMap()->getSurfaceCell(selectedResourceObjectPos)->getObject(); - } -} -Object *Gui::getHighlightedResourceObject() const{ - if(highlightedResourceObjectPos.x==-1){ - return NULL; - } - else { - if(world == NULL) { - throw megaglest_runtime_error("world == NULL"); + Mouse3d::Mouse3d() { + enabled = false; + rot = 0; + fade = 0.f; } - if(world->getMap() == NULL) { - throw megaglest_runtime_error("world->getMap() == NULL"); + + void Mouse3d::enable() { + enabled = true; + fade = 0.f; } - if(world->getMap()->getSurfaceCell(highlightedResourceObjectPos) == NULL) { - throw megaglest_runtime_error("world->getMap()->getSurfaceCell(highlightedResourceObjectPos) == NULL"); - } - return world->getMap()->getSurfaceCell(highlightedResourceObjectPos)->getObject(); - } -} -Unit* Gui::getHighlightedUnit() const{ - if(highlightedUnitId==-1){ - return NULL; - } - else { - return world->findUnitById(highlightedUnitId); - } -} - -// ==================== is ==================== - -bool Gui::isPlacingBuilding() const{ - return isSelectingPos() && activeCommandType!=NULL && activeCommandType->getClass()==ccBuild; -} - -// ==================== set ==================== - -void Gui::invalidatePosObjWorld(){ - validPosObjWorld= false; -} - -// ==================== reset state ==================== - -void Gui::resetState(){ - selectingBuilding= false; - selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); - selectingPos= false; - selectingMeetingPoint= false; - activePos= invalidPos; - activeCommandClass= ccStop; - activeCommandType= NULL; -} - -// ==================== events ==================== - -void Gui::update(){ - - if(selectionQuad.isEnabled() && selectionQuad.getPosUp().dist(selectionQuad.getPosDown())>minQuadSize){ - computeSelected(false,false); - } - mouse3d.update(); -} - -void Gui::tick(){ - computeDisplay(); -} - -//in display coords -bool Gui::mouseValid(int x, int y) { - return computePosDisplay(x, y) != invalidPos; -} - -void Gui::mouseDownLeftDisplay(int x, int y) { - if(selectingPos == false && - selectingMeetingPoint == false) { - - int posDisplay = computePosDisplay(x, y); - - if(posDisplay != invalidPos) { - if(selection.isCommandable()) { - - if(selectingBuilding) { - mouseDownDisplayUnitBuild(posDisplay); - } - else { - mouseDownDisplayUnitSkills(posDisplay); - } + void Mouse3d::update() { + if (enabled) { + rot = (rot + 3) % 360; + fade += fadeSpeed; + if (fade > 1.f) fade = 1.f; } - else { + } + + // =============================== + // class SelectionQuad + // =============================== + + SelectionQuad::SelectionQuad() { + enabled = false; + posDown = Vec2i(0); + posUp = Vec2i(0); + } + + void SelectionQuad::setPosDown(const Vec2i &posDown) { + enabled = true; + this->posDown = posDown; + this->posUp = posDown; + } + + void SelectionQuad::setPosUp(const Vec2i &posUp) { + this->posUp = posUp; + } + + void SelectionQuad::disable() { + enabled = false; + } + + // ===================================================== + // class Gui + // ===================================================== + + //constructor + Gui::Gui() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] START\n", __FILE__, __FUNCTION__); + + lastGroupRecall = -1; + posObjWorld = Vec2i(54, 14); + validPosObjWorld = false; + activeCommandType = NULL; + activeCommandClass = ccStop; + selectingBuilding = false; + selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); + selectingPos = false; + selectingMeetingPoint = false; + activePos = invalidPos; + lastPosDisplay = invalidPos; + lastQuadCalcFrame = 0; + selectionCalculationFrameSkip = 10; + minQuadSize = 20; + selectedResourceObjectPos = Vec2i(-1, -1); + highlightedResourceObjectPos = Vec2i(-1, -1); + highlightedUnitId = -1; + hudTexture = NULL; + commander = NULL; + world = NULL; + game = NULL; + gameCamera = NULL; + console = NULL; + choosenBuildingType = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] END\n", __FILE__, __FUNCTION__); + } + + void Gui::init(Game *game) { + + this->commander = game->getCommander(); + this->gameCamera = game->getGameCameraPtr(); + this->console = game->getConsole(); + this->world = game->getWorld(); + this->game = game; + + selection.init(this, + world->getThisFactionIndex(), + world->getThisTeamIndex(), + game->isFlagType1BitEnabled(ft1_allow_shared_team_units)); + } + + void Gui::end() { + selection.clear(); + if (hudTexture != NULL) { + Renderer::getInstance().endTexture(rsGlobal, hudTexture, false); + } + hudTexture = NULL; + } + + // ==================== get ==================== + + const UnitType *Gui::getBuilding() const { + assert(selectingBuilding); + return choosenBuildingType; + } + const Object *Gui::getSelectedResourceObject() const { + if (selectedResourceObjectPos.x == -1) { + return NULL; + } else { + return world->getMap()->getSurfaceCell(selectedResourceObjectPos)->getObject(); + } + } + Object *Gui::getHighlightedResourceObject() const { + if (highlightedResourceObjectPos.x == -1) { + return NULL; + } else { + if (world == NULL) { + throw megaglest_runtime_error("world == NULL"); + } + if (world->getMap() == NULL) { + throw megaglest_runtime_error("world->getMap() == NULL"); + } + if (world->getMap()->getSurfaceCell(highlightedResourceObjectPos) == NULL) { + throw megaglest_runtime_error("world->getMap()->getSurfaceCell(highlightedResourceObjectPos) == NULL"); + } + return world->getMap()->getSurfaceCell(highlightedResourceObjectPos)->getObject(); + } + } + + Unit* Gui::getHighlightedUnit() const { + if (highlightedUnitId == -1) { + return NULL; + } else { + return world->findUnitById(highlightedUnitId); + } + } + + // ==================== is ==================== + + bool Gui::isPlacingBuilding() const { + return isSelectingPos() && activeCommandType != NULL && activeCommandType->getClass() == ccBuild; + } + + // ==================== set ==================== + + void Gui::invalidatePosObjWorld() { + validPosObjWorld = false; + } + + // ==================== reset state ==================== + + void Gui::resetState() { + selectingBuilding = false; + selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); + selectingPos = false; + selectingMeetingPoint = false; + activePos = invalidPos; + activeCommandClass = ccStop; + activeCommandType = NULL; + } + + // ==================== events ==================== + + void Gui::update() { + + if (selectionQuad.isEnabled() && selectionQuad.getPosUp().dist(selectionQuad.getPosDown()) > minQuadSize) { + computeSelected(false, false); + } + mouse3d.update(); + } + + void Gui::tick() { + computeDisplay(); + } + + //in display coords + bool Gui::mouseValid(int x, int y) { + return computePosDisplay(x, y) != invalidPos; + } + + void Gui::mouseDownLeftDisplay(int x, int y) { + if (selectingPos == false && + selectingMeetingPoint == false) { + + int posDisplay = computePosDisplay(x, y); + + if (posDisplay != invalidPos) { + if (selection.isCommandable()) { + + if (selectingBuilding) { + mouseDownDisplayUnitBuild(posDisplay); + } else { + mouseDownDisplayUnitSkills(posDisplay); + } + } else { + resetState(); + } + } + computeDisplay(); + } + } + + void Gui::mouseMoveDisplay(int x, int y) { + computeInfoString(computePosDisplay(x, y)); + } + + void Gui::mouseMoveOutsideDisplay() { + computeInfoString(invalidPos); + } + + void Gui::mouseDownLeftGraphics(int x, int y, bool prepared) { + if (selectingPos) { + //give standard orders + Vec2i targetPos = game->getMouseCellPos(); + if (prepared || (game->isValidMouseCellPos() && + world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true)) { + giveTwoClickOrders(x, y, prepared); + } resetState(); } - } - computeDisplay(); - } -} - -void Gui::mouseMoveDisplay(int x, int y) { - computeInfoString(computePosDisplay(x, y)); -} - -void Gui::mouseMoveOutsideDisplay() { - computeInfoString(invalidPos); -} - -void Gui::mouseDownLeftGraphics(int x, int y, bool prepared) { - if(selectingPos) { - //give standard orders - Vec2i targetPos=game->getMouseCellPos(); - if(prepared || (game->isValidMouseCellPos() && - world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true)) { - giveTwoClickOrders(x, y, prepared); - } - resetState(); - } - //set meeting point - else if(selectingMeetingPoint) { - if(selection.isCommandable()) { - Vec2i targetPos=game->getMouseCellPos(); - if(prepared || (game->isValidMouseCellPos() && - world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true)) { - commander->trySetMeetingPoint(selection.getFrontUnit(), targetPos); - } - } - resetState(); - } - else { - selectionQuad.setPosDown(Vec2i(x, y)); - computeSelected(false,false); - } - computeDisplay(); -} - -void Gui::mouseDownRightGraphics(int x, int y , bool prepared) { - if(selectingPos || selectingMeetingPoint) { - resetState(); - } - else if(selection.isCommandable()) { - if(prepared) { - //Vec2i targetPos=game->getMouseCellPos(); - givePreparedDefaultOrders(x, y); - } - else { - Vec2i targetPos=game->getMouseCellPos(); - if(game->isValidMouseCellPos() && - world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true) { - giveDefaultOrders(x, y); - } - } - } - computeDisplay(); -} - -void Gui::mouseUpLeftGraphics(int x, int y) { - if(!selectingPos && !selectingMeetingPoint) { - if(selectionQuad.isEnabled()){ - selectionQuad.setPosUp(Vec2i(x, y)); - if(selectionQuad.getPosUp().dist(selectionQuad.getPosDown()) > minQuadSize) { - computeSelected(false,true); - } - if(selection.isCommandable() && random.randRange(0, 1)==0){ - SoundRenderer::getInstance().playFx( - selection.getFrontUnit()->getType()->getSelectionSound(), - selection.getFrontUnit()->getCurrMidHeightVector(), - gameCamera->getPos()); - } - selectionQuad.disable(); - } - } -} - -void Gui::mouseMoveGraphics(int x, int y) { - //compute selection - if(selectionQuad.isEnabled()){ - selectionQuad.setPosUp(Vec2i(x, y)); - computeSelected(false,false); - } - - //compute position for building - if(isPlacingBuilding()){ - posObjWorld=game->getMouseCellPos(); - validPosObjWorld= game->isValidMouseCellPos(); - } - - display.setInfoText(""); -} - -void Gui::mouseDoubleClickLeftGraphics(int x, int y){ - if(!selectingPos && !selectingMeetingPoint){ - selectionQuad.setPosDown(Vec2i(x, y)); - computeSelected(true,true); - computeDisplay(); - } -} - -void Gui::groupKey(int groupIndex) { - if(isKeyDown(vkControl)){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex); -// bool allAssigned=true; - bool clearGroup=!isKeyDown(vkShift); -// if(!clearGroup){ -// Unit* unit=selection.getFrontUnit(); -// if(unit!=null && unit->getType()->getMultiSelect()==false){ -// return; -// } -// } - bool allAssigned=selection.assignGroup(groupIndex,clearGroup); - if(!allAssigned){ - console->addStdMessage("GroupAssignFailed"); - } - } - else{ - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] groupIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,groupIndex); - - int recallGroupCenterCameraTimeout = Config::getInstance().getInt("RecallGroupCenterCameraTimeoutMilliseconds","1500"); - - if(lastGroupRecall == groupIndex && - lastGroupRecallTime.getMillis() > 0 && - lastGroupRecallTime.getMillis() <= recallGroupCenterCameraTimeout) { - - selection.recallGroup(groupIndex,!isKeyDown(vkShift)); - centerCameraOnSelection(); - } - else { - selection.recallGroup(groupIndex,!isKeyDown(vkShift)); - } - - lastGroupRecallTime.start(); - lastGroupRecall = groupIndex; - } -} - -void Gui::hotKey(SDL_KeyboardEvent key) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] key = [%c][%d]\n",__FILE__,__FUNCTION__,key,key); - - Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); - - //if(key == configKeys.getCharKey("HotKeyCenterCameraOnSelection")) { - if(isKeyPressed(configKeys.getSDLKey("HotKeyCenterCameraOnSelection"),key) == true) { - centerCameraOnSelection(); - } - //else if(key == configKeys.getCharKey("HotKeySelectIdleHarvesterUnit")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectIdleHarvesterUnit"),key) == true) { - selectInterestingUnit(iutIdleHarvester); - } - //else if(key == configKeys.getCharKey("HotKeySelectBuiltBuilding")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectBuiltBuilding"),key) == true) { - selectInterestingUnit(iutBuiltBuilding); - } - //else if(key == configKeys.getCharKey("HotKeyDumpWorldToLog")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeyDumpWorldToLog"),key) == true) { - std::string worldLog = world->DumpWorldToLog(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] worldLog dumped to [%s]\n",__FILE__,__FUNCTION__,__LINE__,worldLog.c_str()); - } - //else if(key == configKeys.getCharKey("HotKeyRotateUnitDuringPlacement")){ - else if(isKeyPressed(configKeys.getSDLKey("HotKeyRotateUnitDuringPlacement"),key) == true) { - // Here the user triggers a unit rotation while placing a unit - if(isPlacingBuilding()) { - if(getBuilding()->getRotationAllowed()){ - ++selectedBuildingFacing; - } - } - } - //else if(key == configKeys.getCharKey("HotKeySelectDamagedUnit")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectDamagedUnit"),key) == true) { - selectInterestingUnit(iutDamaged); - } - //else if(key == configKeys.getCharKey("HotKeySelectStoreUnit")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectStoreUnit"),key) == true) { - selectInterestingUnit(iutStore); - } - //else if(key == configKeys.getCharKey("HotKeySelectedUnitsAttack")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsAttack"),key) == true) { - clickCommonCommand(ccAttack); - } - //else if(key == configKeys.getCharKey("HotKeySelectedUnitsAttack")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsMove"),key) == true) { - clickCommonCommand(ccMove); - } - //else if(key == configKeys.getCharKey("HotKeySelectedUnitsStop")) { - else if(isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsStop"),key) == true) { - clickCommonCommand(ccStop); - } -} - -void Gui::switchToNextDisplayColor(){ - display.switchColor(); -} - -void Gui::onSelectionChanged(){ - resetState(); - computeDisplay(); -} - -// ================= PRIVATE ================= - -void Gui::giveOneClickOrders(){ - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - std::pair result(crFailUndefined,""); - bool queueKeyDown = isKeyDown(queueCommandKey); - if(selection.isUniform()){ - result= commander->tryGiveCommand(&selection, activeCommandType, Vec2i(0), (Unit*)NULL, queueKeyDown); - } - else{ - result= commander->tryGiveCommand(&selection, activeCommandClass, Vec2i(0), (Unit*)NULL, queueKeyDown); - } - addOrdersResultToConsole(activeCommandClass, result); - activeCommandType= NULL; - activeCommandClass= ccStop; -} - -void Gui::giveDefaultOrders(int x, int y) { - //compute target - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - const Unit *targetUnit= NULL; - Vec2i targetPos; - if(computeTarget(Vec2i(x, y), targetPos, targetUnit) == false) { - console->addStdMessage("InvalidPosition"); - return; - } - //printf("In [%s::%s Line: %d] targetUnit = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,targetUnit); - giveDefaultOrders(targetPos.x,targetPos.y,targetUnit,true); - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void Gui::givePreparedDefaultOrders(int x, int y){ - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - giveDefaultOrders(x, y, NULL,false); -} - -void Gui::giveDefaultOrders(int x, int y,const Unit *targetUnit, bool paintMouse3d) { - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - bool queueKeyDown = isKeyDown(queueCommandKey); - Vec2i targetPos=Vec2i(x, y); - - //give order - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - std::pair result= commander->tryGiveCommand(&selection, targetPos, targetUnit, queueKeyDown); - - //printf("In [%s::%s Line: %d] selected units = %d result.first = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,selection.getCount(),result.first); - - //graphical result - addOrdersResultToConsole(activeCommandClass, result); - if(result.first == crSuccess || result.first == crSomeFailed) { - if(paintMouse3d) - mouse3d.enable(); - - if(random.randRange(0, 1)==0){ - SoundRenderer::getInstance().playFx( - selection.getFrontUnit()->getType()->getCommandSound(), - selection.getFrontUnit()->getCurrMidHeightVector(), - gameCamera->getPos()); - } - } - - //reset - resetState(); -} - -void Gui::giveTwoClickOrders(int x, int y , bool prepared) { - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - std::pair result(crFailUndefined,""); - - //compute target - const Unit *targetUnit= NULL; - Vec2i targetPos; - if(prepared){ - targetPos=Vec2i(x, y); - } - else { - if(computeTarget(Vec2i(x, y), targetPos, targetUnit) == false) { - console->addStdMessage("InvalidPosition"); - return; - } - } - - bool queueKeyDown = isKeyDown(queueCommandKey); - //give orders to the units of this faction - if(selectingBuilding == false) { - if(selection.isUniform()) { - result= commander->tryGiveCommand(&selection, activeCommandType, - targetPos, targetUnit,queueKeyDown); - } - else { - result= commander->tryGiveCommand(&selection, activeCommandClass, - targetPos, targetUnit,queueKeyDown); - } - } - else { - //selecting building - result= commander->tryGiveCommand(&selection, - activeCommandType, posObjWorld, choosenBuildingType, - selectedBuildingFacing,queueKeyDown); - } - - //graphical result - addOrdersResultToConsole(activeCommandClass, result); - if(result.first == crSuccess || result.first == crSomeFailed) { - if(prepared == false) { - mouse3d.enable(); - } - - if(random.randRange(0, 1) == 0) { - SoundRenderer::getInstance().playFx( - selection.getFrontUnit()->getType()->getCommandSound(), - selection.getFrontUnit()->getCurrMidHeightVector(), - gameCamera->getPos()); - } - } -} - -void Gui::centerCameraOnSelection() { - if(selection.isEmpty() == false) { - Vec3f refPos= selection.getRefPos(); - gameCamera->centerXZ(refPos.x, refPos.z); - } -} - -void Gui::selectInterestingUnit(InterestingUnitType iut) { - const Faction *thisFaction = world->getThisFaction(); - const Unit* previousUnit = NULL; - bool previousFound = true; - - //start at the next harvester - if(selection.getCount() == 1) { - const Unit* refUnit= selection.getFrontUnit(); - - if(refUnit->isInteresting(iut)) { - previousUnit= refUnit; - previousFound= false; - } - } - - //clear selection - selection.clear(); - - //search - for(int index = 0; index < thisFaction->getUnitCount(); ++index){ - Unit* unit = thisFaction->getUnit(index); - - if(previousFound == true) { - if(unit->isInteresting(iut)) { - selection.select(unit,false); - break; - } - } - else{ - if(unit == previousUnit) { - previousFound = true; - } - } - } - - //search again if we have a previous - if(selection.isEmpty() && previousUnit != NULL && previousFound == true) { - for(int index = 0; index < thisFaction->getUnitCount(); ++index) { - Unit* unit = thisFaction->getUnit(index); - - if(unit->isInteresting(iut)) { - selection.select(unit,false); - break; - } - } - } -} - -void Gui::clickCommonCommand(CommandClass commandClass) { - for(int index = 0; index < Display::downCellCount; ++index) { - const CommandType *ct = display.getCommandType(index); - - if((ct != NULL && ct->getClass() == commandClass) || - display.getCommandClass(index) == commandClass) { - - mouseDownDisplayUnitSkills(index); - break; - } - } - computeDisplay(); -} - -void Gui::mouseDownDisplayUnitSkills(int posDisplay) { - if(selection.isEmpty() == false) { - if(posDisplay != cancelPos) { - if(posDisplay != meetingPointPos) { - const Unit *unit= selection.getFrontUnit(); - - //uniform selection - if(selection.isUniform()) { - const CommandType *ct = display.getCommandType(posDisplay); - - // try to switch to next attack type - if(activeCommandClass == ccAttack && activeCommandType!=NULL) { - - int maxI = unit->getType()->getCommandTypeCount(); - int cmdTypeId = activeCommandType->getId(); - int cmdTypeIdNext = cmdTypeId+1; - - while(cmdTypeIdNext != cmdTypeId) { - if(cmdTypeIdNext >= maxI) { - cmdTypeIdNext = 0; - } - const CommandType *ctype = display.getCommandType(cmdTypeIdNext); - if(ctype != NULL && ctype->getClass() == ccAttack) { - if(ctype != NULL && unit->getFaction()->reqsOk(ctype)) { - posDisplay=cmdTypeIdNext; - ct = display.getCommandType(posDisplay); - break; - } - } - cmdTypeIdNext++; - } - } - - if(ct != NULL && unit->getFaction()->reqsOk(ct)) { - activeCommandType= ct; - activeCommandClass= activeCommandType->getClass(); - } - else { - posDisplay= invalidPos; - activeCommandType= NULL; - activeCommandClass= ccStop; - return; + //set meeting point + else if (selectingMeetingPoint) { + if (selection.isCommandable()) { + Vec2i targetPos = game->getMouseCellPos(); + if (prepared || (game->isValidMouseCellPos() && + world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true)) { + commander->trySetMeetingPoint(selection.getFrontUnit(), targetPos); } } + resetState(); + } else { + selectionQuad.setPosDown(Vec2i(x, y)); + computeSelected(false, false); + } + computeDisplay(); + } - //non uniform selection - else { - activeCommandType= NULL; - activeCommandClass= display.getCommandClass(posDisplay); - } - - //give orders depending on command type - if(!selection.isEmpty()){ - const CommandType *ct= selection.getUnit(0)->getType()->getFirstCtOfClass(activeCommandClass); - if(activeCommandType!=NULL && activeCommandType->getClass()==ccBuild){ - assert(selection.isUniform()); - selectingBuilding= true; - } - else if(ct->getClicks()==cOne){ - invalidatePosObjWorld(); - giveOneClickOrders(); - } - else{ - selectingPos= true; - activePos= posDisplay; + void Gui::mouseDownRightGraphics(int x, int y, bool prepared) { + if (selectingPos || selectingMeetingPoint) { + resetState(); + } else if (selection.isCommandable()) { + if (prepared) { + //Vec2i targetPos=game->getMouseCellPos(); + givePreparedDefaultOrders(x, y); + } else { + Vec2i targetPos = game->getMouseCellPos(); + if (game->isValidMouseCellPos() && + world->getMap()->isInsideSurface(world->getMap()->toSurfCoords(targetPos)) == true) { + giveDefaultOrders(x, y); } } } - else{ - activePos= posDisplay; - selectingMeetingPoint= true; + computeDisplay(); + } + + void Gui::mouseUpLeftGraphics(int x, int y) { + if (!selectingPos && !selectingMeetingPoint) { + if (selectionQuad.isEnabled()) { + selectionQuad.setPosUp(Vec2i(x, y)); + if (selectionQuad.getPosUp().dist(selectionQuad.getPosDown()) > minQuadSize) { + computeSelected(false, true); + } + if (selection.isCommandable() && random.randRange(0, 1) == 0) { + SoundRenderer::getInstance().playFx( + selection.getFrontUnit()->getType()->getSelectionSound(), + selection.getFrontUnit()->getCurrMidHeightVector(), + gameCamera->getPos()); + } + selectionQuad.disable(); + } } } - else{ - commander->tryCancelCommand(&selection); + + void Gui::mouseMoveGraphics(int x, int y) { + //compute selection + if (selectionQuad.isEnabled()) { + selectionQuad.setPosUp(Vec2i(x, y)); + computeSelected(false, false); + } + + //compute position for building + if (isPlacingBuilding()) { + posObjWorld = game->getMouseCellPos(); + validPosObjWorld = game->isValidMouseCellPos(); + } + + display.setInfoText(""); } - } -} -void Gui::mouseDownDisplayUnitBuild(int posDisplay) { - //int factionIndex = world->getThisFactionIndex(); + void Gui::mouseDoubleClickLeftGraphics(int x, int y) { + if (!selectingPos && !selectingMeetingPoint) { + selectionQuad.setPosDown(Vec2i(x, y)); + computeSelected(true, true); + computeDisplay(); + } + } - if(posDisplay == cancelPos) { - resetState(); - } - else { - if(activeCommandType != NULL && - activeCommandType->getClass() == ccBuild) { + void Gui::groupKey(int groupIndex) { + if (isKeyDown(vkControl)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] groupIndex = %d\n", __FILE__, __FUNCTION__, __LINE__, groupIndex); + // bool allAssigned=true; + bool clearGroup = !isKeyDown(vkShift); + // if(!clearGroup){ + // Unit* unit=selection.getFrontUnit(); + // if(unit!=null && unit->getType()->getMultiSelect()==false){ + // return; + // } + // } + bool allAssigned = selection.assignGroup(groupIndex, clearGroup); + if (!allAssigned) { + console->addStdMessage("GroupAssignFailed"); + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] groupIndex = %d\n", __FILE__, __FUNCTION__, __LINE__, groupIndex); - const BuildCommandType *bct = dynamic_cast(activeCommandType); - if(bct != NULL) { - const UnitType *ut = bct->getBuilding(posDisplay); + int recallGroupCenterCameraTimeout = Config::getInstance().getInt("RecallGroupCenterCameraTimeoutMilliseconds", "1500"); - const Unit *unit = selection.getFrontUnit(); - if(unit != NULL && unit->getFaction() != NULL) { + if (lastGroupRecall == groupIndex && + lastGroupRecallTime.getMillis() > 0 && + lastGroupRecallTime.getMillis() <= recallGroupCenterCameraTimeout) { - if(selection.canSelectUnitFactionCheck(unit) == true && - unit->getFaction()->reqsOk(ut)) { + selection.recallGroup(groupIndex, !isKeyDown(vkShift)); + centerCameraOnSelection(); + } else { + selection.recallGroup(groupIndex, !isKeyDown(vkShift)); + } - choosenBuildingType = ut; - selectingPos = true; - selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); - activePos = posDisplay; + lastGroupRecallTime.start(); + lastGroupRecall = groupIndex; + } + } + + void Gui::hotKey(SDL_KeyboardEvent key) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] key = [%c][%d]\n", __FILE__, __FUNCTION__, key, key); + + Config &configKeys = Config::getInstance(std::pair(cfgMainKeys, cfgUserKeys)); + + //if(key == configKeys.getCharKey("HotKeyCenterCameraOnSelection")) { + if (isKeyPressed(configKeys.getSDLKey("HotKeyCenterCameraOnSelection"), key) == true) { + centerCameraOnSelection(); + } + //else if(key == configKeys.getCharKey("HotKeySelectIdleHarvesterUnit")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectIdleHarvesterUnit"), key) == true) { + selectInterestingUnit(iutIdleHarvester); + } + //else if(key == configKeys.getCharKey("HotKeySelectBuiltBuilding")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectBuiltBuilding"), key) == true) { + selectInterestingUnit(iutBuiltBuilding); + } + //else if(key == configKeys.getCharKey("HotKeyDumpWorldToLog")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeyDumpWorldToLog"), key) == true) { + std::string worldLog = world->DumpWorldToLog(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] worldLog dumped to [%s]\n", __FILE__, __FUNCTION__, __LINE__, worldLog.c_str()); + } + //else if(key == configKeys.getCharKey("HotKeyRotateUnitDuringPlacement")){ + else if (isKeyPressed(configKeys.getSDLKey("HotKeyRotateUnitDuringPlacement"), key) == true) { + // Here the user triggers a unit rotation while placing a unit + if (isPlacingBuilding()) { + if (getBuilding()->getRotationAllowed()) { + ++selectedBuildingFacing; } } } - } - } -} - - -string Gui::computeDefaultInfoString() { - string result=""; - - if(selection.isUniform()) { - if(selection.isObserver() || selection.isCommandable()) { - // default is the description extension - result = selection.getFrontUnit()->getDescExtension(game->showTranslatedTechTree()); - } - } - return result; -} - - -void Gui::computeInfoString(int posDisplay){ - - Lang &lang= Lang::getInstance(); - - lastPosDisplay = posDisplay; - - display.setInfoText(computeDefaultInfoString()); - - if(posDisplay!=invalidPos && selection.isCommandable()){ - if(!selectingBuilding){ - if(posDisplay==cancelPos){ - display.setInfoText(lang.getString("Cancel")); + //else if(key == configKeys.getCharKey("HotKeySelectDamagedUnit")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectDamagedUnit"), key) == true) { + selectInterestingUnit(iutDamaged); } - else if(posDisplay==meetingPointPos){ - display.setInfoText(lang.getString("MeetingPoint")); + //else if(key == configKeys.getCharKey("HotKeySelectStoreUnit")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectStoreUnit"), key) == true) { + selectInterestingUnit(iutStore); } - else{ - //uniform selection - if(selection.isUniform()){ - const Unit *unit= selection.getFrontUnit(); - const CommandType *ct= display.getCommandType(posDisplay); - - if(ct!=NULL){ - if(unit->getFaction()->reqsOk(ct)){ - display.setInfoText(ct->getDesc(unit->getTotalUpgrade(),game->showTranslatedTechTree())); - } - else{ - display.setInfoText(ct->getReqDesc(game->showTranslatedTechTree())); - if(ct->getClass()==ccUpgrade){ - string text=""; - const UpgradeCommandType *uct= static_cast(ct); - if(unit->getFaction()->getUpgradeManager()->isUpgrading(uct->getProducedUpgrade())){ - text=lang.getString("Upgrading")+"\n\n"; - } - else if(unit->getFaction()->getUpgradeManager()->isUpgraded(uct->getProducedUpgrade())){ - text=lang.getString("AlreadyUpgraded")+"\n\n"; - } - display.setInfoText(text+ct->getReqDesc(game->showTranslatedTechTree())); - } - //locked by scenario - else if(ct->getClass()==ccProduce){ - string text=""; - const ProduceCommandType *pct= static_cast(ct); - if(unit->getFaction()->isUnitLocked(pct->getProducedUnit())){ - display.setInfoText(lang.getString("LockedByScenario")+"\n\n"+ct->getReqDesc(game->showTranslatedTechTree())); - } - } - else if(ct->getClass()==ccMorph){ - const MorphCommandType *mct= static_cast(ct); - if(unit->getFaction()->isUnitLocked(mct->getMorphUnit())){ - display.setInfoText(lang.getString("LockedByScenario")+"\n\n"+ct->getReqDesc(game->showTranslatedTechTree())); - } - } - } - } - } - - //non uniform selection - else{ - const UnitType *ut= selection.getFrontUnit()->getType(); - CommandClass cc= display.getCommandClass(posDisplay); - if(cc!=ccNull){ - display.setInfoText(lang.getString("CommonCommand") + ": " + ut->getFirstCtOfClass(cc)->toString(true)); - } - } + //else if(key == configKeys.getCharKey("HotKeySelectedUnitsAttack")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsAttack"), key) == true) { + clickCommonCommand(ccAttack); } - } - else{ - if(posDisplay==cancelPos){ - display.setInfoText(lang.getString("Return")); + //else if(key == configKeys.getCharKey("HotKeySelectedUnitsAttack")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsMove"), key) == true) { + clickCommonCommand(ccMove); } - else{ - if(activeCommandType!=NULL && activeCommandType->getClass()==ccBuild){ - //locked by scenario - const BuildCommandType *bct= static_cast(activeCommandType); - const Unit *unit= selection.getFrontUnit(); - if(unit->getFaction()->isUnitLocked(bct->getBuilding(posDisplay))){ - display.setInfoText(lang.getString("LockedByScenario")+"\n\n"+bct->getBuilding(posDisplay)->getReqDesc(game->showTranslatedTechTree())); - } else { - bool translatedValue= game->showTranslatedTechTree(); - const UnitType *building=bct->getBuilding(posDisplay); - string str= lang.getString("BuildSpeed",(translatedValue == true ? "" : "english"))+": "+ intToStr(bct->getBuildSkillType()->getSpeed())+"\n"; - str+=""+Lang::getInstance().getString("TimeSteps",(translatedValue == true ? "" : "english"))+": "+intToStr(building->getProductionTime())+"\n"; - int64 speed=bct->getBuildSkillType()->getSpeed()+bct->getBuildSkillType()->getTotalSpeed(unit->getTotalUpgrade()); - int64 time=building->getProductionTime(); - int64 seconds=time*100/speed; - str+=""+Lang::getInstance().getString("Time",(translatedValue == true ? "" : "english"))+": "+intToStr(seconds); - str+="\n\n"; - str+=building->getReqDesc(translatedValue); - display.setInfoText(str); - } - } - } - } - } -} - -void Gui::computeDisplay(){ - - //printf("Start ===> computeDisplay()\n"); - Lang &lang= Lang::getInstance(); - //init - display.clear(); - - // ================ PART 1 ================ - const Object *selectedResourceObject =getSelectedResourceObject(); - if(selection.isEmpty() && selectedResourceObject != NULL && selectedResourceObject->getResource() != NULL) { - Resource *r = selectedResourceObject->getResource(); - display.setTitle(r->getType()->getName(game->showTranslatedTechTree())); - display.setText(lang.getString("Amount")+ ": "+intToStr(r->getAmount())+" / "+intToStr(r->getType()->getDefResPerPatch())); - //display.setProgressBar(r->); - display.setUpImage(0, r->getType()->getImage()); - } - else { - //title, text and progress bar - if(selection.getCount() == 1){ - display.setTitle(selection.getFrontUnit()->getFullName(game->showTranslatedTechTree())); - display.setText(selection.getFrontUnit()->getDesc(game->showTranslatedTechTree())); - display.setProgressBar(selection.getFrontUnit()->getProductionPercent()); - } - - //portraits - int unitIndex = 0; - for(unitIndex = 0; unitIndex < selection.getCount(); ++unitIndex){ - try { - const Unit *unit = selection.getUnit(unitIndex); - if(unit == NULL) { - throw megaglest_runtime_error("unit == NULL"); - } - if(unit->getType() == NULL) { - throw megaglest_runtime_error("unit->getType() == NULL"); - } - if(unit->getType()->getImage() == NULL) { - throw megaglest_runtime_error("unit->getType()->getImage()"); - } - - display.setUpImage(unitIndex, unit->getType()->getImage()); - } - catch(exception &ex) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error in unit selection for index: %d error [%s]",unitIndex,ex.what()); - throw megaglest_runtime_error(szBuf, true); + //else if(key == configKeys.getCharKey("HotKeySelectedUnitsStop")) { + else if (isKeyPressed(configKeys.getSDLKey("HotKeySelectedUnitsStop"), key) == true) { + clickCommonCommand(ccStop); } } - // ================ PART 2 ================ - - if(selectingPos || selectingMeetingPoint){ - //printf("selectingPos || selectingMeetingPoint\n"); - display.setDownSelectedPos(activePos); + void Gui::switchToNextDisplayColor() { + display.switchColor(); } - //printf("computeDisplay selection.isCommandable() = %d\n",selection.isCommandable()); + void Gui::onSelectionChanged() { + resetState(); + computeDisplay(); + } - if(selection.isCommandable()) { - //printf("selection.isComandable()\n"); + // ================= PRIVATE ================= - if(selectingBuilding == false){ + void Gui::giveOneClickOrders() { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //cancel button - const Unit *u= selection.getFrontUnit(); - const UnitType *ut= u->getType(); - if(selection.isCancelable()) { - //printf("selection.isCancelable() commandcount = %d\n",selection.getUnit(0)->getCommandSize()); - if(selection.getUnit(0)->getCommandSize() > 0){ - //printf("Current Command [%s]\n",selection.getUnit(0)->getCurrCommand()->toString().c_str()); - } + std::pair result(crFailUndefined, ""); + bool queueKeyDown = isKeyDown(queueCommandKey); + if (selection.isUniform()) { + result = commander->tryGiveCommand(&selection, activeCommandType, Vec2i(0), (Unit*) NULL, queueKeyDown); + } else { + result = commander->tryGiveCommand(&selection, activeCommandClass, Vec2i(0), (Unit*) NULL, queueKeyDown); + } + addOrdersResultToConsole(activeCommandClass, result); + activeCommandType = NULL; + activeCommandClass = ccStop; + } - display.setDownImage(cancelPos, ut->getCancelImage()); - display.setDownLighted(cancelPos, true); - } + void Gui::giveDefaultOrders(int x, int y) { + //compute target + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //meeting point - if(selection.isMeetable()){ - //printf("selection.isMeetable()\n"); + const Unit *targetUnit = NULL; + Vec2i targetPos; + if (computeTarget(Vec2i(x, y), targetPos, targetUnit) == false) { + console->addStdMessage("InvalidPosition"); + return; + } + //printf("In [%s::%s Line: %d] targetUnit = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,targetUnit); + giveDefaultOrders(targetPos.x, targetPos.y, targetUnit, true); + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + } - display.setDownImage(meetingPointPos, ut->getMeetingPointImage()); - display.setDownLighted(meetingPointPos, true); - } + void Gui::givePreparedDefaultOrders(int x, int y) { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + giveDefaultOrders(x, y, NULL, false); + } + void Gui::giveDefaultOrders(int x, int y, const Unit *targetUnit, bool paintMouse3d) { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + bool queueKeyDown = isKeyDown(queueCommandKey); + Vec2i targetPos = Vec2i(x, y); - //printf("computeDisplay selection.isUniform() = %d\n",selection.isUniform()); + //give order + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + std::pair result = commander->tryGiveCommand(&selection, targetPos, targetUnit, queueKeyDown); - if(selection.isUniform()) { - //printf("selection.isUniform()\n"); + //printf("In [%s::%s Line: %d] selected units = %d result.first = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,selection.getCount(),result.first); - //uniform selection - if(u->isBuilt()){ - //printf("u->isBuilt()\n"); + //graphical result + addOrdersResultToConsole(activeCommandClass, result); + if (result.first == crSuccess || result.first == crSomeFailed) { + if (paintMouse3d) + mouse3d.enable(); - int morphPos= 8; - for(int i= 0; i < ut->getCommandTypeCount(); ++i){ - int displayPos= i; - const CommandType *ct= ut->getCommandType(i); - if(ct->getClass() == ccMorph) { - displayPos= morphPos++; - } - - //printf("computeDisplay i = %d displayPos = %d morphPos = %d ct->getClass() = %d [%s]\n",i,displayPos,morphPos,ct->getClass(),ct->getName().c_str()); - - display.setDownImage(displayPos, ct->getImage()); - display.setCommandType(displayPos, ct); - display.setCommandClass(displayPos, ct->getClass()); - display.setDownLighted(displayPos, u->getFaction()->reqsOk(ct)); - } - } - } - else{ - //printf("selection.isUniform() == FALSE\n"); - - //non uniform selection - int lastCommand= 0; - for(int i= 0; i < ccCount; ++i){ - CommandClass cc= static_cast (i); - - //printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand); - - if(isSharedCommandClass(cc) && cc != ccBuild){ - display.setDownLighted(lastCommand, true); - display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage()); - display.setCommandClass(lastCommand, cc); - lastCommand++; - } - } + if (random.randRange(0, 1) == 0) { + SoundRenderer::getInstance().playFx( + selection.getFrontUnit()->getType()->getCommandSound(), + selection.getFrontUnit()->getCurrMidHeightVector(), + gameCamera->getPos()); } } - else{ + //reset + resetState(); + } + + void Gui::giveTwoClickOrders(int x, int y, bool prepared) { + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + std::pair result(crFailUndefined, ""); + + //compute target + const Unit *targetUnit = NULL; + Vec2i targetPos; + if (prepared) { + targetPos = Vec2i(x, y); + } else { + if (computeTarget(Vec2i(x, y), targetPos, targetUnit) == false) { + console->addStdMessage("InvalidPosition"); + return; + } + } + + bool queueKeyDown = isKeyDown(queueCommandKey); + //give orders to the units of this faction + if (selectingBuilding == false) { + if (selection.isUniform()) { + result = commander->tryGiveCommand(&selection, activeCommandType, + targetPos, targetUnit, queueKeyDown); + } else { + result = commander->tryGiveCommand(&selection, activeCommandClass, + targetPos, targetUnit, queueKeyDown); + } + } else { //selecting building - const Unit *unit= selection.getFrontUnit(); - if(activeCommandType != NULL && activeCommandType->getClass() == ccBuild){ - const BuildCommandType* bct= static_cast (activeCommandType); - for(int i= 0; i < bct->getBuildingCount(); ++i){ - display.setDownImage(i, bct->getBuilding(i)->getImage()); - display.setDownLighted(i, unit->getFaction()->reqsOk(bct->getBuilding(i))); - } - display.setDownImage(cancelPos, selection.getFrontUnit()->getType()->getCancelImage()); - display.setDownLighted(cancelPos, true); + result = commander->tryGiveCommand(&selection, + activeCommandType, posObjWorld, choosenBuildingType, + selectedBuildingFacing, queueKeyDown); + } + + //graphical result + addOrdersResultToConsole(activeCommandClass, result); + if (result.first == crSuccess || result.first == crSomeFailed) { + if (prepared == false) { + mouse3d.enable(); } - } - } - } - // refresh other things - if(!isSelecting() && !isSelectingPos()){ - if(!isSelectingPos() && lastPosDisplay == invalidPos){ - computeInfoString(lastPosDisplay); - } - } - -} - -int Gui::computePosDisplay(int x, int y){ - int posDisplay= display.computeDownIndex(x, y); - - //printf("computePosDisplay x = %d y = %d posDisplay = %d Display::downCellCount = %d cc = %d ct = %p\n",x,y,posDisplay,Display::downCellCount,display.getCommandClass(posDisplay),display.getCommandType(posDisplay)); - - if(posDisplay < 0 || posDisplay >= Display::downCellCount) { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - else if(selection.isCommandable()) { - if(posDisplay != cancelPos) { - if(posDisplay != meetingPointPos) { - if(selectingBuilding == false) { - //standard selection - if(display.getCommandClass(posDisplay) == ccNull && display.getCommandType(posDisplay) == NULL) { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - } - else { - //building selection - if(activeCommandType != NULL && activeCommandType->getClass() == ccBuild){ - const BuildCommandType *bct= static_cast(activeCommandType); - if(posDisplay >= bct->getBuildingCount()) { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - } - } - } - else{ - //check meeting point - if(!selection.isMeetable()) { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - } - } - else { - //check cancel button - if(selection.isCancelable() == false) { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - } - } - else { - posDisplay= invalidPos; - //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - } - - //printf("computePosDisplay returning = %d\n",posDisplay); - - return posDisplay; -} - -void Gui::addOrdersResultToConsole(CommandClass cc, std::pair result) { - - switch(result.first) { - case crSuccess: - break; - case crFailReqs: - switch(cc){ - case ccBuild: - console->addStdMessage("BuildingNoReqs",result.second); - break; - case ccProduce: - console->addStdMessage("UnitNoReqs",result.second); - break; - case ccMorph: - console->addStdMessage("MorphNoReqs",result.second); - break; - case ccUpgrade: - console->addStdMessage("UpgradeNoReqs",result.second); - break; - default: - break; - } - break; - case crFailRes: - switch(cc){ - case ccBuild: - console->addStdMessage("BuildingNoRes",result.second); - break; - case ccProduce: - console->addStdMessage("UnitNoRes",result.second); - break; - case ccMorph: - console->addStdMessage("MorphNoRes",result.second); - break; - case ccUpgrade: - console->addStdMessage("UpgradeNoRes",result.second); - break; - default: - break; - } - break; - - case crFailUndefined: - console->addStdMessage("InvalidOrder",result.second); - break; - - case crSomeFailed: - console->addStdMessage("SomeOrdersFailed",result.second); - break; - } -} - -bool Gui::isSharedCommandClass(CommandClass commandClass){ - for(int i=0; igetType()->getFirstCtOfClass(commandClass); - if(ct==NULL || !unit->getFaction()->reqsOk(ct)) - return false; - } - return true; -} - -void Gui::computeSelected(bool doubleClick, bool force){ - Selection::UnitContainer units; - - if( force || ( lastQuadCalcFrame+selectionCalculationFrameSkip < game->getTotalRenderFps() ) ){ - lastQuadCalcFrame=game->getTotalRenderFps(); - const Object *selectedResourceObject=NULL; - selectedResourceObjectPos=Vec2i(-1,-1); - if(selectionQuad.isEnabled() && selectionQuad.getPosUp().dist(selectionQuad.getPosDown())getMapPos()); - } - } - else{ - Renderer::getInstance().computeSelected(units, selectedResourceObject, false, selectionQuad.getPosDown(), selectionQuad.getPosUp()); - } - selectingBuilding= false; - activeCommandType= NULL; - - //select all units of the same type if double click - if(doubleClick && units.empty() == false){ - const Unit *refUnit= getRelevantObjectFromSelection(&units); - int factionIndex= refUnit->getFactionIndex(); - for(int i=0; igetFaction(factionIndex)->getUnitCount(); ++i){ - Unit *unit= world->getFaction(factionIndex)->getUnit(i); - if(unit->getPos().dist(refUnit->getPosNotThreadSafe())getType()==refUnit->getType() && unit->isOperative()==refUnit->isOperative() ) - { - units.push_back(unit); + if (random.randRange(0, 1) == 0) { + SoundRenderer::getInstance().playFx( + selection.getFrontUnit()->getType()->getCommandSound(), + selection.getFrontUnit()->getCurrMidHeightVector(), + gameCamera->getPos()); } } } - bool shiftDown= isKeyDown(vkShift); - bool controlDown= isKeyDown(vkControl); + void Gui::centerCameraOnSelection() { + if (selection.isEmpty() == false) { + Vec3f refPos = selection.getRefPos(); + gameCamera->centerXZ(refPos.x, refPos.z); + } + } - if(!shiftDown && !controlDown){ - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.clear()\n",__FILE__,__FUNCTION__,__LINE__); + void Gui::selectInterestingUnit(InterestingUnitType iut) { + const Faction *thisFaction = world->getThisFaction(); + const Unit* previousUnit = NULL; + bool previousFound = true; + + //start at the next harvester + if (selection.getCount() == 1) { + const Unit* refUnit = selection.getFrontUnit(); + + if (refUnit->isInteresting(iut)) { + previousUnit = refUnit; + previousFound = false; + } + } + + //clear selection selection.clear(); - } - if(!controlDown){ - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.select(units)\n",__FILE__,__FUNCTION__,__LINE__); - selection.select(units,shiftDown); - if(!selection.isEmpty()){ - selectedResourceObject=NULL; + //search + for (int index = 0; index < thisFaction->getUnitCount(); ++index) { + Unit* unit = thisFaction->getUnit(index); + + if (previousFound == true) { + if (unit->isInteresting(iut)) { + selection.select(unit, false); + break; + } + } else { + if (unit == previousUnit) { + previousFound = true; + } + } + } + + //search again if we have a previous + if (selection.isEmpty() && previousUnit != NULL && previousFound == true) { + for (int index = 0; index < thisFaction->getUnitCount(); ++index) { + Unit* unit = thisFaction->getUnit(index); + + if (unit->isInteresting(iut)) { + selection.select(unit, false); + break; + } + } } } - else{ - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] selection.unSelect(units)\n",__FILE__,__FUNCTION__,__LINE__); - selection.unSelect(units); - } - } -} -bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit){ - Selection::UnitContainer uc; - Renderer &renderer= Renderer::getInstance(); - const Object* obj= NULL; - renderer.computeSelected(uc, obj, true, screenPos, screenPos); - validPosObjWorld= false; + void Gui::clickCommonCommand(CommandClass commandClass) { + for (int index = 0; index < Display::downCellCount; ++index) { + const CommandType *ct = display.getCommandType(index); - if(uc.empty() == false){ - targetUnit= getRelevantObjectFromSelection(&uc); - targetPos= targetUnit->getPosNotThreadSafe(); - // we need to respect cellmaps. Searching for a cell which is really occupied - int size=targetUnit->getType()->getSize(); - bool foundUnit=false; - for ( int x= 0;xgetMap()->getCell(Vec2i(targetPos.x+x,targetPos.y+y))->getUnit(targetUnit->getType()->getField())==targetUnit){ - targetPos=Vec2i(targetPos.x+x,targetPos.y+y); - foundUnit=true; + if ((ct != NULL && ct->getClass() == commandClass) || + display.getCommandClass(index) == commandClass) { + + mouseDownDisplayUnitSkills(index); break; } } - if(foundUnit) break; + computeDisplay(); } - highlightedUnitId=targetUnit->getId(); - getHighlightedUnit()->resetHighlight(); - return true; - } - else if(obj != NULL){ - targetUnit= NULL; - highlightedResourceObjectPos=Map::toSurfCoords(obj->getMapPos()); - Object *selObj = getHighlightedResourceObject(); - if(selObj != NULL) { - selObj->resetHighlight(); - // get real click pos - targetPos=game->getMouseCellPos(); - //validPosObjWorld= true; - //posObjWorld = targetPos; + void Gui::mouseDownDisplayUnitSkills(int posDisplay) { + if (selection.isEmpty() == false) { + if (posDisplay != cancelPos) { + if (posDisplay != meetingPointPos) { + const Unit *unit = selection.getFrontUnit(); - int tx= targetPos.x; - int ty= targetPos.y; + //uniform selection + if (selection.isUniform()) { + const CommandType *ct = display.getCommandType(posDisplay); - int ox= obj->getMapPos().x; - int oy= obj->getMapPos().y; + // try to switch to next attack type + if (activeCommandClass == ccAttack && activeCommandType != NULL) { - Resource* clickedRecource= world->getMap()->getSurfaceCell(Map::toSurfCoords(obj->getMapPos()))->getResource(); + int maxI = unit->getType()->getCommandTypeCount(); + int cmdTypeId = activeCommandType->getId(); + int cmdTypeIdNext = cmdTypeId + 1; - // lets see if the click had the same Resource - if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()){ - // same ressource is meant, so use the user selected position - return true; - } - else{// calculate a valid resource position which is as near as possible to the selected position - Vec2i testIt= Vec2i(obj->getMapPos()); - /////////////// - // test both // - /////////////// - if(ty < oy){ - testIt.y--; - } - else if(ty > oy){ - testIt.y++; - } - if(tx < ox){ - testIt.x--; - } - else if(tx > ox){ - testIt.x++; - } + while (cmdTypeIdNext != cmdTypeId) { + if (cmdTypeIdNext >= maxI) { + cmdTypeIdNext = 0; + } + const CommandType *ctype = display.getCommandType(cmdTypeIdNext); + if (ctype != NULL && ctype->getClass() == ccAttack) { + if (ctype != NULL && unit->getFaction()->reqsOk(ctype)) { + posDisplay = cmdTypeIdNext; + ct = display.getCommandType(posDisplay); + break; + } + } + cmdTypeIdNext++; + } + } - if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ - // same ressource is meant, so use this position - targetPos= testIt; - //posObjWorld= targetPos; - return true; - } - else{ - testIt= Vec2i(obj->getMapPos()); - } + if (ct != NULL && unit->getFaction()->reqsOk(ct)) { + activeCommandType = ct; + activeCommandClass = activeCommandType->getClass(); + } else { + posDisplay = invalidPos; + activeCommandType = NULL; + activeCommandClass = ccStop; + return; + } + } - ///////////////// - // test y-only // - ///////////////// - if(ty < oy){ - testIt.y--; - } - else if(ty > oy){ - testIt.y++; - } - if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ - // same ressource is meant, so use this position - targetPos= testIt; - //posObjWorld= targetPos; - return true; - } - else{ - testIt= Vec2i(obj->getMapPos()); - } + //non uniform selection + else { + activeCommandType = NULL; + activeCommandClass = display.getCommandClass(posDisplay); + } - ///////////////// - // test x-only // - ///////////////// - if(tx < ox){ - testIt.x--; - } - else if(tx > ox){ - testIt.x++; - } - if(clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()){ - // same ressource is meant, so use this position - targetPos= testIt; - //posObjWorld= targetPos; - return true; + //give orders depending on command type + if (!selection.isEmpty()) { + const CommandType *ct = selection.getUnit(0)->getType()->getFirstCtOfClass(activeCommandClass); + if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { + assert(selection.isUniform()); + selectingBuilding = true; + } else if (ct->getClicks() == cOne) { + invalidatePosObjWorld(); + giveOneClickOrders(); + } else { + selectingPos = true; + activePos = posDisplay; + } + } + } else { + activePos = posDisplay; + selectingMeetingPoint = true; + } + } else { + commander->tryCancelCommand(&selection); } } - // give up and use the object position; - targetPos= obj->getMapPos(); - posObjWorld= targetPos; + } + + void Gui::mouseDownDisplayUnitBuild(int posDisplay) { + //int factionIndex = world->getThisFactionIndex(); + + if (posDisplay == cancelPos) { + resetState(); + } else { + if (activeCommandType != NULL && + activeCommandType->getClass() == ccBuild) { + + const BuildCommandType *bct = dynamic_cast(activeCommandType); + if (bct != NULL) { + const UnitType *ut = bct->getBuilding(posDisplay); + + const Unit *unit = selection.getFrontUnit(); + if (unit != NULL && unit->getFaction() != NULL) { + + if (selection.canSelectUnitFactionCheck(unit) == true && + unit->getFaction()->reqsOk(ut)) { + + choosenBuildingType = ut; + selectingPos = true; + selectedBuildingFacing = CardinalDir(CardinalDir::NORTH); + activePos = posDisplay; + } + } + } + } + } + } + + + string Gui::computeDefaultInfoString() { + string result = ""; + + if (selection.isUniform()) { + if (selection.isObserver() || selection.isCommandable()) { + // default is the description extension + result = selection.getFrontUnit()->getDescExtension(game->showTranslatedTechTree()); + } + } + return result; + } + + + void Gui::computeInfoString(int posDisplay) { + + Lang &lang = Lang::getInstance(); + + lastPosDisplay = posDisplay; + + display.setInfoText(computeDefaultInfoString()); + + if (posDisplay != invalidPos && selection.isCommandable()) { + if (!selectingBuilding) { + if (posDisplay == cancelPos) { + display.setInfoText(lang.getString("Cancel")); + } else if (posDisplay == meetingPointPos) { + display.setInfoText(lang.getString("MeetingPoint")); + } else { + //uniform selection + if (selection.isUniform()) { + const Unit *unit = selection.getFrontUnit(); + const CommandType *ct = display.getCommandType(posDisplay); + + if (ct != NULL) { + if (unit->getFaction()->reqsOk(ct)) { + display.setInfoText(ct->getDesc(unit->getTotalUpgrade(), game->showTranslatedTechTree())); + } else { + display.setInfoText(ct->getReqDesc(game->showTranslatedTechTree())); + if (ct->getClass() == ccUpgrade) { + string text = ""; + const UpgradeCommandType *uct = static_cast(ct); + if (unit->getFaction()->getUpgradeManager()->isUpgrading(uct->getProducedUpgrade())) { + text = lang.getString("Upgrading") + "\n\n"; + } else if (unit->getFaction()->getUpgradeManager()->isUpgraded(uct->getProducedUpgrade())) { + text = lang.getString("AlreadyUpgraded") + "\n\n"; + } + display.setInfoText(text + ct->getReqDesc(game->showTranslatedTechTree())); + } + //locked by scenario + else if (ct->getClass() == ccProduce) { + string text = ""; + const ProduceCommandType *pct = static_cast(ct); + if (unit->getFaction()->isUnitLocked(pct->getProducedUnit())) { + display.setInfoText(lang.getString("LockedByScenario") + "\n\n" + ct->getReqDesc(game->showTranslatedTechTree())); + } + } else if (ct->getClass() == ccMorph) { + const MorphCommandType *mct = static_cast(ct); + if (unit->getFaction()->isUnitLocked(mct->getMorphUnit())) { + display.setInfoText(lang.getString("LockedByScenario") + "\n\n" + ct->getReqDesc(game->showTranslatedTechTree())); + } + } + } + } + } + + //non uniform selection + else { + const UnitType *ut = selection.getFrontUnit()->getType(); + CommandClass cc = display.getCommandClass(posDisplay); + if (cc != ccNull) { + display.setInfoText(lang.getString("CommonCommand") + ": " + ut->getFirstCtOfClass(cc)->toString(true)); + } + } + } + } else { + if (posDisplay == cancelPos) { + display.setInfoText(lang.getString("Return")); + } else { + if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { + //locked by scenario + const BuildCommandType *bct = static_cast(activeCommandType); + const Unit *unit = selection.getFrontUnit(); + if (unit->getFaction()->isUnitLocked(bct->getBuilding(posDisplay))) { + display.setInfoText(lang.getString("LockedByScenario") + "\n\n" + bct->getBuilding(posDisplay)->getReqDesc(game->showTranslatedTechTree())); + } else { + bool translatedValue = game->showTranslatedTechTree(); + const UnitType *building = bct->getBuilding(posDisplay); + string str = lang.getString("BuildSpeed", (translatedValue == true ? "" : "english")) + ": " + intToStr(bct->getBuildSkillType()->getSpeed()) + "\n"; + str += "" + Lang::getInstance().getString("TimeSteps", (translatedValue == true ? "" : "english")) + ": " + intToStr(building->getProductionTime()) + "\n"; + int64 speed = bct->getBuildSkillType()->getSpeed() + bct->getBuildSkillType()->getTotalSpeed(unit->getTotalUpgrade()); + int64 time = building->getProductionTime(); + int64 seconds = time * 100 / speed; + str += "" + Lang::getInstance().getString("Time", (translatedValue == true ? "" : "english")) + ": " + intToStr(seconds); + str += "\n\n"; + str += building->getReqDesc(translatedValue); + display.setInfoText(str); + } + } + } + } + } + } + + void Gui::computeDisplay() { + + //printf("Start ===> computeDisplay()\n"); + Lang &lang = Lang::getInstance(); + //init + display.clear(); + + // ================ PART 1 ================ + const Object *selectedResourceObject = getSelectedResourceObject(); + if (selection.isEmpty() && selectedResourceObject != NULL && selectedResourceObject->getResource() != NULL) { + Resource *r = selectedResourceObject->getResource(); + display.setTitle(r->getType()->getName(game->showTranslatedTechTree())); + display.setText(lang.getString("Amount") + ": " + intToStr(r->getAmount()) + " / " + intToStr(r->getType()->getDefResPerPatch())); + //display.setProgressBar(r->); + display.setUpImage(0, r->getType()->getImage()); + } else { + //title, text and progress bar + if (selection.getCount() == 1) { + display.setTitle(selection.getFrontUnit()->getFullName(game->showTranslatedTechTree())); + display.setText(selection.getFrontUnit()->getDesc(game->showTranslatedTechTree())); + display.setProgressBar(selection.getFrontUnit()->getProductionPercent()); + } + + //portraits + int unitIndex = 0; + for (unitIndex = 0; unitIndex < selection.getCount(); ++unitIndex) { + try { + const Unit *unit = selection.getUnit(unitIndex); + if (unit == NULL) { + throw megaglest_runtime_error("unit == NULL"); + } + if (unit->getType() == NULL) { + throw megaglest_runtime_error("unit->getType() == NULL"); + } + if (unit->getType()->getImage() == NULL) { + throw megaglest_runtime_error("unit->getType()->getImage()"); + } + + display.setUpImage(unitIndex, unit->getType()->getImage()); + } catch (exception &ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error in unit selection for index: %d error [%s]", unitIndex, ex.what()); + throw megaglest_runtime_error(szBuf, true); + } + } + + // ================ PART 2 ================ + + if (selectingPos || selectingMeetingPoint) { + //printf("selectingPos || selectingMeetingPoint\n"); + display.setDownSelectedPos(activePos); + } + + //printf("computeDisplay selection.isCommandable() = %d\n",selection.isCommandable()); + + if (selection.isCommandable()) { + //printf("selection.isComandable()\n"); + + if (selectingBuilding == false) { + + //cancel button + const Unit *u = selection.getFrontUnit(); + const UnitType *ut = u->getType(); + if (selection.isCancelable()) { + //printf("selection.isCancelable() commandcount = %d\n",selection.getUnit(0)->getCommandSize()); + if (selection.getUnit(0)->getCommandSize() > 0) { + //printf("Current Command [%s]\n",selection.getUnit(0)->getCurrCommand()->toString().c_str()); + } + + display.setDownImage(cancelPos, ut->getCancelImage()); + display.setDownLighted(cancelPos, true); + } + + //meeting point + if (selection.isMeetable()) { + //printf("selection.isMeetable()\n"); + + display.setDownImage(meetingPointPos, ut->getMeetingPointImage()); + display.setDownLighted(meetingPointPos, true); + } + + + //printf("computeDisplay selection.isUniform() = %d\n",selection.isUniform()); + + if (selection.isUniform()) { + //printf("selection.isUniform()\n"); + + //uniform selection + if (u->isBuilt()) { + //printf("u->isBuilt()\n"); + + int morphPos = 8; + for (int i = 0; i < ut->getCommandTypeCount(); ++i) { + int displayPos = i; + const CommandType *ct = ut->getCommandType(i); + if (ct->getClass() == ccMorph) { + displayPos = morphPos++; + } + + //printf("computeDisplay i = %d displayPos = %d morphPos = %d ct->getClass() = %d [%s]\n",i,displayPos,morphPos,ct->getClass(),ct->getName().c_str()); + + display.setDownImage(displayPos, ct->getImage()); + display.setCommandType(displayPos, ct); + display.setCommandClass(displayPos, ct->getClass()); + display.setDownLighted(displayPos, u->getFaction()->reqsOk(ct)); + } + } + } else { + //printf("selection.isUniform() == FALSE\n"); + + //non uniform selection + int lastCommand = 0; + for (int i = 0; i < ccCount; ++i) { + CommandClass cc = static_cast (i); + + //printf("computeDisplay i = %d cc = %d isshared = %d lastCommand = %d\n",i,cc,isSharedCommandClass(cc),lastCommand); + + if (isSharedCommandClass(cc) && cc != ccBuild) { + display.setDownLighted(lastCommand, true); + display.setDownImage(lastCommand, ut->getFirstCtOfClass(cc)->getImage()); + display.setCommandClass(lastCommand, cc); + lastCommand++; + } + } + } + } else { + + //selecting building + const Unit *unit = selection.getFrontUnit(); + if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { + const BuildCommandType* bct = static_cast (activeCommandType); + for (int i = 0; i < bct->getBuildingCount(); ++i) { + display.setDownImage(i, bct->getBuilding(i)->getImage()); + display.setDownLighted(i, unit->getFaction()->reqsOk(bct->getBuilding(i))); + } + display.setDownImage(cancelPos, selection.getFrontUnit()->getType()->getCancelImage()); + display.setDownLighted(cancelPos, true); + } + } + } + } + + // refresh other things + if (!isSelecting() && !isSelectingPos()) { + if (!isSelectingPos() && lastPosDisplay == invalidPos) { + computeInfoString(lastPosDisplay); + } + } + + } + + int Gui::computePosDisplay(int x, int y) { + int posDisplay = display.computeDownIndex(x, y); + + //printf("computePosDisplay x = %d y = %d posDisplay = %d Display::downCellCount = %d cc = %d ct = %p\n",x,y,posDisplay,Display::downCellCount,display.getCommandClass(posDisplay),display.getCommandType(posDisplay)); + + if (posDisplay < 0 || posDisplay >= Display::downCellCount) { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } else if (selection.isCommandable()) { + if (posDisplay != cancelPos) { + if (posDisplay != meetingPointPos) { + if (selectingBuilding == false) { + //standard selection + if (display.getCommandClass(posDisplay) == ccNull && display.getCommandType(posDisplay) == NULL) { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } + } else { + //building selection + if (activeCommandType != NULL && activeCommandType->getClass() == ccBuild) { + const BuildCommandType *bct = static_cast(activeCommandType); + if (posDisplay >= bct->getBuildingCount()) { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } + } + } + } else { + //check meeting point + if (!selection.isMeetable()) { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } + } + } else { + //check cancel button + if (selection.isCancelable() == false) { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } + } + } else { + posDisplay = invalidPos; + //printf("In [%s:%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); + } + + //printf("computePosDisplay returning = %d\n",posDisplay); + + return posDisplay; + } + + void Gui::addOrdersResultToConsole(CommandClass cc, std::pair result) { + + switch (result.first) { + case crSuccess: + break; + case crFailReqs: + switch (cc) { + case ccBuild: + console->addStdMessage("BuildingNoReqs", result.second); + break; + case ccProduce: + console->addStdMessage("UnitNoReqs", result.second); + break; + case ccMorph: + console->addStdMessage("MorphNoReqs", result.second); + break; + case ccUpgrade: + console->addStdMessage("UpgradeNoReqs", result.second); + break; + default: + break; + } + break; + case crFailRes: + switch (cc) { + case ccBuild: + console->addStdMessage("BuildingNoRes", result.second); + break; + case ccProduce: + console->addStdMessage("UnitNoRes", result.second); + break; + case ccMorph: + console->addStdMessage("MorphNoRes", result.second); + break; + case ccUpgrade: + console->addStdMessage("UpgradeNoRes", result.second); + break; + default: + break; + } + break; + + case crFailUndefined: + console->addStdMessage("InvalidOrder", result.second); + break; + + case crSomeFailed: + console->addStdMessage("SomeOrdersFailed", result.second); + break; + } + } + + bool Gui::isSharedCommandClass(CommandClass commandClass) { + for (int i = 0; i < selection.getCount(); ++i) { + const Unit *unit = selection.getUnit(i); + const CommandType *ct = unit->getType()->getFirstCtOfClass(commandClass); + if (ct == NULL || !unit->getFaction()->reqsOk(ct)) + return false; + } return true; } - else { - return false; - } - } - else{ - targetUnit= NULL; - targetPos=game->getMouseCellPos(); - if(game->isValidMouseCellPos()){ - validPosObjWorld= true; - posObjWorld= targetPos; - if(world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource() != NULL) { - highlightedResourceObjectPos=Map::toSurfCoords(targetPos); + void Gui::computeSelected(bool doubleClick, bool force) { + Selection::UnitContainer units; + + if (force || (lastQuadCalcFrame + selectionCalculationFrameSkip < game->getTotalRenderFps())) { + lastQuadCalcFrame = game->getTotalRenderFps(); + const Object *selectedResourceObject = NULL; + selectedResourceObjectPos = Vec2i(-1, -1); + if (selectionQuad.isEnabled() && selectionQuad.getPosUp().dist(selectionQuad.getPosDown()) < minQuadSize) { + //Renderer::getInstance().computeSelected(units, selectedResourceObject, true, selectionQuad.getPosDown(), selectionQuad.getPosDown()); + Renderer::getInstance().computeSelected(units, selectedResourceObject, true, selectionQuad.getPosDown(), selectionQuad.getPosDown()); + if (selectedResourceObject != NULL) { + selectedResourceObjectPos = Map::toSurfCoords(selectedResourceObject->getMapPos()); + } + } else { + Renderer::getInstance().computeSelected(units, selectedResourceObject, false, selectionQuad.getPosDown(), selectionQuad.getPosUp()); + } + selectingBuilding = false; + activeCommandType = NULL; + + //select all units of the same type if double click + if (doubleClick && units.empty() == false) { + const Unit *refUnit = getRelevantObjectFromSelection(&units); + int factionIndex = refUnit->getFactionIndex(); + for (int i = 0; i < world->getFaction(factionIndex)->getUnitCount(); ++i) { + Unit *unit = world->getFaction(factionIndex)->getUnit(i); + if (unit->getPos().dist(refUnit->getPosNotThreadSafe()) < doubleClickSelectionRadius && + unit->getType() == refUnit->getType() && unit->isOperative() == refUnit->isOperative()) { + units.push_back(unit); + } + } + } + + bool shiftDown = isKeyDown(vkShift); + bool controlDown = isKeyDown(vkControl); + + if (!shiftDown && !controlDown) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.clear()\n",__FILE__,__FUNCTION__,__LINE__); + selection.clear(); + } + + if (!controlDown) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to call selection.select(units)\n",__FILE__,__FUNCTION__,__LINE__); + selection.select(units, shiftDown); + if (!selection.isEmpty()) { + selectedResourceObject = NULL; + } + } else { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] selection.unSelect(units)\n",__FILE__,__FUNCTION__,__LINE__); + selection.unSelect(units); + } + } + } + + bool Gui::computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit) { + Selection::UnitContainer uc; + Renderer &renderer = Renderer::getInstance(); + const Object* obj = NULL; + renderer.computeSelected(uc, obj, true, screenPos, screenPos); + validPosObjWorld = false; + + if (uc.empty() == false) { + targetUnit = getRelevantObjectFromSelection(&uc); + targetPos = targetUnit->getPosNotThreadSafe(); + // we need to respect cellmaps. Searching for a cell which is really occupied + int size = targetUnit->getType()->getSize(); + bool foundUnit = false; + for (int x = 0; x < size; ++x) { + for (int y = 0; y < size; ++y) { + if (world->getMap()->getCell(Vec2i(targetPos.x + x, targetPos.y + y))->getUnit(targetUnit->getType()->getField()) == targetUnit) { + targetPos = Vec2i(targetPos.x + x, targetPos.y + y); + foundUnit = true; + break; + } + } + if (foundUnit) break; + } + highlightedUnitId = targetUnit->getId(); + getHighlightedUnit()->resetHighlight(); + return true; + } else if (obj != NULL) { + targetUnit = NULL; + highlightedResourceObjectPos = Map::toSurfCoords(obj->getMapPos()); Object *selObj = getHighlightedResourceObject(); - if(selObj != NULL) { + if (selObj != NULL) { selObj->resetHighlight(); + // get real click pos + targetPos = game->getMouseCellPos(); + //validPosObjWorld= true; + //posObjWorld = targetPos; + + int tx = targetPos.x; + int ty = targetPos.y; + + int ox = obj->getMapPos().x; + int oy = obj->getMapPos().y; + + Resource* clickedRecource = world->getMap()->getSurfaceCell(Map::toSurfCoords(obj->getMapPos()))->getResource(); + + // lets see if the click had the same Resource + if (clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource()) { + // same ressource is meant, so use the user selected position + return true; + } else {// calculate a valid resource position which is as near as possible to the selected position + Vec2i testIt = Vec2i(obj->getMapPos()); + /////////////// + // test both // + /////////////// + if (ty < oy) { + testIt.y--; + } else if (ty > oy) { + testIt.y++; + } + if (tx < ox) { + testIt.x--; + } else if (tx > ox) { + testIt.x++; + } + + if (clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()) { + // same ressource is meant, so use this position + targetPos = testIt; + //posObjWorld= targetPos; + return true; + } else { + testIt = Vec2i(obj->getMapPos()); + } + + ///////////////// + // test y-only // + ///////////////// + if (ty < oy) { + testIt.y--; + } else if (ty > oy) { + testIt.y++; + } + if (clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()) { + // same ressource is meant, so use this position + targetPos = testIt; + //posObjWorld= targetPos; + return true; + } else { + testIt = Vec2i(obj->getMapPos()); + } + + ///////////////// + // test x-only // + ///////////////// + if (tx < ox) { + testIt.x--; + } else if (tx > ox) { + testIt.x++; + } + if (clickedRecource == world->getMap()->getSurfaceCell(Map::toSurfCoords(testIt))->getResource()) { + // same ressource is meant, so use this position + targetPos = testIt; + //posObjWorld= targetPos; + return true; + } + } + // give up and use the object position; + targetPos = obj->getMapPos(); + posObjWorld = targetPos; + return true; + } else { + return false; + } + } else { + targetUnit = NULL; + targetPos = game->getMouseCellPos(); + if (game->isValidMouseCellPos()) { + validPosObjWorld = true; + posObjWorld = targetPos; + + if (world->getMap()->getSurfaceCell(Map::toSurfCoords(targetPos))->getResource() != NULL) { + highlightedResourceObjectPos = Map::toSurfCoords(targetPos); + + Object *selObj = getHighlightedResourceObject(); + if (selObj != NULL) { + selObj->resetHighlight(); + } + } + + return true; + } else { + return false; } } - - return true; } - else{ - return false; + + Unit* Gui::getRelevantObjectFromSelection(Selection::UnitContainer *uc) { + Unit *resultUnit = NULL; + for (int i = 0; i < (int) uc->size(); ++i) { + resultUnit = uc->at(i); + if (resultUnit->getType()->hasSkillClass(scMove)) {// moving units are more relevant than non moving ones + break; + } + } + return resultUnit; } - } -} -Unit* Gui::getRelevantObjectFromSelection(Selection::UnitContainer *uc){ - Unit *resultUnit=NULL; - for(int i= 0; i < (int)uc->size(); ++i) { - resultUnit= uc->at(i); - if(resultUnit->getType()->hasSkillClass(scMove)){// moving units are more relevant than non moving ones - break; + void Gui::saveGame(XmlNode *rootNode) const { + std::map mapTagReplacements; + XmlNode *guiNode = rootNode->addChild("Gui"); + + guiNode->addAttribute("random", intToStr(random.getLastNumber()), mapTagReplacements); + guiNode->addAttribute("posObjWorld", posObjWorld.getString(), mapTagReplacements); + guiNode->addAttribute("validPosObjWorld", intToStr(validPosObjWorld), mapTagReplacements); + if (choosenBuildingType != NULL) { + const Faction* thisFaction = world->getThisFaction(); + guiNode->addAttribute("choosenBuildingType", choosenBuildingType->getName(false), mapTagReplacements); + guiNode->addAttribute("choosenBuildingTypeFactionIndex", intToStr(thisFaction->getIndex()), mapTagReplacements); + } + if (activeCommandType != NULL) { + guiNode->addAttribute("activeCommandType", activeCommandType->getName(false), mapTagReplacements); + } + + guiNode->addAttribute("activeCommandClass", intToStr(activeCommandClass), mapTagReplacements); + guiNode->addAttribute("activePos", intToStr(activePos), mapTagReplacements); + guiNode->addAttribute("lastPosDisplay", intToStr(lastPosDisplay), mapTagReplacements); + display.saveGame(guiNode); + selection.saveGame(guiNode); + guiNode->addAttribute("lastQuadCalcFrame", intToStr(lastQuadCalcFrame), mapTagReplacements); + guiNode->addAttribute("selectionCalculationFrameSkip", intToStr(selectionCalculationFrameSkip), mapTagReplacements); + guiNode->addAttribute("minQuadSize", intToStr(minQuadSize), mapTagReplacements); + guiNode->addAttribute("lastGroupRecall", intToStr(lastGroupRecall), mapTagReplacements); + guiNode->addAttribute("selectingBuilding", intToStr(selectingBuilding), mapTagReplacements); + guiNode->addAttribute("selectingPos", intToStr(selectingPos), mapTagReplacements); + guiNode->addAttribute("selectingMeetingPoint", intToStr(selectingMeetingPoint), mapTagReplacements); + guiNode->addAttribute("selectedBuildingFacing", intToStr(selectedBuildingFacing), mapTagReplacements); } + + void Gui::loadGame(const XmlNode *rootNode, World *world) { + const XmlNode *guiNode = rootNode->getChild("Gui"); + + random.setLastNumber(guiNode->getAttribute("random")->getIntValue()); + posObjWorld = Vec2i::strToVec2(guiNode->getAttribute("posObjWorld")->getValue()); + validPosObjWorld = guiNode->getAttribute("validPosObjWorld")->getIntValue() != 0; + if (guiNode->hasAttribute("choosenBuildingType") == true) { + string unitType = guiNode->getAttribute("choosenBuildingType")->getValue(); + int factionIndex = guiNode->getAttribute("choosenBuildingTypeFactionIndex")->getIntValue(); + choosenBuildingType = world->getFaction(factionIndex)->getType()->getUnitType(unitType); + } + activePos = guiNode->getAttribute("activePos")->getIntValue(); + lastPosDisplay = guiNode->getAttribute("lastPosDisplay")->getIntValue(); + display.loadGame(guiNode); + selection.loadGame(guiNode, world); + // don't load this! lastQuadCalcFrame = guiNode->getAttribute("lastQuadCalcFrame")->getIntValue(); + lastQuadCalcFrame = game->getTotalRenderFps(); + selectionCalculationFrameSkip = guiNode->getAttribute("selectionCalculationFrameSkip")->getIntValue(); + minQuadSize = guiNode->getAttribute("minQuadSize")->getIntValue(); + lastGroupRecall = guiNode->getAttribute("lastGroupRecall")->getIntValue(); + } + } - return resultUnit; -} - -void Gui::saveGame(XmlNode *rootNode) const { - std::map mapTagReplacements; - XmlNode *guiNode = rootNode->addChild("Gui"); - - guiNode->addAttribute("random",intToStr(random.getLastNumber()), mapTagReplacements); - guiNode->addAttribute("posObjWorld",posObjWorld.getString(), mapTagReplacements); - guiNode->addAttribute("validPosObjWorld",intToStr(validPosObjWorld), mapTagReplacements); - if(choosenBuildingType != NULL) { - const Faction* thisFaction= world->getThisFaction(); - guiNode->addAttribute("choosenBuildingType",choosenBuildingType->getName(false), mapTagReplacements); - guiNode->addAttribute("choosenBuildingTypeFactionIndex",intToStr(thisFaction->getIndex()), mapTagReplacements); - } - if(activeCommandType != NULL) { - guiNode->addAttribute("activeCommandType",activeCommandType->getName(false), mapTagReplacements); - } - - guiNode->addAttribute("activeCommandClass",intToStr(activeCommandClass), mapTagReplacements); - guiNode->addAttribute("activePos",intToStr(activePos), mapTagReplacements); - guiNode->addAttribute("lastPosDisplay",intToStr(lastPosDisplay), mapTagReplacements); - display.saveGame(guiNode); - selection.saveGame(guiNode); - guiNode->addAttribute("lastQuadCalcFrame",intToStr(lastQuadCalcFrame), mapTagReplacements); - guiNode->addAttribute("selectionCalculationFrameSkip",intToStr(selectionCalculationFrameSkip), mapTagReplacements); - guiNode->addAttribute("minQuadSize",intToStr(minQuadSize), mapTagReplacements); - guiNode->addAttribute("lastGroupRecall",intToStr(lastGroupRecall), mapTagReplacements); - guiNode->addAttribute("selectingBuilding",intToStr(selectingBuilding), mapTagReplacements); - guiNode->addAttribute("selectingPos",intToStr(selectingPos), mapTagReplacements); - guiNode->addAttribute("selectingMeetingPoint",intToStr(selectingMeetingPoint), mapTagReplacements); - guiNode->addAttribute("selectedBuildingFacing",intToStr(selectedBuildingFacing), mapTagReplacements); -} - -void Gui::loadGame(const XmlNode *rootNode, World *world) { - const XmlNode *guiNode = rootNode->getChild("Gui"); - - random.setLastNumber(guiNode->getAttribute("random")->getIntValue()); - posObjWorld = Vec2i::strToVec2(guiNode->getAttribute("posObjWorld")->getValue()); - validPosObjWorld = guiNode->getAttribute("validPosObjWorld")->getIntValue() != 0; - if(guiNode->hasAttribute("choosenBuildingType") == true) { - string unitType = guiNode->getAttribute("choosenBuildingType")->getValue(); - int factionIndex = guiNode->getAttribute("choosenBuildingTypeFactionIndex")->getIntValue(); - choosenBuildingType = world->getFaction(factionIndex)->getType()->getUnitType(unitType); - } - activePos = guiNode->getAttribute("activePos")->getIntValue(); - lastPosDisplay = guiNode->getAttribute("lastPosDisplay")->getIntValue(); - display.loadGame(guiNode); - selection.loadGame(guiNode,world); - // don't load this! lastQuadCalcFrame = guiNode->getAttribute("lastQuadCalcFrame")->getIntValue(); - lastQuadCalcFrame = game->getTotalRenderFps(); - selectionCalculationFrameSkip = guiNode->getAttribute("selectionCalculationFrameSkip")->getIntValue(); - minQuadSize = guiNode->getAttribute("minQuadSize")->getIntValue(); - lastGroupRecall = guiNode->getAttribute("lastGroupRecall")->getIntValue(); -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/gui/gui.h b/source/glest_game/gui/gui.h index c9c8905c9..0b888cfd5 100644 --- a/source/glest_game/gui/gui.h +++ b/source/glest_game/gui/gui.h @@ -26,213 +26,255 @@ using Shared::Util::RandomGen; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class Unit; -class World; -class CommandType; -class GameCamera; -class Game; + class Unit; + class World; + class CommandType; + class GameCamera; + class Game; -enum DisplayState{ - dsEmpty, - dsUnitSkills, - dsUnitBuild, - dsEnemy -}; + enum DisplayState { + dsEmpty, + dsUnitSkills, + dsUnitBuild, + dsEnemy + }; -// ===================================================== -// class Mouse3d -// ===================================================== + // ===================================================== + // class Mouse3d + // ===================================================== -class Mouse3d{ -public: - static const float fadeSpeed; + class Mouse3d { + public: + static const float fadeSpeed; -private: - bool enabled; - int rot; - float fade; + private: + bool enabled; + int rot; + float fade; -public: - Mouse3d(); + public: + Mouse3d(); - void enable(); - void update(); + void enable(); + void update(); - bool isEnabled() const {return enabled;} - float getFade() const {return fade;} - int getRot() const {return rot;} -}; + bool isEnabled() const { + return enabled; + } + float getFade() const { + return fade; + } + int getRot() const { + return rot; + } + }; -// ===================================================== -// class SelectionQuad -// ===================================================== + // ===================================================== + // class SelectionQuad + // ===================================================== -class SelectionQuad{ -private: - Vec2i posDown; - Vec2i posUp; - bool enabled; + class SelectionQuad { + private: + Vec2i posDown; + Vec2i posUp; + bool enabled; -public: - SelectionQuad(); + public: + SelectionQuad(); - bool isEnabled() const {return enabled;} - Vec2i getPosDown() const {return posDown;} - Vec2i getPosUp() const {return posUp;} + bool isEnabled() const { + return enabled; + } + Vec2i getPosDown() const { + return posDown; + } + Vec2i getPosUp() const { + return posUp; + } - void setPosDown(const Vec2i &posDown); - void setPosUp(const Vec2i &posUp); - void disable(); -}; + void setPosDown(const Vec2i &posDown); + void setPosUp(const Vec2i &posUp); + void disable(); + }; -// ===================================================== -// class Gui -// -/// In game GUI -// ===================================================== + // ===================================================== + // class Gui + // + /// In game GUI + // ===================================================== -class Gui { -public: - static const int maxSelBuff= 128*5; - static const int upgradeDisplayIndex= 8; + class Gui { + public: + static const int maxSelBuff = 128 * 5; + static const int upgradeDisplayIndex = 8; - static const int meetingPointPos= 14; - static const int cancelPos= 15; - static const int imageCount= 16; + static const int meetingPointPos = 14; + static const int cancelPos = 15; + static const int imageCount = 16; - static const int invalidPos= -1; - static const int doubleClickSelectionRadius= 20; + static const int invalidPos = -1; + static const int doubleClickSelectionRadius = 20; -private: - //External objects - RandomGen random; - const Commander *commander; - const World *world; - const Game *game; - GameCamera *gameCamera; - Console *console; + private: + //External objects + RandomGen random; + const Commander *commander; + const World *world; + const Game *game; + GameCamera *gameCamera; + Console *console; - //Positions - Vec2i posObjWorld; //world coords - bool validPosObjWorld; + //Positions + Vec2i posObjWorld; //world coords + bool validPosObjWorld; - //display - const UnitType *choosenBuildingType; - const CommandType *activeCommandType; - CommandClass activeCommandClass; - int activePos; - int lastPosDisplay; + //display + const UnitType *choosenBuildingType; + const CommandType *activeCommandType; + CommandClass activeCommandClass; + int activePos; + int lastPosDisplay; - //composite - Display display; - Mouse3d mouse3d; - Selection selection; - SelectionQuad selectionQuad; - int lastQuadCalcFrame; - int selectionCalculationFrameSkip; - int minQuadSize; + //composite + Display display; + Mouse3d mouse3d; + Selection selection; + SelectionQuad selectionQuad; + int lastQuadCalcFrame; + int selectionCalculationFrameSkip; + int minQuadSize; - Chrono lastGroupRecallTime; - int lastGroupRecall; + Chrono lastGroupRecallTime; + int lastGroupRecall; - //states - bool selectingBuilding; - bool selectingPos; - bool selectingMeetingPoint; + //states + bool selectingBuilding; + bool selectingPos; + bool selectingMeetingPoint; - CardinalDir selectedBuildingFacing; - Vec2i selectedResourceObjectPos; - Vec2i highlightedResourceObjectPos; - int highlightedUnitId; + CardinalDir selectedBuildingFacing; + Vec2i selectedResourceObjectPos; + Vec2i highlightedResourceObjectPos; + int highlightedUnitId; - Texture2D* hudTexture; + Texture2D* hudTexture; -public: - Gui(); - void init(Game *game); - void end(); + public: + Gui(); + void init(Game *game); + void end(); - //get - Vec2i getPosObjWorld() const {return posObjWorld;} - const UnitType *getBuilding() const; + //get + Vec2i getPosObjWorld() const { + return posObjWorld; + } + const UnitType *getBuilding() const; - Texture2D *getHudTexture() const {return hudTexture;} - void setHudTexture(Texture2D* value) { hudTexture = value;} + Texture2D *getHudTexture() const { + return hudTexture; + } + void setHudTexture(Texture2D* value) { + hudTexture = value; + } - const Mouse3d *getMouse3d() const {return &mouse3d;} - const Display *getDisplay() const {return &display;} - const Selection *getSelection() const {return &selection;} - Selection *getSelectionPtr() {return &selection;} - const Object *getSelectedResourceObject() const; - Object *getHighlightedResourceObject() const; - Unit *getHighlightedUnit() const; + const Mouse3d *getMouse3d() const { + return &mouse3d; + } + const Display *getDisplay() const { + return &display; + } + const Selection *getSelection() const { + return &selection; + } + Selection *getSelectionPtr() { + return &selection; + } + const Object *getSelectedResourceObject() const; + Object *getHighlightedResourceObject() const; + Unit *getHighlightedUnit() const; - const SelectionQuad *getSelectionQuad() const {return &selectionQuad;} - CardinalDir getSelectedFacing() const {return selectedBuildingFacing;} - bool isSelected(const Unit *unit) const {return selection.hasUnit(unit);} + const SelectionQuad *getSelectionQuad() const { + return &selectionQuad; + } + CardinalDir getSelectedFacing() const { + return selectedBuildingFacing; + } + bool isSelected(const Unit *unit) const { + return selection.hasUnit(unit); + } - bool isValidPosObjWorld() const {return validPosObjWorld;} - bool isSelecting() const {return selectionQuad.isEnabled();} - bool isSelectingPos() const {return selectingPos;} - bool isSelectingBuilding() const {return selectingBuilding;} - bool isPlacingBuilding() const; + bool isValidPosObjWorld() const { + return validPosObjWorld; + } + bool isSelecting() const { + return selectionQuad.isEnabled(); + } + bool isSelectingPos() const { + return selectingPos; + } + bool isSelectingBuilding() const { + return selectingBuilding; + } + bool isPlacingBuilding() const; - //set - void invalidatePosObjWorld(); + //set + void invalidatePosObjWorld(); - //events - void update(); - void tick(); - bool mouseValid(int x, int y); - void mouseDownLeftDisplay(int x, int y); - void mouseMoveDisplay(int x, int y); - void mouseMoveOutsideDisplay(); - void mouseDownLeftGraphics(int x, int y, bool prepared); - void mouseDownRightGraphics(int x, int y, bool prepared); - void mouseUpLeftGraphics(int x, int y); - void mouseMoveGraphics(int x, int y); - void mouseDoubleClickLeftGraphics(int x, int y); - void groupKey(int groupIndex); - void hotKey(SDL_KeyboardEvent key); + //events + void update(); + void tick(); + bool mouseValid(int x, int y); + void mouseDownLeftDisplay(int x, int y); + void mouseMoveDisplay(int x, int y); + void mouseMoveOutsideDisplay(); + void mouseDownLeftGraphics(int x, int y, bool prepared); + void mouseDownRightGraphics(int x, int y, bool prepared); + void mouseUpLeftGraphics(int x, int y); + void mouseMoveGraphics(int x, int y); + void mouseDoubleClickLeftGraphics(int x, int y); + void groupKey(int groupIndex); + void hotKey(SDL_KeyboardEvent key); - //misc - void switchToNextDisplayColor(); - void onSelectionChanged(); + //misc + void switchToNextDisplayColor(); + void onSelectionChanged(); - void saveGame(XmlNode *rootNode) const; - void loadGame(const XmlNode *rootNode, World *world); + void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode, World *world); -private: + private: - //orders - void giveDefaultOrders(int x, int y); - void giveDefaultOrders(int x, int y, const Unit *targetUnit, bool paintMouse3d); - void givePreparedDefaultOrders(int x, int y); - void giveOneClickOrders(); - void giveTwoClickOrders(int x, int y, bool prepared); + //orders + void giveDefaultOrders(int x, int y); + void giveDefaultOrders(int x, int y, const Unit *targetUnit, bool paintMouse3d); + void givePreparedDefaultOrders(int x, int y); + void giveOneClickOrders(); + void giveTwoClickOrders(int x, int y, bool prepared); - //hotkeys - void centerCameraOnSelection(); - void selectInterestingUnit(InterestingUnitType iut); - void clickCommonCommand(CommandClass commandClass); + //hotkeys + void centerCameraOnSelection(); + void selectInterestingUnit(InterestingUnitType iut); + void clickCommonCommand(CommandClass commandClass); - //misc - int computePosDisplay(int x, int y); - void computeDisplay(); - void resetState(); - void mouseDownDisplayUnitSkills(int posDisplay); - void mouseDownDisplayUnitBuild(int posDisplay); - void computeInfoString(int posDisplay); - string computeDefaultInfoString(); - void addOrdersResultToConsole(CommandClass cc, std::pair result); - bool isSharedCommandClass(CommandClass commandClass); - void computeSelected(bool doubleCkick,bool force); - bool computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit); - Unit* getRelevantObjectFromSelection(Selection::UnitContainer *uc); -}; + //misc + int computePosDisplay(int x, int y); + void computeDisplay(); + void resetState(); + void mouseDownDisplayUnitSkills(int posDisplay); + void mouseDownDisplayUnitBuild(int posDisplay); + void computeInfoString(int posDisplay); + string computeDefaultInfoString(); + void addOrdersResultToConsole(CommandClass cc, std::pair result); + bool isSharedCommandClass(CommandClass commandClass); + void computeSelected(bool doubleCkick, bool force); + bool computeTarget(const Vec2i &screenPos, Vec2i &targetPos, const Unit *&targetUnit); + Unit* getRelevantObjectFromSelection(Selection::UnitContainer *uc); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/gui/selection.cpp b/source/glest_game/gui/selection.cpp index 878099501..f0a2797fe 100644 --- a/source/glest_game/gui/selection.cpp +++ b/source/glest_game/gui/selection.cpp @@ -21,423 +21,424 @@ using namespace std; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class Selection -// ===================================================== + // ===================================================== + // class Selection + // ===================================================== -void Selection::init(Gui *gui, int factionIndex, int teamIndex, bool allowSharedTeamUnits) { - this->factionIndex = factionIndex; - this->teamIndex = teamIndex; - this->allowSharedTeamUnits = allowSharedTeamUnits; - this->gui = gui; - clear(); -} - -Selection::~Selection(){ - clear(); -} - -bool Selection::canSelectUnitFactionCheck(const Unit *unit) const { - //check if enemy - if(unit->getFactionIndex() != factionIndex) { - if(this->allowSharedTeamUnits == false || - unit->getFaction()->getTeam() != teamIndex) { - return false; + void Selection::init(Gui *gui, int factionIndex, int teamIndex, bool allowSharedTeamUnits) { + this->factionIndex = factionIndex; + this->teamIndex = teamIndex; + this->allowSharedTeamUnits = allowSharedTeamUnits; + this->gui = gui; + clear(); } - } - return true; -} + Selection::~Selection() { + clear(); + } -bool Selection::select(Unit *unit, bool addToSelection) { - bool result = false; - if((int)selectedUnits.size() >= Config::getInstance().getInt("MaxUnitSelectCount",intToStr(maxUnits).c_str())) { - return result; - } + bool Selection::canSelectUnitFactionCheck(const Unit *unit) const { + //check if enemy + if (unit->getFactionIndex() != factionIndex) { + if (this->allowSharedTeamUnits == false || + unit->getFaction()->getTeam() != teamIndex) { + return false; + } + } - // Fix Bug reported on sourceforge.net: Glest::Game::Selection::select crash with NULL pointer - ID: 3608835 - if(unit != NULL) { - //check if already selected - for(int index = 0; index < (int)selectedUnits.size(); ++index) { - if(selectedUnits[index] == unit) { + return true; + } + + bool Selection::select(Unit *unit, bool addToSelection) { + bool result = false; + if ((int) selectedUnits.size() >= Config::getInstance().getInt("MaxUnitSelectCount", intToStr(maxUnits).c_str())) { + return result; + } + + // Fix Bug reported on sourceforge.net: Glest::Game::Selection::select crash with NULL pointer - ID: 3608835 + if (unit != NULL) { + //check if already selected + for (int index = 0; index < (int) selectedUnits.size(); ++index) { + if (selectedUnits[index] == unit) { + return true; + } + } + + //check if dead + if (unit->isDead() == true) { + return false; + } + + //check if commandable + if (unit->getType()->isCommandable() == false && isEmpty() == false) { + return false; + } + + //check if multisel + if (unit->getType()->getMultiSelect() == false && isEmpty() == false) { + return false; + } + + //check if multitypesel + if (selectedUnits.size() > 0) { + bool isUnifromSelectOK = (selectedUnits.front()->getType() == unit->getType() && unit->isOperative() == selectedUnits.front()->isOperative()); + if (selectedUnits.front()->getType()->getUniformSelect() == true && !isUnifromSelectOK) { + if (addToSelection) + return false; + else + clear(); + } + + if (unit->getType()->getUniformSelect() == true + && !isUnifromSelectOK) { + return false; + } + } + + //check if enemy + if (canSelectUnitFactionCheck(unit) == false && isEmpty() == false) { + return false; + } + + //check existing enemy + //if(selectedUnits.size() == 1 && selectedUnits.front()->getFactionIndex() != factionIndex) { + if (selectedUnits.size() == 1 && canSelectUnitFactionCheck(selectedUnits.front()) == false) { + clear(); + } + + //check existing multisel + if (selectedUnits.size() == 1 && + selectedUnits.front()->getType()->getMultiSelect() == false) { + clear(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] unit selected [%s]\n", __FILE__, __FUNCTION__, __LINE__, unit->toString().c_str()); + + unit->addObserver(this); + + int unitTypeId = unit->getType()->getId(); + bool inserted = false; + for (int index = 0; index < (int) selectedUnits.size(); ++index) { + + int currentTypeId = selectedUnits[index]->getType()->getId(); + if (unitTypeId <= currentTypeId) { + + //place unit here + selectedUnits.insert(selectedUnits.begin() + index, unit); + inserted = true; + break; + } + } + if (inserted == false) { + selectedUnits.push_back(unit); + } + result = true; + gui->onSelectionChanged(); + } + + return result; + } + + void Selection::select(const UnitContainer &units, bool addToSelection) { + + //add units to gui + for (UnitIterator it = units.begin(); it != units.end(); ++it) { + select(*it, addToSelection); + } + } + + void Selection::unSelect(const UnitContainer &units) { + + //add units to gui + for (UnitIterator it = units.begin(); it != units.end(); ++it) { + for (int i = 0; i < (int) selectedUnits.size(); ++i) { + if (selectedUnits[i] == *it) { + unSelect(i); + } + } + } + } + + void Selection::unSelect(int i) { + selectedUnits.erase(selectedUnits.begin() + i); + gui->onSelectionChanged(); + } + + void Selection::clear() { + selectedUnits.clear(); + } + + bool Selection::isUniform() const { + if (selectedUnits.empty() == true) { return true; } - } - //check if dead - if(unit->isDead() == true) { - return false; - } + const UnitType *ut = selectedUnits.front()->getType(); - //check if commandable - if(unit->getType()->isCommandable() == false && isEmpty() == false) { - return false; - } - - //check if multisel - if(unit->getType()->getMultiSelect() == false && isEmpty() == false) { - return false; - } - - //check if multitypesel - if(selectedUnits.size() > 0) { - bool isUnifromSelectOK = ( selectedUnits.front()->getType() == unit->getType() && unit->isOperative() == selectedUnits.front()->isOperative()); - if(selectedUnits.front()->getType()->getUniformSelect() == true && !isUnifromSelectOK ) { - if(addToSelection) + for (int i = 0; i < (int) selectedUnits.size(); ++i) { + if (selectedUnits[i]->getType() != ut) { return false; - else - clear(); + } + } + return true; + } + + bool Selection::isEnemy() const { + return selectedUnits.size() == 1 && + //selectedUnits.front()->getFactionIndex() != factionIndex; + canSelectUnitFactionCheck(selectedUnits.front()) == false; + } + + bool Selection::isObserver() const { + return selectedUnits.size() == 1 && + (teamIndex == (GameConstants::maxPlayers - 1 + fpt_Observer)); + } + + bool Selection::isCommandable() const { + //printf("\n\n\n\n********* selection.isCommandable() ---> isEmpty() [%d] isEnemy() [%d] selectedUnits.size() [%d]\n\n",isEmpty(),isEnemy(),(int)selectedUnits.size()); + + return + isEmpty() == false && + isEnemy() == false && + (selectedUnits.size() == 1 && selectedUnits.front()->isAlive() == false) == false && + selectedUnits.front()->getType()->isCommandable(); + } + + bool Selection::isCancelable() const { + return + selectedUnits.size() > 1 || + (selectedUnits.size() == 1 && selectedUnits[0]->anyCommand(true)); + } + + bool Selection::isMeetable() const { + return + isUniform() && + isCommandable() && + selectedUnits.front()->getType()->getMeetingPoint(); + } + + Vec3f Selection::getRefPos() const { + return getFrontUnit()->getCurrMidHeightVector(); + } + + bool Selection::hasUnit(const Unit* unit) const { + return find(selectedUnits.begin(), selectedUnits.end(), unit) != selectedUnits.end(); + } + + bool Selection::assignGroup(int groupIndex, bool clearGroup, const UnitContainer *pUnits) { + if (groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); } - if (unit->getType()->getUniformSelect() == true - && !isUnifromSelectOK ) { + //clear group + if (true == clearGroup) { + groups[groupIndex].clear(); + } + + //assign new group + const UnitContainer *addUnits = &selectedUnits; + if (pUnits != NULL) { + addUnits = pUnits; + } + + for (unsigned int i = 0; i < addUnits->size(); ++i) { + if (false == addUnitToGroup(groupIndex, (*addUnits)[i])) { + // don't try to add more, group is maybe full + return false; + } + } + return true; + } + + /** + * returns false if unit cannot be added + */ + bool Selection::addUnitToGroup(int groupIndex, Unit *unit) { + if (groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + bool alreadyExists = false; + bool groupIsFull = (int) groups[groupIndex].size() >= Config::getInstance().getInt("MaxUnitSelectCount", intToStr(maxUnits).c_str()); + + for (int i = 0; i < (int) groups[groupIndex].size(); ++i) { + if (groups[groupIndex][i] == unit) { + alreadyExists = true; + break; + } + } + + if (alreadyExists) { + return true; + } + + // check for non Multiselect units + if ((int) groups[groupIndex].size() > 0) { + if (!unit->getType()->getMultiSelect()) { + //dont add single selection units to already filled group + return false; + } + Unit* unitInGroup = groups[groupIndex][0]; + if (!unitInGroup->getType()->getMultiSelect()) { + //dont add a unit to a group which has a single selection unit + return false; + } + } + + // check for uniformselect units + if ((int) groups[groupIndex].size() > 0) { + Unit* unitInGroup = groups[groupIndex][0]; + if (unit->getType()->getUniformSelect() || unitInGroup->getType()->getUniformSelect()) { + if (unit->isOperative() != unitInGroup->isOperative()) { + //dont add units that are not in same operative state + return false; + } + if (unitInGroup->getType() != unit->getType()) { + //dont add another unit to a group of uniform selection units + return false; + } + } + } + + if (unit != NULL && !groupIsFull) { + groups[groupIndex].push_back(unit); + return true; + } else { return false; } } - //check if enemy - if(canSelectUnitFactionCheck(unit) == false && isEmpty() == false) { - return false; - } - - //check existing enemy - //if(selectedUnits.size() == 1 && selectedUnits.front()->getFactionIndex() != factionIndex) { - if(selectedUnits.size() == 1 && canSelectUnitFactionCheck(selectedUnits.front()) == false) { - clear(); - } - - //check existing multisel - if(selectedUnits.size() == 1 && - selectedUnits.front()->getType()->getMultiSelect() == false) { - clear(); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit selected [%s]\n",__FILE__,__FUNCTION__,__LINE__,unit->toString().c_str()); - - unit->addObserver(this); - - int unitTypeId = unit->getType()->getId(); - bool inserted = false; - for(int index = 0; index < (int)selectedUnits.size(); ++index) { - - int currentTypeId = selectedUnits[index]->getType()->getId(); - if(unitTypeId <= currentTypeId) { - - //place unit here - selectedUnits.insert(selectedUnits.begin() + index,unit); - inserted = true; - break; + void Selection::removeUnitFromGroup(int groupIndex, int unitId) { + if (groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); } - } - if(inserted == false) { - selectedUnits.push_back(unit); - } - result = true; - gui->onSelectionChanged(); - } - return result; -} - -void Selection::select(const UnitContainer &units, bool addToSelection){ - - //add units to gui - for(UnitIterator it = units.begin(); it != units.end(); ++it) { - select(*it,addToSelection); - } -} - -void Selection::unSelect(const UnitContainer &units) { - - //add units to gui - for(UnitIterator it = units.begin(); it != units.end(); ++it) { - for(int i = 0; i < (int)selectedUnits.size(); ++i) { - if(selectedUnits[i] == *it) { - unSelect(i); - } - } - } -} - -void Selection::unSelect(int i) { - selectedUnits.erase(selectedUnits.begin() + i); - gui->onSelectionChanged(); -} - -void Selection::clear(){ - selectedUnits.clear(); -} - -bool Selection::isUniform() const{ - if(selectedUnits.empty() == true) { - return true; - } - - const UnitType *ut= selectedUnits.front()->getType(); - - for(int i = 0; i < (int)selectedUnits.size(); ++i) { - if(selectedUnits[i]->getType() != ut) { - return false; - } - } - return true; -} - -bool Selection::isEnemy() const { - return selectedUnits.size() == 1 && - //selectedUnits.front()->getFactionIndex() != factionIndex; - canSelectUnitFactionCheck(selectedUnits.front()) == false; -} - -bool Selection::isObserver() const { - return selectedUnits.size() == 1 && - (teamIndex == (GameConstants::maxPlayers -1 + fpt_Observer)); -} - -bool Selection::isCommandable() const { - //printf("\n\n\n\n********* selection.isCommandable() ---> isEmpty() [%d] isEnemy() [%d] selectedUnits.size() [%d]\n\n",isEmpty(),isEnemy(),(int)selectedUnits.size()); - - return - isEmpty() == false && - isEnemy() == false && - (selectedUnits.size() == 1 && selectedUnits.front()->isAlive() == false) == false && - selectedUnits.front()->getType()->isCommandable(); -} - -bool Selection::isCancelable() const { - return - selectedUnits.size() > 1 || - (selectedUnits.size() == 1 && selectedUnits[0]->anyCommand(true)); -} - -bool Selection::isMeetable() const{ - return - isUniform() && - isCommandable() && - selectedUnits.front()->getType()->getMeetingPoint(); -} - -Vec3f Selection::getRefPos() const{ - return getFrontUnit()->getCurrMidHeightVector(); -} - -bool Selection::hasUnit(const Unit* unit) const { - return find(selectedUnits.begin(), selectedUnits.end(), unit) != selectedUnits.end(); -} - -bool Selection::assignGroup(int groupIndex, bool clearGroup,const UnitContainer *pUnits) { - if(groupIndex < 0 || groupIndex >= maxGroups) { - throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); - } - - //clear group - if(true==clearGroup){ - groups[groupIndex].clear(); - } - - //assign new group - const UnitContainer *addUnits = &selectedUnits; - if(pUnits != NULL) { - addUnits = pUnits; - } - - for(unsigned int i = 0; i < addUnits->size(); ++i) { - if(false == addUnitToGroup(groupIndex,(*addUnits)[i])){ - // don't try to add more, group is maybe full - return false; - } - } - return true; -} - -/** - * returns false if unit cannot be added - */ -bool Selection::addUnitToGroup(int groupIndex,Unit *unit) { - if(groupIndex < 0 || groupIndex >= maxGroups) { - throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); - } - bool alreadyExists=false; - bool groupIsFull=(int)groups[groupIndex].size() >= Config::getInstance().getInt("MaxUnitSelectCount",intToStr(maxUnits).c_str()); - - for(int i = 0; i < (int)groups[groupIndex].size(); ++i) { - if(groups[groupIndex][i] == unit) { - alreadyExists=true; - break; - } - } - - if(alreadyExists){ - return true; - } - - // check for non Multiselect units - if((int)groups[groupIndex].size()>0 ){ - if( !unit->getType()->getMultiSelect()){ - //dont add single selection units to already filled group - return false; - } - Unit* unitInGroup=groups[groupIndex][0]; - if( !unitInGroup->getType()->getMultiSelect()){ - //dont add a unit to a group which has a single selection unit - return false; - } - } - - // check for uniformselect units - if((int)groups[groupIndex].size()>0 ) { - Unit* unitInGroup=groups[groupIndex][0]; - if( unit->getType()->getUniformSelect() || unitInGroup->getType()->getUniformSelect() ) { - if( unit->isOperative() != unitInGroup->isOperative()) { - //dont add units that are not in same operative state - return false; - } - if( unitInGroup->getType() != unit->getType()){ - //dont add another unit to a group of uniform selection units - return false; - } - } - } - - if(unit != NULL && !groupIsFull) { - groups[groupIndex].push_back(unit); - return true; - } - else{ - return false; - } -} - -void Selection::removeUnitFromGroup(int groupIndex,int unitId) { - if(groupIndex < 0 || groupIndex >= maxGroups) { - throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); - } - - for(unsigned int i = 0; i < groups[groupIndex].size(); ++i) { - Unit *unit = groups[groupIndex][i]; - if(unit != NULL && unit->getId() == unitId) { - groups[groupIndex].erase(groups[groupIndex].begin() + i); - break; - } - } -} - -//vector Selection::getUnitsForGroup(int groupIndex) { -// if(groupIndex < 0 || groupIndex >= maxGroups) { -// throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); -// } -// return groups[groupIndex]; -//} - -void Selection::recallGroup(int groupIndex,bool clearSelection){ - if(groupIndex < 0 || groupIndex >= maxGroups) { - throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); - } - - if(clearSelection==true){ - clear(); - } - for(int i = 0; i < (int)groups[groupIndex].size(); ++i) { - select(groups[groupIndex][i],!clearSelection); - } -} - -void Selection::unitEvent(UnitObserver::Event event, const Unit *unit) { - - if(event == UnitObserver::eKill) { - - //remove from selection - for(int index = 0; index < (int)selectedUnits.size(); ++index) { - if(selectedUnits[index] == unit){ - selectedUnits.erase(selectedUnits.begin() + index); - break; - } - } - - //remove from groups - for(int index = 0; index < maxGroups; ++index) { - for(int index2 = 0; index2 < (int)groups[index].size(); ++index2) { - if(groups[index][index2] == unit) { - groups[index].erase(groups[index].begin() + index2); + for (unsigned int i = 0; i < groups[groupIndex].size(); ++i) { + Unit *unit = groups[groupIndex][i]; + if (unit != NULL && unit->getId() == unitId) { + groups[groupIndex].erase(groups[groupIndex].begin() + i); break; } } } - //notify gui only if no more units to execute the command - //of course the selection changed, but this doesn't matter in this case. - if( selectedUnits.empty() == true) { - gui->onSelectionChanged(); + //vector Selection::getUnitsForGroup(int groupIndex) { + // if(groupIndex < 0 || groupIndex >= maxGroups) { + // throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + // } + // return groups[groupIndex]; + //} + + void Selection::recallGroup(int groupIndex, bool clearSelection) { + if (groupIndex < 0 || groupIndex >= maxGroups) { + throw megaglest_runtime_error("Invalid value for groupIndex = " + intToStr(groupIndex)); + } + + if (clearSelection == true) { + clear(); + } + for (int i = 0; i < (int) groups[groupIndex].size(); ++i) { + select(groups[groupIndex][i], !clearSelection); + } } - } -} -void Selection::saveGame(XmlNode *rootNode) const { + void Selection::unitEvent(UnitObserver::Event event, const Unit *unit) { - std::map mapTagReplacements; - XmlNode *selectionNode = rootNode->addChild("Selection"); + if (event == UnitObserver::eKill) { - selectionNode->addAttribute("factionIndex",intToStr(factionIndex), mapTagReplacements); - selectionNode->addAttribute("teamIndex",intToStr(teamIndex), mapTagReplacements); - selectionNode->addAttribute("allowSharedTeamUnits",intToStr(allowSharedTeamUnits), mapTagReplacements); + //remove from selection + for (int index = 0; index < (int) selectedUnits.size(); ++index) { + if (selectedUnits[index] == unit) { + selectedUnits.erase(selectedUnits.begin() + index); + break; + } + } - for(unsigned int i = 0; i < selectedUnits.size(); i++) { - Unit *unit = selectedUnits[i]; + //remove from groups + for (int index = 0; index < maxGroups; ++index) { + for (int index2 = 0; index2 < (int) groups[index].size(); ++index2) { + if (groups[index][index2] == unit) { + groups[index].erase(groups[index].begin() + index2); + break; + } + } + } - XmlNode *selectedUnitsNode = selectionNode->addChild("selectedUnits"); - selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements); - } - - for(unsigned int x = 0; x < (unsigned int)maxGroups; ++x) { - XmlNode *groupsNode = selectionNode->addChild("groups"); - for(unsigned int i = 0; i < (unsigned int)groups[x].size(); ++i) { - Unit *unit = groups[x][i]; - - XmlNode *selectedUnitsNode = groupsNode->addChild("selectedUnits"); - selectedUnitsNode->addAttribute("unitId",intToStr(unit->getId()), mapTagReplacements); + //notify gui only if no more units to execute the command + //of course the selection changed, but this doesn't matter in this case. + if (selectedUnits.empty() == true) { + gui->onSelectionChanged(); + } + } } - } -} -void Selection::loadGame(const XmlNode *rootNode, World *world) { + void Selection::saveGame(XmlNode *rootNode) const { - const XmlNode *selectionNode = rootNode->getChild("Selection"); + std::map mapTagReplacements; + XmlNode *selectionNode = rootNode->addChild("Selection"); - factionIndex = selectionNode->getAttribute("factionIndex")->getIntValue(); - teamIndex = selectionNode->getAttribute("teamIndex")->getIntValue(); - if(selectionNode->hasAttribute("allowSharedTeamUnits") == true) { - allowSharedTeamUnits = (selectionNode->getAttribute("allowSharedTeamUnits")->getIntValue() != 0); - } + selectionNode->addAttribute("factionIndex", intToStr(factionIndex), mapTagReplacements); + selectionNode->addAttribute("teamIndex", intToStr(teamIndex), mapTagReplacements); + selectionNode->addAttribute("allowSharedTeamUnits", intToStr(allowSharedTeamUnits), mapTagReplacements); - vector selectedUnitsNodeList = selectionNode->getChildList("selectedUnits"); - for(unsigned int i = 0; i < selectedUnitsNodeList.size(); ++i) { - XmlNode *selectedUnitsNode = selectedUnitsNodeList[i]; + for (unsigned int i = 0; i < selectedUnits.size(); i++) { + Unit *unit = selectedUnits[i]; - int unitId = selectedUnitsNode->getAttribute("unitId")->getIntValue(); - Unit *unit = world->findUnitById(unitId); - //assert(unit != NULL); - //printf("#1 Unit [%s], group: %d\n",unit->getType()->getName().c_str(),i); - selectedUnits.push_back(unit); - } + XmlNode *selectedUnitsNode = selectionNode->addChild("selectedUnits"); + selectedUnitsNode->addAttribute("unitId", intToStr(unit->getId()), mapTagReplacements); + } - vector groupsNodeList = selectionNode->getChildList("groups"); - for(unsigned int i = 0; i < groupsNodeList.size(); ++i) { - XmlNode *groupsNode = groupsNodeList[i]; + for (unsigned int x = 0; x < (unsigned int) maxGroups; ++x) { + XmlNode *groupsNode = selectionNode->addChild("groups"); + for (unsigned int i = 0; i < (unsigned int) groups[x].size(); ++i) { + Unit *unit = groups[x][i]; - vector selectedGroupsUnitsNodeList = groupsNode->getChildList("selectedUnits"); - for(unsigned int j = 0; j < selectedGroupsUnitsNodeList.size(); ++j) { - XmlNode *selectedGroupsUnitsNode = selectedGroupsUnitsNodeList[j]; - - int unitId = selectedGroupsUnitsNode->getAttribute("unitId")->getIntValue(); - Unit *unit = world->findUnitById(unitId); - //assert(unit != NULL); - //printf("Unit #2 [%s], group: %d\n",unit->getType()->getName().c_str(),i); - groups[i].push_back(unit); + XmlNode *selectedUnitsNode = groupsNode->addChild("selectedUnits"); + selectedUnitsNode->addAttribute("unitId", intToStr(unit->getId()), mapTagReplacements); + } + } } + + void Selection::loadGame(const XmlNode *rootNode, World *world) { + + const XmlNode *selectionNode = rootNode->getChild("Selection"); + + factionIndex = selectionNode->getAttribute("factionIndex")->getIntValue(); + teamIndex = selectionNode->getAttribute("teamIndex")->getIntValue(); + if (selectionNode->hasAttribute("allowSharedTeamUnits") == true) { + allowSharedTeamUnits = (selectionNode->getAttribute("allowSharedTeamUnits")->getIntValue() != 0); + } + + vector selectedUnitsNodeList = selectionNode->getChildList("selectedUnits"); + for (unsigned int i = 0; i < selectedUnitsNodeList.size(); ++i) { + XmlNode *selectedUnitsNode = selectedUnitsNodeList[i]; + + int unitId = selectedUnitsNode->getAttribute("unitId")->getIntValue(); + Unit *unit = world->findUnitById(unitId); + //assert(unit != NULL); + //printf("#1 Unit [%s], group: %d\n",unit->getType()->getName().c_str(),i); + selectedUnits.push_back(unit); + } + + vector groupsNodeList = selectionNode->getChildList("groups"); + for (unsigned int i = 0; i < groupsNodeList.size(); ++i) { + XmlNode *groupsNode = groupsNodeList[i]; + + vector selectedGroupsUnitsNodeList = groupsNode->getChildList("selectedUnits"); + for (unsigned int j = 0; j < selectedGroupsUnitsNodeList.size(); ++j) { + XmlNode *selectedGroupsUnitsNode = selectedGroupsUnitsNodeList[j]; + + int unitId = selectedGroupsUnitsNode->getAttribute("unitId")->getIntValue(); + Unit *unit = world->findUnitById(unitId); + //assert(unit != NULL); + //printf("Unit #2 [%s], group: %d\n",unit->getType()->getName().c_str(),i); + groups[i].push_back(unit); + } + } + } + + } -} - - -}}//end namespace +}//end namespace diff --git a/source/glest_game/gui/selection.h b/source/glest_game/gui/selection.h index b305df8f9..9e83b40fd 100644 --- a/source/glest_game/gui/selection.h +++ b/source/glest_game/gui/selection.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_SELECTION_ #ifdef WIN32 - #include - #include +#include +#include #endif #include "unit.h" @@ -23,87 +23,99 @@ using std::vector; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class Gui; -class World; + class Gui; + class World; -class HighlightSpecialUnitInfo { -public: - float radius; - float thickness; - Vec4f color; -}; + class HighlightSpecialUnitInfo { + public: + float radius; + float thickness; + Vec4f color; + }; -// ===================================================== -// class Selection -// -/// List of selected units and groups -// ===================================================== + // ===================================================== + // class Selection + // + /// List of selected units and groups + // ===================================================== -class Selection: public UnitObserver { -public: - typedef vector UnitContainer; - typedef UnitContainer::const_iterator UnitIterator; + class Selection : public UnitObserver { + public: + typedef vector UnitContainer; + typedef UnitContainer::const_iterator UnitIterator; -public: - static const int maxGroups = 10; - static const int maxUnits = 36; + public: + static const int maxGroups = 10; + static const int maxUnits = 36; -private: - int factionIndex; - int teamIndex; - bool allowSharedTeamUnits; - UnitContainer selectedUnits; - UnitContainer groups[maxGroups]; - Gui *gui; + private: + int factionIndex; + int teamIndex; + bool allowSharedTeamUnits; + UnitContainer selectedUnits; + UnitContainer groups[maxGroups]; + Gui *gui; + + public: + Selection() : UnitObserver() { + factionIndex = 0; + teamIndex = 0; + allowSharedTeamUnits = false; + gui = NULL; + } + void init(Gui *gui, int factionIndex, int teamIndex, bool allowSharedTeamUnits); + virtual ~Selection(); + + bool select(Unit *unit, bool addToSelection); + void select(const UnitContainer &units, bool addToSelection); + void unSelect(const UnitContainer &units); + void unSelect(int unitIndex); + void clear(); + + bool isEmpty() const { + return selectedUnits.empty(); + } + bool isUniform() const; + bool isEnemy() const; + bool isObserver() const; + + bool isCommandable() const; + bool isCancelable() const; + bool isMeetable() const; + int getCount() const { + return (int) selectedUnits.size(); + } + const Unit *getUnit(int i) const { + return selectedUnits[i]; + } + Unit *getUnitPtr(int i) { + return selectedUnits[i]; + } + const Unit *getFrontUnit() const { + return selectedUnits.front(); + } + Vec3f getRefPos() const; + bool hasUnit(const Unit* unit) const; + + bool assignGroup(int groupIndex, bool clearGroup = true, const UnitContainer *pUnits = NULL); + bool addUnitToGroup(int groupIndex, Unit *unit); + void removeUnitFromGroup(int groupIndex, int UnitId); + void recallGroup(int groupIndex, bool clearSelection = true); + + //vector getUnitsForGroup(int groupIndex); + + virtual void unitEvent(UnitObserver::Event event, const Unit *unit); + bool canSelectUnitFactionCheck(const Unit *unit) const; + + virtual void saveGame(XmlNode *rootNode) const; + void loadGame(const XmlNode *rootNode, World *world); + + }; -public: - Selection() : UnitObserver() { - factionIndex = 0; - teamIndex = 0; - allowSharedTeamUnits = false; - gui = NULL; } - void init(Gui *gui, int factionIndex, int teamIndex, bool allowSharedTeamUnits); - virtual ~Selection(); - - bool select(Unit *unit, bool addToSelection); - void select(const UnitContainer &units, bool addToSelection); - void unSelect(const UnitContainer &units); - void unSelect(int unitIndex); - void clear(); - - bool isEmpty() const {return selectedUnits.empty();} - bool isUniform() const; - bool isEnemy() const; - bool isObserver() const; - - bool isCommandable() const; - bool isCancelable() const; - bool isMeetable() const; - int getCount() const {return (int)selectedUnits.size();} - const Unit *getUnit(int i) const {return selectedUnits[i];} - Unit *getUnitPtr(int i) {return selectedUnits[i];} - const Unit *getFrontUnit() const {return selectedUnits.front();} - Vec3f getRefPos() const; - bool hasUnit(const Unit* unit) const; - - bool assignGroup(int groupIndex, bool clearGroup=true, const UnitContainer *pUnits=NULL); - bool addUnitToGroup(int groupIndex,Unit *unit); - void removeUnitFromGroup(int groupIndex,int UnitId); - void recallGroup(int groupIndex, bool clearSelection=true); - - //vector getUnitsForGroup(int groupIndex); - - virtual void unitEvent(UnitObserver::Event event, const Unit *unit); - bool canSelectUnitFactionCheck(const Unit *unit) const; - - virtual void saveGame(XmlNode *rootNode) const; - void loadGame(const XmlNode *rootNode, World *world); - -}; - -}}//end namespace +}//end namespace #endif diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 94c8355cc..315edaf1d 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -29,1125 +29,959 @@ #include "leak_dumper.h" using namespace - Shared::Util; - -namespace Glest -{ - namespace Game - { - -// ===================================================== -// class BattleEnd -// ===================================================== - - BattleEnd::BattleEnd (Program * program, const Stats * stats, - ProgramState * originState): - ProgramState (program), - menuBackgroundVideo (NULL), - gameSettings (NULL) - { - - containerName = "BattleEnd"; - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d] stats = %p\n", __FILE__, - __FUNCTION__, __LINE__, stats); - - this->originState = originState; - if (stats != NULL) - { - this->stats = *stats; - } - if (originState != NULL) - { - Game * - game = dynamic_cast < Game * >(originState); - if (game != NULL) - { - gameSettings = new GameSettings (); - *gameSettings = *(game->getGameSettings ()); - } - } - mouseX = 0; - mouseY = 0; - mouse2d = 0; - renderToTexture = NULL; - renderToTextureCount = 0; - - const - Metrics & - metrics = Metrics::getInstance (); - Lang & lang = Lang::getInstance (); - int - buttonWidth = 125; - int - xLocation = (metrics.getVirtualW () / 2) - (buttonWidth / 2); - buttonExit.init (xLocation, 80, buttonWidth); - buttonExit.setText (lang.getString ("Exit")); - -//mesage box - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - mainMessageBox.setEnabled (false); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); - - initBackgroundVideo (); - initBackgroundMusic (); - - GraphicComponent::applyAllCustomProperties (containerName); - } - - void - BattleEnd::reloadUI () - { - Lang & lang = Lang::getInstance (); - - buttonExit.setText (lang.getString ("Exit")); - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - - GraphicComponent::reloadFontsForRegisterGraphicComponents - (containerName); - } - - BattleEnd::~BattleEnd () - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); - - if (menuBackgroundVideo != NULL) - { - menuBackgroundVideo->closePlayer (); - delete menuBackgroundVideo; - menuBackgroundVideo = NULL; - } - - delete gameSettings; - gameSettings = NULL; - - delete originState; - originState = NULL; - - if (CoreData::getInstance ().hasMainMenuVideoFilename () == false) - { - SoundRenderer::getInstance (). - playMusic (CoreData::getInstance ().getMenuMusic ()); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); - - delete renderToTexture; - renderToTexture = NULL; - - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); - } - - std::pair < string, string > BattleEnd::getBattleEndVideo (bool won) - { - std::pair < string, string > result; - - string factionVideoUrl = ""; - string factionVideoUrlFallback = ""; - - if (gameSettings != NULL) - { - string currentTechName_factionPreview = gameSettings->getTech (); - string - currentFactionName_factionPreview = - gameSettings->getFactionTypeName (stats.getThisFactionIndex ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 tech [%s] faction [%s] won = %d\n", - currentTechName_factionPreview.c_str (), - currentFactionName_factionPreview.c_str (), won); - - string - factionDefinitionXML = - Game::findFactionLogoFile (gameSettings, NULL, - currentFactionName_factionPreview + - ".xml"); - if (factionDefinitionXML != "" - && currentFactionName_factionPreview != - GameConstants::RANDOMFACTION_SLOTNAME - && currentFactionName_factionPreview != - GameConstants::OBSERVER_SLOTNAME - && fileExists (factionDefinitionXML) == true) - { - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 tech [%s] faction [%s]\n", - currentTechName_factionPreview.c_str (), - currentFactionName_factionPreview.c_str ()); - - XmlTree xmlTree; - std::map < string, string > mapExtraTagReplacementValues; - xmlTree.load (factionDefinitionXML, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues)); - const XmlNode * - factionNode = xmlTree.getRootNode (); - if (won == true) - { - if (factionNode->hasAttribute ("battle-end-win-video") == true) - { - factionVideoUrl = - factionNode-> - getAttribute ("battle-end-win-video")->getValue (); - } - } - else - { - if (factionNode->hasAttribute ("battle-end-lose-video") == true) - { - factionVideoUrl = - factionNode-> - getAttribute ("battle-end-lose-video")->getValue (); - } - } - - if (factionVideoUrl != "" && fileExists (factionVideoUrl) == false) - { - string techTreePath = ""; - string factionPath = ""; - std::vector < std::string > factionPartsList; - Tokenize (factionDefinitionXML, factionPartsList, "factions/"); - if (factionPartsList.size () > 1) - { - techTreePath = factionPartsList[0]; - - string - factionPath = - techTreePath + "factions/" + - currentFactionName_factionPreview; - endPathWithSlash (factionPath); - factionVideoUrl = factionPath + factionVideoUrl; - } - } - - if (won == true) - { - factionVideoUrlFallback = - Game::findFactionLogoFile (gameSettings, NULL, - "battle_end_win_video.*"); - } - else - { - factionVideoUrlFallback = - Game::findFactionLogoFile (gameSettings, NULL, - "battle_end_lose_video.*"); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n", - factionVideoUrl.c_str (), - factionVideoUrlFallback.c_str ()); -//printf("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n",factionVideoUrl.c_str(),factionVideoUrlFallback.c_str()); - - if (factionVideoUrl == "") - { - factionVideoUrl = factionVideoUrlFallback; - factionVideoUrlFallback = ""; - } - } -//printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); - } - - if (factionVideoUrl != "") - { - result.first = factionVideoUrl; - result.second = factionVideoUrlFallback; - } - else - { - result.first = - CoreData::getInstance ().getBattleEndVideoFilename (won); - result.second = - CoreData::getInstance ().getBattleEndVideoFilenameFallback (won); - } - - return result; - } - - string BattleEnd::getBattleEndMusic (bool won) - { - string result = ""; - string resultFallback = ""; - - if (gameSettings != NULL) - { - string currentTechName_factionPreview = gameSettings->getTech (); - string - currentFactionName_factionPreview = - gameSettings->getFactionTypeName (stats.getThisFactionIndex ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 tech [%s] faction [%s] won = %d\n", - currentTechName_factionPreview.c_str (), - currentFactionName_factionPreview.c_str (), won); - - string - factionDefinitionXML = - Game::findFactionLogoFile (gameSettings, NULL, - currentFactionName_factionPreview + - ".xml"); - if (factionDefinitionXML != "" - && currentFactionName_factionPreview != - GameConstants::RANDOMFACTION_SLOTNAME - && currentFactionName_factionPreview != - GameConstants::OBSERVER_SLOTNAME - && fileExists (factionDefinitionXML) == true) - { - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 tech [%s] faction [%s]\n", - currentTechName_factionPreview.c_str (), - currentFactionName_factionPreview.c_str ()); - - XmlTree xmlTree; - std::map < string, string > mapExtraTagReplacementValues; - xmlTree.load (factionDefinitionXML, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues)); - const XmlNode * - factionNode = xmlTree.getRootNode (); - if (won == true) - { - if (factionNode->hasAttribute ("battle-end-win-music") == true) - { - result = - factionNode-> - getAttribute ("battle-end-win-music")->getValue (); - } - } - else - { - if (factionNode->hasAttribute ("battle-end-lose-music") == true) - { - result = - factionNode-> - getAttribute ("battle-end-lose-music")->getValue (); - } - } - - if (result != "" && fileExists (result) == false) - { - string techTreePath = ""; - string factionPath = ""; - std::vector < std::string > factionPartsList; - Tokenize (factionDefinitionXML, factionPartsList, "factions/"); - if (factionPartsList.size () > 1) - { - techTreePath = factionPartsList[0]; - - string - factionPath = - techTreePath + "factions/" + - currentFactionName_factionPreview; - endPathWithSlash (factionPath); - result = factionPath + result; - } - } - - if (won == true) - { - resultFallback = - Game::findFactionLogoFile (gameSettings, NULL, - "battle_end_win_music.*"); - } - else - { - resultFallback = - Game::findFactionLogoFile (gameSettings, NULL, - "battle_end_lose_music.*"); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#3 result [%s] resultFallback [%s]\n", result.c_str (), - resultFallback.c_str ()); -//printf("#3 result [%s] resultFallback [%s]\n",result.c_str(),resultFallback.c_str()); - - if (result == "") - { - result = resultFallback; - } - } -//printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); - } - - if (result == "") - { - result = CoreData::getInstance ().getBattleEndMusicFilename (won); - } - - return result; - } - - void - BattleEnd::initBackgroundMusic () - { - string music = ""; - - if (stats.getThisFactionIndex () >= 0 - && stats.getTeam (stats.getThisFactionIndex ()) != - GameConstants::maxPlayers - 1 + fpt_Observer) - { - if (stats.getVictory (stats.getThisFactionIndex ())) - { -//header += lang.getString("Victory"); - music = getBattleEndMusic (true); - } - else - { -//header += lang.getString("Defeat"); - music = getBattleEndMusic (false); - } - - if (music != "" && fileExists (music) == true) - { - printf ("music [%s] \n", music.c_str ()); - - battleEndMusic.open (music); - battleEndMusic.setNext (&battleEndMusic); - - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.playMusic (&battleEndMusic); - } - } - } - - void - BattleEnd::initBackgroundVideo () - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == true) - { - - if (menuBackgroundVideo != NULL) - { - menuBackgroundVideo->closePlayer (); - delete menuBackgroundVideo; - menuBackgroundVideo = NULL; - } - - string videoFile = ""; - string videoFileFallback = ""; - - if (stats.getThisFactionIndex () >= 0 - && stats.getTeam (stats.getThisFactionIndex ()) != - GameConstants::maxPlayers - 1 + fpt_Observer) - { - if (stats.getVictory (stats.getThisFactionIndex ())) - { -//header += lang.getString("Victory"); - -//videoFile = CoreData::getInstance().getBattleEndVideoFilename(true); -//videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(true); - std::pair < string, string > wonVideos = getBattleEndVideo (true); - videoFile = wonVideos.first; - videoFileFallback = wonVideos.second; - } - else - { -//header += lang.getString("Defeat"); -//videoFile = CoreData::getInstance().getBattleEndVideoFilename(false); -//videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(false); - std::pair < string, string > lostVideos = - getBattleEndVideo (false); - videoFile = lostVideos.first; - videoFileFallback = lostVideos.second; - } - } - else - { -//header += "Observer"; - } - - if (fileExists (videoFile) || fileExists (videoFileFallback)) - { - printf ("videoFile [%s] videoFileFallback [%s]\n", - videoFile.c_str (), videoFileFallback.c_str ()); - - Context * - c = GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl * - glCtx = - static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window * - window = glCtx->getScreenWindow (); - SDL_Surface * - screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); -//printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); - menuBackgroundVideo = new VideoPlayer (&Renderer::getInstance (), - videoFile, - videoFileFallback, - window, - 0, 0, - screen->w, - screen->h, - screen->format->BitsPerPixel, - true, - vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - menuBackgroundVideo->initPlayer (); - } - } - } - - void - BattleEnd::update () - { - if (Config::getInstance ().getBool ("AutoTest")) - { - AutoTest::getInstance ().updateBattleEnd (program); - return; - } - mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; - - if (this->stats.getIsMasterserverMode () == true) - { - if (program->getWantShutdownApplicationAfterGame () == true) - { - program->setShutdownApplicationEnabled (true); - return; - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); -//program->setState(new MainMenu(program)); - program->initServer (program->getWindow (), false, true, true); - return; - } - } - - void - BattleEnd::render () - { - if (this->stats.getIsMasterserverMode () == true) - { - return; - } - Renderer & renderer = Renderer::getInstance (); -//CoreData &coreData= CoreData::getInstance(); - - canRender (); - incrementFps (); - -//printf("In [%s::%s Line: %d] renderToTexture [%p]\n",__FILE__,__FUNCTION__,__LINE__,renderToTexture); - if (menuBackgroundVideo == NULL && renderToTexture != NULL) - { -//printf("Rendering from texture!\n"); - - renderer.clearBuffers (); - renderer.reset3dMenu (); - renderer.clearZBuffer (); - - renderer.reset2d (); - - renderer.renderBackground (renderToTexture); - - renderer.renderButton (&buttonExit); - -//exit message box - if (mainMessageBox.getEnabled () && mainMessageBox.getVisible ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - } - else - { -//printf("Rendering to texture!\n"); - - if (menuBackgroundVideo == NULL && renderToTextureCount >= 300) - { - renderer.beginRenderToTexture (&renderToTexture); - } - - TextRenderer2D * - textRenderer2D = renderer.getTextRenderer (); - TextRenderer3D * - textRenderer3D = renderer.getTextRenderer3D (); - TextRenderer * - textRenderer = NULL; - - if (Renderer::renderText3DEnabled == true) - { - textRenderer = textRenderer3D; - } - else - { - textRenderer = textRenderer2D; - } - - Lang & lang = Lang::getInstance (); - - renderer.clearBuffers (); - renderer.reset3dMenu (); - renderer.clearZBuffer (); - renderer.reset2d (); - - if (menuBackgroundVideo != NULL) - { -//printf("Rendering video not null!\n"); - - if (menuBackgroundVideo->isPlaying () == true) - { - menuBackgroundVideo->playFrame (false); - -//printf("Rendering video playing!\n"); - } - else - { - if (menuBackgroundVideo != NULL) - { -//initBackgroundVideo(); - menuBackgroundVideo->RestartVideo (); - } - } - } - else - { - renderer. - renderBackground (CoreData:: - getInstance ().getBackgroundTexture ()); - } - -//int winnerIndex = -1; - int - bestScore = -1; -//int mostKillsIndex = -1; - int - bestKills = -1; -//int mostEnemyKillsIndex = -1; - int - bestEnemyKills = -1; -//int leastDeathsIndex = -1; - int - leastDeaths = -1; -//int mostUnitsProducedIndex = -1; - int - bestUnitsProduced = -1; -//int mostResourcesHarvestedIndex = -1; - int - bestResourcesHarvested = -1; - - for (int i = 0; i < stats.getFactionCount (); ++i) - { - if (stats.getTeam (i) == - GameConstants::maxPlayers - 1 + fpt_Observer) - { - continue; - } - -//int team= stats.getTeam(i) + 1; - int - kills = stats.getKills (i); - if (kills > bestKills) - { - bestKills = kills; -//mostKillsIndex = i; - } - - int - enemykills = stats.getEnemyKills (i); - if (enemykills > bestEnemyKills) - { - bestEnemyKills = enemykills; -//mostEnemyKillsIndex = i; - } - - int - deaths = stats.getDeaths (i); - if (deaths < leastDeaths || leastDeaths < 0) - { - leastDeaths = deaths; -//leastDeathsIndex = i; - } - - int - unitsProduced = stats.getUnitsProduced (i); - if (unitsProduced > bestUnitsProduced) - { - bestUnitsProduced = unitsProduced; -//mostUnitsProducedIndex = i; - } - - int - resourcesHarvested = stats.getResourcesHarvested (i); - if (resourcesHarvested > bestResourcesHarvested) - { - bestResourcesHarvested = resourcesHarvested; -//mostResourcesHarvestedIndex = i; - } - - int - score = - enemykills * 100 + unitsProduced * 50 + resourcesHarvested / 10; - - if (score > bestScore) - { - bestScore = score; -//winnerIndex = i; - } - } - - bool - disableStatsColorCoding = - Config::getInstance ().getBool ("DisableBattleEndColorCoding", - "false"); - - if (Renderer::renderText3DEnabled == true) - { - textRenderer3D-> - begin (CoreData::getInstance ().getMenuFontNormal3D ()); - } - else - { - textRenderer2D-> - begin (CoreData::getInstance ().getMenuFontNormal ()); - } - - int - lm = 20; - int - bm = 100; - - int - realPlayerCount = 0; - for (int i = 0; i < stats.getFactionCount (); ++i) - { - if (stats.getTeam (i) == - GameConstants::maxPlayers - 1 + fpt_Observer) - { - continue; - } - - realPlayerCount++; - int - textX = lm + 60 + (realPlayerCount * 100); - int - team = stats.getTeam (i) + 1; - int - kills = stats.getKills (i); - int - enemykills = stats.getEnemyKills (i); - int - deaths = stats.getDeaths (i); - int - unitsProduced = stats.getUnitsProduced (i); - int - resourcesHarvested = stats.getResourcesHarvested (i); - - int - score = - enemykills * 100 + unitsProduced * 50 + resourcesHarvested / 10; - string controlString; - - if (stats.getPersonalityType (i) == fpt_Observer) - { - controlString = GameConstants::OBSERVER_SLOTNAME; - } - else - { - switch (stats.getControl (i)) - { - case ctCpuEasy: - controlString = lang.getString ("CpuEasy"); - break; - case ctCpu: - controlString = lang.getString ("Cpu"); - break; - case ctCpuUltra: - controlString = lang.getString ("CpuUltra"); - break; - case ctCpuZeta: - controlString = lang.getString ("CpuZeta"); - break; - case ctNetwork: - controlString = lang.getString ("Network"); - break; - case ctHuman: - controlString = lang.getString ("Human"); - break; - - case ctNetworkCpuEasy: - controlString = lang.getString ("NetworkCpuEasy"); - break; - case ctNetworkCpu: - controlString = lang.getString ("NetworkCpu"); - break; - case ctNetworkCpuUltra: - controlString = lang.getString ("NetworkCpuUltra"); - break; - case ctNetworkCpuZeta: - controlString = lang.getString ("NetworkCpuZeta"); - break; - - default: - printf ("Error control = %d for i = %d\n", stats.getControl (i), - i); - assert (false); - break; - }; - } - - if (stats.getControl (i) != ctHuman - && stats.getControl (i) != ctNetwork) - { - controlString += - "\nx " + floatToStr (stats.getResourceMultiplier (i), 1); - } - else if (stats.getPlayerLeftBeforeEnd (i) == true) - { - controlString += - "\n" + lang.getString ("CpuUltra") + "\nx " + - floatToStr (stats.getResourceMultiplier (i), 1); - } - - if (score == bestScore && stats.getVictory (i)) - { - if (CoreData::getInstance ().getGameWinnerTexture () != NULL) - { - renderer.renderTextureQuad (textX, bm + 420, -1, -1, - CoreData:: - getInstance ().getGameWinnerTexture - (), 0.7f); - } - } - - Vec3f color = stats.getPlayerColor (i); - if (stats.getPlayerName (i) != "") - { - string textToRender = stats.getPlayerName (i).substr (0, 11); - if (stats.getPlayerLeftBeforeEnd (i) == true) - { - textToRender += - "\n(" + getTimeDuationString (stats.getTimePlayerLeft (i), - GameConstants::updateFps) + ")"; - } - - textRenderer->render (textToRender.c_str (), textX, bm + 400, - false, &color); - } - else - { - textRenderer->render ((lang.getString ("Player") + " " + - intToStr (i + 1)).c_str (), textX, - bm + 400, false, &color); - } - - Vec3f highliteColor = Vec3f (WHITE.x, WHITE.y, WHITE.z); - if (disableStatsColorCoding == false) - { - highliteColor.x = 0.85f; - highliteColor.y = 0.8f; - highliteColor.z = 0.07f; - } - - if (stats.getPersonalityType (i) == fpt_Observer) - { - textRenderer->render (lang.getString ("GameOver").c_str (), textX, - bm + 360); - } - else - { - if (stats.getVictory (i)) - { - textRenderer->render (stats. - getVictory (i) ? - lang.getString ("Victory"). - c_str () : lang.getString ("Defeat"). - c_str (), textX, bm + 360, false, - &highliteColor); - } - else - { - textRenderer->render (stats. - getVictory (i) ? - lang.getString ("Victory"). - c_str () : lang.getString ("Defeat"). - c_str (), textX, bm + 360); - } - } - - textRenderer->render (controlString, textX, bm + 320); - textRenderer->render (stats.getFactionTypeName (i), textX, - bm + 280); - textRenderer->render (intToStr (team).c_str (), textX, bm + 240); - - if (kills == bestKills) - { - textRenderer->render (intToStr (kills).c_str (), textX, bm + 200, - false, &highliteColor); - } - else - { - textRenderer->render (intToStr (kills).c_str (), textX, bm + 200); - } - if (enemykills == bestEnemyKills) - { - textRenderer->render (intToStr (enemykills).c_str (), textX, - bm + 180, false, &highliteColor); - } - else - { - textRenderer->render (intToStr (enemykills).c_str (), textX, - bm + 180); - } - if (deaths == leastDeaths) - { - textRenderer->render (intToStr (deaths).c_str (), textX, bm + 160, - false, &highliteColor); - } - else - { - textRenderer->render (intToStr (deaths).c_str (), textX, - bm + 160); - } - if (unitsProduced == bestUnitsProduced) - { - textRenderer->render (intToStr (unitsProduced).c_str (), textX, - bm + 120, false, &highliteColor); - } - else - { - textRenderer->render (intToStr (unitsProduced).c_str (), textX, - bm + 120); - } - if (resourcesHarvested == bestResourcesHarvested) - { - textRenderer->render (intToStr (resourcesHarvested).c_str (), - textX, bm + 80, false, &highliteColor); - } - else - { - textRenderer->render (intToStr (resourcesHarvested).c_str (), - textX, bm + 80); - } - if (score == bestScore) - { - textRenderer->render (intToStr (score).c_str (), textX, bm + 20, - false, &highliteColor); - } - else - { - textRenderer->render (intToStr (score).c_str (), textX, bm + 20); - } - } - - textRenderer->render ("\n" + (lang.getString ("LeftAt")), lm, - bm + 400); - textRenderer->render (lang.getString ("Result"), lm, bm + 360); - textRenderer->render (lang.getString ("Control"), lm, bm + 320); - textRenderer->render (lang.getString ("Faction"), lm, bm + 280); - textRenderer->render (lang.getString ("Team"), lm, bm + 240); - textRenderer->render (lang.getString ("Kills"), lm, bm + 200); - textRenderer->render (lang.getString ("EnemyKills"), lm, bm + 180); - textRenderer->render (lang.getString ("Deaths"), lm, bm + 160); - textRenderer->render (lang.getString ("UnitsProduced"), lm, bm + 120); - textRenderer->render (lang.getString ("ResourcesHarvested"), lm, - bm + 80); - textRenderer->render (lang.getString ("Score"), lm, bm + 20); - - textRenderer->end (); - - if (Renderer::renderText3DEnabled == true) - { - textRenderer3D-> - begin (CoreData::getInstance ().getMenuFontVeryBig3D ()); - } - else - { - textRenderer2D-> - begin (CoreData::getInstance ().getMenuFontVeryBig ()); - } - - string header = stats.getDescription () + " - "; - - if (stats.getThisFactionIndex () >= 0 - && stats.getTeam (stats.getThisFactionIndex ()) != - GameConstants::maxPlayers - 1 + fpt_Observer) - { - if (stats.getVictory (stats.getThisFactionIndex ())) - { - header += lang.getString ("Victory"); - } - else - { - header += lang.getString ("Defeat"); - } - } - else - { - header += "Observer"; - } - textRenderer->render (header, lm + 250, bm + 550); - -//GameConstants::updateFps -//string header2 = lang.getString("GameDurationTime","",true) + " " + floatToStr(stats.getWorldTimeElapsed() / 24.0,2); - - string - header2 = - lang.getString ("GameDurationTime", "", - true) + ": " + - getTimeDuationString (stats.getFramesToCalculatePlaytime (), - GameConstants::updateFps); - textRenderer->render (header2, lm + 250, bm + 530); - - header2 = - lang.getString ("GameMaxConcurrentUnitCount") + ": " + - intToStr (stats.getMaxConcurrentUnitCount ()); - textRenderer->render (header2, lm + 250, bm + 510); - - header2 = - lang.getString ("GameTotalEndGameConcurrentUnitCount") + ": " + - intToStr (stats.getTotalEndGameConcurrentUnitCount ()); - textRenderer->render (header2, lm + 250, bm + 490); - - textRenderer->end (); - - renderer.renderButton (&buttonExit); - -//exit message box - if (mainMessageBox.getEnabled () && renderToTextureCount < 300) - { - renderer.renderMessageBox (&mainMessageBox); - } - - if (menuBackgroundVideo == NULL && renderToTexture == NULL) - { - renderer.renderMouse2d (mouseX, mouseY, mouse2d, 0.f); - } - - if (menuBackgroundVideo == NULL && renderToTextureCount >= 300) - { - renderer.endRenderToTexture (&renderToTexture); - } - - if (menuBackgroundVideo == NULL) - { - if (renderToTexture == NULL && renderToTextureCount < 300) - { - renderToTextureCount++; - } - } - } - - renderer.renderFPSWhenEnabled (lastFps); - - renderer.swapBuffers (); - } - - void - BattleEnd::keyDown (SDL_KeyboardEvent key) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); - if (isKeyPressed (SDLK_ESCAPE, key) == true) - { -//program->setState(new MainMenu(program)); - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.setEnabled (false); - } - else - { - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("ExitToRootMenu"), "", true); - } - } - else if (isKeyPressed (SDLK_RETURN, key) - && mainMessageBox.getEnabled ()) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - program->setState (new MainMenu (program)); - } - } - - void - BattleEnd::mouseDownLeft (int x, int y) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, "In [%s::%s %d]\n", - __FILE__, __FUNCTION__, __LINE__); -//program->setState(new MainMenu(program)); - - if (buttonExit.mouseClick (x, y)) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - program->setState (new MainMenu (program)); - } - else if (mainMessageBox.getEnabled ()) - { - int - button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - if (button == 0) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - program->setState (new MainMenu (program)); - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); -//close message box - mainMessageBox.setEnabled (false); - } - } - } - - } - - void - BattleEnd::mouseMove (int x, int y, const MouseState * ms) - { - mouseX = x; - mouseY = y; - - buttonExit.mouseMove (x, y); - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - - } - - void - BattleEnd::showMessageBox (const string & text, const string & header, - bool toggle) - { - if (toggle == false) - { - mainMessageBox.setEnabled (false); - } - - if (mainMessageBox.getEnabled () == false) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - } +Shared::Util; + +namespace Glest { + namespace Game { + + // ===================================================== + // class BattleEnd + // ===================================================== + + BattleEnd::BattleEnd(Program * program, const Stats * stats, + ProgramState * originState) : + ProgramState(program), + menuBackgroundVideo(NULL), + gameSettings(NULL) { + + containerName = "BattleEnd"; + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d] stats = %p\n", __FILE__, + __FUNCTION__, __LINE__, stats); + + this->originState = originState; + if (stats != NULL) { + this->stats = *stats; + } + if (originState != NULL) { + Game * + game = dynamic_cast (originState); + if (game != NULL) { + gameSettings = new GameSettings(); + *gameSettings = *(game->getGameSettings()); + } + } + mouseX = 0; + mouseY = 0; + mouse2d = 0; + renderToTexture = NULL; + renderToTextureCount = 0; + + const + Metrics & + metrics = Metrics::getInstance(); + Lang & lang = Lang::getInstance(); + int + buttonWidth = 125; + int + xLocation = (metrics.getVirtualW() / 2) - (buttonWidth / 2); + buttonExit.init(xLocation, 80, buttonWidth); + buttonExit.setText(lang.getString("Exit")); + + //mesage box + mainMessageBox.init(lang.getString("Yes"), lang.getString("No")); + mainMessageBox.setEnabled(false); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + + initBackgroundVideo(); + initBackgroundMusic(); + + GraphicComponent::applyAllCustomProperties(containerName); + } + + void + BattleEnd::reloadUI() { + Lang & lang = Lang::getInstance(); + + buttonExit.setText(lang.getString("Exit")); + mainMessageBox.init(lang.getString("Yes"), lang.getString("No")); + + GraphicComponent::reloadFontsForRegisterGraphicComponents + (containerName); + } + + BattleEnd::~BattleEnd() { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + + if (menuBackgroundVideo != NULL) { + menuBackgroundVideo->closePlayer(); + delete menuBackgroundVideo; + menuBackgroundVideo = NULL; + } + + delete gameSettings; + gameSettings = NULL; + + delete originState; + originState = NULL; + + if (CoreData::getInstance().hasMainMenuVideoFilename() == false) { + SoundRenderer::getInstance(). + playMusic(CoreData::getInstance().getMenuMusic()); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + + delete renderToTexture; + renderToTexture = NULL; + + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + } + + std::pair < string, string > BattleEnd::getBattleEndVideo(bool won) { + std::pair < string, string > result; + + string factionVideoUrl = ""; + string factionVideoUrlFallback = ""; + + if (gameSettings != NULL) { + string currentTechName_factionPreview = gameSettings->getTech(); + string + currentFactionName_factionPreview = + gameSettings->getFactionTypeName(stats.getThisFactionIndex()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 tech [%s] faction [%s] won = %d\n", + currentTechName_factionPreview.c_str(), + currentFactionName_factionPreview.c_str(), won); + + string + factionDefinitionXML = + Game::findFactionLogoFile(gameSettings, NULL, + currentFactionName_factionPreview + + ".xml"); + if (factionDefinitionXML != "" + && currentFactionName_factionPreview != + GameConstants::RANDOMFACTION_SLOTNAME + && currentFactionName_factionPreview != + GameConstants::OBSERVER_SLOTNAME + && fileExists(factionDefinitionXML) == true) { + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 tech [%s] faction [%s]\n", + currentTechName_factionPreview.c_str(), + currentFactionName_factionPreview.c_str()); + + XmlTree xmlTree; + std::map < string, string > mapExtraTagReplacementValues; + xmlTree.load(factionDefinitionXML, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues)); + const XmlNode * + factionNode = xmlTree.getRootNode(); + if (won == true) { + if (factionNode->hasAttribute("battle-end-win-video") == true) { + factionVideoUrl = + factionNode-> + getAttribute("battle-end-win-video")->getValue(); + } + } else { + if (factionNode->hasAttribute("battle-end-lose-video") == true) { + factionVideoUrl = + factionNode-> + getAttribute("battle-end-lose-video")->getValue(); + } + } + + if (factionVideoUrl != "" && fileExists(factionVideoUrl) == false) { + string techTreePath = ""; + string factionPath = ""; + std::vector < std::string > factionPartsList; + Tokenize(factionDefinitionXML, factionPartsList, "factions/"); + if (factionPartsList.size() > 1) { + techTreePath = factionPartsList[0]; + + string + factionPath = + techTreePath + "factions/" + + currentFactionName_factionPreview; + endPathWithSlash(factionPath); + factionVideoUrl = factionPath + factionVideoUrl; + } + } + + if (won == true) { + factionVideoUrlFallback = + Game::findFactionLogoFile(gameSettings, NULL, + "battle_end_win_video.*"); + } else { + factionVideoUrlFallback = + Game::findFactionLogoFile(gameSettings, NULL, + "battle_end_lose_video.*"); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n", + factionVideoUrl.c_str(), + factionVideoUrlFallback.c_str()); + //printf("#3 factionVideoUrl [%s] factionVideoUrlFallback [%s]\n",factionVideoUrl.c_str(),factionVideoUrlFallback.c_str()); + + if (factionVideoUrl == "") { + factionVideoUrl = factionVideoUrlFallback; + factionVideoUrlFallback = ""; + } + } + //printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); + } + + if (factionVideoUrl != "") { + result.first = factionVideoUrl; + result.second = factionVideoUrlFallback; + } else { + result.first = + CoreData::getInstance().getBattleEndVideoFilename(won); + result.second = + CoreData::getInstance().getBattleEndVideoFilenameFallback(won); + } + + return result; + } + + string BattleEnd::getBattleEndMusic(bool won) { + string result = ""; + string resultFallback = ""; + + if (gameSettings != NULL) { + string currentTechName_factionPreview = gameSettings->getTech(); + string + currentFactionName_factionPreview = + gameSettings->getFactionTypeName(stats.getThisFactionIndex()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 tech [%s] faction [%s] won = %d\n", + currentTechName_factionPreview.c_str(), + currentFactionName_factionPreview.c_str(), won); + + string + factionDefinitionXML = + Game::findFactionLogoFile(gameSettings, NULL, + currentFactionName_factionPreview + + ".xml"); + if (factionDefinitionXML != "" + && currentFactionName_factionPreview != + GameConstants::RANDOMFACTION_SLOTNAME + && currentFactionName_factionPreview != + GameConstants::OBSERVER_SLOTNAME + && fileExists(factionDefinitionXML) == true) { + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 tech [%s] faction [%s]\n", + currentTechName_factionPreview.c_str(), + currentFactionName_factionPreview.c_str()); + + XmlTree xmlTree; + std::map < string, string > mapExtraTagReplacementValues; + xmlTree.load(factionDefinitionXML, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues)); + const XmlNode * + factionNode = xmlTree.getRootNode(); + if (won == true) { + if (factionNode->hasAttribute("battle-end-win-music") == true) { + result = + factionNode-> + getAttribute("battle-end-win-music")->getValue(); + } + } else { + if (factionNode->hasAttribute("battle-end-lose-music") == true) { + result = + factionNode-> + getAttribute("battle-end-lose-music")->getValue(); + } + } + + if (result != "" && fileExists(result) == false) { + string techTreePath = ""; + string factionPath = ""; + std::vector < std::string > factionPartsList; + Tokenize(factionDefinitionXML, factionPartsList, "factions/"); + if (factionPartsList.size() > 1) { + techTreePath = factionPartsList[0]; + + string + factionPath = + techTreePath + "factions/" + + currentFactionName_factionPreview; + endPathWithSlash(factionPath); + result = factionPath + result; + } + } + + if (won == true) { + resultFallback = + Game::findFactionLogoFile(gameSettings, NULL, + "battle_end_win_music.*"); + } else { + resultFallback = + Game::findFactionLogoFile(gameSettings, NULL, + "battle_end_lose_music.*"); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#3 result [%s] resultFallback [%s]\n", result.c_str(), + resultFallback.c_str()); + //printf("#3 result [%s] resultFallback [%s]\n",result.c_str(),resultFallback.c_str()); + + if (result == "") { + result = resultFallback; + } + } + //printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); + } + + if (result == "") { + result = CoreData::getInstance().getBattleEndMusicFilename(won); + } + + return result; + } + + void + BattleEnd::initBackgroundMusic() { + string music = ""; + + if (stats.getThisFactionIndex() >= 0 + && stats.getTeam(stats.getThisFactionIndex()) != + GameConstants::maxPlayers - 1 + fpt_Observer) { + if (stats.getVictory(stats.getThisFactionIndex())) { + //header += lang.getString("Victory"); + music = getBattleEndMusic(true); + } else { + //header += lang.getString("Defeat"); + music = getBattleEndMusic(false); + } + + if (music != "" && fileExists(music) == true) { + printf("music [%s] \n", music.c_str()); + + battleEndMusic.open(music); + battleEndMusic.setNext(&battleEndMusic); + + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.playMusic(&battleEndMusic); + } + } + } + + void + BattleEnd::initBackgroundVideo() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true) { + + if (menuBackgroundVideo != NULL) { + menuBackgroundVideo->closePlayer(); + delete menuBackgroundVideo; + menuBackgroundVideo = NULL; + } + + string videoFile = ""; + string videoFileFallback = ""; + + if (stats.getThisFactionIndex() >= 0 + && stats.getTeam(stats.getThisFactionIndex()) != + GameConstants::maxPlayers - 1 + fpt_Observer) { + if (stats.getVictory(stats.getThisFactionIndex())) { + //header += lang.getString("Victory"); + + //videoFile = CoreData::getInstance().getBattleEndVideoFilename(true); + //videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(true); + std::pair < string, string > wonVideos = getBattleEndVideo(true); + videoFile = wonVideos.first; + videoFileFallback = wonVideos.second; + } else { + //header += lang.getString("Defeat"); + //videoFile = CoreData::getInstance().getBattleEndVideoFilename(false); + //videoFileFallback = CoreData::getInstance().getBattleEndVideoFilenameFallback(false); + std::pair < string, string > lostVideos = + getBattleEndVideo(false); + videoFile = lostVideos.first; + videoFileFallback = lostVideos.second; + } + } else { + //header += "Observer"; + } + + if (fileExists(videoFile) || fileExists(videoFileFallback)) { + printf("videoFile [%s] videoFileFallback [%s]\n", + videoFile.c_str(), videoFileFallback.c_str()); + + Context * + c = GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl * + glCtx = + static_cast (c)->getPlatformContextGlPtr(); + SDL_Window * + window = glCtx->getScreenWindow(); + SDL_Surface * + screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + menuBackgroundVideo = new VideoPlayer(&Renderer::getInstance(), + videoFile, + videoFileFallback, + window, + 0, 0, + screen->w, + screen->h, + screen->format->BitsPerPixel, + true, + vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + menuBackgroundVideo->initPlayer(); + } + } + } + + void + BattleEnd::update() { + if (Config::getInstance().getBool("AutoTest")) { + AutoTest::getInstance().updateBattleEnd(program); + return; + } + mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; + + if (this->stats.getIsMasterserverMode() == true) { + if (program->getWantShutdownApplicationAfterGame() == true) { + program->setShutdownApplicationEnabled(true); + return; + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //program->setState(new MainMenu(program)); + program->initServer(program->getWindow(), false, true, true); + return; + } + } + + void + BattleEnd::render() { + if (this->stats.getIsMasterserverMode() == true) { + return; + } + Renderer & renderer = Renderer::getInstance(); + //CoreData &coreData= CoreData::getInstance(); + + canRender(); + incrementFps(); + + //printf("In [%s::%s Line: %d] renderToTexture [%p]\n",__FILE__,__FUNCTION__,__LINE__,renderToTexture); + if (menuBackgroundVideo == NULL && renderToTexture != NULL) { + //printf("Rendering from texture!\n"); + + renderer.clearBuffers(); + renderer.reset3dMenu(); + renderer.clearZBuffer(); + + renderer.reset2d(); + + renderer.renderBackground(renderToTexture); + + renderer.renderButton(&buttonExit); + + //exit message box + if (mainMessageBox.getEnabled() && mainMessageBox.getVisible()) { + renderer.renderMessageBox(&mainMessageBox); + } + + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + } else { + //printf("Rendering to texture!\n"); + + if (menuBackgroundVideo == NULL && renderToTextureCount >= 300) { + renderer.beginRenderToTexture(&renderToTexture); + } + + TextRenderer2D * + textRenderer2D = renderer.getTextRenderer(); + TextRenderer3D * + textRenderer3D = renderer.getTextRenderer3D(); + TextRenderer * + textRenderer = NULL; + + if (Renderer::renderText3DEnabled == true) { + textRenderer = textRenderer3D; + } else { + textRenderer = textRenderer2D; + } + + Lang & lang = Lang::getInstance(); + + renderer.clearBuffers(); + renderer.reset3dMenu(); + renderer.clearZBuffer(); + renderer.reset2d(); + + if (menuBackgroundVideo != NULL) { + //printf("Rendering video not null!\n"); + + if (menuBackgroundVideo->isPlaying() == true) { + menuBackgroundVideo->playFrame(false); + + //printf("Rendering video playing!\n"); + } else { + if (menuBackgroundVideo != NULL) { + //initBackgroundVideo(); + menuBackgroundVideo->RestartVideo(); + } + } + } else { + renderer. + renderBackground(CoreData:: + getInstance().getBackgroundTexture()); + } + + //int winnerIndex = -1; + int + bestScore = -1; + //int mostKillsIndex = -1; + int + bestKills = -1; + //int mostEnemyKillsIndex = -1; + int + bestEnemyKills = -1; + //int leastDeathsIndex = -1; + int + leastDeaths = -1; + //int mostUnitsProducedIndex = -1; + int + bestUnitsProduced = -1; + //int mostResourcesHarvestedIndex = -1; + int + bestResourcesHarvested = -1; + + for (int i = 0; i < stats.getFactionCount(); ++i) { + if (stats.getTeam(i) == + GameConstants::maxPlayers - 1 + fpt_Observer) { + continue; + } + + //int team= stats.getTeam(i) + 1; + int + kills = stats.getKills(i); + if (kills > bestKills) { + bestKills = kills; + //mostKillsIndex = i; + } + + int + enemykills = stats.getEnemyKills(i); + if (enemykills > bestEnemyKills) { + bestEnemyKills = enemykills; + //mostEnemyKillsIndex = i; + } + + int + deaths = stats.getDeaths(i); + if (deaths < leastDeaths || leastDeaths < 0) { + leastDeaths = deaths; + //leastDeathsIndex = i; + } + + int + unitsProduced = stats.getUnitsProduced(i); + if (unitsProduced > bestUnitsProduced) { + bestUnitsProduced = unitsProduced; + //mostUnitsProducedIndex = i; + } + + int + resourcesHarvested = stats.getResourcesHarvested(i); + if (resourcesHarvested > bestResourcesHarvested) { + bestResourcesHarvested = resourcesHarvested; + //mostResourcesHarvestedIndex = i; + } + + int + score = + enemykills * 100 + unitsProduced * 50 + resourcesHarvested / 10; + + if (score > bestScore) { + bestScore = score; + //winnerIndex = i; + } + } + + bool + disableStatsColorCoding = + Config::getInstance().getBool("DisableBattleEndColorCoding", + "false"); + + if (Renderer::renderText3DEnabled == true) { + textRenderer3D-> + begin(CoreData::getInstance().getMenuFontNormal3D()); + } else { + textRenderer2D-> + begin(CoreData::getInstance().getMenuFontNormal()); + } + + int + lm = 20; + int + bm = 100; + + int + realPlayerCount = 0; + for (int i = 0; i < stats.getFactionCount(); ++i) { + if (stats.getTeam(i) == + GameConstants::maxPlayers - 1 + fpt_Observer) { + continue; + } + + realPlayerCount++; + int + textX = lm + 60 + (realPlayerCount * 100); + int + team = stats.getTeam(i) + 1; + int + kills = stats.getKills(i); + int + enemykills = stats.getEnemyKills(i); + int + deaths = stats.getDeaths(i); + int + unitsProduced = stats.getUnitsProduced(i); + int + resourcesHarvested = stats.getResourcesHarvested(i); + + int + score = + enemykills * 100 + unitsProduced * 50 + resourcesHarvested / 10; + string controlString; + + if (stats.getPersonalityType(i) == fpt_Observer) { + controlString = GameConstants::OBSERVER_SLOTNAME; + } else { + switch (stats.getControl(i)) { + case ctCpuEasy: + controlString = lang.getString("CpuEasy"); + break; + case ctCpu: + controlString = lang.getString("Cpu"); + break; + case ctCpuUltra: + controlString = lang.getString("CpuUltra"); + break; + case ctCpuZeta: + controlString = lang.getString("CpuZeta"); + break; + case ctNetwork: + controlString = lang.getString("Network"); + break; + case ctHuman: + controlString = lang.getString("Human"); + break; + + case ctNetworkCpuEasy: + controlString = lang.getString("NetworkCpuEasy"); + break; + case ctNetworkCpu: + controlString = lang.getString("NetworkCpu"); + break; + case ctNetworkCpuUltra: + controlString = lang.getString("NetworkCpuUltra"); + break; + case ctNetworkCpuZeta: + controlString = lang.getString("NetworkCpuZeta"); + break; + + default: + printf("Error control = %d for i = %d\n", stats.getControl(i), + i); + assert(false); + break; + }; + } + + if (stats.getControl(i) != ctHuman + && stats.getControl(i) != ctNetwork) { + controlString += + "\nx " + floatToStr(stats.getResourceMultiplier(i), 1); + } else if (stats.getPlayerLeftBeforeEnd(i) == true) { + controlString += + "\n" + lang.getString("CpuUltra") + "\nx " + + floatToStr(stats.getResourceMultiplier(i), 1); + } + + if (score == bestScore && stats.getVictory(i)) { + if (CoreData::getInstance().getGameWinnerTexture() != NULL) { + renderer.renderTextureQuad(textX, bm + 420, -1, -1, + CoreData:: + getInstance().getGameWinnerTexture + (), 0.7f); + } + } + + Vec3f color = stats.getPlayerColor(i); + if (stats.getPlayerName(i) != "") { + string textToRender = stats.getPlayerName(i).substr(0, 11); + if (stats.getPlayerLeftBeforeEnd(i) == true) { + textToRender += + "\n(" + getTimeDuationString(stats.getTimePlayerLeft(i), + GameConstants::updateFps) + ")"; + } + + textRenderer->render(textToRender.c_str(), textX, bm + 400, + false, &color); + } else { + textRenderer->render((lang.getString("Player") + " " + + intToStr(i + 1)).c_str(), textX, + bm + 400, false, &color); + } + + Vec3f highliteColor = Vec3f(WHITE.x, WHITE.y, WHITE.z); + if (disableStatsColorCoding == false) { + highliteColor.x = 0.85f; + highliteColor.y = 0.8f; + highliteColor.z = 0.07f; + } + + if (stats.getPersonalityType(i) == fpt_Observer) { + textRenderer->render(lang.getString("GameOver").c_str(), textX, + bm + 360); + } else { + if (stats.getVictory(i)) { + textRenderer->render(stats. + getVictory(i) ? + lang.getString("Victory"). + c_str() : lang.getString("Defeat"). + c_str(), textX, bm + 360, false, + &highliteColor); + } else { + textRenderer->render(stats. + getVictory(i) ? + lang.getString("Victory"). + c_str() : lang.getString("Defeat"). + c_str(), textX, bm + 360); + } + } + + textRenderer->render(controlString, textX, bm + 320); + textRenderer->render(stats.getFactionTypeName(i), textX, + bm + 280); + textRenderer->render(intToStr(team).c_str(), textX, bm + 240); + + if (kills == bestKills) { + textRenderer->render(intToStr(kills).c_str(), textX, bm + 200, + false, &highliteColor); + } else { + textRenderer->render(intToStr(kills).c_str(), textX, bm + 200); + } + if (enemykills == bestEnemyKills) { + textRenderer->render(intToStr(enemykills).c_str(), textX, + bm + 180, false, &highliteColor); + } else { + textRenderer->render(intToStr(enemykills).c_str(), textX, + bm + 180); + } + if (deaths == leastDeaths) { + textRenderer->render(intToStr(deaths).c_str(), textX, bm + 160, + false, &highliteColor); + } else { + textRenderer->render(intToStr(deaths).c_str(), textX, + bm + 160); + } + if (unitsProduced == bestUnitsProduced) { + textRenderer->render(intToStr(unitsProduced).c_str(), textX, + bm + 120, false, &highliteColor); + } else { + textRenderer->render(intToStr(unitsProduced).c_str(), textX, + bm + 120); + } + if (resourcesHarvested == bestResourcesHarvested) { + textRenderer->render(intToStr(resourcesHarvested).c_str(), + textX, bm + 80, false, &highliteColor); + } else { + textRenderer->render(intToStr(resourcesHarvested).c_str(), + textX, bm + 80); + } + if (score == bestScore) { + textRenderer->render(intToStr(score).c_str(), textX, bm + 20, + false, &highliteColor); + } else { + textRenderer->render(intToStr(score).c_str(), textX, bm + 20); + } + } + + textRenderer->render("\n" + (lang.getString("LeftAt")), lm, + bm + 400); + textRenderer->render(lang.getString("Result"), lm, bm + 360); + textRenderer->render(lang.getString("Control"), lm, bm + 320); + textRenderer->render(lang.getString("Faction"), lm, bm + 280); + textRenderer->render(lang.getString("Team"), lm, bm + 240); + textRenderer->render(lang.getString("Kills"), lm, bm + 200); + textRenderer->render(lang.getString("EnemyKills"), lm, bm + 180); + textRenderer->render(lang.getString("Deaths"), lm, bm + 160); + textRenderer->render(lang.getString("UnitsProduced"), lm, bm + 120); + textRenderer->render(lang.getString("ResourcesHarvested"), lm, + bm + 80); + textRenderer->render(lang.getString("Score"), lm, bm + 20); + + textRenderer->end(); + + if (Renderer::renderText3DEnabled == true) { + textRenderer3D-> + begin(CoreData::getInstance().getMenuFontVeryBig3D()); + } else { + textRenderer2D-> + begin(CoreData::getInstance().getMenuFontVeryBig()); + } + + string header = stats.getDescription() + " - "; + + if (stats.getThisFactionIndex() >= 0 + && stats.getTeam(stats.getThisFactionIndex()) != + GameConstants::maxPlayers - 1 + fpt_Observer) { + if (stats.getVictory(stats.getThisFactionIndex())) { + header += lang.getString("Victory"); + } else { + header += lang.getString("Defeat"); + } + } else { + header += "Observer"; + } + textRenderer->render(header, lm + 250, bm + 550); + + //GameConstants::updateFps + //string header2 = lang.getString("GameDurationTime","",true) + " " + floatToStr(stats.getWorldTimeElapsed() / 24.0,2); + + string + header2 = + lang.getString("GameDurationTime", "", + true) + ": " + + getTimeDuationString(stats.getFramesToCalculatePlaytime(), + GameConstants::updateFps); + textRenderer->render(header2, lm + 250, bm + 530); + + header2 = + lang.getString("GameMaxConcurrentUnitCount") + ": " + + intToStr(stats.getMaxConcurrentUnitCount()); + textRenderer->render(header2, lm + 250, bm + 510); + + header2 = + lang.getString("GameTotalEndGameConcurrentUnitCount") + ": " + + intToStr(stats.getTotalEndGameConcurrentUnitCount()); + textRenderer->render(header2, lm + 250, bm + 490); + + textRenderer->end(); + + renderer.renderButton(&buttonExit); + + //exit message box + if (mainMessageBox.getEnabled() && renderToTextureCount < 300) { + renderer.renderMessageBox(&mainMessageBox); + } + + if (menuBackgroundVideo == NULL && renderToTexture == NULL) { + renderer.renderMouse2d(mouseX, mouseY, mouse2d, 0.f); + } + + if (menuBackgroundVideo == NULL && renderToTextureCount >= 300) { + renderer.endRenderToTexture(&renderToTexture); + } + + if (menuBackgroundVideo == NULL) { + if (renderToTexture == NULL && renderToTextureCount < 300) { + renderToTextureCount++; + } + } + } + + renderer.renderFPSWhenEnabled(lastFps); + + renderer.swapBuffers(); + } + + void + BattleEnd::keyDown(SDL_KeyboardEvent key) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + if (isKeyPressed(SDLK_ESCAPE, key) == true) { + //program->setState(new MainMenu(program)); + + if (mainMessageBox.getEnabled()) { + mainMessageBox.setEnabled(false); + } else { + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("ExitToRootMenu"), "", true); + } + } else if (isKeyPressed(SDLK_RETURN, key) + && mainMessageBox.getEnabled()) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + program->setState(new MainMenu(program)); + } + } + + void + BattleEnd::mouseDownLeft(int x, int y) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", + __FILE__, __FUNCTION__, __LINE__); + //program->setState(new MainMenu(program)); + + if (buttonExit.mouseClick(x, y)) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + program->setState(new MainMenu(program)); + } else if (mainMessageBox.getEnabled()) { + int + button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + if (button == 0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + program->setState(new MainMenu(program)); + } else { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //close message box + mainMessageBox.setEnabled(false); + } + } + } + + } + + void + BattleEnd::mouseMove(int x, int y, const MouseState * ms) { + mouseX = x; + mouseY = y; + + buttonExit.mouseMove(x, y); + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + + } + + void + BattleEnd::showMessageBox(const string & text, const string & header, + bool toggle) { + if (toggle == false) { + mainMessageBox.setEnabled(false); + } + + if (mainMessageBox.getEnabled() == false) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + } } //end namespace diff --git a/source/glest_game/main/battle_end.h b/source/glest_game/main/battle_end.h index 4a5bec66b..11d3000cb 100644 --- a/source/glest_game/main/battle_end.h +++ b/source/glest_game/main/battle_end.h @@ -21,67 +21,63 @@ # include "stats.h" # include "leak_dumper.h" -namespace Shared -{ - namespace Graphics - { - class VideoPlayer; - } +namespace Shared { + namespace Graphics { + class VideoPlayer; + } } -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class GameSettings; -// ===================================================== -// class BattleEnd -// -/// ProgramState representing the end of the game -// ===================================================== + class GameSettings; + // ===================================================== + // class BattleEnd + // + /// ProgramState representing the end of the game + // ===================================================== - class BattleEnd:public ProgramState - { - private: - Stats stats; + class BattleEnd :public ProgramState { + private: + Stats stats; - GraphicButton buttonExit; - int mouse2d; - GraphicMessageBox mainMessageBox; - Texture2D *renderToTexture; - uint64 renderToTextureCount; - ProgramState *originState; - const char *containerName; + GraphicButton buttonExit; + int mouse2d; + GraphicMessageBox mainMessageBox; + Texture2D *renderToTexture; + uint64 renderToTextureCount; + ProgramState *originState; + const char *containerName; - ::Shared::Graphics::VideoPlayer * menuBackgroundVideo; - GameSettings *gameSettings; - StrSound battleEndMusic; + ::Shared::Graphics::VideoPlayer * menuBackgroundVideo; + GameSettings *gameSettings; + StrSound battleEndMusic; - void showMessageBox (const string & text, const string & header, - bool toggle); + void showMessageBox(const string & text, const string & header, + bool toggle); - public: - BattleEnd (Program * program, const Stats * stats, - ProgramState * originState); - ~BattleEnd (); + public: + BattleEnd(Program * program, const Stats * stats, + ProgramState * originState); + ~BattleEnd(); - virtual void update (); - virtual void render (); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void mouseDownLeft (int x, int y); - virtual void mouseMove (int x, int y, const MouseState * ms); -//virtual void tick(); - virtual void reloadUI (); + virtual void update(); + virtual void render(); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void mouseDownLeft(int x, int y); + virtual void mouseMove(int x, int y, const MouseState * ms); + //virtual void tick(); + virtual void reloadUI(); - private: + private: - void initBackgroundVideo (); - std::pair < string, string > getBattleEndVideo (bool won); - string getBattleEndMusic (bool won); - void initBackgroundMusic (); - }; + void initBackgroundVideo(); + std::pair < string, string > getBattleEndVideo(bool won); + string getBattleEndMusic(bool won); + void initBackgroundMusic(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/main/intro.cpp b/source/glest_game/main/intro.cpp index 21263045e..fb0dfdd5c 100644 --- a/source/glest_game/main/intro.cpp +++ b/source/glest_game/main/intro.cpp @@ -30,1110 +30,979 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using - namespace - Shared::Graphics; +namespace + Shared::Graphics; using - namespace - Shared::Xml; - -namespace Glest -{ - namespace Game - { - -//struct Timer { -//public: -// Timer () -// : current_time (0.0), last_time (0.0) { } -// -//public: -// void update () { -// last_time = current_time; -// current_time = static_cast(SDL_GetTicks ()) / 1000.0; -// } -// -// double deltaTime () const { -// return (current_time - last_time); -// } -// -//public: -// double current_time; -// double last_time; -// -//} animTimer; - -// ===================================================== -// class Text -// ===================================================== - - IntroText::IntroText (const string & text, const Vec2i & pos, int time, - Font2D * font, Font3D * font3D) - { - this->text = text; - this->pos = pos; - this->time = time; - this->texture = NULL; - this->font = font; - this->font3D = font3D; - } - - IntroText::IntroText (const Texture2D * texture, const Vec2i & pos, - const Vec2i & size, int time) - { - this->pos = pos; - this->size = size; - this->time = time; - this->texture = texture; - this->font = NULL; - this->font3D = NULL; - } - -// ===================================================== -// class Intro -// ===================================================== - - int - Intro::introTime = 50000; - int - Intro::appearTime = 2500; - int - Intro::showTime = 3500; - int - Intro::disapearTime = 2500; - - Intro::Intro (Program * program): - ProgramState (program) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false && - (::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == false - || CoreData::getInstance ().hasIntroVideoFilename () == false)) - { - soundRenderer.playMusic (CoreData::getInstance ().getIntroMusic ()); - } - - const - Metrics & - metrics = Metrics::getInstance (); - int - w = metrics.getVirtualW (); - int - h = metrics.getVirtualH (); - timer = 0; - mouseX = 0; - mouseY = 0; - mouse2d = 0; - exitAfterIntroVideo = false; - - Renderer & renderer = Renderer::getInstance (); -//renderer.init3dListMenu(NULL); - renderer.initMenu (NULL); - fade = 0.f; - anim = 0.f; - targetCamera = NULL; - t = 0.f; - - startPosition.x = 5;; - startPosition.y = 10; - startPosition.z = 40; - camera.setPosition (startPosition); - - Vec3f startRotation; - startRotation.x = 0; - startRotation.y = 0; - startRotation.z = 0; - - camera.setOrientation (Quaternion - (EulerAngles - (degToRad (startRotation.x), - degToRad (startRotation.y), - degToRad (startRotation.z)))); - - Intro::introTime = 3000; - Intro::appearTime = 500; - Intro::showTime = 500; - Intro::disapearTime = 500; - int - showIntroPics = 0; - int - showIntroPicsTime = 0; - bool showIntroPicsRandom = false; - bool showIntroModels = false; - bool showIntroModelsRandom = false; - modelMinAnimSpeed = 0; - modelMaxAnimSpeed = 0; - - XmlTree xmlTree; - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - xmlTree.load (getGameCustomCoreDataPath - (data_path, "data/core/menu/menu.xml"), - Properties::getTagReplacementValues ()); - const XmlNode * - menuNode = xmlTree.getRootNode (); - - if (menuNode->hasChild ("intro") == true) - { - const XmlNode * - introNode = menuNode->getChild ("intro"); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, - __FUNCTION__, __LINE__); - -//camera - const XmlNode * - cameraNode = introNode->getChild ("camera"); - -//position - const XmlNode * - positionNode = cameraNode->getChild ("start-position"); - startPosition.x = positionNode->getAttribute ("x")->getFloatValue (); - startPosition.y = positionNode->getAttribute ("y")->getFloatValue (); - startPosition.z = positionNode->getAttribute ("z")->getFloatValue (); - camera.setPosition (startPosition); - -//rotation - const XmlNode * - rotationNode = cameraNode->getChild ("start-rotation"); - Vec3f startRotation; - startRotation.x = rotationNode->getAttribute ("x")->getFloatValue (); - startRotation.y = rotationNode->getAttribute ("y")->getFloatValue (); - startRotation.z = rotationNode->getAttribute ("z")->getFloatValue (); - camera.setOrientation (Quaternion - (EulerAngles - (degToRad (startRotation.x), - degToRad (startRotation.y), - degToRad (startRotation.z)))); - -// intro info - const XmlNode * - introTimeNode = introNode->getChild ("intro-time"); - Intro::introTime = - introTimeNode->getAttribute ("value")->getIntValue (); - const XmlNode * - appearTimeNode = introNode->getChild ("appear-time"); - Intro::appearTime = - appearTimeNode->getAttribute ("value")->getIntValue (); - const XmlNode * - showTimeNode = introNode->getChild ("show-time"); - Intro::showTime = - showTimeNode->getAttribute ("value")->getIntValue (); - const XmlNode * - disappearTimeNode = introNode->getChild ("disappear-time"); - Intro::disapearTime = - disappearTimeNode->getAttribute ("value")->getIntValue (); - const XmlNode * - showIntroPicturesNode = introNode->getChild ("show-intro-pictures"); - showIntroPics = - showIntroPicturesNode->getAttribute ("value")->getIntValue (); - showIntroPicsTime = - showIntroPicturesNode->getAttribute ("time")->getIntValue (); - showIntroPicsRandom = - showIntroPicturesNode->getAttribute ("random")->getBoolValue (); - - const XmlNode * - showIntroModelsNode = introNode->getChild ("show-intro-models"); - showIntroModels = - showIntroModelsNode->getAttribute ("value")->getBoolValue (); - showIntroModelsRandom = - showIntroModelsNode->getAttribute ("random")->getBoolValue (); - modelMinAnimSpeed = - showIntroModelsNode-> - getAttribute ("min-anim-speed")->getFloatValue (); - modelMaxAnimSpeed = - showIntroModelsNode-> - getAttribute ("max-anim-speed")->getFloatValue (); - } - -//load main model - modelIndex = 0; - models.clear (); - if (showIntroModels == true) - { - -//getGameCustomCoreDataPath(data_path, "data/core/menu/menu.xml") - string - introPath = - getGameCustomCoreDataPath (data_path, - "") + - "data/core/menu/main_model/intro*.g3d"; - vector < string > introModels; - findAll (introPath, introModels, false, false); - for (int i = 0; i < (int) introModels.size (); ++i) - { - string logo = introModels[i]; - Model * - model = renderer.newModel (rsMenu, - getGameCustomCoreDataPath (data_path, - "") + - "data/core/menu/main_model/" + logo); - if (model) - { - models.push_back (model); -//printf("#1 Intro model [%s]\n",model->getFileName().c_str()); - } - } - - if (models.empty () == true) - { - introPath = data_path + "data/core/menu/main_model/intro*.g3d"; -//vector introModels; - findAll (introPath, introModels, false, false); - for (int i = 0; i < (int) introModels.size (); ++i) - { - string logo = introModels[i]; - Model * - model = renderer.newModel (rsMenu, - data_path + - "data/core/menu/main_model/" + logo); - if (model) - { - models.push_back (model); -//printf("#2 Intro model [%s]\n",model->getFileName().c_str()); - } - } - } - - if (showIntroModelsRandom == true) - { - std::vector < Model * >modelList; - -//unsigned int seed = time(NULL); - Chrono seed (true); - srand ((unsigned int) seed.getCurTicks ()); - int - failedLookups = 0; - std::map < int, - bool > - usedIndex; - for (; modelList.size () < models.size ();) - { - int - index = rand () % models.size (); - if (usedIndex.find (index) != usedIndex.end ()) - { - failedLookups++; - srand ((unsigned int) seed.getCurTicks () / failedLookups); - continue; - } -//printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); - modelList.push_back (models[index]); - usedIndex[index] = true; - srand ((unsigned int) seed.getCurTicks () / - (unsigned int) modelList.size ()); - } - models = modelList; - } - } - - int - displayItemNumber = 1; - int - appear = Intro::appearTime; - int - disappear = - Intro::showTime + Intro::appearTime + (Intro::disapearTime * 2); - - const unsigned int - maxIntroLines = 100; - Lang & lang = Lang::getInstance (); - for (unsigned int i = 1; i < maxIntroLines; ++i) - { - string introTagName = "IntroText" + intToStr (i); - string introTagTextureName = "IntroTexture" + intToStr (i); - - if (lang.hasString (introTagName, "", true) == true || - lang.hasString (introTagTextureName, "", true) == true) - { - string lineText = ""; - - if (lang.hasString (introTagName, "", true) == true) - { - lineText = lang.getString (introTagName, "", true); - } - - string showStartTime = "IntroStartMilliseconds" + intToStr (i); - - int - displayTime = appear; - if (lang.hasString (showStartTime, "", true) == true) - { - displayTime = strToInt (lang.getString (showStartTime, "", true)); - } - else - { - if (i == 1) - { - displayTime = appear; - } - else if (i == 2) - { - displayTime = disappear; - } - else if (i >= 3) - { - displayTime = disappear * (++displayItemNumber); - } - } - -// Is this a texture? - if (lang.hasString (introTagName, "", true) == false && - lang.hasString (introTagTextureName, "", true) == true) - { - - string - introTagTextureWidthName = "IntroTextureWidth" + intToStr (i); - string - introTagTextureHeightName = "IntroTextureHeight" + intToStr (i); - - lineText = lang.getString (introTagTextureName, "", true); - Texture2D * - logoTexture = renderer.newTexture2D (rsGlobal); - if (logoTexture) - { - logoTexture->setMipmap (false); - logoTexture->getPixmap ()->load (lineText); - - renderer.initTexture (rsGlobal, logoTexture); - } - - - int - textureWidth = 256; - if (logoTexture != NULL) - { - textureWidth = logoTexture->getTextureWidth (); - } - if (lang.hasString (introTagTextureWidthName, "", true) == true) - { - textureWidth = - strToInt (lang.getString - (introTagTextureWidthName, "", true)); - } - - int - textureHeight = 128; - if (logoTexture != NULL) - { - textureHeight = logoTexture->getTextureHeight (); - } - if (lang.hasString (introTagTextureHeightName, "", true) == true) - { - textureHeight = - strToInt (lang.getString - (introTagTextureHeightName, "", true)); - } - - texts.push_back (new - IntroText (logoTexture, - Vec2i (w / 2 - (textureWidth / 2), - h / 2 - (textureHeight / 2)), - Vec2i (textureWidth, textureHeight), - displayTime)); - } -// This is a line of text - else - { - string introTagTextXName = "IntroTextX" + intToStr (i); - string introTagTextYName = "IntroTextY" + intToStr (i); - string - introTagTextFontTypeName = "IntroTextFontType" + intToStr (i); - - int - textX = -1; - if (lang.hasString (introTagTextXName, "", true) == true) - { - string value = lang.getString (introTagTextXName, "", true); - if (value.length () > 0 && (value[0] == '+' || value[0] == '-')) - { - textX = w / 2 + strToInt (value); - } - else - { - textX = strToInt (value); - } - } - - int - textY = -1; - if (lang.hasString (introTagTextYName, "", true) == true) - { - string value = lang.getString (introTagTextYName, "", true); - if (value.length () > 0 && (value[0] == '+' || value[0] == '-')) - { - textY = h / 2 + strToInt (value); - } - else - { - textY = strToInt (value); - } - } - - Font2D * - font = coreData.getMenuFontVeryBig (); - Font3D * - font3d = coreData.getMenuFontVeryBig3D (); - - if (lang.hasString (introTagTextFontTypeName, "", true) == true) - { - string - value = lang.getString (introTagTextFontTypeName, "", true); - if (value == "displaynormal") - { - font = coreData.getDisplayFont (); - font3d = coreData.getDisplayFont3D (); - } - else if (value == "displaysmall") - { - font = coreData.getDisplayFontSmall (); - font3d = coreData.getDisplayFontSmall3D (); - } - else if (value == "menunormal") - { - font = coreData.getMenuFontNormal (); - font3d = coreData.getMenuFontNormal3D (); - } - else if (value == "menubig") - { - font = coreData.getMenuFontBig (); - font3d = coreData.getMenuFontBig3D (); - } - else if (value == "menuverybig") - { - font = coreData.getMenuFontVeryBig (); - font3d = coreData.getMenuFontVeryBig3D (); - } - else if (value == "consolenormal") - { - font = coreData.getConsoleFont (); - font3d = coreData.getConsoleFont3D (); - } - - } - texts.push_back (new - IntroText (lineText, Vec2i (textX, textY), - displayTime, font, font3d)); - } - } - else - { - break; - } - } - modelShowTime = disappear * (displayItemNumber); - if (lang.hasString ("IntroModelStartMilliseconds", "", true) == true) - { - modelShowTime = - strToInt (lang.getString ("IntroModelStartMilliseconds", "", true)); - } - else - { - modelShowTime = disappear * (displayItemNumber); - } - -/* -string lineText = "Based on award-winning classic Glest"; -texts.push_back(new Text(lineText, Vec2i(-1, -1), appear, coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); -lineText = "the MegaGlest Team presents"; -texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear, coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); -lineText = "a libre software real-time strategy game"; -texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear *(++displayItemNumber), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); - -texts.push_back(new Text(coreData.getLogoTexture(), Vec2i(w/2-128, h/2-64), Vec2i(256, 128), disappear *(++displayItemNumber))); -texts.push_back(new Text(glestVersionString, Vec2i(w/2+45, h/2-45), disappear *(displayItemNumber++), coreData.getMenuFontNormal(),coreData.getMenuFontNormal3D())); -lineText = "www.megaglest.org"; -//texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear *(displayItemNumber++), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); -texts.push_back(new Text(lineText, Vec2i(-1, h/2-45-18), disappear *(displayItemNumber-1), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); -*/ - - - - if (showIntroPics > 0 && coreData.getMiscTextureList ().size () > 0) - { - const int - showMiscTime = showIntroPicsTime; - - std::vector < Texture2D * >intoTexList; - if (showIntroPicsRandom == true) - { -//unsigned int seed = time(NULL); - Chrono seed (true); - srand ((unsigned int) seed.getCurTicks ()); - int - failedLookups = 0; - std::map < int, - bool > - usedIndex; - for (; (int) intoTexList.size () < showIntroPics;) - { - int - picIndex = rand () % coreData.getMiscTextureList ().size (); - if (usedIndex.find (picIndex) != usedIndex.end ()) - { - failedLookups++; - srand ((unsigned int) seed.getCurTicks () / failedLookups); - - if (failedLookups > 10000) - { - for (unsigned int i = 0; - i < coreData.getMiscTextureList ().size (); ++i) - { - if (usedIndex.find (i) == usedIndex.end ()) - { - picIndex = i; - break; - } - } - } - else - { - continue; - } - } -//printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); - intoTexList.push_back (coreData.getMiscTextureList ()[picIndex]); - usedIndex[picIndex] = true; - srand ((unsigned int) seed.getCurTicks () / - (unsigned int) intoTexList.size ()); - } - } - else - { - for (unsigned int i = 0; - i < (unsigned int) coreData.getMiscTextureList ().size () && - i < (unsigned int) showIntroPics; ++i) - { - Texture2D * - tex = coreData.getMiscTextureList ()[i]; - intoTexList.push_back (tex); - } - } - - for (unsigned int i = 0; i < intoTexList.size (); ++i) - { - Texture2D * - tex = intoTexList[i]; -//printf("tex # %d [%s]\n",i,tex->getPath().c_str()); - - Vec2i texPlacement; - if (i == 0 || i % 9 == 0) - { - texPlacement = Vec2i (1, h - tex->getTextureHeight ()); - } - else if (i == 1 || i % 9 == 1) - { - texPlacement = Vec2i (1, 1); - } - else if (i == 2 || i % 9 == 2) - { - texPlacement = Vec2i (w - tex->getTextureWidth (), 1); - } - else if (i == 3 || i % 9 == 3) - { - texPlacement = - Vec2i (w - tex->getTextureWidth (), - h - tex->getTextureHeight ()); - } - else if (i == 4 || i % 9 == 4) - { - texPlacement = - Vec2i (w / 2 - tex->getTextureWidth () / 2, - h - tex->getTextureHeight ()); - } - else if (i == 5 || i % 9 == 5) - { - texPlacement = Vec2i (w / 2 - tex->getTextureWidth () / 2, 1); - } - else if (i == 6 || i % 9 == 6) - { - texPlacement = - Vec2i (1, (h / 2) - (tex->getTextureHeight () / 2)); - } - else if (i == 7 || i % 9 == 7) - { - texPlacement = - Vec2i (w - tex->getTextureWidth (), - (h / 2) - (tex->getTextureHeight () / 2)); - } - - int - textureStartTime = disappear * displayItemNumber; - if (lang.hasString ("IntroTextureStartMilliseconds", "", true) == - true) - { - textureStartTime = - strToInt (lang.getString ("IntroTextureStartMilliseconds", "", - true)); - } - - texts.push_back (new - IntroText (tex, texPlacement, - Vec2i (tex->getTextureWidth (), - tex->getTextureHeight ()), - textureStartTime + - (showMiscTime * (i + 1)))); - } - } - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - renderer.swapBuffers (); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == true - && CoreData::getInstance ().hasIntroVideoFilename () == true) - { - string - introVideoFile = CoreData::getInstance ().getIntroVideoFilename (); - string - introVideoFileFallback = - CoreData::getInstance ().getIntroVideoFilenameFallback (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Intro Video [%s] [%s]\n", introVideoFile.c_str (), - introVideoFileFallback.c_str ()); - - Context * - c = GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl * - glCtx = static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window * - window = glCtx->getScreenWindow (); - SDL_Surface * - screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); -//printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); - ::Shared::Graphics::VideoPlayer player (&Renderer::getInstance (), - introVideoFile, - introVideoFileFallback, - window, - 0, 0, - screen->w, - screen->h, - screen->format->BitsPerPixel, - false, - vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - player.PlayVideo (); - exitAfterIntroVideo = true; - return; - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - Intro::~Intro () - { - deleteValues (texts.begin (), texts.end ()); - -//deleteValues(introTextureList.begin(),introTextureList.end()); -// if(test) { -// glmDelete(test); -// } - -//Shared::Graphics::md5::cleanupMD5OpenGL(); - } - - void - Intro::update () - { - if (exitAfterIntroVideo == true) - { - mouseUpLeft (0, 0); -//cleanup(); - return; - } - timer++; - if (timer > introTime * GameConstants::updateFps / 1000) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - cleanup (); - return; - } - - if (Config::getInstance ().getBool ("AutoTest")) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - AutoTest::getInstance ().updateIntro (program); - return; - } - - mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; - - if (targetCamera != NULL) - { - t += - ((0.01f + - (1.f - t) / 10.f) / 20.f) * (60.f / GameConstants::updateFps); - -//interpolate position - camera.setPosition (lastCamera. - getPosition ().lerp (t, - targetCamera-> - getPosition ())); - -//interpolate orientation - Quaternion - q = - lastCamera.getOrientation ().lerp (t, - targetCamera->getOrientation ()); - camera.setOrientation (q); - - if (t >= 1.f) - { - targetCamera = NULL; - t = 0.f; - } - } - -//fade - if (fade <= 1.f) - { - fade += 0.6f / GameConstants::updateFps; - if (fade > 1.f) - { - fade = 1.f; - } - } - -//animation -//const float minSpeed = 0.015f; -//const float minSpeed = 0.010f; -//const float maxSpeed = 0.6f; - const float - minSpeed = modelMinAnimSpeed; - const float - maxSpeed = modelMaxAnimSpeed; - anim += - (maxSpeed / GameConstants::updateFps) / 5 + - random.randRange (minSpeed, - max (minSpeed + 0.0001f, - (maxSpeed / GameConstants::updateFps) / 5.f)); - if (anim > 1.f) - { - anim = 0.f; - } - -//animTimer.update(); - } - - void - Intro::renderModelBackground () - { -// Black background - glClearColor (0, 0, 0, 1); - - if (models.empty () == false) - { - int - difTime = 1000 * timer / GameConstants::updateFps - modelShowTime; - int - totalModelShowTime = Intro::introTime - modelShowTime; - int - individualModelShowTime = totalModelShowTime / (int) models.size (); - -//printf("difTime = %d individualModelShowTime = %d modelIndex = %d\n",difTime,individualModelShowTime,modelIndex); - -//int difTime= 1; - if (difTime > 0) - { - if (difTime > ((modelIndex + 1) * individualModelShowTime)) - { -//int oldmodelIndex = modelIndex; - if (modelIndex + 1 < (int) models.size ()) - { - modelIndex++; - -//position -//nextCamera.setPosition(camera.getPosition()); -// nextCamera.setPosition(Vec3f(84,-9,11)); -// -// //rotation -// //Vec3f startRotation(0,12,0); -// Vec3f startRotation(0,-80,0); -// nextCamera.setOrientation(Quaternion(EulerAngles( -// degToRad(startRotation.x), -// degToRad(startRotation.y), -// degToRad(startRotation.z)))); -// -// this->targetCamera = &nextCamera; -// this->lastCamera= camera; -// this->t= 0.f; - - } -//printf("oldmodelIndex = %d, modelIndex = %d\n",oldmodelIndex,modelIndex); - } - Renderer & renderer = Renderer::getInstance (); - vector < Model * >characterModels; - characterModels.push_back (NULL); - characterModels.push_back (NULL); - characterModels.push_back (models[modelIndex]); - const - Vec3f - characterPosition = startPosition; - renderer.renderMenuBackground (&camera, fade, NULL, characterModels, - characterPosition, anim); - } - } - } - - void - Intro::render () - { - Renderer & renderer = Renderer::getInstance (); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - return; - } - - canRender (); - incrementFps (); - - renderer.clearBuffers (); - renderer.reset3dMenu (); - - renderer.clearZBuffer (); - renderer.loadCameraMatrix (&camera); - - renderModelBackground (); - renderer.renderParticleManager (rsMenu); - - renderer.reset2d (); - - for (int i = 0; i < (int) texts.size (); ++i) - { - IntroText * - text = texts[i]; - - int - difTime = - 1000 * timer / GameConstants::updateFps - text->getTime (); - - if (difTime > 0 && difTime < appearTime + showTime + disapearTime) - { - float - alpha = 1.f; - if (difTime > 0 && difTime < appearTime) - { -//apearing - alpha = static_cast < float >(difTime) / - appearTime; - } - else if (difTime > 0 - && difTime < appearTime + showTime + disapearTime) - { -//disappearing - alpha = - 1.f - static_cast < float >(difTime - appearTime - showTime) / - disapearTime; - } - - if (text->getText ().empty () == false) - { - int - renderX = text->getPos ().x; - int - renderY = text->getPos ().y; - - if (Renderer::renderText3DEnabled) - { - if (renderX < 0) - { - const - Metrics & - metrics = Metrics::getInstance (); - int - w = metrics.getVirtualW (); - renderX = - (w / 2) - - (text->getFont3D ()-> - getMetrics ()->getTextWidth (text->getText ()) / 2); - } - if (renderY < 0) - { - const - Metrics & - metrics = Metrics::getInstance (); - int - h = metrics.getVirtualH (); - renderY = - (h / 2) + - (text->getFont3D ()-> - getMetrics ()->getHeight (text->getText ()) / 2); - } - - renderer.renderText3D (text->getText (), text->getFont3D (), - alpha, renderX, renderY, false); - } - else - { - if (renderX < 0) - { - const - Metrics & - metrics = Metrics::getInstance (); - int - w = metrics.getVirtualW (); - renderX = (w / 2); - } - if (renderY < 0) - { - const - Metrics & - metrics = Metrics::getInstance (); - int - h = metrics.getVirtualH (); - renderY = (h / 2); - } - - renderer.renderText (text->getText (), text->getFont (), alpha, - renderX, renderY, true); - } - } - - if (text->getTexture () != NULL) - { - renderer.renderTextureQuad (text->getPos ().x, text->getPos ().y, - text->getSize ().x, - text->getSize ().y, - text->getTexture (), alpha); - } - } - } - - if (program != NULL) - program->renderProgramMsgBox (); - - if (this->forceMouseRender == true) - renderer.renderMouse2d (mouseX, mouseY, mouse2d, 0.f); - - bool - showIntroTiming = - Config::getInstance ().getBool ("ShowIntroTiming", "false"); - if (showIntroTiming == true && Intro::introTime > 0) - { - CoreData & coreData = CoreData::getInstance (); - int - difTime = 1000 * timer / GameConstants::updateFps; - string - timingText = - intToStr (difTime) + " / " + intToStr (Intro::introTime); - - if (Renderer::renderText3DEnabled) - { -//const Metrics &metrics= Metrics::getInstance(); -//int w= metrics.getVirtualW(); -//int h= metrics.getVirtualH(); - - renderer.renderText3D (timingText, coreData.getMenuFontVeryBig3D (), - 1, 10, 20, false); - } - else - { -//const Metrics &metrics= Metrics::getInstance(); -//int w= metrics.getVirtualW(); -//int h= metrics.getVirtualH(); - - renderer.renderText (timingText, coreData.getMenuFontVeryBig (), 1, - 10, 20, false); - } - } - - renderer.renderFPSWhenEnabled (lastFps); - renderer.swapBuffers (); - } - - void - Intro::keyDown (SDL_KeyboardEvent key) - { - SDL_keysym keystate = key.keysym; -//printf("keystate.mod = %d key = unicode[%d] regular[%d] lalt [%d] ralt [%d] alt [%d]\n",keystate.mod,key.keysym.unicode,key.keysym.sym,(keystate.mod & KMOD_LALT),(keystate.mod & KMOD_RALT),(keystate.mod & KMOD_ALT)); - - if (keystate.mod & (KMOD_LALT | KMOD_RALT)) - { -//printf("ALT KEY #1\n"); - - if (isKeyPressed (SDLK_RETURN, key) == true || - isKeyPressed (SDLK_RALT, key) == true || - isKeyPressed (SDLK_LALT, key) == true) - { - return; - } - } - -//printf("Exiting intro\n"); - mouseUpLeft (0, 0); - } - - void - Intro::mouseUpLeft (int x, int y) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.stopMusic (CoreData::getInstance ().getIntroMusic ()); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (CoreData::getInstance ().hasMainMenuVideoFilename () == false) - { - soundRenderer.playMusic (CoreData::getInstance ().getMenuMusic ()); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - cleanup (); - } - - void - Intro::cleanup () - { - Renderer::getInstance ().endMenu (); - - program->setState (new MainMenu (program)); - } - - void - Intro::mouseMove (int x, int y, const MouseState * ms) - { - mouseX = x; - mouseY = y; - } - -}} //end namespace +namespace + Shared::Xml; + +namespace Glest { + namespace Game { + + //struct Timer { + //public: + // Timer () + // : current_time (0.0), last_time (0.0) { } + // + //public: + // void update () { + // last_time = current_time; + // current_time = static_cast(SDL_GetTicks ()) / 1000.0; + // } + // + // double deltaTime () const { + // return (current_time - last_time); + // } + // + //public: + // double current_time; + // double last_time; + // + //} animTimer; + + // ===================================================== + // class Text + // ===================================================== + + IntroText::IntroText(const string & text, const Vec2i & pos, int time, + Font2D * font, Font3D * font3D) { + this->text = text; + this->pos = pos; + this->time = time; + this->texture = NULL; + this->font = font; + this->font3D = font3D; + } + + IntroText::IntroText(const Texture2D * texture, const Vec2i & pos, + const Vec2i & size, int time) { + this->pos = pos; + this->size = size; + this->time = time; + this->texture = texture; + this->font = NULL; + this->font3D = NULL; + } + + // ===================================================== + // class Intro + // ===================================================== + + int + Intro::introTime = 50000; + int + Intro::appearTime = 2500; + int + Intro::showTime = 3500; + int + Intro::disapearTime = 2500; + + Intro::Intro(Program * program) : + ProgramState(program) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false && + (::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == false + || CoreData::getInstance().hasIntroVideoFilename() == false)) { + soundRenderer.playMusic(CoreData::getInstance().getIntroMusic()); + } + + const + Metrics & + metrics = Metrics::getInstance(); + int + w = metrics.getVirtualW(); + int + h = metrics.getVirtualH(); + timer = 0; + mouseX = 0; + mouseY = 0; + mouse2d = 0; + exitAfterIntroVideo = false; + + Renderer & renderer = Renderer::getInstance(); + //renderer.init3dListMenu(NULL); + renderer.initMenu(NULL); + fade = 0.f; + anim = 0.f; + targetCamera = NULL; + t = 0.f; + + startPosition.x = 5;; + startPosition.y = 10; + startPosition.z = 40; + camera.setPosition(startPosition); + + Vec3f startRotation; + startRotation.x = 0; + startRotation.y = 0; + startRotation.z = 0; + + camera.setOrientation(Quaternion + (EulerAngles + (degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); + + Intro::introTime = 3000; + Intro::appearTime = 500; + Intro::showTime = 500; + Intro::disapearTime = 500; + int + showIntroPics = 0; + int + showIntroPicsTime = 0; + bool showIntroPicsRandom = false; + bool showIntroModels = false; + bool showIntroModelsRandom = false; + modelMinAnimSpeed = 0; + modelMaxAnimSpeed = 0; + + XmlTree xmlTree; + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + xmlTree.load(getGameCustomCoreDataPath + (data_path, "data/core/menu/menu.xml"), + Properties::getTagReplacementValues()); + const XmlNode * + menuNode = xmlTree.getRootNode(); + + if (menuNode->hasChild("intro") == true) { + const XmlNode * + introNode = menuNode->getChild("intro"); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //camera + const XmlNode * + cameraNode = introNode->getChild("camera"); + + //position + const XmlNode * + positionNode = cameraNode->getChild("start-position"); + startPosition.x = positionNode->getAttribute("x")->getFloatValue(); + startPosition.y = positionNode->getAttribute("y")->getFloatValue(); + startPosition.z = positionNode->getAttribute("z")->getFloatValue(); + camera.setPosition(startPosition); + + //rotation + const XmlNode * + rotationNode = cameraNode->getChild("start-rotation"); + Vec3f startRotation; + startRotation.x = rotationNode->getAttribute("x")->getFloatValue(); + startRotation.y = rotationNode->getAttribute("y")->getFloatValue(); + startRotation.z = rotationNode->getAttribute("z")->getFloatValue(); + camera.setOrientation(Quaternion + (EulerAngles + (degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); + + // intro info + const XmlNode * + introTimeNode = introNode->getChild("intro-time"); + Intro::introTime = + introTimeNode->getAttribute("value")->getIntValue(); + const XmlNode * + appearTimeNode = introNode->getChild("appear-time"); + Intro::appearTime = + appearTimeNode->getAttribute("value")->getIntValue(); + const XmlNode * + showTimeNode = introNode->getChild("show-time"); + Intro::showTime = + showTimeNode->getAttribute("value")->getIntValue(); + const XmlNode * + disappearTimeNode = introNode->getChild("disappear-time"); + Intro::disapearTime = + disappearTimeNode->getAttribute("value")->getIntValue(); + const XmlNode * + showIntroPicturesNode = introNode->getChild("show-intro-pictures"); + showIntroPics = + showIntroPicturesNode->getAttribute("value")->getIntValue(); + showIntroPicsTime = + showIntroPicturesNode->getAttribute("time")->getIntValue(); + showIntroPicsRandom = + showIntroPicturesNode->getAttribute("random")->getBoolValue(); + + const XmlNode * + showIntroModelsNode = introNode->getChild("show-intro-models"); + showIntroModels = + showIntroModelsNode->getAttribute("value")->getBoolValue(); + showIntroModelsRandom = + showIntroModelsNode->getAttribute("random")->getBoolValue(); + modelMinAnimSpeed = + showIntroModelsNode-> + getAttribute("min-anim-speed")->getFloatValue(); + modelMaxAnimSpeed = + showIntroModelsNode-> + getAttribute("max-anim-speed")->getFloatValue(); + } + + //load main model + modelIndex = 0; + models.clear(); + if (showIntroModels == true) { + + //getGameCustomCoreDataPath(data_path, "data/core/menu/menu.xml") + string + introPath = + getGameCustomCoreDataPath(data_path, + "") + + "data/core/menu/main_model/intro*.g3d"; + vector < string > introModels; + findAll(introPath, introModels, false, false); + for (int i = 0; i < (int) introModels.size(); ++i) { + string logo = introModels[i]; + Model * + model = renderer.newModel(rsMenu, + getGameCustomCoreDataPath(data_path, + "") + + "data/core/menu/main_model/" + logo); + if (model) { + models.push_back(model); + //printf("#1 Intro model [%s]\n",model->getFileName().c_str()); + } + } + + if (models.empty() == true) { + introPath = data_path + "data/core/menu/main_model/intro*.g3d"; + //vector introModels; + findAll(introPath, introModels, false, false); + for (int i = 0; i < (int) introModels.size(); ++i) { + string logo = introModels[i]; + Model * + model = renderer.newModel(rsMenu, + data_path + + "data/core/menu/main_model/" + logo); + if (model) { + models.push_back(model); + //printf("#2 Intro model [%s]\n",model->getFileName().c_str()); + } + } + } + + if (showIntroModelsRandom == true) { + std::vector < Model * >modelList; + + //unsigned int seed = time(NULL); + Chrono seed(true); + srand((unsigned int) seed.getCurTicks()); + int + failedLookups = 0; + std::map < int, + bool > + usedIndex; + for (; modelList.size() < models.size();) { + int + index = rand() % models.size(); + if (usedIndex.find(index) != usedIndex.end()) { + failedLookups++; + srand((unsigned int) seed.getCurTicks() / failedLookups); + continue; + } + //printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); + modelList.push_back(models[index]); + usedIndex[index] = true; + srand((unsigned int) seed.getCurTicks() / + (unsigned int) modelList.size()); + } + models = modelList; + } + } + + int + displayItemNumber = 1; + int + appear = Intro::appearTime; + int + disappear = + Intro::showTime + Intro::appearTime + (Intro::disapearTime * 2); + + const unsigned int + maxIntroLines = 100; + Lang & lang = Lang::getInstance(); + for (unsigned int i = 1; i < maxIntroLines; ++i) { + string introTagName = "IntroText" + intToStr(i); + string introTagTextureName = "IntroTexture" + intToStr(i); + + if (lang.hasString(introTagName, "", true) == true || + lang.hasString(introTagTextureName, "", true) == true) { + string lineText = ""; + + if (lang.hasString(introTagName, "", true) == true) { + lineText = lang.getString(introTagName, "", true); + } + + string showStartTime = "IntroStartMilliseconds" + intToStr(i); + + int + displayTime = appear; + if (lang.hasString(showStartTime, "", true) == true) { + displayTime = strToInt(lang.getString(showStartTime, "", true)); + } else { + if (i == 1) { + displayTime = appear; + } else if (i == 2) { + displayTime = disappear; + } else if (i >= 3) { + displayTime = disappear * (++displayItemNumber); + } + } + + // Is this a texture? + if (lang.hasString(introTagName, "", true) == false && + lang.hasString(introTagTextureName, "", true) == true) { + + string + introTagTextureWidthName = "IntroTextureWidth" + intToStr(i); + string + introTagTextureHeightName = "IntroTextureHeight" + intToStr(i); + + lineText = lang.getString(introTagTextureName, "", true); + Texture2D * + logoTexture = renderer.newTexture2D(rsGlobal); + if (logoTexture) { + logoTexture->setMipmap(false); + logoTexture->getPixmap()->load(lineText); + + renderer.initTexture(rsGlobal, logoTexture); + } + + + int + textureWidth = 256; + if (logoTexture != NULL) { + textureWidth = logoTexture->getTextureWidth(); + } + if (lang.hasString(introTagTextureWidthName, "", true) == true) { + textureWidth = + strToInt(lang.getString + (introTagTextureWidthName, "", true)); + } + + int + textureHeight = 128; + if (logoTexture != NULL) { + textureHeight = logoTexture->getTextureHeight(); + } + if (lang.hasString(introTagTextureHeightName, "", true) == true) { + textureHeight = + strToInt(lang.getString + (introTagTextureHeightName, "", true)); + } + + texts.push_back(new + IntroText(logoTexture, + Vec2i(w / 2 - (textureWidth / 2), + h / 2 - (textureHeight / 2)), + Vec2i(textureWidth, textureHeight), + displayTime)); + } + // This is a line of text + else { + string introTagTextXName = "IntroTextX" + intToStr(i); + string introTagTextYName = "IntroTextY" + intToStr(i); + string + introTagTextFontTypeName = "IntroTextFontType" + intToStr(i); + + int + textX = -1; + if (lang.hasString(introTagTextXName, "", true) == true) { + string value = lang.getString(introTagTextXName, "", true); + if (value.length() > 0 && (value[0] == '+' || value[0] == '-')) { + textX = w / 2 + strToInt(value); + } else { + textX = strToInt(value); + } + } + + int + textY = -1; + if (lang.hasString(introTagTextYName, "", true) == true) { + string value = lang.getString(introTagTextYName, "", true); + if (value.length() > 0 && (value[0] == '+' || value[0] == '-')) { + textY = h / 2 + strToInt(value); + } else { + textY = strToInt(value); + } + } + + Font2D * + font = coreData.getMenuFontVeryBig(); + Font3D * + font3d = coreData.getMenuFontVeryBig3D(); + + if (lang.hasString(introTagTextFontTypeName, "", true) == true) { + string + value = lang.getString(introTagTextFontTypeName, "", true); + if (value == "displaynormal") { + font = coreData.getDisplayFont(); + font3d = coreData.getDisplayFont3D(); + } else if (value == "displaysmall") { + font = coreData.getDisplayFontSmall(); + font3d = coreData.getDisplayFontSmall3D(); + } else if (value == "menunormal") { + font = coreData.getMenuFontNormal(); + font3d = coreData.getMenuFontNormal3D(); + } else if (value == "menubig") { + font = coreData.getMenuFontBig(); + font3d = coreData.getMenuFontBig3D(); + } else if (value == "menuverybig") { + font = coreData.getMenuFontVeryBig(); + font3d = coreData.getMenuFontVeryBig3D(); + } else if (value == "consolenormal") { + font = coreData.getConsoleFont(); + font3d = coreData.getConsoleFont3D(); + } + + } + texts.push_back(new + IntroText(lineText, Vec2i(textX, textY), + displayTime, font, font3d)); + } + } else { + break; + } + } + modelShowTime = disappear * (displayItemNumber); + if (lang.hasString("IntroModelStartMilliseconds", "", true) == true) { + modelShowTime = + strToInt(lang.getString("IntroModelStartMilliseconds", "", true)); + } else { + modelShowTime = disappear * (displayItemNumber); + } + + /* + string lineText = "Based on award-winning classic Glest"; + texts.push_back(new Text(lineText, Vec2i(-1, -1), appear, coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + lineText = "the MegaGlest Team presents"; + texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear, coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + lineText = "a libre software real-time strategy game"; + texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear *(++displayItemNumber), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + + texts.push_back(new Text(coreData.getLogoTexture(), Vec2i(w/2-128, h/2-64), Vec2i(256, 128), disappear *(++displayItemNumber))); + texts.push_back(new Text(glestVersionString, Vec2i(w/2+45, h/2-45), disappear *(displayItemNumber++), coreData.getMenuFontNormal(),coreData.getMenuFontNormal3D())); + lineText = "www.megaglest.org"; + //texts.push_back(new Text(lineText, Vec2i(-1, -1), disappear *(displayItemNumber++), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + texts.push_back(new Text(lineText, Vec2i(-1, h/2-45-18), disappear *(displayItemNumber-1), coreData.getMenuFontVeryBig(),coreData.getMenuFontVeryBig3D())); + */ + + + + if (showIntroPics > 0 && coreData.getMiscTextureList().size() > 0) { + const int + showMiscTime = showIntroPicsTime; + + std::vector < Texture2D * >intoTexList; + if (showIntroPicsRandom == true) { + //unsigned int seed = time(NULL); + Chrono seed(true); + srand((unsigned int) seed.getCurTicks()); + int + failedLookups = 0; + std::map < int, + bool > + usedIndex; + for (; (int) intoTexList.size() < showIntroPics;) { + int + picIndex = rand() % coreData.getMiscTextureList().size(); + if (usedIndex.find(picIndex) != usedIndex.end()) { + failedLookups++; + srand((unsigned int) seed.getCurTicks() / failedLookups); + + if (failedLookups > 10000) { + for (unsigned int i = 0; + i < coreData.getMiscTextureList().size(); ++i) { + if (usedIndex.find(i) == usedIndex.end()) { + picIndex = i; + break; + } + } + } else { + continue; + } + } + //printf("picIndex = %d list count = %d\n",picIndex,coreData.getMiscTextureList().size()); + intoTexList.push_back(coreData.getMiscTextureList()[picIndex]); + usedIndex[picIndex] = true; + srand((unsigned int) seed.getCurTicks() / + (unsigned int) intoTexList.size()); + } + } else { + for (unsigned int i = 0; + i < (unsigned int) coreData.getMiscTextureList().size() && + i < (unsigned int) showIntroPics; ++i) { + Texture2D * + tex = coreData.getMiscTextureList()[i]; + intoTexList.push_back(tex); + } + } + + for (unsigned int i = 0; i < intoTexList.size(); ++i) { + Texture2D * + tex = intoTexList[i]; + //printf("tex # %d [%s]\n",i,tex->getPath().c_str()); + + Vec2i texPlacement; + if (i == 0 || i % 9 == 0) { + texPlacement = Vec2i(1, h - tex->getTextureHeight()); + } else if (i == 1 || i % 9 == 1) { + texPlacement = Vec2i(1, 1); + } else if (i == 2 || i % 9 == 2) { + texPlacement = Vec2i(w - tex->getTextureWidth(), 1); + } else if (i == 3 || i % 9 == 3) { + texPlacement = + Vec2i(w - tex->getTextureWidth(), + h - tex->getTextureHeight()); + } else if (i == 4 || i % 9 == 4) { + texPlacement = + Vec2i(w / 2 - tex->getTextureWidth() / 2, + h - tex->getTextureHeight()); + } else if (i == 5 || i % 9 == 5) { + texPlacement = Vec2i(w / 2 - tex->getTextureWidth() / 2, 1); + } else if (i == 6 || i % 9 == 6) { + texPlacement = + Vec2i(1, (h / 2) - (tex->getTextureHeight() / 2)); + } else if (i == 7 || i % 9 == 7) { + texPlacement = + Vec2i(w - tex->getTextureWidth(), + (h / 2) - (tex->getTextureHeight() / 2)); + } + + int + textureStartTime = disappear * displayItemNumber; + if (lang.hasString("IntroTextureStartMilliseconds", "", true) == + true) { + textureStartTime = + strToInt(lang.getString("IntroTextureStartMilliseconds", "", + true)); + } + + texts.push_back(new + IntroText(tex, texPlacement, + Vec2i(tex->getTextureWidth(), + tex->getTextureHeight()), + textureStartTime + + (showMiscTime * (i + 1)))); + } + } + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + renderer.swapBuffers(); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true + && CoreData::getInstance().hasIntroVideoFilename() == true) { + string + introVideoFile = CoreData::getInstance().getIntroVideoFilename(); + string + introVideoFileFallback = + CoreData::getInstance().getIntroVideoFilenameFallback(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Intro Video [%s] [%s]\n", introVideoFile.c_str(), + introVideoFileFallback.c_str()); + + Context * + c = GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl * + glCtx = static_cast (c)->getPlatformContextGlPtr(); + SDL_Window * + window = glCtx->getScreenWindow(); + SDL_Surface * + screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + ::Shared::Graphics::VideoPlayer player(&Renderer::getInstance(), + introVideoFile, + introVideoFileFallback, + window, + 0, 0, + screen->w, + screen->h, + screen->format->BitsPerPixel, + false, + vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + player.PlayVideo(); + exitAfterIntroVideo = true; + return; + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + Intro::~Intro() { + deleteValues(texts.begin(), texts.end()); + + //deleteValues(introTextureList.begin(),introTextureList.end()); + // if(test) { + // glmDelete(test); + // } + + //Shared::Graphics::md5::cleanupMD5OpenGL(); + } + + void + Intro::update() { + if (exitAfterIntroVideo == true) { + mouseUpLeft(0, 0); + //cleanup(); + return; + } + timer++; + if (timer > introTime * GameConstants::updateFps / 1000) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + cleanup(); + return; + } + + if (Config::getInstance().getBool("AutoTest")) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + AutoTest::getInstance().updateIntro(program); + return; + } + + mouse2d = (mouse2d + 1) % Renderer::maxMouse2dAnim; + + if (targetCamera != NULL) { + t += + ((0.01f + + (1.f - t) / 10.f) / 20.f) * (60.f / GameConstants::updateFps); + + //interpolate position + camera.setPosition(lastCamera. + getPosition().lerp(t, + targetCamera-> + getPosition())); + + //interpolate orientation + Quaternion + q = + lastCamera.getOrientation().lerp(t, + targetCamera->getOrientation()); + camera.setOrientation(q); + + if (t >= 1.f) { + targetCamera = NULL; + t = 0.f; + } + } + + //fade + if (fade <= 1.f) { + fade += 0.6f / GameConstants::updateFps; + if (fade > 1.f) { + fade = 1.f; + } + } + + //animation + //const float minSpeed = 0.015f; + //const float minSpeed = 0.010f; + //const float maxSpeed = 0.6f; + const float + minSpeed = modelMinAnimSpeed; + const float + maxSpeed = modelMaxAnimSpeed; + anim += + (maxSpeed / GameConstants::updateFps) / 5 + + random.randRange(minSpeed, + max(minSpeed + 0.0001f, + (maxSpeed / GameConstants::updateFps) / 5.f)); + if (anim > 1.f) { + anim = 0.f; + } + + //animTimer.update(); + } + + void + Intro::renderModelBackground() { + // Black background + glClearColor(0, 0, 0, 1); + + if (models.empty() == false) { + int + difTime = 1000 * timer / GameConstants::updateFps - modelShowTime; + int + totalModelShowTime = Intro::introTime - modelShowTime; + int + individualModelShowTime = totalModelShowTime / (int) models.size(); + + //printf("difTime = %d individualModelShowTime = %d modelIndex = %d\n",difTime,individualModelShowTime,modelIndex); + + //int difTime= 1; + if (difTime > 0) { + if (difTime > ((modelIndex + 1) * individualModelShowTime)) { + //int oldmodelIndex = modelIndex; + if (modelIndex + 1 < (int) models.size()) { + modelIndex++; + + //position + //nextCamera.setPosition(camera.getPosition()); + // nextCamera.setPosition(Vec3f(84,-9,11)); + // + // //rotation + // //Vec3f startRotation(0,12,0); + // Vec3f startRotation(0,-80,0); + // nextCamera.setOrientation(Quaternion(EulerAngles( + // degToRad(startRotation.x), + // degToRad(startRotation.y), + // degToRad(startRotation.z)))); + // + // this->targetCamera = &nextCamera; + // this->lastCamera= camera; + // this->t= 0.f; + + } + //printf("oldmodelIndex = %d, modelIndex = %d\n",oldmodelIndex,modelIndex); + } + Renderer & renderer = Renderer::getInstance(); + vector < Model * >characterModels; + characterModels.push_back(NULL); + characterModels.push_back(NULL); + characterModels.push_back(models[modelIndex]); + const + Vec3f + characterPosition = startPosition; + renderer.renderMenuBackground(&camera, fade, NULL, characterModels, + characterPosition, anim); + } + } + } + + void + Intro::render() { + Renderer & renderer = Renderer::getInstance(); + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + return; + } + + canRender(); + incrementFps(); + + renderer.clearBuffers(); + renderer.reset3dMenu(); + + renderer.clearZBuffer(); + renderer.loadCameraMatrix(&camera); + + renderModelBackground(); + renderer.renderParticleManager(rsMenu); + + renderer.reset2d(); + + for (int i = 0; i < (int) texts.size(); ++i) { + IntroText * + text = texts[i]; + + int + difTime = + 1000 * timer / GameConstants::updateFps - text->getTime(); + + if (difTime > 0 && difTime < appearTime + showTime + disapearTime) { + float + alpha = 1.f; + if (difTime > 0 && difTime < appearTime) { + //apearing + alpha = static_cast (difTime) / + appearTime; + } else if (difTime > 0 + && difTime < appearTime + showTime + disapearTime) { + //disappearing + alpha = + 1.f - static_cast (difTime - appearTime - showTime) / + disapearTime; + } + + if (text->getText().empty() == false) { + int + renderX = text->getPos().x; + int + renderY = text->getPos().y; + + if (Renderer::renderText3DEnabled) { + if (renderX < 0) { + const + Metrics & + metrics = Metrics::getInstance(); + int + w = metrics.getVirtualW(); + renderX = + (w / 2) - + (text->getFont3D()-> + getMetrics()->getTextWidth(text->getText()) / 2); + } + if (renderY < 0) { + const + Metrics & + metrics = Metrics::getInstance(); + int + h = metrics.getVirtualH(); + renderY = + (h / 2) + + (text->getFont3D()-> + getMetrics()->getHeight(text->getText()) / 2); + } + + renderer.renderText3D(text->getText(), text->getFont3D(), + alpha, renderX, renderY, false); + } else { + if (renderX < 0) { + const + Metrics & + metrics = Metrics::getInstance(); + int + w = metrics.getVirtualW(); + renderX = (w / 2); + } + if (renderY < 0) { + const + Metrics & + metrics = Metrics::getInstance(); + int + h = metrics.getVirtualH(); + renderY = (h / 2); + } + + renderer.renderText(text->getText(), text->getFont(), alpha, + renderX, renderY, true); + } + } + + if (text->getTexture() != NULL) { + renderer.renderTextureQuad(text->getPos().x, text->getPos().y, + text->getSize().x, + text->getSize().y, + text->getTexture(), alpha); + } + } + } + + if (program != NULL) + program->renderProgramMsgBox(); + + if (this->forceMouseRender == true) + renderer.renderMouse2d(mouseX, mouseY, mouse2d, 0.f); + + bool + showIntroTiming = + Config::getInstance().getBool("ShowIntroTiming", "false"); + if (showIntroTiming == true && Intro::introTime > 0) { + CoreData & coreData = CoreData::getInstance(); + int + difTime = 1000 * timer / GameConstants::updateFps; + string + timingText = + intToStr(difTime) + " / " + intToStr(Intro::introTime); + + if (Renderer::renderText3DEnabled) { + //const Metrics &metrics= Metrics::getInstance(); + //int w= metrics.getVirtualW(); + //int h= metrics.getVirtualH(); + + renderer.renderText3D(timingText, coreData.getMenuFontVeryBig3D(), + 1, 10, 20, false); + } else { + //const Metrics &metrics= Metrics::getInstance(); + //int w= metrics.getVirtualW(); + //int h= metrics.getVirtualH(); + + renderer.renderText(timingText, coreData.getMenuFontVeryBig(), 1, + 10, 20, false); + } + } + + renderer.renderFPSWhenEnabled(lastFps); + renderer.swapBuffers(); + } + + void + Intro::keyDown(SDL_KeyboardEvent key) { + SDL_keysym keystate = key.keysym; + //printf("keystate.mod = %d key = unicode[%d] regular[%d] lalt [%d] ralt [%d] alt [%d]\n",keystate.mod,key.keysym.unicode,key.keysym.sym,(keystate.mod & KMOD_LALT),(keystate.mod & KMOD_RALT),(keystate.mod & KMOD_ALT)); + + if (keystate.mod & (KMOD_LALT | KMOD_RALT)) { + //printf("ALT KEY #1\n"); + + if (isKeyPressed(SDLK_RETURN, key) == true || + isKeyPressed(SDLK_RALT, key) == true || + isKeyPressed(SDLK_LALT, key) == true) { + return; + } + } + + //printf("Exiting intro\n"); + mouseUpLeft(0, 0); + } + + void + Intro::mouseUpLeft(int x, int y) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.stopMusic(CoreData::getInstance().getIntroMusic()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (CoreData::getInstance().hasMainMenuVideoFilename() == false) { + soundRenderer.playMusic(CoreData::getInstance().getMenuMusic()); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + cleanup(); + } + + void + Intro::cleanup() { + Renderer::getInstance().endMenu(); + + program->setState(new MainMenu(program)); + } + + void + Intro::mouseMove(int x, int y, const MouseState * ms) { + mouseX = x; + mouseY = y; + } + + } +} //end namespace diff --git a/source/glest_game/main/intro.h b/source/glest_game/main/intro.h index 600e527a9..1a19927dc 100644 --- a/source/glest_game/main/intro.h +++ b/source/glest_game/main/intro.h @@ -42,156 +42,146 @@ using Shared::Util::RandomGen; //class Md5Object; //}}} -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Text -// ===================================================== + // ===================================================== + // class Text + // ===================================================== - class IntroText - { - private: - string text; - Vec2i pos; - Vec2i size; - int - time; - Font2D * - font; - Font3D * - font3D; - const Texture2D * - texture; + class IntroText { + private: + string text; + Vec2i pos; + Vec2i size; + int + time; + Font2D * + font; + Font3D * + font3D; + const Texture2D * + texture; - public: - IntroText (const string & text, const Vec2i & pos, int time, - Font2D * font, Font3D * font3D); - IntroText (const Texture2D * texture, const Vec2i & pos, - const Vec2i & size, int time); + public: + IntroText(const string & text, const Vec2i & pos, int time, + Font2D * font, Font3D * font3D); + IntroText(const Texture2D * texture, const Vec2i & pos, + const Vec2i & size, int time); - const - string & - getText () const - { - return - text; - } - Font2D * - getFont () - { - return font; - } - Font3D * - getFont3D () - { - return font3D; - } - const - Vec2i & - getPos () const - { - return - pos; - } - const - Vec2i & - getSize () const - { - return - size; - } - int - getTime () const - { - return - time; - } - const Texture2D * - getTexture () const - { - return - texture; - } - }; + const + string & + getText() const { + return + text; + } + Font2D * + getFont() { + return font; + } + Font3D * + getFont3D() { + return font3D; + } + const + Vec2i & + getPos() const { + return + pos; + } + const + Vec2i & + getSize() const { + return + size; + } + int + getTime() const { + return + time; + } + const Texture2D * + getTexture() const { + return + texture; + } + }; -// ===================================================== -// class Intro -// -/// ProgramState representing the intro -// ===================================================== + // ===================================================== + // class Intro + // + /// ProgramState representing the intro + // ===================================================== - class Intro: - public ProgramState - { - private: - static int - introTime; - static int - appearTime; - static int - showTime; - static int - disapearTime; + class Intro : + public ProgramState { + private: + static int + introTime; + static int + appearTime; + static int + showTime; + static int + disapearTime; - private: - vector < IntroText * >texts; - int - timer; - int - mouse2d; + private: + vector < IntroText * >texts; + int + timer; + int + mouse2d; -//Model *mainModel; - int - modelIndex; - float - modelMinAnimSpeed; - float - modelMaxAnimSpeed; - vector < Model * >models; - Camera nextCamera; - Camera camera; - Camera lastCamera; - const Camera * - targetCamera; - float - t; - RandomGen random; - float - anim; - float - fade; - Vec3f startPosition; - int - modelShowTime; + //Model *mainModel; + int + modelIndex; + float + modelMinAnimSpeed; + float + modelMaxAnimSpeed; + vector < Model * >models; + Camera nextCamera; + Camera camera; + Camera lastCamera; + const Camera * + targetCamera; + float + t; + RandomGen random; + float + anim; + float + fade; + Vec3f startPosition; + int + modelShowTime; -//GLMmodel* test; -//Shared::Graphics::md5::Md5Object *md5Test; + //GLMmodel* test; + //Shared::Graphics::md5::Md5Object *md5Test; - bool exitAfterIntroVideo; - void - cleanup (); - void - renderModelBackground (); + bool exitAfterIntroVideo; + void + cleanup(); + void + renderModelBackground(); - public: - explicit Intro (Program * program); - virtual ~ Intro (); + public: + explicit Intro(Program * program); + virtual ~Intro(); - virtual void - update (); - virtual void - render (); - virtual void - keyDown (SDL_KeyboardEvent key); - virtual void - mouseUpLeft (int x, int y); - void - mouseMove (int x, int y, const MouseState * ms); - }; + virtual void + update(); + virtual void + render(); + virtual void + keyDown(SDL_KeyboardEvent key); + virtual void + mouseUpLeft(int x, int y); + void + mouseMove(int x, int y, const MouseState * ms); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 6f752ffba..c553c09e4 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -100,7 +100,7 @@ using namespace std; using namespace - Shared::Platform; +Shared::Platform; using namespace Shared::Util; using namespace Shared::Graphics; using namespace Shared::Graphics::Gl; @@ -116,9371 +116,8291 @@ using namespace Shared; * Namespace used for game related code. */ namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { - static - string - tempDataLocation = getUserHome (); - static - string - mg_app_name = ""; - static - string - mailStringSupport = ""; - static - bool - sdl_quitCalled = false; + static + string + tempDataLocation = getUserHome(); + static + string + mg_app_name = ""; + static + string + mailStringSupport = ""; + static + bool + sdl_quitCalled = false; - static - bool - disableheadless_console = false; - static - bool - disableBacktrace = false; - static - bool - gameInitialized = false; + static + bool + disableheadless_console = false; + static + bool + disableBacktrace = false; + static + bool + gameInitialized = false; - static Program * - mainProgram = NULL; - static FileCRCPreCacheThread * - preCacheThread = NULL; + static Program * + mainProgram = NULL; + static FileCRCPreCacheThread * + preCacheThread = NULL; #ifdef WIN32 - static - string - runtimeErrorMsg = ""; -// keeps in scope for duration of the application -//SocketManager *winSockManager = NULL; + static + string + runtimeErrorMsg = ""; + // keeps in scope for duration of the application + //SocketManager *winSockManager = NULL; #endif #ifdef HAVE_GOOGLE_BREAKPAD - auto_ptr < - google_breakpad::ExceptionHandler > - errorHandlerPtr; + auto_ptr < + google_breakpad::ExceptionHandler > + errorHandlerPtr; #endif - class - NavtiveLanguageNameListCacheGenerator: - public - SimpleTaskCallbackInterface - { - virtual void - simpleTask (BaseThread * callingThread, void *userdata) - { - Lang & lang = Lang::getInstance (); - lang. - getDiscoveredLanguageList (true); - } - }; + class + NavtiveLanguageNameListCacheGenerator : + public + SimpleTaskCallbackInterface { + virtual void + simpleTask(BaseThread * callingThread, void *userdata) { + Lang & lang = Lang::getInstance(); + lang. + getDiscoveredLanguageList(true); + } + }; -// ===================================================== -// class ExceptionHandler -// ===================================================== - class - ExceptionHandler: - public - PlatformExceptionHandler - { - public: + // ===================================================== + // class ExceptionHandler + // ===================================================== + class + ExceptionHandler : + public + PlatformExceptionHandler { + public: #if defined(__WIN32__) && !defined(__GNUC__) - virtual void - handle (LPEXCEPTION_POINTERS pointers); + virtual void + handle(LPEXCEPTION_POINTERS pointers); #endif - virtual void - handle (); + virtual void + handle(); - static void - logError (const char *msg, bool confirmToConsole); - static void - handleRuntimeError (const megaglest_runtime_error & ex); - static void - handleRuntimeError (const char *msg, bool getStackTraceString); - static int - DisplayMessage (const char *msg, bool exitApp); - }; + static void + logError(const char *msg, bool confirmToConsole); + static void + handleRuntimeError(const megaglest_runtime_error & ex); + static void + handleRuntimeError(const char *msg, bool getStackTraceString); + static int + DisplayMessage(const char *msg, bool exitApp); + }; - void - cleanupCRCThread () - { - if (preCacheThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + void + cleanupCRCThread() { + if (preCacheThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - const double - MAX_THREAD_WAIT = 60; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("START - shutting down crc threads\n"); - time_t - elapsed = time (NULL); - preCacheThread-> - signalQuit (); - for (; preCacheThread->canShutdown (false) == false && - difftime ((long int) time (NULL), elapsed) <= MAX_THREAD_WAIT;) - { - } - if (difftime ((long int) time (NULL), elapsed) <= MAX_THREAD_WAIT) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("B - shutting down crc threads\n"); + const double + MAX_THREAD_WAIT = 60; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("START - shutting down crc threads\n"); + time_t + elapsed = time(NULL); + preCacheThread-> + signalQuit(); + for (; preCacheThread->canShutdown(false) == false && + difftime((long int) time(NULL), elapsed) <= MAX_THREAD_WAIT;) { + } + if (difftime((long int) time(NULL), elapsed) <= MAX_THREAD_WAIT) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("B - shutting down crc threads\n"); - for (; preCacheThread->shutdownAndWait () == false && - difftime ((long int) time (NULL), elapsed) <= MAX_THREAD_WAIT;) - { - } - if (preCacheThread->getRunningStatus () == false) - { - delete - preCacheThread; - preCacheThread = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("C - shutting down crc threads\n"); - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("D - shutting down crc threads\n"); + for (; preCacheThread->shutdownAndWait() == false && + difftime((long int) time(NULL), elapsed) <= MAX_THREAD_WAIT;) { + } + if (preCacheThread->getRunningStatus() == false) { + delete + preCacheThread; + preCacheThread = NULL; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("C - shutting down crc threads\n"); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("D - shutting down crc threads\n"); - if (preCacheThread->canShutdown (false) == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - delete - preCacheThread; - preCacheThread = NULL; + if (preCacheThread->canShutdown(false) == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + delete + preCacheThread; + preCacheThread = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("E - shutting down crc threads\n"); - } - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("F - shutting down crc threads\n"); - preCacheThread = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("E - shutting down crc threads\n"); + } + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("F - shutting down crc threads\n"); + preCacheThread = NULL; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } - static void - cleanupProcessObjects () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + static void + cleanupProcessObjects() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - showCursor (true); - restoreVideoMode (::Shared::Platform::Window::getSDLWindow (), true); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + showCursor(true); + restoreVideoMode(::Shared::Platform::Window::getSDLWindow(), true); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 IRCCLient Cache SHUTDOWN\n"); - IRCThread *& - ircClient = - CacheManager::getCachedItem < - IRCThread * >(GameConstants::ircClientCacheLookupKey); - if (ircClient != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 IRCCLient Cache SHUTDOWN\n"); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 IRCCLient Cache SHUTDOWN\n"); + IRCThread *& + ircClient = + CacheManager::getCachedItem < + IRCThread * >(GameConstants::ircClientCacheLookupKey); + if (ircClient != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 IRCCLient Cache SHUTDOWN\n"); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - ircClient->disconnect (); - ircClient->signalQuit (); - ircClient = NULL; - sleep (0); - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#4 IRCCLient Cache SHUTDOWN\n"); + ircClient->disconnect(); + ircClient->signalQuit(); + ircClient = NULL; + sleep(0); + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#4 IRCCLient Cache SHUTDOWN\n"); - cleanupCRCThread (); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + cleanupCRCThread(); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (Renderer::isEnded () == false) - { - Renderer::getInstance ().end (); - CoreData & coreData = CoreData::getInstance (); - coreData.cleanup (); - } + if (Renderer::isEnded() == false) { + Renderer::getInstance().end(); + CoreData & coreData = CoreData::getInstance(); + coreData.cleanup(); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::Close (); - SystemFlags::SHUTDOWN_PROGRAM_MODE = true; + SystemFlags::Close(); + SystemFlags::SHUTDOWN_PROGRAM_MODE = true; -//printf("Closing IRC CLient %d\n",__LINE__); + //printf("Closing IRC CLient %d\n",__LINE__); - Thread::shutdownThreads (); + Thread::shutdownThreads(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("start running threads = " MG_SIZE_T_SPECIFIER "\n", - Thread::getThreadList ().size ()); - time_t - elapsed = time (NULL); - int - lastLazyThreadDump = 0; - for (; Thread::getThreadList ().size () > 0 && - difftime ((long int) time (NULL), elapsed) <= 10;) - { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("start running threads = " MG_SIZE_T_SPECIFIER "\n", + Thread::getThreadList().size()); + time_t + elapsed = time(NULL); + int + lastLazyThreadDump = 0; + for (; Thread::getThreadList().size() > 0 && + difftime((long int) time(NULL), elapsed) <= 10;) { - if (difftime ((long int) time (NULL), elapsed) > 1) - { - if (lastLazyThreadDump != - (int) difftime ((long int) time (NULL), elapsed)) - { - lastLazyThreadDump = difftime ((long int) time (NULL), elapsed); + if (difftime((long int) time(NULL), elapsed) > 1) { + if (lastLazyThreadDump != + (int) difftime((long int) time(NULL), elapsed)) { + lastLazyThreadDump = difftime((long int) time(NULL), elapsed); - printf ("Waiting for the following threads to exit [" - MG_SIZE_T_SPECIFIER "]:\n", - Thread::getThreadList ().size ()); + printf("Waiting for the following threads to exit [" + MG_SIZE_T_SPECIFIER "]:\n", + Thread::getThreadList().size()); - for (int i = 0; i < (int) Thread::getThreadList ().size (); ++i) - { - BaseThread * - baseThread = - dynamic_cast < BaseThread * >(Thread::getThreadList ()[i]); - printf - ("Thread index: %d ptr [%p] isBaseThread: %d, Name: [%s]\n", - i, baseThread, (baseThread != NULL ? 1 : 0), - (baseThread != - NULL ? baseThread->getUniqueID ().c_str () : "")); - } - } - } - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("end running threads = " MG_SIZE_T_SPECIFIER "\n", - Thread::getThreadList ().size ()); + for (int i = 0; i < (int) Thread::getThreadList().size(); ++i) { + BaseThread * + baseThread = + dynamic_cast (Thread::getThreadList()[i]); + printf + ("Thread index: %d ptr [%p] isBaseThread: %d, Name: [%s]\n", + i, baseThread, (baseThread != NULL ? 1 : 0), + (baseThread != + NULL ? baseThread->getUniqueID().c_str() : "")); + } + } + } + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("end running threads = " MG_SIZE_T_SPECIFIER "\n", + Thread::getThreadList().size()); - Thread::shutdownThreads (); + Thread::shutdownThreads(); - std::map < int, - Texture2D * >& - crcPlayerTextureCache = CacheManager::getCachedItem < std::map < int, - Texture2D * > >(GameConstants::playerTextureCacheLookupKey); - crcPlayerTextureCache.clear (); + std::map < int, + Texture2D * >& + crcPlayerTextureCache = CacheManager::getCachedItem < std::map < int, + Texture2D * > >(GameConstants::playerTextureCacheLookupKey); + crcPlayerTextureCache.clear(); - std::map < string, Texture2D * >&crcFactionPreviewTextureCache = - CacheManager::getCachedItem < std::map < string, - Texture2D * > >(GameConstants::factionPreviewTextureCacheLookupKey); - crcFactionPreviewTextureCache.clear (); + std::map < string, Texture2D * >&crcFactionPreviewTextureCache = + CacheManager::getCachedItem < std::map < string, + Texture2D * > >(GameConstants::factionPreviewTextureCacheLookupKey); + crcFactionPreviewTextureCache.clear(); - std::map < string, vector < FileReader < Pixmap2D > const *>*>& - list2d = FileReader < Pixmap2D >::getFileReadersMap (); - deleteMapValues (list2d.begin (), list2d.end ()); - std::map < string, vector < FileReader < Pixmap3D > const *>*>& - list3d = FileReader < Pixmap3D >::getFileReadersMap (); - deleteMapValues (list3d.begin (), list3d.end ()); + std::map < string, vector < FileReader < Pixmap2D > const *>*>& + list2d = FileReader < Pixmap2D >::getFileReadersMap(); + deleteMapValues(list2d.begin(), list2d.end()); + std::map < string, vector < FileReader < Pixmap3D > const *>*>& + list3d = FileReader < Pixmap3D >::getFileReadersMap(); + deleteMapValues(list3d.begin(), list3d.end()); #if defined(WANT_XERCES) - XmlIo::getInstance ().cleanup (); + XmlIo::getInstance().cleanup(); #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::globalCleanupHTTP (); - CacheManager::cleanupMutexes (); - } + SystemFlags::globalCleanupHTTP(); + CacheManager::cleanupMutexes(); + } #if defined(WIN32) && !defined(_DEBUG) && !defined(__GNUC__) - void - fatal (const char *s, ...) // failure exit - { - static int - errors = 0; - errors++; + void + fatal(const char *s, ...) // failure exit + { + static int + errors = 0; + errors++; - if (errors <= 5) - { // print up to two extra recursive errors - defvformatstring (msg, s, s); - string - errText = string (msg) + " [" + runtimeErrorMsg + "]"; -//puts(msg); - string - sErr = string (mg_app_name) + " fatal error"; - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", - errText.c_str ()); - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - errText.c_str ()); + if (errors <= 5) { // print up to two extra recursive errors + defvformatstring(msg, s, s); + string + errText = string(msg) + " [" + runtimeErrorMsg + "]"; + //puts(msg); + string + sErr = string(mg_app_name) + " fatal error"; + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", + errText.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + errText.c_str()); - if (errors <= 1) - { // avoid recursion - if (SDL_WasInit (SDL_INIT_VIDEO)) - { - SDL_SetRelativeMouseMode (SDL_FALSE); - } + if (errors <= 1) { // avoid recursion + if (SDL_WasInit(SDL_INIT_VIDEO)) { + SDL_SetRelativeMouseMode(SDL_FALSE); + } # ifdef WIN32 - LPWSTR - wstr = Ansi2WideString (errText.c_str ()); - LPWSTR - wstr1 = Ansi2WideString (sErr.c_str ()); + LPWSTR + wstr = Ansi2WideString(errText.c_str()); + LPWSTR + wstr1 = Ansi2WideString(sErr.c_str()); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - MessageBox (NULL, wstr, wstr1, MB_OK | MB_SYSTEMMODAL); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + MessageBox(NULL, wstr, wstr1, MB_OK | MB_SYSTEMMODAL); + } - if (wstr) - delete[]wstr; - if (wstr1) - delete[]wstr1; + if (wstr) + delete[]wstr; + if (wstr1) + delete[]wstr1; # endif - } - } + } + } -// Now try to shutdown threads if possible - delete - mainProgram; - mainProgram = NULL; -// END + // Now try to shutdown threads if possible + delete + mainProgram; + mainProgram = NULL; + // END - if (sdl_quitCalled == false) - { - sdl_quitCalled = true; - SDL_Quit (); - } - exit (EXIT_FAILURE); - } + if (sdl_quitCalled == false) { + sdl_quitCalled = true; + SDL_Quit(); + } + exit(EXIT_FAILURE); + } - std::string get_module_path (HMODULE module = 0) - { - char - path_name[MAX_PATH] = { }; - DWORD - size = GetModuleFileNameA (module, path_name, MAX_PATH); - return std::string (path_name, size); - } - void - write_module_name (string & out, HANDLE process, DWORD64 program_counter) - { - DWORD64 - module_base = SymGetModuleBase64 (process, program_counter); - if (module_base) - { - std::string module_name = - get_module_path (reinterpret_cast < HMODULE > (module_base)); - if (!module_name.empty ()) - out += module_name + "|"; - else - out += "Unknown module|"; - } - else - { - out += "Unknown module|"; - } - } + std::string get_module_path(HMODULE module = 0) { + char + path_name[MAX_PATH] = {}; + DWORD + size = GetModuleFileNameA(module, path_name, MAX_PATH); + return std::string(path_name, size); + } + void + write_module_name(string & out, HANDLE process, DWORD64 program_counter) { + DWORD64 + module_base = SymGetModuleBase64(process, program_counter); + if (module_base) { + std::string module_name = + get_module_path(reinterpret_cast (module_base)); + if (!module_name.empty()) + out += module_name + "|"; + else + out += "Unknown module|"; + } else { + out += "Unknown module|"; + } + } - void - write_function_name (string & out, HANDLE process, - DWORD64 program_counter) - { - SYMBOL_INFO_PACKAGE - sym = { - sizeof (sym) - }; - sym.si.MaxNameLen = MAX_SYM_NAME; - if (SymFromAddr (process, program_counter, 0, &sym.si)) - { - out += string (sym.si.Name) + "()"; - } - else - { - out += "Unknown function"; - } - } + void + write_function_name(string & out, HANDLE process, + DWORD64 program_counter) { + SYMBOL_INFO_PACKAGE + sym = { + sizeof(sym) + }; + sym.si.MaxNameLen = MAX_SYM_NAME; + if (SymFromAddr(process, program_counter, 0, &sym.si)) { + out += string(sym.si.Name) + "()"; + } else { + out += "Unknown function"; + } + } - void - write_file_and_line (string & out, HANDLE process, - DWORD64 program_counter) - { - IMAGEHLP_LINE64 - ih_line = { - sizeof (IMAGEHLP_LINE64) - }; - DWORD - dummy = 0; - if (SymGetLineFromAddr64 (process, program_counter, &dummy, &ih_line)) - { - out += - string ("|") + string (ih_line.FileName) + ":" + - intToStr (ih_line.LineNumber); - } - } - void - generate_stack_trace (string & out, CONTEXT ctx, int skip) - { - STACKFRAME64 - sf = { - }; + void + write_file_and_line(string & out, HANDLE process, + DWORD64 program_counter) { + IMAGEHLP_LINE64 + ih_line = { + sizeof(IMAGEHLP_LINE64) + }; + DWORD + dummy = 0; + if (SymGetLineFromAddr64(process, program_counter, &dummy, &ih_line)) { + out += + string("|") + string(ih_line.FileName) + ":" + + intToStr(ih_line.LineNumber); + } + } + void + generate_stack_trace(string & out, CONTEXT ctx, int skip) { + STACKFRAME64 + sf = { + }; # if !defined(_WIN64) - sf.AddrPC.Offset = ctx.Eip; + sf.AddrPC.Offset = ctx.Eip; # else - sf.AddrPC.Offset = ctx.Rip; + sf.AddrPC.Offset = ctx.Rip; # endif - sf.AddrPC.Mode = AddrModeFlat; + sf.AddrPC.Mode = AddrModeFlat; # if !defined(_WIN64) - sf.AddrStack.Offset = ctx.Esp; + sf.AddrStack.Offset = ctx.Esp; # else - sf.AddrStack.Offset = ctx.Rsp; + sf.AddrStack.Offset = ctx.Rsp; # endif - sf.AddrStack.Mode = AddrModeFlat; + sf.AddrStack.Mode = AddrModeFlat; # if !defined(_WIN64) - sf.AddrFrame.Offset = ctx.Ebp; + sf.AddrFrame.Offset = ctx.Ebp; # else - sf.AddrFrame.Offset = ctx.Rbp; + sf.AddrFrame.Offset = ctx.Rbp; # endif - sf.AddrFrame.Mode = AddrModeFlat; + sf.AddrFrame.Mode = AddrModeFlat; - HANDLE - process = GetCurrentProcess (); - HANDLE - thread = GetCurrentThread (); + HANDLE + process = GetCurrentProcess(); + HANDLE + thread = GetCurrentThread(); - bool - tryThreadContext = true; - CONTEXT - threadContext; - memset (&threadContext, 0, sizeof (CONTEXT)); - threadContext.ContextFlags = CONTEXT_FULL; + bool + tryThreadContext = true; + CONTEXT + threadContext; + memset(&threadContext, 0, sizeof(CONTEXT)); + threadContext.ContextFlags = CONTEXT_FULL; - for (;;) - { - SetLastError (0); + for (;;) { + SetLastError(0); # if !defined(_WIN64) - BOOL - stack_walk_ok = StackWalk64 (IMAGE_FILE_MACHINE_I386, + BOOL + stack_walk_ok = StackWalk64(IMAGE_FILE_MACHINE_I386, # else - BOOL - stack_walk_ok = StackWalk64 (IMAGE_FILE_MACHINE_AMD64, + BOOL + stack_walk_ok = StackWalk64(IMAGE_FILE_MACHINE_AMD64, # endif - process, thread, &sf, - (tryThreadContext == - false ? &threadContext : &ctx), - 0, &SymFunctionTableAccess64, - &SymGetModuleBase64, 0); - if (!stack_walk_ok || !sf.AddrFrame.Offset) - { - if (tryThreadContext == true) - { - tryThreadContext = false; - if (GetThreadContext (thread, &threadContext) != 0) - { + process, thread, &sf, + (tryThreadContext == + false ? &threadContext : &ctx), + 0, &SymFunctionTableAccess64, + &SymGetModuleBase64, 0); + if (!stack_walk_ok || !sf.AddrFrame.Offset) { + if (tryThreadContext == true) { + tryThreadContext = false; + if (GetThreadContext(thread, &threadContext) != 0) { # if !defined(_WIN64) - sf.AddrPC.Offset = threadContext.Eip; + sf.AddrPC.Offset = threadContext.Eip; # else - sf.AddrPC.Offset = threadContext.Rip; + sf.AddrPC.Offset = threadContext.Rip; # endif - sf.AddrPC.Mode = AddrModeFlat; + sf.AddrPC.Mode = AddrModeFlat; # if !defined(_WIN64) - sf.AddrStack.Offset = threadContext.Esp; + sf.AddrStack.Offset = threadContext.Esp; # else - sf.AddrStack.Offset = threadContext.Rsp; + sf.AddrStack.Offset = threadContext.Rsp; # endif - sf.AddrStack.Mode = AddrModeFlat; + sf.AddrStack.Mode = AddrModeFlat; # if !defined(_WIN64) - sf.AddrFrame.Offset = threadContext.Ebp; + sf.AddrFrame.Offset = threadContext.Ebp; # else - sf.AddrFrame.Offset = threadContext.Rbp; + sf.AddrFrame.Offset = threadContext.Rbp; # endif - sf.AddrFrame.Mode = AddrModeFlat; - } - else - { - return; - } - } - else - { - return; - } - } + sf.AddrFrame.Mode = AddrModeFlat; + } else { + return; + } + } else { + return; + } + } - if (skip) - { - --skip; - } - else - { -// write the address - out += intToStr (sf.AddrPC.Offset) + "|"; + if (skip) { + --skip; + } else { + // write the address + out += intToStr(sf.AddrPC.Offset) + "|"; - write_module_name (out, process, sf.AddrPC.Offset); - write_function_name (out, process, sf.AddrPC.Offset); - write_file_and_line (out, process, sf.AddrPC.Offset); + write_module_name(out, process, sf.AddrPC.Offset); + write_function_name(out, process, sf.AddrPC.Offset); + write_file_and_line(out, process, sf.AddrPC.Offset); - out += "\n"; - } - } - } + out += "\n"; + } + } + } - struct UntypedException - { - explicit - UntypedException (const EXCEPTION_RECORD & er): - exception_object (reinterpret_cast < - void *>(er.ExceptionInformation[1])), - type_array (reinterpret_cast < - _ThrowInfo * - >(er.ExceptionInformation[2])->pCatchableTypeArray) - { - } - void * - exception_object; - _CatchableTypeArray * - type_array; - }; + struct UntypedException { + explicit + UntypedException(const EXCEPTION_RECORD & er) : + exception_object(reinterpret_cast < + void *>(er.ExceptionInformation[1])), + type_array(reinterpret_cast < + _ThrowInfo * + >(er.ExceptionInformation[2])->pCatchableTypeArray) { + } + void * + exception_object; + _CatchableTypeArray * + type_array; + }; - void * - exception_cast_worker (const UntypedException & e, const type_info & ti) - { - for (int i = 0; i < e.type_array->nCatchableTypes; ++i) - { - _CatchableType & type_i = *e.type_array->arrayOfCatchableTypes[i]; - const - std::type_info & - ti_i = *reinterpret_cast < std::type_info * >(type_i.pType); - if (ti_i == ti) - { - char * - base_address = reinterpret_cast < char *>(e.exception_object); - base_address += type_i.thisDisplacement.mdisp; - return base_address; - } - } - return 0; - } + void * + exception_cast_worker(const UntypedException & e, const type_info & ti) { + for (int i = 0; i < e.type_array->nCatchableTypes; ++i) { + _CatchableType & type_i = *e.type_array->arrayOfCatchableTypes[i]; + const + std::type_info & + ti_i = *reinterpret_cast (type_i.pType); + if (ti_i == ti) { + char * + base_address = reinterpret_cast (e.exception_object); + base_address += type_i.thisDisplacement.mdisp; + return base_address; + } + } + return 0; + } - template < typename T > T * exception_cast (const UntypedException & e) - { - const - std::type_info & - ti = typeid (T); - return reinterpret_cast < T * >(exception_cast_worker (e, ti)); - } - void - stackdumper (unsigned int type, EXCEPTION_POINTERS * ep, bool fatalExit) - { + template < typename T > T * exception_cast(const UntypedException & e) { + const + std::type_info & + ti = typeid (T); + return reinterpret_cast (exception_cast_worker(e, ti)); + } + void + stackdumper(unsigned int type, EXCEPTION_POINTERS * ep, bool fatalExit) { # ifdef HAVE_GOOGLE_BREAKPAD - if (errorHandlerPtr.get () != NULL) - { - errorHandlerPtr->WriteMinidump (); - } + if (errorHandlerPtr.get() != NULL) { + errorHandlerPtr->WriteMinidump(); + } # endif - if (!ep) - { - fatal ("unknown type"); - return; - } - EXCEPTION_RECORD * - er = ep->ExceptionRecord; - CONTEXT * - context = ep->ContextRecord; - string - out = ""; - int - skip = 0; + if (!ep) { + fatal("unknown type"); + return; + } + EXCEPTION_RECORD * + er = ep->ExceptionRecord; + CONTEXT * + context = ep->ContextRecord; + string + out = ""; + int + skip = 0; - switch (er->ExceptionCode) - { - case 0xE06D7363: - { // C++ exception - UntypedException - ue (*er); - if (std::exception * e = exception_cast < std::exception > (ue)) - { - const - std::type_info & - ti = typeid (*e); - out += string (ti.name ()) + ":" + string (e->what ()); - } - else - { - out += "Unknown C++ exception thrown."; - } - skip = 2; // skip RaiseException and _CxxThrowException - } - break; - case EXCEPTION_ACCESS_VIOLATION: - { - out += string ("Access violation. Illegal ") - + (er->ExceptionInformation[0] ? "write" : "read") - + string (" by ") - + intToStr ((int) er->ExceptionAddress) - + string (" at ") + intToStr (er->ExceptionInformation[1]); - } break; - default: - { - out += "SEH exception thrown. Exception code: " - + er->ExceptionCode - + string (" at ") + intToStr ((int) er->ExceptionAddress); - } - } + switch (er->ExceptionCode) { + case 0xE06D7363: + { // C++ exception + UntypedException + ue(*er); + if (std::exception * e = exception_cast (ue)) { + const + std::type_info & + ti = typeid (*e); + out += string(ti.name()) + ":" + string(e->what()); + } else { + out += "Unknown C++ exception thrown."; + } + skip = 2; // skip RaiseException and _CxxThrowException + } + break; + case EXCEPTION_ACCESS_VIOLATION: + { + out += string("Access violation. Illegal ") + + (er->ExceptionInformation[0] ? "write" : "read") + + string(" by ") + + intToStr((int) er->ExceptionAddress) + + string(" at ") + intToStr(er->ExceptionInformation[1]); + } break; + default: + { + out += "SEH exception thrown. Exception code: " + + er->ExceptionCode + + string(" at ") + intToStr((int) er->ExceptionAddress); + } + } - generate_stack_trace (out, *context, skip); + generate_stack_trace(out, *context, skip); - if (fatalExit == true) - { - fatal (out.c_str ()); - } - else - { - ExceptionHandler::logError (out.c_str (), true); - } - } + if (fatalExit == true) { + fatal(out.c_str()); + } else { + ExceptionHandler::logError(out.c_str(), true); + } + } #endif -// ===================================================== -// class ExceptionHandler -// ===================================================== + // ===================================================== + // class ExceptionHandler + // ===================================================== #if defined(WIN32) && !defined(__GNUC__) - void - ExceptionHandler::handle (LPEXCEPTION_POINTERS pointers) - { - string - msg = - "#1 An error occurred and " + string (mg_app_name) + - " will close.\nPlease report this bug to: " + string (mailString); - msg += - ", attaching the generated " + getCrashDumpFileName () + " file."; + void + ExceptionHandler::handle(LPEXCEPTION_POINTERS pointers) { + string + msg = + "#1 An error occurred and " + string(mg_app_name) + + " will close.\nPlease report this bug to: " + string(mailString); + msg += + ", attaching the generated " + getCrashDumpFileName() + " file."; - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", - msg.c_str ()); - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - msg.c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", + msg.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + msg.c_str()); - stackdumper (0, pointers, false); + stackdumper(0, pointers, false); - if (mainProgram && gameInitialized == true) - { - mainProgram->showMessage (msg.c_str ()); - } + if (mainProgram && gameInitialized == true) { + mainProgram->showMessage(msg.c_str()); + } - message (msg.c_str (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } + message(msg.c_str(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } #endif - void - ExceptionHandler::handle () - { - string - msg = - "#1 An error occurred and " + string (mg_app_name) + - " will close.\nPlease report this bug to: " + string (mailString); + void + ExceptionHandler::handle() { + string + msg = + "#1 An error occurred and " + string(mg_app_name) + + " will close.\nPlease report this bug to: " + string(mailString); #ifdef WIN32 - msg += - ", attaching the generated " + getCrashDumpFileName () + " file."; + msg += + ", attaching the generated " + getCrashDumpFileName() + " file."; #endif - SystemFlags::OutputDebug (SystemFlags::debugError, "%s\n", - msg.c_str ()); - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - msg.c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugError, "%s\n", + msg.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + msg.c_str()); - if (mainProgram && gameInitialized == true) - { - mainProgram->showMessage (msg.c_str ()); - } + if (mainProgram && gameInitialized == true) { + mainProgram->showMessage(msg.c_str()); + } - message (msg.c_str (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } + message(msg.c_str(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } - void - ExceptionHandler::logError (const char *msg, bool confirmToConsole) - { - string - errorLogFile = "error.log"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - errorLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - errorLogFile; - } - else - { - string - userData = Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - errorLogFile = userData + errorLogFile; - } + void + ExceptionHandler::logError(const char *msg, bool confirmToConsole) { + string + errorLogFile = "error.log"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + errorLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + errorLogFile; + } else { + string + userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + errorLogFile = userData + errorLogFile; + } #if defined(WIN32) && !defined(__MINGW32__) - FILE * - fp = _wfopen (utf8_decode (errorLogFile).c_str (), L"w"); - std::ofstream logFile (fp); + FILE * + fp = _wfopen(utf8_decode(errorLogFile).c_str(), L"w"); + std::ofstream logFile(fp); #else - std::ofstream logFile; - logFile.open (errorLogFile.c_str (), ios_base::out | ios_base::trunc); + std::ofstream logFile; + logFile.open(errorLogFile.c_str(), ios_base::out | ios_base::trunc); #endif - if (logFile.is_open () == true) - { -//time_t curtime = time (NULL); -//struct tm *loctime = localtime (&curtime); - struct tm - loctime = threadsafe_localtime (systemtime_now ()); - char - szBuf2[100] = ""; - strftime (szBuf2, 100, "%Y-%m-%d %H:%M:%S", &loctime); + if (logFile.is_open() == true) { + //time_t curtime = time (NULL); + //struct tm *loctime = localtime (&curtime); + struct tm + loctime = threadsafe_localtime(systemtime_now()); + char + szBuf2[100] = ""; + strftime(szBuf2, 100, "%Y-%m-%d %H:%M:%S", &loctime); - logFile << "[" << szBuf2 << "] Runtime Error information:" << - std::endl; - logFile << "======================================================" << - std::endl; - logFile << (msg != NULL ? msg : "null") << std::endl; - logFile.close (); + logFile << "[" << szBuf2 << "] Runtime Error information:" << + std::endl; + logFile << "======================================================" << + std::endl; + logFile << (msg != NULL ? msg : "null") << std::endl; + logFile.close(); #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - { - fclose (fp); - } + if (fp) { + fclose(fp); + } #endif - if (confirmToConsole == true) - { - printf ("Error saved to logfile [%s]\n", errorLogFile.c_str ()); - fflush (stdout); - } - } - else - { - if (confirmToConsole == true) - { - printf ("COULD NOT SAVE TO ERROR logfile [%s]\n", - errorLogFile.c_str ()); - fflush (stdout); - } - } - } + if (confirmToConsole == true) { + printf("Error saved to logfile [%s]\n", errorLogFile.c_str()); + fflush(stdout); + } + } else { + if (confirmToConsole == true) { + printf("COULD NOT SAVE TO ERROR logfile [%s]\n", + errorLogFile.c_str()); + fflush(stdout); + } + } + } - void - ExceptionHandler::handleRuntimeError (const megaglest_runtime_error & ex) - { - const char * - msg = ex.what (); - handleRuntimeError (msg, false); - } + void + ExceptionHandler::handleRuntimeError(const megaglest_runtime_error & ex) { + const char * + msg = ex.what(); + handleRuntimeError(msg, false); + } - void - ExceptionHandler::handleRuntimeError (const char *msg, - bool getStackTraceString) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + void + ExceptionHandler::handleRuntimeError(const char *msg, + bool getStackTraceString) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - static - bool - inErrorNow = false; - if (inErrorNow == true) - { - printf ("\n=====================================\n"); - printf ("\n** Already in error handler aborting, msg [%s]\n", msg); - fflush (stdout); - abort (); -//return; - } - inErrorNow = true; + static + bool + inErrorNow = false; + if (inErrorNow == true) { + printf("\n=====================================\n"); + printf("\n** Already in error handler aborting, msg [%s]\n", msg); + fflush(stdout); + abort(); + //return; + } + inErrorNow = true; - logError (msg, true); + logError(msg, true); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] program = %p gameInitialized = %d msg [%s]\n", - __FILE__, __FUNCTION__, __LINE__, mainProgram, gameInitialized, - msg); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] [%s] gameInitialized = %d, program = %p\n", - __FILE__, __FUNCTION__, __LINE__, msg, - gameInitialized, mainProgram); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%s] gameInitialized = %d, program = %p\n", - __FILE__, __FUNCTION__, __LINE__, msg, - gameInitialized, mainProgram); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] program = %p gameInitialized = %d msg [%s]\n", + __FILE__, __FUNCTION__, __LINE__, mainProgram, gameInitialized, + msg); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] [%s] gameInitialized = %d, program = %p\n", + __FILE__, __FUNCTION__, __LINE__, msg, + gameInitialized, mainProgram); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%s] gameInitialized = %d, program = %p\n", + __FILE__, __FUNCTION__, __LINE__, msg, + gameInitialized, mainProgram); - string - errMsg = (msg != NULL ? msg : "null"); + string + errMsg = (msg != NULL ? msg : "null"); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - bool - gotStackTrace = false; - if (getStackTraceString == true && disableBacktrace == false - && sdl_quitCalled == false) - { - string - stackTrace = getStackTrace (); - errMsg += stackTrace; - gotStackTrace = true; - } + bool + gotStackTrace = false; + if (getStackTraceString == true && disableBacktrace == false + && sdl_quitCalled == false) { + string + stackTrace = getStackTrace(); + errMsg += stackTrace; + gotStackTrace = true; + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - logError (errMsg.c_str (), false); + logError(errMsg.c_str(), false); - if (gotStackTrace == true) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] [%s]\n", __FILE__, - __FUNCTION__, __LINE__, errMsg.c_str ()); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%s]\n", __FILE__, - __FUNCTION__, __LINE__, errMsg.c_str ()); + if (gotStackTrace == true) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] [%s]\n", __FILE__, + __FUNCTION__, __LINE__, errMsg.c_str()); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%s]\n", __FILE__, + __FUNCTION__, __LINE__, errMsg.c_str()); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); -//abort(); + //abort(); - if (mainProgram && gameInitialized == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (mainProgram && gameInitialized == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (mainProgram->getState () != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - mainProgram->showMessage (errMsg.c_str ()); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); + if (mainProgram->getState() != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + mainProgram->showMessage(errMsg.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); - if (glActiveTexture != NULL) - { - for (; - GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - && mainProgram->isMessageShowing ();) - { - ::Shared::Platform::Window::handleEvent (); - try - { - mainProgram->loop (); - } - catch (const exception & e) - { - printf ("\n=====================================\n"); - printf - ("\n** Already in error handler exiting error rendering, msg [%s]\n", - e.what ()); - fflush (stdout); - break; - } - } - } - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - mainProgram->showMessage (errMsg.c_str ()); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); + if (glActiveTexture != NULL) { + for (; + GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && mainProgram->isMessageShowing();) { + ::Shared::Platform::Window::handleEvent(); + try { + mainProgram->loop(); + } catch (const exception & e) { + printf("\n=====================================\n"); + printf + ("\n** Already in error handler exiting error rendering, msg [%s]\n", + e.what()); + fflush(stdout); + break; + } + } + } + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + mainProgram->showMessage(errMsg.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); - if (glActiveTexture != NULL) - { - for (; - GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - && mainProgram->isMessageShowing ();) - { - ::Shared::Platform::Window::handleEvent (); - try - { - mainProgram->loop (); - } - catch (const exception & e) - { - printf ("\n=====================================\n"); - printf - ("\n** Already in error handler exiting error rendering, msg [%s]\n", - e.what ()); - fflush (stdout); - break; - } - } - } - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (glActiveTexture != NULL) { + for (; + GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && mainProgram->isMessageShowing();) { + ::Shared::Platform::Window::handleEvent(); + try { + mainProgram->loop(); + } catch (const exception & e) { + printf("\n=====================================\n"); + printf + ("\n** Already in error handler exiting error rendering, msg [%s]\n", + e.what()); + fflush(stdout); + break; + } + } + } + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - string - err = "#2 An error occurred and "; - if (sdl_quitCalled == false) - { - err += mg_app_name; - } - err += - " will close.\nError msg = [" + errMsg + - "]\n\nPlease report this bug to "; - if (sdl_quitCalled == false) - { - err += mailStringSupport; - } + string + err = "#2 An error occurred and "; + if (sdl_quitCalled == false) { + err += mg_app_name; + } + err += + " will close.\nError msg = [" + errMsg + + "]\n\nPlease report this bug to "; + if (sdl_quitCalled == false) { + err += mailStringSupport; + } #ifdef WIN32 - err += - string (", attaching the generated ") + getCrashDumpFileName () + - string (" file."); + err += + string(", attaching the generated ") + getCrashDumpFileName() + + string(" file."); #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - message (err, GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } + message(err, GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); -// Now try to shutdown threads if possible - delete - mainProgram; - mainProgram = NULL; -// END + // Now try to shutdown threads if possible + delete + mainProgram; + mainProgram = NULL; + // END - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); #ifdef WIN32 - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - showCursor (true); - restoreVideoMode (::Shared::Platform::Window::getSDLWindow (), true); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + showCursor(true); + restoreVideoMode(::Shared::Platform::Window::getSDLWindow(), true); + } - runtimeErrorMsg = errMsg; - inErrorNow = false; - throw - runtimeErrorMsg; + runtimeErrorMsg = errMsg; + inErrorNow = false; + throw + runtimeErrorMsg; #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - cleanupProcessObjects (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (sdl_quitCalled == false) - { - sdl_quitCalled = true; - SDL_Quit (); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - inErrorNow = false; - - abort (); - } - - int - ExceptionHandler::DisplayMessage (const char *msg, bool exitApp) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] msg [%s] exitApp = %d\n", - __FILE__, __FUNCTION__, __LINE__, msg, - exitApp); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - - if (mainProgram && gameInitialized == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - mainProgram->showMessage (msg); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - message (msg, GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - - if (exitApp == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", msg); - -// Now try to shutdown threads if possible - delete - mainProgram; - mainProgram = NULL; -// END - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - cleanupProcessObjects (); - - if (sdl_quitCalled == false) - { - sdl_quitCalled = true; - SDL_Quit (); - } - exit (-1); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, - __FUNCTION__, __LINE__, msg, exitApp); - return 0; - } - -// ===================================================== -// class MainWindow -// ===================================================== - - MainWindow::MainWindow (Program * program):WindowGl (), popupMenu ("MainWindow", - "popupMenu") - { - this->program = program; -//this->popupMenu.registerGraphicComponentOnlyFontCallbacks("MainWindow", "popupMenu"); - this->popupMenu.setEnabled (false); - this->popupMenu.setVisible (false); - this->triggerLanguageToggle = false; - this->triggerLanguage = ""; - this->cancelLanguageSelection = -1; - } - - MainWindow::~MainWindow () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - delete - program; - program = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - int - MainWindow::getDesiredScreenWidth () - { - Config & config = Config::getInstance (); - return config.getInt ("ScreenWidth"); - } - int - MainWindow::getDesiredScreenHeight () - { - Config & config = Config::getInstance (); - return config.getInt ("ScreenHeight"); - } - - void - MainWindow::eventToggleFullScreen (bool isFullscreen) - { - WindowGl::eventToggleFullScreen (isFullscreen); - - if (isFullscreen) - { - Metrics::reload (this->program->getWindow ()->getScreenWidth (), - this->program->getWindow ()->getScreenHeight ()); - } - else - { - Config & config = Config::getInstance (); - Metrics::reload (config.getInt ("ScreenWidth"), - config.getInt ("ScreenHeight")); -//window->setText(config.getString("WindowTitle","ZetaGlest")); -//this->mainMenu->init(); - } - - } - - void - MainWindow::eventMouseDown (int x, int y, MouseButton mouseButton) - { - const - Metrics & - metrics = Metrics::getInstance (); - int - vx = metrics.toVirtualX (x); - int - vy = metrics.toVirtualY (getH () - y); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventMouseDown] ERROR, program == NULL!"); - } - - if (popupMenu.getVisible () == true && popupMenu.mouseClick (vx, vy)) - { - std::pair < int, - string > - result = popupMenu.mouseClickedMenuItem (vx, vy); - - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - -// Exit game - if (result.first != cancelLanguageSelection) - { - Lang & lang = Lang::getInstance (); - map < string, string > languageList = - lang.getDiscoveredLanguageList (true); - for (map < string, string >::iterator iterMap = - languageList.begin (); iterMap != languageList.end (); - ++iterMap) - { - string - matchLanguage = iterMap->first + "-" + iterMap->second; - if (matchLanguage == result.second) - { - this->triggerLanguageToggle = true; - this->triggerLanguage = iterMap->first; - break; - } - } - - } - - return; - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - switch (mouseButton) - { - case mbLeft: - program->mouseDownLeft (vx, vy); - break; - case mbRight: -//program->mouseDownRight(vx, vy); - break; - case mbCenter: -//program->mouseDownCenter(vx, vy); - break; - default: - break; - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - ProgramState * - programState = program->getState (); - if (programState != NULL) - { - switch (mouseButton) - { - case mbLeft: - programState->mouseDownLeft (vx, vy); - break; - case mbRight: - programState->mouseDownRight (vx, vy); - break; - case mbCenter: - programState->mouseDownCenter (vx, vy); - break; - default: - break; - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - MainWindow::eventMouseUp (int x, int y, MouseButton mouseButton) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - const - Metrics & - metrics = Metrics::getInstance (); - int - vx = metrics.toVirtualX (x); - int - vy = metrics.toVirtualY (getH () - y); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventMouseUp] ERROR, program == NULL!"); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - ProgramState * - programState = program->getState (); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (programState != NULL) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - switch (mouseButton) - { - case mbLeft: - programState->mouseUpLeft (vx, vy); - break; - case mbRight: - programState->mouseUpRight (vx, vy); - break; - case mbCenter: - programState->mouseUpCenter (vx, vy); - break; - default: - break; - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - MainWindow::eventMouseDoubleClick (int x, int y, MouseButton mouseButton) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - const - Metrics & - metrics = Metrics::getInstance (); - int - vx = metrics.toVirtualX (x); - int - vy = metrics.toVirtualY (getH () - y); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventMouseDoubleClick] ERROR, program == NULL!"); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - ProgramState * - programState = program->getState (); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (programState != NULL) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - switch (mouseButton) - { - case mbLeft: - programState->mouseDoubleClickLeft (vx, vy); - break; - case mbRight: - programState->mouseDoubleClickRight (vx, vy); - break; - case mbCenter: - programState->mouseDoubleClickCenter (vx, vy); - break; - default: - break; - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - MainWindow::eventMouseMove (int x, int y, const MouseState * ms) - { - - const - Metrics & - metrics = Metrics::getInstance (); - int - vx = metrics.toVirtualX (x); - int - vy = metrics.toVirtualY (getH () - y); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventMouseMove] ERROR, program == NULL!"); - } - - program->eventMouseMove (vx, vy, ms); - - ProgramState * - programState = program->getState (); - if (programState != NULL) - { - programState->mouseMove (vx, vy, ms); - } - } - - void - MainWindow::eventMouseWheel (int x, int y, int zDelta) - { - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - const - Metrics & - metrics = Metrics::getInstance (); - int - vx = metrics.toVirtualX (x); - int - vy = metrics.toVirtualY (getH () - y); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventMouseMove] ERROR, program == NULL!"); - } - - ProgramState * - programState = program->getState (); - - if (programState != NULL) - { - programState->eventMouseWheel (vx, vy, zDelta); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void - MainWindow::render () - { - if (popupMenu.getVisible () == true) - { - Renderer & renderer = Renderer::getInstance (); - renderer.renderPopupMenu (&popupMenu); - } - } - - void - MainWindow::showLanguages () - { - Lang & lang = Lang::getInstance (); - std::vector < string > menuItems; - map < string, string > languageList = - lang.getDiscoveredLanguageList (true); - for (map < string, string >::iterator iterMap = languageList.begin (); - iterMap != languageList.end (); ++iterMap) - { - menuItems.push_back (iterMap->first + "-" + iterMap->second); - } - - menuItems.push_back (lang.getString ("Exit")); - cancelLanguageSelection = (int) menuItems.size () - 1; - - popupMenu.setW (100); - popupMenu.setH (100); - popupMenu.init (lang.getString ("GameMenuTitle"), menuItems); - popupMenu.setEnabled (true); - popupMenu.setVisible (true); - } - - void - MainWindow::toggleLanguage (string language) - { - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - this->triggerLanguageToggle = false; - this->triggerLanguage = ""; - - Lang & lang = Lang::getInstance (); - string - currentLanguage = lang.getLanguage (); - - string - newLanguageSelected = language; - if (language == "") - { - newLanguageSelected = currentLanguage; - - vector < string > langResults; - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - string - userDataPath = getGameCustomCoreDataPath (data_path, ""); - findAll (userDataPath + "data/lang/*.lng", langResults, true, false); - - vector < string > langResults2; - findAll (data_path + "data/lang/*.lng", langResults2, true); - if (langResults2.empty () && langResults.empty ()) - { - throw - megaglest_runtime_error ("There are no lang files"); - } - for (unsigned int i = 0; i < langResults2.size (); ++i) - { - string - testLanguage = langResults2[i]; - if (std::find (langResults.begin (), langResults.end (), - testLanguage) == langResults.end ()) - { - langResults.push_back (testLanguage); - } - } - - for (unsigned int i = 0; i < langResults.size (); ++i) - { - string - testLanguage = langResults[i]; - if (testLanguage == currentLanguage) - { - if (i + 1 < langResults.size ()) - { - newLanguageSelected = langResults[i + 1]; - } - else - { - newLanguageSelected = langResults[0]; - } - break; - } - } - } - if (newLanguageSelected != currentLanguage) - { - lang.loadGameStrings (newLanguageSelected); - program->reloadUI (); - program->consoleAddLine (lang.getString ("Language") + " " + - newLanguageSelected); - } - } - - bool - MainWindow::eventTextInput (std::string text) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%s]\n", __FILE__, - __FUNCTION__, __LINE__, text.c_str ()); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); - } - - bool - result = program->textInput (text); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] result = %d\n", - __FILE__, __FUNCTION__, __LINE__, result); - - return result; - } - - bool - MainWindow::eventSdlKeyDown (SDL_KeyboardEvent key) - { - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); - } - return program->sdlKeyDown (key); - } - - void - MainWindow::eventKeyDown (SDL_KeyboardEvent key) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, key.keysym.sym); - -//printf("In mainwindow checking keypress for key [%d]\n",key.keysym.sym); - - SDL_keysym - keystate = key.keysym; - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c][%d]\n", - __FILE__, __FUNCTION__, __LINE__, key, key); - - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); - } - - if (popupMenu.getVisible () == true - && isKeyPressed (SDLK_ESCAPE, key) == true) - { - this->popupMenu.setEnabled (false); - this->popupMenu.setVisible (false); - return; - } - - program->keyDown (key); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (keystate.mod & (KMOD_LALT | KMOD_RALT)) - { -//if(key == vkReturn) { - if (isKeyPressed (SDLK_RETURN, key) == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ALT-ENTER pressed\n", - __FILE__, __FUNCTION__, __LINE__); - -// This stupidity only required in win32. -// We reload the textures so that the canvas paints textures properly + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + cleanupProcessObjects(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (sdl_quitCalled == false) { + sdl_quitCalled = true; + SDL_Quit(); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + inErrorNow = false; + + abort(); + } + + int + ExceptionHandler::DisplayMessage(const char *msg, bool exitApp) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] msg [%s] exitApp = %d\n", + __FILE__, __FUNCTION__, __LINE__, msg, + exitApp); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + + if (mainProgram && gameInitialized == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + mainProgram->showMessage(msg); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + message(msg, GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + + if (exitApp == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", msg); + + // Now try to shutdown threads if possible + delete + mainProgram; + mainProgram = NULL; + // END + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + cleanupProcessObjects(); + + if (sdl_quitCalled == false) { + sdl_quitCalled = true; + SDL_Quit(); + } + exit(-1); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] msg [%s] exitApp = %d\n", __FILE__, + __FUNCTION__, __LINE__, msg, exitApp); + return 0; + } + + // ===================================================== + // class MainWindow + // ===================================================== + + MainWindow::MainWindow(Program * program) :WindowGl(), popupMenu("MainWindow", + "popupMenu") { + this->program = program; + //this->popupMenu.registerGraphicComponentOnlyFontCallbacks("MainWindow", "popupMenu"); + this->popupMenu.setEnabled(false); + this->popupMenu.setVisible(false); + this->triggerLanguageToggle = false; + this->triggerLanguage = ""; + this->cancelLanguageSelection = -1; + } + + MainWindow::~MainWindow() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + delete + program; + program = NULL; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + int + MainWindow::getDesiredScreenWidth() { + Config & config = Config::getInstance(); + return config.getInt("ScreenWidth"); + } + int + MainWindow::getDesiredScreenHeight() { + Config & config = Config::getInstance(); + return config.getInt("ScreenHeight"); + } + + void + MainWindow::eventToggleFullScreen(bool isFullscreen) { + WindowGl::eventToggleFullScreen(isFullscreen); + + if (isFullscreen) { + Metrics::reload(this->program->getWindow()->getScreenWidth(), + this->program->getWindow()->getScreenHeight()); + } else { + Config & config = Config::getInstance(); + Metrics::reload(config.getInt("ScreenWidth"), + config.getInt("ScreenHeight")); + //window->setText(config.getString("WindowTitle","ZetaGlest")); + //this->mainMenu->init(); + } + + } + + void + MainWindow::eventMouseDown(int x, int y, MouseButton mouseButton) { + const + Metrics & + metrics = Metrics::getInstance(); + int + vx = metrics.toVirtualX(x); + int + vy = metrics.toVirtualY(getH() - y); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventMouseDown] ERROR, program == NULL!"); + } + + if (popupMenu.getVisible() == true && popupMenu.mouseClick(vx, vy)) { + std::pair < int, + string > + result = popupMenu.mouseClickedMenuItem(vx, vy); + + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + // Exit game + if (result.first != cancelLanguageSelection) { + Lang & lang = Lang::getInstance(); + map < string, string > languageList = + lang.getDiscoveredLanguageList(true); + for (map < string, string >::iterator iterMap = + languageList.begin(); iterMap != languageList.end(); + ++iterMap) { + string + matchLanguage = iterMap->first + "-" + iterMap->second; + if (matchLanguage == result.second) { + this->triggerLanguageToggle = true; + this->triggerLanguage = iterMap->first; + break; + } + } + + } + + return; + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + switch (mouseButton) { + case mbLeft: + program->mouseDownLeft(vx, vy); + break; + case mbRight: + //program->mouseDownRight(vx, vy); + break; + case mbCenter: + //program->mouseDownCenter(vx, vy); + break; + default: + break; + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + ProgramState * + programState = program->getState(); + if (programState != NULL) { + switch (mouseButton) { + case mbLeft: + programState->mouseDownLeft(vx, vy); + break; + case mbRight: + programState->mouseDownRight(vx, vy); + break; + case mbCenter: + programState->mouseDownCenter(vx, vy); + break; + default: + break; + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + MainWindow::eventMouseUp(int x, int y, MouseButton mouseButton) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + const + Metrics & + metrics = Metrics::getInstance(); + int + vx = metrics.toVirtualX(x); + int + vy = metrics.toVirtualY(getH() - y); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventMouseUp] ERROR, program == NULL!"); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + ProgramState * + programState = program->getState(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (programState != NULL) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + switch (mouseButton) { + case mbLeft: + programState->mouseUpLeft(vx, vy); + break; + case mbRight: + programState->mouseUpRight(vx, vy); + break; + case mbCenter: + programState->mouseUpCenter(vx, vy); + break; + default: + break; + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + MainWindow::eventMouseDoubleClick(int x, int y, MouseButton mouseButton) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + const + Metrics & + metrics = Metrics::getInstance(); + int + vx = metrics.toVirtualX(x); + int + vy = metrics.toVirtualY(getH() - y); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventMouseDoubleClick] ERROR, program == NULL!"); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + ProgramState * + programState = program->getState(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (programState != NULL) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + switch (mouseButton) { + case mbLeft: + programState->mouseDoubleClickLeft(vx, vy); + break; + case mbRight: + programState->mouseDoubleClickRight(vx, vy); + break; + case mbCenter: + programState->mouseDoubleClickCenter(vx, vy); + break; + default: + break; + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + MainWindow::eventMouseMove(int x, int y, const MouseState * ms) { + + const + Metrics & + metrics = Metrics::getInstance(); + int + vx = metrics.toVirtualX(x); + int + vy = metrics.toVirtualY(getH() - y); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventMouseMove] ERROR, program == NULL!"); + } + + program->eventMouseMove(vx, vy, ms); + + ProgramState * + programState = program->getState(); + if (programState != NULL) { + programState->mouseMove(vx, vy, ms); + } + } + + void + MainWindow::eventMouseWheel(int x, int y, int zDelta) { + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + const + Metrics & + metrics = Metrics::getInstance(); + int + vx = metrics.toVirtualX(x); + int + vy = metrics.toVirtualY(getH() - y); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventMouseMove] ERROR, program == NULL!"); + } + + ProgramState * + programState = program->getState(); + + if (programState != NULL) { + programState->eventMouseWheel(vx, vy, zDelta); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void + MainWindow::render() { + if (popupMenu.getVisible() == true) { + Renderer & renderer = Renderer::getInstance(); + renderer.renderPopupMenu(&popupMenu); + } + } + + void + MainWindow::showLanguages() { + Lang & lang = Lang::getInstance(); + std::vector < string > menuItems; + map < string, string > languageList = + lang.getDiscoveredLanguageList(true); + for (map < string, string >::iterator iterMap = languageList.begin(); + iterMap != languageList.end(); ++iterMap) { + menuItems.push_back(iterMap->first + "-" + iterMap->second); + } + + menuItems.push_back(lang.getString("Exit")); + cancelLanguageSelection = (int) menuItems.size() - 1; + + popupMenu.setW(100); + popupMenu.setH(100); + popupMenu.init(lang.getString("GameMenuTitle"), menuItems); + popupMenu.setEnabled(true); + popupMenu.setVisible(true); + } + + void + MainWindow::toggleLanguage(string language) { + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + this->triggerLanguageToggle = false; + this->triggerLanguage = ""; + + Lang & lang = Lang::getInstance(); + string + currentLanguage = lang.getLanguage(); + + string + newLanguageSelected = language; + if (language == "") { + newLanguageSelected = currentLanguage; + + vector < string > langResults; + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + string + userDataPath = getGameCustomCoreDataPath(data_path, ""); + findAll(userDataPath + "data/lang/*.lng", langResults, true, false); + + vector < string > langResults2; + findAll(data_path + "data/lang/*.lng", langResults2, true); + if (langResults2.empty() && langResults.empty()) { + throw + megaglest_runtime_error("There are no lang files"); + } + for (unsigned int i = 0; i < langResults2.size(); ++i) { + string + testLanguage = langResults2[i]; + if (std::find(langResults.begin(), langResults.end(), + testLanguage) == langResults.end()) { + langResults.push_back(testLanguage); + } + } + + for (unsigned int i = 0; i < langResults.size(); ++i) { + string + testLanguage = langResults[i]; + if (testLanguage == currentLanguage) { + if (i + 1 < langResults.size()) { + newLanguageSelected = langResults[i + 1]; + } else { + newLanguageSelected = langResults[0]; + } + break; + } + } + } + if (newLanguageSelected != currentLanguage) { + lang.loadGameStrings(newLanguageSelected); + program->reloadUI(); + program->consoleAddLine(lang.getString("Language") + " " + + newLanguageSelected); + } + } + + bool + MainWindow::eventTextInput(std::string text) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%s]\n", __FILE__, + __FUNCTION__, __LINE__, text.c_str()); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); + } + + bool + result = program->textInput(text); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] result = %d\n", + __FILE__, __FUNCTION__, __LINE__, result); + + return result; + } + + bool + MainWindow::eventSdlKeyDown(SDL_KeyboardEvent key) { + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); + } + return program->sdlKeyDown(key); + } + + void + MainWindow::eventKeyDown(SDL_KeyboardEvent key) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, key.keysym.sym); + + //printf("In mainwindow checking keypress for key [%d]\n",key.keysym.sym); + + SDL_keysym + keystate = key.keysym; + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c][%d]\n", + __FILE__, __FUNCTION__, __LINE__, key, key); + + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyDown] ERROR, program == NULL!"); + } + + if (popupMenu.getVisible() == true + && isKeyPressed(SDLK_ESCAPE, key) == true) { + this->popupMenu.setEnabled(false); + this->popupMenu.setVisible(false); + return; + } + + program->keyDown(key); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (keystate.mod & (KMOD_LALT | KMOD_RALT)) { + //if(key == vkReturn) { + if (isKeyPressed(SDLK_RETURN, key) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ALT-ENTER pressed\n", + __FILE__, __FUNCTION__, __LINE__); + + // This stupidity only required in win32. + // We reload the textures so that the canvas paints textures properly #ifdef WIN32 - if (Window::getAllowAltEnterFullscreenToggle () == true) - { - Renderer & renderer = Renderer::getInstance (); - renderer.reinitAll (); - } + if (Window::getAllowAltEnterFullscreenToggle() == true) { + Renderer & renderer = Renderer::getInstance(); + renderer.reinitAll(); + } #endif - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + } -//printf("In mainwindow checking keypress for key [%d] mod [%d] modvalue: %d\n",key.keysym.sym,keystate.mod,(keystate.mod & (KMOD_LCTRL | KMOD_RCTRL))); + //printf("In mainwindow checking keypress for key [%d] mod [%d] modvalue: %d\n",key.keysym.sym,keystate.mod,(keystate.mod & (KMOD_LCTRL | KMOD_RCTRL))); - if (program != NULL && program->isInSpecialKeyCaptureEvent () == false) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (program != NULL && program->isInSpecialKeyCaptureEvent() == false) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - vector < int > - modifiersToCheck; - modifiersToCheck.push_back (KMOD_LCTRL); - modifiersToCheck.push_back (KMOD_RCTRL); - modifiersToCheck.push_back (KMOD_LALT); - modifiersToCheck.push_back (KMOD_RALT); - modifiersToCheck.push_back (KMOD_LSHIFT); - modifiersToCheck.push_back (KMOD_RSHIFT); + vector < int > + modifiersToCheck; + modifiersToCheck.push_back(KMOD_LCTRL); + modifiersToCheck.push_back(KMOD_RCTRL); + modifiersToCheck.push_back(KMOD_LALT); + modifiersToCheck.push_back(KMOD_RALT); + modifiersToCheck.push_back(KMOD_LSHIFT); + modifiersToCheck.push_back(KMOD_RSHIFT); - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("HotKeyShowDebug"), key) == - true) - { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("HotKeyShowDebug"), key) == + true) { - Renderer & renderer = Renderer::getInstance (); - if (keystate.mod & (KMOD_LALT | KMOD_RALT)) - { - renderer.cycleShowDebugUILevel (); - printf ("**Cycled Debug UI level to: %d\n", - renderer.getShowDebugUILevel ()); - } - else - { - bool - showDebugUI = renderer.getShowDebugUI (); - renderer.setShowDebugUI (!showDebugUI); - } - } - else if ((keystate.mod & (KMOD_LCTRL | KMOD_RCTRL)) && - isKeyPressed (configKeys.getSDLKey ("SwitchLanguage"), - key) == true) - { - if ((keystate.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) - { - this->triggerLanguageToggle = true; - this->triggerLanguage = ""; - } - else - { - showLanguages (); - } - } - else - if (isKeyPressed - (configKeys.getSDLKey ("ReloadINI"), key, - modifiersToCheck) == true) - { - Config & config = Config::getInstance (); - config.reload (); - } - else - if (isKeyPressed - (configKeys.getSDLKey ("Screenshot"), key, - modifiersToCheck) == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot key pressed\n"); + Renderer & renderer = Renderer::getInstance(); + if (keystate.mod & (KMOD_LALT | KMOD_RALT)) { + renderer.cycleShowDebugUILevel(); + printf("**Cycled Debug UI level to: %d\n", + renderer.getShowDebugUILevel()); + } else { + bool + showDebugUI = renderer.getShowDebugUI(); + renderer.setShowDebugUI(!showDebugUI); + } + } else if ((keystate.mod & (KMOD_LCTRL | KMOD_RCTRL)) && + isKeyPressed(configKeys.getSDLKey("SwitchLanguage"), + key) == true) { + if ((keystate.mod & (KMOD_LSHIFT | KMOD_RSHIFT))) { + this->triggerLanguageToggle = true; + this->triggerLanguage = ""; + } else { + showLanguages(); + } + } else + if (isKeyPressed + (configKeys.getSDLKey("ReloadINI"), key, + modifiersToCheck) == true) { + Config & config = Config::getInstance(); + config.reload(); + } else + if (isKeyPressed + (configKeys.getSDLKey("Screenshot"), key, + modifiersToCheck) == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot key pressed\n"); - string - userData = Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } + string + userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } - string - path = userData + GameConstants::folder_path_screenshots; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot checking path [%s]\n", path.c_str ()); + string + path = userData + GameConstants::folder_path_screenshots; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot checking path [%s]\n", path.c_str()); - if (isdir (path.c_str ()) == false) - { - createDirectoryPaths (path); - } + if (isdir(path.c_str()) == false) { + createDirectoryPaths(path); + } - if (isdir (path.c_str ()) == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot path [%s]\n", path.c_str ()); + if (isdir(path.c_str()) == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot path [%s]\n", path.c_str()); - Config & config = Config::getInstance (); - string - fileFormat = config.getString ("ScreenShotFileType", "jpg"); + Config & config = Config::getInstance(); + string + fileFormat = config.getString("ScreenShotFileType", "jpg"); - unsigned int - queueSize = Renderer::getInstance ().getSaveScreenQueueSize (); + unsigned int + queueSize = Renderer::getInstance().getSaveScreenQueueSize(); - for (int i = 0; i < 5000; ++i) - { - path = userData + GameConstants::folder_path_screenshots; - path += - string ("screen") + intToStr (i + queueSize) + string (".") + - fileFormat; + for (int i = 0; i < 5000; ++i) { + path = userData + GameConstants::folder_path_screenshots; + path += + string("screen") + intToStr(i + queueSize) + string(".") + + fileFormat; #ifdef WIN32 - FILE * - f = _wfopen (utf8_decode (path).c_str (), L"rb"); + FILE * + f = _wfopen(utf8_decode(path).c_str(), L"rb"); #else - FILE * - f = fopen (path.c_str (), "rb"); + FILE * + f = fopen(path.c_str(), "rb"); #endif - if (f == NULL) - { - Lang & lang = Lang::getInstance (); - char - szBuf[8096] = ""; - if (lang.getString ("ScreenshotSavedTo").length () > 0 - && lang.getString ("ScreenshotSavedTo")[0] != '?') - { - snprintf (szBuf, 8096, - lang.getString ("ScreenshotSavedTo").c_str (), - path.c_str ()); - } - else - { - snprintf (szBuf, 8096, "Screenshot will be saved to: %s", - path.c_str ()); - } + if (f == NULL) { + Lang & lang = Lang::getInstance(); + char + szBuf[8096] = ""; + if (lang.getString("ScreenshotSavedTo").length() > 0 + && lang.getString("ScreenshotSavedTo")[0] != '?') { + snprintf(szBuf, 8096, + lang.getString("ScreenshotSavedTo").c_str(), + path.c_str()); + } else { + snprintf(szBuf, 8096, "Screenshot will be saved to: %s", + path.c_str()); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, - __LINE__, szBuf); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, + __LINE__, szBuf); - bool - showScreenshotSavedMsg = - Config::getInstance (). - getBool ("DisableScreenshotConsoleText", "false"); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot console showScreenshotSavedMsg = %d\n", - showScreenshotSavedMsg); + bool + showScreenshotSavedMsg = + Config::getInstance(). + getBool("DisableScreenshotConsoleText", "false"); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot console showScreenshotSavedMsg = %d\n", + showScreenshotSavedMsg); - if (showScreenshotSavedMsg == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot console [%s]\n", szBuf); - program->consoleAddLine (szBuf); - } + if (showScreenshotSavedMsg == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot console [%s]\n", szBuf); + program->consoleAddLine(szBuf); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Screenshot save to [%s]\n", path.c_str ()); - Renderer::getInstance ().saveScreen (path); - break; - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("CANNOT save Screenshot [%s]\n", path.c_str ()); - fclose (f); - } - } - } - } - } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Screenshot save to [%s]\n", path.c_str()); + Renderer::getInstance().saveScreen(path); + break; + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("CANNOT save Screenshot [%s]\n", path.c_str()); + fclose(f); + } + } + } + } + } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } - void - MainWindow::eventKeyUp (SDL_KeyboardEvent key) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, key); - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyUp] ERROR, program == NULL!"); - } + void + MainWindow::eventKeyUp(SDL_KeyboardEvent key) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, key); + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyUp] ERROR, program == NULL!"); + } - program->keyUp (key); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, key); - } + program->keyUp(key); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, key); + } - void - MainWindow::eventKeyPress (SDL_KeyboardEvent c) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, c); - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyPress] ERROR, program == NULL!"); - } + void + MainWindow::eventKeyPress(SDL_KeyboardEvent c) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, c); + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyPress] ERROR, program == NULL!"); + } - program->keyPress (c); + program->keyPress(c); - if (program != NULL && program->isInSpecialKeyCaptureEvent () == false) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (program != NULL && program->isInSpecialKeyCaptureEvent() == false) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed - (configKeys.getSDLKey ("HotKeyToggleOSMouseEnabled"), c) == true) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed + (configKeys.getSDLKey("HotKeyToggleOSMouseEnabled"), c) == true) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - bool - showCursorState = false; - int - state = SDL_ShowCursor (SDL_QUERY); - if (state == SDL_DISABLE) - { - showCursorState = true; - } + bool + showCursorState = false; + int + state = SDL_ShowCursor(SDL_QUERY); + if (state == SDL_DISABLE) { + showCursorState = true; + } - showCursor (showCursorState); - Renderer & renderer = Renderer::getInstance (); - renderer.setNo2DMouseRendering (showCursorState); + showCursor(showCursorState); + Renderer & renderer = Renderer::getInstance(); + renderer.setNo2DMouseRendering(showCursorState); - Window::lastShowMouseState = SDL_ShowCursor (SDL_QUERY); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", - __FILE__, __FUNCTION__, __LINE__, - Window::lastShowMouseState); - } - } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, c); - } + Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", + __FILE__, __FUNCTION__, __LINE__, + Window::lastShowMouseState); + } + } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, c); + } - void - MainWindow::eventWindowEvent (SDL_WindowEvent event) - { - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, event.event); - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventKeyPress] ERROR, program == NULL!"); - } + void + MainWindow::eventWindowEvent(SDL_WindowEvent event) { + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, event.event); + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventKeyPress] ERROR, program == NULL!"); + } -// if(program->getState() != NULL && dynamic_cast(program->getState()) != NULL) { -// printf("In eventWindowEvent skip\n"); -// return; -// } -//Renderer &renderer= Renderer::getInstance(); - switch (event.event) - { - case SDL_WINDOWEVENT_ENTER: - { -//printf("In SDL_WINDOWEVENT_ENTER\n"); -// bool showCursorState = Window::lastShowMouseState; -// showCursor(showCursorState); -// renderer.setNo2DMouseRendering(showCursorState); -// -// Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", - __FILE__, __FUNCTION__, __LINE__, - Window::lastShowMouseState); - } - break; - case SDL_WINDOWEVENT_LEAVE: - { -//printf("In SDL_WINDOWEVENT_LEAVE\n"); -// bool showCursorState = false; -// int state = SDL_ShowCursor(SDL_QUERY); -// if(state == SDL_DISABLE) { -// showCursorState = true; -// } -// showCursor(showCursorState); -// renderer.setNo2DMouseRendering(showCursorState); -// -// Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", - __FILE__, __FUNCTION__, __LINE__, - Window::lastShowMouseState); - } - break; - case SDL_WINDOWEVENT_FOCUS_GAINED: - { -//printf("SDL_WINDOWEVENT_FOCUS_GAINED\n"); -// bool showCursorState = Window::lastShowMouseState; -// showCursor(showCursorState); -// renderer.setNo2DMouseRendering(showCursorState); -// -// Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", - __FILE__, __FUNCTION__, __LINE__, - Window::lastShowMouseState); - } - break; - case SDL_WINDOWEVENT_FOCUS_LOST: - { -//printf("SDL_WINDOWEVENT_FOCUS_LOST\n"); -// bool showCursorState = false; -// int state = SDL_ShowCursor(SDL_QUERY); -// if(state == SDL_DISABLE) { -// showCursorState = true; -// } -// showCursor(showCursorState); -// renderer.setNo2DMouseRendering(showCursorState); -// -// Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", - __FILE__, __FUNCTION__, __LINE__, - Window::lastShowMouseState); - } - break; + // if(program->getState() != NULL && dynamic_cast(program->getState()) != NULL) { + // printf("In eventWindowEvent skip\n"); + // return; + // } + //Renderer &renderer= Renderer::getInstance(); + switch (event.event) { + case SDL_WINDOWEVENT_ENTER: + { + //printf("In SDL_WINDOWEVENT_ENTER\n"); + // bool showCursorState = Window::lastShowMouseState; + // showCursor(showCursorState); + // renderer.setNo2DMouseRendering(showCursorState); + // + // Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", + __FILE__, __FUNCTION__, __LINE__, + Window::lastShowMouseState); + } + break; + case SDL_WINDOWEVENT_LEAVE: + { + //printf("In SDL_WINDOWEVENT_LEAVE\n"); + // bool showCursorState = false; + // int state = SDL_ShowCursor(SDL_QUERY); + // if(state == SDL_DISABLE) { + // showCursorState = true; + // } + // showCursor(showCursorState); + // renderer.setNo2DMouseRendering(showCursorState); + // + // Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", + __FILE__, __FUNCTION__, __LINE__, + Window::lastShowMouseState); + } + break; + case SDL_WINDOWEVENT_FOCUS_GAINED: + { + //printf("SDL_WINDOWEVENT_FOCUS_GAINED\n"); + // bool showCursorState = Window::lastShowMouseState; + // showCursor(showCursorState); + // renderer.setNo2DMouseRendering(showCursorState); + // + // Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", + __FILE__, __FUNCTION__, __LINE__, + Window::lastShowMouseState); + } + break; + case SDL_WINDOWEVENT_FOCUS_LOST: + { + //printf("SDL_WINDOWEVENT_FOCUS_LOST\n"); + // bool showCursorState = false; + // int state = SDL_ShowCursor(SDL_QUERY); + // if(state == SDL_DISABLE) { + // showCursorState = true; + // } + // showCursor(showCursorState); + // renderer.setNo2DMouseRendering(showCursorState); + // + // Window::lastShowMouseState = SDL_ShowCursor(SDL_QUERY); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Window::lastShowMouseState = %d\n", + __FILE__, __FUNCTION__, __LINE__, + Window::lastShowMouseState); + } + break; - } + } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] [%d]\n", __FILE__, - __FUNCTION__, __LINE__, event.event); - } + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] [%d]\n", __FILE__, + __FUNCTION__, __LINE__, event.event); + } - void - MainWindow::eventActivate (bool active) - { - if (!active) - { -//minimize(); - } - } + void + MainWindow::eventActivate(bool active) { + if (!active) { + //minimize(); + } + } - void - MainWindow::eventResize (SizeState sizeState) - { - if (program == NULL) - { - throw - megaglest_runtime_error - ("In [MainWindow::eventResize] ERROR, program == NULL!"); - } + void + MainWindow::eventResize(SizeState sizeState) { + if (program == NULL) { + throw + megaglest_runtime_error + ("In [MainWindow::eventResize] ERROR, program == NULL!"); + } - program->resize (sizeState); - } + program->resize(sizeState); + } - void - MainWindow::eventClose () - { - delete - program; - program = NULL; - } + void + MainWindow::eventClose() { + delete + program; + program = NULL; + } - void - MainWindow::setProgram (Program * program) - { - this->program = program; - } + void + MainWindow::setProgram(Program * program) { + this->program = program; + } -// ===================================================== -// Main -// ===================================================== - SystemFlags - debugger; + // ===================================================== + // Main + // ===================================================== + SystemFlags + debugger; - void - print_SDL_version (const char *preamble, SDL_version * v) - { - printf ("%s %u.%u.%u\n", preamble, v->major, v->minor, v->patch); - } + void + print_SDL_version(const char *preamble, SDL_version * v) { + printf("%s %u.%u.%u\n", preamble, v->major, v->minor, v->patch); + } - int - setupGameItemPaths (int argc, char **argv, Config * config) - { -// Setup path cache for files and folders used in the game - std::map < string, string > &pathCache = - CacheManager::getCachedItem < std::map < string, - string > >(GameConstants::pathCacheLookupKey); + int + setupGameItemPaths(int argc, char **argv, Config * config) { + // Setup path cache for files and folders used in the game + std::map < string, string > &pathCache = + CacheManager::getCachedItem < std::map < string, + string > >(GameConstants::pathCacheLookupKey); - Properties - devProperties; - string - devPropertyFile = Properties::getApplicationPath () + "glest-dev.ini"; - if (fileExists (devPropertyFile) == true) - { - devProperties.load (devPropertyFile); + Properties + devProperties; + string + devPropertyFile = Properties::getApplicationPath() + "glest-dev.ini"; + if (fileExists(devPropertyFile) == true) { + devProperties.load(devPropertyFile); - if (devProperties.hasString ("ServerListPath") == true) - { - string - devItem = devProperties.getString ("ServerListPath"); - if (devItem != "") - { - endPathWithSlash (devItem); - } - if (config != NULL) - { - config->setString ("ServerListPath", devItem, true); - } - } + if (devProperties.hasString("ServerListPath") == true) { + string + devItem = devProperties.getString("ServerListPath"); + if (devItem != "") { + endPathWithSlash(devItem); + } + if (config != NULL) { + config->setString("ServerListPath", devItem, true); + } + } - if (devProperties.hasString ("GlestKeysIniPath") == true) - { - string - devItem = devProperties.getString ("GlestKeysIniPath"); - if (devItem != "") - { - endPathWithSlash (devItem); - } - if (config != NULL) - { - config->setString ("GlestKeysIniPath", devItem, true); - } - } - } + if (devProperties.hasString("GlestKeysIniPath") == true) { + string + devItem = devProperties.getString("GlestKeysIniPath"); + if (devItem != "") { + endPathWithSlash(devItem); + } + if (config != NULL) { + config->setString("GlestKeysIniPath", devItem, true); + } + } + } -//GAME_ARG_DATA_PATH - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_DATA_PATH]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_DATA_PATH]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_DATA_PATH]), - &foundParamIndIndex); - } - string - customPath = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (customPath, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - customPathValue = paramPartTokens[1]; - Properties::applyTagsToValue (customPathValue); - if (customPathValue != "") - { - endPathWithSlash (customPathValue); - } - pathCache[GameConstants::path_data_CacheLookupKey] = - customPathValue; - Properties::setApplicationDataPath (pathCache - [GameConstants:: - path_data_CacheLookupKey]); + //GAME_ARG_DATA_PATH + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_DATA_PATH]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_DATA_PATH]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_DATA_PATH]), + &foundParamIndIndex); + } + string + customPath = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + customPathValue = paramPartTokens[1]; + Properties::applyTagsToValue(customPathValue); + if (customPathValue != "") { + endPathWithSlash(customPathValue); + } + pathCache[GameConstants::path_data_CacheLookupKey] = + customPathValue; + Properties::setApplicationDataPath(pathCache + [GameConstants:: + path_data_CacheLookupKey]); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using custom data path [%s]\n", - customPathValue.c_str ()); - } - else - { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using custom data path [%s]\n", + customPathValue.c_str()); + } else { - printf - ("\nInvalid path specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], false); - return 1; - } - } - else if (config != NULL) - { + printf + ("\nInvalid path specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], false); + return 1; + } + } else if (config != NULL) { - bool - foundPath = false; - string - customPathValue = ""; + bool + foundPath = false; + string + customPathValue = ""; - if (fileExists (devPropertyFile) == true - && devProperties.hasString ("DataPath") == true) - { - foundPath = true; - customPathValue = devProperties.getString ("DataPath", ""); - } - else if (config->getString ("DataPath", "") != "") - { - foundPath = true; - customPathValue = config->getString ("DataPath", ""); - } + if (fileExists(devPropertyFile) == true + && devProperties.hasString("DataPath") == true) { + foundPath = true; + customPathValue = devProperties.getString("DataPath", ""); + } else if (config->getString("DataPath", "") != "") { + foundPath = true; + customPathValue = config->getString("DataPath", ""); + } - if (foundPath == true) - { - pathCache[GameConstants::path_data_CacheLookupKey] = - customPathValue; + if (foundPath == true) { + pathCache[GameConstants::path_data_CacheLookupKey] = + customPathValue; - if (customPathValue != "") - { - endPathWithSlash (customPathValue); - } + if (customPathValue != "") { + endPathWithSlash(customPathValue); + } - Properties::setApplicationDataPath (pathCache - [GameConstants:: - path_data_CacheLookupKey]); + Properties::setApplicationDataPath(pathCache + [GameConstants:: + path_data_CacheLookupKey]); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using ini specified data path [%s]\n", - config->getString ("DataPath", "").c_str ()); - } - } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using ini specified data path [%s]\n", + config->getString("DataPath", "").c_str()); + } + } -//GAME_ARG_INI_PATH - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_INI_PATH]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_INI_PATH]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_INI_PATH]), - &foundParamIndIndex); - } - string - customPath = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (customPath, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - customPathValue = paramPartTokens[1]; - Properties::applyTagsToValue (customPathValue); - pathCache[GameConstants::path_ini_CacheLookupKey] = customPathValue; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using custom ini path [%s]\n", customPathValue.c_str ()); - } - else - { + //GAME_ARG_INI_PATH + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_INI_PATH]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_INI_PATH]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_INI_PATH]), + &foundParamIndIndex); + } + string + customPath = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + customPathValue = paramPartTokens[1]; + Properties::applyTagsToValue(customPathValue); + pathCache[GameConstants::path_ini_CacheLookupKey] = customPathValue; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using custom ini path [%s]\n", customPathValue.c_str()); + } else { - printf - ("\nInvalid path specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], false); - return 1; - } - } + printf + ("\nInvalid path specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], false); + return 1; + } + } -//GAME_ARG_LOG_PATH - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LOG_PATH]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LOG_PATH]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LOG_PATH]), - &foundParamIndIndex); - } - string - customPath = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (customPath, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - customPathValue = paramPartTokens[1]; - Properties::applyTagsToValue (customPathValue); - pathCache[GameConstants::path_logs_CacheLookupKey] = - customPathValue; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using custom logs path [%s]\n", - customPathValue.c_str ()); - } - else - { + //GAME_ARG_LOG_PATH + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LOG_PATH]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LOG_PATH]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LOG_PATH]), + &foundParamIndIndex); + } + string + customPath = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + customPathValue = paramPartTokens[1]; + Properties::applyTagsToValue(customPathValue); + pathCache[GameConstants::path_logs_CacheLookupKey] = + customPathValue; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using custom logs path [%s]\n", + customPathValue.c_str()); + } else { - printf - ("\nInvalid path specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], false); - return 1; - } - } - else if (config != NULL) - { + printf + ("\nInvalid path specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], false); + return 1; + } + } else if (config != NULL) { - bool - foundPath = false; - string - customPathValue = ""; + bool + foundPath = false; + string + customPathValue = ""; - if (fileExists (devPropertyFile) == true - && devProperties.hasString ("LogPath") == true) - { - foundPath = true; - customPathValue = devProperties.getString ("LogPath", ""); - } - else if (config->getString ("LogPath", "") != "") - { - foundPath = true; - customPathValue = config->getString ("LogPath", ""); - } + if (fileExists(devPropertyFile) == true + && devProperties.hasString("LogPath") == true) { + foundPath = true; + customPathValue = devProperties.getString("LogPath", ""); + } else if (config->getString("LogPath", "") != "") { + foundPath = true; + customPathValue = config->getString("LogPath", ""); + } - if (foundPath == true) - { - pathCache[GameConstants::path_logs_CacheLookupKey] = - customPathValue; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using ini specified logs path [%s]\n", - config->getString ("LogPath", "").c_str ()); - } - } + if (foundPath == true) { + pathCache[GameConstants::path_logs_CacheLookupKey] = + customPathValue; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using ini specified logs path [%s]\n", + config->getString("LogPath", "").c_str()); + } + } - Text::DEFAULT_FONT_PATH = - pathCache[GameConstants::path_data_CacheLookupKey]; - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_FONT_PATH]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_FONT_PATH]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_FONT_PATH]), - &foundParamIndIndex); - } - string - customPath = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (customPath, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - customPathValue = paramPartTokens[1]; - Properties::applyTagsToValue (customPathValue); + Text::DEFAULT_FONT_PATH = + pathCache[GameConstants::path_data_CacheLookupKey]; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_FONT_PATH]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_FONT_PATH]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_FONT_PATH]), + &foundParamIndIndex); + } + string + customPath = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(customPath, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + customPathValue = paramPartTokens[1]; + Properties::applyTagsToValue(customPathValue); - Text::DEFAULT_FONT_PATH_ABSOLUTE = customPathValue; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Using custom fonts path [%s]\n", - customPathValue.c_str ()); - } - else - { + Text::DEFAULT_FONT_PATH_ABSOLUTE = customPathValue; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Using custom fonts path [%s]\n", + customPathValue.c_str()); + } else { - printf - ("\nInvalid path specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], false); - return 1; - } - } + printf + ("\nInvalid path specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], false); + return 1; + } + } - return 0; - } + return 0; + } - void - setupLogging (Config & config, bool haveSpecialOutputCommandLineOption) - { + void + setupLogging(Config & config, bool haveSpecialOutputCommandLineOption) { - SystemFlags::getSystemSettingType (SystemFlags::debugSystem).enabled = - config.getBool ("DebugMode", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugNetwork).enabled = - config.getBool ("DebugNetwork", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - enabled = config.getBool ("DebugPerformance", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled = config.getBool ("DebugWorldSynch", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugUnitCommands). - enabled = config.getBool ("DebugUnitCommands", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugPathFinder). - enabled = config.getBool ("DebugPathFinder", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled = - config.getBool ("DebugLUA", "false"); - LuaScript::setDebugModeEnabled (SystemFlags:: - getSystemSettingType (SystemFlags:: - debugLUA). - enabled); - SystemFlags::getSystemSettingType (SystemFlags::debugSound).enabled = - config.getBool ("DebugSound", "false"); - SystemFlags::getSystemSettingType (SystemFlags::debugError).enabled = - config.getBool ("DebugError", "true"); + SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = + config.getBool("DebugMode", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = + config.getBool("DebugNetwork", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + enabled = config.getBool("DebugPerformance", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled = config.getBool("DebugWorldSynch", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands). + enabled = config.getBool("DebugUnitCommands", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugPathFinder). + enabled = config.getBool("DebugPathFinder", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled = + config.getBool("DebugLUA", "false"); + LuaScript::setDebugModeEnabled(SystemFlags:: + getSystemSettingType(SystemFlags:: + debugLUA). + enabled); + SystemFlags::getSystemSettingType(SystemFlags::debugSound).enabled = + config.getBool("DebugSound", "false"); + SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled = + config.getBool("DebugError", "true"); - string - userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } + string + userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } #ifdef HAVE_GOOGLE_BREAKPAD - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 In setting up errorHandlerPtr->set_dump_path [%p]...\n", - errorHandlerPtr.get ()); - if (errorHandlerPtr.get () != NULL) - { - string - dumpFilePath; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - dumpFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey); - } - else - { - dumpFilePath = userData; - } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 In setting up errorHandlerPtr->set_dump_path [%p]...\n", + errorHandlerPtr.get()); + if (errorHandlerPtr.get() != NULL) { + string + dumpFilePath; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + dumpFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey); + } else { + dumpFilePath = userData; + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 In setting up errorHandlerPtr->set_dump_path...\n"); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 In setting up errorHandlerPtr->set_dump_path...\n"); # if defined(WIN32) - wstring - dumpfilepath = utf8_decode (dumpFilePath); - if (SystemFlags::VERBOSE_MODE_ENABLED) - wprintf - (L"Hooking up google_breakpad::ExceptionHandler to save dmp files to [%s]...\n", - dumpfilepath.c_str ()); - errorHandlerPtr->set_dump_path (dumpfilepath); + wstring + dumpfilepath = utf8_decode(dumpFilePath); + if (SystemFlags::VERBOSE_MODE_ENABLED) + wprintf + (L"Hooking up google_breakpad::ExceptionHandler to save dmp files to [%s]...\n", + dumpfilepath.c_str()); + errorHandlerPtr->set_dump_path(dumpfilepath); # else - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Hooking up google_breakpad::ExceptionHandler to save dmp files to [%s]...\n", - dumpFilePath.c_str ()); -//errorHandlerPtr->set_dump_path(dumpfilepath); - google_breakpad::MinidumpDescriptor descriptor (dumpFilePath); - errorHandlerPtr->set_minidump_descriptor (descriptor); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Hooking up google_breakpad::ExceptionHandler to save dmp files to [%s]...\n", + dumpFilePath.c_str()); + //errorHandlerPtr->set_dump_path(dumpfilepath); + google_breakpad::MinidumpDescriptor descriptor(dumpFilePath); + errorHandlerPtr->set_minidump_descriptor(descriptor); # endif - } + } #endif - string - debugLogFile = config.getString ("DebugLogFile", ""); - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - debugLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugLogFile; - } - else - { - debugLogFile = userData + debugLogFile; - } - - string - debugWorldSynchLogFile = - config.getString ("DebugLogFileWorldSynch", ""); - if (debugWorldSynchLogFile == "") - { - debugWorldSynchLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugWorldSynchLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugWorldSynchLogFile; - } - else - { - debugWorldSynchLogFile = userData + debugWorldSynchLogFile; - } - - string - debugPerformanceLogFile = - config.getString ("DebugLogFilePerformance", ""); - if (debugPerformanceLogFile == "") - { - debugPerformanceLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugPerformanceLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugPerformanceLogFile; - } - else - { - debugPerformanceLogFile = userData + debugPerformanceLogFile; - } - - string - debugNetworkLogFile = config.getString ("DebugLogFileNetwork", ""); - if (debugNetworkLogFile == "") - { - debugNetworkLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugNetworkLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugNetworkLogFile; - } - else - { - debugNetworkLogFile = userData + debugNetworkLogFile; - } - - string - debugUnitCommandsLogFile = - config.getString ("DebugLogFileUnitCommands", ""); - if (debugUnitCommandsLogFile == "") - { - debugUnitCommandsLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugUnitCommandsLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugUnitCommandsLogFile; - } - else - { - debugUnitCommandsLogFile = userData + debugUnitCommandsLogFile; - } - - string - debugPathFinderLogFile = - config.getString ("DebugLogFilePathFinder", ""); - if (debugPathFinderLogFile == "") - { - debugPathFinderLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugPathFinderLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugPathFinderLogFile; - } - else - { - debugPathFinderLogFile = userData + debugPathFinderLogFile; - } - - string - debugLUALogFile = config.getString ("DebugLogFileLUA", ""); - if (debugLUALogFile == "") - { - debugLUALogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugLUALogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugLUALogFile; - } - else - { - debugLUALogFile = userData + debugLUALogFile; - } - - string - debugSoundLogFile = config.getString ("DebugLogFileSound", ""); - if (debugSoundLogFile == "") - { - debugSoundLogFile = debugLogFile; - } - else if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - debugSoundLogFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - debugSoundLogFile; - } - else - { - debugSoundLogFile = userData + debugSoundLogFile; - } - - string - debugErrorLogFile = config.getString ("DebugLogFileError", ""); - - SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - debugLogFileName = debugLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - debugLogFileName = debugNetworkLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugPerformance). - debugLogFileName = debugPerformanceLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - debugLogFileName = debugWorldSynchLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugUnitCommands). - debugLogFileName = debugUnitCommandsLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugPathFinder). - debugLogFileName = debugPathFinderLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - debugLogFileName = debugLUALogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugSound). - debugLogFileName = debugSoundLogFile; - SystemFlags::getSystemSettingType (SystemFlags::debugError). - debugLogFileName = debugErrorLogFile; - - if (haveSpecialOutputCommandLineOption == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("--- Startup log settings are ---\ndebugSystem [%d][%s]\ndebugNetwork [%d][%s]\ndebugPerformance [%d][%s]\ndebugWorldSynch [%d][%s]\ndebugUnitCommands[%d][%s]\ndebugPathFinder[%d][%s]\ndebugLUA [%d][%s]\ndebugSound [%d][%s]\ndebugError [%d][%s]\n", - SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled, debugLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - enabled, debugNetworkLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags:: - debugPerformance).enabled, - debugPerformanceLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled, debugWorldSynchLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags:: - debugUnitCommands).enabled, - debugUnitCommandsLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugPathFinder). - enabled, debugPathFinderLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled, debugLUALogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugSound). - enabled, debugSoundLogFile.c_str (), - SystemFlags::getSystemSettingType (SystemFlags::debugError). - enabled, debugErrorLogFile.c_str ()); - } - } - - void - runTilesetValidationForPath (string tilesetPath, string tilesetName, - World & world, bool purgeUnusedFiles, - bool purgeDuplicateFiles, - bool showDuplicateFiles, bool gitPurgeFiles, - double &purgedMegaBytes) - { - Checksum - checksum; - -//bool techtree_errors = false; - - std::map < string, vector < pair < string, string > > >loadedFileList; - vector < string > pathList; - pathList.push_back (tilesetPath); - world.loadTileset (pathList, tilesetName, &checksum, loadedFileList); - -// Fixup paths with .. - { - std::map < string, vector < pair < string, - string > > >newLoadedFileList; - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = loadedFileList.begin (); - iterMap != loadedFileList.end (); ++iterMap) - { - string - loadedFile = iterMap->first; - - replaceAll (loadedFile, "//", "/"); - replaceAll (loadedFile, "\\\\", "\\"); - updatePathClimbingParts (loadedFile); - - if (newLoadedFileList.find (loadedFile) != newLoadedFileList.end ()) - { - for (unsigned int xx1 = 0; xx1 < iterMap->second.size (); ++xx1) - { - pair < string, string > &newVal = iterMap->second[xx1]; - replaceAll (newVal.first, "//", "/"); - replaceAll (newVal.first, "\\\\", "\\"); - updatePathClimbingParts (newVal.first); - replaceAll (newVal.second, "//", "/"); - replaceAll (newVal.second, "\\\\", "\\"); - updatePathClimbingParts (newVal.second); - - newLoadedFileList[loadedFile].push_back (newVal); - } - } - else - { - for (unsigned int xx1 = 0; xx1 < iterMap->second.size (); ++xx1) - { - pair < string, string > &newVal = iterMap->second[xx1]; - replaceAll (newVal.first, "//", "/"); - replaceAll (newVal.first, "\\\\", "\\"); - updatePathClimbingParts (newVal.first); - replaceAll (newVal.second, "//", "/"); - replaceAll (newVal.second, "\\\\", "\\"); - updatePathClimbingParts (newVal.second); - } - - newLoadedFileList[loadedFile] = iterMap->second; - } - } - loadedFileList = newLoadedFileList; - } - -// Validate the faction setup to ensure we don't have any bad associations -// std::vector resultErrors = world.validateFactionTypes(); - -// Now check for unused files in the techtree - std::map < string, vector < pair < string, string > > >foundFileList; - for (unsigned int i = 0; i < pathList.size (); ++i) - { - string - path = pathList[i]; - endPathWithSlash (path); - path = path + tilesetName + "/"; - - replaceAll (path, "//", "/"); - replaceAll (path, "\\\\", "\\"); - - vector < string > foundFiles = - getFolderTreeContentsListRecursively (path + "*.", ""); - for (unsigned int j = 0; j < foundFiles.size (); ++j) - { - string - file = foundFiles[j]; - replaceAll (file, "//", "/"); - replaceAll (file, "\\\\", "\\"); - - replaceAll (file, "//", "/"); - replaceAll (file, "\\\\", "\\"); - - foundFileList[file].push_back (make_pair (path, path)); - } - } - - printf ("Found tileset filecount = " MG_SIZE_T_SPECIFIER ", used = " - MG_SIZE_T_SPECIFIER "\n", foundFileList.size (), - loadedFileList.size ()); - - int - purgeCount = 0; - bool - foundUnusedFile = false; - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = foundFileList.begin (); - iterMap != foundFileList.end (); ++iterMap) - { - string - foundFile = iterMap->first; - replaceAll (foundFile, "//", "/"); - replaceAll (foundFile, "\\\\", "\\"); - - if (loadedFileList.find (foundFile) == loadedFileList.end () && - foundFile.find ("lang/") == foundFile.npos) - { - if (foundUnusedFile == false) - { - printf - ("\nLine ref: %d, Warning, unused files were detected - START:\n=====================\n", - __LINE__); - } - foundUnusedFile = true; - - printf ("[%s]\n", foundFile.c_str ()); - - string - fileName = extractFileFromDirectoryPath (foundFile); - if (loadedFileList.find (fileName) != loadedFileList.end ()) - { - printf ("possible match on [%s] ?\n", - loadedFileList.find (fileName)->first.c_str ()); - } - else if (purgeUnusedFiles == true) - { - off_t - fileSize = getFileSize (foundFile); -// convert to MB - purgedMegaBytes += ((double) fileSize / 1048576.0); - purgeCount++; - - if (gitPurgeFiles == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", foundFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error ("Call to command failed [" + - string (szBuf) + "]"); - } - } - else - { - removeFile (foundFile); - } - } - } - } - if (foundUnusedFile == true) - { - if (purgedMegaBytes > 0) - { - printf ("Purged %.2f MB (%d) in files\n", purgedMegaBytes, - purgeCount); - } - printf - ("\nLine ref: %d, Warning, unused files were detected - END:\n", - __LINE__); - } - - if (showDuplicateFiles == true) - { - std::map < uint32, vector < string > >mapDuplicateFiles; -// Now check for duplicate data content - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = loadedFileList.begin (); - iterMap != loadedFileList.end (); ++iterMap) - { - string - fileName = iterMap->first; - Checksum - checksum; - checksum.addFile (fileName); - uint32 - crcValue = checksum.getSum (); - mapDuplicateFiles[crcValue].push_back (fileName); - } - - double - duplicateMegaBytesPurged = 0; - int - duplicateCountPurged = 0; - - double - duplicateMegaBytes = 0; - int - duplicateCount = 0; - - bool - foundDuplicates = false; - for (std::map < uint32, vector < string > >::iterator iterMap = - mapDuplicateFiles.begin (); iterMap != mapDuplicateFiles.end (); - ++iterMap) - { - vector < string > &fileList = iterMap->second; - if (fileList.size () > 1) - { - if (foundDuplicates == false) - { - foundDuplicates = true; - printf - ("\nWarning, duplicate files were detected - START:\n=====================\n"); - } - - map < string, int > - parentList; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - if (idx > 0) - { - off_t - fileSize = getFileSize (duplicateFile); -// convert to MB - duplicateMegaBytes += ((double) fileSize / 1048576.0); - duplicateCount++; - } - else - { - printf ("\n"); - } - - printf ("[%s]\n", duplicateFile.c_str ()); - std::map < string, vector < pair < string, - string > > >::iterator iterFind = - loadedFileList.find (duplicateFile); - if (iterFind != loadedFileList.end ()) - { - for (unsigned int jdx = 0; jdx < iterFind->second.size (); - jdx++) - { - parentList[iterFind->second[jdx].first]++; - } - } - } - - for (map < string, int >::iterator iterMap1 = parentList.begin (); - iterMap1 != parentList.end (); ++iterMap1) - { - - if (iterMap1 == parentList.begin ()) - { - printf ("\tParents:\n"); - } - printf ("\t[%s]\n", iterMap1->first.c_str ()); - } - - if (purgeDuplicateFiles == true) - { - - string - newCommonFileName = ""; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - off_t - fileSize = getFileSize (duplicateFile); - if (idx == 0) - { - newCommonFileName = - "$COMMONDATAPATH/sounds/" + - extractFileFromDirectoryPath (duplicateFile); - - string - expandedNewCommonFileName = newCommonFileName; - - std::map < string, string > mapExtraTagReplacementValues; - - mapExtraTagReplacementValues = - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues); - Properties::applyTagsToValue (expandedNewCommonFileName, - &mapExtraTagReplacementValues); - replaceAll (expandedNewCommonFileName, "//", "/"); - createDirectoryPaths (extractDirectoryPathFromFile - (expandedNewCommonFileName)); - - if (gitPurgeFiles == true) - { - copyFileTo (duplicateFile, expandedNewCommonFileName); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", - duplicateFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error ("Call to command failed [" - + string (szBuf) + "]"); - } - printf - ("*** Duplicate file:\n[%s]\nwas git rm and copied to:\n[%s]\n", - duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - } - else - { -//int result = 0; - int - result = rename (duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - if (result != 0) - { - char - szBuf[8096] = ""; - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "!!! Error [%s] Could not rename [%s] to [%s]!", - errmsg, duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - throw - megaglest_runtime_error (szBuf); - } - else - { - printf - ("*** Duplicate file:\n[%s]\nwas renamed to:\n[%s]\n", - duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - } - } - } - else - { - if (gitPurgeFiles == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", - duplicateFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error ("Call to command failed [" - + string (szBuf) + "]"); - } - printf ("*** Duplicate file:\n[%s]\nwas git rm\n", - duplicateFile.c_str ()); - } - else - { - removeFile (duplicateFile); - } - printf ("*** Duplicate file:\n[%s]\nwas removed\n", - duplicateFile.c_str ()); - -// convert to MB - duplicateMegaBytesPurged += - ((double) fileSize / 1048576.0); - duplicateCountPurged++; - } - } - } - - std::map < string, int > - mapUniqueParentList; - - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - std::map < string, vector < pair < string, - string > > >::iterator iterFind2 = - loadedFileList.find (duplicateFile); - if (iterFind2 != loadedFileList.end ()) - { - for (unsigned int jdx1 = 0; - jdx1 < iterFind2->second.size (); jdx1++) - { - string - parentFile = iterFind2->second[jdx1].first; - string - searchText = iterFind2->second[jdx1].second; - - if (mapUniqueParentList.find (parentFile) == - mapUniqueParentList.end ()) - { - printf - ("*** Searching parent file:\n[%s]\nfor duplicate file reference:\n[%s]\nto replace with newname:\n[%s]\n", - parentFile.c_str (), searchText.c_str (), - newCommonFileName.c_str ()); - bool - foundText = - searchAndReplaceTextInFile (parentFile, searchText, - newCommonFileName, - false); - printf ("foundText = %d\n", foundText); - if (foundText == false) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref = %d, Error finding text [%s] in file [%s]", - __LINE__, searchText.c_str (), - parentFile.c_str ()); - throw - megaglest_runtime_error (szBuf); - } - mapUniqueParentList[parentFile]++; - } - } - } - } - } - } - else - { - string - newCommonFileName = ""; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { -//off_t fileSize = getFileSize(duplicateFile); - if (idx == 0) - { - newCommonFileName = - "$COMMONDATAPATH/sounds/" + - extractFileFromDirectoryPath (duplicateFile); - break; - } - } - } - - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - std::map < string, vector < pair < string, - string > > >::iterator iterFind4 = - loadedFileList.find (duplicateFile); - if (iterFind4 != loadedFileList.end ()) - { - for (unsigned int jdx = 0; - jdx < iterFind4->second.size (); jdx++) - { - string - parentFile = iterFind4->second[jdx].first; - string - searchText = iterFind4->second[jdx].second; - - bool - foundText = - searchAndReplaceTextInFile (parentFile, searchText, - newCommonFileName, true); - if (foundText == false) - { - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", - __LINE__, searchText.c_str (), - parentFile.c_str (), - newCommonFileName.c_str ()); - printf - ("\n\n=================================================\n%s", - szBuf); - - throw - megaglest_runtime_error (szBuf); - } - } - } - } - } - } - } - } - if (foundDuplicates == true) - { - printf ("Duplicates %.2f MB (%d) in files\n", duplicateMegaBytes, - duplicateCount); - printf ("Duplicates purged %.2f MB (%d) in files\n", - duplicateMegaBytesPurged, duplicateCountPurged); - - printf ("\nWarning, duplicate files were detected - END:\n"); - } - } - -//if(techtree_errors == false) { - printf - ("\nValidation found NO ERRORS for tilesetPath [%s] tilesetName [%s]:\n", - tilesetPath.c_str (), tilesetName.c_str ()); -//} - - printf - ("----------------------------------------------------------------"); - } - - void - runTechValidationForPath (string techPath, string techName, - const std::vector < string > - &filteredFactionList, World & world, - bool purgeUnusedFiles, bool purgeDuplicateFiles, - bool showDuplicateFiles, bool gitPurgeFiles, - double &purgedMegaBytes) - { - - string - techTreeFolder = techPath + techName; - string - techTreeFactionFolder = techTreeFolder + "/factions/"; - vector < string > factionsList; - findDirs (techTreeFactionFolder, factionsList, false, false); - - if (factionsList.empty () == false) - { - Checksum - checksum; - set < string > factions; - for (int j = 0; j < (int) factionsList.size (); ++j) - { - if (filteredFactionList.empty () == true || - std::find (filteredFactionList.begin (), - filteredFactionList.end (), - factionsList[j]) != filteredFactionList.end ()) - { - factions.insert (factionsList[j]); - } - } - - printf - ("\n----------------------------------------------------------------"); - printf - ("\nChecking techPath [%s] techName [%s] total faction count = %d\n", - techPath.c_str (), techName.c_str (), (int) factionsList.size ()); - for (int j = 0; j < (int) factionsList.size (); ++j) - { - if (filteredFactionList.empty () == true || - std::find (filteredFactionList.begin (), - filteredFactionList.end (), - factionsList[j]) != filteredFactionList.end ()) - { - printf ("Using faction [%s]\n", factionsList[j].c_str ()); - } - } - - if (factions.empty () == false) - { - bool - techtree_errors = false; - - std::map < string, vector < pair < string, - string > > >loadedFileList; - vector < string > pathList; - pathList.push_back (techPath); - Config & config = Config::getInstance (); - vector < string > otherTechPaths = - config.getPathListForType (ptTechs, ""); - pathList.insert (pathList.end (), otherTechPaths.begin (), - otherTechPaths.end ()); - - try - { - world.loadTech (pathList, techName, factions, &checksum, - loadedFileList, true); - -// Fixup paths with .. - { - std::map < string, vector < pair < string, - string > > >newLoadedFileList; - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = loadedFileList.begin (); - iterMap != loadedFileList.end (); ++iterMap) - { - string - loadedFile = iterMap->first; - - replaceAll (loadedFile, "//", "/"); - replaceAll (loadedFile, "\\\\", "\\"); - updatePathClimbingParts (loadedFile); - - if (newLoadedFileList.find (loadedFile) != - newLoadedFileList.end ()) - { - for (unsigned int xx1 = 0; xx1 < iterMap->second.size (); - ++xx1) - { - pair < string, string > &newVal = iterMap->second[xx1]; - replaceAll (newVal.first, "//", "/"); - replaceAll (newVal.first, "\\\\", "\\"); - updatePathClimbingParts (newVal.first); - replaceAll (newVal.second, "//", "/"); - replaceAll (newVal.second, "\\\\", "\\"); - updatePathClimbingParts (newVal.second); - - newLoadedFileList[loadedFile].push_back (newVal); - } - } - else - { - for (unsigned int xx1 = 0; xx1 < iterMap->second.size (); - ++xx1) - { - pair < string, string > &newVal = iterMap->second[xx1]; - replaceAll (newVal.first, "//", "/"); - replaceAll (newVal.first, "\\\\", "\\"); - updatePathClimbingParts (newVal.first); - replaceAll (newVal.second, "//", "/"); - replaceAll (newVal.second, "\\\\", "\\"); - updatePathClimbingParts (newVal.second); - } - - newLoadedFileList[loadedFile] = iterMap->second; - } - } - loadedFileList = newLoadedFileList; - } - -// Validate the faction setup to ensure we don't have any bad associations - std::vector < std::string > resultErrors = - world.validateFactionTypes (); - if (resultErrors.empty () == false) - { - techtree_errors = true; -// Display the validation errors - string - errorText = - "\nErrors were detected:\n=====================\n"; - for (int i = 0; i < (int) resultErrors.size (); ++i) - { - if (i > 0) - { - errorText += "\n"; - } - errorText = errorText + resultErrors[i]; - } - errorText += "\n=====================\n"; - printf ("%s", errorText.c_str ()); - } - -// Validate the faction resource setup to ensure we don't have any bad associations - printf ("\nChecking resources, count = %d\n", - world.getTechTree ()->getResourceTypeCount ()); - - for (int i = 0; i < world.getTechTree ()->getResourceTypeCount (); - ++i) - { - printf ("Found techtree resource [%s]\n", - world.getTechTree ()->getResourceType (i)->getName (). - c_str ()); - } - - resultErrors = world.validateResourceTypes (); - if (resultErrors.empty () == false) - { - techtree_errors = true; -// Display the validation errors - string - errorText = - "\nErrors were detected:\n=====================\n"; - for (int i = 0; i < (int) resultErrors.size (); ++i) - { - if (i > 0) - { - errorText += "\n"; - } - errorText = errorText + resultErrors[i]; - } - errorText += "\n=====================\n"; - printf ("%s", errorText.c_str ()); - } - -// Now check for unused files in the techtree - std::map < string, vector < pair < string, - string > > >foundFileList; - for (unsigned int i = 0; i < pathList.size (); ++i) - { - string - path = pathList[i]; - endPathWithSlash (path); - path = path + techName + "/"; - - replaceAll (path, "//", "/"); - replaceAll (path, "\\\\", "\\"); - - vector < string > foundFiles = - getFolderTreeContentsListRecursively (path + "*.", ""); - for (unsigned int j = 0; j < foundFiles.size (); ++j) - { - string - file = foundFiles[j]; - replaceAll (file, "//", "/"); - replaceAll (file, "\\\\", "\\"); - -// ignore loading screen, preview screen and hud - if (file.find (GameConstants::LOADING_SCREEN_FILE) != - string::npos - || file.find (GameConstants::PREVIEW_SCREEN_FILE) != - string::npos - || file.find (GameConstants::HUD_SCREEN_FILE) != - string::npos) - { - continue; - } - -// ignore commondata since we are not loading all factions - if (filteredFactionList.size () > 0) - { - if (file.find ("/commondata/") != string::npos) - { - continue; - } - } - - if (file.find ("/factions/") != string::npos) - { - bool - includeFaction = false; - for (set < string >::iterator it = factions.begin (); - it != factions.end (); ++it) - { - string - currentFaction = *it; - if (file.find ("/factions/" + currentFaction) != - string::npos) - { - includeFaction = true; - break; - } - } - if (includeFaction == false) - { - continue; - } - } - - replaceAll (file, "//", "/"); - replaceAll (file, "\\\\", "\\"); - - foundFileList[file].push_back (make_pair (path, path)); - } - } - - printf ("Found techtree filecount = " MG_SIZE_T_SPECIFIER - ", used = " MG_SIZE_T_SPECIFIER "\n", - foundFileList.size (), loadedFileList.size ()); - - int - purgeCount = 0; - bool - foundUnusedFile = false; - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = foundFileList.begin (); - iterMap != foundFileList.end (); ++iterMap) - { - string - foundFile = iterMap->first; - replaceAll (foundFile, "//", "/"); - replaceAll (foundFile, "\\\\", "\\"); - - if (loadedFileList.find (foundFile) == loadedFileList.end () && - foundFile.find ("lang/") == foundFile.npos) - { - if (foundUnusedFile == false) - { - printf - ("\nLine ref: %d, Warning, unused files were detected - START:\n=====================\n", - __LINE__); - } - foundUnusedFile = true; - - printf ("[%s]\n", foundFile.c_str ()); - - string - fileName = extractFileFromDirectoryPath (foundFile); - if (loadedFileList.find (fileName) != loadedFileList.end ()) - { - printf ("possible match on [%s] ?\n", - loadedFileList.find (fileName)->first.c_str ()); - } - else if (purgeUnusedFiles == true) - { - off_t - fileSize = getFileSize (foundFile); -// convert to MB - purgedMegaBytes += ((double) fileSize / 1048576.0); - purgeCount++; - - if (gitPurgeFiles == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", - foundFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error ("Call to command failed [" + - string (szBuf) + "]"); - } - } - else - { - removeFile (foundFile); - } - } - } - } - if (foundUnusedFile == true) - { - if (purgedMegaBytes > 0) - { - printf ("Purged %.2f MB (%d) in files\n", purgedMegaBytes, - purgeCount); - } - printf - ("\nLine ref: %d, Warning, unused files were detected - END:\n", - __LINE__); - } - - if (showDuplicateFiles == true) - { - std::map < uint32, vector < string > >mapDuplicateFiles; -// Now check for duplicate data content - for (std::map < string, vector < pair < string, - string > > >::iterator iterMap = loadedFileList.begin (); - iterMap != loadedFileList.end (); ++iterMap) - { - string - fileName = iterMap->first; - Checksum - checksum; - checksum.addFile (fileName); - uint32 - crcValue = checksum.getSum (); - if (crcValue == 0) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Error calculating CRC for file [%s]", - fileName.c_str ()); - throw - megaglest_runtime_error (szBuf); - } - mapDuplicateFiles[crcValue].push_back (fileName); - } - - double - duplicateMegaBytesPurged = 0; - int - duplicateCountPurged = 0; - - double - duplicateMegaBytes = 0; - int - duplicateCount = 0; - - bool - foundDuplicates = false; - for (std::map < uint32, vector < string > >::iterator iterMap = - mapDuplicateFiles.begin (); - iterMap != mapDuplicateFiles.end (); ++iterMap) - { - vector < string > &fileList = iterMap->second; - if (fileList.size () > 1) - { - if (foundDuplicates == false) - { - foundDuplicates = true; - printf - ("\nWarning, duplicate files were detected - START:\n=====================\n"); - } - - printf ("----- START duplicate files for CRC [%u] count [" - MG_SIZE_T_SPECIFIER "] first file is [%s]\n", - iterMap->first, fileList.size (), - fileList[0].c_str ()); - - map < string, int > - parentList; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - if (idx > 0) - { - off_t - fileSize = getFileSize (duplicateFile); -// convert to MB - duplicateMegaBytes += ((double) fileSize / 1048576.0); - duplicateCount++; - } - else - { - printf ("\n"); - } - - printf ("[%s]\n", duplicateFile.c_str ()); - std::map < string, vector < pair < string, - string > > >::iterator iterFind = - loadedFileList.find (duplicateFile); - if (iterFind != loadedFileList.end ()) - { - for (unsigned int jdx = 0; - jdx < iterFind->second.size (); jdx++) - { - parentList[iterFind->second[jdx].first]++; - } - } - - string - newCommonFileName = - "$COMMONDATAPATH/sounds/" + - extractFileFromDirectoryPath (duplicateFile); - string - expandedNewCommonFileName = newCommonFileName; - std::map < string, string > mapExtraTagReplacementValues; - string - techCommonData = techPath + techName + "/commondata/"; - replaceAll (techCommonData, "//", "/"); - mapExtraTagReplacementValues["$COMMONDATAPATH"] = - techCommonData; - mapExtraTagReplacementValues = - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues); - Properties::applyTagsToValue (expandedNewCommonFileName, - &mapExtraTagReplacementValues); - replaceAll (expandedNewCommonFileName, "//", "/"); - } - - printf ("----- Finding parents for duplicate files [" - MG_SIZE_T_SPECIFIER "] first file is [%s]\n", - fileList.size (), fileList[0].c_str ()); - - for (map < string, int >::iterator iterMap1 = - parentList.begin (); iterMap1 != parentList.end (); - ++iterMap1) - { - - if (iterMap1 == parentList.begin ()) - { - printf ("\tParents:\n"); - } - printf ("\t[%s]\n", iterMap1->first.c_str ()); - } - - if (purgeDuplicateFiles == true) - { - - printf ("----- move / remove duplicate files [" - MG_SIZE_T_SPECIFIER "] first file is [%s]\n", - fileList.size (), fileList[0].c_str ()); -// First move first duplicate to commondata and delete all other copies - string - newCommonFileName = ""; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - off_t - fileSize = getFileSize (duplicateFile); - - printf ("#1 [%u / " MG_SIZE_T_SPECIFIER - "] removing duplicate [%s]\n", idx, - fileList.size (), duplicateFile.c_str ()); - - if (idx == 0) - { - newCommonFileName = - "$COMMONDATAPATH/sounds/" + - extractFileFromDirectoryPath (duplicateFile); - - string - expandedNewCommonFileName = newCommonFileName; - - std::map < string, - string > mapExtraTagReplacementValues; - - string - techCommonData = - techPath + techName + "/commondata/"; - replaceAll (techCommonData, "//", "/"); - - mapExtraTagReplacementValues["$COMMONDATAPATH"] = - techCommonData; - mapExtraTagReplacementValues = - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues); - Properties::applyTagsToValue - (expandedNewCommonFileName, - &mapExtraTagReplacementValues); - replaceAll (expandedNewCommonFileName, "//", "/"); - createDirectoryPaths (extractDirectoryPathFromFile - (expandedNewCommonFileName)); - - if (gitPurgeFiles == true) - { - copyFileTo (duplicateFile, - expandedNewCommonFileName); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", - duplicateFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error - ("Call to command failed [" + string (szBuf) + - "]"); - } - printf - ("*** Duplicate file:\n[%s]\nwas git rm and copied to:\n[%s]\n", - duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - } - else - { - printf - ("moving duplicate [%s] to common data [%s] expanded to [%s]\n", - duplicateFile.c_str (), - newCommonFileName.c_str (), - expandedNewCommonFileName.c_str ()); - - int - result = rename (duplicateFile.c_str (), - expandedNewCommonFileName.c_str - ()); - if (result != 0) - { - char - szBuf[8096] = ""; - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "!!! Error [%s] Could not rename [%s] to [%s]!", - errmsg, duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - throw - megaglest_runtime_error (szBuf); - } - else - { - printf - ("*** Duplicate file:\n[%s]\nwas renamed to:\n[%s]\n", - duplicateFile.c_str (), - expandedNewCommonFileName.c_str ()); - } - } - } - else - { - if (gitPurgeFiles == true) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "git rm \"%s\"", - duplicateFile.c_str ()); - bool - gitOk = executeShellCommand (szBuf, 0); - if (gitOk == false) - { - throw - megaglest_runtime_error - ("Call to command failed [" + string (szBuf) + - "]"); - } - printf ("*** Duplicate file:\n[%s]\nwas git rm\n", - duplicateFile.c_str ()); - } - else - { - printf ("removing duplicate [%s]\n", - duplicateFile.c_str ()); - removeFile (duplicateFile); - } - printf ("*** Duplicate file:\n[%s]\nwas removed\n", - duplicateFile.c_str ()); - -// convert to MB - duplicateMegaBytesPurged += - ((double) fileSize / 1048576.0); - duplicateCountPurged++; - } - } - } - - printf ("----- update XML files for duplicate files [" - MG_SIZE_T_SPECIFIER "] first file is [%s]\n", - fileList.size (), fileList[0].c_str ()); - std::map < string, int > - mapUniqueParentList; - -// Update the XML files to point to the new single copy in commondata - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - std::map < string, vector < pair < string, - string > > >::iterator iterFind2 = - loadedFileList.find (duplicateFile); - if (iterFind2 != loadedFileList.end ()) - { - for (unsigned int jdx1 = 0; - jdx1 < iterFind2->second.size (); jdx1++) - { - string - parentFile = iterFind2->second[jdx1].first; - string - searchText = iterFind2->second[jdx1].second; - - if (mapUniqueParentList.find (parentFile) == - mapUniqueParentList.end ()) - { - printf - ("*** Searching parent file:\n[%s]\nfor duplicate file reference:\n[%s]\nto replace with newname:\n[%s]\n", - parentFile.c_str (), searchText.c_str (), - newCommonFileName.c_str ()); - bool - foundText = - searchAndReplaceTextInFile (parentFile, - searchText, - newCommonFileName, - false); - printf ("foundText = %d\n", foundText); - if (foundText == false) - { - - string - techCommonData = - techPath + techName + "/commondata/"; - replaceAll (techCommonData, "//", "/"); - - if (StartsWith (searchText, techCommonData) == - true) - { - printf - ("WARNING #1 [%d] techCommonData check\n[%s]\n[%s]\n[%s]\n[%s]\n", - foundText, parentFile.c_str (), - techCommonData.c_str (), - searchText.c_str (), - newCommonFileName.c_str ()); - - replaceAll (searchText, techCommonData, - "$COMMONDATAPATH/"); - foundText = - searchAndReplaceTextInFile (parentFile, - searchText, - newCommonFileName, - false); - - printf - ("WARNING #2 [%d] techCommonData check\n[%s]\n[%s]\n[%s]\n[%s]\n", - foundText, parentFile.c_str (), - techCommonData.c_str (), - searchText.c_str (), - newCommonFileName.c_str ()); - } - if (foundText == false) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", - __LINE__, searchText.c_str (), - parentFile.c_str (), - newCommonFileName.c_str ()); - printf - ("\n\n=================================================\n%s", - szBuf); - - throw - megaglest_runtime_error (szBuf); - } - } - mapUniqueParentList[parentFile]++; - } - } - } - } - } - } - else - { - - string - newCommonFileName = ""; - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { -//off_t fileSize = getFileSize(duplicateFile); - if (idx == 0) - { - newCommonFileName = - "$COMMONDATAPATH/sounds/" + - extractFileFromDirectoryPath (duplicateFile); - break; - } - } - } - - for (unsigned int idx = 0; idx < fileList.size (); ++idx) - { - string - duplicateFile = fileList[idx]; - string - fileExt = extractExtension (duplicateFile); - if (fileExt == "wav" || fileExt == "ogg") - { - std::map < string, vector < pair < string, - string > > >::iterator iterFind4 = - loadedFileList.find (duplicateFile); - if (iterFind4 != loadedFileList.end ()) - { - for (unsigned int jdx = 0; - jdx < iterFind4->second.size (); jdx++) - { - string - parentFile = iterFind4->second[jdx].first; - string - searchText = iterFind4->second[jdx].second; - - bool - foundText = - searchAndReplaceTextInFile (parentFile, - searchText, - newCommonFileName, - true); - - if (foundText == false) - { - string - techCommonData = - techPath + techName + "/commondata/"; - replaceAll (techCommonData, "//", "/"); - - if (StartsWith (searchText, techCommonData) == - true) - { - replaceAll (searchText, techCommonData, - "$COMMONDATAPATH/"); - foundText = - searchAndReplaceTextInFile (parentFile, - searchText, - newCommonFileName, - true); - - } - if (foundText == false) - { - -// Check if the sound file already references commandata - foundText = - searchAndReplaceTextInFile (parentFile, - newCommonFileName, - newCommonFileName, - true); - if (foundText == false) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", - __LINE__, searchText.c_str (), - parentFile.c_str (), - newCommonFileName.c_str ()); - printf - ("\n\n=================================================\n%s", - szBuf); - - throw - megaglest_runtime_error (szBuf); - } - } - } - } - } - } - } - } - - - printf ("----- END duplicate files [" MG_SIZE_T_SPECIFIER - "] first file is [%s]\n", fileList.size (), - fileList[0].c_str ()); - } - } - if (foundDuplicates == true) - { - printf ("Duplicates %.2f MB (%d) in files\n", - duplicateMegaBytes, duplicateCount); - printf ("Duplicates purged %.2f MB (%d) in files\n", - duplicateMegaBytesPurged, duplicateCountPurged); - - printf ("\nWarning, duplicate files were detected - END:\n"); - } - } - } - catch (const megaglest_runtime_error & ex) - { - techtree_errors = true; - printf - ("\n\n****ERROR**** detected while validating the techName: %s\nMESSAGE: %s\n", - techName.c_str (), ex.what ()); - } - - if (techtree_errors == false) - { - printf - ("\nValidation found NO ERRORS for techPath [%s] techName [%s] factions checked (count = %d):\n", - techPath.c_str (), techName.c_str (), (int) factions.size ()); - for (set < string >::iterator it = factions.begin (); - it != factions.end (); ++it) - { - printf ("Faction [%s]\n", (*it).c_str ()); - } - } - } - printf - ("----------------------------------------------------------------"); - } - else if (folderExists (techTreeFolder) == true) - { - printf - ("\nWarning, No factions were found for the techtree located in: [%s]\n", - techTreeFolder.c_str ()); - } - } - - - void - runTechTranslationExtractionForPath (string techPath, string techName, - const std::vector < string > - &filteredFactionList, World & world) - { - vector < string > factionsList; - findDirs (techPath + techName + "/factions/", factionsList, false, - false); - - if (factionsList.empty () == false) - { - Checksum - checksum; - set < string > factions; - for (int j = 0; j < (int) factionsList.size (); ++j) - { - if (filteredFactionList.empty () == true || - std::find (filteredFactionList.begin (), - filteredFactionList.end (), - factionsList[j]) != filteredFactionList.end ()) - { - factions.insert (factionsList[j]); - } - } - - printf - ("\n----------------------------------------------------------------"); - printf - ("\nChecking techPath [%s] techName [%s] total faction count = %d\n", - techPath.c_str (), techName.c_str (), (int) factionsList.size ()); - for (int j = 0; j < (int) factionsList.size (); ++j) - { - if (filteredFactionList.empty () == true || - std::find (filteredFactionList.begin (), - filteredFactionList.end (), - factionsList[j]) != filteredFactionList.end ()) - { - printf ("Using faction [%s]\n", factionsList[j].c_str ()); - } - } - - if (factions.empty () == false) - { - std::map < string, vector < pair < string, - string > > >loadedFileList; - vector < string > pathList; - pathList.push_back (techPath); - Config & config = Config::getInstance (); - vector < string > otherTechPaths = - config.getPathListForType (ptTechs, ""); - pathList.insert (pathList.end (), otherTechPaths.begin (), - otherTechPaths.end ()); - - try - { - world.loadTech (pathList, techName, factions, &checksum, - loadedFileList, true); - - const TechTree * - techtree = world.getTechTree (); - string - translationFile = techtree->getPath (); - endPathWithSlash (translationFile); - translationFile += "lang/" + techName + "_default.lng"; - if (fileExists (translationFile) == false) - { - string - txFilePath = extractDirectoryPathFromFile (translationFile); - createDirectoryPaths (txFilePath); + string + debugLogFile = config.getString("DebugLogFile", ""); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + debugLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugLogFile; + } else { + debugLogFile = userData + debugLogFile; + } + + string + debugWorldSynchLogFile = + config.getString("DebugLogFileWorldSynch", ""); + if (debugWorldSynchLogFile == "") { + debugWorldSynchLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugWorldSynchLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugWorldSynchLogFile; + } else { + debugWorldSynchLogFile = userData + debugWorldSynchLogFile; + } + + string + debugPerformanceLogFile = + config.getString("DebugLogFilePerformance", ""); + if (debugPerformanceLogFile == "") { + debugPerformanceLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugPerformanceLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugPerformanceLogFile; + } else { + debugPerformanceLogFile = userData + debugPerformanceLogFile; + } + + string + debugNetworkLogFile = config.getString("DebugLogFileNetwork", ""); + if (debugNetworkLogFile == "") { + debugNetworkLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugNetworkLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugNetworkLogFile; + } else { + debugNetworkLogFile = userData + debugNetworkLogFile; + } + + string + debugUnitCommandsLogFile = + config.getString("DebugLogFileUnitCommands", ""); + if (debugUnitCommandsLogFile == "") { + debugUnitCommandsLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugUnitCommandsLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugUnitCommandsLogFile; + } else { + debugUnitCommandsLogFile = userData + debugUnitCommandsLogFile; + } + + string + debugPathFinderLogFile = + config.getString("DebugLogFilePathFinder", ""); + if (debugPathFinderLogFile == "") { + debugPathFinderLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugPathFinderLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugPathFinderLogFile; + } else { + debugPathFinderLogFile = userData + debugPathFinderLogFile; + } + + string + debugLUALogFile = config.getString("DebugLogFileLUA", ""); + if (debugLUALogFile == "") { + debugLUALogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugLUALogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugLUALogFile; + } else { + debugLUALogFile = userData + debugLUALogFile; + } + + string + debugSoundLogFile = config.getString("DebugLogFileSound", ""); + if (debugSoundLogFile == "") { + debugSoundLogFile = debugLogFile; + } else if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + debugSoundLogFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + debugSoundLogFile; + } else { + debugSoundLogFile = userData + debugSoundLogFile; + } + + string + debugErrorLogFile = config.getString("DebugLogFileError", ""); + + SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + debugLogFileName = debugLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + debugLogFileName = debugNetworkLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugPerformance). + debugLogFileName = debugPerformanceLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + debugLogFileName = debugWorldSynchLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugUnitCommands). + debugLogFileName = debugUnitCommandsLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugPathFinder). + debugLogFileName = debugPathFinderLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + debugLogFileName = debugLUALogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugSound). + debugLogFileName = debugSoundLogFile; + SystemFlags::getSystemSettingType(SystemFlags::debugError). + debugLogFileName = debugErrorLogFile; + + if (haveSpecialOutputCommandLineOption == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("--- Startup log settings are ---\ndebugSystem [%d][%s]\ndebugNetwork [%d][%s]\ndebugPerformance [%d][%s]\ndebugWorldSynch [%d][%s]\ndebugUnitCommands[%d][%s]\ndebugPathFinder[%d][%s]\ndebugLUA [%d][%s]\ndebugSound [%d][%s]\ndebugError [%d][%s]\n", + SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled, debugLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + enabled, debugNetworkLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags:: + debugPerformance).enabled, + debugPerformanceLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled, debugWorldSynchLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags:: + debugUnitCommands).enabled, + debugUnitCommandsLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugPathFinder). + enabled, debugPathFinderLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled, debugLUALogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugSound). + enabled, debugSoundLogFile.c_str(), + SystemFlags::getSystemSettingType(SystemFlags::debugError). + enabled, debugErrorLogFile.c_str()); + } + } + + void + runTilesetValidationForPath(string tilesetPath, string tilesetName, + World & world, bool purgeUnusedFiles, + bool purgeDuplicateFiles, + bool showDuplicateFiles, bool gitPurgeFiles, + double &purgedMegaBytes) { + Checksum + checksum; + + //bool techtree_errors = false; + + std::map < string, vector < pair < string, string > > >loadedFileList; + vector < string > pathList; + pathList.push_back(tilesetPath); + world.loadTileset(pathList, tilesetName, &checksum, loadedFileList); + + // Fixup paths with .. + { + std::map < string, vector < pair < string, + string > > >newLoadedFileList; + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = loadedFileList.begin(); + iterMap != loadedFileList.end(); ++iterMap) { + string + loadedFile = iterMap->first; + + replaceAll(loadedFile, "//", "/"); + replaceAll(loadedFile, "\\\\", "\\"); + updatePathClimbingParts(loadedFile); + + if (newLoadedFileList.find(loadedFile) != newLoadedFileList.end()) { + for (unsigned int xx1 = 0; xx1 < iterMap->second.size(); ++xx1) { + pair < string, string > &newVal = iterMap->second[xx1]; + replaceAll(newVal.first, "//", "/"); + replaceAll(newVal.first, "\\\\", "\\"); + updatePathClimbingParts(newVal.first); + replaceAll(newVal.second, "//", "/"); + replaceAll(newVal.second, "\\\\", "\\"); + updatePathClimbingParts(newVal.second); + + newLoadedFileList[loadedFile].push_back(newVal); + } + } else { + for (unsigned int xx1 = 0; xx1 < iterMap->second.size(); ++xx1) { + pair < string, string > &newVal = iterMap->second[xx1]; + replaceAll(newVal.first, "//", "/"); + replaceAll(newVal.first, "\\\\", "\\"); + updatePathClimbingParts(newVal.first); + replaceAll(newVal.second, "//", "/"); + replaceAll(newVal.second, "\\\\", "\\"); + updatePathClimbingParts(newVal.second); + } + + newLoadedFileList[loadedFile] = iterMap->second; + } + } + loadedFileList = newLoadedFileList; + } + + // Validate the faction setup to ensure we don't have any bad associations + // std::vector resultErrors = world.validateFactionTypes(); + + // Now check for unused files in the techtree + std::map < string, vector < pair < string, string > > >foundFileList; + for (unsigned int i = 0; i < pathList.size(); ++i) { + string + path = pathList[i]; + endPathWithSlash(path); + path = path + tilesetName + "/"; + + replaceAll(path, "//", "/"); + replaceAll(path, "\\\\", "\\"); + + vector < string > foundFiles = + getFolderTreeContentsListRecursively(path + "*.", ""); + for (unsigned int j = 0; j < foundFiles.size(); ++j) { + string + file = foundFiles[j]; + replaceAll(file, "//", "/"); + replaceAll(file, "\\\\", "\\"); + + replaceAll(file, "//", "/"); + replaceAll(file, "\\\\", "\\"); + + foundFileList[file].push_back(make_pair(path, path)); + } + } + + printf("Found tileset filecount = " MG_SIZE_T_SPECIFIER ", used = " + MG_SIZE_T_SPECIFIER "\n", foundFileList.size(), + loadedFileList.size()); + + int + purgeCount = 0; + bool + foundUnusedFile = false; + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = foundFileList.begin(); + iterMap != foundFileList.end(); ++iterMap) { + string + foundFile = iterMap->first; + replaceAll(foundFile, "//", "/"); + replaceAll(foundFile, "\\\\", "\\"); + + if (loadedFileList.find(foundFile) == loadedFileList.end() && + foundFile.find("lang/") == foundFile.npos) { + if (foundUnusedFile == false) { + printf + ("\nLine ref: %d, Warning, unused files were detected - START:\n=====================\n", + __LINE__); + } + foundUnusedFile = true; + + printf("[%s]\n", foundFile.c_str()); + + string + fileName = extractFileFromDirectoryPath(foundFile); + if (loadedFileList.find(fileName) != loadedFileList.end()) { + printf("possible match on [%s] ?\n", + loadedFileList.find(fileName)->first.c_str()); + } else if (purgeUnusedFiles == true) { + off_t + fileSize = getFileSize(foundFile); + // convert to MB + purgedMegaBytes += ((double) fileSize / 1048576.0); + purgeCount++; + + if (gitPurgeFiles == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", foundFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error("Call to command failed [" + + string(szBuf) + "]"); + } + } else { + removeFile(foundFile); + } + } + } + } + if (foundUnusedFile == true) { + if (purgedMegaBytes > 0) { + printf("Purged %.2f MB (%d) in files\n", purgedMegaBytes, + purgeCount); + } + printf + ("\nLine ref: %d, Warning, unused files were detected - END:\n", + __LINE__); + } + + if (showDuplicateFiles == true) { + std::map < uint32, vector < string > >mapDuplicateFiles; + // Now check for duplicate data content + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = loadedFileList.begin(); + iterMap != loadedFileList.end(); ++iterMap) { + string + fileName = iterMap->first; + Checksum + checksum; + checksum.addFile(fileName); + uint32 + crcValue = checksum.getSum(); + mapDuplicateFiles[crcValue].push_back(fileName); + } + + double + duplicateMegaBytesPurged = 0; + int + duplicateCountPurged = 0; + + double + duplicateMegaBytes = 0; + int + duplicateCount = 0; + + bool + foundDuplicates = false; + for (std::map < uint32, vector < string > >::iterator iterMap = + mapDuplicateFiles.begin(); iterMap != mapDuplicateFiles.end(); + ++iterMap) { + vector < string > &fileList = iterMap->second; + if (fileList.size() > 1) { + if (foundDuplicates == false) { + foundDuplicates = true; + printf + ("\nWarning, duplicate files were detected - START:\n=====================\n"); + } + + map < string, int > + parentList; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + if (idx > 0) { + off_t + fileSize = getFileSize(duplicateFile); + // convert to MB + duplicateMegaBytes += ((double) fileSize / 1048576.0); + duplicateCount++; + } else { + printf("\n"); + } + + printf("[%s]\n", duplicateFile.c_str()); + std::map < string, vector < pair < string, + string > > >::iterator iterFind = + loadedFileList.find(duplicateFile); + if (iterFind != loadedFileList.end()) { + for (unsigned int jdx = 0; jdx < iterFind->second.size(); + jdx++) { + parentList[iterFind->second[jdx].first]++; + } + } + } + + for (map < string, int >::iterator iterMap1 = parentList.begin(); + iterMap1 != parentList.end(); ++iterMap1) { + + if (iterMap1 == parentList.begin()) { + printf("\tParents:\n"); + } + printf("\t[%s]\n", iterMap1->first.c_str()); + } + + if (purgeDuplicateFiles == true) { + + string + newCommonFileName = ""; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + off_t + fileSize = getFileSize(duplicateFile); + if (idx == 0) { + newCommonFileName = + "$COMMONDATAPATH/sounds/" + + extractFileFromDirectoryPath(duplicateFile); + + string + expandedNewCommonFileName = newCommonFileName; + + std::map < string, string > mapExtraTagReplacementValues; + + mapExtraTagReplacementValues = + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues); + Properties::applyTagsToValue(expandedNewCommonFileName, + &mapExtraTagReplacementValues); + replaceAll(expandedNewCommonFileName, "//", "/"); + createDirectoryPaths(extractDirectoryPathFromFile + (expandedNewCommonFileName)); + + if (gitPurgeFiles == true) { + copyFileTo(duplicateFile, expandedNewCommonFileName); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", + duplicateFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error("Call to command failed [" + + string(szBuf) + "]"); + } + printf + ("*** Duplicate file:\n[%s]\nwas git rm and copied to:\n[%s]\n", + duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + } else { + //int result = 0; + int + result = rename(duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + if (result != 0) { + char + szBuf[8096] = ""; + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "!!! Error [%s] Could not rename [%s] to [%s]!", + errmsg, duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + throw + megaglest_runtime_error(szBuf); + } else { + printf + ("*** Duplicate file:\n[%s]\nwas renamed to:\n[%s]\n", + duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + } + } + } else { + if (gitPurgeFiles == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", + duplicateFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error("Call to command failed [" + + string(szBuf) + "]"); + } + printf("*** Duplicate file:\n[%s]\nwas git rm\n", + duplicateFile.c_str()); + } else { + removeFile(duplicateFile); + } + printf("*** Duplicate file:\n[%s]\nwas removed\n", + duplicateFile.c_str()); + + // convert to MB + duplicateMegaBytesPurged += + ((double) fileSize / 1048576.0); + duplicateCountPurged++; + } + } + } + + std::map < string, int > + mapUniqueParentList; + + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + std::map < string, vector < pair < string, + string > > >::iterator iterFind2 = + loadedFileList.find(duplicateFile); + if (iterFind2 != loadedFileList.end()) { + for (unsigned int jdx1 = 0; + jdx1 < iterFind2->second.size(); jdx1++) { + string + parentFile = iterFind2->second[jdx1].first; + string + searchText = iterFind2->second[jdx1].second; + + if (mapUniqueParentList.find(parentFile) == + mapUniqueParentList.end()) { + printf + ("*** Searching parent file:\n[%s]\nfor duplicate file reference:\n[%s]\nto replace with newname:\n[%s]\n", + parentFile.c_str(), searchText.c_str(), + newCommonFileName.c_str()); + bool + foundText = + searchAndReplaceTextInFile(parentFile, searchText, + newCommonFileName, + false); + printf("foundText = %d\n", foundText); + if (foundText == false) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref = %d, Error finding text [%s] in file [%s]", + __LINE__, searchText.c_str(), + parentFile.c_str()); + throw + megaglest_runtime_error(szBuf); + } + mapUniqueParentList[parentFile]++; + } + } + } + } + } + } else { + string + newCommonFileName = ""; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + //off_t fileSize = getFileSize(duplicateFile); + if (idx == 0) { + newCommonFileName = + "$COMMONDATAPATH/sounds/" + + extractFileFromDirectoryPath(duplicateFile); + break; + } + } + } + + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + std::map < string, vector < pair < string, + string > > >::iterator iterFind4 = + loadedFileList.find(duplicateFile); + if (iterFind4 != loadedFileList.end()) { + for (unsigned int jdx = 0; + jdx < iterFind4->second.size(); jdx++) { + string + parentFile = iterFind4->second[jdx].first; + string + searchText = iterFind4->second[jdx].second; + + bool + foundText = + searchAndReplaceTextInFile(parentFile, searchText, + newCommonFileName, true); + if (foundText == false) { + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", + __LINE__, searchText.c_str(), + parentFile.c_str(), + newCommonFileName.c_str()); + printf + ("\n\n=================================================\n%s", + szBuf); + + throw + megaglest_runtime_error(szBuf); + } + } + } + } + } + } + } + } + if (foundDuplicates == true) { + printf("Duplicates %.2f MB (%d) in files\n", duplicateMegaBytes, + duplicateCount); + printf("Duplicates purged %.2f MB (%d) in files\n", + duplicateMegaBytesPurged, duplicateCountPurged); + + printf("\nWarning, duplicate files were detected - END:\n"); + } + } + + //if(techtree_errors == false) { + printf + ("\nValidation found NO ERRORS for tilesetPath [%s] tilesetName [%s]:\n", + tilesetPath.c_str(), tilesetName.c_str()); + //} + + printf + ("----------------------------------------------------------------"); + } + + void + runTechValidationForPath(string techPath, string techName, + const std::vector < string > + &filteredFactionList, World & world, + bool purgeUnusedFiles, bool purgeDuplicateFiles, + bool showDuplicateFiles, bool gitPurgeFiles, + double &purgedMegaBytes) { + + string + techTreeFolder = techPath + techName; + string + techTreeFactionFolder = techTreeFolder + "/factions/"; + vector < string > factionsList; + findDirs(techTreeFactionFolder, factionsList, false, false); + + if (factionsList.empty() == false) { + Checksum + checksum; + set < string > factions; + for (int j = 0; j < (int) factionsList.size(); ++j) { + if (filteredFactionList.empty() == true || + std::find(filteredFactionList.begin(), + filteredFactionList.end(), + factionsList[j]) != filteredFactionList.end()) { + factions.insert(factionsList[j]); + } + } + + printf + ("\n----------------------------------------------------------------"); + printf + ("\nChecking techPath [%s] techName [%s] total faction count = %d\n", + techPath.c_str(), techName.c_str(), (int) factionsList.size()); + for (int j = 0; j < (int) factionsList.size(); ++j) { + if (filteredFactionList.empty() == true || + std::find(filteredFactionList.begin(), + filteredFactionList.end(), + factionsList[j]) != filteredFactionList.end()) { + printf("Using faction [%s]\n", factionsList[j].c_str()); + } + } + + if (factions.empty() == false) { + bool + techtree_errors = false; + + std::map < string, vector < pair < string, + string > > >loadedFileList; + vector < string > pathList; + pathList.push_back(techPath); + Config & config = Config::getInstance(); + vector < string > otherTechPaths = + config.getPathListForType(ptTechs, ""); + pathList.insert(pathList.end(), otherTechPaths.begin(), + otherTechPaths.end()); + + try { + world.loadTech(pathList, techName, factions, &checksum, + loadedFileList, true); + + // Fixup paths with .. + { + std::map < string, vector < pair < string, + string > > >newLoadedFileList; + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = loadedFileList.begin(); + iterMap != loadedFileList.end(); ++iterMap) { + string + loadedFile = iterMap->first; + + replaceAll(loadedFile, "//", "/"); + replaceAll(loadedFile, "\\\\", "\\"); + updatePathClimbingParts(loadedFile); + + if (newLoadedFileList.find(loadedFile) != + newLoadedFileList.end()) { + for (unsigned int xx1 = 0; xx1 < iterMap->second.size(); + ++xx1) { + pair < string, string > &newVal = iterMap->second[xx1]; + replaceAll(newVal.first, "//", "/"); + replaceAll(newVal.first, "\\\\", "\\"); + updatePathClimbingParts(newVal.first); + replaceAll(newVal.second, "//", "/"); + replaceAll(newVal.second, "\\\\", "\\"); + updatePathClimbingParts(newVal.second); + + newLoadedFileList[loadedFile].push_back(newVal); + } + } else { + for (unsigned int xx1 = 0; xx1 < iterMap->second.size(); + ++xx1) { + pair < string, string > &newVal = iterMap->second[xx1]; + replaceAll(newVal.first, "//", "/"); + replaceAll(newVal.first, "\\\\", "\\"); + updatePathClimbingParts(newVal.first); + replaceAll(newVal.second, "//", "/"); + replaceAll(newVal.second, "\\\\", "\\"); + updatePathClimbingParts(newVal.second); + } + + newLoadedFileList[loadedFile] = iterMap->second; + } + } + loadedFileList = newLoadedFileList; + } + + // Validate the faction setup to ensure we don't have any bad associations + std::vector < std::string > resultErrors = + world.validateFactionTypes(); + if (resultErrors.empty() == false) { + techtree_errors = true; + // Display the validation errors + string + errorText = + "\nErrors were detected:\n=====================\n"; + for (int i = 0; i < (int) resultErrors.size(); ++i) { + if (i > 0) { + errorText += "\n"; + } + errorText = errorText + resultErrors[i]; + } + errorText += "\n=====================\n"; + printf("%s", errorText.c_str()); + } + + // Validate the faction resource setup to ensure we don't have any bad associations + printf("\nChecking resources, count = %d\n", + world.getTechTree()->getResourceTypeCount()); + + for (int i = 0; i < world.getTechTree()->getResourceTypeCount(); + ++i) { + printf("Found techtree resource [%s]\n", + world.getTechTree()->getResourceType(i)->getName(). + c_str()); + } + + resultErrors = world.validateResourceTypes(); + if (resultErrors.empty() == false) { + techtree_errors = true; + // Display the validation errors + string + errorText = + "\nErrors were detected:\n=====================\n"; + for (int i = 0; i < (int) resultErrors.size(); ++i) { + if (i > 0) { + errorText += "\n"; + } + errorText = errorText + resultErrors[i]; + } + errorText += "\n=====================\n"; + printf("%s", errorText.c_str()); + } + + // Now check for unused files in the techtree + std::map < string, vector < pair < string, + string > > >foundFileList; + for (unsigned int i = 0; i < pathList.size(); ++i) { + string + path = pathList[i]; + endPathWithSlash(path); + path = path + techName + "/"; + + replaceAll(path, "//", "/"); + replaceAll(path, "\\\\", "\\"); + + vector < string > foundFiles = + getFolderTreeContentsListRecursively(path + "*.", ""); + for (unsigned int j = 0; j < foundFiles.size(); ++j) { + string + file = foundFiles[j]; + replaceAll(file, "//", "/"); + replaceAll(file, "\\\\", "\\"); + + // ignore loading screen, preview screen and hud + if (file.find(GameConstants::LOADING_SCREEN_FILE) != + string::npos + || file.find(GameConstants::PREVIEW_SCREEN_FILE) != + string::npos + || file.find(GameConstants::HUD_SCREEN_FILE) != + string::npos) { + continue; + } + + // ignore commondata since we are not loading all factions + if (filteredFactionList.size() > 0) { + if (file.find("/commondata/") != string::npos) { + continue; + } + } + + if (file.find("/factions/") != string::npos) { + bool + includeFaction = false; + for (set < string >::iterator it = factions.begin(); + it != factions.end(); ++it) { + string + currentFaction = *it; + if (file.find("/factions/" + currentFaction) != + string::npos) { + includeFaction = true; + break; + } + } + if (includeFaction == false) { + continue; + } + } + + replaceAll(file, "//", "/"); + replaceAll(file, "\\\\", "\\"); + + foundFileList[file].push_back(make_pair(path, path)); + } + } + + printf("Found techtree filecount = " MG_SIZE_T_SPECIFIER + ", used = " MG_SIZE_T_SPECIFIER "\n", + foundFileList.size(), loadedFileList.size()); + + int + purgeCount = 0; + bool + foundUnusedFile = false; + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = foundFileList.begin(); + iterMap != foundFileList.end(); ++iterMap) { + string + foundFile = iterMap->first; + replaceAll(foundFile, "//", "/"); + replaceAll(foundFile, "\\\\", "\\"); + + if (loadedFileList.find(foundFile) == loadedFileList.end() && + foundFile.find("lang/") == foundFile.npos) { + if (foundUnusedFile == false) { + printf + ("\nLine ref: %d, Warning, unused files were detected - START:\n=====================\n", + __LINE__); + } + foundUnusedFile = true; + + printf("[%s]\n", foundFile.c_str()); + + string + fileName = extractFileFromDirectoryPath(foundFile); + if (loadedFileList.find(fileName) != loadedFileList.end()) { + printf("possible match on [%s] ?\n", + loadedFileList.find(fileName)->first.c_str()); + } else if (purgeUnusedFiles == true) { + off_t + fileSize = getFileSize(foundFile); + // convert to MB + purgedMegaBytes += ((double) fileSize / 1048576.0); + purgeCount++; + + if (gitPurgeFiles == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", + foundFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error("Call to command failed [" + + string(szBuf) + "]"); + } + } else { + removeFile(foundFile); + } + } + } + } + if (foundUnusedFile == true) { + if (purgedMegaBytes > 0) { + printf("Purged %.2f MB (%d) in files\n", purgedMegaBytes, + purgeCount); + } + printf + ("\nLine ref: %d, Warning, unused files were detected - END:\n", + __LINE__); + } + + if (showDuplicateFiles == true) { + std::map < uint32, vector < string > >mapDuplicateFiles; + // Now check for duplicate data content + for (std::map < string, vector < pair < string, + string > > >::iterator iterMap = loadedFileList.begin(); + iterMap != loadedFileList.end(); ++iterMap) { + string + fileName = iterMap->first; + Checksum + checksum; + checksum.addFile(fileName); + uint32 + crcValue = checksum.getSum(); + if (crcValue == 0) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Error calculating CRC for file [%s]", + fileName.c_str()); + throw + megaglest_runtime_error(szBuf); + } + mapDuplicateFiles[crcValue].push_back(fileName); + } + + double + duplicateMegaBytesPurged = 0; + int + duplicateCountPurged = 0; + + double + duplicateMegaBytes = 0; + int + duplicateCount = 0; + + bool + foundDuplicates = false; + for (std::map < uint32, vector < string > >::iterator iterMap = + mapDuplicateFiles.begin(); + iterMap != mapDuplicateFiles.end(); ++iterMap) { + vector < string > &fileList = iterMap->second; + if (fileList.size() > 1) { + if (foundDuplicates == false) { + foundDuplicates = true; + printf + ("\nWarning, duplicate files were detected - START:\n=====================\n"); + } + + printf("----- START duplicate files for CRC [%u] count [" + MG_SIZE_T_SPECIFIER "] first file is [%s]\n", + iterMap->first, fileList.size(), + fileList[0].c_str()); + + map < string, int > + parentList; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + if (idx > 0) { + off_t + fileSize = getFileSize(duplicateFile); + // convert to MB + duplicateMegaBytes += ((double) fileSize / 1048576.0); + duplicateCount++; + } else { + printf("\n"); + } + + printf("[%s]\n", duplicateFile.c_str()); + std::map < string, vector < pair < string, + string > > >::iterator iterFind = + loadedFileList.find(duplicateFile); + if (iterFind != loadedFileList.end()) { + for (unsigned int jdx = 0; + jdx < iterFind->second.size(); jdx++) { + parentList[iterFind->second[jdx].first]++; + } + } + + string + newCommonFileName = + "$COMMONDATAPATH/sounds/" + + extractFileFromDirectoryPath(duplicateFile); + string + expandedNewCommonFileName = newCommonFileName; + std::map < string, string > mapExtraTagReplacementValues; + string + techCommonData = techPath + techName + "/commondata/"; + replaceAll(techCommonData, "//", "/"); + mapExtraTagReplacementValues["$COMMONDATAPATH"] = + techCommonData; + mapExtraTagReplacementValues = + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues); + Properties::applyTagsToValue(expandedNewCommonFileName, + &mapExtraTagReplacementValues); + replaceAll(expandedNewCommonFileName, "//", "/"); + } + + printf("----- Finding parents for duplicate files [" + MG_SIZE_T_SPECIFIER "] first file is [%s]\n", + fileList.size(), fileList[0].c_str()); + + for (map < string, int >::iterator iterMap1 = + parentList.begin(); iterMap1 != parentList.end(); + ++iterMap1) { + + if (iterMap1 == parentList.begin()) { + printf("\tParents:\n"); + } + printf("\t[%s]\n", iterMap1->first.c_str()); + } + + if (purgeDuplicateFiles == true) { + + printf("----- move / remove duplicate files [" + MG_SIZE_T_SPECIFIER "] first file is [%s]\n", + fileList.size(), fileList[0].c_str()); + // First move first duplicate to commondata and delete all other copies + string + newCommonFileName = ""; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + off_t + fileSize = getFileSize(duplicateFile); + + printf("#1 [%u / " MG_SIZE_T_SPECIFIER + "] removing duplicate [%s]\n", idx, + fileList.size(), duplicateFile.c_str()); + + if (idx == 0) { + newCommonFileName = + "$COMMONDATAPATH/sounds/" + + extractFileFromDirectoryPath(duplicateFile); + + string + expandedNewCommonFileName = newCommonFileName; + + std::map < string, + string > mapExtraTagReplacementValues; + + string + techCommonData = + techPath + techName + "/commondata/"; + replaceAll(techCommonData, "//", "/"); + + mapExtraTagReplacementValues["$COMMONDATAPATH"] = + techCommonData; + mapExtraTagReplacementValues = + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues); + Properties::applyTagsToValue + (expandedNewCommonFileName, + &mapExtraTagReplacementValues); + replaceAll(expandedNewCommonFileName, "//", "/"); + createDirectoryPaths(extractDirectoryPathFromFile + (expandedNewCommonFileName)); + + if (gitPurgeFiles == true) { + copyFileTo(duplicateFile, + expandedNewCommonFileName); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", + duplicateFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error + ("Call to command failed [" + string(szBuf) + + "]"); + } + printf + ("*** Duplicate file:\n[%s]\nwas git rm and copied to:\n[%s]\n", + duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + } else { + printf + ("moving duplicate [%s] to common data [%s] expanded to [%s]\n", + duplicateFile.c_str(), + newCommonFileName.c_str(), + expandedNewCommonFileName.c_str()); + + int + result = rename(duplicateFile.c_str(), + expandedNewCommonFileName.c_str + ()); + if (result != 0) { + char + szBuf[8096] = ""; + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "!!! Error [%s] Could not rename [%s] to [%s]!", + errmsg, duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + throw + megaglest_runtime_error(szBuf); + } else { + printf + ("*** Duplicate file:\n[%s]\nwas renamed to:\n[%s]\n", + duplicateFile.c_str(), + expandedNewCommonFileName.c_str()); + } + } + } else { + if (gitPurgeFiles == true) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "git rm \"%s\"", + duplicateFile.c_str()); + bool + gitOk = executeShellCommand(szBuf, 0); + if (gitOk == false) { + throw + megaglest_runtime_error + ("Call to command failed [" + string(szBuf) + + "]"); + } + printf("*** Duplicate file:\n[%s]\nwas git rm\n", + duplicateFile.c_str()); + } else { + printf("removing duplicate [%s]\n", + duplicateFile.c_str()); + removeFile(duplicateFile); + } + printf("*** Duplicate file:\n[%s]\nwas removed\n", + duplicateFile.c_str()); + + // convert to MB + duplicateMegaBytesPurged += + ((double) fileSize / 1048576.0); + duplicateCountPurged++; + } + } + } + + printf("----- update XML files for duplicate files [" + MG_SIZE_T_SPECIFIER "] first file is [%s]\n", + fileList.size(), fileList[0].c_str()); + std::map < string, int > + mapUniqueParentList; + + // Update the XML files to point to the new single copy in commondata + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + std::map < string, vector < pair < string, + string > > >::iterator iterFind2 = + loadedFileList.find(duplicateFile); + if (iterFind2 != loadedFileList.end()) { + for (unsigned int jdx1 = 0; + jdx1 < iterFind2->second.size(); jdx1++) { + string + parentFile = iterFind2->second[jdx1].first; + string + searchText = iterFind2->second[jdx1].second; + + if (mapUniqueParentList.find(parentFile) == + mapUniqueParentList.end()) { + printf + ("*** Searching parent file:\n[%s]\nfor duplicate file reference:\n[%s]\nto replace with newname:\n[%s]\n", + parentFile.c_str(), searchText.c_str(), + newCommonFileName.c_str()); + bool + foundText = + searchAndReplaceTextInFile(parentFile, + searchText, + newCommonFileName, + false); + printf("foundText = %d\n", foundText); + if (foundText == false) { + + string + techCommonData = + techPath + techName + "/commondata/"; + replaceAll(techCommonData, "//", "/"); + + if (StartsWith(searchText, techCommonData) == + true) { + printf + ("WARNING #1 [%d] techCommonData check\n[%s]\n[%s]\n[%s]\n[%s]\n", + foundText, parentFile.c_str(), + techCommonData.c_str(), + searchText.c_str(), + newCommonFileName.c_str()); + + replaceAll(searchText, techCommonData, + "$COMMONDATAPATH/"); + foundText = + searchAndReplaceTextInFile(parentFile, + searchText, + newCommonFileName, + false); + + printf + ("WARNING #2 [%d] techCommonData check\n[%s]\n[%s]\n[%s]\n[%s]\n", + foundText, parentFile.c_str(), + techCommonData.c_str(), + searchText.c_str(), + newCommonFileName.c_str()); + } + if (foundText == false) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", + __LINE__, searchText.c_str(), + parentFile.c_str(), + newCommonFileName.c_str()); + printf + ("\n\n=================================================\n%s", + szBuf); + + throw + megaglest_runtime_error(szBuf); + } + } + mapUniqueParentList[parentFile]++; + } + } + } + } + } + } else { + + string + newCommonFileName = ""; + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + //off_t fileSize = getFileSize(duplicateFile); + if (idx == 0) { + newCommonFileName = + "$COMMONDATAPATH/sounds/" + + extractFileFromDirectoryPath(duplicateFile); + break; + } + } + } + + for (unsigned int idx = 0; idx < fileList.size(); ++idx) { + string + duplicateFile = fileList[idx]; + string + fileExt = extractExtension(duplicateFile); + if (fileExt == "wav" || fileExt == "ogg") { + std::map < string, vector < pair < string, + string > > >::iterator iterFind4 = + loadedFileList.find(duplicateFile); + if (iterFind4 != loadedFileList.end()) { + for (unsigned int jdx = 0; + jdx < iterFind4->second.size(); jdx++) { + string + parentFile = iterFind4->second[jdx].first; + string + searchText = iterFind4->second[jdx].second; + + bool + foundText = + searchAndReplaceTextInFile(parentFile, + searchText, + newCommonFileName, + true); + + if (foundText == false) { + string + techCommonData = + techPath + techName + "/commondata/"; + replaceAll(techCommonData, "//", "/"); + + if (StartsWith(searchText, techCommonData) == + true) { + replaceAll(searchText, techCommonData, + "$COMMONDATAPATH/"); + foundText = + searchAndReplaceTextInFile(parentFile, + searchText, + newCommonFileName, + true); + + } + if (foundText == false) { + + // Check if the sound file already references commandata + foundText = + searchAndReplaceTextInFile(parentFile, + newCommonFileName, + newCommonFileName, + true); + if (foundText == false) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref = %d, Error finding text\n[%s]\nin file\n[%s]\nnew Common File [%s]\n", + __LINE__, searchText.c_str(), + parentFile.c_str(), + newCommonFileName.c_str()); + printf + ("\n\n=================================================\n%s", + szBuf); + + throw + megaglest_runtime_error(szBuf); + } + } + } + } + } + } + } + } + + + printf("----- END duplicate files [" MG_SIZE_T_SPECIFIER + "] first file is [%s]\n", fileList.size(), + fileList[0].c_str()); + } + } + if (foundDuplicates == true) { + printf("Duplicates %.2f MB (%d) in files\n", + duplicateMegaBytes, duplicateCount); + printf("Duplicates purged %.2f MB (%d) in files\n", + duplicateMegaBytesPurged, duplicateCountPurged); + + printf("\nWarning, duplicate files were detected - END:\n"); + } + } + } catch (const megaglest_runtime_error & ex) { + techtree_errors = true; + printf + ("\n\n****ERROR**** detected while validating the techName: %s\nMESSAGE: %s\n", + techName.c_str(), ex.what()); + } + + if (techtree_errors == false) { + printf + ("\nValidation found NO ERRORS for techPath [%s] techName [%s] factions checked (count = %d):\n", + techPath.c_str(), techName.c_str(), (int) factions.size()); + for (set < string >::iterator it = factions.begin(); + it != factions.end(); ++it) { + printf("Faction [%s]\n", (*it).c_str()); + } + } + } + printf + ("----------------------------------------------------------------"); + } else if (folderExists(techTreeFolder) == true) { + printf + ("\nWarning, No factions were found for the techtree located in: [%s]\n", + techTreeFolder.c_str()); + } + } + + + void + runTechTranslationExtractionForPath(string techPath, string techName, + const std::vector < string > + &filteredFactionList, World & world) { + vector < string > factionsList; + findDirs(techPath + techName + "/factions/", factionsList, false, + false); + + if (factionsList.empty() == false) { + Checksum + checksum; + set < string > factions; + for (int j = 0; j < (int) factionsList.size(); ++j) { + if (filteredFactionList.empty() == true || + std::find(filteredFactionList.begin(), + filteredFactionList.end(), + factionsList[j]) != filteredFactionList.end()) { + factions.insert(factionsList[j]); + } + } + + printf + ("\n----------------------------------------------------------------"); + printf + ("\nChecking techPath [%s] techName [%s] total faction count = %d\n", + techPath.c_str(), techName.c_str(), (int) factionsList.size()); + for (int j = 0; j < (int) factionsList.size(); ++j) { + if (filteredFactionList.empty() == true || + std::find(filteredFactionList.begin(), + filteredFactionList.end(), + factionsList[j]) != filteredFactionList.end()) { + printf("Using faction [%s]\n", factionsList[j].c_str()); + } + } + + if (factions.empty() == false) { + std::map < string, vector < pair < string, + string > > >loadedFileList; + vector < string > pathList; + pathList.push_back(techPath); + Config & config = Config::getInstance(); + vector < string > otherTechPaths = + config.getPathListForType(ptTechs, ""); + pathList.insert(pathList.end(), otherTechPaths.begin(), + otherTechPaths.end()); + + try { + world.loadTech(pathList, techName, factions, &checksum, + loadedFileList, true); + + const TechTree * + techtree = world.getTechTree(); + string + translationFile = techtree->getPath(); + endPathWithSlash(translationFile); + translationFile += "lang/" + techName + "_default.lng"; + if (fileExists(translationFile) == false) { + string + txFilePath = extractDirectoryPathFromFile(translationFile); + createDirectoryPaths(txFilePath); #if defined(WIN32) && !defined(__MINGW32__) - FILE * - fp = _wfopen (utf8_decode (translationFile).c_str (), L"w"); - std::ofstream txFile (fp); + FILE * + fp = _wfopen(utf8_decode(translationFile).c_str(), L"w"); + std::ofstream txFile(fp); #else - std::ofstream txFile; - txFile.open (translationFile.c_str (), - ios_base::out | ios_base::trunc); + std::ofstream txFile; + txFile.open(translationFile.c_str(), + ios_base::out | ios_base::trunc); #endif - if (txFile.is_open () == true) - { - string - _transl_TechTreeName = techName; - replaceAll (_transl_TechTreeName, "_", " "); - txFile << "; TechTree" << std::endl; - txFile << "TechTreeName=" << _transl_TechTreeName << - std::endl; + if (txFile.is_open() == true) { + string + _transl_TechTreeName = techName; + replaceAll(_transl_TechTreeName, "_", " "); + txFile << "; TechTree" << std::endl; + txFile << "TechTreeName=" << _transl_TechTreeName << + std::endl; - txFile << "; -------------------------------------" << - std::endl; - txFile << "; Types of Armor" << std::endl; - for (int index = 0; index < techtree->getArmorTypeCount (); - ++index) - { - const ArmorType * - at = techtree->getArmorTypeByIndex (index); - string - _transl_ArmorTypeName = at->getName (false); - replaceAll (_transl_ArmorTypeName, "_", " "); - txFile << "ArmorTypeName_" << at->getName (false) << "=" << - _transl_ArmorTypeName << std::endl; - } + txFile << "; -------------------------------------" << + std::endl; + txFile << "; Types of Armor" << std::endl; + for (int index = 0; index < techtree->getArmorTypeCount(); + ++index) { + const ArmorType * + at = techtree->getArmorTypeByIndex(index); + string + _transl_ArmorTypeName = at->getName(false); + replaceAll(_transl_ArmorTypeName, "_", " "); + txFile << "ArmorTypeName_" << at->getName(false) << "=" << + _transl_ArmorTypeName << std::endl; + } - txFile << "; --------------------" << std::endl; - txFile << "; Types of Attack" << std::endl; - for (int index = 0; index < techtree->getAttackTypeCount (); - ++index) - { - const AttackType * - at = techtree->getAttackTypeByIndex (index); - string - _transl_AttackTypeName = at->getName (false); - replaceAll (_transl_AttackTypeName, "_", " "); - txFile << "AttackTypeName_" << at->getName (false) << "=" << - _transl_AttackTypeName << std::endl; - } + txFile << "; --------------------" << std::endl; + txFile << "; Types of Attack" << std::endl; + for (int index = 0; index < techtree->getAttackTypeCount(); + ++index) { + const AttackType * + at = techtree->getAttackTypeByIndex(index); + string + _transl_AttackTypeName = at->getName(false); + replaceAll(_transl_AttackTypeName, "_", " "); + txFile << "AttackTypeName_" << at->getName(false) << "=" << + _transl_AttackTypeName << std::endl; + } - txFile << "; --------------------" << std::endl; - txFile << "; Types of Resources" << std::endl; - for (int index = 0; index < techtree->getResourceTypeCount (); - ++index) - { - const ResourceType * - rt = techtree->getResourceType (index); - string - _transl_ResourceTypeName = rt->getName (false); - replaceAll (_transl_ResourceTypeName, "_", " "); - txFile << "ResourceTypeName_" << rt->getName (false) << "=" - << _transl_ResourceTypeName << std::endl; - } + txFile << "; --------------------" << std::endl; + txFile << "; Types of Resources" << std::endl; + for (int index = 0; index < techtree->getResourceTypeCount(); + ++index) { + const ResourceType * + rt = techtree->getResourceType(index); + string + _transl_ResourceTypeName = rt->getName(false); + replaceAll(_transl_ResourceTypeName, "_", " "); + txFile << "ResourceTypeName_" << rt->getName(false) << "=" + << _transl_ResourceTypeName << std::endl; + } -//txFile << "FactionName_" << GameConstants::OBSERVER_SLOTNAME << "=" << GameConstants::OBSERVER_SLOTNAME << std::endl; -//txFile << "FactionName_" << GameConstants::RANDOMFACTION_SLOTNAME << "=" << GameConstants::RANDOMFACTION_SLOTNAME << std::endl; - for (int index = 0; index < techtree->getTypeCount (); - ++index) - { - const FactionType * - ft = techtree->getType (index); - string - _transl_FactionName = ft->getName (false); - replaceAll (_transl_FactionName, "_", " "); - txFile << - "; -----------------------------------------------------------------------------" - << std::endl; - txFile << "; Faction" << std::endl; - txFile << "FactionName_" << ft->getName (false) << "=" << - _transl_FactionName << std::endl; + //txFile << "FactionName_" << GameConstants::OBSERVER_SLOTNAME << "=" << GameConstants::OBSERVER_SLOTNAME << std::endl; + //txFile << "FactionName_" << GameConstants::RANDOMFACTION_SLOTNAME << "=" << GameConstants::RANDOMFACTION_SLOTNAME << std::endl; + for (int index = 0; index < techtree->getTypeCount(); + ++index) { + const FactionType * + ft = techtree->getType(index); + string + _transl_FactionName = ft->getName(false); + replaceAll(_transl_FactionName, "_", " "); + txFile << + "; -----------------------------------------------------------------------------" + << std::endl; + txFile << "; Faction" << std::endl; + txFile << "FactionName_" << ft->getName(false) << "=" << + _transl_FactionName << std::endl; - txFile << "; -------------------------------------" << - std::endl; - txFile << "; Types of Upgrades for this Faction" << - std::endl; - for (int upgradeIndex = 0; - upgradeIndex < ft->getUpgradeTypeCount (); - ++upgradeIndex) - { - const UpgradeType * - upt = ft->getUpgradeType (upgradeIndex); - string - _transl_UpgradeTypeName = upt->getName (false); - replaceAll (_transl_UpgradeTypeName, "_", " "); - txFile << "UpgradeTypeName_" << upt->getName (false) << - "=" << _transl_UpgradeTypeName << std::endl; - } + txFile << "; -------------------------------------" << + std::endl; + txFile << "; Types of Upgrades for this Faction" << + std::endl; + for (int upgradeIndex = 0; + upgradeIndex < ft->getUpgradeTypeCount(); + ++upgradeIndex) { + const UpgradeType * + upt = ft->getUpgradeType(upgradeIndex); + string + _transl_UpgradeTypeName = upt->getName(false); + replaceAll(_transl_UpgradeTypeName, "_", " "); + txFile << "UpgradeTypeName_" << upt->getName(false) << + "=" << _transl_UpgradeTypeName << std::endl; + } - for (int unitIndex = 0; unitIndex < ft->getUnitTypeCount (); - ++unitIndex) - { - const UnitType * - ut = ft->getUnitType (unitIndex); - string - _transl_UnitTypeName = ut->getName (false); - replaceAll (_transl_UnitTypeName, "_", " "); - txFile << "; -------------------------------------" << - std::endl; - txFile << "; Unit" << std::endl; - txFile << "UnitTypeName_" << ut->getName (false) << "=" << - _transl_UnitTypeName << std::endl; + for (int unitIndex = 0; unitIndex < ft->getUnitTypeCount(); + ++unitIndex) { + const UnitType * + ut = ft->getUnitType(unitIndex); + string + _transl_UnitTypeName = ut->getName(false); + replaceAll(_transl_UnitTypeName, "_", " "); + txFile << "; -------------------------------------" << + std::endl; + txFile << "; Unit" << std::endl; + txFile << "UnitTypeName_" << ut->getName(false) << "=" << + _transl_UnitTypeName << std::endl; - txFile << "; --------------------" << std::endl; - txFile << "; Levels for this Unit" << std::endl; - for (int levelIndex = 0; - levelIndex < ut->getLevelCount (); ++levelIndex) - { - const Level * - level = ut->getLevel (levelIndex); - string - _transl_LevelName = level->getName (false); - replaceAll (_transl_LevelName, "_", " "); - txFile << "LevelName_" << level->getName (false) << "=" - << _transl_LevelName << std::endl; - } + txFile << "; --------------------" << std::endl; + txFile << "; Levels for this Unit" << std::endl; + for (int levelIndex = 0; + levelIndex < ut->getLevelCount(); ++levelIndex) { + const Level * + level = ut->getLevel(levelIndex); + string + _transl_LevelName = level->getName(false); + replaceAll(_transl_LevelName, "_", " "); + txFile << "LevelName_" << level->getName(false) << "=" + << _transl_LevelName << std::endl; + } - txFile << "; --------------------" << std::endl; - txFile << "; Types of Commands for this Unit" << - std::endl; - for (int commandIndex = 0; - commandIndex < ut->getCommandTypeCount (); - ++commandIndex) - { - const CommandType * - ct = ut->getCommandType (commandIndex); - string - _transl_CommandName = ct->getName (false); - replaceAll (_transl_CommandName, "_", " "); - txFile << "CommandName_" << ct->getName (false) << "=" - << _transl_CommandName << std::endl; - } - } - } - txFile << "; -------------------------------------" << - std::endl; - } - txFile.close (); + txFile << "; --------------------" << std::endl; + txFile << "; Types of Commands for this Unit" << + std::endl; + for (int commandIndex = 0; + commandIndex < ut->getCommandTypeCount(); + ++commandIndex) { + const CommandType * + ct = ut->getCommandType(commandIndex); + string + _transl_CommandName = ct->getName(false); + replaceAll(_transl_CommandName, "_", " "); + txFile << "CommandName_" << ct->getName(false) << "=" + << _transl_CommandName << std::endl; + } + } + } + txFile << "; -------------------------------------" << + std::endl; + } + txFile.close(); #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - { - fclose (fp); - } + if (fp) { + fclose(fp); + } #endif - } - else - { - printf - ("\n** Cannot product techtree translation file [%s] for techPath [%s] techName [%s] because the file already exists!\n", - translationFile.c_str (), techPath.c_str (), - techName.c_str ()); - } - } - catch (const megaglest_runtime_error & ex) - { - printf - ("\n\n****ERROR**** detected while loading the techName: %s\nMESSAGE: %s\n", - techName.c_str (), ex.what ()); - } - - } - printf - ("----------------------------------------------------------------"); - } - } - - void - runTechTranslationExtraction (int argc, char **argv) - { - printf ("====== Started Translation Extraction ======\n"); - - Config & config = Config::getInstance (); - -// Did the user pass a specific list of factions to validate? - std::vector < string > filteredFactionList; - - vector < string > results; - findDirs (config.getPathListForType (ptTechs), results); - vector < string > techTreeFiles = results; -// Did the user pass a specific list of techtrees to validate? - std::vector < string > filteredTechTreeList; - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) + string ("=")) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) + - string ("="), &foundParamIndIndex); - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - if (paramPartTokens.size () >= 2) - { - string - techtreeList = paramPartTokens[1]; - Tokenize (techtreeList, filteredTechTreeList, ","); - - if (filteredTechTreeList.empty () == false) - { - printf - ("Filtering techtrees and only looking for the following:\n"); - for (int idx = 0; idx < (int) filteredTechTreeList.size (); ++idx) - { - filteredTechTreeList[idx] = trim (filteredTechTreeList[idx]); - printf ("%s\n", filteredTechTreeList[idx].c_str ()); - } - } - } - } - - { - printf - ("\n---------------- Loading factions inside world ----------------"); - World - world; - - vector < string > techPaths = config.getPathListForType (ptTechs); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - - for (int idx2 = 0; idx2 < (int) techTreeFiles.size (); idx2++) - { - string & techName = techTreeFiles[idx2]; - - if (filteredTechTreeList.empty () == true || - std::find (filteredTechTreeList.begin (), - filteredTechTreeList.end (), - techName) != filteredTechTreeList.end ()) - { - - runTechTranslationExtractionForPath (techPath, techName, - filteredFactionList, - world); - } - } - } - - printf ("\n====== Finished Translation ======\n"); - } - - } - - - void - runTechValidationReport (int argc, char **argv) - { - printf ("====== Started Validation ======\n"); - - bool - purgeDuplicateFiles = false; - bool - showDuplicateFiles = true; - bool - purgeUnusedFiles = false; - bool - gitPurgeFiles = false; - - double - purgedMegaBytes = 0; - Config & config = Config::getInstance (); - -// Did the user pass a specific scenario to validate? - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) + string ("=")) == - true) - { - - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) + - string ("="), &foundParamIndIndex); - - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - - if (paramPartTokens.size () >= 2) - { - string - validateScenarioName = paramPartTokens[1]; - - printf ("Filtering scenario: %s\n", validateScenarioName.c_str ()); - - if (paramPartTokens.size () >= 3) - { - if (paramPartTokens[2] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused scenario files will be deleted!\n"); - } - } - - { - printf - ("\n---------------- Loading scenario inside world ----------------\n"); - - bool - scenarioFound = false; - World - world; - std::vector < string > filteredFactionList; - - vector < string > scenarioPaths = - config.getPathListForType (ptScenarios); - for (int idx = 0; idx < (int) scenarioPaths.size (); idx++) - { - string & scenarioPath = scenarioPaths[idx]; - endPathWithSlash (scenarioPath); - - vector < string > scenarioList; - findDirs (scenarioPath, scenarioList, false, false); - for (int idx2 = 0; idx2 < (int) scenarioList.size (); idx2++) - { - string & scenarioName = scenarioList[idx2]; - - if (scenarioName == validateScenarioName) - { - scenarioFound = true; - - string - file = - scenarioPath + scenarioName + "/" + scenarioName + ".xml"; - - XmlTree - xmlTree; - xmlTree.load (file, Properties::getTagReplacementValues ()); - const XmlNode * - scenarioNode = xmlTree.getRootNode (); - string - techName = - scenarioNode->getChild ("tech-tree")-> - getAttribute ("value")->getValue (); - -// Self Contained techtree? - string - scenarioTechtree = - scenarioPath + scenarioName + "/" + techName + "/" + - techName + ".xml"; - - printf - ("\nFound Scenario [%s] looking for techtree [%s]...\n", - scenarioName.c_str (), scenarioTechtree.c_str ()); - - if (fileExists (scenarioTechtree) == true) - { - string - techPath = scenarioPath + scenarioName + "/"; - - printf - ("\nFound Scenario [%s] with custom techtree [%s] validating...\n", - scenarioName.c_str (), techName.c_str ()); - runTechValidationForPath (techPath, techName, - filteredFactionList, world, - purgeUnusedFiles, - showDuplicateFiles, false, - false, purgedMegaBytes); - } - else - { - vector < string > techPaths = - config.getPathListForType (ptTechs); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - scenarioTechtree = - techPath + "/" + techName + "/" + techName + ".xml"; - if (fileExists (scenarioTechtree) == true) - { - printf - ("\nFound Scenario [%s] with techtree [%s] validating...\n", - scenarioName.c_str (), techName.c_str ()); - runTechValidationForPath (techPath, techName, - filteredFactionList, world, - purgeUnusedFiles, - showDuplicateFiles, false, - false, purgedMegaBytes); - - break; - } - } - } - } - } - } - - if (scenarioFound == false) - { - printf ("\nWARNING, the scenario [%s] was NOT FOUND!\n", - validateScenarioName.c_str ()); - } - printf ("\n====== Finished Validation ======\n"); - } - return; - } - else - { - printf - ("\nInvalid missing scenario specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return; - } - } - -// Did the user pass a specific list of factions to validate? - std::vector < string > filteredFactionList; - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) + string ("=")) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) + - string ("="), &foundParamIndIndex); - - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - - if (paramPartTokens.size () >= 2) - { - string - factionList = paramPartTokens[1]; - Tokenize (factionList, filteredFactionList, ","); - - if (filteredFactionList.empty () == false) - { - printf - ("Filtering factions and only looking for the following:\n"); - for (int idx = 0; idx < (int) filteredFactionList.size (); ++idx) - { - filteredFactionList[idx] = trim (filteredFactionList[idx]); - printf ("%s\n", filteredFactionList[idx].c_str ()); - } - } - - if (paramPartTokens.size () >= 3) - { - if (paramPartTokens[2] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused faction files will be deleted!\n"); - } - } - } - } - vector < string > results; - findDirs (config.getPathListForType (ptTechs), results); - vector < string > techTreeFiles = results; -// Did the user pass a specific list of techtrees to validate? - std::vector < string > filteredTechTreeList; - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) + string ("=")) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) + - string ("="), &foundParamIndIndex); - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - if (paramPartTokens.size () >= 2) - { - string - techtreeList = paramPartTokens[1]; - Tokenize (techtreeList, filteredTechTreeList, ","); - - if (filteredTechTreeList.empty () == false) - { - printf - ("Filtering techtrees and only looking for the following:\n"); - for (int idx = 0; idx < (int) filteredTechTreeList.size (); ++idx) - { - filteredTechTreeList[idx] = trim (filteredTechTreeList[idx]); - printf ("%s\n", filteredTechTreeList[idx].c_str ()); - } - } - - if (paramPartTokens.size () >= 3) - { - if (paramPartTokens[2] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused techtree files will be deleted!\n"); - } - else if (paramPartTokens[2] == "purgeduplicates") - { - purgeDuplicateFiles = true; - printf ("*NOTE All duplicate techtree files will be merged!\n"); - } - else if (paramPartTokens[2] == "gitdelete") - { - gitPurgeFiles = true; - printf - ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); - } - else if (paramPartTokens[2] == "hideduplicates") - { - showDuplicateFiles = false; - printf - ("*NOTE All duplicate techtree files will NOT be shown!\n"); - } - } - if (paramPartTokens.size () >= 4) - { - if (paramPartTokens[3] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused techtree files will be deleted!\n"); - } - else if (paramPartTokens[3] == "purgeduplicates") - { - purgeDuplicateFiles = true; - printf ("*NOTE All duplicate techtree files will be merged!\n"); - } - else if (paramPartTokens[3] == "gitdelete") - { - gitPurgeFiles = true; - printf - ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); - } - else if (paramPartTokens[3] == "hideduplicates") - { - showDuplicateFiles = false; - printf - ("*NOTE All duplicate techtree files will NOT be shown!\n"); - } - } - if (paramPartTokens.size () >= 5) - { - if (paramPartTokens[4] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused techtree files will be deleted!\n"); - } - else if (paramPartTokens[4] == "purgeduplicates") - { - purgeDuplicateFiles = true; - printf ("*NOTE All duplicate techtree files will be merged!\n"); - } - else if (paramPartTokens[4] == "gitdelete") - { - gitPurgeFiles = true; - printf - ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); - } - else if (paramPartTokens[4] == "hideduplicates") - { - showDuplicateFiles = false; - printf - ("*NOTE All duplicate techtree files will NOT be shown!\n"); - } - } - } - } - - { - printf - ("\n---------------- Loading factions inside world ----------------"); - World - world; - - vector < string > techPaths = config.getPathListForType (ptTechs); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - - for (int idx2 = 0; idx2 < (int) techTreeFiles.size (); idx2++) - { - string & techName = techTreeFiles[idx2]; - - if (filteredTechTreeList.empty () == true || - std::find (filteredTechTreeList.begin (), - filteredTechTreeList.end (), - techName) != filteredTechTreeList.end ()) - { - - runTechValidationForPath (techPath, techName, - filteredFactionList, world, - purgeUnusedFiles, purgeDuplicateFiles, - showDuplicateFiles, gitPurgeFiles, - purgedMegaBytes); - } - } - } - - printf ("\n====== Finished Validation ======\n"); - } - - } - - void - runTilesetValidationReport (int argc, char **argv) - { - printf ("====== Started Validation ======\n"); - - Config & config = Config::getInstance (); - -// Did the user pass a specific tileset to validate? - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) + string ("=")) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) + - string ("="), &foundParamIndIndex); - - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - - if (paramPartTokens.size () >= 2) - { - string - validateTilesetName = paramPartTokens[1]; - - printf ("Filtering tileset: %s\n", validateTilesetName.c_str ()); - - bool - purgeUnusedFiles = false; - if (paramPartTokens.size () >= 3) - { - if (paramPartTokens[2] == "purgeunused") - { - purgeUnusedFiles = true; - printf ("*NOTE All unused tileset files will be deleted!\n"); - } - } - - { - printf - ("\n---------------- Loading tileset inside world ----------------\n"); - - World - world; - double - purgedMegaBytes = 0; - bool - showDuplicateFiles = true; - - bool - tilesetFound = false; - - vector < string > tilesetPaths = - config.getPathListForType (ptTilesets); - for (int idx = 0; idx < (int) tilesetPaths.size (); idx++) - { - string & tilesetPath = tilesetPaths[idx]; - endPathWithSlash (tilesetPath); - - vector < string > tilesetList; - findDirs (tilesetPath, tilesetList, false, false); - for (int idx2 = 0; idx2 < (int) tilesetList.size (); idx2++) - { - string & tilesetName = tilesetList[idx2]; - if (tilesetName == validateTilesetName) - { - tilesetFound = true; - runTilesetValidationForPath (tilesetPath, tilesetName, - world, purgeUnusedFiles, - showDuplicateFiles, false, - false, purgedMegaBytes); - } - } - } - - if (tilesetFound == false) - { - printf ("*ERROR The specified tileset [%s] was NOT FOUND!\n", - validateTilesetName.c_str ()); - } - printf ("\n====== Finished Validation ======\n"); - } - return; - } - else - { - printf - ("\nInvalid missing tileset specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return; - } - } - else - { - printf ("\nInvalid missing tileset specified on commandline\n\n"); - return; - } - } - - void - ShowINISettings (int argc, char **argv, Config & config, - Config & configKeys) - { - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == true) - { - vector < string > filteredPropertyList; - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) + string ("=")) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) + - string ("="), &foundParamIndIndex); - string - filterList = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (filterList, paramPartTokens, "="); - if (paramPartTokens.size () >= 2) - { - string - tokenList = paramPartTokens[1]; - Tokenize (tokenList, filteredPropertyList, ","); - - if (filteredPropertyList.empty () == false) - { - printf - ("Filtering properties and only looking for the following:\n"); - for (int idx = 0; idx < (int) filteredPropertyList.size (); - ++idx) - { - filteredPropertyList[idx] = trim (filteredPropertyList[idx]); - printf ("%s\n", filteredPropertyList[idx].c_str ()); - } - } - } - } - - printf ("\nMain settings report\n"); - printf ("====================\n"); - vector < pair < string, string > >mergedMainSettings = - config.getMergedProperties (); - vector < pair < string, string > >mergedKeySettings = - configKeys.getMergedProperties (); - -// Figure out the max # of tabs we need to format display nicely - int - tabCount = 1; - for (int i = 0; i < (int) mergedMainSettings.size (); ++i) - { - const - pair < - string, - string > & - nameValue = mergedMainSettings[i]; - - bool - displayProperty = false; - if (filteredPropertyList.empty () == false) - { - if (find - (filteredPropertyList.begin (), filteredPropertyList.end (), - nameValue.first) != filteredPropertyList.end ()) - { - displayProperty = true; - } - } - else - { - displayProperty = true; - } - - if (displayProperty == true) - { - int - requredTabs = ((int) nameValue.first.length () / 8) + 1; - if (nameValue.first.length () % 8) - { - requredTabs++; - } - if (requredTabs > tabCount) - { - tabCount = requredTabs; - } - } - } - for (int i = 0; i < (int) mergedKeySettings.size (); ++i) - { - const - pair < - string, - string > & - nameValue = mergedKeySettings[i]; - - bool - displayProperty = false; - if (filteredPropertyList.empty () == false) - { - if (find - (filteredPropertyList.begin (), filteredPropertyList.end (), - nameValue.first) != filteredPropertyList.end ()) - { - displayProperty = true; - } - } - else - { - displayProperty = true; - } - - if (displayProperty == true) - { - int - requredTabs = ((int) nameValue.first.length () / 8) + 1; - if (nameValue.first.length () % 8) - { - requredTabs++; - } - if (requredTabs > tabCount) - { - tabCount = requredTabs; - } - } - } - -// Output the properties - for (int i = 0; i < (int) mergedMainSettings.size (); ++i) - { - const - pair < - string, - string > & - nameValue = mergedMainSettings[i]; - - bool - displayProperty = false; - if (filteredPropertyList.empty () == false) - { - if (find - (filteredPropertyList.begin (), filteredPropertyList.end (), - nameValue.first) != filteredPropertyList.end ()) - { - displayProperty = true; - } - } - else - { - displayProperty = true; - } - - if (displayProperty == true) - { - printf ("Property Name [%s]", nameValue.first.c_str ()); - - int - tabs = ((int) nameValue.first.length () / 8) + 1; - for (int j = 0; j < (tabCount - tabs); ++j) - { - printf ("\t"); - } - - string - displayValue = nameValue.second; - if (nameValue.first == "TranslationGetURLPassword") - { - displayValue = "*****"; - } - - printf ("Value [%s]\n", displayValue.c_str ()); - } - } - - printf ("\n\nMain key binding settings report\n"); - printf ("====================================\n"); - - for (int i = 0; i < (int) mergedKeySettings.size (); ++i) - { - const - pair < - string, - string > & - nameValue = mergedKeySettings[i]; - - bool - displayProperty = false; - if (filteredPropertyList.empty () == false) - { - if (find - (filteredPropertyList.begin (), filteredPropertyList.end (), - nameValue.first) != filteredPropertyList.end ()) - { - displayProperty = true; - } - } - else - { - displayProperty = true; - } - - if (displayProperty == true) - { - printf ("Property Name [%s]", nameValue.first.c_str ()); - - int - tabs = ((int) nameValue.first.length () / 8) + 1; - for (int j = 0; j < (tabCount - tabs); ++j) - { - printf ("\t"); - } - - printf ("Value [%s]\n", nameValue.second.c_str ()); - } - } - } - } - - Steam & initSteamInstance () - { - Steam *& - steamInstance = - CacheManager::getCachedItem < - Steam * >(GameConstants::steamCacheInstanceKey); - if (steamInstance == NULL) - { - steamInstance = new Steam (); - } - return *steamInstance; - } - - void - setupSteamSettings (bool steamEnabled, bool steamResetStats, - bool debugEnabled) - { - Config & config = Config::getInstance (); - config.setBool ("SteamEnabled", steamEnabled, true); - if (steamEnabled) - { - printf ("*NOTE: Steam Integration Enabled.\n"); - - if (debugEnabled) - { - printf ("*NOTE: Steam Debugging Enabled.\n"); - } - Steam::setDebugEnabled (debugEnabled); - - Steam & steam = initSteamInstance (); - -// For Debugging purposes: - if (steamResetStats) - { - printf - ("*WARNING: Steam Stats / Achievements are being RESET by request!\n"); - steam.resetStats (true); - } - - string - steamPlayerName = steam.userName (); - string - steamLang = steam.lang (); - printf - ("Steam Integration Enabled!\nSteam User Name is [%s] Language is [%s]\n", - steamPlayerName.c_str (), steamLang.c_str ()); - - bool - needToSaveConfig = false; - string - currentPLayerName = config.getString ("NetPlayerName", ""); - if (currentPLayerName == "newbie" || currentPLayerName == "") - { - config.setString ("NetPlayerName", steamPlayerName); - needToSaveConfig = true; - } - if (needToSaveConfig == true) - { - config.save (); - } - } - } - - void - CheckForDuplicateData () - { - Config & config = Config::getInstance (); - - string - duplicateWarnings = ""; - - try - { - - - { - - string - scenarioDir = ""; - vector < string > pathList = - config.getPathListForType (ptMaps, scenarioDir); - vector < string > invalidMapList; - vector < string > maps = - MapPreview::findAllValidMaps (pathList, scenarioDir, false, true, - &invalidMapList); - std::sort (maps.begin (), maps.end ()); - - if (maps.empty () == true) - { - throw - megaglest_runtime_error ("No maps were found!", true); - } - else if (invalidMapList.empty () == false) - { - string - errorMsg = - "Warning invalid maps were detected (will be ignored):\n"; - for (int i = 0; i < (int) invalidMapList.size (); ++i) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "map [%s]\n", - invalidMapList[i].c_str ()); - - errorMsg += szBuf; - } - duplicateWarnings += errorMsg; - } - - vector < string > duplicateMapsToRename; - for (int i = 0; i < (int) maps.size (); ++i) - { - string - map1 = maps[i]; - for (int j = 0; j < (int) maps.size (); ++j) - { - if (i != j) - { - string - map2 = maps[j]; - - if (map1 == map2) - { - if (std::find (duplicateMapsToRename.begin (), - duplicateMapsToRename.end (), - map1) == duplicateMapsToRename.end ()) - { - duplicateMapsToRename.push_back (map1); - } - } - } - } - } - if (duplicateMapsToRename.empty () == false) - { - string - errorMsg = - "Warning duplicate maps were detected and renamed:\n"; - for (int i = 0; i < (int) duplicateMapsToRename.size (); ++i) - { - string - currentPath = pathList[1]; - endPathWithSlash (currentPath); - - string - oldFile = currentPath + duplicateMapsToRename[i]; - string - newFile = currentPath + duplicateMapsToRename[i]; - string - ext = extractExtension (newFile); - newFile = - newFile.substr (0, newFile.length () - ext.length () - 1); - newFile = newFile + "_custom." + ext; - - char - szBuf[8096] = ""; - int - result = rename (oldFile.c_str (), newFile.c_str ()); - if (result != 0) - { - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "Error [%s]\nCould not rename [%s] to [%s]!", - errmsg, oldFile.c_str (), newFile.c_str ()); - throw - megaglest_runtime_error (szBuf, true); - } - else - { - snprintf (szBuf, 8096, - "map [%s] in [%s]\nwas renamed to [%s]", - duplicateMapsToRename[i].c_str (), oldFile.c_str (), - newFile.c_str ()); - } - errorMsg += szBuf; - } - duplicateWarnings += errorMsg; - } - } - - { -//tilesets - std::vector < std::string > tileSets; - vector < string > tilesetPaths = - config.getPathListForType (ptTilesets); - findDirs (tilesetPaths, tileSets, false, true); - - if (tileSets.empty ()) - { - throw - megaglest_runtime_error ("No tilesets were found!", true); - } - - vector < string > duplicateTilesetsToRename; - for (int i = 0; i < (int) tileSets.size (); ++i) - { - string - tileSet1 = tileSets[i]; - for (int j = 0; j < (int) tileSets.size (); ++j) - { - if (i != j) - { - string - tileSet2 = tileSets[j]; - if (tileSet1 == tileSet2) - { - if (std::find (duplicateTilesetsToRename.begin (), - duplicateTilesetsToRename.end (), - tileSet1) == - duplicateTilesetsToRename.end ()) - { - duplicateTilesetsToRename.push_back (tileSet1); - } - } - } - } - } - if (duplicateTilesetsToRename.empty () == false) - { - string - errorMsg = - "Warning duplicate tilesets were detected and renamed:\n"; - - for (int i = 0; i < (int) duplicateTilesetsToRename.size (); ++i) - { - string - currentPath = tilesetPaths[1]; - endPathWithSlash (currentPath); - - string - oldFile = currentPath + duplicateTilesetsToRename[i]; - string - newFile = currentPath + duplicateTilesetsToRename[i]; - newFile = newFile + "_custom"; - - char - szBuf[8096] = ""; - int - result = rename (oldFile.c_str (), newFile.c_str ()); - if (result != 0) - { - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "Error [%s]\nCould not rename [%s] to [%s]!", - errmsg, oldFile.c_str (), newFile.c_str ()); - throw - megaglest_runtime_error (szBuf, true); - } - else - { - snprintf (szBuf, 8096, - "tileset [%s] in [%s]\nwas renamed to [%s]", - duplicateTilesetsToRename[i].c_str (), - oldFile.c_str (), newFile.c_str ()); - - string - tilesetName = extractFileFromDirectoryPath (oldFile); - oldFile = newFile + "/" + tilesetName + ".xml"; - newFile = newFile + "/" + tilesetName + "_custom.xml"; - - result = rename (oldFile.c_str (), newFile.c_str ()); - - if (result != 0) - { - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "Error [%s]\nCould not rename [%s] to [%s]!", - errmsg, oldFile.c_str (), newFile.c_str ()); - throw - megaglest_runtime_error (szBuf, true); - } - - } - errorMsg += szBuf; - } - duplicateWarnings += errorMsg; - } - } - - { - vector < string > techPaths = config.getPathListForType (ptTechs); - vector < string > techTrees; - findDirs (techPaths, techTrees, false, true); - if (techTrees.empty ()) - { - throw - megaglest_runtime_error ("No tech-trees were found (dup)!", - true); - } - - vector < string > duplicateTechtreesToRename; - for (int i = 0; i < (int) techTrees.size (); ++i) - { - string - techtree1 = techTrees[i]; - for (int j = 0; j < (int) techTrees.size (); ++j) - { - if (i != j) - { - string - techtree2 = techTrees[j]; - if (techtree1 == techtree2) - { - if (std::find (duplicateTechtreesToRename.begin (), - duplicateTechtreesToRename.end (), - techtree1) == - duplicateTechtreesToRename.end ()) - { - duplicateTechtreesToRename.push_back (techtree1); - } - } - } - } - } - if (duplicateTechtreesToRename.empty () == false) - { - string - errorMsg = - "Warning duplicate techtrees were detected and renamed:\n"; - - for (int i = 0; i < (int) duplicateTechtreesToRename.size (); ++i) - { - string - currentPath = techPaths[1]; - endPathWithSlash (currentPath); - - string - oldFile = currentPath + duplicateTechtreesToRename[i]; - string - newFile = currentPath + duplicateTechtreesToRename[i]; - newFile = newFile + "_custom"; - - char - szBuf[8096] = ""; - int - result = rename (oldFile.c_str (), newFile.c_str ()); - if (result != 0) - { - char * - errmsg = strerror (errno); - snprintf (szBuf, 8096, - "Error [%s]\nCould not rename [%s] to [%s]!", - errmsg, oldFile.c_str (), newFile.c_str ()); - throw - megaglest_runtime_error (szBuf, true); - } - else - { - snprintf (szBuf, 8096, - "techtree [%s] in [%s]\nwas renamed to [%s]", - duplicateTechtreesToRename[i].c_str (), - oldFile.c_str (), newFile.c_str ()); - - string - tilesetName = extractFileFromDirectoryPath (oldFile); - oldFile = newFile + "/" + tilesetName + ".xml"; - newFile = newFile + "/" + tilesetName + "_custom.xml"; - - int - rename_result = rename (oldFile.c_str (), newFile.c_str ()); - if (rename_result != 0) - { - printf ("Error renaming [%s] to [%s]\n", oldFile.c_str (), - newFile.c_str ()); - } - } - errorMsg += szBuf; - } - duplicateWarnings += errorMsg; - } - } - - } - catch (const megaglest_runtime_error & ex) - { - if (mainProgram) - { - mainProgram->getState ()->setForceMouseRender (true); - } - ExceptionHandler::DisplayMessage (ex.what (), false); - } - - if (duplicateWarnings != "") - { - if (mainProgram) - { - mainProgram->getState ()->setForceMouseRender (true); - } - ExceptionHandler::DisplayMessage (duplicateWarnings.c_str (), false); - } - } - - int - handleCreateDataArchivesCommand (int argc, char **argv) - { - int - return_value = 1; - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) - + string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS - [GAME_ARG_CREATE_DATA_ARCHIVES]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - compress_item = paramPartTokens[1]; - bool - includeMainData = false; - if (paramPartTokens.size () >= 3 - && paramPartTokens[2] == "include_main") - { - includeMainData = true; - } - - Config & config = Config::getInstance (); - string - fileArchiveExtension = - config.getString ("FileArchiveExtension", ""); - string - fileArchiveCompressCommand = - config.getString ("FileArchiveCompressCommand", ""); - string - fileArchiveCompressCommandParameters = - config.getString ("FileArchiveCompressCommandParameters", ""); - int32 - fileArchiveCompressCommandSuccessResult = - config.getInt ("FileArchiveCompressCommandSuccessResult", "0"); - - string - userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - - int - typesSelected = 0; - if (compress_item == "techtrees" || compress_item == "all") - { - typesSelected++; - - vector < string > pathList = - config.getPathListForType (ptTechs, ""); - vector < string > results; - findDirs (pathList, results); - - printf - ("Techtrees found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - - for (unsigned int j = 0; j < pathList.size (); ++j) - { - string - techPath = pathList[j]; - if (techPath != "") - { - endPathWithSlash (techPath); - } - - vector < string > results2; - findDirs (techPath + name + "/factions", results2, false, - true); - if (results2.empty () == false) - { - string - techtreePath = techPath + name; - if (includeMainData == false) - { - if (techtreePath.find (userData) == techPath.npos) - { - printf ("Skipping techtree: [%s]\n", - techtreePath.c_str ()); - continue; - } - } - - string - downloadArchive = techtreePath + fileArchiveExtension; - - if (fileExists (downloadArchive) == true) - { - bool - removed = removeFile (downloadArchive); - if (removed == false) - { - printf ("Error could not remove old file: [%s]\n", - downloadArchive.c_str ()); - } - } - string - compressCmd = - getFullFileArchiveCompressCommand - (fileArchiveCompressCommand, - fileArchiveCompressCommandParameters, - downloadArchive, techtreePath); - - printf ("Running compression command: %s\n", - compressCmd.c_str ()); - - if (executeShellCommand - (compressCmd, - fileArchiveCompressCommandSuccessResult) == false) - { - printf ("Error could not create new file: [%s]\n", - downloadArchive.c_str ()); - } - - if (fileExists (downloadArchive) == true) - { - off_t - fileSize = getFileSize (downloadArchive); -// convert to MB - double - megaBytes = ((double) fileSize / 1048576.0); - printf ("%s [download archive %.2fMB]\n", name.c_str (), - megaBytes); - } - } - } - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - } - if (compress_item == "tilesets" || compress_item == "all") - { - typesSelected++; - - vector < string > pathList = - config.getPathListForType (ptTilesets, ""); - vector < string > results; - findDirs (pathList, results); - - printf - ("Tilesets found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - - for (unsigned int j = 0; j < pathList.size (); ++j) - { - string - tilesetPath = pathList[j]; - if (tilesetPath != "") - { - endPathWithSlash (tilesetPath); - } - - if (fileExists (tilesetPath + name + "/" + name + ".xml") == - true) - { - string - tilesetDataPath = tilesetPath + name; - if (includeMainData == false) - { - if (tilesetPath.find (userData) == tilesetPath.npos) - { - printf ("Skipping tileset data: [%s]\n", - tilesetDataPath.c_str ()); - continue; - } - } - - string - downloadArchive = tilesetDataPath + fileArchiveExtension; - - if (fileExists (downloadArchive) == true) - { - bool - removed = removeFile (downloadArchive); - if (removed == false) - { - printf ("Error could not remove old file: [%s]\n", - downloadArchive.c_str ()); - } - } - string - compressCmd = - getFullFileArchiveCompressCommand - (fileArchiveCompressCommand, - fileArchiveCompressCommandParameters, - downloadArchive, tilesetDataPath); - - printf ("Running compression command: %s\n", - compressCmd.c_str ()); - - if (executeShellCommand - (compressCmd, - fileArchiveCompressCommandSuccessResult) == false) - { - printf ("Error could not create new file: [%s]\n", - downloadArchive.c_str ()); - } - - if (fileExists (downloadArchive) == true) - { - off_t - fileSize = getFileSize (downloadArchive); -// convert to MB - double - megaBytes = ((double) fileSize / 1048576.0); - printf ("%s [download archive %.2fMB]\n", name.c_str (), - megaBytes); - } - - break; - } - } - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - - } - if (typesSelected == 0) - { - printf ("Compress item [%s] is not valid!\n", - compress_item.c_str ()); - return_value = 1; - } - else - return_value = 0; - } - else - { - printf - ("\nInvalid missing map specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - - return_value = 1; - } - } - - return return_value; - } - - int - handleShowCRCValuesCommand (int argc, char **argv) - { - int - return_value = 1; - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemName = paramPartTokens[1]; - - string - file = Config::getMapPath (itemName, "", false); - if (file != "") - { - Checksum - checksum; - checksum.addFile (file); - uint32 - crcValue = checksum.getSum (); - - printf ("CRC value for map [%s] file [%s] is [%u]\n", - itemName.c_str (), file.c_str (), crcValue); - - return_value = 0; - } - else - { - printf ("Map [%s] was NOT FOUND\n", itemName.c_str ()); - return_value = 1; - } - } - else - { - printf - ("\nInvalid missing map specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - - return_value = 1; - } - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemName = paramPartTokens[1]; - - Config & config = Config::getInstance (); - uint32 - crcValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + itemName + string ("/*"), ".xml", NULL, true); - if (crcValue != 0) - { - printf ("CRC value for tileset [%s] is [%u]\n", itemName.c_str (), - crcValue); - - return_value = 0; - } - else - { - printf ("Tileset [%s] was NOT FOUND\n", itemName.c_str ()); - return_value = 1; - } - } - else - { - printf - ("\nInvalid missing tileset specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - - return_value = 1; - } - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemName = paramPartTokens[1]; - - Config & config = Config::getInstance (); - uint32 - crcValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), "/" + itemName + "/*", - ".xml", NULL, true); - if (crcValue != 0) - { - printf ("CRC value for techtree [%s] is [%u]\n", - itemName.c_str (), crcValue); - - return_value = 0; - } - else - { - printf ("Techtree [%s] was NOT FOUND\n", itemName.c_str ()); - - return_value = 1; - } - } - else - { - printf - ("\nInvalid missing techtree specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - - return_value = 1; - } - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemName = paramPartTokens[1]; - - Config & config = Config::getInstance (); - uint32 - crcValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptScenarios, ""), - "/" + itemName + "/*", ".xml", NULL, true); - if (crcValue != 0) - { - printf ("CRC value for scenario [%s] is [%u]\n", - itemName.c_str (), crcValue); - - return_value = 0; - } - else - { - printf ("Scenario [%s] was NOT FOUND\n", itemName.c_str ()); - return_value = 1; - } - } - else - { - printf - ("\nInvalid missing scenario specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - - return_value = 0; - } - } - - else - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]), - &foundParamIndIndex); - } - - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 3 && paramPartTokens[1].length () > 0) - { - string - itemName = paramPartTokens[1]; - string - itemNameFilter = paramPartTokens[2]; - uint32 - crcValue = - getFolderTreeContentsCheckSumRecursively (itemName, - itemNameFilter, NULL, - true); - - printf ("CRC value for path [%s] filter [%s] is [%u]\n", - itemName.c_str (), itemNameFilter.c_str (), crcValue); - - return_value = 0; - } - else - { - if (paramPartTokens.size () < 2) - { - printf - ("\nInvalid missing path and filter specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - } - if (paramPartTokens.size () < 3) - { - printf - ("\nInvalid missing filter specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 3 ? paramPartTokens[2].c_str () : NULL)); - } - - return_value = 1; - } - } - - return return_value; - } - - int - handleListDataCommand (int argc, char **argv) - { - int - return_value = 1; - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LIST_MAPS]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_MAPS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_MAPS]), - &foundParamIndIndex); - } - - Config & config = Config::getInstance (); - vector < string > pathList = config.getPathListForType (ptMaps, ""); - vector < string > maps = - MapPreview::findAllValidMaps (pathList, "", false, true); - std::sort (maps.begin (), maps.end ()); - - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemNameFilter = paramPartTokens[1]; - printf ("Using filter for maps list [%s]\n", - itemNameFilter.c_str ()); - - vector < string > filteredMaps; - for (unsigned int i = 0; i < maps.size (); ++i) - { - string - mapName = maps[i]; - if (itemNameFilter.find ("*") != itemNameFilter.npos) - { - if (StartsWith - (mapName, - itemNameFilter.substr (0, - itemNameFilter.find ("*"))) == true) - { - filteredMaps.push_back (mapName); - } - } - else if (mapName == itemNameFilter) - { - filteredMaps.push_back (mapName); - } - } - maps = filteredMaps; - } - - printf ("Maps found:\n===========================================\n"); - for (unsigned int i = 0; i < maps.size (); ++i) - { - string - mapName = maps[i]; - printf ("%s\n", mapName.c_str ()); - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", maps.size ()); - - return_value = 0; - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TECHTRESS]), - &foundParamIndIndex); - } - - Config & config = Config::getInstance (); - vector < string > pathList = config.getPathListForType (ptTechs, ""); - vector < string > results; - findDirs (pathList, results); - - bool - showfactions = false; - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - cmd = paramPartTokens[1]; - if (cmd == "showfactions") - { - showfactions = true; - } - else - { - throw - megaglest_runtime_error ("unknown command for techtreelist [" + - cmd + "]"); - } - printf ("Using special command for techtree list [%s]\n", - cmd.c_str ()); - } - - printf - ("Techtrees found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - - for (unsigned int j = 0; j < pathList.size (); ++j) - { - string - techPath = pathList[j]; - if (techPath != "") - { - endPathWithSlash (techPath); - } - vector < string > results2; - findDirs (techPath + name + "/factions", results2, false, true); - if (results2.empty () == false) - { - string - downloadArchive = techPath + name + ".7z"; - - if (fileExists (downloadArchive) == true) - { - off_t - fileSize = getFileSize (downloadArchive); -// convert to MB - double - megaBytes = ((double) fileSize / 1048576.0); - printf ("%s [download archive %.2fMB]\n", name.c_str (), - megaBytes); - } - else - { - printf ("%s\n", name.c_str ()); - } - - if (showfactions == true) - { - printf ("--> Factions:\n"); - for (unsigned int k = 0; k < results2.size (); ++k) - { - string - name2 = results2[k]; - printf ("--> %s\n", name2.c_str ()); - } - printf ("--> Total Factions: " MG_SIZE_T_SPECIFIER "\n", - results2.size ()); - break; - } - } - } - } - printf - ("===========================================\nTotal Techtrees: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - - return_value = 0; - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_SCENARIOS]), - &foundParamIndIndex); - } - - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptScenarios, ""); - vector < string > results; - findDirs (pathList, results); - - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemNameFilter = paramPartTokens[1]; - printf ("Using filter for scenarios list [%s]\n", - itemNameFilter.c_str ()); - - vector < string > filtered; - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - if (itemNameFilter.find ("*") != itemNameFilter.npos) - { - if (StartsWith - (name, - itemNameFilter.substr (0, - itemNameFilter.find ("*"))) == true) - { - filtered.push_back (name); - } - } - else if (name == itemNameFilter) - { - filtered.push_back (name); - } - } - results = filtered; - } - - printf - ("Scenarios found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - printf ("%s\n", name.c_str ()); - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - - return_value = 0; - } - - else - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LIST_TILESETS]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TILESETS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TILESETS]), - &foundParamIndIndex); - } - - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptTilesets, ""); - vector < string > results; - findDirs (pathList, results); - - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemNameFilter = paramPartTokens[1]; - printf ("Using filter for tilesets list [%s]\n", - itemNameFilter.c_str ()); - - vector < string > filtered; - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - if (itemNameFilter.find ("*") != itemNameFilter.npos) - { - if (StartsWith - (name, - itemNameFilter.substr (0, - itemNameFilter.find ("*"))) == true) - { - filtered.push_back (name); - } - } - else if (name == itemNameFilter) - { - filtered.push_back (name); - } - } - results = filtered; - } - - printf - ("Tilesets found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - - for (unsigned int j = 0; j < pathList.size (); ++j) - { - string - tilesetPath = pathList[j]; - if (tilesetPath != "") - { - endPathWithSlash (tilesetPath); - } - if (fileExists (tilesetPath + name + "/" + name + ".xml") == true) - { - string - downloadArchive = tilesetPath + name + ".7z"; - if (fileExists (downloadArchive) == true) - { - off_t - fileSize = getFileSize (downloadArchive); -// convert to MB - double - megaBytes = ((double) fileSize / 1048576.0); - printf ("%s [download archive %.2fMB]\n", name.c_str (), - megaBytes); - } - else - { - printf ("%s\n", name.c_str ()); - } - - break; - } - } - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - - return_value = 0; - } - - else - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LIST_TUTORIALS]), - &foundParamIndIndex); - } - - Config & config = Config::getInstance (); - vector < string > pathList = - config.getPathListForType (ptTutorials, ""); - vector < string > results; - findDirs (pathList, results); - - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - itemNameFilter = paramPartTokens[1]; - printf ("Using filter for tutorials list [%s]\n", - itemNameFilter.c_str ()); - - vector < string > filtered; - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - if (itemNameFilter.find ("*") != itemNameFilter.npos) - { - if (StartsWith - (name, - itemNameFilter.substr (0, - itemNameFilter.find ("*"))) == true) - { - filtered.push_back (name); - } - } - else if (name == itemNameFilter) - { - filtered.push_back (name); - } - } - results = filtered; - } - - printf - ("Tutorials found:\n===========================================\n"); - for (unsigned int i = 0; i < results.size (); ++i) - { - string - name = results[i]; - - for (unsigned int j = 0; j < pathList.size (); ++j) - { - string - tutorialsPath = pathList[j]; - if (tutorialsPath != "") - { - endPathWithSlash (tutorialsPath); - } - if (fileExists (tutorialsPath + name + "/" + name + ".xml") == - true) - { - string - downloadArchive = tutorialsPath + name + ".7z"; - if (fileExists (downloadArchive) == true) - { - off_t - fileSize = getFileSize (downloadArchive); -// convert to MB - double - megaBytes = ((double) fileSize / 1048576.0); - printf ("%s [download archive %.2fMB]\n", name.c_str (), - megaBytes); - } - else - { - printf ("%s\n", name.c_str ()); - } - - break; - } - } - } - printf ("===========================================\nTotal: " - MG_SIZE_T_SPECIFIER "\n", results.size ()); - - return_value = 0; - } - - return return_value; - } - - int - glestMain (int argc, char **argv) - { + } else { + printf + ("\n** Cannot product techtree translation file [%s] for techPath [%s] techName [%s] because the file already exists!\n", + translationFile.c_str(), techPath.c_str(), + techName.c_str()); + } + } catch (const megaglest_runtime_error & ex) { + printf + ("\n\n****ERROR**** detected while loading the techName: %s\nMESSAGE: %s\n", + techName.c_str(), ex.what()); + } + + } + printf + ("----------------------------------------------------------------"); + } + } + + void + runTechTranslationExtraction(int argc, char **argv) { + printf("====== Started Translation Extraction ======\n"); + + Config & config = Config::getInstance(); + + // Did the user pass a specific list of factions to validate? + std::vector < string > filteredFactionList; + + vector < string > results; + findDirs(config.getPathListForType(ptTechs), results); + vector < string > techTreeFiles = results; + // Did the user pass a specific list of techtrees to validate? + std::vector < string > filteredTechTreeList; + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) + string("=")) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) + + string("="), &foundParamIndIndex); + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + if (paramPartTokens.size() >= 2) { + string + techtreeList = paramPartTokens[1]; + Tokenize(techtreeList, filteredTechTreeList, ","); + + if (filteredTechTreeList.empty() == false) { + printf + ("Filtering techtrees and only looking for the following:\n"); + for (int idx = 0; idx < (int) filteredTechTreeList.size(); ++idx) { + filteredTechTreeList[idx] = trim(filteredTechTreeList[idx]); + printf("%s\n", filteredTechTreeList[idx].c_str()); + } + } + } + } + + { + printf + ("\n---------------- Loading factions inside world ----------------"); + World + world; + + vector < string > techPaths = config.getPathListForType(ptTechs); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + + for (int idx2 = 0; idx2 < (int) techTreeFiles.size(); idx2++) { + string & techName = techTreeFiles[idx2]; + + if (filteredTechTreeList.empty() == true || + std::find(filteredTechTreeList.begin(), + filteredTechTreeList.end(), + techName) != filteredTechTreeList.end()) { + + runTechTranslationExtractionForPath(techPath, techName, + filteredFactionList, + world); + } + } + } + + printf("\n====== Finished Translation ======\n"); + } + + } + + + void + runTechValidationReport(int argc, char **argv) { + printf("====== Started Validation ======\n"); + + bool + purgeDuplicateFiles = false; + bool + showDuplicateFiles = true; + bool + purgeUnusedFiles = false; + bool + gitPurgeFiles = false; + + double + purgedMegaBytes = 0; + Config & config = Config::getInstance(); + + // Did the user pass a specific scenario to validate? + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) + string("=")) == + true) { + + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) + + string("="), &foundParamIndIndex); + + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + + if (paramPartTokens.size() >= 2) { + string + validateScenarioName = paramPartTokens[1]; + + printf("Filtering scenario: %s\n", validateScenarioName.c_str()); + + if (paramPartTokens.size() >= 3) { + if (paramPartTokens[2] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused scenario files will be deleted!\n"); + } + } + + { + printf + ("\n---------------- Loading scenario inside world ----------------\n"); + + bool + scenarioFound = false; + World + world; + std::vector < string > filteredFactionList; + + vector < string > scenarioPaths = + config.getPathListForType(ptScenarios); + for (int idx = 0; idx < (int) scenarioPaths.size(); idx++) { + string & scenarioPath = scenarioPaths[idx]; + endPathWithSlash(scenarioPath); + + vector < string > scenarioList; + findDirs(scenarioPath, scenarioList, false, false); + for (int idx2 = 0; idx2 < (int) scenarioList.size(); idx2++) { + string & scenarioName = scenarioList[idx2]; + + if (scenarioName == validateScenarioName) { + scenarioFound = true; + + string + file = + scenarioPath + scenarioName + "/" + scenarioName + ".xml"; + + XmlTree + xmlTree; + xmlTree.load(file, Properties::getTagReplacementValues()); + const XmlNode * + scenarioNode = xmlTree.getRootNode(); + string + techName = + scenarioNode->getChild("tech-tree")-> + getAttribute("value")->getValue(); + + // Self Contained techtree? + string + scenarioTechtree = + scenarioPath + scenarioName + "/" + techName + "/" + + techName + ".xml"; + + printf + ("\nFound Scenario [%s] looking for techtree [%s]...\n", + scenarioName.c_str(), scenarioTechtree.c_str()); + + if (fileExists(scenarioTechtree) == true) { + string + techPath = scenarioPath + scenarioName + "/"; + + printf + ("\nFound Scenario [%s] with custom techtree [%s] validating...\n", + scenarioName.c_str(), techName.c_str()); + runTechValidationForPath(techPath, techName, + filteredFactionList, world, + purgeUnusedFiles, + showDuplicateFiles, false, + false, purgedMegaBytes); + } else { + vector < string > techPaths = + config.getPathListForType(ptTechs); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + scenarioTechtree = + techPath + "/" + techName + "/" + techName + ".xml"; + if (fileExists(scenarioTechtree) == true) { + printf + ("\nFound Scenario [%s] with techtree [%s] validating...\n", + scenarioName.c_str(), techName.c_str()); + runTechValidationForPath(techPath, techName, + filteredFactionList, world, + purgeUnusedFiles, + showDuplicateFiles, false, + false, purgedMegaBytes); + + break; + } + } + } + } + } + } + + if (scenarioFound == false) { + printf("\nWARNING, the scenario [%s] was NOT FOUND!\n", + validateScenarioName.c_str()); + } + printf("\n====== Finished Validation ======\n"); + } + return; + } else { + printf + ("\nInvalid missing scenario specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return; + } + } + + // Did the user pass a specific list of factions to validate? + std::vector < string > filteredFactionList; + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) + string("=")) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) + + string("="), &foundParamIndIndex); + + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + + if (paramPartTokens.size() >= 2) { + string + factionList = paramPartTokens[1]; + Tokenize(factionList, filteredFactionList, ","); + + if (filteredFactionList.empty() == false) { + printf + ("Filtering factions and only looking for the following:\n"); + for (int idx = 0; idx < (int) filteredFactionList.size(); ++idx) { + filteredFactionList[idx] = trim(filteredFactionList[idx]); + printf("%s\n", filteredFactionList[idx].c_str()); + } + } + + if (paramPartTokens.size() >= 3) { + if (paramPartTokens[2] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused faction files will be deleted!\n"); + } + } + } + } + vector < string > results; + findDirs(config.getPathListForType(ptTechs), results); + vector < string > techTreeFiles = results; + // Did the user pass a specific list of techtrees to validate? + std::vector < string > filteredTechTreeList; + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) + string("=")) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) + + string("="), &foundParamIndIndex); + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + if (paramPartTokens.size() >= 2) { + string + techtreeList = paramPartTokens[1]; + Tokenize(techtreeList, filteredTechTreeList, ","); + + if (filteredTechTreeList.empty() == false) { + printf + ("Filtering techtrees and only looking for the following:\n"); + for (int idx = 0; idx < (int) filteredTechTreeList.size(); ++idx) { + filteredTechTreeList[idx] = trim(filteredTechTreeList[idx]); + printf("%s\n", filteredTechTreeList[idx].c_str()); + } + } + + if (paramPartTokens.size() >= 3) { + if (paramPartTokens[2] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused techtree files will be deleted!\n"); + } else if (paramPartTokens[2] == "purgeduplicates") { + purgeDuplicateFiles = true; + printf("*NOTE All duplicate techtree files will be merged!\n"); + } else if (paramPartTokens[2] == "gitdelete") { + gitPurgeFiles = true; + printf + ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); + } else if (paramPartTokens[2] == "hideduplicates") { + showDuplicateFiles = false; + printf + ("*NOTE All duplicate techtree files will NOT be shown!\n"); + } + } + if (paramPartTokens.size() >= 4) { + if (paramPartTokens[3] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused techtree files will be deleted!\n"); + } else if (paramPartTokens[3] == "purgeduplicates") { + purgeDuplicateFiles = true; + printf("*NOTE All duplicate techtree files will be merged!\n"); + } else if (paramPartTokens[3] == "gitdelete") { + gitPurgeFiles = true; + printf + ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); + } else if (paramPartTokens[3] == "hideduplicates") { + showDuplicateFiles = false; + printf + ("*NOTE All duplicate techtree files will NOT be shown!\n"); + } + } + if (paramPartTokens.size() >= 5) { + if (paramPartTokens[4] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused techtree files will be deleted!\n"); + } else if (paramPartTokens[4] == "purgeduplicates") { + purgeDuplicateFiles = true; + printf("*NOTE All duplicate techtree files will be merged!\n"); + } else if (paramPartTokens[4] == "gitdelete") { + gitPurgeFiles = true; + printf + ("*NOTE All unused / duplicate techtree files will be removed from git!\n"); + } else if (paramPartTokens[4] == "hideduplicates") { + showDuplicateFiles = false; + printf + ("*NOTE All duplicate techtree files will NOT be shown!\n"); + } + } + } + } + + { + printf + ("\n---------------- Loading factions inside world ----------------"); + World + world; + + vector < string > techPaths = config.getPathListForType(ptTechs); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + + for (int idx2 = 0; idx2 < (int) techTreeFiles.size(); idx2++) { + string & techName = techTreeFiles[idx2]; + + if (filteredTechTreeList.empty() == true || + std::find(filteredTechTreeList.begin(), + filteredTechTreeList.end(), + techName) != filteredTechTreeList.end()) { + + runTechValidationForPath(techPath, techName, + filteredFactionList, world, + purgeUnusedFiles, purgeDuplicateFiles, + showDuplicateFiles, gitPurgeFiles, + purgedMegaBytes); + } + } + } + + printf("\n====== Finished Validation ======\n"); + } + + } + + void + runTilesetValidationReport(int argc, char **argv) { + printf("====== Started Validation ======\n"); + + Config & config = Config::getInstance(); + + // Did the user pass a specific tileset to validate? + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) + string("=")) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) + + string("="), &foundParamIndIndex); + + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + + if (paramPartTokens.size() >= 2) { + string + validateTilesetName = paramPartTokens[1]; + + printf("Filtering tileset: %s\n", validateTilesetName.c_str()); + + bool + purgeUnusedFiles = false; + if (paramPartTokens.size() >= 3) { + if (paramPartTokens[2] == "purgeunused") { + purgeUnusedFiles = true; + printf("*NOTE All unused tileset files will be deleted!\n"); + } + } + + { + printf + ("\n---------------- Loading tileset inside world ----------------\n"); + + World + world; + double + purgedMegaBytes = 0; + bool + showDuplicateFiles = true; + + bool + tilesetFound = false; + + vector < string > tilesetPaths = + config.getPathListForType(ptTilesets); + for (int idx = 0; idx < (int) tilesetPaths.size(); idx++) { + string & tilesetPath = tilesetPaths[idx]; + endPathWithSlash(tilesetPath); + + vector < string > tilesetList; + findDirs(tilesetPath, tilesetList, false, false); + for (int idx2 = 0; idx2 < (int) tilesetList.size(); idx2++) { + string & tilesetName = tilesetList[idx2]; + if (tilesetName == validateTilesetName) { + tilesetFound = true; + runTilesetValidationForPath(tilesetPath, tilesetName, + world, purgeUnusedFiles, + showDuplicateFiles, false, + false, purgedMegaBytes); + } + } + } + + if (tilesetFound == false) { + printf("*ERROR The specified tileset [%s] was NOT FOUND!\n", + validateTilesetName.c_str()); + } + printf("\n====== Finished Validation ======\n"); + } + return; + } else { + printf + ("\nInvalid missing tileset specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return; + } + } else { + printf("\nInvalid missing tileset specified on commandline\n\n"); + return; + } + } + + void + ShowINISettings(int argc, char **argv, Config & config, + Config & configKeys) { + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == true) { + vector < string > filteredPropertyList; + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) + string("=")) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) + + string("="), &foundParamIndIndex); + string + filterList = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(filterList, paramPartTokens, "="); + if (paramPartTokens.size() >= 2) { + string + tokenList = paramPartTokens[1]; + Tokenize(tokenList, filteredPropertyList, ","); + + if (filteredPropertyList.empty() == false) { + printf + ("Filtering properties and only looking for the following:\n"); + for (int idx = 0; idx < (int) filteredPropertyList.size(); + ++idx) { + filteredPropertyList[idx] = trim(filteredPropertyList[idx]); + printf("%s\n", filteredPropertyList[idx].c_str()); + } + } + } + } + + printf("\nMain settings report\n"); + printf("====================\n"); + vector < pair < string, string > >mergedMainSettings = + config.getMergedProperties(); + vector < pair < string, string > >mergedKeySettings = + configKeys.getMergedProperties(); + + // Figure out the max # of tabs we need to format display nicely + int + tabCount = 1; + for (int i = 0; i < (int) mergedMainSettings.size(); ++i) { + const + pair < + string, + string > & + nameValue = mergedMainSettings[i]; + + bool + displayProperty = false; + if (filteredPropertyList.empty() == false) { + if (find + (filteredPropertyList.begin(), filteredPropertyList.end(), + nameValue.first) != filteredPropertyList.end()) { + displayProperty = true; + } + } else { + displayProperty = true; + } + + if (displayProperty == true) { + int + requredTabs = ((int) nameValue.first.length() / 8) + 1; + if (nameValue.first.length() % 8) { + requredTabs++; + } + if (requredTabs > tabCount) { + tabCount = requredTabs; + } + } + } + for (int i = 0; i < (int) mergedKeySettings.size(); ++i) { + const + pair < + string, + string > & + nameValue = mergedKeySettings[i]; + + bool + displayProperty = false; + if (filteredPropertyList.empty() == false) { + if (find + (filteredPropertyList.begin(), filteredPropertyList.end(), + nameValue.first) != filteredPropertyList.end()) { + displayProperty = true; + } + } else { + displayProperty = true; + } + + if (displayProperty == true) { + int + requredTabs = ((int) nameValue.first.length() / 8) + 1; + if (nameValue.first.length() % 8) { + requredTabs++; + } + if (requredTabs > tabCount) { + tabCount = requredTabs; + } + } + } + + // Output the properties + for (int i = 0; i < (int) mergedMainSettings.size(); ++i) { + const + pair < + string, + string > & + nameValue = mergedMainSettings[i]; + + bool + displayProperty = false; + if (filteredPropertyList.empty() == false) { + if (find + (filteredPropertyList.begin(), filteredPropertyList.end(), + nameValue.first) != filteredPropertyList.end()) { + displayProperty = true; + } + } else { + displayProperty = true; + } + + if (displayProperty == true) { + printf("Property Name [%s]", nameValue.first.c_str()); + + int + tabs = ((int) nameValue.first.length() / 8) + 1; + for (int j = 0; j < (tabCount - tabs); ++j) { + printf("\t"); + } + + string + displayValue = nameValue.second; + if (nameValue.first == "TranslationGetURLPassword") { + displayValue = "*****"; + } + + printf("Value [%s]\n", displayValue.c_str()); + } + } + + printf("\n\nMain key binding settings report\n"); + printf("====================================\n"); + + for (int i = 0; i < (int) mergedKeySettings.size(); ++i) { + const + pair < + string, + string > & + nameValue = mergedKeySettings[i]; + + bool + displayProperty = false; + if (filteredPropertyList.empty() == false) { + if (find + (filteredPropertyList.begin(), filteredPropertyList.end(), + nameValue.first) != filteredPropertyList.end()) { + displayProperty = true; + } + } else { + displayProperty = true; + } + + if (displayProperty == true) { + printf("Property Name [%s]", nameValue.first.c_str()); + + int + tabs = ((int) nameValue.first.length() / 8) + 1; + for (int j = 0; j < (tabCount - tabs); ++j) { + printf("\t"); + } + + printf("Value [%s]\n", nameValue.second.c_str()); + } + } + } + } + + Steam & initSteamInstance() { + Steam *& + steamInstance = + CacheManager::getCachedItem < + Steam * >(GameConstants::steamCacheInstanceKey); + if (steamInstance == NULL) { + steamInstance = new Steam(); + } + return *steamInstance; + } + + void + setupSteamSettings(bool steamEnabled, bool steamResetStats, + bool debugEnabled) { + Config & config = Config::getInstance(); + config.setBool("SteamEnabled", steamEnabled, true); + if (steamEnabled) { + printf("*NOTE: Steam Integration Enabled.\n"); + + if (debugEnabled) { + printf("*NOTE: Steam Debugging Enabled.\n"); + } + Steam::setDebugEnabled(debugEnabled); + + Steam & steam = initSteamInstance(); + + // For Debugging purposes: + if (steamResetStats) { + printf + ("*WARNING: Steam Stats / Achievements are being RESET by request!\n"); + steam.resetStats(true); + } + + string + steamPlayerName = steam.userName(); + string + steamLang = steam.lang(); + printf + ("Steam Integration Enabled!\nSteam User Name is [%s] Language is [%s]\n", + steamPlayerName.c_str(), steamLang.c_str()); + + bool + needToSaveConfig = false; + string + currentPLayerName = config.getString("NetPlayerName", ""); + if (currentPLayerName == "newbie" || currentPLayerName == "") { + config.setString("NetPlayerName", steamPlayerName); + needToSaveConfig = true; + } + if (needToSaveConfig == true) { + config.save(); + } + } + } + + void + CheckForDuplicateData() { + Config & config = Config::getInstance(); + + string + duplicateWarnings = ""; + + try { + + + { + + string + scenarioDir = ""; + vector < string > pathList = + config.getPathListForType(ptMaps, scenarioDir); + vector < string > invalidMapList; + vector < string > maps = + MapPreview::findAllValidMaps(pathList, scenarioDir, false, true, + &invalidMapList); + std::sort(maps.begin(), maps.end()); + + if (maps.empty() == true) { + throw + megaglest_runtime_error("No maps were found!", true); + } else if (invalidMapList.empty() == false) { + string + errorMsg = + "Warning invalid maps were detected (will be ignored):\n"; + for (int i = 0; i < (int) invalidMapList.size(); ++i) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "map [%s]\n", + invalidMapList[i].c_str()); + + errorMsg += szBuf; + } + duplicateWarnings += errorMsg; + } + + vector < string > duplicateMapsToRename; + for (int i = 0; i < (int) maps.size(); ++i) { + string + map1 = maps[i]; + for (int j = 0; j < (int) maps.size(); ++j) { + if (i != j) { + string + map2 = maps[j]; + + if (map1 == map2) { + if (std::find(duplicateMapsToRename.begin(), + duplicateMapsToRename.end(), + map1) == duplicateMapsToRename.end()) { + duplicateMapsToRename.push_back(map1); + } + } + } + } + } + if (duplicateMapsToRename.empty() == false) { + string + errorMsg = + "Warning duplicate maps were detected and renamed:\n"; + for (int i = 0; i < (int) duplicateMapsToRename.size(); ++i) { + string + currentPath = pathList[1]; + endPathWithSlash(currentPath); + + string + oldFile = currentPath + duplicateMapsToRename[i]; + string + newFile = currentPath + duplicateMapsToRename[i]; + string + ext = extractExtension(newFile); + newFile = + newFile.substr(0, newFile.length() - ext.length() - 1); + newFile = newFile + "_custom." + ext; + + char + szBuf[8096] = ""; + int + result = rename(oldFile.c_str(), newFile.c_str()); + if (result != 0) { + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "Error [%s]\nCould not rename [%s] to [%s]!", + errmsg, oldFile.c_str(), newFile.c_str()); + throw + megaglest_runtime_error(szBuf, true); + } else { + snprintf(szBuf, 8096, + "map [%s] in [%s]\nwas renamed to [%s]", + duplicateMapsToRename[i].c_str(), oldFile.c_str(), + newFile.c_str()); + } + errorMsg += szBuf; + } + duplicateWarnings += errorMsg; + } + } + + { + //tilesets + std::vector < std::string > tileSets; + vector < string > tilesetPaths = + config.getPathListForType(ptTilesets); + findDirs(tilesetPaths, tileSets, false, true); + + if (tileSets.empty()) { + throw + megaglest_runtime_error("No tilesets were found!", true); + } + + vector < string > duplicateTilesetsToRename; + for (int i = 0; i < (int) tileSets.size(); ++i) { + string + tileSet1 = tileSets[i]; + for (int j = 0; j < (int) tileSets.size(); ++j) { + if (i != j) { + string + tileSet2 = tileSets[j]; + if (tileSet1 == tileSet2) { + if (std::find(duplicateTilesetsToRename.begin(), + duplicateTilesetsToRename.end(), + tileSet1) == + duplicateTilesetsToRename.end()) { + duplicateTilesetsToRename.push_back(tileSet1); + } + } + } + } + } + if (duplicateTilesetsToRename.empty() == false) { + string + errorMsg = + "Warning duplicate tilesets were detected and renamed:\n"; + + for (int i = 0; i < (int) duplicateTilesetsToRename.size(); ++i) { + string + currentPath = tilesetPaths[1]; + endPathWithSlash(currentPath); + + string + oldFile = currentPath + duplicateTilesetsToRename[i]; + string + newFile = currentPath + duplicateTilesetsToRename[i]; + newFile = newFile + "_custom"; + + char + szBuf[8096] = ""; + int + result = rename(oldFile.c_str(), newFile.c_str()); + if (result != 0) { + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "Error [%s]\nCould not rename [%s] to [%s]!", + errmsg, oldFile.c_str(), newFile.c_str()); + throw + megaglest_runtime_error(szBuf, true); + } else { + snprintf(szBuf, 8096, + "tileset [%s] in [%s]\nwas renamed to [%s]", + duplicateTilesetsToRename[i].c_str(), + oldFile.c_str(), newFile.c_str()); + + string + tilesetName = extractFileFromDirectoryPath(oldFile); + oldFile = newFile + "/" + tilesetName + ".xml"; + newFile = newFile + "/" + tilesetName + "_custom.xml"; + + result = rename(oldFile.c_str(), newFile.c_str()); + + if (result != 0) { + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "Error [%s]\nCould not rename [%s] to [%s]!", + errmsg, oldFile.c_str(), newFile.c_str()); + throw + megaglest_runtime_error(szBuf, true); + } + + } + errorMsg += szBuf; + } + duplicateWarnings += errorMsg; + } + } + + { + vector < string > techPaths = config.getPathListForType(ptTechs); + vector < string > techTrees; + findDirs(techPaths, techTrees, false, true); + if (techTrees.empty()) { + throw + megaglest_runtime_error("No tech-trees were found (dup)!", + true); + } + + vector < string > duplicateTechtreesToRename; + for (int i = 0; i < (int) techTrees.size(); ++i) { + string + techtree1 = techTrees[i]; + for (int j = 0; j < (int) techTrees.size(); ++j) { + if (i != j) { + string + techtree2 = techTrees[j]; + if (techtree1 == techtree2) { + if (std::find(duplicateTechtreesToRename.begin(), + duplicateTechtreesToRename.end(), + techtree1) == + duplicateTechtreesToRename.end()) { + duplicateTechtreesToRename.push_back(techtree1); + } + } + } + } + } + if (duplicateTechtreesToRename.empty() == false) { + string + errorMsg = + "Warning duplicate techtrees were detected and renamed:\n"; + + for (int i = 0; i < (int) duplicateTechtreesToRename.size(); ++i) { + string + currentPath = techPaths[1]; + endPathWithSlash(currentPath); + + string + oldFile = currentPath + duplicateTechtreesToRename[i]; + string + newFile = currentPath + duplicateTechtreesToRename[i]; + newFile = newFile + "_custom"; + + char + szBuf[8096] = ""; + int + result = rename(oldFile.c_str(), newFile.c_str()); + if (result != 0) { + char * + errmsg = strerror(errno); + snprintf(szBuf, 8096, + "Error [%s]\nCould not rename [%s] to [%s]!", + errmsg, oldFile.c_str(), newFile.c_str()); + throw + megaglest_runtime_error(szBuf, true); + } else { + snprintf(szBuf, 8096, + "techtree [%s] in [%s]\nwas renamed to [%s]", + duplicateTechtreesToRename[i].c_str(), + oldFile.c_str(), newFile.c_str()); + + string + tilesetName = extractFileFromDirectoryPath(oldFile); + oldFile = newFile + "/" + tilesetName + ".xml"; + newFile = newFile + "/" + tilesetName + "_custom.xml"; + + int + rename_result = rename(oldFile.c_str(), newFile.c_str()); + if (rename_result != 0) { + printf("Error renaming [%s] to [%s]\n", oldFile.c_str(), + newFile.c_str()); + } + } + errorMsg += szBuf; + } + duplicateWarnings += errorMsg; + } + } + + } catch (const megaglest_runtime_error & ex) { + if (mainProgram) { + mainProgram->getState()->setForceMouseRender(true); + } + ExceptionHandler::DisplayMessage(ex.what(), false); + } + + if (duplicateWarnings != "") { + if (mainProgram) { + mainProgram->getState()->setForceMouseRender(true); + } + ExceptionHandler::DisplayMessage(duplicateWarnings.c_str(), false); + } + } + + int + handleCreateDataArchivesCommand(int argc, char **argv) { + int + return_value = 1; + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS + [GAME_ARG_CREATE_DATA_ARCHIVES]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + compress_item = paramPartTokens[1]; + bool + includeMainData = false; + if (paramPartTokens.size() >= 3 + && paramPartTokens[2] == "include_main") { + includeMainData = true; + } + + Config & config = Config::getInstance(); + string + fileArchiveExtension = + config.getString("FileArchiveExtension", ""); + string + fileArchiveCompressCommand = + config.getString("FileArchiveCompressCommand", ""); + string + fileArchiveCompressCommandParameters = + config.getString("FileArchiveCompressCommandParameters", ""); + int32 + fileArchiveCompressCommandSuccessResult = + config.getInt("FileArchiveCompressCommandSuccessResult", "0"); + + string + userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + + int + typesSelected = 0; + if (compress_item == "techtrees" || compress_item == "all") { + typesSelected++; + + vector < string > pathList = + config.getPathListForType(ptTechs, ""); + vector < string > results; + findDirs(pathList, results); + + printf + ("Techtrees found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + + for (unsigned int j = 0; j < pathList.size(); ++j) { + string + techPath = pathList[j]; + if (techPath != "") { + endPathWithSlash(techPath); + } + + vector < string > results2; + findDirs(techPath + name + "/factions", results2, false, + true); + if (results2.empty() == false) { + string + techtreePath = techPath + name; + if (includeMainData == false) { + if (techtreePath.find(userData) == techPath.npos) { + printf("Skipping techtree: [%s]\n", + techtreePath.c_str()); + continue; + } + } + + string + downloadArchive = techtreePath + fileArchiveExtension; + + if (fileExists(downloadArchive) == true) { + bool + removed = removeFile(downloadArchive); + if (removed == false) { + printf("Error could not remove old file: [%s]\n", + downloadArchive.c_str()); + } + } + string + compressCmd = + getFullFileArchiveCompressCommand + (fileArchiveCompressCommand, + fileArchiveCompressCommandParameters, + downloadArchive, techtreePath); + + printf("Running compression command: %s\n", + compressCmd.c_str()); + + if (executeShellCommand + (compressCmd, + fileArchiveCompressCommandSuccessResult) == false) { + printf("Error could not create new file: [%s]\n", + downloadArchive.c_str()); + } + + if (fileExists(downloadArchive) == true) { + off_t + fileSize = getFileSize(downloadArchive); + // convert to MB + double + megaBytes = ((double) fileSize / 1048576.0); + printf("%s [download archive %.2fMB]\n", name.c_str(), + megaBytes); + } + } + } + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + } + if (compress_item == "tilesets" || compress_item == "all") { + typesSelected++; + + vector < string > pathList = + config.getPathListForType(ptTilesets, ""); + vector < string > results; + findDirs(pathList, results); + + printf + ("Tilesets found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + + for (unsigned int j = 0; j < pathList.size(); ++j) { + string + tilesetPath = pathList[j]; + if (tilesetPath != "") { + endPathWithSlash(tilesetPath); + } + + if (fileExists(tilesetPath + name + "/" + name + ".xml") == + true) { + string + tilesetDataPath = tilesetPath + name; + if (includeMainData == false) { + if (tilesetPath.find(userData) == tilesetPath.npos) { + printf("Skipping tileset data: [%s]\n", + tilesetDataPath.c_str()); + continue; + } + } + + string + downloadArchive = tilesetDataPath + fileArchiveExtension; + + if (fileExists(downloadArchive) == true) { + bool + removed = removeFile(downloadArchive); + if (removed == false) { + printf("Error could not remove old file: [%s]\n", + downloadArchive.c_str()); + } + } + string + compressCmd = + getFullFileArchiveCompressCommand + (fileArchiveCompressCommand, + fileArchiveCompressCommandParameters, + downloadArchive, tilesetDataPath); + + printf("Running compression command: %s\n", + compressCmd.c_str()); + + if (executeShellCommand + (compressCmd, + fileArchiveCompressCommandSuccessResult) == false) { + printf("Error could not create new file: [%s]\n", + downloadArchive.c_str()); + } + + if (fileExists(downloadArchive) == true) { + off_t + fileSize = getFileSize(downloadArchive); + // convert to MB + double + megaBytes = ((double) fileSize / 1048576.0); + printf("%s [download archive %.2fMB]\n", name.c_str(), + megaBytes); + } + + break; + } + } + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + + } + if (typesSelected == 0) { + printf("Compress item [%s] is not valid!\n", + compress_item.c_str()); + return_value = 1; + } else + return_value = 0; + } else { + printf + ("\nInvalid missing map specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + + return_value = 1; + } + } + + return return_value; + } + + int + handleShowCRCValuesCommand(int argc, char **argv) { + int + return_value = 1; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemName = paramPartTokens[1]; + + string + file = Config::getMapPath(itemName, "", false); + if (file != "") { + Checksum + checksum; + checksum.addFile(file); + uint32 + crcValue = checksum.getSum(); + + printf("CRC value for map [%s] file [%s] is [%u]\n", + itemName.c_str(), file.c_str(), crcValue); + + return_value = 0; + } else { + printf("Map [%s] was NOT FOUND\n", itemName.c_str()); + return_value = 1; + } + } else { + printf + ("\nInvalid missing map specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + + return_value = 1; + } + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemName = paramPartTokens[1]; + + Config & config = Config::getInstance(); + uint32 + crcValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + itemName + string("/*"), ".xml", NULL, true); + if (crcValue != 0) { + printf("CRC value for tileset [%s] is [%u]\n", itemName.c_str(), + crcValue); + + return_value = 0; + } else { + printf("Tileset [%s] was NOT FOUND\n", itemName.c_str()); + return_value = 1; + } + } else { + printf + ("\nInvalid missing tileset specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + + return_value = 1; + } + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemName = paramPartTokens[1]; + + Config & config = Config::getInstance(); + uint32 + crcValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), "/" + itemName + "/*", + ".xml", NULL, true); + if (crcValue != 0) { + printf("CRC value for techtree [%s] is [%u]\n", + itemName.c_str(), crcValue); + + return_value = 0; + } else { + printf("Techtree [%s] was NOT FOUND\n", itemName.c_str()); + + return_value = 1; + } + } else { + printf + ("\nInvalid missing techtree specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + + return_value = 1; + } + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemName = paramPartTokens[1]; + + Config & config = Config::getInstance(); + uint32 + crcValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptScenarios, ""), + "/" + itemName + "/*", ".xml", NULL, true); + if (crcValue != 0) { + printf("CRC value for scenario [%s] is [%u]\n", + itemName.c_str(), crcValue); + + return_value = 0; + } else { + printf("Scenario [%s] was NOT FOUND\n", itemName.c_str()); + return_value = 1; + } + } else { + printf + ("\nInvalid missing scenario specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + + return_value = 0; + } + } + + else + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]), + &foundParamIndIndex); + } + + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 3 && paramPartTokens[1].length() > 0) { + string + itemName = paramPartTokens[1]; + string + itemNameFilter = paramPartTokens[2]; + uint32 + crcValue = + getFolderTreeContentsCheckSumRecursively(itemName, + itemNameFilter, NULL, + true); + + printf("CRC value for path [%s] filter [%s] is [%u]\n", + itemName.c_str(), itemNameFilter.c_str(), crcValue); + + return_value = 0; + } else { + if (paramPartTokens.size() < 2) { + printf + ("\nInvalid missing path and filter specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + } + if (paramPartTokens.size() < 3) { + printf + ("\nInvalid missing filter specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 3 ? paramPartTokens[2].c_str() : NULL)); + } + + return_value = 1; + } + } + + return return_value; + } + + int + handleListDataCommand(int argc, char **argv) { + int + return_value = 1; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LIST_MAPS]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_MAPS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_MAPS]), + &foundParamIndIndex); + } + + Config & config = Config::getInstance(); + vector < string > pathList = config.getPathListForType(ptMaps, ""); + vector < string > maps = + MapPreview::findAllValidMaps(pathList, "", false, true); + std::sort(maps.begin(), maps.end()); + + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemNameFilter = paramPartTokens[1]; + printf("Using filter for maps list [%s]\n", + itemNameFilter.c_str()); + + vector < string > filteredMaps; + for (unsigned int i = 0; i < maps.size(); ++i) { + string + mapName = maps[i]; + if (itemNameFilter.find("*") != itemNameFilter.npos) { + if (StartsWith + (mapName, + itemNameFilter.substr(0, + itemNameFilter.find("*"))) == true) { + filteredMaps.push_back(mapName); + } + } else if (mapName == itemNameFilter) { + filteredMaps.push_back(mapName); + } + } + maps = filteredMaps; + } + + printf("Maps found:\n===========================================\n"); + for (unsigned int i = 0; i < maps.size(); ++i) { + string + mapName = maps[i]; + printf("%s\n", mapName.c_str()); + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", maps.size()); + + return_value = 0; + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TECHTRESS]), + &foundParamIndIndex); + } + + Config & config = Config::getInstance(); + vector < string > pathList = config.getPathListForType(ptTechs, ""); + vector < string > results; + findDirs(pathList, results); + + bool + showfactions = false; + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + cmd = paramPartTokens[1]; + if (cmd == "showfactions") { + showfactions = true; + } else { + throw + megaglest_runtime_error("unknown command for techtreelist [" + + cmd + "]"); + } + printf("Using special command for techtree list [%s]\n", + cmd.c_str()); + } + + printf + ("Techtrees found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + + for (unsigned int j = 0; j < pathList.size(); ++j) { + string + techPath = pathList[j]; + if (techPath != "") { + endPathWithSlash(techPath); + } + vector < string > results2; + findDirs(techPath + name + "/factions", results2, false, true); + if (results2.empty() == false) { + string + downloadArchive = techPath + name + ".7z"; + + if (fileExists(downloadArchive) == true) { + off_t + fileSize = getFileSize(downloadArchive); + // convert to MB + double + megaBytes = ((double) fileSize / 1048576.0); + printf("%s [download archive %.2fMB]\n", name.c_str(), + megaBytes); + } else { + printf("%s\n", name.c_str()); + } + + if (showfactions == true) { + printf("--> Factions:\n"); + for (unsigned int k = 0; k < results2.size(); ++k) { + string + name2 = results2[k]; + printf("--> %s\n", name2.c_str()); + } + printf("--> Total Factions: " MG_SIZE_T_SPECIFIER "\n", + results2.size()); + break; + } + } + } + } + printf + ("===========================================\nTotal Techtrees: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + + return_value = 0; + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_SCENARIOS]), + &foundParamIndIndex); + } + + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptScenarios, ""); + vector < string > results; + findDirs(pathList, results); + + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemNameFilter = paramPartTokens[1]; + printf("Using filter for scenarios list [%s]\n", + itemNameFilter.c_str()); + + vector < string > filtered; + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + if (itemNameFilter.find("*") != itemNameFilter.npos) { + if (StartsWith + (name, + itemNameFilter.substr(0, + itemNameFilter.find("*"))) == true) { + filtered.push_back(name); + } + } else if (name == itemNameFilter) { + filtered.push_back(name); + } + } + results = filtered; + } + + printf + ("Scenarios found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + printf("%s\n", name.c_str()); + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + + return_value = 0; + } + + else + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LIST_TILESETS]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TILESETS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TILESETS]), + &foundParamIndIndex); + } + + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptTilesets, ""); + vector < string > results; + findDirs(pathList, results); + + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemNameFilter = paramPartTokens[1]; + printf("Using filter for tilesets list [%s]\n", + itemNameFilter.c_str()); + + vector < string > filtered; + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + if (itemNameFilter.find("*") != itemNameFilter.npos) { + if (StartsWith + (name, + itemNameFilter.substr(0, + itemNameFilter.find("*"))) == true) { + filtered.push_back(name); + } + } else if (name == itemNameFilter) { + filtered.push_back(name); + } + } + results = filtered; + } + + printf + ("Tilesets found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + + for (unsigned int j = 0; j < pathList.size(); ++j) { + string + tilesetPath = pathList[j]; + if (tilesetPath != "") { + endPathWithSlash(tilesetPath); + } + if (fileExists(tilesetPath + name + "/" + name + ".xml") == true) { + string + downloadArchive = tilesetPath + name + ".7z"; + if (fileExists(downloadArchive) == true) { + off_t + fileSize = getFileSize(downloadArchive); + // convert to MB + double + megaBytes = ((double) fileSize / 1048576.0); + printf("%s [download archive %.2fMB]\n", name.c_str(), + megaBytes); + } else { + printf("%s\n", name.c_str()); + } + + break; + } + } + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + + return_value = 0; + } + + else + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LIST_TUTORIALS]), + &foundParamIndIndex); + } + + Config & config = Config::getInstance(); + vector < string > pathList = + config.getPathListForType(ptTutorials, ""); + vector < string > results; + findDirs(pathList, results); + + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + itemNameFilter = paramPartTokens[1]; + printf("Using filter for tutorials list [%s]\n", + itemNameFilter.c_str()); + + vector < string > filtered; + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + if (itemNameFilter.find("*") != itemNameFilter.npos) { + if (StartsWith + (name, + itemNameFilter.substr(0, + itemNameFilter.find("*"))) == true) { + filtered.push_back(name); + } + } else if (name == itemNameFilter) { + filtered.push_back(name); + } + } + results = filtered; + } + + printf + ("Tutorials found:\n===========================================\n"); + for (unsigned int i = 0; i < results.size(); ++i) { + string + name = results[i]; + + for (unsigned int j = 0; j < pathList.size(); ++j) { + string + tutorialsPath = pathList[j]; + if (tutorialsPath != "") { + endPathWithSlash(tutorialsPath); + } + if (fileExists(tutorialsPath + name + "/" + name + ".xml") == + true) { + string + downloadArchive = tutorialsPath + name + ".7z"; + if (fileExists(downloadArchive) == true) { + off_t + fileSize = getFileSize(downloadArchive); + // convert to MB + double + megaBytes = ((double) fileSize / 1048576.0); + printf("%s [download archive %.2fMB]\n", name.c_str(), + megaBytes); + } else { + printf("%s\n", name.c_str()); + } + + break; + } + } + } + printf("===========================================\nTotal: " + MG_SIZE_T_SPECIFIER "\n", results.size()); + + return_value = 0; + } + + return return_value; + } + + int + glestMain(int argc, char **argv) { #ifdef SL_LEAK_DUMP -//AllocInfo::set_application_binary(executable_path(argv[0],true)); - string & app = AllocInfo::get_application_binary (); - app = executable_path (argv[0], true); -//want_full_leak_stacktrace = true; -//want_full_leak_stacktrace_line_numbers = true; + //AllocInfo::set_application_binary(executable_path(argv[0],true)); + string & app = AllocInfo::get_application_binary(); + app = executable_path(argv[0], true); + //want_full_leak_stacktrace = true; + //want_full_leak_stacktrace_line_numbers = true; #endif - Thread::setMainThreadId (); -// printf("START ALLOC char 200\n"); -//char *ptr = new char[200]; -// printf("END ALLOC char 200\n"); -// return -1; - SystemFlags::VERBOSE_MODE_ENABLED = false; - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_VERBOSE_MODE]) == - true) - { - SystemFlags::VERBOSE_MODE_ENABLED = true; - Thread::setEnableVerboseMode (true); -//LuaScript::setDebugModeEnabled(true); - } -// DEbug testing threads -//Thread::setEnableVerboseMode(true); + Thread::setMainThreadId(); + // printf("START ALLOC char 200\n"); + //char *ptr = new char[200]; + // printf("END ALLOC char 200\n"); + // return -1; + SystemFlags::VERBOSE_MODE_ENABLED = false; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_VERBOSE_MODE]) == + true) { + SystemFlags::VERBOSE_MODE_ENABLED = true; + Thread::setEnableVerboseMode(true); + //LuaScript::setDebugModeEnabled(true); + } + // DEbug testing threads + //Thread::setEnableVerboseMode(true); - PlatformExceptionHandler::application_binary = - executable_path (argv[0], true); - mg_app_name = GameConstants::application_name; - mailStringSupport = mailString; - SystemFlags::ENABLE_THREADED_LOGGING = false; - disableBacktrace = false; - bool - foundInvalidArgs = false; - preCacheThread = NULL; + PlatformExceptionHandler::application_binary = + executable_path(argv[0], true); + mg_app_name = GameConstants::application_name; + mailStringSupport = mailString; + SystemFlags::ENABLE_THREADED_LOGGING = false; + disableBacktrace = false; + bool + foundInvalidArgs = false; + preCacheThread = NULL; - Properties::setApplicationPath (executable_path (argv[0])); - Properties::setApplicationDataPath (executable_path (argv[0])); - Properties::setGameVersion (glestVersionString); + Properties::setApplicationPath(executable_path(argv[0])); + Properties::setApplicationDataPath(executable_path(argv[0])); + Properties::setGameVersion(glestVersionString); - ServerSocket::setMaxPlayerCount (GameConstants::maxPlayers); + ServerSocket::setMaxPlayerCount(GameConstants::maxPlayers); - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_DISABLE_BACKTRACE]) == true) - { - disableBacktrace = true; - } - PlatformExceptionHandler::disableBacktrace = disableBacktrace; + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_DISABLE_BACKTRACE]) == true) { + disableBacktrace = true; + } + PlatformExceptionHandler::disableBacktrace = disableBacktrace; #if defined(CUSTOM_DATA_INSTALL_PATH) - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\nCUSTOM_DATA_INSTALL_PATH = [%s]\n\n", - formatPath (TOSTRING (CUSTOM_DATA_INSTALL_PATH)).c_str ()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\nCUSTOM_DATA_INSTALL_PATH = [%s]\n\n", + formatPath(TOSTRING(CUSTOM_DATA_INSTALL_PATH)).c_str()); #endif - const int - knownArgCount = sizeof (GAME_ARGS) / sizeof (GAME_ARGS[0]); - for (int idx = 1; idx < argc; ++idx) - { - if (hasCommandArgument - (knownArgCount, (char **) &GAME_ARGS[0], argv[idx], NULL, 0, - true) == false) - { - foundInvalidArgs = true; - printf ("\nInvalid argument: %s", argv[idx]); - } - } + const int + knownArgCount = sizeof(GAME_ARGS) / sizeof(GAME_ARGS[0]); + for (int idx = 1; idx < argc; ++idx) { + if (hasCommandArgument + (knownArgCount, (char **) &GAME_ARGS[0], argv[idx], NULL, 0, + true) == false) { + foundInvalidArgs = true; + printf("\nInvalid argument: %s", argv[idx]); + } + } - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_HELP]) == true || - foundInvalidArgs == true) - { + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_HELP]) == true || + foundInvalidArgs == true) { - printParameterHelp (argv[0], foundInvalidArgs); - return 2; - } + printParameterHelp(argv[0], foundInvalidArgs); + return 2; + } - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) - { -//isMasterServerModeEnabled = true; -//Window::setMasterserverMode(isMasterServerModeEnabled); - GlobalStaticFlags::setIsNonGraphicalModeEnabled (true); + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) { + //isMasterServerModeEnabled = true; + //Window::setMasterserverMode(isMasterServerModeEnabled); + GlobalStaticFlags::setIsNonGraphicalModeEnabled(true); - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - headless_command_list = paramPartTokens[1]; + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + headless_command_list = paramPartTokens[1]; - vector < string > paramHeadlessCommandList; - Tokenize (headless_command_list, paramHeadlessCommandList, ","); + vector < string > paramHeadlessCommandList; + Tokenize(headless_command_list, paramHeadlessCommandList, ","); - for (unsigned int i = 0; i < paramHeadlessCommandList.size (); ++i) - { - string - headless_command = paramHeadlessCommandList[i]; - if (headless_command == "exit") - { - printf ("Forcing quit after game has completed [%s]\n", - headless_command.c_str ()); - Program::setWantShutdownApplicationAfterGame (true); - } - else if (headless_command == "vps") - { - printf ("Disabled reading from console [%s]\n", - headless_command.c_str ()); - disableheadless_console = true; - } - else if (headless_command == "lan") - { - printf ("Forcing local LAN mode [%s]\n", - headless_command.c_str ()); - GlobalStaticFlags::setFlag (gsft_lan_mode); - } - } - } - } + for (unsigned int i = 0; i < paramHeadlessCommandList.size(); ++i) { + string + headless_command = paramHeadlessCommandList[i]; + if (headless_command == "exit") { + printf("Forcing quit after game has completed [%s]\n", + headless_command.c_str()); + Program::setWantShutdownApplicationAfterGame(true); + } else if (headless_command == "vps") { + printf("Disabled reading from console [%s]\n", + headless_command.c_str()); + disableheadless_console = true; + } else if (headless_command == "lan") { + printf("Forcing local LAN mode [%s]\n", + headless_command.c_str()); + GlobalStaticFlags::setFlag(gsft_lan_mode); + } + } + } + } - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SERVER_TITLE]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SERVER_TITLE]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SERVER_TITLE]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - Config & config = Config::getInstance (); - string - serverTitle = paramPartTokens[1]; - printf ("Forcing serverTitle[%s]\n", serverTitle.c_str ()); + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SERVER_TITLE]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SERVER_TITLE]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SERVER_TITLE]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + Config & config = Config::getInstance(); + string + serverTitle = paramPartTokens[1]; + printf("Forcing serverTitle[%s]\n", serverTitle.c_str()); - config.setString ("ServerTitle", serverTitle, true); - } - else - { - printf - ("\nInvalid missing server title specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); + config.setString("ServerTitle", serverTitle, true); + } else { + printf + ("\nInvalid missing server title specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); - return 1; - } - } + return 1; + } + } -//#ifdef WIN32 -//SocketManager winSockManager; -//#endif + //#ifdef WIN32 + //SocketManager winSockManager; + //#endif - bool - haveSpecialOutputCommandLineOption = false; + bool + haveSpecialOutputCommandLineOption = false; - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SDL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LUA_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CURL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_XERCES_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VERSION]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_MAPS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == - true) - { - haveSpecialOutputCommandLineOption = true; - } + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SDL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LUA_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CURL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_XERCES_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VERSION]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_MAPS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == + true) { + haveSpecialOutputCommandLineOption = true; + } - if (haveSpecialOutputCommandLineOption == false || - hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VERSION]) == true) - { - printf ("%s %s", extractFileFromDirectoryPath (argv[0]).c_str (), - getNetworkPlatformFreeVersionString ().c_str ()); -// printf("\nCompiled using: %s on: %s platform: %s endianness: %s",getCompilerNameString().c_str(),getCompileDateTime().c_str(),getPlatformNameString().c_str(),(::Shared::PlatformByteOrder::isBigEndian() == true ? "big" : "little")); - printf ("\nCompiled using: %s platform: %s endianness: %s", - getCompilerNameString ().c_str (), - getPlatformNameString ().c_str (), - (::Shared::PlatformByteOrder::isBigEndian () == - true ? "big" : "little")); + if (haveSpecialOutputCommandLineOption == false || + hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VERSION]) == true) { + printf("%s %s", extractFileFromDirectoryPath(argv[0]).c_str(), + getNetworkPlatformFreeVersionString().c_str()); + // printf("\nCompiled using: %s on: %s platform: %s endianness: %s",getCompilerNameString().c_str(),getCompileDateTime().c_str(),getPlatformNameString().c_str(),(::Shared::PlatformByteOrder::isBigEndian() == true ? "big" : "little")); + printf("\nCompiled using: %s platform: %s endianness: %s", + getCompilerNameString().c_str(), + getPlatformNameString().c_str(), + (::Shared::PlatformByteOrder::isBigEndian() == + true ? "big" : "little")); -// printf("\n\nData type sizes int8 = " MG_SIZE_T_SPECIFIER " int16 = " MG_SIZE_T_SPECIFIER " int32 = " MG_SIZE_T_SPECIFIER " int64 = " MG_SIZE_T_SPECIFIER "\n\n",sizeof(int8),sizeof(int16),sizeof(int32),sizeof(int64)); -// -// Config::getInstance().setBool("DebugNetworkPackets",true,true); -// NetworkMessageIntro data(424336, "mg_version_x","player_x", 3, nmgstOk,444444, 555555, "english"); -// unsigned char *buf = data.packMessage(); -// printf("\nSend packet size = %u\n%s\n",data.getPackedSize(),data.toString().c_str()); -// data.dump_packet("Send data", buf, data.getPackedSize()); -// //delete [] buf; -// -// NetworkMessageIntro data2; -// data2.unpackMessage(buf); -// printf("\nReceive packet size = %u\n%s\n",data2.getPackedSize(),data2.toString().c_str()); -// data2.dump_packet("nReceive data", buf, data2.getPackedSize()); -// delete [] buf; + // printf("\n\nData type sizes int8 = " MG_SIZE_T_SPECIFIER " int16 = " MG_SIZE_T_SPECIFIER " int32 = " MG_SIZE_T_SPECIFIER " int64 = " MG_SIZE_T_SPECIFIER "\n\n",sizeof(int8),sizeof(int16),sizeof(int32),sizeof(int64)); + // + // Config::getInstance().setBool("DebugNetworkPackets",true,true); + // NetworkMessageIntro data(424336, "mg_version_x","player_x", 3, nmgstOk,444444, 555555, "english"); + // unsigned char *buf = data.packMessage(); + // printf("\nSend packet size = %u\n%s\n",data.getPackedSize(),data.toString().c_str()); + // data.dump_packet("Send data", buf, data.getPackedSize()); + // //delete [] buf; + // + // NetworkMessageIntro data2; + // data2.unpackMessage(buf); + // printf("\nReceive packet size = %u\n%s\n",data2.getPackedSize(),data2.toString().c_str()); + // data2.dump_packet("nReceive data", buf, data2.getPackedSize()); + // delete [] buf; -// SwitchSetupRequest data("factionname", 3,-1,2,"softcoder",10, 11,"eng"); -// -// unsigned char *buf = data.packMessage(); -// printf("\nSend packet size = %u\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d [%s] [%s] %d %d\n",data.getPackedSize(),buf,data.getToTeam(),data.getSelectedFactionName().c_str(),data.getCurrentFactionIndex(),data.getToFactionIndex(),data.getNetworkPlayerLanguage().c_str(),data.getNetworkPlayerName().c_str(),data.getNetworkPlayerStatus(),data.getSwitchFlags()); -// //delete [] buf; -// -// data.unpackMessage(buf); -// printf("\nGot packet size = %u\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d [%s] [%s] %d %d\n",data.getPackedSize(),buf,data.getToTeam(),data.getSelectedFactionName().c_str(),data.getCurrentFactionIndex(),data.getToFactionIndex(),data.getNetworkPlayerLanguage().c_str(),data.getNetworkPlayerName().c_str(),data.getNetworkPlayerStatus(),data.getSwitchFlags()); -// delete [] buf; + // SwitchSetupRequest data("factionname", 3,-1,2,"softcoder",10, 11,"eng"); + // + // unsigned char *buf = data.packMessage(); + // printf("\nSend packet size = %u\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d [%s] [%s] %d %d\n",data.getPackedSize(),buf,data.getToTeam(),data.getSelectedFactionName().c_str(),data.getCurrentFactionIndex(),data.getToFactionIndex(),data.getNetworkPlayerLanguage().c_str(),data.getNetworkPlayerName().c_str(),data.getNetworkPlayerStatus(),data.getSwitchFlags()); + // //delete [] buf; + // + // data.unpackMessage(buf); + // printf("\nGot packet size = %u\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d [%s] [%s] %d %d\n",data.getPackedSize(),buf,data.getToTeam(),data.getSelectedFactionName().c_str(),data.getCurrentFactionIndex(),data.getToFactionIndex(),data.getNetworkPlayerLanguage().c_str(),data.getNetworkPlayerName().c_str(),data.getNetworkPlayerStatus(),data.getSwitchFlags()); + // delete [] buf; -// int8 a = 1; -// uint8 b = 2; -// int16 c = 3; -// uint16 d = 4; -// int32 e = 5; -// uint32 f = 6; -// -// printf("\nPack test #1: [%d][%u][%d][%u][%d][%u]\n,",a,b,c,d,e,f); -// -// unsigned char *buf = new unsigned char[100]; -// unsigned int packedsize = pack(buf, "cChHlL", -// a, -// b, -// c, -// d, -// e, -// f); -// -// printf("Pack test #2: [%u][%s]\n,",packedsize,buf); -// -// int8 a1 = 0; -// uint8 b1 = 0; -// int16 c1 = 0; -// uint16 d1 = 0; -// int32 e1 = 0; -// uint32 f1 = 0; -// -// unpack(buf, "cChHlL", -// &a1, -// &b1, -// &c1, -// &d1, -// &e1, -// &f1); -// -// printf("UnPack test #3: [%d][%u][%d][%u][%d][%u]\n,",a1,b1,c1,d1,e1,f1); + // int8 a = 1; + // uint8 b = 2; + // int16 c = 3; + // uint16 d = 4; + // int32 e = 5; + // uint32 f = 6; + // + // printf("\nPack test #1: [%d][%u][%d][%u][%d][%u]\n,",a,b,c,d,e,f); + // + // unsigned char *buf = new unsigned char[100]; + // unsigned int packedsize = pack(buf, "cChHlL", + // a, + // b, + // c, + // d, + // e, + // f); + // + // printf("Pack test #2: [%u][%s]\n,",packedsize,buf); + // + // int8 a1 = 0; + // uint8 b1 = 0; + // int16 c1 = 0; + // uint16 d1 = 0; + // int32 e1 = 0; + // uint32 f1 = 0; + // + // unpack(buf, "cChHlL", + // &a1, + // &b1, + // &c1, + // &d1, + // &e1, + // &f1); + // + // printf("UnPack test #3: [%d][%u][%d][%u][%d][%u]\n,",a1,b1,c1,d1,e1,f1); - if (SystemFlags::VERBOSE_MODE_ENABLED == true) - { - int8 - testVar = 111; - printf ("\nEndian value = %d", testVar); - testVar =::Shared::PlatformByteOrder::toCommonEndian (testVar); - printf ("\nEndian to common value = %d", testVar); - testVar =::Shared::PlatformByteOrder::fromCommonEndian (testVar); - printf ("\nEndian from common value = %d", testVar); + if (SystemFlags::VERBOSE_MODE_ENABLED == true) { + int8 + testVar = 111; + printf("\nEndian value = %d", testVar); + testVar = ::Shared::PlatformByteOrder::toCommonEndian(testVar); + printf("\nEndian to common value = %d", testVar); + testVar = ::Shared::PlatformByteOrder::fromCommonEndian(testVar); + printf("\nEndian from common value = %d", testVar); - printf ("\nint8 sizeof = " MG_SIZE_T_SPECIFIER "", sizeof (int8)); - printf ("\nSwitchSetupRequest sizeof = " MG_SIZE_T_SPECIFIER "", - SwitchSetupRequest ().getDataSize ()); - } + printf("\nint8 sizeof = " MG_SIZE_T_SPECIFIER "", sizeof(int8)); + printf("\nSwitchSetupRequest sizeof = " MG_SIZE_T_SPECIFIER "", + SwitchSetupRequest().getDataSize()); + } - printf ("\nversion: [%s]", glestVersionString.c_str()); + printf("\nversion: [%s]", glestVersionString.c_str()); #ifdef USE_STREFLOP # if defined(STREFLOP_SSE) - const char * - instruction_set = "[SSE]"; + const char * + instruction_set = "[SSE]"; # elif defined(STREFLOP_X87) - const char * - instruction_set = "[X87]"; + const char * + instruction_set = "[X87]"; # elif defined(STREFLOP_SOFT) - const char * - instruction_set = "[SOFTFLOAT]"; + const char * + instruction_set = "[SOFTFLOAT]"; # else - const char * - instruction_set = "[none]"; + const char * + instruction_set = "[none]"; # endif # if defined(STREFLOP_NO_DENORMALS) - const char * - denormals = "[no-denormals]"; + const char * + denormals = "[no-denormals]"; # else - const char * - denormals = "[denormals]"; + const char * + denormals = "[denormals]"; # endif - printf (" - using STREFLOP %s - %s\n", instruction_set, denormals); + printf(" - using STREFLOP %s - %s\n", instruction_set, denormals); #else - printf ("\n"); + printf("\n"); #endif - } + } - setGameVersion (glestVersionString); + setGameVersion(glestVersionString); #ifdef WIN32 - CheckPacketThrottling (); + CheckPacketThrottling(); #endif - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SDL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LUA_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CURL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_XERCES_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VERSION]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_MAPS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == - true) - { - VideoPlayer::setDisabled (true); - } + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SDL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LUA_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CURL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_XERCES_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VERSION]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_MAPS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == + true) { + VideoPlayer::setDisabled(true); + } -//throw megaglest_runtime_error("Test!"); + //throw megaglest_runtime_error("Test!"); - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SDL_INFO]) == - true) - { - SDL_version - ver; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SDL_INFO]) == + true) { + SDL_version + ver; -// Prints the compile time version - SDL_VERSION (&ver); - print_SDL_version ("SDL compile-time version", &ver); + // Prints the compile time version + SDL_VERSION(&ver); + print_SDL_version("SDL compile-time version", &ver); -// Prints the run-time version - SDL_GetVersion (&ver); - print_SDL_version ("SDL runtime version", &ver); -//const SDL_VideoInfo *vidInfo = SDL_GetVideoInfo(); -//printf("Video card Memory: %u\n",vidInfo->video_mem); - } + // Prints the run-time version + SDL_GetVersion(&ver); + print_SDL_version("SDL runtime version", &ver); + //const SDL_VideoInfo *vidInfo = SDL_GetVideoInfo(); + //printf("Video card Memory: %u\n",vidInfo->video_mem); + } - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LUA_INFO]) == - true) - { - printf ("LUA version: %s\n", LUA_RELEASE); - } + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LUA_INFO]) == + true) { + printf("LUA version: %s\n", LUA_RELEASE); + } - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_CURL_INFO]) == - true) - { - curl_version_info_data * - curlVersion = curl_version_info (CURLVERSION_NOW); - printf ("CURL version: %s [%s] SSL enabled: %d\n", - curlVersion->version, - (curlVersion->ssl_version != - NULL ? curlVersion->ssl_version : ""), - ((curlVersion->features & CURL_VERSION_SSL) == - CURL_VERSION_SSL ? true : false)); - if (curlVersion->protocols != NULL - && curlVersion->protocols[0] != NULL) - { - printf ("protocols: "); - for (unsigned int i = 0; - curlVersion->protocols != NULL - && curlVersion->protocols[i] != NULL; ++i) - { - printf ("%s ", curlVersion->protocols[i]); - if (i > 0 && i % 10 == 0) - { - printf ("\n "); - } - } - printf ("\n"); - } - } + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_CURL_INFO]) == + true) { + curl_version_info_data * + curlVersion = curl_version_info(CURLVERSION_NOW); + printf("CURL version: %s [%s] SSL enabled: %d\n", + curlVersion->version, + (curlVersion->ssl_version != + NULL ? curlVersion->ssl_version : ""), + ((curlVersion->features & CURL_VERSION_SSL) == + CURL_VERSION_SSL ? true : false)); + if (curlVersion->protocols != NULL + && curlVersion->protocols[0] != NULL) { + printf("protocols: "); + for (unsigned int i = 0; + curlVersion->protocols != NULL + && curlVersion->protocols[i] != NULL; ++i) { + printf("%s ", curlVersion->protocols[i]); + if (i > 0 && i % 10 == 0) { + printf("\n "); + } + } + printf("\n"); + } + } #if defined(WANT_XERCES) - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_XERCES_INFO]) == - true) - { - printf ("XERCES version: %s\n", XERCES_FULLVERSIONDOT); - } + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_XERCES_INFO]) == + true) { + printf("XERCES version: %s\n", XERCES_FULLVERSIONDOT); + } #endif - if ((hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_VERSION]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SDL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LUA_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CURL_INFO]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_XERCES_INFO]) == true) - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_OPENGL_INFO]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == - false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == - false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == - false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == - false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == - false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_MAPS]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TILESETS]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == false - && hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == - false) - { - return 0; - } - - if (hasCommandArgument (argc, argv, string (GAME_ARGS[GAME_ARG_MOD])) == - true) - { - - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_MOD]) + string ("="), - &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, string (GAME_ARGS[GAME_ARG_MOD]), - &foundParamIndIndex); - } - string - scenarioName = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (scenarioName, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 && paramPartTokens[1].length () > 0) - { - string - autoloadModName = paramPartTokens[1]; - if (Properties::applyTagsToValue (autoloadModName) == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Property key [%s] now has value [%s]\n", - Config::ACTIVE_MOD_PROPERTY_NAME, - autoloadModName.c_str ()); - } - - Config::setCustomRuntimeProperty (Config::ACTIVE_MOD_PROPERTY_NAME, - autoloadModName); - - printf ("Setting mod active [%s]\n", autoloadModName.c_str ()); - } - else - { - printf - ("\nInvalid mod pathname specified on commandline [%s] mod [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - return 1; - } - } - - SystemFlags::init (haveSpecialOutputCommandLineOption); -//SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = true; -//SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = true; - - MainWindow * - mainWindow = NULL; - Program * - program = NULL; - ExceptionHandler - exceptionHandler; - exceptionHandler.install (getCrashDumpFileName ()); - - int - shutdownFadeSoundMilliseconds = 1000; - Chrono - chronoshutdownFadeSound; - SimpleTaskThread * - soundThreadManager = NULL; - - try - { -// Setup paths to game items (like data, logs, ini etc) - int - setupResult = setupGameItemPaths (argc, argv, NULL); - if (setupResult != 0) - { - return setupResult; - } - -// Attempt to read ini files - Config & config = Config::getInstance (); - setupGameItemPaths (argc, argv, &config); - - setupSteamSettings (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_STEAM]), - hasCommandArgument (argc, argv, - GAME_ARGS - [GAME_ARG_STEAM_RESET_STATS]), - hasCommandArgument (argc, argv, - GAME_ARGS - [GAME_ARG_STEAM_DEBUG])); - - if (config.getString ("PlayerId", "") == "") - { - char - uuid_str[38]; - get_uuid_string (uuid_str, sizeof (uuid_str)); - - config.setString ("PlayerId", uuid_str); - config.save (); - } - -//printf("Players UUID: [%s]\n",config.getString("PlayerId","").c_str()); - - if (config.getBool ("DisableLuaSandbox", "false") == true) - { - LuaScript::setDisableSandbox (true); - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKETS]) == true) - { - printf ("*NOTE: debugging network packets.\n"); - config.setBool ("DebugNetworkPackets", true, true); - } - - if (hasCommandArgument - (argc, argv, - GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKET_SIZES]) == true) - { - printf ("*NOTE: debugging network packet SIZES.\n"); - config.setBool ("DebugNetworkPacketSizes", true, true); - } - - if (hasCommandArgument - (argc, argv, - GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKET_STATS]) == true) - { - printf ("*NOTE: debugging network packet STATISTICS.\n"); - config.setBool ("DebugNetworkPacketStats", true, true); - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_ENABLE_NEW_PROTOCOL]) == true) - { - printf ("*NOTE: enabling new network protocol.\n"); - NetworkMessage::useOldProtocol = false; - } - - Socket::setBroadCastPort (config.getInt ("BroadcastPort", - intToStr - (Socket::getBroadCastPort - ()).c_str ())); - - Socket::disableNagle = config.getBool ("DisableNagle", "false"); - if (Socket::disableNagle) - { - printf - ("*WARNING users wants to disable the socket nagle algorithm.\n"); - } - Socket::DEFAULT_SOCKET_SENDBUF_SIZE = - config.getInt ("DefaultSocketSendBufferSize", - intToStr (Socket::DEFAULT_SOCKET_SENDBUF_SIZE). - c_str ()); - if (Socket::DEFAULT_SOCKET_SENDBUF_SIZE >= 0) - { - printf - ("*WARNING users wants to set default socket send buffer size to: %d\n", - Socket::DEFAULT_SOCKET_SENDBUF_SIZE); - } - Socket::DEFAULT_SOCKET_RECVBUF_SIZE = - config.getInt ("DefaultSocketReceiveBufferSize", - intToStr (Socket::DEFAULT_SOCKET_RECVBUF_SIZE). - c_str ()); - if (Socket::DEFAULT_SOCKET_RECVBUF_SIZE >= 0) - { - printf - ("*WARNING users wants to set default socket receive buffer size to: %d\n", - Socket::DEFAULT_SOCKET_RECVBUF_SIZE); - } - - shutdownFadeSoundMilliseconds = - config.getInt ("ShutdownFadeSoundMilliseconds", - intToStr (shutdownFadeSoundMilliseconds).c_str ()); - - string - userData = config.getString ("UserData_Root", ""); - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - userData = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey); - } - if (userData != "") - { - endPathWithSlash (userData); - } - - string - data_path_check = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - string - userDataPath_check = - getGameCustomCoreDataPath (data_path_check, ""); - if (data_path_check == userDataPath_check) - { - printf - ("****WARNING**** your game data path and user data path are the same.\nThis will likely create problems: %s\n", - data_path_check.c_str ()); - throw - megaglest_runtime_error - ("Regular and User data paths cannot have the same value [" + - userDataPath_check + "]"); - } - - if (userData != "") - { - if (isdir (userData.c_str ()) == false) - { - createDirectoryPaths (userData); - } - } - string - crcCachePath = userData + "cache/"; - if (isdir (crcCachePath.c_str ()) == false) - { - createDirectoryPaths (crcCachePath); - } - setCRCCacheFilePath (crcCachePath); - - string - savedGamePath = userData + "saved/"; - if (isdir (savedGamePath.c_str ()) == false) - { - createDirectoryPaths (savedGamePath); - } - - string - tempDataPath = userData + "temp/"; - tempDataLocation = tempDataPath; - if (isdir (tempDataPath.c_str ()) == true) - { - removeFolder (tempDataPath); - } - createDirectoryPaths (tempDataPath); - - string - binaryNameOld = - Properties::getApplicationPath () + - extractFileFromDirectoryPath - (PlatformExceptionHandler::application_binary) + "__REMOVE"; - if (fileExists (binaryNameOld)) - { - removeFile (binaryNameOld); - } - - string - netInterfaces = config.getString ("NetworkInterfaces", ""); - if (netInterfaces != "") - { -//printf("Using network interfaces: %s\n",netInterfaces.c_str()); - std::vector < std::string > intfList; - Tokenize (netInterfaces, intfList, ","); - Socket::setIntfTypes (intfList); - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_USE_PORTS]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_PORTS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_PORTS]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - portsToUse = paramPartTokens[1]; - - vector < string > paramPartPortsTokens; - Tokenize (portsToUse, paramPartPortsTokens, ","); - if (paramPartPortsTokens.size () >= 2 - && paramPartPortsTokens[1].length () > 0) - { - int - internalPort = strToInt (paramPartPortsTokens[0]); - int - externalPort = strToInt (paramPartPortsTokens[1]); - - printf ("Forcing internal port# %d, external port# %d\n", - internalPort, externalPort); - - config.setInt ("PortServer", internalPort, true); - config.setInt ("PortExternal", externalPort, true); - config.setInt ("FTPServerPort", internalPort + 1, true); - - if (paramPartPortsTokens.size () >= 3 - && paramPartPortsTokens[2].length () > 0) - { - int - statusPort = strToInt (paramPartPortsTokens[2]); - - printf ("Forcing status port# %d\n", statusPort); - - config.setInt ("ServerAdminPort", statusPort, true); - } - } - else - { - printf - ("\nInvalid ports specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - else - { - printf - ("\nInvalid missing ports specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_STATUS])) == true) - { - Ip - ip ("localhost"); - int - port = Config::getInstance ().getInt ("ServerAdminPort", - intToStr - (GameConstants:: - serverAdminPort).c_str ()); - ClientSocket - clientSocket; - clientSocket.setBlock (false); - clientSocket.connect (ip, port); - if (clientSocket.isConnected () == true) - { - clientSocket.setBlock (true); - - char - szBuf[8096] = ""; - clientSocket.receive (&szBuf[0], 8095, false); - std::cout << szBuf << std::endl; - } - else - { - std::cout << - "Could not connect (possibly no clients connected) to host: " << - ip.getString () << " port: " << port << std::endl; - } - - return 0; - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_DISABLE_SOUND]) - == true - || hasCommandArgument (argc, argv, - string (GAME_ARGS - [GAME_ARG_MASTERSERVER_MODE])) == - true) - { - config.setString ("FactorySound", "None", true); - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) - { -//Logger::getInstance().setMasterserverMode(true); -//Model::setMasterserverMode(true); -//Shared::Sound::Sound::setMasterserverMode(true); - } - } - - if (hasCommandArgument - (argc, argv, - GAME_ARGS[GAME_ARG_DISABLE_OPENGL_CAPS_CHECK]) == true - || config.getBool ("CheckGlCaps") == false) - { - printf ("**WARNING** disabling opengl capability checking...\n"); - config.setBool ("CheckGlCaps", false, true); - } - - bool - enableATIHacks = config.getBool ("EnableATIHacks", "false"); - if (enableATIHacks == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("**WARNING** Enabling ATI video card hacks\n"); - TextureGl::setEnableATIHacks (enableATIHacks); - } - - Renderer::renderText3DEnabled = - config.getBool ("Enable3DFontRendering", - intToStr (Renderer::renderText3DEnabled).c_str ()); - - if (config.getBool ("EnableLegacyFonts", "false") == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_ENABLE_LEGACYFONTS]) == - true) - { - ::Shared::Graphics::Font::forceLegacyFonts = true; - Renderer::renderText3DEnabled = false; - printf ("**WARNING** Forcing Legacy Fonts Enabled\n"); - } - else - { - Renderer::renderText3DEnabled = - config.getBool ("Enable3DFontRendering", - intToStr (Renderer::renderText3DEnabled). - c_str ()); - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_USE_RESOLUTION]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_RESOLUTION]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_RESOLUTION]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - settings = paramPartTokens[1]; - printf ("Forcing resolution [%s]\n", settings.c_str ()); - - paramPartTokens.clear (); - Tokenize (settings, paramPartTokens, "x"); - if (paramPartTokens.size () >= 2) - { - int - newScreenWidth = strToInt (paramPartTokens[0]); - config.setInt ("ScreenWidth", newScreenWidth, true); - - int - newScreenHeight = strToInt (paramPartTokens[1]); - config.setInt ("ScreenHeight", newScreenHeight, true); - } - else - { - printf - ("\nInvalid missing resolution settings specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - else - { - printf - ("\nInvalid missing resolution setting specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_USE_COLORBITS]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_COLORBITS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_COLORBITS]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - settings = paramPartTokens[1]; - printf ("Forcing colorbits [%s]\n", settings.c_str ()); - - int - newColorBits = strToInt (settings); - config.setInt ("ColorBits", newColorBits, true); - } - else - { - printf - ("\nInvalid missing colorbits settings specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_USE_DEPTHBITS]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_DEPTHBITS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_DEPTHBITS]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - settings = paramPartTokens[1]; - printf ("Forcing depthbits [%s]\n", settings.c_str ()); - - int - newDepthBits = strToInt (settings); - config.setInt ("DepthBits", newDepthBits, true); - } - else - { - printf - ("\nInvalid missing depthbits setting specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_USE_FULLSCREEN]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_FULLSCREEN]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_FULLSCREEN]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - settings = paramPartTokens[1]; - printf ("Forcing fullscreen [%s]\n", settings.c_str ()); - - bool - newFullScreenMode = strToBool (settings); - config.setBool ("Windowed", !newFullScreenMode, true); - } - else - { - printf - ("\nInvalid missing fullscreen setting specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SET_GAMMA]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SET_GAMMA]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_SET_GAMMA]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - settings = paramPartTokens[1]; - printf ("Forcing gamma [%s]\n", settings.c_str ()); - - float - newGammaValue = strToFloat (settings); - config.setFloat ("GammaValue", newGammaValue, true); - } - else - { - printf - ("\nInvalid missing gamma setting specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_DISABLE_VIDEOS]) == true) - { - VideoPlayer::setDisabled (true); - } - else if (config.getBool ("EnableVideos", "true") == false) - { - VideoPlayer::setDisabled (true); - } - -// Set some statics based on ini entries - SystemFlags::ENABLE_THREADED_LOGGING = - config.getBool ("ThreadedLogging", "true"); - FontGl::setDefault_fontType (config.getString ("DefaultFont", - FontGl:: - getDefault_fontType (). - c_str ())); - UPNP_Tools::isUPNP = !config.getBool ("DisableUPNP", "false"); - Texture::useTextureCompression = - config.getBool ("EnableTextureCompression", "false"); - -// 256 for English -// 30000 for Chinese - ::Shared::Graphics::Font::charCount = - config.getInt ("FONT_CHARCOUNT", - intToStr (::Shared::Graphics::Font::charCount). - c_str ()); - ::Shared::Graphics::Font::fontTypeName = - config. - getString ("FONT_TYPENAME",::Shared::Graphics::Font::fontTypeName. - c_str ()); - ::Shared::Graphics::Font::fontIsMultibyte = - config.getBool ("FONT_MULTIBYTE", - intToStr (::Shared::Graphics::Font:: - fontIsMultibyte).c_str ()); - ::Shared::Graphics::Font::fontIsRightToLeft = - config.getBool ("FONT_RIGHTTOLEFT", - intToStr (::Shared::Graphics::Font:: - fontIsRightToLeft).c_str ()); - ::Shared::Graphics::Font::baseSize = - config.getInt ("FONT_BASE_SIZE", - intToStr (::Shared::Graphics::Font::baseSize). - c_str ()); - ::Shared::Graphics::Font::scaleFontValue = - config.getFloat ("FONT_SCALE_SIZE", - floatToStr (::Shared::Graphics::Font:: - scaleFontValue).c_str ()); - ::Shared::Graphics::Font::scaleFontValueCenterHFactor = - config.getFloat ("FONT_SCALE_CENTERH_FACTOR", - floatToStr (::Shared::Graphics::Font:: - scaleFontValueCenterHFactor).c_str ()); - ::Shared::Graphics::Font::langHeightText = - config. - getString ("FONT_HEIGHT_TEXT",::Shared::Graphics::Font:: - langHeightText.c_str ()); - ::Shared::Graphics::Font::fontSupportMixedRightToLeft = - config.getBool ("FONT_RIGHTTOLEFT_MIXED_SUPPORT", - intToStr (::Shared::Graphics::Font:: - fontSupportMixedRightToLeft).c_str ()); - -// Example values: -// DEFAULT_CHARSET (English) = 1 -// GB2312_CHARSET (Chinese) = 134 - ::Shared::Platform::PlatformContextGl::charSet = - config.getInt ("FONT_CHARSET", - intToStr (::Shared::Platform::PlatformContextGl:: - charSet).c_str ()); - if (config.getBool ("No2DMouseRendering", "false") == false) - { - showCursor (false); -//showWindowCursorState = false; - } - if (config.getInt ("DEFAULT_HTTP_TIMEOUT", - intToStr (SystemFlags:: - DEFAULT_HTTP_TIMEOUT).c_str ()) >= 0) - { - SystemFlags::DEFAULT_HTTP_TIMEOUT = - config.getInt ("DEFAULT_HTTP_TIMEOUT", - intToStr (SystemFlags::DEFAULT_HTTP_TIMEOUT). - c_str ()); - } - - bool - allowAltEnterFullscreenToggle = - config.getBool ("AllowAltEnterFullscreenToggle", - boolToStr (::Shared::Platform::Window:: - getAllowAltEnterFullscreenToggle ()). - c_str ()); - ::Shared::Platform::Window:: - setAllowAltEnterFullscreenToggle (allowAltEnterFullscreenToggle); - - if (config.getBool ("noTeamColors", "false") == true) - { - MeshCallbackTeamColor::noTeamColors = true; - } - - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LUA_DEBUG]) == - true) - { - printf ("Forcing LUA debugging enabled!\n"); - config.setBool ("DebugLUA", true, true); - } - -// Setup debug logging etc - setupLogging (config, haveSpecialOutputCommandLineOption); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, fontIsRightToLeft = %d\n", - __FILE__, __FUNCTION__, - __LINE__,::Shared::Graphics::Font:: - charCount,::Shared::Graphics::Font:: - fontTypeName. - c_str (),::Shared::Platform:: - PlatformContextGl::charSet,::Shared:: - Graphics::Font::fontIsMultibyte,::Shared:: - Graphics::Font::fontIsRightToLeft); - - NetworkInterface::setDisplayMessageFunction (ExceptionHandler:: - DisplayMessage); - MenuStateMasterserver::setDisplayMessageFunction (ExceptionHandler:: - DisplayMessage); + if ((hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_VERSION]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SDL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LUA_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CURL_INFO]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_XERCES_INFO]) == true) + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_OPENGL_INFO]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == + false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == + false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == + false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == + false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == + false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_MAPS]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TILESETS]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == false + && hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == + false) { + return 0; + } + + if (hasCommandArgument(argc, argv, string(GAME_ARGS[GAME_ARG_MOD])) == + true) { + + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_MOD]) + string("="), + &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, string(GAME_ARGS[GAME_ARG_MOD]), + &foundParamIndIndex); + } + string + scenarioName = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(scenarioName, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 && paramPartTokens[1].length() > 0) { + string + autoloadModName = paramPartTokens[1]; + if (Properties::applyTagsToValue(autoloadModName) == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Property key [%s] now has value [%s]\n", + Config::ACTIVE_MOD_PROPERTY_NAME, + autoloadModName.c_str()); + } + + Config::setCustomRuntimeProperty(Config::ACTIVE_MOD_PROPERTY_NAME, + autoloadModName); + + printf("Setting mod active [%s]\n", autoloadModName.c_str()); + } else { + printf + ("\nInvalid mod pathname specified on commandline [%s] mod [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + return 1; + } + } + + SystemFlags::init(haveSpecialOutputCommandLineOption); + //SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled = true; + //SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled = true; + + MainWindow * + mainWindow = NULL; + Program * + program = NULL; + ExceptionHandler + exceptionHandler; + exceptionHandler.install(getCrashDumpFileName()); + + int + shutdownFadeSoundMilliseconds = 1000; + Chrono + chronoshutdownFadeSound; + SimpleTaskThread * + soundThreadManager = NULL; + + try { + // Setup paths to game items (like data, logs, ini etc) + int + setupResult = setupGameItemPaths(argc, argv, NULL); + if (setupResult != 0) { + return setupResult; + } + + // Attempt to read ini files + Config & config = Config::getInstance(); + setupGameItemPaths(argc, argv, &config); + + setupSteamSettings(hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_STEAM]), + hasCommandArgument(argc, argv, + GAME_ARGS + [GAME_ARG_STEAM_RESET_STATS]), + hasCommandArgument(argc, argv, + GAME_ARGS + [GAME_ARG_STEAM_DEBUG])); + + if (config.getString("PlayerId", "") == "") { + char + uuid_str[38]; + get_uuid_string(uuid_str, sizeof(uuid_str)); + + config.setString("PlayerId", uuid_str); + config.save(); + } + + //printf("Players UUID: [%s]\n",config.getString("PlayerId","").c_str()); + + if (config.getBool("DisableLuaSandbox", "false") == true) { + LuaScript::setDisableSandbox(true); + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKETS]) == true) { + printf("*NOTE: debugging network packets.\n"); + config.setBool("DebugNetworkPackets", true, true); + } + + if (hasCommandArgument + (argc, argv, + GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKET_SIZES]) == true) { + printf("*NOTE: debugging network packet SIZES.\n"); + config.setBool("DebugNetworkPacketSizes", true, true); + } + + if (hasCommandArgument + (argc, argv, + GAME_ARGS[GAME_ARG_DEBUG_NETWORK_PACKET_STATS]) == true) { + printf("*NOTE: debugging network packet STATISTICS.\n"); + config.setBool("DebugNetworkPacketStats", true, true); + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_ENABLE_NEW_PROTOCOL]) == true) { + printf("*NOTE: enabling new network protocol.\n"); + NetworkMessage::useOldProtocol = false; + } + + Socket::setBroadCastPort(config.getInt("BroadcastPort", + intToStr + (Socket::getBroadCastPort + ()).c_str())); + + Socket::disableNagle = config.getBool("DisableNagle", "false"); + if (Socket::disableNagle) { + printf + ("*WARNING users wants to disable the socket nagle algorithm.\n"); + } + Socket::DEFAULT_SOCKET_SENDBUF_SIZE = + config.getInt("DefaultSocketSendBufferSize", + intToStr(Socket::DEFAULT_SOCKET_SENDBUF_SIZE). + c_str()); + if (Socket::DEFAULT_SOCKET_SENDBUF_SIZE >= 0) { + printf + ("*WARNING users wants to set default socket send buffer size to: %d\n", + Socket::DEFAULT_SOCKET_SENDBUF_SIZE); + } + Socket::DEFAULT_SOCKET_RECVBUF_SIZE = + config.getInt("DefaultSocketReceiveBufferSize", + intToStr(Socket::DEFAULT_SOCKET_RECVBUF_SIZE). + c_str()); + if (Socket::DEFAULT_SOCKET_RECVBUF_SIZE >= 0) { + printf + ("*WARNING users wants to set default socket receive buffer size to: %d\n", + Socket::DEFAULT_SOCKET_RECVBUF_SIZE); + } + + shutdownFadeSoundMilliseconds = + config.getInt("ShutdownFadeSoundMilliseconds", + intToStr(shutdownFadeSoundMilliseconds).c_str()); + + string + userData = config.getString("UserData_Root", ""); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + userData = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey); + } + if (userData != "") { + endPathWithSlash(userData); + } + + string + data_path_check = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + string + userDataPath_check = + getGameCustomCoreDataPath(data_path_check, ""); + if (data_path_check == userDataPath_check) { + printf + ("****WARNING**** your game data path and user data path are the same.\nThis will likely create problems: %s\n", + data_path_check.c_str()); + throw + megaglest_runtime_error + ("Regular and User data paths cannot have the same value [" + + userDataPath_check + "]"); + } + + if (userData != "") { + if (isdir(userData.c_str()) == false) { + createDirectoryPaths(userData); + } + } + string + crcCachePath = userData + "cache/"; + if (isdir(crcCachePath.c_str()) == false) { + createDirectoryPaths(crcCachePath); + } + setCRCCacheFilePath(crcCachePath); + + string + savedGamePath = userData + "saved/"; + if (isdir(savedGamePath.c_str()) == false) { + createDirectoryPaths(savedGamePath); + } + + string + tempDataPath = userData + "temp/"; + tempDataLocation = tempDataPath; + if (isdir(tempDataPath.c_str()) == true) { + removeFolder(tempDataPath); + } + createDirectoryPaths(tempDataPath); + + string + binaryNameOld = + Properties::getApplicationPath() + + extractFileFromDirectoryPath + (PlatformExceptionHandler::application_binary) + "__REMOVE"; + if (fileExists(binaryNameOld)) { + removeFile(binaryNameOld); + } + + string + netInterfaces = config.getString("NetworkInterfaces", ""); + if (netInterfaces != "") { + //printf("Using network interfaces: %s\n",netInterfaces.c_str()); + std::vector < std::string > intfList; + Tokenize(netInterfaces, intfList, ","); + Socket::setIntfTypes(intfList); + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_USE_PORTS]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_PORTS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_PORTS]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + portsToUse = paramPartTokens[1]; + + vector < string > paramPartPortsTokens; + Tokenize(portsToUse, paramPartPortsTokens, ","); + if (paramPartPortsTokens.size() >= 2 + && paramPartPortsTokens[1].length() > 0) { + int + internalPort = strToInt(paramPartPortsTokens[0]); + int + externalPort = strToInt(paramPartPortsTokens[1]); + + printf("Forcing internal port# %d, external port# %d\n", + internalPort, externalPort); + + config.setInt("PortServer", internalPort, true); + config.setInt("PortExternal", externalPort, true); + config.setInt("FTPServerPort", internalPort + 1, true); + + if (paramPartPortsTokens.size() >= 3 + && paramPartPortsTokens[2].length() > 0) { + int + statusPort = strToInt(paramPartPortsTokens[2]); + + printf("Forcing status port# %d\n", statusPort); + + config.setInt("ServerAdminPort", statusPort, true); + } + } else { + printf + ("\nInvalid ports specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } else { + printf + ("\nInvalid missing ports specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_STATUS])) == true) { + Ip + ip("localhost"); + int + port = Config::getInstance().getInt("ServerAdminPort", + intToStr + (GameConstants:: + serverAdminPort).c_str()); + ClientSocket + clientSocket; + clientSocket.setBlock(false); + clientSocket.connect(ip, port); + if (clientSocket.isConnected() == true) { + clientSocket.setBlock(true); + + char + szBuf[8096] = ""; + clientSocket.receive(&szBuf[0], 8095, false); + std::cout << szBuf << std::endl; + } else { + std::cout << + "Could not connect (possibly no clients connected) to host: " << + ip.getString() << " port: " << port << std::endl; + } + + return 0; + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_DISABLE_SOUND]) + == true + || hasCommandArgument(argc, argv, + string(GAME_ARGS + [GAME_ARG_MASTERSERVER_MODE])) == + true) { + config.setString("FactorySound", "None", true); + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) { + //Logger::getInstance().setMasterserverMode(true); + //Model::setMasterserverMode(true); + //Shared::Sound::Sound::setMasterserverMode(true); + } + } + + if (hasCommandArgument + (argc, argv, + GAME_ARGS[GAME_ARG_DISABLE_OPENGL_CAPS_CHECK]) == true + || config.getBool("CheckGlCaps") == false) { + printf("**WARNING** disabling opengl capability checking...\n"); + config.setBool("CheckGlCaps", false, true); + } + + bool + enableATIHacks = config.getBool("EnableATIHacks", "false"); + if (enableATIHacks == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("**WARNING** Enabling ATI video card hacks\n"); + TextureGl::setEnableATIHacks(enableATIHacks); + } + + Renderer::renderText3DEnabled = + config.getBool("Enable3DFontRendering", + intToStr(Renderer::renderText3DEnabled).c_str()); + + if (config.getBool("EnableLegacyFonts", "false") == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_ENABLE_LEGACYFONTS]) == + true) { + ::Shared::Graphics::Font::forceLegacyFonts = true; + Renderer::renderText3DEnabled = false; + printf("**WARNING** Forcing Legacy Fonts Enabled\n"); + } else { + Renderer::renderText3DEnabled = + config.getBool("Enable3DFontRendering", + intToStr(Renderer::renderText3DEnabled). + c_str()); + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_USE_RESOLUTION]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_RESOLUTION]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_RESOLUTION]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + settings = paramPartTokens[1]; + printf("Forcing resolution [%s]\n", settings.c_str()); + + paramPartTokens.clear(); + Tokenize(settings, paramPartTokens, "x"); + if (paramPartTokens.size() >= 2) { + int + newScreenWidth = strToInt(paramPartTokens[0]); + config.setInt("ScreenWidth", newScreenWidth, true); + + int + newScreenHeight = strToInt(paramPartTokens[1]); + config.setInt("ScreenHeight", newScreenHeight, true); + } else { + printf + ("\nInvalid missing resolution settings specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } else { + printf + ("\nInvalid missing resolution setting specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_USE_COLORBITS]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_COLORBITS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_COLORBITS]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + settings = paramPartTokens[1]; + printf("Forcing colorbits [%s]\n", settings.c_str()); + + int + newColorBits = strToInt(settings); + config.setInt("ColorBits", newColorBits, true); + } else { + printf + ("\nInvalid missing colorbits settings specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_USE_DEPTHBITS]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_DEPTHBITS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_DEPTHBITS]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + settings = paramPartTokens[1]; + printf("Forcing depthbits [%s]\n", settings.c_str()); + + int + newDepthBits = strToInt(settings); + config.setInt("DepthBits", newDepthBits, true); + } else { + printf + ("\nInvalid missing depthbits setting specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_USE_FULLSCREEN]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_FULLSCREEN]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_FULLSCREEN]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + settings = paramPartTokens[1]; + printf("Forcing fullscreen [%s]\n", settings.c_str()); + + bool + newFullScreenMode = strToBool(settings); + config.setBool("Windowed", !newFullScreenMode, true); + } else { + printf + ("\nInvalid missing fullscreen setting specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SET_GAMMA]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SET_GAMMA]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_SET_GAMMA]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + settings = paramPartTokens[1]; + printf("Forcing gamma [%s]\n", settings.c_str()); + + float + newGammaValue = strToFloat(settings); + config.setFloat("GammaValue", newGammaValue, true); + } else { + printf + ("\nInvalid missing gamma setting specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_DISABLE_VIDEOS]) == true) { + VideoPlayer::setDisabled(true); + } else if (config.getBool("EnableVideos", "true") == false) { + VideoPlayer::setDisabled(true); + } + + // Set some statics based on ini entries + SystemFlags::ENABLE_THREADED_LOGGING = + config.getBool("ThreadedLogging", "true"); + FontGl::setDefault_fontType(config.getString("DefaultFont", + FontGl:: + getDefault_fontType(). + c_str())); + UPNP_Tools::isUPNP = !config.getBool("DisableUPNP", "false"); + Texture::useTextureCompression = + config.getBool("EnableTextureCompression", "false"); + + // 256 for English + // 30000 for Chinese + ::Shared::Graphics::Font::charCount = + config.getInt("FONT_CHARCOUNT", + intToStr(::Shared::Graphics::Font::charCount). + c_str()); + ::Shared::Graphics::Font::fontTypeName = + config. + getString("FONT_TYPENAME", ::Shared::Graphics::Font::fontTypeName. + c_str()); + ::Shared::Graphics::Font::fontIsMultibyte = + config.getBool("FONT_MULTIBYTE", + intToStr(::Shared::Graphics::Font:: + fontIsMultibyte).c_str()); + ::Shared::Graphics::Font::fontIsRightToLeft = + config.getBool("FONT_RIGHTTOLEFT", + intToStr(::Shared::Graphics::Font:: + fontIsRightToLeft).c_str()); + ::Shared::Graphics::Font::baseSize = + config.getInt("FONT_BASE_SIZE", + intToStr(::Shared::Graphics::Font::baseSize). + c_str()); + ::Shared::Graphics::Font::scaleFontValue = + config.getFloat("FONT_SCALE_SIZE", + floatToStr(::Shared::Graphics::Font:: + scaleFontValue).c_str()); + ::Shared::Graphics::Font::scaleFontValueCenterHFactor = + config.getFloat("FONT_SCALE_CENTERH_FACTOR", + floatToStr(::Shared::Graphics::Font:: + scaleFontValueCenterHFactor).c_str()); + ::Shared::Graphics::Font::langHeightText = + config. + getString("FONT_HEIGHT_TEXT", ::Shared::Graphics::Font:: + langHeightText.c_str()); + ::Shared::Graphics::Font::fontSupportMixedRightToLeft = + config.getBool("FONT_RIGHTTOLEFT_MIXED_SUPPORT", + intToStr(::Shared::Graphics::Font:: + fontSupportMixedRightToLeft).c_str()); + + // Example values: + // DEFAULT_CHARSET (English) = 1 + // GB2312_CHARSET (Chinese) = 134 + ::Shared::Platform::PlatformContextGl::charSet = + config.getInt("FONT_CHARSET", + intToStr(::Shared::Platform::PlatformContextGl:: + charSet).c_str()); + if (config.getBool("No2DMouseRendering", "false") == false) { + showCursor(false); + //showWindowCursorState = false; + } + if (config.getInt("DEFAULT_HTTP_TIMEOUT", + intToStr(SystemFlags:: + DEFAULT_HTTP_TIMEOUT).c_str()) >= 0) { + SystemFlags::DEFAULT_HTTP_TIMEOUT = + config.getInt("DEFAULT_HTTP_TIMEOUT", + intToStr(SystemFlags::DEFAULT_HTTP_TIMEOUT). + c_str()); + } + + bool + allowAltEnterFullscreenToggle = + config.getBool("AllowAltEnterFullscreenToggle", + boolToStr(::Shared::Platform::Window:: + getAllowAltEnterFullscreenToggle()). + c_str()); + ::Shared::Platform::Window:: + setAllowAltEnterFullscreenToggle(allowAltEnterFullscreenToggle); + + if (config.getBool("noTeamColors", "false") == true) { + MeshCallbackTeamColor::noTeamColors = true; + } + + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LUA_DEBUG]) == + true) { + printf("Forcing LUA debugging enabled!\n"); + config.setBool("DebugLUA", true, true); + } + + // Setup debug logging etc + setupLogging(config, haveSpecialOutputCommandLineOption); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, fontIsRightToLeft = %d\n", + __FILE__, __FUNCTION__, + __LINE__, ::Shared::Graphics::Font:: + charCount, ::Shared::Graphics::Font:: + fontTypeName. + c_str(), ::Shared::Platform:: + PlatformContextGl::charSet, ::Shared:: + Graphics::Font::fontIsMultibyte, ::Shared:: + Graphics::Font::fontIsRightToLeft); + + NetworkInterface::setDisplayMessageFunction(ExceptionHandler:: + DisplayMessage); + MenuStateMasterserver::setDisplayMessageFunction(ExceptionHandler:: + DisplayMessage); #ifdef USE_STREFLOP - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "%s, STREFLOP enabled.\n", - getNetworkVersionString ().c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "%s, STREFLOP enabled.\n", + getNetworkVersionString().c_str()); #else - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "%s, STREFLOP NOT enabled.\n", - getNetworkVersionString ().c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "%s, STREFLOP NOT enabled.\n", + getNetworkVersionString().c_str()); #endif - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, "START\n"); - SystemFlags::OutputDebug (SystemFlags::debugPathFinder, "START\n"); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, "START\n"); + SystemFlags::OutputDebug(SystemFlags::debugPathFinder, "START\n"); -// Setup hotkeys from key ini files - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys), - std::pair < string, - string > (Config::glestkeys_ini_filename, - Config::glestuserkeys_ini_filename), - std::pair < bool, bool > (true, false), - config.getString ("GlestKeysIniPath", "")); + // Setup hotkeys from key ini files + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys), + std::pair < string, + string >(Config::glestkeys_ini_filename, + Config::glestuserkeys_ini_filename), + std::pair < bool, bool >(true, false), + config.getString("GlestKeysIniPath", "")); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == true) - { - ShowINISettings (argc, argv, config, configKeys); - return 0; - } + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_SHOW_INI_SETTINGS]) == true) { + ShowINISettings(argc, argv, config, configKeys); + return 0; + } -// Explicitly disable VBO's - if (config.getBool ("DisableVBO", "false") == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_DISABLE_VBO]) == true) - { - setVBOSupported (false); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("**INFO** Disabling VBOs\n"); - } + // Explicitly disable VBO's + if (config.getBool("DisableVBO", "false") == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_DISABLE_VBO]) == true) { + setVBOSupported(false); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("**INFO** Disabling VBOs\n"); + } - if (config.getBool ("DisableVertexInterpolation", "false") - || hasCommandArgument (argc, argv, - GAME_ARGS - [GAME_ARG_DISABLE_VERTEX_INTERPOLATION])) - { - InterpolationData::setEnableInterpolation (false); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("**INFO** Disabling Interpolation\n"); - } + if (config.getBool("DisableVertexInterpolation", "false") + || hasCommandArgument(argc, argv, + GAME_ARGS + [GAME_ARG_DISABLE_VERTEX_INTERPOLATION])) { + InterpolationData::setEnableInterpolation(false); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("**INFO** Disabling Interpolation\n"); + } - if (config.getBool ("EnableVSynch", "false") == true) - { - ::Shared::Platform::Window::setTryVSynch (true); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("**ENABLED OPENGL VSYNCH**\n"); - } + if (config.getBool("EnableVSynch", "false") == true) { + ::Shared::Platform::Window::setTryVSynch(true); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("**ENABLED OPENGL VSYNCH**\n"); + } -//float pingTime = Socket::getAveragePingMS("soft-haus.com"); -//printf("Ping time = %f\n",pingTime); + //float pingTime = Socket::getAveragePingMS("soft-haus.com"); + //printf("Ping time = %f\n",pingTime); -// Load the language strings - Lang & lang = Lang::getInstance (); - string - language = config.getString ("Lang"); - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_USE_LANGUAGE]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_LANGUAGE]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_LANGUAGE]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - language = paramPartTokens[1]; - printf ("Forcing language [%s]\n", language.c_str ()); - } - else - { - printf - ("\nInvalid missing language specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - else - { + // Load the language strings + Lang & lang = Lang::getInstance(); + string + language = config.getString("Lang"); + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_USE_LANGUAGE]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_LANGUAGE]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_LANGUAGE]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + language = paramPartTokens[1]; + printf("Forcing language [%s]\n", language.c_str()); + } else { + printf + ("\nInvalid missing language specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } else { #ifdef _WIN32 - int - localeBufferSize = - GetLocaleInfo (LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, - NULL, 0); - wchar_t * - sysLocale = new wchar_t[localeBufferSize]; - GetLocaleInfo (LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, - sysLocale, localeBufferSize); + int + localeBufferSize = + GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, + NULL, 0); + wchar_t * + sysLocale = new wchar_t[localeBufferSize]; + GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SISO639LANGNAME, + sysLocale, localeBufferSize); -//String langValue(sysLocale); -//const char *lang_locale = langValue.c_str(); - char - langValue[1024] = ""; - wcstombs (langValue, sysLocale, 1023); - const char * - lang_locale = &langValue[0]; + //String langValue(sysLocale); + //const char *lang_locale = langValue.c_str(); + char + langValue[1024] = ""; + wcstombs(langValue, sysLocale, 1023); + const char * + lang_locale = &langValue[0]; - delete[]sysLocale; + delete[]sysLocale; #else - const char * - lang_locale = setlocale (LC_ALL, ""); + const char * + lang_locale = setlocale(LC_ALL, ""); #endif - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Locale is: [%s]\n", lang_locale); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Locale is: [%s]\n", lang_locale); - if (lang_locale != NULL && strlen (lang_locale) >= 2) - { - if (config.getBool ("AutoLocaleLanguageDetect", "true") == true) - { - language = lang_locale; - language = language.substr (0, 2); - printf ("Auto setting language [%s]\n", language.c_str ()); + if (lang_locale != NULL && strlen(lang_locale) >= 2) { + if (config.getBool("AutoLocaleLanguageDetect", "true") == true) { + language = lang_locale; + language = language.substr(0, 2); + printf("Auto setting language [%s]\n", language.c_str()); - config.setString ("AutoLocaleLanguageDetect", "false"); - config.save (); - } - } - } + config.setString("AutoLocaleLanguageDetect", "false"); + config.save(); + } + } + } - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_AUTO_TEST])) == true - || Config::getInstance ().getBool ("AutoTest", "false") == true) - { - printf ("Running in auto test mode\n"); - } - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_AUTO_TEST])) == true) - { - Config::getInstance ().setBool ("AutoTest", true, true); + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_AUTO_TEST])) == true + || Config::getInstance().getBool("AutoTest", "false") == true) { + printf("Running in auto test mode\n"); + } + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_AUTO_TEST])) == true) { + Config::getInstance().setBool("AutoTest", true, true); - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_AUTO_TEST]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_AUTO_TEST]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - vector < string > paramPartTokens2; - Tokenize (paramPartTokens[1], paramPartTokens2, ","); - if (paramPartTokens2.empty () == false - && paramPartTokens2[0].length () > 0) - { - string - newMaxSeconds = paramPartTokens2[0]; - time_t - newTimeMaxSeconds = strToInt (newMaxSeconds); - AutoTest::setMaxGameTime (newTimeMaxSeconds); - printf - ("Forcing maximum game time to [%ld] seconds (%.2f minutes)\n", - (long int) newTimeMaxSeconds, - ((double) newTimeMaxSeconds / 60.0)); - } - if (paramPartTokens2.size () >= 3 - && paramPartTokens2[2].length () > 0) - { - string - autoTestCmd = paramPartTokens2[2]; - if (autoTestCmd == "exit") - { - printf - ("Detected auto test command [%s], will exit after game.\n", - autoTestCmd.c_str ()); + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_AUTO_TEST]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_AUTO_TEST]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + vector < string > paramPartTokens2; + Tokenize(paramPartTokens[1], paramPartTokens2, ","); + if (paramPartTokens2.empty() == false + && paramPartTokens2[0].length() > 0) { + string + newMaxSeconds = paramPartTokens2[0]; + time_t + newTimeMaxSeconds = strToInt(newMaxSeconds); + AutoTest::setMaxGameTime(newTimeMaxSeconds); + printf + ("Forcing maximum game time to [%ld] seconds (%.2f minutes)\n", + (long int) newTimeMaxSeconds, + ((double) newTimeMaxSeconds / 60.0)); + } + if (paramPartTokens2.size() >= 3 + && paramPartTokens2[2].length() > 0) { + string + autoTestCmd = paramPartTokens2[2]; + if (autoTestCmd == "exit") { + printf + ("Detected auto test command [%s], will exit after game.\n", + autoTestCmd.c_str()); - AutoTest::setWantExitGameWhenDone (true); - } - else - { - printf - ("WARNING: Detected and UNKNOWN auto test command [%s].\n", - autoTestCmd.c_str ()); - } - } + AutoTest::setWantExitGameWhenDone(true); + } else { + printf + ("WARNING: Detected and UNKNOWN auto test command [%s].\n", + autoTestCmd.c_str()); + } + } - if (paramPartTokens2.size () >= 2 - && paramPartTokens2[1].length () > 0) - { - string - newGameSettingsFileToLoad = paramPartTokens2[1]; + if (paramPartTokens2.size() >= 2 + && paramPartTokens2[1].length() > 0) { + string + newGameSettingsFileToLoad = paramPartTokens2[1]; - printf ("About to auto test using game settings file [%s]\n", - newGameSettingsFileToLoad.c_str ()); - AutoTest::setLoadGameSettingsFile (newGameSettingsFileToLoad); - } - } - } + printf("About to auto test using game settings file [%s]\n", + newGameSettingsFileToLoad.c_str()); + AutoTest::setLoadGameSettingsFile(newGameSettingsFileToLoad); + } + } + } - Renderer & renderer = Renderer::getInstance (); - lang.loadGameStrings (language, false, true); + Renderer & renderer = Renderer::getInstance(); + lang.loadGameStrings(language, false, true); - if (lang.hasString ("FONT_HEIGHT_TEXT")) - { - ::Shared::Graphics::Font::langHeightText = - config. - getString ("FONT_HEIGHT_TEXT",::Shared::Graphics::Font:: - langHeightText.c_str ()); - } + if (lang.hasString("FONT_HEIGHT_TEXT")) { + ::Shared::Graphics::Font::langHeightText = + config. + getString("FONT_HEIGHT_TEXT", ::Shared::Graphics::Font:: + langHeightText.c_str()); + } - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_FONT_BASESIZE]) - == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_FONT_BASESIZE]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_FONT_BASESIZE]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - newfontBaseSize = paramPartTokens[1]; - printf ("Forcing font base size[%s]\n", newfontBaseSize.c_str ()); + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_FONT_BASESIZE]) + == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_FONT_BASESIZE]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_FONT_BASESIZE]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + newfontBaseSize = paramPartTokens[1]; + printf("Forcing font base size[%s]\n", newfontBaseSize.c_str()); - ::Shared::Graphics::Font::baseSize = strToInt (newfontBaseSize); - } - else - { - printf - ("\nInvalid missing font base size specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); + ::Shared::Graphics::Font::baseSize = strToInt(newfontBaseSize); + } else { + printf + ("\nInvalid missing font base size specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); - return 1; - } - } + return 1; + } + } - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, Font::fontIsRightToLeft = %d\n", - __FILE__, __FUNCTION__, - __LINE__,::Shared::Graphics::Font:: - charCount,::Shared::Graphics::Font:: - fontTypeName. - c_str (),::Shared::Platform:: - PlatformContextGl::charSet,::Shared:: - Graphics::Font::fontIsMultibyte,::Shared:: - Graphics::Font::fontIsRightToLeft); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Using Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, Font::fontIsRightToLeft = %d\n",:: - Shared::Graphics::Font::charCount,::Shared::Graphics::Font:: - fontTypeName. - c_str (),::Shared::Platform::PlatformContextGl::charSet,:: - Shared::Graphics::Font::fontIsMultibyte,::Shared::Graphics:: - Font::fontIsRightToLeft); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, Font::fontIsRightToLeft = %d\n", + __FILE__, __FUNCTION__, + __LINE__, ::Shared::Graphics::Font:: + charCount, ::Shared::Graphics::Font:: + fontTypeName. + c_str(), ::Shared::Platform:: + PlatformContextGl::charSet, ::Shared:: + Graphics::Font::fontIsMultibyte, ::Shared:: + Graphics::Font::fontIsRightToLeft); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Using Font::charCount = %d, Font::fontTypeName [%s] Shared::Platform::PlatformContextGl::charSet = %d, Font::fontIsMultibyte = %d, Font::fontIsRightToLeft = %d\n", :: + Shared::Graphics::Font::charCount, ::Shared::Graphics::Font:: + fontTypeName. + c_str(), ::Shared::Platform::PlatformContextGl::charSet, :: + Shared::Graphics::Font::fontIsMultibyte, ::Shared::Graphics:: + Font::fontIsRightToLeft); - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_USE_FONT]) == - true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_FONT]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_USE_FONT]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - newfont = paramPartTokens[1]; + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_USE_FONT]) == + true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_FONT]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_USE_FONT]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + newfont = paramPartTokens[1]; - Properties::applyTagsToValue (newfont); - printf ("Forcing font [%s]\n", newfont.c_str ()); + Properties::applyTagsToValue(newfont); + printf("Forcing font [%s]\n", newfont.c_str()); #if defined(WIN32) - string - newEnvValue = "MEGAGLEST_FONT=" + newfont; - _putenv (newEnvValue.c_str ()); + string + newEnvValue = "MEGAGLEST_FONT=" + newfont; + _putenv(newEnvValue.c_str()); #else - setenv ("MEGAGLEST_FONT", newfont.c_str (), 1); + setenv("MEGAGLEST_FONT", newfont.c_str(), 1); #endif - } - else - { - printf - ("\nInvalid missing font specified on commandline [%s] value [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - return 1; - } - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == true) - { - return handleCreateDataArchivesCommand (argc, argv); - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) - == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) == true) - { - return handleShowCRCValuesCommand (argc, argv); - } - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_LIST_MAPS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == - true) - { - return handleListDataCommand (argc, argv); - - } - - program = new Program (); - mainProgram = program; - renderer.setProgram (program); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugPathFinder). - enabled == true) - { - renderer.setAllowRenderUnitTitles (SystemFlags::getSystemSettingType - (SystemFlags::debugPathFinder). - enabled); - SystemFlags::OutputDebug (SystemFlags::debugPathFinder, - "In [%s::%s Line: %d] renderer.setAllowRenderUnitTitles = %d\n", - __FILE__, __FUNCTION__, __LINE__, - SystemFlags:: - getSystemSettingType (SystemFlags:: - debugPathFinder). - enabled); - } - renderer.setAllowRenderUnitTitles (true); - - string - screenShotsPath = userData + GameConstants::folder_path_screenshots; - if (isdir (screenShotsPath.c_str ()) == false) - { - createDirectoryPaths (screenShotsPath); - } - -// Cache Player textures - START - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - std::map < int, - Texture2D * >& - crcPlayerTextureCache = - CacheManager::getCachedItem < std::map < int, - Texture2D * > >(GameConstants::playerTextureCacheLookupKey); - for (int index = 0; index < GameConstants::maxPlayers; ++index) - { - string - playerTexture = - getGameCustomCoreDataPath (data_path, - "data/core/faction_textures/faction" + - intToStr (index) + ".tga"); - if (fileExists (playerTexture) == true) - { - Texture2D * - texture = Renderer::getInstance ().newTexture2D (rsGlobal); - if (texture) - { - texture->load (playerTexture); - } - crcPlayerTextureCache[index] = texture; - } - else - { - crcPlayerTextureCache[index] = NULL; - } - } -// Cache Player textures - END - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - mainWindow = new MainWindow (program); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - GameSettings - startupGameSettings; - -//parse command line - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_SERVER]) == - true) - { - program->initServer (mainWindow, false, true); - gameInitialized = true; - } - else - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) - { - program->initServer (mainWindow, false, true, true); - gameInitialized = true; - } - else - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_AUTOSTART_LASTGAME])) == true) - { - program->initServer (mainWindow, true, false); - gameInitialized = true; - } - else - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME])) == - true) - { - string - fileName = ""; - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS - [GAME_ARG_AUTOSTART_LAST_SAVED_GAME]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex >= 0) - { - string - loadfileName = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (loadfileName, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - fileName = paramPartTokens[1]; - - if (fileExists (fileName) == false) - { -// Save the file now - string - saveGameFile = "saved/" + fileName; - if (getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) != "") - { - saveGameFile = - getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) + saveGameFile; - } - else - { - saveGameFile = userData + saveGameFile; - } - if (fileExists (saveGameFile) == true) - { - fileName = saveGameFile; - } - } - - if (fileExists (fileName) == false) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "File specified for loading a saved game cannot be found: [%s]", - fileName.c_str ()); - printf - ("\n\n======================================================================================\n%s\n======================================================================================\n\n\n", - szBuf); - - throw - megaglest_runtime_error (szBuf); - } - } - } - program->initSavedGame (mainWindow, false, fileName); - gameInitialized = true; - } - else - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_PREVIEW_MAP])) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_PREVIEW_MAP]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_PREVIEW_MAP]), - &foundParamIndIndex); - } - string - mapName = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (mapName, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - vector < string > paramPartTokens2; - string - tileset = "forest"; - Tokenize (paramPartTokens[1], paramPartTokens2, ","); - if (paramPartTokens2.size () >= 2 - && paramPartTokens2[1].length () > 0) - { - tileset = paramPartTokens2[1]; - } - string - autoloadMapName = paramPartTokens2[0]; - - GameSettings * - gameSettings = &startupGameSettings; - gameSettings->setMap (autoloadMapName); - gameSettings->setTileset (tileset); - gameSettings->setTech ("megapack"); - gameSettings->setDefaultUnits (false); - gameSettings->setDefaultResources (false); - gameSettings->setDefaultVictoryConditions (true); - gameSettings->setFogOfWar (false); - gameSettings->setAllowObservers (true); - gameSettings->setPathFinderType (pfBasic); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ControlType - ct = ctClosed; - - gameSettings->setNetworkPlayerStatuses (i, npst_None); - gameSettings->setFactionControl (i, ct); - gameSettings->setStartLocationIndex (i, i); - gameSettings->setResourceMultiplierIndex (i, 10); - gameSettings->setNetworkPlayerName (i, - GameConstants:: - NETWORK_SLOT_CLOSED_SLOTNAME); - } - - ControlType - ct = ctHuman; - - gameSettings->setNetworkPlayerStatuses (0, npst_None); - gameSettings->setFactionControl (0, ct); - gameSettings->setFactionTypeName (0, - formatString - (GameConstants:: - OBSERVER_SLOTNAME)); - gameSettings->setTeam (0, - GameConstants::maxPlayers + fpt_Observer - - 1); - gameSettings->setStartLocationIndex (0, 0); - gameSettings->setNetworkPlayerName (0, - GameConstants:: - OBSERVER_SLOTNAME); - - gameSettings->setFactionCount (1); - - Config & config = Config::getInstance (); - gameSettings->setEnableServerControlledAI (config.getBool - ("ServerControlledAI", - "true")); - gameSettings->setNetworkFramePeriod (config.getInt - ("NetworkSendFrameCount", - "20")); - - program->initServer (mainWindow, gameSettings); - gameInitialized = true; - } - else - { - printf - ("\nInvalid map name specified on commandline [%s] map [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - delete - mainWindow; - mainWindow = NULL; - return 1; - } - } - - else - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_CONNECT])) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CONNECT]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CONNECT]), - &foundParamIndIndex); - } - string - serverToConnectTo = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (serverToConnectTo, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - autoConnectServer = paramPartTokens[1]; - - int - port = config.getInt ("PortServer", - intToStr (GameConstants:: - serverPort).c_str ()); - vector < string > paramPartTokens2; - Tokenize (autoConnectServer, paramPartTokens2, ":"); - autoConnectServer = paramPartTokens2[0]; - if (paramPartTokens2.size () >= 2 - && paramPartTokens2[1].length () > 0) - { - port = strToInt (paramPartTokens2[1]); - } - - printf ("Connecting to host [%s] using port: %d\n", - autoConnectServer.c_str (), port); - if (autoConnectServer == "auto-connect") - { - program->initClientAutoFindHost (mainWindow); - } - else - { - program->initClient (mainWindow, autoConnectServer, port); - } - gameInitialized = true; - } - else - { - - printf - ("\nInvalid host specified on commandline [%s] host [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - delete - mainWindow; - mainWindow = NULL; - return 1; - } - } - - else - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_CLIENT])) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CLIENT]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CLIENT]), - &foundParamIndIndex); - } - string - serverToConnectTo = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (serverToConnectTo, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - autoConnectServer = paramPartTokens[1]; - - if (autoConnectServer == "auto-connect") - { - program->initClientAutoFindHost (mainWindow); - } - else - { - program->initClient (mainWindow, autoConnectServer); - } - gameInitialized = true; - } - else - { - - printf - ("\nInvalid host specified on commandline [%s] host [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - delete - mainWindow; - mainWindow = NULL; - return 1; - } - } - else - if (hasCommandArgument - (argc, argv, string (GAME_ARGS[GAME_ARG_LOADSCENARIO])) == true) - { - - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LOADSCENARIO]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_LOADSCENARIO]), - &foundParamIndIndex); - } - string - scenarioName = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (scenarioName, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - autoloadScenarioName = paramPartTokens[1]; - - program->initScenario (mainWindow, autoloadScenarioName); - gameInitialized = true; - } - else - { - printf - ("\nInvalid scenario name specified on commandline [%s] scenario [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - delete - mainWindow; - mainWindow = NULL; - return 1; - } - } - else - { - program->initNormal (mainWindow); - } - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] OpenGL Info:\n%s\n", - __FILE__, __FUNCTION__, __LINE__, - renderer.getGlInfo ().c_str ()); - - if (hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) - == true) - { - printf ("%s", renderer.getGlInfo ().c_str ()); - printf ("%s", renderer.getGlMoreInfo ().c_str ()); - - delete - mainWindow; - mainWindow = NULL; - return 0; - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_CONVERT_MODELS]) == true) - { - int - foundParamIndIndex = -1; - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CONVERT_MODELS]) + - string ("="), &foundParamIndIndex); - if (foundParamIndIndex < 0) - { - hasCommandArgument (argc, argv, - string (GAME_ARGS[GAME_ARG_CONVERT_MODELS]), - &foundParamIndIndex); - } - string - paramValue = argv[foundParamIndIndex]; - vector < string > paramPartTokens; - Tokenize (paramValue, paramPartTokens, "="); - if (paramPartTokens.size () >= 2 - && paramPartTokens[1].length () > 0) - { - string - modelFile = paramPartTokens[1]; - printf ("About to convert model(s) [%s]\n", modelFile.c_str ()); - - string - textureFormat = ""; - if (paramPartTokens.size () >= 3 - && paramPartTokens[1].length () > 0) - { - textureFormat = paramPartTokens[2]; - printf ("About to convert using texture format [%s]\n", - textureFormat.c_str ()); - } - - bool - keepsmallest = false; - if (paramPartTokens.size () >= 4 - && paramPartTokens[1].length () > 0) - { - keepsmallest = (paramPartTokens[3] == "keepsmallest"); - printf ("About to convert using keepsmallest = %d\n", - keepsmallest); - } - - showCursor (true); - - const - Metrics & - metrics = Metrics::getInstance (); - renderer.clearBuffers (); - renderer.clearZBuffer (); - renderer.reset2d (); - - if (CoreData::getInstance ().getMenuFontBig3D () != NULL) - { - renderer.renderText3D ("Please wait, converting models...", - CoreData::getInstance (). - getMenuFontBig3D (), Vec3f (1.f, 1.f, - 0.f), - (metrics.getScreenW () / 2) - 400, - (metrics.getScreenH () / 2), true); - } - else - { - renderer.renderText ("Please wait, converting models...", - CoreData::getInstance ().getMenuFontBig (), - Vec3f (1.f, 1.f, 0.f), - (metrics.getScreenW () / 2) - 400, - (metrics.getScreenH () / 2), true); - } - renderer.swapBuffers (); - - std::vector < string > models; - if (isdir (modelFile.c_str ()) == true) - { - models = - getFolderTreeContentsListRecursively (modelFile, ".g3d"); - } - else - { - models.push_back (modelFile); - } - - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - int - result = 0; - char - szTextBuf[8096] = ""; - for (unsigned int i = 0; i < models.size (); ++i) - { - string & file = models[i]; - - renderer.clearBuffers (); - renderer.clearZBuffer (); - renderer.reset2d (); - snprintf (szTextBuf, 8096, - "Please wait, converting models [%u of " - MG_SIZE_T_SPECIFIER "] ...", i, models.size ()); - - if (CoreData::getInstance ().getMenuFontBig3D () != NULL) - { - renderer.renderText3D (szTextBuf, - CoreData::getInstance (). - getMenuFontBig3D (), Vec3f (1.f, 1.f, - 0.f), - (metrics.getScreenW () / 2) - 400, - (metrics.getScreenH () / 2), true); - } - else - { - renderer.renderText (szTextBuf, - CoreData::getInstance (). - getMenuFontBig (), Vec3f (1.f, 1.f, 0.f), - (metrics.getScreenW () / 2) - 400, - (metrics.getScreenH () / 2), true); - } - renderer.swapBuffers (); - - sleep (0); - ::Shared::Platform::Window::handleEvent (); - SDL_PumpEvents (); - - try - { - printf ("About to load model [%s] [%u of " MG_SIZE_T_SPECIFIER - "]\n", file.c_str (), i, models.size ()); - Model * - model = renderer.newModel (rsGlobal, file); - printf ("About to save converted model [%s]\n", - file.c_str ()); - model->save (file, textureFormat, keepsmallest); - Renderer::getInstance ().endModel (rsGlobal, model); - } - catch (const exception & ex) - { - result = 1; - printf ("ERROR loading model [%s] message [%s]\n", - file.c_str (), ex.what ()); - } - - } - - delete - mainWindow; - mainWindow = NULL; - return result; - } - else - { - printf - ("\nInvalid model specified on commandline [%s] texture [%s]\n\n", - argv[foundParamIndIndex], - (paramPartTokens.size () >= - 2 ? paramPartTokens[1].c_str () : NULL)); - printParameterHelp (argv[0], foundInvalidArgs); - delete - mainWindow; - mainWindow = NULL; - return 1; - } - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == - true - || hasCommandArgument (argc, argv, - GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == - true) - { - runTechValidationReport (argc, argv); - - delete - mainWindow; - mainWindow = NULL; - return 0; - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == true) - { - runTechTranslationExtraction (argc, argv); - delete - mainWindow; - mainWindow = NULL; - return 0; - } - - if (hasCommandArgument - (argc, argv, GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true) - { - runTilesetValidationReport (argc, argv); - - delete - mainWindow; - mainWindow = NULL; - return 0; - } - - gameInitialized = true; - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - CheckForDuplicateData (); - - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - -//throw "BLAH!"; - -// START Test out SIGSEGV error handling -//int *foo = (int*)-1; // make a bad pointer -//printf("%d\n", *foo); // causes segfault -// END - - bool - startCRCPrecacheThread = - config.getBool ("PreCacheCRCThread", "true"); -//printf("### In [%s::%s Line: %d] precache thread enabled = %d SystemFlags::VERBOSE_MODE_ENABLED = %d\n",__FILE__,__FUNCTION__,__LINE__,startCRCPrecacheThread,SystemFlags::VERBOSE_MODE_ENABLED); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] precache thread enabled = %d\n", - __FILE__, __FUNCTION__, __LINE__, startCRCPrecacheThread); - if (startCRCPrecacheThread == true - && GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - static - string - mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - vector < string > techDataPaths = - config.getPathListForType (ptTechs); - - FileCRCPreCacheThread::setPreCacheThreadCacheLookupKey - (GameConstants::preCacheThreadCacheLookupKey); - FileCRCPreCacheThread *& - preCacheCRCThreadPtr = - CacheManager::getCachedItem < - FileCRCPreCacheThread * - >(GameConstants::preCacheThreadCacheLookupKey); - if (preCacheCRCThreadPtr == NULL) - { - preCacheCRCThreadPtr = new FileCRCPreCacheThread (); - } - preCacheThread = preCacheCRCThreadPtr; - preCacheThread->setUniqueID (mutexOwnerId); - preCacheThread->setTechDataPaths (techDataPaths); -//preCacheThread->setFileCRCPreCacheThreadCallbackInterface(&preCacheThreadGame); - preCacheThread->start (); - } - - auto_ptr < NavtiveLanguageNameListCacheGenerator > lngCacheGen; - auto_ptr < SimpleTaskThread > languageCacheGen; - - bool - startNativeLanguageNamesPrecacheThread = - config.getBool ("PreCacheNativeLanguageNamesThread", "true"); - if (startNativeLanguageNamesPrecacheThread == true - && GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - lngCacheGen.reset (new NavtiveLanguageNameListCacheGenerator ()); - languageCacheGen.reset (new - SimpleTaskThread (lngCacheGen.get (), 1)); - - languageCacheGen->start (); - } - -// test -//Shared::Platform::MessageBox(NULL,"Mark's test.","Test",0); -//throw megaglest_runtime_error("test!"); -//ExceptionHandler::DisplayMessage("test!", false); - -// Check for commands being input from stdin - string - command = ""; + } else { + printf + ("\nInvalid missing font specified on commandline [%s] value [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + return 1; + } + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_CREATE_DATA_ARCHIVES]) == true) { + return handleCreateDataArchivesCommand(argc, argv); + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SHOW_MAP_CRC]) + == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_TILESET_CRC]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_TECHTREE_CRC]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_SCENARIO_CRC]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_SHOW_PATH_CRC]) == true) { + return handleShowCRCValuesCommand(argc, argv); + } + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_LIST_MAPS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TECHTRESS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_SCENARIOS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TILESETS]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_LIST_TUTORIALS]) == + true) { + return handleListDataCommand(argc, argv); + + } + + program = new Program(); + mainProgram = program; + renderer.setProgram(program); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPathFinder). + enabled == true) { + renderer.setAllowRenderUnitTitles(SystemFlags::getSystemSettingType + (SystemFlags::debugPathFinder). + enabled); + SystemFlags::OutputDebug(SystemFlags::debugPathFinder, + "In [%s::%s Line: %d] renderer.setAllowRenderUnitTitles = %d\n", + __FILE__, __FUNCTION__, __LINE__, + SystemFlags:: + getSystemSettingType(SystemFlags:: + debugPathFinder). + enabled); + } + renderer.setAllowRenderUnitTitles(true); + + string + screenShotsPath = userData + GameConstants::folder_path_screenshots; + if (isdir(screenShotsPath.c_str()) == false) { + createDirectoryPaths(screenShotsPath); + } + + // Cache Player textures - START + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + std::map < int, + Texture2D * >& + crcPlayerTextureCache = + CacheManager::getCachedItem < std::map < int, + Texture2D * > >(GameConstants::playerTextureCacheLookupKey); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + string + playerTexture = + getGameCustomCoreDataPath(data_path, + "data/core/faction_textures/faction" + + intToStr(index) + ".tga"); + if (fileExists(playerTexture) == true) { + Texture2D * + texture = Renderer::getInstance().newTexture2D(rsGlobal); + if (texture) { + texture->load(playerTexture); + } + crcPlayerTextureCache[index] = texture; + } else { + crcPlayerTextureCache[index] = NULL; + } + } + // Cache Player textures - END + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + mainWindow = new MainWindow(program); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + GameSettings + startupGameSettings; + + //parse command line + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_SERVER]) == + true) { + program->initServer(mainWindow, false, true); + gameInitialized = true; + } else + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_MASTERSERVER_MODE])) == true) { + program->initServer(mainWindow, false, true, true); + gameInitialized = true; + } else + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_AUTOSTART_LASTGAME])) == true) { + program->initServer(mainWindow, true, false); + gameInitialized = true; + } else + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_AUTOSTART_LAST_SAVED_GAME])) == + true) { + string + fileName = ""; + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS + [GAME_ARG_AUTOSTART_LAST_SAVED_GAME]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex >= 0) { + string + loadfileName = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(loadfileName, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + fileName = paramPartTokens[1]; + + if (fileExists(fileName) == false) { + // Save the file now + string + saveGameFile = "saved/" + fileName; + if (getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) != "") { + saveGameFile = + getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) + saveGameFile; + } else { + saveGameFile = userData + saveGameFile; + } + if (fileExists(saveGameFile) == true) { + fileName = saveGameFile; + } + } + + if (fileExists(fileName) == false) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "File specified for loading a saved game cannot be found: [%s]", + fileName.c_str()); + printf + ("\n\n======================================================================================\n%s\n======================================================================================\n\n\n", + szBuf); + + throw + megaglest_runtime_error(szBuf); + } + } + } + program->initSavedGame(mainWindow, false, fileName); + gameInitialized = true; + } else + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_PREVIEW_MAP])) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_PREVIEW_MAP]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_PREVIEW_MAP]), + &foundParamIndIndex); + } + string + mapName = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(mapName, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + vector < string > paramPartTokens2; + string + tileset = "forest"; + Tokenize(paramPartTokens[1], paramPartTokens2, ","); + if (paramPartTokens2.size() >= 2 + && paramPartTokens2[1].length() > 0) { + tileset = paramPartTokens2[1]; + } + string + autoloadMapName = paramPartTokens2[0]; + + GameSettings * + gameSettings = &startupGameSettings; + gameSettings->setMap(autoloadMapName); + gameSettings->setTileset(tileset); + gameSettings->setTech("megapack"); + gameSettings->setDefaultUnits(false); + gameSettings->setDefaultResources(false); + gameSettings->setDefaultVictoryConditions(true); + gameSettings->setFogOfWar(false); + gameSettings->setAllowObservers(true); + gameSettings->setPathFinderType(pfBasic); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType + ct = ctClosed; + + gameSettings->setNetworkPlayerStatuses(i, npst_None); + gameSettings->setFactionControl(i, ct); + gameSettings->setStartLocationIndex(i, i); + gameSettings->setResourceMultiplierIndex(i, 10); + gameSettings->setNetworkPlayerName(i, + GameConstants:: + NETWORK_SLOT_CLOSED_SLOTNAME); + } + + ControlType + ct = ctHuman; + + gameSettings->setNetworkPlayerStatuses(0, npst_None); + gameSettings->setFactionControl(0, ct); + gameSettings->setFactionTypeName(0, + formatString + (GameConstants:: + OBSERVER_SLOTNAME)); + gameSettings->setTeam(0, + GameConstants::maxPlayers + fpt_Observer - + 1); + gameSettings->setStartLocationIndex(0, 0); + gameSettings->setNetworkPlayerName(0, + GameConstants:: + OBSERVER_SLOTNAME); + + gameSettings->setFactionCount(1); + + Config & config = Config::getInstance(); + gameSettings->setEnableServerControlledAI(config.getBool + ("ServerControlledAI", + "true")); + gameSettings->setNetworkFramePeriod(config.getInt + ("NetworkSendFrameCount", + "20")); + + program->initServer(mainWindow, gameSettings); + gameInitialized = true; + } else { + printf + ("\nInvalid map name specified on commandline [%s] map [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + delete + mainWindow; + mainWindow = NULL; + return 1; + } + } + + else + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_CONNECT])) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CONNECT]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CONNECT]), + &foundParamIndIndex); + } + string + serverToConnectTo = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(serverToConnectTo, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + autoConnectServer = paramPartTokens[1]; + + int + port = config.getInt("PortServer", + intToStr(GameConstants:: + serverPort).c_str()); + vector < string > paramPartTokens2; + Tokenize(autoConnectServer, paramPartTokens2, ":"); + autoConnectServer = paramPartTokens2[0]; + if (paramPartTokens2.size() >= 2 + && paramPartTokens2[1].length() > 0) { + port = strToInt(paramPartTokens2[1]); + } + + printf("Connecting to host [%s] using port: %d\n", + autoConnectServer.c_str(), port); + if (autoConnectServer == "auto-connect") { + program->initClientAutoFindHost(mainWindow); + } else { + program->initClient(mainWindow, autoConnectServer, port); + } + gameInitialized = true; + } else { + + printf + ("\nInvalid host specified on commandline [%s] host [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + delete + mainWindow; + mainWindow = NULL; + return 1; + } + } + + else + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_CLIENT])) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CLIENT]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CLIENT]), + &foundParamIndIndex); + } + string + serverToConnectTo = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(serverToConnectTo, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + autoConnectServer = paramPartTokens[1]; + + if (autoConnectServer == "auto-connect") { + program->initClientAutoFindHost(mainWindow); + } else { + program->initClient(mainWindow, autoConnectServer); + } + gameInitialized = true; + } else { + + printf + ("\nInvalid host specified on commandline [%s] host [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + delete + mainWindow; + mainWindow = NULL; + return 1; + } + } else + if (hasCommandArgument + (argc, argv, string(GAME_ARGS[GAME_ARG_LOADSCENARIO])) == true) { + + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LOADSCENARIO]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_LOADSCENARIO]), + &foundParamIndIndex); + } + string + scenarioName = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(scenarioName, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + autoloadScenarioName = paramPartTokens[1]; + + program->initScenario(mainWindow, autoloadScenarioName); + gameInitialized = true; + } else { + printf + ("\nInvalid scenario name specified on commandline [%s] scenario [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + delete + mainWindow; + mainWindow = NULL; + return 1; + } + } else { + program->initNormal(mainWindow); + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] OpenGL Info:\n%s\n", + __FILE__, __FUNCTION__, __LINE__, + renderer.getGlInfo().c_str()); + + if (hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_OPENGL_INFO]) + == true) { + printf("%s", renderer.getGlInfo().c_str()); + printf("%s", renderer.getGlMoreInfo().c_str()); + + delete + mainWindow; + mainWindow = NULL; + return 0; + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_CONVERT_MODELS]) == true) { + int + foundParamIndIndex = -1; + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CONVERT_MODELS]) + + string("="), &foundParamIndIndex); + if (foundParamIndIndex < 0) { + hasCommandArgument(argc, argv, + string(GAME_ARGS[GAME_ARG_CONVERT_MODELS]), + &foundParamIndIndex); + } + string + paramValue = argv[foundParamIndIndex]; + vector < string > paramPartTokens; + Tokenize(paramValue, paramPartTokens, "="); + if (paramPartTokens.size() >= 2 + && paramPartTokens[1].length() > 0) { + string + modelFile = paramPartTokens[1]; + printf("About to convert model(s) [%s]\n", modelFile.c_str()); + + string + textureFormat = ""; + if (paramPartTokens.size() >= 3 + && paramPartTokens[1].length() > 0) { + textureFormat = paramPartTokens[2]; + printf("About to convert using texture format [%s]\n", + textureFormat.c_str()); + } + + bool + keepsmallest = false; + if (paramPartTokens.size() >= 4 + && paramPartTokens[1].length() > 0) { + keepsmallest = (paramPartTokens[3] == "keepsmallest"); + printf("About to convert using keepsmallest = %d\n", + keepsmallest); + } + + showCursor(true); + + const + Metrics & + metrics = Metrics::getInstance(); + renderer.clearBuffers(); + renderer.clearZBuffer(); + renderer.reset2d(); + + if (CoreData::getInstance().getMenuFontBig3D() != NULL) { + renderer.renderText3D("Please wait, converting models...", + CoreData::getInstance(). + getMenuFontBig3D(), Vec3f(1.f, 1.f, + 0.f), + (metrics.getScreenW() / 2) - 400, + (metrics.getScreenH() / 2), true); + } else { + renderer.renderText("Please wait, converting models...", + CoreData::getInstance().getMenuFontBig(), + Vec3f(1.f, 1.f, 0.f), + (metrics.getScreenW() / 2) - 400, + (metrics.getScreenH() / 2), true); + } + renderer.swapBuffers(); + + std::vector < string > models; + if (isdir(modelFile.c_str()) == true) { + models = + getFolderTreeContentsListRecursively(modelFile, ".g3d"); + } else { + models.push_back(modelFile); + } + + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + int + result = 0; + char + szTextBuf[8096] = ""; + for (unsigned int i = 0; i < models.size(); ++i) { + string & file = models[i]; + + renderer.clearBuffers(); + renderer.clearZBuffer(); + renderer.reset2d(); + snprintf(szTextBuf, 8096, + "Please wait, converting models [%u of " + MG_SIZE_T_SPECIFIER "] ...", i, models.size()); + + if (CoreData::getInstance().getMenuFontBig3D() != NULL) { + renderer.renderText3D(szTextBuf, + CoreData::getInstance(). + getMenuFontBig3D(), Vec3f(1.f, 1.f, + 0.f), + (metrics.getScreenW() / 2) - 400, + (metrics.getScreenH() / 2), true); + } else { + renderer.renderText(szTextBuf, + CoreData::getInstance(). + getMenuFontBig(), Vec3f(1.f, 1.f, 0.f), + (metrics.getScreenW() / 2) - 400, + (metrics.getScreenH() / 2), true); + } + renderer.swapBuffers(); + + sleep(0); + ::Shared::Platform::Window::handleEvent(); + SDL_PumpEvents(); + + try { + printf("About to load model [%s] [%u of " MG_SIZE_T_SPECIFIER + "]\n", file.c_str(), i, models.size()); + Model * + model = renderer.newModel(rsGlobal, file); + printf("About to save converted model [%s]\n", + file.c_str()); + model->save(file, textureFormat, keepsmallest); + Renderer::getInstance().endModel(rsGlobal, model); + } catch (const exception & ex) { + result = 1; + printf("ERROR loading model [%s] message [%s]\n", + file.c_str(), ex.what()); + } + + } + + delete + mainWindow; + mainWindow = NULL; + return result; + } else { + printf + ("\nInvalid model specified on commandline [%s] texture [%s]\n\n", + argv[foundParamIndIndex], + (paramPartTokens.size() >= + 2 ? paramPartTokens[1].c_str() : NULL)); + printParameterHelp(argv[0], foundInvalidArgs); + delete + mainWindow; + mainWindow = NULL; + return 1; + } + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_VALIDATE_TECHTREES]) == true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_FACTIONS]) == + true + || hasCommandArgument(argc, argv, + GAME_ARGS[GAME_ARG_VALIDATE_SCENARIO]) == + true) { + runTechValidationReport(argc, argv); + + delete + mainWindow; + mainWindow = NULL; + return 0; + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_TRANSLATE_TECHTREES]) == true) { + runTechTranslationExtraction(argc, argv); + delete + mainWindow; + mainWindow = NULL; + return 0; + } + + if (hasCommandArgument + (argc, argv, GAME_ARGS[GAME_ARG_VALIDATE_TILESET]) == true) { + runTilesetValidationReport(argc, argv); + + delete + mainWindow; + mainWindow = NULL; + return 0; + } + + gameInitialized = true; + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + CheckForDuplicateData(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //throw "BLAH!"; + + // START Test out SIGSEGV error handling + //int *foo = (int*)-1; // make a bad pointer + //printf("%d\n", *foo); // causes segfault + // END + + bool + startCRCPrecacheThread = + config.getBool("PreCacheCRCThread", "true"); + //printf("### In [%s::%s Line: %d] precache thread enabled = %d SystemFlags::VERBOSE_MODE_ENABLED = %d\n",__FILE__,__FUNCTION__,__LINE__,startCRCPrecacheThread,SystemFlags::VERBOSE_MODE_ENABLED); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] precache thread enabled = %d\n", + __FILE__, __FUNCTION__, __LINE__, startCRCPrecacheThread); + if (startCRCPrecacheThread == true + && GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + static + string + mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + vector < string > techDataPaths = + config.getPathListForType(ptTechs); + + FileCRCPreCacheThread::setPreCacheThreadCacheLookupKey + (GameConstants::preCacheThreadCacheLookupKey); + FileCRCPreCacheThread *& + preCacheCRCThreadPtr = + CacheManager::getCachedItem < + FileCRCPreCacheThread * + >(GameConstants::preCacheThreadCacheLookupKey); + if (preCacheCRCThreadPtr == NULL) { + preCacheCRCThreadPtr = new FileCRCPreCacheThread(); + } + preCacheThread = preCacheCRCThreadPtr; + preCacheThread->setUniqueID(mutexOwnerId); + preCacheThread->setTechDataPaths(techDataPaths); + //preCacheThread->setFileCRCPreCacheThreadCallbackInterface(&preCacheThreadGame); + preCacheThread->start(); + } + + auto_ptr < NavtiveLanguageNameListCacheGenerator > lngCacheGen; + auto_ptr < SimpleTaskThread > languageCacheGen; + + bool + startNativeLanguageNamesPrecacheThread = + config.getBool("PreCacheNativeLanguageNamesThread", "true"); + if (startNativeLanguageNamesPrecacheThread == true + && GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + lngCacheGen.reset(new NavtiveLanguageNameListCacheGenerator()); + languageCacheGen.reset(new + SimpleTaskThread(lngCacheGen.get(), 1)); + + languageCacheGen->start(); + } + + // test + //Shared::Platform::MessageBox(NULL,"Mark's test.","Test",0); + //throw megaglest_runtime_error("test!"); + //ExceptionHandler::DisplayMessage("test!", false); + + // Check for commands being input from stdin + string + command = ""; #ifndef WIN32 - pollfd - cinfd[1]; + pollfd + cinfd[1]; #else - HANDLE - h = 0; + HANDLE + h = 0; #endif - if (disableheadless_console == false) - { + if (disableheadless_console == false) { #ifndef WIN32 -// Theoretically this should always be 0, but one fileno call isn't going to hurt, and if -// we try to run somewhere that stdin isn't fd 0 then it will still just work - cinfd[0].fd = fileno (stdin); - cinfd[0].events = POLLIN; + // Theoretically this should always be 0, but one fileno call isn't going to hurt, and if + // we try to run somewhere that stdin isn't fd 0 then it will still just work + cinfd[0].fd = fileno(stdin); + cinfd[0].events = POLLIN; #else - h = GetStdHandle (STD_INPUT_HANDLE); -//DWORD dwMode; -//GetConsoleMode(h, &dwMode); -//SetConsoleMode(h, dwMode & ~ENABLE_MOUSE_INPUT); - FlushConsoleInputBuffer (h); + h = GetStdHandle(STD_INPUT_HANDLE); + //DWORD dwMode; + //GetConsoleMode(h, &dwMode); + //SetConsoleMode(h, dwMode & ~ENABLE_MOUSE_INPUT); + FlushConsoleInputBuffer(h); #endif - } + } - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - printf ("Headless server is now running...\n"); - printf ("To shutdown type: quit\n"); - printf ("All commands require you to press ENTER\n"); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + printf("Headless server is now running...\n"); + printf("To shutdown type: quit\n"); + printf("All commands require you to press ENTER\n"); + } -//throw megaglest_runtime_error("Test!"); -//printf("About to throw an exception...\n"); -//throw 123; + //throw megaglest_runtime_error("Test!"); + //printf("About to throw an exception...\n"); + //throw 123; -//main loop - while (program->isShutdownApplicationEnabled () == false - &&::Shared::Platform::Window::handleEvent ()) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { + //main loop + while (program->isShutdownApplicationEnabled() == false + && ::Shared::Platform::Window::handleEvent()) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - if (disableheadless_console == false) - { + if (disableheadless_console == false) { #ifndef WIN32 - int - pollresult = poll (cinfd, 1, 0); - int - pollerror = errno; - if (pollresult) + int + pollresult = poll(cinfd, 1, 0); + int + pollerror = errno; + if (pollresult) #else -// This is problematic because input on Windows is not line-buffered so this will return -// even if getline may block. I haven't found a good way to fix it, so for the moment -// I just strongly suggest only running the server from the Python frontend, which does -// line buffer input. This does work okay as long as the user doesn't enter characters -// without pressing enter, and then try to end the server another way (say a remote -// console command), in which case we'll still be waiting for the stdin EOL and hang. + // This is problematic because input on Windows is not line-buffered so this will return + // even if getline may block. I haven't found a good way to fix it, so for the moment + // I just strongly suggest only running the server from the Python frontend, which does + // line buffer input. This does work okay as long as the user doesn't enter characters + // without pressing enter, and then try to end the server another way (say a remote + // console command), in which case we'll still be waiting for the stdin EOL and hang. - DWORD - saveMode; - GetConsoleMode (h, &saveMode); - DWORD - dwMode = saveMode; - dwMode &= ~ENABLE_MOUSE_INPUT; - dwMode &= ~ENABLE_WINDOW_INPUT; - SetConsoleMode (h, dwMode); + DWORD + saveMode; + GetConsoleMode(h, &saveMode); + DWORD + dwMode = saveMode; + dwMode &= ~ENABLE_MOUSE_INPUT; + dwMode &= ~ENABLE_WINDOW_INPUT; + SetConsoleMode(h, dwMode); - bool - gotData = (WaitForSingleObject (h, 0) == WAIT_OBJECT_0); - SetConsoleMode (h, saveMode); - if (gotData == true) + bool + gotData = (WaitForSingleObject(h, 0) == WAIT_OBJECT_0); + SetConsoleMode(h, saveMode); + if (gotData == true) #endif - { + { #ifdef WIN32 - bool - skip = true; - DWORD - nNumberOfCharsToRead = 1024; - DWORD - nRead = 0; - INPUT_RECORD - irInRec[1025]; + bool + skip = true; + DWORD + nNumberOfCharsToRead = 1024; + DWORD + nRead = 0; + INPUT_RECORD + irInRec[1025]; - PeekConsoleInput (h, &irInRec[0], nNumberOfCharsToRead, - &nRead); - for (int i = 0; i < nRead; ++i) - { - INPUT_RECORD & inr = irInRec[i]; + PeekConsoleInput(h, &irInRec[0], nNumberOfCharsToRead, + &nRead); + for (int i = 0; i < nRead; ++i) { + INPUT_RECORD & inr = irInRec[i]; -//printf("inr.EventType = %d\n",inr.EventType); - if (inr.EventType == KEY_EVENT) - { - if (inr.Event.KeyEvent.bKeyDown) - { - char - cHoldKey = inr.Event.KeyEvent.uChar.AsciiChar; - if (cHoldKey == '\r') - { - skip = false; - break; - } - } - } - } + //printf("inr.EventType = %d\n",inr.EventType); + if (inr.EventType == KEY_EVENT) { + if (inr.Event.KeyEvent.bKeyDown) { + char + cHoldKey = inr.Event.KeyEvent.uChar.AsciiChar; + if (cHoldKey == '\r') { + skip = false; + break; + } + } + } + } #else - bool - skip = false; + bool + skip = false; #endif - if (skip == false) - { - getline (cin, command); - cin.clear (); + if (skip == false) { + getline(cin, command); + cin.clear(); - printf ("server command [%s]\n", command.c_str ()); - if (command == "quit") - { - break; - } + printf("server command [%s]\n", command.c_str()); + if (command == "quit") { + break; + } #ifndef WIN32 - if (cinfd[0].revents & POLLNVAL) - { - printf ("invalid file descriptor\n"); - } - if (cinfd[0].revents & POLLERR) - { - printf ("error in file descriptor\n"); - } - if (cinfd[0].revents & POLLHUP) - { - printf ("hang up in file descriptor\n"); - } + if (cinfd[0].revents & POLLNVAL) { + printf("invalid file descriptor\n"); + } + if (cinfd[0].revents & POLLERR) { + printf("error in file descriptor\n"); + } + if (cinfd[0].revents & POLLHUP) { + printf("hang up in file descriptor\n"); + } - if (pollresult < 0) - { - printf ("pollresult = %d errno = %d [%s]\n", pollresult, - pollerror, strerror (pollerror)); + if (pollresult < 0) { + printf("pollresult = %d errno = %d [%s]\n", pollresult, + pollerror, strerror(pollerror)); - cinfd[0].fd = fileno (stdin); - cinfd[0].events = POLLIN; - } + cinfd[0].fd = fileno(stdin); + cinfd[0].events = POLLIN; + } #endif - } - } - } -//printf("looping\n"); - } + } + } + } + //printf("looping\n"); + } - program->loop (); + program->loop(); -// Because OpenGL really doesn't do multi-threading well -// if(difftime(time(NULL),lastTextureLoadEvent) >= 3) { -// lastTextureLoadEvent = time(NULL); -// vector textureList = preCacheThread->getPendingTextureList(1); -// for(unsigned int i = 0; i < textureList.size(); ++i) { -// Texture2D * factionLogo = textureList[i]; -// if(factionLogo != NULL) { -// printf("\n\n\n\n|||||||||||||||||||||||||| Load texture [%s]\n",factionLogo->getPath().c_str()); -// //Renderer::findTexture(factionLogo); -// renderer.initTexture(rsGlobal,factionLogo); -// } -// } -// } - } + // Because OpenGL really doesn't do multi-threading well + // if(difftime(time(NULL),lastTextureLoadEvent) >= 3) { + // lastTextureLoadEvent = time(NULL); + // vector textureList = preCacheThread->getPendingTextureList(1); + // for(unsigned int i = 0; i < textureList.size(); ++i) { + // Texture2D * factionLogo = textureList[i]; + // if(factionLogo != NULL) { + // printf("\n\n\n\n|||||||||||||||||||||||||| Load texture [%s]\n",factionLogo->getPath().c_str()); + // //Renderer::findTexture(factionLogo); + // renderer.initTexture(rsGlobal,factionLogo); + // } + // } + // } + } - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - printf ("\nHeadless server is about to quit...\n"); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + printf("\nHeadless server is about to quit...\n"); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] starting normal application shutdown\n", - __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] starting normal application shutdown\n", + __FILE__, __FUNCTION__, __LINE__); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = program->getSoundThreadManager (true); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = program->getSoundThreadManager(true); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + } - cleanupCRCThread (); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + cleanupCRCThread(); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - showCursor (true); -//showWindowCursorState = true; - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - catch (const megaglest_runtime_error & e) - { + showCursor(true); + //showWindowCursorState = true; + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } catch (const megaglest_runtime_error & e) { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = - (program != NULL ? program->getSoundThreadManager (true) : NULL); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - if (program != NULL && - program->getTryingRendererInit () == true && - program->getRendererInitOk () == false) - { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = + (program != NULL ? program->getSoundThreadManager(true) : NULL); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + if (program != NULL && + program->getTryingRendererInit() == true && + program->getRendererInitOk() == false) { - message (e.what (), - GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } - } + message(e.what(), + GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } + } - if (program == NULL || program->getTryingRendererInit () == false || - (program->getTryingRendererInit () == true && - program->getRendererInitOk () == true)) - { + if (program == NULL || program->getTryingRendererInit() == false || + (program->getTryingRendererInit() == true && + program->getRendererInitOk() == true)) { - ExceptionHandler::handleRuntimeError (e); - } - } - catch (const exception & e) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = - (program != NULL ? program->getSoundThreadManager (true) : NULL); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - } + ExceptionHandler::handleRuntimeError(e); + } + } catch (const exception & e) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = + (program != NULL ? program->getSoundThreadManager(true) : NULL); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + } - ExceptionHandler::handleRuntimeError (e.what (), true); - } - catch (const char *e) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = - (program != NULL ? program->getSoundThreadManager (true) : NULL); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - } + ExceptionHandler::handleRuntimeError(e.what(), true); + } catch (const char *e) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = + (program != NULL ? program->getSoundThreadManager(true) : NULL); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + } - ExceptionHandler::handleRuntimeError (e, true); - } - catch (const string & ex) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = - (program != NULL ? program->getSoundThreadManager (true) : NULL); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - } + ExceptionHandler::handleRuntimeError(e, true); + } catch (const string & ex) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = + (program != NULL ? program->getSoundThreadManager(true) : NULL); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + } - ExceptionHandler::handleRuntimeError (ex.c_str (), true); - } + ExceptionHandler::handleRuntimeError(ex.c_str(), true); + } #if !defined(HAVE_GOOGLE_BREAKPAD) - catch ( ...) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - soundThreadManager = - (program != NULL ? program->getSoundThreadManager (true) : NULL); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (shutdownFadeSoundMilliseconds); - chronoshutdownFadeSound.start (); - } - } + catch (...) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + soundThreadManager = + (program != NULL ? program->getSoundThreadManager(true) : NULL); + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(shutdownFadeSoundMilliseconds); + chronoshutdownFadeSound.start(); + } + } - ExceptionHandler::handleRuntimeError ("Unknown error [main]!", true); - } + ExceptionHandler::handleRuntimeError("Unknown error [main]!", true); + } #endif - cleanupCRCThread (); + cleanupCRCThread(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - delete - mainWindow; - mainWindow = NULL; + delete + mainWindow; + mainWindow = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - GraphicComponent::clearRegisteredComponents (); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + GraphicComponent::clearRegisteredComponents(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - if (soundThreadManager) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - if (Config::getInstance ().getString ("FactorySound", "") != "None" && - soundRenderer.isVolumeTurnedOff () == false) - { + if (soundThreadManager) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + if (Config::getInstance().getString("FactorySound", "") != "None" && + soundRenderer.isVolumeTurnedOff() == false) { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - for (; - chronoshutdownFadeSound.getMillis () <= - shutdownFadeSoundMilliseconds;) - { - sleep (10); - } - } + for (; + chronoshutdownFadeSound.getMillis() <= + shutdownFadeSoundMilliseconds;) { + sleep(10); + } + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - BaseThread::shutdownAndWait (soundThreadManager); + BaseThread::shutdownAndWait(soundThreadManager); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - delete - soundThreadManager; - soundThreadManager = NULL; - } + delete + soundThreadManager; + soundThreadManager = NULL; + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - return 0; - } + return 0; + } #if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) - void - handleSIGSEGV (int sig) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] Error detected: signal %d:\n", __FILE__, - __FUNCTION__, __LINE__, sig); - printf ("%s", szBuf); -//abort(); + void + handleSIGSEGV(int sig) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] Error detected: signal %d:\n", __FILE__, + __FUNCTION__, __LINE__, sig); + printf("%s", szBuf); + //abort(); - ExceptionHandler::handleRuntimeError (szBuf, true); - } + ExceptionHandler::handleRuntimeError(szBuf, true); + } #endif #if defined(HAVE_GOOGLE_BREAKPAD) # if defined(WIN32) -// Callback when minidump written. - static - bool - MinidumpCallback (const wchar_t * dump_path, - const wchar_t * minidump_id, - void *context, - EXCEPTION_POINTERS * exinfo, - MDRawAssertionInfo * assertion, bool succeeded) - { - printf ("\n======= In MinidumpCallback...\n"); - wprintf - (L"\n***ERROR details captured:\nCrash minidump folder: %s\nfile: %s.dmp\nSucceeded: %d\n", - (dump_path != NULL ? dump_path : L"(null)"), - (minidump_id != NULL ? minidump_id : L"(null)"), succeeded); + // Callback when minidump written. + static + bool + MinidumpCallback(const wchar_t * dump_path, + const wchar_t * minidump_id, + void *context, + EXCEPTION_POINTERS * exinfo, + MDRawAssertionInfo * assertion, bool succeeded) { + printf("\n======= In MinidumpCallback...\n"); + wprintf + (L"\n***ERROR details captured:\nCrash minidump folder: %s\nfile: %s.dmp\nSucceeded: %d\n", + (dump_path != NULL ? dump_path : L"(null)"), + (minidump_id != NULL ? minidump_id : L"(null)"), succeeded); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - wchar_t - szBuf[8096]; - int - bufBytes = _snwprintf (szBuf, 8096, - L"An unhandled error was detected.\n\nA crash dump file has been created in the folder:\n%s\nCrash dump filename is: %s.dmp", - dump_path, minidump_id); - szBuf[bufBytes] = '\0'; - MessageBox (NULL, szBuf, L"Unhandled error", MB_OK | MB_SYSTEMMODAL); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + wchar_t + szBuf[8096]; + int + bufBytes = _snwprintf(szBuf, 8096, + L"An unhandled error was detected.\n\nA crash dump file has been created in the folder:\n%s\nCrash dump filename is: %s.dmp", + dump_path, minidump_id); + szBuf[bufBytes] = '\0'; + MessageBox(NULL, szBuf, L"Unhandled error", MB_OK | MB_SYSTEMMODAL); + } - return succeeded; - } + return succeeded; + } # else -// Callback when minidump written. - static - bool - MinidumpCallback (const google_breakpad::MinidumpDescriptor & descriptor, - void *context, bool succeeded) - { - printf ("\n======= In MinidumpCallback...\n"); - printf - ("\n***ERROR details captured:\nCrash minidump folder: %s\nfile: %s\nSucceeded: %d\n", - descriptor.directory ().c_str (), descriptor.path (), succeeded); + // Callback when minidump written. + static + bool + MinidumpCallback(const google_breakpad::MinidumpDescriptor & descriptor, + void *context, bool succeeded) { + printf("\n======= In MinidumpCallback...\n"); + printf + ("\n***ERROR details captured:\nCrash minidump folder: %s\nfile: %s\nSucceeded: %d\n", + descriptor.directory().c_str(), descriptor.path(), succeeded); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - char - szBuf[8096]; - snprintf (szBuf, 8096, - "An unhandled error was detected.\n\nA crash dump file has been created in the folder:\n%s\nCrash dump filename is: %s", - descriptor.directory ().c_str (), descriptor.path ()); - message (szBuf, GlobalStaticFlags::getIsNonGraphicalModeEnabled (), - tempDataLocation); - } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + char + szBuf[8096]; + snprintf(szBuf, 8096, + "An unhandled error was detected.\n\nA crash dump file has been created in the folder:\n%s\nCrash dump filename is: %s", + descriptor.directory().c_str(), descriptor.path()); + message(szBuf, GlobalStaticFlags::getIsNonGraphicalModeEnabled(), + tempDataLocation); + } - return succeeded; - } + return succeeded; + } # endif #endif #ifdef WIN32 - void - EnableCrashingOnCrashes () - { - typedef - BOOL (WINAPI * tGetPolicy) (LPDWORD lpFlags); - typedef - BOOL (WINAPI * tSetPolicy) (DWORD dwFlags); - const - DWORD - EXCEPTION_SWALLOWING = 0x1; + void + EnableCrashingOnCrashes() { + typedef + BOOL(WINAPI * tGetPolicy) (LPDWORD lpFlags); + typedef + BOOL(WINAPI * tSetPolicy) (DWORD dwFlags); + const + DWORD + EXCEPTION_SWALLOWING = 0x1; - HMODULE - kernel32 = LoadLibraryA ("kernel32.dll"); - if (kernel32 != 0) - { - tGetPolicy - pGetPolicy = - (tGetPolicy) GetProcAddress (kernel32, - "GetProcessUserModeExceptionPolicy"); - tSetPolicy - pSetPolicy = - (tSetPolicy) GetProcAddress (kernel32, - "SetProcessUserModeExceptionPolicy"); - if (pGetPolicy && pSetPolicy) - { - DWORD - dwFlags; - if (pGetPolicy (&dwFlags)) - { -// Turn off the filter - pSetPolicy (dwFlags & ~EXCEPTION_SWALLOWING); - } - } - } - } + HMODULE + kernel32 = LoadLibraryA("kernel32.dll"); + if (kernel32 != 0) { + tGetPolicy + pGetPolicy = + (tGetPolicy) GetProcAddress(kernel32, + "GetProcessUserModeExceptionPolicy"); + tSetPolicy + pSetPolicy = + (tSetPolicy) GetProcAddress(kernel32, + "SetProcessUserModeExceptionPolicy"); + if (pGetPolicy && pSetPolicy) { + DWORD + dwFlags; + if (pGetPolicy(&dwFlags)) { + // Turn off the filter + pSetPolicy(dwFlags & ~EXCEPTION_SWALLOWING); + } + } + } + } #endif - int - glestMainSEHWrapper (int argc, char **argv) - { - int - result = 0; + int + glestMainSEHWrapper(int argc, char **argv) { + int + result = 0; #ifdef WIN32_STACK_TRACE -//printf("Hooking up WIN32_STACK_TRACE...\n"); - __try - { + //printf("Hooking up WIN32_STACK_TRACE...\n"); + __try { #endif -//application_binary= executable_path(argv[0],true); -//printf("\n\nargv0 [%s] application_binary [%s]\n\n",argv[0],application_binary.c_str()); + //application_binary= executable_path(argv[0],true); + //printf("\n\nargv0 [%s] application_binary [%s]\n\n",argv[0],application_binary.c_str()); #if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) - if (hasCommandArgument - (argc, argv, - string (GAME_ARGS[GAME_ARG_DISABLE_SIGSEGV_HANDLER])) == false) - { - signal (SIGSEGV, handleSIGSEGV); - } + if (hasCommandArgument + (argc, argv, + string(GAME_ARGS[GAME_ARG_DISABLE_SIGSEGV_HANDLER])) == false) { + signal(SIGSEGV, handleSIGSEGV); + } -// http://developerweb.net/viewtopic.php?id=3013 -//signal(SIGPIPE, SIG_IGN); + // http://developerweb.net/viewtopic.php?id=3013 + //signal(SIGPIPE, SIG_IGN); #endif - initSpecialStrings (); + initSpecialStrings(); - IRCThread::setGlobalCacheContainerName - (GameConstants::ircClientCacheLookupKey); - result = glestMain (argc, argv); + IRCThread::setGlobalCacheContainerName + (GameConstants::ircClientCacheLookupKey); + result = glestMain(argc, argv); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - cleanupProcessObjects (); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + cleanupProcessObjects(); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); #ifdef WIN32 -//delete winSockManager; -//winSockManager = NULL; + //delete winSockManager; + //winSockManager = NULL; #endif - if (sdl_quitCalled == false) - { - sdl_quitCalled = true; - SDL_Quit (); - } + if (sdl_quitCalled == false) { + sdl_quitCalled = true; + SDL_Quit(); + } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); #ifdef WIN32_STACK_TRACE - } - __except (stackdumper (0, GetExceptionInformation (), true), - EXCEPTION_CONTINUE_SEARCH) - { - return 0; - } + } __except (stackdumper(0, GetExceptionInformation(), true), + EXCEPTION_CONTINUE_SEARCH) { + return 0; + } #endif - return result; - } + return result; + } - int - glestMainWrapper (int argc, char **argv) - { -//setlocale(LC_ALL, "zh_TW.UTF-8"); -//setlocale(LC_ALL, ""); + int + glestMainWrapper(int argc, char **argv) { + //setlocale(LC_ALL, "zh_TW.UTF-8"); + //setlocale(LC_ALL, ""); #ifdef WIN32 - EnableCrashingOnCrashes (); + EnableCrashingOnCrashes(); #endif #if defined(HAVE_GOOGLE_BREAKPAD) -/* -handler = new ExceptionHandler(const wstring& dump_path, -FilterCallback filter, -MinidumpCallback callback, -void* callback_context, -int handler_types, -MINIDUMP_TYPE dump_type, -const wchar_t* pipe_name, -const CustomClientInfo* custom_info); -*/ + /* + handler = new ExceptionHandler(const wstring& dump_path, + FilterCallback filter, + MinidumpCallback callback, + void* callback_context, + int handler_types, + MINIDUMP_TYPE dump_type, + const wchar_t* pipe_name, + const CustomClientInfo* custom_info); + */ -// See this link about swallowed exceptions in Win 7: http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/ -//DWORD dwFlags; -//if (GetProcessUserModeExceptionPolicy(&dwFlags)) { -// SetProcessUserModeExceptionPolicy(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED); // turn off bit 1 -//} + // See this link about swallowed exceptions in Win 7: http://blog.paulbetts.org/index.php/2010/07/20/the-case-of-the-disappearing-onload-exception-user-mode-callback-exceptions-in-x64/ + //DWORD dwFlags; + //if (GetProcessUserModeExceptionPolicy(&dwFlags)) { + // SetProcessUserModeExceptionPolicy(dwFlags & ~PROCESS_CALLBACK_FILTER_ENABLED); // turn off bit 1 + //} -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Hooking up google_breakpad::ExceptionHandler...\n"); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Hooking up google_breakpad::ExceptionHandler...\n"); # if defined(WIN32) - wstring - dumpfilepath = utf8_decode ("."); -//google_breakpad::ExceptionHandler handler(dumpfilepath, NULL, MinidumpCallback, NULL, true); - errorHandlerPtr.reset (new - google_breakpad::ExceptionHandler (dumpfilepath, - NULL, - MinidumpCallback, - NULL, - google_breakpad::ExceptionHandler:: - HANDLER_ALL)); + wstring + dumpfilepath = utf8_decode("."); + //google_breakpad::ExceptionHandler handler(dumpfilepath, NULL, MinidumpCallback, NULL, true); + errorHandlerPtr.reset(new + google_breakpad::ExceptionHandler(dumpfilepath, + NULL, + MinidumpCallback, + NULL, + google_breakpad::ExceptionHandler:: + HANDLER_ALL)); # else - google_breakpad::MinidumpDescriptor descriptor ("."); - errorHandlerPtr.reset (new - google_breakpad::ExceptionHandler (descriptor, - NULL, - MinidumpCallback, - NULL, true, - -1)); + google_breakpad::MinidumpDescriptor descriptor("."); + errorHandlerPtr.reset(new + google_breakpad::ExceptionHandler(descriptor, + NULL, + MinidumpCallback, + NULL, true, + -1)); # endif -// ExceptionHandler(const wstring& dump_path, -// FilterCallback filter, -// MinidumpCallback callback, -// void* callback_context, -// int handler_types); + // ExceptionHandler(const wstring& dump_path, + // FilterCallback filter, + // MinidumpCallback callback, + // void* callback_context, + // int handler_types); #endif @@ -9493,36 +8413,33 @@ const CustomClientInfo* custom_info); #ifdef WIN32 //winSockManager = new SocketManager(); - SocketManager - winSockManager; + SocketManager + winSockManager; #endif - bool - isSteamMode = - hasCommandArgument (argc, argv, GAME_ARGS[GAME_ARG_STEAM]); - if (isSteamMode == true) - { - if (!STEAMSHIM_init ()) - { - printf ("\nSteam API init failed, terminating.\n"); - return 42; - } - } + bool + isSteamMode = + hasCommandArgument(argc, argv, GAME_ARGS[GAME_ARG_STEAM]); + if (isSteamMode == true) { + if (!STEAMSHIM_init()) { + printf("\nSteam API init failed, terminating.\n"); + return 42; + } + } - int - result = glestMainSEHWrapper (argc, argv); + int + result = glestMainSEHWrapper(argc, argv); - if (isSteamMode == true) - { - printf ("\nSteam API deinit.\n"); - STEAMSHIM_deinit (); - } + if (isSteamMode == true) { + printf("\nSteam API deinit.\n"); + STEAMSHIM_deinit(); + } - return result; - } + return result; + } - } + } } //end namespace -MAIN_FUNCTION (Glest::Game::glestMainWrapper) +MAIN_FUNCTION(Glest::Game::glestMainWrapper) diff --git a/source/glest_game/main/main.h b/source/glest_game/main/main.h index d72227e13..d7f4857ec 100644 --- a/source/glest_game/main/main.h +++ b/source/glest_game/main/main.h @@ -20,95 +20,91 @@ using Shared::Platform::MouseButton; using Shared::Platform::MouseState; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class MainWindow -// -/// Main program window -// ===================================================== + // ===================================================== + // class MainWindow + // + /// Main program window + // ===================================================== - class MainWindow: - public WindowGl - { - private: - Program * program; - PopupMenu popupMenu; - int - cancelLanguageSelection; - bool triggerLanguageToggle; - string triggerLanguage; + class MainWindow : + public WindowGl { + private: + Program * program; + PopupMenu popupMenu; + int + cancelLanguageSelection; + bool triggerLanguageToggle; + string triggerLanguage; - void - showLanguages (); + void + showLanguages(); - public: - explicit MainWindow (Program * program); - ~MainWindow (); + public: + explicit MainWindow(Program * program); + ~MainWindow(); - void - setProgram (Program * program); + void + setProgram(Program * program); - virtual void - eventMouseDown (int x, int y, MouseButton mouseButton); - virtual void - eventMouseUp (int x, int y, MouseButton mouseButton); - virtual void - eventMouseDoubleClick (int x, int y, MouseButton mouseButton); - virtual void - eventMouseMove (int x, int y, const MouseState * mouseState); - virtual - bool - eventTextInput (std::string text); - virtual - bool - eventSdlKeyDown (SDL_KeyboardEvent key); - virtual void - eventKeyDown (SDL_KeyboardEvent key); - virtual void - eventMouseWheel (int x, int y, int zDelta); - virtual void - eventKeyUp (SDL_KeyboardEvent key); - virtual void - eventKeyPress (SDL_KeyboardEvent c); - virtual void - eventActivate (bool active); - virtual void - eventResize (SizeState sizeState); - virtual void - eventClose (); - virtual void - eventWindowEvent (SDL_WindowEvent event); + virtual void + eventMouseDown(int x, int y, MouseButton mouseButton); + virtual void + eventMouseUp(int x, int y, MouseButton mouseButton); + virtual void + eventMouseDoubleClick(int x, int y, MouseButton mouseButton); + virtual void + eventMouseMove(int x, int y, const MouseState * mouseState); + virtual + bool + eventTextInput(std::string text); + virtual + bool + eventSdlKeyDown(SDL_KeyboardEvent key); + virtual void + eventKeyDown(SDL_KeyboardEvent key); + virtual void + eventMouseWheel(int x, int y, int zDelta); + virtual void + eventKeyUp(SDL_KeyboardEvent key); + virtual void + eventKeyPress(SDL_KeyboardEvent c); + virtual void + eventActivate(bool active); + virtual void + eventResize(SizeState sizeState); + virtual void + eventClose(); + virtual void + eventWindowEvent(SDL_WindowEvent event); - virtual void - render (); - void - toggleLanguage (string language); - bool getTriggerLanguageToggle ()const - { - return - triggerLanguageToggle; - } - string - getTriggerLanguage () const - { - return - triggerLanguage; - } + virtual void + render(); + void + toggleLanguage(string language); + bool getTriggerLanguageToggle()const { + return + triggerLanguageToggle; + } + string + getTriggerLanguage() const { + return + triggerLanguage; + } - virtual int - getDesiredScreenWidth (); - virtual int - getDesiredScreenHeight (); + virtual int + getDesiredScreenWidth(); + virtual int + getDesiredScreenHeight(); - protected: - virtual void - eventToggleFullScreen (bool isFullscreen); - }; + protected: + virtual void + eventToggleFullScreen(bool isFullscreen); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 0cc689526..ae316037b 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -42,1667 +42,1524 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using - namespace - Shared::Graphics; +namespace + Shared::Graphics; using - namespace - Shared::Graphics::Gl; +namespace + Shared::Graphics::Gl; // ===================================================== // class Program // ===================================================== -namespace Glest -{ - namespace Game - { - - const int - Program::maxTimes = 10; - Program * - Program::singleton = NULL; - const int - SOUND_THREAD_UPDATE_MILLISECONDS = 25; - - bool Program::wantShutdownApplicationAfterGame = false; - const char * - ProgramState::MAIN_PROGRAM_RENDER_KEY = "ZETAGLEST.RENDER"; - -// ===================================================== -// class Program::CrashProgramState -// ===================================================== - - ProgramState::ProgramState (Program * program) - { - this->program = program; - this->forceMouseRender = false; - this->mouseX = 0; - this->mouseY = 0; - this->mouse2dAnim = 0; - this->fps = 0; - this->lastFps = 0; - this->startX = 0; - this->startY = 0; - } - - void - ProgramState::restoreToStartXY () - { - SDL_WarpMouseInWindow (this->program->getWindow ()->getSDLWindow (), - startX, startY); - } - - void - ProgramState::incrementFps () - { - fps++; - } - - void - ProgramState::tick () - { - lastFps = fps; - fps = 0; - } - - bool ProgramState::canRender (bool sleepIfCannotRender) - { - int - maxFPSCap = Config::getInstance ().getInt ("RenderFPSCap", "500"); - int - sleepMillis = - Config::getInstance ().getInt ("RenderFPSCapSleepMillis", "1"); -//Renderer &renderer= Renderer::getInstance(); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - maxFPSCap = - Config::getInstance ().getInt ("RenderFPSCapHeadless", "250"); - sleepMillis = - Config::getInstance ().getInt ("RenderFPSCapHeadlessSleepMillis", - "1"); - } - - if (lastFps > maxFPSCap) - { - if (sleepIfCannotRender == true) - { - sleep (sleepMillis); -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] sleeping because lastFps = %d, maxFPSCap = %d sleepMillis = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,lastFps,maxFPSCap,sleepMillis); - } - return false; - } - return true; - } - - void - ProgramState::render () - { - Renderer & renderer = Renderer::getInstance (); - - canRender (); - incrementFps (); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - renderer.clearBuffers (); - renderer.reset2d (); - renderer.renderMessageBox (program->getMsgBox ()); - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - renderer.swapBuffers (); - } - } - - void - ProgramState::update () - { - mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; - } - - void - ProgramState::mouseMove (int x, int y, const MouseState * mouseState) - { - mouseX = x; - mouseY = y; - program->getMsgBox ()->mouseMove (x, y); - } - - Program::ShowMessageProgramState::ShowMessageProgramState (Program * - program, - const char - *msg): - ProgramState (program) - { - userWantsExit = false; - msgBox.registerGraphicComponent ("ShowMessageProgramState", "msgBox"); - msgBox.init ("Ok"); - - if (msg) - { - fprintf (stderr, "%s\n", msg); - msgBox.setText (msg); - } - else - { - msgBox.setText ("ZetaGlest has crashed."); - } - - mouse2dAnim = mouseY = mouseX = 0; - this->msg = (msg ? msg : ""); - } - - void - Program::ShowMessageProgramState::render () - { - Renderer & renderer = Renderer::getInstance (); - renderer.clearBuffers (); - renderer.reset2d (); - renderer.renderMessageBox (&msgBox); - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - renderer.swapBuffers (); - } - - void - Program::ShowMessageProgramState::mouseDownLeft (int x, int y) - { - int - button = 0; - if (msgBox.mouseClick (x, y, button)) - { - program->exit (); - userWantsExit = true; - } - } - - void - Program::ShowMessageProgramState::keyPress (SDL_KeyboardEvent c) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d] c = [%d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - c.keysym.sym); - -// if user pressed return we exit -//if(c == 13) { - if (isKeyPressed (SDLK_RETURN, c) == true) - { - program->exit (); - userWantsExit = true; - } - } - - void - Program::ShowMessageProgramState::mouseMove (int x, int y, - const MouseState & - mouseState) - { - mouseX = x; - mouseY = y; - msgBox.mouseMove (x, y); - } - - void - Program::ShowMessageProgramState::update () - { - mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; - } - -// ===================== PUBLIC ======================== - - bool Program::rendererInitOk = false; - bool Program::tryingRendererInit = false; - - Program::Program ():msgBox ("Program", "msgBox") - { -//this->masterserverMode = false; - this->window = NULL; - this->shutdownApplicationEnabled = false; - this->skipRenderFrameCount = 0; - this->messageBoxIsSystemError = false; - this->programStateOldSystemError = NULL; - this->programState = NULL; - this->singleton = this; - this->soundThreadManager = NULL; - -//mesage box - Lang & lang = Lang::getInstance (); -//msgBox.registerGraphicComponent("Program", "msgBox"); - msgBox.init (lang.getString ("Ok")); - msgBox.setEnabled (false); - } - -//bool Program::isMasterserverMode() const { -// return this->masterserverMode; -//} - - void - Program::initNormal (WindowGl * window) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - Config & config = Config::getInstance (); - init (window); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - setState (new Intro (this)); - showCursor (config.getBool ("No2DMouseRendering", "false")); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void - Program::initSavedGame (WindowGl * window, bool masterserverMode, - string saveGameFile) - { - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - - if (saveGameFile == "") - { - Config & config = Config::getInstance (); - saveGameFile = config.getString ("LastSavedGame", ""); - if (saveGameFile == "") - { - saveGameFile = GameConstants::saveGameFileDefault; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - saveGameFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - saveGameFile; - } - else - { - string - userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameFile = userData + saveGameFile; - } - } - } - -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Loading game from [%s]\n",saveGameFile.c_str()); - printf ("Loading saved game from [%s]\n", saveGameFile.c_str ()); - - Game::loadGame (saveGameFile, this, masterserverMode); - } - - void - Program::initServer (WindowGl * window, bool autostart, - bool openNetworkSlots, bool masterserverMode) - { -//this->masterserverMode = masterserverMode; - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - mainMenu->setState (new - MenuStateCustomGame (this, mainMenu, - openNetworkSlots, pNewGame, - autostart, NULL, - masterserverMode)); - } - - void - Program::initServer (WindowGl * window, GameSettings * settings) - { - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - mainMenu->setState (new - MenuStateCustomGame (this, mainMenu, false, - pNewGame, true, settings)); - } - - void - Program::initClient (WindowGl * window, const Ip & serverIp, - int portNumber) - { - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - mainMenu->setState (new - MenuStateJoinGame (this, mainMenu, true, serverIp, - portNumber)); - } - - void - Program::initClientAutoFindHost (WindowGl * window) - { - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - bool autoFindHost = true; - mainMenu->setState (new - MenuStateJoinGame (this, mainMenu, &autoFindHost)); - - } - - void - Program::initScenario (WindowGl * window, string autoloadScenarioName) - { - init (window); - MainMenu * - mainMenu = new MainMenu (this); - setState (mainMenu); - mainMenu->setState (new MenuStateScenario (this, mainMenu, false, - Config:: - getInstance - ().getPathListForType - (ptScenarios), - autoloadScenarioName)); - } - - Program::~Program () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - delete programState; - programState = NULL; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - Renderer::getInstance ().end (); - CoreData & coreData = CoreData::getInstance (); - coreData.cleanup (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - -//restore video mode - restoreDisplaySettings (); - singleton = NULL; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - if (soundThreadManager != NULL) - { - BaseThread::shutdownAndWait (soundThreadManager); - delete soundThreadManager; - soundThreadManager = NULL; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - - void - Program::restoreStateFromSystemError () - { - messageBoxIsSystemError = false; - if (this->programStateOldSystemError == NULL) - { - setState (new Intro (this)); - } - else - { - setState (this->programStateOldSystemError); - } - } - - bool Program::textInput (std::string text) - { - if (msgBox.getEnabled ()) - { - return false; - } -//delegate event - return programState->textInput (text); - } - - bool Program::sdlKeyDown (SDL_KeyboardEvent key) - { -//delegate event - return programState->sdlKeyDown (key); - } - - void - Program::keyDown (SDL_KeyboardEvent key) - { - if (msgBox.getEnabled ()) - { -//SDL_keysym keystate = Window::getKeystate(); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//if(key == vkEscape || key == SDLK_ESCAPE || -// ((key == vkReturn || key == SDLK_RETURN || key == SDLK_KP_ENTER) && !(keystate.mod & (KMOD_LALT | KMOD_RALT)))) { - if (isKeyPressed (SDLK_ESCAPE, key) == true - || ((isKeyPressed (SDLK_RETURN, key) == true) - && !(key.keysym.mod & (KMOD_LALT | KMOD_RALT)))) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("---> keystate [%d]\n",keystate); - msgBox.setEnabled (false); - if (messageBoxIsSystemError == true) - { - restoreStateFromSystemError (); - } - } - } -//delegate event - programState->keyDown (key); - } - - void - Program::keyUp (SDL_KeyboardEvent key) - { - programState->keyUp (key); - } - - void - Program::keyPress (SDL_KeyboardEvent c) - { - programState->keyPress (c); - } - - void - Program::mouseDownLeft (int x, int y) - { - if (msgBox.getEnabled ()) - { - int - button = 0; - if (msgBox.mouseClick (x, y, button)) - { -//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -//close message box - msgBox.setEnabled (false); - if (messageBoxIsSystemError == true) - { - restoreStateFromSystemError (); - } - } - } - } - - void - Program::eventMouseMove (int x, int y, const MouseState * ms) - { - if (msgBox.getEnabled ()) - { - msgBox.mouseMove (x, y); - } - } - - void - Program::simpleTask (BaseThread * callingThread, void *userdata) - { - loopWorker (); - } - - void - Program::loop () - { - loopWorker (); - } - - void - Program::loopWorker () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] ================================= MAIN LOOP START ================================= \n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//Renderer &renderer= Renderer::getInstance(); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - && window) - { - MainWindow * - mainWindow = dynamic_cast < MainWindow * >(window); - if (mainWindow) - { -//mainWindow->render(); - if (mainWindow->getTriggerLanguageToggle ()) - { - mainWindow->toggleLanguage (mainWindow->getTriggerLanguage ()); - } - } - } - - Chrono chronoPerformanceCounts; - - bool - showPerfStats = - Config::getInstance ().getBool ("ShowPerfStats", "false"); - Chrono chronoPerf; - char - perfBuf[8096] = ""; - std::vector < string > perfList; - if (showPerfStats) - chronoPerf.start (); - - Chrono chronoLoop; +namespace Glest { + namespace Game { + + const int + Program::maxTimes = 10; + Program * + Program::singleton = NULL; + const int + SOUND_THREAD_UPDATE_MILLISECONDS = 25; + + bool Program::wantShutdownApplicationAfterGame = false; + const char * + ProgramState::MAIN_PROGRAM_RENDER_KEY = "ZETAGLEST.RENDER"; + + // ===================================================== + // class Program::CrashProgramState + // ===================================================== + + ProgramState::ProgramState(Program * program) { + this->program = program; + this->forceMouseRender = false; + this->mouseX = 0; + this->mouseY = 0; + this->mouse2dAnim = 0; + this->fps = 0; + this->lastFps = 0; + this->startX = 0; + this->startY = 0; + } + + void + ProgramState::restoreToStartXY() { + SDL_WarpMouseInWindow(this->program->getWindow()->getSDLWindow(), + startX, startY); + } + + void + ProgramState::incrementFps() { + fps++; + } + + void + ProgramState::tick() { + lastFps = fps; + fps = 0; + } + + bool ProgramState::canRender(bool sleepIfCannotRender) { + int + maxFPSCap = Config::getInstance().getInt("RenderFPSCap", "500"); + int + sleepMillis = + Config::getInstance().getInt("RenderFPSCapSleepMillis", "1"); + //Renderer &renderer= Renderer::getInstance(); + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + maxFPSCap = + Config::getInstance().getInt("RenderFPSCapHeadless", "250"); + sleepMillis = + Config::getInstance().getInt("RenderFPSCapHeadlessSleepMillis", + "1"); + } + + if (lastFps > maxFPSCap) { + if (sleepIfCannotRender == true) { + sleep(sleepMillis); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] sleeping because lastFps = %d, maxFPSCap = %d sleepMillis = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,lastFps,maxFPSCap,sleepMillis); + } + return false; + } + return true; + } + + void + ProgramState::render() { + Renderer & renderer = Renderer::getInstance(); + + canRender(); + incrementFps(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + renderer.clearBuffers(); + renderer.reset2d(); + renderer.renderMessageBox(program->getMsgBox()); + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + renderer.swapBuffers(); + } + } + + void + ProgramState::update() { + mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; + } + + void + ProgramState::mouseMove(int x, int y, const MouseState * mouseState) { + mouseX = x; + mouseY = y; + program->getMsgBox()->mouseMove(x, y); + } + + Program::ShowMessageProgramState::ShowMessageProgramState(Program * + program, + const char + *msg) : + ProgramState(program) { + userWantsExit = false; + msgBox.registerGraphicComponent("ShowMessageProgramState", "msgBox"); + msgBox.init("Ok"); + + if (msg) { + fprintf(stderr, "%s\n", msg); + msgBox.setText(msg); + } else { + msgBox.setText("ZetaGlest has crashed."); + } + + mouse2dAnim = mouseY = mouseX = 0; + this->msg = (msg ? msg : ""); + } + + void + Program::ShowMessageProgramState::render() { + Renderer & renderer = Renderer::getInstance(); + renderer.clearBuffers(); + renderer.reset2d(); + renderer.renderMessageBox(&msgBox); + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + renderer.swapBuffers(); + } + + void + Program::ShowMessageProgramState::mouseDownLeft(int x, int y) { + int + button = 0; + if (msgBox.mouseClick(x, y, button)) { + program->exit(); + userWantsExit = true; + } + } + + void + Program::ShowMessageProgramState::keyPress(SDL_KeyboardEvent c) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d] c = [%d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + c.keysym.sym); + + // if user pressed return we exit + //if(c == 13) { + if (isKeyPressed(SDLK_RETURN, c) == true) { + program->exit(); + userWantsExit = true; + } + } + + void + Program::ShowMessageProgramState::mouseMove(int x, int y, + const MouseState & + mouseState) { + mouseX = x; + mouseY = y; + msgBox.mouseMove(x, y); + } + + void + Program::ShowMessageProgramState::update() { + mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; + } + + // ===================== PUBLIC ======================== + + bool Program::rendererInitOk = false; + bool Program::tryingRendererInit = false; + + Program::Program() :msgBox("Program", "msgBox") { + //this->masterserverMode = false; + this->window = NULL; + this->shutdownApplicationEnabled = false; + this->skipRenderFrameCount = 0; + this->messageBoxIsSystemError = false; + this->programStateOldSystemError = NULL; + this->programState = NULL; + this->singleton = this; + this->soundThreadManager = NULL; + + //mesage box + Lang & lang = Lang::getInstance(); + //msgBox.registerGraphicComponent("Program", "msgBox"); + msgBox.init(lang.getString("Ok")); + msgBox.setEnabled(false); + } + + //bool Program::isMasterserverMode() const { + // return this->masterserverMode; + //} + + void + Program::initNormal(WindowGl * window) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + Config & config = Config::getInstance(); + init(window); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + setState(new Intro(this)); + showCursor(config.getBool("No2DMouseRendering", "false")); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void + Program::initSavedGame(WindowGl * window, bool masterserverMode, + string saveGameFile) { + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + + if (saveGameFile == "") { + Config & config = Config::getInstance(); + saveGameFile = config.getString("LastSavedGame", ""); + if (saveGameFile == "") { + saveGameFile = GameConstants::saveGameFileDefault; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + saveGameFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + saveGameFile; + } else { + string + userData = + Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + saveGameFile = userData + saveGameFile; + } + } + } + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Loading game from [%s]\n",saveGameFile.c_str()); + printf("Loading saved game from [%s]\n", saveGameFile.c_str()); + + Game::loadGame(saveGameFile, this, masterserverMode); + } + + void + Program::initServer(WindowGl * window, bool autostart, + bool openNetworkSlots, bool masterserverMode) { + //this->masterserverMode = masterserverMode; + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + mainMenu->setState(new + MenuStateCustomGame(this, mainMenu, + openNetworkSlots, pNewGame, + autostart, NULL, + masterserverMode)); + } + + void + Program::initServer(WindowGl * window, GameSettings * settings) { + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + mainMenu->setState(new + MenuStateCustomGame(this, mainMenu, false, + pNewGame, true, settings)); + } + + void + Program::initClient(WindowGl * window, const Ip & serverIp, + int portNumber) { + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + mainMenu->setState(new + MenuStateJoinGame(this, mainMenu, true, serverIp, + portNumber)); + } + + void + Program::initClientAutoFindHost(WindowGl * window) { + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + bool autoFindHost = true; + mainMenu->setState(new + MenuStateJoinGame(this, mainMenu, &autoFindHost)); + + } + + void + Program::initScenario(WindowGl * window, string autoloadScenarioName) { + init(window); + MainMenu * + mainMenu = new MainMenu(this); + setState(mainMenu); + mainMenu->setState(new MenuStateScenario(this, mainMenu, false, + Config:: + getInstance + ().getPathListForType + (ptScenarios), + autoloadScenarioName)); + } + + Program::~Program() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + delete programState; + programState = NULL; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + Renderer::getInstance().end(); + CoreData & coreData = CoreData::getInstance(); + coreData.cleanup(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + //restore video mode + restoreDisplaySettings(); + singleton = NULL; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + if (soundThreadManager != NULL) { + BaseThread::shutdownAndWait(soundThreadManager); + delete soundThreadManager; + soundThreadManager = NULL; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + + void + Program::restoreStateFromSystemError() { + messageBoxIsSystemError = false; + if (this->programStateOldSystemError == NULL) { + setState(new Intro(this)); + } else { + setState(this->programStateOldSystemError); + } + } + + bool Program::textInput(std::string text) { + if (msgBox.getEnabled()) { + return false; + } + //delegate event + return programState->textInput(text); + } + + bool Program::sdlKeyDown(SDL_KeyboardEvent key) { + //delegate event + return programState->sdlKeyDown(key); + } + + void + Program::keyDown(SDL_KeyboardEvent key) { + if (msgBox.getEnabled()) { + //SDL_keysym keystate = Window::getKeystate(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //if(key == vkEscape || key == SDLK_ESCAPE || + // ((key == vkReturn || key == SDLK_RETURN || key == SDLK_KP_ENTER) && !(keystate.mod & (KMOD_LALT | KMOD_RALT)))) { + if (isKeyPressed(SDLK_ESCAPE, key) == true + || ((isKeyPressed(SDLK_RETURN, key) == true) + && !(key.keysym.mod & (KMOD_LALT | KMOD_RALT)))) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("---> keystate [%d]\n",keystate); + msgBox.setEnabled(false); + if (messageBoxIsSystemError == true) { + restoreStateFromSystemError(); + } + } + } + //delegate event + programState->keyDown(key); + } + + void + Program::keyUp(SDL_KeyboardEvent key) { + programState->keyUp(key); + } + + void + Program::keyPress(SDL_KeyboardEvent c) { + programState->keyPress(c); + } + + void + Program::mouseDownLeft(int x, int y) { + if (msgBox.getEnabled()) { + int + button = 0; + if (msgBox.mouseClick(x, y, button)) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //close message box + msgBox.setEnabled(false); + if (messageBoxIsSystemError == true) { + restoreStateFromSystemError(); + } + } + } + } + + void + Program::eventMouseMove(int x, int y, const MouseState * ms) { + if (msgBox.getEnabled()) { + msgBox.mouseMove(x, y); + } + } + + void + Program::simpleTask(BaseThread * callingThread, void *userdata) { + loopWorker(); + } + + void + Program::loop() { + loopWorker(); + } + + void + Program::loopWorker() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] ================================= MAIN LOOP START ================================= \n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //Renderer &renderer= Renderer::getInstance(); + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && window) { + MainWindow * + mainWindow = dynamic_cast (window); + if (mainWindow) { + //mainWindow->render(); + if (mainWindow->getTriggerLanguageToggle()) { + mainWindow->toggleLanguage(mainWindow->getTriggerLanguage()); + } + } + } + + Chrono chronoPerformanceCounts; + + bool + showPerfStats = + Config::getInstance().getBool("ShowPerfStats", "false"); + Chrono chronoPerf; + char + perfBuf[8096] = ""; + std::vector < string > perfList; + if (showPerfStats) + chronoPerf.start(); + + Chrono chronoLoop; #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chronoLoop.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chronoLoop.start(); #endif - Chrono chrono; + Chrono chrono; #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); #endif -//render - assert (programState != NULL); + //render + assert(programState != NULL); - if (this->programState->quitTriggered () == true) - { + if (this->programState->quitTriggered() == true) { #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); #endif - Stats endStats = this->programState->quitAndToggleState (); + Stats endStats = this->programState->quitAndToggleState(); #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); #endif - Game::exitGameState (this, endStats); + Game::exitGameState(this, endStats); #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); #endif - return; - } - ProgramState * - prevState = this->programState; + return; + } + ProgramState * + prevState = this->programState; - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } - assert (programState != NULL); + assert(programState != NULL); - chronoPerformanceCounts.start (); + chronoPerformanceCounts.start(); - programState->render (); + programState->render(); - programState->addPerformanceCount (ProgramState:: - MAIN_PROGRAM_RENDER_KEY, - chronoPerformanceCounts. - getMillis ()); + programState->addPerformanceCount(ProgramState:: + MAIN_PROGRAM_RENDER_KEY, + chronoPerformanceCounts. + getMillis()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP RENDERING\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP RENDERING\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); -//update camera - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); + //update camera + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); #endif - chronoPerformanceCounts.start (); + chronoPerformanceCounts.start(); - while (updateCameraTimer.isTime ()) - { - programState->updateCamera (); - } + while (updateCameraTimer.isTime()) { + programState->updateCamera(); + } - programState->addPerformanceCount ("programState->updateCamera()", - chronoPerformanceCounts.getMillis - ()); + programState->addPerformanceCount("programState->updateCamera()", + chronoPerformanceCounts.getMillis + ()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP CAMERA UPDATING\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP CAMERA UPDATING\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); -//update world - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); + //update world + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); #endif - int - updateCount = 0; - while (prevState == this->programState && updateTimer.isTime ()) - { - Chrono chronoUpdateLoop; + int + updateCount = 0; + while (prevState == this->programState && updateTimer.isTime()) { + Chrono chronoUpdateLoop; #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chronoUpdateLoop.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chronoUpdateLoop.start(); #endif - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - " updateCount: %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis (), - updateCount); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + " updateCount: %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis(), + updateCount); + perfList.push_back(perfBuf); + } - GraphicComponent::update (); - programState->update (); + GraphicComponent::update(); + programState->update(); #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] programState->update took msecs: %lld, updateCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chronoUpdateLoop.getMillis (), - updateCount); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - chronoUpdateLoop.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] programState->update took msecs: %lld, updateCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chronoUpdateLoop.getMillis(), + updateCount); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + chronoUpdateLoop.start(); #endif - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - " updateCount: %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis (), - updateCount); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + " updateCount: %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis(), + updateCount); + perfList.push_back(perfBuf); + } - if (prevState == this->programState) - { - chronoPerformanceCounts.start (); + if (prevState == this->programState) { + chronoPerformanceCounts.start(); - if (soundThreadManager == NULL - || soundThreadManager->isThreadExecutionLagging ()) - { - if (soundThreadManager != NULL) - { + if (soundThreadManager == NULL + || soundThreadManager->isThreadExecutionLagging()) { + if (soundThreadManager != NULL) { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - SoundRenderer::getInstance ().update (); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s %d] ERROR / WARNING soundThreadManager->isThreadExecutionLagging is TRUE\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + SoundRenderer::getInstance().update(); #ifdef DEBUG - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] SoundRenderer::getInstance().update() took msecs: %lld, updateCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - chronoUpdateLoop.getMillis (), - updateCount); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - chronoUpdateLoop.start (); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] SoundRenderer::getInstance().update() took msecs: %lld, updateCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + chronoUpdateLoop.getMillis(), + updateCount); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + chronoUpdateLoop.start(); #endif - } + } - programState->addPerformanceCount - ("SoundRenderer::getInstance().update()", - chronoPerformanceCounts.getMillis ()); + programState->addPerformanceCount + ("SoundRenderer::getInstance().update()", + chronoPerformanceCounts.getMillis()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - " updateCount: %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis (), - updateCount); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + " updateCount: %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis(), + updateCount); + perfList.push_back(perfBuf); + } - chronoPerformanceCounts.start (); + chronoPerformanceCounts.start(); - NetworkManager::getInstance ().update (); + NetworkManager::getInstance().update(); - programState->addPerformanceCount - ("NetworkManager::getInstance().update()", - chronoPerformanceCounts.getMillis ()); + programState->addPerformanceCount + ("NetworkManager::getInstance().update()", + chronoPerformanceCounts.getMillis()); #ifdef DEBUG - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] NetworkManager::getInstance().update() took msecs: %lld, updateCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chronoUpdateLoop.getMillis (), - updateCount); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chronoUpdateLoop.getMillis () > 0) - chronoUpdateLoop.start (); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] NetworkManager::getInstance().update() took msecs: %lld, updateCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chronoUpdateLoop.getMillis(), + updateCount); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chronoUpdateLoop.getMillis() > 0) + chronoUpdateLoop.start(); #endif - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER - " updateCount: %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis (), - updateCount); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER + " updateCount: %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis(), + updateCount); + perfList.push_back(perfBuf); + } - } - updateCount++; - } + } + updateCount++; + } #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d AFTER programState->update took msecs: %lld ==============> MAIN LOOP BODY LOGIC, updateCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis (), updateCount); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d AFTER programState->update took msecs: %lld ==============> MAIN LOOP BODY LOGIC, updateCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis(), updateCount); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); #endif - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } - if (prevState == this->programState) - { -//fps timer - chronoPerformanceCounts.start (); + if (prevState == this->programState) { + //fps timer + chronoPerformanceCounts.start(); - chrono.start (); - while (fpsTimer.isTime ()) - { - programState->tick (); - } + chrono.start(); + while (fpsTimer.isTime()) { + programState->tick(); + } - programState->addPerformanceCount ("programState->tick()", - chronoPerformanceCounts.getMillis - ()); + programState->addPerformanceCount("programState->tick()", + chronoPerformanceCounts.getMillis + ()); - if (showPerfStats) - { - sprintf (perfBuf, - "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, chronoPerf.getMillis ()); - perfList.push_back (perfBuf); - } + if (showPerfStats) { + sprintf(perfBuf, + "In [%s::%s] Line: %d took msecs: " MG_I64_SPECIFIER "\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, chronoPerf.getMillis()); + perfList.push_back(perfBuf); + } #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); -//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d programState->render took msecs: %lld ==============> MAIN LOOP TICKING\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); #endif - } + } - if (showPerfStats && chronoPerf.getMillis () >= 100) - { - for (unsigned int x = 0; x < perfList.size (); ++x) - { - printf ("%s", perfList[x].c_str ()); - } - } + if (showPerfStats && chronoPerf.getMillis() >= 100) { + for (unsigned int x = 0; x < perfList.size(); ++x) { + printf("%s", perfList[x].c_str()); + } + } #ifdef DEBUG - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] ------------------------------- MAIN LOOP END, stats: loop took msecs: %lld -------------------------------\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chronoLoop.getMillis ()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] ------------------------------- MAIN LOOP END, stats: loop took msecs: %lld -------------------------------\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chronoLoop.getMillis()); #endif - } - - void - Program::resize (SizeState sizeState) - { - - switch (sizeState) - { - case ssMinimized: -//restoreVideoMode(); - break; - case ssMaximized: - case ssRestored: -//setDisplaySettings(); -//renderer.reloadResources(); - break; - } - } - -// ==================== misc ==================== - - void - Program::renderProgramMsgBox () - { - Renderer & renderer = Renderer::getInstance (); - if (msgBox.getEnabled ()) - { - renderer.renderMessageBox (&msgBox); - } - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - && window) - { - MainWindow * - mainWindow = dynamic_cast < MainWindow * >(window); - if (mainWindow) - { - mainWindow->render (); - } - } - - } - - void - Program::setState (ProgramState * programStateNew, bool cleanupOldState) - { - try - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->programStateOldSystemError = this->programState; - bool msgBoxEnabled = msgBox.getEnabled (); - - if (dynamic_cast < Game * >(programStateNew) != NULL) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - int - X = 0; - int - Y = 0; - SDL_GetMouseState (&X, &Y); - programStateNew->setStartXY (X, Y); - Logger::getInstance ().setProgress (0); - Logger::getInstance ().setState (""); - - - SDL_PumpEvents (); - - showCursor (true); - SDL_PumpEvents (); - sleep (0); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - if (cleanupOldState == true) - { - if (this->programState != programStateNew) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->programStateOldSystemError = NULL; - delete this->programState; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->programState = NULL; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//mesage box - Lang & lang = Lang::getInstance (); - msgBox.init (lang.getString ("Ok")); - msgBox.setEnabled (msgBoxEnabled); - - fpsTimer.init (1, maxTimes); - updateTimer.init (GameConstants::updateFps, maxTimes); - updateCameraTimer.init (GameConstants::cameraFps, maxTimes); - - this->programState = programStateNew; - assert (programStateNew != NULL); - if (programStateNew == NULL) - { - throw megaglest_runtime_error ("programStateNew == NULL"); - } - programStateNew->load (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - programStateNew->init (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - updateTimer.reset (); - updateCameraTimer.reset (); - fpsTimer.reset (); - - Config & config = Config::getInstance (); - if (dynamic_cast < Intro * >(programStateNew) != NULL - && msgBoxEnabled == true) - { - showCursor (true); - } - else - { - showCursor (config.getBool ("No2DMouseRendering", "false")); - } - - this->programStateOldSystemError = NULL; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - catch (megaglest_runtime_error & e) - { -//printf("3333333 ex.wantStackTrace() = %d\n",e.wantStackTrace()); - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, e.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); -//abort(); -//printf("44444444a ex.wantStackTrace() = %d\n",e.wantStackTrace()); - - messageBoxIsSystemError = true; - string errorMsg = e.what (); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - if (dynamic_cast < Game * >(programStateNew) != NULL) - { - try - { - Game * - game = dynamic_cast < Game * >(programStateNew); - Renderer & renderer = Renderer::getInstance (); - game->setQuitPendingIndicator (); // by this the world is no more updated - renderer.initGame (game, game->getGameCameraPtr ()); - } - catch (megaglest_runtime_error & ex2) - { - errorMsg += "\n" + string (ex2.what ()); - } - } - } - -//printf("44444444b ex.wantStackTrace() = %d\n",e.wantStackTrace()); - this->showMessage (errorMsg.c_str ()); -//setState(new Intro(this)); - } - catch (const exception & e) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, e.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, szBuf); -//abort(); - - messageBoxIsSystemError = true; - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - if (dynamic_cast < Game * >(programStateNew) != NULL) - { - Game * - game = dynamic_cast < Game * >(programStateNew); - Renderer & renderer = Renderer::getInstance (); - renderer.initGame (game, game->getGameCameraPtr ()); - } - } - - this->showMessage (e.what ()); -//setState(new Intro(this)); - } - } - - void - Program::exit () - { - window->destroy (); - } - -// ==================== PRIVATE ==================== - - void - Program::initResolution () - { - const - Metrics & - metrics = Metrics::getInstance (); - if (window->getScreenWidth () != metrics.getScreenW () || - window->getScreenHeight () != metrics.getScreenH ()) - { - - int - oldW = metrics.getScreenW (); - int - oldH = metrics.getScreenH (); - - Config & config = Config::getInstance (); - config.setInt ("ScreenWidth", window->getScreenWidth (), true); - config.setInt ("ScreenHeight", window->getScreenHeight (), true); - - metrics.reload (window->getScreenWidth (), - window->getScreenHeight ()); - printf - ("MainWindow forced change of resolution to desktop values (%d x %d) instead of (%d x %d)\n", - metrics.getScreenW (), metrics.getScreenH (), oldW, oldH); - } - } - - void - Program::init (WindowGl * window, bool initSound, bool toggleFullScreen) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->window = window; - Config & config = Config::getInstance (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//set video mode - if (toggleFullScreen == false) - { - setDisplaySettings (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//window -//window->setText("ZetaGlest"); - window->setStyle (config.getBool ("Windowed") ? wsWindowedFixed : - wsFullscreen); - window->setPos (0, 0); - window->setSize (config.getInt ("ScreenWidth"), - config.getInt ("ScreenHeight")); - window->create (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//timers - fpsTimer.init (1, maxTimes); - updateTimer.init (GameConstants::updateFps, maxTimes); - updateCameraTimer.init (GameConstants::cameraFps, maxTimes); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//log start - Logger & logger = Logger::getInstance (); - string logFile = "glest.log"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - logFile = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - logFile; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - - logFile = userData + logFile; - } - logger.setFile (logFile); - logger.clear (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//lang -//Lang &lang= Lang::getInstance(); - Lang::getInstance (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//render - Renderer & renderer = Renderer::getInstance (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - window->initGl (config.getInt ("ColorBits"), - config.getInt ("DepthBits"), - config.getInt ("StencilBits"), - config.getBool ("HardwareAcceleration", "false"), - config.getBool ("FullScreenAntiAliasing", "false"), - config.getFloat ("GammaValue", "0.0")); - window->setText (config.getString ("WindowTitle", "ZetaGlest")); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - window->makeCurrentGl (); - initResolution (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//coreData, needs renderer, but must load before renderer init - CoreData & coreData = CoreData::getInstance (); - coreData.load (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//init renderer (load global textures) - tryingRendererInit = true; - renderer.init (); - tryingRendererInit = false; - rendererInitOk = true; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//sound - if (initSound == true && toggleFullScreen == false) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - bool initOk = soundRenderer.init (window); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] initOk = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, initOk); - -// Test sound system failed -//initOk = false; -// END - - if (initOk == false) - { - string sError = "Sound System could not be initialized!"; - this->showMessage (sError.c_str ()); - } - -// Run sound streaming in a background thread if enabled - if (SoundRenderer::getInstance ().runningThreaded () == true) - { - if (BaseThread::shutdownAndWait (soundThreadManager) == true) - { - delete soundThreadManager; - } - static - string - mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - soundThreadManager = - new SimpleTaskThread (&SoundRenderer::getInstance (), 0, - SOUND_THREAD_UPDATE_MILLISECONDS); - soundThreadManager->setUniqueID (mutexOwnerId); - soundThreadManager->start (); - } - } - - NetworkInterface::setAllowGameDataSynchCheck (Config:: - getInstance ().getBool - ("AllowGameDataSynchCheck", - "false")); - NetworkInterface::setAllowDownloadDataSynch (Config:: - getInstance ().getBool - ("AllowDownloadDataSynch", - "false")); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void - Program::reloadUI () - { - if (programState) - { - programState->reloadUI (); - } - } - - void - Program::setDisplaySettings () - { - - Config & config = Config::getInstance (); - - if (!config.getBool ("Windowed")) - { - -//int freq= config.getInt("RefreshFrequency"); - int - colorBits = config.getInt ("ColorBits"); - int - screenWidth = config.getInt ("ScreenWidth"); - int - screenHeight = config.getInt ("ScreenHeight"); - - if (config.getBool ("AutoMaxFullScreen", "false") == true) - { - getFullscreenVideoInfo (colorBits, screenWidth, screenHeight, - !config.getBool ("Windowed")); - config.setInt ("ColorBits", colorBits); - config.setInt ("ScreenWidth", screenWidth); - config.setInt ("ScreenHeight", screenHeight); - } - } - changeVideoModeFullScreen (!config.getBool ("Windowed")); - } - - void - Program::restoreDisplaySettings () - { - Config & config = Config::getInstance (); - - if (!config.getBool ("Windowed")) - { - restoreVideoMode (this->getWindow ()->getSDLWindow ()); - } - } - - bool Program::isMessageShowing () - { - return msgBox.getEnabled (); - } - - void - Program::showMessage (const char *msg) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d] msg [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - msg); - - msgBox.setText (msg); - msgBox.setEnabled (true); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - printf ("Message:\n%s\n", msg); - - if (messageBoxIsSystemError == true) - { - messageBoxIsSystemError = false; -//setState(new Intro(this)); - initServer (window, false, true, true); - } - } - } - - void - Program::stopSoundSystem () - { - if (soundThreadManager != NULL) - { - BaseThread::shutdownAndWait (soundThreadManager); - delete soundThreadManager; - soundThreadManager = NULL; - } - } - - void - Program::startSoundSystem () - { - stopSoundSystem (); - if (SoundRenderer::getInstance ().runningThreaded () == true) - { - static - string - mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - soundThreadManager = - new SimpleTaskThread (&SoundRenderer::getInstance (), 0, - SOUND_THREAD_UPDATE_MILLISECONDS); - soundThreadManager->setUniqueID (mutexOwnerId); - soundThreadManager->start (); - } - } - -//void Program::resetSoundSystem() { -// startSoundSystem(); -//} - -//void Program::reInitGl() { -// if(window != NULL) { -// Config &config= Config::getInstance(); -// window->initGl(config.getInt("ColorBits"), -// config.getInt("DepthBits"), -// config.getInt("StencilBits"), -// config.getBool("HardwareAcceleration","false"), -// config.getBool("FullScreenAntiAliasing","false"), -// config.getFloat("GammaValue","0.0")); -// window->setText(config.getString("WindowTitle","ZetaGlest")); -// initResolution(); -// } -//} - - void - Program::consoleAddLine (string line) - { - if (programState != NULL) - { - programState->consoleAddLine (line); - } - } - - SimpleTaskThread * - Program::getSoundThreadManager (bool takeOwnership) - { - SimpleTaskThread * - result = soundThreadManager; - if (takeOwnership) - { - soundThreadManager = NULL; - } - return result; - } - - } + } + + void + Program::resize(SizeState sizeState) { + + switch (sizeState) { + case ssMinimized: + //restoreVideoMode(); + break; + case ssMaximized: + case ssRestored: + //setDisplaySettings(); + //renderer.reloadResources(); + break; + } + } + + // ==================== misc ==================== + + void + Program::renderProgramMsgBox() { + Renderer & renderer = Renderer::getInstance(); + if (msgBox.getEnabled()) { + renderer.renderMessageBox(&msgBox); + } + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && window) { + MainWindow * + mainWindow = dynamic_cast (window); + if (mainWindow) { + mainWindow->render(); + } + } + + } + + void + Program::setState(ProgramState * programStateNew, bool cleanupOldState) { + try { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->programStateOldSystemError = this->programState; + bool msgBoxEnabled = msgBox.getEnabled(); + + if (dynamic_cast (programStateNew) != NULL) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + int + X = 0; + int + Y = 0; + SDL_GetMouseState(&X, &Y); + programStateNew->setStartXY(X, Y); + Logger::getInstance().setProgress(0); + Logger::getInstance().setState(""); + + + SDL_PumpEvents(); + + showCursor(true); + SDL_PumpEvents(); + sleep(0); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + if (cleanupOldState == true) { + if (this->programState != programStateNew) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->programStateOldSystemError = NULL; + delete this->programState; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->programState = NULL; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //mesage box + Lang & lang = Lang::getInstance(); + msgBox.init(lang.getString("Ok")); + msgBox.setEnabled(msgBoxEnabled); + + fpsTimer.init(1, maxTimes); + updateTimer.init(GameConstants::updateFps, maxTimes); + updateCameraTimer.init(GameConstants::cameraFps, maxTimes); + + this->programState = programStateNew; + assert(programStateNew != NULL); + if (programStateNew == NULL) { + throw megaglest_runtime_error("programStateNew == NULL"); + } + programStateNew->load(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + programStateNew->init(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + updateTimer.reset(); + updateCameraTimer.reset(); + fpsTimer.reset(); + + Config & config = Config::getInstance(); + if (dynamic_cast (programStateNew) != NULL + && msgBoxEnabled == true) { + showCursor(true); + } else { + showCursor(config.getBool("No2DMouseRendering", "false")); + } + + this->programStateOldSystemError = NULL; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } catch (megaglest_runtime_error & e) { + //printf("3333333 ex.wantStackTrace() = %d\n",e.wantStackTrace()); + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + //abort(); + //printf("44444444a ex.wantStackTrace() = %d\n",e.wantStackTrace()); + + messageBoxIsSystemError = true; + string errorMsg = e.what(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + if (dynamic_cast (programStateNew) != NULL) { + try { + Game * + game = dynamic_cast (programStateNew); + Renderer & renderer = Renderer::getInstance(); + game->setQuitPendingIndicator(); // by this the world is no more updated + renderer.initGame(game, game->getGameCameraPtr()); + } catch (megaglest_runtime_error & ex2) { + errorMsg += "\n" + string(ex2.what()); + } + } + } + + //printf("44444444b ex.wantStackTrace() = %d\n",e.wantStackTrace()); + this->showMessage(errorMsg.c_str()); + //setState(new Intro(this)); + } catch (const exception & e) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf); + //abort(); + + messageBoxIsSystemError = true; + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + if (dynamic_cast (programStateNew) != NULL) { + Game * + game = dynamic_cast (programStateNew); + Renderer & renderer = Renderer::getInstance(); + renderer.initGame(game, game->getGameCameraPtr()); + } + } + + this->showMessage(e.what()); + //setState(new Intro(this)); + } + } + + void + Program::exit() { + window->destroy(); + } + + // ==================== PRIVATE ==================== + + void + Program::initResolution() { + const + Metrics & + metrics = Metrics::getInstance(); + if (window->getScreenWidth() != metrics.getScreenW() || + window->getScreenHeight() != metrics.getScreenH()) { + + int + oldW = metrics.getScreenW(); + int + oldH = metrics.getScreenH(); + + Config & config = Config::getInstance(); + config.setInt("ScreenWidth", window->getScreenWidth(), true); + config.setInt("ScreenHeight", window->getScreenHeight(), true); + + metrics.reload(window->getScreenWidth(), + window->getScreenHeight()); + printf + ("MainWindow forced change of resolution to desktop values (%d x %d) instead of (%d x %d)\n", + metrics.getScreenW(), metrics.getScreenH(), oldW, oldH); + } + } + + void + Program::init(WindowGl * window, bool initSound, bool toggleFullScreen) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->window = window; + Config & config = Config::getInstance(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //set video mode + if (toggleFullScreen == false) { + setDisplaySettings(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //window + //window->setText("ZetaGlest"); + window->setStyle(config.getBool("Windowed") ? wsWindowedFixed : + wsFullscreen); + window->setPos(0, 0); + window->setSize(config.getInt("ScreenWidth"), + config.getInt("ScreenHeight")); + window->create(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //timers + fpsTimer.init(1, maxTimes); + updateTimer.init(GameConstants::updateFps, maxTimes); + updateCameraTimer.init(GameConstants::cameraFps, maxTimes); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //log start + Logger & logger = Logger::getInstance(); + string logFile = "glest.log"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + logFile = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + logFile; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + + logFile = userData + logFile; + } + logger.setFile(logFile); + logger.clear(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //lang + //Lang &lang= Lang::getInstance(); + Lang::getInstance(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //render + Renderer & renderer = Renderer::getInstance(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + window->initGl(config.getInt("ColorBits"), + config.getInt("DepthBits"), + config.getInt("StencilBits"), + config.getBool("HardwareAcceleration", "false"), + config.getBool("FullScreenAntiAliasing", "false"), + config.getFloat("GammaValue", "0.0")); + window->setText(config.getString("WindowTitle", "ZetaGlest")); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + window->makeCurrentGl(); + initResolution(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //coreData, needs renderer, but must load before renderer init + CoreData & coreData = CoreData::getInstance(); + coreData.load(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //init renderer (load global textures) + tryingRendererInit = true; + renderer.init(); + tryingRendererInit = false; + rendererInitOk = true; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //sound + if (initSound == true && toggleFullScreen == false) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + bool initOk = soundRenderer.init(window); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] initOk = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, initOk); + + // Test sound system failed + //initOk = false; + // END + + if (initOk == false) { + string sError = "Sound System could not be initialized!"; + this->showMessage(sError.c_str()); + } + + // Run sound streaming in a background thread if enabled + if (SoundRenderer::getInstance().runningThreaded() == true) { + if (BaseThread::shutdownAndWait(soundThreadManager) == true) { + delete soundThreadManager; + } + static + string + mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + soundThreadManager = + new SimpleTaskThread(&SoundRenderer::getInstance(), 0, + SOUND_THREAD_UPDATE_MILLISECONDS); + soundThreadManager->setUniqueID(mutexOwnerId); + soundThreadManager->start(); + } + } + + NetworkInterface::setAllowGameDataSynchCheck(Config:: + getInstance().getBool + ("AllowGameDataSynchCheck", + "false")); + NetworkInterface::setAllowDownloadDataSynch(Config:: + getInstance().getBool + ("AllowDownloadDataSynch", + "false")); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void + Program::reloadUI() { + if (programState) { + programState->reloadUI(); + } + } + + void + Program::setDisplaySettings() { + + Config & config = Config::getInstance(); + + if (!config.getBool("Windowed")) { + + //int freq= config.getInt("RefreshFrequency"); + int + colorBits = config.getInt("ColorBits"); + int + screenWidth = config.getInt("ScreenWidth"); + int + screenHeight = config.getInt("ScreenHeight"); + + if (config.getBool("AutoMaxFullScreen", "false") == true) { + getFullscreenVideoInfo(colorBits, screenWidth, screenHeight, + !config.getBool("Windowed")); + config.setInt("ColorBits", colorBits); + config.setInt("ScreenWidth", screenWidth); + config.setInt("ScreenHeight", screenHeight); + } + } + changeVideoModeFullScreen(!config.getBool("Windowed")); + } + + void + Program::restoreDisplaySettings() { + Config & config = Config::getInstance(); + + if (!config.getBool("Windowed")) { + restoreVideoMode(this->getWindow()->getSDLWindow()); + } + } + + bool Program::isMessageShowing() { + return msgBox.getEnabled(); + } + + void + Program::showMessage(const char *msg) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d] msg [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + msg); + + msgBox.setText(msg); + msgBox.setEnabled(true); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + printf("Message:\n%s\n", msg); + + if (messageBoxIsSystemError == true) { + messageBoxIsSystemError = false; + //setState(new Intro(this)); + initServer(window, false, true, true); + } + } + } + + void + Program::stopSoundSystem() { + if (soundThreadManager != NULL) { + BaseThread::shutdownAndWait(soundThreadManager); + delete soundThreadManager; + soundThreadManager = NULL; + } + } + + void + Program::startSoundSystem() { + stopSoundSystem(); + if (SoundRenderer::getInstance().runningThreaded() == true) { + static + string + mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + soundThreadManager = + new SimpleTaskThread(&SoundRenderer::getInstance(), 0, + SOUND_THREAD_UPDATE_MILLISECONDS); + soundThreadManager->setUniqueID(mutexOwnerId); + soundThreadManager->start(); + } + } + + //void Program::resetSoundSystem() { + // startSoundSystem(); + //} + + //void Program::reInitGl() { + // if(window != NULL) { + // Config &config= Config::getInstance(); + // window->initGl(config.getInt("ColorBits"), + // config.getInt("DepthBits"), + // config.getInt("StencilBits"), + // config.getBool("HardwareAcceleration","false"), + // config.getBool("FullScreenAntiAliasing","false"), + // config.getFloat("GammaValue","0.0")); + // window->setText(config.getString("WindowTitle","ZetaGlest")); + // initResolution(); + // } + //} + + void + Program::consoleAddLine(string line) { + if (programState != NULL) { + programState->consoleAddLine(line); + } + } + + SimpleTaskThread * + Program::getSoundThreadManager(bool takeOwnership) { + SimpleTaskThread * + result = soundThreadManager; + if (takeOwnership) { + soundThreadManager = NULL; + } + return result; + } + + } } //end namespace diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index 52c4ab96e..409a72974 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -50,461 +50,412 @@ using Shared::Platform::MouseState; using Shared::PlatformCommon::PerformanceTimer; using Shared::Platform::Ip; using - namespace - Shared::PlatformCommon; +namespace + Shared::PlatformCommon; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class Program; - class MainWindow; - class GameSettings; + class Program; + class MainWindow; + class GameSettings; -// ===================================================== -// class ProgramState -// -/// Base class for all program states: -/// Intro, MainMenu, Game, BattleEnd (State Design pattern) -// ===================================================== + // ===================================================== + // class ProgramState + // + /// Base class for all program states: + /// Intro, MainMenu, Game, BattleEnd (State Design pattern) + // ===================================================== - class ProgramState - { - protected: - Program * program; + class ProgramState { + protected: + Program * program; - int - mouseX; - int - mouseY; - int - mouse2dAnim; + int + mouseX; + int + mouseY; + int + mouse2dAnim; - int - startX; - int - startY; - bool forceMouseRender; + int + startX; + int + startY; + bool forceMouseRender; - int - fps; - int - lastFps; + int + fps; + int + lastFps; - public: + public: - static const char * - MAIN_PROGRAM_RENDER_KEY; + static const char * + MAIN_PROGRAM_RENDER_KEY; - explicit ProgramState (Program * program); - virtual ~ ProgramState () - { - }; + explicit ProgramState(Program * program); + virtual ~ProgramState() { + }; - int - getFps () const - { - return - fps; - } - int - getLastFps () const - { - return - lastFps; - } + int + getFps() const { + return + fps; + } + int + getLastFps() const { + return + lastFps; + } -//virtual void render()=0; - virtual - bool - canRender (bool sleepIfCannotRender = true); - virtual void - render (); - virtual void - update (); - virtual void - updateCamera () - { - }; - virtual void - tick (); - virtual void - init () - { - }; - virtual void - load () - { - }; - virtual void - end () - { - }; - virtual void - mouseDownLeft (int x, int y) - { - }; - virtual void - mouseUpLeft (int x, int y) - { - }; - virtual void - mouseUpRight (int x, int y) - { - } - virtual void - mouseUpCenter (int x, int y) - { - } - virtual void - mouseDownRight (int x, int y) - { - }; - virtual void - mouseDownCenter (int x, int y) - { - } - virtual void - mouseDoubleClickLeft (int x, int y) - { - }; - virtual void - mouseDoubleClickRight (int x, int y) - { - } - virtual void - mouseDoubleClickCenter (int x, int y) - { - } - virtual void - eventMouseWheel (int x, int y, int zDelta) - { - } - virtual void - mouseMove (int x, int y, const MouseState * mouseState); - virtual - bool - textInput (std::string text) - { - return false; - }; - virtual void - keyDown (SDL_KeyboardEvent key) - { - }; - virtual - bool - sdlKeyDown (SDL_KeyboardEvent key) - { - return false; - }; - virtual void - keyUp (SDL_KeyboardEvent key) - { - }; - virtual void - keyPress (SDL_KeyboardEvent c) - { - }; - virtual void - setStartXY (int X, int Y) - { - startX = X; - startY = Y; - } - virtual void - restoreToStartXY (); - virtual - bool - isInSpecialKeyCaptureEvent () - { - return false; - } - virtual - bool - quitTriggered () - { - return false; - } - virtual - Stats - quitAndToggleState () - { - return Stats (); - }; - virtual Program * - getProgram () - { - return program; - } - virtual const Program * - getProgramConstPtr () - { - return program; - } - virtual void - setForceMouseRender (bool value) - { - forceMouseRender = value; - } - virtual void - consoleAddLine (string line) - { - }; + //virtual void render()=0; + virtual + bool + canRender(bool sleepIfCannotRender = true); + virtual void + render(); + virtual void + update(); + virtual void + updateCamera() { + }; + virtual void + tick(); + virtual void + init() { + }; + virtual void + load() { + }; + virtual void + end() { + }; + virtual void + mouseDownLeft(int x, int y) { + }; + virtual void + mouseUpLeft(int x, int y) { + }; + virtual void + mouseUpRight(int x, int y) { + } + virtual void + mouseUpCenter(int x, int y) { + } + virtual void + mouseDownRight(int x, int y) { + }; + virtual void + mouseDownCenter(int x, int y) { + } + virtual void + mouseDoubleClickLeft(int x, int y) { + }; + virtual void + mouseDoubleClickRight(int x, int y) { + } + virtual void + mouseDoubleClickCenter(int x, int y) { + } + virtual void + eventMouseWheel(int x, int y, int zDelta) { + } + virtual void + mouseMove(int x, int y, const MouseState * mouseState); + virtual + bool + textInput(std::string text) { + return false; + }; + virtual void + keyDown(SDL_KeyboardEvent key) { + }; + virtual + bool + sdlKeyDown(SDL_KeyboardEvent key) { + return false; + }; + virtual void + keyUp(SDL_KeyboardEvent key) { + }; + virtual void + keyPress(SDL_KeyboardEvent c) { + }; + virtual void + setStartXY(int X, int Y) { + startX = X; + startY = Y; + } + virtual void + restoreToStartXY(); + virtual + bool + isInSpecialKeyCaptureEvent() { + return false; + } + virtual + bool + quitTriggered() { + return false; + } + virtual + Stats + quitAndToggleState() { + return Stats(); + }; + virtual Program * + getProgram() { + return program; + } + virtual const Program * + getProgramConstPtr() { + return program; + } + virtual void + setForceMouseRender(bool value) { + forceMouseRender = value; + } + virtual void + consoleAddLine(string line) { + }; - virtual void - reloadUI () - { - }; - virtual void - addPerformanceCount (string key, int64 value) - { - }; + virtual void + reloadUI() { + }; + virtual void + addPerformanceCount(string key, int64 value) { + }; - protected: - virtual void - incrementFps (); - }; + protected: + virtual void + incrementFps(); + }; -// =============================== -// class Program -// =============================== + // =============================== + // class Program + // =============================== - class Program - { - private: - static const int - maxTimes; - SimpleTaskThread * - soundThreadManager; + class Program { + private: + static const int + maxTimes; + SimpleTaskThread * + soundThreadManager; - class ShowMessageProgramState: - public ProgramState - { - GraphicMessageBox msgBox; - int - mouseX; - int - mouseY; - int - mouse2dAnim; - string msg; - bool userWantsExit; + class ShowMessageProgramState : + public ProgramState { + GraphicMessageBox msgBox; + int + mouseX; + int + mouseY; + int + mouse2dAnim; + string msg; + bool userWantsExit; - public: - ShowMessageProgramState (Program * program, const char *msg); + public: + ShowMessageProgramState(Program * program, const char *msg); - virtual void - render (); - virtual void - mouseDownLeft (int x, int y); - virtual void - mouseMove (int x, int y, const MouseState & mouseState); - virtual void - keyPress (SDL_KeyboardEvent c); - virtual void - update (); - virtual - bool - wantExit () - { - return userWantsExit; - } - }; + virtual void + render(); + virtual void + mouseDownLeft(int x, int y); + virtual void + mouseMove(int x, int y, const MouseState & mouseState); + virtual void + keyPress(SDL_KeyboardEvent c); + virtual void + update(); + virtual + bool + wantExit() { + return userWantsExit; + } + }; - private: - ProgramState * programState; + private: + ProgramState * programState; - PerformanceTimer fpsTimer; - PerformanceTimer updateTimer; - PerformanceTimer updateCameraTimer; + PerformanceTimer fpsTimer; + PerformanceTimer updateTimer; + PerformanceTimer updateCameraTimer; - WindowGl * - window; - static Program * - singleton; + WindowGl * + window; + static Program * + singleton; - GraphicMessageBox msgBox; - int - skipRenderFrameCount; + GraphicMessageBox msgBox; + int + skipRenderFrameCount; - bool messageBoxIsSystemError; - ProgramState * - programStateOldSystemError; + bool messageBoxIsSystemError; + ProgramState * + programStateOldSystemError; -//bool masterserverMode; - bool shutdownApplicationEnabled; - static - bool - wantShutdownApplicationAfterGame; + //bool masterserverMode; + bool shutdownApplicationEnabled; + static + bool + wantShutdownApplicationAfterGame; - static - bool - tryingRendererInit; - static - bool - rendererInitOk; + static + bool + tryingRendererInit; + static + bool + rendererInitOk; - public: - Program (); - virtual ~ Program (); + public: + Program(); + virtual ~Program(); - static - bool - getTryingRendererInit () - { - return tryingRendererInit; - } - static - bool - getRendererInitOk () - { - return rendererInitOk; - } + static + bool + getTryingRendererInit() { + return tryingRendererInit; + } + static + bool + getRendererInitOk() { + return rendererInitOk; + } - static Program * - getInstance () - { - return singleton; - } + static Program * + getInstance() { + return singleton; + } - static void - setWantShutdownApplicationAfterGame (bool value) - { - wantShutdownApplicationAfterGame = value; - } - static - bool - getWantShutdownApplicationAfterGame () - { - return wantShutdownApplicationAfterGame; - } + static void + setWantShutdownApplicationAfterGame(bool value) { + wantShutdownApplicationAfterGame = value; + } + static + bool + getWantShutdownApplicationAfterGame() { + return wantShutdownApplicationAfterGame; + } -//bool isMasterserverMode() const; - bool isShutdownApplicationEnabled ()const - { - return - shutdownApplicationEnabled; - } - void - setShutdownApplicationEnabled (bool value) - { - shutdownApplicationEnabled = value; - } + //bool isMasterserverMode() const; + bool isShutdownApplicationEnabled()const { + return + shutdownApplicationEnabled; + } + void + setShutdownApplicationEnabled(bool value) { + shutdownApplicationEnabled = value; + } - GraphicMessageBox * - getMsgBox () - { - return &msgBox; - } - void - initNormal (WindowGl * window); - void - initServer (WindowGl * window, bool autostart = - false, bool openNetworkSlots = - false, bool masterserverMode = false); - void - initServer (WindowGl * window, GameSettings * settings); - void - initSavedGame (WindowGl * window, bool masterserverMode = - false, string saveGameFile = ""); - void - initClient (WindowGl * window, const Ip & serverIp, int portNumber = - -1); - void - initClientAutoFindHost (WindowGl * window); - void - initScenario (WindowGl * window, string autoloadScenarioName); + GraphicMessageBox * + getMsgBox() { + return &msgBox; + } + void + initNormal(WindowGl * window); + void + initServer(WindowGl * window, bool autostart = + false, bool openNetworkSlots = + false, bool masterserverMode = false); + void + initServer(WindowGl * window, GameSettings * settings); + void + initSavedGame(WindowGl * window, bool masterserverMode = + false, string saveGameFile = ""); + void + initClient(WindowGl * window, const Ip & serverIp, int portNumber = + -1); + void + initClientAutoFindHost(WindowGl * window); + void + initScenario(WindowGl * window, string autoloadScenarioName); -//main - bool textInput (std::string text); - void - keyDown (SDL_KeyboardEvent key); - bool sdlKeyDown (SDL_KeyboardEvent key); - void - keyUp (SDL_KeyboardEvent key); - void - keyPress (SDL_KeyboardEvent c); + //main + bool textInput(std::string text); + void + keyDown(SDL_KeyboardEvent key); + bool sdlKeyDown(SDL_KeyboardEvent key); + void + keyUp(SDL_KeyboardEvent key); + void + keyPress(SDL_KeyboardEvent c); - void - loop (); - void - loopWorker (); - void - resize (SizeState sizeState); - void - showMessage (const char *msg); - bool isMessageShowing (); + void + loop(); + void + loopWorker(); + void + resize(SizeState sizeState); + void + showMessage(const char *msg); + bool isMessageShowing(); -//misc - void - setState (ProgramState * programStateNew, bool cleanupOldState = true); - ProgramState * - getState () - { - return programState; - } - WindowGl * - getWindow () - { - return window; - } - const WindowGl * - getWindowConstPtr () const - { - return - window; - } - void - init (WindowGl * window, bool initSound = true, bool toggleFullScreen = - false); - void - exit (); + //misc + void + setState(ProgramState * programStateNew, bool cleanupOldState = true); + ProgramState * + getState() { + return programState; + } + WindowGl * + getWindow() { + return window; + } + const WindowGl * + getWindowConstPtr() const { + return + window; + } + void + init(WindowGl * window, bool initSound = true, bool toggleFullScreen = + false); + void + exit(); - virtual void - simpleTask (BaseThread * callingThread, void *userdata); + virtual void + simpleTask(BaseThread * callingThread, void *userdata); - void - mouseDownLeft (int x, int y); - void - eventMouseMove (int x, int y, const MouseState * ms); + void + mouseDownLeft(int x, int y); + void + eventMouseMove(int x, int y, const MouseState * ms); - void - renderProgramMsgBox (); - bool isInSpecialKeyCaptureEvent () - { - return programState->isInSpecialKeyCaptureEvent (); - } + void + renderProgramMsgBox(); + bool isInSpecialKeyCaptureEvent() { + return programState->isInSpecialKeyCaptureEvent(); + } -//void reInitGl(); -//void resetSoundSystem(); - void - stopSoundSystem (); - void - startSoundSystem (); + //void reInitGl(); + //void resetSoundSystem(); + void + stopSoundSystem(); + void + startSoundSystem(); - virtual void - consoleAddLine (string line); + virtual void + consoleAddLine(string line); - virtual SimpleTaskThread * - getSoundThreadManager (bool takeOwnership); - virtual void - reloadUI (); + virtual SimpleTaskThread * + getSoundThreadManager(bool takeOwnership); + virtual void + reloadUI(); - private: + private: - void - setDisplaySettings (); - void - restoreDisplaySettings (); - void - restoreStateFromSystemError (); - void - initResolution (); - }; + void + setDisplaySettings(); + void + restoreDisplaySettings(); + void + restoreStateFromSystemError(); + void + initResolution(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/main_menu.cpp b/source/glest_game/menu/main_menu.cpp index 4da5ad29c..955dfe46f 100644 --- a/source/glest_game/menu/main_menu.cpp +++ b/source/glest_game/menu/main_menu.cpp @@ -32,740 +32,651 @@ #include "leak_dumper.h" using namespace - Shared::Sound; +Shared::Sound; using namespace - Shared::Platform; +Shared::Platform; using namespace - Shared::Util; +Shared::Util; using namespace - Shared::Graphics; +Shared::Graphics; using namespace - Shared::Xml; +Shared::Xml; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class MainMenu -// ===================================================== - -// ===================== PUBLIC ======================== - MenuState * - MainMenu::oldstate = NULL; - - MainMenu::MainMenu (Program * program): - ProgramState (program), - menuBackgroundVideo (NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - //printf("In MainMenu::MainMenu()\n"); - - mouseX = 100; - mouseY = 100; - - state = NULL; - this-> - program = program; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - Config & - config = Config::getInstance (); - if (config.getString ("CustomMenuTextColor", "") != "") - { - string - customMenuTextColor = config.getString ("CustomMenuTextColor"); - Vec3f - customTextColor = Vec3f::strToVec3 (customMenuTextColor); - GraphicComponent::setCustomTextColor (customTextColor); - } - - setState (new MenuStateRoot (program, this)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - } - - void - MainMenu::reloadUI () - { - if (state) - { - state->reloadUI (); - } - } - - MainMenu::~MainMenu () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - //printf("In MainMenu::~MainMenu()\n"); - - if (menuBackgroundVideo != NULL) - { - menuBackgroundVideo->closePlayer (); - delete - menuBackgroundVideo; - menuBackgroundVideo = NULL; - } - delete - state; - state = NULL; - delete - oldstate; - oldstate = NULL; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - Renderer::getInstance ().endMenu (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - //soundRenderer.stopAllSounds(); - GraphicComponent::setCustomTextColor (Vec3f (1.0f, 1.0f, 1.0f)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - } - - void - MainMenu::init () - { - Renderer::getInstance ().initMenu (this); - - initBackgroundVideo (); - } - - void - MainMenu::initBackgroundVideo () - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == true - && CoreData::getInstance ().hasMainMenuVideoFilename () == true) - { - string - introVideoFile = - CoreData::getInstance ().getMainMenuVideoFilename (); - string - introVideoFileFallback = - CoreData::getInstance ().getMainMenuVideoFilenameFallback (); - - Context * - c = GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl * - glCtx = static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window * - window = glCtx->getScreenWindow (); - SDL_Surface * - screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); - //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); - menuBackgroundVideo = new VideoPlayer (&Renderer::getInstance (), - introVideoFile, - introVideoFileFallback, - window, - 0, 0, - screen->w, - screen->h, - screen->format->BitsPerPixel, - true, - vlcPluginsPath, - SystemFlags:: - VERBOSE_MODE_ENABLED); - menuBackgroundVideo->initPlayer (); - } - } - -//asynchronus render update - void - MainMenu::render () - { - Renderer & renderer = Renderer::getInstance (); - - canRender (); - incrementFps (); - - if (state->isMasterserverMode () == false) - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - - if (state->isVideoPlaying () == true) - { - if (menuBackgroundVideo != NULL) - { - if (menuBackgroundVideo->isPlaying () == true) - { - menuBackgroundVideo->closePlayer (); - delete - menuBackgroundVideo; - menuBackgroundVideo = NULL; - } - } - } - else if (menuBackgroundVideo == NULL) - { - initBackgroundVideo (); - } - renderer.clearBuffers (); - - //3d - renderer.reset3dMenu (); - renderer.clearZBuffer (); - - //printf("In [%s::%s Line: %d] menuBackgroundVideo [%p]\n",__FILE__,__FUNCTION__,__LINE__,menuBackgroundVideo); - - if (menuBackgroundVideo == NULL) - { - renderer.loadCameraMatrix (menuBackground.getCamera ()); - renderer.renderMenuBackground (&menuBackground); - renderer.renderParticleManager (rsMenu); - } - - //2d - renderer.reset2d (); - - if (menuBackgroundVideo != NULL) - { - if (menuBackgroundVideo->isPlaying () == true) - { - menuBackgroundVideo->playFrame (false); - } - else - { - menuBackgroundVideo->RestartVideo (); - } - } - } - state->render (); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false) - { - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - renderer.renderFPSWhenEnabled (lastFps); - renderer.swapBuffers (); - } - } - } - -//syncronus update - void - MainMenu::update () - { - if (menuBackgroundVideo == NULL) - { - Renderer::getInstance ().updateParticleManager (rsMenu); - } - mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; - if (menuBackgroundVideo == NULL) - { - menuBackground.update (); - } - state->update (); - } - -//event magangement: mouse click - void - MainMenu::mouseMove (int x, int y, const MouseState * ms) - { - mouseX = x; - mouseY = y; - state->mouseMove (x, y, ms); - } - -//returns if exiting - void - MainMenu::mouseDownLeft (int x, int y) - { - if (GraphicComponent::getFade () < 0.2f) - return; - state->mouseClick (x, y, mbLeft); - } - - void - MainMenu::mouseDownRight (int x, int y) - { - if (GraphicComponent::getFade () < 0.2f) - return; - state->mouseClick (x, y, mbRight); - } - - void - MainMenu::mouseUpLeft (int x, int y) - { - if (GraphicComponent::getFade () < 0.2f) - return; - state->mouseUp (x, y, mbLeft); - } - - bool - MainMenu::textInput (std::string text) - { - return state->textInput (text); - } - void - MainMenu::keyDown (SDL_KeyboardEvent key) - { - state->keyDown (key); - } - - void - MainMenu::keyUp (SDL_KeyboardEvent key) - { - state->keyUp (key); - } - - void - MainMenu::keyPress (SDL_KeyboardEvent c) - { - state->keyPress (c); - } - - void - MainMenu::setState (MenuState * newstate) - { - //printf("In [%s::%s Line: %d] oldstate [%p] newstate [%p] this->state [%p]\n",__FILE__,__FUNCTION__,__LINE__,oldstate,newstate,this->state); - - //printf("In MainMenu::setState() #1\n"); - - if (oldstate != NULL && oldstate != newstate) - { - MenuState * - oldstatePtr = oldstate; - delete - oldstate; - - //printf("In MainMenu::setState() #2\n"); - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if (oldstatePtr != this->state) - { - oldstate = this->state; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("In MainMenu::setState() #3\n"); - } - else - { - oldstate = NULL; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("In MainMenu::setState() #4\n"); - } - } - else - { - oldstate = this->state; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("In MainMenu::setState() #5\n"); - } - - if (this->state != NULL) - { - - } - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - this->state = newstate; - - GraphicComponent::resetFade (); - if (newstate) - { - menuBackground.setTargetCamera (newstate->getCamera ()); - } - } - - bool - MainMenu::isInSpecialKeyCaptureEvent () - { - return state->isInSpecialKeyCaptureEvent (); - } - - void - MainMenu::consoleAddLine (string line) - { - if (state != NULL) - { - state->consoleAddLine (line); - } - } - -// ===================================================== -// class MenuState -// ===================================================== - - MenuState::MenuState (Program * program, MainMenu * mainMenu, - const string & stateName) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - this->containerName = "MenuState"; - this->program = program; - this->mainMenu = mainMenu; - console.registerGraphicComponent (containerName, "menuStateConsole"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - //switch on menu music again, it might be muted - Config & config = Config::getInstance (); - float - configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - CoreData::getInstance ().getMenuMusic ()->setVolume (configVolume); - - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - //camera - XmlTree - xmlTree; - xmlTree. - load (getGameCustomCoreDataPath - (data_path, "data/core/menu/menu.xml"), - Properties::getTagReplacementValues ()); - const XmlNode * - menuNode = xmlTree.getRootNode (); - const XmlNode * - cameraNode = menuNode->getChild ("camera"); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - //position - const XmlNode * - positionNode = cameraNode->getChild (stateName + "-position"); - Vec3f - startPosition; - startPosition.x = positionNode->getAttribute ("x")->getFloatValue (); - startPosition.y = positionNode->getAttribute ("y")->getFloatValue (); - startPosition.z = positionNode->getAttribute ("z")->getFloatValue (); - camera.setPosition (startPosition); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - //rotation - const XmlNode * - rotationNode = cameraNode->getChild (stateName + "-rotation"); - Vec3f - startRotation; - startRotation.x = rotationNode->getAttribute ("x")->getFloatValue (); - startRotation.y = rotationNode->getAttribute ("y")->getFloatValue (); - startRotation.z = rotationNode->getAttribute ("z")->getFloatValue (); - camera. - setOrientation (Quaternion - (EulerAngles - (degToRad (startRotation.x), - degToRad (startRotation.y), - degToRad (startRotation.z)))); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - } - - MenuState::~MenuState () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - GraphicComponent::clearRegisteredComponents (this->containerName); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s %d]\n", __FILE__, __FUNCTION__, - __LINE__); - } - - void - MenuState::consoleAddLine (string line) - { - bool - onlyWantChatMsgs = console.getOnlyChatMessagesInStoredLines (); - if (onlyWantChatMsgs == true) - { - console.setOnlyChatMessagesInStoredLines (false); - } - console.addLine (line); - if (onlyWantChatMsgs == true) - { - console.setOnlyChatMessagesInStoredLines (true); - } - } - - void - MenuState::reloadUI () - { - //console.resetFonts(); - } - - void - MenuState::setActiveInputLabel (GraphicLabel * newLabel, - GraphicLabel ** activeInputLabelPtr) - { - GraphicLabel * - activeInputLabelEdit = *activeInputLabelPtr; - if (newLabel != NULL) - { - if (newLabel == activeInputLabelEdit) - { - return; - } - string - text = newLabel->getText (); - size_t - found = text.find_last_of ("_"); - if (found == string::npos || found != text.length () - 1) - { - text += "_"; - } - newLabel->setText (text); - //textCharLength = extractKeyPressedUnicodeLength(text); - newLabel-> - setTextCharLengthList (extractKeyPressedUnicodeLength (text)); - } - if (activeInputLabelEdit != NULL - && activeInputLabelEdit->getText ().empty () == false) - { - string - text = activeInputLabelEdit->getText (); - size_t - found = text.find_last_of ("_"); - if (found != string::npos && found == text.length () - 1) - { - //printf("Removing trailing edit char, found = %d [%d][%s]\n",found,text.length(),text.c_str()); - text = text.substr (0, found); - } - activeInputLabelEdit->setText (text); - //textCharLength = extractKeyPressedUnicodeLength(text); - activeInputLabelEdit-> - setTextCharLengthList (extractKeyPressedUnicodeLength (text)); - activeInputLabelEdit->setEditModeEnabled (false); - } - if (newLabel != NULL) - { - *activeInputLabelPtr = newLabel; - newLabel->setEditModeEnabled (true); - } - else - { - *activeInputLabelPtr = NULL; - } - } - - - bool - MenuState::keyPressEditLabel (SDL_KeyboardEvent c, - GraphicLabel ** activeInputLabelPtr) - { - if (isKeyPressed (SDLK_ESCAPE, c, false) == true || - isKeyPressed (SDLK_RETURN, c, false) == true) - { - GraphicLabel * - activeInputLabel = *activeInputLabelPtr; - setActiveInputLabel (NULL, activeInputLabelPtr); - //textCharLength.clear(); - activeInputLabel->clearTextCharLengthList (); - return true; - } - return false; - } - - bool - MenuState::textInputEditLabel (string input, - GraphicLabel ** activeInputLabelPtr) - { - bool - eventHandled = false; - GraphicLabel * - activeInputLabel = *activeInputLabelPtr; - if (activeInputLabel != NULL) - { - int - maxTextSize = activeInputLabel->getMaxEditWidth (); - - //if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || - // (c=='-') || (c=='(') || (c==')')) { - //if(isAllowedInputTextKey(key)) - { - if ((int) activeInputLabel->getTextCharLengthList ().size () < - maxTextSize) - { - string - text = activeInputLabel->getText (); - - if (text.size () > 0) - { - size_t - found = text.find_last_of ("_"); - if (found == string::npos || found != text.length () - 1) - { - text += input; - activeInputLabel->addTextCharLengthToList (input.length ()); - } - else - { - text = text.substr (0, found) + input + "_"; - //int lastCharLen = activeInputLabel->getTextCharLengthList()[activeInputLabel->getTextCharLengthList().size()-1]; - activeInputLabel->deleteTextCharLengthFromList (); - activeInputLabel->addTextCharLengthToList (input.length ()); - activeInputLabel->addTextCharLengthToList (1); - } - } - else - { - text = input; - activeInputLabel->addTextCharLengthToList (input.length ()); - } - //delete [] utfStr; - - activeInputLabel->setText (text); - - eventHandled = true; - } - } - } - return eventHandled; - } - - bool - MenuState::keyDownEditLabel (SDL_KeyboardEvent c, - GraphicLabel ** activeInputLabelPtr) - { - bool - eventHandled = false; - GraphicLabel * - activeInputLabel = *activeInputLabelPtr; - if (activeInputLabel != NULL) - { - string - text = activeInputLabel->getText (); - if (isKeyPressed (SDLK_BACKSPACE, c) == true && text.length () > 0) - { - //printf("BSPACE text [%s]\n",text.c_str()); - - bool - hasUnderscore = false; - bool - delChar = false; - size_t - found = text.find_last_of ("_"); - if (found == string::npos || found != text.length () - 1) - { - //printf("A text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size()); - if (activeInputLabel-> - getTextCharLengthList ()[activeInputLabel-> - getTextCharLengthList ().size () - - 1] >= 1) - { - delChar = true; - } - } - else - { - //printf("B text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size()); - hasUnderscore = true; - if (activeInputLabel->getTextCharLengthList ().size () >= 2 - && activeInputLabel-> - getTextCharLengthList ()[activeInputLabel-> - getTextCharLengthList ().size () - - 2] >= 1) - { - delChar = true; - } - } - if (delChar == true) - { - if (hasUnderscore) - { - //if(textCharLength.size() > 1) { - if (activeInputLabel->getTextCharLengthList ().size () > 1) - { - //printf("Underscore erase start\n"); - for (unsigned int i = 0; - i < - (unsigned int) activeInputLabel-> - getTextCharLengthList ()[activeInputLabel-> - getTextCharLengthList (). - size () - 2]; ++i) - { - text.erase (text.end () - 2); - } - //printf("AFTER DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); - activeInputLabel->deleteTextCharLengthFromList (); - activeInputLabel->deleteTextCharLengthFromList (); - activeInputLabel->addTextCharLengthToList (1); - } - } - else - { - for (unsigned int i = 0; - i < - (unsigned int) activeInputLabel-> - getTextCharLengthList ()[activeInputLabel-> - getTextCharLengthList ().size () - - 1]; ++i) - { - text.erase (text.end () - 1); - } - activeInputLabel->deleteTextCharLengthFromList (); - } - } - activeInputLabel->setText (text); - eventHandled = true; - } - } - return eventHandled; - } - - } + Glest { + namespace + Game { + + // ===================================================== + // class MainMenu + // ===================================================== + + // ===================== PUBLIC ======================== + MenuState * + MainMenu::oldstate = NULL; + + MainMenu::MainMenu(Program * program) : + ProgramState(program), + menuBackgroundVideo(NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + //printf("In MainMenu::MainMenu()\n"); + + mouseX = 100; + mouseY = 100; + + state = NULL; + this-> + program = program; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + Config & + config = Config::getInstance(); + if (config.getString("CustomMenuTextColor", "") != "") { + string + customMenuTextColor = config.getString("CustomMenuTextColor"); + Vec3f + customTextColor = Vec3f::strToVec3(customMenuTextColor); + GraphicComponent::setCustomTextColor(customTextColor); + } + + setState(new MenuStateRoot(program, this)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + } + + void + MainMenu::reloadUI() { + if (state) { + state->reloadUI(); + } + } + + MainMenu::~MainMenu() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + //printf("In MainMenu::~MainMenu()\n"); + + if (menuBackgroundVideo != NULL) { + menuBackgroundVideo->closePlayer(); + delete + menuBackgroundVideo; + menuBackgroundVideo = NULL; + } + delete + state; + state = NULL; + delete + oldstate; + oldstate = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + Renderer::getInstance().endMenu(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + //soundRenderer.stopAllSounds(); + GraphicComponent::setCustomTextColor(Vec3f(1.0f, 1.0f, 1.0f)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + } + + void + MainMenu::init() { + Renderer::getInstance().initMenu(this); + + initBackgroundVideo(); + } + + void + MainMenu::initBackgroundVideo() { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == true + && CoreData::getInstance().hasMainMenuVideoFilename() == true) { + string + introVideoFile = + CoreData::getInstance().getMainMenuVideoFilename(); + string + introVideoFileFallback = + CoreData::getInstance().getMainMenuVideoFilenameFallback(); + + Context * + c = GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl * + glCtx = static_cast (c)->getPlatformContextGlPtr(); + SDL_Window * + window = glCtx->getScreenWindow(); + SDL_Surface * + screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + menuBackgroundVideo = new VideoPlayer(&Renderer::getInstance(), + introVideoFile, + introVideoFileFallback, + window, + 0, 0, + screen->w, + screen->h, + screen->format->BitsPerPixel, + true, + vlcPluginsPath, + SystemFlags:: + VERBOSE_MODE_ENABLED); + menuBackgroundVideo->initPlayer(); + } + } + + //asynchronus render update + void + MainMenu::render() { + Renderer & renderer = Renderer::getInstance(); + + canRender(); + incrementFps(); + + if (state->isMasterserverMode() == false) { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + + if (state->isVideoPlaying() == true) { + if (menuBackgroundVideo != NULL) { + if (menuBackgroundVideo->isPlaying() == true) { + menuBackgroundVideo->closePlayer(); + delete + menuBackgroundVideo; + menuBackgroundVideo = NULL; + } + } + } else if (menuBackgroundVideo == NULL) { + initBackgroundVideo(); + } + renderer.clearBuffers(); + + //3d + renderer.reset3dMenu(); + renderer.clearZBuffer(); + + //printf("In [%s::%s Line: %d] menuBackgroundVideo [%p]\n",__FILE__,__FUNCTION__,__LINE__,menuBackgroundVideo); + + if (menuBackgroundVideo == NULL) { + renderer.loadCameraMatrix(menuBackground.getCamera()); + renderer.renderMenuBackground(&menuBackground); + renderer.renderParticleManager(rsMenu); + } + + //2d + renderer.reset2d(); + + if (menuBackgroundVideo != NULL) { + if (menuBackgroundVideo->isPlaying() == true) { + menuBackgroundVideo->playFrame(false); + } else { + menuBackgroundVideo->RestartVideo(); + } + } + } + state->render(); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + renderer.renderFPSWhenEnabled(lastFps); + renderer.swapBuffers(); + } + } + } + + //syncronus update + void + MainMenu::update() { + if (menuBackgroundVideo == NULL) { + Renderer::getInstance().updateParticleManager(rsMenu); + } + mouse2dAnim = (mouse2dAnim + 1) % Renderer::maxMouse2dAnim; + if (menuBackgroundVideo == NULL) { + menuBackground.update(); + } + state->update(); + } + + //event magangement: mouse click + void + MainMenu::mouseMove(int x, int y, const MouseState * ms) { + mouseX = x; + mouseY = y; + state->mouseMove(x, y, ms); + } + + //returns if exiting + void + MainMenu::mouseDownLeft(int x, int y) { + if (GraphicComponent::getFade() < 0.2f) + return; + state->mouseClick(x, y, mbLeft); + } + + void + MainMenu::mouseDownRight(int x, int y) { + if (GraphicComponent::getFade() < 0.2f) + return; + state->mouseClick(x, y, mbRight); + } + + void + MainMenu::mouseUpLeft(int x, int y) { + if (GraphicComponent::getFade() < 0.2f) + return; + state->mouseUp(x, y, mbLeft); + } + + bool + MainMenu::textInput(std::string text) { + return state->textInput(text); + } + void + MainMenu::keyDown(SDL_KeyboardEvent key) { + state->keyDown(key); + } + + void + MainMenu::keyUp(SDL_KeyboardEvent key) { + state->keyUp(key); + } + + void + MainMenu::keyPress(SDL_KeyboardEvent c) { + state->keyPress(c); + } + + void + MainMenu::setState(MenuState * newstate) { + //printf("In [%s::%s Line: %d] oldstate [%p] newstate [%p] this->state [%p]\n",__FILE__,__FUNCTION__,__LINE__,oldstate,newstate,this->state); + + //printf("In MainMenu::setState() #1\n"); + + if (oldstate != NULL && oldstate != newstate) { + MenuState * + oldstatePtr = oldstate; + delete + oldstate; + + //printf("In MainMenu::setState() #2\n"); + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if (oldstatePtr != this->state) { + oldstate = this->state; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("In MainMenu::setState() #3\n"); + } else { + oldstate = NULL; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("In MainMenu::setState() #4\n"); + } + } else { + oldstate = this->state; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + //printf("In MainMenu::setState() #5\n"); + } + + if (this->state != NULL) { + + } + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + this->state = newstate; + + GraphicComponent::resetFade(); + if (newstate) { + menuBackground.setTargetCamera(newstate->getCamera()); + } + } + + bool + MainMenu::isInSpecialKeyCaptureEvent() { + return state->isInSpecialKeyCaptureEvent(); + } + + void + MainMenu::consoleAddLine(string line) { + if (state != NULL) { + state->consoleAddLine(line); + } + } + + // ===================================================== + // class MenuState + // ===================================================== + + MenuState::MenuState(Program * program, MainMenu * mainMenu, + const string & stateName) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + this->containerName = "MenuState"; + this->program = program; + this->mainMenu = mainMenu; + console.registerGraphicComponent(containerName, "menuStateConsole"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + //switch on menu music again, it might be muted + Config & config = Config::getInstance(); + float + configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + CoreData::getInstance().getMenuMusic()->setVolume(configVolume); + + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + //camera + XmlTree + xmlTree; + xmlTree. + load(getGameCustomCoreDataPath + (data_path, "data/core/menu/menu.xml"), + Properties::getTagReplacementValues()); + const XmlNode * + menuNode = xmlTree.getRootNode(); + const XmlNode * + cameraNode = menuNode->getChild("camera"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + //position + const XmlNode * + positionNode = cameraNode->getChild(stateName + "-position"); + Vec3f + startPosition; + startPosition.x = positionNode->getAttribute("x")->getFloatValue(); + startPosition.y = positionNode->getAttribute("y")->getFloatValue(); + startPosition.z = positionNode->getAttribute("z")->getFloatValue(); + camera.setPosition(startPosition); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + //rotation + const XmlNode * + rotationNode = cameraNode->getChild(stateName + "-rotation"); + Vec3f + startRotation; + startRotation.x = rotationNode->getAttribute("x")->getFloatValue(); + startRotation.y = rotationNode->getAttribute("y")->getFloatValue(); + startRotation.z = rotationNode->getAttribute("z")->getFloatValue(); + camera. + setOrientation(Quaternion + (EulerAngles + (degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + } + + MenuState::~MenuState() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + GraphicComponent::clearRegisteredComponents(this->containerName); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s %d]\n", __FILE__, __FUNCTION__, + __LINE__); + } + + void + MenuState::consoleAddLine(string line) { + bool + onlyWantChatMsgs = console.getOnlyChatMessagesInStoredLines(); + if (onlyWantChatMsgs == true) { + console.setOnlyChatMessagesInStoredLines(false); + } + console.addLine(line); + if (onlyWantChatMsgs == true) { + console.setOnlyChatMessagesInStoredLines(true); + } + } + + void + MenuState::reloadUI() { + //console.resetFonts(); + } + + void + MenuState::setActiveInputLabel(GraphicLabel * newLabel, + GraphicLabel ** activeInputLabelPtr) { + GraphicLabel * + activeInputLabelEdit = *activeInputLabelPtr; + if (newLabel != NULL) { + if (newLabel == activeInputLabelEdit) { + return; + } + string + text = newLabel->getText(); + size_t + found = text.find_last_of("_"); + if (found == string::npos || found != text.length() - 1) { + text += "_"; + } + newLabel->setText(text); + //textCharLength = extractKeyPressedUnicodeLength(text); + newLabel-> + setTextCharLengthList(extractKeyPressedUnicodeLength(text)); + } + if (activeInputLabelEdit != NULL + && activeInputLabelEdit->getText().empty() == false) { + string + text = activeInputLabelEdit->getText(); + size_t + found = text.find_last_of("_"); + if (found != string::npos && found == text.length() - 1) { + //printf("Removing trailing edit char, found = %d [%d][%s]\n",found,text.length(),text.c_str()); + text = text.substr(0, found); + } + activeInputLabelEdit->setText(text); + //textCharLength = extractKeyPressedUnicodeLength(text); + activeInputLabelEdit-> + setTextCharLengthList(extractKeyPressedUnicodeLength(text)); + activeInputLabelEdit->setEditModeEnabled(false); + } + if (newLabel != NULL) { + *activeInputLabelPtr = newLabel; + newLabel->setEditModeEnabled(true); + } else { + *activeInputLabelPtr = NULL; + } + } + + + bool + MenuState::keyPressEditLabel(SDL_KeyboardEvent c, + GraphicLabel ** activeInputLabelPtr) { + if (isKeyPressed(SDLK_ESCAPE, c, false) == true || + isKeyPressed(SDLK_RETURN, c, false) == true) { + GraphicLabel * + activeInputLabel = *activeInputLabelPtr; + setActiveInputLabel(NULL, activeInputLabelPtr); + //textCharLength.clear(); + activeInputLabel->clearTextCharLengthList(); + return true; + } + return false; + } + + bool + MenuState::textInputEditLabel(string input, + GraphicLabel ** activeInputLabelPtr) { + bool + eventHandled = false; + GraphicLabel * + activeInputLabel = *activeInputLabelPtr; + if (activeInputLabel != NULL) { + int + maxTextSize = activeInputLabel->getMaxEditWidth(); + + //if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z') || + // (c=='-') || (c=='(') || (c==')')) { + //if(isAllowedInputTextKey(key)) + { + if ((int) activeInputLabel->getTextCharLengthList().size() < + maxTextSize) { + string + text = activeInputLabel->getText(); + + if (text.size() > 0) { + size_t + found = text.find_last_of("_"); + if (found == string::npos || found != text.length() - 1) { + text += input; + activeInputLabel->addTextCharLengthToList(input.length()); + } else { + text = text.substr(0, found) + input + "_"; + //int lastCharLen = activeInputLabel->getTextCharLengthList()[activeInputLabel->getTextCharLengthList().size()-1]; + activeInputLabel->deleteTextCharLengthFromList(); + activeInputLabel->addTextCharLengthToList(input.length()); + activeInputLabel->addTextCharLengthToList(1); + } + } else { + text = input; + activeInputLabel->addTextCharLengthToList(input.length()); + } + //delete [] utfStr; + + activeInputLabel->setText(text); + + eventHandled = true; + } + } + } + return eventHandled; + } + + bool + MenuState::keyDownEditLabel(SDL_KeyboardEvent c, + GraphicLabel ** activeInputLabelPtr) { + bool + eventHandled = false; + GraphicLabel * + activeInputLabel = *activeInputLabelPtr; + if (activeInputLabel != NULL) { + string + text = activeInputLabel->getText(); + if (isKeyPressed(SDLK_BACKSPACE, c) == true && text.length() > 0) { + //printf("BSPACE text [%s]\n",text.c_str()); + + bool + hasUnderscore = false; + bool + delChar = false; + size_t + found = text.find_last_of("_"); + if (found == string::npos || found != text.length() - 1) { + //printf("A text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size()); + if (activeInputLabel-> + getTextCharLengthList()[activeInputLabel-> + getTextCharLengthList().size() - + 1] >= 1) { + delChar = true; + } + } else { + //printf("B text.length() = %d textCharLength.size() = %d\n",text.length(),textCharLength.size()); + hasUnderscore = true; + if (activeInputLabel->getTextCharLengthList().size() >= 2 + && activeInputLabel-> + getTextCharLengthList()[activeInputLabel-> + getTextCharLengthList().size() - + 2] >= 1) { + delChar = true; + } + } + if (delChar == true) { + if (hasUnderscore) { + //if(textCharLength.size() > 1) { + if (activeInputLabel->getTextCharLengthList().size() > 1) { + //printf("Underscore erase start\n"); + for (unsigned int i = 0; + i < + (unsigned int) activeInputLabel-> + getTextCharLengthList()[activeInputLabel-> + getTextCharLengthList(). + size() - 2]; ++i) { + text.erase(text.end() - 2); + } + //printf("AFTER DEL textCharLength.size() = %d textCharLength[textCharLength.size()-1] = %d text.length() = %d\n",textCharLength.size(),textCharLength[textCharLength.size()-1],text.length()); + activeInputLabel->deleteTextCharLengthFromList(); + activeInputLabel->deleteTextCharLengthFromList(); + activeInputLabel->addTextCharLengthToList(1); + } + } else { + for (unsigned int i = 0; + i < + (unsigned int) activeInputLabel-> + getTextCharLengthList()[activeInputLabel-> + getTextCharLengthList().size() - + 1]; ++i) { + text.erase(text.end() - 1); + } + activeInputLabel->deleteTextCharLengthFromList(); + } + } + activeInputLabel->setText(text); + eventHandled = true; + } + } + return eventHandled; + } + + } } //end namespace diff --git a/source/glest_game/menu/main_menu.h b/source/glest_game/menu/main_menu.h index 1d949b282..72369d7fe 100644 --- a/source/glest_game/menu/main_menu.h +++ b/source/glest_game/menu/main_menu.h @@ -27,173 +27,152 @@ # include "game_settings.h" # include "leak_dumper.h" -namespace Shared -{ - namespace Graphics - { - class VideoPlayer; - } +namespace Shared { + namespace Graphics { + class VideoPlayer; + } } -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class MenuState; + class MenuState; -// ===================================================== -// class MainMenu -// -/// Main menu ProgramState -// ===================================================== + // ===================================================== + // class MainMenu + // + /// Main menu ProgramState + // ===================================================== - class MainMenu:public ProgramState - { + class MainMenu :public ProgramState { - private: - static MenuState *oldstate; - //up - Program *program; + private: + static MenuState *oldstate; + //up + Program *program; - //shared - GameSettings gameSettings; - MenuBackground menuBackground; - ::Shared::Graphics::VideoPlayer * menuBackgroundVideo; + //shared + GameSettings gameSettings; + MenuBackground menuBackground; + ::Shared::Graphics::VideoPlayer * menuBackgroundVideo; - MenuState *state; + MenuState *state; - //shared - int mouseX, mouseY; - int mouse2dAnim; + //shared + int mouseX, mouseY; + int mouse2dAnim; - void initBackgroundVideo (); + void initBackgroundVideo(); - public: - explicit MainMenu (Program * program); - virtual ~ MainMenu (); + public: + explicit MainMenu(Program * program); + virtual ~MainMenu(); - MenuBackground *getMenuBackground () - { - return &menuBackground; - } - const MenuBackground *getConstMenuBackground () const - { - return &menuBackground; - } + MenuBackground *getMenuBackground() { + return &menuBackground; + } + const MenuBackground *getConstMenuBackground() const { + return &menuBackground; + } - virtual void render (); - virtual void update (); - virtual void init (); - virtual void mouseMove (int x, int y, const MouseState * mouseState); - virtual void mouseDownLeft (int x, int y); - virtual void mouseDownRight (int x, int y); - virtual void mouseUpLeft (int x, int y); - virtual bool textInput (std::string text); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyUp (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent key); + virtual void render(); + virtual void update(); + virtual void init(); + virtual void mouseMove(int x, int y, const MouseState * mouseState); + virtual void mouseDownLeft(int x, int y); + virtual void mouseDownRight(int x, int y); + virtual void mouseUpLeft(int x, int y); + virtual bool textInput(std::string text); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyUp(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent key); - void setState (MenuState * state); - virtual bool isInSpecialKeyCaptureEvent (); + void setState(MenuState * state); + virtual bool isInSpecialKeyCaptureEvent(); - int getMouseX () const - { - return mouseX; - } - int getMouseY () const - { - return mouseY; - } - int getMouse2dAnim () const - { - return mouse2dAnim; - } - virtual void consoleAddLine (string line); - virtual void reloadUI (); - }; + int getMouseX() const { + return mouseX; + } + int getMouseY() const { + return mouseY; + } + int getMouse2dAnim() const { + return mouse2dAnim; + } + virtual void consoleAddLine(string line); + virtual void reloadUI(); + }; -// =============================== -// class MenuState -// =============================== + // =============================== + // class MenuState + // =============================== - class MenuState - { - protected: - Program * program; + class MenuState { + protected: + Program * program; - MainMenu *mainMenu; - Camera camera; + MainMenu *mainMenu; + Camera camera; - const char *containerName; - Console console; + const char *containerName; + Console console; - //vector textCharLength; + //vector textCharLength; - protected: + protected: - void setActiveInputLabel (GraphicLabel * newLabel, - GraphicLabel ** activeInputLabelPtr); - bool textInputEditLabel (string input, - GraphicLabel ** activeInputLabelPtr); - bool keyPressEditLabel (SDL_KeyboardEvent c, - GraphicLabel ** activeInputLabelPtr); - bool keyDownEditLabel (SDL_KeyboardEvent c, - GraphicLabel ** activeInputLabelPtr); + void setActiveInputLabel(GraphicLabel * newLabel, + GraphicLabel ** activeInputLabelPtr); + bool textInputEditLabel(string input, + GraphicLabel ** activeInputLabelPtr); + bool keyPressEditLabel(SDL_KeyboardEvent c, + GraphicLabel ** activeInputLabelPtr); + bool keyDownEditLabel(SDL_KeyboardEvent c, + GraphicLabel ** activeInputLabelPtr); - public: - MenuState (Program * program, MainMenu * mainMenu, - const string & stateName); - virtual ~ MenuState (); - virtual void mouseClick (int x, int y, MouseButton mouseButton) = 0; - virtual void mouseUp (int x, int y, const MouseButton mouseButton) - { - }; - virtual void mouseMove (int x, int y, const MouseState * mouseState) = - 0; - virtual void render () = 0; - virtual void update () - { - }; + public: + MenuState(Program * program, MainMenu * mainMenu, + const string & stateName); + virtual ~MenuState(); + virtual void mouseClick(int x, int y, MouseButton mouseButton) = 0; + virtual void mouseUp(int x, int y, const MouseButton mouseButton) { + }; + virtual void mouseMove(int x, int y, const MouseState * mouseState) = + 0; + virtual void render() = 0; + virtual void update() { + }; - virtual bool textInput (std::string text) - { - return false; - } - virtual void keyDown (SDL_KeyboardEvent key) - { - }; - virtual void keyPress (SDL_KeyboardEvent c) - { - }; - virtual void keyUp (SDL_KeyboardEvent key) - { - }; + virtual bool textInput(std::string text) { + return false; + } + virtual void keyDown(SDL_KeyboardEvent key) { + }; + virtual void keyPress(SDL_KeyboardEvent c) { + }; + virtual void keyUp(SDL_KeyboardEvent key) { + }; - virtual bool isMasterserverMode () const - { - return false; - } - const Camera *getCamera () const - { - return &camera; - } + virtual bool isMasterserverMode() const { + return false; + } + const Camera *getCamera() const { + return &camera; + } - virtual bool isInSpecialKeyCaptureEvent () - { - return false; - } - virtual void consoleAddLine (string line); - virtual void reloadUI (); + virtual bool isInSpecialKeyCaptureEvent() { + return false; + } + virtual void consoleAddLine(string line); + virtual void reloadUI(); - virtual bool isVideoPlaying () - { - return false; - }; - }; + virtual bool isVideoPlaying() { + return false; + }; + }; - } + } } //end namespace #endif diff --git a/source/glest_game/menu/menu_background.cpp b/source/glest_game/menu/menu_background.cpp index 2f53d9630..f4dea70e9 100644 --- a/source/glest_game/menu/menu_background.cpp +++ b/source/glest_game/menu/menu_background.cpp @@ -23,305 +23,272 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using namespace - Shared::Xml; +Shared::Xml; using namespace - Shared::Graphics; +Shared::Graphics; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// ===================================================== -// class MenuBackground -// ===================================================== + // ===================================================== + // class MenuBackground + // ===================================================== - MenuBackground::MenuBackground (): - rps (NULL) - { - Renderer & renderer = Renderer::getInstance (); + MenuBackground::MenuBackground() : + rps(NULL) { + Renderer & renderer = Renderer::getInstance(); - //load data - string - data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); + //load data + string + data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); - XmlTree - xmlTree; - xmlTree. - load (getGameCustomCoreDataPath (data_path, "data/core/menu/menu.xml"), - Properties::getTagReplacementValues ()); - const XmlNode * - menuNode = xmlTree.getRootNode (); + XmlTree + xmlTree; + xmlTree. + load(getGameCustomCoreDataPath(data_path, "data/core/menu/menu.xml"), + Properties::getTagReplacementValues()); + const XmlNode * + menuNode = xmlTree.getRootNode(); - //water - const XmlNode * - waterNode = menuNode->getChild ("water"); - water = waterNode->getAttribute ("value")->getBoolValue (); - if (water) - { - waterHeight = waterNode->getAttribute ("height")->getFloatValue (); + //water + const XmlNode * + waterNode = menuNode->getChild("water"); + water = waterNode->getAttribute("value")->getBoolValue(); + if (water) { + waterHeight = waterNode->getAttribute("height")->getFloatValue(); - //water texture - waterTexture = renderer.newTexture2D (rsMenu); - if (waterTexture) - { - waterTexture->getPixmap ()->init (4); - waterTexture-> - getPixmap ()-> - load (getGameCustomCoreDataPath - (data_path, "data/core/menu/textures/water.tga")); - } - } + //water texture + waterTexture = renderer.newTexture2D(rsMenu); + if (waterTexture) { + waterTexture->getPixmap()->init(4); + waterTexture-> + getPixmap()-> + load(getGameCustomCoreDataPath + (data_path, "data/core/menu/textures/water.tga")); + } + } - //fog - const XmlNode * - fogNode = menuNode->getChild ("fog"); - fog = fogNode->getAttribute ("value")->getBoolValue (); - if (fog) - { - fogDensity = fogNode->getAttribute ("density")->getFloatValue (); - } + //fog + const XmlNode * + fogNode = menuNode->getChild("fog"); + fog = fogNode->getAttribute("value")->getBoolValue(); + if (fog) { + fogDensity = fogNode->getAttribute("density")->getFloatValue(); + } - //rain - bool - withRainEffect = - Config::getInstance ().getBool ("RainEffectMenu", "true"); - if (withRainEffect == true) - { - rain = - menuNode->getChild ("rain")->getAttribute ("value")-> - getBoolValue (); - if (rain) - { - createRainParticleSystem (); - } - } - else - { - rain = false; - } + //rain + bool + withRainEffect = + Config::getInstance().getBool("RainEffectMenu", "true"); + if (withRainEffect == true) { + rain = + menuNode->getChild("rain")->getAttribute("value")-> + getBoolValue(); + if (rain) { + createRainParticleSystem(); + } + } else { + rain = false; + } - //camera - const XmlNode * - cameraNode = menuNode->getChild ("camera"); + //camera + const XmlNode * + cameraNode = menuNode->getChild("camera"); - //position - const XmlNode * - positionNode = cameraNode->getChild ("start-position"); - Vec3f - startPosition; - startPosition.x = positionNode->getAttribute ("x")->getFloatValue (); - startPosition.y = positionNode->getAttribute ("y")->getFloatValue (); - startPosition.z = positionNode->getAttribute ("z")->getFloatValue (); - camera.setPosition (startPosition); + //position + const XmlNode * + positionNode = cameraNode->getChild("start-position"); + Vec3f + startPosition; + startPosition.x = positionNode->getAttribute("x")->getFloatValue(); + startPosition.y = positionNode->getAttribute("y")->getFloatValue(); + startPosition.z = positionNode->getAttribute("z")->getFloatValue(); + camera.setPosition(startPosition); - //rotation - const XmlNode * - rotationNode = cameraNode->getChild ("start-rotation"); - Vec3f - startRotation; - startRotation.x = rotationNode->getAttribute ("x")->getFloatValue (); - startRotation.y = rotationNode->getAttribute ("y")->getFloatValue (); - startRotation.z = rotationNode->getAttribute ("z")->getFloatValue (); - camera. - setOrientation (Quaternion - (EulerAngles - (degToRad (startRotation.x), - degToRad (startRotation.y), - degToRad (startRotation.z)))); + //rotation + const XmlNode * + rotationNode = cameraNode->getChild("start-rotation"); + Vec3f + startRotation; + startRotation.x = rotationNode->getAttribute("x")->getFloatValue(); + startRotation.y = rotationNode->getAttribute("y")->getFloatValue(); + startRotation.z = rotationNode->getAttribute("z")->getFloatValue(); + camera. + setOrientation(Quaternion + (EulerAngles + (degToRad(startRotation.x), + degToRad(startRotation.y), + degToRad(startRotation.z)))); - //load main model - string - mainModelFile = "data/core/menu/main_model/menu_main.g3d"; - if (menuNode->hasChild ("menu-background-model") == true) - { - //mainModel->load(data_path + "data/core/menu/main_model/menu_main.g3d"); - const XmlNode * - mainMenuModelNode = menuNode->getChild ("menu-background-model"); - mainModelFile = - mainMenuModelNode->getAttribute ("value")->getRestrictedValue (); - } - mainModel = - renderer.newModel (rsMenu, - getGameCustomCoreDataPath (data_path, - mainModelFile)); + //load main model + string + mainModelFile = "data/core/menu/main_model/menu_main.g3d"; + if (menuNode->hasChild("menu-background-model") == true) { + //mainModel->load(data_path + "data/core/menu/main_model/menu_main.g3d"); + const XmlNode * + mainMenuModelNode = menuNode->getChild("menu-background-model"); + mainModelFile = + mainMenuModelNode->getAttribute("value")->getRestrictedValue(); + } + mainModel = + renderer.newModel(rsMenu, + getGameCustomCoreDataPath(data_path, + mainModelFile)); - //models - for (int i = 0; i < 5; ++i) - { - characterModels[i] = - renderer.newModel (rsMenu, - getGameCustomCoreDataPath (data_path, - "data/core/menu/about_models/character" - + intToStr (i) + - ".g3d")); - } + //models + for (int i = 0; i < 5; ++i) { + characterModels[i] = + renderer.newModel(rsMenu, + getGameCustomCoreDataPath(data_path, + "data/core/menu/about_models/character" + + intToStr(i) + + ".g3d")); + } - //about position - positionNode = cameraNode->getChild ("about-position"); - aboutPosition.x = positionNode->getAttribute ("x")->getFloatValue (); - aboutPosition.y = positionNode->getAttribute ("y")->getFloatValue (); - aboutPosition.z = positionNode->getAttribute ("z")->getFloatValue (); - //rotationNode= cameraNode->getChild("about-rotation"); + //about position + positionNode = cameraNode->getChild("about-position"); + aboutPosition.x = positionNode->getAttribute("x")->getFloatValue(); + aboutPosition.y = positionNode->getAttribute("y")->getFloatValue(); + aboutPosition.z = positionNode->getAttribute("z")->getFloatValue(); + //rotationNode= cameraNode->getChild("about-rotation"); - targetCamera = NULL; - t = 0.f; - fade = 0.f; - anim = 0.f; - } + targetCamera = NULL; + t = 0.f; + fade = 0.f; + anim = 0.f; + } - MenuBackground::~MenuBackground () - { - //printf("In ~MenuBackground() rps = %p\n",rps); + MenuBackground::~MenuBackground() { + //printf("In ~MenuBackground() rps = %p\n",rps); - cleanup (); - } + cleanup(); + } - void - MenuBackground::cleanup () - { - //printf("In MenuBackground::cleanup() rps = %p\n",rps); + void + MenuBackground::cleanup() { + //printf("In MenuBackground::cleanup() rps = %p\n",rps); - if (rps != NULL) - { - Renderer & renderer = Renderer::getInstance (); - if (renderer.validateParticleSystemStillExists (rps, rsMenu) == true) - { - rps->fade (); - vector < ParticleSystem * >particleSystems; - particleSystems.push_back (rps); - renderer.cleanupParticleSystems (particleSystems, rsMenu); - } + if (rps != NULL) { + Renderer & renderer = Renderer::getInstance(); + if (renderer.validateParticleSystemStillExists(rps, rsMenu) == true) { + rps->fade(); + vector < ParticleSystem * >particleSystems; + particleSystems.push_back(rps); + renderer.cleanupParticleSystems(particleSystems, rsMenu); + } - rps = NULL; - } - } + rps = NULL; + } + } - void - MenuBackground::createRainParticleSystem () - { - //printf("In MenuBackground::createRainParticleSystem() rps = %p\n",rps); + void + MenuBackground::createRainParticleSystem() { + //printf("In MenuBackground::createRainParticleSystem() rps = %p\n",rps); - if (rps == NULL) - { - rps = new RainParticleSystem (); - rps->setSpeed (12.f / GameConstants::updateFps); - rps->setEmissionRate (25); - rps->setWind (-90.f, 4.f / GameConstants::updateFps); - rps->setPos (Vec3f (0.f, 25.f, 0.f)); - rps->setColor (Vec4f (1.f, 1.f, 1.f, 0.2f)); - rps->setRadius (30.f); + if (rps == NULL) { + rps = new RainParticleSystem(); + rps->setSpeed(12.f / GameConstants::updateFps); + rps->setEmissionRate(25); + rps->setWind(-90.f, 4.f / GameConstants::updateFps); + rps->setPos(Vec3f(0.f, 25.f, 0.f)); + rps->setColor(Vec4f(1.f, 1.f, 1.f, 0.2f)); + rps->setRadius(30.f); - Renderer & renderer = Renderer::getInstance (); - renderer.manageParticleSystem (rps, rsMenu); + Renderer & renderer = Renderer::getInstance(); + renderer.manageParticleSystem(rps, rsMenu); - for (int i = 0; i < raindropCount; ++i) - { - raindropStates[i] = random.randRange (0.f, 1.f); - raindropPos[i] = computeRaindropPos (); - } - } - } + for (int i = 0; i < raindropCount; ++i) { + raindropStates[i] = random.randRange(0.f, 1.f); + raindropPos[i] = computeRaindropPos(); + } + } + } - void - MenuBackground::setTargetCamera (const Camera * targetCamera) - { - this->targetCamera = targetCamera; - this->lastCamera = camera; - t = 0.f; - } + void + MenuBackground::setTargetCamera(const Camera * targetCamera) { + this->targetCamera = targetCamera; + this->lastCamera = camera; + t = 0.f; + } - void - MenuBackground::update () - { - //rain drops - bool - withRainEffect = - Config::getInstance ().getBool ("RainEffectMenu", "true"); - if (withRainEffect == true) - { - if (rain == false) - { - rain = true; - createRainParticleSystem (); - } + void + MenuBackground::update() { + //rain drops + bool + withRainEffect = + Config::getInstance().getBool("RainEffectMenu", "true"); + if (withRainEffect == true) { + if (rain == false) { + rain = true; + createRainParticleSystem(); + } - for (int i = 0; i < raindropCount; ++i) - { - raindropStates[i] += 1.f / GameConstants::updateFps; - if (raindropStates[i] >= 1.f) - { - raindropStates[i] = 0.f; - raindropPos[i] = computeRaindropPos (); - } - } - } - else if (rain == true) - { - rain = false; + for (int i = 0; i < raindropCount; ++i) { + raindropStates[i] += 1.f / GameConstants::updateFps; + if (raindropStates[i] >= 1.f) { + raindropStates[i] = 0.f; + raindropPos[i] = computeRaindropPos(); + } + } + } else if (rain == true) { + rain = false; - cleanup (); - } + cleanup(); + } - if (targetCamera != NULL) - { - t += - ((0.01f + - (1.f - t) / 10.f) / 20.f) * (60.f / GameConstants::updateFps); + if (targetCamera != NULL) { + t += + ((0.01f + + (1.f - t) / 10.f) / 20.f) * (60.f / GameConstants::updateFps); - //interpolate position - camera.setPosition (lastCamera.getPosition (). - lerp (t, targetCamera->getPosition ())); + //interpolate position + camera.setPosition(lastCamera.getPosition(). + lerp(t, targetCamera->getPosition())); - //interpolate orientation - Quaternion - q = - lastCamera.getOrientation ().lerp (t, - targetCamera->getOrientation ()); - camera.setOrientation (q); + //interpolate orientation + Quaternion + q = + lastCamera.getOrientation().lerp(t, + targetCamera->getOrientation()); + camera.setOrientation(q); - if (t >= 1.f) - { - targetCamera = NULL; - t = 0.f; - } - } + if (t >= 1.f) { + targetCamera = NULL; + t = 0.f; + } + } - //fade - if (fade <= 1.f) - { - fade += 0.6f / GameConstants::updateFps; - if (fade > 1.f) - { - fade = 1.f; - } - } + //fade + if (fade <= 1.f) { + fade += 0.6f / GameConstants::updateFps; + if (fade > 1.f) { + fade = 1.f; + } + } - //animation - anim += - (0.6f / GameConstants::updateFps) / 5 + random.randRange (0.f, - (0.6f / - GameConstants:: - updateFps) - / 5.f); - if (anim > 1.f) - { - anim = 0.f; - } - } + //animation + anim += + (0.6f / GameConstants::updateFps) / 5 + random.randRange(0.f, + (0.6f / + GameConstants:: + updateFps) + / 5.f); + if (anim > 1.f) { + anim = 0.f; + } + } - Vec2f - MenuBackground::computeRaindropPos () - { - float - f = static_cast < float >(meshSize); - return Vec2f (random.randRange (-f, f), random.randRange (-f, f)); - } + Vec2f + MenuBackground::computeRaindropPos() { + float + f = static_cast (meshSize); + return Vec2f(random.randRange(-f, f), random.randRange(-f, f)); + } - } + } } //end namespace diff --git a/source/glest_game/menu/menu_background.h b/source/glest_game/menu/menu_background.h index 3ed5a8ca0..e47c40ca7 100644 --- a/source/glest_game/menu/menu_background.h +++ b/source/glest_game/menu/menu_background.h @@ -26,215 +26,197 @@ # include "leak_dumper.h" using - Shared::Graphics::RainParticleSystem; +Shared::Graphics::RainParticleSystem; using - Shared::Graphics::FireParticleSystem; +Shared::Graphics::FireParticleSystem; using - Shared::Graphics::Camera; +Shared::Graphics::Camera; using - Shared::Graphics::Vec3f; +Shared::Graphics::Vec3f; using - Shared::Graphics::Vec2f; +Shared::Graphics::Vec2f; using - Shared::Graphics::Texture2D; +Shared::Graphics::Texture2D; using - Shared::Graphics::Model; +Shared::Graphics::Model; using - Shared::Util::RandomGen; +Shared::Util::RandomGen; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// =========================================================== -// class MenuBackground -// -/// Holds the data to display the 3D environment -/// in the MenuState -// =========================================================== + // =========================================================== + // class MenuBackground + // + /// Holds the data to display the 3D environment + /// in the MenuState + // =========================================================== - class - MenuBackground - { - public: - static const int - meshSize = 32; - static const int - raindropCount = 1000; - static const int - characterCount = 5; + class + MenuBackground { + public: + static const int + meshSize = 32; + static const int + raindropCount = 1000; + static const int + characterCount = 5; - private: - Model * - mainModel; + private: + Model * + mainModel; - //water - bool - water; - float - waterHeight; - Texture2D * - waterTexture; + //water + bool + water; + float + waterHeight; + Texture2D * + waterTexture; - //fog - bool - fog; - float - fogDensity; + //fog + bool + fog; + float + fogDensity; - //rain - bool - rain; - Vec2f - raindropPos[raindropCount]; - float - raindropStates[raindropCount]; + //rain + bool + rain; + Vec2f + raindropPos[raindropCount]; + float + raindropStates[raindropCount]; - //camera - Camera - camera; - Camera - lastCamera; - const Camera * - targetCamera; - float - t; + //camera + Camera + camera; + Camera + lastCamera; + const Camera * + targetCamera; + float + t; - //misc - RandomGen - random; - Model * - characterModels[characterCount]; - float - anim; - float - fade; - Vec3f - aboutPosition; + //misc + RandomGen + random; + Model * + characterModels[characterCount]; + float + anim; + float + fade; + Vec3f + aboutPosition; - RainParticleSystem * - rps; + RainParticleSystem * + rps; - public: - MenuBackground (); - ~ - MenuBackground (); + public: + MenuBackground(); + ~ + MenuBackground(); - bool - getWater () const - { - return - water; - } - float - getWaterHeight () const - { - return - waterHeight; - } - bool - getFog () const - { - return - fog; - } - float - getFogDensity () const - { - return - fogDensity; - } - bool - getRain () const - { - return - rain; - } - Texture2D * - getWaterTexture () const - { - return - waterTexture; - } - const Camera * - getCamera () const - { - return & - camera; - } - const Model * - getCharacterModel (int i) const - { - return - characterModels[i]; - } - Model * - getCharacterModelPtr (int i) const - { - return - characterModels[i]; - } - const Model * - getMainModel () const - { - return - mainModel; - } - Model * - getMainModelPtr () const - { - return - mainModel; - } - float - getFade () const - { - return - fade; - } - Vec2f - getRaindropPos (int i) const - { - return - raindropPos[i]; - } - float - getRaindropState (int i) const - { - return - raindropStates[i]; - } - float - getAnim () const - { - return - anim; - } - const - Vec3f & - getAboutPosition () const - { - return - aboutPosition; - } + bool + getWater() const { + return + water; + } + float + getWaterHeight() const { + return + waterHeight; + } + bool + getFog() const { + return + fog; + } + float + getFogDensity() const { + return + fogDensity; + } + bool + getRain() const { + return + rain; + } + Texture2D * + getWaterTexture() const { + return + waterTexture; + } + const Camera * + getCamera() const { + return & + camera; + } + const Model * + getCharacterModel(int i) const { + return + characterModels[i]; + } + Model * + getCharacterModelPtr(int i) const { + return + characterModels[i]; + } + const Model * + getMainModel() const { + return + mainModel; + } + Model * + getMainModelPtr() const { + return + mainModel; + } + float + getFade() const { + return + fade; + } + Vec2f + getRaindropPos(int i) const { + return + raindropPos[i]; + } + float + getRaindropState(int i) const { + return + raindropStates[i]; + } + float + getAnim() const { + return + anim; + } + const + Vec3f & + getAboutPosition() const { + return + aboutPosition; + } - void - setTargetCamera (const Camera * targetCamera); - void - update (); + void + setTargetCamera(const Camera * targetCamera); + void + update(); - private: - Vec2f - computeRaindropPos (); - void - createRainParticleSystem (); - void - cleanup (); - }; + private: + Vec2f + computeRaindropPos(); + void + createRainParticleSystem(); + void + cleanup(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_about.cpp b/source/glest_game/menu/menu_state_about.cpp index c5f76c7f0..3e1f3dd56 100644 --- a/source/glest_game/menu/menu_state_about.cpp +++ b/source/glest_game/menu/menu_state_about.cpp @@ -25,398 +25,348 @@ using namespace std; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class MenuStateAbout -// ===================================================== + // ===================================================== + // class MenuStateAbout + // ===================================================== - MenuStateAbout::MenuStateAbout (Program * program, - MainMenu * mainMenu):MenuState (program, - mainMenu, - "about") - { + MenuStateAbout::MenuStateAbout(Program * program, + MainMenu * mainMenu) :MenuState(program, + mainMenu, + "about") { - containerName = "About"; - Lang & lang = Lang::getInstance (); + containerName = "About"; + Lang & lang = Lang::getInstance(); - adjustModelText = true; + adjustModelText = true; - customModTextureX = 0; - customModTextureY = 0; - customModTextureW = 0; - customModTextureH = 0; - customModTextureAlpha = 0.f; + customModTextureX = 0; + customModTextureY = 0; + customModTextureW = 0; + customModTextureH = 0; + customModTextureAlpha = 0.f; - string additionalCredits = loadAdditionalCredits (); + string additionalCredits = loadAdditionalCredits(); - //init - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (438, 100, 125); - buttonReturn.setText (lang.getString ("Return")); + //init + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(438, 100, 125); + buttonReturn.setText(lang.getString("Return")); - labelAdditionalCredits.registerGraphicComponent (containerName, - "labelAdditionalCredits"); - labelAdditionalCredits.init (500, 700); - labelAdditionalCredits.setText (additionalCredits); + labelAdditionalCredits.registerGraphicComponent(containerName, + "labelAdditionalCredits"); + labelAdditionalCredits.init(500, 700); + labelAdditionalCredits.setText(additionalCredits); - if (additionalCredits == "") - { - for (int i = 0; i < aboutStringCount1; ++i) - { - labelAbout1[i].registerGraphicComponent (containerName, - "labelAbout1" + - intToStr (i)); - labelAbout1[i].init (100, 700 - i * 20); - labelAbout1[i].setText (getAboutString1 (i)); - } + if (additionalCredits == "") { + for (int i = 0; i < aboutStringCount1; ++i) { + labelAbout1[i].registerGraphicComponent(containerName, + "labelAbout1" + + intToStr(i)); + labelAbout1[i].init(100, 700 - i * 20); + labelAbout1[i].setText(getAboutString1(i)); + } - for (int i = 0; i < aboutStringCount2; ++i) - { - labelAbout2[i].registerGraphicComponent (containerName, - "labelAbout2" + - intToStr (i)); - labelAbout2[i].init (450, 620 - i * 20); - labelAbout2[i].setText (getAboutString2 (i)); - } - } - else - { - for (int i = 0; i < aboutStringCount1; ++i) - { - labelAbout1[i].registerGraphicComponent (containerName, - "labelAbout1" + - intToStr (i)); - labelAbout1[i].init (100, 700 - i * 20); - labelAbout1[i].setText (getAboutString1 (i)); - } + for (int i = 0; i < aboutStringCount2; ++i) { + labelAbout2[i].registerGraphicComponent(containerName, + "labelAbout2" + + intToStr(i)); + labelAbout2[i].init(450, 620 - i * 20); + labelAbout2[i].setText(getAboutString2(i)); + } + } else { + for (int i = 0; i < aboutStringCount1; ++i) { + labelAbout1[i].registerGraphicComponent(containerName, + "labelAbout1" + + intToStr(i)); + labelAbout1[i].init(100, 700 - i * 20); + labelAbout1[i].setText(getAboutString1(i)); + } - for (int i = 0; i < aboutStringCount2; ++i) - { - labelAbout2[i].registerGraphicComponent (containerName, - "labelAbout2" + - intToStr (i)); - labelAbout2[i].init (100, 620 - i * 20); - labelAbout2[i].setText (getAboutString2 (i)); - } - } + for (int i = 0; i < aboutStringCount2; ++i) { + labelAbout2[i].registerGraphicComponent(containerName, + "labelAbout2" + + intToStr(i)); + labelAbout2[i].init(100, 620 - i * 20); + labelAbout2[i].setText(getAboutString2(i)); + } + } - for (int i = 0; i < teammateCount; ++i) - { - int xPos = (182 + i * 138); - labelTeammateName[i].registerGraphicComponent (containerName, - "labelTeammateName" + - intToStr (i)); - labelTeammateName[i].init (xPos, 500); - labelTeammateRole[i].registerGraphicComponent (containerName, - "labelTeammateRole" + - intToStr (i)); - labelTeammateRole[i].init (xPos, 520); + for (int i = 0; i < teammateCount; ++i) { + int xPos = (182 + i * 138); + labelTeammateName[i].registerGraphicComponent(containerName, + "labelTeammateName" + + intToStr(i)); + labelTeammateName[i].init(xPos, 500); + labelTeammateRole[i].registerGraphicComponent(containerName, + "labelTeammateRole" + + intToStr(i)); + labelTeammateRole[i].init(xPos, 520); - labelTeammateName[i].setText (getTeammateName (i)); - labelTeammateRole[i].setText (getTeammateRole (i)); - } + labelTeammateName[i].setText(getTeammateName(i)); + labelTeammateRole[i].setText(getTeammateRole(i)); + } - for (int i = teammateTopLineCount; i < teammateCount; ++i) - { - labelTeammateName[i].init (202 + (i - 5) * 138, 160); - labelTeammateRole[i].init (202 + (i - 5) * 138, 180); - } - labelTeammateName[8].init (labelTeammateName[4].getX (), 160); - labelTeammateRole[8].init (labelTeammateRole[4].getX (), 180); + for (int i = teammateTopLineCount; i < teammateCount; ++i) { + labelTeammateName[i].init(202 + (i - 5) * 138, 160); + labelTeammateRole[i].init(202 + (i - 5) * 138, 180); + } + labelTeammateName[8].init(labelTeammateName[4].getX(), 160); + labelTeammateRole[8].init(labelTeammateRole[4].getX(), 180); - customModTexture = NULL; - labelCustomModCredits.registerGraphicComponent (containerName, - "labelCustomModCredits"); - labelCustomModCredits.init (-1, -1); - labelCustomModCredits.setText (""); - labelCustomModCredits.setVisible (false); + customModTexture = NULL; + labelCustomModCredits.registerGraphicComponent(containerName, + "labelCustomModCredits"); + labelCustomModCredits.init(-1, -1); + labelCustomModCredits.setText(""); + labelCustomModCredits.setVisible(false); - enableCustomModCredits = - Config::getInstance ().getBool ("EnabledCustomModCredits", "false"); - if (enableCustomModCredits == true) - { - string customModCreditsText = - Config::getInstance ().getString ("CustomModCreditsText", ""); - if (customModCreditsText != "") - { - replaceAll (customModCreditsText, "\\n", "\n"); - int x = - Config::getInstance ().getInt ("CustomModCreditsTextX", "1"); - int y = - Config::getInstance ().getInt ("CustomModCreditsTextY", "1"); - int w = - Config::getInstance ().getInt ("CustomModCreditsTextW", - intToStr (GraphicLabel::defW). - c_str ()); - int h = - Config::getInstance ().getInt ("CustomModCreditsTextH", - intToStr (GraphicLabel::defH). - c_str ()); + enableCustomModCredits = + Config::getInstance().getBool("EnabledCustomModCredits", "false"); + if (enableCustomModCredits == true) { + string customModCreditsText = + Config::getInstance().getString("CustomModCreditsText", ""); + if (customModCreditsText != "") { + replaceAll(customModCreditsText, "\\n", "\n"); + int x = + Config::getInstance().getInt("CustomModCreditsTextX", "1"); + int y = + Config::getInstance().getInt("CustomModCreditsTextY", "1"); + int w = + Config::getInstance().getInt("CustomModCreditsTextW", + intToStr(GraphicLabel::defW). + c_str()); + int h = + Config::getInstance().getInt("CustomModCreditsTextH", + intToStr(GraphicLabel::defH). + c_str()); - labelCustomModCredits.init (x, y, w, h); - labelCustomModCredits.setText (customModCreditsText); - labelCustomModCredits.setVisible (true); - } + labelCustomModCredits.init(x, y, w, h); + labelCustomModCredits.setText(customModCreditsText); + labelCustomModCredits.setVisible(true); + } - int buttonReturnX = - Config::getInstance ().getInt ("CustomModCreditsReturnX", - intToStr (buttonReturn.getX ()). - c_str ()); - int buttonReturnY = - Config::getInstance ().getInt ("CustomModCreditsReturnY", - intToStr (buttonReturn.getY ()). - c_str ()); - int buttonReturnW = - Config::getInstance ().getInt ("CustomModCreditsReturnW", - intToStr (buttonReturn.getW ()). - c_str ()); + int buttonReturnX = + Config::getInstance().getInt("CustomModCreditsReturnX", + intToStr(buttonReturn.getX()). + c_str()); + int buttonReturnY = + Config::getInstance().getInt("CustomModCreditsReturnY", + intToStr(buttonReturn.getY()). + c_str()); + int buttonReturnW = + Config::getInstance().getInt("CustomModCreditsReturnW", + intToStr(buttonReturn.getW()). + c_str()); - buttonReturn.init (buttonReturnX, buttonReturnY, buttonReturnW); - } + buttonReturn.init(buttonReturnX, buttonReturnY, buttonReturnW); + } - GraphicComponent::applyAllCustomProperties (containerName); - } + GraphicComponent::applyAllCustomProperties(containerName); + } - MenuStateAbout::~MenuStateAbout () - { - if (customModTexture != NULL) - { - Renderer::getInstance ().endTexture (rsGlobal, customModTexture, - false); - customModTexture = NULL; - } - } + MenuStateAbout::~MenuStateAbout() { + if (customModTexture != NULL) { + Renderer::getInstance().endTexture(rsGlobal, customModTexture, + false); + customModTexture = NULL; + } + } - void MenuStateAbout::reloadUI () - { - Lang & lang = Lang::getInstance (); + void MenuStateAbout::reloadUI() { + Lang & lang = Lang::getInstance(); - adjustModelText = true; - string additionalCredits = loadAdditionalCredits (); + adjustModelText = true; + string additionalCredits = loadAdditionalCredits(); - buttonReturn.setText (lang.getString ("Return")); - labelAdditionalCredits.setText (additionalCredits); + buttonReturn.setText(lang.getString("Return")); + labelAdditionalCredits.setText(additionalCredits); - //if(additionalCredits == "") { - for (int i = 0; i < aboutStringCount1; ++i) - { - labelAbout1[i].setText (getAboutString1 (i)); - } + //if(additionalCredits == "") { + for (int i = 0; i < aboutStringCount1; ++i) { + labelAbout1[i].setText(getAboutString1(i)); + } - for (int i = 0; i < aboutStringCount2; ++i) - { - labelAbout2[i].setText (getAboutString2 (i)); - } - //} - //else { - // for(int i= 0; i < aboutStringCount1; ++i){ - // labelAbout1[i].setText(getAboutString1(i)); - // } + for (int i = 0; i < aboutStringCount2; ++i) { + labelAbout2[i].setText(getAboutString2(i)); + } + //} + //else { + // for(int i= 0; i < aboutStringCount1; ++i){ + // labelAbout1[i].setText(getAboutString1(i)); + // } - // for(int i= 0; i < aboutStringCount2; ++i){ - // labelAbout2[i].setText(getAboutString2(i)); - // } - //} + // for(int i= 0; i < aboutStringCount2; ++i){ + // labelAbout2[i].setText(getAboutString2(i)); + // } + //} - for (int i = 0; i < teammateCount; ++i) - { - labelTeammateName[i].setText (getTeammateName (i)); - labelTeammateRole[i].setText (getTeammateRole (i)); - } + for (int i = 0; i < teammateCount; ++i) { + labelTeammateName[i].setText(getTeammateName(i)); + labelTeammateRole[i].setText(getTeammateRole(i)); + } - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } - string MenuStateAbout::loadAdditionalCredits () - { - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - if (data_path != "") - { - endPathWithSlash (data_path); - } - string result = ""; - const string dir = - getGameCustomCoreDataPath (data_path, "data/core/menu/credits.txt"); - //printf("dir [%s]\n",dir.c_str()); + string MenuStateAbout::loadAdditionalCredits() { + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if (data_path != "") { + endPathWithSlash(data_path); + } + string result = ""; + const string dir = + getGameCustomCoreDataPath(data_path, "data/core/menu/credits.txt"); + //printf("dir [%s]\n",dir.c_str()); - if (fileExists (dir) == true) - { + if (fileExists(dir) == true) { #if defined(WIN32) && !defined(__MINGW32__) - FILE *fp = _wfopen (utf8_decode (dir).c_str (), L"r"); - ifstream file (fp); + FILE *fp = _wfopen(utf8_decode(dir).c_str(), L"r"); + ifstream file(fp); #else - ifstream file (dir.c_str ()); + ifstream file(dir.c_str()); #endif - std::string buffer; - while (!file.eof ()) - { - getline (file, buffer); - result += buffer + "\n"; - } - std::cout << buffer << std::endl; - file.close (); + std::string buffer; + while (!file.eof()) { + getline(file, buffer); + result += buffer + "\n"; + } + std::cout << buffer << std::endl; + file.close(); #if defined(WIN32) && !defined(__MINGW32__) - if (fp) - fclose (fp); + if (fp) + fclose(fp); #endif - } - return result; - } + } + return result; + } - void MenuStateAbout::mouseClick (int x, int y, MouseButton mouseButton) - { + void MenuStateAbout::mouseClick(int x, int y, MouseButton mouseButton) { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); - if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - } + if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + } - } + } - void MenuStateAbout::mouseMove (int x, int y, const MouseState * ms) - { - buttonReturn.mouseMove (x, y); - } + void MenuStateAbout::mouseMove(int x, int y, const MouseState * ms) { + buttonReturn.mouseMove(x, y); + } - void MenuStateAbout::render () - { - Renderer & renderer = Renderer::getInstance (); + void MenuStateAbout::render() { + Renderer & renderer = Renderer::getInstance(); - if (enableCustomModCredits == true) - { - if (customModTexture == NULL) - { - string customModCreditsTextureFile = - Config::getInstance ().getString ("CustomModCreditsTextureFile", - ""); - if (customModCreditsTextureFile != "") - { - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - if (data_path != "") - { - endPathWithSlash (data_path); - } - customModTexture = - Renderer::findTexture (data_path + customModCreditsTextureFile); - } - } + if (enableCustomModCredits == true) { + if (customModTexture == NULL) { + string customModCreditsTextureFile = + Config::getInstance().getString("CustomModCreditsTextureFile", + ""); + if (customModCreditsTextureFile != "") { + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if (data_path != "") { + endPathWithSlash(data_path); + } + customModTexture = + Renderer::findTexture(data_path + customModCreditsTextureFile); + } + } - renderer.renderBackground (customModTexture); - renderer.renderLabel (&labelCustomModCredits); - } - else - { - renderer.renderLabel (&labelAdditionalCredits); + renderer.renderBackground(customModTexture); + renderer.renderLabel(&labelCustomModCredits); + } else { + renderer.renderLabel(&labelAdditionalCredits); - for (int i = 0; i < aboutStringCount1; ++i) - { - renderer.renderLabel (&labelAbout1[i]); - } - for (int i = 0; i < aboutStringCount2; ++i) - { - renderer.renderLabel (&labelAbout2[i]); - } + for (int i = 0; i < aboutStringCount1; ++i) { + renderer.renderLabel(&labelAbout1[i]); + } + for (int i = 0; i < aboutStringCount2; ++i) { + renderer.renderLabel(&labelAbout2[i]); + } - if (adjustModelText == true) - { - std::vector < Vec3f > &characterMenuScreenPositionListCache = - CacheManager::getCachedItem < std::vector < Vec3f > - >(GameConstants::characterMenuScreenPositionListCacheLookupKey); + if (adjustModelText == true) { + std::vector < Vec3f > &characterMenuScreenPositionListCache = + CacheManager::getCachedItem < std::vector < Vec3f > + >(GameConstants::characterMenuScreenPositionListCacheLookupKey); - for (int i = 0; i < teammateCount; ++i) - { - int characterPos = (i % teammateTopLineCount); - if (characterPos < - (int) characterMenuScreenPositionListCache.size ()) - { - adjustModelText = false; + for (int i = 0; i < teammateCount; ++i) { + int characterPos = (i % teammateTopLineCount); + if (characterPos < + (int) characterMenuScreenPositionListCache.size()) { + adjustModelText = false; - int xPos = characterMenuScreenPositionListCache[characterPos].x; - if (i == 7 - && characterPos + 1 < - (int) characterMenuScreenPositionListCache.size ()) - { - xPos += - ((characterMenuScreenPositionListCache[characterPos + 1].x - - characterMenuScreenPositionListCache[characterPos].x) / - 2); - } - else if (i == 8 - && characterPos + 1 < - (int) characterMenuScreenPositionListCache.size ()) - { - xPos = - characterMenuScreenPositionListCache[characterPos + 1].x; - } + int xPos = characterMenuScreenPositionListCache[characterPos].x; + if (i == 7 + && characterPos + 1 < + (int) characterMenuScreenPositionListCache.size()) { + xPos += + ((characterMenuScreenPositionListCache[characterPos + 1].x - + characterMenuScreenPositionListCache[characterPos].x) / + 2); + } else if (i == 8 + && characterPos + 1 < + (int) characterMenuScreenPositionListCache.size()) { + xPos = + characterMenuScreenPositionListCache[characterPos + 1].x; + } - FontMetrics *fontMetrics = NULL; - if (Renderer::renderText3DEnabled == false) - { - fontMetrics = labelTeammateName[i].getFont ()->getMetrics (); - } - else - { - fontMetrics = - labelTeammateName[i].getFont3D ()->getMetrics (); - } - int newxPos = - xPos - - (fontMetrics->getTextWidth (labelTeammateName[i].getText ()) / - 2); - if (newxPos != labelTeammateName[i].getX ()) - { - labelTeammateName[i].init (newxPos, - labelTeammateName[i].getY ()); - } + FontMetrics *fontMetrics = NULL; + if (Renderer::renderText3DEnabled == false) { + fontMetrics = labelTeammateName[i].getFont()->getMetrics(); + } else { + fontMetrics = + labelTeammateName[i].getFont3D()->getMetrics(); + } + int newxPos = + xPos - + (fontMetrics->getTextWidth(labelTeammateName[i].getText()) / + 2); + if (newxPos != labelTeammateName[i].getX()) { + labelTeammateName[i].init(newxPos, + labelTeammateName[i].getY()); + } - newxPos = - xPos - - (fontMetrics->getTextWidth (labelTeammateRole[i].getText ()) / - 2); - if (newxPos != labelTeammateRole[i].getX ()) - { - labelTeammateRole[i].init (newxPos, - labelTeammateRole[i].getY ()); - } - } - } - } + newxPos = + xPos - + (fontMetrics->getTextWidth(labelTeammateRole[i].getText()) / + 2); + if (newxPos != labelTeammateRole[i].getX()) { + labelTeammateRole[i].init(newxPos, + labelTeammateRole[i].getY()); + } + } + } + } - for (int i = 0; i < teammateCount; ++i) - { - renderer.renderLabel (&labelTeammateName[i]); - renderer.renderLabel (&labelTeammateRole[i]); - } - } + for (int i = 0; i < teammateCount; ++i) { + renderer.renderLabel(&labelTeammateName[i]); + renderer.renderLabel(&labelTeammateRole[i]); + } + } - renderer.renderButton (&buttonReturn); + renderer.renderButton(&buttonReturn); - if (program != NULL) - program->renderProgramMsgBox (); + if (program != NULL) + program->renderProgramMsgBox(); - } + } - void MenuStateAbout::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - } - } + void MenuStateAbout::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + } + } - } + } } //end namespace diff --git a/source/glest_game/menu/menu_state_about.h b/source/glest_game/menu/menu_state_about.h index 8aff74123..884e79737 100644 --- a/source/glest_game/menu/menu_state_about.h +++ b/source/glest_game/menu/menu_state_about.h @@ -20,56 +20,54 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateAbout -// =============================== + // =============================== + // class MenuStateAbout + // =============================== - class MenuStateAbout:public MenuState - { - public: - static const int aboutStringCount1 = 4; - static const int aboutStringCount2 = 3; - static const int teammateCount = 9; - static const int teammateTopLineCount = 5; + class MenuStateAbout :public MenuState { + public: + static const int aboutStringCount1 = 4; + static const int aboutStringCount2 = 3; + static const int teammateCount = 9; + static const int teammateTopLineCount = 5; - private: - GraphicButton buttonReturn; - GraphicLabel labelAdditionalCredits; - GraphicLabel labelAbout1[aboutStringCount1]; - GraphicLabel labelAbout2[aboutStringCount2]; - GraphicLabel labelTeammateName[teammateCount]; - GraphicLabel labelTeammateRole[teammateCount]; + private: + GraphicButton buttonReturn; + GraphicLabel labelAdditionalCredits; + GraphicLabel labelAbout1[aboutStringCount1]; + GraphicLabel labelAbout2[aboutStringCount2]; + GraphicLabel labelTeammateName[teammateCount]; + GraphicLabel labelTeammateRole[teammateCount]; - bool adjustModelText; - string loadAdditionalCredits (); + bool adjustModelText; + string loadAdditionalCredits(); - bool enableCustomModCredits; - Texture2D *customModTexture; - int customModTextureX; - int customModTextureY; - int customModTextureW; - int customModTextureH; - float customModTextureAlpha; + bool enableCustomModCredits; + Texture2D *customModTexture; + int customModTextureX; + int customModTextureY; + int customModTextureW; + int customModTextureH; + float customModTextureAlpha; - GraphicLabel labelCustomModCredits; + GraphicLabel labelCustomModCredits; - public: - MenuStateAbout (Program * program, MainMenu * mainMenu); - virtual ~ MenuStateAbout (); + public: + MenuStateAbout(Program * program, MainMenu * mainMenu); + virtual ~MenuStateAbout(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - virtual void keyDown (SDL_KeyboardEvent key); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + virtual void keyDown(SDL_KeyboardEvent key); - virtual void reloadUI (); - }; + virtual void reloadUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index e6ecc1581..aaddf04b8 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -45,9119 +45,8131 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; using namespace - Shared::CompressionUtil; - -namespace Glest -{ - namespace Game - { - - static const int - MAX_PING_LAG_COUNT = 6; - static const double - REPROMPT_DOWNLOAD_SECONDS = 7; -//static const string ITEM_MISSING = "***missing***"; -// above replaced with Lang::getInstance().getString("DataMissing","",true) - const int - HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS = 2; - static const char * - HEADLESS_SAVED_GAME_FILENAME = "lastHeadlessGameSettings.mgg"; - - const int - mapPreviewTexture_X = 5; - const int - mapPreviewTexture_Y = 185; - const int - mapPreviewTexture_W = 150; - const int - mapPreviewTexture_H = 150; - - struct FormatString - { - void - operator () (string & s) - { - s = formatString (s); - } - }; - -// ===================================================== -// class MenuStateConnectedGame -// ===================================================== - - MenuStateConnectedGame::MenuStateConnectedGame (Program * program, MainMenu * mainMenu, JoinMenu joinMenuInfo, bool openNetworkSlots): - MenuState (program, mainMenu, "connected-game"), - modHttpServerThread (NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - containerName = "ClientConnectedGame"; - switchSetupRequestFlagType |= ssrft_NetworkPlayerName; - updateDataSynchDetailText = false; - launchingNewGame = false; - isfirstSwitchingMapMessage = true; - - this->zoomedMap = false; - this->render_mapPreviewTexture_X = mapPreviewTexture_X; - this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; - this->render_mapPreviewTexture_W = mapPreviewTexture_W; - this->render_mapPreviewTexture_H = mapPreviewTexture_H; - - needToBroadcastServerSettings = false; - broadcastServerSettingsDelayTimer = 0; - lastGameSettingsReceivedCount = 0; - noReceiveTimer = time (NULL) - 100; // old but inititialized ( must be an "old" time ) - - soundConnectionCount = 0; - - this->factionVideo = NULL; - factionVideoSwitchedOffVolume = false; - currentTechName_factionPreview = ""; - currentFactionName_factionPreview = ""; - - ftpClientThread = NULL; - ftpMissingDataType = ftpmsg_MissingNone; - getMissingMapFromFTPServer = ""; - getMissingMapFromFTPServerLastPrompted = 0; - getMissingMapFromFTPServerInProgress = false; - getMissingTilesetFromFTPServer = ""; - getMissingTilesetFromFTPServerLastPrompted = 0; - getMissingTilesetFromFTPServerInProgress = false; - getMissingTechtreeFromFTPServer = ""; - getMissingTechtreeFromFTPServerLastPrompted = 0; - getMissingTechtreeFromFTPServerInProgress = false; - - getInProgressSavedGameFromFTPServer = ""; - getInProgressSavedGameFromFTPServerInProgress = false; - readyToJoinInProgressGame = false; - - lastCheckedCRCTilesetName = ""; - lastCheckedCRCTechtreeName = ""; - lastCheckedCRCMapName = ""; - lastCheckedCRCTilesetValue = 0; - lastCheckedCRCTechtreeValue = 0; - lastCheckedCRCMapValue = 0; - - mapPreviewTexture = NULL; - currentFactionLogo = ""; - factionTexture = NULL; - lastMissingMap = ""; - lastMissingTechtree = ""; - lastMissingTileSet = ""; - - activeInputLabel = NULL; - lastNetworkSendPing = 0; - pingCount = 0; - needToSetChangedGameSettings = false; - lastSetChangedGameSettings = time (NULL); - showFullConsole = false; - - - currentFactionName = ""; - currentMap = ""; - settingsReceivedFromServer = false; - initialSettingsReceivedFromServer = false; - - validOriginalGameSettings = false; - validDisplayedGamesettings = false; - - returnMenuInfo = joinMenuInfo; - Lang & lang = Lang::getInstance (); - - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Ok")); - mainMessageBox.setEnabled (false); - - ftpMessageBox.registerGraphicComponent (containerName, "ftpMessageBox"); - ftpMessageBox.init (lang.getString ("ModCenter"), - lang.getString ("GameHost")); - ftpMessageBox.addButton (lang.getString ("NoDownload")); - ftpMessageBox.setEnabled (false); - - NetworkManager & networkManager = NetworkManager::getInstance (); - Config & config = Config::getInstance (); - defaultPlayerName = - config.getString ("NetPlayerName", Socket::getHostName ().c_str ()); - enableFactionTexturePreview = config.getBool ("FactionPreview", "true"); - enableMapPreview = config.getBool ("MapPreview", "true"); - - enableScenarioTexturePreview = - Config::getInstance ().getBool ("EnableScenarioTexturePreview", - "true"); - scenarioLogoTexture = NULL; - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - this->dirList = Config::getInstance ().getPathListForType (ptScenarios); - - vector < - string > - techtreesList = Config::getInstance ().getPathListForType (ptTechs); - techTree.reset (new TechTree (techtreesList)); - - vector < - string > - teamItems, controlItems, results, rMultiplier, playerStatuses; - - // Some of these values must also be changed to match those in - // menu_state_custom_game.cpp - int - labelOffset = 23; - int - setupPos = 605; - // mapHeadPos is the placement of the text "map", not the map itself - int mapHeadPos = 310; - int - mapPos = mapHeadPos - labelOffset; - int - aHeadPos = mapHeadPos - 90; - int - aPos = aHeadPos - labelOffset; - int - networkHeadPos = 700; - -//state - labelStatus.registerGraphicComponent (containerName, "labelStatus"); - labelStatus.init (30, networkHeadPos); - labelStatus.setText (""); - - labelInfo.registerGraphicComponent (containerName, "labelInfo"); - labelInfo.init (30, networkHeadPos + 30); - labelInfo.setText (""); - labelInfo.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelInfo.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - - labelWaitingForPlayers.registerGraphicComponent (containerName, - "labelInfo"); - labelWaitingForPlayers.init (30, 100); - labelWaitingForPlayers.setText (""); - labelWaitingForPlayers.setFont (CoreData::getInstance (). - getMenuFontBig ()); - labelWaitingForPlayers.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - labelWaitingForPlayers.setTextColor (Vec3f (1.0f, 1.0f, 0.f)); - - timerLabelFlash = time (NULL); - labelDataSynchInfo.registerGraphicComponent (containerName, - "labelDataSynchInfo"); - labelDataSynchInfo.init (30, networkHeadPos - 60); - labelDataSynchInfo.setText (""); - labelDataSynchInfo.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelDataSynchInfo.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - int - xoffset = 90; - labelAllowTeamUnitSharing.registerGraphicComponent (containerName, - "labelAllowTeamUnitSharing"); - labelAllowTeamUnitSharing.init (xoffset + 410, 670, 80); - labelAllowTeamUnitSharing.setText (lang. - getString ("AllowTeamUnitSharing")); - labelAllowTeamUnitSharing.setVisible (true); - - checkBoxAllowTeamUnitSharing.registerGraphicComponent (containerName, - "checkBoxAllowTeamUnitSharing"); - checkBoxAllowTeamUnitSharing.init (xoffset + 612, 670); - checkBoxAllowTeamUnitSharing.setValue (false); - checkBoxAllowTeamUnitSharing.setVisible (true); - checkBoxAllowTeamUnitSharing.setEditable (false); - - labelAllowTeamResourceSharing.registerGraphicComponent (containerName, - "labelAllowTeamResourceSharing"); - labelAllowTeamResourceSharing.init (xoffset + 410, 640, 80); - labelAllowTeamResourceSharing.setText (lang. - getString - ("AllowTeamResourceSharing")); - labelAllowTeamResourceSharing.setVisible (true); - - checkBoxAllowTeamResourceSharing.registerGraphicComponent - (containerName, "checkBoxAllowTeamResourceSharing"); - checkBoxAllowTeamResourceSharing.init (xoffset + 612, 640); - checkBoxAllowTeamResourceSharing.setValue (false); - checkBoxAllowTeamResourceSharing.setVisible (true); - checkBoxAllowTeamResourceSharing.setEditable (false); - -// fog - o - war - xoffset = 65; - labelFogOfWar.registerGraphicComponent (containerName, "labelFogOfWar"); - labelFogOfWar.init (xoffset + 100, aHeadPos, 165); - labelFogOfWar.setText (lang.getString ("FogOfWar")); - - listBoxFogOfWar.registerGraphicComponent (containerName, - "listBoxFogOfWar"); - listBoxFogOfWar.init (xoffset + 100, aPos, 165); - listBoxFogOfWar.pushBackItem (lang.getString ("Enabled")); - listBoxFogOfWar.pushBackItem (lang.getString ("Explored")); - listBoxFogOfWar.pushBackItem (lang.getString ("Disabled")); - listBoxFogOfWar.setSelectedItemIndex (0); - listBoxFogOfWar.setEditable (false); - - labelAllowObservers.registerGraphicComponent (containerName, - "labelAllowObservers"); - labelAllowObservers.init (xoffset + 325, aHeadPos, 80); - labelAllowObservers.setText (lang.getString ("AllowObservers")); - - checkBoxAllowObservers.registerGraphicComponent (containerName, - "checkBoxAllowObservers"); - checkBoxAllowObservers.init (xoffset + 325, aPos); - checkBoxAllowObservers.setValue (checkBoxAllowObservers.getValue ()); - - checkBoxAllowObservers.setEditable (false); - - for (int i = 0; i < 45; ++i) - { - rMultiplier.push_back (floatToStr (0.5f + 0.1f * i, 1)); - } - - labelFallbackCpuMultiplier.registerGraphicComponent (containerName, - "labelFallbackCpuMultiplier"); - labelFallbackCpuMultiplier.init (xoffset + 500, aHeadPos, 80); - labelFallbackCpuMultiplier.setText (lang.getString - ("FallbackCpuMultiplier")); - - listBoxFallbackCpuMultiplier.registerGraphicComponent (containerName, - "listBoxFallbackCpuMultiplier"); - listBoxFallbackCpuMultiplier.init (xoffset + 500, aPos, 80); - listBoxFallbackCpuMultiplier.setItems (rMultiplier); - listBoxFallbackCpuMultiplier.setSelectedItem ("1.0"); - - -// Allow Switch Team Mode - labelEnableSwitchTeamMode.registerGraphicComponent (containerName, - "labelEnableSwitchTeamMode"); - labelEnableSwitchTeamMode.init (xoffset + 325, aHeadPos + 45, 80); - labelEnableSwitchTeamMode. - setText (lang.getString ("EnableSwitchTeamMode")); - - checkBoxEnableSwitchTeamMode.registerGraphicComponent (containerName, - "checkBoxEnableSwitchTeamMode"); - checkBoxEnableSwitchTeamMode.init (xoffset + 325, aPos + 45); - checkBoxEnableSwitchTeamMode.setValue (false); - checkBoxEnableSwitchTeamMode.setEditable (false); - - labelAISwitchTeamAcceptPercent.registerGraphicComponent (containerName, - "labelAISwitchTeamAcceptPercent"); - labelAISwitchTeamAcceptPercent.init (xoffset + 500, aHeadPos + 45, 80); - labelAISwitchTeamAcceptPercent.setText (lang.getString - ("AISwitchTeamAcceptPercent")); - - listBoxAISwitchTeamAcceptPercent.registerGraphicComponent - (containerName, "listBoxAISwitchTeamAcceptPercent"); - listBoxAISwitchTeamAcceptPercent.init (xoffset + 500, aPos + 45, 80); - for (int i = 0; i <= 100; i = i + 10) - { - listBoxAISwitchTeamAcceptPercent.pushBackItem (intToStr (i)); - } - listBoxAISwitchTeamAcceptPercent.setSelectedItem (intToStr (30)); - listBoxAISwitchTeamAcceptPercent.setEditable (false); - -//create - buttonCancelDownloads.registerGraphicComponent (containerName, - "buttonCancelDownloads"); - buttonCancelDownloads.init (xoffset + 620, 180, 150); - buttonCancelDownloads.setText (lang.getString ("CancelDownloads")); - -// Network Frame Period - xoffset = 65; -//map listBox - - xoffset = 65; -// MapFilter - labelMapFilter.registerGraphicComponent (containerName, - "labelMapFilter"); - labelMapFilter.init (xoffset + 325, mapHeadPos); - labelMapFilter.setText (lang.getString ("MapFilter")); - - listBoxMapFilter.registerGraphicComponent (containerName, - "listBoxMapFilter"); - listBoxMapFilter.init (xoffset + 325, mapPos, 80); - listBoxMapFilter.pushBackItem ("-"); - for (int i = 1; i < GameConstants::maxPlayers + 1; ++i) - { - listBoxMapFilter.pushBackItem (intToStr (i)); - } - listBoxMapFilter.setSelectedItemIndex (0); - listBoxMapFilter.setEditable (false); - - -// put them all in a set, to weed out duplicates (gbm & mgm with same name) -// will also ensure they are alphabetically listed (rather than how the OS provides them) - listBoxMap.registerGraphicComponent (containerName, "listBoxMap"); - listBoxMap.init (xoffset + 100, mapPos, 220); - listBoxMap.setEditable (false); - - labelMapInfo.registerGraphicComponent (containerName, "labelMapInfo"); - labelMapInfo.init (xoffset + 100, mapPos - labelOffset - 10, 200, 40); - labelMapInfo.setText ("?"); - - labelMap.registerGraphicComponent (containerName, "labelMap"); - labelMap.init (xoffset + 100, mapHeadPos); - labelMap.setText (lang.getString ("Map")); - -//tileset listBox - listBoxTileset.registerGraphicComponent (containerName, - "listBoxTileset"); - listBoxTileset.init (xoffset + 500, mapPos, 160); - listBoxTileset.setEditable (false); - - labelTileset.registerGraphicComponent (containerName, "labelTileset"); - labelTileset.init (xoffset + 500, mapHeadPos); - labelTileset.setText (lang.getString ("Tileset")); - - -//tech Tree listBox - listBoxTechTree.setEditable (false); - - listBoxTechTree.registerGraphicComponent (containerName, - "listBoxTechTree"); - listBoxTechTree.init (xoffset + 700, mapPos, 180); - - labelTechTree.registerGraphicComponent (containerName, "labelTechTree"); - labelTechTree.init (xoffset + 700, mapHeadPos); - labelTechTree.setText (lang.getString ("TechTree")); - - labelAllowNativeLanguageTechtree.registerGraphicComponent - (containerName, "labelAllowNativeLanguageTechtree"); - labelAllowNativeLanguageTechtree.init (xoffset + 700, aHeadPos + 45); - labelAllowNativeLanguageTechtree.setText (lang.getString - ("AllowNativeLanguageTechtree")); - - checkBoxAllowNativeLanguageTechtree.registerGraphicComponent - (containerName, "checkBoxAllowNativeLanguageTechtree"); - checkBoxAllowNativeLanguageTechtree.init (xoffset + 700, aPos + 45); - checkBoxAllowNativeLanguageTechtree.setValue (false); - checkBoxAllowNativeLanguageTechtree.setEditable (false); - checkBoxAllowNativeLanguageTechtree.setEnabled (false); - -// Network Scenario - int - scenarioX = xoffset + 700; - int - scenarioY = aPos; - labelScenario.registerGraphicComponent (containerName, "labelScenario"); - labelScenario.init (scenarioX, aHeadPos); - labelScenario.setText (lang.getString ("Scenario")); - listBoxScenario.registerGraphicComponent (containerName, - "listBoxScenario"); - listBoxScenario.init (scenarioX + 30, scenarioY, 190); - listBoxScenario.setEditable (false); - listBoxScenario.setEnabled (false); - checkBoxScenario.registerGraphicComponent (containerName, - "checkBoxScenario"); - checkBoxScenario.init (scenarioX, scenarioY); - checkBoxScenario.setValue (false); - checkBoxScenario.setEditable (false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - xoffset = 5; - int - rowHeight = 27; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - labelPlayers[i].registerGraphicComponent (containerName, - "labelPlayers" + - intToStr (i)); - labelPlayers[i].init (xoffset - 1, setupPos - 30 - i * rowHeight + 2); - labelPlayers[i]. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - labelPlayers[i]. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - labelPlayers[i].setEditable (false); - - labelPlayerStatus[i].registerGraphicComponent (containerName, - "labelPlayerStatus" + - intToStr (i)); - labelPlayerStatus[i].init (xoffset + 14, - setupPos - 30 - i * rowHeight + 2); - labelPlayerNames[i].registerGraphicComponent (containerName, - "labelPlayerNames" + - intToStr (i)); - labelPlayerNames[i].init (xoffset + 30, - setupPos - 30 - i * rowHeight); - - listBoxControls[i].registerGraphicComponent (containerName, - "listBoxControls" + - intToStr (i)); - listBoxControls[i].init (xoffset + 160, setupPos - 30 - i * rowHeight, - 174); - listBoxControls[i].setEditable (false); - - listBoxRMultiplier[i].registerGraphicComponent (containerName, - "listBoxRMultiplier" + - intToStr (i)); - listBoxRMultiplier[i].init (xoffset + 336, - setupPos - 30 - i * rowHeight, 70); - listBoxRMultiplier[i].setEditable (false); - - listBoxFactions[i].registerGraphicComponent (containerName, - "listBoxFactions" + - intToStr (i)); - listBoxFactions[i].init (xoffset + 411, setupPos - 30 - i * rowHeight, - 247); - listBoxFactions[i].setLeftControlled (true); - listBoxFactions[i].setEditable (false); - - listBoxTeams[i].registerGraphicComponent (containerName, - "listBoxTeams" + - intToStr (i)); - listBoxTeams[i].init (xoffset + 660, setupPos - 30 - i * rowHeight, - 60); - listBoxTeams[i].setEditable (false); - listBoxTeams[i].setLighted (true); - - labelNetStatus[i].registerGraphicComponent (containerName, - "labelNetStatus" + - intToStr (i)); - labelNetStatus[i].init (xoffset + 723, setupPos - 30 - i * rowHeight, - 60); - labelNetStatus[i]. - setFont (CoreData::getInstance ().getDisplayFontSmall ()); - labelNetStatus[i]. - setFont3D (CoreData::getInstance ().getDisplayFontSmall3D ()); - - grabSlotButton[i].registerGraphicComponent (containerName, - "grabSlotButton" + - intToStr (i)); - grabSlotButton[i].init (xoffset + 726, setupPos - 30 - i * rowHeight, - 35, rowHeight - 5); - grabSlotButton[i].setText (">"); - } - - labelControl.registerGraphicComponent (containerName, "labelControl"); - labelControl.init (xoffset + 160, setupPos, 50, GraphicListBox::defH, - true); - labelControl.setText (lang.getString ("Control")); - - labelRMultiplier.registerGraphicComponent (containerName, - "labelRMultiplier"); - labelRMultiplier.init (xoffset + 310, setupPos, 50, - GraphicListBox::defH, true); - - labelFaction.registerGraphicComponent (containerName, "labelFaction"); - labelFaction.init (xoffset + 411, setupPos, 50, GraphicListBox::defH, - true); - labelFaction.setText (lang.getString ("Faction")); - - labelTeam.registerGraphicComponent (containerName, "labelTeam"); - labelTeam.init (xoffset + 660, setupPos, 50, GraphicListBox::defH, - true); - labelTeam.setText (lang.getString ("Team")); - - labelControl.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelControl.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelFaction.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelFaction.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTeam.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTeam.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - -//texts - buttonDisconnect.setText (lang.getString ("Return")); - - controlItems.push_back (lang.getString ("Closed")); - controlItems.push_back (lang.getString ("CpuEasy")); - controlItems.push_back (lang.getString ("Cpu")); - controlItems.push_back (lang.getString ("CpuUltra")); - controlItems.push_back (lang.getString ("CpuZeta")); - controlItems.push_back (lang.getString ("Network")); - controlItems.push_back (lang.getString ("NetworkUnassigned")); - controlItems.push_back (lang.getString ("Human")); - - - if (config.getBool ("EnableNetworkCpu", "false") == true) - { - controlItems.push_back (lang.getString ("NetworkCpuEasy")); - controlItems.push_back (lang.getString ("NetworkCpu")); - controlItems.push_back (lang.getString ("NetworkCpuUltra")); - controlItems.push_back (lang.getString ("NetworkCpuZeta")); - } - - for (int i = 1; i <= GameConstants::maxPlayers; ++i) - { - teamItems.push_back (intToStr (i)); - } - for (int i = GameConstants::maxPlayers + 1; - i <= GameConstants::maxPlayers + GameConstants::specialFactions; - ++i) - { - teamItems.push_back (intToStr (i)); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - labelPlayerStatus[i].setText (""); - labelPlayerStatus[i].setTexture (NULL); - labelPlayerStatus[i].setH (16); - labelPlayerStatus[i].setW (12); - - labelPlayers[i].setText (intToStr (i + 1)); - labelPlayerNames[i].setText (""); - labelPlayerNames[i].setMaxEditWidth (16); - labelPlayerNames[i].setMaxEditRenderWidth (127); - - listBoxTeams[i].setItems (teamItems); - listBoxTeams[i].setSelectedItemIndex (i); - listBoxControls[i].setItems (controlItems); - listBoxRMultiplier[i].setItems (rMultiplier); - listBoxRMultiplier[i].setSelectedItem ("1.0"); - - labelNetStatus[i].setText ("V"); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//init controllers - listBoxControls[0].setSelectedItemIndex (ctHuman); - - -//map listBox -// put them all in a set, to weed out duplicates (gbm & mgm with same name) -// will also ensure they are alphabetically listed (rather than how the OS provides them) - setupMapList (""); - listBoxMap.setItems (formattedPlayerSortedMaps[0]); - - int - buttonx = 165; - int - buttony = 150; - - listBoxPlayerStatus.registerGraphicComponent (containerName, - "listBoxPlayerStatus"); - listBoxPlayerStatus.init (buttonx, buttony, 165); - listBoxPlayerStatus.setTextColor (Vec3f (1.0f, 0.f, 0.f)); - listBoxPlayerStatus.setLighted (true); - playerStatuses.push_back (lang.getString ("PlayerStatusSetup")); - playerStatuses.push_back (lang.getString ("PlayerStatusBeRightBack")); - playerStatuses.push_back (lang.getString ("PlayerStatusReady")); - listBoxPlayerStatus.setItems (playerStatuses); - buttonx += 180; - - buttonDisconnect.registerGraphicComponent (containerName, - "buttonDisconnect"); - buttonDisconnect.init (buttonx, buttony, 125); - buttonx += 132; - - buttonRestoreLastSettings.registerGraphicComponent (containerName, - "buttonRestoreLastSettings"); - buttonRestoreLastSettings.init (buttonx, buttony, 240); - buttonRestoreLastSettings.setText (lang.getString - ("ReloadLastGameSettings")); - buttonx += 247; - - buttonPlayNow.registerGraphicComponent (containerName, "buttonPlayNow"); - buttonPlayNow.init (buttonx, buttony, 125); - buttonPlayNow.setText (lang.getString ("PlayNow")); - buttonPlayNow.setVisible (false); - - -// write hint to console: - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - console.addLine (lang.getString ("ToSwitchOffMusicPress") + " - \"" + - configKeys.getString ("ToggleMusic") + "\""); - chatManager.init (&console, -1, true); - - GraphicComponent::applyAllCustomProperties (containerName); - -//tileset listBox - setupTilesetList (""); - - int - initialTechSelection = setupTechList ("", true); - listBoxTechTree.setSelectedItemIndex (initialTechSelection); - - -//scenario listbox - vector < string > resultsScenarios; - findDirs (dirList, resultsScenarios); -// Filter out only scenarios with no network slots - for (int i = 0; i < (int) resultsScenarios.size (); ++i) - { - string scenario = resultsScenarios[i]; - string file = Scenario::getScenarioPath (dirList, scenario); - - try - { - if (file != "") - { - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, &scenarioInfo, isTutorial); - - bool isNetworkScenario = false; - for (unsigned int j = 0; - isNetworkScenario == false - && j < (unsigned int) GameConstants::maxPlayers; ++j) - { - if (scenarioInfo.factionControls[j] == ctNetwork) - { - isNetworkScenario = true; - } - } - if (isNetworkScenario == true) - { - scenarioFiles.push_back (scenario); - } - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d]\nError loading scenario [%s]:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenario.c_str (), ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showMessageBox (szBuf, "Error", false); -//throw megaglest_runtime_error(szBuf); - } - } - resultsScenarios.clear (); - for (int i = 0; i < (int) scenarioFiles.size (); ++i) - { - resultsScenarios.push_back (formatString (scenarioFiles[i])); - } - listBoxScenario.setItems (resultsScenarios); - checkBoxScenario.setEnabled (false); - - if (config.getBool ("EnableFTPXfer", "true") == true) - { - ClientInterface * - clientInterface = networkManager.getClientInterface (); - string serverUrl = clientInterface->getServerIpAddress (); - int - portNumber = clientInterface->getServerFTPPort (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] Using FTP port #: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, portNumber); - - vector < string > mapPathList = config.getPathListForType (ptMaps); - std::pair < string, string > mapsPath; - if (mapPathList.empty () == false) - { - mapsPath.first = mapPathList[0]; - } - if (mapPathList.size () > 1) - { - mapsPath.second = mapPathList[1]; - } - std::pair < string, string > tilesetsPath; - vector < string > tilesetsList = - Config::getInstance ().getPathListForType (ptTilesets); - if (tilesetsList.empty () == false) - { - tilesetsPath.first = tilesetsList[0]; - if (tilesetsList.size () > 1) - { - tilesetsPath.second = tilesetsList[1]; - } - } - - std::pair < string, string > techtreesPath; - if (techtreesList.empty () == false) - { - techtreesPath.first = techtreesList[0]; - if (techtreesList.size () > 1) - { - techtreesPath.second = techtreesList[1]; - } - } - - std::pair < string, string > scenariosPath; - vector < string > scenariosList = - Config::getInstance ().getPathListForType (ptScenarios); - if (scenariosList.empty () == false) - { - scenariosPath.first = scenariosList[0]; - if (scenariosList.size () > 1) - { - scenariosPath.second = scenariosList[1]; - } - } - - string - fileArchiveExtension = - config.getString ("FileArchiveExtension", ""); - string - fileArchiveExtractCommand = - config.getString ("FileArchiveExtractCommand", ""); - string - fileArchiveExtractCommandParameters = - config.getString ("FileArchiveExtractCommandParameters", ""); - int32 - fileArchiveExtractCommandSuccessResult = - config.getInt ("FileArchiveExtractCommandSuccessResult", "0"); - -// Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - tempFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Temp files path [%s]\n", tempFilePath.c_str ()); - - ftpClientThread = new FTPClientThread (portNumber, serverUrl, - mapsPath, tilesetsPath, - techtreesPath, scenariosPath, - this, fileArchiveExtension, - fileArchiveExtractCommand, - fileArchiveExtractCommandParameters, - fileArchiveExtractCommandSuccessResult, - tempFilePath); - ftpClientThread->start (); - } -// Start http meta data thread - static - string - mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - modHttpServerThread = new SimpleTaskThread (this, 0, 200); - modHttpServerThread->setUniqueID (mutexOwnerId); - modHttpServerThread->start (); - - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL - && clientInterface->getJoinGameInProgress () == true) - { - listBoxPlayerStatus.setVisible (false); - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("JoinPlayerToCurrentGameWelcome", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("JoinPlayerToCurrentGameWelcome", - languageList[i]).c_str (), - getHumanPlayerName ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s has connected to the game and would like to join.", - getHumanPlayerName ().c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - sleep (1); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - // I moved this block from loadMapInfo(), and modified it. It was - // setting the slots visible based on the number of hardMaxPlayers - // every time a new map was loaded. Trying it here instead, so the - // labels are made visible only once. Below, we'll be disabling slots - // that exceed hardMaxPlayers - for (int i = 0; i < GameConstants::maxPlayers; i++) - { - labelPlayers[i].setVisible (true); - labelPlayerNames[i].setVisible (true); - listBoxControls[i].setVisible (true); - listBoxFactions[i].setVisible (true); - listBoxTeams[i].setVisible (true); - labelNetStatus[i].setVisible (true); - } - } - - void - MenuStateConnectedGame::reloadUI () - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - console.resetFonts (); - mainMessageBox.init (lang.getString ("Ok")); - ftpMessageBox.init (lang.getString ("ModCenter"), - lang.getString ("GameHost")); - ftpMessageBox.addButton (lang.getString ("NoDownload")); - - labelInfo.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelInfo.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - - labelWaitingForPlayers. - setFont (CoreData::getInstance ().getMenuFontBig ()); - labelWaitingForPlayers. - setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - - labelDataSynchInfo.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelDataSynchInfo. - setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - - buttonCancelDownloads.setText (lang.getString ("CancelDownloads")); - - labelFogOfWar.setText (lang.getString ("FogOfWar")); - - vector < string > fowItems; - fowItems.push_back (lang.getString ("Enabled")); - fowItems.push_back (lang.getString ("Explored")); - fowItems.push_back (lang.getString ("Disabled")); - listBoxFogOfWar.setItems (fowItems); - - labelAllowObservers.setText (lang.getString ("AllowObservers")); - labelFallbackCpuMultiplier.setText (lang.getString - ("FallbackCpuMultiplier")); - - labelEnableSwitchTeamMode. - setText (lang.getString ("EnableSwitchTeamMode")); - - labelAllowTeamUnitSharing. - setText (lang.getString ("AllowTeamUnitSharing")); - labelAllowTeamResourceSharing. - setText (lang.getString ("AllowTeamResourceSharing")); - - labelAISwitchTeamAcceptPercent.setText (lang.getString - ("AISwitchTeamAcceptPercent")); - - vector < string > aiswitchteamModeItems; - for (int i = 0; i <= 100; i = i + 10) - { - aiswitchteamModeItems.push_back (intToStr (i)); - } - listBoxAISwitchTeamAcceptPercent.setItems (aiswitchteamModeItems); - - vector < string > rMultiplier; - for (int i = 0; i < 45; ++i) - { - rMultiplier.push_back (floatToStr (0.5f + 0.1f * i, 1)); - } - listBoxFallbackCpuMultiplier.setItems (rMultiplier); - - labelMap.setText (lang.getString ("Map")); - - labelMapFilter.setText (lang.getString ("MapFilter")); - - labelTileset.setText (lang.getString ("Tileset")); - - labelTechTree.setText (lang.getString ("TechTree")); - - vector < string > playerstatusItems; - playerstatusItems.push_back (lang.getString ("PlayerStatusSetup")); - playerstatusItems. - push_back (lang.getString ("PlayerStatusBeRightBack")); - playerstatusItems.push_back (lang.getString ("PlayerStatusReady")); - listBoxPlayerStatus.setItems (playerstatusItems); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - labelControl.setText (lang.getString ("Control")); - - labelFaction.setText (lang.getString ("Faction")); - - labelTeam.setText (lang.getString ("Team")); - - labelControl.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelControl.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelFaction.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelFaction.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTeam.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTeam.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - -//texts - buttonDisconnect.setText (lang.getString ("Return")); - - vector < string > controlItems; - controlItems.push_back (lang.getString ("Closed")); - controlItems.push_back (lang.getString ("CpuEasy")); - controlItems.push_back (lang.getString ("Cpu")); - controlItems.push_back (lang.getString ("CpuUltra")); - controlItems.push_back (lang.getString ("CpuZeta")); - controlItems.push_back (lang.getString ("Network")); - controlItems.push_back (lang.getString ("NetworkUnassigned")); - controlItems.push_back (lang.getString ("Human")); - - if (config.getBool ("EnableNetworkCpu", "false") == true) - { - controlItems.push_back (lang.getString ("NetworkCpuEasy")); - controlItems.push_back (lang.getString ("NetworkCpu")); - controlItems.push_back (lang.getString ("NetworkCpuUltra")); - controlItems.push_back (lang.getString ("NetworkCpuZeta")); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - labelPlayers[i].setText (intToStr (i + 1)); - listBoxControls[i].setItems (controlItems); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - labelScenario.setText (lang.getString ("Scenario")); - - labelAllowNativeLanguageTechtree.setText (lang.getString - ("AllowNativeLanguageTechtree")); - - buttonPlayNow.setText (lang.getString ("PlayNow")); - buttonRestoreLastSettings.setText (lang.getString - ("ReloadLastGameSettings")); - - chatManager.init (&console, -1, true); - - GraphicComponent::reloadFontsForRegisterGraphicComponents - (containerName); - } - - void - MenuStateConnectedGame::disconnectFromServer () - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (false); - if (clientInterface != NULL) - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - soundRenderer.playFx (coreData.getClickSoundA ()); - if (clientInterface->getSocket () != NULL) - { - if (clientInterface->isConnected () == true) - { - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - string sQuitText = lang.getString ("QuitGame", languageList[i]); - clientInterface->sendTextMessage (sQuitText, -1, false, - languageList[i]); - } - sleep (1); - - } - clientInterface->close (); - } - clientInterface->reset (); - } - currentFactionName = ""; - currentMap = ""; - } - - MenuStateConnectedGame::~MenuStateConnectedGame () - { - if (launchingNewGame == false) - { - disconnectFromServer (); - NetworkManager & networkManager = NetworkManager::getInstance (); - networkManager.end (); - } - - if (modHttpServerThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - modHttpServerThread->setSimpleTaskInterfaceValid (false); - modHttpServerThread->signalQuit (); - modHttpServerThread->setThreadOwnerValid (false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (modHttpServerThread->canShutdown (true) == true - && modHttpServerThread->shutdownAndWait () == true) - { - delete modHttpServerThread; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - modHttpServerThread = NULL; - } - - if (ftpClientThread != NULL) - { - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - } - - ftpClientThread = NULL; - } - - cleanupMapPreviewTexture (); - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - } - - string MenuStateConnectedGame::refreshTilesetModInfo (string tilesetInfo) - { - std::vector < std::string > tilesetInfoList; - Tokenize (tilesetInfo, tilesetInfoList, "|"); - if (tilesetInfoList.size () >= 5) - { - Config & config = Config::getInstance (); - ModInfo modinfo; - modinfo.name = tilesetInfoList[0]; - modinfo.crc = tilesetInfoList[1]; - modinfo.description = tilesetInfoList[2]; - modinfo.url = tilesetInfoList[3]; - modinfo.imageUrl = tilesetInfoList[4]; - modinfo.type = mt_Tileset; - - string - itemPath = - config.getPathListForType (ptTilesets, - "")[1] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - bool - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - uint32 - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", NULL, - forceRefresh); - if (crc == 0) - { - itemPath = - config.getPathListForType (ptTilesets, - "")[0] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, forceRefresh); - } - } - modinfo.localCRC = uIntToStr (crc); -//printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - -//printf("#1 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); - } - else - { - modinfo.localCRC = ""; - -//printf("#2 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); - } - - tilesetCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - string MenuStateConnectedGame::refreshTechModInfo (string techInfo) - { - std::vector < std::string > techInfoList; - Tokenize (techInfo, techInfoList, "|"); - if (techInfoList.size () >= 6) - { - Config & config = Config::getInstance (); - ModInfo modinfo; - modinfo.name = techInfoList[0]; - modinfo.count = techInfoList[1]; - modinfo.crc = techInfoList[2]; - modinfo.description = techInfoList[3]; - modinfo.url = techInfoList[4]; - modinfo.imageUrl = techInfoList[5]; - modinfo.type = mt_Techtree; - - string - itemPath = - config.getPathListForType (ptTechs, - "")[1] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - bool - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - uint32 - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", NULL, - forceRefresh); - if (crc == 0) - { - itemPath = - config.getPathListForType (ptTechs, - "")[0] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, forceRefresh); - } - } - modinfo.localCRC = uIntToStr (crc); -//printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - modinfo.localCRC = ""; - } - techCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - string MenuStateConnectedGame::getMapCRC (string mapName) - { - Config & config = Config::getInstance (); - vector < string > mappaths = config.getPathListForType (ptMaps, ""); - string result = ""; - if (mappaths.empty () == false) - { - Checksum checksum; - string itemPath = mappaths[1] + "/" + mapName; - if (fileExists (itemPath)) - { - checksum.addFile (itemPath); - uint32 crc = checksum.getSum (); - result = uIntToStr (crc); -//printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - itemPath = mappaths[0] + "/" + mapName; - if (fileExists (itemPath)) - { - checksum.addFile (itemPath); - uint32 crc = checksum.getSum (); - result = uIntToStr (crc); -//printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - result = ""; - } - } - } - else - { - result = ""; - } - return result; - } - - string MenuStateConnectedGame::refreshMapModInfo (string mapInfo) - { - std::vector < std::string > mapInfoList; - Tokenize (mapInfo, mapInfoList, "|"); - if (mapInfoList.size () >= 6) - { -//Config &config = Config::getInstance(); - ModInfo modinfo; - modinfo.name = mapInfoList[0]; - modinfo.count = mapInfoList[1]; - modinfo.crc = mapInfoList[2]; - modinfo.description = mapInfoList[3]; - modinfo.url = mapInfoList[4]; - modinfo.imageUrl = mapInfoList[5]; - modinfo.type = mt_Map; - modinfo.localCRC = getMapCRC (modinfo.name); - mapCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - void - MenuStateConnectedGame::simpleTask (BaseThread * callingThread, - void *userdata) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - static - string - mutexOwnerId = string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexThreadOwner (callingThread->getMutexThreadOwnerValid (), - mutexOwnerId); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - callingThread->getMutexThreadOwnerValid ()->setOwnerId (mutexOwnerId); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); - - std::string techsMetaData = ""; - std::string tilesetsMetaData = ""; - std::string mapsMetaData = ""; - std::string scenariosMetaData = ""; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (config.getString ("Masterserver", "") != "") - { - string baseURL = config.getString ("Masterserver"); - if (baseURL != "") - { - endPathWithSlash (baseURL, false); - } - string - phpVersionParam = - config.getString ("phpVersionParam", "?version=0.1"); - string - gameVersion = - "&glestVersion=" + SystemFlags::escapeURL (glestVersionString); - string - playerUUID = - "&uuid=" + - SystemFlags::escapeURL (config.getString ("PlayerId", "")); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", - __FILE__, __FUNCTION__, __LINE__, baseURL.c_str ()); - - CURL * - handle = SystemFlags::initHTTP (); - CURLcode curlResult = CURLE_OK; - techsMetaData = - SystemFlags::getHTTP (baseURL + "showTechsForGlest.php" + - phpVersionParam + gameVersion + playerUUID, - handle, -1, &curlResult); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("techsMetaData [%s] curlResult = %d\n", - techsMetaData.c_str (), curlResult); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#1 ") + szBuf, true); - } - - if (curlResult == CURLE_OK || - (curlResult != CURLE_COULDNT_RESOLVE_HOST && - curlResult != CURLE_COULDNT_CONNECT)) - { - - tilesetsMetaData = - SystemFlags::getHTTP (baseURL + "showTilesetsForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("tilesetsMetaData [%s]\n", tilesetsMetaData.c_str ()); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#2 ") + szBuf, true); - } - - mapsMetaData = - SystemFlags::getHTTP (baseURL + "showMapsForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("mapsMetaData [%s]\n", mapsMetaData.c_str ()); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#3 ") + szBuf, true); - } - - scenariosMetaData = - SystemFlags::getHTTP (baseURL + "showScenariosForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("scenariosMetaData [%s]\n", scenariosMetaData.c_str ()); - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#4 ") + szBuf, true); - } - } - SystemFlags::cleanupHTTP (&handle); - } - else - { - console.addLine (lang.getString ("MasterServerMissing"), true); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - MutexSafeWrapper - safeMutex (callingThread->getMutexThreadObjectAccessor (), - string (__FILE__) + "_" + intToStr (__LINE__)); - tilesetListRemote.clear (); - Tokenize (tilesetsMetaData, tilesetListRemote, "\n"); - safeMutex.ReleaseLock (true); - - for (unsigned int i = 0; i < tilesetListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - safeMutex.Lock (); - string result = refreshTilesetModInfo (tilesetListRemote[i]); - safeMutex.ReleaseLock (true); - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - safeMutex.Lock (); - techListRemote.clear (); - Tokenize (techsMetaData, techListRemote, "\n"); - safeMutex.ReleaseLock (true); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - for (unsigned int i = 0; i < techListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - safeMutex.Lock (); - string result = refreshTechModInfo (techListRemote[i]); - safeMutex.ReleaseLock (true); - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - safeMutex.Lock (); - mapListRemote.clear (); - Tokenize (mapsMetaData, mapListRemote, "\n"); - safeMutex.ReleaseLock (true); - - for (unsigned int i = 0; i < mapListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - safeMutex.Lock (); - string result = refreshMapModInfo (mapListRemote[i]); - safeMutex.ReleaseLock (true); - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (modHttpServerThread != NULL) - { - modHttpServerThread->signalQuit (); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - void - MenuStateConnectedGame::mouseClick (int x, int y, MouseButton mouseButton) - { - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - Lang & lang = Lang::getInstance (); - - string advanceToItemStartingWith = ""; - if (mainMessageBox.getEnabled () == false) - { - if (::Shared::Platform::Window::isKeyStateModPressed (KMOD_SHIFT) == - true) - { - wchar_t - lastKey =::Shared::Platform::Window::extractLastKeyPressed (); -//printf("lastKey = %d [%c]\n",lastKey,lastKey); - advanceToItemStartingWith = lastKey; - } - } - - if (mapPreviewTexture != NULL) - { -// printf("X: %d Y: %d [%d, %d, %d, %d]\n", -// x, y, -// this->render_mapPreviewTexture_X, this->render_mapPreviewTexture_X + this->render_mapPreviewTexture_W, -// this->render_mapPreviewTexture_Y, this->render_mapPreviewTexture_Y + this->render_mapPreviewTexture_H); - - if (x >= this->render_mapPreviewTexture_X - && x <= - this->render_mapPreviewTexture_X + - this->render_mapPreviewTexture_W - && y >= this->render_mapPreviewTexture_Y - && y <= - this->render_mapPreviewTexture_Y + - this->render_mapPreviewTexture_H) - { - - if (this->render_mapPreviewTexture_X == mapPreviewTexture_X && - this->render_mapPreviewTexture_Y == mapPreviewTexture_Y && - this->render_mapPreviewTexture_W == mapPreviewTexture_W && - this->render_mapPreviewTexture_H == mapPreviewTexture_H) - { - - const - Metrics & - metrics = Metrics::getInstance (); - - this->render_mapPreviewTexture_X = 0; - this->render_mapPreviewTexture_Y = 0; - this->render_mapPreviewTexture_W = metrics.getVirtualW (); - this->render_mapPreviewTexture_H = metrics.getVirtualH (); - this->zoomedMap = true; - - cleanupMapPreviewTexture (); - } - else - { - this->render_mapPreviewTexture_X = mapPreviewTexture_X; - this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; - this->render_mapPreviewTexture_W = mapPreviewTexture_W; - this->render_mapPreviewTexture_H = mapPreviewTexture_H; - this->zoomedMap = false; - - cleanupMapPreviewTexture (); - } - return; - } - if (this->zoomedMap == true) - { - return; - } - } - - if (mainMessageBox.getEnabled ()) - { - int - button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - mainMessageBox.setEnabled (false); - } - } - } - else if (ftpMessageBox.getEnabled ()) - { - int - button = 0; - if (ftpMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - ftpMessageBox.setEnabled (false); - - if (button == 0 - || (button == 1 && ftpMessageBox.getButtonCount () == 3)) - { - if (ftpMissingDataType == ftpmsg_MissingMap) - { - getMissingMapFromFTPServerInProgress = true; - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingMapNowDownloading", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingMapNowDownloading", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - getMissingMapFromFTPServer.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is attempting to download the map: %s", - getHumanPlayerName ().c_str (), - getMissingMapFromFTPServer.c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - - if (ftpClientThread != NULL) - { - if (button == 0 && ftpMessageBox.getButtonCount () == 3) - { - string mapName = getMissingMapFromFTPServer; - - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - string mapURL = mapCacheList[mapName].url; - safeMutexThread.ReleaseLock (); - - if (ftpClientThread != NULL) - ftpClientThread->addMapToRequests (mapName, mapURL); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingMapFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - else - { - ftpClientThread->addMapToRequests - (getMissingMapFromFTPServer); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingMapFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - } - } - else if (ftpMissingDataType == ftpmsg_MissingTileset) - { - getMissingTilesetFromFTPServerInProgress = true; - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTilesetNowDownloading", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("DataMissingTilesetNowDownloading", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - getMissingTilesetFromFTPServer.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is attempting to download the tileset: %s", - getHumanPlayerName ().c_str (), - getMissingTilesetFromFTPServer.c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - - if (ftpClientThread != NULL) - { - if (button == 0 && ftpMessageBox.getButtonCount () == 3) - { - string tilesetName = getMissingTilesetFromFTPServer; - - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - string tilesetURL = tilesetCacheList[tilesetName].url; - safeMutexThread.ReleaseLock (); - - if (ftpClientThread != NULL) - ftpClientThread->addTilesetToRequests (tilesetName, - tilesetURL); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingTilesetFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - else - { - ftpClientThread->addTilesetToRequests - (getMissingTilesetFromFTPServer); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingTilesetFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - } - } - else if (ftpMissingDataType == ftpmsg_MissingTechtree) - { - getMissingTechtreeFromFTPServerInProgress = true; - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtreeNowDownloading", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("DataMissingTechtreeNowDownloading", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - getMissingTechtreeFromFTPServer.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is attempting to download the techtree: %s", - getHumanPlayerName ().c_str (), - getMissingTechtreeFromFTPServer.c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - - if (ftpClientThread != NULL) - { - if (button == 0 && ftpMessageBox.getButtonCount () == 3) - { - string techName = getMissingTechtreeFromFTPServer; - - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - string techURL = techCacheList[techName].url; - safeMutexThread.ReleaseLock (); - - if (ftpClientThread != NULL) - ftpClientThread->addTechtreeToRequests (techName, - techURL); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingTechtreeFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - else - { - ftpClientThread->addTechtreeToRequests - (getMissingTechtreeFromFTPServer); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList[getMissingTechtreeFromFTPServer] = - pair < int, - string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - } - } - } - } - } - else if (buttonCancelDownloads.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - - if (ftpClientThread != NULL && fileFTPProgressList.empty () == false) - { - - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - } - ftpClientThread = NULL; - - fileFTPProgressList.clear (); - getMissingMapFromFTPServerInProgress = false; - getMissingTilesetFromFTPServerInProgress = false; - getMissingTechtreeFromFTPServerInProgress = false; - getMissingMapFromFTPServer = ""; - getMissingTilesetFromFTPServer = ""; - getMissingTechtreeFromFTPServer = ""; - getMissingMapFromFTPServerLastPrompted = 0; - getMissingTilesetFromFTPServerLastPrompted = 0; - getMissingTechtreeFromFTPServerLastPrompted = 0; - - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface == NULL) - { - throw megaglest_runtime_error ("clientInterface == NULL"); - } - if (getInProgressSavedGameFromFTPServerInProgress == true) - { - if (clientInterface != NULL) - { - clientInterface->close (); - return; - } - } - - getInProgressSavedGameFromFTPServer = ""; - getInProgressSavedGameFromFTPServerInProgress = false; - - string serverUrl = clientInterface->getServerIpAddress (); - int - portNumber = clientInterface->getServerFTPPort (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] Using FTP port #: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, portNumber); - - Config & config = Config::getInstance (); - vector < string > mapPathList = config.getPathListForType (ptMaps); - std::pair < string, string > mapsPath; - if (mapPathList.empty () == false) - { - mapsPath.first = mapPathList[0]; - } - if (mapPathList.size () > 1) - { - mapsPath.second = mapPathList[1]; - } - std::pair < string, string > tilesetsPath; - vector < string > tilesetsList = - Config::getInstance ().getPathListForType (ptTilesets); - if (tilesetsList.empty () == false) - { - tilesetsPath.first = tilesetsList[0]; - if (tilesetsList.size () > 1) - { - tilesetsPath.second = tilesetsList[1]; - } - } - - std::pair < string, string > techtreesPath; - vector < string > techtreesList = - Config::getInstance ().getPathListForType (ptTechs); - if (techtreesList.empty () == false) - { - techtreesPath.first = techtreesList[0]; - if (techtreesList.size () > 1) - { - techtreesPath.second = techtreesList[1]; - } - } - - std::pair < string, string > scenariosPath; - vector < string > scenariosList = - Config::getInstance ().getPathListForType (ptScenarios); - if (scenariosList.empty () == false) - { - scenariosPath.first = scenariosList[0]; - if (scenariosList.size () > 1) - { - scenariosPath.second = scenariosList[1]; - } - } - - string - fileArchiveExtension = - config.getString ("FileArchiveExtension", ""); - string - fileArchiveExtractCommand = - config.getString ("FileArchiveExtractCommand", ""); - string - fileArchiveExtractCommandParameters = - config.getString ("FileArchiveExtractCommandParameters", ""); - int32 - fileArchiveExtractCommandSuccessResult = - config.getInt ("FileArchiveExtractCommandSuccessResult", "0"); - -// Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - tempFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Temp files path [%s]\n", tempFilePath.c_str ()); - - ftpClientThread = new FTPClientThread (portNumber, serverUrl, - mapsPath, tilesetsPath, - techtreesPath, scenariosPath, - this, fileArchiveExtension, - fileArchiveExtractCommand, - fileArchiveExtractCommandParameters, - fileArchiveExtractCommandSuccessResult, - tempFilePath); - ftpClientThread->start (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("CancelDownloadsMsg", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("CancelDownloadsMsg", - languageList[i]).c_str (), - getHumanPlayerName ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s cancelled all file downloads.", - getHumanPlayerName ().c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - } - } - else if (buttonDisconnect.mouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - disconnectFromServer (); - networkManager.end (); - returnToJoinMenu (); - return; - } - - if (initialSettingsReceivedFromServer == false) - { - return; - } - - if (activeInputLabel != NULL - && activeInputLabel->mouseClick (x, y) == false) - { - setActiveInputLabel (NULL); - } - -// Only allow changes after we get game settings from the server - if (clientInterface != NULL && clientInterface->isConnected () == true) - { - int - myCurrentIndex = -1; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { // find my current index by looking at editable listBoxes - if ( //listBoxFactions[i].getEditable() && - clientInterface-> - getGameSettings ()->getStartLocationIndex (clientInterface-> - getGameSettings - ()->getThisFactionIndex - ()) == i) - { - myCurrentIndex = i; - } - } - -//printf("myCurrentIndex = %d thisFactionIndex = %d\n",myCurrentIndex,clientInterface->getGameSettings()->getThisFactionIndex()); - - if (myCurrentIndex != -1) - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (listBoxFactions[i].getEditable () && - clientInterface-> - getGameSettings ()->getStartLocationIndex (clientInterface-> - getGameSettings - ()->getThisFactionIndex - ()) == i) - { - if (listBoxFactions[i].mouseClick - (x, y, advanceToItemStartingWith)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - ClientInterface * - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface->isConnected ()) - { - clientInterface->setGameSettingsReceived (false); - clientInterface-> - sendSwitchSetupRequest (listBoxFactions[i].getSelectedItem - (), i, -1, - listBoxTeams - [i].getSelectedItemIndex (), - getHumanPlayerName (), - getNetworkPlayerStatus (), - switchSetupRequestFlagType, - lang.getLanguage ()); - switchSetupRequestFlagType = ssrft_None; - noReceiveTimer = time (NULL); - } - break; - } - } - if (listBoxTeams[i].getEditable () && - clientInterface-> - getGameSettings ()->getStartLocationIndex (clientInterface-> - getGameSettings - ()->getThisFactionIndex - ()) == i) - { - if (listBoxTeams[i].mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (clientInterface->isConnected ()) - { - clientInterface->setGameSettingsReceived (false); - clientInterface-> - sendSwitchSetupRequest (listBoxFactions[i].getSelectedItem - (), i, -1, - listBoxTeams - [i].getSelectedItemIndex (), - getHumanPlayerName (), - getNetworkPlayerStatus (), - switchSetupRequestFlagType, - lang.getLanguage ()); - switchSetupRequestFlagType = ssrft_None; - } - break; - } - } - - bool canGrabSlot = false; - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL - && clientInterface->getJoinGameInProgress () == true) - { - canGrabSlot = - ((listBoxControls[i].getSelectedItemIndex () == ctNetwork - && labelNetStatus[i].getText () == - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - || (listBoxControls[i].getSelectedItemIndex () != ctHuman - && listBoxControls[i].getSelectedItemIndex () != ctClosed - && listBoxControls[i].getSelectedItemIndex () != - ctNetwork)); - } - else - { - canGrabSlot = - (listBoxControls[i].getSelectedItemIndex () == ctNetwork - && labelNetStatus[i].getText () == - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); - } - - if (canGrabSlot == true) - { - if (clientInterface != NULL && i < mapInfo.players - && grabSlotButton[i].mouseClick (x, y)) - { -//printf("Send slot switch request for slot = %d, myCurrentIndex = %d\n",i,myCurrentIndex); - - soundRenderer.playFx (coreData.getClickSoundB ()); - clientInterface->setGameSettingsReceived (false); - settingsReceivedFromServer = false; - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] sending a switchSlot request from %d to %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - clientInterface->getGameSettings - ()->getThisFactionIndex (), i); - -//printf("Switch slot from %d to %d\n",myCurrentIndex,i); - - string - desiredFactionName = - listBoxFactions[myCurrentIndex].getSelectedItem (); - int - desiredTeamIndex = - listBoxTeams[myCurrentIndex].getSelectedItemIndex (); - if (checkBoxScenario.getValue () == true) - { - desiredFactionName = listBoxFactions[i].getSelectedItem (); - desiredTeamIndex = listBoxTeams[i].getSelectedItemIndex (); - } - -//printf("Sending switch slot request to server...\n"); - - clientInterface->sendSwitchSetupRequest (desiredFactionName, - myCurrentIndex, - i, - desiredTeamIndex, - getHumanPlayerName - (), - getNetworkPlayerStatus - (), - switchSetupRequestFlagType, - lang.getLanguage ()); - labelPlayerNames[myCurrentIndex].setText (""); - labelPlayerNames[i].setText (""); - switchSetupRequestFlagType = ssrft_None; - break; - } - } - - if (labelPlayerNames[i].mouseClick (x, y) - && (activeInputLabel != &labelPlayerNames[i])) - { - if (clientInterface != NULL - && i == clientInterface->getPlayerIndex ()) - { - setActiveInputLabel (&labelPlayerNames[i]); - } - } - } - - if (listBoxPlayerStatus.mouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - soundRenderer.playFx (coreData.getClickSoundC ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (getNetworkPlayerStatus () == npst_PickSettings) - { - listBoxPlayerStatus.setTextColor (Vec3f (1.0f, 0.0f, 0.0f)); - listBoxPlayerStatus.setLighted (true); - } - else if (getNetworkPlayerStatus () == npst_BeRightBack) - { - listBoxPlayerStatus.setTextColor (Vec3f (1.0f, 1.0f, 0.0f)); - listBoxPlayerStatus.setLighted (true); - } - else if (getNetworkPlayerStatus () == npst_Ready) - { - listBoxPlayerStatus.setTextColor (Vec3f (0.0f, 1.0f, 0.0f)); - listBoxPlayerStatus.setLighted (false); - } - - ClientInterface * - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected ()) - { - clientInterface->setGameSettingsReceived (false); - clientInterface->sendSwitchSetupRequest (listBoxFactions - [clientInterface-> - getPlayerIndex ()]. - getSelectedItem (), - clientInterface-> - getPlayerIndex (), -1, - listBoxTeams - [clientInterface->getPlayerIndex - ()].getSelectedItemIndex - (), - getHumanPlayerName (), - getNetworkPlayerStatus - (), - switchSetupRequestFlagType, - lang.getLanguage ()); - switchSetupRequestFlagType = ssrft_None; - } - } - - if (isHeadlessAdmin () == true) - { -//printf("#1 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); - mouseClickAdmin (x, y, mouseButton, advanceToItemStartingWith); - } - else if (clientInterface != NULL - && clientInterface->getJoinGameInProgress () == true) - { - if (buttonPlayNow.mouseClick (x, y) && buttonPlayNow.getEnabled ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - uint32 tilesetCRC = lastCheckedCRCTilesetValue; - uint32 techCRC = lastCheckedCRCTechtreeValue; - uint32 mapCRC = lastCheckedCRCMapValue; - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - bool - dataSynchMismatch = - ((mapCRC != 0 && mapCRC != gameSettings->getMapCRC ()) - || (tilesetCRC != 0 - && tilesetCRC != gameSettings->getTilesetCRC ()) - || (techCRC != 0 && techCRC != gameSettings->getTechCRC ())); - if (dataSynchMismatch == false) - { - PlayNow (true); - return; - } - else - { - showMessageBox - ("You cannot start the game because\none or more clients do not have the same game data!", - "Data Mismatch Error", false); - } - } - } - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - bool MenuStateConnectedGame::isHeadlessAdmin () - { - bool result = false; - - ClientInterface * - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected ()) - { - const GameSettings * - settings = clientInterface->getGameSettings (); - if (settings != NULL) - { -//printf("#1 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); - - if (settings->getMasterserver_admin () == - clientInterface->getSessionKey ()) - { - result = true; - } - } - } - - return result; - } - - void - MenuStateConnectedGame::broadCastGameSettingsToHeadlessServer (bool - forceNow) - { - if (isHeadlessAdmin () == false) - { - return; - } - - if (forceNow == true || - ((needToBroadcastServerSettings == true) - && - (difftime - ((long int) time (NULL), - broadcastServerSettingsDelayTimer) >= - HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS))) - { -//printf("In [%s:%s] Line: %d forceNow = %d broadcastServerSettingsDelayTimer = " MG_SIZE_T_SPECIFIER ", now =" MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,forceNow,broadcastServerSettingsDelayTimer,time(NULL)); - - needToBroadcastServerSettings = false; - broadcastServerSettingsDelayTimer = time (NULL); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - listBoxControls[i].setSelectedItemIndex (ctNetwork); - } - } - for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork) - { - listBoxControls[i].setSelectedItemIndex (ctNetworkUnassigned); - } - } - - if (validDisplayedGamesettings) - { - loadGameSettings (&displayedGamesettings); - - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("broadcast settings:\n%s\n", - displayedGamesettings.toString ().c_str ()); - -//printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); -//printf("sending Settings map filter=%d\n",displayedGamesettings.getMapFilter()); - clientInterface->broadcastGameSetup (&displayedGamesettings); - noReceiveTimer = time (NULL); - } - } - } - - void - MenuStateConnectedGame::updateResourceMultiplier (const int index) - { - ControlType - ct = - static_cast < ControlType > - (listBoxControls[index].getSelectedItemIndex ()); - if (ct == ctCpuEasy || ct == ctNetworkCpuEasy) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - easyMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (true); - } - else if (ct == ctCpu || ct == ctNetworkCpu) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - normalMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (true); - } - else if (ct == ctCpuUltra || ct == ctNetworkCpuUltra) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - ultraMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (true); - } - else if (ct == ctCpuZeta || ct == ctNetworkCpuZeta) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - megaMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (true); - } - else - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - normalMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (false); - } - - listBoxRMultiplier[index]. - setEditable (listBoxRMultiplier[index].getEnabled ()); - listBoxRMultiplier[index]. - setVisible (listBoxRMultiplier[index].getEnabled ()); - } - - void - MenuStateConnectedGame::mouseClickAdmin (int x, int y, - MouseButton mouseButton, - string advanceToItemStartingWith) - { - - try - { - int - oldListBoxMapfilterIndex = listBoxMapFilter.getSelectedItemIndex (); - if (buttonPlayNow.mouseClick (x, y) && buttonPlayNow.getEnabled ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - PlayNow (true); - return; - } - else if (buttonRestoreLastSettings.mouseClick (x, y) - && buttonRestoreLastSettings.getEnabled ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.playFx (coreData.getClickSoundB ()); - - RestoreLastGameSettings (); - } - else if (checkBoxAllowNativeLanguageTechtree.mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxMap.mouseClick (x, y, advanceToItemStartingWith)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - getCurrentMapFile ().c_str ()); - - if (loadMapInfo - (Config::getMapPath (getCurrentMapFile (), "", false), &mapInfo, - true) == true) - { - labelMapInfo.setText (mapInfo.desc); - } - else - { - labelMapInfo.setText ("???"); - } - - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxFogOfWar.mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (checkBoxAllowObservers.mouseClick (x, y)) - { +Shared::CompressionUtil; + +namespace Glest { + namespace Game { + + static const int + MAX_PING_LAG_COUNT = 6; + static const double + REPROMPT_DOWNLOAD_SECONDS = 7; + //static const string ITEM_MISSING = "***missing***"; + // above replaced with Lang::getInstance().getString("DataMissing","",true) + const int + HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS = 2; + static const char * + HEADLESS_SAVED_GAME_FILENAME = "lastHeadlessGameSettings.mgg"; + + const int + mapPreviewTexture_X = 5; + const int + mapPreviewTexture_Y = 185; + const int + mapPreviewTexture_W = 150; + const int + mapPreviewTexture_H = 150; + + struct FormatString { + void + operator () (string & s) { + s = formatString(s); + } + }; + + // ===================================================== + // class MenuStateConnectedGame + // ===================================================== + + MenuStateConnectedGame::MenuStateConnectedGame(Program * program, MainMenu * mainMenu, JoinMenu joinMenuInfo, bool openNetworkSlots) : + MenuState(program, mainMenu, "connected-game"), + modHttpServerThread(NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + containerName = "ClientConnectedGame"; + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; + updateDataSynchDetailText = false; + launchingNewGame = false; + isfirstSwitchingMapMessage = true; + + this->zoomedMap = false; + this->render_mapPreviewTexture_X = mapPreviewTexture_X; + this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; + this->render_mapPreviewTexture_W = mapPreviewTexture_W; + this->render_mapPreviewTexture_H = mapPreviewTexture_H; + + needToBroadcastServerSettings = false; + broadcastServerSettingsDelayTimer = 0; + lastGameSettingsReceivedCount = 0; + noReceiveTimer = time(NULL) - 100; // old but inititialized ( must be an "old" time ) + + soundConnectionCount = 0; + + this->factionVideo = NULL; + factionVideoSwitchedOffVolume = false; + currentTechName_factionPreview = ""; + currentFactionName_factionPreview = ""; + + ftpClientThread = NULL; + ftpMissingDataType = ftpmsg_MissingNone; + getMissingMapFromFTPServer = ""; + getMissingMapFromFTPServerLastPrompted = 0; + getMissingMapFromFTPServerInProgress = false; + getMissingTilesetFromFTPServer = ""; + getMissingTilesetFromFTPServerLastPrompted = 0; + getMissingTilesetFromFTPServerInProgress = false; + getMissingTechtreeFromFTPServer = ""; + getMissingTechtreeFromFTPServerLastPrompted = 0; + getMissingTechtreeFromFTPServerInProgress = false; + + getInProgressSavedGameFromFTPServer = ""; + getInProgressSavedGameFromFTPServerInProgress = false; + readyToJoinInProgressGame = false; + + lastCheckedCRCTilesetName = ""; + lastCheckedCRCTechtreeName = ""; + lastCheckedCRCMapName = ""; + lastCheckedCRCTilesetValue = 0; + lastCheckedCRCTechtreeValue = 0; + lastCheckedCRCMapValue = 0; + + mapPreviewTexture = NULL; + currentFactionLogo = ""; + factionTexture = NULL; + lastMissingMap = ""; + lastMissingTechtree = ""; + lastMissingTileSet = ""; + + activeInputLabel = NULL; + lastNetworkSendPing = 0; + pingCount = 0; + needToSetChangedGameSettings = false; + lastSetChangedGameSettings = time(NULL); + showFullConsole = false; + + + currentFactionName = ""; + currentMap = ""; + settingsReceivedFromServer = false; + initialSettingsReceivedFromServer = false; + + validOriginalGameSettings = false; + validDisplayedGamesettings = false; + + returnMenuInfo = joinMenuInfo; + Lang & lang = Lang::getInstance(); + + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Ok")); + mainMessageBox.setEnabled(false); + + ftpMessageBox.registerGraphicComponent(containerName, "ftpMessageBox"); + ftpMessageBox.init(lang.getString("ModCenter"), + lang.getString("GameHost")); + ftpMessageBox.addButton(lang.getString("NoDownload")); + ftpMessageBox.setEnabled(false); + + NetworkManager & networkManager = NetworkManager::getInstance(); + Config & config = Config::getInstance(); + defaultPlayerName = + config.getString("NetPlayerName", Socket::getHostName().c_str()); + enableFactionTexturePreview = config.getBool("FactionPreview", "true"); + enableMapPreview = config.getBool("MapPreview", "true"); + + enableScenarioTexturePreview = + Config::getInstance().getBool("EnableScenarioTexturePreview", + "true"); + scenarioLogoTexture = NULL; + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + this->dirList = Config::getInstance().getPathListForType(ptScenarios); + + vector < + string > + techtreesList = Config::getInstance().getPathListForType(ptTechs); + techTree.reset(new TechTree(techtreesList)); + + vector < + string > + teamItems, controlItems, results, rMultiplier, playerStatuses; + + // Some of these values must also be changed to match those in + // menu_state_custom_game.cpp + int + labelOffset = 23; + int + setupPos = 605; + // mapHeadPos is the placement of the text "map", not the map itself + int mapHeadPos = 310; + int + mapPos = mapHeadPos - labelOffset; + int + aHeadPos = mapHeadPos - 90; + int + aPos = aHeadPos - labelOffset; + int + networkHeadPos = 700; + + //state + labelStatus.registerGraphicComponent(containerName, "labelStatus"); + labelStatus.init(30, networkHeadPos); + labelStatus.setText(""); + + labelInfo.registerGraphicComponent(containerName, "labelInfo"); + labelInfo.init(30, networkHeadPos + 30); + labelInfo.setText(""); + labelInfo.setFont(CoreData::getInstance().getMenuFontBig()); + labelInfo.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + labelWaitingForPlayers.registerGraphicComponent(containerName, + "labelInfo"); + labelWaitingForPlayers.init(30, 100); + labelWaitingForPlayers.setText(""); + labelWaitingForPlayers.setFont(CoreData::getInstance(). + getMenuFontBig()); + labelWaitingForPlayers.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + labelWaitingForPlayers.setTextColor(Vec3f(1.0f, 1.0f, 0.f)); + + timerLabelFlash = time(NULL); + labelDataSynchInfo.registerGraphicComponent(containerName, + "labelDataSynchInfo"); + labelDataSynchInfo.init(30, networkHeadPos - 60); + labelDataSynchInfo.setText(""); + labelDataSynchInfo.setFont(CoreData::getInstance().getMenuFontBig()); + labelDataSynchInfo.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + int + xoffset = 90; + labelAllowTeamUnitSharing.registerGraphicComponent(containerName, + "labelAllowTeamUnitSharing"); + labelAllowTeamUnitSharing.init(xoffset + 410, 670, 80); + labelAllowTeamUnitSharing.setText(lang. + getString("AllowTeamUnitSharing")); + labelAllowTeamUnitSharing.setVisible(true); + + checkBoxAllowTeamUnitSharing.registerGraphicComponent(containerName, + "checkBoxAllowTeamUnitSharing"); + checkBoxAllowTeamUnitSharing.init(xoffset + 612, 670); + checkBoxAllowTeamUnitSharing.setValue(false); + checkBoxAllowTeamUnitSharing.setVisible(true); + checkBoxAllowTeamUnitSharing.setEditable(false); + + labelAllowTeamResourceSharing.registerGraphicComponent(containerName, + "labelAllowTeamResourceSharing"); + labelAllowTeamResourceSharing.init(xoffset + 410, 640, 80); + labelAllowTeamResourceSharing.setText(lang. + getString + ("AllowTeamResourceSharing")); + labelAllowTeamResourceSharing.setVisible(true); + + checkBoxAllowTeamResourceSharing.registerGraphicComponent + (containerName, "checkBoxAllowTeamResourceSharing"); + checkBoxAllowTeamResourceSharing.init(xoffset + 612, 640); + checkBoxAllowTeamResourceSharing.setValue(false); + checkBoxAllowTeamResourceSharing.setVisible(true); + checkBoxAllowTeamResourceSharing.setEditable(false); + + // fog - o - war + xoffset = 65; + labelFogOfWar.registerGraphicComponent(containerName, "labelFogOfWar"); + labelFogOfWar.init(xoffset + 100, aHeadPos, 165); + labelFogOfWar.setText(lang.getString("FogOfWar")); + + listBoxFogOfWar.registerGraphicComponent(containerName, + "listBoxFogOfWar"); + listBoxFogOfWar.init(xoffset + 100, aPos, 165); + listBoxFogOfWar.pushBackItem(lang.getString("Enabled")); + listBoxFogOfWar.pushBackItem(lang.getString("Explored")); + listBoxFogOfWar.pushBackItem(lang.getString("Disabled")); + listBoxFogOfWar.setSelectedItemIndex(0); + listBoxFogOfWar.setEditable(false); + + labelAllowObservers.registerGraphicComponent(containerName, + "labelAllowObservers"); + labelAllowObservers.init(xoffset + 325, aHeadPos, 80); + labelAllowObservers.setText(lang.getString("AllowObservers")); + + checkBoxAllowObservers.registerGraphicComponent(containerName, + "checkBoxAllowObservers"); + checkBoxAllowObservers.init(xoffset + 325, aPos); + checkBoxAllowObservers.setValue(checkBoxAllowObservers.getValue()); + + checkBoxAllowObservers.setEditable(false); + + for (int i = 0; i < 45; ++i) { + rMultiplier.push_back(floatToStr(0.5f + 0.1f * i, 1)); + } + + labelFallbackCpuMultiplier.registerGraphicComponent(containerName, + "labelFallbackCpuMultiplier"); + labelFallbackCpuMultiplier.init(xoffset + 500, aHeadPos, 80); + labelFallbackCpuMultiplier.setText(lang.getString + ("FallbackCpuMultiplier")); + + listBoxFallbackCpuMultiplier.registerGraphicComponent(containerName, + "listBoxFallbackCpuMultiplier"); + listBoxFallbackCpuMultiplier.init(xoffset + 500, aPos, 80); + listBoxFallbackCpuMultiplier.setItems(rMultiplier); + listBoxFallbackCpuMultiplier.setSelectedItem("1.0"); + + + // Allow Switch Team Mode + labelEnableSwitchTeamMode.registerGraphicComponent(containerName, + "labelEnableSwitchTeamMode"); + labelEnableSwitchTeamMode.init(xoffset + 325, aHeadPos + 45, 80); + labelEnableSwitchTeamMode. + setText(lang.getString("EnableSwitchTeamMode")); + + checkBoxEnableSwitchTeamMode.registerGraphicComponent(containerName, + "checkBoxEnableSwitchTeamMode"); + checkBoxEnableSwitchTeamMode.init(xoffset + 325, aPos + 45); + checkBoxEnableSwitchTeamMode.setValue(false); + checkBoxEnableSwitchTeamMode.setEditable(false); + + labelAISwitchTeamAcceptPercent.registerGraphicComponent(containerName, + "labelAISwitchTeamAcceptPercent"); + labelAISwitchTeamAcceptPercent.init(xoffset + 500, aHeadPos + 45, 80); + labelAISwitchTeamAcceptPercent.setText(lang.getString + ("AISwitchTeamAcceptPercent")); + + listBoxAISwitchTeamAcceptPercent.registerGraphicComponent + (containerName, "listBoxAISwitchTeamAcceptPercent"); + listBoxAISwitchTeamAcceptPercent.init(xoffset + 500, aPos + 45, 80); + for (int i = 0; i <= 100; i = i + 10) { + listBoxAISwitchTeamAcceptPercent.pushBackItem(intToStr(i)); + } + listBoxAISwitchTeamAcceptPercent.setSelectedItem(intToStr(30)); + listBoxAISwitchTeamAcceptPercent.setEditable(false); + + //create + buttonCancelDownloads.registerGraphicComponent(containerName, + "buttonCancelDownloads"); + buttonCancelDownloads.init(xoffset + 620, 180, 150); + buttonCancelDownloads.setText(lang.getString("CancelDownloads")); + + // Network Frame Period + xoffset = 65; + //map listBox + + xoffset = 65; + // MapFilter + labelMapFilter.registerGraphicComponent(containerName, + "labelMapFilter"); + labelMapFilter.init(xoffset + 325, mapHeadPos); + labelMapFilter.setText(lang.getString("MapFilter")); + + listBoxMapFilter.registerGraphicComponent(containerName, + "listBoxMapFilter"); + listBoxMapFilter.init(xoffset + 325, mapPos, 80); + listBoxMapFilter.pushBackItem("-"); + for (int i = 1; i < GameConstants::maxPlayers + 1; ++i) { + listBoxMapFilter.pushBackItem(intToStr(i)); + } + listBoxMapFilter.setSelectedItemIndex(0); + listBoxMapFilter.setEditable(false); + + + // put them all in a set, to weed out duplicates (gbm & mgm with same name) + // will also ensure they are alphabetically listed (rather than how the OS provides them) + listBoxMap.registerGraphicComponent(containerName, "listBoxMap"); + listBoxMap.init(xoffset + 100, mapPos, 220); + listBoxMap.setEditable(false); + + labelMapInfo.registerGraphicComponent(containerName, "labelMapInfo"); + labelMapInfo.init(xoffset + 100, mapPos - labelOffset - 10, 200, 40); + labelMapInfo.setText("?"); + + labelMap.registerGraphicComponent(containerName, "labelMap"); + labelMap.init(xoffset + 100, mapHeadPos); + labelMap.setText(lang.getString("Map")); + + //tileset listBox + listBoxTileset.registerGraphicComponent(containerName, + "listBoxTileset"); + listBoxTileset.init(xoffset + 500, mapPos, 160); + listBoxTileset.setEditable(false); + + labelTileset.registerGraphicComponent(containerName, "labelTileset"); + labelTileset.init(xoffset + 500, mapHeadPos); + labelTileset.setText(lang.getString("Tileset")); + + + //tech Tree listBox + listBoxTechTree.setEditable(false); + + listBoxTechTree.registerGraphicComponent(containerName, + "listBoxTechTree"); + listBoxTechTree.init(xoffset + 700, mapPos, 180); + + labelTechTree.registerGraphicComponent(containerName, "labelTechTree"); + labelTechTree.init(xoffset + 700, mapHeadPos); + labelTechTree.setText(lang.getString("TechTree")); + + labelAllowNativeLanguageTechtree.registerGraphicComponent + (containerName, "labelAllowNativeLanguageTechtree"); + labelAllowNativeLanguageTechtree.init(xoffset + 700, aHeadPos + 45); + labelAllowNativeLanguageTechtree.setText(lang.getString + ("AllowNativeLanguageTechtree")); + + checkBoxAllowNativeLanguageTechtree.registerGraphicComponent + (containerName, "checkBoxAllowNativeLanguageTechtree"); + checkBoxAllowNativeLanguageTechtree.init(xoffset + 700, aPos + 45); + checkBoxAllowNativeLanguageTechtree.setValue(false); + checkBoxAllowNativeLanguageTechtree.setEditable(false); + checkBoxAllowNativeLanguageTechtree.setEnabled(false); + + // Network Scenario + int + scenarioX = xoffset + 700; + int + scenarioY = aPos; + labelScenario.registerGraphicComponent(containerName, "labelScenario"); + labelScenario.init(scenarioX, aHeadPos); + labelScenario.setText(lang.getString("Scenario")); + listBoxScenario.registerGraphicComponent(containerName, + "listBoxScenario"); + listBoxScenario.init(scenarioX + 30, scenarioY, 190); + listBoxScenario.setEditable(false); + listBoxScenario.setEnabled(false); + checkBoxScenario.registerGraphicComponent(containerName, + "checkBoxScenario"); + checkBoxScenario.init(scenarioX, scenarioY); + checkBoxScenario.setValue(false); + checkBoxScenario.setEditable(false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + xoffset = 5; + int + rowHeight = 27; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + labelPlayers[i].registerGraphicComponent(containerName, + "labelPlayers" + + intToStr(i)); + labelPlayers[i].init(xoffset - 1, setupPos - 30 - i * rowHeight + 2); + labelPlayers[i]. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + labelPlayers[i]. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + labelPlayers[i].setEditable(false); + + labelPlayerStatus[i].registerGraphicComponent(containerName, + "labelPlayerStatus" + + intToStr(i)); + labelPlayerStatus[i].init(xoffset + 14, + setupPos - 30 - i * rowHeight + 2); + labelPlayerNames[i].registerGraphicComponent(containerName, + "labelPlayerNames" + + intToStr(i)); + labelPlayerNames[i].init(xoffset + 30, + setupPos - 30 - i * rowHeight); + + listBoxControls[i].registerGraphicComponent(containerName, + "listBoxControls" + + intToStr(i)); + listBoxControls[i].init(xoffset + 160, setupPos - 30 - i * rowHeight, + 174); + listBoxControls[i].setEditable(false); + + listBoxRMultiplier[i].registerGraphicComponent(containerName, + "listBoxRMultiplier" + + intToStr(i)); + listBoxRMultiplier[i].init(xoffset + 336, + setupPos - 30 - i * rowHeight, 70); + listBoxRMultiplier[i].setEditable(false); + + listBoxFactions[i].registerGraphicComponent(containerName, + "listBoxFactions" + + intToStr(i)); + listBoxFactions[i].init(xoffset + 411, setupPos - 30 - i * rowHeight, + 247); + listBoxFactions[i].setLeftControlled(true); + listBoxFactions[i].setEditable(false); + + listBoxTeams[i].registerGraphicComponent(containerName, + "listBoxTeams" + + intToStr(i)); + listBoxTeams[i].init(xoffset + 660, setupPos - 30 - i * rowHeight, + 60); + listBoxTeams[i].setEditable(false); + listBoxTeams[i].setLighted(true); + + labelNetStatus[i].registerGraphicComponent(containerName, + "labelNetStatus" + + intToStr(i)); + labelNetStatus[i].init(xoffset + 723, setupPos - 30 - i * rowHeight, + 60); + labelNetStatus[i]. + setFont(CoreData::getInstance().getDisplayFontSmall()); + labelNetStatus[i]. + setFont3D(CoreData::getInstance().getDisplayFontSmall3D()); + + grabSlotButton[i].registerGraphicComponent(containerName, + "grabSlotButton" + + intToStr(i)); + grabSlotButton[i].init(xoffset + 726, setupPos - 30 - i * rowHeight, + 35, rowHeight - 5); + grabSlotButton[i].setText(">"); + } + + labelControl.registerGraphicComponent(containerName, "labelControl"); + labelControl.init(xoffset + 160, setupPos, 50, GraphicListBox::defH, + true); + labelControl.setText(lang.getString("Control")); + + labelRMultiplier.registerGraphicComponent(containerName, + "labelRMultiplier"); + labelRMultiplier.init(xoffset + 310, setupPos, 50, + GraphicListBox::defH, true); + + labelFaction.registerGraphicComponent(containerName, "labelFaction"); + labelFaction.init(xoffset + 411, setupPos, 50, GraphicListBox::defH, + true); + labelFaction.setText(lang.getString("Faction")); + + labelTeam.registerGraphicComponent(containerName, "labelTeam"); + labelTeam.init(xoffset + 660, setupPos, 50, GraphicListBox::defH, + true); + labelTeam.setText(lang.getString("Team")); + + labelControl.setFont(CoreData::getInstance().getMenuFontBig()); + labelControl.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelFaction.setFont(CoreData::getInstance().getMenuFontBig()); + labelFaction.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTeam.setFont(CoreData::getInstance().getMenuFontBig()); + labelTeam.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + //texts + buttonDisconnect.setText(lang.getString("Return")); + + controlItems.push_back(lang.getString("Closed")); + controlItems.push_back(lang.getString("CpuEasy")); + controlItems.push_back(lang.getString("Cpu")); + controlItems.push_back(lang.getString("CpuUltra")); + controlItems.push_back(lang.getString("CpuZeta")); + controlItems.push_back(lang.getString("Network")); + controlItems.push_back(lang.getString("NetworkUnassigned")); + controlItems.push_back(lang.getString("Human")); + + + if (config.getBool("EnableNetworkCpu", "false") == true) { + controlItems.push_back(lang.getString("NetworkCpuEasy")); + controlItems.push_back(lang.getString("NetworkCpu")); + controlItems.push_back(lang.getString("NetworkCpuUltra")); + controlItems.push_back(lang.getString("NetworkCpuZeta")); + } + + for (int i = 1; i <= GameConstants::maxPlayers; ++i) { + teamItems.push_back(intToStr(i)); + } + for (int i = GameConstants::maxPlayers + 1; + i <= GameConstants::maxPlayers + GameConstants::specialFactions; + ++i) { + teamItems.push_back(intToStr(i)); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + labelPlayerStatus[i].setText(""); + labelPlayerStatus[i].setTexture(NULL); + labelPlayerStatus[i].setH(16); + labelPlayerStatus[i].setW(12); + + labelPlayers[i].setText(intToStr(i + 1)); + labelPlayerNames[i].setText(""); + labelPlayerNames[i].setMaxEditWidth(16); + labelPlayerNames[i].setMaxEditRenderWidth(127); + + listBoxTeams[i].setItems(teamItems); + listBoxTeams[i].setSelectedItemIndex(i); + listBoxControls[i].setItems(controlItems); + listBoxRMultiplier[i].setItems(rMultiplier); + listBoxRMultiplier[i].setSelectedItem("1.0"); + + labelNetStatus[i].setText("V"); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //init controllers + listBoxControls[0].setSelectedItemIndex(ctHuman); + + + //map listBox + // put them all in a set, to weed out duplicates (gbm & mgm with same name) + // will also ensure they are alphabetically listed (rather than how the OS provides them) + setupMapList(""); + listBoxMap.setItems(formattedPlayerSortedMaps[0]); + + int + buttonx = 165; + int + buttony = 150; + + listBoxPlayerStatus.registerGraphicComponent(containerName, + "listBoxPlayerStatus"); + listBoxPlayerStatus.init(buttonx, buttony, 165); + listBoxPlayerStatus.setTextColor(Vec3f(1.0f, 0.f, 0.f)); + listBoxPlayerStatus.setLighted(true); + playerStatuses.push_back(lang.getString("PlayerStatusSetup")); + playerStatuses.push_back(lang.getString("PlayerStatusBeRightBack")); + playerStatuses.push_back(lang.getString("PlayerStatusReady")); + listBoxPlayerStatus.setItems(playerStatuses); + buttonx += 180; + + buttonDisconnect.registerGraphicComponent(containerName, + "buttonDisconnect"); + buttonDisconnect.init(buttonx, buttony, 125); + buttonx += 132; + + buttonRestoreLastSettings.registerGraphicComponent(containerName, + "buttonRestoreLastSettings"); + buttonRestoreLastSettings.init(buttonx, buttony, 240); + buttonRestoreLastSettings.setText(lang.getString + ("ReloadLastGameSettings")); + buttonx += 247; + + buttonPlayNow.registerGraphicComponent(containerName, "buttonPlayNow"); + buttonPlayNow.init(buttonx, buttony, 125); + buttonPlayNow.setText(lang.getString("PlayNow")); + buttonPlayNow.setVisible(false); + + + // write hint to console: + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + console.addLine(lang.getString("ToSwitchOffMusicPress") + " - \"" + + configKeys.getString("ToggleMusic") + "\""); + chatManager.init(&console, -1, true); + + GraphicComponent::applyAllCustomProperties(containerName); + + //tileset listBox + setupTilesetList(""); + + int + initialTechSelection = setupTechList("", true); + listBoxTechTree.setSelectedItemIndex(initialTechSelection); + + + //scenario listbox + vector < string > resultsScenarios; + findDirs(dirList, resultsScenarios); + // Filter out only scenarios with no network slots + for (int i = 0; i < (int) resultsScenarios.size(); ++i) { + string scenario = resultsScenarios[i]; + string file = Scenario::getScenarioPath(dirList, scenario); + + try { + if (file != "") { + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, &scenarioInfo, isTutorial); + + bool isNetworkScenario = false; + for (unsigned int j = 0; + isNetworkScenario == false + && j < (unsigned int) GameConstants::maxPlayers; ++j) { + if (scenarioInfo.factionControls[j] == ctNetwork) { + isNetworkScenario = true; + } + } + if (isNetworkScenario == true) { + scenarioFiles.push_back(scenario); + } + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d]\nError loading scenario [%s]:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenario.c_str(), ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showMessageBox(szBuf, "Error", false); + //throw megaglest_runtime_error(szBuf); + } + } + resultsScenarios.clear(); + for (int i = 0; i < (int) scenarioFiles.size(); ++i) { + resultsScenarios.push_back(formatString(scenarioFiles[i])); + } + listBoxScenario.setItems(resultsScenarios); + checkBoxScenario.setEnabled(false); + + if (config.getBool("EnableFTPXfer", "true") == true) { + ClientInterface * + clientInterface = networkManager.getClientInterface(); + string serverUrl = clientInterface->getServerIpAddress(); + int + portNumber = clientInterface->getServerFTPPort(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] Using FTP port #: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, portNumber); + + vector < string > mapPathList = config.getPathListForType(ptMaps); + std::pair < string, string > mapsPath; + if (mapPathList.empty() == false) { + mapsPath.first = mapPathList[0]; + } + if (mapPathList.size() > 1) { + mapsPath.second = mapPathList[1]; + } + std::pair < string, string > tilesetsPath; + vector < string > tilesetsList = + Config::getInstance().getPathListForType(ptTilesets); + if (tilesetsList.empty() == false) { + tilesetsPath.first = tilesetsList[0]; + if (tilesetsList.size() > 1) { + tilesetsPath.second = tilesetsList[1]; + } + } + + std::pair < string, string > techtreesPath; + if (techtreesList.empty() == false) { + techtreesPath.first = techtreesList[0]; + if (techtreesList.size() > 1) { + techtreesPath.second = techtreesList[1]; + } + } + + std::pair < string, string > scenariosPath; + vector < string > scenariosList = + Config::getInstance().getPathListForType(ptScenarios); + if (scenariosList.empty() == false) { + scenariosPath.first = scenariosList[0]; + if (scenariosList.size() > 1) { + scenariosPath.second = scenariosList[1]; + } + } + + string + fileArchiveExtension = + config.getString("FileArchiveExtension", ""); + string + fileArchiveExtractCommand = + config.getString("FileArchiveExtractCommand", ""); + string + fileArchiveExtractCommandParameters = + config.getString("FileArchiveExtractCommandParameters", ""); + int32 + fileArchiveExtractCommandSuccessResult = + config.getInt("FileArchiveExtractCommandSuccessResult", "0"); + + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); + + ftpClientThread = new FTPClientThread(portNumber, serverUrl, + mapsPath, tilesetsPath, + techtreesPath, scenariosPath, + this, fileArchiveExtension, + fileArchiveExtractCommand, + fileArchiveExtractCommandParameters, + fileArchiveExtractCommandSuccessResult, + tempFilePath); + ftpClientThread->start(); + } + // Start http meta data thread + static + string + mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + modHttpServerThread = new SimpleTaskThread(this, 0, 200); + modHttpServerThread->setUniqueID(mutexOwnerId); + modHttpServerThread->start(); + + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL + && clientInterface->getJoinGameInProgress() == true) { + listBoxPlayerStatus.setVisible(false); + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("JoinPlayerToCurrentGameWelcome", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("JoinPlayerToCurrentGameWelcome", + languageList[i]).c_str(), + getHumanPlayerName().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s has connected to the game and would like to join.", + getHumanPlayerName().c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + sleep(1); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // I moved this block from loadMapInfo(), and modified it. It was + // setting the slots visible based on the number of hardMaxPlayers + // every time a new map was loaded. Trying it here instead, so the + // labels are made visible only once. Below, we'll be disabling slots + // that exceed hardMaxPlayers + for (int i = 0; i < GameConstants::maxPlayers; i++) { + labelPlayers[i].setVisible(true); + labelPlayerNames[i].setVisible(true); + listBoxControls[i].setVisible(true); + listBoxFactions[i].setVisible(true); + listBoxTeams[i].setVisible(true); + labelNetStatus[i].setVisible(true); + } + } + + void + MenuStateConnectedGame::reloadUI() { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + console.resetFonts(); + mainMessageBox.init(lang.getString("Ok")); + ftpMessageBox.init(lang.getString("ModCenter"), + lang.getString("GameHost")); + ftpMessageBox.addButton(lang.getString("NoDownload")); + + labelInfo.setFont(CoreData::getInstance().getMenuFontBig()); + labelInfo.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + labelWaitingForPlayers. + setFont(CoreData::getInstance().getMenuFontBig()); + labelWaitingForPlayers. + setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + labelDataSynchInfo.setFont(CoreData::getInstance().getMenuFontBig()); + labelDataSynchInfo. + setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + buttonCancelDownloads.setText(lang.getString("CancelDownloads")); + + labelFogOfWar.setText(lang.getString("FogOfWar")); + + vector < string > fowItems; + fowItems.push_back(lang.getString("Enabled")); + fowItems.push_back(lang.getString("Explored")); + fowItems.push_back(lang.getString("Disabled")); + listBoxFogOfWar.setItems(fowItems); + + labelAllowObservers.setText(lang.getString("AllowObservers")); + labelFallbackCpuMultiplier.setText(lang.getString + ("FallbackCpuMultiplier")); + + labelEnableSwitchTeamMode. + setText(lang.getString("EnableSwitchTeamMode")); + + labelAllowTeamUnitSharing. + setText(lang.getString("AllowTeamUnitSharing")); + labelAllowTeamResourceSharing. + setText(lang.getString("AllowTeamResourceSharing")); + + labelAISwitchTeamAcceptPercent.setText(lang.getString + ("AISwitchTeamAcceptPercent")); + + vector < string > aiswitchteamModeItems; + for (int i = 0; i <= 100; i = i + 10) { + aiswitchteamModeItems.push_back(intToStr(i)); + } + listBoxAISwitchTeamAcceptPercent.setItems(aiswitchteamModeItems); + + vector < string > rMultiplier; + for (int i = 0; i < 45; ++i) { + rMultiplier.push_back(floatToStr(0.5f + 0.1f * i, 1)); + } + listBoxFallbackCpuMultiplier.setItems(rMultiplier); + + labelMap.setText(lang.getString("Map")); + + labelMapFilter.setText(lang.getString("MapFilter")); + + labelTileset.setText(lang.getString("Tileset")); + + labelTechTree.setText(lang.getString("TechTree")); + + vector < string > playerstatusItems; + playerstatusItems.push_back(lang.getString("PlayerStatusSetup")); + playerstatusItems. + push_back(lang.getString("PlayerStatusBeRightBack")); + playerstatusItems.push_back(lang.getString("PlayerStatusReady")); + listBoxPlayerStatus.setItems(playerstatusItems); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + labelControl.setText(lang.getString("Control")); + + labelFaction.setText(lang.getString("Faction")); + + labelTeam.setText(lang.getString("Team")); + + labelControl.setFont(CoreData::getInstance().getMenuFontBig()); + labelControl.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelFaction.setFont(CoreData::getInstance().getMenuFontBig()); + labelFaction.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTeam.setFont(CoreData::getInstance().getMenuFontBig()); + labelTeam.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + //texts + buttonDisconnect.setText(lang.getString("Return")); + + vector < string > controlItems; + controlItems.push_back(lang.getString("Closed")); + controlItems.push_back(lang.getString("CpuEasy")); + controlItems.push_back(lang.getString("Cpu")); + controlItems.push_back(lang.getString("CpuUltra")); + controlItems.push_back(lang.getString("CpuZeta")); + controlItems.push_back(lang.getString("Network")); + controlItems.push_back(lang.getString("NetworkUnassigned")); + controlItems.push_back(lang.getString("Human")); + + if (config.getBool("EnableNetworkCpu", "false") == true) { + controlItems.push_back(lang.getString("NetworkCpuEasy")); + controlItems.push_back(lang.getString("NetworkCpu")); + controlItems.push_back(lang.getString("NetworkCpuUltra")); + controlItems.push_back(lang.getString("NetworkCpuZeta")); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + labelPlayers[i].setText(intToStr(i + 1)); + listBoxControls[i].setItems(controlItems); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + labelScenario.setText(lang.getString("Scenario")); + + labelAllowNativeLanguageTechtree.setText(lang.getString + ("AllowNativeLanguageTechtree")); + + buttonPlayNow.setText(lang.getString("PlayNow")); + buttonRestoreLastSettings.setText(lang.getString + ("ReloadLastGameSettings")); + + chatManager.init(&console, -1, true); + + GraphicComponent::reloadFontsForRegisterGraphicComponents + (containerName); + } + + void + MenuStateConnectedGame::disconnectFromServer() { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(false); + if (clientInterface != NULL) { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + soundRenderer.playFx(coreData.getClickSoundA()); + if (clientInterface->getSocket() != NULL) { + if (clientInterface->isConnected() == true) { + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + string sQuitText = lang.getString("QuitGame", languageList[i]); + clientInterface->sendTextMessage(sQuitText, -1, false, + languageList[i]); + } + sleep(1); + + } + clientInterface->close(); + } + clientInterface->reset(); + } + currentFactionName = ""; + currentMap = ""; + } + + MenuStateConnectedGame::~MenuStateConnectedGame() { + if (launchingNewGame == false) { + disconnectFromServer(); + NetworkManager & networkManager = NetworkManager::getInstance(); + networkManager.end(); + } + + if (modHttpServerThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + modHttpServerThread->setSimpleTaskInterfaceValid(false); + modHttpServerThread->signalQuit(); + modHttpServerThread->setThreadOwnerValid(false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (modHttpServerThread->canShutdown(true) == true + && modHttpServerThread->shutdownAndWait() == true) { + delete modHttpServerThread; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + modHttpServerThread = NULL; + } + + if (ftpClientThread != NULL) { + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + } + + ftpClientThread = NULL; + } + + cleanupMapPreviewTexture(); + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + } + + string MenuStateConnectedGame::refreshTilesetModInfo(string tilesetInfo) { + std::vector < std::string > tilesetInfoList; + Tokenize(tilesetInfo, tilesetInfoList, "|"); + if (tilesetInfoList.size() >= 5) { + Config & config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = tilesetInfoList[0]; + modinfo.crc = tilesetInfoList[1]; + modinfo.description = tilesetInfoList[2]; + modinfo.url = tilesetInfoList[3]; + modinfo.imageUrl = tilesetInfoList[4]; + modinfo.type = mt_Tileset; + + string + itemPath = + config.getPathListForType(ptTilesets, + "")[1] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + bool + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + uint32 + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", NULL, + forceRefresh); + if (crc == 0) { + itemPath = + config.getPathListForType(ptTilesets, + "")[0] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, forceRefresh); + } + } + modinfo.localCRC = uIntToStr(crc); + //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + + //printf("#1 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); + } else { + modinfo.localCRC = ""; + + //printf("#2 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); + } + + tilesetCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + string MenuStateConnectedGame::refreshTechModInfo(string techInfo) { + std::vector < std::string > techInfoList; + Tokenize(techInfo, techInfoList, "|"); + if (techInfoList.size() >= 6) { + Config & config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = techInfoList[0]; + modinfo.count = techInfoList[1]; + modinfo.crc = techInfoList[2]; + modinfo.description = techInfoList[3]; + modinfo.url = techInfoList[4]; + modinfo.imageUrl = techInfoList[5]; + modinfo.type = mt_Techtree; + + string + itemPath = + config.getPathListForType(ptTechs, + "")[1] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + bool + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + uint32 + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", NULL, + forceRefresh); + if (crc == 0) { + itemPath = + config.getPathListForType(ptTechs, + "")[0] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, forceRefresh); + } + } + modinfo.localCRC = uIntToStr(crc); + //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + modinfo.localCRC = ""; + } + techCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + string MenuStateConnectedGame::getMapCRC(string mapName) { + Config & config = Config::getInstance(); + vector < string > mappaths = config.getPathListForType(ptMaps, ""); + string result = ""; + if (mappaths.empty() == false) { + Checksum checksum; + string itemPath = mappaths[1] + "/" + mapName; + if (fileExists(itemPath)) { + checksum.addFile(itemPath); + uint32 crc = checksum.getSum(); + result = uIntToStr(crc); + //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + itemPath = mappaths[0] + "/" + mapName; + if (fileExists(itemPath)) { + checksum.addFile(itemPath); + uint32 crc = checksum.getSum(); + result = uIntToStr(crc); + //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + result = ""; + } + } + } else { + result = ""; + } + return result; + } + + string MenuStateConnectedGame::refreshMapModInfo(string mapInfo) { + std::vector < std::string > mapInfoList; + Tokenize(mapInfo, mapInfoList, "|"); + if (mapInfoList.size() >= 6) { + //Config &config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = mapInfoList[0]; + modinfo.count = mapInfoList[1]; + modinfo.crc = mapInfoList[2]; + modinfo.description = mapInfoList[3]; + modinfo.url = mapInfoList[4]; + modinfo.imageUrl = mapInfoList[5]; + modinfo.type = mt_Map; + modinfo.localCRC = getMapCRC(modinfo.name); + mapCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + void + MenuStateConnectedGame::simpleTask(BaseThread * callingThread, + void *userdata) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + static + string + mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexThreadOwner(callingThread->getMutexThreadOwnerValid(), + mutexOwnerId); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + callingThread->getMutexThreadOwnerValid()->setOwnerId(mutexOwnerId); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); + + std::string techsMetaData = ""; + std::string tilesetsMetaData = ""; + std::string mapsMetaData = ""; + std::string scenariosMetaData = ""; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (config.getString("Masterserver", "") != "") { + string baseURL = config.getString("Masterserver"); + if (baseURL != "") { + endPathWithSlash(baseURL, false); + } + string + phpVersionParam = + config.getString("phpVersionParam", "?version=0.1"); + string + gameVersion = + "&glestVersion=" + SystemFlags::escapeURL(glestVersionString); + string + playerUUID = + "&uuid=" + + SystemFlags::escapeURL(config.getString("PlayerId", "")); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", + __FILE__, __FUNCTION__, __LINE__, baseURL.c_str()); + + CURL * + handle = SystemFlags::initHTTP(); + CURLcode curlResult = CURLE_OK; + techsMetaData = + SystemFlags::getHTTP(baseURL + "showTechsForGlest.php" + + phpVersionParam + gameVersion + playerUUID, + handle, -1, &curlResult); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("techsMetaData [%s] curlResult = %d\n", + techsMetaData.c_str(), curlResult); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#1 ") + szBuf, true); + } + + if (curlResult == CURLE_OK || + (curlResult != CURLE_COULDNT_RESOLVE_HOST && + curlResult != CURLE_COULDNT_CONNECT)) { + + tilesetsMetaData = + SystemFlags::getHTTP(baseURL + "showTilesetsForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("tilesetsMetaData [%s]\n", tilesetsMetaData.c_str()); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#2 ") + szBuf, true); + } + + mapsMetaData = + SystemFlags::getHTTP(baseURL + "showMapsForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("mapsMetaData [%s]\n", mapsMetaData.c_str()); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#3 ") + szBuf, true); + } + + scenariosMetaData = + SystemFlags::getHTTP(baseURL + "showScenariosForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("scenariosMetaData [%s]\n", scenariosMetaData.c_str()); + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#4 ") + szBuf, true); + } + } + SystemFlags::cleanupHTTP(&handle); + } else { + console.addLine(lang.getString("MasterServerMissing"), true); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + MutexSafeWrapper + safeMutex(callingThread->getMutexThreadObjectAccessor(), + string(__FILE__) + "_" + intToStr(__LINE__)); + tilesetListRemote.clear(); + Tokenize(tilesetsMetaData, tilesetListRemote, "\n"); + safeMutex.ReleaseLock(true); + + for (unsigned int i = 0; i < tilesetListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + safeMutex.Lock(); + string result = refreshTilesetModInfo(tilesetListRemote[i]); + safeMutex.ReleaseLock(true); + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + safeMutex.Lock(); + techListRemote.clear(); + Tokenize(techsMetaData, techListRemote, "\n"); + safeMutex.ReleaseLock(true); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + for (unsigned int i = 0; i < techListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + safeMutex.Lock(); + string result = refreshTechModInfo(techListRemote[i]); + safeMutex.ReleaseLock(true); + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + safeMutex.Lock(); + mapListRemote.clear(); + Tokenize(mapsMetaData, mapListRemote, "\n"); + safeMutex.ReleaseLock(true); + + for (unsigned int i = 0; i < mapListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + safeMutex.Lock(); + string result = refreshMapModInfo(mapListRemote[i]); + safeMutex.ReleaseLock(true); + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (modHttpServerThread != NULL) { + modHttpServerThread->signalQuit(); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + void + MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton) { + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + Lang & lang = Lang::getInstance(); + + string advanceToItemStartingWith = ""; + if (mainMessageBox.getEnabled() == false) { + if (::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == + true) { + wchar_t + lastKey = ::Shared::Platform::Window::extractLastKeyPressed(); + //printf("lastKey = %d [%c]\n",lastKey,lastKey); + advanceToItemStartingWith = lastKey; + } + } + + if (mapPreviewTexture != NULL) { + // printf("X: %d Y: %d [%d, %d, %d, %d]\n", + // x, y, + // this->render_mapPreviewTexture_X, this->render_mapPreviewTexture_X + this->render_mapPreviewTexture_W, + // this->render_mapPreviewTexture_Y, this->render_mapPreviewTexture_Y + this->render_mapPreviewTexture_H); + + if (x >= this->render_mapPreviewTexture_X + && x <= + this->render_mapPreviewTexture_X + + this->render_mapPreviewTexture_W + && y >= this->render_mapPreviewTexture_Y + && y <= + this->render_mapPreviewTexture_Y + + this->render_mapPreviewTexture_H) { + + if (this->render_mapPreviewTexture_X == mapPreviewTexture_X && + this->render_mapPreviewTexture_Y == mapPreviewTexture_Y && + this->render_mapPreviewTexture_W == mapPreviewTexture_W && + this->render_mapPreviewTexture_H == mapPreviewTexture_H) { + + const + Metrics & + metrics = Metrics::getInstance(); + + this->render_mapPreviewTexture_X = 0; + this->render_mapPreviewTexture_Y = 0; + this->render_mapPreviewTexture_W = metrics.getVirtualW(); + this->render_mapPreviewTexture_H = metrics.getVirtualH(); + this->zoomedMap = true; + + cleanupMapPreviewTexture(); + } else { + this->render_mapPreviewTexture_X = mapPreviewTexture_X; + this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; + this->render_mapPreviewTexture_W = mapPreviewTexture_W; + this->render_mapPreviewTexture_H = mapPreviewTexture_H; + this->zoomedMap = false; + + cleanupMapPreviewTexture(); + } + return; + } + if (this->zoomedMap == true) { + return; + } + } + + if (mainMessageBox.getEnabled()) { + int + button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + mainMessageBox.setEnabled(false); + } + } + } else if (ftpMessageBox.getEnabled()) { + int + button = 0; + if (ftpMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + ftpMessageBox.setEnabled(false); + + if (button == 0 + || (button == 1 && ftpMessageBox.getButtonCount() == 3)) { + if (ftpMissingDataType == ftpmsg_MissingMap) { + getMissingMapFromFTPServerInProgress = true; + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingMapNowDownloading", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingMapNowDownloading", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + getMissingMapFromFTPServer.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is attempting to download the map: %s", + getHumanPlayerName().c_str(), + getMissingMapFromFTPServer.c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + + if (ftpClientThread != NULL) { + if (button == 0 && ftpMessageBox.getButtonCount() == 3) { + string mapName = getMissingMapFromFTPServer; + + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + string mapURL = mapCacheList[mapName].url; + safeMutexThread.ReleaseLock(); + + if (ftpClientThread != NULL) + ftpClientThread->addMapToRequests(mapName, mapURL); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingMapFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } else { + ftpClientThread->addMapToRequests + (getMissingMapFromFTPServer); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingMapFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } + } + } else if (ftpMissingDataType == ftpmsg_MissingTileset) { + getMissingTilesetFromFTPServerInProgress = true; + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTilesetNowDownloading", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("DataMissingTilesetNowDownloading", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + getMissingTilesetFromFTPServer.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is attempting to download the tileset: %s", + getHumanPlayerName().c_str(), + getMissingTilesetFromFTPServer.c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + + if (ftpClientThread != NULL) { + if (button == 0 && ftpMessageBox.getButtonCount() == 3) { + string tilesetName = getMissingTilesetFromFTPServer; + + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + string tilesetURL = tilesetCacheList[tilesetName].url; + safeMutexThread.ReleaseLock(); + + if (ftpClientThread != NULL) + ftpClientThread->addTilesetToRequests(tilesetName, + tilesetURL); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingTilesetFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } else { + ftpClientThread->addTilesetToRequests + (getMissingTilesetFromFTPServer); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingTilesetFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } + } + } else if (ftpMissingDataType == ftpmsg_MissingTechtree) { + getMissingTechtreeFromFTPServerInProgress = true; + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtreeNowDownloading", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("DataMissingTechtreeNowDownloading", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + getMissingTechtreeFromFTPServer.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is attempting to download the techtree: %s", + getHumanPlayerName().c_str(), + getMissingTechtreeFromFTPServer.c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + + if (ftpClientThread != NULL) { + if (button == 0 && ftpMessageBox.getButtonCount() == 3) { + string techName = getMissingTechtreeFromFTPServer; + + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + string techURL = techCacheList[techName].url; + safeMutexThread.ReleaseLock(); + + if (ftpClientThread != NULL) + ftpClientThread->addTechtreeToRequests(techName, + techURL); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingTechtreeFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } else { + ftpClientThread->addTechtreeToRequests + (getMissingTechtreeFromFTPServer); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList[getMissingTechtreeFromFTPServer] = + pair < int, + string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } + } + } + } + } + } else if (buttonCancelDownloads.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + + if (ftpClientThread != NULL && fileFTPProgressList.empty() == false) { + + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + } + ftpClientThread = NULL; + + fileFTPProgressList.clear(); + getMissingMapFromFTPServerInProgress = false; + getMissingTilesetFromFTPServerInProgress = false; + getMissingTechtreeFromFTPServerInProgress = false; + getMissingMapFromFTPServer = ""; + getMissingTilesetFromFTPServer = ""; + getMissingTechtreeFromFTPServer = ""; + getMissingMapFromFTPServerLastPrompted = 0; + getMissingTilesetFromFTPServerLastPrompted = 0; + getMissingTechtreeFromFTPServerLastPrompted = 0; + + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface == NULL) { + throw megaglest_runtime_error("clientInterface == NULL"); + } + if (getInProgressSavedGameFromFTPServerInProgress == true) { + if (clientInterface != NULL) { + clientInterface->close(); + return; + } + } + + getInProgressSavedGameFromFTPServer = ""; + getInProgressSavedGameFromFTPServerInProgress = false; + + string serverUrl = clientInterface->getServerIpAddress(); + int + portNumber = clientInterface->getServerFTPPort(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] Using FTP port #: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, portNumber); + + Config & config = Config::getInstance(); + vector < string > mapPathList = config.getPathListForType(ptMaps); + std::pair < string, string > mapsPath; + if (mapPathList.empty() == false) { + mapsPath.first = mapPathList[0]; + } + if (mapPathList.size() > 1) { + mapsPath.second = mapPathList[1]; + } + std::pair < string, string > tilesetsPath; + vector < string > tilesetsList = + Config::getInstance().getPathListForType(ptTilesets); + if (tilesetsList.empty() == false) { + tilesetsPath.first = tilesetsList[0]; + if (tilesetsList.size() > 1) { + tilesetsPath.second = tilesetsList[1]; + } + } + + std::pair < string, string > techtreesPath; + vector < string > techtreesList = + Config::getInstance().getPathListForType(ptTechs); + if (techtreesList.empty() == false) { + techtreesPath.first = techtreesList[0]; + if (techtreesList.size() > 1) { + techtreesPath.second = techtreesList[1]; + } + } + + std::pair < string, string > scenariosPath; + vector < string > scenariosList = + Config::getInstance().getPathListForType(ptScenarios); + if (scenariosList.empty() == false) { + scenariosPath.first = scenariosList[0]; + if (scenariosList.size() > 1) { + scenariosPath.second = scenariosList[1]; + } + } + + string + fileArchiveExtension = + config.getString("FileArchiveExtension", ""); + string + fileArchiveExtractCommand = + config.getString("FileArchiveExtractCommand", ""); + string + fileArchiveExtractCommandParameters = + config.getString("FileArchiveExtractCommandParameters", ""); + int32 + fileArchiveExtractCommandSuccessResult = + config.getInt("FileArchiveExtractCommandSuccessResult", "0"); + + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); + + ftpClientThread = new FTPClientThread(portNumber, serverUrl, + mapsPath, tilesetsPath, + techtreesPath, scenariosPath, + this, fileArchiveExtension, + fileArchiveExtractCommand, + fileArchiveExtractCommandParameters, + fileArchiveExtractCommandSuccessResult, + tempFilePath); + ftpClientThread->start(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("CancelDownloadsMsg", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("CancelDownloadsMsg", + languageList[i]).c_str(), + getHumanPlayerName().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s cancelled all file downloads.", + getHumanPlayerName().c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + } + } else if (buttonDisconnect.mouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + disconnectFromServer(); + networkManager.end(); + returnToJoinMenu(); + return; + } + + if (initialSettingsReceivedFromServer == false) { + return; + } + + if (activeInputLabel != NULL + && activeInputLabel->mouseClick(x, y) == false) { + setActiveInputLabel(NULL); + } + + // Only allow changes after we get game settings from the server + if (clientInterface != NULL && clientInterface->isConnected() == true) { + int + myCurrentIndex = -1; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { // find my current index by looking at editable listBoxes + if ( //listBoxFactions[i].getEditable() && + clientInterface-> + getGameSettings()->getStartLocationIndex(clientInterface-> + getGameSettings + ()->getThisFactionIndex + ()) == i) { + myCurrentIndex = i; + } + } + + //printf("myCurrentIndex = %d thisFactionIndex = %d\n",myCurrentIndex,clientInterface->getGameSettings()->getThisFactionIndex()); + + if (myCurrentIndex != -1) + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (listBoxFactions[i].getEditable() && + clientInterface-> + getGameSettings()->getStartLocationIndex(clientInterface-> + getGameSettings + ()->getThisFactionIndex + ()) == i) { + if (listBoxFactions[i].mouseClick + (x, y, advanceToItemStartingWith)) { + soundRenderer.playFx(coreData.getClickSoundA()); + ClientInterface * + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface->isConnected()) { + clientInterface->setGameSettingsReceived(false); + clientInterface-> + sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem + (), i, -1, + listBoxTeams + [i].getSelectedItemIndex(), + getHumanPlayerName(), + getNetworkPlayerStatus(), + switchSetupRequestFlagType, + lang.getLanguage()); + switchSetupRequestFlagType = ssrft_None; + noReceiveTimer = time(NULL); + } + break; + } + } + if (listBoxTeams[i].getEditable() && + clientInterface-> + getGameSettings()->getStartLocationIndex(clientInterface-> + getGameSettings + ()->getThisFactionIndex + ()) == i) { + if (listBoxTeams[i].mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (clientInterface->isConnected()) { + clientInterface->setGameSettingsReceived(false); + clientInterface-> + sendSwitchSetupRequest(listBoxFactions[i].getSelectedItem + (), i, -1, + listBoxTeams + [i].getSelectedItemIndex(), + getHumanPlayerName(), + getNetworkPlayerStatus(), + switchSetupRequestFlagType, + lang.getLanguage()); + switchSetupRequestFlagType = ssrft_None; + } + break; + } + } + + bool canGrabSlot = false; + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL + && clientInterface->getJoinGameInProgress() == true) { + canGrabSlot = + ((listBoxControls[i].getSelectedItemIndex() == ctNetwork + && labelNetStatus[i].getText() == + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) + || (listBoxControls[i].getSelectedItemIndex() != ctHuman + && listBoxControls[i].getSelectedItemIndex() != ctClosed + && listBoxControls[i].getSelectedItemIndex() != + ctNetwork)); + } else { + canGrabSlot = + (listBoxControls[i].getSelectedItemIndex() == ctNetwork + && labelNetStatus[i].getText() == + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + + if (canGrabSlot == true) { + if (clientInterface != NULL && i < mapInfo.players + && grabSlotButton[i].mouseClick(x, y)) { + //printf("Send slot switch request for slot = %d, myCurrentIndex = %d\n",i,myCurrentIndex); + + soundRenderer.playFx(coreData.getClickSoundB()); + clientInterface->setGameSettingsReceived(false); + settingsReceivedFromServer = false; + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] sending a switchSlot request from %d to %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + clientInterface->getGameSettings + ()->getThisFactionIndex(), i); + + //printf("Switch slot from %d to %d\n",myCurrentIndex,i); + + string + desiredFactionName = + listBoxFactions[myCurrentIndex].getSelectedItem(); + int + desiredTeamIndex = + listBoxTeams[myCurrentIndex].getSelectedItemIndex(); + if (checkBoxScenario.getValue() == true) { + desiredFactionName = listBoxFactions[i].getSelectedItem(); + desiredTeamIndex = listBoxTeams[i].getSelectedItemIndex(); + } + + //printf("Sending switch slot request to server...\n"); + + clientInterface->sendSwitchSetupRequest(desiredFactionName, + myCurrentIndex, + i, + desiredTeamIndex, + getHumanPlayerName + (), + getNetworkPlayerStatus + (), + switchSetupRequestFlagType, + lang.getLanguage()); + labelPlayerNames[myCurrentIndex].setText(""); + labelPlayerNames[i].setText(""); + switchSetupRequestFlagType = ssrft_None; + break; + } + } + + if (labelPlayerNames[i].mouseClick(x, y) + && (activeInputLabel != &labelPlayerNames[i])) { + if (clientInterface != NULL + && i == clientInterface->getPlayerIndex()) { + setActiveInputLabel(&labelPlayerNames[i]); + } + } + } + + if (listBoxPlayerStatus.mouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + soundRenderer.playFx(coreData.getClickSoundC()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (getNetworkPlayerStatus() == npst_PickSettings) { + listBoxPlayerStatus.setTextColor(Vec3f(1.0f, 0.0f, 0.0f)); + listBoxPlayerStatus.setLighted(true); + } else if (getNetworkPlayerStatus() == npst_BeRightBack) { + listBoxPlayerStatus.setTextColor(Vec3f(1.0f, 1.0f, 0.0f)); + listBoxPlayerStatus.setLighted(true); + } else if (getNetworkPlayerStatus() == npst_Ready) { + listBoxPlayerStatus.setTextColor(Vec3f(0.0f, 1.0f, 0.0f)); + listBoxPlayerStatus.setLighted(false); + } + + ClientInterface * + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected()) { + clientInterface->setGameSettingsReceived(false); + clientInterface->sendSwitchSetupRequest(listBoxFactions + [clientInterface-> + getPlayerIndex()]. + getSelectedItem(), + clientInterface-> + getPlayerIndex(), -1, + listBoxTeams + [clientInterface->getPlayerIndex + ()].getSelectedItemIndex + (), + getHumanPlayerName(), + getNetworkPlayerStatus + (), + switchSetupRequestFlagType, + lang.getLanguage()); + switchSetupRequestFlagType = ssrft_None; + } + } + + if (isHeadlessAdmin() == true) { + //printf("#1 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); + mouseClickAdmin(x, y, mouseButton, advanceToItemStartingWith); + } else if (clientInterface != NULL + && clientInterface->getJoinGameInProgress() == true) { + if (buttonPlayNow.mouseClick(x, y) && buttonPlayNow.getEnabled()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + uint32 tilesetCRC = lastCheckedCRCTilesetValue; + uint32 techCRC = lastCheckedCRCTechtreeValue; + uint32 mapCRC = lastCheckedCRCMapValue; + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + bool + dataSynchMismatch = + ((mapCRC != 0 && mapCRC != gameSettings->getMapCRC()) + || (tilesetCRC != 0 + && tilesetCRC != gameSettings->getTilesetCRC()) + || (techCRC != 0 && techCRC != gameSettings->getTechCRC())); + if (dataSynchMismatch == false) { + PlayNow(true); + return; + } else { + showMessageBox + ("You cannot start the game because\none or more clients do not have the same game data!", + "Data Mismatch Error", false); + } + } + } + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + bool MenuStateConnectedGame::isHeadlessAdmin() { + bool result = false; + + ClientInterface * + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected()) { + const GameSettings * + settings = clientInterface->getGameSettings(); + if (settings != NULL) { + //printf("#1 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); + + if (settings->getMasterserver_admin() == + clientInterface->getSessionKey()) { + result = true; + } + } + } + + return result; + } + + void + MenuStateConnectedGame::broadCastGameSettingsToHeadlessServer(bool + forceNow) { + if (isHeadlessAdmin() == false) { + return; + } + + if (forceNow == true || + ((needToBroadcastServerSettings == true) + && + (difftime + ((long int) time(NULL), + broadcastServerSettingsDelayTimer) >= + HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS))) { + //printf("In [%s:%s] Line: %d forceNow = %d broadcastServerSettingsDelayTimer = " MG_SIZE_T_SPECIFIER ", now =" MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,forceNow,broadcastServerSettingsDelayTimer,time(NULL)); + + needToBroadcastServerSettings = false; + broadcastServerSettingsDelayTimer = time(NULL); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + } + } + for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork) { + listBoxControls[i].setSelectedItemIndex(ctNetworkUnassigned); + } + } + + if (validDisplayedGamesettings) { + loadGameSettings(&displayedGamesettings); + + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("broadcast settings:\n%s\n", + displayedGamesettings.toString().c_str()); + + //printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); + //printf("sending Settings map filter=%d\n",displayedGamesettings.getMapFilter()); + clientInterface->broadcastGameSetup(&displayedGamesettings); + noReceiveTimer = time(NULL); + } + } + } + + void + MenuStateConnectedGame::updateResourceMultiplier(const int index) { + ControlType + ct = + static_cast + (listBoxControls[index].getSelectedItemIndex()); + if (ct == ctCpuEasy || ct == ctNetworkCpuEasy) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + easyMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(true); + } else if (ct == ctCpu || ct == ctNetworkCpu) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + normalMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(true); + } else if (ct == ctCpuUltra || ct == ctNetworkCpuUltra) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + ultraMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(true); + } else if (ct == ctCpuZeta || ct == ctNetworkCpuZeta) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + megaMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(true); + } else { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + normalMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(false); + } + + listBoxRMultiplier[index]. + setEditable(listBoxRMultiplier[index].getEnabled()); + listBoxRMultiplier[index]. + setVisible(listBoxRMultiplier[index].getEnabled()); + } + + void + MenuStateConnectedGame::mouseClickAdmin(int x, int y, + MouseButton mouseButton, + string advanceToItemStartingWith) { + + try { + int + oldListBoxMapfilterIndex = listBoxMapFilter.getSelectedItemIndex(); + if (buttonPlayNow.mouseClick(x, y) && buttonPlayNow.getEnabled()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + PlayNow(true); + return; + } else if (buttonRestoreLastSettings.mouseClick(x, y) + && buttonRestoreLastSettings.getEnabled()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.playFx(coreData.getClickSoundB()); + + RestoreLastGameSettings(); + } else if (checkBoxAllowNativeLanguageTechtree.mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxMap.mouseClick(x, y, advanceToItemStartingWith)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + getCurrentMapFile().c_str()); + + if (loadMapInfo + (Config::getMapPath(getCurrentMapFile(), "", false), &mapInfo, + true) == true) { + labelMapInfo.setText(mapInfo.desc); + } else { + labelMapInfo.setText("???"); + } + + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxFogOfWar.mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (checkBoxAllowObservers.mouseClick(x, y)) { #ifdef DEBUG - PRINT_DEBUG ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); + PRINT_DEBUG("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); #endif - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (checkBoxEnableSwitchTeamMode.mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxAISwitchTeamAcceptPercent.getEnabled () - && listBoxAISwitchTeamAcceptPercent.mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxFallbackCpuMultiplier.getEnabled () - && listBoxFallbackCpuMultiplier.mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxTileset.mouseClick (x, y, advanceToItemStartingWith)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (checkBoxEnableSwitchTeamMode.mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxAISwitchTeamAcceptPercent.getEnabled() + && listBoxAISwitchTeamAcceptPercent.mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxFallbackCpuMultiplier.getEnabled() + && listBoxFallbackCpuMultiplier.mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxTileset.mouseClick(x, y, advanceToItemStartingWith)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } - else if (listBoxMapFilter.mouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - getCurrentMapFile ().c_str ()); - switchToNextMapGroup (listBoxMapFilter.getSelectedItemIndex () - - oldListBoxMapfilterIndex); - if (loadMapInfo - (Config::getMapPath (getCurrentMapFile (), "", false), &mapInfo, - true) == true) - { - labelMapInfo.setText (mapInfo.desc); - } - else - { - labelMapInfo.setText ("???"); - } - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (listBoxTechTree.mouseClick (x, y, advanceToItemStartingWith)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - reloadFactions (false, ""); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } + else if (listBoxMapFilter.mouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + getCurrentMapFile().c_str()); + switchToNextMapGroup(listBoxMapFilter.getSelectedItemIndex() - + oldListBoxMapfilterIndex); + if (loadMapInfo + (Config::getMapPath(getCurrentMapFile(), "", false), &mapInfo, + true) == true) { + labelMapInfo.setText(mapInfo.desc); + } else { + labelMapInfo.setText("???"); + } + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (listBoxTechTree.mouseClick(x, y, advanceToItemStartingWith)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + reloadFactions(false, ""); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } - else if (checkBoxAllowTeamUnitSharing.mouseClick (x, y)) - { - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (checkBoxAllowTeamResourceSharing.mouseClick (x, y)) - { - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); + else if (checkBoxAllowTeamUnitSharing.mouseClick(x, y)) { + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (checkBoxAllowTeamResourceSharing.mouseClick(x, y)) { + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); - for (int i = 0; i < mapInfo.players; ++i) - { -// set multiplier - if (listBoxRMultiplier[i].mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); -//broadCastGameSettingsToMasterserver(); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } + for (int i = 0; i < mapInfo.players; ++i) { + // set multiplier + if (listBoxRMultiplier[i].mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + //broadCastGameSettingsToMasterserver(); + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } -//ensure that only 1 human player is present - if (clientInterface != NULL - && clientInterface->getGameSettings () != NULL - && clientInterface-> - getGameSettings ()->getStartLocationIndex (clientInterface-> - getGameSettings - ()->getThisFactionIndex - ()) != i - && listBoxControls[i].mouseClick (x, y)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); + //ensure that only 1 human player is present + if (clientInterface != NULL + && clientInterface->getGameSettings() != NULL + && clientInterface-> + getGameSettings()->getStartLocationIndex(clientInterface-> + getGameSettings + ()->getThisFactionIndex + ()) != i + && listBoxControls[i].mouseClick(x, y)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); -//!! this must be done two times!"" - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - listBoxControls[i].mouseClick (x, y); - } - if ((isHeadlessAdmin () == true) - && (listBoxControls[i].getSelectedItemIndex () == ctHuman)) - { - listBoxControls[i].mouseClick (x, y); - } -//!! this must be done two times!"" - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - listBoxControls[i].mouseClick (x, y); - } + //!! this must be done two times!"" + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + listBoxControls[i].mouseClick(x, y); + } + if ((isHeadlessAdmin() == true) + && (listBoxControls[i].getSelectedItemIndex() == ctHuman)) { + listBoxControls[i].mouseClick(x, y); + } + //!! this must be done two times!"" + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + listBoxControls[i].mouseClick(x, y); + } - updateResourceMultiplier (i); + updateResourceMultiplier(i); - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (clientInterface != NULL - && clientInterface-> - getGameSettings ()->getStartLocationIndex - (clientInterface->getGameSettings ()->getThisFactionIndex - ()) != i - && listBoxFactions[i].mouseClick (x, y, - advanceToItemStartingWith)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); -// Disallow CPU players to be observers - if (factionFiles[listBoxFactions[i].getSelectedItemIndex ()] == - formatString (GameConstants::OBSERVER_SLOTNAME) - && (listBoxControls[i].getSelectedItemIndex () == ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - listBoxFactions[i].setSelectedItemIndex (0); - } + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (clientInterface != NULL + && clientInterface-> + getGameSettings()->getStartLocationIndex + (clientInterface->getGameSettings()->getThisFactionIndex + ()) != i + && listBoxFactions[i].mouseClick(x, y, + advanceToItemStartingWith)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + // Disallow CPU players to be observers + if (factionFiles[listBoxFactions[i].getSelectedItemIndex()] == + formatString(GameConstants::OBSERVER_SLOTNAME) + && (listBoxControls[i].getSelectedItemIndex() == ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + listBoxFactions[i].setSelectedItemIndex(0); + } - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - else if (clientInterface != NULL - && clientInterface-> - getGameSettings ()->getStartLocationIndex - (clientInterface->getGameSettings ()->getThisFactionIndex - ()) != i && listBoxTeams[i].mouseClick (x, y)) - { + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } else if (clientInterface != NULL + && clientInterface-> + getGameSettings()->getStartLocationIndex + (clientInterface->getGameSettings()->getThisFactionIndex + ()) != i && listBoxTeams[i].mouseClick(x, y)) { #ifdef DEBUG - PRINT_DEBUG ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); + PRINT_DEBUG("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); #endif - if (factionFiles[listBoxFactions[i].getSelectedItemIndex ()] != - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - if (listBoxTeams[i].getSelectedItemIndex () + 1 != - (GameConstants::maxPlayers + fpt_Observer)) - { -//lastSelectedTeamIndex[i] = listBoxTeams[i].getSelectedItemIndex(); - } - } - - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - } - } - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showMessageBox (szBuf, "Error", false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void - MenuStateConnectedGame::PlayNow (bool saveGame) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - - GameSettings gameSettings = *clientInterface->getGameSettings (); - loadGameSettings (&gameSettings); - - if (saveGame == true) - { - CoreData:: - getInstance ().saveGameSettingsToFile (HEADLESS_SAVED_GAME_FILENAME, - &gameSettings, true); - } - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.playFx (coreData.getClickSoundC ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); - - if (clientInterface->getJoinGameInProgress () == true) - { - if (readyToJoinInProgressGame == false && launchingNewGame == false) - { - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("JoinPlayerToCurrentGameLaunch", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("JoinPlayerToCurrentGameLaunch", - languageList[i]).c_str (), - getHumanPlayerName ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is about to join the game, please wait...", - getHumanPlayerName ().c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - - sleep (1); - launchingNewGame = true; - clientInterface->broadcastGameStart (&gameSettings); - } - return; - } - else - { - launchingNewGame = true; - broadCastGameSettingsToHeadlessServer (needToBroadcastServerSettings); - clientInterface->broadcastGameStart (&gameSettings); - } - } - - void - MenuStateConnectedGame::switchToNextMapGroup (const int direction) - { - int - i = listBoxMapFilter.getSelectedItemIndex (); -// if there are no maps for the current selection we switch to next selection - while (formattedPlayerSortedMaps[i].empty ()) - { - i = i + direction; - if (i > GameConstants::maxPlayers) - { - i = 0; - } - if (i < 0) - { - i = GameConstants::maxPlayers; - } - } - switchToMapGroup (i); - } - - void - MenuStateConnectedGame::switchToMapGroup (int filterIndex) - { - int - i = filterIndex; - listBoxMapFilter.setSelectedItemIndex (i); - listBoxMap.setItems (formattedPlayerSortedMaps[i]); -// printf("switching map group to filter=%d mapgroup has %d maps. map=%s \n",i, -// (int)formattedPlayerSortedMaps[i].size(),formattedPlayerSortedMaps[i][0].c_str()); - } - - string MenuStateConnectedGame::getCurrentMapFile () - { - int - i = listBoxMapFilter.getSelectedItemIndex (); - int - mapIndex = listBoxMap.getSelectedItemIndex (); - if (playerSortedMaps[i].empty () == false) - { - return playerSortedMaps[i].at (mapIndex); - } - return ""; - } - - void - MenuStateConnectedGame::reloadFactions (bool keepExistingSelectedItem, - string scenario) - { - vector < string > results; - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > techPaths = - config.getPathListForType (ptTechs, scenarioDir); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - - if (listBoxTechTree.getSelectedItemIndex () >= 0 - && listBoxTechTree.getSelectedItemIndex () < - (int) techTreeFiles.size ()) - { - findDirs (techPath + - techTreeFiles[listBoxTechTree.getSelectedItemIndex ()] + - "/factions/", results, false, false); - } - if (results.empty () == false) - { - break; - } - } - - if (results.empty () == true) - { -//throw megaglest_runtime_error("(2)There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"); -//showGeneralError=true; -//generalErrorToShow = "[#2] There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"; - } - - vector < string > translatedFactionNames; - factionFiles = results; - for (int i = 0; i < (int) results.size (); ++i) - { - results[i] = formatString (results[i]); - string - translatedString = - techTree->getTranslatedFactionName (techTreeFiles - [listBoxTechTree. - getSelectedItemIndex ()], - factionFiles[i]); - if (toLower (translatedString) == toLower (results[i])) - { - translatedFactionNames.push_back (results[i]); - } - else - { - translatedFactionNames.push_back (results[i] + " (" + - translatedString + ")"); - } -//printf("FACTIONS i = %d results [%s]\n",i,results[i].c_str()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "Tech [%s] has faction [%s]\n", - techTreeFiles - [listBoxTechTree.getSelectedItemIndex - ()].c_str (), results[i].c_str ()); - } - - results.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Random", "", - true) + "*"); - -// Add special Observer Faction - if (checkBoxAllowObservers.getValue () == 1) - { - results.push_back (formatString (GameConstants::OBSERVER_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::OBSERVER_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Observer", "", - true) + "*"); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - int - originalIndex = listBoxFactions[i].getSelectedItemIndex (); - string - originalValue = - (listBoxFactions[i].getItemCount () > - 0 ? listBoxFactions[i].getSelectedItem () : ""); - - listBoxFactions[i].setItems (results, translatedFactionNames); - if (keepExistingSelectedItem == false || - (checkBoxAllowObservers.getValue () == true && - originalValue == - formatString (GameConstants::OBSERVER_SLOTNAME))) - { - listBoxFactions[i].setSelectedItemIndex (i % results.size ()); - - if (originalValue == formatString (GameConstants::OBSERVER_SLOTNAME) - && listBoxFactions[i].getSelectedItem () != - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - if (listBoxTeams[i].getSelectedItem () == - intToStr (GameConstants::maxPlayers + fpt_Observer)) - { - listBoxTeams[i].setSelectedItem (intToStr (1)); - } - } - } - else if (originalIndex < (int) results.size ()) - { - listBoxFactions[i].setSelectedItemIndex (originalIndex); - } - } - } - - void - MenuStateConnectedGame::loadGameSettings (GameSettings * gameSettings) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - int - factionCount = 0; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// Test flags values -//gameSettings->setFlagTypes1(ft1_show_map_resources); -// - - if (checkBoxScenario.getValue () == true) - { - gameSettings->setScenario (scenarioInfo.name); - gameSettings->setScenarioDir (Scenario::getScenarioPath - (dirList, scenarioInfo.name)); - - gameSettings->setDefaultResources (scenarioInfo.defaultResources); - gameSettings->setDefaultUnits (scenarioInfo.defaultUnits); - gameSettings-> - setDefaultVictoryConditions (scenarioInfo.defaultVictoryConditions); - } - else - { - gameSettings->setScenario (""); - gameSettings->setScenarioDir (""); - } - - gameSettings->setNetworkAllowNativeLanguageTechtree - (checkBoxAllowNativeLanguageTechtree.getValue ()); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line %d] listBoxMap.getSelectedItemIndex() = %d, mapFiles.size() = " - MG_SIZE_T_SPECIFIER ", getCurrentMapFile() [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, listBoxMap.getSelectedItemIndex (), mapFiles.size (), - getCurrentMapFile ().c_str ()); - - if (listBoxMap.getSelectedItemIndex () >= 0 - && listBoxMap.getSelectedItemIndex () < (int) mapFiles.size ()) - { - gameSettings->setDescription (formatString (getCurrentMapFile ())); - gameSettings->setMap (getCurrentMapFile ()); - gameSettings->setMapFilter (listBoxMapFilter.getSelectedItemIndex ()); - } - else - { - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingMap=Player", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingMap=Player", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - listBoxMap.getSelectedItem ().c_str ()); - } - else - { - snprintf (szMsg, 8096, "Player: %s is missing the map: %s", - getHumanPlayerName ().c_str (), - listBoxMap.getSelectedItem ().c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - } - - if (listBoxTileset.getSelectedItemIndex () >= 0 - && listBoxTileset.getSelectedItemIndex () < - (int) tilesetFiles.size ()) - { - gameSettings->setTileset (tilesetFiles - [listBoxTileset.getSelectedItemIndex ()]); - } - else - { -//printf("A loadGameSettings listBoxTileset.getSelectedItemIndex() = %d tilesetFiles.size() = %d\n",listBoxTileset.getSelectedItemIndex(),tilesetFiles.size()); - - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTileset=Player", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTileset=Player", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - listBoxTileset.getSelectedItem ().c_str ()); - } - else - { - snprintf (szMsg, 8096, "Player: %s is missing the tileset: %s", - getHumanPlayerName ().c_str (), - listBoxTileset.getSelectedItem ().c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - } - if (listBoxTechTree.getSelectedItemIndex () >= 0 - && listBoxTechTree.getSelectedItemIndex () < - (int) techTreeFiles.size ()) - { - gameSettings->setTech (techTreeFiles - [listBoxTechTree.getSelectedItemIndex ()]); - } - else - { - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtree=Player", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTechtree=Player", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - listBoxTechTree.getSelectedItem ().c_str ()); - } - else - { - snprintf (szMsg, 8096, "Player: %s is missing the techtree: %s", - getHumanPlayerName ().c_str (), - listBoxTechTree.getSelectedItem ().c_str ()); - } - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (szMsg, -1, localEcho, - languageList[i]); - } - } - - if (checkBoxScenario.getValue () == false) - { - gameSettings->setDefaultUnits (true); - gameSettings->setDefaultResources (true); - gameSettings->setDefaultVictoryConditions (true); - } - - gameSettings->setFogOfWar (listBoxFogOfWar.getSelectedItemIndex () == 0 - || listBoxFogOfWar.getSelectedItemIndex () == - 1); - - gameSettings->setAllowObservers (checkBoxAllowObservers.getValue () == - true); - - uint32 valueFlags1 = gameSettings->getFlagTypes1 (); - if (listBoxFogOfWar.getSelectedItemIndex () == 1 || - listBoxFogOfWar.getSelectedItemIndex () == 2) - { - valueFlags1 |= ft1_show_map_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_show_map_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - -//gameSettings->setEnableObserverModeAtEndGame(listBoxEnableObserverMode.getSelectedItemIndex() == 0); - gameSettings->setEnableObserverModeAtEndGame (true); -//gameSettings->setPathFinderType(static_cast(listBoxPathFinderType.getSelectedItemIndex())); - - valueFlags1 = gameSettings->getFlagTypes1 (); - if (checkBoxEnableSwitchTeamMode.getValue () == true) - { - valueFlags1 |= ft1_allow_team_switching; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_team_switching; - gameSettings->setFlagTypes1 (valueFlags1); - } - gameSettings->setAiAcceptSwitchTeamPercentChance (strToInt - (listBoxAISwitchTeamAcceptPercent.getSelectedItem - ())); - gameSettings-> - setFallbackCpuMultiplier - (listBoxFallbackCpuMultiplier.getSelectedItemIndex ()); - - valueFlags1 = gameSettings->getFlagTypes1 (); - if (checkBoxAllowTeamUnitSharing.getValue () == true) - { - valueFlags1 |= ft1_allow_shared_team_units; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_shared_team_units; - gameSettings->setFlagTypes1 (valueFlags1); - } - - valueFlags1 = gameSettings->getFlagTypes1 (); - if (checkBoxAllowTeamResourceSharing.getValue () == true) - { - valueFlags1 |= ft1_allow_shared_team_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_shared_team_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - -// First save Used slots -//for(int i=0; i - (listBoxControls[i].getSelectedItemIndex ()); - - if (ct != ctClosed) - { - int - slotIndex = factionCount; - ControlType - oldControlType = gameSettings->getFactionControl (slotIndex); - gameSettings->setFactionControl (slotIndex, ct); - if (ct == ctHuman) - { -//if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, slotIndex = %d, getHumanPlayerName(i) [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,slotIndex,getHumanPlayerName(i).c_str()); - - gameSettings->setThisFactionIndex (slotIndex); - gameSettings->setNetworkPlayerName (slotIndex, - getHumanPlayerName ()); - gameSettings->setNetworkPlayerUUID (slotIndex, - Config:: - getInstance ().getString - ("PlayerId", "")); - gameSettings->setNetworkPlayerPlatform (slotIndex, - getPlatformNameString ()); - gameSettings->setNetworkPlayerStatuses (slotIndex, - getNetworkPlayerStatus - ()); - Lang & lang = Lang::getInstance (); - gameSettings->setNetworkPlayerLanguages (slotIndex, - lang.getLanguage ()); - - gameSettings->setResourceMultiplierIndex (slotIndex, 5); - } - else - { - gameSettings->setResourceMultiplierIndex (slotIndex, - listBoxRMultiplier - [i].getSelectedItemIndex - ()); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()].c_str ()); - - gameSettings->setFactionTypeName (slotIndex, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()]); - if (factionFiles[listBoxFactions[i].getSelectedItemIndex ()] == - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - listBoxTeams[i].setSelectedItem (intToStr - (GameConstants::maxPlayers + - fpt_Observer)); - } - - gameSettings->setTeam (slotIndex, - listBoxTeams[i].getSelectedItemIndex ()); - gameSettings->setStartLocationIndex (slotIndex, i); -//printf("!!! setStartLocationIndex #1 slotIndex = %d, i = %d\n",slotIndex, i); - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - if (oldControlType != ctNetwork - && oldControlType != ctNetworkUnassigned) - { - gameSettings->setNetworkPlayerName (slotIndex, ""); - } - } - else if (listBoxControls[i].getSelectedItemIndex () != ctHuman) - { - AIPlayerCount++; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, playername is AI (blank)\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i); - - Lang & lang = Lang::getInstance (); - gameSettings->setNetworkPlayerName (slotIndex, - lang.getString ("AI") + - intToStr (AIPlayerCount)); - labelPlayerNames[i].setText (""); - } - - factionCount++; - } - else - { - labelPlayerNames[i].setText (""); - } - } - -// Next save closed slots - int - closedCount = 0; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ControlType - ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct == ctClosed) - { - int - slotIndex = factionCount + closedCount; - - gameSettings->setFactionControl (slotIndex, ct); - gameSettings->setTeam (slotIndex, - listBoxTeams[i].getSelectedItemIndex ()); - gameSettings->setStartLocationIndex (slotIndex, i); -//printf("!!! setStartLocationIndex #2 slotIndex = %d, i = %d\n",slotIndex, i); - - gameSettings->setResourceMultiplierIndex (slotIndex, 5); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()].c_str ()); - - gameSettings->setFactionTypeName (slotIndex, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()]); - gameSettings->setNetworkPlayerStatuses (slotIndex, npst_None); - gameSettings->setNetworkPlayerName (slotIndex, - GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME); - - closedCount++; - } - } - - gameSettings->setFactionCount (factionCount); - - Config & config = Config::getInstance (); - gameSettings->setEnableServerControlledAI (config.getBool - ("ServerControlledAI", - "true")); - gameSettings-> - setNetworkFramePeriod (config.getInt ("NetworkSendFrameCount", "20")); - - if (hasNetworkGameSettings () == true) - { - if (gameSettings->getTileset () != "") - { - if (lastCheckedCRCTilesetName != gameSettings->getTileset ()) - { -//console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); - lastCheckedCRCTilesetValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + gameSettings->getTileset () + string ("/*"), - ".xml", NULL); - if (lastCheckedCRCTilesetValue == 0 - || lastCheckedCRCTilesetValue != - gameSettings->getTilesetCRC ()) - { - lastCheckedCRCTilesetValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + gameSettings->getTileset () + string ("/*"), - ".xml", NULL, true); - } - lastCheckedCRCTilesetName = gameSettings->getTileset (); - } - gameSettings->setTilesetCRC (lastCheckedCRCTilesetValue); - } - - if (config.getBool ("DisableServerLobbyTechtreeCRCCheck", "false") == - false) - { - if (gameSettings->getTech () != "") - { - if (lastCheckedCRCTechtreeName != gameSettings->getTech ()) - { -//console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); - lastCheckedCRCTechtreeValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/*", ".xml", NULL); - if (lastCheckedCRCTechtreeValue == 0 - || lastCheckedCRCTechtreeValue != - gameSettings->getTechCRC ()) - { - lastCheckedCRCTechtreeValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/*", ".xml", NULL, true); - } - - reloadFactions (true, gameSettings->getScenario ()); - factionCRCList.clear (); - for (unsigned int factionIdx = 0; - factionIdx < factionFiles.size (); ++factionIdx) - { - string factionName = factionFiles[factionIdx]; - if (factionName != GameConstants::RANDOMFACTION_SLOTNAME && - factionName != GameConstants::OBSERVER_SLOTNAME) - { -//factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); - uint32 - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/factions/" + - factionName + "/*", ".xml", NULL); - if (factionCRC == 0) - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/factions/" + - factionName + "/*", ".xml", NULL, true); - } - factionCRCList.push_back (make_pair - (factionName, factionCRC)); - } - } -//console.addLine("Found factions: " + intToStr(factionCRCList.size())); - lastCheckedCRCTechtreeName = gameSettings->getTech (); - } - - gameSettings->setFactionCRCList (factionCRCList); - gameSettings->setTechCRC (lastCheckedCRCTechtreeValue); - } - } - - if (gameSettings->getMap () != "") - { - if (lastCheckedCRCMapName != gameSettings->getMap ()) - { - Checksum checksum; - string - file = Config::getMapPath (gameSettings->getMap (), "", false); -//console.addLine("Checking map CRC [" + file + "]"); - checksum.addFile (file); - lastCheckedCRCMapValue = checksum.getSum (); - lastCheckedCRCMapName = gameSettings->getMap (); - } - gameSettings->setMapCRC (lastCheckedCRCMapValue); - } - } - -//replace server player by network - for (int i = 0; i < gameSettings->getFactionCount (); ++i) - { -//replace by network - if (gameSettings->getFactionControl (i) == ctHuman) - { - gameSettings->setFactionControl (i, ctNetwork); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void - MenuStateConnectedGame::returnToJoinMenu () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (modHttpServerThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - modHttpServerThread->setSimpleTaskInterfaceValid (false); - modHttpServerThread->signalQuit (); -//modHttpServerThread->setThreadOwnerValid(false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (modHttpServerThread->canShutdown (true) == true - && modHttpServerThread->shutdownAndWait () == true) - { - delete modHttpServerThread; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - modHttpServerThread = NULL; - } - - if (ftpClientThread != NULL) - { - - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - } - ftpClientThread = NULL; - } - - if (returnMenuInfo == jmSimple) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - launchingNewGame = true; - disconnectFromServer (); - mainMenu->setState (new MenuStateJoinGame (program, mainMenu)); - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - launchingNewGame = true; - disconnectFromServer (); - mainMenu->setState (new MenuStateMasterserver (program, mainMenu)); - } - } - - void - MenuStateConnectedGame::mouseMove (int x, int y, const MouseState * ms) - { - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - - if (ftpMessageBox.getEnabled ()) - { - ftpMessageBox.mouseMove (x, y); - } - - buttonCancelDownloads.mouseMove (x, y); - buttonDisconnect.mouseMove (x, y); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxControls[i].mouseMove (x, y); - listBoxFactions[i].mouseMove (x, y); - listBoxTeams[i].mouseMove (x, y); - grabSlotButton[i].mouseMove (x, y); - } - - listBoxMap.mouseMove (x, y); - listBoxFogOfWar.mouseMove (x, y); - checkBoxAllowObservers.mouseMove (x, y); - listBoxTileset.mouseMove (x, y); - listBoxMapFilter.mouseMove (x, y); - listBoxTechTree.mouseMove (x, y); - listBoxPlayerStatus.mouseMove (x, y); - - checkBoxScenario.mouseMove (x, y); - listBoxScenario.mouseMove (x, y); - - labelAllowTeamUnitSharing.mouseMove (x, y); - checkBoxAllowTeamUnitSharing.mouseMove (x, y); - labelAllowTeamResourceSharing.mouseMove (x, y); - checkBoxAllowTeamResourceSharing.mouseMove (x, y); - - checkBoxAllowNativeLanguageTechtree.mouseMove (x, y); - - buttonPlayNow.mouseMove (x, y); - buttonRestoreLastSettings.mouseMove (x, y); - } - - bool MenuStateConnectedGame::isVideoPlaying () - { - bool result = false; - if (factionVideo != NULL) - { - result = factionVideo->isPlaying (); - } - return result; - } - - void - MenuStateConnectedGame::render () - { - try - { - Renderer & renderer = Renderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - - renderer.renderButton (&buttonDisconnect); - - if (initialSettingsReceivedFromServer == false) - { - return; - } - - if (factionTexture != NULL) - { - if (factionVideo == NULL || factionVideo->isPlaying () == false) - { - renderer.renderTextureQuad (800, 600, 200, 150, factionTexture, - 1); - } - } - if (factionVideo != NULL) - { - if (factionVideo->isPlaying () == true) - { - factionVideo->playFrame (false); - } - else - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - - if (validDisplayedGamesettings) - { - initFactionPreview (&displayedGamesettings); - } - } - } - } - } - - if (mapPreviewTexture != NULL) - { -//renderer.renderTextureQuad(5,185,150,150,mapPreviewTexture,1.0f); - renderer.renderTextureQuad (this->render_mapPreviewTexture_X, - this->render_mapPreviewTexture_Y, - this->render_mapPreviewTexture_W, - this->render_mapPreviewTexture_H, - mapPreviewTexture, 1.0f); - if (this->zoomedMap == true) - { - return; - } -//printf("=================> Rendering map preview texture\n"); - } - - if (scenarioLogoTexture != NULL) - { - renderer.renderTextureQuad (300, 350, 400, 300, scenarioLogoTexture, - 1.0f); -//renderer.renderBackground(scenarioLogoTexture); - } - - renderer.renderButton (&buttonDisconnect); - -// Get a reference to the player texture cache - std::map < int, - Texture2D * >& - crcPlayerTextureCache = - CacheManager::getCachedItem < std::map < int, - Texture2D * > >(GameConstants::playerTextureCacheLookupKey); - -// START - this code ensure player title and player names don't overlap - int - offsetPosition = 0; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - const - Metrics & - metrics = Metrics::getInstance (); - FontMetrics * - fontMetrics = NULL; - if (Renderer::renderText3DEnabled == false) - { - fontMetrics = - CoreData::getInstance ().getMenuFontNormal ()->getMetrics (); - } - else - { - fontMetrics = - CoreData::getInstance ().getMenuFontNormal3D ()->getMetrics (); - } - - if (fontMetrics == NULL) - { - throw megaglest_runtime_error ("fontMetrics == NULL"); - } - int - curWidth = - (metrics.toVirtualX - (fontMetrics->getTextWidth (labelPlayers[i].getText ()))); - - if (labelPlayers[i].getX () + curWidth >= - labelPlayerNames[i].getX ()) - { - int - newOffsetPosition = labelPlayers[i].getX () + curWidth + 2; - if (offsetPosition < newOffsetPosition) - { - offsetPosition = newOffsetPosition; - } - } - } -// END - - renderer.renderListBox (&listBoxPlayerStatus); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - bool renderIt = true; -//printf("Player #%d [%s] control = %d\n",i,labelPlayerNames[i].getText().c_str(),listBoxControls[i].getSelectedItemIndex()); - if (labelNetStatus[i].getText () == - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - renderIt = false; - } - labelPlayers[i].setVisible (renderIt); - labelPlayerNames[i].setVisible (renderIt); - listBoxControls[i].setVisible (renderIt); - listBoxRMultiplier[i].setVisible (renderIt); - listBoxFactions[i].setVisible (renderIt); - listBoxTeams[i].setVisible (renderIt); - labelNetStatus[i].setVisible (renderIt); - } - - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - renderer.renderLabel (&labelPlayerStatus[i]); - } - - if (crcPlayerTextureCache[i] != NULL) - { -// Render the player # label the player's color - - Vec3f - playerColor = - crcPlayerTextureCache[i]->getPixmap ()->getPixel3f (0, 0); - if (clientInterface != NULL - && clientInterface->getGameSettings () != NULL - && clientInterface-> - getGameSettings ()->getMasterserver_admin () > 0 - && clientInterface-> - getGameSettings ()->getMasterserver_admin_faction_index () == - i) - { - - if (difftime ((long int) time (NULL), timerLabelFlash) < 1) - { - renderer.renderLabel (&labelPlayers[i], &playerColor); - } - else - { - Vec4f - flashColor = - Vec4f (playerColor.x, playerColor.y, playerColor.z, 0.45f); - renderer.renderLabel (&labelPlayers[i], &flashColor); - } - - } - else - { - renderer.renderLabel (&labelPlayers[i], &playerColor); - } - -// Blend the color with white so make it more readable -//Vec4f newColor(1.f, 1.f, 1.f, 0.57f); -//renderer.renderLabel(&labelPlayers[i],&newColor); - -//int quadWidth = labelPlayerNames[i].getX() - labelPlayers[i].getX() - 5; -//renderer.renderTextureQuad(labelPlayers[i].getX(), labelPlayers[i].getY(), quadWidth, labelPlayers[i].getH(), crcPlayerTextureCache[i],1.0f,&playerColor); - } - else - { - renderer.renderLabel (&labelPlayers[i]); - } - - if (offsetPosition > 0) - { - labelPlayerNames[i].setX (offsetPosition); - } - - renderer.renderListBox (&listBoxControls[i]); - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - renderer.renderListBox (&listBoxRMultiplier[i]); - renderer.renderListBox (&listBoxFactions[i]); - int - teamnumber = listBoxTeams[i].getSelectedItemIndex (); - Vec3f teamcolor = Vec3f (1.0f, 1.0f, 1.0f); - if (teamnumber >= 0 && teamnumber < 8) - { - teamcolor = - crcPlayerTextureCache[teamnumber]-> - getPixmap ()->getPixel3f (0, 0); - } - listBoxTeams[i].setTextColor (teamcolor); - renderer.renderListBox (&listBoxTeams[i]); - - bool canGrabSlot = false; - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL - && clientInterface->getJoinGameInProgress () == true) - { - canGrabSlot = - ((listBoxControls[i].getSelectedItemIndex () == ctNetwork - && labelNetStatus[i].getText () == - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - || (listBoxControls[i].getSelectedItemIndex () != ctHuman - && listBoxControls[i].getSelectedItemIndex () != ctClosed - && listBoxControls[i].getSelectedItemIndex () != - ctNetwork)); - } - else - { - canGrabSlot = - (listBoxControls[i].getSelectedItemIndex () == ctNetwork - && labelNetStatus[i].getText () == - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); - } - - if (canGrabSlot == true) - { - if (i < mapInfo.players) - { - renderer.renderButton (&grabSlotButton[i]); - } - } - else if (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned - || listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - renderer.renderLabel (&labelNetStatus[i]); - } - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork || - listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned - || listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - if (labelNetStatus[i].getText () != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - renderer.renderLabel (&labelPlayerNames[i]); - } - } - } - } - renderer.renderLabel (&labelStatus); - renderer.renderLabel (&labelInfo); - - if (difftime ((long int) time (NULL), timerLabelFlash) < 1) - { - renderer.renderLabel (&labelDataSynchInfo, &RED); - renderer.renderLabel (&labelWaitingForPlayers, &YELLOW); - } - else - { - renderer.renderLabel (&labelDataSynchInfo, &WHITE); - renderer.renderLabel (&labelWaitingForPlayers, &WHITE); - } - - renderer.renderLabel (&labelMap); - renderer.renderLabel (&labelMapFilter); - renderer.renderLabel (&labelFogOfWar); - renderer.renderLabel (&labelAllowObservers); - renderer.renderLabel (&labelFallbackCpuMultiplier); - renderer.renderLabel (&labelTileset); - renderer.renderLabel (&labelTechTree); - renderer.renderLabel (&labelControl); - renderer.renderLabel (&labelFaction); - renderer.renderLabel (&labelTeam); - renderer.renderLabel (&labelMapInfo); - - renderer.renderListBox (&listBoxMap); - renderer.renderListBox (&listBoxMapFilter); - renderer.renderListBox (&listBoxFogOfWar); - renderer.renderCheckBox (&checkBoxAllowObservers); - renderer.renderListBox (&listBoxTileset); - renderer.renderListBox (&listBoxTechTree); - - renderer.renderLabel (&labelEnableSwitchTeamMode); - renderer.renderLabel (&labelAISwitchTeamAcceptPercent); - - renderer.renderCheckBox (&checkBoxEnableSwitchTeamMode); - renderer.renderListBox (&listBoxAISwitchTeamAcceptPercent); - renderer.renderListBox (&listBoxFallbackCpuMultiplier); - - renderer.renderLabel (&labelAllowTeamUnitSharing); - renderer.renderCheckBox (&checkBoxAllowTeamUnitSharing); - - renderer.renderLabel (&labelAllowTeamResourceSharing); - renderer.renderCheckBox (&checkBoxAllowTeamResourceSharing); - - renderer.renderButton (&buttonPlayNow); - renderer.renderButton (&buttonRestoreLastSettings); - - renderer.renderCheckBox (&checkBoxScenario); - renderer.renderLabel (&labelScenario); - if (checkBoxScenario.getValue () == true) - { - renderer.renderListBox (&listBoxScenario); - } - - renderer.renderLabel (&labelAllowNativeLanguageTechtree); - renderer.renderCheckBox (&checkBoxAllowNativeLanguageTechtree); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - -// !!! START TEMP MV -//renderer.renderButton(&buttonCancelDownloads); -//fileFTPProgressList.clear(); -//fileFTPProgressList["test1a dsa asd asda sdasd asd ad ad"] = make_pair(1,"testa"); -//fileFTPProgressList["test2 asdasdasdadas dasdasdasda"] = make_pair(1,"testb"); -//fileFTPProgressList["test3 asdasdad asd ada dasdadasdada"] = make_pair(1,"testc"); -// !!! END TEMP MV - - if (fileFTPProgressList.empty () == false) - { - Lang & lang = Lang::getInstance (); - renderer.renderButton (&buttonCancelDownloads); - int - xLocation = buttonCancelDownloads.getX (); - int - yLocation = buttonCancelDownloads.getY () - 20; - for (std::map < string, pair < int, string > >::iterator iterMap = - fileFTPProgressList.begin (); - iterMap != fileFTPProgressList.end (); ++iterMap) - { - string - progressLabelPrefix = - lang.getString ("ModDownloading") + " " + iterMap->first + " "; -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nRendering file progress with the following prefix [%s]\n",progressLabelPrefix.c_str()); - - if (Renderer::renderText3DEnabled) - { - renderer.renderProgressBar3D (iterMap->second.first, xLocation, -//10, - yLocation, - CoreData::getInstance - ().getDisplayFontSmall3D (), -//350,progressLabelPrefix); - 300, progressLabelPrefix); - } - else - { - renderer.renderProgressBar (iterMap->second.first, -//10, - xLocation, - yLocation, - CoreData:: - getInstance ().getDisplayFontSmall - (), -//350,progressLabelPrefix); - 300, progressLabelPrefix); - } - - yLocation -= 20; - } - } - safeMutexFTPProgress.ReleaseLock (); - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - if (ftpMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&ftpMessageBox); - } - - if (program != NULL) - program->renderProgramMsgBox (); - - if (enableMapPreview && (mapPreview.hasFileLoaded () == true)) - { - - int - mouseX = mainMenu->getMouseX (); - int - mouseY = mainMenu->getMouseY (); - int - mouse2dAnim = mainMenu->getMouse2dAnim (); - - if (mapPreviewTexture == NULL) - { - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - - bool renderAll = (listBoxFogOfWar.getSelectedItemIndex () == 2); -//renderer.renderMapPreview(&mapPreview, renderAll, 10, 350, &mapPreviewTexture); - renderer.renderMapPreview (&mapPreview, renderAll, - this->render_mapPreviewTexture_X, - this->render_mapPreviewTexture_Y, - &mapPreviewTexture); - } - } - renderer.renderChatManager (&chatManager); - renderer.renderConsole (&console, - showFullConsole ? consoleFull : - consoleStoredAndNormal); - - if (difftime ((long int) time (NULL), timerLabelFlash) > 2) - { - timerLabelFlash = time (NULL); - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - void - MenuStateConnectedGame::update () - { - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - - Lang & lang = Lang::getInstance (); - ClientInterface * - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - - string newLabelConnectionInfo = lang.getString ("WaitingHost"); - if (clientInterface != NULL - && clientInterface->getJoinGameInProgress () == true) - { - newLabelConnectionInfo = lang.getString ("MGGameStatus2"); - } -// Test progress bar -//MutexSafeWrapper safeMutexFTPProgress((ftpClientThread != NULL ? ftpClientThread->getProgressMutex() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); -//fileFTPProgressList["test"] = pair(difftime(time(NULL),lastNetworkSendPing) * 20,"test file 123"); -//safeMutexFTPProgress.ReleaseLock(); -// - - if (clientInterface != NULL && clientInterface->isConnected ()) - { -//printf("#2 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); - broadCastGameSettingsToHeadlessServer (false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - bool - notCurrentlySwitching = - ((difftime - ((long int) time (NULL), - broadcastServerSettingsDelayTimer)) >= - HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS); - bool - receiveAllowedNow = - difftime ((long int) time (NULL), noReceiveTimer) > 2; - bool - newMessage = - lastGameSettingsReceivedCount < - clientInterface->getGameSettingsReceivedCount (); - if (validDisplayedGamesettings == false - || (notCurrentlySwitching && newMessage && receiveAllowedNow)) - { - -//printf("I take the whole settings top broadcastDelay=%d noReceiveTimer=%d\n", (int)difftime((long int) time(NULL), broadcastServerSettingsDelayTimer),(int)difftime((long int) time(NULL), noReceiveTimer)); - - displayedGamesettings = *(clientInterface->getGameSettings ()); - originalGamesettings = displayedGamesettings; - validDisplayedGamesettings = true; - } - - checkBoxAllowNativeLanguageTechtree.setEditable (isHeadlessAdmin ()); - checkBoxAllowNativeLanguageTechtree.setEnabled (isHeadlessAdmin ()); - - listBoxMap.setEditable (isHeadlessAdmin ()); - listBoxMapFilter.setEditable (isHeadlessAdmin ()); - buttonPlayNow.setVisible (isHeadlessAdmin () || - clientInterface->getJoinGameInProgress () == - true); - buttonRestoreLastSettings.setVisible (isHeadlessAdmin ()); - listBoxTechTree.setEditable (isHeadlessAdmin ()); - listBoxTileset.setEditable (isHeadlessAdmin ()); - checkBoxEnableSwitchTeamMode.setEditable (isHeadlessAdmin ()); - listBoxAISwitchTeamAcceptPercent.setEditable (isHeadlessAdmin ()); - listBoxFallbackCpuMultiplier.setEditable (isHeadlessAdmin ()); - listBoxFogOfWar.setEditable (isHeadlessAdmin ()); - checkBoxAllowObservers.setEditable (isHeadlessAdmin ()); - - checkBoxAllowTeamUnitSharing.setEditable (isHeadlessAdmin ()); - checkBoxAllowTeamResourceSharing.setEditable (isHeadlessAdmin ()); - - if (isHeadlessAdmin () == true) - { - bool hasOtherPlayer = false; - bool hasOpenSlot = false; - for (int i = 0; - i < GameConstants::maxPlayers; ++i) - { - if (displayedGamesettings.getFactionControl (i) == ctNetwork - && clientInterface->getPlayerIndex () != (int) i) - { - hasOpenSlot = true; - } - if (displayedGamesettings.getFactionControl (i) == ctNetwork && - displayedGamesettings.getNetworkPlayerNameByPlayerIndex (i) != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME && - displayedGamesettings.getNetworkPlayerNameByPlayerIndex (i) != - GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME) - { - // listBoxControls[i].setEditable (false); - if (clientInterface->getPlayerIndex () != (int) i) - { - hasOtherPlayer = true; - } - } - else if (clientInterface->getPlayerIndex () == (int) i || - i >= mapInfo.hardMaxPlayers) - { - // Most of the code that handles how to show the set up - // when observers are allowed is in the update() function - // in menu_state_custom_game.cpp. Explicitly stating to change - // players to observers, changing teams to the special "observer" - // team is done in that function. So basically, the update() function - // in this file receives the info from update() in - // menu_state_custom_game.cpp - // - // Mostly what needs to be done here is disable observer slots - // so the info can't be changed by the headless admin. We don't want - // him or her to accidently enable a non-observer into an observer slot - // that's > mapInfo.hardMaxPlayers, or to change the team. - // - listBoxControls[i].setEditable (false); - } - else - { - if (i < mapInfo.hardMaxPlayers) - { - listBoxControls[i].setEditable (true); - } - } - if (i < mapInfo.hardMaxPlayers) - { - listBoxRMultiplier[i].setEditable (isHeadlessAdmin ()); - listBoxFactions[i].setEditable (isHeadlessAdmin ()); - listBoxTeams[i].setEditable (isHeadlessAdmin ()); - } - } - if (hasOtherPlayer) - { - labelWaitingForPlayers.setText (""); - labelWaitingForPlayers.setVisible (false); - } - else if (hasOpenSlot) - { - labelWaitingForPlayers. - setText (lang.getString ("WaitingForPlayers")); - labelWaitingForPlayers.setVisible (true); - } - else - { - labelWaitingForPlayers. - setText (lang.getString ("OpenANetworkSLot")); - labelWaitingForPlayers.setVisible (true); - } - } - else - { - labelWaitingForPlayers.setText (""); - labelWaitingForPlayers.setVisible (false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (difftime ((long int) time (NULL), lastNetworkSendPing) >= - GameConstants::networkPingInterval) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] about to sendPingMessage...\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - lastNetworkSendPing = time (NULL); - clientInterface->sendPingMessage (GameConstants:: - networkPingInterval, - (int64) time (NULL)); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] pingCount = %d, clientInterface->getLastPingLag() = %f, GameConstants::networkPingInterval = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, pingCount, - clientInterface->getLastPingLag (), - GameConstants::networkPingInterval); - -// Starting checking timeout after sending at least 3 pings to server - if (clientInterface->isConnected () && - pingCount >= MAX_PING_LAG_COUNT - && clientInterface->getLastPingLag () >= - (GameConstants::networkPingInterval * MAX_PING_LAG_COUNT)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - clientInterface->updateLobby (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->isConnected () - && clientInterface->getJoinGameInProgress () == false - && pingCount >= MAX_PING_LAG_COUNT - && clientInterface->getLastPingLag () >= - (GameConstants::networkPingInterval * MAX_PING_LAG_COUNT)) - { - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (fileFTPProgressList.empty () == true) - { - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - displayedGamesettings.getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - clientInterface->sendTextMessage (lang.getString - ("ConnectionTimedOut", - languageList[i]) + - " : " + - doubleToStr - (clientInterface->getLastPingLag - (), 2), -1, false, - languageList[i]); - sleep (1); - clientInterface->close (); - } - } - } - } - - pingCount++; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - buttonDisconnect.setText (lang.getString ("Disconnect")); - - if (clientInterface->getAllowDownloadDataSynch () == false) - { - string label = lang.getString ("ConnectedToServer"); - - if (clientInterface->getServerName ().empty () == false) - { - label = label + " " + clientInterface->getServerName (); - } - - label = label + ", " + clientInterface->getVersionString (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getAllowGameDataSynchCheck () == false && - displayedGamesettings.getTileset () != "" && - displayedGamesettings.getTech () != "" && - displayedGamesettings.getMap () != "") - { - Config & config = Config::getInstance (); - - MutexSafeWrapper - safeMutexFTPProgress (ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex () : - NULL, - string (__FILE__) + "_" + - intToStr (__LINE__)); - - uint32 tilesetCRC = lastCheckedCRCTilesetValue; - if (lastCheckedCRCTilesetName != - displayedGamesettings.getTileset () - && displayedGamesettings.getTileset () != "") - { -//console.addLine("Checking tileset CRC [" + displayedGamesettings.getTileset() + "]"); - tilesetCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + displayedGamesettings.getTileset () + - string ("/*"), ".xml", NULL); - if (tilesetCRC == 0 - || tilesetCRC != displayedGamesettings.getTilesetCRC ()) - { - tilesetCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + displayedGamesettings.getTileset () + - string ("/*"), ".xml", NULL, true); - } - -// Test data synch -//tilesetCRC++; - lastCheckedCRCTilesetValue = tilesetCRC; - lastCheckedCRCTilesetName = displayedGamesettings.getTileset (); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - uint32 techCRC = lastCheckedCRCTechtreeValue; - if (lastCheckedCRCTechtreeName != displayedGamesettings.getTech () - && displayedGamesettings.getTech () != "") - { -//console.addLine("Checking techtree CRC [" + displayedGamesettings.getTech() + "]"); - techCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - string ("/") + displayedGamesettings.getTech () + - string ("/*"), ".xml", NULL); -//clientInterface->sendTextMessage("#1 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); - - if (techCRC == 0 - || techCRC != displayedGamesettings.getTechCRC ()) - { - techCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - string ("/") + displayedGamesettings.getTech () + - string ("/*"), ".xml", NULL, true); -//clientInterface->sendTextMessage("#2 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); - } - - - if (techCRC != 0 - && techCRC != displayedGamesettings.getTechCRC () - && listBoxTechTree.getSelectedItemIndex () >= 0 - && listBoxTechTree.getSelectedItem () != - Lang::getInstance ().getString ("DataMissing", "", true)) - { - -//time_t now = time(NULL); - time_t - lastUpdateDate = - getFolderTreeContentsCheckSumRecursivelyLastGenerated - (config.getPathListForType (ptTechs, ""), - string ("/") + displayedGamesettings.getTech () + - string ("/*"), ".xml"); - - const - time_t - REFRESH_CRC_DAY_SECONDS = 60 * 60 * 1; - if (lastUpdateDate <= 0 || - difftime ((long int) time (NULL), - lastUpdateDate) >= REFRESH_CRC_DAY_SECONDS) - { - techCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - string ("/") + displayedGamesettings.getTech () + - string ("/*"), ".xml", NULL, true); -//clientInterface->sendTextMessage("#3 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -// Test data synch -//techCRC++; - lastCheckedCRCTechtreeValue = techCRC; - lastCheckedCRCTechtreeName = displayedGamesettings.getTech (); - - loadFactions (&displayedGamesettings, false); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - factionCRCList.clear (); - for (unsigned int factionIdx = 0; - factionIdx < factionFiles.size (); ++factionIdx) - { - string factionName = factionFiles[factionIdx]; - if (factionName != GameConstants::RANDOMFACTION_SLOTNAME && - factionName != GameConstants::OBSERVER_SLOTNAME && - factionName != - Lang::getInstance ().getString ("DataMissing", "", true)) - { - - uint32 factionCRC = 0; -//time_t now = time(NULL); - time_t - lastUpdateDate = - getFolderTreeContentsCheckSumRecursivelyLastGenerated - (config.getPathListForType (ptTechs, ""), - "/" + displayedGamesettings.getTech () + "/factions/" + - factionName + "/*", ".xml"); - - const - time_t - REFRESH_CRC_DAY_SECONDS = 60 * 60 * 24; - if (lastUpdateDate <= 0 || - difftime ((long int) time (NULL), - lastUpdateDate) >= REFRESH_CRC_DAY_SECONDS - || (techCRC != 0 - && techCRC != displayedGamesettings.getTechCRC ())) - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + displayedGamesettings.getTech () + "/factions/" + - factionName + "/*", ".xml", NULL, true); - } - else - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + displayedGamesettings.getTech () + "/factions/" + - factionName + "/*", ".xml", NULL); - } - if (factionCRC == 0) - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + displayedGamesettings.getTech () + "/factions/" + - factionName + "/*", ".xml", NULL, true); - } - - if (factionCRC != 0) - { - vector < pair < string, uint32 > >serverFactionCRCList = - displayedGamesettings.getFactionCRCList (); - for (unsigned int factionIdx1 = 0; - factionIdx1 < serverFactionCRCList.size (); - ++factionIdx1) - { - pair < string, uint32 > &serverFaction = - serverFactionCRCList[factionIdx1]; - if (serverFaction.first == factionName) - { - if (serverFaction.second != factionCRC) - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + displayedGamesettings.getTech () + - "/factions/" + factionName + "/*", ".xml", NULL, - true); - } - break; - } - } - } - factionCRCList.push_back (make_pair - (factionName, factionCRC)); - } - } -//console.addLine("Found factions: " + intToStr(factionCRCList.size())); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - uint32 mapCRC = lastCheckedCRCMapValue; - if (lastCheckedCRCMapName != displayedGamesettings.getMap () && - displayedGamesettings.getMap () != "") - { - Checksum checksum; - string - file = - Config::getMapPath (displayedGamesettings.getMap (), "", - false); -//console.addLine("Checking map CRC [" + file + "]"); - checksum.addFile (file); - mapCRC = checksum.getSum (); -// Test data synch -//mapCRC++; - - lastCheckedCRCMapValue = mapCRC; - lastCheckedCRCMapName = displayedGamesettings.getMap (); - } - safeMutexFTPProgress.ReleaseLock (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - bool - dataSynchMismatch = - ((mapCRC != 0 && mapCRC != displayedGamesettings.getMapCRC ()) - || (tilesetCRC != 0 - && tilesetCRC != displayedGamesettings.getTilesetCRC ()) - || (techCRC != 0 - && techCRC != displayedGamesettings.getTechCRC ())); - -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nmapCRC [%d] displayedGamesettings.getMapCRC() [%d]\ntilesetCRC [%d] displayedGamesettings.getTilesetCRC() [%d]\ntechCRC [%d] displayedGamesettings.getTechCRC() [%d]\n",mapCRC,displayedGamesettings.getMapCRC(),tilesetCRC,displayedGamesettings.getTilesetCRC(),techCRC,displayedGamesettings.getTechCRC()); - - if (dataSynchMismatch == true && - (difftime - ((long int) time (NULL), - broadcastServerSettingsDelayTimer) >= - HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS)) - { -//printf("Data not synched: lmap %u rmap: %u ltile: %d rtile: %u ltech: %u rtech: %u\n",mapCRC,displayedGamesettings.getMapCRC(),tilesetCRC,displayedGamesettings.getTilesetCRC(),techCRC,displayedGamesettings.getTechCRC()); - - string labelSynch = lang.getString ("DataNotSynchedTitle"); - - if (mapCRC != 0 && mapCRC != displayedGamesettings.getMapCRC () - && listBoxMap.getSelectedItemIndex () >= 0 - && listBoxMap.getSelectedItem () != - Lang::getInstance ().getString ("DataMissing", "", true)) - { - labelSynch = labelSynch + " " + lang.getString ("Map"); - - if (updateDataSynchDetailText == true && - lastMapDataSynchError != - lang.getString ("DataNotSynchedMap") + " " + - listBoxMap.getSelectedItem ()) - { - lastMapDataSynchError = - lang.getString ("DataNotSynchedMap") + " " + - listBoxMap.getSelectedItem (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - string - msg = - lang.getString ("DataNotSynchedMap", - languageList[i]) + " " + - listBoxMap.getSelectedItem (); - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (msg, -1, localEcho, - languageList[i]); - } - } - } - - if (tilesetCRC != 0 - && tilesetCRC != displayedGamesettings.getTilesetCRC () - && listBoxTileset.getSelectedItemIndex () >= 0 - && listBoxTileset.getSelectedItem () != - Lang::getInstance ().getString ("DataMissing", "", true)) - { - labelSynch = labelSynch + " " + lang.getString ("Tileset"); - if (updateDataSynchDetailText == true && - lastTileDataSynchError != - lang.getString ("DataNotSynchedTileset") + " " + - listBoxTileset.getSelectedItem ()) - { - lastTileDataSynchError = - lang.getString ("DataNotSynchedTileset") + " " + - listBoxTileset.getSelectedItem (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - string - msg = - lang.getString ("DataNotSynchedTileset", - languageList[i]) + " " + - listBoxTileset.getSelectedItem (); - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (msg, -1, localEcho, - languageList[i]); - } - } - } - - if (techCRC != 0 - && techCRC != displayedGamesettings.getTechCRC () - && listBoxTechTree.getSelectedItemIndex () >= 0 - && listBoxTechTree.getSelectedItem () != - Lang::getInstance ().getString ("DataMissing", "", true)) - { - labelSynch = labelSynch + " " + lang.getString ("TechTree"); - if (updateDataSynchDetailText == true && - lastTechtreeDataSynchError != - lang.getString ("DataNotSynchedTechtree") + " " + - listBoxTechTree.getSelectedItem ()) - { - lastTechtreeDataSynchError = - lang.getString ("DataNotSynchedTechtree") + " " + - listBoxTechTree.getSelectedItem (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - string - msg = - lang.getString ("DataNotSynchedTechtree", - languageList[i]) + " " + - listBoxTechTree.getSelectedItem (); - bool localEcho = lang.isLanguageLocal (languageList[i]); - clientInterface->sendTextMessage (msg, -1, localEcho, - languageList[i]); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - const int - MAX_CHAT_TEXT_LINE_LENGTH = 110; -//const vector languageList = clientInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - bool localEcho = lang.isLanguageLocal (languageList[i]); - - string mismatchedFactionText = ""; - vector < string > mismatchedFactionTextList; - vector < pair < string, uint32 > >serverFactionCRCList = - displayedGamesettings.getFactionCRCList (); - - for (unsigned int factionIdx = 0; - factionIdx < serverFactionCRCList.size (); - ++factionIdx) - { - pair < string, uint32 > &serverFaction = - serverFactionCRCList[factionIdx]; - - bool foundFaction = false; - for (unsigned int clientFactionIdx = 0; - clientFactionIdx < factionCRCList.size (); - ++clientFactionIdx) - { - pair < string, uint32 > &clientFaction = - factionCRCList[clientFactionIdx]; - - if (serverFaction.first == clientFaction.first) - { - foundFaction = true; - if (serverFaction.second != clientFaction.second) - { - if (mismatchedFactionText.length () >= 10) - { - mismatchedFactionTextList.push_back - (mismatchedFactionText); - mismatchedFactionText = ""; - } - if (mismatchedFactionText == "") - { - mismatchedFactionText = - "The following factions are mismatched: "; - if (lang.hasString ("MismatchedFactions", - languageList[i]) == true) - { - mismatchedFactionText = - lang.getString ("MismatchedFactions", - languageList[i]); - } - - mismatchedFactionText += - " [" + intToStr (factionCRCList.size ()) + - "][" + - intToStr (serverFactionCRCList.size ()) + - "] - "; - } - else - { - mismatchedFactionText += ", "; - } - mismatchedFactionText += serverFaction.first; - } - break; - } - } - - if (foundFaction == false) - { - if ((int) mismatchedFactionText.length () > - MAX_CHAT_TEXT_LINE_LENGTH) - { - mismatchedFactionTextList.push_back - (mismatchedFactionText); - mismatchedFactionText = ""; - } - - if (mismatchedFactionText == "") - { - mismatchedFactionText = - "The following factions are mismatched: "; - if (lang.hasString ("MismatchedFactions", - languageList[i]) == true) - { - mismatchedFactionText = - lang.getString ("MismatchedFactions", - languageList[i]); - } - - mismatchedFactionText += - " [" + intToStr (factionCRCList.size ()) + "][" + - intToStr (serverFactionCRCList.size ()) + "] - "; - } - else - { - mismatchedFactionText += ", "; - } - - if (lang.hasString ("MismatchedFactionsMissing", - languageList[i]) == true) - { - mismatchedFactionText += - serverFaction.first + " " + - lang.getString ("MismatchedFactionsMissing", - languageList[i]); - } - else - { - mismatchedFactionText += - serverFaction.first + " (missing)"; - } - } - } - - for (unsigned int clientFactionIdx = 0; - clientFactionIdx < factionCRCList.size (); - ++clientFactionIdx) - { - pair < string, uint32 > &clientFaction = - factionCRCList[clientFactionIdx]; - - bool foundFaction = false; - for (unsigned int factionIdx = 0; - factionIdx < serverFactionCRCList.size (); - ++factionIdx) - { - pair < string, uint32 > &serverFaction = - serverFactionCRCList[factionIdx]; - - if (serverFaction.first == clientFaction.first) - { - foundFaction = true; - break; - } - } - - if (foundFaction == false) - { - if ((int) mismatchedFactionText.length () > - MAX_CHAT_TEXT_LINE_LENGTH) - { - mismatchedFactionTextList.push_back - (mismatchedFactionText); - mismatchedFactionText = ""; - } - - if (mismatchedFactionText == "") - { - mismatchedFactionText = - "The following factions are mismatched: "; - if (lang.hasString ("MismatchedFactions", - languageList[i]) == true) - { - mismatchedFactionText = - lang.getString ("MismatchedFactions", - languageList[i]); - } - - mismatchedFactionText += - " [" + intToStr (factionCRCList.size ()) + "][" + - intToStr (serverFactionCRCList.size ()) + "] - "; - } - else - { - mismatchedFactionText += ", "; - } - - if (lang.hasString ("MismatchedFactionsExtra", - languageList[i]) == true) - { - mismatchedFactionText += - clientFaction.first + " " + - lang.getString ("MismatchedFactionsExtra", - languageList[i]); - } - else - { - mismatchedFactionText += - clientFaction.first + " (extra)"; - } - } - } - - if (mismatchedFactionText != "") - { - if (mismatchedFactionTextList.empty () == false) - { - if (mismatchedFactionText != "") - { - mismatchedFactionText += "."; - mismatchedFactionTextList.push_back - (mismatchedFactionText); - } - for (unsigned int splitIdx = 0; - splitIdx < mismatchedFactionTextList.size (); - ++splitIdx) - { - clientInterface->sendTextMessage - (mismatchedFactionTextList[splitIdx], -1, - localEcho, languageList[i]); - } - } - else - { - mismatchedFactionText += "."; - clientInterface->sendTextMessage - (mismatchedFactionText, -1, localEcho, - languageList[i]); - } - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - } - } - - updateDataSynchDetailText = false; - labelDataSynchInfo.setText (labelSynch); - } - else - { - labelDataSynchInfo.setText (""); - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getAllowGameDataSynchCheck () == true && - clientInterface->getNetworkGameDataSynchCheckOk () == false) - { - label = label + " -synch mismatch for:"; - - if (clientInterface->getNetworkGameDataSynchCheckOkMap () == - false) - { - label = label + " map"; - - if (updateDataSynchDetailText == true && - clientInterface->getReceivedDataSynchCheck () && - lastMapDataSynchError != - "map CRC mismatch, " + listBoxMap.getSelectedItem ()) - { - lastMapDataSynchError = - "map CRC mismatch, " + listBoxMap.getSelectedItem (); - clientInterface->sendTextMessage (lastMapDataSynchError, -1, - true, ""); - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getNetworkGameDataSynchCheckOkTile () == - false) - { - label = label + " tile"; - if (updateDataSynchDetailText == true && - clientInterface->getReceivedDataSynchCheck () && - lastTileDataSynchError != - "tile CRC mismatch, " + listBoxTileset.getSelectedItem ()) - { - lastTileDataSynchError = - "tile CRC mismatch, " + listBoxTileset.getSelectedItem (); - clientInterface->sendTextMessage (lastTileDataSynchError, -1, - true, ""); - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getNetworkGameDataSynchCheckOkTech () == - false) - { - label = label + " techtree"; - - if (updateDataSynchDetailText == true && - clientInterface->getReceivedDataSynchCheck ()) - { - - string - report = - clientInterface->getNetworkGameDataSynchCheckTechMismatchReport - (); - if (lastTechtreeDataSynchError != - "techtree CRC mismatch" + report) - { - lastTechtreeDataSynchError = - "techtree CRC mismatch" + report; - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] report: %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - report.c_str ()); - - clientInterface->sendTextMessage ("techtree CRC mismatch", - -1, true, ""); - vector < string > reportLineTokens; - Tokenize (report, reportLineTokens, "\n"); - for (int reportLine = 0; - reportLine < (int) reportLineTokens.size (); - ++reportLine) - { - clientInterface->sendTextMessage (reportLineTokens - [reportLine], -1, true, - ""); - } - } - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getReceivedDataSynchCheck () == true) - { - updateDataSynchDetailText = false; - } - } - else if (clientInterface->getAllowGameDataSynchCheck () == true) - { - label += " - data synch is ok"; - } - - labelStatus.setText (label); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - } - else - { - string label = lang.getString ("ConnectedToServer"); - - if (!clientInterface->getServerName ().empty ()) - { - label = label + " " + clientInterface->getServerName (); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (clientInterface->getAllowGameDataSynchCheck () == true && - clientInterface->getNetworkGameDataSynchCheckOk () == false) - { - label = label + " -waiting to synch:"; - if (clientInterface->getNetworkGameDataSynchCheckOkMap () == - false) - { - label = label + " map"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTile () == - false) - { - label = label + " tile"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTech () == - false) - { - label = label + " techtree"; - } - } - else if (clientInterface->getAllowGameDataSynchCheck () == true) - { - label += " - data synch is ok"; - } - - labelStatus.setText (label); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (clientInterface != NULL - && clientInterface->isConnected () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - clientInterface->close (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - returnToJoinMenu (); - return; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//process network messages - if (clientInterface != NULL && clientInterface->isConnected ()) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - try - { - if (clientInterface->getGameSettingsReceived () - && validDisplayedGamesettings - && lastGameSettingsReceivedCount != - clientInterface->getGameSettingsReceivedCount ()) - { - lastGameSettingsReceivedCount = - clientInterface->getGameSettingsReceivedCount (); - bool - errorOnMissingData = - (clientInterface->getAllowGameDataSynchCheck () == false); - - const GameSettings * - receivedGameSettings = clientInterface->getGameSettings (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//printf("Menu got new settings thisfactionindex = %d startlocation: %d control = %d\n",displayedGamesettings.getThisFactionIndex(),clientInterface->getGameSettings()->getStartLocationIndex(clientInterface->getGameSettings()->getThisFactionIndex()),displayedGamesettings.getFactionControl(clientInterface->getGameSettings()->getThisFactionIndex())); - if (difftime ((long int) time (NULL), noReceiveTimer) < 3 - || difftime ((long int) time (NULL), - broadcastServerSettingsDelayTimer) < - HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS) - { -// copy my current settings in UI to displayedSettings; - loadGameSettings (&displayedGamesettings); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -// check if there are any changed fields from others clients - if (isHeadlessAdmin ()) - { -//printf("I am headless admin and will restore only some parts\n"); -// only copy those parts which are editable by normal clients - for (int i = 0; i < receivedGameSettings->getFactionCount (); - i++) - { - if (displayedGamesettings.getFactionControl (i) == - ctNetwork) - { - if (originalGamesettings.getTeam (i) == - displayedGamesettings.getTeam (i)) - { - displayedGamesettings.setTeam (i, - receivedGameSettings->getTeam - (i)); - originalGamesettings.setTeam (i, - receivedGameSettings->getTeam - (i)); - } - if (originalGamesettings.getFactionTypeName (i) == - displayedGamesettings.getFactionTypeName (i)) - { - displayedGamesettings.setFactionTypeName (i, - receivedGameSettings->getFactionTypeName - (i)); - originalGamesettings.setFactionTypeName (i, - receivedGameSettings->getFactionTypeName - (i)); - } - displayedGamesettings.setNetworkPlayerGameStatus (i, - receivedGameSettings->getNetworkPlayerGameStatus - (i)); - originalGamesettings.setNetworkPlayerGameStatus (i, - receivedGameSettings->getNetworkPlayerGameStatus - (i)); - displayedGamesettings.setNetworkPlayerName (i, - receivedGameSettings->getNetworkPlayerName - (i)); - originalGamesettings.setNetworkPlayerName (i, - receivedGameSettings->getNetworkPlayerName - (i)); - } - } - } - else - { -//printf("I am client and restore everything but not my line\n"); -// copy all received fields just not those which are editable for normal client -//store my changes - int - i = clientInterface->getPlayerIndex (); - int - team = displayedGamesettings.getTeam (i); - string faction = displayedGamesettings.getFactionTypeName (i); - int - status = - displayedGamesettings.getNetworkPlayerGameStatus (i); - string - networkPlayerName = - displayedGamesettings.getNetworkPlayerName (i); - displayedGamesettings = *receivedGameSettings; - originalGamesettings = *receivedGameSettings; - - displayedGamesettings.setTeam (i, team); - originalGamesettings.setTeam (i, team); - displayedGamesettings.setFactionTypeName (i, faction); - originalGamesettings.setFactionTypeName (i, faction); - displayedGamesettings.setNetworkPlayerGameStatus (i, status); - originalGamesettings.setNetworkPlayerGameStatus (i, status); - displayedGamesettings.setNetworkPlayerName (i, - networkPlayerName); - originalGamesettings.setNetworkPlayerName (i, - networkPlayerName); - - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - setupUIFromGameSettings (&displayedGamesettings, - errorOnMissingData); - } - else - { -// do nothing - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - setupUIFromGameSettings (&displayedGamesettings, - errorOnMissingData); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - broadCastGameSettingsToHeadlessServer - (needToBroadcastServerSettings); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -// check if we are joining an in progress game - if (clientInterface->getJoinGameInProgress () == true && - clientInterface->getJoinGameInProgressLaunch () == true && - clientInterface->getReadyForInGameJoin () == true && - ftpClientThread != NULL) - { - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? - ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (readyToJoinInProgressGame == false) - { - if (getInProgressSavedGameFromFTPServer == "") - { - - getInProgressSavedGameFromFTPServerInProgress = true; - ftpClientThread->addTempFileToRequests - (GameConstants::saveNetworkGameFileClientCompressed, - GameConstants::saveNetworkGameFileServerCompressed); - - getInProgressSavedGameFromFTPServer = - GameConstants::saveNetworkGameFileServerCompressed; - fileFTPProgressList[getInProgressSavedGameFromFTPServer] = - pair < int, - string > (0, ""); - } - safeMutexFTPProgress.ReleaseLock (); - } - else - { - safeMutexFTPProgress.ReleaseLock (); - - string - saveGameFile = - "temp/" + string (GameConstants::saveNetworkGameFileClient); - if (getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) != "") - { - saveGameFile = - getGameReadWritePath - (GameConstants::path_logs_CacheLookupKey) + saveGameFile; - } - else - { - string - userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameFile = userData + saveGameFile; - } - -//printf("Loading saved game file [%s]\n",saveGameFile.c_str()); - - GameSettings - gameSettings = *clientInterface->getGameSettings (); - loadGameSettings (&gameSettings); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - Game::loadGame (saveGameFile, program, false, &gameSettings); - return; - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//update lobby - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected ()) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - clientInterface->updateLobby (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected ()) - { - if (initialSettingsReceivedFromServer == true && - clientInterface->getIntroDone () == true && - (switchSetupRequestFlagType & ssrft_NetworkPlayerName) == - ssrft_NetworkPlayerName) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] getHumanPlayerName() = [%s], clientInterface->getGameSettings()->getThisFactionIndex() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - getHumanPlayerName ().c_str (), - clientInterface->getGameSettings - ()->getThisFactionIndex ()); - clientInterface->sendSwitchSetupRequest ("", - clientInterface->getPlayerIndex - (), -1, -1, - getHumanPlayerName (), - getNetworkPlayerStatus - (), - switchSetupRequestFlagType, - lang.getLanguage ()); - - switchSetupRequestFlagType = ssrft_None; - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//intro - if (clientInterface->getIntroDone ()) - { - if (newLabelConnectionInfo != labelInfo.getText ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - labelInfo.setText (newLabelConnectionInfo); - } - } - -//launch - if (clientInterface->getLaunchGame ()) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - assert (clientInterface != NULL); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (modHttpServerThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - modHttpServerThread->setSimpleTaskInterfaceValid (false); - modHttpServerThread->signalQuit (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (modHttpServerThread->canShutdown (true) == true - && modHttpServerThread->shutdownAndWait () == true) - { - delete modHttpServerThread; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - modHttpServerThread = NULL; - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (ftpClientThread != NULL) - { - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - } - ftpClientThread = NULL; - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - launchingNewGame = true; - - GameSettings - gameSettings = *clientInterface->getGameSettings (); -// complete game settings with local stuff - if (gameSettings.getScenario () != "") - { - string scenario = gameSettings.getScenario (); - listBoxScenario.setSelectedItem (formatString (scenario)); - string file = Scenario::getScenarioPath (dirList, scenario); - - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, &scenarioInfo, isTutorial); - - gameSettings.setScenarioDir (Scenario::getScenarioPath - (dirList, scenarioInfo.name)); - - gameSettings. - setDefaultResources (scenarioInfo.defaultResources); - gameSettings.setDefaultUnits (scenarioInfo.defaultUnits); - gameSettings.setDefaultVictoryConditions - (scenarioInfo.defaultVictoryConditions); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - program->setState (new Game (program, &gameSettings, false)); - return; - } - } - -//call the chat manager - chatManager.updateNetwork (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//console732 - console.update (); - -// check for need to switch music on again - if (clientInterface != NULL) - { - int - currentConnectionCount = 0; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (displayedGamesettings.getFactionControl (i) == ctNetwork && - displayedGamesettings.getNetworkPlayerName (i) != "" && - displayedGamesettings.getNetworkPlayerName (i) != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - currentConnectionCount++; - } - } - if (currentConnectionCount > soundConnectionCount) - { - soundConnectionCount = currentConnectionCount; - static PlaySoundClip snd; - SoundRenderer::getInstance (). - playFx (snd.getSound (snd.sfxAttention)); -//switch on music again!! - Config & config = Config::getInstance (); - float - configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - CoreData::getInstance (). - getMenuMusic ()->setVolume (configVolume); - } - soundConnectionCount = currentConnectionCount; - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - } - catch (const runtime_error & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, ex.what ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, szBuf); - showMessageBox (szBuf, "Error", false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - } - } - - bool - MenuStateConnectedGame::loadFactions (const GameSettings * gameSettings, - bool errorOnNoFactions) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Lang & lang = Lang::getInstance (); - bool foundFactions = false; - vector < string > results; - - string - scenarioDir = - Scenario::getScenarioDir (dirList, gameSettings->getScenario ()); - if (gameSettings->getTech () != "") - { - Config & config = Config::getInstance (); - - vector < string > techPaths = - config.getPathListForType (ptTechs, scenarioDir); - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); -//findAll(techPath + gameSettings->getTech() + "/factions/*.", results, false, false); - findDirs (techPath + gameSettings->getTech () + "/factions/", - results, false, false); - if (results.empty () == false) - { - break; - } - } - - if (results.empty () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface->getAllowGameDataSynchCheck () == false) - { - if (errorOnNoFactions == true) - { - throw - megaglest_runtime_error - ("(2)There are no factions for the tech tree [" + - gameSettings->getTech () + "]"); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] (2)There are no factions for the tech tree [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - gameSettings->getTech ().c_str ()); - } - results. - push_back (Lang:: - getInstance ().getString ("DataMissing", "", true)); - factionFiles = results; - vector < string > translatedFactionNames; - for (int i = 0; i < (int) factionFiles.size (); ++i) - { - results[i] = formatString (results[i]); - string - translatedString = - techTree->getTranslatedFactionName (gameSettings->getTech (), - factionFiles[i]); - if (toLower (translatedString) == toLower (results[i])) - { - translatedFactionNames.push_back (results[i]); - } - else - { - translatedFactionNames.push_back (results[i] + " (" + - translatedString + ")"); - } - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxFactions[i].setItems (results, translatedFactionNames); - } - - if (lastMissingTechtree != gameSettings->getTech () && - gameSettings->getTech () != "") - { - lastMissingTechtree = gameSettings->getTech (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtree", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTechtree", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - gameSettings->getTech ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is missing the techtree: %s", - getHumanPlayerName ().c_str (), - gameSettings->getTech ().c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - } - - foundFactions = false; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - else - { - lastMissingTechtree = ""; - getMissingTechtreeFromFTPServer = ""; - - factionFiles = results; - vector < string > translatedFactionNames; - for (int i = 0; i < (int) factionFiles.size (); ++i) - { - results[i] = formatString (results[i]); - string - translatedString = - techTree->getTranslatedFactionName (gameSettings->getTech (), - factionFiles[i]); - if (toLower (translatedString) == toLower (results[i])) - { - translatedFactionNames.push_back (results[i]); - } - else - { - translatedFactionNames.push_back (results[i] + " (" + - translatedString + ")"); - } - } - - results.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Random", "", - true) + "*"); - -// Add special Observer Faction - if (checkBoxAllowObservers.getValue () == 1) - { - results.push_back (formatString - (GameConstants::OBSERVER_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::OBSERVER_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Observer", "", - true) + "*"); - } - - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxFactions[i].setItems (results, translatedFactionNames); - } - - foundFactions = (results.empty () == false); - } - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - return foundFactions; - } - -// ============ PRIVATE =========================== - - bool MenuStateConnectedGame::hasNetworkGameSettings () - { - bool hasNetworkSlot = false; - - try - { - for (int i = 0; i < mapInfo.players; ++i) - { - ControlType - ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct != ctClosed) - { - if (ct == ctNetwork || ct == ctNetworkUnassigned) - { - hasNetworkSlot = true; - break; - } - } - } - if (hasNetworkSlot == false) - { - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ControlType - ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct != ctClosed) - { - if (ct == ctNetworkUnassigned) - { - hasNetworkSlot = true; - break; - } - } - } - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, szBuf); - showMessageBox (szBuf, "Error", false); - } - - return hasNetworkSlot; - } - - void - MenuStateConnectedGame::keyDown (SDL_KeyboardEvent key) - { - if (activeInputLabel != NULL) - { - bool handled = keyDownEditLabel (key, &activeInputLabel); - if (handled == true) - { - switchSetupRequestFlagType |= ssrft_NetworkPlayerName; - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else - { -//send key to the chat manager - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL && - clientInterface->isConnected () == true && - clientInterface->getIntroDone () == true) - { - chatManager.keyDown (key); - } - if (chatManager.getEditEnabled () == false && - (::Shared::Platform::Window::isKeyStateModPressed (KMOD_SHIFT) == - false)) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) == - true) - { - showFullConsole = true; - } - else if (isKeyPressed (configKeys.getSDLKey ("ToggleMusic"), key) == - true) - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - float - configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - float - currentVolume = - CoreData::getInstance ().getMenuMusic ()->getVolume (); - if (currentVolume > 0) - { - CoreData::getInstance ().getMenuMusic ()->setVolume (0.f); - console.addLine (lang.getString ("GameMusic") + " " + - lang.getString ("Off")); - } - else - { - CoreData::getInstance (). - getMenuMusic ()->setVolume (configVolume); -//If the config says zero, use the default music volume -//gameMusic->setVolume(configVolume ? configVolume : 0.9); - console.addLine (lang.getString ("GameMusic")); - } - } -//else if(key == configKeys.getCharKey("SaveGUILayout")) { - else if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) - == true) - { - bool - saved = - GraphicComponent::saveAllCustomProperties (containerName); - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("GUILayoutSaved") + " [" + - (saved ? lang. - getString ("Yes") : lang.getString ("No")) + - "]"); - } - } - } - } - - bool MenuStateConnectedGame::textInput (std::string text) - { - if (activeInputLabel != NULL) - { - bool handled = textInputEditLabel (text, &activeInputLabel); - if (handled == true) - { - switchSetupRequestFlagType |= ssrft_NetworkPlayerName; - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - return handled; - } - else - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected () == true - && clientInterface->getIntroDone () == true) - { - return chatManager.textInput (text); - } - } - return false; - } - - void - MenuStateConnectedGame::keyPress (SDL_KeyboardEvent c) - { - if (activeInputLabel != NULL) - { - keyPressEditLabel (c, &activeInputLabel); - } - else - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected () == true - && clientInterface->getIntroDone () == true) - { - chatManager.keyPress (c); - } - } - } - - void - MenuStateConnectedGame::keyUp (SDL_KeyboardEvent key) - { - if (activeInputLabel == NULL) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - if (clientInterface != NULL && - clientInterface->isConnected () == true && - clientInterface->getIntroDone () == true) - { - chatManager.keyUp (key); - } - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - if (chatManager.getEditEnabled ()) - { -//send key to the chat manager - chatManager.keyUp (key); - } - else if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) - == true) - { - showFullConsole = false; - } - } - } - - void - MenuStateConnectedGame::setActiveInputLabel (GraphicLabel * newLable) - { - MenuState::setActiveInputLabel (newLable, &activeInputLabel); - } - - string MenuStateConnectedGame::getHumanPlayerName () - { - string result = defaultPlayerName; - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - if (clientInterface != NULL && - j == clientInterface->getPlayerIndex () && - labelPlayerNames[j].getText () != "") - { - result = labelPlayerNames[j].getText (); - - if (activeInputLabel != NULL) - { - size_t found = result.find_last_of ("_"); - if (found != string::npos) - { - result = result.substr (0, found); - } - } - - break; - } - } - - return result; - } - - void - MenuStateConnectedGame::loadFactionTexture (string filepath) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (enableFactionTexturePreview == true) - { - if (filepath == "") - { - factionTexture = NULL; - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] filepath = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, filepath.c_str ()); - factionTexture = Renderer::findTexture (filepath); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - } - } - - bool - MenuStateConnectedGame::loadMapInfo (string file, MapInfo * mapInfo, - bool loadMapPreview) - { - bool mapLoaded = false; - try - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] map [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, file.c_str ()); - - if (file != "") - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - lastMissingMap = file; - - Lang & lang = Lang::getInstance (); - if (MapPreview::loadMapInfo - (file, mapInfo, lang.getString ("MaxPlayers"), - lang.getString ("Size"), true) == true) - { - mapInfo->players = GameConstants::maxPlayers; - -// Not painting properly so this is on hold - if (loadMapPreview == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - if (mapPreview.getMapFileLoaded () != file) - { - mapPreview.loadFromFile (file.c_str ()); - cleanupMapPreviewTexture (); - } - } - - mapLoaded = true; - } - } - else - { - cleanupMapPreviewTexture (); - mapInfo->desc = - Lang::getInstance ().getString ("DataMissing", "", true); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (lastMissingMap != gameSettings->getMap ()) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - lastMissingMap = gameSettings->getMap (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingMap", languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingMap", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - gameSettings->getMap ().c_str ()); - } - else - { - snprintf (szMsg, 8096, "Player: %s is missing the map: %s", - getHumanPlayerName ().c_str (), - gameSettings->getMap ().c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - } - } - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - e.what ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, e.what ()); - - showMessageBox ("Error loading map file: " + file + '\n' + e.what (), - "Error", false); - } - - return mapLoaded; - } - - void - MenuStateConnectedGame::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void - MenuStateConnectedGame::showFTPMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - ftpMessageBox.setEnabled (false); - } - - if (!ftpMessageBox.getEnabled ()) - { - ftpMessageBox.setText (text); - ftpMessageBox.setHeader (header); - ftpMessageBox.setEnabled (true); - } - else - { - ftpMessageBox.setEnabled (false); - } - } - - int32 MenuStateConnectedGame::getNetworkPlayerStatus () - { - int32 result = npst_None; - switch (listBoxPlayerStatus.getSelectedItemIndex ()) - { - case 2: - result = npst_Ready; - break; - case 1: - result = npst_BeRightBack; - break; - case 0: - result = npst_PickSettings; - break; - } - - return result; - } - - void - MenuStateConnectedGame::cleanupMapPreviewTexture () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (mapPreviewTexture != NULL) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - mapPreviewTexture->end (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - delete mapPreviewTexture; - mapPreviewTexture = NULL; - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - bool MenuStateConnectedGame::isInSpecialKeyCaptureEvent () - { - bool - result = (chatManager.getEditEnabled () || activeInputLabel != NULL); - return result; - } - - void - MenuStateConnectedGame::FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType - type, - pair < - FTP_Client_ResultType, - string > result, - void *userdata) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (type == ftp_cct_DownloadProgress) - { - FTPClientCallbackInterface::FtpProgressStats * stats = - (FTPClientCallbackInterface::FtpProgressStats *) userdata; - if (stats != NULL) - { - int - fileProgress = 0; - if (stats->download_total > 0) - { - fileProgress = - ((stats->download_now / stats->download_total) * 100.0); - } -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () - : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - pair < int, - string > - lastProgress; - std::map < string, pair < int, - string > >::iterator - iterFind = fileFTPProgressList.find (itemName); - if (iterFind == fileFTPProgressList.end ()) - { - iterFind = - fileFTPProgressList.find (GameConstants:: - saveNetworkGameFileServerCompressed); - if (iterFind == fileFTPProgressList.end ()) - { - iterFind = - fileFTPProgressList.find (GameConstants:: - saveNetworkGameFileClientCompressed); - } - } - if (iterFind != fileFTPProgressList.end ()) - { - lastProgress = iterFind->second; - fileFTPProgressList[iterFind->first] = pair < int, - string > (fileProgress, stats->currentFilename); - } - safeMutexFTPProgress.ReleaseLock (); - - if (itemName != "" - && (lastProgress.first / 25) < (fileProgress / 25)) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("FileDownloadProgress", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("FileDownloadProgress", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str (), - fileProgress); - } - else - { - snprintf (szMsg, 8096, - "Player: %s download progress for [%s] is %d %%", - getHumanPlayerName ().c_str (), itemName.c_str (), - fileProgress); - } - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] szMsg [%s] lastProgress.first = %d, fileProgress = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, szMsg, lastProgress.first, - fileProgress); - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - } - } - } - else if (type == ftp_cct_ExtractProgress) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP extract Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - if (userdata == NULL) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingExtractDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingExtractDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Please wait, player: %s is extracting: %s", - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] szMsg [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, szMsg); - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - } - else - { - char * - szBuf = (char *) userdata; -//printf("%s\n",szBuf); -//console.addLine(szBuf); - console.addLine (szBuf, false, ""); - } - } - else if (type == ftp_cct_Map) - { - getMissingMapFromFTPServerInProgress = false; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (result.first == ftp_crt_SUCCESS) - { -// Clear the CRC file Cache - string file = Config::getMapPath (itemName, "", false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Got map itemName [%s] file [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", - itemName.c_str (), file.c_str (), - lastCheckedCRCMapName.c_str (), - gameSettings->getMap ().c_str ()); - - if (gameSettings != NULL && itemName == gameSettings->getMap () && - lastCheckedCRCMapName == gameSettings->getMap () && - gameSettings->getMap () != "") - { - Checksum::clearFileCache (); - Checksum checksum; - - checksum.addFile (file); - lastCheckedCRCMapValue = checksum.getSum (); - } - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingMapSuccessDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingMapSuccessDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s SUCCESSFULLY downloaded the map: %s", - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - } - else - { - printf - ("FAILED map itemName [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", - itemName.c_str (), lastCheckedCRCMapName.c_str (), - gameSettings->getMap ().c_str ()); - - curl_version_info_data * - curlVersion = curl_version_info (CURLVERSION_NOW); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingMapFailDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingMapFailDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str (), - curlVersion->version); - } - else - { - snprintf (szMsg, 8096, - "Player: %s FAILED to download the map: [%s] using CURL version [%s]", - getHumanPlayerName ().c_str (), itemName.c_str (), - curlVersion->version); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - - if (result.first == ftp_crt_HOST_NOT_ACCEPTING) - { - if (lang.hasString ("HostNotAcceptingDataConnections", - languageList[i]) == true) - { - clientInterface->sendTextMessage (lang.getString - ("HostNotAcceptingDataConnections", - languageList[i]), -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - else - { - clientInterface->sendTextMessage - ("*Warning* the host is not accepting data connections.", - -1, lang.isLanguageLocal (languageList[i]), - languageList[i]); - } - } - } - sleep (1); - - console.addLine (result.second, true); - } - } - else if (type == ftp_cct_Tileset) - { - getMissingTilesetFromFTPServerInProgress = false; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (true); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (result.first == ftp_crt_SUCCESS) - { - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTilesetSuccessDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTilesetSuccessDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s SUCCESSFULLY downloaded the tileset: %s", - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - -// START -// Clear the CRC Cache if it is populated -// -// Clear the CRC file Cache - safeMutexFTPProgress.Lock (); - Checksum::clearFileCache (); - - vector < string > paths = - Config::getInstance ().getPathListForType (ptTilesets); - string pathSearchString = string ("/") + itemName + string ("/*"); - const - string - filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - -// Refresh CRC - -//printf("Got map itemName [%s] file [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", -// itemName.c_str(),file.c_str(),lastCheckedCRCMapName.c_str(),gameSettings->getMap().c_str()); - - if (gameSettings != NULL && itemName == gameSettings->getTileset () - && lastCheckedCRCTilesetName == gameSettings->getTileset () - && gameSettings->getTileset () != "") - { - Config & config = Config::getInstance (); - lastCheckedCRCTilesetValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + itemName + string ("/*"), ".xml", NULL, true); - } - - safeMutexFTPProgress.ReleaseLock (); -// END - -// Reload tilesets for the UI - string - scenarioDir = - Scenario::getScenarioDir (dirList, gameSettings->getScenario ()); - findDirs (Config::getInstance ().getPathListForType (ptTilesets, - scenarioDir), - tilesetFiles); - - std::vector < string > tilesetsFormatted = tilesetFiles; - std::for_each (tilesetsFormatted.begin (), tilesetsFormatted.end (), - FormatString ()); - listBoxTileset.setItems (tilesetsFormatted); - } - else - { - curl_version_info_data * - curlVersion = curl_version_info (CURLVERSION_NOW); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTilesetFailDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTilesetFailDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - (itemName + "(.7z)").c_str (), curlVersion->version); - } - else - { - snprintf (szMsg, 8096, - "Player: %s FAILED to download the tileset: [%s] using CURL version [%s]", - getHumanPlayerName ().c_str (), - (itemName + "(.7z)").c_str (), curlVersion->version); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - - if (result.first == ftp_crt_HOST_NOT_ACCEPTING) - { - if (lang.hasString ("HostNotAcceptingDataConnections", - languageList[i]) == true) - { - clientInterface->sendTextMessage (lang.getString - ("HostNotAcceptingDataConnections", - languageList[i]), -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - else - { - clientInterface->sendTextMessage - ("*Warning* the host is not accepting data connections.", - -1, lang.isLanguageLocal (languageList[i]), - languageList[i]); - } - } - } - sleep (1); - - console.addLine (result.second, true); - } - } - else if (type == ftp_cct_Techtree) - { - getMissingTechtreeFromFTPServerInProgress = false; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (true); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (result.first == ftp_crt_SUCCESS) - { - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtreeSuccessDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTechtreeSuccessDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s SUCCESSFULLY downloaded the techtree: %s", - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - -// START -// Clear the CRC Cache if it is populated -// -// Clear the CRC file Cache - safeMutexFTPProgress.Lock (); - Checksum::clearFileCache (); - - vector < string > paths = - Config::getInstance ().getPathListForType (ptTechs); - string pathSearchString = string ("/") + itemName + string ("/*"); - const - string - filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - -// Refresh CRC - if (gameSettings != NULL && itemName == gameSettings->getTech () && - lastCheckedCRCTechtreeName == gameSettings->getTech () && - gameSettings->getTech () != "") - { - Config & config = Config::getInstance (); - lastCheckedCRCTechtreeValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - string ("/") + itemName + string ("/*"), ".xml", NULL, true); - } - safeMutexFTPProgress.ReleaseLock (); -// END - -// Reload techs for the UI - string - scenarioDir = - Scenario::getScenarioDir (dirList, gameSettings->getScenario ()); - findDirs (Config:: - getInstance ().getPathListForType (ptTechs, scenarioDir), - techTreeFiles); - - vector < string > translatedTechs; - std::vector < string > techsFormatted = techTreeFiles; - for (int i = 0; i < (int) techsFormatted.size (); i++) - { - techsFormatted.at (i) = formatString (techsFormatted.at (i)); - - string - txTech = - techTree->getTranslatedName (techTreeFiles.at (i), true); - translatedTechs.push_back (formatString (txTech)); - } - listBoxTechTree.setItems (techsFormatted, translatedTechs); - } - else - { - curl_version_info_data * - curlVersion = curl_version_info (CURLVERSION_NOW); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < (unsigned int) languageList.size (); - ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtreeFailDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTechtreeFailDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - (itemName + "(.7z)").c_str (), curlVersion->version); - } - else - { - snprintf (szMsg, 8096, - "Player: %s FAILED to download the techtree: [%s] using CURL version [%s]", - getHumanPlayerName ().c_str (), - (itemName + "(.7z)").c_str (), curlVersion->version); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - - if (result.first == ftp_crt_HOST_NOT_ACCEPTING) - { - if (lang.hasString ("HostNotAcceptingDataConnections", - languageList[i]) == true) - { - clientInterface->sendTextMessage (lang.getString - ("HostNotAcceptingDataConnections", - languageList[i]), -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - else - { - clientInterface->sendTextMessage - ("*Warning* the host is not accepting data connections.", - -1, lang.isLanguageLocal (languageList[i]), - languageList[i]); - } - } - } - sleep (1); - - console.addLine (result.second, true); - } - } - else if (type == ftp_cct_TempFile) - { - getInProgressSavedGameFromFTPServerInProgress = false; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread->getProgressMutex () : - NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); -//fileFTPProgressList.erase(itemName); - std::map < string, pair < int, - string > >::iterator - iterFind = fileFTPProgressList.find (itemName); - if (iterFind == fileFTPProgressList.end ()) - { - iterFind = - fileFTPProgressList.find (GameConstants:: - saveNetworkGameFileServerCompressed); - if (iterFind == fileFTPProgressList.end ()) - { - iterFind = - fileFTPProgressList.find (GameConstants:: - saveNetworkGameFileClientCompressed); - } - } - if (iterFind != fileFTPProgressList.end ()) - { - fileFTPProgressList.erase (iterFind->first); - } - safeMutexFTPProgress.ReleaseLock (); - -//printf("Status update downloading saved game file: [%s]\n",itemName.c_str()); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); -//const GameSettings *gameSettings = clientInterface->getGameSettings(); - - if (result.first == ftp_crt_SUCCESS) - { - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("JoinPlayerToCurrentGameSuccessDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("JoinPlayerToCurrentGameSuccessDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s SUCCESSFULLY downloaded the saved game: %s", - getHumanPlayerName ().c_str (), itemName.c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - sleep (1); - - if (itemName == GameConstants::saveNetworkGameFileClientCompressed) - { - string saveGameFilePath = "temp/"; - string - saveGameFile = - saveGameFilePath + - string (GameConstants::saveNetworkGameFileClientCompressed); - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - saveGameFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - + saveGameFilePath; - saveGameFile = - saveGameFilePath + - string (GameConstants::saveNetworkGameFileClientCompressed); - } - else - { - string - userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameFilePath = userData + saveGameFilePath; - saveGameFile = - saveGameFilePath + - string (GameConstants::saveNetworkGameFileClientCompressed); - } - - string - extractedFileName = - saveGameFilePath + - string (GameConstants::saveNetworkGameFileClient); - bool - extract_result = - extractFileFromZIPFile (saveGameFile, extractedFileName); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Saved game [%s] compressed to [%s] returned: %d\n", - saveGameFile.c_str (), extractedFileName.c_str (), - extract_result); - } - readyToJoinInProgressGame = true; - -//printf("Success downloading saved game file: [%s]\n",itemName.c_str()); - } - else - { - curl_version_info_data * - curlVersion = curl_version_info (CURLVERSION_NOW); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - char - szMsg[8096] = ""; - if (lang.hasString ("JoinPlayerToCurrentGameFailDownload", - languageList[i]) == true) - { - snprintf (szMsg, 8096, - lang.getString ("JoinPlayerToCurrentGameFailDownload", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), itemName.c_str (), - curlVersion->version); - } - else - { - snprintf (szMsg, 8096, - "Player: %s FAILED to download the saved game: [%s] using CURL version [%s]", - getHumanPlayerName ().c_str (), itemName.c_str (), - curlVersion->version); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - - if (result.first == ftp_crt_HOST_NOT_ACCEPTING) - { - if (lang.hasString ("HostNotAcceptingDataConnections", - languageList[i]) == true) - { - clientInterface->sendTextMessage (lang.getString - ("HostNotAcceptingDataConnections", - languageList[i]), -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - else - { - clientInterface->sendTextMessage - ("*Warning* the host is not accepting data connections.", - -1, lang.isLanguageLocal (languageList[i]), - languageList[i]); - } - } - } - sleep (1); - - console.addLine (result.second, true); - } - } - } - - void - MenuStateConnectedGame::setupUIFromGameSettings (GameSettings * - gameSettings, - bool errorOnMissingData) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - - updateDataSynchDetailText = true; - vector < string > tilesets, techtree; - - if (gameSettings == NULL) - { - throw megaglest_runtime_error ("gameSettings == NULL"); - } - - - checkBoxScenario.setValue ((gameSettings->getScenario () != "")); - if (checkBoxScenario.getValue () == true) - { - int - originalFOWValue = listBoxFogOfWar.getSelectedItemIndex (); - - string scenario = gameSettings->getScenario (); - listBoxScenario.setSelectedItem (formatString (scenario)); - string file = Scenario::getScenarioPath (dirList, scenario); - - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, &scenarioInfo, isTutorial); - - gameSettings->setScenarioDir (Scenario::getScenarioPath - (dirList, scenarioInfo.name)); - - gameSettings->setDefaultResources (scenarioInfo.defaultResources); - gameSettings->setDefaultUnits (scenarioInfo.defaultUnits); - gameSettings-> - setDefaultVictoryConditions (scenarioInfo.defaultVictoryConditions); - - if (scenarioInfo.fogOfWar == false - && scenarioInfo.fogOfWar_exploredFlag == false) - { - listBoxFogOfWar.setSelectedItemIndex (2); - } - else if (scenarioInfo.fogOfWar_exploredFlag == true) - { - listBoxFogOfWar.setSelectedItemIndex (1); - } - else - { - listBoxFogOfWar.setSelectedItemIndex (0); - } - - checkBoxAllowTeamUnitSharing. - setValue (scenarioInfo.allowTeamUnitSharing); - checkBoxAllowTeamResourceSharing. - setValue (scenarioInfo.allowTeamResourceSharing); - - if (originalFOWValue != listBoxFogOfWar.getSelectedItemIndex ()) - { - cleanupMapPreviewTexture (); - } - } - - if (listBoxMapFilter.getSelectedItemIndex () != - gameSettings->getMapFilter ()) - { - switchToMapGroup (gameSettings->getMapFilter ()); -// printf("Switching to Map filter group %d \n",gameSettings->getMapFilter()); - } - - string - scenarioDir = - Scenario::getScenarioDir (dirList, gameSettings->getScenario ()); - setupMapList (gameSettings->getScenario ()); - setupTechList (gameSettings->getScenario ()); - setupTilesetList (gameSettings->getScenario ()); - - -//printf("A gameSettings->getTileset() [%s]\n",gameSettings->getTileset().c_str()); - - if (getMissingTilesetFromFTPServerInProgress == false - && gameSettings->getTileset () != "") - { -// tileset - tilesets = tilesetFiles; - std::for_each (tilesets.begin (), tilesets.end (), FormatString ()); - - if (std::find (tilesetFiles.begin (), tilesetFiles.end (), - gameSettings->getTileset ()) != tilesetFiles.end ()) - { - lastMissingTileSet = ""; - getMissingTilesetFromFTPServer = ""; - listBoxTileset.setSelectedItem (formatString - (gameSettings->getTileset ())); - } - else - { -// try to get the tileset via ftp - if (ftpClientThread != NULL && - (getMissingTilesetFromFTPServer != gameSettings->getTileset () - || difftime (time (NULL), - getMissingTilesetFromFTPServerLastPrompted) > - REPROMPT_DOWNLOAD_SECONDS)) - { - if (ftpMessageBox.getEnabled () == false) - { - getMissingTilesetFromFTPServerLastPrompted = time (NULL); - getMissingTilesetFromFTPServer = gameSettings->getTileset (); - Lang & lang = Lang::getInstance (); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s %s ?", - lang. - getString ("DownloadMissingTilesetQuestion").c_str (), - gameSettings->getTileset ().c_str ()); - -// Is the item in the mod center? - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (tilesetCacheList.find (getMissingTilesetFromFTPServer) == - tilesetCacheList.end ()) - { - ftpMessageBox.init (lang.getString ("Yes"), - lang.getString ("NoDownload")); - } - else - { - ftpMessageBox.init (lang.getString ("ModCenter"), - lang.getString ("GameHost")); - ftpMessageBox.addButton (lang.getString ("NoDownload")); - } - safeMutexThread.ReleaseLock (); - - ftpMissingDataType = ftpmsg_MissingTileset; - showFTPMessageBox (szBuf, lang.getString ("Question"), false); - } - } - - tilesets. - push_back (Lang:: - getInstance ().getString ("DataMissing", "", true)); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (lastMissingTileSet != gameSettings->getTileset ()) - { - lastMissingTileSet = gameSettings->getTileset (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTileset", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTileset", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - gameSettings->getTileset ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is missing the tileset: %s", - getHumanPlayerName ().c_str (), - gameSettings->getTileset ().c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - } - - listBoxTileset.setItems (tilesets); - listBoxTileset. - setSelectedItem (Lang:: - getInstance ().getString ("DataMissing", "", - true)); - } - - } - - if (getMissingTechtreeFromFTPServerInProgress == false && - gameSettings->getTech () != "") - { -// techtree - techtree = techTreeFiles; - std::for_each (techtree.begin (), techtree.end (), FormatString ()); - - if (std::find (techTreeFiles.begin (), techTreeFiles.end (), - gameSettings->getTech ()) != techTreeFiles.end ()) - { - - lastMissingTechtree = ""; - getMissingTechtreeFromFTPServer = ""; - reloadFactions (true, gameSettings->getScenario ()); - listBoxTechTree.setSelectedItem (formatString - (gameSettings->getTech ())); - } - else - { -// try to get the tileset via ftp - if (ftpClientThread != NULL - && (getMissingTechtreeFromFTPServer != gameSettings->getTech () - || difftime (time (NULL), - getMissingTechtreeFromFTPServerLastPrompted) > - REPROMPT_DOWNLOAD_SECONDS)) - { - if (ftpMessageBox.getEnabled () == false) - { - getMissingTechtreeFromFTPServerLastPrompted = time (NULL); - getMissingTechtreeFromFTPServer = gameSettings->getTech (); - Lang & lang = Lang::getInstance (); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s %s ?", - lang. - getString ("DownloadMissingTechtreeQuestion").c_str - (), gameSettings->getTech ().c_str ()); - -// Is the item in the mod center? - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (techCacheList.find (getMissingTechtreeFromFTPServer) == - techCacheList.end ()) - { - ftpMessageBox.init (lang.getString ("Yes"), - lang.getString ("NoDownload")); - } - else - { - ftpMessageBox.init (lang.getString ("ModCenter"), - lang.getString ("GameHost")); - ftpMessageBox.addButton (lang.getString ("NoDownload")); - } - safeMutexThread.ReleaseLock (); - - ftpMissingDataType = ftpmsg_MissingTechtree; - showFTPMessageBox (szBuf, lang.getString ("Question"), false); - } - } - - techtree. - push_back (Lang:: - getInstance ().getString ("DataMissing", "", true)); - - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - const GameSettings * - gameSettings = clientInterface->getGameSettings (); - - if (lastMissingTechtree != gameSettings->getTech ()) - { - lastMissingTechtree = gameSettings->getTech (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Lang & lang = Lang::getInstance (); - const - vector < - string > - languageList = - clientInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int i = 0; i < languageList.size (); ++i) - { - - char - szMsg[8096] = ""; - if (lang.hasString ("DataMissingTechtree", languageList[i]) == - true) - { - snprintf (szMsg, 8096, - lang.getString ("DataMissingTechtree", - languageList[i]).c_str (), - getHumanPlayerName ().c_str (), - gameSettings->getTech ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "Player: %s is missing the techtree: %s", - getHumanPlayerName ().c_str (), - gameSettings->getTech ().c_str ()); - } - clientInterface->sendTextMessage (szMsg, -1, - lang.isLanguageLocal - (languageList[i]), - languageList[i]); - } - } - - vector < string > translatedTechs; - for (unsigned int i = 0; i < techTreeFiles.size (); i++) - { - string - txTech = techTree->getTranslatedName (techTreeFiles.at (i)); - translatedTechs.push_back (txTech); - } - listBoxTechTree.setItems (techtree, translatedTechs); - listBoxTechTree. - setSelectedItem (Lang:: - getInstance ().getString ("DataMissing", "", - true)); - } - } - -// factions - bool hasFactions = true; - if (currentFactionName != gameSettings->getTech () - && gameSettings->getTech () != "") - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] hasFactions = %d, currentFactionName [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, hasFactions, - currentFactionName.c_str ()); - currentFactionName = gameSettings->getTech (); - hasFactions = loadFactions (gameSettings, false); - } - else - { -// do this to process special faction types like observers - loadFactions (gameSettings, false); - } - - - if (getMissingMapFromFTPServerInProgress == false && - gameSettings->getMap () != "" - && gameSettings->getMapFilter () == - listBoxMapFilter.getSelectedItemIndex ()) - { -// map - bool missingMap = false; - string mapFile = gameSettings->getMap (); - mapFile = formatString (mapFile); - - if (currentMap != gameSettings->getMap ()) - { // load the setup again - currentMap = gameSettings->getMap (); - } - bool - mapLoaded = - loadMapInfo (Config::getMapPath (currentMap, scenarioDir, false), - &mapInfo, true); - if (mapLoaded == false) - { -// try to get the map via ftp - if (ftpClientThread != NULL - && (getMissingMapFromFTPServer != currentMap - || difftime (time (NULL), - getMissingMapFromFTPServerLastPrompted) > - REPROMPT_DOWNLOAD_SECONDS)) - { - if (ftpMessageBox.getEnabled () == false) - { - getMissingMapFromFTPServerLastPrompted = time (NULL); - getMissingMapFromFTPServer = currentMap; - Lang & lang = Lang::getInstance (); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s %s ?", - lang. - getString ("DownloadMissingMapQuestion").c_str (), - currentMap.c_str ()); - -// Is the item in the mod center? - MutexSafeWrapper - safeMutexThread ((modHttpServerThread != - NULL ? - modHttpServerThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (mapCacheList.find (getMissingMapFromFTPServer) == - mapCacheList.end ()) - { - ftpMessageBox.init (lang.getString ("Yes"), - lang.getString ("NoDownload")); - } - else - { - ftpMessageBox.init (lang.getString ("ModCenter"), - lang.getString ("GameHost")); - ftpMessageBox.addButton (lang.getString ("NoDownload")); - } - safeMutexThread.ReleaseLock (); - - ftpMissingDataType = ftpmsg_MissingMap; - showFTPMessageBox (szBuf, lang.getString ("Question"), false); - } - } - - formattedPlayerSortedMaps[gameSettings-> - getMapFilter ()].push_back (Lang:: - getInstance - ().getString - ("DataMissing", - "", true)); - mapFile = Lang::getInstance ().getString ("DataMissing", "", true); - missingMap = true; - } - - if (isHeadlessAdmin () && !missingMap - && mapFile != listBoxMap.getSelectedItem ()) - { -//console.addLine("Headless server does not have map, switching to next one"); - if (isfirstSwitchingMapMessage) - { - isfirstSwitchingMapMessage = false; - } - else - { - console.addLine (Lang::getInstance ().getString - ("HeadlessServerDoesNotHaveMap", "", true)); - } - } - listBoxMap.setItems (formattedPlayerSortedMaps - [gameSettings->getMapFilter ()]); - -//printf("Setting map from game settings map:%s , settingsfilter=%d , boxfilter=%d \n",gameSettings->getMap().c_str(),gameSettings->getMapFilter(),listBoxMapFilter.getSelectedItemIndex()); - listBoxMap.setSelectedItem (mapFile); - labelMapInfo.setText (mapInfo.desc); - } - -// FogOfWar - if (checkBoxScenario.getValue () == false) - { - int - originalFOWValue = listBoxFogOfWar.getSelectedItemIndex (); - listBoxFogOfWar.setSelectedItemIndex (0); // default is 0! - if (gameSettings->getFogOfWar () == false) - { - listBoxFogOfWar.setSelectedItemIndex (2); - } - if ((gameSettings->getFlagTypes1 () & ft1_show_map_resources) == - ft1_show_map_resources) - { - if (gameSettings->getFogOfWar () == true) - { - listBoxFogOfWar.setSelectedItemIndex (1); - } - } - if (originalFOWValue != listBoxFogOfWar.getSelectedItemIndex ()) - { - cleanupMapPreviewTexture (); - } - } - -// Allow Observers - if (gameSettings->getAllowObservers ()) - { - checkBoxAllowObservers.setValue (true); - } - else - { - checkBoxAllowObservers.setValue (false); - } - - if ((gameSettings->getFlagTypes1 () & ft1_allow_team_switching) == - ft1_allow_team_switching) - { - checkBoxEnableSwitchTeamMode.setValue (true); - } - else - { - checkBoxEnableSwitchTeamMode.setValue (false); - } - listBoxAISwitchTeamAcceptPercent.setSelectedItem (intToStr - (gameSettings->getAiAcceptSwitchTeamPercentChance - ())); - listBoxFallbackCpuMultiplier. - setSelectedItemIndex (gameSettings->getFallbackCpuMultiplier ()); - - if ((gameSettings->getFlagTypes1 () & ft1_allow_shared_team_units) == - ft1_allow_shared_team_units) - { - checkBoxAllowTeamUnitSharing.setValue (true); - } - else - { - checkBoxAllowTeamUnitSharing.setValue (false); - } - - if ((gameSettings->getFlagTypes1 () & ft1_allow_shared_team_resources) - == ft1_allow_shared_team_resources) - { - checkBoxAllowTeamResourceSharing.setValue (true); - } - else - { - checkBoxAllowTeamResourceSharing.setValue (false); - } - - checkBoxAllowNativeLanguageTechtree. - setValue (gameSettings->getNetworkAllowNativeLanguageTechtree ()); - -// Control - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxControls[i].setSelectedItemIndex (ctClosed); - - if (isHeadlessAdmin () == false) - { - if (clientInterface->getJoinGameInProgress () == false) - { - listBoxFactions[i].setEditable (false); - listBoxTeams[i].setEditable (false); - } - } - - labelPlayerStatus[i].setTexture (NULL);; - } - - if (hasFactions == true && gameSettings != NULL) - { - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - -//for(int i=0; i < gameSettings->getFactionCount(); ++i){ - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - int - slot = gameSettings->getStartLocationIndex (i); - - if (slot == clientInterface->getPlayerIndex ()) - { - labelPlayerNames[slot].setEditable (true); - } - else - { - labelPlayerNames[slot].setEditable (false); - } - - if (i >= mapInfo.players) - { - if (gameSettings->getFactionControl (i) != ctNetworkUnassigned) - { - continue; - } - else if (clientInterface->getPlayerIndex () != slot) - { - continue; - } - } - - if (gameSettings->getFactionControl (i) == ctNetwork || - gameSettings->getFactionControl (i) == ctNetworkUnassigned || - gameSettings->getFactionControl (i) == ctHuman) - { - switch (gameSettings->getNetworkPlayerStatuses (i)) - { - case npst_BeRightBack: - labelPlayerStatus[slot]. - setTexture (CoreData::getInstance ().getStatusBRBTexture ()); - break; - case npst_Ready: - labelPlayerStatus[slot]. - setTexture (CoreData:: - getInstance ().getStatusReadyTexture ()); - break; - case npst_PickSettings: - labelPlayerStatus[slot]. - setTexture (CoreData:: - getInstance ().getStatusNotReadyTexture ()); - break; - case npst_Disconnected: - labelPlayerStatus[slot].setTexture (NULL); - break; - - default: - labelPlayerStatus[slot].setTexture (NULL); - break; - } - } - - listBoxControls[slot]. - setSelectedItemIndex (gameSettings->getFactionControl (i), - errorOnMissingData); - listBoxRMultiplier[slot]. - setSelectedItemIndex (gameSettings->getResourceMultiplierIndex - (i), errorOnMissingData); - listBoxTeams[slot].setSelectedItemIndex (gameSettings->getTeam (i), - errorOnMissingData); - listBoxFactions[slot].setSelectedItem (formatString - (gameSettings-> - getFactionTypeName (i)), - false); - - if (gameSettings->getFactionControl (i) == ctNetwork || - gameSettings->getFactionControl (i) == ctNetworkUnassigned) - { - labelNetStatus[slot]. - setText (gameSettings->getNetworkPlayerName (i)); - if (gameSettings->getThisFactionIndex () != i - && gameSettings->getNetworkPlayerName (i) != "" - && gameSettings->getNetworkPlayerName (i) != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - labelPlayerNames[slot]. - setText (gameSettings->getNetworkPlayerName (i)); - } - } - - ControlType ct = gameSettings->getFactionControl (i); - if (ct == ctHuman || ct == ctNetwork || ct == ctClosed) - { - listBoxRMultiplier[slot].setEnabled (false); - listBoxRMultiplier[slot].setVisible (false); - } - else - { - listBoxRMultiplier[slot].setEnabled (true); - listBoxRMultiplier[slot].setVisible (true); - } - - if ((gameSettings->getFactionControl (i) == ctNetwork || - gameSettings->getFactionControl (i) == ctNetworkUnassigned) && - gameSettings->getThisFactionIndex () == i) - { - -// set my current slot to ctHuman - if (gameSettings->getFactionControl (i) != ctNetworkUnassigned) - { - listBoxControls[slot].setSelectedItemIndex (ctHuman); - } - if (checkBoxScenario.getValue () == false) - { - if (clientInterface->getJoinGameInProgress () == false) - { - if (i <= mapInfo.hardMaxPlayers) - { - listBoxFactions[slot].setEditable (true); - listBoxTeams[slot].setEditable (true); - } - else - { - // looks more must be done to allow people to take an observer - // slot after the game has started. Extra network slots close - // when the game starts. - listBoxFactions[slot].setEditable (checkBoxAllowObservers.getValue ()); - listBoxTeams[slot].setEditable (checkBoxAllowObservers.getValue ()); - } - } - } - - if (labelPlayerNames[slot].getText () == "" && - gameSettings->getNetworkPlayerName (i) != "" && - gameSettings->getNetworkPlayerName (i) != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - labelPlayerNames[slot]. - setText (gameSettings->getNetworkPlayerName (i)); - } - } - } - settingsReceivedFromServer = true; - initialSettingsReceivedFromServer = true; - - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - - if (enableFactionTexturePreview == true) - { - if (clientInterface != NULL && clientInterface->isConnected () && - gameSettings != NULL) - { - - if (currentTechName_factionPreview != gameSettings->getTech () || - currentFactionName_factionPreview != - gameSettings-> - getFactionTypeName (gameSettings->getThisFactionIndex ())) - { - - currentTechName_factionPreview = gameSettings->getTech (); - currentFactionName_factionPreview = - gameSettings-> - getFactionTypeName (gameSettings->getThisFactionIndex ()); - - initFactionPreview (gameSettings); - } - } - } - - } - - void - MenuStateConnectedGame::initFactionPreview (const GameSettings * - gameSettings) - { - string factionVideoUrl = ""; - string factionVideoUrlFallback = ""; - - string - factionDefinitionXML = - Game::findFactionLogoFile (gameSettings, NULL, - currentFactionName_factionPreview + - ".xml"); - if (factionDefinitionXML != "" - && currentFactionName_factionPreview != - GameConstants::RANDOMFACTION_SLOTNAME - && currentFactionName_factionPreview != - GameConstants::OBSERVER_SLOTNAME - && fileExists (factionDefinitionXML) == true) - { - XmlTree xmlTree; - std::map < string, string > mapExtraTagReplacementValues; - xmlTree.load (factionDefinitionXML, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues)); - const XmlNode * - factionNode = xmlTree.getRootNode (); - if (factionNode->hasAttribute ("faction-preview-video") == true) - { - factionVideoUrl = - factionNode->getAttribute ("faction-preview-video")->getValue (); - } - - factionVideoUrlFallback = - Game::findFactionLogoFile (gameSettings, NULL, "preview_video.*"); - if (factionVideoUrl == "") - { - factionVideoUrl = factionVideoUrlFallback; - factionVideoUrlFallback = ""; - } - } - - if (factionVideoUrl != "") - { - if (CoreData::getInstance ().getMenuMusic ()->getVolume () != 0) - { - CoreData::getInstance ().getMenuMusic ()->setVolume (0); - factionVideoSwitchedOffVolume = true; - } - - if (currentFactionLogo != factionVideoUrl) - { - currentFactionLogo = factionVideoUrl; - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - string introVideoFile = factionVideoUrl; - string introVideoFileFallback = factionVideoUrlFallback; - - Context * - c = GraphicsInterface::getInstance ().getCurrentContext (); - PlatformContextGl * - glCtx = - static_cast < ContextGl * >(c)->getPlatformContextGlPtr (); - SDL_Window * - window = glCtx->getScreenWindow (); - SDL_Surface * - screen = glCtx->getScreenSurface (); - - string - vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); -//printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); - factionVideo = new VideoPlayer (&Renderer::getInstance (), - introVideoFile, - introVideoFileFallback, - window, - 0, 0, - screen->w, - screen->h, - screen->format->BitsPerPixel, - true, - vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - factionVideo->initPlayer (); - } - } - } - else - { -//switch on music again!! - Config & config = Config::getInstance (); - float - configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - if (factionVideoSwitchedOffVolume) - { - if (CoreData::getInstance ().getMenuMusic ()->getVolume () != - configVolume) - { - CoreData::getInstance (). - getMenuMusic ()->setVolume (configVolume); - } - factionVideoSwitchedOffVolume = false; - } - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - } - - if (factionVideo == NULL) - { - string - factionLogo = - Game::findFactionLogoFile (gameSettings, NULL, - GameConstants::PREVIEW_SCREEN_FILE_FILTER); - if (factionLogo == "") - { - factionLogo = Game::findFactionLogoFile (gameSettings, NULL); - } - if (currentFactionLogo != factionLogo) - { - currentFactionLogo = factionLogo; - loadFactionTexture (currentFactionLogo); - } - } - } - - void - MenuStateConnectedGame::RestoreLastGameSettings () - { -// Ensure we have set the gamesettings at least once - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface * - clientInterface = networkManager.getClientInterface (); - GameSettings gameSettings = *clientInterface->getGameSettings (); - CoreData:: - getInstance ().loadGameSettingsFromFile (HEADLESS_SAVED_GAME_FILENAME, - &gameSettings); - if (gameSettings.getMap () == "") - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - loadGameSettings (&gameSettings); - } - - setupUIFromGameSettings (&gameSettings, false); - - needToBroadcastServerSettings = true; - broadcastServerSettingsDelayTimer = time (NULL); - noReceiveTimer = time (NULL); - - } - - int - MenuStateConnectedGame::setupMapList (string scenario) - { - int - initialMapSelection = 0; - - try - { - Config & config = Config::getInstance (); - vector < string > invalidMapList; - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > pathList = - config.getPathListForType (ptMaps, scenarioDir); - vector < string > allMaps = - MapPreview::findAllValidMaps (pathList, scenarioDir, false, true, - &invalidMapList); -// sort map list non case sensitive - std::sort (allMaps.begin (), allMaps.end (), compareNonCaseSensitive); - if (scenario != "") - { - vector < string > allMaps2 = - MapPreview:: - findAllValidMaps (config.getPathListForType (ptMaps, ""), "", - false, true, &invalidMapList); - copy (allMaps2.begin (), allMaps2.end (), - std::inserter (allMaps, allMaps.begin ())); - std::sort (allMaps.begin (), allMaps.end (), - compareNonCaseSensitive); - } - - if (allMaps.empty ()) - { - throw megaglest_runtime_error ("No maps were found!"); - } - vector < string > results; - copy (allMaps.begin (), allMaps.end (), std::back_inserter (results)); - mapFiles = results; - - for (unsigned int i = 0; i < GameConstants::maxPlayers + 1; ++i) - { - playerSortedMaps[i].clear (); - formattedPlayerSortedMaps[i].clear (); - } - - copy (mapFiles.begin (), mapFiles.end (), - std::back_inserter (playerSortedMaps[0])); - copy (playerSortedMaps[0].begin (), playerSortedMaps[0].end (), - std::back_inserter (formattedPlayerSortedMaps[0])); - std::for_each (formattedPlayerSortedMaps[0].begin (), - formattedPlayerSortedMaps[0].end (), FormatString ()); - - formattedMapFiles.clear (); - for (int i = 0; i < (int) mapFiles.size (); i++) - { // fetch info and put map in right list - loadMapInfo (Config::getMapPath - (mapFiles.at (i), scenarioDir, false), &mapInfo, - false); - - if (GameConstants::maxPlayers + 1 <= mapInfo.players) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Sorted map list [%d] does not match\ncurrent map playercount [%d]\nfor file [%s]\nmap [%s]", - GameConstants::maxPlayers + 1, mapInfo.players, - Config::getMapPath (mapFiles.at (i), "", - false).c_str (), - mapInfo.desc.c_str ()); - throw megaglest_runtime_error (szBuf); - } - playerSortedMaps[mapInfo.hardMaxPlayers].push_back (mapFiles.at (i)); - formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].push_back (formatString - (mapFiles.at - (i))); - if (config.getString ("InitialMap", "Conflict") == - formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].back ()) - { - initialMapSelection = i; - } - formattedMapFiles.push_back (formatString (mapFiles.at (i))); - } - - if (scenario != "") - { - string file = Scenario::getScenarioPath (dirList, scenario); - loadScenarioInfo (file, &scenarioInfo); - - loadMapInfo (Config::getMapPath - (scenarioInfo.mapName, scenarioDir, true), &mapInfo, - false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line %d] listBoxMap.getSelectedItemIndex() = %d, mapFiles.size() = " - MG_SIZE_T_SPECIFIER - ", mapInfo.players = %d, formattedPlayerSortedMaps[mapInfo.players].size() = " - MG_SIZE_T_SPECIFIER ", scenarioInfo.mapName [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, listBoxMap.getSelectedItemIndex (), mapFiles.size (), - mapInfo.players, - formattedPlayerSortedMaps[mapInfo.players].size (), - scenarioInfo.mapName.c_str ()); - listBoxMap.setItems (formattedPlayerSortedMaps[mapInfo.players]); - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); -//abort(); - } - - return initialMapSelection; - } - - int - MenuStateConnectedGame::setupTechList (string scenario, bool forceLoad) - { - int - initialTechSelection = 0; - try - { - Config & config = Config::getInstance (); - - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > results; - vector < string > techPaths = - config.getPathListForType (ptTechs, scenarioDir); - findDirs (techPaths, results); - - if (results.empty ()) - { - throw megaglest_runtime_error ("No tech-trees were found!"); - } - - techTreeFiles = results; - - vector < string > translatedTechs; - - for (unsigned int i = 0; i < results.size (); i++) - { -//printf("TECHS i = %d results [%s] scenario [%s]\n",i,results[i].c_str(),scenario.c_str()); - - results.at (i) = formatString (results.at (i)); - if (config.getString ("InitialTechTree", "Megapack") == - results.at (i)) - { - initialTechSelection = i; - } - string - txTech = - techTree->getTranslatedName (techTreeFiles.at (i), forceLoad); - translatedTechs.push_back (formatString (txTech)); - } - - - listBoxTechTree.setItems (results, translatedTechs); - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - - return initialTechSelection; - } - - void - MenuStateConnectedGame::setupTilesetList (string scenario) - { - try - { - Config & config = Config::getInstance (); - - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - - vector < string > results; - findDirs (config.getPathListForType (ptTilesets, scenarioDir), - results); - if (results.empty ()) - { -//throw megaglest_runtime_error("No tile-sets were found!"); - showMessageBox ("No tile-sets were found!", "Error", false); - } - else - { - tilesetFiles = results; - std::for_each (results.begin (), results.end (), FormatString ()); - - listBoxTileset.setItems (results); - } - } - catch (const std::exception & ex) - { - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - } - - void - MenuStateConnectedGame::loadScenarioInfo (string file, - ScenarioInfo * scenarioInfo) - { - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, scenarioInfo, isTutorial); - - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - } - -}} //end namespace + if (factionFiles[listBoxFactions[i].getSelectedItemIndex()] != + formatString(GameConstants::OBSERVER_SLOTNAME)) { + if (listBoxTeams[i].getSelectedItemIndex() + 1 != + (GameConstants::maxPlayers + fpt_Observer)) { + //lastSelectedTeamIndex[i] = listBoxTeams[i].getSelectedItemIndex(); + } + } + + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + } + } + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showMessageBox(szBuf, "Error", false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void + MenuStateConnectedGame::PlayNow(bool saveGame) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + GameSettings gameSettings = *clientInterface->getGameSettings(); + loadGameSettings(&gameSettings); + + if (saveGame == true) { + CoreData:: + getInstance().saveGameSettingsToFile(HEADLESS_SAVED_GAME_FILENAME, + &gameSettings, true); + } + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("Client sending map [%s] admin key [%d]\n",gameSettings.getMap().c_str(),gameSettings.getMasterserver_admin()); + + if (clientInterface->getJoinGameInProgress() == true) { + if (readyToJoinInProgressGame == false && launchingNewGame == false) { + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("JoinPlayerToCurrentGameLaunch", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("JoinPlayerToCurrentGameLaunch", + languageList[i]).c_str(), + getHumanPlayerName().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is about to join the game, please wait...", + getHumanPlayerName().c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + + sleep(1); + launchingNewGame = true; + clientInterface->broadcastGameStart(&gameSettings); + } + return; + } else { + launchingNewGame = true; + broadCastGameSettingsToHeadlessServer(needToBroadcastServerSettings); + clientInterface->broadcastGameStart(&gameSettings); + } + } + + void + MenuStateConnectedGame::switchToNextMapGroup(const int direction) { + int + i = listBoxMapFilter.getSelectedItemIndex(); + // if there are no maps for the current selection we switch to next selection + while (formattedPlayerSortedMaps[i].empty()) { + i = i + direction; + if (i > GameConstants::maxPlayers) { + i = 0; + } + if (i < 0) { + i = GameConstants::maxPlayers; + } + } + switchToMapGroup(i); + } + + void + MenuStateConnectedGame::switchToMapGroup(int filterIndex) { + int + i = filterIndex; + listBoxMapFilter.setSelectedItemIndex(i); + listBoxMap.setItems(formattedPlayerSortedMaps[i]); + // printf("switching map group to filter=%d mapgroup has %d maps. map=%s \n",i, + // (int)formattedPlayerSortedMaps[i].size(),formattedPlayerSortedMaps[i][0].c_str()); + } + + string MenuStateConnectedGame::getCurrentMapFile() { + int + i = listBoxMapFilter.getSelectedItemIndex(); + int + mapIndex = listBoxMap.getSelectedItemIndex(); + if (playerSortedMaps[i].empty() == false) { + return playerSortedMaps[i].at(mapIndex); + } + return ""; + } + + void + MenuStateConnectedGame::reloadFactions(bool keepExistingSelectedItem, + string scenario) { + vector < string > results; + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > techPaths = + config.getPathListForType(ptTechs, scenarioDir); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + + if (listBoxTechTree.getSelectedItemIndex() >= 0 + && listBoxTechTree.getSelectedItemIndex() < + (int) techTreeFiles.size()) { + findDirs(techPath + + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + + "/factions/", results, false, false); + } + if (results.empty() == false) { + break; + } + } + + if (results.empty() == true) { + //throw megaglest_runtime_error("(2)There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"); + //showGeneralError=true; + //generalErrorToShow = "[#2] There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"; + } + + vector < string > translatedFactionNames; + factionFiles = results; + for (int i = 0; i < (int) results.size(); ++i) { + results[i] = formatString(results[i]); + string + translatedString = + techTree->getTranslatedFactionName(techTreeFiles + [listBoxTechTree. + getSelectedItemIndex()], + factionFiles[i]); + if (toLower(translatedString) == toLower(results[i])) { + translatedFactionNames.push_back(results[i]); + } else { + translatedFactionNames.push_back(results[i] + " (" + + translatedString + ")"); + } + //printf("FACTIONS i = %d results [%s]\n",i,results[i].c_str()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "Tech [%s] has faction [%s]\n", + techTreeFiles + [listBoxTechTree.getSelectedItemIndex + ()].c_str(), results[i].c_str()); + } + + results.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Random", "", + true) + "*"); + + // Add special Observer Faction + if (checkBoxAllowObservers.getValue() == 1) { + results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::OBSERVER_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Observer", "", + true) + "*"); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + int + originalIndex = listBoxFactions[i].getSelectedItemIndex(); + string + originalValue = + (listBoxFactions[i].getItemCount() > + 0 ? listBoxFactions[i].getSelectedItem() : ""); + + listBoxFactions[i].setItems(results, translatedFactionNames); + if (keepExistingSelectedItem == false || + (checkBoxAllowObservers.getValue() == true && + originalValue == + formatString(GameConstants::OBSERVER_SLOTNAME))) { + listBoxFactions[i].setSelectedItemIndex(i % results.size()); + + if (originalValue == formatString(GameConstants::OBSERVER_SLOTNAME) + && listBoxFactions[i].getSelectedItem() != + formatString(GameConstants::OBSERVER_SLOTNAME)) { + if (listBoxTeams[i].getSelectedItem() == + intToStr(GameConstants::maxPlayers + fpt_Observer)) { + listBoxTeams[i].setSelectedItem(intToStr(1)); + } + } + } else if (originalIndex < (int) results.size()) { + listBoxFactions[i].setSelectedItemIndex(originalIndex); + } + } + } + + void + MenuStateConnectedGame::loadGameSettings(GameSettings * gameSettings) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + int + factionCount = 0; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Test flags values + //gameSettings->setFlagTypes1(ft1_show_map_resources); + // + + if (checkBoxScenario.getValue() == true) { + gameSettings->setScenario(scenarioInfo.name); + gameSettings->setScenarioDir(Scenario::getScenarioPath + (dirList, scenarioInfo.name)); + + gameSettings->setDefaultResources(scenarioInfo.defaultResources); + gameSettings->setDefaultUnits(scenarioInfo.defaultUnits); + gameSettings-> + setDefaultVictoryConditions(scenarioInfo.defaultVictoryConditions); + } else { + gameSettings->setScenario(""); + gameSettings->setScenarioDir(""); + } + + gameSettings->setNetworkAllowNativeLanguageTechtree + (checkBoxAllowNativeLanguageTechtree.getValue()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line %d] listBoxMap.getSelectedItemIndex() = %d, mapFiles.size() = " + MG_SIZE_T_SPECIFIER ", getCurrentMapFile() [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, listBoxMap.getSelectedItemIndex(), mapFiles.size(), + getCurrentMapFile().c_str()); + + if (listBoxMap.getSelectedItemIndex() >= 0 + && listBoxMap.getSelectedItemIndex() < (int) mapFiles.size()) { + gameSettings->setDescription(formatString(getCurrentMapFile())); + gameSettings->setMap(getCurrentMapFile()); + gameSettings->setMapFilter(listBoxMapFilter.getSelectedItemIndex()); + } else { + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingMap=Player", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingMap=Player", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + listBoxMap.getSelectedItem().c_str()); + } else { + snprintf(szMsg, 8096, "Player: %s is missing the map: %s", + getHumanPlayerName().c_str(), + listBoxMap.getSelectedItem().c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + } + + if (listBoxTileset.getSelectedItemIndex() >= 0 + && listBoxTileset.getSelectedItemIndex() < + (int) tilesetFiles.size()) { + gameSettings->setTileset(tilesetFiles + [listBoxTileset.getSelectedItemIndex()]); + } else { + //printf("A loadGameSettings listBoxTileset.getSelectedItemIndex() = %d tilesetFiles.size() = %d\n",listBoxTileset.getSelectedItemIndex(),tilesetFiles.size()); + + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTileset=Player", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTileset=Player", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + listBoxTileset.getSelectedItem().c_str()); + } else { + snprintf(szMsg, 8096, "Player: %s is missing the tileset: %s", + getHumanPlayerName().c_str(), + listBoxTileset.getSelectedItem().c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + } + if (listBoxTechTree.getSelectedItemIndex() >= 0 + && listBoxTechTree.getSelectedItemIndex() < + (int) techTreeFiles.size()) { + gameSettings->setTech(techTreeFiles + [listBoxTechTree.getSelectedItemIndex()]); + } else { + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtree=Player", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTechtree=Player", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + listBoxTechTree.getSelectedItem().c_str()); + } else { + snprintf(szMsg, 8096, "Player: %s is missing the techtree: %s", + getHumanPlayerName().c_str(), + listBoxTechTree.getSelectedItem().c_str()); + } + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(szMsg, -1, localEcho, + languageList[i]); + } + } + + if (checkBoxScenario.getValue() == false) { + gameSettings->setDefaultUnits(true); + gameSettings->setDefaultResources(true); + gameSettings->setDefaultVictoryConditions(true); + } + + gameSettings->setFogOfWar(listBoxFogOfWar.getSelectedItemIndex() == 0 + || listBoxFogOfWar.getSelectedItemIndex() == + 1); + + gameSettings->setAllowObservers(checkBoxAllowObservers.getValue() == + true); + + uint32 valueFlags1 = gameSettings->getFlagTypes1(); + if (listBoxFogOfWar.getSelectedItemIndex() == 1 || + listBoxFogOfWar.getSelectedItemIndex() == 2) { + valueFlags1 |= ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + + //gameSettings->setEnableObserverModeAtEndGame(listBoxEnableObserverMode.getSelectedItemIndex() == 0); + gameSettings->setEnableObserverModeAtEndGame(true); + //gameSettings->setPathFinderType(static_cast(listBoxPathFinderType.getSelectedItemIndex())); + + valueFlags1 = gameSettings->getFlagTypes1(); + if (checkBoxEnableSwitchTeamMode.getValue() == true) { + valueFlags1 |= ft1_allow_team_switching; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_team_switching; + gameSettings->setFlagTypes1(valueFlags1); + } + gameSettings->setAiAcceptSwitchTeamPercentChance(strToInt + (listBoxAISwitchTeamAcceptPercent.getSelectedItem + ())); + gameSettings-> + setFallbackCpuMultiplier + (listBoxFallbackCpuMultiplier.getSelectedItemIndex()); + + valueFlags1 = gameSettings->getFlagTypes1(); + if (checkBoxAllowTeamUnitSharing.getValue() == true) { + valueFlags1 |= ft1_allow_shared_team_units; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_shared_team_units; + gameSettings->setFlagTypes1(valueFlags1); + } + + valueFlags1 = gameSettings->getFlagTypes1(); + if (checkBoxAllowTeamResourceSharing.getValue() == true) { + valueFlags1 |= ft1_allow_shared_team_resources; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_shared_team_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + + // First save Used slots + //for(int i=0; i + (listBoxControls[i].getSelectedItemIndex()); + + if (ct != ctClosed) { + int + slotIndex = factionCount; + ControlType + oldControlType = gameSettings->getFactionControl(slotIndex); + gameSettings->setFactionControl(slotIndex, ct); + if (ct == ctHuman) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, slotIndex = %d, getHumanPlayerName(i) [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,slotIndex,getHumanPlayerName(i).c_str()); + + gameSettings->setThisFactionIndex(slotIndex); + gameSettings->setNetworkPlayerName(slotIndex, + getHumanPlayerName()); + gameSettings->setNetworkPlayerUUID(slotIndex, + Config:: + getInstance().getString + ("PlayerId", "")); + gameSettings->setNetworkPlayerPlatform(slotIndex, + getPlatformNameString()); + gameSettings->setNetworkPlayerStatuses(slotIndex, + getNetworkPlayerStatus + ()); + Lang & lang = Lang::getInstance(); + gameSettings->setNetworkPlayerLanguages(slotIndex, + lang.getLanguage()); + + gameSettings->setResourceMultiplierIndex(slotIndex, 5); + } else { + gameSettings->setResourceMultiplierIndex(slotIndex, + listBoxRMultiplier + [i].getSelectedItemIndex + ()); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()].c_str()); + + gameSettings->setFactionTypeName(slotIndex, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()]); + if (factionFiles[listBoxFactions[i].getSelectedItemIndex()] == + formatString(GameConstants::OBSERVER_SLOTNAME)) { + listBoxTeams[i].setSelectedItem(intToStr + (GameConstants::maxPlayers + + fpt_Observer)); + } + + gameSettings->setTeam(slotIndex, + listBoxTeams[i].getSelectedItemIndex()); + gameSettings->setStartLocationIndex(slotIndex, i); + //printf("!!! setStartLocationIndex #1 slotIndex = %d, i = %d\n",slotIndex, i); + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + if (oldControlType != ctNetwork + && oldControlType != ctNetworkUnassigned) { + gameSettings->setNetworkPlayerName(slotIndex, ""); + } + } else if (listBoxControls[i].getSelectedItemIndex() != ctHuman) { + AIPlayerCount++; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, playername is AI (blank)\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i); + + Lang & lang = Lang::getInstance(); + gameSettings->setNetworkPlayerName(slotIndex, + lang.getString("AI") + + intToStr(AIPlayerCount)); + labelPlayerNames[i].setText(""); + } + + factionCount++; + } else { + labelPlayerNames[i].setText(""); + } + } + + // Next save closed slots + int + closedCount = 0; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType + ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct == ctClosed) { + int + slotIndex = factionCount + closedCount; + + gameSettings->setFactionControl(slotIndex, ct); + gameSettings->setTeam(slotIndex, + listBoxTeams[i].getSelectedItemIndex()); + gameSettings->setStartLocationIndex(slotIndex, i); + //printf("!!! setStartLocationIndex #2 slotIndex = %d, i = %d\n",slotIndex, i); + + gameSettings->setResourceMultiplierIndex(slotIndex, 5); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()].c_str()); + + gameSettings->setFactionTypeName(slotIndex, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()]); + gameSettings->setNetworkPlayerStatuses(slotIndex, npst_None); + gameSettings->setNetworkPlayerName(slotIndex, + GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME); + + closedCount++; + } + } + + gameSettings->setFactionCount(factionCount); + + Config & config = Config::getInstance(); + gameSettings->setEnableServerControlledAI(config.getBool + ("ServerControlledAI", + "true")); + gameSettings-> + setNetworkFramePeriod(config.getInt("NetworkSendFrameCount", "20")); + + if (hasNetworkGameSettings() == true) { + if (gameSettings->getTileset() != "") { + if (lastCheckedCRCTilesetName != gameSettings->getTileset()) { + //console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); + lastCheckedCRCTilesetValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + gameSettings->getTileset() + string("/*"), + ".xml", NULL); + if (lastCheckedCRCTilesetValue == 0 + || lastCheckedCRCTilesetValue != + gameSettings->getTilesetCRC()) { + lastCheckedCRCTilesetValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + gameSettings->getTileset() + string("/*"), + ".xml", NULL, true); + } + lastCheckedCRCTilesetName = gameSettings->getTileset(); + } + gameSettings->setTilesetCRC(lastCheckedCRCTilesetValue); + } + + if (config.getBool("DisableServerLobbyTechtreeCRCCheck", "false") == + false) { + if (gameSettings->getTech() != "") { + if (lastCheckedCRCTechtreeName != gameSettings->getTech()) { + //console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); + lastCheckedCRCTechtreeValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/*", ".xml", NULL); + if (lastCheckedCRCTechtreeValue == 0 + || lastCheckedCRCTechtreeValue != + gameSettings->getTechCRC()) { + lastCheckedCRCTechtreeValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/*", ".xml", NULL, true); + } + + reloadFactions(true, gameSettings->getScenario()); + factionCRCList.clear(); + for (unsigned int factionIdx = 0; + factionIdx < factionFiles.size(); ++factionIdx) { + string factionName = factionFiles[factionIdx]; + if (factionName != GameConstants::RANDOMFACTION_SLOTNAME && + factionName != GameConstants::OBSERVER_SLOTNAME) { + //factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); + uint32 + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/factions/" + + factionName + "/*", ".xml", NULL); + if (factionCRC == 0) { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/factions/" + + factionName + "/*", ".xml", NULL, true); + } + factionCRCList.push_back(make_pair + (factionName, factionCRC)); + } + } + //console.addLine("Found factions: " + intToStr(factionCRCList.size())); + lastCheckedCRCTechtreeName = gameSettings->getTech(); + } + + gameSettings->setFactionCRCList(factionCRCList); + gameSettings->setTechCRC(lastCheckedCRCTechtreeValue); + } + } + + if (gameSettings->getMap() != "") { + if (lastCheckedCRCMapName != gameSettings->getMap()) { + Checksum checksum; + string + file = Config::getMapPath(gameSettings->getMap(), "", false); + //console.addLine("Checking map CRC [" + file + "]"); + checksum.addFile(file); + lastCheckedCRCMapValue = checksum.getSum(); + lastCheckedCRCMapName = gameSettings->getMap(); + } + gameSettings->setMapCRC(lastCheckedCRCMapValue); + } + } + + //replace server player by network + for (int i = 0; i < gameSettings->getFactionCount(); ++i) { + //replace by network + if (gameSettings->getFactionControl(i) == ctHuman) { + gameSettings->setFactionControl(i, ctNetwork); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void + MenuStateConnectedGame::returnToJoinMenu() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (modHttpServerThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + modHttpServerThread->setSimpleTaskInterfaceValid(false); + modHttpServerThread->signalQuit(); + //modHttpServerThread->setThreadOwnerValid(false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (modHttpServerThread->canShutdown(true) == true + && modHttpServerThread->shutdownAndWait() == true) { + delete modHttpServerThread; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + modHttpServerThread = NULL; + } + + if (ftpClientThread != NULL) { + + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + } + ftpClientThread = NULL; + } + + if (returnMenuInfo == jmSimple) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + launchingNewGame = true; + disconnectFromServer(); + mainMenu->setState(new MenuStateJoinGame(program, mainMenu)); + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + launchingNewGame = true; + disconnectFromServer(); + mainMenu->setState(new MenuStateMasterserver(program, mainMenu)); + } + } + + void + MenuStateConnectedGame::mouseMove(int x, int y, const MouseState * ms) { + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + + if (ftpMessageBox.getEnabled()) { + ftpMessageBox.mouseMove(x, y); + } + + buttonCancelDownloads.mouseMove(x, y); + buttonDisconnect.mouseMove(x, y); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxControls[i].mouseMove(x, y); + listBoxFactions[i].mouseMove(x, y); + listBoxTeams[i].mouseMove(x, y); + grabSlotButton[i].mouseMove(x, y); + } + + listBoxMap.mouseMove(x, y); + listBoxFogOfWar.mouseMove(x, y); + checkBoxAllowObservers.mouseMove(x, y); + listBoxTileset.mouseMove(x, y); + listBoxMapFilter.mouseMove(x, y); + listBoxTechTree.mouseMove(x, y); + listBoxPlayerStatus.mouseMove(x, y); + + checkBoxScenario.mouseMove(x, y); + listBoxScenario.mouseMove(x, y); + + labelAllowTeamUnitSharing.mouseMove(x, y); + checkBoxAllowTeamUnitSharing.mouseMove(x, y); + labelAllowTeamResourceSharing.mouseMove(x, y); + checkBoxAllowTeamResourceSharing.mouseMove(x, y); + + checkBoxAllowNativeLanguageTechtree.mouseMove(x, y); + + buttonPlayNow.mouseMove(x, y); + buttonRestoreLastSettings.mouseMove(x, y); + } + + bool MenuStateConnectedGame::isVideoPlaying() { + bool result = false; + if (factionVideo != NULL) { + result = factionVideo->isPlaying(); + } + return result; + } + + void + MenuStateConnectedGame::render() { + try { + Renderer & renderer = Renderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } + + renderer.renderButton(&buttonDisconnect); + + if (initialSettingsReceivedFromServer == false) { + return; + } + + if (factionTexture != NULL) { + if (factionVideo == NULL || factionVideo->isPlaying() == false) { + renderer.renderTextureQuad(800, 600, 200, 150, factionTexture, + 1); + } + } + if (factionVideo != NULL) { + if (factionVideo->isPlaying() == true) { + factionVideo->playFrame(false); + } else { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + + if (validDisplayedGamesettings) { + initFactionPreview(&displayedGamesettings); + } + } + } + } + } + + if (mapPreviewTexture != NULL) { + //renderer.renderTextureQuad(5,185,150,150,mapPreviewTexture,1.0f); + renderer.renderTextureQuad(this->render_mapPreviewTexture_X, + this->render_mapPreviewTexture_Y, + this->render_mapPreviewTexture_W, + this->render_mapPreviewTexture_H, + mapPreviewTexture, 1.0f); + if (this->zoomedMap == true) { + return; + } + //printf("=================> Rendering map preview texture\n"); + } + + if (scenarioLogoTexture != NULL) { + renderer.renderTextureQuad(300, 350, 400, 300, scenarioLogoTexture, + 1.0f); + //renderer.renderBackground(scenarioLogoTexture); + } + + renderer.renderButton(&buttonDisconnect); + + // Get a reference to the player texture cache + std::map < int, + Texture2D * >& + crcPlayerTextureCache = + CacheManager::getCachedItem < std::map < int, + Texture2D * > >(GameConstants::playerTextureCacheLookupKey); + + // START - this code ensure player title and player names don't overlap + int + offsetPosition = 0; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + const + Metrics & + metrics = Metrics::getInstance(); + FontMetrics * + fontMetrics = NULL; + if (Renderer::renderText3DEnabled == false) { + fontMetrics = + CoreData::getInstance().getMenuFontNormal()->getMetrics(); + } else { + fontMetrics = + CoreData::getInstance().getMenuFontNormal3D()->getMetrics(); + } + + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + int + curWidth = + (metrics.toVirtualX + (fontMetrics->getTextWidth(labelPlayers[i].getText()))); + + if (labelPlayers[i].getX() + curWidth >= + labelPlayerNames[i].getX()) { + int + newOffsetPosition = labelPlayers[i].getX() + curWidth + 2; + if (offsetPosition < newOffsetPosition) { + offsetPosition = newOffsetPosition; + } + } + } + // END + + renderer.renderListBox(&listBoxPlayerStatus); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + bool renderIt = true; + //printf("Player #%d [%s] control = %d\n",i,labelPlayerNames[i].getText().c_str(),listBoxControls[i].getSelectedItemIndex()); + if (labelNetStatus[i].getText() == + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + renderIt = false; + } + labelPlayers[i].setVisible(renderIt); + labelPlayerNames[i].setVisible(renderIt); + listBoxControls[i].setVisible(renderIt); + listBoxRMultiplier[i].setVisible(renderIt); + listBoxFactions[i].setVisible(renderIt); + listBoxTeams[i].setVisible(renderIt); + labelNetStatus[i].setVisible(renderIt); + } + + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + renderer.renderLabel(&labelPlayerStatus[i]); + } + + if (crcPlayerTextureCache[i] != NULL) { + // Render the player # label the player's color + + Vec3f + playerColor = + crcPlayerTextureCache[i]->getPixmap()->getPixel3f(0, 0); + if (clientInterface != NULL + && clientInterface->getGameSettings() != NULL + && clientInterface-> + getGameSettings()->getMasterserver_admin() > 0 + && clientInterface-> + getGameSettings()->getMasterserver_admin_faction_index() == + i) { + + if (difftime((long int) time(NULL), timerLabelFlash) < 1) { + renderer.renderLabel(&labelPlayers[i], &playerColor); + } else { + Vec4f + flashColor = + Vec4f(playerColor.x, playerColor.y, playerColor.z, 0.45f); + renderer.renderLabel(&labelPlayers[i], &flashColor); + } + + } else { + renderer.renderLabel(&labelPlayers[i], &playerColor); + } + + // Blend the color with white so make it more readable + //Vec4f newColor(1.f, 1.f, 1.f, 0.57f); + //renderer.renderLabel(&labelPlayers[i],&newColor); + + //int quadWidth = labelPlayerNames[i].getX() - labelPlayers[i].getX() - 5; + //renderer.renderTextureQuad(labelPlayers[i].getX(), labelPlayers[i].getY(), quadWidth, labelPlayers[i].getH(), crcPlayerTextureCache[i],1.0f,&playerColor); + } else { + renderer.renderLabel(&labelPlayers[i]); + } + + if (offsetPosition > 0) { + labelPlayerNames[i].setX(offsetPosition); + } + + renderer.renderListBox(&listBoxControls[i]); + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + renderer.renderListBox(&listBoxRMultiplier[i]); + renderer.renderListBox(&listBoxFactions[i]); + int + teamnumber = listBoxTeams[i].getSelectedItemIndex(); + Vec3f teamcolor = Vec3f(1.0f, 1.0f, 1.0f); + if (teamnumber >= 0 && teamnumber < 8) { + teamcolor = + crcPlayerTextureCache[teamnumber]-> + getPixmap()->getPixel3f(0, 0); + } + listBoxTeams[i].setTextColor(teamcolor); + renderer.renderListBox(&listBoxTeams[i]); + + bool canGrabSlot = false; + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL + && clientInterface->getJoinGameInProgress() == true) { + canGrabSlot = + ((listBoxControls[i].getSelectedItemIndex() == ctNetwork + && labelNetStatus[i].getText() == + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) + || (listBoxControls[i].getSelectedItemIndex() != ctHuman + && listBoxControls[i].getSelectedItemIndex() != ctClosed + && listBoxControls[i].getSelectedItemIndex() != + ctNetwork)); + } else { + canGrabSlot = + (listBoxControls[i].getSelectedItemIndex() == ctNetwork + && labelNetStatus[i].getText() == + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + + if (canGrabSlot == true) { + if (i < mapInfo.players) { + renderer.renderButton(&grabSlotButton[i]); + } + } else if (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned + || listBoxControls[i].getSelectedItemIndex() == ctHuman) { + renderer.renderLabel(&labelNetStatus[i]); + } + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork || + listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned + || listBoxControls[i].getSelectedItemIndex() == ctHuman) { + if (labelNetStatus[i].getText() != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + renderer.renderLabel(&labelPlayerNames[i]); + } + } + } + } + renderer.renderLabel(&labelStatus); + renderer.renderLabel(&labelInfo); + + if (difftime((long int) time(NULL), timerLabelFlash) < 1) { + renderer.renderLabel(&labelDataSynchInfo, &RED); + renderer.renderLabel(&labelWaitingForPlayers, &YELLOW); + } else { + renderer.renderLabel(&labelDataSynchInfo, &WHITE); + renderer.renderLabel(&labelWaitingForPlayers, &WHITE); + } + + renderer.renderLabel(&labelMap); + renderer.renderLabel(&labelMapFilter); + renderer.renderLabel(&labelFogOfWar); + renderer.renderLabel(&labelAllowObservers); + renderer.renderLabel(&labelFallbackCpuMultiplier); + renderer.renderLabel(&labelTileset); + renderer.renderLabel(&labelTechTree); + renderer.renderLabel(&labelControl); + renderer.renderLabel(&labelFaction); + renderer.renderLabel(&labelTeam); + renderer.renderLabel(&labelMapInfo); + + renderer.renderListBox(&listBoxMap); + renderer.renderListBox(&listBoxMapFilter); + renderer.renderListBox(&listBoxFogOfWar); + renderer.renderCheckBox(&checkBoxAllowObservers); + renderer.renderListBox(&listBoxTileset); + renderer.renderListBox(&listBoxTechTree); + + renderer.renderLabel(&labelEnableSwitchTeamMode); + renderer.renderLabel(&labelAISwitchTeamAcceptPercent); + + renderer.renderCheckBox(&checkBoxEnableSwitchTeamMode); + renderer.renderListBox(&listBoxAISwitchTeamAcceptPercent); + renderer.renderListBox(&listBoxFallbackCpuMultiplier); + + renderer.renderLabel(&labelAllowTeamUnitSharing); + renderer.renderCheckBox(&checkBoxAllowTeamUnitSharing); + + renderer.renderLabel(&labelAllowTeamResourceSharing); + renderer.renderCheckBox(&checkBoxAllowTeamResourceSharing); + + renderer.renderButton(&buttonPlayNow); + renderer.renderButton(&buttonRestoreLastSettings); + + renderer.renderCheckBox(&checkBoxScenario); + renderer.renderLabel(&labelScenario); + if (checkBoxScenario.getValue() == true) { + renderer.renderListBox(&listBoxScenario); + } + + renderer.renderLabel(&labelAllowNativeLanguageTechtree); + renderer.renderCheckBox(&checkBoxAllowNativeLanguageTechtree); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + + // !!! START TEMP MV + //renderer.renderButton(&buttonCancelDownloads); + //fileFTPProgressList.clear(); + //fileFTPProgressList["test1a dsa asd asda sdasd asd ad ad"] = make_pair(1,"testa"); + //fileFTPProgressList["test2 asdasdasdadas dasdasdasda"] = make_pair(1,"testb"); + //fileFTPProgressList["test3 asdasdad asd ada dasdadasdada"] = make_pair(1,"testc"); + // !!! END TEMP MV + + if (fileFTPProgressList.empty() == false) { + Lang & lang = Lang::getInstance(); + renderer.renderButton(&buttonCancelDownloads); + int + xLocation = buttonCancelDownloads.getX(); + int + yLocation = buttonCancelDownloads.getY() - 20; + for (std::map < string, pair < int, string > >::iterator iterMap = + fileFTPProgressList.begin(); + iterMap != fileFTPProgressList.end(); ++iterMap) { + string + progressLabelPrefix = + lang.getString("ModDownloading") + " " + iterMap->first + " "; + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nRendering file progress with the following prefix [%s]\n",progressLabelPrefix.c_str()); + + if (Renderer::renderText3DEnabled) { + renderer.renderProgressBar3D(iterMap->second.first, xLocation, + //10, + yLocation, + CoreData::getInstance + ().getDisplayFontSmall3D(), + //350,progressLabelPrefix); + 300, progressLabelPrefix); + } else { + renderer.renderProgressBar(iterMap->second.first, + //10, + xLocation, + yLocation, + CoreData:: + getInstance().getDisplayFontSmall + (), + //350,progressLabelPrefix); + 300, progressLabelPrefix); + } + + yLocation -= 20; + } + } + safeMutexFTPProgress.ReleaseLock(); + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } + if (ftpMessageBox.getEnabled()) { + renderer.renderMessageBox(&ftpMessageBox); + } + + if (program != NULL) + program->renderProgramMsgBox(); + + if (enableMapPreview && (mapPreview.hasFileLoaded() == true)) { + + int + mouseX = mainMenu->getMouseX(); + int + mouseY = mainMenu->getMouseY(); + int + mouse2dAnim = mainMenu->getMouse2dAnim(); + + if (mapPreviewTexture == NULL) { + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + + bool renderAll = (listBoxFogOfWar.getSelectedItemIndex() == 2); + //renderer.renderMapPreview(&mapPreview, renderAll, 10, 350, &mapPreviewTexture); + renderer.renderMapPreview(&mapPreview, renderAll, + this->render_mapPreviewTexture_X, + this->render_mapPreviewTexture_Y, + &mapPreviewTexture); + } + } + renderer.renderChatManager(&chatManager); + renderer.renderConsole(&console, + showFullConsole ? consoleFull : + consoleStoredAndNormal); + + if (difftime((long int) time(NULL), timerLabelFlash) > 2) { + timerLabelFlash = time(NULL); + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + void + MenuStateConnectedGame::update() { + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + Lang & lang = Lang::getInstance(); + ClientInterface * + clientInterface = + NetworkManager::getInstance().getClientInterface(); + + string newLabelConnectionInfo = lang.getString("WaitingHost"); + if (clientInterface != NULL + && clientInterface->getJoinGameInProgress() == true) { + newLabelConnectionInfo = lang.getString("MGGameStatus2"); + } + // Test progress bar + //MutexSafeWrapper safeMutexFTPProgress((ftpClientThread != NULL ? ftpClientThread->getProgressMutex() : NULL),string(__FILE__) + "_" + intToStr(__LINE__)); + //fileFTPProgressList["test"] = pair(difftime(time(NULL),lastNetworkSendPing) * 20,"test file 123"); + //safeMutexFTPProgress.ReleaseLock(); + // + + if (clientInterface != NULL && clientInterface->isConnected()) { + //printf("#2 admin key [%d] client key [%d]\n",settings->getMasterserver_admin(),clientInterface->getSessionKey()); + broadCastGameSettingsToHeadlessServer(false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + bool + notCurrentlySwitching = + ((difftime + ((long int) time(NULL), + broadcastServerSettingsDelayTimer)) >= + HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS); + bool + receiveAllowedNow = + difftime((long int) time(NULL), noReceiveTimer) > 2; + bool + newMessage = + lastGameSettingsReceivedCount < + clientInterface->getGameSettingsReceivedCount(); + if (validDisplayedGamesettings == false + || (notCurrentlySwitching && newMessage && receiveAllowedNow)) { + + //printf("I take the whole settings top broadcastDelay=%d noReceiveTimer=%d\n", (int)difftime((long int) time(NULL), broadcastServerSettingsDelayTimer),(int)difftime((long int) time(NULL), noReceiveTimer)); + + displayedGamesettings = *(clientInterface->getGameSettings()); + originalGamesettings = displayedGamesettings; + validDisplayedGamesettings = true; + } + + checkBoxAllowNativeLanguageTechtree.setEditable(isHeadlessAdmin()); + checkBoxAllowNativeLanguageTechtree.setEnabled(isHeadlessAdmin()); + + listBoxMap.setEditable(isHeadlessAdmin()); + listBoxMapFilter.setEditable(isHeadlessAdmin()); + buttonPlayNow.setVisible(isHeadlessAdmin() || + clientInterface->getJoinGameInProgress() == + true); + buttonRestoreLastSettings.setVisible(isHeadlessAdmin()); + listBoxTechTree.setEditable(isHeadlessAdmin()); + listBoxTileset.setEditable(isHeadlessAdmin()); + checkBoxEnableSwitchTeamMode.setEditable(isHeadlessAdmin()); + listBoxAISwitchTeamAcceptPercent.setEditable(isHeadlessAdmin()); + listBoxFallbackCpuMultiplier.setEditable(isHeadlessAdmin()); + listBoxFogOfWar.setEditable(isHeadlessAdmin()); + checkBoxAllowObservers.setEditable(isHeadlessAdmin()); + + checkBoxAllowTeamUnitSharing.setEditable(isHeadlessAdmin()); + checkBoxAllowTeamResourceSharing.setEditable(isHeadlessAdmin()); + + if (isHeadlessAdmin() == true) { + bool hasOtherPlayer = false; + bool hasOpenSlot = false; + for (int i = 0; + i < GameConstants::maxPlayers; ++i) { + if (displayedGamesettings.getFactionControl(i) == ctNetwork + && clientInterface->getPlayerIndex() != (int) i) { + hasOpenSlot = true; + } + if (displayedGamesettings.getFactionControl(i) == ctNetwork && + displayedGamesettings.getNetworkPlayerNameByPlayerIndex(i) != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME + && displayedGamesettings.getNetworkPlayerNameByPlayerIndex(i) + != GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME) { + //listBoxControls[i].setEditable(false); + if (clientInterface->getPlayerIndex() != (int) i) { + hasOtherPlayer = true; + } + } else if (clientInterface->getPlayerIndex() == (int) i || + i >= mapInfo.hardMaxPlayers) { + // Most of the code that handles how to show the set up + // when observers are allowed is in the update() function + // in menu_state_custom_game.cpp. Explicitly stating to change + // players to observers, changing teams to the special "observer" + // team is done in that function. So basically, the update() function + // in this file receives the info from update() in + // menu_state_custom_game.cpp + // + // Mostly what needs to be done here is disable observer slots + // so the info can't be changed by the headless admin. We don't want + // him or her to accidently enable a non-observer into an observer slot + // that's > mapInfo.hardMaxPlayers, or to change the team. + // + listBoxControls[i].setEditable(false); + } else { + if (i < mapInfo.hardMaxPlayers) { + listBoxControls[i].setEditable(true); + } + } + if (i < mapInfo.hardMaxPlayers) { + listBoxRMultiplier[i].setEditable(isHeadlessAdmin()); + listBoxFactions[i].setEditable(isHeadlessAdmin()); + listBoxTeams[i].setEditable(isHeadlessAdmin()); + } + } + if (hasOtherPlayer) { + labelWaitingForPlayers.setText(""); + labelWaitingForPlayers.setVisible(false); + } else if (hasOpenSlot) { + labelWaitingForPlayers. + setText(lang.getString("WaitingForPlayers")); + labelWaitingForPlayers.setVisible(true); + } else { + labelWaitingForPlayers. + setText(lang.getString("OpenANetworkSLot")); + labelWaitingForPlayers.setVisible(true); + } + } else { + labelWaitingForPlayers.setText(""); + labelWaitingForPlayers.setVisible(false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (difftime((long int) time(NULL), lastNetworkSendPing) >= + GameConstants::networkPingInterval) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] about to sendPingMessage...\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + lastNetworkSendPing = time(NULL); + clientInterface->sendPingMessage(GameConstants:: + networkPingInterval, + (int64) time(NULL)); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] pingCount = %d, clientInterface->getLastPingLag() = %f, GameConstants::networkPingInterval = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, pingCount, + clientInterface->getLastPingLag(), + GameConstants::networkPingInterval); + + // Starting checking timeout after sending at least 3 pings to server + if (clientInterface->isConnected() && + pingCount >= MAX_PING_LAG_COUNT + && clientInterface->getLastPingLag() >= + (GameConstants::networkPingInterval * MAX_PING_LAG_COUNT)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + clientInterface->updateLobby(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->isConnected() + && clientInterface->getJoinGameInProgress() == false + && pingCount >= MAX_PING_LAG_COUNT + && clientInterface->getLastPingLag() >= + (GameConstants::networkPingInterval * MAX_PING_LAG_COUNT)) { + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (fileFTPProgressList.empty() == true) { + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + displayedGamesettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + clientInterface->sendTextMessage(lang.getString + ("ConnectionTimedOut", + languageList[i]) + + " : " + + doubleToStr + (clientInterface->getLastPingLag + (), 2), -1, false, + languageList[i]); + sleep(1); + clientInterface->close(); + } + } + } + } + + pingCount++; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + buttonDisconnect.setText(lang.getString("Disconnect")); + + if (clientInterface->getAllowDownloadDataSynch() == false) { + string label = lang.getString("ConnectedToServer"); + + if (clientInterface->getServerName().empty() == false) { + label = label + " " + clientInterface->getServerName(); + } + + label = label + ", " + clientInterface->getVersionString(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getAllowGameDataSynchCheck() == false && + displayedGamesettings.getTileset() != "" && + displayedGamesettings.getTech() != "" && + displayedGamesettings.getMap() != "") { + Config & config = Config::getInstance(); + + MutexSafeWrapper + safeMutexFTPProgress(ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex() : + NULL, + string(__FILE__) + "_" + + intToStr(__LINE__)); + + uint32 tilesetCRC = lastCheckedCRCTilesetValue; + if (lastCheckedCRCTilesetName != + displayedGamesettings.getTileset() + && displayedGamesettings.getTileset() != "") { + //console.addLine("Checking tileset CRC [" + displayedGamesettings.getTileset() + "]"); + tilesetCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + displayedGamesettings.getTileset() + + string("/*"), ".xml", NULL); + if (tilesetCRC == 0 + || tilesetCRC != displayedGamesettings.getTilesetCRC()) { + tilesetCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + displayedGamesettings.getTileset() + + string("/*"), ".xml", NULL, true); + } + + // Test data synch + //tilesetCRC++; + lastCheckedCRCTilesetValue = tilesetCRC; + lastCheckedCRCTilesetName = displayedGamesettings.getTileset(); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + uint32 techCRC = lastCheckedCRCTechtreeValue; + if (lastCheckedCRCTechtreeName != displayedGamesettings.getTech() + && displayedGamesettings.getTech() != "") { + //console.addLine("Checking techtree CRC [" + displayedGamesettings.getTech() + "]"); + techCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + string("/") + displayedGamesettings.getTech() + + string("/*"), ".xml", NULL); + //clientInterface->sendTextMessage("#1 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); + + if (techCRC == 0 + || techCRC != displayedGamesettings.getTechCRC()) { + techCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + string("/") + displayedGamesettings.getTech() + + string("/*"), ".xml", NULL, true); + //clientInterface->sendTextMessage("#2 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); + } + + + if (techCRC != 0 + && techCRC != displayedGamesettings.getTechCRC() + && listBoxTechTree.getSelectedItemIndex() >= 0 + && listBoxTechTree.getSelectedItem() != + Lang::getInstance().getString("DataMissing", "", true)) { + + //time_t now = time(NULL); + time_t + lastUpdateDate = + getFolderTreeContentsCheckSumRecursivelyLastGenerated + (config.getPathListForType(ptTechs, ""), + string("/") + displayedGamesettings.getTech() + + string("/*"), ".xml"); + + const + time_t + REFRESH_CRC_DAY_SECONDS = 60 * 60 * 1; + if (lastUpdateDate <= 0 || + difftime((long int) time(NULL), + lastUpdateDate) >= REFRESH_CRC_DAY_SECONDS) { + techCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + string("/") + displayedGamesettings.getTech() + + string("/*"), ".xml", NULL, true); + //clientInterface->sendTextMessage("#3 TechCRC = " + intToStr(techCRC) + " remoteCRC = " + intToStr(displayedGamesettings.getTechCRC()),-1, true, ""); + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // Test data synch + //techCRC++; + lastCheckedCRCTechtreeValue = techCRC; + lastCheckedCRCTechtreeName = displayedGamesettings.getTech(); + + loadFactions(&displayedGamesettings, false); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + factionCRCList.clear(); + for (unsigned int factionIdx = 0; + factionIdx < factionFiles.size(); ++factionIdx) { + string factionName = factionFiles[factionIdx]; + if (factionName != GameConstants::RANDOMFACTION_SLOTNAME && + factionName != GameConstants::OBSERVER_SLOTNAME && + factionName != + Lang::getInstance().getString("DataMissing", "", true)) { + + uint32 factionCRC = 0; + //time_t now = time(NULL); + time_t + lastUpdateDate = + getFolderTreeContentsCheckSumRecursivelyLastGenerated + (config.getPathListForType(ptTechs, ""), + "/" + displayedGamesettings.getTech() + "/factions/" + + factionName + "/*", ".xml"); + + const + time_t + REFRESH_CRC_DAY_SECONDS = 60 * 60 * 24; + if (lastUpdateDate <= 0 || + difftime((long int) time(NULL), + lastUpdateDate) >= REFRESH_CRC_DAY_SECONDS + || (techCRC != 0 + && techCRC != displayedGamesettings.getTechCRC())) { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + displayedGamesettings.getTech() + "/factions/" + + factionName + "/*", ".xml", NULL, true); + } else { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + displayedGamesettings.getTech() + "/factions/" + + factionName + "/*", ".xml", NULL); + } + if (factionCRC == 0) { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + displayedGamesettings.getTech() + "/factions/" + + factionName + "/*", ".xml", NULL, true); + } + + if (factionCRC != 0) { + vector < pair < string, uint32 > >serverFactionCRCList = + displayedGamesettings.getFactionCRCList(); + for (unsigned int factionIdx1 = 0; + factionIdx1 < serverFactionCRCList.size(); + ++factionIdx1) { + pair < string, uint32 > &serverFaction = + serverFactionCRCList[factionIdx1]; + if (serverFaction.first == factionName) { + if (serverFaction.second != factionCRC) { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + displayedGamesettings.getTech() + + "/factions/" + factionName + "/*", ".xml", NULL, + true); + } + break; + } + } + } + factionCRCList.push_back(make_pair + (factionName, factionCRC)); + } + } + //console.addLine("Found factions: " + intToStr(factionCRCList.size())); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + uint32 mapCRC = lastCheckedCRCMapValue; + if (lastCheckedCRCMapName != displayedGamesettings.getMap() && + displayedGamesettings.getMap() != "") { + Checksum checksum; + string + file = + Config::getMapPath(displayedGamesettings.getMap(), "", + false); + //console.addLine("Checking map CRC [" + file + "]"); + checksum.addFile(file); + mapCRC = checksum.getSum(); + // Test data synch + //mapCRC++; + + lastCheckedCRCMapValue = mapCRC; + lastCheckedCRCMapName = displayedGamesettings.getMap(); + } + safeMutexFTPProgress.ReleaseLock(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + bool + dataSynchMismatch = + ((mapCRC != 0 && mapCRC != displayedGamesettings.getMapCRC()) + || (tilesetCRC != 0 + && tilesetCRC != displayedGamesettings.getTilesetCRC()) + || (techCRC != 0 + && techCRC != displayedGamesettings.getTechCRC())); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nmapCRC [%d] displayedGamesettings.getMapCRC() [%d]\ntilesetCRC [%d] displayedGamesettings.getTilesetCRC() [%d]\ntechCRC [%d] displayedGamesettings.getTechCRC() [%d]\n",mapCRC,displayedGamesettings.getMapCRC(),tilesetCRC,displayedGamesettings.getTilesetCRC(),techCRC,displayedGamesettings.getTechCRC()); + + if (dataSynchMismatch == true && + (difftime + ((long int) time(NULL), + broadcastServerSettingsDelayTimer) >= + HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS)) { + //printf("Data not synched: lmap %u rmap: %u ltile: %d rtile: %u ltech: %u rtech: %u\n",mapCRC,displayedGamesettings.getMapCRC(),tilesetCRC,displayedGamesettings.getTilesetCRC(),techCRC,displayedGamesettings.getTechCRC()); + + string labelSynch = lang.getString("DataNotSynchedTitle"); + + if (mapCRC != 0 && mapCRC != displayedGamesettings.getMapCRC() + && listBoxMap.getSelectedItemIndex() >= 0 + && listBoxMap.getSelectedItem() != + Lang::getInstance().getString("DataMissing", "", true)) { + labelSynch = labelSynch + " " + lang.getString("Map"); + + if (updateDataSynchDetailText == true && + lastMapDataSynchError != + lang.getString("DataNotSynchedMap") + " " + + listBoxMap.getSelectedItem()) { + lastMapDataSynchError = + lang.getString("DataNotSynchedMap") + " " + + listBoxMap.getSelectedItem(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + string + msg = + lang.getString("DataNotSynchedMap", + languageList[i]) + " " + + listBoxMap.getSelectedItem(); + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(msg, -1, localEcho, + languageList[i]); + } + } + } + + if (tilesetCRC != 0 + && tilesetCRC != displayedGamesettings.getTilesetCRC() + && listBoxTileset.getSelectedItemIndex() >= 0 + && listBoxTileset.getSelectedItem() != + Lang::getInstance().getString("DataMissing", "", true)) { + labelSynch = labelSynch + " " + lang.getString("Tileset"); + if (updateDataSynchDetailText == true && + lastTileDataSynchError != + lang.getString("DataNotSynchedTileset") + " " + + listBoxTileset.getSelectedItem()) { + lastTileDataSynchError = + lang.getString("DataNotSynchedTileset") + " " + + listBoxTileset.getSelectedItem(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + string + msg = + lang.getString("DataNotSynchedTileset", + languageList[i]) + " " + + listBoxTileset.getSelectedItem(); + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(msg, -1, localEcho, + languageList[i]); + } + } + } + + if (techCRC != 0 + && techCRC != displayedGamesettings.getTechCRC() + && listBoxTechTree.getSelectedItemIndex() >= 0 + && listBoxTechTree.getSelectedItem() != + Lang::getInstance().getString("DataMissing", "", true)) { + labelSynch = labelSynch + " " + lang.getString("TechTree"); + if (updateDataSynchDetailText == true && + lastTechtreeDataSynchError != + lang.getString("DataNotSynchedTechtree") + " " + + listBoxTechTree.getSelectedItem()) { + lastTechtreeDataSynchError = + lang.getString("DataNotSynchedTechtree") + " " + + listBoxTechTree.getSelectedItem(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + string + msg = + lang.getString("DataNotSynchedTechtree", + languageList[i]) + " " + + listBoxTechTree.getSelectedItem(); + bool localEcho = lang.isLanguageLocal(languageList[i]); + clientInterface->sendTextMessage(msg, -1, localEcho, + languageList[i]); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + const int + MAX_CHAT_TEXT_LINE_LENGTH = 110; + //const vector languageList = clientInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + bool localEcho = lang.isLanguageLocal(languageList[i]); + + string mismatchedFactionText = ""; + vector < string > mismatchedFactionTextList; + vector < pair < string, uint32 > >serverFactionCRCList = + displayedGamesettings.getFactionCRCList(); + + for (unsigned int factionIdx = 0; + factionIdx < serverFactionCRCList.size(); + ++factionIdx) { + pair < string, uint32 > &serverFaction = + serverFactionCRCList[factionIdx]; + + bool foundFaction = false; + for (unsigned int clientFactionIdx = 0; + clientFactionIdx < factionCRCList.size(); + ++clientFactionIdx) { + pair < string, uint32 > &clientFaction = + factionCRCList[clientFactionIdx]; + + if (serverFaction.first == clientFaction.first) { + foundFaction = true; + if (serverFaction.second != clientFaction.second) { + if (mismatchedFactionText.length() >= 10) { + mismatchedFactionTextList.push_back + (mismatchedFactionText); + mismatchedFactionText = ""; + } + if (mismatchedFactionText == "") { + mismatchedFactionText = + "The following factions are mismatched: "; + if (lang.hasString("MismatchedFactions", + languageList[i]) == true) { + mismatchedFactionText = + lang.getString("MismatchedFactions", + languageList[i]); + } + + mismatchedFactionText += + " [" + intToStr(factionCRCList.size()) + + "][" + + intToStr(serverFactionCRCList.size()) + + "] - "; + } else { + mismatchedFactionText += ", "; + } + mismatchedFactionText += serverFaction.first; + } + break; + } + } + + if (foundFaction == false) { + if ((int) mismatchedFactionText.length() > + MAX_CHAT_TEXT_LINE_LENGTH) { + mismatchedFactionTextList.push_back + (mismatchedFactionText); + mismatchedFactionText = ""; + } + + if (mismatchedFactionText == "") { + mismatchedFactionText = + "The following factions are mismatched: "; + if (lang.hasString("MismatchedFactions", + languageList[i]) == true) { + mismatchedFactionText = + lang.getString("MismatchedFactions", + languageList[i]); + } + + mismatchedFactionText += + " [" + intToStr(factionCRCList.size()) + "][" + + intToStr(serverFactionCRCList.size()) + "] - "; + } else { + mismatchedFactionText += ", "; + } + + if (lang.hasString("MismatchedFactionsMissing", + languageList[i]) == true) { + mismatchedFactionText += + serverFaction.first + " " + + lang.getString("MismatchedFactionsMissing", + languageList[i]); + } else { + mismatchedFactionText += + serverFaction.first + " (missing)"; + } + } + } + + for (unsigned int clientFactionIdx = 0; + clientFactionIdx < factionCRCList.size(); + ++clientFactionIdx) { + pair < string, uint32 > &clientFaction = + factionCRCList[clientFactionIdx]; + + bool foundFaction = false; + for (unsigned int factionIdx = 0; + factionIdx < serverFactionCRCList.size(); + ++factionIdx) { + pair < string, uint32 > &serverFaction = + serverFactionCRCList[factionIdx]; + + if (serverFaction.first == clientFaction.first) { + foundFaction = true; + break; + } + } + + if (foundFaction == false) { + if ((int) mismatchedFactionText.length() > + MAX_CHAT_TEXT_LINE_LENGTH) { + mismatchedFactionTextList.push_back + (mismatchedFactionText); + mismatchedFactionText = ""; + } + + if (mismatchedFactionText == "") { + mismatchedFactionText = + "The following factions are mismatched: "; + if (lang.hasString("MismatchedFactions", + languageList[i]) == true) { + mismatchedFactionText = + lang.getString("MismatchedFactions", + languageList[i]); + } + + mismatchedFactionText += + " [" + intToStr(factionCRCList.size()) + "][" + + intToStr(serverFactionCRCList.size()) + "] - "; + } else { + mismatchedFactionText += ", "; + } + + if (lang.hasString("MismatchedFactionsExtra", + languageList[i]) == true) { + mismatchedFactionText += + clientFaction.first + " " + + lang.getString("MismatchedFactionsExtra", + languageList[i]); + } else { + mismatchedFactionText += + clientFaction.first + " (extra)"; + } + } + } + + if (mismatchedFactionText != "") { + if (mismatchedFactionTextList.empty() == false) { + if (mismatchedFactionText != "") { + mismatchedFactionText += "."; + mismatchedFactionTextList.push_back + (mismatchedFactionText); + } + for (unsigned int splitIdx = 0; + splitIdx < mismatchedFactionTextList.size(); + ++splitIdx) { + clientInterface->sendTextMessage + (mismatchedFactionTextList[splitIdx], -1, + localEcho, languageList[i]); + } + } else { + mismatchedFactionText += "."; + clientInterface->sendTextMessage + (mismatchedFactionText, -1, localEcho, + languageList[i]); + } + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + } + } + + updateDataSynchDetailText = false; + labelDataSynchInfo.setText(labelSynch); + } else { + labelDataSynchInfo.setText(""); + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getAllowGameDataSynchCheck() == true && + clientInterface->getNetworkGameDataSynchCheckOk() == false) { + label = label + " -synch mismatch for:"; + + if (clientInterface->getNetworkGameDataSynchCheckOkMap() == + false) { + label = label + " map"; + + if (updateDataSynchDetailText == true && + clientInterface->getReceivedDataSynchCheck() && + lastMapDataSynchError != + "map CRC mismatch, " + listBoxMap.getSelectedItem()) { + lastMapDataSynchError = + "map CRC mismatch, " + listBoxMap.getSelectedItem(); + clientInterface->sendTextMessage(lastMapDataSynchError, -1, + true, ""); + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getNetworkGameDataSynchCheckOkTile() == + false) { + label = label + " tile"; + if (updateDataSynchDetailText == true && + clientInterface->getReceivedDataSynchCheck() && + lastTileDataSynchError != + "tile CRC mismatch, " + listBoxTileset.getSelectedItem()) { + lastTileDataSynchError = + "tile CRC mismatch, " + listBoxTileset.getSelectedItem(); + clientInterface->sendTextMessage(lastTileDataSynchError, -1, + true, ""); + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getNetworkGameDataSynchCheckOkTech() == + false) { + label = label + " techtree"; + + if (updateDataSynchDetailText == true && + clientInterface->getReceivedDataSynchCheck()) { + + string + report = + clientInterface->getNetworkGameDataSynchCheckTechMismatchReport + (); + if (lastTechtreeDataSynchError != + "techtree CRC mismatch" + report) { + lastTechtreeDataSynchError = + "techtree CRC mismatch" + report; + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] report: %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + report.c_str()); + + clientInterface->sendTextMessage("techtree CRC mismatch", + -1, true, ""); + vector < string > reportLineTokens; + Tokenize(report, reportLineTokens, "\n"); + for (int reportLine = 0; + reportLine < (int) reportLineTokens.size(); + ++reportLine) { + clientInterface->sendTextMessage(reportLineTokens + [reportLine], -1, true, + ""); + } + } + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getReceivedDataSynchCheck() == true) { + updateDataSynchDetailText = false; + } + } else if (clientInterface->getAllowGameDataSynchCheck() == true) { + label += " - data synch is ok"; + } + + labelStatus.setText(label); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + } else { + string label = lang.getString("ConnectedToServer"); + + if (!clientInterface->getServerName().empty()) { + label = label + " " + clientInterface->getServerName(); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (clientInterface->getAllowGameDataSynchCheck() == true && + clientInterface->getNetworkGameDataSynchCheckOk() == false) { + label = label + " -waiting to synch:"; + if (clientInterface->getNetworkGameDataSynchCheckOkMap() == + false) { + label = label + " map"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTile() == + false) { + label = label + " tile"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTech() == + false) { + label = label + " techtree"; + } + } else if (clientInterface->getAllowGameDataSynchCheck() == true) { + label += " - data synch is ok"; + } + + labelStatus.setText(label); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (clientInterface != NULL + && clientInterface->isConnected() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + clientInterface->close(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + returnToJoinMenu(); + return; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //process network messages + if (clientInterface != NULL && clientInterface->isConnected()) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + try { + if (clientInterface->getGameSettingsReceived() + && validDisplayedGamesettings + && lastGameSettingsReceivedCount != + clientInterface->getGameSettingsReceivedCount()) { + lastGameSettingsReceivedCount = + clientInterface->getGameSettingsReceivedCount(); + bool + errorOnMissingData = + (clientInterface->getAllowGameDataSynchCheck() == false); + + const GameSettings * + receivedGameSettings = clientInterface->getGameSettings(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //printf("Menu got new settings thisfactionindex = %d startlocation: %d control = %d\n",displayedGamesettings.getThisFactionIndex(),clientInterface->getGameSettings()->getStartLocationIndex(clientInterface->getGameSettings()->getThisFactionIndex()),displayedGamesettings.getFactionControl(clientInterface->getGameSettings()->getThisFactionIndex())); + if (difftime((long int) time(NULL), noReceiveTimer) < 3 + || difftime((long int) time(NULL), + broadcastServerSettingsDelayTimer) < + HEADLESSSERVER_BROADCAST_SETTINGS_SECONDS) { + // copy my current settings in UI to displayedSettings; + loadGameSettings(&displayedGamesettings); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // check if there are any changed fields from others clients + if (isHeadlessAdmin()) { + //printf("I am headless admin and will restore only some parts\n"); + // only copy those parts which are editable by normal clients + for (int i = 0; i < receivedGameSettings->getFactionCount(); + i++) { + if (displayedGamesettings.getFactionControl(i) == + ctNetwork) { + if (originalGamesettings.getTeam(i) == + displayedGamesettings.getTeam(i)) { + displayedGamesettings.setTeam(i, + receivedGameSettings->getTeam + (i)); + originalGamesettings.setTeam(i, + receivedGameSettings->getTeam + (i)); + } + if (originalGamesettings.getFactionTypeName(i) == + displayedGamesettings.getFactionTypeName(i)) { + displayedGamesettings.setFactionTypeName(i, + receivedGameSettings->getFactionTypeName + (i)); + originalGamesettings.setFactionTypeName(i, + receivedGameSettings->getFactionTypeName + (i)); + } + displayedGamesettings.setNetworkPlayerGameStatus(i, + receivedGameSettings->getNetworkPlayerGameStatus + (i)); + originalGamesettings.setNetworkPlayerGameStatus(i, + receivedGameSettings->getNetworkPlayerGameStatus + (i)); + displayedGamesettings.setNetworkPlayerName(i, + receivedGameSettings->getNetworkPlayerName + (i)); + originalGamesettings.setNetworkPlayerName(i, + receivedGameSettings->getNetworkPlayerName + (i)); + } + } + } else { + //printf("I am client and restore everything but not my line\n"); + // copy all received fields just not those which are editable for normal client + //store my changes + int + i = clientInterface->getPlayerIndex(); + int + team = displayedGamesettings.getTeam(i); + string faction = displayedGamesettings.getFactionTypeName(i); + int + status = + displayedGamesettings.getNetworkPlayerGameStatus(i); + string + networkPlayerName = + displayedGamesettings.getNetworkPlayerName(i); + displayedGamesettings = *receivedGameSettings; + originalGamesettings = *receivedGameSettings; + + displayedGamesettings.setTeam(i, team); + originalGamesettings.setTeam(i, team); + displayedGamesettings.setFactionTypeName(i, faction); + originalGamesettings.setFactionTypeName(i, faction); + displayedGamesettings.setNetworkPlayerGameStatus(i, status); + originalGamesettings.setNetworkPlayerGameStatus(i, status); + displayedGamesettings.setNetworkPlayerName(i, + networkPlayerName); + originalGamesettings.setNetworkPlayerName(i, + networkPlayerName); + + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + setupUIFromGameSettings(&displayedGamesettings, + errorOnMissingData); + } else { + // do nothing + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + setupUIFromGameSettings(&displayedGamesettings, + errorOnMissingData); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + broadCastGameSettingsToHeadlessServer + (needToBroadcastServerSettings); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // check if we are joining an in progress game + if (clientInterface->getJoinGameInProgress() == true && + clientInterface->getJoinGameInProgressLaunch() == true && + clientInterface->getReadyForInGameJoin() == true && + ftpClientThread != NULL) { + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? + ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (readyToJoinInProgressGame == false) { + if (getInProgressSavedGameFromFTPServer == "") { + + getInProgressSavedGameFromFTPServerInProgress = true; + ftpClientThread->addTempFileToRequests + (GameConstants::saveNetworkGameFileClientCompressed, + GameConstants::saveNetworkGameFileServerCompressed); + + getInProgressSavedGameFromFTPServer = + GameConstants::saveNetworkGameFileServerCompressed; + fileFTPProgressList[getInProgressSavedGameFromFTPServer] = + pair < int, + string >(0, ""); + } + safeMutexFTPProgress.ReleaseLock(); + } else { + safeMutexFTPProgress.ReleaseLock(); + + string + saveGameFile = + "temp/" + string(GameConstants::saveNetworkGameFileClient); + if (getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) != "") { + saveGameFile = + getGameReadWritePath + (GameConstants::path_logs_CacheLookupKey) + saveGameFile; + } else { + string + userData = + Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + saveGameFile = userData + saveGameFile; + } + + //printf("Loading saved game file [%s]\n",saveGameFile.c_str()); + + GameSettings + gameSettings = *clientInterface->getGameSettings(); + loadGameSettings(&gameSettings); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + Game::loadGame(saveGameFile, program, false, &gameSettings); + return; + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //update lobby + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected()) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + clientInterface->updateLobby(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected()) { + if (initialSettingsReceivedFromServer == true && + clientInterface->getIntroDone() == true && + (switchSetupRequestFlagType & ssrft_NetworkPlayerName) == + ssrft_NetworkPlayerName) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] getHumanPlayerName() = [%s], clientInterface->getGameSettings()->getThisFactionIndex() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + getHumanPlayerName().c_str(), + clientInterface->getGameSettings + ()->getThisFactionIndex()); + clientInterface->sendSwitchSetupRequest("", + clientInterface->getPlayerIndex + (), -1, -1, + getHumanPlayerName(), + getNetworkPlayerStatus + (), + switchSetupRequestFlagType, + lang.getLanguage()); + + switchSetupRequestFlagType = ssrft_None; + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //intro + if (clientInterface->getIntroDone()) { + if (newLabelConnectionInfo != labelInfo.getText()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + labelInfo.setText(newLabelConnectionInfo); + } + } + + //launch + if (clientInterface->getLaunchGame()) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + assert(clientInterface != NULL); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (modHttpServerThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + modHttpServerThread->setSimpleTaskInterfaceValid(false); + modHttpServerThread->signalQuit(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (modHttpServerThread->canShutdown(true) == true + && modHttpServerThread->shutdownAndWait() == true) { + delete modHttpServerThread; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + modHttpServerThread = NULL; + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (ftpClientThread != NULL) { + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + } + ftpClientThread = NULL; + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + launchingNewGame = true; + + GameSettings + gameSettings = *clientInterface->getGameSettings(); + // complete game settings with local stuff + if (gameSettings.getScenario() != "") { + string scenario = gameSettings.getScenario(); + listBoxScenario.setSelectedItem(formatString(scenario)); + string file = Scenario::getScenarioPath(dirList, scenario); + + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, &scenarioInfo, isTutorial); + + gameSettings.setScenarioDir(Scenario::getScenarioPath + (dirList, scenarioInfo.name)); + + gameSettings. + setDefaultResources(scenarioInfo.defaultResources); + gameSettings.setDefaultUnits(scenarioInfo.defaultUnits); + gameSettings.setDefaultVictoryConditions + (scenarioInfo.defaultVictoryConditions); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + program->setState(new Game(program, &gameSettings, false)); + return; + } + } + + //call the chat manager + chatManager.updateNetwork(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //console732 + console.update(); + + // check for need to switch music on again + if (clientInterface != NULL) { + int + currentConnectionCount = 0; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (displayedGamesettings.getFactionControl(i) == ctNetwork && + displayedGamesettings.getNetworkPlayerName(i) != "" && + displayedGamesettings.getNetworkPlayerName(i) != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + currentConnectionCount++; + } + } + if (currentConnectionCount > soundConnectionCount) { + soundConnectionCount = currentConnectionCount; + static PlaySoundClip snd; + SoundRenderer::getInstance(). + playFx(snd.getSound(snd.sfxAttention)); + //switch on music again!! + Config & config = Config::getInstance(); + float + configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + CoreData::getInstance(). + getMenuMusic()->setVolume(configVolume); + } + soundConnectionCount = currentConnectionCount; + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + } catch (const runtime_error & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, ex.what()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, szBuf); + showMessageBox(szBuf, "Error", false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + } + } + + bool + MenuStateConnectedGame::loadFactions(const GameSettings * gameSettings, + bool errorOnNoFactions) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Lang & lang = Lang::getInstance(); + bool foundFactions = false; + vector < string > results; + + string + scenarioDir = + Scenario::getScenarioDir(dirList, gameSettings->getScenario()); + if (gameSettings->getTech() != "") { + Config & config = Config::getInstance(); + + vector < string > techPaths = + config.getPathListForType(ptTechs, scenarioDir); + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + //findAll(techPath + gameSettings->getTech() + "/factions/*.", results, false, false); + findDirs(techPath + gameSettings->getTech() + "/factions/", + results, false, false); + if (results.empty() == false) { + break; + } + } + + if (results.empty() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface->getAllowGameDataSynchCheck() == false) { + if (errorOnNoFactions == true) { + throw + megaglest_runtime_error + ("(2)There are no factions for the tech tree [" + + gameSettings->getTech() + "]"); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] (2)There are no factions for the tech tree [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + gameSettings->getTech().c_str()); + } + results. + push_back(Lang:: + getInstance().getString("DataMissing", "", true)); + factionFiles = results; + vector < string > translatedFactionNames; + for (int i = 0; i < (int) factionFiles.size(); ++i) { + results[i] = formatString(results[i]); + string + translatedString = + techTree->getTranslatedFactionName(gameSettings->getTech(), + factionFiles[i]); + if (toLower(translatedString) == toLower(results[i])) { + translatedFactionNames.push_back(results[i]); + } else { + translatedFactionNames.push_back(results[i] + " (" + + translatedString + ")"); + } + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxFactions[i].setItems(results, translatedFactionNames); + } + + if (lastMissingTechtree != gameSettings->getTech() && + gameSettings->getTech() != "") { + lastMissingTechtree = gameSettings->getTech(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtree", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTechtree", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + gameSettings->getTech().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is missing the techtree: %s", + getHumanPlayerName().c_str(), + gameSettings->getTech().c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + } + + foundFactions = false; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } else { + lastMissingTechtree = ""; + getMissingTechtreeFromFTPServer = ""; + + factionFiles = results; + vector < string > translatedFactionNames; + for (int i = 0; i < (int) factionFiles.size(); ++i) { + results[i] = formatString(results[i]); + string + translatedString = + techTree->getTranslatedFactionName(gameSettings->getTech(), + factionFiles[i]); + if (toLower(translatedString) == toLower(results[i])) { + translatedFactionNames.push_back(results[i]); + } else { + translatedFactionNames.push_back(results[i] + " (" + + translatedString + ")"); + } + } + + results.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Random", "", + true) + "*"); + + // Add special Observer Faction + if (checkBoxAllowObservers.getValue() == 1) { + results.push_back(formatString + (GameConstants::OBSERVER_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::OBSERVER_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Observer", "", + true) + "*"); + } + + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxFactions[i].setItems(results, translatedFactionNames); + } + + foundFactions = (results.empty() == false); + } + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + return foundFactions; + } + + // ============ PRIVATE =========================== + + bool MenuStateConnectedGame::hasNetworkGameSettings() { + bool hasNetworkSlot = false; + + try { + for (int i = 0; i < mapInfo.players; ++i) { + ControlType + ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct != ctClosed) { + if (ct == ctNetwork || ct == ctNetworkUnassigned) { + hasNetworkSlot = true; + break; + } + } + } + if (hasNetworkSlot == false) { + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType + ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct != ctClosed) { + if (ct == ctNetworkUnassigned) { + hasNetworkSlot = true; + break; + } + } + } + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, szBuf); + showMessageBox(szBuf, "Error", false); + } + + return hasNetworkSlot; + } + + void + MenuStateConnectedGame::keyDown(SDL_KeyboardEvent key) { + if (activeInputLabel != NULL) { + bool handled = keyDownEditLabel(key, &activeInputLabel); + if (handled == true) { + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else { + //send key to the chat manager + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL && + clientInterface->isConnected() == true && + clientInterface->getIntroDone() == true) { + chatManager.keyDown(key); + } + if (chatManager.getEditEnabled() == false && + (::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == + false)) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) == + true) { + showFullConsole = true; + } else if (isKeyPressed(configKeys.getSDLKey("ToggleMusic"), key) == + true) { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + float + configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + float + currentVolume = + CoreData::getInstance().getMenuMusic()->getVolume(); + if (currentVolume > 0) { + CoreData::getInstance().getMenuMusic()->setVolume(0.f); + console.addLine(lang.getString("GameMusic") + " " + + lang.getString("Off")); + } else { + CoreData::getInstance(). + getMenuMusic()->setVolume(configVolume); + //If the config says zero, use the default music volume + //gameMusic->setVolume(configVolume ? configVolume : 0.9); + console.addLine(lang.getString("GameMusic")); + } + } + //else if(key == configKeys.getCharKey("SaveGUILayout")) { + else if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) + == true) { + bool + saved = + GraphicComponent::saveAllCustomProperties(containerName); + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("GUILayoutSaved") + " [" + + (saved ? lang. + getString("Yes") : lang.getString("No")) + + "]"); + } + } + } + } + + bool MenuStateConnectedGame::textInput(std::string text) { + if (activeInputLabel != NULL) { + bool handled = textInputEditLabel(text, &activeInputLabel); + if (handled == true) { + switchSetupRequestFlagType |= ssrft_NetworkPlayerName; + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + return handled; + } else { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected() == true + && clientInterface->getIntroDone() == true) { + return chatManager.textInput(text); + } + } + return false; + } + + void + MenuStateConnectedGame::keyPress(SDL_KeyboardEvent c) { + if (activeInputLabel != NULL) { + keyPressEditLabel(c, &activeInputLabel); + } else { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected() == true + && clientInterface->getIntroDone() == true) { + chatManager.keyPress(c); + } + } + } + + void + MenuStateConnectedGame::keyUp(SDL_KeyboardEvent key) { + if (activeInputLabel == NULL) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + if (clientInterface != NULL && + clientInterface->isConnected() == true && + clientInterface->getIntroDone() == true) { + chatManager.keyUp(key); + } + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + if (chatManager.getEditEnabled()) { + //send key to the chat manager + chatManager.keyUp(key); + } else if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) + == true) { + showFullConsole = false; + } + } + } + + void + MenuStateConnectedGame::setActiveInputLabel(GraphicLabel * newLable) { + MenuState::setActiveInputLabel(newLable, &activeInputLabel); + } + + string MenuStateConnectedGame::getHumanPlayerName() { + string result = defaultPlayerName; + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + if (clientInterface != NULL && + j == clientInterface->getPlayerIndex() && + labelPlayerNames[j].getText() != "") { + result = labelPlayerNames[j].getText(); + + if (activeInputLabel != NULL) { + size_t found = result.find_last_of("_"); + if (found != string::npos) { + result = result.substr(0, found); + } + } + + break; + } + } + + return result; + } + + void + MenuStateConnectedGame::loadFactionTexture(string filepath) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (enableFactionTexturePreview == true) { + if (filepath == "") { + factionTexture = NULL; + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] filepath = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, filepath.c_str()); + factionTexture = Renderer::findTexture(filepath); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + } + } + + bool + MenuStateConnectedGame::loadMapInfo(string file, MapInfo * mapInfo, + bool loadMapPreview) { + bool mapLoaded = false; + try { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] map [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, file.c_str()); + + if (file != "") { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + lastMissingMap = file; + + Lang & lang = Lang::getInstance(); + if (MapPreview::loadMapInfo + (file, mapInfo, lang.getString("MaxPlayers"), + lang.getString("Size"), true) == true) { + mapInfo->players = GameConstants::maxPlayers; + + // Not painting properly so this is on hold + if (loadMapPreview == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + if (mapPreview.getMapFileLoaded() != file) { + mapPreview.loadFromFile(file.c_str()); + cleanupMapPreviewTexture(); + } + } + + mapLoaded = true; + } + } else { + cleanupMapPreviewTexture(); + mapInfo->desc = + Lang::getInstance().getString("DataMissing", "", true); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (lastMissingMap != gameSettings->getMap()) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + lastMissingMap = gameSettings->getMap(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingMap", languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingMap", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + gameSettings->getMap().c_str()); + } else { + snprintf(szMsg, 8096, "Player: %s is missing the map: %s", + getHumanPlayerName().c_str(), + gameSettings->getMap().c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + } + } + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + e.what()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, e.what()); + + showMessageBox("Error loading map file: " + file + '\n' + e.what(), + "Error", false); + } + + return mapLoaded; + } + + void + MenuStateConnectedGame::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void + MenuStateConnectedGame::showFTPMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + ftpMessageBox.setEnabled(false); + } + + if (!ftpMessageBox.getEnabled()) { + ftpMessageBox.setText(text); + ftpMessageBox.setHeader(header); + ftpMessageBox.setEnabled(true); + } else { + ftpMessageBox.setEnabled(false); + } + } + + int32 MenuStateConnectedGame::getNetworkPlayerStatus() { + int32 result = npst_None; + switch (listBoxPlayerStatus.getSelectedItemIndex()) { + case 2: + result = npst_Ready; + break; + case 1: + result = npst_BeRightBack; + break; + case 0: + result = npst_PickSettings; + break; + } + + return result; + } + + void + MenuStateConnectedGame::cleanupMapPreviewTexture() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (mapPreviewTexture != NULL) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + mapPreviewTexture->end(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + delete mapPreviewTexture; + mapPreviewTexture = NULL; + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + bool MenuStateConnectedGame::isInSpecialKeyCaptureEvent() { + bool + result = (chatManager.getEditEnabled() || activeInputLabel != NULL); + return result; + } + + void + MenuStateConnectedGame::FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType + type, + pair < + FTP_Client_ResultType, + string > result, + void *userdata) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (type == ftp_cct_DownloadProgress) { + FTPClientCallbackInterface::FtpProgressStats * stats = + (FTPClientCallbackInterface::FtpProgressStats *) userdata; + if (stats != NULL) { + int + fileProgress = 0; + if (stats->download_total > 0) { + fileProgress = + ((stats->download_now / stats->download_total) * 100.0); + } + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() + : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + pair < int, + string > + lastProgress; + std::map < string, pair < int, + string > >::iterator + iterFind = fileFTPProgressList.find(itemName); + if (iterFind == fileFTPProgressList.end()) { + iterFind = + fileFTPProgressList.find(GameConstants:: + saveNetworkGameFileServerCompressed); + if (iterFind == fileFTPProgressList.end()) { + iterFind = + fileFTPProgressList.find(GameConstants:: + saveNetworkGameFileClientCompressed); + } + } + if (iterFind != fileFTPProgressList.end()) { + lastProgress = iterFind->second; + fileFTPProgressList[iterFind->first] = pair < int, + string >(fileProgress, stats->currentFilename); + } + safeMutexFTPProgress.ReleaseLock(); + + if (itemName != "" + && (lastProgress.first / 25) < (fileProgress / 25)) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("FileDownloadProgress", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("FileDownloadProgress", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str(), + fileProgress); + } else { + snprintf(szMsg, 8096, + "Player: %s download progress for [%s] is %d %%", + getHumanPlayerName().c_str(), itemName.c_str(), + fileProgress); + } + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] szMsg [%s] lastProgress.first = %d, fileProgress = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, szMsg, lastProgress.first, + fileProgress); + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + } + } + } else if (type == ftp_cct_ExtractProgress) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP extract Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + if (userdata == NULL) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingExtractDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingExtractDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str()); + } else { + snprintf(szMsg, 8096, + "Please wait, player: %s is extracting: %s", + getHumanPlayerName().c_str(), itemName.c_str()); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] szMsg [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, szMsg); + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + } else { + char * + szBuf = (char *) userdata; + //printf("%s\n",szBuf); + //console.addLine(szBuf); + console.addLine(szBuf, false, ""); + } + } else if (type == ftp_cct_Map) { + getMissingMapFromFTPServerInProgress = false; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (result.first == ftp_crt_SUCCESS) { + // Clear the CRC file Cache + string file = Config::getMapPath(itemName, "", false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Got map itemName [%s] file [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", + itemName.c_str(), file.c_str(), + lastCheckedCRCMapName.c_str(), + gameSettings->getMap().c_str()); + + if (gameSettings != NULL && itemName == gameSettings->getMap() && + lastCheckedCRCMapName == gameSettings->getMap() && + gameSettings->getMap() != "") { + Checksum::clearFileCache(); + Checksum checksum; + + checksum.addFile(file); + lastCheckedCRCMapValue = checksum.getSum(); + } + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingMapSuccessDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingMapSuccessDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s SUCCESSFULLY downloaded the map: %s", + getHumanPlayerName().c_str(), itemName.c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + } else { + printf + ("FAILED map itemName [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", + itemName.c_str(), lastCheckedCRCMapName.c_str(), + gameSettings->getMap().c_str()); + + curl_version_info_data * + curlVersion = curl_version_info(CURLVERSION_NOW); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingMapFailDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingMapFailDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str(), + curlVersion->version); + } else { + snprintf(szMsg, 8096, + "Player: %s FAILED to download the map: [%s] using CURL version [%s]", + getHumanPlayerName().c_str(), itemName.c_str(), + curlVersion->version); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + + if (result.first == ftp_crt_HOST_NOT_ACCEPTING) { + if (lang.hasString("HostNotAcceptingDataConnections", + languageList[i]) == true) { + clientInterface->sendTextMessage(lang.getString + ("HostNotAcceptingDataConnections", + languageList[i]), -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } else { + clientInterface->sendTextMessage + ("*Warning* the host is not accepting data connections.", + -1, lang.isLanguageLocal(languageList[i]), + languageList[i]); + } + } + } + sleep(1); + + console.addLine(result.second, true); + } + } else if (type == ftp_cct_Tileset) { + getMissingTilesetFromFTPServerInProgress = false; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(true); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (result.first == ftp_crt_SUCCESS) { + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTilesetSuccessDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTilesetSuccessDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s SUCCESSFULLY downloaded the tileset: %s", + getHumanPlayerName().c_str(), itemName.c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + + // START + // Clear the CRC Cache if it is populated + // + // Clear the CRC file Cache + safeMutexFTPProgress.Lock(); + Checksum::clearFileCache(); + + vector < string > paths = + Config::getInstance().getPathListForType(ptTilesets); + string pathSearchString = string("/") + itemName + string("/*"); + const + string + filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + + // Refresh CRC + + //printf("Got map itemName [%s] file [%s] lastCheckedCRCMapName [%s] gameSettings->getMap() [%s]\n", + // itemName.c_str(),file.c_str(),lastCheckedCRCMapName.c_str(),gameSettings->getMap().c_str()); + + if (gameSettings != NULL && itemName == gameSettings->getTileset() + && lastCheckedCRCTilesetName == gameSettings->getTileset() + && gameSettings->getTileset() != "") { + Config & config = Config::getInstance(); + lastCheckedCRCTilesetValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + itemName + string("/*"), ".xml", NULL, true); + } + + safeMutexFTPProgress.ReleaseLock(); + // END + + // Reload tilesets for the UI + string + scenarioDir = + Scenario::getScenarioDir(dirList, gameSettings->getScenario()); + findDirs(Config::getInstance().getPathListForType(ptTilesets, + scenarioDir), + tilesetFiles); + + std::vector < string > tilesetsFormatted = tilesetFiles; + std::for_each(tilesetsFormatted.begin(), tilesetsFormatted.end(), + FormatString()); + listBoxTileset.setItems(tilesetsFormatted); + } else { + curl_version_info_data * + curlVersion = curl_version_info(CURLVERSION_NOW); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTilesetFailDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTilesetFailDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + (itemName + "(.7z)").c_str(), curlVersion->version); + } else { + snprintf(szMsg, 8096, + "Player: %s FAILED to download the tileset: [%s] using CURL version [%s]", + getHumanPlayerName().c_str(), + (itemName + "(.7z)").c_str(), curlVersion->version); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + + if (result.first == ftp_crt_HOST_NOT_ACCEPTING) { + if (lang.hasString("HostNotAcceptingDataConnections", + languageList[i]) == true) { + clientInterface->sendTextMessage(lang.getString + ("HostNotAcceptingDataConnections", + languageList[i]), -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } else { + clientInterface->sendTextMessage + ("*Warning* the host is not accepting data connections.", + -1, lang.isLanguageLocal(languageList[i]), + languageList[i]); + } + } + } + sleep(1); + + console.addLine(result.second, true); + } + } else if (type == ftp_cct_Techtree) { + getMissingTechtreeFromFTPServerInProgress = false; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(true); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (result.first == ftp_crt_SUCCESS) { + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtreeSuccessDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTechtreeSuccessDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s SUCCESSFULLY downloaded the techtree: %s", + getHumanPlayerName().c_str(), itemName.c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + + // START + // Clear the CRC Cache if it is populated + // + // Clear the CRC file Cache + safeMutexFTPProgress.Lock(); + Checksum::clearFileCache(); + + vector < string > paths = + Config::getInstance().getPathListForType(ptTechs); + string pathSearchString = string("/") + itemName + string("/*"); + const + string + filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + + // Refresh CRC + if (gameSettings != NULL && itemName == gameSettings->getTech() && + lastCheckedCRCTechtreeName == gameSettings->getTech() && + gameSettings->getTech() != "") { + Config & config = Config::getInstance(); + lastCheckedCRCTechtreeValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + string("/") + itemName + string("/*"), ".xml", NULL, true); + } + safeMutexFTPProgress.ReleaseLock(); + // END + + // Reload techs for the UI + string + scenarioDir = + Scenario::getScenarioDir(dirList, gameSettings->getScenario()); + findDirs(Config:: + getInstance().getPathListForType(ptTechs, scenarioDir), + techTreeFiles); + + vector < string > translatedTechs; + std::vector < string > techsFormatted = techTreeFiles; + for (int i = 0; i < (int) techsFormatted.size(); i++) { + techsFormatted.at(i) = formatString(techsFormatted.at(i)); + + string + txTech = + techTree->getTranslatedName(techTreeFiles.at(i), true); + translatedTechs.push_back(formatString(txTech)); + } + listBoxTechTree.setItems(techsFormatted, translatedTechs); + } else { + curl_version_info_data * + curlVersion = curl_version_info(CURLVERSION_NOW); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < (unsigned int) languageList.size(); + ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtreeFailDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTechtreeFailDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + (itemName + "(.7z)").c_str(), curlVersion->version); + } else { + snprintf(szMsg, 8096, + "Player: %s FAILED to download the techtree: [%s] using CURL version [%s]", + getHumanPlayerName().c_str(), + (itemName + "(.7z)").c_str(), curlVersion->version); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + + if (result.first == ftp_crt_HOST_NOT_ACCEPTING) { + if (lang.hasString("HostNotAcceptingDataConnections", + languageList[i]) == true) { + clientInterface->sendTextMessage(lang.getString + ("HostNotAcceptingDataConnections", + languageList[i]), -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } else { + clientInterface->sendTextMessage + ("*Warning* the host is not accepting data connections.", + -1, lang.isLanguageLocal(languageList[i]), + languageList[i]); + } + } + } + sleep(1); + + console.addLine(result.second, true); + } + } else if (type == ftp_cct_TempFile) { + getInProgressSavedGameFromFTPServerInProgress = false; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread->getProgressMutex() : + NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + //fileFTPProgressList.erase(itemName); + std::map < string, pair < int, + string > >::iterator + iterFind = fileFTPProgressList.find(itemName); + if (iterFind == fileFTPProgressList.end()) { + iterFind = + fileFTPProgressList.find(GameConstants:: + saveNetworkGameFileServerCompressed); + if (iterFind == fileFTPProgressList.end()) { + iterFind = + fileFTPProgressList.find(GameConstants:: + saveNetworkGameFileClientCompressed); + } + } + if (iterFind != fileFTPProgressList.end()) { + fileFTPProgressList.erase(iterFind->first); + } + safeMutexFTPProgress.ReleaseLock(); + + //printf("Status update downloading saved game file: [%s]\n",itemName.c_str()); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + //const GameSettings *gameSettings = clientInterface->getGameSettings(); + + if (result.first == ftp_crt_SUCCESS) { + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("JoinPlayerToCurrentGameSuccessDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("JoinPlayerToCurrentGameSuccessDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s SUCCESSFULLY downloaded the saved game: %s", + getHumanPlayerName().c_str(), itemName.c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + sleep(1); + + if (itemName == GameConstants::saveNetworkGameFileClientCompressed) { + string saveGameFilePath = "temp/"; + string + saveGameFile = + saveGameFilePath + + string(GameConstants::saveNetworkGameFileClientCompressed); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + saveGameFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + saveGameFilePath; + saveGameFile = + saveGameFilePath + + string(GameConstants::saveNetworkGameFileClientCompressed); + } else { + string + userData = + Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + saveGameFilePath = userData + saveGameFilePath; + saveGameFile = + saveGameFilePath + + string(GameConstants::saveNetworkGameFileClientCompressed); + } + + string + extractedFileName = + saveGameFilePath + + string(GameConstants::saveNetworkGameFileClient); + bool + extract_result = + extractFileFromZIPFile(saveGameFile, extractedFileName); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Saved game [%s] compressed to [%s] returned: %d\n", + saveGameFile.c_str(), extractedFileName.c_str(), + extract_result); + } + readyToJoinInProgressGame = true; + + //printf("Success downloading saved game file: [%s]\n",itemName.c_str()); + } else { + curl_version_info_data * + curlVersion = curl_version_info(CURLVERSION_NOW); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + char + szMsg[8096] = ""; + if (lang.hasString("JoinPlayerToCurrentGameFailDownload", + languageList[i]) == true) { + snprintf(szMsg, 8096, + lang.getString("JoinPlayerToCurrentGameFailDownload", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), itemName.c_str(), + curlVersion->version); + } else { + snprintf(szMsg, 8096, + "Player: %s FAILED to download the saved game: [%s] using CURL version [%s]", + getHumanPlayerName().c_str(), itemName.c_str(), + curlVersion->version); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + + if (result.first == ftp_crt_HOST_NOT_ACCEPTING) { + if (lang.hasString("HostNotAcceptingDataConnections", + languageList[i]) == true) { + clientInterface->sendTextMessage(lang.getString + ("HostNotAcceptingDataConnections", + languageList[i]), -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } else { + clientInterface->sendTextMessage + ("*Warning* the host is not accepting data connections.", + -1, lang.isLanguageLocal(languageList[i]), + languageList[i]); + } + } + } + sleep(1); + + console.addLine(result.second, true); + } + } + } + + void + MenuStateConnectedGame::setupUIFromGameSettings(GameSettings * + gameSettings, + bool errorOnMissingData) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + updateDataSynchDetailText = true; + vector < string > tilesets, techtree; + + if (gameSettings == NULL) { + throw megaglest_runtime_error("gameSettings == NULL"); + } + + + checkBoxScenario.setValue((gameSettings->getScenario() != "")); + if (checkBoxScenario.getValue() == true) { + int + originalFOWValue = listBoxFogOfWar.getSelectedItemIndex(); + + string scenario = gameSettings->getScenario(); + listBoxScenario.setSelectedItem(formatString(scenario)); + string file = Scenario::getScenarioPath(dirList, scenario); + + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, &scenarioInfo, isTutorial); + + gameSettings->setScenarioDir(Scenario::getScenarioPath + (dirList, scenarioInfo.name)); + + gameSettings->setDefaultResources(scenarioInfo.defaultResources); + gameSettings->setDefaultUnits(scenarioInfo.defaultUnits); + gameSettings-> + setDefaultVictoryConditions(scenarioInfo.defaultVictoryConditions); + + if (scenarioInfo.fogOfWar == false + && scenarioInfo.fogOfWar_exploredFlag == false) { + listBoxFogOfWar.setSelectedItemIndex(2); + } else if (scenarioInfo.fogOfWar_exploredFlag == true) { + listBoxFogOfWar.setSelectedItemIndex(1); + } else { + listBoxFogOfWar.setSelectedItemIndex(0); + } + + checkBoxAllowTeamUnitSharing. + setValue(scenarioInfo.allowTeamUnitSharing); + checkBoxAllowTeamResourceSharing. + setValue(scenarioInfo.allowTeamResourceSharing); + + if (originalFOWValue != listBoxFogOfWar.getSelectedItemIndex()) { + cleanupMapPreviewTexture(); + } + } + + if (listBoxMapFilter.getSelectedItemIndex() != + gameSettings->getMapFilter()) { + switchToMapGroup(gameSettings->getMapFilter()); + // printf("Switching to Map filter group %d \n",gameSettings->getMapFilter()); + } + + string + scenarioDir = + Scenario::getScenarioDir(dirList, gameSettings->getScenario()); + setupMapList(gameSettings->getScenario()); + setupTechList(gameSettings->getScenario()); + setupTilesetList(gameSettings->getScenario()); + + + //printf("A gameSettings->getTileset() [%s]\n",gameSettings->getTileset().c_str()); + + if (getMissingTilesetFromFTPServerInProgress == false + && gameSettings->getTileset() != "") { + // tileset + tilesets = tilesetFiles; + std::for_each(tilesets.begin(), tilesets.end(), FormatString()); + + if (std::find(tilesetFiles.begin(), tilesetFiles.end(), + gameSettings->getTileset()) != tilesetFiles.end()) { + lastMissingTileSet = ""; + getMissingTilesetFromFTPServer = ""; + listBoxTileset.setSelectedItem(formatString + (gameSettings->getTileset())); + } else { + // try to get the tileset via ftp + if (ftpClientThread != NULL && + (getMissingTilesetFromFTPServer != gameSettings->getTileset() + || difftime(time(NULL), + getMissingTilesetFromFTPServerLastPrompted) > + REPROMPT_DOWNLOAD_SECONDS)) { + if (ftpMessageBox.getEnabled() == false) { + getMissingTilesetFromFTPServerLastPrompted = time(NULL); + getMissingTilesetFromFTPServer = gameSettings->getTileset(); + Lang & lang = Lang::getInstance(); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s %s ?", + lang. + getString("DownloadMissingTilesetQuestion").c_str(), + gameSettings->getTileset().c_str()); + + // Is the item in the mod center? + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (tilesetCacheList.find(getMissingTilesetFromFTPServer) == + tilesetCacheList.end()) { + ftpMessageBox.init(lang.getString("Yes"), + lang.getString("NoDownload")); + } else { + ftpMessageBox.init(lang.getString("ModCenter"), + lang.getString("GameHost")); + ftpMessageBox.addButton(lang.getString("NoDownload")); + } + safeMutexThread.ReleaseLock(); + + ftpMissingDataType = ftpmsg_MissingTileset; + showFTPMessageBox(szBuf, lang.getString("Question"), false); + } + } + + tilesets. + push_back(Lang:: + getInstance().getString("DataMissing", "", true)); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (lastMissingTileSet != gameSettings->getTileset()) { + lastMissingTileSet = gameSettings->getTileset(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTileset", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTileset", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + gameSettings->getTileset().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is missing the tileset: %s", + getHumanPlayerName().c_str(), + gameSettings->getTileset().c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + } + + listBoxTileset.setItems(tilesets); + listBoxTileset. + setSelectedItem(Lang:: + getInstance().getString("DataMissing", "", + true)); + } + + } + + if (getMissingTechtreeFromFTPServerInProgress == false && + gameSettings->getTech() != "") { + // techtree + techtree = techTreeFiles; + std::for_each(techtree.begin(), techtree.end(), FormatString()); + + if (std::find(techTreeFiles.begin(), techTreeFiles.end(), + gameSettings->getTech()) != techTreeFiles.end()) { + + lastMissingTechtree = ""; + getMissingTechtreeFromFTPServer = ""; + reloadFactions(true, gameSettings->getScenario()); + listBoxTechTree.setSelectedItem(formatString + (gameSettings->getTech())); + } else { + // try to get the tileset via ftp + if (ftpClientThread != NULL + && (getMissingTechtreeFromFTPServer != gameSettings->getTech() + || difftime(time(NULL), + getMissingTechtreeFromFTPServerLastPrompted) > + REPROMPT_DOWNLOAD_SECONDS)) { + if (ftpMessageBox.getEnabled() == false) { + getMissingTechtreeFromFTPServerLastPrompted = time(NULL); + getMissingTechtreeFromFTPServer = gameSettings->getTech(); + Lang & lang = Lang::getInstance(); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s %s ?", + lang. + getString("DownloadMissingTechtreeQuestion").c_str + (), gameSettings->getTech().c_str()); + + // Is the item in the mod center? + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (techCacheList.find(getMissingTechtreeFromFTPServer) == + techCacheList.end()) { + ftpMessageBox.init(lang.getString("Yes"), + lang.getString("NoDownload")); + } else { + ftpMessageBox.init(lang.getString("ModCenter"), + lang.getString("GameHost")); + ftpMessageBox.addButton(lang.getString("NoDownload")); + } + safeMutexThread.ReleaseLock(); + + ftpMissingDataType = ftpmsg_MissingTechtree; + showFTPMessageBox(szBuf, lang.getString("Question"), false); + } + } + + techtree. + push_back(Lang:: + getInstance().getString("DataMissing", "", true)); + + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + const GameSettings * + gameSettings = clientInterface->getGameSettings(); + + if (lastMissingTechtree != gameSettings->getTech()) { + lastMissingTechtree = gameSettings->getTech(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Lang & lang = Lang::getInstance(); + const + vector < + string > + languageList = + clientInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + + char + szMsg[8096] = ""; + if (lang.hasString("DataMissingTechtree", languageList[i]) == + true) { + snprintf(szMsg, 8096, + lang.getString("DataMissingTechtree", + languageList[i]).c_str(), + getHumanPlayerName().c_str(), + gameSettings->getTech().c_str()); + } else { + snprintf(szMsg, 8096, + "Player: %s is missing the techtree: %s", + getHumanPlayerName().c_str(), + gameSettings->getTech().c_str()); + } + clientInterface->sendTextMessage(szMsg, -1, + lang.isLanguageLocal + (languageList[i]), + languageList[i]); + } + } + + vector < string > translatedTechs; + for (unsigned int i = 0; i < techTreeFiles.size(); i++) { + string + txTech = techTree->getTranslatedName(techTreeFiles.at(i)); + translatedTechs.push_back(txTech); + } + listBoxTechTree.setItems(techtree, translatedTechs); + listBoxTechTree. + setSelectedItem(Lang:: + getInstance().getString("DataMissing", "", + true)); + } + } + + // factions + bool hasFactions = true; + if (currentFactionName != gameSettings->getTech() + && gameSettings->getTech() != "") { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] hasFactions = %d, currentFactionName [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, hasFactions, + currentFactionName.c_str()); + currentFactionName = gameSettings->getTech(); + hasFactions = loadFactions(gameSettings, false); + } else { + // do this to process special faction types like observers + loadFactions(gameSettings, false); + } + + + if (getMissingMapFromFTPServerInProgress == false && + gameSettings->getMap() != "" + && gameSettings->getMapFilter() == + listBoxMapFilter.getSelectedItemIndex()) { + // map + bool missingMap = false; + string mapFile = gameSettings->getMap(); + mapFile = formatString(mapFile); + + if (currentMap != gameSettings->getMap()) { // load the setup again + currentMap = gameSettings->getMap(); + } + bool + mapLoaded = + loadMapInfo(Config::getMapPath(currentMap, scenarioDir, false), + &mapInfo, true); + if (mapLoaded == false) { + // try to get the map via ftp + if (ftpClientThread != NULL + && (getMissingMapFromFTPServer != currentMap + || difftime(time(NULL), + getMissingMapFromFTPServerLastPrompted) > + REPROMPT_DOWNLOAD_SECONDS)) { + if (ftpMessageBox.getEnabled() == false) { + getMissingMapFromFTPServerLastPrompted = time(NULL); + getMissingMapFromFTPServer = currentMap; + Lang & lang = Lang::getInstance(); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s %s ?", + lang. + getString("DownloadMissingMapQuestion").c_str(), + currentMap.c_str()); + + // Is the item in the mod center? + MutexSafeWrapper + safeMutexThread((modHttpServerThread != + NULL ? + modHttpServerThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (mapCacheList.find(getMissingMapFromFTPServer) == + mapCacheList.end()) { + ftpMessageBox.init(lang.getString("Yes"), + lang.getString("NoDownload")); + } else { + ftpMessageBox.init(lang.getString("ModCenter"), + lang.getString("GameHost")); + ftpMessageBox.addButton(lang.getString("NoDownload")); + } + safeMutexThread.ReleaseLock(); + + ftpMissingDataType = ftpmsg_MissingMap; + showFTPMessageBox(szBuf, lang.getString("Question"), false); + } + } + + formattedPlayerSortedMaps[gameSettings-> + getMapFilter()].push_back(Lang:: + getInstance + ().getString + ("DataMissing", + "", true)); + mapFile = Lang::getInstance().getString("DataMissing", "", true); + missingMap = true; + } + + if (isHeadlessAdmin() && !missingMap + && mapFile != listBoxMap.getSelectedItem()) { + //console.addLine("Headless server does not have map, switching to next one"); + if (isfirstSwitchingMapMessage) { + isfirstSwitchingMapMessage = false; + } else { + console.addLine(Lang::getInstance().getString + ("HeadlessServerDoesNotHaveMap", "", true)); + } + } + listBoxMap.setItems(formattedPlayerSortedMaps + [gameSettings->getMapFilter()]); + + //printf("Setting map from game settings map:%s , settingsfilter=%d , boxfilter=%d \n",gameSettings->getMap().c_str(),gameSettings->getMapFilter(),listBoxMapFilter.getSelectedItemIndex()); + listBoxMap.setSelectedItem(mapFile); + labelMapInfo.setText(mapInfo.desc); + } + + // FogOfWar + if (checkBoxScenario.getValue() == false) { + int + originalFOWValue = listBoxFogOfWar.getSelectedItemIndex(); + listBoxFogOfWar.setSelectedItemIndex(0); // default is 0! + if (gameSettings->getFogOfWar() == false) { + listBoxFogOfWar.setSelectedItemIndex(2); + } + if ((gameSettings->getFlagTypes1() & ft1_show_map_resources) == + ft1_show_map_resources) { + if (gameSettings->getFogOfWar() == true) { + listBoxFogOfWar.setSelectedItemIndex(1); + } + } + if (originalFOWValue != listBoxFogOfWar.getSelectedItemIndex()) { + cleanupMapPreviewTexture(); + } + } + + // Allow Observers + if (gameSettings->getAllowObservers()) { + checkBoxAllowObservers.setValue(true); + } else { + checkBoxAllowObservers.setValue(false); + } + + if ((gameSettings->getFlagTypes1() & ft1_allow_team_switching) == + ft1_allow_team_switching) { + checkBoxEnableSwitchTeamMode.setValue(true); + } else { + checkBoxEnableSwitchTeamMode.setValue(false); + } + listBoxAISwitchTeamAcceptPercent.setSelectedItem(intToStr + (gameSettings->getAiAcceptSwitchTeamPercentChance + ())); + listBoxFallbackCpuMultiplier. + setSelectedItemIndex(gameSettings->getFallbackCpuMultiplier()); + + if ((gameSettings->getFlagTypes1() & ft1_allow_shared_team_units) == + ft1_allow_shared_team_units) { + checkBoxAllowTeamUnitSharing.setValue(true); + } else { + checkBoxAllowTeamUnitSharing.setValue(false); + } + + if ((gameSettings->getFlagTypes1() & ft1_allow_shared_team_resources) + == ft1_allow_shared_team_resources) { + checkBoxAllowTeamResourceSharing.setValue(true); + } else { + checkBoxAllowTeamResourceSharing.setValue(false); + } + + checkBoxAllowNativeLanguageTechtree. + setValue(gameSettings->getNetworkAllowNativeLanguageTechtree()); + + // Control + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxControls[i].setSelectedItemIndex(ctClosed); + + if (isHeadlessAdmin() == false) { + if (clientInterface->getJoinGameInProgress() == false) { + listBoxFactions[i].setEditable(false); + listBoxTeams[i].setEditable(false); + } + } + + labelPlayerStatus[i].setTexture(NULL);; + } + + if (hasFactions == true && gameSettings != NULL) { + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + + //for(int i=0; i < gameSettings->getFactionCount(); ++i){ + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + int + slot = gameSettings->getStartLocationIndex(i); + + if (slot == clientInterface->getPlayerIndex()) { + labelPlayerNames[slot].setEditable(true); + } else { + labelPlayerNames[slot].setEditable(false); + } + + if (i >= mapInfo.players) { + if (gameSettings->getFactionControl(i) != ctNetworkUnassigned) { + continue; + } else if (clientInterface->getPlayerIndex() != slot) { + continue; + } + } + + if (gameSettings->getFactionControl(i) == ctNetwork || + gameSettings->getFactionControl(i) == ctNetworkUnassigned || + gameSettings->getFactionControl(i) == ctHuman) { + switch (gameSettings->getNetworkPlayerStatuses(i)) { + case npst_BeRightBack: + labelPlayerStatus[slot]. + setTexture(CoreData::getInstance().getStatusBRBTexture()); + break; + case npst_Ready: + labelPlayerStatus[slot]. + setTexture(CoreData:: + getInstance().getStatusReadyTexture()); + break; + case npst_PickSettings: + labelPlayerStatus[slot]. + setTexture(CoreData:: + getInstance().getStatusNotReadyTexture()); + break; + case npst_Disconnected: + labelPlayerStatus[slot].setTexture(NULL); + break; + + default: + labelPlayerStatus[slot].setTexture(NULL); + break; + } + } + + listBoxControls[slot]. + setSelectedItemIndex(gameSettings->getFactionControl(i), + errorOnMissingData); + listBoxRMultiplier[slot]. + setSelectedItemIndex(gameSettings->getResourceMultiplierIndex + (i), errorOnMissingData); + listBoxTeams[slot].setSelectedItemIndex(gameSettings->getTeam(i), + errorOnMissingData); + listBoxFactions[slot].setSelectedItem(formatString + (gameSettings-> + getFactionTypeName(i)), + false); + + if (gameSettings->getFactionControl(i) == ctNetwork || + gameSettings->getFactionControl(i) == ctNetworkUnassigned) { + labelNetStatus[slot]. + setText(gameSettings->getNetworkPlayerName(i)); + if (gameSettings->getThisFactionIndex() != i + && gameSettings->getNetworkPlayerName(i) != "" + && gameSettings->getNetworkPlayerName(i) != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + labelPlayerNames[slot]. + setText(gameSettings->getNetworkPlayerName(i)); + } + } + + ControlType ct = gameSettings->getFactionControl(i); + if (ct == ctHuman || ct == ctNetwork || ct == ctClosed) { + listBoxRMultiplier[slot].setEnabled(false); + listBoxRMultiplier[slot].setVisible(false); + } else { + listBoxRMultiplier[slot].setEnabled(true); + listBoxRMultiplier[slot].setVisible(true); + } + + if ((gameSettings->getFactionControl(i) == ctNetwork || + gameSettings->getFactionControl(i) == ctNetworkUnassigned) && + gameSettings->getThisFactionIndex() == i) { + + // set my current slot to ctHuman + if (gameSettings->getFactionControl(i) != ctNetworkUnassigned) { + listBoxControls[slot].setSelectedItemIndex(ctHuman); + } + if (checkBoxScenario.getValue() == false) { + if (clientInterface->getJoinGameInProgress() == false) { + if (i <= mapInfo.hardMaxPlayers) { + listBoxFactions[slot].setEditable(true); + listBoxTeams[slot].setEditable(true); + } else { + // looks more must be done to allow people to take an observer + // slot after the game has started. Extra network slots close + // when the game starts. + listBoxFactions[slot].setEditable(checkBoxAllowObservers.getValue()); + listBoxTeams[slot].setEditable(checkBoxAllowObservers.getValue()); + } + } + } + + if (labelPlayerNames[slot].getText() == "" && + gameSettings->getNetworkPlayerName(i) != "" && + gameSettings->getNetworkPlayerName(i) != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + labelPlayerNames[slot]. + setText(gameSettings->getNetworkPlayerName(i)); + } + } + } + settingsReceivedFromServer = true; + initialSettingsReceivedFromServer = true; + + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + + if (enableFactionTexturePreview == true) { + if (clientInterface != NULL && clientInterface->isConnected() && + gameSettings != NULL) { + + if (currentTechName_factionPreview != gameSettings->getTech() || + currentFactionName_factionPreview != + gameSettings-> + getFactionTypeName(gameSettings->getThisFactionIndex())) { + + currentTechName_factionPreview = gameSettings->getTech(); + currentFactionName_factionPreview = + gameSettings-> + getFactionTypeName(gameSettings->getThisFactionIndex()); + + initFactionPreview(gameSettings); + } + } + } + + } + + void + MenuStateConnectedGame::initFactionPreview(const GameSettings * + gameSettings) { + string factionVideoUrl = ""; + string factionVideoUrlFallback = ""; + + string + factionDefinitionXML = + Game::findFactionLogoFile(gameSettings, NULL, + currentFactionName_factionPreview + + ".xml"); + if (factionDefinitionXML != "" + && currentFactionName_factionPreview != + GameConstants::RANDOMFACTION_SLOTNAME + && currentFactionName_factionPreview != + GameConstants::OBSERVER_SLOTNAME + && fileExists(factionDefinitionXML) == true) { + XmlTree xmlTree; + std::map < string, string > mapExtraTagReplacementValues; + xmlTree.load(factionDefinitionXML, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues)); + const XmlNode * + factionNode = xmlTree.getRootNode(); + if (factionNode->hasAttribute("faction-preview-video") == true) { + factionVideoUrl = + factionNode->getAttribute("faction-preview-video")->getValue(); + } + + factionVideoUrlFallback = + Game::findFactionLogoFile(gameSettings, NULL, "preview_video.*"); + if (factionVideoUrl == "") { + factionVideoUrl = factionVideoUrlFallback; + factionVideoUrlFallback = ""; + } + } + + if (factionVideoUrl != "") { + if (CoreData::getInstance().getMenuMusic()->getVolume() != 0) { + CoreData::getInstance().getMenuMusic()->setVolume(0); + factionVideoSwitchedOffVolume = true; + } + + if (currentFactionLogo != factionVideoUrl) { + currentFactionLogo = factionVideoUrl; + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + string introVideoFile = factionVideoUrl; + string introVideoFileFallback = factionVideoUrlFallback; + + Context * + c = GraphicsInterface::getInstance().getCurrentContext(); + PlatformContextGl * + glCtx = + static_cast (c)->getPlatformContextGlPtr(); + SDL_Window * + window = glCtx->getScreenWindow(); + SDL_Surface * + screen = glCtx->getScreenSurface(); + + string + vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + factionVideo = new VideoPlayer(&Renderer::getInstance(), + introVideoFile, + introVideoFileFallback, + window, + 0, 0, + screen->w, + screen->h, + screen->format->BitsPerPixel, + true, + vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + factionVideo->initPlayer(); + } + } + } else { + //switch on music again!! + Config & config = Config::getInstance(); + float + configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + if (factionVideoSwitchedOffVolume) { + if (CoreData::getInstance().getMenuMusic()->getVolume() != + configVolume) { + CoreData::getInstance(). + getMenuMusic()->setVolume(configVolume); + } + factionVideoSwitchedOffVolume = false; + } + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + } + + if (factionVideo == NULL) { + string + factionLogo = + Game::findFactionLogoFile(gameSettings, NULL, + GameConstants::PREVIEW_SCREEN_FILE_FILTER); + if (factionLogo == "") { + factionLogo = Game::findFactionLogoFile(gameSettings, NULL); + } + if (currentFactionLogo != factionLogo) { + currentFactionLogo = factionLogo; + loadFactionTexture(currentFactionLogo); + } + } + } + + void + MenuStateConnectedGame::RestoreLastGameSettings() { + // Ensure we have set the gamesettings at least once + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface * + clientInterface = networkManager.getClientInterface(); + GameSettings gameSettings = *clientInterface->getGameSettings(); + CoreData:: + getInstance().loadGameSettingsFromFile(HEADLESS_SAVED_GAME_FILENAME, + &gameSettings); + if (gameSettings.getMap() == "") { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + loadGameSettings(&gameSettings); + } + + setupUIFromGameSettings(&gameSettings, false); + + needToBroadcastServerSettings = true; + broadcastServerSettingsDelayTimer = time(NULL); + noReceiveTimer = time(NULL); + + } + + int + MenuStateConnectedGame::setupMapList(string scenario) { + int + initialMapSelection = 0; + + try { + Config & config = Config::getInstance(); + vector < string > invalidMapList; + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > pathList = + config.getPathListForType(ptMaps, scenarioDir); + vector < string > allMaps = + MapPreview::findAllValidMaps(pathList, scenarioDir, false, true, + &invalidMapList); + // sort map list non case sensitive + std::sort(allMaps.begin(), allMaps.end(), compareNonCaseSensitive); + if (scenario != "") { + vector < string > allMaps2 = + MapPreview:: + findAllValidMaps(config.getPathListForType(ptMaps, ""), "", + false, true, &invalidMapList); + copy(allMaps2.begin(), allMaps2.end(), + std::inserter(allMaps, allMaps.begin())); + std::sort(allMaps.begin(), allMaps.end(), + compareNonCaseSensitive); + } + + if (allMaps.empty()) { + throw megaglest_runtime_error("No maps were found!"); + } + vector < string > results; + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; + + for (unsigned int i = 0; i < GameConstants::maxPlayers + 1; ++i) { + playerSortedMaps[i].clear(); + formattedPlayerSortedMaps[i].clear(); + } + + copy(mapFiles.begin(), mapFiles.end(), + std::back_inserter(playerSortedMaps[0])); + copy(playerSortedMaps[0].begin(), playerSortedMaps[0].end(), + std::back_inserter(formattedPlayerSortedMaps[0])); + std::for_each(formattedPlayerSortedMaps[0].begin(), + formattedPlayerSortedMaps[0].end(), FormatString()); + + formattedMapFiles.clear(); + for (int i = 0; i < (int) mapFiles.size(); i++) { // fetch info and put map in right list + loadMapInfo(Config::getMapPath + (mapFiles.at(i), scenarioDir, false), &mapInfo, + false); + + if (GameConstants::maxPlayers + 1 <= mapInfo.players) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Sorted map list [%d] does not match\ncurrent map playercount [%d]\nfor file [%s]\nmap [%s]", + GameConstants::maxPlayers + 1, mapInfo.players, + Config::getMapPath(mapFiles.at(i), "", + false).c_str(), + mapInfo.desc.c_str()); + throw megaglest_runtime_error(szBuf); + } + playerSortedMaps[mapInfo.hardMaxPlayers].push_back(mapFiles.at(i)); + formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].push_back(formatString + (mapFiles.at + (i))); + if (config.getString("InitialMap", "Conflict") == + formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].back()) { + initialMapSelection = i; + } + formattedMapFiles.push_back(formatString(mapFiles.at(i))); + } + + if (scenario != "") { + string file = Scenario::getScenarioPath(dirList, scenario); + loadScenarioInfo(file, &scenarioInfo); + + loadMapInfo(Config::getMapPath + (scenarioInfo.mapName, scenarioDir, true), &mapInfo, + false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line %d] listBoxMap.getSelectedItemIndex() = %d, mapFiles.size() = " + MG_SIZE_T_SPECIFIER + ", mapInfo.players = %d, formattedPlayerSortedMaps[mapInfo.players].size() = " + MG_SIZE_T_SPECIFIER ", scenarioInfo.mapName [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, listBoxMap.getSelectedItemIndex(), mapFiles.size(), + mapInfo.players, + formattedPlayerSortedMaps[mapInfo.players].size(), + scenarioInfo.mapName.c_str()); + listBoxMap.setItems(formattedPlayerSortedMaps[mapInfo.players]); + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + //abort(); + } + + return initialMapSelection; + } + + int + MenuStateConnectedGame::setupTechList(string scenario, bool forceLoad) { + int + initialTechSelection = 0; + try { + Config & config = Config::getInstance(); + + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > results; + vector < string > techPaths = + config.getPathListForType(ptTechs, scenarioDir); + findDirs(techPaths, results); + + if (results.empty()) { + throw megaglest_runtime_error("No tech-trees were found!"); + } + + techTreeFiles = results; + + vector < string > translatedTechs; + + for (unsigned int i = 0; i < results.size(); i++) { + //printf("TECHS i = %d results [%s] scenario [%s]\n",i,results[i].c_str(),scenario.c_str()); + + results.at(i) = formatString(results.at(i)); + if (config.getString("InitialTechTree", "Megapack") == + results.at(i)) { + initialTechSelection = i; + } + string + txTech = + techTree->getTranslatedName(techTreeFiles.at(i), forceLoad); + translatedTechs.push_back(formatString(txTech)); + } + + + listBoxTechTree.setItems(results, translatedTechs); + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + + return initialTechSelection; + } + + void + MenuStateConnectedGame::setupTilesetList(string scenario) { + try { + Config & config = Config::getInstance(); + + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + + vector < string > results; + findDirs(config.getPathListForType(ptTilesets, scenarioDir), + results); + if (results.empty()) { + //throw megaglest_runtime_error("No tile-sets were found!"); + showMessageBox("No tile-sets were found!", "Error", false); + } else { + tilesetFiles = results; + std::for_each(results.begin(), results.end(), FormatString()); + + listBoxTileset.setItems(results); + } + } catch (const std::exception & ex) { + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + } + + void + MenuStateConnectedGame::loadScenarioInfo(string file, + ScenarioInfo * scenarioInfo) { + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, scenarioInfo, isTutorial); + + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + } + + } +} //end namespace diff --git a/source/glest_game/menu/menu_state_connected_game.h b/source/glest_game/menu/menu_state_connected_game.h index 6de8b4d26..1db58ce10 100644 --- a/source/glest_game/menu/menu_state_connected_game.h +++ b/source/glest_game/menu/menu_state_connected_game.h @@ -36,326 +36,321 @@ # include "common_scoped_ptr.h" # include "leak_dumper.h" -namespace Shared -{ - namespace Graphics - { - class VideoPlayer; -}} - -namespace Glest -{ - namespace Game - { - - class TechTree; - - enum JoinMenu - { - jmSimple, - jmMasterserver, - - jmCount - }; - - enum FTPMessageType - { - ftpmsg_MissingNone, - ftpmsg_MissingMap, - ftpmsg_MissingTileset, - ftpmsg_MissingTechtree - }; - -// =============================== -// class MenuStateConnectedGame -// =============================== - - class MenuStateConnectedGame:public MenuState, - public FTPClientCallbackInterface, public SimpleTaskCallbackInterface - { - private: - GraphicButton buttonDisconnect; - GraphicLabel labelControl; - GraphicLabel labelRMultiplier; - GraphicLabel labelFaction; - GraphicLabel labelTeam; - GraphicLabel labelMap; - GraphicLabel labelFogOfWar; - GraphicLabel labelTechTree; - GraphicLabel labelTileset; - GraphicLabel labelMapInfo; - GraphicLabel labelStatus; - GraphicLabel labelInfo; - GraphicLabel labelWaitingForPlayers; - GraphicButton buttonRestoreLastSettings; - -//GraphicLabel labelPathFinderType; -//GraphicListBox listBoxPathFinderType; - - GraphicLabel labelMapPlayerCount; - GraphicListBox listBoxMapPlayerCount; - - GraphicLabel labelAdvanced; - GraphicListBox listBoxAdvanced; - - GraphicListBox listBoxMap; - GraphicListBox listBoxFogOfWar; - GraphicListBox listBoxTechTree; - GraphicListBox listBoxTileset; - GraphicLabel labelPlayers[GameConstants::maxPlayers]; - GraphicLabel labelPlayerNames[GameConstants::maxPlayers]; - GraphicListBox listBoxControls[GameConstants::maxPlayers]; - GraphicListBox listBoxRMultiplier[GameConstants::maxPlayers]; - GraphicListBox listBoxFactions[GameConstants::maxPlayers]; - GraphicListBox listBoxTeams[GameConstants::maxPlayers]; - GraphicLabel labelNetStatus[GameConstants::maxPlayers]; - GraphicButton grabSlotButton[GameConstants::maxPlayers]; - - GraphicListBox listBoxPlayerStatus; - GraphicLabel labelPlayerStatus[GameConstants::maxPlayers]; - - GraphicLabel labelMapFilter; - GraphicListBox listBoxMapFilter; - - GraphicLabel labelAllowObservers; - GraphicCheckBox checkBoxAllowObservers; - - GraphicLabel labelAllowNativeLanguageTechtree; - GraphicCheckBox checkBoxAllowNativeLanguageTechtree; - - GraphicLabel *activeInputLabel; - - time_t timerLabelFlash; - GraphicLabel labelDataSynchInfo; - - MapInfo mapInfo; - Texture2D *mapPreviewTexture; - bool zoomedMap; - int render_mapPreviewTexture_X; - int render_mapPreviewTexture_Y; - int render_mapPreviewTexture_W; - int render_mapPreviewTexture_H; - - bool needToSetChangedGameSettings; - time_t lastSetChangedGameSettings; - bool updateDataSynchDetailText; - - int soundConnectionCount; - -//Console console; - ChatManager chatManager; - bool showFullConsole; - - string currentFactionName; - string currentMap; - JoinMenu returnMenuInfo; - bool settingsReceivedFromServer; - time_t lastNetworkSendPing; - int pingCount; - bool initialSettingsReceivedFromServer; - - string lastMapDataSynchError; - string lastTileDataSynchError; - string lastTechtreeDataSynchError; - - int8 switchSetupRequestFlagType; - string defaultPlayerName; - - bool enableFactionTexturePreview; - bool enableMapPreview; - - string currentTechName_factionPreview; - string currentFactionName_factionPreview; - string currentFactionLogo; - Texture2D *factionTexture; - ::Shared::Graphics::VideoPlayer * factionVideo; - bool factionVideoSwitchedOffVolume; - - MapPreview mapPreview; - - GraphicMessageBox mainMessageBox; - - std::string lastMissingMap; - std::string lastMissingTechtree; - std::string lastMissingTileSet; - - vector < string > mapFiles; - vector < string > techTreeFiles; - vector < string > tilesetFiles; - vector < string > factionFiles; +namespace Shared { + namespace Graphics { + class VideoPlayer; + } +} + +namespace Glest { + namespace Game { + + class TechTree; + + enum JoinMenu { + jmSimple, + jmMasterserver, + + jmCount + }; + + enum FTPMessageType { + ftpmsg_MissingNone, + ftpmsg_MissingMap, + ftpmsg_MissingTileset, + ftpmsg_MissingTechtree + }; + + // =============================== + // class MenuStateConnectedGame + // =============================== + + class MenuStateConnectedGame :public MenuState, + public FTPClientCallbackInterface, public SimpleTaskCallbackInterface { + private: + GraphicButton buttonDisconnect; + GraphicLabel labelControl; + GraphicLabel labelRMultiplier; + GraphicLabel labelFaction; + GraphicLabel labelTeam; + GraphicLabel labelMap; + GraphicLabel labelFogOfWar; + GraphicLabel labelTechTree; + GraphicLabel labelTileset; + GraphicLabel labelMapInfo; + GraphicLabel labelStatus; + GraphicLabel labelInfo; + GraphicLabel labelWaitingForPlayers; + GraphicButton buttonRestoreLastSettings; + + //GraphicLabel labelPathFinderType; + //GraphicListBox listBoxPathFinderType; + + GraphicLabel labelMapPlayerCount; + GraphicListBox listBoxMapPlayerCount; + + GraphicLabel labelAdvanced; + GraphicListBox listBoxAdvanced; + + GraphicListBox listBoxMap; + GraphicListBox listBoxFogOfWar; + GraphicListBox listBoxTechTree; + GraphicListBox listBoxTileset; + GraphicLabel labelPlayers[GameConstants::maxPlayers]; + GraphicLabel labelPlayerNames[GameConstants::maxPlayers]; + GraphicListBox listBoxControls[GameConstants::maxPlayers]; + GraphicListBox listBoxRMultiplier[GameConstants::maxPlayers]; + GraphicListBox listBoxFactions[GameConstants::maxPlayers]; + GraphicListBox listBoxTeams[GameConstants::maxPlayers]; + GraphicLabel labelNetStatus[GameConstants::maxPlayers]; + GraphicButton grabSlotButton[GameConstants::maxPlayers]; + + GraphicListBox listBoxPlayerStatus; + GraphicLabel labelPlayerStatus[GameConstants::maxPlayers]; + + GraphicLabel labelMapFilter; + GraphicListBox listBoxMapFilter; + + GraphicLabel labelAllowObservers; + GraphicCheckBox checkBoxAllowObservers; + + GraphicLabel labelAllowNativeLanguageTechtree; + GraphicCheckBox checkBoxAllowNativeLanguageTechtree; + + GraphicLabel *activeInputLabel; + + time_t timerLabelFlash; + GraphicLabel labelDataSynchInfo; + + MapInfo mapInfo; + Texture2D *mapPreviewTexture; + bool zoomedMap; + int render_mapPreviewTexture_X; + int render_mapPreviewTexture_Y; + int render_mapPreviewTexture_W; + int render_mapPreviewTexture_H; + + bool needToSetChangedGameSettings; + time_t lastSetChangedGameSettings; + bool updateDataSynchDetailText; + + int soundConnectionCount; + + //Console console; + ChatManager chatManager; + bool showFullConsole; + + string currentFactionName; + string currentMap; + JoinMenu returnMenuInfo; + bool settingsReceivedFromServer; + time_t lastNetworkSendPing; + int pingCount; + bool initialSettingsReceivedFromServer; + + string lastMapDataSynchError; + string lastTileDataSynchError; + string lastTechtreeDataSynchError; + + int8 switchSetupRequestFlagType; + string defaultPlayerName; + + bool enableFactionTexturePreview; + bool enableMapPreview; + + string currentTechName_factionPreview; + string currentFactionName_factionPreview; + string currentFactionLogo; + Texture2D *factionTexture; + ::Shared::Graphics::VideoPlayer * factionVideo; + bool factionVideoSwitchedOffVolume; + + MapPreview mapPreview; + + GraphicMessageBox mainMessageBox; + + std::string lastMissingMap; + std::string lastMissingTechtree; + std::string lastMissingTileSet; + + vector < string > mapFiles; + vector < string > techTreeFiles; + vector < string > tilesetFiles; + vector < string > factionFiles; - vector < string > playerSortedMaps[GameConstants::maxPlayers + 1]; - vector < string > - formattedPlayerSortedMaps[GameConstants::maxPlayers + 1]; - vector < string > formattedMapFiles; + vector < string > playerSortedMaps[GameConstants::maxPlayers + 1]; + vector < string > + formattedPlayerSortedMaps[GameConstants::maxPlayers + 1]; + vector < string > formattedMapFiles; - GraphicMessageBox ftpMessageBox; - FTPClientThread *ftpClientThread; - FTPMessageType ftpMissingDataType; - - SimpleTaskThread *modHttpServerThread; - std::vector < std::string > tilesetListRemote; - std::map < string, ModInfo > tilesetCacheList; - std::vector < std::string > techListRemote; - std::map < string, ModInfo > techCacheList; - std::vector < std::string > mapListRemote; - std::map < string, ModInfo > mapCacheList; + GraphicMessageBox ftpMessageBox; + FTPClientThread *ftpClientThread; + FTPMessageType ftpMissingDataType; + + SimpleTaskThread *modHttpServerThread; + std::vector < std::string > tilesetListRemote; + std::map < string, ModInfo > tilesetCacheList; + std::vector < std::string > techListRemote; + std::map < string, ModInfo > techCacheList; + std::vector < std::string > mapListRemote; + std::map < string, ModInfo > mapCacheList; - std::map < string, uint32 > mapCRCUpdateList; + std::map < string, uint32 > mapCRCUpdateList; - string getMissingMapFromFTPServer; - bool getMissingMapFromFTPServerInProgress; - time_t getMissingMapFromFTPServerLastPrompted; + string getMissingMapFromFTPServer; + bool getMissingMapFromFTPServerInProgress; + time_t getMissingMapFromFTPServerLastPrompted; - string getMissingTilesetFromFTPServer; - bool getMissingTilesetFromFTPServerInProgress; - time_t getMissingTilesetFromFTPServerLastPrompted; + string getMissingTilesetFromFTPServer; + bool getMissingTilesetFromFTPServerInProgress; + time_t getMissingTilesetFromFTPServerLastPrompted; - string getMissingTechtreeFromFTPServer; - bool getMissingTechtreeFromFTPServerInProgress; - time_t getMissingTechtreeFromFTPServerLastPrompted; + string getMissingTechtreeFromFTPServer; + bool getMissingTechtreeFromFTPServerInProgress; + time_t getMissingTechtreeFromFTPServerLastPrompted; - string getInProgressSavedGameFromFTPServer; - bool getInProgressSavedGameFromFTPServerInProgress; - bool readyToJoinInProgressGame; + string getInProgressSavedGameFromFTPServer; + bool getInProgressSavedGameFromFTPServerInProgress; + bool readyToJoinInProgressGame; - string lastCheckedCRCTilesetName; - string lastCheckedCRCTechtreeName; - string lastCheckedCRCMapName; - uint32 lastCheckedCRCTilesetValue; - uint32 lastCheckedCRCTechtreeValue; - uint32 lastCheckedCRCMapValue; - vector < pair < string, uint32 > >factionCRCList; - - std::map < string, pair < int, string > >fileFTPProgressList; - GraphicButton buttonCancelDownloads; - - GraphicLabel labelEnableSwitchTeamMode; - GraphicCheckBox checkBoxEnableSwitchTeamMode; - - GraphicLabel labelAllowTeamUnitSharing; - GraphicCheckBox checkBoxAllowTeamUnitSharing; - - GraphicLabel labelAllowTeamResourceSharing; - GraphicCheckBox checkBoxAllowTeamResourceSharing; - - GraphicLabel labelAISwitchTeamAcceptPercent; - GraphicListBox listBoxAISwitchTeamAcceptPercent; - GraphicLabel labelFallbackCpuMultiplier; - GraphicListBox listBoxFallbackCpuMultiplier; - - - GraphicButton buttonPlayNow; - - GraphicCheckBox checkBoxScenario; - GraphicLabel labelScenario; - GraphicListBox listBoxScenario; - vector < string > scenarioFiles; - ScenarioInfo scenarioInfo; - vector < string > dirList; - string autoloadScenarioName; - time_t previewLoadDelayTimer; - bool needToLoadTextures; - bool enableScenarioTexturePreview; - Texture2D *scenarioLogoTexture; + string lastCheckedCRCTilesetName; + string lastCheckedCRCTechtreeName; + string lastCheckedCRCMapName; + uint32 lastCheckedCRCTilesetValue; + uint32 lastCheckedCRCTechtreeValue; + uint32 lastCheckedCRCMapValue; + vector < pair < string, uint32 > >factionCRCList; + + std::map < string, pair < int, string > >fileFTPProgressList; + GraphicButton buttonCancelDownloads; + + GraphicLabel labelEnableSwitchTeamMode; + GraphicCheckBox checkBoxEnableSwitchTeamMode; + + GraphicLabel labelAllowTeamUnitSharing; + GraphicCheckBox checkBoxAllowTeamUnitSharing; + + GraphicLabel labelAllowTeamResourceSharing; + GraphicCheckBox checkBoxAllowTeamResourceSharing; + + GraphicLabel labelAISwitchTeamAcceptPercent; + GraphicListBox listBoxAISwitchTeamAcceptPercent; + GraphicLabel labelFallbackCpuMultiplier; + GraphicListBox listBoxFallbackCpuMultiplier; + + + GraphicButton buttonPlayNow; + + GraphicCheckBox checkBoxScenario; + GraphicLabel labelScenario; + GraphicListBox listBoxScenario; + vector < string > scenarioFiles; + ScenarioInfo scenarioInfo; + vector < string > dirList; + string autoloadScenarioName; + time_t previewLoadDelayTimer; + bool needToLoadTextures; + bool enableScenarioTexturePreview; + Texture2D *scenarioLogoTexture; - bool needToBroadcastServerSettings; - time_t broadcastServerSettingsDelayTimer; - int lastGameSettingsReceivedCount; - - time_t noReceiveTimer; + bool needToBroadcastServerSettings; + time_t broadcastServerSettingsDelayTimer; + int lastGameSettingsReceivedCount; + + time_t noReceiveTimer; + + bool launchingNewGame; + bool isfirstSwitchingMapMessage; + auto_ptr < TechTree > techTree; - bool launchingNewGame; - bool isfirstSwitchingMapMessage; - auto_ptr < TechTree > techTree; + GameSettings originalGamesettings; + bool validOriginalGameSettings; + GameSettings displayedGamesettings; + bool validDisplayedGamesettings; + + + public: + + MenuStateConnectedGame(Program * program, MainMenu * mainMenu, + JoinMenu joinMenuInfo = + jmSimple, bool openNetworkSlots = false); + virtual ~MenuStateConnectedGame(); - GameSettings originalGamesettings; - bool validOriginalGameSettings; - GameSettings displayedGamesettings; - bool validDisplayedGamesettings; - - - public: + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + void update(); - MenuStateConnectedGame (Program * program, MainMenu * mainMenu, - JoinMenu joinMenuInfo = - jmSimple, bool openNetworkSlots = false); - virtual ~ MenuStateConnectedGame (); + virtual bool textInput(std::string text); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + virtual void keyUp(SDL_KeyboardEvent key); + + virtual bool isInSpecialKeyCaptureEvent(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - void update (); + virtual void reloadUI(); - virtual bool textInput (std::string text); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - virtual void keyUp (SDL_KeyboardEvent key); - - virtual bool isInSpecialKeyCaptureEvent (); + virtual bool isVideoPlaying(); - virtual void reloadUI (); + private: - virtual bool isVideoPlaying (); - - private: - - bool hasNetworkGameSettings (); - bool loadFactions (const GameSettings * gameSettings, - bool errorOnNoFactions); - void returnToJoinMenu (); - string getHumanPlayerName (); - void setActiveInputLabel (GraphicLabel * newLable); - - void loadFactionTexture (string filepath); - bool loadMapInfo (string file, MapInfo * mapInfo, bool loadMapPreview); - void showMessageBox (const string & text, const string & header, - bool toggle); - - void showFTPMessageBox (const string & text, const string & header, - bool toggle); - virtual void FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType type, - pair < FTP_Client_ResultType, - string > result, void *userdata); - - int32 getNetworkPlayerStatus (); - void cleanupMapPreviewTexture (); - - void mouseClickAdmin (int x, int y, MouseButton mouseButton, - string advanceToItemStartingWith); - void switchToNextMapGroup (const int direction); - void switchToMapGroup (int filterIndex); - string getCurrentMapFile (); - void loadGameSettings (GameSettings * gameSettings); - void reloadFactions (bool keepExistingSelectedItem, string scenario); - void PlayNow (bool saveGame); - bool isHeadlessAdmin (); - void broadCastGameSettingsToHeadlessServer (bool forceNow); - void updateResourceMultiplier (const int index); - - void RestoreLastGameSettings (); - void setupUIFromGameSettings (GameSettings * gameSettings, - bool errorOnMissingData); - - int setupMapList (string scenario); - int setupTechList (string scenario, bool forceLoad = false); - void setupTilesetList (string scenario); - - void loadScenarioInfo (string file, ScenarioInfo * scenarioInfo); - void initFactionPreview (const GameSettings * gameSettings); - - virtual void simpleTask (BaseThread * callingThread, void *userdata); - string refreshTilesetModInfo (string tilesetInfo); - string refreshTechModInfo (string techInfo); - string refreshMapModInfo (string mapInfo); - string getMapCRC (string mapName); - - void disconnectFromServer (); - }; - -}} //end namespace + bool hasNetworkGameSettings(); + bool loadFactions(const GameSettings * gameSettings, + bool errorOnNoFactions); + void returnToJoinMenu(); + string getHumanPlayerName(); + void setActiveInputLabel(GraphicLabel * newLable); + + void loadFactionTexture(string filepath); + bool loadMapInfo(string file, MapInfo * mapInfo, bool loadMapPreview); + void showMessageBox(const string & text, const string & header, + bool toggle); + + void showFTPMessageBox(const string & text, const string & header, + bool toggle); + virtual void FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType type, + pair < FTP_Client_ResultType, + string > result, void *userdata); + + int32 getNetworkPlayerStatus(); + void cleanupMapPreviewTexture(); + + void mouseClickAdmin(int x, int y, MouseButton mouseButton, + string advanceToItemStartingWith); + void switchToNextMapGroup(const int direction); + void switchToMapGroup(int filterIndex); + string getCurrentMapFile(); + void loadGameSettings(GameSettings * gameSettings); + void reloadFactions(bool keepExistingSelectedItem, string scenario); + void PlayNow(bool saveGame); + bool isHeadlessAdmin(); + void broadCastGameSettingsToHeadlessServer(bool forceNow); + void updateResourceMultiplier(const int index); + + void RestoreLastGameSettings(); + void setupUIFromGameSettings(GameSettings * gameSettings, + bool errorOnMissingData); + + int setupMapList(string scenario); + int setupTechList(string scenario, bool forceLoad = false); + void setupTilesetList(string scenario); + + void loadScenarioInfo(string file, ScenarioInfo * scenarioInfo); + void initFactionPreview(const GameSettings * gameSettings); + + virtual void simpleTask(BaseThread * callingThread, void *userdata); + string refreshTilesetModInfo(string tilesetInfo); + string refreshTechModInfo(string techInfo); + string refreshMapModInfo(string mapInfo); + string getMapCRC(string mapName); + + void disconnectFromServer(); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 7cbfc2295..7a189625f 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -47,7072 +47,6279 @@ #include "gen_uuid.h" #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - using namespace::Shared::Util; - - const int MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS = 15; - static const char *SAVED_GAME_FILENAME = "lastCustomGameSettings.mgg"; - static const char *DEFAULT_GAME_FILENAME = "data/defaultGameSetup.mgg"; - static const char *DEFAULT_NETWORKGAME_FILENAME = - "data/defaultNetworkGameSetup.mgg"; - - const int mapPreviewTexture_X = 5; - const int mapPreviewTexture_Y = 185; - const int mapPreviewTexture_W = 150; - const int mapPreviewTexture_H = 150; - - struct FormatString - { - void operator () (string & s) - { - s = formatString (s); - } - }; - -// ===================================================== -// class MenuStateCustomGame -// ===================================================== - enum THREAD_NOTIFIER_TYPE - { - tnt_MASTERSERVER = 1, - tnt_CLIENTS = 2 - }; - - MenuStateCustomGame::MenuStateCustomGame (Program * program, - MainMenu * mainMenu, - bool openNetworkSlots, - ParentMenuState - parentMenuState, - bool autostart, - GameSettings * settings, - bool masterserverMode, - string - autoloadScenarioName):MenuState - (program, mainMenu, "new-game") - { - try - { - - this->headlessServerMode = masterserverMode; - if (this->headlessServerMode == true) - { - printf ("Waiting for players to join and start a game...\n"); - } - - this->gameUUID = getUUIDAsString (); - - this->zoomedMap = false; - this->render_mapPreviewTexture_X = mapPreviewTexture_X; - this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; - this->render_mapPreviewTexture_W = mapPreviewTexture_W; - this->render_mapPreviewTexture_H = mapPreviewTexture_H; - - this->lastMasterServerSettingsUpdateCount = 0; - this->masterserverModeMinimalResources = true; - this->parentMenuState = parentMenuState; - this->factionVideo = NULL; - factionVideoSwitchedOffVolume = false; - -//printf("this->masterserverMode = %d [%d]\n",this->masterserverMode,masterserverMode); - - forceWaitForShutdown = true; - this->autostart = autostart; - this->autoStartSettings = settings; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] autostart = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, autostart); - - containerName = "CustomGame"; - activeInputLabel = NULL; - showGeneralError = false; - generalErrorToShow = "---"; - currentFactionLogo = ""; - factionTexture = NULL; - currentTechName_factionPreview = ""; - currentFactionName_factionPreview = ""; - mapPreviewTexture = NULL; - hasCheckedForUPNP = false; - needToPublishDelayed = false; - mapPublishingDelayTimer = time (NULL); - headlessHasConnectedPlayer = false; - - lastCheckedCRCTilesetName = ""; - lastCheckedCRCTechtreeName = ""; - lastCheckedCRCMapName = ""; - - last_Forced_CheckedCRCTilesetName = ""; - last_Forced_CheckedCRCTechtreeName = ""; - last_Forced_CheckedCRCMapName = ""; - - lastCheckedCRCTilesetValue = 0; - lastCheckedCRCTechtreeValue = 0; - lastCheckedCRCMapValue = 0; - - publishToMasterserverThread = NULL; - publishToClientsThread = NULL; - - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - Config & config = Config::getInstance (); - defaultPlayerName = - config.getString ("NetPlayerName", Socket::getHostName ().c_str ()); - enableFactionTexturePreview = - config.getBool ("FactionPreview", "true"); - enableMapPreview = config.getBool ("MapPreview", "true"); - - showFullConsole = false; - - enableScenarioTexturePreview = - Config::getInstance ().getBool ("EnableScenarioTexturePreview", - "true"); - scenarioLogoTexture = NULL; - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - this->autoloadScenarioName = autoloadScenarioName; - this->dirList = - Config::getInstance ().getPathListForType (ptScenarios); - - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Ok"), 500, 300); - mainMessageBox.setEnabled (false); - mainMessageBoxState = 0; - -//initialize network interface - NetworkManager::getInstance ().end (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - serverInitError = false; - try - { - networkManager.init (nrServer, openNetworkSlots); - } - catch (const std::exception & ex) - { - serverInitError = true; - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nNetwork init error:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showGeneralError = true; - generalErrorToShow = szBuf; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - needToSetChangedGameSettings = false; - needToRepublishToMasterserver = false; - needToBroadcastServerSettings = false; - lastGameSettingsreceivedCount = -1; - showMasterserverError = false; - tMasterserverErrorElapsed = 0; - masterServererErrorToShow = "---"; - lastSetChangedGameSettings = 0; - lastMasterserverPublishing = 0; - lastNetworkPing = 0; - soundConnectionCount = 0; - - techTree.reset (new TechTree (config.getPathListForType (ptTechs))); - - // Some of these values must also be changed to match those in - // menu_state_connected_game.cpp - int labelOffset = 23; - int setupPos = 605; - // mapHeadPos is the placement of the text "map", not the map itself - int mapHeadPos = 310; - int mapPos = mapHeadPos - labelOffset; - int aHeadPos = mapHeadPos - 90; - int aPos = aHeadPos - labelOffset; - int networkHeadPos = 700; - int networkPos = networkHeadPos - labelOffset; - int xoffset = 10; - -//create - int buttonx = 165; - int buttony = mapHeadPos - 150; - -// player status - listBoxPlayerStatus.registerGraphicComponent (containerName, - "listBoxPlayerStatus"); - listBoxPlayerStatus.init (buttonx, buttony, 165); - vector < string > playerStatuses; - playerStatuses.push_back (lang.getString ("PlayerStatusSetup")); - playerStatuses.push_back (lang.getString ("PlayerStatusBeRightBack")); - playerStatuses.push_back (lang.getString ("PlayerStatusReady")); - listBoxPlayerStatus.setItems (playerStatuses); - listBoxPlayerStatus.setSelectedItemIndex (2, true); - listBoxPlayerStatus.setTextColor (Vec3f (0.0f, 1.0f, 0.0f)); - listBoxPlayerStatus.setLighted (false); - listBoxPlayerStatus.setVisible (true); - buttonx += 180; - - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (buttonx, buttony, 125); - buttonx += 132; - - buttonRestoreLastSettings.registerGraphicComponent (containerName, - "buttonRestoreLastSettings"); - buttonRestoreLastSettings.init (buttonx, buttony, 240); - buttonx += 247; - - buttonPlayNow.registerGraphicComponent (containerName, - "buttonPlayNow"); - buttonPlayNow.init (buttonx, buttony, 125); - - labelLocalGameVersion.registerGraphicComponent (containerName, - "labelLocalGameVersion"); - labelLocalGameVersion.init (10, networkHeadPos + labelOffset); - - labelLocalIP.registerGraphicComponent (containerName, "labelLocalIP"); - labelLocalIP.init (360, networkHeadPos + labelOffset); - - string ipText = "none"; - std::vector < std::string > ipList = Socket::getLocalIPAddressList (); - if (ipList.empty () == false) - { - ipText = ""; - for (int idx = 0; idx < (int) ipList.size (); idx++) - { - string ip = ipList[idx]; - if (ipText != "") - { - ipText += ", "; - } - ipText += ip; - } - } - string serverPort = config.getString ("PortServer", - intToStr - (GameConstants:: - serverPort).c_str ()); - string externalPort = - config.getString ("PortExternal", serverPort.c_str ()); - labelLocalIP.setText (lang.getString ("LanIP") + ipText + " ( " + - serverPort + " / " + externalPort + " )"); - ServerSocket::setExternalPort (strToInt (externalPort)); - - if (EndsWith (glestVersionString, "-dev") == false) - { - labelLocalGameVersion.setText (glestVersionString); - } - else - { -//labelLocalGameVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); - labelLocalGameVersion.setText (glestVersionString); - } - - xoffset = 65; -// MapFilter - labelMapFilter.registerGraphicComponent (containerName, - "labelMapFilter"); - labelMapFilter.init (xoffset + 325, mapHeadPos); - labelMapFilter.setText (lang.getString ("MapFilter")); - - listBoxMapFilter.registerGraphicComponent (containerName, - "listBoxMapFilter"); - listBoxMapFilter.init (xoffset + 325, mapPos, 80); - listBoxMapFilter.pushBackItem ("-"); - for (int i = 1; i < GameConstants::maxPlayers + 1; ++i) - { - listBoxMapFilter.pushBackItem (intToStr (i)); - } - listBoxMapFilter.setSelectedItemIndex (0); - -// Map - labelMap.registerGraphicComponent (containerName, "labelMap"); - labelMap.init (xoffset + 100, mapHeadPos); - labelMap.setText (lang.getString ("Map")); - -//map listBox - listBoxMap.registerGraphicComponent (containerName, "listBoxMap"); - listBoxMap.init (xoffset + 100, mapPos, 220); -// put them all in a set, to weed out duplicates (gbm & mgm with same name) -// will also ensure they are alphabetically listed (rather than how the OS provides them) - int initialMapSelection = setupMapList (""); - listBoxMap.setItems (formattedPlayerSortedMaps[0]); - listBoxMap.setSelectedItemIndex (initialMapSelection); - - labelMapInfo.registerGraphicComponent (containerName, "labelMapInfo"); - labelMapInfo.init (xoffset + 100, mapPos - labelOffset - 10, 200, 40); - - labelTileset.registerGraphicComponent (containerName, "labelTileset"); - labelTileset.init (xoffset + 500, mapHeadPos); - labelTileset.setText (lang.getString ("Tileset")); - -//tileset listBox - listBoxTileset.registerGraphicComponent (containerName, - "listBoxTileset"); - listBoxTileset.init (xoffset + 500, mapPos, 160); - - setupTilesetList (""); - Chrono seed (true); - srand ((unsigned int) seed.getCurTicks ()); - - listBoxTileset.setSelectedItemIndex (rand () % - listBoxTileset.getItemCount ()); - -//tech Tree listBox - int initialTechSelection = setupTechList ("", true); - - listBoxTechTree.registerGraphicComponent (containerName, - "listBoxTechTree"); - listBoxTechTree.init (xoffset + 700, mapPos, 180); - if (listBoxTechTree.getItemCount () > 0) - { - listBoxTechTree.setSelectedItemIndex (initialTechSelection); - } - - labelTechTree.registerGraphicComponent (containerName, - "labelTechTree"); - labelTechTree.init (xoffset + 700, mapHeadPos); - labelTechTree.setText (lang.getString ("TechTree")); - -// fog - o - war -// @350 ? 300 ? - labelFogOfWar.registerGraphicComponent (containerName, - "labelFogOfWar"); - labelFogOfWar.init (xoffset + 100, aHeadPos, 165); - labelFogOfWar.setText (lang.getString ("FogOfWar")); - - listBoxFogOfWar.registerGraphicComponent (containerName, - "listBoxFogOfWar"); - listBoxFogOfWar.init (xoffset + 100, aPos, 165); - listBoxFogOfWar.pushBackItem (lang.getString ("Enabled")); - listBoxFogOfWar.pushBackItem (lang.getString ("Explored")); - listBoxFogOfWar.pushBackItem (lang.getString ("Disabled")); - listBoxFogOfWar.setSelectedItemIndex (0); - -// Allow Observers - labelAllowObservers.registerGraphicComponent (containerName, - "labelAllowObservers"); - labelAllowObservers.init (xoffset + 325, aHeadPos, 80); - labelAllowObservers.setText (lang.getString ("AllowObservers")); - - checkBoxAllowObservers.registerGraphicComponent (containerName, - "checkBoxAllowObservers"); - checkBoxAllowObservers.init (xoffset + 325, aPos); - checkBoxAllowObservers.setValue (checkBoxAllowObservers.getValue ()); - - vector < string > rMultiplier; - for (int i = 0; i < 45; ++i) - { - rMultiplier.push_back (floatToStr (0.5f + 0.1f * i, 1)); - } - - labelFallbackCpuMultiplier.registerGraphicComponent (containerName, - "labelFallbackCpuMultiplier"); - labelFallbackCpuMultiplier.init (xoffset + 500, aHeadPos, 80); - labelFallbackCpuMultiplier.setText (lang.getString - ("FallbackCpuMultiplier")); - - listBoxFallbackCpuMultiplier.registerGraphicComponent (containerName, - "listBoxFallbackCpuMultiplier"); - listBoxFallbackCpuMultiplier.init (xoffset + 500, aPos, 80); - listBoxFallbackCpuMultiplier.setItems (rMultiplier); - listBoxFallbackCpuMultiplier.setSelectedItem ("1.0"); - -// Allow Switch Team Mode - labelEnableSwitchTeamMode.registerGraphicComponent (containerName, - "labelEnableSwitchTeamMode"); - labelEnableSwitchTeamMode.init (xoffset + 325, aHeadPos + 45, 80); - labelEnableSwitchTeamMode.setText (lang.getString - ("EnableSwitchTeamMode")); - - checkBoxEnableSwitchTeamMode.registerGraphicComponent (containerName, - "checkBoxEnableSwitchTeamMode"); - checkBoxEnableSwitchTeamMode.init (xoffset + 325, aPos + 45); - checkBoxEnableSwitchTeamMode.setValue (false); - - labelAISwitchTeamAcceptPercent.registerGraphicComponent - (containerName, "labelAISwitchTeamAcceptPercent"); - labelAISwitchTeamAcceptPercent.init (xoffset + 500, aHeadPos + 45, - 80); - labelAISwitchTeamAcceptPercent.setText (lang.getString - ("AISwitchTeamAcceptPercent")); - - listBoxAISwitchTeamAcceptPercent.registerGraphicComponent - (containerName, "listBoxAISwitchTeamAcceptPercent"); - listBoxAISwitchTeamAcceptPercent.init (xoffset + 500, aPos + 45, 80); - for (int i = 0; i <= 100; i = i + 10) - { - listBoxAISwitchTeamAcceptPercent.pushBackItem (intToStr (i)); - } - listBoxAISwitchTeamAcceptPercent.setSelectedItem (intToStr (30)); - - labelAllowNativeLanguageTechtree.registerGraphicComponent - (containerName, "labelAllowNativeLanguageTechtree"); - labelAllowNativeLanguageTechtree.init (xoffset + 700, aHeadPos + 45); - labelAllowNativeLanguageTechtree.setText (lang.getString - ("AllowNativeLanguageTechtree")); - - checkBoxAllowNativeLanguageTechtree.registerGraphicComponent - (containerName, "checkBoxAllowNativeLanguageTechtree"); - checkBoxAllowNativeLanguageTechtree.init (xoffset + 700, aPos + 45); - checkBoxAllowNativeLanguageTechtree.setValue (false); - -// Network Scenario - int scenarioX = xoffset + 700; - int scenarioY = aPos; - labelScenario.registerGraphicComponent (containerName, - "labelScenario"); - labelScenario.init (scenarioX, aHeadPos); - labelScenario.setText (lang.getString ("Scenario")); - listBoxScenario.registerGraphicComponent (containerName, - "listBoxScenario"); - listBoxScenario.init (scenarioX + 30, scenarioY, 190); - checkBoxScenario.registerGraphicComponent (containerName, - "checkBoxScenario"); - checkBoxScenario.init (scenarioX, scenarioY); - checkBoxScenario.setValue (false); - -//scenario listbox - vector < string > resultsScenarios; - findDirs (dirList, resultsScenarios); -// Filter out only scenarios with no network slots - for (int i = 0; i < (int) resultsScenarios.size (); ++i) - { - string scenario = resultsScenarios[i]; - string file = Scenario::getScenarioPath (dirList, scenario); - - try - { - if (file != "") - { - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, &scenarioInfo, isTutorial); - - bool isNetworkScenario = false; - for (unsigned int j = 0; - isNetworkScenario == false - && j < (unsigned int) GameConstants::maxPlayers; ++j) - { - if (scenarioInfo.factionControls[j] == ctNetwork) - { - isNetworkScenario = true; - } - } - if (isNetworkScenario == true) - { - scenarioFiles.push_back (scenario); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d]\nError loading scenario [%s]:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenario.c_str (), ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - - showGeneralError = true; - generalErrorToShow = szBuf; -//throw megaglest_runtime_error(szBuf); - } - } - resultsScenarios.clear (); - for (int i = 0; i < (int) scenarioFiles.size (); ++i) - { - resultsScenarios.push_back (formatString (scenarioFiles[i])); - } - listBoxScenario.setItems (resultsScenarios); - if (resultsScenarios.empty () == true) - { - checkBoxScenario.setEnabled (false); - } -// Advanced Options - labelAdvanced.registerGraphicComponent (containerName, - "labelAdvanced"); - labelAdvanced.init (scenarioX, 80, 80); - labelAdvanced.setText (lang.getString ("AdvancedGameOptions")); - - checkBoxAdvanced.registerGraphicComponent (containerName, - "checkBoxAdvanced"); - checkBoxAdvanced.init (scenarioX, 80 - labelOffset); - checkBoxAdvanced.setValue (false); - -// network things -// PublishServer - xoffset = 90; - - labelPublishServer.registerGraphicComponent (containerName, - "labelPublishServer"); - labelPublishServer.init (20, networkHeadPos, 100); - labelPublishServer.setText (lang.getString ("PublishServer")); - - checkBoxPublishServer.registerGraphicComponent (containerName, - "checkBoxPublishServer"); - checkBoxPublishServer.init (20, networkPos); - - checkBoxPublishServer.setValue (false); - if ((this->headlessServerMode == true || - (openNetworkSlots == true && parentMenuState != pLanGame)) && - GlobalStaticFlags::isFlagSet (gsft_lan_mode) == false) - { - checkBoxPublishServer.setValue (true); - } - - labelGameName.registerGraphicComponent (containerName, - "labelGameName"); - labelGameName.init (20 + checkBoxPublishServer.getW () + 5, - networkPos, 200); - labelGameName.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelGameName. - setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelGameName.setText (createGameName ()); - labelGameName.setEditable (true); - labelGameName.setMaxEditWidth (20); - labelGameName.setMaxEditRenderWidth (200); - - - bool allowInProgressJoin = - Config::getInstance ().getBool ("EnableJoinInProgressGame", "true"); - labelAllowInGameJoinPlayer.registerGraphicComponent (containerName, - "labelAllowInGameJoinPlayer"); - labelAllowInGameJoinPlayer.init (50, networkPos - 30, 80); - labelAllowInGameJoinPlayer.setText (lang.getString - ("AllowInGameJoinPlayer")); - labelAllowInGameJoinPlayer.setVisible (allowInProgressJoin); - - checkBoxAllowInGameJoinPlayer.registerGraphicComponent (containerName, - "checkBoxAllowInGameJoinPlayer"); - checkBoxAllowInGameJoinPlayer.init (20, networkPos - 30); - checkBoxAllowInGameJoinPlayer.setValue (false); - checkBoxAllowInGameJoinPlayer.setVisible (allowInProgressJoin); - - - labelAllowTeamUnitSharing.registerGraphicComponent (containerName, - "labelAllowTeamUnitSharing"); - labelAllowTeamUnitSharing.init (xoffset + 410, 670, 80); - labelAllowTeamUnitSharing.setText (lang.getString - ("AllowTeamUnitSharing")); - labelAllowTeamUnitSharing.setVisible (true); - - checkBoxAllowTeamUnitSharing.registerGraphicComponent (containerName, - "checkBoxAllowTeamUnitSharing"); - checkBoxAllowTeamUnitSharing.init (xoffset + 612, 670); - checkBoxAllowTeamUnitSharing.setValue (false); - checkBoxAllowTeamUnitSharing.setVisible (true); - - labelAllowTeamResourceSharing.registerGraphicComponent (containerName, - "labelAllowTeamResourceSharing"); - labelAllowTeamResourceSharing.init (xoffset + 410, 640, 80); - labelAllowTeamResourceSharing.setText (lang.getString - ("AllowTeamResourceSharing")); - labelAllowTeamResourceSharing.setVisible (true); - - checkBoxAllowTeamResourceSharing.registerGraphicComponent - (containerName, "checkBoxAllowTeamResourceSharing"); - checkBoxAllowTeamResourceSharing.init (xoffset + 612, 640); - checkBoxAllowTeamResourceSharing.setValue (false); - checkBoxAllowTeamResourceSharing.setVisible (true); - - -// Network Pause for lagged clients - labelNetworkPauseGameForLaggedClients.registerGraphicComponent - (containerName, "labelNetworkPauseGameForLaggedClients"); - labelNetworkPauseGameForLaggedClients.init - (labelAllowTeamResourceSharing.getX (), networkHeadPos, 80); - labelNetworkPauseGameForLaggedClients.setText (lang.getString - ("NetworkPauseGameForLaggedClients")); - - checkBoxNetworkPauseGameForLaggedClients.registerGraphicComponent - (containerName, "checkBoxNetworkPauseGameForLaggedClients"); - checkBoxNetworkPauseGameForLaggedClients.init - (checkBoxAllowTeamResourceSharing.getX (), networkHeadPos); - checkBoxNetworkPauseGameForLaggedClients.setValue (true); - - //list boxes - xoffset = 5; - int rowHeight = 27; - - buttonClearBlockedPlayers.registerGraphicComponent (containerName, - "buttonClearBlockedPlayers"); - buttonClearBlockedPlayers.init (xoffset + 800, - setupPos - 30, - 134 + 2 + 50); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - - labelPlayers[i].registerGraphicComponent (containerName, - "labelPlayers" + - intToStr (i)); - labelPlayers[i].init (xoffset - 1, - setupPos - 30 - i * rowHeight + 2); - labelPlayers[i]. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - labelPlayers[i]. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - - labelPlayerStatus[i].registerGraphicComponent (containerName, - "labelPlayerStatus" - + intToStr (i)); - labelPlayerStatus[i].init (xoffset + 14, - setupPos - 30 - i * rowHeight + 2); - labelPlayerNames[i].registerGraphicComponent (containerName, - "labelPlayerNames" + - intToStr (i)); - labelPlayerNames[i].init (xoffset + 30, - setupPos - 30 - i * rowHeight); - - listBoxControls[i].registerGraphicComponent (containerName, - "listBoxControls" + - intToStr (i)); - listBoxControls[i].init (xoffset + 160, - setupPos - 30 - i * rowHeight, 174); - - buttonBlockPlayers[i].registerGraphicComponent (containerName, - "buttonBlockPlayers" - + intToStr (i)); -//buttonBlockPlayers[i].init(xoffset+355, setupPos-30-i*rowHeight, 70); - buttonBlockPlayers[i].init (xoffset + 185, - setupPos - 30 - i * rowHeight, 124); - buttonBlockPlayers[i].setText (lang.getString ("BlockPlayer")); - buttonBlockPlayers[i]. - setFont (CoreData::getInstance ().getDisplayFontSmall ()); - buttonBlockPlayers[i]. - setFont3D (CoreData::getInstance ().getDisplayFontSmall3D ()); - - listBoxRMultiplier[i].registerGraphicComponent (containerName, - "listBoxRMultiplier" - + intToStr (i)); - listBoxRMultiplier[i].init (xoffset + 336, - setupPos - 30 - i * rowHeight, 70); - - listBoxFactions[i].registerGraphicComponent (containerName, - "listBoxFactions" + - intToStr (i)); - listBoxFactions[i].init (xoffset + 411, - setupPos - 30 - i * rowHeight, 247); - listBoxFactions[i].setLeftControlled (true); - - listBoxTeams[i].registerGraphicComponent (containerName, - "listBoxTeams" + - intToStr (i)); - listBoxTeams[i].init (xoffset + 660, - setupPos - 30 - i * rowHeight, 60); - listBoxTeams[i].setLighted (true); - - labelNetStatus[i].registerGraphicComponent (containerName, - "labelNetStatus" + - intToStr (i)); - labelNetStatus[i].init (xoffset + 726, - setupPos - 30 - i * rowHeight, 60); - labelNetStatus[i]. - setFont (CoreData::getInstance ().getDisplayFontSmall ()); - labelNetStatus[i]. - setFont3D (CoreData::getInstance ().getDisplayFontSmall3D ()); - } - - labelControl.registerGraphicComponent (containerName, "labelControl"); - labelControl.init (xoffset + 160, setupPos, 50, GraphicListBox::defH, - true); - labelControl.setText (lang.getString ("Control")); - - labelRMultiplier.registerGraphicComponent (containerName, - "labelRMultiplier"); - labelRMultiplier.init (xoffset + 310, setupPos, 50, - GraphicListBox::defH, true); - - labelFaction.registerGraphicComponent (containerName, "labelFaction"); - labelFaction.init (xoffset + 411, setupPos, 50, GraphicListBox::defH, - true); - labelFaction.setText (lang.getString ("Faction")); - - labelTeam.registerGraphicComponent (containerName, "labelTeam"); - labelTeam.init (xoffset + 660, setupPos, 50, GraphicListBox::defH, - true); - labelTeam.setText (lang.getString ("Team")); - - labelControl.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelControl.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelRMultiplier.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelRMultiplier. - setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelFaction.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelFaction.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTeam.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTeam.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - -//xoffset=100; - -//texts - buttonClearBlockedPlayers. - setText (lang.getString ("BlockPlayerClear")); - buttonReturn.setText (lang.getString ("Return")); - buttonPlayNow.setText (lang.getString ("PlayNow")); - buttonRestoreLastSettings.setText (lang.getString - ("ReloadLastGameSettings")); - - vector < string > controlItems; - controlItems.push_back (lang.getString ("Closed")); - controlItems.push_back (lang.getString ("CpuEasy")); - controlItems.push_back (lang.getString ("Cpu")); - controlItems.push_back (lang.getString ("CpuUltra")); - controlItems.push_back (lang.getString ("CpuZeta")); - controlItems.push_back (lang.getString ("Network")); - controlItems.push_back (lang.getString ("NetworkUnassigned")); - controlItems.push_back (lang.getString ("Human")); - - if (config.getBool ("EnableNetworkCpu", "false") == true) - { - controlItems.push_back (lang.getString ("NetworkCpuEasy")); - controlItems.push_back (lang.getString ("NetworkCpu")); - controlItems.push_back (lang.getString ("NetworkCpuUltra")); - controlItems.push_back (lang.getString ("NetworkCpuZeta")); - } - - vector < string > teamItems; - for (int i = 1; i <= GameConstants::maxPlayers; ++i) - { - teamItems.push_back (intToStr (i)); - } - for (int i = GameConstants::maxPlayers + 1; - i <= GameConstants::maxPlayers + GameConstants::specialFactions; - ++i) - { - teamItems.push_back (intToStr (i)); - } - - reloadFactions (false, ""); - - if (factionFiles.empty () == true) - { - showGeneralError = true; - generalErrorToShow = - "[#1] There are no factions for the tech tree [" + - techTreeFiles[listBoxTechTree.getSelectedItemIndex ()] + "]"; - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - labelPlayerStatus[i].setText (" "); - labelPlayerStatus[i]. - setTexture (CoreData::getInstance ().getStatusReadyTexture ()); - labelPlayerStatus[i].setH (16); - labelPlayerStatus[i].setW (12); - -//labelPlayers[i].setText(lang.getString("Player")+" "+intToStr(i)); - labelPlayers[i].setText (intToStr (i + 1)); - labelPlayerNames[i].setText ("*"); - labelPlayerNames[i].setMaxEditWidth (16); - labelPlayerNames[i].setMaxEditRenderWidth (127); - - listBoxTeams[i].setItems (teamItems); - listBoxTeams[i].setSelectedItemIndex (i); - lastSelectedTeamIndex[i] = listBoxTeams[i].getSelectedItemIndex (); - - listBoxControls[i].setItems (controlItems); - listBoxRMultiplier[i].setItems (rMultiplier); - listBoxRMultiplier[i].setSelectedItem ("1.0"); - labelNetStatus[i].setText (""); - } - - // I moved this block from loadMapInfo(), and modified it. It was - // setting the slots visible based on the number of hardMaxPlayers - // every time a new map was loaded. Trying it here instead, so the - // labels are made visible only once. Below, we'll be disabling slots - // that exceed hardMaxPlayers - for (int i = 0; i < GameConstants::maxPlayers; i++) - { - labelPlayers[i].setVisible (true); - labelPlayerNames[i].setVisible (true); - listBoxControls[i].setVisible (true); - listBoxFactions[i].setVisible (true); - listBoxTeams[i].setVisible (true); - labelNetStatus[i].setVisible (true); - } - - loadMapInfo (Config::getMapPath (getCurrentMapFile ()), &mapInfo, - true); - labelMapInfo.setText (mapInfo.desc); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//init controllers - if (serverInitError == false) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface == NULL) - { - throw megaglest_runtime_error ("serverInterface == NULL"); - } - if (this->headlessServerMode == true) - { - listBoxControls[0].setSelectedItemIndex (ctNetwork); - updateResourceMultiplier (0); - } - else - { - setSlotHuman (0); - updateResourceMultiplier (0); - } - labelPlayerNames[0].setText (""); - labelPlayerNames[0].setText (getHumanPlayerName ()); - - if (openNetworkSlots == true) - { - for (int i = 1; i < mapInfo.players; ++i) - { - listBoxControls[i].setSelectedItemIndex (ctNetwork); - } - } - else - { - listBoxControls[1].setSelectedItemIndex (ctCpu); - } - updateControllers (); - updateNetworkSlots (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - -// Ensure we have set the gamesettings at least once - GameSettings gameSettings; - loadGameSettings (&gameSettings); - - serverInterface->setGameSettings (&gameSettings, false); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - updateAllResourceMultiplier (); - -// write hint to console: - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - console.addLine (lang.getString ("ToSwitchOffMusicPress") + " - \"" + - configKeys.getString ("ToggleMusic") + "\""); - - chatManager.init (&console, -1, true); - - GraphicComponent::applyAllCustomProperties (containerName); - - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - publishToMasterserverThread = - new SimpleTaskThread (this, 0, 300, false, - (void *) tnt_MASTERSERVER); - publishToMasterserverThread->setUniqueID (mutexOwnerId); - - static string mutexOwnerId2 = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - publishToClientsThread = - new SimpleTaskThread (this, 0, 200, false, (void *) tnt_CLIENTS, - false); - publishToClientsThread->setUniqueID (mutexOwnerId2); - - publishToMasterserverThread->start (); - publishToClientsThread->start (); - - if (openNetworkSlots == true) - { - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - if (fileExists (data_path + DEFAULT_NETWORKGAME_FILENAME) == true) - loadGameSettings (data_path + DEFAULT_NETWORKGAME_FILENAME); - } - else - { - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - - if (fileExists (data_path + DEFAULT_GAME_FILENAME) == true) - loadGameSettings (data_path + DEFAULT_GAME_FILENAME); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - } - - string MenuStateCustomGame::createGameName (string controllingPlayer) - { - Config & config = Config::getInstance (); - string serverTitle = config.getString ("ServerTitle", ""); - - if (serverTitle != "" && controllingPlayer == "") - { - return serverTitle; - } - else if (this->headlessServerMode == true) - { - if (controllingPlayer != "") - { - return controllingPlayer + " controls"; - } - else - { - return "[H] " + defaultPlayerName; - } - } - else - { - string defaultPlayerNameEnd = - defaultPlayerName.substr (defaultPlayerName.size () - 1, 1); - if (defaultPlayerNameEnd == "s") - { - return defaultPlayerName + "' game"; - } - else - { - return defaultPlayerName + "'s game"; - } - } - } - - void MenuStateCustomGame::reloadUI () - { - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); - - console.resetFonts (); - mainMessageBox.init (lang.getString ("Ok"), 500, 300); - - - if (EndsWith (glestVersionString, "-dev") == false) - { - labelLocalGameVersion.setText (glestVersionString); - } - else - { -//labelLocalGameVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); - labelLocalGameVersion.setText (glestVersionString); - } - -//vector teamItems, controlItems, results , rMultiplier; - - string ipText = "none"; - std::vector < std::string > ipList = Socket::getLocalIPAddressList (); - if (ipList.empty () == false) - { - ipText = ""; - for (int idx = 0; idx < (int) ipList.size (); idx++) - { - string ip = ipList[idx]; - if (ipText != "") - { - ipText += ", "; - } - ipText += ip; - } - } - string serverPort = config.getString ("PortServer", - intToStr - (GameConstants:: - serverPort).c_str ()); - string externalPort = - config.getString ("PortExternal", serverPort.c_str ()); - - labelLocalIP.setText (lang.getString ("LanIP") + ipText + " ( " + - serverPort + " / " + externalPort + " )"); - - labelMap.setText (lang.getString ("Map")); - - labelMapFilter.setText (lang.getString ("MapFilter")); - - labelTileset.setText (lang.getString ("Tileset")); - - labelTechTree.setText (lang.getString ("TechTree")); - - labelAllowNativeLanguageTechtree.setText (lang.getString - ("AllowNativeLanguageTechtree")); - - labelFogOfWar.setText (lang.getString ("FogOfWar")); - - std::vector < std::string > listBoxData; - listBoxData.push_back (lang.getString ("Enabled")); - listBoxData.push_back (lang.getString ("Explored")); - listBoxData.push_back (lang.getString ("Disabled")); - listBoxFogOfWar.setItems (listBoxData); - -// Allow Observers - labelAllowObservers.setText (lang.getString ("AllowObservers")); - -// Allow Switch Team Mode - labelEnableSwitchTeamMode. - setText (lang.getString ("EnableSwitchTeamMode")); - - labelAllowInGameJoinPlayer.setText (lang.getString - ("AllowInGameJoinPlayer")); - - labelAllowTeamUnitSharing. - setText (lang.getString ("AllowTeamUnitSharing")); - labelAllowTeamResourceSharing. - setText (lang.getString ("AllowTeamResourceSharing")); - - labelAISwitchTeamAcceptPercent.setText (lang.getString - ("AISwitchTeamAcceptPercent")); - - listBoxData.clear (); - -// Advanced Options - labelAdvanced.setText (lang.getString ("AdvancedGameOptions")); - - labelPublishServer.setText (lang.getString ("PublishServer")); - - labelGameName.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelGameName.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - - labelGameName.setText (createGameName ()); - - labelNetworkPauseGameForLaggedClients.setText (lang.getString - ("NetworkPauseGameForLaggedClients")); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - buttonBlockPlayers[i].setText (lang.getString ("BlockPlayer")); - } - - labelControl.setText (lang.getString ("Control")); - - labelFaction.setText (lang.getString ("Faction")); - - labelTeam.setText (lang.getString ("Team")); - - labelControl.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelControl.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelRMultiplier.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelRMultiplier. - setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelFaction.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelFaction.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTeam.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTeam.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - -//texts - buttonClearBlockedPlayers.setText (lang.getString ("BlockPlayerClear")); - buttonReturn.setText (lang.getString ("Return")); - buttonPlayNow.setText (lang.getString ("PlayNow")); - buttonRestoreLastSettings.setText (lang.getString - ("ReloadLastGameSettings")); - - vector < string > controlItems; - controlItems.push_back (lang.getString ("Closed")); - controlItems.push_back (lang.getString ("CpuEasy")); - controlItems.push_back (lang.getString ("Cpu")); - controlItems.push_back (lang.getString ("CpuUltra")); - controlItems.push_back (lang.getString ("CpuZeta")); - controlItems.push_back (lang.getString ("Network")); - controlItems.push_back (lang.getString ("NetworkUnassigned")); - controlItems.push_back (lang.getString ("Human")); - - if (config.getBool ("EnableNetworkCpu", "false") == true) - { - controlItems.push_back (lang.getString ("NetworkCpuEasy")); - controlItems.push_back (lang.getString ("NetworkCpu")); - controlItems.push_back (lang.getString ("NetworkCpuUltra")); - controlItems.push_back (lang.getString ("NetworkCpuZeta")); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - labelPlayers[i].setText (intToStr (i + 1)); - - listBoxControls[i].setItems (controlItems); - } - - labelFallbackCpuMultiplier.setText (lang.getString - ("FallbackCpuMultiplier")); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - vector < string > playerStatuses; - playerStatuses.push_back (lang.getString ("PlayerStatusSetup")); - playerStatuses.push_back (lang.getString ("PlayerStatusBeRightBack")); - playerStatuses.push_back (lang.getString ("PlayerStatusReady")); - listBoxPlayerStatus.setItems (playerStatuses); - - labelScenario.setText (lang.getString ("Scenario")); - - reloadFactions (true, - (checkBoxScenario.getValue () == - true ? - scenarioFiles[listBoxScenario.getSelectedItemIndex ()] - : "")); - -// write hint to console: - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - console.addLine (lang.getString ("ToSwitchOffMusicPress") + " - \"" + - configKeys.getString ("ToggleMusic") + "\""); - - chatManager.init (&console, -1, true); - - GraphicComponent::reloadFontsForRegisterGraphicComponents - (containerName); - } - - void MenuStateCustomGame::cleanupThread (SimpleTaskThread ** thread) - { -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - - if (thread != NULL && *thread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1 cleanupThread callingThread [%p]\n", *thread); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - SimpleTaskThread *threadPtr = *thread; - int value = threadPtr->getUserdataAsInt (); - THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1. cleanupThread callingThread [%p] value = %d\n", - *thread, value); - - needToBroadcastServerSettings = false; - needToRepublishToMasterserver = false; - lastNetworkPing = time (NULL); - threadPtr->setThreadOwnerValid (false); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1.. cleanupThread callingThread [%p] value = %d\n", - *thread, value); - - if (forceWaitForShutdown == true) - { - time_t elapsed = time (NULL); - threadPtr->signalQuit (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1a cleanupThread callingThread [%p]\n", *thread); - - for (; (threadPtr->canShutdown (false) == false || - threadPtr->getRunningStatus () == true) && - difftime ((long int) time (NULL), elapsed) <= 15;) - { -//sleep(150); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1b cleanupThread callingThread [%p]\n", *thread); - - if (threadPtr->canShutdown (true) == true && - threadPtr->getRunningStatus () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1c cleanupThread callingThread [%p]\n", *thread); - - delete threadPtr; -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1d cleanupThread callingThread [%p]\n", *thread); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown thread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); -//SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - - if (threadType == tnt_MASTERSERVER) - { - threadPtr->setOverrideShutdownTask (shutdownTaskStatic); - } - threadPtr->setDeleteSelfOnExecutionDone (true); - threadPtr->setDeleteAfterExecute (true); -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - } - threadPtr = NULL; -//if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#1e cleanupThread callingThread [%p]\n",*thread); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1f cleanupThread callingThread [%p]\n", *thread); - threadPtr->signalQuit (); - sleep (0); - if (threadPtr->canShutdown (true) == true && - threadPtr->getRunningStatus () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1g cleanupThread callingThread [%p]\n", *thread); - delete threadPtr; -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#1h cleanupThread callingThread [%p]\n", *thread); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown thread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); -//SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - - if (threadType == tnt_MASTERSERVER) - { - threadPtr->setOverrideShutdownTask (shutdownTaskStatic); - } - threadPtr->setDeleteSelfOnExecutionDone (true); - threadPtr->setDeleteAfterExecute (true); -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - } - } - - *thread = NULL; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\n#2 cleanupThread callingThread [%p]\n", *thread); - } -//printf("LINE: %d *thread = %p\n",__LINE__,*thread); - } - - void MenuStateCustomGame::cleanup () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (publishToMasterserverThread) - { -//printf("LINE: %d\n",__LINE__); - cleanupThread (&publishToMasterserverThread); - } - if (publishToClientsThread) - { -//printf("LINE: %d\n",__LINE__); - cleanupThread (&publishToClientsThread); - } - -//printf("LINE: %d\n",__LINE__); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - cleanupMapPreviewTexture (); - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - - if (forceWaitForShutdown == true) - { - NetworkManager::getInstance ().end (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - MenuStateCustomGame::~MenuStateCustomGame () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - cleanup (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - - void MenuStateCustomGame::returnToParentMenu () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - needToBroadcastServerSettings = false; - needToRepublishToMasterserver = false; - lastNetworkPing = time (NULL); - ParentMenuState parentMenuState = this->parentMenuState; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - forceWaitForShutdown = false; - if (parentMenuState == pMasterServer) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - cleanup (); - mainMenu->setState (new MenuStateMasterserver (program, mainMenu)); - } - else if (parentMenuState == pLanGame) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - cleanup (); - mainMenu->setState (new MenuStateJoinGame (program, mainMenu)); - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - cleanup (); - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::mouseClick (int x, int y, - MouseButton mouseButton) - { - if (isMasterserverMode () == true) - { - return; - } - - try - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - int oldListBoxMapfilterIndex = - listBoxMapFilter.getSelectedItemIndex (); - - if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - mainMessageBox.setEnabled (false); - } - } - } - else - { - string advanceToItemStartingWith = ""; - if (::Shared::Platform::Window::isKeyStateModPressed (KMOD_SHIFT) == - true) - { - const wchar_t lastKey =::Shared::Platform:: - Window::extractLastKeyPressed (); -// string helpString = ""; -// helpString = lastKey; -// printf("lastKey = '%s'\n",helpString.c_str()); - advanceToItemStartingWith = lastKey; - } - - if (mapPreviewTexture != NULL) - { -// printf("X: %d Y: %d [%d, %d, %d, %d]\n", -// x, y, -// this->render_mapPreviewTexture_X, this->render_mapPreviewTexture_X + this->render_mapPreviewTexture_W, -// this->render_mapPreviewTexture_Y, this->render_mapPreviewTexture_Y + this->render_mapPreviewTexture_H); - - if (x >= this->render_mapPreviewTexture_X - && x <= - this->render_mapPreviewTexture_X + - this->render_mapPreviewTexture_W - && y >= this->render_mapPreviewTexture_Y - && y <= - this->render_mapPreviewTexture_Y + - this->render_mapPreviewTexture_H) - { - - if (this->render_mapPreviewTexture_X == - mapPreviewTexture_X - && this->render_mapPreviewTexture_Y == - mapPreviewTexture_Y - && this->render_mapPreviewTexture_W == - mapPreviewTexture_W - && this->render_mapPreviewTexture_H == mapPreviewTexture_H) - { - - const Metrics & metrics = Metrics::getInstance (); - - this->render_mapPreviewTexture_X = 0; - this->render_mapPreviewTexture_Y = 0; - this->render_mapPreviewTexture_W = metrics.getVirtualW (); - this->render_mapPreviewTexture_H = metrics.getVirtualH (); - this->zoomedMap = true; - - cleanupMapPreviewTexture (); - } - else - { - this->render_mapPreviewTexture_X = mapPreviewTexture_X; - this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; - this->render_mapPreviewTexture_W = mapPreviewTexture_W; - this->render_mapPreviewTexture_H = mapPreviewTexture_H; - this->zoomedMap = false; - - cleanupMapPreviewTexture (); - } - return; - } - if (this->zoomedMap == true) - { - return; - } - } - - if (activeInputLabel != NULL - && !(activeInputLabel->mouseClick (x, y))) - { - setActiveInputLabel (NULL); - } - if (buttonReturn.mouseClick (x, y) || serverInitError == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - soundRenderer.playFx (coreData.getClickSoundA ()); - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - needToBroadcastServerSettings = false; - needToRepublishToMasterserver = false; - lastNetworkPing = time (NULL); - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - returnToParentMenu (); - return; - } - else if (buttonPlayNow.mouseClick (x, y) - && buttonPlayNow.getEnabled ()) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - PlayNow (true); - return; - } - else if (buttonRestoreLastSettings.mouseClick (x, y) - && buttonRestoreLastSettings.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - - RestoreLastGameSettings (); - } - else if (listBoxMap.mouseClick (x, y, advanceToItemStartingWith)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - getCurrentMapFile ().c_str ()); - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - loadMapInfo (Config::getMapPath (getCurrentMapFile (), "", false), - &mapInfo, true); - labelMapInfo.setText (mapInfo.desc); - updateControllers (); - updateNetworkSlots (); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { -//delay publishing for 5 seconds - needToPublishDelayed = true; - mapPublishingDelayTimer = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && listBoxFogOfWar.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - cleanupMapPreviewTexture (); - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && checkBoxAllowObservers.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - reloadFactions (true, - (checkBoxScenario.getValue () == - true ? - scenarioFiles - [listBoxScenario.getSelectedItemIndex ()] : "")); - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAllowInGameJoinPlayer.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface->setAllowInGameConnections - (checkBoxAllowInGameJoinPlayer.getValue () == true); - } - else if (checkBoxAdvanced.getValue () == 1 - && checkBoxAllowTeamUnitSharing.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && checkBoxAllowTeamResourceSharing.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAllowNativeLanguageTechtree.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && checkBoxEnableSwitchTeamMode.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && listBoxAISwitchTeamAcceptPercent.getEnabled () - && listBoxAISwitchTeamAcceptPercent.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.getValue () == 1 - && listBoxFallbackCpuMultiplier.getEditable () == true - && listBoxFallbackCpuMultiplier.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxAdvanced.mouseClick (x, y)) - { - } - else - if (listBoxTileset.mouseClick (x, y, advanceToItemStartingWith)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - if (hasNetworkGameSettings () == true) - { - -//delay publishing for 5 seconds - needToPublishDelayed = true; - mapPublishingDelayTimer = time (NULL); - } - } - else if (listBoxMapFilter.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - switchToNextMapGroup (listBoxMapFilter.getSelectedItemIndex () - - oldListBoxMapfilterIndex); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s\n", - getCurrentMapFile ().c_str ()); - - loadMapInfo (Config::getMapPath (getCurrentMapFile ()), - &mapInfo, true); - labelMapInfo.setText (mapInfo.desc); - updateControllers (); - updateNetworkSlots (); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else - if (listBoxTechTree.mouseClick (x, y, advanceToItemStartingWith)) - { - reloadFactions (listBoxTechTree.getItemCount () <= 1, - (checkBoxScenario.getValue () == - true ? - scenarioFiles - [listBoxScenario.getSelectedItemIndex ()] : "")); - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (checkBoxPublishServer.mouseClick (x, y) - && checkBoxPublishServer.getEditable ()) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - needToRepublishToMasterserver = true; - soundRenderer.playFx (coreData.getClickSoundC ()); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface-> - setPublishEnabled (checkBoxPublishServer.getValue () == true); - } - else if (labelGameName.mouseClick (x, y) - && checkBoxPublishServer.getEditable ()) - { - setActiveInputLabel (&labelGameName); - } - else if (checkBoxAdvanced.getValue () == 1 - && checkBoxNetworkPauseGameForLaggedClients.mouseClick (x, - y)) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - - soundRenderer.playFx (coreData.getClickSoundC ()); - } - else if (listBoxScenario.mouseClick (x, y) - || checkBoxScenario.mouseClick (x, y)) - { - processScenario (); - } - else - { - for (int i = 0; i < mapInfo.players; ++i) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - -// set multiplier - if (listBoxRMultiplier[i].mouseClick (x, y)) - { -//printf("Line: %d multiplier index: %d i: %d itemcount: %d\n",__LINE__,listBoxRMultiplier[i].getSelectedItemIndex(),i,listBoxRMultiplier[i].getItemCount()); - -//for(int indexData = 0; indexData < listBoxRMultiplier[i].getItemCount(); ++indexData) { -//string item = listBoxRMultiplier[i].getItem(indexData); - -//printf("Item index: %d value: %s\n",indexData,item.c_str()); -//} - } - -//ensure thet only 1 human player is present - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = serverInterface->getSlot (i, true); - - bool checkControTypeClicked = false; - int selectedControlItemIndex = - listBoxControls[i].getSelectedItemIndex (); - if (selectedControlItemIndex != ctNetwork - || (selectedControlItemIndex == ctNetwork - && (slot == NULL || slot->isConnected () == false))) - { - checkControTypeClicked = true; - } - -//printf("checkControTypeClicked = %d selectedControlItemIndex = %d i = %d\n",checkControTypeClicked,selectedControlItemIndex,i); - - if (selectedControlItemIndex != ctHuman && - checkControTypeClicked == true && - listBoxControls[i].mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - ControlType currentControlType = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - int slotsToChangeStart = i; - int slotsToChangeEnd = i; -// If control is pressed while changing player types then change all other slots to same type - if (::Shared::Platform:: - Window::isKeyStateModPressed (KMOD_CTRL) == true - && currentControlType != ctHuman) - { - slotsToChangeStart = 0; - slotsToChangeEnd = mapInfo.players - 1; - } - - for (int index = slotsToChangeStart; - index <= slotsToChangeEnd; ++index) - { - if (index != i - && static_cast < ControlType > - (listBoxControls[index].getSelectedItemIndex ()) != - ctHuman) - { - listBoxControls[index].setSelectedItemIndex - (listBoxControls[i].getSelectedItemIndex ()); - } -// Skip over networkunassigned - if (listBoxControls[index].getSelectedItemIndex () == - ctNetworkUnassigned - && selectedControlItemIndex != ctNetworkUnassigned) - { - listBoxControls[index].mouseClick (x, y); - } - -//look for human players - int humanIndex1 = -1; - int humanIndex2 = -1; - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[j].getSelectedItemIndex ()); - if (ct == ctHuman) - { - if (humanIndex1 == -1) - { - humanIndex1 = j; - } - else - { - humanIndex2 = j; - } - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] humanIndex1 = %d, humanIndex2 = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - humanIndex1, humanIndex2); - -//no human - if (humanIndex1 == -1 && humanIndex2 == -1) - { - setSlotHuman (index); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] i = %d, labelPlayerNames[i].getText() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, index, - labelPlayerNames - [index].getText ().c_str ()); - -//printf("humanIndex1 = %d humanIndex2 = %d i = %d listBoxControls[i].getSelectedItemIndex() = %d\n",humanIndex1,humanIndex2,i,listBoxControls[i].getSelectedItemIndex()); - } -//2 humans - else if (humanIndex1 != -1 && humanIndex2 != -1) - { - int closeSlotIndex = - (humanIndex1 == index ? humanIndex2 : humanIndex1); - int humanSlotIndex = - (closeSlotIndex == - humanIndex1 ? humanIndex2 : humanIndex1); - - string origPlayName = - labelPlayerNames[closeSlotIndex].getText (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] closeSlotIndex = %d, origPlayName [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - closeSlotIndex, - origPlayName.c_str ()); - - listBoxControls[closeSlotIndex].setSelectedItemIndex - (ctClosed); - setSlotHuman (humanSlotIndex); - labelPlayerNames[humanSlotIndex].setText ((origPlayName != - "" ? - origPlayName : - getHumanPlayerName - ())); - } - updateNetworkSlots (); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - updateResourceMultiplier (index); - } - } - else if (buttonClearBlockedPlayers.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface != NULL) - { - ServerSocket *serverSocket = - serverInterface->getServerSocket (); - if (serverSocket != NULL) - { - serverSocket->clearBlockedIPAddress (); - } - } - } - else if (buttonBlockPlayers[i].mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface != NULL) - { - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, true)->isConnected ()) - { - - ServerSocket *serverSocket = - serverInterface->getServerSocket (); - if (serverSocket != NULL) - { - serverSocket->addIPAddressToBlockedList - (serverInterface->getSlot (i, true)->getIpAddress ()); - - Lang & lang = Lang::getInstance (); - const vector < string > languageList = - serverInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages - (); - for (unsigned int j = 0; j < languageList.size (); ++j) - { - char szMsg[8096] = ""; - if (lang.hasString ("BlockPlayerServerMsg", - languageList[j]) == true) - { - snprintf (szMsg, 8096, - lang.getString - ("BlockPlayerServerMsg", - languageList[j]).c_str (), - serverInterface->getSlot (i, - true)->getIpAddress - ().c_str ()); - } - else - { - snprintf (szMsg, 8096, - "The server has temporarily blocked IP Address [%s] from this game.", - serverInterface->getSlot (i, - true)->getIpAddress - ().c_str ()); - } - - serverInterface->sendTextMessage (szMsg, -1, true, - languageList[j]); - } - sleep (1); - serverInterface->getSlot (i, true)->close (); - } - } - } - } - else - if (listBoxFactions[i].mouseClick - (x, y, advanceToItemStartingWith)) - { -// Disallow CPU players to be observers - if (factionFiles - [listBoxFactions[i].getSelectedItemIndex ()] == - formatString (GameConstants::OBSERVER_SLOTNAME) - && (listBoxControls[i].getSelectedItemIndex () == - ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta)) - { - listBoxFactions[i].setSelectedItemIndex (0); - } -// - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - else if (listBoxTeams[i].mouseClick (x, y)) - { - if (factionFiles - [listBoxFactions[i].getSelectedItemIndex ()] != - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - if (listBoxTeams[i].getSelectedItemIndex () + 1 != - (GameConstants::maxPlayers + fpt_Observer)) - { - lastSelectedTeamIndex[i] = - listBoxTeams[i].getSelectedItemIndex (); - } - } - else - { - lastSelectedTeamIndex[i] = -1; - } - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL);; - } - } - else if (labelPlayerNames[i].mouseClick (x, y)) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct == ctHuman) - { - setActiveInputLabel (&labelPlayerNames[i]); - break; - } - } - } - } - } - - if (hasNetworkGameSettings () == true - && listBoxPlayerStatus.mouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - soundRenderer.playFx (coreData.getClickSoundC ()); - if (getNetworkPlayerStatus () == npst_PickSettings) - { - listBoxPlayerStatus.setTextColor (Vec3f (1.0f, 0.0f, 0.0f)); - listBoxPlayerStatus.setLighted (true); - } - else if (getNetworkPlayerStatus () == npst_BeRightBack) - { - listBoxPlayerStatus.setTextColor (Vec3f (1.0f, 1.0f, 0.0f)); - listBoxPlayerStatus.setLighted (true); - } - else if (getNetworkPlayerStatus () == npst_Ready) - { - listBoxPlayerStatus.setTextColor (Vec3f (0.0f, 1.0f, 0.0f)); - listBoxPlayerStatus.setLighted (false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showGeneralError = true; - generalErrorToShow = szBuf; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::loadGameSettings (const std::string & fileName) - { -// Ensure we have set the gamesettings at least once - GameSettings gameSettings = loadGameSettingsFromFile (fileName); - if (gameSettings.getMap () == "") - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - loadGameSettings (&gameSettings); - } - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface->setGameSettings (&gameSettings, false); - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor () - : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - - void MenuStateCustomGame::RestoreLastGameSettings () - { - loadGameSettings (SAVED_GAME_FILENAME); - } - - bool MenuStateCustomGame::checkNetworkPlayerDataSynch (bool checkMapCRC, - bool - checkTileSetCRC, - bool - checkTechTreeCRC) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - - bool dataSynchCheckOk = true; - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork) - { - - MutexSafeWrapper safeMutex (serverInterface->getSlotMutex (i), - CODE_AT_LINE); - ConnectionSlot *slot = serverInterface->getSlot (i, false); - if (slot != NULL && slot->isConnected () && - (slot->getAllowDownloadDataSynch () == true || - slot->getAllowGameDataSynchCheck () == true)) - { - - if (checkMapCRC == true && - slot->getNetworkGameDataSynchCheckOkMap () == false) - { - dataSynchCheckOk = false; - break; - } - if (checkTileSetCRC == true && - slot->getNetworkGameDataSynchCheckOkTile () == false) - { - dataSynchCheckOk = false; - break; - } - if (checkTechTreeCRC == true && - slot->getNetworkGameDataSynchCheckOkTech () == false) - { - dataSynchCheckOk = false; - break; - } - } - } - } - - return dataSynchCheckOk; - } - - void MenuStateCustomGame::PlayNow (bool saveGame) - { - if (listBoxTechTree.getItemCount () <= 0) - { - mainMessageBoxState = 1; - - char szMsg[8096] = ""; - strcpy (szMsg, "Cannot start game.\nThere are no tech-trees!\n"); - printf ("%s", szMsg); - - showMessageBox (szMsg, "", false); - return; - } - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor () - : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (saveGame == true) - { - saveGameSettingsToFile (SAVED_GAME_FILENAME); - } - - forceWaitForShutdown = false; - closeUnusedSlots (); - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.playFx (coreData.getClickSoundC ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - std::vector < string > randomFactionSelectionList; - int RandomCount = 0; - for (int i = 0; i < mapInfo.players; ++i) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// Check for random faction selection and choose the faction now - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - if (listBoxFactions[i].getSelectedItem () == - formatString (GameConstants::RANDOMFACTION_SLOTNAME) - && listBoxFactions[i].getItemCount () > 1) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] i = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i); - -// Max 1000 tries to get a random, unused faction - for (int findRandomFaction = 1; findRandomFaction < 1000; - ++findRandomFaction) - { - Chrono seed (true); - srand ((unsigned int) seed.getCurTicks () + findRandomFaction); - - int selectedFactionIndex = - rand () % listBoxFactions[i].getItemCount (); - string selectedFactionName = - listBoxFactions[i].getItem (selectedFactionIndex); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] selectedFactionName [%s] selectedFactionIndex = %d, findRandomFaction = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - selectedFactionName.c_str (), - selectedFactionIndex, - findRandomFaction); - - if (selectedFactionName != - formatString (GameConstants::RANDOMFACTION_SLOTNAME) - && selectedFactionName != - formatString (GameConstants::OBSERVER_SLOTNAME) - && std::find (randomFactionSelectionList.begin (), - randomFactionSelectionList.end (), - selectedFactionName) == - randomFactionSelectionList.end ()) - { - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - listBoxFactions[i].setSelectedItem (selectedFactionName); - randomFactionSelectionList.push_back (selectedFactionName); - break; - } - } - - if (listBoxFactions[i].getSelectedItem () == - formatString (GameConstants::RANDOMFACTION_SLOTNAME)) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] RandomCount = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, RandomCount); - -// Find first real faction and use it - int factionIndexToUse = RandomCount; - for (int useIdx = 0; - useIdx < listBoxFactions[i].getItemCount (); useIdx++) - { - string selectedFactionName = - listBoxFactions[i].getItem (useIdx); - if (selectedFactionName != - formatString (GameConstants::RANDOMFACTION_SLOTNAME) - && selectedFactionName != - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - factionIndexToUse = useIdx; - break; - } - } - listBoxFactions[i].setSelectedItemIndex (factionIndexToUse); - randomFactionSelectionList.push_back (listBoxFactions[i].getItem - (factionIndexToUse)); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] i = %d, listBoxFactions[i].getSelectedItem() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - listBoxFactions[i].getSelectedItem (). - c_str ()); - - RandomCount++; - } - } - } - - if (RandomCount > 0) - { - needToSetChangedGameSettings = true; - } - - safeMutex.ReleaseLock (true); - safeMutexCLI.ReleaseLock (true); - GameSettings gameSettings; - loadGameSettings (&gameSettings, true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - -// Send the game settings to each client if we have at least one networked client - safeMutex.Lock (); - safeMutexCLI.Lock (); - - bool dataSynchCheckOk = checkNetworkPlayerDataSynch (true, true, true); - -// Ensure we have no dangling network players - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == ctNetworkUnassigned) - { - mainMessageBoxState = 1; - - Lang & lang = Lang::getInstance (); - string sMsg = ""; - if (lang.hasString ("NetworkSlotUnassignedErrorUI") == true) - { - sMsg = lang.getString ("NetworkSlotUnassignedErrorUI"); - } - else - { - sMsg = - "Cannot start game.\nSome player(s) are not in a network game slot!"; - } - - showMessageBox (sMsg, "", false); - - const vector < string > languageList = - serverInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int j = 0; j < languageList.size (); ++j) - { - char szMsg[8096] = ""; - if (lang.hasString ("NetworkSlotUnassignedError", - languageList[j]) == true) - { - string msg_string = - lang.getString ("NetworkSlotUnassignedError"); +namespace Glest { + namespace Game { + + using namespace::Shared::Util; + + const int MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS = 15; + static const char *SAVED_GAME_FILENAME = "lastCustomGameSettings.mgg"; + static const char *DEFAULT_GAME_FILENAME = "data/defaultGameSetup.mgg"; + static const char *DEFAULT_NETWORKGAME_FILENAME = + "data/defaultNetworkGameSetup.mgg"; + + const int mapPreviewTexture_X = 5; + const int mapPreviewTexture_Y = 185; + const int mapPreviewTexture_W = 150; + const int mapPreviewTexture_H = 150; + + struct FormatString { + void operator () (string & s) { + s = formatString(s); + } + }; + + // ===================================================== + // class MenuStateCustomGame + // ===================================================== + enum THREAD_NOTIFIER_TYPE { + tnt_MASTERSERVER = 1, + tnt_CLIENTS = 2 + }; + + MenuStateCustomGame::MenuStateCustomGame(Program * program, + MainMenu * mainMenu, + bool openNetworkSlots, + ParentMenuState + parentMenuState, + bool autostart, + GameSettings * settings, + bool masterserverMode, + string + autoloadScenarioName) :MenuState + (program, mainMenu, "new-game") { + try { + + this->headlessServerMode = masterserverMode; + if (this->headlessServerMode == true) { + printf("Waiting for players to join and start a game...\n"); + } + + this->gameUUID = getUUIDAsString(); + + this->zoomedMap = false; + this->render_mapPreviewTexture_X = mapPreviewTexture_X; + this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; + this->render_mapPreviewTexture_W = mapPreviewTexture_W; + this->render_mapPreviewTexture_H = mapPreviewTexture_H; + + this->lastMasterServerSettingsUpdateCount = 0; + this->masterserverModeMinimalResources = true; + this->parentMenuState = parentMenuState; + this->factionVideo = NULL; + factionVideoSwitchedOffVolume = false; + + //printf("this->masterserverMode = %d [%d]\n",this->masterserverMode,masterserverMode); + + forceWaitForShutdown = true; + this->autostart = autostart; + this->autoStartSettings = settings; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] autostart = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, autostart); + + containerName = "CustomGame"; + activeInputLabel = NULL; + showGeneralError = false; + generalErrorToShow = "---"; + currentFactionLogo = ""; + factionTexture = NULL; + currentTechName_factionPreview = ""; + currentFactionName_factionPreview = ""; + mapPreviewTexture = NULL; + hasCheckedForUPNP = false; + needToPublishDelayed = false; + mapPublishingDelayTimer = time(NULL); + headlessHasConnectedPlayer = false; + + lastCheckedCRCTilesetName = ""; + lastCheckedCRCTechtreeName = ""; + lastCheckedCRCMapName = ""; + + last_Forced_CheckedCRCTilesetName = ""; + last_Forced_CheckedCRCTechtreeName = ""; + last_Forced_CheckedCRCMapName = ""; + + lastCheckedCRCTilesetValue = 0; + lastCheckedCRCTechtreeValue = 0; + lastCheckedCRCMapValue = 0; + + publishToMasterserverThread = NULL; + publishToClientsThread = NULL; + + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + Config & config = Config::getInstance(); + defaultPlayerName = + config.getString("NetPlayerName", Socket::getHostName().c_str()); + enableFactionTexturePreview = + config.getBool("FactionPreview", "true"); + enableMapPreview = config.getBool("MapPreview", "true"); + + showFullConsole = false; + + enableScenarioTexturePreview = + Config::getInstance().getBool("EnableScenarioTexturePreview", + "true"); + scenarioLogoTexture = NULL; + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + this->autoloadScenarioName = autoloadScenarioName; + this->dirList = + Config::getInstance().getPathListForType(ptScenarios); + + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Ok"), 500, 300); + mainMessageBox.setEnabled(false); + mainMessageBoxState = 0; + + //initialize network interface + NetworkManager::getInstance().end(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + serverInitError = false; + try { + networkManager.init(nrServer, openNetworkSlots); + } catch (const std::exception & ex) { + serverInitError = true; + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nNetwork init error:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showGeneralError = true; + generalErrorToShow = szBuf; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + needToSetChangedGameSettings = false; + needToRepublishToMasterserver = false; + needToBroadcastServerSettings = false; + lastGameSettingsreceivedCount = -1; + showMasterserverError = false; + tMasterserverErrorElapsed = 0; + masterServererErrorToShow = "---"; + lastSetChangedGameSettings = 0; + lastMasterserverPublishing = 0; + lastNetworkPing = 0; + soundConnectionCount = 0; + + techTree.reset(new TechTree(config.getPathListForType(ptTechs))); + + // Some of these values must also be changed to match those in + // menu_state_connected_game.cpp + int labelOffset = 23; + int setupPos = 605; + // mapHeadPos is the placement of the text "map", not the map itself + int mapHeadPos = 310; + int mapPos = mapHeadPos - labelOffset; + int aHeadPos = mapHeadPos - 90; + int aPos = aHeadPos - labelOffset; + int networkHeadPos = 700; + int networkPos = networkHeadPos - labelOffset; + int xoffset = 10; + + //create + int buttonx = 165; + int buttony = mapHeadPos - 150; + + // player status + listBoxPlayerStatus.registerGraphicComponent(containerName, + "listBoxPlayerStatus"); + listBoxPlayerStatus.init(buttonx, buttony, 165); + vector < string > playerStatuses; + playerStatuses.push_back(lang.getString("PlayerStatusSetup")); + playerStatuses.push_back(lang.getString("PlayerStatusBeRightBack")); + playerStatuses.push_back(lang.getString("PlayerStatusReady")); + listBoxPlayerStatus.setItems(playerStatuses); + listBoxPlayerStatus.setSelectedItemIndex(2, true); + listBoxPlayerStatus.setTextColor(Vec3f(0.0f, 1.0f, 0.0f)); + listBoxPlayerStatus.setLighted(false); + listBoxPlayerStatus.setVisible(true); + buttonx += 180; + + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(buttonx, buttony, 125); + buttonx += 132; + + buttonRestoreLastSettings.registerGraphicComponent(containerName, + "buttonRestoreLastSettings"); + buttonRestoreLastSettings.init(buttonx, buttony, 240); + buttonx += 247; + + buttonPlayNow.registerGraphicComponent(containerName, + "buttonPlayNow"); + buttonPlayNow.init(buttonx, buttony, 125); + + labelLocalGameVersion.registerGraphicComponent(containerName, + "labelLocalGameVersion"); + labelLocalGameVersion.init(10, networkHeadPos + labelOffset); + + labelLocalIP.registerGraphicComponent(containerName, "labelLocalIP"); + labelLocalIP.init(360, networkHeadPos + labelOffset); + + string ipText = "none"; + std::vector < std::string > ipList = Socket::getLocalIPAddressList(); + if (ipList.empty() == false) { + ipText = ""; + for (int idx = 0; idx < (int) ipList.size(); idx++) { + string ip = ipList[idx]; + if (ipText != "") { + ipText += ", "; + } + ipText += ip; + } + } + string serverPort = config.getString("PortServer", + intToStr + (GameConstants:: + serverPort).c_str()); + string externalPort = + config.getString("PortExternal", serverPort.c_str()); + labelLocalIP.setText(lang.getString("LanIP") + ipText + " ( " + + serverPort + " / " + externalPort + " )"); + ServerSocket::setExternalPort(strToInt(externalPort)); + + if (EndsWith(glestVersionString, "-dev") == false) { + labelLocalGameVersion.setText(glestVersionString); + } else { + //labelLocalGameVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); + labelLocalGameVersion.setText(glestVersionString); + } + + xoffset = 65; + // MapFilter + labelMapFilter.registerGraphicComponent(containerName, + "labelMapFilter"); + labelMapFilter.init(xoffset + 325, mapHeadPos); + labelMapFilter.setText(lang.getString("MapFilter")); + + listBoxMapFilter.registerGraphicComponent(containerName, + "listBoxMapFilter"); + listBoxMapFilter.init(xoffset + 325, mapPos, 80); + listBoxMapFilter.pushBackItem("-"); + for (int i = 1; i < GameConstants::maxPlayers + 1; ++i) { + listBoxMapFilter.pushBackItem(intToStr(i)); + } + listBoxMapFilter.setSelectedItemIndex(0); + + // Map + labelMap.registerGraphicComponent(containerName, "labelMap"); + labelMap.init(xoffset + 100, mapHeadPos); + labelMap.setText(lang.getString("Map")); + + //map listBox + listBoxMap.registerGraphicComponent(containerName, "listBoxMap"); + listBoxMap.init(xoffset + 100, mapPos, 220); + // put them all in a set, to weed out duplicates (gbm & mgm with same name) + // will also ensure they are alphabetically listed (rather than how the OS provides them) + int initialMapSelection = setupMapList(""); + listBoxMap.setItems(formattedPlayerSortedMaps[0]); + listBoxMap.setSelectedItemIndex(initialMapSelection); + + labelMapInfo.registerGraphicComponent(containerName, "labelMapInfo"); + labelMapInfo.init(xoffset + 100, mapPos - labelOffset - 10, 200, 40); + + labelTileset.registerGraphicComponent(containerName, "labelTileset"); + labelTileset.init(xoffset + 500, mapHeadPos); + labelTileset.setText(lang.getString("Tileset")); + + //tileset listBox + listBoxTileset.registerGraphicComponent(containerName, + "listBoxTileset"); + listBoxTileset.init(xoffset + 500, mapPos, 160); + + setupTilesetList(""); + Chrono seed(true); + srand((unsigned int) seed.getCurTicks()); + + listBoxTileset.setSelectedItemIndex(rand() % + listBoxTileset.getItemCount()); + + //tech Tree listBox + int initialTechSelection = setupTechList("", true); + + listBoxTechTree.registerGraphicComponent(containerName, + "listBoxTechTree"); + listBoxTechTree.init(xoffset + 700, mapPos, 180); + if (listBoxTechTree.getItemCount() > 0) { + listBoxTechTree.setSelectedItemIndex(initialTechSelection); + } + + labelTechTree.registerGraphicComponent(containerName, + "labelTechTree"); + labelTechTree.init(xoffset + 700, mapHeadPos); + labelTechTree.setText(lang.getString("TechTree")); + + // fog - o - war + // @350 ? 300 ? + labelFogOfWar.registerGraphicComponent(containerName, + "labelFogOfWar"); + labelFogOfWar.init(xoffset + 100, aHeadPos, 165); + labelFogOfWar.setText(lang.getString("FogOfWar")); + + listBoxFogOfWar.registerGraphicComponent(containerName, + "listBoxFogOfWar"); + listBoxFogOfWar.init(xoffset + 100, aPos, 165); + listBoxFogOfWar.pushBackItem(lang.getString("Enabled")); + listBoxFogOfWar.pushBackItem(lang.getString("Explored")); + listBoxFogOfWar.pushBackItem(lang.getString("Disabled")); + listBoxFogOfWar.setSelectedItemIndex(0); + + // Allow Observers + labelAllowObservers.registerGraphicComponent(containerName, + "labelAllowObservers"); + labelAllowObservers.init(xoffset + 325, aHeadPos, 80); + labelAllowObservers.setText(lang.getString("AllowObservers")); + + checkBoxAllowObservers.registerGraphicComponent(containerName, + "checkBoxAllowObservers"); + checkBoxAllowObservers.init(xoffset + 325, aPos); + checkBoxAllowObservers.setValue(checkBoxAllowObservers.getValue()); + + vector < string > rMultiplier; + for (int i = 0; i < 45; ++i) { + rMultiplier.push_back(floatToStr(0.5f + 0.1f * i, 1)); + } + + labelFallbackCpuMultiplier.registerGraphicComponent(containerName, + "labelFallbackCpuMultiplier"); + labelFallbackCpuMultiplier.init(xoffset + 500, aHeadPos, 80); + labelFallbackCpuMultiplier.setText(lang.getString + ("FallbackCpuMultiplier")); + + listBoxFallbackCpuMultiplier.registerGraphicComponent(containerName, + "listBoxFallbackCpuMultiplier"); + listBoxFallbackCpuMultiplier.init(xoffset + 500, aPos, 80); + listBoxFallbackCpuMultiplier.setItems(rMultiplier); + listBoxFallbackCpuMultiplier.setSelectedItem("1.0"); + + // Allow Switch Team Mode + labelEnableSwitchTeamMode.registerGraphicComponent(containerName, + "labelEnableSwitchTeamMode"); + labelEnableSwitchTeamMode.init(xoffset + 325, aHeadPos + 45, 80); + labelEnableSwitchTeamMode.setText(lang.getString + ("EnableSwitchTeamMode")); + + checkBoxEnableSwitchTeamMode.registerGraphicComponent(containerName, + "checkBoxEnableSwitchTeamMode"); + checkBoxEnableSwitchTeamMode.init(xoffset + 325, aPos + 45); + checkBoxEnableSwitchTeamMode.setValue(false); + + labelAISwitchTeamAcceptPercent.registerGraphicComponent + (containerName, "labelAISwitchTeamAcceptPercent"); + labelAISwitchTeamAcceptPercent.init(xoffset + 500, aHeadPos + 45, + 80); + labelAISwitchTeamAcceptPercent.setText(lang.getString + ("AISwitchTeamAcceptPercent")); + + listBoxAISwitchTeamAcceptPercent.registerGraphicComponent + (containerName, "listBoxAISwitchTeamAcceptPercent"); + listBoxAISwitchTeamAcceptPercent.init(xoffset + 500, aPos + 45, 80); + for (int i = 0; i <= 100; i = i + 10) { + listBoxAISwitchTeamAcceptPercent.pushBackItem(intToStr(i)); + } + listBoxAISwitchTeamAcceptPercent.setSelectedItem(intToStr(30)); + + labelAllowNativeLanguageTechtree.registerGraphicComponent + (containerName, "labelAllowNativeLanguageTechtree"); + labelAllowNativeLanguageTechtree.init(xoffset + 700, aHeadPos + 45); + labelAllowNativeLanguageTechtree.setText(lang.getString + ("AllowNativeLanguageTechtree")); + + checkBoxAllowNativeLanguageTechtree.registerGraphicComponent + (containerName, "checkBoxAllowNativeLanguageTechtree"); + checkBoxAllowNativeLanguageTechtree.init(xoffset + 700, aPos + 45); + checkBoxAllowNativeLanguageTechtree.setValue(false); + + // Network Scenario + int scenarioX = xoffset + 700; + int scenarioY = aPos; + labelScenario.registerGraphicComponent(containerName, + "labelScenario"); + labelScenario.init(scenarioX, aHeadPos); + labelScenario.setText(lang.getString("Scenario")); + listBoxScenario.registerGraphicComponent(containerName, + "listBoxScenario"); + listBoxScenario.init(scenarioX + 30, scenarioY, 190); + checkBoxScenario.registerGraphicComponent(containerName, + "checkBoxScenario"); + checkBoxScenario.init(scenarioX, scenarioY); + checkBoxScenario.setValue(false); + + //scenario listbox + vector < string > resultsScenarios; + findDirs(dirList, resultsScenarios); + // Filter out only scenarios with no network slots + for (int i = 0; i < (int) resultsScenarios.size(); ++i) { + string scenario = resultsScenarios[i]; + string file = Scenario::getScenarioPath(dirList, scenario); + + try { + if (file != "") { + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, &scenarioInfo, isTutorial); + + bool isNetworkScenario = false; + for (unsigned int j = 0; + isNetworkScenario == false + && j < (unsigned int) GameConstants::maxPlayers; ++j) { + if (scenarioInfo.factionControls[j] == ctNetwork) { + isNetworkScenario = true; + } + } + if (isNetworkScenario == true) { + scenarioFiles.push_back(scenario); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d]\nError loading scenario [%s]:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenario.c_str(), ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + + showGeneralError = true; + generalErrorToShow = szBuf; + //throw megaglest_runtime_error(szBuf); + } + } + resultsScenarios.clear(); + for (int i = 0; i < (int) scenarioFiles.size(); ++i) { + resultsScenarios.push_back(formatString(scenarioFiles[i])); + } + listBoxScenario.setItems(resultsScenarios); + if (resultsScenarios.empty() == true) { + checkBoxScenario.setEnabled(false); + } + // Advanced Options + labelAdvanced.registerGraphicComponent(containerName, + "labelAdvanced"); + labelAdvanced.init(scenarioX, 80, 80); + labelAdvanced.setText(lang.getString("AdvancedGameOptions")); + + checkBoxAdvanced.registerGraphicComponent(containerName, + "checkBoxAdvanced"); + checkBoxAdvanced.init(scenarioX, 80 - labelOffset); + checkBoxAdvanced.setValue(false); + + // network things + // PublishServer + xoffset = 90; + + labelPublishServer.registerGraphicComponent(containerName, + "labelPublishServer"); + labelPublishServer.init(20, networkHeadPos, 100); + labelPublishServer.setText(lang.getString("PublishServer")); + + checkBoxPublishServer.registerGraphicComponent(containerName, + "checkBoxPublishServer"); + checkBoxPublishServer.init(20, networkPos); + + checkBoxPublishServer.setValue(false); + if ((this->headlessServerMode == true || + (openNetworkSlots == true && parentMenuState != pLanGame)) && + GlobalStaticFlags::isFlagSet(gsft_lan_mode) == false) { + checkBoxPublishServer.setValue(true); + } + + labelGameName.registerGraphicComponent(containerName, + "labelGameName"); + labelGameName.init(20 + checkBoxPublishServer.getW() + 5, + networkPos, 200); + labelGameName.setFont(CoreData::getInstance().getMenuFontBig()); + labelGameName. + setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelGameName.setText(createGameName()); + labelGameName.setEditable(true); + labelGameName.setMaxEditWidth(20); + labelGameName.setMaxEditRenderWidth(200); + + + bool allowInProgressJoin = + Config::getInstance().getBool("EnableJoinInProgressGame", "true"); + labelAllowInGameJoinPlayer.registerGraphicComponent(containerName, + "labelAllowInGameJoinPlayer"); + labelAllowInGameJoinPlayer.init(50, networkPos - 30, 80); + labelAllowInGameJoinPlayer.setText(lang.getString + ("AllowInGameJoinPlayer")); + labelAllowInGameJoinPlayer.setVisible(allowInProgressJoin); + + checkBoxAllowInGameJoinPlayer.registerGraphicComponent(containerName, + "checkBoxAllowInGameJoinPlayer"); + checkBoxAllowInGameJoinPlayer.init(20, networkPos - 30); + checkBoxAllowInGameJoinPlayer.setValue(false); + checkBoxAllowInGameJoinPlayer.setVisible(allowInProgressJoin); + + + labelAllowTeamUnitSharing.registerGraphicComponent(containerName, + "labelAllowTeamUnitSharing"); + labelAllowTeamUnitSharing.init(xoffset + 410, 670, 80); + labelAllowTeamUnitSharing.setText(lang.getString + ("AllowTeamUnitSharing")); + labelAllowTeamUnitSharing.setVisible(true); + + checkBoxAllowTeamUnitSharing.registerGraphicComponent(containerName, + "checkBoxAllowTeamUnitSharing"); + checkBoxAllowTeamUnitSharing.init(xoffset + 612, 670); + checkBoxAllowTeamUnitSharing.setValue(false); + checkBoxAllowTeamUnitSharing.setVisible(true); + + labelAllowTeamResourceSharing.registerGraphicComponent(containerName, + "labelAllowTeamResourceSharing"); + labelAllowTeamResourceSharing.init(xoffset + 410, 640, 80); + labelAllowTeamResourceSharing.setText(lang.getString + ("AllowTeamResourceSharing")); + labelAllowTeamResourceSharing.setVisible(true); + + checkBoxAllowTeamResourceSharing.registerGraphicComponent + (containerName, "checkBoxAllowTeamResourceSharing"); + checkBoxAllowTeamResourceSharing.init(xoffset + 612, 640); + checkBoxAllowTeamResourceSharing.setValue(false); + checkBoxAllowTeamResourceSharing.setVisible(true); + + + // Network Pause for lagged clients + labelNetworkPauseGameForLaggedClients.registerGraphicComponent + (containerName, "labelNetworkPauseGameForLaggedClients"); + labelNetworkPauseGameForLaggedClients.init + (labelAllowTeamResourceSharing.getX(), networkHeadPos, 80); + labelNetworkPauseGameForLaggedClients.setText(lang.getString + ("NetworkPauseGameForLaggedClients")); + + checkBoxNetworkPauseGameForLaggedClients.registerGraphicComponent + (containerName, "checkBoxNetworkPauseGameForLaggedClients"); + checkBoxNetworkPauseGameForLaggedClients.init + (checkBoxAllowTeamResourceSharing.getX(), networkHeadPos); + checkBoxNetworkPauseGameForLaggedClients.setValue(true); + + //list boxes + xoffset = 5; + int rowHeight = 27; + + buttonClearBlockedPlayers.registerGraphicComponent(containerName, + "buttonClearBlockedPlayers"); + buttonClearBlockedPlayers.init(xoffset + 800, + setupPos - 30, + 134 + 2 + 50); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + + labelPlayers[i].registerGraphicComponent(containerName, + "labelPlayers" + + intToStr(i)); + labelPlayers[i].init(xoffset - 1, + setupPos - 30 - i * rowHeight + 2); + labelPlayers[i]. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + labelPlayers[i]. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + + labelPlayerStatus[i].registerGraphicComponent(containerName, + "labelPlayerStatus" + + intToStr(i)); + labelPlayerStatus[i].init(xoffset + 14, + setupPos - 30 - i * rowHeight + 2); + labelPlayerNames[i].registerGraphicComponent(containerName, + "labelPlayerNames" + + intToStr(i)); + labelPlayerNames[i].init(xoffset + 30, + setupPos - 30 - i * rowHeight); + + listBoxControls[i].registerGraphicComponent(containerName, + "listBoxControls" + + intToStr(i)); + listBoxControls[i].init(xoffset + 160, + setupPos - 30 - i * rowHeight, 174); + + buttonBlockPlayers[i].registerGraphicComponent(containerName, + "buttonBlockPlayers" + + intToStr(i)); + //buttonBlockPlayers[i].init(xoffset+355, setupPos-30-i*rowHeight, 70); + buttonBlockPlayers[i].init(xoffset + 185, + setupPos - 30 - i * rowHeight, 124); + buttonBlockPlayers[i].setText(lang.getString("BlockPlayer")); + buttonBlockPlayers[i]. + setFont(CoreData::getInstance().getDisplayFontSmall()); + buttonBlockPlayers[i]. + setFont3D(CoreData::getInstance().getDisplayFontSmall3D()); + + listBoxRMultiplier[i].registerGraphicComponent(containerName, + "listBoxRMultiplier" + + intToStr(i)); + listBoxRMultiplier[i].init(xoffset + 336, + setupPos - 30 - i * rowHeight, 70); + + listBoxFactions[i].registerGraphicComponent(containerName, + "listBoxFactions" + + intToStr(i)); + listBoxFactions[i].init(xoffset + 411, + setupPos - 30 - i * rowHeight, 247); + listBoxFactions[i].setLeftControlled(true); + + listBoxTeams[i].registerGraphicComponent(containerName, + "listBoxTeams" + + intToStr(i)); + listBoxTeams[i].init(xoffset + 660, + setupPos - 30 - i * rowHeight, 60); + listBoxTeams[i].setLighted(true); + + labelNetStatus[i].registerGraphicComponent(containerName, + "labelNetStatus" + + intToStr(i)); + labelNetStatus[i].init(xoffset + 726, + setupPos - 30 - i * rowHeight, 60); + labelNetStatus[i]. + setFont(CoreData::getInstance().getDisplayFontSmall()); + labelNetStatus[i]. + setFont3D(CoreData::getInstance().getDisplayFontSmall3D()); + } + + labelControl.registerGraphicComponent(containerName, "labelControl"); + labelControl.init(xoffset + 160, setupPos, 50, GraphicListBox::defH, + true); + labelControl.setText(lang.getString("Control")); + + labelRMultiplier.registerGraphicComponent(containerName, + "labelRMultiplier"); + labelRMultiplier.init(xoffset + 310, setupPos, 50, + GraphicListBox::defH, true); + + labelFaction.registerGraphicComponent(containerName, "labelFaction"); + labelFaction.init(xoffset + 411, setupPos, 50, GraphicListBox::defH, + true); + labelFaction.setText(lang.getString("Faction")); + + labelTeam.registerGraphicComponent(containerName, "labelTeam"); + labelTeam.init(xoffset + 660, setupPos, 50, GraphicListBox::defH, + true); + labelTeam.setText(lang.getString("Team")); + + labelControl.setFont(CoreData::getInstance().getMenuFontBig()); + labelControl.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelRMultiplier.setFont(CoreData::getInstance().getMenuFontBig()); + labelRMultiplier. + setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelFaction.setFont(CoreData::getInstance().getMenuFontBig()); + labelFaction.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTeam.setFont(CoreData::getInstance().getMenuFontBig()); + labelTeam.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + //xoffset=100; + + //texts + buttonClearBlockedPlayers. + setText(lang.getString("BlockPlayerClear")); + buttonReturn.setText(lang.getString("Return")); + buttonPlayNow.setText(lang.getString("PlayNow")); + buttonRestoreLastSettings.setText(lang.getString + ("ReloadLastGameSettings")); + + vector < string > controlItems; + controlItems.push_back(lang.getString("Closed")); + controlItems.push_back(lang.getString("CpuEasy")); + controlItems.push_back(lang.getString("Cpu")); + controlItems.push_back(lang.getString("CpuUltra")); + controlItems.push_back(lang.getString("CpuZeta")); + controlItems.push_back(lang.getString("Network")); + controlItems.push_back(lang.getString("NetworkUnassigned")); + controlItems.push_back(lang.getString("Human")); + + if (config.getBool("EnableNetworkCpu", "false") == true) { + controlItems.push_back(lang.getString("NetworkCpuEasy")); + controlItems.push_back(lang.getString("NetworkCpu")); + controlItems.push_back(lang.getString("NetworkCpuUltra")); + controlItems.push_back(lang.getString("NetworkCpuZeta")); + } + + vector < string > teamItems; + for (int i = 1; i <= GameConstants::maxPlayers; ++i) { + teamItems.push_back(intToStr(i)); + } + for (int i = GameConstants::maxPlayers + 1; + i <= GameConstants::maxPlayers + GameConstants::specialFactions; + ++i) { + teamItems.push_back(intToStr(i)); + } + + reloadFactions(false, ""); + + if (factionFiles.empty() == true) { + showGeneralError = true; + generalErrorToShow = + "[#1] There are no factions for the tech tree [" + + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"; + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + labelPlayerStatus[i].setText(" "); + labelPlayerStatus[i]. + setTexture(CoreData::getInstance().getStatusReadyTexture()); + labelPlayerStatus[i].setH(16); + labelPlayerStatus[i].setW(12); + + //labelPlayers[i].setText(lang.getString("Player")+" "+intToStr(i)); + labelPlayers[i].setText(intToStr(i + 1)); + labelPlayerNames[i].setText("*"); + labelPlayerNames[i].setMaxEditWidth(16); + labelPlayerNames[i].setMaxEditRenderWidth(127); + + listBoxTeams[i].setItems(teamItems); + listBoxTeams[i].setSelectedItemIndex(i); + lastSelectedTeamIndex[i] = listBoxTeams[i].getSelectedItemIndex(); + + listBoxControls[i].setItems(controlItems); + listBoxRMultiplier[i].setItems(rMultiplier); + listBoxRMultiplier[i].setSelectedItem("1.0"); + labelNetStatus[i].setText(""); + } + + // I moved this block from loadMapInfo(), and modified it. It was + // setting the slots visible based on the number of hardMaxPlayers + // every time a new map was loaded. Trying it here instead, so the + // labels are made visible only once. Below, we'll be disabling slots + // that exceed hardMaxPlayers + for (int i = 0; i < GameConstants::maxPlayers; i++) { + labelPlayers[i].setVisible(true); + labelPlayerNames[i].setVisible(true); + listBoxControls[i].setVisible(true); + listBoxFactions[i].setVisible(true); + listBoxTeams[i].setVisible(true); + labelNetStatus[i].setVisible(true); + } + + loadMapInfo(Config::getMapPath(getCurrentMapFile()), &mapInfo, + true); + labelMapInfo.setText(mapInfo.desc); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //init controllers + if (serverInitError == false) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface == NULL) { + throw megaglest_runtime_error("serverInterface == NULL"); + } + if (this->headlessServerMode == true) { + listBoxControls[0].setSelectedItemIndex(ctNetwork); + updateResourceMultiplier(0); + } else { + setSlotHuman(0); + updateResourceMultiplier(0); + } + labelPlayerNames[0].setText(""); + labelPlayerNames[0].setText(getHumanPlayerName()); + + if (openNetworkSlots == true) { + for (int i = 1; i < mapInfo.players; ++i) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + } + } else { + listBoxControls[1].setSelectedItemIndex(ctCpu); + } + updateControllers(); + updateNetworkSlots(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + // Ensure we have set the gamesettings at least once + GameSettings gameSettings; + loadGameSettings(&gameSettings); + + serverInterface->setGameSettings(&gameSettings, false); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + updateAllResourceMultiplier(); + + // write hint to console: + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + console.addLine(lang.getString("ToSwitchOffMusicPress") + " - \"" + + configKeys.getString("ToggleMusic") + "\""); + + chatManager.init(&console, -1, true); + + GraphicComponent::applyAllCustomProperties(containerName); + + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + publishToMasterserverThread = + new SimpleTaskThread(this, 0, 300, false, + (void *) tnt_MASTERSERVER); + publishToMasterserverThread->setUniqueID(mutexOwnerId); + + static string mutexOwnerId2 = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + publishToClientsThread = + new SimpleTaskThread(this, 0, 200, false, (void *) tnt_CLIENTS, + false); + publishToClientsThread->setUniqueID(mutexOwnerId2); + + publishToMasterserverThread->start(); + publishToClientsThread->start(); + + if (openNetworkSlots == true) { + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + if (fileExists(data_path + DEFAULT_NETWORKGAME_FILENAME) == true) + loadGameSettings(data_path + DEFAULT_NETWORKGAME_FILENAME); + } else { + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + + if (fileExists(data_path + DEFAULT_GAME_FILENAME) == true) + loadGameSettings(data_path + DEFAULT_GAME_FILENAME); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + } + + string MenuStateCustomGame::createGameName(string controllingPlayer) { + Config & config = Config::getInstance(); + string serverTitle = config.getString("ServerTitle", ""); + + if (serverTitle != "" && controllingPlayer == "") { + return serverTitle; + } else if (this->headlessServerMode == true) { + if (controllingPlayer != "") { + return controllingPlayer + " controls"; + } else { + return "[H] " + defaultPlayerName; + } + } else { + string defaultPlayerNameEnd = + defaultPlayerName.substr(defaultPlayerName.size() - 1, 1); + if (defaultPlayerNameEnd == "s") { + return defaultPlayerName + "' game"; + } else { + return defaultPlayerName + "'s game"; + } + } + } + + void MenuStateCustomGame::reloadUI() { + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); + + console.resetFonts(); + mainMessageBox.init(lang.getString("Ok"), 500, 300); + + + if (EndsWith(glestVersionString, "-dev") == false) { + labelLocalGameVersion.setText(glestVersionString); + } else { + //labelLocalGameVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); + labelLocalGameVersion.setText(glestVersionString); + } + + //vector teamItems, controlItems, results , rMultiplier; + + string ipText = "none"; + std::vector < std::string > ipList = Socket::getLocalIPAddressList(); + if (ipList.empty() == false) { + ipText = ""; + for (int idx = 0; idx < (int) ipList.size(); idx++) { + string ip = ipList[idx]; + if (ipText != "") { + ipText += ", "; + } + ipText += ip; + } + } + string serverPort = config.getString("PortServer", + intToStr + (GameConstants:: + serverPort).c_str()); + string externalPort = + config.getString("PortExternal", serverPort.c_str()); + + labelLocalIP.setText(lang.getString("LanIP") + ipText + " ( " + + serverPort + " / " + externalPort + " )"); + + labelMap.setText(lang.getString("Map")); + + labelMapFilter.setText(lang.getString("MapFilter")); + + labelTileset.setText(lang.getString("Tileset")); + + labelTechTree.setText(lang.getString("TechTree")); + + labelAllowNativeLanguageTechtree.setText(lang.getString + ("AllowNativeLanguageTechtree")); + + labelFogOfWar.setText(lang.getString("FogOfWar")); + + std::vector < std::string > listBoxData; + listBoxData.push_back(lang.getString("Enabled")); + listBoxData.push_back(lang.getString("Explored")); + listBoxData.push_back(lang.getString("Disabled")); + listBoxFogOfWar.setItems(listBoxData); + + // Allow Observers + labelAllowObservers.setText(lang.getString("AllowObservers")); + + // Allow Switch Team Mode + labelEnableSwitchTeamMode. + setText(lang.getString("EnableSwitchTeamMode")); + + labelAllowInGameJoinPlayer.setText(lang.getString + ("AllowInGameJoinPlayer")); + + labelAllowTeamUnitSharing. + setText(lang.getString("AllowTeamUnitSharing")); + labelAllowTeamResourceSharing. + setText(lang.getString("AllowTeamResourceSharing")); + + labelAISwitchTeamAcceptPercent.setText(lang.getString + ("AISwitchTeamAcceptPercent")); + + listBoxData.clear(); + + // Advanced Options + labelAdvanced.setText(lang.getString("AdvancedGameOptions")); + + labelPublishServer.setText(lang.getString("PublishServer")); + + labelGameName.setFont(CoreData::getInstance().getMenuFontBig()); + labelGameName.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + labelGameName.setText(createGameName()); + + labelNetworkPauseGameForLaggedClients.setText(lang.getString + ("NetworkPauseGameForLaggedClients")); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + buttonBlockPlayers[i].setText(lang.getString("BlockPlayer")); + } + + labelControl.setText(lang.getString("Control")); + + labelFaction.setText(lang.getString("Faction")); + + labelTeam.setText(lang.getString("Team")); + + labelControl.setFont(CoreData::getInstance().getMenuFontBig()); + labelControl.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelRMultiplier.setFont(CoreData::getInstance().getMenuFontBig()); + labelRMultiplier. + setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelFaction.setFont(CoreData::getInstance().getMenuFontBig()); + labelFaction.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTeam.setFont(CoreData::getInstance().getMenuFontBig()); + labelTeam.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + + //texts + buttonClearBlockedPlayers.setText(lang.getString("BlockPlayerClear")); + buttonReturn.setText(lang.getString("Return")); + buttonPlayNow.setText(lang.getString("PlayNow")); + buttonRestoreLastSettings.setText(lang.getString + ("ReloadLastGameSettings")); + + vector < string > controlItems; + controlItems.push_back(lang.getString("Closed")); + controlItems.push_back(lang.getString("CpuEasy")); + controlItems.push_back(lang.getString("Cpu")); + controlItems.push_back(lang.getString("CpuUltra")); + controlItems.push_back(lang.getString("CpuZeta")); + controlItems.push_back(lang.getString("Network")); + controlItems.push_back(lang.getString("NetworkUnassigned")); + controlItems.push_back(lang.getString("Human")); + + if (config.getBool("EnableNetworkCpu", "false") == true) { + controlItems.push_back(lang.getString("NetworkCpuEasy")); + controlItems.push_back(lang.getString("NetworkCpu")); + controlItems.push_back(lang.getString("NetworkCpuUltra")); + controlItems.push_back(lang.getString("NetworkCpuZeta")); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + labelPlayers[i].setText(intToStr(i + 1)); + + listBoxControls[i].setItems(controlItems); + } + + labelFallbackCpuMultiplier.setText(lang.getString + ("FallbackCpuMultiplier")); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + vector < string > playerStatuses; + playerStatuses.push_back(lang.getString("PlayerStatusSetup")); + playerStatuses.push_back(lang.getString("PlayerStatusBeRightBack")); + playerStatuses.push_back(lang.getString("PlayerStatusReady")); + listBoxPlayerStatus.setItems(playerStatuses); + + labelScenario.setText(lang.getString("Scenario")); + + reloadFactions(true, + (checkBoxScenario.getValue() == + true ? + scenarioFiles[listBoxScenario.getSelectedItemIndex()] + : "")); + + // write hint to console: + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + console.addLine(lang.getString("ToSwitchOffMusicPress") + " - \"" + + configKeys.getString("ToggleMusic") + "\""); + + chatManager.init(&console, -1, true); + + GraphicComponent::reloadFontsForRegisterGraphicComponents + (containerName); + } + + void MenuStateCustomGame::cleanupThread(SimpleTaskThread ** thread) { + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + + if (thread != NULL && *thread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1 cleanupThread callingThread [%p]\n", *thread); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + SimpleTaskThread *threadPtr = *thread; + int value = threadPtr->getUserdataAsInt(); + THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1. cleanupThread callingThread [%p] value = %d\n", + *thread, value); + + needToBroadcastServerSettings = false; + needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); + threadPtr->setThreadOwnerValid(false); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1.. cleanupThread callingThread [%p] value = %d\n", + *thread, value); + + if (forceWaitForShutdown == true) { + time_t elapsed = time(NULL); + threadPtr->signalQuit(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1a cleanupThread callingThread [%p]\n", *thread); + + for (; (threadPtr->canShutdown(false) == false || + threadPtr->getRunningStatus() == true) && + difftime((long int) time(NULL), elapsed) <= 15;) { + //sleep(150); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1b cleanupThread callingThread [%p]\n", *thread); + + if (threadPtr->canShutdown(true) == true && + threadPtr->getRunningStatus() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1c cleanupThread callingThread [%p]\n", *thread); + + delete threadPtr; + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1d cleanupThread callingThread [%p]\n", *thread); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown thread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + + if (threadType == tnt_MASTERSERVER) { + threadPtr->setOverrideShutdownTask(shutdownTaskStatic); + } + threadPtr->setDeleteSelfOnExecutionDone(true); + threadPtr->setDeleteAfterExecute(true); + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + } + threadPtr = NULL; + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\n\n#1e cleanupThread callingThread [%p]\n",*thread); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1f cleanupThread callingThread [%p]\n", *thread); + threadPtr->signalQuit(); + sleep(0); + if (threadPtr->canShutdown(true) == true && + threadPtr->getRunningStatus() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1g cleanupThread callingThread [%p]\n", *thread); + delete threadPtr; + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#1h cleanupThread callingThread [%p]\n", *thread); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown thread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + + if (threadType == tnt_MASTERSERVER) { + threadPtr->setOverrideShutdownTask(shutdownTaskStatic); + } + threadPtr->setDeleteSelfOnExecutionDone(true); + threadPtr->setDeleteAfterExecute(true); + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + } + } + + *thread = NULL; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\n#2 cleanupThread callingThread [%p]\n", *thread); + } + //printf("LINE: %d *thread = %p\n",__LINE__,*thread); + } + + void MenuStateCustomGame::cleanup() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (publishToMasterserverThread) { + //printf("LINE: %d\n",__LINE__); + cleanupThread(&publishToMasterserverThread); + } + if (publishToClientsThread) { + //printf("LINE: %d\n",__LINE__); + cleanupThread(&publishToClientsThread); + } + + //printf("LINE: %d\n",__LINE__); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + cleanupMapPreviewTexture(); + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + + if (forceWaitForShutdown == true) { + NetworkManager::getInstance().end(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + MenuStateCustomGame::~MenuStateCustomGame() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + cleanup(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + + void MenuStateCustomGame::returnToParentMenu() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + needToBroadcastServerSettings = false; + needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); + ParentMenuState parentMenuState = this->parentMenuState; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + forceWaitForShutdown = false; + if (parentMenuState == pMasterServer) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + cleanup(); + mainMenu->setState(new MenuStateMasterserver(program, mainMenu)); + } else if (parentMenuState == pLanGame) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + cleanup(); + mainMenu->setState(new MenuStateJoinGame(program, mainMenu)); + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + cleanup(); + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::mouseClick(int x, int y, + MouseButton mouseButton) { + if (isMasterserverMode() == true) { + return; + } + + try { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + int oldListBoxMapfilterIndex = + listBoxMapFilter.getSelectedItemIndex(); + + if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + mainMessageBox.setEnabled(false); + } + } + } else { + string advanceToItemStartingWith = ""; + if (::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == + true) { + const wchar_t lastKey = ::Shared::Platform:: + Window::extractLastKeyPressed(); + // string helpString = ""; + // helpString = lastKey; + // printf("lastKey = '%s'\n",helpString.c_str()); + advanceToItemStartingWith = lastKey; + } + + if (mapPreviewTexture != NULL) { + // printf("X: %d Y: %d [%d, %d, %d, %d]\n", + // x, y, + // this->render_mapPreviewTexture_X, this->render_mapPreviewTexture_X + this->render_mapPreviewTexture_W, + // this->render_mapPreviewTexture_Y, this->render_mapPreviewTexture_Y + this->render_mapPreviewTexture_H); + + if (x >= this->render_mapPreviewTexture_X + && x <= + this->render_mapPreviewTexture_X + + this->render_mapPreviewTexture_W + && y >= this->render_mapPreviewTexture_Y + && y <= + this->render_mapPreviewTexture_Y + + this->render_mapPreviewTexture_H) { + + if (this->render_mapPreviewTexture_X == + mapPreviewTexture_X + && this->render_mapPreviewTexture_Y == + mapPreviewTexture_Y + && this->render_mapPreviewTexture_W == + mapPreviewTexture_W + && this->render_mapPreviewTexture_H == mapPreviewTexture_H) { + + const Metrics & metrics = Metrics::getInstance(); + + this->render_mapPreviewTexture_X = 0; + this->render_mapPreviewTexture_Y = 0; + this->render_mapPreviewTexture_W = metrics.getVirtualW(); + this->render_mapPreviewTexture_H = metrics.getVirtualH(); + this->zoomedMap = true; + + cleanupMapPreviewTexture(); + } else { + this->render_mapPreviewTexture_X = mapPreviewTexture_X; + this->render_mapPreviewTexture_Y = mapPreviewTexture_Y; + this->render_mapPreviewTexture_W = mapPreviewTexture_W; + this->render_mapPreviewTexture_H = mapPreviewTexture_H; + this->zoomedMap = false; + + cleanupMapPreviewTexture(); + } + return; + } + if (this->zoomedMap == true) { + return; + } + } + + if (activeInputLabel != NULL + && !(activeInputLabel->mouseClick(x, y))) { + setActiveInputLabel(NULL); + } + if (buttonReturn.mouseClick(x, y) || serverInitError == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + soundRenderer.playFx(coreData.getClickSoundA()); + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + needToBroadcastServerSettings = false; + needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + returnToParentMenu(); + return; + } else if (buttonPlayNow.mouseClick(x, y) + && buttonPlayNow.getEnabled()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + PlayNow(true); + return; + } else if (buttonRestoreLastSettings.mouseClick(x, y) + && buttonRestoreLastSettings.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + + RestoreLastGameSettings(); + } else if (listBoxMap.mouseClick(x, y, advanceToItemStartingWith)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + getCurrentMapFile().c_str()); + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + loadMapInfo(Config::getMapPath(getCurrentMapFile(), "", false), + &mapInfo, true); + labelMapInfo.setText(mapInfo.desc); + updateControllers(); + updateNetworkSlots(); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + //delay publishing for 5 seconds + needToPublishDelayed = true; + mapPublishingDelayTimer = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && listBoxFogOfWar.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + cleanupMapPreviewTexture(); + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && checkBoxAllowObservers.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + reloadFactions(true, + (checkBoxScenario.getValue() == + true ? + scenarioFiles + [listBoxScenario.getSelectedItemIndex()] : "")); + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAllowInGameJoinPlayer.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface->setAllowInGameConnections + (checkBoxAllowInGameJoinPlayer.getValue() == true); + } else if (checkBoxAdvanced.getValue() == 1 + && checkBoxAllowTeamUnitSharing.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && checkBoxAllowTeamResourceSharing.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAllowNativeLanguageTechtree.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && checkBoxEnableSwitchTeamMode.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && listBoxAISwitchTeamAcceptPercent.getEnabled() + && listBoxAISwitchTeamAcceptPercent.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.getValue() == 1 + && listBoxFallbackCpuMultiplier.getEditable() == true + && listBoxFallbackCpuMultiplier.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxAdvanced.mouseClick(x, y)) { + } else + if (listBoxTileset.mouseClick(x, y, advanceToItemStartingWith)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + if (hasNetworkGameSettings() == true) { + + //delay publishing for 5 seconds + needToPublishDelayed = true; + mapPublishingDelayTimer = time(NULL); + } + } else if (listBoxMapFilter.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + switchToNextMapGroup(listBoxMapFilter.getSelectedItemIndex() - + oldListBoxMapfilterIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s\n", + getCurrentMapFile().c_str()); + + loadMapInfo(Config::getMapPath(getCurrentMapFile()), + &mapInfo, true); + labelMapInfo.setText(mapInfo.desc); + updateControllers(); + updateNetworkSlots(); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else + if (listBoxTechTree.mouseClick(x, y, advanceToItemStartingWith)) { + reloadFactions(listBoxTechTree.getItemCount() <= 1, + (checkBoxScenario.getValue() == + true ? + scenarioFiles + [listBoxScenario.getSelectedItemIndex()] : "")); + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (checkBoxPublishServer.mouseClick(x, y) + && checkBoxPublishServer.getEditable()) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + needToRepublishToMasterserver = true; + soundRenderer.playFx(coreData.getClickSoundC()); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface-> + setPublishEnabled(checkBoxPublishServer.getValue() == true); + } else if (labelGameName.mouseClick(x, y) + && checkBoxPublishServer.getEditable()) { + setActiveInputLabel(&labelGameName); + } else if (checkBoxAdvanced.getValue() == 1 + && checkBoxNetworkPauseGameForLaggedClients.mouseClick(x, + y)) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + + soundRenderer.playFx(coreData.getClickSoundC()); + } else if (listBoxScenario.mouseClick(x, y) + || checkBoxScenario.mouseClick(x, y)) { + processScenario(); + } else { + for (int i = 0; i < mapInfo.players; ++i) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + // set multiplier + if (listBoxRMultiplier[i].mouseClick(x, y)) { + //printf("Line: %d multiplier index: %d i: %d itemcount: %d\n",__LINE__,listBoxRMultiplier[i].getSelectedItemIndex(),i,listBoxRMultiplier[i].getItemCount()); + + //for(int indexData = 0; indexData < listBoxRMultiplier[i].getItemCount(); ++indexData) { + //string item = listBoxRMultiplier[i].getItem(indexData); + + //printf("Item index: %d value: %s\n",indexData,item.c_str()); + //} + } + + //ensure thet only 1 human player is present + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = serverInterface->getSlot(i, true); + + bool checkControTypeClicked = false; + int selectedControlItemIndex = + listBoxControls[i].getSelectedItemIndex(); + if (selectedControlItemIndex != ctNetwork + || (selectedControlItemIndex == ctNetwork + && (slot == NULL || slot->isConnected() == false))) { + checkControTypeClicked = true; + } + + //printf("checkControTypeClicked = %d selectedControlItemIndex = %d i = %d\n",checkControTypeClicked,selectedControlItemIndex,i); + + if (selectedControlItemIndex != ctHuman && + checkControTypeClicked == true && + listBoxControls[i].mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + ControlType currentControlType = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + int slotsToChangeStart = i; + int slotsToChangeEnd = i; + // If control is pressed while changing player types then change all other slots to same type + if (::Shared::Platform:: + Window::isKeyStateModPressed(KMOD_CTRL) == true + && currentControlType != ctHuman) { + slotsToChangeStart = 0; + slotsToChangeEnd = mapInfo.players - 1; + } + + for (int index = slotsToChangeStart; + index <= slotsToChangeEnd; ++index) { + if (index != i + && static_cast + (listBoxControls[index].getSelectedItemIndex()) != + ctHuman) { + listBoxControls[index].setSelectedItemIndex + (listBoxControls[i].getSelectedItemIndex()); + } + // Skip over networkunassigned + if (listBoxControls[index].getSelectedItemIndex() == + ctNetworkUnassigned + && selectedControlItemIndex != ctNetworkUnassigned) { + listBoxControls[index].mouseClick(x, y); + } + + //look for human players + int humanIndex1 = -1; + int humanIndex2 = -1; + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + ControlType ct = + static_cast + (listBoxControls[j].getSelectedItemIndex()); + if (ct == ctHuman) { + if (humanIndex1 == -1) { + humanIndex1 = j; + } else { + humanIndex2 = j; + } + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] humanIndex1 = %d, humanIndex2 = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + humanIndex1, humanIndex2); + + //no human + if (humanIndex1 == -1 && humanIndex2 == -1) { + setSlotHuman(index); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] i = %d, labelPlayerNames[i].getText() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, index, + labelPlayerNames + [index].getText().c_str()); + + //printf("humanIndex1 = %d humanIndex2 = %d i = %d listBoxControls[i].getSelectedItemIndex() = %d\n",humanIndex1,humanIndex2,i,listBoxControls[i].getSelectedItemIndex()); + } + //2 humans + else if (humanIndex1 != -1 && humanIndex2 != -1) { + int closeSlotIndex = + (humanIndex1 == index ? humanIndex2 : humanIndex1); + int humanSlotIndex = + (closeSlotIndex == + humanIndex1 ? humanIndex2 : humanIndex1); + + string origPlayName = + labelPlayerNames[closeSlotIndex].getText(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] closeSlotIndex = %d, origPlayName [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + closeSlotIndex, + origPlayName.c_str()); + + listBoxControls[closeSlotIndex].setSelectedItemIndex + (ctClosed); + setSlotHuman(humanSlotIndex); + labelPlayerNames[humanSlotIndex].setText((origPlayName != + "" ? + origPlayName : + getHumanPlayerName + ())); + } + updateNetworkSlots(); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + updateResourceMultiplier(index); + } + } else if (buttonClearBlockedPlayers.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface != NULL) { + ServerSocket *serverSocket = + serverInterface->getServerSocket(); + if (serverSocket != NULL) { + serverSocket->clearBlockedIPAddress(); + } + } + } else if (buttonBlockPlayers[i].mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface != NULL) { + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, true)->isConnected()) { + + ServerSocket *serverSocket = + serverInterface->getServerSocket(); + if (serverSocket != NULL) { + serverSocket->addIPAddressToBlockedList + (serverInterface->getSlot(i, true)->getIpAddress()); + + Lang & lang = Lang::getInstance(); + const vector < string > languageList = + serverInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages + (); + for (unsigned int j = 0; j < languageList.size(); ++j) { + char szMsg[8096] = ""; + if (lang.hasString("BlockPlayerServerMsg", + languageList[j]) == true) { + snprintf(szMsg, 8096, + lang.getString + ("BlockPlayerServerMsg", + languageList[j]).c_str(), + serverInterface->getSlot(i, + true)->getIpAddress + ().c_str()); + } else { + snprintf(szMsg, 8096, + "The server has temporarily blocked IP Address [%s] from this game.", + serverInterface->getSlot(i, + true)->getIpAddress + ().c_str()); + } + + serverInterface->sendTextMessage(szMsg, -1, true, + languageList[j]); + } + sleep(1); + serverInterface->getSlot(i, true)->close(); + } + } + } + } else + if (listBoxFactions[i].mouseClick + (x, y, advanceToItemStartingWith)) { + // Disallow CPU players to be observers + if (factionFiles + [listBoxFactions[i].getSelectedItemIndex()] == + formatString(GameConstants::OBSERVER_SLOTNAME) + && (listBoxControls[i].getSelectedItemIndex() == + ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta)) { + listBoxFactions[i].setSelectedItemIndex(0); + } + // + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } else if (listBoxTeams[i].mouseClick(x, y)) { + if (factionFiles + [listBoxFactions[i].getSelectedItemIndex()] != + formatString(GameConstants::OBSERVER_SLOTNAME)) { + if (listBoxTeams[i].getSelectedItemIndex() + 1 != + (GameConstants::maxPlayers + fpt_Observer)) { + lastSelectedTeamIndex[i] = + listBoxTeams[i].getSelectedItemIndex(); + } + } else { + lastSelectedTeamIndex[i] = -1; + } + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL);; + } + } else if (labelPlayerNames[i].mouseClick(x, y)) { + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct == ctHuman) { + setActiveInputLabel(&labelPlayerNames[i]); + break; + } + } + } + } + } + + if (hasNetworkGameSettings() == true + && listBoxPlayerStatus.mouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + soundRenderer.playFx(coreData.getClickSoundC()); + if (getNetworkPlayerStatus() == npst_PickSettings) { + listBoxPlayerStatus.setTextColor(Vec3f(1.0f, 0.0f, 0.0f)); + listBoxPlayerStatus.setLighted(true); + } else if (getNetworkPlayerStatus() == npst_BeRightBack) { + listBoxPlayerStatus.setTextColor(Vec3f(1.0f, 1.0f, 0.0f)); + listBoxPlayerStatus.setLighted(true); + } else if (getNetworkPlayerStatus() == npst_Ready) { + listBoxPlayerStatus.setTextColor(Vec3f(0.0f, 1.0f, 0.0f)); + listBoxPlayerStatus.setLighted(false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showGeneralError = true; + generalErrorToShow = szBuf; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::loadGameSettings(const std::string & fileName) { + // Ensure we have set the gamesettings at least once + GameSettings gameSettings = loadGameSettingsFromFile(fileName); + if (gameSettings.getMap() == "") { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + loadGameSettings(&gameSettings); + } + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface->setGameSettings(&gameSettings, false); + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor() + : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + + void MenuStateCustomGame::RestoreLastGameSettings() { + loadGameSettings(SAVED_GAME_FILENAME); + } + + bool MenuStateCustomGame::checkNetworkPlayerDataSynch(bool checkMapCRC, + bool + checkTileSetCRC, + bool + checkTechTreeCRC) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + + bool dataSynchCheckOk = true; + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork) { + + MutexSafeWrapper safeMutex(serverInterface->getSlotMutex(i), + CODE_AT_LINE); + ConnectionSlot *slot = serverInterface->getSlot(i, false); + if (slot != NULL && slot->isConnected() && + (slot->getAllowDownloadDataSynch() == true || + slot->getAllowGameDataSynchCheck() == true)) { + + if (checkMapCRC == true && + slot->getNetworkGameDataSynchCheckOkMap() == false) { + dataSynchCheckOk = false; + break; + } + if (checkTileSetCRC == true && + slot->getNetworkGameDataSynchCheckOkTile() == false) { + dataSynchCheckOk = false; + break; + } + if (checkTechTreeCRC == true && + slot->getNetworkGameDataSynchCheckOkTech() == false) { + dataSynchCheckOk = false; + break; + } + } + } + } + + return dataSynchCheckOk; + } + + void MenuStateCustomGame::PlayNow(bool saveGame) { + if (listBoxTechTree.getItemCount() <= 0) { + mainMessageBoxState = 1; + + char szMsg[8096] = ""; + strcpy(szMsg, "Cannot start game.\nThere are no tech-trees!\n"); + printf("%s", szMsg); + + showMessageBox(szMsg, "", false); + return; + } + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor() + : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (saveGame == true) { + saveGameSettingsToFile(SAVED_GAME_FILENAME); + } + + forceWaitForShutdown = false; + closeUnusedSlots(); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + std::vector < string > randomFactionSelectionList; + int RandomCount = 0; + for (int i = 0; i < mapInfo.players; ++i) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Check for random faction selection and choose the faction now + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + if (listBoxFactions[i].getSelectedItem() == + formatString(GameConstants::RANDOMFACTION_SLOTNAME) + && listBoxFactions[i].getItemCount() > 1) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] i = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i); + + // Max 1000 tries to get a random, unused faction + for (int findRandomFaction = 1; findRandomFaction < 1000; + ++findRandomFaction) { + Chrono seed(true); + srand((unsigned int) seed.getCurTicks() + findRandomFaction); + + int selectedFactionIndex = + rand() % listBoxFactions[i].getItemCount(); + string selectedFactionName = + listBoxFactions[i].getItem(selectedFactionIndex); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] selectedFactionName [%s] selectedFactionIndex = %d, findRandomFaction = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + selectedFactionName.c_str(), + selectedFactionIndex, + findRandomFaction); + + if (selectedFactionName != + formatString(GameConstants::RANDOMFACTION_SLOTNAME) + && selectedFactionName != + formatString(GameConstants::OBSERVER_SLOTNAME) + && std::find(randomFactionSelectionList.begin(), + randomFactionSelectionList.end(), + selectedFactionName) == + randomFactionSelectionList.end()) { + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + listBoxFactions[i].setSelectedItem(selectedFactionName); + randomFactionSelectionList.push_back(selectedFactionName); + break; + } + } + + if (listBoxFactions[i].getSelectedItem() == + formatString(GameConstants::RANDOMFACTION_SLOTNAME)) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] RandomCount = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, RandomCount); + + // Find first real faction and use it + int factionIndexToUse = RandomCount; + for (int useIdx = 0; + useIdx < listBoxFactions[i].getItemCount(); useIdx++) { + string selectedFactionName = + listBoxFactions[i].getItem(useIdx); + if (selectedFactionName != + formatString(GameConstants::RANDOMFACTION_SLOTNAME) + && selectedFactionName != + formatString(GameConstants::OBSERVER_SLOTNAME)) { + factionIndexToUse = useIdx; + break; + } + } + listBoxFactions[i].setSelectedItemIndex(factionIndexToUse); + randomFactionSelectionList.push_back(listBoxFactions[i].getItem + (factionIndexToUse)); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] i = %d, listBoxFactions[i].getSelectedItem() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + listBoxFactions[i].getSelectedItem(). + c_str()); + + RandomCount++; + } + } + } + + if (RandomCount > 0) { + needToSetChangedGameSettings = true; + } + + safeMutex.ReleaseLock(true); + safeMutexCLI.ReleaseLock(true); + GameSettings gameSettings; + loadGameSettings(&gameSettings, true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + + // Send the game settings to each client if we have at least one networked client + safeMutex.Lock(); + safeMutexCLI.Lock(); + + bool dataSynchCheckOk = checkNetworkPlayerDataSynch(true, true, true); + + // Ensure we have no dangling network players + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned) { + mainMessageBoxState = 1; + + Lang & lang = Lang::getInstance(); + string sMsg = ""; + if (lang.hasString("NetworkSlotUnassignedErrorUI") == true) { + sMsg = lang.getString("NetworkSlotUnassignedErrorUI"); + } else { + sMsg = + "Cannot start game.\nSome player(s) are not in a network game slot!"; + } + + showMessageBox(sMsg, "", false); + + const vector < string > languageList = + serverInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int j = 0; j < languageList.size(); ++j) { + char szMsg[8096] = ""; + if (lang.hasString("NetworkSlotUnassignedError", + languageList[j]) == true) { + string msg_string = + lang.getString("NetworkSlotUnassignedError"); #ifdef WIN32 - strncpy (szMsg, msg_string.c_str (), - min ((int) msg_string.length (), 8095)); + strncpy(szMsg, msg_string.c_str(), + min((int) msg_string.length(), 8095)); #else - strncpy (szMsg, msg_string.c_str (), - std::min ((int) msg_string.length (), 8095)); + strncpy(szMsg, msg_string.c_str(), + std::min((int) msg_string.length(), 8095)); #endif - } - else - { - strcpy (szMsg, - "Cannot start game, some player(s) are not in a network game slot!"); - } - - serverInterface->sendTextMessage (szMsg, -1, true, - languageList[j]); - } - - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - return; - } - } - - if (dataSynchCheckOk == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - mainMessageBoxState = 1; - showMessageBox - ("You cannot start the game because\none or more clients do not have the same game data!", - "Data Mismatch Error", false); - - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - return; - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - if ((hasNetworkGameSettings () == true - && needToSetChangedGameSettings == true) || (RandomCount > 0)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - serverInterface->setGameSettings (&gameSettings, true); - serverInterface->broadcastGameSetup (&gameSettings); - - needToSetChangedGameSettings = false; - lastSetChangedGameSettings = time (NULL); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// Last check, stop human player from being in same slot as network - if (isMasterserverMode () == false) - { - bool hasHuman = false; - for (int i = 0; i < mapInfo.players; ++i) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// Check for random faction selection and choose the faction now - if (listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - hasHuman = true; - break; - } - } - if (hasHuman == false) - { - mainMessageBoxState = 1; - - Lang & lang = Lang::getInstance (); - string sMsg = - lang.getString ("NetworkSlotNoHumanErrorUI", "", true); - showMessageBox (sMsg, "", false); - - const vector < string > languageList = - serverInterface-> - getGameSettings ()->getUniqueNetworkPlayerLanguages (); - for (unsigned int j = 0; j < languageList.size (); ++j) - { - sMsg = lang.getString ("NetworkSlotNoHumanError", "", true); - - serverInterface->sendTextMessage (sMsg, -1, true, - languageList[j]); - } - - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - return; - } - } - -// Tell the server Interface whether or not to publish game status updates to masterserver - serverInterface->setNeedToRepublishToMasterserver - (checkBoxPublishServer.getValue () == true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - bool bOkToStart = serverInterface->launchGame (&gameSettings); - if (bOkToStart == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (checkBoxPublishServer.getEditable () && - checkBoxPublishServer.getValue () == true) - { - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - needToRepublishToMasterserver = true; - lastMasterserverPublishing = 0; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - needToBroadcastServerSettings = false; - needToRepublishToMasterserver = false; - lastNetworkPing = time (NULL); - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - assert (program != NULL); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - cleanup (); - Game *newGame = - new Game (program, &gameSettings, this->headlessServerMode); - forceWaitForShutdown = false; - program->setState (newGame); - return; - } - else - { - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - } - } - } - - void MenuStateCustomGame::mouseMove (int x, int y, const MouseState * ms) - { - if (isMasterserverMode () == true) - { - return; - } - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - buttonReturn.mouseMove (x, y); - buttonPlayNow.mouseMove (x, y); - buttonRestoreLastSettings.mouseMove (x, y); - buttonClearBlockedPlayers.mouseMove (x, y); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxRMultiplier[i].mouseMove (x, y); - listBoxControls[i].mouseMove (x, y); - buttonBlockPlayers[i].mouseMove (x, y); - listBoxFactions[i].mouseMove (x, y); - listBoxTeams[i].mouseMove (x, y); - } - - listBoxMap.mouseMove (x, y); - - if (checkBoxAdvanced.getValue () == 1) - { - listBoxFogOfWar.mouseMove (x, y); - checkBoxAllowObservers.mouseMove (x, y); - - checkBoxEnableSwitchTeamMode.mouseMove (x, y); - listBoxAISwitchTeamAcceptPercent.mouseMove (x, y); - listBoxFallbackCpuMultiplier.mouseMove (x, y); - - labelNetworkPauseGameForLaggedClients.mouseMove (x, y); - checkBoxNetworkPauseGameForLaggedClients.mouseMove (x, y); - - labelAllowTeamUnitSharing.mouseMove (x, y); - checkBoxAllowTeamUnitSharing.mouseMove (x, y); - labelAllowTeamResourceSharing.mouseMove (x, y); - checkBoxAllowTeamResourceSharing.mouseMove (x, y); - } - checkBoxAllowInGameJoinPlayer.mouseMove (x, y); - - checkBoxAllowNativeLanguageTechtree.mouseMove (x, y); - - listBoxTileset.mouseMove (x, y); - listBoxMapFilter.mouseMove (x, y); - listBoxTechTree.mouseMove (x, y); - checkBoxPublishServer.mouseMove (x, y); - - checkBoxAdvanced.mouseMove (x, y); - - checkBoxScenario.mouseMove (x, y); - listBoxScenario.mouseMove (x, y); - } - - bool MenuStateCustomGame::isMasterserverMode () const - { - return (this->headlessServerMode == true - && this->masterserverModeMinimalResources == true); -//return false; - } - - bool MenuStateCustomGame::isVideoPlaying () - { - bool result = false; - if (factionVideo != NULL) - { - result = factionVideo->isPlaying (); - } - return result; - } - - void MenuStateCustomGame::render () - { - try - { - Renderer & renderer = Renderer::getInstance (); - - if (mainMessageBox.getEnabled () == false) - { - if (factionTexture != NULL) - { - if (factionVideo == NULL || factionVideo->isPlaying () == false) - { - renderer.renderTextureQuad (800, 600, 200, 150, - factionTexture, 1.0f); - } - } - } - if (factionVideo != NULL) - { - if (factionVideo->isPlaying () == true) - { - factionVideo->playFrame (false); - } - else - { - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == - false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface != NULL) - { - initFactionPreview (serverInterface->getGameSettings ()); - } - } - } - } - } - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - - renderer.renderButton (&buttonReturn); - } - else - { - if (mapPreviewTexture != NULL) - { -//renderer.renderTextureQuad(5,185,150,150,mapPreviewTexture,1.0f); - renderer.renderTextureQuad (this->render_mapPreviewTexture_X, - this->render_mapPreviewTexture_Y, - this->render_mapPreviewTexture_W, - this->render_mapPreviewTexture_H, - mapPreviewTexture, 1.0f); - if (this->zoomedMap == true) - { - return; - } -//printf("=================> Rendering map preview texture\n"); - } - if (scenarioLogoTexture != NULL) - { - renderer.renderTextureQuad (300, 350, 400, 300, - scenarioLogoTexture, 1.0f); -//renderer.renderBackground(scenarioLogoTexture); - } - - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonPlayNow); - renderer.renderButton (&buttonRestoreLastSettings); - -// Get a reference to the player texture cache - std::map < int, Texture2D * >&crcPlayerTextureCache = - CacheManager::getCachedItem < std::map < int, - Texture2D * > >(GameConstants::playerTextureCacheLookupKey); - -// START - this code ensure player title and player names don't overlap - int offsetPosition = 0; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - FontMetrics *fontMetrics = NULL; - if (Renderer::renderText3DEnabled == false) - { - fontMetrics = labelPlayers[i].getFont ()->getMetrics (); - } - else - { - fontMetrics = labelPlayers[i].getFont3D ()->getMetrics (); - } - if (fontMetrics == NULL) - { - throw megaglest_runtime_error ("fontMetrics == NULL"); - } - int curWidth = - (fontMetrics->getTextWidth (labelPlayers[i].getText ())); - int newOffsetPosition = labelPlayers[i].getX () + curWidth + 2; - -//printf("labelPlayers[i].getX() = %d curWidth = %d labelPlayerNames[i].getX() = %d offsetPosition = %d newOffsetPosition = %d [%s]\n",labelPlayers[i].getX(),curWidth,labelPlayerNames[i].getX(),offsetPosition,newOffsetPosition,labelPlayers[i].getText().c_str()); - - if (labelPlayers[i].getX () + curWidth >= - labelPlayerNames[i].getX ()) - { - if (offsetPosition < newOffsetPosition) - { - offsetPosition = newOffsetPosition; - } - } - } -// END - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (hasNetworkGameSettings () == true) - { - renderer.renderListBox (&listBoxPlayerStatus); - if (serverInterface != NULL && - serverInterface->getServerSocket () != NULL) - { - renderer.renderButton (&buttonClearBlockedPlayers); - } - } - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { -//printf("Player #%d [%s] control = %d\n",i,labelPlayerNames[i].getText().c_str(),listBoxControls[i].getSelectedItemIndex()); - labelPlayers[i].setVisible (true); - labelPlayerNames[i].setVisible (true); - listBoxControls[i].setVisible (true); - listBoxFactions[i].setVisible (true); - listBoxTeams[i].setVisible (true); - labelNetStatus[i].setVisible (true); - } - - if (hasNetworkGameSettings () == true && - listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - - renderer.renderLabel (&labelPlayerStatus[i]); - } - - if (crcPlayerTextureCache[i] != NULL) - { -// Render the player # label the player's color - Vec3f playerColor = - crcPlayerTextureCache[i]->getPixmap ()->getPixel3f (0, - 0); - renderer.renderLabel (&labelPlayers[i], &playerColor); - } - else - { - renderer.renderLabel (&labelPlayers[i]); - } - - if (offsetPosition > 0) - { - labelPlayerNames[i].setX (offsetPosition); - } - renderer.renderLabel (&labelPlayerNames[i]); - - renderer.renderListBox (&listBoxControls[i]); - - if (hasNetworkGameSettings () == true && - listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - - renderer.renderLabel (&labelPlayerStatus[i]); - - if (listBoxControls[i].getSelectedItemIndex () == - ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface != NULL - && serverInterface->getSlot (i, true) != NULL - && serverInterface->getSlot (i, true)->isConnected ()) - { - renderer.renderButton (&buttonBlockPlayers[i]); - } - } - } - - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - renderer.renderListBox (&listBoxRMultiplier[i]); - renderer.renderListBox (&listBoxFactions[i]); - - int teamnumber = listBoxTeams[i].getSelectedItemIndex (); - Vec3f teamcolor = Vec3f (1.0f, 1.0f, 1.0f); - if (teamnumber >= 0 && teamnumber < 8) - { - teamcolor = - crcPlayerTextureCache[teamnumber]-> - getPixmap ()->getPixel3f (0, 0); - } - listBoxTeams[i].setTextColor (teamcolor); - - renderer.renderListBox (&listBoxTeams[i]); - renderer.renderLabel (&labelNetStatus[i]); - } - } - - renderer.renderLabel (&labelLocalGameVersion); - renderer.renderLabel (&labelLocalIP); - renderer.renderLabel (&labelMap); - - if (checkBoxAdvanced.getValue () == 1) - { - renderer.renderLabel (&labelFogOfWar); - renderer.renderLabel (&labelAllowObservers); - renderer.renderLabel (&labelFallbackCpuMultiplier); - - renderer.renderLabel (&labelEnableSwitchTeamMode); - renderer.renderLabel (&labelAISwitchTeamAcceptPercent); - - renderer.renderListBox (&listBoxFogOfWar); - renderer.renderCheckBox (&checkBoxAllowObservers); - - renderer.renderCheckBox (&checkBoxEnableSwitchTeamMode); - renderer.renderListBox (&listBoxAISwitchTeamAcceptPercent); - renderer.renderListBox (&listBoxFallbackCpuMultiplier); - - renderer.renderLabel (&labelAllowTeamUnitSharing); - renderer.renderCheckBox (&checkBoxAllowTeamUnitSharing); - - renderer.renderLabel (&labelAllowTeamResourceSharing); - renderer.renderCheckBox (&checkBoxAllowTeamResourceSharing); - } - renderer.renderLabel (&labelAllowInGameJoinPlayer); - renderer.renderCheckBox (&checkBoxAllowInGameJoinPlayer); - - renderer.renderLabel (&labelTileset); - renderer.renderLabel (&labelMapFilter); - renderer.renderLabel (&labelTechTree); - renderer.renderLabel (&labelControl); - renderer.renderLabel (&labelFaction); - renderer.renderLabel (&labelTeam); - renderer.renderLabel (&labelMapInfo); - renderer.renderLabel (&labelAdvanced); - - renderer.renderListBox (&listBoxMap); - renderer.renderListBox (&listBoxTileset); - renderer.renderListBox (&listBoxMapFilter); - renderer.renderListBox (&listBoxTechTree); - renderer.renderCheckBox (&checkBoxAdvanced); - - if (checkBoxPublishServer.getEditable ()) - { - renderer.renderCheckBox (&checkBoxPublishServer); - renderer.renderLabel (&labelPublishServer); - renderer.renderLabel (&labelGameName); - if (checkBoxAdvanced.getValue () == 1) - { - renderer.renderLabel (&labelNetworkPauseGameForLaggedClients); - renderer.renderCheckBox - (&checkBoxNetworkPauseGameForLaggedClients); - } - } - - renderer.renderCheckBox (&checkBoxScenario); - renderer.renderLabel (&labelScenario); - if (checkBoxScenario.getValue () == true) - { - renderer.renderListBox (&listBoxScenario); - } - - renderer.renderLabel (&labelAllowNativeLanguageTechtree); - renderer.renderCheckBox (&checkBoxAllowNativeLanguageTechtree); - } - - if (program != NULL) - program->renderProgramMsgBox (); - - if (enableMapPreview == true && mapPreview.hasFileLoaded () == true) - { - - if (mapPreviewTexture == NULL) - { - bool renderAll = (listBoxFogOfWar.getSelectedItemIndex () == 2); -//printf("=================> Rendering map preview into a texture BEFORE (%p)\n", mapPreviewTexture); - -//renderer.renderMapPreview(&mapPreview, renderAll, 10, 350,&mapPreviewTexture); - renderer.renderMapPreview (&mapPreview, renderAll, - this->render_mapPreviewTexture_X, - this->render_mapPreviewTexture_Y, - &mapPreviewTexture); - -//printf("=================> Rendering map preview into a texture AFTER (%p)\n", mapPreviewTexture); - } - } - - if (mainMessageBox.getEnabled () == false) - { - if (hasNetworkGameSettings () == true) - { - renderer.renderChatManager (&chatManager); - } - } - renderer.renderConsole (&console, - showFullConsole ? consoleFull : - consoleStoredAndNormal); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); -//throw megaglest_runtime_error(szBuf); - - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - showGeneralError = true; - generalErrorToShow = szBuf; - } - } - - void MenuStateCustomGame::switchSetupForSlots (SwitchSetupRequest ** - switchSetupRequests, - ServerInterface * - &serverInterface, - int startIndex, - int endIndex, - bool onlyNetworkUnassigned) - { - for (int i = startIndex; i < endIndex; ++i) - { - if (switchSetupRequests[i] != NULL) - { -//printf("Switch slot = %d control = %d newIndex = %d currentindex = %d onlyNetworkUnassigned = %d\n",i,listBoxControls[i].getSelectedItemIndex(),switchSetupRequests[i]->getToFactionIndex(),switchSetupRequests[i]->getCurrentFactionIndex(),onlyNetworkUnassigned); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - switchSetupRequests[i]->getSwitchFlags - ()); - - if (onlyNetworkUnassigned == true - && listBoxControls[i].getSelectedItemIndex () != - ctNetworkUnassigned) - { - if (i < mapInfo.players - || (i >= mapInfo.players - && listBoxControls[i].getSelectedItemIndex () != - ctNetwork)) - { - continue; - } - } - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - switchSetupRequests[i]->getToSlotIndex - ()); - - if (switchSetupRequests[i]->getToSlotIndex () != -1) - { - int newFactionIdx = switchSetupRequests[i]->getToSlotIndex (); - -//printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex()); - int switchFactionIdx = - switchSetupRequests[i]->getCurrentSlotIndex (); - if (serverInterface->switchSlot - (switchFactionIdx, newFactionIdx)) - { - try - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = - serverInterface->getSlot (newFactionIdx, true); - - if (switchSetupRequests[i]->getSelectedFactionName () != "" - && (slot != NULL - && switchSetupRequests[i]->getSelectedFactionName () - != Lang::getInstance ().getString ("DataMissing", - slot->getNetworkPlayerLanguage - (), true) - && switchSetupRequests[i]->getSelectedFactionName () - != "???DataMissing???")) - { - // I don't believe we need to check to see if Observers - // are allowed. If it's not, there should be not button on the client - // side that would allow them to switch to a slot > mapInfo.hardMaxPlayers - if (newFactionIdx <= mapInfo.hardMaxPlayers) - { - - listBoxFactions[newFactionIdx].setSelectedItem - (switchSetupRequests[i]->getSelectedFactionName ()); - } - else - { - listBoxFactions[newFactionIdx].setSelectedItem (GameConstants::OBSERVER_SLOTNAME); - } - - } - if (switchSetupRequests[i]->getToTeam () != -1) - { - if (newFactionIdx <= mapInfo.hardMaxPlayers) - { - listBoxTeams[newFactionIdx].setSelectedItemIndex - (switchSetupRequests[i]->getToTeam ()); - } - else - { - listBoxTeams[i].setSelectedItem (intToStr (GameConstants::maxPlayers + - fpt_Observer)); - } - } - if (switchSetupRequests[i]->getNetworkPlayerName () != "") - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, i, - labelPlayerNames - [newFactionIdx].getText - ().c_str (), - switchSetupRequests - [i]->getNetworkPlayerName (). - c_str ()); - labelPlayerNames[newFactionIdx].setText - (switchSetupRequests[i]->getNetworkPlayerName ()); - } - - if (listBoxControls[switchFactionIdx].getSelectedItemIndex - () == ctNetworkUnassigned) - { - serverInterface->removeSlot (switchFactionIdx); - listBoxControls[switchFactionIdx].setSelectedItemIndex - (ctClosed); - - labelPlayers[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - labelPlayerNames[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - listBoxControls[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - listBoxFactions[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - listBoxTeams[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - labelNetStatus[switchFactionIdx].setVisible - (switchFactionIdx + 1 <= mapInfo.players); - } - } - catch (const runtime_error & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - e.what ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] caught exception error = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - e.what ()); - } - } - } - else - { - try - { - int factionIdx = - switchSetupRequests[i]->getCurrentSlotIndex (); - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = - serverInterface->getSlot (factionIdx, true); - - if (switchSetupRequests[i]->getSelectedFactionName () != "" - && (slot != NULL - && switchSetupRequests[i]->getSelectedFactionName () - != Lang::getInstance ().getString ("DataMissing", - slot->getNetworkPlayerLanguage - (), true) - && switchSetupRequests[i]->getSelectedFactionName () - != "???DataMissing???")) - { - listBoxFactions[i].setSelectedItem (switchSetupRequests - [i]->getSelectedFactionName - ()); - } - if (switchSetupRequests[i]->getToTeam () != -1) - { - listBoxTeams[i].setSelectedItemIndex (switchSetupRequests - [i]->getToTeam ()); - } - - if ((switchSetupRequests[i]->getSwitchFlags () & - ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, i, - switchSetupRequests - [i]->getSwitchFlags (), - switchSetupRequests - [i]->getNetworkPlayerName (). - c_str (), - labelPlayerNames[i].getText (). - c_str ()); - - if (switchSetupRequests[i]->getNetworkPlayerName () != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - labelPlayerNames[i].setText (switchSetupRequests - [i]->getNetworkPlayerName - ()); - } - else - { - labelPlayerNames[i].setText (""); - } - } - } - catch (const runtime_error & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, e.what ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] caught exception error = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, e.what ()); - } - } - } - - delete switchSetupRequests[i]; - switchSetupRequests[i] = NULL; - } - } - } - - void MenuStateCustomGame::initFactionPreview (const GameSettings * - gameSettings) - { - string factionVideoUrl = ""; - string factionVideoUrlFallback = ""; - - string factionDefinitionXML = - Game::findFactionLogoFile (gameSettings, NULL, - currentFactionName_factionPreview + - ".xml"); - if (factionDefinitionXML != "" - && currentFactionName_factionPreview != - GameConstants::RANDOMFACTION_SLOTNAME - && currentFactionName_factionPreview != - GameConstants::OBSERVER_SLOTNAME - && fileExists (factionDefinitionXML) == true) - { - XmlTree xmlTree; - std::map < string, string > mapExtraTagReplacementValues; - xmlTree.load (factionDefinitionXML, - Properties::getTagReplacementValues - (&mapExtraTagReplacementValues)); - const XmlNode *factionNode = xmlTree.getRootNode (); - if (factionNode->hasAttribute ("faction-preview-video") == true) - { - factionVideoUrl = - factionNode->getAttribute ("faction-preview-video")->getValue (); - } - - factionVideoUrlFallback = - Game::findFactionLogoFile (gameSettings, NULL, "preview_video.*"); - if (factionVideoUrl == "") - { - factionVideoUrl = factionVideoUrlFallback; - factionVideoUrlFallback = ""; - } - } -//printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); - - - if (factionVideoUrl != "") - { -//SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - if (CoreData::getInstance ().getMenuMusic ()->getVolume () != 0) - { - CoreData::getInstance ().getMenuMusic ()->setVolume (0); - factionVideoSwitchedOffVolume = true; - } - - if (currentFactionLogo != factionVideoUrl) - { - currentFactionLogo = factionVideoUrl; - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - &&::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer () == - true) - { - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - string introVideoFile = factionVideoUrl; - string introVideoFileFallback = factionVideoUrlFallback; - - Context *c = - GraphicsInterface::getInstance ().getCurrentContext (); - SDL_Window *window = - static_cast < - ContextGl * - >(c)->getPlatformContextGlPtr ()->getScreenWindow (); - SDL_Surface *screen = - static_cast < - ContextGl * - >(c)->getPlatformContextGlPtr ()->getScreenSurface (); - - string vlcPluginsPath = - Config::getInstance ().getString ("VideoPlayerPluginsPath", ""); -//printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); - factionVideo = new VideoPlayer (&Renderer::getInstance (), - introVideoFile, - introVideoFileFallback, - window, - 0, 0, - screen->w, - screen->h, - screen->format->BitsPerPixel, - true, vlcPluginsPath, - SystemFlags::VERBOSE_MODE_ENABLED); - factionVideo->initPlayer (); - } - } - } - else - { -//SoundRenderer &soundRenderer= SoundRenderer::getInstance(); -//switch on music again!! - Config & config = Config::getInstance (); - float configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - if (factionVideoSwitchedOffVolume) - { - if (CoreData::getInstance ().getMenuMusic ()->getVolume () != - configVolume) - { - CoreData::getInstance (). - getMenuMusic ()->setVolume (configVolume); - } - factionVideoSwitchedOffVolume = false; - } - - if (factionVideo != NULL) - { - factionVideo->closePlayer (); - delete factionVideo; - factionVideo = NULL; - } - } - - if (factionVideo == NULL) - { - string factionLogo = Game::findFactionLogoFile (gameSettings, NULL, - GameConstants::PREVIEW_SCREEN_FILE_FILTER); - if (factionLogo == "") - { - factionLogo = Game::findFactionLogoFile (gameSettings, NULL); - } - if (currentFactionLogo != factionLogo) - { - currentFactionLogo = factionLogo; - loadFactionTexture (currentFactionLogo); - } - } - } - - void MenuStateCustomGame::publishToMasterserver () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - int slotCountUsed = 0; - int slotCountHumans = 0; - int slotCountConnectedPlayers = 0; - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - GameSettings gameSettings; - loadGameSettings (&gameSettings); - Config & config = Config::getInstance (); -//string serverinfo=""; - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - publishToServerInfo.clear (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - slotCountUsed++; - } - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - slotCountHumans++; - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, true)->isConnected ()) - { - slotCountConnectedPlayers++; - } - } - else if (listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - slotCountHumans++; - slotCountConnectedPlayers++; - } - } - - publishToServerInfo["uuid"] = - Config::getInstance ().getString ("PlayerId", ""); - -//?status=waiting&system=linux&info=titus - publishToServerInfo["glestVersion"] = glestVersionString; - publishToServerInfo["platform"] = - getPlatformNameString (); - publishToServerInfo["binaryCompileDate"] = getCompileDateTime (); - -//game info: - publishToServerInfo["serverTitle"] = gameSettings.getGameName (); -//ip is automatically set - -//game setup info: - -//publishToServerInfo["tech"] = listBoxTechTree.getSelectedItem(); - publishToServerInfo["tech"] = - techTreeFiles[listBoxTechTree.getSelectedItemIndex ()]; -//publishToServerInfo["map"] = listBoxMap.getSelectedItem(); - publishToServerInfo["map"] = getCurrentMapFile (); -//publishToServerInfo["tileset"] = listBoxTileset.getSelectedItem(); - publishToServerInfo["tileset"] = - tilesetFiles[listBoxTileset.getSelectedItemIndex ()]; - - publishToServerInfo["activeSlots"] = intToStr (slotCountUsed); - publishToServerInfo["networkSlots"] = intToStr (slotCountHumans); - publishToServerInfo["connectedClients"] = - intToStr (slotCountConnectedPlayers); - - string serverPort = config.getString ("PortServer", - intToStr - (GameConstants:: - serverPort).c_str ()); - string externalPort = - config.getString ("PortExternal", serverPort.c_str ()); - publishToServerInfo["externalconnectport"] = externalPort; - publishToServerInfo["privacyPlease"] = - intToStr (config.getBool ("PrivacyPlease", "false")); - - publishToServerInfo["gameStatus"] = - intToStr (game_status_waiting_for_players); - if (slotCountHumans <= slotCountConnectedPlayers) - { - publishToServerInfo["gameStatus"] = - intToStr (game_status_waiting_for_start); - } - - publishToServerInfo["gameUUID"] = gameSettings.getGameUUID (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::setupTask (BaseThread * callingThread, - void *userdata) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\nsetupTask callingThread [%p] userdata [%p]\n", - callingThread, userdata); - if (userdata != NULL) - { - int value = *((int *) &userdata); - THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; -//printf("\n\nsetupTask callingThread [%p] userdata [%p]\n",callingThread,userdata); - if (threadType == tnt_MASTERSERVER) - { - MenuStateCustomGame::setupTaskStatic (callingThread); - } - } - } - void MenuStateCustomGame::shutdownTask (BaseThread * callingThread, - void *userdata) - { -//printf("\n\nshutdownTask callingThread [%p] userdata [%p]\n",callingThread,userdata); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("\n\nshutdownTask callingThread [%p] userdata [%p]\n", - callingThread, userdata); - if (userdata != NULL) - { - int value = *((int *) &userdata); - THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; -//printf("\n\nshutdownTask callingThread [%p] userdata [%p]\n",callingThread,userdata); - if (threadType == tnt_MASTERSERVER) - { - MenuStateCustomGame::shutdownTaskStatic (callingThread); - } - } - } - void MenuStateCustomGame::setupTaskStatic (BaseThread * callingThread) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - - CURL *handle = SystemFlags::initHTTP (); - callingThread->setGenericData < CURL > (handle); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - void MenuStateCustomGame::shutdownTaskStatic (BaseThread * callingThread) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - -//printf("LINE: %d\n",__LINE__); - CURL *handle = callingThread->getGenericData < CURL > (); - SystemFlags::cleanupHTTP (&handle); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - } - - void MenuStateCustomGame::simpleTask (BaseThread * callingThread, - void *userdata) - { -//printf("\n\nSimple Task callingThread [%p] userdata [%p]\n",callingThread,userdata); - int value = *((int *) &userdata); -//printf("\n\nSimple Task callingThread [%p] userdata [%p] value = %d\n",callingThread,userdata,value); - - THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; - if (threadType == tnt_MASTERSERVER) - { - simpleTaskForMasterServer (callingThread); - } - else if (threadType == tnt_CLIENTS) - { - simpleTaskForClients (callingThread); - } - } - - void MenuStateCustomGame::simpleTaskForMasterServer (BaseThread * - callingThread) - { - try - { -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - A\n"); - - MutexSafeWrapper - safeMutexThreadOwner (callingThread->getMutexThreadOwnerValid (), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - return; - } - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - B\n"); - - MutexSafeWrapper - safeMutex (callingThread->getMutexThreadObjectAccessor (), - string (__FILE__) + "_" + intToStr (__LINE__)); - bool republish = (needToRepublishToMasterserver == true - && publishToServerInfo.empty () == false); - needToRepublishToMasterserver = false; - std::map < string, string > newPublishToServerInfo = - publishToServerInfo; - publishToServerInfo.clear (); - -//printf("simpleTask broadCastSettings = %d\n",broadCastSettings); - - if (callingThread->getQuitStatus () == true) - { - return; - } - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - C\n"); - - if (republish == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - string request = Config::getInstance ().getString ("Masterserver"); - if (request != "") - { - endPathWithSlash (request, false); - } - request += "addServerInfo.php?"; - -//CURL *handle = SystemFlags::initHTTP(); - CURL *handle = callingThread->getGenericData < CURL > (); - - int paramIndex = 0; - for (std::map < string, string >::const_iterator iterMap = - newPublishToServerInfo.begin (); - iterMap != newPublishToServerInfo.end (); ++iterMap) - { - - request += iterMap->first; - request += "="; - request += SystemFlags::escapeURL (iterMap->second, handle); - - paramIndex++; - if (paramIndex < (int) newPublishToServerInfo.size ()) - { - request += "&"; - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("The Lobby request is:\n%s\n", request.c_str ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] the request is:\n%s\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - request.c_str ()); - safeMutex.ReleaseLock (true); - safeMutexThreadOwner.ReleaseLock (); - - std::string serverInfo = SystemFlags::getHTTP (request, handle); -//SystemFlags::cleanupHTTP(&handle); - - MutexSafeWrapper - safeMutexThreadOwner2 (callingThread->getMutexThreadOwnerValid (), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner2.isValidMutex () == false) - { - return; - } - safeMutex.Lock (); - -//printf("the result is:\n'%s'\n",serverInfo.c_str()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] the result is:\n'%s'\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - serverInfo.c_str ()); - -// uncomment to enable router setup check of this server - if (EndsWith (serverInfo, "OK") == false) - { - if (callingThread->getQuitStatus () == true) - { - return; - } - -// Give things another chance to see if we can get a connection from the master server - if (tMasterserverErrorElapsed > 0 && - difftime ((long int) time (NULL), - tMasterserverErrorElapsed) > - MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS) - { - showMasterserverError = true; - masterServererErrorToShow = - (serverInfo != "" ? serverInfo : "No Reply"); - } - else - { - if (tMasterserverErrorElapsed == 0) - { - tMasterserverErrorElapsed = time (NULL); - } - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line %d] error checking response from masterserver elapsed seconds = %.2f / %d\nResponse:\n%s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, __LINE__, - difftime ((long int) - time (NULL), - tMasterserverErrorElapsed), - MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS, - serverInfo.c_str ()); - - needToRepublishToMasterserver = true; - } - } - } - else - { - safeMutexThreadOwner.ReleaseLock (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - D\n"); - - safeMutex.ReleaseLock (); - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - F\n"); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - if (callingThread->getQuitStatus () == false) - { -//throw megaglest_runtime_error(szBuf); - showGeneralError = true; - generalErrorToShow = ex.what (); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::simpleTaskForClients (BaseThread * - callingThread) - { - try - { -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - A\n"); - - MutexSafeWrapper - safeMutexThreadOwner (callingThread->getMutexThreadOwnerValid (), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - return; - } - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - B\n"); - - MutexSafeWrapper - safeMutex (callingThread->getMutexThreadObjectAccessor (), - string (__FILE__) + "_" + intToStr (__LINE__)); - bool broadCastSettings = needToBroadcastServerSettings; - -//printf("simpleTask broadCastSettings = %d\n",broadCastSettings); - - needToBroadcastServerSettings = false; - bool hasClientConnection = false; - - if (broadCastSettings == true) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (false); - if (serverInterface != NULL) - { - hasClientConnection = serverInterface->hasClientConnection (); - } - } - bool needPing = - (difftime ((long int) time (NULL), lastNetworkPing) >= - GameConstants::networkPingInterval); - - if (callingThread->getQuitStatus () == true) - { - return; - } - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - C\n"); - - safeMutexThreadOwner.ReleaseLock (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - D\n"); - - if (broadCastSettings == true) - { - MutexSafeWrapper - safeMutexThreadOwner2 (callingThread->getMutexThreadOwnerValid (), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner2.isValidMutex () == false) - { - return; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - -//printf("simpleTask broadCastSettings = %d hasClientConnection = %d\n",broadCastSettings,hasClientConnection); - - if (callingThread->getQuitStatus () == true) - { - return; - } - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (false); - if (serverInterface != NULL) - { - lastGameSettingsreceivedCount++; - if (this->headlessServerMode == false - || (serverInterface->getGameSettingsUpdateCount () <= - lastMasterServerSettingsUpdateCount)) - { - GameSettings gameSettings; - loadGameSettings (&gameSettings); - -//printf("\n\n\n\n=====#2 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),gameSettings.toString().c_str()); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - serverInterface->setGameSettings (&gameSettings, false); - lastMasterServerSettingsUpdateCount = - serverInterface->getGameSettingsUpdateCount (); - - if (hasClientConnection == true) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - serverInterface->broadcastGameSetup (&gameSettings); - } - } - } - } - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - E\n"); - - if (needPing == true) - { - MutexSafeWrapper - safeMutexThreadOwner2 (callingThread->getMutexThreadOwnerValid (), - string (__FILE__) + "_" + - intToStr (__LINE__)); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner2.isValidMutex () == false) - { - return; - } - - lastNetworkPing = time (NULL); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] Sending nmtPing to clients\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (false); - if (serverInterface != NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - NetworkMessagePing *msg = - new NetworkMessagePing (GameConstants::networkPingInterval, - time (NULL)); -//serverInterface->broadcastPing(&msg); - serverInterface->queueBroadcastMessage (msg); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - } - safeMutex.ReleaseLock (); - -//printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - F\n"); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - if (callingThread->getQuitStatus () == false) - { -//throw megaglest_runtime_error(szBuf); - showGeneralError = true; - generalErrorToShow = ex.what (); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::loadGameSettings (GameSettings * gameSettings, - bool forceCloseUnusedSlots) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - int factionCount = 0; - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (this->headlessServerMode == true - && serverInterface->getGameSettingsUpdateCount () > - lastMasterServerSettingsUpdateCount - && serverInterface->getGameSettings () != NULL) - { - const GameSettings *settings = serverInterface->getGameSettings (); -//printf("\n\n\n\n=====#3 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),settings->toString().c_str()); - - lastMasterServerSettingsUpdateCount = - serverInterface->getGameSettingsUpdateCount (); -//printf("#1 custom menu got map [%s]\n",settings->getMap().c_str()); - - setupUIFromGameSettings (*settings); - } - - gameSettings->setGameName (labelGameName.getText ()); - -// Test flags values -//gameSettings->setFlagTypes1(ft1_show_map_resources); -// - - if (checkBoxScenario.getValue () == true) - { - gameSettings->setScenario (scenarioInfo.name); - gameSettings->setScenarioDir (Scenario::getScenarioPath - (dirList, scenarioInfo.name)); - - gameSettings->setDefaultResources (scenarioInfo.defaultResources); - gameSettings->setDefaultUnits (scenarioInfo.defaultUnits); - gameSettings-> - setDefaultVictoryConditions (scenarioInfo.defaultVictoryConditions); - } - else - { - gameSettings->setScenario (""); - gameSettings->setScenarioDir (""); - } - - gameSettings->setGameUUID (this->gameUUID); - -//printf("scenarioInfo.name [%s] [%s] [%s]\n",scenarioInfo.name.c_str(),listBoxMap.getSelectedItem().c_str(),getCurrentMapFile().c_str()); - - gameSettings->setMapFilter (listBoxMapFilter.getSelectedItemIndex ()); - gameSettings->setDescription (formatString (getCurrentMapFile ())); - gameSettings->setMap (getCurrentMapFile ()); - if (tilesetFiles.empty () == false) - { - gameSettings->setTileset (tilesetFiles - [listBoxTileset.getSelectedItemIndex ()]); - } - if (techTreeFiles.empty () == false) - { - gameSettings->setTech (techTreeFiles - [listBoxTechTree.getSelectedItemIndex ()]); - } - - if (autoStartSettings != NULL) - { - gameSettings->setDefaultUnits (autoStartSettings->getDefaultUnits ()); - gameSettings-> - setDefaultResources (autoStartSettings->getDefaultResources ()); - gameSettings-> - setDefaultVictoryConditions - (autoStartSettings->getDefaultVictoryConditions ()); - } - else if (checkBoxScenario.getValue () == false) - { - gameSettings->setDefaultUnits (true); - gameSettings->setDefaultResources (true); - gameSettings->setDefaultVictoryConditions (true); - } - - gameSettings->setFogOfWar (listBoxFogOfWar.getSelectedItemIndex () == 0 - || listBoxFogOfWar.getSelectedItemIndex () == - 1); - - gameSettings->setAllowObservers (checkBoxAllowObservers.getValue () == - 1); - - uint32 valueFlags1 = gameSettings->getFlagTypes1 (); - if (listBoxFogOfWar.getSelectedItemIndex () == 1 || - listBoxFogOfWar.getSelectedItemIndex () == 2) - { - valueFlags1 |= ft1_show_map_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_show_map_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - -//gameSettings->setEnableObserverModeAtEndGame(listBoxEnableObserverMode.getSelectedItemIndex() == 0); - gameSettings->setEnableObserverModeAtEndGame (true); -//gameSettings->setPathFinderType(static_cast(listBoxPathFinderType.getSelectedItemIndex())); - - valueFlags1 = gameSettings->getFlagTypes1 (); - if (checkBoxEnableSwitchTeamMode.getValue () == true) - { - valueFlags1 |= ft1_allow_team_switching; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_team_switching; - gameSettings->setFlagTypes1 (valueFlags1); - } - gameSettings->setAiAcceptSwitchTeamPercentChance (strToInt - (listBoxAISwitchTeamAcceptPercent.getSelectedItem - ())); - gameSettings-> - setFallbackCpuMultiplier - (listBoxFallbackCpuMultiplier.getSelectedItemIndex ()); - - if (checkBoxAllowInGameJoinPlayer.getValue () == true) - { - valueFlags1 |= ft1_allow_in_game_joining; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_in_game_joining; - gameSettings->setFlagTypes1 (valueFlags1); - } - - if (checkBoxAllowTeamUnitSharing.getValue () == true) - { - valueFlags1 |= ft1_allow_shared_team_units; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_shared_team_units; - gameSettings->setFlagTypes1 (valueFlags1); - } - - if (checkBoxAllowTeamResourceSharing.getValue () == true) - { - valueFlags1 |= ft1_allow_shared_team_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - else - { - valueFlags1 &= ~ft1_allow_shared_team_resources; - gameSettings->setFlagTypes1 (valueFlags1); - } - - if (Config::getInstance ().getBool ("EnableNetworkGameSynchChecks", - "false") == true) - { -//printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); - - valueFlags1 |= ft1_network_synch_checks_verbose; - gameSettings->setFlagTypes1 (valueFlags1); - - } - else - { - valueFlags1 &= ~ft1_network_synch_checks_verbose; - gameSettings->setFlagTypes1 (valueFlags1); - - } - if (Config::getInstance ().getBool ("EnableNetworkGameSynchMonitor", - "false") == true) - { -//printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); - - valueFlags1 |= ft1_network_synch_checks; - gameSettings->setFlagTypes1 (valueFlags1); - - } - else - { - valueFlags1 &= ~ft1_network_synch_checks; - gameSettings->setFlagTypes1 (valueFlags1); - - } - - gameSettings->setNetworkAllowNativeLanguageTechtree - (checkBoxAllowNativeLanguageTechtree.getValue ()); - -// First save Used slots -//for(int i=0; iheadlessServerMode == true) - { -// switch slot to network, because no human in headless mode - listBoxControls[i].setSelectedItemIndex (ctNetwork); - updateResourceMultiplier (i); - } - - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - - if (forceCloseUnusedSlots == true - && (ct == ctNetworkUnassigned || ct == ctNetwork)) - { - if (serverInterface != NULL && - (serverInterface->getSlot (i, true) == NULL || - serverInterface->getSlot (i, true)->isConnected () == false)) - { - if (checkBoxScenario.getValue () == false) - { -//printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); - - listBoxControls[i].setSelectedItemIndex (ctClosed); - ct = ctClosed; - } - } - } - else if (ct == ctNetworkUnassigned && i < mapInfo.players) - { - listBoxControls[i].setSelectedItemIndex (ctNetwork); - ct = ctNetwork; - } - - if (ct != ctClosed) - { - int slotIndex = factionCount; - gameSettings->setFactionControl (slotIndex, ct); - if (ct == ctHuman) - { - -// I'm putting this inside a ppd for now. I don't see it needs to be -// built in unless DEBUG is defined during building -andy5995 2018-01-26 + } else { + strcpy(szMsg, + "Cannot start game, some player(s) are not in a network game slot!"); + } + + serverInterface->sendTextMessage(szMsg, -1, true, + languageList[j]); + } + + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + return; + } + } + + if (dataSynchCheckOk == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + mainMessageBoxState = 1; + showMessageBox + ("You cannot start the game because\none or more clients do not have the same game data!", + "Data Mismatch Error", false); + + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + return; + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + if ((hasNetworkGameSettings() == true + && needToSetChangedGameSettings == true) || (RandomCount > 0)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + serverInterface->setGameSettings(&gameSettings, true); + serverInterface->broadcastGameSetup(&gameSettings); + + needToSetChangedGameSettings = false; + lastSetChangedGameSettings = time(NULL); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Last check, stop human player from being in same slot as network + if (isMasterserverMode() == false) { + bool hasHuman = false; + for (int i = 0; i < mapInfo.players; ++i) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Check for random faction selection and choose the faction now + if (listBoxControls[i].getSelectedItemIndex() == ctHuman) { + hasHuman = true; + break; + } + } + if (hasHuman == false) { + mainMessageBoxState = 1; + + Lang & lang = Lang::getInstance(); + string sMsg = + lang.getString("NetworkSlotNoHumanErrorUI", "", true); + showMessageBox(sMsg, "", false); + + const vector < string > languageList = + serverInterface-> + getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int j = 0; j < languageList.size(); ++j) { + sMsg = lang.getString("NetworkSlotNoHumanError", "", true); + + serverInterface->sendTextMessage(sMsg, -1, true, + languageList[j]); + } + + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + return; + } + } + + // Tell the server Interface whether or not to publish game status updates to masterserver + serverInterface->setNeedToRepublishToMasterserver + (checkBoxPublishServer.getValue() == true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + bool bOkToStart = serverInterface->launchGame(&gameSettings); + if (bOkToStart == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (checkBoxPublishServer.getEditable() && + checkBoxPublishServer.getValue() == true) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + needToRepublishToMasterserver = true; + lastMasterserverPublishing = 0; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + needToBroadcastServerSettings = false; + needToRepublishToMasterserver = false; + lastNetworkPing = time(NULL); + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + assert(program != NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + cleanup(); + Game *newGame = + new Game(program, &gameSettings, this->headlessServerMode); + forceWaitForShutdown = false; + program->setState(newGame); + return; + } else { + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + } + } + } + + void MenuStateCustomGame::mouseMove(int x, int y, const MouseState * ms) { + if (isMasterserverMode() == true) { + return; + } + + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + buttonReturn.mouseMove(x, y); + buttonPlayNow.mouseMove(x, y); + buttonRestoreLastSettings.mouseMove(x, y); + buttonClearBlockedPlayers.mouseMove(x, y); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxRMultiplier[i].mouseMove(x, y); + listBoxControls[i].mouseMove(x, y); + buttonBlockPlayers[i].mouseMove(x, y); + listBoxFactions[i].mouseMove(x, y); + listBoxTeams[i].mouseMove(x, y); + } + + listBoxMap.mouseMove(x, y); + + if (checkBoxAdvanced.getValue() == 1) { + listBoxFogOfWar.mouseMove(x, y); + checkBoxAllowObservers.mouseMove(x, y); + + checkBoxEnableSwitchTeamMode.mouseMove(x, y); + listBoxAISwitchTeamAcceptPercent.mouseMove(x, y); + listBoxFallbackCpuMultiplier.mouseMove(x, y); + + labelNetworkPauseGameForLaggedClients.mouseMove(x, y); + checkBoxNetworkPauseGameForLaggedClients.mouseMove(x, y); + + labelAllowTeamUnitSharing.mouseMove(x, y); + checkBoxAllowTeamUnitSharing.mouseMove(x, y); + labelAllowTeamResourceSharing.mouseMove(x, y); + checkBoxAllowTeamResourceSharing.mouseMove(x, y); + } + checkBoxAllowInGameJoinPlayer.mouseMove(x, y); + + checkBoxAllowNativeLanguageTechtree.mouseMove(x, y); + + listBoxTileset.mouseMove(x, y); + listBoxMapFilter.mouseMove(x, y); + listBoxTechTree.mouseMove(x, y); + checkBoxPublishServer.mouseMove(x, y); + + checkBoxAdvanced.mouseMove(x, y); + + checkBoxScenario.mouseMove(x, y); + listBoxScenario.mouseMove(x, y); + } + + bool MenuStateCustomGame::isMasterserverMode() const { + return (this->headlessServerMode == true + && this->masterserverModeMinimalResources == true); + //return false; + } + + bool MenuStateCustomGame::isVideoPlaying() { + bool result = false; + if (factionVideo != NULL) { + result = factionVideo->isPlaying(); + } + return result; + } + + void MenuStateCustomGame::render() { + try { + Renderer & renderer = Renderer::getInstance(); + + if (mainMessageBox.getEnabled() == false) { + if (factionTexture != NULL) { + if (factionVideo == NULL || factionVideo->isPlaying() == false) { + renderer.renderTextureQuad(800, 600, 200, 150, + factionTexture, 1.0f); + } + } + } + if (factionVideo != NULL) { + if (factionVideo->isPlaying() == true) { + factionVideo->playFrame(false); + } else { + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == + false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface != NULL) { + initFactionPreview(serverInterface->getGameSettings()); + } + } + } + } + } + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + + renderer.renderButton(&buttonReturn); + } else { + if (mapPreviewTexture != NULL) { + //renderer.renderTextureQuad(5,185,150,150,mapPreviewTexture,1.0f); + renderer.renderTextureQuad(this->render_mapPreviewTexture_X, + this->render_mapPreviewTexture_Y, + this->render_mapPreviewTexture_W, + this->render_mapPreviewTexture_H, + mapPreviewTexture, 1.0f); + if (this->zoomedMap == true) { + return; + } + //printf("=================> Rendering map preview texture\n"); + } + if (scenarioLogoTexture != NULL) { + renderer.renderTextureQuad(300, 350, 400, 300, + scenarioLogoTexture, 1.0f); + //renderer.renderBackground(scenarioLogoTexture); + } + + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonPlayNow); + renderer.renderButton(&buttonRestoreLastSettings); + + // Get a reference to the player texture cache + std::map < int, Texture2D * >&crcPlayerTextureCache = + CacheManager::getCachedItem < std::map < int, + Texture2D * > >(GameConstants::playerTextureCacheLookupKey); + + // START - this code ensure player title and player names don't overlap + int offsetPosition = 0; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + FontMetrics *fontMetrics = NULL; + if (Renderer::renderText3DEnabled == false) { + fontMetrics = labelPlayers[i].getFont()->getMetrics(); + } else { + fontMetrics = labelPlayers[i].getFont3D()->getMetrics(); + } + if (fontMetrics == NULL) { + throw megaglest_runtime_error("fontMetrics == NULL"); + } + int curWidth = + (fontMetrics->getTextWidth(labelPlayers[i].getText())); + int newOffsetPosition = labelPlayers[i].getX() + curWidth + 2; + + //printf("labelPlayers[i].getX() = %d curWidth = %d labelPlayerNames[i].getX() = %d offsetPosition = %d newOffsetPosition = %d [%s]\n",labelPlayers[i].getX(),curWidth,labelPlayerNames[i].getX(),offsetPosition,newOffsetPosition,labelPlayers[i].getText().c_str()); + + if (labelPlayers[i].getX() + curWidth >= + labelPlayerNames[i].getX()) { + if (offsetPosition < newOffsetPosition) { + offsetPosition = newOffsetPosition; + } + } + } + // END + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (hasNetworkGameSettings() == true) { + renderer.renderListBox(&listBoxPlayerStatus); + if (serverInterface != NULL && + serverInterface->getServerSocket() != NULL) { + renderer.renderButton(&buttonClearBlockedPlayers); + } + } + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + //printf("Player #%d [%s] control = %d\n",i,labelPlayerNames[i].getText().c_str(),listBoxControls[i].getSelectedItemIndex()); + labelPlayers[i].setVisible(true); + labelPlayerNames[i].setVisible(true); + listBoxControls[i].setVisible(true); + listBoxFactions[i].setVisible(true); + listBoxTeams[i].setVisible(true); + labelNetStatus[i].setVisible(true); + } + + if (hasNetworkGameSettings() == true && + listBoxControls[i].getSelectedItemIndex() != ctClosed) { + + renderer.renderLabel(&labelPlayerStatus[i]); + } + + if (crcPlayerTextureCache[i] != NULL) { + // Render the player # label the player's color + Vec3f playerColor = + crcPlayerTextureCache[i]->getPixmap()->getPixel3f(0, + 0); + renderer.renderLabel(&labelPlayers[i], &playerColor); + } else { + renderer.renderLabel(&labelPlayers[i]); + } + + if (offsetPosition > 0) { + labelPlayerNames[i].setX(offsetPosition); + } + renderer.renderLabel(&labelPlayerNames[i]); + + renderer.renderListBox(&listBoxControls[i]); + + if (hasNetworkGameSettings() == true && + listBoxControls[i].getSelectedItemIndex() != ctClosed) { + + renderer.renderLabel(&labelPlayerStatus[i]); + + if (listBoxControls[i].getSelectedItemIndex() == + ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface != NULL + && serverInterface->getSlot(i, true) != NULL + && serverInterface->getSlot(i, true)->isConnected()) { + renderer.renderButton(&buttonBlockPlayers[i]); + } + } + } + + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + renderer.renderListBox(&listBoxRMultiplier[i]); + renderer.renderListBox(&listBoxFactions[i]); + + int teamnumber = listBoxTeams[i].getSelectedItemIndex(); + Vec3f teamcolor = Vec3f(1.0f, 1.0f, 1.0f); + if (teamnumber >= 0 && teamnumber < 8) { + teamcolor = + crcPlayerTextureCache[teamnumber]-> + getPixmap()->getPixel3f(0, 0); + } + listBoxTeams[i].setTextColor(teamcolor); + + renderer.renderListBox(&listBoxTeams[i]); + renderer.renderLabel(&labelNetStatus[i]); + } + } + + renderer.renderLabel(&labelLocalGameVersion); + renderer.renderLabel(&labelLocalIP); + renderer.renderLabel(&labelMap); + + if (checkBoxAdvanced.getValue() == 1) { + renderer.renderLabel(&labelFogOfWar); + renderer.renderLabel(&labelAllowObservers); + renderer.renderLabel(&labelFallbackCpuMultiplier); + + renderer.renderLabel(&labelEnableSwitchTeamMode); + renderer.renderLabel(&labelAISwitchTeamAcceptPercent); + + renderer.renderListBox(&listBoxFogOfWar); + renderer.renderCheckBox(&checkBoxAllowObservers); + + renderer.renderCheckBox(&checkBoxEnableSwitchTeamMode); + renderer.renderListBox(&listBoxAISwitchTeamAcceptPercent); + renderer.renderListBox(&listBoxFallbackCpuMultiplier); + + renderer.renderLabel(&labelAllowTeamUnitSharing); + renderer.renderCheckBox(&checkBoxAllowTeamUnitSharing); + + renderer.renderLabel(&labelAllowTeamResourceSharing); + renderer.renderCheckBox(&checkBoxAllowTeamResourceSharing); + } + renderer.renderLabel(&labelAllowInGameJoinPlayer); + renderer.renderCheckBox(&checkBoxAllowInGameJoinPlayer); + + renderer.renderLabel(&labelTileset); + renderer.renderLabel(&labelMapFilter); + renderer.renderLabel(&labelTechTree); + renderer.renderLabel(&labelControl); + renderer.renderLabel(&labelFaction); + renderer.renderLabel(&labelTeam); + renderer.renderLabel(&labelMapInfo); + renderer.renderLabel(&labelAdvanced); + + renderer.renderListBox(&listBoxMap); + renderer.renderListBox(&listBoxTileset); + renderer.renderListBox(&listBoxMapFilter); + renderer.renderListBox(&listBoxTechTree); + renderer.renderCheckBox(&checkBoxAdvanced); + + if (checkBoxPublishServer.getEditable()) { + renderer.renderCheckBox(&checkBoxPublishServer); + renderer.renderLabel(&labelPublishServer); + renderer.renderLabel(&labelGameName); + if (checkBoxAdvanced.getValue() == 1) { + renderer.renderLabel(&labelNetworkPauseGameForLaggedClients); + renderer.renderCheckBox + (&checkBoxNetworkPauseGameForLaggedClients); + } + } + + renderer.renderCheckBox(&checkBoxScenario); + renderer.renderLabel(&labelScenario); + if (checkBoxScenario.getValue() == true) { + renderer.renderListBox(&listBoxScenario); + } + + renderer.renderLabel(&labelAllowNativeLanguageTechtree); + renderer.renderCheckBox(&checkBoxAllowNativeLanguageTechtree); + } + + if (program != NULL) + program->renderProgramMsgBox(); + + if (enableMapPreview == true && mapPreview.hasFileLoaded() == true) { + + if (mapPreviewTexture == NULL) { + bool renderAll = (listBoxFogOfWar.getSelectedItemIndex() == 2); + //printf("=================> Rendering map preview into a texture BEFORE (%p)\n", mapPreviewTexture); + + //renderer.renderMapPreview(&mapPreview, renderAll, 10, 350,&mapPreviewTexture); + renderer.renderMapPreview(&mapPreview, renderAll, + this->render_mapPreviewTexture_X, + this->render_mapPreviewTexture_Y, + &mapPreviewTexture); + + //printf("=================> Rendering map preview into a texture AFTER (%p)\n", mapPreviewTexture); + } + } + + if (mainMessageBox.getEnabled() == false) { + if (hasNetworkGameSettings() == true) { + renderer.renderChatManager(&chatManager); + } + } + renderer.renderConsole(&console, + showFullConsole ? consoleFull : + consoleStoredAndNormal); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + //throw megaglest_runtime_error(szBuf); + + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + showGeneralError = true; + generalErrorToShow = szBuf; + } + } + + void MenuStateCustomGame::switchSetupForSlots(SwitchSetupRequest ** + switchSetupRequests, + ServerInterface * + &serverInterface, + int startIndex, + int endIndex, + bool onlyNetworkUnassigned) { + for (int i = startIndex; i < endIndex; ++i) { + if (switchSetupRequests[i] != NULL) { + //printf("Switch slot = %d control = %d newIndex = %d currentindex = %d onlyNetworkUnassigned = %d\n",i,listBoxControls[i].getSelectedItemIndex(),switchSetupRequests[i]->getToFactionIndex(),switchSetupRequests[i]->getCurrentFactionIndex(),onlyNetworkUnassigned); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + switchSetupRequests[i]->getSwitchFlags + ()); + + if (onlyNetworkUnassigned == true + && listBoxControls[i].getSelectedItemIndex() != + ctNetworkUnassigned) { + if (i < mapInfo.players + || (i >= mapInfo.players + && listBoxControls[i].getSelectedItemIndex() != + ctNetwork)) { + continue; + } + } + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + switchSetupRequests[i]->getToSlotIndex + ()); + + if (switchSetupRequests[i]->getToSlotIndex() != -1) { + int newFactionIdx = switchSetupRequests[i]->getToSlotIndex(); + + //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex()); + int switchFactionIdx = + switchSetupRequests[i]->getCurrentSlotIndex(); + if (serverInterface->switchSlot + (switchFactionIdx, newFactionIdx)) { + try { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = + serverInterface->getSlot(newFactionIdx, true); + + if (switchSetupRequests[i]->getSelectedFactionName() != "" + && (slot != NULL + && switchSetupRequests[i]->getSelectedFactionName() + != Lang::getInstance().getString("DataMissing", + slot->getNetworkPlayerLanguage + (), true) + && switchSetupRequests[i]->getSelectedFactionName() + != "???DataMissing???")) { + // I don't believe we need to check to see if Observers + // are allowed. If it's not, there should be not button on the client + // side that would allow them to switch to a slot > mapInfo.hardMaxPlayers + if (newFactionIdx <= mapInfo.hardMaxPlayers) { + + listBoxFactions[newFactionIdx].setSelectedItem + (switchSetupRequests[i]->getSelectedFactionName()); + } else { + listBoxFactions[newFactionIdx].setSelectedItem(GameConstants::OBSERVER_SLOTNAME); + } + + } + if (switchSetupRequests[i]->getToTeam() != -1) { + if (newFactionIdx <= mapInfo.hardMaxPlayers) { + listBoxTeams[newFactionIdx].setSelectedItemIndex + (switchSetupRequests[i]->getToTeam()); + } else { + listBoxTeams[i].setSelectedItem(intToStr(GameConstants::maxPlayers + + fpt_Observer)); + } + } + if (switchSetupRequests[i]->getNetworkPlayerName() != "") { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, i, + labelPlayerNames + [newFactionIdx].getText + ().c_str(), + switchSetupRequests + [i]->getNetworkPlayerName(). + c_str()); + labelPlayerNames[newFactionIdx].setText + (switchSetupRequests[i]->getNetworkPlayerName()); + } + + if (listBoxControls[switchFactionIdx].getSelectedItemIndex + () == ctNetworkUnassigned) { + serverInterface->removeSlot(switchFactionIdx); + listBoxControls[switchFactionIdx].setSelectedItemIndex + (ctClosed); + + labelPlayers[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + labelPlayerNames[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + listBoxControls[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + listBoxFactions[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + listBoxTeams[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + labelNetStatus[switchFactionIdx].setVisible + (switchFactionIdx + 1 <= mapInfo.players); + } + } catch (const runtime_error & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + e.what()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] caught exception error = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + e.what()); + } + } + } else { + try { + int factionIdx = + switchSetupRequests[i]->getCurrentSlotIndex(); + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = + serverInterface->getSlot(factionIdx, true); + + if (switchSetupRequests[i]->getSelectedFactionName() != "" + && (slot != NULL + && switchSetupRequests[i]->getSelectedFactionName() + != Lang::getInstance().getString("DataMissing", + slot->getNetworkPlayerLanguage + (), true) + && switchSetupRequests[i]->getSelectedFactionName() + != "???DataMissing???")) { + listBoxFactions[i].setSelectedItem(switchSetupRequests + [i]->getSelectedFactionName + ()); + } + if (switchSetupRequests[i]->getToTeam() != -1) { + listBoxTeams[i].setSelectedItemIndex(switchSetupRequests + [i]->getToTeam()); + } + + if ((switchSetupRequests[i]->getSwitchFlags() & + ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, i, + switchSetupRequests + [i]->getSwitchFlags(), + switchSetupRequests + [i]->getNetworkPlayerName(). + c_str(), + labelPlayerNames[i].getText(). + c_str()); + + if (switchSetupRequests[i]->getNetworkPlayerName() != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + labelPlayerNames[i].setText(switchSetupRequests + [i]->getNetworkPlayerName + ()); + } else { + labelPlayerNames[i].setText(""); + } + } + } catch (const runtime_error & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, e.what()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] caught exception error = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, e.what()); + } + } + } + + delete switchSetupRequests[i]; + switchSetupRequests[i] = NULL; + } + } + } + + void MenuStateCustomGame::initFactionPreview(const GameSettings * + gameSettings) { + string factionVideoUrl = ""; + string factionVideoUrlFallback = ""; + + string factionDefinitionXML = + Game::findFactionLogoFile(gameSettings, NULL, + currentFactionName_factionPreview + + ".xml"); + if (factionDefinitionXML != "" + && currentFactionName_factionPreview != + GameConstants::RANDOMFACTION_SLOTNAME + && currentFactionName_factionPreview != + GameConstants::OBSERVER_SLOTNAME + && fileExists(factionDefinitionXML) == true) { + XmlTree xmlTree; + std::map < string, string > mapExtraTagReplacementValues; + xmlTree.load(factionDefinitionXML, + Properties::getTagReplacementValues + (&mapExtraTagReplacementValues)); + const XmlNode *factionNode = xmlTree.getRootNode(); + if (factionNode->hasAttribute("faction-preview-video") == true) { + factionVideoUrl = + factionNode->getAttribute("faction-preview-video")->getValue(); + } + + factionVideoUrlFallback = + Game::findFactionLogoFile(gameSettings, NULL, "preview_video.*"); + if (factionVideoUrl == "") { + factionVideoUrl = factionVideoUrlFallback; + factionVideoUrlFallback = ""; + } + } + //printf("currentFactionName_factionPreview [%s] random [%s] observer [%s] factionVideoUrl [%s]\n",currentFactionName_factionPreview.c_str(),GameConstants::RANDOMFACTION_SLOTNAME,GameConstants::OBSERVER_SLOTNAME,factionVideoUrl.c_str()); + + + if (factionVideoUrl != "") { + //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + if (CoreData::getInstance().getMenuMusic()->getVolume() != 0) { + CoreData::getInstance().getMenuMusic()->setVolume(0); + factionVideoSwitchedOffVolume = true; + } + + if (currentFactionLogo != factionVideoUrl) { + currentFactionLogo = factionVideoUrl; + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && ::Shared::Graphics::VideoPlayer::hasBackEndVideoPlayer() == + true) { + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + string introVideoFile = factionVideoUrl; + string introVideoFileFallback = factionVideoUrlFallback; + + Context *c = + GraphicsInterface::getInstance().getCurrentContext(); + SDL_Window *window = + static_cast < + ContextGl * + >(c)->getPlatformContextGlPtr()->getScreenWindow(); + SDL_Surface *screen = + static_cast < + ContextGl * + >(c)->getPlatformContextGlPtr()->getScreenSurface(); + + string vlcPluginsPath = + Config::getInstance().getString("VideoPlayerPluginsPath", ""); + //printf("screen->w = %d screen->h = %d screen->format->BitsPerPixel = %d\n",screen->w,screen->h,screen->format->BitsPerPixel); + factionVideo = new VideoPlayer(&Renderer::getInstance(), + introVideoFile, + introVideoFileFallback, + window, + 0, 0, + screen->w, + screen->h, + screen->format->BitsPerPixel, + true, vlcPluginsPath, + SystemFlags::VERBOSE_MODE_ENABLED); + factionVideo->initPlayer(); + } + } + } else { + //SoundRenderer &soundRenderer= SoundRenderer::getInstance(); + //switch on music again!! + Config & config = Config::getInstance(); + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + if (factionVideoSwitchedOffVolume) { + if (CoreData::getInstance().getMenuMusic()->getVolume() != + configVolume) { + CoreData::getInstance(). + getMenuMusic()->setVolume(configVolume); + } + factionVideoSwitchedOffVolume = false; + } + + if (factionVideo != NULL) { + factionVideo->closePlayer(); + delete factionVideo; + factionVideo = NULL; + } + } + + if (factionVideo == NULL) { + string factionLogo = Game::findFactionLogoFile(gameSettings, NULL, + GameConstants::PREVIEW_SCREEN_FILE_FILTER); + if (factionLogo == "") { + factionLogo = Game::findFactionLogoFile(gameSettings, NULL); + } + if (currentFactionLogo != factionLogo) { + currentFactionLogo = factionLogo; + loadFactionTexture(currentFactionLogo); + } + } + } + + void MenuStateCustomGame::publishToMasterserver() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + int slotCountUsed = 0; + int slotCountHumans = 0; + int slotCountConnectedPlayers = 0; + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + GameSettings gameSettings; + loadGameSettings(&gameSettings); + Config & config = Config::getInstance(); + //string serverinfo=""; + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + publishToServerInfo.clear(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + slotCountUsed++; + } + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + slotCountHumans++; + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, true)->isConnected()) { + slotCountConnectedPlayers++; + } + } else if (listBoxControls[i].getSelectedItemIndex() == ctHuman) { + slotCountHumans++; + slotCountConnectedPlayers++; + } + } + + publishToServerInfo["uuid"] = + Config::getInstance().getString("PlayerId", ""); + + //?status=waiting&system=linux&info=titus + publishToServerInfo["glestVersion"] = glestVersionString; + publishToServerInfo["platform"] = + getPlatformNameString(); + publishToServerInfo["binaryCompileDate"] = getCompileDateTime(); + + //game info: + publishToServerInfo["serverTitle"] = gameSettings.getGameName(); + //ip is automatically set + + //game setup info: + + //publishToServerInfo["tech"] = listBoxTechTree.getSelectedItem(); + publishToServerInfo["tech"] = + techTreeFiles[listBoxTechTree.getSelectedItemIndex()]; + //publishToServerInfo["map"] = listBoxMap.getSelectedItem(); + publishToServerInfo["map"] = getCurrentMapFile(); + //publishToServerInfo["tileset"] = listBoxTileset.getSelectedItem(); + publishToServerInfo["tileset"] = + tilesetFiles[listBoxTileset.getSelectedItemIndex()]; + + publishToServerInfo["activeSlots"] = intToStr(slotCountUsed); + publishToServerInfo["networkSlots"] = intToStr(slotCountHumans); + publishToServerInfo["connectedClients"] = + intToStr(slotCountConnectedPlayers); + + string serverPort = config.getString("PortServer", + intToStr + (GameConstants:: + serverPort).c_str()); + string externalPort = + config.getString("PortExternal", serverPort.c_str()); + publishToServerInfo["externalconnectport"] = externalPort; + publishToServerInfo["privacyPlease"] = + intToStr(config.getBool("PrivacyPlease", "false")); + + publishToServerInfo["gameStatus"] = + intToStr(game_status_waiting_for_players); + if (slotCountHumans <= slotCountConnectedPlayers) { + publishToServerInfo["gameStatus"] = + intToStr(game_status_waiting_for_start); + } + + publishToServerInfo["gameUUID"] = gameSettings.getGameUUID(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::setupTask(BaseThread * callingThread, + void *userdata) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\nsetupTask callingThread [%p] userdata [%p]\n", + callingThread, userdata); + if (userdata != NULL) { + int value = *((int *) &userdata); + THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; + //printf("\n\nsetupTask callingThread [%p] userdata [%p]\n",callingThread,userdata); + if (threadType == tnt_MASTERSERVER) { + MenuStateCustomGame::setupTaskStatic(callingThread); + } + } + } + void MenuStateCustomGame::shutdownTask(BaseThread * callingThread, + void *userdata) { + //printf("\n\nshutdownTask callingThread [%p] userdata [%p]\n",callingThread,userdata); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("\n\nshutdownTask callingThread [%p] userdata [%p]\n", + callingThread, userdata); + if (userdata != NULL) { + int value = *((int *) &userdata); + THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; + //printf("\n\nshutdownTask callingThread [%p] userdata [%p]\n",callingThread,userdata); + if (threadType == tnt_MASTERSERVER) { + MenuStateCustomGame::shutdownTaskStatic(callingThread); + } + } + } + void MenuStateCustomGame::setupTaskStatic(BaseThread * callingThread) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + CURL *handle = SystemFlags::initHTTP(); + callingThread->setGenericData < CURL >(handle); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + void MenuStateCustomGame::shutdownTaskStatic(BaseThread * callingThread) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + + //printf("LINE: %d\n",__LINE__); + CURL *handle = callingThread->getGenericData < CURL >(); + SystemFlags::cleanupHTTP(&handle); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + } + + void MenuStateCustomGame::simpleTask(BaseThread * callingThread, + void *userdata) { + //printf("\n\nSimple Task callingThread [%p] userdata [%p]\n",callingThread,userdata); + int value = *((int *) &userdata); + //printf("\n\nSimple Task callingThread [%p] userdata [%p] value = %d\n",callingThread,userdata,value); + + THREAD_NOTIFIER_TYPE threadType = (THREAD_NOTIFIER_TYPE) value; + if (threadType == tnt_MASTERSERVER) { + simpleTaskForMasterServer(callingThread); + } else if (threadType == tnt_CLIENTS) { + simpleTaskForClients(callingThread); + } + } + + void MenuStateCustomGame::simpleTaskForMasterServer(BaseThread * + callingThread) { + try { + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - A\n"); + + MutexSafeWrapper + safeMutexThreadOwner(callingThread->getMutexThreadOwnerValid(), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + return; + } + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - B\n"); + + MutexSafeWrapper + safeMutex(callingThread->getMutexThreadObjectAccessor(), + string(__FILE__) + "_" + intToStr(__LINE__)); + bool republish = (needToRepublishToMasterserver == true + && publishToServerInfo.empty() == false); + needToRepublishToMasterserver = false; + std::map < string, string > newPublishToServerInfo = + publishToServerInfo; + publishToServerInfo.clear(); + + //printf("simpleTask broadCastSettings = %d\n",broadCastSettings); + + if (callingThread->getQuitStatus() == true) { + return; + } + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - C\n"); + + if (republish == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + string request = Config::getInstance().getString("Masterserver"); + if (request != "") { + endPathWithSlash(request, false); + } + request += "addServerInfo.php?"; + + //CURL *handle = SystemFlags::initHTTP(); + CURL *handle = callingThread->getGenericData < CURL >(); + + int paramIndex = 0; + for (std::map < string, string >::const_iterator iterMap = + newPublishToServerInfo.begin(); + iterMap != newPublishToServerInfo.end(); ++iterMap) { + + request += iterMap->first; + request += "="; + request += SystemFlags::escapeURL(iterMap->second, handle); + + paramIndex++; + if (paramIndex < (int) newPublishToServerInfo.size()) { + request += "&"; + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("The Lobby request is:\n%s\n", request.c_str()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] the request is:\n%s\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + request.c_str()); + safeMutex.ReleaseLock(true); + safeMutexThreadOwner.ReleaseLock(); + + std::string serverInfo = SystemFlags::getHTTP(request, handle); + //SystemFlags::cleanupHTTP(&handle); + + MutexSafeWrapper + safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid(), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner2.isValidMutex() == false) { + return; + } + safeMutex.Lock(); + + //printf("the result is:\n'%s'\n",serverInfo.c_str()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] the result is:\n'%s'\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + serverInfo.c_str()); + + // uncomment to enable router setup check of this server + if (EndsWith(serverInfo, "OK") == false) { + if (callingThread->getQuitStatus() == true) { + return; + } + + // Give things another chance to see if we can get a connection from the master server + if (tMasterserverErrorElapsed > 0 && + difftime((long int) time(NULL), + tMasterserverErrorElapsed) > + MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS) { + showMasterserverError = true; + masterServererErrorToShow = + (serverInfo != "" ? serverInfo : "No Reply"); + } else { + if (tMasterserverErrorElapsed == 0) { + tMasterserverErrorElapsed = time(NULL); + } + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line %d] error checking response from masterserver elapsed seconds = %.2f / %d\nResponse:\n%s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, __LINE__, + difftime((long int) + time(NULL), + tMasterserverErrorElapsed), + MASTERSERVER_BROADCAST_MAX_WAIT_RESPONSE_SECONDS, + serverInfo.c_str()); + + needToRepublishToMasterserver = true; + } + } + } else { + safeMutexThreadOwner.ReleaseLock(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - D\n"); + + safeMutex.ReleaseLock(); + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - F\n"); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + if (callingThread->getQuitStatus() == false) { + //throw megaglest_runtime_error(szBuf); + showGeneralError = true; + generalErrorToShow = ex.what(); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::simpleTaskForClients(BaseThread * + callingThread) { + try { + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - A\n"); + + MutexSafeWrapper + safeMutexThreadOwner(callingThread->getMutexThreadOwnerValid(), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + return; + } + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - B\n"); + + MutexSafeWrapper + safeMutex(callingThread->getMutexThreadObjectAccessor(), + string(__FILE__) + "_" + intToStr(__LINE__)); + bool broadCastSettings = needToBroadcastServerSettings; + + //printf("simpleTask broadCastSettings = %d\n",broadCastSettings); + + needToBroadcastServerSettings = false; + bool hasClientConnection = false; + + if (broadCastSettings == true) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(false); + if (serverInterface != NULL) { + hasClientConnection = serverInterface->hasClientConnection(); + } + } + bool needPing = + (difftime((long int) time(NULL), lastNetworkPing) >= + GameConstants::networkPingInterval); + + if (callingThread->getQuitStatus() == true) { + return; + } + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - C\n"); + + safeMutexThreadOwner.ReleaseLock(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - D\n"); + + if (broadCastSettings == true) { + MutexSafeWrapper + safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid(), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner2.isValidMutex() == false) { + return; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + //printf("simpleTask broadCastSettings = %d hasClientConnection = %d\n",broadCastSettings,hasClientConnection); + + if (callingThread->getQuitStatus() == true) { + return; + } + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(false); + if (serverInterface != NULL) { + lastGameSettingsreceivedCount++; + if (this->headlessServerMode == false + || (serverInterface->getGameSettingsUpdateCount() <= + lastMasterServerSettingsUpdateCount)) { + GameSettings gameSettings; + loadGameSettings(&gameSettings); + + //printf("\n\n\n\n=====#2 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),gameSettings.toString().c_str()); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + serverInterface->setGameSettings(&gameSettings, false); + lastMasterServerSettingsUpdateCount = + serverInterface->getGameSettingsUpdateCount(); + + if (hasClientConnection == true) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + serverInterface->broadcastGameSetup(&gameSettings); + } + } + } + } + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - E\n"); + + if (needPing == true) { + MutexSafeWrapper + safeMutexThreadOwner2(callingThread->getMutexThreadOwnerValid(), + string(__FILE__) + "_" + + intToStr(__LINE__)); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner2.isValidMutex() == false) { + return; + } + + lastNetworkPing = time(NULL); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] Sending nmtPing to clients\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(false); + if (serverInterface != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + NetworkMessagePing *msg = + new NetworkMessagePing(GameConstants::networkPingInterval, + time(NULL)); + //serverInterface->broadcastPing(&msg); + serverInterface->queueBroadcastMessage(msg); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + } + safeMutex.ReleaseLock(); + + //printf("-=-=-=-=- IN MenuStateCustomGame simpleTask - F\n"); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + if (callingThread->getQuitStatus() == false) { + //throw megaglest_runtime_error(szBuf); + showGeneralError = true; + generalErrorToShow = ex.what(); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::loadGameSettings(GameSettings * gameSettings, + bool forceCloseUnusedSlots) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + int factionCount = 0; + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (this->headlessServerMode == true + && serverInterface->getGameSettingsUpdateCount() > + lastMasterServerSettingsUpdateCount + && serverInterface->getGameSettings() != NULL) { + const GameSettings *settings = serverInterface->getGameSettings(); + //printf("\n\n\n\n=====#3 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),settings->toString().c_str()); + + lastMasterServerSettingsUpdateCount = + serverInterface->getGameSettingsUpdateCount(); + //printf("#1 custom menu got map [%s]\n",settings->getMap().c_str()); + + setupUIFromGameSettings(*settings); + } + + gameSettings->setGameName(labelGameName.getText()); + + // Test flags values + //gameSettings->setFlagTypes1(ft1_show_map_resources); + // + + if (checkBoxScenario.getValue() == true) { + gameSettings->setScenario(scenarioInfo.name); + gameSettings->setScenarioDir(Scenario::getScenarioPath + (dirList, scenarioInfo.name)); + + gameSettings->setDefaultResources(scenarioInfo.defaultResources); + gameSettings->setDefaultUnits(scenarioInfo.defaultUnits); + gameSettings-> + setDefaultVictoryConditions(scenarioInfo.defaultVictoryConditions); + } else { + gameSettings->setScenario(""); + gameSettings->setScenarioDir(""); + } + + gameSettings->setGameUUID(this->gameUUID); + + //printf("scenarioInfo.name [%s] [%s] [%s]\n",scenarioInfo.name.c_str(),listBoxMap.getSelectedItem().c_str(),getCurrentMapFile().c_str()); + + gameSettings->setMapFilter(listBoxMapFilter.getSelectedItemIndex()); + gameSettings->setDescription(formatString(getCurrentMapFile())); + gameSettings->setMap(getCurrentMapFile()); + if (tilesetFiles.empty() == false) { + gameSettings->setTileset(tilesetFiles + [listBoxTileset.getSelectedItemIndex()]); + } + if (techTreeFiles.empty() == false) { + gameSettings->setTech(techTreeFiles + [listBoxTechTree.getSelectedItemIndex()]); + } + + if (autoStartSettings != NULL) { + gameSettings->setDefaultUnits(autoStartSettings->getDefaultUnits()); + gameSettings-> + setDefaultResources(autoStartSettings->getDefaultResources()); + gameSettings-> + setDefaultVictoryConditions + (autoStartSettings->getDefaultVictoryConditions()); + } else if (checkBoxScenario.getValue() == false) { + gameSettings->setDefaultUnits(true); + gameSettings->setDefaultResources(true); + gameSettings->setDefaultVictoryConditions(true); + } + + gameSettings->setFogOfWar(listBoxFogOfWar.getSelectedItemIndex() == 0 + || listBoxFogOfWar.getSelectedItemIndex() == + 1); + + gameSettings->setAllowObservers(checkBoxAllowObservers.getValue() == + 1); + + uint32 valueFlags1 = gameSettings->getFlagTypes1(); + if (listBoxFogOfWar.getSelectedItemIndex() == 1 || + listBoxFogOfWar.getSelectedItemIndex() == 2) { + valueFlags1 |= ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_show_map_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + + //gameSettings->setEnableObserverModeAtEndGame(listBoxEnableObserverMode.getSelectedItemIndex() == 0); + gameSettings->setEnableObserverModeAtEndGame(true); + //gameSettings->setPathFinderType(static_cast(listBoxPathFinderType.getSelectedItemIndex())); + + valueFlags1 = gameSettings->getFlagTypes1(); + if (checkBoxEnableSwitchTeamMode.getValue() == true) { + valueFlags1 |= ft1_allow_team_switching; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_team_switching; + gameSettings->setFlagTypes1(valueFlags1); + } + gameSettings->setAiAcceptSwitchTeamPercentChance(strToInt + (listBoxAISwitchTeamAcceptPercent.getSelectedItem + ())); + gameSettings-> + setFallbackCpuMultiplier + (listBoxFallbackCpuMultiplier.getSelectedItemIndex()); + + if (checkBoxAllowInGameJoinPlayer.getValue() == true) { + valueFlags1 |= ft1_allow_in_game_joining; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_in_game_joining; + gameSettings->setFlagTypes1(valueFlags1); + } + + if (checkBoxAllowTeamUnitSharing.getValue() == true) { + valueFlags1 |= ft1_allow_shared_team_units; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_shared_team_units; + gameSettings->setFlagTypes1(valueFlags1); + } + + if (checkBoxAllowTeamResourceSharing.getValue() == true) { + valueFlags1 |= ft1_allow_shared_team_resources; + gameSettings->setFlagTypes1(valueFlags1); + } else { + valueFlags1 &= ~ft1_allow_shared_team_resources; + gameSettings->setFlagTypes1(valueFlags1); + } + + if (Config::getInstance().getBool("EnableNetworkGameSynchChecks", + "false") == true) { + //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); + + valueFlags1 |= ft1_network_synch_checks_verbose; + gameSettings->setFlagTypes1(valueFlags1); + + } else { + valueFlags1 &= ~ft1_network_synch_checks_verbose; + gameSettings->setFlagTypes1(valueFlags1); + + } + if (Config::getInstance().getBool("EnableNetworkGameSynchMonitor", + "false") == true) { + //printf("*WARNING* - EnableNetworkGameSynchChecks is enabled\n"); + + valueFlags1 |= ft1_network_synch_checks; + gameSettings->setFlagTypes1(valueFlags1); + + } else { + valueFlags1 &= ~ft1_network_synch_checks; + gameSettings->setFlagTypes1(valueFlags1); + + } + + gameSettings->setNetworkAllowNativeLanguageTechtree + (checkBoxAllowNativeLanguageTechtree.getValue()); + + // First save Used slots + //for(int i=0; iheadlessServerMode == true) { + // switch slot to network, because no human in headless mode + listBoxControls[i].setSelectedItemIndex(ctNetwork); + updateResourceMultiplier(i); + } + + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + + if (forceCloseUnusedSlots == true + && (ct == ctNetworkUnassigned || ct == ctNetwork)) { + if (serverInterface != NULL && + (serverInterface->getSlot(i, true) == NULL || + serverInterface->getSlot(i, true)->isConnected() == false)) { + if (checkBoxScenario.getValue() == false) { + //printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); + + listBoxControls[i].setSelectedItemIndex(ctClosed); + ct = ctClosed; + } + } + } else if (ct == ctNetworkUnassigned && i < mapInfo.players) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + ct = ctNetwork; + } + + if (ct != ctClosed) { + int slotIndex = factionCount; + gameSettings->setFactionControl(slotIndex, ct); + if (ct == ctHuman) { + + // I'm putting this inside a ppd for now. I don't see it needs to be + // built in unless DEBUG is defined during building -andy5995 2018-01-26 #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, slotIndex = %d, getHumanPlayerName(i) [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, slotIndex, - getHumanPlayerName (i).c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, slotIndex = %d, getHumanPlayerName(i) [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, slotIndex, + getHumanPlayerName(i).c_str()); #endif - gameSettings->setThisFactionIndex (slotIndex); - gameSettings->setNetworkPlayerName (slotIndex, - getHumanPlayerName (i)); - gameSettings->setNetworkPlayerUUID (slotIndex, - Config:: - getInstance ().getString - ("PlayerId", "")); - gameSettings->setNetworkPlayerPlatform (slotIndex, - getPlatformNameString ()); - gameSettings->setNetworkPlayerStatuses (slotIndex, - getNetworkPlayerStatus - ()); - Lang & lang = Lang::getInstance (); - gameSettings->setNetworkPlayerLanguages (slotIndex, - lang.getLanguage ()); - } - else if (serverInterface != NULL - && serverInterface->getSlot (i, true) != NULL) - { - gameSettings->setNetworkPlayerLanguages (slotIndex, - serverInterface->getSlot - (i, - true)->getNetworkPlayerLanguage - ()); - } + gameSettings->setThisFactionIndex(slotIndex); + gameSettings->setNetworkPlayerName(slotIndex, + getHumanPlayerName(i)); + gameSettings->setNetworkPlayerUUID(slotIndex, + Config:: + getInstance().getString + ("PlayerId", "")); + gameSettings->setNetworkPlayerPlatform(slotIndex, + getPlatformNameString()); + gameSettings->setNetworkPlayerStatuses(slotIndex, + getNetworkPlayerStatus + ()); + Lang & lang = Lang::getInstance(); + gameSettings->setNetworkPlayerLanguages(slotIndex, + lang.getLanguage()); + } else if (serverInterface != NULL + && serverInterface->getSlot(i, true) != NULL) { + gameSettings->setNetworkPlayerLanguages(slotIndex, + serverInterface->getSlot + (i, + true)->getNetworkPlayerLanguage + ()); + } - gameSettings->setResourceMultiplierIndex (slotIndex, - listBoxRMultiplier - [i].getSelectedItemIndex - ()); + gameSettings->setResourceMultiplierIndex(slotIndex, + listBoxRMultiplier + [i].getSelectedItemIndex + ()); -// I'm putting this inside a ppd for now. I don't see it needs to be -// built in unless DEBUG is defined during building -andy5995 2018-01-26 + // I'm putting this inside a ppd for now. I don't see it needs to be + // built in unless DEBUG is defined during building -andy5995 2018-01-26 #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, i, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()].c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, i, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()].c_str()); #endif - gameSettings->setFactionTypeName (slotIndex, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()]); - if (factionFiles[listBoxFactions[i].getSelectedItemIndex ()] == - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - listBoxTeams[i].setSelectedItem (intToStr - (GameConstants::maxPlayers + - fpt_Observer)); - } - else if (listBoxTeams[i].getSelectedItem () == - intToStr (GameConstants::maxPlayers + fpt_Observer)) - { + gameSettings->setFactionTypeName(slotIndex, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()]); + if (factionFiles[listBoxFactions[i].getSelectedItemIndex()] == + formatString(GameConstants::OBSERVER_SLOTNAME)) { + listBoxTeams[i].setSelectedItem(intToStr + (GameConstants::maxPlayers + + fpt_Observer)); + } else if (listBoxTeams[i].getSelectedItem() == + intToStr(GameConstants::maxPlayers + fpt_Observer)) { -//printf("Line: %d lastSelectedTeamIndex[i] = %d \n",__LINE__,lastSelectedTeamIndex[i]); + //printf("Line: %d lastSelectedTeamIndex[i] = %d \n",__LINE__,lastSelectedTeamIndex[i]); - if ((listBoxControls[i].getSelectedItemIndex () == ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta) && checkBoxScenario.getValue () == true) - { + if ((listBoxControls[i].getSelectedItemIndex() == ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta) && checkBoxScenario.getValue() == true) { - } - else - { - if (lastSelectedTeamIndex[i] >= 0 - && lastSelectedTeamIndex[i] + 1 != - (GameConstants::maxPlayers + fpt_Observer)) - { - if (lastSelectedTeamIndex[i] == 0) - { - lastSelectedTeamIndex[i] = GameConstants::maxPlayers - 1; - } - else if (lastSelectedTeamIndex[i] == - GameConstants::maxPlayers - 1) - { - lastSelectedTeamIndex[i] = 0; - } + } else { + if (lastSelectedTeamIndex[i] >= 0 + && lastSelectedTeamIndex[i] + 1 != + (GameConstants::maxPlayers + fpt_Observer)) { + if (lastSelectedTeamIndex[i] == 0) { + lastSelectedTeamIndex[i] = GameConstants::maxPlayers - 1; + } else if (lastSelectedTeamIndex[i] == + GameConstants::maxPlayers - 1) { + lastSelectedTeamIndex[i] = 0; + } - listBoxTeams[i].setSelectedItemIndex (lastSelectedTeamIndex - [i]); - } - else - { - listBoxTeams[i].setSelectedItem (intToStr (1)); - } - } - } + listBoxTeams[i].setSelectedItemIndex(lastSelectedTeamIndex + [i]); + } else { + listBoxTeams[i].setSelectedItem(intToStr(1)); + } + } + } - gameSettings->setTeam (slotIndex, - listBoxTeams[i].getSelectedItemIndex ()); - gameSettings->setStartLocationIndex (slotIndex, i); + gameSettings->setTeam(slotIndex, + listBoxTeams[i].getSelectedItemIndex()); + gameSettings->setStartLocationIndex(slotIndex, i); - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork || - listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - if (serverInterface != NULL && - serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, true)->isConnected ()) - { + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork || + listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + if (serverInterface != NULL && + serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, true)->isConnected()) { - gameSettings->setNetworkPlayerStatuses (slotIndex, - serverInterface->getSlot - (i, - true)->getNetworkPlayerStatus - ()); + gameSettings->setNetworkPlayerStatuses(slotIndex, + serverInterface->getSlot + (i, + true)->getNetworkPlayerStatus + ()); -// I'm putting this inside a ppd for now. I don't see it needs to be -// built in unless DEBUG is defined during building -andy5995 2018-01-26 + // I'm putting this inside a ppd for now. I don't see it needs to be + // built in unless DEBUG is defined during building -andy5995 2018-01-26 #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, connectionSlot->getName() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - serverInterface->getSlot (i, - true)->getName - ().c_str ()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, connectionSlot->getName() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + serverInterface->getSlot(i, + true)->getName + ().c_str()); #endif - gameSettings->setNetworkPlayerName (slotIndex, - serverInterface->getSlot (i, - true)->getName - ()); - gameSettings->setNetworkPlayerUUID (i, - serverInterface->getSlot (i, - true)->getUUID - ()); - gameSettings->setNetworkPlayerPlatform (i, - serverInterface->getSlot - (i, - true)->getPlatform ()); - labelPlayerNames[i]. - setText (serverInterface->getSlot (i, true)->getName ()); - } - else - { + gameSettings->setNetworkPlayerName(slotIndex, + serverInterface->getSlot(i, + true)->getName + ()); + gameSettings->setNetworkPlayerUUID(i, + serverInterface->getSlot(i, + true)->getUUID + ()); + gameSettings->setNetworkPlayerPlatform(i, + serverInterface->getSlot + (i, + true)->getPlatform()); + labelPlayerNames[i]. + setText(serverInterface->getSlot(i, true)->getName()); + } else { -// I'm putting this inside a ppd for now. I don't see it needs to be -// built in unless DEBUG is defined during building -andy5995 2018-01-26 + // I'm putting this inside a ppd for now. I don't see it needs to be + // built in unless DEBUG is defined during building -andy5995 2018-01-26 #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, playername unconnected\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, playername unconnected\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i); #endif - gameSettings->setNetworkPlayerName (slotIndex, - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); - labelPlayerNames[i].setText (""); - } - } - else if (listBoxControls[i].getSelectedItemIndex () != ctHuman) - { - AIPlayerCount++; + gameSettings->setNetworkPlayerName(slotIndex, + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + labelPlayerNames[i].setText(""); + } + } else if (listBoxControls[i].getSelectedItemIndex() != ctHuman) { + AIPlayerCount++; #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, playername is AI (blank)\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, playername is AI (blank)\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i); #endif - Lang & lang = Lang::getInstance (); - gameSettings->setNetworkPlayerName (slotIndex, - lang.getString ("AI") + - intToStr (AIPlayerCount)); - labelPlayerNames[i].setText (""); - } - if (listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - setSlotHuman (i); - } - if (serverInterface != NULL - && serverInterface->getSlot (i, true) != NULL) - { - gameSettings->setNetworkPlayerUUID (slotIndex, - serverInterface->getSlot (i, - true)->getUUID - ()); - gameSettings->setNetworkPlayerPlatform (slotIndex, - serverInterface->getSlot - (i, - true)->getPlatform ()); - } + Lang & lang = Lang::getInstance(); + gameSettings->setNetworkPlayerName(slotIndex, + lang.getString("AI") + + intToStr(AIPlayerCount)); + labelPlayerNames[i].setText(""); + } + if (listBoxControls[i].getSelectedItemIndex() == ctHuman) { + setSlotHuman(i); + } + if (serverInterface != NULL + && serverInterface->getSlot(i, true) != NULL) { + gameSettings->setNetworkPlayerUUID(slotIndex, + serverInterface->getSlot(i, + true)->getUUID + ()); + gameSettings->setNetworkPlayerPlatform(slotIndex, + serverInterface->getSlot + (i, + true)->getPlatform()); + } - factionCount++; - } - else - { -//gameSettings->setNetworkPlayerName(""); - gameSettings->setNetworkPlayerStatuses (factionCount, npst_None); - labelPlayerNames[i].setText (""); - } - } + factionCount++; + } else { + //gameSettings->setNetworkPlayerName(""); + gameSettings->setNetworkPlayerStatuses(factionCount, npst_None); + labelPlayerNames[i].setText(""); + } + } -// Next save closed slots - int closedCount = 0; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct == ctClosed) - { - int slotIndex = factionCount + closedCount; + // Next save closed slots + int closedCount = 0; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct == ctClosed) { + int slotIndex = factionCount + closedCount; - gameSettings->setFactionControl (slotIndex, ct); - gameSettings->setTeam (slotIndex, - listBoxTeams[i].getSelectedItemIndex ()); - gameSettings->setStartLocationIndex (slotIndex, i); -//gameSettings->setResourceMultiplierIndex(slotIndex, 10); - listBoxRMultiplier[i].setSelectedItem ("1.0"); - gameSettings->setResourceMultiplierIndex (slotIndex, - listBoxRMultiplier - [i].getSelectedItemIndex - ()); -//printf("Test multiplier = %s\n",listBoxRMultiplier[i].getSelectedItem().c_str()); + gameSettings->setFactionControl(slotIndex, ct); + gameSettings->setTeam(slotIndex, + listBoxTeams[i].getSelectedItemIndex()); + gameSettings->setStartLocationIndex(slotIndex, i); + //gameSettings->setResourceMultiplierIndex(slotIndex, 10); + listBoxRMultiplier[i].setSelectedItem("1.0"); + gameSettings->setResourceMultiplierIndex(slotIndex, + listBoxRMultiplier + [i].getSelectedItemIndex + ()); + //printf("Test multiplier = %s\n",listBoxRMultiplier[i].getSelectedItem().c_str()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, i, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()].c_str ()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, factionFiles[listBoxFactions[i].getSelectedItemIndex()] [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, i, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()].c_str()); - gameSettings->setFactionTypeName (slotIndex, - factionFiles[listBoxFactions - [i].getSelectedItemIndex - ()]); - gameSettings->setNetworkPlayerName (slotIndex, - GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME); - gameSettings->setNetworkPlayerUUID (slotIndex, ""); - gameSettings->setNetworkPlayerPlatform (slotIndex, ""); + gameSettings->setFactionTypeName(slotIndex, + factionFiles[listBoxFactions + [i].getSelectedItemIndex + ()]); + gameSettings->setNetworkPlayerName(slotIndex, + GameConstants::NETWORK_SLOT_CLOSED_SLOTNAME); + gameSettings->setNetworkPlayerUUID(slotIndex, ""); + gameSettings->setNetworkPlayerPlatform(slotIndex, ""); - closedCount++; - } - } + closedCount++; + } + } - gameSettings->setFactionCount (factionCount); + gameSettings->setFactionCount(factionCount); - Config & config = Config::getInstance (); - gameSettings->setEnableServerControlledAI (config.getBool - ("ServerControlledAI", - "true")); - gameSettings-> - setNetworkFramePeriod (config.getInt ("NetworkSendFrameCount", "20")); - gameSettings->setNetworkPauseGameForLaggedClients (((checkBoxNetworkPauseGameForLaggedClients.getValue () == true))); + Config & config = Config::getInstance(); + gameSettings->setEnableServerControlledAI(config.getBool + ("ServerControlledAI", + "true")); + gameSettings-> + setNetworkFramePeriod(config.getInt("NetworkSendFrameCount", "20")); + gameSettings->setNetworkPauseGameForLaggedClients(((checkBoxNetworkPauseGameForLaggedClients.getValue() == true))); - if (gameSettings->getTileset () != "") - { -// Check if client has different data, if so force a CRC refresh - bool forceRefresh = false; - if (checkNetworkPlayerDataSynch (false, true, false) == false && - last_Forced_CheckedCRCTilesetName != gameSettings->getTileset ()) - { - lastCheckedCRCTilesetName = ""; - forceRefresh = true; - last_Forced_CheckedCRCTilesetName = gameSettings->getTileset (); - } + if (gameSettings->getTileset() != "") { + // Check if client has different data, if so force a CRC refresh + bool forceRefresh = false; + if (checkNetworkPlayerDataSynch(false, true, false) == false && + last_Forced_CheckedCRCTilesetName != gameSettings->getTileset()) { + lastCheckedCRCTilesetName = ""; + forceRefresh = true; + last_Forced_CheckedCRCTilesetName = gameSettings->getTileset(); + } - if (lastCheckedCRCTilesetName != gameSettings->getTileset ()) - { -//console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); - lastCheckedCRCTilesetValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + gameSettings->getTileset () + string ("/*"), - ".xml", NULL, forceRefresh); - if (lastCheckedCRCTilesetValue == 0) - { - lastCheckedCRCTilesetValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTilesets, ""), - string ("/") + gameSettings->getTileset () + string ("/*"), - ".xml", NULL, true); - } - lastCheckedCRCTilesetName = gameSettings->getTileset (); - } - gameSettings->setTilesetCRC (lastCheckedCRCTilesetValue); - } + if (lastCheckedCRCTilesetName != gameSettings->getTileset()) { + //console.addLine("Checking tileset CRC [" + gameSettings->getTileset() + "]"); + lastCheckedCRCTilesetValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + gameSettings->getTileset() + string("/*"), + ".xml", NULL, forceRefresh); + if (lastCheckedCRCTilesetValue == 0) { + lastCheckedCRCTilesetValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTilesets, ""), + string("/") + gameSettings->getTileset() + string("/*"), + ".xml", NULL, true); + } + lastCheckedCRCTilesetName = gameSettings->getTileset(); + } + gameSettings->setTilesetCRC(lastCheckedCRCTilesetValue); + } - if (config.getBool ("DisableServerLobbyTechtreeCRCCheck", "false") == - false) - { - if (gameSettings->getTech () != "") - { -// Check if client has different data, if so force a CRC refresh - bool forceRefresh = false; - if (checkNetworkPlayerDataSynch (false, false, true) == false && - last_Forced_CheckedCRCTechtreeName != gameSettings->getTech ()) - { - lastCheckedCRCTechtreeName = ""; - forceRefresh = true; - last_Forced_CheckedCRCTechtreeName = gameSettings->getTech (); - } + if (config.getBool("DisableServerLobbyTechtreeCRCCheck", "false") == + false) { + if (gameSettings->getTech() != "") { + // Check if client has different data, if so force a CRC refresh + bool forceRefresh = false; + if (checkNetworkPlayerDataSynch(false, false, true) == false && + last_Forced_CheckedCRCTechtreeName != gameSettings->getTech()) { + lastCheckedCRCTechtreeName = ""; + forceRefresh = true; + last_Forced_CheckedCRCTechtreeName = gameSettings->getTech(); + } - if (lastCheckedCRCTechtreeName != gameSettings->getTech ()) - { -//console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); - lastCheckedCRCTechtreeValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/*", ".xml", NULL, - forceRefresh); - if (lastCheckedCRCTechtreeValue == 0) - { - lastCheckedCRCTechtreeValue = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + "/*", ".xml", NULL, true); - } + if (lastCheckedCRCTechtreeName != gameSettings->getTech()) { + //console.addLine("Checking techtree CRC [" + gameSettings->getTech() + "]"); + lastCheckedCRCTechtreeValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/*", ".xml", NULL, + forceRefresh); + if (lastCheckedCRCTechtreeValue == 0) { + lastCheckedCRCTechtreeValue = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + "/*", ".xml", NULL, true); + } - reloadFactions (true, - (checkBoxScenario.getValue () == - true ? - scenarioFiles - [listBoxScenario.getSelectedItemIndex ()] : "")); - factionCRCList.clear (); - for (unsigned int factionIdx = 0; - factionIdx < factionFiles.size (); ++factionIdx) - { - string factionName = factionFiles[factionIdx]; - if (factionName != GameConstants::RANDOMFACTION_SLOTNAME - && factionName != GameConstants::OBSERVER_SLOTNAME) - { -//factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); - uint32 factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, - ""), - "/" + - gameSettings->getTech - () + "/factions/" + factionName + "/*", - ".xml", - NULL); - if (factionCRC == 0) - { - factionCRC = - getFolderTreeContentsCheckSumRecursively - (config.getPathListForType (ptTechs, ""), - "/" + gameSettings->getTech () + - "/factions/" + factionName + "/*", ".xml", NULL, true); - } - factionCRCList.push_back (make_pair - (factionName, factionCRC)); - } - } -//console.addLine("Found factions: " + intToStr(factionCRCList.size())); - lastCheckedCRCTechtreeName = gameSettings->getTech (); - } + reloadFactions(true, + (checkBoxScenario.getValue() == + true ? + scenarioFiles + [listBoxScenario.getSelectedItemIndex()] : "")); + factionCRCList.clear(); + for (unsigned int factionIdx = 0; + factionIdx < factionFiles.size(); ++factionIdx) { + string factionName = factionFiles[factionIdx]; + if (factionName != GameConstants::RANDOMFACTION_SLOTNAME + && factionName != GameConstants::OBSERVER_SLOTNAME) { + //factionCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,""), "/" + gameSettings->getTech() + "/factions/" + factionName + "/*", ".xml", NULL, true); + uint32 factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, + ""), + "/" + + gameSettings->getTech + () + "/factions/" + factionName + "/*", + ".xml", + NULL); + if (factionCRC == 0) { + factionCRC = + getFolderTreeContentsCheckSumRecursively + (config.getPathListForType(ptTechs, ""), + "/" + gameSettings->getTech() + + "/factions/" + factionName + "/*", ".xml", NULL, true); + } + factionCRCList.push_back(make_pair + (factionName, factionCRC)); + } + } + //console.addLine("Found factions: " + intToStr(factionCRCList.size())); + lastCheckedCRCTechtreeName = gameSettings->getTech(); + } - gameSettings->setFactionCRCList (factionCRCList); - gameSettings->setTechCRC (lastCheckedCRCTechtreeValue); - } - } + gameSettings->setFactionCRCList(factionCRCList); + gameSettings->setTechCRC(lastCheckedCRCTechtreeValue); + } + } - if (gameSettings->getMap () != "") - { -// Check if client has different data, if so force a CRC refresh -//bool forceRefresh = false; - if (checkNetworkPlayerDataSynch (true, false, false) == false && - last_Forced_CheckedCRCMapName != gameSettings->getMap ()) - { - lastCheckedCRCMapName = ""; -//forceRefresh = true; - last_Forced_CheckedCRCMapName = gameSettings->getMap (); - } + if (gameSettings->getMap() != "") { + // Check if client has different data, if so force a CRC refresh + //bool forceRefresh = false; + if (checkNetworkPlayerDataSynch(true, false, false) == false && + last_Forced_CheckedCRCMapName != gameSettings->getMap()) { + lastCheckedCRCMapName = ""; + //forceRefresh = true; + last_Forced_CheckedCRCMapName = gameSettings->getMap(); + } - if (lastCheckedCRCMapName != gameSettings->getMap ()) - { - Checksum checksum; - string file = - Config::getMapPath (gameSettings->getMap (), "", false); -//console.addLine("Checking map CRC [" + file + "]"); - checksum.addFile (file); - lastCheckedCRCMapValue = checksum.getSum (); - lastCheckedCRCMapName = gameSettings->getMap (); - } - gameSettings->setMapCRC (lastCheckedCRCMapValue); - } + if (lastCheckedCRCMapName != gameSettings->getMap()) { + Checksum checksum; + string file = + Config::getMapPath(gameSettings->getMap(), "", false); + //console.addLine("Checking map CRC [" + file + "]"); + checksum.addFile(file); + lastCheckedCRCMapValue = checksum.getSum(); + lastCheckedCRCMapName = gameSettings->getMap(); + } + gameSettings->setMapCRC(lastCheckedCRCMapValue); + } - if (this->headlessServerMode == true) - { - time_t clientConnectedTime = 0; - bool masterserver_admin_found = false; + if (this->headlessServerMode == true) { + time_t clientConnectedTime = 0; + bool masterserver_admin_found = false; - for (int i = 0; i < mapInfo.players; ++i) - { + for (int i = 0; i < mapInfo.players; ++i) { #ifdef DEBUG - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); #endif - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (serverInterface->getSlot (i, true) != NULL - && serverInterface->getSlot (i, true)->isConnected ()) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("slot = %d serverInterface->getSlot(i)->getConnectedTime() = %d session key [%d]\n",i,serverInterface->getSlot(i)->getConnectedTime(),serverInterface->getSlot(i)->getSessionKey()); - - if (clientConnectedTime == 0 || - (serverInterface->getSlot (i, true)->getConnectedTime () > 0 - && serverInterface->getSlot (i, - true)->getConnectedTime () < - clientConnectedTime)) - { - clientConnectedTime = - serverInterface->getSlot (i, true)->getConnectedTime (); - gameSettings->setMasterserver_admin (serverInterface->getSlot - (i, - true)->getSessionKey - ()); - gameSettings->setMasterserver_admin_faction_index - (serverInterface->getSlot (i, true)->getPlayerIndex ()); - labelGameName.setText (createGameName - (serverInterface-> - getSlot (i, true)->getName ())); -//printf("slot = %d, admin key [%d] slot connected time[" MG_SIZE_T_SPECIFIER "] clientConnectedTime [" MG_SIZE_T_SPECIFIER "]\n",i,gameSettings->getMasterserver_admin(),serverInterface->getSlot(i)->getConnectedTime(),clientConnectedTime); - } - if (serverInterface->getSlot (i, true)->getSessionKey () == - gameSettings->getMasterserver_admin ()) - { - masterserver_admin_found = true; - } - } - } - } - if (masterserver_admin_found == false) - { - for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); -//ConnectionSlot *slot = serverInterface->getSlot(i); - - if (serverInterface->getSlot (i, true) != NULL - && serverInterface->getSlot (i, true)->isConnected ()) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("slot = %d serverInterface->getSlot(i)->getConnectedTime() = %d session key [%d]\n",i,serverInterface->getSlot(i)->getConnectedTime(),serverInterface->getSlot(i)->getSessionKey()); - - if (clientConnectedTime == 0 || - (serverInterface->getSlot (i, true)->getConnectedTime () > 0 - && serverInterface->getSlot (i, - true)->getConnectedTime () < - clientConnectedTime)) - { - clientConnectedTime = - serverInterface->getSlot (i, true)->getConnectedTime (); - gameSettings->setMasterserver_admin (serverInterface->getSlot - (i, - true)->getSessionKey - ()); - gameSettings->setMasterserver_admin_faction_index - (serverInterface->getSlot (i, true)->getPlayerIndex ()); - labelGameName.setText (createGameName - (serverInterface-> - getSlot (i, true)->getName ())); -//printf("slot = %d, admin key [%d] slot connected time[" MG_SIZE_T_SPECIFIER "] clientConnectedTime [" MG_SIZE_T_SPECIFIER "]\n",i,gameSettings->getMasterserver_admin(),serverInterface->getSlot(i)->getConnectedTime(),clientConnectedTime); - } - if (serverInterface->getSlot (i, true)->getSessionKey () == - gameSettings->getMasterserver_admin ()) - { - masterserver_admin_found = true; - } - } - } - } - - if (masterserver_admin_found == false) - { - labelGameName.setText (createGameName ()); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::saveGameSettingsToFile (std::string fileName) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - GameSettings gameSettings; - loadGameSettings (&gameSettings); - CoreData::getInstance ().saveGameSettingsToFile (fileName, - &gameSettings, - checkBoxAdvanced.getValue - ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - void MenuStateCustomGame::KeepCurrentHumanPlayerSlots (GameSettings & - gameSettings) - { -//look for human players - bool foundValidHumanControlTypeInFile = false; - for (int index2 = 0; index2 < GameConstants::maxPlayers; ++index2) - { - ControlType ctFile = - static_cast < ControlType > - (gameSettings.getFactionControl (index2)); - if (ctFile == ctHuman) - { - ControlType ctUI = - static_cast < ControlType > - (listBoxControls[index2].getSelectedItemIndex ()); - if (ctUI != ctNetwork && ctUI != ctNetworkUnassigned) - { - foundValidHumanControlTypeInFile = true; -//printf("Human found in file [%d]\n",index2); - } - else if (labelPlayerNames[index2].getText () == "") - { - foundValidHumanControlTypeInFile = true; - } - } - } - - for (int index = 0; index < GameConstants::maxPlayers; ++index) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[index].getSelectedItemIndex ()); - if (ct == ctHuman) - { -//printf("Human found in UI [%d] and file [%d]\n",index,foundControlType); - - if (foundValidHumanControlTypeInFile == false) - { - gameSettings.setFactionControl (index, ctHuman); - gameSettings.setNetworkPlayerName (index, getHumanPlayerName ()); - } - } - - ControlType ctFile = - static_cast < ControlType > - (gameSettings.getFactionControl (index)); - if (ctFile == ctHuman) - { - gameSettings.setFactionControl (index, ctHuman); - } - } - } - - GameSettings MenuStateCustomGame::loadGameSettingsFromFile (std::string - fileName) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - GameSettings gameSettings; - - GameSettings originalGameSettings; - loadGameSettings (&originalGameSettings); - - try - { - CoreData::getInstance ().loadGameSettingsFromFile (fileName, - &gameSettings); - KeepCurrentHumanPlayerSlots (gameSettings); - -// correct game settings for headless: - if (this->headlessServerMode == true) - { - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (gameSettings.getFactionControl (i) == ctHuman) - { - gameSettings.setFactionControl (i, ctNetwork); - } - } - } - setupUIFromGameSettings (gameSettings); - } - catch (const exception & ex) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] ERROR = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, ex.what ()); - - showMessageBox (ex.what (), "Error", false); - - setupUIFromGameSettings (originalGameSettings); - gameSettings = originalGameSettings; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - return gameSettings; - } - - void MenuStateCustomGame::setupUIFromGameSettings (const GameSettings & - gameSettings) - { - string humanPlayerName = getHumanPlayerName (); - - string scenarioDir = ""; - checkBoxScenario.setValue ((gameSettings.getScenario () != "")); - if (checkBoxScenario.getValue () == true) - { - listBoxScenario.setSelectedItem (formatString - (gameSettings.getScenario ())); - - loadScenarioInfo (Scenario::getScenarioPath (dirList, - scenarioFiles - [listBoxScenario.getSelectedItemIndex - ()]), &scenarioInfo); - scenarioDir = - Scenario::getScenarioDir (dirList, gameSettings.getScenario ()); - -//printf("scenarioInfo.fogOfWar = %d scenarioInfo.fogOfWar_exploredFlag = %d\n",scenarioInfo.fogOfWar,scenarioInfo.fogOfWar_exploredFlag); - if (scenarioInfo.fogOfWar == false - && scenarioInfo.fogOfWar_exploredFlag == false) - { - listBoxFogOfWar.setSelectedItemIndex (2); - } - else if (scenarioInfo.fogOfWar_exploredFlag == true) - { - listBoxFogOfWar.setSelectedItemIndex (1); - } - else - { - listBoxFogOfWar.setSelectedItemIndex (0); - } - checkBoxAllowTeamUnitSharing. - setValue (scenarioInfo.allowTeamUnitSharing); - checkBoxAllowTeamResourceSharing. - setValue (scenarioInfo.allowTeamResourceSharing); - } - setupMapList (gameSettings.getScenario ()); - setupTechList (gameSettings.getScenario (), false); - setupTilesetList (gameSettings.getScenario ()); - - if (checkBoxScenario.getValue () == true) - { -//string file = Scenario::getScenarioPath(dirList, gameSettings.getScenario()); -//loadScenarioInfo(file, &scenarioInfo); - -//printf("#6.1 about to load map [%s]\n",scenarioInfo.mapName.c_str()); -//loadMapInfo(Config::getMapPath(scenarioInfo.mapName, scenarioDir, true), &mapInfo, false); -//printf("#6.2\n"); - - listBoxMapFilter.setSelectedItemIndex (0); - listBoxMap.setItems (formattedPlayerSortedMaps[mapInfo.hardMaxPlayers]); - listBoxMap.setSelectedItem (formatString (scenarioInfo.mapName)); - } - else - { -//printf("gameSettings.getMapFilter()=%d \n",gameSettings.getMapFilter()); - if (gameSettings.getMapFilter () == 0) - { - listBoxMapFilter.setSelectedItemIndex (0); - } - else - { - listBoxMapFilter.setSelectedItem (intToStr - (gameSettings.getMapFilter ())); - } - listBoxMap.setItems (formattedPlayerSortedMaps - [gameSettings.getMapFilter ()]); - } - -//printf("gameSettings.getMap() [%s] [%s]\n",gameSettings.getMap().c_str(),listBoxMap.getSelectedItem().c_str()); - - string mapFile = gameSettings.getMap (); - if (find (mapFiles.begin (), mapFiles.end (), mapFile) != - mapFiles.end ()) - { - mapFile = formatString (mapFile); - listBoxMap.setSelectedItem (mapFile); - - loadMapInfo (Config::getMapPath - (getCurrentMapFile (), scenarioDir, true), &mapInfo, - true); - labelMapInfo.setText (mapInfo.desc); - } - - string tilesetFile = gameSettings.getTileset (); - if (find (tilesetFiles.begin (), tilesetFiles.end (), tilesetFile) != - tilesetFiles.end ()) - { - tilesetFile = formatString (tilesetFile); - listBoxTileset.setSelectedItem (tilesetFile); - } - - string techtreeFile = gameSettings.getTech (); - if (find (techTreeFiles.begin (), techTreeFiles.end (), techtreeFile) != - techTreeFiles.end ()) - { - techtreeFile = formatString (techtreeFile); - listBoxTechTree.setSelectedItem (techtreeFile); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//gameSettings->setDefaultUnits(true); -//gameSettings->setDefaultResources(true); -//gameSettings->setDefaultVictoryConditions(true); - -//FogOfWar - if (checkBoxScenario.getValue () == false) - { - listBoxFogOfWar.setSelectedItemIndex (0); // default is 0! - if (gameSettings.getFogOfWar () == false) - { - listBoxFogOfWar.setSelectedItemIndex (2); - } - - if ((gameSettings.getFlagTypes1 () & ft1_show_map_resources) == - ft1_show_map_resources) - { - if (gameSettings.getFogOfWar () == true) - { - listBoxFogOfWar.setSelectedItemIndex (1); - } - } - } - -//printf("In [%s::%s line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - checkBoxAllowObservers.setValue (gameSettings.getAllowObservers () == - true ? true : false); -//listBoxEnableObserverMode.setSelectedItem(gameSettings.getEnableObserverModeAtEndGame() == true ? lang.getString("Yes") : lang.getString("No")); - - checkBoxEnableSwitchTeamMode.setValue ((gameSettings.getFlagTypes1 () & - ft1_allow_team_switching) == - ft1_allow_team_switching ? true : - false); - listBoxAISwitchTeamAcceptPercent.setSelectedItem (intToStr - (gameSettings.getAiAcceptSwitchTeamPercentChance - ())); - listBoxFallbackCpuMultiplier. - setSelectedItemIndex (gameSettings.getFallbackCpuMultiplier ()); - - checkBoxAllowInGameJoinPlayer.setValue ((gameSettings.getFlagTypes1 () & - ft1_allow_in_game_joining) == - ft1_allow_in_game_joining ? true - : false); - - checkBoxAllowTeamUnitSharing.setValue ((gameSettings.getFlagTypes1 () & - ft1_allow_shared_team_units) == - ft1_allow_shared_team_units ? - true : false); - checkBoxAllowTeamResourceSharing.setValue ((gameSettings.getFlagTypes1 - () & - ft1_allow_shared_team_resources) - == - ft1_allow_shared_team_resources - ? true : false); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (serverInterface != NULL) - { - serverInterface->setAllowInGameConnections - (checkBoxAllowInGameJoinPlayer.getValue () == true); - } - - checkBoxAllowNativeLanguageTechtree. - setValue (gameSettings.getNetworkAllowNativeLanguageTechtree ()); - -//listBoxPathFinderType.setSelectedItemIndex(gameSettings.getPathFinderType()); - -//listBoxEnableServerControlledAI.setSelectedItem(gameSettings.getEnableServerControlledAI() == true ? lang.getString("Yes") : lang.getString("No")); - -//labelNetworkFramePeriod.setText(lang.getString("NetworkFramePeriod")); - -//listBoxNetworkFramePeriod.setSelectedItem(intToStr(gameSettings.getNetworkFramePeriod()/10*10)); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - checkBoxNetworkPauseGameForLaggedClients. - setValue (gameSettings.getNetworkPauseGameForLaggedClients ()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - reloadFactions (false, - (checkBoxScenario.getValue () == - true ? - scenarioFiles[listBoxScenario.getSelectedItemIndex ()] - : "")); -//reloadFactions(true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d] gameSettings.getFactionCount() = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - gameSettings.getFactionCount ()); - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - int slotIndex = gameSettings.getStartLocationIndex (i); - if (gameSettings.getFactionControl (i) < - listBoxControls[slotIndex].getItemCount ()) - { - listBoxControls[slotIndex]. - setSelectedItemIndex (gameSettings.getFactionControl (i)); - } - -//if(slotIndex == 0) printf("#2 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); - - updateResourceMultiplier (slotIndex); - -//if(slotIndex == 0) printf("#3 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); - - listBoxRMultiplier[slotIndex]. - setSelectedItemIndex (gameSettings.getResourceMultiplierIndex (i)); - -//if(slotIndex == 0) printf("#4 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); - - listBoxTeams[slotIndex]. - setSelectedItemIndex (gameSettings.getTeam (i)); - - lastSelectedTeamIndex[slotIndex] = - listBoxTeams[slotIndex].getSelectedItemIndex (); - - string factionName = gameSettings.getFactionTypeName (i); - factionName = formatString (factionName); - -//printf("\n\n\n*** setupUIFromGameSettings A, i = %d, startLoc = %d, factioncontrol = %d, factionName [%s]\n",i,gameSettings.getStartLocationIndex(i),gameSettings.getFactionControl(i),factionName.c_str()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] factionName = [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, factionName.c_str ()); - - if (listBoxFactions[slotIndex].hasItem (factionName) == true) - { - listBoxFactions[slotIndex].setSelectedItem (factionName); - } - else - { - listBoxFactions[slotIndex].setSelectedItem (formatString - (GameConstants:: - RANDOMFACTION_SLOTNAME)); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] i = %d, gameSettings.getNetworkPlayerName(i) [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - gameSettings. - getNetworkPlayerName (i).c_str ()); - -//labelPlayerNames[slotIndex].setText(gameSettings.getNetworkPlayerName(i)); - } - -//SetActivePlayerNameEditor(); - - updateControllers (); - updateNetworkSlots (); - - if (this->headlessServerMode == false && humanPlayerName != "") - { - for (int index = 0; index < GameConstants::maxPlayers; ++index) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[index].getSelectedItemIndex ()); - if (ct == ctHuman) - { - if (humanPlayerName != labelPlayerNames[index].getText ()) - { -//printf("Player name changing from [%s] to [%s]\n",labelPlayerNames[index].getText().c_str(),humanPlayerName.c_str()); - - labelPlayerNames[index].setText (""); - labelPlayerNames[index].setText (humanPlayerName); - } - } - } - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } -// ============ PRIVATE =========================== - - void MenuStateCustomGame::lastPlayerDisconnected () - { -// this is for headless mode only! -// if last player disconnects we load the network defaults. - if (this->headlessServerMode == false) - { - return; - } - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - bool foundConnectedPlayer = false; - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (serverInterface->getSlot (i, true) != NULL && - (listBoxControls[i].getSelectedItemIndex () == ctNetwork || - listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned)) - { - if (serverInterface->getSlot (i, true)->isConnected () == true) - { - foundConnectedPlayer = true; - } - } - } - - if (!foundConnectedPlayer && headlessHasConnectedPlayer == true) - { -// load defaults - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - if (fileExists (data_path + DEFAULT_NETWORKGAME_FILENAME) == true) - loadGameSettings (data_path + DEFAULT_NETWORKGAME_FILENAME); - } - headlessHasConnectedPlayer = foundConnectedPlayer; - } - - bool MenuStateCustomGame::hasNetworkGameSettings () - { - bool hasNetworkSlot = false; - - try - { - for (int i = 0; i < mapInfo.players; ++i) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct != ctClosed) - { - if (ct == ctNetwork || ct == ctNetworkUnassigned) - { - hasNetworkSlot = true; - break; - } - } - } - if (hasNetworkSlot == false) - { - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct != ctClosed) - { - if (ct == ctNetworkUnassigned) - { - hasNetworkSlot = true; - break; - } - } - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - ex.what ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showGeneralError = true; - generalErrorToShow = szBuf; - } - - return hasNetworkSlot; - } - - void MenuStateCustomGame::loadMapInfo (string file, MapInfo * mapInfo, - bool loadMapPreview) - { - try - { - Lang & lang = Lang::getInstance (); - if (MapPreview::loadMapInfo - (file, mapInfo, lang.getString ("MaxPlayers"), - lang.getString ("Size"), true) == true) - { - // Though we prefer not to change the actual value of mapInfo->players, - // which is the number of players assigned when making the map, we still know - // the actual value from mapInfo.hardMaxPlayers. Changing it here means - // not having to change a variable name in many places of this file - // to implement enhanced observer mode (issue #13)' - if (checkBoxAllowObservers.getValue () == 1) - { - mapInfo->players = GameConstants::maxPlayers; - } - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (serverInterface->getSlot (i, true) != NULL && - (listBoxControls[i].getSelectedItemIndex () == ctNetwork - || listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned)) - { - if (serverInterface->getSlot (i, true)->isConnected () == true) - { - if (i + 1 > mapInfo->players && - listBoxControls[i].getSelectedItemIndex () != - ctNetworkUnassigned) - { - listBoxControls[i].setSelectedItemIndex - (ctNetworkUnassigned); - } - } - } - } - -// Not painting properly so this is on hold - if (loadMapPreview == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - mapPreview.loadFromFile (file.c_str ()); - -//printf("Loading map preview MAP\n"); - cleanupMapPreviewTexture (); - } - } - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s] loading map [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - e.what (), file.c_str ()); - throw megaglest_runtime_error ("Error loading map file: [" + file + - "] msg: " + e.what ()); - } - } - - void MenuStateCustomGame::closeUnusedSlots () - { - try - { - if (checkBoxScenario.getValue () == false) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); -//for(int i= 0; igetSlot (i, true) == NULL || - serverInterface->getSlot (i, - true)->isConnected () == - false - || serverInterface->getSlot (i, - true)->getConnectHasHandshaked - () == false) - { -//printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); - - listBoxControls[i].setSelectedItemIndex (ctClosed); - } - } - } - updateNetworkSlots (); - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - bool MenuStateCustomGame::textInput (std::string text) - { -//printf("In [%s::%s Line: %d] text [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,text.c_str()); - if (activeInputLabel != NULL) - { - bool handled = textInputEditLabel (text, &activeInputLabel); - if (handled == true && &labelGameName != activeInputLabel) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - } - else - { - if (hasNetworkGameSettings () == true) - { - chatManager.textInput (text); - } - } - return false; - } - - void MenuStateCustomGame::keyDown (SDL_KeyboardEvent key) - { - if (isMasterserverMode () == true) - { - return; - } - - if (activeInputLabel != NULL) - { - bool handled = keyDownEditLabel (key, &activeInputLabel); - if (handled == true) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - } - else - { -//send key to the chat manager - if (hasNetworkGameSettings () == true) - { - chatManager.keyDown (key); - } - if (chatManager.getEditEnabled () == false && - (::Shared::Platform::Window::isKeyStateModPressed (KMOD_SHIFT) == - false)) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - -//if(key == configKeys.getCharKey("ShowFullConsole")) { - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) - == true) - { - showFullConsole = true; - } -//Toggle music -//else if(key == configKeys.getCharKey("ToggleMusic")) { - else - if (isKeyPressed (configKeys.getSDLKey ("ToggleMusic"), key) - == true) - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - float configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - float currentVolume = - CoreData::getInstance ().getMenuMusic ()->getVolume (); - if (currentVolume > 0) - { - CoreData::getInstance ().getMenuMusic ()->setVolume (0.f); - console.addLine (lang.getString ("GameMusic") + " " + - lang.getString ("Off")); - } - else - { - CoreData::getInstance (). - getMenuMusic ()->setVolume (configVolume); -//If the config says zero, use the default music volume -//gameMusic->setVolume(configVolume ? configVolume : 0.9); - console.addLine (lang.getString ("GameMusic")); - } - } -//else if(key == configKeys.getCharKey("SaveGUILayout")) { - else - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) - == true) - { - bool saved = - GraphicComponent::saveAllCustomProperties (containerName); - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("GUILayoutSaved") + " [" + - (saved ? lang. - getString ("Yes") : lang.getString ("No")) + - "]"); - } - } - } - } - - void MenuStateCustomGame::keyPress (SDL_KeyboardEvent c) - { - if (isMasterserverMode () == true) - { - return; - } - - if (activeInputLabel != NULL) - { - bool handled = keyPressEditLabel (c, &activeInputLabel); - if (handled == true && &labelGameName != activeInputLabel) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - } - } - else - { - if (hasNetworkGameSettings () == true) - { - chatManager.keyPress (c); - } - } - } - - void MenuStateCustomGame::keyUp (SDL_KeyboardEvent key) - { - if (isMasterserverMode () == true) - { - return; - } - - if (activeInputLabel == NULL) - { - if (hasNetworkGameSettings () == true) - { - chatManager.keyUp (key); - } - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - if (chatManager.getEditEnabled ()) - { -//send key to the chat manager - if (hasNetworkGameSettings () == true) - { - chatManager.keyUp (key); - } - } -//else if(key == configKeys.getCharKey("ShowFullConsole")) { - else - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) - == true) - { - showFullConsole = false; - } - } - } - - void MenuStateCustomGame::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void MenuStateCustomGame::switchToNextMapGroup (const int direction) - { - int i = listBoxMapFilter.getSelectedItemIndex (); -// if there are no maps for the current selection we switch to next selection - while (formattedPlayerSortedMaps[i].empty ()) - { - i = i + direction; - if (i > GameConstants::maxPlayers) - { - i = 0; - } - if (i < 0) - { - i = GameConstants::maxPlayers; - } - } - listBoxMapFilter.setSelectedItemIndex (i); - listBoxMap.setItems (formattedPlayerSortedMaps[i]); - } - - string MenuStateCustomGame::getCurrentMapFile () - { - int i = listBoxMapFilter.getSelectedItemIndex (); - int mapIndex = listBoxMap.getSelectedItemIndex (); - if (playerSortedMaps[i].empty () == false) - { - return playerSortedMaps[i].at (mapIndex); - } - return ""; - } - - void MenuStateCustomGame::setActiveInputLabel (GraphicLabel * newLable) - { - MenuState::setActiveInputLabel (newLable, &activeInputLabel); - } - - string MenuStateCustomGame::getHumanPlayerName (int index) - { - string result = defaultPlayerName; - if (index < 0) - { - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - if (listBoxControls[j].getSelectedItemIndex () >= 0) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[j].getSelectedItemIndex ()); - if (ct == ctHuman) - { - index = j; - break; - } - } - } - } - - if (index >= 0 && index < GameConstants::maxPlayers && - labelPlayerNames[index].getText () != "" && - labelPlayerNames[index].getText () != - GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) - { - result = labelPlayerNames[index].getText (); - - if (activeInputLabel != NULL) - { - size_t found = result.find_last_of ("_"); - if (found != string::npos) - { - result = result.substr (0, found); - } - } - } - - return result; - } - - void MenuStateCustomGame::loadFactionTexture (string filepath) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (enableFactionTexturePreview == true) - { - if (filepath == "") - { - factionTexture = NULL; - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] filepath = [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - filepath.c_str ()); - - factionTexture = Renderer::findTexture (filepath); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - } - } - - void MenuStateCustomGame::cleanupMapPreviewTexture () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -//printf("CLEANUP map preview texture\n"); - - if (mapPreviewTexture != NULL) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - mapPreviewTexture->end (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - delete mapPreviewTexture; - mapPreviewTexture = NULL; - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - int32 MenuStateCustomGame::getNetworkPlayerStatus () - { - int32 result = npst_None; - switch (listBoxPlayerStatus.getSelectedItemIndex ()) - { - case 2: - result = npst_Ready; - break; - case 1: - result = npst_BeRightBack; - break; - case 0: - default: - result = npst_PickSettings; - break; - } - - return result; - } - - void MenuStateCustomGame::loadScenarioInfo (string file, - ScenarioInfo * scenarioInfo) - { -//printf("Load scenario file [%s]\n",file.c_str()); - bool isTutorial = Scenario::isGameTutorial (file); - Scenario::loadScenarioInfo (file, scenarioInfo, isTutorial); - -//cleanupPreviewTexture(); - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - } - - bool MenuStateCustomGame::isInSpecialKeyCaptureEvent () - { - bool result = (chatManager.getEditEnabled () - || activeInputLabel != NULL); - return result; - } - - void MenuStateCustomGame::processScenario () - { - try - { - if (checkBoxScenario.getValue () == true) - { -//printf("listBoxScenario.getSelectedItemIndex() = %d [%s] scenarioFiles.size() = %d\n",listBoxScenario.getSelectedItemIndex(),listBoxScenario.getSelectedItem().c_str(),scenarioFiles.size()); - loadScenarioInfo (Scenario::getScenarioPath (dirList, - scenarioFiles - [listBoxScenario.getSelectedItemIndex - ()]), &scenarioInfo); - string scenarioDir = - Scenario::getScenarioDir (dirList, scenarioInfo.name); - -//printf("scenarioInfo.fogOfWar = %d scenarioInfo.fogOfWar_exploredFlag = %d\n",scenarioInfo.fogOfWar,scenarioInfo.fogOfWar_exploredFlag); - if (scenarioInfo.fogOfWar == false - && scenarioInfo.fogOfWar_exploredFlag == false) - { - listBoxFogOfWar.setSelectedItemIndex (2); - } - else if (scenarioInfo.fogOfWar_exploredFlag == true) - { - listBoxFogOfWar.setSelectedItemIndex (1); - } - else - { - listBoxFogOfWar.setSelectedItemIndex (0); - } - - checkBoxAllowTeamUnitSharing. - setValue (scenarioInfo.allowTeamUnitSharing); - checkBoxAllowTeamResourceSharing. - setValue (scenarioInfo.allowTeamResourceSharing); - - setupTechList (scenarioInfo.name, false); - listBoxTechTree.setSelectedItem (formatString - (scenarioInfo.techTreeName)); - reloadFactions (false, scenarioInfo.name); - - setupTilesetList (scenarioInfo.name); - listBoxTileset.setSelectedItem (formatString - (scenarioInfo.tilesetName)); - - checkBoxAllowObservers.setValue (false); - - setupMapList (scenarioInfo.name); - listBoxMap.setSelectedItem (formatString (scenarioInfo.mapName)); - loadMapInfo (Config::getMapPath - (getCurrentMapFile (), scenarioDir, true), &mapInfo, - true); - labelMapInfo.setText (mapInfo.desc); - -//printf("scenarioInfo.name [%s] [%s]\n",scenarioInfo.name.c_str(),listBoxMap.getSelectedItem().c_str()); - -// Loop twice to set the human slot or else it closes network slots in some cases - for (int humanIndex = 0; humanIndex < 2; ++humanIndex) - { - for (int i = 0; i < mapInfo.players; ++i) - { - listBoxRMultiplier[i].setSelectedItem (floatToStr - (scenarioInfo. - resourceMultipliers[i], - 1)); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = serverInterface->getSlot (i, true); - - int selectedControlItemIndex = - listBoxControls[i].getSelectedItemIndex (); - if (selectedControlItemIndex != ctNetwork - || (selectedControlItemIndex == ctNetwork - && (slot == NULL || slot->isConnected () == false))) - { - } - - listBoxControls[i]. - setSelectedItemIndex (scenarioInfo.factionControls[i]); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - -// Skip over networkunassigned -//if(listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned && -// selectedControlItemIndex != ctNetworkUnassigned) { -// listBoxControls[i].mouseClick(x, y); -//} - -//look for human players - int humanIndex1 = -1; - int humanIndex2 = -1; - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - ControlType ct = - static_cast < ControlType > - (listBoxControls[j].getSelectedItemIndex ()); - if (ct == ctHuman) - { - if (humanIndex1 == -1) - { - humanIndex1 = j; - } - else - { - humanIndex2 = j; - } - } - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] humanIndex1 = %d, humanIndex2 = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, humanIndex1, humanIndex2); - -//no human - if (humanIndex1 == -1 && humanIndex2 == -1) - { - setSlotHuman (i); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] i = %d, labelPlayerNames[i].getText() [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, i, - labelPlayerNames[i].getText (). - c_str ()); - -//printf("humanIndex1 = %d humanIndex2 = %d i = %d listBoxControls[i].getSelectedItemIndex() = %d\n",humanIndex1,humanIndex2,i,listBoxControls[i].getSelectedItemIndex()); - } -//2 humans - else if (humanIndex1 != -1 && humanIndex2 != -1) - { - int closeSlotIndex = - (humanIndex1 == i ? humanIndex2 : humanIndex1); - int humanSlotIndex = - (closeSlotIndex == humanIndex1 ? humanIndex2 : humanIndex1); - - string origPlayName = - labelPlayerNames[closeSlotIndex].getText (); - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] closeSlotIndex = %d, origPlayName [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, closeSlotIndex, - origPlayName.c_str ()); -//printf("humanIndex1 = %d humanIndex2 = %d i = %d closeSlotIndex = %d humanSlotIndex = %d\n",humanIndex1,humanIndex2,i,closeSlotIndex,humanSlotIndex); - - listBoxControls[closeSlotIndex].setSelectedItemIndex - (ctClosed); - labelPlayerNames[humanSlotIndex].setText ((origPlayName != - "" ? origPlayName : - getHumanPlayerName - ())); - } - - ControlType ct = - static_cast < ControlType > - (listBoxControls[i].getSelectedItemIndex ()); - if (ct != ctClosed) - { -//updateNetworkSlots(); -//updateResourceMultiplier(i); - updateResourceMultiplier (i); - -//printf("Setting scenario faction i = %d [ %s]\n",i,scenarioInfo.factionTypeNames[i].c_str()); - listBoxFactions[i].setSelectedItem (formatString - (scenarioInfo.factionTypeNames - [i])); -//printf("DONE Setting scenario faction i = %d [ %s]\n",i,scenarioInfo.factionTypeNames[i].c_str()); - -// Disallow CPU players to be observers - if (factionFiles - [listBoxFactions[i].getSelectedItemIndex ()] == - formatString (GameConstants::OBSERVER_SLOTNAME) - && (listBoxControls[i].getSelectedItemIndex () == - ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta)) - { - listBoxFactions[i].setSelectedItemIndex (0); - } -// - - listBoxTeams[i].setSelectedItem (intToStr - (scenarioInfo.teams[i])); - if (factionFiles[listBoxFactions[i].getSelectedItemIndex ()] - != formatString (GameConstants::OBSERVER_SLOTNAME)) - { - if (listBoxTeams[i].getSelectedItemIndex () + 1 != - (GameConstants::maxPlayers + fpt_Observer)) - { - lastSelectedTeamIndex[i] = - listBoxTeams[i].getSelectedItemIndex (); - } -// Alow Neutral cpu players - else if (listBoxControls[i].getSelectedItemIndex () == - ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == - ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta) - { - lastSelectedTeamIndex[i] = - listBoxTeams[i].getSelectedItemIndex (); - } - } - else - { - lastSelectedTeamIndex[i] = -1; - } - } - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL);; - } - } - } - - updateControllers (); - updateNetworkSlots (); - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - } - if (hasNetworkGameSettings () == true) - { - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); - } - -//labelInfo.setText(scenarioInfo.desc); - } - else - { - setupMapList (""); - listBoxMap.setSelectedItem (formatString - (formattedPlayerSortedMaps[0][0])); - loadMapInfo (Config::getMapPath (getCurrentMapFile (), "", true), - &mapInfo, true); - - labelMapInfo.setText (mapInfo.desc); - - setupTechList ("", false); - reloadFactions (false, ""); - setupTilesetList (""); - updateControllers (); - } - SetupUIForScenarios (); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - mainMessageBoxState = 1; - showMessageBox (szBuf, "Error detected", false); - } - } - - void MenuStateCustomGame::SetupUIForScenarios () - { - try - { - if (checkBoxScenario.getValue () == true) - { -// START - Disable changes to controls while in Scenario mode - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxControls[i].setEditable (false); - listBoxFactions[i].setEditable (false); - listBoxRMultiplier[i].setEditable (false); - listBoxTeams[i].setEditable (false); - } - listBoxFogOfWar.setEditable (false); - checkBoxAllowObservers.setEditable (false); - checkBoxAllowTeamUnitSharing.setEditable (false); - checkBoxAllowTeamResourceSharing.setEditable (false); -//listBoxPathFinderType.setEditable(false); - checkBoxEnableSwitchTeamMode.setEditable (false); - listBoxAISwitchTeamAcceptPercent.setEditable (false); - listBoxFallbackCpuMultiplier.setEditable (false); - listBoxMap.setEditable (false); - listBoxTileset.setEditable (false); - listBoxMapFilter.setEditable (false); - listBoxTechTree.setEditable (false); -// END - Disable changes to controls while in Scenario mode - } - else - { -// START - Disable changes to controls while in Scenario mode - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - listBoxControls[i].setEditable (true); - listBoxFactions[i].setEditable (true); - listBoxRMultiplier[i].setEditable (true); - listBoxTeams[i].setEditable (true); - } - listBoxFogOfWar.setEditable (true); - checkBoxAllowObservers.setEditable (true); - checkBoxAllowTeamUnitSharing.setEditable (true); - checkBoxAllowTeamResourceSharing.setEditable (true); -//listBoxPathFinderType.setEditable(true); - checkBoxEnableSwitchTeamMode.setEditable (true); - listBoxAISwitchTeamAcceptPercent.setEditable (true); - listBoxFallbackCpuMultiplier.setEditable (true); - listBoxMap.setEditable (true); - listBoxTileset.setEditable (true); - listBoxMapFilter.setEditable (true); - listBoxTechTree.setEditable (true); -// END - Disable changes to controls while in Scenario mode - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - - } - - int MenuStateCustomGame::setupMapList (string scenario) - { - int initialMapSelection = 0; - - try - { - Config & config = Config::getInstance (); - vector < string > invalidMapList; - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > pathList = - config.getPathListForType (ptMaps, scenarioDir); - vector < string > allMaps = - MapPreview::findAllValidMaps (pathList, scenarioDir, false, true, - &invalidMapList); - if (scenario != "") - { - vector < string > allMaps2 = - MapPreview:: - findAllValidMaps (config.getPathListForType (ptMaps, ""), "", - false, true, &invalidMapList); - copy (allMaps2.begin (), allMaps2.end (), - std::inserter (allMaps, allMaps.begin ())); - } -// sort map list non case sensitive - std::sort (allMaps.begin (), allMaps.end (), compareNonCaseSensitive); - - if (allMaps.empty ()) - { - throw megaglest_runtime_error ("No maps were found!"); - } - vector < string > results; - copy (allMaps.begin (), allMaps.end (), std::back_inserter (results)); - mapFiles = results; - - for (unsigned int i = 0; i < GameConstants::maxPlayers + 1; ++i) - { - playerSortedMaps[i].clear (); - formattedPlayerSortedMaps[i].clear (); - } - -// at index=0 fill in the whole list - copy (mapFiles.begin (), mapFiles.end (), - std::back_inserter (playerSortedMaps[0])); - copy (playerSortedMaps[0].begin (), playerSortedMaps[0].end (), - std::back_inserter (formattedPlayerSortedMaps[0])); - std::for_each (formattedPlayerSortedMaps[0].begin (), - formattedPlayerSortedMaps[0].end (), FormatString ()); - -// fill playerSortedMaps and formattedPlayerSortedMaps according to map player count - for (int i = 0; i < (int) mapFiles.size (); i++) - { // fetch info and put map in right list - loadMapInfo (Config::getMapPath - (mapFiles.at (i), scenarioDir, false), &mapInfo, - false); - - if (GameConstants::maxPlayers + 1 <= mapInfo.players) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Sorted map list [%d] does not match\ncurrent map playercount [%d]\nfor file [%s]\nmap [%s]", - GameConstants::maxPlayers + 1, mapInfo.players, - Config::getMapPath (mapFiles.at (i), "", - false).c_str (), - mapInfo.desc.c_str ()); - throw megaglest_runtime_error (szBuf); - } - playerSortedMaps[mapInfo.hardMaxPlayers].push_back (mapFiles.at (i)); - formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].push_back (formatString - (mapFiles.at - (i))); - if (config.getString ("InitialMap", "Conflict") == - formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].back ()) - { - initialMapSelection = i; - } - } - -//printf("#6 scenario [%s] [%s]\n",scenario.c_str(),scenarioDir.c_str()); - if (scenario != "") - { - string file = Scenario::getScenarioPath (dirList, scenario); - loadScenarioInfo (file, &scenarioInfo); - -//printf("#6.1 about to load map [%s]\n",scenarioInfo.mapName.c_str()); - loadMapInfo (Config::getMapPath - (scenarioInfo.mapName, scenarioDir, true), &mapInfo, - false); - - listBoxMapFilter.setSelectedItem (intToStr (mapInfo.players)); - listBoxMap.setItems (formattedPlayerSortedMaps[mapInfo.players]); - } - else - { - listBoxMapFilter.setSelectedItemIndex (0); - listBoxMap.setItems (formattedPlayerSortedMaps[0]); - } -//printf("#7\n"); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); -//abort(); - } - - return initialMapSelection; - } - - int MenuStateCustomGame::setupTechList (string scenario, bool forceLoad) - { - int initialTechSelection = 0; - try - { - Config & config = Config::getInstance (); - - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > results; - vector < string > techPaths = - config.getPathListForType (ptTechs, scenarioDir); - findDirs (techPaths, results); - - if (results.empty ()) - { -//throw megaglest_runtime_error("No tech-trees were found!"); - printf ("No tech-trees were found (custom)!\n"); - } - - techTreeFiles = results; - - vector < string > translatedTechs; - - for (unsigned int i = 0; i < results.size (); i++) - { -//printf("TECHS i = %d results [%s] scenario [%s]\n",i,results[i].c_str(),scenario.c_str()); - - results.at (i) = formatString (results.at (i)); - if (config.getString ("InitialTechTree", "Megapack") == - results.at (i)) - { - initialTechSelection = i; - } - string txTech = - techTree->getTranslatedName (techTreeFiles.at (i), forceLoad); - translatedTechs.push_back (formatString (txTech)); - } - - - listBoxTechTree.setItems (results, translatedTechs); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - - return initialTechSelection; - } - - void MenuStateCustomGame::reloadFactions (bool keepExistingSelectedItem, - string scenario) - { - try - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - vector < string > results; - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - vector < string > techPaths = - config.getPathListForType (ptTechs, scenarioDir); - -//printf("#1 techPaths.size() = %d scenarioDir [%s] [%s]\n",techPaths.size(),scenario.c_str(),scenarioDir.c_str()); - - if (listBoxTechTree.getItemCount () > 0) - { - for (int idx = 0; idx < (int) techPaths.size (); idx++) - { - string & techPath = techPaths[idx]; - endPathWithSlash (techPath); - string factionPath = - techPath + - techTreeFiles[listBoxTechTree.getSelectedItemIndex ()] + - "/factions/"; - findDirs (factionPath, results, false, false); - -//printf("idx = %d factionPath [%s] results.size() = %d\n",idx,factionPath.c_str(),results.size()); - - if (results.empty () == false) - { - break; - } - } - } - - if (results.empty () == true) - { -//throw megaglest_runtime_error("(2)There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"); - showGeneralError = true; - if (listBoxTechTree.getItemCount () > 0) - { - generalErrorToShow = - "[#2] There are no factions for the tech tree [" + - techTreeFiles[listBoxTechTree.getSelectedItemIndex ()] + "]"; - } - else - { - generalErrorToShow = - "[#2] There are no factions since there is no tech tree!"; - } - } - -// results.push_back(formatString(GameConstants::RANDOMFACTION_SLOTNAME)); -// -// // Add special Observer Faction -// if(checkBoxAllowObservers.getValue() == 1) { -// results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); -// } - - vector < string > translatedFactionNames; - factionFiles = results; - for (int i = 0; i < (int) results.size (); ++i) - { - results[i] = formatString (results[i]); - - string translatedString = ""; - if (listBoxTechTree.getItemCount () > 0) - { - translatedString = - techTree->getTranslatedFactionName (techTreeFiles - [listBoxTechTree.getSelectedItemIndex - ()], factionFiles[i]); - } -//printf("translatedString=%s formatString(results[i])=%s \n",translatedString.c_str(),formatString(results[i]).c_str() ); - if (toLower (translatedString) == toLower (results[i])) - { - translatedFactionNames.push_back (results[i]); - } - else - { - translatedFactionNames.push_back (results[i] + " (" + - translatedString + ")"); - } -//printf("FACTIONS i = %d results [%s]\n",i,results[i].c_str()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "Tech [%s] has faction [%s]\n", - techTreeFiles - [listBoxTechTree.getSelectedItemIndex - ()].c_str (), results[i].c_str ()); - } - results.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::RANDOMFACTION_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Random", "", - true) + "*"); - -// Add special Observer Faction - if (checkBoxAllowObservers.getValue () == 1) - { - results.push_back (formatString (GameConstants::OBSERVER_SLOTNAME)); - factionFiles.push_back (formatString - (GameConstants::OBSERVER_SLOTNAME)); - translatedFactionNames.push_back ("*" + - lang.getString ("Observer", "", - true) + "*"); - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - int originalIndex = listBoxFactions[i].getSelectedItemIndex (); - string originalValue = - (listBoxFactions[i].getItemCount () > - 0 ? listBoxFactions[i].getSelectedItem () : ""); - - listBoxFactions[i].setItems (results, translatedFactionNames); - if (keepExistingSelectedItem == false || - (checkBoxAllowObservers.getValue () == 0 && - originalValue == - formatString (GameConstants::OBSERVER_SLOTNAME))) - { - listBoxFactions[i].setSelectedItemIndex (i % results.size ()); - - if (originalValue == - formatString (GameConstants::OBSERVER_SLOTNAME) - && listBoxFactions[i].getSelectedItem () != - formatString (GameConstants::OBSERVER_SLOTNAME)) - { - if (listBoxTeams[i].getSelectedItem () == - intToStr (GameConstants::maxPlayers + fpt_Observer)) - { - listBoxTeams[i].setSelectedItem (intToStr (1)); - } - } - } - else if (originalIndex < (int) results.size ()) - { - listBoxFactions[i].setSelectedItemIndex (originalIndex); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - } - - void MenuStateCustomGame::setSlotHuman (int i) - { - if (labelPlayerNames[i].getEditable ()) - { - return; - } - listBoxControls[i].setSelectedItemIndex (ctHuman); - listBoxRMultiplier[i].setSelectedItem ("1.0"); - - labelPlayerNames[i].setText (getHumanPlayerName ()); - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - labelPlayerNames[j].setEditable (false); - } - labelPlayerNames[i].setEditable (true); - } - - void MenuStateCustomGame::setupTilesetList (string scenario) - { - try - { - Config & config = Config::getInstance (); - - string scenarioDir = Scenario::getScenarioDir (dirList, scenario); - - vector < string > results; - findDirs (config.getPathListForType (ptTilesets, scenarioDir), - results); - if (results.empty ()) - { - throw megaglest_runtime_error ("No tile-sets were found!"); - } - tilesetFiles = results; - std::for_each (results.begin (), results.end (), FormatString ()); - - listBoxTileset.setItems (results); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - throw megaglest_runtime_error (szBuf); - } - - } - - } + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (serverInterface->getSlot(i, true) != NULL + && serverInterface->getSlot(i, true)->isConnected()) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("slot = %d serverInterface->getSlot(i)->getConnectedTime() = %d session key [%d]\n",i,serverInterface->getSlot(i)->getConnectedTime(),serverInterface->getSlot(i)->getSessionKey()); + + if (clientConnectedTime == 0 || + (serverInterface->getSlot(i, true)->getConnectedTime() > 0 + && serverInterface->getSlot(i, + true)->getConnectedTime() < + clientConnectedTime)) { + clientConnectedTime = + serverInterface->getSlot(i, true)->getConnectedTime(); + gameSettings->setMasterserver_admin(serverInterface->getSlot + (i, + true)->getSessionKey + ()); + gameSettings->setMasterserver_admin_faction_index + (serverInterface->getSlot(i, true)->getPlayerIndex()); + labelGameName.setText(createGameName + (serverInterface-> + getSlot(i, true)->getName())); + //printf("slot = %d, admin key [%d] slot connected time[" MG_SIZE_T_SPECIFIER "] clientConnectedTime [" MG_SIZE_T_SPECIFIER "]\n",i,gameSettings->getMasterserver_admin(),serverInterface->getSlot(i)->getConnectedTime(),clientConnectedTime); + } + if (serverInterface->getSlot(i, true)->getSessionKey() == + gameSettings->getMasterserver_admin()) { + masterserver_admin_found = true; + } + } + } + } + if (masterserver_admin_found == false) { + for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + //ConnectionSlot *slot = serverInterface->getSlot(i); + + if (serverInterface->getSlot(i, true) != NULL + && serverInterface->getSlot(i, true)->isConnected()) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("slot = %d serverInterface->getSlot(i)->getConnectedTime() = %d session key [%d]\n",i,serverInterface->getSlot(i)->getConnectedTime(),serverInterface->getSlot(i)->getSessionKey()); + + if (clientConnectedTime == 0 || + (serverInterface->getSlot(i, true)->getConnectedTime() > 0 + && serverInterface->getSlot(i, + true)->getConnectedTime() < + clientConnectedTime)) { + clientConnectedTime = + serverInterface->getSlot(i, true)->getConnectedTime(); + gameSettings->setMasterserver_admin(serverInterface->getSlot + (i, + true)->getSessionKey + ()); + gameSettings->setMasterserver_admin_faction_index + (serverInterface->getSlot(i, true)->getPlayerIndex()); + labelGameName.setText(createGameName + (serverInterface-> + getSlot(i, true)->getName())); + //printf("slot = %d, admin key [%d] slot connected time[" MG_SIZE_T_SPECIFIER "] clientConnectedTime [" MG_SIZE_T_SPECIFIER "]\n",i,gameSettings->getMasterserver_admin(),serverInterface->getSlot(i)->getConnectedTime(),clientConnectedTime); + } + if (serverInterface->getSlot(i, true)->getSessionKey() == + gameSettings->getMasterserver_admin()) { + masterserver_admin_found = true; + } + } + } + } + + if (masterserver_admin_found == false) { + labelGameName.setText(createGameName()); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::saveGameSettingsToFile(std::string fileName) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + GameSettings gameSettings; + loadGameSettings(&gameSettings); + CoreData::getInstance().saveGameSettingsToFile(fileName, + &gameSettings, + checkBoxAdvanced.getValue + ()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + void MenuStateCustomGame::KeepCurrentHumanPlayerSlots(GameSettings & + gameSettings) { + //look for human players + bool foundValidHumanControlTypeInFile = false; + for (int index2 = 0; index2 < GameConstants::maxPlayers; ++index2) { + ControlType ctFile = + static_cast + (gameSettings.getFactionControl(index2)); + if (ctFile == ctHuman) { + ControlType ctUI = + static_cast + (listBoxControls[index2].getSelectedItemIndex()); + if (ctUI != ctNetwork && ctUI != ctNetworkUnassigned) { + foundValidHumanControlTypeInFile = true; + //printf("Human found in file [%d]\n",index2); + } else if (labelPlayerNames[index2].getText() == "") { + foundValidHumanControlTypeInFile = true; + } + } + } + + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + ControlType ct = + static_cast + (listBoxControls[index].getSelectedItemIndex()); + if (ct == ctHuman) { + //printf("Human found in UI [%d] and file [%d]\n",index,foundControlType); + + if (foundValidHumanControlTypeInFile == false) { + gameSettings.setFactionControl(index, ctHuman); + gameSettings.setNetworkPlayerName(index, getHumanPlayerName()); + } + } + + ControlType ctFile = + static_cast + (gameSettings.getFactionControl(index)); + if (ctFile == ctHuman) { + gameSettings.setFactionControl(index, ctHuman); + } + } + } + + GameSettings MenuStateCustomGame::loadGameSettingsFromFile(std::string + fileName) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + GameSettings gameSettings; + + GameSettings originalGameSettings; + loadGameSettings(&originalGameSettings); + + try { + CoreData::getInstance().loadGameSettingsFromFile(fileName, + &gameSettings); + KeepCurrentHumanPlayerSlots(gameSettings); + + // correct game settings for headless: + if (this->headlessServerMode == true) { + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (gameSettings.getFactionControl(i) == ctHuman) { + gameSettings.setFactionControl(i, ctNetwork); + } + } + } + setupUIFromGameSettings(gameSettings); + } catch (const exception & ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] ERROR = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, ex.what()); + + showMessageBox(ex.what(), "Error", false); + + setupUIFromGameSettings(originalGameSettings); + gameSettings = originalGameSettings; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + return gameSettings; + } + + void MenuStateCustomGame::setupUIFromGameSettings(const GameSettings & + gameSettings) { + string humanPlayerName = getHumanPlayerName(); + + string scenarioDir = ""; + checkBoxScenario.setValue((gameSettings.getScenario() != "")); + if (checkBoxScenario.getValue() == true) { + listBoxScenario.setSelectedItem(formatString + (gameSettings.getScenario())); + + loadScenarioInfo(Scenario::getScenarioPath(dirList, + scenarioFiles + [listBoxScenario.getSelectedItemIndex + ()]), &scenarioInfo); + scenarioDir = + Scenario::getScenarioDir(dirList, gameSettings.getScenario()); + + //printf("scenarioInfo.fogOfWar = %d scenarioInfo.fogOfWar_exploredFlag = %d\n",scenarioInfo.fogOfWar,scenarioInfo.fogOfWar_exploredFlag); + if (scenarioInfo.fogOfWar == false + && scenarioInfo.fogOfWar_exploredFlag == false) { + listBoxFogOfWar.setSelectedItemIndex(2); + } else if (scenarioInfo.fogOfWar_exploredFlag == true) { + listBoxFogOfWar.setSelectedItemIndex(1); + } else { + listBoxFogOfWar.setSelectedItemIndex(0); + } + checkBoxAllowTeamUnitSharing. + setValue(scenarioInfo.allowTeamUnitSharing); + checkBoxAllowTeamResourceSharing. + setValue(scenarioInfo.allowTeamResourceSharing); + } + setupMapList(gameSettings.getScenario()); + setupTechList(gameSettings.getScenario(), false); + setupTilesetList(gameSettings.getScenario()); + + if (checkBoxScenario.getValue() == true) { + //string file = Scenario::getScenarioPath(dirList, gameSettings.getScenario()); + //loadScenarioInfo(file, &scenarioInfo); + + //printf("#6.1 about to load map [%s]\n",scenarioInfo.mapName.c_str()); + //loadMapInfo(Config::getMapPath(scenarioInfo.mapName, scenarioDir, true), &mapInfo, false); + //printf("#6.2\n"); + + listBoxMapFilter.setSelectedItemIndex(0); + listBoxMap.setItems(formattedPlayerSortedMaps[mapInfo.hardMaxPlayers]); + listBoxMap.setSelectedItem(formatString(scenarioInfo.mapName)); + } else { + //printf("gameSettings.getMapFilter()=%d \n",gameSettings.getMapFilter()); + if (gameSettings.getMapFilter() == 0) { + listBoxMapFilter.setSelectedItemIndex(0); + } else { + listBoxMapFilter.setSelectedItem(intToStr + (gameSettings.getMapFilter())); + } + listBoxMap.setItems(formattedPlayerSortedMaps + [gameSettings.getMapFilter()]); + } + + //printf("gameSettings.getMap() [%s] [%s]\n",gameSettings.getMap().c_str(),listBoxMap.getSelectedItem().c_str()); + + string mapFile = gameSettings.getMap(); + if (find(mapFiles.begin(), mapFiles.end(), mapFile) != + mapFiles.end()) { + mapFile = formatString(mapFile); + listBoxMap.setSelectedItem(mapFile); + + loadMapInfo(Config::getMapPath + (getCurrentMapFile(), scenarioDir, true), &mapInfo, + true); + labelMapInfo.setText(mapInfo.desc); + } + + string tilesetFile = gameSettings.getTileset(); + if (find(tilesetFiles.begin(), tilesetFiles.end(), tilesetFile) != + tilesetFiles.end()) { + tilesetFile = formatString(tilesetFile); + listBoxTileset.setSelectedItem(tilesetFile); + } + + string techtreeFile = gameSettings.getTech(); + if (find(techTreeFiles.begin(), techTreeFiles.end(), techtreeFile) != + techTreeFiles.end()) { + techtreeFile = formatString(techtreeFile); + listBoxTechTree.setSelectedItem(techtreeFile); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //gameSettings->setDefaultUnits(true); + //gameSettings->setDefaultResources(true); + //gameSettings->setDefaultVictoryConditions(true); + + //FogOfWar + if (checkBoxScenario.getValue() == false) { + listBoxFogOfWar.setSelectedItemIndex(0); // default is 0! + if (gameSettings.getFogOfWar() == false) { + listBoxFogOfWar.setSelectedItemIndex(2); + } + + if ((gameSettings.getFlagTypes1() & ft1_show_map_resources) == + ft1_show_map_resources) { + if (gameSettings.getFogOfWar() == true) { + listBoxFogOfWar.setSelectedItemIndex(1); + } + } + } + + //printf("In [%s::%s line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + checkBoxAllowObservers.setValue(gameSettings.getAllowObservers() == + true ? true : false); + //listBoxEnableObserverMode.setSelectedItem(gameSettings.getEnableObserverModeAtEndGame() == true ? lang.getString("Yes") : lang.getString("No")); + + checkBoxEnableSwitchTeamMode.setValue((gameSettings.getFlagTypes1() & + ft1_allow_team_switching) == + ft1_allow_team_switching ? true : + false); + listBoxAISwitchTeamAcceptPercent.setSelectedItem(intToStr + (gameSettings.getAiAcceptSwitchTeamPercentChance + ())); + listBoxFallbackCpuMultiplier. + setSelectedItemIndex(gameSettings.getFallbackCpuMultiplier()); + + checkBoxAllowInGameJoinPlayer.setValue((gameSettings.getFlagTypes1() & + ft1_allow_in_game_joining) == + ft1_allow_in_game_joining ? true + : false); + + checkBoxAllowTeamUnitSharing.setValue((gameSettings.getFlagTypes1() & + ft1_allow_shared_team_units) == + ft1_allow_shared_team_units ? + true : false); + checkBoxAllowTeamResourceSharing.setValue((gameSettings.getFlagTypes1 + () & + ft1_allow_shared_team_resources) + == + ft1_allow_shared_team_resources + ? true : false); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (serverInterface != NULL) { + serverInterface->setAllowInGameConnections + (checkBoxAllowInGameJoinPlayer.getValue() == true); + } + + checkBoxAllowNativeLanguageTechtree. + setValue(gameSettings.getNetworkAllowNativeLanguageTechtree()); + + //listBoxPathFinderType.setSelectedItemIndex(gameSettings.getPathFinderType()); + + //listBoxEnableServerControlledAI.setSelectedItem(gameSettings.getEnableServerControlledAI() == true ? lang.getString("Yes") : lang.getString("No")); + + //labelNetworkFramePeriod.setText(lang.getString("NetworkFramePeriod")); + + //listBoxNetworkFramePeriod.setSelectedItem(intToStr(gameSettings.getNetworkFramePeriod()/10*10)); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + checkBoxNetworkPauseGameForLaggedClients. + setValue(gameSettings.getNetworkPauseGameForLaggedClients()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + reloadFactions(false, + (checkBoxScenario.getValue() == + true ? + scenarioFiles[listBoxScenario.getSelectedItemIndex()] + : "")); + //reloadFactions(true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d] gameSettings.getFactionCount() = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + gameSettings.getFactionCount()); + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + int slotIndex = gameSettings.getStartLocationIndex(i); + if (gameSettings.getFactionControl(i) < + listBoxControls[slotIndex].getItemCount()) { + listBoxControls[slotIndex]. + setSelectedItemIndex(gameSettings.getFactionControl(i)); + } + + //if(slotIndex == 0) printf("#2 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); + + updateResourceMultiplier(slotIndex); + + //if(slotIndex == 0) printf("#3 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); + + listBoxRMultiplier[slotIndex]. + setSelectedItemIndex(gameSettings.getResourceMultiplierIndex(i)); + + //if(slotIndex == 0) printf("#4 slotIndex = %d, i = %d, multiplier = %d\n",slotIndex,i,listBoxRMultiplier[i].getSelectedItemIndex()); + + listBoxTeams[slotIndex]. + setSelectedItemIndex(gameSettings.getTeam(i)); + + lastSelectedTeamIndex[slotIndex] = + listBoxTeams[slotIndex].getSelectedItemIndex(); + + string factionName = gameSettings.getFactionTypeName(i); + factionName = formatString(factionName); + + //printf("\n\n\n*** setupUIFromGameSettings A, i = %d, startLoc = %d, factioncontrol = %d, factionName [%s]\n",i,gameSettings.getStartLocationIndex(i),gameSettings.getFactionControl(i),factionName.c_str()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] factionName = [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, factionName.c_str()); + + if (listBoxFactions[slotIndex].hasItem(factionName) == true) { + listBoxFactions[slotIndex].setSelectedItem(factionName); + } else { + listBoxFactions[slotIndex].setSelectedItem(formatString + (GameConstants:: + RANDOMFACTION_SLOTNAME)); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] i = %d, gameSettings.getNetworkPlayerName(i) [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + gameSettings. + getNetworkPlayerName(i).c_str()); + + //labelPlayerNames[slotIndex].setText(gameSettings.getNetworkPlayerName(i)); + } + + //SetActivePlayerNameEditor(); + + updateControllers(); + updateNetworkSlots(); + + if (this->headlessServerMode == false && humanPlayerName != "") { + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + ControlType ct = + static_cast + (listBoxControls[index].getSelectedItemIndex()); + if (ct == ctHuman) { + if (humanPlayerName != labelPlayerNames[index].getText()) { + //printf("Player name changing from [%s] to [%s]\n",labelPlayerNames[index].getText().c_str(),humanPlayerName.c_str()); + + labelPlayerNames[index].setText(""); + labelPlayerNames[index].setText(humanPlayerName); + } + } + } + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + // ============ PRIVATE =========================== + + void MenuStateCustomGame::lastPlayerDisconnected() { + // this is for headless mode only! + // if last player disconnects we load the network defaults. + if (this->headlessServerMode == false) { + return; + } + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + bool foundConnectedPlayer = false; + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (serverInterface->getSlot(i, true) != NULL && + (listBoxControls[i].getSelectedItemIndex() == ctNetwork || + listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned)) { + if (serverInterface->getSlot(i, true)->isConnected() == true) { + foundConnectedPlayer = true; + } + } + } + + if (!foundConnectedPlayer && headlessHasConnectedPlayer == true) { + // load defaults + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if (fileExists(data_path + DEFAULT_NETWORKGAME_FILENAME) == true) + loadGameSettings(data_path + DEFAULT_NETWORKGAME_FILENAME); + } + headlessHasConnectedPlayer = foundConnectedPlayer; + } + + bool MenuStateCustomGame::hasNetworkGameSettings() { + bool hasNetworkSlot = false; + + try { + for (int i = 0; i < mapInfo.players; ++i) { + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct != ctClosed) { + if (ct == ctNetwork || ct == ctNetworkUnassigned) { + hasNetworkSlot = true; + break; + } + } + } + if (hasNetworkSlot == false) { + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct != ctClosed) { + if (ct == ctNetworkUnassigned) { + hasNetworkSlot = true; + break; + } + } + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + ex.what()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showGeneralError = true; + generalErrorToShow = szBuf; + } + + return hasNetworkSlot; + } + + void MenuStateCustomGame::loadMapInfo(string file, MapInfo * mapInfo, + bool loadMapPreview) { + try { + Lang & lang = Lang::getInstance(); + if (MapPreview::loadMapInfo + (file, mapInfo, lang.getString("MaxPlayers"), + lang.getString("Size"), true) == true) { + // Though we prefer not to change the actual value of mapInfo->players, + // which is the number of players assigned when making the map, we still know + // the actual value from mapInfo.hardMaxPlayers. Changing it here means + // not having to change a variable name in many places of this file + // to implement enhanced observer mode (issue #13)' + if (checkBoxAllowObservers.getValue() == 1) { + mapInfo->players = GameConstants::maxPlayers; + } + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (serverInterface->getSlot(i, true) != NULL && + (listBoxControls[i].getSelectedItemIndex() == ctNetwork + || listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned)) { + if (serverInterface->getSlot(i, true)->isConnected() == true) { + if (i + 1 > mapInfo->players && + listBoxControls[i].getSelectedItemIndex() != + ctNetworkUnassigned) { + listBoxControls[i].setSelectedItemIndex + (ctNetworkUnassigned); + } + } + } + } + + // Not painting properly so this is on hold + if (loadMapPreview == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + mapPreview.loadFromFile(file.c_str()); + + //printf("Loading map preview MAP\n"); + cleanupMapPreviewTexture(); + } + } + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s] loading map [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + e.what(), file.c_str()); + throw megaglest_runtime_error("Error loading map file: [" + file + + "] msg: " + e.what()); + } + } + + void MenuStateCustomGame::closeUnusedSlots() { + try { + if (checkBoxScenario.getValue() == false) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + //for(int i= 0; igetSlot(i, true) == NULL || + serverInterface->getSlot(i, + true)->isConnected() == + false + || serverInterface->getSlot(i, + true)->getConnectHasHandshaked + () == false) { + //printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); + + listBoxControls[i].setSelectedItemIndex(ctClosed); + } + } + } + updateNetworkSlots(); + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + bool MenuStateCustomGame::textInput(std::string text) { + //printf("In [%s::%s Line: %d] text [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,text.c_str()); + if (activeInputLabel != NULL) { + bool handled = textInputEditLabel(text, &activeInputLabel); + if (handled == true && &labelGameName != activeInputLabel) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + } else { + if (hasNetworkGameSettings() == true) { + chatManager.textInput(text); + } + } + return false; + } + + void MenuStateCustomGame::keyDown(SDL_KeyboardEvent key) { + if (isMasterserverMode() == true) { + return; + } + + if (activeInputLabel != NULL) { + bool handled = keyDownEditLabel(key, &activeInputLabel); + if (handled == true) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + } else { + //send key to the chat manager + if (hasNetworkGameSettings() == true) { + chatManager.keyDown(key); + } + if (chatManager.getEditEnabled() == false && + (::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == + false)) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + //if(key == configKeys.getCharKey("ShowFullConsole")) { + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) + == true) { + showFullConsole = true; + } + //Toggle music + //else if(key == configKeys.getCharKey("ToggleMusic")) { + else + if (isKeyPressed(configKeys.getSDLKey("ToggleMusic"), key) + == true) { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + float currentVolume = + CoreData::getInstance().getMenuMusic()->getVolume(); + if (currentVolume > 0) { + CoreData::getInstance().getMenuMusic()->setVolume(0.f); + console.addLine(lang.getString("GameMusic") + " " + + lang.getString("Off")); + } else { + CoreData::getInstance(). + getMenuMusic()->setVolume(configVolume); + //If the config says zero, use the default music volume + //gameMusic->setVolume(configVolume ? configVolume : 0.9); + console.addLine(lang.getString("GameMusic")); + } + } + //else if(key == configKeys.getCharKey("SaveGUILayout")) { + else + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) + == true) { + bool saved = + GraphicComponent::saveAllCustomProperties(containerName); + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("GUILayoutSaved") + " [" + + (saved ? lang. + getString("Yes") : lang.getString("No")) + + "]"); + } + } + } + } + + void MenuStateCustomGame::keyPress(SDL_KeyboardEvent c) { + if (isMasterserverMode() == true) { + return; + } + + if (activeInputLabel != NULL) { + bool handled = keyPressEditLabel(c, &activeInputLabel); + if (handled == true && &labelGameName != activeInputLabel) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + } + } else { + if (hasNetworkGameSettings() == true) { + chatManager.keyPress(c); + } + } + } + + void MenuStateCustomGame::keyUp(SDL_KeyboardEvent key) { + if (isMasterserverMode() == true) { + return; + } + + if (activeInputLabel == NULL) { + if (hasNetworkGameSettings() == true) { + chatManager.keyUp(key); + } + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + if (chatManager.getEditEnabled()) { + //send key to the chat manager + if (hasNetworkGameSettings() == true) { + chatManager.keyUp(key); + } + } + //else if(key == configKeys.getCharKey("ShowFullConsole")) { + else + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) + == true) { + showFullConsole = false; + } + } + } + + void MenuStateCustomGame::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void MenuStateCustomGame::switchToNextMapGroup(const int direction) { + int i = listBoxMapFilter.getSelectedItemIndex(); + // if there are no maps for the current selection we switch to next selection + while (formattedPlayerSortedMaps[i].empty()) { + i = i + direction; + if (i > GameConstants::maxPlayers) { + i = 0; + } + if (i < 0) { + i = GameConstants::maxPlayers; + } + } + listBoxMapFilter.setSelectedItemIndex(i); + listBoxMap.setItems(formattedPlayerSortedMaps[i]); + } + + string MenuStateCustomGame::getCurrentMapFile() { + int i = listBoxMapFilter.getSelectedItemIndex(); + int mapIndex = listBoxMap.getSelectedItemIndex(); + if (playerSortedMaps[i].empty() == false) { + return playerSortedMaps[i].at(mapIndex); + } + return ""; + } + + void MenuStateCustomGame::setActiveInputLabel(GraphicLabel * newLable) { + MenuState::setActiveInputLabel(newLable, &activeInputLabel); + } + + string MenuStateCustomGame::getHumanPlayerName(int index) { + string result = defaultPlayerName; + if (index < 0) { + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + if (listBoxControls[j].getSelectedItemIndex() >= 0) { + ControlType ct = + static_cast + (listBoxControls[j].getSelectedItemIndex()); + if (ct == ctHuman) { + index = j; + break; + } + } + } + } + + if (index >= 0 && index < GameConstants::maxPlayers && + labelPlayerNames[index].getText() != "" && + labelPlayerNames[index].getText() != + GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + result = labelPlayerNames[index].getText(); + + if (activeInputLabel != NULL) { + size_t found = result.find_last_of("_"); + if (found != string::npos) { + result = result.substr(0, found); + } + } + } + + return result; + } + + void MenuStateCustomGame::loadFactionTexture(string filepath) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (enableFactionTexturePreview == true) { + if (filepath == "") { + factionTexture = NULL; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] filepath = [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + filepath.c_str()); + + factionTexture = Renderer::findTexture(filepath); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + } + } + + void MenuStateCustomGame::cleanupMapPreviewTexture() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //printf("CLEANUP map preview texture\n"); + + if (mapPreviewTexture != NULL) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + mapPreviewTexture->end(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + delete mapPreviewTexture; + mapPreviewTexture = NULL; + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + int32 MenuStateCustomGame::getNetworkPlayerStatus() { + int32 result = npst_None; + switch (listBoxPlayerStatus.getSelectedItemIndex()) { + case 2: + result = npst_Ready; + break; + case 1: + result = npst_BeRightBack; + break; + case 0: + default: + result = npst_PickSettings; + break; + } + + return result; + } + + void MenuStateCustomGame::loadScenarioInfo(string file, + ScenarioInfo * scenarioInfo) { + //printf("Load scenario file [%s]\n",file.c_str()); + bool isTutorial = Scenario::isGameTutorial(file); + Scenario::loadScenarioInfo(file, scenarioInfo, isTutorial); + + //cleanupPreviewTexture(); + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + } + + bool MenuStateCustomGame::isInSpecialKeyCaptureEvent() { + bool result = (chatManager.getEditEnabled() + || activeInputLabel != NULL); + return result; + } + + void MenuStateCustomGame::processScenario() { + try { + if (checkBoxScenario.getValue() == true) { + //printf("listBoxScenario.getSelectedItemIndex() = %d [%s] scenarioFiles.size() = %d\n",listBoxScenario.getSelectedItemIndex(),listBoxScenario.getSelectedItem().c_str(),scenarioFiles.size()); + loadScenarioInfo(Scenario::getScenarioPath(dirList, + scenarioFiles + [listBoxScenario.getSelectedItemIndex + ()]), &scenarioInfo); + string scenarioDir = + Scenario::getScenarioDir(dirList, scenarioInfo.name); + + //printf("scenarioInfo.fogOfWar = %d scenarioInfo.fogOfWar_exploredFlag = %d\n",scenarioInfo.fogOfWar,scenarioInfo.fogOfWar_exploredFlag); + if (scenarioInfo.fogOfWar == false + && scenarioInfo.fogOfWar_exploredFlag == false) { + listBoxFogOfWar.setSelectedItemIndex(2); + } else if (scenarioInfo.fogOfWar_exploredFlag == true) { + listBoxFogOfWar.setSelectedItemIndex(1); + } else { + listBoxFogOfWar.setSelectedItemIndex(0); + } + + checkBoxAllowTeamUnitSharing. + setValue(scenarioInfo.allowTeamUnitSharing); + checkBoxAllowTeamResourceSharing. + setValue(scenarioInfo.allowTeamResourceSharing); + + setupTechList(scenarioInfo.name, false); + listBoxTechTree.setSelectedItem(formatString + (scenarioInfo.techTreeName)); + reloadFactions(false, scenarioInfo.name); + + setupTilesetList(scenarioInfo.name); + listBoxTileset.setSelectedItem(formatString + (scenarioInfo.tilesetName)); + + checkBoxAllowObservers.setValue(false); + + setupMapList(scenarioInfo.name); + listBoxMap.setSelectedItem(formatString(scenarioInfo.mapName)); + loadMapInfo(Config::getMapPath + (getCurrentMapFile(), scenarioDir, true), &mapInfo, + true); + labelMapInfo.setText(mapInfo.desc); + + //printf("scenarioInfo.name [%s] [%s]\n",scenarioInfo.name.c_str(),listBoxMap.getSelectedItem().c_str()); + + // Loop twice to set the human slot or else it closes network slots in some cases + for (int humanIndex = 0; humanIndex < 2; ++humanIndex) { + for (int i = 0; i < mapInfo.players; ++i) { + listBoxRMultiplier[i].setSelectedItem(floatToStr + (scenarioInfo. + resourceMultipliers[i], + 1)); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = serverInterface->getSlot(i, true); + + int selectedControlItemIndex = + listBoxControls[i].getSelectedItemIndex(); + if (selectedControlItemIndex != ctNetwork + || (selectedControlItemIndex == ctNetwork + && (slot == NULL || slot->isConnected() == false))) { + } + + listBoxControls[i]. + setSelectedItemIndex(scenarioInfo.factionControls[i]); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + // Skip over networkunassigned + //if(listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned && + // selectedControlItemIndex != ctNetworkUnassigned) { + // listBoxControls[i].mouseClick(x, y); + //} + + //look for human players + int humanIndex1 = -1; + int humanIndex2 = -1; + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + ControlType ct = + static_cast + (listBoxControls[j].getSelectedItemIndex()); + if (ct == ctHuman) { + if (humanIndex1 == -1) { + humanIndex1 = j; + } else { + humanIndex2 = j; + } + } + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] humanIndex1 = %d, humanIndex2 = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, humanIndex1, humanIndex2); + + //no human + if (humanIndex1 == -1 && humanIndex2 == -1) { + setSlotHuman(i); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] i = %d, labelPlayerNames[i].getText() [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, i, + labelPlayerNames[i].getText(). + c_str()); + + //printf("humanIndex1 = %d humanIndex2 = %d i = %d listBoxControls[i].getSelectedItemIndex() = %d\n",humanIndex1,humanIndex2,i,listBoxControls[i].getSelectedItemIndex()); + } + //2 humans + else if (humanIndex1 != -1 && humanIndex2 != -1) { + int closeSlotIndex = + (humanIndex1 == i ? humanIndex2 : humanIndex1); + int humanSlotIndex = + (closeSlotIndex == humanIndex1 ? humanIndex2 : humanIndex1); + + string origPlayName = + labelPlayerNames[closeSlotIndex].getText(); + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] closeSlotIndex = %d, origPlayName [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, closeSlotIndex, + origPlayName.c_str()); + //printf("humanIndex1 = %d humanIndex2 = %d i = %d closeSlotIndex = %d humanSlotIndex = %d\n",humanIndex1,humanIndex2,i,closeSlotIndex,humanSlotIndex); + + listBoxControls[closeSlotIndex].setSelectedItemIndex + (ctClosed); + labelPlayerNames[humanSlotIndex].setText((origPlayName != + "" ? origPlayName : + getHumanPlayerName + ())); + } + + ControlType ct = + static_cast + (listBoxControls[i].getSelectedItemIndex()); + if (ct != ctClosed) { + //updateNetworkSlots(); + //updateResourceMultiplier(i); + updateResourceMultiplier(i); + + //printf("Setting scenario faction i = %d [ %s]\n",i,scenarioInfo.factionTypeNames[i].c_str()); + listBoxFactions[i].setSelectedItem(formatString + (scenarioInfo.factionTypeNames + [i])); + //printf("DONE Setting scenario faction i = %d [ %s]\n",i,scenarioInfo.factionTypeNames[i].c_str()); + + // Disallow CPU players to be observers + if (factionFiles + [listBoxFactions[i].getSelectedItemIndex()] == + formatString(GameConstants::OBSERVER_SLOTNAME) + && (listBoxControls[i].getSelectedItemIndex() == + ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta)) { + listBoxFactions[i].setSelectedItemIndex(0); + } + // + + listBoxTeams[i].setSelectedItem(intToStr + (scenarioInfo.teams[i])); + if (factionFiles[listBoxFactions[i].getSelectedItemIndex()] + != formatString(GameConstants::OBSERVER_SLOTNAME)) { + if (listBoxTeams[i].getSelectedItemIndex() + 1 != + (GameConstants::maxPlayers + fpt_Observer)) { + lastSelectedTeamIndex[i] = + listBoxTeams[i].getSelectedItemIndex(); + } + // Alow Neutral cpu players + else if (listBoxControls[i].getSelectedItemIndex() == + ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == + ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta) { + lastSelectedTeamIndex[i] = + listBoxTeams[i].getSelectedItemIndex(); + } + } else { + lastSelectedTeamIndex[i] = -1; + } + } + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL);; + } + } + } + + updateControllers(); + updateNetworkSlots(); + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + } + if (hasNetworkGameSettings() == true) { + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + } + + //labelInfo.setText(scenarioInfo.desc); + } else { + setupMapList(""); + listBoxMap.setSelectedItem(formatString + (formattedPlayerSortedMaps[0][0])); + loadMapInfo(Config::getMapPath(getCurrentMapFile(), "", true), + &mapInfo, true); + + labelMapInfo.setText(mapInfo.desc); + + setupTechList("", false); + reloadFactions(false, ""); + setupTilesetList(""); + updateControllers(); + } + SetupUIForScenarios(); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + mainMessageBoxState = 1; + showMessageBox(szBuf, "Error detected", false); + } + } + + void MenuStateCustomGame::SetupUIForScenarios() { + try { + if (checkBoxScenario.getValue() == true) { + // START - Disable changes to controls while in Scenario mode + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxControls[i].setEditable(false); + listBoxFactions[i].setEditable(false); + listBoxRMultiplier[i].setEditable(false); + listBoxTeams[i].setEditable(false); + } + listBoxFogOfWar.setEditable(false); + checkBoxAllowObservers.setEditable(false); + checkBoxAllowTeamUnitSharing.setEditable(false); + checkBoxAllowTeamResourceSharing.setEditable(false); + //listBoxPathFinderType.setEditable(false); + checkBoxEnableSwitchTeamMode.setEditable(false); + listBoxAISwitchTeamAcceptPercent.setEditable(false); + listBoxFallbackCpuMultiplier.setEditable(false); + listBoxMap.setEditable(false); + listBoxTileset.setEditable(false); + listBoxMapFilter.setEditable(false); + listBoxTechTree.setEditable(false); + // END - Disable changes to controls while in Scenario mode + } else { + // START - Disable changes to controls while in Scenario mode + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + listBoxControls[i].setEditable(true); + listBoxFactions[i].setEditable(true); + listBoxRMultiplier[i].setEditable(true); + listBoxTeams[i].setEditable(true); + } + listBoxFogOfWar.setEditable(true); + checkBoxAllowObservers.setEditable(true); + checkBoxAllowTeamUnitSharing.setEditable(true); + checkBoxAllowTeamResourceSharing.setEditable(true); + //listBoxPathFinderType.setEditable(true); + checkBoxEnableSwitchTeamMode.setEditable(true); + listBoxAISwitchTeamAcceptPercent.setEditable(true); + listBoxFallbackCpuMultiplier.setEditable(true); + listBoxMap.setEditable(true); + listBoxTileset.setEditable(true); + listBoxMapFilter.setEditable(true); + listBoxTechTree.setEditable(true); + // END - Disable changes to controls while in Scenario mode + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + + } + + int MenuStateCustomGame::setupMapList(string scenario) { + int initialMapSelection = 0; + + try { + Config & config = Config::getInstance(); + vector < string > invalidMapList; + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > pathList = + config.getPathListForType(ptMaps, scenarioDir); + vector < string > allMaps = + MapPreview::findAllValidMaps(pathList, scenarioDir, false, true, + &invalidMapList); + if (scenario != "") { + vector < string > allMaps2 = + MapPreview:: + findAllValidMaps(config.getPathListForType(ptMaps, ""), "", + false, true, &invalidMapList); + copy(allMaps2.begin(), allMaps2.end(), + std::inserter(allMaps, allMaps.begin())); + } + // sort map list non case sensitive + std::sort(allMaps.begin(), allMaps.end(), compareNonCaseSensitive); + + if (allMaps.empty()) { + throw megaglest_runtime_error("No maps were found!"); + } + vector < string > results; + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; + + for (unsigned int i = 0; i < GameConstants::maxPlayers + 1; ++i) { + playerSortedMaps[i].clear(); + formattedPlayerSortedMaps[i].clear(); + } + + // at index=0 fill in the whole list + copy(mapFiles.begin(), mapFiles.end(), + std::back_inserter(playerSortedMaps[0])); + copy(playerSortedMaps[0].begin(), playerSortedMaps[0].end(), + std::back_inserter(formattedPlayerSortedMaps[0])); + std::for_each(formattedPlayerSortedMaps[0].begin(), + formattedPlayerSortedMaps[0].end(), FormatString()); + + // fill playerSortedMaps and formattedPlayerSortedMaps according to map player count + for (int i = 0; i < (int) mapFiles.size(); i++) { // fetch info and put map in right list + loadMapInfo(Config::getMapPath + (mapFiles.at(i), scenarioDir, false), &mapInfo, + false); + + if (GameConstants::maxPlayers + 1 <= mapInfo.players) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Sorted map list [%d] does not match\ncurrent map playercount [%d]\nfor file [%s]\nmap [%s]", + GameConstants::maxPlayers + 1, mapInfo.players, + Config::getMapPath(mapFiles.at(i), "", + false).c_str(), + mapInfo.desc.c_str()); + throw megaglest_runtime_error(szBuf); + } + playerSortedMaps[mapInfo.hardMaxPlayers].push_back(mapFiles.at(i)); + formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].push_back(formatString + (mapFiles.at + (i))); + if (config.getString("InitialMap", "Conflict") == + formattedPlayerSortedMaps[mapInfo.hardMaxPlayers].back()) { + initialMapSelection = i; + } + } + + //printf("#6 scenario [%s] [%s]\n",scenario.c_str(),scenarioDir.c_str()); + if (scenario != "") { + string file = Scenario::getScenarioPath(dirList, scenario); + loadScenarioInfo(file, &scenarioInfo); + + //printf("#6.1 about to load map [%s]\n",scenarioInfo.mapName.c_str()); + loadMapInfo(Config::getMapPath + (scenarioInfo.mapName, scenarioDir, true), &mapInfo, + false); + + listBoxMapFilter.setSelectedItem(intToStr(mapInfo.players)); + listBoxMap.setItems(formattedPlayerSortedMaps[mapInfo.players]); + } else { + listBoxMapFilter.setSelectedItemIndex(0); + listBoxMap.setItems(formattedPlayerSortedMaps[0]); + } + //printf("#7\n"); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + //abort(); + } + + return initialMapSelection; + } + + int MenuStateCustomGame::setupTechList(string scenario, bool forceLoad) { + int initialTechSelection = 0; + try { + Config & config = Config::getInstance(); + + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > results; + vector < string > techPaths = + config.getPathListForType(ptTechs, scenarioDir); + findDirs(techPaths, results); + + if (results.empty()) { + //throw megaglest_runtime_error("No tech-trees were found!"); + printf("No tech-trees were found (custom)!\n"); + } + + techTreeFiles = results; + + vector < string > translatedTechs; + + for (unsigned int i = 0; i < results.size(); i++) { + //printf("TECHS i = %d results [%s] scenario [%s]\n",i,results[i].c_str(),scenario.c_str()); + + results.at(i) = formatString(results.at(i)); + if (config.getString("InitialTechTree", "Megapack") == + results.at(i)) { + initialTechSelection = i; + } + string txTech = + techTree->getTranslatedName(techTreeFiles.at(i), forceLoad); + translatedTechs.push_back(formatString(txTech)); + } + + + listBoxTechTree.setItems(results, translatedTechs); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + + return initialTechSelection; + } + + void MenuStateCustomGame::reloadFactions(bool keepExistingSelectedItem, + string scenario) { + try { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + vector < string > results; + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + vector < string > techPaths = + config.getPathListForType(ptTechs, scenarioDir); + + //printf("#1 techPaths.size() = %d scenarioDir [%s] [%s]\n",techPaths.size(),scenario.c_str(),scenarioDir.c_str()); + + if (listBoxTechTree.getItemCount() > 0) { + for (int idx = 0; idx < (int) techPaths.size(); idx++) { + string & techPath = techPaths[idx]; + endPathWithSlash(techPath); + string factionPath = + techPath + + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + + "/factions/"; + findDirs(factionPath, results, false, false); + + //printf("idx = %d factionPath [%s] results.size() = %d\n",idx,factionPath.c_str(),results.size()); + + if (results.empty() == false) { + break; + } + } + } + + if (results.empty() == true) { + //throw megaglest_runtime_error("(2)There are no factions for the tech tree [" + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"); + showGeneralError = true; + if (listBoxTechTree.getItemCount() > 0) { + generalErrorToShow = + "[#2] There are no factions for the tech tree [" + + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + "]"; + } else { + generalErrorToShow = + "[#2] There are no factions since there is no tech tree!"; + } + } + + // results.push_back(formatString(GameConstants::RANDOMFACTION_SLOTNAME)); + // + // // Add special Observer Faction + // if(checkBoxAllowObservers.getValue() == 1) { + // results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); + // } + + vector < string > translatedFactionNames; + factionFiles = results; + for (int i = 0; i < (int) results.size(); ++i) { + results[i] = formatString(results[i]); + + string translatedString = ""; + if (listBoxTechTree.getItemCount() > 0) { + translatedString = + techTree->getTranslatedFactionName(techTreeFiles + [listBoxTechTree.getSelectedItemIndex + ()], factionFiles[i]); + } + //printf("translatedString=%s formatString(results[i])=%s \n",translatedString.c_str(),formatString(results[i]).c_str() ); + if (toLower(translatedString) == toLower(results[i])) { + translatedFactionNames.push_back(results[i]); + } else { + translatedFactionNames.push_back(results[i] + " (" + + translatedString + ")"); + } + //printf("FACTIONS i = %d results [%s]\n",i,results[i].c_str()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "Tech [%s] has faction [%s]\n", + techTreeFiles + [listBoxTechTree.getSelectedItemIndex + ()].c_str(), results[i].c_str()); + } + results.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::RANDOMFACTION_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Random", "", + true) + "*"); + + // Add special Observer Faction + if (checkBoxAllowObservers.getValue() == 1) { + results.push_back(formatString(GameConstants::OBSERVER_SLOTNAME)); + factionFiles.push_back(formatString + (GameConstants::OBSERVER_SLOTNAME)); + translatedFactionNames.push_back("*" + + lang.getString("Observer", "", + true) + "*"); + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + int originalIndex = listBoxFactions[i].getSelectedItemIndex(); + string originalValue = + (listBoxFactions[i].getItemCount() > + 0 ? listBoxFactions[i].getSelectedItem() : ""); + + listBoxFactions[i].setItems(results, translatedFactionNames); + if (keepExistingSelectedItem == false || + (checkBoxAllowObservers.getValue() == 0 && + originalValue == + formatString(GameConstants::OBSERVER_SLOTNAME))) { + listBoxFactions[i].setSelectedItemIndex(i % results.size()); + + if (originalValue == + formatString(GameConstants::OBSERVER_SLOTNAME) + && listBoxFactions[i].getSelectedItem() != + formatString(GameConstants::OBSERVER_SLOTNAME)) { + if (listBoxTeams[i].getSelectedItem() == + intToStr(GameConstants::maxPlayers + fpt_Observer)) { + listBoxTeams[i].setSelectedItem(intToStr(1)); + } + } + } else if (originalIndex < (int) results.size()) { + listBoxFactions[i].setSelectedItemIndex(originalIndex); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + } + + void MenuStateCustomGame::setSlotHuman(int i) { + if (labelPlayerNames[i].getEditable()) { + return; + } + listBoxControls[i].setSelectedItemIndex(ctHuman); + listBoxRMultiplier[i].setSelectedItem("1.0"); + + labelPlayerNames[i].setText(getHumanPlayerName()); + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + labelPlayerNames[j].setEditable(false); + } + labelPlayerNames[i].setEditable(true); + } + + void MenuStateCustomGame::setupTilesetList(string scenario) { + try { + Config & config = Config::getInstance(); + + string scenarioDir = Scenario::getScenarioDir(dirList, scenario); + + vector < string > results; + findDirs(config.getPathListForType(ptTilesets, scenarioDir), + results); + if (results.empty()) { + throw megaglest_runtime_error("No tile-sets were found!"); + } + tilesetFiles = results; + std::for_each(results.begin(), results.end(), FormatString()); + + listBoxTileset.setItems(results); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + throw megaglest_runtime_error(szBuf); + } + + } + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_custom_game.h b/source/glest_game/menu/menu_state_custom_game.h index b43e69af5..3fb829f96 100644 --- a/source/glest_game/menu/menu_state_custom_game.h +++ b/source/glest_game/menu/menu_state_custom_game.h @@ -32,385 +32,380 @@ # include "leak_dumper.h" using namespace - Shared::Map; +Shared::Map; -namespace Shared -{ - namespace Graphics - { - class VideoPlayer; - } +namespace Shared { + namespace Graphics { + class VideoPlayer; + } } -namespace Glest -{ - namespace Game - { - - class SwitchSetupRequest; - class ServerInterface; - class TechTree; - - enum ParentMenuState - { - pNewGame, - pMasterServer, - pLanGame - }; - -// =============================== -// class MenuStateCustomGame -// =============================== - - class MenuStateCustomGame: - public MenuState, public SimpleTaskCallbackInterface - { - private: - GraphicButton buttonReturn; - GraphicButton buttonPlayNow; - GraphicButton buttonRestoreLastSettings; - GraphicLabel labelControl; - GraphicLabel labelRMultiplier; - GraphicLabel labelFaction; - GraphicLabel labelTeam; - GraphicLabel labelMap; - GraphicLabel labelFogOfWar; - GraphicLabel labelTechTree; - GraphicLabel labelTileset; - GraphicLabel labelMapInfo; - GraphicLabel labelLocalGameVersion; - GraphicLabel labelLocalIP; - GraphicLabel labelGameName; - - GraphicListBox listBoxMap; - GraphicListBox listBoxFogOfWar; - GraphicListBox listBoxTechTree; - GraphicListBox listBoxTileset; - - vector < string > mapFiles; - vector < string > playerSortedMaps[GameConstants::maxPlayers + 1]; - vector < - string > formattedPlayerSortedMaps[GameConstants::maxPlayers + 1]; - vector < string > techTreeFiles; - vector < string > tilesetFiles; - vector < string > factionFiles; - GraphicLabel labelPlayers[GameConstants::maxPlayers]; - GraphicLabel labelPlayerNames[GameConstants::maxPlayers]; - GraphicListBox listBoxControls[GameConstants::maxPlayers]; - GraphicButton buttonBlockPlayers[GameConstants::maxPlayers]; - GraphicListBox listBoxRMultiplier[GameConstants::maxPlayers]; - GraphicListBox listBoxFactions[GameConstants::maxPlayers]; - GraphicListBox listBoxTeams[GameConstants::maxPlayers]; - GraphicLabel labelNetStatus[GameConstants::maxPlayers]; - MapInfo mapInfo; - - GraphicButton buttonClearBlockedPlayers; - - GraphicLabel labelPublishServer; - GraphicCheckBox checkBoxPublishServer; - - GraphicMessageBox mainMessageBox; - int - mainMessageBoxState; - - GraphicLabel labelNetworkPauseGameForLaggedClients; - GraphicCheckBox checkBoxNetworkPauseGameForLaggedClients; - - GraphicLabel labelMapFilter; - GraphicListBox listBoxMapFilter; - - GraphicLabel labelAdvanced; - GraphicCheckBox checkBoxAdvanced; - - GraphicLabel labelAllowObservers; - GraphicCheckBox checkBoxAllowObservers; - - GraphicLabel * - activeInputLabel; - - GraphicLabel labelPlayerStatus[GameConstants::maxPlayers]; - GraphicListBox listBoxPlayerStatus; - - GraphicLabel labelEnableSwitchTeamMode; - GraphicCheckBox checkBoxEnableSwitchTeamMode; - - GraphicLabel labelAISwitchTeamAcceptPercent; - GraphicListBox listBoxAISwitchTeamAcceptPercent; - GraphicLabel labelFallbackCpuMultiplier; - GraphicListBox listBoxFallbackCpuMultiplier; - - GraphicLabel labelAllowInGameJoinPlayer; - GraphicCheckBox checkBoxAllowInGameJoinPlayer; - - GraphicLabel labelAllowTeamUnitSharing; - GraphicCheckBox checkBoxAllowTeamUnitSharing; - - GraphicLabel labelAllowTeamResourceSharing; - GraphicCheckBox checkBoxAllowTeamResourceSharing; - - - GraphicLabel labelAllowNativeLanguageTechtree; - GraphicCheckBox checkBoxAllowNativeLanguageTechtree; - - GraphicCheckBox checkBoxScenario; - GraphicLabel labelScenario; - GraphicListBox listBoxScenario; - - vector < string > scenarioFiles; - ScenarioInfo scenarioInfo; - vector < string > dirList; - string autoloadScenarioName; - time_t previewLoadDelayTimer; - bool needToLoadTextures; - bool enableScenarioTexturePreview; - Texture2D * - scenarioLogoTexture; - - bool needToSetChangedGameSettings; - time_t lastSetChangedGameSettings; - time_t lastMasterserverPublishing; - time_t lastNetworkPing; - time_t mapPublishingDelayTimer; - bool needToPublishDelayed; - - bool headlessHasConnectedPlayer; - - bool needToRepublishToMasterserver; - bool needToBroadcastServerSettings; - std::map < string, string > publishToServerInfo; - SimpleTaskThread * - publishToMasterserverThread; - SimpleTaskThread * - publishToClientsThread; - - ParentMenuState parentMenuState; - int - soundConnectionCount; - - time_t tMasterserverErrorElapsed; - bool showMasterserverError; - string masterServererErrorToShow; - - bool showGeneralError; - string generalErrorToShow; - bool serverInitError; - -//Console console; - ChatManager chatManager; - bool showFullConsole; - - string lastMapDataSynchError; - string lastTileDataSynchError; - string lastTechtreeDataSynchError; - - string defaultPlayerName; - int8 switchSetupRequestFlagType; - - bool enableFactionTexturePreview; - bool enableMapPreview; - - string currentTechName_factionPreview; - string currentFactionName_factionPreview; - string currentFactionLogo; - Texture2D * - factionTexture; - ::Shared::Graphics::VideoPlayer * factionVideo; - bool factionVideoSwitchedOffVolume; - - MapPreview mapPreview; - Texture2D * - mapPreviewTexture; - bool zoomedMap; - int - render_mapPreviewTexture_X; - int - render_mapPreviewTexture_Y; - int - render_mapPreviewTexture_W; - int - render_mapPreviewTexture_H; - - bool autostart; - GameSettings * - autoStartSettings; - - std::map < int, int > - lastSelectedTeamIndex; - float - rMultiplierOffset; - bool hasCheckedForUPNP; - - string lastCheckedCRCTilesetName; - string lastCheckedCRCTechtreeName; - string lastCheckedCRCMapName; - - string last_Forced_CheckedCRCTilesetName; - string last_Forced_CheckedCRCTechtreeName; - string last_Forced_CheckedCRCMapName; - - uint32 lastCheckedCRCTilesetValue; - uint32 lastCheckedCRCTechtreeValue; - uint32 lastCheckedCRCMapValue; - vector < pair < string, uint32 > >factionCRCList; - - bool forceWaitForShutdown; - bool headlessServerMode; - bool masterserverModeMinimalResources; - int - lastMasterServerSettingsUpdateCount; - - auto_ptr < TechTree > techTree; - - string gameUUID; - - int - lastGameSettingsreceivedCount; - - public: - MenuStateCustomGame (Program * program, MainMenu * mainMenu, - bool openNetworkSlots = - false, ParentMenuState parentMenuState = - pNewGame, bool autostart = - false, GameSettings * settings = - NULL, bool masterserverMode = - false, string autoloadScenarioName = ""); - virtual ~ MenuStateCustomGame (); - - void - mouseClick (int x, int y, MouseButton mouseButton); - void - mouseMove (int x, int y, const MouseState * mouseState); - void - render (); - void - update (); - - virtual bool textInput (std::string text); - virtual void - keyDown (SDL_KeyboardEvent key); - virtual void - keyPress (SDL_KeyboardEvent c); - virtual void - keyUp (SDL_KeyboardEvent key); - - - virtual void - simpleTask (BaseThread * callingThread, void *userdata); - virtual void - setupTask (BaseThread * callingThread, void *userdata); - virtual void - shutdownTask (BaseThread * callingThread, void *userdata); - static void - setupTaskStatic (BaseThread * callingThread); - static void - shutdownTaskStatic (BaseThread * callingThread); - - virtual bool isInSpecialKeyCaptureEvent (); - virtual bool isMasterserverMode ()const; - - virtual bool isVideoPlaying (); - private: - - void - lastPlayerDisconnected (); - bool hasNetworkGameSettings (); - void - loadGameSettings (GameSettings * gameSettings, - bool forceCloseUnusedSlots = false); - void - loadMapInfo (string file, MapInfo * mapInfo, bool loadMapPreview); - void - cleanupMapPreviewTexture (); - - void - updateControllers (); - void - closeUnusedSlots (); - void - updateNetworkSlots (); - void - publishToMasterserver (); - void - returnToParentMenu (); - void - showMessageBox (const string & text, const string & header, - bool toggle); - - void - saveGameSettingsToFile (std::string fileName); - void - switchToNextMapGroup (const int direction); - void - updateAllResourceMultiplier (); - void - updateResourceMultiplier (const int index); - string getCurrentMapFile (); - void - setActiveInputLabel (GraphicLabel * newLable); - string getHumanPlayerName (int index = -1); - - void - loadFactionTexture (string filepath); - - GameSettings loadGameSettingsFromFile (std::string fileName); - void - loadGameSettings (const std::string & fileName); - void - RestoreLastGameSettings (); - void - PlayNow (bool saveGame); - - void - SetActivePlayerNameEditor (); - void - cleanup (); - - int32 getNetworkPlayerStatus (); - void - setupUIFromGameSettings (const GameSettings & gameSettings); - - void - switchSetupForSlots (SwitchSetupRequest ** switchSetupRequests, - ServerInterface * &serverInterface, int startIndex, - int endIndex, bool onlyNetworkUnassigned); - - string createGameName (string controllingPlayer = ""); - void - reloadUI (); - void - loadScenarioInfo (string file, ScenarioInfo * scenarioInfo); - void - processScenario (); - void - SetupUIForScenarios (); - int - setupMapList (string scenario); - int - setupTechList (string scenario, bool forceLoad = false); - void - reloadFactions (bool keepExistingSelectedItem, string scenario); - void - setupTilesetList (string scenario); - void - setSlotHuman (int i); - - void - initFactionPreview (const GameSettings * gameSettings); - - bool - checkNetworkPlayerDataSynch (bool checkMapCRC, bool checkTileSetCRC, - bool checkTechTreeCRC); - - void - cleanupThread (SimpleTaskThread ** thread); - void - simpleTaskForMasterServer (BaseThread * callingThread); - void - simpleTaskForClients (BaseThread * callingThread); - void - KeepCurrentHumanPlayerSlots (GameSettings & gameSettings); - }; - -}} //end namespace +namespace Glest { + namespace Game { + + class SwitchSetupRequest; + class ServerInterface; + class TechTree; + + enum ParentMenuState { + pNewGame, + pMasterServer, + pLanGame + }; + + // =============================== + // class MenuStateCustomGame + // =============================== + + class MenuStateCustomGame : + public MenuState, public SimpleTaskCallbackInterface { + private: + GraphicButton buttonReturn; + GraphicButton buttonPlayNow; + GraphicButton buttonRestoreLastSettings; + GraphicLabel labelControl; + GraphicLabel labelRMultiplier; + GraphicLabel labelFaction; + GraphicLabel labelTeam; + GraphicLabel labelMap; + GraphicLabel labelFogOfWar; + GraphicLabel labelTechTree; + GraphicLabel labelTileset; + GraphicLabel labelMapInfo; + GraphicLabel labelLocalGameVersion; + GraphicLabel labelLocalIP; + GraphicLabel labelGameName; + + GraphicListBox listBoxMap; + GraphicListBox listBoxFogOfWar; + GraphicListBox listBoxTechTree; + GraphicListBox listBoxTileset; + + vector < string > mapFiles; + vector < string > playerSortedMaps[GameConstants::maxPlayers + 1]; + vector < + string > formattedPlayerSortedMaps[GameConstants::maxPlayers + 1]; + vector < string > techTreeFiles; + vector < string > tilesetFiles; + vector < string > factionFiles; + GraphicLabel labelPlayers[GameConstants::maxPlayers]; + GraphicLabel labelPlayerNames[GameConstants::maxPlayers]; + GraphicListBox listBoxControls[GameConstants::maxPlayers]; + GraphicButton buttonBlockPlayers[GameConstants::maxPlayers]; + GraphicListBox listBoxRMultiplier[GameConstants::maxPlayers]; + GraphicListBox listBoxFactions[GameConstants::maxPlayers]; + GraphicListBox listBoxTeams[GameConstants::maxPlayers]; + GraphicLabel labelNetStatus[GameConstants::maxPlayers]; + MapInfo mapInfo; + + GraphicButton buttonClearBlockedPlayers; + + GraphicLabel labelPublishServer; + GraphicCheckBox checkBoxPublishServer; + + GraphicMessageBox mainMessageBox; + int + mainMessageBoxState; + + GraphicLabel labelNetworkPauseGameForLaggedClients; + GraphicCheckBox checkBoxNetworkPauseGameForLaggedClients; + + GraphicLabel labelMapFilter; + GraphicListBox listBoxMapFilter; + + GraphicLabel labelAdvanced; + GraphicCheckBox checkBoxAdvanced; + + GraphicLabel labelAllowObservers; + GraphicCheckBox checkBoxAllowObservers; + + GraphicLabel * + activeInputLabel; + + GraphicLabel labelPlayerStatus[GameConstants::maxPlayers]; + GraphicListBox listBoxPlayerStatus; + + GraphicLabel labelEnableSwitchTeamMode; + GraphicCheckBox checkBoxEnableSwitchTeamMode; + + GraphicLabel labelAISwitchTeamAcceptPercent; + GraphicListBox listBoxAISwitchTeamAcceptPercent; + GraphicLabel labelFallbackCpuMultiplier; + GraphicListBox listBoxFallbackCpuMultiplier; + + GraphicLabel labelAllowInGameJoinPlayer; + GraphicCheckBox checkBoxAllowInGameJoinPlayer; + + GraphicLabel labelAllowTeamUnitSharing; + GraphicCheckBox checkBoxAllowTeamUnitSharing; + + GraphicLabel labelAllowTeamResourceSharing; + GraphicCheckBox checkBoxAllowTeamResourceSharing; + + + GraphicLabel labelAllowNativeLanguageTechtree; + GraphicCheckBox checkBoxAllowNativeLanguageTechtree; + + GraphicCheckBox checkBoxScenario; + GraphicLabel labelScenario; + GraphicListBox listBoxScenario; + + vector < string > scenarioFiles; + ScenarioInfo scenarioInfo; + vector < string > dirList; + string autoloadScenarioName; + time_t previewLoadDelayTimer; + bool needToLoadTextures; + bool enableScenarioTexturePreview; + Texture2D * + scenarioLogoTexture; + + bool needToSetChangedGameSettings; + time_t lastSetChangedGameSettings; + time_t lastMasterserverPublishing; + time_t lastNetworkPing; + time_t mapPublishingDelayTimer; + bool needToPublishDelayed; + + bool headlessHasConnectedPlayer; + + bool needToRepublishToMasterserver; + bool needToBroadcastServerSettings; + std::map < string, string > publishToServerInfo; + SimpleTaskThread * + publishToMasterserverThread; + SimpleTaskThread * + publishToClientsThread; + + ParentMenuState parentMenuState; + int + soundConnectionCount; + + time_t tMasterserverErrorElapsed; + bool showMasterserverError; + string masterServererErrorToShow; + + bool showGeneralError; + string generalErrorToShow; + bool serverInitError; + + //Console console; + ChatManager chatManager; + bool showFullConsole; + + string lastMapDataSynchError; + string lastTileDataSynchError; + string lastTechtreeDataSynchError; + + string defaultPlayerName; + int8 switchSetupRequestFlagType; + + bool enableFactionTexturePreview; + bool enableMapPreview; + + string currentTechName_factionPreview; + string currentFactionName_factionPreview; + string currentFactionLogo; + Texture2D * + factionTexture; + ::Shared::Graphics::VideoPlayer * factionVideo; + bool factionVideoSwitchedOffVolume; + + MapPreview mapPreview; + Texture2D * + mapPreviewTexture; + bool zoomedMap; + int + render_mapPreviewTexture_X; + int + render_mapPreviewTexture_Y; + int + render_mapPreviewTexture_W; + int + render_mapPreviewTexture_H; + + bool autostart; + GameSettings * + autoStartSettings; + + std::map < int, int > + lastSelectedTeamIndex; + float + rMultiplierOffset; + bool hasCheckedForUPNP; + + string lastCheckedCRCTilesetName; + string lastCheckedCRCTechtreeName; + string lastCheckedCRCMapName; + + string last_Forced_CheckedCRCTilesetName; + string last_Forced_CheckedCRCTechtreeName; + string last_Forced_CheckedCRCMapName; + + uint32 lastCheckedCRCTilesetValue; + uint32 lastCheckedCRCTechtreeValue; + uint32 lastCheckedCRCMapValue; + vector < pair < string, uint32 > >factionCRCList; + + bool forceWaitForShutdown; + bool headlessServerMode; + bool masterserverModeMinimalResources; + int + lastMasterServerSettingsUpdateCount; + + auto_ptr < TechTree > techTree; + + string gameUUID; + + int + lastGameSettingsreceivedCount; + + public: + MenuStateCustomGame(Program * program, MainMenu * mainMenu, + bool openNetworkSlots = + false, ParentMenuState parentMenuState = + pNewGame, bool autostart = + false, GameSettings * settings = + NULL, bool masterserverMode = + false, string autoloadScenarioName = ""); + virtual ~MenuStateCustomGame(); + + void + mouseClick(int x, int y, MouseButton mouseButton); + void + mouseMove(int x, int y, const MouseState * mouseState); + void + render(); + void + update(); + + virtual bool textInput(std::string text); + virtual void + keyDown(SDL_KeyboardEvent key); + virtual void + keyPress(SDL_KeyboardEvent c); + virtual void + keyUp(SDL_KeyboardEvent key); + + + virtual void + simpleTask(BaseThread * callingThread, void *userdata); + virtual void + setupTask(BaseThread * callingThread, void *userdata); + virtual void + shutdownTask(BaseThread * callingThread, void *userdata); + static void + setupTaskStatic(BaseThread * callingThread); + static void + shutdownTaskStatic(BaseThread * callingThread); + + virtual bool isInSpecialKeyCaptureEvent(); + virtual bool isMasterserverMode()const; + + virtual bool isVideoPlaying(); + private: + + void + lastPlayerDisconnected(); + bool hasNetworkGameSettings(); + void + loadGameSettings(GameSettings * gameSettings, + bool forceCloseUnusedSlots = false); + void + loadMapInfo(string file, MapInfo * mapInfo, bool loadMapPreview); + void + cleanupMapPreviewTexture(); + + void + updateControllers(); + void + closeUnusedSlots(); + void + updateNetworkSlots(); + void + publishToMasterserver(); + void + returnToParentMenu(); + void + showMessageBox(const string & text, const string & header, + bool toggle); + + void + saveGameSettingsToFile(std::string fileName); + void + switchToNextMapGroup(const int direction); + void + updateAllResourceMultiplier(); + void + updateResourceMultiplier(const int index); + string getCurrentMapFile(); + void + setActiveInputLabel(GraphicLabel * newLable); + string getHumanPlayerName(int index = -1); + + void + loadFactionTexture(string filepath); + + GameSettings loadGameSettingsFromFile(std::string fileName); + void + loadGameSettings(const std::string & fileName); + void + RestoreLastGameSettings(); + void + PlayNow(bool saveGame); + + void + SetActivePlayerNameEditor(); + void + cleanup(); + + int32 getNetworkPlayerStatus(); + void + setupUIFromGameSettings(const GameSettings & gameSettings); + + void + switchSetupForSlots(SwitchSetupRequest ** switchSetupRequests, + ServerInterface * &serverInterface, int startIndex, + int endIndex, bool onlyNetworkUnassigned); + + string createGameName(string controllingPlayer = ""); + void + reloadUI(); + void + loadScenarioInfo(string file, ScenarioInfo * scenarioInfo); + void + processScenario(); + void + SetupUIForScenarios(); + int + setupMapList(string scenario); + int + setupTechList(string scenario, bool forceLoad = false); + void + reloadFactions(bool keepExistingSelectedItem, string scenario); + void + setupTilesetList(string scenario); + void + setSlotHuman(int i); + + void + initFactionPreview(const GameSettings * gameSettings); + + bool + checkNetworkPlayerDataSynch(bool checkMapCRC, bool checkTileSetCRC, + bool checkTechTreeCRC); + + void + cleanupThread(SimpleTaskThread ** thread); + void + simpleTaskForMasterServer(BaseThread * callingThread); + void + simpleTaskForClients(BaseThread * callingThread); + void + KeepCurrentHumanPlayerSlots(GameSettings & gameSettings); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_custom_game_update.cpp b/source/glest_game/menu/menu_state_custom_game_update.cpp index 902d790fb..fbbcce85c 100644 --- a/source/glest_game/menu/menu_state_custom_game_update.cpp +++ b/source/glest_game/menu/menu_state_custom_game_update.cpp @@ -27,1272 +27,1108 @@ #include "server_interface.h" #include "network_manager.h" -namespace Glest -{ - namespace Game - { - const int MASTERSERVER_BROADCAST_PUBLISH_SECONDS = 6; - const int BROADCAST_SETTINGS_SECONDS = 4; - const int BROADCAST_MAP_DELAY_SECONDS = 5; - -// These functions are prototyped in menu_state_custom_game.h - - void MenuStateCustomGame::update () - { - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - -// Test openal buffer underrun issue -//sleep(200); -// END - - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - MutexSafeWrapper - safeMutexCLI ((publishToClientsThread != - NULL ? - publishToClientsThread->getMutexThreadObjectAccessor () - : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - try - { - if (serverInitError == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (showGeneralError) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - - showGeneralError = false; - mainMessageBoxState = 1; - showMessageBox (generalErrorToShow, "Error", false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (this->headlessServerMode == false) - { - return; - } - } - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - Lang & lang = Lang::getInstance (); - - if (serverInterface != NULL - && serverInterface->getServerSocket () != NULL) - { - buttonClearBlockedPlayers. - setEditable (serverInterface->getServerSocket - ()->hasBlockedIPAddresses ()); - } - - if (this->autoloadScenarioName != "") - { - listBoxScenario.setSelectedItem (formatString - (this->autoloadScenarioName), - false); - lastSetChangedGameSettings = time (NULL); - if (serverInterface != NULL) - { - lastGameSettingsreceivedCount = - serverInterface->getGameSettingsUpdateCount (); - } - if (listBoxScenario.getSelectedItem () != - formatString (this->autoloadScenarioName)) - { - mainMessageBoxState = 1; - showMessageBox ("Could not find scenario name: " + - formatString (this->autoloadScenarioName), - "Scenario Missing", false); - this->autoloadScenarioName = ""; - } - else - { - loadScenarioInfo (Scenario::getScenarioPath (dirList, - scenarioFiles - [listBoxScenario.getSelectedItemIndex - ()]), - &scenarioInfo); -//labelInfo.setText(scenarioInfo.desc); - - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - CoreData & coreData = CoreData::getInstance (); - soundRenderer.playFx (coreData.getClickSoundC ()); -//launchGame(); - PlayNow (true); - return; - } - } - - if (needToLoadTextures) - { -// this delay is done to make it possible to switch faster - if (difftime ((long int) time (NULL), previewLoadDelayTimer) >= 2) - { -//loadScenarioPreviewTexture(); - needToLoadTextures = false; - } - } - -//bool haveAtLeastOneNetworkClientConnected = false; - bool hasOneNetworkSlotOpen = false; - int currentConnectionCount = 0; - Config & config = Config::getInstance (); - - bool masterServerErr = showMasterserverError; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (masterServerErr) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - if (EndsWith (masterServererErrorToShow, "wrong router setup") == - true) - { - masterServererErrorToShow = lang.getString ("WrongRouterSetup"); - } - - Lang & lang = Lang::getInstance (); - string publishText = " (disabling publish)"; - if (lang.hasString ("PublishDisabled") == true) - { - publishText = lang.getString ("PublishDisabled"); - } - - masterServererErrorToShow += "\n\n" + publishText; - showMasterserverError = false; - mainMessageBoxState = 1; - showMessageBox (masterServererErrorToShow, - lang.getString ("ErrorFromMasterserver"), false); - - if (this->headlessServerMode == false) - { - checkBoxPublishServer.setValue (false); - } - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface-> - setPublishEnabled (checkBoxPublishServer.getValue () == true); - } - else if (showGeneralError) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - showGeneralError = false; - mainMessageBoxState = 1; - showMessageBox (generalErrorToShow, "Error", false); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (this->headlessServerMode == true && serverInterface == NULL) - { - throw megaglest_runtime_error ("serverInterface == NULL"); - } - if (this->headlessServerMode == true - && serverInterface->getGameSettingsUpdateCount () > - lastMasterServerSettingsUpdateCount - && serverInterface->getGameSettings () != NULL) - { - const GameSettings *settings = serverInterface->getGameSettings (); -//printf("\n\n\n\n=====#1 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),settings->toString().c_str()); - - lastMasterServerSettingsUpdateCount = - serverInterface->getGameSettingsUpdateCount (); -//printf("#2 custom menu got map [%s]\n",settings->getMap().c_str()); - - setupUIFromGameSettings (*settings); - printf ("received Settings map filter=%d\n", - settings->getMapFilter ()); - - GameSettings gameSettings; - loadGameSettings (&gameSettings); - -//printf("\n\n\n\n=====#1.1 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),gameSettings.toString().c_str()); - - } - if (this->headlessServerMode == true - && serverInterface->getMasterserverAdminRequestLaunch () == true) - { - serverInterface->setMasterserverAdminRequestLaunch (false); - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - - PlayNow (false); - return; - } - -// handle setting changes from clients - SwitchSetupRequest **switchSetupRequests = - serverInterface->getSwitchSetupRequests (); -//!!! - switchSetupForSlots (switchSetupRequests, serverInterface, 0, - mapInfo.players, false); - switchSetupForSlots (switchSetupRequests, serverInterface, - mapInfo.players, GameConstants::maxPlayers, - true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - GameSettings gameSettings; - loadGameSettings (&gameSettings); - - listBoxAISwitchTeamAcceptPercent.setEnabled - (checkBoxEnableSwitchTeamMode.getValue ()); - - int factionCount = 0; - for (int i = 0; i < mapInfo.players; ++i) - { - if (hasNetworkGameSettings () == true) - { - if (listBoxControls[i].getSelectedItemIndex () != ctClosed) - { - int slotIndex = factionCount; - if (listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - switch (gameSettings.getNetworkPlayerStatuses (slotIndex)) - { - case npst_BeRightBack: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusBRBTexture ()); - break; - case npst_Ready: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusReadyTexture - ()); - break; - case npst_PickSettings: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusNotReadyTexture - ()); - break; - case npst_Disconnected: - labelPlayerStatus[i].setTexture (NULL); - break; - - default: - labelPlayerStatus[i].setTexture (NULL); - break; - } - } - else - { - labelPlayerStatus[i].setTexture (NULL); - } - - factionCount++; - } - else - { - labelPlayerStatus[i].setTexture (NULL); - } - } - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork || - listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - hasOneNetworkSlotOpen = true; - - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, true)->isConnected ()) - { - - if (hasNetworkGameSettings () == true) - { - switch (serverInterface-> - getSlot (i, true)->getNetworkPlayerStatus ()) - { - case npst_BeRightBack: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusBRBTexture ()); - break; - case npst_Ready: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusReadyTexture - ()); - break; - case npst_PickSettings: - default: - labelPlayerStatus[i].setTexture (CoreData::getInstance - ().getStatusNotReadyTexture - ()); - break; - } - } - - serverInterface->getSlot (i, - true)->setName (labelPlayerNames[i]. - getText ()); - -//printf("FYI we have at least 1 client connected, slot = %d'\n",i); - -//haveAtLeastOneNetworkClientConnected = true; - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, - true)->getConnectHasHandshaked ()) - { - currentConnectionCount++; - } - string label = - (serverInterface->getSlot (i, true) != - NULL ? serverInterface->getSlot (i, - true)->getVersionString () : - ""); - - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, - true)->getAllowDownloadDataSynch - () == true - && serverInterface->getSlot (i, - true)->getAllowGameDataSynchCheck - () == true) - { - if (serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOk - () == false) - { - label += " -waiting to synch:"; - if (serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkMap - () == false) - { - label = label + " map"; - } - if (serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkTile - () == false) - { - label = label + " tile"; - } - if (serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkTech - () == false) - { - label = label + " techtree"; - } - } - else - { - label += " - data synch is ok"; - } - } - else - { - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, - true)->getAllowGameDataSynchCheck - () == true - && serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOk - () == false) - { - label += " -synch mismatch:"; - - if (serverInterface->getSlot (i, true) != NULL - && serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkMap - () == false) - { - label = label + " map"; - - if (serverInterface->getSlot (i, - true)->getReceivedDataSynchCheck - () == true - && lastMapDataSynchError != - "map CRC mismatch, " + listBoxMap.getSelectedItem ()) - { - lastMapDataSynchError = - "map CRC mismatch, " + listBoxMap.getSelectedItem (); - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface->sendTextMessage (lastMapDataSynchError, - -1, true, ""); - } - } - - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkTile - () == false) - { - label = label + " tile"; - - if (serverInterface->getSlot (i, - true)->getReceivedDataSynchCheck - () == true - && lastTileDataSynchError != - "tile CRC mismatch, " + - listBoxTileset.getSelectedItem ()) - { - lastTileDataSynchError = - "tile CRC mismatch, " + - listBoxTileset.getSelectedItem (); - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface->sendTextMessage - (lastTileDataSynchError, -1, true, ""); - } - } - - if (serverInterface->getSlot (i, true) != NULL && - serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckOkTech - () == false) - { - label = label + " techtree"; - - if (serverInterface->getSlot (i, - true)->getReceivedDataSynchCheck - () == true) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - string report = serverInterface->getSlot (i, - true)->getNetworkGameDataSynchCheckTechMismatchReport - (); - - if (lastTechtreeDataSynchError != - "techtree CRC mismatch" + report) - { - lastTechtreeDataSynchError = - "techtree CRC mismatch" + report; - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] report: %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), - __FUNCTION__, - __LINE__, - report.c_str ()); - - serverInterface->sendTextMessage - ("techtree CRC mismatch", -1, true, ""); - vector < string > reportLineTokens; - Tokenize (report, reportLineTokens, "\n"); - for (int reportLine = 0; - reportLine < - (int) reportLineTokens.size (); ++reportLine) - { - serverInterface->sendTextMessage - (reportLineTokens[reportLine], -1, true, ""); - } - } - } - } - - if (serverInterface->getSlot (i, true) != NULL) - { - serverInterface->getSlot (i, - true)->setReceivedDataSynchCheck - (false); - } - } - } - -//float pingTime = serverInterface->getSlot(i)->getThreadedPingMS(serverInterface->getSlot(i)->getIpAddress().c_str()); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s", label.c_str ()); - - labelNetStatus[i].setText (szBuf); - } - else - { - string port = - "(" + intToStr (config.getInt ("PortServer")) + ")"; - labelNetStatus[i].setText ("--- " + port); - } - } - else - { - labelNetStatus[i].setText (""); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (checkBoxScenario.getValue () == false) - { - // When scenario is checked the value for mapInfo.players is reset to - // hardMaxPlayers. This resets it. - mapInfo.players = checkBoxAllowObservers.getValue () == true ? - GameConstants::maxPlayers : - mapInfo.hardMaxPlayers; - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - if (i >= mapInfo.hardMaxPlayers) - { - if (checkBoxAllowObservers.getValue() == false) - { - listBoxControls[i].setSelectedItemIndex (ctClosed); - listBoxControls[i].setEditable (false); - listBoxControls[i].setEnabled (false); - } - else - { - if (listBoxControls[i].getSelectedItemIndex () != ctHuman) - { - listBoxControls[i].setSelectedItemIndex (ctNetwork); - } - listBoxControls[i].setEditable (i == GameConstants::maxPlayers - 1); - listBoxControls[i].setEnabled (i == GameConstants::maxPlayers - 1); - listBoxFactions[i].setSelectedItem (GameConstants::OBSERVER_SLOTNAME); - listBoxFactions[i].setEditable (false); - listBoxTeams[i].setSelectedItem (intToStr (GameConstants::maxPlayers + - fpt_Observer)); - listBoxTeams[i].setEditable (false); - } - - listBoxRMultiplier[i].setEditable (false); - listBoxRMultiplier[i].setEnabled (false); - listBoxRMultiplier[i].setVisible (false); - } - else if (listBoxControls[i].getSelectedItemIndex () != - ctNetworkUnassigned) - { - ConnectionSlot *slot = serverInterface->getSlot (i, true); - if ((listBoxControls[i].getSelectedItemIndex () != - ctNetwork) - || (listBoxControls[i].getSelectedItemIndex () == - ctNetwork && (slot == NULL - || slot->isConnected () == false))) - { - listBoxControls[i].setEditable (true); - listBoxControls[i].setEnabled (true); - listBoxFactions[i].setEditable (true); - listBoxTeams[i].setEditable (true); - - if (listBoxControls[i].getSelectedItemIndex () == ctNetwork && - listBoxFactions[i].getSelectedItem () == formatString (GameConstants::OBSERVER_SLOTNAME)) - { - listBoxFactions[i].setSelectedItemIndex (0); - } - } - else - { - listBoxControls[i].setEditable (false); - listBoxControls[i].setEnabled (false); - } - } - else - { - listBoxControls[i].setEditable (false); - listBoxControls[i].setEnabled (false); - } - } - } - else // if this is a scenario... - { - int i = mapInfo.players; - do - { - listBoxControls[i].setSelectedItemIndex (ctClosed); - listBoxControls[i].setEditable (false); - listBoxControls[i].setEnabled (false); - } while (++i < GameConstants::maxPlayers); - } - - // updateNetworkSlots (); - - bool checkDataSynch = - (serverInterface->getAllowGameDataSynchCheck () == true - && needToSetChangedGameSettings == true - && - ((difftime ((long int) time (NULL), lastSetChangedGameSettings) >= - BROADCAST_SETTINGS_SECONDS) - || (this->headlessServerMode == true))); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -// Send the game settings to each client if we have at least one networked client - if (checkDataSynch == true) - { - serverInterface->setGameSettings (&gameSettings, false); - needToSetChangedGameSettings = false; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (this->headlessServerMode == true || hasOneNetworkSlotOpen == true - || checkBoxAllowInGameJoinPlayer.getValue () == true) - { - if (this->headlessServerMode == true && - GlobalStaticFlags::isFlagSet (gsft_lan_mode) == false) - { - checkBoxPublishServer.setValue (true); - } - listBoxFallbackCpuMultiplier.setEditable (true); - checkBoxPublishServer.setEditable (true); - -// Masterserver always needs one network slot - if (this->headlessServerMode == true - && hasOneNetworkSlotOpen == false) - { - bool anyoneConnected = false; - for (int i = 0; i < mapInfo.players; ++i) - { - MutexSafeWrapper - safeMutex ((publishToMasterserverThread != - NULL ? - publishToMasterserverThread->getMutexThreadObjectAccessor - () : NULL), - string (__FILE__) + "_" + intToStr (__LINE__)); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = serverInterface->getSlot (i, true); - if (slot != NULL && slot->isConnected () == true) - { - anyoneConnected = true; - break; - } - } - - for (int i = 0; i < mapInfo.players; ++i) - { - if (anyoneConnected == false - && listBoxControls[i].getSelectedItemIndex () != ctNetwork) - { - listBoxControls[i].setSelectedItemIndex (ctNetwork); - } - } - - updateNetworkSlots (); - } - } - else - { - checkBoxPublishServer.setValue (false); - checkBoxPublishServer.setEditable (false); - listBoxFallbackCpuMultiplier.setEditable (false); - listBoxFallbackCpuMultiplier.setSelectedItem ("1.0"); - - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface-> - setPublishEnabled (checkBoxPublishServer.getValue () == true); - } - - bool republishToMaster = - (difftime ((long int) time (NULL), lastMasterserverPublishing) >= - MASTERSERVER_BROADCAST_PUBLISH_SECONDS); - - if (republishToMaster == true) - { - if (checkBoxPublishServer.getValue () == true) - { - needToRepublishToMasterserver = true; - lastMasterserverPublishing = time (NULL); - } - } - - bool callPublishNow = (checkBoxPublishServer.getEditable () && - checkBoxPublishServer.getValue () == true && - needToRepublishToMasterserver == true); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (callPublishNow == true) - { -// give it to me baby, aha aha ... - publishToMasterserver (); - } - if (needToPublishDelayed) - { -// this delay is done to make it possible to switch over maps which are not meant to be distributed - if ((difftime ((long int) time (NULL), mapPublishingDelayTimer) >= - BROADCAST_MAP_DELAY_SECONDS) - || (this->headlessServerMode == true)) - { -// after 5 seconds we are allowed to publish again! - needToSetChangedGameSettings = true; - lastSetChangedGameSettings = time (NULL); -// set to normal.... - needToPublishDelayed = false; - } - } - if (needToPublishDelayed == false || headlessServerMode == true) - { - bool broadCastSettings = - (difftime ((long int) time (NULL), lastSetChangedGameSettings) - >= BROADCAST_SETTINGS_SECONDS); - - if (headlessServerMode == true) - { -// publish settings directly when we receive them - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - if (lastGameSettingsreceivedCount < - serverInterface->getGameSettingsUpdateCount ()) - { - needToBroadcastServerSettings = true; - lastSetChangedGameSettings = time (NULL); - lastGameSettingsreceivedCount = - serverInterface->getGameSettingsUpdateCount (); - } - } - - if (broadCastSettings == true) - { - needToBroadcastServerSettings = true; - lastSetChangedGameSettings = time (NULL); - } - - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - -//broadCastSettings = (difftime(time(NULL),lastSetChangedGameSettings) >= 2); -//if (broadCastSettings == true) {// reset timer here on bottom becasue used for different things -// lastSetChangedGameSettings = time(NULL); -//} - } - - if (this->headlessServerMode == true) - { - lastPlayerDisconnected (); - } - -//call the chat manager - chatManager.updateNetwork (); - -//console - console.update (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (currentConnectionCount > soundConnectionCount) - { - soundConnectionCount = currentConnectionCount; - static PlaySoundClip snd; - SoundRenderer::getInstance (). - playFx (snd.getSound (snd.sfxAttention)); -//switch on music again!! - Config & config = Config::getInstance (); - float configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - CoreData::getInstance ().getMenuMusic ()->setVolume (configVolume); - } - soundConnectionCount = currentConnectionCount; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (enableFactionTexturePreview == true) - { - if (currentTechName_factionPreview != gameSettings.getTech () || - currentFactionName_factionPreview != - gameSettings. - getFactionTypeName (gameSettings.getThisFactionIndex ())) - { - - currentTechName_factionPreview = gameSettings.getTech (); - currentFactionName_factionPreview = - gameSettings. - getFactionTypeName (gameSettings.getThisFactionIndex ()); - - initFactionPreview (&gameSettings); - } - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s Line: %d] took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - chrono.start (); - - if (autostart == true) - { - autostart = false; - safeMutex.ReleaseLock (); - safeMutexCLI.ReleaseLock (); - if (autoStartSettings != NULL) - { - - setupUIFromGameSettings (*autoStartSettings); - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - serverInterface->setGameSettings (autoStartSettings, false); - } - else - { - RestoreLastGameSettings (); - } - PlayNow ((autoStartSettings == NULL)); - return; - } - } - catch (megaglest_runtime_error & ex) - { -//abort(); -//printf("1111111bbbb ex.wantStackTrace() = %d\n",ex.wantStackTrace()); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - -//printf("2222222bbbb ex.wantStackTrace() = %d\n",ex.wantStackTrace()); - - showGeneralError = true; - generalErrorToShow = szBuf; - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - showGeneralError = true; - generalErrorToShow = szBuf; - } - } - - void MenuStateCustomGame::updateControllers () - { - try - { - bool humanPlayer = false; - - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == ctHuman) - { - humanPlayer = true; - } - } - - if (humanPlayer == false) - { - if (this->headlessServerMode == false) - { - bool foundNewSlotForHuman = false; - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == ctClosed) - { - setSlotHuman (i); - foundNewSlotForHuman = true; - break; - } - } - - if (foundNewSlotForHuman == false) - { - for (int i = 0; i < mapInfo.players; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () == - ctClosed - || listBoxControls[i].getSelectedItemIndex () == - ctCpuEasy - || listBoxControls[i].getSelectedItemIndex () == - ctCpu - || listBoxControls[i].getSelectedItemIndex () == - ctCpuUltra - || listBoxControls[i].getSelectedItemIndex () == - ctCpuZeta) - { - setSlotHuman (i); - - foundNewSlotForHuman = true; - break; - } - } - } - - if (foundNewSlotForHuman == false) - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - ConnectionSlot *slot = serverInterface->getSlot (0, true); - if (slot != NULL && slot->isConnected () == true) - { - serverInterface->removeSlot (0); - } - setSlotHuman (0); - } - } - } - - for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) - { - if (listBoxControls[i].getSelectedItemIndex () != ctNetwork && - listBoxControls[i].getSelectedItemIndex () != - ctNetworkUnassigned) - { -//printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); - - listBoxControls[i].setSelectedItemIndex (ctClosed); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - void MenuStateCustomGame::updateNetworkSlots () - { - try - { - ServerInterface *serverInterface = - NetworkManager::getInstance ().getServerInterface (); - - if (hasNetworkGameSettings () == true) - { - if (hasCheckedForUPNP == false) - { - - if (checkBoxPublishServer.getValue () == true || - this->headlessServerMode == true) - { - - hasCheckedForUPNP = true; - serverInterface->getServerSocket ()->NETdiscoverUPnPDevices (); - } - } - } - else - { - hasCheckedForUPNP = false; - } - - for (int i = 0; i < GameConstants::maxPlayers; ++i) - { - ConnectionSlot *slot = serverInterface->getSlot (i, true); -//printf("A i = %d control type = %d slot [%p]\n",i,listBoxControls[i].getSelectedItemIndex(),slot); - - if (slot == NULL && - listBoxControls[i].getSelectedItemIndex () == ctNetwork) - { - try - { - serverInterface->addSlot (i); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - showGeneralError = true; - if (serverInterface->isPortBound () == false) - { - generalErrorToShow = - Lang::getInstance ().getString ("ErrorBindingPort") + - " : " + intToStr (serverInterface->getBindPort ()); - } - else - { - generalErrorToShow = ex.what (); - } - -// Revert network to CPU - listBoxControls[i].setSelectedItemIndex (ctCpu); - } - } - slot = serverInterface->getSlot (i, true); - if (slot != NULL) - { - if ((listBoxControls[i].getSelectedItemIndex () != ctNetwork) - || (listBoxControls[i].getSelectedItemIndex () == - ctNetwork && slot->isConnected () == false - && i >= mapInfo.players)) - { - if (slot->getCanAcceptConnections () == true) - { - slot->setCanAcceptConnections (false); - } - if (slot->isConnected () == true) - { - if (listBoxControls[i].getSelectedItemIndex () != - ctNetworkUnassigned) - { - listBoxControls[i].setSelectedItemIndex - (ctNetworkUnassigned); - } - } - else - { - serverInterface->removeSlot (i); - - if (listBoxControls[i].getSelectedItemIndex () == - ctNetworkUnassigned) - { - listBoxControls[i].setSelectedItemIndex (ctClosed); - } - } - } - else if (slot->getCanAcceptConnections () == false) - { - slot->setCanAcceptConnections (true); - } - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); -//throw megaglest_runtime_error(szBuf); - showGeneralError = true; - generalErrorToShow = szBuf; - - } - } - - void MenuStateCustomGame::updateAllResourceMultiplier () - { - for (int j = 0; j < GameConstants::maxPlayers; ++j) - { - updateResourceMultiplier (j); - } - } - - void MenuStateCustomGame::updateResourceMultiplier (const int index) - { -//printf("Line: %d multiplier index: %d index: %d\n",__LINE__,listBoxRMultiplier[index].getSelectedItemIndex(),index); - - ControlType ct = - static_cast < ControlType > - (listBoxControls[index].getSelectedItemIndex ()); - if (ct == ctCpuEasy || ct == ctNetworkCpuEasy) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - easyMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (checkBoxScenario.getValue () == - false); - } - else if (ct == ctCpu || ct == ctNetworkCpu) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - normalMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (checkBoxScenario.getValue () == - false); - } - else if (ct == ctCpuUltra || ct == ctNetworkCpuUltra) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - ultraMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (checkBoxScenario.getValue () == - false); - } - else if (ct == ctCpuZeta || ct == ctNetworkCpuZeta) - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - megaMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (checkBoxScenario.getValue () == - false); - } -//if(ct == ctHuman || ct == ctNetwork || ct == ctClosed) { - else - { - listBoxRMultiplier[index].setSelectedItem (floatToStr - (GameConstants:: - normalMultiplier, 1)); - listBoxRMultiplier[index].setEnabled (false); -//!!!listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == false); - } - - listBoxRMultiplier[index]. - setEditable (listBoxRMultiplier[index].getEnabled ()); - listBoxRMultiplier[index].setVisible (ct != ctHuman && ct != ctNetwork - && ct != ctClosed); -//listBoxRMultiplier[index].setVisible(ct != ctClosed); - -//printf("Line: %d multiplier index: %d index: %d\n",__LINE__,listBoxRMultiplier[index].getSelectedItemIndex(),index); - } - } // end namespace +namespace Glest { + namespace Game { + const int MASTERSERVER_BROADCAST_PUBLISH_SECONDS = 6; + const int BROADCAST_SETTINGS_SECONDS = 4; + const int BROADCAST_MAP_DELAY_SECONDS = 5; + + // These functions are prototyped in menu_state_custom_game.h + + void MenuStateCustomGame::update() { + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + // Test openal buffer underrun issue + //sleep(200); + // END + + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + MutexSafeWrapper + safeMutexCLI((publishToClientsThread != + NULL ? + publishToClientsThread->getMutexThreadObjectAccessor() + : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + try { + if (serverInitError == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (showGeneralError) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + + showGeneralError = false; + mainMessageBoxState = 1; + showMessageBox(generalErrorToShow, "Error", false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (this->headlessServerMode == false) { + return; + } + } + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + Lang & lang = Lang::getInstance(); + + if (serverInterface != NULL + && serverInterface->getServerSocket() != NULL) { + buttonClearBlockedPlayers. + setEditable(serverInterface->getServerSocket + ()->hasBlockedIPAddresses()); + } + + if (this->autoloadScenarioName != "") { + listBoxScenario.setSelectedItem(formatString + (this->autoloadScenarioName), + false); + lastSetChangedGameSettings = time(NULL); + if (serverInterface != NULL) { + lastGameSettingsreceivedCount = + serverInterface->getGameSettingsUpdateCount(); + } + if (listBoxScenario.getSelectedItem() != + formatString(this->autoloadScenarioName)) { + mainMessageBoxState = 1; + showMessageBox("Could not find scenario name: " + + formatString(this->autoloadScenarioName), + "Scenario Missing", false); + this->autoloadScenarioName = ""; + } else { + loadScenarioInfo(Scenario::getScenarioPath(dirList, + scenarioFiles + [listBoxScenario.getSelectedItemIndex + ()]), + &scenarioInfo); + //labelInfo.setText(scenarioInfo.desc); + + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + CoreData & coreData = CoreData::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + //launchGame(); + PlayNow(true); + return; + } + } + + if (needToLoadTextures) { + // this delay is done to make it possible to switch faster + if (difftime((long int) time(NULL), previewLoadDelayTimer) >= 2) { + //loadScenarioPreviewTexture(); + needToLoadTextures = false; + } + } + + //bool haveAtLeastOneNetworkClientConnected = false; + bool hasOneNetworkSlotOpen = false; + int currentConnectionCount = 0; + Config & config = Config::getInstance(); + + bool masterServerErr = showMasterserverError; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (masterServerErr) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + if (EndsWith(masterServererErrorToShow, "wrong router setup") == + true) { + masterServererErrorToShow = lang.getString("WrongRouterSetup"); + } + + Lang & lang = Lang::getInstance(); + string publishText = " (disabling publish)"; + if (lang.hasString("PublishDisabled") == true) { + publishText = lang.getString("PublishDisabled"); + } + + masterServererErrorToShow += "\n\n" + publishText; + showMasterserverError = false; + mainMessageBoxState = 1; + showMessageBox(masterServererErrorToShow, + lang.getString("ErrorFromMasterserver"), false); + + if (this->headlessServerMode == false) { + checkBoxPublishServer.setValue(false); + } + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface-> + setPublishEnabled(checkBoxPublishServer.getValue() == true); + } else if (showGeneralError) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + showGeneralError = false; + mainMessageBoxState = 1; + showMessageBox(generalErrorToShow, "Error", false); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (this->headlessServerMode == true && serverInterface == NULL) { + throw megaglest_runtime_error("serverInterface == NULL"); + } + if (this->headlessServerMode == true + && serverInterface->getGameSettingsUpdateCount() > + lastMasterServerSettingsUpdateCount + && serverInterface->getGameSettings() != NULL) { + const GameSettings *settings = serverInterface->getGameSettings(); + //printf("\n\n\n\n=====#1 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),settings->toString().c_str()); + + lastMasterServerSettingsUpdateCount = + serverInterface->getGameSettingsUpdateCount(); + //printf("#2 custom menu got map [%s]\n",settings->getMap().c_str()); + + setupUIFromGameSettings(*settings); + printf("received Settings map filter=%d\n", + settings->getMapFilter()); + + GameSettings gameSettings; + loadGameSettings(&gameSettings); + + //printf("\n\n\n\n=====#1.1 got settings [%d] [%d]:\n%s\n",lastMasterServerSettingsUpdateCount,serverInterface->getGameSettingsUpdateCount(),gameSettings.toString().c_str()); + + } + if (this->headlessServerMode == true + && serverInterface->getMasterserverAdminRequestLaunch() == true) { + serverInterface->setMasterserverAdminRequestLaunch(false); + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + + PlayNow(false); + return; + } + + // handle setting changes from clients + SwitchSetupRequest **switchSetupRequests = + serverInterface->getSwitchSetupRequests(); + //!!! + switchSetupForSlots(switchSetupRequests, serverInterface, 0, + mapInfo.players, false); + switchSetupForSlots(switchSetupRequests, serverInterface, + mapInfo.players, GameConstants::maxPlayers, + true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + GameSettings gameSettings; + loadGameSettings(&gameSettings); + + listBoxAISwitchTeamAcceptPercent.setEnabled + (checkBoxEnableSwitchTeamMode.getValue()); + + int factionCount = 0; + for (int i = 0; i < mapInfo.players; ++i) { + if (hasNetworkGameSettings() == true) { + if (listBoxControls[i].getSelectedItemIndex() != ctClosed) { + int slotIndex = factionCount; + if (listBoxControls[i].getSelectedItemIndex() == ctHuman) { + switch (gameSettings.getNetworkPlayerStatuses(slotIndex)) { + case npst_BeRightBack: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusBRBTexture()); + break; + case npst_Ready: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusReadyTexture + ()); + break; + case npst_PickSettings: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusNotReadyTexture + ()); + break; + case npst_Disconnected: + labelPlayerStatus[i].setTexture(NULL); + break; + + default: + labelPlayerStatus[i].setTexture(NULL); + break; + } + } else { + labelPlayerStatus[i].setTexture(NULL); + } + + factionCount++; + } else { + labelPlayerStatus[i].setTexture(NULL); + } + } + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork || + listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + hasOneNetworkSlotOpen = true; + + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, true)->isConnected()) { + + if (hasNetworkGameSettings() == true) { + switch (serverInterface-> + getSlot(i, true)->getNetworkPlayerStatus()) { + case npst_BeRightBack: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusBRBTexture()); + break; + case npst_Ready: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusReadyTexture + ()); + break; + case npst_PickSettings: + default: + labelPlayerStatus[i].setTexture(CoreData::getInstance + ().getStatusNotReadyTexture + ()); + break; + } + } + + serverInterface->getSlot(i, + true)->setName(labelPlayerNames[i]. + getText()); + + //printf("FYI we have at least 1 client connected, slot = %d'\n",i); + + //haveAtLeastOneNetworkClientConnected = true; + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, + true)->getConnectHasHandshaked()) { + currentConnectionCount++; + } + string label = + (serverInterface->getSlot(i, true) != + NULL ? serverInterface->getSlot(i, + true)->getVersionString() : + ""); + + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, + true)->getAllowDownloadDataSynch + () == true + && serverInterface->getSlot(i, + true)->getAllowGameDataSynchCheck + () == true) { + if (serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOk + () == false) { + label += " -waiting to synch:"; + if (serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkMap + () == false) { + label = label + " map"; + } + if (serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkTile + () == false) { + label = label + " tile"; + } + if (serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkTech + () == false) { + label = label + " techtree"; + } + } else { + label += " - data synch is ok"; + } + } else { + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, + true)->getAllowGameDataSynchCheck + () == true + && serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOk + () == false) { + label += " -synch mismatch:"; + + if (serverInterface->getSlot(i, true) != NULL + && serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkMap + () == false) { + label = label + " map"; + + if (serverInterface->getSlot(i, + true)->getReceivedDataSynchCheck + () == true + && lastMapDataSynchError != + "map CRC mismatch, " + listBoxMap.getSelectedItem()) { + lastMapDataSynchError = + "map CRC mismatch, " + listBoxMap.getSelectedItem(); + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface->sendTextMessage(lastMapDataSynchError, + -1, true, ""); + } + } + + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkTile + () == false) { + label = label + " tile"; + + if (serverInterface->getSlot(i, + true)->getReceivedDataSynchCheck + () == true + && lastTileDataSynchError != + "tile CRC mismatch, " + + listBoxTileset.getSelectedItem()) { + lastTileDataSynchError = + "tile CRC mismatch, " + + listBoxTileset.getSelectedItem(); + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface->sendTextMessage + (lastTileDataSynchError, -1, true, ""); + } + } + + if (serverInterface->getSlot(i, true) != NULL && + serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckOkTech + () == false) { + label = label + " techtree"; + + if (serverInterface->getSlot(i, + true)->getReceivedDataSynchCheck + () == true) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + string report = serverInterface->getSlot(i, + true)->getNetworkGameDataSynchCheckTechMismatchReport + (); + + if (lastTechtreeDataSynchError != + "techtree CRC mismatch" + report) { + lastTechtreeDataSynchError = + "techtree CRC mismatch" + report; + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] report: %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), + __FUNCTION__, + __LINE__, + report.c_str()); + + serverInterface->sendTextMessage + ("techtree CRC mismatch", -1, true, ""); + vector < string > reportLineTokens; + Tokenize(report, reportLineTokens, "\n"); + for (int reportLine = 0; + reportLine < + (int) reportLineTokens.size(); ++reportLine) { + serverInterface->sendTextMessage + (reportLineTokens[reportLine], -1, true, ""); + } + } + } + } + + if (serverInterface->getSlot(i, true) != NULL) { + serverInterface->getSlot(i, + true)->setReceivedDataSynchCheck + (false); + } + } + } + + //float pingTime = serverInterface->getSlot(i)->getThreadedPingMS(serverInterface->getSlot(i)->getIpAddress().c_str()); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s", label.c_str()); + + labelNetStatus[i].setText(szBuf); + } else { + string port = + "(" + intToStr(config.getInt("PortServer")) + ")"; + labelNetStatus[i].setText("--- " + port); + } + } else { + labelNetStatus[i].setText(""); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (checkBoxScenario.getValue() == false) { + // When scenario is checked the value for mapInfo.players is reset to + // hardMaxPlayers. This resets it. + mapInfo.players = checkBoxAllowObservers.getValue() == true ? + GameConstants::maxPlayers : + mapInfo.hardMaxPlayers; + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + if (i >= mapInfo.hardMaxPlayers) { + if (checkBoxAllowObservers.getValue() == false) { + listBoxControls[i].setSelectedItemIndex(ctClosed); + listBoxControls[i].setEditable(false); + listBoxControls[i].setEnabled(false); + } else { + if (listBoxControls[i].getSelectedItemIndex() != ctHuman) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + } + listBoxControls[i].setEditable(i == GameConstants::maxPlayers - 1); + listBoxControls[i].setEnabled(i == GameConstants::maxPlayers - 1); + listBoxFactions[i].setSelectedItem(GameConstants::OBSERVER_SLOTNAME); + listBoxFactions[i].setEditable(false); + listBoxTeams[i].setSelectedItem(intToStr(GameConstants::maxPlayers + + fpt_Observer)); + listBoxTeams[i].setEditable(false); + } + + listBoxRMultiplier[i].setEditable(false); + listBoxRMultiplier[i].setEnabled(false); + listBoxRMultiplier[i].setVisible(false); + } else if (listBoxControls[i].getSelectedItemIndex() != + ctNetworkUnassigned) { + ConnectionSlot *slot = serverInterface->getSlot(i, true); + if ((listBoxControls[i].getSelectedItemIndex() != + ctNetwork) + || (listBoxControls[i].getSelectedItemIndex() == + ctNetwork && (slot == NULL + || slot->isConnected() == false))) { + listBoxControls[i].setEditable(true); + listBoxControls[i].setEnabled(true); + listBoxFactions[i].setEditable(true); + listBoxTeams[i].setEditable(true); + + if (listBoxControls[i].getSelectedItemIndex() == ctNetwork && + listBoxFactions[i].getSelectedItem() == formatString(GameConstants::OBSERVER_SLOTNAME)) { + listBoxFactions[i].setSelectedItemIndex(0); + } + } else { + listBoxControls[i].setEditable(false); + listBoxControls[i].setEnabled(false); + } + } else { + listBoxControls[i].setEditable(false); + listBoxControls[i].setEnabled(false); + } + } + } else // if this is a scenario... + { + int i = mapInfo.players; + do { + listBoxControls[i].setSelectedItemIndex(ctClosed); + listBoxControls[i].setEditable(false); + listBoxControls[i].setEnabled(false); + } while (++i < GameConstants::maxPlayers); + } + + // updateNetworkSlots (); + + bool checkDataSynch = + (serverInterface->getAllowGameDataSynchCheck() == true + && needToSetChangedGameSettings == true + && + ((difftime((long int) time(NULL), lastSetChangedGameSettings) >= + BROADCAST_SETTINGS_SECONDS) + || (this->headlessServerMode == true))); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + // Send the game settings to each client if we have at least one networked client + if (checkDataSynch == true) { + serverInterface->setGameSettings(&gameSettings, false); + needToSetChangedGameSettings = false; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (this->headlessServerMode == true || hasOneNetworkSlotOpen == true + || checkBoxAllowInGameJoinPlayer.getValue() == true) { + if (this->headlessServerMode == true && + GlobalStaticFlags::isFlagSet(gsft_lan_mode) == false) { + checkBoxPublishServer.setValue(true); + } + listBoxFallbackCpuMultiplier.setEditable(true); + checkBoxPublishServer.setEditable(true); + + // Masterserver always needs one network slot + if (this->headlessServerMode == true + && hasOneNetworkSlotOpen == false) { + bool anyoneConnected = false; + for (int i = 0; i < mapInfo.players; ++i) { + MutexSafeWrapper + safeMutex((publishToMasterserverThread != + NULL ? + publishToMasterserverThread->getMutexThreadObjectAccessor + () : NULL), + string(__FILE__) + "_" + intToStr(__LINE__)); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = serverInterface->getSlot(i, true); + if (slot != NULL && slot->isConnected() == true) { + anyoneConnected = true; + break; + } + } + + for (int i = 0; i < mapInfo.players; ++i) { + if (anyoneConnected == false + && listBoxControls[i].getSelectedItemIndex() != ctNetwork) { + listBoxControls[i].setSelectedItemIndex(ctNetwork); + } + } + + updateNetworkSlots(); + } + } else { + checkBoxPublishServer.setValue(false); + checkBoxPublishServer.setEditable(false); + listBoxFallbackCpuMultiplier.setEditable(false); + listBoxFallbackCpuMultiplier.setSelectedItem("1.0"); + + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface-> + setPublishEnabled(checkBoxPublishServer.getValue() == true); + } + + bool republishToMaster = + (difftime((long int) time(NULL), lastMasterserverPublishing) >= + MASTERSERVER_BROADCAST_PUBLISH_SECONDS); + + if (republishToMaster == true) { + if (checkBoxPublishServer.getValue() == true) { + needToRepublishToMasterserver = true; + lastMasterserverPublishing = time(NULL); + } + } + + bool callPublishNow = (checkBoxPublishServer.getEditable() && + checkBoxPublishServer.getValue() == true && + needToRepublishToMasterserver == true); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (callPublishNow == true) { + // give it to me baby, aha aha ... + publishToMasterserver(); + } + if (needToPublishDelayed) { + // this delay is done to make it possible to switch over maps which are not meant to be distributed + if ((difftime((long int) time(NULL), mapPublishingDelayTimer) >= + BROADCAST_MAP_DELAY_SECONDS) + || (this->headlessServerMode == true)) { + // after 5 seconds we are allowed to publish again! + needToSetChangedGameSettings = true; + lastSetChangedGameSettings = time(NULL); + // set to normal.... + needToPublishDelayed = false; + } + } + if (needToPublishDelayed == false || headlessServerMode == true) { + bool broadCastSettings = + (difftime((long int) time(NULL), lastSetChangedGameSettings) + >= BROADCAST_SETTINGS_SECONDS); + + if (headlessServerMode == true) { + // publish settings directly when we receive them + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + if (lastGameSettingsreceivedCount < + serverInterface->getGameSettingsUpdateCount()) { + needToBroadcastServerSettings = true; + lastSetChangedGameSettings = time(NULL); + lastGameSettingsreceivedCount = + serverInterface->getGameSettingsUpdateCount(); + } + } + + if (broadCastSettings == true) { + needToBroadcastServerSettings = true; + lastSetChangedGameSettings = time(NULL); + } + + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + //broadCastSettings = (difftime(time(NULL),lastSetChangedGameSettings) >= 2); + //if (broadCastSettings == true) {// reset timer here on bottom becasue used for different things + // lastSetChangedGameSettings = time(NULL); + //} + } + + if (this->headlessServerMode == true) { + lastPlayerDisconnected(); + } + + //call the chat manager + chatManager.updateNetwork(); + + //console + console.update(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (currentConnectionCount > soundConnectionCount) { + soundConnectionCount = currentConnectionCount; + static PlaySoundClip snd; + SoundRenderer::getInstance(). + playFx(snd.getSound(snd.sfxAttention)); + //switch on music again!! + Config & config = Config::getInstance(); + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + CoreData::getInstance().getMenuMusic()->setVolume(configVolume); + } + soundConnectionCount = currentConnectionCount; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (enableFactionTexturePreview == true) { + if (currentTechName_factionPreview != gameSettings.getTech() || + currentFactionName_factionPreview != + gameSettings. + getFactionTypeName(gameSettings.getThisFactionIndex())) { + + currentTechName_factionPreview = gameSettings.getTech(); + currentFactionName_factionPreview = + gameSettings. + getFactionTypeName(gameSettings.getThisFactionIndex()); + + initFactionPreview(&gameSettings); + } + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s Line: %d] took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + chrono.start(); + + if (autostart == true) { + autostart = false; + safeMutex.ReleaseLock(); + safeMutexCLI.ReleaseLock(); + if (autoStartSettings != NULL) { + + setupUIFromGameSettings(*autoStartSettings); + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + serverInterface->setGameSettings(autoStartSettings, false); + } else { + RestoreLastGameSettings(); + } + PlayNow((autoStartSettings == NULL)); + return; + } + } catch (megaglest_runtime_error & ex) { + //abort(); + //printf("1111111bbbb ex.wantStackTrace() = %d\n",ex.wantStackTrace()); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + //printf("2222222bbbb ex.wantStackTrace() = %d\n",ex.wantStackTrace()); + + showGeneralError = true; + generalErrorToShow = szBuf; + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + showGeneralError = true; + generalErrorToShow = szBuf; + } + } + + void MenuStateCustomGame::updateControllers() { + try { + bool humanPlayer = false; + + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == ctHuman) { + humanPlayer = true; + } + } + + if (humanPlayer == false) { + if (this->headlessServerMode == false) { + bool foundNewSlotForHuman = false; + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == ctClosed) { + setSlotHuman(i); + foundNewSlotForHuman = true; + break; + } + } + + if (foundNewSlotForHuman == false) { + for (int i = 0; i < mapInfo.players; ++i) { + if (listBoxControls[i].getSelectedItemIndex() == + ctClosed + || listBoxControls[i].getSelectedItemIndex() == + ctCpuEasy + || listBoxControls[i].getSelectedItemIndex() == + ctCpu + || listBoxControls[i].getSelectedItemIndex() == + ctCpuUltra + || listBoxControls[i].getSelectedItemIndex() == + ctCpuZeta) { + setSlotHuman(i); + + foundNewSlotForHuman = true; + break; + } + } + } + + if (foundNewSlotForHuman == false) { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + ConnectionSlot *slot = serverInterface->getSlot(0, true); + if (slot != NULL && slot->isConnected() == true) { + serverInterface->removeSlot(0); + } + setSlotHuman(0); + } + } + } + + for (int i = mapInfo.players; i < GameConstants::maxPlayers; ++i) { + if (listBoxControls[i].getSelectedItemIndex() != ctNetwork && + listBoxControls[i].getSelectedItemIndex() != + ctNetworkUnassigned) { + //printf("Closed A [%d] [%s]\n",i,labelPlayerNames[i].getText().c_str()); + + listBoxControls[i].setSelectedItemIndex(ctClosed); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + void MenuStateCustomGame::updateNetworkSlots() { + try { + ServerInterface *serverInterface = + NetworkManager::getInstance().getServerInterface(); + + if (hasNetworkGameSettings() == true) { + if (hasCheckedForUPNP == false) { + + if (checkBoxPublishServer.getValue() == true || + this->headlessServerMode == true) { + + hasCheckedForUPNP = true; + serverInterface->getServerSocket()->NETdiscoverUPnPDevices(); + } + } + } else { + hasCheckedForUPNP = false; + } + + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + ConnectionSlot *slot = serverInterface->getSlot(i, true); + //printf("A i = %d control type = %d slot [%p]\n",i,listBoxControls[i].getSelectedItemIndex(),slot); + + if (slot == NULL && + listBoxControls[i].getSelectedItemIndex() == ctNetwork) { + try { + serverInterface->addSlot(i); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + showGeneralError = true; + if (serverInterface->isPortBound() == false) { + generalErrorToShow = + Lang::getInstance().getString("ErrorBindingPort") + + " : " + intToStr(serverInterface->getBindPort()); + } else { + generalErrorToShow = ex.what(); + } + + // Revert network to CPU + listBoxControls[i].setSelectedItemIndex(ctCpu); + } + } + slot = serverInterface->getSlot(i, true); + if (slot != NULL) { + if ((listBoxControls[i].getSelectedItemIndex() != ctNetwork) + || (listBoxControls[i].getSelectedItemIndex() == + ctNetwork && slot->isConnected() == false + && i >= mapInfo.players)) { + if (slot->getCanAcceptConnections() == true) { + slot->setCanAcceptConnections(false); + } + if (slot->isConnected() == true) { + if (listBoxControls[i].getSelectedItemIndex() != + ctNetworkUnassigned) { + listBoxControls[i].setSelectedItemIndex + (ctNetworkUnassigned); + } + } else { + serverInterface->removeSlot(i); + + if (listBoxControls[i].getSelectedItemIndex() == + ctNetworkUnassigned) { + listBoxControls[i].setSelectedItemIndex(ctClosed); + } + } + } else if (slot->getCanAcceptConnections() == false) { + slot->setCanAcceptConnections(true); + } + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d]\nError detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + //throw megaglest_runtime_error(szBuf); + showGeneralError = true; + generalErrorToShow = szBuf; + + } + } + + void MenuStateCustomGame::updateAllResourceMultiplier() { + for (int j = 0; j < GameConstants::maxPlayers; ++j) { + updateResourceMultiplier(j); + } + } + + void MenuStateCustomGame::updateResourceMultiplier(const int index) { + //printf("Line: %d multiplier index: %d index: %d\n",__LINE__,listBoxRMultiplier[index].getSelectedItemIndex(),index); + + ControlType ct = + static_cast + (listBoxControls[index].getSelectedItemIndex()); + if (ct == ctCpuEasy || ct == ctNetworkCpuEasy) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + easyMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == + false); + } else if (ct == ctCpu || ct == ctNetworkCpu) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + normalMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == + false); + } else if (ct == ctCpuUltra || ct == ctNetworkCpuUltra) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + ultraMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == + false); + } else if (ct == ctCpuZeta || ct == ctNetworkCpuZeta) { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + megaMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == + false); + } + //if(ct == ctHuman || ct == ctNetwork || ct == ctClosed) { + else { + listBoxRMultiplier[index].setSelectedItem(floatToStr + (GameConstants:: + normalMultiplier, 1)); + listBoxRMultiplier[index].setEnabled(false); + //!!!listBoxRMultiplier[index].setEnabled(checkBoxScenario.getValue() == false); + } + + listBoxRMultiplier[index]. + setEditable(listBoxRMultiplier[index].getEnabled()); + listBoxRMultiplier[index].setVisible(ct != ctHuman && ct != ctNetwork + && ct != ctClosed); + //listBoxRMultiplier[index].setVisible(ct != ctClosed); + + //printf("Line: %d multiplier index: %d index: %d\n",__LINE__,listBoxRMultiplier[index].getSelectedItemIndex(),index); + } + } // end namespace } diff --git a/source/glest_game/menu/menu_state_graphic_info.cpp b/source/glest_game/menu/menu_state_graphic_info.cpp index b59e920be..262266f00 100644 --- a/source/glest_game/menu/menu_state_graphic_info.cpp +++ b/source/glest_game/menu/menu_state_graphic_info.cpp @@ -19,192 +19,180 @@ #include "opengl.h" #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class MenuStateGraphicInfo -// ===================================================== + // ===================================================== + // class MenuStateGraphicInfo + // ===================================================== - MenuStateGraphicInfo::MenuStateGraphicInfo (Program * program, - MainMenu * - mainMenu):MenuState (program, - mainMenu, - "info") - { - Lang & lang = Lang::getInstance (); + MenuStateGraphicInfo::MenuStateGraphicInfo(Program * program, + MainMenu * + mainMenu) :MenuState(program, + mainMenu, + "info") { + Lang & lang = Lang::getInstance(); - containerName = "GraphicInfo"; - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (650, 575, 125); + containerName = "GraphicInfo"; + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(650, 575, 125); - buttonReturn.setText (lang.getString ("Return")); + buttonReturn.setText(lang.getString("Return")); - labelInfo.registerGraphicComponent (containerName, "labelInfo"); - labelInfo.init (0, 730); + labelInfo.registerGraphicComponent(containerName, "labelInfo"); + labelInfo.init(0, 730); - labelMoreInfo.registerGraphicComponent (containerName, "labelMoreInfo"); - labelMoreInfo.init (0, 555); - labelMoreInfo.setFont (CoreData::getInstance ().getDisplayFontSmall ()); - labelMoreInfo.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + labelMoreInfo.registerGraphicComponent(containerName, "labelMoreInfo"); + labelMoreInfo.init(0, 555); + labelMoreInfo.setFont(CoreData::getInstance().getDisplayFontSmall()); + labelMoreInfo.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - labelInternalInfo.registerGraphicComponent (containerName, - "labelInternalInfo"); - labelInternalInfo.init (300, 730); - labelInternalInfo.setFont (CoreData::getInstance (). - getDisplayFontSmall ()); - labelInternalInfo.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + labelInternalInfo.registerGraphicComponent(containerName, + "labelInternalInfo"); + labelInternalInfo.init(300, 730); + labelInternalInfo.setFont(CoreData::getInstance(). + getDisplayFontSmall()); + labelInternalInfo.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - GraphicComponent::applyAllCustomProperties (containerName); + GraphicComponent::applyAllCustomProperties(containerName); - Renderer & renderer = Renderer::getInstance (); + Renderer & renderer = Renderer::getInstance(); - string glInfo = renderer.getGlInfo (); - string glMoreInfo = renderer.getGlMoreInfo (); - labelInfo.setText (glInfo); - labelMoreInfo.setText (glMoreInfo); + string glInfo = renderer.getGlInfo(); + string glMoreInfo = renderer.getGlMoreInfo(); + labelInfo.setText(glInfo); + labelMoreInfo.setText(glMoreInfo); - string strInternalInfo = ""; - strInternalInfo += "VBOSupported: " + boolToStr (getVBOSupported ()); - if (getenv ("MEGAGLEST_FONT") != NULL) - { - char *tryFont = getenv ("MEGAGLEST_FONT"); - strInternalInfo += "\nMEGAGLEST_FONT: " + string (tryFont); - } - strInternalInfo += - "\nforceLegacyFonts: " + boolToStr (Font::forceLegacyFonts); - strInternalInfo += - "\nrenderText3DEnabled: " + boolToStr (Renderer::renderText3DEnabled); - strInternalInfo += - "\nuseTextureCompression: " + - boolToStr (Texture::useTextureCompression); - strInternalInfo += - "\nfontIsRightToLeft: " + boolToStr (Font::fontIsRightToLeft); - strInternalInfo += - "\nscaleFontValue: " + floatToStr (Font::scaleFontValue); - strInternalInfo += - "\nscaleFontValueCenterHFactor: " + - floatToStr (Font::scaleFontValueCenterHFactor); - strInternalInfo += "\nlangHeightText: " + Font::langHeightText; - strInternalInfo += - "\nAllowAltEnterFullscreenToggle: " + - boolToStr (Window::getAllowAltEnterFullscreenToggle ()); - strInternalInfo += - "\nTryVSynch: " + boolToStr (Window::getTryVSynch ()); - strInternalInfo += - "\nVERBOSE_MODE_ENABLED: " + - boolToStr (SystemFlags::VERBOSE_MODE_ENABLED); - labelInternalInfo.setText (strInternalInfo); - } + string strInternalInfo = ""; + strInternalInfo += "VBOSupported: " + boolToStr(getVBOSupported()); + if (getenv("MEGAGLEST_FONT") != NULL) { + char *tryFont = getenv("MEGAGLEST_FONT"); + strInternalInfo += "\nMEGAGLEST_FONT: " + string(tryFont); + } + strInternalInfo += + "\nforceLegacyFonts: " + boolToStr(Font::forceLegacyFonts); + strInternalInfo += + "\nrenderText3DEnabled: " + boolToStr(Renderer::renderText3DEnabled); + strInternalInfo += + "\nuseTextureCompression: " + + boolToStr(Texture::useTextureCompression); + strInternalInfo += + "\nfontIsRightToLeft: " + boolToStr(Font::fontIsRightToLeft); + strInternalInfo += + "\nscaleFontValue: " + floatToStr(Font::scaleFontValue); + strInternalInfo += + "\nscaleFontValueCenterHFactor: " + + floatToStr(Font::scaleFontValueCenterHFactor); + strInternalInfo += "\nlangHeightText: " + Font::langHeightText; + strInternalInfo += + "\nAllowAltEnterFullscreenToggle: " + + boolToStr(Window::getAllowAltEnterFullscreenToggle()); + strInternalInfo += + "\nTryVSynch: " + boolToStr(Window::getTryVSynch()); + strInternalInfo += + "\nVERBOSE_MODE_ENABLED: " + + boolToStr(SystemFlags::VERBOSE_MODE_ENABLED); + labelInternalInfo.setText(strInternalInfo); + } - void MenuStateGraphicInfo::reloadUI () - { - Lang & lang = Lang::getInstance (); + void MenuStateGraphicInfo::reloadUI() { + Lang & lang = Lang::getInstance(); - console.resetFonts (); - buttonReturn.setText (lang.getString ("Return")); + console.resetFonts(); + buttonReturn.setText(lang.getString("Return")); - labelMoreInfo.setFont (CoreData::getInstance ().getDisplayFontSmall ()); - labelMoreInfo.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + labelMoreInfo.setFont(CoreData::getInstance().getDisplayFontSmall()); + labelMoreInfo.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - labelInternalInfo.setFont (CoreData::getInstance (). - getDisplayFontSmall ()); - labelInternalInfo.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + labelInternalInfo.setFont(CoreData::getInstance(). + getDisplayFontSmall()); + labelInternalInfo.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - Renderer & renderer = Renderer::getInstance (); + Renderer & renderer = Renderer::getInstance(); - string glInfo = renderer.getGlInfo (); - string glMoreInfo = renderer.getGlMoreInfo (); - labelInfo.setText (glInfo); - labelMoreInfo.setText (glMoreInfo); + string glInfo = renderer.getGlInfo(); + string glMoreInfo = renderer.getGlMoreInfo(); + labelInfo.setText(glInfo); + labelMoreInfo.setText(glMoreInfo); - string strInternalInfo = ""; - strInternalInfo += "VBOSupported: " + boolToStr (getVBOSupported ()); - if (getenv ("MEGAGLEST_FONT") != NULL) - { - char *tryFont = getenv ("MEGAGLEST_FONT"); - strInternalInfo += "\nMEGAGLEST_FONT: " + string (tryFont); - } - strInternalInfo += - "\nforceLegacyFonts: " + boolToStr (Font::forceLegacyFonts); - strInternalInfo += - "\nrenderText3DEnabled: " + boolToStr (Renderer::renderText3DEnabled); - strInternalInfo += - "\nuseTextureCompression: " + - boolToStr (Texture::useTextureCompression); - strInternalInfo += - "\nfontIsRightToLeft: " + boolToStr (Font::fontIsRightToLeft); - strInternalInfo += - "\nscaleFontValue: " + floatToStr (Font::scaleFontValue); - strInternalInfo += - "\nscaleFontValueCenterHFactor: " + - floatToStr (Font::scaleFontValueCenterHFactor); - strInternalInfo += "\nlangHeightText: " + Font::langHeightText; - strInternalInfo += - "\nAllowAltEnterFullscreenToggle: " + - boolToStr (Window::getAllowAltEnterFullscreenToggle ()); - strInternalInfo += - "\nTryVSynch: " + boolToStr (Window::getTryVSynch ()); - strInternalInfo += - "\nVERBOSE_MODE_ENABLED: " + - boolToStr (SystemFlags::VERBOSE_MODE_ENABLED); - labelInternalInfo.setText (strInternalInfo); + string strInternalInfo = ""; + strInternalInfo += "VBOSupported: " + boolToStr(getVBOSupported()); + if (getenv("MEGAGLEST_FONT") != NULL) { + char *tryFont = getenv("MEGAGLEST_FONT"); + strInternalInfo += "\nMEGAGLEST_FONT: " + string(tryFont); + } + strInternalInfo += + "\nforceLegacyFonts: " + boolToStr(Font::forceLegacyFonts); + strInternalInfo += + "\nrenderText3DEnabled: " + boolToStr(Renderer::renderText3DEnabled); + strInternalInfo += + "\nuseTextureCompression: " + + boolToStr(Texture::useTextureCompression); + strInternalInfo += + "\nfontIsRightToLeft: " + boolToStr(Font::fontIsRightToLeft); + strInternalInfo += + "\nscaleFontValue: " + floatToStr(Font::scaleFontValue); + strInternalInfo += + "\nscaleFontValueCenterHFactor: " + + floatToStr(Font::scaleFontValueCenterHFactor); + strInternalInfo += "\nlangHeightText: " + Font::langHeightText; + strInternalInfo += + "\nAllowAltEnterFullscreenToggle: " + + boolToStr(Window::getAllowAltEnterFullscreenToggle()); + strInternalInfo += + "\nTryVSynch: " + boolToStr(Window::getTryVSynch()); + strInternalInfo += + "\nVERBOSE_MODE_ENABLED: " + + boolToStr(SystemFlags::VERBOSE_MODE_ENABLED); + labelInternalInfo.setText(strInternalInfo); - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } - void MenuStateGraphicInfo::mouseClick (int x, int y, - MouseButton mouseButton) - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); + void MenuStateGraphicInfo::mouseClick(int x, int y, + MouseButton mouseButton) { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); - if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu)); - } - } + if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); + } + } - void MenuStateGraphicInfo::mouseMove (int x, int y, const MouseState * ms) - { - buttonReturn.mouseMove (x, y); - } + void MenuStateGraphicInfo::mouseMove(int x, int y, const MouseState * ms) { + buttonReturn.mouseMove(x, y); + } - void MenuStateGraphicInfo::render () - { + void MenuStateGraphicInfo::render() { - Renderer & renderer = Renderer::getInstance (); - //Lang &lang= Lang::getInstance(); + Renderer & renderer = Renderer::getInstance(); + //Lang &lang= Lang::getInstance(); - renderer.renderButton (&buttonReturn); - renderer.renderLabel (&labelInfo); - renderer.renderLabel (&labelInternalInfo); - renderer.renderLabel (&labelMoreInfo); + renderer.renderButton(&buttonReturn); + renderer.renderLabel(&labelInfo); + renderer.renderLabel(&labelInternalInfo); + renderer.renderLabel(&labelMoreInfo); - renderer.renderConsole (&console); - } + renderer.renderConsole(&console); + } - void MenuStateGraphicInfo::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //if(key == configKeys.getCharKey("SaveGUILayout")) { - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } - } + void MenuStateGraphicInfo::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(key == configKeys.getCharKey("SaveGUILayout")) { + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + } - } + } } //end namespace diff --git a/source/glest_game/menu/menu_state_graphic_info.h b/source/glest_game/menu/menu_state_graphic_info.h index 03bff0048..8a3f2430a 100644 --- a/source/glest_game/menu/menu_state_graphic_info.h +++ b/source/glest_game/menu/menu_state_graphic_info.h @@ -16,54 +16,52 @@ # include "leak_dumper.h" using namespace - Shared::Graphics::Gl; +Shared::Graphics::Gl; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// =============================== -// class MenuStateGraphicInfo -// =============================== + // =============================== + // class MenuStateGraphicInfo + // =============================== - class - MenuStateGraphicInfo: - public - MenuState - { - private: - GraphicButton - buttonReturn; - GraphicLabel - labelInfo; - GraphicLabel - labelMoreInfo; + class + MenuStateGraphicInfo : + public + MenuState { + private: + GraphicButton + buttonReturn; + GraphicLabel + labelInfo; + GraphicLabel + labelMoreInfo; - GraphicLabel - labelInternalInfo; + GraphicLabel + labelInternalInfo; - //string glInfo; - //string glMoreInfo; + //string glInfo; + //string glMoreInfo; - public: - MenuStateGraphicInfo (Program * program, MainMenu * mainMenu); + public: + MenuStateGraphicInfo(Program * program, MainMenu * mainMenu); - void - mouseClick (int x, int y, MouseButton mouseButton); - void - mouseMove (int x, int y, const MouseState * mouseState); - void - render (); - virtual void - keyDown (SDL_KeyboardEvent key); + void + mouseClick(int x, int y, MouseButton mouseButton); + void + mouseMove(int x, int y, const MouseState * mouseState); + void + render(); + virtual void + keyDown(SDL_KeyboardEvent key); - virtual void - reloadUI (); - }; + virtual void + reloadUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_join_game.cpp b/source/glest_game/menu/menu_state_join_game.cpp index a1d098022..5a83cc4e1 100644 --- a/source/glest_game/menu/menu_state_join_game.cpp +++ b/source/glest_game/menu/menu_state_join_game.cpp @@ -36,1093 +36,951 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using namespace::Shared::Util; + using namespace::Shared::Util; -// =============================== -// class MenuStateJoinGame -// =============================== + // =============================== + // class MenuStateJoinGame + // =============================== - const int MenuStateJoinGame::newServerIndex = 0; - const int MenuStateJoinGame::newPrevServerIndex = 1; - const int MenuStateJoinGame::foundServersIndex = 2; + const int MenuStateJoinGame::newServerIndex = 0; + const int MenuStateJoinGame::newPrevServerIndex = 1; + const int MenuStateJoinGame::foundServersIndex = 2; - const string MenuStateJoinGame::serverFileName = "servers.ini"; + const string MenuStateJoinGame::serverFileName = "servers.ini"; - MenuStateJoinGame::MenuStateJoinGame (Program * program, - MainMenu * mainMenu, - bool * - autoFindHost):MenuState (program, - mainMenu, - "join-game") - { - CommonInit (false, Ip (), -1); + MenuStateJoinGame::MenuStateJoinGame(Program * program, + MainMenu * mainMenu, + bool * + autoFindHost) :MenuState(program, + mainMenu, + "join-game") { + CommonInit(false, Ip(), -1); - if (autoFindHost != NULL && *autoFindHost == true) - { -//if(clientInterface->isConnected() == false) { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (autoFindHost != NULL && *autoFindHost == true) { + //if(clientInterface->isConnected() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - buttonAutoFindServers.setEnabled (false); - buttonConnect.setEnabled (false); + buttonAutoFindServers.setEnabled(false); + buttonConnect.setEnabled(false); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface *clientInterface = - networkManager.getClientInterface (); - clientInterface->discoverServers (this); -//} - } - } - MenuStateJoinGame::MenuStateJoinGame (Program * program, - MainMenu * mainMenu, bool connect, - Ip serverIp, - int - portNumberOverride):MenuState - (program, mainMenu, "join-game") - { - CommonInit (connect, serverIp, portNumberOverride); - } + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface *clientInterface = + networkManager.getClientInterface(); + clientInterface->discoverServers(this); + //} + } + } + MenuStateJoinGame::MenuStateJoinGame(Program * program, + MainMenu * mainMenu, bool connect, + Ip serverIp, + int + portNumberOverride) :MenuState + (program, mainMenu, "join-game") { + CommonInit(connect, serverIp, portNumberOverride); + } - void MenuStateJoinGame::CommonInit (bool connect, Ip serverIp, - int portNumberOverride) - { - containerName = "JoinGame"; - abortAutoFind = false; - autoConnectToServer = false; - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - networkManager.end (); - networkManager.init (nrClient); + void MenuStateJoinGame::CommonInit(bool connect, Ip serverIp, + int portNumberOverride) { + containerName = "JoinGame"; + abortAutoFind = false; + autoConnectToServer = false; + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + networkManager.end(); + networkManager.init(nrClient); - serversSavedFile = serverFileName; + serversSavedFile = serverFileName; - string serverListPath = config.getString ("ServerListPath", ""); - if (serverListPath != "") - { - endPathWithSlash (serverListPath); - } + string serverListPath = config.getString("ServerListPath", ""); + if (serverListPath != "") { + endPathWithSlash(serverListPath); + } - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - serversSavedFile = userData + serversSavedFile; + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + serversSavedFile = userData + serversSavedFile; - if (fileExists (serversSavedFile) == true) - { - servers.load (serversSavedFile); - } - else if (fileExists (serverListPath + serverFileName) == true) - { - servers.load (serverListPath + serverFileName); - } - else if (fileExists (Properties::getApplicationPath () + serverFileName) - == true) - { - servers.load (Properties::getApplicationPath () + serverFileName); - } - else if (fileExists (serverFileName) == true) - { - servers.load (serverFileName); - } + if (fileExists(serversSavedFile) == true) { + servers.load(serversSavedFile); + } else if (fileExists(serverListPath + serverFileName) == true) { + servers.load(serverListPath + serverFileName); + } else if (fileExists(Properties::getApplicationPath() + serverFileName) + == true) { + servers.load(Properties::getApplicationPath() + serverFileName); + } else if (fileExists(serverFileName) == true) { + servers.load(serverFileName); + } -//buttons - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (250, 300, 150); - buttonReturn.setText (lang.getString ("Return")); + //buttons + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(250, 300, 150); + buttonReturn.setText(lang.getString("Return")); - buttonConnect.registerGraphicComponent (containerName, "buttonConnect"); - buttonConnect.init (425, 300, 150); - buttonConnect.setText (lang.getString ("Connect")); + buttonConnect.registerGraphicComponent(containerName, "buttonConnect"); + buttonConnect.init(425, 300, 150); + buttonConnect.setText(lang.getString("Connect")); - buttonCreateGame.registerGraphicComponent (containerName, - "buttonCreateGame"); - buttonCreateGame.init (600, 300, 150); - buttonCreateGame.setText (lang.getString ("HostGame")); + buttonCreateGame.registerGraphicComponent(containerName, + "buttonCreateGame"); + buttonCreateGame.init(600, 300, 150); + buttonCreateGame.setText(lang.getString("HostGame")); - buttonAutoFindServers.registerGraphicComponent (containerName, - "buttonAutoFindServers"); - buttonAutoFindServers.init (360, 250, 280); - buttonAutoFindServers.setText (lang.getString ("FindLANGames")); - buttonAutoFindServers.setEnabled (true); + buttonAutoFindServers.registerGraphicComponent(containerName, + "buttonAutoFindServers"); + buttonAutoFindServers.init(360, 250, 280); + buttonAutoFindServers.setText(lang.getString("FindLANGames")); + buttonAutoFindServers.setEnabled(true); - int labelXleft = 300; - int labelXright = 480; + int labelXleft = 300; + int labelXright = 480; -//server type label - labelServerType.registerGraphicComponent (containerName, - "labelServerType"); - labelServerType.init (labelXleft, 490); - labelServerType.setText (lang.getString ("ServerType")); + //server type label + labelServerType.registerGraphicComponent(containerName, + "labelServerType"); + labelServerType.init(labelXleft, 490); + labelServerType.setText(lang.getString("ServerType")); -//server type list box - listBoxServerType.registerGraphicComponent (containerName, - "listBoxServerType"); - listBoxServerType.init (labelXright, 490, 210); - listBoxServerType.pushBackItem (lang.getString ("ServerTypeNew")); - listBoxServerType.pushBackItem (lang.getString ("ServerTypePrevious")); - listBoxServerType.pushBackItem (lang.getString ("ServerTypeFound")); + //server type list box + listBoxServerType.registerGraphicComponent(containerName, + "listBoxServerType"); + listBoxServerType.init(labelXright, 490, 210); + listBoxServerType.pushBackItem(lang.getString("ServerTypeNew")); + listBoxServerType.pushBackItem(lang.getString("ServerTypePrevious")); + listBoxServerType.pushBackItem(lang.getString("ServerTypeFound")); -//server label - labelServer.registerGraphicComponent (containerName, "labelServer"); - labelServer.init (labelXleft, 460); - labelServer.setText (lang.getString ("Server")); + //server label + labelServer.registerGraphicComponent(containerName, "labelServer"); + labelServer.init(labelXleft, 460); + labelServer.setText(lang.getString("Server")); -//server listbox - listBoxServers.registerGraphicComponent (containerName, - "listBoxServers"); - listBoxServers.init (labelXright, 460, 210); - for (int i = 0; i < servers.getPropertyCount (); ++i) - { - listBoxServers.pushBackItem (servers.getKey (i)); - } + //server listbox + listBoxServers.registerGraphicComponent(containerName, + "listBoxServers"); + listBoxServers.init(labelXright, 460, 210); + for (int i = 0; i < servers.getPropertyCount(); ++i) { + listBoxServers.pushBackItem(servers.getKey(i)); + } -// found servers listbox - listBoxFoundServers.registerGraphicComponent (containerName, - "listBoxFoundServers"); - listBoxFoundServers.init (labelXright, 460, 210); + // found servers listbox + listBoxFoundServers.registerGraphicComponent(containerName, + "listBoxFoundServers"); + listBoxFoundServers.init(labelXright, 460, 210); -//server ip - labelServerIp.registerGraphicComponent (containerName, "labelServerIp"); - labelServerIp.setEditable (true); - labelServerIp.setMaxEditWidth (26); - labelServerIp.setMaxEditRenderWidth (210); - labelServerIp.init (labelXright, 460); + //server ip + labelServerIp.registerGraphicComponent(containerName, "labelServerIp"); + labelServerIp.setEditable(true); + labelServerIp.setMaxEditWidth(26); + labelServerIp.setMaxEditRenderWidth(210); + labelServerIp.init(labelXright, 460); -// server port - labelServerPortLabel.registerGraphicComponent (containerName, - "labelServerPortLabel"); - labelServerPortLabel.init (labelXleft, 430); - labelServerPortLabel.setText (lang.getString ("ServerPort")); + // server port + labelServerPortLabel.registerGraphicComponent(containerName, + "labelServerPortLabel"); + labelServerPortLabel.init(labelXleft, 430); + labelServerPortLabel.setText(lang.getString("ServerPort")); - labelServerPort.registerGraphicComponent (containerName, - "labelServerPort"); - labelServerPort.init (labelXright, 430); + labelServerPort.registerGraphicComponent(containerName, + "labelServerPort"); + labelServerPort.init(labelXright, 430); - string host = labelServerIp.getText (); - int portNumber = config.getInt ("PortServer", - intToStr (GameConstants::serverPort). - c_str ()); - std::vector < std::string > hostPartsList; - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - portNumber = strToInt (hostPartsList[1]); - } + string host = labelServerIp.getText(); + int portNumber = config.getInt("PortServer", + intToStr(GameConstants::serverPort). + c_str()); + std::vector < std::string > hostPartsList; + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + portNumber = strToInt(hostPartsList[1]); + } - string port = " (" + intToStr (portNumber) + ")"; - labelServerPort.setText (port); + string port = " (" + intToStr(portNumber) + ")"; + labelServerPort.setText(port); - labelStatus.registerGraphicComponent (containerName, "labelStatus"); - labelStatus.init (labelXleft, 400); - labelStatus.setText (""); + labelStatus.registerGraphicComponent(containerName, "labelStatus"); + labelStatus.init(labelXleft, 400); + labelStatus.setText(""); - labelInfo.registerGraphicComponent (containerName, "labelInfo"); - labelInfo.init (labelXleft, 370); - labelInfo.setText (""); + labelInfo.registerGraphicComponent(containerName, "labelInfo"); + labelInfo.init(labelXleft, 370); + labelInfo.setText(""); - connected = false; - playerIndex = -1; + connected = false; + playerIndex = -1; -//server ip - if (connect == true) - { - string hostIP = serverIp.getString (); - if (portNumberOverride > 0) - { - hostIP += ":" + intToStr (portNumberOverride); - } + //server ip + if (connect == true) { + string hostIP = serverIp.getString(); + if (portNumberOverride > 0) { + hostIP += ":" + intToStr(portNumberOverride); + } - labelServerIp.setText (hostIP + "_"); + labelServerIp.setText(hostIP + "_"); - autoConnectToServer = true; - } - else - { - string hostIP = config.getString ("ServerIp"); - if (portNumberOverride > 0) - { - hostIP += ":" + intToStr (portNumberOverride); - } + autoConnectToServer = true; + } else { + string hostIP = config.getString("ServerIp"); + if (portNumberOverride > 0) { + hostIP += ":" + intToStr(portNumberOverride); + } - labelServerIp.setText (hostIP + "_"); - } + labelServerIp.setText(hostIP + "_"); + } - host = labelServerIp.getText (); - portNumber = - config.getInt ("PortServer", - intToStr (GameConstants::serverPort).c_str ()); - hostPartsList.clear (); - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - portNumber = strToInt (hostPartsList[1]); - } + host = labelServerIp.getText(); + portNumber = + config.getInt("PortServer", + intToStr(GameConstants::serverPort).c_str()); + hostPartsList.clear(); + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + portNumber = strToInt(hostPartsList[1]); + } - port = " (" + intToStr (portNumber) + ")"; - labelServerPort.setText (port); + port = " (" + intToStr(portNumber) + ")"; + labelServerPort.setText(port); - GraphicComponent::applyAllCustomProperties (containerName); + GraphicComponent::applyAllCustomProperties(containerName); - chatManager.init (&console, -1); - } + chatManager.init(&console, -1); + } - void MenuStateJoinGame::reloadUI () - { - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); + void MenuStateJoinGame::reloadUI() { + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); - console.resetFonts (); + console.resetFonts(); - buttonReturn.setText (lang.getString ("Return")); - buttonConnect.setText (lang.getString ("Connect")); - buttonCreateGame.setText (lang.getString ("HostGame")); - buttonAutoFindServers.setText (lang.getString ("FindLANGames")); - labelServerType.setText (lang.getString ("ServerType")); + buttonReturn.setText(lang.getString("Return")); + buttonConnect.setText(lang.getString("Connect")); + buttonCreateGame.setText(lang.getString("HostGame")); + buttonAutoFindServers.setText(lang.getString("FindLANGames")); + labelServerType.setText(lang.getString("ServerType")); - std::vector < string > listboxData; - listboxData.push_back (lang.getString ("ServerTypeNew")); - listboxData.push_back (lang.getString ("ServerTypePrevious")); - listboxData.push_back (lang.getString ("ServerTypeFound")); - listBoxServerType.setItems (listboxData); + std::vector < string > listboxData; + listboxData.push_back(lang.getString("ServerTypeNew")); + listboxData.push_back(lang.getString("ServerTypePrevious")); + listboxData.push_back(lang.getString("ServerTypeFound")); + listBoxServerType.setItems(listboxData); - labelServer.setText (lang.getString ("Server")); + labelServer.setText(lang.getString("Server")); - labelServerPortLabel.setText (lang.getString ("ServerPort")); + labelServerPortLabel.setText(lang.getString("ServerPort")); - string host = labelServerIp.getText (); - int portNumber = config.getInt ("PortServer", - intToStr (GameConstants::serverPort). - c_str ()); - std::vector < std::string > hostPartsList; - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - portNumber = strToInt (hostPartsList[1]); - } + string host = labelServerIp.getText(); + int portNumber = config.getInt("PortServer", + intToStr(GameConstants::serverPort). + c_str()); + std::vector < std::string > hostPartsList; + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + portNumber = strToInt(hostPartsList[1]); + } - string port = " (" + intToStr (portNumber) + ")"; - labelServerPort.setText (port); + string port = " (" + intToStr(portNumber) + ")"; + labelServerPort.setText(port); - chatManager.init (&console, -1); + chatManager.init(&console, -1); - GraphicComponent::reloadFontsForRegisterGraphicComponents - (containerName); - } + GraphicComponent::reloadFontsForRegisterGraphicComponents + (containerName); + } - MenuStateJoinGame::~MenuStateJoinGame () - { - abortAutoFind = true; + MenuStateJoinGame::~MenuStateJoinGame() { + abortAutoFind = true; #ifdef DEBUG - PRINT_DEBUG ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); + PRINT_DEBUG("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); #endif - } + } - void MenuStateJoinGame::DiscoveredServers (std::vector < string > - serverList) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + void MenuStateJoinGame::DiscoveredServers(std::vector < string > + serverList) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (abortAutoFind == true) - { - return; - } -// Testing multi-server -//serverList.push_back("test1"); -//serverList.push_back("test2"); -// + if (abortAutoFind == true) { + return; + } + // Testing multi-server + //serverList.push_back("test1"); + //serverList.push_back("test2"); + // #ifdef DEBUG - PRINT_DEBUG ("In [%s::%s Line: %d] serverList.size() = %d\n", __FILE__, - __FUNCTION__, __LINE__, (int) serverList.size ()); + PRINT_DEBUG("In [%s::%s Line: %d] serverList.size() = %d\n", __FILE__, + __FUNCTION__, __LINE__, (int) serverList.size()); #endif - autoConnectToServer = false; - buttonAutoFindServers.setEnabled (true); - buttonConnect.setEnabled (true); - if (serverList.empty () == false) - { - Config & config = Config::getInstance (); - string bestIPMatch = ""; - int serverGamePort = config.getInt ("PortServer", - intToStr (GameConstants:: - serverPort).c_str ()); - std::vector < std::string > localIPList = - Socket::getLocalIPAddressList (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - for (int idx = 0; idx < (int) serverList.size (); idx++) - { - - vector < string > paramPartPortsTokens; - Tokenize (serverList[idx], paramPartPortsTokens, ":"); - if (paramPartPortsTokens.size () >= 2 - && paramPartPortsTokens[1].length () > 0) - { - serverGamePort = strToInt (paramPartPortsTokens[1]); - } - - bestIPMatch = serverList[idx]; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] bestIPMatch = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - bestIPMatch.c_str ()); - if (localIPList.empty () == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - break; - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] bestIPMatch = [%s] localIPList[0] = [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - bestIPMatch.c_str (), - localIPList[0].c_str ()); - if (strncmp (localIPList[0].c_str (), serverList[idx].c_str (), 4) - == 0) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, - __LINE__); - break; - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (bestIPMatch != "") - { - bestIPMatch += ":" + intToStr (serverGamePort); - } - labelServerIp.setText (bestIPMatch); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (serverList.size () > 1) - { - listBoxServerType.setSelectedItemIndex (MenuStateJoinGame:: - foundServersIndex); - listBoxFoundServers.setItems (serverList); - } - else - { - autoConnectToServer = true; - } - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - void MenuStateJoinGame::mouseClick (int x, int y, MouseButton mouseButton) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] START\n", __FILE__, - __FUNCTION__); - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - ClientInterface *clientInterface = networkManager.getClientInterface (); - - if (clientInterface->isConnected () == false) - { -//server type - if (listBoxServerType.mouseClick (x, y)) - { - if (!listBoxServers.getText ().empty ()) - { - labelServerIp. - setText (servers.getString (listBoxServers.getText ()) + "_"); - } - } -//server list - else if (listBoxServerType.getSelectedItemIndex () == - newPrevServerIndex) - { - if (listBoxServers.mouseClick (x, y)) - { - labelServerIp. - setText (servers.getString (listBoxServers.getText ()) + "_"); - } - } - else if (listBoxServerType.getSelectedItemIndex () == - foundServersIndex) - { - if (listBoxFoundServers.mouseClick (x, y)) - { - labelServerIp.setText (listBoxFoundServers.getText ()); - } - } - - string host = labelServerIp.getText (); - Config & config = Config::getInstance (); - int portNumber = config.getInt ("PortServer", - intToStr (GameConstants::serverPort). - c_str ()); - std::vector < std::string > hostPartsList; - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - portNumber = strToInt (hostPartsList[1]); - } - - string port = " (" + intToStr (portNumber) + ")"; - labelServerPort.setText (port); - - } - -//return - if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - - clientInterface->stopServerDiscovery (); - - if (clientInterface->getSocket () != NULL) - { -//if(clientInterface->isConnected() == true) { -// string sQuitText = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()) + " has chosen to leave the game!"; -// clientInterface->sendTextMessage(sQuitText,-1); -//} - clientInterface->close (); - } - abortAutoFind = true; - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - return; - } - -//connect - else if (buttonConnect.mouseClick (x, y) - && buttonConnect.getEnabled () == true) - { - ClientInterface *clientInterface = - networkManager.getClientInterface (); - - soundRenderer.playFx (coreData.getClickSoundB ()); - labelInfo.setText (""); - - if (clientInterface->isConnected ()) - { - clientInterface->reset (); - } - else - { - if (connectToServer () == true) - { - return; - } - } - } - else if (buttonCreateGame.mouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundB ()); - - clientInterface->stopServerDiscovery (); - if (clientInterface->getSocket () != NULL) - { - clientInterface->close (); - } - abortAutoFind = true; - mainMenu->setState (new - MenuStateCustomGame (program, mainMenu, true, - pLanGame)); - return; - } - - else if (buttonAutoFindServers.mouseClick (x, y) - && buttonAutoFindServers.getEnabled () == true) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - ClientInterface *clientInterface = - networkManager.getClientInterface (); - soundRenderer.playFx (coreData.getClickSoundB ()); - -// Triggers a thread which calls back into MenuStateJoinGame::DiscoveredServers -// with the results - if (clientInterface->isConnected () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - buttonAutoFindServers.setEnabled (false); - buttonConnect.setEnabled (false); - clientInterface->discoverServers (this); - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] END\n", __FILE__, - __FUNCTION__); - } - - void MenuStateJoinGame::mouseMove (int x, int y, const MouseState * ms) - { - buttonReturn.mouseMove (x, y); - buttonConnect.mouseMove (x, y); - buttonAutoFindServers.mouseMove (x, y); - buttonCreateGame.mouseMove (x, y); - listBoxServerType.mouseMove (x, y); - -//hide-show options depending on the selection - if (listBoxServers.getSelectedItemIndex () == newServerIndex) - { - labelServerIp.mouseMove (x, y); - } - else if (listBoxServers.getSelectedItemIndex () == newPrevServerIndex) - { - listBoxServers.mouseMove (x, y); - } - else - { - listBoxFoundServers.mouseMove (x, y); - } - } - - void MenuStateJoinGame::render () - { - Renderer & renderer = Renderer::getInstance (); - - renderer.renderButton (&buttonReturn); - renderer.renderLabel (&labelServer); - renderer.renderLabel (&labelServerType); - renderer.renderLabel (&labelStatus); - renderer.renderLabel (&labelInfo); - renderer.renderLabel (&labelServerPort); - renderer.renderLabel (&labelServerPortLabel); - renderer.renderButton (&buttonConnect); - renderer.renderButton (&buttonCreateGame); - - renderer.renderButton (&buttonAutoFindServers); - renderer.renderListBox (&listBoxServerType); - - if (listBoxServerType.getSelectedItemIndex () == newServerIndex) - { - renderer.renderLabel (&labelServerIp); - } - else if (listBoxServerType.getSelectedItemIndex () == - newPrevServerIndex) - { - renderer.renderListBox (&listBoxServers); - } - else - { - renderer.renderListBox (&listBoxFoundServers); - } - - renderer.renderChatManager (&chatManager); - renderer.renderConsole (&console); - - if (program != NULL) - program->renderProgramMsgBox (); - } - - void MenuStateJoinGame::update () - { - ClientInterface *clientInterface = - NetworkManager::getInstance ().getClientInterface (); - Lang & lang = Lang::getInstance (); - -//update status label - if (clientInterface->isConnected ()) - { - buttonConnect.setText (lang.getString ("Disconnect")); - - if (clientInterface->getAllowDownloadDataSynch () == false) - { - string label = lang.getString ("ConnectedToServer"); - - if (!clientInterface->getServerName ().empty ()) - { - label = label + " " + clientInterface->getServerName (); - } - - if (clientInterface->getAllowGameDataSynchCheck () == true && - clientInterface->getNetworkGameDataSynchCheckOk () == false) - { - label = label + " - warning synch mismatch for:"; - if (clientInterface->getNetworkGameDataSynchCheckOkMap () == - false) - { - label = label + " map"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTile () == - false) - { - label = label + " tile"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTech () == - false) - { - label = label + " techtree"; - } - } - else if (clientInterface->getAllowGameDataSynchCheck () == true) - { - label += " - data synch is ok"; - } - labelStatus.setText (label); - } - else - { - string label = lang.getString ("ConnectedToServer"); - - if (!clientInterface->getServerName ().empty ()) - { - label = label + " " + clientInterface->getServerName (); - } - - if (clientInterface->getAllowGameDataSynchCheck () == true && - clientInterface->getNetworkGameDataSynchCheckOk () == false) - { - label = label + " - waiting to synch:"; - if (clientInterface->getNetworkGameDataSynchCheckOkMap () == - false) - { - label = label + " map"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTile () == - false) - { - label = label + " tile"; - } - if (clientInterface->getNetworkGameDataSynchCheckOkTech () == - false) - { - label = label + " techtree"; - } - } - else if (clientInterface->getAllowGameDataSynchCheck () == true) - { - label += " - data synch is ok"; - } - - labelStatus.setText (label); - } - } - else - { - buttonConnect.setText (lang.getString ("Connect")); - string connectedStatus = lang.getString ("NotConnected"); - if (buttonAutoFindServers.getEnabled () == false) - { - connectedStatus += " - searching for servers, please wait..."; - } - labelStatus.setText (connectedStatus); - labelInfo.setText (""); - } - -//process network messages - if (clientInterface->isConnected ()) - { -//update lobby - clientInterface->updateLobby (); - - clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface != NULL && clientInterface->isConnected ()) - { -//call the chat manager - chatManager.updateNetwork (); - -//console - console.update (); - -//intro - if (clientInterface->getIntroDone ()) - { - labelInfo.setText (lang.getString ("WaitingHost")); - - string host = labelServerIp.getText (); - std::vector < std::string > hostPartsList; - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - } - string saveHost = Ip (host).getString (); - if (hostPartsList.size () > 1) - { - saveHost += ":" + hostPartsList[1]; - } - - servers.setString (clientInterface->getServerName (), saveHost); - } - -//launch - if (clientInterface->getLaunchGame ()) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] clientInterface->getLaunchGame() - A\n", - __FILE__, __FUNCTION__); - - servers.save (serversSavedFile); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] clientInterface->getLaunchGame() - B\n", - __FILE__, __FUNCTION__); - - abortAutoFind = true; - clientInterface->stopServerDiscovery (); - program->setState (new - Game (program, - clientInterface->getGameSettings (), - false)); - return; - } - } - } - else if (autoConnectToServer == true) - { - autoConnectToServer = false; - if (connectToServer () == true) - { - return; - } - } - - if (clientInterface != NULL && clientInterface->getLaunchGame ()) - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] clientInterface->getLaunchGame() - D\n", - __FILE__, __FUNCTION__); - } - - bool MenuStateJoinGame::textInput (std::string text) - { - if (chatManager.getEditEnabled () == true) - { - return chatManager.textInput (text); - } - return false; - } - - void MenuStateJoinGame::keyDown (SDL_KeyboardEvent key) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c][%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - - ClientInterface *clientInterface = - NetworkManager::getInstance ().getClientInterface (); - if (clientInterface->isConnected () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - string text = labelServerIp.getText (); - if (isKeyPressed (SDLK_BACKSPACE, key) == true && text.length () > 0) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - size_t found = text.find_last_of ("_"); - if (found == string::npos) - { - text.erase (text.end () - 1); - } - else - { - if (text.size () > 1) - { - text.erase (text.end () - 2); - } - } - - labelServerIp.setText (text); - } -//else if(key == configKeys.getCharKey("SaveGUILayout")) { - else if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == - true) - { - bool saved = - GraphicComponent::saveAllCustomProperties (containerName); - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("GUILayoutSaved") + " [" + - (saved ? lang. - getString ("Yes") : lang.getString ("No")) + "]"); - } - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - -//send key to the chat manager - chatManager.keyDown (key); - - if (chatManager.getEditEnabled () == false) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); -//if(key == configKeys.getCharKey("SaveGUILayout")) { - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == - true) - { - bool saved = - GraphicComponent::saveAllCustomProperties (containerName); - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("GUILayoutSaved") + " [" + - (saved ? lang. - getString ("Yes") : lang.getString ("No")) + - "]"); - } - } - } - } - - void MenuStateJoinGame::keyPress (SDL_KeyboardEvent c) - { - ClientInterface *clientInterface = - NetworkManager::getInstance ().getClientInterface (); - - if (clientInterface->isConnected () == false) - { - int maxTextSize = 22; - -//Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); - - SDL_Keycode key = extractKeyPressed (c); - -//if(c>='0' && c<='9') { - if ((key >= SDLK_0 && key <= SDLK_9) || - (key >= SDLK_KP_0 && key <= SDLK_KP_9)) - { - if ((int) labelServerIp.getText ().size () < maxTextSize) - { - string text = labelServerIp.getText (); -//text.insert(text.end()-1, key); - char szCharText[26] = ""; - snprintf (szCharText, 26, "%c", key); - char *utfStr = ConvertToUTF8 (&szCharText[0]); - if (text.size () > 0) - { - text.insert (text.end () - 1, utfStr[0]); - } - else - { - text = utfStr[0]; - } - - delete[]utfStr; - - labelServerIp.setText (text); - } - } -//else if (c=='.') { - else if (key == SDLK_PERIOD) - { - if ((int) labelServerIp.getText ().size () < maxTextSize) - { - string text = labelServerIp.getText (); - if (text.size () > 0) - { - text.insert (text.end () - 1, '.'); - } - else - { - text = "."; - } - - labelServerIp.setText (text); - } - } -//else if (c==':') { - else if (key == SDLK_COLON) - { - if ((int) labelServerIp.getText ().size () < maxTextSize) - { - string text = labelServerIp.getText (); - if (text.size () > 0) - { - text.insert (text.end () - 1, ':'); - } - else - { - text = ":"; - } - - labelServerIp.setText (text); - } - } - } - else - { - chatManager.keyPress (c); - } - } - - bool MenuStateJoinGame::connectToServer () - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] START\n", __FILE__, - __FUNCTION__); - - Config & config = Config::getInstance (); - string host = labelServerIp.getText (); - int port = config.getInt ("PortServer", - intToStr (GameConstants::serverPort). - c_str ()); - std::vector < std::string > hostPartsList; - Tokenize (host, hostPartsList, ":"); - if (hostPartsList.size () > 1) - { - host = hostPartsList[0]; - replaceAll (hostPartsList[1], "_", ""); - port = strToInt (hostPartsList[1]); - } - Ip serverIp (host); - - ClientInterface *clientInterface = - NetworkManager::getInstance ().getClientInterface (); - clientInterface->connect (serverIp, port); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] server - [%s]\n", __FILE__, - __FUNCTION__, - serverIp.getString ().c_str ()); - - labelServerIp.setText (serverIp.getString () + '_'); - labelInfo.setText (""); - -//save server ip - if (config.getString ("ServerIp") != serverIp.getString ()) - { - config.setString ("ServerIp", serverIp.getString ()); - config.save (); - } - - for (time_t elapsedWait = time (NULL); - clientInterface->getIntroDone () == false && - clientInterface->isConnected () && - difftime (time (NULL), elapsedWait) <= 10;) - { - if (clientInterface->isConnected ()) - { -//update lobby - clientInterface->updateLobby (); - sleep (0); -//this->render(); - } - } - if (clientInterface->isConnected () == true && - clientInterface->getIntroDone () == true) - { - - string saveHost = Ip (host).getString (); - if (hostPartsList.size () > 1) - { - saveHost += ":" + hostPartsList[1]; - } - servers.setString (clientInterface->getServerName (), saveHost); - servers.save (serversSavedFile); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d] Using FTP port #: %d\n", - __FILE__, __FUNCTION__, __LINE__, - clientInterface->getServerFTPPort ()); - abortAutoFind = true; - clientInterface->stopServerDiscovery (); - mainMenu->setState (new MenuStateConnectedGame (program, mainMenu)); - return true; - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] END\n", __FILE__, - __FUNCTION__); - return false; - } - - } + autoConnectToServer = false; + buttonAutoFindServers.setEnabled(true); + buttonConnect.setEnabled(true); + if (serverList.empty() == false) { + Config & config = Config::getInstance(); + string bestIPMatch = ""; + int serverGamePort = config.getInt("PortServer", + intToStr(GameConstants:: + serverPort).c_str()); + std::vector < std::string > localIPList = + Socket::getLocalIPAddressList(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + for (int idx = 0; idx < (int) serverList.size(); idx++) { + + vector < string > paramPartPortsTokens; + Tokenize(serverList[idx], paramPartPortsTokens, ":"); + if (paramPartPortsTokens.size() >= 2 + && paramPartPortsTokens[1].length() > 0) { + serverGamePort = strToInt(paramPartPortsTokens[1]); + } + + bestIPMatch = serverList[idx]; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] bestIPMatch = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + bestIPMatch.c_str()); + if (localIPList.empty() == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + break; + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] bestIPMatch = [%s] localIPList[0] = [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + bestIPMatch.c_str(), + localIPList[0].c_str()); + if (strncmp(localIPList[0].c_str(), serverList[idx].c_str(), 4) + == 0) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, + __LINE__); + break; + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (bestIPMatch != "") { + bestIPMatch += ":" + intToStr(serverGamePort); + } + labelServerIp.setText(bestIPMatch); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (serverList.size() > 1) { + listBoxServerType.setSelectedItemIndex(MenuStateJoinGame:: + foundServersIndex); + listBoxFoundServers.setItems(serverList); + } else { + autoConnectToServer = true; + } + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + void MenuStateJoinGame::mouseClick(int x, int y, MouseButton mouseButton) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] START\n", __FILE__, + __FUNCTION__); + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + ClientInterface *clientInterface = networkManager.getClientInterface(); + + if (clientInterface->isConnected() == false) { + //server type + if (listBoxServerType.mouseClick(x, y)) { + if (!listBoxServers.getText().empty()) { + labelServerIp. + setText(servers.getString(listBoxServers.getText()) + "_"); + } + } + //server list + else if (listBoxServerType.getSelectedItemIndex() == + newPrevServerIndex) { + if (listBoxServers.mouseClick(x, y)) { + labelServerIp. + setText(servers.getString(listBoxServers.getText()) + "_"); + } + } else if (listBoxServerType.getSelectedItemIndex() == + foundServersIndex) { + if (listBoxFoundServers.mouseClick(x, y)) { + labelServerIp.setText(listBoxFoundServers.getText()); + } + } + + string host = labelServerIp.getText(); + Config & config = Config::getInstance(); + int portNumber = config.getInt("PortServer", + intToStr(GameConstants::serverPort). + c_str()); + std::vector < std::string > hostPartsList; + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + portNumber = strToInt(hostPartsList[1]); + } + + string port = " (" + intToStr(portNumber) + ")"; + labelServerPort.setText(port); + + } + + //return + if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + + clientInterface->stopServerDiscovery(); + + if (clientInterface->getSocket() != NULL) { + //if(clientInterface->isConnected() == true) { + // string sQuitText = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()) + " has chosen to leave the game!"; + // clientInterface->sendTextMessage(sQuitText,-1); + //} + clientInterface->close(); + } + abortAutoFind = true; + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + return; + } + + //connect + else if (buttonConnect.mouseClick(x, y) + && buttonConnect.getEnabled() == true) { + ClientInterface *clientInterface = + networkManager.getClientInterface(); + + soundRenderer.playFx(coreData.getClickSoundB()); + labelInfo.setText(""); + + if (clientInterface->isConnected()) { + clientInterface->reset(); + } else { + if (connectToServer() == true) { + return; + } + } + } else if (buttonCreateGame.mouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundB()); + + clientInterface->stopServerDiscovery(); + if (clientInterface->getSocket() != NULL) { + clientInterface->close(); + } + abortAutoFind = true; + mainMenu->setState(new + MenuStateCustomGame(program, mainMenu, true, + pLanGame)); + return; + } + + else if (buttonAutoFindServers.mouseClick(x, y) + && buttonAutoFindServers.getEnabled() == true) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + ClientInterface *clientInterface = + networkManager.getClientInterface(); + soundRenderer.playFx(coreData.getClickSoundB()); + + // Triggers a thread which calls back into MenuStateJoinGame::DiscoveredServers + // with the results + if (clientInterface->isConnected() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + buttonAutoFindServers.setEnabled(false); + buttonConnect.setEnabled(false); + clientInterface->discoverServers(this); + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] END\n", __FILE__, + __FUNCTION__); + } + + void MenuStateJoinGame::mouseMove(int x, int y, const MouseState * ms) { + buttonReturn.mouseMove(x, y); + buttonConnect.mouseMove(x, y); + buttonAutoFindServers.mouseMove(x, y); + buttonCreateGame.mouseMove(x, y); + listBoxServerType.mouseMove(x, y); + + //hide-show options depending on the selection + if (listBoxServers.getSelectedItemIndex() == newServerIndex) { + labelServerIp.mouseMove(x, y); + } else if (listBoxServers.getSelectedItemIndex() == newPrevServerIndex) { + listBoxServers.mouseMove(x, y); + } else { + listBoxFoundServers.mouseMove(x, y); + } + } + + void MenuStateJoinGame::render() { + Renderer & renderer = Renderer::getInstance(); + + renderer.renderButton(&buttonReturn); + renderer.renderLabel(&labelServer); + renderer.renderLabel(&labelServerType); + renderer.renderLabel(&labelStatus); + renderer.renderLabel(&labelInfo); + renderer.renderLabel(&labelServerPort); + renderer.renderLabel(&labelServerPortLabel); + renderer.renderButton(&buttonConnect); + renderer.renderButton(&buttonCreateGame); + + renderer.renderButton(&buttonAutoFindServers); + renderer.renderListBox(&listBoxServerType); + + if (listBoxServerType.getSelectedItemIndex() == newServerIndex) { + renderer.renderLabel(&labelServerIp); + } else if (listBoxServerType.getSelectedItemIndex() == + newPrevServerIndex) { + renderer.renderListBox(&listBoxServers); + } else { + renderer.renderListBox(&listBoxFoundServers); + } + + renderer.renderChatManager(&chatManager); + renderer.renderConsole(&console); + + if (program != NULL) + program->renderProgramMsgBox(); + } + + void MenuStateJoinGame::update() { + ClientInterface *clientInterface = + NetworkManager::getInstance().getClientInterface(); + Lang & lang = Lang::getInstance(); + + //update status label + if (clientInterface->isConnected()) { + buttonConnect.setText(lang.getString("Disconnect")); + + if (clientInterface->getAllowDownloadDataSynch() == false) { + string label = lang.getString("ConnectedToServer"); + + if (!clientInterface->getServerName().empty()) { + label = label + " " + clientInterface->getServerName(); + } + + if (clientInterface->getAllowGameDataSynchCheck() == true && + clientInterface->getNetworkGameDataSynchCheckOk() == false) { + label = label + " - warning synch mismatch for:"; + if (clientInterface->getNetworkGameDataSynchCheckOkMap() == + false) { + label = label + " map"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTile() == + false) { + label = label + " tile"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTech() == + false) { + label = label + " techtree"; + } + } else if (clientInterface->getAllowGameDataSynchCheck() == true) { + label += " - data synch is ok"; + } + labelStatus.setText(label); + } else { + string label = lang.getString("ConnectedToServer"); + + if (!clientInterface->getServerName().empty()) { + label = label + " " + clientInterface->getServerName(); + } + + if (clientInterface->getAllowGameDataSynchCheck() == true && + clientInterface->getNetworkGameDataSynchCheckOk() == false) { + label = label + " - waiting to synch:"; + if (clientInterface->getNetworkGameDataSynchCheckOkMap() == + false) { + label = label + " map"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTile() == + false) { + label = label + " tile"; + } + if (clientInterface->getNetworkGameDataSynchCheckOkTech() == + false) { + label = label + " techtree"; + } + } else if (clientInterface->getAllowGameDataSynchCheck() == true) { + label += " - data synch is ok"; + } + + labelStatus.setText(label); + } + } else { + buttonConnect.setText(lang.getString("Connect")); + string connectedStatus = lang.getString("NotConnected"); + if (buttonAutoFindServers.getEnabled() == false) { + connectedStatus += " - searching for servers, please wait..."; + } + labelStatus.setText(connectedStatus); + labelInfo.setText(""); + } + + //process network messages + if (clientInterface->isConnected()) { + //update lobby + clientInterface->updateLobby(); + + clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface != NULL && clientInterface->isConnected()) { + //call the chat manager + chatManager.updateNetwork(); + + //console + console.update(); + + //intro + if (clientInterface->getIntroDone()) { + labelInfo.setText(lang.getString("WaitingHost")); + + string host = labelServerIp.getText(); + std::vector < std::string > hostPartsList; + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + } + string saveHost = Ip(host).getString(); + if (hostPartsList.size() > 1) { + saveHost += ":" + hostPartsList[1]; + } + + servers.setString(clientInterface->getServerName(), saveHost); + } + + //launch + if (clientInterface->getLaunchGame()) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] clientInterface->getLaunchGame() - A\n", + __FILE__, __FUNCTION__); + + servers.save(serversSavedFile); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] clientInterface->getLaunchGame() - B\n", + __FILE__, __FUNCTION__); + + abortAutoFind = true; + clientInterface->stopServerDiscovery(); + program->setState(new + Game(program, + clientInterface->getGameSettings(), + false)); + return; + } + } + } else if (autoConnectToServer == true) { + autoConnectToServer = false; + if (connectToServer() == true) { + return; + } + } + + if (clientInterface != NULL && clientInterface->getLaunchGame()) + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] clientInterface->getLaunchGame() - D\n", + __FILE__, __FUNCTION__); + } + + bool MenuStateJoinGame::textInput(std::string text) { + if (chatManager.getEditEnabled() == true) { + return chatManager.textInput(text); + } + return false; + } + + void MenuStateJoinGame::keyDown(SDL_KeyboardEvent key) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c][%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + + ClientInterface *clientInterface = + NetworkManager::getInstance().getClientInterface(); + if (clientInterface->isConnected() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + string text = labelServerIp.getText(); + if (isKeyPressed(SDLK_BACKSPACE, key) == true && text.length() > 0) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + size_t found = text.find_last_of("_"); + if (found == string::npos) { + text.erase(text.end() - 1); + } else { + if (text.size() > 1) { + text.erase(text.end() - 2); + } + } + + labelServerIp.setText(text); + } + //else if(key == configKeys.getCharKey("SaveGUILayout")) { + else if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == + true) { + bool saved = + GraphicComponent::saveAllCustomProperties(containerName); + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("GUILayoutSaved") + " [" + + (saved ? lang. + getString("Yes") : lang.getString("No")) + "]"); + } + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //send key to the chat manager + chatManager.keyDown(key); + + if (chatManager.getEditEnabled() == false) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(key == configKeys.getCharKey("SaveGUILayout")) { + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == + true) { + bool saved = + GraphicComponent::saveAllCustomProperties(containerName); + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("GUILayoutSaved") + " [" + + (saved ? lang. + getString("Yes") : lang.getString("No")) + + "]"); + } + } + } + } + + void MenuStateJoinGame::keyPress(SDL_KeyboardEvent c) { + ClientInterface *clientInterface = + NetworkManager::getInstance().getClientInterface(); + + if (clientInterface->isConnected() == false) { + int maxTextSize = 22; + + //Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); + + SDL_Keycode key = extractKeyPressed(c); + + //if(c>='0' && c<='9') { + if ((key >= SDLK_0 && key <= SDLK_9) || + (key >= SDLK_KP_0 && key <= SDLK_KP_9)) { + if ((int) labelServerIp.getText().size() < maxTextSize) { + string text = labelServerIp.getText(); + //text.insert(text.end()-1, key); + char szCharText[26] = ""; + snprintf(szCharText, 26, "%c", key); + char *utfStr = ConvertToUTF8(&szCharText[0]); + if (text.size() > 0) { + text.insert(text.end() - 1, utfStr[0]); + } else { + text = utfStr[0]; + } + + delete[]utfStr; + + labelServerIp.setText(text); + } + } + //else if (c=='.') { + else if (key == SDLK_PERIOD) { + if ((int) labelServerIp.getText().size() < maxTextSize) { + string text = labelServerIp.getText(); + if (text.size() > 0) { + text.insert(text.end() - 1, '.'); + } else { + text = "."; + } + + labelServerIp.setText(text); + } + } + //else if (c==':') { + else if (key == SDLK_COLON) { + if ((int) labelServerIp.getText().size() < maxTextSize) { + string text = labelServerIp.getText(); + if (text.size() > 0) { + text.insert(text.end() - 1, ':'); + } else { + text = ":"; + } + + labelServerIp.setText(text); + } + } + } else { + chatManager.keyPress(c); + } + } + + bool MenuStateJoinGame::connectToServer() { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] START\n", __FILE__, + __FUNCTION__); + + Config & config = Config::getInstance(); + string host = labelServerIp.getText(); + int port = config.getInt("PortServer", + intToStr(GameConstants::serverPort). + c_str()); + std::vector < std::string > hostPartsList; + Tokenize(host, hostPartsList, ":"); + if (hostPartsList.size() > 1) { + host = hostPartsList[0]; + replaceAll(hostPartsList[1], "_", ""); + port = strToInt(hostPartsList[1]); + } + Ip serverIp(host); + + ClientInterface *clientInterface = + NetworkManager::getInstance().getClientInterface(); + clientInterface->connect(serverIp, port); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] server - [%s]\n", __FILE__, + __FUNCTION__, + serverIp.getString().c_str()); + + labelServerIp.setText(serverIp.getString() + '_'); + labelInfo.setText(""); + + //save server ip + if (config.getString("ServerIp") != serverIp.getString()) { + config.setString("ServerIp", serverIp.getString()); + config.save(); + } + + for (time_t elapsedWait = time(NULL); + clientInterface->getIntroDone() == false && + clientInterface->isConnected() && + difftime(time(NULL), elapsedWait) <= 10;) { + if (clientInterface->isConnected()) { + //update lobby + clientInterface->updateLobby(); + sleep(0); + //this->render(); + } + } + if (clientInterface->isConnected() == true && + clientInterface->getIntroDone() == true) { + + string saveHost = Ip(host).getString(); + if (hostPartsList.size() > 1) { + saveHost += ":" + hostPartsList[1]; + } + servers.setString(clientInterface->getServerName(), saveHost); + servers.save(serversSavedFile); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d] Using FTP port #: %d\n", + __FILE__, __FUNCTION__, __LINE__, + clientInterface->getServerFTPPort()); + abortAutoFind = true; + clientInterface->stopServerDiscovery(); + mainMenu->setState(new MenuStateConnectedGame(program, mainMenu)); + return true; + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] END\n", __FILE__, + __FUNCTION__); + return false; + } + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_join_game.h b/source/glest_game/menu/menu_state_join_game.h index c6545fc29..6dd3c8b48 100644 --- a/source/glest_game/menu/menu_state_join_game.h +++ b/source/glest_game/menu/menu_state_join_game.h @@ -26,104 +26,101 @@ using Shared::Util::Properties; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class NetworkMessageIntro; + class NetworkMessageIntro; -// =============================== -// class MenuStateJoinGame -// =============================== + // =============================== + // class MenuStateJoinGame + // =============================== - class MenuStateJoinGame: - public MenuState, public DiscoveredServersInterface - { - private: - static const int - newServerIndex; - static const int - newPrevServerIndex; - static const int - foundServersIndex; - static const - string - serverFileName; + class MenuStateJoinGame : + public MenuState, public DiscoveredServersInterface { + private: + static const int + newServerIndex; + static const int + newPrevServerIndex; + static const int + foundServersIndex; + static const + string + serverFileName; - private: - GraphicButton buttonReturn; - GraphicButton buttonConnect; - GraphicButton buttonAutoFindServers; - GraphicButton buttonCreateGame; + private: + GraphicButton buttonReturn; + GraphicButton buttonConnect; + GraphicButton buttonAutoFindServers; + GraphicButton buttonCreateGame; - GraphicLabel labelServer; - GraphicLabel labelServerType; - GraphicLabel labelServerIp; - GraphicLabel labelStatus; - GraphicLabel labelInfo; - GraphicListBox listBoxServerType; - GraphicListBox listBoxServers; - GraphicListBox listBoxFoundServers; - GraphicLabel labelServerPort; - GraphicLabel labelServerPortLabel; + GraphicLabel labelServer; + GraphicLabel labelServerType; + GraphicLabel labelServerIp; + GraphicLabel labelStatus; + GraphicLabel labelInfo; + GraphicListBox listBoxServerType; + GraphicListBox listBoxServers; + GraphicListBox listBoxFoundServers; + GraphicLabel labelServerPort; + GraphicLabel labelServerPortLabel; - bool connected; - int - playerIndex; - Properties servers; + bool connected; + int + playerIndex; + Properties servers; -//Console console; - ChatManager chatManager; + //Console console; + ChatManager chatManager; - string serversSavedFile; - bool abortAutoFind; - bool autoConnectToServer; + string serversSavedFile; + bool abortAutoFind; + bool autoConnectToServer; - public: - MenuStateJoinGame (Program * program, MainMenu * mainMenu, - bool connect = false, Ip serverIp = - Ip (), int portNumberOverride = -1); - MenuStateJoinGame (Program * program, MainMenu * mainMenu, - bool * autoFindHost); - virtual ~ MenuStateJoinGame (); + public: + MenuStateJoinGame(Program * program, MainMenu * mainMenu, + bool connect = false, Ip serverIp = + Ip(), int portNumberOverride = -1); + MenuStateJoinGame(Program * program, MainMenu * mainMenu, + bool * autoFindHost); + virtual ~MenuStateJoinGame(); - void - mouseClick (int x, int y, MouseButton mouseButton); - void - mouseMove (int x, int y, const MouseState * mouseState); - void - render (); - void - update (); + void + mouseClick(int x, int y, MouseButton mouseButton); + void + mouseMove(int x, int y, const MouseState * mouseState); + void + render(); + void + update(); - virtual - bool - textInput (std::string text); - virtual void - keyDown (SDL_KeyboardEvent key); - virtual void - keyPress (SDL_KeyboardEvent c); + virtual + bool + textInput(std::string text); + virtual void + keyDown(SDL_KeyboardEvent key); + virtual void + keyPress(SDL_KeyboardEvent c); - virtual - bool - isInSpecialKeyCaptureEvent () - { - return chatManager.getEditEnabled (); - } + virtual + bool + isInSpecialKeyCaptureEvent() { + return chatManager.getEditEnabled(); + } - void - reloadUI (); + void + reloadUI(); - private: + private: - void - CommonInit (bool connect, Ip serverIp, int portNumberOverride); - bool connectToServer (); - virtual void - DiscoveredServers (std::vector < string > serverList); - }; -}} //end namespace + void + CommonInit(bool connect, Ip serverIp, int portNumberOverride); + bool connectToServer(); + virtual void + DiscoveredServers(std::vector < string > serverList); + }; + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_keysetup.cpp b/source/glest_game/menu/menu_state_keysetup.cpp index 92c4eae04..95778d95c 100644 --- a/source/glest_game/menu/menu_state_keysetup.cpp +++ b/source/glest_game/menu/menu_state_keysetup.cpp @@ -27,841 +27,754 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - -// ===================================================== -// class MenuStateKeysetup -// ===================================================== - - MenuStateKeysetup::MenuStateKeysetup (Program * program, - MainMenu * mainMenu, - ProgramState ** - parentUI):MenuState (program, - mainMenu, - "config"), - buttonOk ("KeySetup", "buttonOk"), buttonDefaults ("KeySetup", - "buttonDefaults"), - buttonReturn ("KeySetup", "buttonReturn"), - buttonKeyboardSetup ("KeySetup", "buttonKeyboardSetup"), - buttonVideoSection ("KeySetup", "buttonVideoSection"), - buttonAudioSection ("KeySetup", "buttonAudioSection"), - buttonMiscSection ("KeySetup", "buttonMiscSection"), - buttonNetworkSettings ("KeySetup", "buttonNetworkSettings"), - labelTitle ("KeySetup", "labelTitle"), keyScrollBar ("KeySetup", - "keyScrollBar"), - mainMessageBox ("KeySetup", "mainMessageBox"), - labelTestTitle ("KeySetup", "labelTestTitle"), - labelTestValue ("KeySetup", "labelTestValue") - { - try - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - containerName = "KeySetup"; - - keyButtonsLineHeight = 30; - keyButtonsHeight = 25; - keyButtonsWidth = 400; - keyButtonsXBase = 200; - keyButtonsYBase = 200 + 400 - keyButtonsLineHeight; - keyButtonsToRender = 400 / keyButtonsLineHeight; - int labelWidth = 100; - - this->parentUI = parentUI; - this->console.setOnlyChatMessagesInStoredLines (false); - hotkeyIndex = -1; - hotkeyChar = SDLK_UNKNOWN; - - Lang & lang = Lang::getInstance (); - int buttonStartPos = 170; - int buttonRowPos = 50; - if (this->parentUI == NULL) - { - int tabButtonWidth = 200; - int tabButtonHeight = 30; - - buttonAudioSection.init (0, 720, tabButtonWidth, tabButtonHeight); - buttonAudioSection.setFont (CoreData::getInstance (). - getMenuFontVeryBig ()); - buttonAudioSection.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - buttonAudioSection.setText (lang.getString ("Audio")); - // Video Section - buttonVideoSection.init (200, 720, tabButtonWidth, - tabButtonHeight); - buttonVideoSection.setFont (CoreData::getInstance (). - getMenuFontVeryBig ()); - buttonVideoSection.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - buttonVideoSection.setText (lang.getString ("Video")); - //currentLine-=lineOffset; - //MiscSection - buttonMiscSection.init (400, 720, tabButtonWidth, - tabButtonHeight); - buttonMiscSection.setFont (CoreData::getInstance (). - getMenuFontVeryBig ()); - buttonMiscSection.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - buttonMiscSection.setText (lang.getString ("Misc")); - //NetworkSettings - buttonNetworkSettings.init (600, 720, tabButtonWidth, - tabButtonHeight); - buttonNetworkSettings.setFont (CoreData::getInstance (). - getMenuFontVeryBig ()); - buttonNetworkSettings.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - buttonNetworkSettings.setText (lang.getString ("Network")); - - //KeyboardSetup - buttonKeyboardSetup.init (800, 700, tabButtonWidth, - tabButtonHeight + 20); - buttonKeyboardSetup.setFont (CoreData::getInstance (). - getMenuFontVeryBig ()); - buttonKeyboardSetup.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - buttonKeyboardSetup.setText (lang.getString ("Keyboardsetup")); - } - // header - labelTitle.init (375, 650); - labelTitle.setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - labelTitle.setFont3D (CoreData::getInstance (). - getMenuFontVeryBig3D ()); - labelTitle.setText (lang.getString ("KeyboardsetupL")); - - labelTestTitle.init (keyButtonsXBase, 155); - labelTestTitle.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - labelTestTitle.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - labelTestTitle.setText (lang.getString ("KeyboardsetupTest")); - - labelTestValue.init (keyButtonsXBase, 155 - 28); - labelTestValue.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTestValue.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - labelTestValue.setRenderBackground (true); - labelTestValue.setMaxEditRenderWidth (keyButtonsWidth); - labelTestValue.setText (""); - - // mainMassegeBox - mainMessageBox.init (lang.getString ("Ok")); - mainMessageBox.setEnabled (false); - mainMessageBoxState = 0; - - keyScrollBar.init (800, 200, false, 200, 20); - keyScrollBar.setLength (400); - keyScrollBar.setElementCount (0); - keyScrollBar.setVisibleSize (keyButtonsToRender); - keyScrollBar.setVisibleStart (0); - - // buttons - buttonOk.init (buttonStartPos, buttonRowPos, 100); - buttonOk.setText (lang.getString ("Save")); - - buttonReturn.init (buttonStartPos + 110, buttonRowPos, 100); - buttonReturn.setText (lang.getString ("Return")); - - buttonDefaults.init (buttonStartPos + 230, buttonRowPos, 125); - buttonDefaults.setText (lang.getString ("Defaults")); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - mergedProperties = configKeys.getMergedProperties (); - masterProperties = configKeys.getMasterProperties (); - //userProperties=configKeys.getUserProperties(); - userProperties.clear (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - //throw megaglest_runtime_error("Test!"); - - for (int i = 0; i < (int) mergedProperties.size (); ++i) - { - - string keyName = mergedProperties[i].second; - if (keyName.length () > 0) - { - //char c = configKeys.translateStringToCharKey(keyName); - SDL_Keycode c = configKeys.translateStringToSDLKey (keyName); - if (c > SDLK_UNKNOWN && c < SDL_NUM_SCANCODES) - { - SDL_Keycode keysym = static_cast < SDL_Keycode > (c); - // SDL skips capital letters - if (keysym >= 65 && keysym <= 90) - { - keysym = (SDL_Keycode) ((int) keysym + 32); - } - keyName = SDL_GetKeyName (keysym); - } - else - { - keyName = ""; - } - if (keyName == "unknown key" || keyName == "") - { - keyName = mergedProperties[i].second; - } - } - - GraphicButton *button = - new GraphicButton (containerName, - string ("ScrollButton") + intToStr (i)); - button->init (keyButtonsXBase, keyButtonsYBase, keyButtonsWidth, - keyButtonsHeight); - button->setText (mergedProperties[i].first); - keyButtons.push_back (button); - GraphicLabel *label = - new GraphicLabel (containerName, - string ("ScrollLabel") + intToStr (i)); - label->init (keyButtonsXBase + keyButtonsWidth + 5, keyButtonsYBase, - labelWidth, 20); - label->setRenderBackground (true); - label->setMaxEditRenderWidth (105); - label->setText (" " + keyName); - labels.push_back (label); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - keyScrollBar.init (keyButtonsXBase + keyButtonsWidth + labelWidth + - 20, 200, false, 200, 20); - keyScrollBar.setLength (400); - keyScrollBar.setElementCount ((int) keyButtons.size ()); - keyScrollBar.setVisibleSize (keyButtonsToRender); - keyScrollBar.setVisibleStart (0); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", - __FILE__, __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - mainMessageBoxState = 1; - showMessageBox ("Error: " + string (ex.what ()), "Error detected", - false); - } - } - - void MenuStateKeysetup::reloadUI () - { - Lang & lang = Lang::getInstance (); - - labelTitle.setText (lang.getString ("KeyboardsetupL")); - labelTestTitle.setText (lang.getString ("KeyboardsetupTest")); - labelTestValue.setText (""); - - // mainMassegeBox - mainMessageBox.init (lang.getString ("Ok")); - - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - buttonDefaults.setText (lang.getString ("Defaults")); - } - - void MenuStateKeysetup::cleanup () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - clearUserButtons (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] END\n", __FILE__, - __FUNCTION__, __LINE__); - } - - MenuStateKeysetup::~MenuStateKeysetup () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - cleanup (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] END\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void MenuStateKeysetup::clearUserButtons () - { - while (!keyButtons.empty ()) - { - delete keyButtons.back (); - keyButtons.pop_back (); - } - while (!labels.empty ()) - { - delete labels.back (); - labels.pop_back (); - } - } - - void MenuStateKeysetup::mouseClick (int x, int y, MouseButton mouseButton) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - mainMessageBox.setEnabled (false); - } - } - } - else if (keyScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundB ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (buttonReturn.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundB ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (this->parentUI != NULL) - { - // Set the parent pointer to NULL so the owner knows it was deleted - *this->parentUI = NULL; - // Delete the main menu - delete mainMenu; - return; - } - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (buttonDefaults.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundB ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - string userKeysFile = configKeys.getFileName (true); - - bool result = removeFile (userKeysFile.c_str ()); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] delete file [%s] returned %d\n", - __FILE__, __FUNCTION__, __LINE__, userKeysFile.c_str (), - result); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] delete file [%s] returned %d\n", - __FILE__, __FUNCTION__, __LINE__, - userKeysFile.c_str (), result); - configKeys.reload (); - - if (this->parentUI != NULL) - { - // Set the parent pointer to NULL so the owner knows it was deleted - *this->parentUI = NULL; - // Delete the main menu - delete mainMenu; - return; - } - - mainMenu->setState (new MenuStateKeysetup (program, mainMenu)); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (buttonOk.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundB ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (userProperties.empty () == false) - { - Config & config = Config::getInstance (); - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys), - std::pair < string, - string > (Config::glestkeys_ini_filename, - Config:: - glestuserkeys_ini_filename), - std::pair < bool, bool > (true, false), - config.getString ("GlestKeysIniPath", "")); - string userKeysFile = configKeys.getFileName (true); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] save file [%s] userProperties.size() = " - MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, __LINE__, - userKeysFile.c_str (), userProperties.size ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] save file [%s] userProperties.size() = " - MG_SIZE_T_SPECIFIER "\n", __FILE__, - __FUNCTION__, __LINE__, - userKeysFile.c_str (), - userProperties.size ()); - - configKeys.setUserProperties (userProperties); - configKeys.save (); - configKeys.reload (); - } - - Lang & lang = Lang::getInstance (); - console.addLine (lang.getString ("SettingsSaved")); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (keyScrollBar.getElementCount () != 0) - { - for (int i = keyScrollBar.getVisibleStart (); i - <= keyScrollBar.getVisibleEnd (); ++i) - { - if (keyButtons[i]->mouseClick (x, y)) - { - hotkeyIndex = i; - hotkeyChar = SDLK_UNKNOWN; - break; - } - } - } - - if (this->parentUI == NULL) - { - if (buttonKeyboardSetup.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - return; - } - else if (buttonAudioSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsSound (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonNetworkSettings.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsNetwork (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonMiscSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptions (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonVideoSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - } - } - - void MenuStateKeysetup::mouseUp (int x, int y, - const MouseButton mouseButton) - { - if (mouseButton == mbLeft) - { - keyScrollBar.mouseUp (x, y); - } - } - - void MenuStateKeysetup::mouseMove (int x, int y, const MouseState * ms) - { - buttonReturn.mouseMove (x, y); - buttonOk.mouseMove (x, y); - if (this->parentUI == NULL) - { - buttonKeyboardSetup.mouseMove (x, y); - buttonAudioSection.mouseMove (x, y); - buttonNetworkSettings.mouseMove (x, y); - buttonMiscSection.mouseMove (x, y); - buttonVideoSection.mouseMove (x, y); - } - if (ms->get (mbLeft)) - { - keyScrollBar.mouseDown (x, y); - } - else - { - keyScrollBar.mouseMove (x, y); - } - - if (keyScrollBar.getElementCount () != 0) - { - for (int i = keyScrollBar.getVisibleStart (); - i <= keyScrollBar.getVisibleEnd (); ++i) - { - keyButtons[i]->mouseMove (x, y); - } - } - - } - - void MenuStateKeysetup::render () - { - Renderer & renderer = Renderer::getInstance (); - - //printf("MenuStateKeysetup::render A\n"); - - if (mainMessageBox.getEnabled ()) - { - //printf("MenuStateKeysetup::render B\n"); - renderer.renderMessageBox (&mainMessageBox); - } - else - { - //printf("MenuStateKeysetup::render C\n"); - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonDefaults); - renderer.renderButton (&buttonOk); - if (this->parentUI == NULL) - { - renderer.renderButton (&buttonKeyboardSetup); - renderer.renderButton (&buttonVideoSection); - renderer.renderButton (&buttonAudioSection); - renderer.renderButton (&buttonMiscSection); - renderer.renderButton (&buttonNetworkSettings); - } - renderer.renderLabel (&labelTitle); - renderer.renderLabel (&labelTestTitle); - renderer.renderLabel (&labelTestValue); - - if (keyScrollBar.getElementCount () != 0) - { - for (int i = keyScrollBar.getVisibleStart (); - i <= keyScrollBar.getVisibleEnd (); ++i) - { - if (hotkeyIndex == i) - { - renderer.renderButton (keyButtons[i], &YELLOW); - } - else - { - renderer.renderButton (keyButtons[i]); - } - renderer.renderLabel (labels[i]); - } - } - renderer.renderScrollBar (&keyScrollBar); - } - - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } - - void MenuStateKeysetup::update () - { - //printf("MenuStateKeysetup::update A\n"); - - if (keyScrollBar.getElementCount () != 0) - { - for (int i = keyScrollBar.getVisibleStart (); i - <= keyScrollBar.getVisibleEnd (); ++i) - { - keyButtons[i]->setY (keyButtonsYBase - keyButtonsLineHeight * (i - - - keyScrollBar. - getVisibleStart - ())); - labels[i]->setY (keyButtonsYBase - - keyButtonsLineHeight * (i - - keyScrollBar. - getVisibleStart ())); - } - } - - console.update (); - } - - - - void MenuStateKeysetup::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - - void MenuStateKeysetup::keyDown (SDL_KeyboardEvent key) - { - hotkeyChar = extractKeyPressed (key); - //printf("\nkeyDown [%d]\n",hotkeyChar); - - string keyName = ""; - //if(hotkeyChar > SDLK_UNKNOWN && hotkeyChar < SDL_NUM_SCANCODES) { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", __FILE__, - __FUNCTION__, __LINE__, keyName.c_str (), hotkeyChar, - key.keysym.sym); - keyName = SDL_GetKeyName (hotkeyChar); - //printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n",__FILE__,__FUNCTION__,__LINE__,keyName.c_str(),hotkeyChar,key.keysym.sym); - //} - //key = hotkeyChar; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", __FILE__, - __FUNCTION__, __LINE__, keyName.c_str (), hotkeyChar, - key.keysym.sym); - -// SDLKey keysym = SDLK_UNKNOWN; -// if(keyName == "unknown key" || keyName == "") { -// Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); -// keysym = configKeys.translateSpecialStringToSDLKey(hotkeyChar); -// -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] keysym [%d]\n",__FILE__,__FUNCTION__,__LINE__,keysym); -// -// // SDL skips capital letters -// if(keysym >= 65 && keysym <= 90) { -// keysym = (SDLKey)((int)keysym + 32); -// } -// //if(keysym < 255) { -// // key = keysym; -// //} -// keyName = SDL_GetKeyName(keysym); -// } - - char szCharText[20] = ""; - snprintf (szCharText, 20, "%c", hotkeyChar); - char *utfStr = ConvertToUTF8 (&szCharText[0]); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, " %s [%s][%d][%d][%d]", keyName.c_str (), - utfStr, key.keysym.sym, hotkeyChar, key.keysym.mod); - labelTestValue.setText (szBuf); - //printf ("In [%s::%s Line: %d] szBuf [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf); - - delete[]utfStr; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] hotkeyChar [%d]\n", __FILE__, - __FUNCTION__, __LINE__, hotkeyChar); - } - - void MenuStateKeysetup::keyPress (SDL_KeyboardEvent c) - { - } - - void MenuStateKeysetup::keyUp (SDL_KeyboardEvent key) - { - //Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); - - if (hotkeyIndex >= 0) - { - if (hotkeyChar != 0) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] char [%d][%d]\n", __FILE__, - __FUNCTION__, __LINE__, hotkeyChar, key.keysym.sym); - - //string keyName = ""; - //if(hotkeyChar > SDLK_UNKNOWN && hotkeyChar < SDL_NUM_SCANCODES) { - - string keyName = SDL_GetKeyName (key.keysym.sym); - if (StartsWith (keyName, "Keypad ") == false) - { - keyName = SDL_GetKeyName (hotkeyChar); - key.keysym.sym = hotkeyChar; - } - //} - //key.keysym.sym = hotkeyChar; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", - __FILE__, __FUNCTION__, __LINE__, keyName.c_str (), - hotkeyChar, key.keysym.sym); - - //SDLKey keysym = SDLK_UNKNOWN; -// if(keyName == "unknown key" || keyName == "") { -// Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); -// keysym = configKeys.translateSpecialStringToSDLKey(hotkeyChar); -// -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] keysym [%d]\n",__FILE__,__FUNCTION__,__LINE__,keysym); -// -// // SDL skips capital letters -// if(keysym >= 65 && keysym <= 90) { -// keysym = (SDLKey)((int)keysym + 32); -// } -// if(keysym < 255) { -// key = keysym; -// } -// keyName = SDL_GetKeyName(keysym); -// } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", - __FILE__, __FUNCTION__, __LINE__, keyName.c_str (), - hotkeyChar, key.keysym.sym); - - if (keyName != "unknown key") - { - GraphicLabel *label = labels[hotkeyIndex]; - label->setText (keyName); - - pair < string, string > &nameValuePair = - mergedProperties[hotkeyIndex]; - - // Need to distinguish numeric keys to be translated to real keys - // from these ACTUAL sdl keys so surround in quotes. - //printf("KeyUp #1 keyName [%s]\n", keyName.c_str()); - - if (keyName.size () == 1 && keyName[0] >= '0' - && keyName[0] <= '9') - { - keyName = "'" + keyName + "'"; - } - //printf("KeyUp #2 keyName [%s]\n", keyName.c_str()); - - bool isNewUserKeyEntry = true; - for (int i = 0; i < (int) userProperties.size (); ++i) - { - string hotKeyName = userProperties[i].first; - if (nameValuePair.first == hotKeyName) - { -// if(keysym <= SDLK_ESCAPE || keysym > 255) { -// if(keysym <= SDLK_ESCAPE) { -// userProperties[i].second = intToStr(extractKeyPressed(key)); -// } -// else { -// userProperties[i].second = keyName; -// } -// } -// else { -// userProperties[i].second = ""; -// userProperties[i].second.push_back(extractKeyPressed(key)); -// } - userProperties[i].second = keyName; - isNewUserKeyEntry = false; - break; - } - } - if (isNewUserKeyEntry == true) - { - pair < string, string > newNameValuePair = nameValuePair; -// if(keysym <= SDLK_ESCAPE || keysym > 255) { -// if(keysym <= SDLK_ESCAPE) { -// newNameValuePair.second = intToStr(extractKeyPressed(key)); -// } -// else { -// newNameValuePair.second = keyName; -// } -// } -// else { -// newNameValuePair.second = extractKeyPressed(key); -// } - newNameValuePair.second = keyName; - userProperties.push_back (newNameValuePair); - } - } - } - hotkeyIndex = -1; - hotkeyChar = SDLK_UNKNOWN; - } - } - - - } +namespace Glest { + namespace Game { + + + // ===================================================== + // class MenuStateKeysetup + // ===================================================== + + MenuStateKeysetup::MenuStateKeysetup(Program * program, + MainMenu * mainMenu, + ProgramState ** + parentUI) :MenuState(program, + mainMenu, + "config"), + buttonOk("KeySetup", "buttonOk"), buttonDefaults("KeySetup", + "buttonDefaults"), + buttonReturn("KeySetup", "buttonReturn"), + buttonKeyboardSetup("KeySetup", "buttonKeyboardSetup"), + buttonVideoSection("KeySetup", "buttonVideoSection"), + buttonAudioSection("KeySetup", "buttonAudioSection"), + buttonMiscSection("KeySetup", "buttonMiscSection"), + buttonNetworkSettings("KeySetup", "buttonNetworkSettings"), + labelTitle("KeySetup", "labelTitle"), keyScrollBar("KeySetup", + "keyScrollBar"), + mainMessageBox("KeySetup", "mainMessageBox"), + labelTestTitle("KeySetup", "labelTestTitle"), + labelTestValue("KeySetup", "labelTestValue") { + try { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + containerName = "KeySetup"; + + keyButtonsLineHeight = 30; + keyButtonsHeight = 25; + keyButtonsWidth = 400; + keyButtonsXBase = 200; + keyButtonsYBase = 200 + 400 - keyButtonsLineHeight; + keyButtonsToRender = 400 / keyButtonsLineHeight; + int labelWidth = 100; + + this->parentUI = parentUI; + this->console.setOnlyChatMessagesInStoredLines(false); + hotkeyIndex = -1; + hotkeyChar = SDLK_UNKNOWN; + + Lang & lang = Lang::getInstance(); + int buttonStartPos = 170; + int buttonRowPos = 50; + if (this->parentUI == NULL) { + int tabButtonWidth = 200; + int tabButtonHeight = 30; + + buttonAudioSection.init(0, 720, tabButtonWidth, tabButtonHeight); + buttonAudioSection.setFont(CoreData::getInstance(). + getMenuFontVeryBig()); + buttonAudioSection.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + buttonAudioSection.setText(lang.getString("Audio")); + // Video Section + buttonVideoSection.init(200, 720, tabButtonWidth, + tabButtonHeight); + buttonVideoSection.setFont(CoreData::getInstance(). + getMenuFontVeryBig()); + buttonVideoSection.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + buttonVideoSection.setText(lang.getString("Video")); + //currentLine-=lineOffset; + //MiscSection + buttonMiscSection.init(400, 720, tabButtonWidth, + tabButtonHeight); + buttonMiscSection.setFont(CoreData::getInstance(). + getMenuFontVeryBig()); + buttonMiscSection.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + buttonMiscSection.setText(lang.getString("Misc")); + //NetworkSettings + buttonNetworkSettings.init(600, 720, tabButtonWidth, + tabButtonHeight); + buttonNetworkSettings.setFont(CoreData::getInstance(). + getMenuFontVeryBig()); + buttonNetworkSettings.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + buttonNetworkSettings.setText(lang.getString("Network")); + + //KeyboardSetup + buttonKeyboardSetup.init(800, 700, tabButtonWidth, + tabButtonHeight + 20); + buttonKeyboardSetup.setFont(CoreData::getInstance(). + getMenuFontVeryBig()); + buttonKeyboardSetup.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + buttonKeyboardSetup.setText(lang.getString("Keyboardsetup")); + } + // header + labelTitle.init(375, 650); + labelTitle.setFont(CoreData::getInstance().getMenuFontVeryBig()); + labelTitle.setFont3D(CoreData::getInstance(). + getMenuFontVeryBig3D()); + labelTitle.setText(lang.getString("KeyboardsetupL")); + + labelTestTitle.init(keyButtonsXBase, 155); + labelTestTitle.setFont(CoreData::getInstance(). + getMenuFontNormal()); + labelTestTitle.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + labelTestTitle.setText(lang.getString("KeyboardsetupTest")); + + labelTestValue.init(keyButtonsXBase, 155 - 28); + labelTestValue.setFont(CoreData::getInstance().getMenuFontBig()); + labelTestValue.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + labelTestValue.setRenderBackground(true); + labelTestValue.setMaxEditRenderWidth(keyButtonsWidth); + labelTestValue.setText(""); + + // mainMassegeBox + mainMessageBox.init(lang.getString("Ok")); + mainMessageBox.setEnabled(false); + mainMessageBoxState = 0; + + keyScrollBar.init(800, 200, false, 200, 20); + keyScrollBar.setLength(400); + keyScrollBar.setElementCount(0); + keyScrollBar.setVisibleSize(keyButtonsToRender); + keyScrollBar.setVisibleStart(0); + + // buttons + buttonOk.init(buttonStartPos, buttonRowPos, 100); + buttonOk.setText(lang.getString("Save")); + + buttonReturn.init(buttonStartPos + 110, buttonRowPos, 100); + buttonReturn.setText(lang.getString("Return")); + + buttonDefaults.init(buttonStartPos + 230, buttonRowPos, 125); + buttonDefaults.setText(lang.getString("Defaults")); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + mergedProperties = configKeys.getMergedProperties(); + masterProperties = configKeys.getMasterProperties(); + //userProperties=configKeys.getUserProperties(); + userProperties.clear(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //throw megaglest_runtime_error("Test!"); + + for (int i = 0; i < (int) mergedProperties.size(); ++i) { + + string keyName = mergedProperties[i].second; + if (keyName.length() > 0) { + //char c = configKeys.translateStringToCharKey(keyName); + SDL_Keycode c = configKeys.translateStringToSDLKey(keyName); + if (c > SDLK_UNKNOWN && c < SDL_NUM_SCANCODES) { + SDL_Keycode keysym = static_cast (c); + // SDL skips capital letters + if (keysym >= 65 && keysym <= 90) { + keysym = (SDL_Keycode) ((int) keysym + 32); + } + keyName = SDL_GetKeyName(keysym); + } else { + keyName = ""; + } + if (keyName == "unknown key" || keyName == "") { + keyName = mergedProperties[i].second; + } + } + + GraphicButton *button = + new GraphicButton(containerName, + string("ScrollButton") + intToStr(i)); + button->init(keyButtonsXBase, keyButtonsYBase, keyButtonsWidth, + keyButtonsHeight); + button->setText(mergedProperties[i].first); + keyButtons.push_back(button); + GraphicLabel *label = + new GraphicLabel(containerName, + string("ScrollLabel") + intToStr(i)); + label->init(keyButtonsXBase + keyButtonsWidth + 5, keyButtonsYBase, + labelWidth, 20); + label->setRenderBackground(true); + label->setMaxEditRenderWidth(105); + label->setText(" " + keyName); + labels.push_back(label); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + keyScrollBar.init(keyButtonsXBase + keyButtonsWidth + labelWidth + + 20, 200, false, 200, 20); + keyScrollBar.setLength(400); + keyScrollBar.setElementCount((int) keyButtons.size()); + keyScrollBar.setVisibleSize(keyButtonsToRender); + keyScrollBar.setVisibleStart(0); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", + __FILE__, __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + mainMessageBoxState = 1; + showMessageBox("Error: " + string(ex.what()), "Error detected", + false); + } + } + + void MenuStateKeysetup::reloadUI() { + Lang & lang = Lang::getInstance(); + + labelTitle.setText(lang.getString("KeyboardsetupL")); + labelTestTitle.setText(lang.getString("KeyboardsetupTest")); + labelTestValue.setText(""); + + // mainMassegeBox + mainMessageBox.init(lang.getString("Ok")); + + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + buttonDefaults.setText(lang.getString("Defaults")); + } + + void MenuStateKeysetup::cleanup() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + clearUserButtons(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] END\n", __FILE__, + __FUNCTION__, __LINE__); + } + + MenuStateKeysetup::~MenuStateKeysetup() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + cleanup(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] END\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void MenuStateKeysetup::clearUserButtons() { + while (!keyButtons.empty()) { + delete keyButtons.back(); + keyButtons.pop_back(); + } + while (!labels.empty()) { + delete labels.back(); + labels.pop_back(); + } + } + + void MenuStateKeysetup::mouseClick(int x, int y, MouseButton mouseButton) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + mainMessageBox.setEnabled(false); + } + } + } else if (keyScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundB()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (buttonReturn.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundB()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (this->parentUI != NULL) { + // Set the parent pointer to NULL so the owner knows it was deleted + *this->parentUI = NULL; + // Delete the main menu + delete mainMenu; + return; + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (buttonDefaults.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundB()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + string userKeysFile = configKeys.getFileName(true); + + bool result = removeFile(userKeysFile.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] delete file [%s] returned %d\n", + __FILE__, __FUNCTION__, __LINE__, userKeysFile.c_str(), + result); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] delete file [%s] returned %d\n", + __FILE__, __FUNCTION__, __LINE__, + userKeysFile.c_str(), result); + configKeys.reload(); + + if (this->parentUI != NULL) { + // Set the parent pointer to NULL so the owner knows it was deleted + *this->parentUI = NULL; + // Delete the main menu + delete mainMenu; + return; + } + + mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (buttonOk.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundB()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (userProperties.empty() == false) { + Config & config = Config::getInstance(); + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys), + std::pair < string, + string >(Config::glestkeys_ini_filename, + Config:: + glestuserkeys_ini_filename), + std::pair < bool, bool >(true, false), + config.getString("GlestKeysIniPath", "")); + string userKeysFile = configKeys.getFileName(true); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] save file [%s] userProperties.size() = " + MG_SIZE_T_SPECIFIER "\n", __FILE__, __FUNCTION__, __LINE__, + userKeysFile.c_str(), userProperties.size()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] save file [%s] userProperties.size() = " + MG_SIZE_T_SPECIFIER "\n", __FILE__, + __FUNCTION__, __LINE__, + userKeysFile.c_str(), + userProperties.size()); + + configKeys.setUserProperties(userProperties); + configKeys.save(); + configKeys.reload(); + } + + Lang & lang = Lang::getInstance(); + console.addLine(lang.getString("SettingsSaved")); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (keyScrollBar.getElementCount() != 0) { + for (int i = keyScrollBar.getVisibleStart(); i + <= keyScrollBar.getVisibleEnd(); ++i) { + if (keyButtons[i]->mouseClick(x, y)) { + hotkeyIndex = i; + hotkeyChar = SDLK_UNKNOWN; + break; + } + } + } + + if (this->parentUI == NULL) { + if (buttonKeyboardSetup.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + return; + } else if (buttonAudioSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsSound(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonNetworkSettings.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonMiscSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptions(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonVideoSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } + } + } + + void MenuStateKeysetup::mouseUp(int x, int y, + const MouseButton mouseButton) { + if (mouseButton == mbLeft) { + keyScrollBar.mouseUp(x, y); + } + } + + void MenuStateKeysetup::mouseMove(int x, int y, const MouseState * ms) { + buttonReturn.mouseMove(x, y); + buttonOk.mouseMove(x, y); + if (this->parentUI == NULL) { + buttonKeyboardSetup.mouseMove(x, y); + buttonAudioSection.mouseMove(x, y); + buttonNetworkSettings.mouseMove(x, y); + buttonMiscSection.mouseMove(x, y); + buttonVideoSection.mouseMove(x, y); + } + if (ms->get(mbLeft)) { + keyScrollBar.mouseDown(x, y); + } else { + keyScrollBar.mouseMove(x, y); + } + + if (keyScrollBar.getElementCount() != 0) { + for (int i = keyScrollBar.getVisibleStart(); + i <= keyScrollBar.getVisibleEnd(); ++i) { + keyButtons[i]->mouseMove(x, y); + } + } + + } + + void MenuStateKeysetup::render() { + Renderer & renderer = Renderer::getInstance(); + + //printf("MenuStateKeysetup::render A\n"); + + if (mainMessageBox.getEnabled()) { + //printf("MenuStateKeysetup::render B\n"); + renderer.renderMessageBox(&mainMessageBox); + } else { + //printf("MenuStateKeysetup::render C\n"); + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonDefaults); + renderer.renderButton(&buttonOk); + if (this->parentUI == NULL) { + renderer.renderButton(&buttonKeyboardSetup); + renderer.renderButton(&buttonVideoSection); + renderer.renderButton(&buttonAudioSection); + renderer.renderButton(&buttonMiscSection); + renderer.renderButton(&buttonNetworkSettings); + } + renderer.renderLabel(&labelTitle); + renderer.renderLabel(&labelTestTitle); + renderer.renderLabel(&labelTestValue); + + if (keyScrollBar.getElementCount() != 0) { + for (int i = keyScrollBar.getVisibleStart(); + i <= keyScrollBar.getVisibleEnd(); ++i) { + if (hotkeyIndex == i) { + renderer.renderButton(keyButtons[i], &YELLOW); + } else { + renderer.renderButton(keyButtons[i]); + } + renderer.renderLabel(labels[i]); + } + } + renderer.renderScrollBar(&keyScrollBar); + } + + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } + + void MenuStateKeysetup::update() { + //printf("MenuStateKeysetup::update A\n"); + + if (keyScrollBar.getElementCount() != 0) { + for (int i = keyScrollBar.getVisibleStart(); i + <= keyScrollBar.getVisibleEnd(); ++i) { + keyButtons[i]->setY(keyButtonsYBase - keyButtonsLineHeight * (i + - + keyScrollBar. + getVisibleStart + ())); + labels[i]->setY(keyButtonsYBase - + keyButtonsLineHeight * (i - + keyScrollBar. + getVisibleStart())); + } + } + + console.update(); + } + + + + void MenuStateKeysetup::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + + void MenuStateKeysetup::keyDown(SDL_KeyboardEvent key) { + hotkeyChar = extractKeyPressed(key); + //printf("\nkeyDown [%d]\n",hotkeyChar); + + string keyName = ""; + //if(hotkeyChar > SDLK_UNKNOWN && hotkeyChar < SDL_NUM_SCANCODES) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", __FILE__, + __FUNCTION__, __LINE__, keyName.c_str(), hotkeyChar, + key.keysym.sym); + keyName = SDL_GetKeyName(hotkeyChar); + //printf ("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n",__FILE__,__FUNCTION__,__LINE__,keyName.c_str(),hotkeyChar,key.keysym.sym); + //} + //key = hotkeyChar; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", __FILE__, + __FUNCTION__, __LINE__, keyName.c_str(), hotkeyChar, + key.keysym.sym); + + // SDLKey keysym = SDLK_UNKNOWN; + // if(keyName == "unknown key" || keyName == "") { + // Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); + // keysym = configKeys.translateSpecialStringToSDLKey(hotkeyChar); + // + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] keysym [%d]\n",__FILE__,__FUNCTION__,__LINE__,keysym); + // + // // SDL skips capital letters + // if(keysym >= 65 && keysym <= 90) { + // keysym = (SDLKey)((int)keysym + 32); + // } + // //if(keysym < 255) { + // // key = keysym; + // //} + // keyName = SDL_GetKeyName(keysym); + // } + + char szCharText[20] = ""; + snprintf(szCharText, 20, "%c", hotkeyChar); + char *utfStr = ConvertToUTF8(&szCharText[0]); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, " %s [%s][%d][%d][%d]", keyName.c_str(), + utfStr, key.keysym.sym, hotkeyChar, key.keysym.mod); + labelTestValue.setText(szBuf); + //printf ("In [%s::%s Line: %d] szBuf [%s]\n",__FILE__,__FUNCTION__,__LINE__,szBuf); + + delete[]utfStr; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] hotkeyChar [%d]\n", __FILE__, + __FUNCTION__, __LINE__, hotkeyChar); + } + + void MenuStateKeysetup::keyPress(SDL_KeyboardEvent c) { + } + + void MenuStateKeysetup::keyUp(SDL_KeyboardEvent key) { + //Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); + + if (hotkeyIndex >= 0) { + if (hotkeyChar != 0) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] char [%d][%d]\n", __FILE__, + __FUNCTION__, __LINE__, hotkeyChar, key.keysym.sym); + + //string keyName = ""; + //if(hotkeyChar > SDLK_UNKNOWN && hotkeyChar < SDL_NUM_SCANCODES) { + + string keyName = SDL_GetKeyName(key.keysym.sym); + if (StartsWith(keyName, "Keypad ") == false) { + keyName = SDL_GetKeyName(hotkeyChar); + key.keysym.sym = hotkeyChar; + } + //} + //key.keysym.sym = hotkeyChar; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", + __FILE__, __FUNCTION__, __LINE__, keyName.c_str(), + hotkeyChar, key.keysym.sym); + + //SDLKey keysym = SDLK_UNKNOWN; + // if(keyName == "unknown key" || keyName == "") { + // Config &configKeys = Config::getInstance(std::pair(cfgMainKeys,cfgUserKeys)); + // keysym = configKeys.translateSpecialStringToSDLKey(hotkeyChar); + // + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf ("In [%s::%s Line: %d] keysym [%d]\n",__FILE__,__FUNCTION__,__LINE__,keysym); + // + // // SDL skips capital letters + // if(keysym >= 65 && keysym <= 90) { + // keysym = (SDLKey)((int)keysym + 32); + // } + // if(keysym < 255) { + // key = keysym; + // } + // keyName = SDL_GetKeyName(keysym); + // } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] keyName [%s] char [%d][%d]\n", + __FILE__, __FUNCTION__, __LINE__, keyName.c_str(), + hotkeyChar, key.keysym.sym); + + if (keyName != "unknown key") { + GraphicLabel *label = labels[hotkeyIndex]; + label->setText(keyName); + + pair < string, string > &nameValuePair = + mergedProperties[hotkeyIndex]; + + // Need to distinguish numeric keys to be translated to real keys + // from these ACTUAL sdl keys so surround in quotes. + //printf("KeyUp #1 keyName [%s]\n", keyName.c_str()); + + if (keyName.size() == 1 && keyName[0] >= '0' + && keyName[0] <= '9') { + keyName = "'" + keyName + "'"; + } + //printf("KeyUp #2 keyName [%s]\n", keyName.c_str()); + + bool isNewUserKeyEntry = true; + for (int i = 0; i < (int) userProperties.size(); ++i) { + string hotKeyName = userProperties[i].first; + if (nameValuePair.first == hotKeyName) { + // if(keysym <= SDLK_ESCAPE || keysym > 255) { + // if(keysym <= SDLK_ESCAPE) { + // userProperties[i].second = intToStr(extractKeyPressed(key)); + // } + // else { + // userProperties[i].second = keyName; + // } + // } + // else { + // userProperties[i].second = ""; + // userProperties[i].second.push_back(extractKeyPressed(key)); + // } + userProperties[i].second = keyName; + isNewUserKeyEntry = false; + break; + } + } + if (isNewUserKeyEntry == true) { + pair < string, string > newNameValuePair = nameValuePair; + // if(keysym <= SDLK_ESCAPE || keysym > 255) { + // if(keysym <= SDLK_ESCAPE) { + // newNameValuePair.second = intToStr(extractKeyPressed(key)); + // } + // else { + // newNameValuePair.second = keyName; + // } + // } + // else { + // newNameValuePair.second = extractKeyPressed(key); + // } + newNameValuePair.second = keyName; + userProperties.push_back(newNameValuePair); + } + } + } + hotkeyIndex = -1; + hotkeyChar = SDLK_UNKNOWN; + } + } + + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_keysetup.h b/source/glest_game/menu/menu_state_keysetup.h index 60e3a1fbf..e41d00e9e 100644 --- a/source/glest_game/menu/menu_state_keysetup.h +++ b/source/glest_game/menu/menu_state_keysetup.h @@ -16,91 +16,88 @@ # include "server_line.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class -// =============================== - typedef vector < GraphicButton * >UserButtons; - typedef vector < GraphicLabel * >GraphicLabels; + // =============================== + // class + // =============================== + typedef vector < GraphicButton * >UserButtons; + typedef vector < GraphicLabel * >GraphicLabels; - class MenuStateKeysetup:public MenuState - { + class MenuStateKeysetup :public MenuState { - private: + private: - GraphicButton buttonOk; - GraphicButton buttonDefaults; - GraphicButton buttonReturn; + GraphicButton buttonOk; + GraphicButton buttonDefaults; + GraphicButton buttonReturn; - GraphicButton buttonKeyboardSetup; // configure the keyboard - GraphicButton buttonVideoSection; - GraphicButton buttonAudioSection; - GraphicButton buttonMiscSection; - GraphicButton buttonNetworkSettings; + GraphicButton buttonKeyboardSetup; // configure the keyboard + GraphicButton buttonVideoSection; + GraphicButton buttonAudioSection; + GraphicButton buttonMiscSection; + GraphicButton buttonNetworkSettings; - GraphicLabel labelTitle; + GraphicLabel labelTitle; - GraphicScrollBar keyScrollBar; - UserButtons keyButtons; - GraphicLabels labels; - int keyButtonsToRender; - int keyButtonsYBase; - int keyButtonsXBase; - int keyButtonsLineHeight; - int keyButtonsHeight; - int keyButtonsWidth; + GraphicScrollBar keyScrollBar; + UserButtons keyButtons; + GraphicLabels labels; + int keyButtonsToRender; + int keyButtonsYBase; + int keyButtonsXBase; + int keyButtonsLineHeight; + int keyButtonsHeight; + int keyButtonsWidth; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; - vector < pair < string, string > >mergedProperties; - vector < pair < string, string > >masterProperties; - vector < pair < string, string > >userProperties; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; + vector < pair < string, string > >mergedProperties; + vector < pair < string, string > >masterProperties; + vector < pair < string, string > >userProperties; - int hotkeyIndex; - //char hotkeyChar; - SDL_Keycode hotkeyChar; + int hotkeyIndex; + //char hotkeyChar; + SDL_Keycode hotkeyChar; - GraphicLabel labelTestTitle; - GraphicLabel labelTestValue; + GraphicLabel labelTestTitle; + GraphicLabel labelTestValue; - ProgramState **parentUI; + ProgramState **parentUI; - public: - MenuStateKeysetup (Program * program, MainMenu * mainMenu, - ProgramState ** parentUI = NULL); - virtual ~ MenuStateKeysetup (); + public: + MenuStateKeysetup(Program * program, MainMenu * mainMenu, + ProgramState ** parentUI = NULL); + virtual ~MenuStateKeysetup(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseUp (int x, int y, const MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void update (); - void render (); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseUp(int x, int y, const MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void update(); + void render(); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - virtual void keyUp (SDL_KeyboardEvent key); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + virtual void keyUp(SDL_KeyboardEvent key); - virtual bool isInSpecialKeyCaptureEvent () - { - return true; - } + virtual bool isInSpecialKeyCaptureEvent() { + return true; + } - //static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; } + //static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; } - void reloadUI (); + void reloadUI(); - private: - void showMessageBox (const string & text, const string & header, - bool toggle); - void clearUserButtons (); - void cleanup (); - }; + private: + void showMessageBox(const string & text, const string & header, + bool toggle); + void clearUserButtons(); + void cleanup(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_load_game.cpp b/source/glest_game/menu/menu_state_load_game.cpp index 0ade8ab8b..66be69b07 100644 --- a/source/glest_game/menu/menu_state_load_game.cpp +++ b/source/glest_game/menu/menu_state_load_game.cpp @@ -23,609 +23,525 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class MenuStateLoadGame -// ===================================================== + // ===================================================== + // class MenuStateLoadGame + // ===================================================== - MenuStateLoadGame::MenuStateLoadGame (Program * program, - MainMenu * - mainMenu):MenuState (program, - mainMenu, - "root") - { - containerName = "LoadGame"; - Lang & lang = Lang::getInstance (); + MenuStateLoadGame::MenuStateLoadGame(Program * program, + MainMenu * + mainMenu) :MenuState(program, + mainMenu, + "root") { + containerName = "LoadGame"; + Lang & lang = Lang::getInstance(); - int buttonWidth = 120; - int yPos = 40; - int xPos = 20; - int xSpacing = 20; - int slotsToRender = 20; - int slotWidth = 200; + int buttonWidth = 120; + int yPos = 40; + int xPos = 20; + int xSpacing = 20; + int slotsToRender = 20; + int slotWidth = 200; - slotLinesYBase = 650; - slotsLineHeight = 30; - previewTexture = NULL; - buttonToDelete = NULL; + slotLinesYBase = 650; + slotsLineHeight = 30; + previewTexture = NULL; + buttonToDelete = NULL; - selectedButton = NULL; + selectedButton = NULL; - string userData = - Config::getInstance ().getString ("UserData_Root", ""); - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - userData = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey); - } - if (userData != "") - { - endPathWithSlash (userData); - } - saveGameDir = userData + "saved/"; + string userData = + Config::getInstance().getString("UserData_Root", ""); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + userData = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey); + } + if (userData != "") { + endPathWithSlash(userData); + } + saveGameDir = userData + "saved/"; - lines[0].init (0, slotLinesYBase + slotsLineHeight); - lines[1].init (0, - slotLinesYBase - (slotsToRender - 1) * slotsLineHeight - - 5); + lines[0].init(0, slotLinesYBase + slotsLineHeight); + lines[1].init(0, + slotLinesYBase - (slotsToRender - 1) * slotsLineHeight - + 5); - headerLabel.registerGraphicComponent (containerName, "headerLabel"); - headerLabel.init (400, 730); - headerLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - headerLabel.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - headerLabel.setText (lang.getString ("LoadGameMenu")); + headerLabel.registerGraphicComponent(containerName, "headerLabel"); + headerLabel.init(400, 730); + headerLabel.setFont(CoreData::getInstance().getMenuFontBig()); + headerLabel.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + headerLabel.setText(lang.getString("LoadGameMenu")); - noSavedGamesLabel.registerGraphicComponent (containerName, - "noSavedGamesLabel"); - noSavedGamesLabel.init (20, 400); - noSavedGamesLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - noSavedGamesLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - noSavedGamesLabel.setText (lang.getString ("NoSavedGames")); + noSavedGamesLabel.registerGraphicComponent(containerName, + "noSavedGamesLabel"); + noSavedGamesLabel.init(20, 400); + noSavedGamesLabel.setFont(CoreData::getInstance().getMenuFontBig()); + noSavedGamesLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + noSavedGamesLabel.setText(lang.getString("NoSavedGames")); - savedGamesLabel.registerGraphicComponent (containerName, - "savedGamesLabel"); - savedGamesLabel.init (150, slotLinesYBase + slotsLineHeight + 10); - savedGamesLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - savedGamesLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - savedGamesLabel.setText (lang.getString ("SavedGames")); + savedGamesLabel.registerGraphicComponent(containerName, + "savedGamesLabel"); + savedGamesLabel.init(150, slotLinesYBase + slotsLineHeight + 10); + savedGamesLabel.setFont(CoreData::getInstance().getMenuFontBig()); + savedGamesLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + savedGamesLabel.setText(lang.getString("SavedGames")); - infoHeaderLabel.registerGraphicComponent (containerName, - "infoHeaderLabel"); - infoHeaderLabel.init (600, slotLinesYBase + slotsLineHeight + 10); - infoHeaderLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - infoHeaderLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - infoHeaderLabel.setText (lang.getString ("SavegameInfo")); + infoHeaderLabel.registerGraphicComponent(containerName, + "infoHeaderLabel"); + infoHeaderLabel.init(600, slotLinesYBase + slotsLineHeight + 10); + infoHeaderLabel.setFont(CoreData::getInstance().getMenuFontBig()); + infoHeaderLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + infoHeaderLabel.setText(lang.getString("SavegameInfo")); - versionWarningLabel.registerGraphicComponent (containerName, - "versionWarningLabel"); - versionWarningLabel.init (550, 350); - versionWarningLabel.setText (""); - versionWarningLabel.setTextColor (Vec3f (1.0f, 0.5f, 0.5f)); + versionWarningLabel.registerGraphicComponent(containerName, + "versionWarningLabel"); + versionWarningLabel.init(550, 350); + versionWarningLabel.setText(""); + versionWarningLabel.setTextColor(Vec3f(1.0f, 0.5f, 0.5f)); - infoTextLabel.registerGraphicComponent (containerName, "infoTextLabel"); - infoTextLabel.init (550, 310); - infoTextLabel.setText (""); + infoTextLabel.registerGraphicComponent(containerName, "infoTextLabel"); + infoTextLabel.init(550, 310); + infoTextLabel.setText(""); - abortButton.registerGraphicComponent (containerName, "abortButton"); - abortButton.init (xPos, yPos, buttonWidth); - abortButton.setText (lang.getString ("Abort")); - xPos += buttonWidth + xSpacing; - loadButton.registerGraphicComponent (containerName, "loadButton"); - loadButton.init (xPos, yPos, buttonWidth + 80); - loadButton.setText (lang.getString ("LoadGame")); - xPos += buttonWidth + 80 + xSpacing; - deleteButton.registerGraphicComponent (containerName, "deleteButton"); - deleteButton.init (xPos, yPos, buttonWidth); - deleteButton.setText (lang.getString ("Delete")); + abortButton.registerGraphicComponent(containerName, "abortButton"); + abortButton.init(xPos, yPos, buttonWidth); + abortButton.setText(lang.getString("Abort")); + xPos += buttonWidth + xSpacing; + loadButton.registerGraphicComponent(containerName, "loadButton"); + loadButton.init(xPos, yPos, buttonWidth + 80); + loadButton.setText(lang.getString("LoadGame")); + xPos += buttonWidth + 80 + xSpacing; + deleteButton.registerGraphicComponent(containerName, "deleteButton"); + deleteButton.init(xPos, yPos, buttonWidth); + deleteButton.setText(lang.getString("Delete")); - slotsScrollBar.init (500 - 20, - slotLinesYBase - slotsLineHeight * (slotsToRender - - 1), false, - slotWidth, 20); - slotsScrollBar.setLength (slotsLineHeight * slotsToRender); - slotsScrollBar.setElementCount (0); - slotsScrollBar.setVisibleSize (slotsToRender); - slotsScrollBar.setVisibleStart (0); + slotsScrollBar.init(500 - 20, + slotLinesYBase - slotsLineHeight * (slotsToRender - + 1), false, + slotWidth, 20); + slotsScrollBar.setLength(slotsLineHeight * slotsToRender); + slotsScrollBar.setElementCount(0); + slotsScrollBar.setVisibleSize(slotsToRender); + slotsScrollBar.setVisibleStart(0); - listFiles (); - slotsScrollBar.setElementCount ((int) filenames.size ()); + listFiles(); + slotsScrollBar.setElementCount((int) filenames.size()); - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Ok"), 450); - mainMessageBox.setEnabled (false); + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Ok"), 450); + mainMessageBox.setEnabled(false); - GraphicComponent::applyAllCustomProperties (containerName); - } + GraphicComponent::applyAllCustomProperties(containerName); + } - MenuStateLoadGame::~MenuStateLoadGame () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - clearSlots (); + MenuStateLoadGame::~MenuStateLoadGame() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + clearSlots(); - cleanupTexture (&previewTexture); + cleanupTexture(&previewTexture); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] END\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] END\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } - void MenuStateLoadGame::cleanupTexture (Texture2D ** texture) - { - if (texture != NULL && *texture != NULL) - { - (*texture)->end (); - delete *texture; - *texture = NULL; - } - } + void MenuStateLoadGame::cleanupTexture(Texture2D ** texture) { + if (texture != NULL && *texture != NULL) { + (*texture)->end(); + delete *texture; + *texture = NULL; + } + } - void MenuStateLoadGame::clearSlots () - { - while (!slots.empty ()) - { - delete slots.back (); - slots.pop_back (); - slotsGB.pop_back (); - } - } + void MenuStateLoadGame::clearSlots() { + while (!slots.empty()) { + delete slots.back(); + slots.pop_back(); + slotsGB.pop_back(); + } + } - void MenuStateLoadGame::listFiles () - { - int keyButtonsXBase = 2; - int keyButtonsYBase = slotLinesYBase; - int keyButtonsWidth = 476; - int keyButtonsHeight = slotsLineHeight; + void MenuStateLoadGame::listFiles() { + int keyButtonsXBase = 2; + int keyButtonsYBase = slotLinesYBase; + int keyButtonsWidth = 476; + int keyButtonsHeight = slotsLineHeight; - clearSlots (); - // Save the file now - vector < string > paths; - paths.push_back (saveGameDir); - filenames.clear (); - findAll (paths, "*.xml", filenames, true, false, true); - sort (filenames.begin (), filenames.end ()); - for (int i = (int)filenames.size () - 1; i > -1; i--) - { - GraphicButton *button = new GraphicButton (); - button->init (keyButtonsXBase, keyButtonsYBase, keyButtonsWidth, - keyButtonsHeight); - button->setText (filenames[i]); + clearSlots(); + // Save the file now + vector < string > paths; + paths.push_back(saveGameDir); + filenames.clear(); + findAll(paths, "*.xml", filenames, true, false, true); + sort(filenames.begin(), filenames.end()); + for (int i = (int) filenames.size() - 1; i > -1; i--) { + GraphicButton *button = new GraphicButton(); + button->init(keyButtonsXBase, keyButtonsYBase, keyButtonsWidth, + keyButtonsHeight); + button->setText(filenames[i]); - slots.push_back (button); - slotsGB.push_back (button); - } - } + slots.push_back(button); + slotsGB.push_back(button); + } + } - void MenuStateLoadGame::reloadUI () - { - Lang & lang = Lang::getInstance (); + void MenuStateLoadGame::reloadUI() { + Lang & lang = Lang::getInstance(); - infoHeaderLabel.setText (lang.getString ("SavegameInfo")); - savedGamesLabel.setText (lang.getString ("SavedGames")); - noSavedGamesLabel.setText (lang.getString ("NoSavedGames")); - headerLabel.setText (lang.getString ("LoadGameMenu")); + infoHeaderLabel.setText(lang.getString("SavegameInfo")); + savedGamesLabel.setText(lang.getString("SavedGames")); + noSavedGamesLabel.setText(lang.getString("NoSavedGames")); + headerLabel.setText(lang.getString("LoadGameMenu")); - abortButton.setText (lang.getString ("Abort")); - deleteButton.setText (lang.getString ("Delete")); - loadButton.setText (lang.getString ("LoadGame")); + abortButton.setText(lang.getString("Abort")); + deleteButton.setText(lang.getString("Delete")); + loadButton.setText(lang.getString("LoadGame")); - mainMessageBox.init (lang.getString ("Ok"), 450); + mainMessageBox.init(lang.getString("Ok"), 450); - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } - void MenuStateLoadGame::mouseClick (int x, int y, MouseButton mouseButton) - { + void MenuStateLoadGame::mouseClick(int x, int y, MouseButton mouseButton) { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); - if (mainMessageBox.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - mainMessageBox.setEnabled (false); + if (mainMessageBox.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundA()); + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + mainMessageBox.setEnabled(false); - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok"), 450); - } - } - if (abortButton.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - } - else if (deleteButton.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedButton == NULL) - { - console.addStdMessage ("NothingSelected", true); - } - else - { - string slotname = selectedButton->getText (); - string filename = saveGameDir + selectedButton->getText () + ".xml"; - string jpgfilename = - saveGameDir + selectedButton->getText () + ".xml.jpg"; - string replayfilename = - saveGameDir + selectedButton->getText () + ".xml.replay"; + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok"), 450); + } + } + if (abortButton.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + } else if (deleteButton.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedButton == NULL) { + console.addStdMessage("NothingSelected", true); + } else { + string slotname = selectedButton->getText(); + string filename = saveGameDir + selectedButton->getText() + ".xml"; + string jpgfilename = + saveGameDir + selectedButton->getText() + ".xml.jpg"; + string replayfilename = + saveGameDir + selectedButton->getText() + ".xml.replay"; - Lang & lang = Lang::getInstance (); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("LoadGameDeletingFile", "", - true).c_str (), filename.c_str ()); - console.addLineOnly (szBuf); + Lang & lang = Lang::getInstance(); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("LoadGameDeletingFile", "", + true).c_str(), filename.c_str()); + console.addLineOnly(szBuf); - for (int i = 0; i < (int) slots.size (); i++) - { - if (slots[i] == selectedButton) - { - if (removeFile (filename) == true) - { - removeFile (jpgfilename); - removeFile (replayfilename); - cleanupTexture (&previewTexture); + for (int i = 0; i < (int) slots.size(); i++) { + if (slots[i] == selectedButton) { + if (removeFile(filename) == true) { + removeFile(jpgfilename); + removeFile(replayfilename); + cleanupTexture(&previewTexture); - infoTextLabel.setText (""); - listFiles (); - slotsScrollBar.setElementCount ((int) filenames.size ()); + infoTextLabel.setText(""); + listFiles(); + slotsScrollBar.setElementCount((int) filenames.size()); - selectedButton = NULL; - } - break; - } - } - } - } - else if (loadButton.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); + selectedButton = NULL; + } + break; + } + } + } + } else if (loadButton.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); - if (selectedButton == NULL) - { - console.addStdMessage ("NothingSelected", true); - } - else - { - string filename = saveGameDir + selectedButton->getText () + ".xml"; + if (selectedButton == NULL) { + console.addStdMessage("NothingSelected", true); + } else { + string filename = saveGameDir + selectedButton->getText() + ".xml"; - Lang & lang = Lang::getInstance (); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("LoadGameLoadingFile", "", true).c_str (), - filename.c_str ()); - console.addLineOnly (szBuf); + Lang & lang = Lang::getInstance(); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("LoadGameLoadingFile", "", true).c_str(), + filename.c_str()); + console.addLineOnly(szBuf); - try - { - Game::loadGame (filename, program, false); - } - catch (const megaglest_runtime_error & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); + try { + Game::loadGame(filename, program, false); + } catch (const megaglest_runtime_error & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); - showMessageBox (ex.what (), lang.getString ("Notice"), false); - } - return; - } - } - else if (slotsScrollBar.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - } - else - { - if (slotsScrollBar.getElementCount () != 0) - { - for (int i = slotsScrollBar.getVisibleStart (); - i <= slotsScrollBar.getVisibleEnd (); ++i) - { - if (slots[i]->mouseClick (x, y) && selectedButton != slots[i]) - { - soundRenderer.playFx (coreData.getClickSoundB ()); + showMessageBox(ex.what(), lang.getString("Notice"), false); + } + return; + } + } else if (slotsScrollBar.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + } else { + if (slotsScrollBar.getElementCount() != 0) { + for (int i = slotsScrollBar.getVisibleStart(); + i <= slotsScrollBar.getVisibleEnd(); ++i) { + if (slots[i]->mouseClick(x, y) && selectedButton != slots[i]) { + soundRenderer.playFx(coreData.getClickSoundB()); - Lang & lang = Lang::getInstance (); - cleanupTexture (&previewTexture); - selectedButton = slots[i]; - string filename = - saveGameDir + selectedButton->getText () + ".xml"; - string screenShotFilename = filename + ".jpg"; - if (fileExists (screenShotFilename) == true) - { - try - { - previewTexture = - GraphicsInterface::getInstance ().getFactory ()-> - newTexture2D (); - if (previewTexture) - { - previewTexture->setMipmap (true); - previewTexture->load (screenShotFilename); - previewTexture->init (); - } - } - catch (const megaglest_runtime_error & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); + Lang & lang = Lang::getInstance(); + cleanupTexture(&previewTexture); + selectedButton = slots[i]; + string filename = + saveGameDir + selectedButton->getText() + ".xml"; + string screenShotFilename = filename + ".jpg"; + if (fileExists(screenShotFilename) == true) { + try { + previewTexture = + GraphicsInterface::getInstance().getFactory()-> + newTexture2D(); + if (previewTexture) { + previewTexture->setMipmap(true); + previewTexture->load(screenShotFilename); + previewTexture->init(); + } + } catch (const megaglest_runtime_error & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); - cleanupTexture (&previewTexture); - showMessageBox (ex.what (), lang.getString ("Notice"), - false); - } - } - else - { - previewTexture = NULL; - } + cleanupTexture(&previewTexture); + showMessageBox(ex.what(), lang.getString("Notice"), + false); + } + } else { + previewTexture = NULL; + } - if (fileExists (filename) == true) - { - // Xerces is infinitely slower than rapidxml - xml_engine_parser_type engine_type = XML_RAPIDXML_ENGINE; + if (fileExists(filename) == true) { + // Xerces is infinitely slower than rapidxml + xml_engine_parser_type engine_type = XML_RAPIDXML_ENGINE; #if defined(WANT_XERCES) - if (Config::getInstance (). - getBool ("ForceXMLLoadGameUsingXerces", "false") == true) - { - engine_type = XML_XERCES_ENGINE; - } + if (Config::getInstance(). + getBool("ForceXMLLoadGameUsingXerces", "false") == true) { + engine_type = XML_XERCES_ENGINE; + } #endif - XmlTree xmlTree (engine_type); + XmlTree xmlTree(engine_type); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Before load of XML\n"); - std::map < string, string > mapExtraTagReplacementValues; - try - { - xmlTree.load (filename, - Properties:: - getTagReplacementValues - (&mapExtraTagReplacementValues), true, false, - true); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Before load of XML\n"); + std::map < string, string > mapExtraTagReplacementValues; + try { + xmlTree.load(filename, + Properties:: + getTagReplacementValues + (&mapExtraTagReplacementValues), true, false, + true); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("After load of XML\n"); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("After load of XML\n"); - const XmlNode *rootNode = xmlTree.getRootNode (); - if (rootNode != NULL - && rootNode->hasChild ("zetaglest-saved-game") == true) - { - rootNode = rootNode->getChild ("zetaglest-saved-game"); - } + const XmlNode *rootNode = xmlTree.getRootNode(); + if (rootNode != NULL + && rootNode->hasChild("zetaglest-saved-game") == true) { + rootNode = rootNode->getChild("zetaglest-saved-game"); + } - if (rootNode == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Invalid XML saved game file: [%s]", - filename.c_str ()); - infoTextLabel.setText (szBuf); - return; - } + if (rootNode == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Invalid XML saved game file: [%s]", + filename.c_str()); + infoTextLabel.setText(szBuf); + return; + } - const XmlNode *versionNode = rootNode; - string gameVer = - versionNode->getAttribute ("version")->getValue (); - if (gameVer != glestVersionString - && checkVersionComptability (gameVer, - glestVersionString) == - false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("SavedGameBadVersion").c_str (), - gameVer.c_str (), glestVersionString.c_str ()); - versionWarningLabel.setText (szBuf); - } - else - { - versionWarningLabel.setText (""); - } - XmlNode *gameNode = rootNode->getChild ("Game"); - GameSettings newGameSettings; - newGameSettings.loadGame (gameNode); + const XmlNode *versionNode = rootNode; + string gameVer = + versionNode->getAttribute("version")->getValue(); + if (gameVer != glestVersionString + && checkVersionComptability(gameVer, + glestVersionString) == + false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("SavedGameBadVersion").c_str(), + gameVer.c_str(), glestVersionString.c_str()); + versionWarningLabel.setText(szBuf); + } else { + versionWarningLabel.setText(""); + } + XmlNode *gameNode = rootNode->getChild("Game"); + GameSettings newGameSettings; + newGameSettings.loadGame(gameNode); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("LoadSavedGameInfo").c_str (), - newGameSettings.getMap ().c_str (), - newGameSettings.getTileset ().c_str (), - newGameSettings.getTech ().c_str (), - newGameSettings.getScenario ().c_str (), - newGameSettings.getFactionCount (), - (newGameSettings.getThisFactionIndex () >= 0 - && newGameSettings.getThisFactionIndex () < - newGameSettings. - getFactionCount ()? newGameSettings. - getFactionTypeName (newGameSettings. - getThisFactionIndex ()). - c_str () : "")); - infoTextLabel.setText (szBuf); - } - catch (const megaglest_runtime_error & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("LoadSavedGameInfo").c_str(), + newGameSettings.getMap().c_str(), + newGameSettings.getTileset().c_str(), + newGameSettings.getTech().c_str(), + newGameSettings.getScenario().c_str(), + newGameSettings.getFactionCount(), + (newGameSettings.getThisFactionIndex() >= 0 + && newGameSettings.getThisFactionIndex() < + newGameSettings. + getFactionCount() ? newGameSettings. + getFactionTypeName(newGameSettings. + getThisFactionIndex()). + c_str() : "")); + infoTextLabel.setText(szBuf); + } catch (const megaglest_runtime_error & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); - showMessageBox (ex.what (), lang.getString ("Notice"), - false); - } - } - else - { - infoTextLabel.setText (""); - } + showMessageBox(ex.what(), lang.getString("Notice"), + false); + } + } else { + infoTextLabel.setText(""); + } - break; - } - } - } - } - } + break; + } + } + } + } + } - void MenuStateLoadGame::mouseUp (int x, int y, - const MouseButton mouseButton) - { - if (mouseButton == mbLeft) - { - slotsScrollBar.mouseUp (x, y); - } - } + void MenuStateLoadGame::mouseUp(int x, int y, + const MouseButton mouseButton) { + if (mouseButton == mbLeft) { + slotsScrollBar.mouseUp(x, y); + } + } - void MenuStateLoadGame::mouseMove (int x, int y, const MouseState * ms) - { - abortButton.mouseMove (x, y); - deleteButton.mouseMove (x, y); - loadButton.mouseMove (x, y); - if (slotsScrollBar.getElementCount () != 0) - { - for (int i = slotsScrollBar.getVisibleStart (); - i <= slotsScrollBar.getVisibleEnd (); ++i) - { - slots[i]->mouseMove (x, y); - } - } - slotsScrollBar.mouseMove (x, y); - } + void MenuStateLoadGame::mouseMove(int x, int y, const MouseState * ms) { + abortButton.mouseMove(x, y); + deleteButton.mouseMove(x, y); + loadButton.mouseMove(x, y); + if (slotsScrollBar.getElementCount() != 0) { + for (int i = slotsScrollBar.getVisibleStart(); + i <= slotsScrollBar.getVisibleEnd(); ++i) { + slots[i]->mouseMove(x, y); + } + } + slotsScrollBar.mouseMove(x, y); + } - void MenuStateLoadGame::render () - { - Renderer & renderer = Renderer::getInstance (); + void MenuStateLoadGame::render() { + Renderer & renderer = Renderer::getInstance(); - renderer.renderLabel (&headerLabel); - renderer.renderLabel (&savedGamesLabel); - renderer.renderLabel (&infoHeaderLabel); - renderer.renderLabel (&infoTextLabel); - if (versionWarningLabel.getText () != "") - renderer.renderLabel (&versionWarningLabel); + renderer.renderLabel(&headerLabel); + renderer.renderLabel(&savedGamesLabel); + renderer.renderLabel(&infoHeaderLabel); + renderer.renderLabel(&infoTextLabel); + if (versionWarningLabel.getText() != "") + renderer.renderLabel(&versionWarningLabel); - renderer.renderButton (&abortButton); - renderer.renderButton (&deleteButton); - renderer.renderButton (&loadButton); + renderer.renderButton(&abortButton); + renderer.renderButton(&deleteButton); + renderer.renderButton(&loadButton); - for (int i = 0; i < (int) sizeof (lines) / (int) sizeof (lines[0]); ++i) - { - renderer.renderLine (&lines[i]); - } + for (int i = 0; i < (int) sizeof(lines) / (int) sizeof(lines[0]); ++i) { + renderer.renderLine(&lines[i]); + } - if (slotsScrollBar.getElementCount () == 0) - { - renderer.renderLabel (&noSavedGamesLabel); - } - else - { - for (int i = slotsScrollBar.getVisibleStart (); - i <= slotsScrollBar.getVisibleEnd (); ++i) - { - if (slots[i] == selectedButton) - { - bool lightedOverride = true; - renderer.renderButton (slots[i], &YELLOW, &lightedOverride); - } - else - { - renderer.renderButton (slots[i]); - } - } - } - renderer.renderScrollBar (&slotsScrollBar); + if (slotsScrollBar.getElementCount() == 0) { + renderer.renderLabel(&noSavedGamesLabel); + } else { + for (int i = slotsScrollBar.getVisibleStart(); + i <= slotsScrollBar.getVisibleEnd(); ++i) { + if (slots[i] == selectedButton) { + bool lightedOverride = true; + renderer.renderButton(slots[i], &YELLOW, &lightedOverride); + } else { + renderer.renderButton(slots[i]); + } + } + } + renderer.renderScrollBar(&slotsScrollBar); - if (previewTexture != NULL) - { - renderer.renderTextureQuad (550, - slotLinesYBase - 300 + slotsLineHeight, - 400, 300, previewTexture, 1.0f); - } + if (previewTexture != NULL) { + renderer.renderTextureQuad(550, + slotLinesYBase - 300 + slotsLineHeight, + 400, 300, previewTexture, 1.0f); + } - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } - void MenuStateLoadGame::update () - { - if (Config::getInstance ().getBool ("AutoTest")) - { - AutoTest::getInstance ().updateNewGame (program, mainMenu); - return; - } - slotsScrollBar.arrangeComponents (slotsGB); - console.update (); - } + void MenuStateLoadGame::update() { + if (Config::getInstance().getBool("AutoTest")) { + AutoTest::getInstance().updateNewGame(program, mainMenu); + return; + } + slotsScrollBar.arrangeComponents(slotsGB); + console.update(); + } - void MenuStateLoadGame::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - } - } + void MenuStateLoadGame::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + } + } - void MenuStateLoadGame::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (toggle == false) - { - mainMessageBox.setEnabled (false); - } + void MenuStateLoadGame::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (toggle == false) { + mainMessageBox.setEnabled(false); + } - if (mainMessageBox.getEnabled () == false) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } + if (mainMessageBox.getEnabled() == false) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } - } + } } //end namespace diff --git a/source/glest_game/menu/menu_state_load_game.h b/source/glest_game/menu/menu_state_load_game.h index 1d4c14028..6de689fb0 100644 --- a/source/glest_game/menu/menu_state_load_game.h +++ b/source/glest_game/menu/menu_state_load_game.h @@ -15,71 +15,69 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateLoadGame -// =============================== -//typedef vector SaveSlotButtons; - class MenuStateLoadGame:public MenuState - { - private: - GraphicButton loadButton; - GraphicButton deleteButton; - GraphicButton abortButton; - vector < GraphicButton * >slots; - vector < GraphicComponent * >slotsGB; - vector < string > filenames; - GraphicScrollBar slotsScrollBar; - GraphicButton *selectedButton; + // =============================== + // class MenuStateLoadGame + // =============================== + //typedef vector SaveSlotButtons; + class MenuStateLoadGame :public MenuState { + private: + GraphicButton loadButton; + GraphicButton deleteButton; + GraphicButton abortButton; + vector < GraphicButton * >slots; + vector < GraphicComponent * >slotsGB; + vector < string > filenames; + GraphicScrollBar slotsScrollBar; + GraphicButton *selectedButton; - GraphicButton *buttonToDelete; + GraphicButton *buttonToDelete; - Texture2D *previewTexture; + Texture2D *previewTexture; - GraphicLabel headerLabel; - GraphicLabel noSavedGamesLabel; - GraphicLabel savedGamesLabel; - GraphicLabel infoHeaderLabel; - GraphicLabel infoTextLabel; - GraphicLabel versionWarningLabel; + GraphicLabel headerLabel; + GraphicLabel noSavedGamesLabel; + GraphicLabel savedGamesLabel; + GraphicLabel infoHeaderLabel; + GraphicLabel infoTextLabel; + GraphicLabel versionWarningLabel; - GraphicLine lines[2]; + GraphicLine lines[2]; - GraphicMessageBox mainMessageBox; + GraphicMessageBox mainMessageBox; - string saveGameDir; - int slotLinesYBase; - int slotsLineHeight; + string saveGameDir; + int slotLinesYBase; + int slotsLineHeight; - public: - MenuStateLoadGame (Program * program, MainMenu * mainMenu); - ~MenuStateLoadGame (); + public: + MenuStateLoadGame(Program * program, MainMenu * mainMenu); + ~MenuStateLoadGame(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseUp (int x, int y, const MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void update (); - void render (); - virtual void keyDown (SDL_KeyboardEvent key); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseUp(int x, int y, const MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void update(); + void render(); + virtual void keyDown(SDL_KeyboardEvent key); - void reloadUI (); + void reloadUI(); - private: + private: - void clearSlots (); - void listFiles (); + void clearSlots(); + void listFiles(); - void showMessageBox (const string & text, const string & header, - bool toggle); + void showMessageBox(const string & text, const string & header, + bool toggle); - void cleanupTexture (Texture2D ** texture); - }; + void cleanupTexture(Texture2D ** texture); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 059ce3a03..bd07f3e20 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -41,1851 +41,1662 @@ #include "cache_manager.h" #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - DisplayMessageFunction MenuStateMasterserver::pCB_DisplayMessage = NULL; - - static string IRC_SERVER = "irc.freenode.net"; - static string IRC_CHANNEL = "#zetaglest-lobby"; - -// ===================================================== -// class MenuStateMasterserver -// ===================================================== - - MenuStateMasterserver::MenuStateMasterserver (Program * program, - MainMenu * - mainMenu):MenuState - (program, mainMenu, "masterserver"), - mutexIRCClient (new Mutex (CODE_AT_LINE)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("\n\n\n\n******************** ENTERING MASTERSERVER MENU\n\n\n\n\n"); - - containerName = "MasterServer"; - masterserverParseErrorShown = false; - updateFromMasterserverThread = NULL; - ircClient = NULL; - serverInfoString = "empty"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - - //Configure ConsolePosition - consoleIRC.setYPos (60); - consoleIRC.setFont (CoreData::getInstance ().getMenuFontNormal ()); - consoleIRC.setFont3D (CoreData::getInstance ().getMenuFontNormal3D ()); - consoleIRC.setLineHeight (18); - consoleIRC.setOnlyChatMessagesInStoredLines (false); - - serverLinesToRender = 8; - serverLinesLineHeight = 25; - serverLinesYBase = 680; - - userButtonsHeight = 20; - userButtonsWidth = 150; - userButtonsLineHeight = userButtonsHeight + 2; - userButtonsYBase = - serverLinesYBase - (serverLinesToRender) * serverLinesLineHeight - 90; - userButtonsToRender = userButtonsYBase / userButtonsLineHeight; - userButtonsXBase = 1000 - userButtonsWidth; - - userScrollBar.init (1000 - 20, 0, false, 200, 20); - userScrollBar.setLength (userButtonsYBase + userButtonsLineHeight); - userScrollBar.setElementCount (0); - userScrollBar.setVisibleSize (userButtonsToRender); - userScrollBar.setVisibleStart (0); - - userButtonsXBase = 1000 - userButtonsWidth - userScrollBar.getW (); - - serverScrollBar.init (1000 - 20, - serverLinesYBase - - serverLinesLineHeight * (serverLinesToRender - 1), - false, 200, 20); - serverScrollBar.setLength (serverLinesLineHeight * serverLinesToRender); - serverScrollBar.setElementCount (0); - serverScrollBar.setVisibleSize (serverLinesToRender); - serverScrollBar.setVisibleStart (0); - - lines[0].init (0, - userButtonsYBase + userButtonsLineHeight + - serverLinesLineHeight); - lines[1].init (userButtonsXBase - 5, 0, 5, - userButtonsYBase + 2 * userButtonsLineHeight); - lines[1].setHorizontal (false); - - autoRefreshTime = 0; - playServerFoundSound = false; - announcementLoaded = false; - - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Ok")); - mainMessageBox.setEnabled (false); - mainMessageBoxState = 0; - - lastRefreshTimer = time (NULL); - - // announcement - announcementLabel.registerGraphicComponent (containerName, - "announcementLabel"); - announcementLabel.init (10, 730); - announcementLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - announcementLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - announcementLabel.setText (""); - - // versionInfo - versionInfoLabel.registerGraphicComponent (containerName, - "versionInfoLabel"); - versionInfoLabel.init (10, 680); - versionInfoLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - versionInfoLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - versionInfoLabel.setText (""); - - // header - labelTitle.registerGraphicComponent (containerName, "labelTitle"); - labelTitle.init (410, serverLinesYBase + 45); - labelTitle.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTitle.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTitle.setText (lang.getString ("AvailableServers")); - - if (Config::getInstance ().getString ("Masterserver", "") == "") - { - labelTitle.setText ("*** " + lang.getString ("AvailableServers")); - } - - // bottom - int buttonPos = 230; - - // Titles for current games - START - int lineIndex = 0; - int lineOffset = 25 * lineIndex; - int i = 5; - int startOffset = serverLinesYBase + 23; - - //general info: - //i+=10; - glestVersionLabel.registerGraphicComponent (containerName, - "glestVersionLabel"); - glestVersionLabel.init (i, startOffset - lineOffset); - glestVersionLabel.setText (lang.getString ("MGVersion")); - - i += 80; - platformLabel.registerGraphicComponent (containerName, - "platformLabel"); - platformLabel.init (i + 15, startOffset - lineOffset); - platformLabel.setText (lang.getString ("MGPlatform")); - -// i+=50; -// binaryCompileDateLabel.registerGraphicComponent(containerName,"binaryCompileDateLabel"); -// binaryCompileDateLabel.init(i,startOffset-lineOffset); -// binaryCompileDateLabel.setText(lang.getString("MGBuildDateTime")); - - //game info: - i += 120; - serverTitleLabel.registerGraphicComponent (containerName, - "serverTitleLabel"); - serverTitleLabel.init (i, startOffset - lineOffset); - serverTitleLabel.setText (lang.getString ("MGGameTitle")); - - i += 170; - countryLabel.registerGraphicComponent (containerName, "countryLabel"); - countryLabel.init (i - 10, startOffset - lineOffset); - countryLabel.setText (lang.getString ("MGGameCountry")); - - i += 60; - -// ipAddressLabel.registerGraphicComponent(containerName,"ipAddressLabel"); -// ipAddressLabel.init(i,startOffset-lineOffset); -// ipAddressLabel.setText(lang.getString("MGGameIP")); -// i+=100; - - //game setup info: - techLabel.registerGraphicComponent (containerName, "techLabel"); - techLabel.init (i, startOffset - lineOffset); - techLabel.setText (lang.getString ("TechTree")); - - i += 165; - mapLabel.registerGraphicComponent (containerName, "mapLabel"); - mapLabel.init (i, startOffset - lineOffset); - mapLabel.setText (lang.getString ("Map")); - - i += 95; -// tilesetLabel.registerGraphicComponent(containerName,"tilesetLabel"); -// tilesetLabel.init(i,startOffset-lineOffset); -// tilesetLabel.setText(lang.getString("Tileset")); -// i+=100; - - activeSlotsLabel.registerGraphicComponent (containerName, - "activeSlotsLabel"); - activeSlotsLabel.init (i, startOffset - lineOffset); - activeSlotsLabel.setText (lang.getString ("MGGameSlots")); - - i += 50; - //externalConnectPort.registerGraphicComponent(containerName,"externalConnectPort"); - //externalConnectPort.init(i,startOffset-lineOffset); - //externalConnectPort.setText(lang.getString("Port")); - - i += 30; - statusLabel.registerGraphicComponent (containerName, "statusLabel"); - statusLabel.init (i + 5, startOffset - lineOffset); - statusLabel.setText (lang.getString ("MGGameStatus")); - - i += 130; - selectButton.registerGraphicComponent (containerName, "selectButton"); - selectButton.init (i - 5, startOffset - lineOffset); - selectButton.setText (lang.getString ("MGJoinGameSlots")); - - // Titles for current games - END - - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (50, buttonPos, 150); - - buttonCreateGame.registerGraphicComponent (containerName, - "buttonCreateGame"); - buttonCreateGame.init (275, buttonPos, 150); - - buttonRefresh.registerGraphicComponent (containerName, - "buttonRefresh"); - buttonRefresh.init (500, buttonPos, 150); - - buttonRefresh.setText (lang.getString ("RefreshList")); - buttonReturn.setText (lang.getString ("Return")); - buttonCreateGame.setText (lang.getString ("HostGame")); - labelAutoRefresh.setText (lang.getString ("AutoRefreshRate")); - - labelAutoRefresh.registerGraphicComponent (containerName, - "labelAutoRefresh"); - labelAutoRefresh.init (750, buttonPos + 30); - - listBoxAutoRefresh.registerGraphicComponent (containerName, - "listBoxAutoRefresh"); - listBoxAutoRefresh.init (750, buttonPos); - listBoxAutoRefresh.pushBackItem (lang.getString ("Off")); - listBoxAutoRefresh.pushBackItem ("10 s"); - listBoxAutoRefresh.pushBackItem ("20 s"); - listBoxAutoRefresh.pushBackItem ("30 s"); - listBoxAutoRefresh.setSelectedItemIndex (2); - autoRefreshTime = 10 * listBoxAutoRefresh.getSelectedItemIndex (); - - ircOnlinePeopleLabel.registerGraphicComponent (containerName, - "ircOnlinePeopleLabel"); - ircOnlinePeopleLabel.init (userButtonsXBase, - userButtonsYBase + userButtonsLineHeight); - ircOnlinePeopleLabel.setText (lang.getString ("IRCPeopleOnline")); - - ircOnlinePeopleStatusLabel.registerGraphicComponent (containerName, - "ircOnlinePeopleStatusLabel"); - ircOnlinePeopleStatusLabel.init (userButtonsXBase, - userButtonsYBase + - userButtonsLineHeight - 20); - ircOnlinePeopleStatusLabel.setText (""); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - NetworkManager::getInstance ().end (); - NetworkManager::getInstance ().init (nrClient); - - //console.addLine(lang.getString("ToSwitchOffMusicPress")+" - \""+configKeys.getCharKey("ToggleMusic")+"\""); - - GraphicComponent::applyAllCustomProperties (containerName); - - char szIRCNick[80] = ""; - Chrono seed (true); - srand ((unsigned int) seed.getCurTicks ()); - - int randomNickId = (rand () % 899) + 100; - string netPlayerName = - Config::getInstance ().getString ("NetPlayerName", - Socket::getHostName ().c_str ()); - string ircname = netPlayerName.substr (0, 9); - snprintf (szIRCNick, 80, "ZG_%s_%d", ircname.c_str (), randomNickId); - normalizeNick (szIRCNick); - - currentIrcNick = ircname; - consoleIRC.setStringToHighlight (currentIrcNick); - - lines[2].init (0, consoleIRC.getYPos () - 10, userButtonsXBase, 5); - chatManager.init (&consoleIRC, -1, true, szIRCNick); - chatManager.setXPos (0); - chatManager.setYPos (consoleIRC.getYPos () - 20); - chatManager.setFont (CoreData::getInstance ().getMenuFontNormal ()); - chatManager.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - needUpdateFromServer = true; - - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - updateFromMasterserverThread = new SimpleTaskThread (this, 0, 100); - updateFromMasterserverThread->setUniqueID (mutexOwnerId); - updateFromMasterserverThread->start (); - - if (Config::getInstance ().getString ("IRCServer", "") != "") - { - IRC_SERVER = Config::getInstance ().getString ("IRCServer"); - } - ircArgs.push_back (IRC_SERVER); - - if (Config::getInstance ().getString ("IRCNick", "") != "") - { - ircArgs.push_back (Config::getInstance ().getString ("IRCNick")); - } - else - { - ircArgs.push_back (szIRCNick); - } - - if (Config::getInstance ().getString ("IRCChannel", "") != "") - { - IRC_CHANNEL = Config::getInstance ().getString ("IRCChannel"); - } - ircArgs.push_back (IRC_CHANNEL); - - if (Config::getInstance ().getString ("IRCUsername", "") != "") - { - ircArgs.push_back (Config::getInstance ().getString ("IRCUsername")); - } - else - { - ircArgs.push_back (szIRCNick); - } - if (Config::getInstance ().getString ("IRCPassword", "") != "") - { - ircArgs.push_back ("identify " + - Config::getInstance ().getString ("IRCPassword")); - } - else - { - ircArgs.push_back (""); - } - - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#1 IRCCLient Cache check\n"); - IRCThread *&ircThread = - CacheManager::getCachedItem < - IRCThread * >(GameConstants::ircClientCacheLookupKey); - - // Playername changed so restart the IRC Thread - if (ircThread != NULL && netPlayerName != ircThread->getPlayerName ()) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - ircThread->leaveChannel (); - ircThread->setCallbackObj (NULL); - ircThread->signalQuit (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - ircThread = NULL; - } - - if (ircThread == NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#2 IRCCLient Cache check\n"); - - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - ircThread = new IRCThread (ircArgs, this); - ircClient = ircThread; - ircClient->setUniqueID (mutexOwnerId); - ircClient->setPlayerName (netPlayerName); - ircClient->setGlestVersionString (glestVersionString); - ircClient->start (); - } - else - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("#3 IRCCLient Cache check\n"); - ircClient = ircThread; - ircClient->setCallbackObj (this); - ircClient->joinChannel (); - } - - if (netPlayerName == "newbie") - { - showMessageBox (lang.getString ("PlayerNameNotSetPrompt"), - lang.getString ("PlayerNameNotSetTitle"), false); - } - //showMessageBox("Go back and set your name in the game options!\n\nAt the moment you are just called >>newbie<< !","Player name not set!",false); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - void MenuStateMasterserver::reloadUI () - { - Lang & lang = Lang::getInstance (); - - console.resetFonts (); - consoleIRC.setFont (CoreData::getInstance ().getMenuFontNormal ()); - consoleIRC.setFont3D (CoreData::getInstance ().getMenuFontNormal3D ()); - - mainMessageBox.init (lang.getString ("Ok")); - - announcementLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - announcementLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - versionInfoLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - versionInfoLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - labelTitle.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelTitle.setFont3D (CoreData::getInstance ().getMenuFontBig3D ()); - labelTitle.setText (lang.getString ("AvailableServers")); - - if (Config::getInstance ().getString ("Masterserver", "") == "") - { - labelTitle.setText ("*** " + lang.getString ("AvailableServers")); - } - - glestVersionLabel.setText (lang.getString ("MGVersion")); - - platformLabel.setText (lang.getString ("MGPlatform")); - - serverTitleLabel.setText (lang.getString ("MGGameTitle")); - - countryLabel.setText (lang.getString ("MGGameCountry")); - - techLabel.setText (lang.getString ("TechTree")); - - mapLabel.setText (lang.getString ("Map")); - - activeSlotsLabel.setText (lang.getString ("MGGameSlots")); - - //externalConnectPort.setText(lang.getString("Port")); - - statusLabel.setText (lang.getString ("MGGameStatus")); - - selectButton.setText (lang.getString ("MGJoinGameSlots")); - - // Titles for current games - END - - buttonRefresh.setText (lang.getString ("RefreshList")); - buttonReturn.setText (lang.getString ("Return")); - buttonCreateGame.setText (lang.getString ("HostGame")); - labelAutoRefresh.setText (lang.getString ("AutoRefreshRate")); - - ircOnlinePeopleLabel.setText (lang.getString ("IRCPeopleOnline")); - - chatManager.setFont (CoreData::getInstance ().getMenuFontNormal ()); - chatManager.setFont3D (CoreData::getInstance ().getMenuFontNormal3D ()); - - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } - -//void MenuStateMasterserver::setConsolePos(int yPos){ -// consoleIRC.setYPos(yPos); -// lines[2].setY(consoleIRC.getYPos()-10); -// chatManager.setYPos(consoleIRC.getYPos()-20); -//} - - void MenuStateMasterserver::setButtonLinePosition (int pos) - { - buttonReturn.setY (pos); - buttonCreateGame.setY (pos); - buttonRefresh.setY (pos); - labelAutoRefresh.setY (pos + 30); - listBoxAutoRefresh.setY (pos); - } - - void MenuStateMasterserver::IRC_CallbackEvent (IRCEventType evt, - const char *origin, - const char **params, - unsigned int count) - { - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - if (ircClient != NULL) - { - if (evt == IRC_evt_exitThread) - { - ircClient->leaveChannel (); - ircClient->setCallbackObj (NULL); - - ircClient = NULL; - } - else if (evt == IRC_evt_chatText) - { - //printf ("===> IRC: '%s' said in channel %s: %s\n",origin ? origin : "someone",params[0], params[1] ); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s: %s", origin ? origin : "someone", - params[1]); - string helpSTr = szBuf; - if (helpSTr.find (currentIrcNick) != string::npos) - { - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - static PlaySoundClip snd; - soundRenderer.playFx (snd.getSound (snd.sfxHighlight)); - } - consoleIRC.addLine (szBuf); - } - } - } - - void MenuStateMasterserver::cleanup () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - "_" + intToStr (__LINE__)); - needUpdateFromServer = false; - safeMutex.ReleaseLock (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - -// if(updateFromMasterserverThread != NULL && -// updateFromMasterserverThread->canShutdown(true) == true) { -// if(updateFromMasterserverThread->shutdownAndWait() == true) { -// delete updateFromMasterserverThread; -// } -// } -// updateFromMasterserverThread = NULL; - if (updateFromMasterserverThread != NULL) - { - updateFromMasterserverThread->signalQuit (); - } - if (updateFromMasterserverThread != NULL - && updateFromMasterserverThread->canShutdown (false) == true - && updateFromMasterserverThread->getRunningStatus () == false) - { - //printf("#2 Ending client SLOT: %d\n",playerIndex); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugNetwork, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - delete updateFromMasterserverThread; - if (SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugNetwork, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - //else if(BaseThread::shutdownAndWait(slotThreadWorker) == true) { - else if (updateFromMasterserverThread != NULL - && updateFromMasterserverThread->canShutdown (true) == true) - { - if (updateFromMasterserverThread->getRunningStatus () == false) - { - //printf("#3 Ending client SLOT: %d\n",playerIndex); - if (SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugNetwork, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - delete updateFromMasterserverThread; - if (SystemFlags::getSystemSettingType (SystemFlags::debugNetwork). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugNetwork, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else - { - updateFromMasterserverThread->setDeleteSelfOnExecutionDone (true); - updateFromMasterserverThread->setDeleteAfterExecute (true); - } - } - //printf("#4 Ending client SLOT: %d\n",playerIndex); - updateFromMasterserverThread = NULL; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - clearServerLines (); - clearUserButtons (); - - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - if (ircClient != NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - ircClient->leaveChannel (); - ircClient->setCallbackObj (NULL); - //ircClient->signalQuit(); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - ircClient = NULL; - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] END\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - MenuStateMasterserver::~MenuStateMasterserver () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - cleanup (); - - delete mutexIRCClient; - mutexIRCClient = NULL; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] END\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - void MenuStateMasterserver::clearServerLines () - { - while (!serverLines.empty ()) - { - delete serverLines.back (); - serverLines.pop_back (); - } - } - - void MenuStateMasterserver::clearUserButtons () - { - while (!userButtons.empty ()) - { - delete userButtons.back (); - userButtons.pop_back (); - } - } - - void MenuStateMasterserver::mouseClick (int x, int y, - MouseButton mouseButton) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - mainMessageBox.setEnabled (false); - } - } - } - else if (userScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - else if (serverScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - else if (buttonRefresh.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__). - c_str ()) + "_" + intToStr (__LINE__)); - soundRenderer.playFx (coreData.getClickSoundB ()); - needUpdateFromServer = true; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - else if (buttonReturn.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - soundRenderer.playFx (coreData.getClickSoundA ()); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - ircClient->SendIRCCmdMessage (IRC_CHANNEL, - " << left the lobby"); - sleep (30); - } - cleanup (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - else if (buttonCreateGame.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__). - c_str ()) + "_" + intToStr (__LINE__)); - soundRenderer.playFx (coreData.getClickSoundB ()); - needUpdateFromServer = false; - safeMutex.ReleaseLock (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - ircClient->SendIRCCmdMessage (IRC_CHANNEL, - " << tries to create a game"); - sleep (30); - } - cleanup (); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - mainMenu-> - setState (new - MenuStateCustomGame (program, mainMenu, true, - pMasterServer)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - else if (listBoxAutoRefresh.mouseClick (x, y)) - { - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__). - c_str ()) + "_" + intToStr (__LINE__)); - soundRenderer.playFx (coreData.getClickSoundA ()); - autoRefreshTime = 10 * listBoxAutoRefresh.getSelectedItemIndex (); - } - else - { - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__). - c_str ()) + "_" + intToStr (__LINE__)); - bool clicked = false; - if (serverScrollBar.getElementCount () != 0) - { - for (int i = serverScrollBar.getVisibleStart (); - i <= serverScrollBar.getVisibleEnd (); ++i) - { - if (serverLines[i]->buttonMouseClick (x, y)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - clicked = true; - soundRenderer.playFx (coreData.getClickSoundB ()); - string connectServerIP = - serverLines[i]->getMasterServerInfo ()->getIpAddress (); - int connectServerPort = - serverLines[i]->getMasterServerInfo ()-> - getExternalConnectPort (); - bool connected = - connectToServer (connectServerIP, connectServerPort); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - safeMutex.ReleaseLock (); - if (connected) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - ircClient->SendIRCCmdMessage (IRC_CHANNEL, - " << is connecting to '" + - serverLines[i]-> - getMasterServerInfo ()-> - getServerTitle () + "'"); - sleep (30); - } - cleanup (); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - mainMenu-> - setState (new - MenuStateConnectedGame (program, mainMenu, - jmMasterserver)); - } - break; - } - } - } - if (!clicked && userScrollBar.getElementCount () != 0) - { - for (int i = userScrollBar.getVisibleStart (); - i <= userScrollBar.getVisibleEnd (); ++i) - { - if (userButtons[i]->getEnabled () == true - && userButtons[i]->mouseClick (x, y)) - { - //clicked=true; - if (!chatManager.getEditEnabled ()) - { - chatManager.switchOnEdit (); - chatManager.addText (nickList[i] + " "); - } - else - { - chatManager.addText (nickList[i]); - } - break; - } - } - } - } - } - - void MenuStateMasterserver::mouseUp (int x, int y, - const MouseButton mouseButton) - { - if (mouseButton == mbLeft) - { - userScrollBar.mouseUp (x, y); - serverScrollBar.mouseUp (x, y); - } - } - - void MenuStateMasterserver::mouseMove (int x, int y, - const MouseState * ms) - { - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - "_" + intToStr (__LINE__)); - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - buttonRefresh.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - buttonCreateGame.mouseMove (x, y); - if (ms->get (mbLeft)) - { - userScrollBar.mouseDown (x, y); - serverScrollBar.mouseDown (x, y); - } - else - { - userScrollBar.mouseMove (x, y); - serverScrollBar.mouseMove (x, y); - } - listBoxAutoRefresh.mouseMove (x, y); - - if (serverScrollBar.getElementCount () != 0) - { - for (int i = serverScrollBar.getVisibleStart (); - i <= serverScrollBar.getVisibleEnd (); ++i) - { - serverLines[i]->buttonMouseMove (x, y); - } - } - if (userScrollBar.getElementCount () != 0) - { - for (int i = userScrollBar.getVisibleStart (); - i <= userScrollBar.getVisibleEnd (); ++i) - { - if (userButtons[i]->getEnabled () == true) - { - userButtons[i]->mouseMove (x, y); - } - } - } - - } - - void MenuStateMasterserver::render () - { - Renderer & renderer = Renderer::getInstance (); - - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - "_" + intToStr (__LINE__)); - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else - { - renderer.renderLabel (&labelTitle, &GREEN); - renderer.renderLabel (&announcementLabel, &YELLOW); - renderer.renderLabel (&versionInfoLabel); - - // Render titles for server games listed - const Vec4f titleLabelColor = CYAN; - - //general info: - renderer.renderLabel (&glestVersionLabel, &titleLabelColor); - renderer.renderLabel (&platformLabel, &titleLabelColor); - //renderer.renderLabel(&binaryCompileDateLabel,&titleLabelColor); - - //game info: - renderer.renderLabel (&serverTitleLabel, &titleLabelColor); - renderer.renderLabel (&countryLabel, &titleLabelColor); - renderer.renderLabel (&statusLabel, &titleLabelColor); - //renderer.renderLabel(&ipAddressLabel,&titleLabelColor); - - //game setup info: - renderer.renderLabel (&techLabel, &titleLabelColor); - renderer.renderLabel (&mapLabel, &titleLabelColor); - //renderer.renderLabel(&tilesetLabel,&titleLabelColor); - renderer.renderLabel (&activeSlotsLabel, &titleLabelColor); - //renderer.renderLabel(&externalConnectPort,&titleLabelColor); - renderer.renderLabel (&selectButton, &titleLabelColor); - - Lang & lang = Lang::getInstance (); - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - const Vec4f titleLabelColor = GREEN; - - if (ircOnlinePeopleStatusLabel.getText () != "") - { - ircOnlinePeopleStatusLabel.setText (""); - } - - renderer.renderLabel (&ircOnlinePeopleLabel, &titleLabelColor); - //renderer.renderLabel(&ircOnlinePeopleStatusLabel,&titleLabelColor); - } - else - { - const Vec4f titleLabelColor = RED; - - if (ircOnlinePeopleStatusLabel.getText () != - lang.getString ("Connecting")) - { - ircOnlinePeopleStatusLabel.setText (lang. - getString ("Connecting")); - } - - renderer.renderLabel (&ircOnlinePeopleLabel, &titleLabelColor); - renderer.renderLabel (&ircOnlinePeopleStatusLabel, - &titleLabelColor); - } - safeMutexIRCPtr.ReleaseLock (); - - //const Vec4f titleLabelColorList = YELLOW; - - if (serverScrollBar.getElementCount () != 0) - { - for (int i = serverScrollBar.getVisibleStart (); - i <= serverScrollBar.getVisibleEnd (); ++i) - { - serverLines[i]->render (); - } - } - renderer.renderScrollBar (&serverScrollBar); - - for (int i = 0; i < (int) sizeof (lines) / (int) sizeof (lines[0]); - ++i) - { - renderer.renderLine (&lines[i]); - } - renderer.renderButton (&buttonRefresh); - renderer.renderButton (&buttonReturn); - renderer.renderLabel (&labelAutoRefresh); - renderer.renderButton (&buttonCreateGame); - renderer.renderListBox (&listBoxAutoRefresh); - - if (userScrollBar.getElementCount () != 0) - { - for (int i = userScrollBar.getVisibleStart (); - i <= userScrollBar.getVisibleEnd (); ++i) - { - renderer.renderButton (userButtons[i]); - } - } - renderer.renderScrollBar (&userScrollBar); - if (ircClient != NULL && - ircClient->isConnected () == true && - ircClient->getHasJoinedChannel () == true) - { - renderer.renderChatManager (&chatManager); - } - renderer.renderConsole (&consoleIRC, consoleStoredOnly, 21); - - } - if (program != NULL) - program->renderProgramMsgBox (); - } - - void MenuStateMasterserver::update () - { - MutexSafeWrapper - safeMutex ((updateFromMasterserverThread != - NULL ? updateFromMasterserverThread-> - getMutexThreadObjectAccessor () : NULL), - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - "_" + intToStr (__LINE__)); - if (autoRefreshTime != 0 - && difftime (time (NULL), lastRefreshTimer) >= autoRefreshTime) - { - needUpdateFromServer = true; - lastRefreshTimer = time (NULL); - } - - // calculate button linepos: - setButtonLinePosition (serverLinesYBase - - (serverLinesToRender) * serverLinesLineHeight - - 30); - - if (playServerFoundSound) - { - static PlaySoundClip snd; - SoundRenderer::getInstance ().playFx (snd.getSound (snd.sfxNewServer)); - - //switch on music again!! - Config & config = Config::getInstance (); - float configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - CoreData::getInstance ().getMenuMusic ()->setVolume (configVolume); - - playServerFoundSound = false; - } - - //console.update(); - - //call the chat manager - chatManager.updateNetwork (); - - //console - consoleIRC.update (); - - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - if (ircClient != NULL) - { - nickList = ircClient->getNickList (); - - if (currentIrcNick != ircClient->getNick ()) - { - currentIrcNick = ircClient->getNick (); - consoleIRC.setStringToHighlight (currentIrcNick); - } - - bool isNew = false; - //check if there is something new - if (oldNickList.size () != nickList.size ()) - { - isNew = true; - } - else - { - for (unsigned int i = 0; i < nickList.size (); ++i) - { - if (nickList[i] != oldNickList[i]) - { - isNew = true; - break; - } - } - } - - if (isNew) - { - clearUserButtons (); - for (int i = 0; i < (int) nickList.size (); ++i) - { - GraphicButton *button = new GraphicButton (); - button->init (userButtonsXBase, userButtonsYBase, - userButtonsWidth, userButtonsHeight); - //button->init(userButtonsXBase,userButtonsYBase-userButtonsLineHeight*i,userButtonsWidth,userButtonsHeight); - button->setFont (CoreData::getInstance ().getDisplayFontSmall ()); - button->setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); - - if (strncmp (&nickList[i][0], "ZG_", 3) == 0) { - button->setText (nickList[i].substr(3,nickList[i].length()-7)); - } else { - button->setText (nickList[i]); - } - - if (strncmp (&nickList[i][0], "ZG_", 3) != 0 - || &nickList[i][0] == currentIrcNick) - { - button->setEnabled (false); - button->setEditable (false); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - button->setUseCustomTexture (true); - } - - userButtons.push_back (button); - - } - userScrollBar.setElementCount ((int) userButtons.size ()); - oldNickList = nickList; - chatManager.setAutoCompleteTextList (oldNickList); - } - if (userScrollBar.getElementCount () != 0) - { - for (int i = userScrollBar.getVisibleStart (); - i <= userScrollBar.getVisibleEnd (); ++i) - { - userButtons[i]->setY (userButtonsYBase - - userButtonsLineHeight * (i - - userScrollBar. - getVisibleStart - ())); - } - } - } - safeMutexIRCPtr.ReleaseLock (); - if (serverInfoString != "empty") - { - rebuildServerLines (serverInfoString); - serverInfoString = "empty"; - } - - serverScrollBar.setElementCount ((int) serverLines.size ()); - if (serverScrollBar.getElementCount () != 0) - { - for (int i = serverScrollBar.getVisibleStart (); - i <= serverScrollBar.getVisibleEnd (); ++i) - { - serverLines[i]->setY (serverLinesYBase - - serverLinesLineHeight * (i - - serverScrollBar. - getVisibleStart ())); - } - } - - if (threadedErrorMsg != "") - { - std::string sError = threadedErrorMsg; - threadedErrorMsg = ""; - - if (pCB_DisplayMessage != NULL) - { - pCB_DisplayMessage (sError.c_str (), false); - } - else - { - throw megaglest_runtime_error (sError.c_str ()); - } - } - } - - void MenuStateMasterserver::simpleTask (BaseThread * callingThread, - void *userdata) - { - if (callingThread->getQuitStatus () == true) - { - return; - } - MutexSafeWrapper safeMutex (callingThread-> - getMutexThreadObjectAccessor (), - string (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - bool needUpdate = needUpdateFromServer; - - if (needUpdate == true) - { - try - { - - if (callingThread->getQuitStatus () == true) - { - return; - } - - needUpdateFromServer = false; - - if (announcementLoaded == false) - { - string announcementURL = - Config::getInstance ().getString ("AnnouncementURL", - "http://zetaglest.dreamhosters.com/files/announcement.txt"); - if (announcementURL != "") - { - - safeMutex.ReleaseLock (true); - CURL *handle = SystemFlags::initHTTP (); - std::string announcementTxt = - SystemFlags::getHTTP (announcementURL, handle); - SystemFlags::cleanupHTTP (&handle); - if (callingThread->getQuitStatus () == true) - { - return; - } - safeMutex.Lock (); - - if (StartsWith - (announcementTxt, - "Announcement from Masterserver:") == true) - { - int newlineCount = 0; - size_t lastIndex = 0; - - //announcementLabel.setText(announcementTxt); - consoleIRC.addLine (announcementTxt, true, - Vec3f (1.0f, 1.0f, 0.0f)); - - while (true) - { - lastIndex = announcementTxt.find ("\n", lastIndex + 1); - if (lastIndex == string::npos) - { - break; - } - else - { - newlineCount++; - } - } - newlineCount--; // remove my own line - for (int i = 0; i < newlineCount; ++i) - { - consoleIRC.addLine (""); - } - } - } - consoleIRC. - addLine ("---------------------------------------------"); - string versionURL = - Config::getInstance ().getString ("VersionURL", - "http://zetaglest.dreamhosters.com/files/versions/") - + glestVersionString + ".txt"; - //printf("\nversionURL=%s\n",versionURL.c_str()); - if (versionURL != "") - { - safeMutex.ReleaseLock (true); - CURL *handle = SystemFlags::initHTTP (); - std::string versionTxt = - SystemFlags::getHTTP (versionURL, handle); - SystemFlags::cleanupHTTP (&handle); - if (callingThread->getQuitStatus () == true) - { - return; - } - safeMutex.Lock (); - - if (StartsWith (versionTxt, "Version info:") == true) - { - int newlineCount = 0; - size_t lastIndex = 0; - - //versionInfoLabel.setText(versionTxt); - consoleIRC.addLine (versionTxt, true, - Vec3f (1.0f, 0.0f, 0.0f)); - - while (true) - { - lastIndex = versionTxt.find ("\n", lastIndex + 1); - if (lastIndex == string::npos) - { - break; - } - else - { - newlineCount++; - } - } - newlineCount--; // remove my own line - for (int i = 0; i < newlineCount; ++i) - { - consoleIRC.addLine (""); - } - } - } - consoleIRC. - addLine ("---------------------------------------------"); - // write hint to console: - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - consoleIRC.addLine (Lang::getInstance (). - getString ("ToSwitchOffMusicPress") + - " - \"" + - configKeys.getString ("ToggleMusic") + "\""); - - announcementLoaded = true; - } - - //Lang &lang= Lang::getInstance(); - try - { - if (Config::getInstance ().getString ("Masterserver", "") != "") - { - - safeMutex.ReleaseLock (true); - CURL *handle = SystemFlags::initHTTP (); - - string playerUUID = - "?uuid=" + - SystemFlags::escapeURL (Config::getInstance (). - getString ("PlayerId", "")); - - string baseURL = - Config::getInstance ().getString ("Masterserver"); - if (baseURL != "") - { - endPathWithSlash (baseURL, false); - } - - std::string localServerInfoString = - SystemFlags::getHTTP (baseURL + "showServersForGlest.php" + - playerUUID, handle); - SystemFlags::cleanupHTTP (&handle); - if (callingThread->getQuitStatus () == true) - { - return; - } - safeMutex.Lock (); - - serverInfoString = localServerInfoString; - } - } - catch (const exception & ex) - { - serverInfoString = ex.what (); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line %d] error during Internet game status update: [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - ex.what ()); - } - } - catch (const exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - e.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d, error [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - e.what ()); - threadedErrorMsg = e.what (); - } - } - } - - void MenuStateMasterserver::rebuildServerLines (const string & serverInfo) - { - int numberOfOldServerLines = (int) serverLines.size (); - clearServerLines (); - Lang & lang = Lang::getInstance (); - try - { - if (serverInfo != "") - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("--------------> serverInfo [%s]\n", serverInfo.c_str ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - std::vector < std::string > serverList; - Tokenize (serverInfo, serverList, "\n"); - for (int i = 0; i < (int) serverList.size (); i++) - { - string & server = serverList[i]; - if (trim (server) == "") - { - continue; - } - std::vector < std::string > serverEntities; - Tokenize (server, serverEntities, "|"); - const int MIN_FIELDS_EXPECTED = 14; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("--------------> server [%s] serverEntities.size() = " - MG_SIZE_T_SPECIFIER " MIN_FIELDS_EXPECTED = %d\n", - server.c_str (), serverEntities.size (), - MIN_FIELDS_EXPECTED); - - if ((int) serverEntities.size () >= MIN_FIELDS_EXPECTED) - { - labelTitle.setText (lang.getString ("AvailableServers")); - - if (Config::getInstance ().getString ("Masterserver", "") == "") - { - labelTitle.setText ("*** " + - lang.getString ("AvailableServers")); - } - - MasterServerInfo *masterServerInfo = new MasterServerInfo (); - - //general info: - masterServerInfo->setGlestVersion (serverEntities[0]); - masterServerInfo->setPlatform (serverEntities[1]); - masterServerInfo->setBinaryCompileDate (serverEntities[2]); - - //game info: - masterServerInfo->setServerTitle (serverEntities[3]); - masterServerInfo->setIpAddress (serverEntities[4]); - - //game setup info: - masterServerInfo->setTech (serverEntities[5]); - masterServerInfo->setMap (serverEntities[6]); - masterServerInfo->setTileset (serverEntities[7]); - masterServerInfo->setActiveSlots (strToInt (serverEntities[8])); - masterServerInfo-> - setNetworkSlots (strToInt (serverEntities[9])); - masterServerInfo-> - setConnectedClients (strToInt (serverEntities[10])); - masterServerInfo-> - setExternalConnectPort (strToInt (serverEntities[11])); - masterServerInfo->setCountry (serverEntities[12]); - masterServerInfo->setStatus (strToInt (serverEntities[13])); - - //printf("--------------> Country [%s] Status [%d]\n",masterServerInfo->getCountry().c_str(),masterServerInfo->getStatus()); - - //printf("Getting Ping time for host %s\n",masterServerInfo->getIpAddress().c_str()); - //float pingTime = Socket::getAveragePingMS(masterServerInfo->getIpAddress().c_str(),1); - //printf("Ping time = %f\n",pingTime); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "%s", - masterServerInfo->getServerTitle ().c_str ()); - masterServerInfo->setServerTitle (szBuf); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - serverLines. - push_back (new - ServerLine (masterServerInfo, i, serverLinesYBase, - serverLinesLineHeight, containerName)); - delete masterServerInfo; - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - Lang & lang = Lang::getInstance (); - labelTitle.setText ("*** " + - lang.getString ("AvailableServers") + "[" + - intToStr (serverEntities.size ()) + "][" + - intToStr (MIN_FIELDS_EXPECTED) + "] [" + - serverInfo + "]"); - - if (masterserverParseErrorShown == false) - { - masterserverParseErrorShown = true; - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line %d] error, no masterserver defined!\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - } - catch (const exception & ex) - { - labelTitle.setText ("*** " + lang.getString ("AvailableServers") + - " [" + ex.what () + "]"); - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line %d] error during Internet game status update: [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - ex.what ()); - } - - if ((int) serverLines.size () > numberOfOldServerLines) - { - playServerFoundSound = true; - } - } - - - bool MenuStateMasterserver::connectToServer (string ipString, int port) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] START ipString='%s'\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, ipString.c_str ()); - - ClientInterface *clientInterface = - NetworkManager::getInstance ().getClientInterface (); - //Config& config= Config::getInstance(); - Ip serverIp (ipString); - - //int serverPort = Config::getInstance().getInt("PortServer",intToStr(GameConstants::serverPort).c_str()); - int serverPort = port; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] try to connect to [%s] serverPort = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, - serverIp.getString ().c_str (), serverPort); - clientInterface->connect (serverIp, serverPort); - if (clientInterface->isConnected () == false) - { - NetworkManager::getInstance ().end (); - NetworkManager::getInstance ().init (nrClient); - - mainMessageBoxState = 1; - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("CouldNotConnect"), - lang.getString ("ConnectionFailed"), false); - return false; - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] connection failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - } - else - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] connected to [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, - serverIp.getString ().c_str ()); - - //save server ip - //config.setString("ServerIp", serverIp.getString()); - //config.save(); - - for (time_t elapsedWait = time (NULL); - clientInterface->getIntroDone () == false && - clientInterface->isConnected () && - difftime (time (NULL), elapsedWait) <= 8;) - { - if (clientInterface->isConnected ()) - { - //update lobby - clientInterface->updateLobby (); - sleep (0); - //this->render(); - } - } - if (clientInterface->isConnected () == true && - clientInterface->getIntroDone () == true) - { - return true; - } - - return false; - } - } - - void MenuStateMasterserver::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - - bool MenuStateMasterserver::textInput (std::string text) - { - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - return chatManager.textInput (text); - } - return false; - } - - void MenuStateMasterserver::keyDown (SDL_KeyboardEvent key) - { - //printf("In [%s::%s Line: %d] key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key.keysym.sym); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - - if (ircClient != NULL && ircClient->isConnected () == true - && ircClient->getHasJoinedChannel () == true) - { - //chatmanger only if connected to irc! - if (chatManager.getEditEnabled () == true) - { - //printf("keyDown key [%d] chatManager.getText() [%s]\n",key,chatManager.getText().c_str()); - MutexSafeWrapper safeMutexIRCPtr (mutexIRCClient, - string - (extractFileFromDirectoryPath - (__FILE__).c_str ()) + "_" + - intToStr (__LINE__)); - //if (key == vkReturn && ircClient != NULL) { - if (isKeyPressed (SDLK_RETURN, key, false) == true - && ircClient != NULL) - { - ircClient->SendIRCCmdMessage (IRC_CHANNEL, - chatManager.getText ()); - } - } - chatManager.keyDown (key); - } - if (chatManager.getEditEnabled () == false) - { - //if(key == configKeys.getCharKey("ToggleMusic")) { - if (isKeyPressed (configKeys.getSDLKey ("ToggleMusic"), key) == true) - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - - float configVolume = (config.getInt ("SoundVolumeMusic") / 100.f); - float currentVolume = - CoreData::getInstance ().getMenuMusic ()->getVolume (); - if (currentVolume > 0) - { - CoreData::getInstance ().getMenuMusic ()->setVolume (0.f); - consoleIRC.addLine (lang.getString ("GameMusic") + " " + - lang.getString ("Off")); - } - else - { - CoreData::getInstance ().getMenuMusic ()-> - setVolume (configVolume); - //If the config says zero, use the default music volume - //gameMusic->setVolume(configVolume ? configVolume : 0.9); - consoleIRC.addLine (lang.getString ("GameMusic")); - } - } - //else if(key == configKeys.getCharKey("SaveGUILayout")) { - else if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == - true) - { - bool saved = - GraphicComponent::saveAllCustomProperties (containerName); - Lang & lang = Lang::getInstance (); - consoleIRC.addLine (lang.getString ("GUILayoutSaved") + " [" + - (saved ? lang.getString ("Yes") : lang. - getString ("No")) + "]"); - } - } - } - - } +namespace Glest { + namespace Game { + + DisplayMessageFunction MenuStateMasterserver::pCB_DisplayMessage = NULL; + + static string IRC_SERVER = "irc.freenode.net"; + static string IRC_CHANNEL = "#zetaglest-lobby"; + + // ===================================================== + // class MenuStateMasterserver + // ===================================================== + + MenuStateMasterserver::MenuStateMasterserver(Program * program, + MainMenu * + mainMenu) :MenuState + (program, mainMenu, "masterserver"), + mutexIRCClient(new Mutex(CODE_AT_LINE)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("\n\n\n\n******************** ENTERING MASTERSERVER MENU\n\n\n\n\n"); + + containerName = "MasterServer"; + masterserverParseErrorShown = false; + updateFromMasterserverThread = NULL; + ircClient = NULL; + serverInfoString = "empty"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + + //Configure ConsolePosition + consoleIRC.setYPos(60); + consoleIRC.setFont(CoreData::getInstance().getMenuFontNormal()); + consoleIRC.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + consoleIRC.setLineHeight(18); + consoleIRC.setOnlyChatMessagesInStoredLines(false); + + serverLinesToRender = 8; + serverLinesLineHeight = 25; + serverLinesYBase = 680; + + userButtonsHeight = 20; + userButtonsWidth = 150; + userButtonsLineHeight = userButtonsHeight + 2; + userButtonsYBase = + serverLinesYBase - (serverLinesToRender) * serverLinesLineHeight - 90; + userButtonsToRender = userButtonsYBase / userButtonsLineHeight; + userButtonsXBase = 1000 - userButtonsWidth; + + userScrollBar.init(1000 - 20, 0, false, 200, 20); + userScrollBar.setLength(userButtonsYBase + userButtonsLineHeight); + userScrollBar.setElementCount(0); + userScrollBar.setVisibleSize(userButtonsToRender); + userScrollBar.setVisibleStart(0); + + userButtonsXBase = 1000 - userButtonsWidth - userScrollBar.getW(); + + serverScrollBar.init(1000 - 20, + serverLinesYBase - + serverLinesLineHeight * (serverLinesToRender - 1), + false, 200, 20); + serverScrollBar.setLength(serverLinesLineHeight * serverLinesToRender); + serverScrollBar.setElementCount(0); + serverScrollBar.setVisibleSize(serverLinesToRender); + serverScrollBar.setVisibleStart(0); + + lines[0].init(0, + userButtonsYBase + userButtonsLineHeight + + serverLinesLineHeight); + lines[1].init(userButtonsXBase - 5, 0, 5, + userButtonsYBase + 2 * userButtonsLineHeight); + lines[1].setHorizontal(false); + + autoRefreshTime = 0; + playServerFoundSound = false; + announcementLoaded = false; + + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Ok")); + mainMessageBox.setEnabled(false); + mainMessageBoxState = 0; + + lastRefreshTimer = time(NULL); + + // announcement + announcementLabel.registerGraphicComponent(containerName, + "announcementLabel"); + announcementLabel.init(10, 730); + announcementLabel.setFont(CoreData::getInstance().getMenuFontBig()); + announcementLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + announcementLabel.setText(""); + + // versionInfo + versionInfoLabel.registerGraphicComponent(containerName, + "versionInfoLabel"); + versionInfoLabel.init(10, 680); + versionInfoLabel.setFont(CoreData::getInstance().getMenuFontBig()); + versionInfoLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + versionInfoLabel.setText(""); + + // header + labelTitle.registerGraphicComponent(containerName, "labelTitle"); + labelTitle.init(410, serverLinesYBase + 45); + labelTitle.setFont(CoreData::getInstance().getMenuFontBig()); + labelTitle.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTitle.setText(lang.getString("AvailableServers")); + + if (Config::getInstance().getString("Masterserver", "") == "") { + labelTitle.setText("*** " + lang.getString("AvailableServers")); + } + + // bottom + int buttonPos = 230; + + // Titles for current games - START + int lineIndex = 0; + int lineOffset = 25 * lineIndex; + int i = 5; + int startOffset = serverLinesYBase + 23; + + //general info: + //i+=10; + glestVersionLabel.registerGraphicComponent(containerName, + "glestVersionLabel"); + glestVersionLabel.init(i, startOffset - lineOffset); + glestVersionLabel.setText(lang.getString("MGVersion")); + + i += 80; + platformLabel.registerGraphicComponent(containerName, + "platformLabel"); + platformLabel.init(i + 15, startOffset - lineOffset); + platformLabel.setText(lang.getString("MGPlatform")); + + // i+=50; + // binaryCompileDateLabel.registerGraphicComponent(containerName,"binaryCompileDateLabel"); + // binaryCompileDateLabel.init(i,startOffset-lineOffset); + // binaryCompileDateLabel.setText(lang.getString("MGBuildDateTime")); + + //game info: + i += 120; + serverTitleLabel.registerGraphicComponent(containerName, + "serverTitleLabel"); + serverTitleLabel.init(i, startOffset - lineOffset); + serverTitleLabel.setText(lang.getString("MGGameTitle")); + + i += 170; + countryLabel.registerGraphicComponent(containerName, "countryLabel"); + countryLabel.init(i - 10, startOffset - lineOffset); + countryLabel.setText(lang.getString("MGGameCountry")); + + i += 60; + + // ipAddressLabel.registerGraphicComponent(containerName,"ipAddressLabel"); + // ipAddressLabel.init(i,startOffset-lineOffset); + // ipAddressLabel.setText(lang.getString("MGGameIP")); + // i+=100; + + //game setup info: + techLabel.registerGraphicComponent(containerName, "techLabel"); + techLabel.init(i, startOffset - lineOffset); + techLabel.setText(lang.getString("TechTree")); + + i += 165; + mapLabel.registerGraphicComponent(containerName, "mapLabel"); + mapLabel.init(i, startOffset - lineOffset); + mapLabel.setText(lang.getString("Map")); + + i += 95; + // tilesetLabel.registerGraphicComponent(containerName,"tilesetLabel"); + // tilesetLabel.init(i,startOffset-lineOffset); + // tilesetLabel.setText(lang.getString("Tileset")); + // i+=100; + + activeSlotsLabel.registerGraphicComponent(containerName, + "activeSlotsLabel"); + activeSlotsLabel.init(i, startOffset - lineOffset); + activeSlotsLabel.setText(lang.getString("MGGameSlots")); + + i += 50; + //externalConnectPort.registerGraphicComponent(containerName,"externalConnectPort"); + //externalConnectPort.init(i,startOffset-lineOffset); + //externalConnectPort.setText(lang.getString("Port")); + + i += 30; + statusLabel.registerGraphicComponent(containerName, "statusLabel"); + statusLabel.init(i + 5, startOffset - lineOffset); + statusLabel.setText(lang.getString("MGGameStatus")); + + i += 130; + selectButton.registerGraphicComponent(containerName, "selectButton"); + selectButton.init(i - 5, startOffset - lineOffset); + selectButton.setText(lang.getString("MGJoinGameSlots")); + + // Titles for current games - END + + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(50, buttonPos, 150); + + buttonCreateGame.registerGraphicComponent(containerName, + "buttonCreateGame"); + buttonCreateGame.init(275, buttonPos, 150); + + buttonRefresh.registerGraphicComponent(containerName, + "buttonRefresh"); + buttonRefresh.init(500, buttonPos, 150); + + buttonRefresh.setText(lang.getString("RefreshList")); + buttonReturn.setText(lang.getString("Return")); + buttonCreateGame.setText(lang.getString("HostGame")); + labelAutoRefresh.setText(lang.getString("AutoRefreshRate")); + + labelAutoRefresh.registerGraphicComponent(containerName, + "labelAutoRefresh"); + labelAutoRefresh.init(750, buttonPos + 30); + + listBoxAutoRefresh.registerGraphicComponent(containerName, + "listBoxAutoRefresh"); + listBoxAutoRefresh.init(750, buttonPos); + listBoxAutoRefresh.pushBackItem(lang.getString("Off")); + listBoxAutoRefresh.pushBackItem("10 s"); + listBoxAutoRefresh.pushBackItem("20 s"); + listBoxAutoRefresh.pushBackItem("30 s"); + listBoxAutoRefresh.setSelectedItemIndex(2); + autoRefreshTime = 10 * listBoxAutoRefresh.getSelectedItemIndex(); + + ircOnlinePeopleLabel.registerGraphicComponent(containerName, + "ircOnlinePeopleLabel"); + ircOnlinePeopleLabel.init(userButtonsXBase, + userButtonsYBase + userButtonsLineHeight); + ircOnlinePeopleLabel.setText(lang.getString("IRCPeopleOnline")); + + ircOnlinePeopleStatusLabel.registerGraphicComponent(containerName, + "ircOnlinePeopleStatusLabel"); + ircOnlinePeopleStatusLabel.init(userButtonsXBase, + userButtonsYBase + + userButtonsLineHeight - 20); + ircOnlinePeopleStatusLabel.setText(""); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + NetworkManager::getInstance().end(); + NetworkManager::getInstance().init(nrClient); + + //console.addLine(lang.getString("ToSwitchOffMusicPress")+" - \""+configKeys.getCharKey("ToggleMusic")+"\""); + + GraphicComponent::applyAllCustomProperties(containerName); + + char szIRCNick[80] = ""; + Chrono seed(true); + srand((unsigned int) seed.getCurTicks()); + + int randomNickId = (rand() % 899) + 100; + string netPlayerName = + Config::getInstance().getString("NetPlayerName", + Socket::getHostName().c_str()); + string ircname = netPlayerName.substr(0, 9); + snprintf(szIRCNick, 80, "ZG_%s_%d", ircname.c_str(), randomNickId); + normalizeNick(szIRCNick); + + currentIrcNick = ircname; + consoleIRC.setStringToHighlight(currentIrcNick); + + lines[2].init(0, consoleIRC.getYPos() - 10, userButtonsXBase, 5); + chatManager.init(&consoleIRC, -1, true, szIRCNick); + chatManager.setXPos(0); + chatManager.setYPos(consoleIRC.getYPos() - 20); + chatManager.setFont(CoreData::getInstance().getMenuFontNormal()); + chatManager.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + needUpdateFromServer = true; + + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + updateFromMasterserverThread = new SimpleTaskThread(this, 0, 100); + updateFromMasterserverThread->setUniqueID(mutexOwnerId); + updateFromMasterserverThread->start(); + + if (Config::getInstance().getString("IRCServer", "") != "") { + IRC_SERVER = Config::getInstance().getString("IRCServer"); + } + ircArgs.push_back(IRC_SERVER); + + if (Config::getInstance().getString("IRCNick", "") != "") { + ircArgs.push_back(Config::getInstance().getString("IRCNick")); + } else { + ircArgs.push_back(szIRCNick); + } + + if (Config::getInstance().getString("IRCChannel", "") != "") { + IRC_CHANNEL = Config::getInstance().getString("IRCChannel"); + } + ircArgs.push_back(IRC_CHANNEL); + + if (Config::getInstance().getString("IRCUsername", "") != "") { + ircArgs.push_back(Config::getInstance().getString("IRCUsername")); + } else { + ircArgs.push_back(szIRCNick); + } + if (Config::getInstance().getString("IRCPassword", "") != "") { + ircArgs.push_back("identify " + + Config::getInstance().getString("IRCPassword")); + } else { + ircArgs.push_back(""); + } + + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#1 IRCCLient Cache check\n"); + IRCThread *&ircThread = + CacheManager::getCachedItem < + IRCThread * >(GameConstants::ircClientCacheLookupKey); + + // Playername changed so restart the IRC Thread + if (ircThread != NULL && netPlayerName != ircThread->getPlayerName()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + ircThread->leaveChannel(); + ircThread->setCallbackObj(NULL); + ircThread->signalQuit(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + ircThread = NULL; + } + + if (ircThread == NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#2 IRCCLient Cache check\n"); + + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + ircThread = new IRCThread(ircArgs, this); + ircClient = ircThread; + ircClient->setUniqueID(mutexOwnerId); + ircClient->setPlayerName(netPlayerName); + ircClient->setGlestVersionString(glestVersionString); + ircClient->start(); + } else { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("#3 IRCCLient Cache check\n"); + ircClient = ircThread; + ircClient->setCallbackObj(this); + ircClient->joinChannel(); + } + + if (netPlayerName == "newbie") { + showMessageBox(lang.getString("PlayerNameNotSetPrompt"), + lang.getString("PlayerNameNotSetTitle"), false); + } + //showMessageBox("Go back and set your name in the game options!\n\nAt the moment you are just called >>newbie<< !","Player name not set!",false); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + void MenuStateMasterserver::reloadUI() { + Lang & lang = Lang::getInstance(); + + console.resetFonts(); + consoleIRC.setFont(CoreData::getInstance().getMenuFontNormal()); + consoleIRC.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + mainMessageBox.init(lang.getString("Ok")); + + announcementLabel.setFont(CoreData::getInstance().getMenuFontBig()); + announcementLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + versionInfoLabel.setFont(CoreData::getInstance().getMenuFontBig()); + versionInfoLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + labelTitle.setFont(CoreData::getInstance().getMenuFontBig()); + labelTitle.setFont3D(CoreData::getInstance().getMenuFontBig3D()); + labelTitle.setText(lang.getString("AvailableServers")); + + if (Config::getInstance().getString("Masterserver", "") == "") { + labelTitle.setText("*** " + lang.getString("AvailableServers")); + } + + glestVersionLabel.setText(lang.getString("MGVersion")); + + platformLabel.setText(lang.getString("MGPlatform")); + + serverTitleLabel.setText(lang.getString("MGGameTitle")); + + countryLabel.setText(lang.getString("MGGameCountry")); + + techLabel.setText(lang.getString("TechTree")); + + mapLabel.setText(lang.getString("Map")); + + activeSlotsLabel.setText(lang.getString("MGGameSlots")); + + //externalConnectPort.setText(lang.getString("Port")); + + statusLabel.setText(lang.getString("MGGameStatus")); + + selectButton.setText(lang.getString("MGJoinGameSlots")); + + // Titles for current games - END + + buttonRefresh.setText(lang.getString("RefreshList")); + buttonReturn.setText(lang.getString("Return")); + buttonCreateGame.setText(lang.getString("HostGame")); + labelAutoRefresh.setText(lang.getString("AutoRefreshRate")); + + ircOnlinePeopleLabel.setText(lang.getString("IRCPeopleOnline")); + + chatManager.setFont(CoreData::getInstance().getMenuFontNormal()); + chatManager.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } + + //void MenuStateMasterserver::setConsolePos(int yPos){ + // consoleIRC.setYPos(yPos); + // lines[2].setY(consoleIRC.getYPos()-10); + // chatManager.setYPos(consoleIRC.getYPos()-20); + //} + + void MenuStateMasterserver::setButtonLinePosition(int pos) { + buttonReturn.setY(pos); + buttonCreateGame.setY(pos); + buttonRefresh.setY(pos); + labelAutoRefresh.setY(pos + 30); + listBoxAutoRefresh.setY(pos); + } + + void MenuStateMasterserver::IRC_CallbackEvent(IRCEventType evt, + const char *origin, + const char **params, + unsigned int count) { + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + if (ircClient != NULL) { + if (evt == IRC_evt_exitThread) { + ircClient->leaveChannel(); + ircClient->setCallbackObj(NULL); + + ircClient = NULL; + } else if (evt == IRC_evt_chatText) { + //printf ("===> IRC: '%s' said in channel %s: %s\n",origin ? origin : "someone",params[0], params[1] ); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s: %s", origin ? origin : "someone", + params[1]); + string helpSTr = szBuf; + if (helpSTr.find(currentIrcNick) != string::npos) { + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + static PlaySoundClip snd; + soundRenderer.playFx(snd.getSound(snd.sfxHighlight)); + } + consoleIRC.addLine(szBuf); + } + } + } + + void MenuStateMasterserver::cleanup() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + "_" + intToStr(__LINE__)); + needUpdateFromServer = false; + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + // if(updateFromMasterserverThread != NULL && + // updateFromMasterserverThread->canShutdown(true) == true) { + // if(updateFromMasterserverThread->shutdownAndWait() == true) { + // delete updateFromMasterserverThread; + // } + // } + // updateFromMasterserverThread = NULL; + if (updateFromMasterserverThread != NULL) { + updateFromMasterserverThread->signalQuit(); + } + if (updateFromMasterserverThread != NULL + && updateFromMasterserverThread->canShutdown(false) == true + && updateFromMasterserverThread->getRunningStatus() == false) { + //printf("#2 Ending client SLOT: %d\n",playerIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugNetwork, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + delete updateFromMasterserverThread; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugNetwork, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + //else if(BaseThread::shutdownAndWait(slotThreadWorker) == true) { + else if (updateFromMasterserverThread != NULL + && updateFromMasterserverThread->canShutdown(true) == true) { + if (updateFromMasterserverThread->getRunningStatus() == false) { + //printf("#3 Ending client SLOT: %d\n",playerIndex); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugNetwork, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + delete updateFromMasterserverThread; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugNetwork, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else { + updateFromMasterserverThread->setDeleteSelfOnExecutionDone(true); + updateFromMasterserverThread->setDeleteAfterExecute(true); + } + } + //printf("#4 Ending client SLOT: %d\n",playerIndex); + updateFromMasterserverThread = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + clearServerLines(); + clearUserButtons(); + + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + if (ircClient != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + ircClient->leaveChannel(); + ircClient->setCallbackObj(NULL); + //ircClient->signalQuit(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + ircClient = NULL; + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] END\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + MenuStateMasterserver::~MenuStateMasterserver() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + cleanup(); + + delete mutexIRCClient; + mutexIRCClient = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] END\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + void MenuStateMasterserver::clearServerLines() { + while (!serverLines.empty()) { + delete serverLines.back(); + serverLines.pop_back(); + } + } + + void MenuStateMasterserver::clearUserButtons() { + while (!userButtons.empty()) { + delete userButtons.back(); + userButtons.pop_back(); + } + } + + void MenuStateMasterserver::mouseClick(int x, int y, + MouseButton mouseButton) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + mainMessageBox.setEnabled(false); + } + } + } else if (userScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } else if (serverScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } else if (buttonRefresh.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__). + c_str()) + "_" + intToStr(__LINE__)); + soundRenderer.playFx(coreData.getClickSoundB()); + needUpdateFromServer = true; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } else if (buttonReturn.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + soundRenderer.playFx(coreData.getClickSoundA()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + ircClient->SendIRCCmdMessage(IRC_CHANNEL, + " << left the lobby"); + sleep(30); + } + cleanup(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } else if (buttonCreateGame.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__). + c_str()) + "_" + intToStr(__LINE__)); + soundRenderer.playFx(coreData.getClickSoundB()); + needUpdateFromServer = false; + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + ircClient->SendIRCCmdMessage(IRC_CHANNEL, + " << tries to create a game"); + sleep(30); + } + cleanup(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + mainMenu-> + setState(new + MenuStateCustomGame(program, mainMenu, true, + pMasterServer)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } else if (listBoxAutoRefresh.mouseClick(x, y)) { + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__). + c_str()) + "_" + intToStr(__LINE__)); + soundRenderer.playFx(coreData.getClickSoundA()); + autoRefreshTime = 10 * listBoxAutoRefresh.getSelectedItemIndex(); + } else { + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__). + c_str()) + "_" + intToStr(__LINE__)); + bool clicked = false; + if (serverScrollBar.getElementCount() != 0) { + for (int i = serverScrollBar.getVisibleStart(); + i <= serverScrollBar.getVisibleEnd(); ++i) { + if (serverLines[i]->buttonMouseClick(x, y)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + clicked = true; + soundRenderer.playFx(coreData.getClickSoundB()); + string connectServerIP = + serverLines[i]->getMasterServerInfo()->getIpAddress(); + int connectServerPort = + serverLines[i]->getMasterServerInfo()-> + getExternalConnectPort(); + bool connected = + connectToServer(connectServerIP, connectServerPort); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + safeMutex.ReleaseLock(); + if (connected) { + soundRenderer.playFx(coreData.getClickSoundB()); + + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + ircClient->SendIRCCmdMessage(IRC_CHANNEL, + " << is connecting to '" + + serverLines[i]-> + getMasterServerInfo()-> + getServerTitle() + "'"); + sleep(30); + } + cleanup(); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + mainMenu-> + setState(new + MenuStateConnectedGame(program, mainMenu, + jmMasterserver)); + } + break; + } + } + } + if (!clicked && userScrollBar.getElementCount() != 0) { + for (int i = userScrollBar.getVisibleStart(); + i <= userScrollBar.getVisibleEnd(); ++i) { + if (userButtons[i]->getEnabled() == true + && userButtons[i]->mouseClick(x, y)) { + //clicked=true; + if (!chatManager.getEditEnabled()) { + chatManager.switchOnEdit(); + chatManager.addText(nickList[i] + " "); + } else { + chatManager.addText(nickList[i]); + } + break; + } + } + } + } + } + + void MenuStateMasterserver::mouseUp(int x, int y, + const MouseButton mouseButton) { + if (mouseButton == mbLeft) { + userScrollBar.mouseUp(x, y); + serverScrollBar.mouseUp(x, y); + } + } + + void MenuStateMasterserver::mouseMove(int x, int y, + const MouseState * ms) { + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + "_" + intToStr(__LINE__)); + + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + buttonRefresh.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + buttonCreateGame.mouseMove(x, y); + if (ms->get(mbLeft)) { + userScrollBar.mouseDown(x, y); + serverScrollBar.mouseDown(x, y); + } else { + userScrollBar.mouseMove(x, y); + serverScrollBar.mouseMove(x, y); + } + listBoxAutoRefresh.mouseMove(x, y); + + if (serverScrollBar.getElementCount() != 0) { + for (int i = serverScrollBar.getVisibleStart(); + i <= serverScrollBar.getVisibleEnd(); ++i) { + serverLines[i]->buttonMouseMove(x, y); + } + } + if (userScrollBar.getElementCount() != 0) { + for (int i = userScrollBar.getVisibleStart(); + i <= userScrollBar.getVisibleEnd(); ++i) { + if (userButtons[i]->getEnabled() == true) { + userButtons[i]->mouseMove(x, y); + } + } + } + + } + + void MenuStateMasterserver::render() { + Renderer & renderer = Renderer::getInstance(); + + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + "_" + intToStr(__LINE__)); + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else { + renderer.renderLabel(&labelTitle, &GREEN); + renderer.renderLabel(&announcementLabel, &YELLOW); + renderer.renderLabel(&versionInfoLabel); + + // Render titles for server games listed + const Vec4f titleLabelColor = CYAN; + + //general info: + renderer.renderLabel(&glestVersionLabel, &titleLabelColor); + renderer.renderLabel(&platformLabel, &titleLabelColor); + //renderer.renderLabel(&binaryCompileDateLabel,&titleLabelColor); + + //game info: + renderer.renderLabel(&serverTitleLabel, &titleLabelColor); + renderer.renderLabel(&countryLabel, &titleLabelColor); + renderer.renderLabel(&statusLabel, &titleLabelColor); + //renderer.renderLabel(&ipAddressLabel,&titleLabelColor); + + //game setup info: + renderer.renderLabel(&techLabel, &titleLabelColor); + renderer.renderLabel(&mapLabel, &titleLabelColor); + //renderer.renderLabel(&tilesetLabel,&titleLabelColor); + renderer.renderLabel(&activeSlotsLabel, &titleLabelColor); + //renderer.renderLabel(&externalConnectPort,&titleLabelColor); + renderer.renderLabel(&selectButton, &titleLabelColor); + + Lang & lang = Lang::getInstance(); + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + const Vec4f titleLabelColor = GREEN; + + if (ircOnlinePeopleStatusLabel.getText() != "") { + ircOnlinePeopleStatusLabel.setText(""); + } + + renderer.renderLabel(&ircOnlinePeopleLabel, &titleLabelColor); + //renderer.renderLabel(&ircOnlinePeopleStatusLabel,&titleLabelColor); + } else { + const Vec4f titleLabelColor = RED; + + if (ircOnlinePeopleStatusLabel.getText() != + lang.getString("Connecting")) { + ircOnlinePeopleStatusLabel.setText(lang. + getString("Connecting")); + } + + renderer.renderLabel(&ircOnlinePeopleLabel, &titleLabelColor); + renderer.renderLabel(&ircOnlinePeopleStatusLabel, + &titleLabelColor); + } + safeMutexIRCPtr.ReleaseLock(); + + //const Vec4f titleLabelColorList = YELLOW; + + if (serverScrollBar.getElementCount() != 0) { + for (int i = serverScrollBar.getVisibleStart(); + i <= serverScrollBar.getVisibleEnd(); ++i) { + serverLines[i]->render(); + } + } + renderer.renderScrollBar(&serverScrollBar); + + for (int i = 0; i < (int) sizeof(lines) / (int) sizeof(lines[0]); + ++i) { + renderer.renderLine(&lines[i]); + } + renderer.renderButton(&buttonRefresh); + renderer.renderButton(&buttonReturn); + renderer.renderLabel(&labelAutoRefresh); + renderer.renderButton(&buttonCreateGame); + renderer.renderListBox(&listBoxAutoRefresh); + + if (userScrollBar.getElementCount() != 0) { + for (int i = userScrollBar.getVisibleStart(); + i <= userScrollBar.getVisibleEnd(); ++i) { + renderer.renderButton(userButtons[i]); + } + } + renderer.renderScrollBar(&userScrollBar); + if (ircClient != NULL && + ircClient->isConnected() == true && + ircClient->getHasJoinedChannel() == true) { + renderer.renderChatManager(&chatManager); + } + renderer.renderConsole(&consoleIRC, consoleStoredOnly, 21); + + } + if (program != NULL) + program->renderProgramMsgBox(); + } + + void MenuStateMasterserver::update() { + MutexSafeWrapper + safeMutex((updateFromMasterserverThread != + NULL ? updateFromMasterserverThread-> + getMutexThreadObjectAccessor() : NULL), + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + "_" + intToStr(__LINE__)); + if (autoRefreshTime != 0 + && difftime(time(NULL), lastRefreshTimer) >= autoRefreshTime) { + needUpdateFromServer = true; + lastRefreshTimer = time(NULL); + } + + // calculate button linepos: + setButtonLinePosition(serverLinesYBase - + (serverLinesToRender) * serverLinesLineHeight - + 30); + + if (playServerFoundSound) { + static PlaySoundClip snd; + SoundRenderer::getInstance().playFx(snd.getSound(snd.sfxNewServer)); + + //switch on music again!! + Config & config = Config::getInstance(); + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + CoreData::getInstance().getMenuMusic()->setVolume(configVolume); + + playServerFoundSound = false; + } + + //console.update(); + + //call the chat manager + chatManager.updateNetwork(); + + //console + consoleIRC.update(); + + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + if (ircClient != NULL) { + nickList = ircClient->getNickList(); + + if (currentIrcNick != ircClient->getNick()) { + currentIrcNick = ircClient->getNick(); + consoleIRC.setStringToHighlight(currentIrcNick); + } + + bool isNew = false; + //check if there is something new + if (oldNickList.size() != nickList.size()) { + isNew = true; + } else { + for (unsigned int i = 0; i < nickList.size(); ++i) { + if (nickList[i] != oldNickList[i]) { + isNew = true; + break; + } + } + } + + if (isNew) { + clearUserButtons(); + for (int i = 0; i < (int) nickList.size(); ++i) { + GraphicButton *button = new GraphicButton(); + button->init(userButtonsXBase, userButtonsYBase, + userButtonsWidth, userButtonsHeight); + //button->init(userButtonsXBase,userButtonsYBase-userButtonsLineHeight*i,userButtonsWidth,userButtonsHeight); + button->setFont(CoreData::getInstance().getDisplayFontSmall()); + button->setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); + + if (strncmp(&nickList[i][0], "ZG_", 3) == 0) { + button->setText(nickList[i].substr(3, nickList[i].length() - 7)); + } else { + button->setText(nickList[i]); + } + + if (strncmp(&nickList[i][0], "ZG_", 3) != 0 + || &nickList[i][0] == currentIrcNick) { + button->setEnabled(false); + button->setEditable(false); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + button->setUseCustomTexture(true); + } + + userButtons.push_back(button); + + } + userScrollBar.setElementCount((int) userButtons.size()); + oldNickList = nickList; + chatManager.setAutoCompleteTextList(oldNickList); + } + if (userScrollBar.getElementCount() != 0) { + for (int i = userScrollBar.getVisibleStart(); + i <= userScrollBar.getVisibleEnd(); ++i) { + userButtons[i]->setY(userButtonsYBase - + userButtonsLineHeight * (i - + userScrollBar. + getVisibleStart + ())); + } + } + } + safeMutexIRCPtr.ReleaseLock(); + if (serverInfoString != "empty") { + rebuildServerLines(serverInfoString); + serverInfoString = "empty"; + } + + serverScrollBar.setElementCount((int) serverLines.size()); + if (serverScrollBar.getElementCount() != 0) { + for (int i = serverScrollBar.getVisibleStart(); + i <= serverScrollBar.getVisibleEnd(); ++i) { + serverLines[i]->setY(serverLinesYBase - + serverLinesLineHeight * (i - + serverScrollBar. + getVisibleStart())); + } + } + + if (threadedErrorMsg != "") { + std::string sError = threadedErrorMsg; + threadedErrorMsg = ""; + + if (pCB_DisplayMessage != NULL) { + pCB_DisplayMessage(sError.c_str(), false); + } else { + throw megaglest_runtime_error(sError.c_str()); + } + } + } + + void MenuStateMasterserver::simpleTask(BaseThread * callingThread, + void *userdata) { + if (callingThread->getQuitStatus() == true) { + return; + } + MutexSafeWrapper safeMutex(callingThread-> + getMutexThreadObjectAccessor(), + string(extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + bool needUpdate = needUpdateFromServer; + + if (needUpdate == true) { + try { + + if (callingThread->getQuitStatus() == true) { + return; + } + + needUpdateFromServer = false; + + if (announcementLoaded == false) { + string announcementURL = + Config::getInstance().getString("AnnouncementURL", + "http://zetaglest.dreamhosters.com/files/announcement.txt"); + if (announcementURL != "") { + + safeMutex.ReleaseLock(true); + CURL *handle = SystemFlags::initHTTP(); + std::string announcementTxt = + SystemFlags::getHTTP(announcementURL, handle); + SystemFlags::cleanupHTTP(&handle); + if (callingThread->getQuitStatus() == true) { + return; + } + safeMutex.Lock(); + + if (StartsWith + (announcementTxt, + "Announcement from Masterserver:") == true) { + int newlineCount = 0; + size_t lastIndex = 0; + + //announcementLabel.setText(announcementTxt); + consoleIRC.addLine(announcementTxt, true, + Vec3f(1.0f, 1.0f, 0.0f)); + + while (true) { + lastIndex = announcementTxt.find("\n", lastIndex + 1); + if (lastIndex == string::npos) { + break; + } else { + newlineCount++; + } + } + newlineCount--; // remove my own line + for (int i = 0; i < newlineCount; ++i) { + consoleIRC.addLine(""); + } + } + } + consoleIRC. + addLine("---------------------------------------------"); + string versionURL = + Config::getInstance().getString("VersionURL", + "http://zetaglest.dreamhosters.com/files/versions/") + + glestVersionString + ".txt"; + //printf("\nversionURL=%s\n",versionURL.c_str()); + if (versionURL != "") { + safeMutex.ReleaseLock(true); + CURL *handle = SystemFlags::initHTTP(); + std::string versionTxt = + SystemFlags::getHTTP(versionURL, handle); + SystemFlags::cleanupHTTP(&handle); + if (callingThread->getQuitStatus() == true) { + return; + } + safeMutex.Lock(); + + if (StartsWith(versionTxt, "Version info:") == true) { + int newlineCount = 0; + size_t lastIndex = 0; + + //versionInfoLabel.setText(versionTxt); + consoleIRC.addLine(versionTxt, true, + Vec3f(1.0f, 0.0f, 0.0f)); + + while (true) { + lastIndex = versionTxt.find("\n", lastIndex + 1); + if (lastIndex == string::npos) { + break; + } else { + newlineCount++; + } + } + newlineCount--; // remove my own line + for (int i = 0; i < newlineCount; ++i) { + consoleIRC.addLine(""); + } + } + } + consoleIRC. + addLine("---------------------------------------------"); + // write hint to console: + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + consoleIRC.addLine(Lang::getInstance(). + getString("ToSwitchOffMusicPress") + + " - \"" + + configKeys.getString("ToggleMusic") + "\""); + + announcementLoaded = true; + } + + //Lang &lang= Lang::getInstance(); + try { + if (Config::getInstance().getString("Masterserver", "") != "") { + + safeMutex.ReleaseLock(true); + CURL *handle = SystemFlags::initHTTP(); + + string playerUUID = + "?uuid=" + + SystemFlags::escapeURL(Config::getInstance(). + getString("PlayerId", "")); + + string baseURL = + Config::getInstance().getString("Masterserver"); + if (baseURL != "") { + endPathWithSlash(baseURL, false); + } + + std::string localServerInfoString = + SystemFlags::getHTTP(baseURL + "showServersForGlest.php" + + playerUUID, handle); + SystemFlags::cleanupHTTP(&handle); + if (callingThread->getQuitStatus() == true) { + return; + } + safeMutex.Lock(); + + serverInfoString = localServerInfoString; + } + } catch (const exception & ex) { + serverInfoString = ex.what(); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line %d] error during Internet game status update: [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + ex.what()); + } + } catch (const exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + e.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d, error [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + e.what()); + threadedErrorMsg = e.what(); + } + } + } + + void MenuStateMasterserver::rebuildServerLines(const string & serverInfo) { + int numberOfOldServerLines = (int) serverLines.size(); + clearServerLines(); + Lang & lang = Lang::getInstance(); + try { + if (serverInfo != "") { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("--------------> serverInfo [%s]\n", serverInfo.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + std::vector < std::string > serverList; + Tokenize(serverInfo, serverList, "\n"); + for (int i = 0; i < (int) serverList.size(); i++) { + string & server = serverList[i]; + if (trim(server) == "") { + continue; + } + std::vector < std::string > serverEntities; + Tokenize(server, serverEntities, "|"); + const int MIN_FIELDS_EXPECTED = 14; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("--------------> server [%s] serverEntities.size() = " + MG_SIZE_T_SPECIFIER " MIN_FIELDS_EXPECTED = %d\n", + server.c_str(), serverEntities.size(), + MIN_FIELDS_EXPECTED); + + if ((int) serverEntities.size() >= MIN_FIELDS_EXPECTED) { + labelTitle.setText(lang.getString("AvailableServers")); + + if (Config::getInstance().getString("Masterserver", "") == "") { + labelTitle.setText("*** " + + lang.getString("AvailableServers")); + } + + MasterServerInfo *masterServerInfo = new MasterServerInfo(); + + //general info: + masterServerInfo->setGlestVersion(serverEntities[0]); + masterServerInfo->setPlatform(serverEntities[1]); + masterServerInfo->setBinaryCompileDate(serverEntities[2]); + + //game info: + masterServerInfo->setServerTitle(serverEntities[3]); + masterServerInfo->setIpAddress(serverEntities[4]); + + //game setup info: + masterServerInfo->setTech(serverEntities[5]); + masterServerInfo->setMap(serverEntities[6]); + masterServerInfo->setTileset(serverEntities[7]); + masterServerInfo->setActiveSlots(strToInt(serverEntities[8])); + masterServerInfo-> + setNetworkSlots(strToInt(serverEntities[9])); + masterServerInfo-> + setConnectedClients(strToInt(serverEntities[10])); + masterServerInfo-> + setExternalConnectPort(strToInt(serverEntities[11])); + masterServerInfo->setCountry(serverEntities[12]); + masterServerInfo->setStatus(strToInt(serverEntities[13])); + + //printf("--------------> Country [%s] Status [%d]\n",masterServerInfo->getCountry().c_str(),masterServerInfo->getStatus()); + + //printf("Getting Ping time for host %s\n",masterServerInfo->getIpAddress().c_str()); + //float pingTime = Socket::getAveragePingMS(masterServerInfo->getIpAddress().c_str(),1); + //printf("Ping time = %f\n",pingTime); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s", + masterServerInfo->getServerTitle().c_str()); + masterServerInfo->setServerTitle(szBuf); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + serverLines. + push_back(new + ServerLine(masterServerInfo, i, serverLinesYBase, + serverLinesLineHeight, containerName)); + delete masterServerInfo; + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + Lang & lang = Lang::getInstance(); + labelTitle.setText("*** " + + lang.getString("AvailableServers") + "[" + + intToStr(serverEntities.size()) + "][" + + intToStr(MIN_FIELDS_EXPECTED) + "] [" + + serverInfo + "]"); + + if (masterserverParseErrorShown == false) { + masterserverParseErrorShown = true; + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line %d] error, no masterserver defined!\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + } catch (const exception & ex) { + labelTitle.setText("*** " + lang.getString("AvailableServers") + + " [" + ex.what() + "]"); + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line %d] error during Internet game status update: [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + ex.what()); + } + + if ((int) serverLines.size() > numberOfOldServerLines) { + playServerFoundSound = true; + } + } + + + bool MenuStateMasterserver::connectToServer(string ipString, int port) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] START ipString='%s'\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, ipString.c_str()); + + ClientInterface *clientInterface = + NetworkManager::getInstance().getClientInterface(); + //Config& config= Config::getInstance(); + Ip serverIp(ipString); + + //int serverPort = Config::getInstance().getInt("PortServer",intToStr(GameConstants::serverPort).c_str()); + int serverPort = port; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] try to connect to [%s] serverPort = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, + serverIp.getString().c_str(), serverPort); + clientInterface->connect(serverIp, serverPort); + if (clientInterface->isConnected() == false) { + NetworkManager::getInstance().end(); + NetworkManager::getInstance().init(nrClient); + + mainMessageBoxState = 1; + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("CouldNotConnect"), + lang.getString("ConnectionFailed"), false); + return false; + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] connection failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] connected to [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, + serverIp.getString().c_str()); + + //save server ip + //config.setString("ServerIp", serverIp.getString()); + //config.save(); + + for (time_t elapsedWait = time(NULL); + clientInterface->getIntroDone() == false && + clientInterface->isConnected() && + difftime(time(NULL), elapsedWait) <= 8;) { + if (clientInterface->isConnected()) { + //update lobby + clientInterface->updateLobby(); + sleep(0); + //this->render(); + } + } + if (clientInterface->isConnected() == true && + clientInterface->getIntroDone() == true) { + return true; + } + + return false; + } + } + + void MenuStateMasterserver::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + + bool MenuStateMasterserver::textInput(std::string text) { + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + return chatManager.textInput(text); + } + return false; + } + + void MenuStateMasterserver::keyDown(SDL_KeyboardEvent key) { + //printf("In [%s::%s Line: %d] key [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,key.keysym.sym); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + + if (ircClient != NULL && ircClient->isConnected() == true + && ircClient->getHasJoinedChannel() == true) { + //chatmanger only if connected to irc! + if (chatManager.getEditEnabled() == true) { + //printf("keyDown key [%d] chatManager.getText() [%s]\n",key,chatManager.getText().c_str()); + MutexSafeWrapper safeMutexIRCPtr(mutexIRCClient, + string + (extractFileFromDirectoryPath + (__FILE__).c_str()) + "_" + + intToStr(__LINE__)); + //if (key == vkReturn && ircClient != NULL) { + if (isKeyPressed(SDLK_RETURN, key, false) == true + && ircClient != NULL) { + ircClient->SendIRCCmdMessage(IRC_CHANNEL, + chatManager.getText()); + } + } + chatManager.keyDown(key); + } + if (chatManager.getEditEnabled() == false) { + //if(key == configKeys.getCharKey("ToggleMusic")) { + if (isKeyPressed(configKeys.getSDLKey("ToggleMusic"), key) == true) { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + + float configVolume = (config.getInt("SoundVolumeMusic") / 100.f); + float currentVolume = + CoreData::getInstance().getMenuMusic()->getVolume(); + if (currentVolume > 0) { + CoreData::getInstance().getMenuMusic()->setVolume(0.f); + consoleIRC.addLine(lang.getString("GameMusic") + " " + + lang.getString("Off")); + } else { + CoreData::getInstance().getMenuMusic()-> + setVolume(configVolume); + //If the config says zero, use the default music volume + //gameMusic->setVolume(configVolume ? configVolume : 0.9); + consoleIRC.addLine(lang.getString("GameMusic")); + } + } + //else if(key == configKeys.getCharKey("SaveGUILayout")) { + else if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == + true) { + bool saved = + GraphicComponent::saveAllCustomProperties(containerName); + Lang & lang = Lang::getInstance(); + consoleIRC.addLine(lang.getString("GUILayoutSaved") + " [" + + (saved ? lang.getString("Yes") : lang. + getString("No")) + "]"); + } + } + } + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_masterserver.h b/source/glest_game/menu/menu_state_masterserver.h index 0fabd00b1..d4582849e 100644 --- a/source/glest_game/menu/menu_state_masterserver.h +++ b/source/glest_game/menu/menu_state_masterserver.h @@ -33,148 +33,144 @@ # include "chat_manager.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateMasterserver -// =============================== - typedef vector < ServerLine * >ServerLines; - typedef vector < GraphicButton * >UserButtons; - typedef vector < MasterServerInfo * >MasterServerInfos; + // =============================== + // class MenuStateMasterserver + // =============================== + typedef vector < ServerLine * >ServerLines; + typedef vector < GraphicButton * >UserButtons; + typedef vector < MasterServerInfo * >MasterServerInfos; - class MenuStateMasterserver:public MenuState, - public SimpleTaskCallbackInterface, public IRCCallbackInterface - { + class MenuStateMasterserver :public MenuState, + public SimpleTaskCallbackInterface, public IRCCallbackInterface { - private: + private: - GraphicButton buttonRefresh; - GraphicButton buttonReturn; - GraphicButton buttonCreateGame; - GraphicLabel labelAutoRefresh; - GraphicListBox listBoxAutoRefresh; - GraphicLabel labelTitle; + GraphicButton buttonRefresh; + GraphicButton buttonReturn; + GraphicButton buttonCreateGame; + GraphicLabel labelAutoRefresh; + GraphicListBox listBoxAutoRefresh; + GraphicLabel labelTitle; - GraphicLabel announcementLabel; - GraphicLabel versionInfoLabel; + GraphicLabel announcementLabel; + GraphicLabel versionInfoLabel; - GraphicLine lines[3]; + GraphicLine lines[3]; - GraphicLabel glestVersionLabel; - GraphicLabel platformLabel; - //GraphicLabel binaryCompileDateLabel; + GraphicLabel glestVersionLabel; + GraphicLabel platformLabel; + //GraphicLabel binaryCompileDateLabel; - //game info: - GraphicLabel serverTitleLabel; - GraphicLabel countryLabel; - GraphicLabel statusLabel; + //game info: + GraphicLabel serverTitleLabel; + GraphicLabel countryLabel; + GraphicLabel statusLabel; - GraphicLabel ipAddressLabel; + GraphicLabel ipAddressLabel; - //game setup info: - GraphicLabel techLabel; - GraphicLabel mapLabel; - GraphicLabel tilesetLabel; - GraphicLabel activeSlotsLabel; + //game setup info: + GraphicLabel techLabel; + GraphicLabel mapLabel; + GraphicLabel tilesetLabel; + GraphicLabel activeSlotsLabel; - GraphicLabel externalConnectPort; + GraphicLabel externalConnectPort; - GraphicLabel selectButton; + GraphicLabel selectButton; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - GraphicLabel ircOnlinePeopleLabel; - GraphicLabel ircOnlinePeopleStatusLabel; + GraphicLabel ircOnlinePeopleLabel; + GraphicLabel ircOnlinePeopleStatusLabel; - bool announcementLoaded; - bool needUpdateFromServer; - int autoRefreshTime; - time_t lastRefreshTimer; - SimpleTaskThread *updateFromMasterserverThread; - bool playServerFoundSound; - ServerLines serverLines; - string serverInfoString; - int serverLinesToRender; - int serverLinesYBase; - int serverLinesLineHeight; - GraphicScrollBar userScrollBar; - GraphicScrollBar serverScrollBar; - UserButtons userButtons; - UserButtons userButtonsToRemove; - int userButtonsToRender; - int userButtonsYBase; - int userButtonsXBase; - int userButtonsLineHeight; - int userButtonsHeight; - int userButtonsWidth; - string currentIrcNick; + bool announcementLoaded; + bool needUpdateFromServer; + int autoRefreshTime; + time_t lastRefreshTimer; + SimpleTaskThread *updateFromMasterserverThread; + bool playServerFoundSound; + ServerLines serverLines; + string serverInfoString; + int serverLinesToRender; + int serverLinesYBase; + int serverLinesLineHeight; + GraphicScrollBar userScrollBar; + GraphicScrollBar serverScrollBar; + UserButtons userButtons; + UserButtons userButtonsToRemove; + int userButtonsToRender; + int userButtonsYBase; + int userButtonsXBase; + int userButtonsLineHeight; + int userButtonsHeight; + int userButtonsWidth; + string currentIrcNick; - //Console console; + //Console console; - static DisplayMessageFunction pCB_DisplayMessage; - std::string threadedErrorMsg; + static DisplayMessageFunction pCB_DisplayMessage; + std::string threadedErrorMsg; - std::vector < string > ircArgs; - Mutex *mutexIRCClient; - IRCThread *ircClient; - std::vector < string > oldNickList; + std::vector < string > ircArgs; + Mutex *mutexIRCClient; + IRCThread *ircClient; + std::vector < string > oldNickList; - Console consoleIRC; - ChatManager chatManager; + Console consoleIRC; + ChatManager chatManager; - bool masterserverParseErrorShown; + bool masterserverParseErrorShown; - public: - MenuStateMasterserver (Program * program, MainMenu * mainMenu); - ~MenuStateMasterserver (); + public: + MenuStateMasterserver(Program * program, MainMenu * mainMenu); + ~MenuStateMasterserver(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseUp (int x, int y, const MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void update (); - void render (); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseUp(int x, int y, const MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void update(); + void render(); - virtual bool textInput (std::string text); - virtual void keyDown (SDL_KeyboardEvent key); + virtual bool textInput(std::string text); + virtual void keyDown(SDL_KeyboardEvent key); - virtual void simpleTask (BaseThread * callingThread, void *userdata); - virtual bool isInSpecialKeyCaptureEvent () - { - return chatManager.getEditEnabled (); - } + virtual void simpleTask(BaseThread * callingThread, void *userdata); + virtual bool isInSpecialKeyCaptureEvent() { + return chatManager.getEditEnabled(); + } - static void setDisplayMessageFunction (DisplayMessageFunction - pDisplayMessage) - { - pCB_DisplayMessage = pDisplayMessage; - } + static void setDisplayMessageFunction(DisplayMessageFunction + pDisplayMessage) { + pCB_DisplayMessage = pDisplayMessage; + } - virtual void reloadUI (); + virtual void reloadUI(); - private: - void showMessageBox (const string & text, const string & header, - bool toggle); - bool connectToServer (string ipString, int port); - //void setConsolePos(int yPos); - void setButtonLinePosition (int pos); - void clearServerLines (); - void clearUserButtons (); - void rebuildServerLines (const string & serverInfo); - void cleanup (); - virtual void IRC_CallbackEvent (IRCEventType evt, const char *origin, - const char **params, - unsigned int count); + private: + void showMessageBox(const string & text, const string & header, + bool toggle); + bool connectToServer(string ipString, int port); + //void setConsolePos(int yPos); + void setButtonLinePosition(int pos); + void clearServerLines(); + void clearUserButtons(); + void rebuildServerLines(const string & serverInfo); + void cleanup(); + virtual void IRC_CallbackEvent(IRCEventType evt, const char *origin, + const char **params, + unsigned int count); - std::vector < string > nickList; + std::vector < string > nickList; - }; + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_mods.cpp b/source/glest_game/menu/menu_state_mods.cpp index e4475df9a..70b873a03 100644 --- a/source/glest_game/menu/menu_state_mods.cpp +++ b/source/glest_game/menu/menu_state_mods.cpp @@ -26,4014 +26,3555 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - using namespace::Shared::Util; - - struct FormatString - { - void operator () (string & s) - { - s = formatString (s); - } - }; - - - -// =============================== -// class ModInfo -// =============================== - - ModInfo::ModInfo () - { - name = ""; - url = ""; - imageUrl = ""; - description = ""; - count = ""; - crc = ""; - type = mt_None; - } - - - -// ===================================================== -// class MenuStateConnectedGame -// ===================================================== - - MenuStateMods::MenuStateMods (Program * program, - MainMenu * mainMenu):MenuState (program, - mainMenu, - "mods") - { - - containerName = "Mods"; - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); - - modPreviewImage = NULL; - displayModPreviewImage.clear (); - - ftpClientThread = NULL; - selectedTechName = ""; - selectedTilesetName = ""; - selectedMapName = ""; - selectedScenarioName = ""; - modInfoSelected = NULL; - showFullConsole = false; - keyButtonsLineHeight = 20; - keyButtonsHeight = 20; - keyButtonsWidth = 200; - keyButtonsWidthTech = keyButtonsWidth; - keyButtonsWidthMap = keyButtonsWidth + 15; - keyButtonsWidthTil = keyButtonsWidth - 5; - keyButtonsWidthScen = keyButtonsWidth + 20; - scrollListsYPos = 700; - listBoxLength = 200; - keyButtonsYBase = scrollListsYPos; - keyButtonsToRender = listBoxLength / keyButtonsLineHeight; - labelWidth = 5; - keyButtonsXBase = 0; - modMenuState = mmst_None; - oldMenuState = mmst_None; - - int installButtonYPos = scrollListsYPos - listBoxLength - 20; - - int returnLineY = 80; - enableMapPreview = config.getBool ("MapPreview", "true"); - validMapPreview = false; - mapPreviewTexture = NULL; - - //create - techInfoXPos = 5; - keyTechScrollBarTitle1.registerGraphicComponent (containerName, - "keyTechScrollBarTitle1"); - keyTechScrollBarTitle1.init (techInfoXPos + 5, scrollListsYPos + 30, - labelWidth, 20); - keyTechScrollBarTitle1.setText (lang.getString ("TechTitle1")); - keyTechScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyTechScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - keyTechScrollBarTitle2.registerGraphicComponent (containerName, - "keyTechScrollBarTitle2"); - keyTechScrollBarTitle2.init (techInfoXPos - 10 + keyButtonsWidthTech, - scrollListsYPos + 17, labelWidth, 20); - keyTechScrollBarTitle2.setText (lang.getString ("TechTitle2")); - keyTechScrollBarTitle2.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - keyTechScrollBarTitle2.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - mapInfoXPos = 260; - keyMapScrollBarTitle1.registerGraphicComponent (containerName, - "keyMapScrollBarTitle1"); - keyMapScrollBarTitle1.init (mapInfoXPos + 5, scrollListsYPos + 30, - labelWidth, 20); - keyMapScrollBarTitle1.setText (lang.getString ("MapTitle1")); - keyMapScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyMapScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - keyMapScrollBarTitle2.registerGraphicComponent (containerName, - "keyMapScrollBarTitle2"); - keyMapScrollBarTitle2.init (mapInfoXPos - 10 + keyButtonsWidthMap, - scrollListsYPos + 17, labelWidth, 20); - keyMapScrollBarTitle2.setText (lang.getString ("MapTitle2")); - keyMapScrollBarTitle2.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - keyMapScrollBarTitle2.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - tilesetInfoXPos = 530; - keyTilesetScrollBarTitle1.registerGraphicComponent (containerName, - "keyTilesetScrollBarTitle1"); - keyTilesetScrollBarTitle1.init (tilesetInfoXPos + 5, - scrollListsYPos + 30, labelWidth, 20); - keyTilesetScrollBarTitle1.setText (lang.getString ("TilesetTitle1")); - keyTilesetScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyTilesetScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - - scenarioInfoXPos = 755; - keyScenarioScrollBarTitle1.registerGraphicComponent (containerName, - "keyScenarioScrollBarTitle1"); - keyScenarioScrollBarTitle1.init (scenarioInfoXPos, scrollListsYPos + 30, - labelWidth, 20); - keyScenarioScrollBarTitle1.setText (lang.getString ("ScenarioTitle1")); - keyScenarioScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyScenarioScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - mainMessageBoxState = ftpmsg_None; - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No"), - 450); - mainMessageBox.setEnabled (false); - - lineHorizontal.init (0, installButtonYPos - 60); - lineVertical.init (500, returnLineY, 5, - installButtonYPos - 60 - returnLineY); - lineVertical.setHorizontal (false); - lineReturn.init (0, returnLineY); - - modDescrLabel.registerGraphicComponent (containerName, "modDescrLabel"); - modDescrLabel.init (15, installButtonYPos - 60 - 20, 450, 20); - modDescrLabel.setWordWrap (true); - modDescrLabel.setText ("description is empty"); - - pleaseWaitLabel.registerGraphicComponent (containerName, - "pleaseWaitLabel"); - pleaseWaitLabel.init (50, installButtonYPos - 120, 450, 20); - pleaseWaitLabel.setText (""); - pleaseWaitLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - pleaseWaitLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (800, returnLineY - 30, 125); - buttonReturn.setText (lang.getString ("Return")); - - lineVerticalReturn.init (buttonReturn.getX () - 10, returnLineY - 80, 5, - 81); - lineVerticalReturn.setHorizontal (false); - - int buttonLineUpY = installButtonYPos + 10; - int buttonLineDownY = installButtonYPos - 20; - - int legendButtonY = buttonLineDownY - 30; - buttonInstalled.registerGraphicComponent (containerName, - "buttonInstalled"); - buttonInstalled.init (5, legendButtonY, 240); - buttonInstalled.setText (" " + lang.getString ("ModInstalled")); - buttonInstalled.setUseCustomTexture (true); - buttonInstalled.setCustomTexture (CoreData::getInstance (). - getOnServerInstalledTexture ()); - buttonInstalled.setEnabled (false); - - buttonAvailable.registerGraphicComponent (containerName, - "buttonAvailable"); - buttonAvailable.init (255, legendButtonY, 240); - buttonAvailable.setUseCustomTexture (true); - buttonAvailable.setCustomTexture (CoreData::getInstance (). - getOnServerTexture ()); - buttonAvailable.setText (" " + lang.getString ("ModAvailable")); - - buttonOnlyLocal.registerGraphicComponent (containerName, - "buttonOnlyLocal"); - buttonOnlyLocal.init (505, legendButtonY, 240); - buttonOnlyLocal.setUseCustomTexture (true); - buttonOnlyLocal.setCustomTexture (CoreData::getInstance (). - getNotOnServerTexture ()); - buttonOnlyLocal.setText (" " + lang.getString ("ModOnlyLocal")); - - buttonConflict.registerGraphicComponent (containerName, - "buttonConflict"); - buttonConflict.init (755, legendButtonY, 240); - buttonConflict.setUseCustomTexture (true); - buttonConflict.setCustomTexture (CoreData::getInstance (). - getOnServerDifferentTexture ()); - buttonConflict.setText (" " + lang.getString ("ModHasConflict")); - - - buttonInstallTech.registerGraphicComponent (containerName, - "buttonInstallTech"); - buttonInstallTech.init (techInfoXPos + 45, buttonLineUpY, 125); - buttonInstallTech.setText (lang.getString ("Install")); - buttonRemoveTech.registerGraphicComponent (containerName, - "buttonRemoveTech"); - buttonRemoveTech.init (techInfoXPos + 45, buttonLineDownY, 125); - buttonRemoveTech.setText (lang.getString ("Remove")); - - buttonInstallTileset.registerGraphicComponent (containerName, - "buttonInstallTileset"); - buttonInstallTileset.init (tilesetInfoXPos + 30, buttonLineUpY, 125); - buttonInstallTileset.setText (lang.getString ("Install")); - buttonRemoveTileset.registerGraphicComponent (containerName, - "buttonRemoveTileset"); - buttonRemoveTileset.init (tilesetInfoXPos + 30, buttonLineDownY, 125); - buttonRemoveTileset.setText (lang.getString ("Remove")); - - buttonInstallMap.registerGraphicComponent (containerName, - "buttonInstallMap"); - buttonInstallMap.init (mapInfoXPos + 50, buttonLineUpY, 125); - buttonInstallMap.setText (lang.getString ("Install")); - buttonRemoveMap.registerGraphicComponent (containerName, - "buttonRemoveMap"); - buttonRemoveMap.init (mapInfoXPos + 50, buttonLineDownY, 125); - buttonRemoveMap.setText (lang.getString ("Remove")); - - buttonInstallScenario.registerGraphicComponent (containerName, - "buttonInstallScenario"); - buttonInstallScenario.init (scenarioInfoXPos + 45, buttonLineUpY, 125); - buttonInstallScenario.setText (lang.getString ("Install")); - buttonRemoveScenario.registerGraphicComponent (containerName, - "buttonRemoveScenario"); - buttonRemoveScenario.init (scenarioInfoXPos + 45, buttonLineDownY, 125); - buttonRemoveScenario.setText (lang.getString ("Remove")); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - keyTilesetScrollBar.init (tilesetInfoXPos + keyButtonsWidthTil, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyTilesetScrollBar.setLength (listBoxLength); - keyTilesetScrollBar.setElementCount (0); - keyTilesetScrollBar.setVisibleSize (keyButtonsToRender); - keyTilesetScrollBar.setVisibleStart (0); - - keyTechScrollBar.init (techInfoXPos + keyButtonsWidthTech + labelWidth + - 20, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyTechScrollBar.setLength (listBoxLength); - keyTechScrollBar.setElementCount (0); - keyTechScrollBar.setVisibleSize (keyButtonsToRender); - keyTechScrollBar.setVisibleStart (0); - - keyMapScrollBar.init (mapInfoXPos + keyButtonsWidthMap + labelWidth + - 20, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyMapScrollBar.setLength (listBoxLength); - keyMapScrollBar.setElementCount (0); - keyMapScrollBar.setVisibleSize (keyButtonsToRender); - keyMapScrollBar.setVisibleStart (0); - - keyScenarioScrollBar.init (scenarioInfoXPos + keyButtonsWidthScen, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyScenarioScrollBar.setLength (listBoxLength); - keyScenarioScrollBar.setElementCount (0); - keyScenarioScrollBar.setVisibleSize (keyButtonsToRender); - keyScenarioScrollBar.setVisibleStart (0); - - GraphicComponent::applyAllCustomProperties (containerName); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - // Start http meta data thread - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - modHttpServerThread = new SimpleTaskThread (this, 0, 200); - modHttpServerThread->setUniqueID (mutexOwnerId); - modHttpServerThread->start (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - // Setup File Transfer thread - findDirs (config.getPathListForType (ptTilesets), tilesetFiles); - findDirs (config.getPathListForType (ptTechs), techTreeFiles); - - vector < string > mapPathList = config.getPathListForType (ptMaps); - std::pair < string, string > mapsPath; - if (mapPathList.empty () == false) - { - mapsPath.first = mapPathList[0]; - } - if (mapPathList.size () > 1) - { - mapsPath.second = mapPathList[1]; - } - std::pair < string, string > tilesetsPath; - vector < string > tilesetsList = - Config::getInstance ().getPathListForType (ptTilesets); - if (tilesetsList.empty () == false) - { - tilesetsPath.first = tilesetsList[0]; - if (tilesetsList.size () > 1) - { - tilesetsPath.second = tilesetsList[1]; - } - } - - std::pair < string, string > techtreesPath; - vector < string > techtreesList = - Config::getInstance ().getPathListForType (ptTechs); - if (techtreesList.empty () == false) - { - techtreesPath.first = techtreesList[0]; - if (techtreesList.size () > 1) - { - techtreesPath.second = techtreesList[1]; - } - } - - std::pair < string, string > scenariosPath; - vector < string > scenariosList = - Config::getInstance ().getPathListForType (ptScenarios); - if (scenariosList.empty () == false) - { - scenariosPath.first = scenariosList[0]; - if (scenariosList.size () > 1) - { - scenariosPath.second = scenariosList[1]; - } - } - - string fileArchiveExtension = - config.getString ("FileArchiveExtension", ""); - string fileArchiveExtractCommand = - config.getString ("FileArchiveExtractCommand", ""); - string fileArchiveExtractCommandParameters = - config.getString ("FileArchiveExtractCommandParameters", ""); - int32 fileArchiveExtractCommandSuccessResult = - config.getInt ("FileArchiveExtractCommandSuccessResult", "0"); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - console.setOnlyChatMessagesInStoredLines (false); - - // Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - tempFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Temp files path [%s]\n", tempFilePath.c_str ()); - - ftpClientThread = new FTPClientThread (-1, "", - mapsPath, tilesetsPath, - techtreesPath, scenariosPath, - this, fileArchiveExtension, - fileArchiveExtractCommand, - fileArchiveExtractCommandParameters, - fileArchiveExtractCommandSuccessResult, - tempFilePath); - ftpClientThread->start (); - - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void MenuStateMods::reloadUI () - { - Lang & lang = Lang::getInstance (); - - console.resetFonts (); - keyTechScrollBarTitle1.setText (lang.getString ("TechTitle1")); - keyTechScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyTechScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - keyTechScrollBarTitle2.setText (lang.getString ("TechTitle2")); - keyTechScrollBarTitle2.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - keyTechScrollBarTitle2.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - keyMapScrollBarTitle1.setText (lang.getString ("MapTitle1")); - keyMapScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyMapScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - keyMapScrollBarTitle2.setText (lang.getString ("MapTitle2")); - keyMapScrollBarTitle2.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - keyMapScrollBarTitle2.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - keyTilesetScrollBarTitle1.setText (lang.getString ("TilesetTitle1")); - keyTilesetScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyTilesetScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - pleaseWaitLabel.setText (""); - pleaseWaitLabel.setFont (CoreData::getInstance ().getMenuFontBig ()); - pleaseWaitLabel.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - keyScenarioScrollBarTitle1.setText (lang.getString ("ScenarioTitle1")); - keyScenarioScrollBarTitle1.setFont (CoreData::getInstance (). - getMenuFontBig ()); - keyScenarioScrollBarTitle1.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No"), - 450); - - modDescrLabel.setText ("description is empty"); - - buttonReturn.setText (lang.getString ("Return")); - - buttonInstalled.setText (lang.getString ("ModInstalled")); - - buttonAvailable.setText (lang.getString ("ModAvailable")); - - buttonOnlyLocal.setText (lang.getString ("ModOnlyLocal")); - - buttonConflict.setText (lang.getString ("ModHasConflict")); - - buttonInstallTech.setText (lang.getString ("Install")); - buttonRemoveTech.setText (lang.getString ("Remove")); - - buttonInstallTileset.setText (lang.getString ("Install")); - buttonRemoveTileset.setText (lang.getString ("Remove")); - - buttonInstallMap.setText (lang.getString ("Install")); - buttonRemoveMap.setText (lang.getString ("Remove")); - - buttonInstallScenario.setText (lang.getString ("Install")); - buttonRemoveScenario.setText (lang.getString ("Remove")); - - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } - - void MenuStateMods::simpleTask (BaseThread * callingThread, - void *userdata) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutexThreadOwner (callingThread-> - getMutexThreadOwnerValid (), - mutexOwnerId); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - callingThread->getMutexThreadOwnerValid ()->setOwnerId (mutexOwnerId); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - Config & config = Config::getInstance (); - string fileArchiveExtractCommand = - config.getString ("FileArchiveExtractCommand", ""); - int expectedResult = - config.getInt ("FileArchiveExtractCommandSuccessResult", "0"); - bool findArchive = - executeShellCommand (fileArchiveExtractCommand, expectedResult); - if (findArchive == false) - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModRequires7z"), - lang.getString ("Notice"), true); - } - - std::string techsMetaData = ""; - std::string tilesetsMetaData = ""; - std::string mapsMetaData = ""; - std::string scenariosMetaData = ""; - - modMenuState = mmst_Loading; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (config.getString ("Masterserver", "") != "") - { - string baseURL = config.getString ("Masterserver"); - if (baseURL != "") - { - endPathWithSlash (baseURL, false); - } - - string phpVersionParam = - config.getString ("phpVersionParam", "?version=0.1"); - string playerUUID = - "&uuid=" + - SystemFlags::escapeURL (Config::getInstance (). - getString ("PlayerId", "")); - string gameVersion = - "&glestVersion=" + SystemFlags::escapeURL (glestVersionString); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", - __FILE__, __FUNCTION__, __LINE__, baseURL.c_str ()); - - CURL *handle = SystemFlags::initHTTP (); - CURLcode curlResult = CURLE_OK; - techsMetaData = - SystemFlags::getHTTP (baseURL + "showTechsForGlest.php" + - phpVersionParam + gameVersion + playerUUID, - handle, -1, &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("techsMetaData [%s] curlResult = %d\n", - techsMetaData.c_str (), curlResult); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#1 ") + szBuf, true); - } - - if (curlResult == CURLE_OK || - (curlResult != CURLE_COULDNT_RESOLVE_HOST && - curlResult != CURLE_COULDNT_CONNECT)) - { - - tilesetsMetaData = - SystemFlags::getHTTP (baseURL + "showTilesetsForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("tilesetsMetaData [%s]\n", tilesetsMetaData.c_str ()); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#2 ") + szBuf, true); - } - - mapsMetaData = - SystemFlags::getHTTP (baseURL + "showMapsForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("mapsMetaData [%s]\n", mapsMetaData.c_str ()); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#3 ") + szBuf, true); - } - - scenariosMetaData = - SystemFlags::getHTTP (baseURL + "showScenariosForGlest.php" + - phpVersionParam + gameVersion, handle, -1, - &curlResult); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("scenariosMetaData [%s]\n", scenariosMetaData.c_str ()); - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModErrorGettingServerData").c_str (), - curlError.c_str ()); - console.addLine (string ("#4 ") + szBuf, true); - } - } - SystemFlags::cleanupHTTP (&handle); - } - else - { - console.addLine (lang.getString ("MasterServerMissing"), true); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - tilesetListRemote.clear (); - Tokenize (tilesetsMetaData, tilesetListRemote, "\n"); - - modMenuState = mmst_CalculatingCRC; - - getTilesetsLocalList (); - for (unsigned int i = 0; i < tilesetListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string result = refreshTilesetModInfo (tilesetListRemote[i]); - if (result != "") - { - ModInfo modinfo; - modinfo = tilesetCacheList[result]; - GraphicButton *button = new GraphicButton (); - button->init (tilesetInfoXPos, keyButtonsYBase, keyButtonsWidthTil, - keyButtonsHeight); - button->setText (modinfo.name); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyTilesetButtons.push_back (button); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - for (unsigned int i = 0; i < tilesetFilesUserData.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string tilesetName = tilesetFilesUserData[i]; - bool alreadyHasTileset = - (tilesetCacheList.find (tilesetName) != tilesetCacheList.end ()); - if (alreadyHasTileset == false) - { - GraphicButton *button = new GraphicButton (); - button->init (tilesetInfoXPos, keyButtonsYBase, keyButtonsWidthTil, - keyButtonsHeight); - button->setText (tilesetName); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyTilesetButtons.push_back (button); - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - techListRemote.clear (); - Tokenize (techsMetaData, techListRemote, "\n"); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - getTechsLocalList (); - for (unsigned int i = 0; i < techListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string result = refreshTechModInfo (techListRemote[i]); - if (result != "") - { - ModInfo modinfo; - modinfo = techCacheList[result]; - - GraphicButton *button = new GraphicButton (); - button->init (techInfoXPos, keyButtonsYBase, keyButtonsWidthTech, - keyButtonsHeight); - button->setText (modinfo.name); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - - keyTechButtons.push_back (button); - GraphicLabel *label = new GraphicLabel (); - label->init (techInfoXPos + keyButtonsWidthTech + 10, - keyButtonsYBase, labelWidth, 20); - label->setText (modinfo.count); - labelsTech.push_back (label); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - for (unsigned int i = 0; i < techTreeFilesUserData.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string techName = techTreeFilesUserData[i]; - bool alreadyHasTech = - (techCacheList.find (techName) != techCacheList.end ()); - if (alreadyHasTech == false) - { - vector < string > techPaths = config.getPathListForType (ptTechs); - string & techPath = techPaths[1]; - endPathWithSlash (techPath); - vector < string > factions; - findAll (techPath + techName + "/factions/*.", factions, false, - false); - - GraphicButton *button = new GraphicButton (); - button->init (techInfoXPos, keyButtonsYBase, keyButtonsWidthTech, - keyButtonsHeight); - button->setText (techName); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyTechButtons.push_back (button); - - int techFactionCount = (int) factions.size (); - GraphicLabel *label = new GraphicLabel (); - label->init (techInfoXPos + keyButtonsWidthTech + 10, - keyButtonsYBase, labelWidth, 20); - label->setText (intToStr (techFactionCount)); - labelsTech.push_back (label); - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - mapListRemote.clear (); - Tokenize (mapsMetaData, mapListRemote, "\n"); - - getMapsLocalList (); - for (unsigned int i = 0; i < mapListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string result = refreshMapModInfo (mapListRemote[i]); - if (result != "") - { - ModInfo modinfo; - modinfo = mapCacheList[result]; - - GraphicButton *button = new GraphicButton (); - button->init (mapInfoXPos, keyButtonsYBase, keyButtonsWidthMap, - keyButtonsHeight); - button->setText (modinfo.name); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyMapButtons.push_back (button); - - GraphicLabel *label = new GraphicLabel (); - label->init (mapInfoXPos + keyButtonsWidthMap + 10, keyButtonsYBase, - labelWidth, 20); - label->setText (modinfo.count); - labelsMap.push_back (label); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - for (unsigned int i = 0; i < mapFilesUserData.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string mapName = mapFilesUserData[i]; - bool alreadyHasMap = - (mapCacheList.find (mapName) != mapCacheList.end ()); - if (alreadyHasMap == false) - { - vector < string > mapPaths = config.getPathListForType (ptMaps); - string & mapPath = mapPaths[1]; - endPathWithSlash (mapPath); - mapPath += mapName; - MapInfo mapInfo = loadMapInfo (mapPath); - - GraphicButton *button = new GraphicButton (); - button->init (mapInfoXPos, keyButtonsYBase, keyButtonsWidthMap, - keyButtonsHeight); - button->setText (mapName); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyMapButtons.push_back (button); - - int mapPlayerCount = mapInfo.players; - GraphicLabel *label = new GraphicLabel (); - label->init (mapInfoXPos + keyButtonsWidthMap + 10, keyButtonsYBase, - labelWidth, 20); - label->setText (intToStr (mapPlayerCount)); - labelsMap.push_back (label); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - - scenarioListRemote.clear (); - Tokenize (scenariosMetaData, scenarioListRemote, "\n"); - - getScenariosLocalList (); - for (unsigned int i = 0; i < scenarioListRemote.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string result = refreshScenarioModInfo (scenarioListRemote[i]); - if (result != "") - { - ModInfo modinfo; - modinfo = scenarioCacheList[result]; - GraphicButton *button = new GraphicButton (); - button->init (scenarioInfoXPos, keyButtonsYBase, - keyButtonsWidthScen, keyButtonsHeight); - button->setText (modinfo.name); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyScenarioButtons.push_back (button); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - for (unsigned int i = 0; i < scenarioFilesUserData.size (); i++) - { - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - string scenarioName = scenarioFilesUserData[i]; - bool alreadyHasScenario = - (scenarioCacheList.find (scenarioName) != scenarioCacheList.end ()); - if (alreadyHasScenario == false) - { - vector < string > scenarioPaths = - config.getPathListForType (ptScenarios); - string & scenarioPath = scenarioPaths[1]; - endPathWithSlash (scenarioPath); - scenarioPath += scenarioName; - - GraphicButton *button = new GraphicButton (); - button->init (scenarioInfoXPos, keyButtonsYBase, - keyButtonsWidthScen, keyButtonsHeight); - button->setText (scenarioName); - button->setUseCustomTexture (true); - button->setCustomTexture (CoreData::getInstance (). - getCustomTexture ()); - keyScenarioButtons.push_back (button); - } - } - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - keyTilesetScrollBar.init (tilesetInfoXPos + keyButtonsWidthTil, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyTilesetScrollBar.setLength (listBoxLength); - keyTilesetScrollBar.setElementCount ((int) keyTilesetButtons.size ()); - keyTilesetScrollBar.setVisibleSize (keyButtonsToRender); - keyTilesetScrollBar.setVisibleStart (0); - - keyTechScrollBar.init (techInfoXPos + keyButtonsWidthTech + labelWidth + - 20, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyTechScrollBar.setLength (listBoxLength); - keyTechScrollBar.setElementCount ((int) keyTechButtons.size ()); - keyTechScrollBar.setVisibleSize (keyButtonsToRender); - keyTechScrollBar.setVisibleStart (0); - - keyMapScrollBar.init (mapInfoXPos + keyButtonsWidthMap + labelWidth + - 20, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyMapScrollBar.setLength (listBoxLength); - keyMapScrollBar.setElementCount ((int) keyMapButtons.size ()); - keyMapScrollBar.setVisibleSize (keyButtonsToRender); - keyMapScrollBar.setVisibleStart (0); - - keyScenarioScrollBar.init (scenarioInfoXPos + keyButtonsWidthScen, - scrollListsYPos - listBoxLength + - keyButtonsLineHeight, false, 200, 20); - keyScenarioScrollBar.setLength (listBoxLength); - keyScenarioScrollBar.setElementCount ((int) keyScenarioButtons.size ()); - keyScenarioScrollBar.setVisibleSize (keyButtonsToRender); - keyScenarioScrollBar.setVisibleStart (0); - - modMenuState = mmst_None; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (modHttpServerThread != NULL) - { - modHttpServerThread->signalQuit (); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - MapInfo MenuStateMods::loadMapInfo (string file) - { - try - { - Lang & lang = Lang::getInstance (); - // Not painting properly so this is on hold - MapPreview::loadMapInfo (file, &mapInfo, - lang.getString ("MaxPlayers"), - lang.getString ("Size"), true); - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s] loading map [%s]\n", - __FILE__, __FUNCTION__, __LINE__, e.what (), - file.c_str ()); - throw megaglest_runtime_error ("Error loading map file: [" + file + - "] msg: " + e.what ()); - } - - return mapInfo; - } - - void MenuStateMods::getTechsLocalList () - { - Config & config = Config::getInstance (); - vector < string > results; - findDirs (config.getPathListForType (ptTechs), results); - techTreeFiles = results; - - techTreeFilesUserData.clear (); - if (config.getPathListForType (ptTechs).size () > 1) - { - string path = config.getPathListForType (ptTechs)[1]; - endPathWithSlash (path); - findDirs (path, techTreeFilesUserData, false, false); - } - } - - string MenuStateMods::refreshTechModInfo (string techInfo) - { - std::vector < std::string > techInfoList; - Tokenize (techInfo, techInfoList, "|"); - if (techInfoList.size () >= 6) - { - Config & config = Config::getInstance (); - ModInfo modinfo; - modinfo.name = techInfoList[0]; - modinfo.count = techInfoList[1]; - modinfo.crc = techInfoList[2]; - modinfo.description = techInfoList[3]; - modinfo.url = techInfoList[4]; - modinfo.imageUrl = techInfoList[5]; - modinfo.type = mt_Techtree; - - string itemPath = - config.getPathListForType (ptTechs, - "")[1] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - uint32 crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", NULL, - forceRefresh); - if (crc == 0) - { - itemPath = - config.getPathListForType (ptTechs, - "")[0] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, forceRefresh); - } - } - modinfo.localCRC = uIntToStr (crc); - //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - modinfo.localCRC = ""; - } - techCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - void MenuStateMods::refreshTechs () - { - getTechsLocalList (); - for (int i = 0; i < (int) techListRemote.size (); i++) - { - refreshTechModInfo (techListRemote[i]); - } - } - - void MenuStateMods::getTilesetsLocalList () - { - Config & config = Config::getInstance (); - vector < string > results; - findDirs (config.getPathListForType (ptTilesets), results); - tilesetFiles = results; - - tilesetFilesUserData.clear (); - if (config.getPathListForType (ptTilesets).size () > 1) - { - string path = config.getPathListForType (ptTilesets)[1]; - endPathWithSlash (path); - findDirs (path, tilesetFilesUserData, false, false); - } - } - - string MenuStateMods::refreshTilesetModInfo (string tilesetInfo) - { - std::vector < std::string > tilesetInfoList; - Tokenize (tilesetInfo, tilesetInfoList, "|"); - if (tilesetInfoList.size () >= 5) - { - Config & config = Config::getInstance (); - ModInfo modinfo; - modinfo.name = tilesetInfoList[0]; - modinfo.crc = tilesetInfoList[1]; - modinfo.description = tilesetInfoList[2]; - modinfo.url = tilesetInfoList[3]; - modinfo.imageUrl = tilesetInfoList[4]; - modinfo.type = mt_Tileset; - - string itemPath = - config.getPathListForType (ptTilesets, - "")[1] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - uint32 crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", NULL, - forceRefresh); - if (crc == 0) - { - itemPath = - config.getPathListForType (ptTilesets, - "")[0] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, forceRefresh); - } - } - modinfo.localCRC = uIntToStr (crc); - //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - - //printf("#1 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); - } - else - { - modinfo.localCRC = ""; - - //printf("#2 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); - } - - tilesetCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - void MenuStateMods::refreshTilesets () - { - getTilesetsLocalList (); - for (int i = 0; i < (int) tilesetListRemote.size (); i++) - { - refreshTilesetModInfo (tilesetListRemote[i]); - } - } - - void MenuStateMods::getMapsLocalList () - { - -/* - Config &config = Config::getInstance(); - vector results; - set allMaps; - findAll(config.getPathListForType(ptMaps), "*.gbm", results, false, false); - copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); - results.clear(); - findAll(config.getPathListForType(ptMaps), "*.mgm", results, false, false); - copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); - results.clear(); - - copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); - mapFiles = results; - - mapFilesUserData.clear(); - if(config.getPathListForType(ptMaps).size() > 1) { - string path = config.getPathListForType(ptMaps)[1]; - endPathWithSlash(path); - - vector results2; - set allMaps2; - findAll(path + "*.gbm", results2, false, false); - copy(results2.begin(), results2.end(), std::inserter(allMaps2, allMaps2.begin())); - - results2.clear(); - findAll(path + "*.mgm", results2, false, false); - copy(results2.begin(), results2.end(), std::inserter(allMaps2, allMaps2.begin())); - - results2.clear(); - copy(allMaps2.begin(), allMaps2.end(), std::back_inserter(results2)); - mapFilesUserData = results2; - //printf("\n\nMap path [%s] mapFilesUserData.size() = %d\n\n\n",path.c_str(),mapFilesUserData.size()); +namespace Glest { + namespace Game { + + using namespace::Shared::Util; + + struct FormatString { + void operator () (string & s) { + s = formatString(s); + } + }; + + + + // =============================== + // class ModInfo + // =============================== + + ModInfo::ModInfo() { + name = ""; + url = ""; + imageUrl = ""; + description = ""; + count = ""; + crc = ""; + type = mt_None; + } + + + + // ===================================================== + // class MenuStateConnectedGame + // ===================================================== + + MenuStateMods::MenuStateMods(Program * program, + MainMenu * mainMenu) :MenuState(program, + mainMenu, + "mods") { + + containerName = "Mods"; + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); + + modPreviewImage = NULL; + displayModPreviewImage.clear(); + + ftpClientThread = NULL; + selectedTechName = ""; + selectedTilesetName = ""; + selectedMapName = ""; + selectedScenarioName = ""; + modInfoSelected = NULL; + showFullConsole = false; + keyButtonsLineHeight = 20; + keyButtonsHeight = 20; + keyButtonsWidth = 200; + keyButtonsWidthTech = keyButtonsWidth; + keyButtonsWidthMap = keyButtonsWidth + 15; + keyButtonsWidthTil = keyButtonsWidth - 5; + keyButtonsWidthScen = keyButtonsWidth + 20; + scrollListsYPos = 700; + listBoxLength = 200; + keyButtonsYBase = scrollListsYPos; + keyButtonsToRender = listBoxLength / keyButtonsLineHeight; + labelWidth = 5; + keyButtonsXBase = 0; + modMenuState = mmst_None; + oldMenuState = mmst_None; + + int installButtonYPos = scrollListsYPos - listBoxLength - 20; + + int returnLineY = 80; + enableMapPreview = config.getBool("MapPreview", "true"); + validMapPreview = false; + mapPreviewTexture = NULL; + + //create + techInfoXPos = 5; + keyTechScrollBarTitle1.registerGraphicComponent(containerName, + "keyTechScrollBarTitle1"); + keyTechScrollBarTitle1.init(techInfoXPos + 5, scrollListsYPos + 30, + labelWidth, 20); + keyTechScrollBarTitle1.setText(lang.getString("TechTitle1")); + keyTechScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyTechScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + keyTechScrollBarTitle2.registerGraphicComponent(containerName, + "keyTechScrollBarTitle2"); + keyTechScrollBarTitle2.init(techInfoXPos - 10 + keyButtonsWidthTech, + scrollListsYPos + 17, labelWidth, 20); + keyTechScrollBarTitle2.setText(lang.getString("TechTitle2")); + keyTechScrollBarTitle2.setFont(CoreData::getInstance(). + getMenuFontNormal()); + keyTechScrollBarTitle2.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + mapInfoXPos = 260; + keyMapScrollBarTitle1.registerGraphicComponent(containerName, + "keyMapScrollBarTitle1"); + keyMapScrollBarTitle1.init(mapInfoXPos + 5, scrollListsYPos + 30, + labelWidth, 20); + keyMapScrollBarTitle1.setText(lang.getString("MapTitle1")); + keyMapScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyMapScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + keyMapScrollBarTitle2.registerGraphicComponent(containerName, + "keyMapScrollBarTitle2"); + keyMapScrollBarTitle2.init(mapInfoXPos - 10 + keyButtonsWidthMap, + scrollListsYPos + 17, labelWidth, 20); + keyMapScrollBarTitle2.setText(lang.getString("MapTitle2")); + keyMapScrollBarTitle2.setFont(CoreData::getInstance(). + getMenuFontNormal()); + keyMapScrollBarTitle2.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + tilesetInfoXPos = 530; + keyTilesetScrollBarTitle1.registerGraphicComponent(containerName, + "keyTilesetScrollBarTitle1"); + keyTilesetScrollBarTitle1.init(tilesetInfoXPos + 5, + scrollListsYPos + 30, labelWidth, 20); + keyTilesetScrollBarTitle1.setText(lang.getString("TilesetTitle1")); + keyTilesetScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyTilesetScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + + scenarioInfoXPos = 755; + keyScenarioScrollBarTitle1.registerGraphicComponent(containerName, + "keyScenarioScrollBarTitle1"); + keyScenarioScrollBarTitle1.init(scenarioInfoXPos, scrollListsYPos + 30, + labelWidth, 20); + keyScenarioScrollBarTitle1.setText(lang.getString("ScenarioTitle1")); + keyScenarioScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyScenarioScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + mainMessageBoxState = ftpmsg_None; + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Yes"), lang.getString("No"), + 450); + mainMessageBox.setEnabled(false); + + lineHorizontal.init(0, installButtonYPos - 60); + lineVertical.init(500, returnLineY, 5, + installButtonYPos - 60 - returnLineY); + lineVertical.setHorizontal(false); + lineReturn.init(0, returnLineY); + + modDescrLabel.registerGraphicComponent(containerName, "modDescrLabel"); + modDescrLabel.init(15, installButtonYPos - 60 - 20, 450, 20); + modDescrLabel.setWordWrap(true); + modDescrLabel.setText("description is empty"); + + pleaseWaitLabel.registerGraphicComponent(containerName, + "pleaseWaitLabel"); + pleaseWaitLabel.init(50, installButtonYPos - 120, 450, 20); + pleaseWaitLabel.setText(""); + pleaseWaitLabel.setFont(CoreData::getInstance().getMenuFontBig()); + pleaseWaitLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(800, returnLineY - 30, 125); + buttonReturn.setText(lang.getString("Return")); + + lineVerticalReturn.init(buttonReturn.getX() - 10, returnLineY - 80, 5, + 81); + lineVerticalReturn.setHorizontal(false); + + int buttonLineUpY = installButtonYPos + 10; + int buttonLineDownY = installButtonYPos - 20; + + int legendButtonY = buttonLineDownY - 30; + buttonInstalled.registerGraphicComponent(containerName, + "buttonInstalled"); + buttonInstalled.init(5, legendButtonY, 240); + buttonInstalled.setText(" " + lang.getString("ModInstalled")); + buttonInstalled.setUseCustomTexture(true); + buttonInstalled.setCustomTexture(CoreData::getInstance(). + getOnServerInstalledTexture()); + buttonInstalled.setEnabled(false); + + buttonAvailable.registerGraphicComponent(containerName, + "buttonAvailable"); + buttonAvailable.init(255, legendButtonY, 240); + buttonAvailable.setUseCustomTexture(true); + buttonAvailable.setCustomTexture(CoreData::getInstance(). + getOnServerTexture()); + buttonAvailable.setText(" " + lang.getString("ModAvailable")); + + buttonOnlyLocal.registerGraphicComponent(containerName, + "buttonOnlyLocal"); + buttonOnlyLocal.init(505, legendButtonY, 240); + buttonOnlyLocal.setUseCustomTexture(true); + buttonOnlyLocal.setCustomTexture(CoreData::getInstance(). + getNotOnServerTexture()); + buttonOnlyLocal.setText(" " + lang.getString("ModOnlyLocal")); + + buttonConflict.registerGraphicComponent(containerName, + "buttonConflict"); + buttonConflict.init(755, legendButtonY, 240); + buttonConflict.setUseCustomTexture(true); + buttonConflict.setCustomTexture(CoreData::getInstance(). + getOnServerDifferentTexture()); + buttonConflict.setText(" " + lang.getString("ModHasConflict")); + + + buttonInstallTech.registerGraphicComponent(containerName, + "buttonInstallTech"); + buttonInstallTech.init(techInfoXPos + 45, buttonLineUpY, 125); + buttonInstallTech.setText(lang.getString("Install")); + buttonRemoveTech.registerGraphicComponent(containerName, + "buttonRemoveTech"); + buttonRemoveTech.init(techInfoXPos + 45, buttonLineDownY, 125); + buttonRemoveTech.setText(lang.getString("Remove")); + + buttonInstallTileset.registerGraphicComponent(containerName, + "buttonInstallTileset"); + buttonInstallTileset.init(tilesetInfoXPos + 30, buttonLineUpY, 125); + buttonInstallTileset.setText(lang.getString("Install")); + buttonRemoveTileset.registerGraphicComponent(containerName, + "buttonRemoveTileset"); + buttonRemoveTileset.init(tilesetInfoXPos + 30, buttonLineDownY, 125); + buttonRemoveTileset.setText(lang.getString("Remove")); + + buttonInstallMap.registerGraphicComponent(containerName, + "buttonInstallMap"); + buttonInstallMap.init(mapInfoXPos + 50, buttonLineUpY, 125); + buttonInstallMap.setText(lang.getString("Install")); + buttonRemoveMap.registerGraphicComponent(containerName, + "buttonRemoveMap"); + buttonRemoveMap.init(mapInfoXPos + 50, buttonLineDownY, 125); + buttonRemoveMap.setText(lang.getString("Remove")); + + buttonInstallScenario.registerGraphicComponent(containerName, + "buttonInstallScenario"); + buttonInstallScenario.init(scenarioInfoXPos + 45, buttonLineUpY, 125); + buttonInstallScenario.setText(lang.getString("Install")); + buttonRemoveScenario.registerGraphicComponent(containerName, + "buttonRemoveScenario"); + buttonRemoveScenario.init(scenarioInfoXPos + 45, buttonLineDownY, 125); + buttonRemoveScenario.setText(lang.getString("Remove")); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + keyTilesetScrollBar.init(tilesetInfoXPos + keyButtonsWidthTil, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyTilesetScrollBar.setLength(listBoxLength); + keyTilesetScrollBar.setElementCount(0); + keyTilesetScrollBar.setVisibleSize(keyButtonsToRender); + keyTilesetScrollBar.setVisibleStart(0); + + keyTechScrollBar.init(techInfoXPos + keyButtonsWidthTech + labelWidth + + 20, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyTechScrollBar.setLength(listBoxLength); + keyTechScrollBar.setElementCount(0); + keyTechScrollBar.setVisibleSize(keyButtonsToRender); + keyTechScrollBar.setVisibleStart(0); + + keyMapScrollBar.init(mapInfoXPos + keyButtonsWidthMap + labelWidth + + 20, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyMapScrollBar.setLength(listBoxLength); + keyMapScrollBar.setElementCount(0); + keyMapScrollBar.setVisibleSize(keyButtonsToRender); + keyMapScrollBar.setVisibleStart(0); + + keyScenarioScrollBar.init(scenarioInfoXPos + keyButtonsWidthScen, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyScenarioScrollBar.setLength(listBoxLength); + keyScenarioScrollBar.setElementCount(0); + keyScenarioScrollBar.setVisibleSize(keyButtonsToRender); + keyScenarioScrollBar.setVisibleStart(0); + + GraphicComponent::applyAllCustomProperties(containerName); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + // Start http meta data thread + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + modHttpServerThread = new SimpleTaskThread(this, 0, 200); + modHttpServerThread->setUniqueID(mutexOwnerId); + modHttpServerThread->start(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + // Setup File Transfer thread + findDirs(config.getPathListForType(ptTilesets), tilesetFiles); + findDirs(config.getPathListForType(ptTechs), techTreeFiles); + + vector < string > mapPathList = config.getPathListForType(ptMaps); + std::pair < string, string > mapsPath; + if (mapPathList.empty() == false) { + mapsPath.first = mapPathList[0]; + } + if (mapPathList.size() > 1) { + mapsPath.second = mapPathList[1]; + } + std::pair < string, string > tilesetsPath; + vector < string > tilesetsList = + Config::getInstance().getPathListForType(ptTilesets); + if (tilesetsList.empty() == false) { + tilesetsPath.first = tilesetsList[0]; + if (tilesetsList.size() > 1) { + tilesetsPath.second = tilesetsList[1]; + } + } + + std::pair < string, string > techtreesPath; + vector < string > techtreesList = + Config::getInstance().getPathListForType(ptTechs); + if (techtreesList.empty() == false) { + techtreesPath.first = techtreesList[0]; + if (techtreesList.size() > 1) { + techtreesPath.second = techtreesList[1]; + } + } + + std::pair < string, string > scenariosPath; + vector < string > scenariosList = + Config::getInstance().getPathListForType(ptScenarios); + if (scenariosList.empty() == false) { + scenariosPath.first = scenariosList[0]; + if (scenariosList.size() > 1) { + scenariosPath.second = scenariosList[1]; + } + } + + string fileArchiveExtension = + config.getString("FileArchiveExtension", ""); + string fileArchiveExtractCommand = + config.getString("FileArchiveExtractCommand", ""); + string fileArchiveExtractCommandParameters = + config.getString("FileArchiveExtractCommandParameters", ""); + int32 fileArchiveExtractCommandSuccessResult = + config.getInt("FileArchiveExtractCommandSuccessResult", "0"); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + console.setOnlyChatMessagesInStoredLines(false); + + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); + + ftpClientThread = new FTPClientThread(-1, "", + mapsPath, tilesetsPath, + techtreesPath, scenariosPath, + this, fileArchiveExtension, + fileArchiveExtractCommand, + fileArchiveExtractCommandParameters, + fileArchiveExtractCommandSuccessResult, + tempFilePath); + ftpClientThread->start(); + + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void MenuStateMods::reloadUI() { + Lang & lang = Lang::getInstance(); + + console.resetFonts(); + keyTechScrollBarTitle1.setText(lang.getString("TechTitle1")); + keyTechScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyTechScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + keyTechScrollBarTitle2.setText(lang.getString("TechTitle2")); + keyTechScrollBarTitle2.setFont(CoreData::getInstance(). + getMenuFontNormal()); + keyTechScrollBarTitle2.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + keyMapScrollBarTitle1.setText(lang.getString("MapTitle1")); + keyMapScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyMapScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + keyMapScrollBarTitle2.setText(lang.getString("MapTitle2")); + keyMapScrollBarTitle2.setFont(CoreData::getInstance(). + getMenuFontNormal()); + keyMapScrollBarTitle2.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + keyTilesetScrollBarTitle1.setText(lang.getString("TilesetTitle1")); + keyTilesetScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyTilesetScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + pleaseWaitLabel.setText(""); + pleaseWaitLabel.setFont(CoreData::getInstance().getMenuFontBig()); + pleaseWaitLabel.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + keyScenarioScrollBarTitle1.setText(lang.getString("ScenarioTitle1")); + keyScenarioScrollBarTitle1.setFont(CoreData::getInstance(). + getMenuFontBig()); + keyScenarioScrollBarTitle1.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + mainMessageBox.init(lang.getString("Yes"), lang.getString("No"), + 450); + + modDescrLabel.setText("description is empty"); + + buttonReturn.setText(lang.getString("Return")); + + buttonInstalled.setText(lang.getString("ModInstalled")); + + buttonAvailable.setText(lang.getString("ModAvailable")); + + buttonOnlyLocal.setText(lang.getString("ModOnlyLocal")); + + buttonConflict.setText(lang.getString("ModHasConflict")); + + buttonInstallTech.setText(lang.getString("Install")); + buttonRemoveTech.setText(lang.getString("Remove")); + + buttonInstallTileset.setText(lang.getString("Install")); + buttonRemoveTileset.setText(lang.getString("Remove")); + + buttonInstallMap.setText(lang.getString("Install")); + buttonRemoveMap.setText(lang.getString("Remove")); + + buttonInstallScenario.setText(lang.getString("Install")); + buttonRemoveScenario.setText(lang.getString("Remove")); + + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } + + void MenuStateMods::simpleTask(BaseThread * callingThread, + void *userdata) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutexThreadOwner(callingThread-> + getMutexThreadOwnerValid(), + mutexOwnerId); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + callingThread->getMutexThreadOwnerValid()->setOwnerId(mutexOwnerId); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + Config & config = Config::getInstance(); + string fileArchiveExtractCommand = + config.getString("FileArchiveExtractCommand", ""); + int expectedResult = + config.getInt("FileArchiveExtractCommandSuccessResult", "0"); + bool findArchive = + executeShellCommand(fileArchiveExtractCommand, expectedResult); + if (findArchive == false) { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModRequires7z"), + lang.getString("Notice"), true); + } + + std::string techsMetaData = ""; + std::string tilesetsMetaData = ""; + std::string mapsMetaData = ""; + std::string scenariosMetaData = ""; + + modMenuState = mmst_Loading; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (config.getString("Masterserver", "") != "") { + string baseURL = config.getString("Masterserver"); + if (baseURL != "") { + endPathWithSlash(baseURL, false); + } + + string phpVersionParam = + config.getString("phpVersionParam", "?version=0.1"); + string playerUUID = + "&uuid=" + + SystemFlags::escapeURL(Config::getInstance(). + getString("PlayerId", "")); + string gameVersion = + "&glestVersion=" + SystemFlags::escapeURL(glestVersionString); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", + __FILE__, __FUNCTION__, __LINE__, baseURL.c_str()); + + CURL *handle = SystemFlags::initHTTP(); + CURLcode curlResult = CURLE_OK; + techsMetaData = + SystemFlags::getHTTP(baseURL + "showTechsForGlest.php" + + phpVersionParam + gameVersion + playerUUID, + handle, -1, &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("techsMetaData [%s] curlResult = %d\n", + techsMetaData.c_str(), curlResult); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#1 ") + szBuf, true); + } + + if (curlResult == CURLE_OK || + (curlResult != CURLE_COULDNT_RESOLVE_HOST && + curlResult != CURLE_COULDNT_CONNECT)) { + + tilesetsMetaData = + SystemFlags::getHTTP(baseURL + "showTilesetsForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("tilesetsMetaData [%s]\n", tilesetsMetaData.c_str()); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#2 ") + szBuf, true); + } + + mapsMetaData = + SystemFlags::getHTTP(baseURL + "showMapsForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("mapsMetaData [%s]\n", mapsMetaData.c_str()); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#3 ") + szBuf, true); + } + + scenariosMetaData = + SystemFlags::getHTTP(baseURL + "showScenariosForGlest.php" + + phpVersionParam + gameVersion, handle, -1, + &curlResult); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("scenariosMetaData [%s]\n", scenariosMetaData.c_str()); + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModErrorGettingServerData").c_str(), + curlError.c_str()); + console.addLine(string("#4 ") + szBuf, true); + } + } + SystemFlags::cleanupHTTP(&handle); + } else { + console.addLine(lang.getString("MasterServerMissing"), true); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + tilesetListRemote.clear(); + Tokenize(tilesetsMetaData, tilesetListRemote, "\n"); + + modMenuState = mmst_CalculatingCRC; + + getTilesetsLocalList(); + for (unsigned int i = 0; i < tilesetListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string result = refreshTilesetModInfo(tilesetListRemote[i]); + if (result != "") { + ModInfo modinfo; + modinfo = tilesetCacheList[result]; + GraphicButton *button = new GraphicButton(); + button->init(tilesetInfoXPos, keyButtonsYBase, keyButtonsWidthTil, + keyButtonsHeight); + button->setText(modinfo.name); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyTilesetButtons.push_back(button); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + for (unsigned int i = 0; i < tilesetFilesUserData.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string tilesetName = tilesetFilesUserData[i]; + bool alreadyHasTileset = + (tilesetCacheList.find(tilesetName) != tilesetCacheList.end()); + if (alreadyHasTileset == false) { + GraphicButton *button = new GraphicButton(); + button->init(tilesetInfoXPos, keyButtonsYBase, keyButtonsWidthTil, + keyButtonsHeight); + button->setText(tilesetName); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyTilesetButtons.push_back(button); + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + techListRemote.clear(); + Tokenize(techsMetaData, techListRemote, "\n"); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + getTechsLocalList(); + for (unsigned int i = 0; i < techListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string result = refreshTechModInfo(techListRemote[i]); + if (result != "") { + ModInfo modinfo; + modinfo = techCacheList[result]; + + GraphicButton *button = new GraphicButton(); + button->init(techInfoXPos, keyButtonsYBase, keyButtonsWidthTech, + keyButtonsHeight); + button->setText(modinfo.name); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + + keyTechButtons.push_back(button); + GraphicLabel *label = new GraphicLabel(); + label->init(techInfoXPos + keyButtonsWidthTech + 10, + keyButtonsYBase, labelWidth, 20); + label->setText(modinfo.count); + labelsTech.push_back(label); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + for (unsigned int i = 0; i < techTreeFilesUserData.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string techName = techTreeFilesUserData[i]; + bool alreadyHasTech = + (techCacheList.find(techName) != techCacheList.end()); + if (alreadyHasTech == false) { + vector < string > techPaths = config.getPathListForType(ptTechs); + string & techPath = techPaths[1]; + endPathWithSlash(techPath); + vector < string > factions; + findAll(techPath + techName + "/factions/*.", factions, false, + false); + + GraphicButton *button = new GraphicButton(); + button->init(techInfoXPos, keyButtonsYBase, keyButtonsWidthTech, + keyButtonsHeight); + button->setText(techName); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyTechButtons.push_back(button); + + int techFactionCount = (int) factions.size(); + GraphicLabel *label = new GraphicLabel(); + label->init(techInfoXPos + keyButtonsWidthTech + 10, + keyButtonsYBase, labelWidth, 20); + label->setText(intToStr(techFactionCount)); + labelsTech.push_back(label); + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + mapListRemote.clear(); + Tokenize(mapsMetaData, mapListRemote, "\n"); + + getMapsLocalList(); + for (unsigned int i = 0; i < mapListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string result = refreshMapModInfo(mapListRemote[i]); + if (result != "") { + ModInfo modinfo; + modinfo = mapCacheList[result]; + + GraphicButton *button = new GraphicButton(); + button->init(mapInfoXPos, keyButtonsYBase, keyButtonsWidthMap, + keyButtonsHeight); + button->setText(modinfo.name); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyMapButtons.push_back(button); + + GraphicLabel *label = new GraphicLabel(); + label->init(mapInfoXPos + keyButtonsWidthMap + 10, keyButtonsYBase, + labelWidth, 20); + label->setText(modinfo.count); + labelsMap.push_back(label); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + for (unsigned int i = 0; i < mapFilesUserData.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string mapName = mapFilesUserData[i]; + bool alreadyHasMap = + (mapCacheList.find(mapName) != mapCacheList.end()); + if (alreadyHasMap == false) { + vector < string > mapPaths = config.getPathListForType(ptMaps); + string & mapPath = mapPaths[1]; + endPathWithSlash(mapPath); + mapPath += mapName; + MapInfo mapInfo = loadMapInfo(mapPath); + + GraphicButton *button = new GraphicButton(); + button->init(mapInfoXPos, keyButtonsYBase, keyButtonsWidthMap, + keyButtonsHeight); + button->setText(mapName); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyMapButtons.push_back(button); + + int mapPlayerCount = mapInfo.players; + GraphicLabel *label = new GraphicLabel(); + label->init(mapInfoXPos + keyButtonsWidthMap + 10, keyButtonsYBase, + labelWidth, 20); + label->setText(intToStr(mapPlayerCount)); + labelsMap.push_back(label); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + + scenarioListRemote.clear(); + Tokenize(scenariosMetaData, scenarioListRemote, "\n"); + + getScenariosLocalList(); + for (unsigned int i = 0; i < scenarioListRemote.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string result = refreshScenarioModInfo(scenarioListRemote[i]); + if (result != "") { + ModInfo modinfo; + modinfo = scenarioCacheList[result]; + GraphicButton *button = new GraphicButton(); + button->init(scenarioInfoXPos, keyButtonsYBase, + keyButtonsWidthScen, keyButtonsHeight); + button->setText(modinfo.name); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyScenarioButtons.push_back(button); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + for (unsigned int i = 0; i < scenarioFilesUserData.size(); i++) { + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + string scenarioName = scenarioFilesUserData[i]; + bool alreadyHasScenario = + (scenarioCacheList.find(scenarioName) != scenarioCacheList.end()); + if (alreadyHasScenario == false) { + vector < string > scenarioPaths = + config.getPathListForType(ptScenarios); + string & scenarioPath = scenarioPaths[1]; + endPathWithSlash(scenarioPath); + scenarioPath += scenarioName; + + GraphicButton *button = new GraphicButton(); + button->init(scenarioInfoXPos, keyButtonsYBase, + keyButtonsWidthScen, keyButtonsHeight); + button->setText(scenarioName); + button->setUseCustomTexture(true); + button->setCustomTexture(CoreData::getInstance(). + getCustomTexture()); + keyScenarioButtons.push_back(button); + } + } + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + keyTilesetScrollBar.init(tilesetInfoXPos + keyButtonsWidthTil, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyTilesetScrollBar.setLength(listBoxLength); + keyTilesetScrollBar.setElementCount((int) keyTilesetButtons.size()); + keyTilesetScrollBar.setVisibleSize(keyButtonsToRender); + keyTilesetScrollBar.setVisibleStart(0); + + keyTechScrollBar.init(techInfoXPos + keyButtonsWidthTech + labelWidth + + 20, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyTechScrollBar.setLength(listBoxLength); + keyTechScrollBar.setElementCount((int) keyTechButtons.size()); + keyTechScrollBar.setVisibleSize(keyButtonsToRender); + keyTechScrollBar.setVisibleStart(0); + + keyMapScrollBar.init(mapInfoXPos + keyButtonsWidthMap + labelWidth + + 20, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyMapScrollBar.setLength(listBoxLength); + keyMapScrollBar.setElementCount((int) keyMapButtons.size()); + keyMapScrollBar.setVisibleSize(keyButtonsToRender); + keyMapScrollBar.setVisibleStart(0); + + keyScenarioScrollBar.init(scenarioInfoXPos + keyButtonsWidthScen, + scrollListsYPos - listBoxLength + + keyButtonsLineHeight, false, 200, 20); + keyScenarioScrollBar.setLength(listBoxLength); + keyScenarioScrollBar.setElementCount((int) keyScenarioButtons.size()); + keyScenarioScrollBar.setVisibleSize(keyButtonsToRender); + keyScenarioScrollBar.setVisibleStart(0); + + modMenuState = mmst_None; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (modHttpServerThread != NULL) { + modHttpServerThread->signalQuit(); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + MapInfo MenuStateMods::loadMapInfo(string file) { + try { + Lang & lang = Lang::getInstance(); + // Not painting properly so this is on hold + MapPreview::loadMapInfo(file, &mapInfo, + lang.getString("MaxPlayers"), + lang.getString("Size"), true); + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s] loading map [%s]\n", + __FILE__, __FUNCTION__, __LINE__, e.what(), + file.c_str()); + throw megaglest_runtime_error("Error loading map file: [" + file + + "] msg: " + e.what()); + } + + return mapInfo; + } + + void MenuStateMods::getTechsLocalList() { + Config & config = Config::getInstance(); + vector < string > results; + findDirs(config.getPathListForType(ptTechs), results); + techTreeFiles = results; + + techTreeFilesUserData.clear(); + if (config.getPathListForType(ptTechs).size() > 1) { + string path = config.getPathListForType(ptTechs)[1]; + endPathWithSlash(path); + findDirs(path, techTreeFilesUserData, false, false); + } + } + + string MenuStateMods::refreshTechModInfo(string techInfo) { + std::vector < std::string > techInfoList; + Tokenize(techInfo, techInfoList, "|"); + if (techInfoList.size() >= 6) { + Config & config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = techInfoList[0]; + modinfo.count = techInfoList[1]; + modinfo.crc = techInfoList[2]; + modinfo.description = techInfoList[3]; + modinfo.url = techInfoList[4]; + modinfo.imageUrl = techInfoList[5]; + modinfo.type = mt_Techtree; + + string itemPath = + config.getPathListForType(ptTechs, + "")[1] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + uint32 crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", NULL, + forceRefresh); + if (crc == 0) { + itemPath = + config.getPathListForType(ptTechs, + "")[0] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, forceRefresh); + } + } + modinfo.localCRC = uIntToStr(crc); + //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + modinfo.localCRC = ""; + } + techCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + void MenuStateMods::refreshTechs() { + getTechsLocalList(); + for (int i = 0; i < (int) techListRemote.size(); i++) { + refreshTechModInfo(techListRemote[i]); + } + } + + void MenuStateMods::getTilesetsLocalList() { + Config & config = Config::getInstance(); + vector < string > results; + findDirs(config.getPathListForType(ptTilesets), results); + tilesetFiles = results; + + tilesetFilesUserData.clear(); + if (config.getPathListForType(ptTilesets).size() > 1) { + string path = config.getPathListForType(ptTilesets)[1]; + endPathWithSlash(path); + findDirs(path, tilesetFilesUserData, false, false); + } + } + + string MenuStateMods::refreshTilesetModInfo(string tilesetInfo) { + std::vector < std::string > tilesetInfoList; + Tokenize(tilesetInfo, tilesetInfoList, "|"); + if (tilesetInfoList.size() >= 5) { + Config & config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = tilesetInfoList[0]; + modinfo.crc = tilesetInfoList[1]; + modinfo.description = tilesetInfoList[2]; + modinfo.url = tilesetInfoList[3]; + modinfo.imageUrl = tilesetInfoList[4]; + modinfo.type = mt_Tileset; + + string itemPath = + config.getPathListForType(ptTilesets, + "")[1] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + uint32 crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", NULL, + forceRefresh); + if (crc == 0) { + itemPath = + config.getPathListForType(ptTilesets, + "")[0] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, forceRefresh); + } + } + modinfo.localCRC = uIntToStr(crc); + //printf("itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + + //printf("#1 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); + } else { + modinfo.localCRC = ""; + + //printf("#2 refreshTilesetModInfo name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str()); + } + + tilesetCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + void MenuStateMods::refreshTilesets() { + getTilesetsLocalList(); + for (int i = 0; i < (int) tilesetListRemote.size(); i++) { + refreshTilesetModInfo(tilesetListRemote[i]); + } + } + + void MenuStateMods::getMapsLocalList() { + + /* + Config &config = Config::getInstance(); + vector results; + set allMaps; + findAll(config.getPathListForType(ptMaps), "*.gbm", results, false, false); + copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); + results.clear(); + findAll(config.getPathListForType(ptMaps), "*.mgm", results, false, false); + copy(results.begin(), results.end(), std::inserter(allMaps, allMaps.begin())); + results.clear(); + + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; + + mapFilesUserData.clear(); + if(config.getPathListForType(ptMaps).size() > 1) { + string path = config.getPathListForType(ptMaps)[1]; + endPathWithSlash(path); + + vector results2; + set allMaps2; + findAll(path + "*.gbm", results2, false, false); + copy(results2.begin(), results2.end(), std::inserter(allMaps2, allMaps2.begin())); + + results2.clear(); + findAll(path + "*.mgm", results2, false, false); + copy(results2.begin(), results2.end(), std::inserter(allMaps2, allMaps2.begin())); + + results2.clear(); + copy(allMaps2.begin(), allMaps2.end(), std::back_inserter(results2)); + mapFilesUserData = results2; + //printf("\n\nMap path [%s] mapFilesUserData.size() = %d\n\n\n",path.c_str(),mapFilesUserData.size()); + } + */ + + Config & config = Config::getInstance(); + string scenarioDir = ""; + vector < string > pathList = + config.getPathListForType(ptMaps, scenarioDir); + vector < string > invalidMapList; + vector < string > allMaps = + MapPreview::findAllValidMaps(pathList, scenarioDir, false, false, + &invalidMapList); + if (allMaps.empty()) { + throw megaglest_runtime_error("No maps were found!"); + } + vector < string > results; + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; + + mapFilesUserData = + MapPreview::findAllValidMaps(pathList, scenarioDir, true, false, + &invalidMapList); + } + + string MenuStateMods::refreshMapModInfo(string mapInfo) { + std::vector < std::string > mapInfoList; + Tokenize(mapInfo, mapInfoList, "|"); + if (mapInfoList.size() >= 6) { + //Config &config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = mapInfoList[0]; + modinfo.count = mapInfoList[1]; + modinfo.crc = mapInfoList[2]; + modinfo.description = mapInfoList[3]; + modinfo.url = mapInfoList[4]; + modinfo.imageUrl = mapInfoList[5]; + modinfo.type = mt_Map; + modinfo.localCRC = getMapCRC(modinfo.name); + mapCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + string MenuStateMods::getMapCRC(string mapName) { + Config & config = Config::getInstance(); + vector < string > mappaths = config.getPathListForType(ptMaps, ""); + string result = ""; + if (mappaths.empty() == false) { + Checksum checksum; + string itemPath = mappaths[1] + "/" + mapName; + if (fileExists(itemPath)) { + checksum.addFile(itemPath); + uint32 crc = checksum.getSum(); + result = uIntToStr(crc); + //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + itemPath = mappaths[0] + "/" + mapName; + if (fileExists(itemPath)) { + checksum.addFile(itemPath); + uint32 crc = checksum.getSum(); + result = uIntToStr(crc); + //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + result = ""; + } + } + } else { + result = ""; + } + return result; + } + + void MenuStateMods::refreshMaps() { + getMapsLocalList(); + for (int i = 0; i < (int) mapListRemote.size(); i++) { + refreshMapModInfo(mapListRemote[i]); + } + } + + void MenuStateMods::getScenariosLocalList() { + Config & config = Config::getInstance(); + vector < string > results; + findDirs(config.getPathListForType(ptScenarios), results); + scenarioFiles = results; + + scenarioFilesUserData.clear(); + if (config.getPathListForType(ptScenarios).size() > 1) { + string path = config.getPathListForType(ptScenarios)[1]; + endPathWithSlash(path); + findDirs(path, scenarioFilesUserData, false, false); + } + } + + string MenuStateMods::refreshScenarioModInfo(string scenarioInfo) { + std::vector < std::string > scenarioInfoList; + Tokenize(scenarioInfo, scenarioInfoList, "|"); + if (scenarioInfoList.size() >= 5) { + Config & config = Config::getInstance(); + ModInfo modinfo; + modinfo.name = scenarioInfoList[0]; + modinfo.crc = scenarioInfoList[1]; + modinfo.description = scenarioInfoList[2]; + modinfo.url = scenarioInfoList[3]; + modinfo.imageUrl = scenarioInfoList[4]; + modinfo.type = mt_Scenario; + + string itemPath = + config.getPathListForType(ptScenarios, + "")[1] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + uint32 crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", NULL, + forceRefresh); + if (crc == 0) { + itemPath = + config.getPathListForType(ptScenarios, + "")[0] + "/" + modinfo.name + + string("/*"); + if (itemPath.empty() == false) { + forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + crc = + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, forceRefresh); + } + } + modinfo.localCRC = uIntToStr(crc); + //printf(" itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); + } else { + modinfo.localCRC = ""; + } + scenarioCacheList[modinfo.name] = modinfo; + return modinfo.name; + } + return ""; + } + + void MenuStateMods::refreshScenarios() { + getScenariosLocalList(); + for (int i = 0; i < (int) scenarioListRemote.size(); i++) { + refreshScenarioModInfo(scenarioListRemote[i]); + } + } + + + void MenuStateMods::cleanUp() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (modHttpServerThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + modHttpServerThread->signalQuit(); + //modHttpServerThread->setThreadOwnerValid(false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (modHttpServerThread->canShutdown(true) == true + && modHttpServerThread->shutdownAndWait() == true) { + delete modHttpServerThread; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + modHttpServerThread = NULL; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (ftpClientThread != NULL) { + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + //publishToMasterserverThread->cleanup(); + } + ftpClientThread = NULL; + + // ftpClientThread->signalQuit(); + // ftpClientThread->setCallBackObject(NULL); + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + // if( ftpClientThread->shutdownAndWait() == true) { + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + // delete ftpClientThread; + // } + // ftpClientThread = NULL; + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + clearUserButtons(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + cleanupPreviewTexture(); + cleanupMapPreviewTexture(); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + MenuStateMods::~MenuStateMods() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + cleanUp(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + void MenuStateMods::clearUserButtons() { + // Techs + while (!keyTechButtons.empty()) { + delete keyTechButtons.back(); + keyTechButtons.pop_back(); + } + keyTechScrollBar.setElementCount(0); + + while (!labelsTech.empty()) { + delete labelsTech.back(); + labelsTech.pop_back(); + } + + // Tilesets + while (!keyTilesetButtons.empty()) { + delete keyTilesetButtons.back(); + keyTilesetButtons.pop_back(); + } + keyTilesetScrollBar.setElementCount(0); + + // Maps + while (!keyMapButtons.empty()) { + delete keyMapButtons.back(); + keyMapButtons.pop_back(); + } + while (!labelsMap.empty()) { + delete labelsMap.back(); + labelsMap.pop_back(); + } + keyMapScrollBar.setElementCount(0); + + // Scenarios + while (!keyScenarioButtons.empty()) { + delete keyScenarioButtons.back(); + keyScenarioButtons.pop_back(); + } + keyScenarioScrollBar.setElementCount(0); + } + + void MenuStateMods::mouseClick(int x, int y, MouseButton mouseButton) { + + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + Lang & lang = Lang::getInstance(); + + if (buttonReturn.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + soundRenderer.playFx(coreData.getClickSoundA()); + + if (fileFTPProgressList.empty() == false) { + mainMessageBoxState = ftpmsg_Quit; + mainMessageBox.init(lang.getString("Yes"), lang.getString("No"), + 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadInProgressCancelQuestion"). + c_str(), fileFTPProgressList.size()); + showMessageBox(szBuf, lang.getString("Question"), true); + } else { + cleanUp(); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } + } else if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMessageBox.setEnabled(false); + mainMessageBox.init(lang.getString("Yes"), lang.getString("No"), + 450); + if (button == 0) { + if (mainMessageBoxState == ftpmsg_Quit) { + mainMessageBoxState = ftpmsg_None; + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } else if (mainMessageBoxState == ftpmsg_GetMap || + mainMessageBoxState == ftpmsg_ReplaceMap) { + bool getItemAfterRemoval = + (mainMessageBoxState == ftpmsg_ReplaceMap); + mainMessageBoxState = ftpmsg_None; + + Config & config = Config::getInstance(); + vector < string > mapPaths = config.getPathListForType(ptMaps); + if (mapPaths.size() > 1) { + string removeMap = mapPaths[1]; + endPathWithSlash(removeMap); + removeMap += selectedMapName; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Removing Map [%s]\n", removeMap.c_str()); + removeFile(removeMap); + + bool remoteHasMap = + (mapCacheList.find(selectedMapName) != + mapCacheList.end()); + if (remoteHasMap == false) { + for (unsigned int i = 0; i < keyMapButtons.size(); ++i) { + GraphicButton *button = keyMapButtons[i]; + if (button != NULL + && button->getText() == selectedMapName) { + delete button; + keyMapButtons.erase(keyMapButtons.begin() + i); + labelsMap.erase(labelsMap.begin() + i); + keyMapScrollBar.setElementCount((int) keyMapButtons. + size()); + break; + } + } + } + + if (getItemAfterRemoval == false) { + selectedMapName = ""; + } + refreshMaps(); + Checksum::clearFileCache(); + + if (getItemAfterRemoval == true) { + string mapName = selectedMapName; + string mapURL = mapCacheList[mapName].url; + if (ftpClientThread != NULL) + ftpClientThread->addMapToRequests(mapName, mapURL); + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + fileFTPProgressList[mapName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallMap.setEnabled(false); + } + } + } else if (mainMessageBoxState == ftpmsg_GetTileset || + mainMessageBoxState == ftpmsg_ReplaceTileset) { + bool getItemAfterRemoval = + (mainMessageBoxState == ftpmsg_ReplaceTileset); + mainMessageBoxState = ftpmsg_None; + + Config & config = Config::getInstance(); + vector < string > tilesetPaths = + config.getPathListForType(ptTilesets); + if (tilesetPaths.size() > 1) { + string removeTileset = tilesetPaths[1]; + endPathWithSlash(removeTileset); + removeTileset += selectedTilesetName; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Removing Tileset [%s]\n", removeTileset.c_str()); + removeFolder(removeTileset); + + bool remoteHasTileset = + (tilesetCacheList.find(selectedTilesetName) != + tilesetCacheList.end()); + if (remoteHasTileset == false) { + //printf("\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ remote DOES NOT have removed tileset [%s]\n\n\n",selectedTilesetName.c_str()); + + for (unsigned int i = 0; i < keyTilesetButtons.size(); ++i) { + GraphicButton *button = keyTilesetButtons[i]; + if (button != NULL + && button->getText() == selectedTilesetName) { + delete button; + keyTilesetButtons.erase(keyTilesetButtons.begin() + + i); + keyTilesetScrollBar. + setElementCount((int) keyTilesetButtons.size()); + break; + } + } + } + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + Checksum::clearFileCache(); + vector < string > paths = + Config::getInstance().getPathListForType(ptTilesets); + string pathSearchString = + string("/") + selectedTilesetName + string("/*"); + const string filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + safeMutexFTPProgress.ReleaseLock(); + + if (getItemAfterRemoval == false) { + selectedTilesetName = ""; + } + refreshTilesets(); + } + + if (getItemAfterRemoval == true) { + string tilesetName = selectedTilesetName; + string tilesetURL = tilesetCacheList[tilesetName].url; + if (ftpClientThread != NULL) + ftpClientThread->addTilesetToRequests(tilesetName, + tilesetURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + fileFTPProgressList[tilesetName] = + pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallTileset.setEnabled(false); + } + } else if (mainMessageBoxState == ftpmsg_GetTechtree || + mainMessageBoxState == ftpmsg_ReplaceTechtree) { + bool getItemAfterRemoval = + (mainMessageBoxState == ftpmsg_ReplaceTechtree); + mainMessageBoxState = ftpmsg_None; + + Config & config = Config::getInstance(); + vector < string > techPaths = + config.getPathListForType(ptTechs); + if (techPaths.size() > 1) { + string removeTech = techPaths[1]; + endPathWithSlash(removeTech); + removeTech += selectedTechName; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Removing Techtree [%s]\n", removeTech.c_str()); + removeFolder(removeTech); + + bool remoteHasTech = + (techCacheList.find(selectedTechName) != + techCacheList.end()); + if (remoteHasTech == false) { + for (unsigned int i = 0; i < keyTechButtons.size(); ++i) { + GraphicButton *button = keyTechButtons[i]; + if (button != NULL + && button->getText() == selectedTechName) { + delete button; + keyTechButtons.erase(keyTechButtons.begin() + i); + labelsTech.erase(labelsTech.begin() + i); + keyTechScrollBar.setElementCount((int) keyTechButtons. + size()); + break; + } + } + } + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + // Clear the CRC file Cache + Checksum::clearFileCache(); + vector < string > paths = + Config::getInstance().getPathListForType(ptTechs); + string pathSearchString = + string("/") + selectedTechName + string("/*"); + const string filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + safeMutexFTPProgress.ReleaseLock(); + + if (getItemAfterRemoval == false) { + selectedTechName = ""; + } + refreshTechs(); + } + + if (getItemAfterRemoval == true) { + string techName = selectedTechName; + string techURL = techCacheList[techName].url; + if (ftpClientThread != NULL) + ftpClientThread->addTechtreeToRequests(techName, techURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + fileFTPProgressList[techName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallTech.setEnabled(false); + } + } else if (mainMessageBoxState == ftpmsg_GetScenario || + mainMessageBoxState == ftpmsg_ReplaceScenario) { + bool getItemAfterRemoval = + (mainMessageBoxState == ftpmsg_ReplaceScenario); + mainMessageBoxState = ftpmsg_None; + + Config & config = Config::getInstance(); + vector < string > scenarioPaths = + config.getPathListForType(ptScenarios); + if (scenarioPaths.size() > 1) { + string removeScenario = scenarioPaths[1]; + endPathWithSlash(removeScenario); + removeScenario += selectedScenarioName; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Removing Scenario [%s]\n", + removeScenario.c_str()); + removeFolder(removeScenario); + + bool remoteHasScenario = + (scenarioCacheList.find(selectedScenarioName) != + scenarioCacheList.end()); + if (remoteHasScenario == false) { + for (unsigned int i = 0; i < keyScenarioButtons.size(); + ++i) { + GraphicButton *button = keyScenarioButtons[i]; + if (button != NULL + && button->getText() == selectedScenarioName) { + delete button; + keyScenarioButtons.erase(keyScenarioButtons.begin() + + i); + keyScenarioScrollBar. + setElementCount((int) keyScenarioButtons.size()); + break; + } + } + } + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + Checksum::clearFileCache(); + vector < string > paths = + Config::getInstance().getPathListForType(ptScenarios); + string pathSearchString = + string("/") + selectedScenarioName + string("/*"); + const string filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + safeMutexFTPProgress.ReleaseLock(); + + if (getItemAfterRemoval == false) { + selectedScenarioName = ""; + } + refreshScenarios(); + } + + if (getItemAfterRemoval == true) { + string scenarioName = selectedScenarioName; + string scenarioURL = scenarioCacheList[scenarioName].url; + if (ftpClientThread != NULL) + ftpClientThread->addScenarioToRequests(scenarioName, + scenarioURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + fileFTPProgressList[scenarioName] = + pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallScenario.setEnabled(false); + } + } + } + } + } else if (keyTechScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (keyTilesetScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (keyMapScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (keyScenarioScrollBar.mouseClick(x, y)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } else if (buttonInstallTech.mouseClick(x, y) + && buttonInstallTech.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedTechName != "") { + bool alreadyHasTech = + (std:: + find(techTreeFiles.begin(), techTreeFiles.end(), + selectedTechName) != techTreeFiles.end()); + if (alreadyHasTech == true) { + bool remoteHasTech = + (techCacheList.find(selectedTechName) != techCacheList.end()); + if (remoteHasTech == true) { + ModInfo & modInfo = techCacheList[selectedTechName]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, + __FUNCTION__, __LINE__, modInfo.crc.c_str()); + + Config & config = Config::getInstance(); + string itemPath = + config.getPathListForType(ptTechs, + "")[1] + "/" + selectedTechName + + string("/*"); + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + if (strToUInt(modInfo.crc) != 0 + && strToUInt(modInfo.crc) != + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, + forceRefresh)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] local CRC [%u]\n", __FILE__, + __FUNCTION__, __LINE__, + getFolderTreeContentsCheckSumRecursively(itemPath, + ".xml", + NULL)); + + mainMessageBoxState = ftpmsg_ReplaceTechtree; + mainMessageBox.init(lang.getString("Yes"), + lang.getString("No"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModLocalRemoteMismatch").c_str(), + selectedTechName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModTechAlreadyInstalled").c_str(), + selectedTechName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + mapCRCUpdateList[itemPath] = true; + } + } else { + string techName = selectedTechName; + string techURL = techCacheList[techName].url; + if (ftpClientThread != NULL) + ftpClientThread->addTechtreeToRequests(techName, techURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList[techName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallTech.setEnabled(false); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectTechToInstall"), + lang.getString("Notice"), true); + } + } else if (buttonRemoveTech.mouseClick(x, y) + && buttonRemoveTech.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedTechName != "") { + bool alreadyHasTech = + (std:: + find(techTreeFiles.begin(), techTreeFiles.end(), + selectedTechName) != techTreeFiles.end()); + if (alreadyHasTech == true) { + mainMessageBoxState = ftpmsg_GetTechtree; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModRemoveTechConfirm").c_str(), + selectedTechName.c_str()); + showMessageBox(szBuf, lang.getString("Question"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModCannotRemoveTechNotInstalled"). + c_str(), selectedTechName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + + showMessageBox(lang.getString("ModSelectTechToRemove"), + lang.getString("Notice"), true); + } + } + + else if (buttonInstallTileset.mouseClick(x, y) + && buttonInstallTileset.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedTilesetName != "") { + bool alreadyHasTileset = + (std:: + find(tilesetFiles.begin(), tilesetFiles.end(), + selectedTilesetName) != tilesetFiles.end()); + if (alreadyHasTileset == true) { + bool remoteHasTileset = + (tilesetCacheList.find(selectedTilesetName) != + tilesetCacheList.end()); + if (remoteHasTileset) { + ModInfo & modInfo = tilesetCacheList[selectedTilesetName]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, + __FUNCTION__, __LINE__, modInfo.crc.c_str()); + + Config & config = Config::getInstance(); + string itemPath = + config.getPathListForType(ptTilesets, + "")[1] + "/" + + selectedTilesetName + string("/*"); + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + + if (strToUInt(modInfo.crc) != 0 && + strToUInt(modInfo.crc) != + getFolderTreeContentsCheckSumRecursively(itemPath, ".xml", + NULL, + forceRefresh)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] local CRC [%u] [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + getFolderTreeContentsCheckSumRecursively(itemPath, + ".xml", + NULL), + itemPath.c_str()); + + mainMessageBoxState = ftpmsg_ReplaceTileset; + mainMessageBox.init(lang.getString("Yes"), + lang.getString("No"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModLocalRemoteMismatch").c_str(), + selectedTilesetName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModTilesetAlreadyInstalled"). + c_str(), selectedTilesetName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } + } else { + string tilesetName = selectedTilesetName; + string tilesetURL = tilesetCacheList[tilesetName].url; + if (ftpClientThread != NULL) + ftpClientThread->addTilesetToRequests(tilesetName, tilesetURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList[tilesetName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallTileset.setEnabled(false); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectTilesetToInstall"), + lang.getString("Notice"), true); + } + } else if (buttonRemoveTileset.mouseClick(x, y) + && buttonRemoveTileset.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedTilesetName != "") { + bool alreadyHasTileset = + (std:: + find(tilesetFiles.begin(), tilesetFiles.end(), + selectedTilesetName) != tilesetFiles.end()); + if (alreadyHasTileset == true) { + mainMessageBoxState = ftpmsg_GetTileset; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModRemoveTilesetConfirm").c_str(), + selectedTilesetName.c_str()); + showMessageBox(szBuf, lang.getString("Question"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModCannotRemoveTilesetNotInstalled"). + c_str(), selectedTilesetName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectTilesetToRemove"), + lang.getString("Notice"), true); + } + } + + else if (buttonInstallMap.mouseClick(x, y) + && buttonInstallMap.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedMapName != "") { + bool alreadyHasMap = + (std:: + find(mapFiles.begin(), mapFiles.end(), + selectedMapName) != mapFiles.end()); + if (alreadyHasMap == true) { + bool remoteHasMap = + (mapCacheList.find(selectedMapName) != mapCacheList.end()); + if (remoteHasMap) { + ModInfo & modInfo = mapCacheList[selectedMapName]; + if (modInfo.crc != modInfo.localCRC) { + mainMessageBoxState = ftpmsg_ReplaceMap; + mainMessageBox.init(lang.getString("Yes"), + lang.getString("No"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModLocalRemoteMismatch").c_str(), + selectedMapName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModMapAlreadyInstalled").c_str(), + selectedMapName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } + } else { + string mapName = selectedMapName; + string mapURL = mapCacheList[mapName].url; + if (ftpClientThread != NULL) + ftpClientThread->addMapToRequests(mapName, mapURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList[mapName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallMap.setEnabled(false); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectMapToInstall"), + lang.getString("Notice"), true); + } + } else if (buttonRemoveMap.mouseClick(x, y) + && buttonRemoveMap.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedMapName != "") { + bool alreadyHasMap = + (std:: + find(mapFiles.begin(), mapFiles.end(), + selectedMapName) != mapFiles.end()); + if (alreadyHasMap == true) { + mainMessageBoxState = ftpmsg_GetMap; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModRemoveMapConfirm").c_str(), + selectedMapName.c_str()); + showMessageBox(szBuf, lang.getString("Question"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModCannotRemoveMapNotInstalled"). + c_str(), selectedMapName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectMapToRemove"), + lang.getString("Notice"), true); + } + } + + else if (buttonInstallScenario.mouseClick(x, y) + && buttonInstallScenario.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedScenarioName != "") { + bool alreadyHasScenario = + (std:: + find(scenarioFiles.begin(), scenarioFiles.end(), + selectedScenarioName) != scenarioFiles.end()); + if (alreadyHasScenario == true) { + bool remoteHasScenario = + (scenarioCacheList.find(selectedScenarioName) != + scenarioCacheList.end()); + if (remoteHasScenario) { + ModInfo & modInfo = scenarioCacheList[selectedScenarioName]; + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, + __FUNCTION__, __LINE__, modInfo.crc.c_str()); + + Config & config = Config::getInstance(); + string itemPath = + config.getPathListForType(ptScenarios, + "")[1] + "/" + + selectedScenarioName + string("/*"); + bool forceRefresh = + (mapCRCUpdateList.find(itemPath) == mapCRCUpdateList.end()); + + if (strToUInt(modInfo.crc) != 0 && + strToUInt(modInfo.crc) != + getFolderTreeContentsCheckSumRecursively(itemPath, "", + NULL, + forceRefresh)) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] local CRC [%u]\n", __FILE__, + __FUNCTION__, __LINE__, + getFolderTreeContentsCheckSumRecursively(itemPath, + "", + NULL)); + + mainMessageBoxState = ftpmsg_ReplaceScenario; + mainMessageBox.init(lang.getString("Yes"), + lang.getString("No"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModLocalRemoteMismatch").c_str(), + selectedScenarioName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModScenarioAlreadyInstalled"). + c_str(), selectedScenarioName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } + } else { + string scenarioName = selectedScenarioName; + string scenarioURL = scenarioCacheList[scenarioName].url; + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] adding file to download [%s]\n",__FILE__,__FUNCTION__,__LINE__,scenarioURL.c_str()); + if (ftpClientThread != NULL) + ftpClientThread->addScenarioToRequests(scenarioName, + scenarioURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList[scenarioName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + buttonInstallScenario.setEnabled(false); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectScenarioToInstall"), + lang.getString("Notice"), true); + } + } else if (buttonRemoveScenario.mouseClick(x, y) + && buttonRemoveScenario.getEnabled()) { + soundRenderer.playFx(coreData.getClickSoundB()); + if (selectedScenarioName != "") { + bool alreadyHasScenario = + (std:: + find(scenarioFiles.begin(), scenarioFiles.end(), + selectedScenarioName) != scenarioFiles.end()); + if (alreadyHasScenario == true) { + mainMessageBoxState = ftpmsg_GetScenario; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModRemoveScenarioConfirm").c_str(), + selectedScenarioName.c_str()); + showMessageBox(szBuf, lang.getString("Question"), true); + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModCannotRemoveScenarioNotInstalled"). + c_str(), selectedScenarioName.c_str()); + showMessageBox(szBuf, lang.getString("Notice"), true); + } + } else { + mainMessageBoxState = ftpmsg_None; + mainMessageBox.init(lang.getString("Ok"), 450); + showMessageBox(lang.getString("ModSelectScenarioToRemove"), + lang.getString("Notice"), true); + } + } + + else { + if (keyMapScrollBar.getElementCount() != 0) { + for (int i = keyMapScrollBar.getVisibleStart(); + i <= keyMapScrollBar.getVisibleEnd(); ++i) { + if (keyMapButtons[i]->mouseClick(x, y) + && keyMapButtons[i]->getEnabled()) { + string mapName = keyMapButtons[i]->getText(); + selectedTechName = ""; + selectedTilesetName = ""; + selectedMapName = ""; + selectedScenarioName = ""; + if (mapName != "") { + selectedMapName = mapName; + bool remoteHasMap = + (mapCacheList.find(mapName) != mapCacheList.end()); + if (remoteHasMap) { + showRemoteDesription(&mapCacheList[selectedMapName]); + if (mapCacheList[selectedMapName].localCRC != "") { + loadMapPreview(mapName); + } + } else { + showLocalDescription(mapName); + loadMapPreview(mapName); + } + + } + break; + } + } + } + if (keyTechScrollBar.getElementCount() != 0) { + for (int i = keyTechScrollBar.getVisibleStart(); + i <= keyTechScrollBar.getVisibleEnd(); ++i) { + if (keyTechButtons[i]->mouseClick(x, y) + && keyTechButtons[i]->getEnabled()) { + string techName = keyTechButtons[i]->getText(); + selectedTechName = ""; + selectedTilesetName = ""; + selectedMapName = ""; + selectedScenarioName = ""; + if (techName != "") { + selectedTechName = techName; + bool remoteHasTech = + (techCacheList.find(techName) != techCacheList.end()); + if (remoteHasTech) { + showRemoteDesription(&techCacheList[techName]); + } else { + showLocalDescription(techName); + } + } + break; + } + } + } + if (keyTilesetScrollBar.getElementCount() != 0) { + for (int i = keyTilesetScrollBar.getVisibleStart(); + i <= keyTilesetScrollBar.getVisibleEnd(); ++i) { + if (keyTilesetButtons[i]->mouseClick(x, y) + && keyTilesetButtons[i]->getEnabled()) { + string tilesetName = keyTilesetButtons[i]->getText(); + selectedTechName = ""; + selectedTilesetName = ""; + selectedMapName = ""; + selectedScenarioName = ""; + if (tilesetName != "") { + selectedTilesetName = tilesetName; + bool remoteHasTileset = + (tilesetCacheList.find(tilesetName) != + tilesetCacheList.end()); + if (remoteHasTileset) { + showRemoteDesription(&tilesetCacheList[tilesetName]); + } else { + showLocalDescription(tilesetName); + } + } + break; + } + } + } + if (keyScenarioScrollBar.getElementCount() != 0) { + for (int i = keyScenarioScrollBar.getVisibleStart(); + i <= keyScenarioScrollBar.getVisibleEnd(); ++i) { + if (keyScenarioButtons[i]->mouseClick(x, y) + && keyScenarioButtons[i]->getEnabled()) { + string scenarioName = keyScenarioButtons[i]->getText(); + selectedTechName = ""; + selectedTilesetName = ""; + selectedMapName = ""; + selectedScenarioName = ""; + if (scenarioName != "") { + selectedScenarioName = scenarioName; + bool remoteHasScenario = + (scenarioCacheList.find(scenarioName) != + scenarioCacheList.end()); + if (remoteHasScenario) { + showRemoteDesription(&scenarioCacheList[scenarioName]); + } else { + showLocalDescription(scenarioName); + } + + } + break; + } + } + } + + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string MenuStateMods::getPreviewImageFileForMod(const ModInfo * modInfo) { + string fileName = ""; + if (modInfo->imageUrl != "") { + Config & config = Config::getInstance(); + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + string tempPath = userData + "temp/"; + if (isdir(tempPath.c_str()) == false) { + createDirectoryPaths(tempPath); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("### tempPath [%s] isdir = %d\n", tempPath.c_str(), + isdir(tempPath.c_str())); + + if (isdir(tempPath.c_str()) == true) { + fileName = tempPath; + switch (modInfo->type) { + case mt_Map: + fileName += "map_"; + break; + case mt_Tileset: + fileName += "tileset_"; + break; + case mt_Techtree: + fileName += "tech_"; + break; + case mt_Scenario: + fileName += "scenario_"; + break; + } + fileName += extractFileFromDirectoryPath(modInfo->imageUrl); + } + } + return fileName; + } + + + void MenuStateMods::showLocalDescription(string name) { + Lang & lang = Lang::getInstance(); + modInfoSelected = NULL; + cleanupPreviewTexture(); + validMapPreview = false; + cleanupMapPreviewTexture(); + modDescrLabel.setText(lang.getString("ModOnlyLocal") + ":\n'" + name + + "'"); + } + + void MenuStateMods::loadMapPreview(string mapName) { + Config & config = Config::getInstance(); + cleanupMapPreviewTexture(); + vector < string > mapPaths = config.getPathListForType(ptMaps); + string & mapPath = mapPaths[1]; + endPathWithSlash(mapPath); + mapPath += mapName; + MapInfo mapInfo = loadMapInfo(mapPath); + if (enableMapPreview == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + mapPreview.loadFromFile(mapPath.c_str()); + + //printf("Loading map preview MAP\n"); + cleanupMapPreviewTexture(); + validMapPreview = true; + } + } + + + + void MenuStateMods::showRemoteDesription(ModInfo * modInfo) { + //displayModPreviewImage = false; + modInfoSelected = modInfo; + validMapPreview = false; + cleanupMapPreviewTexture(); + + string modText = modInfo->description; + replaceAll(modText, "\\n", "\n"); + modDescrLabel.setText(modText); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("### modInfo->imageUrl [%s]\n", modInfo->imageUrl.c_str()); + + if (modInfo->imageUrl != "") { + cleanupPreviewTexture(); + string tempImage = getPreviewImageFileForMod(modInfo); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("### tempImage [%s] exists [%d]\n", tempImage.c_str(), + fileExists(tempImage)); + + if (tempImage != "" && fileExists(tempImage) == false) { + if (ftpClientThread != NULL) + ftpClientThread->addFileToRequests(tempImage, modInfo->imageUrl); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList[tempImage] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + + } else { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + if (fileFTPProgressList.find(tempImage) == + fileFTPProgressList.end()) { + //displayModPreviewImage = true; + displayModPreviewImage[tempImage] = true; + } + safeMutexFTPProgress.ReleaseLock(); + } + } + } + + void MenuStateMods::mouseUp(int x, int y, const MouseButton mouseButton) { + if (mouseButton == mbLeft) { + keyTechScrollBar.mouseUp(x, y); + keyTilesetScrollBar.mouseUp(x, y); + keyMapScrollBar.mouseUp(x, y); + keyScenarioScrollBar.mouseUp(x, y); + } + } + + void MenuStateMods::mouseMove(int x, int y, const MouseState * ms) { + buttonReturn.mouseMove(x, y); + + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + + buttonInstallTech.mouseMove(x, y); + buttonRemoveTech.mouseMove(x, y); + buttonInstallTileset.mouseMove(x, y); + buttonRemoveTileset.mouseMove(x, y); + buttonInstallMap.mouseMove(x, y); + buttonRemoveMap.mouseMove(x, y); + buttonInstallScenario.mouseMove(x, y); + buttonRemoveScenario.mouseMove(x, y); + + if (ms->get(mbLeft)) { + keyMapScrollBar.mouseDown(x, y); + keyTechScrollBar.mouseDown(x, y); + keyTilesetScrollBar.mouseDown(x, y); + keyScenarioScrollBar.mouseDown(x, y); + } else { + keyMapScrollBar.mouseMove(x, y); + keyTechScrollBar.mouseMove(x, y); + keyTilesetScrollBar.mouseMove(x, y); + keyScenarioScrollBar.mouseMove(x, y); + } + + if (keyMapScrollBar.getElementCount() != 0) { + for (int i = keyMapScrollBar.getVisibleStart(); + i <= keyMapScrollBar.getVisibleEnd(); ++i) { + keyMapButtons[i]->mouseMove(x, y); + } + } + if (keyTechScrollBar.getElementCount() != 0) { + for (int i = keyTechScrollBar.getVisibleStart(); + i <= keyTechScrollBar.getVisibleEnd(); ++i) { + keyTechButtons[i]->mouseMove(x, y); + } + } + if (keyTilesetScrollBar.getElementCount() != 0) { + for (int i = keyTilesetScrollBar.getVisibleStart(); + i <= keyTilesetScrollBar.getVisibleEnd(); ++i) { + keyTilesetButtons[i]->mouseMove(x, y); + } + } + if (keyScenarioScrollBar.getElementCount() != 0) { + for (int i = keyScenarioScrollBar.getVisibleStart(); + i <= keyScenarioScrollBar.getVisibleEnd(); ++i) { + keyScenarioButtons[i]->mouseMove(x, y); + } + } + } + + void MenuStateMods::cleanupPreviewTexture() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n", __FILE__, + __FUNCTION__, __LINE__, modPreviewImage); + + if (modPreviewImage != NULL) { + Renderer::getInstance().endTexture(rsGlobal, modPreviewImage, + false); + } + modPreviewImage = NULL; + } + + void MenuStateMods::cleanupMapPreviewTexture() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + //printf("CLEANUP map preview texture\n"); + if (mapPreviewTexture != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + mapPreviewTexture->end(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + delete mapPreviewTexture; + mapPreviewTexture = NULL; + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + + void MenuStateMods::render() { + try { + Renderer & renderer = Renderer::getInstance(); + + renderer.renderLine(&lineHorizontal); + renderer.renderLine(&lineVertical); + renderer.renderLine(&lineReturn); + renderer.renderLine(&lineVerticalReturn); + renderer.renderButton(&buttonReturn); + + renderer.renderButton(&buttonInstallTech); + renderer.renderButton(&buttonRemoveTech); + renderer.renderButton(&buttonInstallTileset); + renderer.renderButton(&buttonRemoveTileset); + renderer.renderButton(&buttonInstallMap); + renderer.renderButton(&buttonRemoveMap); + renderer.renderButton(&buttonInstallScenario); + renderer.renderButton(&buttonRemoveScenario); + + renderer.renderButton(&buttonInstalled); + renderer.renderButton(&buttonAvailable); + renderer.renderButton(&buttonConflict); + renderer.renderButton(&buttonOnlyLocal); + + renderer.renderLabel(&modDescrLabel); + if (modInfoSelected != NULL) { + string tempImage = getPreviewImageFileForMod(modInfoSelected); + if (displayModPreviewImage.find(tempImage) != + displayModPreviewImage.end() + && displayModPreviewImage[tempImage] == true) { + if (modPreviewImage == NULL) { + string tempImage = getPreviewImageFileForMod(modInfoSelected); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("### Render tempImage [%s] fileExists(tempImage) = %d\n", + tempImage.c_str(), fileExists(tempImage)); + + if (tempImage != "" && fileExists(tempImage) == true) { + cleanupPreviewTexture(); + modPreviewImage = Renderer::findTexture(tempImage); + } + } + if (modPreviewImage != NULL) { + renderer.renderTextureQuad(508, 90, 485, 325, modPreviewImage, + 1.0f); + } + } + } + + // Render Tech List + renderer.renderLabel(&keyTechScrollBarTitle1); + renderer.renderLabel(&keyTechScrollBarTitle2); + if (keyTechScrollBar.getElementCount() != 0) { + for (int i = keyTechScrollBar.getVisibleStart(); + i <= keyTechScrollBar.getVisibleEnd(); ++i) { + bool alreadyHasTech = + (std:: + find(techTreeFiles.begin(), techTreeFiles.end(), + keyTechButtons[i]->getText()) != techTreeFiles.end()); + if (keyTechButtons[i]->getText() == selectedTechName) { + bool lightedOverride = true; + renderer.renderButton(keyTechButtons[i], &WHITE, + &lightedOverride); + } else if (alreadyHasTech == true) { + Vec4f buttonColor = WHITE; + buttonColor.w = 0.75f; + bool remoteHasTech = + (techCacheList.find(keyTechButtons[i]->getText()) != + techCacheList.end()); + if (remoteHasTech) { + ModInfo & modInfo = + techCacheList[keyTechButtons[i]->getText()]; + if (modInfo.crc == modInfo.localCRC) { + keyTechButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerInstalledTexture()); + } else { + //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); + keyTechButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerDifferentTexture()); + } + } else { + keyTechButtons[i]->setCustomTexture(CoreData::getInstance(). + getNotOnServerTexture + ()); + } + renderer.renderButton(keyTechButtons[i], &buttonColor); + } else { + Vec4f fontColor = + Vec4f(200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, + 0.75f); + //Vec4f fontColor=Vec4f(1.0f, 0.0f, 0.0f, 0.75f); + keyTechButtons[i]->setCustomTexture(CoreData::getInstance(). + getOnServerTexture()); + renderer.renderButton(keyTechButtons[i], &fontColor); + } + renderer.renderLabel(labelsTech[i]); + } + } + renderer.renderScrollBar(&keyTechScrollBar); + + // Render Tileset List + renderer.renderLabel(&keyTilesetScrollBarTitle1); + if (keyTilesetScrollBar.getElementCount() != 0) { + for (int i = keyTilesetScrollBar.getVisibleStart(); + i <= keyTilesetScrollBar.getVisibleEnd(); ++i) { + bool alreadyHasTileset = + (std:: + find(tilesetFiles.begin(), tilesetFiles.end(), + keyTilesetButtons[i]->getText()) != + tilesetFiles.end()); + if (keyTilesetButtons[i]->getText() == selectedTilesetName) { + bool lightedOverride = true; + renderer.renderButton(keyTilesetButtons[i], &WHITE, + &lightedOverride); + } else if (alreadyHasTileset == true) { + Vec4f buttonColor = WHITE; + buttonColor.w = 0.75f; + bool remoteHasTileset = + (tilesetCacheList.find(keyTilesetButtons[i]->getText()) != + tilesetCacheList.end()); + if (remoteHasTileset) { + ModInfo & modInfo = + tilesetCacheList[keyTilesetButtons[i]->getText()]; + + if (modInfo.crc == modInfo.localCRC) { + keyTilesetButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerInstalledTexture()); + } else { + //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); + //printf("name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); + + keyTilesetButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerDifferentTexture()); + } + } else { + keyTilesetButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getNotOnServerTexture()); + } + renderer.renderButton(keyTilesetButtons[i], &buttonColor); + } else { + Vec4f fontColor = + Vec4f(200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, + 0.75f); + keyTilesetButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerTexture()); + renderer.renderButton(keyTilesetButtons[i], &fontColor); + } + } + } + renderer.renderScrollBar(&keyTilesetScrollBar); + + // Render Map list + renderer.renderLabel(&keyMapScrollBarTitle1); + renderer.renderLabel(&keyMapScrollBarTitle2); + if (keyMapScrollBar.getElementCount() != 0) { + for (int i = keyMapScrollBar.getVisibleStart(); + i <= keyMapScrollBar.getVisibleEnd(); ++i) { + string mapNameToRender = keyMapButtons[i]->getText(); + bool alreadyHasMap = + (std:: + find(mapFiles.begin(), mapFiles.end(), + mapNameToRender) != mapFiles.end()); + if (keyMapButtons[i]->getText() == selectedMapName) { + bool lightedOverride = true; + renderer.renderButton(keyMapButtons[i], &WHITE, + &lightedOverride); + } else if (alreadyHasMap == true) { + Vec4f buttonColor = WHITE; + buttonColor.w = 0.75f; + bool remoteHasMap = + (mapCacheList.find(keyMapButtons[i]->getText()) != + mapCacheList.end()); + if (remoteHasMap) { + ModInfo & modInfo = + mapCacheList[keyMapButtons[i]->getText()]; + if (modInfo.crc == modInfo.localCRC) { + keyMapButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerInstalledTexture()); + } else { + //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); + keyMapButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerDifferentTexture()); + } + } else { + keyMapButtons[i]->setCustomTexture(CoreData::getInstance(). + getNotOnServerTexture()); + } + renderer.renderButton(keyMapButtons[i], &buttonColor); + } else { + Vec4f fontColor = + Vec4f(200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, + 0.75f); + keyMapButtons[i]->setCustomTexture(CoreData::getInstance(). + getOnServerTexture()); + renderer.renderButton(keyMapButtons[i], &fontColor); + } + renderer.renderLabel(labelsMap[i]); + } + } + renderer.renderScrollBar(&keyMapScrollBar); + + // Render Scenario List + renderer.renderLabel(&keyScenarioScrollBarTitle1); + if (keyScenarioScrollBar.getElementCount() != 0) { + for (int i = keyScenarioScrollBar.getVisibleStart(); + i <= keyScenarioScrollBar.getVisibleEnd(); ++i) { + if (i >= (int) keyScenarioButtons.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "i >= keyScenarioButtons.size(), i = %d keyScenarioButtons.size() = %d", + i, (int) keyScenarioButtons.size()); + throw megaglest_runtime_error(szBuf); + } + bool alreadyHasScenario = + (std:: + find(scenarioFiles.begin(), scenarioFiles.end(), + keyScenarioButtons[i]->getText()) != + scenarioFiles.end()); + + if (keyScenarioButtons[i]->getText() == selectedScenarioName) { + bool lightedOverride = true; + renderer.renderButton(keyScenarioButtons[i], &WHITE, + &lightedOverride); + } else if (alreadyHasScenario == true) { + Vec4f buttonColor = WHITE; + buttonColor.w = 0.75f; + bool remoteHasScenario = + (scenarioCacheList.find(keyScenarioButtons[i]->getText()) != + scenarioCacheList.end()); + if (remoteHasScenario) { + ModInfo & modInfo = + scenarioCacheList[keyScenarioButtons[i]->getText()]; + if (modInfo.crc == modInfo.localCRC) { + keyScenarioButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerInstalledTexture()); + } else { + //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); + keyScenarioButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerDifferentTexture()); + } + } else { + keyScenarioButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getNotOnServerTexture()); + } + renderer.renderButton(keyScenarioButtons[i], &buttonColor); + } else { + Vec4f fontColor = + Vec4f(200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, + 0.75f); + keyScenarioButtons[i]-> + setCustomTexture(CoreData::getInstance(). + getOnServerTexture()); + renderer.renderButton(keyScenarioButtons[i], &fontColor); + } + } + } + renderer.renderScrollBar(&keyScenarioScrollBar); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + if (fileFTPProgressList.empty() == false) { + Lang & lang = Lang::getInstance(); + int xLocation = buttonReturn.getX(); + int yLocation = buttonReturn.getY() - 12; + for (std::map < string, pair < int, string > >::iterator iterMap = + fileFTPProgressList.begin(); + iterMap != fileFTPProgressList.end(); ++iterMap) { + + string progressLabelPrefix = + lang.getString("ModDownloading") + " " + + extractFileFromDirectoryPath(iterMap->first) + " "; + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nRendering file progress with the following prefix [%s]\n",progressLabelPrefix.c_str()); + + if (Renderer::renderText3DEnabled) { + renderer.renderProgressBar3D(iterMap->second.first, + //10, + //yLocation, + xLocation, + yLocation, + CoreData::getInstance(). + getDisplayFontSmall3D(), 185, + progressLabelPrefix, false); + } else { + renderer.renderProgressBar(iterMap->second.first, + //10, + //yLocation, + xLocation, + yLocation, + CoreData::getInstance(). + getDisplayFontSmall(), 185, + progressLabelPrefix, false); + } + + yLocation -= 14; + } + } + safeMutexFTPProgress.ReleaseLock(); + + renderer.renderConsole(&console, consoleNormal, 3); + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } + + if (modMenuState != mmst_None) { + if (oldMenuState != modMenuState) { + Lang & lang = Lang::getInstance(); + if (modMenuState == mmst_Loading) { + pleaseWaitLabel.setText(lang. + getString + ("GettingModlistFromMasterserver")); + } else if (modMenuState == mmst_CalculatingCRC) { + pleaseWaitLabel.setText(lang. + getString + ("PleaseWaitCalculatingCRC")); + } + oldMenuState = modMenuState; + } + float anim = GraphicComponent::getAnim(); + if (anim < 0.5f) { + anim = 1.f - anim; + } + Vec4f colorWithAlpha = Vec4f(ORANGE.x, ORANGE.y, ORANGE.z, anim); + renderer.renderLabel(&pleaseWaitLabel, &colorWithAlpha); + } + if (validMapPreview) { + if (mapPreviewTexture != NULL) { + renderer.renderTextureQuad(5, 185, 150, 150, mapPreviewTexture, + 1.0f); + } + if (enableMapPreview && (mapPreview.hasFileLoaded() == true)) { + + int mouseX = mainMenu->getMouseX(); + int mouseY = mainMenu->getMouseY(); + int mouse2dAnim = mainMenu->getMouse2dAnim(); + + if (mapPreviewTexture == NULL) { + renderer.renderMouse2d(mouseX, mouseY, mouse2dAnim); + renderer.renderMapPreview(&mapPreview, true, 10, 350, + &mapPreviewTexture); + } + } + } + + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + } + + void MenuStateMods::update() { + Chrono chrono; + chrono.start(); + + //Lang &lang= Lang::getInstance(); + + // Tech List + if (keyTechScrollBar.getElementCount() != 0) { + for (int i = keyTechScrollBar.getVisibleStart(); + i <= keyTechScrollBar.getVisibleEnd(); ++i) { + if (i >= (int) keyTechButtons.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "i >= keyTechButtons.size(), i = %d, keyTechButtons.size() = %d", + i, (int) keyTechButtons.size()); + throw megaglest_runtime_error(szBuf); + } + + keyTechButtons[i]->setY(keyButtonsYBase - keyButtonsLineHeight * (i + - + keyTechScrollBar. + getVisibleStart + ())); + labelsTech[i]->setY(keyButtonsYBase - + keyButtonsLineHeight * (i - + keyTechScrollBar. + getVisibleStart())); + } + } + + // Tileset List + if (keyTilesetScrollBar.getElementCount() != 0) { + for (int i = keyTilesetScrollBar.getVisibleStart(); + i <= keyTilesetScrollBar.getVisibleEnd(); ++i) { + if (i >= (int) keyTilesetButtons.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "i >= keyTilesetButtons.size(), i = %d, keyTilesetButtons.size() = %d", + i, (int) keyTilesetButtons.size()); + throw megaglest_runtime_error(szBuf); + } + + int yPos = keyButtonsYBase - keyButtonsLineHeight * + (i - keyTilesetScrollBar.getVisibleStart()); + keyTilesetButtons[i]->setY(yPos); + } + } + + // Map List + if (keyMapScrollBar.getElementCount() != 0) { + for (int i = keyMapScrollBar.getVisibleStart(); + i <= keyMapScrollBar.getVisibleEnd(); ++i) { + if (i >= (int) keyMapButtons.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "i >= keyMapButtons.size(), i = %d, keyMapButtons.size() = %d", + i, (int) keyMapButtons.size()); + throw megaglest_runtime_error(szBuf); + } + + keyMapButtons[i]->setY(keyButtonsYBase - keyButtonsLineHeight * (i + - + keyMapScrollBar. + getVisibleStart + ())); + labelsMap[i]->setY(keyButtonsYBase - + keyButtonsLineHeight * (i - + keyMapScrollBar. + getVisibleStart())); + } + } + + // Scenario List + if (keyScenarioScrollBar.getElementCount() != 0) { + for (int i = keyScenarioScrollBar.getVisibleStart(); + i <= keyScenarioScrollBar.getVisibleEnd(); ++i) { + if (i >= (int) keyScenarioButtons.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "i >= keyScenarioButtons.size(), i = %d, keyScenarioButtons.size() = %d", + i, (int) keyScenarioButtons.size()); + throw megaglest_runtime_error(szBuf); + } + + int yPos = keyButtonsYBase - keyButtonsLineHeight * + (i - keyScenarioScrollBar.getVisibleStart()); + keyScenarioButtons[i]->setY(yPos); + } + } + + console.update(); + } + + void MenuStateMods::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(key == configKeys.getCharKey("ShowFullConsole")) { + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) == + true) { + showFullConsole = true; + } + } + + void MenuStateMods::keyPress(SDL_KeyboardEvent c) { + } + + void MenuStateMods::keyUp(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(key== configKeys.getCharKey("ShowFullConsole")) { + if (isKeyPressed(configKeys.getSDLKey("ShowFullConsole"), key) == + true) { + showFullConsole = false; + } + } + + void MenuStateMods::showMessageBox(const string & text, + const string & header, bool toggle) { + if (toggle == false) { + mainMessageBox.setEnabled(false); + } + + if (mainMessageBox.getEnabled() == false) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void MenuStateMods::FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType type, + pair < FTP_Client_ResultType, + string > result, + void *userdata) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + if (type == ftp_cct_DownloadProgress) { + FTPClientCallbackInterface::FtpProgressStats * stats = + (FTPClientCallbackInterface::FtpProgressStats *) userdata; + if (stats != NULL) { + int fileProgress = 0; + if (stats->download_total > 0) { + fileProgress = + ((stats->download_now / stats->download_total) * 100.0); + } + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + pair < int, string > lastProgress = fileFTPProgressList[itemName]; + fileFTPProgressList[itemName] = + pair < int, string >(fileProgress, stats->currentFilename); + safeMutexFTPProgress.ReleaseLock(); + } + } else if (type == ftp_cct_ExtractProgress) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP extract Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + //printf("Got FTP extract Callback for [%s] result = %d [%s]\n",itemName.c_str(),result.first,result.second.c_str()); + + if (userdata == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("DataMissingExtractDownloadMod").c_str(), + itemName.c_str()); + //printf("%s\n",szBuf); + console.addLine(szBuf, true); + } else { + char *szBuf = (char *) userdata; + //printf("%s\n",szBuf); + console.addLine(szBuf); + } + } else if (type == ftp_cct_File) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(); + + //printf("### downloaded file [%s] result = %d\n",itemName.c_str(),result.first); + + if (result.first == ftp_crt_SUCCESS) { + displayModPreviewImage[itemName] = true; + } + } + + else if (type == ftp_cct_Map) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(); + selectedMapName = ""; + buttonInstallMap.setEnabled(true); + + if (result.first == ftp_crt_SUCCESS) { + refreshMaps(); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadMapSuccess").c_str(), + itemName.c_str()); + console.addLine(szBuf, true); + } else { + curl_version_info_data *curlVersion = + curl_version_info(CURLVERSION_NOW); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadMapFail").c_str(), + itemName.c_str(), curlVersion->version, + result.second.c_str()); + console.addLine(szBuf, true); + } + } else if (type == ftp_cct_Tileset) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(true); + + selectedTilesetName = ""; + buttonInstallTileset.setEnabled(true); + + if (result.first == ftp_crt_SUCCESS) { + refreshTilesets(); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadTilesetSuccess").c_str(), + itemName.c_str()); + console.addLine(szBuf, true); + // END + } else { + curl_version_info_data *curlVersion = + curl_version_info(CURLVERSION_NOW); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadTilesetFail").c_str(), + itemName.c_str(), curlVersion->version, + result.second.c_str()); + console.addLine(szBuf, true); + } + } else if (type == ftp_cct_Techtree) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(true); + + selectedTechName = ""; + buttonInstallTech.setEnabled(true); + + if (result.first == ftp_crt_SUCCESS) { + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadTechSuccess").c_str(), + itemName.c_str()); + console.addLine(szBuf, true); + + // START + // Clear the CRC Cache if it is populated + safeMutexFTPProgress.Lock(); + Checksum::clearFileCache(); + + vector < string > paths = + Config::getInstance().getPathListForType(ptTechs); + string pathSearchString = string("/") + itemName + string("/*"); + const string filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + + // Refresh CRC + Config & config = Config::getInstance(); + getFolderTreeContentsCheckSumRecursively(config. + getPathListForType + (ptTechs, ""), + string("/") + itemName + + string("/*"), ".xml", + NULL, true); + safeMutexFTPProgress.ReleaseLock(); + + refreshTechs(); + // END + } else { + curl_version_info_data *curlVersion = + curl_version_info(CURLVERSION_NOW); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadTechFail").c_str(), + itemName.c_str(), curlVersion->version, + result.second.c_str()); + console.addLine(szBuf, true); + } + } else if (type == ftp_cct_Scenario) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(true); + + selectedScenarioName = ""; + buttonInstallScenario.setEnabled(true); + + if (result.first == ftp_crt_SUCCESS) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadScenarioSuccess").c_str(), + itemName.c_str()); + console.addLine(szBuf, true); + + // START + // Clear the CRC Cache if it is populated + // + // Clear the CRC file Cache + safeMutexFTPProgress.Lock(); + Checksum::clearFileCache(); + + vector < string > paths = + Config::getInstance().getPathListForType(ptScenarios); + string pathSearchString = string("/") + itemName + string("/*"); + const string filterFileExt = ".xml"; + clearFolderTreeContentsCheckSum(paths, pathSearchString, + filterFileExt); + clearFolderTreeContentsCheckSumList(paths, pathSearchString, + filterFileExt); + + // Refresh CRC + Config & config = Config::getInstance(); + getFolderTreeContentsCheckSumRecursively(config. + getPathListForType + (ptScenarios, ""), + string("/") + itemName + + string("/*"), ".xml", + NULL, true); + safeMutexFTPProgress.ReleaseLock(); + + refreshScenarios(); + // END + } else { + curl_version_info_data *curlVersion = + curl_version_info(CURLVERSION_NOW); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("ModDownloadScenarioFail").c_str(), + itemName.c_str(), curlVersion->version, + result.second.c_str()); + console.addLine(szBuf, true); + } + } + + } + } -*/ - - Config & config = Config::getInstance (); - string scenarioDir = ""; - vector < string > pathList = - config.getPathListForType (ptMaps, scenarioDir); - vector < string > invalidMapList; - vector < string > allMaps = - MapPreview::findAllValidMaps (pathList, scenarioDir, false, false, - &invalidMapList); - if (allMaps.empty ()) - { - throw megaglest_runtime_error ("No maps were found!"); - } - vector < string > results; - copy (allMaps.begin (), allMaps.end (), std::back_inserter (results)); - mapFiles = results; - - mapFilesUserData = - MapPreview::findAllValidMaps (pathList, scenarioDir, true, false, - &invalidMapList); - } - - string MenuStateMods::refreshMapModInfo (string mapInfo) - { - std::vector < std::string > mapInfoList; - Tokenize (mapInfo, mapInfoList, "|"); - if (mapInfoList.size () >= 6) - { - //Config &config = Config::getInstance(); - ModInfo modinfo; - modinfo.name = mapInfoList[0]; - modinfo.count = mapInfoList[1]; - modinfo.crc = mapInfoList[2]; - modinfo.description = mapInfoList[3]; - modinfo.url = mapInfoList[4]; - modinfo.imageUrl = mapInfoList[5]; - modinfo.type = mt_Map; - modinfo.localCRC = getMapCRC (modinfo.name); - mapCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - string MenuStateMods::getMapCRC (string mapName) - { - Config & config = Config::getInstance (); - vector < string > mappaths = config.getPathListForType (ptMaps, ""); - string result = ""; - if (mappaths.empty () == false) - { - Checksum checksum; - string itemPath = mappaths[1] + "/" + mapName; - if (fileExists (itemPath)) - { - checksum.addFile (itemPath); - uint32 crc = checksum.getSum (); - result = uIntToStr (crc); - //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - itemPath = mappaths[0] + "/" + mapName; - if (fileExists (itemPath)) - { - checksum.addFile (itemPath); - uint32 crc = checksum.getSum (); - result = uIntToStr (crc); - //printf("itemPath='%s' modinfo.name='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.name.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - result = ""; - } - } - } - else - { - result = ""; - } - return result; - } - - void MenuStateMods::refreshMaps () - { - getMapsLocalList (); - for (int i = 0; i < (int) mapListRemote.size (); i++) - { - refreshMapModInfo (mapListRemote[i]); - } - } - - void MenuStateMods::getScenariosLocalList () - { - Config & config = Config::getInstance (); - vector < string > results; - findDirs (config.getPathListForType (ptScenarios), results); - scenarioFiles = results; - - scenarioFilesUserData.clear (); - if (config.getPathListForType (ptScenarios).size () > 1) - { - string path = config.getPathListForType (ptScenarios)[1]; - endPathWithSlash (path); - findDirs (path, scenarioFilesUserData, false, false); - } - } - - string MenuStateMods::refreshScenarioModInfo (string scenarioInfo) - { - std::vector < std::string > scenarioInfoList; - Tokenize (scenarioInfo, scenarioInfoList, "|"); - if (scenarioInfoList.size () >= 5) - { - Config & config = Config::getInstance (); - ModInfo modinfo; - modinfo.name = scenarioInfoList[0]; - modinfo.crc = scenarioInfoList[1]; - modinfo.description = scenarioInfoList[2]; - modinfo.url = scenarioInfoList[3]; - modinfo.imageUrl = scenarioInfoList[4]; - modinfo.type = mt_Scenario; - - string itemPath = - config.getPathListForType (ptScenarios, - "")[1] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - uint32 crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", NULL, - forceRefresh); - if (crc == 0) - { - itemPath = - config.getPathListForType (ptScenarios, - "")[0] + "/" + modinfo.name + - string ("/*"); - if (itemPath.empty () == false) - { - forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - crc = - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, forceRefresh); - } - } - modinfo.localCRC = uIntToStr (crc); - //printf(" itemPath='%s' remote crc:'%s' local crc:'%s' crc='%d' \n",itemPath.c_str(),modinfo.crc.c_str(),modinfo.localCRC.c_str(),crc); - } - else - { - modinfo.localCRC = ""; - } - scenarioCacheList[modinfo.name] = modinfo; - return modinfo.name; - } - return ""; - } - - void MenuStateMods::refreshScenarios () - { - getScenariosLocalList (); - for (int i = 0; i < (int) scenarioListRemote.size (); i++) - { - refreshScenarioModInfo (scenarioListRemote[i]); - } - } - - - void MenuStateMods::cleanUp () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (modHttpServerThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - modHttpServerThread->signalQuit (); - //modHttpServerThread->setThreadOwnerValid(false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (modHttpServerThread->canShutdown (true) == true - && modHttpServerThread->shutdownAndWait () == true) - { - delete modHttpServerThread; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - modHttpServerThread = NULL; - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - if (ftpClientThread != NULL) - { - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - //publishToMasterserverThread->cleanup(); - } - ftpClientThread = NULL; - -// ftpClientThread->signalQuit(); -// ftpClientThread->setCallBackObject(NULL); -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); -// if( ftpClientThread->shutdownAndWait() == true) { -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); -// delete ftpClientThread; -// } -// ftpClientThread = NULL; -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - clearUserButtons (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - cleanupPreviewTexture (); - cleanupMapPreviewTexture (); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - MenuStateMods::~MenuStateMods () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - cleanUp (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - - void MenuStateMods::clearUserButtons () - { - // Techs - while (!keyTechButtons.empty ()) - { - delete keyTechButtons.back (); - keyTechButtons.pop_back (); - } - keyTechScrollBar.setElementCount (0); - - while (!labelsTech.empty ()) - { - delete labelsTech.back (); - labelsTech.pop_back (); - } - - // Tilesets - while (!keyTilesetButtons.empty ()) - { - delete keyTilesetButtons.back (); - keyTilesetButtons.pop_back (); - } - keyTilesetScrollBar.setElementCount (0); - - // Maps - while (!keyMapButtons.empty ()) - { - delete keyMapButtons.back (); - keyMapButtons.pop_back (); - } - while (!labelsMap.empty ()) - { - delete labelsMap.back (); - labelsMap.pop_back (); - } - keyMapScrollBar.setElementCount (0); - - // Scenarios - while (!keyScenarioButtons.empty ()) - { - delete keyScenarioButtons.back (); - keyScenarioButtons.pop_back (); - } - keyScenarioScrollBar.setElementCount (0); - } - - void MenuStateMods::mouseClick (int x, int y, MouseButton mouseButton) - { - - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - Lang & lang = Lang::getInstance (); - - if (buttonReturn.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - soundRenderer.playFx (coreData.getClickSoundA ()); - - if (fileFTPProgressList.empty () == false) - { - mainMessageBoxState = ftpmsg_Quit; - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No"), - 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadInProgressCancelQuestion"). - c_str (), fileFTPProgressList.size ()); - showMessageBox (szBuf, lang.getString ("Question"), true); - } - else - { - cleanUp (); - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - } - else if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMessageBox.setEnabled (false); - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No"), - 450); - if (button == 0) - { - if (mainMessageBoxState == ftpmsg_Quit) - { - mainMessageBoxState = ftpmsg_None; - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - else if (mainMessageBoxState == ftpmsg_GetMap || - mainMessageBoxState == ftpmsg_ReplaceMap) - { - bool getItemAfterRemoval = - (mainMessageBoxState == ftpmsg_ReplaceMap); - mainMessageBoxState = ftpmsg_None; - - Config & config = Config::getInstance (); - vector < string > mapPaths = config.getPathListForType (ptMaps); - if (mapPaths.size () > 1) - { - string removeMap = mapPaths[1]; - endPathWithSlash (removeMap); - removeMap += selectedMapName; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Removing Map [%s]\n", removeMap.c_str ()); - removeFile (removeMap); - - bool remoteHasMap = - (mapCacheList.find (selectedMapName) != - mapCacheList.end ()); - if (remoteHasMap == false) - { - for (unsigned int i = 0; i < keyMapButtons.size (); ++i) - { - GraphicButton *button = keyMapButtons[i]; - if (button != NULL - && button->getText () == selectedMapName) - { - delete button; - keyMapButtons.erase (keyMapButtons.begin () + i); - labelsMap.erase (labelsMap.begin () + i); - keyMapScrollBar.setElementCount ((int) keyMapButtons. - size ()); - break; - } - } - } - - if (getItemAfterRemoval == false) - { - selectedMapName = ""; - } - refreshMaps (); - Checksum::clearFileCache (); - - if (getItemAfterRemoval == true) - { - string mapName = selectedMapName; - string mapURL = mapCacheList[mapName].url; - if (ftpClientThread != NULL) - ftpClientThread->addMapToRequests (mapName, mapURL); - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - fileFTPProgressList[mapName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallMap.setEnabled (false); - } - } - } - else if (mainMessageBoxState == ftpmsg_GetTileset || - mainMessageBoxState == ftpmsg_ReplaceTileset) - { - bool getItemAfterRemoval = - (mainMessageBoxState == ftpmsg_ReplaceTileset); - mainMessageBoxState = ftpmsg_None; - - Config & config = Config::getInstance (); - vector < string > tilesetPaths = - config.getPathListForType (ptTilesets); - if (tilesetPaths.size () > 1) - { - string removeTileset = tilesetPaths[1]; - endPathWithSlash (removeTileset); - removeTileset += selectedTilesetName; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Removing Tileset [%s]\n", removeTileset.c_str ()); - removeFolder (removeTileset); - - bool remoteHasTileset = - (tilesetCacheList.find (selectedTilesetName) != - tilesetCacheList.end ()); - if (remoteHasTileset == false) - { - //printf("\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ remote DOES NOT have removed tileset [%s]\n\n\n",selectedTilesetName.c_str()); - - for (unsigned int i = 0; i < keyTilesetButtons.size (); ++i) - { - GraphicButton *button = keyTilesetButtons[i]; - if (button != NULL - && button->getText () == selectedTilesetName) - { - delete button; - keyTilesetButtons.erase (keyTilesetButtons.begin () + - i); - keyTilesetScrollBar. - setElementCount ((int) keyTilesetButtons.size ()); - break; - } - } - } - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - Checksum::clearFileCache (); - vector < string > paths = - Config::getInstance ().getPathListForType (ptTilesets); - string pathSearchString = - string ("/") + selectedTilesetName + string ("/*"); - const string filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - safeMutexFTPProgress.ReleaseLock (); - - if (getItemAfterRemoval == false) - { - selectedTilesetName = ""; - } - refreshTilesets (); - } - - if (getItemAfterRemoval == true) - { - string tilesetName = selectedTilesetName; - string tilesetURL = tilesetCacheList[tilesetName].url; - if (ftpClientThread != NULL) - ftpClientThread->addTilesetToRequests (tilesetName, - tilesetURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - fileFTPProgressList[tilesetName] = - pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallTileset.setEnabled (false); - } - } - else if (mainMessageBoxState == ftpmsg_GetTechtree || - mainMessageBoxState == ftpmsg_ReplaceTechtree) - { - bool getItemAfterRemoval = - (mainMessageBoxState == ftpmsg_ReplaceTechtree); - mainMessageBoxState = ftpmsg_None; - - Config & config = Config::getInstance (); - vector < string > techPaths = - config.getPathListForType (ptTechs); - if (techPaths.size () > 1) - { - string removeTech = techPaths[1]; - endPathWithSlash (removeTech); - removeTech += selectedTechName; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Removing Techtree [%s]\n", removeTech.c_str ()); - removeFolder (removeTech); - - bool remoteHasTech = - (techCacheList.find (selectedTechName) != - techCacheList.end ()); - if (remoteHasTech == false) - { - for (unsigned int i = 0; i < keyTechButtons.size (); ++i) - { - GraphicButton *button = keyTechButtons[i]; - if (button != NULL - && button->getText () == selectedTechName) - { - delete button; - keyTechButtons.erase (keyTechButtons.begin () + i); - labelsTech.erase (labelsTech.begin () + i); - keyTechScrollBar.setElementCount ((int) keyTechButtons. - size ()); - break; - } - } - } - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - // Clear the CRC file Cache - Checksum::clearFileCache (); - vector < string > paths = - Config::getInstance ().getPathListForType (ptTechs); - string pathSearchString = - string ("/") + selectedTechName + string ("/*"); - const string filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - safeMutexFTPProgress.ReleaseLock (); - - if (getItemAfterRemoval == false) - { - selectedTechName = ""; - } - refreshTechs (); - } - - if (getItemAfterRemoval == true) - { - string techName = selectedTechName; - string techURL = techCacheList[techName].url; - if (ftpClientThread != NULL) - ftpClientThread->addTechtreeToRequests (techName, techURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - fileFTPProgressList[techName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallTech.setEnabled (false); - } - } - else if (mainMessageBoxState == ftpmsg_GetScenario || - mainMessageBoxState == ftpmsg_ReplaceScenario) - { - bool getItemAfterRemoval = - (mainMessageBoxState == ftpmsg_ReplaceScenario); - mainMessageBoxState = ftpmsg_None; - - Config & config = Config::getInstance (); - vector < string > scenarioPaths = - config.getPathListForType (ptScenarios); - if (scenarioPaths.size () > 1) - { - string removeScenario = scenarioPaths[1]; - endPathWithSlash (removeScenario); - removeScenario += selectedScenarioName; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Removing Scenario [%s]\n", - removeScenario.c_str ()); - removeFolder (removeScenario); - - bool remoteHasScenario = - (scenarioCacheList.find (selectedScenarioName) != - scenarioCacheList.end ()); - if (remoteHasScenario == false) - { - for (unsigned int i = 0; i < keyScenarioButtons.size (); - ++i) - { - GraphicButton *button = keyScenarioButtons[i]; - if (button != NULL - && button->getText () == selectedScenarioName) - { - delete button; - keyScenarioButtons.erase (keyScenarioButtons.begin () + - i); - keyScenarioScrollBar. - setElementCount ((int) keyScenarioButtons.size ()); - break; - } - } - } - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - Checksum::clearFileCache (); - vector < string > paths = - Config::getInstance ().getPathListForType (ptScenarios); - string pathSearchString = - string ("/") + selectedScenarioName + string ("/*"); - const string filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - safeMutexFTPProgress.ReleaseLock (); - - if (getItemAfterRemoval == false) - { - selectedScenarioName = ""; - } - refreshScenarios (); - } - - if (getItemAfterRemoval == true) - { - string scenarioName = selectedScenarioName; - string scenarioURL = scenarioCacheList[scenarioName].url; - if (ftpClientThread != NULL) - ftpClientThread->addScenarioToRequests (scenarioName, - scenarioURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - fileFTPProgressList[scenarioName] = - pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallScenario.setEnabled (false); - } - } - } - } - } - else if (keyTechScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (keyTilesetScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (keyMapScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (keyScenarioScrollBar.mouseClick (x, y)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - else if (buttonInstallTech.mouseClick (x, y) - && buttonInstallTech.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedTechName != "") - { - bool alreadyHasTech = - (std:: - find (techTreeFiles.begin (), techTreeFiles.end (), - selectedTechName) != techTreeFiles.end ()); - if (alreadyHasTech == true) - { - bool remoteHasTech = - (techCacheList.find (selectedTechName) != techCacheList.end ()); - if (remoteHasTech == true) - { - ModInfo & modInfo = techCacheList[selectedTechName]; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, - __FUNCTION__, __LINE__, modInfo.crc.c_str ()); - - Config & config = Config::getInstance (); - string itemPath = - config.getPathListForType (ptTechs, - "")[1] + "/" + selectedTechName + - string ("/*"); - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - if (strToUInt (modInfo.crc) != 0 - && strToUInt (modInfo.crc) != - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, - forceRefresh)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] local CRC [%u]\n", __FILE__, - __FUNCTION__, __LINE__, - getFolderTreeContentsCheckSumRecursively (itemPath, - ".xml", - NULL)); - - mainMessageBoxState = ftpmsg_ReplaceTechtree; - mainMessageBox.init (lang.getString ("Yes"), - lang.getString ("No"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModLocalRemoteMismatch").c_str (), - selectedTechName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModTechAlreadyInstalled").c_str (), - selectedTechName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - mapCRCUpdateList[itemPath] = true; - } - } - else - { - string techName = selectedTechName; - string techURL = techCacheList[techName].url; - if (ftpClientThread != NULL) - ftpClientThread->addTechtreeToRequests (techName, techURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList[techName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallTech.setEnabled (false); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectTechToInstall"), - lang.getString ("Notice"), true); - } - } - else if (buttonRemoveTech.mouseClick (x, y) - && buttonRemoveTech.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedTechName != "") - { - bool alreadyHasTech = - (std:: - find (techTreeFiles.begin (), techTreeFiles.end (), - selectedTechName) != techTreeFiles.end ()); - if (alreadyHasTech == true) - { - mainMessageBoxState = ftpmsg_GetTechtree; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModRemoveTechConfirm").c_str (), - selectedTechName.c_str ()); - showMessageBox (szBuf, lang.getString ("Question"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModCannotRemoveTechNotInstalled"). - c_str (), selectedTechName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - - showMessageBox (lang.getString ("ModSelectTechToRemove"), - lang.getString ("Notice"), true); - } - } - - else if (buttonInstallTileset.mouseClick (x, y) - && buttonInstallTileset.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedTilesetName != "") - { - bool alreadyHasTileset = - (std:: - find (tilesetFiles.begin (), tilesetFiles.end (), - selectedTilesetName) != tilesetFiles.end ()); - if (alreadyHasTileset == true) - { - bool remoteHasTileset = - (tilesetCacheList.find (selectedTilesetName) != - tilesetCacheList.end ()); - if (remoteHasTileset) - { - ModInfo & modInfo = tilesetCacheList[selectedTilesetName]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, - __FUNCTION__, __LINE__, modInfo.crc.c_str ()); - - Config & config = Config::getInstance (); - string itemPath = - config.getPathListForType (ptTilesets, - "")[1] + "/" + - selectedTilesetName + string ("/*"); - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - - if (strToUInt (modInfo.crc) != 0 && - strToUInt (modInfo.crc) != - getFolderTreeContentsCheckSumRecursively (itemPath, ".xml", - NULL, - forceRefresh)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] local CRC [%u] [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - getFolderTreeContentsCheckSumRecursively (itemPath, - ".xml", - NULL), - itemPath.c_str ()); - - mainMessageBoxState = ftpmsg_ReplaceTileset; - mainMessageBox.init (lang.getString ("Yes"), - lang.getString ("No"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModLocalRemoteMismatch").c_str (), - selectedTilesetName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModTilesetAlreadyInstalled"). - c_str (), selectedTilesetName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - } - else - { - string tilesetName = selectedTilesetName; - string tilesetURL = tilesetCacheList[tilesetName].url; - if (ftpClientThread != NULL) - ftpClientThread->addTilesetToRequests (tilesetName, tilesetURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList[tilesetName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallTileset.setEnabled (false); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectTilesetToInstall"), - lang.getString ("Notice"), true); - } - } - else if (buttonRemoveTileset.mouseClick (x, y) - && buttonRemoveTileset.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedTilesetName != "") - { - bool alreadyHasTileset = - (std:: - find (tilesetFiles.begin (), tilesetFiles.end (), - selectedTilesetName) != tilesetFiles.end ()); - if (alreadyHasTileset == true) - { - mainMessageBoxState = ftpmsg_GetTileset; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModRemoveTilesetConfirm").c_str (), - selectedTilesetName.c_str ()); - showMessageBox (szBuf, lang.getString ("Question"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModCannotRemoveTilesetNotInstalled"). - c_str (), selectedTilesetName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectTilesetToRemove"), - lang.getString ("Notice"), true); - } - } - - else if (buttonInstallMap.mouseClick (x, y) - && buttonInstallMap.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedMapName != "") - { - bool alreadyHasMap = - (std:: - find (mapFiles.begin (), mapFiles.end (), - selectedMapName) != mapFiles.end ()); - if (alreadyHasMap == true) - { - bool remoteHasMap = - (mapCacheList.find (selectedMapName) != mapCacheList.end ()); - if (remoteHasMap) - { - ModInfo & modInfo = mapCacheList[selectedMapName]; - if (modInfo.crc != modInfo.localCRC) - { - mainMessageBoxState = ftpmsg_ReplaceMap; - mainMessageBox.init (lang.getString ("Yes"), - lang.getString ("No"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModLocalRemoteMismatch").c_str (), - selectedMapName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModMapAlreadyInstalled").c_str (), - selectedMapName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - } - else - { - string mapName = selectedMapName; - string mapURL = mapCacheList[mapName].url; - if (ftpClientThread != NULL) - ftpClientThread->addMapToRequests (mapName, mapURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList[mapName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallMap.setEnabled (false); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectMapToInstall"), - lang.getString ("Notice"), true); - } - } - else if (buttonRemoveMap.mouseClick (x, y) - && buttonRemoveMap.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedMapName != "") - { - bool alreadyHasMap = - (std:: - find (mapFiles.begin (), mapFiles.end (), - selectedMapName) != mapFiles.end ()); - if (alreadyHasMap == true) - { - mainMessageBoxState = ftpmsg_GetMap; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModRemoveMapConfirm").c_str (), - selectedMapName.c_str ()); - showMessageBox (szBuf, lang.getString ("Question"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModCannotRemoveMapNotInstalled"). - c_str (), selectedMapName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectMapToRemove"), - lang.getString ("Notice"), true); - } - } - - else if (buttonInstallScenario.mouseClick (x, y) - && buttonInstallScenario.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedScenarioName != "") - { - bool alreadyHasScenario = - (std:: - find (scenarioFiles.begin (), scenarioFiles.end (), - selectedScenarioName) != scenarioFiles.end ()); - if (alreadyHasScenario == true) - { - bool remoteHasScenario = - (scenarioCacheList.find (selectedScenarioName) != - scenarioCacheList.end ()); - if (remoteHasScenario) - { - ModInfo & modInfo = scenarioCacheList[selectedScenarioName]; - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] remote CRC [%s]\n", __FILE__, - __FUNCTION__, __LINE__, modInfo.crc.c_str ()); - - Config & config = Config::getInstance (); - string itemPath = - config.getPathListForType (ptScenarios, - "")[1] + "/" + - selectedScenarioName + string ("/*"); - bool forceRefresh = - (mapCRCUpdateList.find (itemPath) == mapCRCUpdateList.end ()); - - if (strToUInt (modInfo.crc) != 0 && - strToUInt (modInfo.crc) != - getFolderTreeContentsCheckSumRecursively (itemPath, "", - NULL, - forceRefresh)) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] local CRC [%u]\n", __FILE__, - __FUNCTION__, __LINE__, - getFolderTreeContentsCheckSumRecursively (itemPath, - "", - NULL)); - - mainMessageBoxState = ftpmsg_ReplaceScenario; - mainMessageBox.init (lang.getString ("Yes"), - lang.getString ("No"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModLocalRemoteMismatch").c_str (), - selectedScenarioName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModScenarioAlreadyInstalled"). - c_str (), selectedScenarioName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - } - else - { - string scenarioName = selectedScenarioName; - string scenarioURL = scenarioCacheList[scenarioName].url; - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] adding file to download [%s]\n",__FILE__,__FUNCTION__,__LINE__,scenarioURL.c_str()); - if (ftpClientThread != NULL) - ftpClientThread->addScenarioToRequests (scenarioName, - scenarioURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList[scenarioName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - buttonInstallScenario.setEnabled (false); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectScenarioToInstall"), - lang.getString ("Notice"), true); - } - } - else if (buttonRemoveScenario.mouseClick (x, y) - && buttonRemoveScenario.getEnabled ()) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - if (selectedScenarioName != "") - { - bool alreadyHasScenario = - (std:: - find (scenarioFiles.begin (), scenarioFiles.end (), - selectedScenarioName) != scenarioFiles.end ()); - if (alreadyHasScenario == true) - { - mainMessageBoxState = ftpmsg_GetScenario; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModRemoveScenarioConfirm").c_str (), - selectedScenarioName.c_str ()); - showMessageBox (szBuf, lang.getString ("Question"), true); - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModCannotRemoveScenarioNotInstalled"). - c_str (), selectedScenarioName.c_str ()); - showMessageBox (szBuf, lang.getString ("Notice"), true); - } - } - else - { - mainMessageBoxState = ftpmsg_None; - mainMessageBox.init (lang.getString ("Ok"), 450); - showMessageBox (lang.getString ("ModSelectScenarioToRemove"), - lang.getString ("Notice"), true); - } - } - - else - { - if (keyMapScrollBar.getElementCount () != 0) - { - for (int i = keyMapScrollBar.getVisibleStart (); - i <= keyMapScrollBar.getVisibleEnd (); ++i) - { - if (keyMapButtons[i]->mouseClick (x, y) - && keyMapButtons[i]->getEnabled ()) - { - string mapName = keyMapButtons[i]->getText (); - selectedTechName = ""; - selectedTilesetName = ""; - selectedMapName = ""; - selectedScenarioName = ""; - if (mapName != "") - { - selectedMapName = mapName; - bool remoteHasMap = - (mapCacheList.find (mapName) != mapCacheList.end ()); - if (remoteHasMap) - { - showRemoteDesription (&mapCacheList[selectedMapName]); - if (mapCacheList[selectedMapName].localCRC != "") - { - loadMapPreview (mapName); - } - } - else - { - showLocalDescription (mapName); - loadMapPreview (mapName); - } - - } - break; - } - } - } - if (keyTechScrollBar.getElementCount () != 0) - { - for (int i = keyTechScrollBar.getVisibleStart (); - i <= keyTechScrollBar.getVisibleEnd (); ++i) - { - if (keyTechButtons[i]->mouseClick (x, y) - && keyTechButtons[i]->getEnabled ()) - { - string techName = keyTechButtons[i]->getText (); - selectedTechName = ""; - selectedTilesetName = ""; - selectedMapName = ""; - selectedScenarioName = ""; - if (techName != "") - { - selectedTechName = techName; - bool remoteHasTech = - (techCacheList.find (techName) != techCacheList.end ()); - if (remoteHasTech) - { - showRemoteDesription (&techCacheList[techName]); - } - else - { - showLocalDescription (techName); - } - } - break; - } - } - } - if (keyTilesetScrollBar.getElementCount () != 0) - { - for (int i = keyTilesetScrollBar.getVisibleStart (); - i <= keyTilesetScrollBar.getVisibleEnd (); ++i) - { - if (keyTilesetButtons[i]->mouseClick (x, y) - && keyTilesetButtons[i]->getEnabled ()) - { - string tilesetName = keyTilesetButtons[i]->getText (); - selectedTechName = ""; - selectedTilesetName = ""; - selectedMapName = ""; - selectedScenarioName = ""; - if (tilesetName != "") - { - selectedTilesetName = tilesetName; - bool remoteHasTileset = - (tilesetCacheList.find (tilesetName) != - tilesetCacheList.end ()); - if (remoteHasTileset) - { - showRemoteDesription (&tilesetCacheList[tilesetName]); - } - else - { - showLocalDescription (tilesetName); - } - } - break; - } - } - } - if (keyScenarioScrollBar.getElementCount () != 0) - { - for (int i = keyScenarioScrollBar.getVisibleStart (); - i <= keyScenarioScrollBar.getVisibleEnd (); ++i) - { - if (keyScenarioButtons[i]->mouseClick (x, y) - && keyScenarioButtons[i]->getEnabled ()) - { - string scenarioName = keyScenarioButtons[i]->getText (); - selectedTechName = ""; - selectedTilesetName = ""; - selectedMapName = ""; - selectedScenarioName = ""; - if (scenarioName != "") - { - selectedScenarioName = scenarioName; - bool remoteHasScenario = - (scenarioCacheList.find (scenarioName) != - scenarioCacheList.end ()); - if (remoteHasScenario) - { - showRemoteDesription (&scenarioCacheList[scenarioName]); - } - else - { - showLocalDescription (scenarioName); - } - - } - break; - } - } - } - - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string MenuStateMods::getPreviewImageFileForMod (const ModInfo * modInfo) - { - string fileName = ""; - if (modInfo->imageUrl != "") - { - Config & config = Config::getInstance (); - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - string tempPath = userData + "temp/"; - if (isdir (tempPath.c_str ()) == false) - { - createDirectoryPaths (tempPath); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("### tempPath [%s] isdir = %d\n", tempPath.c_str (), - isdir (tempPath.c_str ())); - - if (isdir (tempPath.c_str ()) == true) - { - fileName = tempPath; - switch (modInfo->type) - { - case mt_Map: - fileName += "map_"; - break; - case mt_Tileset: - fileName += "tileset_"; - break; - case mt_Techtree: - fileName += "tech_"; - break; - case mt_Scenario: - fileName += "scenario_"; - break; - } - fileName += extractFileFromDirectoryPath (modInfo->imageUrl); - } - } - return fileName; - } - - - void MenuStateMods::showLocalDescription (string name) - { - Lang & lang = Lang::getInstance (); - modInfoSelected = NULL; - cleanupPreviewTexture (); - validMapPreview = false; - cleanupMapPreviewTexture (); - modDescrLabel.setText (lang.getString ("ModOnlyLocal") + ":\n'" + name + - "'"); - } - - void MenuStateMods::loadMapPreview (string mapName) - { - Config & config = Config::getInstance (); - cleanupMapPreviewTexture (); - vector < string > mapPaths = config.getPathListForType (ptMaps); - string & mapPath = mapPaths[1]; - endPathWithSlash (mapPath); - mapPath += mapName; - MapInfo mapInfo = loadMapInfo (mapPath); - if (enableMapPreview == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - mapPreview.loadFromFile (mapPath.c_str ()); - - //printf("Loading map preview MAP\n"); - cleanupMapPreviewTexture (); - validMapPreview = true; - } - } - - - - void MenuStateMods::showRemoteDesription (ModInfo * modInfo) - { - //displayModPreviewImage = false; - modInfoSelected = modInfo; - validMapPreview = false; - cleanupMapPreviewTexture (); - - string modText = modInfo->description; - replaceAll (modText, "\\n", "\n"); - modDescrLabel.setText (modText); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("### modInfo->imageUrl [%s]\n", modInfo->imageUrl.c_str ()); - - if (modInfo->imageUrl != "") - { - cleanupPreviewTexture (); - string tempImage = getPreviewImageFileForMod (modInfo); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("### tempImage [%s] exists [%d]\n", tempImage.c_str (), - fileExists (tempImage)); - - if (tempImage != "" && fileExists (tempImage) == false) - { - if (ftpClientThread != NULL) - ftpClientThread->addFileToRequests (tempImage, modInfo->imageUrl); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList[tempImage] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - - } - else - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - if (fileFTPProgressList.find (tempImage) == - fileFTPProgressList.end ()) - { - //displayModPreviewImage = true; - displayModPreviewImage[tempImage] = true; - } - safeMutexFTPProgress.ReleaseLock (); - } - } - } - - void MenuStateMods::mouseUp (int x, int y, const MouseButton mouseButton) - { - if (mouseButton == mbLeft) - { - keyTechScrollBar.mouseUp (x, y); - keyTilesetScrollBar.mouseUp (x, y); - keyMapScrollBar.mouseUp (x, y); - keyScenarioScrollBar.mouseUp (x, y); - } - } - - void MenuStateMods::mouseMove (int x, int y, const MouseState * ms) - { - buttonReturn.mouseMove (x, y); - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - - buttonInstallTech.mouseMove (x, y); - buttonRemoveTech.mouseMove (x, y); - buttonInstallTileset.mouseMove (x, y); - buttonRemoveTileset.mouseMove (x, y); - buttonInstallMap.mouseMove (x, y); - buttonRemoveMap.mouseMove (x, y); - buttonInstallScenario.mouseMove (x, y); - buttonRemoveScenario.mouseMove (x, y); - - if (ms->get (mbLeft)) - { - keyMapScrollBar.mouseDown (x, y); - keyTechScrollBar.mouseDown (x, y); - keyTilesetScrollBar.mouseDown (x, y); - keyScenarioScrollBar.mouseDown (x, y); - } - else - { - keyMapScrollBar.mouseMove (x, y); - keyTechScrollBar.mouseMove (x, y); - keyTilesetScrollBar.mouseMove (x, y); - keyScenarioScrollBar.mouseMove (x, y); - } - - if (keyMapScrollBar.getElementCount () != 0) - { - for (int i = keyMapScrollBar.getVisibleStart (); - i <= keyMapScrollBar.getVisibleEnd (); ++i) - { - keyMapButtons[i]->mouseMove (x, y); - } - } - if (keyTechScrollBar.getElementCount () != 0) - { - for (int i = keyTechScrollBar.getVisibleStart (); - i <= keyTechScrollBar.getVisibleEnd (); ++i) - { - keyTechButtons[i]->mouseMove (x, y); - } - } - if (keyTilesetScrollBar.getElementCount () != 0) - { - for (int i = keyTilesetScrollBar.getVisibleStart (); - i <= keyTilesetScrollBar.getVisibleEnd (); ++i) - { - keyTilesetButtons[i]->mouseMove (x, y); - } - } - if (keyScenarioScrollBar.getElementCount () != 0) - { - for (int i = keyScenarioScrollBar.getVisibleStart (); - i <= keyScenarioScrollBar.getVisibleEnd (); ++i) - { - keyScenarioButtons[i]->mouseMove (x, y); - } - } - } - - void MenuStateMods::cleanupPreviewTexture () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n", __FILE__, - __FUNCTION__, __LINE__, modPreviewImage); - - if (modPreviewImage != NULL) - { - Renderer::getInstance ().endTexture (rsGlobal, modPreviewImage, - false); - } - modPreviewImage = NULL; - } - - void MenuStateMods::cleanupMapPreviewTexture () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - //printf("CLEANUP map preview texture\n"); - if (mapPreviewTexture != NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - mapPreviewTexture->end (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - delete mapPreviewTexture; - mapPreviewTexture = NULL; - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - - void MenuStateMods::render () - { - try - { - Renderer & renderer = Renderer::getInstance (); - - renderer.renderLine (&lineHorizontal); - renderer.renderLine (&lineVertical); - renderer.renderLine (&lineReturn); - renderer.renderLine (&lineVerticalReturn); - renderer.renderButton (&buttonReturn); - - renderer.renderButton (&buttonInstallTech); - renderer.renderButton (&buttonRemoveTech); - renderer.renderButton (&buttonInstallTileset); - renderer.renderButton (&buttonRemoveTileset); - renderer.renderButton (&buttonInstallMap); - renderer.renderButton (&buttonRemoveMap); - renderer.renderButton (&buttonInstallScenario); - renderer.renderButton (&buttonRemoveScenario); - - renderer.renderButton (&buttonInstalled); - renderer.renderButton (&buttonAvailable); - renderer.renderButton (&buttonConflict); - renderer.renderButton (&buttonOnlyLocal); - - renderer.renderLabel (&modDescrLabel); - if (modInfoSelected != NULL) - { - string tempImage = getPreviewImageFileForMod (modInfoSelected); - if (displayModPreviewImage.find (tempImage) != - displayModPreviewImage.end () - && displayModPreviewImage[tempImage] == true) - { - if (modPreviewImage == NULL) - { - string tempImage = getPreviewImageFileForMod (modInfoSelected); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("### Render tempImage [%s] fileExists(tempImage) = %d\n", - tempImage.c_str (), fileExists (tempImage)); - - if (tempImage != "" && fileExists (tempImage) == true) - { - cleanupPreviewTexture (); - modPreviewImage = Renderer::findTexture (tempImage); - } - } - if (modPreviewImage != NULL) - { - renderer.renderTextureQuad (508, 90, 485, 325, modPreviewImage, - 1.0f); - } - } - } - - // Render Tech List - renderer.renderLabel (&keyTechScrollBarTitle1); - renderer.renderLabel (&keyTechScrollBarTitle2); - if (keyTechScrollBar.getElementCount () != 0) - { - for (int i = keyTechScrollBar.getVisibleStart (); - i <= keyTechScrollBar.getVisibleEnd (); ++i) - { - bool alreadyHasTech = - (std:: - find (techTreeFiles.begin (), techTreeFiles.end (), - keyTechButtons[i]->getText ()) != techTreeFiles.end ()); - if (keyTechButtons[i]->getText () == selectedTechName) - { - bool lightedOverride = true; - renderer.renderButton (keyTechButtons[i], &WHITE, - &lightedOverride); - } - else if (alreadyHasTech == true) - { - Vec4f buttonColor = WHITE; - buttonColor.w = 0.75f; - bool remoteHasTech = - (techCacheList.find (keyTechButtons[i]->getText ()) != - techCacheList.end ()); - if (remoteHasTech) - { - ModInfo & modInfo = - techCacheList[keyTechButtons[i]->getText ()]; - if (modInfo.crc == modInfo.localCRC) - { - keyTechButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerInstalledTexture ()); - } - else - { - //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); - keyTechButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerDifferentTexture ()); - } - } - else - { - keyTechButtons[i]->setCustomTexture (CoreData::getInstance (). - getNotOnServerTexture - ()); - } - renderer.renderButton (keyTechButtons[i], &buttonColor); - } - else - { - Vec4f fontColor = - Vec4f (200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, - 0.75f); - //Vec4f fontColor=Vec4f(1.0f, 0.0f, 0.0f, 0.75f); - keyTechButtons[i]->setCustomTexture (CoreData::getInstance (). - getOnServerTexture ()); - renderer.renderButton (keyTechButtons[i], &fontColor); - } - renderer.renderLabel (labelsTech[i]); - } - } - renderer.renderScrollBar (&keyTechScrollBar); - - // Render Tileset List - renderer.renderLabel (&keyTilesetScrollBarTitle1); - if (keyTilesetScrollBar.getElementCount () != 0) - { - for (int i = keyTilesetScrollBar.getVisibleStart (); - i <= keyTilesetScrollBar.getVisibleEnd (); ++i) - { - bool alreadyHasTileset = - (std:: - find (tilesetFiles.begin (), tilesetFiles.end (), - keyTilesetButtons[i]->getText ()) != - tilesetFiles.end ()); - if (keyTilesetButtons[i]->getText () == selectedTilesetName) - { - bool lightedOverride = true; - renderer.renderButton (keyTilesetButtons[i], &WHITE, - &lightedOverride); - } - else if (alreadyHasTileset == true) - { - Vec4f buttonColor = WHITE; - buttonColor.w = 0.75f; - bool remoteHasTileset = - (tilesetCacheList.find (keyTilesetButtons[i]->getText ()) != - tilesetCacheList.end ()); - if (remoteHasTileset) - { - ModInfo & modInfo = - tilesetCacheList[keyTilesetButtons[i]->getText ()]; - - if (modInfo.crc == modInfo.localCRC) - { - keyTilesetButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerInstalledTexture ()); - } - else - { - //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); - //printf("name [%s] modInfo.crc [%s] modInfo.localCRC [%s]\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); - - keyTilesetButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerDifferentTexture ()); - } - } - else - { - keyTilesetButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getNotOnServerTexture ()); - } - renderer.renderButton (keyTilesetButtons[i], &buttonColor); - } - else - { - Vec4f fontColor = - Vec4f (200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, - 0.75f); - keyTilesetButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerTexture ()); - renderer.renderButton (keyTilesetButtons[i], &fontColor); - } - } - } - renderer.renderScrollBar (&keyTilesetScrollBar); - - // Render Map list - renderer.renderLabel (&keyMapScrollBarTitle1); - renderer.renderLabel (&keyMapScrollBarTitle2); - if (keyMapScrollBar.getElementCount () != 0) - { - for (int i = keyMapScrollBar.getVisibleStart (); - i <= keyMapScrollBar.getVisibleEnd (); ++i) - { - string mapNameToRender = keyMapButtons[i]->getText (); - bool alreadyHasMap = - (std:: - find (mapFiles.begin (), mapFiles.end (), - mapNameToRender) != mapFiles.end ()); - if (keyMapButtons[i]->getText () == selectedMapName) - { - bool lightedOverride = true; - renderer.renderButton (keyMapButtons[i], &WHITE, - &lightedOverride); - } - else if (alreadyHasMap == true) - { - Vec4f buttonColor = WHITE; - buttonColor.w = 0.75f; - bool remoteHasMap = - (mapCacheList.find (keyMapButtons[i]->getText ()) != - mapCacheList.end ()); - if (remoteHasMap) - { - ModInfo & modInfo = - mapCacheList[keyMapButtons[i]->getText ()]; - if (modInfo.crc == modInfo.localCRC) - { - keyMapButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerInstalledTexture ()); - } - else - { - //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); - keyMapButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerDifferentTexture ()); - } - } - else - { - keyMapButtons[i]->setCustomTexture (CoreData::getInstance (). - getNotOnServerTexture ()); - } - renderer.renderButton (keyMapButtons[i], &buttonColor); - } - else - { - Vec4f fontColor = - Vec4f (200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, - 0.75f); - keyMapButtons[i]->setCustomTexture (CoreData::getInstance (). - getOnServerTexture ()); - renderer.renderButton (keyMapButtons[i], &fontColor); - } - renderer.renderLabel (labelsMap[i]); - } - } - renderer.renderScrollBar (&keyMapScrollBar); - - // Render Scenario List - renderer.renderLabel (&keyScenarioScrollBarTitle1); - if (keyScenarioScrollBar.getElementCount () != 0) - { - for (int i = keyScenarioScrollBar.getVisibleStart (); - i <= keyScenarioScrollBar.getVisibleEnd (); ++i) - { - if (i >= (int) keyScenarioButtons.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "i >= keyScenarioButtons.size(), i = %d keyScenarioButtons.size() = %d", - i, (int) keyScenarioButtons.size ()); - throw megaglest_runtime_error (szBuf); - } - bool alreadyHasScenario = - (std:: - find (scenarioFiles.begin (), scenarioFiles.end (), - keyScenarioButtons[i]->getText ()) != - scenarioFiles.end ()); - - if (keyScenarioButtons[i]->getText () == selectedScenarioName) - { - bool lightedOverride = true; - renderer.renderButton (keyScenarioButtons[i], &WHITE, - &lightedOverride); - } - else if (alreadyHasScenario == true) - { - Vec4f buttonColor = WHITE; - buttonColor.w = 0.75f; - bool remoteHasScenario = - (scenarioCacheList.find (keyScenarioButtons[i]->getText ()) != - scenarioCacheList.end ()); - if (remoteHasScenario) - { - ModInfo & modInfo = - scenarioCacheList[keyScenarioButtons[i]->getText ()]; - if (modInfo.crc == modInfo.localCRC) - { - keyScenarioButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerInstalledTexture ()); - } - else - { - //printf("modInfo.name=%s modInfo.crc=%s modInfo.localCRC=%s\n",modInfo.name.c_str(),modInfo.crc.c_str(),modInfo.localCRC.c_str()); - keyScenarioButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerDifferentTexture ()); - } - } - else - { - keyScenarioButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getNotOnServerTexture ()); - } - renderer.renderButton (keyScenarioButtons[i], &buttonColor); - } - else - { - Vec4f fontColor = - Vec4f (200.0f / 255.0f, 187.0f / 255.0f, 190.0f / 255.0f, - 0.75f); - keyScenarioButtons[i]-> - setCustomTexture (CoreData::getInstance (). - getOnServerTexture ()); - renderer.renderButton (keyScenarioButtons[i], &fontColor); - } - } - } - renderer.renderScrollBar (&keyScenarioScrollBar); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - if (fileFTPProgressList.empty () == false) - { - Lang & lang = Lang::getInstance (); - int xLocation = buttonReturn.getX (); - int yLocation = buttonReturn.getY () - 12; - for (std::map < string, pair < int, string > >::iterator iterMap = - fileFTPProgressList.begin (); - iterMap != fileFTPProgressList.end (); ++iterMap) - { - - string progressLabelPrefix = - lang.getString ("ModDownloading") + " " + - extractFileFromDirectoryPath (iterMap->first) + " "; - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nRendering file progress with the following prefix [%s]\n",progressLabelPrefix.c_str()); - - if (Renderer::renderText3DEnabled) - { - renderer.renderProgressBar3D (iterMap->second.first, - //10, - //yLocation, - xLocation, - yLocation, - CoreData::getInstance (). - getDisplayFontSmall3D (), 185, - progressLabelPrefix, false); - } - else - { - renderer.renderProgressBar (iterMap->second.first, - //10, - //yLocation, - xLocation, - yLocation, - CoreData::getInstance (). - getDisplayFontSmall (), 185, - progressLabelPrefix, false); - } - - yLocation -= 14; - } - } - safeMutexFTPProgress.ReleaseLock (); - - renderer.renderConsole (&console, consoleNormal, 3); - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - - if (modMenuState != mmst_None) - { - if (oldMenuState != modMenuState) - { - Lang & lang = Lang::getInstance (); - if (modMenuState == mmst_Loading) - { - pleaseWaitLabel.setText (lang. - getString - ("GettingModlistFromMasterserver")); - } - else if (modMenuState == mmst_CalculatingCRC) - { - pleaseWaitLabel.setText (lang. - getString - ("PleaseWaitCalculatingCRC")); - } - oldMenuState = modMenuState; - } - float anim = GraphicComponent::getAnim (); - if (anim < 0.5f) - { - anim = 1.f - anim; - } - Vec4f colorWithAlpha = Vec4f (ORANGE.x, ORANGE.y, ORANGE.z, anim); - renderer.renderLabel (&pleaseWaitLabel, &colorWithAlpha); - } - if (validMapPreview) - { - if (mapPreviewTexture != NULL) - { - renderer.renderTextureQuad (5, 185, 150, 150, mapPreviewTexture, - 1.0f); - } - if (enableMapPreview && (mapPreview.hasFileLoaded () == true)) - { - - int mouseX = mainMenu->getMouseX (); - int mouseY = mainMenu->getMouseY (); - int mouse2dAnim = mainMenu->getMouse2dAnim (); - - if (mapPreviewTexture == NULL) - { - renderer.renderMouse2d (mouseX, mouseY, mouse2dAnim); - renderer.renderMapPreview (&mapPreview, true, 10, 350, - &mapPreviewTexture); - } - } - } - - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] error [%s]\n", __FILE__, - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - } - - void MenuStateMods::update () - { - Chrono chrono; - chrono.start (); - - //Lang &lang= Lang::getInstance(); - - // Tech List - if (keyTechScrollBar.getElementCount () != 0) - { - for (int i = keyTechScrollBar.getVisibleStart (); - i <= keyTechScrollBar.getVisibleEnd (); ++i) - { - if (i >= (int) keyTechButtons.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "i >= keyTechButtons.size(), i = %d, keyTechButtons.size() = %d", - i, (int) keyTechButtons.size ()); - throw megaglest_runtime_error (szBuf); - } - - keyTechButtons[i]->setY (keyButtonsYBase - keyButtonsLineHeight * (i - - - keyTechScrollBar. - getVisibleStart - ())); - labelsTech[i]->setY (keyButtonsYBase - - keyButtonsLineHeight * (i - - keyTechScrollBar. - getVisibleStart ())); - } - } - - // Tileset List - if (keyTilesetScrollBar.getElementCount () != 0) - { - for (int i = keyTilesetScrollBar.getVisibleStart (); - i <= keyTilesetScrollBar.getVisibleEnd (); ++i) - { - if (i >= (int) keyTilesetButtons.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "i >= keyTilesetButtons.size(), i = %d, keyTilesetButtons.size() = %d", - i, (int) keyTilesetButtons.size ()); - throw megaglest_runtime_error (szBuf); - } - - int yPos = keyButtonsYBase - keyButtonsLineHeight * - (i - keyTilesetScrollBar.getVisibleStart ()); - keyTilesetButtons[i]->setY (yPos); - } - } - - // Map List - if (keyMapScrollBar.getElementCount () != 0) - { - for (int i = keyMapScrollBar.getVisibleStart (); - i <= keyMapScrollBar.getVisibleEnd (); ++i) - { - if (i >= (int) keyMapButtons.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "i >= keyMapButtons.size(), i = %d, keyMapButtons.size() = %d", - i, (int) keyMapButtons.size ()); - throw megaglest_runtime_error (szBuf); - } - - keyMapButtons[i]->setY (keyButtonsYBase - keyButtonsLineHeight * (i - - - keyMapScrollBar. - getVisibleStart - ())); - labelsMap[i]->setY (keyButtonsYBase - - keyButtonsLineHeight * (i - - keyMapScrollBar. - getVisibleStart ())); - } - } - - // Scenario List - if (keyScenarioScrollBar.getElementCount () != 0) - { - for (int i = keyScenarioScrollBar.getVisibleStart (); - i <= keyScenarioScrollBar.getVisibleEnd (); ++i) - { - if (i >= (int) keyScenarioButtons.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "i >= keyScenarioButtons.size(), i = %d, keyScenarioButtons.size() = %d", - i, (int) keyScenarioButtons.size ()); - throw megaglest_runtime_error (szBuf); - } - - int yPos = keyButtonsYBase - keyButtonsLineHeight * - (i - keyScenarioScrollBar.getVisibleStart ()); - keyScenarioButtons[i]->setY (yPos); - } - } - - console.update (); - } - - void MenuStateMods::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //if(key == configKeys.getCharKey("ShowFullConsole")) { - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) == - true) - { - showFullConsole = true; - } - } - - void MenuStateMods::keyPress (SDL_KeyboardEvent c) - { - } - - void MenuStateMods::keyUp (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //if(key== configKeys.getCharKey("ShowFullConsole")) { - if (isKeyPressed (configKeys.getSDLKey ("ShowFullConsole"), key) == - true) - { - showFullConsole = false; - } - } - - void MenuStateMods::showMessageBox (const string & text, - const string & header, bool toggle) - { - if (toggle == false) - { - mainMessageBox.setEnabled (false); - } - - if (mainMessageBox.getEnabled () == false) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void MenuStateMods::FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType type, - pair < FTP_Client_ResultType, - string > result, - void *userdata) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - if (type == ftp_cct_DownloadProgress) - { - FTPClientCallbackInterface::FtpProgressStats * stats = - (FTPClientCallbackInterface::FtpProgressStats *) userdata; - if (stats != NULL) - { - int fileProgress = 0; - if (stats->download_total > 0) - { - fileProgress = - ((stats->download_now / stats->download_total) * 100.0); - } - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - pair < int, string > lastProgress = fileFTPProgressList[itemName]; - fileFTPProgressList[itemName] = - pair < int, string > (fileProgress, stats->currentFilename); - safeMutexFTPProgress.ReleaseLock (); - } - } - else if (type == ftp_cct_ExtractProgress) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP extract Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - //printf("Got FTP extract Callback for [%s] result = %d [%s]\n",itemName.c_str(),result.first,result.second.c_str()); - - if (userdata == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("DataMissingExtractDownloadMod").c_str (), - itemName.c_str ()); - //printf("%s\n",szBuf); - console.addLine (szBuf, true); - } - else - { - char *szBuf = (char *) userdata; - //printf("%s\n",szBuf); - console.addLine (szBuf); - } - } - else if (type == ftp_cct_File) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (); - - //printf("### downloaded file [%s] result = %d\n",itemName.c_str(),result.first); - - if (result.first == ftp_crt_SUCCESS) - { - displayModPreviewImage[itemName] = true; - } - } - - else if (type == ftp_cct_Map) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (); - selectedMapName = ""; - buttonInstallMap.setEnabled (true); - - if (result.first == ftp_crt_SUCCESS) - { - refreshMaps (); - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadMapSuccess").c_str (), - itemName.c_str ()); - console.addLine (szBuf, true); - } - else - { - curl_version_info_data *curlVersion = - curl_version_info (CURLVERSION_NOW); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadMapFail").c_str (), - itemName.c_str (), curlVersion->version, - result.second.c_str ()); - console.addLine (szBuf, true); - } - } - else if (type == ftp_cct_Tileset) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (true); - - selectedTilesetName = ""; - buttonInstallTileset.setEnabled (true); - - if (result.first == ftp_crt_SUCCESS) - { - refreshTilesets (); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadTilesetSuccess").c_str (), - itemName.c_str ()); - console.addLine (szBuf, true); - // END - } - else - { - curl_version_info_data *curlVersion = - curl_version_info (CURLVERSION_NOW); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadTilesetFail").c_str (), - itemName.c_str (), curlVersion->version, - result.second.c_str ()); - console.addLine (szBuf, true); - } - } - else if (type == ftp_cct_Techtree) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (true); - - selectedTechName = ""; - buttonInstallTech.setEnabled (true); - - if (result.first == ftp_crt_SUCCESS) - { - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadTechSuccess").c_str (), - itemName.c_str ()); - console.addLine (szBuf, true); - - // START - // Clear the CRC Cache if it is populated - safeMutexFTPProgress.Lock (); - Checksum::clearFileCache (); - - vector < string > paths = - Config::getInstance ().getPathListForType (ptTechs); - string pathSearchString = string ("/") + itemName + string ("/*"); - const string filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - - // Refresh CRC - Config & config = Config::getInstance (); - getFolderTreeContentsCheckSumRecursively (config. - getPathListForType - (ptTechs, ""), - string ("/") + itemName + - string ("/*"), ".xml", - NULL, true); - safeMutexFTPProgress.ReleaseLock (); - - refreshTechs (); - // END - } - else - { - curl_version_info_data *curlVersion = - curl_version_info (CURLVERSION_NOW); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadTechFail").c_str (), - itemName.c_str (), curlVersion->version, - result.second.c_str ()); - console.addLine (szBuf, true); - } - } - else if (type == ftp_cct_Scenario) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (true); - - selectedScenarioName = ""; - buttonInstallScenario.setEnabled (true); - - if (result.first == ftp_crt_SUCCESS) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadScenarioSuccess").c_str (), - itemName.c_str ()); - console.addLine (szBuf, true); - - // START - // Clear the CRC Cache if it is populated - // - // Clear the CRC file Cache - safeMutexFTPProgress.Lock (); - Checksum::clearFileCache (); - - vector < string > paths = - Config::getInstance ().getPathListForType (ptScenarios); - string pathSearchString = string ("/") + itemName + string ("/*"); - const string filterFileExt = ".xml"; - clearFolderTreeContentsCheckSum (paths, pathSearchString, - filterFileExt); - clearFolderTreeContentsCheckSumList (paths, pathSearchString, - filterFileExt); - - // Refresh CRC - Config & config = Config::getInstance (); - getFolderTreeContentsCheckSumRecursively (config. - getPathListForType - (ptScenarios, ""), - string ("/") + itemName + - string ("/*"), ".xml", - NULL, true); - safeMutexFTPProgress.ReleaseLock (); - - refreshScenarios (); - // END - } - else - { - curl_version_info_data *curlVersion = - curl_version_info (CURLVERSION_NOW); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("ModDownloadScenarioFail").c_str (), - itemName.c_str (), curlVersion->version, - result.second.c_str ()); - console.addLine (szBuf, true); - } - } - - } - -}} //end namespace +} //end namespace diff --git a/source/glest_game/menu/menu_state_mods.h b/source/glest_game/menu/menu_state_mods.h index a83d2f29b..6d137fe54 100644 --- a/source/glest_game/menu/menu_state_mods.h +++ b/source/glest_game/menu/menu_state_mods.h @@ -19,212 +19,208 @@ # include # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - enum FTPMessageType - { - ftpmsg_None, - ftpmsg_GetMap, - ftpmsg_GetTileset, - ftpmsg_GetTechtree, - ftpmsg_GetScenario, + enum FTPMessageType { + ftpmsg_None, + ftpmsg_GetMap, + ftpmsg_GetTileset, + ftpmsg_GetTechtree, + ftpmsg_GetScenario, - ftpmsg_ReplaceMap, - ftpmsg_ReplaceTileset, - ftpmsg_ReplaceTechtree, - ftpmsg_ReplaceScenario, + ftpmsg_ReplaceMap, + ftpmsg_ReplaceTileset, + ftpmsg_ReplaceTechtree, + ftpmsg_ReplaceScenario, - ftpmsg_Quit - }; + ftpmsg_Quit + }; - enum ModMenuState - { - mmst_None, - mmst_Loading, - mmst_CalculatingCRC - }; + enum ModMenuState { + mmst_None, + mmst_Loading, + mmst_CalculatingCRC + }; - typedef vector < GraphicButton * >UserButtons; - typedef vector < GraphicLabel * >GraphicLabels; + typedef vector < GraphicButton * >UserButtons; + typedef vector < GraphicLabel * >GraphicLabels; -// =============================== -// class MenuStateMods -// =============================== - class MenuStateMods:public MenuState, public FTPClientCallbackInterface, - public SimpleTaskCallbackInterface - { - private: + // =============================== + // class MenuStateMods + // =============================== + class MenuStateMods :public MenuState, public FTPClientCallbackInterface, + public SimpleTaskCallbackInterface { + private: - GraphicButton buttonReturn; - GraphicLine lineHorizontal; - GraphicLine lineVertical; - GraphicLine lineReturn; - GraphicLine lineVerticalReturn; + GraphicButton buttonReturn; + GraphicLine lineHorizontal; + GraphicLine lineVertical; + GraphicLine lineReturn; + GraphicLine lineVerticalReturn; - GraphicMessageBox mainMessageBox; - FTPMessageType mainMessageBoxState; + GraphicMessageBox mainMessageBox; + FTPMessageType mainMessageBoxState; - int techInfoXPos; - int mapInfoXPos; - int tilesetInfoXPos; - int scenarioInfoXPos; - int labelWidth; - int scrollListsYPos; - int listBoxLength; + int techInfoXPos; + int mapInfoXPos; + int tilesetInfoXPos; + int scenarioInfoXPos; + int labelWidth; + int scrollListsYPos; + int listBoxLength; - GraphicButton buttonInstallTech; - GraphicButton buttonRemoveTech; - GraphicLabel keyTechScrollBarTitle1; - GraphicLabel keyTechScrollBarTitle2; - GraphicScrollBar keyTechScrollBar; - UserButtons keyTechButtons; - GraphicLabels labelsTech; + GraphicButton buttonInstallTech; + GraphicButton buttonRemoveTech; + GraphicLabel keyTechScrollBarTitle1; + GraphicLabel keyTechScrollBarTitle2; + GraphicScrollBar keyTechScrollBar; + UserButtons keyTechButtons; + GraphicLabels labelsTech; - GraphicButton buttonInstallTileset; - GraphicButton buttonRemoveTileset; - GraphicLabel keyTilesetScrollBarTitle1; - GraphicScrollBar keyTilesetScrollBar; - UserButtons keyTilesetButtons; + GraphicButton buttonInstallTileset; + GraphicButton buttonRemoveTileset; + GraphicLabel keyTilesetScrollBarTitle1; + GraphicScrollBar keyTilesetScrollBar; + UserButtons keyTilesetButtons; - GraphicButton buttonInstallMap; - GraphicButton buttonRemoveMap; - GraphicLabel keyMapScrollBarTitle1; - GraphicLabel keyMapScrollBarTitle2; - GraphicScrollBar keyMapScrollBar; - UserButtons keyMapButtons; - GraphicLabels labelsMap; + GraphicButton buttonInstallMap; + GraphicButton buttonRemoveMap; + GraphicLabel keyMapScrollBarTitle1; + GraphicLabel keyMapScrollBarTitle2; + GraphicScrollBar keyMapScrollBar; + UserButtons keyMapButtons; + GraphicLabels labelsMap; - GraphicButton buttonInstallScenario; - GraphicButton buttonRemoveScenario; - GraphicLabel keyScenarioScrollBarTitle1; - GraphicLabel keyScenarioScrollBarTitle2; - GraphicScrollBar keyScenarioScrollBar; - UserButtons keyScenarioButtons; + GraphicButton buttonInstallScenario; + GraphicButton buttonRemoveScenario; + GraphicLabel keyScenarioScrollBarTitle1; + GraphicLabel keyScenarioScrollBarTitle2; + GraphicScrollBar keyScenarioScrollBar; + UserButtons keyScenarioButtons; - GraphicLabel modDescrLabel; - Texture2D *modPreviewImage; - ModInfo *modInfoSelected; - map < string, bool > displayModPreviewImage; + GraphicLabel modDescrLabel; + Texture2D *modPreviewImage; + ModInfo *modInfoSelected; + map < string, bool > displayModPreviewImage; - GraphicButton buttonInstalled; - GraphicButton buttonAvailable; - GraphicButton buttonConflict; - GraphicButton buttonOnlyLocal; + GraphicButton buttonInstalled; + GraphicButton buttonAvailable; + GraphicButton buttonConflict; + GraphicButton buttonOnlyLocal; - GraphicLabel pleaseWaitLabel; - ModMenuState modMenuState; - ModMenuState oldMenuState; + GraphicLabel pleaseWaitLabel; + ModMenuState modMenuState; + ModMenuState oldMenuState; - bool enableMapPreview; - bool validMapPreview; - MapInfo mapInfo; - MapPreview mapPreview; - Texture2D *mapPreviewTexture; + bool enableMapPreview; + bool validMapPreview; + MapInfo mapInfo; + MapPreview mapPreview; + Texture2D *mapPreviewTexture; - int keyButtonsToRender; - int keyButtonsYBase; - int keyButtonsXBase; - int keyButtonsLineHeight; - int keyButtonsHeight; - int keyButtonsWidth; - int keyButtonsWidthTech; - int keyButtonsWidthMap; - int keyButtonsWidthTil; - int keyButtonsWidthScen; + int keyButtonsToRender; + int keyButtonsYBase; + int keyButtonsXBase; + int keyButtonsLineHeight; + int keyButtonsHeight; + int keyButtonsWidth; + int keyButtonsWidthTech; + int keyButtonsWidthMap; + int keyButtonsWidthTil; + int keyButtonsWidthScen; - //Console console; - bool showFullConsole; + //Console console; + bool showFullConsole; - string selectedTechName; - std::vector < std::string > techListRemote; - std::map < string, ModInfo > techCacheList; - vector < string > techTreeFiles; - vector < string > techTreeFilesUserData; + string selectedTechName; + std::vector < std::string > techListRemote; + std::map < string, ModInfo > techCacheList; + vector < string > techTreeFiles; + vector < string > techTreeFilesUserData; - string selectedTilesetName; - std::vector < std::string > tilesetListRemote; - std::map < string, ModInfo > tilesetCacheList; - vector < string > tilesetFiles; - vector < string > tilesetFilesUserData; + string selectedTilesetName; + std::vector < std::string > tilesetListRemote; + std::map < string, ModInfo > tilesetCacheList; + vector < string > tilesetFiles; + vector < string > tilesetFilesUserData; - string selectedMapName; - std::vector < std::string > mapListRemote; - std::map < string, ModInfo > mapCacheList; - vector < string > mapFiles; - vector < string > mapFilesUserData; + string selectedMapName; + std::vector < std::string > mapListRemote; + std::map < string, ModInfo > mapCacheList; + vector < string > mapFiles; + vector < string > mapFilesUserData; - string selectedScenarioName; - std::vector < std::string > scenarioListRemote; - std::map < string, ModInfo > scenarioCacheList; - vector < string > scenarioFiles; - vector < string > scenarioFilesUserData; + string selectedScenarioName; + std::vector < std::string > scenarioListRemote; + std::map < string, ModInfo > scenarioCacheList; + vector < string > scenarioFiles; + vector < string > scenarioFilesUserData; - FTPClientThread *ftpClientThread; - std::map < string, pair < int, string > >fileFTPProgressList; + FTPClientThread *ftpClientThread; + std::map < string, pair < int, string > >fileFTPProgressList; - SimpleTaskThread *modHttpServerThread; + SimpleTaskThread *modHttpServerThread; - void getTechsLocalList (); - string refreshTechModInfo (string techInfo); - void refreshTechs (); + void getTechsLocalList(); + string refreshTechModInfo(string techInfo); + void refreshTechs(); - void getTilesetsLocalList (); - string refreshTilesetModInfo (string tilesetInfo); - void refreshTilesets (); + void getTilesetsLocalList(); + string refreshTilesetModInfo(string tilesetInfo); + void refreshTilesets(); - void getMapsLocalList (); - string refreshMapModInfo (string mapInfo); - void refreshMaps (); - string getMapCRC (string mapName); + void getMapsLocalList(); + string refreshMapModInfo(string mapInfo); + void refreshMaps(); + string getMapCRC(string mapName); - void getScenariosLocalList (); - string refreshScenarioModInfo (string scenarioInfo); - void refreshScenarios (); + void getScenariosLocalList(); + string refreshScenarioModInfo(string scenarioInfo); + void refreshScenarios(); - void showLocalDescription (string name); - void loadMapPreview (string name); - void showRemoteDesription (ModInfo * modInfo); + void showLocalDescription(string name); + void loadMapPreview(string name); + void showRemoteDesription(ModInfo * modInfo); - std::map < string, bool > mapCRCUpdateList; - public: + std::map < string, bool > mapCRCUpdateList; + public: - MenuStateMods (Program * program, MainMenu * mainMenu); - virtual ~ MenuStateMods (); + MenuStateMods(Program * program, MainMenu * mainMenu); + virtual ~MenuStateMods(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseUp (int x, int y, const MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - void update (); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseUp(int x, int y, const MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + void update(); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - virtual void keyUp (SDL_KeyboardEvent key); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + virtual void keyUp(SDL_KeyboardEvent key); - virtual void simpleTask (BaseThread * callingThread, void *userdata); + virtual void simpleTask(BaseThread * callingThread, void *userdata); - virtual void reloadUI (); + virtual void reloadUI(); - private: + private: - void cleanUp (); - MapInfo loadMapInfo (string file); - void showMessageBox (const string & text, const string & header, - bool toggle); - void clearUserButtons (); - virtual void FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType type, - pair < FTP_Client_ResultType, - string > result, void *userdata); + void cleanUp(); + MapInfo loadMapInfo(string file); + void showMessageBox(const string & text, const string & header, + bool toggle); + void clearUserButtons(); + virtual void FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType type, + pair < FTP_Client_ResultType, + string > result, void *userdata); - string getPreviewImageFileForMod (const ModInfo * modInfo); - void cleanupPreviewTexture (); - void cleanupMapPreviewTexture (); - }; + string getPreviewImageFileForMod(const ModInfo * modInfo); + void cleanupPreviewTexture(); + void cleanupMapPreviewTexture(); + }; -}} //end namespace + } +} //end namespace #endif /* MENU_STATE_MODS_H_ */ diff --git a/source/glest_game/menu/menu_state_new_game.cpp b/source/glest_game/menu/menu_state_new_game.cpp index 6d430fb12..2804090f0 100644 --- a/source/glest_game/menu/menu_state_new_game.cpp +++ b/source/glest_game/menu/menu_state_new_game.cpp @@ -28,169 +28,147 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class MenuStateNewGame -// ===================================================== + // ===================================================== + // class MenuStateNewGame + // ===================================================== - MenuStateNewGame::MenuStateNewGame (Program * program, - MainMenu * - mainMenu):MenuState (program, - mainMenu, "root") - { - containerName = "NewGame"; - Lang & lang = Lang::getInstance (); + MenuStateNewGame::MenuStateNewGame(Program * program, + MainMenu * + mainMenu) :MenuState(program, + mainMenu, "root") { + containerName = "NewGame"; + Lang & lang = Lang::getInstance(); - int buttonWidth = 200; - int buttonXPosition = (1000 - buttonWidth) / 2; - int yPos = 465; - buttonTutorial.registerGraphicComponent (containerName, - "buttonTutorial"); - buttonTutorial.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - buttonScenario.registerGraphicComponent (containerName, - "buttonScenario"); - buttonScenario.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - buttonCustomGame.registerGraphicComponent (containerName, - "buttonCustomGame"); - buttonCustomGame.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - buttonMasterserverGame.registerGraphicComponent (containerName, - "buttonMasterserverGame"); - buttonMasterserverGame.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - buttonJoinGame.registerGraphicComponent (containerName, - "buttonJoinGame"); - buttonJoinGame.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (buttonXPosition, yPos, buttonWidth); + int buttonWidth = 200; + int buttonXPosition = (1000 - buttonWidth) / 2; + int yPos = 465; + buttonTutorial.registerGraphicComponent(containerName, + "buttonTutorial"); + buttonTutorial.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + buttonScenario.registerGraphicComponent(containerName, + "buttonScenario"); + buttonScenario.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + buttonCustomGame.registerGraphicComponent(containerName, + "buttonCustomGame"); + buttonCustomGame.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + buttonMasterserverGame.registerGraphicComponent(containerName, + "buttonMasterserverGame"); + buttonMasterserverGame.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + buttonJoinGame.registerGraphicComponent(containerName, + "buttonJoinGame"); + buttonJoinGame.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(buttonXPosition, yPos, buttonWidth); - buttonCustomGame.setText (lang.getString ("CustomGame")); - buttonScenario.setText (lang.getString ("Scenario")); - buttonJoinGame.setText (lang.getString ("JoinGame")); - buttonMasterserverGame.setText (lang.getString ("JoinInternetGame")); - buttonTutorial.setText (lang.getString ("Tutorial")); - buttonReturn.setText (lang.getString ("Return")); + buttonCustomGame.setText(lang.getString("CustomGame")); + buttonScenario.setText(lang.getString("Scenario")); + buttonJoinGame.setText(lang.getString("JoinGame")); + buttonMasterserverGame.setText(lang.getString("JoinInternetGame")); + buttonTutorial.setText(lang.getString("Tutorial")); + buttonReturn.setText(lang.getString("Return")); - GraphicComponent::applyAllCustomProperties (containerName); + GraphicComponent::applyAllCustomProperties(containerName); - NetworkManager::getInstance ().end (); - } + NetworkManager::getInstance().end(); + } - void MenuStateNewGame::reloadUI () - { - Lang & lang = Lang::getInstance (); + void MenuStateNewGame::reloadUI() { + Lang & lang = Lang::getInstance(); - buttonCustomGame.setText (lang.getString ("CustomGame")); - buttonScenario.setText (lang.getString ("Scenario")); - buttonJoinGame.setText (lang.getString ("JoinGame")); - buttonMasterserverGame.setText (lang.getString ("JoinInternetGame")); - buttonTutorial.setText (lang.getString ("Tutorial")); - buttonReturn.setText (lang.getString ("Return")); + buttonCustomGame.setText(lang.getString("CustomGame")); + buttonScenario.setText(lang.getString("Scenario")); + buttonJoinGame.setText(lang.getString("JoinGame")); + buttonMasterserverGame.setText(lang.getString("JoinInternetGame")); + buttonTutorial.setText(lang.getString("Tutorial")); + buttonReturn.setText(lang.getString("Return")); - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } - void MenuStateNewGame::mouseClick (int x, int y, MouseButton mouseButton) - { + void MenuStateNewGame::mouseClick(int x, int y, MouseButton mouseButton) { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); - if (buttonCustomGame.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateCustomGame (program, mainMenu)); - } - else if (buttonScenario.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateScenario (program, mainMenu, false, - Config::getInstance (). - getPathListForType - (ptScenarios))); - } - else if (buttonJoinGame.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateJoinGame (program, mainMenu)); - } - else if (buttonMasterserverGame.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateMasterserver (program, mainMenu)); - } - else if (buttonTutorial.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateScenario (program, mainMenu, true, - Config::getInstance (). - getPathListForType - (ptTutorials))); - } - else if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - } - } + if (buttonCustomGame.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateCustomGame(program, mainMenu)); + } else if (buttonScenario.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateScenario(program, mainMenu, false, + Config::getInstance(). + getPathListForType + (ptScenarios))); + } else if (buttonJoinGame.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateJoinGame(program, mainMenu)); + } else if (buttonMasterserverGame.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateMasterserver(program, mainMenu)); + } else if (buttonTutorial.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateScenario(program, mainMenu, true, + Config::getInstance(). + getPathListForType + (ptTutorials))); + } else if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + } + } - void MenuStateNewGame::mouseMove (int x, int y, const MouseState * ms) - { - buttonCustomGame.mouseMove (x, y); - buttonScenario.mouseMove (x, y); - buttonJoinGame.mouseMove (x, y); - buttonMasterserverGame.mouseMove (x, y); - buttonTutorial.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - } + void MenuStateNewGame::mouseMove(int x, int y, const MouseState * ms) { + buttonCustomGame.mouseMove(x, y); + buttonScenario.mouseMove(x, y); + buttonJoinGame.mouseMove(x, y); + buttonMasterserverGame.mouseMove(x, y); + buttonTutorial.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + } - void MenuStateNewGame::render () - { - Renderer & renderer = Renderer::getInstance (); + void MenuStateNewGame::render() { + Renderer & renderer = Renderer::getInstance(); - renderer.renderButton (&buttonCustomGame); - renderer.renderButton (&buttonScenario); - renderer.renderButton (&buttonJoinGame); - renderer.renderButton (&buttonMasterserverGame); - renderer.renderButton (&buttonTutorial); - renderer.renderButton (&buttonReturn); + renderer.renderButton(&buttonCustomGame); + renderer.renderButton(&buttonScenario); + renderer.renderButton(&buttonJoinGame); + renderer.renderButton(&buttonMasterserverGame); + renderer.renderButton(&buttonTutorial); + renderer.renderButton(&buttonReturn); - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } - void MenuStateNewGame::update () - { - if (Config::getInstance ().getBool ("AutoTest")) - { - AutoTest::getInstance ().updateNewGame (program, mainMenu); - return; - } - console.update (); - } + void MenuStateNewGame::update() { + if (Config::getInstance().getBool("AutoTest")) { + AutoTest::getInstance().updateNewGame(program, mainMenu); + return; + } + console.update(); + } - void MenuStateNewGame::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //if(key == configKeys.getCharKey("SaveGUILayout")) { - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } - } + void MenuStateNewGame::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //if(key == configKeys.getCharKey("SaveGUILayout")) { + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + } - } + } } //end namespace diff --git a/source/glest_game/menu/menu_state_new_game.h b/source/glest_game/menu/menu_state_new_game.h index 71fe59c4a..42498d099 100644 --- a/source/glest_game/menu/menu_state_new_game.h +++ b/source/glest_game/menu/menu_state_new_game.h @@ -15,38 +15,36 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateNewGame -// =============================== + // =============================== + // class MenuStateNewGame + // =============================== - class MenuStateNewGame:public MenuState - { - private: - GraphicButton buttonCustomGame; - GraphicButton buttonScenario; - GraphicButton buttonJoinGame; - GraphicButton buttonMasterserverGame; - GraphicButton buttonTutorial; - GraphicButton buttonReturn; + class MenuStateNewGame :public MenuState { + private: + GraphicButton buttonCustomGame; + GraphicButton buttonScenario; + GraphicButton buttonJoinGame; + GraphicButton buttonMasterserverGame; + GraphicButton buttonTutorial; + GraphicButton buttonReturn; - public: - MenuStateNewGame (Program * program, MainMenu * mainMenu); + public: + MenuStateNewGame(Program * program, MainMenu * mainMenu); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void update (); - void render (); - virtual void keyDown (SDL_KeyboardEvent key); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void update(); + void render(); + virtual void keyDown(SDL_KeyboardEvent key); - void reloadUI (); - }; + void reloadUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_options.cpp b/source/glest_game/menu/menu_state_options.cpp index 022fac0bd..7d79dd8ac 100644 --- a/source/glest_game/menu/menu_state_options.cpp +++ b/source/glest_game/menu/menu_state_options.cpp @@ -29,1497 +29,1357 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class MenuStateOptions -// ===================================================== - MenuStateOptions::MenuStateOptions (Program * program, - MainMenu * mainMenu, - ProgramState ** parentUI): - MenuState (program, mainMenu, "config"), - buttonOk ("Options", "buttonOk"), - buttonReturn ("Options", "buttonReturn"), - labelLang ("Options", "labelLang"), - listBoxLang ("Options", "listBoxLang"), - labelPlayerName ("Options", "labelPlayerName"), - labelPlayerNameLabel ("Options", "labelPlayerNameLabel"), - buttonKeyboardSetup ("Options", "buttonKeyboardSetup"), - buttonVideoSection ("Options", "buttonVideoSection"), - buttonAudioSection ("Options", "buttonAudioSection"), - buttonMiscSection ("Options", "buttonMiscSection"), - buttonNetworkSettings ("Options", "buttonNetworkSettings"), - labelFontSizeAdjustment ("Options", "labelFontSizeAdjustment"), - listFontSizeAdjustment ("Options", "listFontSizeAdjustment"), - mainMessageBox ("Options", "mainMessageBox"), - labelScreenShotType ("Options", "labelScreenShotType"), - listBoxScreenShotType ("Options", "listBoxScreenShotType"), - labelDisableScreenshotConsoleText ("Options", - "labelDisableScreenshotConsoleText"), - checkBoxDisableScreenshotConsoleText ("Options", - "checkBoxDisableScreenshotConsoleText"), - labelMouseMoveScrollsWorld ("Options", "labelMouseMoveScrollsWorld"), - checkBoxMouseMoveScrollsWorld ("Options", - "checkBoxMouseMoveScrollsWorld"), - labelCameraMoveSpeed ("Options", "labelCameraMoveSpeed"), - listCameraMoveSpeed ("Options", "listCameraMoveSpeed"), - labelVisibleHud ("Options", "labelVisibleHud"), - checkBoxVisibleHud ("Options", "checkBoxVisibleHud"), - labelHealthBars ("Options", "labelHealthBars"), - listBoxHealthBars ("Options", "listBoxHealthBars"), - labelTimeDisplay ("Options", "labelTimeDisplay"), - checkBoxTimeDisplay ("Options", "checkBoxTimeDisplay"), - labelChatStaysActive ("Options", "labelChatStaysActive"), - checkBoxChatStaysActive ("Options", "checkBoxChatStaysActive"), - labelLuaDisableSecuritySandbox ("Options", - "labelLuaDisableSecuritySandbox"), - checkBoxLuaDisableSecuritySandbox ("Options", - "checkBoxLuaDisableSecuritySandbox"), - luaMessageBox ("Options", "luaMessageBox"), - labelCustomTranslation ("Options", "labelCustomTranslation"), - checkBoxCustomTranslation ("Options", "checkBoxCustomTranslation"), - buttonGetNewLanguageFiles ("Options", "buttonGetNewLanguageFiles"), - buttonDeleteNewLanguageFiles ("Options", "buttonDeleteNewLanguageFiles"), - labelTransifexUserLabel ("Options", "labelTransifexUserLabel"), - labelTransifexUser ("Options", "labelTransifexUser"), - labelTransifexPwdLabel ("Options", "labelTransifexPwdLabel"), - labelTransifexPwd ("Options", "labelTransifexPwd"), - labelTransifexI18NLabel ("Options", "labelTransifexI18NLabel"), - labelTransifexI18N ("Options", "labelTransifexI18N") - { - try - { - containerName = "Options"; - this-> - parentUI = parentUI; - Lang & - lang = Lang::getInstance (); - Config & - config = Config::getInstance (); - - this-> - console. - setOnlyChatMessagesInStoredLines (false); - activeInputLabel = NULL; - - int - leftLabelStart = 100; - int - leftColumnStart = leftLabelStart + 300; - int - buttonRowPos = 50; - int - buttonStartPos = 170; - int - lineOffset = 30; - int - tabButtonWidth = 200; - int - tabButtonHeight = 30; - - mainMessageBox. - init (lang.getString ("Ok")); - mainMessageBox. - setEnabled (false); - mainMessageBoxState = 0; - - buttonAudioSection. - init (0, 720, tabButtonWidth, tabButtonHeight); - buttonAudioSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonAudioSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonAudioSection. - setText (lang.getString ("Audio")); - // Video Section - buttonVideoSection. - init (200, 720, tabButtonWidth, tabButtonHeight); - buttonVideoSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonVideoSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonVideoSection. - setText (lang.getString ("Video")); - //MiscSection - buttonMiscSection. - init (400, 700, tabButtonWidth, tabButtonHeight + 20); - buttonMiscSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonMiscSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonMiscSection. - setText (lang.getString ("Misc")); - //NetworkSettings - buttonNetworkSettings. - init (600, 720, tabButtonWidth, tabButtonHeight); - buttonNetworkSettings. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonNetworkSettings. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonNetworkSettings. - setText (lang.getString ("Network")); - - //KeyboardSetup - buttonKeyboardSetup. - init (800, 720, tabButtonWidth, tabButtonHeight); - buttonKeyboardSetup. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonKeyboardSetup. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonKeyboardSetup. - setText (lang.getString ("Keyboardsetup")); - - int - currentLine = 650; // reset line pos - int - currentLabelStart = leftLabelStart; // set to right side - int - currentColumnStart = leftColumnStart; // set to right side - - //lang - labelLang. - init (currentLabelStart, currentLine); - labelLang. - setText (lang.getString ("Language")); - - listBoxLang. - init (currentColumnStart, currentLine, 375); - vector < - string > - langResults; - - languageList = Lang::getInstance ().getDiscoveredLanguageList (true); - for (map < string, string >::iterator iterMap = languageList.begin (); - iterMap != languageList.end (); ++iterMap) - { - langResults.push_back (iterMap->first + "-" + iterMap->second); - } - - listBoxLang. - setItems (langResults); - - pair < - string, - string > - defaultLang = - Lang::getInstance ().getNavtiveNameFromLanguageName (config. - getString - ("Lang")); - if (defaultLang.first == "" && defaultLang.second == "") - { - defaultLang = - Lang::getInstance (). - getNavtiveNameFromLanguageName (Lang::getInstance (). - getDefaultLanguage ()); - } - listBoxLang. - setSelectedItem (defaultLang.second + "-" + defaultLang.first); - currentLine -= lineOffset; - - //playerName - labelPlayerNameLabel.init (currentLabelStart, currentLine); - labelPlayerNameLabel.setText (lang.getString ("Playername")); - - labelPlayerName.init (currentColumnStart, currentLine); - labelPlayerName.setText (config. - getString ("NetPlayerName", - Socket::getHostName ().c_str ())); - labelPlayerName.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelPlayerName.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - labelPlayerName.setEditable (true); - labelPlayerName.setMaxEditWidth (16); - labelPlayerName.setMaxEditRenderWidth (200); - currentLine -= lineOffset; - - //FontSizeAdjustment - labelFontSizeAdjustment.init (currentLabelStart, currentLine); - labelFontSizeAdjustment.setText (lang. - getString ("FontSizeAdjustment")); - - listFontSizeAdjustment.init (currentColumnStart, currentLine, 80); - for (int i = -5; i <= 5; i += 1) - { - listFontSizeAdjustment.pushBackItem (intToStr (i)); - } - listFontSizeAdjustment. - setSelectedItem (intToStr (config.getInt ("FontSizeAdjustment"))); - - currentLine -= lineOffset; - // Screenshot type flag - labelScreenShotType.init (currentLabelStart, currentLine); - labelScreenShotType.setText (lang.getString ("ScreenShotFileType")); - - listBoxScreenShotType.init (currentColumnStart, currentLine, 80); - listBoxScreenShotType.pushBackItem ("bmp"); - listBoxScreenShotType.pushBackItem ("jpg"); - listBoxScreenShotType.pushBackItem ("png"); - listBoxScreenShotType.pushBackItem ("tga"); - listBoxScreenShotType.setSelectedItem (config. - getString - ("ScreenShotFileType", "jpg")); - - currentLine -= lineOffset; - - labelDisableScreenshotConsoleText.init (currentLabelStart, - currentLine); - labelDisableScreenshotConsoleText.setText (lang. - getString - ("ScreenShotConsoleText")); - - checkBoxDisableScreenshotConsoleText.init (currentColumnStart, - currentLine); - checkBoxDisableScreenshotConsoleText.setValue (!config. - getBool - ("DisableScreenshotConsoleText", - "false")); - - currentLine -= lineOffset; - - labelMouseMoveScrollsWorld.init (currentLabelStart, currentLine); - labelMouseMoveScrollsWorld.setText (lang. - getString ("MouseScrollsWorld")); - - checkBoxMouseMoveScrollsWorld.init (currentColumnStart, currentLine); - checkBoxMouseMoveScrollsWorld.setValue (config. - getBool - ("MouseMoveScrollsWorld", - "true")); - currentLine -= lineOffset; - - //CameraMoveSpeed - labelCameraMoveSpeed.init (currentLabelStart, currentLine); - labelCameraMoveSpeed.setText (lang.getString ("CameraMoveSpeed")); - - listCameraMoveSpeed.init (currentColumnStart, currentLine, 80); - for (int i = 15; i <= 50; i += 5) - { - listCameraMoveSpeed.pushBackItem (intToStr (i)); - } - listCameraMoveSpeed. - setSelectedItem (intToStr - ((int) - (config.getFloat ("CameraMoveSpeed", "15")))); - currentLine -= lineOffset; - - labelVisibleHud.init (currentLabelStart, currentLine); - labelVisibleHud.setText (lang.getString ("VisibleHUD")); - - checkBoxVisibleHud.init (currentColumnStart, currentLine); - checkBoxVisibleHud.setValue (config.getBool ("VisibleHud", "true")); - - currentLine -= lineOffset; - - labelHealthBars.init (currentLabelStart, currentLine); - labelHealthBars.setText (lang.getString ("Healthbar")); - - listBoxHealthBars.init (currentColumnStart, currentLine, 375); - listBoxHealthBars.pushBackItem (lang. - getString - ("HealthbarsFactionDefault")); - listBoxHealthBars.pushBackItem (lang.getString ("HealthbarsOff")); - listBoxHealthBars.pushBackItem (lang.getString ("HealthbarsAlways")); - listBoxHealthBars.pushBackItem (lang. - getString ("HealthbarsIfNeeded")); - listBoxHealthBars.pushBackItem (lang. - getString ("HealthbarsSelected")); - listBoxHealthBars.pushBackItem (lang. - getString - ("HealthbarsSelectedOrNeeded")); - - int - hpMode = config.getInt ("HealthBarMode", "4"); - int - hpIndex = 0; - switch (hpMode) - { - case hbvUndefined: - hpIndex = 0; - break; - case hbvOff: - hpIndex = 1; - break; - case hbvAlways: - hpIndex = 2; - break; - case hbvIfNeeded: - hpIndex = 3; - break; - case hbvSelected: - hpIndex = 4; - break; - case hbvSelected | hbvIfNeeded: - hpIndex = 5; - break; - default: - hpIndex = 0; - break; - } - - listBoxHealthBars.setSelectedItemIndex (hpIndex); - - currentLine -= lineOffset; - - labelChatStaysActive.init (currentLabelStart, currentLine); - labelChatStaysActive.setText (lang.getString ("ChatStaysActive")); - - checkBoxChatStaysActive.init (currentColumnStart, currentLine); - checkBoxChatStaysActive.setValue (config. - getBool ("ChatStaysActive", - "false")); - - currentLine -= lineOffset; - - labelTimeDisplay.init (currentLabelStart, currentLine); - labelTimeDisplay.setText (lang.getString ("TimeDisplay")); - - checkBoxTimeDisplay.init (currentColumnStart, currentLine); - checkBoxTimeDisplay.setValue (config.getBool ("TimeDisplay", "true")); - - currentLine -= lineOffset; - - labelLuaDisableSecuritySandbox.init (currentLabelStart, currentLine); - labelLuaDisableSecuritySandbox.setText (lang. - getString - ("LuaDisableSecuritySandbox")); - - checkBoxLuaDisableSecuritySandbox.init (currentColumnStart, - currentLine); - checkBoxLuaDisableSecuritySandbox.setValue (config. - getBool - ("DisableLuaSandbox", - "false")); - - luaMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - luaMessageBox.setEnabled (false); - luaMessageBoxState = 0; - - currentLine -= lineOffset; - - currentLine -= lineOffset / 2; - - // buttons - buttonOk.init (buttonStartPos, buttonRowPos, 100); - buttonOk.setText (lang.getString ("Save")); - - buttonReturn.init (buttonStartPos + 110, buttonRowPos, 100); - buttonReturn.setText (lang.getString ("Return")); - - // Transifex related UI - currentLine -= lineOffset * 3; - labelCustomTranslation.init (currentLabelStart, currentLine); - labelCustomTranslation.setText (lang.getString ("CustomTranslation")); - - checkBoxCustomTranslation.init (currentColumnStart, currentLine); - checkBoxCustomTranslation.setValue (false); - currentLine -= lineOffset; - - labelTransifexUserLabel.init (currentLabelStart, currentLine); - labelTransifexUserLabel.setText (lang. - getString ("TransifexUserName")); - - labelTransifexPwdLabel.init (currentLabelStart + 260, currentLine); - labelTransifexPwdLabel.setText (lang.getString ("TransifexPwd")); - - labelTransifexI18NLabel.init (currentLabelStart + 520, currentLine); - labelTransifexI18NLabel.setText (lang.getString ("TransifexI18N")); - - currentLine -= lineOffset; - - labelTransifexUser.init (currentLabelStart, currentLine); - labelTransifexUser.setEditable (true); - labelTransifexUser.setMaxEditWidth (28); - labelTransifexUser.setMaxEditRenderWidth (250); - labelTransifexUser.setText (config. - getString ("TranslationGetURLUser", - "")); - - labelTransifexPwd.init (currentLabelStart + 260, currentLine); - labelTransifexPwd.setIsPassword (true); - labelTransifexPwd.setEditable (true); - labelTransifexPwd.setMaxEditWidth (28); - labelTransifexPwd.setMaxEditRenderWidth (250); - labelTransifexPwd.setText (config. - getString ("TranslationGetURLPassword", - "")); - - labelTransifexI18N.init (currentLabelStart + 520, currentLine); - labelTransifexI18N.setEditable (true); - labelTransifexI18N.setMaxEditWidth (6); - labelTransifexI18N.setMaxEditRenderWidth (70); - labelTransifexI18N.setText (config. - getString ("TranslationGetURLLanguage", - "en")); - currentLine -= lineOffset; - - buttonGetNewLanguageFiles.init (currentLabelStart, currentLine, 250); - buttonGetNewLanguageFiles.setText (lang. - getString - ("TransifexGetLanguageFiles")); - - buttonDeleteNewLanguageFiles.init (currentLabelStart + 260, - currentLine, 250); - buttonDeleteNewLanguageFiles.setText (lang. - getString - ("TransifexDeleteLanguageFiles")); - - setupTransifexUI (); - - GraphicComponent::applyAllCustomProperties (containerName); - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error loading options: %s\n", - __FILE__, __FUNCTION__, __LINE__, - e.what ()); - throw - megaglest_runtime_error (string ("Error loading options msg: ") + - e.what ()); - } - } - - MenuStateOptions::~MenuStateOptions () - { - } - - void - MenuStateOptions::reloadUI () - { - Lang & lang = Lang::getInstance (); - - mainMessageBox.init (lang.getString ("Ok")); - luaMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - - buttonAudioSection.setText (lang.getString ("Audio")); - buttonVideoSection.setText (lang.getString ("Video")); - buttonMiscSection.setText (lang.getString ("Misc")); - buttonNetworkSettings.setText (lang.getString ("Network")); - buttonKeyboardSetup.setText (lang.getString ("Keyboardsetup")); - - labelVisibleHud.setText (lang.getString ("VisibleHUD")); - labelHealthBars.setText (lang.getString ("Healthbar")); - labelChatStaysActive.setText (lang.getString ("ChatStaysActive")); - labelTimeDisplay.setText (lang.getString ("TimeDisplay")); - - labelLuaDisableSecuritySandbox.setText (lang. - getString - ("LuaDisableSecuritySandbox")); - labelLang.setText (lang.getString ("Language")); - labelPlayerNameLabel.setText (lang.getString ("Playername")); - labelFontSizeAdjustment.setText (lang.getString ("FontSizeAdjustment")); - labelScreenShotType.setText (lang.getString ("ScreenShotFileType")); - labelDisableScreenshotConsoleText.setText (lang. - getString - ("ScreenShotConsoleText")); - labelMouseMoveScrollsWorld.setText (lang. - getString ("MouseScrollsWorld")); - labelCameraMoveSpeed.setText (lang.getString ("CameraMoveSpeed")); - - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - - labelCustomTranslation.setText (lang.getString ("CustomTranslation")); - buttonGetNewLanguageFiles.setText (lang. - getString - ("TransifexGetLanguageFiles")); - buttonDeleteNewLanguageFiles.setText (lang. - getString - ("TransifexDeleteLanguageFiles")); - labelTransifexUserLabel.setText (lang.getString ("TransifexUserName")); - labelTransifexPwdLabel.setText (lang.getString ("TransifexPwd")); - labelTransifexI18NLabel.setText (lang.getString ("TransifexI18N")); - } - - void - MenuStateOptions::setupTransifexUI () - { - buttonGetNewLanguageFiles.setEnabled (checkBoxCustomTranslation. - getValue ()); - buttonDeleteNewLanguageFiles.setEnabled (checkBoxCustomTranslation. - getValue ()); - labelTransifexUserLabel.setEnabled (checkBoxCustomTranslation. - getValue ()); - labelTransifexUser.setEnabled (checkBoxCustomTranslation.getValue ()); - labelTransifexPwdLabel.setEnabled (checkBoxCustomTranslation. - getValue ()); - labelTransifexPwd.setEnabled (checkBoxCustomTranslation.getValue ()); - labelTransifexI18NLabel.setEnabled (checkBoxCustomTranslation. - getValue ()); - labelTransifexI18N.setEnabled (checkBoxCustomTranslation.getValue ()); - } - - void - MenuStateOptions::showMessageBox (const string & text, - const string & header, bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void - MenuStateOptions::showLuaMessageBox (const string & text, - const string & header, bool toggle) - { - if (!toggle) - { - luaMessageBox.setEnabled (false); - } - - if (!luaMessageBox.getEnabled ()) - { - luaMessageBox.setText (text); - luaMessageBox.setHeader (header); - luaMessageBox.setEnabled (true); - } - else - { - luaMessageBox.setEnabled (false); - } - } - - void - MenuStateOptions::mouseClick (int x, int y, MouseButton mouseButton) - { - - Config & config = Config::getInstance (); - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - int - button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - saveConfig (); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - } - else - { - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - } - } - else - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - } - } - } - } - else if (luaMessageBox.getEnabled ()) - { - int - button = 0; - if (luaMessageBox.mouseClick (x, y, button)) - { - checkBoxLuaDisableSecuritySandbox.setValue (false); - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - if (luaMessageBoxState == 1) - { - checkBoxLuaDisableSecuritySandbox.setValue (true); - } - } - luaMessageBox.setEnabled (false); - } - } - else if (checkBoxLuaDisableSecuritySandbox.mouseClick (x, y)) - { - if (checkBoxLuaDisableSecuritySandbox.getValue () == true) - { - checkBoxLuaDisableSecuritySandbox.setValue (false); - - luaMessageBoxState = 1; - Lang & lang = Lang::getInstance (); - showLuaMessageBox (lang. - getString ("LuaDisableSecuritySandboxWarning"), - lang.getString ("Question"), false); - } - } - else if (buttonOk.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - - string - currentFontSizeAdjustment = config.getString ("FontSizeAdjustment"); - string - selectedFontSizeAdjustment = - listFontSizeAdjustment.getSelectedItem (); - if (currentFontSizeAdjustment != selectedFontSizeAdjustment) - { - mainMessageBoxState = 1; - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("RestartNeeded"), - lang.getString ("FontSizeAdjustmentChanged"), - false); - return; - } - saveConfig (); - //mainMenu->setState(new MenuStateRoot(program, mainMenu)); - reloadUI (); - return; - } - else if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (this->parentUI != NULL) - { - *this->parentUI = NULL; - delete * - this-> - parentUI; - } - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - else if (buttonKeyboardSetup.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateKeysetup (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - //showMessageBox("Not implemented yet", "Keyboard setup", false); - return; - } - else if (buttonAudioSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsSound (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonNetworkSettings.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsNetwork (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonMiscSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateOptions(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonVideoSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (checkBoxCustomTranslation.mouseClick (x, y)) - { - setupTransifexUI (); - } - else if (buttonDeleteNewLanguageFiles.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - - setActiveInputLable (NULL); - - if (labelTransifexI18N.getText () != "") - { - Lang & lang = Lang::getInstance (); - string - language = lang.getLanguageFile (labelTransifexI18N.getText ()); - replaceAll (language, "(", ""); - replaceAll (language, ")", ""); - - if (language != "") - { - bool - foundFilesToDelete = false; - - Config & config = Config::getInstance (); - string - data_path = config.getString ("UserData_Root", ""); - if (data_path != "") - { - endPathWithSlash (data_path); - } - - if (data_path != "") - { - - string - txnURLFileListMapping = - Config::getInstance (). - getString ("TranslationGetURLFileListMapping"); - vector < string > languageFileMappings; - Tokenize (txnURLFileListMapping, languageFileMappings, "|"); - - Config & config = Config::getInstance (); - - // Cleanup Scenarios - vector < string > scenarioPaths = - config.getPathListForType (ptScenarios); - if (scenarioPaths.size () > 1) - { - string & scenarioPath = scenarioPaths[1]; - endPathWithSlash (scenarioPath); - - vector < string > scenarioList; - findDirs (scenarioPath, scenarioList, false, false); - for (unsigned int i = 0; i < scenarioList.size (); ++i) - { - string - scenario = scenarioList[i]; - - vector < string > langResults; - findAll (scenarioPath + scenario + "/*.lng", langResults, - false, false); - for (unsigned int j = 0; j < langResults.size (); ++j) - { - string - testLanguage = langResults[j]; - - string - removeLngFile = - scenarioPath + scenario + "/" + testLanguage; - - if (EndsWith (testLanguage, language + ".lng") == true) - { - - for (unsigned int k = 0; - k < languageFileMappings.size (); ++k) - { - string - mapping = languageFileMappings[k]; - replaceAll (mapping, "$language", language); - - //printf("Comparing found [%s] with [%s]\n",removeLngFile.c_str(),mapping.c_str()); - - if (EndsWith (removeLngFile, mapping) == true) - { - printf ("About to delete file [%s]\n", - removeLngFile.c_str ()); - removeFile (removeLngFile); - foundFilesToDelete = true; - break; - } - } - } - } - } - } - - // Cleanup tutorials - vector < string > tutorialPaths = - config.getPathListForType (ptTutorials); - if (tutorialPaths.size () > 1) - { - string & tutorialPath = tutorialPaths[1]; - endPathWithSlash (tutorialPath); - - vector < string > tutorialList; - findDirs (tutorialPath, tutorialList, false, false); - for (unsigned int i = 0; i < tutorialList.size (); ++i) - { - string - tutorial = tutorialList[i]; - - vector < string > langResults; - findAll (tutorialPath + tutorial + "/*.lng", langResults, - false, false); - for (unsigned int j = 0; j < langResults.size (); ++j) - { - string - testLanguage = langResults[j]; - - string - removeLngFile = - tutorialPath + tutorial + "/" + testLanguage; - if (EndsWith (testLanguage, language + ".lng") == true) - { - - - for (unsigned int k = 0; - k < languageFileMappings.size (); ++k) - { - string - mapping = languageFileMappings[k]; - replaceAll (mapping, "$language", language); - - //printf("Comparing found [%s] with [%s]\n",removeLngFile.c_str(),mapping.c_str()); - - if (EndsWith (removeLngFile, mapping) == true) - { - printf ("About to delete file [%s]\n", - removeLngFile.c_str ()); - removeFile (removeLngFile); - foundFilesToDelete = true; - break; - } - } - } - } - } - } - - // Cleanup main and hint language files - string - mainLngFile = data_path + "data/lang/" + language + ".lng"; - if (fileExists (mainLngFile) == true) - { - - for (unsigned int k = 0; k < languageFileMappings.size (); - ++k) - { - string - mapping = languageFileMappings[k]; - replaceAll (mapping, "$language", language); - - if (EndsWith (mainLngFile, mapping) == true) - { - printf ("About to delete file [%s]\n", - mainLngFile.c_str ()); - removeFile (mainLngFile); - foundFilesToDelete = true; - break; - } - } - } - - string - hintLngFile = - data_path + "data/lang/hint/hint_" + language + ".lng"; - if (fileExists (hintLngFile) == true) - { - for (unsigned int k = 0; k < languageFileMappings.size (); - ++k) - { - string - mapping = languageFileMappings[k]; - replaceAll (mapping, "$language", language); - - if (EndsWith (hintLngFile, mapping) == true) - { - printf ("About to delete file [%s]\n", - hintLngFile.c_str ()); - removeFile (hintLngFile); - foundFilesToDelete = true; - break; - } - } - } - } - - if (lang.isLanguageLocal (toLower (language)) == true) - { - lang.loadGameStrings (toLower (language)); - } - - if (foundFilesToDelete == true) - { - mainMessageBoxState = 0; - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("TransifexDeleteSuccess"), - lang.getString ("Notice"), false); - } - } - } - } - else if (buttonGetNewLanguageFiles.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - - setActiveInputLable (NULL); - - string - orig_txnURLUser = - Config::getInstance ().getString ("TranslationGetURLUser"); - //string orig_txnURLPwd = Config::getInstance().getString("TranslationGetURLPassword",""); - string - orig_txnURLLang = - Config::getInstance ().getString ("TranslationGetURLLanguage"); - - Config::getInstance ().setString ("TranslationGetURLUser", - labelTransifexUser.getText ()); - Config::getInstance ().setString ("TranslationGetURLPassword", - labelTransifexPwd.getText (), true); - Config::getInstance ().setString ("TranslationGetURLLanguage", - labelTransifexI18N.getText ()); - - bool - saveChanges = (orig_txnURLUser != labelTransifexUser.getText () || - orig_txnURLLang != labelTransifexI18N.getText ()); - - string - txnURL = Config::getInstance ().getString ("TranslationGetURL"); - string - txnURLUser = - Config::getInstance ().getString ("TranslationGetURLUser"); - string - txnURLPwd = - Config::getInstance ().getString ("TranslationGetURLPassword"); - string - txnURLLang = - Config::getInstance ().getString ("TranslationGetURLLanguage"); - string - txnURLFileList = - Config::getInstance ().getString ("TranslationGetURLFileList"); - string - txnURLFileListMapping = - Config::getInstance (). - getString ("TranslationGetURLFileListMapping"); - - string - txnURLDetails = - Config::getInstance ().getString ("TranslationGetURLDetails"); - - string - credentials = txnURLUser + ":" + txnURLPwd; - - printf ("URL1 [%s] credentials [%s]\n", txnURL.c_str (), - credentials.c_str ()); - - //txnURLUser = SystemFlags::escapeURL(txnURLUser,handle); - //replaceAll(txnURL,"$user",txnURLUser); - - //printf("URL2 [%s]\n",txnURL.c_str()); - - //txnURLPwd = SystemFlags::escapeURL(txnURLPwd,handle); - //replaceAll(txnURL,"$password",txnURLPwd); - - //printf("URL3 [%s]\n",txnURL.c_str()); - - replaceAll (txnURL, "$language", txnURLLang); - - printf ("URL4 [%s]\n", txnURL.c_str ()); - - //txnURLFileList - vector < string > languageFiles; - Tokenize (txnURLFileList, languageFiles, "|"); - - vector < string > languageFileMappings; - Tokenize (txnURLFileListMapping, languageFileMappings, "|"); - - printf ("URL5 file count = " MG_SIZE_T_SPECIFIER ", " - MG_SIZE_T_SPECIFIER " [%s]\n", languageFiles.size (), - languageFileMappings.size (), - (languageFiles.empty () == - false ? languageFiles[0].c_str () : "")); - - if (languageFiles.empty () == false) - { - - bool - gotDownloads = false; - bool - reloadLanguage = false; - string - langName = ""; - - CURL * - handle = SystemFlags::initHTTP (); - for (unsigned int i = 0; i < languageFiles.size (); ++i) - { - string - fileURL = txnURL; - replaceAll (fileURL, "$file", languageFiles[i]); - - if (langName == "") - { - // Get language name for file - string - fileURLDetails = txnURLDetails; - replaceAll (fileURLDetails, "$file", languageFiles[0]); - - printf (" i = %u Trying [%s]\n", i, fileURLDetails.c_str ()); - curl_easy_setopt (handle, CURLOPT_VERBOSE, 1); - curl_easy_setopt (handle, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt (handle, CURLOPT_USERPWD, - credentials.c_str ()); - std::string fileDataDetails = - SystemFlags::getHTTP (fileURLDetails, handle); - - // "available_languages": [ - // { - // "code_aliases": " ", - // "code": "ca", - // "name": "Catalan" - // }, - // { - // "code_aliases": " ", - // "code": "zh", - // "name": "Chinese" - // }, - // curl -i -L --user softcoder -X GET https://www.transifex.com/api/2/project/megaglest/resource/main-language-file/?details - - string - search_detail_key = "\"code\": \"" + txnURLLang + "\""; - size_t - posDetails = fileDataDetails.find (search_detail_key, 0); - if (posDetails != fileDataDetails.npos) - { - posDetails = - fileDataDetails.find ("\"name\": \"", - posDetails + - search_detail_key.length ()); - - if (posDetails != fileDataDetails.npos) - { - - size_t - posDetailsEnd = - fileDataDetails.find ("\"", posDetails + 9); - - langName = - fileDataDetails.substr (posDetails + 9, - posDetailsEnd - (posDetails + 9)); - replaceAll (langName, ",", ""); - replaceAll (langName, "\\", ""); - replaceAll (langName, "/", ""); - replaceAll (langName, "?", ""); - replaceAll (langName, ":", ""); - replaceAll (langName, "@", ""); - replaceAll (langName, "!", ""); - replaceAll (langName, "*", ""); - replaceAll (langName, "(", ""); - replaceAll (langName, ")", ""); - langName = trim (langName); - replaceAll (langName, " ", "-"); - } - - printf ("PARSED Language filename [%s]\n", langName.c_str ()); - } - } - - printf ("i = %u Trying [%s]\n", i, fileURL.c_str ()); - curl_easy_setopt (handle, CURLOPT_VERBOSE, 1); - curl_easy_setopt (handle, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt (handle, CURLOPT_USERPWD, credentials.c_str ()); - std::string fileData = SystemFlags::getHTTP (fileURL, handle); - - // "content": " - // ", - // "mimetype": "text/plain" - size_t - pos = fileData.find ("\"content\": \"", 0); - if (pos != fileData.npos) - { - fileData = fileData.substr (pos + 12, fileData.length ()); - - pos = fileData.find ("\",\n", 0); - if (pos != fileData.npos) - { - fileData = fileData.substr (0, pos); - } - - replaceAll (fileData, "\\\\n", "$requires-newline$"); - replaceAll (fileData, "\\n", "\n"); - replaceAll (fileData, "$requires-newline$", "\\n"); - - //replaceAll(fileData,""","\""); - replaceAllHTMLEntities (fileData); - - - printf ("PARSED Language text\n[%s]\n", fileData.c_str ()); - - //vector languageName; - //Tokenize(fileData,languageName," "); - //printf("PARSED Language Name guessed to be [%s]\n",languageName[1].c_str()); - - //string data_path= getGameReadWritePath(GameConstants::path_data_CacheLookupKey); - //if(data_path != ""){ - //endPathWithSlash(data_path); - //} - Config & config = Config::getInstance (); - string - data_path = config.getString ("UserData_Root", ""); - if (data_path != "") - { - endPathWithSlash (data_path); - } - - string - outputFile = languageFileMappings[i]; - replaceAll (outputFile, "$language", toLower (langName)); - //string lngFile = getGameCustomCoreDataPath(data_path, "data/lang/" + toLower(languageName[1]) + ".lng"); - string - lngFile = getGameCustomCoreDataPath (data_path, outputFile); - - string - lngPath = extractDirectoryPathFromFile (lngFile); - createDirectoryPaths (lngPath); - - printf ("Save data to Language Name [%s]\n", lngFile.c_str ()); - saveDataToFile (lngFile, fileData); - gotDownloads = true; - - reloadLanguage = true; - if (saveChanges == true) - { - saveChanges = false; - config.save (); - } - } - else - { - printf ("UNPARSED Language text\n[%s]\n", fileData.c_str ()); - } - } - - SystemFlags::cleanupHTTP (&handle); - - if (reloadLanguage == true && langName != "") - { - Lang & lang = Lang::getInstance (); - if (lang.isLanguageLocal (toLower (langName)) == true) - { - lang.loadGameStrings (toLower (langName)); - } - } - - if (gotDownloads == true) - { - mainMessageBoxState = 0; - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("TransifexDownloadSuccess") + - "\n" + langName, lang.getString ("Notice"), - false); - } - } - return; - } - else if (labelPlayerName.mouseClick (x, y) - && (activeInputLabel != &labelPlayerName)) - { - setActiveInputLable (&labelPlayerName); - } - else if (labelTransifexUser.mouseClick (x, y) - && (activeInputLabel != &labelTransifexUser)) - { - setActiveInputLable (&labelTransifexUser); - } - else if (labelTransifexPwd.mouseClick (x, y) - && (activeInputLabel != &labelTransifexPwd)) - { - setActiveInputLable (&labelTransifexPwd); - } - else if (labelTransifexI18N.mouseClick (x, y) - && (activeInputLabel != &labelTransifexI18N)) - { - setActiveInputLable (&labelTransifexI18N); - } - else - { - listBoxLang.mouseClick (x, y); - listFontSizeAdjustment.mouseClick (x, y); - - listBoxScreenShotType.mouseClick (x, y); - - checkBoxDisableScreenshotConsoleText.mouseClick (x, y); - checkBoxMouseMoveScrollsWorld.mouseClick (x, y); - listCameraMoveSpeed.mouseClick (x, y); - checkBoxVisibleHud.mouseClick (x, y); - listBoxHealthBars.mouseClick (x, y); - checkBoxChatStaysActive.mouseClick (x, y); - checkBoxTimeDisplay.mouseClick (x, y); - checkBoxLuaDisableSecuritySandbox.mouseClick (x, y); - } - } - - void - MenuStateOptions::mouseMove (int x, int y, const MouseState * ms) - { - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - if (luaMessageBox.getEnabled ()) - { - luaMessageBox.mouseMove (x, y); - } - - buttonOk.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - buttonKeyboardSetup.mouseMove (x, y); - buttonAudioSection.mouseMove (x, y); - buttonNetworkSettings.mouseMove (x, y); - buttonMiscSection.mouseMove (x, y); - buttonVideoSection.mouseMove (x, y); - buttonGetNewLanguageFiles.mouseMove (x, y); - buttonDeleteNewLanguageFiles.mouseMove (x, y); - listBoxLang.mouseMove (x, y); - listBoxLang.mouseMove (x, y); - listFontSizeAdjustment.mouseMove (x, y); - listBoxScreenShotType.mouseMove (x, y); - checkBoxDisableScreenshotConsoleText.mouseMove (x, y); - checkBoxMouseMoveScrollsWorld.mouseMove (x, y); - listCameraMoveSpeed.mouseMove (x, y); - listBoxHealthBars.mouseMove (x, y); - checkBoxVisibleHud.mouseMove (x, y); - checkBoxChatStaysActive.mouseMove (x, y); - checkBoxTimeDisplay.mouseMove (x, y); - checkBoxLuaDisableSecuritySandbox.mouseMove (x, y); - checkBoxCustomTranslation.mouseMove (x, y); - } - - bool - MenuStateOptions::isInSpecialKeyCaptureEvent () - { - return (activeInputLabel != NULL); - } - - void - MenuStateOptions::keyDown (SDL_KeyboardEvent key) - { - if (activeInputLabel != NULL) - { - keyDownEditLabel (key, &activeInputLabel); - } - } - - bool - MenuStateOptions::textInput (std::string text) - { - if (activeInputLabel != NULL) - { - //printf("[%d]\n",c); fflush(stdout); - if (&labelPlayerName == activeInputLabel || - &labelTransifexUser == activeInputLabel || - &labelTransifexPwd == activeInputLabel || - &labelTransifexI18N == activeInputLabel) - { - return textInputEditLabel (text, &activeInputLabel); - } - } - return false; - } - - void - MenuStateOptions::keyPress (SDL_KeyboardEvent c) - { - if (activeInputLabel != NULL) - { - keyPressEditLabel (c, &activeInputLabel); - } - else - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), c) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } - } - } - - void - MenuStateOptions::render () - { - Renderer & renderer = Renderer::getInstance (); -// char szBuf[8096]=""; -// snprintf(szBuf,8096,"\nIn [%s::%s Line: %d]\n\nRender options menu [%p]!\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this); -// printf(szBuf); - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else if (luaMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&luaMessageBox); - } - else - { - renderer.renderButton (&buttonOk); - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonKeyboardSetup); - renderer.renderButton (&buttonVideoSection); - renderer.renderButton (&buttonAudioSection); - renderer.renderButton (&buttonMiscSection); - renderer.renderButton (&buttonNetworkSettings); - - renderer.renderLabel (&labelCustomTranslation); - renderer.renderCheckBox (&checkBoxCustomTranslation); - - if (buttonGetNewLanguageFiles.getEnabled ()) - renderer.renderButton (&buttonGetNewLanguageFiles); - if (buttonDeleteNewLanguageFiles.getEnabled ()) - renderer.renderButton (&buttonDeleteNewLanguageFiles); - if (labelTransifexUserLabel.getEnabled ()) - renderer.renderLabel (&labelTransifexUserLabel); - if (labelTransifexPwdLabel.getEnabled ()) - renderer.renderLabel (&labelTransifexPwdLabel); - if (labelTransifexI18NLabel.getEnabled ()) - renderer.renderLabel (&labelTransifexI18NLabel); - if (labelTransifexUser.getEnabled ()) - renderer.renderLabel (&labelTransifexUser); - if (labelTransifexPwd.getEnabled ()) - renderer.renderLabel (&labelTransifexPwd); - if (labelTransifexI18N.getEnabled ()) - renderer.renderLabel (&labelTransifexI18N); - - renderer.renderListBox (&listBoxLang); - renderer.renderLabel (&labelLang); - renderer.renderLabel (&labelPlayerNameLabel); - renderer.renderLabel (&labelPlayerName); - renderer.renderListBox (&listFontSizeAdjustment); - renderer.renderLabel (&labelFontSizeAdjustment); - - renderer.renderLabel (&labelScreenShotType); - renderer.renderListBox (&listBoxScreenShotType); - - renderer.renderLabel (&labelDisableScreenshotConsoleText); - renderer.renderCheckBox (&checkBoxDisableScreenshotConsoleText); - - renderer.renderLabel (&labelMouseMoveScrollsWorld); - renderer.renderCheckBox (&checkBoxMouseMoveScrollsWorld); - renderer.renderLabel (&labelCameraMoveSpeed); - renderer.renderListBox (&listCameraMoveSpeed); - - renderer.renderLabel (&labelVisibleHud); - renderer.renderLabel (&labelHealthBars); - renderer.renderListBox (&listBoxHealthBars); - renderer.renderLabel (&labelChatStaysActive); - renderer.renderLabel (&labelTimeDisplay); - - renderer.renderLabel (&labelLuaDisableSecuritySandbox); - renderer.renderCheckBox (&checkBoxLuaDisableSecuritySandbox); - - renderer.renderCheckBox (&checkBoxVisibleHud); - renderer.renderCheckBox (&checkBoxChatStaysActive); - renderer.renderCheckBox (&checkBoxTimeDisplay); - - } - - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } - - void - MenuStateOptions::saveConfig () - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - setActiveInputLable (NULL); - - if (labelPlayerName.getText ().length () > 0) - { - config.setString ("NetPlayerName", labelPlayerName.getText ()); - } - //Copy values - map < string, string >::iterator iterMap = languageList.begin (); - std::advance (iterMap, listBoxLang.getSelectedItemIndex ()); - - config.setString ("Lang", iterMap->first); - lang.loadGameStrings (config.getString ("Lang")); - - config.setString ("FontSizeAdjustment", - listFontSizeAdjustment.getSelectedItem ()); - config.setString ("ScreenShotFileType", - listBoxScreenShotType.getSelectedItem ()); - - config.setBool ("DisableScreenshotConsoleText", - !checkBoxDisableScreenshotConsoleText.getValue ()); - config.setBool ("MouseMoveScrollsWorld", - checkBoxMouseMoveScrollsWorld.getValue ()); - config.setString ("CameraMoveSpeed", - listCameraMoveSpeed.getSelectedItem ()); - - int - hpIndex = listBoxHealthBars.getSelectedItemIndex (); - int - hpMode = hbvUndefined; - switch (hpIndex) - { - case 0: - hpMode = hbvUndefined; - break; - case 1: - hpMode = hbvOff; - break; - case 2: - hpMode = hbvAlways; - break; - case 3: - hpMode = hbvIfNeeded; - break; - case 4: - hpMode = hbvSelected; - break; - case 5: - hpMode = hbvSelected | hbvIfNeeded; - break; - default: - hpMode = hbvUndefined; - break; - } - - config.setInt ("HealthBarMode", hpMode); - config.setBool ("VisibleHud", checkBoxVisibleHud.getValue ()); - config.setBool ("ChatStaysActive", checkBoxChatStaysActive.getValue ()); - config.setBool ("TimeDisplay", checkBoxTimeDisplay.getValue ()); - - config.setBool ("DisableLuaSandbox", - checkBoxLuaDisableSecuritySandbox.getValue ()); - config.save (); - - if (config.getBool ("DisableLuaSandbox", "false") == true) - { - LuaScript::setDisableSandbox (true); - } - Renderer::getInstance ().loadConfig (); - console.addLine (lang.getString ("SettingsSaved")); - } - - void - MenuStateOptions::setActiveInputLable (GraphicLabel * newLable) - { - MenuState::setActiveInputLabel (newLable, &activeInputLabel); - - if (newLable == &labelTransifexPwd) - { - labelTransifexPwd.setIsPassword (false); - } - else - { - labelTransifexPwd.setIsPassword (true); - } - } - - } + Glest { + namespace + Game { + + // ===================================================== + // class MenuStateOptions + // ===================================================== + MenuStateOptions::MenuStateOptions(Program * program, + MainMenu * mainMenu, + ProgramState ** parentUI) : + MenuState(program, mainMenu, "config"), + buttonOk("Options", "buttonOk"), + buttonReturn("Options", "buttonReturn"), + labelLang("Options", "labelLang"), + listBoxLang("Options", "listBoxLang"), + labelPlayerName("Options", "labelPlayerName"), + labelPlayerNameLabel("Options", "labelPlayerNameLabel"), + buttonKeyboardSetup("Options", "buttonKeyboardSetup"), + buttonVideoSection("Options", "buttonVideoSection"), + buttonAudioSection("Options", "buttonAudioSection"), + buttonMiscSection("Options", "buttonMiscSection"), + buttonNetworkSettings("Options", "buttonNetworkSettings"), + labelFontSizeAdjustment("Options", "labelFontSizeAdjustment"), + listFontSizeAdjustment("Options", "listFontSizeAdjustment"), + mainMessageBox("Options", "mainMessageBox"), + labelScreenShotType("Options", "labelScreenShotType"), + listBoxScreenShotType("Options", "listBoxScreenShotType"), + labelDisableScreenshotConsoleText("Options", + "labelDisableScreenshotConsoleText"), + checkBoxDisableScreenshotConsoleText("Options", + "checkBoxDisableScreenshotConsoleText"), + labelMouseMoveScrollsWorld("Options", "labelMouseMoveScrollsWorld"), + checkBoxMouseMoveScrollsWorld("Options", + "checkBoxMouseMoveScrollsWorld"), + labelCameraMoveSpeed("Options", "labelCameraMoveSpeed"), + listCameraMoveSpeed("Options", "listCameraMoveSpeed"), + labelVisibleHud("Options", "labelVisibleHud"), + checkBoxVisibleHud("Options", "checkBoxVisibleHud"), + labelHealthBars("Options", "labelHealthBars"), + listBoxHealthBars("Options", "listBoxHealthBars"), + labelTimeDisplay("Options", "labelTimeDisplay"), + checkBoxTimeDisplay("Options", "checkBoxTimeDisplay"), + labelChatStaysActive("Options", "labelChatStaysActive"), + checkBoxChatStaysActive("Options", "checkBoxChatStaysActive"), + labelLuaDisableSecuritySandbox("Options", + "labelLuaDisableSecuritySandbox"), + checkBoxLuaDisableSecuritySandbox("Options", + "checkBoxLuaDisableSecuritySandbox"), + luaMessageBox("Options", "luaMessageBox"), + labelCustomTranslation("Options", "labelCustomTranslation"), + checkBoxCustomTranslation("Options", "checkBoxCustomTranslation"), + buttonGetNewLanguageFiles("Options", "buttonGetNewLanguageFiles"), + buttonDeleteNewLanguageFiles("Options", "buttonDeleteNewLanguageFiles"), + labelTransifexUserLabel("Options", "labelTransifexUserLabel"), + labelTransifexUser("Options", "labelTransifexUser"), + labelTransifexPwdLabel("Options", "labelTransifexPwdLabel"), + labelTransifexPwd("Options", "labelTransifexPwd"), + labelTransifexI18NLabel("Options", "labelTransifexI18NLabel"), + labelTransifexI18N("Options", "labelTransifexI18N") { + try { + containerName = "Options"; + this-> + parentUI = parentUI; + Lang & + lang = Lang::getInstance(); + Config & + config = Config::getInstance(); + + this-> + console. + setOnlyChatMessagesInStoredLines(false); + activeInputLabel = NULL; + + int + leftLabelStart = 100; + int + leftColumnStart = leftLabelStart + 300; + int + buttonRowPos = 50; + int + buttonStartPos = 170; + int + lineOffset = 30; + int + tabButtonWidth = 200; + int + tabButtonHeight = 30; + + mainMessageBox. + init(lang.getString("Ok")); + mainMessageBox. + setEnabled(false); + mainMessageBoxState = 0; + + buttonAudioSection. + init(0, 720, tabButtonWidth, tabButtonHeight); + buttonAudioSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonAudioSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonAudioSection. + setText(lang.getString("Audio")); + // Video Section + buttonVideoSection. + init(200, 720, tabButtonWidth, tabButtonHeight); + buttonVideoSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonVideoSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonVideoSection. + setText(lang.getString("Video")); + //MiscSection + buttonMiscSection. + init(400, 700, tabButtonWidth, tabButtonHeight + 20); + buttonMiscSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonMiscSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonMiscSection. + setText(lang.getString("Misc")); + //NetworkSettings + buttonNetworkSettings. + init(600, 720, tabButtonWidth, tabButtonHeight); + buttonNetworkSettings. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonNetworkSettings. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonNetworkSettings. + setText(lang.getString("Network")); + + //KeyboardSetup + buttonKeyboardSetup. + init(800, 720, tabButtonWidth, tabButtonHeight); + buttonKeyboardSetup. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonKeyboardSetup. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonKeyboardSetup. + setText(lang.getString("Keyboardsetup")); + + int + currentLine = 650; // reset line pos + int + currentLabelStart = leftLabelStart; // set to right side + int + currentColumnStart = leftColumnStart; // set to right side + + //lang + labelLang. + init(currentLabelStart, currentLine); + labelLang. + setText(lang.getString("Language")); + + listBoxLang. + init(currentColumnStart, currentLine, 375); + vector < + string > + langResults; + + languageList = Lang::getInstance().getDiscoveredLanguageList(true); + for (map < string, string >::iterator iterMap = languageList.begin(); + iterMap != languageList.end(); ++iterMap) { + langResults.push_back(iterMap->first + "-" + iterMap->second); + } + + listBoxLang. + setItems(langResults); + + pair < + string, + string > + defaultLang = + Lang::getInstance().getNavtiveNameFromLanguageName(config. + getString + ("Lang")); + if (defaultLang.first == "" && defaultLang.second == "") { + defaultLang = + Lang::getInstance(). + getNavtiveNameFromLanguageName(Lang::getInstance(). + getDefaultLanguage()); + } + listBoxLang. + setSelectedItem(defaultLang.second + "-" + defaultLang.first); + currentLine -= lineOffset; + + //playerName + labelPlayerNameLabel.init(currentLabelStart, currentLine); + labelPlayerNameLabel.setText(lang.getString("Playername")); + + labelPlayerName.init(currentColumnStart, currentLine); + labelPlayerName.setText(config. + getString("NetPlayerName", + Socket::getHostName().c_str())); + labelPlayerName.setFont(CoreData::getInstance().getMenuFontBig()); + labelPlayerName.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + labelPlayerName.setEditable(true); + labelPlayerName.setMaxEditWidth(16); + labelPlayerName.setMaxEditRenderWidth(200); + currentLine -= lineOffset; + + //FontSizeAdjustment + labelFontSizeAdjustment.init(currentLabelStart, currentLine); + labelFontSizeAdjustment.setText(lang. + getString("FontSizeAdjustment")); + + listFontSizeAdjustment.init(currentColumnStart, currentLine, 80); + for (int i = -5; i <= 5; i += 1) { + listFontSizeAdjustment.pushBackItem(intToStr(i)); + } + listFontSizeAdjustment. + setSelectedItem(intToStr(config.getInt("FontSizeAdjustment"))); + + currentLine -= lineOffset; + // Screenshot type flag + labelScreenShotType.init(currentLabelStart, currentLine); + labelScreenShotType.setText(lang.getString("ScreenShotFileType")); + + listBoxScreenShotType.init(currentColumnStart, currentLine, 80); + listBoxScreenShotType.pushBackItem("bmp"); + listBoxScreenShotType.pushBackItem("jpg"); + listBoxScreenShotType.pushBackItem("png"); + listBoxScreenShotType.pushBackItem("tga"); + listBoxScreenShotType.setSelectedItem(config. + getString + ("ScreenShotFileType", "jpg")); + + currentLine -= lineOffset; + + labelDisableScreenshotConsoleText.init(currentLabelStart, + currentLine); + labelDisableScreenshotConsoleText.setText(lang. + getString + ("ScreenShotConsoleText")); + + checkBoxDisableScreenshotConsoleText.init(currentColumnStart, + currentLine); + checkBoxDisableScreenshotConsoleText.setValue(!config. + getBool + ("DisableScreenshotConsoleText", + "false")); + + currentLine -= lineOffset; + + labelMouseMoveScrollsWorld.init(currentLabelStart, currentLine); + labelMouseMoveScrollsWorld.setText(lang. + getString("MouseScrollsWorld")); + + checkBoxMouseMoveScrollsWorld.init(currentColumnStart, currentLine); + checkBoxMouseMoveScrollsWorld.setValue(config. + getBool + ("MouseMoveScrollsWorld", + "true")); + currentLine -= lineOffset; + + //CameraMoveSpeed + labelCameraMoveSpeed.init(currentLabelStart, currentLine); + labelCameraMoveSpeed.setText(lang.getString("CameraMoveSpeed")); + + listCameraMoveSpeed.init(currentColumnStart, currentLine, 80); + for (int i = 15; i <= 50; i += 5) { + listCameraMoveSpeed.pushBackItem(intToStr(i)); + } + listCameraMoveSpeed. + setSelectedItem(intToStr + ((int) + (config.getFloat("CameraMoveSpeed", "15")))); + currentLine -= lineOffset; + + labelVisibleHud.init(currentLabelStart, currentLine); + labelVisibleHud.setText(lang.getString("VisibleHUD")); + + checkBoxVisibleHud.init(currentColumnStart, currentLine); + checkBoxVisibleHud.setValue(config.getBool("VisibleHud", "true")); + + currentLine -= lineOffset; + + labelHealthBars.init(currentLabelStart, currentLine); + labelHealthBars.setText(lang.getString("Healthbar")); + + listBoxHealthBars.init(currentColumnStart, currentLine, 375); + listBoxHealthBars.pushBackItem(lang. + getString + ("HealthbarsFactionDefault")); + listBoxHealthBars.pushBackItem(lang.getString("HealthbarsOff")); + listBoxHealthBars.pushBackItem(lang.getString("HealthbarsAlways")); + listBoxHealthBars.pushBackItem(lang. + getString("HealthbarsIfNeeded")); + listBoxHealthBars.pushBackItem(lang. + getString("HealthbarsSelected")); + listBoxHealthBars.pushBackItem(lang. + getString + ("HealthbarsSelectedOrNeeded")); + + int + hpMode = config.getInt("HealthBarMode", "4"); + int + hpIndex = 0; + switch (hpMode) { + case hbvUndefined: + hpIndex = 0; + break; + case hbvOff: + hpIndex = 1; + break; + case hbvAlways: + hpIndex = 2; + break; + case hbvIfNeeded: + hpIndex = 3; + break; + case hbvSelected: + hpIndex = 4; + break; + case hbvSelected | hbvIfNeeded: + hpIndex = 5; + break; + default: + hpIndex = 0; + break; + } + + listBoxHealthBars.setSelectedItemIndex(hpIndex); + + currentLine -= lineOffset; + + labelChatStaysActive.init(currentLabelStart, currentLine); + labelChatStaysActive.setText(lang.getString("ChatStaysActive")); + + checkBoxChatStaysActive.init(currentColumnStart, currentLine); + checkBoxChatStaysActive.setValue(config. + getBool("ChatStaysActive", + "false")); + + currentLine -= lineOffset; + + labelTimeDisplay.init(currentLabelStart, currentLine); + labelTimeDisplay.setText(lang.getString("TimeDisplay")); + + checkBoxTimeDisplay.init(currentColumnStart, currentLine); + checkBoxTimeDisplay.setValue(config.getBool("TimeDisplay", "true")); + + currentLine -= lineOffset; + + labelLuaDisableSecuritySandbox.init(currentLabelStart, currentLine); + labelLuaDisableSecuritySandbox.setText(lang. + getString + ("LuaDisableSecuritySandbox")); + + checkBoxLuaDisableSecuritySandbox.init(currentColumnStart, + currentLine); + checkBoxLuaDisableSecuritySandbox.setValue(config. + getBool + ("DisableLuaSandbox", + "false")); + + luaMessageBox.init(lang.getString("Yes"), lang.getString("No")); + luaMessageBox.setEnabled(false); + luaMessageBoxState = 0; + + currentLine -= lineOffset; + + currentLine -= lineOffset / 2; + + // buttons + buttonOk.init(buttonStartPos, buttonRowPos, 100); + buttonOk.setText(lang.getString("Save")); + + buttonReturn.init(buttonStartPos + 110, buttonRowPos, 100); + buttonReturn.setText(lang.getString("Return")); + + // Transifex related UI + currentLine -= lineOffset * 3; + labelCustomTranslation.init(currentLabelStart, currentLine); + labelCustomTranslation.setText(lang.getString("CustomTranslation")); + + checkBoxCustomTranslation.init(currentColumnStart, currentLine); + checkBoxCustomTranslation.setValue(false); + currentLine -= lineOffset; + + labelTransifexUserLabel.init(currentLabelStart, currentLine); + labelTransifexUserLabel.setText(lang. + getString("TransifexUserName")); + + labelTransifexPwdLabel.init(currentLabelStart + 260, currentLine); + labelTransifexPwdLabel.setText(lang.getString("TransifexPwd")); + + labelTransifexI18NLabel.init(currentLabelStart + 520, currentLine); + labelTransifexI18NLabel.setText(lang.getString("TransifexI18N")); + + currentLine -= lineOffset; + + labelTransifexUser.init(currentLabelStart, currentLine); + labelTransifexUser.setEditable(true); + labelTransifexUser.setMaxEditWidth(28); + labelTransifexUser.setMaxEditRenderWidth(250); + labelTransifexUser.setText(config. + getString("TranslationGetURLUser", + "")); + + labelTransifexPwd.init(currentLabelStart + 260, currentLine); + labelTransifexPwd.setIsPassword(true); + labelTransifexPwd.setEditable(true); + labelTransifexPwd.setMaxEditWidth(28); + labelTransifexPwd.setMaxEditRenderWidth(250); + labelTransifexPwd.setText(config. + getString("TranslationGetURLPassword", + "")); + + labelTransifexI18N.init(currentLabelStart + 520, currentLine); + labelTransifexI18N.setEditable(true); + labelTransifexI18N.setMaxEditWidth(6); + labelTransifexI18N.setMaxEditRenderWidth(70); + labelTransifexI18N.setText(config. + getString("TranslationGetURLLanguage", + "en")); + currentLine -= lineOffset; + + buttonGetNewLanguageFiles.init(currentLabelStart, currentLine, 250); + buttonGetNewLanguageFiles.setText(lang. + getString + ("TransifexGetLanguageFiles")); + + buttonDeleteNewLanguageFiles.init(currentLabelStart + 260, + currentLine, 250); + buttonDeleteNewLanguageFiles.setText(lang. + getString + ("TransifexDeleteLanguageFiles")); + + setupTransifexUI(); + + GraphicComponent::applyAllCustomProperties(containerName); + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error loading options: %s\n", + __FILE__, __FUNCTION__, __LINE__, + e.what()); + throw + megaglest_runtime_error(string("Error loading options msg: ") + + e.what()); + } + } + + MenuStateOptions::~MenuStateOptions() { + } + + void + MenuStateOptions::reloadUI() { + Lang & lang = Lang::getInstance(); + + mainMessageBox.init(lang.getString("Ok")); + luaMessageBox.init(lang.getString("Yes"), lang.getString("No")); + + buttonAudioSection.setText(lang.getString("Audio")); + buttonVideoSection.setText(lang.getString("Video")); + buttonMiscSection.setText(lang.getString("Misc")); + buttonNetworkSettings.setText(lang.getString("Network")); + buttonKeyboardSetup.setText(lang.getString("Keyboardsetup")); + + labelVisibleHud.setText(lang.getString("VisibleHUD")); + labelHealthBars.setText(lang.getString("Healthbar")); + labelChatStaysActive.setText(lang.getString("ChatStaysActive")); + labelTimeDisplay.setText(lang.getString("TimeDisplay")); + + labelLuaDisableSecuritySandbox.setText(lang. + getString + ("LuaDisableSecuritySandbox")); + labelLang.setText(lang.getString("Language")); + labelPlayerNameLabel.setText(lang.getString("Playername")); + labelFontSizeAdjustment.setText(lang.getString("FontSizeAdjustment")); + labelScreenShotType.setText(lang.getString("ScreenShotFileType")); + labelDisableScreenshotConsoleText.setText(lang. + getString + ("ScreenShotConsoleText")); + labelMouseMoveScrollsWorld.setText(lang. + getString("MouseScrollsWorld")); + labelCameraMoveSpeed.setText(lang.getString("CameraMoveSpeed")); + + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + + labelCustomTranslation.setText(lang.getString("CustomTranslation")); + buttonGetNewLanguageFiles.setText(lang. + getString + ("TransifexGetLanguageFiles")); + buttonDeleteNewLanguageFiles.setText(lang. + getString + ("TransifexDeleteLanguageFiles")); + labelTransifexUserLabel.setText(lang.getString("TransifexUserName")); + labelTransifexPwdLabel.setText(lang.getString("TransifexPwd")); + labelTransifexI18NLabel.setText(lang.getString("TransifexI18N")); + } + + void + MenuStateOptions::setupTransifexUI() { + buttonGetNewLanguageFiles.setEnabled(checkBoxCustomTranslation. + getValue()); + buttonDeleteNewLanguageFiles.setEnabled(checkBoxCustomTranslation. + getValue()); + labelTransifexUserLabel.setEnabled(checkBoxCustomTranslation. + getValue()); + labelTransifexUser.setEnabled(checkBoxCustomTranslation.getValue()); + labelTransifexPwdLabel.setEnabled(checkBoxCustomTranslation. + getValue()); + labelTransifexPwd.setEnabled(checkBoxCustomTranslation.getValue()); + labelTransifexI18NLabel.setEnabled(checkBoxCustomTranslation. + getValue()); + labelTransifexI18N.setEnabled(checkBoxCustomTranslation.getValue()); + } + + void + MenuStateOptions::showMessageBox(const string & text, + const string & header, bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void + MenuStateOptions::showLuaMessageBox(const string & text, + const string & header, bool toggle) { + if (!toggle) { + luaMessageBox.setEnabled(false); + } + + if (!luaMessageBox.getEnabled()) { + luaMessageBox.setText(text); + luaMessageBox.setHeader(header); + luaMessageBox.setEnabled(true); + } else { + luaMessageBox.setEnabled(false); + } + } + + void + MenuStateOptions::mouseClick(int x, int y, MouseButton mouseButton) { + + Config & config = Config::getInstance(); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + int + button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + saveConfig(); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + } else { + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + } + } else { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + } + } + } + } else if (luaMessageBox.getEnabled()) { + int + button = 0; + if (luaMessageBox.mouseClick(x, y, button)) { + checkBoxLuaDisableSecuritySandbox.setValue(false); + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + if (luaMessageBoxState == 1) { + checkBoxLuaDisableSecuritySandbox.setValue(true); + } + } + luaMessageBox.setEnabled(false); + } + } else if (checkBoxLuaDisableSecuritySandbox.mouseClick(x, y)) { + if (checkBoxLuaDisableSecuritySandbox.getValue() == true) { + checkBoxLuaDisableSecuritySandbox.setValue(false); + + luaMessageBoxState = 1; + Lang & lang = Lang::getInstance(); + showLuaMessageBox(lang. + getString("LuaDisableSecuritySandboxWarning"), + lang.getString("Question"), false); + } + } else if (buttonOk.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + + string + currentFontSizeAdjustment = config.getString("FontSizeAdjustment"); + string + selectedFontSizeAdjustment = + listFontSizeAdjustment.getSelectedItem(); + if (currentFontSizeAdjustment != selectedFontSizeAdjustment) { + mainMessageBoxState = 1; + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("RestartNeeded"), + lang.getString("FontSizeAdjustmentChanged"), + false); + return; + } + saveConfig(); + //mainMenu->setState(new MenuStateRoot(program, mainMenu)); + reloadUI(); + return; + } else if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (this->parentUI != NULL) { + *this->parentUI = NULL; + delete * + this-> + parentUI; + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } else if (buttonKeyboardSetup.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateKeysetup(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + //showMessageBox("Not implemented yet", "Keyboard setup", false); + return; + } else if (buttonAudioSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsSound(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonNetworkSettings.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonMiscSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateOptions(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonVideoSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (checkBoxCustomTranslation.mouseClick(x, y)) { + setupTransifexUI(); + } else if (buttonDeleteNewLanguageFiles.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + + setActiveInputLable(NULL); + + if (labelTransifexI18N.getText() != "") { + Lang & lang = Lang::getInstance(); + string + language = lang.getLanguageFile(labelTransifexI18N.getText()); + replaceAll(language, "(", ""); + replaceAll(language, ")", ""); + + if (language != "") { + bool + foundFilesToDelete = false; + + Config & config = Config::getInstance(); + string + data_path = config.getString("UserData_Root", ""); + if (data_path != "") { + endPathWithSlash(data_path); + } + + if (data_path != "") { + + string + txnURLFileListMapping = + Config::getInstance(). + getString("TranslationGetURLFileListMapping"); + vector < string > languageFileMappings; + Tokenize(txnURLFileListMapping, languageFileMappings, "|"); + + Config & config = Config::getInstance(); + + // Cleanup Scenarios + vector < string > scenarioPaths = + config.getPathListForType(ptScenarios); + if (scenarioPaths.size() > 1) { + string & scenarioPath = scenarioPaths[1]; + endPathWithSlash(scenarioPath); + + vector < string > scenarioList; + findDirs(scenarioPath, scenarioList, false, false); + for (unsigned int i = 0; i < scenarioList.size(); ++i) { + string + scenario = scenarioList[i]; + + vector < string > langResults; + findAll(scenarioPath + scenario + "/*.lng", langResults, + false, false); + for (unsigned int j = 0; j < langResults.size(); ++j) { + string + testLanguage = langResults[j]; + + string + removeLngFile = + scenarioPath + scenario + "/" + testLanguage; + + if (EndsWith(testLanguage, language + ".lng") == true) { + + for (unsigned int k = 0; + k < languageFileMappings.size(); ++k) { + string + mapping = languageFileMappings[k]; + replaceAll(mapping, "$language", language); + + //printf("Comparing found [%s] with [%s]\n",removeLngFile.c_str(),mapping.c_str()); + + if (EndsWith(removeLngFile, mapping) == true) { + printf("About to delete file [%s]\n", + removeLngFile.c_str()); + removeFile(removeLngFile); + foundFilesToDelete = true; + break; + } + } + } + } + } + } + + // Cleanup tutorials + vector < string > tutorialPaths = + config.getPathListForType(ptTutorials); + if (tutorialPaths.size() > 1) { + string & tutorialPath = tutorialPaths[1]; + endPathWithSlash(tutorialPath); + + vector < string > tutorialList; + findDirs(tutorialPath, tutorialList, false, false); + for (unsigned int i = 0; i < tutorialList.size(); ++i) { + string + tutorial = tutorialList[i]; + + vector < string > langResults; + findAll(tutorialPath + tutorial + "/*.lng", langResults, + false, false); + for (unsigned int j = 0; j < langResults.size(); ++j) { + string + testLanguage = langResults[j]; + + string + removeLngFile = + tutorialPath + tutorial + "/" + testLanguage; + if (EndsWith(testLanguage, language + ".lng") == true) { + + + for (unsigned int k = 0; + k < languageFileMappings.size(); ++k) { + string + mapping = languageFileMappings[k]; + replaceAll(mapping, "$language", language); + + //printf("Comparing found [%s] with [%s]\n",removeLngFile.c_str(),mapping.c_str()); + + if (EndsWith(removeLngFile, mapping) == true) { + printf("About to delete file [%s]\n", + removeLngFile.c_str()); + removeFile(removeLngFile); + foundFilesToDelete = true; + break; + } + } + } + } + } + } + + // Cleanup main and hint language files + string + mainLngFile = data_path + "data/lang/" + language + ".lng"; + if (fileExists(mainLngFile) == true) { + + for (unsigned int k = 0; k < languageFileMappings.size(); + ++k) { + string + mapping = languageFileMappings[k]; + replaceAll(mapping, "$language", language); + + if (EndsWith(mainLngFile, mapping) == true) { + printf("About to delete file [%s]\n", + mainLngFile.c_str()); + removeFile(mainLngFile); + foundFilesToDelete = true; + break; + } + } + } + + string + hintLngFile = + data_path + "data/lang/hint/hint_" + language + ".lng"; + if (fileExists(hintLngFile) == true) { + for (unsigned int k = 0; k < languageFileMappings.size(); + ++k) { + string + mapping = languageFileMappings[k]; + replaceAll(mapping, "$language", language); + + if (EndsWith(hintLngFile, mapping) == true) { + printf("About to delete file [%s]\n", + hintLngFile.c_str()); + removeFile(hintLngFile); + foundFilesToDelete = true; + break; + } + } + } + } + + if (lang.isLanguageLocal(toLower(language)) == true) { + lang.loadGameStrings(toLower(language)); + } + + if (foundFilesToDelete == true) { + mainMessageBoxState = 0; + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("TransifexDeleteSuccess"), + lang.getString("Notice"), false); + } + } + } + } else if (buttonGetNewLanguageFiles.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + + setActiveInputLable(NULL); + + string + orig_txnURLUser = + Config::getInstance().getString("TranslationGetURLUser"); + //string orig_txnURLPwd = Config::getInstance().getString("TranslationGetURLPassword",""); + string + orig_txnURLLang = + Config::getInstance().getString("TranslationGetURLLanguage"); + + Config::getInstance().setString("TranslationGetURLUser", + labelTransifexUser.getText()); + Config::getInstance().setString("TranslationGetURLPassword", + labelTransifexPwd.getText(), true); + Config::getInstance().setString("TranslationGetURLLanguage", + labelTransifexI18N.getText()); + + bool + saveChanges = (orig_txnURLUser != labelTransifexUser.getText() || + orig_txnURLLang != labelTransifexI18N.getText()); + + string + txnURL = Config::getInstance().getString("TranslationGetURL"); + string + txnURLUser = + Config::getInstance().getString("TranslationGetURLUser"); + string + txnURLPwd = + Config::getInstance().getString("TranslationGetURLPassword"); + string + txnURLLang = + Config::getInstance().getString("TranslationGetURLLanguage"); + string + txnURLFileList = + Config::getInstance().getString("TranslationGetURLFileList"); + string + txnURLFileListMapping = + Config::getInstance(). + getString("TranslationGetURLFileListMapping"); + + string + txnURLDetails = + Config::getInstance().getString("TranslationGetURLDetails"); + + string + credentials = txnURLUser + ":" + txnURLPwd; + + printf("URL1 [%s] credentials [%s]\n", txnURL.c_str(), + credentials.c_str()); + + //txnURLUser = SystemFlags::escapeURL(txnURLUser,handle); + //replaceAll(txnURL,"$user",txnURLUser); + + //printf("URL2 [%s]\n",txnURL.c_str()); + + //txnURLPwd = SystemFlags::escapeURL(txnURLPwd,handle); + //replaceAll(txnURL,"$password",txnURLPwd); + + //printf("URL3 [%s]\n",txnURL.c_str()); + + replaceAll(txnURL, "$language", txnURLLang); + + printf("URL4 [%s]\n", txnURL.c_str()); + + //txnURLFileList + vector < string > languageFiles; + Tokenize(txnURLFileList, languageFiles, "|"); + + vector < string > languageFileMappings; + Tokenize(txnURLFileListMapping, languageFileMappings, "|"); + + printf("URL5 file count = " MG_SIZE_T_SPECIFIER ", " + MG_SIZE_T_SPECIFIER " [%s]\n", languageFiles.size(), + languageFileMappings.size(), + (languageFiles.empty() == + false ? languageFiles[0].c_str() : "")); + + if (languageFiles.empty() == false) { + + bool + gotDownloads = false; + bool + reloadLanguage = false; + string + langName = ""; + + CURL * + handle = SystemFlags::initHTTP(); + for (unsigned int i = 0; i < languageFiles.size(); ++i) { + string + fileURL = txnURL; + replaceAll(fileURL, "$file", languageFiles[i]); + + if (langName == "") { + // Get language name for file + string + fileURLDetails = txnURLDetails; + replaceAll(fileURLDetails, "$file", languageFiles[0]); + + printf(" i = %u Trying [%s]\n", i, fileURLDetails.c_str()); + curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); + curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(handle, CURLOPT_USERPWD, + credentials.c_str()); + std::string fileDataDetails = + SystemFlags::getHTTP(fileURLDetails, handle); + + // "available_languages": [ + // { + // "code_aliases": " ", + // "code": "ca", + // "name": "Catalan" + // }, + // { + // "code_aliases": " ", + // "code": "zh", + // "name": "Chinese" + // }, + // curl -i -L --user softcoder -X GET https://www.transifex.com/api/2/project/megaglest/resource/main-language-file/?details + + string + search_detail_key = "\"code\": \"" + txnURLLang + "\""; + size_t + posDetails = fileDataDetails.find(search_detail_key, 0); + if (posDetails != fileDataDetails.npos) { + posDetails = + fileDataDetails.find("\"name\": \"", + posDetails + + search_detail_key.length()); + + if (posDetails != fileDataDetails.npos) { + + size_t + posDetailsEnd = + fileDataDetails.find("\"", posDetails + 9); + + langName = + fileDataDetails.substr(posDetails + 9, + posDetailsEnd - (posDetails + 9)); + replaceAll(langName, ",", ""); + replaceAll(langName, "\\", ""); + replaceAll(langName, "/", ""); + replaceAll(langName, "?", ""); + replaceAll(langName, ":", ""); + replaceAll(langName, "@", ""); + replaceAll(langName, "!", ""); + replaceAll(langName, "*", ""); + replaceAll(langName, "(", ""); + replaceAll(langName, ")", ""); + langName = trim(langName); + replaceAll(langName, " ", "-"); + } + + printf("PARSED Language filename [%s]\n", langName.c_str()); + } + } + + printf("i = %u Trying [%s]\n", i, fileURL.c_str()); + curl_easy_setopt(handle, CURLOPT_VERBOSE, 1); + curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(handle, CURLOPT_USERPWD, credentials.c_str()); + std::string fileData = SystemFlags::getHTTP(fileURL, handle); + + // "content": " + // ", + // "mimetype": "text/plain" + size_t + pos = fileData.find("\"content\": \"", 0); + if (pos != fileData.npos) { + fileData = fileData.substr(pos + 12, fileData.length()); + + pos = fileData.find("\",\n", 0); + if (pos != fileData.npos) { + fileData = fileData.substr(0, pos); + } + + replaceAll(fileData, "\\\\n", "$requires-newline$"); + replaceAll(fileData, "\\n", "\n"); + replaceAll(fileData, "$requires-newline$", "\\n"); + + //replaceAll(fileData,""","\""); + replaceAllHTMLEntities(fileData); + + + printf("PARSED Language text\n[%s]\n", fileData.c_str()); + + //vector languageName; + //Tokenize(fileData,languageName," "); + //printf("PARSED Language Name guessed to be [%s]\n",languageName[1].c_str()); + + //string data_path= getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + //if(data_path != ""){ + //endPathWithSlash(data_path); + //} + Config & config = Config::getInstance(); + string + data_path = config.getString("UserData_Root", ""); + if (data_path != "") { + endPathWithSlash(data_path); + } + + string + outputFile = languageFileMappings[i]; + replaceAll(outputFile, "$language", toLower(langName)); + //string lngFile = getGameCustomCoreDataPath(data_path, "data/lang/" + toLower(languageName[1]) + ".lng"); + string + lngFile = getGameCustomCoreDataPath(data_path, outputFile); + + string + lngPath = extractDirectoryPathFromFile(lngFile); + createDirectoryPaths(lngPath); + + printf("Save data to Language Name [%s]\n", lngFile.c_str()); + saveDataToFile(lngFile, fileData); + gotDownloads = true; + + reloadLanguage = true; + if (saveChanges == true) { + saveChanges = false; + config.save(); + } + } else { + printf("UNPARSED Language text\n[%s]\n", fileData.c_str()); + } + } + + SystemFlags::cleanupHTTP(&handle); + + if (reloadLanguage == true && langName != "") { + Lang & lang = Lang::getInstance(); + if (lang.isLanguageLocal(toLower(langName)) == true) { + lang.loadGameStrings(toLower(langName)); + } + } + + if (gotDownloads == true) { + mainMessageBoxState = 0; + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("TransifexDownloadSuccess") + + "\n" + langName, lang.getString("Notice"), + false); + } + } + return; + } else if (labelPlayerName.mouseClick(x, y) + && (activeInputLabel != &labelPlayerName)) { + setActiveInputLable(&labelPlayerName); + } else if (labelTransifexUser.mouseClick(x, y) + && (activeInputLabel != &labelTransifexUser)) { + setActiveInputLable(&labelTransifexUser); + } else if (labelTransifexPwd.mouseClick(x, y) + && (activeInputLabel != &labelTransifexPwd)) { + setActiveInputLable(&labelTransifexPwd); + } else if (labelTransifexI18N.mouseClick(x, y) + && (activeInputLabel != &labelTransifexI18N)) { + setActiveInputLable(&labelTransifexI18N); + } else { + listBoxLang.mouseClick(x, y); + listFontSizeAdjustment.mouseClick(x, y); + + listBoxScreenShotType.mouseClick(x, y); + + checkBoxDisableScreenshotConsoleText.mouseClick(x, y); + checkBoxMouseMoveScrollsWorld.mouseClick(x, y); + listCameraMoveSpeed.mouseClick(x, y); + checkBoxVisibleHud.mouseClick(x, y); + listBoxHealthBars.mouseClick(x, y); + checkBoxChatStaysActive.mouseClick(x, y); + checkBoxTimeDisplay.mouseClick(x, y); + checkBoxLuaDisableSecuritySandbox.mouseClick(x, y); + } + } + + void + MenuStateOptions::mouseMove(int x, int y, const MouseState * ms) { + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + if (luaMessageBox.getEnabled()) { + luaMessageBox.mouseMove(x, y); + } + + buttonOk.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + buttonKeyboardSetup.mouseMove(x, y); + buttonAudioSection.mouseMove(x, y); + buttonNetworkSettings.mouseMove(x, y); + buttonMiscSection.mouseMove(x, y); + buttonVideoSection.mouseMove(x, y); + buttonGetNewLanguageFiles.mouseMove(x, y); + buttonDeleteNewLanguageFiles.mouseMove(x, y); + listBoxLang.mouseMove(x, y); + listBoxLang.mouseMove(x, y); + listFontSizeAdjustment.mouseMove(x, y); + listBoxScreenShotType.mouseMove(x, y); + checkBoxDisableScreenshotConsoleText.mouseMove(x, y); + checkBoxMouseMoveScrollsWorld.mouseMove(x, y); + listCameraMoveSpeed.mouseMove(x, y); + listBoxHealthBars.mouseMove(x, y); + checkBoxVisibleHud.mouseMove(x, y); + checkBoxChatStaysActive.mouseMove(x, y); + checkBoxTimeDisplay.mouseMove(x, y); + checkBoxLuaDisableSecuritySandbox.mouseMove(x, y); + checkBoxCustomTranslation.mouseMove(x, y); + } + + bool + MenuStateOptions::isInSpecialKeyCaptureEvent() { + return (activeInputLabel != NULL); + } + + void + MenuStateOptions::keyDown(SDL_KeyboardEvent key) { + if (activeInputLabel != NULL) { + keyDownEditLabel(key, &activeInputLabel); + } + } + + bool + MenuStateOptions::textInput(std::string text) { + if (activeInputLabel != NULL) { + //printf("[%d]\n",c); fflush(stdout); + if (&labelPlayerName == activeInputLabel || + &labelTransifexUser == activeInputLabel || + &labelTransifexPwd == activeInputLabel || + &labelTransifexI18N == activeInputLabel) { + return textInputEditLabel(text, &activeInputLabel); + } + } + return false; + } + + void + MenuStateOptions::keyPress(SDL_KeyboardEvent c) { + if (activeInputLabel != NULL) { + keyPressEditLabel(c, &activeInputLabel); + } else { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), c) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + } + } + + void + MenuStateOptions::render() { + Renderer & renderer = Renderer::getInstance(); + // char szBuf[8096]=""; + // snprintf(szBuf,8096,"\nIn [%s::%s Line: %d]\n\nRender options menu [%p]!\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this); + // printf(szBuf); + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else if (luaMessageBox.getEnabled()) { + renderer.renderMessageBox(&luaMessageBox); + } else { + renderer.renderButton(&buttonOk); + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonKeyboardSetup); + renderer.renderButton(&buttonVideoSection); + renderer.renderButton(&buttonAudioSection); + renderer.renderButton(&buttonMiscSection); + renderer.renderButton(&buttonNetworkSettings); + + renderer.renderLabel(&labelCustomTranslation); + renderer.renderCheckBox(&checkBoxCustomTranslation); + + if (buttonGetNewLanguageFiles.getEnabled()) + renderer.renderButton(&buttonGetNewLanguageFiles); + if (buttonDeleteNewLanguageFiles.getEnabled()) + renderer.renderButton(&buttonDeleteNewLanguageFiles); + if (labelTransifexUserLabel.getEnabled()) + renderer.renderLabel(&labelTransifexUserLabel); + if (labelTransifexPwdLabel.getEnabled()) + renderer.renderLabel(&labelTransifexPwdLabel); + if (labelTransifexI18NLabel.getEnabled()) + renderer.renderLabel(&labelTransifexI18NLabel); + if (labelTransifexUser.getEnabled()) + renderer.renderLabel(&labelTransifexUser); + if (labelTransifexPwd.getEnabled()) + renderer.renderLabel(&labelTransifexPwd); + if (labelTransifexI18N.getEnabled()) + renderer.renderLabel(&labelTransifexI18N); + + renderer.renderListBox(&listBoxLang); + renderer.renderLabel(&labelLang); + renderer.renderLabel(&labelPlayerNameLabel); + renderer.renderLabel(&labelPlayerName); + renderer.renderListBox(&listFontSizeAdjustment); + renderer.renderLabel(&labelFontSizeAdjustment); + + renderer.renderLabel(&labelScreenShotType); + renderer.renderListBox(&listBoxScreenShotType); + + renderer.renderLabel(&labelDisableScreenshotConsoleText); + renderer.renderCheckBox(&checkBoxDisableScreenshotConsoleText); + + renderer.renderLabel(&labelMouseMoveScrollsWorld); + renderer.renderCheckBox(&checkBoxMouseMoveScrollsWorld); + renderer.renderLabel(&labelCameraMoveSpeed); + renderer.renderListBox(&listCameraMoveSpeed); + + renderer.renderLabel(&labelVisibleHud); + renderer.renderLabel(&labelHealthBars); + renderer.renderListBox(&listBoxHealthBars); + renderer.renderLabel(&labelChatStaysActive); + renderer.renderLabel(&labelTimeDisplay); + + renderer.renderLabel(&labelLuaDisableSecuritySandbox); + renderer.renderCheckBox(&checkBoxLuaDisableSecuritySandbox); + + renderer.renderCheckBox(&checkBoxVisibleHud); + renderer.renderCheckBox(&checkBoxChatStaysActive); + renderer.renderCheckBox(&checkBoxTimeDisplay); + + } + + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } + + void + MenuStateOptions::saveConfig() { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + setActiveInputLable(NULL); + + if (labelPlayerName.getText().length() > 0) { + config.setString("NetPlayerName", labelPlayerName.getText()); + } + //Copy values + map < string, string >::iterator iterMap = languageList.begin(); + std::advance(iterMap, listBoxLang.getSelectedItemIndex()); + + config.setString("Lang", iterMap->first); + lang.loadGameStrings(config.getString("Lang")); + + config.setString("FontSizeAdjustment", + listFontSizeAdjustment.getSelectedItem()); + config.setString("ScreenShotFileType", + listBoxScreenShotType.getSelectedItem()); + + config.setBool("DisableScreenshotConsoleText", + !checkBoxDisableScreenshotConsoleText.getValue()); + config.setBool("MouseMoveScrollsWorld", + checkBoxMouseMoveScrollsWorld.getValue()); + config.setString("CameraMoveSpeed", + listCameraMoveSpeed.getSelectedItem()); + + int + hpIndex = listBoxHealthBars.getSelectedItemIndex(); + int + hpMode = hbvUndefined; + switch (hpIndex) { + case 0: + hpMode = hbvUndefined; + break; + case 1: + hpMode = hbvOff; + break; + case 2: + hpMode = hbvAlways; + break; + case 3: + hpMode = hbvIfNeeded; + break; + case 4: + hpMode = hbvSelected; + break; + case 5: + hpMode = hbvSelected | hbvIfNeeded; + break; + default: + hpMode = hbvUndefined; + break; + } + + config.setInt("HealthBarMode", hpMode); + config.setBool("VisibleHud", checkBoxVisibleHud.getValue()); + config.setBool("ChatStaysActive", checkBoxChatStaysActive.getValue()); + config.setBool("TimeDisplay", checkBoxTimeDisplay.getValue()); + + config.setBool("DisableLuaSandbox", + checkBoxLuaDisableSecuritySandbox.getValue()); + config.save(); + + if (config.getBool("DisableLuaSandbox", "false") == true) { + LuaScript::setDisableSandbox(true); + } + Renderer::getInstance().loadConfig(); + console.addLine(lang.getString("SettingsSaved")); + } + + void + MenuStateOptions::setActiveInputLable(GraphicLabel * newLable) { + MenuState::setActiveInputLabel(newLable, &activeInputLabel); + + if (newLable == &labelTransifexPwd) { + labelTransifexPwd.setIsPassword(false); + } else { + labelTransifexPwd.setIsPassword(true); + } + } + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_options.h b/source/glest_game/menu/menu_state_options.h index 48653bb3a..4ce0f266e 100644 --- a/source/glest_game/menu/menu_state_options.h +++ b/source/glest_game/menu/menu_state_options.h @@ -15,110 +15,108 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateOptions -// =============================== + // =============================== + // class MenuStateOptions + // =============================== - class MenuStateOptions:public MenuState - { - private: - GraphicButton buttonOk; - GraphicButton buttonReturn; + class MenuStateOptions :public MenuState { + private: + GraphicButton buttonOk; + GraphicButton buttonReturn; - GraphicLabel labelLang; - GraphicListBox listBoxLang; - GraphicLabel labelPlayerName; - GraphicLabel labelPlayerNameLabel; - GraphicLabel *activeInputLabel; + GraphicLabel labelLang; + GraphicListBox listBoxLang; + GraphicLabel labelPlayerName; + GraphicLabel labelPlayerNameLabel; + GraphicLabel *activeInputLabel; - GraphicButton buttonKeyboardSetup; // configure the keyboard - GraphicButton buttonVideoSection; - GraphicButton buttonAudioSection; - GraphicButton buttonMiscSection; - GraphicButton buttonNetworkSettings; + GraphicButton buttonKeyboardSetup; // configure the keyboard + GraphicButton buttonVideoSection; + GraphicButton buttonAudioSection; + GraphicButton buttonMiscSection; + GraphicButton buttonNetworkSettings; - GraphicLabel labelFontSizeAdjustment; - GraphicListBox listFontSizeAdjustment; + GraphicLabel labelFontSizeAdjustment; + GraphicListBox listFontSizeAdjustment; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - GraphicLabel labelScreenShotType; - GraphicListBox listBoxScreenShotType; + GraphicLabel labelScreenShotType; + GraphicListBox listBoxScreenShotType; - GraphicLabel labelDisableScreenshotConsoleText; - GraphicCheckBox checkBoxDisableScreenshotConsoleText; + GraphicLabel labelDisableScreenshotConsoleText; + GraphicCheckBox checkBoxDisableScreenshotConsoleText; - GraphicLabel labelMouseMoveScrollsWorld; - GraphicCheckBox checkBoxMouseMoveScrollsWorld; + GraphicLabel labelMouseMoveScrollsWorld; + GraphicCheckBox checkBoxMouseMoveScrollsWorld; - GraphicLabel labelCameraMoveSpeed; - GraphicListBox listCameraMoveSpeed; + GraphicLabel labelCameraMoveSpeed; + GraphicListBox listCameraMoveSpeed; - GraphicLabel labelVisibleHud; - GraphicCheckBox checkBoxVisibleHud; - GraphicLabel labelHealthBars; - GraphicListBox listBoxHealthBars; + GraphicLabel labelVisibleHud; + GraphicCheckBox checkBoxVisibleHud; + GraphicLabel labelHealthBars; + GraphicListBox listBoxHealthBars; - GraphicLabel labelTimeDisplay; - GraphicCheckBox checkBoxTimeDisplay; - GraphicLabel labelChatStaysActive; - GraphicCheckBox checkBoxChatStaysActive; + GraphicLabel labelTimeDisplay; + GraphicCheckBox checkBoxTimeDisplay; + GraphicLabel labelChatStaysActive; + GraphicCheckBox checkBoxChatStaysActive; - GraphicLabel labelLuaDisableSecuritySandbox; - GraphicCheckBox checkBoxLuaDisableSecuritySandbox; + GraphicLabel labelLuaDisableSecuritySandbox; + GraphicCheckBox checkBoxLuaDisableSecuritySandbox; - GraphicMessageBox luaMessageBox; - int luaMessageBoxState; + GraphicMessageBox luaMessageBox; + int luaMessageBoxState; - map < string, string > languageList; + map < string, string > languageList; - GraphicLabel labelCustomTranslation; - GraphicCheckBox checkBoxCustomTranslation; + GraphicLabel labelCustomTranslation; + GraphicCheckBox checkBoxCustomTranslation; - GraphicButton buttonGetNewLanguageFiles; - GraphicButton buttonDeleteNewLanguageFiles; - GraphicLabel labelTransifexUserLabel; - GraphicLabel labelTransifexUser; - GraphicLabel labelTransifexPwdLabel; - GraphicLabel labelTransifexPwd; - GraphicLabel labelTransifexI18NLabel; - GraphicLabel labelTransifexI18N; + GraphicButton buttonGetNewLanguageFiles; + GraphicButton buttonDeleteNewLanguageFiles; + GraphicLabel labelTransifexUserLabel; + GraphicLabel labelTransifexUser; + GraphicLabel labelTransifexPwdLabel; + GraphicLabel labelTransifexPwd; + GraphicLabel labelTransifexI18NLabel; + GraphicLabel labelTransifexI18N; - ProgramState **parentUI; + ProgramState **parentUI; - public: - MenuStateOptions (Program * program, MainMenu * mainMenu, - ProgramState ** parentUI = NULL); - virtual ~ MenuStateOptions (); + public: + MenuStateOptions(Program * program, MainMenu * mainMenu, + ProgramState ** parentUI = NULL); + virtual ~MenuStateOptions(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - virtual bool textInput (std::string text); - virtual void keyDown (SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - virtual bool isInSpecialKeyCaptureEvent (); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + virtual bool textInput(std::string text); + virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + virtual bool isInSpecialKeyCaptureEvent(); - virtual void reloadUI (); + virtual void reloadUI(); - private: - void saveConfig (); - void setActiveInputLable (GraphicLabel * newLable); - void showMessageBox (const string & text, const string & header, - bool toggle); - void showLuaMessageBox (const string & text, const string & header, - bool toggle); + private: + void saveConfig(); + void setActiveInputLable(GraphicLabel * newLable); + void showMessageBox(const string & text, const string & header, + bool toggle); + void showLuaMessageBox(const string & text, const string & header, + bool toggle); - void setupTransifexUI (); - }; + void setupTransifexUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_options_graphics.cpp b/source/glest_game/menu/menu_state_options_graphics.cpp index b43f80301..6ae1a1471 100644 --- a/source/glest_game/menu/menu_state_options_graphics.cpp +++ b/source/glest_game/menu/menu_state_options_graphics.cpp @@ -30,1170 +30,1079 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class MenuStateOptions -// ===================================================== - MenuStateOptionsGraphics::MenuStateOptionsGraphics (Program * program, - MainMenu * mainMenu, - ProgramState ** - parentUI): - MenuState (program, mainMenu, "config"), - buttonOk ("Options_Graphics", "buttonOk"), - buttonReturn ("Options_Graphics", "buttonReturn"), - buttonAutoConfig ("Options_Graphics", "buttonAutoConfig"), - buttonVideoInfo ("Options_Graphics", "buttonVideoInfo"), - buttonKeyboardSetup ("Options_Graphics", "buttonKeyboardSetup"), - buttonVideoSection ("Options_Graphics", "buttonVideoSection"), - buttonAudioSection ("Options_Graphics", "buttonAudioSection"), - buttonMiscSection ("Options_Graphics", "buttonMiscSection"), - buttonNetworkSettings ("Options_Graphics", "buttonNetworkSettings"), - labelShadows ("Options_Graphics", "labelShadows"), - listBoxShadows ("Options_Graphics", "listBoxShadows"), - labelFilter ("Options_Graphics", "labelFilter"), - listBoxFilter ("Options_Graphics", "listBoxFilter"), - labelFilterMaxAnisotropy ("Options_Graphics", "labelFilterMaxAnisotropy"), - listBoxFilterMaxAnisotropy ("Options_Graphics", - "listBoxFilterMaxAnisotropy"), - labelTextures3D ("Options_Graphics", "labelTextures3D"), - checkBoxTextures3D ("Options_Graphics", "checkBoxTextures3D"), - labelLights ("Options_Graphics", "labelLights"), - listBoxLights ("Options_Graphics", "listBoxLights"), - labelUnitParticles ("Options_Graphics", "labelUnitParticles"), - checkBoxUnitParticles ("Options_Graphics", "checkBoxUnitParticles"), - labelTilesetParticles ("Options_Graphics", "labelTilesetParticles"), - checkBoxTilesetParticles ("Options_Graphics", "checkBoxTilesetParticles"), - labelAnimatedTilesetObjects ("Options_Graphics", - "labelAnimatedTilesetObjects"), - listBoxAnimatedTilesetObjects ("Options_Graphics", - "listBoxAnimatedTilesetObjects"), - labelScreenModes ("Options_Graphics", "labelScreenModes"), - listBoxScreenModes ("Options_Graphics", "listBoxScreenModes"), - labelFullscreenWindowed ("Options_Graphics", "labelFullscreenWindowed"), - checkBoxFullscreenWindowed ("Options_Graphics", - "checkBoxFullscreenWindowed"), - labelMapPreview ("Options_Graphics", "labelMapPreview"), - checkBoxMapPreview ("Options_Graphics", "checkBoxMapPreview"), - mainMessageBox ("Options_Graphics", "mainMessageBox"), - labelEnableTextureCompression ("Options_Graphics", - "labelEnableTextureCompression"), - checkBoxEnableTextureCompression ("Options_Graphics", - "checkBoxEnableTextureCompression"), - labelRainEffect ("Options_Graphics", "labelRainEffect"), - labelRainEffectSeparator ("Options_Graphics", "labelRainEffectSeparator"), - checkBoxRainEffect ("Options_Graphics", "checkBoxRainEffect"), - checkBoxRainEffectMenu ("Options_Graphics", "checkBoxRainEffectMenu"), - labelGammaCorrection ("Options_Graphics", "labelGammaCorrection"), - listBoxGammaCorrection ("Options_Graphics", "listBoxGammaCorrection"), - labelShadowIntensity ("Options_Graphics", "labelShadowIntensity"), - listBoxShadowIntensity ("Options_Graphics", "listBoxShadowIntensity"), - labelShadowTextureSize ("Options_Graphics", "labelShadowTextureSize"), - listBoxShadowTextureSize ("Options_Graphics", "listBoxShadowTextureSize"), - labelVideos ("Options_Graphics", "labelVideos"), - checkBoxVideos ("Options_Graphics", "checkBoxVideos"), - labelSelectionType ("Options_Graphics", "labelSelectionType"), - listBoxSelectionType ("Options_Graphics", "listBoxSelectionType") - { - try - { - containerName = "Options_Graphics"; - this-> - parentUI = parentUI; - Lang & - lang = Lang::getInstance (); - Config & - config = Config::getInstance (); - this-> - console. - setOnlyChatMessagesInStoredLines (false); - screenModeChangedTimer = time (NULL); // just init - - ::Shared::PlatformCommon::getFullscreenVideoModes (&modeInfos, - !config. - getBool - ("Windowed")); - - int - leftLabelStart = 100; - int - leftColumnStart = leftLabelStart + 300; - int - buttonRowPos = 50; - int - buttonStartPos = 170; - int - lineOffset = 30; - int - tabButtonWidth = 200; - int - tabButtonHeight = 30; - - mainMessageBox. - init (lang.getString ("Ok")); - mainMessageBox. - setEnabled (false); - mainMessageBoxState = 0; - - buttonAudioSection. - init (0, 720, tabButtonWidth, tabButtonHeight); - buttonAudioSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonAudioSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonAudioSection. - setText (lang.getString ("Audio")); - // Video Section - buttonVideoSection. - init (200, 700, tabButtonWidth, tabButtonHeight + 20); - buttonVideoSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonVideoSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonVideoSection. - setText (lang.getString ("Video")); - //MiscSection - buttonMiscSection. - init (400, 720, tabButtonWidth, tabButtonHeight); - buttonMiscSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonMiscSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonMiscSection. - setText (lang.getString ("Misc")); - //NetworkSettings - buttonNetworkSettings. - init (600, 720, tabButtonWidth, tabButtonHeight); - buttonNetworkSettings. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonNetworkSettings. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonNetworkSettings. - setText (lang.getString ("Network")); - - //KeyboardSetup - buttonKeyboardSetup. - init (800, 720, tabButtonWidth, tabButtonHeight); - buttonKeyboardSetup. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonKeyboardSetup. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonKeyboardSetup. - setText (lang.getString ("Keyboardsetup")); - - int - currentLine = 650; // reset line pos - int - currentLabelStart = leftLabelStart; // set to right side - int - currentColumnStart = leftColumnStart; // set to right side - - //resolution - labelScreenModes. - init (currentLabelStart, currentLine); - labelScreenModes. - setText (lang.getString ("Resolution")); - - listBoxScreenModes. - init (currentColumnStart, currentLine, 200); - - string - currentResString = config.getString ("ScreenWidth") + "x" + - config.getString ("ScreenHeight") + "-" + - intToStr (config.getInt ("ColorBits")); - bool - currentResolutionFound = false; - for (vector < ModeInfo >::const_iterator it = modeInfos.begin (); - it != modeInfos.end (); ++it) - { - if ((*it).getString () == currentResString) - { - currentResolutionFound = true; - } - listBoxScreenModes. - pushBackItem ((*it).getString ()); - } - if (currentResolutionFound == false) - { - listBoxScreenModes.pushBackItem (currentResString); - } - listBoxScreenModes.setSelectedItem (currentResString); - currentLine -= lineOffset; - - - //FullscreenWindowed - labelFullscreenWindowed.init (currentLabelStart, currentLine); - - checkBoxFullscreenWindowed.init (currentColumnStart, currentLine); - labelFullscreenWindowed.setText (lang.getString ("Windowed")); - checkBoxFullscreenWindowed.setValue (config.getBool ("Windowed")); - currentLine -= lineOffset; - - //gammaCorrection - labelGammaCorrection.init (currentLabelStart, currentLine); - labelGammaCorrection.setText (lang.getString ("GammaCorrection")); - - listBoxGammaCorrection.init (currentColumnStart, currentLine, 200); - for (float f = 0.5; f < 3.0f; f = f + 0.1f) - { - listBoxGammaCorrection.pushBackItem (floatToStr (f)); - } - float - gammaValue = config.getFloat ("GammaValue", "1.0"); - if (gammaValue == 0.0f) - gammaValue = 1.0f; - listBoxGammaCorrection.setSelectedItem (floatToStr (gammaValue), - false); - - currentLine -= lineOffset; - - //filter - labelFilter.init (currentLabelStart, currentLine); - labelFilter.setText (lang.getString ("Filter")); - - listBoxFilter.init (currentColumnStart, currentLine, 200); - listBoxFilter.pushBackItem ("Bilinear"); - listBoxFilter.pushBackItem ("Trilinear"); - listBoxFilter.setSelectedItem (config.getString ("Filter")); - currentLine -= lineOffset; - - //FilterMaxAnisotropy - labelFilterMaxAnisotropy.init (currentLabelStart, currentLine); - labelFilterMaxAnisotropy.setText (lang. - getString ("FilterMaxAnisotropy")); - - listBoxFilterMaxAnisotropy.init (currentColumnStart, currentLine, - 200); - listBoxFilterMaxAnisotropy.pushBackItem ("1"); - listBoxFilterMaxAnisotropy.pushBackItem ("2"); - listBoxFilterMaxAnisotropy.pushBackItem ("4"); - listBoxFilterMaxAnisotropy.pushBackItem ("8"); - listBoxFilterMaxAnisotropy.pushBackItem ("16"); - listBoxFilterMaxAnisotropy.setSelectedItem (config. - getString - ("FilterMaxAnisotropy", - "1")); - currentLine -= lineOffset; - - //selectionType - labelSelectionType.init (currentLabelStart, currentLine); - labelSelectionType.setText (lang.getString ("SelectionType")); - - listBoxSelectionType.init (currentColumnStart, currentLine, 250); - listBoxSelectionType.pushBackItem ("SelectBuffer (nvidia)"); - listBoxSelectionType.pushBackItem ("ColorPicking (default)"); - listBoxSelectionType.pushBackItem ("FrustumPicking (bad)"); - - const string - selectionType = - toLower (config.getString ("SelectionType", Config::colorPicking)); - if (selectionType == Config::colorPicking) - listBoxSelectionType.setSelectedItemIndex (1); - else if (selectionType == Config::frustumPicking) - listBoxSelectionType.setSelectedItemIndex (2); - else - listBoxSelectionType.setSelectedItemIndex (0); - currentLine -= lineOffset; - - //shadows - labelShadows.init (currentLabelStart, currentLine); - labelShadows.setText (lang.getString ("Shadows")); - - listBoxShadows.init (currentColumnStart, currentLine, 250); - for (int i = 0; i < Renderer::sCount; ++i) - { - listBoxShadows.pushBackItem (lang. - getString (Renderer:: - shadowsToStr (static_cast < - Renderer:: - Shadows > - (i)))); - } - string - str = config.getString ("Shadows"); - listBoxShadows. - setSelectedItemIndex (clamp - (Renderer::strToShadows (str), 0, - Renderer::sCount - 1)); - currentLine -= lineOffset; - - //shadows - labelShadowTextureSize.init (currentLabelStart, currentLine); - labelShadowTextureSize.setText (lang.getString ("ShadowTextureSize")); - - listBoxShadowTextureSize.init (currentColumnStart, currentLine, 200); - listBoxShadowTextureSize.pushBackItem ("256"); - listBoxShadowTextureSize.pushBackItem ("512"); - listBoxShadowTextureSize.pushBackItem ("1024"); - listBoxShadowTextureSize.setSelectedItemIndex (1, false); - listBoxShadowTextureSize. - setSelectedItem (intToStr - (config.getInt ("ShadowTextureSize", "512")), - false); - currentLine -= lineOffset; - - //shadows - labelShadowIntensity.init (currentLabelStart, currentLine); - labelShadowIntensity.setText (lang.getString ("ShadowIntensity")); - - listBoxShadowIntensity.init (currentColumnStart, currentLine, 200); - for (float f = 0.5f; f < 3.0f; f = f + 0.1f) - { - listBoxShadowIntensity.pushBackItem (floatToStr (f)); - } - float - shadowIntensity = config.getFloat ("ShadowIntensity", "1.0"); - if (shadowIntensity <= 0.0f) - shadowIntensity = 1.0f; - listBoxShadowIntensity.setSelectedItem (floatToStr (shadowIntensity), - false); - - currentLine -= lineOffset; - - //textures 3d - labelTextures3D.init (currentLabelStart, currentLine); - - checkBoxTextures3D.init (currentColumnStart, currentLine); - labelTextures3D.setText (lang.getString ("Textures3D")); - checkBoxTextures3D.setValue (config.getBool ("Textures3D")); - currentLine -= lineOffset; - - //lights - labelLights.init (currentLabelStart, currentLine); - labelLights.setText (lang.getString ("MaxLights")); - - listBoxLights.init (currentColumnStart, currentLine, 80); - for (int i = 1; i <= 8; ++i) - { - listBoxLights.pushBackItem (intToStr (i)); - } - listBoxLights. - setSelectedItemIndex (clamp - (config.getInt ("MaxLights") - 1, 0, 7)); - currentLine -= lineOffset; - - //unit particles - labelUnitParticles.init (currentLabelStart, currentLine); - labelUnitParticles.setText (lang.getString ("ShowUnitParticles")); - - checkBoxUnitParticles.init (currentColumnStart, currentLine); - checkBoxUnitParticles.setValue (config. - getBool ("UnitParticles", "true")); - currentLine -= lineOffset; - - //tileset particles - labelTilesetParticles.init (currentLabelStart, currentLine); - labelTilesetParticles.setText (lang. - getString ("ShowTilesetParticles")); - - checkBoxTilesetParticles.init (currentColumnStart, currentLine); - checkBoxTilesetParticles.setValue (config. - getBool ("TilesetParticles", - "true")); - currentLine -= lineOffset; - - //animated tileset objects - labelAnimatedTilesetObjects.init (currentLabelStart, currentLine); - labelAnimatedTilesetObjects.setText (lang. - getString - ("AnimatedTilesetObjects")); - - listBoxAnimatedTilesetObjects.init (currentColumnStart, currentLine, - 80); - listBoxAnimatedTilesetObjects.pushBackItem ("0"); - listBoxAnimatedTilesetObjects.pushBackItem ("10"); - listBoxAnimatedTilesetObjects.pushBackItem ("25"); - listBoxAnimatedTilesetObjects.pushBackItem ("50"); - listBoxAnimatedTilesetObjects.pushBackItem ("100"); - listBoxAnimatedTilesetObjects.pushBackItem ("300"); - listBoxAnimatedTilesetObjects.pushBackItem ("500"); - listBoxAnimatedTilesetObjects.pushBackItem ("∞"); - listBoxAnimatedTilesetObjects.setSelectedItem ("∞", true); - listBoxAnimatedTilesetObjects.setSelectedItem (config. - getString - ("AnimatedTilesetObjects", - "-1"), false); - currentLine -= lineOffset; - - //unit particles - labelMapPreview.init (currentLabelStart, currentLine); - labelMapPreview.setText (lang.getString ("ShowMapPreview")); - - checkBoxMapPreview.init (currentColumnStart, currentLine); - checkBoxMapPreview.setValue (config.getBool ("MapPreview", "true")); - currentLine -= lineOffset; - - // Texture Compression flag - labelEnableTextureCompression.init (currentLabelStart, currentLine); - labelEnableTextureCompression.setText (lang. - getString - ("EnableTextureCompression")); - - checkBoxEnableTextureCompression.init (currentColumnStart, - currentLine); - checkBoxEnableTextureCompression.setValue (config. - getBool - ("EnableTextureCompression", - "false")); - currentLine -= lineOffset; - - labelRainEffect.init (currentLabelStart, currentLine); - labelRainEffect.setText (lang.getString ("RainEffectMenuGame")); - - checkBoxRainEffectMenu.init (currentColumnStart, currentLine); - checkBoxRainEffectMenu.setValue (config. - getBool ("RainEffectMenu", "true")); - - labelRainEffectSeparator.init (currentColumnStart + 30, currentLine); - labelRainEffectSeparator.setText ("/"); - - checkBoxRainEffect.init (currentColumnStart + 42, currentLine); - checkBoxRainEffect.setValue (config.getBool ("RainEffect", "true")); - currentLine -= lineOffset; - - labelVideos.init (currentLabelStart, currentLine); - labelVideos.setText (lang.getString ("EnableVideos")); - - checkBoxVideos.init (currentColumnStart, currentLine); - checkBoxVideos.setValue (config.getBool ("EnableVideos", "true")); - - // end - - // external server port - - // buttons - buttonOk.init (buttonStartPos, buttonRowPos, 100); - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - - buttonReturn.init (buttonStartPos + 110, buttonRowPos, 100); - buttonAutoConfig.setText (lang.getString ("AutoConfig")); - - buttonAutoConfig.init (buttonStartPos + 230, buttonRowPos, 175); - - buttonVideoInfo.setText (lang.getString ("VideoInfo")); - buttonVideoInfo.init (buttonStartPos + 415, buttonRowPos, 175); // was 620 - - GraphicComponent::applyAllCustomProperties (containerName); - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error loading options: %s\n", - __FILE__, __FUNCTION__, __LINE__, - e.what ()); - throw - megaglest_runtime_error (string ("Error loading options msg: ") + - e.what ()); - } - } - - void - MenuStateOptionsGraphics::reloadUI () - { - Lang & lang = Lang::getInstance (); - - mainMessageBox.init (lang.getString ("Ok")); - - buttonAudioSection.setText (lang.getString ("Audio")); - buttonVideoSection.setText (lang.getString ("Video")); - buttonMiscSection.setText (lang.getString ("Misc")); - buttonNetworkSettings.setText (lang.getString ("Network")); - - std::vector < string > listboxData; - listboxData.push_back ("None"); - listboxData.push_back ("OpenAL"); - - labelScreenModes.setText (lang.getString ("Resolution")); - - labelFullscreenWindowed.setText (lang.getString ("Windowed")); - labelFilter.setText (lang.getString ("Filter")); - - listboxData.clear (); - listboxData.push_back ("Bilinear"); - listboxData.push_back ("Trilinear"); - listBoxFilter.setItems (listboxData); - - labelFilterMaxAnisotropy.setText (lang. - getString ("FilterMaxAnisotropy")); - listboxData.clear (); - listboxData.push_back ("1"); - listboxData.push_back ("2"); - listboxData.push_back ("4"); - listboxData.push_back ("8"); - listboxData.push_back ("16"); - listBoxFilterMaxAnisotropy.setItems (listboxData); - - listboxData.clear (); - for (float f = 0.0; f < 2.1f; f = f + 0.1f) - { - listboxData.push_back (floatToStr (f)); - } - listBoxGammaCorrection.setItems (listboxData); - - - listboxData.clear (); - for (float f = 0.5; f < 3.0f; f = f + 0.1f) - { - listboxData.push_back (floatToStr (f)); - } - listBoxShadowIntensity.setItems (listboxData); - - - labelShadows.setText (lang.getString ("Shadows")); - labelShadowTextureSize.setText (lang.getString ("ShadowTextureSize")); - - labelShadowIntensity.setText (lang.getString ("ShadowIntensity")); - labelGammaCorrection.setText (lang.getString ("GammaCorrection")); - - listboxData.clear (); - for (int i = 0; i < Renderer::sCount; ++i) - { - listboxData.push_back (lang. - getString (Renderer:: - shadowsToStr (static_cast < - Renderer::Shadows > - (i)))); - } - listBoxShadows.setItems (listboxData); - - labelTextures3D.setText (lang.getString ("Textures3D")); - labelLights.setText (lang.getString ("MaxLights")); - labelUnitParticles.setText (lang.getString ("ShowUnitParticles")); - labelTilesetParticles.setText (lang.getString ("ShowTilesetParticles")); - labelAnimatedTilesetObjects.setText (lang. - getString - ("AnimatedTilesetObjects")); - labelMapPreview.setText (lang.getString ("ShowMapPreview")); - labelEnableTextureCompression.setText (lang. - getString - ("EnableTextureCompression")); - labelRainEffect.setText (lang.getString ("RainEffectMenuGame")); - labelVideos.setText (lang.getString ("EnableVideos")); - - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - buttonAutoConfig.setText (lang.getString ("AutoConfig")); - buttonVideoInfo.setText (lang.getString ("VideoInfo")); - - labelSelectionType.setText (lang.getString ("SelectionType")); - } - - - void - MenuStateOptionsGraphics::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void - MenuStateOptionsGraphics::revertScreenMode () - { - Config & config = Config::getInstance (); - //!!! - // Revert resolution or fullscreen - checkBoxFullscreenWindowed.setValue (config.getBool ("Windowed")); - string - currentResString = config.getString ("ScreenWidth") + "x" + - config.getString ("ScreenHeight") + "-" + - intToStr (config.getInt ("ColorBits")); - listBoxScreenModes.setSelectedItem (currentResString); - - - changeVideoModeFullScreen (!config.getBool ("Windowed")); - WindowGl * - window = this->program->getWindow (); - window->ChangeVideoMode (true, - config.getInt ("ScreenWidth"), - config.getInt ("ScreenHeight"), - !config.getBool ("Windowed"), - config.getInt ("ColorBits"), - config.getInt ("DepthBits"), - config.getInt ("StencilBits"), - config.getBool ("HardwareAcceleration", - "false"), - config.getBool ("FullScreenAntiAliasing", - "false"), - config.getFloat ("GammaValue", "0.0")); - Metrics::reload (this->program->getWindow ()->getScreenWidth (), - this->program->getWindow ()->getScreenHeight ()); - window->setText (config.getString ("WindowTitle", "ZetaGlest")); - - this->mainMenu->init (); - } - - void - MenuStateOptionsGraphics::update () - { - if (mainMessageBox.getEnabled () && (mainMessageBoxState == 1)) - { - int - waitTime = 10; - if ((time (NULL) - screenModeChangedTimer > waitTime)) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - - revertScreenMode (); - } - else - { - Lang & lang = Lang::getInstance (); - int - timeToShow = waitTime - time (NULL) + screenModeChangedTimer; - // show timer in button - mainMessageBox.getButton (0)->setText (lang.getString ("Ok") + - " (" + - intToStr (timeToShow) + ")"); - } - } - } - - void - MenuStateOptionsGraphics::mouseClick (int x, int y, - MouseButton mouseButton) - { - - Config & config = Config::getInstance (); - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - - - if (mainMessageBox.getEnabled ()) - { - int - button = 0; - - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - saveConfig (); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - //mainMenu->setState(new MenuStateOptions(program, mainMenu)); - } - else - { - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - } - } - else - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - - revertScreenMode (); - } - } - } - } - else if (buttonOk.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - Lang & lang = Lang::getInstance (); - bool - selectedFullscreenWindowed = checkBoxFullscreenWindowed.getValue (); - string - currentResolution = - config.getString ("ScreenWidth") + "x" + - config.getString ("ScreenHeight") + "-" + - intToStr (config.getInt ("ColorBits")); - string - selectedResolution = listBoxScreenModes.getSelectedItem (); - bool - currentFullscreenWindowed = config.getBool ("Windowed"); - if (currentResolution != selectedResolution - || currentFullscreenWindowed != selectedFullscreenWindowed) - { - - changeVideoModeFullScreen (!selectedFullscreenWindowed); - const ModeInfo * - selectedMode = NULL; - for (vector < ModeInfo >::const_iterator it = modeInfos.begin (); - it != modeInfos.end (); ++it) - { - if ((*it).getString () == selectedResolution) - { - //config.setInt("ScreenWidth",(*it).width); - //config.setInt("ScreenHeight",(*it).height); - //config.setInt("ColorBits",(*it).depth); - selectedMode = &(*it); - } - } - if (selectedMode == NULL) - { // if we cannot find the selectedResolution we try it with current one - for (vector < ModeInfo >::const_iterator it = modeInfos.begin (); - it != modeInfos.end (); ++it) - { - if ((*it).getString () == currentResolution) - { - //config.setInt("ScreenWidth",(*it).width); - //config.setInt("ScreenHeight",(*it).height); - //config.setInt("ColorBits",(*it).depth); - selectedMode = &(*it); - } - } - } - if (selectedMode == NULL) - { - throw - megaglest_runtime_error ("selectedMode == NULL"); - } + Glest { + namespace + Game { + + // ===================================================== + // class MenuStateOptions + // ===================================================== + MenuStateOptionsGraphics::MenuStateOptionsGraphics(Program * program, + MainMenu * mainMenu, + ProgramState ** + parentUI) : + MenuState(program, mainMenu, "config"), + buttonOk("Options_Graphics", "buttonOk"), + buttonReturn("Options_Graphics", "buttonReturn"), + buttonAutoConfig("Options_Graphics", "buttonAutoConfig"), + buttonVideoInfo("Options_Graphics", "buttonVideoInfo"), + buttonKeyboardSetup("Options_Graphics", "buttonKeyboardSetup"), + buttonVideoSection("Options_Graphics", "buttonVideoSection"), + buttonAudioSection("Options_Graphics", "buttonAudioSection"), + buttonMiscSection("Options_Graphics", "buttonMiscSection"), + buttonNetworkSettings("Options_Graphics", "buttonNetworkSettings"), + labelShadows("Options_Graphics", "labelShadows"), + listBoxShadows("Options_Graphics", "listBoxShadows"), + labelFilter("Options_Graphics", "labelFilter"), + listBoxFilter("Options_Graphics", "listBoxFilter"), + labelFilterMaxAnisotropy("Options_Graphics", "labelFilterMaxAnisotropy"), + listBoxFilterMaxAnisotropy("Options_Graphics", + "listBoxFilterMaxAnisotropy"), + labelTextures3D("Options_Graphics", "labelTextures3D"), + checkBoxTextures3D("Options_Graphics", "checkBoxTextures3D"), + labelLights("Options_Graphics", "labelLights"), + listBoxLights("Options_Graphics", "listBoxLights"), + labelUnitParticles("Options_Graphics", "labelUnitParticles"), + checkBoxUnitParticles("Options_Graphics", "checkBoxUnitParticles"), + labelTilesetParticles("Options_Graphics", "labelTilesetParticles"), + checkBoxTilesetParticles("Options_Graphics", "checkBoxTilesetParticles"), + labelAnimatedTilesetObjects("Options_Graphics", + "labelAnimatedTilesetObjects"), + listBoxAnimatedTilesetObjects("Options_Graphics", + "listBoxAnimatedTilesetObjects"), + labelScreenModes("Options_Graphics", "labelScreenModes"), + listBoxScreenModes("Options_Graphics", "listBoxScreenModes"), + labelFullscreenWindowed("Options_Graphics", "labelFullscreenWindowed"), + checkBoxFullscreenWindowed("Options_Graphics", + "checkBoxFullscreenWindowed"), + labelMapPreview("Options_Graphics", "labelMapPreview"), + checkBoxMapPreview("Options_Graphics", "checkBoxMapPreview"), + mainMessageBox("Options_Graphics", "mainMessageBox"), + labelEnableTextureCompression("Options_Graphics", + "labelEnableTextureCompression"), + checkBoxEnableTextureCompression("Options_Graphics", + "checkBoxEnableTextureCompression"), + labelRainEffect("Options_Graphics", "labelRainEffect"), + labelRainEffectSeparator("Options_Graphics", "labelRainEffectSeparator"), + checkBoxRainEffect("Options_Graphics", "checkBoxRainEffect"), + checkBoxRainEffectMenu("Options_Graphics", "checkBoxRainEffectMenu"), + labelGammaCorrection("Options_Graphics", "labelGammaCorrection"), + listBoxGammaCorrection("Options_Graphics", "listBoxGammaCorrection"), + labelShadowIntensity("Options_Graphics", "labelShadowIntensity"), + listBoxShadowIntensity("Options_Graphics", "listBoxShadowIntensity"), + labelShadowTextureSize("Options_Graphics", "labelShadowTextureSize"), + listBoxShadowTextureSize("Options_Graphics", "listBoxShadowTextureSize"), + labelVideos("Options_Graphics", "labelVideos"), + checkBoxVideos("Options_Graphics", "checkBoxVideos"), + labelSelectionType("Options_Graphics", "labelSelectionType"), + listBoxSelectionType("Options_Graphics", "listBoxSelectionType") { + try { + containerName = "Options_Graphics"; + this-> + parentUI = parentUI; + Lang & + lang = Lang::getInstance(); + Config & + config = Config::getInstance(); + this-> + console. + setOnlyChatMessagesInStoredLines(false); + screenModeChangedTimer = time(NULL); // just init + + ::Shared::PlatformCommon::getFullscreenVideoModes(&modeInfos, + !config. + getBool + ("Windowed")); + + int + leftLabelStart = 100; + int + leftColumnStart = leftLabelStart + 300; + int + buttonRowPos = 50; + int + buttonStartPos = 170; + int + lineOffset = 30; + int + tabButtonWidth = 200; + int + tabButtonHeight = 30; + + mainMessageBox. + init(lang.getString("Ok")); + mainMessageBox. + setEnabled(false); + mainMessageBoxState = 0; + + buttonAudioSection. + init(0, 720, tabButtonWidth, tabButtonHeight); + buttonAudioSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonAudioSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonAudioSection. + setText(lang.getString("Audio")); + // Video Section + buttonVideoSection. + init(200, 700, tabButtonWidth, tabButtonHeight + 20); + buttonVideoSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonVideoSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonVideoSection. + setText(lang.getString("Video")); + //MiscSection + buttonMiscSection. + init(400, 720, tabButtonWidth, tabButtonHeight); + buttonMiscSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonMiscSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonMiscSection. + setText(lang.getString("Misc")); + //NetworkSettings + buttonNetworkSettings. + init(600, 720, tabButtonWidth, tabButtonHeight); + buttonNetworkSettings. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonNetworkSettings. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonNetworkSettings. + setText(lang.getString("Network")); + + //KeyboardSetup + buttonKeyboardSetup. + init(800, 720, tabButtonWidth, tabButtonHeight); + buttonKeyboardSetup. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonKeyboardSetup. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonKeyboardSetup. + setText(lang.getString("Keyboardsetup")); + + int + currentLine = 650; // reset line pos + int + currentLabelStart = leftLabelStart; // set to right side + int + currentColumnStart = leftColumnStart; // set to right side + + //resolution + labelScreenModes. + init(currentLabelStart, currentLine); + labelScreenModes. + setText(lang.getString("Resolution")); + + listBoxScreenModes. + init(currentColumnStart, currentLine, 200); + + string + currentResString = config.getString("ScreenWidth") + "x" + + config.getString("ScreenHeight") + "-" + + intToStr(config.getInt("ColorBits")); + bool + currentResolutionFound = false; + for (vector < ModeInfo >::const_iterator it = modeInfos.begin(); + it != modeInfos.end(); ++it) { + if ((*it).getString() == currentResString) { + currentResolutionFound = true; + } + listBoxScreenModes. + pushBackItem((*it).getString()); + } + if (currentResolutionFound == false) { + listBoxScreenModes.pushBackItem(currentResString); + } + listBoxScreenModes.setSelectedItem(currentResString); + currentLine -= lineOffset; + + + //FullscreenWindowed + labelFullscreenWindowed.init(currentLabelStart, currentLine); + + checkBoxFullscreenWindowed.init(currentColumnStart, currentLine); + labelFullscreenWindowed.setText(lang.getString("Windowed")); + checkBoxFullscreenWindowed.setValue(config.getBool("Windowed")); + currentLine -= lineOffset; + + //gammaCorrection + labelGammaCorrection.init(currentLabelStart, currentLine); + labelGammaCorrection.setText(lang.getString("GammaCorrection")); + + listBoxGammaCorrection.init(currentColumnStart, currentLine, 200); + for (float f = 0.5; f < 3.0f; f = f + 0.1f) { + listBoxGammaCorrection.pushBackItem(floatToStr(f)); + } + float + gammaValue = config.getFloat("GammaValue", "1.0"); + if (gammaValue == 0.0f) + gammaValue = 1.0f; + listBoxGammaCorrection.setSelectedItem(floatToStr(gammaValue), + false); + + currentLine -= lineOffset; + + //filter + labelFilter.init(currentLabelStart, currentLine); + labelFilter.setText(lang.getString("Filter")); + + listBoxFilter.init(currentColumnStart, currentLine, 200); + listBoxFilter.pushBackItem("Bilinear"); + listBoxFilter.pushBackItem("Trilinear"); + listBoxFilter.setSelectedItem(config.getString("Filter")); + currentLine -= lineOffset; + + //FilterMaxAnisotropy + labelFilterMaxAnisotropy.init(currentLabelStart, currentLine); + labelFilterMaxAnisotropy.setText(lang. + getString("FilterMaxAnisotropy")); + + listBoxFilterMaxAnisotropy.init(currentColumnStart, currentLine, + 200); + listBoxFilterMaxAnisotropy.pushBackItem("1"); + listBoxFilterMaxAnisotropy.pushBackItem("2"); + listBoxFilterMaxAnisotropy.pushBackItem("4"); + listBoxFilterMaxAnisotropy.pushBackItem("8"); + listBoxFilterMaxAnisotropy.pushBackItem("16"); + listBoxFilterMaxAnisotropy.setSelectedItem(config. + getString + ("FilterMaxAnisotropy", + "1")); + currentLine -= lineOffset; + + //selectionType + labelSelectionType.init(currentLabelStart, currentLine); + labelSelectionType.setText(lang.getString("SelectionType")); + + listBoxSelectionType.init(currentColumnStart, currentLine, 250); + listBoxSelectionType.pushBackItem("SelectBuffer (nvidia)"); + listBoxSelectionType.pushBackItem("ColorPicking (default)"); + listBoxSelectionType.pushBackItem("FrustumPicking (bad)"); + + const string + selectionType = + toLower(config.getString("SelectionType", Config::colorPicking)); + if (selectionType == Config::colorPicking) + listBoxSelectionType.setSelectedItemIndex(1); + else if (selectionType == Config::frustumPicking) + listBoxSelectionType.setSelectedItemIndex(2); + else + listBoxSelectionType.setSelectedItemIndex(0); + currentLine -= lineOffset; + + //shadows + labelShadows.init(currentLabelStart, currentLine); + labelShadows.setText(lang.getString("Shadows")); + + listBoxShadows.init(currentColumnStart, currentLine, 250); + for (int i = 0; i < Renderer::sCount; ++i) { + listBoxShadows.pushBackItem(lang. + getString(Renderer:: + shadowsToStr(static_cast < + Renderer:: + Shadows> + (i)))); + } + string + str = config.getString("Shadows"); + listBoxShadows. + setSelectedItemIndex(clamp + (Renderer::strToShadows(str), 0, + Renderer::sCount - 1)); + currentLine -= lineOffset; + + //shadows + labelShadowTextureSize.init(currentLabelStart, currentLine); + labelShadowTextureSize.setText(lang.getString("ShadowTextureSize")); + + listBoxShadowTextureSize.init(currentColumnStart, currentLine, 200); + listBoxShadowTextureSize.pushBackItem("256"); + listBoxShadowTextureSize.pushBackItem("512"); + listBoxShadowTextureSize.pushBackItem("1024"); + listBoxShadowTextureSize.setSelectedItemIndex(1, false); + listBoxShadowTextureSize. + setSelectedItem(intToStr + (config.getInt("ShadowTextureSize", "512")), + false); + currentLine -= lineOffset; + + //shadows + labelShadowIntensity.init(currentLabelStart, currentLine); + labelShadowIntensity.setText(lang.getString("ShadowIntensity")); + + listBoxShadowIntensity.init(currentColumnStart, currentLine, 200); + for (float f = 0.5f; f < 3.0f; f = f + 0.1f) { + listBoxShadowIntensity.pushBackItem(floatToStr(f)); + } + float + shadowIntensity = config.getFloat("ShadowIntensity", "1.0"); + if (shadowIntensity <= 0.0f) + shadowIntensity = 1.0f; + listBoxShadowIntensity.setSelectedItem(floatToStr(shadowIntensity), + false); + + currentLine -= lineOffset; + + //textures 3d + labelTextures3D.init(currentLabelStart, currentLine); + + checkBoxTextures3D.init(currentColumnStart, currentLine); + labelTextures3D.setText(lang.getString("Textures3D")); + checkBoxTextures3D.setValue(config.getBool("Textures3D")); + currentLine -= lineOffset; + + //lights + labelLights.init(currentLabelStart, currentLine); + labelLights.setText(lang.getString("MaxLights")); + + listBoxLights.init(currentColumnStart, currentLine, 80); + for (int i = 1; i <= 8; ++i) { + listBoxLights.pushBackItem(intToStr(i)); + } + listBoxLights. + setSelectedItemIndex(clamp + (config.getInt("MaxLights") - 1, 0, 7)); + currentLine -= lineOffset; + + //unit particles + labelUnitParticles.init(currentLabelStart, currentLine); + labelUnitParticles.setText(lang.getString("ShowUnitParticles")); + + checkBoxUnitParticles.init(currentColumnStart, currentLine); + checkBoxUnitParticles.setValue(config. + getBool("UnitParticles", "true")); + currentLine -= lineOffset; + + //tileset particles + labelTilesetParticles.init(currentLabelStart, currentLine); + labelTilesetParticles.setText(lang. + getString("ShowTilesetParticles")); + + checkBoxTilesetParticles.init(currentColumnStart, currentLine); + checkBoxTilesetParticles.setValue(config. + getBool("TilesetParticles", + "true")); + currentLine -= lineOffset; + + //animated tileset objects + labelAnimatedTilesetObjects.init(currentLabelStart, currentLine); + labelAnimatedTilesetObjects.setText(lang. + getString + ("AnimatedTilesetObjects")); + + listBoxAnimatedTilesetObjects.init(currentColumnStart, currentLine, + 80); + listBoxAnimatedTilesetObjects.pushBackItem("0"); + listBoxAnimatedTilesetObjects.pushBackItem("10"); + listBoxAnimatedTilesetObjects.pushBackItem("25"); + listBoxAnimatedTilesetObjects.pushBackItem("50"); + listBoxAnimatedTilesetObjects.pushBackItem("100"); + listBoxAnimatedTilesetObjects.pushBackItem("300"); + listBoxAnimatedTilesetObjects.pushBackItem("500"); + listBoxAnimatedTilesetObjects.pushBackItem("∞"); + listBoxAnimatedTilesetObjects.setSelectedItem("∞", true); + listBoxAnimatedTilesetObjects.setSelectedItem(config. + getString + ("AnimatedTilesetObjects", + "-1"), false); + currentLine -= lineOffset; + + //unit particles + labelMapPreview.init(currentLabelStart, currentLine); + labelMapPreview.setText(lang.getString("ShowMapPreview")); + + checkBoxMapPreview.init(currentColumnStart, currentLine); + checkBoxMapPreview.setValue(config.getBool("MapPreview", "true")); + currentLine -= lineOffset; + + // Texture Compression flag + labelEnableTextureCompression.init(currentLabelStart, currentLine); + labelEnableTextureCompression.setText(lang. + getString + ("EnableTextureCompression")); + + checkBoxEnableTextureCompression.init(currentColumnStart, + currentLine); + checkBoxEnableTextureCompression.setValue(config. + getBool + ("EnableTextureCompression", + "false")); + currentLine -= lineOffset; + + labelRainEffect.init(currentLabelStart, currentLine); + labelRainEffect.setText(lang.getString("RainEffectMenuGame")); + + checkBoxRainEffectMenu.init(currentColumnStart, currentLine); + checkBoxRainEffectMenu.setValue(config. + getBool("RainEffectMenu", "true")); + + labelRainEffectSeparator.init(currentColumnStart + 30, currentLine); + labelRainEffectSeparator.setText("/"); + + checkBoxRainEffect.init(currentColumnStart + 42, currentLine); + checkBoxRainEffect.setValue(config.getBool("RainEffect", "true")); + currentLine -= lineOffset; + + labelVideos.init(currentLabelStart, currentLine); + labelVideos.setText(lang.getString("EnableVideos")); + + checkBoxVideos.init(currentColumnStart, currentLine); + checkBoxVideos.setValue(config.getBool("EnableVideos", "true")); + + // end + + // external server port + + // buttons + buttonOk.init(buttonStartPos, buttonRowPos, 100); + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + + buttonReturn.init(buttonStartPos + 110, buttonRowPos, 100); + buttonAutoConfig.setText(lang.getString("AutoConfig")); + + buttonAutoConfig.init(buttonStartPos + 230, buttonRowPos, 175); + + buttonVideoInfo.setText(lang.getString("VideoInfo")); + buttonVideoInfo.init(buttonStartPos + 415, buttonRowPos, 175); // was 620 + + GraphicComponent::applyAllCustomProperties(containerName); + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error loading options: %s\n", + __FILE__, __FUNCTION__, __LINE__, + e.what()); + throw + megaglest_runtime_error(string("Error loading options msg: ") + + e.what()); + } + } + + void + MenuStateOptionsGraphics::reloadUI() { + Lang & lang = Lang::getInstance(); + + mainMessageBox.init(lang.getString("Ok")); + + buttonAudioSection.setText(lang.getString("Audio")); + buttonVideoSection.setText(lang.getString("Video")); + buttonMiscSection.setText(lang.getString("Misc")); + buttonNetworkSettings.setText(lang.getString("Network")); + + std::vector < string > listboxData; + listboxData.push_back("None"); + listboxData.push_back("OpenAL"); + + labelScreenModes.setText(lang.getString("Resolution")); + + labelFullscreenWindowed.setText(lang.getString("Windowed")); + labelFilter.setText(lang.getString("Filter")); + + listboxData.clear(); + listboxData.push_back("Bilinear"); + listboxData.push_back("Trilinear"); + listBoxFilter.setItems(listboxData); + + labelFilterMaxAnisotropy.setText(lang. + getString("FilterMaxAnisotropy")); + listboxData.clear(); + listboxData.push_back("1"); + listboxData.push_back("2"); + listboxData.push_back("4"); + listboxData.push_back("8"); + listboxData.push_back("16"); + listBoxFilterMaxAnisotropy.setItems(listboxData); + + listboxData.clear(); + for (float f = 0.0; f < 2.1f; f = f + 0.1f) { + listboxData.push_back(floatToStr(f)); + } + listBoxGammaCorrection.setItems(listboxData); + + + listboxData.clear(); + for (float f = 0.5; f < 3.0f; f = f + 0.1f) { + listboxData.push_back(floatToStr(f)); + } + listBoxShadowIntensity.setItems(listboxData); + + + labelShadows.setText(lang.getString("Shadows")); + labelShadowTextureSize.setText(lang.getString("ShadowTextureSize")); + + labelShadowIntensity.setText(lang.getString("ShadowIntensity")); + labelGammaCorrection.setText(lang.getString("GammaCorrection")); + + listboxData.clear(); + for (int i = 0; i < Renderer::sCount; ++i) { + listboxData.push_back(lang. + getString(Renderer:: + shadowsToStr(static_cast < + Renderer::Shadows> + (i)))); + } + listBoxShadows.setItems(listboxData); + + labelTextures3D.setText(lang.getString("Textures3D")); + labelLights.setText(lang.getString("MaxLights")); + labelUnitParticles.setText(lang.getString("ShowUnitParticles")); + labelTilesetParticles.setText(lang.getString("ShowTilesetParticles")); + labelAnimatedTilesetObjects.setText(lang. + getString + ("AnimatedTilesetObjects")); + labelMapPreview.setText(lang.getString("ShowMapPreview")); + labelEnableTextureCompression.setText(lang. + getString + ("EnableTextureCompression")); + labelRainEffect.setText(lang.getString("RainEffectMenuGame")); + labelVideos.setText(lang.getString("EnableVideos")); + + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + buttonAutoConfig.setText(lang.getString("AutoConfig")); + buttonVideoInfo.setText(lang.getString("VideoInfo")); + + labelSelectionType.setText(lang.getString("SelectionType")); + } + + + void + MenuStateOptionsGraphics::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void + MenuStateOptionsGraphics::revertScreenMode() { + Config & config = Config::getInstance(); + //!!! + // Revert resolution or fullscreen + checkBoxFullscreenWindowed.setValue(config.getBool("Windowed")); + string + currentResString = config.getString("ScreenWidth") + "x" + + config.getString("ScreenHeight") + "-" + + intToStr(config.getInt("ColorBits")); + listBoxScreenModes.setSelectedItem(currentResString); + + + changeVideoModeFullScreen(!config.getBool("Windowed")); + WindowGl * + window = this->program->getWindow(); + window->ChangeVideoMode(true, + config.getInt("ScreenWidth"), + config.getInt("ScreenHeight"), + !config.getBool("Windowed"), + config.getInt("ColorBits"), + config.getInt("DepthBits"), + config.getInt("StencilBits"), + config.getBool("HardwareAcceleration", + "false"), + config.getBool("FullScreenAntiAliasing", + "false"), + config.getFloat("GammaValue", "0.0")); + Metrics::reload(this->program->getWindow()->getScreenWidth(), + this->program->getWindow()->getScreenHeight()); + window->setText(config.getString("WindowTitle", "ZetaGlest")); + + this->mainMenu->init(); + } + + void + MenuStateOptionsGraphics::update() { + if (mainMessageBox.getEnabled() && (mainMessageBoxState == 1)) { + int + waitTime = 10; + if ((time(NULL) - screenModeChangedTimer > waitTime)) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + + revertScreenMode(); + } else { + Lang & lang = Lang::getInstance(); + int + timeToShow = waitTime - time(NULL) + screenModeChangedTimer; + // show timer in button + mainMessageBox.getButton(0)->setText(lang.getString("Ok") + + " (" + + intToStr(timeToShow) + ")"); + } + } + } + + void + MenuStateOptionsGraphics::mouseClick(int x, int y, + MouseButton mouseButton) { + + Config & config = Config::getInstance(); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + + + if (mainMessageBox.getEnabled()) { + int + button = 0; + + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + saveConfig(); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + //mainMenu->setState(new MenuStateOptions(program, mainMenu)); + } else { + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + } + } else { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + + revertScreenMode(); + } + } + } + } else if (buttonOk.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + Lang & lang = Lang::getInstance(); + bool + selectedFullscreenWindowed = checkBoxFullscreenWindowed.getValue(); + string + currentResolution = + config.getString("ScreenWidth") + "x" + + config.getString("ScreenHeight") + "-" + + intToStr(config.getInt("ColorBits")); + string + selectedResolution = listBoxScreenModes.getSelectedItem(); + bool + currentFullscreenWindowed = config.getBool("Windowed"); + if (currentResolution != selectedResolution + || currentFullscreenWindowed != selectedFullscreenWindowed) { + + changeVideoModeFullScreen(!selectedFullscreenWindowed); + const ModeInfo * + selectedMode = NULL; + for (vector < ModeInfo >::const_iterator it = modeInfos.begin(); + it != modeInfos.end(); ++it) { + if ((*it).getString() == selectedResolution) { + //config.setInt("ScreenWidth",(*it).width); + //config.setInt("ScreenHeight",(*it).height); + //config.setInt("ColorBits",(*it).depth); + selectedMode = &(*it); + } + } + if (selectedMode == NULL) { // if we cannot find the selectedResolution we try it with current one + for (vector < ModeInfo >::const_iterator it = modeInfos.begin(); + it != modeInfos.end(); ++it) { + if ((*it).getString() == currentResolution) { + //config.setInt("ScreenWidth",(*it).width); + //config.setInt("ScreenHeight",(*it).height); + //config.setInt("ColorBits",(*it).depth); + selectedMode = &(*it); + } + } + } + if (selectedMode == NULL) { + throw + megaglest_runtime_error("selectedMode == NULL"); + } #if defined(__APPLE__) - mainMessageBoxState = 1; - mainMessageBox.init (lang.getString ("Ok"), - lang.getString ("Cancel")); - screenModeChangedTimer = time (NULL); + mainMessageBoxState = 1; + mainMessageBox.init(lang.getString("Ok"), + lang.getString("Cancel")); + screenModeChangedTimer = time(NULL); - showMessageBox (lang.getString ("RestartNeeded"), - lang.getString ("ResolutionChanged"), false); + showMessageBox(lang.getString("RestartNeeded"), + lang.getString("ResolutionChanged"), false); #else - WindowGl * - window = this->program->getWindow (); - window->ChangeVideoMode (true, - selectedMode->width, - selectedMode->height, - !selectedFullscreenWindowed, - selectedMode->depth, - config.getInt ("DepthBits"), - config.getInt ("StencilBits"), - config.getBool ("HardwareAcceleration", - "false"), - config.getBool ("FullScreenAntiAliasing", - "false"), - strToFloat (listBoxGammaCorrection. - getSelectedItem ())); + WindowGl * + window = this->program->getWindow(); + window->ChangeVideoMode(true, + selectedMode->width, + selectedMode->height, + !selectedFullscreenWindowed, + selectedMode->depth, + config.getInt("DepthBits"), + config.getInt("StencilBits"), + config.getBool("HardwareAcceleration", + "false"), + config.getBool("FullScreenAntiAliasing", + "false"), + strToFloat(listBoxGammaCorrection. + getSelectedItem())); - Metrics::reload (this->program->getWindow ()->getScreenWidth (), - this->program->getWindow ()->getScreenHeight ()); + Metrics::reload(this->program->getWindow()->getScreenWidth(), + this->program->getWindow()->getScreenHeight()); - this->mainMenu->init (); + this->mainMenu->init(); - mainMessageBoxState = 1; - mainMessageBox.init (lang.getString ("Ok"), - lang.getString ("Cancel")); - screenModeChangedTimer = time (NULL); + mainMessageBoxState = 1; + mainMessageBox.init(lang.getString("Ok"), + lang.getString("Cancel")); + screenModeChangedTimer = time(NULL); - showMessageBox (lang.getString ("ResolutionChanged"), - lang.getString ("Notice"), false); + showMessageBox(lang.getString("ResolutionChanged"), + lang.getString("Notice"), false); #endif - //No saveConfig() here! this is done by the messageBox - return; - } - saveConfig (); - return; - } - else if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); + //No saveConfig() here! this is done by the messageBox + return; + } + saveConfig(); + return; + } else if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); - // reset the gamma to former value - string - currentGammaCorrection = config.getString ("GammaValue", "1.0"); - string - selectedGammaCorrection = listBoxGammaCorrection.getSelectedItem (); - if (currentGammaCorrection != selectedGammaCorrection) - { - float - gammaValue = strToFloat (currentGammaCorrection); - if (gammaValue == 0.0f) - gammaValue = 1.0f; - if (gammaValue != 0.0) - { - program->getWindow ()->setGamma (gammaValue); - } - } - if (this->parentUI != NULL) - { - *this->parentUI = NULL; - delete * - this-> - parentUI; - } - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - else if (buttonKeyboardSetup.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); // open keyboard shortcuts setup screen - //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); // open keyboard shortcuts setup screen - //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu)); // open keyboard shortcuts setup screen - mainMenu->setState (new MenuStateKeysetup (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - //showMessageBox("Not implemented yet", "Keyboard setup", false); - return; - } - else if (buttonAudioSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsSound (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonNetworkSettings.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsNetwork (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonMiscSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptions (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonVideoSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonAutoConfig.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - Renderer::getInstance ().autoConfig (); - //saveConfig(); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu)); - return; - } - else if (buttonVideoInfo.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateGraphicInfo (program, mainMenu)); - return; - } - else - { - listBoxSelectionType.mouseClick (x, y); - listBoxShadows.mouseClick (x, y); - listBoxAnimatedTilesetObjects.mouseClick (x, y); - listBoxShadowTextureSize.mouseClick (x, y); - listBoxShadowIntensity.mouseClick (x, y); - listBoxFilter.mouseClick (x, y); - listBoxFilterMaxAnisotropy.mouseClick (x, y); - if (listBoxGammaCorrection.mouseClick (x, y)) - { - float - gammaValue = - strToFloat (listBoxGammaCorrection.getSelectedItem ()); - if (gammaValue != 0.0) - { - program->getWindow ()->setGamma (gammaValue); - } - } - checkBoxTextures3D.mouseClick (x, y); - checkBoxUnitParticles.mouseClick (x, y); - checkBoxTilesetParticles.mouseClick (x, y); - checkBoxMapPreview.mouseClick (x, y); - listBoxLights.mouseClick (x, y); - listBoxScreenModes.mouseClick (x, y); - checkBoxFullscreenWindowed.mouseClick (x, y); - checkBoxEnableTextureCompression.mouseClick (x, y); - checkBoxRainEffect.mouseClick (x, y); - checkBoxRainEffectMenu.mouseClick (x, y); + // reset the gamma to former value + string + currentGammaCorrection = config.getString("GammaValue", "1.0"); + string + selectedGammaCorrection = listBoxGammaCorrection.getSelectedItem(); + if (currentGammaCorrection != selectedGammaCorrection) { + float + gammaValue = strToFloat(currentGammaCorrection); + if (gammaValue == 0.0f) + gammaValue = 1.0f; + if (gammaValue != 0.0) { + program->getWindow()->setGamma(gammaValue); + } + } + if (this->parentUI != NULL) { + *this->parentUI = NULL; + delete * + this-> + parentUI; + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } else if (buttonKeyboardSetup.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); // open keyboard shortcuts setup screen + //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); // open keyboard shortcuts setup screen + //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu)); // open keyboard shortcuts setup screen + mainMenu->setState(new MenuStateKeysetup(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + //showMessageBox("Not implemented yet", "Keyboard setup", false); + return; + } else if (buttonAudioSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsSound(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonNetworkSettings.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonMiscSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptions(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonVideoSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonAutoConfig.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + Renderer::getInstance().autoConfig(); + //saveConfig(); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); + return; + } else if (buttonVideoInfo.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateGraphicInfo(program, mainMenu)); + return; + } else { + listBoxSelectionType.mouseClick(x, y); + listBoxShadows.mouseClick(x, y); + listBoxAnimatedTilesetObjects.mouseClick(x, y); + listBoxShadowTextureSize.mouseClick(x, y); + listBoxShadowIntensity.mouseClick(x, y); + listBoxFilter.mouseClick(x, y); + listBoxFilterMaxAnisotropy.mouseClick(x, y); + if (listBoxGammaCorrection.mouseClick(x, y)) { + float + gammaValue = + strToFloat(listBoxGammaCorrection.getSelectedItem()); + if (gammaValue != 0.0) { + program->getWindow()->setGamma(gammaValue); + } + } + checkBoxTextures3D.mouseClick(x, y); + checkBoxUnitParticles.mouseClick(x, y); + checkBoxTilesetParticles.mouseClick(x, y); + checkBoxMapPreview.mouseClick(x, y); + listBoxLights.mouseClick(x, y); + listBoxScreenModes.mouseClick(x, y); + checkBoxFullscreenWindowed.mouseClick(x, y); + checkBoxEnableTextureCompression.mouseClick(x, y); + checkBoxRainEffect.mouseClick(x, y); + checkBoxRainEffectMenu.mouseClick(x, y); - checkBoxVideos.mouseClick (x, y); - } - } + checkBoxVideos.mouseClick(x, y); + } + } - void - MenuStateOptionsGraphics::mouseMove (int x, int y, const MouseState * ms) - { - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } + void + MenuStateOptionsGraphics::mouseMove(int x, int y, const MouseState * ms) { + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } - buttonOk.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - buttonKeyboardSetup.mouseMove (x, y); - buttonAudioSection.mouseMove (x, y); - buttonNetworkSettings.mouseMove (x, y); - buttonMiscSection.mouseMove (x, y); - buttonVideoSection.mouseMove (x, y); - buttonAutoConfig.mouseMove (x, y); - buttonVideoInfo.mouseMove (x, y); - listBoxFilter.mouseMove (x, y); - listBoxFilterMaxAnisotropy.mouseMove (x, y); - listBoxGammaCorrection.mouseMove (x, y); - listBoxShadowIntensity.mouseMove (x, y); - listBoxSelectionType.mouseMove (x, y); - listBoxShadows.mouseMove (x, y); - checkBoxTextures3D.mouseMove (x, y); - checkBoxUnitParticles.mouseMove (x, y); - checkBoxTilesetParticles.mouseMove (x, y); - labelAnimatedTilesetObjects.mouseMove (x, y); - listBoxAnimatedTilesetObjects.mouseMove (x, y); - checkBoxTilesetParticles.mouseMove (x, y); - checkBoxMapPreview.mouseMove (x, y); - listBoxLights.mouseMove (x, y); - listBoxScreenModes.mouseMove (x, y); - checkBoxFullscreenWindowed.mouseMove (x, y); - checkBoxEnableTextureCompression.mouseMove (x, y); + buttonOk.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + buttonKeyboardSetup.mouseMove(x, y); + buttonAudioSection.mouseMove(x, y); + buttonNetworkSettings.mouseMove(x, y); + buttonMiscSection.mouseMove(x, y); + buttonVideoSection.mouseMove(x, y); + buttonAutoConfig.mouseMove(x, y); + buttonVideoInfo.mouseMove(x, y); + listBoxFilter.mouseMove(x, y); + listBoxFilterMaxAnisotropy.mouseMove(x, y); + listBoxGammaCorrection.mouseMove(x, y); + listBoxShadowIntensity.mouseMove(x, y); + listBoxSelectionType.mouseMove(x, y); + listBoxShadows.mouseMove(x, y); + checkBoxTextures3D.mouseMove(x, y); + checkBoxUnitParticles.mouseMove(x, y); + checkBoxTilesetParticles.mouseMove(x, y); + labelAnimatedTilesetObjects.mouseMove(x, y); + listBoxAnimatedTilesetObjects.mouseMove(x, y); + checkBoxTilesetParticles.mouseMove(x, y); + checkBoxMapPreview.mouseMove(x, y); + listBoxLights.mouseMove(x, y); + listBoxScreenModes.mouseMove(x, y); + checkBoxFullscreenWindowed.mouseMove(x, y); + checkBoxEnableTextureCompression.mouseMove(x, y); - checkBoxRainEffect.mouseMove (x, y); - checkBoxRainEffectMenu.mouseMove (x, y); + checkBoxRainEffect.mouseMove(x, y); + checkBoxRainEffectMenu.mouseMove(x, y); - checkBoxVideos.mouseMove (x, y); - } + checkBoxVideos.mouseMove(x, y); + } -//bool MenuStateOptionsGraphics::isInSpecialKeyCaptureEvent() { -// return (activeInputLabel != NULL); -//} -// -//void MenuStateOptionsGraphics::keyDown(SDL_KeyboardEvent key) { -// if(activeInputLabel != NULL) { -// keyDownEditLabel(key, &activeInputLabel); -// } -//} + //bool MenuStateOptionsGraphics::isInSpecialKeyCaptureEvent() { + // return (activeInputLabel != NULL); + //} + // + //void MenuStateOptionsGraphics::keyDown(SDL_KeyboardEvent key) { + // if(activeInputLabel != NULL) { + // keyDownEditLabel(key, &activeInputLabel); + // } + //} - void - MenuStateOptionsGraphics::keyPress (SDL_KeyboardEvent c) - { -// if(activeInputLabel != NULL) { -// //printf("[%d]\n",c); fflush(stdout); -// if( &labelPlayerName == activeInputLabel || -// &labelTransifexUser == activeInputLabel || -// &labelTransifexPwd == activeInputLabel || -// &labelTransifexI18N == activeInputLabel) { -// textInputEditLabel(c, &activeInputLabel); -// } -// } -// else { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), c) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } -// } - } + void + MenuStateOptionsGraphics::keyPress(SDL_KeyboardEvent c) { + // if(activeInputLabel != NULL) { + // //printf("[%d]\n",c); fflush(stdout); + // if( &labelPlayerName == activeInputLabel || + // &labelTransifexUser == activeInputLabel || + // &labelTransifexPwd == activeInputLabel || + // &labelTransifexI18N == activeInputLabel) { + // textInputEditLabel(c, &activeInputLabel); + // } + // } + // else { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), c) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + // } + } - void - MenuStateOptionsGraphics::render () - { - Renderer & renderer = Renderer::getInstance (); + void + MenuStateOptionsGraphics::render() { + Renderer & renderer = Renderer::getInstance(); - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else - { - renderer.renderButton (&buttonOk); - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonKeyboardSetup); - renderer.renderButton (&buttonVideoSection); - renderer.renderButton (&buttonAudioSection); - renderer.renderButton (&buttonMiscSection); - renderer.renderButton (&buttonNetworkSettings); - renderer.renderButton (&buttonAutoConfig); - renderer.renderButton (&buttonVideoInfo); - renderer.renderListBox (&listBoxShadows); - renderer.renderCheckBox (&checkBoxTextures3D); - renderer.renderCheckBox (&checkBoxUnitParticles); - renderer.renderCheckBox (&checkBoxTilesetParticles); - renderer.renderCheckBox (&checkBoxMapPreview); - renderer.renderListBox (&listBoxLights); - renderer.renderListBox (&listBoxFilter); - renderer.renderListBox (&listBoxFilterMaxAnisotropy); - renderer.renderListBox (&listBoxGammaCorrection); - renderer.renderListBox (&listBoxShadowIntensity); - renderer.renderLabel (&labelShadows); - renderer.renderLabel (&labelTextures3D); - renderer.renderLabel (&labelUnitParticles); - renderer.renderLabel (&labelTilesetParticles); - renderer.renderListBox (&listBoxAnimatedTilesetObjects); - renderer.renderLabel (&labelAnimatedTilesetObjects); - renderer.renderLabel (&labelMapPreview); - renderer.renderLabel (&labelLights); - renderer.renderLabel (&labelFilter); - renderer.renderLabel (&labelFilterMaxAnisotropy); - renderer.renderLabel (&labelGammaCorrection); - renderer.renderLabel (&labelShadowIntensity); - renderer.renderLabel (&labelScreenModes); - renderer.renderListBox (&listBoxScreenModes); - renderer.renderLabel (&labelFullscreenWindowed); - renderer.renderCheckBox (&checkBoxFullscreenWindowed); + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else { + renderer.renderButton(&buttonOk); + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonKeyboardSetup); + renderer.renderButton(&buttonVideoSection); + renderer.renderButton(&buttonAudioSection); + renderer.renderButton(&buttonMiscSection); + renderer.renderButton(&buttonNetworkSettings); + renderer.renderButton(&buttonAutoConfig); + renderer.renderButton(&buttonVideoInfo); + renderer.renderListBox(&listBoxShadows); + renderer.renderCheckBox(&checkBoxTextures3D); + renderer.renderCheckBox(&checkBoxUnitParticles); + renderer.renderCheckBox(&checkBoxTilesetParticles); + renderer.renderCheckBox(&checkBoxMapPreview); + renderer.renderListBox(&listBoxLights); + renderer.renderListBox(&listBoxFilter); + renderer.renderListBox(&listBoxFilterMaxAnisotropy); + renderer.renderListBox(&listBoxGammaCorrection); + renderer.renderListBox(&listBoxShadowIntensity); + renderer.renderLabel(&labelShadows); + renderer.renderLabel(&labelTextures3D); + renderer.renderLabel(&labelUnitParticles); + renderer.renderLabel(&labelTilesetParticles); + renderer.renderListBox(&listBoxAnimatedTilesetObjects); + renderer.renderLabel(&labelAnimatedTilesetObjects); + renderer.renderLabel(&labelMapPreview); + renderer.renderLabel(&labelLights); + renderer.renderLabel(&labelFilter); + renderer.renderLabel(&labelFilterMaxAnisotropy); + renderer.renderLabel(&labelGammaCorrection); + renderer.renderLabel(&labelShadowIntensity); + renderer.renderLabel(&labelScreenModes); + renderer.renderListBox(&listBoxScreenModes); + renderer.renderLabel(&labelFullscreenWindowed); + renderer.renderCheckBox(&checkBoxFullscreenWindowed); - renderer.renderLabel (&labelEnableTextureCompression); - renderer.renderCheckBox (&checkBoxEnableTextureCompression); + renderer.renderLabel(&labelEnableTextureCompression); + renderer.renderCheckBox(&checkBoxEnableTextureCompression); - renderer.renderLabel (&labelRainEffect); - renderer.renderCheckBox (&checkBoxRainEffect); - renderer.renderLabel (&labelRainEffectSeparator); - renderer.renderCheckBox (&checkBoxRainEffectMenu); + renderer.renderLabel(&labelRainEffect); + renderer.renderCheckBox(&checkBoxRainEffect); + renderer.renderLabel(&labelRainEffectSeparator); + renderer.renderCheckBox(&checkBoxRainEffectMenu); - renderer.renderLabel (&labelShadowTextureSize); - renderer.renderListBox (&listBoxShadowTextureSize); + renderer.renderLabel(&labelShadowTextureSize); + renderer.renderListBox(&listBoxShadowTextureSize); - renderer.renderLabel (&labelSelectionType); - renderer.renderListBox (&listBoxSelectionType); + renderer.renderLabel(&labelSelectionType); + renderer.renderListBox(&listBoxSelectionType); - renderer.renderLabel (&labelVideos); - renderer.renderCheckBox (&checkBoxVideos); - } + renderer.renderLabel(&labelVideos); + renderer.renderCheckBox(&checkBoxVideos); + } - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } - void - MenuStateOptionsGraphics::saveConfig () - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - //setActiveInputLable(NULL); + void + MenuStateOptionsGraphics::saveConfig() { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + //setActiveInputLable(NULL); - int - selectionTypeindex = listBoxSelectionType.getSelectedItemIndex (); - if (selectionTypeindex == 0) - { - config.setString ("SelectionType", Config::selectBufPicking); - } - else if (selectionTypeindex == 1) - { - config.setString ("SelectionType", Config::colorPicking); - } - else if (selectionTypeindex == 2) - { - config.setString ("SelectionType", Config::frustumPicking); - } + int + selectionTypeindex = listBoxSelectionType.getSelectedItemIndex(); + if (selectionTypeindex == 0) { + config.setString("SelectionType", Config::selectBufPicking); + } else if (selectionTypeindex == 1) { + config.setString("SelectionType", Config::colorPicking); + } else if (selectionTypeindex == 2) { + config.setString("SelectionType", Config::frustumPicking); + } - int - index = listBoxShadows.getSelectedItemIndex (); - config.setString ("Shadows", - Renderer::shadowsToStr (static_cast < - Renderer::Shadows > (index))); + int + index = listBoxShadows.getSelectedItemIndex(); + config.setString("Shadows", + Renderer::shadowsToStr(static_cast < + Renderer::Shadows> (index))); - string - texSizeString = listBoxShadowTextureSize.getSelectedItem (); - config.setInt ("ShadowTextureSize", strToInt (texSizeString)); + string + texSizeString = listBoxShadowTextureSize.getSelectedItem(); + config.setInt("ShadowTextureSize", strToInt(texSizeString)); - config.setBool ("Windowed", checkBoxFullscreenWindowed.getValue ()); - config.setString ("Filter", listBoxFilter.getSelectedItem ()); - config.setInt ("FilterMaxAnisotropy", - strToInt (listBoxFilterMaxAnisotropy. - getSelectedItem ())); - config.setFloat ("GammaValue", - strToFloat (listBoxGammaCorrection. - getSelectedItem ())); - config.setFloat ("ShadowIntensity", - strToFloat (listBoxShadowIntensity. - getSelectedItem ())); - config.setBool ("Textures3D", checkBoxTextures3D.getValue ()); - config.setBool ("UnitParticles", (checkBoxUnitParticles.getValue ())); - config.setBool ("TilesetParticles", - (checkBoxTilesetParticles.getValue ())); - config.setBool ("MapPreview", checkBoxMapPreview.getValue ()); - config.setInt ("MaxLights", listBoxLights.getSelectedItemIndex () + 1); + config.setBool("Windowed", checkBoxFullscreenWindowed.getValue()); + config.setString("Filter", listBoxFilter.getSelectedItem()); + config.setInt("FilterMaxAnisotropy", + strToInt(listBoxFilterMaxAnisotropy. + getSelectedItem())); + config.setFloat("GammaValue", + strToFloat(listBoxGammaCorrection. + getSelectedItem())); + config.setFloat("ShadowIntensity", + strToFloat(listBoxShadowIntensity. + getSelectedItem())); + config.setBool("Textures3D", checkBoxTextures3D.getValue()); + config.setBool("UnitParticles", (checkBoxUnitParticles.getValue())); + config.setBool("TilesetParticles", + (checkBoxTilesetParticles.getValue())); + config.setBool("MapPreview", checkBoxMapPreview.getValue()); + config.setInt("MaxLights", listBoxLights.getSelectedItemIndex() + 1); - if (listBoxAnimatedTilesetObjects.getSelectedItem () == "∞") - { - config.setInt ("AnimatedTilesetObjects", -1); - } - else - { - config.setInt ("AnimatedTilesetObjects", - atoi (listBoxAnimatedTilesetObjects.getSelectedItem (). - c_str ())); - } + if (listBoxAnimatedTilesetObjects.getSelectedItem() == "∞") { + config.setInt("AnimatedTilesetObjects", -1); + } else { + config.setInt("AnimatedTilesetObjects", + atoi(listBoxAnimatedTilesetObjects.getSelectedItem(). + c_str())); + } - config.setBool ("RainEffect", checkBoxRainEffect.getValue ()); - config.setBool ("RainEffectMenu", checkBoxRainEffectMenu.getValue ()); + config.setBool("RainEffect", checkBoxRainEffect.getValue()); + config.setBool("RainEffectMenu", checkBoxRainEffectMenu.getValue()); - config.setBool ("EnableTextureCompression", - checkBoxEnableTextureCompression.getValue ()); + config.setBool("EnableTextureCompression", + checkBoxEnableTextureCompression.getValue()); - config.setBool ("EnableVideos", checkBoxVideos.getValue ()); + config.setBool("EnableVideos", checkBoxVideos.getValue()); - string - currentResolution = - config.getString ("ScreenWidth") + "x" + - config.getString ("ScreenHeight"); - string - selectedResolution = listBoxScreenModes.getSelectedItem (); - if (currentResolution != selectedResolution) - { - for (vector < ModeInfo >::const_iterator it = modeInfos.begin (); - it != modeInfos.end (); ++it) - { - if ((*it).getString () == selectedResolution) - { - config.setInt ("ScreenWidth", (*it).width); - config.setInt ("ScreenHeight", (*it).height); - config.setInt ("ColorBits", (*it).depth); - } - } - } + string + currentResolution = + config.getString("ScreenWidth") + "x" + + config.getString("ScreenHeight"); + string + selectedResolution = listBoxScreenModes.getSelectedItem(); + if (currentResolution != selectedResolution) { + for (vector < ModeInfo >::const_iterator it = modeInfos.begin(); + it != modeInfos.end(); ++it) { + if ((*it).getString() == selectedResolution) { + config.setInt("ScreenWidth", (*it).width); + config.setInt("ScreenHeight", (*it).height); + config.setInt("ColorBits", (*it).depth); + } + } + } - config.save (); + config.save(); - if (config.getBool ("DisableLuaSandbox", "false") == true) - { - LuaScript::setDisableSandbox (true); - } + if (config.getBool("DisableLuaSandbox", "false") == true) { + LuaScript::setDisableSandbox(true); + } - Renderer::getInstance ().loadConfig (); - console.addLine (lang.getString ("SettingsSaved")); - } + Renderer::getInstance().loadConfig(); + console.addLine(lang.getString("SettingsSaved")); + } -//void MenuStateOptionsGraphics::setActiveInputLable(GraphicLabel *newLable) { -// MenuState::setActiveInputLabel(newLable,&activeInputLabel); -// -// if(newLable == &labelTransifexPwd) { -// labelTransifexPwd.setIsPassword(false); -// } -// else { -// labelTransifexPwd.setIsPassword(true); -// } -//} + //void MenuStateOptionsGraphics::setActiveInputLable(GraphicLabel *newLable) { + // MenuState::setActiveInputLabel(newLable,&activeInputLabel); + // + // if(newLable == &labelTransifexPwd) { + // labelTransifexPwd.setIsPassword(false); + // } + // else { + // labelTransifexPwd.setIsPassword(true); + // } + //} - } + } } //end namespace diff --git a/source/glest_game/menu/menu_state_options_graphics.h b/source/glest_game/menu/menu_state_options_graphics.h index ac7cf9898..867427a84 100644 --- a/source/glest_game/menu/menu_state_options_graphics.h +++ b/source/glest_game/menu/menu_state_options_graphics.h @@ -15,114 +15,112 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateOptionsGraphics -// =============================== + // =============================== + // class MenuStateOptionsGraphics + // =============================== - class MenuStateOptionsGraphics:public MenuState - { - private: + class MenuStateOptionsGraphics :public MenuState { + private: - GraphicButton buttonOk; - GraphicButton buttonReturn; - GraphicButton buttonAutoConfig; - GraphicButton buttonVideoInfo; + GraphicButton buttonOk; + GraphicButton buttonReturn; + GraphicButton buttonAutoConfig; + GraphicButton buttonVideoInfo; - GraphicButton buttonKeyboardSetup; // configure the keyboard - GraphicButton buttonVideoSection; - GraphicButton buttonAudioSection; - GraphicButton buttonMiscSection; - GraphicButton buttonNetworkSettings; + GraphicButton buttonKeyboardSetup; // configure the keyboard + GraphicButton buttonVideoSection; + GraphicButton buttonAudioSection; + GraphicButton buttonMiscSection; + GraphicButton buttonNetworkSettings; - GraphicLabel labelShadows; - GraphicListBox listBoxShadows; - GraphicLabel labelFilter; - GraphicListBox listBoxFilter; - GraphicLabel labelFilterMaxAnisotropy; - GraphicListBox listBoxFilterMaxAnisotropy; + GraphicLabel labelShadows; + GraphicListBox listBoxShadows; + GraphicLabel labelFilter; + GraphicListBox listBoxFilter; + GraphicLabel labelFilterMaxAnisotropy; + GraphicListBox listBoxFilterMaxAnisotropy; - GraphicLabel labelTextures3D; - GraphicCheckBox checkBoxTextures3D; - GraphicLabel labelLights; - GraphicListBox listBoxLights; - GraphicLabel labelUnitParticles; - GraphicCheckBox checkBoxUnitParticles; + GraphicLabel labelTextures3D; + GraphicCheckBox checkBoxTextures3D; + GraphicLabel labelLights; + GraphicListBox listBoxLights; + GraphicLabel labelUnitParticles; + GraphicCheckBox checkBoxUnitParticles; - GraphicLabel labelTilesetParticles; - GraphicCheckBox checkBoxTilesetParticles; - GraphicLabel labelAnimatedTilesetObjects; - GraphicListBox listBoxAnimatedTilesetObjects; + GraphicLabel labelTilesetParticles; + GraphicCheckBox checkBoxTilesetParticles; + GraphicLabel labelAnimatedTilesetObjects; + GraphicListBox listBoxAnimatedTilesetObjects; - GraphicLabel labelScreenModes; - GraphicListBox listBoxScreenModes; - vector < ModeInfo > modeInfos; + GraphicLabel labelScreenModes; + GraphicListBox listBoxScreenModes; + vector < ModeInfo > modeInfos; - GraphicLabel labelFullscreenWindowed; - GraphicCheckBox checkBoxFullscreenWindowed; + GraphicLabel labelFullscreenWindowed; + GraphicCheckBox checkBoxFullscreenWindowed; - GraphicLabel labelMapPreview; - GraphicCheckBox checkBoxMapPreview; + GraphicLabel labelMapPreview; + GraphicCheckBox checkBoxMapPreview; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - GraphicLabel labelEnableTextureCompression; - GraphicCheckBox checkBoxEnableTextureCompression; + GraphicLabel labelEnableTextureCompression; + GraphicCheckBox checkBoxEnableTextureCompression; - GraphicLabel labelRainEffect; - GraphicLabel labelRainEffectSeparator; - GraphicCheckBox checkBoxRainEffect; - GraphicCheckBox checkBoxRainEffectMenu; + GraphicLabel labelRainEffect; + GraphicLabel labelRainEffectSeparator; + GraphicCheckBox checkBoxRainEffect; + GraphicCheckBox checkBoxRainEffectMenu; - GraphicLabel labelGammaCorrection; - GraphicListBox listBoxGammaCorrection; + GraphicLabel labelGammaCorrection; + GraphicListBox listBoxGammaCorrection; - GraphicLabel labelShadowIntensity; - GraphicListBox listBoxShadowIntensity; + GraphicLabel labelShadowIntensity; + GraphicListBox listBoxShadowIntensity; - GraphicLabel labelShadowTextureSize; - GraphicListBox listBoxShadowTextureSize; + GraphicLabel labelShadowTextureSize; + GraphicListBox listBoxShadowTextureSize; - GraphicLabel labelVideos; - GraphicCheckBox checkBoxVideos; + GraphicLabel labelVideos; + GraphicCheckBox checkBoxVideos; - GraphicLabel labelSelectionType; - GraphicListBox listBoxSelectionType; + GraphicLabel labelSelectionType; + GraphicListBox listBoxSelectionType; - ProgramState **parentUI; - time_t screenModeChangedTimer; + ProgramState **parentUI; + time_t screenModeChangedTimer; - public: - MenuStateOptionsGraphics (Program * program, MainMenu * mainMenu, - ProgramState ** parentUI = NULL); + public: + MenuStateOptionsGraphics(Program * program, MainMenu * mainMenu, + ProgramState ** parentUI = NULL); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - //virtual void keyDown(SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - //virtual bool isInSpecialKeyCaptureEvent(); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + //virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + //virtual bool isInSpecialKeyCaptureEvent(); - virtual void reloadUI (); + virtual void reloadUI(); - private: - void saveConfig (); - void setActiveInputLable (GraphicLabel * newLable); - void showMessageBox (const string & text, const string & header, - bool toggle); - void revertScreenMode (); - void setupTransifexUI (); - virtual void update (); - }; + private: + void saveConfig(); + void setActiveInputLable(GraphicLabel * newLable); + void showMessageBox(const string & text, const string & header, + bool toggle); + void revertScreenMode(); + void setupTransifexUI(); + virtual void update(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_options_network.cpp b/source/glest_game/menu/menu_state_options_network.cpp index 21b140ea2..e6034a57c 100644 --- a/source/glest_game/menu/menu_state_options_network.cpp +++ b/source/glest_game/menu/menu_state_options_network.cpp @@ -29,619 +29,569 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { - -// ===================================================== -// class MenuStateOptions -// ===================================================== - MenuStateOptionsNetwork::MenuStateOptionsNetwork (Program * program, - MainMenu * mainMenu, - ProgramState ** - parentUI): - MenuState (program, mainMenu, "config"), - buttonOk ("Options_Network", "buttonOk"), - buttonReturn ("Options_Network", "buttonReturn"), - buttonKeyboardSetup ("Options_Network", "buttonKeyboardSetup"), - buttonVideoSection ("Options_Network", "buttonVideoSection"), - buttonAudioSection ("Options_Network", "buttonAudioSection"), - buttonMiscSection ("Options_Network", "buttonMiscSection"), - buttonNetworkSettings ("Options_Network", "buttonNetworkSettings"), - mainMessageBox ("Options_Network", "mainMessageBox"), - labelExternalPort ("Options_Network", "labelExternalPort"), - labelServerPortLabel ("Options_Network", "labelServerPortLabel"), - labelPublishServerExternalPort ("Options_Network", - "labelPublishServerExternalPort"), - listBoxServerPort ("Options_Network", "listBoxServerPort"), - labelEnableFTP ("Options_Network", "labelEnableFTP"), - checkBoxEnableFTP ("Options_Network", "checkBoxEnableFTP"), - labelEnableFTPServer ("Options_Network", "labelEnableFTPServer"), - checkBoxEnableFTPServer ("Options_Network", "checkBoxEnableFTPServer"), - labelFTPServerPortLabel ("Options_Network", "labelFTPServerPortLabel"), - labelFTPServerPort ("Options_Network", "labelFTPServerPort"), - labelFTPServerDataPortsLabel ("Options_Network", - "labelFTPServerDataPortsLabel"), - labelFTPServerDataPorts ("Options_Network", "labelFTPServerDataPorts"), - labelEnableFTPServerInternetTilesetXfer ("Options_Network", - "labelEnableFTPServerInternetTilesetXfer"), - checkBoxEnableFTPServerInternetTilesetXfer ("Options_Network", - "checkBoxEnableFTPServerInternetTilesetXfer"), - labelEnableFTPServerInternetTechtreeXfer ("Options_Network", - "labelEnableFTPServerInternetTechtreeXfer"), - checkBoxEnableFTPServerInternetTechtreeXfer ("Options_Network", - "checkBoxEnableFTPServerInternetTechtreeXfer"), - labelEnablePrivacy ("Options_Network", "labelEnablePrivacy"), - checkBoxEnablePrivacy ("Options_Network", "checkBoxEnablePrivacy") - { - try - { - containerName = "Options_Network"; - Lang & - lang = Lang::getInstance (); - Config & - config = Config::getInstance (); - this-> - parentUI = parentUI; - this-> - console. - setOnlyChatMessagesInStoredLines (false); - - int - leftLabelStart = 100; - int - leftColumnStart = leftLabelStart + 300; - int - buttonRowPos = 50; - int - buttonStartPos = 170; - int - lineOffset = 30; - int - tabButtonWidth = 200; - int - tabButtonHeight = 30; - - mainMessageBox. - init (lang.getString ("Ok")); - mainMessageBox. - setEnabled (false); - mainMessageBoxState = 0; - - buttonAudioSection. - init (0, 720, tabButtonWidth, tabButtonHeight); - buttonAudioSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonAudioSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonAudioSection. - setText (lang.getString ("Audio")); - // Video Section - buttonVideoSection. - init (200, 720, tabButtonWidth, tabButtonHeight); - buttonVideoSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonVideoSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonVideoSection. - setText (lang.getString ("Video")); - //currentLine-=lineOffset; - //MiscSection - buttonMiscSection. - init (400, 720, tabButtonWidth, tabButtonHeight); - buttonMiscSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonMiscSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonMiscSection. - setText (lang.getString ("Misc")); - //NetworkSettings - buttonNetworkSettings. - init (600, 700, tabButtonWidth, tabButtonHeight + 20); - buttonNetworkSettings. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonNetworkSettings. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonNetworkSettings. - setText (lang.getString ("Network")); - - //KeyboardSetup - buttonKeyboardSetup. - init (800, 720, tabButtonWidth, tabButtonHeight); - buttonKeyboardSetup. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonKeyboardSetup. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonKeyboardSetup. - setText (lang.getString ("Keyboardsetup")); - - int - currentLine = 650; // reset line pos - int - currentLabelStart = leftLabelStart; // set to right side - int - currentColumnStart = leftColumnStart; // set to right side - - - // external server port - labelPublishServerExternalPort. - init (currentLabelStart, currentLine, 150); - labelPublishServerExternalPort. - setText (lang.getString ("PublishServerExternalPort")); - - labelExternalPort. - init (currentColumnStart, currentLine); - string - extPort = config.getString ("PortExternal", "not set"); - if (extPort == "not set" || extPort == "0") - { - extPort = " --- "; - } - else - { - extPort = "!!! " + extPort + " !!!"; - } - labelExternalPort. - setText (extPort); - - currentLine -= lineOffset; - // server port - labelServerPortLabel.init (currentLabelStart, currentLine); - labelServerPortLabel.setText (lang.getString ("ServerPort")); - - listBoxServerPort.init (currentColumnStart, currentLine, 160); - - string - portListString = - config.getString ("PortList", - intToStr (GameConstants::serverPort).c_str ()); - std::vector < std::string > portList; - Tokenize (portListString, portList, ","); - - string - currentPort = - config.getString ("PortServer", - intToStr (GameConstants::serverPort).c_str ()); - int - portSelectionIndex = 0; - for (int idx = 0; idx < (int) portList.size (); idx++) - { - if (portList[idx] != "" - && IsNumeric (portList[idx].c_str (), false)) - { - listBoxServerPort.pushBackItem (portList[idx]); - if (currentPort == portList[idx]) - { - portSelectionIndex = idx; - } - } - } - listBoxServerPort.setSelectedItemIndex (portSelectionIndex); - - currentLine -= lineOffset; - labelFTPServerPortLabel.init (currentLabelStart, currentLine); - labelFTPServerPortLabel.setText (lang.getString ("FTPServerPort")); - - int - FTPPort = - config.getInt ("FTPServerPort", - intToStr (ServerSocket::getFTPServerPort ()). - c_str ()); - labelFTPServerPort.init (currentColumnStart, currentLine); - labelFTPServerPort.setText (intToStr (FTPPort)); - currentLine -= lineOffset; - labelFTPServerDataPortsLabel.init (currentLabelStart, currentLine); - labelFTPServerDataPortsLabel.setText (lang. - getString - ("FTPServerDataPort")); - - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%d - %d", FTPPort + 1, - FTPPort + GameConstants::maxPlayers); - - labelFTPServerDataPorts.init (currentColumnStart, currentLine); - labelFTPServerDataPorts.setText (szBuf); - currentLine -= lineOffset; - labelEnableFTPServer.init (currentLabelStart, currentLine); - labelEnableFTPServer.setText (lang.getString ("EnableFTPServer")); - - checkBoxEnableFTPServer.init (currentColumnStart, currentLine); - checkBoxEnableFTPServer.setValue (config. - getBool ("EnableFTPServer", - "true")); - currentLine -= lineOffset; - // FTP Config - start - labelEnableFTP.init (currentLabelStart, currentLine); - labelEnableFTP.setText (lang.getString ("EnableFTP")); - - checkBoxEnableFTP.init (currentColumnStart, currentLine); - checkBoxEnableFTP.setValue (config.getBool ("EnableFTPXfer", "true")); - currentLine -= lineOffset; - - labelEnableFTPServerInternetTilesetXfer.init (currentLabelStart, - currentLine); - labelEnableFTPServerInternetTilesetXfer.setText (lang. - getString - ("EnableFTPServerInternetTilesetXfer")); - - checkBoxEnableFTPServerInternetTilesetXfer.init (currentColumnStart, - currentLine); - checkBoxEnableFTPServerInternetTilesetXfer.setValue (config. - getBool - ("EnableFTPServerInternetTilesetXfer", - "true")); - - currentLine -= lineOffset; - - labelEnableFTPServerInternetTechtreeXfer.init (currentLabelStart, - currentLine); - labelEnableFTPServerInternetTechtreeXfer.setText (lang. - getString - ("EnableFTPServerInternetTechtreeXfer")); - - checkBoxEnableFTPServerInternetTechtreeXfer.init (currentColumnStart, - currentLine); - checkBoxEnableFTPServerInternetTechtreeXfer.setValue (config. - getBool - ("EnableFTPServerInternetTechtreeXfer", - "true")); - - currentLine -= lineOffset; - - - // FTP config end - - // Privacy flag - labelEnablePrivacy.init (currentLabelStart, currentLine); - labelEnablePrivacy.setText (lang.getString ("PrivacyPlease")); - - checkBoxEnablePrivacy.init (currentColumnStart, currentLine); - checkBoxEnablePrivacy.setValue (config. - getBool ("PrivacyPlease", "false")); - //currentLine-=lineOffset; - // end - - // buttons - buttonOk.init (buttonStartPos, buttonRowPos, 100); - buttonOk.setText (lang.getString ("Save")); - - buttonReturn.setText (lang.getString ("Return")); - buttonReturn.init (buttonStartPos + 110, buttonRowPos, 100); - - GraphicComponent::applyAllCustomProperties (containerName); - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error loading options: %s\n", - __FILE__, __FUNCTION__, __LINE__, - e.what ()); - throw - megaglest_runtime_error (string ("Error loading options msg: ") + - e.what ()); - } - } - - void - MenuStateOptionsNetwork::reloadUI () - { - Lang & lang = Lang::getInstance (); - - mainMessageBox.init (lang.getString ("Ok")); - - buttonAudioSection.setText (lang.getString ("Audio")); - buttonVideoSection.setText (lang.getString ("Video")); - buttonMiscSection.setText (lang.getString ("Misc")); - buttonNetworkSettings.setText (lang.getString ("Network")); - - std::vector < string > listboxData; - listboxData.push_back ("None"); - listboxData.push_back ("OpenAL"); - - listboxData.clear (); - listboxData.push_back ("Bilinear"); - listboxData.push_back ("Trilinear"); - - listboxData.clear (); - for (float f = 0.0; f < 2.1f; f = f + 0.1f) - { - listboxData.push_back (floatToStr (f)); - } - listboxData.clear (); - for (int i = 0; i < Renderer::sCount; ++i) - { - listboxData.push_back (lang. - getString (Renderer:: - shadowsToStr (static_cast < - Renderer::Shadows > - (i)))); - } - - - labelServerPortLabel.setText (lang.getString ("ServerPort")); - labelPublishServerExternalPort.setText (lang. - getString - ("PublishServerExternalPort")); - labelEnableFTP.setText (lang.getString ("EnableFTP")); - labelEnableFTPServer.setText (lang.getString ("EnableFTPServer")); - labelFTPServerPortLabel.setText (lang.getString ("FTPServerPort")); - labelFTPServerDataPortsLabel.setText (lang. - getString ("FTPServerDataPort")); - labelEnableFTPServerInternetTilesetXfer.setText (lang. - getString - ("EnableFTPServerInternetTilesetXfer")); - labelEnableFTPServerInternetTechtreeXfer.setText (lang. - getString - ("EnableFTPServerInternetTechtreeXfer")); - labelEnablePrivacy.setText (lang.getString ("PrivacyPlease")); - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - } - - void - MenuStateOptionsNetwork::mouseClick (int x, int y, - MouseButton mouseButton) - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - int - button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - saveConfig (); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - mainMenu->setState (new MenuStateOptions (program, mainMenu)); - } - else - { - mainMessageBox.setEnabled (false); - - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - } - } - - } - } - else if (buttonOk.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - saveConfig (); - //mainMenu->setState(new MenuStateOptions(program, mainMenu,this->parentUI)); - return; - } - else if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (this->parentUI != NULL) - { - *this->parentUI = NULL; - delete * - this-> - parentUI; - } - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - else if (buttonAudioSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsSound (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonNetworkSettings.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonMiscSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptions (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonVideoSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - return; - } - else if (buttonKeyboardSetup.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateKeysetup (program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen - //showMessageBox("Not implemented yet", "Keyboard setup", false); - return; - } - else - { - if (listBoxServerPort.mouseClick (x, y)) - { - int - selectedPort = strToInt (listBoxServerPort.getSelectedItem ()); - if (selectedPort < 10000) - { - selectedPort = GameConstants::serverPort; - } - // use the following ports for ftp - char - szBuf[8096] = ""; - snprintf (szBuf, 8096, "%d - %d", selectedPort + 2, - selectedPort + 1 + GameConstants::maxPlayers); - labelFTPServerPort.setText (intToStr (selectedPort + 1)); - labelFTPServerDataPorts.setText (szBuf); - } - - checkBoxEnableFTP.mouseClick (x, y); - checkBoxEnableFTPServer.mouseClick (x, y); - - checkBoxEnableFTPServerInternetTilesetXfer.mouseClick (x, y); - checkBoxEnableFTPServerInternetTechtreeXfer.mouseClick (x, y); - - checkBoxEnablePrivacy.mouseClick (x, y); - } - } - - void - MenuStateOptionsNetwork::mouseMove (int x, int y, const MouseState * ms) - { - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - - buttonOk.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - buttonKeyboardSetup.mouseMove (x, y); - buttonAudioSection.mouseMove (x, y); - buttonNetworkSettings.mouseMove (x, y); - buttonMiscSection.mouseMove (x, y); - buttonVideoSection.mouseMove (x, y); - listBoxServerPort.mouseMove (x, y); - checkBoxEnableFTP.mouseMove (x, y); - checkBoxEnableFTPServer.mouseMove (x, y); - checkBoxEnableFTPServerInternetTilesetXfer.mouseMove (x, y); - checkBoxEnableFTPServerInternetTechtreeXfer.mouseMove (x, y); - checkBoxEnablePrivacy.mouseMove (x, y); - } - -//bool MenuStateOptionsNetwork::isInSpecialKeyCaptureEvent() { -// return (activeInputLabel != NULL); -//} -// -//void MenuStateOptionsNetwork::keyDown(SDL_KeyboardEvent key) { -// if(activeInputLabel != NULL) { -// keyDownEditLabel(key, &activeInputLabel); -// } -//} - - void - MenuStateOptionsNetwork::keyPress (SDL_KeyboardEvent c) - { -// if(activeInputLabel != NULL) { -// //printf("[%d]\n",c); fflush(stdout); -// if( &labelPlayerName == activeInputLabel || -// &labelTransifexUser == activeInputLabel || -// &labelTransifexPwd == activeInputLabel || -// &labelTransifexI18N == activeInputLabel) { -// textInputEditLabel(c, &activeInputLabel); -// } -// } -// else { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), c) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } -// } - } - - void - MenuStateOptionsNetwork::render () - { - Renderer & renderer = Renderer::getInstance (); - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else - { - renderer.renderButton (&buttonOk); - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonKeyboardSetup); - renderer.renderButton (&buttonVideoSection); - renderer.renderButton (&buttonAudioSection); - renderer.renderButton (&buttonMiscSection); - renderer.renderButton (&buttonNetworkSettings); - renderer.renderLabel (&labelServerPortLabel); - renderer.renderLabel (&labelExternalPort); - renderer.renderLabel (&labelPublishServerExternalPort); - renderer.renderListBox (&listBoxServerPort); - - - renderer.renderLabel (&labelEnableFTP); - renderer.renderCheckBox (&checkBoxEnableFTP); - - renderer.renderLabel (&labelEnableFTPServer); - renderer.renderCheckBox (&checkBoxEnableFTPServer); - - renderer.renderLabel (&labelFTPServerPortLabel); - renderer.renderLabel (&labelFTPServerPort); - renderer.renderLabel (&labelFTPServerDataPortsLabel); - renderer.renderLabel (&labelFTPServerDataPorts); - - renderer.renderLabel (&labelEnableFTPServerInternetTilesetXfer); - renderer.renderCheckBox (&checkBoxEnableFTPServerInternetTilesetXfer); - renderer.renderLabel (&labelEnableFTPServerInternetTechtreeXfer); - renderer. - renderCheckBox (&checkBoxEnableFTPServerInternetTechtreeXfer); - - renderer.renderLabel (&labelEnablePrivacy); - renderer.renderCheckBox (&checkBoxEnablePrivacy); - - } - - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } - - void - MenuStateOptionsNetwork::saveConfig () - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - setActiveInputLable (NULL); - - - lang.loadGameStrings (config.getString ("Lang")); - - config.setString ("PortServer", listBoxServerPort.getSelectedItem ()); - config.setInt ("FTPServerPort", config.getInt ("PortServer") + 1); - config.setBool ("EnableFTPXfer", checkBoxEnableFTP.getValue ()); - config.setBool ("EnableFTPServer", checkBoxEnableFTPServer.getValue ()); - - config.setBool ("EnableFTPServerInternetTilesetXfer", - checkBoxEnableFTPServerInternetTilesetXfer.getValue ()); - config.setBool ("EnableFTPServerInternetTechtreeXfer", - checkBoxEnableFTPServerInternetTechtreeXfer. - getValue ()); - - config.setBool ("PrivacyPlease", checkBoxEnablePrivacy.getValue ()); - - config.save (); - - Renderer::getInstance ().loadConfig (); - console.addLine (lang.getString ("SettingsSaved")); - } - - void - MenuStateOptionsNetwork::setActiveInputLable (GraphicLabel * newLable) - { - } - -}} //end namespace + Glest { + namespace + Game { + + // ===================================================== + // class MenuStateOptions + // ===================================================== + MenuStateOptionsNetwork::MenuStateOptionsNetwork(Program * program, + MainMenu * mainMenu, + ProgramState ** + parentUI) : + MenuState(program, mainMenu, "config"), + buttonOk("Options_Network", "buttonOk"), + buttonReturn("Options_Network", "buttonReturn"), + buttonKeyboardSetup("Options_Network", "buttonKeyboardSetup"), + buttonVideoSection("Options_Network", "buttonVideoSection"), + buttonAudioSection("Options_Network", "buttonAudioSection"), + buttonMiscSection("Options_Network", "buttonMiscSection"), + buttonNetworkSettings("Options_Network", "buttonNetworkSettings"), + mainMessageBox("Options_Network", "mainMessageBox"), + labelExternalPort("Options_Network", "labelExternalPort"), + labelServerPortLabel("Options_Network", "labelServerPortLabel"), + labelPublishServerExternalPort("Options_Network", + "labelPublishServerExternalPort"), + listBoxServerPort("Options_Network", "listBoxServerPort"), + labelEnableFTP("Options_Network", "labelEnableFTP"), + checkBoxEnableFTP("Options_Network", "checkBoxEnableFTP"), + labelEnableFTPServer("Options_Network", "labelEnableFTPServer"), + checkBoxEnableFTPServer("Options_Network", "checkBoxEnableFTPServer"), + labelFTPServerPortLabel("Options_Network", "labelFTPServerPortLabel"), + labelFTPServerPort("Options_Network", "labelFTPServerPort"), + labelFTPServerDataPortsLabel("Options_Network", + "labelFTPServerDataPortsLabel"), + labelFTPServerDataPorts("Options_Network", "labelFTPServerDataPorts"), + labelEnableFTPServerInternetTilesetXfer("Options_Network", + "labelEnableFTPServerInternetTilesetXfer"), + checkBoxEnableFTPServerInternetTilesetXfer("Options_Network", + "checkBoxEnableFTPServerInternetTilesetXfer"), + labelEnableFTPServerInternetTechtreeXfer("Options_Network", + "labelEnableFTPServerInternetTechtreeXfer"), + checkBoxEnableFTPServerInternetTechtreeXfer("Options_Network", + "checkBoxEnableFTPServerInternetTechtreeXfer"), + labelEnablePrivacy("Options_Network", "labelEnablePrivacy"), + checkBoxEnablePrivacy("Options_Network", "checkBoxEnablePrivacy") { + try { + containerName = "Options_Network"; + Lang & + lang = Lang::getInstance(); + Config & + config = Config::getInstance(); + this-> + parentUI = parentUI; + this-> + console. + setOnlyChatMessagesInStoredLines(false); + + int + leftLabelStart = 100; + int + leftColumnStart = leftLabelStart + 300; + int + buttonRowPos = 50; + int + buttonStartPos = 170; + int + lineOffset = 30; + int + tabButtonWidth = 200; + int + tabButtonHeight = 30; + + mainMessageBox. + init(lang.getString("Ok")); + mainMessageBox. + setEnabled(false); + mainMessageBoxState = 0; + + buttonAudioSection. + init(0, 720, tabButtonWidth, tabButtonHeight); + buttonAudioSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonAudioSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonAudioSection. + setText(lang.getString("Audio")); + // Video Section + buttonVideoSection. + init(200, 720, tabButtonWidth, tabButtonHeight); + buttonVideoSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonVideoSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonVideoSection. + setText(lang.getString("Video")); + //currentLine-=lineOffset; + //MiscSection + buttonMiscSection. + init(400, 720, tabButtonWidth, tabButtonHeight); + buttonMiscSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonMiscSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonMiscSection. + setText(lang.getString("Misc")); + //NetworkSettings + buttonNetworkSettings. + init(600, 700, tabButtonWidth, tabButtonHeight + 20); + buttonNetworkSettings. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonNetworkSettings. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonNetworkSettings. + setText(lang.getString("Network")); + + //KeyboardSetup + buttonKeyboardSetup. + init(800, 720, tabButtonWidth, tabButtonHeight); + buttonKeyboardSetup. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonKeyboardSetup. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonKeyboardSetup. + setText(lang.getString("Keyboardsetup")); + + int + currentLine = 650; // reset line pos + int + currentLabelStart = leftLabelStart; // set to right side + int + currentColumnStart = leftColumnStart; // set to right side + + + // external server port + labelPublishServerExternalPort. + init(currentLabelStart, currentLine, 150); + labelPublishServerExternalPort. + setText(lang.getString("PublishServerExternalPort")); + + labelExternalPort. + init(currentColumnStart, currentLine); + string + extPort = config.getString("PortExternal", "not set"); + if (extPort == "not set" || extPort == "0") { + extPort = " --- "; + } else { + extPort = "!!! " + extPort + " !!!"; + } + labelExternalPort. + setText(extPort); + + currentLine -= lineOffset; + // server port + labelServerPortLabel.init(currentLabelStart, currentLine); + labelServerPortLabel.setText(lang.getString("ServerPort")); + + listBoxServerPort.init(currentColumnStart, currentLine, 160); + + string + portListString = + config.getString("PortList", + intToStr(GameConstants::serverPort).c_str()); + std::vector < std::string > portList; + Tokenize(portListString, portList, ","); + + string + currentPort = + config.getString("PortServer", + intToStr(GameConstants::serverPort).c_str()); + int + portSelectionIndex = 0; + for (int idx = 0; idx < (int) portList.size(); idx++) { + if (portList[idx] != "" + && IsNumeric(portList[idx].c_str(), false)) { + listBoxServerPort.pushBackItem(portList[idx]); + if (currentPort == portList[idx]) { + portSelectionIndex = idx; + } + } + } + listBoxServerPort.setSelectedItemIndex(portSelectionIndex); + + currentLine -= lineOffset; + labelFTPServerPortLabel.init(currentLabelStart, currentLine); + labelFTPServerPortLabel.setText(lang.getString("FTPServerPort")); + + int + FTPPort = + config.getInt("FTPServerPort", + intToStr(ServerSocket::getFTPServerPort()). + c_str()); + labelFTPServerPort.init(currentColumnStart, currentLine); + labelFTPServerPort.setText(intToStr(FTPPort)); + currentLine -= lineOffset; + labelFTPServerDataPortsLabel.init(currentLabelStart, currentLine); + labelFTPServerDataPortsLabel.setText(lang. + getString + ("FTPServerDataPort")); + + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%d - %d", FTPPort + 1, + FTPPort + GameConstants::maxPlayers); + + labelFTPServerDataPorts.init(currentColumnStart, currentLine); + labelFTPServerDataPorts.setText(szBuf); + currentLine -= lineOffset; + labelEnableFTPServer.init(currentLabelStart, currentLine); + labelEnableFTPServer.setText(lang.getString("EnableFTPServer")); + + checkBoxEnableFTPServer.init(currentColumnStart, currentLine); + checkBoxEnableFTPServer.setValue(config. + getBool("EnableFTPServer", + "true")); + currentLine -= lineOffset; + // FTP Config - start + labelEnableFTP.init(currentLabelStart, currentLine); + labelEnableFTP.setText(lang.getString("EnableFTP")); + + checkBoxEnableFTP.init(currentColumnStart, currentLine); + checkBoxEnableFTP.setValue(config.getBool("EnableFTPXfer", "true")); + currentLine -= lineOffset; + + labelEnableFTPServerInternetTilesetXfer.init(currentLabelStart, + currentLine); + labelEnableFTPServerInternetTilesetXfer.setText(lang. + getString + ("EnableFTPServerInternetTilesetXfer")); + + checkBoxEnableFTPServerInternetTilesetXfer.init(currentColumnStart, + currentLine); + checkBoxEnableFTPServerInternetTilesetXfer.setValue(config. + getBool + ("EnableFTPServerInternetTilesetXfer", + "true")); + + currentLine -= lineOffset; + + labelEnableFTPServerInternetTechtreeXfer.init(currentLabelStart, + currentLine); + labelEnableFTPServerInternetTechtreeXfer.setText(lang. + getString + ("EnableFTPServerInternetTechtreeXfer")); + + checkBoxEnableFTPServerInternetTechtreeXfer.init(currentColumnStart, + currentLine); + checkBoxEnableFTPServerInternetTechtreeXfer.setValue(config. + getBool + ("EnableFTPServerInternetTechtreeXfer", + "true")); + + currentLine -= lineOffset; + + + // FTP config end + + // Privacy flag + labelEnablePrivacy.init(currentLabelStart, currentLine); + labelEnablePrivacy.setText(lang.getString("PrivacyPlease")); + + checkBoxEnablePrivacy.init(currentColumnStart, currentLine); + checkBoxEnablePrivacy.setValue(config. + getBool("PrivacyPlease", "false")); + //currentLine-=lineOffset; + // end + + // buttons + buttonOk.init(buttonStartPos, buttonRowPos, 100); + buttonOk.setText(lang.getString("Save")); + + buttonReturn.setText(lang.getString("Return")); + buttonReturn.init(buttonStartPos + 110, buttonRowPos, 100); + + GraphicComponent::applyAllCustomProperties(containerName); + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error loading options: %s\n", + __FILE__, __FUNCTION__, __LINE__, + e.what()); + throw + megaglest_runtime_error(string("Error loading options msg: ") + + e.what()); + } + } + + void + MenuStateOptionsNetwork::reloadUI() { + Lang & lang = Lang::getInstance(); + + mainMessageBox.init(lang.getString("Ok")); + + buttonAudioSection.setText(lang.getString("Audio")); + buttonVideoSection.setText(lang.getString("Video")); + buttonMiscSection.setText(lang.getString("Misc")); + buttonNetworkSettings.setText(lang.getString("Network")); + + std::vector < string > listboxData; + listboxData.push_back("None"); + listboxData.push_back("OpenAL"); + + listboxData.clear(); + listboxData.push_back("Bilinear"); + listboxData.push_back("Trilinear"); + + listboxData.clear(); + for (float f = 0.0; f < 2.1f; f = f + 0.1f) { + listboxData.push_back(floatToStr(f)); + } + listboxData.clear(); + for (int i = 0; i < Renderer::sCount; ++i) { + listboxData.push_back(lang. + getString(Renderer:: + shadowsToStr(static_cast < + Renderer::Shadows> + (i)))); + } + + + labelServerPortLabel.setText(lang.getString("ServerPort")); + labelPublishServerExternalPort.setText(lang. + getString + ("PublishServerExternalPort")); + labelEnableFTP.setText(lang.getString("EnableFTP")); + labelEnableFTPServer.setText(lang.getString("EnableFTPServer")); + labelFTPServerPortLabel.setText(lang.getString("FTPServerPort")); + labelFTPServerDataPortsLabel.setText(lang. + getString("FTPServerDataPort")); + labelEnableFTPServerInternetTilesetXfer.setText(lang. + getString + ("EnableFTPServerInternetTilesetXfer")); + labelEnableFTPServerInternetTechtreeXfer.setText(lang. + getString + ("EnableFTPServerInternetTechtreeXfer")); + labelEnablePrivacy.setText(lang.getString("PrivacyPlease")); + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + } + + void + MenuStateOptionsNetwork::mouseClick(int x, int y, + MouseButton mouseButton) { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + int + button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + saveConfig(); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + mainMenu->setState(new MenuStateOptions(program, mainMenu)); + } else { + mainMessageBox.setEnabled(false); + + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + } + } + + } + } else if (buttonOk.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + saveConfig(); + //mainMenu->setState(new MenuStateOptions(program, mainMenu,this->parentUI)); + return; + } else if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (this->parentUI != NULL) { + *this->parentUI = NULL; + delete * + this-> + parentUI; + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } else if (buttonAudioSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsSound(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonNetworkSettings.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu,this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonMiscSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptions(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonVideoSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + return; + } else if (buttonKeyboardSetup.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateKeysetup(program, mainMenu, this->parentUI)); // open keyboard shortcuts setup screen + //showMessageBox("Not implemented yet", "Keyboard setup", false); + return; + } else { + if (listBoxServerPort.mouseClick(x, y)) { + int + selectedPort = strToInt(listBoxServerPort.getSelectedItem()); + if (selectedPort < 10000) { + selectedPort = GameConstants::serverPort; + } + // use the following ports for ftp + char + szBuf[8096] = ""; + snprintf(szBuf, 8096, "%d - %d", selectedPort + 2, + selectedPort + 1 + GameConstants::maxPlayers); + labelFTPServerPort.setText(intToStr(selectedPort + 1)); + labelFTPServerDataPorts.setText(szBuf); + } + + checkBoxEnableFTP.mouseClick(x, y); + checkBoxEnableFTPServer.mouseClick(x, y); + + checkBoxEnableFTPServerInternetTilesetXfer.mouseClick(x, y); + checkBoxEnableFTPServerInternetTechtreeXfer.mouseClick(x, y); + + checkBoxEnablePrivacy.mouseClick(x, y); + } + } + + void + MenuStateOptionsNetwork::mouseMove(int x, int y, const MouseState * ms) { + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + + buttonOk.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + buttonKeyboardSetup.mouseMove(x, y); + buttonAudioSection.mouseMove(x, y); + buttonNetworkSettings.mouseMove(x, y); + buttonMiscSection.mouseMove(x, y); + buttonVideoSection.mouseMove(x, y); + listBoxServerPort.mouseMove(x, y); + checkBoxEnableFTP.mouseMove(x, y); + checkBoxEnableFTPServer.mouseMove(x, y); + checkBoxEnableFTPServerInternetTilesetXfer.mouseMove(x, y); + checkBoxEnableFTPServerInternetTechtreeXfer.mouseMove(x, y); + checkBoxEnablePrivacy.mouseMove(x, y); + } + + //bool MenuStateOptionsNetwork::isInSpecialKeyCaptureEvent() { + // return (activeInputLabel != NULL); + //} + // + //void MenuStateOptionsNetwork::keyDown(SDL_KeyboardEvent key) { + // if(activeInputLabel != NULL) { + // keyDownEditLabel(key, &activeInputLabel); + // } + //} + + void + MenuStateOptionsNetwork::keyPress(SDL_KeyboardEvent c) { + // if(activeInputLabel != NULL) { + // //printf("[%d]\n",c); fflush(stdout); + // if( &labelPlayerName == activeInputLabel || + // &labelTransifexUser == activeInputLabel || + // &labelTransifexPwd == activeInputLabel || + // &labelTransifexI18N == activeInputLabel) { + // textInputEditLabel(c, &activeInputLabel); + // } + // } + // else { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), c) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + // } + } + + void + MenuStateOptionsNetwork::render() { + Renderer & renderer = Renderer::getInstance(); + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else { + renderer.renderButton(&buttonOk); + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonKeyboardSetup); + renderer.renderButton(&buttonVideoSection); + renderer.renderButton(&buttonAudioSection); + renderer.renderButton(&buttonMiscSection); + renderer.renderButton(&buttonNetworkSettings); + renderer.renderLabel(&labelServerPortLabel); + renderer.renderLabel(&labelExternalPort); + renderer.renderLabel(&labelPublishServerExternalPort); + renderer.renderListBox(&listBoxServerPort); + + + renderer.renderLabel(&labelEnableFTP); + renderer.renderCheckBox(&checkBoxEnableFTP); + + renderer.renderLabel(&labelEnableFTPServer); + renderer.renderCheckBox(&checkBoxEnableFTPServer); + + renderer.renderLabel(&labelFTPServerPortLabel); + renderer.renderLabel(&labelFTPServerPort); + renderer.renderLabel(&labelFTPServerDataPortsLabel); + renderer.renderLabel(&labelFTPServerDataPorts); + + renderer.renderLabel(&labelEnableFTPServerInternetTilesetXfer); + renderer.renderCheckBox(&checkBoxEnableFTPServerInternetTilesetXfer); + renderer.renderLabel(&labelEnableFTPServerInternetTechtreeXfer); + renderer. + renderCheckBox(&checkBoxEnableFTPServerInternetTechtreeXfer); + + renderer.renderLabel(&labelEnablePrivacy); + renderer.renderCheckBox(&checkBoxEnablePrivacy); + + } + + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } + + void + MenuStateOptionsNetwork::saveConfig() { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + setActiveInputLable(NULL); + + + lang.loadGameStrings(config.getString("Lang")); + + config.setString("PortServer", listBoxServerPort.getSelectedItem()); + config.setInt("FTPServerPort", config.getInt("PortServer") + 1); + config.setBool("EnableFTPXfer", checkBoxEnableFTP.getValue()); + config.setBool("EnableFTPServer", checkBoxEnableFTPServer.getValue()); + + config.setBool("EnableFTPServerInternetTilesetXfer", + checkBoxEnableFTPServerInternetTilesetXfer.getValue()); + config.setBool("EnableFTPServerInternetTechtreeXfer", + checkBoxEnableFTPServerInternetTechtreeXfer. + getValue()); + + config.setBool("PrivacyPlease", checkBoxEnablePrivacy.getValue()); + + config.save(); + + Renderer::getInstance().loadConfig(); + console.addLine(lang.getString("SettingsSaved")); + } + + void + MenuStateOptionsNetwork::setActiveInputLable(GraphicLabel * newLable) { + } + + } +} //end namespace diff --git a/source/glest_game/menu/menu_state_options_network.h b/source/glest_game/menu/menu_state_options_network.h index 750d93112..4d4d78750 100644 --- a/source/glest_game/menu/menu_state_options_network.h +++ b/source/glest_game/menu/menu_state_options_network.h @@ -15,81 +15,79 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateOptionsNetwork -// =============================== + // =============================== + // class MenuStateOptionsNetwork + // =============================== - class MenuStateOptionsNetwork:public MenuState - { - private: + class MenuStateOptionsNetwork :public MenuState { + private: - GraphicButton buttonOk; - GraphicButton buttonReturn; + GraphicButton buttonOk; + GraphicButton buttonReturn; - GraphicButton buttonKeyboardSetup; // configure the keyboard - GraphicButton buttonVideoSection; - GraphicButton buttonAudioSection; - GraphicButton buttonMiscSection; - GraphicButton buttonNetworkSettings; + GraphicButton buttonKeyboardSetup; // configure the keyboard + GraphicButton buttonVideoSection; + GraphicButton buttonAudioSection; + GraphicButton buttonMiscSection; + GraphicButton buttonNetworkSettings; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - GraphicLabel labelExternalPort; - GraphicLabel labelServerPortLabel; + GraphicLabel labelExternalPort; + GraphicLabel labelServerPortLabel; - GraphicLabel labelPublishServerExternalPort; - GraphicListBox listBoxServerPort; + GraphicLabel labelPublishServerExternalPort; + GraphicListBox listBoxServerPort; - GraphicLabel labelEnableFTP; - GraphicCheckBox checkBoxEnableFTP; + GraphicLabel labelEnableFTP; + GraphicCheckBox checkBoxEnableFTP; - GraphicLabel labelEnableFTPServer; - GraphicCheckBox checkBoxEnableFTPServer; + GraphicLabel labelEnableFTPServer; + GraphicCheckBox checkBoxEnableFTPServer; - GraphicLabel labelFTPServerPortLabel; - GraphicLabel labelFTPServerPort; + GraphicLabel labelFTPServerPortLabel; + GraphicLabel labelFTPServerPort; - GraphicLabel labelFTPServerDataPortsLabel; - GraphicLabel labelFTPServerDataPorts; + GraphicLabel labelFTPServerDataPortsLabel; + GraphicLabel labelFTPServerDataPorts; - GraphicLabel labelEnableFTPServerInternetTilesetXfer; - GraphicCheckBox checkBoxEnableFTPServerInternetTilesetXfer; + GraphicLabel labelEnableFTPServerInternetTilesetXfer; + GraphicCheckBox checkBoxEnableFTPServerInternetTilesetXfer; - GraphicLabel labelEnableFTPServerInternetTechtreeXfer; - GraphicCheckBox checkBoxEnableFTPServerInternetTechtreeXfer; + GraphicLabel labelEnableFTPServerInternetTechtreeXfer; + GraphicCheckBox checkBoxEnableFTPServerInternetTechtreeXfer; - GraphicLabel labelEnablePrivacy; - GraphicCheckBox checkBoxEnablePrivacy; + GraphicLabel labelEnablePrivacy; + GraphicCheckBox checkBoxEnablePrivacy; - ProgramState **parentUI; + ProgramState **parentUI; - public: - MenuStateOptionsNetwork (Program * program, MainMenu * mainMenu, - ProgramState ** parentUI = NULL); + public: + MenuStateOptionsNetwork(Program * program, MainMenu * mainMenu, + ProgramState ** parentUI = NULL); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - //virtual void keyDown(SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - //virtual bool isInSpecialKeyCaptureEvent(); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + //virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + //virtual bool isInSpecialKeyCaptureEvent(); - virtual void reloadUI (); + virtual void reloadUI(); - private: - void saveConfig (); - void setActiveInputLable (GraphicLabel * newLable); - //void showMessageBox(const string &text, const string &header, bool toggle); - }; + private: + void saveConfig(); + void setActiveInputLable(GraphicLabel * newLable); + //void showMessageBox(const string &text, const string &header, bool toggle); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_options_sound.cpp b/source/glest_game/menu/menu_state_options_sound.cpp index d958f3b28..5701f885d 100644 --- a/source/glest_game/menu/menu_state_options_sound.cpp +++ b/source/glest_game/menu/menu_state_options_sound.cpp @@ -29,527 +29,479 @@ #include "leak_dumper.h" using namespace - Shared::Util; +Shared::Util; namespace - Glest -{ - namespace - Game - { + Glest { + namespace + Game { -// ===================================================== -// class MenuStateOptions -// ===================================================== - MenuStateOptionsSound::MenuStateOptionsSound (Program * program, - MainMenu * mainMenu, - ProgramState ** parentUI): - MenuState (program, mainMenu, "config"), - buttonOk ("Options_Sound", "buttonOk"), - buttonReturn ("Options_Sound", "buttonReturn"), - buttonKeyboardSetup ("Options_Sound", "buttonKeyboardSetup"), - buttonVideoSection ("Options_Sound", "buttonVideoSection"), - buttonAudioSection ("Options_Sound", "buttonAudioSection"), - buttonMiscSection ("Options_Sound", "buttonMiscSection"), - buttonNetworkSettings ("Options_Sound", "buttonNetworkSettings"), - labelSoundFactory ("Options_Sound", "labelSoundFactory"), - listBoxSoundFactory ("Options_Sound", "listBoxSoundFactory"), - labelVolumeFx ("Options_Sound", "labelVolumeFx"), - listBoxVolumeFx ("Options_Sound", "listBoxVolumeFx"), - labelVolumeAmbient ("Options_Sound", "labelVolumeAmbient"), - listBoxVolumeAmbient ("Options_Sound", "listBoxVolumeAmbient"), - labelVolumeMusic ("Options_Sound", "labelVolumeMusic"), - listBoxVolumeMusic ("Options_Sound", "listBoxVolumeMusic"), - mainMessageBox ("Options_Sound", "mainMessageBox") - { - try - { - containerName = "Options_Sound"; - this-> - parentUI = parentUI; - Lang & - lang = Lang::getInstance (); - Config & - config = Config::getInstance (); - this-> - console. - setOnlyChatMessagesInStoredLines (false); + // ===================================================== + // class MenuStateOptions + // ===================================================== + MenuStateOptionsSound::MenuStateOptionsSound(Program * program, + MainMenu * mainMenu, + ProgramState ** parentUI) : + MenuState(program, mainMenu, "config"), + buttonOk("Options_Sound", "buttonOk"), + buttonReturn("Options_Sound", "buttonReturn"), + buttonKeyboardSetup("Options_Sound", "buttonKeyboardSetup"), + buttonVideoSection("Options_Sound", "buttonVideoSection"), + buttonAudioSection("Options_Sound", "buttonAudioSection"), + buttonMiscSection("Options_Sound", "buttonMiscSection"), + buttonNetworkSettings("Options_Sound", "buttonNetworkSettings"), + labelSoundFactory("Options_Sound", "labelSoundFactory"), + listBoxSoundFactory("Options_Sound", "listBoxSoundFactory"), + labelVolumeFx("Options_Sound", "labelVolumeFx"), + listBoxVolumeFx("Options_Sound", "listBoxVolumeFx"), + labelVolumeAmbient("Options_Sound", "labelVolumeAmbient"), + listBoxVolumeAmbient("Options_Sound", "listBoxVolumeAmbient"), + labelVolumeMusic("Options_Sound", "labelVolumeMusic"), + listBoxVolumeMusic("Options_Sound", "listBoxVolumeMusic"), + mainMessageBox("Options_Sound", "mainMessageBox") { + try { + containerName = "Options_Sound"; + this-> + parentUI = parentUI; + Lang & + lang = Lang::getInstance(); + Config & + config = Config::getInstance(); + this-> + console. + setOnlyChatMessagesInStoredLines(false); - int - leftLabelStart = 100; - int - leftColumnStart = leftLabelStart + 300; - int - buttonRowPos = 50; - int - buttonStartPos = 170; - int - lineOffset = 30; - int - tabButtonWidth = 200; - int - tabButtonHeight = 30; + int + leftLabelStart = 100; + int + leftColumnStart = leftLabelStart + 300; + int + buttonRowPos = 50; + int + buttonStartPos = 170; + int + lineOffset = 30; + int + tabButtonWidth = 200; + int + tabButtonHeight = 30; - mainMessageBox. - init (lang.getString ("Ok")); - mainMessageBox. - setEnabled (false); - mainMessageBoxState = 0; + mainMessageBox. + init(lang.getString("Ok")); + mainMessageBox. + setEnabled(false); + mainMessageBoxState = 0; - buttonAudioSection. - init (0, 700, tabButtonWidth, tabButtonHeight + 20); - buttonAudioSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonAudioSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonAudioSection. - setText (lang.getString ("Audio")); - // Video Section - buttonVideoSection. - init (200, 720, tabButtonWidth, tabButtonHeight); - buttonVideoSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonVideoSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonVideoSection. - setText (lang.getString ("Video")); - //currentLine-=lineOffset; - //MiscSection - buttonMiscSection. - init (400, 720, tabButtonWidth, tabButtonHeight); - buttonMiscSection. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonMiscSection. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonMiscSection. - setText (lang.getString ("Misc")); - //NetworkSettings - buttonNetworkSettings. - init (600, 720, tabButtonWidth, tabButtonHeight); - buttonNetworkSettings. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonNetworkSettings. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonNetworkSettings. - setText (lang.getString ("Network")); + buttonAudioSection. + init(0, 700, tabButtonWidth, tabButtonHeight + 20); + buttonAudioSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonAudioSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonAudioSection. + setText(lang.getString("Audio")); + // Video Section + buttonVideoSection. + init(200, 720, tabButtonWidth, tabButtonHeight); + buttonVideoSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonVideoSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonVideoSection. + setText(lang.getString("Video")); + //currentLine-=lineOffset; + //MiscSection + buttonMiscSection. + init(400, 720, tabButtonWidth, tabButtonHeight); + buttonMiscSection. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonMiscSection. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonMiscSection. + setText(lang.getString("Misc")); + //NetworkSettings + buttonNetworkSettings. + init(600, 720, tabButtonWidth, tabButtonHeight); + buttonNetworkSettings. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonNetworkSettings. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonNetworkSettings. + setText(lang.getString("Network")); - //KeyboardSetup - buttonKeyboardSetup. - init (800, 720, tabButtonWidth, tabButtonHeight); - buttonKeyboardSetup. - setFont (CoreData::getInstance ().getMenuFontVeryBig ()); - buttonKeyboardSetup. - setFont3D (CoreData::getInstance ().getMenuFontVeryBig3D ()); - buttonKeyboardSetup. - setText (lang.getString ("Keyboardsetup")); + //KeyboardSetup + buttonKeyboardSetup. + init(800, 720, tabButtonWidth, tabButtonHeight); + buttonKeyboardSetup. + setFont(CoreData::getInstance().getMenuFontVeryBig()); + buttonKeyboardSetup. + setFont3D(CoreData::getInstance().getMenuFontVeryBig3D()); + buttonKeyboardSetup. + setText(lang.getString("Keyboardsetup")); - int - currentLine = 650; // reset line pos - int - currentLabelStart = leftLabelStart; // set to right side - int - currentColumnStart = leftColumnStart; // set to right side + int + currentLine = 650; // reset line pos + int + currentLabelStart = leftLabelStart; // set to right side + int + currentColumnStart = leftColumnStart; // set to right side - //soundboxes - labelSoundFactory. - init (currentLabelStart, currentLine); - labelSoundFactory. - setText (lang.getString ("SoundAndMusic2")); + //soundboxes + labelSoundFactory. + init(currentLabelStart, currentLine); + labelSoundFactory. + setText(lang.getString("SoundAndMusic2")); - listBoxSoundFactory. - init (currentColumnStart, currentLine, 175); - listBoxSoundFactory. - pushBackItem (lang.getString ("None")); - listBoxSoundFactory. - pushBackItem ("OpenAL"); + listBoxSoundFactory. + init(currentColumnStart, currentLine, 175); + listBoxSoundFactory. + pushBackItem(lang.getString("None")); + listBoxSoundFactory. + pushBackItem("OpenAL"); - string - FSoundMode = config.getString ("FactorySound"); - string - FSoundModeT = lang.getString (config.getString ("FactorySound")); - if (FSoundModeT != "???" + FSoundMode + "???") - { - FSoundMode = FSoundModeT; - } - listBoxSoundFactory. - setSelectedItem (FSoundMode); - currentLine -= - lineOffset; + string + FSoundMode = config.getString("FactorySound"); + string + FSoundModeT = lang.getString(config.getString("FactorySound")); + if (FSoundModeT != "???" + FSoundMode + "???") { + FSoundMode = FSoundModeT; + } + listBoxSoundFactory. + setSelectedItem(FSoundMode); + currentLine -= + lineOffset; - labelVolumeFx. - init (currentLabelStart, currentLine); - labelVolumeFx. - setText (lang.getString ("FxVolume")); + labelVolumeFx. + init(currentLabelStart, currentLine); + labelVolumeFx. + setText(lang.getString("FxVolume")); - listBoxVolumeFx. - init (currentColumnStart, currentLine, 80); - currentLine -= - lineOffset; + listBoxVolumeFx. + init(currentColumnStart, currentLine, 80); + currentLine -= + lineOffset; - labelVolumeAmbient. - init (currentLabelStart, currentLine); + labelVolumeAmbient. + init(currentLabelStart, currentLine); - listBoxVolumeAmbient. - init (currentColumnStart, currentLine, 80); - labelVolumeAmbient. - setText (lang.getString ("AmbientVolume")); - currentLine -= - lineOffset; + listBoxVolumeAmbient. + init(currentColumnStart, currentLine, 80); + labelVolumeAmbient. + setText(lang.getString("AmbientVolume")); + currentLine -= + lineOffset; - labelVolumeMusic. - init (currentLabelStart, currentLine); + labelVolumeMusic. + init(currentLabelStart, currentLine); - listBoxVolumeMusic. - init (currentColumnStart, currentLine, 80); - labelVolumeMusic. - setText (lang.getString ("MusicVolume")); - //currentLine-=lineOffset; + listBoxVolumeMusic. + init(currentColumnStart, currentLine, 80); + labelVolumeMusic. + setText(lang.getString("MusicVolume")); + //currentLine-=lineOffset; - for (int i = 0; i <= 100; i += 5) - { - listBoxVolumeFx.pushBackItem (intToStr (i)); - listBoxVolumeAmbient. - pushBackItem (intToStr (i)); - listBoxVolumeMusic. - pushBackItem (intToStr (i)); - } - listBoxVolumeFx. - setSelectedItem (intToStr (config.getInt ("SoundVolumeFx") / 5 * 5)); - listBoxVolumeAmbient. - setSelectedItem (intToStr - (config.getInt ("SoundVolumeAmbient") / 5 * 5)); - listBoxVolumeMusic. - setSelectedItem (intToStr - (config.getInt ("SoundVolumeMusic") / 5 * 5)); + for (int i = 0; i <= 100; i += 5) { + listBoxVolumeFx.pushBackItem(intToStr(i)); + listBoxVolumeAmbient. + pushBackItem(intToStr(i)); + listBoxVolumeMusic. + pushBackItem(intToStr(i)); + } + listBoxVolumeFx. + setSelectedItem(intToStr(config.getInt("SoundVolumeFx") / 5 * 5)); + listBoxVolumeAmbient. + setSelectedItem(intToStr + (config.getInt("SoundVolumeAmbient") / 5 * 5)); + listBoxVolumeMusic. + setSelectedItem(intToStr + (config.getInt("SoundVolumeMusic") / 5 * 5)); - //currentLine-=lineOffset/2; + //currentLine-=lineOffset/2; - ////////////////////////////////////////////////////////////////// - ///////// RIGHT SIDE - ////////////////////////////////////////////////////////////////// + ////////////////////////////////////////////////////////////////// + ///////// RIGHT SIDE + ////////////////////////////////////////////////////////////////// - //currentLine=700; // reset line pos - //currentLabelStart=rightLabelStart; // set to right side - //currentColumnStart=rightColumnStart; // set to right side + //currentLine=700; // reset line pos + //currentLabelStart=rightLabelStart; // set to right side + //currentColumnStart=rightColumnStart; // set to right side - // buttons - buttonOk.init (buttonStartPos, buttonRowPos, 100); - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); + // buttons + buttonOk.init(buttonStartPos, buttonRowPos, 100); + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); - buttonReturn.init (buttonStartPos + 110, buttonRowPos, 100); + buttonReturn.init(buttonStartPos + 110, buttonRowPos, 100); - GraphicComponent::applyAllCustomProperties (containerName); - } - catch (exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error loading options: %s\n", - __FILE__, __FUNCTION__, __LINE__, - e.what ()); - throw - megaglest_runtime_error (string ("Error loading options msg: ") + - e.what ()); - } - } + GraphicComponent::applyAllCustomProperties(containerName); + } catch (exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error loading options: %s\n", + __FILE__, __FUNCTION__, __LINE__, + e.what()); + throw + megaglest_runtime_error(string("Error loading options msg: ") + + e.what()); + } + } - void - MenuStateOptionsSound::reloadUI () - { - Lang & lang = Lang::getInstance (); + void + MenuStateOptionsSound::reloadUI() { + Lang & lang = Lang::getInstance(); - mainMessageBox.init (lang.getString ("Ok")); + mainMessageBox.init(lang.getString("Ok")); - buttonAudioSection.setText (lang.getString ("Audio")); - buttonVideoSection.setText (lang.getString ("Video")); - buttonMiscSection.setText (lang.getString ("Misc")); - buttonNetworkSettings.setText (lang.getString ("Network")); - labelSoundFactory.setText (lang.getString ("SoundAndMusic2")); + buttonAudioSection.setText(lang.getString("Audio")); + buttonVideoSection.setText(lang.getString("Video")); + buttonMiscSection.setText(lang.getString("Misc")); + buttonNetworkSettings.setText(lang.getString("Network")); + labelSoundFactory.setText(lang.getString("SoundAndMusic2")); - std::vector < string > listboxData; - listboxData.push_back (lang.getString ("None")); - listboxData.push_back ("OpenAL"); + std::vector < string > listboxData; + listboxData.push_back(lang.getString("None")); + listboxData.push_back("OpenAL"); - listBoxSoundFactory.setItems (listboxData); + listBoxSoundFactory.setItems(listboxData); - labelVolumeFx.setText (lang.getString ("FxVolume")); - labelVolumeAmbient.setText (lang.getString ("AmbientVolume")); - labelVolumeMusic.setText (lang.getString ("MusicVolume")); + labelVolumeFx.setText(lang.getString("FxVolume")); + labelVolumeAmbient.setText(lang.getString("AmbientVolume")); + labelVolumeMusic.setText(lang.getString("MusicVolume")); - listboxData.clear (); + listboxData.clear(); - buttonOk.setText (lang.getString ("Save")); - buttonReturn.setText (lang.getString ("Return")); - } + buttonOk.setText(lang.getString("Save")); + buttonReturn.setText(lang.getString("Return")); + } - void - MenuStateOptionsSound::mouseClick (int x, int y, MouseButton mouseButton) - { + void + MenuStateOptionsSound::mouseClick(int x, int y, MouseButton mouseButton) { - //Config &config= Config::getInstance(); - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); + //Config &config= Config::getInstance(); + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); - if (mainMessageBox.getEnabled ()) - { - int - button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); - saveConfig (); + if (mainMessageBox.getEnabled()) { + int + button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); + saveConfig(); - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - mainMenu->setState (new MenuStateOptions (program, mainMenu)); - } - else - { - mainMessageBox.setEnabled (false); + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + mainMenu->setState(new MenuStateOptions(program, mainMenu)); + } else { + mainMessageBox.setEnabled(false); - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); - } - } - else - { - if (mainMessageBoxState == 1) - { - mainMessageBoxState = 0; - mainMessageBox.setEnabled (false); + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); + } + } else { + if (mainMessageBoxState == 1) { + mainMessageBoxState = 0; + mainMessageBox.setEnabled(false); - Lang & lang = Lang::getInstance (); - mainMessageBox.init (lang.getString ("Ok")); + Lang & lang = Lang::getInstance(); + mainMessageBox.init(lang.getString("Ok")); - this->mainMenu->init (); - } - } - } - } - else if (buttonOk.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - saveConfig (); - //mainMenu->setState(new MenuStateOptions(program, mainMenu)); - return; - } - else if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (this->parentUI != NULL) - { - *this->parentUI = NULL; - delete * - this-> - parentUI; - } - mainMenu->setState (new MenuStateRoot (program, mainMenu)); - return; - } - else if (buttonKeyboardSetup.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); // open keyboard shortcuts setup screen - //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); // open keyboard shortcuts setup screen - //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu)); // open keyboard shortcuts setup screen - mainMenu->setState (new MenuStateKeysetup (program, mainMenu)); // open keyboard shortcuts setup screen - //showMessageBox("Not implemented yet", "Keyboard setup", false); - return; - } - else if (buttonAudioSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - //mainMenu->setState(new MenuStateOptionsSound(program, mainMenu)); // open keyboard shortcuts setup screen - return; - } - else if (buttonNetworkSettings.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsNetwork (program, mainMenu)); // open keyboard shortcuts setup screen - return; - } - else if (buttonMiscSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptions (program, mainMenu)); // open keyboard shortcuts setup screen - return; - } - else if (buttonVideoSection.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateOptionsGraphics (program, mainMenu)); // open keyboard shortcuts setup screen - return; - } - else - { - listBoxSoundFactory.mouseClick (x, y); - listBoxVolumeFx.mouseClick (x, y); - listBoxVolumeAmbient.mouseClick (x, y); - listBoxVolumeMusic.mouseClick (x, y); - } - } + this->mainMenu->init(); + } + } + } + } else if (buttonOk.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + saveConfig(); + //mainMenu->setState(new MenuStateOptions(program, mainMenu)); + return; + } else if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (this->parentUI != NULL) { + *this->parentUI = NULL; + delete * + this-> + parentUI; + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + return; + } else if (buttonKeyboardSetup.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); // open keyboard shortcuts setup screen + //mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); // open keyboard shortcuts setup screen + //mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu)); // open keyboard shortcuts setup screen + mainMenu->setState(new MenuStateKeysetup(program, mainMenu)); // open keyboard shortcuts setup screen + //showMessageBox("Not implemented yet", "Keyboard setup", false); + return; + } else if (buttonAudioSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + //mainMenu->setState(new MenuStateOptionsSound(program, mainMenu)); // open keyboard shortcuts setup screen + return; + } else if (buttonNetworkSettings.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsNetwork(program, mainMenu)); // open keyboard shortcuts setup screen + return; + } else if (buttonMiscSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptions(program, mainMenu)); // open keyboard shortcuts setup screen + return; + } else if (buttonVideoSection.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateOptionsGraphics(program, mainMenu)); // open keyboard shortcuts setup screen + return; + } else { + listBoxSoundFactory.mouseClick(x, y); + listBoxVolumeFx.mouseClick(x, y); + listBoxVolumeAmbient.mouseClick(x, y); + listBoxVolumeMusic.mouseClick(x, y); + } + } - void - MenuStateOptionsSound::mouseMove (int x, int y, const MouseState * ms) - { - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - buttonOk.mouseMove (x, y); - buttonReturn.mouseMove (x, y); - buttonKeyboardSetup.mouseMove (x, y); - buttonAudioSection.mouseMove (x, y); - buttonNetworkSettings.mouseMove (x, y); - buttonMiscSection.mouseMove (x, y); - buttonVideoSection.mouseMove (x, y); + void + MenuStateOptionsSound::mouseMove(int x, int y, const MouseState * ms) { + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + buttonOk.mouseMove(x, y); + buttonReturn.mouseMove(x, y); + buttonKeyboardSetup.mouseMove(x, y); + buttonAudioSection.mouseMove(x, y); + buttonNetworkSettings.mouseMove(x, y); + buttonMiscSection.mouseMove(x, y); + buttonVideoSection.mouseMove(x, y); - listBoxSoundFactory.mouseMove (x, y); - listBoxVolumeFx.mouseMove (x, y); - listBoxVolumeAmbient.mouseMove (x, y); - listBoxVolumeMusic.mouseMove (x, y); - } + listBoxSoundFactory.mouseMove(x, y); + listBoxVolumeFx.mouseMove(x, y); + listBoxVolumeAmbient.mouseMove(x, y); + listBoxVolumeMusic.mouseMove(x, y); + } -//bool MenuStateOptionsSound::isInSpecialKeyCaptureEvent() { -// return (activeInputLabel != NULL); -//} -// -//void MenuStateOptionsSound::keyDown(SDL_KeyboardEvent key) { -// if(activeInputLabel != NULL) { -// keyDownEditLabel(key, &activeInputLabel); -// } -//} + //bool MenuStateOptionsSound::isInSpecialKeyCaptureEvent() { + // return (activeInputLabel != NULL); + //} + // + //void MenuStateOptionsSound::keyDown(SDL_KeyboardEvent key) { + // if(activeInputLabel != NULL) { + // keyDownEditLabel(key, &activeInputLabel); + // } + //} - void - MenuStateOptionsSound::keyPress (SDL_KeyboardEvent c) - { -// if(activeInputLabel != NULL) { -// //printf("[%d]\n",c); fflush(stdout); -// if( &labelPlayerName == activeInputLabel || -// &labelTransifexUser == activeInputLabel || -// &labelTransifexPwd == activeInputLabel || -// &labelTransifexI18N == activeInputLabel) { -// textInputEditLabel(c, &activeInputLabel); -// } -// } -// else { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), c) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } -// } - } + void + MenuStateOptionsSound::keyPress(SDL_KeyboardEvent c) { + // if(activeInputLabel != NULL) { + // //printf("[%d]\n",c); fflush(stdout); + // if( &labelPlayerName == activeInputLabel || + // &labelTransifexUser == activeInputLabel || + // &labelTransifexPwd == activeInputLabel || + // &labelTransifexI18N == activeInputLabel) { + // textInputEditLabel(c, &activeInputLabel); + // } + // } + // else { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), c) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + // } + } - void - MenuStateOptionsSound::render () - { - Renderer & renderer = Renderer::getInstance (); + void + MenuStateOptionsSound::render() { + Renderer & renderer = Renderer::getInstance(); - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else - { - renderer.renderButton (&buttonOk); - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonKeyboardSetup); - renderer.renderButton (&buttonVideoSection); - renderer.renderButton (&buttonAudioSection); - renderer.renderButton (&buttonMiscSection); - renderer.renderButton (&buttonNetworkSettings); - renderer.renderListBox (&listBoxSoundFactory); - renderer.renderLabel (&labelSoundFactory); + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else { + renderer.renderButton(&buttonOk); + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonKeyboardSetup); + renderer.renderButton(&buttonVideoSection); + renderer.renderButton(&buttonAudioSection); + renderer.renderButton(&buttonMiscSection); + renderer.renderButton(&buttonNetworkSettings); + renderer.renderListBox(&listBoxSoundFactory); + renderer.renderLabel(&labelSoundFactory); - renderer.renderListBox (&listBoxVolumeFx); - renderer.renderLabel (&labelVolumeFx); - renderer.renderListBox (&listBoxVolumeAmbient); - renderer.renderLabel (&labelVolumeAmbient); - renderer.renderListBox (&listBoxVolumeMusic); - renderer.renderLabel (&labelVolumeMusic); + renderer.renderListBox(&listBoxVolumeFx); + renderer.renderLabel(&labelVolumeFx); + renderer.renderListBox(&listBoxVolumeAmbient); + renderer.renderLabel(&labelVolumeAmbient); + renderer.renderListBox(&listBoxVolumeMusic); + renderer.renderLabel(&labelVolumeMusic); - } + } - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } - void - MenuStateOptionsSound::saveConfig () - { - Config & config = Config::getInstance (); - Lang & lang = Lang::getInstance (); - setActiveInputLable (NULL); + void + MenuStateOptionsSound::saveConfig() { + Config & config = Config::getInstance(); + Lang & lang = Lang::getInstance(); + setActiveInputLable(NULL); - int - FSoundIndex = listBoxSoundFactory.getSelectedItemIndex (); - string - FSoundMode; - switch (FSoundIndex) - { - case 0: - FSoundMode = "None"; - break; - case 1: - FSoundMode = "OpenAL"; - break; - } - config.setString ("FactorySound", FSoundMode); + int + FSoundIndex = listBoxSoundFactory.getSelectedItemIndex(); + string + FSoundMode; + switch (FSoundIndex) { + case 0: + FSoundMode = "None"; + break; + case 1: + FSoundMode = "OpenAL"; + break; + } + config.setString("FactorySound", FSoundMode); - config.setString ("SoundVolumeFx", listBoxVolumeFx.getSelectedItem ()); - config.setString ("SoundVolumeAmbient", - listBoxVolumeAmbient.getSelectedItem ()); - CoreData::getInstance ().getMenuMusic ()-> - setVolume (strToInt (listBoxVolumeMusic.getSelectedItem ()) / 100.f); - config.setString ("SoundVolumeMusic", - listBoxVolumeMusic.getSelectedItem ()); + config.setString("SoundVolumeFx", listBoxVolumeFx.getSelectedItem()); + config.setString("SoundVolumeAmbient", + listBoxVolumeAmbient.getSelectedItem()); + CoreData::getInstance().getMenuMusic()-> + setVolume(strToInt(listBoxVolumeMusic.getSelectedItem()) / 100.f); + config.setString("SoundVolumeMusic", + listBoxVolumeMusic.getSelectedItem()); - config.save (); + config.save(); - if (config.getBool ("DisableLuaSandbox", "false") == true) - { - LuaScript::setDisableSandbox (true); - } + if (config.getBool("DisableLuaSandbox", "false") == true) { + LuaScript::setDisableSandbox(true); + } - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - soundRenderer.stopAllSounds (); - program->stopSoundSystem (); - soundRenderer.init (program->getWindow ()); - soundRenderer.loadConfig (); - soundRenderer.setMusicVolume (CoreData::getInstance ().getMenuMusic ()); - program->startSoundSystem (); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + soundRenderer.stopAllSounds(); + program->stopSoundSystem(); + soundRenderer.init(program->getWindow()); + soundRenderer.loadConfig(); + soundRenderer.setMusicVolume(CoreData::getInstance().getMenuMusic()); + program->startSoundSystem(); - if (CoreData::getInstance ().hasMainMenuVideoFilename () == false) - { - soundRenderer.playMusic (CoreData::getInstance ().getMenuMusic ()); - } + if (CoreData::getInstance().hasMainMenuVideoFilename() == false) { + soundRenderer.playMusic(CoreData::getInstance().getMenuMusic()); + } - Renderer::getInstance ().loadConfig (); - console.addLine (lang.getString ("SettingsSaved")); - } + Renderer::getInstance().loadConfig(); + console.addLine(lang.getString("SettingsSaved")); + } - void - MenuStateOptionsSound::setActiveInputLable (GraphicLabel * newLable) - { - } + void + MenuStateOptionsSound::setActiveInputLable(GraphicLabel * newLable) { + } -}} //end namespace + } +} //end namespace diff --git a/source/glest_game/menu/menu_state_options_sound.h b/source/glest_game/menu/menu_state_options_sound.h index c8ea1cb50..f29498ce7 100644 --- a/source/glest_game/menu/menu_state_options_sound.h +++ b/source/glest_game/menu/menu_state_options_sound.h @@ -15,65 +15,63 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateOptionsSound -// =============================== + // =============================== + // class MenuStateOptionsSound + // =============================== - class MenuStateOptionsSound:public MenuState - { - private: + class MenuStateOptionsSound :public MenuState { + private: - GraphicButton buttonOk; - GraphicButton buttonReturn; + GraphicButton buttonOk; + GraphicButton buttonReturn; - GraphicButton buttonKeyboardSetup; // configure the keyboard - GraphicButton buttonVideoSection; - GraphicButton buttonAudioSection; - GraphicButton buttonMiscSection; - GraphicButton buttonNetworkSettings; + GraphicButton buttonKeyboardSetup; // configure the keyboard + GraphicButton buttonVideoSection; + GraphicButton buttonAudioSection; + GraphicButton buttonMiscSection; + GraphicButton buttonNetworkSettings; - GraphicLabel labelSoundFactory; - GraphicListBox listBoxSoundFactory; + GraphicLabel labelSoundFactory; + GraphicListBox listBoxSoundFactory; - GraphicLabel labelVolumeFx; - GraphicListBox listBoxVolumeFx; + GraphicLabel labelVolumeFx; + GraphicListBox listBoxVolumeFx; - GraphicLabel labelVolumeAmbient; - GraphicListBox listBoxVolumeAmbient; + GraphicLabel labelVolumeAmbient; + GraphicListBox listBoxVolumeAmbient; - GraphicLabel labelVolumeMusic; - GraphicListBox listBoxVolumeMusic; + GraphicLabel labelVolumeMusic; + GraphicListBox listBoxVolumeMusic; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - ProgramState **parentUI; + ProgramState **parentUI; - public: - MenuStateOptionsSound (Program * program, MainMenu * mainMenu, - ProgramState ** parentUI = NULL); + public: + MenuStateOptionsSound(Program * program, MainMenu * mainMenu, + ProgramState ** parentUI = NULL); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - //virtual void keyDown(SDL_KeyboardEvent key); - virtual void keyPress (SDL_KeyboardEvent c); - //virtual bool isInSpecialKeyCaptureEvent(); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + //virtual void keyDown(SDL_KeyboardEvent key); + virtual void keyPress(SDL_KeyboardEvent c); + //virtual bool isInSpecialKeyCaptureEvent(); - virtual void reloadUI (); + virtual void reloadUI(); - private: - void saveConfig (); - void setActiveInputLable (GraphicLabel * newLable); - //void showMessageBox(const string &text, const string &header, bool toggle); - }; + private: + void saveConfig(); + void setActiveInputLable(GraphicLabel * newLable); + //void showMessageBox(const string &text, const string &header, bool toggle); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_root.cpp b/source/glest_game/menu/menu_state_root.cpp index 12e6b3b58..9b7b48fca 100644 --- a/source/glest_game/menu/menu_state_root.cpp +++ b/source/glest_game/menu/menu_state_root.cpp @@ -31,1115 +31,978 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - -// ===================================================== -// class MenuStateRoot -// ===================================================== - - bool MenuStateRoot::gameUpdateChecked = false; - - MenuStateRoot::MenuStateRoot (Program * program, - MainMenu * mainMenu):MenuState (program, - mainMenu, - "root"), - updatesHttpServerThread (NULL), buttonNewGame ("MainMenu", - "buttonNewGame"), - buttonLoadGame ("MainMenu", "buttonLoadGame"), buttonMods ("MainMenu", - "buttonMods"), - buttonOptions ("MainMenu", "buttonOptions"), buttonAbout ("MainMenu", - "buttonAbout"), - buttonExit ("MainMenu", "buttonExit"), labelVersion ("MainMenu", - "labelVersion"), - labelGreeting ("MainMenu", "labelGreeting"), mainMessageBox ("MainMenu", - "mainMessageBox"), - errorMessageBox ("MainMenu", "errorMessageBox"), - ftpMessageBox ("MainMenu", "ftpMessageBox"), popupMenu ("MainMenu", - "popupMenu") - { - containerName = "MainMenu"; - - ftpClientThread = NULL; - lastDownloadProgress = 0; - - Lang & lang = Lang::getInstance (); - int yPos = 440; - int buttonWidth = 200; - int buttonXPosition = (1000 - buttonWidth) / 2; - - - //labelVersion.registerGraphicComponent(containerName,"labelVersion"); - if (EndsWith (glestVersionString, "-dev") == false) - { - labelVersion.init (525, yPos); - labelVersion.setText (glestVersionString); - } - else - { - labelVersion.init (buttonXPosition, yPos); - //labelVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); - labelVersion.setText (glestVersionString); - } - - labelGreeting.init (labelVersion.getX (), labelVersion.getY () - 16); - labelGreeting.setText (""); - - Steam *steamInstance = - CacheManager::getCachedItem < - Steam * >(GameConstants::steamCacheInstanceKey); - if (steamInstance != NULL) - { - string steamPlayerName = steamInstance->userName (); - labelGreeting.setText ("Welcome Steam Player: " + steamPlayerName); - } - - yPos -= 55; - //buttonNewGame.registerGraphicComponent(containerName,"buttonNewGame"); - buttonNewGame.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - //buttonLoadGame.registerGraphicComponent(containerName,"buttonLoadGame"); - buttonLoadGame.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - //buttonMods.registerGraphicComponent(containerName,"buttonMods"); - buttonMods.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - //buttonOptions.registerGraphicComponent(containerName,"buttonOptions"); - buttonOptions.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - //buttonAbout.registerGraphicComponent(containerName,"buttonAbout"); - buttonAbout.init (buttonXPosition, yPos, buttonWidth); - yPos -= 40; - //buttonExit.registerGraphicComponent(containerName,"buttonExit"); - buttonExit.init (buttonXPosition, yPos, buttonWidth); - - buttonNewGame.setText (lang.getString ("NewGame")); - buttonLoadGame.setText (lang.getString ("LoadGame")); - buttonMods.setText (lang.getString ("Mods")); - buttonOptions.setText (lang.getString ("Options")); - buttonAbout.setText (lang.getString ("About")); - buttonExit.setText (lang.getString ("Exit")); - - //mesage box - //mainMessageBox.registerGraphicComponent(containerName,"mainMessageBox"); - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - mainMessageBox.setEnabled (false); - - //errorMessageBox.registerGraphicComponent(containerName,"errorMessageBox"); - errorMessageBox.init (lang.getString ("Ok")); - errorMessageBox.setEnabled (false); - - //ftpMessageBox.registerGraphicComponent(containerName,"ftpMessageBox"); - ftpMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - ftpMessageBox.setEnabled (false); - - //PopupMenu popupMenu; - std::vector < string > menuItems; - menuItems.push_back ("1"); - menuItems.push_back ("2"); - menuItems.push_back ("3"); - //popupMenu.registerGraphicComponentOnlyFontCallbacks(containerName, "popupMenu"); - popupMenu.setW (100); - popupMenu.setH (100); - popupMenu.init ("Test Menu", menuItems); - popupMenu.setEnabled (false); - popupMenu.setVisible (false); - - GraphicComponent::applyAllCustomProperties (containerName); - } - - MenuStateRoot::~MenuStateRoot () - { - if (updatesHttpServerThread != NULL) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - updatesHttpServerThread->setSimpleTaskInterfaceValid (false); - updatesHttpServerThread->signalQuit (); - updatesHttpServerThread->setThreadOwnerValid (false); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (updatesHttpServerThread->canShutdown (true) == true - && updatesHttpServerThread->shutdownAndWait () == true) - { - delete updatesHttpServerThread; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - updatesHttpServerThread = NULL; - } - - if (ftpClientThread != NULL) - { - ftpClientThread->setCallBackObject (NULL); - ftpClientThread->signalQuit (); - sleep (0); - if (ftpClientThread->canShutdown (true) == true && - ftpClientThread->shutdownAndWait () == true) - { - delete ftpClientThread; - } - else - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("%s", szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - //publishToMasterserverThread->cleanup(); - } - ftpClientThread = NULL; - -// ftpClientThread->signalQuit(); -// ftpClientThread->setCallBackObject(NULL); -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); -// if( ftpClientThread->shutdownAndWait() == true) { -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); -// delete ftpClientThread; -// } -// ftpClientThread = NULL; -// if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - - } - - void MenuStateRoot::reloadUI () - { - Lang & lang = Lang::getInstance (); - - console.resetFonts (); - - if (EndsWith (glestVersionString, "-dev") == false) - { - labelVersion.setText (glestVersionString); - } - else - { - //labelVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); - labelVersion.setText (glestVersionString); - } - - buttonNewGame.setText (lang.getString ("NewGame")); - buttonLoadGame.setText (lang.getString ("LoadGame")); - buttonMods.setText (lang.getString ("Mods")); - buttonOptions.setText (lang.getString ("Options")); - buttonAbout.setText (lang.getString ("About")); - buttonExit.setText (lang.getString ("Exit")); - - mainMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - errorMessageBox.init (lang.getString ("Ok")); - ftpMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - - console.resetFonts (); - - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } - - void MenuStateRoot::mouseClick (int x, int y, MouseButton mouseButton) - { - try - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - - if (popupMenu.mouseClick (x, y)) - { - //std::pair result = popupMenu.mouseClickedMenuItem(x, y); - popupMenu.mouseClickedMenuItem (x, y); - - //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); - } - //exit message box, has to be the last thing to do in this function - else if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - if (button == 0) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - soundRenderer.playFx (coreData.getClickSoundA ()); - program->exit (); - } - else - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - //close message box - mainMessageBox.setEnabled (false); - } - } - } - //exit message box, has to be the last thing to do in this function - else if (errorMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - //close message box - errorMessageBox.setEnabled (false); - } - } - - else if (ftpMessageBox.getEnabled ()) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - - int button = 0; - if (ftpMessageBox.mouseClick (x, y, button)) - { - ftpMessageBox.setEnabled (false); - if (button == 0) - { - startFTPClientIfRequired (); - - lastDownloadProgress = 0; - printf ("Adding ftpFileName [%s] ftpFileURL [%s]\n", - ftpFileName.c_str (), ftpFileURL.c_str ()); - if (ftpClientThread != NULL) - ftpClientThread->addTempFileToRequests (ftpFileName, - ftpFileURL); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), - mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()-> - setOwnerId (mutexOwnerId); - fileFTPProgressList[ftpFileName] = pair < int, string > (0, ""); - safeMutexFTPProgress.ReleaseLock (); - } - } - } - else if (mainMessageBox.getEnabled () == false - && buttonNewGame.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - } - else if (mainMessageBox.getEnabled () == false - && buttonLoadGame.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateLoadGame (program, mainMenu)); - } - else if (mainMessageBox.getEnabled () == false - && buttonMods.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateMods (program, mainMenu)); - } - else if (mainMessageBox.getEnabled () == false - && buttonOptions.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateOptions (program, mainMenu)); - } - else if (mainMessageBox.getEnabled () == false - && buttonAbout.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundB ()); - mainMenu->setState (new MenuStateAbout (program, mainMenu)); - } - else if (buttonExit.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - program->exit (); - } - } - catch (exception & e) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d]\nError in menu event:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, e.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - showErrorMessageBox (szBuf, "", true); - } - } - - void MenuStateRoot::startFTPClientIfRequired () - { - if (ftpClientThread == NULL) - { - // Setup File Transfer thread - Config & config = Config::getInstance (); - - vector < string > tilesetFiles; - vector < string > tilesetFilesUserData; - - vector < string > techTreeFiles; - vector < string > techTreeFilesUserData; - - - findDirs (config.getPathListForType (ptTilesets), tilesetFiles); - findDirs (config.getPathListForType (ptTechs), techTreeFiles); - - vector < string > mapPathList = config.getPathListForType (ptMaps); - std::pair < string, string > mapsPath; - if (mapPathList.empty () == false) - { - mapsPath.first = mapPathList[0]; - } - if (mapPathList.size () > 1) - { - mapsPath.second = mapPathList[1]; - } - std::pair < string, string > tilesetsPath; - vector < string > tilesetsList = - Config::getInstance ().getPathListForType (ptTilesets); - if (tilesetsList.empty () == false) - { - tilesetsPath.first = tilesetsList[0]; - if (tilesetsList.size () > 1) - { - tilesetsPath.second = tilesetsList[1]; - } - } - - std::pair < string, string > techtreesPath; - vector < string > techtreesList = - Config::getInstance ().getPathListForType (ptTechs); - if (techtreesList.empty () == false) - { - techtreesPath.first = techtreesList[0]; - if (techtreesList.size () > 1) - { - techtreesPath.second = techtreesList[1]; - } - } - - std::pair < string, string > scenariosPath; - vector < string > scenariosList = - Config::getInstance ().getPathListForType (ptScenarios); - if (scenariosList.empty () == false) - { - scenariosPath.first = scenariosList[0]; - if (scenariosList.size () > 1) - { - scenariosPath.second = scenariosList[1]; - } - } - - string fileArchiveExtension = - config.getString ("FileArchiveExtension", ""); - string fileArchiveExtractCommand = - config.getString ("FileArchiveExtractCommand", ""); - string fileArchiveExtractCommandParameters = - config.getString ("FileArchiveExtractCommandParameters", ""); - int32 fileArchiveExtractCommandSuccessResult = - config.getInt ("FileArchiveExtractCommandSuccessResult", "0"); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - console.setOnlyChatMessagesInStoredLines (false); - - // Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) != - "") - { - tempFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } - else - { - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Temp files path [%s]\n", tempFilePath.c_str ()); - - ftpClientThread = new FTPClientThread (-1, "", - mapsPath, tilesetsPath, - techtreesPath, scenariosPath, - this, fileArchiveExtension, - fileArchiveExtractCommand, - fileArchiveExtractCommandParameters, - fileArchiveExtractCommandSuccessResult, - tempFilePath); - ftpClientThread->start (); - - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - } - } - - void MenuStateRoot::FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType type, - pair < FTP_Client_ResultType, - string > result, - void *userdata) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - Lang & lang = Lang::getInstance (); - if (type == ftp_cct_DownloadProgress) - { - FTPClientCallbackInterface::FtpProgressStats * stats = - (FTPClientCallbackInterface::FtpProgressStats *) userdata; - if (stats != NULL) - { - int fileProgress = 0; - if (stats->download_total > 0) - { - fileProgress = - ((stats->download_now / stats->download_total) * 100.0); - } - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - pair < int, string > lastProgress = fileFTPProgressList[itemName]; - fileFTPProgressList[itemName] = - pair < int, string > (fileProgress, stats->currentFilename); - safeMutexFTPProgress.ReleaseLock (); - - if (itemName != "" - && (lastDownloadProgress < fileProgress - && fileProgress % 25 == 0)) - { - lastDownloadProgress = fileProgress; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "Downloaded %d%% of file: %s", - fileProgress, itemName.c_str ()); - console.addLine (szBuf); - } - } - } - else if (type == ftp_cct_ExtractProgress) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP extract Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - printf ("Got FTP extract Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - if (userdata == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - lang.getString ("DataMissingExtractDownloadMod").c_str (), - itemName.c_str ()); - //printf("%s\n",szBuf); - console.addLine (szBuf, true); - } - else - { - char *szBuf = (char *) userdata; - //printf("%s\n",szBuf); - console.addLine (szBuf); - } - } - else if (type == ftp_cct_TempFile) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Got FTP Callback for [%s] result = %d [%s]\n", - itemName.c_str (), result.first, result.second.c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper - safeMutexFTPProgress ((ftpClientThread != - NULL ? ftpClientThread-> - getProgressMutex () : NULL), mutexOwnerId); - if (ftpClientThread != NULL - && ftpClientThread->getProgressMutex () != NULL) - ftpClientThread->getProgressMutex ()->setOwnerId (mutexOwnerId); - fileFTPProgressList.erase (itemName); - safeMutexFTPProgress.ReleaseLock (); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("### downloaded TEMP file [%s] result = %d\n", - itemName.c_str (), result.first); - - if (result.first == ftp_crt_SUCCESS) - { - // Get path to temp files - string tempFilePath = "temp/"; - if (getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) - != "") - { - tempFilePath = - getGameReadWritePath (GameConstants::path_logs_CacheLookupKey) + - tempFilePath; - } - else - { - Config & config = Config::getInstance (); - string userData = config.getString ("UserData_Root", ""); - if (userData != "") - { - endPathWithSlash (userData); - } - tempFilePath = userData + tempFilePath; - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("Temp files path [%s]\n", tempFilePath.c_str ()); - - // Delete the downloaded archive - if (fileExists (tempFilePath + itemName)) - { - removeFile (tempFilePath + itemName); - } - - bool result = upgradeFilesInTemp (); - if (result == false) - { - string binaryName = - Properties::getApplicationPath () + - extractFileFromDirectoryPath (PlatformExceptionHandler:: - application_binary); - string binaryNameOld = - Properties::getApplicationPath () + - extractFileFromDirectoryPath (PlatformExceptionHandler:: - application_binary) + "__REMOVE"; - bool resultRename = renameFile (binaryName, binaryNameOld); - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Rename: [%s] to [%s] result = %d\n",binaryName.c_str(),binaryNameOld.c_str(),resultRename); - printf ("#1 Rename: [%s] to [%s] result = %d errno = %d\n", - binaryName.c_str (), binaryNameOld.c_str (), resultRename, - errno); - - //result = upgradeFilesInTemp(); - binaryName = - Properties::getApplicationPath () + - extractFileFromDirectoryPath (PlatformExceptionHandler:: - application_binary); - binaryNameOld = - tempFilePath + - extractFileFromDirectoryPath (PlatformExceptionHandler:: - application_binary); - resultRename = renameFile (binaryNameOld, binaryName); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Rename: [%s] to [%s] result = %d\n",binaryName.c_str(),binaryNameOld.c_str(),resultRename); - printf ("#2 Rename: [%s] to [%s] result = %d errno = %d\n", - binaryNameOld.c_str (), binaryName.c_str (), resultRename, - errno); - } - - console.addLine ("Successfully updated, please restart!", true); - } - else - { - curl_version_info_data *curlVersion = - curl_version_info (CURLVERSION_NOW); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "FAILED to download the updates: [%s] using CURL version [%s] [%s]", - itemName.c_str (), curlVersion->version, - result.second.c_str ()); - console.addLine (szBuf, true); - showErrorMessageBox (szBuf, "ERROR", false); - } - } - } - - - void MenuStateRoot::mouseMove (int x, int y, const MouseState * ms) - { - popupMenu.mouseMove (x, y); - buttonNewGame.mouseMove (x, y); - buttonLoadGame.mouseMove (x, y); - buttonMods.mouseMove (x, y); - buttonOptions.mouseMove (x, y); - buttonAbout.mouseMove (x, y); - buttonExit.mouseMove (x, y); - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - if (errorMessageBox.getEnabled ()) - { - errorMessageBox.mouseMove (x, y); - } - if (ftpMessageBox.getEnabled ()) - { - ftpMessageBox.mouseMove (x, y); - } - } - - bool MenuStateRoot::isMasterserverMode () const - { - return GlobalStaticFlags::getIsNonGraphicalModeEnabled (); - } - - void MenuStateRoot::render () - { - if (isMasterserverMode () == true) - { - return; - } - Renderer & renderer = Renderer::getInstance (); - CoreData & coreData = CoreData::getInstance (); - const Metrics & metrics = Metrics::getInstance (); - - int w = 400; - int h = 200; - int yPos = 510; - - int logoMainX = (metrics.getVirtualW () - w) / 2; - int logoMainY = yPos - h / 2; - int logoMainW = w; - int logoMainH = h; - logoMainX = - Config::getInstance ().getInt (string (containerName) + "_MainLogo_x", - intToStr (logoMainX).c_str ()); - logoMainY = - Config::getInstance ().getInt (string (containerName) + "_MainLogo_y", - intToStr (logoMainY).c_str ()); - logoMainW = - Config::getInstance ().getInt (string (containerName) + "_MainLogo_w", - intToStr (logoMainW).c_str ()); - logoMainH = - Config::getInstance ().getInt (string (containerName) + "_MainLogo_h", - intToStr (logoMainH).c_str ()); - - renderer.renderTextureQuad (logoMainX, logoMainY, logoMainW, logoMainH, - coreData.getLogoTexture (), - GraphicComponent::getFade ()); - - int maxLogoWidth = 0; - for (int idx = 0; idx < (int) coreData.getLogoTextureExtraCount (); - ++idx) - { - Texture2D *extraLogo = coreData.getLogoTextureExtra (idx); - maxLogoWidth += extraLogo->getPixmap ()->getW (); - } - - int currentX = (metrics.getVirtualW () - maxLogoWidth) / 2; - int currentY = 50; - for (int idx = 0; idx < (int) coreData.getLogoTextureExtraCount (); - ++idx) - { - Texture2D *extraLogo = coreData.getLogoTextureExtra (idx); - - logoMainX = currentX; - logoMainY = currentY; - logoMainW = extraLogo->getPixmap ()->getW (); - logoMainH = extraLogo->getPixmap ()->getH (); - - string logoTagName = - string (containerName) + "_ExtraLogo" + intToStr (idx + 1) + "_"; - logoMainX = - Config::getInstance ().getInt (logoTagName + "x", - intToStr (logoMainX).c_str ()); - logoMainY = - Config::getInstance ().getInt (logoTagName + "y", - intToStr (logoMainY).c_str ()); - logoMainW = - Config::getInstance ().getInt (logoTagName + "w", - intToStr (logoMainW).c_str ()); - logoMainH = - Config::getInstance ().getInt (logoTagName + "h", - intToStr (logoMainH).c_str ()); - - renderer.renderTextureQuad (logoMainX, logoMainY, - logoMainW, logoMainH, - extraLogo, GraphicComponent::getFade ()); - - currentX += extraLogo->getPixmap ()->getW (); - } - - renderer.renderButton (&buttonNewGame); - renderer.renderButton (&buttonLoadGame); - renderer.renderButton (&buttonMods); - renderer.renderButton (&buttonOptions); - renderer.renderButton (&buttonAbout); - renderer.renderButton (&buttonExit); - renderer.renderLabel (&labelVersion); - renderer.renderLabel (&labelGreeting); - - renderer.renderConsole (&console); - - renderer.renderPopupMenu (&popupMenu); - - //exit message box - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - if (errorMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&errorMessageBox); - } - if (ftpMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&ftpMessageBox); - } - - if (program != NULL) - program->renderProgramMsgBox (); - } - - void MenuStateRoot::update () - { - if (Config::getInstance ().getBool ("AutoTest")) - { - if (AutoTest::getInstance ().mustExitGame () == false) - { - AutoTest::getInstance ().updateRoot (program, mainMenu); - } - else - { - program->exit (); - } - return; - } - - if (gameUpdateChecked == false) - { - gameUpdateChecked = true; - - string updateCheckURL = - Config::getInstance ().getString ("UpdateCheckURL", ""); - if (updateCheckURL != "") - { - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - updatesHttpServerThread = new SimpleTaskThread (this, 1, 200); - updatesHttpServerThread->setUniqueID (mutexOwnerId); - updatesHttpServerThread->start (); - } - } - - console.update (); - } - - void MenuStateRoot::simpleTask (BaseThread * callingThread, - void *userdata) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutexThreadOwner (callingThread-> - getMutexThreadOwnerValid (), - mutexOwnerId); - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - return; - } - - callingThread->getMutexThreadOwnerValid ()->setOwnerId (mutexOwnerId); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); - - - string updateCheckURL = - Config::getInstance ().getString ("UpdateCheckURL", ""); - if (updateCheckURL != "") - { - - string baseURL = updateCheckURL; - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", - __FILE__, __FUNCTION__, __LINE__, baseURL.c_str ()); - - CURL *handle = SystemFlags::initHTTP (); - CURLcode curlResult = CURLE_OK; - string updateMetaData = - SystemFlags::getHTTP (baseURL, handle, -1, &curlResult); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("techsMetaData [%s] curlResult = %d\n", - updateMetaData.c_str (), curlResult); - - if (callingThread->getQuitStatus () == true - || safeMutexThreadOwner.isValidMutex () == false) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, - __LINE__); - return; - } - - if (curlResult != CURLE_OK) - { - string curlError = curl_easy_strerror (curlResult); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line %d] curlError [%s]..\n", __FILE__, - __FUNCTION__, __LINE__, curlError.c_str ()); - - char szMsg[8096] = ""; - snprintf (szMsg, 8096, - "An error was detected while checking for new updates\n%s", - curlError.c_str ()); - showErrorMessageBox (szMsg, "ERROR", false); - } - - if (curlResult == CURLE_OK || - (curlResult != CURLE_COULDNT_RESOLVE_HOST && - curlResult != CURLE_COULDNT_CONNECT)) - { - - Properties props; - props.loadFromText (updateMetaData); - - int compareResult = - compareMajorMinorVersion (glestVersionString, - props.getString ("LatestGameVersion", - "")); - if (compareResult == 0) - { - if (glestVersionString != - props.getString ("LatestGameVersion", "")) - { - compareResult = -1; - } - } - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("compareResult = %d local [%s] remote [%s]\n", - compareResult, glestVersionString.c_str (), - props.getString ("LatestGameVersion", "").c_str ()); - - if (compareResult < 0) - { - - string downloadBinaryKey = - "LatestGameBinaryUpdateArchiveURL-" + - getPlatformTypeNameString () + getPlatformArchTypeNameString (); - if (props.hasString (downloadBinaryKey)) - { - ftpFileName = - extractFileFromDirectoryPath (props. - getString (downloadBinaryKey)); - ftpFileURL = props.getString (downloadBinaryKey); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("Checking update key downloadBinaryKey [%s] ftpFileURL [%s]\n", - downloadBinaryKey.c_str (), ftpFileURL.c_str ()); - - if (props.getBool ("AllowUpdateDownloads", "false") == false - || ftpFileURL == "") - { - char szMsg[8096] = ""; - snprintf (szMsg, 8096, - "A new update was detected: %s\nUpdate Date: %s\nPlease visit zetaglest.github.io for details!", - props.getString ("LatestGameVersion", "?").c_str (), - props.getString ("LatestGameVersionReleaseDate", - "?").c_str ()); - showFTPMessageBox (szMsg, "Update", false, true); - } - else - { - char szMsg[8096] = ""; - snprintf (szMsg, 8096, - "A new update was detected: %s\nUpdate Date: %s\nDownload update now?", - props.getString ("LatestGameVersion", "?").c_str (), - props.getString ("LatestGameVersionReleaseDate", - "?").c_str ()); - showFTPMessageBox (szMsg, "Update", false, false); - } - } - } - SystemFlags::cleanupHTTP (&handle); - } - } - - void MenuStateRoot::keyDown (SDL_KeyboardEvent key) - { - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] key = [%c] [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - key.keysym.sym, key.keysym.sym); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] key = [%d - %c]\n", __FILE__, - __FUNCTION__, __LINE__, key.keysym.sym, key.keysym.sym); - - //printf("\n\n\nIN MENU STATE ROOT KEYDOWN!!!\n\n\n"); - - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - //exit - //if(key == configKeys.getCharKey("ExitKey")) { - //printf("Check Root menu exit key!\n"); - if (isKeyPressed (configKeys.getSDLKey ("ExitKey"), key) == true) - { - Lang & lang = Lang::getInstance (); - showMessageBox (lang.getString ("ExitGameQuestion"), "", true); - } - //else if(mainMessageBox.getEnabled() == true && key == vkReturn) { - else if (mainMessageBox.getEnabled () == true - && isKeyPressed (SDLK_RETURN, key) == true) - { - //SDL_keysym keystate = Window::getKeystate(); - SDL_keysym keystate = key.keysym; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] keystate.mod [%d]\n", - __FILE__, __FUNCTION__, __LINE__, - keystate.mod); - - //printf("---> keystate.mod [%d]\n",keystate.mod); - if (keystate.mod & (KMOD_LALT | KMOD_RALT)) - { - } - else - { - //printf("EXITING ---> keystate.mod [%d]\n",keystate.mod); - program->exit (); - } - } - //else if(key == configKeys.getCharKey("SaveGUILayout")) { - else if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == - true) - { - GraphicComponent::saveAllCustomProperties (containerName); - //Lang &lang= Lang::getInstance(); - //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); - } - - } - - void MenuStateRoot::showMessageBox (const string & text, - const string & header, bool toggle) - { - if (toggle == false) - { - mainMessageBox.setEnabled (false); - } - - if (mainMessageBox.getEnabled () == false) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void MenuStateRoot::showErrorMessageBox (const string & text, - const string & header, - bool toggle) - { - if (toggle == false) - { - errorMessageBox.setEnabled (false); - } - - if (errorMessageBox.getEnabled () == false) - { - errorMessageBox.setText (text); - errorMessageBox.setHeader (header); - errorMessageBox.setEnabled (true); - } - else - { - errorMessageBox.setEnabled (false); - } - } - - void MenuStateRoot::showFTPMessageBox (const string & text, - const string & header, bool toggle, - bool okOnly) - { - if (toggle == false) - { - ftpMessageBox.setEnabled (false); - } - - Lang & lang = Lang::getInstance (); - if (okOnly) - { - ftpMessageBox.init (lang.getString ("Ok")); - } - else - { - ftpMessageBox.init (lang.getString ("Yes"), lang.getString ("No")); - } - - if (ftpMessageBox.getEnabled () == false) - { - ftpMessageBox.setText (text); - ftpMessageBox.setHeader (header); - ftpMessageBox.setEnabled (true); - } - else - { - ftpMessageBox.setEnabled (false); - } - } - - - } +namespace Glest { + namespace Game { + + // ===================================================== + // class MenuStateRoot + // ===================================================== + + bool MenuStateRoot::gameUpdateChecked = false; + + MenuStateRoot::MenuStateRoot(Program * program, + MainMenu * mainMenu) :MenuState(program, + mainMenu, + "root"), + updatesHttpServerThread(NULL), buttonNewGame("MainMenu", + "buttonNewGame"), + buttonLoadGame("MainMenu", "buttonLoadGame"), buttonMods("MainMenu", + "buttonMods"), + buttonOptions("MainMenu", "buttonOptions"), buttonAbout("MainMenu", + "buttonAbout"), + buttonExit("MainMenu", "buttonExit"), labelVersion("MainMenu", + "labelVersion"), + labelGreeting("MainMenu", "labelGreeting"), mainMessageBox("MainMenu", + "mainMessageBox"), + errorMessageBox("MainMenu", "errorMessageBox"), + ftpMessageBox("MainMenu", "ftpMessageBox"), popupMenu("MainMenu", + "popupMenu") { + containerName = "MainMenu"; + + ftpClientThread = NULL; + lastDownloadProgress = 0; + + Lang & lang = Lang::getInstance(); + int yPos = 440; + int buttonWidth = 200; + int buttonXPosition = (1000 - buttonWidth) / 2; + + + //labelVersion.registerGraphicComponent(containerName,"labelVersion"); + if (EndsWith(glestVersionString, "-dev") == false) { + labelVersion.init(525, yPos); + labelVersion.setText(glestVersionString); + } else { + labelVersion.init(buttonXPosition, yPos); + //labelVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); + labelVersion.setText(glestVersionString); + } + + labelGreeting.init(labelVersion.getX(), labelVersion.getY() - 16); + labelGreeting.setText(""); + + Steam *steamInstance = + CacheManager::getCachedItem < + Steam * >(GameConstants::steamCacheInstanceKey); + if (steamInstance != NULL) { + string steamPlayerName = steamInstance->userName(); + labelGreeting.setText("Welcome Steam Player: " + steamPlayerName); + } + + yPos -= 55; + //buttonNewGame.registerGraphicComponent(containerName,"buttonNewGame"); + buttonNewGame.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + //buttonLoadGame.registerGraphicComponent(containerName,"buttonLoadGame"); + buttonLoadGame.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + //buttonMods.registerGraphicComponent(containerName,"buttonMods"); + buttonMods.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + //buttonOptions.registerGraphicComponent(containerName,"buttonOptions"); + buttonOptions.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + //buttonAbout.registerGraphicComponent(containerName,"buttonAbout"); + buttonAbout.init(buttonXPosition, yPos, buttonWidth); + yPos -= 40; + //buttonExit.registerGraphicComponent(containerName,"buttonExit"); + buttonExit.init(buttonXPosition, yPos, buttonWidth); + + buttonNewGame.setText(lang.getString("NewGame")); + buttonLoadGame.setText(lang.getString("LoadGame")); + buttonMods.setText(lang.getString("Mods")); + buttonOptions.setText(lang.getString("Options")); + buttonAbout.setText(lang.getString("About")); + buttonExit.setText(lang.getString("Exit")); + + //mesage box + //mainMessageBox.registerGraphicComponent(containerName,"mainMessageBox"); + mainMessageBox.init(lang.getString("Yes"), lang.getString("No")); + mainMessageBox.setEnabled(false); + + //errorMessageBox.registerGraphicComponent(containerName,"errorMessageBox"); + errorMessageBox.init(lang.getString("Ok")); + errorMessageBox.setEnabled(false); + + //ftpMessageBox.registerGraphicComponent(containerName,"ftpMessageBox"); + ftpMessageBox.init(lang.getString("Yes"), lang.getString("No")); + ftpMessageBox.setEnabled(false); + + //PopupMenu popupMenu; + std::vector < string > menuItems; + menuItems.push_back("1"); + menuItems.push_back("2"); + menuItems.push_back("3"); + //popupMenu.registerGraphicComponentOnlyFontCallbacks(containerName, "popupMenu"); + popupMenu.setW(100); + popupMenu.setH(100); + popupMenu.init("Test Menu", menuItems); + popupMenu.setEnabled(false); + popupMenu.setVisible(false); + + GraphicComponent::applyAllCustomProperties(containerName); + } + + MenuStateRoot::~MenuStateRoot() { + if (updatesHttpServerThread != NULL) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + updatesHttpServerThread->setSimpleTaskInterfaceValid(false); + updatesHttpServerThread->signalQuit(); + updatesHttpServerThread->setThreadOwnerValid(false); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (updatesHttpServerThread->canShutdown(true) == true + && updatesHttpServerThread->shutdownAndWait() == true) { + delete updatesHttpServerThread; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + updatesHttpServerThread = NULL; + } + + if (ftpClientThread != NULL) { + ftpClientThread->setCallBackObject(NULL); + ftpClientThread->signalQuit(); + sleep(0); + if (ftpClientThread->canShutdown(true) == true && + ftpClientThread->shutdownAndWait() == true) { + delete ftpClientThread; + } else { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s %d] Error cannot shutdown ftpClientThread\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("%s", szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + //publishToMasterserverThread->cleanup(); + } + ftpClientThread = NULL; + + // ftpClientThread->signalQuit(); + // ftpClientThread->setCallBackObject(NULL); + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + // if( ftpClientThread->shutdownAndWait() == true) { + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + // delete ftpClientThread; + // } + // ftpClientThread = NULL; + // if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + + } + + void MenuStateRoot::reloadUI() { + Lang & lang = Lang::getInstance(); + + console.resetFonts(); + + if (EndsWith(glestVersionString, "-dev") == false) { + labelVersion.setText(glestVersionString); + } else { + //labelVersion.setText(glestVersionString + " [" + getCompileDateTime() + ", " + getGITRevisionString() + "]"); + labelVersion.setText(glestVersionString); + } + + buttonNewGame.setText(lang.getString("NewGame")); + buttonLoadGame.setText(lang.getString("LoadGame")); + buttonMods.setText(lang.getString("Mods")); + buttonOptions.setText(lang.getString("Options")); + buttonAbout.setText(lang.getString("About")); + buttonExit.setText(lang.getString("Exit")); + + mainMessageBox.init(lang.getString("Yes"), lang.getString("No")); + errorMessageBox.init(lang.getString("Ok")); + ftpMessageBox.init(lang.getString("Yes"), lang.getString("No")); + + console.resetFonts(); + + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } + + void MenuStateRoot::mouseClick(int x, int y, MouseButton mouseButton) { + try { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + + if (popupMenu.mouseClick(x, y)) { + //std::pair result = popupMenu.mouseClickedMenuItem(x, y); + popupMenu.mouseClickedMenuItem(x, y); + + //printf("In popup callback menuItemSelected [%s] menuIndexSelected = %d\n",result.second.c_str(),result.first); + } + //exit message box, has to be the last thing to do in this function + else if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + if (button == 0) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + soundRenderer.playFx(coreData.getClickSoundA()); + program->exit(); + } else { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //close message box + mainMessageBox.setEnabled(false); + } + } + } + //exit message box, has to be the last thing to do in this function + else if (errorMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + //close message box + errorMessageBox.setEnabled(false); + } + } + + else if (ftpMessageBox.getEnabled()) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + + int button = 0; + if (ftpMessageBox.mouseClick(x, y, button)) { + ftpMessageBox.setEnabled(false); + if (button == 0) { + startFTPClientIfRequired(); + + lastDownloadProgress = 0; + printf("Adding ftpFileName [%s] ftpFileURL [%s]\n", + ftpFileName.c_str(), ftpFileURL.c_str()); + if (ftpClientThread != NULL) + ftpClientThread->addTempFileToRequests(ftpFileName, + ftpFileURL); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), + mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()-> + setOwnerId(mutexOwnerId); + fileFTPProgressList[ftpFileName] = pair < int, string >(0, ""); + safeMutexFTPProgress.ReleaseLock(); + } + } + } else if (mainMessageBox.getEnabled() == false + && buttonNewGame.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + } else if (mainMessageBox.getEnabled() == false + && buttonLoadGame.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateLoadGame(program, mainMenu)); + } else if (mainMessageBox.getEnabled() == false + && buttonMods.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateMods(program, mainMenu)); + } else if (mainMessageBox.getEnabled() == false + && buttonOptions.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateOptions(program, mainMenu)); + } else if (mainMessageBox.getEnabled() == false + && buttonAbout.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundB()); + mainMenu->setState(new MenuStateAbout(program, mainMenu)); + } else if (buttonExit.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + program->exit(); + } + } catch (exception & e) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d]\nError in menu event:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + showErrorMessageBox(szBuf, "", true); + } + } + + void MenuStateRoot::startFTPClientIfRequired() { + if (ftpClientThread == NULL) { + // Setup File Transfer thread + Config & config = Config::getInstance(); + + vector < string > tilesetFiles; + vector < string > tilesetFilesUserData; + + vector < string > techTreeFiles; + vector < string > techTreeFilesUserData; + + + findDirs(config.getPathListForType(ptTilesets), tilesetFiles); + findDirs(config.getPathListForType(ptTechs), techTreeFiles); + + vector < string > mapPathList = config.getPathListForType(ptMaps); + std::pair < string, string > mapsPath; + if (mapPathList.empty() == false) { + mapsPath.first = mapPathList[0]; + } + if (mapPathList.size() > 1) { + mapsPath.second = mapPathList[1]; + } + std::pair < string, string > tilesetsPath; + vector < string > tilesetsList = + Config::getInstance().getPathListForType(ptTilesets); + if (tilesetsList.empty() == false) { + tilesetsPath.first = tilesetsList[0]; + if (tilesetsList.size() > 1) { + tilesetsPath.second = tilesetsList[1]; + } + } + + std::pair < string, string > techtreesPath; + vector < string > techtreesList = + Config::getInstance().getPathListForType(ptTechs); + if (techtreesList.empty() == false) { + techtreesPath.first = techtreesList[0]; + if (techtreesList.size() > 1) { + techtreesPath.second = techtreesList[1]; + } + } + + std::pair < string, string > scenariosPath; + vector < string > scenariosList = + Config::getInstance().getPathListForType(ptScenarios); + if (scenariosList.empty() == false) { + scenariosPath.first = scenariosList[0]; + if (scenariosList.size() > 1) { + scenariosPath.second = scenariosList[1]; + } + } + + string fileArchiveExtension = + config.getString("FileArchiveExtension", ""); + string fileArchiveExtractCommand = + config.getString("FileArchiveExtractCommand", ""); + string fileArchiveExtractCommandParameters = + config.getString("FileArchiveExtractCommandParameters", ""); + int32 fileArchiveExtractCommandSuccessResult = + config.getInt("FileArchiveExtractCommandSuccessResult", "0"); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + console.setOnlyChatMessagesInStoredLines(false); + + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != + "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); + + ftpClientThread = new FTPClientThread(-1, "", + mapsPath, tilesetsPath, + techtreesPath, scenariosPath, + this, fileArchiveExtension, + fileArchiveExtractCommand, + fileArchiveExtractCommandParameters, + fileArchiveExtractCommandSuccessResult, + tempFilePath); + ftpClientThread->start(); + + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + } + + void MenuStateRoot::FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType type, + pair < FTP_Client_ResultType, + string > result, + void *userdata) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + Lang & lang = Lang::getInstance(); + if (type == ftp_cct_DownloadProgress) { + FTPClientCallbackInterface::FtpProgressStats * stats = + (FTPClientCallbackInterface::FtpProgressStats *) userdata; + if (stats != NULL) { + int fileProgress = 0; + if (stats->download_total > 0) { + fileProgress = + ((stats->download_now / stats->download_total) * 100.0); + } + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Got FTP Callback for [%s] current file [%s] fileProgress = %d [now = %f, total = %f]\n",itemName.c_str(),stats->currentFilename.c_str(), fileProgress,stats->download_now,stats->download_total); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + pair < int, string > lastProgress = fileFTPProgressList[itemName]; + fileFTPProgressList[itemName] = + pair < int, string >(fileProgress, stats->currentFilename); + safeMutexFTPProgress.ReleaseLock(); + + if (itemName != "" + && (lastDownloadProgress < fileProgress + && fileProgress % 25 == 0)) { + lastDownloadProgress = fileProgress; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Downloaded %d%% of file: %s", + fileProgress, itemName.c_str()); + console.addLine(szBuf); + } + } + } else if (type == ftp_cct_ExtractProgress) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP extract Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + printf("Got FTP extract Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + if (userdata == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + lang.getString("DataMissingExtractDownloadMod").c_str(), + itemName.c_str()); + //printf("%s\n",szBuf); + console.addLine(szBuf, true); + } else { + char *szBuf = (char *) userdata; + //printf("%s\n",szBuf); + console.addLine(szBuf); + } + } else if (type == ftp_cct_TempFile) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Got FTP Callback for [%s] result = %d [%s]\n", + itemName.c_str(), result.first, result.second.c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper + safeMutexFTPProgress((ftpClientThread != + NULL ? ftpClientThread-> + getProgressMutex() : NULL), mutexOwnerId); + if (ftpClientThread != NULL + && ftpClientThread->getProgressMutex() != NULL) + ftpClientThread->getProgressMutex()->setOwnerId(mutexOwnerId); + fileFTPProgressList.erase(itemName); + safeMutexFTPProgress.ReleaseLock(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("### downloaded TEMP file [%s] result = %d\n", + itemName.c_str(), result.first); + + if (result.first == ftp_crt_SUCCESS) { + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + != "") { + tempFilePath = + getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + + tempFilePath; + } else { + Config & config = Config::getInstance(); + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("Temp files path [%s]\n", tempFilePath.c_str()); + + // Delete the downloaded archive + if (fileExists(tempFilePath + itemName)) { + removeFile(tempFilePath + itemName); + } + + bool result = upgradeFilesInTemp(); + if (result == false) { + string binaryName = + Properties::getApplicationPath() + + extractFileFromDirectoryPath(PlatformExceptionHandler:: + application_binary); + string binaryNameOld = + Properties::getApplicationPath() + + extractFileFromDirectoryPath(PlatformExceptionHandler:: + application_binary) + "__REMOVE"; + bool resultRename = renameFile(binaryName, binaryNameOld); + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Rename: [%s] to [%s] result = %d\n",binaryName.c_str(),binaryNameOld.c_str(),resultRename); + printf("#1 Rename: [%s] to [%s] result = %d errno = %d\n", + binaryName.c_str(), binaryNameOld.c_str(), resultRename, + errno); + + //result = upgradeFilesInTemp(); + binaryName = + Properties::getApplicationPath() + + extractFileFromDirectoryPath(PlatformExceptionHandler:: + application_binary); + binaryNameOld = + tempFilePath + + extractFileFromDirectoryPath(PlatformExceptionHandler:: + application_binary); + resultRename = renameFile(binaryNameOld, binaryName); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Rename: [%s] to [%s] result = %d\n",binaryName.c_str(),binaryNameOld.c_str(),resultRename); + printf("#2 Rename: [%s] to [%s] result = %d errno = %d\n", + binaryNameOld.c_str(), binaryName.c_str(), resultRename, + errno); + } + + console.addLine("Successfully updated, please restart!", true); + } else { + curl_version_info_data *curlVersion = + curl_version_info(CURLVERSION_NOW); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "FAILED to download the updates: [%s] using CURL version [%s] [%s]", + itemName.c_str(), curlVersion->version, + result.second.c_str()); + console.addLine(szBuf, true); + showErrorMessageBox(szBuf, "ERROR", false); + } + } + } + + + void MenuStateRoot::mouseMove(int x, int y, const MouseState * ms) { + popupMenu.mouseMove(x, y); + buttonNewGame.mouseMove(x, y); + buttonLoadGame.mouseMove(x, y); + buttonMods.mouseMove(x, y); + buttonOptions.mouseMove(x, y); + buttonAbout.mouseMove(x, y); + buttonExit.mouseMove(x, y); + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + if (errorMessageBox.getEnabled()) { + errorMessageBox.mouseMove(x, y); + } + if (ftpMessageBox.getEnabled()) { + ftpMessageBox.mouseMove(x, y); + } + } + + bool MenuStateRoot::isMasterserverMode() const { + return GlobalStaticFlags::getIsNonGraphicalModeEnabled(); + } + + void MenuStateRoot::render() { + if (isMasterserverMode() == true) { + return; + } + Renderer & renderer = Renderer::getInstance(); + CoreData & coreData = CoreData::getInstance(); + const Metrics & metrics = Metrics::getInstance(); + + int w = 400; + int h = 200; + int yPos = 510; + + int logoMainX = (metrics.getVirtualW() - w) / 2; + int logoMainY = yPos - h / 2; + int logoMainW = w; + int logoMainH = h; + logoMainX = + Config::getInstance().getInt(string(containerName) + "_MainLogo_x", + intToStr(logoMainX).c_str()); + logoMainY = + Config::getInstance().getInt(string(containerName) + "_MainLogo_y", + intToStr(logoMainY).c_str()); + logoMainW = + Config::getInstance().getInt(string(containerName) + "_MainLogo_w", + intToStr(logoMainW).c_str()); + logoMainH = + Config::getInstance().getInt(string(containerName) + "_MainLogo_h", + intToStr(logoMainH).c_str()); + + renderer.renderTextureQuad(logoMainX, logoMainY, logoMainW, logoMainH, + coreData.getLogoTexture(), + GraphicComponent::getFade()); + + int maxLogoWidth = 0; + for (int idx = 0; idx < (int) coreData.getLogoTextureExtraCount(); + ++idx) { + Texture2D *extraLogo = coreData.getLogoTextureExtra(idx); + maxLogoWidth += extraLogo->getPixmap()->getW(); + } + + int currentX = (metrics.getVirtualW() - maxLogoWidth) / 2; + int currentY = 50; + for (int idx = 0; idx < (int) coreData.getLogoTextureExtraCount(); + ++idx) { + Texture2D *extraLogo = coreData.getLogoTextureExtra(idx); + + logoMainX = currentX; + logoMainY = currentY; + logoMainW = extraLogo->getPixmap()->getW(); + logoMainH = extraLogo->getPixmap()->getH(); + + string logoTagName = + string(containerName) + "_ExtraLogo" + intToStr(idx + 1) + "_"; + logoMainX = + Config::getInstance().getInt(logoTagName + "x", + intToStr(logoMainX).c_str()); + logoMainY = + Config::getInstance().getInt(logoTagName + "y", + intToStr(logoMainY).c_str()); + logoMainW = + Config::getInstance().getInt(logoTagName + "w", + intToStr(logoMainW).c_str()); + logoMainH = + Config::getInstance().getInt(logoTagName + "h", + intToStr(logoMainH).c_str()); + + renderer.renderTextureQuad(logoMainX, logoMainY, + logoMainW, logoMainH, + extraLogo, GraphicComponent::getFade()); + + currentX += extraLogo->getPixmap()->getW(); + } + + renderer.renderButton(&buttonNewGame); + renderer.renderButton(&buttonLoadGame); + renderer.renderButton(&buttonMods); + renderer.renderButton(&buttonOptions); + renderer.renderButton(&buttonAbout); + renderer.renderButton(&buttonExit); + renderer.renderLabel(&labelVersion); + renderer.renderLabel(&labelGreeting); + + renderer.renderConsole(&console); + + renderer.renderPopupMenu(&popupMenu); + + //exit message box + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } + if (errorMessageBox.getEnabled()) { + renderer.renderMessageBox(&errorMessageBox); + } + if (ftpMessageBox.getEnabled()) { + renderer.renderMessageBox(&ftpMessageBox); + } + + if (program != NULL) + program->renderProgramMsgBox(); + } + + void MenuStateRoot::update() { + if (Config::getInstance().getBool("AutoTest")) { + if (AutoTest::getInstance().mustExitGame() == false) { + AutoTest::getInstance().updateRoot(program, mainMenu); + } else { + program->exit(); + } + return; + } + + if (gameUpdateChecked == false) { + gameUpdateChecked = true; + + string updateCheckURL = + Config::getInstance().getString("UpdateCheckURL", ""); + if (updateCheckURL != "") { + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + updatesHttpServerThread = new SimpleTaskThread(this, 1, 200); + updatesHttpServerThread->setUniqueID(mutexOwnerId); + updatesHttpServerThread->start(); + } + } + + console.update(); + } + + void MenuStateRoot::simpleTask(BaseThread * callingThread, + void *userdata) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutexThreadOwner(callingThread-> + getMutexThreadOwnerValid(), + mutexOwnerId); + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + return; + } + + callingThread->getMutexThreadOwnerValid()->setOwnerId(mutexOwnerId); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, __LINE__); + + + string updateCheckURL = + Config::getInstance().getString("UpdateCheckURL", ""); + if (updateCheckURL != "") { + + string baseURL = updateCheckURL; + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line %d] About to call first http url, base [%s]..\n", + __FILE__, __FUNCTION__, __LINE__, baseURL.c_str()); + + CURL *handle = SystemFlags::initHTTP(); + CURLcode curlResult = CURLE_OK; + string updateMetaData = + SystemFlags::getHTTP(baseURL, handle, -1, &curlResult); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("techsMetaData [%s] curlResult = %d\n", + updateMetaData.c_str(), curlResult); + + if (callingThread->getQuitStatus() == true + || safeMutexThreadOwner.isValidMutex() == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d]\n", __FILE__, __FUNCTION__, + __LINE__); + return; + } + + if (curlResult != CURLE_OK) { + string curlError = curl_easy_strerror(curlResult); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line %d] curlError [%s]..\n", __FILE__, + __FUNCTION__, __LINE__, curlError.c_str()); + + char szMsg[8096] = ""; + snprintf(szMsg, 8096, + "An error was detected while checking for new updates\n%s", + curlError.c_str()); + showErrorMessageBox(szMsg, "ERROR", false); + } + + if (curlResult == CURLE_OK || + (curlResult != CURLE_COULDNT_RESOLVE_HOST && + curlResult != CURLE_COULDNT_CONNECT)) { + + Properties props; + props.loadFromText(updateMetaData); + + int compareResult = + compareMajorMinorVersion(glestVersionString, + props.getString("LatestGameVersion", + "")); + if (compareResult == 0) { + if (glestVersionString != + props.getString("LatestGameVersion", "")) { + compareResult = -1; + } + } + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("compareResult = %d local [%s] remote [%s]\n", + compareResult, glestVersionString.c_str(), + props.getString("LatestGameVersion", "").c_str()); + + if (compareResult < 0) { + + string downloadBinaryKey = + "LatestGameBinaryUpdateArchiveURL-" + + getPlatformTypeNameString() + getPlatformArchTypeNameString(); + if (props.hasString(downloadBinaryKey)) { + ftpFileName = + extractFileFromDirectoryPath(props. + getString(downloadBinaryKey)); + ftpFileURL = props.getString(downloadBinaryKey); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("Checking update key downloadBinaryKey [%s] ftpFileURL [%s]\n", + downloadBinaryKey.c_str(), ftpFileURL.c_str()); + + if (props.getBool("AllowUpdateDownloads", "false") == false + || ftpFileURL == "") { + char szMsg[8096] = ""; + snprintf(szMsg, 8096, + "A new update was detected: %s\nUpdate Date: %s\nPlease visit zetaglest.github.io for details!", + props.getString("LatestGameVersion", "?").c_str(), + props.getString("LatestGameVersionReleaseDate", + "?").c_str()); + showFTPMessageBox(szMsg, "Update", false, true); + } else { + char szMsg[8096] = ""; + snprintf(szMsg, 8096, + "A new update was detected: %s\nUpdate Date: %s\nDownload update now?", + props.getString("LatestGameVersion", "?").c_str(), + props.getString("LatestGameVersionReleaseDate", + "?").c_str()); + showFTPMessageBox(szMsg, "Update", false, false); + } + } + } + SystemFlags::cleanupHTTP(&handle); + } + } + + void MenuStateRoot::keyDown(SDL_KeyboardEvent key) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] key = [%c] [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + key.keysym.sym, key.keysym.sym); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] key = [%d - %c]\n", __FILE__, + __FUNCTION__, __LINE__, key.keysym.sym, key.keysym.sym); + + //printf("\n\n\nIN MENU STATE ROOT KEYDOWN!!!\n\n\n"); + + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + //exit + //if(key == configKeys.getCharKey("ExitKey")) { + //printf("Check Root menu exit key!\n"); + if (isKeyPressed(configKeys.getSDLKey("ExitKey"), key) == true) { + Lang & lang = Lang::getInstance(); + showMessageBox(lang.getString("ExitGameQuestion"), "", true); + } + //else if(mainMessageBox.getEnabled() == true && key == vkReturn) { + else if (mainMessageBox.getEnabled() == true + && isKeyPressed(SDLK_RETURN, key) == true) { + //SDL_keysym keystate = Window::getKeystate(); + SDL_keysym keystate = key.keysym; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] keystate.mod [%d]\n", + __FILE__, __FUNCTION__, __LINE__, + keystate.mod); + + //printf("---> keystate.mod [%d]\n",keystate.mod); + if (keystate.mod & (KMOD_LALT | KMOD_RALT)) { + } else { + //printf("EXITING ---> keystate.mod [%d]\n",keystate.mod); + program->exit(); + } + } + //else if(key == configKeys.getCharKey("SaveGUILayout")) { + else if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == + true) { + GraphicComponent::saveAllCustomProperties(containerName); + //Lang &lang= Lang::getInstance(); + //console.addLine(lang.getString("GUILayoutSaved") + " [" + (saved ? lang.getString("Yes") : lang.getString("No"))+ "]"); + } + + } + + void MenuStateRoot::showMessageBox(const string & text, + const string & header, bool toggle) { + if (toggle == false) { + mainMessageBox.setEnabled(false); + } + + if (mainMessageBox.getEnabled() == false) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void MenuStateRoot::showErrorMessageBox(const string & text, + const string & header, + bool toggle) { + if (toggle == false) { + errorMessageBox.setEnabled(false); + } + + if (errorMessageBox.getEnabled() == false) { + errorMessageBox.setText(text); + errorMessageBox.setHeader(header); + errorMessageBox.setEnabled(true); + } else { + errorMessageBox.setEnabled(false); + } + } + + void MenuStateRoot::showFTPMessageBox(const string & text, + const string & header, bool toggle, + bool okOnly) { + if (toggle == false) { + ftpMessageBox.setEnabled(false); + } + + Lang & lang = Lang::getInstance(); + if (okOnly) { + ftpMessageBox.init(lang.getString("Ok")); + } else { + ftpMessageBox.init(lang.getString("Yes"), lang.getString("No")); + } + + if (ftpMessageBox.getEnabled() == false) { + ftpMessageBox.setText(text); + ftpMessageBox.setHeader(header); + ftpMessageBox.setEnabled(true); + } else { + ftpMessageBox.setEnabled(false); + } + } + + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_root.h b/source/glest_game/menu/menu_state_root.h index 4d810dcce..82576c72d 100644 --- a/source/glest_game/menu/menu_state_root.h +++ b/source/glest_game/menu/menu_state_root.h @@ -18,74 +18,72 @@ # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateRoot -// =============================== + // =============================== + // class MenuStateRoot + // =============================== - class GraphicMessageBox; - class PopupMenu; + class GraphicMessageBox; + class PopupMenu; - class MenuStateRoot:public MenuState, public SimpleTaskCallbackInterface, - public FTPClientCallbackInterface - { - private: - GraphicButton buttonNewGame; - GraphicButton buttonLoadGame; - GraphicButton buttonMods; - GraphicButton buttonOptions; - GraphicButton buttonAbout; - GraphicButton buttonExit; - GraphicLabel labelVersion; - GraphicLabel labelGreeting; + class MenuStateRoot :public MenuState, public SimpleTaskCallbackInterface, + public FTPClientCallbackInterface { + private: + GraphicButton buttonNewGame; + GraphicButton buttonLoadGame; + GraphicButton buttonMods; + GraphicButton buttonOptions; + GraphicButton buttonAbout; + GraphicButton buttonExit; + GraphicLabel labelVersion; + GraphicLabel labelGreeting; - GraphicMessageBox mainMessageBox; - GraphicMessageBox errorMessageBox; - GraphicMessageBox ftpMessageBox; + GraphicMessageBox mainMessageBox; + GraphicMessageBox errorMessageBox; + GraphicMessageBox ftpMessageBox; - PopupMenu popupMenu; + PopupMenu popupMenu; - static bool gameUpdateChecked; - SimpleTaskThread *updatesHttpServerThread; - FTPClientThread *ftpClientThread; - std::map < string, pair < int, string > >fileFTPProgressList; - string ftpFileName; - string ftpFileURL; - int lastDownloadProgress; + static bool gameUpdateChecked; + SimpleTaskThread *updatesHttpServerThread; + FTPClientThread *ftpClientThread; + std::map < string, pair < int, string > >fileFTPProgressList; + string ftpFileName; + string ftpFileURL; + int lastDownloadProgress; - virtual void simpleTask (BaseThread * callingThread, void *userdata); - void startFTPClientIfRequired (); - virtual void FTPClient_CallbackEvent (string itemName, - FTP_Client_CallbackType type, - pair < FTP_Client_ResultType, - string > result, void *userdata); + virtual void simpleTask(BaseThread * callingThread, void *userdata); + void startFTPClientIfRequired(); + virtual void FTPClient_CallbackEvent(string itemName, + FTP_Client_CallbackType type, + pair < FTP_Client_ResultType, + string > result, void *userdata); - public: - MenuStateRoot (Program * program, MainMenu * mainMenu); - virtual ~ MenuStateRoot (); + public: + MenuStateRoot(Program * program, MainMenu * mainMenu); + virtual ~MenuStateRoot(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - void update (); - virtual void keyDown (SDL_KeyboardEvent key); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + void update(); + virtual void keyDown(SDL_KeyboardEvent key); - void showMessageBox (const string & text, const string & header, - bool toggle); - void showErrorMessageBox (const string & text, const string & header, - bool toggle); - void showFTPMessageBox (const string & text, const string & header, - bool toggle, bool okOnly); + void showMessageBox(const string & text, const string & header, + bool toggle); + void showErrorMessageBox(const string & text, const string & header, + bool toggle); + void showFTPMessageBox(const string & text, const string & header, + bool toggle, bool okOnly); - virtual bool isMasterserverMode () const; - virtual void reloadUI (); - }; + virtual bool isMasterserverMode() const; + virtual void reloadUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/menu_state_scenario.cpp b/source/glest_game/menu/menu_state_scenario.cpp index 9b0589818..9e7aa3c77 100644 --- a/source/glest_game/menu/menu_state_scenario.cpp +++ b/source/glest_game/menu/menu_state_scenario.cpp @@ -23,688 +23,596 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - using namespace::Shared::Xml; - -// ===================================================== -// class MenuStateScenario -// ===================================================== - - MenuStateScenario::MenuStateScenario (Program * program, - MainMenu * mainMenu, - bool isTutorialMode, - const vector < string > &dirList, - string - autoloadScenarioName):MenuState - (program, mainMenu, "scenario") - { - containerName = "Scenario"; - this->isTutorialMode = isTutorialMode; - - enableScenarioTexturePreview = - Config::getInstance ().getBool ("EnableScenarioTexturePreview", - "true"); - scenarioLogoTexture = NULL; - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - - Lang & lang = Lang::getInstance (); - NetworkManager & networkManager = NetworkManager::getInstance (); - try - { - networkManager.init (nrServer); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - mainMessageBoxState = 1; - showMessageBox ("Error: " + string (ex.what ()), "Error detected", - false); - } - - mainMessageBox.registerGraphicComponent (containerName, - "mainMessageBox"); - mainMessageBox.init (lang.getString ("Ok")); - mainMessageBox.setEnabled (false); - mainMessageBoxState = 0; - - this->autoloadScenarioName = autoloadScenarioName; - vector < string > results; - - this->dirList = dirList; - - int buttonStartY = 50; - int buttonStartX = 70; - - buttonReturn.registerGraphicComponent (containerName, "buttonReturn"); - buttonReturn.init (buttonStartX, buttonStartY, 125); - buttonReturn.setText (lang.getString ("Return")); - - buttonPlayNow.registerGraphicComponent (containerName, "buttonPlayNow"); - buttonPlayNow.init (buttonStartX + 150, buttonStartY, 125); - buttonPlayNow.setText (lang.getString ("PlayNow")); - - int startY = 700; - int startX = 50; - - labelScenario.registerGraphicComponent (containerName, "labelScenario"); - labelScenario.init (startX, startY); - - listBoxScenario.registerGraphicComponent (containerName, - "listBoxScenario"); - listBoxScenario.init (startX, startY - 30, 290); - - labelScenarioName.registerGraphicComponent (containerName, - "labelScenarioName"); - labelScenarioName.init (startX, startY - 80); - labelScenarioName.setFont (CoreData::getInstance ().getMenuFontBig ()); - labelScenarioName.setFont3D (CoreData::getInstance (). - getMenuFontBig3D ()); - - labelInfo.registerGraphicComponent (containerName, "labelInfo"); - labelInfo.init (startX, startY - 110); - labelInfo.setFont (CoreData::getInstance ().getMenuFontNormal ()); - labelInfo.setFont3D (CoreData::getInstance ().getMenuFontNormal3D ()); - - if (this->isTutorialMode == true) - { - labelScenario.setText (lang.getString ("Tutorial")); - } - else - { - labelScenario.setText (lang.getString ("Scenario")); - } - - //scenario listbox - findDirs (dirList, results); - scenarioFiles = results; - //printf("scenarioFiles[0] [%s]\n",scenarioFiles[0].c_str()); - - if (results.empty () == true) - { - //throw megaglest_runtime_error("There are no scenarios found to load"); - mainMessageBoxState = 1; - if (this->isTutorialMode == true) - { - showMessageBox ("Error: There are no tutorials found to load", - "Error detected", false); - } - else - { - showMessageBox ("Error: There are no scenarios found to load", - "Error detected", false); - } - } - - std::map < string, string > scenarioErrors; - for (int i = 0; i < (int) results.size (); ++i) - { - results[i] = formatString (results[i]); - } - listBoxScenario.setItems (results); - - try - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] listBoxScenario.getSelectedItemIndex() = %d scenarioFiles.size() = %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, listBoxScenario.getSelectedItemIndex (), - (int) scenarioFiles.size ()); - - if (listBoxScenario.getItemCount () > 0 - && listBoxScenario.getSelectedItemIndex () >= 0 - && listBoxScenario.getSelectedItemIndex () < - (int) scenarioFiles.size ()) - { - string scenarioPath = - Scenario::getScenarioPath (dirList, - scenarioFiles[listBoxScenario. - getSelectedItemIndex - ()]); - //printf("scenarioPath [%s]\n",scenarioPath.c_str()); - - loadScenarioInfo (scenarioPath, &scenarioInfo); - labelInfo.setText (scenarioInfo.desc); - if (scenarioInfo.namei18n != "") - { - labelScenarioName.setText (scenarioInfo.namei18n); - } - else - { - labelScenarioName.setText (listBoxScenario.getSelectedItem ()); - } - } - - GraphicComponent::applyAllCustomProperties (containerName); - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - mainMessageBoxState = 1; - showMessageBox ("Error: " + string (ex.what ()), "Error detected", - false); - } - - if (scenarioErrors.empty () == false) - { - mainMessageBoxState = 1; - - string errorMsg = ""; - for (std::map < string, string >::iterator iterMap = - scenarioErrors.begin (); iterMap != scenarioErrors.end (); - ++iterMap) - { - errorMsg += - "scenario: " + iterMap->first + " error text: " + - iterMap->second.substr (0, 400) + "\n"; - } - showMessageBox ("Error loading scenario(s): " + errorMsg, - "Error detected", false); - } - } - - void MenuStateScenario::reloadUI () - { - Lang & lang = Lang::getInstance (); - - console.resetFonts (); - mainMessageBox.init (lang.getString ("Ok")); - labelInfo.setFont (CoreData::getInstance ().getMenuFontNormal ()); - labelInfo.setFont3D (CoreData::getInstance ().getMenuFontNormal3D ()); - - labelScenarioName.setFont (CoreData::getInstance (). - getMenuFontNormal ()); - labelScenarioName.setFont3D (CoreData::getInstance (). - getMenuFontNormal3D ()); - - buttonReturn.setText (lang.getString ("Return")); - buttonPlayNow.setText (lang.getString ("PlayNow")); - - labelScenario.setText (lang.getString ("Scenario")); - - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } - - MenuStateScenario::~MenuStateScenario () - { - cleanupPreviewTexture (); - } - - void MenuStateScenario::cleanupPreviewTexture () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenarioLogoTexture); - - if (scenarioLogoTexture != NULL) - { - Renderer::getInstance ().endTexture (rsGlobal, scenarioLogoTexture, - false); - } - scenarioLogoTexture = NULL; - } - - void MenuStateScenario::mouseClick (int x, int y, MouseButton mouseButton) - { - CoreData & coreData = CoreData::getInstance (); - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - string advanceToItemStartingWith = ""; - - if (mainMessageBox.getEnabled ()) - { - int button = 0; - if (mainMessageBox.mouseClick (x, y, button)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - if (button == 0) - { - mainMessageBox.setEnabled (false); - - if (scenarioFiles.empty () == true && mainMessageBoxState == 1) - { - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - return; - } - } - } - return; - } - else - { - if (::Shared::Platform::Window::isKeyStateModPressed (KMOD_SHIFT) == - true) - { - const wchar_t lastKey =::Shared::Platform::Window:: - extractLastKeyPressed (); -// xxx: -// string hehe=lastKey; -// printf("lastKey = %d [%c] '%s'\n",lastKey,lastKey,hehe); - advanceToItemStartingWith = lastKey; - } - } - - if (buttonReturn.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundA ()); - mainMenu->setState (new MenuStateNewGame (program, mainMenu)); - return; - } - else if (buttonPlayNow.mouseClick (x, y)) - { - soundRenderer.playFx (coreData.getClickSoundC ()); - launchGame (); - return; - } - else if (listBoxScenario.mouseClick (x, y, advanceToItemStartingWith)) - { - try - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] listBoxScenario.getSelectedItemIndex() = %d scenarioFiles.size() = %d\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, listBoxScenario.getSelectedItemIndex (), - (int) scenarioFiles.size ()); - - if (listBoxScenario.getItemCount () > 0 - && listBoxScenario.getSelectedItemIndex () >= 0 - && listBoxScenario.getSelectedItemIndex () < - (int) scenarioFiles.size ()) - { - loadScenarioInfo (Scenario:: - getScenarioPath (dirList, - scenarioFiles[listBoxScenario. - getSelectedItemIndex - ()]), - &scenarioInfo); - labelInfo.setText (scenarioInfo.desc); - if (scenarioInfo.namei18n != "") - { - labelScenarioName.setText (scenarioInfo.namei18n); - } - else - { - labelScenarioName.setText (listBoxScenario.getSelectedItem ()); - } - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", szBuf); - - mainMessageBoxState = 1; - showMessageBox ("Error: " + string (ex.what ()), "Error detected", - false); - } - } - } - - void MenuStateScenario::mouseMove (int x, int y, const MouseState * ms) - { - - if (mainMessageBox.getEnabled ()) - { - mainMessageBox.mouseMove (x, y); - } - - listBoxScenario.mouseMove (x, y); - - buttonReturn.mouseMove (x, y); - buttonPlayNow.mouseMove (x, y); - } - - void MenuStateScenario::render () - { - - Renderer & renderer = Renderer::getInstance (); - - if (scenarioLogoTexture != NULL) - { - renderer.renderTextureQuad (450, 200, 533, 400, scenarioLogoTexture, - 1.0f); - //renderer.renderBackground(scenarioLogoTexture); - } - - if (mainMessageBox.getEnabled ()) - { - renderer.renderMessageBox (&mainMessageBox); - } - else - { - renderer.renderLabel (&labelInfo); - renderer.renderLabel (&labelScenarioName); - - renderer.renderLabel (&labelScenario); - renderer.renderListBox (&listBoxScenario); - - renderer.renderButton (&buttonReturn); - renderer.renderButton (&buttonPlayNow); - } - renderer.renderConsole (&console); - if (program != NULL) - program->renderProgramMsgBox (); - } - - void MenuStateScenario::update () - { - if (Config::getInstance ().getBool ("AutoTest")) - { - AutoTest::getInstance ().updateScenario (this); - return; - } - if (this->autoloadScenarioName != "") - { - string scenarioPath = - Scenario::getScenarioPath (dirList, this->autoloadScenarioName); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->autoloadScenarioName.c_str(),scenarioPath.c_str()); - printf - ("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, this->autoloadScenarioName.c_str (), - scenarioPath.c_str ()); - - loadScenarioInfo (scenarioPath, &scenarioInfo); - //if(scenarioInfo.namei18n != "") { - // this->autoloadScenarioName = scenarioInfo.namei18n; - //} - //else { - this->autoloadScenarioName = - formatString (this->autoloadScenarioName); - //} - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->autoloadScenarioName.c_str(),scenarioPath.c_str()); - printf - ("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s] file [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), __FUNCTION__, - __LINE__, this->autoloadScenarioName.c_str (), - scenarioPath.c_str (), scenarioInfo.file.c_str ()); - - listBoxScenario.setSelectedItem (this->autoloadScenarioName, false); - - if (listBoxScenario.getSelectedItem () != this->autoloadScenarioName) - { - mainMessageBoxState = 1; - showMessageBox ("Could not find scenario name: " + - this->autoloadScenarioName, "Scenario Missing", - false); - this->autoloadScenarioName = ""; - } - else - { - try - { - this->autoloadScenarioName = ""; - if (listBoxScenario.getItemCount () > 0 - && listBoxScenario.getSelectedItemIndex () >= 0 - && listBoxScenario.getSelectedItemIndex () < - (int) scenarioFiles.size ()) - { - - printf - ("[%s:%s] Line: %d scenarioFiles[listBoxScenario.getSelectedItemIndex()] [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, - scenarioFiles[listBoxScenario.getSelectedItemIndex ()]. - c_str ()); - - loadScenarioInfo (Scenario:: - getScenarioPath (dirList, - scenarioFiles - [listBoxScenario. - getSelectedItemIndex ()]), - &scenarioInfo); - - printf ("[%s:%s] Line: %d scenarioInfo.file [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenarioInfo.file.c_str ()); - - labelInfo.setText (scenarioInfo.desc); - if (scenarioInfo.namei18n != "") - { - labelScenarioName.setText (scenarioInfo.namei18n); - } - else - { - labelScenarioName.setText (listBoxScenario. - getSelectedItem ()); - } - - SoundRenderer & soundRenderer = SoundRenderer::getInstance (); - CoreData & coreData = CoreData::getInstance (); - soundRenderer.playFx (coreData.getClickSoundC ()); - launchGame (); - return; - } - } - catch (const std::exception & ex) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, ex.what ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, "%s", - szBuf); - - mainMessageBoxState = 1; - showMessageBox ("Error: " + string (ex.what ()), "Error detected", - false); - } - } - } - - if (needToLoadTextures) - { - // this delay is done to make it possible to switch faster - if (difftime (time (NULL), previewLoadDelayTimer) >= 2) - { - loadScenarioPreviewTexture (); - needToLoadTextures = false; - } - } - console.update (); - } - - void MenuStateScenario::launchGame () - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] scenarioInfo.file [%s] [%s][%s][%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenarioInfo.file.c_str (), - scenarioInfo.tilesetName.c_str (), - scenarioInfo.mapName.c_str (), - scenarioInfo.techTreeName.c_str ()); - - if (scenarioInfo.file != "" && scenarioInfo.tilesetName != "" - && scenarioInfo.mapName != "" && scenarioInfo.techTreeName != "") - { - GameSettings gameSettings; - loadGameSettings (&scenarioInfo, &gameSettings); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] gameSettings.getScenarioDir() [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, - gameSettings.getScenarioDir ().c_str ()); - - const vector < string > pathTechList = - Config::getInstance ().getPathListForType (ptTechs, - gameSettings. - getScenarioDir ()); - if (TechTree::exists (gameSettings.getTech (), pathTechList) == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Line ref: %d Error: cannot find techtree [%s]\n", - __LINE__, scenarioInfo.techTreeName.c_str ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - - mainMessageBoxState = 1; - showMessageBox (szBuf, "Error detected", false); - - return; - } - program->setState (new Game (program, &gameSettings, false)); - return; - } - } - - void MenuStateScenario::setScenario (int i) - { - listBoxScenario.setSelectedItemIndex (i); - loadScenarioInfo (Scenario:: - getScenarioPath (dirList, - scenarioFiles[listBoxScenario. - getSelectedItemIndex - ()]), &scenarioInfo); - } - - void MenuStateScenario::loadScenarioInfo (string file, - ScenarioInfo * scenarioInfo) - { - bool isTutorial = Scenario::isGameTutorial (file); - - cleanupPreviewTexture (); - needToLoadTextures = false; - - if (Scenario::loadScenarioInfo (file, scenarioInfo, isTutorial) == true) - { - previewLoadDelayTimer = time (NULL); - needToLoadTextures = true; - } - } - - void MenuStateScenario::loadScenarioPreviewTexture () - { - if (enableScenarioTexturePreview == true) - { - //if(listBoxScenario.getSelectedItemIndex() >= 0) { - if (listBoxScenario.getItemCount () > 0 - && listBoxScenario.getSelectedItemIndex () >= 0 - && listBoxScenario.getSelectedItemIndex () < - (int) scenarioFiles.size ()) - { - GameSettings gameSettings; - loadGameSettings (&scenarioInfo, &gameSettings); - - string scenarioLogo = ""; - bool loadingImageUsed = false; - - Game::extractScenarioLogoFile (&gameSettings, scenarioLogo, - loadingImageUsed); - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf ("In [%s::%s Line: %d] scenarioLogo [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, scenarioLogo.c_str ()); - - if (scenarioLogo != "") - { - cleanupPreviewTexture (); - scenarioLogoTexture = Renderer::findTexture (scenarioLogo); - } - else - { - cleanupPreviewTexture (); - scenarioLogoTexture = NULL; - } - } - } - } - - void MenuStateScenario::loadGameSettings (const ScenarioInfo * - scenarioInfo, - GameSettings * gameSettings) - { - if (listBoxScenario.getSelectedItemIndex () < 0) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "listBoxScenario.getSelectedItemIndex() < 0, = %d", - listBoxScenario.getSelectedItemIndex ()); - throw megaglest_runtime_error (szBuf); - } - else if (listBoxScenario.getSelectedItemIndex () >= - (int) scenarioFiles.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "listBoxScenario.getSelectedItemIndex() >= scenarioFiles.size(), = [%d][%d]", - listBoxScenario.getSelectedItemIndex (), - (int) scenarioFiles.size ()); - throw megaglest_runtime_error (szBuf); - } - - Scenario::loadGameSettings (dirList, scenarioInfo, gameSettings, - formatString (scenarioFiles - [listBoxScenario. - getSelectedItemIndex ()])); - } - - void MenuStateScenario::showMessageBox (const string & text, - const string & header, - bool toggle) - { - if (!toggle) - { - mainMessageBox.setEnabled (false); - } - - if (!mainMessageBox.getEnabled ()) - { - mainMessageBox.setText (text); - mainMessageBox.setHeader (header); - mainMessageBox.setEnabled (true); - } - else - { - mainMessageBox.setEnabled (false); - } - } - - void MenuStateScenario::keyDown (SDL_KeyboardEvent key) - { - Config & configKeys = - Config::getInstance (std::pair < ConfigType, - ConfigType > (cfgMainKeys, cfgUserKeys)); - if (isKeyPressed (configKeys.getSDLKey ("SaveGUILayout"), key) == true) - { - GraphicComponent::saveAllCustomProperties (containerName); - } - } - - } +namespace Glest { + namespace Game { + + using namespace::Shared::Xml; + + // ===================================================== + // class MenuStateScenario + // ===================================================== + + MenuStateScenario::MenuStateScenario(Program * program, + MainMenu * mainMenu, + bool isTutorialMode, + const vector < string > &dirList, + string + autoloadScenarioName) :MenuState + (program, mainMenu, "scenario") { + containerName = "Scenario"; + this->isTutorialMode = isTutorialMode; + + enableScenarioTexturePreview = + Config::getInstance().getBool("EnableScenarioTexturePreview", + "true"); + scenarioLogoTexture = NULL; + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + + Lang & lang = Lang::getInstance(); + NetworkManager & networkManager = NetworkManager::getInstance(); + try { + networkManager.init(nrServer); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + mainMessageBoxState = 1; + showMessageBox("Error: " + string(ex.what()), "Error detected", + false); + } + + mainMessageBox.registerGraphicComponent(containerName, + "mainMessageBox"); + mainMessageBox.init(lang.getString("Ok")); + mainMessageBox.setEnabled(false); + mainMessageBoxState = 0; + + this->autoloadScenarioName = autoloadScenarioName; + vector < string > results; + + this->dirList = dirList; + + int buttonStartY = 50; + int buttonStartX = 70; + + buttonReturn.registerGraphicComponent(containerName, "buttonReturn"); + buttonReturn.init(buttonStartX, buttonStartY, 125); + buttonReturn.setText(lang.getString("Return")); + + buttonPlayNow.registerGraphicComponent(containerName, "buttonPlayNow"); + buttonPlayNow.init(buttonStartX + 150, buttonStartY, 125); + buttonPlayNow.setText(lang.getString("PlayNow")); + + int startY = 700; + int startX = 50; + + labelScenario.registerGraphicComponent(containerName, "labelScenario"); + labelScenario.init(startX, startY); + + listBoxScenario.registerGraphicComponent(containerName, + "listBoxScenario"); + listBoxScenario.init(startX, startY - 30, 290); + + labelScenarioName.registerGraphicComponent(containerName, + "labelScenarioName"); + labelScenarioName.init(startX, startY - 80); + labelScenarioName.setFont(CoreData::getInstance().getMenuFontBig()); + labelScenarioName.setFont3D(CoreData::getInstance(). + getMenuFontBig3D()); + + labelInfo.registerGraphicComponent(containerName, "labelInfo"); + labelInfo.init(startX, startY - 110); + labelInfo.setFont(CoreData::getInstance().getMenuFontNormal()); + labelInfo.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + if (this->isTutorialMode == true) { + labelScenario.setText(lang.getString("Tutorial")); + } else { + labelScenario.setText(lang.getString("Scenario")); + } + + //scenario listbox + findDirs(dirList, results); + scenarioFiles = results; + //printf("scenarioFiles[0] [%s]\n",scenarioFiles[0].c_str()); + + if (results.empty() == true) { + //throw megaglest_runtime_error("There are no scenarios found to load"); + mainMessageBoxState = 1; + if (this->isTutorialMode == true) { + showMessageBox("Error: There are no tutorials found to load", + "Error detected", false); + } else { + showMessageBox("Error: There are no scenarios found to load", + "Error detected", false); + } + } + + std::map < string, string > scenarioErrors; + for (int i = 0; i < (int) results.size(); ++i) { + results[i] = formatString(results[i]); + } + listBoxScenario.setItems(results); + + try { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] listBoxScenario.getSelectedItemIndex() = %d scenarioFiles.size() = %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, listBoxScenario.getSelectedItemIndex(), + (int) scenarioFiles.size()); + + if (listBoxScenario.getItemCount() > 0 + && listBoxScenario.getSelectedItemIndex() >= 0 + && listBoxScenario.getSelectedItemIndex() < + (int) scenarioFiles.size()) { + string scenarioPath = + Scenario::getScenarioPath(dirList, + scenarioFiles[listBoxScenario. + getSelectedItemIndex + ()]); + //printf("scenarioPath [%s]\n",scenarioPath.c_str()); + + loadScenarioInfo(scenarioPath, &scenarioInfo); + labelInfo.setText(scenarioInfo.desc); + if (scenarioInfo.namei18n != "") { + labelScenarioName.setText(scenarioInfo.namei18n); + } else { + labelScenarioName.setText(listBoxScenario.getSelectedItem()); + } + } + + GraphicComponent::applyAllCustomProperties(containerName); + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + mainMessageBoxState = 1; + showMessageBox("Error: " + string(ex.what()), "Error detected", + false); + } + + if (scenarioErrors.empty() == false) { + mainMessageBoxState = 1; + + string errorMsg = ""; + for (std::map < string, string >::iterator iterMap = + scenarioErrors.begin(); iterMap != scenarioErrors.end(); + ++iterMap) { + errorMsg += + "scenario: " + iterMap->first + " error text: " + + iterMap->second.substr(0, 400) + "\n"; + } + showMessageBox("Error loading scenario(s): " + errorMsg, + "Error detected", false); + } + } + + void MenuStateScenario::reloadUI() { + Lang & lang = Lang::getInstance(); + + console.resetFonts(); + mainMessageBox.init(lang.getString("Ok")); + labelInfo.setFont(CoreData::getInstance().getMenuFontNormal()); + labelInfo.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); + + labelScenarioName.setFont(CoreData::getInstance(). + getMenuFontNormal()); + labelScenarioName.setFont3D(CoreData::getInstance(). + getMenuFontNormal3D()); + + buttonReturn.setText(lang.getString("Return")); + buttonPlayNow.setText(lang.getString("PlayNow")); + + labelScenario.setText(lang.getString("Scenario")); + + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } + + MenuStateScenario::~MenuStateScenario() { + cleanupPreviewTexture(); + } + + void MenuStateScenario::cleanupPreviewTexture() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] scenarioLogoTexture [%p]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenarioLogoTexture); + + if (scenarioLogoTexture != NULL) { + Renderer::getInstance().endTexture(rsGlobal, scenarioLogoTexture, + false); + } + scenarioLogoTexture = NULL; + } + + void MenuStateScenario::mouseClick(int x, int y, MouseButton mouseButton) { + CoreData & coreData = CoreData::getInstance(); + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + string advanceToItemStartingWith = ""; + + if (mainMessageBox.getEnabled()) { + int button = 0; + if (mainMessageBox.mouseClick(x, y, button)) { + soundRenderer.playFx(coreData.getClickSoundA()); + if (button == 0) { + mainMessageBox.setEnabled(false); + + if (scenarioFiles.empty() == true && mainMessageBoxState == 1) { + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + return; + } + } + } + return; + } else { + if (::Shared::Platform::Window::isKeyStateModPressed(KMOD_SHIFT) == + true) { + const wchar_t lastKey = ::Shared::Platform::Window:: + extractLastKeyPressed(); + // xxx: + // string hehe=lastKey; + // printf("lastKey = %d [%c] '%s'\n",lastKey,lastKey,hehe); + advanceToItemStartingWith = lastKey; + } + } + + if (buttonReturn.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundA()); + mainMenu->setState(new MenuStateNewGame(program, mainMenu)); + return; + } else if (buttonPlayNow.mouseClick(x, y)) { + soundRenderer.playFx(coreData.getClickSoundC()); + launchGame(); + return; + } else if (listBoxScenario.mouseClick(x, y, advanceToItemStartingWith)) { + try { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] listBoxScenario.getSelectedItemIndex() = %d scenarioFiles.size() = %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, listBoxScenario.getSelectedItemIndex(), + (int) scenarioFiles.size()); + + if (listBoxScenario.getItemCount() > 0 + && listBoxScenario.getSelectedItemIndex() >= 0 + && listBoxScenario.getSelectedItemIndex() < + (int) scenarioFiles.size()) { + loadScenarioInfo(Scenario:: + getScenarioPath(dirList, + scenarioFiles[listBoxScenario. + getSelectedItemIndex + ()]), + &scenarioInfo); + labelInfo.setText(scenarioInfo.desc); + if (scenarioInfo.namei18n != "") { + labelScenarioName.setText(scenarioInfo.namei18n); + } else { + labelScenarioName.setText(listBoxScenario.getSelectedItem()); + } + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + + mainMessageBoxState = 1; + showMessageBox("Error: " + string(ex.what()), "Error detected", + false); + } + } + } + + void MenuStateScenario::mouseMove(int x, int y, const MouseState * ms) { + + if (mainMessageBox.getEnabled()) { + mainMessageBox.mouseMove(x, y); + } + + listBoxScenario.mouseMove(x, y); + + buttonReturn.mouseMove(x, y); + buttonPlayNow.mouseMove(x, y); + } + + void MenuStateScenario::render() { + + Renderer & renderer = Renderer::getInstance(); + + if (scenarioLogoTexture != NULL) { + renderer.renderTextureQuad(450, 200, 533, 400, scenarioLogoTexture, + 1.0f); + //renderer.renderBackground(scenarioLogoTexture); + } + + if (mainMessageBox.getEnabled()) { + renderer.renderMessageBox(&mainMessageBox); + } else { + renderer.renderLabel(&labelInfo); + renderer.renderLabel(&labelScenarioName); + + renderer.renderLabel(&labelScenario); + renderer.renderListBox(&listBoxScenario); + + renderer.renderButton(&buttonReturn); + renderer.renderButton(&buttonPlayNow); + } + renderer.renderConsole(&console); + if (program != NULL) + program->renderProgramMsgBox(); + } + + void MenuStateScenario::update() { + if (Config::getInstance().getBool("AutoTest")) { + AutoTest::getInstance().updateScenario(this); + return; + } + if (this->autoloadScenarioName != "") { + string scenarioPath = + Scenario::getScenarioPath(dirList, this->autoloadScenarioName); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->autoloadScenarioName.c_str(),scenarioPath.c_str()); + printf + ("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, this->autoloadScenarioName.c_str(), + scenarioPath.c_str()); + + loadScenarioInfo(scenarioPath, &scenarioInfo); + //if(scenarioInfo.namei18n != "") { + // this->autoloadScenarioName = scenarioInfo.namei18n; + //} + //else { + this->autoloadScenarioName = + formatString(this->autoloadScenarioName); + //} + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->autoloadScenarioName.c_str(),scenarioPath.c_str()); + printf + ("[%s:%s] Line: %d this->autoloadScenarioName [%s] scenarioPath [%s] file [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, + __LINE__, this->autoloadScenarioName.c_str(), + scenarioPath.c_str(), scenarioInfo.file.c_str()); + + listBoxScenario.setSelectedItem(this->autoloadScenarioName, false); + + if (listBoxScenario.getSelectedItem() != this->autoloadScenarioName) { + mainMessageBoxState = 1; + showMessageBox("Could not find scenario name: " + + this->autoloadScenarioName, "Scenario Missing", + false); + this->autoloadScenarioName = ""; + } else { + try { + this->autoloadScenarioName = ""; + if (listBoxScenario.getItemCount() > 0 + && listBoxScenario.getSelectedItemIndex() >= 0 + && listBoxScenario.getSelectedItemIndex() < + (int) scenarioFiles.size()) { + + printf + ("[%s:%s] Line: %d scenarioFiles[listBoxScenario.getSelectedItemIndex()] [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, + scenarioFiles[listBoxScenario.getSelectedItemIndex()]. + c_str()); + + loadScenarioInfo(Scenario:: + getScenarioPath(dirList, + scenarioFiles + [listBoxScenario. + getSelectedItemIndex()]), + &scenarioInfo); + + printf("[%s:%s] Line: %d scenarioInfo.file [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenarioInfo.file.c_str()); + + labelInfo.setText(scenarioInfo.desc); + if (scenarioInfo.namei18n != "") { + labelScenarioName.setText(scenarioInfo.namei18n); + } else { + labelScenarioName.setText(listBoxScenario. + getSelectedItem()); + } + + SoundRenderer & soundRenderer = SoundRenderer::getInstance(); + CoreData & coreData = CoreData::getInstance(); + soundRenderer.playFx(coreData.getClickSoundC()); + launchGame(); + return; + } + } catch (const std::exception & ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] Error detected:\n%s\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", + szBuf); + + mainMessageBoxState = 1; + showMessageBox("Error: " + string(ex.what()), "Error detected", + false); + } + } + } + + if (needToLoadTextures) { + // this delay is done to make it possible to switch faster + if (difftime(time(NULL), previewLoadDelayTimer) >= 2) { + loadScenarioPreviewTexture(); + needToLoadTextures = false; + } + } + console.update(); + } + + void MenuStateScenario::launchGame() { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] scenarioInfo.file [%s] [%s][%s][%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenarioInfo.file.c_str(), + scenarioInfo.tilesetName.c_str(), + scenarioInfo.mapName.c_str(), + scenarioInfo.techTreeName.c_str()); + + if (scenarioInfo.file != "" && scenarioInfo.tilesetName != "" + && scenarioInfo.mapName != "" && scenarioInfo.techTreeName != "") { + GameSettings gameSettings; + loadGameSettings(&scenarioInfo, &gameSettings); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] gameSettings.getScenarioDir() [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, + gameSettings.getScenarioDir().c_str()); + + const vector < string > pathTechList = + Config::getInstance().getPathListForType(ptTechs, + gameSettings. + getScenarioDir()); + if (TechTree::exists(gameSettings.getTech(), pathTechList) == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Line ref: %d Error: cannot find techtree [%s]\n", + __LINE__, scenarioInfo.techTreeName.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + + mainMessageBoxState = 1; + showMessageBox(szBuf, "Error detected", false); + + return; + } + program->setState(new Game(program, &gameSettings, false)); + return; + } + } + + void MenuStateScenario::setScenario(int i) { + listBoxScenario.setSelectedItemIndex(i); + loadScenarioInfo(Scenario:: + getScenarioPath(dirList, + scenarioFiles[listBoxScenario. + getSelectedItemIndex + ()]), &scenarioInfo); + } + + void MenuStateScenario::loadScenarioInfo(string file, + ScenarioInfo * scenarioInfo) { + bool isTutorial = Scenario::isGameTutorial(file); + + cleanupPreviewTexture(); + needToLoadTextures = false; + + if (Scenario::loadScenarioInfo(file, scenarioInfo, isTutorial) == true) { + previewLoadDelayTimer = time(NULL); + needToLoadTextures = true; + } + } + + void MenuStateScenario::loadScenarioPreviewTexture() { + if (enableScenarioTexturePreview == true) { + //if(listBoxScenario.getSelectedItemIndex() >= 0) { + if (listBoxScenario.getItemCount() > 0 + && listBoxScenario.getSelectedItemIndex() >= 0 + && listBoxScenario.getSelectedItemIndex() < + (int) scenarioFiles.size()) { + GameSettings gameSettings; + loadGameSettings(&scenarioInfo, &gameSettings); + + string scenarioLogo = ""; + bool loadingImageUsed = false; + + Game::extractScenarioLogoFile(&gameSettings, scenarioLogo, + loadingImageUsed); + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf("In [%s::%s Line: %d] scenarioLogo [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, scenarioLogo.c_str()); + + if (scenarioLogo != "") { + cleanupPreviewTexture(); + scenarioLogoTexture = Renderer::findTexture(scenarioLogo); + } else { + cleanupPreviewTexture(); + scenarioLogoTexture = NULL; + } + } + } + } + + void MenuStateScenario::loadGameSettings(const ScenarioInfo * + scenarioInfo, + GameSettings * gameSettings) { + if (listBoxScenario.getSelectedItemIndex() < 0) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "listBoxScenario.getSelectedItemIndex() < 0, = %d", + listBoxScenario.getSelectedItemIndex()); + throw megaglest_runtime_error(szBuf); + } else if (listBoxScenario.getSelectedItemIndex() >= + (int) scenarioFiles.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "listBoxScenario.getSelectedItemIndex() >= scenarioFiles.size(), = [%d][%d]", + listBoxScenario.getSelectedItemIndex(), + (int) scenarioFiles.size()); + throw megaglest_runtime_error(szBuf); + } + + Scenario::loadGameSettings(dirList, scenarioInfo, gameSettings, + formatString(scenarioFiles + [listBoxScenario. + getSelectedItemIndex()])); + } + + void MenuStateScenario::showMessageBox(const string & text, + const string & header, + bool toggle) { + if (!toggle) { + mainMessageBox.setEnabled(false); + } + + if (!mainMessageBox.getEnabled()) { + mainMessageBox.setText(text); + mainMessageBox.setHeader(header); + mainMessageBox.setEnabled(true); + } else { + mainMessageBox.setEnabled(false); + } + } + + void MenuStateScenario::keyDown(SDL_KeyboardEvent key) { + Config & configKeys = + Config::getInstance(std::pair < ConfigType, + ConfigType >(cfgMainKeys, cfgUserKeys)); + if (isKeyPressed(configKeys.getSDLKey("SaveGUILayout"), key) == true) { + GraphicComponent::saveAllCustomProperties(containerName); + } + } + + } } //end namespace diff --git a/source/glest_game/menu/menu_state_scenario.h b/source/glest_game/menu/menu_state_scenario.h index dc6557c13..61c4f7fe5 100644 --- a/source/glest_game/menu/menu_state_scenario.h +++ b/source/glest_game/menu/menu_state_scenario.h @@ -15,83 +15,80 @@ # include "main_menu.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class MenuStateScenario -// =============================== + // =============================== + // class MenuStateScenario + // =============================== - class MenuStateScenario:public MenuState - { - private: + class MenuStateScenario :public MenuState { + private: - GraphicButton buttonReturn; - GraphicButton buttonPlayNow; + GraphicButton buttonReturn; + GraphicButton buttonPlayNow; - GraphicLabel labelInfo; - GraphicLabel labelScenario; - GraphicListBox listBoxScenario; - GraphicLabel labelScenarioName; + GraphicLabel labelInfo; + GraphicLabel labelScenario; + GraphicListBox listBoxScenario; + GraphicLabel labelScenarioName; - vector < string > scenarioFiles; + vector < string > scenarioFiles; - ScenarioInfo scenarioInfo; - vector < string > dirList; + ScenarioInfo scenarioInfo; + vector < string > dirList; - GraphicMessageBox mainMessageBox; - int mainMessageBoxState; + GraphicMessageBox mainMessageBox; + int mainMessageBoxState; - string autoloadScenarioName; + string autoloadScenarioName; - time_t previewLoadDelayTimer; - bool needToLoadTextures; + time_t previewLoadDelayTimer; + bool needToLoadTextures; - bool enableScenarioTexturePreview; - Texture2D *scenarioLogoTexture; + bool enableScenarioTexturePreview; + Texture2D *scenarioLogoTexture; - bool isTutorialMode; + bool isTutorialMode; - public: - MenuStateScenario (Program * program, MainMenu * mainMenu, - bool isTutorialMode, - const vector < string > &dirList, - string autoloadScenarioName = ""); - virtual ~ MenuStateScenario (); + public: + MenuStateScenario(Program * program, MainMenu * mainMenu, + bool isTutorialMode, + const vector < string > &dirList, + string autoloadScenarioName = ""); + virtual ~MenuStateScenario(); - void mouseClick (int x, int y, MouseButton mouseButton); - void mouseMove (int x, int y, const MouseState * mouseState); - void render (); - void update (); + void mouseClick(int x, int y, MouseButton mouseButton); + void mouseMove(int x, int y, const MouseState * mouseState); + void render(); + void update(); - void launchGame (); - void setScenario (int i); - int getScenarioCount () const - { - return listBoxScenario.getItemCount (); - } + void launchGame(); + void setScenario(int i); + int getScenarioCount() const { + return listBoxScenario.getItemCount(); + } - virtual void keyDown (SDL_KeyboardEvent key); + virtual void keyDown(SDL_KeyboardEvent key); - virtual void reloadUI (); + virtual void reloadUI(); - private: + private: - void loadScenarioInfo (string file, ScenarioInfo * scenarioInfo); - void loadGameSettings (const ScenarioInfo * scenarioInfo, - GameSettings * gameSettings); - void loadScenarioPreviewTexture (); - Difficulty computeDifficulty (const ScenarioInfo * scenarioInfo); - void showMessageBox (const string & text, const string & header, - bool toggle); + void loadScenarioInfo(string file, ScenarioInfo * scenarioInfo); + void loadGameSettings(const ScenarioInfo * scenarioInfo, + GameSettings * gameSettings); + void loadScenarioPreviewTexture(); + Difficulty computeDifficulty(const ScenarioInfo * scenarioInfo); + void showMessageBox(const string & text, const string & header, + bool toggle); - void cleanupPreviewTexture (); - }; + void cleanupPreviewTexture(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/menu/server_line.cpp b/source/glest_game/menu/server_line.cpp index 66f4f1689..9650b1c98 100644 --- a/source/glest_game/menu/server_line.cpp +++ b/source/glest_game/menu/server_line.cpp @@ -21,342 +21,316 @@ #include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class ServerLine -// ===================================================== + // ===================================================== + // class ServerLine + // ===================================================== - ServerLine::ServerLine (MasterServerInfo * mServerInfo, int lineIndex, - int baseY, int lineHeight, - const char *containerName) - { - this->containerName = containerName; - this->countryTexture = NULL; - Lang & lang = Lang::getInstance (); + ServerLine::ServerLine(MasterServerInfo * mServerInfo, int lineIndex, + int baseY, int lineHeight, + const char *containerName) { + this->containerName = containerName; + this->countryTexture = NULL; + Lang & lang = Lang::getInstance(); - this->lineHeight = lineHeight; - int lineOffset = lineHeight * lineIndex; - masterServerInfo = *mServerInfo; - int i = 5; - this->baseY = baseY; - Vec3f color = Vec3f (1.0f, 1.0f, 1.0f); + this->lineHeight = lineHeight; + int lineOffset = lineHeight * lineIndex; + masterServerInfo = *mServerInfo; + int i = 5; + this->baseY = baseY; + Vec3f color = Vec3f(1.0f, 1.0f, 1.0f); - if (masterServerInfo.getConnectedClients () == 0) - { - color = Vec3f (0.6f, 0.7f, 1.0f); - } + if (masterServerInfo.getConnectedClients() == 0) { + color = Vec3f(0.6f, 0.7f, 1.0f); + } - //general info: - //i+= 10; - glestVersionLabel.init (i, baseY - lineOffset); - glestVersionLabel.setRenderBackground (true); - glestVersionLabel.setMaxEditRenderWidth (970); // use background for whole line - glestVersionLabel.setTextColor (color); - glestVersionLabel.setText (" " + masterServerInfo.getGlestVersion ()); - glestVersionLabel.setFont (CoreData::getInstance (). - getDisplayFontSmall ()); - glestVersionLabel.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + //general info: + //i+= 10; + glestVersionLabel.init(i, baseY - lineOffset); + glestVersionLabel.setRenderBackground(true); + glestVersionLabel.setMaxEditRenderWidth(970); // use background for whole line + glestVersionLabel.setTextColor(color); + glestVersionLabel.setText(" " + masterServerInfo.getGlestVersion()); + glestVersionLabel.setFont(CoreData::getInstance(). + getDisplayFontSmall()); + glestVersionLabel.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - i += 80; - string platform = masterServerInfo.getPlatform (); - size_t revOffset = platform.find ("-Rev"); - if (revOffset != platform.npos) - { - platform = platform.substr (0, revOffset); - } + i += 80; + string platform = masterServerInfo.getPlatform(); + size_t revOffset = platform.find("-Rev"); + if (revOffset != platform.npos) { + platform = platform.substr(0, revOffset); + } - platformLabel.init (i, baseY - lineOffset); - platformLabel.setTextColor (color); - platformLabel.setText (platform); - platformLabel.setFont (CoreData::getInstance ().getDisplayFontSmall ()); - platformLabel.setFont3D (CoreData::getInstance (). - getDisplayFontSmall3D ()); + platformLabel.init(i, baseY - lineOffset); + platformLabel.setTextColor(color); + platformLabel.setText(platform); + platformLabel.setFont(CoreData::getInstance().getDisplayFontSmall()); + platformLabel.setFont3D(CoreData::getInstance(). + getDisplayFontSmall3D()); - // i+=50; - // registeredObjNameList.push_back("binaryCompileDateLabel" + intToStr(lineIndex)); - // binaryCompileDateLabel.registerGraphicComponent(containerName,"binaryCompileDateLabel" + intToStr(lineIndex)); - // binaryCompileDateLabel.init(i,baseY-lineOffset); - // binaryCompileDateLabel.setText(masterServerInfo.getBinaryCompileDate()); + // i+=50; + // registeredObjNameList.push_back("binaryCompileDateLabel" + intToStr(lineIndex)); + // binaryCompileDateLabel.registerGraphicComponent(containerName,"binaryCompileDateLabel" + intToStr(lineIndex)); + // binaryCompileDateLabel.init(i,baseY-lineOffset); + // binaryCompileDateLabel.setText(masterServerInfo.getBinaryCompileDate()); - //game info: - i += 120; - serverTitleLabel.init (i, baseY - lineOffset); - serverTitleLabel.setTextColor (color); - serverTitleLabel.setText (masterServerInfo.getServerTitle ()); + //game info: + i += 120; + serverTitleLabel.init(i, baseY - lineOffset); + serverTitleLabel.setTextColor(color); + serverTitleLabel.setText(masterServerInfo.getServerTitle()); - i += 170; - country.init (i, baseY - lineOffset); - country.setTextColor (color); - country.setText (masterServerInfo.getCountry ()); + i += 170; + country.init(i, baseY - lineOffset); + country.setTextColor(color); + country.setText(masterServerInfo.getCountry()); - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - string countryLogoPath = data_path + "data/core/misc_textures/flags"; + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + string countryLogoPath = data_path + "data/core/misc_textures/flags"; - Config & config = Config::getInstance (); - if (config.getString ("CountryTexturePath", "") != "") - { - countryLogoPath = config.getString ("CountryTexturePath", ""); - } - endPathWithSlash (countryLogoPath); + Config & config = Config::getInstance(); + if (config.getString("CountryTexturePath", "") != "") { + countryLogoPath = config.getString("CountryTexturePath", ""); + } + endPathWithSlash(countryLogoPath); - string logoFile = - countryLogoPath + toLower (masterServerInfo.getCountry ()) + ".png"; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] logoFile [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - logoFile.c_str ()); + string logoFile = + countryLogoPath + toLower(masterServerInfo.getCountry()) + ".png"; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] logoFile [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + logoFile.c_str()); - if (fileExists (logoFile) == true) - { - countryTexture = - GraphicsInterface::getInstance ().getFactory ()->newTexture2D (); - //loadingTexture = renderer.newTexture2D(rsGlobal); - countryTexture->setMipmap (true); - //loadingTexture->getPixmap()->load(filepath); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] logoFile [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - logoFile.c_str ()); - countryTexture->load (logoFile); + if (fileExists(logoFile) == true) { + countryTexture = + GraphicsInterface::getInstance().getFactory()->newTexture2D(); + //loadingTexture = renderer.newTexture2D(rsGlobal); + countryTexture->setMipmap(true); + //loadingTexture->getPixmap()->load(filepath); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] logoFile [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + logoFile.c_str()); + countryTexture->load(logoFile); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - Renderer & renderer = Renderer::getInstance (); - renderer.initTexture (rsGlobal, countryTexture); - } + Renderer & renderer = Renderer::getInstance(); + renderer.initTexture(rsGlobal, countryTexture); + } - i += 60; - // ipAddressLabel.init(i,baseY-lineOffset); - // ipAddressLabel.setText(masterServerInfo.getIpAddress()); - // i+=100; + i += 60; + // ipAddressLabel.init(i,baseY-lineOffset); + // ipAddressLabel.setText(masterServerInfo.getIpAddress()); + // i+=100; - wrongVersionLabel.init (i, baseY - lineOffset); - wrongVersionLabel.setTextColor (Vec3f (1.0f, 0.0f, 0.0f)); - wrongVersionLabel.setText (lang.getString ("IncompatibleVersion")); + wrongVersionLabel.init(i, baseY - lineOffset); + wrongVersionLabel.setTextColor(Vec3f(1.0f, 0.0f, 0.0f)); + wrongVersionLabel.setText(lang.getString("IncompatibleVersion")); - //game setup info: - techLabel.init (i, baseY - lineOffset); - techLabel.setTextColor (color); - techLabel.setText (masterServerInfo.getTech ()); + //game setup info: + techLabel.init(i, baseY - lineOffset); + techLabel.setTextColor(color); + techLabel.setText(masterServerInfo.getTech()); - i += 130; - mapLabel.init (i, baseY - lineOffset); - mapLabel.setTextColor (color); - mapLabel.setText (masterServerInfo.getMap ()); - i += 130; + i += 130; + mapLabel.init(i, baseY - lineOffset); + mapLabel.setTextColor(color); + mapLabel.setText(masterServerInfo.getMap()); + i += 130; - // tilesetLabel.init(i,baseY-lineOffset); - // tilesetLabel.setText(masterServerInfo.getTileset()); - // i+=100; + // tilesetLabel.init(i,baseY-lineOffset); + // tilesetLabel.setText(masterServerInfo.getTileset()); + // i+=100; - activeSlotsLabel.init (i, baseY - lineOffset); - activeSlotsLabel.setTextColor (color); - activeSlotsLabel. - setText (intToStr (masterServerInfo.getActiveSlots ()) + "/" + - intToStr (masterServerInfo.getNetworkSlots ()) + "/" + - intToStr (masterServerInfo.getConnectedClients ())); + activeSlotsLabel.init(i, baseY - lineOffset); + activeSlotsLabel.setTextColor(color); + activeSlotsLabel. + setText(intToStr(masterServerInfo.getActiveSlots()) + "/" + + intToStr(masterServerInfo.getNetworkSlots()) + "/" + + intToStr(masterServerInfo.getConnectedClients())); - i += 50; - //externalConnectPort.init(i, baseY - lineOffset); - //externalConnectPort.setTextColor(color); - //externalConnectPort.setText(intToStr(masterServerInfo.getExternalConnectPort())); + i += 50; + //externalConnectPort.init(i, baseY - lineOffset); + //externalConnectPort.setTextColor(color); + //externalConnectPort.setText(intToStr(masterServerInfo.getExternalConnectPort())); - i += 30; - status.init (i - 10, baseY - lineOffset); - status.setTextColor (color); - status.setText (lang. - getString ("MGGameStatus" + - intToStr (masterServerInfo.getStatus ()))); + i += 30; + status.init(i - 10, baseY - lineOffset); + status.setTextColor(color); + status.setText(lang. + getString("MGGameStatus" + + intToStr(masterServerInfo.getStatus()))); - i += 130; - selectButton.init (i + 25, baseY - lineOffset, 35, lineHeight - 5); - selectButton.setText (">"); - selectButton.setAlwaysLighted (true); + i += 130; + selectButton.init(i + 25, baseY - lineOffset, 35, lineHeight - 5); + selectButton.setText(">"); + selectButton.setAlwaysLighted(true); - //printf("glestVersionString [%s] masterServerInfo->getGlestVersion() [%s]\n",glestVersionString.c_str(),masterServerInfo->getGlestVersion().c_str()); - compatible = - checkVersionComptability (glestVersionString, - masterServerInfo.getGlestVersion ()); - selectButton.setEnabled (compatible); - selectButton.setEditable (compatible); + //printf("glestVersionString [%s] masterServerInfo->getGlestVersion() [%s]\n",glestVersionString.c_str(),masterServerInfo->getGlestVersion().c_str()); + compatible = + checkVersionComptability(glestVersionString, + masterServerInfo.getGlestVersion()); + selectButton.setEnabled(compatible); + selectButton.setEditable(compatible); - } + } - void ServerLine::reloadUI () - { - Lang & lang = Lang::getInstance (); + void ServerLine::reloadUI() { + Lang & lang = Lang::getInstance(); - glestVersionLabel.setText (masterServerInfo.getGlestVersion ()); + glestVersionLabel.setText(masterServerInfo.getGlestVersion()); - string platform = masterServerInfo.getPlatform (); - size_t revOffset = platform.find ("-Rev"); - if (revOffset != platform.npos) - { - platform = platform.substr (0, revOffset); - } + string platform = masterServerInfo.getPlatform(); + size_t revOffset = platform.find("-Rev"); + if (revOffset != platform.npos) { + platform = platform.substr(0, revOffset); + } - platformLabel.setText (platform); + platformLabel.setText(platform); - serverTitleLabel.setText (masterServerInfo.getServerTitle ()); + serverTitleLabel.setText(masterServerInfo.getServerTitle()); - country.setText (masterServerInfo.getCountry ()); + country.setText(masterServerInfo.getCountry()); - wrongVersionLabel.setText (lang.getString ("IncompatibleVersion")); + wrongVersionLabel.setText(lang.getString("IncompatibleVersion")); - techLabel.setText (masterServerInfo.getTech ()); + techLabel.setText(masterServerInfo.getTech()); - mapLabel.setText (masterServerInfo.getMap ()); - activeSlotsLabel. - setText (intToStr (masterServerInfo.getActiveSlots ()) + "/" + - intToStr (masterServerInfo.getNetworkSlots ()) + "/" + - intToStr (masterServerInfo.getConnectedClients ())); + mapLabel.setText(masterServerInfo.getMap()); + activeSlotsLabel. + setText(intToStr(masterServerInfo.getActiveSlots()) + "/" + + intToStr(masterServerInfo.getNetworkSlots()) + "/" + + intToStr(masterServerInfo.getConnectedClients())); - //externalConnectPort.setText(intToStr(masterServerInfo.getExternalConnectPort())); + //externalConnectPort.setText(intToStr(masterServerInfo.getExternalConnectPort())); - status.setText (lang. - getString ("MGGameStatus" + - intToStr (masterServerInfo.getStatus ()))); + status.setText(lang. + getString("MGGameStatus" + + intToStr(masterServerInfo.getStatus()))); - GraphicComponent:: - reloadFontsForRegisterGraphicComponents (containerName); - } + GraphicComponent:: + reloadFontsForRegisterGraphicComponents(containerName); + } - ServerLine::~ServerLine () - { - //delete masterServerInfo; + ServerLine::~ServerLine() { + //delete masterServerInfo; - if (countryTexture != NULL) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (countryTexture != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - countryTexture->end (); - delete countryTexture; + countryTexture->end(); + delete countryTexture; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); - //delete loadingTexture; - countryTexture = NULL; - } - } + //delete loadingTexture; + countryTexture = NULL; + } + } - bool ServerLine::buttonMouseClick (int x, int y) - { - return selectButton.mouseClick (x, y); - } + bool ServerLine::buttonMouseClick(int x, int y) { + return selectButton.mouseClick(x, y); + } - bool ServerLine::buttonMouseMove (int x, int y) - { - return selectButton.mouseMove (x, y); - } + bool ServerLine::buttonMouseMove(int x, int y) { + return selectButton.mouseMove(x, y); + } - void ServerLine::render () - { - Renderer & renderer = Renderer::getInstance (); - //general info: - renderer.renderLabel (&glestVersionLabel); - renderer.renderLabel (&platformLabel); - //renderer.renderLabel(&binaryCompileDateLabel); + void ServerLine::render() { + Renderer & renderer = Renderer::getInstance(); + //general info: + renderer.renderLabel(&glestVersionLabel); + renderer.renderLabel(&platformLabel); + //renderer.renderLabel(&binaryCompileDateLabel); - //game info: - renderer.renderLabel (&serverTitleLabel); - if (countryTexture != NULL) - { - renderer.renderTextureQuad (country.getX () + 1, country.getY () + 4, - countryTexture->getTextureWidth (), - countryTexture->getTextureHeight (), - countryTexture, 0.7f); - } - else - { - renderer.renderLabel (&country); - } - if (compatible) - { - if (selectButton.getEnabled () == true) - { - //renderer.renderLabel(&ipAddressLabel); - //game setup info: - renderer.renderLabel (&techLabel); - renderer.renderLabel (&mapLabel); - //renderer.renderLabel(&tilesetLabel); - renderer.renderLabel (&activeSlotsLabel); - //renderer.renderLabel(&externalConnectPort); - renderer.renderLabel (&status); - } - } - else - { - renderer.renderLabel (&wrongVersionLabel); - } - renderer.renderLabel (&status); + //game info: + renderer.renderLabel(&serverTitleLabel); + if (countryTexture != NULL) { + renderer.renderTextureQuad(country.getX() + 1, country.getY() + 4, + countryTexture->getTextureWidth(), + countryTexture->getTextureHeight(), + countryTexture, 0.7f); + } else { + renderer.renderLabel(&country); + } + if (compatible) { + if (selectButton.getEnabled() == true) { + //renderer.renderLabel(&ipAddressLabel); + //game setup info: + renderer.renderLabel(&techLabel); + renderer.renderLabel(&mapLabel); + //renderer.renderLabel(&tilesetLabel); + renderer.renderLabel(&activeSlotsLabel); + //renderer.renderLabel(&externalConnectPort); + renderer.renderLabel(&status); + } + } else { + renderer.renderLabel(&wrongVersionLabel); + } + renderer.renderLabel(&status); - bool joinEnabled = - (masterServerInfo.getNetworkSlots () > - masterServerInfo.getConnectedClients ()); - if (joinEnabled == true) - { - if (compatible) - { - selectButton.setEnabled (true); - selectButton.setVisible (true); - renderer.renderButton (&selectButton); - } - } - else - { - selectButton.setEnabled (false); - selectButton.setVisible (false); - } - } + bool joinEnabled = + (masterServerInfo.getNetworkSlots() > + masterServerInfo.getConnectedClients()); + if (joinEnabled == true) { + if (compatible) { + selectButton.setEnabled(true); + selectButton.setVisible(true); + renderer.renderButton(&selectButton); + } + } else { + selectButton.setEnabled(false); + selectButton.setVisible(false); + } + } - void ServerLine::setY (int y) - { - selectButton.setY (y); + void ServerLine::setY(int y) { + selectButton.setY(y); - //general info: - glestVersionLabel.setY (y); - platformLabel.setY (y); - //binaryCompileDateLabel.setY(y); + //general info: + glestVersionLabel.setY(y); + platformLabel.setY(y); + //binaryCompileDateLabel.setY(y); - //game info: - serverTitleLabel.setY (y); - country.setY (y); - status.setY (y); - //ipAddressLabel.setY(y); + //game info: + serverTitleLabel.setY(y); + country.setY(y); + status.setY(y); + //ipAddressLabel.setY(y); - //game setup info: - techLabel.setY (y); - mapLabel.setY (y); - //tilesetLabel.setY(y); - activeSlotsLabel.setY (y); + //game setup info: + techLabel.setY(y); + mapLabel.setY(y); + //tilesetLabel.setY(y); + activeSlotsLabel.setY(y); - //externalConnectPort.setY(y); + //externalConnectPort.setY(y); - } + } - } + } } //end namespace diff --git a/source/glest_game/menu/server_line.h b/source/glest_game/menu/server_line.h index 9750c08c4..d1ca2a000 100644 --- a/source/glest_game/menu/server_line.h +++ b/source/glest_game/menu/server_line.h @@ -24,71 +24,67 @@ # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// ServerLine -// =============================== + // =============================== + // ServerLine + // =============================== - class ServerLine - { - private: + class ServerLine { + private: - MasterServerInfo masterServerInfo; - int lineHeight; - int baseY; - bool compatible; - GraphicButton selectButton; - GraphicLabel wrongVersionLabel; + MasterServerInfo masterServerInfo; + int lineHeight; + int baseY; + bool compatible; + GraphicButton selectButton; + GraphicLabel wrongVersionLabel; - //general info: - GraphicLabel glestVersionLabel; - GraphicLabel platformLabel; - //GraphicLabel binaryCompileDateLabel; + //general info: + GraphicLabel glestVersionLabel; + GraphicLabel platformLabel; + //GraphicLabel binaryCompileDateLabel; - //game info: - GraphicLabel serverTitleLabel; - GraphicLabel ipAddressLabel; + //game info: + GraphicLabel serverTitleLabel; + GraphicLabel ipAddressLabel; - //game setup info: - GraphicLabel techLabel; - GraphicLabel mapLabel; - GraphicLabel tilesetLabel; - GraphicLabel activeSlotsLabel; + //game setup info: + GraphicLabel techLabel; + GraphicLabel mapLabel; + GraphicLabel tilesetLabel; + GraphicLabel activeSlotsLabel; - GraphicLabel externalConnectPort; + GraphicLabel externalConnectPort; - GraphicLabel country; - GraphicLabel status; + GraphicLabel country; + GraphicLabel status; - Texture2D *countryTexture; + Texture2D *countryTexture; - const char *containerName; + const char *containerName; - public: - ServerLine (MasterServerInfo * mServerInfo, int lineIndex, int baseY, - int lineHeight, const char *containerName); - virtual ~ ServerLine (); - MasterServerInfo *getMasterServerInfo () - { - return &masterServerInfo; - } - const int getLineHeight () const - { - return lineHeight; - } - bool buttonMouseClick (int x, int y); - bool buttonMouseMove (int x, int y); - void setY (int y); - //void setIndex(int value); - void render (); + public: + ServerLine(MasterServerInfo * mServerInfo, int lineIndex, int baseY, + int lineHeight, const char *containerName); + virtual ~ServerLine(); + MasterServerInfo *getMasterServerInfo() { + return &masterServerInfo; + } + const int getLineHeight() const { + return lineHeight; + } + bool buttonMouseClick(int x, int y); + bool buttonMouseMove(int x, int y); + void setY(int y); + //void setIndex(int value); + void render(); - virtual void reloadUI (); - }; + virtual void reloadUI(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 93105bd0e..0d6b72b9e 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -46,1139 +46,566 @@ using namespace Shared::Util; #endif -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -const bool debugClientInterfacePerf = false; + const bool debugClientInterfacePerf = false; -const int ClientInterface::messageWaitTimeout = 10000; //10 seconds -const int ClientInterface::waitSleepTime = 10; -const int ClientInterface::maxNetworkCommandListSendTimeWait = 5; + const int ClientInterface::messageWaitTimeout = 10000; //10 seconds + const int ClientInterface::waitSleepTime = 10; + const int ClientInterface::maxNetworkCommandListSendTimeWait = 5; -// ===================================================== -// class ClientInterfaceThread -// ===================================================== + // ===================================================== + // class ClientInterfaceThread + // ===================================================== -ClientInterfaceThread::ClientInterfaceThread(ClientInterface *client) : BaseThread() { - this->clientInterface = client; - this->uniqueID = "ClientInterfaceThread"; -} - -ClientInterfaceThread::~ClientInterfaceThread() { - this->clientInterface = NULL; -} - -void ClientInterfaceThread::setQuitStatus(bool value) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d value = %d\n",__FILE__,__FUNCTION__,__LINE__,value); - - BaseThread::setQuitStatus(value); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); -} - -bool ClientInterfaceThread::canShutdown(bool deleteSelfIfShutdownDelayed) { - bool ret = (getExecutingTask() == false); - if(ret == false && deleteSelfIfShutdownDelayed == true) { - setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); - deleteSelfIfRequired(); - signalQuit(); - } - - return ret; -} - -void ClientInterfaceThread::execute() { - RunningStatusSafeWrapper runningStatus(this); - try { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n",__FILE__,__FUNCTION__,__LINE__,this); - - // Set socket to non blocking - if(clientInterface != NULL && clientInterface->getSocket(true) != NULL) { - clientInterface->getSocket(true)->setBlock(false); + ClientInterfaceThread::ClientInterfaceThread(ClientInterface *client) : BaseThread() { + this->clientInterface = client; + this->uniqueID = "ClientInterfaceThread"; } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); + ClientInterfaceThread::~ClientInterfaceThread() { + this->clientInterface = NULL; + } - time_t clientSimulationLagStartTime = 0; - Chrono chrono; - for(;this->clientInterface != NULL;) { + void ClientInterfaceThread::setQuitStatus(bool value) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] Line: %d value = %d\n", __FILE__, __FUNCTION__, __LINE__, value); - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; + BaseThread::setQuitStatus(value); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] Line: %d\n", __FILE__, __FUNCTION__, __LINE__); + } + + bool ClientInterfaceThread::canShutdown(bool deleteSelfIfShutdownDelayed) { + bool ret = (getExecutingTask() == false); + if (ret == false && deleteSelfIfShutdownDelayed == true) { + setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); + deleteSelfIfRequired(); + signalQuit(); } - ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + return ret; + } - if(debugClientInterfacePerf == true) printf("START === Client thread\n"); + void ClientInterfaceThread::execute() { + RunningStatusSafeWrapper runningStatus(this); + try { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n", __FILE__, __FUNCTION__, __LINE__, this); - //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); - - uint64 loopCount = 0; - if(debugClientInterfacePerf == true) { - chrono.start(); - } - while( this->getQuitStatus() == false && - clientInterface != NULL) { - //printf("ClientInterfaceThread::exec Line: %d this->getQuitStatus(): %d\n",__LINE__,this->getQuitStatus()); - - // START: Test simulating lag for the client - int simulateLag = Config::getInstance().getInt("SimulateClientLag","0"); - if(simulateLag > 0) { - if(clientSimulationLagStartTime == 0) { - clientSimulationLagStartTime = time(NULL); - } - if(difftime((long int)time(NULL),clientSimulationLagStartTime) <= Config::getInstance().getInt("SimulateClientLagDurationSeconds","0")) { - sleep(simulateLag); - } + // Set socket to non blocking + if (clientInterface != NULL && clientInterface->getSocket(true) != NULL) { + clientInterface->getSocket(true)->setBlock(false); } - // END: Test simulating lag for the client - clientInterface->updateNetworkFrame(); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("ClientInterfaceThread::exec Line: %d\n", __LINE__); - //printf("ClientInterfaceThread::exec Line: %d this->getQuitStatus(): %d\n",__LINE__,this->getQuitStatus()); + time_t clientSimulationLagStartTime = 0; + Chrono chrono; + for (; this->clientInterface != NULL;) { - if(debugClientInterfacePerf == true) { - loopCount++; - if(chrono.getMillis() >= 1000) { - printf("Client thread loopCount = %llu\n",(long long unsigned int)loopCount); + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } - loopCount = 0; - //sleep(0); + ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + + if (debugClientInterfacePerf == true) printf("START === Client thread\n"); + + //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); + + uint64 loopCount = 0; + if (debugClientInterfacePerf == true) { chrono.start(); } - } - } - - if(debugClientInterfacePerf == true) { - printf("END === Client thread\n"); - } - - //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); - - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n",__FILE__,__FUNCTION__,__LINE__,this); - } - catch(const exception &ex) { - - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if(clientInterface == NULL || clientInterface->getSocket(true) == NULL || clientInterface->getSocket(true)->isConnected() == true) { - throw megaglest_runtime_error(ex.what()); - } - } - catch(...) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] UNKNOWN error\n",__FILE__,__FUNCTION__,__LINE__); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); - throw megaglest_runtime_error(szBuf); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); -} - -// ===================================================== -// class ClientInterface -// ===================================================== - -ClientInterface::ClientInterface() : GameNetworkInterface() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] constructor for %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this); - - networkCommandListThreadAccessor = new Mutex(CODE_AT_LINE); - networkCommandListThread = NULL; - cachedPendingCommandsIndex = 0; - cachedLastPendingFrameCount = 0; - timeClientWaitedForLastMessage = 0; - - flagAccessor = new Mutex(CODE_AT_LINE); - - clientSocket = NULL; - sessionKey = 0; - launchGame = false; - introDone = false; - - this->joinGameInProgress = false; - this->joinGameInProgressLaunch = false; - this->readyForInGameJoin = false; - this->resumeInGameJoin = false; - - quitThreadAccessor = new Mutex(CODE_AT_LINE); - setQuitThread(false); - - playerIndex = -1; - gameSettingsReceivedCount = 0; - setGameSettingsReceived(false); - gameSettingsReceivedCount = 0; - connectedTime = 0; - port = 0; - serverFTPPort = 0; - - gotIntro = false; - lastNetworkCommandListSendTime = 0; - currentFrameCount = 0; - lastSentFrameCount = 0; - clientSimulationLagStartTime = 0; - - networkGameDataSynchCheckOkMap = false; - networkGameDataSynchCheckOkTile = false; - networkGameDataSynchCheckOkTech = false; - this->setNetworkGameDataSynchCheckTechMismatchReport(""); - this->setReceivedDataSynchCheck(false); -} - -void ClientInterface::shutdownNetworkCommandListThread(MutexSafeWrapper &safeMutexWrapper) { - if(networkCommandListThread != NULL) { - //printf("START === shutdownNetworkCommandListThread\n"); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - setQuitThread(true); - networkCommandListThread->signalQuit(); - safeMutexWrapper.ReleaseLock(true); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - Chrono chronoElapsed(true); - for(;chronoElapsed.getMillis() <= 10000;) { - safeMutexWrapper.Lock(); - if(networkCommandListThread != NULL && - networkCommandListThread->canShutdown(false) == false && - networkCommandListThread->getRunningStatus() == true) { - - safeMutexWrapper.ReleaseLock(true); - if(chronoElapsed.getMillis() % 1000 == 0) { - sleep(1); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - } - } - else { - safeMutexWrapper.ReleaseLock(true); - break; - } - //printf("%s Line: %d\n",__FUNCTION__,__LINE__); - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n chronoElapsed.getMillis(): %lld",__FUNCTION__,__LINE__,(long long int)chronoElapsed.getMillis()); - //printf("A === shutdownNetworkCommandListThread\n"); - - safeMutexWrapper.Lock(); - if(networkCommandListThread != NULL && - networkCommandListThread->canShutdown(true)) { - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - delete networkCommandListThread; - networkCommandListThread = NULL; - } - else { - networkCommandListThread = NULL; - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - //printf("END === shutdownNetworkCommandListThread\n"); - } -} - -ClientInterface::~ClientInterface() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] destructor for %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this); - //printf("START === Client destructor\n"); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - shutdownNetworkCommandListThread(safeMutex); - //printf("A === Client destructor\n"); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - if(clientSocket != NULL && - clientSocket->isConnected() == true) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string sQuitText = "has chosen to leave the game!"; - if(lang.hasString("PlayerLeftGame",languageList[langIndex]) == true) { - sQuitText = lang.getString("PlayerLeftGame",languageList[langIndex]); - } - - if(clientSocket != NULL && clientSocket->isConnected() == true) { - sendTextMessage(sQuitText,-1,false,languageList[langIndex]); - } - } - } - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - //printf("B === Client destructor\n"); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - close(false); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - //printf("C === Client destructor\n"); - - networkCommandListThreadAccessor = NULL; - safeMutex.ReleaseLock(false,true); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - - delete flagAccessor; - flagAccessor = NULL; - //printf("END === Client destructor\n"); - - delete quitThreadAccessor; - quitThreadAccessor = NULL; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n",__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -bool ClientInterface::getQuitThread() { - MutexSafeWrapper safeMutex(quitThreadAccessor,CODE_AT_LINE); - return this->quitThread; -} -void ClientInterface::setQuitThread(bool value) { - MutexSafeWrapper safeMutex(quitThreadAccessor,CODE_AT_LINE); - this->quitThread = value; -} - -bool ClientInterface::getQuit() { - MutexSafeWrapper safeMutex(quitThreadAccessor,CODE_AT_LINE); - return this->quit; -} -void ClientInterface::setQuit(bool value) { - MutexSafeWrapper safeMutex(quitThreadAccessor,CODE_AT_LINE); - this->quit = value; -} - -bool ClientInterface::getJoinGameInProgress() { - MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); - return joinGameInProgress; -} -bool ClientInterface::getJoinGameInProgressLaunch() { - MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); - return joinGameInProgressLaunch; -} - -bool ClientInterface::getReadyForInGameJoin() { - MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); - return readyForInGameJoin; -} - -bool ClientInterface::getResumeInGameJoin() { - MutexSafeWrapper safeMutex(flagAccessor,CODE_AT_LINE); - return resumeInGameJoin; -} - -void ClientInterface::connect(const Ip &ip, int port) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - this->ip = ip; - this->port = port; - - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - shutdownNetworkCommandListThread(safeMutex); - - delete clientSocket; - clientSocket = NULL; - - safeMutex.ReleaseLock(); - - clientSocket = new ClientSocket(); - clientSocket->setBlock(false); - clientSocket->connect(ip, port); - connectedTime = time(NULL); - //clientSocket->setBlock(true); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END - socket = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,clientSocket->getSocketId()); -} - -void ClientInterface::reset() { - if(getSocket() != NULL) { - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string sQuitText = "has chosen to leave the game!"; - if(lang.hasString("PlayerLeftGame",languageList[langIndex]) == true) { - sQuitText = lang.getString("PlayerLeftGame",languageList[langIndex]); - } - sendTextMessage(sQuitText,-1,false,languageList[langIndex]); - } - close(); - } -} - -void ClientInterface::update() { - bool wasConnected = this->isConnected(); - if(gotIntro == true && - wasConnected == false) { - string playerNameStr = getHumanPlayerName(); - - Lang &lang= Lang::getInstance(); - - char szBuf1[8096]=""; - string statusTextFormat= lang.getString("PlayerDisconnected"); - snprintf(szBuf1,8096,statusTextFormat.c_str(),playerNameStr.c_str()); - - DisplayErrorMessage(szBuf1); - setQuit(true); - return; - } - - try { - NetworkMessageCommandList networkMessageCommandList(currentFrameCount); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - networkMessageCommandList.setNetworkPlayerFactionCRC(index,this->getNetworkPlayerFactionCRC(index)); - } - - //send as many commands as we can - while(requestedCommands.empty() == false) { - if(networkMessageCommandList.addCommand(&requestedCommands.back())) { - requestedCommands.pop_back(); - } - else { - break; - } - } - - double lastSendElapsed = difftime((long int)time(NULL),lastNetworkCommandListSendTime); - - // If we are on a frame that should send packets or we have commands - // to send now, send it now. - if((currentFrameCount >= this->gameSettings.getNetworkFramePeriod() && - currentFrameCount % this->gameSettings.getNetworkFramePeriod() == 0) || - networkMessageCommandList.getCommandCount() > 0) { - - if(lastSentFrameCount < currentFrameCount || - networkMessageCommandList.getCommandCount() > 0) { - - lastSentFrameCount = currentFrameCount; - sendMessage(&networkMessageCommandList); - - lastNetworkCommandListSendTime = time(NULL); - lastSendElapsed = 0; - } - } - - // If we have not sent anything for maxNetworkCommandListSendTimeWait - // seconds, send one now. - if(lastNetworkCommandListSendTime > 0 && - lastSendElapsed >= ClientInterface::maxNetworkCommandListSendTimeWait) { - - lastSentFrameCount = currentFrameCount; - sendMessage(&networkMessageCommandList); - lastNetworkCommandListSendTime = time(NULL); - } - - // Possible cause of out of synch since we have more commands that need - // to be sent in this frame - if(requestedCommands.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,requestedCommands.size()); - - string sMsg = "may go out of synch: client requestedCommands.size() = " + intToStr(requestedCommands.size()); - sendTextMessage(sMsg,-1, true,""); - sleep(1); - } - } - catch(const megaglest_runtime_error &ex) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - - if(this->isConnected() == false) { - if(gotIntro == false || wasConnected == false) { - string sErr = string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " network error: " + string(ex.what()); - DisplayErrorMessage(sErr); - } - - setQuit(true); - } - else { - throw megaglest_runtime_error(ex.what()); - } - } -} - -std::string ClientInterface::getIpAddress(bool mutexLock) { - string result = ""; - //MutexSafeWrapper safeMutexSlot((mutexLock == true ? mutexSocket : NULL),CODE_AT_LINE); - if(clientSocket != NULL) { - result = clientSocket->getIpAddress(); - } - return result; -} - -std::string ClientInterface::getServerIpAddress() { - return this->ip.getString(); -} - -void ClientInterface::updateLobby() { - Chrono chrono; - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); - - NetworkMessageType networkMessageType = getNextMessageType(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - switch(networkMessageType) - { - case nmtInvalid: - break; - - case nmtIntro: - { - NetworkMessageIntro networkMessageIntro; - if(receiveMessage(&networkMessageIntro)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - gotIntro = true; - sessionKey = networkMessageIntro.getSessionId(); - versionString = networkMessageIntro.getVersionString(); - playerIndex = networkMessageIntro.getPlayerIndex(); - serverName = networkMessageIntro.getName(); - serverUUID = networkMessageIntro.getPlayerUUID(); - serverPlatform = networkMessageIntro.getPlayerPlatform(); - serverFTPPort = networkMessageIntro.getFtpPort(); - - if(playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { - throw megaglest_runtime_error("playerIndex < 0 || playerIndex >= GameConstants::maxPlayers"); - } - - MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); - this->joinGameInProgress = (networkMessageIntro.getGameInProgress() != 0); - this->joinGameInProgressLaunch = false; - safeMutexFlags.ReleaseLock(); - - //printf("Client got intro playerIndex = %d\n",playerIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d, playerIndex = %d, serverFTPPort = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageIntro.getGameState(),versionString.c_str(),sessionKey,playerIndex,serverFTPPort); - - //check consistency - bool compatible = checkVersionComptability(networkMessageIntro.getVersionString(), getNetworkVersionGITString()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d, playerIndex = %d, serverFTPPort = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageIntro.getGameState(),versionString.c_str(),sessionKey,playerIndex,serverFTPPort); - - if(compatible == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - bool versionMatched = false; - string platformFreeVersion = getNetworkPlatformFreeVersionString(); - string sErr = ""; - - if(strncmp(platformFreeVersion.c_str(),networkMessageIntro.getVersionString().c_str(),strlen(platformFreeVersion.c_str())) != 0) { - string playerNameStr = getHumanPlayerName(); - sErr = "Server and client binary mismatch!\nYou have to use the exactly same binaries!\n\nServer: " + networkMessageIntro.getVersionString() + - "\nClient: " + getNetworkVersionGITString() + " player [" + playerNameStr + "]"; - printf("%s\n",sErr.c_str()); - - sendTextMessage("Server and client binary mismatch!!",-1, true,""); - sendTextMessage(" Server:" + networkMessageIntro.getVersionString(),-1, true,""); - sendTextMessage(" Client: "+ getNetworkVersionGITString(),-1, true,""); - sendTextMessage(" Client player [" + playerNameStr + "]",-1, true,""); - } - else { - versionMatched = true; - string playerNameStr = getHumanPlayerName(); - sErr = "Warning, Server and client are using the same version but different platforms.\n\nServer: " + networkMessageIntro.getVersionString() + - "\nClient: " + getNetworkVersionGITString() + " player [" + playerNameStr + "]"; - //printf("%s\n",sErr.c_str()); - } - - // error message and disconnect only if checked - if(Config::getInstance().getBool("PlatformConsistencyChecks","true") && - versionMatched == false) { - - DisplayErrorMessage(sErr); - sleep(1); - - setQuit(true); - close(); - return; - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(networkMessageIntro.getGameState() == nmgstOk) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //send intro message - Lang &lang= Lang::getInstance(); - NetworkMessageIntro sendNetworkMessageIntro( - sessionKey,getNetworkVersionGITString(), - getHumanPlayerName(), - -1, - nmgstOk, - this->getSocket()->getConnectedIPAddress(), - serverFTPPort, - lang.getLanguage(), - networkMessageIntro.getGameInProgress(), - Config::getInstance().getString("PlayerId",""), - getPlatformNameString()); - sendMessage(&sendNetworkMessageIntro); - - //printf("Got intro sending client details to server\n"); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(clientSocket == NULL || - clientSocket->isConnected() == false) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - string sErr = "Disconnected from server during intro handshake."; - DisplayErrorMessage(sErr); - setQuit(true); - close(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - introDone = true; - } - } - else if(networkMessageIntro.getGameState() == nmgstNoSlots) { - string sErr = "Cannot join the server because there are no open slots for new players."; - DisplayErrorMessage(sErr); - setQuit(true); - close(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return; - } - else { - string sErr = "Unknown response from server: " + intToStr(networkMessageIntro.getGameState()); - DisplayErrorMessage(sErr); - setQuit(true); - close(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return; - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - } - } - break; - - case nmtPing: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtPing\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - NetworkMessagePing networkMessagePing; - if(receiveMessage(&networkMessagePing)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - this->setLastPingInfo(networkMessagePing); - } - } - break; - - case nmtSynchNetworkGameData: - { - NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData; - - if(receiveMessage(&networkMessageSynchNetworkGameData)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got NetworkMessageSynchNetworkGameData, getTechCRCFileCount() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageSynchNetworkGameData.getTechCRCFileCount()); - - this->setLastPingInfoToNow(); - - networkGameDataSynchCheckOkMap = false; - networkGameDataSynchCheckOkTile = false; - networkGameDataSynchCheckOkTech = false; - this->setNetworkGameDataSynchCheckTechMismatchReport(""); - this->setReceivedDataSynchCheck(false); - - uint32 tilesetCRC = 0; - uint32 techCRC = 0; - uint32 mapCRC = 0; - vector > vctFileList; - - try { - Config &config = Config::getInstance(); - string scenarioDir = ""; - if(gameSettings.getScenarioDir() != "") { - - scenarioDir = gameSettings.getScenarioDir(); - if(EndsWith(scenarioDir, ".xml") == true) { - scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); - scenarioDir = scenarioDir.erase(scenarioDir.size() - gameSettings.getScenario().size(), gameSettings.getScenario().size() + 1); + while (this->getQuitStatus() == false && + clientInterface != NULL) { + //printf("ClientInterfaceThread::exec Line: %d this->getQuitStatus(): %d\n",__LINE__,this->getQuitStatus()); + + // START: Test simulating lag for the client + int simulateLag = Config::getInstance().getInt("SimulateClientLag", "0"); + if (simulateLag > 0) { + if (clientSimulationLagStartTime == 0) { + clientSimulationLagStartTime = time(NULL); + } + if (difftime((long int) time(NULL), clientSimulationLagStartTime) <= Config::getInstance().getInt("SimulateClientLagDurationSeconds", "0")) { + sleep(simulateLag); + } } + // END: Test simulating lag for the client - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameSettings.getScenarioDir().c_str(),gameSettings.getScenario().c_str(),scenarioDir.c_str()); - } + clientInterface->updateNetworkFrame(); - // check the checksum's - tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets,scenarioDir), string("/") + networkMessageSynchNetworkGameData.getTileset() + string("/*"), ".xml", NULL); + //printf("ClientInterfaceThread::exec Line: %d this->getQuitStatus(): %d\n",__LINE__,this->getQuitStatus()); - this->setNetworkGameDataSynchCheckOkTile((tilesetCRC == networkMessageSynchNetworkGameData.getTilesetCRC())); + if (debugClientInterfacePerf == true) { + loopCount++; + if (chrono.getMillis() >= 1000) { + printf("Client thread loopCount = %llu\n", (long long unsigned int)loopCount); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] tilesetCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTileset() = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,tilesetCRC,networkMessageSynchNetworkGameData.getTilesetCRC(),networkMessageSynchNetworkGameData.getTileset().c_str()); - - //tech, load before map because of resources - techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,scenarioDir), string("/") + networkMessageSynchNetworkGameData.getTech() + string("/*"), ".xml", NULL); - - this->setNetworkGameDataSynchCheckOkTech((techCRC == networkMessageSynchNetworkGameData.getTechCRC())); - - if(this->getNetworkGameDataSynchCheckOkTech() == false) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - string pathSearchString = "/" + networkMessageSynchNetworkGameData.getTech() + "/*"; - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),pathSearchString, ".xml", NULL); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - string report = networkMessageSynchNetworkGameData.getTechCRCFileMismatchReport(vctFileList); - this->setNetworkGameDataSynchCheckTechMismatchReport(report); - - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] techCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTech() = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,techCRC,networkMessageSynchNetworkGameData.getTechCRC(),networkMessageSynchNetworkGameData.getTech().c_str()); - - //map - Checksum checksum; - string file = Config::getMapPath(networkMessageSynchNetworkGameData.getMap(),scenarioDir, false); - if(file != "") { - checksum.addFile(file); - mapCRC = checksum.getSum(); - } - this->setNetworkGameDataSynchCheckOkMap((mapCRC == networkMessageSynchNetworkGameData.getMapCRC())); - this->setReceivedDataSynchCheck(true); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] mapCRC info, local = %d, remote = %d, file = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,mapCRC,networkMessageSynchNetworkGameData.getMapCRC(),file.c_str()); - } - catch(const runtime_error &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - string sErr = ex.what(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error during processing, sErr = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str()); - - DisplayErrorMessage(sErr); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus(mapCRC,tilesetCRC,techCRC,vctFileList); - sendMessage(&sendNetworkMessageSynchNetworkGameDataStatus); - } - } - break; - - case nmtSynchNetworkGameDataFileCRCCheck: - { - NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck; - if(receiveMessage(&networkMessageSynchNetworkGameDataFileCRCCheck)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - - Checksum checksum; - string file = networkMessageSynchNetworkGameDataFileCRCCheck.getFileName(); - checksum.addFile(file); - uint32 fileCRC = checksum.getSum(); - - if(fileCRC != networkMessageSynchNetworkGameDataFileCRCCheck.getFileCRC()) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck localCRC = %d, remoteCRC = %d, file [%s]\n", - extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,fileCRC, - networkMessageSynchNetworkGameDataFileCRCCheck.getFileCRC(), - networkMessageSynchNetworkGameDataFileCRCCheck.getFileName().c_str()); - - // Here we initiate a download of missing or mismatched content - - NetworkMessageSynchNetworkGameDataFileGet sendNetworkMessageSynchNetworkGameDataFileGet(networkMessageSynchNetworkGameDataFileCRCCheck.getFileName()); - sendMessage(&sendNetworkMessageSynchNetworkGameDataFileGet); - - FileTransferInfo fileInfo; - fileInfo.hostType = eClient; - fileInfo.serverIP = this->ip.getString(); - fileInfo.serverPort = this->port; - fileInfo.fileName = networkMessageSynchNetworkGameDataFileCRCCheck.getFileName(); - - FileTransferSocketThread *fileXferThread = new FileTransferSocketThread(fileInfo); - fileXferThread->start(); - } - - if(networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex() < networkMessageSynchNetworkGameDataFileCRCCheck.getTotalFileCount()) - { - NetworkMessageSynchNetworkGameDataFileCRCCheck sendNetworkMessageSynchNetworkGameDataFileCRCCheck( - networkMessageSynchNetworkGameDataFileCRCCheck.getTotalFileCount(), - networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex() + 1, - 0, - ""); - sendMessage(&sendNetworkMessageSynchNetworkGameDataFileCRCCheck); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - } - } - break; - - case nmtText: - { - NetworkMessageText networkMessageText; - if(receiveMessage(&networkMessageText)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtText\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - ChatMsgInfo msg(networkMessageText.getText().c_str(),networkMessageText.getTeamIndex(),networkMessageText.getPlayerIndex(),networkMessageText.getTargetLanguage()); - this->addChatInfo(msg); - } - } - break; - - case nmtMarkCell: - { - NetworkMessageMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - MarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); - this->addMarkedCell(msg); - } - } - break; - case nmtUnMarkCell: - { - NetworkMessageUnMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - UnMarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); - this->addUnMarkedCell(msg); - } - } - break; - case nmtHighlightCell: - { - NetworkMessageHighlightCell networkMessageHighlightCell; - if(receiveMessage(&networkMessageHighlightCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtHighlightCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - MarkedCell msg(networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex(), - "none",-1); - this->setHighlightedCell(msg); - } - } - break; - - case nmtLaunch: - case nmtBroadCastSetup: - { - //printf("#1 Got new game setup playerIndex = %d!\n",playerIndex); - - NetworkMessageLaunch networkMessageLaunch; - if(receiveMessage(&networkMessageLaunch, networkMessageType)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - - if(networkMessageLaunch.getMessageType() == nmtLaunch) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtLaunch\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - else if(networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtBroadCastSetup\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); - - char szBuf[1024]=""; - snprintf(szBuf,1023,"In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); - throw megaglest_runtime_error(szBuf); - } - - networkMessageLaunch.buildGameSettings(&gameSettings); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - //printf("Client got game settings playerIndex = %d lookingfor match...\n",playerIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); - //replace server player by network - for(int factionIndex = 0; factionIndex 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - } - } - break; - case nmtPlayerIndexMessage: - { - PlayerIndexMessage playerIndexMessage(-1); - if(receiveMessage(&playerIndexMessage)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - playerIndex= playerIndexMessage.getPlayerIndex(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtPlayerIndexMessage, playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex); - } - - //printf("Got player index changed msg: %d\n",playerIndex); - } - break; - - case nmtReady: - { - NetworkMessageReady networkMessageReady; - if(receiveMessage(&networkMessageReady)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); - this->readyForInGameJoin = true; - } - - //printf("ClientInterface got nmtReady this->readyForInGameJoin: %d\n",this->readyForInGameJoin); - } - break; - - case nmtCommandList: - { - - //make sure we read the message - //time_t receiveTimeElapsed = time(NULL); - NetworkMessageCommandList networkMessageCommandList; - bool gotCmd = receiveMessage(&networkMessageCommandList); - if(gotCmd == false) { - throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - } - break; - - case nmtQuit: - { - //time_t receiveTimeElapsed = time(NULL); - NetworkMessageQuit networkMessageQuit; - bool gotCmd = receiveMessage(&networkMessageQuit); - if(gotCmd == false) { - throw megaglest_runtime_error("error retrieving nmtQuit returned false!"); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - setQuit(true); - close(); - } - break; - - case nmtLoadingStatusMessage: - { - NetworkMessageLoadingStatus networkMessageLoadingStatus(nmls_NONE); - if(receiveMessage(&networkMessageLoadingStatus)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - this->setLastPingInfoToNow(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - break; - - default: - { - string sErr = string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType); - //throw megaglest_runtime_error(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType)); - sendTextMessage("Unexpected network message: " + intToStr(networkMessageType),-1, true,""); - DisplayErrorMessage(sErr); - sleep(1); - - setQuit(true); - close(); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - - if( clientSocket != NULL && clientSocket->isConnected() == true && - gotIntro == false && difftime((long int)time(NULL),connectedTime) > GameConstants::maxClientConnectHandshakeSecs) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] difftime(time(NULL),connectedTime) = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,difftime((long int)time(NULL),connectedTime)); - close(); - } - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); -} - -void ClientInterface::updateNetworkFrame() { - this->updateFrame(NULL); -} - -void ClientInterface::updateFrame(int *checkFrame) { - //printf("#1 ClientInterface::updateFrame\n"); - - //printf("In updateFrame: %d\n",(checkFrame ? *checkFrame : -1)); - - if(isConnected() == true && getQuitThread() == false) { - //printf("#2 ClientInterface::updateFrame\n"); - - uint64 loopCount = 0; - Chrono chronoPerf; - if(debugClientInterfacePerf == true) { - chronoPerf.start(); - } - - int waitMicroseconds = (checkFrame == NULL ? 10 : 0); - - bool done= false; - while(done == false && getQuitThread() == false) { - //printf("BEFORE Client get networkMessageType\n"); - - - //wait for the next message - NetworkMessageType networkMessageType = waitForMessage(waitMicroseconds); - - //printf("AFTER Client got networkMessageType = %d\n",networkMessageType); - - //check we have an expected message - //NetworkMessageType networkMessageType= getNextMessageType(); - - //printf("Got Network networkMessageType: %d\n",networkMessageType); - - switch(networkMessageType) - { - case nmtCommandList: - { - - //make sure we read the message - //time_t receiveTimeElapsed = time(NULL); - NetworkMessageCommandList networkMessageCommandList; - bool gotCmd = receiveMessage(&networkMessageCommandList); - if(gotCmd == false) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] error retrieving nmtCommandList returned false!\n",__FILE__,__FUNCTION__,__LINE__); - if(isConnected() == false) { - setQuit(true); - close(); - return; + loopCount = 0; + //sleep(0); + chrono.start(); + } } - - throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); } - //printf("Client Thread getFrameCount(): %d getCommandCount(): %d\n",networkMessageCommandList.getFrameCount(),networkMessageCommandList.getCommandCount()); + if (debugClientInterfacePerf == true) { + printf("END === Client thread\n"); + } - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - cachedLastPendingFrameCount = networkMessageCommandList.getFrameCount(); - //printf("cachedLastPendingFrameCount = %lld\n",(long long int)cachedLastPendingFrameCount); + //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); - //check that we are in the right frame - if(checkFrame != NULL) { - if(networkMessageCommandList.getFrameCount() != *checkFrame) { - string sErr = "Player: " + getHumanPlayerName() + - " got a Network synchronization error, frame counts do not match, server frameCount = " + - intToStr(networkMessageCommandList.getFrameCount()) + ", local frameCount = " + - intToStr(*checkFrame); - sendTextMessage(sErr,-1, true,""); - DisplayErrorMessage(sErr); + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + //printf("ClientInterfaceThread::exec Line: %d\n",__LINE__); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("ClientInterfaceThread::exec Line: %d\n", __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n", __FILE__, __FUNCTION__, __LINE__, this); + } catch (const exception &ex) { + + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (clientInterface == NULL || clientInterface->getSocket(true) == NULL || clientInterface->getSocket(true)->isConnected() == true) { + throw megaglest_runtime_error(ex.what()); + } + } catch (...) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error\n", __FILE__, __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s] Line: %d\n", __FILE__, __FUNCTION__, __LINE__); + } + + // ===================================================== + // class ClientInterface + // ===================================================== + + ClientInterface::ClientInterface() : GameNetworkInterface() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] constructor for %p\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, this); + + networkCommandListThreadAccessor = new Mutex(CODE_AT_LINE); + networkCommandListThread = NULL; + cachedPendingCommandsIndex = 0; + cachedLastPendingFrameCount = 0; + timeClientWaitedForLastMessage = 0; + + flagAccessor = new Mutex(CODE_AT_LINE); + + clientSocket = NULL; + sessionKey = 0; + launchGame = false; + introDone = false; + + this->joinGameInProgress = false; + this->joinGameInProgressLaunch = false; + this->readyForInGameJoin = false; + this->resumeInGameJoin = false; + + quitThreadAccessor = new Mutex(CODE_AT_LINE); + setQuitThread(false); + + playerIndex = -1; + gameSettingsReceivedCount = 0; + setGameSettingsReceived(false); + gameSettingsReceivedCount = 0; + connectedTime = 0; + port = 0; + serverFTPPort = 0; + + gotIntro = false; + lastNetworkCommandListSendTime = 0; + currentFrameCount = 0; + lastSentFrameCount = 0; + clientSimulationLagStartTime = 0; + + networkGameDataSynchCheckOkMap = false; + networkGameDataSynchCheckOkTile = false; + networkGameDataSynchCheckOkTech = false; + this->setNetworkGameDataSynchCheckTechMismatchReport(""); + this->setReceivedDataSynchCheck(false); + } + + void ClientInterface::shutdownNetworkCommandListThread(MutexSafeWrapper &safeMutexWrapper) { + if (networkCommandListThread != NULL) { + //printf("START === shutdownNetworkCommandListThread\n"); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + setQuitThread(true); + networkCommandListThread->signalQuit(); + safeMutexWrapper.ReleaseLock(true); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + Chrono chronoElapsed(true); + for (; chronoElapsed.getMillis() <= 10000;) { + safeMutexWrapper.Lock(); + if (networkCommandListThread != NULL && + networkCommandListThread->canShutdown(false) == false && + networkCommandListThread->getRunningStatus() == true) { + + safeMutexWrapper.ReleaseLock(true); + if (chronoElapsed.getMillis() % 1000 == 0) { sleep(1); - - setQuit(true); - close(); - return; + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); } - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - printf("Frame: %d faction: %d local CRC: %u Remote CRC: %u\n",*checkFrame,index,getNetworkPlayerFactionCRC(index),networkMessageCommandList.getNetworkPlayerFactionCRC(index)); + } else { + safeMutexWrapper.ReleaseLock(true); + break; + } + //printf("%s Line: %d\n",__FUNCTION__,__LINE__); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n chronoElapsed.getMillis(): %lld", __FUNCTION__, __LINE__, (long long int)chronoElapsed.getMillis()); + //printf("A === shutdownNetworkCommandListThread\n"); + + safeMutexWrapper.Lock(); + if (networkCommandListThread != NULL && + networkCommandListThread->canShutdown(true)) { + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + delete networkCommandListThread; + networkCommandListThread = NULL; + } else { + networkCommandListThread = NULL; + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + //printf("END === shutdownNetworkCommandListThread\n"); + } + } + + ClientInterface::~ClientInterface() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] destructor for %p\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, this); + //printf("START === Client destructor\n"); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + shutdownNetworkCommandListThread(safeMutex); + //printf("A === Client destructor\n"); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + if (clientSocket != NULL && + clientSocket->isConnected() == true) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + + string sQuitText = "has chosen to leave the game!"; + if (lang.hasString("PlayerLeftGame", languageList[langIndex]) == true) { + sQuitText = lang.getString("PlayerLeftGame", languageList[langIndex]); + } + + if (clientSocket != NULL && clientSocket->isConnected() == true) { + sendTextMessage(sQuitText, -1, false, languageList[langIndex]); + } + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + //printf("B === Client destructor\n"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + close(false); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + //printf("C === Client destructor\n"); + + networkCommandListThreadAccessor = NULL; + safeMutex.ReleaseLock(false, true); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + + delete flagAccessor; + flagAccessor = NULL; + //printf("END === Client destructor\n"); + + delete quitThreadAccessor; + quitThreadAccessor = NULL; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("%s Line: %d\n", __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + bool ClientInterface::getQuitThread() { + MutexSafeWrapper safeMutex(quitThreadAccessor, CODE_AT_LINE); + return this->quitThread; + } + void ClientInterface::setQuitThread(bool value) { + MutexSafeWrapper safeMutex(quitThreadAccessor, CODE_AT_LINE); + this->quitThread = value; + } + + bool ClientInterface::getQuit() { + MutexSafeWrapper safeMutex(quitThreadAccessor, CODE_AT_LINE); + return this->quit; + } + void ClientInterface::setQuit(bool value) { + MutexSafeWrapper safeMutex(quitThreadAccessor, CODE_AT_LINE); + this->quit = value; + } + + bool ClientInterface::getJoinGameInProgress() { + MutexSafeWrapper safeMutex(flagAccessor, CODE_AT_LINE); + return joinGameInProgress; + } + bool ClientInterface::getJoinGameInProgressLaunch() { + MutexSafeWrapper safeMutex(flagAccessor, CODE_AT_LINE); + return joinGameInProgressLaunch; + } + + bool ClientInterface::getReadyForInGameJoin() { + MutexSafeWrapper safeMutex(flagAccessor, CODE_AT_LINE); + return readyForInGameJoin; + } + + bool ClientInterface::getResumeInGameJoin() { + MutexSafeWrapper safeMutex(flagAccessor, CODE_AT_LINE); + return resumeInGameJoin; + } + + void ClientInterface::connect(const Ip &ip, int port) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] START\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + this->ip = ip; + this->port = port; + + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + shutdownNetworkCommandListThread(safeMutex); + + delete clientSocket; + clientSocket = NULL; + + safeMutex.ReleaseLock(); + + clientSocket = new ClientSocket(); + clientSocket->setBlock(false); + clientSocket->connect(ip, port); + connectedTime = time(NULL); + //clientSocket->setBlock(true); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END - socket = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, clientSocket->getSocketId()); + } + + void ClientInterface::reset() { + if (getSocket() != NULL) { + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + + string sQuitText = "has chosen to leave the game!"; + if (lang.hasString("PlayerLeftGame", languageList[langIndex]) == true) { + sQuitText = lang.getString("PlayerLeftGame", languageList[langIndex]); + } + sendTextMessage(sQuitText, -1, false, languageList[langIndex]); + } + close(); + } + } + + void ClientInterface::update() { + bool wasConnected = this->isConnected(); + if (gotIntro == true && + wasConnected == false) { + string playerNameStr = getHumanPlayerName(); + + Lang &lang = Lang::getInstance(); + + char szBuf1[8096] = ""; + string statusTextFormat = lang.getString("PlayerDisconnected"); + snprintf(szBuf1, 8096, statusTextFormat.c_str(), playerNameStr.c_str()); + + DisplayErrorMessage(szBuf1); + setQuit(true); + return; + } + + try { + NetworkMessageCommandList networkMessageCommandList(currentFrameCount); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + networkMessageCommandList.setNetworkPlayerFactionCRC(index, this->getNetworkPlayerFactionCRC(index)); + } + + //send as many commands as we can + while (requestedCommands.empty() == false) { + if (networkMessageCommandList.addCommand(&requestedCommands.back())) { + requestedCommands.pop_back(); + } else { + break; + } + } + + double lastSendElapsed = difftime((long int) time(NULL), lastNetworkCommandListSendTime); + + // If we are on a frame that should send packets or we have commands + // to send now, send it now. + if ((currentFrameCount >= this->gameSettings.getNetworkFramePeriod() && + currentFrameCount % this->gameSettings.getNetworkFramePeriod() == 0) || + networkMessageCommandList.getCommandCount() > 0) { + + if (lastSentFrameCount < currentFrameCount || + networkMessageCommandList.getCommandCount() > 0) { + + lastSentFrameCount = currentFrameCount; + sendMessage(&networkMessageCommandList); + + lastNetworkCommandListSendTime = time(NULL); + lastSendElapsed = 0; + } + } + + // If we have not sent anything for maxNetworkCommandListSendTimeWait + // seconds, send one now. + if (lastNetworkCommandListSendTime > 0 && + lastSendElapsed >= ClientInterface::maxNetworkCommandListSendTimeWait) { + + lastSentFrameCount = currentFrameCount; + sendMessage(&networkMessageCommandList); + lastNetworkCommandListSendTime = time(NULL); + } + + // Possible cause of out of synch since we have more commands that need + // to be sent in this frame + if (requestedCommands.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, requestedCommands.size()); + + string sMsg = "may go out of synch: client requestedCommands.size() = " + intToStr(requestedCommands.size()); + sendTextMessage(sMsg, -1, true, ""); + sleep(1); + } + } catch (const megaglest_runtime_error &ex) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + + if (this->isConnected() == false) { + if (gotIntro == false || wasConnected == false) { + string sErr = string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " network error: " + string(ex.what()); + DisplayErrorMessage(sErr); + } + + setQuit(true); + } else { + throw megaglest_runtime_error(ex.what()); + } + } + } + + std::string ClientInterface::getIpAddress(bool mutexLock) { + string result = ""; + //MutexSafeWrapper safeMutexSlot((mutexLock == true ? mutexSocket : NULL),CODE_AT_LINE); + if (clientSocket != NULL) { + result = clientSocket->getIpAddress(); + } + return result; + } + + std::string ClientInterface::getServerIpAddress() { + return this->ip.getString(); + } + + void ClientInterface::updateLobby() { + Chrono chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); + + NetworkMessageType networkMessageType = getNextMessageType(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + switch (networkMessageType) { + case nmtInvalid: + break; + + case nmtIntro: + { + NetworkMessageIntro networkMessageIntro; + if (receiveMessage(&networkMessageIntro)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + gotIntro = true; + sessionKey = networkMessageIntro.getSessionId(); + versionString = networkMessageIntro.getVersionString(); + playerIndex = networkMessageIntro.getPlayerIndex(); + serverName = networkMessageIntro.getName(); + serverUUID = networkMessageIntro.getPlayerUUID(); + serverPlatform = networkMessageIntro.getPlayerPlatform(); + serverFTPPort = networkMessageIntro.getFtpPort(); + + if (playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { + throw megaglest_runtime_error("playerIndex < 0 || playerIndex >= GameConstants::maxPlayers"); + } + + MutexSafeWrapper safeMutexFlags(flagAccessor, CODE_AT_LINE); + this->joinGameInProgress = (networkMessageIntro.getGameInProgress() != 0); + this->joinGameInProgressLaunch = false; + safeMutexFlags.ReleaseLock(); + + //printf("Client got intro playerIndex = %d\n",playerIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d, playerIndex = %d, serverFTPPort = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageIntro.getGameState(), versionString.c_str(), sessionKey, playerIndex, serverFTPPort); + + //check consistency + bool compatible = checkVersionComptability(networkMessageIntro.getVersionString(), getNetworkVersionGITString()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got NetworkMessageIntro, networkMessageIntro.getGameState() = %d, versionString [%s], sessionKey = %d, playerIndex = %d, serverFTPPort = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageIntro.getGameState(), versionString.c_str(), sessionKey, playerIndex, serverFTPPort); + + if (compatible == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + bool versionMatched = false; + string platformFreeVersion = getNetworkPlatformFreeVersionString(); + string sErr = ""; + + if (strncmp(platformFreeVersion.c_str(), networkMessageIntro.getVersionString().c_str(), strlen(platformFreeVersion.c_str())) != 0) { + string playerNameStr = getHumanPlayerName(); + sErr = "Server and client binary mismatch!\nYou have to use the exactly same binaries!\n\nServer: " + networkMessageIntro.getVersionString() + + "\nClient: " + getNetworkVersionGITString() + " player [" + playerNameStr + "]"; + printf("%s\n", sErr.c_str()); + + sendTextMessage("Server and client binary mismatch!!", -1, true, ""); + sendTextMessage(" Server:" + networkMessageIntro.getVersionString(), -1, true, ""); + sendTextMessage(" Client: " + getNetworkVersionGITString(), -1, true, ""); + sendTextMessage(" Client player [" + playerNameStr + "]", -1, true, ""); + } else { + versionMatched = true; + string playerNameStr = getHumanPlayerName(); + sErr = "Warning, Server and client are using the same version but different platforms.\n\nServer: " + networkMessageIntro.getVersionString() + + "\nClient: " + getNetworkVersionGITString() + " player [" + playerNameStr + "]"; + //printf("%s\n",sErr.c_str()); + } + + // error message and disconnect only if checked + if (Config::getInstance().getBool("PlatformConsistencyChecks", "true") && + versionMatched == false) { - if(networkMessageCommandList.getNetworkPlayerFactionCRC(index) != getNetworkPlayerFactionCRC(index)) { - string sErr = "Player: " + getHumanPlayerName() + - " got a Network CRC error, CRC's do not match, server CRC = " + - uIntToStr(networkMessageCommandList.getNetworkPlayerFactionCRC(index)) + ", local CRC = " + - uIntToStr(getNetworkPlayerFactionCRC(index)); - sendTextMessage(sErr,-1, true,""); DisplayErrorMessage(sErr); sleep(1); @@ -1187,1132 +614,1675 @@ void ClientInterface::updateFrame(int *checkFrame) { return; } } - } - cachedPendingCommands[networkMessageCommandList.getFrameCount()].reserve(networkMessageCommandList.getCommandCount()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - // give all commands - for(int i= 0; i < networkMessageCommandList.getCommandCount(); ++i) { - //pendingCommands.push_back(*networkMessageCommandList.getCommand(i)); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - //if(networkMessageCommandList.getCommand(i)->getNetworkCommandType() == nctPauseResume) { - //printf("Network cmd type: %d [%d] frame: %d\n",networkMessageCommandList.getCommand(i)->getNetworkCommandType(),nctPauseResume,networkMessageCommandList.getFrameCount()); - //} + if (networkMessageIntro.getGameState() == nmgstOk) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - cachedPendingCommands[networkMessageCommandList.getFrameCount()].push_back(*networkMessageCommandList.getCommand(i)); + //send intro message + Lang &lang = Lang::getInstance(); + NetworkMessageIntro sendNetworkMessageIntro( + sessionKey, getNetworkVersionGITString(), + getHumanPlayerName(), + -1, + nmgstOk, + this->getSocket()->getConnectedIPAddress(), + serverFTPPort, + lang.getLanguage(), + networkMessageIntro.getGameInProgress(), + Config::getInstance().getString("PlayerId", ""), + getPlatformNameString()); + sendMessage(&sendNetworkMessageIntro); - if(cachedPendingCommandCRCs.find(networkMessageCommandList.getFrameCount()) == cachedPendingCommandCRCs.end()) { - cachedPendingCommandCRCs[networkMessageCommandList.getFrameCount()].reserve(GameConstants::maxPlayers); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - cachedPendingCommandCRCs[networkMessageCommandList.getFrameCount()].push_back(networkMessageCommandList.getNetworkPlayerFactionCRC(index)); + //printf("Got intro sending client details to server\n"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (clientSocket == NULL || + clientSocket->isConnected() == false) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + string sErr = "Disconnected from server during intro handshake."; + DisplayErrorMessage(sErr); + setQuit(true); + close(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + introDone = true; } + } else if (networkMessageIntro.getGameState() == nmgstNoSlots) { + string sErr = "Cannot join the server because there are no open slots for new players."; + DisplayErrorMessage(sErr); + setQuit(true); + close(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; + } else { + string sErr = "Unknown response from server: " + intToStr(networkMessageIntro.getGameState()); + DisplayErrorMessage(sErr); + setQuit(true); + close(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; } + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); } - safeMutex.ReleaseLock(); - - done = true; } break; case nmtPing: { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtPing\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtPing\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); NetworkMessagePing networkMessagePing; - if(receiveMessage(&networkMessagePing)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (receiveMessage(&networkMessagePing)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); this->setLastPingInfo(networkMessagePing); } } break; - case nmtQuit: + case nmtSynchNetworkGameData: { - NetworkMessageQuit networkMessageQuit; - bool gotCmd = receiveMessage(&networkMessageQuit); - if(gotCmd == false) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] error retrieving nmtQuit returned false!\n",__FILE__,__FUNCTION__,__LINE__); - if(isConnected() == false) { - setQuit(true); - close(); - return; + NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData; + + if (receiveMessage(&networkMessageSynchNetworkGameData)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got NetworkMessageSynchNetworkGameData, getTechCRCFileCount() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageSynchNetworkGameData.getTechCRCFileCount()); + + this->setLastPingInfoToNow(); + + networkGameDataSynchCheckOkMap = false; + networkGameDataSynchCheckOkTile = false; + networkGameDataSynchCheckOkTech = false; + this->setNetworkGameDataSynchCheckTechMismatchReport(""); + this->setReceivedDataSynchCheck(false); + + uint32 tilesetCRC = 0; + uint32 techCRC = 0; + uint32 mapCRC = 0; + vector > vctFileList; + + try { + Config &config = Config::getInstance(); + string scenarioDir = ""; + if (gameSettings.getScenarioDir() != "") { + + scenarioDir = gameSettings.getScenarioDir(); + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); + scenarioDir = scenarioDir.erase(scenarioDir.size() - gameSettings.getScenario().size(), gameSettings.getScenario().size() + 1); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, gameSettings.getScenarioDir().c_str(), gameSettings.getScenario().c_str(), scenarioDir.c_str()); + } + + // check the checksum's + tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets, scenarioDir), string("/") + networkMessageSynchNetworkGameData.getTileset() + string("/*"), ".xml", NULL); + + this->setNetworkGameDataSynchCheckOkTile((tilesetCRC == networkMessageSynchNetworkGameData.getTilesetCRC())); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] tilesetCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTileset() = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, tilesetCRC, networkMessageSynchNetworkGameData.getTilesetCRC(), networkMessageSynchNetworkGameData.getTileset().c_str()); + + //tech, load before map because of resources + techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs, scenarioDir), string("/") + networkMessageSynchNetworkGameData.getTech() + string("/*"), ".xml", NULL); + + this->setNetworkGameDataSynchCheckOkTech((techCRC == networkMessageSynchNetworkGameData.getTechCRC())); + + if (this->getNetworkGameDataSynchCheckOkTech() == false) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + string pathSearchString = "/" + networkMessageSynchNetworkGameData.getTech() + "/*"; + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs, scenarioDir), pathSearchString, ".xml", NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + string report = networkMessageSynchNetworkGameData.getTechCRCFileMismatchReport(vctFileList); + this->setNetworkGameDataSynchCheckTechMismatchReport(report); + + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] techCRC info, local = %d, remote = %d, networkMessageSynchNetworkGameData.getTech() = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, techCRC, networkMessageSynchNetworkGameData.getTechCRC(), networkMessageSynchNetworkGameData.getTech().c_str()); + + //map + Checksum checksum; + string file = Config::getMapPath(networkMessageSynchNetworkGameData.getMap(), scenarioDir, false); + if (file != "") { + checksum.addFile(file); + mapCRC = checksum.getSum(); + } + this->setNetworkGameDataSynchCheckOkMap((mapCRC == networkMessageSynchNetworkGameData.getMapCRC())); + this->setReceivedDataSynchCheck(true); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] mapCRC info, local = %d, remote = %d, file = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, mapCRC, networkMessageSynchNetworkGameData.getMapCRC(), file.c_str()); + } catch (const runtime_error &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + string sErr = ex.what(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error during processing, sErr = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, sErr.c_str()); + + DisplayErrorMessage(sErr); } - //throw megaglest_runtime_error("error retrieving nmtQuit returned false!"); - setQuit(true); - done = true; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + NetworkMessageSynchNetworkGameDataStatus sendNetworkMessageSynchNetworkGameDataStatus(mapCRC, tilesetCRC, techCRC, vctFileList); + sendMessage(&sendNetworkMessageSynchNetworkGameDataStatus); + } + } + break; + + case nmtSynchNetworkGameDataFileCRCCheck: + { + NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck; + if (receiveMessage(&networkMessageSynchNetworkGameDataFileCRCCheck)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + + Checksum checksum; + string file = networkMessageSynchNetworkGameDataFileCRCCheck.getFileName(); + checksum.addFile(file); + uint32 fileCRC = checksum.getSum(); + + if (fileCRC != networkMessageSynchNetworkGameDataFileCRCCheck.getFileCRC()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck localCRC = %d, remoteCRC = %d, file [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, fileCRC, + networkMessageSynchNetworkGameDataFileCRCCheck.getFileCRC(), + networkMessageSynchNetworkGameDataFileCRCCheck.getFileName().c_str()); + + // Here we initiate a download of missing or mismatched content + + NetworkMessageSynchNetworkGameDataFileGet sendNetworkMessageSynchNetworkGameDataFileGet(networkMessageSynchNetworkGameDataFileCRCCheck.getFileName()); + sendMessage(&sendNetworkMessageSynchNetworkGameDataFileGet); + + FileTransferInfo fileInfo; + fileInfo.hostType = eClient; + fileInfo.serverIP = this->ip.getString(); + fileInfo.serverPort = this->port; + fileInfo.fileName = networkMessageSynchNetworkGameDataFileCRCCheck.getFileName(); + + FileTransferSocketThread *fileXferThread = new FileTransferSocketThread(fileInfo); + fileXferThread->start(); + } + + if (networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex() < networkMessageSynchNetworkGameDataFileCRCCheck.getTotalFileCount()) { + NetworkMessageSynchNetworkGameDataFileCRCCheck sendNetworkMessageSynchNetworkGameDataFileCRCCheck( + networkMessageSynchNetworkGameDataFileCRCCheck.getTotalFileCount(), + networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex() + 1, + 0, + ""); + sendMessage(&sendNetworkMessageSynchNetworkGameDataFileCRCCheck); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); } - setQuit(true); - done = true; } break; case nmtText: { NetworkMessageText networkMessageText; - bool gotCmd = receiveMessage(&networkMessageText); - if(gotCmd == false) { + if (receiveMessage(&networkMessageText)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] error retrieving nmtText returned false!\n",__FILE__,__FUNCTION__,__LINE__); - if(isConnected() == false) { + this->setLastPingInfoToNow(); - setQuit(true); - close(); - return; - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtText\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); - throw megaglest_runtime_error("error retrieving nmtText returned false!"); + ChatMsgInfo msg(networkMessageText.getText().c_str(), networkMessageText.getTeamIndex(), networkMessageText.getPlayerIndex(), networkMessageText.getTargetLanguage()); + this->addChatInfo(msg); } - - ChatMsgInfo msg(networkMessageText.getText().c_str(),networkMessageText.getTeamIndex(),networkMessageText.getPlayerIndex(),networkMessageText.getTargetLanguage()); - this->addChatInfo(msg); } break; - case nmtMarkCell: - { - NetworkMessageMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + case nmtMarkCell: + { + NetworkMessageMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - MarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); - this->addMarkedCell(msg); - } - } - break; + this->setLastPingInfoToNow(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); - case nmtUnMarkCell: - { - NetworkMessageUnMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + MarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + this->addMarkedCell(msg); + } + } + break; + case nmtUnMarkCell: + { + NetworkMessageUnMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - UnMarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); - this->addUnMarkedCell(msg); - } - } - break; - case nmtHighlightCell: - { - NetworkMessageHighlightCell networkMessageHighlightCell; - if(receiveMessage(&networkMessageHighlightCell)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtHighlightCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + this->setLastPingInfoToNow(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); - MarkedCell msg(networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex(), - "none",-1); - this->setHighlightedCell(msg); - } - } - break; + UnMarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + this->addUnMarkedCell(msg); + } + } + break; + case nmtHighlightCell: + { + NetworkMessageHighlightCell networkMessageHighlightCell; + if (receiveMessage(&networkMessageHighlightCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + this->setLastPingInfoToNow(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtHighlightCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + MarkedCell msg(networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex(), + "none", -1); + this->setHighlightedCell(msg); + } + } + break; case nmtLaunch: case nmtBroadCastSetup: { - //printf("#2 Got new game setup playerIndex = %d!\n",playerIndex); + //printf("#1 Got new game setup playerIndex = %d!\n",playerIndex); NetworkMessageLaunch networkMessageLaunch; - if(receiveMessage(&networkMessageLaunch,networkMessageType)) { + if (receiveMessage(&networkMessageLaunch, networkMessageType)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - if(networkMessageLaunch.getMessageType() == nmtLaunch) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtLaunch\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - else if(networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtBroadCastSetup\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); + this->setLastPingInfoToNow(); - char szBuf[1024]=""; - snprintf(szBuf,1023,"In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); + if (networkMessageLaunch.getMessageType() == nmtLaunch) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtLaunch\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } else if (networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtBroadCastSetup\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + + char szBuf[1024] = ""; + snprintf(szBuf, 1023, "In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); throw megaglest_runtime_error(szBuf); } networkMessageLaunch.buildGameSettings(&gameSettings); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + //printf("Client got game settings playerIndex = %d lookingfor match...\n",playerIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); //replace server player by network - for(int i= 0; i 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + } + } + break; + case nmtPlayerIndexMessage: + { + PlayerIndexMessage playerIndexMessage(-1); + if (receiveMessage(&playerIndexMessage)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + playerIndex = playerIndexMessage.getPlayerIndex(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtPlayerIndexMessage, playerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex); + } + + //printf("Got player index changed msg: %d\n",playerIndex); + } + break; + + case nmtReady: + { + NetworkMessageReady networkMessageReady; + if (receiveMessage(&networkMessageReady)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + MutexSafeWrapper safeMutexFlags(flagAccessor, CODE_AT_LINE); + this->readyForInGameJoin = true; + } + + //printf("ClientInterface got nmtReady this->readyForInGameJoin: %d\n",this->readyForInGameJoin); + } + break; + + case nmtCommandList: + { + + //make sure we read the message + //time_t receiveTimeElapsed = time(NULL); + NetworkMessageCommandList networkMessageCommandList; + bool gotCmd = receiveMessage(&networkMessageCommandList); + if (gotCmd == false) { + throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + } + break; + + case nmtQuit: + { + //time_t receiveTimeElapsed = time(NULL); + NetworkMessageQuit networkMessageQuit; + bool gotCmd = receiveMessage(&networkMessageQuit); + if (gotCmd == false) { + throw megaglest_runtime_error("error retrieving nmtQuit returned false!"); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + setQuit(true); + close(); + } + break; + + case nmtLoadingStatusMessage: + { + NetworkMessageLoadingStatus networkMessageLoadingStatus(nmls_NONE); + if (receiveMessage(&networkMessageLoadingStatus)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + this->setLastPingInfoToNow(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); } } break; - - case nmtLoadingStatusMessage: - break; - - case nmtInvalid: - break; - default: - { - sendTextMessage("Unexpected message in client interface: " + intToStr(networkMessageType),-1, true,""); - DisplayErrorMessage(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected message in client interface: " + intToStr(networkMessageType)); + { + string sErr = string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType); + //throw megaglest_runtime_error(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType)); + sendTextMessage("Unexpected network message: " + intToStr(networkMessageType), -1, true, ""); + DisplayErrorMessage(sErr); sleep(1); setQuit(true); close(); - done = true; - } - break; + } } - if(isConnected() == false && getQuit() == true) { - done = true; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + if (clientSocket != NULL && clientSocket->isConnected() == true && + gotIntro == false && difftime((long int) time(NULL), connectedTime) > GameConstants::maxClientConnectHandshakeSecs) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] difftime(time(NULL),connectedTime) = %f\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, difftime((long int) time(NULL), connectedTime)); + close(); } - if(debugClientInterfacePerf == true) { - loopCount++; - if(chronoPerf.getMillis() >= 1000) { - printf("Client updateFrame loopCount = %llu\n",(long long unsigned int)loopCount); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + } - loopCount = 0; - //sleep(0); + void ClientInterface::updateNetworkFrame() { + this->updateFrame(NULL); + } + + void ClientInterface::updateFrame(int *checkFrame) { + //printf("#1 ClientInterface::updateFrame\n"); + + //printf("In updateFrame: %d\n",(checkFrame ? *checkFrame : -1)); + + if (isConnected() == true && getQuitThread() == false) { + //printf("#2 ClientInterface::updateFrame\n"); + + uint64 loopCount = 0; + Chrono chronoPerf; + if (debugClientInterfacePerf == true) { chronoPerf.start(); } - } - } - if(done == true) { - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - cachedPendingCommandsIndex++; - } - } - //printf("#3 ClientInterface::updateFrame\n"); -} + int waitMicroseconds = (checkFrame == NULL ? 10 : 0); -uint64 ClientInterface::getCachedLastPendingFrameCount() { - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - uint64 result = cachedLastPendingFrameCount; - return result; -} + bool done = false; + while (done == false && getQuitThread() == false) { + //printf("BEFORE Client get networkMessageType\n"); -int64 ClientInterface::getTimeClientWaitedForLastMessage() { - MutexSafeWrapper safeMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - uint64 result = timeClientWaitedForLastMessage; - return result; -} -bool ClientInterface::getNetworkCommand(int frameCount, int currentCachedPendingCommandsIndex) { - bool result = false; - bool waitForData = false; - uint64 copyCachedLastPendingFrameCount = 0; - uint64 waitCount = 0; - uint64 frameCountAsUInt64 = frameCount; - timeClientWaitedForLastMessage = 0; + //wait for the next message + NetworkMessageType networkMessageType = waitForMessage(waitMicroseconds); - //printf("In getNetworkCommand: %d [%d]\n",frameCount,currentCachedPendingCommandsIndex); + //printf("AFTER Client got networkMessageType = %d\n",networkMessageType); - if(getQuit() == false && getQuitThread() == false) { + //check we have an expected message + //NetworkMessageType networkMessageType= getNextMessageType(); - Chrono chrono; - MutexSafeWrapper safeMutex(NULL,CODE_AT_LINE); + //printf("Got Network networkMessageType: %d\n",networkMessageType); - for(;getQuit() == false && getQuitThread() == false;) { + switch (networkMessageType) { + case nmtCommandList: + { - if(safeMutex.isValidMutex() == false) { - safeMutex.setMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - } - else { - safeMutex.Lock(); - } - copyCachedLastPendingFrameCount = cachedLastPendingFrameCount; + //make sure we read the message + //time_t receiveTimeElapsed = time(NULL); + NetworkMessageCommandList networkMessageCommandList; + bool gotCmd = receiveMessage(&networkMessageCommandList); + if (gotCmd == false) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] error retrieving nmtCommandList returned false!\n", __FILE__, __FUNCTION__, __LINE__); + if (isConnected() == false) { + setQuit(true); + close(); + return; + } - if(cachedPendingCommands.find(frameCount) != cachedPendingCommands.end()) { + throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); + } - Commands &frameCmdList = cachedPendingCommands[frameCount]; + //printf("Client Thread getFrameCount(): %d getCommandCount(): %d\n",networkMessageCommandList.getFrameCount(),networkMessageCommandList.getCommandCount()); - //printf("In getNetworkCommand frameCmdList.size(): %d\n",(int)frameCmdList.size()); + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + cachedLastPendingFrameCount = networkMessageCommandList.getFrameCount(); + //printf("cachedLastPendingFrameCount = %lld\n",(long long int)cachedLastPendingFrameCount); - if(frameCmdList.empty() == false) { - for(int index = 0; index < (int)frameCmdList.size(); ++index) { - pendingCommands.push_back(frameCmdList[index]); - } - cachedPendingCommands[frameCount].clear(); + //check that we are in the right frame + if (checkFrame != NULL) { + if (networkMessageCommandList.getFrameCount() != *checkFrame) { + string sErr = "Player: " + getHumanPlayerName() + + " got a Network synchronization error, frame counts do not match, server frameCount = " + + intToStr(networkMessageCommandList.getFrameCount()) + ", local frameCount = " + + intToStr(*checkFrame); + sendTextMessage(sErr, -1, true, ""); + DisplayErrorMessage(sErr); + sleep(1); - if(frameCount >= 0) { - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - //printf("X**X Frame: %d faction: %d local CRC: %u Remote CRC: %u\n",frameCount,index,getNetworkPlayerFactionCRC(index),cachedPendingCommandCRCs[frameCount][index]); + setQuit(true); + close(); + return; + } + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + printf("Frame: %d faction: %d local CRC: %u Remote CRC: %u\n", *checkFrame, index, getNetworkPlayerFactionCRC(index), networkMessageCommandList.getNetworkPlayerFactionCRC(index)); - if(cachedPendingCommandCRCs[frameCount][index] != getNetworkPlayerFactionCRC(index)) { + if (networkMessageCommandList.getNetworkPlayerFactionCRC(index) != getNetworkPlayerFactionCRC(index)) { + string sErr = "Player: " + getHumanPlayerName() + + " got a Network CRC error, CRC's do not match, server CRC = " + + uIntToStr(networkMessageCommandList.getNetworkPlayerFactionCRC(index)) + ", local CRC = " + + uIntToStr(getNetworkPlayerFactionCRC(index)); + sendTextMessage(sErr, -1, true, ""); + DisplayErrorMessage(sErr); + sleep(1); - printf("X**X Frame: %d faction: %d local CRC: %u Remote CRC: %u\n",frameCount,index,getNetworkPlayerFactionCRC(index),cachedPendingCommandCRCs[frameCount][index]); + setQuit(true); + close(); + return; + } + } + } - string sErr = "Player: " + getHumanPlayerName() + - " got a Network CRC error, CRC's do not match, server CRC = " + - uIntToStr(cachedPendingCommandCRCs[frameCount][index]) + ", local CRC = " + - uIntToStr(getNetworkPlayerFactionCRC(index)); - sendTextMessage(sErr,-1, true,""); - DisplayErrorMessage(sErr); - sleep(1); + cachedPendingCommands[networkMessageCommandList.getFrameCount()].reserve(networkMessageCommandList.getCommandCount()); - setQuit(true); - close(); + // give all commands + for (int i = 0; i < networkMessageCommandList.getCommandCount(); ++i) { + //pendingCommands.push_back(*networkMessageCommandList.getCommand(i)); + + //if(networkMessageCommandList.getCommand(i)->getNetworkCommandType() == nctPauseResume) { + //printf("Network cmd type: %d [%d] frame: %d\n",networkMessageCommandList.getCommand(i)->getNetworkCommandType(),nctPauseResume,networkMessageCommandList.getFrameCount()); + //} + + cachedPendingCommands[networkMessageCommandList.getFrameCount()].push_back(*networkMessageCommandList.getCommand(i)); + + if (cachedPendingCommandCRCs.find(networkMessageCommandList.getFrameCount()) == cachedPendingCommandCRCs.end()) { + cachedPendingCommandCRCs[networkMessageCommandList.getFrameCount()].reserve(GameConstants::maxPlayers); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + cachedPendingCommandCRCs[networkMessageCommandList.getFrameCount()].push_back(networkMessageCommandList.getNetworkPlayerFactionCRC(index)); + } + } + } + safeMutex.ReleaseLock(); + + done = true; + } + break; + + case nmtPing: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtPing\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + NetworkMessagePing networkMessagePing; + if (receiveMessage(&networkMessagePing)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + this->setLastPingInfo(networkMessagePing); } } + break; + + case nmtQuit: + { + NetworkMessageQuit networkMessageQuit; + bool gotCmd = receiveMessage(&networkMessageQuit); + if (gotCmd == false) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] error retrieving nmtQuit returned false!\n", __FILE__, __FUNCTION__, __LINE__); + if (isConnected() == false) { + setQuit(true); + close(); + return; + } + + //throw megaglest_runtime_error("error retrieving nmtQuit returned false!"); + setQuit(true); + done = true; + } + setQuit(true); + done = true; + } + break; + + case nmtText: + { + NetworkMessageText networkMessageText; + bool gotCmd = receiveMessage(&networkMessageText); + if (gotCmd == false) { + + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] error retrieving nmtText returned false!\n", __FILE__, __FUNCTION__, __LINE__); + if (isConnected() == false) { + + setQuit(true); + close(); + return; + } + + throw megaglest_runtime_error("error retrieving nmtText returned false!"); + } + + ChatMsgInfo msg(networkMessageText.getText().c_str(), networkMessageText.getTeamIndex(), networkMessageText.getPlayerIndex(), networkMessageText.getTargetLanguage()); + this->addChatInfo(msg); + } + break; + + case nmtMarkCell: + { + NetworkMessageMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + MarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + this->addMarkedCell(msg); + } + } + break; + + case nmtUnMarkCell: + { + NetworkMessageUnMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + UnMarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + this->addUnMarkedCell(msg); + } + } + break; + case nmtHighlightCell: + { + NetworkMessageHighlightCell networkMessageHighlightCell; + if (receiveMessage(&networkMessageHighlightCell)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtHighlightCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + MarkedCell msg(networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex(), + "none", -1); + this->setHighlightedCell(msg); + } + } + break; + + + case nmtLaunch: + case nmtBroadCastSetup: + { + //printf("#2 Got new game setup playerIndex = %d!\n",playerIndex); + + NetworkMessageLaunch networkMessageLaunch; + if (receiveMessage(&networkMessageLaunch, networkMessageType)) { + + if (networkMessageLaunch.getMessageType() == nmtLaunch) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtLaunch\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } else if (networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtBroadCastSetup\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + + char szBuf[1024] = ""; + snprintf(szBuf, 1023, "In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + throw megaglest_runtime_error(szBuf); + } + + networkMessageLaunch.buildGameSettings(&gameSettings); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + //replace server player by network + for (int i = 0; i < gameSettings.getFactionCount(); ++i) { + //replace by network + if (gameSettings.getFactionControl(i) == ctHuman) { + gameSettings.setFactionControl(i, ctNetwork); + } + + //printf("i = %d gameSettings.getStartLocationIndex(i) = %d playerIndex = %d!\n",i,gameSettings.getStartLocationIndex(i),playerIndex); + + //set the faction index + if (gameSettings.getStartLocationIndex(i) == playerIndex) { + //printf("Setting my factionindex to: %d for playerIndex: %d\n",i,playerIndex); + + gameSettings.setThisFactionIndex(i); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] gameSettings.getThisFactionIndex(i) = %d, playerIndex = %d, i = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, gameSettings.getThisFactionIndex(), playerIndex, i); + } + } + } + } + break; + + + case nmtLoadingStatusMessage: + break; + + case nmtInvalid: + break; + + default: + { + sendTextMessage("Unexpected message in client interface: " + intToStr(networkMessageType), -1, true, ""); + DisplayErrorMessage(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected message in client interface: " + intToStr(networkMessageType)); + sleep(1); + + setQuit(true); + close(); + done = true; + } + break; } - cachedPendingCommandCRCs.erase(frameCount); - } - if(waitForData == true) { - timeClientWaitedForLastMessage = chrono.getMillis(); - chrono.stop(); - } - safeMutex.ReleaseLock(true); - result = true; - break; + if (isConnected() == false && getQuit() == true) { + done = true; + } + + if (debugClientInterfacePerf == true) { + loopCount++; + if (chronoPerf.getMillis() >= 1000) { + printf("Client updateFrame loopCount = %llu\n", (long long unsigned int)loopCount); + + loopCount = 0; + //sleep(0); + chronoPerf.start(); + } + } + } + + if (done == true) { + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + cachedPendingCommandsIndex++; + } } - else { - safeMutex.ReleaseLock(true); - // No data for this frame - if(waitForData == false) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Client waiting for packet for frame: %d, copyCachedLastPendingFrameCount = %lld\n",frameCount,(long long int)copyCachedLastPendingFrameCount); - chrono.start(); - } - if(copyCachedLastPendingFrameCount > frameCountAsUInt64) { - break; - } + //printf("#3 ClientInterface::updateFrame\n"); + } + + uint64 ClientInterface::getCachedLastPendingFrameCount() { + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + uint64 result = cachedLastPendingFrameCount; + return result; + } + + int64 ClientInterface::getTimeClientWaitedForLastMessage() { + MutexSafeWrapper safeMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + uint64 result = timeClientWaitedForLastMessage; + return result; + } + + bool ClientInterface::getNetworkCommand(int frameCount, int currentCachedPendingCommandsIndex) { + bool result = false; + bool waitForData = false; + uint64 copyCachedLastPendingFrameCount = 0; + uint64 waitCount = 0; + uint64 frameCountAsUInt64 = frameCount; + timeClientWaitedForLastMessage = 0; + + //printf("In getNetworkCommand: %d [%d]\n",frameCount,currentCachedPendingCommandsIndex); + + if (getQuit() == false && getQuitThread() == false) { + + Chrono chrono; + MutexSafeWrapper safeMutex(NULL, CODE_AT_LINE); + + for (; getQuit() == false && getQuitThread() == false;) { + + if (safeMutex.isValidMutex() == false) { + safeMutex.setMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + } else { + safeMutex.Lock(); + } + copyCachedLastPendingFrameCount = cachedLastPendingFrameCount; + + if (cachedPendingCommands.find(frameCount) != cachedPendingCommands.end()) { + + Commands &frameCmdList = cachedPendingCommands[frameCount]; + + //printf("In getNetworkCommand frameCmdList.size(): %d\n",(int)frameCmdList.size()); + + if (frameCmdList.empty() == false) { + for (int index = 0; index < (int) frameCmdList.size(); ++index) { + pendingCommands.push_back(frameCmdList[index]); + } + cachedPendingCommands[frameCount].clear(); + + if (frameCount >= 0) { + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + //printf("X**X Frame: %d faction: %d local CRC: %u Remote CRC: %u\n",frameCount,index,getNetworkPlayerFactionCRC(index),cachedPendingCommandCRCs[frameCount][index]); + + if (cachedPendingCommandCRCs[frameCount][index] != getNetworkPlayerFactionCRC(index)) { + + printf("X**X Frame: %d faction: %d local CRC: %u Remote CRC: %u\n", frameCount, index, getNetworkPlayerFactionCRC(index), cachedPendingCommandCRCs[frameCount][index]); + + string sErr = "Player: " + getHumanPlayerName() + + " got a Network CRC error, CRC's do not match, server CRC = " + + uIntToStr(cachedPendingCommandCRCs[frameCount][index]) + ", local CRC = " + + uIntToStr(getNetworkPlayerFactionCRC(index)); + sendTextMessage(sErr, -1, true, ""); + DisplayErrorMessage(sErr); + sleep(1); + + setQuit(true); + close(); + } + } + } + cachedPendingCommandCRCs.erase(frameCount); + } + if (waitForData == true) { + timeClientWaitedForLastMessage = chrono.getMillis(); + chrono.stop(); + } + safeMutex.ReleaseLock(true); + + result = true; + break; + } else { + safeMutex.ReleaseLock(true); + // No data for this frame + if (waitForData == false) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Client waiting for packet for frame: %d, copyCachedLastPendingFrameCount = %lld\n", frameCount, (long long int)copyCachedLastPendingFrameCount); + chrono.start(); + } + if (copyCachedLastPendingFrameCount > frameCountAsUInt64) { + break; + } + + if (waitForData == false) { + waitForData = true; + sleep(0); + } + + waitCount++; + //printf("Client waiting for packet for frame: %d, currentCachedPendingCommandsIndex = %d, cachedPendingCommandsIndex = %lld\n",frameCount,currentCachedPendingCommandsIndex,(long long int)cachedPendingCommandsIndex); + } + } + } + if (waitForData == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Client waiting for packet FINISHED for frame: %d, copyCachedLastPendingFrameCount = %lld waitCount = %llu\n", frameCount, (long long int)copyCachedLastPendingFrameCount, (long long unsigned int)waitCount); + } + + return result; + } + + void ClientInterface::updateKeyframe(int frameCount) { + currentFrameCount = frameCount; + + //printf("In updateKeyFrame: %d\n",currentFrameCount); + + if (getQuit() == false && getQuitThread() == false) { + if (networkCommandListThread == NULL) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + networkCommandListThread = new ClientInterfaceThread(this); + networkCommandListThread->setUniqueID(mutexOwnerId); + networkCommandListThread->start(); - if(waitForData == false) { - waitForData = true; sleep(0); } - waitCount++; - //printf("Client waiting for packet for frame: %d, currentCachedPendingCommandsIndex = %d, cachedPendingCommandsIndex = %lld\n",frameCount,currentCachedPendingCommandsIndex,(long long int)cachedPendingCommandsIndex); + getNetworkCommand(frameCount, cachedPendingCommandsIndex); } } - } - if(waitForData == true) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Client waiting for packet FINISHED for frame: %d, copyCachedLastPendingFrameCount = %lld waitCount = %llu\n",frameCount,(long long int)copyCachedLastPendingFrameCount,(long long unsigned int)waitCount); - } - return result; -} - -void ClientInterface::updateKeyframe(int frameCount) { - currentFrameCount = frameCount; - - //printf("In updateKeyFrame: %d\n",currentFrameCount); - - if(getQuit() == false && getQuitThread() == false) { - if(networkCommandListThread == NULL) { - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - networkCommandListThread = new ClientInterfaceThread(this); - networkCommandListThread->setUniqueID(mutexOwnerId); - networkCommandListThread->start(); - - sleep(0); + bool ClientInterface::isMasterServerAdminOverride() { + return (gameSettings.getMasterserver_admin() == this->getSessionKey()); } - getNetworkCommand(frameCount,cachedPendingCommandsIndex); - } -} + void ClientInterface::waitUntilReady(Checksum* checksum) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -bool ClientInterface::isMasterServerAdminOverride() { - return (gameSettings.getMasterserver_admin() == this->getSessionKey()); -} + MutexSafeWrapper safeMutexFlags(flagAccessor, CODE_AT_LINE); + bool signalServerWhenReadyToStartJoinedGame = this->readyForInGameJoin; + this->readyForInGameJoin = false; + safeMutexFlags.ReleaseLock(); -void ClientInterface::waitUntilReady(Checksum* checksum) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + Logger &logger = Logger::getInstance(); - MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); - bool signalServerWhenReadyToStartJoinedGame = this->readyForInGameJoin; - this->readyForInGameJoin = false; - safeMutexFlags.ReleaseLock(); + Chrono chrono; + chrono.start(); - Logger &logger= Logger::getInstance(); + // FOR TESTING ONLY - delay to see the client count up while waiting + //sleep(5000); - Chrono chrono; - chrono.start(); + //clientSocket->setBlock(true); + //send ready message + NetworkMessageReady networkMessageReady; + sendMessage(&networkMessageReady); - // FOR TESTING ONLY - delay to see the client count up while waiting - //sleep(5000); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - //clientSocket->setBlock(true); - //send ready message - NetworkMessageReady networkMessageReady; - sendMessage(&networkMessageReady); + NetworkMessageLoadingStatus networkMessageLoadingStatus(nmls_NONE); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + Lang &lang = Lang::getInstance(); - NetworkMessageLoadingStatus networkMessageLoadingStatus(nmls_NONE); + int64 lastMillisCheck = 0; + uint64 waitLoopIterationCount = 0; + uint64 MAX_LOOP_COUNT_BEFORE_SLEEP = 100; + MAX_LOOP_COUNT_BEFORE_SLEEP = Config::getInstance().getInt("NetworkClientLoopGameLoadingCap", intToStr(MAX_LOOP_COUNT_BEFORE_SLEEP).c_str()); + if (MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { + MAX_LOOP_COUNT_BEFORE_SLEEP = 1; + } + int sleepMillis = Config::getInstance().getInt("NetworkClientLoopGameLoadingCapSleepMillis", "10"); - Lang &lang= Lang::getInstance(); + //wait until we get a ready message from the server + while (true) { + // FOR TESTING ONLY - delay to see the client count up while waiting + //sleep(2000); - int64 lastMillisCheck = 0; - uint64 waitLoopIterationCount = 0; - uint64 MAX_LOOP_COUNT_BEFORE_SLEEP = 100; - MAX_LOOP_COUNT_BEFORE_SLEEP = Config::getInstance().getInt("NetworkClientLoopGameLoadingCap",intToStr(MAX_LOOP_COUNT_BEFORE_SLEEP).c_str()); - if(MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { - MAX_LOOP_COUNT_BEFORE_SLEEP = 1; - } - int sleepMillis = Config::getInstance().getInt("NetworkClientLoopGameLoadingCapSleepMillis","10"); - - //wait until we get a ready message from the server - while(true) { - // FOR TESTING ONLY - delay to see the client count up while waiting - //sleep(2000); - - waitLoopIterationCount++; - if(waitLoopIterationCount > 0 && - waitLoopIterationCount % MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { - sleep(sleepMillis); - waitLoopIterationCount = 0; - } - - if(isConnected() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - string sErr = "Error, Server has disconnected!"; - DisplayErrorMessage(sErr); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - setQuit(true); - close(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return; - } - NetworkMessageType networkMessageType = getNextMessageType(); - - // consume old messages from the lobby - bool discarded = shouldDiscardNetworkMessage(networkMessageType); - if(discarded == false) { - if(networkMessageType == nmtReady) { - if(receiveMessage(&networkMessageReady)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - break; + waitLoopIterationCount++; + if (waitLoopIterationCount > 0 && + waitLoopIterationCount % MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { + sleep(sleepMillis); + waitLoopIterationCount = 0; } - } - else if(networkMessageType == nmtLoadingStatusMessage) { - if(receiveMessage(&networkMessageLoadingStatus)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - else if(networkMessageType == nmtQuit) { - NetworkMessageQuit networkMessageQuit; - if(receiveMessage(&networkMessageQuit)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - DisplayErrorMessage(lang.getString("GameCancelledByUser")); + if (isConnected() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string sErr = "Error, Server has disconnected!"; + DisplayErrorMessage(sErr); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); setQuit(true); close(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); return; + } + NetworkMessageType networkMessageType = getNextMessageType(); + // consume old messages from the lobby + bool discarded = shouldDiscardNetworkMessage(networkMessageType); + if (discarded == false) { + if (networkMessageType == nmtReady) { + if (receiveMessage(&networkMessageReady)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + break; + } + } else if (networkMessageType == nmtLoadingStatusMessage) { + if (receiveMessage(&networkMessageLoadingStatus)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + } else if (networkMessageType == nmtQuit) { + NetworkMessageQuit networkMessageQuit; + if (receiveMessage(&networkMessageQuit)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + DisplayErrorMessage(lang.getString("GameCancelledByUser")); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + setQuit(true); + close(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; + + } + } else if (networkMessageType == nmtCommandList) { + //make sure we read the message + NetworkMessageCommandList networkMessageCommandList; + bool gotCmd = receiveMessage(&networkMessageCommandList); + if (gotCmd == false) { + throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); + } + } else if (networkMessageType == nmtInvalid) { + if (chrono.getMillis() > readyWaitTimeout) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int i = 0; i < languageList.size(); ++i) { + string sErr = "Timeout waiting for server"; + if (lang.hasString("TimeoutWaitingForServer", languageList[i]) == true) { + sErr = lang.getString("TimeoutWaitingForServer", languageList[i]); + } + bool echoLocal = lang.isLanguageLocal(lang.getLanguage()); + sendTextMessage(sErr, -1, echoLocal, languageList[i]); + + if (echoLocal) { + DisplayErrorMessage(sErr); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + sleep(1); + setQuit(true); + close(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; + } else { + if (chrono.getMillis() % 100 == 0) { + lastMillisCheck = (chrono.getMillis() / 1000); + + char szBuf[8096] = ""; + string updateTextFormat = "Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds)"; + if (lang.hasString("NetworkGameClientLoadStatus") == true) { + updateTextFormat = lang.getString("NetworkGameClientLoadStatus"); + } + + string waitForHosts = ""; + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER1_CONNECTED) == nmls_PLAYER1_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER1_READY) != nmls_PLAYER1_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(0); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER2_CONNECTED) == nmls_PLAYER2_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER2_READY) != nmls_PLAYER2_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(1); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER3_CONNECTED) == nmls_PLAYER3_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER3_READY) != nmls_PLAYER3_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(2); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER4_CONNECTED) == nmls_PLAYER4_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER4_READY) != nmls_PLAYER4_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(3); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER5_CONNECTED) == nmls_PLAYER5_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER5_READY) != nmls_PLAYER5_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(4); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER6_CONNECTED) == nmls_PLAYER6_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER6_READY) != nmls_PLAYER6_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(5); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER7_CONNECTED) == nmls_PLAYER7_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER7_READY) != nmls_PLAYER7_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(6); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER8_CONNECTED) == nmls_PLAYER8_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER8_READY) != nmls_PLAYER8_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER9_CONNECTED) == nmls_PLAYER9_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER9_READY) != nmls_PLAYER9_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); + } + } + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER10_CONNECTED) == nmls_PLAYER10_CONNECTED) { + if ((networkMessageLoadingStatus.getStatus() & nmls_PLAYER10_READY) != nmls_PLAYER10_READY) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); + } + } + + if (waitForHosts == "") { + waitForHosts = lang.getString("Server"); + } + snprintf(szBuf, 8096, updateTextFormat.c_str(), (long long int)lastMillisCheck, int(readyWaitTimeout / 1000)); + + char szBuf1[8096] = ""; + string statusTextFormat = "Waiting for players: %s"; + if (lang.hasString("NetworkGameStatusWaiting") == true) { + statusTextFormat = lang.getString("NetworkGameStatusWaiting"); + } + snprintf(szBuf1, 8096, statusTextFormat.c_str(), waitForHosts.c_str()); + + logger.add(szBuf, true, szBuf1); + + sleep(0); + } + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + sendTextMessage("Unexpected network message: " + intToStr(networkMessageType), -1, true, ""); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + DisplayErrorMessage(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + sleep(1); + setQuit(true); + close(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return; + } + + Shared::Platform::Window::handleEvent(); + // sleep a bit + sleep(waitSleepTime); } } - else if(networkMessageType == nmtCommandList) { - //make sure we read the message - NetworkMessageCommandList networkMessageCommandList; - bool gotCmd = receiveMessage(&networkMessageCommandList); - if(gotCmd == false) { - throw megaglest_runtime_error("error retrieving nmtCommandList returned false!"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //check checksum + if (getJoinGameInProgress() == false && + networkMessageReady.getChecksum() != checksum->getSum()) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + + string sErr = "Checksum error, you don't have the same data as the server"; + if (lang.hasString("CheckSumGameLoadError", languageList[langIndex]) == true) { + sErr = lang.getString("CheckSumGameLoadError", languageList[langIndex]); + } + bool echoLocal = lang.isLanguageLocal(lang.getLanguage()); + sendTextMessage(sErr, -1, echoLocal, languageList[langIndex]); + + string playerNameStr = "Player with error is: " + getHumanPlayerName(); + if (lang.hasString("CheckSumGameLoadPlayer", languageList[langIndex]) == true) { + playerNameStr = lang.getString("CheckSumGameLoadPlayer", languageList[langIndex]) + " " + getHumanPlayerName(); + } + sendTextMessage(playerNameStr, -1, echoLocal, languageList[langIndex]); + + string sErr1 = "Client Checksum: " + intToStr(checksum->getSum()); + if (lang.hasString("CheckSumGameLoadClient", languageList[langIndex]) == true) { + sErr1 = lang.getString("CheckSumGameLoadClient", languageList[langIndex]) + " " + intToStr(checksum->getSum()); + } + + sendTextMessage(sErr1, -1, echoLocal, languageList[langIndex]); + + string sErr2 = "Server Checksum: " + intToStr(networkMessageReady.getChecksum()); + if (lang.hasString("CheckSumGameLoadServer", languageList[langIndex]) == true) { + sErr2 = lang.getString("CheckSumGameLoadServer", languageList[langIndex]) + " " + intToStr(networkMessageReady.getChecksum()); + } + sendTextMessage(sErr2, -1, echoLocal, languageList[langIndex]); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d %s %s %s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, sErr.c_str(), sErr1.c_str(), sErr2.c_str()); + + if (echoLocal == true) { + if (Config::getInstance().getBool("NetworkConsistencyChecks")) { + // error message and disconnect only if checked + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + string niceError = sErr + string("\n") + sErr1 + string("\n") + sErr2; + DisplayErrorMessage(niceError); + } + } } - } - else if(networkMessageType == nmtInvalid) { - if(chrono.getMillis() > readyWaitTimeout) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int i = 0; i < languageList.size(); ++i) { - string sErr = "Timeout waiting for server"; - if(lang.hasString("TimeoutWaitingForServer",languageList[i]) == true) { - sErr = lang.getString("TimeoutWaitingForServer",languageList[i]); - } - bool echoLocal = lang.isLanguageLocal(lang.getLanguage()); - sendTextMessage(sErr,-1,echoLocal,languageList[i]); - - if(echoLocal) { - DisplayErrorMessage(sErr); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (Config::getInstance().getBool("NetworkConsistencyChecks")) { + // error message and disconnect only if checked + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); sleep(1); setQuit(true); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + close(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return; } - else { - if(chrono.getMillis() % 100 == 0) { - lastMillisCheck = (chrono.getMillis() / 1000); - - char szBuf[8096]=""; - string updateTextFormat = "Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds)"; - if(lang.hasString("NetworkGameClientLoadStatus") == true) { - updateTextFormat = lang.getString("NetworkGameClientLoadStatus"); - } - - string waitForHosts = ""; - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER1_CONNECTED) == nmls_PLAYER1_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER1_READY) != nmls_PLAYER1_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(0); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER2_CONNECTED) == nmls_PLAYER2_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER2_READY) != nmls_PLAYER2_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(1); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER3_CONNECTED) == nmls_PLAYER3_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER3_READY) != nmls_PLAYER3_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(2); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER4_CONNECTED) == nmls_PLAYER4_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER4_READY) != nmls_PLAYER4_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(3); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER5_CONNECTED) == nmls_PLAYER5_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER5_READY) != nmls_PLAYER5_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(4); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER6_CONNECTED) == nmls_PLAYER6_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER6_READY) != nmls_PLAYER6_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(5); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER7_CONNECTED) == nmls_PLAYER7_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER7_READY) != nmls_PLAYER7_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(6); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER8_CONNECTED) == nmls_PLAYER8_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER8_READY) != nmls_PLAYER8_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER9_CONNECTED) == nmls_PLAYER9_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER9_READY) != nmls_PLAYER9_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); - } - } - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER10_CONNECTED) == nmls_PLAYER10_CONNECTED) { - if((networkMessageLoadingStatus.getStatus() & nmls_PLAYER10_READY) != nmls_PLAYER10_READY) { - if(waitForHosts != "") { - waitForHosts += ", "; - } - waitForHosts += gameSettings.getNetworkPlayerNameByPlayerIndex(7); - } - } - - if(waitForHosts == "") { - waitForHosts = lang.getString("Server"); - } - snprintf(szBuf,8096,updateTextFormat.c_str(),(long long int)lastMillisCheck,int(readyWaitTimeout / 1000)); - - char szBuf1[8096]=""; - string statusTextFormat = "Waiting for players: %s"; - if(lang.hasString("NetworkGameStatusWaiting") == true) { - statusTextFormat = lang.getString("NetworkGameStatusWaiting"); - } - snprintf(szBuf1,8096,statusTextFormat.c_str(),waitForHosts.c_str()); - - logger.add(szBuf, true, szBuf1); - - sleep(0); - } - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - sendTextMessage("Unexpected network message: " + intToStr(networkMessageType),-1, true,""); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - DisplayErrorMessage(string(extractFileFromDirectoryPath(__FILE__).c_str()) + "::" + string(__FUNCTION__) + " Unexpected network message: " + intToStr(networkMessageType)); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - sleep(1); - setQuit(true); - close(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); return; } - Shared::Platform::Window::handleEvent(); - // sleep a bit - sleep(waitSleepTime); - } - } + MutexSafeWrapper safeMutexFlags2(flagAccessor, CODE_AT_LINE); + this->joinGameInProgress = false; + this->joinGameInProgressLaunch = false; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //printf("Client signalServerWhenReadyToStartJoinedGame = %d\n",signalServerWhenReadyToStartJoinedGame); + if (signalServerWhenReadyToStartJoinedGame == true) { - //check checksum - if(getJoinGameInProgress() == false && - networkMessageReady.getChecksum() != checksum->getSum()) { + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string sErr = "Checksum error, you don't have the same data as the server"; - if(lang.hasString("CheckSumGameLoadError",languageList[langIndex]) == true) { - sErr = lang.getString("CheckSumGameLoadError",languageList[langIndex]); - } - bool echoLocal = lang.isLanguageLocal(lang.getLanguage()); - sendTextMessage(sErr,-1,echoLocal,languageList[langIndex]); - - string playerNameStr = "Player with error is: " + getHumanPlayerName(); - if(lang.hasString("CheckSumGameLoadPlayer",languageList[langIndex]) == true) { - playerNameStr = lang.getString("CheckSumGameLoadPlayer",languageList[langIndex]) + " " + getHumanPlayerName(); - } - sendTextMessage(playerNameStr,-1,echoLocal,languageList[langIndex]); - - string sErr1 = "Client Checksum: " + intToStr(checksum->getSum()); - if(lang.hasString("CheckSumGameLoadClient",languageList[langIndex]) == true) { - sErr1 = lang.getString("CheckSumGameLoadClient",languageList[langIndex]) + " " + intToStr(checksum->getSum()); - } - - sendTextMessage(sErr1,-1,echoLocal,languageList[langIndex]); - - string sErr2 = "Server Checksum: " + intToStr(networkMessageReady.getChecksum()); - if(lang.hasString("CheckSumGameLoadServer",languageList[langIndex]) == true) { - sErr2 = lang.getString("CheckSumGameLoadServer",languageList[langIndex]) + " " + intToStr(networkMessageReady.getChecksum()); - } - sendTextMessage(sErr2,-1,echoLocal,languageList[langIndex]); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d %s %s %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str(),sErr1.c_str(),sErr2.c_str()); - - if(echoLocal == true) { - if(Config::getInstance().getBool("NetworkConsistencyChecks")) { - // error message and disconnect only if checked - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - string niceError = sErr + string("\n") + sErr1 + string("\n") + sErr2; - DisplayErrorMessage(niceError); - } - } - } - - if(Config::getInstance().getBool("NetworkConsistencyChecks")) { - // error message and disconnect only if checked - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - sleep(1); - setQuit(true); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - close(); - } - return; - } - - MutexSafeWrapper safeMutexFlags2(flagAccessor,CODE_AT_LINE); - this->joinGameInProgress = false; - this->joinGameInProgressLaunch = false; - - //printf("Client signalServerWhenReadyToStartJoinedGame = %d\n",signalServerWhenReadyToStartJoinedGame); - if(signalServerWhenReadyToStartJoinedGame == true) { - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string sText = "Player: %s is joining the game now."; - if(lang.hasString("JoinPlayerToCurrentGameLaunchDone",languageList[langIndex]) == true) { - sText = lang.getString("JoinPlayerToCurrentGameLaunchDone",languageList[langIndex]); - } - - if(clientSocket != NULL && clientSocket->isConnected() == true) { - string playerNameStr = getHumanPlayerName(); - char szBuf[8096]=""; - snprintf(szBuf,8096,sText.c_str(),playerNameStr.c_str()); - - sendTextMessage(szBuf,-1,false,languageList[langIndex]); - } - } - - this->resumeInGameJoin = true; - safeMutexFlags2.ReleaseLock(); - } - else { - safeMutexFlags2.ReleaseLock(); - // delay the start a bit, so clients have more room to get messages - // This is to ensure clients don't start ahead of the server and thus - // constantly freeze because they are waiting for the server to catch up - sleep(120); - } - - // This triggers LAG update packets to begin as required - lastNetworkCommandListSendTime = time(NULL); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); -} - -void ClientInterface::sendResumeGameMessage() { - NetworkMessageReady networkMessageReady; - sendMessage(&networkMessageReady); -} - -void ClientInterface::sendTextMessage(const string &text, int teamIndex, bool echoLocal, - string targetLanguage) { - - string humanPlayerName = getHumanPlayerName(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,humanPlayerName.c_str(),playerIndex); - - NetworkMessageText networkMessageText(text, teamIndex,playerIndex,targetLanguage); - sendMessage(&networkMessageText); - - if(echoLocal == true) { - ChatMsgInfo msg(networkMessageText.getText().c_str(),networkMessageText.getTeamIndex(),networkMessageText.getPlayerIndex(),targetLanguage); - this->addChatInfo(msg); - } -} - -void ClientInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note,int playerIndex) { - string humanPlayerName = getHumanPlayerName(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,humanPlayerName.c_str(),playerIndex); - - NetworkMessageMarkCell networkMessageMarkCell(targetPos,factionIndex, note,playerIndex); - sendMessage(&networkMessageMarkCell); -} - -void ClientInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex); - - NetworkMessageHighlightCell networkMessageHighlightCell(targetPos,factionIndex); - sendMessage(&networkMessageHighlightCell); -} - -void ClientInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) { - string humanPlayerName = getHumanPlayerName(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,humanPlayerName.c_str(),playerIndex); - - NetworkMessageUnMarkCell networkMessageMarkCell(targetPos,factionIndex); - sendMessage(&networkMessageMarkCell); -} - -void ClientInterface::sendPingMessage(int32 pingFrequency, int64 pingTime) { - NetworkMessagePing networkMessagePing(pingFrequency,pingTime); - sendMessage(&networkMessagePing); -} - -string ClientInterface::getNetworkStatus() { - std::string label = Lang::getInstance().getString("Server") + ": " + serverName; - //float pingTime = getThreadedPingMS(getServerIpAddress().c_str()); - char szBuf[8096]=""; - snprintf(szBuf,8096,"%s",label.c_str()); - - return szBuf; -} - -NetworkMessageType ClientInterface::waitForMessage(int waitMicroseconds) -{ - // Debug! -/* - sendTextMessage("Timeout waiting for message",-1); - DisplayErrorMessage("Timeout waiting for message"); - quit= true; - close(); - return; -*/ - - uint64 loopCount = 0; - Chrono chronoPerf; - if(debugClientInterfacePerf == true) { - chronoPerf.start(); - } - - Chrono chrono; - chrono.start(); - - NetworkMessageType msg = nmtInvalid; - while( msg == nmtInvalid && - getQuitThread() == false) { - - msg = getNextMessageType(waitMicroseconds); - if(msg == nmtInvalid) { - if(getSocket() == NULL || (chrono.getMillis() % 250 == 0 && isConnected() == false)) { - if(getQuit() == false) { - //throw megaglest_runtime_error("Disconnected"); - //sendTextMessage("Server has Disconnected.",-1); - DisplayErrorMessage("Server has Disconnected."); - setQuit(true); - } - close(); - return msg; - } - - if(chrono.getMillis() > messageWaitTimeout) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string msg = "Timeout waiting for message."; - if(lang.hasString("TimeoutWaitingForMessage",languageList[langIndex]) == true) { - msg = lang.getString("TimeoutWaitingForMessage",languageList[langIndex]); - } - - sendTextMessage(msg,-1, lang.isLanguageLocal(languageList[langIndex]),languageList[langIndex]); - if(lang.isLanguageLocal(languageList[langIndex]) == true) { - DisplayErrorMessage(msg); + string sText = "Player: %s is joining the game now."; + if (lang.hasString("JoinPlayerToCurrentGameLaunchDone", languageList[langIndex]) == true) { + sText = lang.getString("JoinPlayerToCurrentGameLaunchDone", languageList[langIndex]); } - } - sleep(1); - setQuit(true); - close(); - return msg; + if (clientSocket != NULL && clientSocket->isConnected() == true) { + string playerNameStr = getHumanPlayerName(); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, sText.c_str(), playerNameStr.c_str()); + + sendTextMessage(szBuf, -1, false, languageList[langIndex]); + } + } + + this->resumeInGameJoin = true; + safeMutexFlags2.ReleaseLock(); + } else { + safeMutexFlags2.ReleaseLock(); + // delay the start a bit, so clients have more room to get messages + // This is to ensure clients don't start ahead of the server and thus + // constantly freeze because they are waiting for the server to catch up + sleep(120); } - // Sleep every x milli-seconds we wait to let other threads work - else if(chrono.getMillis() % 2 == 0) { - sleep(1); - } - else { - sleep(0); + + // This triggers LAG update packets to begin as required + lastNetworkCommandListSendTime = time(NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + } + + void ClientInterface::sendResumeGameMessage() { + NetworkMessageReady networkMessageReady; + sendMessage(&networkMessageReady); + } + + void ClientInterface::sendTextMessage(const string &text, int teamIndex, bool echoLocal, + string targetLanguage) { + + string humanPlayerName = getHumanPlayerName(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, humanPlayerName.c_str(), playerIndex); + + NetworkMessageText networkMessageText(text, teamIndex, playerIndex, targetLanguage); + sendMessage(&networkMessageText); + + if (echoLocal == true) { + ChatMsgInfo msg(networkMessageText.getText().c_str(), networkMessageText.getTeamIndex(), networkMessageText.getPlayerIndex(), targetLanguage); + this->addChatInfo(msg); } } - if(debugClientInterfacePerf == true) { - loopCount++; - if(chronoPerf.getMillis() >= 100) { - printf("Client waitForMessage loopCount = %llu\n",(long long unsigned int)loopCount); + void ClientInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex) { + string humanPlayerName = getHumanPlayerName(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, humanPlayerName.c_str(), playerIndex); - loopCount = 0; + NetworkMessageMarkCell networkMessageMarkCell(targetPos, factionIndex, note, playerIndex); + sendMessage(&networkMessageMarkCell); + } + + void ClientInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex); + + NetworkMessageHighlightCell networkMessageHighlightCell(targetPos, factionIndex); + sendMessage(&networkMessageHighlightCell); + } + + void ClientInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) { + string humanPlayerName = getHumanPlayerName(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] humanPlayerName = [%s] playerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, humanPlayerName.c_str(), playerIndex); + + NetworkMessageUnMarkCell networkMessageMarkCell(targetPos, factionIndex); + sendMessage(&networkMessageMarkCell); + } + + void ClientInterface::sendPingMessage(int32 pingFrequency, int64 pingTime) { + NetworkMessagePing networkMessagePing(pingFrequency, pingTime); + sendMessage(&networkMessagePing); + } + + string ClientInterface::getNetworkStatus() { + std::string label = Lang::getInstance().getString("Server") + ": " + serverName; + //float pingTime = getThreadedPingMS(getServerIpAddress().c_str()); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "%s", label.c_str()); + + return szBuf; + } + + NetworkMessageType ClientInterface::waitForMessage(int waitMicroseconds) { + // Debug! + /* + sendTextMessage("Timeout waiting for message",-1); + DisplayErrorMessage("Timeout waiting for message"); + quit= true; + close(); + return; + */ + + uint64 loopCount = 0; + Chrono chronoPerf; + if (debugClientInterfacePerf == true) { chronoPerf.start(); } + + Chrono chrono; + chrono.start(); + + NetworkMessageType msg = nmtInvalid; + while (msg == nmtInvalid && + getQuitThread() == false) { + + msg = getNextMessageType(waitMicroseconds); + if (msg == nmtInvalid) { + if (getSocket() == NULL || (chrono.getMillis() % 250 == 0 && isConnected() == false)) { + if (getQuit() == false) { + //throw megaglest_runtime_error("Disconnected"); + //sendTextMessage("Server has Disconnected.",-1); + DisplayErrorMessage("Server has Disconnected."); + setQuit(true); + } + close(); + return msg; + } + + if (chrono.getMillis() > messageWaitTimeout) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + + string msg = "Timeout waiting for message."; + if (lang.hasString("TimeoutWaitingForMessage", languageList[langIndex]) == true) { + msg = lang.getString("TimeoutWaitingForMessage", languageList[langIndex]); + } + + sendTextMessage(msg, -1, lang.isLanguageLocal(languageList[langIndex]), languageList[langIndex]); + if (lang.isLanguageLocal(languageList[langIndex]) == true) { + DisplayErrorMessage(msg); + } + } + + sleep(1); + setQuit(true); + close(); + return msg; + } + // Sleep every x milli-seconds we wait to let other threads work + else if (chrono.getMillis() % 2 == 0) { + sleep(1); + } else { + sleep(0); + } + } + + if (debugClientInterfacePerf == true) { + loopCount++; + if (chronoPerf.getMillis() >= 100) { + printf("Client waitForMessage loopCount = %llu\n", (long long unsigned int)loopCount); + + loopCount = 0; + chronoPerf.start(); + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] waiting took %lld msecs, msg = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis(), msg); + + return msg; } - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 1) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] waiting took %lld msecs, msg = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis(),msg); + void ClientInterface::quitGame(bool userManuallyQuit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] userManuallyQuit = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, userManuallyQuit); - return msg; -} + if (clientSocket != NULL && userManuallyQuit == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void ClientInterface::quitGame(bool userManuallyQuit) -{ - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] userManuallyQuit = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,userManuallyQuit); + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - if(clientSocket != NULL && userManuallyQuit == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + string msg = "has chosen to leave the game!"; + if (lang.hasString("PlayerLeftGame", languageList[langIndex]) == true) { + msg = lang.getString("PlayerLeftGame", languageList[langIndex]); + } - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string msg = "has chosen to leave the game!"; - if(lang.hasString("PlayerLeftGame",languageList[langIndex]) == true) { - msg = lang.getString("PlayerLeftGame",languageList[langIndex]); - } - - sendTextMessage(msg,-1, lang.isLanguageLocal(languageList[langIndex]),languageList[langIndex]); - } - sleep(1); - close(); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ClientInterface::close(bool lockMutex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] START, clientSocket = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientSocket); - - MutexSafeWrapper safeMutex(NULL,CODE_AT_LINE); - if(lockMutex == true) { - safeMutex.setMutex(networkCommandListThreadAccessor,CODE_AT_LINE); - } - shutdownNetworkCommandListThread(safeMutex); - - delete clientSocket; - clientSocket = NULL; - - safeMutex.ReleaseLock(); - - connectedTime = 0; - gotIntro = false; - - MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); - this->joinGameInProgress = false; - this->joinGameInProgressLaunch = false; - this->readyForInGameJoin = false; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ClientInterface::close() { - close(true); -} - -void ClientInterface::discoverServers(DiscoveredServersInterface *cb) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - ClientSocket::discoverServers(cb); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} -void ClientInterface::stopServerDiscovery() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - ClientSocket::stopBroadCastClientThread(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, - int8 toSlotIndex,int8 toTeam, string networkPlayerName, - int8 networkPlayerStatus, int8 flags, - string language) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkPlayerName [%s] flags = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkPlayerName.c_str(),flags); - SwitchSetupRequest message = SwitchSetupRequest(selectedFactionName, - currentSlotIndex, toSlotIndex,toTeam,networkPlayerName, - networkPlayerStatus, flags,language); - sendMessage(&message); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -bool ClientInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMessageType) { - bool discard = false; - - switch(networkMessageType) { - case nmtIntro: - { - discard = true; - NetworkMessageIntro msg = NetworkMessageIntro(); - this->receiveMessage(&msg); + sendTextMessage(msg, -1, lang.isLanguageLocal(languageList[langIndex]), languageList[langIndex]); + } + sleep(1); + close(); } - break; - case nmtPing: - { - discard = true; - NetworkMessagePing msg = NetworkMessagePing(); - this->receiveMessage(&msg); - this->setLastPingInfo(msg); - } - break; - case nmtLaunch: - { - discard = true; - NetworkMessageLaunch msg = NetworkMessageLaunch(); - this->receiveMessage(&msg); - } - break; - case nmtText: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtText\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - discard = true; - NetworkMessageText netMsg = NetworkMessageText(); - this->receiveMessage(&netMsg); - ChatMsgInfo msg(netMsg.getText().c_str(),netMsg.getTeamIndex(),netMsg.getPlayerIndex(),netMsg.getTargetLanguage()); - this->addChatInfo(msg); - } - break; - - case nmtMarkCell: - { - discard = true; - NetworkMessageMarkCell networkMessageMarkCell; - receiveMessage(&networkMessageMarkCell); - - MarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); - this->addMarkedCell(msg); - } - break; - - case nmtUnMarkCell: - { - discard = true; - NetworkMessageUnMarkCell networkMessageMarkCell; - receiveMessage(&networkMessageMarkCell); - - UnMarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); - this->addUnMarkedCell(msg); - } - break; - - case nmtHighlightCell: - { - discard = true; - NetworkMessageHighlightCell networkMessageHighlightCell; - receiveMessage(&networkMessageHighlightCell); - - MarkedCell msg(networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex(), - "none",-1); - this->setHighlightedCell(msg); - } - break; - - case nmtSynchNetworkGameData: - { - discard = true; - NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData(); - this->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataStatus: - { - discard = true; - NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus(); - this->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataFileCRCCheck: - { - discard = true; - NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck(); - this->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataFileGet: - { - discard = true; - NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet(); - this->receiveMessage(&msg); - } - break; - case nmtSwitchSetupRequest: - { - discard = true; - SwitchSetupRequest msg = SwitchSetupRequest(); - this->receiveMessage(&msg); - } - break; - case nmtBroadCastSetup: - { - discard = true; - NetworkMessageLaunch msg = NetworkMessageLaunch(); - this->receiveMessage(&msg); - } - break; - - case nmtPlayerIndexMessage: - { - discard = true; - PlayerIndexMessage msg = PlayerIndexMessage(0); - this->receiveMessage(&msg); - } - break; - } - - return discard; -} - -string ClientInterface::getHumanPlayerName(int index) { - string result = Config::getInstance().getString("NetPlayerName",Socket::getHostName().c_str()); - - if(index >= 0 || gameSettings.getThisFactionIndex() >= 0) { - if(index < 0) { - index = gameSettings.getThisFactionIndex(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); } - if(gameSettings.getNetworkPlayerName(index) != "") { - result = gameSettings.getNetworkPlayerName(index); + + void ClientInterface::close(bool lockMutex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] START, clientSocket = %p\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientSocket); + + MutexSafeWrapper safeMutex(NULL, CODE_AT_LINE); + if (lockMutex == true) { + safeMutex.setMutex(networkCommandListThreadAccessor, CODE_AT_LINE); + } + shutdownNetworkCommandListThread(safeMutex); + + delete clientSocket; + clientSocket = NULL; + + safeMutex.ReleaseLock(); + + connectedTime = 0; + gotIntro = false; + + MutexSafeWrapper safeMutexFlags(flagAccessor, CODE_AT_LINE); + this->joinGameInProgress = false; + this->joinGameInProgressLaunch = false; + this->readyForInGameJoin = false; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] END\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); } + + void ClientInterface::close() { + close(true); + } + + void ClientInterface::discoverServers(DiscoveredServersInterface *cb) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + ClientSocket::discoverServers(cb); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + void ClientInterface::stopServerDiscovery() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + ClientSocket::stopBroadCastClientThread(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, + int8 toSlotIndex, int8 toTeam, string networkPlayerName, + int8 networkPlayerStatus, int8 flags, + string language) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] networkPlayerName [%s] flags = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkPlayerName.c_str(), flags); + SwitchSetupRequest message = SwitchSetupRequest(selectedFactionName, + currentSlotIndex, toSlotIndex, toTeam, networkPlayerName, + networkPlayerStatus, flags, language); + sendMessage(&message); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + bool ClientInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMessageType) { + bool discard = false; + + switch (networkMessageType) { + case nmtIntro: + { + discard = true; + NetworkMessageIntro msg = NetworkMessageIntro(); + this->receiveMessage(&msg); + } + break; + case nmtPing: + { + discard = true; + NetworkMessagePing msg = NetworkMessagePing(); + this->receiveMessage(&msg); + this->setLastPingInfo(msg); + } + break; + case nmtLaunch: + { + discard = true; + NetworkMessageLaunch msg = NetworkMessageLaunch(); + this->receiveMessage(&msg); + } + break; + case nmtText: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtText\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + discard = true; + NetworkMessageText netMsg = NetworkMessageText(); + this->receiveMessage(&netMsg); + + ChatMsgInfo msg(netMsg.getText().c_str(), netMsg.getTeamIndex(), netMsg.getPlayerIndex(), netMsg.getTargetLanguage()); + this->addChatInfo(msg); + } + break; + + case nmtMarkCell: + { + discard = true; + NetworkMessageMarkCell networkMessageMarkCell; + receiveMessage(&networkMessageMarkCell); + + MarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + this->addMarkedCell(msg); + } + break; + + case nmtUnMarkCell: + { + discard = true; + NetworkMessageUnMarkCell networkMessageMarkCell; + receiveMessage(&networkMessageMarkCell); + + UnMarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + this->addUnMarkedCell(msg); + } + break; + + case nmtHighlightCell: + { + discard = true; + NetworkMessageHighlightCell networkMessageHighlightCell; + receiveMessage(&networkMessageHighlightCell); + + MarkedCell msg(networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex(), + "none", -1); + this->setHighlightedCell(msg); + } + break; + + case nmtSynchNetworkGameData: + { + discard = true; + NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData(); + this->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataStatus: + { + discard = true; + NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus(); + this->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataFileCRCCheck: + { + discard = true; + NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck(); + this->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataFileGet: + { + discard = true; + NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet(); + this->receiveMessage(&msg); + } + break; + case nmtSwitchSetupRequest: + { + discard = true; + SwitchSetupRequest msg = SwitchSetupRequest(); + this->receiveMessage(&msg); + } + break; + case nmtBroadCastSetup: + { + discard = true; + NetworkMessageLaunch msg = NetworkMessageLaunch(); + this->receiveMessage(&msg); + } + break; + + case nmtPlayerIndexMessage: + { + discard = true; + PlayerIndexMessage msg = PlayerIndexMessage(0); + this->receiveMessage(&msg); + } + break; + } + + return discard; + } + + string ClientInterface::getHumanPlayerName(int index) { + string result = Config::getInstance().getString("NetPlayerName", Socket::getHostName().c_str()); + + if (index >= 0 || gameSettings.getThisFactionIndex() >= 0) { + if (index < 0) { + index = gameSettings.getThisFactionIndex(); + } + if (gameSettings.getNetworkPlayerName(index) != "") { + result = gameSettings.getNetworkPlayerName(index); + } + } + + return result; + } + + void ClientInterface::setGameSettings(GameSettings *serverGameSettings) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] START\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + gameSettings = *serverGameSettings; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + } + + void ClientInterface::broadcastGameSetup(const GameSettings *gameSettings) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + NetworkMessageLaunch networkMessageLaunch(gameSettings, nmtBroadCastSetup); + sendMessage(&networkMessageLaunch); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ClientInterface::broadcastGameStart(const GameSettings *gameSettings) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutexFlags(flagAccessor, CODE_AT_LINE); + if (this->joinGameInProgress == true) { + this->joinGameInProgressLaunch = true; + } + safeMutexFlags.ReleaseLock(); + + //printf("Sending game launch joinGameInProgress: %d\n",joinGameInProgress); + + NetworkMessageLaunch networkMessageLaunch(gameSettings, nmtLaunch); + sendMessage(&networkMessageLaunch); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ClientInterface::setGameSettingsReceived(bool value) { + //printf("In [%s:%s] Line: %d gameSettingsReceived = %d value = %d, gameSettingsReceivedCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameSettingsReceived,value,gameSettingsReceivedCount); + gameSettingsReceived = value; + gameSettingsReceivedCount++; + } + } - - return result; -} - -void ClientInterface::setGameSettings(GameSettings *serverGameSettings) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - gameSettings = *serverGameSettings; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); -} - -void ClientInterface::broadcastGameSetup(const GameSettings *gameSettings) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - NetworkMessageLaunch networkMessageLaunch(gameSettings, nmtBroadCastSetup); - sendMessage(&networkMessageLaunch); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ClientInterface::broadcastGameStart(const GameSettings *gameSettings) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutexFlags(flagAccessor,CODE_AT_LINE); - if(this->joinGameInProgress == true) { - this->joinGameInProgressLaunch = true; - } - safeMutexFlags.ReleaseLock(); - - //printf("Sending game launch joinGameInProgress: %d\n",joinGameInProgress); - - NetworkMessageLaunch networkMessageLaunch(gameSettings, nmtLaunch); - sendMessage(&networkMessageLaunch); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ClientInterface::setGameSettingsReceived(bool value) { - //printf("In [%s:%s] Line: %d gameSettingsReceived = %d value = %d, gameSettingsReceivedCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameSettingsReceived,value,gameSettingsReceivedCount); - gameSettingsReceived = value; - gameSettingsReceivedCount++; -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index 221ac3a8f..b5a5ce331 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -24,8 +24,8 @@ #define _GLEST_GAME_CLIENTINTERFACE_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -37,181 +37,216 @@ using Shared::Platform::Ip; using Shared::Platform::ClientSocket; using std::vector; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class ClientInterface; + class ClientInterface; -class ClientInterfaceThread : public BaseThread, public SlaveThreadControllerInterface { -protected: + class ClientInterfaceThread : public BaseThread, public SlaveThreadControllerInterface { + protected: - ClientInterface *clientInterface; + ClientInterface * clientInterface; - virtual void setQuitStatus(bool value); + virtual void setQuitStatus(bool value); -public: - explicit ClientInterfaceThread(ClientInterface *client); - virtual ~ClientInterfaceThread(); - virtual void execute(); + public: + explicit ClientInterfaceThread(ClientInterface *client); + virtual ~ClientInterfaceThread(); + virtual void execute(); - virtual void setMasterController(MasterSlaveThreadController *master) { } - virtual void signalSlave(void *userdata) { } + virtual void setMasterController(MasterSlaveThreadController *master) { + } + virtual void signalSlave(void *userdata) { + } - virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false); -}; + virtual bool canShutdown(bool deleteSelfIfShutdownDelayed = false); + }; -// ===================================================== -// class ClientInterface -// ===================================================== + // ===================================================== + // class ClientInterface + // ===================================================== -class ClientInterface: public GameNetworkInterface { -private: - static const int messageWaitTimeout; - static const int waitSleepTime; - static const int maxNetworkCommandListSendTimeWait; + class ClientInterface : public GameNetworkInterface { + private: + static const int messageWaitTimeout; + static const int waitSleepTime; + static const int maxNetworkCommandListSendTimeWait; -private: - ClientSocket *clientSocket; - string serverName; - bool introDone; - bool launchGame; - int playerIndex; - bool gameSettingsReceived; - int gameSettingsReceivedCount; - time_t connectedTime; - bool gotIntro; + private: + ClientSocket * clientSocket; + string serverName; + bool introDone; + bool launchGame; + int playerIndex; + bool gameSettingsReceived; + int gameSettingsReceivedCount; + time_t connectedTime; + bool gotIntro; - Ip ip; - int port; + Ip ip; + int port; - int currentFrameCount; - int lastSentFrameCount; - time_t lastNetworkCommandListSendTime; + int currentFrameCount; + int lastSentFrameCount; + time_t lastNetworkCommandListSendTime; - time_t clientSimulationLagStartTime; - string versionString; - int sessionKey; - int serverFTPPort; + time_t clientSimulationLagStartTime; + string versionString; + int sessionKey; + int serverFTPPort; - string serverUUID; - string serverPlatform; + string serverUUID; + string serverPlatform; - ClientInterfaceThread *networkCommandListThread; + ClientInterfaceThread *networkCommandListThread; - Mutex *networkCommandListThreadAccessor; - std::map cachedPendingCommands; //commands ready to be given - std::map > cachedPendingCommandCRCs; //commands ready to be given - uint64 cachedPendingCommandsIndex; - uint64 cachedLastPendingFrameCount; - int64 timeClientWaitedForLastMessage; + Mutex *networkCommandListThreadAccessor; + std::map cachedPendingCommands; //commands ready to be given + std::map > cachedPendingCommandCRCs; //commands ready to be given + uint64 cachedPendingCommandsIndex; + uint64 cachedLastPendingFrameCount; + int64 timeClientWaitedForLastMessage; - Mutex *flagAccessor; - bool joinGameInProgress; - bool joinGameInProgressLaunch; - bool readyForInGameJoin; - bool resumeInGameJoin; + Mutex *flagAccessor; + bool joinGameInProgress; + bool joinGameInProgressLaunch; + bool readyForInGameJoin; + bool resumeInGameJoin; - Mutex *quitThreadAccessor; - bool quitThread; + Mutex *quitThreadAccessor; + bool quitThread; - bool getQuitThread(); - void setQuitThread(bool value); - bool getQuit(); - void setQuit(bool value); + bool getQuitThread(); + void setQuitThread(bool value); + bool getQuit(); + void setQuit(bool value); -public: - ClientInterface(); - virtual ~ClientInterface(); + public: + ClientInterface(); + virtual ~ClientInterface(); - virtual std::string getIpAddress(bool mutexLock=true); - virtual Socket* getSocket(bool mutexLock=true) {return clientSocket;} - virtual void close(); + virtual std::string getIpAddress(bool mutexLock = true); + virtual Socket* getSocket(bool mutexLock = true) { + return clientSocket; + } + virtual void close(); - bool getJoinGameInProgress(); - bool getJoinGameInProgressLaunch(); + bool getJoinGameInProgress(); + bool getJoinGameInProgressLaunch(); - bool getReadyForInGameJoin(); + bool getReadyForInGameJoin(); - bool getResumeInGameJoin(); - void sendResumeGameMessage(); + bool getResumeInGameJoin(); + void sendResumeGameMessage(); - uint64 getCachedLastPendingFrameCount(); - int64 getTimeClientWaitedForLastMessage(); + uint64 getCachedLastPendingFrameCount(); + int64 getTimeClientWaitedForLastMessage(); - //message processing - virtual void update(); - virtual void updateLobby(); - virtual void updateKeyframe(int frameCount); - virtual void setKeyframe(int frameCount) { currentFrameCount = frameCount; } - virtual void waitUntilReady(Checksum* checksum); + //message processing + virtual void update(); + virtual void updateLobby(); + virtual void updateKeyframe(int frameCount); + virtual void setKeyframe(int frameCount) { + currentFrameCount = frameCount; + } + virtual void waitUntilReady(Checksum* checksum); - // message sending - virtual void sendTextMessage(const string &text, int teamIndex, bool echoLocal, - string targetLanguage); - virtual void quitGame(bool userManuallyQuit); + // message sending + virtual void sendTextMessage(const string &text, int teamIndex, bool echoLocal, + string targetLanguage); + virtual void quitGame(bool userManuallyQuit); - virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note,int playerIndex); - virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex); - virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex); - //misc - virtual string getNetworkStatus() ; + virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex); + virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex); + virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex); + //misc + virtual string getNetworkStatus(); - //accessors - string getServerName() const {return serverName;} - bool getLaunchGame() const {return launchGame;} - bool getIntroDone() const {return introDone;} - bool getGameSettingsReceived() const {return gameSettingsReceived;} - void setGameSettingsReceived(bool value); + //accessors + string getServerName() const { + return serverName; + } + bool getLaunchGame() const { + return launchGame; + } + bool getIntroDone() const { + return introDone; + } + bool getGameSettingsReceived() const { + return gameSettingsReceived; + } + void setGameSettingsReceived(bool value); - int getGameSettingsReceivedCount() const { return gameSettingsReceivedCount; } + int getGameSettingsReceivedCount() const { + return gameSettingsReceivedCount; + } - int getPlayerIndex() const {return playerIndex;} + int getPlayerIndex() const { + return playerIndex; + } - void connect(const Ip &ip, int port); - void reset(); + void connect(const Ip &ip, int port); + void reset(); - void discoverServers(DiscoveredServersInterface *cb); - void stopServerDiscovery(); + void discoverServers(DiscoveredServersInterface *cb); + void stopServerDiscovery(); - void sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, - int8 toSlotIndex, int8 toTeam,string networkPlayerName, - int8 networkPlayerStatus, int8 flags, - string language); - virtual bool getConnectHasHandshaked() const { return gotIntro; } - std::string getServerIpAddress(); + void sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, + int8 toSlotIndex, int8 toTeam, string networkPlayerName, + int8 networkPlayerStatus, int8 flags, + string language); + virtual bool getConnectHasHandshaked() const { + return gotIntro; + } + std::string getServerIpAddress(); - int getCurrentFrameCount() const { return currentFrameCount; } + int getCurrentFrameCount() const { + return currentFrameCount; + } - virtual void sendPingMessage(int32 pingFrequency, int64 pingTime); + virtual void sendPingMessage(int32 pingFrequency, int64 pingTime); - const string &getVersionString() const {return versionString;} - virtual string getHumanPlayerName(int index=-1); - virtual int getHumanPlayerIndex() const {return playerIndex;} - int getServerFTPPort() const { return serverFTPPort; } + const string &getVersionString() const { + return versionString; + } + virtual string getHumanPlayerName(int index = -1); + virtual int getHumanPlayerIndex() const { + return playerIndex; + } + int getServerFTPPort() const { + return serverFTPPort; + } - int getSessionKey() const { return sessionKey; } - bool isMasterServerAdminOverride(); + int getSessionKey() const { + return sessionKey; + } + bool isMasterServerAdminOverride(); - void setGameSettings(GameSettings *serverGameSettings); - void broadcastGameSetup(const GameSettings *gameSettings); - void broadcastGameStart(const GameSettings *gameSettings); + void setGameSettings(GameSettings *serverGameSettings); + void broadcastGameSetup(const GameSettings *gameSettings); + void broadcastGameStart(const GameSettings *gameSettings); - void updateNetworkFrame(); + void updateNetworkFrame(); - virtual void saveGame(XmlNode *rootNode) {}; + virtual void saveGame(XmlNode *rootNode) { + }; -protected: + protected: - Mutex * getServerSynchAccessor() { return NULL; } - NetworkMessageType waitForMessage(int waitMicroseconds=0); - bool shouldDiscardNetworkMessage(NetworkMessageType networkMessageType); + Mutex * getServerSynchAccessor() { + return NULL; + } + NetworkMessageType waitForMessage(int waitMicroseconds = 0); + bool shouldDiscardNetworkMessage(NetworkMessageType networkMessageType); - void updateFrame(int *checkFrame); - void shutdownNetworkCommandListThread(MutexSafeWrapper &safeMutexWrapper); - bool getNetworkCommand(int frameCount, int currentCachedPendingCommandsIndex); + void updateFrame(int *checkFrame); + void shutdownNetworkCommandListThread(MutexSafeWrapper &safeMutexWrapper); + bool getNetworkCommand(int frameCount, int currentCachedPendingCommandsIndex); - void close(bool lockMutex); -}; + void close(bool lockMutex); + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index dcaac4362..6b368739f 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -33,1630 +33,1586 @@ using namespace std; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class ConnectionSlotThread -// ===================================================== + // ===================================================== + // class ConnectionSlotThread + // ===================================================== -ConnectionSlotThread::ConnectionSlotThread(int slotIndex) : BaseThread() { - this->masterController = NULL; - this->triggerIdMutex = new Mutex(CODE_AT_LINE); - this->slotIndex = slotIndex; - this->slotInterface = NULL; - uniqueID = "ConnectionSlotThread"; - eventList.clear(); - eventList.reserve(1000); + ConnectionSlotThread::ConnectionSlotThread(int slotIndex) : BaseThread() { + this->masterController = NULL; + this->triggerIdMutex = new Mutex(CODE_AT_LINE); + this->slotIndex = slotIndex; + this->slotInterface = NULL; + uniqueID = "ConnectionSlotThread"; + eventList.clear(); + eventList.reserve(1000); - triggerGameStarted = new Mutex(CODE_AT_LINE); - gameStarted = false; -} - -ConnectionSlotThread::ConnectionSlotThread(ConnectionSlotCallbackInterface *slotInterface,int slotIndex) : BaseThread() { - this->masterController = NULL; - this->triggerIdMutex = new Mutex(CODE_AT_LINE); - this->slotIndex = slotIndex; - this->slotInterface = slotInterface; - uniqueID = "ConnectionSlotThread"; - eventList.clear(); - - triggerGameStarted = new Mutex(CODE_AT_LINE); - gameStarted = false; -} - -ConnectionSlotThread::~ConnectionSlotThread() { - delete triggerIdMutex; - triggerIdMutex = NULL; - - delete triggerGameStarted; - triggerGameStarted = NULL; -} - -void ConnectionSlotThread::setQuitStatus(bool value) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d value = %d\n",__FILE__,__FUNCTION__,__LINE__,value); - - BaseThread::setQuitStatus(value); - if(value == true) { - signalUpdate(NULL); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); -} - -void ConnectionSlotThread::signalUpdate(ConnectionSlotEvent *event) { - if(event != NULL) { - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - eventList.push_back(*event); - } - if(getGameStarted() == true && getQuitStatus() == true) { - return; - } - semTaskSignalled.signal(); -} - -void ConnectionSlotThread::setTaskCompleted(int eventId) { - if(eventId > 0) { - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - for(int index = 0; index < (int)eventList.size(); ++index) { - ConnectionSlotEvent &slotEvent = eventList[index]; - if(slotEvent.eventId == eventId) { - slotEvent.eventCompleted = true; - break; - } + triggerGameStarted = new Mutex(CODE_AT_LINE); + gameStarted = false; } - } -} -//void ConnectionSlotThread::purgeAllEvents() { -// MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); -// eventList.clear(); -//} + ConnectionSlotThread::ConnectionSlotThread(ConnectionSlotCallbackInterface *slotInterface, int slotIndex) : BaseThread() { + this->masterController = NULL; + this->triggerIdMutex = new Mutex(CODE_AT_LINE); + this->slotIndex = slotIndex; + this->slotInterface = slotInterface; + uniqueID = "ConnectionSlotThread"; + eventList.clear(); -void ConnectionSlotThread::setAllEventsCompleted() { - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - for(int index = 0; index < (int)eventList.size(); ++index) { - ConnectionSlotEvent &slotEvent = eventList[index]; - if(slotEvent.eventCompleted == false) { - slotEvent.eventCompleted = true; - } - } -} - -void ConnectionSlotThread::purgeCompletedEvents() { - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - for(int index = (int)eventList.size() - 1; index >= 0; index--) { - ConnectionSlotEvent &slotEvent = eventList[index]; - if(slotEvent.eventCompleted == true) { - eventList.erase(eventList.begin() + index); - } - } -} - -bool ConnectionSlotThread::canShutdown(bool deleteSelfIfShutdownDelayed) { - bool ret = (getExecutingTask() == false); - if(ret == false && deleteSelfIfShutdownDelayed == true) { - setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); - deleteSelfIfRequired(); - signalQuit(); - } - - return ret; -} - -bool ConnectionSlotThread::isSignalCompleted(ConnectionSlotEvent *event) { - bool result = false; - if(event != NULL) { - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - for(int index = 0; index < (int)eventList.size(); ++index) { - ConnectionSlotEvent &slotEvent = eventList[index]; - if(slotEvent.eventId == event->eventId) { - result = slotEvent.eventCompleted; - break; - } - } - } - return result; -} - -void ConnectionSlotThread::slotUpdateTask(ConnectionSlotEvent *event) { - if(event != NULL && event->connectionSlot != NULL) { - if(event->eventType == eSendSocketData) { - event->connectionSlot->sendMessage(event->networkMessage); + triggerGameStarted = new Mutex(CODE_AT_LINE); + gameStarted = false; } - else if(event->eventType == eReceiveSocketData) { - event->connectionSlot->updateSlot(event); + + ConnectionSlotThread::~ConnectionSlotThread() { + delete triggerIdMutex; + triggerIdMutex = NULL; + + delete triggerGameStarted; + triggerGameStarted = NULL; } - } -} -void ConnectionSlotThread::signalSlave(void *userdata) { - std::map *eventList = (std::map *)userdata; - ConnectionSlotEvent &event = (*eventList)[slotIndex]; - signalUpdate(&event); -} + void ConnectionSlotThread::setQuitStatus(bool value) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d value = %d\n", __FILE__, __FUNCTION__, __LINE__, value); -bool ConnectionSlotThread::getGameStarted() { - MutexSafeWrapper safeMutexGameStarted(triggerGameStarted,CODE_AT_LINE); - return gameStarted; -} -void ConnectionSlotThread::setGameStarted(bool value) { - MutexSafeWrapper safeMutexGameStarted(triggerGameStarted,CODE_AT_LINE); - if(gameStarted != value) { - gameStarted = value; + BaseThread::setQuitStatus(value); + if (value == true) { + signalUpdate(NULL); + } - if(gameStarted == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", __FILE__, __FUNCTION__, __LINE__); + } + + void ConnectionSlotThread::signalUpdate(ConnectionSlotEvent *event) { + if (event != NULL) { + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + eventList.push_back(*event); + } + if (getGameStarted() == true && getQuitStatus() == true) { + return; + } semTaskSignalled.signal(); } - } -} -void ConnectionSlotThread::execute() { - RunningStatusSafeWrapper runningStatus(this); - try { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("Starting client SLOT thread: %d\n",slotIndex); + void ConnectionSlotThread::setTaskCompleted(int eventId) { + if (eventId > 0) { + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + for (int index = 0; index < (int) eventList.size(); ++index) { + ConnectionSlotEvent &slotEvent = eventList[index]; + if (slotEvent.eventId == eventId) { + slotEvent.eventCompleted = true; + break; + } + } + } + } - for(;this->slotInterface != NULL;) { - if(getQuitStatus() == true) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; + //void ConnectionSlotThread::purgeAllEvents() { + // MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); + // eventList.clear(); + //} + + void ConnectionSlotThread::setAllEventsCompleted() { + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + for (int index = 0; index < (int) eventList.size(); ++index) { + ConnectionSlotEvent &slotEvent = eventList[index]; + if (slotEvent.eventCompleted == false) { + slotEvent.eventCompleted = true; + } + } + } + + void ConnectionSlotThread::purgeCompletedEvents() { + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + for (int index = (int) eventList.size() - 1; index >= 0; index--) { + ConnectionSlotEvent &slotEvent = eventList[index]; + if (slotEvent.eventCompleted == true) { + eventList.erase(eventList.begin() + index); + } + } + } + + bool ConnectionSlotThread::canShutdown(bool deleteSelfIfShutdownDelayed) { + bool ret = (getExecutingTask() == false); + if (ret == false && deleteSelfIfShutdownDelayed == true) { + setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); + deleteSelfIfRequired(); + signalQuit(); } - // Does this game allow joining in progress play and is this slot - // not already connected to a client? - if( this->slotInterface->getAllowInGameConnections() == true && - this->slotInterface->isClientConnected(slotIndex) == false) { - //printf("#1 Non connected slot: %d waiting for client connection..\n",slotIndex); - sleep(100); + return ret; + } - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; + bool ConnectionSlotThread::isSignalCompleted(ConnectionSlotEvent *event) { + bool result = false; + if (event != NULL) { + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + for (int index = 0; index < (int) eventList.size(); ++index) { + ConnectionSlotEvent &slotEvent = eventList[index]; + if (slotEvent.eventId == event->eventId) { + result = slotEvent.eventCompleted; + break; + } } - - //printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount); - - ConnectionSlotEvent eventCopy; - eventCopy.eventType = eReceiveSocketData; - eventCopy.connectionSlot = this->slotInterface->getSlot(slotIndex,true); - - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); - this->slotUpdateTask(&eventCopy); } - else { - // If the game already started? - if(getGameStarted() == true) { - //printf("#A Checking action for slot: %d\n",slotIndex); + return result; + } - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); - - // If the slot or socket are NULL the connection was lost - // so exit the thread - MutexSafeWrapper safeMutex(this->slotInterface->getSlotMutex(slotIndex),CODE_AT_LINE); - ConnectionSlot *slot = this->slotInterface->getSlot(slotIndex,false); - if(slot == NULL) { - break; - } - Socket *socket = slot->getSocket(true); - if(socket == NULL) { - break; - } - - PLATFORM_SOCKET socketId = socket->getSocketId(); - safeMutex.ReleaseLock(); - - // Avoid mutex locking - //bool socketHasReadData = Socket::hasDataToRead(socket->getSocketId()); - bool socketHasReadData = Socket::hasDataToReadWithWait(socketId,150000); - - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - ConnectionSlotEvent eventCopy; - eventCopy.eventType = eReceiveSocketData; - eventCopy.connectionSlot = this->slotInterface->getSlot(slotIndex,true); - eventCopy.eventId = slotIndex; - eventCopy.socketTriggered = socketHasReadData; - - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - this->slotUpdateTask(&eventCopy); + void ConnectionSlotThread::slotUpdateTask(ConnectionSlotEvent *event) { + if (event != NULL && event->connectionSlot != NULL) { + if (event->eventType == eSendSocketData) { + event->connectionSlot->sendMessage(event->networkMessage); + } else if (event->eventType == eReceiveSocketData) { + event->connectionSlot->updateSlot(event); } - // Game has not yet started - else { - //printf("#1 Checking action for slot: %d\n",slotIndex); + } + } - if(getGameStarted() == true) { - continue; - } - //printf("#2 Checking action for slot: %d\n",slotIndex); + void ConnectionSlotThread::signalSlave(void *userdata) { + std::map *eventList = (std::map *)userdata; + ConnectionSlotEvent &event = (*eventList)[slotIndex]; + signalUpdate(&event); + } - semTaskSignalled.waitTillSignalled(); - //printf("#3 Checking action for slot: %d\n",slotIndex); + bool ConnectionSlotThread::getGameStarted() { + MutexSafeWrapper safeMutexGameStarted(triggerGameStarted, CODE_AT_LINE); + return gameStarted; + } + void ConnectionSlotThread::setGameStarted(bool value) { + MutexSafeWrapper safeMutexGameStarted(triggerGameStarted, CODE_AT_LINE); + if (gameStarted != value) { + gameStarted = value; - if(getGameStarted() == true) { - continue; - } - //printf("#4 Checking action for slot: %d\n",slotIndex); + if (gameStarted == true) { + semTaskSignalled.signal(); + } + } + } - static string masterSlaveOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MasterSlaveThreadControllerSafeWrapper safeMasterController(masterController,20000,masterSlaveOwnerId); - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + void ConnectionSlotThread::execute() { + RunningStatusSafeWrapper runningStatus(this); + try { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + //printf("Starting client SLOT thread: %d\n",slotIndex); + + for (; this->slotInterface != NULL;) { + if (getQuitStatus() == true) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); break; } - MutexSafeWrapper safeMutex(triggerIdMutex,CODE_AT_LINE); - int eventCount = (int)eventList.size(); + // Does this game allow joining in progress play and is this slot + // not already connected to a client? + if (this->slotInterface->getAllowInGameConnections() == true && + this->slotInterface->isClientConnected(slotIndex) == false) { + //printf("#1 Non connected slot: %d waiting for client connection..\n",slotIndex); + sleep(100); - //printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,eventCount); + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + //printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount); - if(eventCount > 0) { ConnectionSlotEvent eventCopy; - for(int i = 0; i < (int)eventList.size(); ++i) { - ConnectionSlotEvent &slotEvent = eventList[i]; - if(slotEvent.eventCompleted == false) { - eventCopy = slotEvent; + eventCopy.eventType = eReceiveSocketData; + eventCopy.connectionSlot = this->slotInterface->getSlot(slotIndex, true); + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + this->slotUpdateTask(&eventCopy); + } else { + // If the game already started? + if (getGameStarted() == true) { + //printf("#A Checking action for slot: %d\n",slotIndex); + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); break; } - } - safeMutex.ReleaseLock(); - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } - - if(eventCopy.eventId > 0) { ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",__FILE__,__FUNCTION__,__LINE__,slotIndex,eventCount,(int)eventCopy.eventId); - //printf("#1 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId); + // If the slot or socket are NULL the connection was lost + // so exit the thread + MutexSafeWrapper safeMutex(this->slotInterface->getSlotMutex(slotIndex), CODE_AT_LINE); + ConnectionSlot *slot = this->slotInterface->getSlot(slotIndex, false); + if (slot == NULL) { + break; + } + Socket *socket = slot->getSocket(true); + if (socket == NULL) { + break; + } + + PLATFORM_SOCKET socketId = socket->getSocketId(); + safeMutex.ReleaseLock(); + + // Avoid mutex locking + //bool socketHasReadData = Socket::hasDataToRead(socket->getSocketId()); + bool socketHasReadData = Socket::hasDataToReadWithWait(socketId, 150000); + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + ConnectionSlotEvent eventCopy; + eventCopy.eventType = eReceiveSocketData; + eventCopy.connectionSlot = this->slotInterface->getSlot(slotIndex, true); + eventCopy.eventId = slotIndex; + eventCopy.socketTriggered = socketHasReadData; + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } this->slotUpdateTask(&eventCopy); - setTaskCompleted(eventCopy.eventId); + } + // Game has not yet started + else { + //printf("#1 Checking action for slot: %d\n",slotIndex); - //printf("#2 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId); + if (getGameStarted() == true) { + continue; + } + //printf("#2 Checking action for slot: %d\n",slotIndex); + + semTaskSignalled.waitTillSignalled(); + //printf("#3 Checking action for slot: %d\n",slotIndex); + + if (getGameStarted() == true) { + continue; + } + //printf("#4 Checking action for slot: %d\n",slotIndex); + + static string masterSlaveOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MasterSlaveThreadControllerSafeWrapper safeMasterController(masterController, 20000, masterSlaveOwnerId); + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + MutexSafeWrapper safeMutex(triggerIdMutex, CODE_AT_LINE); + int eventCount = (int) eventList.size(); + + //printf("Slot thread slotIndex: %d eventCount: %d\n",slotIndex,eventCount); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d\n", __FILE__, __FUNCTION__, __LINE__, slotIndex, eventCount); + + if (eventCount > 0) { + ConnectionSlotEvent eventCopy; + for (int i = 0; i < (int) eventList.size(); ++i) { + ConnectionSlotEvent &slotEvent = eventList[i]; + if (slotEvent.eventCompleted == false) { + eventCopy = slotEvent; + break; + } + } + safeMutex.ReleaseLock(); + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } + + if (eventCopy.eventId > 0) { + ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n", __FILE__, __FUNCTION__, __LINE__, slotIndex, eventCount, (int) eventCopy.eventId); + //printf("#1 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId); + + this->slotUpdateTask(&eventCopy); + setTaskCompleted(eventCopy.eventId); + + //printf("#2 Slot thread slotIndex: %d eventCount: %d eventCopy.eventId: %d\n",slotIndex,eventCount,(int)eventCopy.eventId); + } + } } } + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + break; + } } + + //printf("Ending client SLOT thread: %d\n",slotIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } catch (const exception &ex) { + + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + throw megaglest_runtime_error(ex.what()); } - - if(getQuitStatus() == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - break; - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", __FILE__, __FUNCTION__, __LINE__); } - //printf("Ending client SLOT thread: %d\n",slotIndex); + // ===================================================== + // class ConnectionSlot + // ===================================================== - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - catch(const exception &ex) { + ConnectionSlot::ConnectionSlot(ServerInterface* serverInterface, int playerIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", __FILE__, __FUNCTION__, __LINE__); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + this->mutexSocket = new Mutex(CODE_AT_LINE); + this->socket = NULL; + this->mutexCloseConnection = new Mutex(CODE_AT_LINE); + this->mutexPendingNetworkCommandList = new Mutex(CODE_AT_LINE); + this->socketSynchAccessor = new Mutex(CODE_AT_LINE); + this->connectedRemoteIPAddress = 0; + this->sessionKey = 0; + this->serverInterface = serverInterface; + this->playerIndex = playerIndex; + this->playerStatus = npst_None; + this->playerLanguage = ""; + this->playerUUID = ""; + this->platform = ""; + this->currentFrameCount = 0; + this->currentLagCount = 0; + this->graceLagCtr = 0; + this->gotLagCountWarning = false; + this->lastReceiveCommandListTime = 0; + this->receivedNetworkGameStatus = false; - throw megaglest_runtime_error(ex.what()); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); -} + this->autoPauseGameCountForLag = 0; + this->skipLagCheck = false; + this->joinGameInProgress = false; + this->canAcceptConnections = true; + this->startInGameConnectionLaunch = false; + this->pauseForInGameConnection = false; + this->unPauseForInGameConnection = false; + this->sentSavedGameInfo = false; -// ===================================================== -// class ConnectionSlot -// ===================================================== + this->ready = false; + this->gotIntro = false; + this->connectedTime = 0; -ConnectionSlot::ConnectionSlot(ServerInterface* serverInterface, int playerIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__); - - this->mutexSocket = new Mutex(CODE_AT_LINE); - this->socket = NULL; - this->mutexCloseConnection = new Mutex(CODE_AT_LINE); - this->mutexPendingNetworkCommandList = new Mutex(CODE_AT_LINE); - this->socketSynchAccessor = new Mutex(CODE_AT_LINE); - this->connectedRemoteIPAddress = 0; - this->sessionKey = 0; - this->serverInterface = serverInterface; - this->playerIndex = playerIndex; - this->playerStatus = npst_None; - this->playerLanguage = ""; - this->playerUUID = ""; - this->platform = ""; - this->currentFrameCount = 0; - this->currentLagCount = 0; - this->graceLagCtr = 0; - this->gotLagCountWarning = false; - this->lastReceiveCommandListTime = 0; - this->receivedNetworkGameStatus = false; - - this->autoPauseGameCountForLag = 0; - this->skipLagCheck = false; - this->joinGameInProgress = false; - this->canAcceptConnections = true; - this->startInGameConnectionLaunch = false; - this->pauseForInGameConnection = false; - this->unPauseForInGameConnection = false; - this->sentSavedGameInfo = false; - - this->ready = false; - this->gotIntro = false; - this->connectedTime = 0; - - this->networkGameDataSynchCheckOkMap = false; - this->networkGameDataSynchCheckOkTile = false; - this->networkGameDataSynchCheckOkTech = false; - this->setNetworkGameDataSynchCheckTechMismatchReport(""); - this->setReceivedDataSynchCheck(false); - - this->clearChatInfo(); - - this->setSocket(NULL); - this->slotThreadWorker = NULL; - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - this->slotThreadWorker = new ConnectionSlotThread(this->serverInterface,playerIndex); - this->slotThreadWorker->setUniqueID(mutexOwnerId); - this->slotThreadWorker->start(); -} - -ConnectionSlot::~ConnectionSlot() { - //printf("===> Destructor for ConnectionSlot = %d\n",playerIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] START\n",__FILE__,__FUNCTION__,__LINE__); - - //printf("Deleting connection slot\n"); - close(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - //printf("#1 Ending client SLOT: %d slotThreadWorker: %p\n",playerIndex,slotThreadWorker); - if(slotThreadWorker != NULL) { - slotThreadWorker->signalQuit(); - } - if( slotThreadWorker != NULL && - slotThreadWorker->canShutdown(false) == true && - slotThreadWorker->getRunningStatus() == false) { - //printf("#2 Ending client SLOT: %d\n",playerIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - delete slotThreadWorker; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - else if(slotThreadWorker != NULL && - slotThreadWorker->canShutdown(true) == true) { - - if(slotThreadWorker->getRunningStatus() == false) { - //printf("#3 Ending client SLOT: %d\n",playerIndex); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - delete slotThreadWorker; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - else { - slotThreadWorker->setDeleteSelfOnExecutionDone(true); - slotThreadWorker->setDeleteAfterExecute(true); - } - } - //printf("#4 Ending client SLOT: %d\n",playerIndex); - slotThreadWorker = NULL; - - delete socketSynchAccessor; - socketSynchAccessor = NULL; - - delete mutexPendingNetworkCommandList; - mutexPendingNetworkCommandList = NULL; - - delete mutexCloseConnection; - mutexCloseConnection = NULL; - - delete mutexSocket; - mutexSocket = NULL; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); -} - -int ConnectionSlot::getAutoPauseGameCountForLag() { - return autoPauseGameCountForLag; -} - -void ConnectionSlot::incrementAutoPauseGameCountForLag() { - autoPauseGameCountForLag++; -} - -bool ConnectionSlot::getGameStarted() { - bool result = false; - if(this->slotThreadWorker != NULL) { - result = this->slotThreadWorker->getGameStarted(); - } - return result; -} -void ConnectionSlot::setGameStarted(bool value) { - if(this->slotThreadWorker != NULL) { - this->slotThreadWorker->setGameStarted(value); - } -} - -void ConnectionSlot::setPlayerIndex(int value) { - playerIndex = value; - - if(this->slotThreadWorker != NULL) { - this->slotThreadWorker->setSlotIndex(playerIndex); - } -} - -void ConnectionSlot::setReady() { - this->ready= true; - this->skipLagCheck = false; - this->joinGameInProgress = false; - this->sentSavedGameInfo = false; -} - -void ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { - if(event != NULL) { - bool &socketTriggered = event->socketTriggered; - bool checkForNewClients = - (serverInterface->getGameHasBeenInitiated() == false || - serverInterface->getAllowInGameConnections() == true); - - //if((serverInterface->getGameHasBeenInitiated() == false || - // (serverInterface->getAllowInGameConnections() == true && this->isConnected() == false) || - // socketTriggered == true)) { - if(socketTriggered == true || - ((serverInterface->getGameHasBeenInitiated() == false || - serverInterface->getAllowInGameConnections() == true) && - this->isConnected() == false)) { - - this->update(checkForNewClients,event->triggerId); - } - //} - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - -string ConnectionSlot::getIpAddress(bool mutexLock) { - string result = ""; - MutexSafeWrapper safeMutexSlot((mutexLock == true ? mutexSocket : NULL),CODE_AT_LINE); - if(socket != NULL) { - result = socket->getIpAddress(); - } - return result; -} - -void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { - try { - clearThreadErrorList(); - - if(slotThreadWorker != NULL) { - slotThreadWorker->purgeCompletedEvents(); - } - - pair socketInfo = this->getSocketInfo(); - if(socketInfo.second == NULL) { - if(networkGameDataSynchCheckOkMap) networkGameDataSynchCheckOkMap = false; - if(networkGameDataSynchCheckOkTile) networkGameDataSynchCheckOkTile = false; - if(networkGameDataSynchCheckOkTech) networkGameDataSynchCheckOkTech = false; + this->networkGameDataSynchCheckOkMap = false; + this->networkGameDataSynchCheckOkTile = false; + this->networkGameDataSynchCheckOkTech = false; + this->setNetworkGameDataSynchCheckTechMismatchReport(""); this->setReceivedDataSynchCheck(false); - // Is the listener socket ready to be read? - if(checkForNewClients == true && this->canAcceptConnections == true) { + this->clearChatInfo(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] BEFORE accept new client connection, serverInterface->getOpenSlotCount() = %d\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getOpenSlotCount()); + this->setSocket(NULL); + this->slotThreadWorker = NULL; + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + this->slotThreadWorker = new ConnectionSlotThread(this->serverInterface, playerIndex); + this->slotThreadWorker->setUniqueID(mutexOwnerId); + this->slotThreadWorker->start(); + } - //printf("Checking for new connections...\n"); - bool hasData = (serverInterface->getServerSocket() != NULL && - serverInterface->getServerSocket()->hasDataToRead() == true); - //printf("Server socket hasData: %d\n",hasData); + ConnectionSlot::~ConnectionSlot() { + //printf("===> Destructor for ConnectionSlot = %d\n",playerIndex); - if(hasData == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to accept new client connection playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] START\n", __FILE__, __FUNCTION__, __LINE__); - Socket *newSocket = serverInterface->getServerSocket()->accept(false); + //printf("Deleting connection slot\n"); + close(); - //printf("Server socket newSocket: %p\n",newSocket); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] called accept new client connection playerIndex = %d newSocket = %p\n",__FILE__,__FUNCTION__,__LINE__,playerIndex,newSocket); - if(newSocket != NULL) { - // Set Socket as non-blocking - newSocket->setBlock(false); + //printf("#1 Ending client SLOT: %d slotThreadWorker: %p\n",playerIndex,slotThreadWorker); + if (slotThreadWorker != NULL) { + slotThreadWorker->signalQuit(); + } + if (slotThreadWorker != NULL && + slotThreadWorker->canShutdown(false) == true && + slotThreadWorker->getRunningStatus() == false) { + //printf("#2 Ending client SLOT: %d\n",playerIndex); - MutexSafeWrapper safeMutex(mutexCloseConnection,CODE_AT_LINE); - this->setSocket(newSocket); - safeMutex.ReleaseLock(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - this->connectedTime = time(NULL); - this->clearChatInfo(); - this->name = ""; - this->playerStatus = npst_PickSettings; - this->playerLanguage = ""; - this->playerUUID = ""; - this->platform = ""; - this->ready = false; - this->vctFileList.clear(); - this->receivedNetworkGameStatus = false; - this->gotIntro = false; + delete slotThreadWorker; - MutexSafeWrapper safeMutexSlot1(mutexPendingNetworkCommandList,CODE_AT_LINE); - this->vctPendingNetworkCommandList.clear(); - safeMutexSlot1.ReleaseLock(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } else if (slotThreadWorker != NULL && + slotThreadWorker->canShutdown(true) == true) { - this->currentFrameCount = 0; - this->currentLagCount = 0; - this->lastReceiveCommandListTime = 0; - this->gotLagCountWarning = false; - this->versionString = ""; + if (slotThreadWorker->getRunningStatus() == false) { + //printf("#3 Ending client SLOT: %d\n",playerIndex); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - serverInterface->updateListen(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex); - } - else { - close(); - return; - } + delete slotThreadWorker; - //send intro message when connected - if(this->isConnected() == true) { - //printf("Server socket newSocket is connected: %d\n",playerIndex); - - Chrono seed(true); - srand((unsigned int)seed.getCurTicks() / (this->playerIndex + 1)); - - sessionKey = rand() % 1000000; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getOpenSlotCount(),sessionKey); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] client will be assigned to the next open slot\n",__FILE__,__FUNCTION__,__LINE__); - - NetworkMessageIntro networkMessageIntro( - sessionKey, - getNetworkVersionGITString(), - getHostName(), - playerIndex, - nmgstOk, - 0, - ServerSocket::getFTPServerPort(), - "", - serverInterface->getGameHasBeenInitiated(), - Config::getInstance().getString("PlayerId",""), - getPlatformNameString()); - sendMessage(&networkMessageIntro); - - if(this->serverInterface->getGameHasBeenInitiated() == true) { - setGameStarted(true); - } - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } else { + slotThreadWorker->setDeleteSelfOnExecutionDone(true); + slotThreadWorker->setDeleteAfterExecute(true); } } + //printf("#4 Ending client SLOT: %d\n",playerIndex); + slotThreadWorker = NULL; + + delete socketSynchAccessor; + socketSynchAccessor = NULL; + + delete mutexPendingNetworkCommandList; + mutexPendingNetworkCommandList = NULL; + + delete mutexCloseConnection; + mutexCloseConnection = NULL; + + delete mutexSocket; + mutexSocket = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END\n", __FILE__, __FUNCTION__); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(socketInfo.first == true) { - this->clearChatInfo(); + int ConnectionSlot::getAutoPauseGameCountForLag() { + return autoPauseGameCountForLag; + } - bool gotTextMsg = true; - bool gotCellMarkerMsg = true; - bool waitForLaggingClient = false; - bool waitedForLaggingClient = false; + void ConnectionSlot::incrementAutoPauseGameCountForLag() { + autoPauseGameCountForLag++; + } - //printf("Update slot: %d this->hasDataToRead(): %d\n",this->playerIndex,this->hasDataToRead()); + bool ConnectionSlot::getGameStarted() { + bool result = false; + if (this->slotThreadWorker != NULL) { + result = this->slotThreadWorker->getGameStarted(); + } + return result; + } + void ConnectionSlot::setGameStarted(bool value) { + if (this->slotThreadWorker != NULL) { + this->slotThreadWorker->setGameStarted(value); + } + } - for(;waitForLaggingClient == true || - (this->hasDataToRead() == true && - (gotTextMsg == true || gotCellMarkerMsg == true));) { + void ConnectionSlot::setPlayerIndex(int value) { + playerIndex = value; - //printf("Server slot checking for waitForLaggingClient = %d this->hasDataToRead() = %d gotTextMsg = %d gotCellMarkerMsg = %d\n",waitForLaggingClient,this->hasDataToRead(),gotTextMsg,gotCellMarkerMsg); + if (this->slotThreadWorker != NULL) { + this->slotThreadWorker->setSlotIndex(playerIndex); + } + } - waitForLaggingClient = false; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] polling for networkMessageType...\n",__FILE__,__FUNCTION__,__LINE__); + void ConnectionSlot::setReady() { + this->ready = true; + this->skipLagCheck = false; + this->joinGameInProgress = false; + this->sentSavedGameInfo = false; + } - NetworkMessageType networkMessageType= getNextMessageType(); + void ConnectionSlot::updateSlot(ConnectionSlotEvent *event) { + if (event != NULL) { + bool &socketTriggered = event->socketTriggered; + bool checkForNewClients = + (serverInterface->getGameHasBeenInitiated() == false || + serverInterface->getAllowInGameConnections() == true); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkMessageType = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageType); + //if((serverInterface->getGameHasBeenInitiated() == false || + // (serverInterface->getAllowInGameConnections() == true && this->isConnected() == false) || + // socketTriggered == true)) { + if (socketTriggered == true || + ((serverInterface->getGameHasBeenInitiated() == false || + serverInterface->getAllowInGameConnections() == true) && + this->isConnected() == false)) { - gotTextMsg = false; - gotCellMarkerMsg = false; - //process incoming commands - switch(networkMessageType) { + this->update(checkForNewClients, event->triggerId); + } + //} + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } - case nmtInvalid: - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtInvalid\n",__FILE__,__FUNCTION__,__LINE__); - break; + string ConnectionSlot::getIpAddress(bool mutexLock) { + string result = ""; + MutexSafeWrapper safeMutexSlot((mutexLock == true ? mutexSocket : NULL), CODE_AT_LINE); + if (socket != NULL) { + result = socket->getIpAddress(); + } + return result; + } - case nmtPing: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtPing\n",__FILE__,__FUNCTION__); + void ConnectionSlot::update(bool checkForNewClients, int lockedSlotIndex) { + try { + clearThreadErrorList(); - // client REQUIRES a ping before completing intro - // authentication - NetworkMessagePing networkMessagePing; - if(receiveMessage(&networkMessagePing)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - lastPingInfo = networkMessagePing; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + if (slotThreadWorker != NULL) { + slotThreadWorker->purgeCompletedEvents(); + } + + pair socketInfo = this->getSocketInfo(); + if (socketInfo.second == NULL) { + if (networkGameDataSynchCheckOkMap) networkGameDataSynchCheckOkMap = false; + if (networkGameDataSynchCheckOkTile) networkGameDataSynchCheckOkTile = false; + if (networkGameDataSynchCheckOkTech) networkGameDataSynchCheckOkTech = false; + this->setReceivedDataSynchCheck(false); + + // Is the listener socket ready to be read? + if (checkForNewClients == true && this->canAcceptConnections == true) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] BEFORE accept new client connection, serverInterface->getOpenSlotCount() = %d\n", __FILE__, __FUNCTION__, __LINE__, serverInterface->getOpenSlotCount()); + + //printf("Checking for new connections...\n"); + bool hasData = (serverInterface->getServerSocket() != NULL && + serverInterface->getServerSocket()->hasDataToRead() == true); + //printf("Server socket hasData: %d\n",hasData); + + if (hasData == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] about to accept new client connection playerIndex = %d\n", __FILE__, __FUNCTION__, __LINE__, playerIndex); + + Socket *newSocket = serverInterface->getServerSocket()->accept(false); + + //printf("Server socket newSocket: %p\n",newSocket); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] called accept new client connection playerIndex = %d newSocket = %p\n", __FILE__, __FUNCTION__, __LINE__, playerIndex, newSocket); + if (newSocket != NULL) { + // Set Socket as non-blocking + newSocket->setBlock(false); + + MutexSafeWrapper safeMutex(mutexCloseConnection, CODE_AT_LINE); + this->setSocket(newSocket); + safeMutex.ReleaseLock(); + + this->connectedTime = time(NULL); + this->clearChatInfo(); + this->name = ""; + this->playerStatus = npst_PickSettings; + this->playerLanguage = ""; + this->playerUUID = ""; + this->platform = ""; + this->ready = false; + this->vctFileList.clear(); + this->receivedNetworkGameStatus = false; + this->gotIntro = false; + + MutexSafeWrapper safeMutexSlot1(mutexPendingNetworkCommandList, CODE_AT_LINE); + this->vctPendingNetworkCommandList.clear(); + safeMutexSlot1.ReleaseLock(); + + this->currentFrameCount = 0; + this->currentLagCount = 0; + this->lastReceiveCommandListTime = 0; + this->gotLagCountWarning = false; + this->versionString = ""; + + serverInterface->updateListen(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d\n", __FILE__, __FUNCTION__, __LINE__, playerIndex); + } else { close(); return; } - } - break; - case nmtText: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtText gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + //send intro message when connected + if (this->isConnected() == true) { + //printf("Server socket newSocket is connected: %d\n",playerIndex); - if(gotIntro == true) { - NetworkMessageText networkMessageText; - if(receiveMessage(&networkMessageText)) { - ChatMsgInfo msg(networkMessageText.getText().c_str(),networkMessageText.getTeamIndex(),networkMessageText.getPlayerIndex(),networkMessageText.getTargetLanguage()); - this->addChatInfo(msg); - gotTextMsg = true; + Chrono seed(true); + srand((unsigned int) seed.getCurTicks() / (this->playerIndex + 1)); + + sessionKey = rand() % 1000000; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] accepted new client connection, serverInterface->getOpenSlotCount() = %d, sessionKey = %d\n", __FILE__, __FUNCTION__, __LINE__, serverInterface->getOpenSlotCount(), sessionKey); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] client will be assigned to the next open slot\n", __FILE__, __FUNCTION__, __LINE__); + + NetworkMessageIntro networkMessageIntro( + sessionKey, + getNetworkVersionGITString(), + getHostName(), + playerIndex, + nmgstOk, + 0, + ServerSocket::getFTPServerPort(), + "", + serverInterface->getGameHasBeenInitiated(), + Config::getInstance().getString("PlayerId", ""), + getPlatformNameString()); + sendMessage(&networkMessageIntro); + + if (this->serverInterface->getGameHasBeenInitiated() == true) { + setGameStarted(true); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; } } - break; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - case nmtMarkCell: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + if (socketInfo.first == true) { + this->clearChatInfo(); - if(gotIntro == true) { - NetworkMessageMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - MarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); + bool gotTextMsg = true; + bool gotCellMarkerMsg = true; + bool waitForLaggingClient = false; + bool waitedForLaggingClient = false; - this->addMarkedCell(msg); - gotCellMarkerMsg = true; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + //printf("Update slot: %d this->hasDataToRead(): %d\n",this->playerIndex,this->hasDataToRead()); - case nmtUnMarkCell: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtUnMarkCell gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + for (; waitForLaggingClient == true || + (this->hasDataToRead() == true && + (gotTextMsg == true || gotCellMarkerMsg == true));) { - if(gotIntro == true) { - NetworkMessageUnMarkCell networkMessageMarkCell; - if(receiveMessage(&networkMessageMarkCell)) { - UnMarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); + //printf("Server slot checking for waitForLaggingClient = %d this->hasDataToRead() = %d gotTextMsg = %d gotCellMarkerMsg = %d\n",waitForLaggingClient,this->hasDataToRead(),gotTextMsg,gotCellMarkerMsg); - this->addUnMarkedCell(msg); - gotCellMarkerMsg = true; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + waitForLaggingClient = false; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] polling for networkMessageType...\n", __FILE__, __FUNCTION__, __LINE__); - case nmtHighlightCell: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + NetworkMessageType networkMessageType = getNextMessageType(); - if(gotIntro == true) { - NetworkMessageHighlightCell networkMessageHighlightCell; - if(receiveMessage(&networkMessageHighlightCell)) { - MarkedCell msg(networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex(),"none",-1); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] networkMessageType = %d\n", __FILE__, __FUNCTION__, __LINE__, networkMessageType); - this->setHighlightedCell(msg); - gotCellMarkerMsg = true; - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + gotTextMsg = false; + gotCellMarkerMsg = false; + //process incoming commands + switch (networkMessageType) { - //command list - case nmtCommandList: { + case nmtInvalid: + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtInvalid\n", __FILE__, __FUNCTION__, __LINE__); + break; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtCommandList gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + case nmtPing: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtPing\n", __FILE__, __FUNCTION__); - if(gotIntro == true) { - NetworkMessageCommandList networkMessageCommandList; - if(receiveMessage(&networkMessageCommandList)) { - currentFrameCount = networkMessageCommandList.getFrameCount(); - lastReceiveCommandListTime = time(NULL); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] currentFrameCount = %d\n",__FILE__,__FUNCTION__,__LINE__,currentFrameCount); - - MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList,CODE_AT_LINE); - for(int i = 0; i < networkMessageCommandList.getCommandCount(); ++i) { - vctPendingNetworkCommandList.push_back(*networkMessageCommandList.getCommand(i)); + // client REQUIRES a ping before completing intro + // authentication + NetworkMessagePing networkMessagePing; + if (receiveMessage(&networkMessagePing)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + lastPingInfo = networkMessagePing; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - //printf("Got commands from client frame: %d count: %d\n",currentFrameCount,vctPendingNetworkCommandList.size()); - - //printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + break; - //process intro messages - case nmtIntro: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtIntro\n",__FILE__,__FUNCTION__); + case nmtText: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtText gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); - NetworkMessageIntro networkMessageIntro; - if(receiveMessage(&networkMessageIntro)) { - int32 msgSessionId = networkMessageIntro.getSessionId(); - this->name= networkMessageIntro.getName(); - this->versionString = networkMessageIntro.getVersionString(); - this->connectedRemoteIPAddress = networkMessageIntro.getExternalIp(); - this->playerLanguage = networkMessageIntro.getPlayerLanguage(); - this->playerUUID = networkMessageIntro.getPlayerUUID(); - this->platform = networkMessageIntro.getPlayerPlatform(); - - //printf("Got uuid from client [%s]\n",this->playerUUID.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got name [%s] versionString [%s], msgSessionId = %d\n",__FILE__,__FUNCTION__,name.c_str(),versionString.c_str(),msgSessionId); - - if(msgSessionId != sessionKey) { - string playerNameStr = name; - string sErr = "Client gave invalid sessionid for player [" + playerNameStr + "] actual [" + intToStr(msgSessionId) + "] expected [" + intToStr(sessionKey) + "]"; - printf("%s\n",sErr.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); - - close(); - return; - } - else if(this->playerUUID == "") { - string playerNameStr = name; - string sErr = "Client gave an invalid UUID for player [" + playerNameStr + "]"; - printf("%s\n",sErr.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); - - close(); - return; - } - else { - //check consistency - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - bool compatible = checkVersionComptability(getNetworkVersionGITString(), networkMessageIntro.getVersionString()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if(compatible == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - bool versionMatched = false; - string platformFreeVersion = getNetworkPlatformFreeVersionString(); - string sErr = ""; - - if(strncmp(platformFreeVersion.c_str(),networkMessageIntro.getVersionString().c_str(),strlen(platformFreeVersion.c_str())) != 0) { - string playerNameStr = name; - sErr = "Server and client binary mismatch!\nYou have to use the exactly same binaries!\n\nServer: " + getNetworkVersionGITString() + - "\nClient: " + networkMessageIntro.getVersionString() + " player [" + playerNameStr + "]"; - printf("%s\n",sErr.c_str()); - - serverInterface->sendTextMessage("Server and client binary mismatch!!",-1, true,"",lockedSlotIndex); - serverInterface->sendTextMessage(" Server:" + getNetworkVersionGITString(),-1, true,"",lockedSlotIndex); - serverInterface->sendTextMessage(" Client: "+ networkMessageIntro.getVersionString(),-1, true,"",lockedSlotIndex); - serverInterface->sendTextMessage(" Client player [" + playerNameStr + "]",-1, true,"",lockedSlotIndex); - } - else { - versionMatched = true; - - string playerNameStr = name; - sErr = "Warning, Server and client are using the same version but different platforms.\n\nServer: " + getNetworkVersionGITString() + - "\nClient: " + networkMessageIntro.getVersionString() + " player [" + playerNameStr + "]"; - //printf("%s\n",sErr.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); - } - - if(Config::getInstance().getBool("PlatformConsistencyChecks","true") && - versionMatched == false) { // error message and disconnect only if checked - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); + if (gotIntro == true) { + NetworkMessageText networkMessageText; + if (receiveMessage(&networkMessageText)) { + ChatMsgInfo msg(networkMessageText.getText().c_str(), networkMessageText.getTeamIndex(), networkMessageText.getPlayerIndex(), networkMessageText.getTargetLanguage()); + this->addChatInfo(msg); + gotTextMsg = true; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); close(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); return; } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - gotIntro = true; - - int factionIndex = this->serverInterface->gameSettings.getFactionIndexForStartLocation(playerIndex); - this->serverInterface->addClientToServerIPAddress(this->getSocket()->getConnectedIPAddress(this->getSocket()->getIpAddress()),this->connectedRemoteIPAddress); - - this->serverInterface->gameSettings.setNetworkPlayerUUID(factionIndex,this->playerUUID); - this->serverInterface->gameSettings.setNetworkPlayerPlatform(factionIndex,this->platform); - - if(serverInterface->getGameHasBeenInitiated() == true && - serverInterface->getAllowInGameConnections() == true) { - this->serverInterface->gameSettings.setNetworkPlayerStatuses(factionIndex,npst_None); - } - - if(getAllowGameDataSynchCheck() == true && serverInterface->getGameSettings() != NULL) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending NetworkMessageSynchNetworkGameData\n",__FILE__,__FUNCTION__,__LINE__); - - NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData(serverInterface->getGameSettings()); - sendMessage(&networkMessageSynchNetworkGameData); - } - - if(serverInterface->getGameHasBeenInitiated() == true && - serverInterface->getAllowInGameConnections() == true) { - - ConnectionSlot *slot = serverInterface->findSlotForUUID(this->playerUUID,true); - if(slot != NULL) { - slot->setJoinGameInProgressFlags(); - slot->setPauseForInGameConnection(true); - - serverInterface->switchSlot(this->playerIndex,slot->getPlayerIndex()); - } - else { - setJoinGameInProgressFlags(); - this->setPauseForInGameConnection(true); - } - } - else { - ConnectionSlot *slot = serverInterface->findSlotForUUID(this->playerUUID,true); - if(slot != NULL) { - serverInterface->switchSlot(this->playerIndex,slot->getPlayerIndex()); - } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + break; - case nmtLaunch: - case nmtBroadCastSetup: - { - if(gotIntro == true) { - if(this->serverInterface->getGameSettings() == NULL || - (joinGameInProgress == false && sessionKey != this->serverInterface->getGameSettings()->getMasterserver_admin())) { - string playerNameStr = name; - string sErr = "Client has invalid admin sessionid for player [" + playerNameStr + "]"; - printf("%s\n",sErr.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,sErr.c_str()); + case nmtMarkCell: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); - close(); - return; + if (gotIntro == true) { + NetworkMessageMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + MarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + + this->addMarkedCell(msg); + gotCellMarkerMsg = true; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } } + break; - NetworkMessageLaunch networkMessageLaunch; - if(receiveMessage(&networkMessageLaunch,networkMessageType)) { - if(networkMessageLaunch.getMessageType() == nmtLaunch) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtLaunch\n",__FILE__,__FUNCTION__,__LINE__); - //printf("Got launch request from client joinGameInProgress = %d joinGameInProgress = %d!\n",joinGameInProgress,joinGameInProgress); + case nmtUnMarkCell: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtUnMarkCell gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); + + if (gotIntro == true) { + NetworkMessageUnMarkCell networkMessageMarkCell; + if (receiveMessage(&networkMessageMarkCell)) { + UnMarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + + this->addUnMarkedCell(msg); + gotCellMarkerMsg = true; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - else if(networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got nmtBroadCastSetup\n",__FILE__,__FUNCTION__,__LINE__); + } + break; + + case nmtHighlightCell: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtMarkCell gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); + + if (gotIntro == true) { + NetworkMessageHighlightCell networkMessageHighlightCell; + if (receiveMessage(&networkMessageHighlightCell)) { + MarkedCell msg(networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex(), "none", -1); + + this->setHighlightedCell(msg); + gotCellMarkerMsg = true; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); + } + break; - char szBuf[1024]=""; - snprintf(szBuf,1023,"In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d",__FILE__,__FUNCTION__,__LINE__,networkMessageLaunch.getMessageType()); - throw megaglest_runtime_error(szBuf); - } + //command list + case nmtCommandList: { - int minHeadLessPlayersRequired = Config::getInstance().getInt("MinHeadlessPlayersRequired","2"); - if(this->joinGameInProgress == false && - networkMessageLaunch.getMessageType() == nmtLaunch && - this->ready == false && - this->serverInterface->getConnectedSlotCount(true) < minHeadLessPlayersRequired) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtCommandList gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); - Lang &lang= Lang::getInstance(); - const vector languageList = this->serverInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); - for(unsigned int index = 0; index < languageList.size(); ++index) { - char szBuf[4096]=""; - string msgTemplate = "You must have have at least %d player(s) connected to start this game!"; - if(lang.hasString("HeadlessAdminRequiresMorePlayers",languageList[index]) == true) { - msgTemplate = lang.getString("HeadlessAdminRequiresMorePlayers",languageList[index]); + if (gotIntro == true) { + NetworkMessageCommandList networkMessageCommandList; + if (receiveMessage(&networkMessageCommandList)) { + currentFrameCount = networkMessageCommandList.getFrameCount(); + lastReceiveCommandListTime = time(NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] currentFrameCount = %d\n", __FILE__, __FUNCTION__, __LINE__, currentFrameCount); + + MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList, CODE_AT_LINE); + for (int i = 0; i < networkMessageCommandList.getCommandCount(); ++i) { + vctPendingNetworkCommandList.push_back(*networkMessageCommandList.getCommand(i)); } - #ifdef WIN32 - _snprintf(szBuf,4095,msgTemplate.c_str(),minHeadLessPlayersRequired); - #else - snprintf(szBuf,4095,msgTemplate.c_str(),minHeadLessPlayersRequired); - #endif - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,szBuf); + //printf("Got commands from client frame: %d count: %d\n",currentFrameCount,vctPendingNetworkCommandList.size()); - string sMsg = szBuf; - bool echoLocal = lang.isLanguageLocal(languageList[index]); - this->serverInterface->sendTextMessage(sMsg,-1, echoLocal, languageList[index], this->getPlayerIndex()); - } + //printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - else { - if(this->joinGameInProgress == false) { - GameSettings gameSettingsBuffer; - networkMessageLaunch.buildGameSettings(&gameSettingsBuffer); + } + break; - //printf("Connection slot got networkMessageLaunch.getMessageType() = %d, got map [%s]\n",networkMessageLaunch.getMessageType(),gameSettings.getMap().c_str()); - //printf("\n\n\n\n=====Connection slot got settings:\n%s\n",gameSettings.toString().c_str()); + //process intro messages + case nmtIntro: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtIntro\n", __FILE__, __FUNCTION__); - this->serverInterface->broadcastGameSetup(&gameSettingsBuffer, true); - } + NetworkMessageIntro networkMessageIntro; + if (receiveMessage(&networkMessageIntro)) { + int32 msgSessionId = networkMessageIntro.getSessionId(); + this->name = networkMessageIntro.getName(); + this->versionString = networkMessageIntro.getVersionString(); + this->connectedRemoteIPAddress = networkMessageIntro.getExternalIp(); + this->playerLanguage = networkMessageIntro.getPlayerLanguage(); + this->playerUUID = networkMessageIntro.getPlayerUUID(); + this->platform = networkMessageIntro.getPlayerPlatform(); - if(this->joinGameInProgress == false && - networkMessageLaunch.getMessageType() == nmtLaunch) { + //printf("Got uuid from client [%s]\n",this->playerUUID.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got name [%s] versionString [%s], msgSessionId = %d\n", __FILE__, __FUNCTION__, name.c_str(), versionString.c_str(), msgSessionId); - this->serverInterface->setMasterserverAdminRequestLaunch(true); - } - else if(this->joinGameInProgress == true && - networkMessageLaunch.getMessageType() == nmtLaunch) { - //printf("!!! setStartInGameConnectionLaunch for client joinGameInProgress = %d!\n",joinGameInProgress); + if (msgSessionId != sessionKey) { + string playerNameStr = name; + string sErr = "Client gave invalid sessionid for player [" + playerNameStr + "] actual [" + intToStr(msgSessionId) + "] expected [" + intToStr(sessionKey) + "]"; + printf("%s\n", sErr.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); + + close(); + return; + } else if (this->playerUUID == "") { + string playerNameStr = name; + string sErr = "Client gave an invalid UUID for player [" + playerNameStr + "]"; + printf("%s\n", sErr.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); + + close(); + return; + } else { + //check consistency + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + bool compatible = checkVersionComptability(getNetworkVersionGITString(), networkMessageIntro.getVersionString()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + if (compatible == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + bool versionMatched = false; + string platformFreeVersion = getNetworkPlatformFreeVersionString(); + string sErr = ""; + + if (strncmp(platformFreeVersion.c_str(), networkMessageIntro.getVersionString().c_str(), strlen(platformFreeVersion.c_str())) != 0) { + string playerNameStr = name; + sErr = "Server and client binary mismatch!\nYou have to use the exactly same binaries!\n\nServer: " + getNetworkVersionGITString() + + "\nClient: " + networkMessageIntro.getVersionString() + " player [" + playerNameStr + "]"; + printf("%s\n", sErr.c_str()); + + serverInterface->sendTextMessage("Server and client binary mismatch!!", -1, true, "", lockedSlotIndex); + serverInterface->sendTextMessage(" Server:" + getNetworkVersionGITString(), -1, true, "", lockedSlotIndex); + serverInterface->sendTextMessage(" Client: " + networkMessageIntro.getVersionString(), -1, true, "", lockedSlotIndex); + serverInterface->sendTextMessage(" Client player [" + playerNameStr + "]", -1, true, "", lockedSlotIndex); + } else { + versionMatched = true; + + string playerNameStr = name; + sErr = "Warning, Server and client are using the same version but different platforms.\n\nServer: " + getNetworkVersionGITString() + + "\nClient: " + networkMessageIntro.getVersionString() + " player [" + playerNameStr + "]"; + //printf("%s\n",sErr.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); + } + + if (Config::getInstance().getBool("PlatformConsistencyChecks", "true") && + versionMatched == false) { // error message and disconnect only if checked + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); + close(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); + return; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + gotIntro = true; int factionIndex = this->serverInterface->gameSettings.getFactionIndexForStartLocation(playerIndex); - this->serverInterface->gameSettings.setFactionControl(factionIndex,ctNetwork); - this->serverInterface->gameSettings.setNetworkPlayerName(factionIndex,this->name); - this->serverInterface->gameSettings.setNetworkPlayerUUID(factionIndex,this->playerUUID); - this->serverInterface->gameSettings.setNetworkPlayerPlatform(factionIndex,this->platform); + this->serverInterface->addClientToServerIPAddress(this->getSocket()->getConnectedIPAddress(this->getSocket()->getIpAddress()), this->connectedRemoteIPAddress); - if(this->serverInterface->gameSettings.getNetworkPlayerStatuses(factionIndex) == npst_Disconnected) { - this->serverInterface->gameSettings.setNetworkPlayerStatuses(factionIndex,npst_None); + this->serverInterface->gameSettings.setNetworkPlayerUUID(factionIndex, this->playerUUID); + this->serverInterface->gameSettings.setNetworkPlayerPlatform(factionIndex, this->platform); + + if (serverInterface->getGameHasBeenInitiated() == true && + serverInterface->getAllowInGameConnections() == true) { + this->serverInterface->gameSettings.setNetworkPlayerStatuses(factionIndex, npst_None); } - this->serverInterface->broadcastGameSetup(&this->serverInterface->gameSettings, true); + if (getAllowGameDataSynchCheck() == true && serverInterface->getGameSettings() != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] sending NetworkMessageSynchNetworkGameData\n", __FILE__, __FUNCTION__, __LINE__); - this->setStartInGameConnectionLaunch(true); + NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData(serverInterface->getGameSettings()); + sendMessage(&networkMessageSynchNetworkGameData); + } + + if (serverInterface->getGameHasBeenInitiated() == true && + serverInterface->getAllowInGameConnections() == true) { + + ConnectionSlot *slot = serverInterface->findSlotForUUID(this->playerUUID, true); + if (slot != NULL) { + slot->setJoinGameInProgressFlags(); + slot->setPauseForInGameConnection(true); + + serverInterface->switchSlot(this->playerIndex, slot->getPlayerIndex()); + } else { + setJoinGameInProgressFlags(); + this->setPauseForInGameConnection(true); + } + } else { + ConnectionSlot *slot = serverInterface->findSlotForUUID(this->playerUUID, true); + if (slot != NULL) { + serverInterface->switchSlot(this->playerIndex, slot->getPlayerIndex()); + } + } } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + break; - //process datasynch messages - case nmtSynchNetworkGameDataStatus: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtSynchNetworkGameDataStatus, gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); + case nmtLaunch: + case nmtBroadCastSetup: + { + if (gotIntro == true) { + if (this->serverInterface->getGameSettings() == NULL || + (joinGameInProgress == false && sessionKey != this->serverInterface->getGameSettings()->getMasterserver_admin())) { + string playerNameStr = name; + string sErr = "Client has invalid admin sessionid for player [" + playerNameStr + "]"; + printf("%s\n", sErr.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, sErr.c_str()); - if(gotIntro == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - NetworkMessageSynchNetworkGameDataStatus networkMessageSynchNetworkGameDataStatus; - if(receiveMessage(&networkMessageSynchNetworkGameDataStatus)) { - this->setNetworkGameDataSynchCheckTechMismatchReport(""); - this->setReceivedDataSynchCheck(false); - - Config &config = Config::getInstance(); - string scenarioDir = ""; - if(serverInterface->getGameSettings()->getScenarioDir() != "") { - scenarioDir = serverInterface->getGameSettings()->getScenarioDir(); - if(EndsWith(scenarioDir, ".xml") == true) { - scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); - scenarioDir = scenarioDir.erase(scenarioDir.size() - serverInterface->getGameSettings()->getScenario().size(), serverInterface->getGameSettings()->getScenario().size() + 1); + close(); + return; } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getGameSettings()->getScenarioDir().c_str(),serverInterface->getGameSettings()->getScenario().c_str(),scenarioDir.c_str()); + NetworkMessageLaunch networkMessageLaunch; + if (receiveMessage(&networkMessageLaunch, networkMessageType)) { + if (networkMessageLaunch.getMessageType() == nmtLaunch) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtLaunch\n", __FILE__, __FUNCTION__, __LINE__); + //printf("Got launch request from client joinGameInProgress = %d joinGameInProgress = %d!\n",joinGameInProgress,joinGameInProgress); + } else if (networkMessageLaunch.getMessageType() == nmtBroadCastSetup) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got nmtBroadCastSetup\n", __FILE__, __FUNCTION__, __LINE__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Lined: %d] got networkMessageLaunch.getMessageType() = %d\n", __FILE__, __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + + char szBuf[1024] = ""; + snprintf(szBuf, 1023, "In [%s::%s Line: %d] Invalid networkMessageLaunch.getMessageType() = %d", __FILE__, __FUNCTION__, __LINE__, networkMessageLaunch.getMessageType()); + throw megaglest_runtime_error(szBuf); + } + + int minHeadLessPlayersRequired = Config::getInstance().getInt("MinHeadlessPlayersRequired", "2"); + if (this->joinGameInProgress == false && + networkMessageLaunch.getMessageType() == nmtLaunch && + this->ready == false && + this->serverInterface->getConnectedSlotCount(true) < minHeadLessPlayersRequired) { + + Lang &lang = Lang::getInstance(); + const vector languageList = this->serverInterface->getGameSettings()->getUniqueNetworkPlayerLanguages(); + for (unsigned int index = 0; index < languageList.size(); ++index) { + char szBuf[4096] = ""; + string msgTemplate = "You must have have at least %d player(s) connected to start this game!"; + if (lang.hasString("HeadlessAdminRequiresMorePlayers", languageList[index]) == true) { + msgTemplate = lang.getString("HeadlessAdminRequiresMorePlayers", languageList[index]); + } +#ifdef WIN32 + _snprintf(szBuf, 4095, msgTemplate.c_str(), minHeadLessPlayersRequired); +#else + snprintf(szBuf, 4095, msgTemplate.c_str(), minHeadLessPlayersRequired); +#endif + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, szBuf); + + string sMsg = szBuf; + bool echoLocal = lang.isLanguageLocal(languageList[index]); + this->serverInterface->sendTextMessage(sMsg, -1, echoLocal, languageList[index], this->getPlayerIndex()); + } + } else { + if (this->joinGameInProgress == false) { + GameSettings gameSettingsBuffer; + networkMessageLaunch.buildGameSettings(&gameSettingsBuffer); + + //printf("Connection slot got networkMessageLaunch.getMessageType() = %d, got map [%s]\n",networkMessageLaunch.getMessageType(),gameSettings.getMap().c_str()); + //printf("\n\n\n\n=====Connection slot got settings:\n%s\n",gameSettings.toString().c_str()); + + this->serverInterface->broadcastGameSetup(&gameSettingsBuffer, true); + } + + if (this->joinGameInProgress == false && + networkMessageLaunch.getMessageType() == nmtLaunch) { + + this->serverInterface->setMasterserverAdminRequestLaunch(true); + } else if (this->joinGameInProgress == true && + networkMessageLaunch.getMessageType() == nmtLaunch) { + //printf("!!! setStartInGameConnectionLaunch for client joinGameInProgress = %d!\n",joinGameInProgress); + + int factionIndex = this->serverInterface->gameSettings.getFactionIndexForStartLocation(playerIndex); + this->serverInterface->gameSettings.setFactionControl(factionIndex, ctNetwork); + this->serverInterface->gameSettings.setNetworkPlayerName(factionIndex, this->name); + this->serverInterface->gameSettings.setNetworkPlayerUUID(factionIndex, this->playerUUID); + this->serverInterface->gameSettings.setNetworkPlayerPlatform(factionIndex, this->platform); + + if (this->serverInterface->gameSettings.getNetworkPlayerStatuses(factionIndex) == npst_Disconnected) { + this->serverInterface->gameSettings.setNetworkPlayerStatuses(factionIndex, npst_None); + } + + this->serverInterface->broadcastGameSetup(&this->serverInterface->gameSettings, true); + + this->setStartInGameConnectionLaunch(true); + } + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } + } + break; - //tileset - uint32 tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets,scenarioDir), string("/") + serverInterface->getGameSettings()->getTileset() + string("/*"), ".xml", NULL); - uint32 techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,scenarioDir), "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", NULL); - Checksum checksum; - string file = Config::getMapPath(serverInterface->getGameSettings()->getMap(),scenarioDir,false); - checksum.addFile(file); - uint32 mapCRC = checksum.getSum(); + //process datasynch messages + case nmtSynchNetworkGameDataStatus: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtSynchNetworkGameDataStatus, gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); - networkGameDataSynchCheckOkMap = (networkMessageSynchNetworkGameDataStatus.getMapCRC() == mapCRC); - networkGameDataSynchCheckOkTile = (networkMessageSynchNetworkGameDataStatus.getTilesetCRC() == tilesetCRC); - networkGameDataSynchCheckOkTech = (networkMessageSynchNetworkGameDataStatus.getTechCRC() == techCRC); + if (gotIntro == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - // For testing - //techCRC++; - - if( networkGameDataSynchCheckOkMap == true && - networkGameDataSynchCheckOkTile == true && - networkGameDataSynchCheckOkTech == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] client data synch ok\n",__FILE__,__FUNCTION__); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] mapCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,mapCRC,networkMessageSynchNetworkGameDataStatus.getMapCRC()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] tilesetCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,tilesetCRC,networkMessageSynchNetworkGameDataStatus.getTilesetCRC()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] techCRC = %d, remote = %d\n",__FILE__,__FUNCTION__,techCRC,networkMessageSynchNetworkGameDataStatus.getTechCRC()); - - if(allowDownloadDataSynch == true) { - // Now get all filenames with their CRC values and send to the client - vctFileList.clear(); + NetworkMessageSynchNetworkGameDataStatus networkMessageSynchNetworkGameDataStatus; + if (receiveMessage(&networkMessageSynchNetworkGameDataStatus)) { + this->setNetworkGameDataSynchCheckTechMismatchReport(""); + this->setReceivedDataSynchCheck(false); Config &config = Config::getInstance(); string scenarioDir = ""; - if(serverInterface->getGameSettings()->getScenarioDir() != "") { + if (serverInterface->getGameSettings()->getScenarioDir() != "") { scenarioDir = serverInterface->getGameSettings()->getScenarioDir(); - if(EndsWith(scenarioDir, ".xml") == true) { + if (EndsWith(scenarioDir, ".xml") == true) { scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); scenarioDir = scenarioDir.erase(scenarioDir.size() - serverInterface->getGameSettings()->getScenario().size(), serverInterface->getGameSettings()->getScenario().size() + 1); } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getGameSettings()->getScenarioDir().c_str(),serverInterface->getGameSettings()->getScenario().c_str(),scenarioDir.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", __FILE__, __FUNCTION__, __LINE__, serverInterface->getGameSettings()->getScenarioDir().c_str(), serverInterface->getGameSettings()->getScenario().c_str(), scenarioDir.c_str()); } - if(networkGameDataSynchCheckOkTile == false) { - if(tilesetCRC == 0) { - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTilesets,scenarioDir), string("/") + serverInterface->getGameSettings()->getTileset() + string("/*"), "", &vctFileList); - } - else { - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTilesets,scenarioDir), "/" + serverInterface->getGameSettings()->getTileset() + "/*", ".xml", &vctFileList); - } - } - if(networkGameDataSynchCheckOkTech == false) { - if(techCRC == 0) { - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),"/" + serverInterface->getGameSettings()->getTech() + "/*", "", &vctFileList); - } - else { - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),"/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", &vctFileList); - } + //tileset + uint32 tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets, scenarioDir), string("/") + serverInterface->getGameSettings()->getTileset() + string("/*"), ".xml", NULL); + uint32 techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs, scenarioDir), "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", NULL); + Checksum checksum; + string file = Config::getMapPath(serverInterface->getGameSettings()->getMap(), scenarioDir, false); + checksum.addFile(file); + uint32 mapCRC = checksum.getSum(); - string report = networkMessageSynchNetworkGameDataStatus.getTechCRCFileMismatchReport(serverInterface->getGameSettings()->getTech(),vctFileList); - this->setNetworkGameDataSynchCheckTechMismatchReport(report); - } - if(networkGameDataSynchCheckOkMap == false) { - vctFileList.push_back(std::pair(Config::getMapPath(serverInterface->getGameSettings()->getMap(),scenarioDir,false),mapCRC)); + networkGameDataSynchCheckOkMap = (networkMessageSynchNetworkGameDataStatus.getMapCRC() == mapCRC); + networkGameDataSynchCheckOkTile = (networkMessageSynchNetworkGameDataStatus.getTilesetCRC() == tilesetCRC); + networkGameDataSynchCheckOkTech = (networkMessageSynchNetworkGameDataStatus.getTechCRC() == techCRC); + + // For testing + //techCRC++; + + if (networkGameDataSynchCheckOkMap == true && + networkGameDataSynchCheckOkTile == true && + networkGameDataSynchCheckOkTech == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] client data synch ok\n", __FILE__, __FUNCTION__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] mapCRC = %d, remote = %d\n", __FILE__, __FUNCTION__, mapCRC, networkMessageSynchNetworkGameDataStatus.getMapCRC()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] tilesetCRC = %d, remote = %d\n", __FILE__, __FUNCTION__, tilesetCRC, networkMessageSynchNetworkGameDataStatus.getTilesetCRC()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] techCRC = %d, remote = %d\n", __FILE__, __FUNCTION__, techCRC, networkMessageSynchNetworkGameDataStatus.getTechCRC()); + + if (allowDownloadDataSynch == true) { + // Now get all filenames with their CRC values and send to the client + vctFileList.clear(); + + Config &config = Config::getInstance(); + string scenarioDir = ""; + if (serverInterface->getGameSettings()->getScenarioDir() != "") { + scenarioDir = serverInterface->getGameSettings()->getScenarioDir(); + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); + scenarioDir = scenarioDir.erase(scenarioDir.size() - serverInterface->getGameSettings()->getScenario().size(), serverInterface->getGameSettings()->getScenario().size() + 1); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", __FILE__, __FUNCTION__, __LINE__, serverInterface->getGameSettings()->getScenarioDir().c_str(), serverInterface->getGameSettings()->getScenario().c_str(), scenarioDir.c_str()); + } + + if (networkGameDataSynchCheckOkTile == false) { + if (tilesetCRC == 0) { + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTilesets, scenarioDir), string("/") + serverInterface->getGameSettings()->getTileset() + string("/*"), "", &vctFileList); + } else { + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTilesets, scenarioDir), "/" + serverInterface->getGameSettings()->getTileset() + "/*", ".xml", &vctFileList); + } + } + if (networkGameDataSynchCheckOkTech == false) { + if (techCRC == 0) { + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs, scenarioDir), "/" + serverInterface->getGameSettings()->getTech() + "/*", "", &vctFileList); + } else { + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs, scenarioDir), "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", &vctFileList); + } + + string report = networkMessageSynchNetworkGameDataStatus.getTechCRCFileMismatchReport(serverInterface->getGameSettings()->getTech(), vctFileList); + this->setNetworkGameDataSynchCheckTechMismatchReport(report); + } + if (networkGameDataSynchCheckOkMap == false) { + vctFileList.push_back(std::pair(Config::getMapPath(serverInterface->getGameSettings()->getMap(), scenarioDir, false), mapCRC)); + } + + NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck((int) vctFileList.size(), 1, vctFileList[0].second, vctFileList[0].first); + sendMessage(&networkMessageSynchNetworkGameDataFileCRCCheck); + } else { + if (networkGameDataSynchCheckOkTech == false) { + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs, scenarioDir), "/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", NULL); + + string report = networkMessageSynchNetworkGameDataStatus.getTechCRCFileMismatchReport(serverInterface->getGameSettings()->getTech(), vctFileList); + this->setNetworkGameDataSynchCheckTechMismatchReport(report); + } + } } - NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck((int)vctFileList.size(), 1, vctFileList[0].second, vctFileList[0].first); - sendMessage(&networkMessageSynchNetworkGameDataFileCRCCheck); - } - else { - if(networkGameDataSynchCheckOkTech == false) { - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),"/" + serverInterface->getGameSettings()->getTech() + "/*", ".xml", NULL); - - string report = networkMessageSynchNetworkGameDataStatus.getTechCRCFileMismatchReport(serverInterface->getGameSettings()->getTech(),vctFileList); - this->setNetworkGameDataSynchCheckTechMismatchReport(report); - } + this->setReceivedDataSynchCheck(true); + receivedNetworkGameStatus = true; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - - this->setReceivedDataSynchCheck(true); - receivedNetworkGameStatus = true; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; + break; - case nmtSynchNetworkGameDataFileCRCCheck: - { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck\n",__FILE__,__FUNCTION__); - - if(gotIntro == true) { - NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck; - if(receiveMessage(&networkMessageSynchNetworkGameDataFileCRCCheck)) + case nmtSynchNetworkGameDataFileCRCCheck: { - int fileIndex = networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex(); - NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck((int)vctFileList.size(), fileIndex, vctFileList[fileIndex-1].second, vctFileList[fileIndex-1].first); - sendMessage(&networkMessageSynchNetworkGameDataFileCRCCheck); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; - case nmtSynchNetworkGameDataFileGet: - { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtSynchNetworkGameDataFileCRCCheck\n", __FILE__, __FUNCTION__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] got nmtSynchNetworkGameDataFileGet\n",__FILE__,__FUNCTION__); - - if(gotIntro == true) { - NetworkMessageSynchNetworkGameDataFileGet networkMessageSynchNetworkGameDataFileGet; - if(receiveMessage(&networkMessageSynchNetworkGameDataFileGet)) { - FileTransferInfo fileInfo; - fileInfo.hostType = eServer; - //fileInfo.serverIP = this->ip.getString(); - fileInfo.serverPort = Config::getInstance().getInt("PortServer",intToStr(GameConstants::serverPort).c_str()); - fileInfo.fileName = networkMessageSynchNetworkGameDataFileGet.getFileName(); - - FileTransferSocketThread *fileXferThread = new FileTransferSocketThread(fileInfo); - fileXferThread->start(); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } - } - break; - - case nmtSwitchSetupRequest: - { - //printf("Got nmtSwitchSetupRequest A gotIntro = %d\n",gotIntro); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); - - if(gotIntro == true) { - //printf("Got nmtSwitchSetupRequest B\n"); - - SwitchSetupRequest switchSetupRequest; - if(receiveMessage(&switchSetupRequest)) { - MutexSafeWrapper safeMutex(getServerSynchAccessor(),CODE_AT_LINE); - - int slotIdx = switchSetupRequest.getCurrentSlotIndex(); - //int newSlotIdx = switchSetupRequest.getToSlotIndex(); - //printf("slotIdx = %d newSlotIdx = %d\n",slotIdx,newSlotIdx); - - if(serverInterface->getSwitchSetupRequests(slotIdx) == NULL) { - serverInterface->setSwitchSetupRequests(slotIdx,new SwitchSetupRequest()); + if (gotIntro == true) { + NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck; + if (receiveMessage(&networkMessageSynchNetworkGameDataFileCRCCheck)) { + int fileIndex = networkMessageSynchNetworkGameDataFileCRCCheck.getFileIndex(); + NetworkMessageSynchNetworkGameDataFileCRCCheck networkMessageSynchNetworkGameDataFileCRCCheck((int) vctFileList.size(), fileIndex, vctFileList[fileIndex - 1].second, vctFileList[fileIndex - 1].first); + sendMessage(&networkMessageSynchNetworkGameDataFileCRCCheck); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } - *(serverInterface->getSwitchSetupRequests(slotIdx)) = switchSetupRequest; - - //printf("slotIdx = %d newSlotIdx = %d\n",serverInterface->getSwitchSetupRequests(slotIdx)->getCurrentSlotIndex(),serverInterface->getSwitchSetupRequests(slotIdx)->getToSlotIndex()); - - this->playerStatus = switchSetupRequest.getNetworkPlayerStatus(); - this->name = switchSetupRequest.getNetworkPlayerName(); - this->playerLanguage = switchSetupRequest.getNetworkPlayerLanguage(); - - //printf("Got nmtSwitchSetupRequest C\n"); - //printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[factionIdx]->getNetworkPlayerName().c_str()); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[slotIdx]->getNetworkPlayerName().c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getNetworkPlayerStatus() = %d, switchSetupRequest.getSwitchFlags() = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIdx,switchSetupRequest.getNetworkPlayerName().c_str(),switchSetupRequest.getNetworkPlayerStatus(),switchSetupRequest.getSwitchFlags()); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; + break; + + case nmtSynchNetworkGameDataFileGet: + { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] got nmtSynchNetworkGameDataFileGet\n", __FILE__, __FUNCTION__); + + if (gotIntro == true) { + NetworkMessageSynchNetworkGameDataFileGet networkMessageSynchNetworkGameDataFileGet; + if (receiveMessage(&networkMessageSynchNetworkGameDataFileGet)) { + FileTransferInfo fileInfo; + fileInfo.hostType = eServer; + //fileInfo.serverIP = this->ip.getString(); + fileInfo.serverPort = Config::getInstance().getInt("PortServer", intToStr(GameConstants::serverPort).c_str()); + fileInfo.fileName = networkMessageSynchNetworkGameDataFileGet.getFileName(); + + FileTransferSocketThread *fileXferThread = new FileTransferSocketThread(fileInfo); + fileXferThread->start(); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; - } + break; - break; - } + case nmtSwitchSetupRequest: + { + //printf("Got nmtSwitchSetupRequest A gotIntro = %d\n",gotIntro); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d\n", __FILE__, __FUNCTION__, __LINE__, gotIntro); - case nmtReady: - { - NetworkMessageReady networkMessageReady; - this->receiveMessage(&networkMessageReady); + if (gotIntro == true) { + //printf("Got nmtSwitchSetupRequest B\n"); - // its simply ignored here. Probably we are starting a game - //printf("Got ready message from client slot joinGameInProgress = %d\n",joinGameInProgress); - if(joinGameInProgress == true) { - NetworkMessageReady networkMessageReady(0); - this->sendMessage(&networkMessageReady); - this->setGameStarted(true); + SwitchSetupRequest switchSetupRequest; + if (receiveMessage(&switchSetupRequest)) { + MutexSafeWrapper safeMutex(getServerSynchAccessor(), CODE_AT_LINE); - this->currentFrameCount = serverInterface->getCurrentFrameCount(); - //printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount); + int slotIdx = switchSetupRequest.getCurrentSlotIndex(); + //int newSlotIdx = switchSetupRequest.getToSlotIndex(); + //printf("slotIdx = %d newSlotIdx = %d\n",slotIdx,newSlotIdx); - this->currentLagCount = 0; - this->lastReceiveCommandListTime = time(NULL); + if (serverInterface->getSwitchSetupRequests(slotIdx) == NULL) { + serverInterface->setSwitchSetupRequests(slotIdx, new SwitchSetupRequest()); + } + *(serverInterface->getSwitchSetupRequests(slotIdx)) = switchSetupRequest; - this->setReady(); - } - // unpause the game - else { - this->setUnPauseForInGameConnection(true); - } - break; - } - case nmtLoadingStatusMessage: - break; + //printf("slotIdx = %d newSlotIdx = %d\n",serverInterface->getSwitchSetupRequests(slotIdx)->getCurrentSlotIndex(),serverInterface->getSwitchSetupRequests(slotIdx)->getToSlotIndex()); - default: - { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkMessageType = %d\n",__FILE__,__FUNCTION__,__LINE__,networkMessageType); + this->playerStatus = switchSetupRequest.getNetworkPlayerStatus(); + this->name = switchSetupRequest.getNetworkPlayerName(); + this->playerLanguage = switchSetupRequest.getNetworkPlayerLanguage(); - if(gotIntro == true) { - //throw megaglest_runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType)); - string sErr = "Unexpected message in connection slot: " + intToStr(networkMessageType); - //sendTextMessage(sErr,-1); - //DisplayErrorMessage(sErr); - threadErrorList.push_back(sErr); - return; + //printf("Got nmtSwitchSetupRequest C\n"); + //printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[factionIdx]->getNetworkPlayerName().c_str()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] networkPlayerName [%s]\n", __FILE__, __FUNCTION__, __LINE__, serverInterface->getSwitchSetupRequests()[slotIdx]->getNetworkPlayerName().c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getNetworkPlayerStatus() = %d, switchSetupRequest.getSwitchFlags() = %d\n", __FILE__, __FUNCTION__, __LINE__, slotIdx, switchSetupRequest.getNetworkPlayerName().c_str(), switchSetupRequest.getNetworkPlayerStatus(), switchSetupRequest.getSwitchFlags()); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; + } + + break; } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got invalid message type before intro, disconnecting socket.\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); - this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); - close(); - return; + case nmtReady: + { + NetworkMessageReady networkMessageReady; + this->receiveMessage(&networkMessageReady); + + // its simply ignored here. Probably we are starting a game + //printf("Got ready message from client slot joinGameInProgress = %d\n",joinGameInProgress); + if (joinGameInProgress == true) { + NetworkMessageReady networkMessageReady(0); + this->sendMessage(&networkMessageReady); + this->setGameStarted(true); + + this->currentFrameCount = serverInterface->getCurrentFrameCount(); + //printf("#2 Server slot got currentFrameCount = %d\n",currentFrameCount); + + this->currentLagCount = 0; + this->lastReceiveCommandListTime = time(NULL); + + this->setReady(); + } + // unpause the game + else { + this->setUnPauseForInGameConnection(true); + } + break; } - } - } + case nmtLoadingStatusMessage: + break; - //printf("#3 Server slot got currentFrameCount = %d\n",currentFrameCount); + default: + { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] networkMessageType = %d\n", __FILE__, __FUNCTION__, __LINE__, networkMessageType); - // This may end up continuously lagging and not disconnecting players who have - // just the 'wrong' amount of lag (but not enough to be horrible for a disconnect) - if(Config::getInstance().getBool("AutoClientLagCorrection","true") == true) { - double LAG_CHECK_GRACE_PERIOD = 15; + if (gotIntro == true) { + //throw megaglest_runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType)); + string sErr = "Unexpected message in connection slot: " + intToStr(networkMessageType); + //sendTextMessage(sErr,-1); + //DisplayErrorMessage(sErr); + threadErrorList.push_back(sErr); + return; + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got invalid message type before intro, disconnecting socket.\n", __FILE__, __FUNCTION__, __LINE__); - //printf("#4 Server slot got currentFrameCount = %d\n",currentFrameCount); - - if(this->serverInterface->getGameStartTime() > 0 && - difftime((long int)time(NULL),this->serverInterface->getGameStartTime()) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),this->getConnectedTime()) >= LAG_CHECK_GRACE_PERIOD) { - if(this->isConnected() == true && this->gotIntro == true && this->skipLagCheck == false) { - double clientLag = this->serverInterface->getCurrentFrameCount() - this->getCurrentFrameCount(); - double clientLagCount = (gameSettings.getNetworkFramePeriod() > 0 ? (clientLag / gameSettings.getNetworkFramePeriod()) : 0); - double clientLagTime = difftime((long int)time(NULL),this->getLastReceiveCommandListTime()); - - double maxFrameCountLagAllowed = 10; - double maxClientLagTimeAllowed = 8; - - // New lag check - if((clientLagCount > maxFrameCountLagAllowed) || - (maxClientLagTimeAllowed > 0 && clientLagTime > maxClientLagTimeAllowed)) { - - waitForLaggingClient = true; - if(waitedForLaggingClient == false) { - waitedForLaggingClient = true; - printf("*TESTING*: START Waiting for lagging client playerIndex = %d [%s] clientLagCount = %f [%f]\n",playerIndex,name.c_str(),clientLagCount,clientLagTime); + if (SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, networkMessageType, this->playerIndex, this->getIpAddress().c_str()); + this->serverInterface->notifyBadClientConnectAttempt(this->getIpAddress()); + close(); + return; } } } + + //printf("#3 Server slot got currentFrameCount = %d\n",currentFrameCount); + + // This may end up continuously lagging and not disconnecting players who have + // just the 'wrong' amount of lag (but not enough to be horrible for a disconnect) + if (Config::getInstance().getBool("AutoClientLagCorrection", "true") == true) { + double LAG_CHECK_GRACE_PERIOD = 15; + + //printf("#4 Server slot got currentFrameCount = %d\n",currentFrameCount); + + if (this->serverInterface->getGameStartTime() > 0 && + difftime((long int) time(NULL), this->serverInterface->getGameStartTime()) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), this->getConnectedTime()) >= LAG_CHECK_GRACE_PERIOD) { + if (this->isConnected() == true && this->gotIntro == true && this->skipLagCheck == false) { + double clientLag = this->serverInterface->getCurrentFrameCount() - this->getCurrentFrameCount(); + double clientLagCount = (gameSettings.getNetworkFramePeriod() > 0 ? (clientLag / gameSettings.getNetworkFramePeriod()) : 0); + double clientLagTime = difftime((long int) time(NULL), this->getLastReceiveCommandListTime()); + + double maxFrameCountLagAllowed = 10; + double maxClientLagTimeAllowed = 8; + + // New lag check + if ((clientLagCount > maxFrameCountLagAllowed) || + (maxClientLagTimeAllowed > 0 && clientLagTime > maxClientLagTimeAllowed)) { + + waitForLaggingClient = true; + if (waitedForLaggingClient == false) { + waitedForLaggingClient = true; + printf("*TESTING*: START Waiting for lagging client playerIndex = %d [%s] clientLagCount = %f [%f]\n", playerIndex, name.c_str(), clientLagCount, clientLagTime); + } + } + } + } + + //printf("#5 Server slot got currentFrameCount = %d\n",currentFrameCount); + } + //printf("#5a Server slot got currentFrameCount = %d\n",currentFrameCount); } - //printf("#5 Server slot got currentFrameCount = %d\n",currentFrameCount); + //printf("#6 Server slot got currentFrameCount = %d\n",currentFrameCount); + + if (waitedForLaggingClient == true) { + printf("*TESTING*: FINISHED Waiting for lagging client playerIndex = %d [%s]\n", playerIndex, name.c_str()); + } + + //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); + + validateConnection(); + + //printf("#7 Server slot got currentFrameCount = %d\n",currentFrameCount); + + //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] calling close...\n", __FILE__, __FUNCTION__, __LINE__); + + //printf("Closing connection slot socketInfo.first = %d\n",socketInfo.first); + + close(); + + //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); } - //printf("#5a Server slot got currentFrameCount = %d\n",currentFrameCount); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", __FILE__, __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", __FILE__, __FUNCTION__, __LINE__, ex.what()); - //printf("#6 Server slot got currentFrameCount = %d\n",currentFrameCount); - - if(waitedForLaggingClient == true) { - printf("*TESTING*: FINISHED Waiting for lagging client playerIndex = %d [%s]\n",playerIndex,name.c_str()); - } - - //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); - - validateConnection(); - - //printf("#7 Server slot got currentFrameCount = %d\n",currentFrameCount); + threadErrorList.push_back(ex.what()); //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] calling close...\n",__FILE__,__FUNCTION__,__LINE__); - //printf("Closing connection slot socketInfo.first = %d\n",socketInfo.first); + //printf("#8 Server slot got currentFrameCount = %d\n",currentFrameCount); + //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); + } + void ConnectionSlot::validateConnection() { + if (this->isConnected() == true && + gotIntro == false && connectedTime > 0 && + difftime((long int) time(NULL), connectedTime) > GameConstants::maxClientConnectHandshakeSecs) { + + //printf("Closing connection slot timed out!\n"); close(); + } + } - //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); + //void ConnectionSlot::resetJoinGameInProgressFlags() { + // this->gotIntro = false; + // this->skipLagCheck = false; + // this->joinGameInProgress = false; + // this->ready = false; + //} + + void ConnectionSlot::setJoinGameInProgressFlags() { + this->gotIntro = true; + this->skipLagCheck = true; + this->joinGameInProgress = true; + this->ready = false; + this->sentSavedGameInfo = false; + } + + void ConnectionSlot::close() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s LINE: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + //printf("Closing slot for playerIndex = %d\n",playerIndex); + //if(serverInterface->getAllowInGameConnections() == true) { + //printf("Closing connection slot!\n"); + //} + //printf("ConnectionSlot::close() #1 this->getSocket() = %p\n",this->getSocket()); + + this->gotIntro = false; + this->skipLagCheck = false; + this->joinGameInProgress = false; + this->sentSavedGameInfo = false; + this->pauseForInGameConnection = false; + this->unPauseForInGameConnection = false; + this->ready = false; + this->connectedTime = 0; + + if (this->slotThreadWorker != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + this->slotThreadWorker->setAllEventsCompleted(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + //printf("ConnectionSlot::close() #2 this->getSocket() = %p\n",this->getSocket()); + + MutexSafeWrapper safeMutex(mutexCloseConnection, CODE_AT_LINE); + bool updateServerListener = (this->getSocket() != NULL); + + //printf("ConnectionSlot::close() #3 this->getSocket() = %p updateServerListener = %d\n",this->getSocket(),updateServerListener); + + this->deleteSocket(); + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s LINE: %d]\n", __FILE__, __FUNCTION__, __LINE__); + //printf("Closing slot for playerIndex = %d updateServerListener = %d ready = %d\n",playerIndex,updateServerListener,ready); + + if (updateServerListener == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s LINE: %d]\n", __FILE__, __FUNCTION__, __LINE__); + serverInterface->updateListen(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END\n", __FILE__, __FUNCTION__); } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); - threadErrorList.push_back(ex.what()); - - //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); - } - - //printf("#8 Server slot got currentFrameCount = %d\n",currentFrameCount); - //if(chrono.getMillis() > 1) printf("In [%s::%s Line: %d] action running for msecs: %lld\n",__FILE__,__FUNCTION__,__LINE__,(long long int)chrono.getMillis()); -} - -void ConnectionSlot::validateConnection() { - if(this->isConnected() == true && - gotIntro == false && connectedTime > 0 && - difftime((long int)time(NULL),connectedTime) > GameConstants::maxClientConnectHandshakeSecs) { - - //printf("Closing connection slot timed out!\n"); - close(); - } -} - -//void ConnectionSlot::resetJoinGameInProgressFlags() { -// this->gotIntro = false; -// this->skipLagCheck = false; -// this->joinGameInProgress = false; -// this->ready = false; -//} - -void ConnectionSlot::setJoinGameInProgressFlags() { - this->gotIntro = true; - this->skipLagCheck = true; - this->joinGameInProgress = true; - this->ready = false; - this->sentSavedGameInfo = false; -} - -void ConnectionSlot::close() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - //printf("Closing slot for playerIndex = %d\n",playerIndex); - //if(serverInterface->getAllowInGameConnections() == true) { - //printf("Closing connection slot!\n"); - //} - //printf("ConnectionSlot::close() #1 this->getSocket() = %p\n",this->getSocket()); - - this->gotIntro = false; - this->skipLagCheck = false; - this->joinGameInProgress = false; - this->sentSavedGameInfo = false; - this->pauseForInGameConnection = false; - this->unPauseForInGameConnection = false; - this->ready = false; - this->connectedTime = 0; - - if(this->slotThreadWorker != NULL) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - this->slotThreadWorker->setAllEventsCompleted(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - //printf("ConnectionSlot::close() #2 this->getSocket() = %p\n",this->getSocket()); - - MutexSafeWrapper safeMutex(mutexCloseConnection,CODE_AT_LINE); - bool updateServerListener = (this->getSocket() != NULL); - - //printf("ConnectionSlot::close() #3 this->getSocket() = %p updateServerListener = %d\n",this->getSocket(),updateServerListener); - - this->deleteSocket(); - safeMutex.ReleaseLock(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //printf("Closing slot for playerIndex = %d updateServerListener = %d ready = %d\n",playerIndex,updateServerListener,ready); - - if(updateServerListener == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - serverInterface->updateListen(); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__); -} - -Mutex * ConnectionSlot::getServerSynchAccessor() { - return (serverInterface != NULL ? serverInterface->getServerSynchAccessor() : NULL); -} - -void ConnectionSlot::signalUpdate(ConnectionSlotEvent *event) { - if(slotThreadWorker != NULL) { - slotThreadWorker->signalUpdate(event); - } -} - -bool ConnectionSlot::updateCompleted(ConnectionSlotEvent *event) { - bool waitingForThread = (slotThreadWorker != NULL && - slotThreadWorker->isSignalCompleted(event) == false && - slotThreadWorker->getQuitStatus() == false && - slotThreadWorker->getRunningStatus() == true); - - return (waitingForThread == false); -} - -void ConnectionSlot::sendMessage(NetworkMessage* networkMessage) { - MutexSafeWrapper safeMutex(socketSynchAccessor,CODE_AT_LINE); - - // Skip text messages not intended for the players preferred language - NetworkMessageText *textMsg = dynamic_cast(networkMessage); - if(textMsg != NULL) { - //printf("\n\n\n~~~ SERVER HAS NetworkMessageText target [%s] player [%s] msg[%s]\n\n\n",textMsg->getTargetLanguage().c_str(),this->getNetworkPlayerLanguage().c_str(), textMsg->getText().c_str()); - if(textMsg->getTargetLanguage() != "" && - textMsg->getTargetLanguage() != this->getNetworkPlayerLanguage()) { - return; + Mutex * ConnectionSlot::getServerSynchAccessor() { + return (serverInterface != NULL ? serverInterface->getServerSynchAccessor() : NULL); } - } - NetworkInterface::sendMessage(networkMessage); -} - -string ConnectionSlot::getHumanPlayerName(int index) { - return serverInterface->getHumanPlayerName(index); -} - -vector ConnectionSlot::getPendingNetworkCommandList(bool clearList) { - vector ret; - MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList,CODE_AT_LINE); - if(vctPendingNetworkCommandList.empty() == false) { - ret = vctPendingNetworkCommandList; - if(clearList == true) { - vctPendingNetworkCommandList.clear(); + void ConnectionSlot::signalUpdate(ConnectionSlotEvent *event) { + if (slotThreadWorker != NULL) { + slotThreadWorker->signalUpdate(event); + } } - } - return ret; -} + bool ConnectionSlot::updateCompleted(ConnectionSlotEvent *event) { + bool waitingForThread = (slotThreadWorker != NULL && + slotThreadWorker->isSignalCompleted(event) == false && + slotThreadWorker->getQuitStatus() == false && + slotThreadWorker->getRunningStatus() == true); -//void ConnectionSlot::clearPendingNetworkCommandList() { -// MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList,CODE_AT_LINE); -// if(vctPendingNetworkCommandList.empty() == false) { -// vctPendingNetworkCommandList.clear(); -// } -//} + return (waitingForThread == false); + } -bool ConnectionSlot::hasValidSocketId() { - bool result = false; - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - if(socket != NULL && socket->getSocketId() > 0) { - result = true; - } - return result; + void ConnectionSlot::sendMessage(NetworkMessage* networkMessage) { + MutexSafeWrapper safeMutex(socketSynchAccessor, CODE_AT_LINE); -} + // Skip text messages not intended for the players preferred language + NetworkMessageText *textMsg = dynamic_cast(networkMessage); + if (textMsg != NULL) { + //printf("\n\n\n~~~ SERVER HAS NetworkMessageText target [%s] player [%s] msg[%s]\n\n\n",textMsg->getTargetLanguage().c_str(),this->getNetworkPlayerLanguage().c_str(), textMsg->getText().c_str()); + if (textMsg->getTargetLanguage() != "" && + textMsg->getTargetLanguage() != this->getNetworkPlayerLanguage()) { + return; + } + } -bool ConnectionSlot::isConnected() { - bool result = false; - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - if(socket != NULL && socket->isConnected() == true) { - result = true; - } - return result; -} + NetworkInterface::sendMessage(networkMessage); + } + + string ConnectionSlot::getHumanPlayerName(int index) { + return serverInterface->getHumanPlayerName(index); + } + + vector ConnectionSlot::getPendingNetworkCommandList(bool clearList) { + vector ret; + MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList, CODE_AT_LINE); + if (vctPendingNetworkCommandList.empty() == false) { + ret = vctPendingNetworkCommandList; + if (clearList == true) { + vctPendingNetworkCommandList.clear(); + } + } + + return ret; + } + + //void ConnectionSlot::clearPendingNetworkCommandList() { + // MutexSafeWrapper safeMutexSlot(mutexPendingNetworkCommandList,CODE_AT_LINE); + // if(vctPendingNetworkCommandList.empty() == false) { + // vctPendingNetworkCommandList.clear(); + // } + //} + + bool ConnectionSlot::hasValidSocketId() { + bool result = false; + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + if (socket != NULL && socket->getSocketId() > 0) { + result = true; + } + return result; + + } + + bool ConnectionSlot::isConnected() { + bool result = false; + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + if (socket != NULL && socket->isConnected() == true) { + result = true; + } + return result; + } + + PLATFORM_SOCKET ConnectionSlot::getSocketId() { + PLATFORM_SOCKET result = 0; + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + if (socket != NULL) { + result = socket->getSocketId(); + } + return result; + } + + pair ConnectionSlot::getSocketInfo() { + pair result; + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + result.first = (socket != NULL && socket->isConnected()); + result.second = socket; + + return result; + + } + + Socket* ConnectionSlot::getSocket(bool mutexLock) { + MutexSafeWrapper safeMutexSlot(NULL, CODE_AT_LINE); + if (mutexLock == true) { + safeMutexSlot.setMutex(mutexSocket, CODE_AT_LINE); + } + return socket; + } + + void ConnectionSlot::setSocket(Socket *newSocket) { + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + socket = newSocket; + } + + void ConnectionSlot::deleteSocket() { + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + delete socket; + socket = NULL; + } + + bool ConnectionSlot::hasDataToRead() { + bool result = false; + + MutexSafeWrapper safeMutexSlot(mutexSocket, CODE_AT_LINE); + if (socket != NULL && socket->hasDataToRead() == true) { + result = true; + } + + return result; + } -PLATFORM_SOCKET ConnectionSlot::getSocketId() { - PLATFORM_SOCKET result = 0; - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - if(socket != NULL) { - result = socket->getSocketId(); } - return result; -} - -pair ConnectionSlot::getSocketInfo() { - pair result; - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - result.first = (socket != NULL && socket->isConnected()); - result.second = socket; - - return result; - -} - -Socket* ConnectionSlot::getSocket(bool mutexLock) { - MutexSafeWrapper safeMutexSlot(NULL,CODE_AT_LINE); - if(mutexLock == true) { - safeMutexSlot.setMutex(mutexSocket,CODE_AT_LINE); - } - return socket; -} - -void ConnectionSlot::setSocket(Socket *newSocket) { - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - socket = newSocket; -} - -void ConnectionSlot::deleteSocket() { - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - delete socket; - socket = NULL; -} - -bool ConnectionSlot::hasDataToRead() { - bool result = false; - - MutexSafeWrapper safeMutexSlot(mutexSocket,CODE_AT_LINE); - if(socket != NULL && socket->hasDataToRead() == true) { - result = true; - } - - return result; -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index b4165ed2e..bac4f24fe 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -33,263 +33,350 @@ using Shared::Platform::ServerSocket; using Shared::Platform::Socket; using std::vector; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class ServerInterface; -class ConnectionSlot; + class ServerInterface; + class ConnectionSlot; -// ===================================================== -// class ConnectionSlotThread -// ===================================================== + // ===================================================== + // class ConnectionSlotThread + // ===================================================== -enum ConnectionSlotEventType -{ - eNone, - eReceiveSocketData, - eSendSocketData -}; + enum ConnectionSlotEventType { + eNone, + eReceiveSocketData, + eSendSocketData + }; -class ConnectionSlotEvent { -public: + class ConnectionSlotEvent { + public: + + ConnectionSlotEvent() { + eventType = eNone; + triggerId = -1; + connectionSlot = NULL; + networkMessage = NULL; + socketTriggered = false; + eventCompleted = false; + eventId = -1; + } + + int64 triggerId; + ConnectionSlot* connectionSlot; + ConnectionSlotEventType eventType; + NetworkMessage *networkMessage; + bool socketTriggered; + bool eventCompleted; + int64 eventId; + }; + + // + // This interface describes the methods a callback object must implement + // + class ConnectionSlotCallbackInterface { + public: + virtual bool isClientConnected(int index) = 0; + virtual bool getAllowInGameConnections() const = 0; + virtual ConnectionSlot *getSlot(int index, bool lockMutex) = 0; + virtual Mutex *getSlotMutex(int index) = 0; + + virtual void slotUpdateTask(ConnectionSlotEvent *event) = 0; + virtual ~ConnectionSlotCallbackInterface() { + } + }; + + class ConnectionSlotThread : public BaseThread, public SlaveThreadControllerInterface { + protected: + + ConnectionSlotCallbackInterface * slotInterface; + Semaphore semTaskSignalled; + Mutex *triggerIdMutex; + vector eventList; + int slotIndex; + MasterSlaveThreadController *masterController; + + Mutex *triggerGameStarted; + bool gameStarted; + + virtual void setQuitStatus(bool value); + virtual void setTaskCompleted(int eventId); + + void slotUpdateTask(ConnectionSlotEvent *event); + + public: + explicit ConnectionSlotThread(int slotIndex); + ConnectionSlotThread(ConnectionSlotCallbackInterface *slotInterface, int slotIndex); + virtual ~ConnectionSlotThread(); + + bool getGameStarted(); + void setGameStarted(bool value); + + virtual void setMasterController(MasterSlaveThreadController *master) { + masterController = master; + } + virtual void signalSlave(void *userdata); + + virtual void execute(); + void signalUpdate(ConnectionSlotEvent *event); + bool isSignalCompleted(ConnectionSlotEvent *event); + + int getSlotIndex() const { + return slotIndex; + } + void setSlotIndex(int index) { + this->slotIndex = index; + } + + void purgeCompletedEvents(); + //void purgeAllEvents(); + void setAllEventsCompleted(); + + virtual bool canShutdown(bool deleteSelfIfShutdownDelayed = false); + }; + + // ===================================================== + // class ConnectionSlot + // ===================================================== + + class ConnectionSlot : public NetworkInterface { + private: + ServerInterface * serverInterface; + + Mutex *mutexSocket; + Socket* socket; + int playerIndex; + string name; + bool ready; + vector > vctFileList; + bool receivedNetworkGameStatus; + time_t connectedTime; + bool gotIntro; + + Mutex *mutexCloseConnection; + + Mutex *mutexPendingNetworkCommandList; + vector vctPendingNetworkCommandList; + ConnectionSlotThread* slotThreadWorker; + int currentFrameCount; + int currentLagCount; + int graceLagCtr; + time_t lastReceiveCommandListTime; + bool gotLagCountWarning; + string versionString; + int sessionKey; + uint32 connectedRemoteIPAddress; + int playerStatus; + string playerLanguage; + string playerUUID; + string platform; + + bool skipLagCheck; + bool joinGameInProgress; + bool canAcceptConnections; + bool startInGameConnectionLaunch; + bool pauseForInGameConnection; + bool unPauseForInGameConnection; + bool sentSavedGameInfo; + + int autoPauseGameCountForLag; + + public: + ConnectionSlot(ServerInterface* serverInterface, int playerIndex); + ~ConnectionSlot(); + + int getAutoPauseGameCountForLag(); + void incrementAutoPauseGameCountForLag(); + + bool getGameStarted(); + void setGameStarted(bool value); + + bool getStartInGameConnectionLaunch() const { + return startInGameConnectionLaunch; + } + void setStartInGameConnectionLaunch(bool value) { + startInGameConnectionLaunch = value; + } + + bool getPauseForInGameConnection() const { + return pauseForInGameConnection; + } + void setPauseForInGameConnection(bool value) { + pauseForInGameConnection = value; + } + + bool getUnPauseForInGameConnection() const { + return unPauseForInGameConnection; + } + void setUnPauseForInGameConnection(bool value) { + unPauseForInGameConnection = value; + } + + bool getSkipLagCheck() const { + return skipLagCheck; + } + bool getJoinGameInProgress() const { + return joinGameInProgress; + } + + int getGraceLagCtr() { + return graceLagCtr++; + } + void resetGraceLagCtr() { + graceLagCtr = 0; + } + + bool getSentSavedGameInfo() const { + return sentSavedGameInfo; + } + void setSentSavedGameInfo(bool value) { + sentSavedGameInfo = value; + } + + ConnectionSlotThread *getWorkerThread() { + return slotThreadWorker; + } + + void update(bool checkForNewClients, int lockedSlotIndex); + void setPlayerIndex(int value); + int getPlayerIndex() const { + return playerIndex; + } + + uint32 getConnectedRemoteIPAddress() const { + return connectedRemoteIPAddress; + } + + void setReady(); + const string &getName() const { + return name; + } + const string &getUUID() const { + return playerUUID; + } + const string &getPlatform() const { + return platform; + } + void setName(string value) { + name = value; + } + bool isReady() const { + return ready; + } + + virtual std::string getIpAddress(bool mutexLock = true); + + virtual Socket* getSocket(bool mutexLock = true); + pair getSocketInfo(); + + virtual void close(); + //virtual bool getFogOfWar(); + + bool getReceivedNetworkGameStatus() const { + return receivedNetworkGameStatus; + } + void setReceivedNetworkGameStatus(bool value) { + receivedNetworkGameStatus = value; + } + + bool hasValidSocketId(); + virtual bool getConnectHasHandshaked() const { + return gotIntro; + } + std::vector getThreadErrorList() const { + return threadErrorList; + } + void clearThreadErrorList() { + threadErrorList.clear(); + } + + vector getPendingNetworkCommandList(bool clearList = false); + //void clearPendingNetworkCommandList(); + + void signalUpdate(ConnectionSlotEvent *event); + bool updateCompleted(ConnectionSlotEvent *event); + + virtual void sendMessage(NetworkMessage* networkMessage); + int getCurrentFrameCount() const { + return currentFrameCount; + } + + int getCurrentLagCount() const { + return currentLagCount; + } + void setCurrentLagCount(int value) { + currentLagCount = value; + } + + time_t getLastReceiveCommandListTime() const { + return lastReceiveCommandListTime; + } + + bool getLagCountWarning() const { + return gotLagCountWarning; + } + void setLagCountWarning(bool value) { + gotLagCountWarning = value; + } + + const string &getVersionString() const { + return versionString; + } + + void validateConnection(); + virtual string getHumanPlayerName(int index = -1); + virtual int getHumanPlayerIndex() const { + return playerIndex; + } + + int getNetworkPlayerStatus() const { + return playerStatus; + } + + string getNetworkPlayerLanguage() const { + return playerLanguage; + } + + time_t getConnectedTime() const { + return connectedTime; + } + int getSessionKey() const { + return sessionKey; + } + + void updateSlot(ConnectionSlotEvent *event); + virtual bool isConnected(); + + PLATFORM_SOCKET getSocketId(); + + void setCanAcceptConnections(bool value) { + canAcceptConnections = value; + } + bool getCanAcceptConnections() const { + return canAcceptConnections; + } + + virtual void saveGame(XmlNode *rootNode) { + }; + + //void resetJoinGameInProgressFlags(); + void setJoinGameInProgressFlags(); + + protected: + + Mutex * getServerSynchAccessor(); + std::vector threadErrorList; + Mutex *socketSynchAccessor; + + void setSocket(Socket *newSocket); + void deleteSocket(); + virtual void update() { + } + + bool hasDataToRead(); + }; - ConnectionSlotEvent() { - eventType = eNone; - triggerId = -1; - connectionSlot = NULL; - networkMessage = NULL; - socketTriggered = false; - eventCompleted = false; - eventId = -1; } - - int64 triggerId; - ConnectionSlot* connectionSlot; - ConnectionSlotEventType eventType; - NetworkMessage *networkMessage; - bool socketTriggered; - bool eventCompleted; - int64 eventId; -}; - -// -// This interface describes the methods a callback object must implement -// -class ConnectionSlotCallbackInterface { -public: - virtual bool isClientConnected(int index) = 0; - virtual bool getAllowInGameConnections() const = 0; - virtual ConnectionSlot *getSlot(int index, bool lockMutex) = 0; - virtual Mutex *getSlotMutex(int index) = 0; - - virtual void slotUpdateTask(ConnectionSlotEvent *event) = 0; - virtual ~ConnectionSlotCallbackInterface() {} -}; - -class ConnectionSlotThread : public BaseThread, public SlaveThreadControllerInterface -{ -protected: - - ConnectionSlotCallbackInterface *slotInterface; - Semaphore semTaskSignalled; - Mutex *triggerIdMutex; - vector eventList; - int slotIndex; - MasterSlaveThreadController *masterController; - - Mutex *triggerGameStarted; - bool gameStarted; - - virtual void setQuitStatus(bool value); - virtual void setTaskCompleted(int eventId); - - void slotUpdateTask(ConnectionSlotEvent *event); - -public: - explicit ConnectionSlotThread(int slotIndex); - ConnectionSlotThread(ConnectionSlotCallbackInterface *slotInterface,int slotIndex); - virtual ~ConnectionSlotThread(); - - bool getGameStarted(); - void setGameStarted(bool value); - - virtual void setMasterController(MasterSlaveThreadController *master) { masterController = master; } - virtual void signalSlave(void *userdata); - - virtual void execute(); - void signalUpdate(ConnectionSlotEvent *event); - bool isSignalCompleted(ConnectionSlotEvent *event); - - int getSlotIndex() const {return slotIndex; } - void setSlotIndex(int index) { this->slotIndex = index; } - - void purgeCompletedEvents(); - //void purgeAllEvents(); - void setAllEventsCompleted(); - - virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false); -}; - -// ===================================================== -// class ConnectionSlot -// ===================================================== - -class ConnectionSlot: public NetworkInterface { -private: - ServerInterface* serverInterface; - - Mutex *mutexSocket; - Socket* socket; - int playerIndex; - string name; - bool ready; - vector > vctFileList; - bool receivedNetworkGameStatus; - time_t connectedTime; - bool gotIntro; - - Mutex *mutexCloseConnection; - - Mutex *mutexPendingNetworkCommandList; - vector vctPendingNetworkCommandList; - ConnectionSlotThread* slotThreadWorker; - int currentFrameCount; - int currentLagCount; - int graceLagCtr; - time_t lastReceiveCommandListTime; - bool gotLagCountWarning; - string versionString; - int sessionKey; - uint32 connectedRemoteIPAddress; - int playerStatus; - string playerLanguage; - string playerUUID; - string platform; - - bool skipLagCheck; - bool joinGameInProgress; - bool canAcceptConnections; - bool startInGameConnectionLaunch; - bool pauseForInGameConnection; - bool unPauseForInGameConnection; - bool sentSavedGameInfo; - - int autoPauseGameCountForLag; - -public: - ConnectionSlot(ServerInterface* serverInterface, int playerIndex); - ~ConnectionSlot(); - - int getAutoPauseGameCountForLag(); - void incrementAutoPauseGameCountForLag(); - - bool getGameStarted(); - void setGameStarted(bool value); - - bool getStartInGameConnectionLaunch() const { return startInGameConnectionLaunch; } - void setStartInGameConnectionLaunch(bool value) { startInGameConnectionLaunch = value; } - - bool getPauseForInGameConnection() const { return pauseForInGameConnection; } - void setPauseForInGameConnection(bool value) { pauseForInGameConnection = value; } - - bool getUnPauseForInGameConnection() const { return unPauseForInGameConnection; } - void setUnPauseForInGameConnection(bool value) { unPauseForInGameConnection = value; } - - bool getSkipLagCheck() const { return skipLagCheck; } - bool getJoinGameInProgress() const { return joinGameInProgress; } - - int getGraceLagCtr() { return graceLagCtr++; } - void resetGraceLagCtr() { graceLagCtr = 0; } - - bool getSentSavedGameInfo() const { return sentSavedGameInfo; } - void setSentSavedGameInfo(bool value) { sentSavedGameInfo = value; } - - ConnectionSlotThread *getWorkerThread() { return slotThreadWorker; } - - void update(bool checkForNewClients,int lockedSlotIndex); - void setPlayerIndex(int value); - int getPlayerIndex() const {return playerIndex;} - - uint32 getConnectedRemoteIPAddress() const { return connectedRemoteIPAddress; } - - void setReady(); - const string &getName() const {return name;} - const string &getUUID() const {return playerUUID;} - const string &getPlatform() const { return platform; } - void setName(string value) {name = value;} - bool isReady() const {return ready;} - - virtual std::string getIpAddress(bool mutexLock=true); - - virtual Socket* getSocket(bool mutexLock=true); - pair getSocketInfo(); - - virtual void close(); - //virtual bool getFogOfWar(); - - bool getReceivedNetworkGameStatus() const { return receivedNetworkGameStatus; } - void setReceivedNetworkGameStatus(bool value) { receivedNetworkGameStatus = value; } - - bool hasValidSocketId(); - virtual bool getConnectHasHandshaked() const { return gotIntro; } - std::vector getThreadErrorList() const { return threadErrorList; } - void clearThreadErrorList() { threadErrorList.clear(); } - - vector getPendingNetworkCommandList(bool clearList=false); - //void clearPendingNetworkCommandList(); - - void signalUpdate(ConnectionSlotEvent *event); - bool updateCompleted(ConnectionSlotEvent *event); - - virtual void sendMessage(NetworkMessage* networkMessage); - int getCurrentFrameCount() const { return currentFrameCount; } - - int getCurrentLagCount() const { return currentLagCount; } - void setCurrentLagCount(int value) { currentLagCount = value; } - - time_t getLastReceiveCommandListTime() const { return lastReceiveCommandListTime; } - - bool getLagCountWarning() const { return gotLagCountWarning; } - void setLagCountWarning(bool value) { gotLagCountWarning = value; } - - const string &getVersionString() const {return versionString;} - - void validateConnection(); - virtual string getHumanPlayerName(int index=-1); - virtual int getHumanPlayerIndex() const {return playerIndex;} - - int getNetworkPlayerStatus() const { return playerStatus;} - - string getNetworkPlayerLanguage() const { return playerLanguage; } - - time_t getConnectedTime() const { return connectedTime; } - int getSessionKey() const { return sessionKey; } - - void updateSlot(ConnectionSlotEvent *event); - virtual bool isConnected(); - - PLATFORM_SOCKET getSocketId(); - - void setCanAcceptConnections(bool value) { canAcceptConnections = value; } - bool getCanAcceptConnections() const { return canAcceptConnections; } - - virtual void saveGame(XmlNode *rootNode) {}; - - //void resetJoinGameInProgressFlags(); - void setJoinGameInProgressFlags(); - -protected: - - Mutex * getServerSynchAccessor(); - std::vector threadErrorList; - Mutex *socketSynchAccessor; - - void setSocket(Socket *newSocket); - void deleteSocket(); - virtual void update() {} - - bool hasDataToRead(); -}; - -}}//end namespace +}//end namespace #endif diff --git a/source/glest_game/network/masterserver_info.h b/source/glest_game/network/masterserver_info.h index dae40d32e..f6f13d83a 100644 --- a/source/glest_game/network/masterserver_info.h +++ b/source/glest_game/network/masterserver_info.h @@ -18,77 +18,135 @@ using std::string; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// =========================================================== -// class ParticleSystemType -// -/// A type of particle system -// =========================================================== + // =========================================================== + // class ParticleSystemType + // + /// A type of particle system + // =========================================================== -class MasterServerInfo { -protected: - //general info: - string glestVersion; - string platform; - string binaryCompileDate; - - //game info: - string serverTitle; - string ipAddress; - - //game setup info: - string tech; - string map; - string tileset; - int activeSlots; - int networkSlots; - int connectedClients; - int externalconnectport; - string country; - int status; + class MasterServerInfo { + protected: + //general info: + string glestVersion; + string platform; + string binaryCompileDate; -public: - const string &getGlestVersion() const {return glestVersion;} - const string &getPlatform() const {return platform;} - const string &getBinaryCompileDate() const {return binaryCompileDate;} - - const string &getServerTitle() const {return serverTitle;} - const string &getIpAddress() const {return ipAddress;} - - const string &getTech() const {return tech;} - const string &getMap() const {return map;} - const string &getTileset() const {return tileset;} - const int getActiveSlots() const {return activeSlots;} - const int getNetworkSlots() const {return networkSlots;} - const int getConnectedClients() const {return connectedClients;} - const int getExternalConnectPort() const {return externalconnectport;} - const string &getCountry() const { return country;} - const int getStatus() const { return status;} - - - //general info: - void setGlestVersion(string value) { glestVersion = value; } - void setPlatform(string value) { platform = value; } - void setBinaryCompileDate(string value) { binaryCompileDate = value; } - - //game info: - void setServerTitle(string value) { serverTitle = value; } - void setIpAddress(string value) { ipAddress = value; } - - //game setup info: - void setTech(string value) { tech = value; } - void setMap(string value) { map = value; } - void setTileset(string value) { tileset = value; } - - void setActiveSlots(int value) { activeSlots = value; } - void setNetworkSlots(int value) { networkSlots = value; } - void setConnectedClients(int value) { connectedClients = value; } - void setExternalConnectPort(int value) { externalconnectport = value; } - void setCountry(string value) { country = value; } - void setStatus(int value) { status = value; } -}; + //game info: + string serverTitle; + string ipAddress; -}}//end namespace + //game setup info: + string tech; + string map; + string tileset; + int activeSlots; + int networkSlots; + int connectedClients; + int externalconnectport; + string country; + int status; + + public: + const string &getGlestVersion() const { + return glestVersion; + } + const string &getPlatform() const { + return platform; + } + const string &getBinaryCompileDate() const { + return binaryCompileDate; + } + + const string &getServerTitle() const { + return serverTitle; + } + const string &getIpAddress() const { + return ipAddress; + } + + const string &getTech() const { + return tech; + } + const string &getMap() const { + return map; + } + const string &getTileset() const { + return tileset; + } + const int getActiveSlots() const { + return activeSlots; + } + const int getNetworkSlots() const { + return networkSlots; + } + const int getConnectedClients() const { + return connectedClients; + } + const int getExternalConnectPort() const { + return externalconnectport; + } + const string &getCountry() const { + return country; + } + const int getStatus() const { + return status; + } + + + //general info: + void setGlestVersion(string value) { + glestVersion = value; + } + void setPlatform(string value) { + platform = value; + } + void setBinaryCompileDate(string value) { + binaryCompileDate = value; + } + + //game info: + void setServerTitle(string value) { + serverTitle = value; + } + void setIpAddress(string value) { + ipAddress = value; + } + + //game setup info: + void setTech(string value) { + tech = value; + } + void setMap(string value) { + map = value; + } + void setTileset(string value) { + tileset = value; + } + + void setActiveSlots(int value) { + activeSlots = value; + } + void setNetworkSlots(int value) { + networkSlots = value; + } + void setConnectedClients(int value) { + connectedClients = value; + } + void setExternalConnectPort(int value) { + externalconnectport = value; + } + void setCountry(string value) { + country = value; + } + void setStatus(int value) { + status = value; + } + }; + + } +}//end namespace #endif diff --git a/source/glest_game/network/network_interface.cpp b/source/glest_game/network/network_interface.cpp index 0eb959479..be8a9aa35 100644 --- a/source/glest_game/network/network_interface.cpp +++ b/source/glest_game/network/network_interface.cpp @@ -26,571 +26,556 @@ using namespace Shared::Platform; using namespace Shared::Util; using namespace std; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class NetworkInterface -// ===================================================== + // ===================================================== + // class NetworkInterface + // ===================================================== -const int NetworkInterface::readyWaitTimeout= 99000; // 99 seconds to 0 looks good on the screen + const int NetworkInterface::readyWaitTimeout = 99000; // 99 seconds to 0 looks good on the screen -bool NetworkInterface::allowGameDataSynchCheck = false; -bool NetworkInterface::allowDownloadDataSynch = false; -DisplayMessageFunction NetworkInterface::pCB_DisplayMessage = NULL; + bool NetworkInterface::allowGameDataSynchCheck = false; + bool NetworkInterface::allowDownloadDataSynch = false; + DisplayMessageFunction NetworkInterface::pCB_DisplayMessage = NULL; -Vec3f MarkedCell::static_system_marker_color(MAGENTA.x,MAGENTA.y,MAGENTA.z); + Vec3f MarkedCell::static_system_marker_color(MAGENTA.x, MAGENTA.y, MAGENTA.z); -NetworkInterface::NetworkInterface() { - networkAccessMutex = new Mutex(CODE_AT_LINE); + NetworkInterface::NetworkInterface() { + networkAccessMutex = new Mutex(CODE_AT_LINE); - networkGameDataSynchCheckOkMap=false; - networkGameDataSynchCheckOkTile=false; - networkGameDataSynchCheckOkTech=false; - receivedDataSynchCheck=false; + networkGameDataSynchCheckOkMap = false; + networkGameDataSynchCheckOkTile = false; + networkGameDataSynchCheckOkTech = false; + receivedDataSynchCheck = false; - networkPlayerFactionCRCMutex = new Mutex(CODE_AT_LINE); - for(unsigned int index = 0; index < (unsigned int)GameConstants::maxPlayers; ++index) { - networkPlayerFactionCRC[index] = 0; - } -} - -void NetworkInterface::init() { - networkAccessMutex = NULL; - - networkGameDataSynchCheckOkMap=false; - networkGameDataSynchCheckOkTile=false; - networkGameDataSynchCheckOkTech=false; - receivedDataSynchCheck=false; - - gameSettings = GameSettings(); - - networkPlayerFactionCRCMutex = NULL; - for(unsigned int index = 0; index < (unsigned int)GameConstants::maxPlayers; ++index) { - networkPlayerFactionCRC[index] = 0; - } -} - -NetworkInterface::~NetworkInterface() { - delete networkAccessMutex; - networkAccessMutex = NULL; - - delete networkPlayerFactionCRCMutex; - networkPlayerFactionCRCMutex = NULL; -} - -uint32 NetworkInterface::getNetworkPlayerFactionCRC(int index) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkPlayerFactionCRCMutex,mutexOwnerId); - - return networkPlayerFactionCRC[index]; -} -void NetworkInterface::setNetworkPlayerFactionCRC(int index, uint32 crc) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkPlayerFactionCRCMutex,mutexOwnerId); - - networkPlayerFactionCRC[index]=crc; -} - -void NetworkInterface::addChatInfo(const ChatMsgInfo &msg) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - chatTextList.push_back(msg); -} - -void NetworkInterface::addMarkedCell(const MarkedCell &msg) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - markedCellList.push_back(msg); -} -void NetworkInterface::addUnMarkedCell(const UnMarkedCell &msg) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - unmarkedCellList.push_back(msg); -} - -void NetworkInterface::sendMessage(NetworkMessage* networkMessage){ - Socket* socket= getSocket(false); - - networkMessage->send(socket); -} - -NetworkMessageType NetworkInterface::getNextMessageType(int waitMilliseconds) { - Socket* socket= getSocket(false); - int8 messageType= nmtInvalid; - -/* - if(socket != NULL && - ((waitMilliseconds <= 0 && socket->hasDataToRead() == true) || - (waitMilliseconds > 0 && socket->hasDataToReadWithWait(waitMilliseconds) == true))) { - //peek message type - int dataSize = socket->getDataToRead(); - if(dataSize >= (int)sizeof(messageType)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataSize); - - int iPeek = socket->peek(&messageType, sizeof(messageType)); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPeek,messageType,sizeof(messageType)); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); + networkPlayerFactionCRCMutex = new Mutex(CODE_AT_LINE); + for (unsigned int index = 0; index < (unsigned int) GameConstants::maxPlayers; ++index) { + networkPlayerFactionCRC[index] = 0; + } } - //sanity check new message type - if(messageType < 0 || messageType >= nmtCount) { - if(getConnectHasHandshaked() == true) { - throw megaglest_runtime_error("Invalid message type: " + intToStr(messageType)); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Invalid message type = %d (no packet handshake yet so ignored)\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType); - } - } - } + void NetworkInterface::init() { + networkAccessMutex = NULL; - return static_cast(messageType); -*/ + networkGameDataSynchCheckOkMap = false; + networkGameDataSynchCheckOkTile = false; + networkGameDataSynchCheckOkTech = false; + receivedDataSynchCheck = false; + gameSettings = GameSettings(); - // According to here: https://support.microsoft.com/en-us/kb/192599 - // its a terrible sin to use MSG_PEEK so lets try an alternative - - /* - int bytesReceived = socket->receive(&messageType, sizeof(messageType), true); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,bytesReceived,messageType,sizeof(messageType)); - - return static_cast(messageType); - */ - - - if(socket != NULL && - ((waitMilliseconds <= 0 && socket->hasDataToRead() == true) || - (waitMilliseconds > 0 && socket->hasDataToReadWithWait(waitMilliseconds) == true))) { - //peek message type - int dataSize = socket->getDataToRead(); - if(dataSize >= (int)sizeof(messageType)) { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataSize); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] before recv\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - -// int iPeek = socket->peek(&messageType, sizeof(messageType)); -// if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPeek,messageType,sizeof(messageType)); -// if(iPeek > 0) { - int bytesReceived = socket->receive(&messageType, sizeof(messageType), true); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,bytesReceived,messageType,sizeof(messageType)); - //} - } - //else { - // if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); - //} - - if (socket->isSocketValid() == false) { - return nmtInvalid; + networkPlayerFactionCRCMutex = NULL; + for (unsigned int index = 0; index < (unsigned int) GameConstants::maxPlayers; ++index) { + networkPlayerFactionCRC[index] = 0; + } } - //sanity check new message type - if(messageType < 0 || messageType >= nmtCount) { - if(getConnectHasHandshaked() == true) { - throw megaglest_runtime_error("Invalid message type: " + intToStr(messageType)); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Invalid message type = %d (no packet handshake yet so ignored)\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType); - } - } - } - return static_cast(messageType); + NetworkInterface::~NetworkInterface() { + delete networkAccessMutex; + networkAccessMutex = NULL; -} - -bool NetworkInterface::receiveMessage(NetworkMessage* networkMessage){ - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - Socket* socket= getSocket(false); - - return networkMessage->receive(socket); -} - -bool NetworkInterface::receiveMessage(NetworkMessage* networkMessage, NetworkMessageType type) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - Socket* socket = getSocket(false); - - return networkMessage->receive(socket, type); -} - -bool NetworkInterface::isConnected(){ - bool result = (getSocket()!=NULL && getSocket()->isConnected()); - return result; -} - -void NetworkInterface::setLastPingInfo(const NetworkMessagePing &ping) { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - this->lastPingInfo = ping; -} - -void NetworkInterface::setLastPingInfoToNow() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - this->lastPingInfo.setPingReceivedLocalTime(time(NULL)); -} - -NetworkMessagePing NetworkInterface::getLastPingInfo() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - return lastPingInfo; -} -double NetworkInterface::getLastPingLag() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - return difftime((long int)time(NULL),lastPingInfo.getPingReceivedLocalTime()); -} - -void NetworkInterface::DisplayErrorMessage(string sErr, bool closeSocket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sErr [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str()); - //SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] sErr [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str()); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] sErr [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str()); - - if(closeSocket == true && getSocket() != NULL) { - close(); - } - - if(pCB_DisplayMessage != NULL) { - pCB_DisplayMessage(sErr.c_str(), false); - } - else { - throw megaglest_runtime_error(sErr); - } -} - -std::vector NetworkInterface::getChatTextList(bool clearList) { - std::vector result; - - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - if(chatTextList.empty() == false) { - result = chatTextList; - - if(clearList == true) { - chatTextList.clear(); + delete networkPlayerFactionCRCMutex; + networkPlayerFactionCRCMutex = NULL; } - } - return result; -} -void NetworkInterface::clearChatInfo() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); + uint32 NetworkInterface::getNetworkPlayerFactionCRC(int index) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkPlayerFactionCRCMutex, mutexOwnerId); - if(chatTextList.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] chatTextList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chatTextList.size()); - chatTextList.clear(); - } -} - -std::vector NetworkInterface::getMarkedCellList(bool clearList) { - std::vector result; - - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - if(markedCellList.empty() == false) { - result = markedCellList; - - if(clearList == true) { - markedCellList.clear(); + return networkPlayerFactionCRC[index]; } - } - return result; -} + void NetworkInterface::setNetworkPlayerFactionCRC(int index, uint32 crc) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkPlayerFactionCRCMutex, mutexOwnerId); -void NetworkInterface::clearMarkedCellList() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - if(markedCellList.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] markedCellList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,markedCellList.size()); - markedCellList.clear(); - } -} - -std::vector NetworkInterface::getUnMarkedCellList(bool clearList) { - std::vector result; - - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - if(unmarkedCellList.empty() == false) { - result = unmarkedCellList; - - if(clearList == true) { - unmarkedCellList.clear(); + networkPlayerFactionCRC[index] = crc; } - } - return result; -} -void NetworkInterface::clearUnMarkedCellList() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); + void NetworkInterface::addChatInfo(const ChatMsgInfo &msg) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); - if(unmarkedCellList.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] unmarkedCellList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,unmarkedCellList.size()); - unmarkedCellList.clear(); - } -} - -std::vector NetworkInterface::getHighlightedCellList(bool clearList) { - std::vector result; - - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - if(highlightedCellList.empty() == false) { - result = highlightedCellList; - - if(clearList == true) { - highlightedCellList.clear(); + chatTextList.push_back(msg); } - } - return result; -} -void NetworkInterface::clearHighlightedCellList() { - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); + void NetworkInterface::addMarkedCell(const MarkedCell &msg) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); - if(highlightedCellList.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] markedCellList.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,markedCellList.size()); - highlightedCellList.clear(); - } -} - -void NetworkInterface::setHighlightedCell(const MarkedCell &msg){ - static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); - MutexSafeWrapper safeMutex(networkAccessMutex,mutexOwnerId); - - for(int idx = 0; idx < (int)highlightedCellList.size(); idx++) { - MarkedCell mc = highlightedCellList[idx]; - if(mc.getFactionIndex()==msg.getFactionIndex()){ - highlightedCellList.erase(highlightedCellList.begin()+ idx); - break; + markedCellList.push_back(msg); } + void NetworkInterface::addUnMarkedCell(const UnMarkedCell &msg) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + unmarkedCellList.push_back(msg); + } + + void NetworkInterface::sendMessage(NetworkMessage* networkMessage) { + Socket* socket = getSocket(false); + + networkMessage->send(socket); + } + + NetworkMessageType NetworkInterface::getNextMessageType(int waitMilliseconds) { + Socket* socket = getSocket(false); + int8 messageType = nmtInvalid; + + /* + if(socket != NULL && + ((waitMilliseconds <= 0 && socket->hasDataToRead() == true) || + (waitMilliseconds > 0 && socket->hasDataToReadWithWait(waitMilliseconds) == true))) { + //peek message type + int dataSize = socket->getDataToRead(); + if(dataSize >= (int)sizeof(messageType)) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataSize); + + int iPeek = socket->peek(&messageType, sizeof(messageType)); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPeek,messageType,sizeof(messageType)); + } + else { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); + } + + //sanity check new message type + if(messageType < 0 || messageType >= nmtCount) { + if(getConnectHasHandshaked() == true) { + throw megaglest_runtime_error("Invalid message type: " + intToStr(messageType)); + } + else { + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Invalid message type = %d (no packet handshake yet so ignored)\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType); + } + } + } + + return static_cast(messageType); + */ + + + // According to here: https://support.microsoft.com/en-us/kb/192599 + // its a terrible sin to use MSG_PEEK so lets try an alternative + + /* + int bytesReceived = socket->receive(&messageType, sizeof(messageType), true); + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,bytesReceived,messageType,sizeof(messageType)); + + return static_cast(messageType); + */ + + + if (socket != NULL && + ((waitMilliseconds <= 0 && socket->hasDataToRead() == true) || + (waitMilliseconds > 0 && socket->hasDataToReadWithWait(waitMilliseconds) == true))) { + //peek message type + int dataSize = socket->getDataToRead(); + if (dataSize >= (int)sizeof(messageType)) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataSize); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] before recv\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + // int iPeek = socket->peek(&messageType, sizeof(messageType)); + // if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPeek,messageType,sizeof(messageType)); + // if(iPeek > 0) { + int bytesReceived = socket->receive(&messageType, sizeof(messageType), true); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] socket->getDataToRead() iPeek = %d, messageType = %d [size = %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, bytesReceived, messageType, sizeof(messageType)); + //} + } + //else { + // if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] PEEK WARNING, socket->getDataToRead() messageType = %d [size = %d], dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType,sizeof(messageType),dataSize); + //} + + if (socket->isSocketValid() == false) { + return nmtInvalid; + } + //sanity check new message type + if (messageType < 0 || messageType >= nmtCount) { + if (getConnectHasHandshaked() == true) { + throw megaglest_runtime_error("Invalid message type: " + intToStr(messageType)); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] Invalid message type = %d (no packet handshake yet so ignored)\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, messageType); + } + } + } + + return static_cast(messageType); + + } + + bool NetworkInterface::receiveMessage(NetworkMessage* networkMessage) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + Socket* socket = getSocket(false); + + return networkMessage->receive(socket); + } + + bool NetworkInterface::receiveMessage(NetworkMessage* networkMessage, NetworkMessageType type) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + Socket* socket = getSocket(false); + + return networkMessage->receive(socket, type); + } + + bool NetworkInterface::isConnected() { + bool result = (getSocket() != NULL && getSocket()->isConnected()); + return result; + } + + void NetworkInterface::setLastPingInfo(const NetworkMessagePing &ping) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + this->lastPingInfo = ping; + } + + void NetworkInterface::setLastPingInfoToNow() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + this->lastPingInfo.setPingReceivedLocalTime(time(NULL)); + } + + NetworkMessagePing NetworkInterface::getLastPingInfo() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + return lastPingInfo; + } + double NetworkInterface::getLastPingLag() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + return difftime((long int) time(NULL), lastPingInfo.getPingReceivedLocalTime()); + } + + void NetworkInterface::DisplayErrorMessage(string sErr, bool closeSocket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] sErr [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, sErr.c_str()); + //SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] sErr [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,sErr.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] sErr [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, sErr.c_str()); + + if (closeSocket == true && getSocket() != NULL) { + close(); + } + + if (pCB_DisplayMessage != NULL) { + pCB_DisplayMessage(sErr.c_str(), false); + } else { + throw megaglest_runtime_error(sErr); + } + } + + std::vector NetworkInterface::getChatTextList(bool clearList) { + std::vector result; + + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (chatTextList.empty() == false) { + result = chatTextList; + + if (clearList == true) { + chatTextList.clear(); + } + } + return result; + } + + void NetworkInterface::clearChatInfo() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (chatTextList.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] chatTextList.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chatTextList.size()); + chatTextList.clear(); + } + } + + std::vector NetworkInterface::getMarkedCellList(bool clearList) { + std::vector result; + + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (markedCellList.empty() == false) { + result = markedCellList; + + if (clearList == true) { + markedCellList.clear(); + } + } + return result; + } + + void NetworkInterface::clearMarkedCellList() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (markedCellList.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] markedCellList.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, markedCellList.size()); + markedCellList.clear(); + } + } + + std::vector NetworkInterface::getUnMarkedCellList(bool clearList) { + std::vector result; + + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (unmarkedCellList.empty() == false) { + result = unmarkedCellList; + + if (clearList == true) { + unmarkedCellList.clear(); + } + } + return result; + } + + void NetworkInterface::clearUnMarkedCellList() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (unmarkedCellList.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] unmarkedCellList.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, unmarkedCellList.size()); + unmarkedCellList.clear(); + } + } + + std::vector NetworkInterface::getHighlightedCellList(bool clearList) { + std::vector result; + + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (highlightedCellList.empty() == false) { + result = highlightedCellList; + + if (clearList == true) { + highlightedCellList.clear(); + } + } + return result; + } + + void NetworkInterface::clearHighlightedCellList() { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + if (highlightedCellList.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] markedCellList.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, markedCellList.size()); + highlightedCellList.clear(); + } + } + + void NetworkInterface::setHighlightedCell(const MarkedCell &msg) { + static string mutexOwnerId = string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(networkAccessMutex, mutexOwnerId); + + for (int idx = 0; idx < (int) highlightedCellList.size(); idx++) { + MarkedCell mc = highlightedCellList[idx]; + if (mc.getFactionIndex() == msg.getFactionIndex()) { + highlightedCellList.erase(highlightedCellList.begin() + idx); + break; + } + } + highlightedCellList.push_back(msg); + } + + float NetworkInterface::getThreadedPingMS(std::string host) { + float result = -1; + + if (getSocket() != NULL) { + result = getSocket()->getThreadedPingMS(host); + } + return result; + } + + // ===================================================== + // class GameNetworkInterface + // ===================================================== + + GameNetworkInterface::GameNetworkInterface() { + quit = false; + } + + void GameNetworkInterface::requestCommand(const NetworkCommand *networkCommand, bool insertAtStart) { + assert(networkCommand != NULL); + Mutex *mutex = getServerSynchAccessor(); + + if (insertAtStart == false) { + MutexSafeWrapper safeMutex(mutex, string(__FILE__) + "_" + intToStr(__LINE__)); + requestedCommands.push_back(*networkCommand); + } else { + MutexSafeWrapper safeMutex(mutex, string(__FILE__) + "_" + intToStr(__LINE__)); + requestedCommands.insert(requestedCommands.begin(), *networkCommand); + } + } + + // ===================================================== + // class FileTransferSocketThread + // ===================================================== + + const int32 SEND_FILE = 0x20; + const int32 ACK = 0x47; + + FileTransferSocketThread::FileTransferSocketThread(FileTransferInfo fileInfo) : info(fileInfo) { + this->info.serverPort += 100; + } + + void FileTransferSocketThread::execute() { + if (info.hostType == eServer) { + ServerSocket serverSocket; + serverSocket.bind(this->info.serverPort); + serverSocket.listen(1); + Socket *clientSocket = serverSocket.accept(); + + char data[513] = ""; + memset(data, 0, 256); + + clientSocket->receive(data, 256, true); + if (*data == SEND_FILE) { + FileInfo file; + + memcpy(&file, data + 1, sizeof(file)); + + *data = ACK; + clientSocket->send(data, 256); + + Checksum checksum; + checksum.addFile(file.fileName); + file.filecrc = checksum.getSum(); + + ifstream infile(file.fileName.c_str(), ios::in | ios::binary | ios::ate); + if (infile.is_open() == true) { + file.filesize = infile.tellg(); + infile.seekg(0, ios::beg); + + memset(data, 0, 256); + *data = SEND_FILE; + memcpy(data + 1, &file, sizeof(file)); + + clientSocket->send(data, 256); + clientSocket->receive(data, 256, true); + if (*data != ACK) { + //transfer error + } + + int remain = file.filesize % 512; + int packs = (file.filesize - remain) / 512; + + while (packs--) { + infile.read(data, 512); + //if(!ReadFile(file,data,512,&read,NULL)) + // ; //read error + //if(written!=pack) + // ; //read error + clientSocket->send(data, 512); + clientSocket->receive(data, 256, true); + if (*data != ACK) { + //transfer error + } + } + + infile.read(data, remain); + //if(!ReadFile(file,data,remain,&read,NULL)) + // ; //read error + //if(written!=pack) + // ; //read error + + clientSocket->send(data, remain); + clientSocket->receive(data, 256, true); + if (*data != ACK) { + //transfer error + } + + infile.close(); + } + } + + delete clientSocket; + } else { + Ip ip(this->info.serverIP); + ClientSocket clientSocket; + clientSocket.connect(this->info.serverIP, this->info.serverPort); + + if (clientSocket.isConnected() == true) { + FileInfo file; + file.fileName = this->info.fileName; + //file.filesize = + //file.filecrc = this->info. + + string path = extractDirectoryPathFromFile(file.fileName); + createDirectoryPaths(path); + ofstream outFile(file.fileName.c_str(), ios_base::binary | ios_base::out); + if (outFile.is_open() == true) { + char data[513] = ""; + memset(data, 0, 256); + *data = SEND_FILE; + memcpy(data + 1, &file, sizeof(file)); + + clientSocket.send(data, 256); + clientSocket.receive(data, 256, true); + if (*data != ACK) { + //transfer error + } + + clientSocket.receive(data, 256, true); + if (*data == SEND_FILE) { + memcpy(&file, data + 1, sizeof(file)); + *data = ACK; + clientSocket.send(data, 256); + + int remain = file.filesize % 512; + int packs = (file.filesize - remain) / 512; + + while (packs--) { + clientSocket.receive(data, 512, true); + + outFile.write(data, 512); + if (outFile.bad()) { + //int ii = 0; + } + //if(!WriteFile(file,data,512,&written,NULL)) + // ; //write error + //if(written != pack) + // ; //write error + *data = ACK; + clientSocket.send(data, 256); + } + clientSocket.receive(data, remain, true); + + outFile.write(data, remain); + if (outFile.bad()) { + //int ii = 0; + } + + //if(!WriteFile(file,data,remain,&written,NULL)) + // ; //write error + //if(written!=pack) + // ; //write error + *data = ACK; + clientSocket.send(data, 256); + + Checksum checksum; + checksum.addFile(file.fileName); + uint32 crc = checksum.getSum(); + if (file.filecrc != crc) { + //int ii = 0; + } + + //if(calc_crc(file)!=info.crc) + // ; //transfeer error + } + + outFile.close(); + } + } + } + } + + } - highlightedCellList.push_back(msg); -} - -float NetworkInterface::getThreadedPingMS(std::string host) { - float result = -1; - - if(getSocket() != NULL) { - result = getSocket()->getThreadedPingMS(host); - } - return result; -} - -// ===================================================== -// class GameNetworkInterface -// ===================================================== - -GameNetworkInterface::GameNetworkInterface(){ - quit= false; -} - -void GameNetworkInterface::requestCommand(const NetworkCommand *networkCommand, bool insertAtStart) { - assert(networkCommand != NULL); - Mutex *mutex = getServerSynchAccessor(); - - if(insertAtStart == false) { - MutexSafeWrapper safeMutex(mutex,string(__FILE__) + "_" + intToStr(__LINE__)); - requestedCommands.push_back(*networkCommand); - } - else { - MutexSafeWrapper safeMutex(mutex,string(__FILE__) + "_" + intToStr(__LINE__)); - requestedCommands.insert(requestedCommands.begin(),*networkCommand); - } -} - -// ===================================================== -// class FileTransferSocketThread -// ===================================================== - -const int32 SEND_FILE = 0x20; -const int32 ACK = 0x47; - -FileTransferSocketThread::FileTransferSocketThread(FileTransferInfo fileInfo) : info(fileInfo) { - this->info.serverPort += 100; -} - -void FileTransferSocketThread::execute() -{ - if(info.hostType == eServer) - { - ServerSocket serverSocket; - serverSocket.bind(this->info.serverPort); - serverSocket.listen(1); - Socket *clientSocket = serverSocket.accept(); - - char data[513]=""; - memset(data, 0, 256); - - clientSocket->receive(data,256, true); - if(*data == SEND_FILE) - { - FileInfo file; - - memcpy(&file, data+1, sizeof(file)); - - *data=ACK; - clientSocket->send(data,256); - - Checksum checksum; - checksum.addFile(file.fileName); - file.filecrc = checksum.getSum(); - - ifstream infile(file.fileName.c_str(), ios::in | ios::binary | ios::ate); - if(infile.is_open() == true) - { - file.filesize = infile.tellg(); - infile.seekg (0, ios::beg); - - memset(data, 0, 256); - *data=SEND_FILE; - memcpy(data+1,&file,sizeof(file)); - - clientSocket->send(data,256); - clientSocket->receive(data,256, true); - if(*data != ACK) { - //transfer error - } - - int remain=file.filesize % 512 ; - int packs=(file.filesize-remain)/512; - - while(packs--) - { - infile.read(data,512); - //if(!ReadFile(file,data,512,&read,NULL)) - // ; //read error - //if(written!=pack) - // ; //read error - clientSocket->send(data,512); - clientSocket->receive(data,256, true); - if(*data!=ACK) { - //transfer error - } - } - - infile.read(data,remain); - //if(!ReadFile(file,data,remain,&read,NULL)) - // ; //read error - //if(written!=pack) - // ; //read error - - clientSocket->send(data,remain); - clientSocket->receive(data,256, true); - if(*data!=ACK) { - //transfer error - } - - infile.close(); - } - } - - delete clientSocket; - } - else - { - Ip ip(this->info.serverIP); - ClientSocket clientSocket; - clientSocket.connect(this->info.serverIP, this->info.serverPort); - - if(clientSocket.isConnected() == true) - { - FileInfo file; - file.fileName = this->info.fileName; - //file.filesize = - //file.filecrc = this->info. - - string path = extractDirectoryPathFromFile(file.fileName); - createDirectoryPaths(path); - ofstream outFile(file.fileName.c_str(), ios_base::binary | ios_base::out); - if(outFile.is_open() == true) - { - char data[513]=""; - memset(data, 0, 256); - *data=SEND_FILE; - memcpy(data+1,&file,sizeof(file)); - - clientSocket.send(data,256); - clientSocket.receive(data,256, true); - if(*data!=ACK) { - //transfer error - } - - clientSocket.receive(data,256,true); - if(*data == SEND_FILE) - { - memcpy(&file, data+1, sizeof(file)); - *data=ACK; - clientSocket.send(data,256); - - int remain = file.filesize % 512 ; - int packs = (file.filesize-remain) / 512; - - while(packs--) - { - clientSocket.receive(data,512,true); - - outFile.write(data, 512); - if(outFile.bad()) - { - //int ii = 0; - } - //if(!WriteFile(file,data,512,&written,NULL)) - // ; //write error - //if(written != pack) - // ; //write error - *data=ACK; - clientSocket.send(data,256); - } - clientSocket.receive(data,remain,true); - - outFile.write(data, remain); - if(outFile.bad()) - { - //int ii = 0; - } - - //if(!WriteFile(file,data,remain,&written,NULL)) - // ; //write error - //if(written!=pack) - // ; //write error - *data=ACK; - clientSocket.send(data,256); - - Checksum checksum; - checksum.addFile(file.fileName); - uint32 crc = checksum.getSum(); - if(file.filecrc != crc) - { - //int ii = 0; - } - - //if(calc_crc(file)!=info.crc) - // ; //transfeer error - } - - outFile.close(); - } - } - } -} - - -}}//end namespace +}//end namespace diff --git a/source/glest_game/network/network_interface.h b/source/glest_game/network/network_interface.h index 939097179..8b878e063 100644 --- a/source/glest_game/network/network_interface.h +++ b/source/glest_game/network/network_interface.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_NETWORKINTERFACE_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -34,389 +34,464 @@ using Shared::Util::Checksum; using namespace Shared::Util; using namespace Shared::Platform; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class NetworkInterface -// ===================================================== + // ===================================================== + // class NetworkInterface + // ===================================================== -// -// This interface describes the methods to notify when a client is lagging -// -class ClientLagCallbackInterface { -public: - virtual bool clientLagHandler(int slotIndex, bool networkPauseGameForLaggedClients) = 0; + // + // This interface describes the methods to notify when a client is lagging + // + class ClientLagCallbackInterface { + public: + virtual bool clientLagHandler(int slotIndex, bool networkPauseGameForLaggedClients) = 0; - virtual ~ClientLagCallbackInterface() {} -}; + virtual ~ClientLagCallbackInterface() { + } + }; -class ChatMsgInfo { + class ChatMsgInfo { + + protected: + + void copyAll(const ChatMsgInfo &obj) { + this->chatText = obj.chatText.c_str(); + this->chatTeamIndex = obj.chatTeamIndex; + this->chatPlayerIndex = obj.chatPlayerIndex; + this->targetLanguage = obj.targetLanguage; + } + public: + + ChatMsgInfo() { + this->chatText = ""; + this->chatTeamIndex = -1; + this->chatPlayerIndex = -1; + this->targetLanguage = ""; + } + ChatMsgInfo(string chatText, int chatTeamIndex, int chatPlayerIndex, + string targetLanguage) { + this->chatText = chatText; + this->chatTeamIndex = chatTeamIndex; + this->chatPlayerIndex = chatPlayerIndex; + this->targetLanguage = targetLanguage; + } + ChatMsgInfo(const ChatMsgInfo& obj) { + copyAll(obj); + } + ChatMsgInfo & operator=(const ChatMsgInfo & obj) { + copyAll(obj); + return *this; + } + + string chatText; + int chatTeamIndex; + int chatPlayerIndex; + string targetLanguage; + + }; + + class MarkedCell { + protected: + Vec2i targetPos; + const Faction *faction; + int factionIndex; + int playerIndex; + string note; + int aliveCount; + + public: + static Vec3f static_system_marker_color; + + MarkedCell() { + faction = NULL; + factionIndex = -1; + playerIndex = -1; + note = ""; + aliveCount = 200; + } + MarkedCell(Vec2i targetPos, const Faction *faction, string note, int playerIndex) { + this->targetPos = targetPos; + this->faction = faction; + this->factionIndex = -1; + this->playerIndex = playerIndex; + this->note = note; + aliveCount = 200; + } + MarkedCell(Vec2i targetPos, int factionIndex, string note, int playerIndex) { + this->targetPos = targetPos; + this->faction = NULL; + this->factionIndex = factionIndex; + this->playerIndex = playerIndex; + this->note = note; + aliveCount = 200; + } + + Vec2i getTargetPos() const { + return targetPos; + } + const Faction * getFaction() const { + return faction; + } + void setFaction(const Faction *faction) { + this->faction = faction; + } + int getFactionIndex() const { + return factionIndex; + } + string getNote() const { + return note; + } + void decrementAliveCount() { + this->aliveCount--; + } + int getAliveCount() const { + return aliveCount; + } + void setAliveCount(int value) { + this->aliveCount = value; + } + int getPlayerIndex() const { + return playerIndex; + } + + void setNote(string value) { + note = value; + } + void setPlayerIndex(int value) { + playerIndex = value; + } + }; + + class UnMarkedCell { + protected: + Vec2i targetPos; + const Faction *faction; + int factionIndex; + + public: + UnMarkedCell() { + faction = NULL; + factionIndex = -1; + } + UnMarkedCell(Vec2i targetPos, const Faction *faction) { + this->targetPos = targetPos; + this->faction = faction; + this->factionIndex = -1; + } + UnMarkedCell(Vec2i targetPos, int factionIndex) { + this->targetPos = targetPos; + this->faction = NULL; + this->factionIndex = factionIndex; + } + + Vec2i getTargetPos() const { + return targetPos; + } + const Faction * getFaction() const { + return faction; + } + void setFaction(const Faction *faction) { + this->faction = faction; + } + int getFactionIndex() const { + return factionIndex; + } + }; + + typedef int(*DisplayMessageFunction)(const char *msg, bool exit); + + class NetworkInterface { + + protected: + static bool allowGameDataSynchCheck; + static bool allowDownloadDataSynch; + bool networkGameDataSynchCheckOkMap; + bool networkGameDataSynchCheckOkTile; + bool networkGameDataSynchCheckOkTech; + string networkGameDataSynchCheckTechMismatchReport; + bool receivedDataSynchCheck; + + NetworkMessagePing lastPingInfo; + + std::vector chatTextList; + std::vector markedCellList; + std::vector unmarkedCellList; + + static DisplayMessageFunction pCB_DisplayMessage; + void DisplayErrorMessage(string sErr, bool closeSocket = true); + + virtual Mutex * getServerSynchAccessor() = 0; + + std::vector highlightedCellList; + + Mutex *networkAccessMutex; + + void init(); + + Mutex *networkPlayerFactionCRCMutex; + uint32 networkPlayerFactionCRC[GameConstants::maxPlayers]; + + public: + static const int readyWaitTimeout; + GameSettings gameSettings; + + public: + NetworkInterface(); + virtual ~NetworkInterface(); + + NetworkInterface(const NetworkInterface& obj) { + init(); + throw megaglest_runtime_error("class NetworkInterface is NOT safe to copy!"); + } + NetworkInterface & operator=(const NetworkInterface& obj) { + init(); + throw megaglest_runtime_error("class NetworkInterface is NOT safe to assign!"); + } + + uint32 getNetworkPlayerFactionCRC(int index); + void setNetworkPlayerFactionCRC(int index, uint32 crc); + + virtual Socket* getSocket(bool mutexLock = true) = 0; + + virtual void close() = 0; + virtual string getHumanPlayerName(int index = -1) = 0; + virtual int getHumanPlayerIndex() const = 0; + + static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { + pCB_DisplayMessage = pDisplayMessage; + } + static DisplayMessageFunction getDisplayMessageFunction() { + return pCB_DisplayMessage; + } + + virtual std::string getIpAddress(bool mutexLock = true) = 0; + string getIp() const { + return Socket::getIp(); + } + string getHostName() const { + return Socket::getHostName(); + } + + virtual void sendMessage(NetworkMessage* networkMessage); + NetworkMessageType getNextMessageType(int waitMilliseconds = 0); + bool receiveMessage(NetworkMessage* networkMessage); + bool receiveMessage(NetworkMessage* networkMessage, NetworkMessageType type); + + virtual bool isConnected(); + + const virtual GameSettings * getGameSettings() { + return &gameSettings; + } + GameSettings * getGameSettingsPtr() { + return &gameSettings; + } + + static void setAllowDownloadDataSynch(bool value) { + allowDownloadDataSynch = value; + } + static bool getAllowDownloadDataSynch() { + return allowDownloadDataSynch; + } + + static void setAllowGameDataSynchCheck(bool value) { + allowGameDataSynchCheck = value; + } + static bool getAllowGameDataSynchCheck() { + return allowGameDataSynchCheck; + } + + virtual bool getNetworkGameDataSynchCheckOk() { + return (networkGameDataSynchCheckOkMap && networkGameDataSynchCheckOkTile && networkGameDataSynchCheckOkTech); + } + virtual void setNetworkGameDataSynchCheckOkMap(bool value) { + networkGameDataSynchCheckOkMap = value; + } + virtual void setNetworkGameDataSynchCheckOkTile(bool value) { + networkGameDataSynchCheckOkTile = value; + } + virtual void setNetworkGameDataSynchCheckOkTech(bool value) { + networkGameDataSynchCheckOkTech = value; + } + virtual bool getNetworkGameDataSynchCheckOkMap() { + return networkGameDataSynchCheckOkMap; + } + virtual bool getNetworkGameDataSynchCheckOkTile() { + return networkGameDataSynchCheckOkTile; + } + virtual bool getNetworkGameDataSynchCheckOkTech() { + return networkGameDataSynchCheckOkTech; + } + + std::vector getChatTextList(bool clearList); + void clearChatInfo(); + void addChatInfo(const ChatMsgInfo &msg); + + std::vector getMarkedCellList(bool clearList); + void clearMarkedCellList(); + void addMarkedCell(const MarkedCell &msg); + + std::vector getUnMarkedCellList(bool clearList); + void clearUnMarkedCellList(); + void addUnMarkedCell(const UnMarkedCell &msg); + + std::vector getHighlightedCellList(bool clearList); + void clearHighlightedCellList(); + void setHighlightedCell(const MarkedCell &msg); + + virtual bool getConnectHasHandshaked() const = 0; + + void setLastPingInfo(const NetworkMessagePing &ping); + void setLastPingInfoToNow(); + NetworkMessagePing getLastPingInfo(); + double getLastPingLag(); + + float getThreadedPingMS(std::string host); + + string getNetworkGameDataSynchCheckTechMismatchReport() const { + return networkGameDataSynchCheckTechMismatchReport; + } + void setNetworkGameDataSynchCheckTechMismatchReport(string value) { + networkGameDataSynchCheckTechMismatchReport = value; + } + + bool getReceivedDataSynchCheck() const { + return receivedDataSynchCheck; + } + void setReceivedDataSynchCheck(bool value) { + receivedDataSynchCheck = value; + } + + virtual void saveGame(XmlNode *rootNode) = 0; + //static void loadGame(string name); + + }; + + // ===================================================== + // class GameNetworkInterface + // + // Adds functions common to servers and clients + // but not connection slots + // ===================================================== + + class GameNetworkInterface : public NetworkInterface { + + protected: + typedef vector Commands; + + Commands requestedCommands; //commands requested by the user + Commands pendingCommands; //commands ready to be given + bool quit; + + public: + GameNetworkInterface(); + virtual ~GameNetworkInterface() { + } + + //message processimg + virtual void update() = 0; + virtual void updateLobby() = 0; + virtual void updateKeyframe(int frameCount) = 0; + virtual void setKeyframe(int frameCount) = 0; + virtual void waitUntilReady(Checksum* checksum) = 0; + + //message sending + virtual void sendTextMessage(const string &text, int teamIndex, bool echoLocal, + string targetLanguage) = 0; + virtual void quitGame(bool userManuallyQuit) = 0; + + virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex) = 0; + virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) = 0; + virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex) = 0; + + + //misc + virtual string getNetworkStatus() = 0; + + //access functions + void requestCommand(const NetworkCommand *networkCommand, bool insertAtStart = false); + int getPendingCommandCount() const { + return (int) pendingCommands.size(); + } + NetworkCommand* getPendingCommand(int i) { + return &pendingCommands[i]; + } + void clearPendingCommands() { + pendingCommands.clear(); + } + bool getQuit() const { + return quit; + } + }; + + // ===================================================== + // class FileTransferSocketThread + // ===================================================== + + enum FileTransferHostType { + eClient, + eServer + }; + + enum FileTransferOperationType { + eSend, + eReceive + }; + + class FileTransferInfo { + private: + + void CopyAll(const FileTransferInfo &obj) { + hostType = obj.hostType; + serverIP = obj.serverIP; + serverPort = obj.serverPort; + opType = obj.opType; + fileName = obj.fileName; + } + + public: + FileTransferInfo() { + hostType = eClient; + serverIP = ""; + serverPort = 0; + opType = eSend; + fileName = ""; + } + FileTransferInfo(const FileTransferInfo &obj) { + CopyAll(obj); + } + FileTransferInfo &operator=(const FileTransferInfo &obj) { + CopyAll(obj); + return *this; + } + + FileTransferHostType hostType; + string serverIP; + int32 serverPort; + FileTransferOperationType opType; + string fileName; + }; + + class FileInfo { + public: + string fileName; + int64 filesize; + uint32 filecrc; + }; + + class FileTransferSocketThread : public Thread { + private: + FileTransferInfo info; + + public: + explicit FileTransferSocketThread(FileTransferInfo fileInfo); + virtual void execute(); + }; -protected: - void copyAll(const ChatMsgInfo &obj) { - this->chatText = obj.chatText.c_str(); - this->chatTeamIndex = obj.chatTeamIndex; - this->chatPlayerIndex = obj.chatPlayerIndex; - this->targetLanguage = obj.targetLanguage; } -public: - - ChatMsgInfo() { - this->chatText = ""; - this->chatTeamIndex = -1; - this->chatPlayerIndex = -1; - this->targetLanguage = ""; - } - ChatMsgInfo(string chatText, int chatTeamIndex, int chatPlayerIndex, - string targetLanguage) { - this->chatText = chatText; - this->chatTeamIndex = chatTeamIndex; - this->chatPlayerIndex = chatPlayerIndex; - this->targetLanguage = targetLanguage; - } - ChatMsgInfo(const ChatMsgInfo& obj) { - copyAll(obj); - } - ChatMsgInfo & operator=(const ChatMsgInfo & obj) { - copyAll(obj); - return *this; - } - - string chatText; - int chatTeamIndex; - int chatPlayerIndex; - string targetLanguage; - -}; - -class MarkedCell { -protected: - Vec2i targetPos; - const Faction *faction; - int factionIndex; - int playerIndex; - string note; - int aliveCount; - -public: - static Vec3f static_system_marker_color; - - MarkedCell() { - faction = NULL; - factionIndex = -1; - playerIndex = -1; - note = ""; - aliveCount=200; - } - MarkedCell(Vec2i targetPos,const Faction *faction,string note, int playerIndex) { - this->targetPos = targetPos; - this->faction = faction; - this->factionIndex = -1; - this->playerIndex = playerIndex; - this->note = note; - aliveCount=200; - } - MarkedCell(Vec2i targetPos,int factionIndex,string note, int playerIndex) { - this->targetPos = targetPos; - this->faction = NULL; - this->factionIndex = factionIndex; - this->playerIndex = playerIndex; - this->note = note; - aliveCount=200; - } - - Vec2i getTargetPos() const { return targetPos; } - const Faction * getFaction() const { return faction; } - void setFaction(const Faction *faction) { this->faction = faction; } - int getFactionIndex() const { return factionIndex; } - string getNote() const { return note; } - void decrementAliveCount() { this->aliveCount--; } - int getAliveCount() const { return aliveCount; } - void setAliveCount(int value) { this->aliveCount = value; } - int getPlayerIndex() const { return playerIndex; } - - void setNote(string value) { note = value; } - void setPlayerIndex(int value) { playerIndex = value; } -}; - -class UnMarkedCell { -protected: - Vec2i targetPos; - const Faction *faction; - int factionIndex; - -public: - UnMarkedCell() { - faction = NULL; - factionIndex = -1; - } - UnMarkedCell(Vec2i targetPos,const Faction *faction) { - this->targetPos = targetPos; - this->faction = faction; - this->factionIndex = -1; - } - UnMarkedCell(Vec2i targetPos,int factionIndex) { - this->targetPos = targetPos; - this->faction = NULL; - this->factionIndex = factionIndex; - } - - Vec2i getTargetPos() const { return targetPos; } - const Faction * getFaction() const { return faction; } - void setFaction(const Faction *faction) { this->faction = faction; } - int getFactionIndex() const { return factionIndex; } -}; - -typedef int (*DisplayMessageFunction)(const char *msg, bool exit); - -class NetworkInterface { - -protected: - static bool allowGameDataSynchCheck; - static bool allowDownloadDataSynch; - bool networkGameDataSynchCheckOkMap; - bool networkGameDataSynchCheckOkTile; - bool networkGameDataSynchCheckOkTech; - string networkGameDataSynchCheckTechMismatchReport; - bool receivedDataSynchCheck; - - NetworkMessagePing lastPingInfo; - - std::vector chatTextList; - std::vector markedCellList; - std::vector unmarkedCellList; - - static DisplayMessageFunction pCB_DisplayMessage; - void DisplayErrorMessage(string sErr, bool closeSocket=true); - - virtual Mutex * getServerSynchAccessor() = 0; - - std::vector highlightedCellList; - - Mutex *networkAccessMutex; - - void init(); - - Mutex *networkPlayerFactionCRCMutex; - uint32 networkPlayerFactionCRC[GameConstants::maxPlayers]; - -public: - static const int readyWaitTimeout; - GameSettings gameSettings; - -public: - NetworkInterface(); - virtual ~NetworkInterface(); - - NetworkInterface(const NetworkInterface& obj) { - init(); - throw megaglest_runtime_error("class NetworkInterface is NOT safe to copy!"); - } - NetworkInterface & operator=(const NetworkInterface& obj) { - init(); - throw megaglest_runtime_error("class NetworkInterface is NOT safe to assign!"); - } - - uint32 getNetworkPlayerFactionCRC(int index); - void setNetworkPlayerFactionCRC(int index, uint32 crc); - - virtual Socket* getSocket(bool mutexLock=true)= 0; - - virtual void close()= 0; - virtual string getHumanPlayerName(int index=-1) = 0; - virtual int getHumanPlayerIndex() const = 0; - - static void setDisplayMessageFunction(DisplayMessageFunction pDisplayMessage) { pCB_DisplayMessage = pDisplayMessage; } - static DisplayMessageFunction getDisplayMessageFunction() { return pCB_DisplayMessage; } - - virtual std::string getIpAddress(bool mutexLock=true) = 0; - string getIp() const {return Socket::getIp();} - string getHostName() const {return Socket::getHostName();} - - virtual void sendMessage(NetworkMessage* networkMessage); - NetworkMessageType getNextMessageType(int waitMilliseconds=0); - bool receiveMessage(NetworkMessage* networkMessage); - bool receiveMessage(NetworkMessage* networkMessage, NetworkMessageType type); - - virtual bool isConnected(); - - const virtual GameSettings * getGameSettings() { return &gameSettings; } - GameSettings * getGameSettingsPtr() { return &gameSettings; } - - static void setAllowDownloadDataSynch(bool value) { allowDownloadDataSynch = value; } - static bool getAllowDownloadDataSynch() { return allowDownloadDataSynch; } - - static void setAllowGameDataSynchCheck(bool value) { allowGameDataSynchCheck = value; } - static bool getAllowGameDataSynchCheck() { return allowGameDataSynchCheck; } - - virtual bool getNetworkGameDataSynchCheckOk() { return (networkGameDataSynchCheckOkMap && networkGameDataSynchCheckOkTile && networkGameDataSynchCheckOkTech); } - virtual void setNetworkGameDataSynchCheckOkMap(bool value) { networkGameDataSynchCheckOkMap = value; } - virtual void setNetworkGameDataSynchCheckOkTile(bool value) { networkGameDataSynchCheckOkTile = value; } - virtual void setNetworkGameDataSynchCheckOkTech(bool value) { networkGameDataSynchCheckOkTech = value; } - virtual bool getNetworkGameDataSynchCheckOkMap() { return networkGameDataSynchCheckOkMap; } - virtual bool getNetworkGameDataSynchCheckOkTile() { return networkGameDataSynchCheckOkTile; } - virtual bool getNetworkGameDataSynchCheckOkTech() { return networkGameDataSynchCheckOkTech; } - - std::vector getChatTextList(bool clearList); - void clearChatInfo(); - void addChatInfo(const ChatMsgInfo &msg); - - std::vector getMarkedCellList(bool clearList); - void clearMarkedCellList(); - void addMarkedCell(const MarkedCell &msg); - - std::vector getUnMarkedCellList(bool clearList); - void clearUnMarkedCellList(); - void addUnMarkedCell(const UnMarkedCell &msg); - - std::vector getHighlightedCellList(bool clearList); - void clearHighlightedCellList(); - void setHighlightedCell(const MarkedCell &msg); - - virtual bool getConnectHasHandshaked() const= 0; - - void setLastPingInfo(const NetworkMessagePing &ping); - void setLastPingInfoToNow(); - NetworkMessagePing getLastPingInfo(); - double getLastPingLag(); - - float getThreadedPingMS(std::string host); - - string getNetworkGameDataSynchCheckTechMismatchReport() const {return networkGameDataSynchCheckTechMismatchReport;} - void setNetworkGameDataSynchCheckTechMismatchReport(string value) {networkGameDataSynchCheckTechMismatchReport = value;} - - bool getReceivedDataSynchCheck() const {return receivedDataSynchCheck;} - void setReceivedDataSynchCheck(bool value) { receivedDataSynchCheck = value; } - - virtual void saveGame(XmlNode *rootNode) = 0; - //static void loadGame(string name); - -}; - -// ===================================================== -// class GameNetworkInterface -// -// Adds functions common to servers and clients -// but not connection slots -// ===================================================== - -class GameNetworkInterface: public NetworkInterface { - -protected: - typedef vector Commands; - - Commands requestedCommands; //commands requested by the user - Commands pendingCommands; //commands ready to be given - bool quit; - -public: - GameNetworkInterface(); - virtual ~GameNetworkInterface(){} - - //message processimg - virtual void update()= 0; - virtual void updateLobby()= 0; - virtual void updateKeyframe(int frameCount)= 0; - virtual void setKeyframe(int frameCount)= 0; - virtual void waitUntilReady(Checksum* checksum)= 0; - - //message sending - virtual void sendTextMessage(const string &text, int teamIndex,bool echoLocal, - string targetLanguage)= 0; - virtual void quitGame(bool userManuallyQuit)=0; - - virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note,int playerIndex) = 0; - virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) = 0; - virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex) = 0; - - - //misc - virtual string getNetworkStatus() = 0; - - //access functions - void requestCommand(const NetworkCommand *networkCommand, bool insertAtStart=false); - int getPendingCommandCount() const {return (int)pendingCommands.size();} - NetworkCommand* getPendingCommand(int i) {return &pendingCommands[i];} - void clearPendingCommands() {pendingCommands.clear();} - bool getQuit() const {return quit;} -}; - -// ===================================================== -// class FileTransferSocketThread -// ===================================================== - -enum FileTransferHostType -{ - eClient, - eServer -}; - -enum FileTransferOperationType -{ - eSend, - eReceive -}; - -class FileTransferInfo -{ -private: - - void CopyAll(const FileTransferInfo &obj) - { - hostType = obj.hostType; - serverIP = obj.serverIP; - serverPort = obj.serverPort; - opType = obj.opType; - fileName = obj.fileName; - } - -public: - FileTransferInfo() - { - hostType = eClient; - serverIP = ""; - serverPort = 0; - opType = eSend; - fileName = ""; - } - FileTransferInfo(const FileTransferInfo &obj) - { - CopyAll(obj); - } - FileTransferInfo &operator=(const FileTransferInfo &obj) - { - CopyAll(obj); - return *this; - } - - FileTransferHostType hostType; - string serverIP; - int32 serverPort; - FileTransferOperationType opType; - string fileName; -}; - -class FileInfo -{ -public: - string fileName; - int64 filesize; - uint32 filecrc; -}; - -class FileTransferSocketThread : public Thread -{ -private: - FileTransferInfo info; - -public: - explicit FileTransferSocketThread(FileTransferInfo fileInfo); - virtual void execute(); -}; - - -}}//end namespace +}//end namespace #endif diff --git a/source/glest_game/network/network_manager.cpp b/source/glest_game/network/network_manager.cpp index 4b24ce11c..42d8df954 100644 --- a/source/glest_game/network/network_manager.cpp +++ b/source/glest_game/network/network_manager.cpp @@ -15,126 +15,127 @@ using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class NetworkManager -// ===================================================== + // ===================================================== + // class NetworkManager + // ===================================================== -NetworkManager &NetworkManager::getInstance(){ - static NetworkManager networkManager; - return networkManager; -} + NetworkManager &NetworkManager::getInstance() { + static NetworkManager networkManager; + return networkManager; + } -NetworkManager::NetworkManager() { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + NetworkManager::NetworkManager() { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); - gameNetworkInterface= NULL; - networkRole= nrIdle; + gameNetworkInterface = NULL; + networkRole = nrIdle; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); -} + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, gameNetworkInterface); + } -NetworkManager::~NetworkManager() { - end(); -} + NetworkManager::~NetworkManager() { + end(); + } -void NetworkManager::init(NetworkRole networkRole, bool publishEnabled) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); + void NetworkManager::init(NetworkRole networkRole, bool publishEnabled) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, networkRole, gameNetworkInterface); - //assert(gameNetworkInterface==NULL); - NetworkMessage::resetNetworkPacketStats(); - this->networkRole = networkRole; + //assert(gameNetworkInterface==NULL); + NetworkMessage::resetNetworkPacketStats(); + this->networkRole = networkRole; + + if (networkRole == nrServer) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, networkRole, gameNetworkInterface); + gameNetworkInterface = new ServerInterface(publishEnabled, NULL); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, networkRole, gameNetworkInterface); + gameNetworkInterface = new ClientInterface(); + } + + //printf("==========] CREATING gameNetworkInterface [%p]\n",gameNetworkInterface); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, gameNetworkInterface); + } + + void NetworkManager::initServerInterfaces(ClientLagCallbackInterface *intf) { + ServerInterface *server = getServerInterface(); + server->setClientLagCallbackInterface(intf); + } + + void NetworkManager::end() { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, gameNetworkInterface); + //printf("In [%s::%s] Line: %d gameNetworkInterface = %p\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface); + //printf("==========] DELETING gameNetworkInterface [%p]\n",gameNetworkInterface); + + delete gameNetworkInterface; + gameNetworkInterface = NULL; + networkRole = nrIdle; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n", __FILE__, __FUNCTION__, __LINE__, this->networkRole, gameNetworkInterface); + } + + void NetworkManager::update() { + if (gameNetworkInterface != NULL) { + gameNetworkInterface->update(); + } + } + + bool NetworkManager::isNetworkGame() { + return networkRole == nrClient || (networkRole == nrServer && getServerInterface()->getSlotCount() > 0); + } + + bool NetworkManager::isNetworkGameWithConnectedClients() { + return networkRole == nrClient || (networkRole == nrServer && getServerInterface()->getConnectedSlotCount(true) > 0); + } + + GameNetworkInterface* NetworkManager::getGameNetworkInterface(bool throwErrorOnNull) { + if (throwErrorOnNull) { + //assert(gameNetworkInterface!=NULL); + + if (gameNetworkInterface == NULL) { + throw megaglest_runtime_error("gameNetworkInterface==NULL"); + } + } + return gameNetworkInterface; + } + + ServerInterface* NetworkManager::getServerInterface(bool throwErrorOnNull) { + if (throwErrorOnNull) { + //assert(gameNetworkInterface!=NULL); + if (gameNetworkInterface == NULL) { + throw megaglest_runtime_error("gameNetworkInterface==NULL"); + } + + assert(networkRole == nrServer); + if (networkRole != nrServer) { + throw megaglest_runtime_error("networkRole!=nrServer"); + } + } + return dynamic_cast(gameNetworkInterface); + } + + ClientInterface* NetworkManager::getClientInterface(bool throwErrorOnNull) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); + + if (throwErrorOnNull) { + //assert(gameNetworkInterface!=NULL); + if (gameNetworkInterface == NULL) { + throw megaglest_runtime_error("gameNetworkInterface==NULL"); + } + + assert(networkRole == nrClient); + if (networkRole != nrClient) { + throw megaglest_runtime_error("networkRole!=nrClient"); + } + } + return dynamic_cast(gameNetworkInterface); + } - if(networkRole == nrServer) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); - gameNetworkInterface = new ServerInterface(publishEnabled,NULL); } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d, networkRole = %d, gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,networkRole,gameNetworkInterface); - gameNetworkInterface = new ClientInterface(); - } - - //printf("==========] CREATING gameNetworkInterface [%p]\n",gameNetworkInterface); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); -} - -void NetworkManager::initServerInterfaces(ClientLagCallbackInterface *intf) { - ServerInterface *server = getServerInterface(); - server->setClientLagCallbackInterface(intf); -} - -void NetworkManager::end() { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); - //printf("In [%s::%s] Line: %d gameNetworkInterface = %p\n",__FILE__,__FUNCTION__,__LINE__,gameNetworkInterface); - //printf("==========] DELETING gameNetworkInterface [%p]\n",gameNetworkInterface); - - delete gameNetworkInterface; - gameNetworkInterface= NULL; - networkRole= nrIdle; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); -} - -void NetworkManager::update() { - if(gameNetworkInterface!=NULL) { - gameNetworkInterface->update(); - } -} - -bool NetworkManager::isNetworkGame() { - return networkRole==nrClient || (networkRole==nrServer && getServerInterface()->getSlotCount() > 0); -} - -bool NetworkManager::isNetworkGameWithConnectedClients() { - return networkRole==nrClient || (networkRole==nrServer && getServerInterface()->getConnectedSlotCount(true) > 0); -} - -GameNetworkInterface* NetworkManager::getGameNetworkInterface(bool throwErrorOnNull) { - if(throwErrorOnNull) { - //assert(gameNetworkInterface!=NULL); - - if(gameNetworkInterface==NULL) { - throw megaglest_runtime_error("gameNetworkInterface==NULL"); - } - } - return gameNetworkInterface; -} - -ServerInterface* NetworkManager::getServerInterface(bool throwErrorOnNull) { - if(throwErrorOnNull) { - //assert(gameNetworkInterface!=NULL); - if(gameNetworkInterface==NULL) { - throw megaglest_runtime_error("gameNetworkInterface==NULL"); - } - - assert(networkRole==nrServer); - if(networkRole!=nrServer) { - throw megaglest_runtime_error("networkRole!=nrServer"); - } - } - return dynamic_cast(gameNetworkInterface); -} - -ClientInterface* NetworkManager::getClientInterface(bool throwErrorOnNull) { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] this->networkRole = %d gameNetworkInterface [%p]\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole,gameNetworkInterface); - - if(throwErrorOnNull) { - //assert(gameNetworkInterface!=NULL); - if(gameNetworkInterface==NULL) { - throw megaglest_runtime_error("gameNetworkInterface==NULL"); - } - - assert(networkRole==nrClient); - if(networkRole!=nrClient) { - throw megaglest_runtime_error("networkRole!=nrClient"); - } - } - return dynamic_cast(gameNetworkInterface); -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/network/network_manager.h b/source/glest_game/network/network_manager.h index 028c8e1de..e37f25a16 100644 --- a/source/glest_game/network/network_manager.h +++ b/source/glest_game/network/network_manager.h @@ -21,38 +21,42 @@ using Shared::Util::Checksum; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class NetworkManager -// ===================================================== + // ===================================================== + // class NetworkManager + // ===================================================== -class NetworkManager { -private: - GameNetworkInterface* gameNetworkInterface; - NetworkRole networkRole; + class NetworkManager { + private: + GameNetworkInterface * gameNetworkInterface; + NetworkRole networkRole; -public: - static NetworkManager &getInstance(); + public: + static NetworkManager &getInstance(); - NetworkManager(); - virtual ~NetworkManager(); + NetworkManager(); + virtual ~NetworkManager(); - void init(NetworkRole networkRole,bool publishEnabled=false); - void end(); - void update(); + void init(NetworkRole networkRole, bool publishEnabled = false); + void end(); + void update(); - bool isNetworkGame(); - bool isNetworkGameWithConnectedClients(); + bool isNetworkGame(); + bool isNetworkGameWithConnectedClients(); - GameNetworkInterface* getGameNetworkInterface(bool throwErrorOnNull=true); - ServerInterface* getServerInterface(bool throwErrorOnNull=true); - ClientInterface* getClientInterface(bool throwErrorOnNull=true); - NetworkRole getNetworkRole() const { return networkRole; } + GameNetworkInterface* getGameNetworkInterface(bool throwErrorOnNull = true); + ServerInterface* getServerInterface(bool throwErrorOnNull = true); + ClientInterface* getClientInterface(bool throwErrorOnNull = true); + NetworkRole getNetworkRole() const { + return networkRole; + } - void initServerInterfaces(ClientLagCallbackInterface *intf); -}; + void initServerInterfaces(ClientLagCallbackInterface *intf); + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 14eaee67b..8ff07fb98 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -42,1705 +42,1684 @@ using namespace Shared::Util; using namespace std; using std::min; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -bool NetworkMessage::useOldProtocol = true; + bool NetworkMessage::useOldProtocol = true; -auto_ptr NetworkMessage::mutexMessageStats(new Mutex(CODE_AT_LINE)); -Chrono NetworkMessage::statsTimer; -Chrono NetworkMessage::lastSend; -Chrono NetworkMessage::lastRecv; -std::map NetworkMessage::mapMessageStats; + auto_ptr NetworkMessage::mutexMessageStats(new Mutex(CODE_AT_LINE)); + Chrono NetworkMessage::statsTimer; + Chrono NetworkMessage::lastSend; + Chrono NetworkMessage::lastRecv; + std::map NetworkMessage::mapMessageStats; -// ===================================================== -// class NetworkMessage -// ===================================================== + // ===================================================== + // class NetworkMessage + // ===================================================== -bool NetworkMessage::receive(Socket* socket, void* data, int dataSize, bool tryReceiveUntilDataSizeMet) { - if(socket != NULL) { - int dataReceived = socket->receive(data, dataSize, tryReceiveUntilDataSizeMet); - if(dataReceived != dataSize) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING, dataReceived = %d dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataReceived,dataSize); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, dataReceived = %d dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataReceived,dataSize); + bool NetworkMessage::receive(Socket* socket, void* data, int dataSize, bool tryReceiveUntilDataSizeMet) { + if (socket != NULL) { + int dataReceived = socket->receive(data, dataSize, tryReceiveUntilDataSizeMet); + if (dataReceived != dataSize) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] WARNING, dataReceived = %d dataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, dataReceived, dataSize); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, dataReceived = %d dataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, dataReceived, dataSize); - if(socket != NULL && socket->isSocketValid() == true) { - throw megaglest_runtime_error("Error receiving NetworkMessage, dataReceived = " + intToStr(dataReceived) + ", dataSize = " + intToStr(dataSize)); + if (socket != NULL && socket->isSocketValid() == true) { + throw megaglest_runtime_error("Error receiving NetworkMessage, dataReceived = " + intToStr(dataReceived) + ", dataSize = " + intToStr(dataSize)); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] socket has been disconnected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] dataSize = %d, dataReceived = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, dataSize, dataReceived); + + dump_packet("\nINCOMING PACKET:\n", data, dataSize, false); + return true; + } } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket has been disconnected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] dataSize = %d, dataReceived = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,dataSize,dataReceived); - - dump_packet("\nINCOMING PACKET:\n",data, dataSize, false); - return true; - } - } - return false; - -} - -void NetworkMessage::send(Socket* socket, const void* data, int dataSize) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,socket,data,dataSize); - - if(socket != NULL) { - dump_packet("\nOUTGOING PACKET:\n",data, dataSize, true); - int sendResult = socket->send(data, dataSize); - if(sendResult != dataSize) { - if(socket != NULL && socket->isSocketValid() == true) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error sending NetworkMessage, sendResult = %d, dataSize = %d",sendResult,dataSize); - throw megaglest_runtime_error(szBuf); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d socket has been disconnected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - } -} - -void NetworkMessage::send(Socket* socket, const void* data, int dataSize, int8 messageType) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,socket,data,dataSize); - - if(socket != NULL) { - int msgTypeSize = sizeof(messageType); - int fullMsgSize = msgTypeSize + dataSize; - - char *out_buffer = new char[fullMsgSize]; - memcpy(out_buffer,&messageType,msgTypeSize); - memcpy(&out_buffer[msgTypeSize],(const char *)data,dataSize); - - dump_packet("\nOUTGOING PACKET:\n",out_buffer, fullMsgSize, true); - int sendResult = socket->send(out_buffer, fullMsgSize); - if(sendResult != fullMsgSize) { - delete [] out_buffer; - if(socket != NULL && socket->isSocketValid() == true) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error sending NetworkMessage, sendResult = %d, dataSize = %d",sendResult,fullMsgSize); - throw megaglest_runtime_error(szBuf); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d socket has been disconnected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - else { - delete [] out_buffer; - } - } -} - -void NetworkMessage::send(Socket* socket, const void* data, int dataSize, int8 messageType, uint32 compressedLength) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,socket,data,dataSize); - - if(socket != NULL) { - int msgTypeSize = sizeof(messageType); - int compressedSize = sizeof(compressedLength); - int fullMsgSize = msgTypeSize + compressedSize + dataSize; - - char *out_buffer = new char[fullMsgSize]; - memcpy(out_buffer,&messageType,msgTypeSize); - memcpy(&out_buffer[msgTypeSize],&compressedLength,compressedSize); - memcpy(&out_buffer[msgTypeSize+compressedSize],(const char *)data,dataSize); - - dump_packet("\nOUTGOING PACKET:\n",out_buffer, fullMsgSize, true); - int sendResult = socket->send(out_buffer, fullMsgSize); - if(sendResult != fullMsgSize) { - delete [] out_buffer; - if(socket != NULL && socket->isSocketValid() == true) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"Error sending NetworkMessage, sendResult = %d, dataSize = %d",sendResult,fullMsgSize); - throw megaglest_runtime_error(szBuf); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d socket has been disconnected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - else { - delete [] out_buffer; - } - } -} - -void NetworkMessage::resetNetworkPacketStats() { - NetworkMessage::statsTimer.stop(); - NetworkMessage::lastSend.stop(); - NetworkMessage::lastRecv.stop(); - NetworkMessage::mapMessageStats.clear(); -} - -string NetworkMessage::getNetworkPacketStats() { - string result = "Current Timer Milliseconds: " + intToStr(NetworkMessage::statsTimer.getMillis()) + "\n"; - - for(std::map::iterator iterMap = mapMessageStats.begin(); - iterMap != mapMessageStats.end(); ++iterMap) { - switch(iterMap->first) { - case netmsgstPacketsPerMillisecondSend: - result += "send p / msec: " + intToStr(iterMap->second) + (iterMap->second > 10 ? " ****WARNING WIN32 LIMIT EXCEEDED****" : "") + "\n"; - break; - case netmsgstPacketsPerSecondSend: - result += "send p / sec: " + intToStr(iterMap->second) + "\n"; - break; - case netmsgstAverageSendSize: - result += "send avg size: " + intToStr(iterMap->second) + "\n"; - break; - - case netmsgstPacketsPerMillisecondRecv: - result += "recv p / msec: " + intToStr(iterMap->second) + "\n"; - break; - case netmsgstPacketsPerSecondRecv: - result += "recv p / sec: " + intToStr(iterMap->second) + (iterMap->second > 10 ? " ****WARNING WIN32 LIMIT EXCEEDED****" : "") + "\n"; - break; - case netmsgstAverageRecvSize: - result += "recv avg size: " + intToStr(iterMap->second) + "\n"; - break; + return false; } - } - return result; -} -void NetworkMessage::dump_packet(string label, const void* data, int dataSize, bool isSend) { - Config &config = Config::getInstance(); - if( config.getBool("DebugNetworkPacketStats","false") == true) { + void NetworkMessage::send(Socket* socket, const void* data, int dataSize) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, socket, data, dataSize); - MutexSafeWrapper safeMutex(NetworkMessage::mutexMessageStats.get()); - - if(NetworkMessage::statsTimer.isStarted() == false) { - NetworkMessage::statsTimer.start(); - } - - bool secondChanged = false; - if(NetworkMessage::statsTimer.getSeconds() - NetworkMessage::mapMessageStats[netmsgstLastEvent] >= 3) { - - NetworkMessage::mapMessageStats[netmsgstLastEvent] = NetworkMessage::statsTimer.getSeconds(); - secondChanged = true; - } - - if(isSend == true) { - if(NetworkMessage::lastSend.isStarted() == false) { - NetworkMessage::lastSend.start(); - } - int64 millisecondsSinceLastSend = NetworkMessage::lastSend.getMillis(); - int64 secondsSinceLastSend = NetworkMessage::lastSend.getSeconds(); - - //char szBuf[8096]=""; - //snprintf(szBuf,8095,"\nSEND check cur [%lld] last [%lld]\n", (long long)millisecondsSinceLastSend,(long long)NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last] ); - //printf("%s",szBuf); - - if(millisecondsSinceLastSend == NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last]) { - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_current_count]++; - - } - else { - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last] = millisecondsSinceLastSend; - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend] = - (NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend] + - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_current_count]) / 2; - } - - if(secondsSinceLastSend == NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_last]) { - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_current_count]++; - - } - else { - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_last] = secondsSinceLastSend; - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend] = - (NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend] + - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_current_count]) / 2; - } - - NetworkMessage::mapMessageStats[netmsgstAverageSendSize] = - (NetworkMessage::mapMessageStats[netmsgstAverageSendSize] + - dataSize) / 2; - } - else { - if(NetworkMessage::lastRecv.isStarted() == false) { - NetworkMessage::lastRecv.start(); - } - int64 millisecondsSinceLastRecv = NetworkMessage::lastRecv.getMillis(); - int64 secondsSinceLastRecv = NetworkMessage::lastRecv.getSeconds(); - - //char szBuf[8096]=""; - //snprintf(szBuf,8095,"\nRECV check cur [%lld] last [%lld]\n", (long long)millisecondsSinceLastRecv,(long long)NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last] ); - //printf("%s",szBuf); - - if(millisecondsSinceLastRecv == NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last]) { - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_current_count]++; - - } - else { - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last] = millisecondsSinceLastRecv; - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv] = - (NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv] + - NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_current_count]) / 2; - } - - if(secondsSinceLastRecv == NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_last]) { - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_current_count]++; - - } - else { - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_last] = secondsSinceLastRecv; - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv] = - (NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv] + - NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_current_count]) / 2; - } - - NetworkMessage::mapMessageStats[netmsgstAverageRecvSize] = - (NetworkMessage::mapMessageStats[netmsgstAverageRecvSize] + - dataSize) / 2; - } - - if(secondChanged == true) { - printf("%s",NetworkMessage::getNetworkPacketStats().c_str()); - } - } - - if( config.getBool("DebugNetworkPackets","false") == true || - config.getBool("DebugNetworkPacketSizes","false") == true) { - - printf("%s DataSize = %d",label.c_str(),dataSize); - - if(config.getBool("DebugNetworkPackets","false") == true) { - - printf("\n"); - const char *buf = static_cast(data); - for(unsigned int index = 0; index < (unsigned int)dataSize; ++index) { - - printf("%u[%X][%d] ",index,buf[index],buf[index]); - if(index % 10 == 0) { - printf("\n"); + if (socket != NULL) { + dump_packet("\nOUTGOING PACKET:\n", data, dataSize, true); + int sendResult = socket->send(data, dataSize); + if (sendResult != dataSize) { + if (socket != NULL && socket->isSocketValid() == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error sending NetworkMessage, sendResult = %d, dataSize = %d", sendResult, dataSize); + throw megaglest_runtime_error(szBuf); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d socket has been disconnected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } } } } - printf("\n======= END =======\n"); - } -} -// ===================================================== -// class NetworkMessageIntro -// ===================================================== -NetworkMessageIntro::NetworkMessageIntro() { - messageType= -1; - data.sessionId= -1; - data.playerIndex= -1; - data.gameState = nmgstInvalid; - data.externalIp = 0; - data.ftpPort = 0; - data.gameInProgress = 0; -} + void NetworkMessage::send(Socket* socket, const void* data, int dataSize, int8 messageType) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, socket, data, dataSize); -NetworkMessageIntro::NetworkMessageIntro(int32 sessionId,const string &versionString, - const string &name, int playerIndex, - NetworkGameStateType gameState, - uint32 externalIp, - uint32 ftpPort, - const string &playerLanguage, - int gameInProgress, const string &playerUUID, - const string &platform) { - messageType = nmtIntro; - data.sessionId = sessionId; - data.versionString = versionString; - data.name = name; - data.playerIndex = static_cast(playerIndex); - data.gameState = static_cast(gameState); - data.externalIp = externalIp; - data.ftpPort = ftpPort; - data.language = playerLanguage; - data.gameInProgress = gameInProgress; - data.playerUUID = playerUUID; - data.platform = platform; -} + if (socket != NULL) { + int msgTypeSize = sizeof(messageType); + int fullMsgSize = msgTypeSize + dataSize; -const char * NetworkMessageIntro::getPackedMessageFormat() const { - return "cl128s32shcLL60sc60s60s"; -} + char *out_buffer = new char[fullMsgSize]; + memcpy(out_buffer, &messageType, msgTypeSize); + memcpy(&out_buffer[msgTypeSize], (const char *) data, dataSize); -unsigned int NetworkMessageIntro::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.externalIp = 0; - packedData.ftpPort = 0; - packedData.gameInProgress = 0; - packedData.gameState = 0; - messageType = nmtIntro; - packedData.playerIndex = 0; - packedData.sessionId = 0; + dump_packet("\nOUTGOING PACKET:\n", out_buffer, fullMsgSize, true); + int sendResult = socket->send(out_buffer, fullMsgSize); + if (sendResult != fullMsgSize) { + delete[] out_buffer; + if (socket != NULL && socket->isSocketValid() == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error sending NetworkMessage, sendResult = %d, dataSize = %d", sendResult, fullMsgSize); + throw megaglest_runtime_error(szBuf); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d socket has been disconnected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + } else { + delete[] out_buffer; + } + } + } - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + void NetworkMessage::send(Socket* socket, const void* data, int dataSize, int8 messageType, uint32 compressedLength) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] socket = %p, data = %p, dataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, socket, data, dataSize); + + if (socket != NULL) { + int msgTypeSize = sizeof(messageType); + int compressedSize = sizeof(compressedLength); + int fullMsgSize = msgTypeSize + compressedSize + dataSize; + + char *out_buffer = new char[fullMsgSize]; + memcpy(out_buffer, &messageType, msgTypeSize); + memcpy(&out_buffer[msgTypeSize], &compressedLength, compressedSize); + memcpy(&out_buffer[msgTypeSize + compressedSize], (const char *) data, dataSize); + + dump_packet("\nOUTGOING PACKET:\n", out_buffer, fullMsgSize, true); + int sendResult = socket->send(out_buffer, fullMsgSize); + if (sendResult != fullMsgSize) { + delete[] out_buffer; + if (socket != NULL && socket->isSocketValid() == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "Error sending NetworkMessage, sendResult = %d, dataSize = %d", sendResult, fullMsgSize); + throw megaglest_runtime_error(szBuf); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d socket has been disconnected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + } else { + delete[] out_buffer; + } + } + } + + void NetworkMessage::resetNetworkPacketStats() { + NetworkMessage::statsTimer.stop(); + NetworkMessage::lastSend.stop(); + NetworkMessage::lastRecv.stop(); + NetworkMessage::mapMessageStats.clear(); + } + + string NetworkMessage::getNetworkPacketStats() { + string result = "Current Timer Milliseconds: " + intToStr(NetworkMessage::statsTimer.getMillis()) + "\n"; + + for (std::map::iterator iterMap = mapMessageStats.begin(); + iterMap != mapMessageStats.end(); ++iterMap) { + switch (iterMap->first) { + case netmsgstPacketsPerMillisecondSend: + result += "send p / msec: " + intToStr(iterMap->second) + (iterMap->second > 10 ? " ****WARNING WIN32 LIMIT EXCEEDED****" : "") + "\n"; + break; + case netmsgstPacketsPerSecondSend: + result += "send p / sec: " + intToStr(iterMap->second) + "\n"; + break; + case netmsgstAverageSendSize: + result += "send avg size: " + intToStr(iterMap->second) + "\n"; + break; + + case netmsgstPacketsPerMillisecondRecv: + result += "recv p / msec: " + intToStr(iterMap->second) + "\n"; + break; + case netmsgstPacketsPerSecondRecv: + result += "recv p / sec: " + intToStr(iterMap->second) + (iterMap->second > 10 ? " ****WARNING WIN32 LIMIT EXCEEDED****" : "") + "\n"; + break; + case netmsgstAverageRecvSize: + result += "recv avg size: " + intToStr(iterMap->second) + "\n"; + break; + + } + } + return result; + } + + void NetworkMessage::dump_packet(string label, const void* data, int dataSize, bool isSend) { + Config &config = Config::getInstance(); + if (config.getBool("DebugNetworkPacketStats", "false") == true) { + + MutexSafeWrapper safeMutex(NetworkMessage::mutexMessageStats.get()); + + if (NetworkMessage::statsTimer.isStarted() == false) { + NetworkMessage::statsTimer.start(); + } + + bool secondChanged = false; + if (NetworkMessage::statsTimer.getSeconds() - NetworkMessage::mapMessageStats[netmsgstLastEvent] >= 3) { + + NetworkMessage::mapMessageStats[netmsgstLastEvent] = NetworkMessage::statsTimer.getSeconds(); + secondChanged = true; + } + + if (isSend == true) { + if (NetworkMessage::lastSend.isStarted() == false) { + NetworkMessage::lastSend.start(); + } + int64 millisecondsSinceLastSend = NetworkMessage::lastSend.getMillis(); + int64 secondsSinceLastSend = NetworkMessage::lastSend.getSeconds(); + + //char szBuf[8096]=""; + //snprintf(szBuf,8095,"\nSEND check cur [%lld] last [%lld]\n", (long long)millisecondsSinceLastSend,(long long)NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last] ); + //printf("%s",szBuf); + + if (millisecondsSinceLastSend == NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last]) { + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_current_count]++; + + } else { + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_last] = millisecondsSinceLastSend; + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend] = + (NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend] + + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondSend_current_count]) / 2; + } + + if (secondsSinceLastSend == NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_last]) { + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_current_count]++; + + } else { + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_last] = secondsSinceLastSend; + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend] = + (NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend] + + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondSend_current_count]) / 2; + } + + NetworkMessage::mapMessageStats[netmsgstAverageSendSize] = + (NetworkMessage::mapMessageStats[netmsgstAverageSendSize] + + dataSize) / 2; + } else { + if (NetworkMessage::lastRecv.isStarted() == false) { + NetworkMessage::lastRecv.start(); + } + int64 millisecondsSinceLastRecv = NetworkMessage::lastRecv.getMillis(); + int64 secondsSinceLastRecv = NetworkMessage::lastRecv.getSeconds(); + + //char szBuf[8096]=""; + //snprintf(szBuf,8095,"\nRECV check cur [%lld] last [%lld]\n", (long long)millisecondsSinceLastRecv,(long long)NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last] ); + //printf("%s",szBuf); + + if (millisecondsSinceLastRecv == NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last]) { + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_current_count]++; + + } else { + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_last] = millisecondsSinceLastRecv; + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv] = + (NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv] + + NetworkMessage::mapMessageStats[netmsgstPacketsPerMillisecondRecv_current_count]) / 2; + } + + if (secondsSinceLastRecv == NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_last]) { + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_current_count]++; + + } else { + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_last] = secondsSinceLastRecv; + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv] = + (NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv] + + NetworkMessage::mapMessageStats[netmsgstPacketsPerSecondRecv_current_count]) / 2; + } + + NetworkMessage::mapMessageStats[netmsgstAverageRecvSize] = + (NetworkMessage::mapMessageStats[netmsgstAverageRecvSize] + + dataSize) / 2; + } + + if (secondChanged == true) { + printf("%s", NetworkMessage::getNetworkPacketStats().c_str()); + } + } + + if (config.getBool("DebugNetworkPackets", "false") == true || + config.getBool("DebugNetworkPacketSizes", "false") == true) { + + printf("%s DataSize = %d", label.c_str(), dataSize); + + if (config.getBool("DebugNetworkPackets", "false") == true) { + + printf("\n"); + const char *buf = static_cast(data); + for (unsigned int index = 0; index < (unsigned int) dataSize; ++index) { + + printf("%u[%X][%d] ", index, buf[index], buf[index]); + if (index % 10 == 0) { + printf("\n"); + } + } + } + printf("\n======= END =======\n"); + } + } + + // ===================================================== + // class NetworkMessageIntro + // ===================================================== + NetworkMessageIntro::NetworkMessageIntro() { + messageType = -1; + data.sessionId = -1; + data.playerIndex = -1; + data.gameState = nmgstInvalid; + data.externalIp = 0; + data.ftpPort = 0; + data.gameInProgress = 0; + } + + NetworkMessageIntro::NetworkMessageIntro(int32 sessionId, const string &versionString, + const string &name, int playerIndex, + NetworkGameStateType gameState, + uint32 externalIp, + uint32 ftpPort, + const string &playerLanguage, + int gameInProgress, const string &playerUUID, + const string &platform) { + messageType = nmtIntro; + data.sessionId = sessionId; + data.versionString = versionString; + data.name = name; + data.playerIndex = static_cast(playerIndex); + data.gameState = static_cast(gameState); + data.externalIp = externalIp; + data.ftpPort = ftpPort; + data.language = playerLanguage; + data.gameInProgress = gameInProgress; + data.playerUUID = playerUUID; + data.platform = platform; + } + + const char * NetworkMessageIntro::getPackedMessageFormat() const { + return "cl128s32shcLL60sc60s60s"; + } + + unsigned int NetworkMessageIntro::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.externalIp = 0; + packedData.ftpPort = 0; + packedData.gameInProgress = 0; + packedData.gameState = 0; + messageType = nmtIntro; + packedData.playerIndex = 0; + packedData.sessionId = 0; + + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.sessionId, + packedData.versionString.getBuffer(), + packedData.name.getBuffer(), + packedData.playerIndex, + packedData.gameState, + packedData.externalIp, + packedData.ftpPort, + packedData.language.getBuffer(), + data.gameInProgress, + packedData.playerUUID.getBuffer(), + packedData.platform.getBuffer()); + delete[] buf; + } + return result; + } + void NetworkMessageIntro::unpackMessage(unsigned char *buf) { + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s] about to unpack...\n", __FUNCTION__); + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.sessionId, + data.versionString.getBuffer(), + data.name.getBuffer(), + &data.playerIndex, + &data.gameState, + &data.externalIp, + &data.ftpPort, + data.language.getBuffer(), + &data.gameInProgress, + data.playerUUID.getBuffer(), + data.platform.getBuffer()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s] unpacked data:\n%s\n", __FUNCTION__, this->toString().c_str()); + } + + unsigned char * NetworkMessageIntro::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s] about to pack...\n", __FUNCTION__); + pack(buf, getPackedMessageFormat(), messageType, - packedData.sessionId, - packedData.versionString.getBuffer(), - packedData.name.getBuffer(), - packedData.playerIndex, - packedData.gameState, - packedData.externalIp, - packedData.ftpPort, - packedData.language.getBuffer(), + data.sessionId, + data.versionString.getBuffer(), + data.name.getBuffer(), + data.playerIndex, + data.gameState, + data.externalIp, + data.ftpPort, + data.language.getBuffer(), data.gameInProgress, - packedData.playerUUID.getBuffer(), - packedData.platform.getBuffer()); - delete [] buf; - } - return result; -} -void NetworkMessageIntro::unpackMessage(unsigned char *buf) { - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s] about to unpack...\n",__FUNCTION__); - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.sessionId, - data.versionString.getBuffer(), - data.name.getBuffer(), - &data.playerIndex, - &data.gameState, - &data.externalIp, - &data.ftpPort, - data.language.getBuffer(), - &data.gameInProgress, - data.playerUUID.getBuffer(), - data.platform.getBuffer()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s] unpacked data:\n%s\n",__FUNCTION__,this->toString().c_str()); -} - -unsigned char * NetworkMessageIntro::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s] about to pack...\n",__FUNCTION__); - pack(buf, getPackedMessageFormat(), - messageType, - data.sessionId, - data.versionString.getBuffer(), - data.name.getBuffer(), - data.playerIndex, - data.gameState, - data.externalIp, - data.ftpPort, - data.language.getBuffer(), - data.gameInProgress, - data.playerUUID.getBuffer(), - data.platform.getBuffer()); - return buf; -} - -string NetworkMessageIntro::toString() const { - string result = "messageType = " + intToStr(messageType); - result += " sessionId = " + intToStr(data.sessionId); - result += " versionString = " + data.versionString.getString(); - result += " name = " + data.name.getString(); - result += " playerIndex = " + intToStr(data.playerIndex); - result += " gameState = " + intToStr(data.gameState); - result += " externalIp = " + uIntToStr(data.externalIp); - result += " ftpPort = " + uIntToStr(data.ftpPort); - result += " language = " + data.language.getString(); - result += " gameInProgress = " + uIntToStr(data.gameInProgress); - result += " playerUUID = " + data.playerUUID.getString(); - result += " platform = " + data.platform.getString(); - - return result; -} - -bool NetworkMessageIntro::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { - messageType = this->getNetworkMessageType(); + data.playerUUID.getBuffer(), + data.platform.getBuffer()); + return buf; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - data.name.nullTerminate(); - data.versionString.nullTerminate(); - data.language.nullTerminate(); - data.playerUUID.nullTerminate(); - data.platform.nullTerminate(); + string NetworkMessageIntro::toString() const { + string result = "messageType = " + intToStr(messageType); + result += " sessionId = " + intToStr(data.sessionId); + result += " versionString = " + data.versionString.getString(); + result += " name = " + data.name.getString(); + result += " playerIndex = " + intToStr(data.playerIndex); + result += " gameState = " + intToStr(data.gameState); + result += " externalIp = " + uIntToStr(data.externalIp); + result += " ftpPort = " + uIntToStr(data.ftpPort); + result += " language = " + data.language.getString(); + result += " gameInProgress = " + uIntToStr(data.gameInProgress); + result += " playerUUID = " + data.playerUUID.getString(); + result += " platform = " + data.platform.getString(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] get nmtIntro, data.playerIndex = %d, data.sessionId = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.playerIndex,data.sessionId); - return result; -} + return result; + } -void NetworkMessageIntro::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] sending nmtIntro, data.playerIndex = %d, data.sessionId = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.playerIndex,data.sessionId); - assert(messageType == nmtIntro); - toEndian(); + bool NetworkMessageIntro::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = this->getNetworkMessageType(); + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data),messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - //NetworkMessage::send(socket, &data, sizeof(data)); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + data.name.nullTerminate(); + data.versionString.nullTerminate(); + data.language.nullTerminate(); + data.playerUUID.nullTerminate(); + data.platform.nullTerminate(); -void NetworkMessageIntro::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.sessionId = Shared::PlatformByteOrder::toCommonEndian(data.sessionId); - data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); - data.gameState = Shared::PlatformByteOrder::toCommonEndian(data.gameState); - data.externalIp = Shared::PlatformByteOrder::toCommonEndian(data.externalIp); - data.ftpPort = Shared::PlatformByteOrder::toCommonEndian(data.ftpPort); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] get nmtIntro, data.playerIndex = %d, data.sessionId = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.playerIndex, data.sessionId); + return result; + } - data.gameInProgress = Shared::PlatformByteOrder::toCommonEndian(data.gameInProgress); - } -} -void NetworkMessageIntro::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.sessionId = Shared::PlatformByteOrder::fromCommonEndian(data.sessionId); - data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); - data.gameState = Shared::PlatformByteOrder::fromCommonEndian(data.gameState); - data.externalIp = Shared::PlatformByteOrder::fromCommonEndian(data.externalIp); - data.ftpPort = Shared::PlatformByteOrder::fromCommonEndian(data.ftpPort); + void NetworkMessageIntro::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] sending nmtIntro, data.playerIndex = %d, data.sessionId = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.playerIndex, data.sessionId); + assert(messageType == nmtIntro); + toEndian(); - data.gameInProgress = Shared::PlatformByteOrder::fromCommonEndian(data.gameInProgress); - } -} + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + //NetworkMessage::send(socket, &data, sizeof(data)); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } -// ===================================================== -// class NetworkMessagePing -// ===================================================== + void NetworkMessageIntro::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.sessionId = Shared::PlatformByteOrder::toCommonEndian(data.sessionId); + data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); + data.gameState = Shared::PlatformByteOrder::toCommonEndian(data.gameState); + data.externalIp = Shared::PlatformByteOrder::toCommonEndian(data.externalIp); + data.ftpPort = Shared::PlatformByteOrder::toCommonEndian(data.ftpPort); -NetworkMessagePing::NetworkMessagePing(){ - messageType = nmtPing; - data.pingFrequency = 0; - data.pingTime = 0; - pingReceivedLocalTime = 0; -} + data.gameInProgress = Shared::PlatformByteOrder::toCommonEndian(data.gameInProgress); + } + } + void NetworkMessageIntro::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.sessionId = Shared::PlatformByteOrder::fromCommonEndian(data.sessionId); + data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); + data.gameState = Shared::PlatformByteOrder::fromCommonEndian(data.gameState); + data.externalIp = Shared::PlatformByteOrder::fromCommonEndian(data.externalIp); + data.ftpPort = Shared::PlatformByteOrder::fromCommonEndian(data.ftpPort); -NetworkMessagePing::NetworkMessagePing(int32 pingFrequency, int64 pingTime){ - messageType= nmtPing; - data.pingFrequency= pingFrequency; - data.pingTime= pingTime; - pingReceivedLocalTime=0; -} + data.gameInProgress = Shared::PlatformByteOrder::fromCommonEndian(data.gameInProgress); + } + } -const char * NetworkMessagePing::getPackedMessageFormat() const { - return "clq"; -} + // ===================================================== + // class NetworkMessagePing + // ===================================================== -unsigned int NetworkMessagePing::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - messageType = 0; - packedData.pingFrequency = 0; - packedData.pingTime = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + NetworkMessagePing::NetworkMessagePing() { + messageType = nmtPing; + data.pingFrequency = 0; + data.pingTime = 0; + pingReceivedLocalTime = 0; + } + + NetworkMessagePing::NetworkMessagePing(int32 pingFrequency, int64 pingTime) { + messageType = nmtPing; + data.pingFrequency = pingFrequency; + data.pingTime = pingTime; + pingReceivedLocalTime = 0; + } + + const char * NetworkMessagePing::getPackedMessageFormat() const { + return "clq"; + } + + unsigned int NetworkMessagePing::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + messageType = 0; + packedData.pingFrequency = 0; + packedData.pingTime = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.pingFrequency, + packedData.pingTime); + delete[] buf; + } + return result; + } + void NetworkMessagePing::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.pingFrequency, + &data.pingTime); + } + + unsigned char * NetworkMessagePing::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.pingFrequency, - packedData.pingTime); - delete [] buf; - } - return result; -} -void NetworkMessagePing::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.pingFrequency, - &data.pingTime); -} - -unsigned char * NetworkMessagePing::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.pingFrequency, - data.pingTime); - return buf; -} - -bool NetworkMessagePing::receive(Socket* socket){ - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { - messageType = this->getNetworkMessageType(); + data.pingFrequency, + data.pingTime); + return buf; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - pingReceivedLocalTime = time(NULL); - return result; -} + bool NetworkMessagePing::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = this->getNetworkMessageType(); + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); -void NetworkMessagePing::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtPing\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - assert(messageType == nmtPing); - toEndian(); + pingReceivedLocalTime = time(NULL); + return result; + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - //NetworkMessage::send(socket, &data, sizeof(data)); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + void NetworkMessagePing::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtPing\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + assert(messageType == nmtPing); + toEndian(); -void NetworkMessagePing::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.pingFrequency = Shared::PlatformByteOrder::toCommonEndian(data.pingFrequency); - data.pingTime = Shared::PlatformByteOrder::toCommonEndian(data.pingTime); - } -} -void NetworkMessagePing::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.pingFrequency = Shared::PlatformByteOrder::fromCommonEndian(data.pingFrequency); - data.pingTime = Shared::PlatformByteOrder::fromCommonEndian(data.pingTime); - } -} + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + //NetworkMessage::send(socket, &data, sizeof(data)); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } -// ===================================================== -// class NetworkMessageReady -// ===================================================== + void NetworkMessagePing::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.pingFrequency = Shared::PlatformByteOrder::toCommonEndian(data.pingFrequency); + data.pingTime = Shared::PlatformByteOrder::toCommonEndian(data.pingTime); + } + } + void NetworkMessagePing::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.pingFrequency = Shared::PlatformByteOrder::fromCommonEndian(data.pingFrequency); + data.pingTime = Shared::PlatformByteOrder::fromCommonEndian(data.pingTime); + } + } -NetworkMessageReady::NetworkMessageReady() { - messageType = nmtReady; - data.checksum= 0; -} + // ===================================================== + // class NetworkMessageReady + // ===================================================== -NetworkMessageReady::NetworkMessageReady(uint32 checksum) { - messageType = nmtReady; - data.checksum= checksum; -} + NetworkMessageReady::NetworkMessageReady() { + messageType = nmtReady; + data.checksum = 0; + } -const char * NetworkMessageReady::getPackedMessageFormat() const { - return "cL"; -} + NetworkMessageReady::NetworkMessageReady(uint32 checksum) { + messageType = nmtReady; + data.checksum = checksum; + } -unsigned int NetworkMessageReady::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.checksum = 0; - messageType = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + const char * NetworkMessageReady::getPackedMessageFormat() const { + return "cL"; + } + + unsigned int NetworkMessageReady::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.checksum = 0; + messageType = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.checksum); + delete[] buf; + } + return result; + } + void NetworkMessageReady::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.checksum); + } + + unsigned char * NetworkMessageReady::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.checksum); - delete [] buf; - } - return result; -} -void NetworkMessageReady::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.checksum); -} - -unsigned char * NetworkMessageReady::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.checksum); - return buf; -} - -bool NetworkMessageReady::receive(Socket* socket){ - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { - messageType = this->getNetworkMessageType(); + data.checksum); + return buf; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - //bool result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - return result; -} -void NetworkMessageReady::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtReady\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - assert(messageType == nmtReady); - toEndian(); - - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void NetworkMessageReady::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.checksum = Shared::PlatformByteOrder::toCommonEndian(data.checksum); - } -} -void NetworkMessageReady::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.checksum = Shared::PlatformByteOrder::fromCommonEndian(data.checksum); - } -} - -// ===================================================== -// class NetworkMessageLaunch -// ===================================================== - -NetworkMessageLaunch::NetworkMessageLaunch() { - messageType = -1; - compressedLength = 0; - for(unsigned int i = 0; i < (unsigned int)maxFactionCRCCount; ++i) { - data.factionNameList[i] = ""; - data.factionCRCList[i] = 0; - } - data.aiAcceptSwitchTeamPercentChance = 0; - data.cpuReplacementMultiplier = 10 ; - data.masterserver_admin = -1; - data.masterserver_admin_factionIndex = -1; -} - -NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings,int8 messageType) { - this->messageType = messageType; - compressedLength = 0; - - data.mapFilter = gameSettings->getMapFilter(); - data.mapCRC = gameSettings->getMapCRC(); - data.tilesetCRC = gameSettings->getTilesetCRC(); - data.techCRC = gameSettings->getTechCRC(); - - for(unsigned int i = 0; i < (unsigned int)maxFactionCRCCount; ++i) { - data.factionNameList[i] = ""; - data.factionCRCList[i] = 0; - } - - vector > factionCRCList = gameSettings->getFactionCRCList(); - for(unsigned int i = 0; i < factionCRCList.size() && i < (unsigned int)maxFactionCRCCount; ++i) { - data.factionNameList[i] = factionCRCList[i].first; - data.factionCRCList[i] = factionCRCList[i].second; - } - - data.description= gameSettings->getDescription(); - data.map= gameSettings->getMap(); - data.tileset= gameSettings->getTileset(); - data.tech= gameSettings->getTech(); - data.factionCount= gameSettings->getFactionCount(); - data.thisFactionIndex= gameSettings->getThisFactionIndex(); - data.defaultResources= gameSettings->getDefaultResources(); - data.defaultUnits= gameSettings->getDefaultUnits(); - data.defaultVictoryConditions= gameSettings->getDefaultVictoryConditions(); - data.fogOfWar = gameSettings->getFogOfWar(); - data.allowObservers = gameSettings->getAllowObservers(); - data.enableObserverModeAtEndGame = gameSettings->getEnableObserverModeAtEndGame(); - data.enableServerControlledAI = gameSettings->getEnableServerControlledAI(); - data.networkFramePeriod = gameSettings->getNetworkFramePeriod(); - data.networkPauseGameForLaggedClients = gameSettings->getNetworkPauseGameForLaggedClients(); - data.pathFinderType = gameSettings->getPathFinderType(); - data.flagTypes1 = gameSettings->getFlagTypes1(); - - //for(int i= 0; i < data.factionCount; ++i) { - for(int i= 0; i < GameConstants::maxPlayers; ++i) { - data.factionTypeNames[i]= gameSettings->getFactionTypeName(i); - data.networkPlayerNames[i]= gameSettings->getNetworkPlayerName(i); - data.networkPlayerPlatform[i]= gameSettings->getNetworkPlayerPlatform(i); - data.networkPlayerStatuses[i] = gameSettings->getNetworkPlayerStatuses(i); - data.networkPlayerLanguages[i] = gameSettings->getNetworkPlayerLanguages(i); - data.factionControls[i]= gameSettings->getFactionControl(i); - data.resourceMultiplierIndex[i]= gameSettings->getResourceMultiplierIndex(i); - data.teams[i]= gameSettings->getTeam(i); - data.startLocationIndex[i]= gameSettings->getStartLocationIndex(i); - - data.networkPlayerUUID[i] = gameSettings->getNetworkPlayerUUID(i); - //printf("Build netmsg for index: %d [%s]\n",i,data.networkPlayerUUID[i].getString().c_str()); - } - data.cpuReplacementMultiplier = gameSettings->getFallbackCpuMultiplier(); - data.aiAcceptSwitchTeamPercentChance = gameSettings->getAiAcceptSwitchTeamPercentChance(); - data.masterserver_admin = gameSettings->getMasterserver_admin(); - data.masterserver_admin_factionIndex = gameSettings->getMasterserver_admin_faction_index(); - - data.scenario = gameSettings->getScenario(); - data.gameUUID = gameSettings->getGameUUID(); - - data.networkAllowNativeLanguageTechtree = gameSettings->getNetworkAllowNativeLanguageTechtree(); -} - -void NetworkMessageLaunch::buildGameSettings(GameSettings *gameSettings) const { - gameSettings->setDescription(data.description.getString()); - gameSettings->setMap(data.map.getString()); - gameSettings->setTileset(data.tileset.getString()); - gameSettings->setTech(data.tech.getString()); - gameSettings->setFactionCount(data.factionCount); - gameSettings->setThisFactionIndex(data.thisFactionIndex); - gameSettings->setDefaultResources((data.defaultResources != 0)); - gameSettings->setDefaultUnits((data.defaultUnits != 0)); - gameSettings->setDefaultVictoryConditions((data.defaultVictoryConditions != 0)); - gameSettings->setFogOfWar((data.fogOfWar != 0)); - gameSettings->setAllowObservers((data.allowObservers != 0)); - - gameSettings->setEnableObserverModeAtEndGame((data.enableObserverModeAtEndGame != 0)); - gameSettings->setEnableServerControlledAI((data.enableServerControlledAI != 0)); - gameSettings->setNetworkFramePeriod(data.networkFramePeriod); - gameSettings->setNetworkPauseGameForLaggedClients((data.networkPauseGameForLaggedClients != 0)); - gameSettings->setPathFinderType(static_cast(data.pathFinderType)); - gameSettings->setFlagTypes1(data.flagTypes1); - - gameSettings->setMapCRC(data.mapCRC); - gameSettings->setMapFilter(data.mapFilter); - gameSettings->setTilesetCRC(data.tilesetCRC); - gameSettings->setTechCRC(data.techCRC); - - vector > factionCRCList; - for(unsigned int i = 0; i < (unsigned int)maxFactionCRCCount; ++i) { - if(data.factionNameList[i].getString() != "") { - factionCRCList.push_back(make_pair(data.factionNameList[i].getString(),data.factionCRCList[i])); + bool NetworkMessageReady::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = this->getNetworkMessageType(); + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + //bool result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); + return result; } - } - gameSettings->setFactionCRCList(factionCRCList); - //for(int i= 0; i < data.factionCount; ++i) { - for(int i= 0; i < GameConstants::maxPlayers; ++i) { - gameSettings->setFactionTypeName(i, data.factionTypeNames[i].getString()); - gameSettings->setNetworkPlayerName(i,data.networkPlayerNames[i].getString()); - gameSettings->setNetworkPlayerPlatform(i,data.networkPlayerPlatform[i].getString()); - gameSettings->setNetworkPlayerStatuses(i, data.networkPlayerStatuses[i]); - gameSettings->setNetworkPlayerLanguages(i, data.networkPlayerLanguages[i].getString()); - gameSettings->setFactionControl(i, static_cast(data.factionControls[i])); - gameSettings->setResourceMultiplierIndex(i,data.resourceMultiplierIndex[i]); - gameSettings->setTeam(i, data.teams[i]); - gameSettings->setStartLocationIndex(i, data.startLocationIndex[i]); + void NetworkMessageReady::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtReady\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + assert(messageType == nmtReady); + toEndian(); - gameSettings->setNetworkPlayerUUID(i,data.networkPlayerUUID[i].getString()); - gameSettings->setNetworkPlayerPlatform(i,data.networkPlayerPlatform[i].getString()); - //printf("Build game settings for index: %d [%s]\n",i,data.networkPlayerUUID[i].getString().c_str()); - } - - gameSettings->setAiAcceptSwitchTeamPercentChance(data.aiAcceptSwitchTeamPercentChance); - gameSettings->setFallbackCpuMultiplier(data.cpuReplacementMultiplier); - - gameSettings->setMasterserver_admin(data.masterserver_admin); - gameSettings->setMasterserver_admin_faction_index(data.masterserver_admin_factionIndex); - - gameSettings->setScenario(data.scenario.getString()); - - gameSettings->setGameUUID(data.gameUUID.getString()); - - gameSettings->setNetworkAllowNativeLanguageTechtree((data.networkAllowNativeLanguageTechtree != 0)); -} - -vector > NetworkMessageLaunch::getFactionCRCList() const { - - vector > factionCRCList; - for(unsigned int i = 0; i < (unsigned int)maxFactionCRCCount; ++i) { - if(data.factionNameList[i].getString() != "") { - factionCRCList.push_back(make_pair(data.factionNameList[i].getString(),data.factionCRCList[i])); + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } } - } - return factionCRCList; -} -const char * NetworkMessageLaunch::getPackedMessageFormat() const { - return "c256s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60sllllllll60s60s60s60s60s60s60s60sLLL60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60sLLLLLLLLLLLLLLLLLLLLcccccccccccccccccccccccccccccccccccccccccCccLccll256s60s60s60s60s60s60s60s60sc60s"; -} - -unsigned int NetworkMessageLaunch::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.aiAcceptSwitchTeamPercentChance = 0; - packedData.allowObservers = 0; - packedData.cpuReplacementMultiplier = 0; - packedData.defaultResources = 0; - packedData.defaultUnits = 0; - packedData.defaultVictoryConditions = 0; - packedData.enableObserverModeAtEndGame = 0; - packedData.enableServerControlledAI = 0; - for(unsigned int index =0; index < (unsigned int)maxFactionCRCCount; ++index) { - packedData.factionCRCList[index] = 0; + void NetworkMessageReady::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.checksum = Shared::PlatformByteOrder::toCommonEndian(data.checksum); + } } - for(unsigned int index =0; index < (unsigned int)GameConstants::maxPlayers; ++index) { - packedData.factionControls[index] = 0; - packedData.networkPlayerStatuses[index] = 0; - packedData.resourceMultiplierIndex[index] = 0; - packedData.startLocationIndex[index] = 0; - packedData.teams[index] = 0; + void NetworkMessageReady::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.checksum = Shared::PlatformByteOrder::fromCommonEndian(data.checksum); + } } - packedData.factionCount = 0; - packedData.flagTypes1 = 0; - packedData.fogOfWar = 0; - packedData.mapCRC = 0; - packedData.mapFilter = 0; - packedData.masterserver_admin = 0; - packedData.masterserver_admin_factionIndex = 0; - messageType = 0; - packedData.networkAllowNativeLanguageTechtree = 0; - packedData.networkFramePeriod = 0; - packedData.networkPauseGameForLaggedClients = 0; - packedData.pathFinderType = 0; - packedData.techCRC = 0; - packedData.thisFactionIndex = 0; - packedData.tilesetCRC = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), - messageType, - packedData.description.getBuffer(), - packedData.map.getBuffer(), - packedData.tileset.getBuffer(), - packedData.tech.getBuffer(), - packedData.factionTypeNames[0].getBuffer(), - packedData.factionTypeNames[1].getBuffer(), - packedData.factionTypeNames[2].getBuffer(), - packedData.factionTypeNames[3].getBuffer(), - packedData.factionTypeNames[4].getBuffer(), - packedData.factionTypeNames[5].getBuffer(), - packedData.factionTypeNames[6].getBuffer(), - packedData.factionTypeNames[7].getBuffer(), - packedData.networkPlayerNames[0].getBuffer(), - packedData.networkPlayerNames[1].getBuffer(), - packedData.networkPlayerNames[2].getBuffer(), - packedData.networkPlayerNames[3].getBuffer(), - packedData.networkPlayerNames[4].getBuffer(), - packedData.networkPlayerNames[5].getBuffer(), - packedData.networkPlayerNames[6].getBuffer(), - packedData.networkPlayerNames[7].getBuffer(), - packedData.networkPlayerPlatform[0].getBuffer(), - packedData.networkPlayerPlatform[1].getBuffer(), - packedData.networkPlayerPlatform[2].getBuffer(), - packedData.networkPlayerPlatform[3].getBuffer(), - packedData.networkPlayerPlatform[4].getBuffer(), - packedData.networkPlayerPlatform[5].getBuffer(), - packedData.networkPlayerPlatform[6].getBuffer(), - packedData.networkPlayerPlatform[7].getBuffer(), - packedData.networkPlayerStatuses[0], - packedData.networkPlayerStatuses[1], - packedData.networkPlayerStatuses[2], - packedData.networkPlayerStatuses[3], - packedData.networkPlayerStatuses[4], - packedData.networkPlayerStatuses[5], - packedData.networkPlayerStatuses[6], - packedData.networkPlayerStatuses[7], - packedData.networkPlayerLanguages[0].getBuffer(), - packedData.networkPlayerLanguages[1].getBuffer(), - packedData.networkPlayerLanguages[2].getBuffer(), - packedData.networkPlayerLanguages[3].getBuffer(), - packedData.networkPlayerLanguages[4].getBuffer(), - packedData.networkPlayerLanguages[5].getBuffer(), - packedData.networkPlayerLanguages[6].getBuffer(), - packedData.networkPlayerLanguages[7].getBuffer(), - packedData.mapCRC, - packedData.mapFilter, - packedData.tilesetCRC, - packedData.techCRC, - packedData.factionNameList[0].getBuffer(), - packedData.factionNameList[1].getBuffer(), - packedData.factionNameList[2].getBuffer(), - packedData.factionNameList[3].getBuffer(), - packedData.factionNameList[4].getBuffer(), - packedData.factionNameList[5].getBuffer(), - packedData.factionNameList[6].getBuffer(), - packedData.factionNameList[7].getBuffer(), - packedData.factionNameList[8].getBuffer(), - packedData.factionNameList[9].getBuffer(), - packedData.factionNameList[10].getBuffer(), - packedData.factionNameList[11].getBuffer(), - packedData.factionNameList[12].getBuffer(), - packedData.factionNameList[13].getBuffer(), - packedData.factionNameList[14].getBuffer(), - packedData.factionNameList[15].getBuffer(), - packedData.factionNameList[16].getBuffer(), - packedData.factionNameList[17].getBuffer(), - packedData.factionNameList[18].getBuffer(), - packedData.factionNameList[19].getBuffer(), - packedData.factionCRCList[0], - packedData.factionCRCList[1], - packedData.factionCRCList[2], - packedData.factionCRCList[3], - packedData.factionCRCList[4], - packedData.factionCRCList[5], - packedData.factionCRCList[6], - packedData.factionCRCList[7], - packedData.factionCRCList[8], - packedData.factionCRCList[9], - packedData.factionCRCList[10], - packedData.factionCRCList[11], - packedData.factionCRCList[12], - packedData.factionCRCList[13], - packedData.factionCRCList[14], - packedData.factionCRCList[15], - packedData.factionCRCList[16], - packedData.factionCRCList[17], - packedData.factionCRCList[18], - packedData.factionCRCList[19], - packedData.factionControls[0], - packedData.factionControls[1], - packedData.factionControls[2], - packedData.factionControls[3], - packedData.factionControls[4], - packedData.factionControls[5], - packedData.factionControls[6], - packedData.factionControls[7], - packedData.resourceMultiplierIndex[0], - packedData.resourceMultiplierIndex[1], - packedData.resourceMultiplierIndex[2], - packedData.resourceMultiplierIndex[3], - packedData.resourceMultiplierIndex[4], - packedData.resourceMultiplierIndex[5], - packedData.resourceMultiplierIndex[6], - packedData.resourceMultiplierIndex[7], - packedData.thisFactionIndex, - packedData.factionCount, - packedData.teams[0], - packedData.teams[1], - packedData.teams[2], - packedData.teams[3], - packedData.teams[4], - packedData.teams[5], - packedData.teams[6], - packedData.teams[7], - packedData.startLocationIndex[0], - packedData.startLocationIndex[1], - packedData.startLocationIndex[2], - packedData.startLocationIndex[3], - packedData.startLocationIndex[4], - packedData.startLocationIndex[5], - packedData.startLocationIndex[6], - packedData.startLocationIndex[7], - packedData.defaultResources, - packedData.defaultUnits, - packedData.defaultVictoryConditions, - packedData.fogOfWar, - packedData.allowObservers, - packedData.enableObserverModeAtEndGame, - packedData.enableServerControlledAI, - packedData.networkFramePeriod, - packedData.networkPauseGameForLaggedClients, - packedData.pathFinderType, - packedData.flagTypes1, - packedData.aiAcceptSwitchTeamPercentChance, - packedData.cpuReplacementMultiplier, - packedData.masterserver_admin, - packedData.masterserver_admin_factionIndex, - packedData.scenario.getBuffer(), - packedData.networkPlayerUUID[0].getBuffer(), - packedData.networkPlayerUUID[1].getBuffer(), - packedData.networkPlayerUUID[2].getBuffer(), - packedData.networkPlayerUUID[3].getBuffer(), - packedData.networkPlayerUUID[4].getBuffer(), - packedData.networkPlayerUUID[5].getBuffer(), - packedData.networkPlayerUUID[6].getBuffer(), - packedData.networkPlayerUUID[7].getBuffer(), - packedData.networkAllowNativeLanguageTechtree, - packedData.gameUUID.getBuffer() - ); - delete [] buf; - } - return result; -} -void NetworkMessageLaunch::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - data.description.getBuffer(), - data.map.getBuffer(), - data.tileset.getBuffer(), - data.tech.getBuffer(), - data.factionTypeNames[0].getBuffer(), - data.factionTypeNames[1].getBuffer(), - data.factionTypeNames[2].getBuffer(), - data.factionTypeNames[3].getBuffer(), - data.factionTypeNames[4].getBuffer(), - data.factionTypeNames[5].getBuffer(), - data.factionTypeNames[6].getBuffer(), - data.factionTypeNames[7].getBuffer(), - data.networkPlayerNames[0].getBuffer(), - data.networkPlayerNames[1].getBuffer(), - data.networkPlayerNames[2].getBuffer(), - data.networkPlayerNames[3].getBuffer(), - data.networkPlayerNames[4].getBuffer(), - data.networkPlayerNames[5].getBuffer(), - data.networkPlayerNames[6].getBuffer(), - data.networkPlayerNames[7].getBuffer(), - data.networkPlayerPlatform[0].getBuffer(), - data.networkPlayerPlatform[1].getBuffer(), - data.networkPlayerPlatform[2].getBuffer(), - data.networkPlayerPlatform[3].getBuffer(), - data.networkPlayerPlatform[4].getBuffer(), - data.networkPlayerPlatform[5].getBuffer(), - data.networkPlayerPlatform[6].getBuffer(), - data.networkPlayerPlatform[7].getBuffer(), - &data.networkPlayerStatuses[0], - &data.networkPlayerStatuses[1], - &data.networkPlayerStatuses[2], - &data.networkPlayerStatuses[3], - &data.networkPlayerStatuses[4], - &data.networkPlayerStatuses[5], - &data.networkPlayerStatuses[6], - &data.networkPlayerStatuses[7], - data.networkPlayerLanguages[0].getBuffer(), - data.networkPlayerLanguages[1].getBuffer(), - data.networkPlayerLanguages[2].getBuffer(), - data.networkPlayerLanguages[3].getBuffer(), - data.networkPlayerLanguages[4].getBuffer(), - data.networkPlayerLanguages[5].getBuffer(), - data.networkPlayerLanguages[6].getBuffer(), - data.networkPlayerLanguages[7].getBuffer(), - &data.mapFilter, - &data.mapCRC, - &data.tilesetCRC, - &data.techCRC, - data.factionNameList[0].getBuffer(), - data.factionNameList[1].getBuffer(), - data.factionNameList[2].getBuffer(), - data.factionNameList[3].getBuffer(), - data.factionNameList[4].getBuffer(), - data.factionNameList[5].getBuffer(), - data.factionNameList[6].getBuffer(), - data.factionNameList[7].getBuffer(), - data.factionNameList[8].getBuffer(), - data.factionNameList[9].getBuffer(), - data.factionNameList[10].getBuffer(), - data.factionNameList[11].getBuffer(), - data.factionNameList[12].getBuffer(), - data.factionNameList[13].getBuffer(), - data.factionNameList[14].getBuffer(), - data.factionNameList[15].getBuffer(), - data.factionNameList[16].getBuffer(), - data.factionNameList[17].getBuffer(), - data.factionNameList[18].getBuffer(), - data.factionNameList[19].getBuffer(), - &data.factionCRCList[0], - &data.factionCRCList[1], - &data.factionCRCList[2], - &data.factionCRCList[3], - &data.factionCRCList[4], - &data.factionCRCList[5], - &data.factionCRCList[6], - &data.factionCRCList[7], - &data.factionCRCList[8], - &data.factionCRCList[9], - &data.factionCRCList[10], - &data.factionCRCList[11], - &data.factionCRCList[12], - &data.factionCRCList[13], - &data.factionCRCList[14], - &data.factionCRCList[15], - &data.factionCRCList[16], - &data.factionCRCList[17], - &data.factionCRCList[18], - &data.factionCRCList[19], - &data.factionControls[0], - &data.factionControls[1], - &data.factionControls[2], - &data.factionControls[3], - &data.factionControls[4], - &data.factionControls[5], - &data.factionControls[6], - &data.factionControls[7], - &data.resourceMultiplierIndex[0], - &data.resourceMultiplierIndex[1], - &data.resourceMultiplierIndex[2], - &data.resourceMultiplierIndex[3], - &data.resourceMultiplierIndex[4], - &data.resourceMultiplierIndex[5], - &data.resourceMultiplierIndex[6], - &data.resourceMultiplierIndex[7], - &data.thisFactionIndex, - &data.factionCount, - &data.teams[0], - &data.teams[1], - &data.teams[2], - &data.teams[3], - &data.teams[4], - &data.teams[5], - &data.teams[6], - &data.teams[7], - &data.startLocationIndex[0], - &data.startLocationIndex[1], - &data.startLocationIndex[2], - &data.startLocationIndex[3], - &data.startLocationIndex[4], - &data.startLocationIndex[5], - &data.startLocationIndex[6], - &data.startLocationIndex[7], - &data.defaultResources, - &data.defaultUnits, - &data.defaultVictoryConditions, - &data.fogOfWar, - &data.allowObservers, - &data.enableObserverModeAtEndGame, - &data.enableServerControlledAI, - &data.networkFramePeriod, - &data.networkPauseGameForLaggedClients, - &data.pathFinderType, - &data.flagTypes1, - &data.aiAcceptSwitchTeamPercentChance, - &data.cpuReplacementMultiplier, - &data.masterserver_admin, - &data.masterserver_admin_factionIndex, - data.scenario.getBuffer(), - data.networkPlayerUUID[0].getBuffer(), - data.networkPlayerUUID[1].getBuffer(), - data.networkPlayerUUID[2].getBuffer(), - data.networkPlayerUUID[3].getBuffer(), - data.networkPlayerUUID[4].getBuffer(), - data.networkPlayerUUID[5].getBuffer(), - data.networkPlayerUUID[6].getBuffer(), - data.networkPlayerUUID[7].getBuffer(), - &data.networkAllowNativeLanguageTechtree, - data.gameUUID.getBuffer() - ); -} + // ===================================================== + // class NetworkMessageLaunch + // ===================================================== -unsigned char * NetworkMessageLaunch::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.description.getBuffer(), - data.map.getBuffer(), - data.tileset.getBuffer(), - data.tech.getBuffer(), - data.factionTypeNames[0].getBuffer(), - data.factionTypeNames[1].getBuffer(), - data.factionTypeNames[2].getBuffer(), - data.factionTypeNames[3].getBuffer(), - data.factionTypeNames[4].getBuffer(), - data.factionTypeNames[5].getBuffer(), - data.factionTypeNames[6].getBuffer(), - data.factionTypeNames[7].getBuffer(), - data.networkPlayerNames[0].getBuffer(), - data.networkPlayerNames[1].getBuffer(), - data.networkPlayerNames[2].getBuffer(), - data.networkPlayerNames[3].getBuffer(), - data.networkPlayerNames[4].getBuffer(), - data.networkPlayerNames[5].getBuffer(), - data.networkPlayerNames[6].getBuffer(), - data.networkPlayerNames[7].getBuffer(), - data.networkPlayerPlatform[0].getBuffer(), - data.networkPlayerPlatform[1].getBuffer(), - data.networkPlayerPlatform[2].getBuffer(), - data.networkPlayerPlatform[3].getBuffer(), - data.networkPlayerPlatform[4].getBuffer(), - data.networkPlayerPlatform[5].getBuffer(), - data.networkPlayerPlatform[6].getBuffer(), - data.networkPlayerPlatform[7].getBuffer(), - data.networkPlayerStatuses[0], - data.networkPlayerStatuses[1], - data.networkPlayerStatuses[2], - data.networkPlayerStatuses[3], - data.networkPlayerStatuses[4], - data.networkPlayerStatuses[5], - data.networkPlayerStatuses[6], - data.networkPlayerStatuses[7], - data.networkPlayerLanguages[0].getBuffer(), - data.networkPlayerLanguages[1].getBuffer(), - data.networkPlayerLanguages[2].getBuffer(), - data.networkPlayerLanguages[3].getBuffer(), - data.networkPlayerLanguages[4].getBuffer(), - data.networkPlayerLanguages[5].getBuffer(), - data.networkPlayerLanguages[6].getBuffer(), - data.networkPlayerLanguages[7].getBuffer(), - data.mapFilter, - data.mapCRC, - data.tilesetCRC, - data.techCRC, - data.factionNameList[0].getBuffer(), - data.factionNameList[1].getBuffer(), - data.factionNameList[2].getBuffer(), - data.factionNameList[3].getBuffer(), - data.factionNameList[4].getBuffer(), - data.factionNameList[5].getBuffer(), - data.factionNameList[6].getBuffer(), - data.factionNameList[7].getBuffer(), - data.factionNameList[8].getBuffer(), - data.factionNameList[9].getBuffer(), - data.factionNameList[10].getBuffer(), - data.factionNameList[11].getBuffer(), - data.factionNameList[12].getBuffer(), - data.factionNameList[13].getBuffer(), - data.factionNameList[14].getBuffer(), - data.factionNameList[15].getBuffer(), - data.factionNameList[16].getBuffer(), - data.factionNameList[17].getBuffer(), - data.factionNameList[18].getBuffer(), - data.factionNameList[19].getBuffer(), - data.factionCRCList[0], - data.factionCRCList[1], - data.factionCRCList[2], - data.factionCRCList[3], - data.factionCRCList[4], - data.factionCRCList[5], - data.factionCRCList[6], - data.factionCRCList[7], - data.factionCRCList[8], - data.factionCRCList[9], - data.factionCRCList[10], - data.factionCRCList[11], - data.factionCRCList[12], - data.factionCRCList[13], - data.factionCRCList[14], - data.factionCRCList[15], - data.factionCRCList[16], - data.factionCRCList[17], - data.factionCRCList[18], - data.factionCRCList[19], - data.factionControls[0], - data.factionControls[1], - data.factionControls[2], - data.factionControls[3], - data.factionControls[4], - data.factionControls[5], - data.factionControls[6], - data.factionControls[7], - data.resourceMultiplierIndex[0], - data.resourceMultiplierIndex[1], - data.resourceMultiplierIndex[2], - data.resourceMultiplierIndex[3], - data.resourceMultiplierIndex[4], - data.resourceMultiplierIndex[5], - data.resourceMultiplierIndex[6], - data.resourceMultiplierIndex[7], - data.thisFactionIndex, - data.factionCount, - data.teams[0], - data.teams[1], - data.teams[2], - data.teams[3], - data.teams[4], - data.teams[5], - data.teams[6], - data.teams[7], - data.startLocationIndex[0], - data.startLocationIndex[1], - data.startLocationIndex[2], - data.startLocationIndex[3], - data.startLocationIndex[4], - data.startLocationIndex[5], - data.startLocationIndex[6], - data.startLocationIndex[7], - data.defaultResources, - data.defaultUnits, - data.defaultVictoryConditions, - data.fogOfWar, - data.allowObservers, - data.enableObserverModeAtEndGame, - data.enableServerControlledAI, - data.networkFramePeriod, - data.networkPauseGameForLaggedClients, - data.pathFinderType, - data.flagTypes1, - data.aiAcceptSwitchTeamPercentChance, - data.cpuReplacementMultiplier, - data.masterserver_admin, - data.masterserver_admin_factionIndex, - data.scenario.getBuffer(), - data.networkPlayerUUID[0].getBuffer(), - data.networkPlayerUUID[1].getBuffer(), - data.networkPlayerUUID[2].getBuffer(), - data.networkPlayerUUID[3].getBuffer(), - data.networkPlayerUUID[4].getBuffer(), - data.networkPlayerUUID[5].getBuffer(), - data.networkPlayerUUID[6].getBuffer(), - data.networkPlayerUUID[7].getBuffer(), - data.networkAllowNativeLanguageTechtree, - data.gameUUID.getBuffer() + NetworkMessageLaunch::NetworkMessageLaunch() { + messageType = -1; + compressedLength = 0; + for (unsigned int i = 0; i < (unsigned int) maxFactionCRCCount; ++i) { + data.factionNameList[i] = ""; + data.factionCRCList[i] = 0; + } + data.aiAcceptSwitchTeamPercentChance = 0; + data.cpuReplacementMultiplier = 10; + data.masterserver_admin = -1; + data.masterserver_admin_factionIndex = -1; + } + + NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings, int8 messageType) { + this->messageType = messageType; + compressedLength = 0; + + data.mapFilter = gameSettings->getMapFilter(); + data.mapCRC = gameSettings->getMapCRC(); + data.tilesetCRC = gameSettings->getTilesetCRC(); + data.techCRC = gameSettings->getTechCRC(); + + for (unsigned int i = 0; i < (unsigned int) maxFactionCRCCount; ++i) { + data.factionNameList[i] = ""; + data.factionCRCList[i] = 0; + } + + vector > factionCRCList = gameSettings->getFactionCRCList(); + for (unsigned int i = 0; i < factionCRCList.size() && i < (unsigned int) maxFactionCRCCount; ++i) { + data.factionNameList[i] = factionCRCList[i].first; + data.factionCRCList[i] = factionCRCList[i].second; + } + + data.description = gameSettings->getDescription(); + data.map = gameSettings->getMap(); + data.tileset = gameSettings->getTileset(); + data.tech = gameSettings->getTech(); + data.factionCount = gameSettings->getFactionCount(); + data.thisFactionIndex = gameSettings->getThisFactionIndex(); + data.defaultResources = gameSettings->getDefaultResources(); + data.defaultUnits = gameSettings->getDefaultUnits(); + data.defaultVictoryConditions = gameSettings->getDefaultVictoryConditions(); + data.fogOfWar = gameSettings->getFogOfWar(); + data.allowObservers = gameSettings->getAllowObservers(); + data.enableObserverModeAtEndGame = gameSettings->getEnableObserverModeAtEndGame(); + data.enableServerControlledAI = gameSettings->getEnableServerControlledAI(); + data.networkFramePeriod = gameSettings->getNetworkFramePeriod(); + data.networkPauseGameForLaggedClients = gameSettings->getNetworkPauseGameForLaggedClients(); + data.pathFinderType = gameSettings->getPathFinderType(); + data.flagTypes1 = gameSettings->getFlagTypes1(); + + //for(int i= 0; i < data.factionCount; ++i) { + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + data.factionTypeNames[i] = gameSettings->getFactionTypeName(i); + data.networkPlayerNames[i] = gameSettings->getNetworkPlayerName(i); + data.networkPlayerPlatform[i] = gameSettings->getNetworkPlayerPlatform(i); + data.networkPlayerStatuses[i] = gameSettings->getNetworkPlayerStatuses(i); + data.networkPlayerLanguages[i] = gameSettings->getNetworkPlayerLanguages(i); + data.factionControls[i] = gameSettings->getFactionControl(i); + data.resourceMultiplierIndex[i] = gameSettings->getResourceMultiplierIndex(i); + data.teams[i] = gameSettings->getTeam(i); + data.startLocationIndex[i] = gameSettings->getStartLocationIndex(i); + + data.networkPlayerUUID[i] = gameSettings->getNetworkPlayerUUID(i); + //printf("Build netmsg for index: %d [%s]\n",i,data.networkPlayerUUID[i].getString().c_str()); + } + data.cpuReplacementMultiplier = gameSettings->getFallbackCpuMultiplier(); + data.aiAcceptSwitchTeamPercentChance = gameSettings->getAiAcceptSwitchTeamPercentChance(); + data.masterserver_admin = gameSettings->getMasterserver_admin(); + data.masterserver_admin_factionIndex = gameSettings->getMasterserver_admin_faction_index(); + + data.scenario = gameSettings->getScenario(); + data.gameUUID = gameSettings->getGameUUID(); + + data.networkAllowNativeLanguageTechtree = gameSettings->getNetworkAllowNativeLanguageTechtree(); + } + + void NetworkMessageLaunch::buildGameSettings(GameSettings *gameSettings) const { + gameSettings->setDescription(data.description.getString()); + gameSettings->setMap(data.map.getString()); + gameSettings->setTileset(data.tileset.getString()); + gameSettings->setTech(data.tech.getString()); + gameSettings->setFactionCount(data.factionCount); + gameSettings->setThisFactionIndex(data.thisFactionIndex); + gameSettings->setDefaultResources((data.defaultResources != 0)); + gameSettings->setDefaultUnits((data.defaultUnits != 0)); + gameSettings->setDefaultVictoryConditions((data.defaultVictoryConditions != 0)); + gameSettings->setFogOfWar((data.fogOfWar != 0)); + gameSettings->setAllowObservers((data.allowObservers != 0)); + + gameSettings->setEnableObserverModeAtEndGame((data.enableObserverModeAtEndGame != 0)); + gameSettings->setEnableServerControlledAI((data.enableServerControlledAI != 0)); + gameSettings->setNetworkFramePeriod(data.networkFramePeriod); + gameSettings->setNetworkPauseGameForLaggedClients((data.networkPauseGameForLaggedClients != 0)); + gameSettings->setPathFinderType(static_cast(data.pathFinderType)); + gameSettings->setFlagTypes1(data.flagTypes1); + + gameSettings->setMapCRC(data.mapCRC); + gameSettings->setMapFilter(data.mapFilter); + gameSettings->setTilesetCRC(data.tilesetCRC); + gameSettings->setTechCRC(data.techCRC); + + vector > factionCRCList; + for (unsigned int i = 0; i < (unsigned int) maxFactionCRCCount; ++i) { + if (data.factionNameList[i].getString() != "") { + factionCRCList.push_back(make_pair(data.factionNameList[i].getString(), data.factionCRCList[i])); + } + } + gameSettings->setFactionCRCList(factionCRCList); + + //for(int i= 0; i < data.factionCount; ++i) { + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + gameSettings->setFactionTypeName(i, data.factionTypeNames[i].getString()); + gameSettings->setNetworkPlayerName(i, data.networkPlayerNames[i].getString()); + gameSettings->setNetworkPlayerPlatform(i, data.networkPlayerPlatform[i].getString()); + gameSettings->setNetworkPlayerStatuses(i, data.networkPlayerStatuses[i]); + gameSettings->setNetworkPlayerLanguages(i, data.networkPlayerLanguages[i].getString()); + gameSettings->setFactionControl(i, static_cast(data.factionControls[i])); + gameSettings->setResourceMultiplierIndex(i, data.resourceMultiplierIndex[i]); + gameSettings->setTeam(i, data.teams[i]); + gameSettings->setStartLocationIndex(i, data.startLocationIndex[i]); + + gameSettings->setNetworkPlayerUUID(i, data.networkPlayerUUID[i].getString()); + gameSettings->setNetworkPlayerPlatform(i, data.networkPlayerPlatform[i].getString()); + //printf("Build game settings for index: %d [%s]\n",i,data.networkPlayerUUID[i].getString().c_str()); + } + + gameSettings->setAiAcceptSwitchTeamPercentChance(data.aiAcceptSwitchTeamPercentChance); + gameSettings->setFallbackCpuMultiplier(data.cpuReplacementMultiplier); + + gameSettings->setMasterserver_admin(data.masterserver_admin); + gameSettings->setMasterserver_admin_faction_index(data.masterserver_admin_factionIndex); + + gameSettings->setScenario(data.scenario.getString()); + + gameSettings->setGameUUID(data.gameUUID.getString()); + + gameSettings->setNetworkAllowNativeLanguageTechtree((data.networkAllowNativeLanguageTechtree != 0)); + } + + vector > NetworkMessageLaunch::getFactionCRCList() const { + + vector > factionCRCList; + for (unsigned int i = 0; i < (unsigned int) maxFactionCRCCount; ++i) { + if (data.factionNameList[i].getString() != "") { + factionCRCList.push_back(make_pair(data.factionNameList[i].getString(), data.factionCRCList[i])); + } + } + return factionCRCList; + } + + const char * NetworkMessageLaunch::getPackedMessageFormat() const { + return "c256s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60sllllllll60s60s60s60s60s60s60s60sLLL60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60s60sLLLLLLLLLLLLLLLLLLLLcccccccccccccccccccccccccccccccccccccccccCccLccll256s60s60s60s60s60s60s60s60sc60s"; + } + + unsigned int NetworkMessageLaunch::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.aiAcceptSwitchTeamPercentChance = 0; + packedData.allowObservers = 0; + packedData.cpuReplacementMultiplier = 0; + packedData.defaultResources = 0; + packedData.defaultUnits = 0; + packedData.defaultVictoryConditions = 0; + packedData.enableObserverModeAtEndGame = 0; + packedData.enableServerControlledAI = 0; + for (unsigned int index = 0; index < (unsigned int) maxFactionCRCCount; ++index) { + packedData.factionCRCList[index] = 0; + } + for (unsigned int index = 0; index < (unsigned int) GameConstants::maxPlayers; ++index) { + packedData.factionControls[index] = 0; + packedData.networkPlayerStatuses[index] = 0; + packedData.resourceMultiplierIndex[index] = 0; + packedData.startLocationIndex[index] = 0; + packedData.teams[index] = 0; + } + packedData.factionCount = 0; + packedData.flagTypes1 = 0; + packedData.fogOfWar = 0; + packedData.mapCRC = 0; + packedData.mapFilter = 0; + packedData.masterserver_admin = 0; + packedData.masterserver_admin_factionIndex = 0; + messageType = 0; + packedData.networkAllowNativeLanguageTechtree = 0; + packedData.networkFramePeriod = 0; + packedData.networkPauseGameForLaggedClients = 0; + packedData.pathFinderType = 0; + packedData.techCRC = 0; + packedData.thisFactionIndex = 0; + packedData.tilesetCRC = 0; + + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.description.getBuffer(), + packedData.map.getBuffer(), + packedData.tileset.getBuffer(), + packedData.tech.getBuffer(), + packedData.factionTypeNames[0].getBuffer(), + packedData.factionTypeNames[1].getBuffer(), + packedData.factionTypeNames[2].getBuffer(), + packedData.factionTypeNames[3].getBuffer(), + packedData.factionTypeNames[4].getBuffer(), + packedData.factionTypeNames[5].getBuffer(), + packedData.factionTypeNames[6].getBuffer(), + packedData.factionTypeNames[7].getBuffer(), + packedData.networkPlayerNames[0].getBuffer(), + packedData.networkPlayerNames[1].getBuffer(), + packedData.networkPlayerNames[2].getBuffer(), + packedData.networkPlayerNames[3].getBuffer(), + packedData.networkPlayerNames[4].getBuffer(), + packedData.networkPlayerNames[5].getBuffer(), + packedData.networkPlayerNames[6].getBuffer(), + packedData.networkPlayerNames[7].getBuffer(), + packedData.networkPlayerPlatform[0].getBuffer(), + packedData.networkPlayerPlatform[1].getBuffer(), + packedData.networkPlayerPlatform[2].getBuffer(), + packedData.networkPlayerPlatform[3].getBuffer(), + packedData.networkPlayerPlatform[4].getBuffer(), + packedData.networkPlayerPlatform[5].getBuffer(), + packedData.networkPlayerPlatform[6].getBuffer(), + packedData.networkPlayerPlatform[7].getBuffer(), + packedData.networkPlayerStatuses[0], + packedData.networkPlayerStatuses[1], + packedData.networkPlayerStatuses[2], + packedData.networkPlayerStatuses[3], + packedData.networkPlayerStatuses[4], + packedData.networkPlayerStatuses[5], + packedData.networkPlayerStatuses[6], + packedData.networkPlayerStatuses[7], + packedData.networkPlayerLanguages[0].getBuffer(), + packedData.networkPlayerLanguages[1].getBuffer(), + packedData.networkPlayerLanguages[2].getBuffer(), + packedData.networkPlayerLanguages[3].getBuffer(), + packedData.networkPlayerLanguages[4].getBuffer(), + packedData.networkPlayerLanguages[5].getBuffer(), + packedData.networkPlayerLanguages[6].getBuffer(), + packedData.networkPlayerLanguages[7].getBuffer(), + packedData.mapCRC, + packedData.mapFilter, + packedData.tilesetCRC, + packedData.techCRC, + packedData.factionNameList[0].getBuffer(), + packedData.factionNameList[1].getBuffer(), + packedData.factionNameList[2].getBuffer(), + packedData.factionNameList[3].getBuffer(), + packedData.factionNameList[4].getBuffer(), + packedData.factionNameList[5].getBuffer(), + packedData.factionNameList[6].getBuffer(), + packedData.factionNameList[7].getBuffer(), + packedData.factionNameList[8].getBuffer(), + packedData.factionNameList[9].getBuffer(), + packedData.factionNameList[10].getBuffer(), + packedData.factionNameList[11].getBuffer(), + packedData.factionNameList[12].getBuffer(), + packedData.factionNameList[13].getBuffer(), + packedData.factionNameList[14].getBuffer(), + packedData.factionNameList[15].getBuffer(), + packedData.factionNameList[16].getBuffer(), + packedData.factionNameList[17].getBuffer(), + packedData.factionNameList[18].getBuffer(), + packedData.factionNameList[19].getBuffer(), + packedData.factionCRCList[0], + packedData.factionCRCList[1], + packedData.factionCRCList[2], + packedData.factionCRCList[3], + packedData.factionCRCList[4], + packedData.factionCRCList[5], + packedData.factionCRCList[6], + packedData.factionCRCList[7], + packedData.factionCRCList[8], + packedData.factionCRCList[9], + packedData.factionCRCList[10], + packedData.factionCRCList[11], + packedData.factionCRCList[12], + packedData.factionCRCList[13], + packedData.factionCRCList[14], + packedData.factionCRCList[15], + packedData.factionCRCList[16], + packedData.factionCRCList[17], + packedData.factionCRCList[18], + packedData.factionCRCList[19], + packedData.factionControls[0], + packedData.factionControls[1], + packedData.factionControls[2], + packedData.factionControls[3], + packedData.factionControls[4], + packedData.factionControls[5], + packedData.factionControls[6], + packedData.factionControls[7], + packedData.resourceMultiplierIndex[0], + packedData.resourceMultiplierIndex[1], + packedData.resourceMultiplierIndex[2], + packedData.resourceMultiplierIndex[3], + packedData.resourceMultiplierIndex[4], + packedData.resourceMultiplierIndex[5], + packedData.resourceMultiplierIndex[6], + packedData.resourceMultiplierIndex[7], + packedData.thisFactionIndex, + packedData.factionCount, + packedData.teams[0], + packedData.teams[1], + packedData.teams[2], + packedData.teams[3], + packedData.teams[4], + packedData.teams[5], + packedData.teams[6], + packedData.teams[7], + packedData.startLocationIndex[0], + packedData.startLocationIndex[1], + packedData.startLocationIndex[2], + packedData.startLocationIndex[3], + packedData.startLocationIndex[4], + packedData.startLocationIndex[5], + packedData.startLocationIndex[6], + packedData.startLocationIndex[7], + packedData.defaultResources, + packedData.defaultUnits, + packedData.defaultVictoryConditions, + packedData.fogOfWar, + packedData.allowObservers, + packedData.enableObserverModeAtEndGame, + packedData.enableServerControlledAI, + packedData.networkFramePeriod, + packedData.networkPauseGameForLaggedClients, + packedData.pathFinderType, + packedData.flagTypes1, + packedData.aiAcceptSwitchTeamPercentChance, + packedData.cpuReplacementMultiplier, + packedData.masterserver_admin, + packedData.masterserver_admin_factionIndex, + packedData.scenario.getBuffer(), + packedData.networkPlayerUUID[0].getBuffer(), + packedData.networkPlayerUUID[1].getBuffer(), + packedData.networkPlayerUUID[2].getBuffer(), + packedData.networkPlayerUUID[3].getBuffer(), + packedData.networkPlayerUUID[4].getBuffer(), + packedData.networkPlayerUUID[5].getBuffer(), + packedData.networkPlayerUUID[6].getBuffer(), + packedData.networkPlayerUUID[7].getBuffer(), + packedData.networkAllowNativeLanguageTechtree, + packedData.gameUUID.getBuffer() + ); + delete[] buf; + } + return result; + } + void NetworkMessageLaunch::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + data.description.getBuffer(), + data.map.getBuffer(), + data.tileset.getBuffer(), + data.tech.getBuffer(), + data.factionTypeNames[0].getBuffer(), + data.factionTypeNames[1].getBuffer(), + data.factionTypeNames[2].getBuffer(), + data.factionTypeNames[3].getBuffer(), + data.factionTypeNames[4].getBuffer(), + data.factionTypeNames[5].getBuffer(), + data.factionTypeNames[6].getBuffer(), + data.factionTypeNames[7].getBuffer(), + data.networkPlayerNames[0].getBuffer(), + data.networkPlayerNames[1].getBuffer(), + data.networkPlayerNames[2].getBuffer(), + data.networkPlayerNames[3].getBuffer(), + data.networkPlayerNames[4].getBuffer(), + data.networkPlayerNames[5].getBuffer(), + data.networkPlayerNames[6].getBuffer(), + data.networkPlayerNames[7].getBuffer(), + data.networkPlayerPlatform[0].getBuffer(), + data.networkPlayerPlatform[1].getBuffer(), + data.networkPlayerPlatform[2].getBuffer(), + data.networkPlayerPlatform[3].getBuffer(), + data.networkPlayerPlatform[4].getBuffer(), + data.networkPlayerPlatform[5].getBuffer(), + data.networkPlayerPlatform[6].getBuffer(), + data.networkPlayerPlatform[7].getBuffer(), + &data.networkPlayerStatuses[0], + &data.networkPlayerStatuses[1], + &data.networkPlayerStatuses[2], + &data.networkPlayerStatuses[3], + &data.networkPlayerStatuses[4], + &data.networkPlayerStatuses[5], + &data.networkPlayerStatuses[6], + &data.networkPlayerStatuses[7], + data.networkPlayerLanguages[0].getBuffer(), + data.networkPlayerLanguages[1].getBuffer(), + data.networkPlayerLanguages[2].getBuffer(), + data.networkPlayerLanguages[3].getBuffer(), + data.networkPlayerLanguages[4].getBuffer(), + data.networkPlayerLanguages[5].getBuffer(), + data.networkPlayerLanguages[6].getBuffer(), + data.networkPlayerLanguages[7].getBuffer(), + &data.mapFilter, + &data.mapCRC, + &data.tilesetCRC, + &data.techCRC, + data.factionNameList[0].getBuffer(), + data.factionNameList[1].getBuffer(), + data.factionNameList[2].getBuffer(), + data.factionNameList[3].getBuffer(), + data.factionNameList[4].getBuffer(), + data.factionNameList[5].getBuffer(), + data.factionNameList[6].getBuffer(), + data.factionNameList[7].getBuffer(), + data.factionNameList[8].getBuffer(), + data.factionNameList[9].getBuffer(), + data.factionNameList[10].getBuffer(), + data.factionNameList[11].getBuffer(), + data.factionNameList[12].getBuffer(), + data.factionNameList[13].getBuffer(), + data.factionNameList[14].getBuffer(), + data.factionNameList[15].getBuffer(), + data.factionNameList[16].getBuffer(), + data.factionNameList[17].getBuffer(), + data.factionNameList[18].getBuffer(), + data.factionNameList[19].getBuffer(), + &data.factionCRCList[0], + &data.factionCRCList[1], + &data.factionCRCList[2], + &data.factionCRCList[3], + &data.factionCRCList[4], + &data.factionCRCList[5], + &data.factionCRCList[6], + &data.factionCRCList[7], + &data.factionCRCList[8], + &data.factionCRCList[9], + &data.factionCRCList[10], + &data.factionCRCList[11], + &data.factionCRCList[12], + &data.factionCRCList[13], + &data.factionCRCList[14], + &data.factionCRCList[15], + &data.factionCRCList[16], + &data.factionCRCList[17], + &data.factionCRCList[18], + &data.factionCRCList[19], + &data.factionControls[0], + &data.factionControls[1], + &data.factionControls[2], + &data.factionControls[3], + &data.factionControls[4], + &data.factionControls[5], + &data.factionControls[6], + &data.factionControls[7], + &data.resourceMultiplierIndex[0], + &data.resourceMultiplierIndex[1], + &data.resourceMultiplierIndex[2], + &data.resourceMultiplierIndex[3], + &data.resourceMultiplierIndex[4], + &data.resourceMultiplierIndex[5], + &data.resourceMultiplierIndex[6], + &data.resourceMultiplierIndex[7], + &data.thisFactionIndex, + &data.factionCount, + &data.teams[0], + &data.teams[1], + &data.teams[2], + &data.teams[3], + &data.teams[4], + &data.teams[5], + &data.teams[6], + &data.teams[7], + &data.startLocationIndex[0], + &data.startLocationIndex[1], + &data.startLocationIndex[2], + &data.startLocationIndex[3], + &data.startLocationIndex[4], + &data.startLocationIndex[5], + &data.startLocationIndex[6], + &data.startLocationIndex[7], + &data.defaultResources, + &data.defaultUnits, + &data.defaultVictoryConditions, + &data.fogOfWar, + &data.allowObservers, + &data.enableObserverModeAtEndGame, + &data.enableServerControlledAI, + &data.networkFramePeriod, + &data.networkPauseGameForLaggedClients, + &data.pathFinderType, + &data.flagTypes1, + &data.aiAcceptSwitchTeamPercentChance, + &data.cpuReplacementMultiplier, + &data.masterserver_admin, + &data.masterserver_admin_factionIndex, + data.scenario.getBuffer(), + data.networkPlayerUUID[0].getBuffer(), + data.networkPlayerUUID[1].getBuffer(), + data.networkPlayerUUID[2].getBuffer(), + data.networkPlayerUUID[3].getBuffer(), + data.networkPlayerUUID[4].getBuffer(), + data.networkPlayerUUID[5].getBuffer(), + data.networkPlayerUUID[6].getBuffer(), + data.networkPlayerUUID[7].getBuffer(), + &data.networkAllowNativeLanguageTechtree, + data.gameUUID.getBuffer() ); - return buf; -} + } -bool NetworkMessageLaunch::receive(Socket* socket, NetworkMessageType type) { - bool result = receive(socket); - messageType = type; - return result; -} + unsigned char * NetworkMessageLaunch::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), + messageType, + data.description.getBuffer(), + data.map.getBuffer(), + data.tileset.getBuffer(), + data.tech.getBuffer(), + data.factionTypeNames[0].getBuffer(), + data.factionTypeNames[1].getBuffer(), + data.factionTypeNames[2].getBuffer(), + data.factionTypeNames[3].getBuffer(), + data.factionTypeNames[4].getBuffer(), + data.factionTypeNames[5].getBuffer(), + data.factionTypeNames[6].getBuffer(), + data.factionTypeNames[7].getBuffer(), + data.networkPlayerNames[0].getBuffer(), + data.networkPlayerNames[1].getBuffer(), + data.networkPlayerNames[2].getBuffer(), + data.networkPlayerNames[3].getBuffer(), + data.networkPlayerNames[4].getBuffer(), + data.networkPlayerNames[5].getBuffer(), + data.networkPlayerNames[6].getBuffer(), + data.networkPlayerNames[7].getBuffer(), + data.networkPlayerPlatform[0].getBuffer(), + data.networkPlayerPlatform[1].getBuffer(), + data.networkPlayerPlatform[2].getBuffer(), + data.networkPlayerPlatform[3].getBuffer(), + data.networkPlayerPlatform[4].getBuffer(), + data.networkPlayerPlatform[5].getBuffer(), + data.networkPlayerPlatform[6].getBuffer(), + data.networkPlayerPlatform[7].getBuffer(), + data.networkPlayerStatuses[0], + data.networkPlayerStatuses[1], + data.networkPlayerStatuses[2], + data.networkPlayerStatuses[3], + data.networkPlayerStatuses[4], + data.networkPlayerStatuses[5], + data.networkPlayerStatuses[6], + data.networkPlayerStatuses[7], + data.networkPlayerLanguages[0].getBuffer(), + data.networkPlayerLanguages[1].getBuffer(), + data.networkPlayerLanguages[2].getBuffer(), + data.networkPlayerLanguages[3].getBuffer(), + data.networkPlayerLanguages[4].getBuffer(), + data.networkPlayerLanguages[5].getBuffer(), + data.networkPlayerLanguages[6].getBuffer(), + data.networkPlayerLanguages[7].getBuffer(), + data.mapFilter, + data.mapCRC, + data.tilesetCRC, + data.techCRC, + data.factionNameList[0].getBuffer(), + data.factionNameList[1].getBuffer(), + data.factionNameList[2].getBuffer(), + data.factionNameList[3].getBuffer(), + data.factionNameList[4].getBuffer(), + data.factionNameList[5].getBuffer(), + data.factionNameList[6].getBuffer(), + data.factionNameList[7].getBuffer(), + data.factionNameList[8].getBuffer(), + data.factionNameList[9].getBuffer(), + data.factionNameList[10].getBuffer(), + data.factionNameList[11].getBuffer(), + data.factionNameList[12].getBuffer(), + data.factionNameList[13].getBuffer(), + data.factionNameList[14].getBuffer(), + data.factionNameList[15].getBuffer(), + data.factionNameList[16].getBuffer(), + data.factionNameList[17].getBuffer(), + data.factionNameList[18].getBuffer(), + data.factionNameList[19].getBuffer(), + data.factionCRCList[0], + data.factionCRCList[1], + data.factionCRCList[2], + data.factionCRCList[3], + data.factionCRCList[4], + data.factionCRCList[5], + data.factionCRCList[6], + data.factionCRCList[7], + data.factionCRCList[8], + data.factionCRCList[9], + data.factionCRCList[10], + data.factionCRCList[11], + data.factionCRCList[12], + data.factionCRCList[13], + data.factionCRCList[14], + data.factionCRCList[15], + data.factionCRCList[16], + data.factionCRCList[17], + data.factionCRCList[18], + data.factionCRCList[19], + data.factionControls[0], + data.factionControls[1], + data.factionControls[2], + data.factionControls[3], + data.factionControls[4], + data.factionControls[5], + data.factionControls[6], + data.factionControls[7], + data.resourceMultiplierIndex[0], + data.resourceMultiplierIndex[1], + data.resourceMultiplierIndex[2], + data.resourceMultiplierIndex[3], + data.resourceMultiplierIndex[4], + data.resourceMultiplierIndex[5], + data.resourceMultiplierIndex[6], + data.resourceMultiplierIndex[7], + data.thisFactionIndex, + data.factionCount, + data.teams[0], + data.teams[1], + data.teams[2], + data.teams[3], + data.teams[4], + data.teams[5], + data.teams[6], + data.teams[7], + data.startLocationIndex[0], + data.startLocationIndex[1], + data.startLocationIndex[2], + data.startLocationIndex[3], + data.startLocationIndex[4], + data.startLocationIndex[5], + data.startLocationIndex[6], + data.startLocationIndex[7], + data.defaultResources, + data.defaultUnits, + data.defaultVictoryConditions, + data.fogOfWar, + data.allowObservers, + data.enableObserverModeAtEndGame, + data.enableServerControlledAI, + data.networkFramePeriod, + data.networkPauseGameForLaggedClients, + data.pathFinderType, + data.flagTypes1, + data.aiAcceptSwitchTeamPercentChance, + data.cpuReplacementMultiplier, + data.masterserver_admin, + data.masterserver_admin_factionIndex, + data.scenario.getBuffer(), + data.networkPlayerUUID[0].getBuffer(), + data.networkPlayerUUID[1].getBuffer(), + data.networkPlayerUUID[2].getBuffer(), + data.networkPlayerUUID[3].getBuffer(), + data.networkPlayerUUID[4].getBuffer(), + data.networkPlayerUUID[5].getBuffer(), + data.networkPlayerUUID[6].getBuffer(), + data.networkPlayerUUID[7].getBuffer(), + data.networkAllowNativeLanguageTechtree, + data.gameUUID.getBuffer() + ); + return buf; + } -bool NetworkMessageLaunch::receive(Socket* socket) { - //printf("Receive NetworkMessageLaunch\n"); - bool result = false; + bool NetworkMessageLaunch::receive(Socket* socket, NetworkMessageType type) { + bool result = receive(socket); + messageType = type; + return result; + } - Chrono chrono; - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); + bool NetworkMessageLaunch::receive(Socket* socket) { + //printf("Receive NetworkMessageLaunch\n"); + bool result = false; - if(useOldProtocol == true) { + Chrono chrono; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + if (useOldProtocol == true) { - //printf("UnCompressed launch packet before read compressed size\n"); - result = NetworkMessage::receive(socket, &compressedLength, sizeof(compressedLength), true); - //printf("UnCompressed launch packet after read compressed size: %d\n",compressedLength); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + //printf("UnCompressed launch packet before read compressed size\n"); + result = NetworkMessage::receive(socket, &compressedLength, sizeof(compressedLength), true); + //printf("UnCompressed launch packet after read compressed size: %d\n",compressedLength); - if(result == true && compressedLength > 0 && socket != NULL && socket->isSocketValid()) { - //printf("UnCompressed launch packet before: %u after: %d\n",compressedLength,(int)getDataSize()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - unsigned char *compressedMessage = new unsigned char[compressedLength+1]; - memset(compressedMessage,0,compressedLength+1); + if (result == true && compressedLength > 0 && socket != NULL && socket->isSocketValid()) { + //printf("UnCompressed launch packet before: %u after: %d\n",compressedLength,(int)getDataSize()); - result = NetworkMessage::receive(socket, compressedMessage, compressedLength, true); - //printf("UnCompressed launch packet READ returned: %d\n",result); + unsigned char *compressedMessage = new unsigned char[compressedLength + 1]; + memset(compressedMessage, 0, compressedLength + 1); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + result = NetworkMessage::receive(socket, compressedMessage, compressedLength, true); + //printf("UnCompressed launch packet READ returned: %d\n",result); - if(result == true && socket != NULL && socket->isSocketValid()) { - //printf("UnCompressed launch packet before decompress\n"); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); -// printf("\n"); -// const unsigned char *buf = static_cast(compressedMessage); -// for(unsigned int index = 0; index < (unsigned int)compressedLength; ++index) { -// printf("%u[%X][%d] ",index,buf[index],buf[index]); -// if(index % 10 == 0) { -// printf("\n"); -// } -// } -// printf("\n"); + if (result == true && socket != NULL && socket->isSocketValid()) { + //printf("UnCompressed launch packet before decompress\n"); - unsigned long buffer_size = compressedLength; - std::pair decompressedBuffer = - Shared::CompressionUtil::extractMemoryToMemory(compressedMessage, buffer_size, maxNetworkMessageSize); + // printf("\n"); + // const unsigned char *buf = static_cast(compressedMessage); + // for(unsigned int index = 0; index < (unsigned int)compressedLength; ++index) { + // printf("%u[%X][%d] ",index,buf[index],buf[index]); + // if(index % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + unsigned long buffer_size = compressedLength; + std::pair decompressedBuffer = + Shared::CompressionUtil::extractMemoryToMemory(compressedMessage, buffer_size, maxNetworkMessageSize); - unsigned char *decompressed_buffer = decompressedBuffer.first; - memcpy(&data,decompressed_buffer,decompressedBuffer.second); - delete [] decompressed_buffer; + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + unsigned char *decompressed_buffer = decompressedBuffer.first; + memcpy(&data, decompressed_buffer, decompressedBuffer.second); + delete[] decompressed_buffer; - //printf("SUCCESS UnCompressed launch packet before: %u after: %lu\n",compressedLength,decompressedBuffer.second); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + //printf("SUCCESS UnCompressed launch packet before: %u after: %lu\n",compressedLength,decompressedBuffer.second); + } + delete[] compressedMessage; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + } else if (result == true) { + //printf("Normal launch packet detected (uncompressed)\n"); + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; } - delete [] compressedMessage; + fromEndian(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + + data.description.nullTerminate(); + data.map.nullTerminate(); + data.tileset.nullTerminate(); + data.tech.nullTerminate(); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + data.factionTypeNames[i].nullTerminate(); + data.networkPlayerNames[i].nullTerminate(); + data.networkPlayerPlatform[i].nullTerminate(); + data.networkPlayerLanguages[i].nullTerminate(); + + data.networkPlayerUUID[i].nullTerminate(); + } + for (unsigned int i = 0; i < (unsigned int) maxFactionCRCCount; ++i) { + data.factionNameList[i].nullTerminate(); + } + + data.scenario.nullTerminate(); + + data.gameUUID.nullTerminate(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance, "In [%s::%s Line: %d] took msecs: %lld\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, chrono.getMillis()); + + //for(int i= 0; i < GameConstants::maxPlayers; ++i){ + // printf("Receive index: %d resource multiplier index: %d sizeof(data): %d\n",i,data.resourceMultiplierIndex[i],sizeof(data)); + //} + + return result; } - else if(result == true) { - //printf("Normal launch packet detected (uncompressed)\n"); - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + unsigned char * NetworkMessageLaunch::getData() { + unsigned char *buffer = new unsigned char[getDataSize()]; + memcpy(buffer, &data, getDataSize()); + return buffer; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) chrono.start(); + std::pair NetworkMessageLaunch::getCompressedMessage() { + unsigned char *buffer = this->getData(); + std::pair result = + Shared::CompressionUtil::compressMemoryToMemory(buffer, getDataSize()); + delete[] buffer; + return result; + } - data.description.nullTerminate(); - data.map.nullTerminate(); - data.tileset.nullTerminate(); - data.tech.nullTerminate(); - for(int i= 0; i < GameConstants::maxPlayers; ++i){ - data.factionTypeNames[i].nullTerminate(); - data.networkPlayerNames[i].nullTerminate(); - data.networkPlayerPlatform[i].nullTerminate(); - data.networkPlayerLanguages[i].nullTerminate(); + void NetworkMessageLaunch::send(Socket* socket) { + //printf("Sending NetworkMessageLaunch\n"); - data.networkPlayerUUID[i].nullTerminate(); - } - for(unsigned int i = 0; i < (unsigned int)maxFactionCRCCount; ++i) { - data.factionNameList[i].nullTerminate(); - } + //for(int i= 0; i < GameConstants::maxPlayers; ++i){ + // printf("Send index: %d resource multiplier index: %d sizeof(data): %d\n",i,data.resourceMultiplierIndex[i],sizeof(data)); + //} - data.scenario.nullTerminate(); + if (messageType == nmtLaunch) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtLaunch\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] messageType = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, messageType); + } + toEndian(); - data.gameUUID.nullTerminate(); + if (useOldProtocol == true) { + ////NetworkMessage::send(socket, &messageType, sizeof(messageType)); + //NetworkMessage::send(socket, &data, sizeof(data), messageType); - if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled && chrono.getMillis() > 0) SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] took msecs: %lld\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,chrono.getMillis()); + std::pair compressionResult = getCompressedMessage(); + compressedLength = compressionResult.second; + //printf("Compressed launch packet before: %d after: %lu\n",(int)getDataSize(),compressionResult.second); - //for(int i= 0; i < GameConstants::maxPlayers; ++i){ - // printf("Receive index: %d resource multiplier index: %d sizeof(data): %d\n",i,data.resourceMultiplierIndex[i],sizeof(data)); - //} + // printf("\n"); + // const unsigned char *buf = static_cast(compressionResult.first); + // for(unsigned int index = 0; index < (unsigned int)compressionResult.second; ++index) { + // printf("%u[%X][%d] ",index,buf[index],buf[index]); + // if(index % 10 == 0) { + // printf("\n"); + // } + // } + // printf("\n"); - return result; -} - -unsigned char * NetworkMessageLaunch::getData() { - unsigned char *buffer = new unsigned char[getDataSize()]; - memcpy(buffer,&data,getDataSize()); - return buffer; -} - -std::pair NetworkMessageLaunch::getCompressedMessage() { - unsigned char *buffer = this->getData(); - std::pair result = - Shared::CompressionUtil::compressMemoryToMemory(buffer,getDataSize()); - delete [] buffer; - return result; -} - -void NetworkMessageLaunch::send(Socket* socket) { - //printf("Sending NetworkMessageLaunch\n"); - - //for(int i= 0; i < GameConstants::maxPlayers; ++i){ - // printf("Send index: %d resource multiplier index: %d sizeof(data): %d\n",i,data.resourceMultiplierIndex[i],sizeof(data)); - //} - - if(messageType == nmtLaunch) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtLaunch\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] messageType = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,messageType); - } - toEndian(); - - if(useOldProtocol == true) { - ////NetworkMessage::send(socket, &messageType, sizeof(messageType)); - //NetworkMessage::send(socket, &data, sizeof(data), messageType); - - std::pair compressionResult = getCompressedMessage(); - compressedLength = compressionResult.second; - //printf("Compressed launch packet before: %d after: %lu\n",(int)getDataSize(),compressionResult.second); - -// printf("\n"); -// const unsigned char *buf = static_cast(compressionResult.first); -// for(unsigned int index = 0; index < (unsigned int)compressionResult.second; ++index) { -// printf("%u[%X][%d] ",index,buf[index],buf[index]); -// if(index % 10 == 0) { -// printf("\n"); -// } -// } -// printf("\n"); - -/* - NetworkMessage::send(socket, &messageType, sizeof(messageType)); - if(socket != NULL && socket->isSocketValid()) { - NetworkMessage::send(socket, &compressedLength, sizeof(compressedLength)); - if(socket != NULL && socket->isSocketValid()) { - NetworkMessage::send(socket, compressionResult.first, compressionResult.second); + /* + NetworkMessage::send(socket, &messageType, sizeof(messageType)); + if(socket != NULL && socket->isSocketValid()) { + NetworkMessage::send(socket, &compressedLength, sizeof(compressedLength)); + if(socket != NULL && socket->isSocketValid()) { + NetworkMessage::send(socket, compressionResult.first, compressionResult.second); + } + } + */ + NetworkMessage::send(socket, compressionResult.first, compressionResult.second, messageType, compressedLength); + delete[] compressionResult.first; + //printf("Compressed launch packet SENT\n"); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; } } -*/ - NetworkMessage::send(socket, compressionResult.first, compressionResult.second, messageType, compressedLength); - delete [] compressionResult.first; - //printf("Compressed launch packet SENT\n"); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} -void NetworkMessageLaunch::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - for(int i= 0; i < GameConstants::maxPlayers; ++i){ - data.networkPlayerStatuses[i] = Shared::PlatformByteOrder::toCommonEndian(data.networkPlayerStatuses[i]); - data.factionCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.factionCRCList[i]); - data.factionControls[i] = Shared::PlatformByteOrder::toCommonEndian(data.factionControls[i]); - data.resourceMultiplierIndex[i] = Shared::PlatformByteOrder::toCommonEndian(data.resourceMultiplierIndex[i]); - data.teams[i] = Shared::PlatformByteOrder::toCommonEndian(data.teams[i]); - data.startLocationIndex[i] = Shared::PlatformByteOrder::toCommonEndian(data.startLocationIndex[i]); + void NetworkMessageLaunch::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + data.networkPlayerStatuses[i] = Shared::PlatformByteOrder::toCommonEndian(data.networkPlayerStatuses[i]); + data.factionCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.factionCRCList[i]); + data.factionControls[i] = Shared::PlatformByteOrder::toCommonEndian(data.factionControls[i]); + data.resourceMultiplierIndex[i] = Shared::PlatformByteOrder::toCommonEndian(data.resourceMultiplierIndex[i]); + data.teams[i] = Shared::PlatformByteOrder::toCommonEndian(data.teams[i]); + data.startLocationIndex[i] = Shared::PlatformByteOrder::toCommonEndian(data.startLocationIndex[i]); + } + data.mapFilter = Shared::PlatformByteOrder::toCommonEndian(data.mapFilter); + data.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.mapCRC); + data.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.tilesetCRC); + data.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.techCRC); + data.thisFactionIndex = Shared::PlatformByteOrder::toCommonEndian(data.thisFactionIndex); + data.factionCount = Shared::PlatformByteOrder::toCommonEndian(data.factionCount); + data.defaultResources = Shared::PlatformByteOrder::toCommonEndian(data.defaultResources); + data.defaultUnits = Shared::PlatformByteOrder::toCommonEndian(data.defaultUnits); + + data.defaultVictoryConditions = Shared::PlatformByteOrder::toCommonEndian(data.defaultVictoryConditions); + data.fogOfWar = Shared::PlatformByteOrder::toCommonEndian(data.fogOfWar); + data.allowObservers = Shared::PlatformByteOrder::toCommonEndian(data.allowObservers); + data.enableObserverModeAtEndGame = Shared::PlatformByteOrder::toCommonEndian(data.enableObserverModeAtEndGame); + data.enableServerControlledAI = Shared::PlatformByteOrder::toCommonEndian(data.enableServerControlledAI); + data.networkFramePeriod = Shared::PlatformByteOrder::toCommonEndian(data.networkFramePeriod); + data.networkPauseGameForLaggedClients = Shared::PlatformByteOrder::toCommonEndian(data.networkPauseGameForLaggedClients); + data.pathFinderType = Shared::PlatformByteOrder::toCommonEndian(data.pathFinderType); + data.flagTypes1 = Shared::PlatformByteOrder::toCommonEndian(data.flagTypes1); + + data.aiAcceptSwitchTeamPercentChance = Shared::PlatformByteOrder::toCommonEndian(data.aiAcceptSwitchTeamPercentChance); + data.cpuReplacementMultiplier = Shared::PlatformByteOrder::toCommonEndian(data.cpuReplacementMultiplier); + data.masterserver_admin = Shared::PlatformByteOrder::toCommonEndian(data.masterserver_admin); + data.masterserver_admin_factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.masterserver_admin_factionIndex); + + data.networkAllowNativeLanguageTechtree = Shared::PlatformByteOrder::toCommonEndian(data.networkAllowNativeLanguageTechtree); + } } - data.mapFilter = Shared::PlatformByteOrder::toCommonEndian(data.mapFilter); - data.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.mapCRC); - data.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.tilesetCRC); - data.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.techCRC); - data.thisFactionIndex = Shared::PlatformByteOrder::toCommonEndian(data.thisFactionIndex); - data.factionCount = Shared::PlatformByteOrder::toCommonEndian(data.factionCount); - data.defaultResources = Shared::PlatformByteOrder::toCommonEndian(data.defaultResources); - data.defaultUnits = Shared::PlatformByteOrder::toCommonEndian(data.defaultUnits); - data.defaultVictoryConditions = Shared::PlatformByteOrder::toCommonEndian(data.defaultVictoryConditions); - data.fogOfWar = Shared::PlatformByteOrder::toCommonEndian(data.fogOfWar); - data.allowObservers = Shared::PlatformByteOrder::toCommonEndian(data.allowObservers); - data.enableObserverModeAtEndGame = Shared::PlatformByteOrder::toCommonEndian(data.enableObserverModeAtEndGame); - data.enableServerControlledAI = Shared::PlatformByteOrder::toCommonEndian(data.enableServerControlledAI); - data.networkFramePeriod = Shared::PlatformByteOrder::toCommonEndian(data.networkFramePeriod); - data.networkPauseGameForLaggedClients = Shared::PlatformByteOrder::toCommonEndian(data.networkPauseGameForLaggedClients); - data.pathFinderType = Shared::PlatformByteOrder::toCommonEndian(data.pathFinderType); - data.flagTypes1 = Shared::PlatformByteOrder::toCommonEndian(data.flagTypes1); + void NetworkMessageLaunch::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + for (int i = 0; i < GameConstants::maxPlayers; ++i) { + data.networkPlayerStatuses[i] = Shared::PlatformByteOrder::fromCommonEndian(data.networkPlayerStatuses[i]); + data.factionCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.factionCRCList[i]); + data.factionControls[i] = Shared::PlatformByteOrder::fromCommonEndian(data.factionControls[i]); + data.resourceMultiplierIndex[i] = Shared::PlatformByteOrder::fromCommonEndian(data.resourceMultiplierIndex[i]); + data.teams[i] = Shared::PlatformByteOrder::fromCommonEndian(data.teams[i]); + data.startLocationIndex[i] = Shared::PlatformByteOrder::fromCommonEndian(data.startLocationIndex[i]); + } + data.mapFilter = Shared::PlatformByteOrder::fromCommonEndian(data.mapFilter); + data.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.mapCRC); + data.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.tilesetCRC); + data.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.techCRC); + data.thisFactionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.thisFactionIndex); + data.factionCount = Shared::PlatformByteOrder::fromCommonEndian(data.factionCount); + data.defaultResources = Shared::PlatformByteOrder::fromCommonEndian(data.defaultResources); + data.defaultUnits = Shared::PlatformByteOrder::fromCommonEndian(data.defaultUnits); - data.aiAcceptSwitchTeamPercentChance = Shared::PlatformByteOrder::toCommonEndian(data.aiAcceptSwitchTeamPercentChance); - data.cpuReplacementMultiplier = Shared::PlatformByteOrder::toCommonEndian(data.cpuReplacementMultiplier); - data.masterserver_admin = Shared::PlatformByteOrder::toCommonEndian(data.masterserver_admin); - data.masterserver_admin_factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.masterserver_admin_factionIndex); + data.defaultVictoryConditions = Shared::PlatformByteOrder::fromCommonEndian(data.defaultVictoryConditions); + data.fogOfWar = Shared::PlatformByteOrder::fromCommonEndian(data.fogOfWar); + data.allowObservers = Shared::PlatformByteOrder::fromCommonEndian(data.allowObservers); + data.enableObserverModeAtEndGame = Shared::PlatformByteOrder::fromCommonEndian(data.enableObserverModeAtEndGame); + data.enableServerControlledAI = Shared::PlatformByteOrder::fromCommonEndian(data.enableServerControlledAI); + data.networkFramePeriod = Shared::PlatformByteOrder::fromCommonEndian(data.networkFramePeriod); + data.networkPauseGameForLaggedClients = Shared::PlatformByteOrder::fromCommonEndian(data.networkPauseGameForLaggedClients); + data.pathFinderType = Shared::PlatformByteOrder::fromCommonEndian(data.pathFinderType); + data.flagTypes1 = Shared::PlatformByteOrder::fromCommonEndian(data.flagTypes1); - data.networkAllowNativeLanguageTechtree = Shared::PlatformByteOrder::toCommonEndian(data.networkAllowNativeLanguageTechtree); - } -} + data.aiAcceptSwitchTeamPercentChance = Shared::PlatformByteOrder::fromCommonEndian(data.aiAcceptSwitchTeamPercentChance); + data.cpuReplacementMultiplier = Shared::PlatformByteOrder::fromCommonEndian(data.cpuReplacementMultiplier); + data.masterserver_admin = Shared::PlatformByteOrder::fromCommonEndian(data.masterserver_admin); + data.masterserver_admin_factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.masterserver_admin_factionIndex); -void NetworkMessageLaunch::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - for(int i= 0; i < GameConstants::maxPlayers; ++i){ - data.networkPlayerStatuses[i] = Shared::PlatformByteOrder::fromCommonEndian(data.networkPlayerStatuses[i]); - data.factionCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.factionCRCList[i]); - data.factionControls[i] = Shared::PlatformByteOrder::fromCommonEndian(data.factionControls[i]); - data.resourceMultiplierIndex[i] = Shared::PlatformByteOrder::fromCommonEndian(data.resourceMultiplierIndex[i]); - data.teams[i] = Shared::PlatformByteOrder::fromCommonEndian(data.teams[i]); - data.startLocationIndex[i] = Shared::PlatformByteOrder::fromCommonEndian(data.startLocationIndex[i]); + data.networkAllowNativeLanguageTechtree = Shared::PlatformByteOrder::fromCommonEndian(data.networkAllowNativeLanguageTechtree); + } } - data.mapFilter = Shared::PlatformByteOrder::fromCommonEndian(data.mapFilter); - data.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.mapCRC); - data.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.tilesetCRC); - data.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.techCRC); - data.thisFactionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.thisFactionIndex); - data.factionCount = Shared::PlatformByteOrder::fromCommonEndian(data.factionCount); - data.defaultResources = Shared::PlatformByteOrder::fromCommonEndian(data.defaultResources); - data.defaultUnits = Shared::PlatformByteOrder::fromCommonEndian(data.defaultUnits); - data.defaultVictoryConditions = Shared::PlatformByteOrder::fromCommonEndian(data.defaultVictoryConditions); - data.fogOfWar = Shared::PlatformByteOrder::fromCommonEndian(data.fogOfWar); - data.allowObservers = Shared::PlatformByteOrder::fromCommonEndian(data.allowObservers); - data.enableObserverModeAtEndGame = Shared::PlatformByteOrder::fromCommonEndian(data.enableObserverModeAtEndGame); - data.enableServerControlledAI = Shared::PlatformByteOrder::fromCommonEndian(data.enableServerControlledAI); - data.networkFramePeriod = Shared::PlatformByteOrder::fromCommonEndian(data.networkFramePeriod); - data.networkPauseGameForLaggedClients = Shared::PlatformByteOrder::fromCommonEndian(data.networkPauseGameForLaggedClients); - data.pathFinderType = Shared::PlatformByteOrder::fromCommonEndian(data.pathFinderType); - data.flagTypes1 = Shared::PlatformByteOrder::fromCommonEndian(data.flagTypes1); + // ===================================================== + // class NetworkMessageLaunch + // ===================================================== - data.aiAcceptSwitchTeamPercentChance = Shared::PlatformByteOrder::fromCommonEndian(data.aiAcceptSwitchTeamPercentChance); - data.cpuReplacementMultiplier = Shared::PlatformByteOrder::fromCommonEndian(data.cpuReplacementMultiplier); - data.masterserver_admin = Shared::PlatformByteOrder::fromCommonEndian(data.masterserver_admin); - data.masterserver_admin_factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.masterserver_admin_factionIndex); + NetworkMessageCommandList::NetworkMessageCommandList(int32 frameCount) { + data.messageType = nmtCommandList; + data.header.frameCount = frameCount; + data.header.commandCount = 0; + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + data.header.networkPlayerFactionCRC[index] = 0; + } + } - data.networkAllowNativeLanguageTechtree = Shared::PlatformByteOrder::fromCommonEndian(data.networkAllowNativeLanguageTechtree); - } -} + bool NetworkMessageCommandList::addCommand(const NetworkCommand* networkCommand) { + data.commands.push_back(*networkCommand); + data.header.commandCount++; + return true; + } -// ===================================================== -// class NetworkMessageLaunch -// ===================================================== + const char * NetworkMessageCommandList::getPackedMessageFormatHeader() const { + return "cHlLLLLLLLL"; + } -NetworkMessageCommandList::NetworkMessageCommandList(int32 frameCount) { - data.messageType = nmtCommandList; - data.header.frameCount= frameCount; - data.header.commandCount= 0; - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - data.header.networkPlayerFactionCRC[index]=0; - } -} + unsigned int NetworkMessageCommandList::getPackedSizeHeader() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + init(packedData); + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormatHeader(), + packedData.messageType, + packedData.header.commandCount, + packedData.header.frameCount, + packedData.header.networkPlayerFactionCRC[0], + packedData.header.networkPlayerFactionCRC[1], + packedData.header.networkPlayerFactionCRC[2], + packedData.header.networkPlayerFactionCRC[3], + packedData.header.networkPlayerFactionCRC[4], + packedData.header.networkPlayerFactionCRC[5], + packedData.header.networkPlayerFactionCRC[6], + packedData.header.networkPlayerFactionCRC[7]); + delete[] buf; + } + return result; + } + void NetworkMessageCommandList::unpackMessageHeader(unsigned char *buf) { + unpack(buf, getPackedMessageFormatHeader(), + &data.messageType, + &data.header.commandCount, + &data.header.frameCount, + &data.header.networkPlayerFactionCRC[0], + &data.header.networkPlayerFactionCRC[1], + &data.header.networkPlayerFactionCRC[2], + &data.header.networkPlayerFactionCRC[3], + &data.header.networkPlayerFactionCRC[4], + &data.header.networkPlayerFactionCRC[5], + &data.header.networkPlayerFactionCRC[6], + &data.header.networkPlayerFactionCRC[7]); + } -bool NetworkMessageCommandList::addCommand(const NetworkCommand* networkCommand){ - data.commands.push_back(*networkCommand); - data.header.commandCount++; - return true; -} + unsigned char * NetworkMessageCommandList::packMessageHeader() { + unsigned char *buf = new unsigned char[getPackedSizeHeader() + 1]; + pack(buf, getPackedMessageFormatHeader(), + data.messageType, + data.header.commandCount, + data.header.frameCount, + data.header.networkPlayerFactionCRC[0], + data.header.networkPlayerFactionCRC[1], + data.header.networkPlayerFactionCRC[2], + data.header.networkPlayerFactionCRC[3], + data.header.networkPlayerFactionCRC[4], + data.header.networkPlayerFactionCRC[5], + data.header.networkPlayerFactionCRC[6], + data.header.networkPlayerFactionCRC[7]); + return buf; + } -const char * NetworkMessageCommandList::getPackedMessageFormatHeader() const { - return "cHlLLLLLLLL"; -} + const char * NetworkMessageCommandList::getPackedMessageFormatDetail() const { + return "hlhhhhlccHccll"; + } -unsigned int NetworkMessageCommandList::getPackedSizeHeader() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - init(packedData); - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormatHeader(), - packedData.messageType, - packedData.header.commandCount, - packedData.header.frameCount, - packedData.header.networkPlayerFactionCRC[0], - packedData.header.networkPlayerFactionCRC[1], - packedData.header.networkPlayerFactionCRC[2], - packedData.header.networkPlayerFactionCRC[3], - packedData.header.networkPlayerFactionCRC[4], - packedData.header.networkPlayerFactionCRC[5], - packedData.header.networkPlayerFactionCRC[6], - packedData.header.networkPlayerFactionCRC[7]); - delete [] buf; - } - return result; -} -void NetworkMessageCommandList::unpackMessageHeader(unsigned char *buf) { - unpack(buf, getPackedMessageFormatHeader(), - &data.messageType, - &data.header.commandCount, - &data.header.frameCount, - &data.header.networkPlayerFactionCRC[0], - &data.header.networkPlayerFactionCRC[1], - &data.header.networkPlayerFactionCRC[2], - &data.header.networkPlayerFactionCRC[3], - &data.header.networkPlayerFactionCRC[4], - &data.header.networkPlayerFactionCRC[5], - &data.header.networkPlayerFactionCRC[6], - &data.header.networkPlayerFactionCRC[7]); -} - -unsigned char * NetworkMessageCommandList::packMessageHeader() { - unsigned char *buf = new unsigned char[getPackedSizeHeader()+1]; - pack(buf, getPackedMessageFormatHeader(), - data.messageType, - data.header.commandCount, - data.header.frameCount, - data.header.networkPlayerFactionCRC[0], - data.header.networkPlayerFactionCRC[1], - data.header.networkPlayerFactionCRC[2], - data.header.networkPlayerFactionCRC[3], - data.header.networkPlayerFactionCRC[4], - data.header.networkPlayerFactionCRC[5], - data.header.networkPlayerFactionCRC[6], - data.header.networkPlayerFactionCRC[7]); - return buf; -} - -const char * NetworkMessageCommandList::getPackedMessageFormatDetail() const { - return "hlhhhhlccHccll"; -} - -unsigned int NetworkMessageCommandList::getPackedSizeDetail(int count) { - unsigned int result = 0; - //if(result == 0) { - for(unsigned int i = 0; i < (unsigned int)count; ++i) { - NetworkCommand packedData; - unsigned char *buf = new unsigned char[sizeof(NetworkCommand)*3]; - result += pack(buf, getPackedMessageFormatDetail(), + unsigned int NetworkMessageCommandList::getPackedSizeDetail(int count) { + unsigned int result = 0; + //if(result == 0) { + for (unsigned int i = 0; i < (unsigned int) count; ++i) { + NetworkCommand packedData; + unsigned char *buf = new unsigned char[sizeof(NetworkCommand) * 3]; + result += pack(buf, getPackedMessageFormatDetail(), packedData.networkCommandType, packedData.unitId, packedData.unitTypeId, @@ -1755,1957 +1734,1920 @@ unsigned int NetworkMessageCommandList::getPackedSizeDetail(int count) { packedData.commandStateType, packedData.commandStateValue, packedData.unitCommandGroupId); - delete [] buf; + delete[] buf; + } + //} + return result; } - //} - return result; -} -void NetworkMessageCommandList::unpackMessageDetail(unsigned char *buf,int count) { - data.commands.clear(); - data.commands.resize(count); - //unsigned int bytes_processed_total = 0; - unsigned char *bufMove = buf; - for(unsigned int i = 0; i < (unsigned int)count; ++i) { - unsigned int bytes_processed = unpack(bufMove, getPackedMessageFormatDetail(), - &data.commands[i].networkCommandType, - &data.commands[i].unitId, - &data.commands[i].unitTypeId, - &data.commands[i].commandTypeId, - &data.commands[i].positionX, - &data.commands[i].positionY, - &data.commands[i].targetId, - &data.commands[i].wantQueue, - &data.commands[i].fromFactionIndex, - &data.commands[i].unitFactionUnitCount, - &data.commands[i].unitFactionIndex, - &data.commands[i].commandStateType, - &data.commands[i].commandStateValue, - &data.commands[i].unitCommandGroupId); - bufMove += bytes_processed; - //bytes_processed_total += bytes_processed; - } - //printf("\nUnPacked detail size = %u\n",bytes_processed_total); -} - -unsigned char * NetworkMessageCommandList::packMessageDetail(uint16 totalCommand) { - int packetSize = getPackedSizeDetail(totalCommand) +1; - unsigned char *buf = new unsigned char[packetSize]; - unsigned char *bufMove = buf; - //unsigned int bytes_processed_total = 0; - for(unsigned int i = 0; i < totalCommand; ++i) { - unsigned int bytes_processed = pack(bufMove, getPackedMessageFormatDetail(), - data.commands[i].networkCommandType, - data.commands[i].unitId, - data.commands[i].unitTypeId, - data.commands[i].commandTypeId, - data.commands[i].positionX, - data.commands[i].positionY, - data.commands[i].targetId, - data.commands[i].wantQueue, - data.commands[i].fromFactionIndex, - data.commands[i].unitFactionUnitCount, - data.commands[i].unitFactionIndex, - data.commands[i].commandStateType, - data.commands[i].commandStateValue, - data.commands[i].unitCommandGroupId); - bufMove += bytes_processed; - //bytes_processed_total += bytes_processed; - } - //printf("\nPacked detail size = %u, allocated = %d\n",bytes_processed_total,packetSize); - return buf; -} - -bool NetworkMessageCommandList::receive(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - unsigned char *buf = NULL; - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data.header, commandListHeaderSize, true); - if(result == true) { - data.messageType = this->getNetworkMessageType(); + void NetworkMessageCommandList::unpackMessageDetail(unsigned char *buf, int count) { + data.commands.clear(); + data.commands.resize(count); + //unsigned int bytes_processed_total = 0; + unsigned char *bufMove = buf; + for (unsigned int i = 0; i < (unsigned int) count; ++i) { + unsigned int bytes_processed = unpack(bufMove, getPackedMessageFormatDetail(), + &data.commands[i].networkCommandType, + &data.commands[i].unitId, + &data.commands[i].unitTypeId, + &data.commands[i].commandTypeId, + &data.commands[i].positionX, + &data.commands[i].positionY, + &data.commands[i].targetId, + &data.commands[i].wantQueue, + &data.commands[i].fromFactionIndex, + &data.commands[i].unitFactionUnitCount, + &data.commands[i].unitFactionIndex, + &data.commands[i].commandStateType, + &data.commands[i].commandStateValue, + &data.commands[i].unitCommandGroupId); + bufMove += bytes_processed; + //bytes_processed_total += bytes_processed; + } + //printf("\nUnPacked detail size = %u\n",bytes_processed_total); } - //printf("!!! =====> IN Network hdr cmd get frame: %d data.header.commandCount: %u\n",data.header.frameCount,data.header.commandCount); - } - else { - buf = new unsigned char[getPackedSizeHeader()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSizeHeader(), true); - unpackMessageHeader(buf); - //if(data.header.commandCount) printf("\n\nGot packet size = %u data.messageType = %d\n%s\ncommandcount [%u] framecount [%d]\n",getPackedSizeHeader(),data.header.messageType,buf,data.header.commandCount,data.header.frameCount); - delete [] buf; - } - fromEndianHeader(); - - if(result == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got header, messageType = %d, commandCount = %u, frameCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.messageType,data.header.commandCount,data.header.frameCount); - - //printf("!!! =====> IN Network cmd get frame: %d data.header.commandCount: %u\n",data.header.frameCount,data.header.commandCount); - - if(data.header.commandCount > 0) { - data.commands.resize(data.header.commandCount); - - if(useOldProtocol == true) { - int totalMsgSize = (sizeof(NetworkCommand) * data.header.commandCount); - result = NetworkMessage::receive(socket, &data.commands[0], totalMsgSize, true); - -// if(data.commands[0].getNetworkCommandType() == nctPauseResume) { -// printf("=====> IN Network cmd type: %d [%d] frame: %d\n",data.commands[0].getNetworkCommandType(),nctPauseResume,data.header.frameCount); -// } + unsigned char * NetworkMessageCommandList::packMessageDetail(uint16 totalCommand) { + int packetSize = getPackedSizeDetail(totalCommand) + 1; + unsigned char *buf = new unsigned char[packetSize]; + unsigned char *bufMove = buf; + //unsigned int bytes_processed_total = 0; + for (unsigned int i = 0; i < totalCommand; ++i) { + unsigned int bytes_processed = pack(bufMove, getPackedMessageFormatDetail(), + data.commands[i].networkCommandType, + data.commands[i].unitId, + data.commands[i].unitTypeId, + data.commands[i].commandTypeId, + data.commands[i].positionX, + data.commands[i].positionY, + data.commands[i].targetId, + data.commands[i].wantQueue, + data.commands[i].fromFactionIndex, + data.commands[i].unitFactionUnitCount, + data.commands[i].unitFactionIndex, + data.commands[i].commandStateType, + data.commands[i].commandStateValue, + data.commands[i].unitCommandGroupId); + bufMove += bytes_processed; + //bytes_processed_total += bytes_processed; } - else { - //int totalMsgSize = (sizeof(NetworkCommand) * data.header.commandCount); - //result = NetworkMessage::receive(socket, &data.commands[0], totalMsgSize, true); - buf = new unsigned char[getPackedSizeDetail(data.header.commandCount)+1]; - result = NetworkMessage::receive(socket, buf, getPackedSizeDetail(data.header.commandCount), true); - unpackMessageDetail(buf,data.header.commandCount); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; + //printf("\nPacked detail size = %u, allocated = %d\n",bytes_processed_total,packetSize); + return buf; + } + + bool NetworkMessageCommandList::receive(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + unsigned char *buf = NULL; + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data.header, commandListHeaderSize, true); + if (result == true) { + data.messageType = this->getNetworkMessageType(); + } + + //printf("!!! =====> IN Network hdr cmd get frame: %d data.header.commandCount: %u\n",data.header.frameCount,data.header.commandCount); + } else { + buf = new unsigned char[getPackedSizeHeader() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSizeHeader(), true); + unpackMessageHeader(buf); + //if(data.header.commandCount) printf("\n\nGot packet size = %u data.messageType = %d\n%s\ncommandcount [%u] framecount [%d]\n",getPackedSizeHeader(),data.header.messageType,buf,data.header.commandCount,data.header.frameCount); + delete[] buf; } - fromEndianDetail(); + fromEndianHeader(); -// for(int idx = 0 ; idx < data.header.commandCount; ++idx) { -// const NetworkCommand &cmd = data.commands[idx]; -// printf("========> Got index = %d / %u, got networkCommand [%s]\n",idx, data.header.commandCount,cmd.toString().c_str()); -// } + if (result == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] got header, messageType = %d, commandCount = %u, frameCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.messageType, data.header.commandCount, data.header.frameCount); - if(result == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { - for(int idx = 0 ; idx < data.header.commandCount; ++idx) { + //printf("!!! =====> IN Network cmd get frame: %d data.header.commandCount: %u\n",data.header.frameCount,data.header.commandCount); + + if (data.header.commandCount > 0) { + data.commands.resize(data.header.commandCount); + + if (useOldProtocol == true) { + int totalMsgSize = (sizeof(NetworkCommand) * data.header.commandCount); + result = NetworkMessage::receive(socket, &data.commands[0], totalMsgSize, true); + + // if(data.commands[0].getNetworkCommandType() == nctPauseResume) { + // printf("=====> IN Network cmd type: %d [%d] frame: %d\n",data.commands[0].getNetworkCommandType(),nctPauseResume,data.header.frameCount); + // } + } else { + //int totalMsgSize = (sizeof(NetworkCommand) * data.header.commandCount); + //result = NetworkMessage::receive(socket, &data.commands[0], totalMsgSize, true); + buf = new unsigned char[getPackedSizeDetail(data.header.commandCount) + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSizeDetail(data.header.commandCount), true); + unpackMessageDetail(buf, data.header.commandCount); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndianDetail(); + + // for(int idx = 0 ; idx < data.header.commandCount; ++idx) { + // const NetworkCommand &cmd = data.commands[idx]; + // printf("========> Got index = %d / %u, got networkCommand [%s]\n",idx, data.header.commandCount,cmd.toString().c_str()); + // } + + if (result == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { + for (int idx = 0; idx < data.header.commandCount; ++idx) { + const NetworkCommand &cmd = data.commands[idx]; + + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] index = %d, received networkCommand [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, idx, cmd.toString().c_str()); + } + } + } else { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR Failed to get command data, totalMsgSize = %d.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,totalMsgSize); + } + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ERROR header not received as expected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] ERROR header not received as expected\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + return result; + + } + + unsigned char * NetworkMessageCommandList::getData() { + int headerSize = sizeof(data.header); + uint16 totalCommand = data.header.commandCount; + int detailSize = (sizeof(NetworkCommand) * totalCommand); + int fullBufferSize = headerSize + detailSize; + + unsigned char *buffer = new unsigned char[fullBufferSize]; + memcpy(buffer, &data.header, headerSize); + memcpy(&buffer[headerSize], &data.commands[0], detailSize); + return buffer; + } + + void NetworkMessageCommandList::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtCommandList, frameCount = %d, data.header.commandCount = %d, data.header.messageType = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.frameCount, data.header.commandCount, data.messageType); + + assert(data.messageType == nmtCommandList); + uint16 totalCommand = data.header.commandCount; + toEndianHeader(); + toEndianDetail(totalCommand); + + unsigned char *buf = NULL; + //bool result = false; + if (useOldProtocol == true) { + //printf("<===== OUT Network hdr cmd type: frame: %d totalCommand: %u [%u]\n",data.header.frameCount,totalCommand,data.header.commandCount); + //NetworkMessage::send(socket, &data.messageType, sizeof(data.messageType)); + + //NetworkMessage::send(socket, &data.header, commandListHeaderSize, data.messageType); + unsigned char *send_buffer = getData(); + int headerSize = sizeof(data.header); + uint16 totalCommand = data.header.commandCount; + int detailSize = (sizeof(NetworkCommand) * totalCommand); + int fullBufferSize = headerSize + detailSize; + NetworkMessage::send(socket, send_buffer, fullBufferSize, data.messageType); + delete[] send_buffer; + } else { + //NetworkMessage::send(socket, &data.header, commandListHeaderSize); + buf = packMessageHeader(); + //if(totalCommand) printf("\n\nSend packet size = %u data.messageType = %d\n%s\ncommandcount [%u] framecount [%d]\n",getPackedSizeHeader(),data.header.messageType,buf,totalCommand,data.header.frameCount); + NetworkMessage::send(socket, buf, getPackedSizeHeader()); + delete[] buf; + } + + if (totalCommand > 0) { + //printf("\n#2 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); + //toEndianDetail(totalCommand); + //printf("\n#3 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); + + //bool result = false; + if (useOldProtocol == true) { + // if(data.commands[0].getNetworkCommandType() == nctPauseResume) { + // printf("<===== OUT Network cmd type: %d [%d] frame: %d totalCommand: %u [%u]\n",data.commands[0].getNetworkCommandType(),nctPauseResume,data.header.frameCount,totalCommand,data.header.commandCount); + // } + //NetworkMessage::send(socket, &data.commands[0], (sizeof(NetworkCommand) * totalCommand)); + } else { + buf = packMessageDetail(totalCommand); + //printf("\n#4 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSizeDetail(totalCommand)); + //printf("\n#5 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); + delete[] buf; + //printf("\n#6 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); + + // for(int idx = 0 ; idx < totalCommand; ++idx) { + // const NetworkCommand &cmd = data.commands[idx]; + // printf("========> Send index = %d / %u, sent networkCommand [%s]\n",idx, totalCommand,cmd.toString().c_str()); + // } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] messageType = %d, frameCount = %d, data.commandCount = %d\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.messageType, data.header.frameCount, data.header.commandCount); + + if (totalCommand > 0) { + for (int idx = 0; idx < totalCommand; ++idx) { const NetworkCommand &cmd = data.commands[idx]; - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] index = %d, received networkCommand [%s]\n", - extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,idx, cmd.toString().c_str()); + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] index = %d, sent networkCommand [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, idx, cmd.toString().c_str()); + } + + SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] END of loop, nmtCommandList, frameCount = %d, data.header.commandCount = %d, data.header.messageType = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.frameCount, totalCommand, data.messageType); + } + } + } + + void NetworkMessageCommandList::toEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); + data.header.commandCount = Shared::PlatformByteOrder::toCommonEndian(data.header.commandCount); + data.header.frameCount = Shared::PlatformByteOrder::toCommonEndian(data.header.frameCount); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + data.header.networkPlayerFactionCRC[index] = Shared::PlatformByteOrder::toCommonEndian(data.header.networkPlayerFactionCRC[index]); + } + } + } + void NetworkMessageCommandList::fromEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); + data.header.commandCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.commandCount); + data.header.frameCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.frameCount); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + data.header.networkPlayerFactionCRC[index] = Shared::PlatformByteOrder::fromCommonEndian(data.header.networkPlayerFactionCRC[index]); + } + } + } + + void NetworkMessageCommandList::toEndianDetail(uint16 totalCommand) { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + if (totalCommand > 0) { + for (int idx = 0; idx < totalCommand; ++idx) { + NetworkCommand &cmd = data.commands[idx]; + cmd.toEndian(); } } } - else { - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR Failed to get command data, totalMsgSize = %d.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,totalMsgSize); + } + + void NetworkMessageCommandList::fromEndianDetail() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + if (data.header.commandCount > 0) { + for (int idx = 0; idx < data.header.commandCount; ++idx) { + NetworkCommand &cmd = data.commands[idx]; + cmd.fromEndian(); + } + } } } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR header not received as expected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] ERROR header not received as expected\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - return result; -} + // ===================================================== + // class NetworkMessageText + // ===================================================== -unsigned char * NetworkMessageCommandList::getData() { - int headerSize = sizeof(data.header); - uint16 totalCommand = data.header.commandCount; - int detailSize = (sizeof(NetworkCommand) * totalCommand); - int fullBufferSize = headerSize + detailSize; - - unsigned char *buffer = new unsigned char[fullBufferSize]; - memcpy(buffer,&data.header,headerSize); - memcpy(&buffer[headerSize],&data.commands[0],detailSize); - return buffer; -} - -void NetworkMessageCommandList::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtCommandList, frameCount = %d, data.header.commandCount = %d, data.header.messageType = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.header.frameCount,data.header.commandCount,data.messageType); - - assert(data.messageType == nmtCommandList); - uint16 totalCommand = data.header.commandCount; - toEndianHeader(); - toEndianDetail(totalCommand); - - unsigned char *buf = NULL; - //bool result = false; - if(useOldProtocol == true) { - //printf("<===== OUT Network hdr cmd type: frame: %d totalCommand: %u [%u]\n",data.header.frameCount,totalCommand,data.header.commandCount); - //NetworkMessage::send(socket, &data.messageType, sizeof(data.messageType)); - - //NetworkMessage::send(socket, &data.header, commandListHeaderSize, data.messageType); - unsigned char *send_buffer = getData(); - int headerSize = sizeof(data.header); - uint16 totalCommand = data.header.commandCount; - int detailSize = (sizeof(NetworkCommand) * totalCommand); - int fullBufferSize = headerSize + detailSize; - NetworkMessage::send(socket, send_buffer, fullBufferSize, data.messageType); - delete [] send_buffer; - } - else { - //NetworkMessage::send(socket, &data.header, commandListHeaderSize); - buf = packMessageHeader(); - //if(totalCommand) printf("\n\nSend packet size = %u data.messageType = %d\n%s\ncommandcount [%u] framecount [%d]\n",getPackedSizeHeader(),data.header.messageType,buf,totalCommand,data.header.frameCount); - NetworkMessage::send(socket, buf, getPackedSizeHeader()); - delete [] buf; - } - - if(totalCommand > 0) { - //printf("\n#2 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); - //toEndianDetail(totalCommand); - //printf("\n#3 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); - - //bool result = false; - if(useOldProtocol == true) { -// if(data.commands[0].getNetworkCommandType() == nctPauseResume) { -// printf("<===== OUT Network cmd type: %d [%d] frame: %d totalCommand: %u [%u]\n",data.commands[0].getNetworkCommandType(),nctPauseResume,data.header.frameCount,totalCommand,data.header.commandCount); -// } - //NetworkMessage::send(socket, &data.commands[0], (sizeof(NetworkCommand) * totalCommand)); + NetworkMessageText::NetworkMessageText() { + messageType = nmtText; } - else { - buf = packMessageDetail(totalCommand); - //printf("\n#4 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSizeDetail(totalCommand)); - //printf("\n#5 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); - delete [] buf; - //printf("\n#6 Send packet commandcount [%u] framecount [%d]\n",totalCommand,data.header.frameCount); - - // for(int idx = 0 ; idx < totalCommand; ++idx) { - // const NetworkCommand &cmd = data.commands[idx]; - // printf("========> Send index = %d / %u, sent networkCommand [%s]\n",idx, totalCommand,cmd.toString().c_str()); - // } - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled == true) { - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] messageType = %d, frameCount = %d, data.commandCount = %d\n", - extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.messageType,data.header.frameCount,data.header.commandCount); - - if (totalCommand > 0) { - for(int idx = 0 ; idx < totalCommand; ++idx) { - const NetworkCommand &cmd = data.commands[idx]; - - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] index = %d, sent networkCommand [%s]\n", - extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,idx, cmd.toString().c_str()); - } - - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] END of loop, nmtCommandList, frameCount = %d, data.header.commandCount = %d, data.header.messageType = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.header.frameCount,totalCommand,data.messageType); - } - } -} - -void NetworkMessageCommandList::toEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); - data.header.commandCount = Shared::PlatformByteOrder::toCommonEndian(data.header.commandCount); - data.header.frameCount = Shared::PlatformByteOrder::toCommonEndian(data.header.frameCount); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - data.header.networkPlayerFactionCRC[index] = Shared::PlatformByteOrder::toCommonEndian(data.header.networkPlayerFactionCRC[index]); - } - } -} -void NetworkMessageCommandList::fromEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); - data.header.commandCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.commandCount); - data.header.frameCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.frameCount); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - data.header.networkPlayerFactionCRC[index] = Shared::PlatformByteOrder::fromCommonEndian(data.header.networkPlayerFactionCRC[index]); - } - } -} - -void NetworkMessageCommandList::toEndianDetail(uint16 totalCommand) { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - if(totalCommand > 0) { - for(int idx = 0 ; idx < totalCommand; ++idx) { - NetworkCommand &cmd = data.commands[idx]; - cmd.toEndian(); + NetworkMessageText::NetworkMessageText(const string &text, int teamIndex, int playerIndex, + const string targetLanguage) { + if ((int) text.length() >= maxTextStringSize) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] WARNING / ERROR - text [%s] length = %d, max = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, text.c_str(), text.length(), maxTextStringSize); } - } - } -} -void NetworkMessageCommandList::fromEndianDetail() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - if(data.header.commandCount > 0) { - for(int idx = 0 ; idx < data.header.commandCount; ++idx) { - NetworkCommand &cmd = data.commands[idx]; - cmd.fromEndian(); + messageType = nmtText; + data.text = text.substr(0, maxTextStringSize); + data.teamIndex = teamIndex; + data.playerIndex = playerIndex; + data.targetLanguage = targetLanguage; + } + + NetworkMessageText * NetworkMessageText::getCopy() const { + NetworkMessageText *copy = new NetworkMessageText(); + copy->data = this->data; + return copy; + } + + const char * NetworkMessageText::getPackedMessageFormat() const { + return "c500scc60s"; + } + + unsigned int NetworkMessageText::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + messageType = nmtText; + packedData.playerIndex = 0; + packedData.teamIndex = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.text.getBuffer(), + packedData.teamIndex, + packedData.playerIndex, + packedData.targetLanguage.getBuffer()); + delete[] buf; } + return result; + } + void NetworkMessageText::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + data.text.getBuffer(), + &data.teamIndex, + &data.playerIndex, + data.targetLanguage.getBuffer()); } - } -} -// ===================================================== -// class NetworkMessageText -// ===================================================== - -NetworkMessageText::NetworkMessageText() { - messageType = nmtText; -} -NetworkMessageText::NetworkMessageText(const string &text, int teamIndex, int playerIndex, - const string targetLanguage) { - if((int)text.length() >= maxTextStringSize) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING / ERROR - text [%s] length = %d, max = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,text.c_str(),text.length(),maxTextStringSize); - } - - messageType = nmtText; - data.text = text.substr(0,maxTextStringSize); - data.teamIndex = teamIndex; - data.playerIndex = playerIndex; - data.targetLanguage = targetLanguage; -} - -NetworkMessageText * NetworkMessageText::getCopy() const { - NetworkMessageText *copy = new NetworkMessageText(); - copy->data = this->data; - return copy; -} - -const char * NetworkMessageText::getPackedMessageFormat() const { - return "c500scc60s"; -} - -unsigned int NetworkMessageText::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - messageType = nmtText; - packedData.playerIndex = 0; - packedData.teamIndex = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * NetworkMessageText::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.text.getBuffer(), - packedData.teamIndex, - packedData.playerIndex, - packedData.targetLanguage.getBuffer()); - delete [] buf; - } - return result; -} -void NetworkMessageText::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - data.text.getBuffer(), - &data.teamIndex, - &data.playerIndex, - data.targetLanguage.getBuffer()); -} - -unsigned char * NetworkMessageText::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.text.getBuffer(), - data.teamIndex, - data.playerIndex, - data.targetLanguage.getBuffer()); - return buf; -} - -bool NetworkMessageText::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { - messageType = this->getNetworkMessageType(); + data.text.getBuffer(), + data.teamIndex, + data.playerIndex, + data.targetLanguage.getBuffer()); + return buf; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - data.text.nullTerminate(); - data.targetLanguage.nullTerminate(); + bool NetworkMessageText::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = this->getNetworkMessageType(); + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); - return result; -} + data.text.nullTerminate(); + data.targetLanguage.nullTerminate(); -void NetworkMessageText::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtText\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return result; + } - assert(messageType == nmtText); - toEndian(); + void NetworkMessageText::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtText\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + assert(messageType == nmtText); + toEndian(); -void NetworkMessageText::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.teamIndex = Shared::PlatformByteOrder::toCommonEndian(data.teamIndex); - data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); - } -} -void NetworkMessageText::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.teamIndex = Shared::PlatformByteOrder::fromCommonEndian(data.teamIndex); - data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); - } -} + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } -// ===================================================== -// class NetworkMessageQuit -// ===================================================== + void NetworkMessageText::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.teamIndex = Shared::PlatformByteOrder::toCommonEndian(data.teamIndex); + data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); + } + } + void NetworkMessageText::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.teamIndex = Shared::PlatformByteOrder::fromCommonEndian(data.teamIndex); + data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); + } + } -NetworkMessageQuit::NetworkMessageQuit(){ - messageType = nmtQuit; -} + // ===================================================== + // class NetworkMessageQuit + // ===================================================== -const char * NetworkMessageQuit::getPackedMessageFormat() const { - return "c"; -} + NetworkMessageQuit::NetworkMessageQuit() { + messageType = nmtQuit; + } -unsigned int NetworkMessageQuit::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - //Data packedData; - messageType = 0; - unsigned char *buf = new unsigned char[sizeof(messageType)*3]; - result = pack(buf, getPackedMessageFormat(), + const char * NetworkMessageQuit::getPackedMessageFormat() const { + return "c"; + } + + unsigned int NetworkMessageQuit::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + //Data packedData; + messageType = 0; + unsigned char *buf = new unsigned char[sizeof(messageType) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType); + delete[] buf; + } + return result; + } + void NetworkMessageQuit::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType); + } + + unsigned char * NetworkMessageQuit::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType); - delete [] buf; - } - return result; -} -void NetworkMessageQuit::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType); -} + return buf; + } -unsigned char * NetworkMessageQuit::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType); - return buf; -} - -bool NetworkMessageQuit::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &messageType, sizeof(messageType),true); - } - else { - //fromEndian(); - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - - return result; -} - -void NetworkMessageQuit::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtQuit\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - assert(messageType == nmtQuit); - toEndian(); - - if(useOldProtocol == true) { - NetworkMessage::send(socket, &messageType, sizeof(messageType)); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void NetworkMessageQuit::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - } -} -void NetworkMessageQuit::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - } -} - -// ===================================================== -// class NetworkMessageSynchNetworkGameData -// ===================================================== - -NetworkMessageSynchNetworkGameData::NetworkMessageSynchNetworkGameData(const GameSettings *gameSettings) -{ - data.messageType= nmtSynchNetworkGameData; - - if(gameSettings == NULL) { - throw std::runtime_error("gameSettings == NULL"); - } - data.header.map = gameSettings->getMap(); - data.header.tileset = gameSettings->getTileset(); - data.header.tech = gameSettings->getTech(); - - Config &config = Config::getInstance(); - string scenarioDir = ""; - if(gameSettings->getScenarioDir() != "") { - scenarioDir = gameSettings->getScenarioDir(); - if(EndsWith(scenarioDir, ".xml") == true) { - scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); - scenarioDir = scenarioDir.erase(scenarioDir.size() - gameSettings->getScenario().size(), gameSettings->getScenario().size() + 1); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameSettings->getScenarioDir().c_str(),gameSettings->getScenario().c_str(),scenarioDir.c_str()); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - data.header.tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets,scenarioDir), string("/") + gameSettings->getTileset() + string("/*"), ".xml", NULL); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] data.tilesetCRC = %d, [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__, data.header.tilesetCRC,gameSettings->getTileset().c_str()); - - //tech, load before map because of resources - data.header.techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs,scenarioDir), string("/") + gameSettings->getTech() + string("/*"), ".xml", NULL); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] data.techCRC = %d, [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__, data.header.techCRC,gameSettings->getTech().c_str()); - - vector > vctFileList; - vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs,scenarioDir),string("/") + gameSettings->getTech() + string("/*"), ".xml",&vctFileList); - data.header.techCRCFileCount = min((int)vctFileList.size(),(int)maxFileCRCCount); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] vctFileList.size() = %d, maxFileCRCCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__, vctFileList.size(),maxFileCRCCount); - - for(int idx =0; idx < (int)data.header.techCRCFileCount; ++idx) { - const std::pair &fileInfo = vctFileList[idx]; - data.detail.techCRCFileList[idx] = fileInfo.first; - data.detail.techCRCFileCRCList[idx] = fileInfo.second; - } - - //map - Checksum checksum; - string file = Config::getMapPath(gameSettings->getMap(),scenarioDir,false); - checksum.addFile(file); - data.header.mapCRC = checksum.getSum(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] data.mapCRC = %d, [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__, data.header.mapCRC,gameSettings->getMap().c_str()); -} - -string NetworkMessageSynchNetworkGameData::getTechCRCFileMismatchReport(vector > &vctFileList) { - string result = "Techtree: [" + data.header.tech.getString() + "] Filecount local: " + intToStr(vctFileList.size()) + " remote: " + intToStr(data.header.techCRCFileCount) + "\n"; - if(vctFileList.size() <= 0) { - result = result + "Local player has no files.\n"; - } - else if(data.header.techCRCFileCount <= 0) { - result = result + "Remote player has no files.\n"; - } - else { - for(int idx = 0; idx < (int)vctFileList.size(); ++idx) { - std::pair &fileInfo = vctFileList[idx]; - bool fileFound = false; - uint32 remoteCRC = 0; - for(int j = 0; j < (int)data.header.techCRCFileCount; ++j) { - string networkFile = data.detail.techCRCFileList[j].getString(); - uint32 &networkFileCRC = data.detail.techCRCFileCRCList[j]; - if(fileInfo.first == networkFile) { - fileFound = true; - remoteCRC = networkFileCRC; - break; - } + bool NetworkMessageQuit::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &messageType, sizeof(messageType), true); + } else { + //fromEndian(); + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; } + fromEndian(); - if(fileFound == false) { - result = result + "local file [" + fileInfo.first + "] missing remotely.\n"; - } - else if(fileInfo.second != remoteCRC) { - result = result + "local file [" + fileInfo.first + "] CRC mismatch.\n"; + return result; + } + + void NetworkMessageQuit::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtQuit\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + assert(messageType == nmtQuit); + toEndian(); + + if (useOldProtocol == true) { + NetworkMessage::send(socket, &messageType, sizeof(messageType)); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; } } - for(int i = 0; i < (int)data.header.techCRCFileCount; ++i) { - string networkFile = data.detail.techCRCFileList[i].getString(); - uint32 &networkFileCRC = data.detail.techCRCFileCRCList[i]; - bool fileFound = false; - uint32 localCRC = 0; - for(int idx = 0; idx < (int)vctFileList.size(); ++idx) { - std::pair &fileInfo = vctFileList[idx]; - if(networkFile == fileInfo.first) { - fileFound = true; - localCRC = fileInfo.second; - break; - } - } - - if(fileFound == false) { - result = result + "remote file [" + networkFile + "] missing locally.\n"; - } - else if(networkFileCRC != localCRC) { - result = result + "remote file [" + networkFile + "] CRC mismatch.\n"; + void NetworkMessageQuit::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); } } - } - return result; -} - -const char * NetworkMessageSynchNetworkGameData::getPackedMessageFormatHeader() const { - return "c255s255s255sLLLL"; -} - -unsigned int NetworkMessageSynchNetworkGameData::getPackedSizeHeader() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.messageType=0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormatHeader(), - packedData.messageType, - packedData.header.map.getBuffer(), - packedData.header.tileset.getBuffer(), - packedData.header.tech.getBuffer(), - packedData.header.mapCRC, - packedData.header.tilesetCRC, - packedData.header.techCRC, - packedData.header.techCRCFileCount); - delete [] buf; - } - return result; -} -void NetworkMessageSynchNetworkGameData::unpackMessageHeader(unsigned char *buf) { - unpack(buf, getPackedMessageFormatHeader(), - &data.messageType, - data.header.map.getBuffer(), - data.header.tileset.getBuffer(), - data.header.tech.getBuffer(), - &data.header.mapCRC, - &data.header.tilesetCRC, - &data.header.techCRC, - &data.header.techCRCFileCount); -} - -unsigned char * NetworkMessageSynchNetworkGameData::packMessageHeader() { - unsigned char *buf = new unsigned char[getPackedSizeHeader()+1]; - pack(buf, getPackedMessageFormatHeader(), - data.messageType, - data.header.map.getBuffer(), - data.header.tileset.getBuffer(), - data.header.tech.getBuffer(), - data.header.mapCRC, - data.header.tilesetCRC, - data.header.techCRC, - data.header.techCRCFileCount); - - return buf; -} - -unsigned int NetworkMessageSynchNetworkGameData::getPackedSizeDetail() { - static unsigned int result = 0; - if(result == 0) { - DataDetail packedData; - for(unsigned int index = 0; index < (unsigned int)maxFileCRCCount; ++index) { - packedData.techCRCFileCRCList[index] = 0; - } - unsigned char *buf = new unsigned char[sizeof(DataDetail)*3]; - - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - result += pack(buf, "255s", - packedData.techCRCFileList[i].getBuffer()); - buf += result; - } - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - result += pack(buf, "L", - packedData.techCRCFileCRCList[i]); - buf += result; - } - - delete [] buf; - } - return result; -} -void NetworkMessageSynchNetworkGameData::unpackMessageDetail(unsigned char *buf) { - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - unsigned int bytes_processed = unpack(buf, "255s", - data.detail.techCRCFileList[i].getBuffer()); - buf += bytes_processed; - } - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - unsigned int bytes_processed = unpack(buf, "L", - &data.detail.techCRCFileCRCList[i]); - buf += bytes_processed; - } -} - -unsigned char * NetworkMessageSynchNetworkGameData::packMessageDetail() { - unsigned char *buf = new unsigned char[sizeof(DataDetail)*3 +1]; - unsigned char *bufMove = buf; - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - unsigned int bytes_processed = pack(bufMove, "255s", - data.detail.techCRCFileList[i].getBuffer()); - bufMove += bytes_processed; - } - for(unsigned int i = 0; i < (unsigned int)maxFileCRCCount; ++i) { - unsigned int bytes_processed = pack(bufMove, "L", - data.detail.techCRCFileCRCList[i]); - bufMove += bytes_processed; - } - - return buf; -} - - -bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to get nmtSynchNetworkGameData\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - data.header.techCRCFileCount = 0; - bool result = NetworkMessage::receive(socket, &data.header, HeaderSize, true); - fromEndianHeader(); - if(result == true) { - data.messageType = nmtSynchNetworkGameData; - } - - if(result == true && data.header.techCRCFileCount > 0) { - data.header.map.nullTerminate(); - data.header.tileset.nullTerminate(); - data.header.tech.nullTerminate(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] messageType = %d, data.techCRCFileCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.messageType,data.header.techCRCFileCount); - - - - // Here we loop possibly multiple times - int packetLoopCount = 1; - if(data.header.techCRCFileCount > (uint32)NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount) { - packetLoopCount = (data.header.techCRCFileCount / NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount); - if(data.header.techCRCFileCount % NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount > 0) { - packetLoopCount++; + void NetworkMessageQuit::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); } } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] packetLoopCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,packetLoopCount); + // ===================================================== + // class NetworkMessageSynchNetworkGameData + // ===================================================== - for(int iPacketLoop = 0; result == true && iPacketLoop < packetLoopCount; ++iPacketLoop) { + NetworkMessageSynchNetworkGameData::NetworkMessageSynchNetworkGameData(const GameSettings *gameSettings) { + data.messageType = nmtSynchNetworkGameData; - int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount; - int maxFileCountPerPacket = maxFileCRCPacketCount; - int packetFileCount = min((uint32)maxFileCountPerPacket,data.header.techCRCFileCount - packetIndex); - int packetDetail1DataSize = (DetailSize1 * packetFileCount); - int packetDetail2DataSize = (DetailSize2 * packetFileCount); + if (gameSettings == NULL) { + throw std::runtime_error("gameSettings == NULL"); + } + data.header.map = gameSettings->getMap(); + data.header.tileset = gameSettings->getTileset(); + data.header.tech = gameSettings->getTech(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] iPacketLoop = %d, packetIndex = %d, maxFileCountPerPacket = %d, packetFileCount = %d, packetDetail1DataSize = %d, packetDetail2DataSize = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPacketLoop,packetIndex,maxFileCountPerPacket,packetFileCount,packetDetail1DataSize,packetDetail2DataSize); - - // Wait a max of x seconds for this message - result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], packetDetail1DataSize, true); - if(result == true) { - for(unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { - data.detail.techCRCFileList[i].nullTerminate(); + Config &config = Config::getInstance(); + string scenarioDir = ""; + if (gameSettings->getScenarioDir() != "") { + scenarioDir = gameSettings->getScenarioDir(); + if (EndsWith(scenarioDir, ".xml") == true) { + scenarioDir = scenarioDir.erase(scenarioDir.size() - 4, 4); + scenarioDir = scenarioDir.erase(scenarioDir.size() - gameSettings->getScenario().size(), gameSettings->getScenario().size() + 1); } - result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], packetDetail2DataSize, true); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] gameSettings.getScenarioDir() = [%s] gameSettings.getScenario() = [%s] scenarioDir = [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, gameSettings->getScenarioDir().c_str(), gameSettings->getScenario().c_str(), scenarioDir.c_str()); } - } - fromEndianDetail(); - } - return result; -} + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void NetworkMessageSynchNetworkGameData::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to send nmtSynchNetworkGameData\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + data.header.tilesetCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTilesets, scenarioDir), string("/") + gameSettings->getTileset() + string("/*"), ".xml", NULL); - assert(data.messageType == nmtSynchNetworkGameData); - uint32 totalFileCount = data.header.techCRCFileCount; - toEndianHeader(); - NetworkMessage::send(socket, &data, HeaderSize); - if(totalFileCount > 0) { - // Here we loop possibly multiple times - int packetLoopCount = 1; - if(totalFileCount > (uint32)NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount) { - packetLoopCount = (totalFileCount / NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount); - if(totalFileCount % NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount > 0) { - packetLoopCount++; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] data.tilesetCRC = %d, [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.tilesetCRC, gameSettings->getTileset().c_str()); + + //tech, load before map because of resources + data.header.techCRC = getFolderTreeContentsCheckSumRecursively(config.getPathListForType(ptTechs, scenarioDir), string("/") + gameSettings->getTech() + string("/*"), ".xml", NULL); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] data.techCRC = %d, [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.techCRC, gameSettings->getTech().c_str()); + + vector > vctFileList; + vctFileList = getFolderTreeContentsCheckSumListRecursively(config.getPathListForType(ptTechs, scenarioDir), string("/") + gameSettings->getTech() + string("/*"), ".xml", &vctFileList); + data.header.techCRCFileCount = min((int) vctFileList.size(), (int) maxFileCRCCount); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] vctFileList.size() = %d, maxFileCRCCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, vctFileList.size(), maxFileCRCCount); + + for (int idx = 0; idx < (int) data.header.techCRCFileCount; ++idx) { + const std::pair &fileInfo = vctFileList[idx]; + data.detail.techCRCFileList[idx] = fileInfo.first; + data.detail.techCRCFileCRCList[idx] = fileInfo.second; } + + //map + Checksum checksum; + string file = Config::getMapPath(gameSettings->getMap(), scenarioDir, false); + checksum.addFile(file); + data.header.mapCRC = checksum.getSum(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] data.mapCRC = %d, [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.mapCRC, gameSettings->getMap().c_str()); } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] packetLoopCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,packetLoopCount); + string NetworkMessageSynchNetworkGameData::getTechCRCFileMismatchReport(vector > &vctFileList) { + string result = "Techtree: [" + data.header.tech.getString() + "] Filecount local: " + intToStr(vctFileList.size()) + " remote: " + intToStr(data.header.techCRCFileCount) + "\n"; + if (vctFileList.size() <= 0) { + result = result + "Local player has no files.\n"; + } else if (data.header.techCRCFileCount <= 0) { + result = result + "Remote player has no files.\n"; + } else { + for (int idx = 0; idx < (int) vctFileList.size(); ++idx) { + std::pair &fileInfo = vctFileList[idx]; + bool fileFound = false; + uint32 remoteCRC = 0; + for (int j = 0; j < (int) data.header.techCRCFileCount; ++j) { + string networkFile = data.detail.techCRCFileList[j].getString(); + uint32 &networkFileCRC = data.detail.techCRCFileCRCList[j]; + if (fileInfo.first == networkFile) { + fileFound = true; + remoteCRC = networkFileCRC; + break; + } + } - for(int iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { - - int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount; - int maxFileCountPerPacket = maxFileCRCPacketCount; - int packetFileCount = min((uint32)maxFileCountPerPacket,totalFileCount - packetIndex); - - NetworkMessage::send(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)); - NetworkMessage::send(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount)); - } - toEndianDetail(totalFileCount); - } -} - -void NetworkMessageSynchNetworkGameData::toEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); - data.header.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.mapCRC); - data.header.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.tilesetCRC); - data.header.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRC); - data.header.techCRCFileCount = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRCFileCount); - } -} -void NetworkMessageSynchNetworkGameData::fromEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); - data.header.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.mapCRC); - data.header.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.tilesetCRC); - data.header.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRC); - data.header.techCRCFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRCFileCount); - } -} - -void NetworkMessageSynchNetworkGameData::toEndianDetail(uint32 totalFileCount) { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - for(unsigned int i = 0; i < totalFileCount; ++i) { - data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.detail.techCRCFileCRCList[i]); - } - } -} -void NetworkMessageSynchNetworkGameData::fromEndianDetail() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - for(unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { - data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.detail.techCRCFileCRCList[i]); - } - } -} - -// ===================================================== -// class NetworkMessageSynchNetworkGameDataStatus -// ===================================================== - -NetworkMessageSynchNetworkGameDataStatus::NetworkMessageSynchNetworkGameDataStatus(uint32 mapCRC, uint32 tilesetCRC, uint32 techCRC, vector > &vctFileList) -{ - data.messageType= nmtSynchNetworkGameDataStatus; - - data.header.tilesetCRC = tilesetCRC; - data.header.techCRC = techCRC; - data.header.mapCRC = mapCRC; - - data.header.techCRCFileCount = min((int)vctFileList.size(),(int)maxFileCRCCount); - for(unsigned int idx =0; idx < data.header.techCRCFileCount; ++idx) { - const std::pair &fileInfo = vctFileList[idx]; - data.detail.techCRCFileList[idx] = fileInfo.first; - data.detail.techCRCFileCRCList[idx] = fileInfo.second; - } -} - -string NetworkMessageSynchNetworkGameDataStatus::getTechCRCFileMismatchReport(string techtree, vector > &vctFileList) { - string result = "Techtree: [" + techtree + "] Filecount local: " + intToStr(vctFileList.size()) + " remote: " + intToStr(data.header.techCRCFileCount) + "\n"; - if(vctFileList.size() <= 0) { - result = result + "Local player has no files.\n"; - } - else if(data.header.techCRCFileCount <= 0) { - result = result + "Remote player has no files.\n"; - } - else { - for(int idx = 0; idx < (int)vctFileList.size(); ++idx) { - std::pair &fileInfo = vctFileList[idx]; - bool fileFound = false; - uint32 remoteCRC = 0; - for(int j = 0; j < (int)data.header.techCRCFileCount; ++j) { - string networkFile = data.detail.techCRCFileList[j].getString(); - uint32 &networkFileCRC = data.detail.techCRCFileCRCList[j]; - if(fileInfo.first == networkFile) { - fileFound = true; - remoteCRC = networkFileCRC; - break; - } - } - - if(fileFound == false) { - result = result + "local file [" + fileInfo.first + "] missing remotely.\n"; - } - else if(fileInfo.second != remoteCRC) { - result = result + "local file [" + fileInfo.first + "] CRC mismatch.\n"; - } - } - - for(int i = 0; i < (int)data.header.techCRCFileCount; ++i) { - string networkFile = data.detail.techCRCFileList[i].getString(); - uint32 &networkFileCRC = data.detail.techCRCFileCRCList[i]; - bool fileFound = false; - uint32 localCRC = 0; - for(int idx = 0; idx < (int)vctFileList.size(); ++idx) { - std::pair &fileInfo = vctFileList[idx]; - - if(networkFile == fileInfo.first) { - fileFound = true; - localCRC = fileInfo.second; - break; - } - } - - if(fileFound == false) { - result = result + "remote file [" + networkFile + "] missing locally.\n"; - } - else if(networkFileCRC != localCRC) { - result = result + "remote file [" + networkFile + "] CRC mismatch.\n"; - } - } - } - return result; -} - -bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to get nmtSynchNetworkGameDataStatus\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - data.header.techCRCFileCount = 0; - - bool result = NetworkMessage::receive(socket, &data.header, HeaderSize, true); - if(result == true && data.header.techCRCFileCount > 0) { - fromEndianHeader(); - // Here we loop possibly multiple times - uint32 packetLoopCount = 1; - if(data.header.techCRCFileCount > NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount) { - packetLoopCount = (data.header.techCRCFileCount / NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount); - if(data.header.techCRCFileCount % NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount > 0) { - packetLoopCount++; - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] packetLoopCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,packetLoopCount); - - for(uint32 iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { - - uint32 packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount; - uint32 maxFileCountPerPacket = maxFileCRCPacketCount; - uint32 packetFileCount = min((uint32)maxFileCountPerPacket,data.header.techCRCFileCount - packetIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] iPacketLoop = %u, packetIndex = %u, packetFileCount = %u\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPacketLoop,packetIndex,packetFileCount); - - result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], ((uint32)DetailSize1 * packetFileCount),true); - if(result == true) { - for(int i = 0; i < (int)data.header.techCRCFileCount; ++i) { - data.detail.techCRCFileList[i].nullTerminate(); + if (fileFound == false) { + result = result + "local file [" + fileInfo.first + "] missing remotely.\n"; + } else if (fileInfo.second != remoteCRC) { + result = result + "local file [" + fileInfo.first + "] CRC mismatch.\n"; + } } - // Wait a max of x seconds for this message - result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], ((uint32)DetailSize2 * packetFileCount),true); + for (int i = 0; i < (int) data.header.techCRCFileCount; ++i) { + string networkFile = data.detail.techCRCFileList[i].getString(); + uint32 &networkFileCRC = data.detail.techCRCFileCRCList[i]; + bool fileFound = false; + uint32 localCRC = 0; + for (int idx = 0; idx < (int) vctFileList.size(); ++idx) { + std::pair &fileInfo = vctFileList[idx]; + if (networkFile == fileInfo.first) { + fileFound = true; + localCRC = fileInfo.second; + break; + } + } + + if (fileFound == false) { + result = result + "remote file [" + networkFile + "] missing locally.\n"; + } else if (networkFileCRC != localCRC) { + result = result + "remote file [" + networkFile + "] CRC mismatch.\n"; + } + } } + return result; } - fromEndianDetail(); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] result = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,result); + const char * NetworkMessageSynchNetworkGameData::getPackedMessageFormatHeader() const { + return "c255s255s255sLLLL"; + } - return result; -} + unsigned int NetworkMessageSynchNetworkGameData::getPackedSizeHeader() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.messageType = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormatHeader(), + packedData.messageType, + packedData.header.map.getBuffer(), + packedData.header.tileset.getBuffer(), + packedData.header.tech.getBuffer(), + packedData.header.mapCRC, + packedData.header.tilesetCRC, + packedData.header.techCRC, + packedData.header.techCRCFileCount); + delete[] buf; + } + return result; + } + void NetworkMessageSynchNetworkGameData::unpackMessageHeader(unsigned char *buf) { + unpack(buf, getPackedMessageFormatHeader(), + &data.messageType, + data.header.map.getBuffer(), + data.header.tileset.getBuffer(), + data.header.tech.getBuffer(), + &data.header.mapCRC, + &data.header.tilesetCRC, + &data.header.techCRC, + &data.header.techCRCFileCount); + } -void NetworkMessageSynchNetworkGameDataStatus::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] about to send nmtSynchNetworkGameDataStatus, data.header.techCRCFileCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.header.techCRCFileCount); + unsigned char * NetworkMessageSynchNetworkGameData::packMessageHeader() { + unsigned char *buf = new unsigned char[getPackedSizeHeader() + 1]; + pack(buf, getPackedMessageFormatHeader(), + data.messageType, + data.header.map.getBuffer(), + data.header.tileset.getBuffer(), + data.header.tech.getBuffer(), + data.header.mapCRC, + data.header.tilesetCRC, + data.header.techCRC, + data.header.techCRCFileCount); - assert(data.messageType == nmtSynchNetworkGameDataStatus); - uint32 totalFileCount = data.header.techCRCFileCount; - toEndianHeader(); - NetworkMessage::send(socket, &data, HeaderSize); - if(totalFileCount > 0) { - // Here we loop possibly multiple times - int packetLoopCount = 1; - if(totalFileCount > NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount) { - packetLoopCount = (totalFileCount / NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount); - if(totalFileCount % NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount > 0) { - packetLoopCount++; + return buf; + } + + unsigned int NetworkMessageSynchNetworkGameData::getPackedSizeDetail() { + static unsigned int result = 0; + if (result == 0) { + DataDetail packedData; + for (unsigned int index = 0; index < (unsigned int) maxFileCRCCount; ++index) { + packedData.techCRCFileCRCList[index] = 0; + } + unsigned char *buf = new unsigned char[sizeof(DataDetail) * 3]; + + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + result += pack(buf, "255s", + packedData.techCRCFileList[i].getBuffer()); + buf += result; + } + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + result += pack(buf, "L", + packedData.techCRCFileCRCList[i]); + buf += result; + } + + delete[] buf; + } + return result; + } + void NetworkMessageSynchNetworkGameData::unpackMessageDetail(unsigned char *buf) { + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + unsigned int bytes_processed = unpack(buf, "255s", + data.detail.techCRCFileList[i].getBuffer()); + buf += bytes_processed; + } + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + unsigned int bytes_processed = unpack(buf, "L", + &data.detail.techCRCFileCRCList[i]); + buf += bytes_processed; } } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] packetLoopCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,packetLoopCount); + unsigned char * NetworkMessageSynchNetworkGameData::packMessageDetail() { + unsigned char *buf = new unsigned char[sizeof(DataDetail) * 3 + 1]; + unsigned char *bufMove = buf; + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + unsigned int bytes_processed = pack(bufMove, "255s", + data.detail.techCRCFileList[i].getBuffer()); + bufMove += bytes_processed; + } + for (unsigned int i = 0; i < (unsigned int) maxFileCRCCount; ++i) { + unsigned int bytes_processed = pack(bufMove, "L", + data.detail.techCRCFileCRCList[i]); + bufMove += bytes_processed; + } - toEndianDetail(totalFileCount); - for(int iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { - - int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount; - int maxFileCountPerPacket = maxFileCRCPacketCount; - int packetFileCount = min((uint32)maxFileCountPerPacket,totalFileCount - packetIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] packetLoop = %d, packetIndex = %d, packetFileCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,iPacketLoop,packetIndex,packetFileCount); - - NetworkMessage::send(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)); - NetworkMessage::send(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount)); + return buf; } - } -} -void NetworkMessageSynchNetworkGameDataStatus::toEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); - data.header.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.mapCRC); - data.header.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.tilesetCRC); - data.header.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRC); - data.header.techCRCFileCount = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRCFileCount); - } -} -void NetworkMessageSynchNetworkGameDataStatus::fromEndianHeader() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); - data.header.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.mapCRC); - data.header.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.tilesetCRC); - data.header.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRC); - data.header.techCRCFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRCFileCount); - } -} + bool NetworkMessageSynchNetworkGameData::receive(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] about to get nmtSynchNetworkGameData\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void NetworkMessageSynchNetworkGameDataStatus::toEndianDetail(uint32 totalFileCount) { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - for(unsigned int i = 0; i < totalFileCount; ++i) { - data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.detail.techCRCFileCRCList[i]); + data.header.techCRCFileCount = 0; + bool result = NetworkMessage::receive(socket, &data.header, HeaderSize, true); + fromEndianHeader(); + if (result == true) { + data.messageType = nmtSynchNetworkGameData; + } + + if (result == true && data.header.techCRCFileCount > 0) { + data.header.map.nullTerminate(); + data.header.tileset.nullTerminate(); + data.header.tech.nullTerminate(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] messageType = %d, data.techCRCFileCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.messageType, data.header.techCRCFileCount); + + + + // Here we loop possibly multiple times + int packetLoopCount = 1; + if (data.header.techCRCFileCount > (uint32) NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount) { + packetLoopCount = (data.header.techCRCFileCount / NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount); + if (data.header.techCRCFileCount % NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount > 0) { + packetLoopCount++; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] packetLoopCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, packetLoopCount); + + for (int iPacketLoop = 0; result == true && iPacketLoop < packetLoopCount; ++iPacketLoop) { + + int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount; + int maxFileCountPerPacket = maxFileCRCPacketCount; + int packetFileCount = min((uint32) maxFileCountPerPacket, data.header.techCRCFileCount - packetIndex); + int packetDetail1DataSize = (DetailSize1 * packetFileCount); + int packetDetail2DataSize = (DetailSize2 * packetFileCount); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] iPacketLoop = %d, packetIndex = %d, maxFileCountPerPacket = %d, packetFileCount = %d, packetDetail1DataSize = %d, packetDetail2DataSize = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, iPacketLoop, packetIndex, maxFileCountPerPacket, packetFileCount, packetDetail1DataSize, packetDetail2DataSize); + + // Wait a max of x seconds for this message + result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], packetDetail1DataSize, true); + if (result == true) { + for (unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { + data.detail.techCRCFileList[i].nullTerminate(); + } + + result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], packetDetail2DataSize, true); + } + } + fromEndianDetail(); + } + + return result; } - } -} -void NetworkMessageSynchNetworkGameDataStatus::fromEndianDetail() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - for(unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { - data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.detail.techCRCFileCRCList[i]); + + void NetworkMessageSynchNetworkGameData::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] about to send nmtSynchNetworkGameData\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + assert(data.messageType == nmtSynchNetworkGameData); + uint32 totalFileCount = data.header.techCRCFileCount; + toEndianHeader(); + NetworkMessage::send(socket, &data, HeaderSize); + if (totalFileCount > 0) { + // Here we loop possibly multiple times + int packetLoopCount = 1; + if (totalFileCount > (uint32) NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount) { + packetLoopCount = (totalFileCount / NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount); + if (totalFileCount % NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount > 0) { + packetLoopCount++; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] packetLoopCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, packetLoopCount); + + for (int iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { + + int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameData::maxFileCRCPacketCount; + int maxFileCountPerPacket = maxFileCRCPacketCount; + int packetFileCount = min((uint32) maxFileCountPerPacket, totalFileCount - packetIndex); + + NetworkMessage::send(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)); + NetworkMessage::send(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount)); + } + toEndianDetail(totalFileCount); + } } - } -} -// ===================================================== -// class NetworkMessageSynchNetworkGameDataFileCRCCheck -// ===================================================== + void NetworkMessageSynchNetworkGameData::toEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); + data.header.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.mapCRC); + data.header.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.tilesetCRC); + data.header.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRC); + data.header.techCRCFileCount = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRCFileCount); + } + } + void NetworkMessageSynchNetworkGameData::fromEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); + data.header.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.mapCRC); + data.header.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.tilesetCRC); + data.header.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRC); + data.header.techCRCFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRCFileCount); + } + } -NetworkMessageSynchNetworkGameDataFileCRCCheck::NetworkMessageSynchNetworkGameDataFileCRCCheck() { - messageType= nmtSynchNetworkGameDataFileCRCCheck; -} + void NetworkMessageSynchNetworkGameData::toEndianDetail(uint32 totalFileCount) { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + for (unsigned int i = 0; i < totalFileCount; ++i) { + data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.detail.techCRCFileCRCList[i]); + } + } + } + void NetworkMessageSynchNetworkGameData::fromEndianDetail() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + for (unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { + data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.detail.techCRCFileCRCList[i]); + } + } + } -NetworkMessageSynchNetworkGameDataFileCRCCheck::NetworkMessageSynchNetworkGameDataFileCRCCheck( - uint32 totalFileCount, uint32 fileIndex, uint32 fileCRC, const string fileName) -{ - messageType= nmtSynchNetworkGameDataFileCRCCheck; + // ===================================================== + // class NetworkMessageSynchNetworkGameDataStatus + // ===================================================== - data.totalFileCount = totalFileCount; - data.fileIndex = fileIndex; - data.fileCRC = fileCRC; - data.fileName = fileName; -} + NetworkMessageSynchNetworkGameDataStatus::NetworkMessageSynchNetworkGameDataStatus(uint32 mapCRC, uint32 tilesetCRC, uint32 techCRC, vector > &vctFileList) { + data.messageType = nmtSynchNetworkGameDataStatus; -const char * NetworkMessageSynchNetworkGameDataFileCRCCheck::getPackedMessageFormat() const { - return "cLLL256s"; -} + data.header.tilesetCRC = tilesetCRC; + data.header.techCRC = techCRC; + data.header.mapCRC = mapCRC; -unsigned int NetworkMessageSynchNetworkGameDataFileCRCCheck::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.fileCRC = 0; - packedData.fileIndex = 0; - messageType = nmtSynchNetworkGameDataFileCRCCheck; - packedData.totalFileCount = 0; + data.header.techCRCFileCount = min((int) vctFileList.size(), (int) maxFileCRCCount); + for (unsigned int idx = 0; idx < data.header.techCRCFileCount; ++idx) { + const std::pair &fileInfo = vctFileList[idx]; + data.detail.techCRCFileList[idx] = fileInfo.first; + data.detail.techCRCFileCRCList[idx] = fileInfo.second; + } + } - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + string NetworkMessageSynchNetworkGameDataStatus::getTechCRCFileMismatchReport(string techtree, vector > &vctFileList) { + string result = "Techtree: [" + techtree + "] Filecount local: " + intToStr(vctFileList.size()) + " remote: " + intToStr(data.header.techCRCFileCount) + "\n"; + if (vctFileList.size() <= 0) { + result = result + "Local player has no files.\n"; + } else if (data.header.techCRCFileCount <= 0) { + result = result + "Remote player has no files.\n"; + } else { + for (int idx = 0; idx < (int) vctFileList.size(); ++idx) { + std::pair &fileInfo = vctFileList[idx]; + bool fileFound = false; + uint32 remoteCRC = 0; + for (int j = 0; j < (int) data.header.techCRCFileCount; ++j) { + string networkFile = data.detail.techCRCFileList[j].getString(); + uint32 &networkFileCRC = data.detail.techCRCFileCRCList[j]; + if (fileInfo.first == networkFile) { + fileFound = true; + remoteCRC = networkFileCRC; + break; + } + } + + if (fileFound == false) { + result = result + "local file [" + fileInfo.first + "] missing remotely.\n"; + } else if (fileInfo.second != remoteCRC) { + result = result + "local file [" + fileInfo.first + "] CRC mismatch.\n"; + } + } + + for (int i = 0; i < (int) data.header.techCRCFileCount; ++i) { + string networkFile = data.detail.techCRCFileList[i].getString(); + uint32 &networkFileCRC = data.detail.techCRCFileCRCList[i]; + bool fileFound = false; + uint32 localCRC = 0; + for (int idx = 0; idx < (int) vctFileList.size(); ++idx) { + std::pair &fileInfo = vctFileList[idx]; + + if (networkFile == fileInfo.first) { + fileFound = true; + localCRC = fileInfo.second; + break; + } + } + + if (fileFound == false) { + result = result + "remote file [" + networkFile + "] missing locally.\n"; + } else if (networkFileCRC != localCRC) { + result = result + "remote file [" + networkFile + "] CRC mismatch.\n"; + } + } + } + return result; + } + + bool NetworkMessageSynchNetworkGameDataStatus::receive(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] about to get nmtSynchNetworkGameDataStatus\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + data.header.techCRCFileCount = 0; + + bool result = NetworkMessage::receive(socket, &data.header, HeaderSize, true); + if (result == true && data.header.techCRCFileCount > 0) { + fromEndianHeader(); + // Here we loop possibly multiple times + uint32 packetLoopCount = 1; + if (data.header.techCRCFileCount > NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount) { + packetLoopCount = (data.header.techCRCFileCount / NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount); + if (data.header.techCRCFileCount % NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount > 0) { + packetLoopCount++; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] packetLoopCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, packetLoopCount); + + for (uint32 iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { + + uint32 packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount; + uint32 maxFileCountPerPacket = maxFileCRCPacketCount; + uint32 packetFileCount = min((uint32) maxFileCountPerPacket, data.header.techCRCFileCount - packetIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] iPacketLoop = %u, packetIndex = %u, packetFileCount = %u\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, iPacketLoop, packetIndex, packetFileCount); + + result = NetworkMessage::receive(socket, &data.detail.techCRCFileList[packetIndex], ((uint32) DetailSize1 * packetFileCount), true); + if (result == true) { + for (int i = 0; i < (int) data.header.techCRCFileCount; ++i) { + data.detail.techCRCFileList[i].nullTerminate(); + } + + // Wait a max of x seconds for this message + result = NetworkMessage::receive(socket, &data.detail.techCRCFileCRCList[packetIndex], ((uint32) DetailSize2 * packetFileCount), true); + } + } + fromEndianDetail(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] result = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, result); + + return result; + } + + void NetworkMessageSynchNetworkGameDataStatus::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] about to send nmtSynchNetworkGameDataStatus, data.header.techCRCFileCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.header.techCRCFileCount); + + assert(data.messageType == nmtSynchNetworkGameDataStatus); + uint32 totalFileCount = data.header.techCRCFileCount; + toEndianHeader(); + NetworkMessage::send(socket, &data, HeaderSize); + if (totalFileCount > 0) { + // Here we loop possibly multiple times + int packetLoopCount = 1; + if (totalFileCount > NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount) { + packetLoopCount = (totalFileCount / NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount); + if (totalFileCount % NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount > 0) { + packetLoopCount++; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] packetLoopCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, packetLoopCount); + + toEndianDetail(totalFileCount); + for (int iPacketLoop = 0; iPacketLoop < packetLoopCount; ++iPacketLoop) { + + int packetIndex = iPacketLoop * NetworkMessageSynchNetworkGameDataStatus::maxFileCRCPacketCount; + int maxFileCountPerPacket = maxFileCRCPacketCount; + int packetFileCount = min((uint32) maxFileCountPerPacket, totalFileCount - packetIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] packetLoop = %d, packetIndex = %d, packetFileCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, iPacketLoop, packetIndex, packetFileCount); + + NetworkMessage::send(socket, &data.detail.techCRCFileList[packetIndex], (DetailSize1 * packetFileCount)); + NetworkMessage::send(socket, &data.detail.techCRCFileCRCList[packetIndex], (DetailSize2 * packetFileCount)); + } + } + } + + void NetworkMessageSynchNetworkGameDataStatus::toEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); + data.header.mapCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.mapCRC); + data.header.tilesetCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.tilesetCRC); + data.header.techCRC = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRC); + data.header.techCRCFileCount = Shared::PlatformByteOrder::toCommonEndian(data.header.techCRCFileCount); + } + } + + void NetworkMessageSynchNetworkGameDataStatus::fromEndianHeader() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); + data.header.mapCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.mapCRC); + data.header.tilesetCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.tilesetCRC); + data.header.techCRC = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRC); + data.header.techCRCFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.header.techCRCFileCount); + } + } + + void NetworkMessageSynchNetworkGameDataStatus::toEndianDetail(uint32 totalFileCount) { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + for (unsigned int i = 0; i < totalFileCount; ++i) { + data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::toCommonEndian(data.detail.techCRCFileCRCList[i]); + } + } + } + void NetworkMessageSynchNetworkGameDataStatus::fromEndianDetail() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + for (unsigned int i = 0; i < data.header.techCRCFileCount; ++i) { + data.detail.techCRCFileCRCList[i] = Shared::PlatformByteOrder::fromCommonEndian(data.detail.techCRCFileCRCList[i]); + } + } + } + + // ===================================================== + // class NetworkMessageSynchNetworkGameDataFileCRCCheck + // ===================================================== + + NetworkMessageSynchNetworkGameDataFileCRCCheck::NetworkMessageSynchNetworkGameDataFileCRCCheck() { + messageType = nmtSynchNetworkGameDataFileCRCCheck; + } + + NetworkMessageSynchNetworkGameDataFileCRCCheck::NetworkMessageSynchNetworkGameDataFileCRCCheck( + uint32 totalFileCount, uint32 fileIndex, uint32 fileCRC, const string fileName) { + messageType = nmtSynchNetworkGameDataFileCRCCheck; + + data.totalFileCount = totalFileCount; + data.fileIndex = fileIndex; + data.fileCRC = fileCRC; + data.fileName = fileName; + } + + const char * NetworkMessageSynchNetworkGameDataFileCRCCheck::getPackedMessageFormat() const { + return "cLLL256s"; + } + + unsigned int NetworkMessageSynchNetworkGameDataFileCRCCheck::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.fileCRC = 0; + packedData.fileIndex = 0; + messageType = nmtSynchNetworkGameDataFileCRCCheck; + packedData.totalFileCount = 0; + + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.totalFileCount, + packedData.fileIndex, + packedData.fileCRC, + packedData.fileName.getBuffer()); + delete[] buf; + } + return result; + } + void NetworkMessageSynchNetworkGameDataFileCRCCheck::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.totalFileCount, + &data.fileIndex, + &data.fileCRC, + data.fileName.getBuffer()); + } + + unsigned char * NetworkMessageSynchNetworkGameDataFileCRCCheck::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.totalFileCount, - packedData.fileIndex, - packedData.fileCRC, - packedData.fileName.getBuffer()); - delete [] buf; - } - return result; -} -void NetworkMessageSynchNetworkGameDataFileCRCCheck::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.totalFileCount, - &data.fileIndex, - &data.fileCRC, - data.fileName.getBuffer()); -} + data.totalFileCount, + data.fileIndex, + data.fileCRC, + data.fileName.getBuffer()); + return buf; + } -unsigned char * NetworkMessageSynchNetworkGameDataFileCRCCheck::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.totalFileCount, - data.fileIndex, - data.fileCRC, - data.fileName.getBuffer()); - return buf; -} + bool NetworkMessageSynchNetworkGameDataFileCRCCheck::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); + data.fileName.nullTerminate(); -bool NetworkMessageSynchNetworkGameDataFileCRCCheck::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data),true); - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - data.fileName.nullTerminate(); + return result; + } - return result; -} + void NetworkMessageSynchNetworkGameDataFileCRCCheck::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtSynchNetworkGameDataFileCRCCheck\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void NetworkMessageSynchNetworkGameDataFileCRCCheck::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtSynchNetworkGameDataFileCRCCheck\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + assert(messageType == nmtSynchNetworkGameDataFileCRCCheck); + toEndian(); - assert(messageType == nmtSynchNetworkGameDataFileCRCCheck); - toEndian(); + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + void NetworkMessageSynchNetworkGameDataFileCRCCheck::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.totalFileCount = Shared::PlatformByteOrder::toCommonEndian(data.totalFileCount); + data.fileIndex = Shared::PlatformByteOrder::toCommonEndian(data.fileIndex); + data.fileCRC = Shared::PlatformByteOrder::toCommonEndian(data.fileCRC); + } + } -void NetworkMessageSynchNetworkGameDataFileCRCCheck::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.totalFileCount = Shared::PlatformByteOrder::toCommonEndian(data.totalFileCount); - data.fileIndex = Shared::PlatformByteOrder::toCommonEndian(data.fileIndex); - data.fileCRC = Shared::PlatformByteOrder::toCommonEndian(data.fileCRC); - } -} + void NetworkMessageSynchNetworkGameDataFileCRCCheck::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.totalFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.totalFileCount); + data.fileIndex = Shared::PlatformByteOrder::fromCommonEndian(data.fileIndex); + data.fileCRC = Shared::PlatformByteOrder::fromCommonEndian(data.fileCRC); + } + } + // ===================================================== + // class NetworkMessageSynchNetworkGameDataFileGet + // ===================================================== + NetworkMessageSynchNetworkGameDataFileGet::NetworkMessageSynchNetworkGameDataFileGet() { + messageType = nmtSynchNetworkGameDataFileGet; + } + NetworkMessageSynchNetworkGameDataFileGet::NetworkMessageSynchNetworkGameDataFileGet(const string fileName) { + messageType = nmtSynchNetworkGameDataFileGet; + data.fileName = fileName; + } -void NetworkMessageSynchNetworkGameDataFileCRCCheck::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.totalFileCount = Shared::PlatformByteOrder::fromCommonEndian(data.totalFileCount); - data.fileIndex = Shared::PlatformByteOrder::fromCommonEndian(data.fileIndex); - data.fileCRC = Shared::PlatformByteOrder::fromCommonEndian(data.fileCRC); - } -} -// ===================================================== -// class NetworkMessageSynchNetworkGameDataFileGet -// ===================================================== -NetworkMessageSynchNetworkGameDataFileGet::NetworkMessageSynchNetworkGameDataFileGet() { - messageType= nmtSynchNetworkGameDataFileGet; -} -NetworkMessageSynchNetworkGameDataFileGet::NetworkMessageSynchNetworkGameDataFileGet(const string fileName) { - messageType= nmtSynchNetworkGameDataFileGet; - data.fileName = fileName; -} + const char * NetworkMessageSynchNetworkGameDataFileGet::getPackedMessageFormat() const { + return "c256s"; + } -const char * NetworkMessageSynchNetworkGameDataFileGet::getPackedMessageFormat() const { - return "c256s"; -} + unsigned int NetworkMessageSynchNetworkGameDataFileGet::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + messageType = nmtSynchNetworkGameDataFileGet; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.fileName.getBuffer()); + delete[] buf; + } + return result; + } + void NetworkMessageSynchNetworkGameDataFileGet::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + data.fileName.getBuffer()); + } -unsigned int NetworkMessageSynchNetworkGameDataFileGet::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - messageType = nmtSynchNetworkGameDataFileGet; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * NetworkMessageSynchNetworkGameDataFileGet::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.fileName.getBuffer()); - delete [] buf; - } - return result; -} -void NetworkMessageSynchNetworkGameDataFileGet::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - data.fileName.getBuffer()); -} + data.fileName.getBuffer()); + return buf; + } -unsigned char * NetworkMessageSynchNetworkGameDataFileGet::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.fileName.getBuffer()); - return buf; -} + bool NetworkMessageSynchNetworkGameDataFileGet::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); + data.fileName.nullTerminate(); -bool NetworkMessageSynchNetworkGameDataFileGet::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data),true); - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - data.fileName.nullTerminate(); + return result; + } - return result; -} + void NetworkMessageSynchNetworkGameDataFileGet::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtSynchNetworkGameDataFileGet\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void NetworkMessageSynchNetworkGameDataFileGet::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtSynchNetworkGameDataFileGet\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + assert(messageType == nmtSynchNetworkGameDataFileGet); + toEndian(); + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } - assert(messageType == nmtSynchNetworkGameDataFileGet); - toEndian(); - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void NetworkMessageSynchNetworkGameDataFileGet::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - } -} -void NetworkMessageSynchNetworkGameDataFileGet::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - } -} + void NetworkMessageSynchNetworkGameDataFileGet::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + } + } + void NetworkMessageSynchNetworkGameDataFileGet::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + } + } -// ===================================================== -// class SwitchSetupRequest -// ===================================================== + // ===================================================== + // class SwitchSetupRequest + // ===================================================== -SwitchSetupRequest::SwitchSetupRequest() { - messageType= nmtSwitchSetupRequest; - data.selectedFactionName=""; - data.currentSlotIndex=-1; - data.toSlotIndex=-1; - data.toTeam = -1; - data.networkPlayerName=""; - data.networkPlayerStatus = npst_None; - data.switchFlags = ssrft_None; - data.language = ""; -} - -SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, - int8 toFactionIndex,int8 toTeam,string networkPlayerName, - int8 networkPlayerStatus, int8 flags, - string language) { - messageType= nmtSwitchSetupRequest; - data.selectedFactionName=selectedFactionName; - data.currentSlotIndex=currentFactionIndex; - data.toSlotIndex=toFactionIndex; - data.toTeam = toTeam; - data.networkPlayerName=networkPlayerName; - data.networkPlayerStatus=networkPlayerStatus; - data.switchFlags = flags; - data.language = language; -} - -const char * SwitchSetupRequest::getPackedMessageFormat() const { - return "c256sccc80scc60s"; -} - -unsigned int SwitchSetupRequest::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.currentSlotIndex = 0; - messageType = nmtSwitchSetupRequest; - packedData.networkPlayerStatus = 0; - packedData.switchFlags = 0; - packedData.toSlotIndex = 0; - packedData.toTeam = 0; - - unsigned char *buf = new unsigned char[sizeof(Data)*3]; - result = pack(buf, getPackedMessageFormat(), - messageType, - packedData.selectedFactionName.getBuffer(), - packedData.currentSlotIndex, - packedData.toSlotIndex, - packedData.toTeam, - packedData.networkPlayerName.getBuffer(), - packedData.networkPlayerStatus, - packedData.switchFlags, - packedData.language.getBuffer()); - delete [] buf; - } - return result; -} -void SwitchSetupRequest::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - data.selectedFactionName.getBuffer(), - &data.currentSlotIndex, - &data.toSlotIndex, - &data.toTeam, - data.networkPlayerName.getBuffer(), - &data.networkPlayerStatus, - &data.switchFlags, - data.language.getBuffer()); -} - -unsigned char * SwitchSetupRequest::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.selectedFactionName.getBuffer(), - data.currentSlotIndex, - data.toSlotIndex, - data.toTeam, - data.networkPlayerName.getBuffer(), - data.networkPlayerStatus, - data.switchFlags, - data.language.getBuffer()); - return buf; -} - -bool SwitchSetupRequest::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { + SwitchSetupRequest::SwitchSetupRequest() { messageType = nmtSwitchSetupRequest; + data.selectedFactionName = ""; + data.currentSlotIndex = -1; + data.toSlotIndex = -1; + data.toTeam = -1; + data.networkPlayerName = ""; + data.networkPlayerStatus = npst_None; + data.switchFlags = ssrft_None; + data.language = ""; } - } - else { - //fromEndian(); - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d\n",getPackedSize(),data.messageType,buf,data.toTeam,data.selectedFactionName.getBuffer(),data.currentFactionIndex,data.toFactionIndex); - delete [] buf; - } - fromEndian(); + SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex, int8 toTeam, string networkPlayerName, + int8 networkPlayerStatus, int8 flags, + string language) { + messageType = nmtSwitchSetupRequest; + data.selectedFactionName = selectedFactionName; + data.currentSlotIndex = currentFactionIndex; + data.toSlotIndex = toFactionIndex; + data.toTeam = toTeam; + data.networkPlayerName = networkPlayerName; + data.networkPlayerStatus = networkPlayerStatus; + data.switchFlags = flags; + data.language = language; + } - data.selectedFactionName.nullTerminate(); - data.networkPlayerName.nullTerminate(); - data.language.nullTerminate(); + const char * SwitchSetupRequest::getPackedMessageFormat() const { + return "c256sccc80scc60s"; + } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] data.networkPlayerName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.networkPlayerName.getString().c_str()); + unsigned int SwitchSetupRequest::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.currentSlotIndex = 0; + messageType = nmtSwitchSetupRequest; + packedData.networkPlayerStatus = 0; + packedData.switchFlags = 0; + packedData.toSlotIndex = 0; + packedData.toTeam = 0; - return result; -} + unsigned char *buf = new unsigned char[sizeof(Data) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.selectedFactionName.getBuffer(), + packedData.currentSlotIndex, + packedData.toSlotIndex, + packedData.toTeam, + packedData.networkPlayerName.getBuffer(), + packedData.networkPlayerStatus, + packedData.switchFlags, + packedData.language.getBuffer()); + delete[] buf; + } + return result; + } + void SwitchSetupRequest::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + data.selectedFactionName.getBuffer(), + &data.currentSlotIndex, + &data.toSlotIndex, + &data.toTeam, + data.networkPlayerName.getBuffer(), + &data.networkPlayerStatus, + &data.switchFlags, + data.language.getBuffer()); + } -void SwitchSetupRequest::send(Socket* socket) { - assert(messageType == nmtSwitchSetupRequest); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] data.networkPlayerName [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,data.networkPlayerName.getString().c_str()); - toEndian(); - - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d\n",getPackedSize(),data.messageType,buf,data.toTeam,data.selectedFactionName.getBuffer(),data.currentFactionIndex,data.toFactionIndex); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void SwitchSetupRequest::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.currentSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.currentSlotIndex); - data.toSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.toSlotIndex); - data.toTeam = Shared::PlatformByteOrder::toCommonEndian(data.toTeam); - data.networkPlayerStatus = Shared::PlatformByteOrder::toCommonEndian(data.networkPlayerStatus); - data.switchFlags = Shared::PlatformByteOrder::toCommonEndian(data.switchFlags); - } -} -void SwitchSetupRequest::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.currentSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.currentSlotIndex); - data.toSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.toSlotIndex); - data.toTeam = Shared::PlatformByteOrder::fromCommonEndian(data.toTeam); - data.networkPlayerStatus = Shared::PlatformByteOrder::fromCommonEndian(data.networkPlayerStatus); - data.switchFlags = Shared::PlatformByteOrder::fromCommonEndian(data.switchFlags); - } -} - -// ===================================================== -// class PlayerIndexMessage -// ===================================================== -PlayerIndexMessage::PlayerIndexMessage(int16 playerIndex) { - messageType = nmtPlayerIndexMessage; - data.playerIndex=playerIndex; -} - -const char * PlayerIndexMessage::getPackedMessageFormat() const { - return "ch"; -} - -unsigned int PlayerIndexMessage::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - messageType = 0; - packedData.playerIndex = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * SwitchSetupRequest::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.playerIndex); - delete [] buf; - } - return result; -} -void PlayerIndexMessage::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.playerIndex); -} + data.selectedFactionName.getBuffer(), + data.currentSlotIndex, + data.toSlotIndex, + data.toTeam, + data.networkPlayerName.getBuffer(), + data.networkPlayerStatus, + data.switchFlags, + data.language.getBuffer()); + return buf; + } -unsigned char * PlayerIndexMessage::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.playerIndex); - return buf; -} + bool SwitchSetupRequest::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { -bool PlayerIndexMessage::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtSwitchSetupRequest; + } + + } else { + //fromEndian(); + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d\n",getPackedSize(),data.messageType,buf,data.toTeam,data.selectedFactionName.getBuffer(),data.currentFactionIndex,data.toFactionIndex); + delete[] buf; + } + fromEndian(); + + data.selectedFactionName.nullTerminate(); + data.networkPlayerName.nullTerminate(); + data.language.nullTerminate(); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] data.networkPlayerName [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.networkPlayerName.getString().c_str()); + + return result; + } + + void SwitchSetupRequest::send(Socket* socket) { + assert(messageType == nmtSwitchSetupRequest); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] data.networkPlayerName [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, data.networkPlayerName.getString().c_str()); + toEndian(); + + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n%s\nTeam = %d faction [%s] currentFactionIndex = %d toFactionIndex = %d\n",getPackedSize(),data.messageType,buf,data.toTeam,data.selectedFactionName.getBuffer(),data.currentFactionIndex,data.toFactionIndex); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void SwitchSetupRequest::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.currentSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.currentSlotIndex); + data.toSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.toSlotIndex); + data.toTeam = Shared::PlatformByteOrder::toCommonEndian(data.toTeam); + data.networkPlayerStatus = Shared::PlatformByteOrder::toCommonEndian(data.networkPlayerStatus); + data.switchFlags = Shared::PlatformByteOrder::toCommonEndian(data.switchFlags); + } + } + void SwitchSetupRequest::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.currentSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.currentSlotIndex); + data.toSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.toSlotIndex); + data.toTeam = Shared::PlatformByteOrder::fromCommonEndian(data.toTeam); + data.networkPlayerStatus = Shared::PlatformByteOrder::fromCommonEndian(data.networkPlayerStatus); + data.switchFlags = Shared::PlatformByteOrder::fromCommonEndian(data.switchFlags); + } + } + + // ===================================================== + // class PlayerIndexMessage + // ===================================================== + PlayerIndexMessage::PlayerIndexMessage(int16 playerIndex) { messageType = nmtPlayerIndexMessage; + data.playerIndex = playerIndex; } - } - else { - //fromEndian(); - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); + const char * PlayerIndexMessage::getPackedMessageFormat() const { + return "ch"; + } - return result; -} + unsigned int PlayerIndexMessage::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + messageType = 0; + packedData.playerIndex = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.playerIndex); + delete[] buf; + } + return result; + } + void PlayerIndexMessage::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.playerIndex); + } -void PlayerIndexMessage::send(Socket* socket) { - assert(messageType == nmtPlayerIndexMessage); - toEndian(); - - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - //NetworkMessage::send(socket, &data, sizeof(data)); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void PlayerIndexMessage::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); - } -} -void PlayerIndexMessage::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); - } -} - -// ===================================================== -// class NetworkMessageLoadingStatus -// ===================================================== -NetworkMessageLoadingStatus::NetworkMessageLoadingStatus(uint32 status) -{ - messageType = nmtLoadingStatusMessage; - data.status=status; -} - -const char * NetworkMessageLoadingStatus::getPackedMessageFormat() const { - return "cL"; -} - -unsigned int NetworkMessageLoadingStatus::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - messageType = 0; - packedData.status = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * PlayerIndexMessage::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.status); - delete [] buf; - } - return result; -} -void NetworkMessageLoadingStatus::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.status); -} + data.playerIndex); + return buf; + } -unsigned char * NetworkMessageLoadingStatus::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.status); - return buf; -} + bool PlayerIndexMessage::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtPlayerIndexMessage; + } -bool NetworkMessageLoadingStatus::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { + } else { + //fromEndian(); + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); + + return result; + } + + void PlayerIndexMessage::send(Socket* socket) { + assert(messageType == nmtPlayerIndexMessage); + toEndian(); + + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + //NetworkMessage::send(socket, &data, sizeof(data)); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void PlayerIndexMessage::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); + } + } + void PlayerIndexMessage::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); + } + } + + // ===================================================== + // class NetworkMessageLoadingStatus + // ===================================================== + NetworkMessageLoadingStatus::NetworkMessageLoadingStatus(uint32 status) { messageType = nmtLoadingStatusMessage; + data.status = status; } - } - else { - //fromEndian(); - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - return result; -} + const char * NetworkMessageLoadingStatus::getPackedMessageFormat() const { + return "cL"; + } -void NetworkMessageLoadingStatus::send(Socket* socket) { - assert(messageType == nmtLoadingStatusMessage); - toEndian(); + unsigned int NetworkMessageLoadingStatus::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + messageType = 0; + packedData.status = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.status); + delete[] buf; + } + return result; + } + void NetworkMessageLoadingStatus::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.status); + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} - -void NetworkMessageLoadingStatus::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.status = Shared::PlatformByteOrder::toCommonEndian(data.status); - } -} -void NetworkMessageLoadingStatus::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.status = Shared::PlatformByteOrder::fromCommonEndian(data.status); - } -} - -// ===================================================== -// class NetworkMessageMarkCell -// ===================================================== -NetworkMessageMarkCell::NetworkMessageMarkCell() { - messageType = nmtMarkCell; -} - -NetworkMessageMarkCell::NetworkMessageMarkCell(Vec2i target, int factionIndex, const string &text, int playerIndex) { - if((int)text.length() >= maxTextStringSize) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING / ERROR - text [%s] length = %d, max = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,text.c_str(),text.length(),maxTextStringSize); - } - - messageType = nmtMarkCell; - data.text = text; - data.targetX = target.x; - data.targetY = target.y; - data.factionIndex = factionIndex; - data.playerIndex = playerIndex; -} - -NetworkMessageMarkCell * NetworkMessageMarkCell::getCopy() const { - NetworkMessageMarkCell *copy = new NetworkMessageMarkCell(); - copy->data = this->data; - return copy; -} - -const char * NetworkMessageMarkCell::getPackedMessageFormat() const { - return "chhcc500s"; -} - -unsigned int NetworkMessageMarkCell::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.factionIndex = 0; - messageType = nmtMarkCell; - packedData.playerIndex = 0; - packedData.targetX = 0; - packedData.targetY = 0; - - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * NetworkMessageLoadingStatus::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.targetX, - packedData.targetY, - packedData.factionIndex, - packedData.playerIndex, - packedData.text.getBuffer()); - delete [] buf; - } - return result; -} -void NetworkMessageMarkCell::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.targetX, - &data.targetY, - &data.factionIndex, - &data.playerIndex, - data.text.getBuffer()); + data.status); + return buf; + } -} + bool NetworkMessageLoadingStatus::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtLoadingStatusMessage; + } + } else { + //fromEndian(); + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); -unsigned char * NetworkMessageMarkCell::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.targetX, - data.targetY, - data.factionIndex, - data.playerIndex, - data.text.getBuffer()); + return result; + } - return buf; -} + void NetworkMessageLoadingStatus::send(Socket* socket) { + assert(messageType == nmtLoadingStatusMessage); + toEndian(); -bool NetworkMessageMarkCell::receive(Socket* socket){ - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void NetworkMessageLoadingStatus::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.status = Shared::PlatformByteOrder::toCommonEndian(data.status); + } + } + void NetworkMessageLoadingStatus::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.status = Shared::PlatformByteOrder::fromCommonEndian(data.status); + } + } + + // ===================================================== + // class NetworkMessageMarkCell + // ===================================================== + NetworkMessageMarkCell::NetworkMessageMarkCell() { messageType = nmtMarkCell; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - data.text.nullTerminate(); - return result; -} + NetworkMessageMarkCell::NetworkMessageMarkCell(Vec2i target, int factionIndex, const string &text, int playerIndex) { + if ((int) text.length() >= maxTextStringSize) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] WARNING / ERROR - text [%s] length = %d, max = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, text.c_str(), text.length(), maxTextStringSize); + } -void NetworkMessageMarkCell::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + messageType = nmtMarkCell; + data.text = text; + data.targetX = target.x; + data.targetY = target.y; + data.factionIndex = factionIndex; + data.playerIndex = playerIndex; + } - assert(messageType == nmtMarkCell); - toEndian(); + NetworkMessageMarkCell * NetworkMessageMarkCell::getCopy() const { + NetworkMessageMarkCell *copy = new NetworkMessageMarkCell(); + copy->data = this->data; + return copy; + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + const char * NetworkMessageMarkCell::getPackedMessageFormat() const { + return "chhcc500s"; + } -void NetworkMessageMarkCell::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); - data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); - } -} -void NetworkMessageMarkCell::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); - data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); - } -} + unsigned int NetworkMessageMarkCell::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.factionIndex = 0; + messageType = nmtMarkCell; + packedData.playerIndex = 0; + packedData.targetX = 0; + packedData.targetY = 0; -// ===================================================== -// class NetworkMessageUnMarkCell -// ===================================================== + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.targetX, + packedData.targetY, + packedData.factionIndex, + packedData.playerIndex, + packedData.text.getBuffer()); + delete[] buf; + } + return result; + } + void NetworkMessageMarkCell::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.targetX, + &data.targetY, + &data.factionIndex, + &data.playerIndex, + data.text.getBuffer()); -NetworkMessageUnMarkCell::NetworkMessageUnMarkCell() { - messageType = nmtUnMarkCell; - data.targetX = 0; - data.targetY = 0; - data.factionIndex = 0; -} + } -NetworkMessageUnMarkCell::NetworkMessageUnMarkCell(Vec2i target, int factionIndex) { - messageType = nmtUnMarkCell; - data.targetX = target.x; - data.targetY = target.y; - data.factionIndex = factionIndex; -} - -NetworkMessageUnMarkCell * NetworkMessageUnMarkCell::getCopy() const { - NetworkMessageUnMarkCell *copy = new NetworkMessageUnMarkCell(); - copy->data = this->data; - return copy; -} - -const char * NetworkMessageUnMarkCell::getPackedMessageFormat() const { - return "chhc"; -} - -unsigned int NetworkMessageUnMarkCell::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.factionIndex = 0; - messageType = 0; - packedData.targetX = 0; - packedData.targetY = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * NetworkMessageMarkCell::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.targetX, - packedData.targetY, - packedData.factionIndex); - delete [] buf; - } - return result; -} -void NetworkMessageUnMarkCell::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.targetX, - &data.targetY, - &data.factionIndex); + data.targetX, + data.targetY, + data.factionIndex, + data.playerIndex, + data.text.getBuffer()); -} + return buf; + } -unsigned char * NetworkMessageUnMarkCell::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.targetX, - data.targetY, - data.factionIndex); + bool NetworkMessageMarkCell::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtMarkCell; + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); - return buf; -} + data.text.nullTerminate(); + return result; + } -bool NetworkMessageUnMarkCell::receive(Socket* socket){ - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { + void NetworkMessageMarkCell::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + assert(messageType == nmtMarkCell); + toEndian(); + + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void NetworkMessageMarkCell::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); + data.playerIndex = Shared::PlatformByteOrder::toCommonEndian(data.playerIndex); + } + } + void NetworkMessageMarkCell::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); + data.playerIndex = Shared::PlatformByteOrder::fromCommonEndian(data.playerIndex); + } + } + + // ===================================================== + // class NetworkMessageUnMarkCell + // ===================================================== + + NetworkMessageUnMarkCell::NetworkMessageUnMarkCell() { messageType = nmtUnMarkCell; + data.targetX = 0; + data.targetY = 0; + data.factionIndex = 0; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - return result; -} + NetworkMessageUnMarkCell::NetworkMessageUnMarkCell(Vec2i target, int factionIndex) { + messageType = nmtUnMarkCell; + data.targetX = target.x; + data.targetY = target.y; + data.factionIndex = factionIndex; + } -void NetworkMessageUnMarkCell::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtUnMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + NetworkMessageUnMarkCell * NetworkMessageUnMarkCell::getCopy() const { + NetworkMessageUnMarkCell *copy = new NetworkMessageUnMarkCell(); + copy->data = this->data; + return copy; + } - assert(messageType == nmtUnMarkCell); - toEndian(); + const char * NetworkMessageUnMarkCell::getPackedMessageFormat() const { + return "chhc"; + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + unsigned int NetworkMessageUnMarkCell::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.factionIndex = 0; + messageType = 0; + packedData.targetX = 0; + packedData.targetY = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.targetX, + packedData.targetY, + packedData.factionIndex); + delete[] buf; + } + return result; + } + void NetworkMessageUnMarkCell::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.targetX, + &data.targetY, + &data.factionIndex); -void NetworkMessageUnMarkCell::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); - } -} -void NetworkMessageUnMarkCell::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); - } -} + } -// ===================================================== -// class NetworkMessageHighlightCell -// ===================================================== - -NetworkMessageHighlightCell::NetworkMessageHighlightCell() { - messageType = nmtHighlightCell; - data.targetX = 0; - data.targetY = 0; - data.factionIndex = 0; -} - -NetworkMessageHighlightCell::NetworkMessageHighlightCell(Vec2i target, int factionIndex) { - messageType = nmtHighlightCell; - data.targetX = target.x; - data.targetY = target.y; - data.factionIndex = factionIndex; -} - -const char * NetworkMessageHighlightCell::getPackedMessageFormat() const { - return "chhc"; -} - -unsigned int NetworkMessageHighlightCell::getPackedSize() { - static unsigned int result = 0; - if(result == 0) { - Data packedData; - packedData.factionIndex = 0; - messageType = 0; - packedData.targetX = 0; - packedData.targetY = 0; - unsigned char *buf = new unsigned char[sizeof(packedData)*3]; - result = pack(buf, getPackedMessageFormat(), + unsigned char * NetworkMessageUnMarkCell::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), messageType, - packedData.targetX, - packedData.targetY, - packedData.factionIndex); - delete [] buf; - } - return result; -} -void NetworkMessageHighlightCell::unpackMessage(unsigned char *buf) { - unpack(buf, getPackedMessageFormat(), - &messageType, - &data.targetX, - &data.targetY, - &data.factionIndex); + data.targetX, + data.targetY, + data.factionIndex); -} - -unsigned char * NetworkMessageHighlightCell::packMessage() { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - pack(buf, getPackedMessageFormat(), - messageType, - data.targetX, - data.targetY, - data.factionIndex); - - return buf; -} - -bool NetworkMessageHighlightCell::receive(Socket* socket) { - bool result = false; - if(useOldProtocol == true) { - result = NetworkMessage::receive(socket, &data, sizeof(data), true); - if(result == true) { - messageType = nmtHighlightCell; + return buf; } - } - else { - unsigned char *buf = new unsigned char[getPackedSize()+1]; - result = NetworkMessage::receive(socket, buf, getPackedSize(), true); - unpackMessage(buf); - //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); - delete [] buf; - } - fromEndian(); - return result; -} -void NetworkMessageHighlightCell::send(Socket* socket) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtMarkCell\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + bool NetworkMessageUnMarkCell::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtUnMarkCell; + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); - assert(messageType == nmtHighlightCell); - toEndian(); + return result; + } - if(useOldProtocol == true) { - //NetworkMessage::send(socket, &messageType, sizeof(messageType)); - NetworkMessage::send(socket, &data, sizeof(data), messageType); - } - else { - unsigned char *buf = packMessage(); - //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); - NetworkMessage::send(socket, buf, getPackedSize()); - delete [] buf; - } -} + void NetworkMessageUnMarkCell::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtUnMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); -void NetworkMessageHighlightCell::toEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); - } -} -void NetworkMessageHighlightCell::fromEndian() { - static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); - if(bigEndianSystem == true) { - messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); - data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); - data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); - data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); - } -} + assert(messageType == nmtUnMarkCell); + toEndian(); -}}//end namespace + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void NetworkMessageUnMarkCell::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); + } + } + void NetworkMessageUnMarkCell::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); + } + } + + // ===================================================== + // class NetworkMessageHighlightCell + // ===================================================== + + NetworkMessageHighlightCell::NetworkMessageHighlightCell() { + messageType = nmtHighlightCell; + data.targetX = 0; + data.targetY = 0; + data.factionIndex = 0; + } + + NetworkMessageHighlightCell::NetworkMessageHighlightCell(Vec2i target, int factionIndex) { + messageType = nmtHighlightCell; + data.targetX = target.x; + data.targetY = target.y; + data.factionIndex = factionIndex; + } + + const char * NetworkMessageHighlightCell::getPackedMessageFormat() const { + return "chhc"; + } + + unsigned int NetworkMessageHighlightCell::getPackedSize() { + static unsigned int result = 0; + if (result == 0) { + Data packedData; + packedData.factionIndex = 0; + messageType = 0; + packedData.targetX = 0; + packedData.targetY = 0; + unsigned char *buf = new unsigned char[sizeof(packedData) * 3]; + result = pack(buf, getPackedMessageFormat(), + messageType, + packedData.targetX, + packedData.targetY, + packedData.factionIndex); + delete[] buf; + } + return result; + } + void NetworkMessageHighlightCell::unpackMessage(unsigned char *buf) { + unpack(buf, getPackedMessageFormat(), + &messageType, + &data.targetX, + &data.targetY, + &data.factionIndex); + + } + + unsigned char * NetworkMessageHighlightCell::packMessage() { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + pack(buf, getPackedMessageFormat(), + messageType, + data.targetX, + data.targetY, + data.factionIndex); + + return buf; + } + + bool NetworkMessageHighlightCell::receive(Socket* socket) { + bool result = false; + if (useOldProtocol == true) { + result = NetworkMessage::receive(socket, &data, sizeof(data), true); + if (result == true) { + messageType = nmtHighlightCell; + } + } else { + unsigned char *buf = new unsigned char[getPackedSize() + 1]; + result = NetworkMessage::receive(socket, buf, getPackedSize(), true); + unpackMessage(buf); + //printf("Got packet size = %u data.messageType = %d\n%s\n",getPackedSize(),data.messageType,buf); + delete[] buf; + } + fromEndian(); + return result; + } + + void NetworkMessageHighlightCell::send(Socket* socket) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] nmtMarkCell\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + assert(messageType == nmtHighlightCell); + toEndian(); + + if (useOldProtocol == true) { + //NetworkMessage::send(socket, &messageType, sizeof(messageType)); + NetworkMessage::send(socket, &data, sizeof(data), messageType); + } else { + unsigned char *buf = packMessage(); + //printf("Send packet size = %u data.messageType = %d\n[%s]\n",getPackedSize(),data.messageType,buf); + NetworkMessage::send(socket, buf, getPackedSize()); + delete[] buf; + } + } + + void NetworkMessageHighlightCell::toEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::toCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::toCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::toCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::toCommonEndian(data.factionIndex); + } + } + void NetworkMessageHighlightCell::fromEndian() { + static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); + if (bigEndianSystem == true) { + messageType = Shared::PlatformByteOrder::fromCommonEndian(messageType); + data.targetX = Shared::PlatformByteOrder::fromCommonEndian(data.targetX); + data.targetY = Shared::PlatformByteOrder::fromCommonEndian(data.targetY); + data.factionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.factionIndex); + } + } + + } +}//end namespace diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index fda5c29b3..f203a5311 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -36,1196 +36,1400 @@ using Shared::Platform::int8; using Shared::Platform::uint8; using Shared::Platform::int16; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class GameSettings; + class GameSettings; -enum NetworkMessageType { - nmtInvalid, - nmtIntro, - nmtPing, - nmtReady, - nmtLaunch, - nmtCommandList, - nmtText, - nmtQuit, - nmtSynchNetworkGameData, - nmtSynchNetworkGameDataStatus, - nmtSynchNetworkGameDataFileCRCCheck, - nmtSynchNetworkGameDataFileGet, - nmtBroadCastSetup, - nmtSwitchSetupRequest, - nmtPlayerIndexMessage, - nmtLoadingStatusMessage, - nmtMarkCell, - nmtUnMarkCell, - nmtHighlightCell, -// nmtCompressedPacket, + enum NetworkMessageType { + nmtInvalid, + nmtIntro, + nmtPing, + nmtReady, + nmtLaunch, + nmtCommandList, + nmtText, + nmtQuit, + nmtSynchNetworkGameData, + nmtSynchNetworkGameDataStatus, + nmtSynchNetworkGameDataFileCRCCheck, + nmtSynchNetworkGameDataFileGet, + nmtBroadCastSetup, + nmtSwitchSetupRequest, + nmtPlayerIndexMessage, + nmtLoadingStatusMessage, + nmtMarkCell, + nmtUnMarkCell, + nmtHighlightCell, + // nmtCompressedPacket, - nmtCount -}; + nmtCount + }; -enum NetworkGameStateType { - nmgstInvalid, - nmgstOk, - nmgstNoSlots, + enum NetworkGameStateType { + nmgstInvalid, + nmgstOk, + nmgstNoSlots, - nmgstCount -}; + nmgstCount + }; -static const int maxLanguageStringSize= 60; -static const int maxNetworkMessageSize= 20000; + static const int maxLanguageStringSize = 60; + static const int maxNetworkMessageSize = 20000; -// ===================================================== -// class NetworkMessage -// ===================================================== + // ===================================================== + // class NetworkMessage + // ===================================================== -enum NetworkMessageStatisticType { - netmsgstPacketsPerMillisecondSend, - netmsgstPacketsPerMillisecondSend_current_count, - netmsgstPacketsPerMillisecondSend_last, + enum NetworkMessageStatisticType { + netmsgstPacketsPerMillisecondSend, + netmsgstPacketsPerMillisecondSend_current_count, + netmsgstPacketsPerMillisecondSend_last, - netmsgstPacketsPerSecondSend, - netmsgstPacketsPerSecondSend_current_count, - netmsgstPacketsPerSecondSend_last, + netmsgstPacketsPerSecondSend, + netmsgstPacketsPerSecondSend_current_count, + netmsgstPacketsPerSecondSend_last, - netmsgstAverageSendSize, + netmsgstAverageSendSize, - // --------------------------------------------- - netmsgstPacketsPerMillisecondRecv, - netmsgstPacketsPerMillisecondRecv_current_count, - netmsgstPacketsPerMillisecondRecv_last, + // --------------------------------------------- + netmsgstPacketsPerMillisecondRecv, + netmsgstPacketsPerMillisecondRecv_current_count, + netmsgstPacketsPerMillisecondRecv_last, - netmsgstPacketsPerSecondRecv, - netmsgstPacketsPerSecondRecv_current_count, - netmsgstPacketsPerSecondRecv_last, + netmsgstPacketsPerSecondRecv, + netmsgstPacketsPerSecondRecv_current_count, + netmsgstPacketsPerSecondRecv_last, - netmsgstAverageRecvSize, + netmsgstAverageRecvSize, - netmsgstLastEvent + netmsgstLastEvent -}; + }; -class NetworkMessage { -private: + class NetworkMessage { + private: - static auto_ptr mutexMessageStats; - static Chrono statsTimer; - static Chrono lastSend; - static Chrono lastRecv; - static std::map mapMessageStats; + static auto_ptr mutexMessageStats; + static Chrono statsTimer; + static Chrono lastSend; + static Chrono lastRecv; + static std::map mapMessageStats; -public: - static void resetNetworkPacketStats(); - static string getNetworkPacketStats(); + public: + static void resetNetworkPacketStats(); + static string getNetworkPacketStats(); - static bool useOldProtocol; - virtual ~NetworkMessage(){} - virtual bool receive(Socket* socket)= 0; - virtual bool receive(Socket* socket, NetworkMessageType type) { return receive(socket); }; + static bool useOldProtocol; + virtual ~NetworkMessage() { + } + virtual bool receive(Socket* socket) = 0; + virtual bool receive(Socket* socket, NetworkMessageType type) { + return receive(socket); + }; - virtual void send(Socket* socket) = 0; - virtual size_t getDataSize() const = 0; - virtual unsigned char * getData() { return NULL; } + virtual void send(Socket* socket) = 0; + virtual size_t getDataSize() const = 0; + virtual unsigned char * getData() { + return NULL; + } - virtual NetworkMessageType getNetworkMessageType() const = 0; + virtual NetworkMessageType getNetworkMessageType() const = 0; - void dump_packet(string label, const void* data, int dataSize, bool isSend); + void dump_packet(string label, const void* data, int dataSize, bool isSend); -protected: - //bool peek(Socket* socket, void* data, int dataSize); - bool receive(Socket* socket, void* data, int dataSize,bool tryReceiveUntilDataSizeMet); - void send(Socket* socket, const void* data, int dataSize); - void send(Socket* socket, const void* data, int dataSize, int8 messageType); - void send(Socket* socket, const void* data, int dataSize, int8 messageType, uint32 compressedLength); + protected: + //bool peek(Socket* socket, void* data, int dataSize); + bool receive(Socket* socket, void* data, int dataSize, bool tryReceiveUntilDataSizeMet); + void send(Socket* socket, const void* data, int dataSize); + void send(Socket* socket, const void* data, int dataSize, int8 messageType); + void send(Socket* socket, const void* data, int dataSize, int8 messageType, uint32 compressedLength); - virtual const char * getPackedMessageFormat() const = 0; - virtual unsigned int getPackedSize() = 0; - virtual void unpackMessage(unsigned char *buf) = 0; - virtual unsigned char * packMessage() = 0; -}; + virtual const char * getPackedMessageFormat() const = 0; + virtual unsigned int getPackedSize() = 0; + virtual void unpackMessage(unsigned char *buf) = 0; + virtual unsigned char * packMessage() = 0; + }; -// ===================================================== -// class NetworkMessageIntro -// -// Message sent from the server to the client -// when the client connects and vice versa -// ===================================================== + // ===================================================== + // class NetworkMessageIntro + // + // Message sent from the server to the client + // when the client connects and vice versa + // ===================================================== #pragma pack(push, 1) -class NetworkMessageIntro: public NetworkMessage{ -private: - static const int maxVersionStringSize= 128; - static const int maxNameSize= 32; - static const int maxSmallStringSize= 60; + class NetworkMessageIntro : public NetworkMessage { + private: + static const int maxVersionStringSize = 128; + static const int maxNameSize = 32; + static const int maxSmallStringSize = 60; -private: - int8 messageType; - struct Data { - int32 sessionId; - NetworkString versionString; - NetworkString name; - int16 playerIndex; - int8 gameState; - uint32 externalIp; - uint32 ftpPort; - NetworkString language; - int8 gameInProgress; - NetworkString playerUUID; - NetworkString platform; - }; + private: + int8 messageType; + struct Data { + int32 sessionId; + NetworkString versionString; + NetworkString name; + int16 playerIndex; + int8 gameState; + uint32 externalIp; + uint32 ftpPort; + NetworkString language; + int8 gameInProgress; + NetworkString playerUUID; + NetworkString platform; + }; - void toEndian(); - void fromEndian(); + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: + protected: -public: - NetworkMessageIntro(); - NetworkMessageIntro(int32 sessionId, const string &versionString, - const string &name, int playerIndex, NetworkGameStateType gameState, - uint32 externalIp, uint32 ftpPort, const string &playerLanguage, - int gameInProgress, const string &playerUUID, const string &platform); + public: + NetworkMessageIntro(); + NetworkMessageIntro(int32 sessionId, const string &versionString, + const string &name, int playerIndex, NetworkGameStateType gameState, + uint32 externalIp, uint32 ftpPort, const string &playerLanguage, + int gameInProgress, const string &playerUUID, const string &platform); - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtIntro; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtIntro; + } - int32 getSessionId() const { return data.sessionId;} - string getVersionString() const { return data.versionString.getString(); } - string getName() const { return data.name.getString(); } - int getPlayerIndex() const { return data.playerIndex; } - NetworkGameStateType getGameState() const { return static_cast(data.gameState); } - uint32 getExternalIp() const { return data.externalIp;} - uint32 getFtpPort() const { return data.ftpPort; } - string getPlayerLanguage() const { return data.language.getString(); } - uint8 getGameInProgress() const { return data.gameInProgress; } + int32 getSessionId() const { + return data.sessionId; + } + string getVersionString() const { + return data.versionString.getString(); + } + string getName() const { + return data.name.getString(); + } + int getPlayerIndex() const { + return data.playerIndex; + } + NetworkGameStateType getGameState() const { + return static_cast(data.gameState); + } + uint32 getExternalIp() const { + return data.externalIp; + } + uint32 getFtpPort() const { + return data.ftpPort; + } + string getPlayerLanguage() const { + return data.language.getString(); + } + uint8 getGameInProgress() const { + return data.gameInProgress; + } - string getPlayerUUID() const { return data.playerUUID.getString();} - string getPlayerPlatform() const { return data.platform.getString();} + string getPlayerUUID() const { + return data.playerUUID.getString(); + } + string getPlayerPlatform() const { + return data.platform.getString(); + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); - string toString() const; -}; + string toString() const; + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessagePing -// -// Message sent at any time -// ===================================================== + // ===================================================== + // class NetworkMessagePing + // + // Message sent at any time + // ===================================================== #pragma pack(push, 1) -class NetworkMessagePing: public NetworkMessage{ -private: - int8 messageType; - struct Data{ - int32 pingFrequency; - int64 pingTime; - }; - void toEndian(); - void fromEndian(); + class NetworkMessagePing : public NetworkMessage { + private: + int8 messageType; + struct Data { + int32 pingFrequency; + int64 pingTime; + }; + void toEndian(); + void fromEndian(); -private: - Data data; - int64 pingReceivedLocalTime; + private: + Data data; + int64 pingReceivedLocalTime; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessagePing(); - NetworkMessagePing(int32 pingFrequency, int64 pingTime); + public: + NetworkMessagePing(); + NetworkMessagePing(int32 pingFrequency, int64 pingTime); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtPing; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtPing; + } - int32 getPingFrequency() const {return data.pingFrequency;} - int64 getPingTime() const {return data.pingTime;} - int64 getPingReceivedLocalTime() const { return pingReceivedLocalTime; } - void setPingReceivedLocalTime(int64 pingTime) { pingReceivedLocalTime = pingTime; } + int32 getPingFrequency() const { + return data.pingFrequency; + } + int64 getPingTime() const { + return data.pingTime; + } + int64 getPingReceivedLocalTime() const { + return pingReceivedLocalTime; + } + void setPingReceivedLocalTime(int64 pingTime) { + pingReceivedLocalTime = pingTime; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageReady -// -// Message sent at the beginning of the game -// ===================================================== + // ===================================================== + // class NetworkMessageReady + // + // Message sent at the beginning of the game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageReady: public NetworkMessage{ -private: - int8 messageType; - struct Data{ - uint32 checksum; - }; - void toEndian(); - void fromEndian(); + class NetworkMessageReady : public NetworkMessage { + private: + int8 messageType; + struct Data { + uint32 checksum; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageReady(); - explicit NetworkMessageReady(uint32 checksum); + public: + NetworkMessageReady(); + explicit NetworkMessageReady(uint32 checksum); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtReady; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtReady; + } - uint32 getChecksum() const {return data.checksum;} + uint32 getChecksum() const { + return data.checksum; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageLaunch -// -// Message sent from the server to the client -// to launch the game -// ===================================================== + // ===================================================== + // class NetworkMessageLaunch + // + // Message sent from the server to the client + // to launch the game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageLaunch: public NetworkMessage { -private: - static const int maxStringSize= 256; - static const int maxSmallStringSize= 60; - static const int maxFactionCRCCount= 20; + class NetworkMessageLaunch : public NetworkMessage { + private: + static const int maxStringSize = 256; + static const int maxSmallStringSize = 60; + static const int maxFactionCRCCount = 20; -private: + private: - int8 messageType; - uint32 compressedLength; - struct Data { - NetworkString description; - NetworkString map; - NetworkString tileset; - NetworkString tech; - NetworkString factionTypeNames[GameConstants::maxPlayers]; //faction names - NetworkString networkPlayerNames[GameConstants::maxPlayers]; //networkPlayerNames - NetworkString networkPlayerPlatform[GameConstants::maxPlayers]; - int32 networkPlayerStatuses[GameConstants::maxPlayers]; //networkPlayerStatuses - NetworkString networkPlayerLanguages[GameConstants::maxPlayers]; + int8 messageType; + uint32 compressedLength; + struct Data { + NetworkString description; + NetworkString map; + NetworkString tileset; + NetworkString tech; + NetworkString factionTypeNames[GameConstants::maxPlayers]; //faction names + NetworkString networkPlayerNames[GameConstants::maxPlayers]; //networkPlayerNames + NetworkString networkPlayerPlatform[GameConstants::maxPlayers]; + int32 networkPlayerStatuses[GameConstants::maxPlayers]; //networkPlayerStatuses + NetworkString networkPlayerLanguages[GameConstants::maxPlayers]; - uint32 mapCRC; - int8 mapFilter; - uint32 tilesetCRC; - uint32 techCRC; - NetworkString factionNameList[maxFactionCRCCount]; - uint32 factionCRCList[maxFactionCRCCount]; + uint32 mapCRC; + int8 mapFilter; + uint32 tilesetCRC; + uint32 techCRC; + NetworkString factionNameList[maxFactionCRCCount]; + uint32 factionCRCList[maxFactionCRCCount]; - int8 factionControls[GameConstants::maxPlayers]; - int8 resourceMultiplierIndex[GameConstants::maxPlayers]; + int8 factionControls[GameConstants::maxPlayers]; + int8 resourceMultiplierIndex[GameConstants::maxPlayers]; - int8 thisFactionIndex; - int8 factionCount; - int8 teams[GameConstants::maxPlayers]; - int8 startLocationIndex[GameConstants::maxPlayers]; + int8 thisFactionIndex; + int8 factionCount; + int8 teams[GameConstants::maxPlayers]; + int8 startLocationIndex[GameConstants::maxPlayers]; - int8 defaultResources; - int8 defaultUnits; - int8 defaultVictoryConditions; - int8 fogOfWar; - int8 allowObservers; - int8 enableObserverModeAtEndGame; - int8 enableServerControlledAI; - uint8 networkFramePeriod; // allowed values 0 - 255 - int8 networkPauseGameForLaggedClients; - int8 pathFinderType; - uint32 flagTypes1; + int8 defaultResources; + int8 defaultUnits; + int8 defaultVictoryConditions; + int8 fogOfWar; + int8 allowObservers; + int8 enableObserverModeAtEndGame; + int8 enableServerControlledAI; + uint8 networkFramePeriod; // allowed values 0 - 255 + int8 networkPauseGameForLaggedClients; + int8 pathFinderType; + uint32 flagTypes1; - int8 aiAcceptSwitchTeamPercentChance; - int8 cpuReplacementMultiplier; - int32 masterserver_admin; - int32 masterserver_admin_factionIndex; + int8 aiAcceptSwitchTeamPercentChance; + int8 cpuReplacementMultiplier; + int32 masterserver_admin; + int32 masterserver_admin_factionIndex; - NetworkString scenario; + NetworkString scenario; - NetworkString networkPlayerUUID[GameConstants::maxPlayers]; + NetworkString networkPlayerUUID[GameConstants::maxPlayers]; - int8 networkAllowNativeLanguageTechtree; - NetworkString gameUUID; - }; - void toEndian(); - void fromEndian(); - std::pair getCompressedMessage(); -private: - Data data; + int8 networkAllowNativeLanguageTechtree; + NetworkString gameUUID; + }; + void toEndian(); + void fromEndian(); + std::pair getCompressedMessage(); + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageLaunch(); - NetworkMessageLaunch(const GameSettings *gameSettings,int8 messageType); + public: + NetworkMessageLaunch(); + NetworkMessageLaunch(const GameSettings *gameSettings, int8 messageType); - virtual size_t getDataSize() const { return sizeof(Data); } - virtual unsigned char * getData(); + virtual size_t getDataSize() const { + return sizeof(Data); + } + virtual unsigned char * getData(); - virtual NetworkMessageType getNetworkMessageType() const { - return nmtLaunch; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtLaunch; + } - void buildGameSettings(GameSettings *gameSettings) const; - int getMessageType() const { return messageType; } + void buildGameSettings(GameSettings *gameSettings) const; + int getMessageType() const { + return messageType; + } - int getMapCRC() const { return data.mapCRC; } - int getTilesetCRC() const { return data.tilesetCRC; } - int getTechCRC() const { return data.techCRC; } - vector > getFactionCRCList() const; + int getMapCRC() const { + return data.mapCRC; + } + int getTilesetCRC() const { + return data.tilesetCRC; + } + int getTechCRC() const { + return data.techCRC; + } + vector > getFactionCRCList() const; - virtual bool receive(Socket* socket); - virtual bool receive(Socket* socket, NetworkMessageType type); + virtual bool receive(Socket* socket); + virtual bool receive(Socket* socket, NetworkMessageType type); - virtual void send(Socket* socket); -}; + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class CommandList -// -// Message to order a commands to several units -// ===================================================== + // ===================================================== + // class CommandList + // + // Message to order a commands to several units + // ===================================================== #pragma pack(push, 1) -class NetworkMessageCommandList: public NetworkMessage { + class NetworkMessageCommandList : public NetworkMessage { -private: + private: - struct DataHeader { + struct DataHeader { - uint16 commandCount; - int32 frameCount; - uint32 networkPlayerFactionCRC[GameConstants::maxPlayers]; - }; + uint16 commandCount; + int32 frameCount; + uint32 networkPlayerFactionCRC[GameConstants::maxPlayers]; + }; - static const int32 commandListHeaderSize = sizeof(DataHeader); + static const int32 commandListHeaderSize = sizeof(DataHeader); - struct Data { - int8 messageType; - DataHeader header; - std::vector commands; - }; - void init(Data &data_ref) { - data_ref.messageType = 0; - data_ref.header.commandCount = 0; - data_ref.header.frameCount = 0; - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - data_ref.header.networkPlayerFactionCRC[index] = 0; - } - } - void toEndianHeader(); - void fromEndianHeader(); - void toEndianDetail(uint16 totalCommand); - void fromEndianDetail(); + struct Data { + int8 messageType; + DataHeader header; + std::vector commands; + }; + void init(Data &data_ref) { + data_ref.messageType = 0; + data_ref.header.commandCount = 0; + data_ref.header.frameCount = 0; + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + data_ref.header.networkPlayerFactionCRC[index] = 0; + } + } + void toEndianHeader(); + void fromEndianHeader(); + void toEndianDetail(uint16 totalCommand); + void fromEndianDetail(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const { return NULL; } - virtual unsigned int getPackedSize() { return 0; } - virtual void unpackMessage(unsigned char *buf) { }; - virtual unsigned char * packMessage() { return NULL; } + protected: + virtual const char * getPackedMessageFormat() const { + return NULL; + } + virtual unsigned int getPackedSize() { + return 0; + } + virtual void unpackMessage(unsigned char *buf) { + }; + virtual unsigned char * packMessage() { + return NULL; + } - const char * getPackedMessageFormatHeader() const; - unsigned int getPackedSizeHeader(); - void unpackMessageHeader(unsigned char *buf); - unsigned char * packMessageHeader(); + const char * getPackedMessageFormatHeader() const; + unsigned int getPackedSizeHeader(); + void unpackMessageHeader(unsigned char *buf); + unsigned char * packMessageHeader(); - const char * getPackedMessageFormatDetail() const; - unsigned int getPackedSizeDetail(int count); - void unpackMessageDetail(unsigned char *buf,int count); - unsigned char * packMessageDetail(uint16 totalCommand); + const char * getPackedMessageFormatDetail() const; + unsigned int getPackedSizeDetail(int count); + void unpackMessageDetail(unsigned char *buf, int count); + unsigned char * packMessageDetail(uint16 totalCommand); -public: - explicit NetworkMessageCommandList(int32 frameCount= -1); + public: + explicit NetworkMessageCommandList(int32 frameCount = -1); - virtual size_t getDataSize() const { return sizeof(Data); } - virtual unsigned char * getData(); + virtual size_t getDataSize() const { + return sizeof(Data); + } + virtual unsigned char * getData(); - virtual NetworkMessageType getNetworkMessageType() const { - return nmtCommandList; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtCommandList; + } - bool addCommand(const NetworkCommand* networkCommand); + bool addCommand(const NetworkCommand* networkCommand); - void clear() {data.header.commandCount= 0;} - int getCommandCount() const {return data.header.commandCount;} - int getFrameCount() const {return data.header.frameCount;} - uint32 getNetworkPlayerFactionCRC(int index) const {return data.header.networkPlayerFactionCRC[index];} - void setNetworkPlayerFactionCRC(int index, uint32 crc) { data.header.networkPlayerFactionCRC[index]=crc;} + void clear() { + data.header.commandCount = 0; + } + int getCommandCount() const { + return data.header.commandCount; + } + int getFrameCount() const { + return data.header.frameCount; + } + uint32 getNetworkPlayerFactionCRC(int index) const { + return data.header.networkPlayerFactionCRC[index]; + } + void setNetworkPlayerFactionCRC(int index, uint32 crc) { + data.header.networkPlayerFactionCRC[index] = crc; + } - const NetworkCommand* getCommand(int i) const {return &data.commands[i];} + const NetworkCommand* getCommand(int i) const { + return &data.commands[i]; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageText -// -// Chat text message -// ===================================================== + // ===================================================== + // class NetworkMessageText + // + // Chat text message + // ===================================================== #pragma pack(push, 1) -class NetworkMessageText: public NetworkMessage { -private: - static const int maxTextStringSize= 500; + class NetworkMessageText : public NetworkMessage { + private: + static const int maxTextStringSize = 500; -private: - int8 messageType; - struct Data{ + private: + int8 messageType; + struct Data { - NetworkString text; - int8 teamIndex; - int8 playerIndex; - NetworkString targetLanguage; - }; - void toEndian(); - void fromEndian(); + NetworkString text; + int8 teamIndex; + int8 playerIndex; + NetworkString targetLanguage; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageText(); - NetworkMessageText(const string &text, int teamIndex, int playerIndex, - const string targetLanguage); + public: + NetworkMessageText(); + NetworkMessageText(const string &text, int teamIndex, int playerIndex, + const string targetLanguage); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtText; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtText; + } - string getText() const {return data.text.getString();} - int getTeamIndex() const {return data.teamIndex;} - int getPlayerIndex() const {return data.playerIndex;} - string getTargetLanguage() const {return data.targetLanguage.getString();} + string getText() const { + return data.text.getString(); + } + int getTeamIndex() const { + return data.teamIndex; + } + int getPlayerIndex() const { + return data.playerIndex; + } + string getTargetLanguage() const { + return data.targetLanguage.getString(); + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); - NetworkMessageText * getCopy() const; -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + NetworkMessageText * getCopy() const; + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageQuit -// -// Message sent at the beginning of the game -// ===================================================== + // ===================================================== + // class NetworkMessageQuit + // + // Message sent at the beginning of the game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageQuit: public NetworkMessage{ -private: + class NetworkMessageQuit : public NetworkMessage { + private: - int8 messageType; - //struct Data{ - // int8 messageType; - //}; - void toEndian(); - void fromEndian(); + int8 messageType; + //struct Data{ + // int8 messageType; + //}; + void toEndian(); + void fromEndian(); -private: - //Data data; + private: + //Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageQuit(); + public: + NetworkMessageQuit(); - //virtual size_t getDataSize() const { return sizeof(Data); } - virtual size_t getDataSize() const { return 0; } + //virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return 0; + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtQuit; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtQuit; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageSynchNetworkGameData -// -// Message sent at the beginning of a network game -// ===================================================== + // ===================================================== + // class NetworkMessageSynchNetworkGameData + // + // Message sent at the beginning of a network game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageSynchNetworkGameData: public NetworkMessage{ + class NetworkMessageSynchNetworkGameData : public NetworkMessage { -private: + private: -static const int maxStringSize= 255; -static const int maxFileCRCCount= 1500; -static const int maxFileCRCPacketCount= 25; + static const int maxStringSize = 255; + static const int maxFileCRCCount = 1500; + static const int maxFileCRCPacketCount = 25; -private: + private: - struct DataHeader { - NetworkString map; - NetworkString tileset; - NetworkString tech; + struct DataHeader { + NetworkString map; + NetworkString tileset; + NetworkString tech; - uint32 mapCRC; - uint32 tilesetCRC; - uint32 techCRC; + uint32 mapCRC; + uint32 tilesetCRC; + uint32 techCRC; - uint32 techCRCFileCount; - }; + uint32 techCRCFileCount; + }; - static const int32 HeaderSize = sizeof(DataHeader); + static const int32 HeaderSize = sizeof(DataHeader); - struct DataDetail { - NetworkString techCRCFileList[maxFileCRCCount]; - uint32 techCRCFileCRCList[maxFileCRCCount]; - }; + struct DataDetail { + NetworkString techCRCFileList[maxFileCRCCount]; + uint32 techCRCFileCRCList[maxFileCRCCount]; + }; - static const int32 DetailSize1 = sizeof(NetworkString); - static const int32 DetailSize2 = sizeof(uint32); + static const int32 DetailSize1 = sizeof(NetworkString); + static const int32 DetailSize2 = sizeof(uint32); - struct Data { - int8 messageType; - DataHeader header; - DataDetail detail; - }; - void toEndianHeader(); - void fromEndianHeader(); - void toEndianDetail(uint32 totalFileCount); - void fromEndianDetail(); + struct Data { + int8 messageType; + DataHeader header; + DataDetail detail; + }; + void toEndianHeader(); + void fromEndianHeader(); + void toEndianDetail(uint32 totalFileCount); + void fromEndianDetail(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const { return NULL; } - virtual unsigned int getPackedSize() { return 0; } - virtual void unpackMessage(unsigned char *buf) { }; - virtual unsigned char * packMessage() { return NULL; } + protected: + virtual const char * getPackedMessageFormat() const { + return NULL; + } + virtual unsigned int getPackedSize() { + return 0; + } + virtual void unpackMessage(unsigned char *buf) { + }; + virtual unsigned char * packMessage() { + return NULL; + } - const char * getPackedMessageFormatHeader() const; - unsigned int getPackedSizeHeader(); - void unpackMessageHeader(unsigned char *buf); - unsigned char * packMessageHeader(); + const char * getPackedMessageFormatHeader() const; + unsigned int getPackedSizeHeader(); + void unpackMessageHeader(unsigned char *buf); + unsigned char * packMessageHeader(); - unsigned int getPackedSizeDetail(); - void unpackMessageDetail(unsigned char *buf); - unsigned char * packMessageDetail(); + unsigned int getPackedSizeDetail(); + void unpackMessageDetail(unsigned char *buf); + unsigned char * packMessageDetail(); -public: - NetworkMessageSynchNetworkGameData() {}; - explicit NetworkMessageSynchNetworkGameData(const GameSettings *gameSettings); + public: + NetworkMessageSynchNetworkGameData() { + }; + explicit NetworkMessageSynchNetworkGameData(const GameSettings *gameSettings); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtSynchNetworkGameData; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtSynchNetworkGameData; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); - string getMap() const {return data.header.map.getString();} - string getTileset() const {return data.header.tileset.getString();} - string getTech() const {return data.header.tech.getString();} + string getMap() const { + return data.header.map.getString(); + } + string getTileset() const { + return data.header.tileset.getString(); + } + string getTech() const { + return data.header.tech.getString(); + } - uint32 getMapCRC() const {return data.header.mapCRC;} - uint32 getTilesetCRC() const {return data.header.tilesetCRC;} - uint32 getTechCRC() const {return data.header.techCRC;} + uint32 getMapCRC() const { + return data.header.mapCRC; + } + uint32 getTilesetCRC() const { + return data.header.tilesetCRC; + } + uint32 getTechCRC() const { + return data.header.techCRC; + } - uint32 getTechCRCFileCount() const {return data.header.techCRCFileCount;} - const NetworkString * getTechCRCFileList() const {return &data.detail.techCRCFileList[0];} - const uint32 * getTechCRCFileCRCList() const {return data.detail.techCRCFileCRCList;} + uint32 getTechCRCFileCount() const { + return data.header.techCRCFileCount; + } + const NetworkString * getTechCRCFileList() const { + return &data.detail.techCRCFileList[0]; + } + const uint32 * getTechCRCFileCRCList() const { + return data.detail.techCRCFileCRCList; + } - string getTechCRCFileMismatchReport(vector > &vctFileList); -}; + string getTechCRCFileMismatchReport(vector > &vctFileList); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageSynchNetworkGameDataStatus -// -// Message sent at the beggining of a network game -// ===================================================== + // ===================================================== + // class NetworkMessageSynchNetworkGameDataStatus + // + // Message sent at the beggining of a network game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageSynchNetworkGameDataStatus: public NetworkMessage{ + class NetworkMessageSynchNetworkGameDataStatus : public NetworkMessage { -private: + private: -static const int maxStringSize= 255; -static const int maxFileCRCCount= 1500; -static const uint32 maxFileCRCPacketCount= 25; + static const int maxStringSize = 255; + static const int maxFileCRCCount = 1500; + static const uint32 maxFileCRCPacketCount = 25; -private: + private: - struct DataHeader { - uint32 mapCRC; - uint32 tilesetCRC; - uint32 techCRC; + struct DataHeader { + uint32 mapCRC; + uint32 tilesetCRC; + uint32 techCRC; - uint32 techCRCFileCount; - }; - static const int32 HeaderSize = sizeof(DataHeader); + uint32 techCRCFileCount; + }; + static const int32 HeaderSize = sizeof(DataHeader); - struct DataDetail { - NetworkString techCRCFileList[maxFileCRCCount]; - uint32 techCRCFileCRCList[maxFileCRCCount]; - }; + struct DataDetail { + NetworkString techCRCFileList[maxFileCRCCount]; + uint32 techCRCFileCRCList[maxFileCRCCount]; + }; - static const int32 DetailSize1 = sizeof(NetworkString); - static const int32 DetailSize2 = sizeof(uint32); + static const int32 DetailSize1 = sizeof(NetworkString); + static const int32 DetailSize2 = sizeof(uint32); - struct Data { - int8 messageType; - DataHeader header; - DataDetail detail; - }; - void toEndianHeader(); - void fromEndianHeader(); - void toEndianDetail(uint32 totalFileCount); - void fromEndianDetail(); + struct Data { + int8 messageType; + DataHeader header; + DataDetail detail; + }; + void toEndianHeader(); + void fromEndianHeader(); + void toEndianDetail(uint32 totalFileCount); + void fromEndianDetail(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const { return NULL; } - virtual unsigned int getPackedSize() { return 0; } - virtual void unpackMessage(unsigned char *buf) { }; - virtual unsigned char * packMessage() { return NULL; } + protected: + virtual const char * getPackedMessageFormat() const { + return NULL; + } + virtual unsigned int getPackedSize() { + return 0; + } + virtual void unpackMessage(unsigned char *buf) { + }; + virtual unsigned char * packMessage() { + return NULL; + } -public: - NetworkMessageSynchNetworkGameDataStatus() {}; - NetworkMessageSynchNetworkGameDataStatus(uint32 mapCRC, uint32 tilesetCRC, uint32 techCRC, vector > &vctFileList); + public: + NetworkMessageSynchNetworkGameDataStatus() { + }; + NetworkMessageSynchNetworkGameDataStatus(uint32 mapCRC, uint32 tilesetCRC, uint32 techCRC, vector > &vctFileList); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtSynchNetworkGameDataStatus; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtSynchNetworkGameDataStatus; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); - uint32 getMapCRC() const {return data.header.mapCRC;} - uint32 getTilesetCRC() const {return data.header.tilesetCRC;} - uint32 getTechCRC() const {return data.header.techCRC;} + uint32 getMapCRC() const { + return data.header.mapCRC; + } + uint32 getTilesetCRC() const { + return data.header.tilesetCRC; + } + uint32 getTechCRC() const { + return data.header.techCRC; + } - uint32 getTechCRCFileCount() const {return data.header.techCRCFileCount;} - const NetworkString * getTechCRCFileList() const {return &data.detail.techCRCFileList[0];} - const uint32 * getTechCRCFileCRCList() const {return data.detail.techCRCFileCRCList;} + uint32 getTechCRCFileCount() const { + return data.header.techCRCFileCount; + } + const NetworkString * getTechCRCFileList() const { + return &data.detail.techCRCFileList[0]; + } + const uint32 * getTechCRCFileCRCList() const { + return data.detail.techCRCFileCRCList; + } - string getTechCRCFileMismatchReport(string techtree, vector > &vctFileList); + string getTechCRCFileMismatchReport(string techtree, vector > &vctFileList); -}; + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageSynchNetworkGameDataFileCRCCheck -// -// Message sent at the beginning of a network game -// ===================================================== + // ===================================================== + // class NetworkMessageSynchNetworkGameDataFileCRCCheck + // + // Message sent at the beginning of a network game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageSynchNetworkGameDataFileCRCCheck: public NetworkMessage{ + class NetworkMessageSynchNetworkGameDataFileCRCCheck : public NetworkMessage { -private: + private: -static const int maxStringSize= 256; + static const int maxStringSize = 256; -private: - int8 messageType; - struct Data{ + private: + int8 messageType; + struct Data { - uint32 totalFileCount; - uint32 fileIndex; - uint32 fileCRC; - NetworkString fileName; - }; - void toEndian(); - void fromEndian(); + uint32 totalFileCount; + uint32 fileIndex; + uint32 fileCRC; + NetworkString fileName; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageSynchNetworkGameDataFileCRCCheck(); - NetworkMessageSynchNetworkGameDataFileCRCCheck(uint32 totalFileCount, uint32 fileIndex, uint32 fileCRC, const string fileName); + public: + NetworkMessageSynchNetworkGameDataFileCRCCheck(); + NetworkMessageSynchNetworkGameDataFileCRCCheck(uint32 totalFileCount, uint32 fileIndex, uint32 fileCRC, const string fileName); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtSynchNetworkGameDataFileCRCCheck; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtSynchNetworkGameDataFileCRCCheck; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); - uint32 getTotalFileCount() const {return data.totalFileCount;} - uint32 getFileIndex() const {return data.fileIndex;} - uint32 getFileCRC() const {return data.fileCRC;} - string getFileName() const {return data.fileName.getString();} -}; + uint32 getTotalFileCount() const { + return data.totalFileCount; + } + uint32 getFileIndex() const { + return data.fileIndex; + } + uint32 getFileCRC() const { + return data.fileCRC; + } + string getFileName() const { + return data.fileName.getString(); + } + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageSynchNetworkGameDataFileGet -// -// Message sent at the beginning of a network game -// ===================================================== + // ===================================================== + // class NetworkMessageSynchNetworkGameDataFileGet + // + // Message sent at the beginning of a network game + // ===================================================== #pragma pack(push, 1) -class NetworkMessageSynchNetworkGameDataFileGet: public NetworkMessage{ + class NetworkMessageSynchNetworkGameDataFileGet : public NetworkMessage { -private: + private: -static const int maxStringSize= 256; + static const int maxStringSize = 256; -private: + private: - int8 messageType; - struct Data{ + int8 messageType; + struct Data { - NetworkString fileName; - }; - void toEndian(); - void fromEndian(); + NetworkString fileName; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageSynchNetworkGameDataFileGet(); - explicit NetworkMessageSynchNetworkGameDataFileGet(const string fileName); + public: + NetworkMessageSynchNetworkGameDataFileGet(); + explicit NetworkMessageSynchNetworkGameDataFileGet(const string fileName); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtSynchNetworkGameDataFileGet; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtSynchNetworkGameDataFileGet; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); - string getFileName() const {return data.fileName.getString();} -}; + string getFileName() const { + return data.fileName.getString(); + } + }; #pragma pack(pop) -// ===================================================== -// class SwitchSetupRequest -// -// Message sent from the client to the server -// to switch its settings -// ===================================================== + // ===================================================== + // class SwitchSetupRequest + // + // Message sent from the client to the server + // to switch its settings + // ===================================================== -// Each bit represents which item in the packet has a changed value -enum SwitchSetupRequestFlagType { - ssrft_None = 0x00, - ssrft_SelectedFactionName = 0x01, - ssrft_CurrentFactionIndex = 0x02, - ssrft_ToFactionIndex = 0x04, - ssrft_ToTeam = 0x08, - ssrft_NetworkPlayerName = 0x10, - ssrft_PlayerStatus = 0x20 -}; + // Each bit represents which item in the packet has a changed value + enum SwitchSetupRequestFlagType { + ssrft_None = 0x00, + ssrft_SelectedFactionName = 0x01, + ssrft_CurrentFactionIndex = 0x02, + ssrft_ToFactionIndex = 0x04, + ssrft_ToTeam = 0x08, + ssrft_NetworkPlayerName = 0x10, + ssrft_PlayerStatus = 0x20 + }; #pragma pack(push, 1) -class SwitchSetupRequest: public NetworkMessage{ -private: - static const int maxStringSize= 256; - static const int maxPlayernameStringSize= 80; + class SwitchSetupRequest : public NetworkMessage { + private: + static const int maxStringSize = 256; + static const int maxPlayernameStringSize = 80; -private: + private: - int8 messageType; - struct Data { + int8 messageType; + struct Data { - NetworkString selectedFactionName; //wanted faction name - int8 currentSlotIndex; - int8 toSlotIndex; - int8 toTeam; - NetworkString networkPlayerName; - int8 networkPlayerStatus; - int8 switchFlags; - NetworkString language; - }; - void toEndian(); - void fromEndian(); + NetworkString selectedFactionName; //wanted faction name + int8 currentSlotIndex; + int8 toSlotIndex; + int8 toTeam; + NetworkString networkPlayerName; + int8 networkPlayerStatus; + int8 switchFlags; + NetworkString language; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -public: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + public: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - SwitchSetupRequest(); - SwitchSetupRequest( string selectedFactionName, int8 currentFactionIndex, - int8 toFactionIndex,int8 toTeam,string networkPlayerName, - int8 networkPlayerStatus, int8 flags, - string language); + public: + SwitchSetupRequest(); + SwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, + int8 toFactionIndex, int8 toTeam, string networkPlayerName, + int8 networkPlayerStatus, int8 flags, + string language); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtSwitchSetupRequest; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtSwitchSetupRequest; + } - string getSelectedFactionName() const {return data.selectedFactionName.getString();} - int getCurrentSlotIndex() const {return data.currentSlotIndex;} - int getToSlotIndex() const {return data.toSlotIndex;} - int getToTeam() const {return data.toTeam;} - string getNetworkPlayerName() const {return data.networkPlayerName.getString(); } - int getSwitchFlags() const {return data.switchFlags;} - void addSwitchFlag(SwitchSetupRequestFlagType flag) { data.switchFlags |= flag;} - void clearSwitchFlag(SwitchSetupRequestFlagType flag) { data.switchFlags &= ~flag;} + string getSelectedFactionName() const { + return data.selectedFactionName.getString(); + } + int getCurrentSlotIndex() const { + return data.currentSlotIndex; + } + int getToSlotIndex() const { + return data.toSlotIndex; + } + int getToTeam() const { + return data.toTeam; + } + string getNetworkPlayerName() const { + return data.networkPlayerName.getString(); + } + int getSwitchFlags() const { + return data.switchFlags; + } + void addSwitchFlag(SwitchSetupRequestFlagType flag) { + data.switchFlags |= flag; + } + void clearSwitchFlag(SwitchSetupRequestFlagType flag) { + data.switchFlags &= ~flag; + } - int getNetworkPlayerStatus() const { return data.networkPlayerStatus; } - string getNetworkPlayerLanguage() const { return data.language.getString(); } + int getNetworkPlayerStatus() const { + return data.networkPlayerStatus; + } + string getNetworkPlayerLanguage() const { + return data.language.getString(); + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class PlayerIndexMessage -// -// Message sent from the server to the clients -// to tell them about a slot change ( caused by another client ) -// ===================================================== + // ===================================================== + // class PlayerIndexMessage + // + // Message sent from the server to the clients + // to tell them about a slot change ( caused by another client ) + // ===================================================== #pragma pack(push, 1) -class PlayerIndexMessage: public NetworkMessage{ + class PlayerIndexMessage : public NetworkMessage { -private: + private: - int8 messageType; - struct Data { + int8 messageType; + struct Data { - int16 playerIndex; - }; - void toEndian(); - void fromEndian(); + int16 playerIndex; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - explicit PlayerIndexMessage( int16 playerIndex); + public: + explicit PlayerIndexMessage(int16 playerIndex); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtPlayerIndexMessage; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtPlayerIndexMessage; + } - int16 getPlayerIndex() const {return data.playerIndex;} + int16 getPlayerIndex() const { + return data.playerIndex; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageLoadingStatus -// -// Message sent during game loading -// ===================================================== + // ===================================================== + // class NetworkMessageLoadingStatus + // + // Message sent during game loading + // ===================================================== -enum NetworkMessageLoadingStatusType { - nmls_NONE = 0x00, + enum NetworkMessageLoadingStatusType { + nmls_NONE = 0x00, - nmls_PLAYER1_CONNECTED = 0x01, - nmls_PLAYER2_CONNECTED = 0x02, - nmls_PLAYER3_CONNECTED = 0x04, - nmls_PLAYER4_CONNECTED = 0x08, - nmls_PLAYER5_CONNECTED = 0x10, - nmls_PLAYER6_CONNECTED = 0x20, - nmls_PLAYER7_CONNECTED = 0x40, - nmls_PLAYER8_CONNECTED = 0x80, - nmls_PLAYER9_CONNECTED = 0x160, - nmls_PLAYER10_CONNECTED = 0x320, + nmls_PLAYER1_CONNECTED = 0x01, + nmls_PLAYER2_CONNECTED = 0x02, + nmls_PLAYER3_CONNECTED = 0x04, + nmls_PLAYER4_CONNECTED = 0x08, + nmls_PLAYER5_CONNECTED = 0x10, + nmls_PLAYER6_CONNECTED = 0x20, + nmls_PLAYER7_CONNECTED = 0x40, + nmls_PLAYER8_CONNECTED = 0x80, + nmls_PLAYER9_CONNECTED = 0x160, + nmls_PLAYER10_CONNECTED = 0x320, - nmls_PLAYER1_READY = 0x100, - nmls_PLAYER2_READY = 0x200, - nmls_PLAYER3_READY = 0x400, - nmls_PLAYER4_READY = 0x1000, - nmls_PLAYER5_READY = 0x2000, - nmls_PLAYER6_READY = 0x4000, - nmls_PLAYER7_READY = 0x8000, - nmls_PLAYER8_READY = 0x10000, - nmls_PLAYER9_READY = 0x12000, - nmls_PLAYER10_READY = 0x14000 -}; + nmls_PLAYER1_READY = 0x100, + nmls_PLAYER2_READY = 0x200, + nmls_PLAYER3_READY = 0x400, + nmls_PLAYER4_READY = 0x1000, + nmls_PLAYER5_READY = 0x2000, + nmls_PLAYER6_READY = 0x4000, + nmls_PLAYER7_READY = 0x8000, + nmls_PLAYER8_READY = 0x10000, + nmls_PLAYER9_READY = 0x12000, + nmls_PLAYER10_READY = 0x14000 + }; #pragma pack(push, 1) -class NetworkMessageLoadingStatus : public NetworkMessage { -private: + class NetworkMessageLoadingStatus : public NetworkMessage { + private: - int8 messageType; - struct Data { + int8 messageType; + struct Data { - uint32 status; - }; - void toEndian(); - void fromEndian(); + uint32 status; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageLoadingStatus(); - explicit NetworkMessageLoadingStatus(uint32 status); + public: + NetworkMessageLoadingStatus(); + explicit NetworkMessageLoadingStatus(uint32 status); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtLoadingStatusMessage; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtLoadingStatusMessage; + } - uint32 getStatus() const {return data.status;} + uint32 getStatus() const { + return data.status; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -// ===================================================== -// class NetworkMessageMarkCell -// -// Mark a Cell message nmtMarkCell -// ===================================================== + // ===================================================== + // class NetworkMessageMarkCell + // + // Mark a Cell message nmtMarkCell + // ===================================================== #pragma pack(push, 1) -class NetworkMessageMarkCell: public NetworkMessage { -private: - static const int maxTextStringSize= 500; + class NetworkMessageMarkCell : public NetworkMessage { + private: + static const int maxTextStringSize = 500; -private: + private: - int8 messageType; - struct Data{ + int8 messageType; + struct Data { - int16 targetX; - int16 targetY; - int8 factionIndex; - int8 playerIndex; - NetworkString text; - }; - void toEndian(); - void fromEndian(); + int16 targetX; + int16 targetY; + int8 factionIndex; + int8 playerIndex; + NetworkString text; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageMarkCell(); - NetworkMessageMarkCell(Vec2i target, int factionIndex, const string &text, int playerIndex); + public: + NetworkMessageMarkCell(); + NetworkMessageMarkCell(Vec2i target, int factionIndex, const string &text, int playerIndex); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtMarkCell; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtMarkCell; + } - string getText() const { return data.text.getString(); } - Vec2i getTarget() const { return Vec2i(data.targetX,data.targetY); } - int getFactionIndex() const { return data.factionIndex; } - int getPlayerIndex() const { return data.playerIndex; } + string getText() const { + return data.text.getString(); + } + Vec2i getTarget() const { + return Vec2i(data.targetX, data.targetY); + } + int getFactionIndex() const { + return data.factionIndex; + } + int getPlayerIndex() const { + return data.playerIndex; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); - NetworkMessageMarkCell * getCopy() const; -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + NetworkMessageMarkCell * getCopy() const; + }; #pragma pack(pop) -// ===================================================== -// class NetworkUnMessageMarkCell -// -// Mark a Cell message nmtUnMarkCell -// ===================================================== + // ===================================================== + // class NetworkUnMessageMarkCell + // + // Mark a Cell message nmtUnMarkCell + // ===================================================== #pragma pack(push, 1) -class NetworkMessageUnMarkCell: public NetworkMessage { + class NetworkMessageUnMarkCell : public NetworkMessage { -private: + private: - int8 messageType; - struct Data { + int8 messageType; + struct Data { - int16 targetX; - int16 targetY; - int8 factionIndex; - }; - void toEndian(); - void fromEndian(); + int16 targetX; + int16 targetY; + int8 factionIndex; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageUnMarkCell(); - NetworkMessageUnMarkCell(Vec2i target, int factionIndex); + public: + NetworkMessageUnMarkCell(); + NetworkMessageUnMarkCell(Vec2i target, int factionIndex); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtUnMarkCell; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtUnMarkCell; + } - Vec2i getTarget() const { return Vec2i(data.targetX,data.targetY); } - int getFactionIndex() const { return data.factionIndex; } + Vec2i getTarget() const { + return Vec2i(data.targetX, data.targetY); + } + int getFactionIndex() const { + return data.factionIndex; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); - NetworkMessageUnMarkCell * getCopy() const; -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + NetworkMessageUnMarkCell * getCopy() const; + }; #pragma pack(pop) -// ===================================================== -// class NetworkHighlightCellMessage -// -// Highlight a Cell message -// ===================================================== + // ===================================================== + // class NetworkHighlightCellMessage + // + // Highlight a Cell message + // ===================================================== #pragma pack(push, 1) -class NetworkMessageHighlightCell: public NetworkMessage { -private: - static const int maxTextStringSize= 500; + class NetworkMessageHighlightCell : public NetworkMessage { + private: + static const int maxTextStringSize = 500; -private: + private: - int8 messageType; - struct Data{ + int8 messageType; + struct Data { - int16 targetX; - int16 targetY; - int8 factionIndex; - }; - void toEndian(); - void fromEndian(); + int16 targetX; + int16 targetY; + int8 factionIndex; + }; + void toEndian(); + void fromEndian(); -private: - Data data; + private: + Data data; -protected: - virtual const char * getPackedMessageFormat() const; - virtual unsigned int getPackedSize(); - virtual void unpackMessage(unsigned char *buf); - virtual unsigned char * packMessage(); + protected: + virtual const char * getPackedMessageFormat() const; + virtual unsigned int getPackedSize(); + virtual void unpackMessage(unsigned char *buf); + virtual unsigned char * packMessage(); -public: - NetworkMessageHighlightCell(); - NetworkMessageHighlightCell(Vec2i target, int factionIndex); + public: + NetworkMessageHighlightCell(); + NetworkMessageHighlightCell(Vec2i target, int factionIndex); - virtual size_t getDataSize() const { return sizeof(Data); } + virtual size_t getDataSize() const { + return sizeof(Data); + } - virtual NetworkMessageType getNetworkMessageType() const { - return nmtHighlightCell; - } + virtual NetworkMessageType getNetworkMessageType() const { + return nmtHighlightCell; + } - Vec2i getTarget() const { return Vec2i(data.targetX,data.targetY); } - int getFactionIndex() const { return data.factionIndex; } + Vec2i getTarget() const { + return Vec2i(data.targetX, data.targetY); + } + int getFactionIndex() const { + return data.factionIndex; + } - virtual bool receive(Socket* socket); - virtual void send(Socket* socket); -}; + virtual bool receive(Socket* socket); + virtual void send(Socket* socket); + }; #pragma pack(pop) -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/network/network_protocol.cpp b/source/glest_game/network/network_protocol.cpp index fdb3adfa6..574b31494 100644 --- a/source/glest_game/network/network_protocol.cpp +++ b/source/glest_game/network/network_protocol.cpp @@ -20,11 +20,12 @@ using namespace Shared::Platform; using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { #pragma pack(push, 1) -// macros for packing floats and doubles: + // macros for packing floats and doubles: #define pack754_16(f) (pack754((f), 16, 5)) #define pack754_32(f) (pack754((f), 32, 8)) #define pack754_64(f) (pack754((f), 64, 11)) @@ -35,551 +36,549 @@ namespace Glest{ namespace Game{ /* ** pack754() -- pack a floating point number into IEEE-754 format */ -unsigned long long int pack754(long double f, unsigned bits, unsigned expbits) -{ - long double fnorm; - int shift; - long long sign, exp, significand; - unsigned significandbits = bits - expbits - 1; // -1 for sign bit + unsigned long long int pack754(long double f, unsigned bits, unsigned expbits) { + long double fnorm; + int shift; + long long sign, exp, significand; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit - if (f == 0.0) return 0; // get this special case out of the way + if (f == 0.0) return 0; // get this special case out of the way - // check sign and begin normalization - if (f < 0) { sign = 1; fnorm = -f; } - else { sign = 0; fnorm = f; } - - // get the normalized form of f and track the exponent - shift = 0; - while(fnorm >= 2.0) { fnorm /= 2.0; shift++; } - while(fnorm < 1.0) { fnorm *= 2.0; shift--; } - fnorm = fnorm - 1.0; - - // calculate the binary form (non-float) of the significand data - significand = fnorm * ((1LL<>significandbits)&((1LL< 0) { result *= 2.0; shift--; } - while(shift < 0) { result /= 2.0; shift++; } - - // sign it - result *= ((i>>(bits-1))&1) ? -1.0: 1.0; - - return result; -} - -/* -** packi16() -- store a 16-bit int into a char buffer (like htons()) -*/ -void packi16(unsigned char *buf, uint16 i) -{ - *buf++ = i>>8; *buf++ = i; -} - -/* -** packi32() -- store a 32-bit int into a char buffer (like htonl()) -*/ -void packi32(unsigned char *buf, uint32 i) -{ - *buf++ = i>>24; *buf++ = i>>16; - *buf++ = i>>8; *buf++ = i; -} - -/* -** packi64() -- store a 64-bit int into a char buffer (like htonl()) -*/ -void packi64(unsigned char *buf, uint64 i) -{ - *buf++ = i>>56; *buf++ = i>>48; - *buf++ = i>>40; *buf++ = i>>32; - *buf++ = i>>24; *buf++ = i>>16; - *buf++ = i>>8; *buf++ = i; -} - -/* -** unpacki16() -- unpack a 16-bit int from a char buffer (like ntohs()) -*/ -int16 unpacki16(unsigned char *buf) -{ - uint16 i2 = ((uint16)buf[0]<<8) | buf[1]; - int16 i; - - // change unsigned numbers to signed - if (i2 <= 0x7fffu) { - i = i2; - } - else { - i = -1 - (uint16)(0xffffu - i2); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%u]\n",__FUNCTION__,buf[0],buf[1],i,i2); - } - - return i; -} - -/* -** unpacku16() -- unpack a 16-bit unsigned from a char buffer (like ntohs()) -*/ -uint16 unpacku16(unsigned char *buf) -{ - return ((uint16)buf[0]<<8) | buf[1]; -} - -/* -** unpacki32() -- unpack a 32-bit int from a char buffer (like ntohl()) -*/ -int32 unpacki32(unsigned char *buf) -{ - uint32 i2 = ((uint32)buf[0]<<24) | - ((uint32)buf[1]<<16) | - ((uint32)buf[2]<<8) | - buf[3]; - int32 i; - - // change unsigned numbers to signed - if (i2 <= 0x7fffffffu) { - i = i2; - } - else { - i = -1 - (int32)(0xffffffffu - i2); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%d] [%d] [%u]\n",__FUNCTION__,buf[0],buf[1],buf[2],buf[3],i,i2); - } - - return i; -} - -/* -** unpacku32() -- unpack a 32-bit unsigned from a char buffer (like ntohl()) -*/ -uint32 unpacku32(unsigned char *buf) -{ - return ((uint32)buf[0]<<24) | - ((uint32)buf[1]<<16) | - ((uint32)buf[2]<<8) | - buf[3]; -} - -/* -** unpacki64() -- unpack a 64-bit int from a char buffer (like ntohl()) -*/ -int64 unpacki64(unsigned char *buf) -{ - uint64 i2 = ((uint64)buf[0]<<56) | - ((uint64)buf[1]<<48) | - ((uint64)buf[2]<<40) | - ((uint64)buf[3]<<32) | - ((uint64)buf[4]<<24) | - ((uint64)buf[5]<<16) | - ((uint64)buf[6]<<8) | - buf[7]; - int64 i; - - // change unsigned numbers to signed - if (i2 <= 0x7fffffffffffffffull) { - i = i2; - } - else { - i = -1 -(int64)(0xffffffffffffffffull - i2); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%d] [%d] [%d] [%d] [%d] [" MG_I64_SPECIFIER "] [" MG_I64U_SPECIFIER "]\n",__FUNCTION__,buf[0],buf[1],buf[2],buf[3],buf[4],buf[5],buf[6],buf[7],i,i2); - } - - return i; -} - -/* -** unpacku64() -- unpack a 64-bit unsigned from a char buffer (like ntohl()) -*/ -uint64 unpacku64(unsigned char *buf) -{ - return ((uint64)buf[0]<<56) | - ((uint64)buf[1]<<48) | - ((uint64)buf[2]<<40) | - ((uint64)buf[3]<<32) | - ((uint64)buf[4]<<24) | - ((uint64)buf[5]<<16) | - ((uint64)buf[6]<<8) | - buf[7]; -} - -/* -** pack() -- store data dictated by the format string in the buffer -** -** bits |signed unsigned float string -** -----+---------------------------------- -** 8 | c C -** 16 | h H f -** 32 | l L d -** 64 | q Q g -** - | s -** -** (16-bit unsigned length is automatically prepended to strings) -*/ - -unsigned int pack(unsigned char *buf, const char *format, ...) { - va_list ap; - - int8 c; // 8-bit - uint8 C; - - int16 h; // 16-bit - uint16 H; - - int32 l; // 32-bit - uint32 L; - - int64 q; // 64-bit - uint64 Q; - - float f; // floats - double d; - long double g; - unsigned long long int fhold; - - char *s; // strings - uint16 len; - - unsigned int size = 0; - - uint16 maxstrlen=0; - - unsigned char *bufStart = buf; - - va_start(ap, format); - - for(; *format != '\0'; format++) { - switch(*format) { - case 'c': // 8-bit - size += 1; - c = (int8)va_arg(ap, int); // promoted - *buf++ = (unsigned char)c; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int8 = %d [%X] c = %d [%X] buf pos = %lu\n",*(buf-1),*(buf-1),c,c,(unsigned long)(buf - bufStart)); - break; - - case 'C': // 8-bit unsigned - size += 1; - C = (uint8)va_arg(ap, unsigned int); // promoted - *buf++ = (unsigned char)C; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint8 = %u [%X] C = %u [%X] buf pos = %lu\n",*(buf-1),*(buf-1),C,C,(unsigned long)(buf - bufStart)); - break; - - case 'h': // 16-bit - size += 2; - h = (int16)va_arg(ap, int); - packi16(buf, h); - buf += 2; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int16 = %d [%X] h = %d [%X] buf pos = %lu\n",*(buf-2),*(buf-2),h,h,(unsigned long)(buf - bufStart)); - break; - - case 'H': // 16-bit unsigned - size += 2; - H = (uint16)va_arg(ap, unsigned int); - packi16(buf, H); - buf += 2; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint16 = %u [%X] H = %u [%X] buf pos = %lu\n",*(buf-2),*(buf-2),H,H,(unsigned long)(buf - bufStart)); - break; - - case 'l': // 32-bit - size += 4; - l = va_arg(ap, int32); - packi32(buf, l); - buf += 4; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int32 = %d [%X] l = %d [%X] buf pos = %lu\n",*(buf-4),*(buf-4),l,l,(unsigned long)(buf - bufStart)); - break; - - case 'L': // 32-bit unsigned - size += 4; - L = va_arg(ap, uint32); - packi32(buf, L); - buf += 4; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint32 = %u [%X] L = %u [%X] buf pos = %lu\n",*(buf-4),*(buf-4),L,L,(unsigned long)(buf - bufStart)); - break; - - case 'q': // 64-bit - size += 8; - q = va_arg(ap, int64); - packi64(buf, q); - buf += 8; - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " [%lX] buf pos = %lu\n",(int64)*(buf-8),*(buf-8),q,q,(unsigned long)(buf - bufStart)); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " buf pos = %lu\n",(int64)*(buf-8),*(buf-8),q,(unsigned long)(buf - bufStart)); - break; - - case 'Q': // 64-bit unsigned - size += 8; - Q = va_arg(ap, uint64); - packi64(buf, Q); - buf += 8; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint64 = " MG_I64U_SPECIFIER " [%X] Q = " MG_I64U_SPECIFIER " buf pos = %lu\n",(uint64)*(buf-8),*(buf-8),Q,(unsigned long)(buf - bufStart)); - break; - - case 'f': // float-16 - size += 2; - f = (float)va_arg(ap, double); // promoted - fhold = pack754_16(f); // convert to IEEE 754 - packi16(buf, fhold); - buf += 2; - break; - - case 'd': // float-32 - size += 4; - d = va_arg(ap, double); - fhold = pack754_32(d); // convert to IEEE 754 - packi32(buf, fhold); - buf += 4; - break; - - case 'g': // float-64 - size += 8; - g = va_arg(ap, long double); - fhold = pack754_64(g); // convert to IEEE 754 - packi64(buf, fhold); - buf += 8; - break; - - case 's': // string - s = va_arg(ap, char*); - len = (uint16)strlen(s); - if (maxstrlen > 0 && len < maxstrlen) - len = maxstrlen - 1; - - size += len + 2; - packi16(buf, len); - buf += 2; - - memcpy(buf, s, len); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack string size = %d [%X] len = %d str [%s] buf pos = %lu\n",*(buf-2),*(buf-2),len,s,(unsigned long)(buf - bufStart)); - - buf += len; - break; - - default: - if (isdigit(*format)) { // track max str len - maxstrlen = maxstrlen * 10 + (*format-'0'); + // check sign and begin normalization + if (f < 0) { + sign = 1; fnorm = -f; + } else { + sign = 0; fnorm = f; } - break; + + // get the normalized form of f and track the exponent + shift = 0; + while (fnorm >= 2.0) { + fnorm /= 2.0; shift++; + } + while (fnorm < 1.0) { + fnorm *= 2.0; shift--; + } + fnorm = fnorm - 1.0; + + // calculate the binary form (non-float) of the significand data + significand = fnorm * ((1LL << significandbits) + 0.5f); + + // get the biased exponent + exp = (long long) shift + ((1 << (expbits - 1)) - 1); // shift + bias + + // return the final answer + return (sign << (bits - 1)) | (exp << (bits - expbits - 1)) | significand; } - if (!isdigit(*format)) maxstrlen = 0; + /* + ** unpack754() -- unpack a floating point number from IEEE-754 format + */ + long double unpack754(unsigned long long int i, unsigned bits, unsigned expbits) { + long double result; + long long shift; + unsigned bias; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit - } + if (i == 0) return 0.0; - va_end(ap); + // pull the significand + result = (i&((1LL << significandbits) - 1)); // mask + result /= (1LL << significandbits); // convert back to float + result += 1.0f; // add the one back on - return size; -} - -/* -** unpack() -- unpack data dictated by the format string into the buffer -** -** bits |signed unsigned float string -** -----+---------------------------------- -** 8 | c C -** 16 | h H f -** 32 | l L d -** 64 | q Q g -** - | s -** -** (string is extracted based on its stored length, but 's' can be -** prepended with a max length) -*/ -unsigned int unpack(unsigned char *buf, const char *format, ...) { - va_list ap; - - int8 *c; // 8-bit - uint8 *C; - - int16 *h; // 16-bit - uint16 *H; - - int32 *l; // 32-bit - uint32 *L; - - int64 *q; // 64-bit - uint64 *Q; - - unsigned long long int fhold; - - char *s; - uint16 len, maxstrlen=0, count; - - unsigned int size = 0; - - unsigned char *bufStart = buf; - - va_start(ap, format); - - for(; *format != '\0'; format++) { - switch(*format) { - case 'c': // 8-bit - c = va_arg(ap, int8*); -// if (*buf <= 0x7f) { -// *c = *buf++; -// size += 1; -// } // re-sign -// else { -// *c = -1 - (unsigned char)(0xffu - *buf); -// } - *c = (int8)*buf++; - size += 1; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int8 = %d [%X] c = %d [%X] buf pos = %lu\n",*(buf-1),*(buf-1),*c,*c,(unsigned long)(buf - bufStart)); - break; - - case 'C': // 8-bit unsigned - C = va_arg(ap, uint8*); - *C = (uint8)*buf++; - size += 1; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint8 = %u [%X] C = %u [%X] buf pos = %lu\n",*(buf-1),*(buf-1),*C,*C,(unsigned long)(buf - bufStart)); - break; - - case 'h': // 16-bit - h = va_arg(ap, int16*); - *h = unpacki16(buf); - buf += 2; - size += 2; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int16 = %d [%X] h = %d [%X] buf pos = %lu\n",*(buf-2),*(buf-2),*h,*h,(unsigned long)(buf - bufStart)); - break; - - case 'H': // 16-bit unsigned - H = va_arg(ap, uint16*); - *H = unpacku16(buf); - buf += 2; - size += 2; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint16 = %u [%X] H = %u [%X] buf pos = %lu\n",*(buf-2),*(buf-2),*H,*H,(unsigned long)(buf - bufStart)); - break; - - case 'l': // 32-bit - l = va_arg(ap, int32*); - *l = unpacki32(buf); - buf += 4; - size += 4; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int32 = %d [%X] l = %d [%X] buf pos = %lu\n",*(buf-4),*(buf-4),*l,*l,(unsigned long)(buf - bufStart)); - break; - - case 'L': // 32-bit unsigned - L = va_arg(ap, uint32*); - *L = unpacku32(buf); - buf += 4; - size += 4; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint32 = %u [%X] L = %u [%X] buf pos = %lu\n",*(buf-4),*(buf-4),*L,*L,(unsigned long)(buf - bufStart)); - break; - - case 'q': // 64-bit - q = va_arg(ap, int64*); - *q = unpacki64(buf); - buf += 8; - size += 8; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " buf pos = %lu\n",(int64)*(buf-8),*(buf-8),*q,(unsigned long)(buf - bufStart)); - break; - - case 'Q': // 64-bit unsigned - Q = va_arg(ap, uint64*); - *Q = unpacku64(buf); - buf += 8; - size += 8; - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint64 = " MG_I64U_SPECIFIER " [%X] Q = " MG_I64U_SPECIFIER " buf pos = %lu\n",(uint64)*(buf-8),*(buf-8),*Q,(unsigned long)(buf - bufStart)); - break; - - case 'f': // float - { - float *f = va_arg(ap, float*); - fhold = unpacku16(buf); - *f = unpack754_16(fhold); - buf += 2; - size += 2; + // deal with the exponent + bias = (1 << (expbits - 1)) - 1; + shift = ((i >> significandbits)&((1LL << expbits) - 1)) - bias; + while (shift > 0) { + result *= 2.0; shift--; } - break; - - case 'd': // float-32 - { - double *d = va_arg(ap, double*); - fhold = unpacku32(buf); - *d = unpack754_32(fhold); - buf += 4; - size += 4; + while (shift < 0) { + result /= 2.0; shift++; } - break; - case 'g': // float-64 - { - long double *g = va_arg(ap, long double*); - fhold = unpacku64(buf); - *g = unpack754_64(fhold); - buf += 8; - size += 8; - } - break; + // sign it + result *= ((i >> (bits - 1)) & 1) ? -1.0 : 1.0; - case 's': // string - s = va_arg(ap, char*); - len = unpacku16(buf); - buf += 2; - if (maxstrlen > 0 && len > maxstrlen) - count = maxstrlen - 1; - else - count = len; - - memcpy(s, buf, count); - s[count] = '\0'; - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack string size = %d [%X] count = %d len = %d str [%s] buf pos = %lu\n",*(buf-2),*(buf-2),count,len,s,(unsigned long)(buf - bufStart)); - - buf += len; - size += len; - break; - - default: - if (isdigit(*format)) { // track max str len - maxstrlen = maxstrlen * 10 + (*format-'0'); - } - break; + return result; } - if (!isdigit(*format)) maxstrlen = 0; - } + /* + ** packi16() -- store a 16-bit int into a char buffer (like htons()) + */ + void packi16(unsigned char *buf, uint16 i) { + *buf++ = i >> 8; *buf++ = i; + } - va_end(ap); + /* + ** packi32() -- store a 32-bit int into a char buffer (like htonl()) + */ + void packi32(unsigned char *buf, uint32 i) { + *buf++ = i >> 24; *buf++ = i >> 16; + *buf++ = i >> 8; *buf++ = i; + } - return size; -} + /* + ** packi64() -- store a 64-bit int into a char buffer (like htonl()) + */ + void packi64(unsigned char *buf, uint64 i) { + *buf++ = i >> 56; *buf++ = i >> 48; + *buf++ = i >> 40; *buf++ = i >> 32; + *buf++ = i >> 24; *buf++ = i >> 16; + *buf++ = i >> 8; *buf++ = i; + } + + /* + ** unpacki16() -- unpack a 16-bit int from a char buffer (like ntohs()) + */ + int16 unpacki16(unsigned char *buf) { + uint16 i2 = ((uint16) buf[0] << 8) | buf[1]; + int16 i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffu) { + i = i2; + } else { + i = -1 - (uint16) (0xffffu - i2); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%u]\n", __FUNCTION__, buf[0], buf[1], i, i2); + } + + return i; + } + + /* + ** unpacku16() -- unpack a 16-bit unsigned from a char buffer (like ntohs()) + */ + uint16 unpacku16(unsigned char *buf) { + return ((uint16) buf[0] << 8) | buf[1]; + } + + /* + ** unpacki32() -- unpack a 32-bit int from a char buffer (like ntohl()) + */ + int32 unpacki32(unsigned char *buf) { + uint32 i2 = ((uint32) buf[0] << 24) | + ((uint32) buf[1] << 16) | + ((uint32) buf[2] << 8) | + buf[3]; + int32 i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffffffu) { + i = i2; + } else { + i = -1 - (int32) (0xffffffffu - i2); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%d] [%d] [%u]\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3], i, i2); + } + + return i; + } + + /* + ** unpacku32() -- unpack a 32-bit unsigned from a char buffer (like ntohl()) + */ + uint32 unpacku32(unsigned char *buf) { + return ((uint32) buf[0] << 24) | + ((uint32) buf[1] << 16) | + ((uint32) buf[2] << 8) | + buf[3]; + } + + /* + ** unpacki64() -- unpack a 64-bit int from a char buffer (like ntohl()) + */ + int64 unpacki64(unsigned char *buf) { + uint64 i2 = ((uint64) buf[0] << 56) | + ((uint64) buf[1] << 48) | + ((uint64) buf[2] << 40) | + ((uint64) buf[3] << 32) | + ((uint64) buf[4] << 24) | + ((uint64) buf[5] << 16) | + ((uint64) buf[6] << 8) | + buf[7]; + int64 i; + + // change unsigned numbers to signed + if (i2 <= 0x7fffffffffffffffull) { + i = i2; + } else { + i = -1 - (int64) (0xffffffffffffffffull - i2); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("IN [%s] [%d] [%d] [%d] [%d] [%d] [%d] [%d] [%d] [" MG_I64_SPECIFIER "] [" MG_I64U_SPECIFIER "]\n", __FUNCTION__, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], i, i2); + } + + return i; + } + + /* + ** unpacku64() -- unpack a 64-bit unsigned from a char buffer (like ntohl()) + */ + uint64 unpacku64(unsigned char *buf) { + return ((uint64) buf[0] << 56) | + ((uint64) buf[1] << 48) | + ((uint64) buf[2] << 40) | + ((uint64) buf[3] << 32) | + ((uint64) buf[4] << 24) | + ((uint64) buf[5] << 16) | + ((uint64) buf[6] << 8) | + buf[7]; + } + + /* + ** pack() -- store data dictated by the format string in the buffer + ** + ** bits |signed unsigned float string + ** -----+---------------------------------- + ** 8 | c C + ** 16 | h H f + ** 32 | l L d + ** 64 | q Q g + ** - | s + ** + ** (16-bit unsigned length is automatically prepended to strings) + */ + + unsigned int pack(unsigned char *buf, const char *format, ...) { + va_list ap; + + int8 c; // 8-bit + uint8 C; + + int16 h; // 16-bit + uint16 H; + + int32 l; // 32-bit + uint32 L; + + int64 q; // 64-bit + uint64 Q; + + float f; // floats + double d; + long double g; + unsigned long long int fhold; + + char *s; // strings + uint16 len; + + unsigned int size = 0; + + uint16 maxstrlen = 0; + + unsigned char *bufStart = buf; + + va_start(ap, format); + + for (; *format != '\0'; format++) { + switch (*format) { + case 'c': // 8-bit + size += 1; + c = (int8) va_arg(ap, int); // promoted + *buf++ = (unsigned char) c; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int8 = %d [%X] c = %d [%X] buf pos = %lu\n", *(buf - 1), *(buf - 1), c, c, (unsigned long) (buf - bufStart)); + break; + + case 'C': // 8-bit unsigned + size += 1; + C = (uint8) va_arg(ap, unsigned int); // promoted + *buf++ = (unsigned char) C; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint8 = %u [%X] C = %u [%X] buf pos = %lu\n", *(buf - 1), *(buf - 1), C, C, (unsigned long) (buf - bufStart)); + break; + + case 'h': // 16-bit + size += 2; + h = (int16) va_arg(ap, int); + packi16(buf, h); + buf += 2; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int16 = %d [%X] h = %d [%X] buf pos = %lu\n", *(buf - 2), *(buf - 2), h, h, (unsigned long) (buf - bufStart)); + break; + + case 'H': // 16-bit unsigned + size += 2; + H = (uint16) va_arg(ap, unsigned int); + packi16(buf, H); + buf += 2; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint16 = %u [%X] H = %u [%X] buf pos = %lu\n", *(buf - 2), *(buf - 2), H, H, (unsigned long) (buf - bufStart)); + break; + + case 'l': // 32-bit + size += 4; + l = va_arg(ap, int32); + packi32(buf, l); + buf += 4; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int32 = %d [%X] l = %d [%X] buf pos = %lu\n", *(buf - 4), *(buf - 4), l, l, (unsigned long) (buf - bufStart)); + break; + + case 'L': // 32-bit unsigned + size += 4; + L = va_arg(ap, uint32); + packi32(buf, L); + buf += 4; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint32 = %u [%X] L = %u [%X] buf pos = %lu\n", *(buf - 4), *(buf - 4), L, L, (unsigned long) (buf - bufStart)); + break; + + case 'q': // 64-bit + size += 8; + q = va_arg(ap, int64); + packi64(buf, q); + buf += 8; + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " [%lX] buf pos = %lu\n",(int64)*(buf-8),*(buf-8),q,q,(unsigned long)(buf - bufStart)); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " buf pos = %lu\n", (int64)*(buf - 8), *(buf - 8), q, (unsigned long) (buf - bufStart)); + break; + + case 'Q': // 64-bit unsigned + size += 8; + Q = va_arg(ap, uint64); + packi64(buf, Q); + buf += 8; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack uint64 = " MG_I64U_SPECIFIER " [%X] Q = " MG_I64U_SPECIFIER " buf pos = %lu\n", (uint64)*(buf - 8), *(buf - 8), Q, (unsigned long) (buf - bufStart)); + break; + + case 'f': // float-16 + size += 2; + f = (float) va_arg(ap, double); // promoted + fhold = pack754_16(f); // convert to IEEE 754 + packi16(buf, fhold); + buf += 2; + break; + + case 'd': // float-32 + size += 4; + d = va_arg(ap, double); + fhold = pack754_32(d); // convert to IEEE 754 + packi32(buf, fhold); + buf += 4; + break; + + case 'g': // float-64 + size += 8; + g = va_arg(ap, long double); + fhold = pack754_64(g); // convert to IEEE 754 + packi64(buf, fhold); + buf += 8; + break; + + case 's': // string + s = va_arg(ap, char*); + len = (uint16) strlen(s); + if (maxstrlen > 0 && len < maxstrlen) + len = maxstrlen - 1; + + size += len + 2; + packi16(buf, len); + buf += 2; + + memcpy(buf, s, len); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("pack string size = %d [%X] len = %d str [%s] buf pos = %lu\n", *(buf - 2), *(buf - 2), len, s, (unsigned long) (buf - bufStart)); + + buf += len; + break; + + default: + if (isdigit(*format)) { // track max str len + maxstrlen = maxstrlen * 10 + (*format - '0'); + } + break; + } + + if (!isdigit(*format)) maxstrlen = 0; + + } + + va_end(ap); + + return size; + } + + /* + ** unpack() -- unpack data dictated by the format string into the buffer + ** + ** bits |signed unsigned float string + ** -----+---------------------------------- + ** 8 | c C + ** 16 | h H f + ** 32 | l L d + ** 64 | q Q g + ** - | s + ** + ** (string is extracted based on its stored length, but 's' can be + ** prepended with a max length) + */ + unsigned int unpack(unsigned char *buf, const char *format, ...) { + va_list ap; + + int8 *c; // 8-bit + uint8 *C; + + int16 *h; // 16-bit + uint16 *H; + + int32 *l; // 32-bit + uint32 *L; + + int64 *q; // 64-bit + uint64 *Q; + + unsigned long long int fhold; + + char *s; + uint16 len, maxstrlen = 0, count; + + unsigned int size = 0; + + unsigned char *bufStart = buf; + + va_start(ap, format); + + for (; *format != '\0'; format++) { + switch (*format) { + case 'c': // 8-bit + c = va_arg(ap, int8*); + // if (*buf <= 0x7f) { + // *c = *buf++; + // size += 1; + // } // re-sign + // else { + // *c = -1 - (unsigned char)(0xffu - *buf); + // } + *c = (int8) *buf++; + size += 1; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int8 = %d [%X] c = %d [%X] buf pos = %lu\n", *(buf - 1), *(buf - 1), *c, *c, (unsigned long) (buf - bufStart)); + break; + + case 'C': // 8-bit unsigned + C = va_arg(ap, uint8*); + *C = (uint8) *buf++; + size += 1; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint8 = %u [%X] C = %u [%X] buf pos = %lu\n", *(buf - 1), *(buf - 1), *C, *C, (unsigned long) (buf - bufStart)); + break; + + case 'h': // 16-bit + h = va_arg(ap, int16*); + *h = unpacki16(buf); + buf += 2; + size += 2; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int16 = %d [%X] h = %d [%X] buf pos = %lu\n", *(buf - 2), *(buf - 2), *h, *h, (unsigned long) (buf - bufStart)); + break; + + case 'H': // 16-bit unsigned + H = va_arg(ap, uint16*); + *H = unpacku16(buf); + buf += 2; + size += 2; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint16 = %u [%X] H = %u [%X] buf pos = %lu\n", *(buf - 2), *(buf - 2), *H, *H, (unsigned long) (buf - bufStart)); + break; + + case 'l': // 32-bit + l = va_arg(ap, int32*); + *l = unpacki32(buf); + buf += 4; + size += 4; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int32 = %d [%X] l = %d [%X] buf pos = %lu\n", *(buf - 4), *(buf - 4), *l, *l, (unsigned long) (buf - bufStart)); + break; + + case 'L': // 32-bit unsigned + L = va_arg(ap, uint32*); + *L = unpacku32(buf); + buf += 4; + size += 4; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint32 = %u [%X] L = %u [%X] buf pos = %lu\n", *(buf - 4), *(buf - 4), *L, *L, (unsigned long) (buf - bufStart)); + break; + + case 'q': // 64-bit + q = va_arg(ap, int64*); + *q = unpacki64(buf); + buf += 8; + size += 8; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack int64 = " MG_I64_SPECIFIER " [%X] q = " MG_I64_SPECIFIER " buf pos = %lu\n", (int64)*(buf - 8), *(buf - 8), *q, (unsigned long) (buf - bufStart)); + break; + + case 'Q': // 64-bit unsigned + Q = va_arg(ap, uint64*); + *Q = unpacku64(buf); + buf += 8; + size += 8; + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack uint64 = " MG_I64U_SPECIFIER " [%X] Q = " MG_I64U_SPECIFIER " buf pos = %lu\n", (uint64)*(buf - 8), *(buf - 8), *Q, (unsigned long) (buf - bufStart)); + break; + + case 'f': // float + { + float *f = va_arg(ap, float*); + fhold = unpacku16(buf); + *f = unpack754_16(fhold); + buf += 2; + size += 2; + } + break; + + case 'd': // float-32 + { + double *d = va_arg(ap, double*); + fhold = unpacku32(buf); + *d = unpack754_32(fhold); + buf += 4; + size += 4; + } + break; + + case 'g': // float-64 + { + long double *g = va_arg(ap, long double*); + fhold = unpacku64(buf); + *g = unpack754_64(fhold); + buf += 8; + size += 8; + } + break; + + case 's': // string + s = va_arg(ap, char*); + len = unpacku16(buf); + buf += 2; + if (maxstrlen > 0 && len > maxstrlen) + count = maxstrlen - 1; + else + count = len; + + memcpy(s, buf, count); + s[count] = '\0'; + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("unpack string size = %d [%X] count = %d len = %d str [%s] buf pos = %lu\n", *(buf - 2), *(buf - 2), count, len, s, (unsigned long) (buf - bufStart)); + + buf += len; + size += len; + break; + + default: + if (isdigit(*format)) { // track max str len + maxstrlen = maxstrlen * 10 + (*format - '0'); + } + break; + } + + if (!isdigit(*format)) maxstrlen = 0; + } + + va_end(ap); + + return size; + } #pragma pack(pop) -}} + } +} diff --git a/source/glest_game/network/network_protocol.h b/source/glest_game/network/network_protocol.h index eb23fb1bd..706e9ee98 100644 --- a/source/glest_game/network/network_protocol.h +++ b/source/glest_game/network/network_protocol.h @@ -12,11 +12,13 @@ #ifndef NETWORK_PROTOCOL_H_ #define NETWORK_PROTOCOL_H_ -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -unsigned int pack(unsigned char *buf, const char *format, ...); -unsigned int unpack(unsigned char *buf, const char *format, ...); + unsigned int pack(unsigned char *buf, const char *format, ...); + unsigned int unpack(unsigned char *buf, const char *format, ...); -}}; + } +}; #endif /* NETWORK_PROTOCOL_H_ */ diff --git a/source/glest_game/network/network_types.cpp b/source/glest_game/network/network_types.cpp index 62b73425a..4f0791335 100644 --- a/source/glest_game/network/network_types.cpp +++ b/source/glest_game/network/network_types.cpp @@ -20,186 +20,187 @@ using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class NetworkCommand -// ===================================================== + // ===================================================== + // class NetworkCommand + // ===================================================== -NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, - int commandTypeId, const Vec2i &pos, int unitTypeId, - int targetId, int facing, bool wantQueue, - CommandStateType commandStateType, - int commandStateValue, int unitCommandGroupId) - : networkCommandType(networkCommandType) - , unitId(unitId) - , unitTypeId(unitTypeId) - , commandTypeId(commandTypeId) - , positionX(pos.x) - , positionY(pos.y) - , wantQueue(wantQueue) - , commandStateType(commandStateType) - , commandStateValue(commandStateValue) - , unitCommandGroupId(unitCommandGroupId) - , unitFactionUnitCount(0) - , unitFactionIndex(0) { + NetworkCommand::NetworkCommand(World *world, int networkCommandType, int unitId, + int commandTypeId, const Vec2i &pos, int unitTypeId, + int targetId, int facing, bool wantQueue, + CommandStateType commandStateType, + int commandStateValue, int unitCommandGroupId) + : networkCommandType(networkCommandType) + , unitId(unitId) + , unitTypeId(unitTypeId) + , commandTypeId(commandTypeId) + , positionX(pos.x) + , positionY(pos.y) + , wantQueue(wantQueue) + , commandStateType(commandStateType) + , commandStateValue(commandStateValue) + , unitCommandGroupId(unitCommandGroupId) + , unitFactionUnitCount(0) + , unitFactionIndex(0) { - assert(targetId == -1 || facing == -1); - this->targetId = targetId >= 0 ? targetId : facing; - this->fromFactionIndex = world->getThisFactionIndex(); + assert(targetId == -1 || facing == -1); + this->targetId = targetId >= 0 ? targetId : facing; + this->fromFactionIndex = world->getThisFactionIndex(); - if(this->networkCommandType == nctGiveCommand) { - const Unit *unit= world->findUnitById(this->unitId); + if (this->networkCommandType == nctGiveCommand) { + const Unit *unit = world->findUnitById(this->unitId); - //validate unit - if(unit != NULL) { - this->unitFactionIndex = unit->getFaction()->getIndex(); - this->unitFactionUnitCount = unit->getFaction()->getUnitCount(); + //validate unit + if (unit != NULL) { + this->unitFactionIndex = unit->getFaction()->getIndex(); + this->unitFactionUnitCount = unit->getFaction()->getUnitCount(); - //const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId); - const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId); - if(ct != NULL && ct->getClass() == ccBuild) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); - CardinalDir::assertDirValid(facing); - assert(targetId == -1); - } - } - } + //const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), this->unitTypeId); + const CommandType *ct = unit->getType()->findCommandTypeById(this->commandTypeId); + if (ct != NULL && ct->getClass() == ccBuild) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", __FILE__, __FUNCTION__, __LINE__, toString().c_str()); + CardinalDir::assertDirValid(facing); + assert(targetId == -1); + } + } + } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] Created NetworkCommand as follows:\n%s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); -} + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] Created NetworkCommand as follows:\n%s\n", __FILE__, __FUNCTION__, __LINE__, toString().c_str()); + } -void NetworkCommand::preprocessNetworkCommand(World *world) { - if(networkCommandType == nctGiveCommand) { - const Unit *unit= world->findUnitById(unitId); + void NetworkCommand::preprocessNetworkCommand(World *world) { + if (networkCommandType == nctGiveCommand) { + const Unit *unit = world->findUnitById(unitId); - //validate unit - if(unit != NULL) { - //const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId); - const CommandType *ct = unit->getType()->findCommandTypeById(commandTypeId); - if(ct != NULL && ct->getClass() == ccBuild && targetId >= 0) { - CardinalDir::assertDirValid(targetId); - } - } - else { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] (unit == NULL) %s\n",__FILE__,__FUNCTION__,__LINE__,toString().c_str()); - } - } -} + //validate unit + if (unit != NULL) { + //const UnitType *unitType= world->findUnitTypeById(unit->getFaction()->getType(), unitTypeId); + const CommandType *ct = unit->getType()->findCommandTypeById(commandTypeId); + if (ct != NULL && ct->getClass() == ccBuild && targetId >= 0) { + CardinalDir::assertDirValid(targetId); + } + } else { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] (unit == NULL) %s\n", __FILE__, __FUNCTION__, __LINE__, toString().c_str()); + } + } + } -string NetworkCommand::toString() const { - char szBuf[8096]=""; - snprintf(szBuf,8096,"networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\npositionY = %d\nunitTypeId = %d\ntargetId = %d\nwantQueue= %d\nfromFactionIndex = %d\nunitFactionUnitCount = %d\nunitFactionIndex = %d, commandStateType = %d, commandStateValue = %d, unitCommandGroupId = %d", - networkCommandType,unitId,commandTypeId,positionX,positionY,unitTypeId,targetId,wantQueue, - fromFactionIndex,unitFactionUnitCount,unitFactionIndex,commandStateType,commandStateValue, - unitCommandGroupId); + string NetworkCommand::toString() const { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "networkCommandType = %d\nunitId = %d\ncommandTypeId = %d\npositionX = %d\npositionY = %d\nunitTypeId = %d\ntargetId = %d\nwantQueue= %d\nfromFactionIndex = %d\nunitFactionUnitCount = %d\nunitFactionIndex = %d, commandStateType = %d, commandStateValue = %d, unitCommandGroupId = %d", + networkCommandType, unitId, commandTypeId, positionX, positionY, unitTypeId, targetId, wantQueue, + fromFactionIndex, unitFactionUnitCount, unitFactionIndex, commandStateType, commandStateValue, + unitCommandGroupId); - string result = szBuf; - return result; -} + string result = szBuf; + return result; + } -void NetworkCommand::toEndian() { - networkCommandType = ::Shared::PlatformByteOrder::toCommonEndian(networkCommandType); - unitId = ::Shared::PlatformByteOrder::toCommonEndian(unitId); - unitTypeId = ::Shared::PlatformByteOrder::toCommonEndian(unitTypeId); - commandTypeId = ::Shared::PlatformByteOrder::toCommonEndian(commandTypeId); - positionX = ::Shared::PlatformByteOrder::toCommonEndian(positionX); - positionY = ::Shared::PlatformByteOrder::toCommonEndian(positionY); - targetId = ::Shared::PlatformByteOrder::toCommonEndian(targetId); - wantQueue = ::Shared::PlatformByteOrder::toCommonEndian(wantQueue); - fromFactionIndex = ::Shared::PlatformByteOrder::toCommonEndian(fromFactionIndex); - unitFactionUnitCount = ::Shared::PlatformByteOrder::toCommonEndian(unitFactionUnitCount); - unitFactionIndex = ::Shared::PlatformByteOrder::toCommonEndian(unitFactionIndex); - commandStateType = ::Shared::PlatformByteOrder::toCommonEndian(commandStateType); - commandStateValue = ::Shared::PlatformByteOrder::toCommonEndian(commandStateValue); - unitCommandGroupId = ::Shared::PlatformByteOrder::toCommonEndian(unitCommandGroupId); + void NetworkCommand::toEndian() { + networkCommandType = ::Shared::PlatformByteOrder::toCommonEndian(networkCommandType); + unitId = ::Shared::PlatformByteOrder::toCommonEndian(unitId); + unitTypeId = ::Shared::PlatformByteOrder::toCommonEndian(unitTypeId); + commandTypeId = ::Shared::PlatformByteOrder::toCommonEndian(commandTypeId); + positionX = ::Shared::PlatformByteOrder::toCommonEndian(positionX); + positionY = ::Shared::PlatformByteOrder::toCommonEndian(positionY); + targetId = ::Shared::PlatformByteOrder::toCommonEndian(targetId); + wantQueue = ::Shared::PlatformByteOrder::toCommonEndian(wantQueue); + fromFactionIndex = ::Shared::PlatformByteOrder::toCommonEndian(fromFactionIndex); + unitFactionUnitCount = ::Shared::PlatformByteOrder::toCommonEndian(unitFactionUnitCount); + unitFactionIndex = ::Shared::PlatformByteOrder::toCommonEndian(unitFactionIndex); + commandStateType = ::Shared::PlatformByteOrder::toCommonEndian(commandStateType); + commandStateValue = ::Shared::PlatformByteOrder::toCommonEndian(commandStateValue); + unitCommandGroupId = ::Shared::PlatformByteOrder::toCommonEndian(unitCommandGroupId); -} -void NetworkCommand::fromEndian() { - networkCommandType = ::Shared::PlatformByteOrder::fromCommonEndian(networkCommandType); - unitId = ::Shared::PlatformByteOrder::fromCommonEndian(unitId); - unitTypeId = ::Shared::PlatformByteOrder::fromCommonEndian(unitTypeId); - commandTypeId = ::Shared::PlatformByteOrder::fromCommonEndian(commandTypeId); - positionX = ::Shared::PlatformByteOrder::fromCommonEndian(positionX); - positionY = ::Shared::PlatformByteOrder::fromCommonEndian(positionY); - targetId = ::Shared::PlatformByteOrder::fromCommonEndian(targetId); - wantQueue = ::Shared::PlatformByteOrder::fromCommonEndian(wantQueue); - fromFactionIndex = ::Shared::PlatformByteOrder::fromCommonEndian(fromFactionIndex); - unitFactionUnitCount = ::Shared::PlatformByteOrder::fromCommonEndian(unitFactionUnitCount); - unitFactionIndex = ::Shared::PlatformByteOrder::fromCommonEndian(unitFactionIndex); - commandStateType = ::Shared::PlatformByteOrder::fromCommonEndian(commandStateType); - commandStateValue = ::Shared::PlatformByteOrder::fromCommonEndian(commandStateValue); - unitCommandGroupId = ::Shared::PlatformByteOrder::fromCommonEndian(unitCommandGroupId); -} + } + void NetworkCommand::fromEndian() { + networkCommandType = ::Shared::PlatformByteOrder::fromCommonEndian(networkCommandType); + unitId = ::Shared::PlatformByteOrder::fromCommonEndian(unitId); + unitTypeId = ::Shared::PlatformByteOrder::fromCommonEndian(unitTypeId); + commandTypeId = ::Shared::PlatformByteOrder::fromCommonEndian(commandTypeId); + positionX = ::Shared::PlatformByteOrder::fromCommonEndian(positionX); + positionY = ::Shared::PlatformByteOrder::fromCommonEndian(positionY); + targetId = ::Shared::PlatformByteOrder::fromCommonEndian(targetId); + wantQueue = ::Shared::PlatformByteOrder::fromCommonEndian(wantQueue); + fromFactionIndex = ::Shared::PlatformByteOrder::fromCommonEndian(fromFactionIndex); + unitFactionUnitCount = ::Shared::PlatformByteOrder::fromCommonEndian(unitFactionUnitCount); + unitFactionIndex = ::Shared::PlatformByteOrder::fromCommonEndian(unitFactionIndex); + commandStateType = ::Shared::PlatformByteOrder::fromCommonEndian(commandStateType); + commandStateValue = ::Shared::PlatformByteOrder::fromCommonEndian(commandStateValue); + unitCommandGroupId = ::Shared::PlatformByteOrder::fromCommonEndian(unitCommandGroupId); + } -XmlNode * NetworkCommand::saveGame(XmlNode *rootNode) { - std::map mapTagReplacements; - XmlNode *networkCommandNode = rootNode->addChild("NetworkCommand"); + XmlNode * NetworkCommand::saveGame(XmlNode *rootNode) { + std::map mapTagReplacements; + XmlNode *networkCommandNode = rootNode->addChild("NetworkCommand"); -// int16 networkCommandType; - networkCommandNode->addAttribute("networkCommandType",intToStr(networkCommandType), mapTagReplacements); -// int32 unitId; - networkCommandNode->addAttribute("unitId",intToStr(unitId), mapTagReplacements); -// int16 unitTypeId; - networkCommandNode->addAttribute("unitTypeId",intToStr(unitTypeId), mapTagReplacements); -// int16 commandTypeId; - networkCommandNode->addAttribute("commandTypeId",intToStr(commandTypeId), mapTagReplacements); -// int16 positionX; - networkCommandNode->addAttribute("positionX",intToStr(positionX), mapTagReplacements); -// int16 positionY; - networkCommandNode->addAttribute("positionY",intToStr(positionY), mapTagReplacements); -// int32 targetId; - networkCommandNode->addAttribute("targetId",intToStr(targetId), mapTagReplacements); -// int8 wantQueue; - networkCommandNode->addAttribute("wantQueue",intToStr(wantQueue), mapTagReplacements); -// int8 fromFactionIndex; - networkCommandNode->addAttribute("fromFactionIndex",intToStr(fromFactionIndex), mapTagReplacements); -// uint16 unitFactionUnitCount; - networkCommandNode->addAttribute("unitFactionUnitCount",intToStr(unitFactionUnitCount), mapTagReplacements); -// int8 unitFactionIndex; - networkCommandNode->addAttribute("unitFactionIndex",intToStr(unitFactionIndex), mapTagReplacements); -// int8 commandStateType; - networkCommandNode->addAttribute("commandStateType",intToStr(commandStateType), mapTagReplacements); -// int32 commandStateValue; - networkCommandNode->addAttribute("commandStateValue",intToStr(commandStateValue), mapTagReplacements); -// int32 unitCommandGroupId; - networkCommandNode->addAttribute("unitCommandGroupId",intToStr(unitCommandGroupId), mapTagReplacements); + // int16 networkCommandType; + networkCommandNode->addAttribute("networkCommandType", intToStr(networkCommandType), mapTagReplacements); + // int32 unitId; + networkCommandNode->addAttribute("unitId", intToStr(unitId), mapTagReplacements); + // int16 unitTypeId; + networkCommandNode->addAttribute("unitTypeId", intToStr(unitTypeId), mapTagReplacements); + // int16 commandTypeId; + networkCommandNode->addAttribute("commandTypeId", intToStr(commandTypeId), mapTagReplacements); + // int16 positionX; + networkCommandNode->addAttribute("positionX", intToStr(positionX), mapTagReplacements); + // int16 positionY; + networkCommandNode->addAttribute("positionY", intToStr(positionY), mapTagReplacements); + // int32 targetId; + networkCommandNode->addAttribute("targetId", intToStr(targetId), mapTagReplacements); + // int8 wantQueue; + networkCommandNode->addAttribute("wantQueue", intToStr(wantQueue), mapTagReplacements); + // int8 fromFactionIndex; + networkCommandNode->addAttribute("fromFactionIndex", intToStr(fromFactionIndex), mapTagReplacements); + // uint16 unitFactionUnitCount; + networkCommandNode->addAttribute("unitFactionUnitCount", intToStr(unitFactionUnitCount), mapTagReplacements); + // int8 unitFactionIndex; + networkCommandNode->addAttribute("unitFactionIndex", intToStr(unitFactionIndex), mapTagReplacements); + // int8 commandStateType; + networkCommandNode->addAttribute("commandStateType", intToStr(commandStateType), mapTagReplacements); + // int32 commandStateValue; + networkCommandNode->addAttribute("commandStateValue", intToStr(commandStateValue), mapTagReplacements); + // int32 unitCommandGroupId; + networkCommandNode->addAttribute("unitCommandGroupId", intToStr(unitCommandGroupId), mapTagReplacements); - return networkCommandNode; -} + return networkCommandNode; + } -void NetworkCommand::loadGame(const XmlNode *rootNode) { - const XmlNode *networkCommandNode = rootNode; + void NetworkCommand::loadGame(const XmlNode *rootNode) { + const XmlNode *networkCommandNode = rootNode; -// int16 networkCommandType; - networkCommandType = networkCommandNode->getAttribute("networkCommandType")->getIntValue(); -// int32 unitId; - unitId = networkCommandNode->getAttribute("unitId")->getIntValue(); -// int16 unitTypeId; - unitTypeId = networkCommandNode->getAttribute("unitTypeId")->getIntValue(); -// int16 commandTypeId; - commandTypeId = networkCommandNode->getAttribute("commandTypeId")->getIntValue(); -// int16 positionX; - positionX = networkCommandNode->getAttribute("positionX")->getIntValue(); -// int16 positionY; - positionY = networkCommandNode->getAttribute("positionY")->getIntValue(); -// int32 targetId; - targetId = networkCommandNode->getAttribute("targetId")->getIntValue(); -// int8 wantQueue; - wantQueue = networkCommandNode->getAttribute("wantQueue")->getIntValue(); -// int8 fromFactionIndex; - fromFactionIndex = networkCommandNode->getAttribute("fromFactionIndex")->getIntValue(); -// uint16 unitFactionUnitCount; - unitFactionUnitCount = networkCommandNode->getAttribute("unitFactionUnitCount")->getIntValue(); -// int8 unitFactionIndex; - unitFactionIndex = networkCommandNode->getAttribute("unitFactionIndex")->getIntValue(); -// int8 commandStateType; - commandStateType = networkCommandNode->getAttribute("commandStateType")->getIntValue(); -// int32 commandStateValue; - commandStateValue = networkCommandNode->getAttribute("commandStateValue")->getIntValue(); -// int32 unitCommandGroupId; - unitCommandGroupId = networkCommandNode->getAttribute("unitCommandGroupId")->getIntValue(); -} + // int16 networkCommandType; + networkCommandType = networkCommandNode->getAttribute("networkCommandType")->getIntValue(); + // int32 unitId; + unitId = networkCommandNode->getAttribute("unitId")->getIntValue(); + // int16 unitTypeId; + unitTypeId = networkCommandNode->getAttribute("unitTypeId")->getIntValue(); + // int16 commandTypeId; + commandTypeId = networkCommandNode->getAttribute("commandTypeId")->getIntValue(); + // int16 positionX; + positionX = networkCommandNode->getAttribute("positionX")->getIntValue(); + // int16 positionY; + positionY = networkCommandNode->getAttribute("positionY")->getIntValue(); + // int32 targetId; + targetId = networkCommandNode->getAttribute("targetId")->getIntValue(); + // int8 wantQueue; + wantQueue = networkCommandNode->getAttribute("wantQueue")->getIntValue(); + // int8 fromFactionIndex; + fromFactionIndex = networkCommandNode->getAttribute("fromFactionIndex")->getIntValue(); + // uint16 unitFactionUnitCount; + unitFactionUnitCount = networkCommandNode->getAttribute("unitFactionUnitCount")->getIntValue(); + // int8 unitFactionIndex; + unitFactionIndex = networkCommandNode->getAttribute("unitFactionIndex")->getIntValue(); + // int8 commandStateType; + commandStateType = networkCommandNode->getAttribute("commandStateType")->getIntValue(); + // int32 commandStateValue; + commandStateValue = networkCommandNode->getAttribute("commandStateValue")->getIntValue(); + // int32 unitCommandGroupId; + unitCommandGroupId = networkCommandNode->getAttribute("unitCommandGroupId")->getIntValue(); + } -}}//end namespace + } +}//end namespace diff --git a/source/glest_game/network/network_types.h b/source/glest_game/network/network_types.h index 224935821..17d37b9f7 100644 --- a/source/glest_game/network/network_types.h +++ b/source/glest_game/network/network_types.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_NETWORKTYPES_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -32,137 +32,169 @@ using Shared::Platform::uint16; using Shared::Platform::int32; using Shared::Graphics::Vec2i; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class World; -// ===================================================== -// class NetworkString -// ===================================================== + class World; + // ===================================================== + // class NetworkString + // ===================================================== #pragma pack(push, 1) -template -class NetworkString{ -private: - char buffer[S]; + template + class NetworkString { + private: + char buffer[S]; -public: - NetworkString() { - memset(buffer, 0, S); - } - NetworkString & operator=(const string& str) { - // ensure we don't have a buffer overflow - int maxBufferSize = sizeof(buffer) / sizeof(buffer[0]); - strncpy(buffer, str.c_str(), min(S-1,maxBufferSize-1)); + public: + NetworkString() { + memset(buffer, 0, S); + } + NetworkString & operator=(const string& str) { + // ensure we don't have a buffer overflow + int maxBufferSize = sizeof(buffer) / sizeof(buffer[0]); + strncpy(buffer, str.c_str(), min(S - 1, maxBufferSize - 1)); - return *this; - } - void nullTerminate() { - int maxBufferSize = sizeof(buffer) / sizeof(buffer[0]); - buffer[maxBufferSize-1] = '\0'; - } + return *this; + } + void nullTerminate() { + int maxBufferSize = sizeof(buffer) / sizeof(buffer[0]); + buffer[maxBufferSize - 1] = '\0'; + } - char *getBuffer() { return &buffer[0]; } - string getString() const { return (buffer[0] != '\0' ? buffer : ""); } -}; + char *getBuffer() { + return &buffer[0]; + } + string getString() const { + return (buffer[0] != '\0' ? buffer : ""); + } + }; #pragma pack(pop) -// ===================================================== -// class NetworkCommand -// ===================================================== + // ===================================================== + // class NetworkCommand + // ===================================================== -enum NetworkCommandType { - nctGiveCommand, - nctCancelCommand, - nctSetMeetingPoint, - nctSwitchTeam, - nctSwitchTeamVote, - nctPauseResume, - nctPlayerStatusChange, - nctDisconnectNetworkPlayer - //nctNetworkCommand -}; + enum NetworkCommandType { + nctGiveCommand, + nctCancelCommand, + nctSetMeetingPoint, + nctSwitchTeam, + nctSwitchTeamVote, + nctPauseResume, + nctPlayerStatusChange, + nctDisconnectNetworkPlayer + //nctNetworkCommand + }; -//enum NetworkCommandSubType { -// ncstRotateUnit -//}; + //enum NetworkCommandSubType { + // ncstRotateUnit + //}; #pragma pack(push, 1) -class NetworkCommand { + class NetworkCommand { -public: - NetworkCommand() { - networkCommandType=0; - unitId=0; - unitTypeId=0; - commandTypeId=0; - positionX=0; - positionY=0; - targetId=0; - wantQueue=0; - fromFactionIndex=0; - unitFactionUnitCount=0; - unitFactionIndex=0; - commandStateType=0; - commandStateValue=0; - unitCommandGroupId=0; - } + public: + NetworkCommand() { + networkCommandType = 0; + unitId = 0; + unitTypeId = 0; + commandTypeId = 0; + positionX = 0; + positionY = 0; + targetId = 0; + wantQueue = 0; + fromFactionIndex = 0; + unitFactionUnitCount = 0; + unitFactionIndex = 0; + commandStateType = 0; + commandStateValue = 0; + unitCommandGroupId = 0; + } - NetworkCommand( - World *world, - int networkCommandType, - int unitId, - int commandTypeId= -1, - const Vec2i &pos= Vec2i(0), - int unitTypeId= -1, - int targetId= -1, - int facing= -1, - bool wantQueue = false, - CommandStateType commandStateType = cst_None, - int commandTypeStateValue = -1, - int unitCommandGroupId = -1); + NetworkCommand( + World *world, + int networkCommandType, + int unitId, + int commandTypeId = -1, + const Vec2i &pos = Vec2i(0), + int unitTypeId = -1, + int targetId = -1, + int facing = -1, + bool wantQueue = false, + CommandStateType commandStateType = cst_None, + int commandTypeStateValue = -1, + int unitCommandGroupId = -1); - int16 networkCommandType; - int32 unitId; - int16 unitTypeId; - int16 commandTypeId; - int16 positionX; - int16 positionY; - int32 targetId; - int8 wantQueue; - int8 fromFactionIndex; - uint16 unitFactionUnitCount; - int8 unitFactionIndex; - int8 commandStateType; - int32 commandStateValue; - int32 unitCommandGroupId; + int16 networkCommandType; + int32 unitId; + int16 unitTypeId; + int16 commandTypeId; + int16 positionX; + int16 positionY; + int32 targetId; + int8 wantQueue; + int8 fromFactionIndex; + uint16 unitFactionUnitCount; + int8 unitFactionIndex; + int8 commandStateType; + int32 commandStateValue; + int32 unitCommandGroupId; - NetworkCommandType getNetworkCommandType() const {return static_cast(networkCommandType);} - int getUnitId() const {return unitId;} - int getCommandTypeId() const {return commandTypeId;} - Vec2i getPosition() const {return Vec2i(positionX, positionY);} - int getUnitTypeId() const {return unitTypeId;} - int getTargetId() const {return targetId;} - int getWantQueue() const {return wantQueue;} - int getFromFactionIndex() const {return fromFactionIndex;} - int getUnitFactionUnitCount() const {return unitFactionUnitCount;} - int getUnitFactionIndex() const {return unitFactionIndex;} + NetworkCommandType getNetworkCommandType() const { + return static_cast(networkCommandType); + } + int getUnitId() const { + return unitId; + } + int getCommandTypeId() const { + return commandTypeId; + } + Vec2i getPosition() const { + return Vec2i(positionX, positionY); + } + int getUnitTypeId() const { + return unitTypeId; + } + int getTargetId() const { + return targetId; + } + int getWantQueue() const { + return wantQueue; + } + int getFromFactionIndex() const { + return fromFactionIndex; + } + int getUnitFactionUnitCount() const { + return unitFactionUnitCount; + } + int getUnitFactionIndex() const { + return unitFactionIndex; + } - CommandStateType getCommandStateType() const {return static_cast(commandStateType);} - int getCommandStateValue() const {return commandStateValue;} + CommandStateType getCommandStateType() const { + return static_cast(commandStateType); + } + int getCommandStateValue() const { + return commandStateValue; + } - int getUnitCommandGroupId() const { return unitCommandGroupId; } + int getUnitCommandGroupId() const { + return unitCommandGroupId; + } - void preprocessNetworkCommand(World *world); - string toString() const; + void preprocessNetworkCommand(World *world); + string toString() const; - void toEndian(); - void fromEndian(); + void toEndian(); + void fromEndian(); - XmlNode * saveGame(XmlNode *rootNode); - void loadGame(const XmlNode *rootNode); -}; + XmlNode * saveGame(XmlNode *rootNode); + void loadGame(const XmlNode *rootNode); + }; #pragma pack(pop) -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 42916dd3c..4325f1086 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -48,3263 +48,3222 @@ using namespace Shared::Platform; using namespace Shared::Util; using namespace Shared::Map; -namespace Glest { namespace Game { +namespace Glest { + namespace Game { -double maxFrameCountLagAllowed = 30; -double maxClientLagTimeAllowed = 25; -double maxFrameCountLagAllowedEver = 30; -double maxClientLagTimeAllowedEver = 25; + double maxFrameCountLagAllowed = 30; + double maxClientLagTimeAllowed = 25; + double maxFrameCountLagAllowedEver = 30; + double maxClientLagTimeAllowedEver = 25; -double warnFrameCountLagPercent = 0.50; -double LAG_CHECK_GRACE_PERIOD = 15; -double LAG_CHECK_INTERVAL_PERIOD = 4; -int GRACE_LAG_CTR_LIMIT = 5; + double warnFrameCountLagPercent = 0.50; + double LAG_CHECK_GRACE_PERIOD = 15; + double LAG_CHECK_INTERVAL_PERIOD = 4; + int GRACE_LAG_CTR_LIMIT = 5; -const int MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = 15000; -const int MAX_CLIENT_PAUSE_FOR_LAG_COUNT = 3; -const int MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS = 1500; -const int MASTERSERVER_HEARTBEAT_GAME_STATUS_SECONDS = 30; + const int MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = 15000; + const int MAX_CLIENT_PAUSE_FOR_LAG_COUNT = 3; + const int MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS = 1500; + const int MASTERSERVER_HEARTBEAT_GAME_STATUS_SECONDS = 30; -const int MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS = 4000; + const int MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS = 4000; -ServerInterface::ServerInterface(bool publishEnabled, ClientLagCallbackInterface *clientLagCallbackInterface) : GameNetworkInterface() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + ServerInterface::ServerInterface(bool publishEnabled, ClientLagCallbackInterface *clientLagCallbackInterface) : GameNetworkInterface() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - this->clientLagCallbackInterface = clientLagCallbackInterface; - this->clientsAutoPausedDueToLag = false; + this->clientLagCallbackInterface = clientLagCallbackInterface; + this->clientsAutoPausedDueToLag = false; - allowInGameConnections = false; - gameLaunched = false; + allowInGameConnections = false; + gameLaunched = false; - serverSynchAccessor = new Mutex(CODE_AT_LINE); - switchSetupRequestsSynchAccessor = new Mutex(CODE_AT_LINE); + serverSynchAccessor = new Mutex(CODE_AT_LINE); + switchSetupRequestsSynchAccessor = new Mutex(CODE_AT_LINE); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - slotAccessorMutexes[index] = new Mutex(CODE_AT_LINE); - } - masterServerThreadAccessor = new Mutex(CODE_AT_LINE); - textMessageQueueThreadAccessor = new Mutex(CODE_AT_LINE); - broadcastMessageQueueThreadAccessor = new Mutex(CODE_AT_LINE); - inBroadcastMessageThreadAccessor = new Mutex(CODE_AT_LINE); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + slotAccessorMutexes[index] = new Mutex(CODE_AT_LINE); + } + masterServerThreadAccessor = new Mutex(CODE_AT_LINE); + textMessageQueueThreadAccessor = new Mutex(CODE_AT_LINE); + broadcastMessageQueueThreadAccessor = new Mutex(CODE_AT_LINE); + inBroadcastMessageThreadAccessor = new Mutex(CODE_AT_LINE); - serverSocketAdmin = NULL; - nextEventId = 1; - gameHasBeenInitiated = false; - exitServer = false; - gameSettingsUpdateCount = 0; - currentFrameCount = 0; - gameStartTime = 0; - resumeGameStartTime = 0; - publishToMasterserverThread = NULL; - lastMasterserverHeartbeatTime = 0; - needToRepublishToMasterserver = false; - ftpServer = NULL; - inBroadcastMessage = false; - lastGlobalLagCheckTime = 0; - masterserverAdminRequestLaunch = false; - lastListenerSlotCheckTime = 0; + serverSocketAdmin = NULL; + nextEventId = 1; + gameHasBeenInitiated = false; + exitServer = false; + gameSettingsUpdateCount = 0; + currentFrameCount = 0; + gameStartTime = 0; + resumeGameStartTime = 0; + publishToMasterserverThread = NULL; + lastMasterserverHeartbeatTime = 0; + needToRepublishToMasterserver = false; + ftpServer = NULL; + inBroadcastMessage = false; + lastGlobalLagCheckTime = 0; + masterserverAdminRequestLaunch = false; + lastListenerSlotCheckTime = 0; - // This is an admin port listening only on the localhost intended to - // give current connection status info + // This is an admin port listening only on the localhost intended to + // give current connection status info #ifndef __APPLE__ - try { - serverSocketAdmin = new ServerSocket(true); - serverSocketAdmin->setBlock(false); - serverSocketAdmin->setBindPort(Config::getInstance().getInt("ServerAdminPort", intToStr(GameConstants::serverAdminPort).c_str())); - serverSocketAdmin->setBindSpecificAddress(Config::getInstance().getString("ServerAdminBindAddress", "127.0.0.1")); - serverSocketAdmin->listen(5); - } - catch(const std::exception &ex) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s Line: %d] Warning Server admin port bind/listen error:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + try { + serverSocketAdmin = new ServerSocket(true); + serverSocketAdmin->setBlock(false); + serverSocketAdmin->setBindPort(Config::getInstance().getInt("ServerAdminPort", intToStr(GameConstants::serverAdminPort).c_str())); + serverSocketAdmin->setBindSpecificAddress(Config::getInstance().getString("ServerAdminBindAddress", "127.0.0.1")); + serverSocketAdmin->listen(5); + } catch (const std::exception &ex) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s Line: %d] Warning Server admin port bind/listen error:\n%s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "%s", szBuf); + } #endif - maxFrameCountLagAllowed = Config::getInstance().getInt("MaxFrameCountLagAllowed", intToStr(maxFrameCountLagAllowed).c_str()); - maxFrameCountLagAllowedEver = Config::getInstance().getInt("MaxFrameCountLagAllowedEver", intToStr(maxFrameCountLagAllowedEver).c_str()); - maxClientLagTimeAllowedEver = Config::getInstance().getInt("MaxClientLagTimeAllowedEver", intToStr(maxClientLagTimeAllowedEver).c_str()); - maxClientLagTimeAllowed = Config::getInstance().getInt("MaxClientLagTimeAllowed", intToStr(maxClientLagTimeAllowed).c_str()); - warnFrameCountLagPercent = Config::getInstance().getFloat("WarnFrameCountLagPercent", doubleToStr(warnFrameCountLagPercent).c_str()); + maxFrameCountLagAllowed = Config::getInstance().getInt("MaxFrameCountLagAllowed", intToStr(maxFrameCountLagAllowed).c_str()); + maxFrameCountLagAllowedEver = Config::getInstance().getInt("MaxFrameCountLagAllowedEver", intToStr(maxFrameCountLagAllowedEver).c_str()); + maxClientLagTimeAllowedEver = Config::getInstance().getInt("MaxClientLagTimeAllowedEver", intToStr(maxClientLagTimeAllowedEver).c_str()); + maxClientLagTimeAllowed = Config::getInstance().getInt("MaxClientLagTimeAllowed", intToStr(maxClientLagTimeAllowed).c_str()); + warnFrameCountLagPercent = Config::getInstance().getFloat("WarnFrameCountLagPercent", doubleToStr(warnFrameCountLagPercent).c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] maxFrameCountLagAllowed = %f, maxFrameCountLagAllowedEver = %f, maxClientLagTimeAllowed = %f, maxClientLagTimeAllowedEver = %f\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,maxFrameCountLagAllowed,maxFrameCountLagAllowedEver,maxClientLagTimeAllowed,maxClientLagTimeAllowedEver); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] maxFrameCountLagAllowed = %f, maxFrameCountLagAllowedEver = %f, maxClientLagTimeAllowed = %f, maxClientLagTimeAllowedEver = %f\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, maxFrameCountLagAllowed, maxFrameCountLagAllowedEver, maxClientLagTimeAllowed, maxClientLagTimeAllowedEver); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - slots[index] = NULL; - switchSetupRequests[index] = NULL; - } + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + slots[index] = NULL; + switchSetupRequests[index] = NULL; + } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - serverSocket.setBlock(false); - serverSocket.setBindPort(Config::getInstance().getInt("PortServer", intToStr(GameConstants::serverPort).c_str())); + serverSocket.setBlock(false); + serverSocket.setBindPort(Config::getInstance().getInt("PortServer", intToStr(GameConstants::serverPort).c_str())); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - gameStatsThreadAccessor = new Mutex(CODE_AT_LINE); - gameStats = NULL; + gameStatsThreadAccessor = new Mutex(CODE_AT_LINE); + gameStats = NULL; - Config &config = Config::getInstance(); - string scenarioDir = ""; - vector pathList = config.getPathListForType(ptMaps,scenarioDir); - vector invalidMapList; - vector allMaps = MapPreview::findAllValidMaps(pathList,scenarioDir,false,true,&invalidMapList); - if (allMaps.empty()) { - //throw megaglest_runtime_error("No maps were found!"); - printf("No maps were found (srv)!\n"); - } - std::sort(allMaps.begin(),allMaps.end(),compareNonCaseSensitive); - vector results; - copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); - mapFiles = results; + Config &config = Config::getInstance(); + string scenarioDir = ""; + vector pathList = config.getPathListForType(ptMaps, scenarioDir); + vector invalidMapList; + vector allMaps = MapPreview::findAllValidMaps(pathList, scenarioDir, false, true, &invalidMapList); + if (allMaps.empty()) { + //throw megaglest_runtime_error("No maps were found!"); + printf("No maps were found (srv)!\n"); + } + std::sort(allMaps.begin(), allMaps.end(), compareNonCaseSensitive); + vector results; + copy(allMaps.begin(), allMaps.end(), std::back_inserter(results)); + mapFiles = results; - //player Sorted maps - //////////////////// - for(unsigned int i = 0; i < GameConstants::maxPlayers+1; ++i) { - playerSortedMaps[i].clear(); - } + //player Sorted maps + //////////////////// + for (unsigned int i = 0; i < GameConstants::maxPlayers + 1; ++i) { + playerSortedMaps[i].clear(); + } - // at index=0 fill in the whole list - copy(mapFiles.begin(), mapFiles.end(), std::back_inserter(playerSortedMaps[0])); + // at index=0 fill in the whole list + copy(mapFiles.begin(), mapFiles.end(), std::back_inserter(playerSortedMaps[0])); - MapInfo mapInfo; - // fill playerSortedMaps according to map player count - for(int i= 0; i < (int)mapFiles.size(); i++){// fetch info and put map in right list - //printf("mapFiles.at(i) %s allMaps.at[i] %s\n",mapFiles[i].c_str(),allMaps.at(i).c_str()); - MapPreview::loadMapInfo(Config::getMapPath(mapFiles.at(i)), &mapInfo, "MaxPlayers","Size",true) ; - playerSortedMaps[mapInfo.players].push_back(mapFiles.at(i)); - } - /////////////////// + MapInfo mapInfo; + // fill playerSortedMaps according to map player count + for (int i = 0; i < (int) mapFiles.size(); i++) {// fetch info and put map in right list + //printf("mapFiles.at(i) %s allMaps.at[i] %s\n",mapFiles[i].c_str(),allMaps.at(i).c_str()); + MapPreview::loadMapInfo(Config::getMapPath(mapFiles.at(i)), &mapInfo, "MaxPlayers", "Size", true); + playerSortedMaps[mapInfo.players].push_back(mapFiles.at(i)); + } + /////////////////// - results.clear(); - findDirs(config.getPathListForType(ptTilesets), results); - if (results.empty()) { - //throw megaglest_runtime_error("No tile-sets were found!"); - printf("No tile-sets were found (srv)!"); - } - tilesetFiles = results; + results.clear(); + findDirs(config.getPathListForType(ptTilesets), results); + if (results.empty()) { + //throw megaglest_runtime_error("No tile-sets were found!"); + printf("No tile-sets were found (srv)!"); + } + tilesetFiles = results; - results.clear(); - findDirs(config.getPathListForType(ptTechs), results); - if(results.empty()) { - //throw megaglest_runtime_error("No tech-trees were found!"); - printf("No tech-trees were found (srv)!\n"); - } - techTreeFiles = results; + results.clear(); + findDirs(config.getPathListForType(ptTechs), results); + if (results.empty()) { + //throw megaglest_runtime_error("No tech-trees were found!"); + printf("No tech-trees were found (srv)!\n"); + } + techTreeFiles = results; - if(Config::getInstance().getBool("EnableFTPServer","true") == true) { - std::pair mapsPath; - vector pathList = Config::getInstance().getPathListForType(ptMaps); - if(pathList.empty() == false) { - mapsPath.first = pathList[0]; - if(pathList.size() > 1) { - mapsPath.second = pathList[1]; + if (Config::getInstance().getBool("EnableFTPServer", "true") == true) { + std::pair mapsPath; + vector pathList = Config::getInstance().getPathListForType(ptMaps); + if (pathList.empty() == false) { + mapsPath.first = pathList[0]; + if (pathList.size() > 1) { + mapsPath.second = pathList[1]; + } + } + + std::pair tilesetsPath; + vector tilesetsList = Config::getInstance().getPathListForType(ptTilesets); + if (tilesetsList.empty() == false) { + tilesetsPath.first = tilesetsList[0]; + if (tilesetsList.size() > 1) { + tilesetsPath.second = tilesetsList[1]; + } + } + + std::pair techtreesPath; + vector techtreesList = Config::getInstance().getPathListForType(ptTechs); + if (techtreesList.empty() == false) { + techtreesPath.first = techtreesList[0]; + if (techtreesList.size() > 1) { + techtreesPath.second = techtreesList[1]; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + int portNumber = Config::getInstance().getInt("FTPServerPort", intToStr(ServerSocket::getFTPServerPort()).c_str()); + ServerSocket::setFTPServerPort(portNumber); + //printf("In [%s::%s] portNumber = %d ServerSocket::getFTPServerPort() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,portNumber,ServerSocket::getFTPServerPort()); + + bool allowInternetTilesetFileTransfers = Config::getInstance().getBool("EnableFTPServerInternetTilesetXfer", "true"); + bool allowInternetTechtreeFileTransfers = Config::getInstance().getBool("EnableFTPServerInternetTechtreeXfer", "true"); + + // Get path to temp files + string tempFilePath = "temp/"; + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { + tempFilePath = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + tempFilePath; + } else { + string userData = config.getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + tempFilePath = userData + tempFilePath; + } + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Temp files path [%s]\n", tempFilePath.c_str()); + + ftpServer = new FTPServerThread(mapsPath, tilesetsPath, techtreesPath, + publishEnabled, allowInternetTilesetFileTransfers, + allowInternetTechtreeFileTransfers, portNumber, GameConstants::maxPlayers, + this, tempFilePath); + ftpServer->start(); + } + + if (publishToMasterserverThread == NULL) { + if (needToRepublishToMasterserver == true || GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + publishToMasterserverThread = new SimpleTaskThread(this, 0, 125); + publishToMasterserverThread->setUniqueID(mutexOwnerId); + publishToMasterserverThread->start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::setPublishEnabled(bool value) { + if (ftpServer != NULL) { + ftpServer->setInternetEnabled(value); } } - std::pair tilesetsPath; - vector tilesetsList = Config::getInstance().getPathListForType(ptTilesets); - if(tilesetsList.empty() == false) { - tilesetsPath.first = tilesetsList[0]; - if(tilesetsList.size() > 1) { - tilesetsPath.second = tilesetsList[1]; - } - } + void ServerInterface::shutdownMasterserverPublishThread() { + MutexSafeWrapper safeMutex(masterServerThreadAccessor, CODE_AT_LINE); - std::pair techtreesPath; - vector techtreesList = Config::getInstance().getPathListForType(ptTechs); - if(techtreesList.empty() == false) { - techtreesPath.first = techtreesList[0]; - if(techtreesList.size() > 1) { - techtreesPath.second = techtreesList[1]; - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - int portNumber = Config::getInstance().getInt("FTPServerPort",intToStr(ServerSocket::getFTPServerPort()).c_str()); - ServerSocket::setFTPServerPort(portNumber); - //printf("In [%s::%s] portNumber = %d ServerSocket::getFTPServerPort() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,portNumber,ServerSocket::getFTPServerPort()); - - bool allowInternetTilesetFileTransfers = Config::getInstance().getBool("EnableFTPServerInternetTilesetXfer","true"); - bool allowInternetTechtreeFileTransfers = Config::getInstance().getBool("EnableFTPServerInternetTechtreeXfer","true"); - - // Get path to temp files - string tempFilePath = "temp/"; - if(getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { - tempFilePath = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + tempFilePath; - } - else { - string userData = config.getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - tempFilePath = userData + tempFilePath; - } - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Temp files path [%s]\n",tempFilePath.c_str()); - - ftpServer = new FTPServerThread(mapsPath,tilesetsPath,techtreesPath, - publishEnabled,allowInternetTilesetFileTransfers, - allowInternetTechtreeFileTransfers,portNumber,GameConstants::maxPlayers, - this,tempFilePath); - ftpServer->start(); - } - - if(publishToMasterserverThread == NULL) { - if(needToRepublishToMasterserver == true || GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - publishToMasterserverThread = new SimpleTaskThread(this,0,125); - publishToMasterserverThread->setUniqueID(mutexOwnerId); - publishToMasterserverThread->start(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::setPublishEnabled(bool value) { - if(ftpServer != NULL) { - ftpServer->setInternetEnabled(value); - } -} - -void ServerInterface::shutdownMasterserverPublishThread() { - MutexSafeWrapper safeMutex(masterServerThreadAccessor,CODE_AT_LINE); - - if(publishToMasterserverThread != NULL) { - time_t elapsed = time(NULL); - publishToMasterserverThread->signalQuit(); - for(;publishToMasterserverThread->canShutdown(false) == false && - difftime((long int)time(NULL),elapsed) <= 15;) { - //sleep(150); - } - if(publishToMasterserverThread->canShutdown(true)) { - delete publishToMasterserverThread; - publishToMasterserverThread = NULL; - } - } -} - -ServerInterface::~ServerInterface() { - //printf("===> Destructor for ServerInterface\n"); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - masterController.clearSlaves(true); - exitServer = true; - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - if(slots[index] != NULL) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - delete slots[index]; - slots[index] = NULL; - } - - if(switchSetupRequests[index] != NULL) { - delete switchSetupRequests[index]; - switchSetupRequests[index] = NULL; - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - close(); - shutdownFTPServer(); - shutdownMasterserverPublishThread(); - - lastMasterserverHeartbeatTime = 0; - if(needToRepublishToMasterserver == true) { - simpleTask(NULL,NULL); - } - - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - delete slotAccessorMutexes[index]; - slotAccessorMutexes[index] = NULL; - } - - delete textMessageQueueThreadAccessor; - textMessageQueueThreadAccessor = NULL; - - delete broadcastMessageQueueThreadAccessor; - broadcastMessageQueueThreadAccessor = NULL; - - delete inBroadcastMessageThreadAccessor; - inBroadcastMessageThreadAccessor = NULL; - - delete serverSynchAccessor; - serverSynchAccessor = NULL; - - delete masterServerThreadAccessor; - masterServerThreadAccessor = NULL; - - delete serverSocketAdmin; - serverSocketAdmin = NULL; - - for(int index = 0; index < (int)broadcastMessageQueue.size(); ++index) { - pair &item = broadcastMessageQueue[index]; - if(item.first != NULL) { - delete item.first; - } - item.first = NULL; - } - broadcastMessageQueue.clear(); - - delete switchSetupRequestsSynchAccessor; - switchSetupRequestsSynchAccessor = NULL; - - delete gameStatsThreadAccessor; - gameStatsThreadAccessor = NULL; - - delete gameStats; - gameStats = NULL; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -SwitchSetupRequest ** ServerInterface::getSwitchSetupRequests() { - MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); - return &switchSetupRequests[0]; -} - -SwitchSetupRequest * ServerInterface::getSwitchSetupRequests(int index) { - MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); - return switchSetupRequests[index]; -} - -void ServerInterface::setSwitchSetupRequests(int index,SwitchSetupRequest *ptr) { - MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); - switchSetupRequests[index] = ptr; -} - -int ServerInterface::isValidClientType(uint32 clientIp) { - int result = 0; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - if(slots[index] != NULL) { - Socket *socket = slots[index]->getSocket(); - if(socket != NULL) { - uint32 slotIp = socket->getConnectedIPAddress(socket->getIpAddress()); - if(slotIp == clientIp) { - result = 1; - break; + if (publishToMasterserverThread != NULL) { + time_t elapsed = time(NULL); + publishToMasterserverThread->signalQuit(); + for (; publishToMasterserverThread->canShutdown(false) == false && + difftime((long int) time(NULL), elapsed) <= 15;) { + //sleep(150); + } + if (publishToMasterserverThread->canShutdown(true)) { + delete publishToMasterserverThread; + publishToMasterserverThread = NULL; } } } - } - return result; -} -int ServerInterface::isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename) { - int result = 1; + ServerInterface::~ServerInterface() { + //printf("===> Destructor for ServerInterface\n"); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); - if( username != NULL && - strlen(username) > 0 && - filename != NULL && - strlen(filename) > 0) { - string user = username; - string file = filename; + masterController.clearSlaves(true); + exitServer = true; + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + if (slots[index] != NULL) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + delete slots[index]; + slots[index] = NULL; + } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d username [%s] file [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,username,filename); - - if(StartsWith(user,"tilesets") == true && EndsWith(file,"7z") == false) { - if(Config::getInstance().getBool("DisableFTPServerXferUncompressedTilesets","false") == true) { - result = 0; + if (switchSetupRequests[index] != NULL) { + delete switchSetupRequests[index]; + switchSetupRequests[index] = NULL; + } } - else { - char szIP[100] = ""; - Ip::Inet_NtoA(clientIp,szIP); - string clientIP = szIP; - std::vector serverList = Socket::getLocalIPAddressList(); - result = 0; - for(unsigned int index = 0; index < serverList.size(); ++index) { - string serverIP = serverList[index]; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + close(); + shutdownFTPServer(); + shutdownMasterserverPublishThread(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d clientIP [%s] serverIP [%s] %d / %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientIP.c_str(),serverIP.c_str(),index,serverList.size()); + lastMasterserverHeartbeatTime = 0; + if (needToRepublishToMasterserver == true) { + simpleTask(NULL, NULL); + } - vector clientTokens; - Tokenize(clientIP,clientTokens,"."); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + delete slotAccessorMutexes[index]; + slotAccessorMutexes[index] = NULL; + } - vector serverTokens; - Tokenize(serverIP,serverTokens,"."); + delete textMessageQueueThreadAccessor; + textMessageQueueThreadAccessor = NULL; - if(clientTokens.size() == 4 && serverTokens.size() == 4) { - if( clientTokens[0] == serverTokens[0] || - clientTokens[1] == serverTokens[1] || - clientTokens[2] == serverTokens[2]) { + delete broadcastMessageQueueThreadAccessor; + broadcastMessageQueueThreadAccessor = NULL; + + delete inBroadcastMessageThreadAccessor; + inBroadcastMessageThreadAccessor = NULL; + + delete serverSynchAccessor; + serverSynchAccessor = NULL; + + delete masterServerThreadAccessor; + masterServerThreadAccessor = NULL; + + delete serverSocketAdmin; + serverSocketAdmin = NULL; + + for (int index = 0; index < (int) broadcastMessageQueue.size(); ++index) { + pair &item = broadcastMessageQueue[index]; + if (item.first != NULL) { + delete item.first; + } + item.first = NULL; + } + broadcastMessageQueue.clear(); + + delete switchSetupRequestsSynchAccessor; + switchSetupRequestsSynchAccessor = NULL; + + delete gameStatsThreadAccessor; + gameStatsThreadAccessor = NULL; + + delete gameStats; + gameStats = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + SwitchSetupRequest ** ServerInterface::getSwitchSetupRequests() { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor, CODE_AT_LINE); + return &switchSetupRequests[0]; + } + + SwitchSetupRequest * ServerInterface::getSwitchSetupRequests(int index) { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor, CODE_AT_LINE); + return switchSetupRequests[index]; + } + + void ServerInterface::setSwitchSetupRequests(int index, SwitchSetupRequest *ptr) { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor, CODE_AT_LINE); + switchSetupRequests[index] = ptr; + } + + int ServerInterface::isValidClientType(uint32 clientIp) { + int result = 0; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + if (slots[index] != NULL) { + Socket *socket = slots[index]->getSocket(); + if (socket != NULL) { + uint32 slotIp = socket->getConnectedIPAddress(socket->getIpAddress()); + if (slotIp == clientIp) { result = 1; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d clientIP [%s] IS NOT BLOCKED\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientIP.c_str()); - break; } } } } + return result; } - } - return result; -} -void ServerInterface::addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp) { - FTPServerThread::addClientToServerIPAddress(clientIp, ServerIp); -} + int ServerInterface::isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename) { + int result = 1; -void ServerInterface::addSlot(int playerIndex) { - //printf("Adding slot for playerIndex = %d, serverSocket.isPortBound() = %d\n",playerIndex,serverSocket.isPortBound()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (username != NULL && + strlen(username) > 0 && + filename != NULL && + strlen(filename) > 0) { + string user = username; + string file = filename; - if(playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] playerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,playerIndex); - throw megaglest_runtime_error(szBuf); - } - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - if(serverSocketAdmin != NULL && serverSocketAdmin->isSocketValid() == false) { - serverSocketAdmin->listen(5); - } - if(serverSocket.isPortBound() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - serverSocket.bind(serverSocket.getBindPort()); - } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d username [%s] file [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, username, filename); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (StartsWith(user, "tilesets") == true && EndsWith(file, "7z") == false) { + if (Config::getInstance().getBool("DisableFTPServerXferUncompressedTilesets", "false") == true) { + result = 0; + } else { + char szIP[100] = ""; + Ip::Inet_NtoA(clientIp, szIP); + string clientIP = szIP; + std::vector serverList = Socket::getLocalIPAddressList(); - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[playerIndex],CODE_AT_LINE_X(playerIndex)); + result = 0; + for (unsigned int index = 0; index < serverList.size(); ++index) { + string serverIP = serverList[index]; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d clientIP [%s] serverIP [%s] %d / %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientIP.c_str(), serverIP.c_str(), index, serverList.size()); - ConnectionSlot *slot = slots[playerIndex]; - if(slot != NULL) { - slots[playerIndex] = NULL; - } - slots[playerIndex] = new ConnectionSlot(this, playerIndex); + vector clientTokens; + Tokenize(clientIP, clientTokens, "."); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + vector serverTokens; + Tokenize(serverIP, serverTokens, "."); - safeMutexSlot.ReleaseLock(); - delete slot; + if (clientTokens.size() == 4 && serverTokens.size() == 4) { + if (clientTokens[0] == serverTokens[0] || + clientTokens[1] == serverTokens[1] || + clientTokens[2] == serverTokens[2]) { + result = 1; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d clientIP [%s] IS NOT BLOCKED\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientIP.c_str()); - safeMutex.ReleaseLock(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - updateListen(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::removeSlot(int playerIndex, int lockedSlotIndex) { - //printf("Removing slot for playerIndex = %d, serverSocket.isPortBound() = %d\n",playerIndex,serverSocket.isPortBound()); - - if(playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] playerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,playerIndex); - throw megaglest_runtime_error(szBuf); - } - - Lang &lang= Lang::getInstance(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - MutexSafeWrapper safeMutexSlot(NULL,CODE_AT_LINE_X(playerIndex)); - if(playerIndex != lockedSlotIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - safeMutexSlot.setMutex(slotAccessorMutexes[playerIndex],CODE_AT_LINE_X(playerIndex)); - } - - vector msgList; - ConnectionSlot *slot = slots[playerIndex]; - bool notifyDisconnect = false; - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - if(slot != NULL) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - if(slot->getLastReceiveCommandListTime() > 0) { - char szBuf[4096] = ""; - - for(unsigned int index = 0; index < languageList.size(); ++index) { - string msgTemplate = "Player %s, disconnected from the game."; - if(lang.hasString("PlayerDisconnected",languageList[index]) == true) { - msgTemplate = lang.getString("PlayerDisconnected",languageList[index]); - } -#ifdef WIN32 - _snprintf(szBuf,4095,msgTemplate.c_str(),slot->getName().c_str()); -#else - snprintf(szBuf,4095,msgTemplate.c_str(),slot->getName().c_str()); -#endif - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,szBuf); - - msgList.push_back(szBuf); - } - - notifyDisconnect = true; - } - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - slots[playerIndex]= NULL; - safeMutexSlot.ReleaseLock(); - safeMutex.ReleaseLock(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - if(slot != NULL) slot->close(); - delete slot; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - updateListen(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - if(notifyDisconnect == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); - - for(unsigned int index = 0; index < languageList.size(); ++index) { - bool localEcho = lang.isLanguageLocal(languageList[index]); - queueTextMessage(msgList[index],-1, localEcho, languageList[index]); - } - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex,lockedSlotIndex); -} - -bool ServerInterface::switchSlot(int fromPlayerIndex, int toPlayerIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - bool result = false; - - //printf("#1 Server is switching slots\n"); - - if(fromPlayerIndex < 0 || fromPlayerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] fromPlayerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,fromPlayerIndex); - throw megaglest_runtime_error(szBuf); - } - - if(toPlayerIndex < 0 || toPlayerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] toPlayerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,toPlayerIndex); - throw megaglest_runtime_error(szBuf); - } - - if(fromPlayerIndex == toPlayerIndex) { - return false; - } - - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[fromPlayerIndex],CODE_AT_LINE_X(fromPlayerIndex)); - MutexSafeWrapper safeMutexSlot2(slotAccessorMutexes[toPlayerIndex],CODE_AT_LINE_X(toPlayerIndex)); - - //printf("#1a Server is switching slots\n"); - - if(slots[toPlayerIndex] != NULL && - slots[toPlayerIndex]->hasValidSocketId() == false) { - - //printf("#2 Server is switching slots\n"); - - slots[fromPlayerIndex]->setPlayerIndex(toPlayerIndex); - slots[toPlayerIndex]->setPlayerIndex(fromPlayerIndex); - ConnectionSlot *tmp = slots[toPlayerIndex]; - slots[toPlayerIndex] = slots[fromPlayerIndex]; - slots[fromPlayerIndex] = tmp; - - safeMutex.ReleaseLock(); - - PlayerIndexMessage playerIndexMessage(toPlayerIndex); - slots[toPlayerIndex]->sendMessage(&playerIndexMessage); - - //slots[fromPlayerIndex]->resetJoinGameInProgressFlags(); - //slots[toPlayerIndex]->setJoinGameInProgressFlags(); - - safeMutexSlot.ReleaseLock(); - safeMutexSlot2.ReleaseLock(); - result = true; - updateListen(); - } - else { - //printf("#3 Server is switching slots aborted, is slot already connected?\n"); - - safeMutexSlot.ReleaseLock(); - safeMutexSlot2.ReleaseLock(); - safeMutex.ReleaseLock(); - } - //printf("#4 Server is switching slots\n"); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return result; -} - -Mutex *ServerInterface::getSlotMutex(int playerIndex) { - return slotAccessorMutexes[playerIndex]; -} - -ConnectionSlot *ServerInterface::getSlot(int playerIndex, bool lockMutex) { - if(playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] playerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,playerIndex); - throw megaglest_runtime_error(szBuf); - } - - MutexSafeWrapper safeMutexSlot((lockMutex == true ? slotAccessorMutexes[playerIndex] : NULL),CODE_AT_LINE_X(playerIndex)); - ConnectionSlot *result = slots[playerIndex]; - return result; -} - -bool ServerInterface::isClientConnected(int playerIndex) { - if(playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { - char szBuf[8096]=""; - snprintf(szBuf,8096,"In [%s::%s %d] playerIndex is invalid = %d",extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(),__FUNCTION__,__LINE__,playerIndex); - throw megaglest_runtime_error(szBuf); - } - - bool result = false; - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[playerIndex],CODE_AT_LINE_X(playerIndex)); - if(slots[playerIndex] != NULL && slots[playerIndex]->isConnected() == true) { - result = true; - } - return result; -} - -bool ServerInterface::hasClientConnection() { - bool result = false; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - if(isClientConnected(index) == true) { - result = true; - break; - } - } - return result; -} - -int ServerInterface::getSlotCount() { - int slotCount = 0; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - if(slots[index] != NULL) { - ++slotCount; - } - } - return slotCount; -} - -int ServerInterface::getConnectedSlotCount(bool authenticated) { - int connectedSlotCount = 0; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - if(slots[index] != NULL && slots[index]->isConnected() == true) { - if(authenticated == false || - (authenticated == true && slots[index]->getConnectHasHandshaked() == true)) { - ++connectedSlotCount; - } - } - } - return connectedSlotCount; -} - -int64 ServerInterface::getNextEventId() { - nextEventId++; - if(nextEventId > INT_MAX) { - nextEventId = 1; - } - return nextEventId; -} - -std::pair ServerInterface::clientLagCheck(ConnectionSlot *connectionSlot, bool skipNetworkBroadCast) { - std::pair clientLagExceededOrWarned = std::make_pair(false, false); - static bool alreadyInLagCheck = false; - - if(alreadyInLagCheck == true || - (connectionSlot != NULL && (connectionSlot->getSkipLagCheck() == true || - connectionSlot->getConnectHasHandshaked() == false))) { - return clientLagExceededOrWarned; - } - - try { - alreadyInLagCheck = true; - - if((gameStartTime > 0 && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD) && - (resumeGameStartTime == 0 || - (resumeGameStartTime > 0 && - difftime((long int)time(NULL),resumeGameStartTime) >= LAG_CHECK_GRACE_PERIOD))) { - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - - double clientLag = this->getCurrentFrameCount() - connectionSlot->getCurrentFrameCount(); - double clientLagCount = (gameSettings.getNetworkFramePeriod() > 0 ? (clientLag / gameSettings.getNetworkFramePeriod()) : 0); - connectionSlot->setCurrentLagCount(clientLagCount); - - double clientLagTime = difftime((long int)time(NULL),connectionSlot->getLastReceiveCommandListTime()); - - if(this->getCurrentFrameCount() > 0) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] playerIndex = %d, clientLag = %f, clientLagCount = %f, this->getCurrentFrameCount() = %d, connectionSlot->getCurrentFrameCount() = %d, clientLagTime = %f\n", - extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__, - connectionSlot->getPlayerIndex(), - clientLag,clientLagCount, - this->getCurrentFrameCount(), - connectionSlot->getCurrentFrameCount(), - clientLagTime); - } - - // TEST LAG Error and warnings!!! - //clientLagCount = maxFrameCountLagAllowed + 1; - //clientLagTime = maxClientLagTimeAllowed + 1; - /* - if(difftime(time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD + 5) { - clientLagTime = maxClientLagTimeAllowed + 1; - } - else if(difftime(time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD) { - clientLagTime = (maxClientLagTimeAllowed * warnFrameCountLagPercent) + 1; - } - */ - // END test - - - //printf("skipNetworkBroadCast [%d] clientLagCount [%f][%f][%f] clientLagTime [%f][%f][%f]\n",skipNetworkBroadCast,clientLagCount,(maxFrameCountLagAllowed * warnFrameCountLagPercent),maxFrameCountLagAllowed,clientLagTime,(maxClientLagTimeAllowed * warnFrameCountLagPercent),maxClientLagTimeAllowed); - - // New lag check - if((maxFrameCountLagAllowed > 0 && clientLagCount > maxFrameCountLagAllowed) || - (maxClientLagTimeAllowed > 0 && clientLagTime > maxClientLagTimeAllowed) || - (maxFrameCountLagAllowedEver > 0 && clientLagCount > maxFrameCountLagAllowedEver) || - ( maxClientLagTimeAllowedEver > 0 && clientLagTime > maxClientLagTimeAllowedEver)) { - - clientLagExceededOrWarned.first = true; - //printf("#1 Client Warned\n"); - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int index = 0; index < languageList.size(); ++index) { - char szBuf[4096]=""; - - string msgTemplate = "DROPPING %s, exceeded max allowed LAG count of %f [time = %f], clientLag = %f [%f], disconnecting client."; - if(lang.hasString("ClientLagDropping") == true) { - msgTemplate = lang.getString("ClientLagDropping",languageList[index]); - } - if(gameSettings.getNetworkPauseGameForLaggedClients() == true && - ((maxFrameCountLagAllowedEver <= 0 || clientLagCount <= maxFrameCountLagAllowedEver) && - (maxClientLagTimeAllowedEver <= 0 || clientLagTime <= maxClientLagTimeAllowedEver))) { - msgTemplate = "PAUSING GAME TEMPORARILY for %s, exceeded max allowed LAG count of %f [time = %f], clientLag = %f [%f], waiting for client to catch up..."; - if(lang.hasString("ClientLagPausing") == true) { - msgTemplate = lang.getString("ClientLagPausing",languageList[index]); + break; + } } } + } + } + } + return result; + } + + void ServerInterface::addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp) { + FTPServerThread::addClientToServerIPAddress(clientIp, ServerIp); + } + + void ServerInterface::addSlot(int playerIndex) { + //printf("Adding slot for playerIndex = %d, serverSocket.isPortBound() = %d\n",playerIndex,serverSocket.isPortBound()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] playerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, playerIndex); + throw megaglest_runtime_error(szBuf); + } + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + if (serverSocketAdmin != NULL && serverSocketAdmin->isSocketValid() == false) { + serverSocketAdmin->listen(5); + } + if (serverSocket.isPortBound() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + serverSocket.bind(serverSocket.getBindPort()); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[playerIndex], CODE_AT_LINE_X(playerIndex)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + ConnectionSlot *slot = slots[playerIndex]; + if (slot != NULL) { + slots[playerIndex] = NULL; + } + slots[playerIndex] = new ConnectionSlot(this, playerIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + safeMutexSlot.ReleaseLock(); + delete slot; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + updateListen(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::removeSlot(int playerIndex, int lockedSlotIndex) { + //printf("Removing slot for playerIndex = %d, serverSocket.isPortBound() = %d\n",playerIndex,serverSocket.isPortBound()); + + if (playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] playerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, playerIndex); + throw megaglest_runtime_error(szBuf); + } + + Lang &lang = Lang::getInstance(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); + + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + MutexSafeWrapper safeMutexSlot(NULL, CODE_AT_LINE_X(playerIndex)); + if (playerIndex != lockedSlotIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); + safeMutexSlot.setMutex(slotAccessorMutexes[playerIndex], CODE_AT_LINE_X(playerIndex)); + } + + vector msgList; + ConnectionSlot *slot = slots[playerIndex]; + bool notifyDisconnect = false; + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + if (slot != NULL) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); + + if (slot->getLastReceiveCommandListTime() > 0) { + char szBuf[4096] = ""; + + for (unsigned int index = 0; index < languageList.size(); ++index) { + string msgTemplate = "Player %s, disconnected from the game."; + if (lang.hasString("PlayerDisconnected", languageList[index]) == true) { + msgTemplate = lang.getString("PlayerDisconnected", languageList[index]); + } #ifdef WIN32 - _snprintf(szBuf,4095,msgTemplate.c_str(),connectionSlot->getName().c_str() ,maxFrameCountLagAllowed,maxClientLagTimeAllowed,clientLagCount,clientLagTime); + _snprintf(szBuf, 4095, msgTemplate.c_str(), slot->getName().c_str()); #else - snprintf(szBuf,4095,msgTemplate.c_str(),connectionSlot->getName().c_str(),maxFrameCountLagAllowed,maxClientLagTimeAllowed,clientLagCount,clientLagTime); + snprintf(szBuf, 4095, msgTemplate.c_str(), slot->getName().c_str()); #endif - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,szBuf); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, szBuf); - if(skipNetworkBroadCast == false) { - string sMsg = szBuf; - bool echoLocal = lang.isLanguageLocal(languageList[index]); - sendTextMessage(sMsg,-1, echoLocal, languageList[index], connectionSlot->getPlayerIndex()); - } - } - - if((gameSettings.getNetworkPauseGameForLaggedClients() == false || - (maxFrameCountLagAllowedEver > 0 && clientLagCount > maxFrameCountLagAllowedEver) || - (maxClientLagTimeAllowedEver > 0 && clientLagTime > maxClientLagTimeAllowedEver) && - connectionSlot->getGraceLagCtr() > GRACE_LAG_CTR_LIMIT)) { - - //printf("Closing connection slot lagged out!\n"); - connectionSlot->close(); - // not needed now, but will be needed when in-game joins and rejoins - // are used - connectionSlot->resetGraceLagCtr(); + msgList.push_back(szBuf); } - } - // New lag check warning - else if((maxFrameCountLagAllowed > 0 && warnFrameCountLagPercent > 0 && - clientLagCount > (maxFrameCountLagAllowed * warnFrameCountLagPercent)) || - (maxClientLagTimeAllowed > 0 && warnFrameCountLagPercent > 0 && - clientLagTime > (maxClientLagTimeAllowed * warnFrameCountLagPercent)) ) { - - clientLagExceededOrWarned.second = true; - //printf("#2 Client Warned\n"); - - if(connectionSlot->getLagCountWarning() == false) { - connectionSlot->setLagCountWarning(true); - - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int index = 0; index < languageList.size(); ++index) { - char szBuf[4096]=""; - - string msgTemplate = "LAG WARNING for %s, may exceed max allowed LAG count of %f [time = %f], clientLag = %f [%f], WARNING..."; - if(lang.hasString("ClientLagWarning") == true) { - msgTemplate = lang.getString("ClientLagWarning",languageList[index]); - } - - #ifdef WIN32 - _snprintf(szBuf,4095,msgTemplate.c_str(),connectionSlot->getName().c_str(),maxFrameCountLagAllowed,maxClientLagTimeAllowed,clientLagCount,clientLagTime); - #else - snprintf(szBuf,4095,msgTemplate.c_str(),connectionSlot->getName().c_str(),maxFrameCountLagAllowed,maxClientLagTimeAllowed,clientLagCount,clientLagTime); - #endif - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] %s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,szBuf); - - if(skipNetworkBroadCast == false) { - string sMsg = szBuf; - bool echoLocal = lang.isLanguageLocal(languageList[index]); - sendTextMessage(sMsg,-1, echoLocal, languageList[index], connectionSlot->getPlayerIndex()); - } - } - } - } - else if(connectionSlot->getLagCountWarning() == true) { - connectionSlot->setLagCountWarning(false); + notifyDisconnect = true; } } - } - } - catch(const exception &ex) { - alreadyInLagCheck = false; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - throw megaglest_runtime_error(ex.what()); - } + slots[playerIndex] = NULL; + safeMutexSlot.ReleaseLock(); + safeMutex.ReleaseLock(); - alreadyInLagCheck = false; - return clientLagExceededOrWarned; -} + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); -void ServerInterface::updateSocketTriggeredList(std::map & socketTriggeredList) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot = slots[index]; - if(connectionSlot != NULL) { - PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); - if(Socket::isSocketValid(&clientSocket) == true) { - socketTriggeredList[clientSocket] = false; - } - } - } -} + if (slot != NULL) slot->close(); + delete slot; -void ServerInterface::validateConnectedClients() { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot = slots[index]; - if(connectionSlot != NULL) { - connectionSlot->validateConnection(); - } - } -} + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); -bool ServerInterface::signalClientReceiveCommands(ConnectionSlot *connectionSlot, - int slotIndex, bool socketTriggered, ConnectionSlotEvent & event) { - bool slotSignalled = false; + updateListen(); - event.eventType = eReceiveSocketData; - event.networkMessage = NULL; - event.connectionSlot = connectionSlot; - event.socketTriggered = socketTriggered; - event.triggerId = slotIndex; - event.eventId = getNextEventId(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); - if(connectionSlot != NULL) { - if(socketTriggered == true || connectionSlot->isConnected() == false) { - connectionSlot->signalUpdate(&event); - slotSignalled = true; - } - } - return slotSignalled; -} + if (notifyDisconnect == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); -void ServerInterface::signalClientsToRecieveData(std::map &socketTriggeredList, - std::map &eventList, - std::map & mapSlotSignalledList) { - //printf("====================================In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //printf("Signal clients get new data\n"); - const bool newThreadManager = Config::getInstance().getBool("EnableNewThreadManager","false"); - if(newThreadManager == true) { - masterController.clearSlaves(true); - std::vector slaveThreadList; - for(int i= 0; exitServer == false && i < GameConstants::maxPlayers; ++i) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[i],CODE_AT_LINE_X(i)); - ConnectionSlot* connectionSlot = slots[i]; - - bool socketTriggered = false; - - if(connectionSlot != NULL) { - PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); - if(Socket::isSocketValid(&clientSocket)) { - socketTriggered = socketTriggeredList[clientSocket]; - } - else if(this->getGameHasBeenInitiated() == true && - this->getAllowInGameConnections() == true) { - socketTriggeredList[clientSocket] = true; - socketTriggered = socketTriggeredList[clientSocket]; + for (unsigned int index = 0; index < languageList.size(); ++index) { + bool localEcho = lang.isLanguageLocal(languageList[index]); + queueTextMessage(msgList[index], -1, localEcho, languageList[index]); } } - ConnectionSlotEvent &event = eventList[i]; - event.eventType = eReceiveSocketData; - event.networkMessage = NULL; - event.connectionSlot = connectionSlot; - event.socketTriggered = socketTriggered; - event.triggerId = i; - event.eventId = getNextEventId(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, playerIndex, lockedSlotIndex); + } - if(connectionSlot != NULL) { - if(socketTriggered == true || connectionSlot->isConnected() == false) { - if(connectionSlot->getWorkerThread() != NULL) { - slaveThreadList.push_back(connectionSlot->getWorkerThread()); - mapSlotSignalledList[i] = true; + bool ServerInterface::switchSlot(int fromPlayerIndex, int toPlayerIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + bool result = false; + + //printf("#1 Server is switching slots\n"); + + if (fromPlayerIndex < 0 || fromPlayerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] fromPlayerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, fromPlayerIndex); + throw megaglest_runtime_error(szBuf); + } + + if (toPlayerIndex < 0 || toPlayerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] toPlayerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, toPlayerIndex); + throw megaglest_runtime_error(szBuf); + } + + if (fromPlayerIndex == toPlayerIndex) { + return false; + } + + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[fromPlayerIndex], CODE_AT_LINE_X(fromPlayerIndex)); + MutexSafeWrapper safeMutexSlot2(slotAccessorMutexes[toPlayerIndex], CODE_AT_LINE_X(toPlayerIndex)); + + //printf("#1a Server is switching slots\n"); + + if (slots[toPlayerIndex] != NULL && + slots[toPlayerIndex]->hasValidSocketId() == false) { + + //printf("#2 Server is switching slots\n"); + + slots[fromPlayerIndex]->setPlayerIndex(toPlayerIndex); + slots[toPlayerIndex]->setPlayerIndex(fromPlayerIndex); + ConnectionSlot *tmp = slots[toPlayerIndex]; + slots[toPlayerIndex] = slots[fromPlayerIndex]; + slots[fromPlayerIndex] = tmp; + + safeMutex.ReleaseLock(); + + PlayerIndexMessage playerIndexMessage(toPlayerIndex); + slots[toPlayerIndex]->sendMessage(&playerIndexMessage); + + //slots[fromPlayerIndex]->resetJoinGameInProgressFlags(); + //slots[toPlayerIndex]->setJoinGameInProgressFlags(); + + safeMutexSlot.ReleaseLock(); + safeMutexSlot2.ReleaseLock(); + result = true; + updateListen(); + } else { + //printf("#3 Server is switching slots aborted, is slot already connected?\n"); + + safeMutexSlot.ReleaseLock(); + safeMutexSlot2.ReleaseLock(); + safeMutex.ReleaseLock(); + } + //printf("#4 Server is switching slots\n"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return result; + } + + Mutex *ServerInterface::getSlotMutex(int playerIndex) { + return slotAccessorMutexes[playerIndex]; + } + + ConnectionSlot *ServerInterface::getSlot(int playerIndex, bool lockMutex) { + if (playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] playerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, playerIndex); + throw megaglest_runtime_error(szBuf); + } + + MutexSafeWrapper safeMutexSlot((lockMutex == true ? slotAccessorMutexes[playerIndex] : NULL), CODE_AT_LINE_X(playerIndex)); + ConnectionSlot *result = slots[playerIndex]; + return result; + } + + bool ServerInterface::isClientConnected(int playerIndex) { + if (playerIndex < 0 || playerIndex >= GameConstants::maxPlayers) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] playerIndex is invalid = %d", extractFileFromDirectoryPath(extractFileFromDirectoryPath(__FILE__).c_str()).c_str(), __FUNCTION__, __LINE__, playerIndex); + throw megaglest_runtime_error(szBuf); + } + + bool result = false; + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[playerIndex], CODE_AT_LINE_X(playerIndex)); + if (slots[playerIndex] != NULL && slots[playerIndex]->isConnected() == true) { + result = true; + } + return result; + } + + bool ServerInterface::hasClientConnection() { + bool result = false; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + if (isClientConnected(index) == true) { + result = true; + break; + } + } + return result; + } + + int ServerInterface::getSlotCount() { + int slotCount = 0; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + if (slots[index] != NULL) { + ++slotCount; + } + } + return slotCount; + } + + int ServerInterface::getConnectedSlotCount(bool authenticated) { + int connectedSlotCount = 0; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + if (slots[index] != NULL && slots[index]->isConnected() == true) { + if (authenticated == false || + (authenticated == true && slots[index]->getConnectHasHandshaked() == true)) { + ++connectedSlotCount; } } } + return connectedSlotCount; } - masterController.setSlaves(slaveThreadList); - masterController.signalSlaves(&eventList); - } - else { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot = slots[index]; - if(connectionSlot != NULL) { - bool socketTriggered = false; - PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); - if(Socket::isSocketValid(&clientSocket)) { - socketTriggered = socketTriggeredList[clientSocket]; - } - - ConnectionSlotEvent &event = eventList[index]; - bool socketSignalled = signalClientReceiveCommands(connectionSlot,index,socketTriggered,event); - if(connectionSlot != NULL && socketTriggered == true) { - mapSlotSignalledList[index] = socketSignalled; - } + int64 ServerInterface::getNextEventId() { + nextEventId++; + if (nextEventId > INT_MAX) { + nextEventId = 1; } + return nextEventId; } - } -} -void ServerInterface::checkForCompletedClientsUsingThreadManager( - std::map &mapSlotSignalledList, std::vector& errorMsgList) { + std::pair ServerInterface::clientLagCheck(ConnectionSlot *connectionSlot, bool skipNetworkBroadCast) { + std::pair clientLagExceededOrWarned = std::make_pair(false, false); + static bool alreadyInLagCheck = false; - masterController.waitTillSlavesTrigger(MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS); - masterController.clearSlaves(true); - - for (int i = 0; exitServer == false && i < GameConstants::maxPlayers; ++i) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[i],CODE_AT_LINE_X(i)); - - ConnectionSlot* connectionSlot = slots[i]; - if (connectionSlot != NULL && mapSlotSignalledList[i] == true) { + if (alreadyInLagCheck == true || + (connectionSlot != NULL && (connectionSlot->getSkipLagCheck() == true || + connectionSlot->getConnectHasHandshaked() == false))) { + return clientLagExceededOrWarned; + } try { - std::vector errorList = - connectionSlot->getThreadErrorList(); - // Collect any collected errors from threads - if (errorList.empty() == false) { - for (int iErrIdx = 0; iErrIdx < (int) errorList.size();++iErrIdx) { - string &sErr = errorList[iErrIdx]; + alreadyInLagCheck = true; - if (sErr != "") { - errorMsgList.push_back(sErr); + if ((gameStartTime > 0 && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD) && + (resumeGameStartTime == 0 || + (resumeGameStartTime > 0 && + difftime((long int) time(NULL), resumeGameStartTime) >= LAG_CHECK_GRACE_PERIOD))) { + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + + double clientLag = this->getCurrentFrameCount() - connectionSlot->getCurrentFrameCount(); + double clientLagCount = (gameSettings.getNetworkFramePeriod() > 0 ? (clientLag / gameSettings.getNetworkFramePeriod()) : 0); + connectionSlot->setCurrentLagCount(clientLagCount); + + double clientLagTime = difftime((long int) time(NULL), connectionSlot->getLastReceiveCommandListTime()); + + if (this->getCurrentFrameCount() > 0) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] playerIndex = %d, clientLag = %f, clientLagCount = %f, this->getCurrentFrameCount() = %d, connectionSlot->getCurrentFrameCount() = %d, clientLagTime = %f\n", + extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, + connectionSlot->getPlayerIndex(), + clientLag, clientLagCount, + this->getCurrentFrameCount(), + connectionSlot->getCurrentFrameCount(), + clientLagTime); + } + + // TEST LAG Error and warnings!!! + //clientLagCount = maxFrameCountLagAllowed + 1; + //clientLagTime = maxClientLagTimeAllowed + 1; + /* + if(difftime(time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD + 5) { + clientLagTime = maxClientLagTimeAllowed + 1; + } + else if(difftime(time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD) { + clientLagTime = (maxClientLagTimeAllowed * warnFrameCountLagPercent) + 1; + } + */ + // END test + + + //printf("skipNetworkBroadCast [%d] clientLagCount [%f][%f][%f] clientLagTime [%f][%f][%f]\n",skipNetworkBroadCast,clientLagCount,(maxFrameCountLagAllowed * warnFrameCountLagPercent),maxFrameCountLagAllowed,clientLagTime,(maxClientLagTimeAllowed * warnFrameCountLagPercent),maxClientLagTimeAllowed); + + // New lag check + if ((maxFrameCountLagAllowed > 0 && clientLagCount > maxFrameCountLagAllowed) || + (maxClientLagTimeAllowed > 0 && clientLagTime > maxClientLagTimeAllowed) || + (maxFrameCountLagAllowedEver > 0 && clientLagCount > maxFrameCountLagAllowedEver) || + (maxClientLagTimeAllowedEver > 0 && clientLagTime > maxClientLagTimeAllowedEver)) { + + clientLagExceededOrWarned.first = true; + //printf("#1 Client Warned\n"); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int index = 0; index < languageList.size(); ++index) { + char szBuf[4096] = ""; + + string msgTemplate = "DROPPING %s, exceeded max allowed LAG count of %f [time = %f], clientLag = %f [%f], disconnecting client."; + if (lang.hasString("ClientLagDropping") == true) { + msgTemplate = lang.getString("ClientLagDropping", languageList[index]); + } + if (gameSettings.getNetworkPauseGameForLaggedClients() == true && + ((maxFrameCountLagAllowedEver <= 0 || clientLagCount <= maxFrameCountLagAllowedEver) && + (maxClientLagTimeAllowedEver <= 0 || clientLagTime <= maxClientLagTimeAllowedEver))) { + msgTemplate = "PAUSING GAME TEMPORARILY for %s, exceeded max allowed LAG count of %f [time = %f], clientLag = %f [%f], waiting for client to catch up..."; + if (lang.hasString("ClientLagPausing") == true) { + msgTemplate = lang.getString("ClientLagPausing", languageList[index]); + } + } +#ifdef WIN32 + _snprintf(szBuf, 4095, msgTemplate.c_str(), connectionSlot->getName().c_str(), maxFrameCountLagAllowed, maxClientLagTimeAllowed, clientLagCount, clientLagTime); +#else + snprintf(szBuf, 4095, msgTemplate.c_str(), connectionSlot->getName().c_str(), maxFrameCountLagAllowed, maxClientLagTimeAllowed, clientLagCount, clientLagTime); +#endif + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, szBuf); + + if (skipNetworkBroadCast == false) { + string sMsg = szBuf; + bool echoLocal = lang.isLanguageLocal(languageList[index]); + sendTextMessage(sMsg, -1, echoLocal, languageList[index], connectionSlot->getPlayerIndex()); + } + } + + if ((gameSettings.getNetworkPauseGameForLaggedClients() == false || + (maxFrameCountLagAllowedEver > 0 && clientLagCount > maxFrameCountLagAllowedEver) || + (maxClientLagTimeAllowedEver > 0 && clientLagTime > maxClientLagTimeAllowedEver) && + connectionSlot->getGraceLagCtr() > GRACE_LAG_CTR_LIMIT)) { + + //printf("Closing connection slot lagged out!\n"); + connectionSlot->close(); + // not needed now, but will be needed when in-game joins and rejoins + // are used + connectionSlot->resetGraceLagCtr(); + } + + } + // New lag check warning + else if ((maxFrameCountLagAllowed > 0 && warnFrameCountLagPercent > 0 && + clientLagCount > (maxFrameCountLagAllowed * warnFrameCountLagPercent)) || + (maxClientLagTimeAllowed > 0 && warnFrameCountLagPercent > 0 && + clientLagTime > (maxClientLagTimeAllowed * warnFrameCountLagPercent))) { + + clientLagExceededOrWarned.second = true; + //printf("#2 Client Warned\n"); + + if (connectionSlot->getLagCountWarning() == false) { + connectionSlot->setLagCountWarning(true); + + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int index = 0; index < languageList.size(); ++index) { + char szBuf[4096] = ""; + + string msgTemplate = "LAG WARNING for %s, may exceed max allowed LAG count of %f [time = %f], clientLag = %f [%f], WARNING..."; + if (lang.hasString("ClientLagWarning") == true) { + msgTemplate = lang.getString("ClientLagWarning", languageList[index]); + } + +#ifdef WIN32 + _snprintf(szBuf, 4095, msgTemplate.c_str(), connectionSlot->getName().c_str(), maxFrameCountLagAllowed, maxClientLagTimeAllowed, clientLagCount, clientLagTime); +#else + snprintf(szBuf, 4095, msgTemplate.c_str(), connectionSlot->getName().c_str(), maxFrameCountLagAllowed, maxClientLagTimeAllowed, clientLagCount, clientLagTime); +#endif + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] %s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, szBuf); + + if (skipNetworkBroadCast == false) { + string sMsg = szBuf; + bool echoLocal = lang.isLanguageLocal(languageList[index]); + sendTextMessage(sMsg, -1, echoLocal, languageList[index], connectionSlot->getPlayerIndex()); + } + } + } + } else if (connectionSlot->getLagCountWarning() == true) { + connectionSlot->setLagCountWarning(false); } } - connectionSlot->clearThreadErrorList(); } } catch (const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + alreadyInLagCheck = false; - errorMsgList.push_back(ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ERROR [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + throw megaglest_runtime_error(ex.what()); } + + alreadyInLagCheck = false; + return clientLagExceededOrWarned; } - } -} -void ServerInterface::checkForCompletedClientsUsingLoop( - std::map& mapSlotSignalledList, std::vector &errorMsgList, - std::map &eventList) { - - //time_t waitForThreadElapsed = time(NULL); - Chrono waitForThreadElapsed(true); - - std::map slotsCompleted; - for (bool threadsDone = false; exitServer == false && threadsDone == false && - waitForThreadElapsed.getMillis() <= MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS;) { - - threadsDone = true; - // Examine all threads for completion of delegation - for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - - ConnectionSlot *connectionSlot = slots[index]; - if (connectionSlot != NULL && connectionSlot->isConnected() == true && - mapSlotSignalledList[index] == true && - connectionSlot->getJoinGameInProgress() == false && - slotsCompleted.find(index) == slotsCompleted.end()) { - - try { - std::vector errorList = connectionSlot->getThreadErrorList(); - // Collect any collected errors from threads - if (errorList.empty() == false) { - - for (int iErrIdx = 0; iErrIdx < (int) errorList.size();++iErrIdx) { - - string &sErr = errorList[iErrIdx]; - if (sErr != "") { - errorMsgList.push_back(sErr); - } - } - connectionSlot->clearThreadErrorList(); - } - - // Not done waiting for data yet - bool updateFinished = (connectionSlot != NULL ? connectionSlot->updateCompleted(&eventList[index]) : true); - if (updateFinished == false) { - threadsDone = false; - break; - } - else { - slotsCompleted[index] = true; - } - } - catch (const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - - errorMsgList.push_back(ex.what()); - } - } - } - } -} - -std::string ServerInterface::getIpAddress(bool mutexLock) { - string result = serverSocket.getIpAddress(); - return result; -} - -void ServerInterface::setClientLagCallbackInterface(ClientLagCallbackInterface *intf) { - this->clientLagCallbackInterface = intf; -} - -bool ServerInterface::getClientsAutoPausedDueToLag() { - return this->clientsAutoPausedDueToLag; -} - -void ServerInterface::checkForCompletedClients(std::map & mapSlotSignalledList, - std::vector &errorMsgList, - std::map &eventList) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - const bool newThreadManager = Config::getInstance().getBool("EnableNewThreadManager","false"); - if(newThreadManager == true) { - checkForCompletedClientsUsingThreadManager(mapSlotSignalledList, errorMsgList); - } - else { - checkForCompletedClientsUsingLoop(mapSlotSignalledList, errorMsgList, eventList); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - -void ServerInterface::checkForAutoPauseForLaggingClient(int index,ConnectionSlot* connectionSlot) { - - if (gameSettings.getNetworkPauseGameForLaggedClients() == true && - this->clientsAutoPausedDueToLag == false) { - if (connectionSlot != NULL && connectionSlot->isConnected() == true) { - if (connectionSlot->getAutoPauseGameCountForLag() < MAX_CLIENT_PAUSE_FOR_LAG_COUNT) { - if (this->clientLagCallbackInterface != NULL) { - - if (this->clientsAutoPausedDueToLagTimer.isStarted() == false || - this->clientsAutoPausedDueToLagTimer.getMillis() >= MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS) { - - connectionSlot->incrementAutoPauseGameCountForLag(); - - this->clientsAutoPausedDueToLag = true; - if ((this->clientLagCallbackInterface->clientLagHandler(index, true) == false) && - connectionSlot->getGraceLagCtr() > GRACE_LAG_CTR_LIMIT) { - connectionSlot->close(); - connectionSlot->resetGraceLagCtr(); - } - else { - if (this->clientsAutoPausedDueToLagTimer.isStarted()== true) { - - this->clientsAutoPausedDueToLagTimer.reset(); - this->clientsAutoPausedDueToLagTimer.stop(); - } - this->clientsAutoPausedDueToLagTimer.start(); - } + void ServerInterface::updateSocketTriggeredList(std::map & socketTriggeredList) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + if (connectionSlot != NULL) { + PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); + if (Socket::isSocketValid(&clientSocket) == true) { + socketTriggeredList[clientSocket] = false; } } } } - } -} -void ServerInterface::checkForLaggingClients(std::map &mapSlotSignalledList, - std::map &eventList, - std::map &socketTriggeredList, - std::vector &errorMsgList) { - bool lastGlobalLagCheckTimeUpdate = false; - if(gameHasBeenInitiated == true) { - - //time_t waitForClientsElapsed = time(NULL); - Chrono waitForClientsElapsed(true); - //time_t waitForThreadElapsed = time(NULL); - Chrono waitForThreadElapsed(true); - std::map slotsCompleted; - std::map slotsWarnedList; - - for(bool threadsDone = false; - exitServer == false && threadsDone == false && - waitForThreadElapsed.getMillis() <= MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS;) { - - threadsDone = true; - // Examine all threads for completion of delegation - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - //printf("#1 Check lag for i: %d\n",i); - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); + void ServerInterface::validateConnectedClients() { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); ConnectionSlot* connectionSlot = slots[index]; + if (connectionSlot != NULL) { + connectionSlot->validateConnection(); + } + } + } - if(connectionSlot != NULL && connectionSlot->isConnected() == true && - connectionSlot->getSkipLagCheck() == false && - mapSlotSignalledList[index] == true && - slotsCompleted.find(index) == slotsCompleted.end()) { + bool ServerInterface::signalClientReceiveCommands(ConnectionSlot *connectionSlot, + int slotIndex, bool socketTriggered, ConnectionSlotEvent & event) { + bool slotSignalled = false; + + event.eventType = eReceiveSocketData; + event.networkMessage = NULL; + event.connectionSlot = connectionSlot; + event.socketTriggered = socketTriggered; + event.triggerId = slotIndex; + event.eventId = getNextEventId(); + + if (connectionSlot != NULL) { + if (socketTriggered == true || connectionSlot->isConnected() == false) { + connectionSlot->signalUpdate(&event); + slotSignalled = true; + } + } + return slotSignalled; + } + + void ServerInterface::signalClientsToRecieveData(std::map &socketTriggeredList, + std::map &eventList, + std::map & mapSlotSignalledList) { + //printf("====================================In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + //printf("Signal clients get new data\n"); + const bool newThreadManager = Config::getInstance().getBool("EnableNewThreadManager", "false"); + if (newThreadManager == true) { + masterController.clearSlaves(true); + std::vector slaveThreadList; + for (int i = 0; exitServer == false && i < GameConstants::maxPlayers; ++i) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[i], CODE_AT_LINE_X(i)); + ConnectionSlot* connectionSlot = slots[i]; + + bool socketTriggered = false; + + if (connectionSlot != NULL) { + PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); + if (Socket::isSocketValid(&clientSocket)) { + socketTriggered = socketTriggeredList[clientSocket]; + } else if (this->getGameHasBeenInitiated() == true && + this->getAllowInGameConnections() == true) { + socketTriggeredList[clientSocket] = true; + socketTriggered = socketTriggeredList[clientSocket]; + } + } + ConnectionSlotEvent &event = eventList[i]; + event.eventType = eReceiveSocketData; + event.networkMessage = NULL; + event.connectionSlot = connectionSlot; + event.socketTriggered = socketTriggered; + event.triggerId = i; + event.eventId = getNextEventId(); + + if (connectionSlot != NULL) { + if (socketTriggered == true || connectionSlot->isConnected() == false) { + if (connectionSlot->getWorkerThread() != NULL) { + slaveThreadList.push_back(connectionSlot->getWorkerThread()); + mapSlotSignalledList[i] = true; + } + } + } + } + masterController.setSlaves(slaveThreadList); + masterController.signalSlaves(&eventList); + } else { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + + if (connectionSlot != NULL) { + bool socketTriggered = false; + PLATFORM_SOCKET clientSocket = connectionSlot->getSocketId(); + if (Socket::isSocketValid(&clientSocket)) { + socketTriggered = socketTriggeredList[clientSocket]; + } + + ConnectionSlotEvent &event = eventList[index]; + bool socketSignalled = signalClientReceiveCommands(connectionSlot, index, socketTriggered, event); + if (connectionSlot != NULL && socketTriggered == true) { + mapSlotSignalledList[index] = socketSignalled; + } + } + } + } + } + + void ServerInterface::checkForCompletedClientsUsingThreadManager( + std::map &mapSlotSignalledList, std::vector& errorMsgList) { + + masterController.waitTillSlavesTrigger(MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS); + masterController.clearSlaves(true); + + for (int i = 0; exitServer == false && i < GameConstants::maxPlayers; ++i) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[i], CODE_AT_LINE_X(i)); + + ConnectionSlot* connectionSlot = slots[i]; + if (connectionSlot != NULL && mapSlotSignalledList[i] == true) { - //printf("#2 Check lag for i: %d playerindex: %d name [%s] socket: %d\n",i,connectionSlot->getPlayerIndex(),connectionSlot->getName().c_str(),connectionSlot->getSocketId()); try { - std::vector errorList = connectionSlot->getThreadErrorList(); - // Show any collected errors from threads - if(errorList.empty() == false) { - for(int iErrIdx = 0; iErrIdx < (int)errorList.size(); ++iErrIdx) { + std::vector errorList = + connectionSlot->getThreadErrorList(); + // Collect any collected errors from threads + if (errorList.empty() == false) { + for (int iErrIdx = 0; iErrIdx < (int) errorList.size(); ++iErrIdx) { string &sErr = errorList[iErrIdx]; - if(sErr != "") { + + if (sErr != "") { errorMsgList.push_back(sErr); } } connectionSlot->clearThreadErrorList(); } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); - // Not done waiting for data yet - bool updateFinished = (connectionSlot != NULL ? connectionSlot->updateCompleted(&eventList[index]) : true); - if(updateFinished == false) { - //printf("#2a Check lag for i: %d\n",i); - threadsDone = false; - break; - } - else { - // New lag check - std::pair clientLagExceededOrWarned = std::make_pair(false,false); - if( gameHasBeenInitiated == true && connectionSlot != NULL && - connectionSlot->isConnected() == true) { - clientLagExceededOrWarned = clientLagCheck(connectionSlot,slotsWarnedList[index]); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, gameSettings.getNetworkPauseGameForLaggedClients() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientLagExceededOrWarned.first,clientLagExceededOrWarned.second,gameSettings.getNetworkPauseGameForLaggedClients()); - - if(clientLagExceededOrWarned.first == true) { - slotsWarnedList[index] = true; - } - } - // If the client has exceeded lag and the server wants - // to pause while they catch up, re-trigger the - // client reader thread - if((clientLagExceededOrWarned.second == true && - gameSettings.getNetworkPauseGameForLaggedClients() == true)) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d, clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, waitForClientsElapsed.getMillis() = %d, MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientLagExceededOrWarned.first,clientLagExceededOrWarned.second,(int)waitForClientsElapsed.getMillis(),MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS); - - checkForAutoPauseForLaggingClient(index, connectionSlot); - - slotsCompleted[index] = true; - } - else { - slotsCompleted[index] = true; - } - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); errorMsgList.push_back(ex.what()); } } + } + } - //printf("#3 Check lag for i: %d\n",i); - if(connectionSlot != NULL && - connectionSlot->isConnected() == true && - connectionSlot->getSkipLagCheck() == false) { - //printf("#4 Check lag for i: %d\n",i); + void ServerInterface::checkForCompletedClientsUsingLoop( + std::map& mapSlotSignalledList, std::vector &errorMsgList, + std::map &eventList) { + + //time_t waitForThreadElapsed = time(NULL); + Chrono waitForThreadElapsed(true); + + std::map slotsCompleted; + for (bool threadsDone = false; exitServer == false && threadsDone == false && + waitForThreadElapsed.getMillis() <= MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS;) { + + threadsDone = true; + // Examine all threads for completion of delegation + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + + ConnectionSlot *connectionSlot = slots[index]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true && + mapSlotSignalledList[index] == true && + connectionSlot->getJoinGameInProgress() == false && + slotsCompleted.find(index) == slotsCompleted.end()) { + + try { + std::vector errorList = connectionSlot->getThreadErrorList(); + // Collect any collected errors from threads + if (errorList.empty() == false) { + + for (int iErrIdx = 0; iErrIdx < (int) errorList.size(); ++iErrIdx) { + + string &sErr = errorList[iErrIdx]; + if (sErr != "") { + errorMsgList.push_back(sErr); + } + } + connectionSlot->clearThreadErrorList(); + } + + // Not done waiting for data yet + bool updateFinished = (connectionSlot != NULL ? connectionSlot->updateCompleted(&eventList[index]) : true); + if (updateFinished == false) { + threadsDone = false; + break; + } else { + slotsCompleted[index] = true; + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + + errorMsgList.push_back(ex.what()); + } + } + } + } + } + + std::string ServerInterface::getIpAddress(bool mutexLock) { + string result = serverSocket.getIpAddress(); + return result; + } + + void ServerInterface::setClientLagCallbackInterface(ClientLagCallbackInterface *intf) { + this->clientLagCallbackInterface = intf; + } + + bool ServerInterface::getClientsAutoPausedDueToLag() { + return this->clientsAutoPausedDueToLag; + } + + void ServerInterface::checkForCompletedClients(std::map & mapSlotSignalledList, + std::vector &errorMsgList, + std::map &eventList) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + + const bool newThreadManager = Config::getInstance().getBool("EnableNewThreadManager", "false"); + if (newThreadManager == true) { + checkForCompletedClientsUsingThreadManager(mapSlotSignalledList, errorMsgList); + } else { + checkForCompletedClientsUsingLoop(mapSlotSignalledList, errorMsgList, eventList); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + void ServerInterface::checkForAutoPauseForLaggingClient(int index, ConnectionSlot* connectionSlot) { + + if (gameSettings.getNetworkPauseGameForLaggedClients() == true && + this->clientsAutoPausedDueToLag == false) { + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + if (connectionSlot->getAutoPauseGameCountForLag() < MAX_CLIENT_PAUSE_FOR_LAG_COUNT) { + if (this->clientLagCallbackInterface != NULL) { + + if (this->clientsAutoPausedDueToLagTimer.isStarted() == false || + this->clientsAutoPausedDueToLagTimer.getMillis() >= MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS) { + + connectionSlot->incrementAutoPauseGameCountForLag(); + + this->clientsAutoPausedDueToLag = true; + if ((this->clientLagCallbackInterface->clientLagHandler(index, true) == false) && + connectionSlot->getGraceLagCtr() > GRACE_LAG_CTR_LIMIT) { + connectionSlot->close(); + connectionSlot->resetGraceLagCtr(); + } else { + if (this->clientsAutoPausedDueToLagTimer.isStarted() == true) { + + this->clientsAutoPausedDueToLagTimer.reset(); + this->clientsAutoPausedDueToLagTimer.stop(); + } + this->clientsAutoPausedDueToLagTimer.start(); + } + } + } + } + } + } + } + + void ServerInterface::checkForLaggingClients(std::map &mapSlotSignalledList, + std::map &eventList, + std::map &socketTriggeredList, + std::vector &errorMsgList) { + bool lastGlobalLagCheckTimeUpdate = false; + if (gameHasBeenInitiated == true) { + + //time_t waitForClientsElapsed = time(NULL); + Chrono waitForClientsElapsed(true); + //time_t waitForThreadElapsed = time(NULL); + Chrono waitForThreadElapsed(true); + std::map slotsCompleted; + std::map slotsWarnedList; + + for (bool threadsDone = false; + exitServer == false && threadsDone == false && + waitForThreadElapsed.getMillis() <= MAX_SLOT_THREAD_WAIT_TIME_MILLISECONDS;) { + + threadsDone = true; + // Examine all threads for completion of delegation + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + //printf("#1 Check lag for i: %d\n",i); + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + + if (connectionSlot != NULL && connectionSlot->isConnected() == true && + connectionSlot->getSkipLagCheck() == false && + mapSlotSignalledList[index] == true && + slotsCompleted.find(index) == slotsCompleted.end()) { + + //printf("#2 Check lag for i: %d playerindex: %d name [%s] socket: %d\n",i,connectionSlot->getPlayerIndex(),connectionSlot->getName().c_str(),connectionSlot->getSocketId()); + try { + std::vector errorList = connectionSlot->getThreadErrorList(); + // Show any collected errors from threads + if (errorList.empty() == false) { + for (int iErrIdx = 0; iErrIdx < (int) errorList.size(); ++iErrIdx) { + string &sErr = errorList[iErrIdx]; + if (sErr != "") { + errorMsgList.push_back(sErr); + } + } + connectionSlot->clearThreadErrorList(); + } + + // Not done waiting for data yet + bool updateFinished = (connectionSlot != NULL ? connectionSlot->updateCompleted(&eventList[index]) : true); + if (updateFinished == false) { + //printf("#2a Check lag for i: %d\n",i); + threadsDone = false; + break; + } else { + // New lag check + std::pair clientLagExceededOrWarned = std::make_pair(false, false); + if (gameHasBeenInitiated == true && connectionSlot != NULL && + connectionSlot->isConnected() == true) { + clientLagExceededOrWarned = clientLagCheck(connectionSlot, slotsWarnedList[index]); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, gameSettings.getNetworkPauseGameForLaggedClients() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientLagExceededOrWarned.first, clientLagExceededOrWarned.second, gameSettings.getNetworkPauseGameForLaggedClients()); + + if (clientLagExceededOrWarned.first == true) { + slotsWarnedList[index] = true; + } + } + // If the client has exceeded lag and the server wants + // to pause while they catch up, re-trigger the + // client reader thread + if ((clientLagExceededOrWarned.second == true && + gameSettings.getNetworkPauseGameForLaggedClients() == true)) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d, clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, waitForClientsElapsed.getMillis() = %d, MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientLagExceededOrWarned.first, clientLagExceededOrWarned.second, (int) waitForClientsElapsed.getMillis(), MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS); + + checkForAutoPauseForLaggingClient(index, connectionSlot); + + slotsCompleted[index] = true; + } else { + slotsCompleted[index] = true; + } + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); + } + } + + //printf("#3 Check lag for i: %d\n",i); + if (connectionSlot != NULL && + connectionSlot->isConnected() == true && + connectionSlot->getSkipLagCheck() == false) { + //printf("#4 Check lag for i: %d\n",i); + + try { + if (gameHasBeenInitiated == true && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { + + //printf("\n\n\n^^^^^^^^^^^^^^ PART A\n\n\n"); + + // New lag check + std::pair clientLagExceededOrWarned = std::make_pair(false, false); + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + //printf("\n\n\n^^^^^^^^^^^^^^ PART B\n\n\n"); + + lastGlobalLagCheckTimeUpdate = true; + clientLagExceededOrWarned = clientLagCheck(connectionSlot, slotsWarnedList[index]); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, gameSettings.getNetworkPauseGameForLaggedClients() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientLagExceededOrWarned.first, clientLagExceededOrWarned.second, gameSettings.getNetworkPauseGameForLaggedClients()); + + if (clientLagExceededOrWarned.first == true) { + slotsWarnedList[index] = true; + } + + // If the client has exceeded lag and the server wants + // to pause while they catch up, re-trigger the + // client reader thread + if ((clientLagExceededOrWarned.second == true && + gameSettings.getNetworkPauseGameForLaggedClients() == true)) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d, clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, waitForClientsElapsed.getMillis() = %d, MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, clientLagExceededOrWarned.first, clientLagExceededOrWarned.second, (int) waitForClientsElapsed.getMillis(), MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS); + + checkForAutoPauseForLaggingClient(index, connectionSlot); + } + } + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); + } + } + + //printf("#5 Check lag for i: %d\n",i); + } + } + } + if (lastGlobalLagCheckTimeUpdate == true) { + lastGlobalLagCheckTime = time(NULL); + } + } + + void ServerInterface::executeNetworkCommandsFromClients() { + if (gameHasBeenInitiated == true) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + vector pendingList = connectionSlot->getPendingNetworkCommandList(true); + if (pendingList.empty() == false) { + for (int idx = 0; exitServer == false && idx < (int) pendingList.size(); ++idx) { + NetworkCommand &cmd = pendingList[idx]; + this->requestCommand(&cmd); + } + //printf("Executed: %d commands from slot: %d\n",pendingList.size(),index); + } + } + } + } + } + + void ServerInterface::dispatchPendingChatMessages(std::vector &errorMsgList) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + + if (connectionSlot != NULL && + connectionSlot->getChatTextList(false).empty() == false) { + try { + std::vector chatText = connectionSlot->getChatTextList(true); + for (int chatIdx = 0; + exitServer == false && slots[index] != NULL && + chatIdx < (int) chatText.size(); chatIdx++) { + + connectionSlot = slots[index]; + if (connectionSlot != NULL) { + ChatMsgInfo msg(chatText[chatIdx]); + this->addChatInfo(msg); + + string newChatText = msg.chatText.c_str(); + int newChatTeamIndex = msg.chatTeamIndex; + int newChatPlayerIndex = msg.chatPlayerIndex; + string newChatLanguage = msg.targetLanguage; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatTeamIndex = %d, newChatPlayerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, newChatText.c_str(), newChatTeamIndex, newChatPlayerIndex); + + if (newChatLanguage == "" || + newChatLanguage == connectionSlot->getNetworkPlayerLanguage()) { + + NetworkMessageText networkMessageText(newChatText.c_str(), newChatTeamIndex, newChatPlayerIndex, newChatLanguage); + broadcastMessage(&networkMessageText, connectionSlot->getPlayerIndex(), index); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, newChatText.c_str(), newChatTeamIndex); + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] index = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, index); + // Its possible that the slot is disconnected here + // so check the original pointer again + if (slots[index] != NULL) { + slots[index]->clearChatInfo(); + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); + } + } + } + } + + void ServerInterface::dispatchPendingMarkCellMessages(std::vector &errorMsgList) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + + if (connectionSlot != NULL && + connectionSlot->getMarkedCellList(false).empty() == false) { try { - if(gameHasBeenInitiated == true && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { + std::vector chatText = connectionSlot->getMarkedCellList(true); + for (int chatIdx = 0; + exitServer == false && slots[index] != NULL && + chatIdx < (int) chatText.size(); chatIdx++) { - //printf("\n\n\n^^^^^^^^^^^^^^ PART A\n\n\n"); + connectionSlot = slots[index]; + if (connectionSlot != NULL) { + MarkedCell msg(chatText[chatIdx]); + this->addMarkedCell(msg); - // New lag check - std::pair clientLagExceededOrWarned = std::make_pair(false,false); - if( connectionSlot != NULL && connectionSlot->isConnected() == true) { - //printf("\n\n\n^^^^^^^^^^^^^^ PART B\n\n\n"); - - lastGlobalLagCheckTimeUpdate = true; - clientLagExceededOrWarned = clientLagCheck(connectionSlot,slotsWarnedList[index]); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, gameSettings.getNetworkPauseGameForLaggedClients() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientLagExceededOrWarned.first,clientLagExceededOrWarned.second,gameSettings.getNetworkPauseGameForLaggedClients()); - - if(clientLagExceededOrWarned.first == true) { - slotsWarnedList[index] = true; - } - - // If the client has exceeded lag and the server wants - // to pause while they catch up, re-trigger the - // client reader thread - if((clientLagExceededOrWarned.second == true && - gameSettings.getNetworkPauseGameForLaggedClients() == true)) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d, clientLagExceededOrWarned.first = %d, clientLagExceededOrWarned.second = %d, waitForClientsElapsed.getMillis() = %d, MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,clientLagExceededOrWarned.first,clientLagExceededOrWarned.second,(int)waitForClientsElapsed.getMillis(),MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS); - - checkForAutoPauseForLaggingClient(index, connectionSlot); - } + NetworkMessageMarkCell networkMessageMarkCell(msg.getTargetPos(), msg.getFactionIndex(), msg.getNote(), msg.getPlayerIndex()); + broadcastMessage(&networkMessageMarkCell, connectionSlot->getPlayerIndex(), index); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); } } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] i = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, index); + // Its possible that the slot is disconnected here + // so check the original pointer again + if (slots[index] != NULL) { + slots[index]->clearMarkedCellList(); + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); errorMsgList.push_back(ex.what()); } } - - //printf("#5 Check lag for i: %d\n",i); } } - } - if(lastGlobalLagCheckTimeUpdate == true) { - lastGlobalLagCheckTime = time(NULL); - } -} + void ServerInterface::dispatchPendingHighlightCellMessages(std::vector &errorMsgList) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { -void ServerInterface::executeNetworkCommandsFromClients() { - if(gameHasBeenInitiated == true) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot= slots[index]; - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - vector pendingList = connectionSlot->getPendingNetworkCommandList(true); - if(pendingList.empty() == false) { - for(int idx = 0; exitServer == false && idx < (int)pendingList.size(); ++idx) { - NetworkCommand &cmd = pendingList[idx]; - this->requestCommand(&cmd); - } - //printf("Executed: %d commands from slot: %d\n",pendingList.size(),index); - } - } - } - } -} + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + if (connectionSlot != NULL && + connectionSlot->getHighlightedCellList(false).empty() == false) { -void ServerInterface::dispatchPendingChatMessages(std::vector &errorMsgList) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + try { + std::vector highlightedCells = connectionSlot->getHighlightedCellList(true); + for (int chatIdx = 0; + exitServer == false && slots[index] != NULL && + chatIdx < (int) highlightedCells.size(); chatIdx++) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot = slots[index]; + connectionSlot = slots[index]; + if (connectionSlot != NULL) { + MarkedCell msg(highlightedCells[chatIdx]); + this->setHighlightedCell(msg); - if(connectionSlot != NULL && - connectionSlot->getChatTextList(false).empty() == false) { - try { - std::vector chatText = connectionSlot->getChatTextList(true); - for(int chatIdx = 0; - exitServer == false && slots[index] != NULL && - chatIdx < (int)chatText.size(); chatIdx++) { - - connectionSlot = slots[index]; - if(connectionSlot != NULL) { - ChatMsgInfo msg(chatText[chatIdx]); - this->addChatInfo(msg); - - string newChatText = msg.chatText.c_str(); - int newChatTeamIndex = msg.chatTeamIndex; - int newChatPlayerIndex = msg.chatPlayerIndex; - string newChatLanguage = msg.targetLanguage; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatTeamIndex = %d, newChatPlayerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex,newChatPlayerIndex); - - if(newChatLanguage == "" || - newChatLanguage == connectionSlot->getNetworkPlayerLanguage()) { - - NetworkMessageText networkMessageText(newChatText.c_str(),newChatTeamIndex,newChatPlayerIndex,newChatLanguage); - broadcastMessage(&networkMessageText, connectionSlot->getPlayerIndex(),index); + NetworkMessageHighlightCell networkMessageHighlightCell(msg.getTargetPos(), msg.getFactionIndex()); + broadcastMessage(&networkMessageHighlightCell, connectionSlot->getPlayerIndex(), index); + } } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] index = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,index); - // Its possible that the slot is disconnected here - // so check the original pointer again - if(slots[index] != NULL) { - slots[index]->clearChatInfo(); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - errorMsgList.push_back(ex.what()); - } - } - } -} - -void ServerInterface::dispatchPendingMarkCellMessages(std::vector &errorMsgList) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot= slots[index]; - - if(connectionSlot != NULL && - connectionSlot->getMarkedCellList(false).empty() == false) { - - try { - std::vector chatText = connectionSlot->getMarkedCellList(true); - for(int chatIdx = 0; - exitServer == false && slots[index] != NULL && - chatIdx < (int)chatText.size(); chatIdx++) { - - connectionSlot= slots[index]; - if(connectionSlot != NULL) { - MarkedCell msg(chatText[chatIdx]); - this->addMarkedCell(msg); - - NetworkMessageMarkCell networkMessageMarkCell(msg.getTargetPos(),msg.getFactionIndex(),msg.getNote(),msg.getPlayerIndex()); - broadcastMessage(&networkMessageMarkCell, connectionSlot->getPlayerIndex(),index); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] i = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,index); - // Its possible that the slot is disconnected here - // so check the original pointer again - if(slots[index] != NULL) { - slots[index]->clearMarkedCellList(); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - errorMsgList.push_back(ex.what()); - } - } - } -} -void ServerInterface::dispatchPendingHighlightCellMessages(std::vector &errorMsgList) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot= slots[index]; - if(connectionSlot != NULL && - connectionSlot->getHighlightedCellList(false).empty() == false) { - - try { - std::vector highlightedCells = connectionSlot->getHighlightedCellList(true); - for(int chatIdx = 0; - exitServer == false && slots[index] != NULL && - chatIdx < (int)highlightedCells.size(); chatIdx++) { - - connectionSlot= slots[index]; - if(connectionSlot != NULL) { - MarkedCell msg(highlightedCells[chatIdx]); - this->setHighlightedCell(msg); - - NetworkMessageHighlightCell networkMessageHighlightCell(msg.getTargetPos(),msg.getFactionIndex()); - broadcastMessage(&networkMessageHighlightCell, connectionSlot->getPlayerIndex(),index); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] index = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,index); - // Its possible that the slot is disconnected here - // so check the original pointer again - if(slots[index] != NULL) { - slots[index]->clearHighlightedCellList(); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - errorMsgList.push_back(ex.what()); - } - } - } -} - -void ServerInterface::dispatchPendingUnMarkCellMessages(std::vector &errorMsgList) { - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot= slots[index]; - if(connectionSlot != NULL && - connectionSlot->getUnMarkedCellList(false).empty() == false) { - - try { - std::vector chatText = connectionSlot->getUnMarkedCellList(true); - for(int chatIdx = 0; - exitServer == false && slots[index] != NULL && - chatIdx < (int)chatText.size(); chatIdx++) { - - connectionSlot = slots[index]; - if(connectionSlot != NULL) { - UnMarkedCell msg(chatText[chatIdx]); - this->addUnMarkedCell(msg); - - NetworkMessageUnMarkCell networkMessageMarkCell(msg.getTargetPos(),msg.getFactionIndex()); - broadcastMessage(&networkMessageMarkCell, connectionSlot->getPlayerIndex(),index); - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] i = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,index); - // Its possible that the slot is disconnected here - // so check the original pointer again - if(slots[index] != NULL) { - slots[index]->clearUnMarkedCellList(); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - errorMsgList.push_back(ex.what()); - } - } - } -} - -void ServerInterface::checkForAutoResumeForLaggingClients() { - if (gameSettings.getNetworkPauseGameForLaggedClients() == true && - this->clientsAutoPausedDueToLag == true && - this->clientsAutoPausedDueToLagTimer.getMillis() >= MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS) { - - //printf("this->clientsAutoPausedDueToLag: %d [%lld]\n",this->clientsAutoPausedDueToLag,(long long)this->clientsAutoPausedDueToLagTimer.getMillis()); - if (this->clientLagCallbackInterface != NULL) { - - this->clientsAutoPausedDueToLag = false; - this->clientLagCallbackInterface->clientLagHandler(-1, false); - - this->clientsAutoPausedDueToLagTimer.reset(); - this->clientsAutoPausedDueToLagTimer.stop(); - this->clientsAutoPausedDueToLagTimer.start(); - } - } -} - -void ServerInterface::update() { - //printf("\nServerInterface::update -- A\n"); - - std::vector errorMsgList; - try { - // The first thing we will do is check all clients to ensure they have - // properly identified themselves within the alloted time period - validateConnectedClients(); - - //printf("\nServerInterface::update -- B\n"); - - processTextMessageQueue(); - processBroadCastMessageQueue(); - - checkForAutoResumeForLaggingClients(); - - //printf("\nServerInterface::update -- C\n"); - - std::map socketTriggeredList; - //update all slots - updateSocketTriggeredList(socketTriggeredList); - - //printf("\nServerInterface::update -- D\n"); - - if(gameHasBeenInitiated == false || - socketTriggeredList.empty() == false) { - //printf("\nServerInterface::update -- E\n"); - - std::map eventList; - - bool hasData = false; - if(gameHasBeenInitiated == false) { - hasData = Socket::hasDataToRead(socketTriggeredList); - } - else { - hasData = true; - } - - if(hasData && SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] hasData == true\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - if(gameHasBeenInitiated == false || hasData == true) { - //printf("START Server update #2\n"); - std::map mapSlotSignalledList; - - // Step #1 tell all connection slot worker threads to receive socket data - if(gameHasBeenInitiated == false) { - signalClientsToRecieveData(socketTriggeredList, eventList, mapSlotSignalledList); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ============ Step #2\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //printf("START Server update #2\n"); - if(gameHasBeenInitiated == false || hasData == true) { - //printf("START Server update #3\n"); - - // Step #2 check all connection slot worker threads for completed status - if(gameHasBeenInitiated == false) { - checkForCompletedClients(mapSlotSignalledList,errorMsgList, eventList); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ============ Step #3\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //printf("START Server update #4\n"); - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - // Step #3 check clients for any lagging scenarios and try to deal with them - if(gameHasBeenInitiated == false) { - checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList,errorMsgList); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ============ Step #4\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //printf("START Server update #5\n"); - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - // Step #4 dispatch network commands to the pending list so that they are done in proper order - executeNetworkCommandsFromClients(); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ============ Step #5\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - //printf("START Server update #6\n"); - //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - // Step #5 dispatch pending chat messages - dispatchPendingChatMessages(errorMsgList); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - dispatchPendingMarkCellMessages(errorMsgList); - dispatchPendingUnMarkCellMessages(errorMsgList); - - dispatchPendingHighlightCellMessages(errorMsgList); - - if(gameHasBeenInitiated == true && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { - - std::map mapSlotSignalledList; - checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList,errorMsgList); - } - //printf("START Server update #7\n"); - } - else if(gameHasBeenInitiated == true && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { - - //printf("Skip network data process because hasData == false\n"); - //printf("START Server update #8\n"); - - std::map mapSlotSignalledList; - checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList,errorMsgList); - } - //printf("START Server update #9\n"); - } - else if(gameHasBeenInitiated == true && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { - - //printf("\nServerInterface::update -- E1\n"); - //printf("START Server update #10\n"); - - std::map mapSlotSignalledList; - checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList,errorMsgList); - } - //printf("START Server update #11\n"); - } - else if(gameHasBeenInitiated == true && - difftime((long int)time(NULL),gameStartTime) >= LAG_CHECK_GRACE_PERIOD && - difftime((long int)time(NULL),lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { - - //printf("\nServerInterface::update -- F\n"); - //printf("START Server update #12\n"); - - std::map eventList; - std::map mapSlotSignalledList; - - checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList,errorMsgList); - } - //printf("START Server update #13\n"); - - // Check if we need to switch masterserver admin to a new player because original admin disconnected - if(gameHasBeenInitiated == true && - this->gameSettings.getMasterserver_admin() > 0) { - - bool foundAdminSlot = false; - int iFirstConnectedSlot = -1; - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - if(slots[index] != NULL && slots[index]->isConnected() == true) { - if(iFirstConnectedSlot < 0) { - iFirstConnectedSlot = index; - } - if(this->gameSettings.getMasterserver_admin() == slots[index]->getSessionKey()) { - foundAdminSlot = true; - break; - } - } - } - - if(foundAdminSlot == false && iFirstConnectedSlot >= 0) { - printf("Switching masterserver admin to slot#%d...\n",iFirstConnectedSlot); - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[iFirstConnectedSlot],CODE_AT_LINE_X(iFirstConnectedSlot)); - if(slots[iFirstConnectedSlot] != NULL) { - string sMsg = "Switching player to admin mode: " + slots[iFirstConnectedSlot]->getName(); - sendTextMessage(sMsg,-1, true,""); - - this->gameSettings.setMasterserver_admin(slots[iFirstConnectedSlot]->getSessionKey()); - this->gameSettings.setMasterserver_admin_faction_index(slots[iFirstConnectedSlot]->getPlayerIndex()); - - safeMutexSlot.ReleaseLock(); - this->broadcastGameSetup(&this->gameSettings); - } - } - } - //printf("\nServerInterface::update -- G\n"); - //printf("START Server update #14\n"); - - checkListenerSlots(); - - //printf("START Server update #15\n"); - } - catch(const exception &ex) { - //printf("\nServerInterface::update -- H\n"); - - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - errorMsgList.push_back(ex.what()); - } - - if(errorMsgList.empty() == false){ - for(int iErrIdx = 0; iErrIdx < (int)errorMsgList.size(); ++iErrIdx) { - string &sErr = errorMsgList[iErrIdx]; - if(sErr != "") { - DisplayErrorMessage(sErr); - } - } - } -} - -void ServerInterface::updateKeyframe(int frameCount) { - currentFrameCount = frameCount; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] currentFrameCount = %d, requestedCommands.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,currentFrameCount,requestedCommands.size()); - - NetworkMessageCommandList networkMessageCommandList(frameCount); - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - networkMessageCommandList.setNetworkPlayerFactionCRC(index,this->getNetworkPlayerFactionCRC(index)); - } - - while(requestedCommands.empty() == false) { - // First add the command to the broadcast list (for all clients) - if(networkMessageCommandList.addCommand(&requestedCommands.back())) { - // Add the command to the local server command list - pendingCommands.push_back(requestedCommands.back()); - requestedCommands.pop_back(); - } - else { - break; - } - } - - try { - // Possible cause of out of synch since we have more commands that need - // to be sent in this frame - if(requestedCommands.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,requestedCommands.size()); - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,requestedCommands.size()); - - string sMsg = "may go out of synch: server requestedCommands.size() = " + intToStr(requestedCommands.size()); - sendTextMessage(sMsg,-1, true,""); - } - - // broadcast commands - // If we have more than 0 commands to send, automatically broadcast them - bool sendBroadcastMessage = (networkMessageCommandList.getCommandCount() > 0); - if(sendBroadcastMessage == false) { - - // Is auto pause due to lag NOT enabled - if(this->getClientsAutoPausedDueToLag() == false) { - - // ****NOTE: - // We always need to broadcast when not pause as clients - // look for broadcasts every network frame. - sendBroadcastMessage = true; - } - // Auto pause is enabled due to client lagging, only send empty command - // broadcasts every MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS - else if(this->getClientsAutoPausedDueToLag() == true && - (lastBroadcastCommandsTimer.isStarted() == false || - lastBroadcastCommandsTimer.getMillis() >= MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS)) { - - sendBroadcastMessage = true; - } - } - - if(sendBroadcastMessage == true) { - - if(lastBroadcastCommandsTimer.isStarted() == false) { - lastBroadcastCommandsTimer.start(); - } - else { - lastBroadcastCommandsTimer.stop(); - lastBroadcastCommandsTimer.reset(); - lastBroadcastCommandsTimer.start(); - } - broadcastMessage(&networkMessageCommandList); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - DisplayErrorMessage(ex.what()); - } -} - -bool ServerInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMessageType, - ConnectionSlot *connectionSlot) { - bool discard = false; - if(connectionSlot != NULL) { - switch(networkMessageType) { - case nmtIntro: - { - discard = true; - NetworkMessageIntro msg = NetworkMessageIntro(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtPing: - { - discard = true; - NetworkMessagePing msg = NetworkMessagePing(); - connectionSlot->receiveMessage(&msg); - lastPingInfo = msg; - } - break; - - case nmtLaunch: - { - discard = true; - NetworkMessageLaunch msg = NetworkMessageLaunch(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtText: - { - discard = true; - NetworkMessageText netMsg = NetworkMessageText(); - connectionSlot->receiveMessage(&netMsg); - - ChatMsgInfo msg(netMsg.getText().c_str(),netMsg.getTeamIndex(),netMsg.getPlayerIndex(),netMsg.getTargetLanguage()); - this->addChatInfo(msg); - - string newChatText = msg.chatText.c_str(); - //string newChatSender = msg.chatSender.c_str(); - int newChatTeamIndex = msg.chatTeamIndex; - int newChatPlayerIndex = msg.chatPlayerIndex; - string newChatLanguage = msg.targetLanguage.c_str(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatTeamIndex = %d, newChatPlayerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex,newChatPlayerIndex); - - NetworkMessageText networkMessageText(newChatText.c_str(),newChatTeamIndex,newChatPlayerIndex,newChatLanguage); - broadcastMessage(&networkMessageText, connectionSlot->getPlayerIndex()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - - } - break; - - case nmtMarkCell: - { - discard = true; - NetworkMessageMarkCell networkMessageMarkCell; - connectionSlot->receiveMessage(&networkMessageMarkCell); - - MarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); - - this->addMarkedCell(msg); - - NetworkMessageMarkCell networkMessageMarkCellBroadcast( - networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex(), - networkMessageMarkCell.getText().c_str(), - networkMessageMarkCell.getPlayerIndex()); - broadcastMessage(&networkMessageMarkCellBroadcast, connectionSlot->getPlayerIndex()); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - - } - break; - - case nmtUnMarkCell: - { - discard = true; - NetworkMessageUnMarkCell networkMessageMarkCell; - connectionSlot->receiveMessage(&networkMessageMarkCell); - - UnMarkedCell msg(networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); - - this->addUnMarkedCell(msg); - - NetworkMessageUnMarkCell networkMessageMarkCellBroadcast( - networkMessageMarkCell.getTarget(), - networkMessageMarkCell.getFactionIndex()); - broadcastMessage(&networkMessageMarkCellBroadcast, connectionSlot->getPlayerIndex()); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); - - } - break; - case nmtHighlightCell: - { - discard = true; - NetworkMessageHighlightCell networkMessageHighlightCell; - connectionSlot->receiveMessage(&networkMessageHighlightCell); - - MarkedCell msg(networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex(), - "none",-1); - - this->setHighlightedCell(msg); - - NetworkMessageHighlightCell networkMessageHighlightCellBroadcast( - networkMessageHighlightCell.getTarget(), - networkMessageHighlightCell.getFactionIndex()); - broadcastMessage(&networkMessageHighlightCellBroadcast, connectionSlot->getPlayerIndex()); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatTeamIndex); - - } - break; - - case nmtSynchNetworkGameData: - { - discard = true; - NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataStatus: - { - discard = true; - NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataFileCRCCheck: - { - discard = true; - NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtSynchNetworkGameDataFileGet: - { - discard = true; - NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtSwitchSetupRequest: - { - discard = true; - SwitchSetupRequest msg = SwitchSetupRequest(); - connectionSlot->receiveMessage(&msg); - } - break; - case nmtPlayerIndexMessage: - { - discard = true; - PlayerIndexMessage msg = PlayerIndexMessage(0); - connectionSlot->receiveMessage(&msg); - } - break; - } - } - return discard; -} - -void ServerInterface::waitUntilReady(Checksum *checksum) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s] START\n",__FUNCTION__); - Logger & logger = Logger::getInstance(); - gameHasBeenInitiated = true; - Chrono chrono; - chrono.start(); - - bool allReady = false; - - if(Config::getInstance().getBool("EnableGameServerLoadCancel","false") == true) { - logger.setCancelLoadingEnabled(true); - } - - Lang &lang = Lang::getInstance(); - uint64 waitLoopIterationCount = 0; - uint64 MAX_LOOP_COUNT_BEFORE_SLEEP = 10; - MAX_LOOP_COUNT_BEFORE_SLEEP = Config::getInstance().getInt("NetworkServerLoopGameLoadingCap",intToStr(MAX_LOOP_COUNT_BEFORE_SLEEP).c_str()); - if(MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { - MAX_LOOP_COUNT_BEFORE_SLEEP = 1; - } - int sleepMillis = Config::getInstance().getInt("NetworkServerLoopGameLoadingCapSleepMillis","10"); - int64 lastStatusUpdate = 0; - - while(exitServer == false && - allReady == false && - logger.getCancelLoading() == false) { - - waitLoopIterationCount++; - if(waitLoopIterationCount > 0 && - waitLoopIterationCount % MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { - - sleep(sleepMillis); - waitLoopIterationCount = 0; - } - vector waitingForHosts; - allReady= true; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot* connectionSlot= slots[index]; - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - if(connectionSlot->isReady() == false) { - - NetworkMessageType networkMessageType= connectionSlot->getNextMessageType(); - - // consume old messages from the lobby - bool discarded = shouldDiscardNetworkMessage(networkMessageType,connectionSlot); - if(discarded == false) { - - NetworkMessageReady networkMessageReady; - if(networkMessageType == nmtReady && - connectionSlot->receiveMessage(&networkMessageReady)) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s] networkMessageType==nmtReady\n",__FUNCTION__); - - connectionSlot->setReady(); - connectionSlot->setGameStarted(true); - } - else if(networkMessageType != nmtInvalid) { - string sErr = "Unexpected network message: " + intToStr(networkMessageType); - sendTextMessage(sErr,-1, true,"",index); - DisplayErrorMessage(sErr); - logger.setCancelLoading(false); - return; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] index = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, index); + // Its possible that the slot is disconnected here + // so check the original pointer again + if (slots[index] != NULL) { + slots[index]->clearHighlightedCellList(); } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); } - waitingForHosts.push_back(connectionSlot->getName()); - allReady= false; } } } - //check for timeout - if(allReady == false) { - if(chrono.getMillis() > readyWaitTimeout) { - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - string sErr = "Timeout waiting for clients."; - if(lang.hasString("TimeoutWaitingForClients") == true) { - sErr = lang.getString("TimeoutWaitingForClients",languageList[langIndex]); - } - bool localEcho = lang.isLanguageLocal(languageList[langIndex]); - sendTextMessage(sErr,-1, localEcho, languageList[langIndex]); - if(localEcho == true) { - DisplayErrorMessage(sErr); - } - } - logger.setCancelLoading(false); - return; - } - else { - if(chrono.getMillis() - lastStatusUpdate > 200) { - lastStatusUpdate = chrono.getMillis(); + void ServerInterface::dispatchPendingUnMarkCellMessages(std::vector &errorMsgList) { + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - string waitForHosts = ""; - for(int hostIndex = 0; hostIndex < (int)waitingForHosts.size(); hostIndex++) { - if(waitForHosts != "") { - waitForHosts += ", "; + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + if (connectionSlot != NULL && + connectionSlot->getUnMarkedCellList(false).empty() == false) { + + try { + std::vector chatText = connectionSlot->getUnMarkedCellList(true); + for (int chatIdx = 0; + exitServer == false && slots[index] != NULL && + chatIdx < (int) chatText.size(); chatIdx++) { + + connectionSlot = slots[index]; + if (connectionSlot != NULL) { + UnMarkedCell msg(chatText[chatIdx]); + this->addUnMarkedCell(msg); + + NetworkMessageUnMarkCell networkMessageMarkCell(msg.getTargetPos(), msg.getFactionIndex()); + broadcastMessage(&networkMessageMarkCell, connectionSlot->getPlayerIndex(), index); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); + } } - waitForHosts += waitingForHosts[hostIndex]; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] i = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, index); + // Its possible that the slot is disconnected here + // so check the original pointer again + if (slots[index] != NULL) { + slots[index]->clearUnMarkedCellList(); + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); + } + } + } + } + + void ServerInterface::checkForAutoResumeForLaggingClients() { + if (gameSettings.getNetworkPauseGameForLaggedClients() == true && + this->clientsAutoPausedDueToLag == true && + this->clientsAutoPausedDueToLagTimer.getMillis() >= MAX_CLIENT_WAIT_SECONDS_FOR_PAUSE_MILLISECONDS) { + + //printf("this->clientsAutoPausedDueToLag: %d [%lld]\n",this->clientsAutoPausedDueToLag,(long long)this->clientsAutoPausedDueToLagTimer.getMillis()); + if (this->clientLagCallbackInterface != NULL) { + + this->clientsAutoPausedDueToLag = false; + this->clientLagCallbackInterface->clientLagHandler(-1, false); + + this->clientsAutoPausedDueToLagTimer.reset(); + this->clientsAutoPausedDueToLagTimer.stop(); + this->clientsAutoPausedDueToLagTimer.start(); + } + } + } + + void ServerInterface::update() { + //printf("\nServerInterface::update -- A\n"); + + std::vector errorMsgList; + try { + // The first thing we will do is check all clients to ensure they have + // properly identified themselves within the alloted time period + validateConnectedClients(); + + //printf("\nServerInterface::update -- B\n"); + + processTextMessageQueue(); + processBroadCastMessageQueue(); + + checkForAutoResumeForLaggingClients(); + + //printf("\nServerInterface::update -- C\n"); + + std::map socketTriggeredList; + //update all slots + updateSocketTriggeredList(socketTriggeredList); + + //printf("\nServerInterface::update -- D\n"); + + if (gameHasBeenInitiated == false || + socketTriggeredList.empty() == false) { + //printf("\nServerInterface::update -- E\n"); + + std::map eventList; + + bool hasData = false; + if (gameHasBeenInitiated == false) { + hasData = Socket::hasDataToRead(socketTriggeredList); + } else { + hasData = true; } - char szBuf[8096]=""; - string updateTextFormat = lang.getString("NetworkGameServerLoadStatus"); - if(updateTextFormat == "" || - updateTextFormat[0] == '?') { + if (hasData && SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] hasData == true\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); - updateTextFormat = "Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds)"; + if (gameHasBeenInitiated == false || hasData == true) { + //printf("START Server update #2\n"); + std::map mapSlotSignalledList; + + // Step #1 tell all connection slot worker threads to receive socket data + if (gameHasBeenInitiated == false) { + signalClientsToRecieveData(socketTriggeredList, eventList, mapSlotSignalledList); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ============ Step #2\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //printf("START Server update #2\n"); + if (gameHasBeenInitiated == false || hasData == true) { + //printf("START Server update #3\n"); + + // Step #2 check all connection slot worker threads for completed status + if (gameHasBeenInitiated == false) { + checkForCompletedClients(mapSlotSignalledList, errorMsgList, eventList); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ============ Step #3\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //printf("START Server update #4\n"); + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + // Step #3 check clients for any lagging scenarios and try to deal with them + if (gameHasBeenInitiated == false) { + checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList, errorMsgList); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ============ Step #4\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //printf("START Server update #5\n"); + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // Step #4 dispatch network commands to the pending list so that they are done in proper order + executeNetworkCommandsFromClients(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ============ Step #5\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + //printf("START Server update #6\n"); + //printf("In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + // Step #5 dispatch pending chat messages + dispatchPendingChatMessages(errorMsgList); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + dispatchPendingMarkCellMessages(errorMsgList); + dispatchPendingUnMarkCellMessages(errorMsgList); + + dispatchPendingHighlightCellMessages(errorMsgList); + + if (gameHasBeenInitiated == true && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { + + std::map mapSlotSignalledList; + checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList, errorMsgList); + } + //printf("START Server update #7\n"); + } else if (gameHasBeenInitiated == true && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { + + //printf("Skip network data process because hasData == false\n"); + //printf("START Server update #8\n"); + + std::map mapSlotSignalledList; + checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList, errorMsgList); + } + //printf("START Server update #9\n"); + } else if (gameHasBeenInitiated == true && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { + + //printf("\nServerInterface::update -- E1\n"); + //printf("START Server update #10\n"); + + std::map mapSlotSignalledList; + checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList, errorMsgList); } - snprintf(szBuf,8096,updateTextFormat.c_str(),(long long int)(chrono.getMillis() / 1000),int(readyWaitTimeout / 1000)); + //printf("START Server update #11\n"); + } else if (gameHasBeenInitiated == true && + difftime((long int) time(NULL), gameStartTime) >= LAG_CHECK_GRACE_PERIOD && + difftime((long int) time(NULL), lastGlobalLagCheckTime) >= LAG_CHECK_INTERVAL_PERIOD) { - char szBuf1[8096]=""; - string statusTextFormat = lang.getString("NetworkGameStatusWaiting"); - if(statusTextFormat == "" || - statusTextFormat[0] == '?') { + //printf("\nServerInterface::update -- F\n"); + //printf("START Server update #12\n"); - statusTextFormat = "Waiting for players: %s"; - } - snprintf(szBuf1,8096,statusTextFormat.c_str(),waitForHosts.c_str()); + std::map eventList; + std::map mapSlotSignalledList; - logger.add(szBuf, true, szBuf1); + checkForLaggingClients(mapSlotSignalledList, eventList, socketTriggeredList, errorMsgList); + } + //printf("START Server update #13\n"); - uint32 loadingStatus = nmls_NONE; - //send ready message after, so clients start delayed - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot= slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - // FIXME: maxPlayers is declared in game_constants.h. Code such as - // this must be updated when maxPlayers is changed - // - // The PLAYER vars are enum'ed in network_message.h - switch(slotIndex) { - case 0: - loadingStatus |= nmls_PLAYER1_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER1_READY; - } - break; - case 1: - loadingStatus |= nmls_PLAYER2_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER2_READY; - } - break; - case 2: - loadingStatus |= nmls_PLAYER3_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER3_READY; - } - break; - case 3: - loadingStatus |= nmls_PLAYER4_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER4_READY; - } - break; - case 4: - loadingStatus |= nmls_PLAYER5_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER5_READY; - } - break; - case 5: - loadingStatus |= nmls_PLAYER6_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER6_READY; - } - break; - case 6: - loadingStatus |= nmls_PLAYER7_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER7_READY; - } - break; - case 7: - loadingStatus |= nmls_PLAYER8_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER8_READY; - } - break; - case 8: - loadingStatus |= nmls_PLAYER9_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER9_READY; - } - break; - case 9: - loadingStatus |= nmls_PLAYER10_CONNECTED; - if(connectionSlot->isReady()) { - loadingStatus |= nmls_PLAYER10_READY; - } - break; + // Check if we need to switch masterserver admin to a new player because original admin disconnected + if (gameHasBeenInitiated == true && + this->gameSettings.getMasterserver_admin() > 0) { + bool foundAdminSlot = false; + int iFirstConnectedSlot = -1; + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + if (slots[index] != NULL && slots[index]->isConnected() == true) { + if (iFirstConnectedSlot < 0) { + iFirstConnectedSlot = index; + } + if (this->gameSettings.getMasterserver_admin() == slots[index]->getSessionKey()) { + foundAdminSlot = true; + break; } } } - // send loading status message - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot* connectionSlot= slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - NetworkMessageLoadingStatus networkMessageLoadingStatus(loadingStatus); - connectionSlot->sendMessage(&networkMessageLoadingStatus); + if (foundAdminSlot == false && iFirstConnectedSlot >= 0) { + printf("Switching masterserver admin to slot#%d...\n", iFirstConnectedSlot); + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[iFirstConnectedSlot], CODE_AT_LINE_X(iFirstConnectedSlot)); + if (slots[iFirstConnectedSlot] != NULL) { + string sMsg = "Switching player to admin mode: " + slots[iFirstConnectedSlot]->getName(); + sendTextMessage(sMsg, -1, true, ""); + + this->gameSettings.setMasterserver_admin(slots[iFirstConnectedSlot]->getSessionKey()); + this->gameSettings.setMasterserver_admin_faction_index(slots[iFirstConnectedSlot]->getPlayerIndex()); + + safeMutexSlot.ReleaseLock(); + this->broadcastGameSetup(&this->gameSettings); } } - - sleep(0); } + //printf("\nServerInterface::update -- G\n"); + //printf("START Server update #14\n"); + + checkListenerSlots(); + + //printf("START Server update #15\n"); + } catch (const exception &ex) { + //printf("\nServerInterface::update -- H\n"); + + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + errorMsgList.push_back(ex.what()); } - } - Shared::Platform::Window::handleEvent(); - } - - if(logger.getCancelLoading() == true) { - Lang &lang= Lang::getInstance(); - const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); - for(unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { - - string sErr = lang.getString("GameCancelledByUser",languageList[langIndex]); - bool localEcho = lang.isLanguageLocal(languageList[langIndex]); - sendTextMessage(sErr,-1, localEcho,languageList[langIndex]); - - if(localEcho == true) { - DisplayErrorMessage(sErr); - } - } - quitGame(true); - logger.setCancelLoading(false); - return; - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s] PART B (telling client we are ready!\n",__FUNCTION__); - try { - //send ready message after, so clients start delayed - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot* connectionSlot= slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected() == true) { - NetworkMessageReady networkMessageReady(checksum->getSum()); - connectionSlot->sendMessage(&networkMessageReady); - connectionSlot->setGameStarted(true); - } - } - - gameStartTime = time(NULL); - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] error detected [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - DisplayErrorMessage(ex.what()); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s] END\n",__FUNCTION__); -} - -void ServerInterface::processBroadCastMessageQueue() { - MutexSafeWrapper safeMutexSlot(broadcastMessageQueueThreadAccessor,CODE_AT_LINE); - if(broadcastMessageQueue.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] broadcastMessageQueue.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,broadcastMessageQueue.size()); - for(int index = 0; index < (int)broadcastMessageQueue.size(); ++index) { - pair &item = broadcastMessageQueue[index]; - if(item.first != NULL) { - this->broadcastMessage(item.first,item.second); - delete item.first; - } - item.first = NULL; - } - broadcastMessageQueue.clear(); - } -} - -void ServerInterface::queueBroadcastMessage(NetworkMessage *networkMessage, int excludeSlot) { - MutexSafeWrapper safeMutexSlot(broadcastMessageQueueThreadAccessor,CODE_AT_LINE); - pair item; - item.first = networkMessage; - item.second = excludeSlot; - broadcastMessageQueue.push_back(item); -} - -void ServerInterface::processTextMessageQueue() { - MutexSafeWrapper safeMutexSlot(textMessageQueueThreadAccessor,CODE_AT_LINE); - if(textMessageQueue.empty() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] textMessageQueue.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,textMessageQueue.size()); - for(int index = 0; index < (int)textMessageQueue.size(); ++index) { - TextMessageQueue &item = textMessageQueue[index]; - sendTextMessage(item.text, item.teamIndex, item.echoLocal, item.targetLanguage); - } - textMessageQueue.clear(); - } -} - -void ServerInterface::queueTextMessage(const string & text, int teamIndex, - bool echoLocal, string targetLanguage) { - //printf("Line: %d text [%s]\n",__LINE__,text.c_str()); - - MutexSafeWrapper safeMutexSlot(textMessageQueueThreadAccessor,CODE_AT_LINE); - TextMessageQueue item; - item.text = text; - item.teamIndex = teamIndex; - item.echoLocal = echoLocal; - item.targetLanguage = targetLanguage; - textMessageQueue.push_back(item); -} - -void ServerInterface::sendTextMessage(const string & text, int teamIndex, - bool echoLocal,string targetLanguage) { - sendTextMessage(text, teamIndex, echoLocal, targetLanguage, -1); -} - -void ServerInterface::sendTextMessage(const string& text, int teamIndex, bool echoLocal, - string targetLanguage, int lockedSlotIndex) { - //printf("Line: %d text [%s] echoLocal = %d\n",__LINE__,text.c_str(),echoLocal); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] text [%s] teamIndex = %d, echoLocal = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,text.c_str(),teamIndex,echoLocal,lockedSlotIndex); - - NetworkMessageText networkMessageText(text, teamIndex, getHumanPlayerIndex(), targetLanguage); - broadcastMessage(&networkMessageText, -1, lockedSlotIndex); - - if(echoLocal == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - ChatMsgInfo msg(text.c_str(),teamIndex,networkMessageText.getPlayerIndex(), targetLanguage); - this->addChatInfo(msg); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note,int playerIndex) { - sendMarkCellMessage(targetPos, factionIndex, note, playerIndex, -1); -} - -void ServerInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex, int lockedSlotIndex) { - NetworkMessageMarkCell networkMessageMarkCell(targetPos,factionIndex, note, playerIndex); - broadcastMessage(&networkMessageMarkCell, -1, lockedSlotIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex) { - sendHighlightCellMessage(targetPos, factionIndex, -1); -} - -void ServerInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex) { - NetworkMessageHighlightCell networkMessageHighlightCell(targetPos,factionIndex); - broadcastMessage(&networkMessageHighlightCell, -1, lockedSlotIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) { - sendUnMarkCellMessage(targetPos, factionIndex, -1); -} - -void ServerInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex) { - NetworkMessageUnMarkCell networkMessageMarkCell(targetPos,factionIndex); - broadcastMessage(&networkMessageMarkCell, -1, lockedSlotIndex); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::quitGame(bool userManuallyQuit) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - NetworkMessageQuit networkMessageQuit; - broadcastMessage(&networkMessageQuit); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -string ServerInterface::getNetworkStatus() { - Lang &lang = Lang::getInstance(); - string str = ""; - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot= slots[index]; - - str += intToStr(index)+ ": "; - - if(connectionSlot!= NULL) { - if(connectionSlot->isConnected()) { - int clientLagCount = connectionSlot->getCurrentLagCount(); - double lastClientCommandListTimeLag = difftime((long int)time(NULL),connectionSlot->getLastReceiveCommandListTime()); - //float pingTime = connectionSlot->getThreadedPingMS(connectionSlot->getIpAddress().c_str()); - char szBuf[8096]=""; - snprintf(szBuf,8096,", lag = %d [%.2f]",clientLagCount,lastClientCommandListTimeLag); - str += connectionSlot->getName() + " [" + connectionSlot->getUUID() + "] " + string(szBuf); - } - } - else { - str += lang.getString("NotConnected"); - } - - str += '\n'; - } - return str; -} - -bool ServerInterface::launchGame(const GameSettings *gameSettings) { - bool bOkToStart = true; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - for(int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot= slots[index]; - if(connectionSlot != NULL && - (connectionSlot->getAllowDownloadDataSynch() == true || connectionSlot->getAllowGameDataSynchCheck() == true) && - connectionSlot->isConnected()) { - - if(connectionSlot->getNetworkGameDataSynchCheckOk() == false) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] map [%d] tile [%d] techtree [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,connectionSlot->getNetworkGameDataSynchCheckOkMap(),connectionSlot->getNetworkGameDataSynchCheckOkTile(),connectionSlot->getNetworkGameDataSynchCheckOkTech()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] map [%d] tile [%d] techtree [%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,connectionSlot->getNetworkGameDataSynchCheckOkMap(),connectionSlot->getNetworkGameDataSynchCheckOkTile(),connectionSlot->getNetworkGameDataSynchCheckOkTech()); - - bOkToStart = false; - break; - } - } - } - if(bOkToStart == true) { - - bool useInGameBlockingClientSockets = Config::getInstance().getBool("EnableInGameBlockingSockets","true"); - if(useInGameBlockingClientSockets == true) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - for(int index = 0; index < GameConstants::maxPlayers; ++index) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index],CODE_AT_LINE_X(index)); - ConnectionSlot *connectionSlot= slots[index]; - if(connectionSlot != NULL && connectionSlot->isConnected()) { - connectionSlot->getSocket()->setBlock(true); - } - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - bool requiresUPNPTrigger = false; - for(int startIndex = 0; startIndex < GameConstants::maxPlayers; ++startIndex) { - - int factionIndex = gameSettings->getFactionIndexForStartLocation(startIndex); - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[startIndex],CODE_AT_LINE_X(startIndex)); - ConnectionSlot *connectionSlot= slots[startIndex]; - if((connectionSlot == NULL || connectionSlot->isConnected() == false) && - this->getAllowInGameConnections() == true) { - - // Open slots for joining in progress game - if(gameSettings->getFactionControl(factionIndex) != ctClosed && - gameSettings->getFactionControl(factionIndex) != ctHuman) { - - //printf("Opening slot for in game connections for slot: %d, faction: %d\n",i,factionIndex); - if(connectionSlot == NULL) { - addSlot(startIndex); - connectionSlot = slots[startIndex]; - requiresUPNPTrigger = true; - } - connectionSlot->setCanAcceptConnections(true); - } - } - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - - if(this->getAllowInGameConnections() == false) { - serverSocket.stopBroadCastThread(); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - - this->gameSettings = *gameSettings; - //printf("#1 Data synch: lmap %u ltile: %d ltech: %u\n",gameSettings->getMapCRC(),gameSettings->getTilesetCRC(),gameSettings->getTechCRC()); - - NetworkMessageLaunch networkMessageLaunch(gameSettings,nmtLaunch); - broadcastMessage(&networkMessageLaunch); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - - shutdownMasterserverPublishThread(); - MutexSafeWrapper safeMutex(masterServerThreadAccessor,CODE_AT_LINE); - lastMasterserverHeartbeatTime = 0; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ftpServer = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ftpServer); - - if(this->getAllowInGameConnections() == false) { - shutdownFTPServer(); - } - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - - if(publishToMasterserverThread == NULL) { - if(needToRepublishToMasterserver == true || - GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - - static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); - publishToMasterserverThread = new SimpleTaskThread(this,0,125); - publishToMasterserverThread->setUniqueID(mutexOwnerId); - publishToMasterserverThread->start(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); - } - } - - if(this->getAllowInGameConnections() == false) { - shutdownFTPServer(); - } - - if((needToRepublishToMasterserver == true || - GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) && - requiresUPNPTrigger == true) { - - this->getServerSocket()->NETdiscoverUPnPDevices(); - } - - gameLaunched = true; - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - return bOkToStart; -} - -void ServerInterface::shutdownFTPServer() { - if(ftpServer != NULL) { - ftpServer->shutdownAndWait(); - delete ftpServer; - ftpServer = NULL; - } -} - -void ServerInterface::checkListenerSlots() { - if(gameLaunched == true && - this->getAllowInGameConnections() == true) { - - if(difftime((long int)time(NULL),lastListenerSlotCheckTime) >= 7) { - - lastListenerSlotCheckTime = time(NULL); - bool useInGameBlockingClientSockets = Config::getInstance().getBool("EnableInGameBlockingSockets","true"); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - for(int startIndex = 0; startIndex < GameConstants::maxPlayers; ++startIndex) { - - int factionIndex = gameSettings.getFactionIndexForStartLocation(startIndex); - if(gameSettings.getFactionControl(factionIndex) != ctClosed && - gameSettings.getFactionControl(factionIndex) != ctHuman) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[startIndex],CODE_AT_LINE_X(startIndex)); - ConnectionSlot *connectionSlot= slots[startIndex]; - // Open slots for joining in progress game - if(connectionSlot == NULL) { - printf("Opening slot for in game connections, slot: %d, factionindex: %d name: %s\n",startIndex,factionIndex,gameSettings.getFactionTypeName(factionIndex).c_str()); - - addSlot(startIndex); - connectionSlot = slots[startIndex]; - if(useInGameBlockingClientSockets == true) { - connectionSlot->getSocket()->setBlock(true); - } - connectionSlot->setCanAcceptConnections(true); - } - else if(connectionSlot != NULL && - connectionSlot->getCanAcceptConnections() == false && - connectionSlot->isConnected() == false) { - printf("Removing slot for in game connections, slot: %d, factionindex: %d name: %s\n",startIndex,factionIndex,gameSettings.getFactionTypeName(factionIndex).c_str()); - - this->removeSlot(startIndex); + if (errorMsgList.empty() == false) { + for (int iErrIdx = 0; iErrIdx < (int) errorMsgList.size(); ++iErrIdx) { + string &sErr = errorMsgList[iErrIdx]; + if (sErr != "") { + DisplayErrorMessage(sErr); } } } } - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); -} -void ServerInterface::broadcastGameSetup(GameSettings *gameSettingsBuffer, bool setGameSettingsBuffer) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + void ServerInterface::updateKeyframe(int frameCount) { + currentFrameCount = frameCount; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] currentFrameCount = %d, requestedCommands.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, currentFrameCount, requestedCommands.size()); - if(gameSettingsBuffer == NULL) { - throw megaglest_runtime_error("gameSettingsBuffer == NULL"); - } - for(unsigned int factionIndex = 0; factionIndex < (unsigned int)gameSettingsBuffer->getFactionCount(); ++factionIndex) { - - int slotIndex = gameSettingsBuffer->getStartLocationIndex(factionIndex); - if(gameSettingsBuffer->getFactionControl(factionIndex) == ctNetwork && - isClientConnected(slotIndex) == false) { - - gameSettingsBuffer->setNetworkPlayerName(factionIndex,GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); - } - } - if(setGameSettingsBuffer == true) { - validateGameSettings(gameSettingsBuffer); - } - - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - if(setGameSettingsBuffer == true) { - gameSettings = *gameSettingsBuffer; - gameSettingsUpdateCount++; - } - - NetworkMessageLaunch networkMessageLaunch(gameSettingsBuffer, nmtBroadCastSetup); - broadcastMessage(&networkMessageLaunch); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::broadcastMessage(NetworkMessage *networkMessage, int excludeSlot, int lockedSlotIndex) { - try { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutexSlotBroadCastAccessor(inBroadcastMessageThreadAccessor,CODE_AT_LINE); - if(inBroadcastMessage == true && - dynamic_cast(networkMessage) != NULL) { - - safeMutexSlotBroadCastAccessor.ReleaseLock(); - NetworkMessageText *txtMsg = dynamic_cast(networkMessage); - if(txtMsg != NULL) { - NetworkMessageText *msgCopy = txtMsg->getCopy(); - queueBroadcastMessage(msgCopy, excludeSlot); - } - return; - } - else { - inBroadcastMessage = true; - safeMutexSlotBroadCastAccessor.ReleaseLock(true); - } - - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(NULL,CODE_AT_LINE_X(slotIndex)); - if(slotIndex != lockedSlotIndex) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] i = %d, lockedSlotIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,slotIndex,lockedSlotIndex); - safeMutexSlot.setMutex(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); + NetworkMessageCommandList networkMessageCommandList(frameCount); + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + networkMessageCommandList.setNetworkPlayerFactionCRC(index, this->getNetworkPlayerFactionCRC(index)); } - ConnectionSlot* connectionSlot= slots[slotIndex]; - - if(slotIndex != excludeSlot && connectionSlot != NULL) { - if(connectionSlot->isConnected()) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] before sendMessage\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - connectionSlot->sendMessage(networkMessage); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after sendMessage\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - if(gameHasBeenInitiated == true && connectionSlot->isConnected() == false) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 before removeSlot for slot# %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,slotIndex); - - if(this->getAllowInGameConnections() == false) { - removeSlot(slotIndex,slotIndex); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #1 after removeSlot for slot# %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,slotIndex); - } - } - else if(slotIndex == excludeSlot && gameHasBeenInitiated == true && - connectionSlot != NULL && connectionSlot->isConnected() == false) { - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 before removeSlot for slot# %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,slotIndex); - - if(this->getAllowInGameConnections() == false) { - removeSlot(slotIndex,slotIndex); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] #2 after removeSlot for slot# %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,slotIndex); - } - } - - safeMutexSlotBroadCastAccessor.Lock(); - - inBroadcastMessage = false; - - safeMutexSlotBroadCastAccessor.ReleaseLock(); - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - - MutexSafeWrapper safeMutexSlotBroadCastAccessor(inBroadcastMessageThreadAccessor,CODE_AT_LINE); - inBroadcastMessage = false; - safeMutexSlotBroadCastAccessor.ReleaseLock(); - - string sMsg = ex.what(); - sendTextMessage(sMsg,-1, true, "", lockedSlotIndex); - } -} - -void ServerInterface::broadcastMessageToConnectedClients(NetworkMessage *networkMessage, int excludeSlot) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - try { - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot= slots[slotIndex]; - - if(slotIndex != excludeSlot && connectionSlot != NULL) { - if(connectionSlot->isConnected()) { - connectionSlot->sendMessage(networkMessage); - } - } - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] ERROR [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - DisplayErrorMessage(ex.what()); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); -} - -void ServerInterface::updateListen() { - if(gameHasBeenInitiated == true && - this->getAllowInGameConnections() == false) { - return; - } - - int openSlotCount = 0; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - - bool isSlotOpen = (slots[slotIndex] != NULL && slots[slotIndex]->isConnected() == false); - if(isSlotOpen == true) { - ++openSlotCount; - } - } - - serverSocket.listen(openSlotCount); -} - -int ServerInterface::getOpenSlotCount() { - int openSlotCount = 0; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - - bool isSlotOpen = (slots[slotIndex] != NULL && slots[slotIndex]->isConnected() == false); - if(isSlotOpen == true) { - ++openSlotCount; - } - } - return openSlotCount; -} - -int ServerInterface::getGameSettingsUpdateCount() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START gameSettingsUpdateCount = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,gameSettingsUpdateCount); - - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - int result = gameSettingsUpdateCount; - safeMutex.ReleaseLock(); - return result; -} - -void ServerInterface::validateGameSettings(GameSettings *serverGameSettings) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - string mapFile = serverGameSettings->getMap(); - //printf("Trying to set map to [%s]. Current map is [%s]\n",serverGameSettings->getMap().c_str(),gameSettings.getMap().c_str()); - if(gameSettings.getMapFilter()!=serverGameSettings->getMapFilter()){ - if( playerSortedMaps[serverGameSettings->getMapFilter()].size()==0){ - serverGameSettings->setMapFilter(0); - } - } - int playerIndex=serverGameSettings->getMapFilter(); - if(find(playerSortedMaps[playerIndex].begin(),playerSortedMaps[playerIndex].end(),mapFile) == playerSortedMaps[playerIndex].end()) { - // switch to closest map - string foundMap = ""; - for (int i = 0 ;i < (int)playerSortedMaps[playerIndex].size(); ++i) { - foundMap=playerSortedMaps[playerIndex][i]; - if(toLower(foundMap)>toLower(serverGameSettings->getMap())){ - break; - } - } - printf("map %s not found on this server. Switching to map %s\n",serverGameSettings->getMap().c_str(),foundMap.c_str()); - serverGameSettings->setMap(foundMap); - } - Checksum checksum; - string file = Config::getMapPath(serverGameSettings->getMap(),"",false); - checksum.addFile(file); - serverGameSettings->setMapCRC(checksum.getSum()); - - string tilesetFile = serverGameSettings->getTileset(); - if(find(tilesetFiles.begin(),tilesetFiles.end(),tilesetFile) == tilesetFiles.end()) { - printf("Reverting tileset from [%s] to [%s]\n",serverGameSettings->getTileset().c_str(),gameSettings.getTileset().c_str()); - - serverGameSettings->setTileset(gameSettings.getTileset()); - serverGameSettings->setTilesetCRC(gameSettings.getTilesetCRC()); - } - - string techtreeFile = serverGameSettings->getTech(); - if(find(techTreeFiles.begin(),techTreeFiles.end(),techtreeFile) == techTreeFiles.end()) { - printf("Reverting tech from [%s] to [%s]\n",serverGameSettings->getTech().c_str(),gameSettings.getTech().c_str()); - - serverGameSettings->setTech(gameSettings.getTech()); - serverGameSettings->setTechCRC(gameSettings.getTechCRC()); - } -} - -void ServerInterface::setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck) { - MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START gameSettingsUpdateCount = %d, waitForClientAck = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,gameSettingsUpdateCount,waitForClientAck); - - if(serverGameSettings->getScenario() == "") { - string mapFile = serverGameSettings->getMap(); - if(find(mapFiles.begin(),mapFiles.end(),mapFile) == mapFiles.end()) { - printf("Reverting map from [%s] to [%s]\n",serverGameSettings->getMap().c_str(),gameSettings.getMap().c_str()); - - serverGameSettings->setMapFilter(gameSettings.getMapFilter()); - serverGameSettings->setMap(gameSettings.getMap()); - serverGameSettings->setMapCRC(gameSettings.getMapCRC()); - } - - string tilesetFile = serverGameSettings->getTileset(); - if(find(tilesetFiles.begin(),tilesetFiles.end(),tilesetFile) == tilesetFiles.end()) { - printf("Reverting tileset from [%s] to [%s]\n",serverGameSettings->getTileset().c_str(),gameSettings.getTileset().c_str()); - - serverGameSettings->setTileset(gameSettings.getTileset()); - serverGameSettings->setTilesetCRC(gameSettings.getTilesetCRC()); - } - - string techtreeFile = serverGameSettings->getTech(); - if(find(techTreeFiles.begin(),techTreeFiles.end(),techtreeFile) == techTreeFiles.end()) { - printf("Reverting tech from [%s] to [%s]\n",serverGameSettings->getTech().c_str(),gameSettings.getTech().c_str()); - - serverGameSettings->setTech(gameSettings.getTech()); - serverGameSettings->setTechCRC(gameSettings.getTechCRC()); - } - } - - gameSettings = *serverGameSettings; - - if(getAllowGameDataSynchCheck() == true) { - if(waitForClientAck == true && gameSettingsUpdateCount > 0) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Waiting for client acks #1\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - time_t tStart = time(NULL); - bool gotAckFromAllClients = false; - while(gotAckFromAllClients == false && difftime((long int)time(NULL),tStart) <= 5) { - gotAckFromAllClients = true; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - //printf("===> START slot %d - About to setGameSettings #1\n",i); - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot = slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected()) { - if(connectionSlot->getReceivedNetworkGameStatus() == false) { - gotAckFromAllClients = false; - } - - connectionSlot->update(true,slotIndex); - } - //printf("===> END slot %d - About to setGameSettings #1\n",i); - } - } - } - - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot = slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected()) { - connectionSlot->setReceivedNetworkGameStatus(false); - } - } - - NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData(getGameSettings()); - broadcastMessageToConnectedClients(&networkMessageSynchNetworkGameData); - - if(waitForClientAck == true) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Waiting for client acks #2\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); - - time_t tStart = time(NULL); - bool gotAckFromAllClients = false; - while(gotAckFromAllClients == false && difftime((long int)time(NULL),tStart) <= 5) { - gotAckFromAllClients = true; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - //printf("===> START slot %d - About to setGameSettings 2\n",slotIndex); - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot = slots[slotIndex]; - if(connectionSlot != NULL && connectionSlot->isConnected()) { - if(connectionSlot->getReceivedNetworkGameStatus() == false) { - gotAckFromAllClients = false; - } - - connectionSlot->update(true,slotIndex); - } - //printf("===> END slot %d - About to setGameSettings 2\n",slotIndex); - } - } - } - - } - gameSettingsUpdateCount++; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); -} - -void ServerInterface::close() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); -} - -string ServerInterface::getHumanPlayerName(int index) { - string result = Config::getInstance().getString("NetPlayerName", Socket::getHostName().c_str()); - if(index >= 0 || gameSettings.getThisFactionIndex() >= 0) { - if(index < 0) { - index = gameSettings.getThisFactionIndex(); - } - if(gameSettings.getNetworkPlayerName(index) != "") { - result = gameSettings.getNetworkPlayerName(index); - } - } - - return result; -} - -int ServerInterface::getHumanPlayerIndex() const { - return gameSettings.getStartLocationIndex(gameSettings.getThisFactionIndex()); -} - -std::map ServerInterface::publishToMasterserver() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - int slotCountUsed = 1; - int slotCountHumans = 1; - int slotCountConnectedPlayers = 1; - - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - slotCountUsed = 0; - slotCountHumans = 0; - slotCountConnectedPlayers = 0; - } - - Config & config = Config::getInstance(); - std::map < string, string > publishToServerInfo; - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - if(slots[slotIndex] != NULL) { - slotCountUsed++; - slotCountHumans++; - ConnectionSlot* connectionSlot= slots[slotIndex]; - if((connectionSlot!=NULL) && (connectionSlot->isConnected())) { - slotCountConnectedPlayers++; - } - } - } - publishToServerInfo["uuid"] = Config::getInstance().getString("PlayerId",""); - publishToServerInfo["glestVersion"] = glestVersionString; - publishToServerInfo["platform"] = getPlatformNameString(); - publishToServerInfo["binaryCompileDate"] = getCompileDateTime(); - publishToServerInfo["serverTitle"] = this->getGameSettings()->getGameName(); - publishToServerInfo["tech"] = this->getGameSettings()->getTech(); - publishToServerInfo["map"] = this->getGameSettings()->getMap(); - publishToServerInfo["tileset"] = this->getGameSettings()->getTileset(); - - bool updateSlots = true; - MutexSafeWrapper safeMutex2(gameStatsThreadAccessor,CODE_AT_LINE); - if(gameStats != NULL) { - for(int factionIndex = 0; factionIndex < gameStats->getFactionCount(); ++factionIndex) { - if(gameStats->getVictory(factionIndex) == true) { - updateSlots = false; - break; - } - } - } - safeMutex2.ReleaseLock(); - - if(updateSlots == true) { - publishToServerInfo["activeSlots"] = intToStr(slotCountUsed); - publishToServerInfo["networkSlots"] = intToStr(slotCountHumans); - publishToServerInfo["connectedClients"] = intToStr(slotCountConnectedPlayers); - } - - string serverPort = config.getString("PortServer", intToStr(GameConstants::serverPort).c_str()); - string externalPort = config.getString("PortExternal", serverPort.c_str()); - publishToServerInfo["externalconnectport"] = externalPort; - publishToServerInfo["privacyPlease"] = intToStr(config.getBool("PrivacyPlease","false")); - publishToServerInfo["gameStatus"] = intToStr(game_status_in_progress); - - if(publishToMasterserverThread == NULL) { - publishToServerInfo["gameCmd"] = "gameOver"; - publishToServerInfo["gameStatus"] = intToStr(game_status_finished); - } - - //printf("Host game id = %s\n",this->getGameSettings()->getGameUUID().c_str()); - publishToServerInfo["gameUUID"] = this->getGameSettings()->getGameUUID(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - return publishToServerInfo; -} - -std::map ServerInterface::publishToMasterserverStats() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutex(gameStatsThreadAccessor,CODE_AT_LINE); - std::map < string, string > publishToServerInfo; - if(gameStats != NULL) { - publishToServerInfo["gameUUID"] = this->getGameSettings()->getGameUUID(); - publishToServerInfo["tech"] = this->getGameSettings()->getTech(); - publishToServerInfo["factionCount"] = intToStr(gameStats->getFactionCount()); - publishToServerInfo["framesPlayed"] = intToStr(gameStats->getFramesPlayed()); - publishToServerInfo["framesToCalculatePlaytime"] = intToStr(gameStats->getFramesToCalculatePlaytime()); - publishToServerInfo["maxConcurrentUnitCount"] = intToStr(gameStats->getMaxConcurrentUnitCount()); - publishToServerInfo["totalEndGameConcurrentUnitCount"] = intToStr(gameStats->getTotalEndGameConcurrentUnitCount()); - publishToServerInfo["isHeadlessServer"] = intToStr(gameStats->getIsMasterserverMode()); - - for(int factionIndex = 0; factionIndex < gameStats->getFactionCount(); ++factionIndex) { - publishToServerInfo["factionIndex_" + intToStr(factionIndex)] = intToStr(factionIndex); - publishToServerInfo["controlType_" + intToStr(factionIndex)] = intToStr(gameStats->getControl(factionIndex)); - publishToServerInfo["resourceMultiplier_" + intToStr(factionIndex)] = floatToStr(gameStats->getResourceMultiplier(factionIndex)); - publishToServerInfo["factionTypeName_" + intToStr(factionIndex)] = gameStats->getFactionTypeName(factionIndex); - publishToServerInfo["personalityType_" + intToStr(factionIndex)] = intToStr(gameStats->getPersonalityType(factionIndex)); - publishToServerInfo["teamIndex_" + intToStr(factionIndex)] = intToStr(gameStats->getTeam(factionIndex)); - publishToServerInfo["wonGame_" + intToStr(factionIndex)] = intToStr(gameStats->getVictory(factionIndex)); - publishToServerInfo["killCount_" + intToStr(factionIndex)] = intToStr(gameStats->getKills(factionIndex)); - publishToServerInfo["enemyKillCount_" + intToStr(factionIndex)] = intToStr(gameStats->getEnemyKills(factionIndex)); - publishToServerInfo["deathCount_" + intToStr(factionIndex)] = intToStr(gameStats->getDeaths(factionIndex)); - publishToServerInfo["unitsProducedCount_" + intToStr(factionIndex)] = intToStr(gameStats->getUnitsProduced(factionIndex)); - publishToServerInfo["resourceHarvestedCount_" + intToStr(factionIndex)] = intToStr(gameStats->getResourcesHarvested(factionIndex)); - publishToServerInfo["playerName_" + intToStr(factionIndex)] = gameStats->getPlayerName(factionIndex); - publishToServerInfo["quitBeforeGameEnd_" + intToStr(factionIndex)] = intToStr(gameStats->getPlayerLeftBeforeEnd(factionIndex)); - publishToServerInfo["quitTime_" + intToStr(factionIndex)] = intToStr(gameStats->getTimePlayerLeft(factionIndex)); - publishToServerInfo["playerUUID_" + intToStr(factionIndex)] = this->getGameSettings()->getNetworkPlayerUUID(factionIndex); - publishToServerInfo["platform_" + intToStr(factionIndex)] = this->getGameSettings()->getNetworkPlayerPlatform(factionIndex); - } - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - return publishToServerInfo; -} - -void ServerInterface::setGameStats(Stats *stats) { - if(stats == NULL) { - return; - } - MutexSafeWrapper safeMutex(gameStatsThreadAccessor,CODE_AT_LINE); - if(gameStats == NULL) { - gameStats = new Stats(); - } - *gameStats = *stats; -} - -void ServerInterface::simpleTask(BaseThread *callingThread,void *userdata) { - MutexSafeWrapper safeMutex(masterServerThreadAccessor,CODE_AT_LINE); - - if(difftime((long int)time(NULL),lastMasterserverHeartbeatTime) >= MASTERSERVER_HEARTBEAT_GAME_STATUS_SECONDS) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Checking to see masterserver needs an update of the game status [%d] callingThread [%p] publishToMasterserverThread [%p]\n",needToRepublishToMasterserver,callingThread,publishToMasterserverThread); - - lastMasterserverHeartbeatTime = time(NULL); - if(needToRepublishToMasterserver == true) { - try { - if(Config::getInstance().getString("Masterserver","") != "") { - string request = Config::getInstance().getString("Masterserver"); - if(request != "") { - endPathWithSlash(request,false); - } - request += "addServerInfo.php?"; - - std::map newPublishToServerInfo = publishToMasterserver(); - - CURL *handle = SystemFlags::initHTTP(); - for(std::map::const_iterator iterMap = newPublishToServerInfo.begin(); - iterMap != newPublishToServerInfo.end(); ++iterMap) { - - request += iterMap->first; - request += "="; - request += SystemFlags::escapeURL(iterMap->second,handle); - request += "&"; - } - - //printf("The Host request is:\n%s\n",request.c_str()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("The Host request is:\n%s\n",request.c_str()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d] the request is:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,request.c_str()); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Calling masterserver [%s]...\n",request.c_str()); - - std::string serverInfo = SystemFlags::getHTTP(request,handle); - //printf("Result:\n%s\n",serverInfo .c_str()); - - string requestStats = Config::getInstance().getString("Masterserver"); - if(requestStats != "") { - endPathWithSlash(requestStats,false); - } - requestStats += "addGameStats.php?"; - - std::map newPublishToServerInfoStats = publishToMasterserverStats(); - if(newPublishToServerInfoStats.empty() == false) { - for(std::map::const_iterator iterMap = newPublishToServerInfoStats.begin(); - iterMap != newPublishToServerInfoStats.end(); ++iterMap) { - - requestStats += iterMap->first; - requestStats += "="; - requestStats += SystemFlags::escapeURL(iterMap->second,handle); - requestStats += "&"; - } - - //printf("The Host stats request is:\n%s\n",requestStats.c_str()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("The Host request is:\n%s\n",requestStats.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d] the request is:\n%s\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,requestStats.c_str()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Calling masterserver [%s]...\n",requestStats.c_str()); - - std::string serverInfoStats = SystemFlags::getHTTP(requestStats,handle); - //printf("Result:\n%s\n",serverInfoStats .c_str()); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d] the result is:\n'%s'\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,serverInfoStats.c_str()); - } - - SystemFlags::cleanupHTTP(&handle); - - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("Done Calling masterserver\n"); - - //printf("the result is:\n'%s'\n",serverInfo.c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line %d] the result is:\n'%s'\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,serverInfo.c_str()); - } - else { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line %d] error, no masterserver defined!\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - } - } - catch(const exception &ex) { - SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line %d] error during game status update: [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ex.what()); - } - } - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - DumpStatsToLog(false); - } - } - if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { - //printf("Attempt Accept\n"); - if(serverSocketAdmin != NULL) { - Socket *cli = serverSocketAdmin->accept(false); - if(cli != NULL) { - printf("Got status request connection, dumping info...\n"); - - string data = DumpStatsToLog(true); - cli->send(data.c_str(),(int)data.length()); - cli->disconnectSocket(); - } - } - } -} - -std::string ServerInterface::DumpStatsToLog(bool dumpToStringOnly) const { - string headlessLogFile = Config::getInstance().getString("HeadlessLogFile","headless.log"); - if(getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { - headlessLogFile = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + headlessLogFile ; - } - else { - string userData = Config::getInstance().getString("UserData_Root",""); - if(userData != "") { - endPathWithSlash(userData); - } - headlessLogFile = userData + headlessLogFile ; - } - - ostringstream out; - out << "=========================================" << std::endl; - out << "Headless Server Current Game information:" << std::endl; - out << "=========================================" << std::endl; - - int connectedSlotCount = 0; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *slot = slots[slotIndex]; - if(slot != NULL) { - - connectedSlotCount++; - out << "Network connection for index: " << slotIndex << std::endl; - out << "------------------------------" << std::endl; - out << "Connected: " << boolToStr(slot->isConnected()) << std::endl; - out << "Handshake received: " << boolToStr(slot->getConnectHasHandshaked()) << std::endl; - if(slot->isConnected() == true) { - - time_t connectTime = slot->getConnectedTime(); - //struct tm *loctime = localtime (&connectTime); - struct tm loctime = threadsafe_localtime(connectTime); - char szBuf[8096] = ""; - strftime(szBuf,100,"%Y-%m-%d %H:%M:%S",&loctime); - - const int HOURS_IN_DAY = 24; - const int MINUTES_IN_HOUR = 60; - const int SECONDS_IN_MINUTE = 60; - int InSeconds = difftime((long int)time(NULL),slot->getConnectedTime()); - // compute seconds - int seconds = InSeconds % SECONDS_IN_MINUTE ; - // throw away seconds used in previous statement and convert to minutes - int InMinutes = InSeconds / SECONDS_IN_MINUTE ; - // compute minutes - int minutes = InMinutes % MINUTES_IN_HOUR ; - - // throw away minutes used in previous statement and convert to hours - int InHours = InMinutes / MINUTES_IN_HOUR ; - // compute hours - int hours = InHours % HOURS_IN_DAY ; - - out << "Connected at: " << szBuf << std::endl; - out << "Connection duration: " << hours << " hours " << minutes << " minutes " << seconds << " seconds." << std::endl; - out << "Player Index: " << slot->getPlayerIndex() << std::endl; - out << "IP Address: " << slot->getIpAddress() << std::endl; - out << "Player name: " << slot->getName() << std::endl; - out << "Player uuid: " << slot->getUUID() << std::endl; - out << "Language: " << slot->getNetworkPlayerLanguage() << std::endl; - out << "Game Version: " << slot->getVersionString() << std::endl; - out << "Session id: " << slot->getSessionKey() << std::endl; - out << "Socket id: " << slot->getSocketId() << std::endl; - } - } - } - out << "Total Slot Count: " << connectedSlotCount << std::endl; - out << "=========================================" << std::endl; - - std::string result = out.str(); - - if(dumpToStringOnly == false) { - -#if defined(WIN32) && !defined(__MINGW32__) - FILE *fp = _wfopen(utf8_decode(headlessLogFile ).c_str(), L"w"); - std::ofstream logFile(fp); -#else - std::ofstream logFile; - logFile.open(headlessLogFile .c_str(), ios_base::out | ios_base::trunc); -#endif - logFile << result; - logFile.close(); -#if defined(WIN32) && !defined(__MINGW32__) - if(fp) { - fclose(fp); - } -#endif - } - - return result; -} - -void ServerInterface::notifyBadClientConnectAttempt(string ipAddress) { - //printf("In [%s::%s Line: %d] ipAddress [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ipAddress.c_str()); - - if(badClientConnectIPList.find(ipAddress) == badClientConnectIPList.end()) { - badClientConnectIPList[ipAddress] = make_pair(0,(long int)time(NULL)); - } - - pair &lastBadConnectionAttempt = badClientConnectIPList[ipAddress]; - - const uint64 BLOCK_BAD_CLIENT_CONNECT_MAX_SECONDS = Config::getInstance().getInt("BlockBadClientConnectMaxSeconds", "60"); - const uint64 BLOCK_BAD_CLIENT_CONNECT_MAX_ATTEMPTS = Config::getInstance().getInt("BlockBadClientConnectMaxAttempts", "6"); - bool addToBlockedClientsList = false; - - if(difftime((long int)time(NULL),lastBadConnectionAttempt.second) <= BLOCK_BAD_CLIENT_CONNECT_MAX_SECONDS) { - - if(lastBadConnectionAttempt.first+1 > BLOCK_BAD_CLIENT_CONNECT_MAX_ATTEMPTS) { - addToBlockedClientsList = true; - } - } - else { - // Reset after x seconds - lastBadConnectionAttempt.first = 0; - } - - if(this->getAllowInGameConnections() == true) { - printf("notifyBadClientConnectAttempt() #1: %s!\n",ipAddress.c_str()); - } - - if(addToBlockedClientsList == true) { - serverSocket.addIPAddressToBlockedList(ipAddress); - } - - lastBadConnectionAttempt.first++; - lastBadConnectionAttempt.second = time(NULL); -} - -bool ServerInterface::getStartInGameConnectionLaunch() { - bool result = false; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - if(slots[slotIndex] != NULL) { - - ConnectionSlot *slot = slots[slotIndex]; - if(slot->getStartInGameConnectionLaunch() == true) { - result = true; - break; - } - } - } - return result; -} - -bool ServerInterface::getPauseForInGameConnection() { - bool result = false; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - if(slots[slotIndex] != NULL) { - - ConnectionSlot *slot = slots[slotIndex]; - if(slot->getPauseForInGameConnection() == true) { - result = true; - break; - } - } - } - return result; -} - -bool ServerInterface::getUnPauseForInGameConnection() { - bool result = false; - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - if(slots[slotIndex] != NULL) { - - ConnectionSlot *slot = slots[slotIndex]; - if(slot->isConnected() == true) { - if(slot->isReady() == true) { - result = true; - if(slot->getUnPauseForInGameConnection() == false) { - result = false; - break; - } - } - else { - result = false; + while (requestedCommands.empty() == false) { + // First add the command to the broadcast list (for all clients) + if (networkMessageCommandList.addCommand(&requestedCommands.back())) { + // Add the command to the local server command list + pendingCommands.push_back(requestedCommands.back()); + requestedCommands.pop_back(); + } else { break; } } + + try { + // Possible cause of out of synch since we have more commands that need + // to be sent in this frame + if (requestedCommands.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, requestedCommands.size()); + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] WARNING / ERROR, requestedCommands.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, requestedCommands.size()); + + string sMsg = "may go out of synch: server requestedCommands.size() = " + intToStr(requestedCommands.size()); + sendTextMessage(sMsg, -1, true, ""); + } + + // broadcast commands + // If we have more than 0 commands to send, automatically broadcast them + bool sendBroadcastMessage = (networkMessageCommandList.getCommandCount() > 0); + if (sendBroadcastMessage == false) { + + // Is auto pause due to lag NOT enabled + if (this->getClientsAutoPausedDueToLag() == false) { + + // ****NOTE: + // We always need to broadcast when not pause as clients + // look for broadcasts every network frame. + sendBroadcastMessage = true; + } + // Auto pause is enabled due to client lagging, only send empty command + // broadcasts every MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS + else if (this->getClientsAutoPausedDueToLag() == true && + (lastBroadcastCommandsTimer.isStarted() == false || + lastBroadcastCommandsTimer.getMillis() >= MAX_EMPTY_NETWORK_COMMAND_LIST_BROADCAST_INTERVAL_MILLISECONDS)) { + + sendBroadcastMessage = true; + } + } + + if (sendBroadcastMessage == true) { + + if (lastBroadcastCommandsTimer.isStarted() == false) { + lastBroadcastCommandsTimer.start(); + } else { + lastBroadcastCommandsTimer.stop(); + lastBroadcastCommandsTimer.reset(); + lastBroadcastCommandsTimer.start(); + } + broadcastMessage(&networkMessageCommandList); + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + DisplayErrorMessage(ex.what()); + } } - } - if(result == true) { - resumeGameStartTime = time(NULL); - } - return result; -} -ConnectionSlot * ServerInterface::findSlotForUUID(string uuid, bool unConnectedOnly) { - ConnectionSlot *result = NULL; - if(uuid != "") { - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + bool ServerInterface::shouldDiscardNetworkMessage(NetworkMessageType networkMessageType, + ConnectionSlot *connectionSlot) { + bool discard = false; + if (connectionSlot != NULL) { + switch (networkMessageType) { + case nmtIntro: + { + discard = true; + NetworkMessageIntro msg = NetworkMessageIntro(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtPing: + { + discard = true; + NetworkMessagePing msg = NetworkMessagePing(); + connectionSlot->receiveMessage(&msg); + lastPingInfo = msg; + } + break; - MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - ConnectionSlot *connectionSlot= slots[slotIndex]; + case nmtLaunch: + { + discard = true; + NetworkMessageLaunch msg = NetworkMessageLaunch(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtText: + { + discard = true; + NetworkMessageText netMsg = NetworkMessageText(); + connectionSlot->receiveMessage(&netMsg); - if(connectionSlot != NULL) { - if(connectionSlot->getUUID() == uuid) { - if(unConnectedOnly == false || - (unConnectedOnly == true && connectionSlot->isConnected() == false)) { + ChatMsgInfo msg(netMsg.getText().c_str(), netMsg.getTeamIndex(), netMsg.getPlayerIndex(), netMsg.getTargetLanguage()); + this->addChatInfo(msg); - if(result == NULL || - (result->getConnectedTime() > connectionSlot->getConnectedTime())) + string newChatText = msg.chatText.c_str(); + //string newChatSender = msg.chatSender.c_str(); + int newChatTeamIndex = msg.chatTeamIndex; + int newChatPlayerIndex = msg.chatPlayerIndex; + string newChatLanguage = msg.targetLanguage.c_str(); - result = connectionSlot; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #1 about to broadcast nmtText chatText [%s] chatTeamIndex = %d, newChatPlayerIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, newChatText.c_str(), newChatTeamIndex, newChatPlayerIndex); + + NetworkMessageText networkMessageText(newChatText.c_str(), newChatTeamIndex, newChatPlayerIndex, newChatLanguage); + broadcastMessage(&networkMessageText, connectionSlot->getPlayerIndex()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] after broadcast nmtText chatText [%s] chatTeamIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, newChatText.c_str(), newChatTeamIndex); + + } + break; + + case nmtMarkCell: + { + discard = true; + NetworkMessageMarkCell networkMessageMarkCell; + connectionSlot->receiveMessage(&networkMessageMarkCell); + + MarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + + this->addMarkedCell(msg); + + NetworkMessageMarkCell networkMessageMarkCellBroadcast( + networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex(), + networkMessageMarkCell.getText().c_str(), + networkMessageMarkCell.getPlayerIndex()); + broadcastMessage(&networkMessageMarkCellBroadcast, connectionSlot->getPlayerIndex()); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); + + } + break; + + case nmtUnMarkCell: + { + discard = true; + NetworkMessageUnMarkCell networkMessageMarkCell; + connectionSlot->receiveMessage(&networkMessageMarkCell); + + UnMarkedCell msg(networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + + this->addUnMarkedCell(msg); + + NetworkMessageUnMarkCell networkMessageMarkCellBroadcast( + networkMessageMarkCell.getTarget(), + networkMessageMarkCell.getFactionIndex()); + broadcastMessage(&networkMessageMarkCellBroadcast, connectionSlot->getPlayerIndex()); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatText [%s] chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatText.c_str(),newChatTeamIndex); + + } + break; + case nmtHighlightCell: + { + discard = true; + NetworkMessageHighlightCell networkMessageHighlightCell; + connectionSlot->receiveMessage(&networkMessageHighlightCell); + + MarkedCell msg(networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex(), + "none", -1); + + this->setHighlightedCell(msg); + + NetworkMessageHighlightCell networkMessageHighlightCellBroadcast( + networkMessageHighlightCell.getTarget(), + networkMessageHighlightCell.getFactionIndex()); + broadcastMessage(&networkMessageHighlightCellBroadcast, connectionSlot->getPlayerIndex()); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] after broadcast nmtMarkCell chatTeamIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,newChatTeamIndex); + + } + break; + + case nmtSynchNetworkGameData: + { + discard = true; + NetworkMessageSynchNetworkGameData msg = NetworkMessageSynchNetworkGameData(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataStatus: + { + discard = true; + NetworkMessageSynchNetworkGameDataStatus msg = NetworkMessageSynchNetworkGameDataStatus(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataFileCRCCheck: + { + discard = true; + NetworkMessageSynchNetworkGameDataFileCRCCheck msg = NetworkMessageSynchNetworkGameDataFileCRCCheck(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtSynchNetworkGameDataFileGet: + { + discard = true; + NetworkMessageSynchNetworkGameDataFileGet msg = NetworkMessageSynchNetworkGameDataFileGet(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtSwitchSetupRequest: + { + discard = true; + SwitchSetupRequest msg = SwitchSetupRequest(); + connectionSlot->receiveMessage(&msg); + } + break; + case nmtPlayerIndexMessage: + { + discard = true; + PlayerIndexMessage msg = PlayerIndexMessage(0); + connectionSlot->receiveMessage(&msg); + } + break; + } + } + return discard; + } + + void ServerInterface::waitUntilReady(Checksum *checksum) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s] START\n", __FUNCTION__); + Logger & logger = Logger::getInstance(); + gameHasBeenInitiated = true; + Chrono chrono; + chrono.start(); + + bool allReady = false; + + if (Config::getInstance().getBool("EnableGameServerLoadCancel", "false") == true) { + logger.setCancelLoadingEnabled(true); + } + + Lang &lang = Lang::getInstance(); + uint64 waitLoopIterationCount = 0; + uint64 MAX_LOOP_COUNT_BEFORE_SLEEP = 10; + MAX_LOOP_COUNT_BEFORE_SLEEP = Config::getInstance().getInt("NetworkServerLoopGameLoadingCap", intToStr(MAX_LOOP_COUNT_BEFORE_SLEEP).c_str()); + if (MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { + MAX_LOOP_COUNT_BEFORE_SLEEP = 1; + } + int sleepMillis = Config::getInstance().getInt("NetworkServerLoopGameLoadingCapSleepMillis", "10"); + int64 lastStatusUpdate = 0; + + while (exitServer == false && + allReady == false && + logger.getCancelLoading() == false) { + + waitLoopIterationCount++; + if (waitLoopIterationCount > 0 && + waitLoopIterationCount % MAX_LOOP_COUNT_BEFORE_SLEEP == 0) { + + sleep(sleepMillis); + waitLoopIterationCount = 0; + } + vector waitingForHosts; + allReady = true; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot* connectionSlot = slots[index]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + if (connectionSlot->isReady() == false) { + + NetworkMessageType networkMessageType = connectionSlot->getNextMessageType(); + + // consume old messages from the lobby + bool discarded = shouldDiscardNetworkMessage(networkMessageType, connectionSlot); + if (discarded == false) { + + NetworkMessageReady networkMessageReady; + if (networkMessageType == nmtReady && + connectionSlot->receiveMessage(&networkMessageReady)) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s] networkMessageType==nmtReady\n", __FUNCTION__); + + connectionSlot->setReady(); + connectionSlot->setGameStarted(true); + } else if (networkMessageType != nmtInvalid) { + string sErr = "Unexpected network message: " + intToStr(networkMessageType); + sendTextMessage(sErr, -1, true, "", index); + DisplayErrorMessage(sErr); + logger.setCancelLoading(false); + return; + } + } + waitingForHosts.push_back(connectionSlot->getName()); + allReady = false; + } + } + } + + //check for timeout + if (allReady == false) { + if (chrono.getMillis() > readyWaitTimeout) { + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + string sErr = "Timeout waiting for clients."; + if (lang.hasString("TimeoutWaitingForClients") == true) { + sErr = lang.getString("TimeoutWaitingForClients", languageList[langIndex]); + } + bool localEcho = lang.isLanguageLocal(languageList[langIndex]); + sendTextMessage(sErr, -1, localEcho, languageList[langIndex]); + if (localEcho == true) { + DisplayErrorMessage(sErr); + } + } + logger.setCancelLoading(false); + return; + } else { + if (chrono.getMillis() - lastStatusUpdate > 200) { + lastStatusUpdate = chrono.getMillis(); + + string waitForHosts = ""; + for (int hostIndex = 0; hostIndex < (int) waitingForHosts.size(); hostIndex++) { + if (waitForHosts != "") { + waitForHosts += ", "; + } + waitForHosts += waitingForHosts[hostIndex]; + } + + char szBuf[8096] = ""; + string updateTextFormat = lang.getString("NetworkGameServerLoadStatus"); + if (updateTextFormat == "" || + updateTextFormat[0] == '?') { + + updateTextFormat = "Waiting for network: %lld seconds elapsed (maximum wait time: %d seconds)"; + } + snprintf(szBuf, 8096, updateTextFormat.c_str(), (long long int)(chrono.getMillis() / 1000), int(readyWaitTimeout / 1000)); + + char szBuf1[8096] = ""; + string statusTextFormat = lang.getString("NetworkGameStatusWaiting"); + if (statusTextFormat == "" || + statusTextFormat[0] == '?') { + + statusTextFormat = "Waiting for players: %s"; + } + snprintf(szBuf1, 8096, statusTextFormat.c_str(), waitForHosts.c_str()); + + logger.add(szBuf, true, szBuf1); + + uint32 loadingStatus = nmls_NONE; + //send ready message after, so clients start delayed + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + // FIXME: maxPlayers is declared in game_constants.h. Code such as + // this must be updated when maxPlayers is changed + // + // The PLAYER vars are enum'ed in network_message.h + switch (slotIndex) { + case 0: + loadingStatus |= nmls_PLAYER1_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER1_READY; + } + break; + case 1: + loadingStatus |= nmls_PLAYER2_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER2_READY; + } + break; + case 2: + loadingStatus |= nmls_PLAYER3_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER3_READY; + } + break; + case 3: + loadingStatus |= nmls_PLAYER4_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER4_READY; + } + break; + case 4: + loadingStatus |= nmls_PLAYER5_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER5_READY; + } + break; + case 5: + loadingStatus |= nmls_PLAYER6_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER6_READY; + } + break; + case 6: + loadingStatus |= nmls_PLAYER7_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER7_READY; + } + break; + case 7: + loadingStatus |= nmls_PLAYER8_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER8_READY; + } + break; + case 8: + loadingStatus |= nmls_PLAYER9_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER9_READY; + } + break; + case 9: + loadingStatus |= nmls_PLAYER10_CONNECTED; + if (connectionSlot->isReady()) { + loadingStatus |= nmls_PLAYER10_READY; + } + break; + + } + } + } + + // send loading status message + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot* connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + NetworkMessageLoadingStatus networkMessageLoadingStatus(loadingStatus); + connectionSlot->sendMessage(&networkMessageLoadingStatus); + } + } + + sleep(0); + } + } + } + + Shared::Platform::Window::handleEvent(); + } + + if (logger.getCancelLoading() == true) { + Lang &lang = Lang::getInstance(); + const vector languageList = this->gameSettings.getUniqueNetworkPlayerLanguages(); + for (unsigned int langIndex = 0; langIndex < languageList.size(); ++langIndex) { + + string sErr = lang.getString("GameCancelledByUser", languageList[langIndex]); + bool localEcho = lang.isLanguageLocal(languageList[langIndex]); + sendTextMessage(sErr, -1, localEcho, languageList[langIndex]); + + if (localEcho == true) { + DisplayErrorMessage(sErr); + } + } + quitGame(true); + logger.setCancelLoading(false); + return; + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s] PART B (telling client we are ready!\n", __FUNCTION__); + try { + //send ready message after, so clients start delayed + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot* connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected() == true) { + NetworkMessageReady networkMessageReady(checksum->getSum()); + connectionSlot->sendMessage(&networkMessageReady); + connectionSlot->setGameStarted(true); + } + } + + gameStartTime = time(NULL); + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] error detected [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + DisplayErrorMessage(ex.what()); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s] END\n", __FUNCTION__); + } + + void ServerInterface::processBroadCastMessageQueue() { + MutexSafeWrapper safeMutexSlot(broadcastMessageQueueThreadAccessor, CODE_AT_LINE); + if (broadcastMessageQueue.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] broadcastMessageQueue.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, broadcastMessageQueue.size()); + for (int index = 0; index < (int) broadcastMessageQueue.size(); ++index) { + pair &item = broadcastMessageQueue[index]; + if (item.first != NULL) { + this->broadcastMessage(item.first, item.second); + delete item.first; + } + item.first = NULL; + } + broadcastMessageQueue.clear(); + } + } + + void ServerInterface::queueBroadcastMessage(NetworkMessage *networkMessage, int excludeSlot) { + MutexSafeWrapper safeMutexSlot(broadcastMessageQueueThreadAccessor, CODE_AT_LINE); + pair item; + item.first = networkMessage; + item.second = excludeSlot; + broadcastMessageQueue.push_back(item); + } + + void ServerInterface::processTextMessageQueue() { + MutexSafeWrapper safeMutexSlot(textMessageQueueThreadAccessor, CODE_AT_LINE); + if (textMessageQueue.empty() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] textMessageQueue.size() = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, textMessageQueue.size()); + for (int index = 0; index < (int) textMessageQueue.size(); ++index) { + TextMessageQueue &item = textMessageQueue[index]; + sendTextMessage(item.text, item.teamIndex, item.echoLocal, item.targetLanguage); + } + textMessageQueue.clear(); + } + } + + void ServerInterface::queueTextMessage(const string & text, int teamIndex, + bool echoLocal, string targetLanguage) { + //printf("Line: %d text [%s]\n",__LINE__,text.c_str()); + + MutexSafeWrapper safeMutexSlot(textMessageQueueThreadAccessor, CODE_AT_LINE); + TextMessageQueue item; + item.text = text; + item.teamIndex = teamIndex; + item.echoLocal = echoLocal; + item.targetLanguage = targetLanguage; + textMessageQueue.push_back(item); + } + + void ServerInterface::sendTextMessage(const string & text, int teamIndex, + bool echoLocal, string targetLanguage) { + sendTextMessage(text, teamIndex, echoLocal, targetLanguage, -1); + } + + void ServerInterface::sendTextMessage(const string& text, int teamIndex, bool echoLocal, + string targetLanguage, int lockedSlotIndex) { + //printf("Line: %d text [%s] echoLocal = %d\n",__LINE__,text.c_str(),echoLocal); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] text [%s] teamIndex = %d, echoLocal = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, text.c_str(), teamIndex, echoLocal, lockedSlotIndex); + + NetworkMessageText networkMessageText(text, teamIndex, getHumanPlayerIndex(), targetLanguage); + broadcastMessage(&networkMessageText, -1, lockedSlotIndex); + + if (echoLocal == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + ChatMsgInfo msg(text.c_str(), teamIndex, networkMessageText.getPlayerIndex(), targetLanguage); + this->addChatInfo(msg); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex) { + sendMarkCellMessage(targetPos, factionIndex, note, playerIndex, -1); + } + + void ServerInterface::sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex, int lockedSlotIndex) { + NetworkMessageMarkCell networkMessageMarkCell(targetPos, factionIndex, note, playerIndex); + broadcastMessage(&networkMessageMarkCell, -1, lockedSlotIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex) { + sendHighlightCellMessage(targetPos, factionIndex, -1); + } + + void ServerInterface::sendHighlightCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex) { + NetworkMessageHighlightCell networkMessageHighlightCell(targetPos, factionIndex); + broadcastMessage(&networkMessageHighlightCell, -1, lockedSlotIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex) { + sendUnMarkCellMessage(targetPos, factionIndex, -1); + } + + void ServerInterface::sendUnMarkCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex) { + NetworkMessageUnMarkCell networkMessageMarkCell(targetPos, factionIndex); + broadcastMessage(&networkMessageMarkCell, -1, lockedSlotIndex); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::quitGame(bool userManuallyQuit) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + NetworkMessageQuit networkMessageQuit; + broadcastMessage(&networkMessageQuit); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + string ServerInterface::getNetworkStatus() { + Lang &lang = Lang::getInstance(); + string str = ""; + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + + str += intToStr(index) + ": "; + + if (connectionSlot != NULL) { + if (connectionSlot->isConnected()) { + int clientLagCount = connectionSlot->getCurrentLagCount(); + double lastClientCommandListTimeLag = difftime((long int) time(NULL), connectionSlot->getLastReceiveCommandListTime()); + //float pingTime = connectionSlot->getThreadedPingMS(connectionSlot->getIpAddress().c_str()); + char szBuf[8096] = ""; + snprintf(szBuf, 8096, ", lag = %d [%.2f]", clientLagCount, lastClientCommandListTimeLag); + str += connectionSlot->getName() + " [" + connectionSlot->getUUID() + "] " + string(szBuf); + } + } else { + str += lang.getString("NotConnected"); + } + + str += '\n'; + } + return str; + } + + bool ServerInterface::launchGame(const GameSettings *gameSettings) { + bool bOkToStart = true; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + for (int index = 0; exitServer == false && index < GameConstants::maxPlayers; ++index) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + if (connectionSlot != NULL && + (connectionSlot->getAllowDownloadDataSynch() == true || connectionSlot->getAllowGameDataSynchCheck() == true) && + connectionSlot->isConnected()) { + + if (connectionSlot->getNetworkGameDataSynchCheckOk() == false) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] map [%d] tile [%d] techtree [%d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, connectionSlot->getNetworkGameDataSynchCheckOkMap(), connectionSlot->getNetworkGameDataSynchCheckOkTile(), connectionSlot->getNetworkGameDataSynchCheckOkTech()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] map [%d] tile [%d] techtree [%d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, connectionSlot->getNetworkGameDataSynchCheckOkMap(), connectionSlot->getNetworkGameDataSynchCheckOkTile(), connectionSlot->getNetworkGameDataSynchCheckOkTech()); + + bOkToStart = false; + break; + } + } + } + if (bOkToStart == true) { + + bool useInGameBlockingClientSockets = Config::getInstance().getBool("EnableInGameBlockingSockets", "true"); + if (useInGameBlockingClientSockets == true) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + for (int index = 0; index < GameConstants::maxPlayers; ++index) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[index], CODE_AT_LINE_X(index)); + ConnectionSlot *connectionSlot = slots[index]; + if (connectionSlot != NULL && connectionSlot->isConnected()) { + connectionSlot->getSocket()->setBlock(true); + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + bool requiresUPNPTrigger = false; + for (int startIndex = 0; startIndex < GameConstants::maxPlayers; ++startIndex) { + + int factionIndex = gameSettings->getFactionIndexForStartLocation(startIndex); + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[startIndex], CODE_AT_LINE_X(startIndex)); + ConnectionSlot *connectionSlot = slots[startIndex]; + if ((connectionSlot == NULL || connectionSlot->isConnected() == false) && + this->getAllowInGameConnections() == true) { + + // Open slots for joining in progress game + if (gameSettings->getFactionControl(factionIndex) != ctClosed && + gameSettings->getFactionControl(factionIndex) != ctHuman) { + + //printf("Opening slot for in game connections for slot: %d, faction: %d\n",i,factionIndex); + if (connectionSlot == NULL) { + addSlot(startIndex); + connectionSlot = slots[startIndex]; + requiresUPNPTrigger = true; + } + connectionSlot->setCanAcceptConnections(true); + } + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + + if (this->getAllowInGameConnections() == false) { + serverSocket.stopBroadCastThread(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + + this->gameSettings = *gameSettings; + //printf("#1 Data synch: lmap %u ltile: %d ltech: %u\n",gameSettings->getMapCRC(),gameSettings->getTilesetCRC(),gameSettings->getTechCRC()); + + NetworkMessageLaunch networkMessageLaunch(gameSettings, nmtLaunch); + broadcastMessage(&networkMessageLaunch); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + + shutdownMasterserverPublishThread(); + MutexSafeWrapper safeMutex(masterServerThreadAccessor, CODE_AT_LINE); + lastMasterserverHeartbeatTime = 0; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ftpServer = %p\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ftpServer); + + if (this->getAllowInGameConnections() == false) { + shutdownFTPServer(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + + if (publishToMasterserverThread == NULL) { + if (needToRepublishToMasterserver == true || + GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + publishToMasterserverThread = new SimpleTaskThread(this, 0, 125); + publishToMasterserverThread->setUniqueID(mutexOwnerId); + publishToMasterserverThread->start(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + } + } + + if (this->getAllowInGameConnections() == false) { + shutdownFTPServer(); + } + + if ((needToRepublishToMasterserver == true || + GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) && + requiresUPNPTrigger == true) { + + this->getServerSocket()->NETdiscoverUPnPDevices(); + } + + gameLaunched = true; + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + return bOkToStart; + } + + void ServerInterface::shutdownFTPServer() { + if (ftpServer != NULL) { + ftpServer->shutdownAndWait(); + delete ftpServer; + ftpServer = NULL; + } + } + + void ServerInterface::checkListenerSlots() { + if (gameLaunched == true && + this->getAllowInGameConnections() == true) { + + if (difftime((long int) time(NULL), lastListenerSlotCheckTime) >= 7) { + + lastListenerSlotCheckTime = time(NULL); + bool useInGameBlockingClientSockets = Config::getInstance().getBool("EnableInGameBlockingSockets", "true"); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + for (int startIndex = 0; startIndex < GameConstants::maxPlayers; ++startIndex) { + + int factionIndex = gameSettings.getFactionIndexForStartLocation(startIndex); + if (gameSettings.getFactionControl(factionIndex) != ctClosed && + gameSettings.getFactionControl(factionIndex) != ctHuman) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[startIndex], CODE_AT_LINE_X(startIndex)); + ConnectionSlot *connectionSlot = slots[startIndex]; + // Open slots for joining in progress game + if (connectionSlot == NULL) { + printf("Opening slot for in game connections, slot: %d, factionindex: %d name: %s\n", startIndex, factionIndex, gameSettings.getFactionTypeName(factionIndex).c_str()); + + addSlot(startIndex); + connectionSlot = slots[startIndex]; + if (useInGameBlockingClientSockets == true) { + connectionSlot->getSocket()->setBlock(true); + } + connectionSlot->setCanAcceptConnections(true); + } else if (connectionSlot != NULL && + connectionSlot->getCanAcceptConnections() == false && + connectionSlot->isConnected() == false) { + printf("Removing slot for in game connections, slot: %d, factionindex: %d name: %s\n", startIndex, factionIndex, gameSettings.getFactionTypeName(factionIndex).c_str()); + + this->removeSlot(startIndex); + } + } + } + } + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, needToRepublishToMasterserver); + } + + void ServerInterface::broadcastGameSetup(GameSettings *gameSettingsBuffer, bool setGameSettingsBuffer) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (gameSettingsBuffer == NULL) { + throw megaglest_runtime_error("gameSettingsBuffer == NULL"); + } + for (unsigned int factionIndex = 0; factionIndex < (unsigned int) gameSettingsBuffer->getFactionCount(); ++factionIndex) { + + int slotIndex = gameSettingsBuffer->getStartLocationIndex(factionIndex); + if (gameSettingsBuffer->getFactionControl(factionIndex) == ctNetwork && + isClientConnected(slotIndex) == false) { + + gameSettingsBuffer->setNetworkPlayerName(factionIndex, GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + } + if (setGameSettingsBuffer == true) { + validateGameSettings(gameSettingsBuffer); + } + + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + if (setGameSettingsBuffer == true) { + gameSettings = *gameSettingsBuffer; + gameSettingsUpdateCount++; + } + + NetworkMessageLaunch networkMessageLaunch(gameSettingsBuffer, nmtBroadCastSetup); + broadcastMessage(&networkMessageLaunch); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::broadcastMessage(NetworkMessage *networkMessage, int excludeSlot, int lockedSlotIndex) { + try { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutexSlotBroadCastAccessor(inBroadcastMessageThreadAccessor, CODE_AT_LINE); + if (inBroadcastMessage == true && + dynamic_cast(networkMessage) != NULL) { + + safeMutexSlotBroadCastAccessor.ReleaseLock(); + NetworkMessageText *txtMsg = dynamic_cast(networkMessage); + if (txtMsg != NULL) { + NetworkMessageText *msgCopy = txtMsg->getCopy(); + queueBroadcastMessage(msgCopy, excludeSlot); + } + return; + } else { + inBroadcastMessage = true; + safeMutexSlotBroadCastAccessor.ReleaseLock(true); + } + + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(NULL, CODE_AT_LINE_X(slotIndex)); + if (slotIndex != lockedSlotIndex) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] i = %d, lockedSlotIndex = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, slotIndex, lockedSlotIndex); + safeMutexSlot.setMutex(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + } + + ConnectionSlot* connectionSlot = slots[slotIndex]; + + if (slotIndex != excludeSlot && connectionSlot != NULL) { + if (connectionSlot->isConnected()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] before sendMessage\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + connectionSlot->sendMessage(networkMessage); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] after sendMessage\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + if (gameHasBeenInitiated == true && connectionSlot->isConnected() == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #1 before removeSlot for slot# %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, slotIndex); + + if (this->getAllowInGameConnections() == false) { + removeSlot(slotIndex, slotIndex); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #1 after removeSlot for slot# %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, slotIndex); + } + } else if (slotIndex == excludeSlot && gameHasBeenInitiated == true && + connectionSlot != NULL && connectionSlot->isConnected() == false) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #2 before removeSlot for slot# %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, slotIndex); + + if (this->getAllowInGameConnections() == false) { + removeSlot(slotIndex, slotIndex); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] #2 after removeSlot for slot# %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, slotIndex); + } + } + + safeMutexSlotBroadCastAccessor.Lock(); + + inBroadcastMessage = false; + + safeMutexSlotBroadCastAccessor.ReleaseLock(); + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ERROR [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + + MutexSafeWrapper safeMutexSlotBroadCastAccessor(inBroadcastMessageThreadAccessor, CODE_AT_LINE); + inBroadcastMessage = false; + safeMutexSlotBroadCastAccessor.ReleaseLock(); + + string sMsg = ex.what(); + sendTextMessage(sMsg, -1, true, "", lockedSlotIndex); + } + } + + void ServerInterface::broadcastMessageToConnectedClients(NetworkMessage *networkMessage, int excludeSlot) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + try { + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + + if (slotIndex != excludeSlot && connectionSlot != NULL) { + if (connectionSlot->isConnected()) { + connectionSlot->sendMessage(networkMessage); + } + } + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line: %d] Error [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line: %d] ERROR [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + DisplayErrorMessage(ex.what()); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Line: %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + + void ServerInterface::updateListen() { + if (gameHasBeenInitiated == true && + this->getAllowInGameConnections() == false) { + return; + } + + int openSlotCount = 0; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + + bool isSlotOpen = (slots[slotIndex] != NULL && slots[slotIndex]->isConnected() == false); + if (isSlotOpen == true) { + ++openSlotCount; + } + } + + serverSocket.listen(openSlotCount); + } + + int ServerInterface::getOpenSlotCount() { + int openSlotCount = 0; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + + bool isSlotOpen = (slots[slotIndex] != NULL && slots[slotIndex]->isConnected() == false); + if (isSlotOpen == true) { + ++openSlotCount; + } + } + return openSlotCount; + } + + int ServerInterface::getGameSettingsUpdateCount() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] START gameSettingsUpdateCount = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, gameSettingsUpdateCount); + + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + int result = gameSettingsUpdateCount; + safeMutex.ReleaseLock(); + return result; + } + + void ServerInterface::validateGameSettings(GameSettings *serverGameSettings) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + string mapFile = serverGameSettings->getMap(); + //printf("Trying to set map to [%s]. Current map is [%s]\n",serverGameSettings->getMap().c_str(),gameSettings.getMap().c_str()); + if (gameSettings.getMapFilter() != serverGameSettings->getMapFilter()) { + if (playerSortedMaps[serverGameSettings->getMapFilter()].size() == 0) { + serverGameSettings->setMapFilter(0); + } + } + int playerIndex = serverGameSettings->getMapFilter(); + if (find(playerSortedMaps[playerIndex].begin(), playerSortedMaps[playerIndex].end(), mapFile) == playerSortedMaps[playerIndex].end()) { + // switch to closest map + string foundMap = ""; + for (int i = 0; i < (int) playerSortedMaps[playerIndex].size(); ++i) { + foundMap = playerSortedMaps[playerIndex][i]; + if (toLower(foundMap) > toLower(serverGameSettings->getMap())) { + break; + } + } + printf("map %s not found on this server. Switching to map %s\n", serverGameSettings->getMap().c_str(), foundMap.c_str()); + serverGameSettings->setMap(foundMap); + } + Checksum checksum; + string file = Config::getMapPath(serverGameSettings->getMap(), "", false); + checksum.addFile(file); + serverGameSettings->setMapCRC(checksum.getSum()); + + string tilesetFile = serverGameSettings->getTileset(); + if (find(tilesetFiles.begin(), tilesetFiles.end(), tilesetFile) == tilesetFiles.end()) { + printf("Reverting tileset from [%s] to [%s]\n", serverGameSettings->getTileset().c_str(), gameSettings.getTileset().c_str()); + + serverGameSettings->setTileset(gameSettings.getTileset()); + serverGameSettings->setTilesetCRC(gameSettings.getTilesetCRC()); + } + + string techtreeFile = serverGameSettings->getTech(); + if (find(techTreeFiles.begin(), techTreeFiles.end(), techtreeFile) == techTreeFiles.end()) { + printf("Reverting tech from [%s] to [%s]\n", serverGameSettings->getTech().c_str(), gameSettings.getTech().c_str()); + + serverGameSettings->setTech(gameSettings.getTech()); + serverGameSettings->setTechCRC(gameSettings.getTechCRC()); + } + } + + void ServerInterface::setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck) { + MutexSafeWrapper safeMutex(serverSynchAccessor, CODE_AT_LINE); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] START gameSettingsUpdateCount = %d, waitForClientAck = %d\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, gameSettingsUpdateCount, waitForClientAck); + + if (serverGameSettings->getScenario() == "") { + string mapFile = serverGameSettings->getMap(); + if (find(mapFiles.begin(), mapFiles.end(), mapFile) == mapFiles.end()) { + printf("Reverting map from [%s] to [%s]\n", serverGameSettings->getMap().c_str(), gameSettings.getMap().c_str()); + + serverGameSettings->setMapFilter(gameSettings.getMapFilter()); + serverGameSettings->setMap(gameSettings.getMap()); + serverGameSettings->setMapCRC(gameSettings.getMapCRC()); + } + + string tilesetFile = serverGameSettings->getTileset(); + if (find(tilesetFiles.begin(), tilesetFiles.end(), tilesetFile) == tilesetFiles.end()) { + printf("Reverting tileset from [%s] to [%s]\n", serverGameSettings->getTileset().c_str(), gameSettings.getTileset().c_str()); + + serverGameSettings->setTileset(gameSettings.getTileset()); + serverGameSettings->setTilesetCRC(gameSettings.getTilesetCRC()); + } + + string techtreeFile = serverGameSettings->getTech(); + if (find(techTreeFiles.begin(), techTreeFiles.end(), techtreeFile) == techTreeFiles.end()) { + printf("Reverting tech from [%s] to [%s]\n", serverGameSettings->getTech().c_str(), gameSettings.getTech().c_str()); + + serverGameSettings->setTech(gameSettings.getTech()); + serverGameSettings->setTechCRC(gameSettings.getTechCRC()); + } + } + + gameSettings = *serverGameSettings; + + if (getAllowGameDataSynchCheck() == true) { + if (waitForClientAck == true && gameSettingsUpdateCount > 0) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Waiting for client acks #1\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + time_t tStart = time(NULL); + bool gotAckFromAllClients = false; + while (gotAckFromAllClients == false && difftime((long int) time(NULL), tStart) <= 5) { + gotAckFromAllClients = true; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + //printf("===> START slot %d - About to setGameSettings #1\n",i); + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected()) { + if (connectionSlot->getReceivedNetworkGameStatus() == false) { + gotAckFromAllClients = false; + } + + connectionSlot->update(true, slotIndex); + } + //printf("===> END slot %d - About to setGameSettings #1\n",i); + } + } + } + + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected()) { + connectionSlot->setReceivedNetworkGameStatus(false); + } + } + + NetworkMessageSynchNetworkGameData networkMessageSynchNetworkGameData(getGameSettings()); + broadcastMessageToConnectedClients(&networkMessageSynchNetworkGameData); + + if (waitForClientAck == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] Waiting for client acks #2\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + + time_t tStart = time(NULL); + bool gotAckFromAllClients = false; + while (gotAckFromAllClients == false && difftime((long int) time(NULL), tStart) <= 5) { + gotAckFromAllClients = true; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + //printf("===> START slot %d - About to setGameSettings 2\n",slotIndex); + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + if (connectionSlot != NULL && connectionSlot->isConnected()) { + if (connectionSlot->getReceivedNetworkGameStatus() == false) { + gotAckFromAllClients = false; + } + + connectionSlot->update(true, slotIndex); + } + //printf("===> END slot %d - About to setGameSettings 2\n",slotIndex); + } + } + } + + } + gameSettingsUpdateCount++; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] END\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + } + + void ServerInterface::close() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s] START\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__); + } + + string ServerInterface::getHumanPlayerName(int index) { + string result = Config::getInstance().getString("NetPlayerName", Socket::getHostName().c_str()); + if (index >= 0 || gameSettings.getThisFactionIndex() >= 0) { + if (index < 0) { + index = gameSettings.getThisFactionIndex(); + } + if (gameSettings.getNetworkPlayerName(index) != "") { + result = gameSettings.getNetworkPlayerName(index); + } + } + + return result; + } + + int ServerInterface::getHumanPlayerIndex() const { + return gameSettings.getStartLocationIndex(gameSettings.getThisFactionIndex()); + } + + std::map ServerInterface::publishToMasterserver() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + int slotCountUsed = 1; + int slotCountHumans = 1; + int slotCountConnectedPlayers = 1; + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + slotCountUsed = 0; + slotCountHumans = 0; + slotCountConnectedPlayers = 0; + } + + Config & config = Config::getInstance(); + std::map < string, string > publishToServerInfo; + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + if (slots[slotIndex] != NULL) { + slotCountUsed++; + slotCountHumans++; + ConnectionSlot* connectionSlot = slots[slotIndex]; + if ((connectionSlot != NULL) && (connectionSlot->isConnected())) { + slotCountConnectedPlayers++; + } + } + } + publishToServerInfo["uuid"] = Config::getInstance().getString("PlayerId", ""); + publishToServerInfo["glestVersion"] = glestVersionString; + publishToServerInfo["platform"] = getPlatformNameString(); + publishToServerInfo["binaryCompileDate"] = getCompileDateTime(); + publishToServerInfo["serverTitle"] = this->getGameSettings()->getGameName(); + publishToServerInfo["tech"] = this->getGameSettings()->getTech(); + publishToServerInfo["map"] = this->getGameSettings()->getMap(); + publishToServerInfo["tileset"] = this->getGameSettings()->getTileset(); + + bool updateSlots = true; + MutexSafeWrapper safeMutex2(gameStatsThreadAccessor, CODE_AT_LINE); + if (gameStats != NULL) { + for (int factionIndex = 0; factionIndex < gameStats->getFactionCount(); ++factionIndex) { + if (gameStats->getVictory(factionIndex) == true) { + updateSlots = false; + break; + } + } + } + safeMutex2.ReleaseLock(); + + if (updateSlots == true) { + publishToServerInfo["activeSlots"] = intToStr(slotCountUsed); + publishToServerInfo["networkSlots"] = intToStr(slotCountHumans); + publishToServerInfo["connectedClients"] = intToStr(slotCountConnectedPlayers); + } + + string serverPort = config.getString("PortServer", intToStr(GameConstants::serverPort).c_str()); + string externalPort = config.getString("PortExternal", serverPort.c_str()); + publishToServerInfo["externalconnectport"] = externalPort; + publishToServerInfo["privacyPlease"] = intToStr(config.getBool("PrivacyPlease", "false")); + publishToServerInfo["gameStatus"] = intToStr(game_status_in_progress); + + if (publishToMasterserverThread == NULL) { + publishToServerInfo["gameCmd"] = "gameOver"; + publishToServerInfo["gameStatus"] = intToStr(game_status_finished); + } + + //printf("Host game id = %s\n",this->getGameSettings()->getGameUUID().c_str()); + publishToServerInfo["gameUUID"] = this->getGameSettings()->getGameUUID(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + return publishToServerInfo; + } + + std::map ServerInterface::publishToMasterserverStats() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutex(gameStatsThreadAccessor, CODE_AT_LINE); + std::map < string, string > publishToServerInfo; + if (gameStats != NULL) { + publishToServerInfo["gameUUID"] = this->getGameSettings()->getGameUUID(); + publishToServerInfo["tech"] = this->getGameSettings()->getTech(); + publishToServerInfo["factionCount"] = intToStr(gameStats->getFactionCount()); + publishToServerInfo["framesPlayed"] = intToStr(gameStats->getFramesPlayed()); + publishToServerInfo["framesToCalculatePlaytime"] = intToStr(gameStats->getFramesToCalculatePlaytime()); + publishToServerInfo["maxConcurrentUnitCount"] = intToStr(gameStats->getMaxConcurrentUnitCount()); + publishToServerInfo["totalEndGameConcurrentUnitCount"] = intToStr(gameStats->getTotalEndGameConcurrentUnitCount()); + publishToServerInfo["isHeadlessServer"] = intToStr(gameStats->getIsMasterserverMode()); + + for (int factionIndex = 0; factionIndex < gameStats->getFactionCount(); ++factionIndex) { + publishToServerInfo["factionIndex_" + intToStr(factionIndex)] = intToStr(factionIndex); + publishToServerInfo["controlType_" + intToStr(factionIndex)] = intToStr(gameStats->getControl(factionIndex)); + publishToServerInfo["resourceMultiplier_" + intToStr(factionIndex)] = floatToStr(gameStats->getResourceMultiplier(factionIndex)); + publishToServerInfo["factionTypeName_" + intToStr(factionIndex)] = gameStats->getFactionTypeName(factionIndex); + publishToServerInfo["personalityType_" + intToStr(factionIndex)] = intToStr(gameStats->getPersonalityType(factionIndex)); + publishToServerInfo["teamIndex_" + intToStr(factionIndex)] = intToStr(gameStats->getTeam(factionIndex)); + publishToServerInfo["wonGame_" + intToStr(factionIndex)] = intToStr(gameStats->getVictory(factionIndex)); + publishToServerInfo["killCount_" + intToStr(factionIndex)] = intToStr(gameStats->getKills(factionIndex)); + publishToServerInfo["enemyKillCount_" + intToStr(factionIndex)] = intToStr(gameStats->getEnemyKills(factionIndex)); + publishToServerInfo["deathCount_" + intToStr(factionIndex)] = intToStr(gameStats->getDeaths(factionIndex)); + publishToServerInfo["unitsProducedCount_" + intToStr(factionIndex)] = intToStr(gameStats->getUnitsProduced(factionIndex)); + publishToServerInfo["resourceHarvestedCount_" + intToStr(factionIndex)] = intToStr(gameStats->getResourcesHarvested(factionIndex)); + publishToServerInfo["playerName_" + intToStr(factionIndex)] = gameStats->getPlayerName(factionIndex); + publishToServerInfo["quitBeforeGameEnd_" + intToStr(factionIndex)] = intToStr(gameStats->getPlayerLeftBeforeEnd(factionIndex)); + publishToServerInfo["quitTime_" + intToStr(factionIndex)] = intToStr(gameStats->getTimePlayerLeft(factionIndex)); + publishToServerInfo["playerUUID_" + intToStr(factionIndex)] = this->getGameSettings()->getNetworkPlayerUUID(factionIndex); + publishToServerInfo["platform_" + intToStr(factionIndex)] = this->getGameSettings()->getNetworkPlayerPlatform(factionIndex); + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + return publishToServerInfo; + } + + void ServerInterface::setGameStats(Stats *stats) { + if (stats == NULL) { + return; + } + MutexSafeWrapper safeMutex(gameStatsThreadAccessor, CODE_AT_LINE); + if (gameStats == NULL) { + gameStats = new Stats(); + } + *gameStats = *stats; + } + + void ServerInterface::simpleTask(BaseThread *callingThread, void *userdata) { + MutexSafeWrapper safeMutex(masterServerThreadAccessor, CODE_AT_LINE); + + if (difftime((long int) time(NULL), lastMasterserverHeartbeatTime) >= MASTERSERVER_HEARTBEAT_GAME_STATUS_SECONDS) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Checking to see masterserver needs an update of the game status [%d] callingThread [%p] publishToMasterserverThread [%p]\n", needToRepublishToMasterserver, callingThread, publishToMasterserverThread); + + lastMasterserverHeartbeatTime = time(NULL); + if (needToRepublishToMasterserver == true) { + try { + if (Config::getInstance().getString("Masterserver", "") != "") { + string request = Config::getInstance().getString("Masterserver"); + if (request != "") { + endPathWithSlash(request, false); + } + request += "addServerInfo.php?"; + + std::map newPublishToServerInfo = publishToMasterserver(); + + CURL *handle = SystemFlags::initHTTP(); + for (std::map::const_iterator iterMap = newPublishToServerInfo.begin(); + iterMap != newPublishToServerInfo.end(); ++iterMap) { + + request += iterMap->first; + request += "="; + request += SystemFlags::escapeURL(iterMap->second, handle); + request += "&"; + } + + //printf("The Host request is:\n%s\n",request.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("The Host request is:\n%s\n", request.c_str()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d] the request is:\n%s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, request.c_str()); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Calling masterserver [%s]...\n", request.c_str()); + + std::string serverInfo = SystemFlags::getHTTP(request, handle); + //printf("Result:\n%s\n",serverInfo .c_str()); + + string requestStats = Config::getInstance().getString("Masterserver"); + if (requestStats != "") { + endPathWithSlash(requestStats, false); + } + requestStats += "addGameStats.php?"; + + std::map newPublishToServerInfoStats = publishToMasterserverStats(); + if (newPublishToServerInfoStats.empty() == false) { + for (std::map::const_iterator iterMap = newPublishToServerInfoStats.begin(); + iterMap != newPublishToServerInfoStats.end(); ++iterMap) { + + requestStats += iterMap->first; + requestStats += "="; + requestStats += SystemFlags::escapeURL(iterMap->second, handle); + requestStats += "&"; + } + + //printf("The Host stats request is:\n%s\n",requestStats.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("The Host request is:\n%s\n", requestStats.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d] the request is:\n%s\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, requestStats.c_str()); + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Calling masterserver [%s]...\n", requestStats.c_str()); + + std::string serverInfoStats = SystemFlags::getHTTP(requestStats, handle); + //printf("Result:\n%s\n",serverInfoStats .c_str()); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d] the result is:\n'%s'\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, serverInfoStats.c_str()); + } + + SystemFlags::cleanupHTTP(&handle); + + if (SystemFlags::VERBOSE_MODE_ENABLED) printf("Done Calling masterserver\n"); + + //printf("the result is:\n'%s'\n",serverInfo.c_str()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork, "In [%s::%s Line %d] the result is:\n'%s'\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, serverInfo.c_str()); + } else { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line %d] error, no masterserver defined!\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__); + } + } catch (const exception &ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, "In [%s::%s Line %d] error during game status update: [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, ex.what()); + } + } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + DumpStatsToLog(false); + } + } + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + //printf("Attempt Accept\n"); + if (serverSocketAdmin != NULL) { + Socket *cli = serverSocketAdmin->accept(false); + if (cli != NULL) { + printf("Got status request connection, dumping info...\n"); + + string data = DumpStatsToLog(true); + cli->send(data.c_str(), (int) data.length()); + cli->disconnectSocket(); } } } } - } - return result; -} -void ServerInterface::saveGame(XmlNode *rootNode) { - std::map mapTagReplacements; - XmlNode *serverInterfaceNode = rootNode->addChild("ServerInterface"); - - for(int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { - MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex],CODE_AT_LINE_X(slotIndex)); - if(slots[slotIndex] != NULL) { - - XmlNode *slotNode = serverInterfaceNode->addChild("Slot"); - - ConnectionSlot *slot = slots[slotIndex]; - if(slot != NULL) { - slotNode->addAttribute("isconnected",intToStr(slot->isConnected()), mapTagReplacements); - slotNode->addAttribute("sessionkey",intToStr(slot->getSessionKey()), mapTagReplacements); - slotNode->addAttribute("ipaddress",slot->getSocket(false)->getIpAddress(), mapTagReplacements); - slotNode->addAttribute("name",slot->getName(), mapTagReplacements); - slotNode->addAttribute("uuid",slot->getUUID(), mapTagReplacements); + std::string ServerInterface::DumpStatsToLog(bool dumpToStringOnly) const { + string headlessLogFile = Config::getInstance().getString("HeadlessLogFile", "headless.log"); + if (getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) != "") { + headlessLogFile = getGameReadWritePath(GameConstants::path_logs_CacheLookupKey) + headlessLogFile; + } else { + string userData = Config::getInstance().getString("UserData_Root", ""); + if (userData != "") { + endPathWithSlash(userData); + } + headlessLogFile = userData + headlessLogFile; } - else { - slotNode->addAttribute("isconnected",intToStr(false), mapTagReplacements); + + ostringstream out; + out << "=========================================" << std::endl; + out << "Headless Server Current Game information:" << std::endl; + out << "=========================================" << std::endl; + + int connectedSlotCount = 0; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *slot = slots[slotIndex]; + if (slot != NULL) { + + connectedSlotCount++; + out << "Network connection for index: " << slotIndex << std::endl; + out << "------------------------------" << std::endl; + out << "Connected: " << boolToStr(slot->isConnected()) << std::endl; + out << "Handshake received: " << boolToStr(slot->getConnectHasHandshaked()) << std::endl; + if (slot->isConnected() == true) { + + time_t connectTime = slot->getConnectedTime(); + //struct tm *loctime = localtime (&connectTime); + struct tm loctime = threadsafe_localtime(connectTime); + char szBuf[8096] = ""; + strftime(szBuf, 100, "%Y-%m-%d %H:%M:%S", &loctime); + + const int HOURS_IN_DAY = 24; + const int MINUTES_IN_HOUR = 60; + const int SECONDS_IN_MINUTE = 60; + int InSeconds = difftime((long int) time(NULL), slot->getConnectedTime()); + // compute seconds + int seconds = InSeconds % SECONDS_IN_MINUTE; + // throw away seconds used in previous statement and convert to minutes + int InMinutes = InSeconds / SECONDS_IN_MINUTE; + // compute minutes + int minutes = InMinutes % MINUTES_IN_HOUR; + + // throw away minutes used in previous statement and convert to hours + int InHours = InMinutes / MINUTES_IN_HOUR; + // compute hours + int hours = InHours % HOURS_IN_DAY; + + out << "Connected at: " << szBuf << std::endl; + out << "Connection duration: " << hours << " hours " << minutes << " minutes " << seconds << " seconds." << std::endl; + out << "Player Index: " << slot->getPlayerIndex() << std::endl; + out << "IP Address: " << slot->getIpAddress() << std::endl; + out << "Player name: " << slot->getName() << std::endl; + out << "Player uuid: " << slot->getUUID() << std::endl; + out << "Language: " << slot->getNetworkPlayerLanguage() << std::endl; + out << "Game Version: " << slot->getVersionString() << std::endl; + out << "Session id: " << slot->getSessionKey() << std::endl; + out << "Socket id: " << slot->getSocketId() << std::endl; + } + } + } + out << "Total Slot Count: " << connectedSlotCount << std::endl; + out << "=========================================" << std::endl; + + std::string result = out.str(); + + if (dumpToStringOnly == false) { + +#if defined(WIN32) && !defined(__MINGW32__) + FILE *fp = _wfopen(utf8_decode(headlessLogFile).c_str(), L"w"); + std::ofstream logFile(fp); +#else + std::ofstream logFile; + logFile.open(headlessLogFile.c_str(), ios_base::out | ios_base::trunc); +#endif + logFile << result; + logFile.close(); +#if defined(WIN32) && !defined(__MINGW32__) + if (fp) { + fclose(fp); + } +#endif + } + + return result; + } + + void ServerInterface::notifyBadClientConnectAttempt(string ipAddress) { + //printf("In [%s::%s Line: %d] ipAddress [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,ipAddress.c_str()); + + if (badClientConnectIPList.find(ipAddress) == badClientConnectIPList.end()) { + badClientConnectIPList[ipAddress] = make_pair(0, (long int) time(NULL)); + } + + pair &lastBadConnectionAttempt = badClientConnectIPList[ipAddress]; + + const uint64 BLOCK_BAD_CLIENT_CONNECT_MAX_SECONDS = Config::getInstance().getInt("BlockBadClientConnectMaxSeconds", "60"); + const uint64 BLOCK_BAD_CLIENT_CONNECT_MAX_ATTEMPTS = Config::getInstance().getInt("BlockBadClientConnectMaxAttempts", "6"); + bool addToBlockedClientsList = false; + + if (difftime((long int) time(NULL), lastBadConnectionAttempt.second) <= BLOCK_BAD_CLIENT_CONNECT_MAX_SECONDS) { + + if (lastBadConnectionAttempt.first + 1 > BLOCK_BAD_CLIENT_CONNECT_MAX_ATTEMPTS) { + addToBlockedClientsList = true; + } + } else { + // Reset after x seconds + lastBadConnectionAttempt.first = 0; + } + + if (this->getAllowInGameConnections() == true) { + printf("notifyBadClientConnectAttempt() #1: %s!\n", ipAddress.c_str()); + } + + if (addToBlockedClientsList == true) { + serverSocket.addIPAddressToBlockedList(ipAddress); + } + + lastBadConnectionAttempt.first++; + lastBadConnectionAttempt.second = time(NULL); + } + + bool ServerInterface::getStartInGameConnectionLaunch() { + bool result = false; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + if (slots[slotIndex] != NULL) { + + ConnectionSlot *slot = slots[slotIndex]; + if (slot->getStartInGameConnectionLaunch() == true) { + result = true; + break; + } + } + } + return result; + } + + bool ServerInterface::getPauseForInGameConnection() { + bool result = false; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + if (slots[slotIndex] != NULL) { + + ConnectionSlot *slot = slots[slotIndex]; + if (slot->getPauseForInGameConnection() == true) { + result = true; + break; + } + } + } + return result; + } + + bool ServerInterface::getUnPauseForInGameConnection() { + bool result = false; + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + if (slots[slotIndex] != NULL) { + + ConnectionSlot *slot = slots[slotIndex]; + if (slot->isConnected() == true) { + if (slot->isReady() == true) { + result = true; + if (slot->getUnPauseForInGameConnection() == false) { + result = false; + break; + } + } else { + result = false; + break; + } + } + } + } + if (result == true) { + resumeGameStartTime = time(NULL); + } + return result; + } + + ConnectionSlot * ServerInterface::findSlotForUUID(string uuid, bool unConnectedOnly) { + ConnectionSlot *result = NULL; + if (uuid != "") { + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + + MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + ConnectionSlot *connectionSlot = slots[slotIndex]; + + if (connectionSlot != NULL) { + if (connectionSlot->getUUID() == uuid) { + if (unConnectedOnly == false || + (unConnectedOnly == true && connectionSlot->isConnected() == false)) { + + if (result == NULL || + (result->getConnectedTime() > connectionSlot->getConnectedTime())) + + result = connectionSlot; + } + } + } + } + } + return result; + } + + void ServerInterface::saveGame(XmlNode *rootNode) { + std::map mapTagReplacements; + XmlNode *serverInterfaceNode = rootNode->addChild("ServerInterface"); + + for (int slotIndex = 0; exitServer == false && slotIndex < GameConstants::maxPlayers; ++slotIndex) { + MutexSafeWrapper safeMutex(slotAccessorMutexes[slotIndex], CODE_AT_LINE_X(slotIndex)); + if (slots[slotIndex] != NULL) { + + XmlNode *slotNode = serverInterfaceNode->addChild("Slot"); + + ConnectionSlot *slot = slots[slotIndex]; + if (slot != NULL) { + slotNode->addAttribute("isconnected", intToStr(slot->isConnected()), mapTagReplacements); + slotNode->addAttribute("sessionkey", intToStr(slot->getSessionKey()), mapTagReplacements); + slotNode->addAttribute("ipaddress", slot->getSocket(false)->getIpAddress(), mapTagReplacements); + slotNode->addAttribute("name", slot->getName(), mapTagReplacements); + slotNode->addAttribute("uuid", slot->getUUID(), mapTagReplacements); + } else { + slotNode->addAttribute("isconnected", intToStr(false), mapTagReplacements); + } + } } } - } -} -}}//end namespace + } +}//end namespace diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h index 62e0ef9f7..ef2fe2b00 100644 --- a/source/glest_game/network/server_interface.h +++ b/source/glest_game/network/server_interface.h @@ -24,8 +24,8 @@ #define _GLEST_GAME_SERVERINTERFACE_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -38,269 +38,293 @@ using std::vector; using Shared::Platform::ServerSocket; -namespace Shared { namespace PlatformCommon { class FTPServerThread; }} +namespace Shared { + namespace PlatformCommon { + class FTPServerThread; + } +} -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -class Stats; -// ===================================================== -// class ServerInterface -// ===================================================== + class Stats; + // ===================================================== + // class ServerInterface + // ===================================================== -class ServerInterface: public GameNetworkInterface, - public ConnectionSlotCallbackInterface, - // This is for publishing game status to the masterserver - public SimpleTaskCallbackInterface, - public FTPClientValidationInterface { + class ServerInterface : public GameNetworkInterface, + public ConnectionSlotCallbackInterface, + // This is for publishing game status to the masterserver + public SimpleTaskCallbackInterface, + public FTPClientValidationInterface { -class TextMessageQueue { -public: - string text; - int teamIndex; - bool echoLocal; - string targetLanguage; -}; + class TextMessageQueue { + public: + string text; + int teamIndex; + bool echoLocal; + string targetLanguage; + }; -private: - ConnectionSlot* slots[GameConstants::maxPlayers]; - Mutex *slotAccessorMutexes[GameConstants::maxPlayers]; + private: + ConnectionSlot * slots[GameConstants::maxPlayers]; + Mutex *slotAccessorMutexes[GameConstants::maxPlayers]; - ServerSocket serverSocket; + ServerSocket serverSocket; - Mutex *switchSetupRequestsSynchAccessor; - SwitchSetupRequest* switchSetupRequests[GameConstants::maxPlayers]; + Mutex *switchSetupRequestsSynchAccessor; + SwitchSetupRequest* switchSetupRequests[GameConstants::maxPlayers]; - Mutex *serverSynchAccessor; - int currentFrameCount; + Mutex *serverSynchAccessor; + int currentFrameCount; - time_t gameStartTime; + time_t gameStartTime; - time_t lastGlobalLagCheckTime; + time_t lastGlobalLagCheckTime; - SimpleTaskThread *publishToMasterserverThread; - Mutex *masterServerThreadAccessor; - time_t lastMasterserverHeartbeatTime; - bool needToRepublishToMasterserver; + SimpleTaskThread *publishToMasterserverThread; + Mutex *masterServerThreadAccessor; + time_t lastMasterserverHeartbeatTime; + bool needToRepublishToMasterserver; - ::Shared::PlatformCommon::FTPServerThread *ftpServer; - bool exitServer; - int64 nextEventId; + ::Shared::PlatformCommon::FTPServerThread *ftpServer; + bool exitServer; + int64 nextEventId; - Mutex *textMessageQueueThreadAccessor; - vector textMessageQueue; + Mutex *textMessageQueueThreadAccessor; + vector textMessageQueue; - Mutex *broadcastMessageQueueThreadAccessor; - vector > broadcastMessageQueue; + Mutex *broadcastMessageQueueThreadAccessor; + vector > broadcastMessageQueue; - Mutex *inBroadcastMessageThreadAccessor; - bool inBroadcastMessage; + Mutex *inBroadcastMessageThreadAccessor; + bool inBroadcastMessage; - bool masterserverAdminRequestLaunch; + bool masterserverAdminRequestLaunch; + + vector mapFiles; + vector playerSortedMaps[GameConstants::maxPlayers + 1]; + vector techTreeFiles; + vector tilesetFiles; + + map > badClientConnectIPList; + + ServerSocket *serverSocketAdmin; + MasterSlaveThreadController masterController; - vector mapFiles; - vector playerSortedMaps[GameConstants::maxPlayers+1]; - vector techTreeFiles; - vector tilesetFiles; - - map > badClientConnectIPList; - - ServerSocket *serverSocketAdmin; - MasterSlaveThreadController masterController; - - bool gameHasBeenInitiated; - int gameSettingsUpdateCount; - - bool allowInGameConnections; - bool gameLaunched; - time_t lastListenerSlotCheckTime; - - time_t resumeGameStartTime; - - Mutex *gameStatsThreadAccessor; - Stats *gameStats; - - bool clientsAutoPausedDueToLag; - Chrono clientsAutoPausedDueToLagTimer; - Chrono lastBroadcastCommandsTimer; - ClientLagCallbackInterface *clientLagCallbackInterface; - -public: - ServerInterface(bool publishEnabled, ClientLagCallbackInterface *clientLagCallbackInterface); - virtual ~ServerInterface(); - - bool getClientsAutoPausedDueToLag(); - void setClientLagCallbackInterface(ClientLagCallbackInterface *intf); - void setGameStats(Stats *gameStats); - - virtual Socket* getSocket(bool mutexLock=true) {return &serverSocket;} - virtual std::string getIpAddress(bool mutexLock=true); - - time_t getGameStartTime() const { return gameStartTime; } - - virtual bool getAllowInGameConnections() const { return allowInGameConnections; } - void setAllowInGameConnections(bool value) { allowInGameConnections = value; } - - bool getStartInGameConnectionLaunch(); - bool getPauseForInGameConnection(); - bool getUnPauseForInGameConnection(); - - void shutdownFTPServer(); - - virtual void close(); - virtual void update(); - virtual void updateLobby() { }; - virtual void updateKeyframe(int frameCount); - virtual void setKeyframe(int frameCount) { currentFrameCount = frameCount; } - - virtual void waitUntilReady(Checksum *checksum); - virtual void sendTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage); - void sendTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage, int lockedSlotIndex); - - void queueTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage); - - virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note,int playerIndex); - void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex, int lockedSlotIndex); - - virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex); - void sendHighlightCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex); - - virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex); - void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex); - - virtual void quitGame(bool userManuallyQuit); - virtual string getNetworkStatus(); - ServerSocket *getServerSocket() { - return &serverSocket; - } - - SwitchSetupRequest **getSwitchSetupRequests(); - SwitchSetupRequest *getSwitchSetupRequests(int index); - void setSwitchSetupRequests(int index,SwitchSetupRequest *ptr); - Mutex * getSwitchSetupRequestsMutex() { return switchSetupRequestsSynchAccessor; } - - void addSlot(int playerIndex); - bool switchSlot(int fromPlayerIndex, int toPlayerIndex); - void removeSlot(int playerIndex, int lockedSlotIndex = -1); - virtual ConnectionSlot *getSlot(int playerIndex, bool lockMutex); - virtual Mutex *getSlotMutex(int playerIndex); - int getSlotCount(); - int getConnectedSlotCount(bool authenticated); - - int getOpenSlotCount(); - bool launchGame(const GameSettings *gameSettings); - void validateGameSettings(GameSettings *serverGameSettings); - void setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck); - void broadcastGameSetup(GameSettings *gameSettingsBuffer, bool setGameSettingsBuffer=false); - - int getGameSettingsUpdateCount(); - - bool getMasterserverAdminRequestLaunch() const { return masterserverAdminRequestLaunch; } - void setMasterserverAdminRequestLaunch(bool value) { masterserverAdminRequestLaunch = value; } - - void updateListen(); - virtual bool getConnectHasHandshaked() const { - return false; - } - - virtual void slotUpdateTask(ConnectionSlotEvent *event) { }; - bool hasClientConnection(); - virtual bool isClientConnected(int index); - - int getCurrentFrameCount() const { - return currentFrameCount; - } - - std::pair clientLagCheck(ConnectionSlot *connectionSlot, bool skipNetworkBroadCast = false); - bool signalClientReceiveCommands(ConnectionSlot *connectionSlot, int slotIndex, bool socketTriggered, ConnectionSlotEvent & event); - void updateSocketTriggeredList(std::map & socketTriggeredList); - bool isPortBound() const { - return serverSocket.isPortBound(); - } - - int getBindPort() const { - return serverSocket.getBindPort(); - } - - void broadcastPing(NetworkMessagePing *networkMessage, int excludeSlot = -1) { - this->broadcastMessage(networkMessage, excludeSlot); - } - - void queueBroadcastMessage(NetworkMessage *networkMessage, int excludeSlot = -1); - virtual string getHumanPlayerName(int index = -1); - virtual int getHumanPlayerIndex() const; - bool getNeedToRepublishToMasterserver() const { - return needToRepublishToMasterserver; - } - - void setNeedToRepublishToMasterserver(bool value) { - needToRepublishToMasterserver = value; - } - - void setPublishEnabled(bool value); - - bool getGameHasBeenInitiated() const { - return gameHasBeenInitiated; - } - -public: - Mutex *getServerSynchAccessor() { - return serverSynchAccessor; - } - - virtual void simpleTask(BaseThread *callingThread,void *userdata); - void addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp); - virtual int isValidClientType(uint32 clientIp); - virtual int isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename); - - void notifyBadClientConnectAttempt(string ipAddress); - std::string DumpStatsToLog(bool dumpToStringOnly) const; - - virtual void saveGame(XmlNode *rootNode); - - void broadcastMessage(NetworkMessage *networkMessage, int excludeSlot = -1, int lockedSlotIndex = -1); - - ConnectionSlot * findSlotForUUID(string uuid, bool unConnectedOnly=true); - -private: - - void broadcastMessageToConnectedClients(NetworkMessage *networkMessage, int excludeSlot = -1); - bool shouldDiscardNetworkMessage(NetworkMessageType networkMessageType, ConnectionSlot *connectionSlot); - void updateSlot(ConnectionSlotEvent *event); - void validateConnectedClients(); - - std::map publishToMasterserver(); - std::map publishToMasterserverStats(); - - int64 getNextEventId(); - void processTextMessageQueue(); - void processBroadCastMessageQueue(); - void checkListenerSlots(); - void checkForCompletedClientsUsingThreadManager( - std::map& mapSlotSignalledList, - std::vector& errorMsgList); - void checkForCompletedClientsUsingLoop( - std::map& mapSlotSignalledList, - std::vector& errorMsgList, - std::map& eventList); - void checkForAutoPauseForLaggingClient(int index, - ConnectionSlot* connectionSlot); - void checkForAutoResumeForLaggingClients(); - -protected: - void signalClientsToRecieveData(std::map & socketTriggeredList, std::map & eventList, std::map & mapSlotSignalledList); - void checkForCompletedClients(std::map & mapSlotSignalledList,std::vector &errorMsgList,std::map &eventList); - void checkForLaggingClients(std::map &mapSlotSignalledList, std::map &eventList, std::map &socketTriggeredList,std::vector &errorMsgList); - void executeNetworkCommandsFromClients(); - void dispatchPendingChatMessages(std::vector &errorMsgList); - void dispatchPendingMarkCellMessages(std::vector &errorMsgList); - void dispatchPendingUnMarkCellMessages(std::vector &errorMsgList); - void dispatchPendingHighlightCellMessages(std::vector &errorMsgList); - - void shutdownMasterserverPublishThread(); - - -}; - -}}//end namespace + bool gameHasBeenInitiated; + int gameSettingsUpdateCount; + + bool allowInGameConnections; + bool gameLaunched; + time_t lastListenerSlotCheckTime; + + time_t resumeGameStartTime; + + Mutex *gameStatsThreadAccessor; + Stats *gameStats; + + bool clientsAutoPausedDueToLag; + Chrono clientsAutoPausedDueToLagTimer; + Chrono lastBroadcastCommandsTimer; + ClientLagCallbackInterface *clientLagCallbackInterface; + + public: + ServerInterface(bool publishEnabled, ClientLagCallbackInterface *clientLagCallbackInterface); + virtual ~ServerInterface(); + + bool getClientsAutoPausedDueToLag(); + void setClientLagCallbackInterface(ClientLagCallbackInterface *intf); + void setGameStats(Stats *gameStats); + + virtual Socket* getSocket(bool mutexLock = true) { + return &serverSocket; + } + virtual std::string getIpAddress(bool mutexLock = true); + + time_t getGameStartTime() const { + return gameStartTime; + } + + virtual bool getAllowInGameConnections() const { + return allowInGameConnections; + } + void setAllowInGameConnections(bool value) { + allowInGameConnections = value; + } + + bool getStartInGameConnectionLaunch(); + bool getPauseForInGameConnection(); + bool getUnPauseForInGameConnection(); + + void shutdownFTPServer(); + + virtual void close(); + virtual void update(); + virtual void updateLobby() { + }; + virtual void updateKeyframe(int frameCount); + virtual void setKeyframe(int frameCount) { + currentFrameCount = frameCount; + } + + virtual void waitUntilReady(Checksum *checksum); + virtual void sendTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage); + void sendTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage, int lockedSlotIndex); + + void queueTextMessage(const string & text, int teamIndex, bool echoLocal, string targetLanguage); + + virtual void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex); + void sendMarkCellMessage(Vec2i targetPos, int factionIndex, string note, int playerIndex, int lockedSlotIndex); + + virtual void sendHighlightCellMessage(Vec2i targetPos, int factionIndex); + void sendHighlightCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex); + + virtual void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex); + void sendUnMarkCellMessage(Vec2i targetPos, int factionIndex, int lockedSlotIndex); + + virtual void quitGame(bool userManuallyQuit); + virtual string getNetworkStatus(); + ServerSocket *getServerSocket() { + return &serverSocket; + } + + SwitchSetupRequest **getSwitchSetupRequests(); + SwitchSetupRequest *getSwitchSetupRequests(int index); + void setSwitchSetupRequests(int index, SwitchSetupRequest *ptr); + Mutex * getSwitchSetupRequestsMutex() { + return switchSetupRequestsSynchAccessor; + } + + void addSlot(int playerIndex); + bool switchSlot(int fromPlayerIndex, int toPlayerIndex); + void removeSlot(int playerIndex, int lockedSlotIndex = -1); + virtual ConnectionSlot *getSlot(int playerIndex, bool lockMutex); + virtual Mutex *getSlotMutex(int playerIndex); + int getSlotCount(); + int getConnectedSlotCount(bool authenticated); + + int getOpenSlotCount(); + bool launchGame(const GameSettings *gameSettings); + void validateGameSettings(GameSettings *serverGameSettings); + void setGameSettings(GameSettings *serverGameSettings, bool waitForClientAck); + void broadcastGameSetup(GameSettings *gameSettingsBuffer, bool setGameSettingsBuffer = false); + + int getGameSettingsUpdateCount(); + + bool getMasterserverAdminRequestLaunch() const { + return masterserverAdminRequestLaunch; + } + void setMasterserverAdminRequestLaunch(bool value) { + masterserverAdminRequestLaunch = value; + } + + void updateListen(); + virtual bool getConnectHasHandshaked() const { + return false; + } + + virtual void slotUpdateTask(ConnectionSlotEvent *event) { + }; + bool hasClientConnection(); + virtual bool isClientConnected(int index); + + int getCurrentFrameCount() const { + return currentFrameCount; + } + + std::pair clientLagCheck(ConnectionSlot *connectionSlot, bool skipNetworkBroadCast = false); + bool signalClientReceiveCommands(ConnectionSlot *connectionSlot, int slotIndex, bool socketTriggered, ConnectionSlotEvent & event); + void updateSocketTriggeredList(std::map & socketTriggeredList); + bool isPortBound() const { + return serverSocket.isPortBound(); + } + + int getBindPort() const { + return serverSocket.getBindPort(); + } + + void broadcastPing(NetworkMessagePing *networkMessage, int excludeSlot = -1) { + this->broadcastMessage(networkMessage, excludeSlot); + } + + void queueBroadcastMessage(NetworkMessage *networkMessage, int excludeSlot = -1); + virtual string getHumanPlayerName(int index = -1); + virtual int getHumanPlayerIndex() const; + bool getNeedToRepublishToMasterserver() const { + return needToRepublishToMasterserver; + } + + void setNeedToRepublishToMasterserver(bool value) { + needToRepublishToMasterserver = value; + } + + void setPublishEnabled(bool value); + + bool getGameHasBeenInitiated() const { + return gameHasBeenInitiated; + } + + public: + Mutex * getServerSynchAccessor() { + return serverSynchAccessor; + } + + virtual void simpleTask(BaseThread *callingThread, void *userdata); + void addClientToServerIPAddress(uint32 clientIp, uint32 ServerIp); + virtual int isValidClientType(uint32 clientIp); + virtual int isClientAllowedToGetFile(uint32 clientIp, const char *username, const char *filename); + + void notifyBadClientConnectAttempt(string ipAddress); + std::string DumpStatsToLog(bool dumpToStringOnly) const; + + virtual void saveGame(XmlNode *rootNode); + + void broadcastMessage(NetworkMessage *networkMessage, int excludeSlot = -1, int lockedSlotIndex = -1); + + ConnectionSlot * findSlotForUUID(string uuid, bool unConnectedOnly = true); + + private: + + void broadcastMessageToConnectedClients(NetworkMessage *networkMessage, int excludeSlot = -1); + bool shouldDiscardNetworkMessage(NetworkMessageType networkMessageType, ConnectionSlot *connectionSlot); + void updateSlot(ConnectionSlotEvent *event); + void validateConnectedClients(); + + std::map publishToMasterserver(); + std::map publishToMasterserverStats(); + + int64 getNextEventId(); + void processTextMessageQueue(); + void processBroadCastMessageQueue(); + void checkListenerSlots(); + void checkForCompletedClientsUsingThreadManager( + std::map& mapSlotSignalledList, + std::vector& errorMsgList); + void checkForCompletedClientsUsingLoop( + std::map& mapSlotSignalledList, + std::vector& errorMsgList, + std::map& eventList); + void checkForAutoPauseForLaggingClient(int index, + ConnectionSlot* connectionSlot); + void checkForAutoResumeForLaggingClients(); + + protected: + void signalClientsToRecieveData(std::map & socketTriggeredList, std::map & eventList, std::map & mapSlotSignalledList); + void checkForCompletedClients(std::map & mapSlotSignalledList, std::vector &errorMsgList, std::map &eventList); + void checkForLaggingClients(std::map &mapSlotSignalledList, std::map &eventList, std::map &socketTriggeredList, std::vector &errorMsgList); + void executeNetworkCommandsFromClients(); + void dispatchPendingChatMessages(std::vector &errorMsgList); + void dispatchPendingMarkCellMessages(std::vector &errorMsgList); + void dispatchPendingUnMarkCellMessages(std::vector &errorMsgList); + void dispatchPendingHighlightCellMessages(std::vector &errorMsgList); + + void shutdownMasterserverPublishThread(); + + + }; + + } +}//end namespace #endif diff --git a/source/glest_game/sound/sound_container.cpp b/source/glest_game/sound/sound_container.cpp index 2b1f2f40f..a09b46420 100644 --- a/source/glest_game/sound/sound_container.cpp +++ b/source/glest_game/sound/sound_container.cpp @@ -16,30 +16,32 @@ using namespace Shared::Util; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class SoundContainer -// ===================================================== + // ===================================================== + // class SoundContainer + // ===================================================== -SoundContainer::SoundContainer(){ - lastSound= -1; -} - -StaticSound *SoundContainer::getRandSound() const{ - switch(sounds.size()){ - case 0: - return NULL; - case 1: - return sounds[0]; - default: - int soundIndex= random.randRange(0, (int)sounds.size()-1); - if(soundIndex==lastSound){ - soundIndex= (lastSound+1) % sounds.size(); + SoundContainer::SoundContainer() { + lastSound = -1; } - lastSound= soundIndex; - return sounds[soundIndex]; - } -} -}}//end namespace + StaticSound *SoundContainer::getRandSound() const { + switch (sounds.size()) { + case 0: + return NULL; + case 1: + return sounds[0]; + default: + int soundIndex = random.randRange(0, (int) sounds.size() - 1); + if (soundIndex == lastSound) { + soundIndex = (lastSound + 1) % sounds.size(); + } + lastSound = soundIndex; + return sounds[soundIndex]; + } + } + + } +}//end namespace diff --git a/source/glest_game/sound/sound_container.h b/source/glest_game/sound/sound_container.h index ee4c058e2..e74d669d9 100644 --- a/source/glest_game/sound/sound_container.h +++ b/source/glest_game/sound/sound_container.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_SOUNDCONTAINER_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include @@ -26,35 +26,47 @@ using std::vector; using Shared::Util::RandomGen; using Shared::Sound::StaticSound; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -// ===================================================== -// class SoundContainer -// -/// Holds a list of sounds that are usually played at random -// ===================================================== + // ===================================================== + // class SoundContainer + // + /// Holds a list of sounds that are usually played at random + // ===================================================== -class SoundContainer{ -public: - typedef vector Sounds; + class SoundContainer { + public: + typedef vector Sounds; -private: - Sounds sounds; - mutable RandomGen random; - mutable int lastSound; + private: + Sounds sounds; + mutable RandomGen random; + mutable int lastSound; -public: - SoundContainer(); + public: + SoundContainer(); - void resize(int size) {sounds.resize(size);} - StaticSound *&operator[](int i) {return sounds[i];} + void resize(int size) { + sounds.resize(size); + } + StaticSound *&operator[](int i) { + return sounds[i]; + } - const Sounds &getSounds() const {return sounds;} - void clearSounds() {sounds.clear();} - Sounds *getSoundsPtr() {return &sounds;} - StaticSound *getRandSound() const; -}; + const Sounds &getSounds() const { + return sounds; + } + void clearSounds() { + sounds.clear(); + } + Sounds *getSoundsPtr() { + return &sounds; + } + StaticSound *getRandSound() const; + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/sound/sound_renderer.cpp b/source/glest_game/sound/sound_renderer.cpp index 7d0948095..4ded3fba8 100644 --- a/source/glest_game/sound/sound_renderer.cpp +++ b/source/glest_game/sound/sound_renderer.cpp @@ -21,260 +21,261 @@ using namespace Shared::Util; using namespace Shared::Graphics; using namespace Shared::Sound; -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -const int SoundRenderer::ambientFade= 6000; -const float SoundRenderer::audibleDist= 50.f; + const int SoundRenderer::ambientFade = 6000; + const float SoundRenderer::audibleDist = 50.f; -// ===================================================== -// class SoundRenderer -// ===================================================== + // ===================================================== + // class SoundRenderer + // ===================================================== -SoundRenderer::SoundRenderer() : mutex(new Mutex(CODE_AT_LINE)) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + SoundRenderer::SoundRenderer() : mutex(new Mutex(CODE_AT_LINE)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); - soundPlayer = NULL; - loadConfig(); + soundPlayer = NULL; + loadConfig(); - Config &config= Config::getInstance(); - runThreadSafe = config.getBool("ThreadedSoundStream","true"); + Config &config = Config::getInstance(); + runThreadSafe = config.getBool("ThreadedSoundStream", "true"); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] runThreadSafe = %d\n",__FILE__,__FUNCTION__,__LINE__,runThreadSafe); -} - -bool SoundRenderer::init(Window *window) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - - SoundInterface &si= SoundInterface::getInstance(); - FactoryRepository &fr= FactoryRepository::getInstance(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - Config &config= Config::getInstance(); - si.setFactory(fr.getSoundFactory(config.getString("FactorySound"))); - - cleanup(); - stopAllSounds(); - - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - soundPlayer= si.newSoundPlayer(); - if(soundPlayer != NULL) { - SoundPlayerParams soundPlayerParams; - soundPlayerParams.staticBufferCount= config.getInt("SoundStaticBuffers"); - soundPlayerParams.strBufferCount= config.getInt("SoundStreamingBuffers"); - soundPlayer->init(&soundPlayerParams); - } - safeMutex.ReleaseLock(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - - return wasInitOk(); -} - -void SoundRenderer::cleanup() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - - stopAllSounds(); - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - delete soundPlayer; - soundPlayer = NULL; - - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); -} - -bool SoundRenderer::wasInitOk() const { - bool result = false; - if(soundPlayer != NULL) { - result = soundPlayer->wasInitOk(); - } - else { - Config &config= Config::getInstance(); - if(config.getString("FactorySound") == "" || - config.getString("FactorySound") == "None") { - result = true; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] runThreadSafe = %d\n", __FILE__, __FUNCTION__, __LINE__, runThreadSafe); } - } - return result; -} -SoundRenderer::~SoundRenderer() { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); + bool SoundRenderer::init(Window *window) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); - cleanup(); + SoundInterface &si = SoundInterface::getInstance(); + FactoryRepository &fr = FactoryRepository::getInstance(); - delete mutex; - mutex = NULL; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + Config &config = Config::getInstance(); + si.setFactory(fr.getSoundFactory(config.getString("FactorySound"))); - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); -} + cleanup(); + stopAllSounds(); -SoundRenderer &SoundRenderer::getInstance() { - static SoundRenderer soundRenderer; - return soundRenderer; -} - -void SoundRenderer::update() { - if(wasInitOk() == true && soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - if(soundPlayer) { - soundPlayer->updateStreams(); - } - } -} - -// ======================= Music ============================ - -void SoundRenderer::playMusic(StrSound *strSound) { - if(strSound != NULL) { - strSound->setVolume(musicVolume); - strSound->restart(); - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->play(strSound); + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); } + + soundPlayer = si.newSoundPlayer(); + if (soundPlayer != NULL) { + SoundPlayerParams soundPlayerParams; + soundPlayerParams.staticBufferCount = config.getInt("SoundStaticBuffers"); + soundPlayerParams.strBufferCount = config.getInt("SoundStreamingBuffers"); + soundPlayer->init(&soundPlayerParams); + } + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + + return wasInitOk(); } - } -} -void SoundRenderer::setMusicVolume(StrSound *strSound) { - if(strSound != NULL) { - strSound->setVolume(musicVolume); - } -} + void SoundRenderer::cleanup() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); -void SoundRenderer::stopMusic(StrSound *strSound) { - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } + stopAllSounds(); - if(soundPlayer) { - soundPlayer->stop(strSound); - if(strSound != NULL) { - if(strSound->getNext() != NULL) { - soundPlayer->stop(strSound->getNext()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + delete soundPlayer; + soundPlayer = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + bool SoundRenderer::wasInitOk() const { + bool result = false; + if (soundPlayer != NULL) { + result = soundPlayer->wasInitOk(); + } else { + Config &config = Config::getInstance(); + if (config.getString("FactorySound") == "" || + config.getString("FactorySound") == "None") { + result = true; + } + } + return result; + } + + SoundRenderer::~SoundRenderer() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + + cleanup(); + + delete mutex; + mutex = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s %d]\n", __FILE__, __FUNCTION__, __LINE__); + } + + SoundRenderer &SoundRenderer::getInstance() { + static SoundRenderer soundRenderer; + return soundRenderer; + } + + void SoundRenderer::update() { + if (wasInitOk() == true && soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + if (soundPlayer) { + soundPlayer->updateStreams(); } } } - } -} -// ======================= Fx ============================ + // ======================= Music ============================ -void SoundRenderer::playFx(StaticSound *staticSound, Vec3f soundPos, Vec3f camPos) { - if(staticSound!=NULL){ - float d= soundPos.dist(camPos); + void SoundRenderer::playMusic(StrSound *strSound) { + if (strSound != NULL) { + strSound->setVolume(musicVolume); + strSound->restart(); + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } - if(d < audibleDist){ - float vol= (1.f-d/audibleDist)*fxVolume; - float correctedVol= std::log10(std::log10(vol*9+1)*9+1); - - staticSound->setVolume(correctedVol); - - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->play(staticSound); + if (soundPlayer) { + soundPlayer->play(strSound); + } } } } - } -} -void SoundRenderer::playFx(StaticSound *staticSound, bool force) { - if(staticSound!=NULL){ - staticSound->setVolume(fxVolume); - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->play(staticSound, force); + void SoundRenderer::setMusicVolume(StrSound *strSound) { + if (strSound != NULL) { + strSound->setVolume(musicVolume); } } - } -} -// ======================= Ambient ============================ + void SoundRenderer::stopMusic(StrSound *strSound) { + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } -void SoundRenderer::playAmbient(StrSound *strSound) { - if(strSound != NULL) { - strSound->setVolume(ambientVolume); - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->play(strSound, ambientFade); + if (soundPlayer) { + soundPlayer->stop(strSound); + if (strSound != NULL) { + if (strSound->getNext() != NULL) { + soundPlayer->stop(strSound->getNext()); + } + } + } } } + + // ======================= Fx ============================ + + void SoundRenderer::playFx(StaticSound *staticSound, Vec3f soundPos, Vec3f camPos) { + if (staticSound != NULL) { + float d = soundPos.dist(camPos); + + if (d < audibleDist) { + float vol = (1.f - d / audibleDist)*fxVolume; + float correctedVol = std::log10(std::log10(vol * 9 + 1) * 9 + 1); + + staticSound->setVolume(correctedVol); + + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + + if (soundPlayer) { + soundPlayer->play(staticSound); + } + } + } + } + } + + void SoundRenderer::playFx(StaticSound *staticSound, bool force) { + if (staticSound != NULL) { + staticSound->setVolume(fxVolume); + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + + if (soundPlayer) { + soundPlayer->play(staticSound, force); + } + } + } + } + + // ======================= Ambient ============================ + + void SoundRenderer::playAmbient(StrSound *strSound) { + if (strSound != NULL) { + strSound->setVolume(ambientVolume); + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + + if (soundPlayer) { + soundPlayer->play(strSound, ambientFade); + } + } + } + } + + void SoundRenderer::stopAmbient(StrSound *strSound) { + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + + if (soundPlayer) { + soundPlayer->stop(strSound, ambientFade); + } + } + } + + // ======================= Misc ============================ + + void SoundRenderer::stopAllSounds(int64 fadeOff) { + if (soundPlayer != NULL) { + MutexSafeWrapper safeMutex(NULL, string(__FILE__) + "_" + intToStr(__LINE__)); + if (runThreadSafe == true) { + safeMutex.setMutex(mutex); + } + + if (soundPlayer) { + soundPlayer->stopAllSounds(fadeOff); + } + } + } + + bool SoundRenderer::isVolumeTurnedOff() const { + return (fxVolume <= 0 && musicVolume <= 0 && ambientVolume <= 0); + } + + void SoundRenderer::loadConfig() { + Config &config = Config::getInstance(); + + fxVolume = config.getInt("SoundVolumeFx") / 100.f; + musicVolume = config.getInt("SoundVolumeMusic") / 100.f; + ambientVolume = config.getInt("SoundVolumeAmbient") / 100.f; + } + } -} - -void SoundRenderer::stopAmbient(StrSound *strSound) { - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->stop(strSound, ambientFade); - } - } -} - -// ======================= Misc ============================ - -void SoundRenderer::stopAllSounds(int64 fadeOff) { - if(soundPlayer != NULL) { - MutexSafeWrapper safeMutex(NULL,string(__FILE__) + "_" + intToStr(__LINE__)); - if(runThreadSafe == true) { - safeMutex.setMutex(mutex); - } - - if(soundPlayer) { - soundPlayer->stopAllSounds(fadeOff); - } - } -} - -bool SoundRenderer::isVolumeTurnedOff() const { - return (fxVolume <= 0 && musicVolume <= 0 && ambientVolume <= 0); -} - -void SoundRenderer::loadConfig() { - Config &config= Config::getInstance(); - - fxVolume= config.getInt("SoundVolumeFx")/100.f; - musicVolume= config.getInt("SoundVolumeMusic")/100.f; - ambientVolume= config.getInt("SoundVolumeAmbient")/100.f; -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/sound/sound_renderer.h b/source/glest_game/sound/sound_renderer.h index 0f17196b4..cdf730f1f 100644 --- a/source/glest_game/sound/sound_renderer.h +++ b/source/glest_game/sound/sound_renderer.h @@ -13,8 +13,8 @@ #define _GLEST_GAME_SOUNDRENDERER_H_ #ifdef WIN32 - #include - #include +#include +#include #endif #include "sound.h" @@ -25,73 +25,81 @@ #include "platform_common.h" #include "leak_dumper.h" -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -using ::Shared::Sound::StrSound; -using ::Shared::Sound::StaticSound; -using ::Shared::Sound::SoundPlayer; -using ::Shared::Graphics::Vec3f; -using namespace ::Shared::PlatformCommon; + using ::Shared::Sound::StrSound; + using ::Shared::Sound::StaticSound; + using ::Shared::Sound::SoundPlayer; + using ::Shared::Graphics::Vec3f; + using namespace ::Shared::PlatformCommon; -// ===================================================== -// class SoundRenderer -// -/// Wrapper to acces the shared library sound engine -// ===================================================== + // ===================================================== + // class SoundRenderer + // + /// Wrapper to acces the shared library sound engine + // ===================================================== -class SoundRenderer : public SimpleTaskCallbackInterface { -public: - static const int ambientFade; - static const float audibleDist; -private: - SoundPlayer *soundPlayer; + class SoundRenderer : public SimpleTaskCallbackInterface { + public: + static const int ambientFade; + static const float audibleDist; + private: + SoundPlayer * soundPlayer; - //volume - float fxVolume; - float musicVolume; - float ambientVolume; + //volume + float fxVolume; + float musicVolume; + float ambientVolume; - Mutex *mutex; - bool runThreadSafe; + Mutex *mutex; + bool runThreadSafe; -private: - SoundRenderer(); + private: + SoundRenderer(); - void cleanup(); + void cleanup(); -public: - //misc - virtual ~SoundRenderer(); - static SoundRenderer &getInstance(); - bool init(Window *window); - void update(); - virtual void simpleTask(BaseThread *callingThread,void *userdata) { update(); } - SoundPlayer *getSoundPlayer() const {return soundPlayer;} + public: + //misc + virtual ~SoundRenderer(); + static SoundRenderer &getInstance(); + bool init(Window *window); + void update(); + virtual void simpleTask(BaseThread *callingThread, void *userdata) { + update(); + } + SoundPlayer *getSoundPlayer() const { + return soundPlayer; + } - //music - void playMusic(StrSound *strSound); - void setMusicVolume(StrSound *strSound); - void stopMusic(StrSound *strSound); + //music + void playMusic(StrSound *strSound); + void setMusicVolume(StrSound *strSound); + void stopMusic(StrSound *strSound); - //fx - void playFx(StaticSound *staticSound, Vec3f soundPos, Vec3f camPos); - void playFx(StaticSound *staticSound, bool force=false); + //fx + void playFx(StaticSound *staticSound, Vec3f soundPos, Vec3f camPos); + void playFx(StaticSound *staticSound, bool force = false); - //ambient - void playAmbient(StrSound *strSound); - void stopAmbient(StrSound *strSound); + //ambient + void playAmbient(StrSound *strSound); + void stopAmbient(StrSound *strSound); - //misc - void stopAllSounds(int64 fadeOff=0); - void loadConfig(); + //misc + void stopAllSounds(int64 fadeOff = 0); + void loadConfig(); - bool wasInitOk() const; + bool wasInitOk() const; - bool runningThreaded() const { return runThreadSafe; } + bool runningThreaded() const { + return runThreadSafe; + } - bool isVolumeTurnedOff() const; -}; + bool isVolumeTurnedOff() const; + }; -}}//end namespace + } +}//end namespace #endif diff --git a/source/glest_game/steam/steam.cpp b/source/glest_game/steam/steam.cpp index 01371dc16..d2e9c2c55 100644 --- a/source/glest_game/steam/steam.cpp +++ b/source/glest_game/steam/steam.cpp @@ -5,308 +5,308 @@ #include "steamshim_child.h" #include "platform_common.h" -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -std::map Steam::SteamStatNameTypes = Steam::create_map(); + std::map Steam::SteamStatNameTypes = Steam::create_map(); -// Language map -static inline std::map gen_langToCode() -{ - std::map map; - map["brazilian"] = "pt_BR"; - map["bulgarian"] = "bg"; - map["czech"] = "cz"; - map["danish"] = "da"; - map["dutch"] = "nl"; - map["english"] = "en"; - map["finnish"] = "fi"; - map["french"] = "fr"; - map["german"] = "de"; - map["greek"] = "el"; - map["hungarian"] = "hu"; - map["italian"] = "it"; - map["japanese"] = "ja"; - map["koreana"] = "ko"; - map["korean"] = "ko"; - map["norwegian"] = "no"; - map["polish"] = "pl"; - map["portuguese"] = "pt"; - map["romanian"] = "ro"; - map["russian"] = "ru"; - map["schinese"] = "zh_CN"; - map["spanish"] = "es"; - map["swedish"] = "sv"; - map["tchinese"] = "zh_TW"; - map["thai"] = "th"; - map["turkish"] = "tr"; - map["ukrainian"] = "uk"; - return map; -} -static const std::map langToCode = gen_langToCode(); + // Language map + static inline std::map gen_langToCode() { + std::map map; + map["brazilian"] = "pt_BR"; + map["bulgarian"] = "bg"; + map["czech"] = "cz"; + map["danish"] = "da"; + map["dutch"] = "nl"; + map["english"] = "en"; + map["finnish"] = "fi"; + map["french"] = "fr"; + map["german"] = "de"; + map["greek"] = "el"; + map["hungarian"] = "hu"; + map["italian"] = "it"; + map["japanese"] = "ja"; + map["koreana"] = "ko"; + map["korean"] = "ko"; + map["norwegian"] = "no"; + map["polish"] = "pl"; + map["portuguese"] = "pt"; + map["romanian"] = "ro"; + map["russian"] = "ru"; + map["schinese"] = "zh_CN"; + map["spanish"] = "es"; + map["swedish"] = "sv"; + map["tchinese"] = "zh_TW"; + map["thai"] = "th"; + map["turkish"] = "tr"; + map["ukrainian"] = "uk"; + return map; + } + static const std::map langToCode = gen_langToCode(); -static std::string steamToIsoLang(const char *steamLang) { - //printf("Steam language [%s]\n",steamLang); - std::map::const_iterator it = langToCode.find(steamLang); - if (it != langToCode.end()) { - return it->second; - } - return "en"; -} - -// SteamPrivate -struct SteamPrivate { - static bool debugEnabled; - std::map achievements; - std::map stats; - - std::string userName; - std::string lang; - - SteamPrivate() { - if(debugEnabled) printf("\nCreating private steam state container\n"); - STEAMSHIM_getPersonaName(); - STEAMSHIM_getCurrentGameLanguage(); - STEAMSHIM_requestStats(); - STEAMSHIM_EventType statsReceived = SHIMEVENT_STATSRECEIVED; - - Shared::PlatformCommon::Chrono timerStats; - timerStats.start(); - while(update(&statsReceived) == NULL && timerStats.getMillis() < 2500) { - SDL_Delay(100); + static std::string steamToIsoLang(const char *steamLang) { + //printf("Steam language [%s]\n",steamLang); + std::map::const_iterator it = langToCode.find(steamLang); + if (it != langToCode.end()) { + return it->second; + } + return "en"; } - refreshAllStats(); - } + // SteamPrivate + struct SteamPrivate { + static bool debugEnabled; + std::map achievements; + std::map stats; - static void setDebugEnabled(bool value) { - debugEnabled = value; - } + std::string userName; + std::string lang; - void refreshAllStats() { - achievements.clear(); - stats.clear(); + SteamPrivate() { + if (debugEnabled) printf("\nCreating private steam state container\n"); + STEAMSHIM_getPersonaName(); + STEAMSHIM_getCurrentGameLanguage(); + STEAMSHIM_requestStats(); + STEAMSHIM_EventType statsReceived = SHIMEVENT_STATSRECEIVED; - for(int index = 0; index < EnumParser::getCount(); ++index) { - SteamStatName statName = static_cast(index); - string statNameStr = EnumParser::getString(statName); - SteamStatType statType = Steam::getSteamStatNameType(statNameStr); - switch(statType) { - case stat_int: - STEAMSHIM_getStatI(statNameStr.c_str()); - break; - case stat_float: - STEAMSHIM_getStatF(statNameStr.c_str()); - break; - default: + Shared::PlatformCommon::Chrono timerStats; + timerStats.start(); + while (update(&statsReceived) == NULL && timerStats.getMillis() < 2500) { + SDL_Delay(100); + } + + refreshAllStats(); + } + + static void setDebugEnabled(bool value) { + debugEnabled = value; + } + + void refreshAllStats() { + achievements.clear(); + stats.clear(); + + for (int index = 0; index < EnumParser::getCount(); ++index) { + SteamStatName statName = static_cast(index); + string statNameStr = EnumParser::getString(statName); + SteamStatType statType = Steam::getSteamStatNameType(statNameStr); + switch (statType) { + case stat_int: + STEAMSHIM_getStatI(statNameStr.c_str()); + break; + case stat_float: + STEAMSHIM_getStatF(statNameStr.c_str()); + break; + default: + break; + } + } + for (int index = 0; index < EnumParser::getCount(); ++index) { + SteamAchievementName achName = static_cast(index); + string achNameStr = EnumParser::getString(achName); + STEAMSHIM_getAchievement(achNameStr.c_str()); + } + + Shared::PlatformCommon::Chrono timer; + timer.start(); + while (!initialized() && timer.getMillis() < 2500) { + SDL_Delay(100); + update(); + } + } + + void setAchievement(const char *name, bool set) { + achievements[name] = set; + STEAMSHIM_setAchievement(name, set); + } + + void updateAchievement(const char *name, bool isSet) { + achievements[name] = isSet; + } + + bool isAchievementSet(const char *name) { + return achievements[name]; + } + + void updateStat(const char *name, double value) { + stats[name] = value; + } + + int getStatAsInt(const char *name) const { + std::map::const_iterator iterFind = stats.find(name); + if (iterFind != stats.end()) { + return iterFind->second; + } + return 0; + } + double getStatAsDouble(const char *name) const { + std::map::const_iterator iterFind = stats.find(name); + if (iterFind != stats.end()) { + return iterFind->second; + } + return 0; + } + + void setStatAsInt(const char *name, int value) { + STEAMSHIM_setStatI(name, value); + update(); + } + void setStatAsFloat(const char *name, float value) { + STEAMSHIM_setStatF(name, value); + update(); + } + // void clearLocalStats() { + // stats.clear(); + // } + + const STEAMSHIM_Event * update(STEAMSHIM_EventType *waitForEvent = NULL) { + const STEAMSHIM_Event *e; + while ((e = STEAMSHIM_pump()) != 0) { + // Handle events + switch (e->type) { + case SHIMEVENT_GETACHIEVEMENT: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_GETACHIEVEMENT name [%s] value [%d] isOk = %d\n", e->name, e->ivalue, e->okay); + if (e->okay) { + updateAchievement(e->name, e->ivalue); + } + break; + case SHIMEVENT_SETACHIEVEMENT: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_SETACHIEVEMENT for name [%s] value [%d] isOk = %d\n", e->name, e->ivalue, e->okay); + break; + case SHIMEVENT_GETPERSONANAME: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_GETPERSONANAME isOk = %d value [%s]\n", e->okay, e->name); + userName = e->name; + break; + case SHIMEVENT_GETCURRENTGAMELANGUAGE: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_GETCURRENTGAMELANGUAGE isOk = %d value [%s]\n", e->okay, e->name); + lang = steamToIsoLang(e->name); + break; + case SHIMEVENT_STATSRECEIVED: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_STATSRECEIVED isOk = %d\n", e->okay); + break; + case SHIMEVENT_STATSSTORED: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_STATSSTORED isOk = %d\n", e->okay); + break; + case SHIMEVENT_GETSTATI: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_GETSTATI for stat [%s] value [%d] isOk = %d\n", e->name, e->ivalue, e->okay); + if (e->okay) { + updateStat(e->name, e->ivalue); + } + break; + case SHIMEVENT_GETSTATF: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_GETSTATF for stat [%s] value [%f] isOk = %d\n", e->name, e->fvalue, e->okay); + if (e->okay) { + updateStat(e->name, e->fvalue); + } + break; + case SHIMEVENT_SETSTATI: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_SETSTATI for stat [%s] value [%d] isOk = %d\n", e->name, e->ivalue, e->okay); + break; + case SHIMEVENT_SETSTATF: + if (debugEnabled) printf("\nGot Shim event SHIMEVENT_SETSTATF for stat [%s] value [%f] isOk = %d\n", e->name, e->fvalue, e->okay); + break; + default: + if (debugEnabled) printf("\nGot Shim event [%d] isOk = %d\n", e->type, e->okay); + break; + } + if (waitForEvent != NULL && *waitForEvent == e->type) { + return e; + } + } + return NULL; + } + + bool initialized() { + return !userName.empty() + && !lang.empty() + && (int) stats.size() >= EnumParser::getCount() + && (int) achievements.size() >= EnumParser::getCount(); + } + }; + + bool SteamPrivate::debugEnabled = false; + + /* Steam */ + Steam::Steam() : p(new SteamPrivate()) { + } + + Steam::~Steam() { + delete p; + } + + const std::string &Steam::userName() const { + return p->userName; + } + + const std::string &Steam::lang() const { + return p->lang; + } + + void Steam::resetStats(const int bAlsoAchievements) const { + STEAMSHIM_resetStats(false); + p->update(); + } + + void Steam::storeStats() const { + STEAMSHIM_storeStats(); + STEAMSHIM_EventType statsStored = SHIMEVENT_STATSSTORED; + + Shared::PlatformCommon::Chrono timer; + timer.start(); + while (timer.getMillis() < 2500) { + SDL_Delay(100); + const STEAMSHIM_Event *evt = p->update(&statsStored); + if (evt != NULL && evt->type == statsStored) { break; + } } } - for(int index = 0; index < EnumParser::getCount(); ++index) { - SteamAchievementName achName = static_cast(index); - string achNameStr = EnumParser::getString(achName); - STEAMSHIM_getAchievement(achNameStr.c_str()); + + int Steam::getStatAsInt(const char *name) const { + return p->getStatAsInt(name); } - Shared::PlatformCommon::Chrono timer; - timer.start(); - while(!initialized() && timer.getMillis() < 2500) { - SDL_Delay(100); - update(); + double Steam::getStatAsDouble(const char *name) const { + return p->getStatAsDouble(name); } - } - void setAchievement(const char *name, bool set) { - achievements[name] = set; - STEAMSHIM_setAchievement(name, set); - } - - void updateAchievement(const char *name, bool isSet) { - achievements[name] = isSet; - } - - bool isAchievementSet(const char *name) { - return achievements[name]; - } - - void updateStat(const char *name, double value) { - stats[name] = value; - } - - int getStatAsInt(const char *name) const { - std::map::const_iterator iterFind = stats.find(name); - if(iterFind != stats.end()) { - return iterFind->second; + void Steam::setStatAsInt(const char *name, int value) { + p->setStatAsInt(name, value); } - return 0; - } - double getStatAsDouble(const char *name) const { - std::map::const_iterator iterFind = stats.find(name); - if(iterFind != stats.end()) { - return iterFind->second; + void Steam::setStatAsDouble(const char *name, double value) { + p->setStatAsFloat(name, value); } - return 0; - } - void setStatAsInt(const char *name, int value) { - STEAMSHIM_setStatI(name, value); - update(); - } - void setStatAsFloat(const char *name, float value) { - STEAMSHIM_setStatF(name, value); - update(); - } -// void clearLocalStats() { -// stats.clear(); -// } - - const STEAMSHIM_Event * update(STEAMSHIM_EventType *waitForEvent=NULL) { - const STEAMSHIM_Event *e; - while ((e = STEAMSHIM_pump()) != 0) { - // Handle events - switch (e->type) - { - case SHIMEVENT_GETACHIEVEMENT: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_GETACHIEVEMENT name [%s] value [%d] isOk = %d\n",e->name,e->ivalue,e->okay); - if(e->okay) { - updateAchievement(e->name, e->ivalue); - } - break; - case SHIMEVENT_SETACHIEVEMENT: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_SETACHIEVEMENT for name [%s] value [%d] isOk = %d\n",e->name,e->ivalue,e->okay); - break; - case SHIMEVENT_GETPERSONANAME: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_GETPERSONANAME isOk = %d value [%s]\n",e->okay,e->name); - userName = e->name; - break; - case SHIMEVENT_GETCURRENTGAMELANGUAGE: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_GETCURRENTGAMELANGUAGE isOk = %d value [%s]\n",e->okay,e->name); - lang = steamToIsoLang(e->name); - break; - case SHIMEVENT_STATSRECEIVED: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_STATSRECEIVED isOk = %d\n",e->okay); - break; - case SHIMEVENT_STATSSTORED: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_STATSSTORED isOk = %d\n",e->okay); - break; - case SHIMEVENT_GETSTATI: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_GETSTATI for stat [%s] value [%d] isOk = %d\n",e->name,e->ivalue,e->okay); - if(e->okay) { - updateStat(e->name, e->ivalue); - } - break; - case SHIMEVENT_GETSTATF: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_GETSTATF for stat [%s] value [%f] isOk = %d\n",e->name,e->fvalue,e->okay); - if(e->okay) { - updateStat(e->name, e->fvalue); - } - break; - case SHIMEVENT_SETSTATI: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_SETSTATI for stat [%s] value [%d] isOk = %d\n",e->name,e->ivalue,e->okay); - break; - case SHIMEVENT_SETSTATF: - if(debugEnabled) printf("\nGot Shim event SHIMEVENT_SETSTATF for stat [%s] value [%f] isOk = %d\n",e->name,e->fvalue,e->okay); - break; - default: - if(debugEnabled) printf("\nGot Shim event [%d] isOk = %d\n",e->type,e->okay); - break; + void Steam::requestRefreshStats() { + STEAMSHIM_requestStats(); + STEAMSHIM_EventType statsReceived = SHIMEVENT_STATSRECEIVED; + Shared::PlatformCommon::Chrono timerStats; + timerStats.start(); + while (p->update(&statsReceived) == NULL && timerStats.getMillis() < 2500) { + SDL_Delay(100); } - if(waitForEvent != NULL && *waitForEvent == e->type) { - return e; - } - } - return NULL; - } - - bool initialized() { - return !userName.empty() - && !lang.empty() - && (int)stats.size() >= EnumParser::getCount() - && (int)achievements.size() >= EnumParser::getCount(); - } -}; - -bool SteamPrivate::debugEnabled = false; - -/* Steam */ -Steam::Steam() : p(new SteamPrivate()) { -} - -Steam::~Steam() { - delete p; -} - -const std::string &Steam::userName() const { - return p->userName; -} - -const std::string &Steam::lang() const { - return p->lang; -} - -void Steam::resetStats(const int bAlsoAchievements) const { - STEAMSHIM_resetStats(false); - p->update(); -} - -void Steam::storeStats() const { - STEAMSHIM_storeStats(); - STEAMSHIM_EventType statsStored = SHIMEVENT_STATSSTORED; - - Shared::PlatformCommon::Chrono timer; - timer.start(); - while(timer.getMillis() < 2500) { - SDL_Delay(100); - const STEAMSHIM_Event *evt = p->update(&statsStored); - if(evt != NULL && evt->type == statsStored) { - break; + p->refreshAllStats(); } + + SteamStatType Steam::getSteamStatNameType(string value) { + return SteamStatNameTypes[value]; + } + + void Steam::unlock(const char *name) { + p->setAchievement(name, true); + } + + void Steam::lock(const char *name) { + p->setAchievement(name, false); + } + + bool Steam::isUnlocked(const char *name) { + return p->isAchievementSet(name); + } + + void Steam::setDebugEnabled(bool value) { + SteamPrivate::setDebugEnabled(value); + } + } -} - -int Steam::getStatAsInt(const char *name) const { - return p->getStatAsInt(name); -} - -double Steam::getStatAsDouble(const char *name) const { - return p->getStatAsDouble(name); -} - -void Steam::setStatAsInt(const char *name, int value) { - p->setStatAsInt(name, value); -} -void Steam::setStatAsDouble(const char *name, double value) { - p->setStatAsFloat(name, value); -} - -void Steam::requestRefreshStats() { - STEAMSHIM_requestStats(); - STEAMSHIM_EventType statsReceived = SHIMEVENT_STATSRECEIVED; - Shared::PlatformCommon::Chrono timerStats; - timerStats.start(); - while(p->update(&statsReceived) == NULL && timerStats.getMillis() < 2500) { - SDL_Delay(100); - } - p->refreshAllStats(); -} - -SteamStatType Steam::getSteamStatNameType(string value) { - return SteamStatNameTypes[value]; -} - -void Steam::unlock(const char *name) { - p->setAchievement(name, true); -} - -void Steam::lock(const char *name) { - p->setAchievement(name, false); -} - -bool Steam::isUnlocked(const char *name) { - return p->isAchievementSet(name); -} - -void Steam::setDebugEnabled(bool value) { - SteamPrivate::setDebugEnabled(value); -} - -}}//end namespace +}//end namespace diff --git a/source/glest_game/steam/steam.h b/source/glest_game/steam/steam.h index 10b867719..e459ec956 100644 --- a/source/glest_game/steam/steam.h +++ b/source/glest_game/steam/steam.h @@ -5,98 +5,99 @@ #include #include "game_constants.h" -namespace Glest{ namespace Game{ +namespace Glest { + namespace Game { -struct SteamPrivate; + struct SteamPrivate; -enum SteamStatName { - stat_online_wins, - stat_online_loses, - stat_online_kills, - stat_online_kills_enemy, - stat_online_deaths, - stat_online_units, - stat_online_resources_harvested, - stat_online_quit_before_end, - stat_online_minutes_played -}; + enum SteamStatName { + stat_online_wins, + stat_online_loses, + stat_online_kills, + stat_online_kills_enemy, + stat_online_deaths, + stat_online_units, + stat_online_resources_harvested, + stat_online_quit_before_end, + stat_online_minutes_played + }; -enum SteamStatType { - stat_int, - stat_float, - stat_avg -}; + enum SteamStatType { + stat_int, + stat_float, + stat_avg + }; -template <> -inline EnumParser::EnumParser() { - enumMap["stat_online_wins"] = stat_online_wins; - enumMap["stat_online_loses"] = stat_online_loses; - enumMap["stat_online_kills"] = stat_online_kills; - enumMap["stat_online_kills_enemy"] = stat_online_kills_enemy; - enumMap["stat_online_deaths"] = stat_online_deaths; - enumMap["stat_online_units"] = stat_online_units; - enumMap["stat_online_resources_harvested"] = stat_online_resources_harvested; - enumMap["stat_online_quit_before_end"] = stat_online_quit_before_end; - enumMap["stat_online_minutes_played"] = stat_online_minutes_played; -} + template <> + inline EnumParser::EnumParser() { + enumMap["stat_online_wins"] = stat_online_wins; + enumMap["stat_online_loses"] = stat_online_loses; + enumMap["stat_online_kills"] = stat_online_kills; + enumMap["stat_online_kills_enemy"] = stat_online_kills_enemy; + enumMap["stat_online_deaths"] = stat_online_deaths; + enumMap["stat_online_units"] = stat_online_units; + enumMap["stat_online_resources_harvested"] = stat_online_resources_harvested; + enumMap["stat_online_quit_before_end"] = stat_online_quit_before_end; + enumMap["stat_online_minutes_played"] = stat_online_minutes_played; + } -enum SteamAchievementName { - ACH_WIN_ONE_GAME, - ACH_WIN_ONE_GAME_ONLINE -}; + enum SteamAchievementName { + ACH_WIN_ONE_GAME, + ACH_WIN_ONE_GAME_ONLINE + }; -template <> -inline EnumParser::EnumParser() { - enumMap["ACH_WIN_ONE_GAME"] = ACH_WIN_ONE_GAME; - enumMap["ACH_WIN_ONE_GAME_ONLINE"] = ACH_WIN_ONE_GAME_ONLINE; -} + template <> + inline EnumParser::EnumParser() { + enumMap["ACH_WIN_ONE_GAME"] = ACH_WIN_ONE_GAME; + enumMap["ACH_WIN_ONE_GAME_ONLINE"] = ACH_WIN_ONE_GAME_ONLINE; + } -class Steam -{ -public: - void unlock(const char *name); - void lock(const char *name); - bool isUnlocked(const char *name); + class Steam { + public: + void unlock(const char *name); + void lock(const char *name); + bool isUnlocked(const char *name); - static SteamStatType getSteamStatNameType(string value); + static SteamStatType getSteamStatNameType(string value); - const std::string &userName() const; - const std::string &lang() const; + const std::string &userName() const; + const std::string &lang() const; - void resetStats(const int bAlsoAchievements) const; - void storeStats() const; - int getStatAsInt(const char *name) const; - double getStatAsDouble(const char *name) const; - void setStatAsInt(const char *name, int value); - void setStatAsDouble(const char *name, double value); + void resetStats(const int bAlsoAchievements) const; + void storeStats() const; + int getStatAsInt(const char *name) const; + double getStatAsDouble(const char *name) const; + void setStatAsInt(const char *name, int value); + void setStatAsDouble(const char *name, double value); - void requestRefreshStats(); - static void setDebugEnabled(bool value); + void requestRefreshStats(); + static void setDebugEnabled(bool value); - Steam(); - ~Steam(); + Steam(); + ~Steam(); -private: - //friend struct SharedStatePrivate; + private: + //friend struct SharedStatePrivate; - SteamPrivate *p; - static std::map SteamStatNameTypes; + SteamPrivate * p; + static std::map SteamStatNameTypes; + + static std::map create_map() { + std::map steamStatNameTypes; + steamStatNameTypes["stat_online_wins"] = stat_int; + steamStatNameTypes["stat_online_loses"] = stat_int; + steamStatNameTypes["stat_online_kills"] = stat_int; + steamStatNameTypes["stat_online_kills_enemy"] = stat_int; + steamStatNameTypes["stat_online_deaths"] = stat_int; + steamStatNameTypes["stat_online_units"] = stat_int; + steamStatNameTypes["stat_online_resources_harvested"] = stat_int; + steamStatNameTypes["stat_online_quit_before_end"] = stat_int; + steamStatNameTypes["stat_online_minutes_played"] = stat_float; + return steamStatNameTypes; + } + }; - static std::map create_map() { - std::map steamStatNameTypes; - steamStatNameTypes["stat_online_wins"] = stat_int; - steamStatNameTypes["stat_online_loses"] = stat_int; - steamStatNameTypes["stat_online_kills"] = stat_int; - steamStatNameTypes["stat_online_kills_enemy"] = stat_int; - steamStatNameTypes["stat_online_deaths"] = stat_int; - steamStatNameTypes["stat_online_units"] = stat_int; - steamStatNameTypes["stat_online_resources_harvested"] = stat_int; - steamStatNameTypes["stat_online_quit_before_end"] = stat_int; - steamStatNameTypes["stat_online_minutes_played"] = stat_float; - return steamStatNameTypes; } -}; - -}}//end namespace +}//end namespace #endif // STEAM_H diff --git a/source/glest_game/steamshim/steamshim_child.c b/source/glest_game/steamshim/steamshim_child.c index be456a6ec..8f726eaa2 100644 --- a/source/glest_game/steamshim/steamshim_child.c +++ b/source/glest_game/steamshim/steamshim_child.c @@ -2,6 +2,7 @@ #ifdef _WIN32 #define WIN32_LEAN_AND_MEAN 1 #include +#include typedef HANDLE PipeType; #define NULLPIPE NULL typedef unsigned __int8 uint8; @@ -28,7 +29,8 @@ typedef int PipeType; #ifdef STEAMSHIM_DEBUG #define dbgpipe printf #else -static inline void dbgpipe(const char *fmt, ...) {} +static inline void dbgpipe(const char *fmt, ...) { +} #endif static int writePipe(PipeType fd, const void *buf, const unsigned int _len); @@ -40,76 +42,69 @@ static int pipeReady(PipeType fd); #ifdef _WIN32 -static int pipeReady(PipeType fd) -{ - DWORD avail = 0; - return (PeekNamedPipe(fd, NULL, 0, NULL, &avail, NULL) && (avail > 0)); +static int pipeReady(PipeType fd) { + DWORD avail = 0; + return (PeekNamedPipe(fd, NULL, 0, NULL, &avail, NULL) && (avail > 0)); } /* pipeReady */ -static int writePipe(PipeType fd, const void *buf, const unsigned int _len) -{ - const DWORD len = (DWORD) _len; - DWORD bw = 0; - return ((WriteFile(fd, buf, len, &bw, NULL) != 0) && (bw == len)); +static int writePipe(PipeType fd, const void *buf, const unsigned int _len) { + const DWORD len = (DWORD) _len; + DWORD bw = 0; + return ((WriteFile(fd, buf, len, &bw, NULL) != 0) && (bw == len)); } /* writePipe */ -static int readPipe(PipeType fd, void *buf, const unsigned int _len) -{ - const DWORD len = (DWORD) _len; - DWORD br = 0; - return ReadFile(fd, buf, len, &br, NULL) ? (int) br : -1; +static int readPipe(PipeType fd, void *buf, const unsigned int _len) { + const DWORD len = (DWORD) _len; + DWORD br = 0; + return ReadFile(fd, buf, len, &br, NULL) ? (int) br : -1; } /* readPipe */ -static void closePipe(PipeType fd) -{ - CloseHandle(fd); +static void closePipe(PipeType fd) { + CloseHandle(fd); } /* closePipe */ -static char *getEnvVar(const char *key, char *buf, const size_t buflen) -{ - const DWORD rc = GetEnvironmentVariableA(key, buf, buflen); - /* rc doesn't count null char, hence "<". */ - return ((rc > 0) && (rc < buflen)) ? buf : NULL; +static char *getEnvVar(const char *key, char *buf, const size_t buflen) { + const DWORD rc = GetEnvironmentVariableA(key, buf, buflen); + /* rc doesn't count null char, hence "<". */ + return ((rc > 0) && (rc < buflen)) ? buf : NULL; } /* getEnvVar */ #else -static int pipeReady(PipeType fd) -{ - int rc; - struct pollfd pfd = { fd, POLLIN | POLLERR | POLLHUP, 0 }; - while (((rc = poll(&pfd, 1, 0)) == -1) && (errno == EINTR)) { /*spin*/ } - return (rc == 1); +static int pipeReady(PipeType fd) { + int rc; + struct pollfd pfd = { fd, POLLIN | POLLERR | POLLHUP, 0 }; + while (((rc = poll(&pfd, 1, 0)) == -1) && (errno == EINTR)) { /*spin*/ + } + return (rc == 1); } /* pipeReady */ -static int writePipe(PipeType fd, const void *buf, const unsigned int _len) -{ - const ssize_t len = (ssize_t) _len; - ssize_t bw; - while (((bw = write(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ } - return (bw == len); +static int writePipe(PipeType fd, const void *buf, const unsigned int _len) { + const ssize_t len = (ssize_t) _len; + ssize_t bw; + while (((bw = write(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ + } + return (bw == len); } /* writePipe */ -static int readPipe(PipeType fd, void *buf, const unsigned int _len) -{ - const ssize_t len = (ssize_t) _len; - ssize_t br; - while (((br = read(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ } - return (int) br; +static int readPipe(PipeType fd, void *buf, const unsigned int _len) { + const ssize_t len = (ssize_t) _len; + ssize_t br; + while (((br = read(fd, buf, len)) == -1) && (errno == EINTR)) { /*spin*/ + } + return (int) br; } /* readPipe */ -static void closePipe(PipeType fd) -{ - close(fd); +static void closePipe(PipeType fd) { + close(fd); } /* closePipe */ -static char *getEnvVar(const char *key, char *buf, const size_t buflen) -{ - const char *envr = getenv(key); - if (!envr || (strlen(envr) >= buflen)) - return NULL; - strcpy(buf, envr); - return buf; +static char *getEnvVar(const char *key, char *buf, const size_t buflen) { + const char *envr = getenv(key); + if (!envr || (strlen(envr) >= buflen)) + return NULL; + strcpy(buf, envr); + return buf; } /* getEnvVar */ #endif @@ -118,346 +113,316 @@ static char *getEnvVar(const char *key, char *buf, const size_t buflen) static PipeType GPipeRead = NULLPIPE; static PipeType GPipeWrite = NULLPIPE; -typedef enum ShimCmd -{ - SHIMCMD_BYE, - SHIMCMD_PUMP, - SHIMCMD_REQUESTSTATS, - SHIMCMD_STORESTATS, - SHIMCMD_SETACHIEVEMENT, - SHIMCMD_GETACHIEVEMENT, - SHIMCMD_RESETSTATS, - SHIMCMD_SETSTATI, - SHIMCMD_GETSTATI, - SHIMCMD_SETSTATF, - SHIMCMD_GETSTATF, - SHIMCMD_GETPERSONANAME, - SHIMCMD_GETCURRENTGAMELANGUAGE, +typedef enum ShimCmd { + SHIMCMD_BYE, + SHIMCMD_PUMP, + SHIMCMD_REQUESTSTATS, + SHIMCMD_STORESTATS, + SHIMCMD_SETACHIEVEMENT, + SHIMCMD_GETACHIEVEMENT, + SHIMCMD_RESETSTATS, + SHIMCMD_SETSTATI, + SHIMCMD_GETSTATI, + SHIMCMD_SETSTATF, + SHIMCMD_GETSTATF, + SHIMCMD_GETPERSONANAME, + SHIMCMD_GETCURRENTGAMELANGUAGE, } ShimCmd; -static int write1ByteCmd(const uint8 b1) -{ - const uint8 buf[] = { 1, b1 }; - return writePipe(GPipeWrite, buf, sizeof (buf)); +static int write1ByteCmd(const uint8 b1) { + const uint8 buf[] = { 1, b1 }; + return writePipe(GPipeWrite, buf, sizeof(buf)); } /* write1ByteCmd */ -static int write2ByteCmd(const uint8 b1, const uint8 b2) -{ - const uint8 buf[] = { 2, b1, b2 }; - return writePipe(GPipeWrite, buf, sizeof (buf)); +static int write2ByteCmd(const uint8 b1, const uint8 b2) { + const uint8 buf[] = { 2, b1, b2 }; + return writePipe(GPipeWrite, buf, sizeof(buf)); } /* write2ByteCmd */ -static inline int writeBye(void) -{ - dbgpipe("Child sending SHIMCMD_BYE().\n"); - return write1ByteCmd(SHIMCMD_BYE); +static inline int writeBye(void) { + dbgpipe("Child sending SHIMCMD_BYE().\n"); + return write1ByteCmd(SHIMCMD_BYE); } // writeBye -static int initPipes(void) -{ - char buf[64]; +static int initPipes(void) { + char buf[64]; - if (!getEnvVar("STEAMSHIM_READHANDLE", buf, sizeof (buf))) - return 0; - GPipeRead = (PipeType) strtoull(buf, 0, 10); + if (!getEnvVar("STEAMSHIM_READHANDLE", buf, sizeof(buf))) + return 0; + GPipeRead = (PipeType) strtoull(buf, 0, 10); - if (!getEnvVar("STEAMSHIM_WRITEHANDLE", buf, sizeof (buf))) - return 0; - GPipeWrite = (PipeType) strtoull(buf, 0, 10); + if (!getEnvVar("STEAMSHIM_WRITEHANDLE", buf, sizeof(buf))) + return 0; + GPipeWrite = (PipeType) strtoull(buf, 0, 10); - return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE)); + return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE)); } /* initPipes */ -int STEAMSHIM_init(void) -{ +int STEAMSHIM_init(void) { printf("Initializing Steam Shim...\n"); - dbgpipe("Child init start.\n"); - if (!initPipes()) - { - dbgpipe("Child init failed.\n"); - return 0; - } /* if */ + dbgpipe("Child init start.\n"); + if (!initPipes()) { + dbgpipe("Child init failed.\n"); + return 0; + } /* if */ #ifndef _WIN32 - signal(SIGPIPE, SIG_IGN); + signal(SIGPIPE, SIG_IGN); #endif - printf("Init of Steam Shim successful!\n"); - dbgpipe("Child init success!\n"); - return 1; + printf("Init of Steam Shim successful!\n"); + dbgpipe("Child init success!\n"); + return 1; } /* STEAMSHIM_init */ -void STEAMSHIM_deinit(void) -{ - dbgpipe("Child deinit.\n"); - if (GPipeWrite != NULLPIPE) - { - writeBye(); - closePipe(GPipeWrite); - } /* if */ +void STEAMSHIM_deinit(void) { + dbgpipe("Child deinit.\n"); + if (GPipeWrite != NULLPIPE) { + writeBye(); + closePipe(GPipeWrite); + } /* if */ - if (GPipeRead != NULLPIPE) - closePipe(GPipeRead); + if (GPipeRead != NULLPIPE) + closePipe(GPipeRead); - GPipeRead = GPipeWrite = NULLPIPE; + GPipeRead = GPipeWrite = NULLPIPE; #ifndef _WIN32 - signal(SIGPIPE, SIG_DFL); + signal(SIGPIPE, SIG_DFL); #endif } /* STEAMSHIM_deinit */ -static inline int isAlive(void) -{ - return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE)); +static inline int isAlive(void) { + return ((GPipeRead != NULLPIPE) && (GPipeWrite != NULLPIPE)); } /* isAlive */ -static inline int isDead(void) -{ - return !isAlive(); +static inline int isDead(void) { + return !isAlive(); } /* isDead */ -int STEAMSHIM_alive(void) -{ - return isAlive(); +int STEAMSHIM_alive(void) { + return isAlive(); } /* STEAMSHIM_alive */ -static const STEAMSHIM_Event *processEvent(const uint8 *buf, size_t buflen) -{ - static STEAMSHIM_Event event; - const STEAMSHIM_EventType type = (STEAMSHIM_EventType) *(buf++); - buflen--; +static const STEAMSHIM_Event *processEvent(const uint8 *buf, size_t buflen) { + static STEAMSHIM_Event event; + const STEAMSHIM_EventType type = (STEAMSHIM_EventType) *(buf++); + buflen--; - memset(&event, '\0', sizeof (event)); - event.type = type; - event.okay = 1; + memset(&event, '\0', sizeof(event)); + event.type = type; + event.okay = 1; - #ifdef STEAMSHIM_DEBUG - if (0) {} - #define PRINTGOTEVENT(x) else if (type == x) printf("Child got " #x ".\n") - PRINTGOTEVENT(SHIMEVENT_BYE); - PRINTGOTEVENT(SHIMEVENT_STATSRECEIVED); - PRINTGOTEVENT(SHIMEVENT_STATSSTORED); - PRINTGOTEVENT(SHIMEVENT_SETACHIEVEMENT); - PRINTGOTEVENT(SHIMEVENT_GETACHIEVEMENT); - PRINTGOTEVENT(SHIMEVENT_RESETSTATS); - PRINTGOTEVENT(SHIMEVENT_SETSTATI); - PRINTGOTEVENT(SHIMEVENT_GETSTATI); - PRINTGOTEVENT(SHIMEVENT_SETSTATF); - PRINTGOTEVENT(SHIMEVENT_GETSTATF); - PRINTGOTEVENT(SHIMEVENT_GETPERSONANAME); - PRINTGOTEVENT(SHIMEVENT_GETCURRENTGAMELANGUAGE); - #undef PRINTGOTEVENT - else printf("Child got unknown shimevent %d.\n", (int) type); - #endif +#ifdef STEAMSHIM_DEBUG + if (0) { + } +#define PRINTGOTEVENT(x) else if (type == x) printf("Child got " #x ".\n") + PRINTGOTEVENT(SHIMEVENT_BYE); + PRINTGOTEVENT(SHIMEVENT_STATSRECEIVED); + PRINTGOTEVENT(SHIMEVENT_STATSSTORED); + PRINTGOTEVENT(SHIMEVENT_SETACHIEVEMENT); + PRINTGOTEVENT(SHIMEVENT_GETACHIEVEMENT); + PRINTGOTEVENT(SHIMEVENT_RESETSTATS); + PRINTGOTEVENT(SHIMEVENT_SETSTATI); + PRINTGOTEVENT(SHIMEVENT_GETSTATI); + PRINTGOTEVENT(SHIMEVENT_SETSTATF); + PRINTGOTEVENT(SHIMEVENT_GETSTATF); + PRINTGOTEVENT(SHIMEVENT_GETPERSONANAME); + PRINTGOTEVENT(SHIMEVENT_GETCURRENTGAMELANGUAGE); +#undef PRINTGOTEVENT +else printf("Child got unknown shimevent %d.\n", (int) type); +#endif - switch (type) - { - case SHIMEVENT_BYE: - break; +switch (type) { + case SHIMEVENT_BYE: + break; - case SHIMEVENT_STATSRECEIVED: - case SHIMEVENT_STATSSTORED: - if (!buflen) return NULL; - event.okay = *(buf++) ? 1 : 0; - break; + case SHIMEVENT_STATSRECEIVED: + case SHIMEVENT_STATSSTORED: + if (!buflen) return NULL; + event.okay = *(buf++) ? 1 : 0; + break; - case SHIMEVENT_SETACHIEVEMENT: - if (buflen < 3) return NULL; - event.ivalue = *(buf++) ? 1 : 0; - event.okay = *(buf++) ? 1 : 0; - strcpy(event.name, (const char *) buf); - break; + case SHIMEVENT_SETACHIEVEMENT: + if (buflen < 3) return NULL; + event.ivalue = *(buf++) ? 1 : 0; + event.okay = *(buf++) ? 1 : 0; + strcpy(event.name, (const char *) buf); + break; - case SHIMEVENT_GETACHIEVEMENT: - if (buflen < 10) return NULL; - event.ivalue = (int) *(buf++); - if (event.ivalue == 2) - event.ivalue = event.okay = 0; - event.epochsecs = (long long unsigned) *((uint64 *) buf); - buf += sizeof (uint64); - strcpy(event.name, (const char *) buf); - break; + case SHIMEVENT_GETACHIEVEMENT: + if (buflen < 10) return NULL; + event.ivalue = (int) *(buf++); + if (event.ivalue == 2) + event.ivalue = event.okay = 0; + event.epochsecs = (long long unsigned) *((uint64 *) buf); + buf += sizeof(uint64); + strcpy(event.name, (const char *) buf); + break; - case SHIMEVENT_RESETSTATS: - if (buflen != 2) return NULL; - event.ivalue = *(buf++) ? 1 : 0; - event.okay = *(buf++) ? 1 : 0; - break; + case SHIMEVENT_RESETSTATS: + if (buflen != 2) return NULL; + event.ivalue = *(buf++) ? 1 : 0; + event.okay = *(buf++) ? 1 : 0; + break; - case SHIMEVENT_SETSTATI: - case SHIMEVENT_GETSTATI: - event.okay = *(buf++) ? 1 : 0; - event.ivalue = (int) *((int32 *) buf); - buf += sizeof (int32); - strcpy(event.name, (const char *) buf); - break; + case SHIMEVENT_SETSTATI: + case SHIMEVENT_GETSTATI: + event.okay = *(buf++) ? 1 : 0; + event.ivalue = (int) *((int32 *) buf); + buf += sizeof(int32); + strcpy(event.name, (const char *) buf); + break; - case SHIMEVENT_SETSTATF: - case SHIMEVENT_GETSTATF: - event.okay = *(buf++) ? 1 : 0; - event.fvalue = (int) *((float *) buf); - buf += sizeof (float); - strcpy(event.name, (const char *) buf); - break; + case SHIMEVENT_SETSTATF: + case SHIMEVENT_GETSTATF: + event.okay = *(buf++) ? 1 : 0; + event.fvalue = (int) *((float *) buf); + buf += sizeof(float); + strcpy(event.name, (const char *) buf); + break; - case SHIMEVENT_GETPERSONANAME: - case SHIMEVENT_GETCURRENTGAMELANGUAGE: - strcpy(event.name, (const char *) buf); - break; + case SHIMEVENT_GETPERSONANAME: + case SHIMEVENT_GETCURRENTGAMELANGUAGE: + strcpy(event.name, (const char *) buf); + break; - default: /* uh oh */ - return NULL; - } /* switch */ + default: /* uh oh */ + return NULL; +} /* switch */ - return &event; +return &event; } /* processEvent */ -const STEAMSHIM_Event *STEAMSHIM_pump(void) -{ - static uint8 buf[256]; - static int br = 0; - int evlen = (br > 0) ? ((int) buf[0]) : 0; +const STEAMSHIM_Event *STEAMSHIM_pump(void) { + static uint8 buf[256]; + static int br = 0; + int evlen = (br > 0) ? ((int) buf[0]) : 0; - if (isDead()) - return NULL; + if (isDead()) + return NULL; - if (br <= evlen) /* we have an incomplete commmand. Try to read more. */ - { - if (pipeReady(GPipeRead)) - { - const int morebr = readPipe(GPipeRead, buf + br, sizeof (buf) - br); - if (morebr > 0) - br += morebr; - else /* uh oh */ - { - dbgpipe("Child readPipe failed! Shutting down.\n"); - STEAMSHIM_deinit(); /* kill it all. */ - } /* else */ - } /* if */ - } /* if */ + if (br <= evlen) /* we have an incomplete commmand. Try to read more. */ + { + if (pipeReady(GPipeRead)) { + const int morebr = readPipe(GPipeRead, buf + br, sizeof(buf) - br); + if (morebr > 0) + br += morebr; + else /* uh oh */ + { + dbgpipe("Child readPipe failed! Shutting down.\n"); + STEAMSHIM_deinit(); /* kill it all. */ + } /* else */ + } /* if */ + } /* if */ - if (evlen && (br > evlen)) - { - const STEAMSHIM_Event *retval = processEvent(buf+1, evlen); - br -= evlen + 1; - if (br > 0) - memmove(buf, buf+evlen+1, br); - return retval; - } /* if */ + if (evlen && (br > evlen)) { + const STEAMSHIM_Event *retval = processEvent(buf + 1, evlen); + br -= evlen + 1; + if (br > 0) + memmove(buf, buf + evlen + 1, br); + return retval; + } /* if */ - /* Run Steam event loop. */ - if (br == 0) - { - dbgpipe("Child sending SHIMCMD_PUMP().\n"); - write1ByteCmd(SHIMCMD_PUMP); - } /* if */ + /* Run Steam event loop. */ + if (br == 0) { + dbgpipe("Child sending SHIMCMD_PUMP().\n"); + write1ByteCmd(SHIMCMD_PUMP); + } /* if */ - return NULL; + return NULL; } /* STEAMSHIM_pump */ -void STEAMSHIM_requestStats(void) -{ - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_REQUESTSTATS().\n"); - write1ByteCmd(SHIMCMD_REQUESTSTATS); +void STEAMSHIM_requestStats(void) { + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_REQUESTSTATS().\n"); + write1ByteCmd(SHIMCMD_REQUESTSTATS); } /* STEAMSHIM_requestStats */ -void STEAMSHIM_storeStats(void) -{ - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_STORESTATS().\n"); - write1ByteCmd(SHIMCMD_STORESTATS); +void STEAMSHIM_storeStats(void) { + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_STORESTATS().\n"); + write1ByteCmd(SHIMCMD_STORESTATS); } /* STEAMSHIM_storeStats */ -void STEAMSHIM_setAchievement(const char *name, const int enable) -{ - uint8 buf[256]; - uint8 *ptr = buf+1; - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_SETACHIEVEMENT('%s', %senable).\n", name, enable ? "" : "!"); - *(ptr++) = (uint8) SHIMCMD_SETACHIEVEMENT; - *(ptr++) = enable ? 1 : 0; - strcpy((char *) ptr, name); - ptr += strlen(name) + 1; - buf[0] = (uint8) ((ptr-1) - buf); - writePipe(GPipeWrite, buf, buf[0] + 1); +void STEAMSHIM_setAchievement(const char *name, const int enable) { + uint8 buf[256]; + uint8 *ptr = buf + 1; + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_SETACHIEVEMENT('%s', %senable).\n", name, enable ? "" : "!"); + *(ptr++) = (uint8) SHIMCMD_SETACHIEVEMENT; + *(ptr++) = enable ? 1 : 0; + strcpy((char *) ptr, name); + ptr += strlen(name) + 1; + buf[0] = (uint8) ((ptr - 1) - buf); + writePipe(GPipeWrite, buf, buf[0] + 1); } /* STEAMSHIM_setAchievement */ -void STEAMSHIM_getAchievement(const char *name) -{ - uint8 buf[256]; - uint8 *ptr = buf+1; - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_GETACHIEVEMENT('%s').\n", name); - *(ptr++) = (uint8) SHIMCMD_GETACHIEVEMENT; - strcpy((char *) ptr, name); - ptr += strlen(name) + 1; - buf[0] = (uint8) ((ptr-1) - buf); - writePipe(GPipeWrite, buf, buf[0] + 1); +void STEAMSHIM_getAchievement(const char *name) { + uint8 buf[256]; + uint8 *ptr = buf + 1; + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_GETACHIEVEMENT('%s').\n", name); + *(ptr++) = (uint8) SHIMCMD_GETACHIEVEMENT; + strcpy((char *) ptr, name); + ptr += strlen(name) + 1; + buf[0] = (uint8) ((ptr - 1) - buf); + writePipe(GPipeWrite, buf, buf[0] + 1); } /* STEAMSHIM_getAchievement */ -void STEAMSHIM_resetStats(const int bAlsoAchievements) -{ - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_RESETSTATS(%salsoAchievements).\n", bAlsoAchievements ? "" : "!"); - write2ByteCmd(SHIMCMD_RESETSTATS, bAlsoAchievements ? 1 : 0); +void STEAMSHIM_resetStats(const int bAlsoAchievements) { + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_RESETSTATS(%salsoAchievements).\n", bAlsoAchievements ? "" : "!"); + write2ByteCmd(SHIMCMD_RESETSTATS, bAlsoAchievements ? 1 : 0); } /* STEAMSHIM_resetStats */ -static void writeStatThing(const ShimCmd cmd, const char *name, const void *val, const size_t vallen) -{ - uint8 buf[256]; - uint8 *ptr = buf+1; - if (isDead()) return; - *(ptr++) = (uint8) cmd; - if (vallen) - { - memcpy(ptr, val, vallen); - ptr += vallen; - } /* if */ - strcpy((char *) ptr, name); - ptr += strlen(name) + 1; - buf[0] = (uint8) ((ptr-1) - buf); - writePipe(GPipeWrite, buf, buf[0] + 1); +static void writeStatThing(const ShimCmd cmd, const char *name, const void *val, const size_t vallen) { + uint8 buf[256]; + uint8 *ptr = buf + 1; + if (isDead()) return; + *(ptr++) = (uint8) cmd; + if (vallen) { + memcpy(ptr, val, vallen); + ptr += vallen; + } /* if */ + strcpy((char *) ptr, name); + ptr += strlen(name) + 1; + buf[0] = (uint8) ((ptr - 1) - buf); + writePipe(GPipeWrite, buf, buf[0] + 1); } /* writeStatThing */ -void STEAMSHIM_setStatI(const char *name, const int _val) -{ - const int32 val = (int32) _val; - dbgpipe("Child sending SHIMCMD_SETSTATI('%s', val %d).\n", name, val); - writeStatThing(SHIMCMD_SETSTATI, name, &val, sizeof (val)); +void STEAMSHIM_setStatI(const char *name, const int _val) { + const int32 val = (int32) _val; + dbgpipe("Child sending SHIMCMD_SETSTATI('%s', val %d).\n", name, val); + writeStatThing(SHIMCMD_SETSTATI, name, &val, sizeof(val)); } /* STEAMSHIM_setStatI */ -void STEAMSHIM_getStatI(const char *name) -{ - dbgpipe("Child sending SHIMCMD_GETSTATI('%s').\n", name); - writeStatThing(SHIMCMD_GETSTATI, name, NULL, 0); +void STEAMSHIM_getStatI(const char *name) { + dbgpipe("Child sending SHIMCMD_GETSTATI('%s').\n", name); + writeStatThing(SHIMCMD_GETSTATI, name, NULL, 0); } /* STEAMSHIM_getStatI */ -void STEAMSHIM_setStatF(const char *name, const float val) -{ - dbgpipe("Child sending SHIMCMD_SETSTATF('%s', val %f).\n", name, val); - writeStatThing(SHIMCMD_SETSTATF, name, &val, sizeof (val)); +void STEAMSHIM_setStatF(const char *name, const float val) { + dbgpipe("Child sending SHIMCMD_SETSTATF('%s', val %f).\n", name, val); + writeStatThing(SHIMCMD_SETSTATF, name, &val, sizeof(val)); } /* STEAMSHIM_setStatF */ -void STEAMSHIM_getStatF(const char *name) -{ - dbgpipe("Child sending SHIMCMD_GETSTATF('%s').\n", name); - writeStatThing(SHIMCMD_GETSTATF, name, NULL, 0); +void STEAMSHIM_getStatF(const char *name) { + dbgpipe("Child sending SHIMCMD_GETSTATF('%s').\n", name); + writeStatThing(SHIMCMD_GETSTATF, name, NULL, 0); } /* STEAMSHIM_getStatF */ -void STEAMSHIM_getPersonaName() -{ - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_GETPERSONANAME().\n"); - write1ByteCmd(SHIMCMD_GETPERSONANAME); +void STEAMSHIM_getPersonaName() { + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_GETPERSONANAME().\n"); + write1ByteCmd(SHIMCMD_GETPERSONANAME); } /* STEAMSHIM_getPersonaName */ -void STEAMSHIM_getCurrentGameLanguage() -{ - if (isDead()) return; - dbgpipe("Child sending SHIMCMD_GETCURRENTGAMELANGUAGE().\n"); - write1ByteCmd(SHIMCMD_GETCURRENTGAMELANGUAGE); +void STEAMSHIM_getCurrentGameLanguage() { + if (isDead()) return; + dbgpipe("Child sending SHIMCMD_GETCURRENTGAMELANGUAGE().\n"); + write1ByteCmd(SHIMCMD_GETCURRENTGAMELANGUAGE); } /* STEAMSHIM_getCurrentGameLanguage */ /* end of steamshim_child.c ... */ diff --git a/source/glest_game/steamshim/steamshim_child.h b/source/glest_game/steamshim/steamshim_child.h index 0e3d3a9d3..233f81ab3 100644 --- a/source/glest_game/steamshim/steamshim_child.h +++ b/source/glest_game/steamshim/steamshim_child.h @@ -5,48 +5,46 @@ extern "C" { #endif -typedef enum STEAMSHIM_EventType -{ - SHIMEVENT_BYE, - SHIMEVENT_STATSRECEIVED, - SHIMEVENT_STATSSTORED, - SHIMEVENT_SETACHIEVEMENT, - SHIMEVENT_GETACHIEVEMENT, - SHIMEVENT_RESETSTATS, - SHIMEVENT_SETSTATI, - SHIMEVENT_GETSTATI, - SHIMEVENT_SETSTATF, - SHIMEVENT_GETSTATF, - SHIMEVENT_GETPERSONANAME, - SHIMEVENT_GETCURRENTGAMELANGUAGE, -} STEAMSHIM_EventType; + typedef enum STEAMSHIM_EventType { + SHIMEVENT_BYE, + SHIMEVENT_STATSRECEIVED, + SHIMEVENT_STATSSTORED, + SHIMEVENT_SETACHIEVEMENT, + SHIMEVENT_GETACHIEVEMENT, + SHIMEVENT_RESETSTATS, + SHIMEVENT_SETSTATI, + SHIMEVENT_GETSTATI, + SHIMEVENT_SETSTATF, + SHIMEVENT_GETSTATF, + SHIMEVENT_GETPERSONANAME, + SHIMEVENT_GETCURRENTGAMELANGUAGE, + } STEAMSHIM_EventType; -/* not all of these fields make sense in a given event. */ -typedef struct STEAMSHIM_Event -{ - STEAMSHIM_EventType type; - int okay; - int ivalue; - float fvalue; - unsigned long long epochsecs; - char name[256]; -} STEAMSHIM_Event; + /* not all of these fields make sense in a given event. */ + typedef struct STEAMSHIM_Event { + STEAMSHIM_EventType type; + int okay; + int ivalue; + float fvalue; + unsigned long long epochsecs; + char name[256]; + } STEAMSHIM_Event; -int STEAMSHIM_init(void); /* non-zero on success, zero on failure. */ -void STEAMSHIM_deinit(void); -int STEAMSHIM_alive(void); -const STEAMSHIM_Event *STEAMSHIM_pump(void); -void STEAMSHIM_requestStats(void); -void STEAMSHIM_storeStats(void); -void STEAMSHIM_setAchievement(const char *name, const int enable); -void STEAMSHIM_getAchievement(const char *name); -void STEAMSHIM_resetStats(const int bAlsoAchievements); -void STEAMSHIM_setStatI(const char *name, const int _val); -void STEAMSHIM_getStatI(const char *name); -void STEAMSHIM_setStatF(const char *name, const float val); -void STEAMSHIM_getStatF(const char *name); -void STEAMSHIM_getPersonaName(); -void STEAMSHIM_getCurrentGameLanguage(); + int STEAMSHIM_init(void); /* non-zero on success, zero on failure. */ + void STEAMSHIM_deinit(void); + int STEAMSHIM_alive(void); + const STEAMSHIM_Event *STEAMSHIM_pump(void); + void STEAMSHIM_requestStats(void); + void STEAMSHIM_storeStats(void); + void STEAMSHIM_setAchievement(const char *name, const int enable); + void STEAMSHIM_getAchievement(const char *name); + void STEAMSHIM_resetStats(const int bAlsoAchievements); + void STEAMSHIM_setStatI(const char *name, const int _val); + void STEAMSHIM_getStatI(const char *name); + void STEAMSHIM_setStatF(const char *name, const float val); + void STEAMSHIM_getStatF(const char *name); + void STEAMSHIM_getPersonaName(); + void STEAMSHIM_getCurrentGameLanguage(); #ifdef __cplusplus } diff --git a/source/glest_game/type_instances/command.cpp b/source/glest_game/type_instances/command.cpp index f0f92d175..6fb123b8e 100644 --- a/source/glest_game/type_instances/command.cpp +++ b/source/glest_game/type_instances/command.cpp @@ -21,281 +21,254 @@ using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Command -// ===================================================== - Command::Command ():unitRef () - { - this->commandType = NULL; - unitType = NULL; - stateType = cst_None; - stateValue = -1; - unitCommandGroupId = -1; - } + // ===================================================== + // class Command + // ===================================================== + Command::Command() :unitRef() { + this->commandType = NULL; + unitType = NULL; + stateType = cst_None; + stateValue = -1; + unitCommandGroupId = -1; + } - Command::Command (const CommandType * ct, const Vec2i & pos):unitRef () - { - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ct = [%p]\n",__FILE__,__FUNCTION__,__LINE__,ct); + Command::Command(const CommandType * ct, const Vec2i & pos) :unitRef() { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] ct = [%p]\n",__FILE__,__FUNCTION__,__LINE__,ct); - this->commandType = ct; - this->pos = pos; - this->originalPos = this->pos; - unitType = NULL; - stateType = cst_None; - stateValue = -1; - unitCommandGroupId = -1; - } + this->commandType = ct; + this->pos = pos; + this->originalPos = this->pos; + unitType = NULL; + stateType = cst_None; + stateValue = -1; + unitCommandGroupId = -1; + } - Command::Command (const CommandType * ct, Unit * unit) - { - this->commandType = ct; - this->pos = Vec2i (0); - this->originalPos = this->pos; - this->unitRef = unit; - unitType = NULL; - if (unit != NULL) - { - //unit->resetHighlight(); is in gui now - pos = unit->getCellPos (); - } - stateType = cst_None; - stateValue = -1; - unitCommandGroupId = -1; - } + Command::Command(const CommandType * ct, Unit * unit) { + this->commandType = ct; + this->pos = Vec2i(0); + this->originalPos = this->pos; + this->unitRef = unit; + unitType = NULL; + if (unit != NULL) { + //unit->resetHighlight(); is in gui now + pos = unit->getCellPos(); + } + stateType = cst_None; + stateValue = -1; + unitCommandGroupId = -1; + } - Command::Command (const CommandType * ct, const Vec2i & pos, - const UnitType * unitType, - CardinalDir facing):unitRef () - { - this->commandType = ct; - this->pos = pos; - this->originalPos = this->pos; - this->unitType = unitType; - this->facing = facing; - stateType = cst_None; - stateValue = -1; - unitCommandGroupId = -1; + Command::Command(const CommandType * ct, const Vec2i & pos, + const UnitType * unitType, + CardinalDir facing) :unitRef() { + this->commandType = ct; + this->pos = pos; + this->originalPos = this->pos; + this->unitType = unitType; + this->facing = facing; + stateType = cst_None; + stateValue = -1; + unitCommandGroupId = -1; - //if(this->unitType != NULL) { - // SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitType = [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->unitType->toString().c_str()); - //} - } + //if(this->unitType != NULL) { + // SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unitType = [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->unitType->toString().c_str()); + //} + } - int Command::getPriority () - { - if (this->commandType->commandTypeClass == ccAttack - && getUnit () == NULL) - { - return 5; // attacks to the ground have low priority - } - return this->commandType->getTypePriority (); - } -// =============== set =============== + int Command::getPriority() { + if (this->commandType->commandTypeClass == ccAttack + && getUnit() == NULL) { + return 5; // attacks to the ground have low priority + } + return this->commandType->getTypePriority(); + } + // =============== set =============== - void Command::setCommandType (const CommandType * commandType) - { - this->commandType = commandType; - } + void Command::setCommandType(const CommandType * commandType) { + this->commandType = commandType; + } - void Command::setPos (const Vec2i & pos) - { - this->pos = pos; - } + void Command::setPos(const Vec2i & pos) { + this->pos = pos; + } -//void Command::setOriginalPos(const Vec2i &pos) { -// this->originalPos= pos; -//} + //void Command::setOriginalPos(const Vec2i &pos) { + // this->originalPos= pos; + //} - void Command::setPosToOriginalPos () - { - this->pos = this->originalPos; - } + void Command::setPosToOriginalPos() { + this->pos = this->originalPos; + } - void Command::setUnit (Unit * unit) - { - this->unitRef = unit; - } + void Command::setUnit(Unit * unit) { + this->unitRef = unit; + } - std::string Command::toString (bool translatedValue) const - { - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + std::string Command::toString(bool translatedValue) const { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - std::string result = ""; - if (commandType != NULL) - { - result = - "commandType id = " + intToStr (commandType->getId ()) + - ", desc = " + commandType->toString (translatedValue); - } - else - { - result = "commandType = NULL"; - } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + std::string result = ""; + if (commandType != NULL) { + result = + "commandType id = " + intToStr(commandType->getId()) + + ", desc = " + commandType->toString(translatedValue); + } else { + result = "commandType = NULL"; + } + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - result += - ", pos = " + pos.getString () + ", originalPos = " + - originalPos.getString () + ", facing = " + intToStr (facing.asInt ()); + result += + ", pos = " + pos.getString() + ", originalPos = " + + originalPos.getString() + ", facing = " + intToStr(facing.asInt()); - //if(unitRef.getUnit() != NULL) { - if (unitRef.getUnitId () >= 0) - { - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + //if(unitRef.getUnit() != NULL) { + if (unitRef.getUnitId() >= 0) { + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - result += - ", unitRef.getUnit() id = " + intToStr (unitRef.getUnitId ()); + result += + ", unitRef.getUnit() id = " + intToStr(unitRef.getUnitId()); - // The code below causes a STACK OVERFLOW! - //if(unitRef.getUnit() != NULL) { - // result += ", unitRef.getUnit() = " + unitRef.getUnit()->toString(); - //} - } + // The code below causes a STACK OVERFLOW! + //if(unitRef.getUnit() != NULL) { + // result += ", unitRef.getUnit() = " + unitRef.getUnit()->toString(); + //} + } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - if (unitType != NULL) - { - result += ", unitTypeId = " + intToStr (unitType->getId ()); - result += - ", unitTypeDesc = " + unitType->getReqDesc (translatedValue); - } + if (unitType != NULL) { + result += ", unitTypeId = " + intToStr(unitType->getId()); + result += + ", unitTypeDesc = " + unitType->getReqDesc(translatedValue); + } - //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); + //SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); - result += - ", stateType = " + intToStr (stateType) + ", stateValue = " + - intToStr (stateValue); + result += + ", stateType = " + intToStr(stateType) + ", stateValue = " + + intToStr(stateValue); - result += ", unitCommandGroupId = " + intToStr (unitCommandGroupId); + result += ", unitCommandGroupId = " + intToStr(unitCommandGroupId); - return result; - } + return result; + } - Checksum Command::getCRC () - { - Checksum crcForCmd; + Checksum Command::getCRC() { + Checksum crcForCmd; - crcForCmd.addInt (commandType->getId ()); - crcForCmd.addInt (originalPos.x); - crcForCmd.addInt (originalPos.y); - crcForCmd.addInt (pos.x); - crcForCmd.addInt (pos.y); - crcForCmd.addInt (unitRef.getUnitId ()); - crcForCmd.addInt (facing); - if (unitType != NULL) - { - crcForCmd.addInt (unitType->getId ()); - } - crcForCmd.addInt (stateType); - crcForCmd.addInt (stateValue); - crcForCmd.addInt (unitCommandGroupId); + crcForCmd.addInt(commandType->getId()); + crcForCmd.addInt(originalPos.x); + crcForCmd.addInt(originalPos.y); + crcForCmd.addInt(pos.x); + crcForCmd.addInt(pos.y); + crcForCmd.addInt(unitRef.getUnitId()); + crcForCmd.addInt(facing); + if (unitType != NULL) { + crcForCmd.addInt(unitType->getId()); + } + crcForCmd.addInt(stateType); + crcForCmd.addInt(stateValue); + crcForCmd.addInt(unitCommandGroupId); - return crcForCmd; - } + return crcForCmd; + } - void Command::saveGame (XmlNode * rootNode, Faction * faction) - { - std::map < string, string > mapTagReplacements; - XmlNode *commandNode = rootNode->addChild ("Command"); + void Command::saveGame(XmlNode * rootNode, Faction * faction) { + std::map < string, string > mapTagReplacements; + XmlNode *commandNode = rootNode->addChild("Command"); -// const CommandType *commandType; - if (commandType != NULL) - { - commandNode->addAttribute ("commandType", - intToStr (commandType->getId ()), - mapTagReplacements); - } -// Vec2i originalPos; - commandNode->addAttribute ("originalPos", originalPos.getString (), - mapTagReplacements); -// Vec2i pos; - commandNode->addAttribute ("pos", pos.getString (), mapTagReplacements); -// UnitReference unitRef; //target unit, used to move and attack optionally - unitRef.saveGame (commandNode); -// CardinalDir facing; // facing, for build command - commandNode->addAttribute ("facing", intToStr (facing), - mapTagReplacements); -// const UnitType *unitType; //used for build - if (unitType != NULL) - { - commandNode->addAttribute ("unitTypeId", - intToStr (unitType->getId ()), - mapTagReplacements); - commandNode->addAttribute ("unitTypeFactionIndex", - intToStr (faction->getIndex ()), - mapTagReplacements); - } -// CommandStateType stateType; - commandNode->addAttribute ("stateType", intToStr (stateType), - mapTagReplacements); -// int stateValue; - commandNode->addAttribute ("stateValue", intToStr (stateValue), - mapTagReplacements); -// int unitCommandGroupId; - commandNode->addAttribute ("unitCommandGroupId", - intToStr (unitCommandGroupId), - mapTagReplacements); - } + // const CommandType *commandType; + if (commandType != NULL) { + commandNode->addAttribute("commandType", + intToStr(commandType->getId()), + mapTagReplacements); + } + // Vec2i originalPos; + commandNode->addAttribute("originalPos", originalPos.getString(), + mapTagReplacements); + // Vec2i pos; + commandNode->addAttribute("pos", pos.getString(), mapTagReplacements); + // UnitReference unitRef; //target unit, used to move and attack optionally + unitRef.saveGame(commandNode); + // CardinalDir facing; // facing, for build command + commandNode->addAttribute("facing", intToStr(facing), + mapTagReplacements); + // const UnitType *unitType; //used for build + if (unitType != NULL) { + commandNode->addAttribute("unitTypeId", + intToStr(unitType->getId()), + mapTagReplacements); + commandNode->addAttribute("unitTypeFactionIndex", + intToStr(faction->getIndex()), + mapTagReplacements); + } + // CommandStateType stateType; + commandNode->addAttribute("stateType", intToStr(stateType), + mapTagReplacements); + // int stateValue; + commandNode->addAttribute("stateValue", intToStr(stateValue), + mapTagReplacements); + // int unitCommandGroupId; + commandNode->addAttribute("unitCommandGroupId", + intToStr(unitCommandGroupId), + mapTagReplacements); + } - Command *Command::loadGame (const XmlNode * rootNode, const UnitType * ut, - World * world) - { - Command *result = new Command (); - const XmlNode *commandNode = rootNode; + Command *Command::loadGame(const XmlNode * rootNode, const UnitType * ut, + World * world) { + Command *result = new Command(); + const XmlNode *commandNode = rootNode; - //description = commandNode->getAttribute("description")->getValue(); + //description = commandNode->getAttribute("description")->getValue(); - // const CommandType *commandType; - if (commandNode->hasAttribute ("commandType") == true) - { - int cmdTypeId = - commandNode->getAttribute ("commandType")->getIntValue (); - result->commandType = ut->findCommandTypeById (cmdTypeId); - } - // Vec2i originalPos; - result->originalPos = - Vec2i::strToVec2 (commandNode->getAttribute ("originalPos")-> - getValue ()); - // Vec2i pos; - result->pos = - Vec2i::strToVec2 (commandNode->getAttribute ("pos")->getValue ()); - // UnitReference unitRef; //target unit, used to move and attack optionally - result->unitRef.loadGame (commandNode, world); - // CardinalDir facing; // facing, for build command - result->facing = - static_cast < CardinalDir > - (commandNode->getAttribute ("facing")->getIntValue ()); - // const UnitType *unitType; //used for build - if (commandNode->hasAttribute ("unitTypeId") == true) - { - //result->unitType = ut; - int unitTypeId = - commandNode->getAttribute ("unitTypeId")->getIntValue (); - int unitTypeFactionIndex = - commandNode->getAttribute ("unitTypeFactionIndex")->getIntValue (); - Faction *faction = world->getFaction (unitTypeFactionIndex); - result->unitType = - world->findUnitTypeById (faction->getType (), unitTypeId); - } - // CommandStateType stateType; - result->stateType = - static_cast < CommandStateType > - (commandNode->getAttribute ("stateType")->getIntValue ()); - // int stateValue; - result->stateValue = - commandNode->getAttribute ("stateValue")->getIntValue (); - // int unitCommandGroupId; - result->unitCommandGroupId = - commandNode->getAttribute ("unitCommandGroupId")->getIntValue (); + // const CommandType *commandType; + if (commandNode->hasAttribute("commandType") == true) { + int cmdTypeId = + commandNode->getAttribute("commandType")->getIntValue(); + result->commandType = ut->findCommandTypeById(cmdTypeId); + } + // Vec2i originalPos; + result->originalPos = + Vec2i::strToVec2(commandNode->getAttribute("originalPos")-> + getValue()); + // Vec2i pos; + result->pos = + Vec2i::strToVec2(commandNode->getAttribute("pos")->getValue()); + // UnitReference unitRef; //target unit, used to move and attack optionally + result->unitRef.loadGame(commandNode, world); + // CardinalDir facing; // facing, for build command + result->facing = + static_cast + (commandNode->getAttribute("facing")->getIntValue()); + // const UnitType *unitType; //used for build + if (commandNode->hasAttribute("unitTypeId") == true) { + //result->unitType = ut; + int unitTypeId = + commandNode->getAttribute("unitTypeId")->getIntValue(); + int unitTypeFactionIndex = + commandNode->getAttribute("unitTypeFactionIndex")->getIntValue(); + Faction *faction = world->getFaction(unitTypeFactionIndex); + result->unitType = + world->findUnitTypeById(faction->getType(), unitTypeId); + } + // CommandStateType stateType; + result->stateType = + static_cast + (commandNode->getAttribute("stateType")->getIntValue()); + // int stateValue; + result->stateValue = + commandNode->getAttribute("stateValue")->getIntValue(); + // int unitCommandGroupId; + result->unitCommandGroupId = + commandNode->getAttribute("unitCommandGroupId")->getIntValue(); - return result; - } + return result; + } - } + } } //end namespace diff --git a/source/glest_game/type_instances/command.h b/source/glest_game/type_instances/command.h index c67f45b6c..bafa491a0 100644 --- a/source/glest_game/type_instances/command.h +++ b/source/glest_game/type_instances/command.h @@ -23,127 +23,111 @@ # include "game_constants.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using Shared::Graphics::Vec2i; + using Shared::Graphics::Vec2i; - class CommandType; + class CommandType; - enum CommandStateType - { - cst_None, - cst_linkedUnit, - cst_EmergencyReturnResource - }; + enum CommandStateType { + cst_None, + cst_linkedUnit, + cst_EmergencyReturnResource + }; -// ===================================================== -// class Command -// -/// A unit command -// ===================================================== + // ===================================================== + // class Command + // + /// A unit command + // ===================================================== - class Command - { - private: - const CommandType *commandType; - Vec2i originalPos; - Vec2i pos; - UnitReference unitRef; //target unit, used to move and attack optionally - CardinalDir facing; // facing, for build command - const UnitType *unitType; //used for build + class Command { + private: + const CommandType *commandType; + Vec2i originalPos; + Vec2i pos; + UnitReference unitRef; //target unit, used to move and attack optionally + CardinalDir facing; // facing, for build command + const UnitType *unitType; //used for build - CommandStateType stateType; - int stateValue; + CommandStateType stateType; + int stateValue; - int unitCommandGroupId; + int unitCommandGroupId; - Command (); - public: - //constructor - Command (const CommandType * ct, const Vec2i & pos = Vec2i (0)); - Command (const CommandType * ct, Unit * unit); - Command (const CommandType * ct, const Vec2i & pos, - const UnitType * unitType, CardinalDir facing); + Command(); + public: + //constructor + Command(const CommandType * ct, const Vec2i & pos = Vec2i(0)); + Command(const CommandType * ct, Unit * unit); + Command(const CommandType * ct, const Vec2i & pos, + const UnitType * unitType, CardinalDir facing); - virtual ~ Command () - { - } - //get - inline const CommandType *getCommandType () const - { - return commandType; - } - inline Vec2i getPos () const - { - return pos; - } - inline Vec2i getOriginalPos () const - { - return originalPos; - } - inline Unit *getUnit () const - { - return unitRef.getUnit (); - } - inline const UnitType *getUnitType () const - { - return unitType; - } - inline CardinalDir getFacing () const - { - return facing; - } + virtual ~Command() { + } + //get + inline const CommandType *getCommandType() const { + return commandType; + } + inline Vec2i getPos() const { + return pos; + } + inline Vec2i getOriginalPos() const { + return originalPos; + } + inline Unit *getUnit() const { + return unitRef.getUnit(); + } + inline const UnitType *getUnitType() const { + return unitType; + } + inline CardinalDir getFacing() const { + return facing; + } - //Priority: commands of higher priority will cancel commands of lower priority - virtual int getPriority (); + //Priority: commands of higher priority will cancel commands of lower priority + virtual int getPriority(); - //set - void setCommandType (const CommandType * commandType); - void setPos (const Vec2i & pos); - //void setOriginalPos(const Vec2i &pos); - void setPosToOriginalPos (); + //set + void setCommandType(const CommandType * commandType); + void setPos(const Vec2i & pos); + //void setOriginalPos(const Vec2i &pos); + void setPosToOriginalPos(); - void setUnit (Unit * unit); + void setUnit(Unit * unit); - inline void setStateType (CommandStateType value) - { - stateType = value; - } - inline CommandStateType getStateType () const - { - return stateType; - } + inline void setStateType(CommandStateType value) { + stateType = value; + } + inline CommandStateType getStateType() const { + return stateType; + } - inline void setStateValue (int value) - { - stateValue = value; - } - inline int getStateValue () const - { - return stateValue; - } + inline void setStateValue(int value) { + stateValue = value; + } + inline int getStateValue() const { + return stateValue; + } - inline void setUnitCommandGroupId (int value) - { - unitCommandGroupId = value; - } - inline int getUnitCommandGroupId () const - { - return unitCommandGroupId; - } + inline void setUnitCommandGroupId(int value) { + unitCommandGroupId = value; + } + inline int getUnitCommandGroupId() const { + return unitCommandGroupId; + } - std::string toString (bool translatedValue) const; + std::string toString(bool translatedValue) const; - void saveGame (XmlNode * rootNode, Faction * faction); - static Command *loadGame (const XmlNode * rootNode, const UnitType * ut, - World * world); + void saveGame(XmlNode * rootNode, Faction * faction); + static Command *loadGame(const XmlNode * rootNode, const UnitType * ut, + World * world); - Checksum getCRC (); - }; + Checksum getCRC(); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 94bf01c23..e12c60045 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -28,3492 +28,3004 @@ using namespace Shared::Util; using Shared::Util::RandomGen; -namespace Glest -{ - namespace Game - { - - bool CommandGroupUnitSorterId::operator () (const int l, const int r) - { - const Unit *lUnit = faction->findUnit (l); - const Unit *rUnit = faction->findUnit (r); - - if (!lUnit) - { - printf ("Error lUnit == NULL for id = %d factionIndex = %d\n", l, - faction->getIndex ()); - - for (unsigned int i = 0; i < (unsigned int) faction->getUnitCount (); - ++i) - { - printf ("%u / %d id = %d [%s]\n", i, faction->getUnitCount (), - faction->getUnit (i)->getId (), - faction->getUnit (i)->getType ()->getName (false).c_str ()); - } - } - if (!rUnit) - { - printf ("Error rUnit == NULL for id = %d factionIndex = %d\n", r, - faction->getIndex ()); - - for (unsigned int i = 0; i < (unsigned int) faction->getUnitCount (); - ++i) - { - printf ("%u / %d id = %d [%s]\n", i, faction->getUnitCount (), - faction->getUnit (i)->getId (), - faction->getUnit (i)->getType ()->getName (false).c_str ()); - } - } - - CommandGroupUnitSorter sorter; - return sorter.compare (lUnit, rUnit); - } - - bool CommandGroupUnitSorter::operator () (const Unit * l, const Unit * r) - { - return compare (l, r); - } - - bool CommandGroupUnitSorter::compare (const Unit * l, const Unit * r) - { - //printf("l [%p] r [%p] <>",l,r); - - if (!l) - { - printf ("Error l == NULL\n"); - } - if (!r) - { - printf ("Error r == NULL\n"); - } - - assert (l && r); - - if (l == NULL || r == NULL) - printf ("Unit l [%s - %d] r [%s - %d]\n", - (l != - NULL ? l->getType ()->getName (false).c_str () : "null"), - (l != NULL ? l->getId () : -1), - (r != - NULL ? r->getType ()->getName (false).c_str () : "null"), - (r != NULL ? r->getId () : -1)); - - - bool result = false; - // If comparer is null or dead - if (r == NULL || r->isAlive () == false) - { - // if source is null or dead also - if ((l == NULL || l->isAlive () == false)) - { - return false; - } - return true; - } - else if ((l == NULL || l->isAlive () == false)) - { - return false; - } - -// const Command *command= l->getCurrrentCommandThreadSafe(); -// const Command *commandPeer = r->getCurrrentCommandThreadSafe(); - const Command *command = l->getCurrCommand (); - const Command *commandPeer = r->getCurrCommand (); - - //Command *command= this->unit->getCurrCommand(); - - // Are we moving or attacking - if (command != NULL && command->getCommandType () != NULL && - (command->getCommandType ()->getClass () == ccMove || - command->getCommandType ()->getClass () == ccAttack) && - command->getUnitCommandGroupId () > 0) - { - int curCommandGroupId = command->getUnitCommandGroupId (); - - //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); - //Command *commandPeer = j.unit->getCurrCommand(); - - // is comparer a valid command - if (commandPeer == NULL || commandPeer->getCommandType () == NULL) - { - result = true; - } - // is comparer command the same type? - else if (commandPeer->getCommandType ()->getClass () != - command->getCommandType ()->getClass ()) - { - result = true; - } - // is comparer command groupid invalid? - else if (commandPeer->getUnitCommandGroupId () < 0) - { - result = true; - } - // If comparer command group id is less than current group id - else if (curCommandGroupId != commandPeer->getUnitCommandGroupId ()) - { - result = curCommandGroupId < commandPeer->getUnitCommandGroupId (); - } - else - { - float unitDist = l->getCenteredPos ().dist (command->getPos ()); - float unitDistPeer = - r->getCenteredPos ().dist (commandPeer->getPos ()); - - // Closest unit in commandgroup - result = (unitDist < unitDistPeer); - } - } - else if (command == NULL && commandPeer != NULL) - { - result = false; - } -// else if(command == NULL && j.unit->getCurrrentCommandThreadSafe() == NULL) { -// return this->unit->getId() < j.unit->getId(); -// } - else - { - //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); - //if( commandPeer != NULL && commandPeer->getCommandType() != NULL && - // (commandPeer->getCommandType()->getClass() != ccMove && - // commandPeer->getCommandType()->getClass() != ccAttack)) { - result = (l->getId () < r->getId ()); - //} - //else { - // result = (l->getId() < r->getId()); - //} - } - - //printf("Sorting, unit [%d - %s] cmd [%s] | unit2 [%d - %s] cmd [%s] result = %d\n",this->unit->getId(),this->unit->getFullName().c_str(),(this->unit->getCurrCommand() == NULL ? "NULL" : this->unit->getCurrCommand()->toString().c_str()),j.unit->getId(),j.unit->getFullName().c_str(),(j.unit->getCurrCommand() == NULL ? "NULL" : j.unit->getCurrCommand()->toString().c_str()),result); - - return result; - } - - void Faction::sortUnitsByCommandGroups () - { - MutexSafeWrapper safeMutex (unitsMutex, - string (__FILE__) + "_" + - intToStr (__LINE__)); - //printf("====== sortUnitsByCommandGroups for faction # %d [%s] unitCount = %d\n",this->getIndex(),this->getType()->getName().c_str(),units.size()); - //for(unsigned int i = 0; i < units.size(); ++i) { - // printf("%d / %d [%p] <>",i,units.size(),&units[i]); -// // printf("i = %d [%p]\n",i,&units[i]); -// if(Unit::isUnitDeleted(units[i]) == true) { -// printf("i = %d [%p]\n",i,&units[i]); -// throw megaglest_runtime_error("unit already deleted!"); -// } - //} - //printf("\nSorting\n"); - - //std::sort(units.begin(),units.end(),CommandGroupUnitSorter()); - - //printf("====== Done sorting for faction # %d [%s] unitCount = %d\n",this->getIndex(),this->getType()->getName().c_str(),units.size()); - - //unsigned int originalUnitSize = (unsigned int)units.size(); - - std::vector < int >unitIds; - for (unsigned int i = 0; i < units.size (); ++i) - { - int unitId = units[i]->getId (); - if (this->findUnit (unitId) == NULL) - { - printf - ("#1 Error unitId not found for id = %d [%s] factionIndex = %d\n", - unitId, units[i]->getType ()->getName (false).c_str (), - this->getIndex ()); - - for (unsigned int j = 0; j < units.size (); ++j) - { - printf ("%u / %d id = %d [%s]\n", j, (int) units.size (), - units[j]->getId (), - units[j]->getType ()->getName (false).c_str ()); - } - } - unitIds.push_back (unitId); - } - CommandGroupUnitSorterId sorter; - sorter.faction = this; - std::stable_sort (unitIds.begin (), unitIds.end (), sorter); - - units.clear (); - for (unsigned int i = 0; i < unitIds.size (); ++i) - { - - int unitId = unitIds[i]; - if (this->findUnit (unitId) == NULL) - { - printf ("#2 Error unitId not found for id = %d factionIndex = %d\n", - unitId, this->getIndex ()); - - for (unsigned int j = 0; j < units.size (); ++j) - { - printf ("%u / %d id = %d [%s]\n", j, (int) units.size (), - units[j]->getId (), - units[j]->getType ()->getName (false).c_str ()); - } - } - - units.push_back (this->findUnit (unitId)); - } - - //assert(originalUnitSize == units.size()); - } - -// ===================================================== -// class FactionThread -// ===================================================== - - FactionThread::FactionThread (Faction * faction):BaseThread () - { - this->triggerIdMutex = new Mutex (CODE_AT_LINE); - this->faction = faction; - this->masterController = NULL; - uniqueID = "FactionThread"; - } - - FactionThread::~FactionThread () - { - this->faction = NULL; - this->masterController = NULL; - delete this->triggerIdMutex; - this->triggerIdMutex = NULL; - } - - void FactionThread::setQuitStatus (bool value) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d value = %d\n", - __FILE__, __FUNCTION__, __LINE__, value); - - BaseThread::setQuitStatus (value); - if (value == true) - { - signalPathfinder (-1); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void FactionThread::signalPathfinder (int frameIndex) - { - if (frameIndex >= 0) - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (triggerIdMutex, mutexOwnerId); - this->frameIndex.first = frameIndex; - this->frameIndex.second = false; - - safeMutex.ReleaseLock (); - } - semTaskSignalled.signal (); - } - - void FactionThread::setTaskCompleted (int frameIndex) - { - if (frameIndex >= 0) - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (triggerIdMutex, mutexOwnerId); - if (this->frameIndex.first == frameIndex) - { - this->frameIndex.second = true; - } - safeMutex.ReleaseLock (); - } - } - - bool FactionThread::canShutdown (bool deleteSelfIfShutdownDelayed) - { - bool ret = (getExecutingTask () == false); - if (ret == false && deleteSelfIfShutdownDelayed == true) - { - setDeleteSelfOnExecutionDone (deleteSelfIfShutdownDelayed); - deleteSelfIfRequired (); - signalQuit (); - } - - return ret; - } - - bool FactionThread::isSignalPathfinderCompleted (int frameIndex) - { - if (getRunningStatus () == false) - { - return true; - } - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (triggerIdMutex, mutexOwnerId); - //bool result = (event != NULL ? event->eventCompleted : true); - bool result = (this->frameIndex.first == frameIndex - && this->frameIndex.second == true); - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] worker thread this = %p, this->frameIndex.first = %d, this->frameIndex.second = %d\n",__FILE__,__FUNCTION__,__LINE__,this,this->frameIndex.first,this->frameIndex.second); - - safeMutex.ReleaseLock (); - return result; - } - - void FactionThread::execute () - { - string codeLocation = "1"; - RunningStatusSafeWrapper runningStatus (this); - try - { - //setRunningStatus(true); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n", - __FILE__, __FUNCTION__, __LINE__, this); - - bool minorDebugPerformance = false; - Chrono chrono; - - codeLocation = "2"; - //unsigned int idx = 0; - for (; this->faction != NULL;) - { - if (getQuitStatus () == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - break; - } - - semTaskSignalled.waitTillSignalled (); - - codeLocation = "3"; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - static string masterSlaveOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MasterSlaveThreadControllerSafeWrapper - safeMasterController (masterController, 20000, - masterSlaveOwnerId); - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if (getQuitStatus () == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - break; - } - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (triggerIdMutex, mutexOwnerId); - bool executeTask = (this->frameIndex.first >= 0); - int currentTriggeredFrameIndex = this->frameIndex.first; - - //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] frameIndex = %d this = %p executeTask = %d\n",__FILE__,__FUNCTION__,__LINE__,frameIndex.first, this, executeTask); - - safeMutex.ReleaseLock (); - - codeLocation = "5"; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - if (executeTask == true) - { - codeLocation = "6"; - ExecutingTaskSafeWrapper safeExecutingTaskMutex (this); - - if (this->faction == NULL) - { - throw megaglest_runtime_error ("this->faction == NULL"); - } - World *world = this->faction->getWorld (); - if (world == NULL) - { - throw megaglest_runtime_error ("world == NULL"); - } - - codeLocation = "7"; - //Config &config= Config::getInstance(); - //bool sortedUnitsAllowed = config.getBool("AllowGroupedUnitCommands","true"); - //bool sortedUnitsAllowed = false; - //if(sortedUnitsAllowed == true) { - - /// TODO: Why does this cause and OOS? - //this->faction->sortUnitsByCommandGroups (); - - //} - - codeLocation = "8"; - static string mutexOwnerId2 = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (faction->getUnitMutex (), - mutexOwnerId2); - - //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); - if (minorDebugPerformance) - chrono.start (); - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - codeLocation = "9"; - int unitCount = this->faction->getUnitCount (); - for (int j = 0; j < unitCount; ++j) - { - codeLocation = "10"; - Unit *unit = this->faction->getUnit (j); - if (unit == NULL) - { - throw megaglest_runtime_error ("unit == NULL"); - } - - codeLocation = "11"; - int64 elapsed1 = 0; - if (minorDebugPerformance) - elapsed1 = chrono.getMillis (); - - bool update = unit->needToUpdate (); - - codeLocation = "12"; - if (minorDebugPerformance - && (chrono.getMillis () - elapsed1) >= 1) - printf - ("Faction [%d - %s] #1-unit threaded updates on frame: %d for [%d] unit # %d, unitCount = %d, took [%lld] msecs\n", - faction->getStartLocationIndex (), - faction->getType ()->getName (false).c_str (), - currentTriggeredFrameIndex, - faction->getUnitPathfindingListCount (), j, unitCount, - (long long int) chrono.getMillis () - elapsed1); - - //update = true; - if (update == true) - { - codeLocation = "13"; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - int64 updateProgressValue = unit->getUpdateProgress (); - int64 speed = - unit->getCurrSkill ()->getTotalSpeed (unit-> - getTotalUpgrade ()); - int64 df = unit->getDiagonalFactor (); - int64 hf = unit->getHeightFactor (); - bool changedActiveCommand = unit->isChangedActiveCommand (); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "unit->needToUpdate() returned: %d updateProgressValue: %lld speed: %lld changedActiveCommand: %d df: %lld hf: %lld", - update, (long long int) updateProgressValue, - (long long int) speed, changedActiveCommand, - (long long int) df, (long long int) hf); - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - - int64 elapsed2 = 0; - if (minorDebugPerformance) - elapsed2 = chrono.getMillis (); - - if (world->getUnitUpdater () == NULL) - { - throw - megaglest_runtime_error - ("world->getUnitUpdater() == NULL"); - } - - world->getUnitUpdater ()->updateUnitCommand (unit, - currentTriggeredFrameIndex); - - codeLocation = "15"; - if (minorDebugPerformance - && (chrono.getMillis () - elapsed2) >= 1) - printf - ("Faction [%d - %s] #2-unit threaded updates on frame: %d for [%d] unit # %d, unitCount = %d, took [%lld] msecs\n", - faction->getStartLocationIndex (), - faction->getType ()->getName (false).c_str (), - currentTriggeredFrameIndex, - faction->getUnitPathfindingListCount (), j, unitCount, - (long long int) chrono.getMillis () - elapsed2); - } - else - { - codeLocation = "16"; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - int64 updateProgressValue = unit->getUpdateProgress (); - int64 speed = - unit->getCurrSkill ()->getTotalSpeed (unit-> - getTotalUpgrade ()); - int64 df = unit->getDiagonalFactor (); - int64 hf = unit->getHeightFactor (); - bool changedActiveCommand = unit->isChangedActiveCommand (); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "unit->needToUpdate() returned: %d updateProgressValue: %lld speed: %lld changedActiveCommand: %d df: %lld hf: %lld", - update, (long long int) updateProgressValue, - (long long int) speed, changedActiveCommand, - (long long int) df, (long long int) hf); - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - } - } - - codeLocation = "17"; - if (minorDebugPerformance && chrono.getMillis () >= 1) - printf - ("Faction [%d - %s] threaded updates on frame: %d for [%d] units took [%lld] msecs\n", - faction->getStartLocationIndex (), - faction->getType ()->getName (false).c_str (), - currentTriggeredFrameIndex, - faction->getUnitPathfindingListCount (), - (long long int) chrono.getMillis ()); - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - safeMutex.ReleaseLock (); - - codeLocation = "18"; - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - setTaskCompleted (currentTriggeredFrameIndex); - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - } - - //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - - codeLocation = "19"; - if (getQuitStatus () == true) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - break; - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n", - __FILE__, __FUNCTION__, __LINE__, this); - } - catch (const exception & ex) - { - //setRunningStatus(false); - - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Loc [%s] Error [%s]\n", - __FILE__, __FUNCTION__, __LINE__, - codeLocation.c_str (), ex.what ()); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - throw megaglest_runtime_error (ex.what ()); - } - catch ( ...) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "In [%s::%s %d] UNKNOWN error Loc [%s]\n", - __FILE__, __FUNCTION__, __LINE__, codeLocation.c_str ()); - SystemFlags::OutputDebug (SystemFlags::debugError, szBuf); - throw megaglest_runtime_error (szBuf); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s] Line: %d\n", __FILE__, - __FUNCTION__, __LINE__); - } - - -// ===================================================== -// class Faction -// ===================================================== - - Faction::Faction () - { - init (); - } - - void Faction::init () - { - unitsMutex = new Mutex (CODE_AT_LINE); - texture = NULL; - //lastResourceTargettListPurge = 0; - cachingDisabled = false; - factionDisconnectHandled = false; - workerThread = NULL; - - world = NULL; - scriptManager = NULL; - factionType = NULL; - index = 0; - teamIndex = 0; - startLocationIndex = 0; - thisFaction = false; - currentSwitchTeamVoteFactionIndex = -1; - allowSharedTeamUnits = false; - - loadWorldNode = NULL; - techTree = NULL; - - control = ctClosed; - - overridePersonalityType = fpt_EndCount; - - upgradeManager = UpgradeManager (); - } - - Faction::~Faction () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - //Renderer &renderer= Renderer::getInstance(); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - //renderer.endTexture(rsGame,texture); - //texture->end(); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (workerThread != NULL) - { - workerThread->signalQuit (); - if (workerThread->shutdownAndWait () == true) - { - delete workerThread; - } - workerThread = NULL; - } - - MutexSafeWrapper safeMutex (unitsMutex, - string (__FILE__) + "_" + - intToStr (__LINE__)); - deleteValues (units.begin (), units.end ()); - units.clear (); - - safeMutex.ReleaseLock (); - - //delete texture; - texture = NULL; - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - delete unitsMutex; - unitsMutex = NULL; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void Faction::end () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - if (workerThread != NULL) - { - workerThread->signalQuit (); - if (workerThread->shutdownAndWait () == true) - { - delete workerThread; - } - workerThread = NULL; - } - - MutexSafeWrapper safeMutex (unitsMutex, - string (__FILE__) + "_" + - intToStr (__LINE__)); - deleteValues (units.begin (), units.end ()); - units.clear (); - - safeMutex.ReleaseLock (); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - void Faction::notifyUnitAliveStatusChange (const Unit * unit) - { - if (unit != NULL) - { - if (unit->isAlive () == true) - { - aliveUnitListCache[unit->getId ()] = unit; - - if (unit->getType ()->isMobile () == true) - { - mobileUnitListCache[unit->getId ()] = unit; - } - } - else - { - aliveUnitListCache.erase (unit->getId ()); - mobileUnitListCache.erase (unit->getId ()); - beingBuiltUnitListCache.erase (unit->getId ()); - } - } - } - - void Faction::notifyUnitTypeChange (const Unit * unit, - const UnitType * newType) - { - if (unit != NULL) - { - if (unit->getType ()->isMobile () == true) - { - mobileUnitListCache.erase (unit->getId ()); - } - - if (newType != NULL && newType->isMobile () == true) - { - mobileUnitListCache[unit->getId ()] = unit; - } - } - } - - void Faction::notifyUnitSkillTypeChange (const Unit * unit, - const SkillType * newType) - { - if (unit != NULL) - { - if (unit->isBeingBuilt () == true) - { - beingBuiltUnitListCache.erase (unit->getId ()); - } - if (newType != NULL && newType->getClass () == scBeBuilt) - { - beingBuiltUnitListCache[unit->getId ()] = unit; - } - } - } - - bool Faction::hasAliveUnits (bool filterMobileUnits, - bool filterBuiltUnits) const - { - bool result = false; - if (aliveUnitListCache.empty () == false) - { - if (filterMobileUnits == true) - { - result = (mobileUnitListCache.empty () == false); - } - else - { - result = true; - } - - if (result == true && filterBuiltUnits == true) - { - result = (beingBuiltUnitListCache.empty () == true); - } - } - return result; - } - - FactionPersonalityType Faction::getPersonalityType () const - { - if (overridePersonalityType != fpt_EndCount) - { - return overridePersonalityType; - } - return factionType->getPersonalityType (); - } - - int Faction:: - getAIBehaviorStaticOverideValue (AIBehaviorStaticValueCategory type) - const - { - return factionType->getAIBehaviorStaticOverideValue (type); - } - - void Faction::addUnitToMovingList (int unitId) - { - unitsMovingList[unitId] = getWorld ()->getFrameCount (); - } - void Faction::removeUnitFromMovingList (int unitId) - { - unitsMovingList.erase (unitId); - } - -//int Faction::getUnitMovingListCount() { -// return (int)unitsMovingList.size(); -//} - - void Faction::addUnitToPathfindingList (int unitId) - { - //printf("ADD (1) Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); - unitsPathfindingList[unitId] = getWorld ()->getFrameCount (); - //printf("ADD (2) Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); - } -//void Faction::removeUnitFromPathfindingList(int unitId) { -// unitsPathfindingList.erase(unitId); -//} - - int Faction::getUnitPathfindingListCount () - { - //printf("GET Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); - return (int) unitsPathfindingList.size (); - } - - void Faction::clearUnitsPathfinding () - { - //printf("CLEAR Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); - if (unitsPathfindingList.empty () == false) - { - unitsPathfindingList.clear (); - } - } - - bool Faction::canUnitsPathfind () - { - bool result = true; - if (control == ctCpuEasy || control == ctCpu || - control == ctCpuUltra || control == ctCpuZeta) - { - //printf("AI player for faction index: %d (%s) current pathfinding: %d\n",index,factionType->getName().c_str(),getUnitPathfindingListCount()); - - const int MAX_UNITS_PATHFINDING_PER_FRAME = 10; - result = - (getUnitPathfindingListCount () <= MAX_UNITS_PATHFINDING_PER_FRAME); - if (result == false) - { - //printf("WARNING limited AI player for faction index: %d (%s) current pathfinding: %d\n",index,factionType->getName().c_str(),getUnitPathfindingListCount()); - } - } - return result; - } - - void Faction::setLockedUnitForFaction (const UnitType * ut, bool lock) - { - if (lock) - { - lockedUnits.insert (ut); - } - else - { - std::set < const UnitType *>::iterator it; - it = lockedUnits.find (ut); - if (it != lockedUnits.end ()) - { - lockedUnits.erase (it); - } - } - - } - - void Faction::signalWorkerThread (int frameIndex) - { - if (workerThread != NULL) - { - workerThread->signalPathfinder (frameIndex); - } - } - - bool Faction::isWorkerThreadSignalCompleted (int frameIndex) - { - if (workerThread != NULL) - { - return workerThread->isSignalPathfinderCompleted (frameIndex); - } - return true; - } - - - void Faction::init (FactionType * factionType, ControlType control, - TechTree * techTree, Game * game, int factionIndex, - int teamIndex, int startLocationIndex, - bool thisFaction, bool giveResources, - const XmlNode * loadWorldNode) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - this->techTree = techTree; - this->loadWorldNode = loadWorldNode; - this->control = control; - this->factionType = factionType; - this->startLocationIndex = startLocationIndex; - this->index = factionIndex; - this->teamIndex = teamIndex; - this->thisFaction = thisFaction; - this->world = game->getWorld (); - this->scriptManager = game->getScriptManager (); - //cachingDisabled = (Config::getInstance().getBool("DisableCaching","false") == true); - cachingDisabled = false; - - resources.resize (techTree->getResourceTypeCount ()); - store.resize (techTree->getResourceTypeCount ()); - - if (loadWorldNode == NULL) - { - for (int index = 0; index < techTree->getResourceTypeCount (); - ++index) - { - const ResourceType *rt = techTree->getResourceType (index); - int resourceAmount = - giveResources ? factionType->getStartingResourceAmount (rt) : 0; - resources[index].init (rt, resourceAmount); - store[index].init (rt, 0); - - this->world->initTeamResource (rt, this->teamIndex, 0); - } - } - //initialize cache - for (int index = 0; index < techTree->getResourceTypeCount (); ++index) - { - const ResourceType *rt = techTree->getResourceType (index); - this->updateUnitTypeWithResourceCostCache (rt); - } - - texture = Renderer::getInstance ().newTexture2D (rsGame); - string data_path = - getGameReadWritePath (GameConstants::path_data_CacheLookupKey); - if (texture) - { - string playerTexture = - getGameCustomCoreDataPath (data_path, - "data/core/faction_textures/faction" + - intToStr (startLocationIndex) + ".tga"); - texture->load (playerTexture); - } - - if (loadWorldNode != NULL) - { - loadGame (loadWorldNode, this->index, game->getGameSettings (), - game->getWorld ()); - } - - if (game->getGameSettings ()->getPathFinderType () == pfBasic) - { - if (workerThread != NULL) - { - workerThread->signalQuit (); - if (workerThread->shutdownAndWait () == true) - { - delete workerThread; - } - workerThread = NULL; - } - static string mutexOwnerId = - string (extractFileFromDirectoryPath (__FILE__).c_str ()) + - string ("_") + intToStr (__LINE__); - this->workerThread = new FactionThread (this); - this->workerThread->setUniqueID (mutexOwnerId); - this->workerThread->start (); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - -// ================== get ================== - - bool Faction::hasUnitTypeWithResourceCostInCache (const ResourceType * rt) const - { - std::string resourceTypeName = rt->getName (false); - std::map < std::string, bool >::const_iterator iterFind = - resourceTypeCostCache.find (resourceTypeName); - if (iterFind != resourceTypeCostCache.end ()) - { - return iterFind->second; - } - return false; - } - void Faction::updateUnitTypeWithResourceCostCache (const ResourceType * - rt) - { - std::string resourceTypeName = rt->getName (false); - - if (resourceTypeCostCache.find (resourceTypeName) == - resourceTypeCostCache.end ()) - { - resourceTypeCostCache[resourceTypeName] = - hasUnitTypeWithResouceCost (rt); - } - } - - bool Faction::hasUnitTypeWithResouceCost (const ResourceType * rt) - { - for (int factionUnitTypeIndex = 0; - factionUnitTypeIndex < getType ()->getUnitTypeCount (); - ++factionUnitTypeIndex) - { - - const UnitType *ut = getType ()->getUnitType (factionUnitTypeIndex); - if (ut->getCost (rt) != NULL) - { - return true; - } - } - return false; - } - - const Resource *Faction::getResource (const ResourceType * rt, - bool localFactionOnly) const - { - - if (localFactionOnly == false && - world != NULL && world->getGame () != NULL) - { - - Game *game = world->getGame (); - if (game->isFlagType1BitEnabled (ft1_allow_shared_team_resources) == - true) - { - return world->getResourceForTeam (rt, this->getTeam ()); - } - } - - for (int index = 0; index < (int) resources.size (); ++index) - { - if (rt == resources[index].getType ()) - { - return &resources[index]; - } - } - - printf ("ERROR cannot find resource type [%s] in list:\n", - (rt != NULL ? rt->getName ().c_str () : "null")); - for (int i = 0; i < (int) resources.size (); ++i) - { - printf ("Index %d [%s]", i, - resources[i].getType ()->getName ().c_str ()); - } - - assert (false); - return NULL; - } - - int Faction::getStoreAmount (const ResourceType * rt, - bool localFactionOnly) const - { - - if (localFactionOnly == false && - world != NULL && world->getGame () != NULL) - { - - Game *game = world->getGame (); - if (game->isFlagType1BitEnabled (ft1_allow_shared_team_resources) == - true) - { - return world->getStoreAmountForTeam (rt, this->getTeam ()); - } - } - - for (int index = 0; index < (int) store.size (); ++index) - { - if (rt == store[index].getType ()) - { - return store[index].getAmount (); - } - } - printf ("ERROR cannot find store type [%s] in list:\n", - (rt != NULL ? rt->getName ().c_str () : "null")); - for (int i = 0; i < (int) store.size (); ++i) - { - printf ("Index %d [%s]", i, store[i].getType ()->getName ().c_str ()); - } - - assert (false); - return 0; - } - - bool Faction::getCpuControl (bool enableServerControlledAI, - bool isNetworkGame, NetworkRole role) const - { - bool result = false; - if (enableServerControlledAI == false || isNetworkGame == false) - { - result = (control == ctCpuEasy || control == ctCpu - || control == ctCpuUltra || control == ctCpuZeta); - } - else - { - if (isNetworkGame == true) - { - if (role == nrServer) - { - result = (control == ctCpuEasy || control == ctCpu - || control == ctCpuUltra || control == ctCpuZeta); - } - else - { - result = (control == ctNetworkCpuEasy || control == ctNetworkCpu - || control == ctNetworkCpuUltra - || control == ctNetworkCpuZeta); - } - } - } - - return result; - } - - bool Faction::getCpuControl () const - { - return control == ctCpuEasy || control == ctCpu || control == ctCpuUltra - || control == ctCpuZeta || control == ctNetworkCpuEasy - || control == ctNetworkCpu || control == ctNetworkCpuUltra - || control == ctNetworkCpuZeta; - } - -// ==================== upgrade manager ==================== - - void Faction::startUpgrade (const UpgradeType * ut) - { - upgradeManager.startUpgrade (ut, index); - } - - void Faction::cancelUpgrade (const UpgradeType * ut) - { - upgradeManager.cancelUpgrade (ut); - } - - void Faction::finishUpgrade (const UpgradeType * ut) - { - upgradeManager.finishUpgrade (ut); - if (world->getThisFaction () != NULL - && this->getIndex () == world->getThisFaction ()->getIndex ()) - { - Console *console = world->getGame ()->getConsole (); - console->addStdMessage ("UpgradeFinished", - ": " + formatString (ut->getName (true))); - } - for (int i = 0; i < getUnitCount (); ++i) - { - getUnit (i)->applyUpgrade (ut); - } - } - -// ==================== reqs ==================== - -//checks if all required units and upgrades are present and maxUnitCount is within limit - bool Faction::reqsOk (const RequirableType * rt) const - { - assert (rt != NULL); - //required units - for (int i = 0; i < rt->getUnitReqCount (); ++i) - { - bool found = false; - for (int j = 0; j < getUnitCount (); ++j) - { - Unit *unit = getUnit (j); - const UnitType *ut = unit->getType (); - if (rt->getUnitReq (i) == ut && unit->isOperative ()) - { - found = true; - break; - } - } - if (found == false) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - return false; - } - } - - //required upgrades - for (int i = 0; i < rt->getUpgradeReqCount (); ++i) - { - if (upgradeManager.isUpgraded (rt->getUpgradeReq (i)) == false) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - return false; - } - } - - if (dynamic_cast < const UnitType * >(rt) != NULL) - { - const UnitType *producedUnitType = - dynamic_cast < const UnitType * >(rt); - if (producedUnitType != NULL - && producedUnitType->getMaxUnitCount () > 0) - { - if (producedUnitType->getMaxUnitCount () <= - getCountForMaxUnitCount (producedUnitType)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - return false; - } - } - - if (producedUnitType != NULL && isUnitLocked (producedUnitType)) - { - return false; - } - } - - return true; - } - - int Faction::getCountForMaxUnitCount (const UnitType * unitType) const - { - int count = 0; - //calculate current unit count - for (int j = 0; j < getUnitCount (); ++j) - { - Unit *unit = getUnit (j); - const UnitType *currentUt = unit->getType (); - if (unitType == currentUt && unit->isOperative ()) - { - count++; - } - //check if there is any command active which already produces this unit - count = count + unit->getCountOfProducedUnits (unitType); - } - return count; - } - - - bool Faction::reqsOk (const CommandType * ct) const - { - assert (ct != NULL); - if (ct == NULL) - { - throw megaglest_runtime_error ("In [Faction::reqsOk] ct == NULL"); - } - - if (ct->getProduced () != NULL && reqsOk (ct->getProduced ()) == false) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] reqsOk FAILED\n", - __FILE__, __FUNCTION__, __LINE__); - return false; - } - - if (ct->getClass () == ccUpgrade) - { - const UpgradeCommandType *uct = - static_cast < const UpgradeCommandType * >(ct); - if (upgradeManager.isUpgradingOrUpgraded (uct->getProducedUpgrade ())) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] upgrade check FAILED\n", - __FILE__, __FUNCTION__, __LINE__); - return false; - } - } - - return reqsOk (static_cast < const RequirableType * >(ct)); - } - -// ================== cost application ================== - -//apply costs except static production (start building/production) - bool Faction::applyCosts (const ProducibleType * p, - const CommandType * ct) - { - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - if (checkCosts (p, ct) == false) - { - return false; - } - - assert (p != NULL); - //for each unit cost spend it - //pass 2, decrease resources, except negative static costs (ie: farms) - for (int i = 0; i < p->getCostCount (); ++i) - { - const Resource *r = p->getCost (i); - if (r == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "cannot apply costs for p [%s] %d of %d costs resource is null", - p->getName (false).c_str (), i, p->getCostCount ()); - throw megaglest_runtime_error (szBuf); - } - - const ResourceType *rt = r->getType (); - if (rt == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "cannot apply costs for p [%s] %d of %d costs resourcetype [%s] is null", - p->getName (false).c_str (), i, p->getCostCount (), - r->getDescription (false).c_str ()); - throw megaglest_runtime_error (szBuf); - } - int cost = r->getAmount (); - if ((cost > 0 || (rt->getClass () != rcStatic)) - && rt->getClass () != rcConsumable) - { - incResourceAmount (rt, -(cost)); - } - - } - } - return true; - } - -//apply discount (when a morph ends) - void Faction::applyDiscount (const ProducibleType * p, int discount) - { - assert (p != NULL); - //increase resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - assert (rt != NULL); - int cost = p->getCost (i)->getAmount (); - if ((cost > 0 || (rt->getClass () != rcStatic)) - && rt->getClass () != rcConsumable) - { - incResourceAmount (rt, cost * discount / 100); - } - } - } - -//apply static production (for starting units) - void Faction::applyStaticCosts (const ProducibleType * p, - const CommandType * ct) - { - assert (p != NULL); - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - //decrease static resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - //assert(rt != NULL); - if (rt == NULL) - { - throw megaglest_runtime_error (string (__FUNCTION__) + - " rt == NULL for ProducibleType [" - + p->getName (false) + - "] index: " + intToStr (i)); - } - if (rt->getClass () == rcStatic) - { - int cost = p->getCost (i)->getAmount (); - if (cost > 0) - { - incResourceAmount (rt, -cost); - } - } - } - } - } - -//apply static production (when a mana source is done) - void Faction::applyStaticProduction (const ProducibleType * p, - const CommandType * ct) - { - assert (p != NULL); - - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - //decrease static resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - assert (rt != NULL); - if (rt->getClass () == rcStatic) - { - int cost = p->getCost (i)->getAmount (); - if (cost < 0) - { - incResourceAmount (rt, -cost); - } - } - } - } - } - -//deapply all costs except static production (usually when a building is cancelled) - void Faction::deApplyCosts (const ProducibleType * p, - const CommandType * ct) - { - assert (p != NULL); - - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - //increase resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - assert (rt != NULL); - int cost = p->getCost (i)->getAmount (); - if ((cost > 0 || (rt->getClass () != rcStatic)) - && rt->getClass () != rcConsumable) - { - incResourceAmount (rt, cost); - } - } - } - } - -//deapply static costs (usually when a unit dies) - void Faction::deApplyStaticCosts (const ProducibleType * p, - const CommandType * ct) - { - assert (p != NULL); - - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - //decrease resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - assert (rt != NULL); - if (rt->getClass () == rcStatic) - { - if (rt->getRecoup_cost () == true) - { - int cost = p->getCost (i)->getAmount (); - incResourceAmount (rt, cost); - } - } - } - } - } - -//deapply static costs, but not negative costs, for when building gets killed - void Faction::deApplyStaticConsumption (const ProducibleType * p, - const CommandType * ct) - { - assert (p != NULL); - - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - } - - if (ignoreResourceCosts == false) - { - //decrease resources - for (int i = 0; i < p->getCostCount (); ++i) - { - const ResourceType *rt = p->getCost (i)->getType (); - assert (rt != NULL); - if (rt->getClass () == rcStatic) - { - int cost = p->getCost (i)->getAmount (); - if (cost > 0) - { - incResourceAmount (rt, cost); - } - } - } - } - } - -//apply resource on interval (cosumable resouces) - void Faction::applyCostsOnInterval (const ResourceType * rtApply) - { - - // For each Resource type we store in the int a total consumed value, then - // a vector of units that consume the resource type - std::map < const ResourceType *, std::pair < int, - std::vector > >resourceIntervalUsage; - - // count up consumables usage for the interval - for (int j = 0; j < getUnitCount (); ++j) - { - Unit *unit = getUnit (j); - if (unit->isOperative () == true) - { - for (int k = 0; k < unit->getType ()->getCostCount (); ++k) - { - const Resource *resource = unit->getType ()->getCost (k); - if (resource->getType () == rtApply - && resource->getType ()->getClass () == rcConsumable - && resource->getAmount () != 0) - { - if (resourceIntervalUsage.find (resource->getType ()) == - resourceIntervalUsage.end ()) - { - resourceIntervalUsage[resource->getType ()] = - make_pair < int, std::vector >(0, - std::vector < - Unit * >()); - } - // Negative cost means accumulate the resource type - resourceIntervalUsage[resource->getType ()].first += - -resource->getAmount (); - - // If the cost > 0 then the unit is a consumer - if (resource->getAmount () > 0) - { - resourceIntervalUsage[resource->getType ()].second. - push_back (unit); - } - } - } - } - } - - // Apply consumable resource usage - if (resourceIntervalUsage.empty () == false) - { - for (std::map < const ResourceType *, std::pair < int, - std::vector < Unit * > > >::iterator iter = - resourceIntervalUsage.begin (); - iter != resourceIntervalUsage.end (); ++iter) - { - // Apply resource type usage to faction resource store - const ResourceType *rt = iter->first; - int resourceTypeUsage = iter->second.first; - incResourceAmount (rt, resourceTypeUsage); - - // Check if we have any unit consumers - if (getResource (rt)->getAmount () < 0) - { - resetResourceAmount (rt); - - // Apply consequences to consumer units of this resource type - std::vector < Unit * >&resourceConsumers = iter->second.second; - - for (int i = 0; i < (int) resourceConsumers.size (); ++i) - { - Unit *unit = resourceConsumers[i]; - - //decrease unit hp - if (scriptManager->getPlayerModifiers (this->index)-> - getConsumeEnabled () == true) - { - bool decHpResult = - unit->decHp (unit->getType ()-> - getTotalMaxHp (unit->getTotalUpgrade ()) / 3); - if (decHpResult) - { - unit->setCauseOfDeath (ucodStarvedResource); - world->getStats ()->die (unit->getFactionIndex (), - unit->getType ()-> - getCountUnitDeathInStats ()); - scriptManager->onUnitDied (unit); - } - StaticSound *sound = - static_cast < - const DieSkillType * - >(unit->getType ()->getFirstStOfClass (scDie))->getSound (); - if (sound != NULL - && (thisFaction == true - || world->showWorldForPlayer (world-> - getThisTeamIndex ()) == - true)) - { - SoundRenderer::getInstance ().playFx (sound); - } - } - } - } - } - } - } - - bool Faction::checkCosts (const ProducibleType * pt, - const CommandType * ct) - { - assert (pt != NULL); - - bool ignoreResourceCosts = false; - if (ct != NULL && ct->getClass () == ccMorph) - { - const MorphCommandType *mct = - dynamic_cast < const MorphCommandType * >(ct); - if (mct != NULL) - { - ignoreResourceCosts = mct->getIgnoreResourceRequirements (); - } - //printf("Checking costs = %d for commandtype:\n%s\n",ignoreResourceCosts,mct->getDesc(NULL).c_str()); - } - - if (ignoreResourceCosts == false) - { - //for each unit cost check if enough resources - for (int i = 0; i < pt->getCostCount (); ++i) - { - const ResourceType *rt = pt->getCost (i)->getType (); - int cost = pt->getCost (i)->getAmount (); - if (cost > 0) - { - int available = getResource (rt)->getAmount (); - if (cost > available) - { - return false; - } - } - } - } - - return true; - } - -// ================== diplomacy ================== - - bool Faction::isAlly (const Faction * faction) - { - assert (faction != NULL); - return (teamIndex == faction->getTeam () || - faction->getTeam () == - GameConstants::maxPlayers - 1 + fpt_Observer); - } - -// ================== misc ================== - - void Faction::incResourceAmount (const ResourceType * rt, int amount) - { - if (world != NULL && world->getGame () != NULL - && world->getGame ()-> - isFlagType1BitEnabled (ft1_allow_shared_team_resources) == true) - { - for (int i = 0; i < (int) resources.size (); ++i) - { - Resource *r = &resources[i]; - if (r->getType () == rt) - { - r->setAmount (r->getAmount () + amount); - if (r->getType ()->getClass () != rcStatic - && (getResource (rt, false)->getAmount () + amount) > - getStoreAmount (rt, false)) - { - r->setAmount (getStoreAmount (rt, false) - - (getResource (rt, false)->getAmount () - - r->getAmount ())); - } - return; - } - } - } - else - { - for (int i = 0; i < (int) resources.size (); ++i) - { - Resource *r = &resources[i]; - if (r->getType () == rt) - { - r->setAmount (r->getAmount () + amount); - if (r->getType ()->getClass () != rcStatic - && r->getAmount () > getStoreAmount (rt)) - { - r->setAmount (getStoreAmount (rt)); - } - return; - } - } - } - assert (false); - } - - void Faction::setResourceBalance (const ResourceType * rt, int balance) - { - for (int i = 0; i < (int) resources.size (); ++i) - { - Resource *r = &resources[i]; - if (r->getType () == rt) - { - r->setBalance (balance); - return; - } - } - assert (false); - } - - Unit *Faction::findUnit (int id) const - { - UnitMap::const_iterator itFound = unitMap.find (id); - if (itFound == unitMap.end ()) - { - return NULL; - } - return itFound->second; - } - - void Faction::addUnit (Unit * unit) - { - MutexSafeWrapper safeMutex (unitsMutex, - string (__FILE__) + "_" + - intToStr (__LINE__)); - units.push_back (unit); - unitMap[unit->getId ()] = unit; - } - - void Faction::removeUnit (Unit * unit) - { - MutexSafeWrapper safeMutex (unitsMutex, - string (__FILE__) + "_" + - intToStr (__LINE__)); - - assert (units.size () == unitMap.size ()); - - int unitId = unit->getId (); - for (int i = 0; i < (int) units.size (); ++i) - { - if (units[i]->getId () == unitId) - { - units.erase (units.begin () + i); - unitMap.erase (unitId); - assert (units.size () == unitMap.size ()); - return; - } - } - - throw megaglest_runtime_error ("Could not remove unit from faction!"); - //assert(false); - } - - void Faction::addStore (const UnitType * unitType) - { - assert (unitType != NULL); - for (int newUnitStoredResourceIndex = 0; - newUnitStoredResourceIndex < unitType->getStoredResourceCount (); - ++newUnitStoredResourceIndex) - { - const Resource *newUnitStoredResource = - unitType->getStoredResource (newUnitStoredResourceIndex); - - for (int currentStoredResourceIndex = 0; - currentStoredResourceIndex < (int) store.size (); - ++currentStoredResourceIndex) - { - Resource *storedResource = &store[currentStoredResourceIndex]; - - if (storedResource->getType () == newUnitStoredResource->getType ()) - { - storedResource->setAmount (storedResource->getAmount () + - newUnitStoredResource->getAmount ()); - } - } - } - } - - void Faction::removeStore (const UnitType * unitType) - { - assert (unitType != NULL); - for (int i = 0; i < unitType->getStoredResourceCount (); ++i) - { - const Resource *r = unitType->getStoredResource (i); - for (int j = 0; j < (int) store.size (); ++j) - { - Resource *storedResource = &store[j]; - if (storedResource->getType () == r->getType ()) - { - storedResource->setAmount (storedResource->getAmount () - - r->getAmount ()); - } - } - } - limitResourcesToStore (); - } - - void Faction::limitResourcesToStore () - { - if (world != NULL && world->getGame () != NULL - && world->getGame ()-> - isFlagType1BitEnabled (ft1_allow_shared_team_resources) == true) - { - for (int i = 0; i < (int) resources.size (); ++i) - { - Resource *r = &resources[i]; - const ResourceType *rt = r->getType (); - if (rt->getClass () != rcStatic - && (getResource (rt, false)->getAmount ()) > getStoreAmount (rt, - false)) - { - r->setAmount (getStoreAmount (rt, false) - - (getResource (rt, false)->getAmount () - - r->getAmount ())); - } - } - } - else - { - for (int i = 0; i < (int) resources.size (); ++i) - { - Resource *r = &resources[i]; - Resource *s = &store[i]; - if (r->getType ()->getClass () != rcStatic - && r->getAmount () > s->getAmount ()) - { - r->setAmount (s->getAmount ()); - } - } - } - } - - void Faction::resetResourceAmount (const ResourceType * rt) - { - for (int i = 0; i < (int) resources.size (); ++i) - { - if (resources[i].getType () == rt) - { - resources[i].setAmount (0); - return; - } - } - assert (false); - } - - bool Faction::isResourceTargetInCache (const Vec2i & pos, - bool incrementUseCounter) - { - bool result = false; - - if (cachingDisabled == false) - { - if (cacheResourceTargetList.empty () == false) - { - std::map < Vec2i, int >::iterator iter = - cacheResourceTargetList.find (pos); - - result = (iter != cacheResourceTargetList.end ()); - if (result == true && incrementUseCounter == true) - { - iter->second++; - } - } - } - - return result; - } - - void Faction::addResourceTargetToCache (const Vec2i & pos, - bool incrementUseCounter) - { - if (cachingDisabled == false) - { - - bool duplicateEntry = - isResourceTargetInCache (pos, incrementUseCounter); - //bool duplicateEntry = false; - - if (duplicateEntry == false) - { - cacheResourceTargetList[pos] = 1; - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[addResourceTargetToCache] pos [%s]cacheResourceTargetList.size() [" - MG_SIZE_T_SPECIFIER "]", pos.getString ().c_str (), - cacheResourceTargetList.size ()); - - //unit->logSynchData(szBuf); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "----------------------------------- START [%d] ------------------------------------------------\n", - getFrameCount ()); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "[%s::%d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, "%s\n", - szBuf); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "------------------------------------ END [%d] -------------------------------------------------\n", - getFrameCount ()); - } - } - } - } - - void Faction::removeResourceTargetFromCache (const Vec2i & pos) - { - if (cachingDisabled == false) - { - if (cacheResourceTargetList.empty () == false) - { - std::map < Vec2i, int >::iterator iter = - cacheResourceTargetList.find (pos); - - if (iter != cacheResourceTargetList.end ()) - { - cacheResourceTargetList.erase (pos); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[removeResourceTargetFromCache] pos [%s]cacheResourceTargetList.size() [" - MG_SIZE_T_SPECIFIER "]", pos.getString ().c_str (), - cacheResourceTargetList.size ()); - - //unit->logSynchData(szBuf); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "----------------------------------- START [%d] ------------------------------------------------\n", - getFrameCount ()); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "[%s::%d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __LINE__); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, "%s\n", - szBuf); - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "------------------------------------ END [%d] -------------------------------------------------\n", - getFrameCount ()); - } - } - } - } - } - - void Faction::addCloseResourceTargetToCache (const Vec2i & pos) - { - if (cachingDisabled == false) - { - if (cachedCloseResourceTargetLookupList.find (pos) == - cachedCloseResourceTargetLookupList.end ()) - { - const Map *map = world->getMap (); - const int harvestDistance = 5; - - for (int j = -harvestDistance; j <= harvestDistance; ++j) - { - for (int k = -harvestDistance; k <= harvestDistance; ++k) - { - Vec2i newPos = pos + Vec2i (j, k); - if (isResourceTargetInCache (newPos) == false) - { - if (map->isInside (newPos.x, newPos.y)) - { - Resource *r = - map->getSurfaceCell (map->toSurfCoords (newPos))-> - getResource (); - if (r != NULL) - { - addResourceTargetToCache (newPos); - //cacheResourceTargetList[newPos] = 1; - } - } - } - } - } - - cachedCloseResourceTargetLookupList[pos] = true; - } - } - } - - Vec2i Faction::getClosestResourceTypeTargetFromCache (Unit * unit, - const ResourceType * - type, - int frameIndex) - { - Vec2i result (-1); - - if (cachingDisabled == false) - { - if (cacheResourceTargetList.empty () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "cacheResourceTargetList.size() [" MG_SIZE_T_SPECIFIER - "]", cacheResourceTargetList.size ()); - - if (frameIndex < 0) - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - } - - - std::vector < Vec2i > deleteList; - - const int harvestDistance = 5; - const Map *map = world->getMap (); - Vec2i pos = unit->getPos (); - - bool foundCloseResource = false; - // First look immediately around the unit's position - - // 0 means start looking leftbottom to top right -// if(Thread::isCurrentThreadMainThread() == false) { -// throw megaglest_runtime_error("#1 Invalid access to Faction random from outside main thread current id = " + -// intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); -// } - int tryRadius = random.randRange (0, 1); - //int tryRadius = unit->getRandom(true)->randRange(0,1); - //int tryRadius = 0; - if (tryRadius == 0) - { - for (int j = -harvestDistance; - j <= harvestDistance && foundCloseResource == false; ++j) - { - for (int k = -harvestDistance; - k <= harvestDistance && foundCloseResource == false; ++k) - { - Vec2i newPos = pos + Vec2i (j, k); - if (map->isInside (newPos) == true - && isResourceTargetInCache (newPos) == false) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (newPos)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 - || unit->getPos ().dist (newPos) < - unit->getPos ().dist (result)) - { - if (unit->isBadHarvestPos (newPos) == false) - { - result = newPos; - foundCloseResource = true; - break; - } - } - } - } - else - { - deleteList.push_back (newPos); - } - } - } - } - } - // start looking topright to leftbottom - else - { - for (int j = harvestDistance; - j >= -harvestDistance && foundCloseResource == false; --j) - { - for (int k = harvestDistance; - k >= -harvestDistance && foundCloseResource == false; --k) - { - Vec2i newPos = pos + Vec2i (j, k); - if (map->isInside (newPos) == true - && isResourceTargetInCache (newPos) == false) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (newPos)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 - || unit->getPos ().dist (newPos) < - unit->getPos ().dist (result)) - { - if (unit->isBadHarvestPos (newPos) == false) - { - result = newPos; - foundCloseResource = true; - break; - } - } - } - } - else - { - deleteList.push_back (newPos); - } - } - } - } - } - - if (foundCloseResource == false) - { - // Now check the whole cache - for (std::map < Vec2i, int >::iterator iter = - cacheResourceTargetList.begin (); - iter != cacheResourceTargetList.end () - && foundCloseResource == false; ++iter) - { - const Vec2i & cache = iter->first; - if (map->isInside (cache) == true) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (cache)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 - || unit->getPos ().dist (cache) < - unit->getPos ().dist (result)) - { - if (unit->isBadHarvestPos (cache) == false) - { - result = cache; - // Close enough to our position, no more looking - if (unit->getPos ().dist (result) <= - (harvestDistance * 2)) - { - foundCloseResource = true; - break; - } - } - } - } - } - else - { - deleteList.push_back (cache); - } - } - else - { - deleteList.push_back (cache); - } - } - } - - if (deleteList.empty () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == - true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[cleaning old resource targets] deleteList.size() [" - MG_SIZE_T_SPECIFIER - "] cacheResourceTargetList.size() [" - MG_SIZE_T_SPECIFIER "] result [%s]", - deleteList.size (), cacheResourceTargetList.size (), - result.getString ().c_str ()); - - if (frameIndex < 0) - { - unit->logSynchData (__FILE__, __LINE__, szBuf); - } - else - { - unit->logSynchDataThreaded (__FILE__, __LINE__, szBuf); - } - } - - cleanupResourceTypeTargetCache (&deleteList, frameIndex); - } - } - } - - return result; - } - -// CANNOT MODIFY the cache here since the AI calls this method and the AI is only controlled -// by the server for network games and it would cause out of synch since clients do not call -// this method so DO NOT modify the cache here! - Vec2i Faction::getClosestResourceTypeTargetFromCache (const Vec2i & pos, - const ResourceType * - type) - { - Vec2i result (-1); - if (cachingDisabled == false) - { - if (cacheResourceTargetList.empty () == false) - { - //std::vector deleteList; - - const int harvestDistance = 5; - const Map *map = world->getMap (); - - bool foundCloseResource = false; - - // 0 means start looking leftbottom to top right -// if(Thread::isCurrentThreadMainThread() == false) { -// throw megaglest_runtime_error("#2 Invalid access to Faction random from outside main thread current id = " + -// intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); -// } - int tryRadius = random.randRange (0, 1); - if (tryRadius == 0) - { - // First look immediately around the given position - for (int j = -harvestDistance; - j <= harvestDistance && foundCloseResource == false; ++j) - { - for (int k = -harvestDistance; - k <= harvestDistance && foundCloseResource == false; ++k) - { - Vec2i newPos = pos + Vec2i (j, k); - if (map->isInside (newPos) == true - && isResourceTargetInCache (newPos) == false) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (newPos)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 - || pos.dist (newPos) < pos.dist (result)) - { - result = newPos; - foundCloseResource = true; - break; - } - } - } - //else { - // deleteList.push_back(newPos); - //} - } - } - } - } - else - { - // First look immediately around the given position - for (int j = harvestDistance; - j >= -harvestDistance && foundCloseResource == false; --j) - { - for (int k = harvestDistance; - k >= -harvestDistance && foundCloseResource == false; --k) - { - Vec2i newPos = pos + Vec2i (j, k); - if (map->isInside (newPos) == true - && isResourceTargetInCache (newPos) == false) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (newPos)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 - || pos.dist (newPos) < pos.dist (result)) - { - result = newPos; - foundCloseResource = true; - break; - } - } - } - //else { - // deleteList.push_back(newPos); - //} - } - } - } - } - - if (foundCloseResource == false) - { - // Now check the whole cache - for (std::map < Vec2i, int >::iterator iter = - cacheResourceTargetList.begin (); - iter != cacheResourceTargetList.end () - && foundCloseResource == false; ++iter) - { - const Vec2i & cache = iter->first; - if (map->isInside (cache) == true) - { - const SurfaceCell *sc = - map->getSurfaceCell (map->toSurfCoords (cache)); - if (sc != NULL && sc->getResource () != NULL) - { - const Resource *resource = sc->getResource (); - if (resource->getType () != NULL - && resource->getType () == type) - { - if (result.x < 0 || pos.dist (cache) < pos.dist (result)) - { - result = cache; - // Close enough to our position, no more looking - if (pos.dist (result) <= (harvestDistance * 2)) - { - foundCloseResource = true; - break; - } - } - } - } - //else { - // deleteList.push_back(cache); - //} - } - //else { - // deleteList.push_back(cache); - //} - } - } - } - } - - return result; - } - - void Faction::cleanupResourceTypeTargetCache (std::vector < Vec2i > - *deleteListPtr, - int frameIndex) - { - if (cachingDisabled == false) - { - if (cacheResourceTargetList.empty () == false) - { - const int cleanupInterval = (GameConstants::updateFps * 5); - bool needToCleanup = (getFrameCount () % cleanupInterval == 0); - - if (deleteListPtr != NULL || needToCleanup == true) - { - std::vector < Vec2i > deleteList; - - if (deleteListPtr != NULL) - { - deleteList = *deleteListPtr; - } - else - { - for (std::map < Vec2i, int >::iterator iter = - cacheResourceTargetList.begin (); - iter != cacheResourceTargetList.end (); ++iter) - { - const Vec2i & cache = iter->first; - - if (world->getMap ()-> - getSurfaceCell (world->getMap ()->toSurfCoords (cache)) != - NULL) - { - Resource *resource = - world->getMap ()->getSurfaceCell (world->getMap ()-> - toSurfCoords (cache))-> - getResource (); - if (resource == NULL) - { - deleteList.push_back (cache); - } - } - else - { - deleteList.push_back (cache); - } - } - } - - if (deleteList.empty () == false) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[cleaning old resource targets] deleteList.size() [" - MG_SIZE_T_SPECIFIER - "] cacheResourceTargetList.size() [" - MG_SIZE_T_SPECIFIER "], needToCleanup [%d]", - deleteList.size (), cacheResourceTargetList.size (), - needToCleanup); - //unit->logSynchData(szBuf); - - char szBuf1[8096] = ""; - snprintf (szBuf1, 8096, - "----------------------------------- START [%d] ------------------------------------------------\n", - getFrameCount ()); - string logDataText = szBuf1; - - snprintf (szBuf1, 8096, "[%s::%d]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - logDataText += szBuf1; - - snprintf (szBuf1, 8096, "%s\n", szBuf); - logDataText += szBuf1; - - snprintf (szBuf1, 8096, - "------------------------------------ END [%d] -------------------------------------------------\n", - getFrameCount ()); - logDataText += szBuf1; - - if (frameIndex < 0) - { - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - "%s", logDataText.c_str ()); - } - else - { - addWorldSynchThreadedLogList (logDataText); - } - } - - for (int i = 0; i < (int) deleteList.size (); ++i) - { - Vec2i & cache = deleteList[i]; - cacheResourceTargetList.erase (cache); - } - } - } - } - } - } - -//std::vector Faction::findCachedPath(const Vec2i &target, Unit *unit) { -// std::vector result; -// if(cachingDisabled == false) { -// if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) { -// // Lets find the shortest and most successful path already taken by a -// // similar sized unit -// -// bool foundCachedPath = false; -// std::vector &cacheList = successfulPathFinderTargetList[target]; -// int unitSize = unit->getType()->getSize(); -// for(int i = 0; i < cacheList.size(); ++i) { -// FactionPathSuccessCache &cache = cacheList[i]; -// if(cache.unitSize <= unitSize) { -// vector, int> > &pathQueue = cache.pathQueue; -// -// for(int j = 0; j < pathQueue.size(); ++j) { -// // Now start at the end of the path and see how many nodes -// // until we reach a cell near the unit's current position -// std::pair, int> &path = pathQueue[j]; -// -// for(int k = path.first.size() - 1; k >= 0; --k) { -// if(world->getMap()->canMove(unit, unit->getPos(), path.first[k]) == true) { -// if(foundCachedPath == false) { -// for(int l = k; l < path.first.size(); ++l) { -// result.push_back(path.first[l]); -// } -// } -// else { -// if(result.size() > (path.first.size() - k)) { -// for(int l = k; l < path.first.size(); ++l) { -// result.push_back(path.first[l]); -// } -// } -// } -// foundCachedPath = true; -// -// break; -// } -// } -// } -// } -// } -// } -// } -// -// return result; -//} - -//void Faction::addCachedPath(const Vec2i &target, Unit *unit) { -// if(cachingDisabled == false) { -// if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) { -// FactionPathSuccessCache cache; -// cache.unitSize = unit->getType()->getSize(); -// cache.pathQueue.push_back(make_pair, int>(unit->getCurrentTargetPathTaken().second,1)); -// successfulPathFinderTargetList[target].push_back(cache); -// } -// else { -// bool finishedAdd = false; -// std::pair > currentTargetPathTaken = unit->getCurrentTargetPathTaken(); -// std::vector &cacheList = successfulPathFinderTargetList[target]; -// int unitSize = unit->getType()->getSize(); -// -// for(int i = 0; i < cacheList.size() && finishedAdd == false; ++i) { -// FactionPathSuccessCache &cache = cacheList[i]; -// if(cache.unitSize <= unitSize) { -// vector, int> > &pathQueue = cache.pathQueue; -// -// for(int j = 0; j < pathQueue.size() && finishedAdd == false; ++j) { -// // Now start at the end of the path and see how many nodes are the same -// std::pair, int> &path = pathQueue[j]; -// int minPathSize = std::min(path.first.size(),currentTargetPathTaken.second.size()); -// int intersectIndex = -1; -// -// for(int k = 0; k < minPathSize; ++k) { -// if(path.first[path.first.size() - k - 1] != currentTargetPathTaken.second[currentTargetPathTaken.second.size() - k - 1]) { -// intersectIndex = k; -// break; -// } -// } -// -// // New path is same or longer than old path so replace -// // old path with new -// if(intersectIndex + 1 == path.first.size()) { -// path.first = currentTargetPathTaken.second; -// path.second++; -// finishedAdd = true; -// } -// // Old path is same or longer than new path so -// // do nothing -// else if(intersectIndex + 1 == currentTargetPathTaken.second.size()) { -// path.second++; -// finishedAdd = true; -// } -// } -// -// // If new path is >= 10 cells add it -// if(finishedAdd == false && currentTargetPathTaken.second.size() >= 10) { -// pathQueue.push_back(make_pair, int>(currentTargetPathTaken.second,1)); -// } -// } -// } -// } -// } -//} - - void Faction::deletePixels () - { - if (factionType != NULL) - { - factionType->deletePixels (); - } - } - -//Unit * Faction::findClosestUnitWithSkillClass( const Vec2i &pos,const CommandClass &cmdClass, -// const std::vector &skillClassList, -// const UnitType *unitType) { -// Unit *result = NULL; -// -///* -// std::map >::iterator iterFind = cacheUnitCommandClassList.find(cmdClass); -// if(iterFind != cacheUnitCommandClassList.end()) { -// for(std::map::iterator iter = iterFind->second.begin(); -// iter != iterFind->second.end(); ++iter) { -// Unit *curUnit = findUnit(iter->second); -// if(curUnit != NULL) { -// -// const CommandType *cmdType = curUnit->getType()->getFirstCtOfClass(cmdClass); -// bool isUnitPossibleCandidate = (cmdType != NULL); -// if(skillClassList.empty() == false) { -// isUnitPossibleCandidate = false; -// -// for(int j = 0; j < skillClassList.size(); ++j) { -// SkillClass skValue = skillClassList[j]; -// if(curUnit->getCurrSkill()->getClass() == skValue) { -// isUnitPossibleCandidate = true; -// break; -// } -// } -// } -// -// if(isUnitPossibleCandidate == true) { -// if(result == NULL || curUnit->getPos().dist(pos) < result->getPos().dist(pos)) { -// result = curUnit; -// } -// } -// } -// } -// } -//*/ -// -// //if(result == NULL) { -// for(int i = 0; i < getUnitCount(); ++i) { -// Unit *curUnit = getUnit(i); -// -// bool isUnitPossibleCandidate = false; -// -// const CommandType *cmdType = curUnit->getType()->getFirstCtOfClass(cmdClass); -// if(cmdType != NULL) { -// const RepairCommandType *rct = dynamic_cast(cmdType); -// if(rct != NULL && rct->isRepairableUnitType(unitType)) { -// isUnitPossibleCandidate = true; -// } -// } -// else { -// isUnitPossibleCandidate = false; -// } -// -// if(isUnitPossibleCandidate == true && skillClassList.empty() == false) { -// isUnitPossibleCandidate = false; -// -// for(int j = 0; j < (int)skillClassList.size(); ++j) { -// SkillClass skValue = skillClassList[j]; -// if(curUnit->getCurrSkill()->getClass() == skValue) { -// isUnitPossibleCandidate = true; -// break; -// } -// } -// } -// -// -// if(isUnitPossibleCandidate == true) { -// //cacheUnitCommandClassList[cmdClass][curUnit->getId()] = curUnit->getId(); -// -// if(result == NULL || curUnit->getPos().dist(pos) < result->getPos().dist(pos)) { -// result = curUnit; -// } -// } -// } -// //} -// return result; -//} - - int Faction::getFrameCount () - { - int frameCount = 0; - const Game *game = Renderer::getInstance ().getGame (); - if (game != NULL && game->getWorld () != NULL) - { - frameCount = game->getWorld ()->getFrameCount (); - } - - return frameCount; - } - - const SwitchTeamVote *Faction::getFirstSwitchTeamVote () const - { - const SwitchTeamVote *vote = NULL; - if (switchTeamVotes.empty () == false) - { - for (std::map < int, SwitchTeamVote >::const_iterator iterMap = - switchTeamVotes.begin (); iterMap != switchTeamVotes.end (); - ++iterMap) - { - const SwitchTeamVote & curVote = iterMap->second; - if (curVote.voted == false) - { - vote = &curVote; - break; - } - } - } - - return vote; - } - - SwitchTeamVote *Faction::getSwitchTeamVote (int factionIndex) - { - SwitchTeamVote *vote = NULL; - if (switchTeamVotes.find (factionIndex) != switchTeamVotes.end ()) - { - vote = &switchTeamVotes[factionIndex]; - } - - return vote; - } - - void Faction::setSwitchTeamVote (SwitchTeamVote & vote) - { - switchTeamVotes[vote.factionIndex] = vote; - } - - bool Faction::canCreateUnit (const UnitType * ut, bool checkBuild, - bool checkProduce, bool checkMorph) const - { - // Now check that at least 1 other unit can produce, build or morph this unit - bool foundUnit = false; - for (int l = 0; l < this->getUnitCount () && foundUnit == false; ++l) - { - const UnitType *unitType2 = this->getUnit (l)->getType (); - - for (int j = 0; - j < unitType2->getCommandTypeCount () && foundUnit == false; ++j) - { - const CommandType *cmdType = unitType2->getCommandType (j); - if (cmdType != NULL) - { - // Check if this is a produce command - if (checkProduce == true && cmdType->getClass () == ccProduce) - { - const ProduceCommandType *produce = - dynamic_cast < const ProduceCommandType * >(cmdType); - if (produce != NULL) - { - const UnitType *produceUnit = produce->getProducedUnit (); - - if (produceUnit != NULL && - ut->getId () != unitType2->getId () && - ut->getName (false) == produceUnit->getName (false)) - { - foundUnit = true; - break; - } - } - } - // Check if this is a build command - else if (checkBuild == true && cmdType->getClass () == ccBuild) - { - const BuildCommandType *build = - dynamic_cast < const BuildCommandType * >(cmdType); - if (build != NULL) - { - for (int k = 0; - k < build->getBuildingCount () && foundUnit == false; - ++k) - { - const UnitType *buildUnit = build->getBuilding (k); - - if (buildUnit != NULL && - ut->getId () != unitType2->getId () && - ut->getName (false) == buildUnit->getName (false)) - { - foundUnit = true; - break; - } - } - } - } - // Check if this is a morph command - else if (checkMorph == true && cmdType->getClass () == ccMorph) - { - const MorphCommandType *morph = - dynamic_cast < const MorphCommandType * >(cmdType); - if (morph != NULL) - { - const UnitType *morphUnit = morph->getMorphUnit (); - - if (morphUnit != NULL && - ut->getId () != unitType2->getId () && - ut->getName (false) == morphUnit->getName (false)) - { - foundUnit = true; - break; - } - } - } - } - } - } - - return foundUnit; - } - - void Faction::clearCaches () - { - cacheResourceTargetList.clear (); - cachedCloseResourceTargetLookupList.clear (); - - //aliveUnitListCache.clear(); - //mobileUnitListCache.clear(); - //beingBuiltUnitListCache.clear(); - - unsigned int unitCount = this->getUnitCount (); - for (unsigned int i = 0; i < unitCount; ++i) - { - Unit *unit = this->getUnit (i); - if (unit != NULL) - { - unit->clearCaches (); - } - } - } - - uint64 Faction::getCacheKBytes (uint64 * cache1Size, uint64 * cache2Size, - uint64 * cache3Size, uint64 * cache4Size, - uint64 * cache5Size) - { - uint64 cache1Count = 0; - uint64 cache2Count = 0; - uint64 cache3Count = 0; - uint64 cache4Count = 0; - uint64 cache5Count = 0; - - for (std::map < Vec2i, int >::iterator iterMap1 = - cacheResourceTargetList.begin (); - iterMap1 != cacheResourceTargetList.end (); ++iterMap1) - { - cache1Count++; - } - for (std::map < Vec2i, bool >::iterator iterMap1 = - cachedCloseResourceTargetLookupList.begin (); - iterMap1 != cachedCloseResourceTargetLookupList.end (); ++iterMap1) - { - cache2Count++; - } - for (std::map < int, const Unit * >::iterator iterMap1 = - aliveUnitListCache.begin (); iterMap1 != aliveUnitListCache.end (); - ++iterMap1) - { - cache3Count++; - } - for (std::map < int, const Unit * >::iterator iterMap1 = - mobileUnitListCache.begin (); - iterMap1 != mobileUnitListCache.end (); ++iterMap1) - { - cache4Count++; - } - for (std::map < int, const Unit * >::iterator iterMap1 = - beingBuiltUnitListCache.begin (); - iterMap1 != beingBuiltUnitListCache.end (); ++iterMap1) - { - cache5Count++; - } - - if (cache1Size) - { - *cache1Size = cache1Count; - } - if (cache2Size) - { - *cache2Size = cache2Count; - } - if (cache3Size) - { - *cache3Size = cache3Count; - } - if (cache4Size) - { - *cache4Size = cache4Count; - } - if (cache5Size) - { - *cache5Size = cache5Count; - } - - uint64 totalBytes = cache1Count * sizeof (int); - totalBytes += cache2Count * sizeof (bool); - totalBytes += cache3Count * (sizeof (int) + sizeof (const Unit *)); - totalBytes += cache4Count * (sizeof (int) + sizeof (const Unit *)); - totalBytes += cache5Count * (sizeof (int) + sizeof (const Unit *)); - - totalBytes /= 1000; - - return totalBytes; - } - - string Faction::getCacheStats () - { - string result = ""; - - int cache1Count = 0; - int cache2Count = 0; - - for (std::map < Vec2i, int >::iterator iterMap1 = - cacheResourceTargetList.begin (); - iterMap1 != cacheResourceTargetList.end (); ++iterMap1) - { - cache1Count++; - } - for (std::map < Vec2i, bool >::iterator iterMap1 = - cachedCloseResourceTargetLookupList.begin (); - iterMap1 != cachedCloseResourceTargetLookupList.end (); ++iterMap1) - { - cache2Count++; - } - - uint64 totalBytes = cache1Count * sizeof (int); - totalBytes += cache2Count * sizeof (bool); - - totalBytes /= 1000; - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, "cache1Count [%d] cache2Count [%d] total KB: %s", - cache1Count, cache2Count, formatNumber (totalBytes).c_str ()); - result = szBuf; - return result; - } - - std::string Faction::toString (bool crcMode) const - { - std::string result = "FactionIndex = " + intToStr (this->index) + "\n"; - result += "teamIndex = " + intToStr (this->teamIndex) + "\n"; - result += - "startLocationIndex = " + intToStr (this->startLocationIndex) + "\n"; - if (crcMode == false) - { - result += "thisFaction = " + intToStr (this->thisFaction) + "\n"; - result += "control = " + intToStr (this->control) + "\n"; - } - - if (this->factionType != NULL) - { - result += this->factionType->toString () + "\n"; - } - - result += this->upgradeManager.toString () + "\n"; - - result += "ResourceCount = " + intToStr (resources.size ()) + "\n"; - for (int idx = 0; idx < (int) resources.size (); idx++) - { - result += - "index = " + intToStr (idx) + " " + resources[idx].toString () + - "\n"; - } - - result += "StoreCount = " + intToStr (store.size ()) + "\n"; - for (int idx = 0; idx < (int) store.size (); idx++) - { - result += - "index = " + intToStr (idx) + " " + store[idx].toString () + "\n"; - } - - result += "Allies = " + intToStr (allies.size ()) + "\n"; - for (int idx = 0; idx < (int) allies.size (); idx++) - { - result += - "index = " + intToStr (idx) + " name: " + - allies[idx]->factionType->getName (false) + " factionindex = " + - intToStr (allies[idx]->index) + "\n"; - } - - result += "Units = " + intToStr (units.size ()) + "\n"; - for (int idx = 0; idx < (int) units.size (); idx++) - { - result += units[idx]->toString (crcMode) + "\n"; - } - - return result; - } - - void Faction::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *factionNode = rootNode->addChild ("Faction"); - - upgradeManager.saveGame (factionNode); - for (unsigned int i = 0; i < resources.size (); ++i) - { - Resource & resource = resources[i]; - resource.saveGame (factionNode); - } - XmlNode *storeNode = factionNode->addChild ("Store"); - for (unsigned int i = 0; i < store.size (); ++i) - { - Resource & resource = store[i]; - resource.saveGame (storeNode); - } - - for (unsigned int i = 0; i < allies.size (); ++i) - { - Faction *ally = allies[i]; - XmlNode *allyNode = factionNode->addChild ("Ally"); - allyNode->addAttribute ("allyFactionIndex", - intToStr (ally->getIndex ()), - mapTagReplacements); - } - for (unsigned int i = 0; i < units.size (); ++i) - { - Unit *unit = units[i]; - unit->saveGame (factionNode); - } - - factionNode->addAttribute ("control", intToStr (control), - mapTagReplacements); - - factionNode->addAttribute ("overridePersonalityType", - intToStr (overridePersonalityType), - mapTagReplacements); - factionNode->addAttribute ("factiontype", factionType->getName (false), - mapTagReplacements); - factionNode->addAttribute ("index", intToStr (index), - mapTagReplacements); - factionNode->addAttribute ("teamIndex", intToStr (teamIndex), - mapTagReplacements); - factionNode->addAttribute ("startLocationIndex", - intToStr (startLocationIndex), - mapTagReplacements); - factionNode->addAttribute ("thisFaction", intToStr (thisFaction), - mapTagReplacements); - - for (std::map < Vec2i, int >::iterator iterMap = - cacheResourceTargetList.begin (); - iterMap != cacheResourceTargetList.end (); ++iterMap) - { - XmlNode *cacheResourceTargetListNode = - factionNode->addChild ("cacheResourceTargetList"); - - cacheResourceTargetListNode->addAttribute ("key", - iterMap->first. - getString (), - mapTagReplacements); - cacheResourceTargetListNode->addAttribute ("value", - intToStr (iterMap->second), - mapTagReplacements); - } - - for (std::map < Vec2i, bool >::iterator iterMap = - cachedCloseResourceTargetLookupList.begin (); - iterMap != cachedCloseResourceTargetLookupList.end (); ++iterMap) - { - XmlNode *cachedCloseResourceTargetLookupListNode = - factionNode->addChild ("cachedCloseResourceTargetLookupList"); - - cachedCloseResourceTargetLookupListNode->addAttribute ("key", - iterMap->first. - getString (), - mapTagReplacements); - cachedCloseResourceTargetLookupListNode->addAttribute ("value", - intToStr - (iterMap-> - second), - mapTagReplacements); - } - - factionNode->addAttribute ("random", intToStr (random.getLastNumber ()), - mapTagReplacements); - factionNode->addAttribute ("currentSwitchTeamVoteFactionIndex", - intToStr (currentSwitchTeamVoteFactionIndex), - mapTagReplacements); - factionNode->addAttribute ("allowSharedTeamUnits", - intToStr (allowSharedTeamUnits), - mapTagReplacements); - - for (std::set < const UnitType * >::iterator iterMap = - lockedUnits.begin (); iterMap != lockedUnits.end (); ++iterMap) - { - XmlNode *lockedUnitsListNode = - factionNode->addChild ("lockedUnitList"); - const UnitType *ut = *iterMap; - - lockedUnitsListNode->addAttribute ("value", ut->getName (false), - mapTagReplacements); - } - - for (std::map < int, int >::iterator iterMap = unitsMovingList.begin (); - iterMap != unitsMovingList.end (); ++iterMap) - { - XmlNode *unitsMovingListNode = - factionNode->addChild ("unitsMovingList"); - - unitsMovingListNode->addAttribute ("key", intToStr (iterMap->first), - mapTagReplacements); - unitsMovingListNode->addAttribute ("value", - intToStr (iterMap->second), - mapTagReplacements); - } - - for (std::map < int, int >::iterator iterMap = - unitsPathfindingList.begin (); - iterMap != unitsPathfindingList.end (); ++iterMap) - { - XmlNode *unitsPathfindingListNode = - factionNode->addChild ("unitsPathfindingList"); - - unitsPathfindingListNode->addAttribute ("key", - intToStr (iterMap->first), - mapTagReplacements); - unitsPathfindingListNode->addAttribute ("value", - intToStr (iterMap->second), - mapTagReplacements); - } - } - - void Faction::loadGame (const XmlNode * rootNode, int factionIndex, - GameSettings * settings, World * world) - { - XmlNode *factionNode = NULL; - vector < XmlNode * >factionNodeList = - rootNode->getChildList ("Faction"); - for (unsigned int i = 0; i < factionNodeList.size (); ++i) - { - XmlNode *node = factionNodeList[i]; - if (node->getAttribute ("index")->getIntValue () == factionIndex) - { - factionNode = node; - break; - } - } - - if (factionNode != NULL) - { - - allies.clear (); - vector < XmlNode * >allyNodeList = factionNode->getChildList ("Ally"); - for (unsigned int i = 0; i < allyNodeList.size (); ++i) - { - XmlNode *allyNode = allyNodeList[i]; - - int allyFactionIndex = - allyNode->getAttribute ("allyFactionIndex")->getIntValue (); - allies.push_back (world->getFaction (allyFactionIndex)); - } - - vector < XmlNode * >unitNodeList = factionNode->getChildList ("Unit"); - for (unsigned int i = 0; i < unitNodeList.size (); ++i) - { - XmlNode *unitNode = unitNodeList[i]; - Unit *unit = Unit::loadGame (unitNode, settings, this, world); - this->addUnit (unit); - } - - for (unsigned int i = 0; i < resources.size (); ++i) - { - Resource & resource = resources[i]; - resource.loadGame (factionNode, i, techTree); - } - XmlNode *storeNode = factionNode->getChild ("Store"); - for (unsigned int i = 0; i < store.size (); ++i) - { - Resource & resource = store[i]; - resource.loadGame (storeNode, i, techTree); - } - - upgradeManager.loadGame (factionNode, this); - - control = - static_cast < ControlType > - (factionNode->getAttribute ("control")->getIntValue ()); - - if (factionNode->hasAttribute ("overridePersonalityType") == true) - { - overridePersonalityType = - static_cast < FactionPersonalityType > - (factionNode->getAttribute ("overridePersonalityType")-> - getIntValue ()); - } - - teamIndex = factionNode->getAttribute ("teamIndex")->getIntValue (); - - startLocationIndex = - factionNode->getAttribute ("startLocationIndex")->getIntValue (); - - thisFaction = - factionNode->getAttribute ("thisFaction")->getIntValue () != 0; - - if (factionNode->hasAttribute ("allowSharedTeamUnits") == true) - { - allowSharedTeamUnits = - factionNode->getAttribute ("allowSharedTeamUnits")-> - getIntValue () != 0; - } - - vector < XmlNode * >cacheResourceTargetListNodeList = - factionNode->getChildList ("cacheResourceTargetList"); - for (unsigned int i = 0; i < cacheResourceTargetListNodeList.size (); - ++i) - { - XmlNode *cacheResourceTargetListNode = - cacheResourceTargetListNodeList[i]; - - Vec2i vec = - Vec2i::strToVec2 (cacheResourceTargetListNode-> - getAttribute ("key")->getValue ()); - cacheResourceTargetList[vec] = - cacheResourceTargetListNode->getAttribute ("value")-> - getIntValue (); - } - vector < XmlNode * >cachedCloseResourceTargetLookupListNodeList = - factionNode->getChildList ("cachedCloseResourceTargetLookupList"); - for (unsigned int i = 0; - i < cachedCloseResourceTargetLookupListNodeList.size (); ++i) - { - XmlNode *cachedCloseResourceTargetLookupListNode = - cachedCloseResourceTargetLookupListNodeList[i]; - - Vec2i vec = - Vec2i::strToVec2 (cachedCloseResourceTargetLookupListNode-> - getAttribute ("key")->getValue ()); - cachedCloseResourceTargetLookupList[vec] = - cachedCloseResourceTargetLookupListNode->getAttribute ("value")-> - getIntValue () != 0; - } - - random.setLastNumber (factionNode->getAttribute ("random")-> - getIntValue ()); - - vector < XmlNode * >lockedUnitsListNodeList = - factionNode->getChildList ("lockedUnitList"); - for (unsigned int i = 0; i < lockedUnitsListNodeList.size (); ++i) - { - XmlNode *lockedUnitsListNode = lockedUnitsListNodeList[i]; - - string unitName = - lockedUnitsListNode->getAttribute ("value")->getValue (); - lockedUnits.insert (getType ()->getUnitType (unitName)); - } - - vector < XmlNode * >unitsMovingListNodeList = - factionNode->getChildList ("unitsMovingList"); - for (unsigned int i = 0; i < unitsMovingListNodeList.size (); ++i) - { - XmlNode *unitsMovingListNode = unitsMovingListNodeList[i]; - - int unitId = - unitsMovingListNode->getAttribute ("key")->getIntValue (); - unitsMovingList[unitId] = - unitsMovingListNode->getAttribute ("value")->getIntValue (); - } - vector < XmlNode * >unitsPathfindingListNodeList = - factionNode->getChildList ("unitsPathfindingList"); - for (unsigned int i = 0; i < unitsPathfindingListNodeList.size (); - ++i) - { - XmlNode *unitsPathfindingListNode = unitsPathfindingListNodeList[i]; - - int unitId = - unitsPathfindingListNode->getAttribute ("key")->getIntValue (); - unitsPathfindingList[unitId] = - unitsPathfindingListNode->getAttribute ("value")->getIntValue (); - } - } - } - - Checksum Faction::getCRC () - { - const bool consoleDebug = false; - - Checksum crcForFaction; - - // UpgradeManager upgradeManager; - - for (unsigned int i = 0; i < resources.size (); ++i) - { - Resource & resource = resources[i]; - //crcForFaction.addSum(resource.getCRC().getSum()); - uint32 crc = resource.getCRC ().getSum (); - crcForFaction.addBytes (&crc, sizeof (uint32)); - } - - if (consoleDebug) - { - if (getWorld ()->getFrameCount () % 40 == 0) - { - printf ("#1 Frame #: %d Faction: %d CRC: %u\n", - getWorld ()->getFrameCount (), index, - crcForFaction.getSum ()); - } - } - - for (unsigned int i = 0; i < store.size (); ++i) - { - Resource & resource = store[i]; - //crcForFaction.addSum(resource.getCRC().getSum()); - uint32 crc = resource.getCRC ().getSum (); - crcForFaction.addBytes (&crc, sizeof (uint32)); - } - - if (consoleDebug) - { - if (getWorld ()->getFrameCount () % 40 == 0) - { - printf ("#2 Frame #: %d Faction: %d CRC: %u\n", - getWorld ()->getFrameCount (), index, - crcForFaction.getSum ()); - } - } - - for (unsigned int i = 0; i < units.size (); ++i) - { - Unit *unit = units[i]; - //crcForFaction.addSum(unit->getCRC().getSum()); - uint32 crc = unit->getCRC ().getSum (); - crcForFaction.addBytes (&crc, sizeof (uint32)); - } - - if (consoleDebug) - { - if (getWorld ()->getFrameCount () % 40 == 0) - { - printf ("#3 Frame #: %d Faction: %d CRC: %u\n", - getWorld ()->getFrameCount (), index, - crcForFaction.getSum ()); - } - } - - return crcForFaction; - } - - void Faction::addCRC_DetailsForWorldFrame (int worldFrameCount, - bool isNetworkServer) - { - unsigned int MAX_FRAME_CACHE = 250; - if (isNetworkServer == true) - { - MAX_FRAME_CACHE += 250; - } - crcWorldFrameDetails[worldFrameCount] = this->toString (true); - //if(worldFrameCount <= 0) printf("Adding world frame: %d log entries: %lld\n",worldFrameCount,(long long int)crcWorldFrameDetails.size()); - - for (unsigned int i = 0; i < units.size (); ++i) - { - Unit *unit = units[i]; - - unit->getRandom ()->clearLastCaller (); - unit->clearNetworkCRCDecHpList (); - unit->clearParticleInfo (); - } - - if ((unsigned int) crcWorldFrameDetails.size () > MAX_FRAME_CACHE) - { - //printf("===> Removing older world frame log entries: %lld\n",(long long int)crcWorldFrameDetails.size()); - - for (; - (unsigned int) crcWorldFrameDetails.size () - MAX_FRAME_CACHE > - 0;) - { - crcWorldFrameDetails.erase (crcWorldFrameDetails.begin ()); - } - } - } - - string Faction::getCRC_DetailsForWorldFrame (int worldFrameCount) - { - if (crcWorldFrameDetails.empty ()) - { - return ""; - } - return crcWorldFrameDetails[worldFrameCount]; - } - - std::pair < int, - string > - Faction::getCRC_DetailsForWorldFrameIndex (int worldFrameIndex) const - { - if (crcWorldFrameDetails.empty ()) - { - return make_pair < int, string > (0, ""); - } - std::map < int, string >::const_iterator iterMap = - crcWorldFrameDetails.begin (); - std::advance (iterMap, worldFrameIndex); - if (iterMap == crcWorldFrameDetails.end ()) - { - return make_pair < int, string > (0, ""); - } - return std::pair < int, string > (iterMap->first, iterMap->second); - } - - string Faction::getCRC_DetailsForWorldFrames () const - { - string result = ""; - for (std::map < int, string >::const_iterator iterMap = - crcWorldFrameDetails.begin (); - iterMap != crcWorldFrameDetails.end (); ++iterMap) - { - result += - string - ("============================================================================\n"); - result += - string ("** world frame: ") + intToStr (iterMap->first) + - string (" detail: ") + iterMap->second; - } - return result; - } - - uint64 Faction::getCRC_DetailsForWorldFrameCount () const - { - return crcWorldFrameDetails.size (); - } - - } +namespace Glest { + namespace Game { + + bool CommandGroupUnitSorterId::operator () (const int l, const int r) { + const Unit *lUnit = faction->findUnit(l); + const Unit *rUnit = faction->findUnit(r); + + if (!lUnit) { + printf("Error lUnit == NULL for id = %d factionIndex = %d\n", l, + faction->getIndex()); + + for (unsigned int i = 0; i < (unsigned int) faction->getUnitCount(); + ++i) { + printf("%u / %d id = %d [%s]\n", i, faction->getUnitCount(), + faction->getUnit(i)->getId(), + faction->getUnit(i)->getType()->getName(false).c_str()); + } + } + if (!rUnit) { + printf("Error rUnit == NULL for id = %d factionIndex = %d\n", r, + faction->getIndex()); + + for (unsigned int i = 0; i < (unsigned int) faction->getUnitCount(); + ++i) { + printf("%u / %d id = %d [%s]\n", i, faction->getUnitCount(), + faction->getUnit(i)->getId(), + faction->getUnit(i)->getType()->getName(false).c_str()); + } + } + + CommandGroupUnitSorter sorter; + return sorter.compare(lUnit, rUnit); + } + + bool CommandGroupUnitSorter::operator () (const Unit * l, const Unit * r) { + return compare(l, r); + } + + bool CommandGroupUnitSorter::compare(const Unit * l, const Unit * r) { + //printf("l [%p] r [%p] <>",l,r); + + if (!l) { + printf("Error l == NULL\n"); + } + if (!r) { + printf("Error r == NULL\n"); + } + + assert(l && r); + + if (l == NULL || r == NULL) + printf("Unit l [%s - %d] r [%s - %d]\n", + (l != + NULL ? l->getType()->getName(false).c_str() : "null"), + (l != NULL ? l->getId() : -1), + (r != + NULL ? r->getType()->getName(false).c_str() : "null"), + (r != NULL ? r->getId() : -1)); + + + bool result = false; + // If comparer is null or dead + if (r == NULL || r->isAlive() == false) { + // if source is null or dead also + if ((l == NULL || l->isAlive() == false)) { + return false; + } + return true; + } else if ((l == NULL || l->isAlive() == false)) { + return false; + } + + // const Command *command= l->getCurrrentCommandThreadSafe(); + // const Command *commandPeer = r->getCurrrentCommandThreadSafe(); + const Command *command = l->getCurrCommand(); + const Command *commandPeer = r->getCurrCommand(); + + //Command *command= this->unit->getCurrCommand(); + + // Are we moving or attacking + if (command != NULL && command->getCommandType() != NULL && + (command->getCommandType()->getClass() == ccMove || + command->getCommandType()->getClass() == ccAttack) && + command->getUnitCommandGroupId() > 0) { + int curCommandGroupId = command->getUnitCommandGroupId(); + + //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); + //Command *commandPeer = j.unit->getCurrCommand(); + + // is comparer a valid command + if (commandPeer == NULL || commandPeer->getCommandType() == NULL) { + result = true; + } + // is comparer command the same type? + else if (commandPeer->getCommandType()->getClass() != + command->getCommandType()->getClass()) { + result = true; + } + // is comparer command groupid invalid? + else if (commandPeer->getUnitCommandGroupId() < 0) { + result = true; + } + // If comparer command group id is less than current group id + else if (curCommandGroupId != commandPeer->getUnitCommandGroupId()) { + result = curCommandGroupId < commandPeer->getUnitCommandGroupId(); + } else { + float unitDist = l->getCenteredPos().dist(command->getPos()); + float unitDistPeer = + r->getCenteredPos().dist(commandPeer->getPos()); + + // Closest unit in commandgroup + result = (unitDist < unitDistPeer); + } + } else if (command == NULL && commandPeer != NULL) { + result = false; + } + // else if(command == NULL && j.unit->getCurrrentCommandThreadSafe() == NULL) { + // return this->unit->getId() < j.unit->getId(); + // } + else { + //Command *commandPeer = j.unit->getCurrrentCommandThreadSafe(); + //if( commandPeer != NULL && commandPeer->getCommandType() != NULL && + // (commandPeer->getCommandType()->getClass() != ccMove && + // commandPeer->getCommandType()->getClass() != ccAttack)) { + result = (l->getId() < r->getId()); + //} + //else { + // result = (l->getId() < r->getId()); + //} + } + + //printf("Sorting, unit [%d - %s] cmd [%s] | unit2 [%d - %s] cmd [%s] result = %d\n",this->unit->getId(),this->unit->getFullName().c_str(),(this->unit->getCurrCommand() == NULL ? "NULL" : this->unit->getCurrCommand()->toString().c_str()),j.unit->getId(),j.unit->getFullName().c_str(),(j.unit->getCurrCommand() == NULL ? "NULL" : j.unit->getCurrCommand()->toString().c_str()),result); + + return result; + } + + void Faction::sortUnitsByCommandGroups() { + MutexSafeWrapper safeMutex(unitsMutex, + string(__FILE__) + "_" + + intToStr(__LINE__)); + //printf("====== sortUnitsByCommandGroups for faction # %d [%s] unitCount = %d\n",this->getIndex(),this->getType()->getName().c_str(),units.size()); + //for(unsigned int i = 0; i < units.size(); ++i) { + // printf("%d / %d [%p] <>",i,units.size(),&units[i]); + // // printf("i = %d [%p]\n",i,&units[i]); + // if(Unit::isUnitDeleted(units[i]) == true) { + // printf("i = %d [%p]\n",i,&units[i]); + // throw megaglest_runtime_error("unit already deleted!"); + // } + //} + //printf("\nSorting\n"); + + //std::sort(units.begin(),units.end(),CommandGroupUnitSorter()); + + //printf("====== Done sorting for faction # %d [%s] unitCount = %d\n",this->getIndex(),this->getType()->getName().c_str(),units.size()); + + //unsigned int originalUnitSize = (unsigned int)units.size(); + + std::vector < int >unitIds; + for (unsigned int i = 0; i < units.size(); ++i) { + int unitId = units[i]->getId(); + if (this->findUnit(unitId) == NULL) { + printf + ("#1 Error unitId not found for id = %d [%s] factionIndex = %d\n", + unitId, units[i]->getType()->getName(false).c_str(), + this->getIndex()); + + for (unsigned int j = 0; j < units.size(); ++j) { + printf("%u / %d id = %d [%s]\n", j, (int) units.size(), + units[j]->getId(), + units[j]->getType()->getName(false).c_str()); + } + } + unitIds.push_back(unitId); + } + CommandGroupUnitSorterId sorter; + sorter.faction = this; + std::stable_sort(unitIds.begin(), unitIds.end(), sorter); + + units.clear(); + for (unsigned int i = 0; i < unitIds.size(); ++i) { + + int unitId = unitIds[i]; + if (this->findUnit(unitId) == NULL) { + printf("#2 Error unitId not found for id = %d factionIndex = %d\n", + unitId, this->getIndex()); + + for (unsigned int j = 0; j < units.size(); ++j) { + printf("%u / %d id = %d [%s]\n", j, (int) units.size(), + units[j]->getId(), + units[j]->getType()->getName(false).c_str()); + } + } + + units.push_back(this->findUnit(unitId)); + } + + //assert(originalUnitSize == units.size()); + } + + // ===================================================== + // class FactionThread + // ===================================================== + + FactionThread::FactionThread(Faction * faction) :BaseThread() { + this->triggerIdMutex = new Mutex(CODE_AT_LINE); + this->faction = faction; + this->masterController = NULL; + uniqueID = "FactionThread"; + } + + FactionThread::~FactionThread() { + this->faction = NULL; + this->masterController = NULL; + delete this->triggerIdMutex; + this->triggerIdMutex = NULL; + } + + void FactionThread::setQuitStatus(bool value) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d value = %d\n", + __FILE__, __FUNCTION__, __LINE__, value); + + BaseThread::setQuitStatus(value); + if (value == true) { + signalPathfinder(-1); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void FactionThread::signalPathfinder(int frameIndex) { + if (frameIndex >= 0) { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(triggerIdMutex, mutexOwnerId); + this->frameIndex.first = frameIndex; + this->frameIndex.second = false; + + safeMutex.ReleaseLock(); + } + semTaskSignalled.signal(); + } + + void FactionThread::setTaskCompleted(int frameIndex) { + if (frameIndex >= 0) { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(triggerIdMutex, mutexOwnerId); + if (this->frameIndex.first == frameIndex) { + this->frameIndex.second = true; + } + safeMutex.ReleaseLock(); + } + } + + bool FactionThread::canShutdown(bool deleteSelfIfShutdownDelayed) { + bool ret = (getExecutingTask() == false); + if (ret == false && deleteSelfIfShutdownDelayed == true) { + setDeleteSelfOnExecutionDone(deleteSelfIfShutdownDelayed); + deleteSelfIfRequired(); + signalQuit(); + } + + return ret; + } + + bool FactionThread::isSignalPathfinderCompleted(int frameIndex) { + if (getRunningStatus() == false) { + return true; + } + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(triggerIdMutex, mutexOwnerId); + //bool result = (event != NULL ? event->eventCompleted : true); + bool result = (this->frameIndex.first == frameIndex + && this->frameIndex.second == true); + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] worker thread this = %p, this->frameIndex.first = %d, this->frameIndex.second = %d\n",__FILE__,__FUNCTION__,__LINE__,this,this->frameIndex.first,this->frameIndex.second); + + safeMutex.ReleaseLock(); + return result; + } + + void FactionThread::execute() { + string codeLocation = "1"; + RunningStatusSafeWrapper runningStatus(this); + try { + //setRunningStatus(true); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] ****************** STARTING worker thread this = %p\n", + __FILE__, __FUNCTION__, __LINE__, this); + + bool minorDebugPerformance = false; + Chrono chrono; + + codeLocation = "2"; + //unsigned int idx = 0; + for (; this->faction != NULL;) { + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + break; + } + + semTaskSignalled.waitTillSignalled(); + + codeLocation = "3"; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + static string masterSlaveOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MasterSlaveThreadControllerSafeWrapper + safeMasterController(masterController, 20000, + masterSlaveOwnerId); + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + break; + } + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(triggerIdMutex, mutexOwnerId); + bool executeTask = (this->frameIndex.first >= 0); + int currentTriggeredFrameIndex = this->frameIndex.first; + + //if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] frameIndex = %d this = %p executeTask = %d\n",__FILE__,__FUNCTION__,__LINE__,frameIndex.first, this, executeTask); + + safeMutex.ReleaseLock(); + + codeLocation = "5"; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + if (executeTask == true) { + codeLocation = "6"; + ExecutingTaskSafeWrapper safeExecutingTaskMutex(this); + + if (this->faction == NULL) { + throw megaglest_runtime_error("this->faction == NULL"); + } + World *world = this->faction->getWorld(); + if (world == NULL) { + throw megaglest_runtime_error("world == NULL"); + } + + codeLocation = "7"; + //Config &config= Config::getInstance(); + //bool sortedUnitsAllowed = config.getBool("AllowGroupedUnitCommands","true"); + //bool sortedUnitsAllowed = false; + //if(sortedUnitsAllowed == true) { + + /// TODO: Why does this cause and OOS? + //this->faction->sortUnitsByCommandGroups (); + + //} + + codeLocation = "8"; + static string mutexOwnerId2 = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(faction->getUnitMutex(), + mutexOwnerId2); + + //if(SystemFlags::getSystemSettingType(SystemFlags::debugPerformance).enabled) chrono.start(); + if (minorDebugPerformance) + chrono.start(); + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + codeLocation = "9"; + int unitCount = this->faction->getUnitCount(); + for (int j = 0; j < unitCount; ++j) { + codeLocation = "10"; + Unit *unit = this->faction->getUnit(j); + if (unit == NULL) { + throw megaglest_runtime_error("unit == NULL"); + } + + codeLocation = "11"; + int64 elapsed1 = 0; + if (minorDebugPerformance) + elapsed1 = chrono.getMillis(); + + bool update = unit->needToUpdate(); + + codeLocation = "12"; + if (minorDebugPerformance + && (chrono.getMillis() - elapsed1) >= 1) + printf + ("Faction [%d - %s] #1-unit threaded updates on frame: %d for [%d] unit # %d, unitCount = %d, took [%lld] msecs\n", + faction->getStartLocationIndex(), + faction->getType()->getName(false).c_str(), + currentTriggeredFrameIndex, + faction->getUnitPathfindingListCount(), j, unitCount, + (long long int) chrono.getMillis() - elapsed1); + + //update = true; + if (update == true) { + codeLocation = "13"; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + int64 updateProgressValue = unit->getUpdateProgress(); + int64 speed = + unit->getCurrSkill()->getTotalSpeed(unit-> + getTotalUpgrade()); + int64 df = unit->getDiagonalFactor(); + int64 hf = unit->getHeightFactor(); + bool changedActiveCommand = unit->isChangedActiveCommand(); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "unit->needToUpdate() returned: %d updateProgressValue: %lld speed: %lld changedActiveCommand: %d df: %lld hf: %lld", + update, (long long int) updateProgressValue, + (long long int) speed, changedActiveCommand, + (long long int) df, (long long int) hf); + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } + + int64 elapsed2 = 0; + if (minorDebugPerformance) + elapsed2 = chrono.getMillis(); + + if (world->getUnitUpdater() == NULL) { + throw + megaglest_runtime_error + ("world->getUnitUpdater() == NULL"); + } + + world->getUnitUpdater()->updateUnitCommand(unit, + currentTriggeredFrameIndex); + + codeLocation = "15"; + if (minorDebugPerformance + && (chrono.getMillis() - elapsed2) >= 1) + printf + ("Faction [%d - %s] #2-unit threaded updates on frame: %d for [%d] unit # %d, unitCount = %d, took [%lld] msecs\n", + faction->getStartLocationIndex(), + faction->getType()->getName(false).c_str(), + currentTriggeredFrameIndex, + faction->getUnitPathfindingListCount(), j, unitCount, + (long long int) chrono.getMillis() - elapsed2); + } else { + codeLocation = "16"; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + int64 updateProgressValue = unit->getUpdateProgress(); + int64 speed = + unit->getCurrSkill()->getTotalSpeed(unit-> + getTotalUpgrade()); + int64 df = unit->getDiagonalFactor(); + int64 hf = unit->getHeightFactor(); + bool changedActiveCommand = unit->isChangedActiveCommand(); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "unit->needToUpdate() returned: %d updateProgressValue: %lld speed: %lld changedActiveCommand: %d df: %lld hf: %lld", + update, (long long int) updateProgressValue, + (long long int) speed, changedActiveCommand, + (long long int) df, (long long int) hf); + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } + } + } + + codeLocation = "17"; + if (minorDebugPerformance && chrono.getMillis() >= 1) + printf + ("Faction [%d - %s] threaded updates on frame: %d for [%d] units took [%lld] msecs\n", + faction->getStartLocationIndex(), + faction->getType()->getName(false).c_str(), + currentTriggeredFrameIndex, + faction->getUnitPathfindingListCount(), + (long long int) chrono.getMillis()); + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + safeMutex.ReleaseLock(); + + codeLocation = "18"; + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + setTaskCompleted(currentTriggeredFrameIndex); + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + } + + //printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + codeLocation = "19"; + if (getQuitStatus() == true) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + break; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("In [%s::%s Line: %d] ****************** ENDING worker thread this = %p\n", + __FILE__, __FUNCTION__, __LINE__, this); + } catch (const exception & ex) { + //setRunningStatus(false); + + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Loc [%s] Error [%s]\n", + __FILE__, __FUNCTION__, __LINE__, + codeLocation.c_str(), ex.what()); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + throw megaglest_runtime_error(ex.what()); + } catch (...) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "In [%s::%s %d] UNKNOWN error Loc [%s]\n", + __FILE__, __FUNCTION__, __LINE__, codeLocation.c_str()); + SystemFlags::OutputDebug(SystemFlags::debugError, szBuf); + throw megaglest_runtime_error(szBuf); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s] Line: %d\n", __FILE__, + __FUNCTION__, __LINE__); + } + + + // ===================================================== + // class Faction + // ===================================================== + + Faction::Faction() { + init(); + } + + void Faction::init() { + unitsMutex = new Mutex(CODE_AT_LINE); + texture = NULL; + //lastResourceTargettListPurge = 0; + cachingDisabled = false; + factionDisconnectHandled = false; + workerThread = NULL; + + world = NULL; + scriptManager = NULL; + factionType = NULL; + index = 0; + teamIndex = 0; + startLocationIndex = 0; + thisFaction = false; + currentSwitchTeamVoteFactionIndex = -1; + allowSharedTeamUnits = false; + + loadWorldNode = NULL; + techTree = NULL; + + control = ctClosed; + + overridePersonalityType = fpt_EndCount; + + upgradeManager = UpgradeManager(); + } + + Faction::~Faction() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //Renderer &renderer= Renderer::getInstance(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + //renderer.endTexture(rsGame,texture); + //texture->end(); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (workerThread != NULL) { + workerThread->signalQuit(); + if (workerThread->shutdownAndWait() == true) { + delete workerThread; + } + workerThread = NULL; + } + + MutexSafeWrapper safeMutex(unitsMutex, + string(__FILE__) + "_" + + intToStr(__LINE__)); + deleteValues(units.begin(), units.end()); + units.clear(); + + safeMutex.ReleaseLock(); + + //delete texture; + texture = NULL; + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + delete unitsMutex; + unitsMutex = NULL; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void Faction::end() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + if (workerThread != NULL) { + workerThread->signalQuit(); + if (workerThread->shutdownAndWait() == true) { + delete workerThread; + } + workerThread = NULL; + } + + MutexSafeWrapper safeMutex(unitsMutex, + string(__FILE__) + "_" + + intToStr(__LINE__)); + deleteValues(units.begin(), units.end()); + units.clear(); + + safeMutex.ReleaseLock(); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + void Faction::notifyUnitAliveStatusChange(const Unit * unit) { + if (unit != NULL) { + if (unit->isAlive() == true) { + aliveUnitListCache[unit->getId()] = unit; + + if (unit->getType()->isMobile() == true) { + mobileUnitListCache[unit->getId()] = unit; + } + } else { + aliveUnitListCache.erase(unit->getId()); + mobileUnitListCache.erase(unit->getId()); + beingBuiltUnitListCache.erase(unit->getId()); + } + } + } + + void Faction::notifyUnitTypeChange(const Unit * unit, + const UnitType * newType) { + if (unit != NULL) { + if (unit->getType()->isMobile() == true) { + mobileUnitListCache.erase(unit->getId()); + } + + if (newType != NULL && newType->isMobile() == true) { + mobileUnitListCache[unit->getId()] = unit; + } + } + } + + void Faction::notifyUnitSkillTypeChange(const Unit * unit, + const SkillType * newType) { + if (unit != NULL) { + if (unit->isBeingBuilt() == true) { + beingBuiltUnitListCache.erase(unit->getId()); + } + if (newType != NULL && newType->getClass() == scBeBuilt) { + beingBuiltUnitListCache[unit->getId()] = unit; + } + } + } + + bool Faction::hasAliveUnits(bool filterMobileUnits, + bool filterBuiltUnits) const { + bool result = false; + if (aliveUnitListCache.empty() == false) { + if (filterMobileUnits == true) { + result = (mobileUnitListCache.empty() == false); + } else { + result = true; + } + + if (result == true && filterBuiltUnits == true) { + result = (beingBuiltUnitListCache.empty() == true); + } + } + return result; + } + + FactionPersonalityType Faction::getPersonalityType() const { + if (overridePersonalityType != fpt_EndCount) { + return overridePersonalityType; + } + return factionType->getPersonalityType(); + } + + int Faction:: + getAIBehaviorStaticOverideValue(AIBehaviorStaticValueCategory type) + const { + return factionType->getAIBehaviorStaticOverideValue(type); + } + + void Faction::addUnitToMovingList(int unitId) { + unitsMovingList[unitId] = getWorld()->getFrameCount(); + } + void Faction::removeUnitFromMovingList(int unitId) { + unitsMovingList.erase(unitId); + } + + //int Faction::getUnitMovingListCount() { + // return (int)unitsMovingList.size(); + //} + + void Faction::addUnitToPathfindingList(int unitId) { + //printf("ADD (1) Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); + unitsPathfindingList[unitId] = getWorld()->getFrameCount(); + //printf("ADD (2) Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); + } + //void Faction::removeUnitFromPathfindingList(int unitId) { + // unitsPathfindingList.erase(unitId); + //} + + int Faction::getUnitPathfindingListCount() { + //printf("GET Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); + return (int) unitsPathfindingList.size(); + } + + void Faction::clearUnitsPathfinding() { + //printf("CLEAR Faction [%d - %s] threaded updates for [%d] units\n",this->getStartLocationIndex(),this->getType()->getName().c_str(),unitsPathfindingList.size()); + if (unitsPathfindingList.empty() == false) { + unitsPathfindingList.clear(); + } + } + + bool Faction::canUnitsPathfind() { + bool result = true; + if (control == ctCpuEasy || control == ctCpu || + control == ctCpuUltra || control == ctCpuZeta) { + //printf("AI player for faction index: %d (%s) current pathfinding: %d\n",index,factionType->getName().c_str(),getUnitPathfindingListCount()); + + const int MAX_UNITS_PATHFINDING_PER_FRAME = 10; + result = + (getUnitPathfindingListCount() <= MAX_UNITS_PATHFINDING_PER_FRAME); + if (result == false) { + //printf("WARNING limited AI player for faction index: %d (%s) current pathfinding: %d\n",index,factionType->getName().c_str(),getUnitPathfindingListCount()); + } + } + return result; + } + + void Faction::setLockedUnitForFaction(const UnitType * ut, bool lock) { + if (lock) { + lockedUnits.insert(ut); + } else { + std::set < const UnitType *>::iterator it; + it = lockedUnits.find(ut); + if (it != lockedUnits.end()) { + lockedUnits.erase(it); + } + } + + } + + void Faction::signalWorkerThread(int frameIndex) { + if (workerThread != NULL) { + workerThread->signalPathfinder(frameIndex); + } + } + + bool Faction::isWorkerThreadSignalCompleted(int frameIndex) { + if (workerThread != NULL) { + return workerThread->isSignalPathfinderCompleted(frameIndex); + } + return true; + } + + + void Faction::init(FactionType * factionType, ControlType control, + TechTree * techTree, Game * game, int factionIndex, + int teamIndex, int startLocationIndex, + bool thisFaction, bool giveResources, + const XmlNode * loadWorldNode) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + this->techTree = techTree; + this->loadWorldNode = loadWorldNode; + this->control = control; + this->factionType = factionType; + this->startLocationIndex = startLocationIndex; + this->index = factionIndex; + this->teamIndex = teamIndex; + this->thisFaction = thisFaction; + this->world = game->getWorld(); + this->scriptManager = game->getScriptManager(); + //cachingDisabled = (Config::getInstance().getBool("DisableCaching","false") == true); + cachingDisabled = false; + + resources.resize(techTree->getResourceTypeCount()); + store.resize(techTree->getResourceTypeCount()); + + if (loadWorldNode == NULL) { + for (int index = 0; index < techTree->getResourceTypeCount(); + ++index) { + const ResourceType *rt = techTree->getResourceType(index); + int resourceAmount = + giveResources ? factionType->getStartingResourceAmount(rt) : 0; + resources[index].init(rt, resourceAmount); + store[index].init(rt, 0); + + this->world->initTeamResource(rt, this->teamIndex, 0); + } + } + //initialize cache + for (int index = 0; index < techTree->getResourceTypeCount(); ++index) { + const ResourceType *rt = techTree->getResourceType(index); + this->updateUnitTypeWithResourceCostCache(rt); + } + + texture = Renderer::getInstance().newTexture2D(rsGame); + string data_path = + getGameReadWritePath(GameConstants::path_data_CacheLookupKey); + if (texture) { + string playerTexture = + getGameCustomCoreDataPath(data_path, + "data/core/faction_textures/faction" + + intToStr(startLocationIndex) + ".tga"); + texture->load(playerTexture); + } + + if (loadWorldNode != NULL) { + loadGame(loadWorldNode, this->index, game->getGameSettings(), + game->getWorld()); + } + + if (game->getGameSettings()->getPathFinderType() == pfBasic) { + if (workerThread != NULL) { + workerThread->signalQuit(); + if (workerThread->shutdownAndWait() == true) { + delete workerThread; + } + workerThread = NULL; + } + static string mutexOwnerId = + string(extractFileFromDirectoryPath(__FILE__).c_str()) + + string("_") + intToStr(__LINE__); + this->workerThread = new FactionThread(this); + this->workerThread->setUniqueID(mutexOwnerId); + this->workerThread->start(); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + // ================== get ================== + + bool Faction::hasUnitTypeWithResourceCostInCache(const ResourceType * rt) const { + std::string resourceTypeName = rt->getName(false); + std::map < std::string, bool >::const_iterator iterFind = + resourceTypeCostCache.find(resourceTypeName); + if (iterFind != resourceTypeCostCache.end()) { + return iterFind->second; + } + return false; + } + void Faction::updateUnitTypeWithResourceCostCache(const ResourceType * + rt) { + std::string resourceTypeName = rt->getName(false); + + if (resourceTypeCostCache.find(resourceTypeName) == + resourceTypeCostCache.end()) { + resourceTypeCostCache[resourceTypeName] = + hasUnitTypeWithResouceCost(rt); + } + } + + bool Faction::hasUnitTypeWithResouceCost(const ResourceType * rt) { + for (int factionUnitTypeIndex = 0; + factionUnitTypeIndex < getType()->getUnitTypeCount(); + ++factionUnitTypeIndex) { + + const UnitType *ut = getType()->getUnitType(factionUnitTypeIndex); + if (ut->getCost(rt) != NULL) { + return true; + } + } + return false; + } + + const Resource *Faction::getResource(const ResourceType * rt, + bool localFactionOnly) const { + + if (localFactionOnly == false && + world != NULL && world->getGame() != NULL) { + + Game *game = world->getGame(); + if (game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == + true) { + return world->getResourceForTeam(rt, this->getTeam()); + } + } + + for (int index = 0; index < (int) resources.size(); ++index) { + if (rt == resources[index].getType()) { + return &resources[index]; + } + } + + printf("ERROR cannot find resource type [%s] in list:\n", + (rt != NULL ? rt->getName().c_str() : "null")); + for (int i = 0; i < (int) resources.size(); ++i) { + printf("Index %d [%s]", i, + resources[i].getType()->getName().c_str()); + } + + assert(false); + return NULL; + } + + int Faction::getStoreAmount(const ResourceType * rt, + bool localFactionOnly) const { + + if (localFactionOnly == false && + world != NULL && world->getGame() != NULL) { + + Game *game = world->getGame(); + if (game->isFlagType1BitEnabled(ft1_allow_shared_team_resources) == + true) { + return world->getStoreAmountForTeam(rt, this->getTeam()); + } + } + + for (int index = 0; index < (int) store.size(); ++index) { + if (rt == store[index].getType()) { + return store[index].getAmount(); + } + } + printf("ERROR cannot find store type [%s] in list:\n", + (rt != NULL ? rt->getName().c_str() : "null")); + for (int i = 0; i < (int) store.size(); ++i) { + printf("Index %d [%s]", i, store[i].getType()->getName().c_str()); + } + + assert(false); + return 0; + } + + bool Faction::getCpuControl(bool enableServerControlledAI, + bool isNetworkGame, NetworkRole role) const { + bool result = false; + if (enableServerControlledAI == false || isNetworkGame == false) { + result = (control == ctCpuEasy || control == ctCpu + || control == ctCpuUltra || control == ctCpuZeta); + } else { + if (isNetworkGame == true) { + if (role == nrServer) { + result = (control == ctCpuEasy || control == ctCpu + || control == ctCpuUltra || control == ctCpuZeta); + } else { + result = (control == ctNetworkCpuEasy || control == ctNetworkCpu + || control == ctNetworkCpuUltra + || control == ctNetworkCpuZeta); + } + } + } + + return result; + } + + bool Faction::getCpuControl() const { + return control == ctCpuEasy || control == ctCpu || control == ctCpuUltra + || control == ctCpuZeta || control == ctNetworkCpuEasy + || control == ctNetworkCpu || control == ctNetworkCpuUltra + || control == ctNetworkCpuZeta; + } + + // ==================== upgrade manager ==================== + + void Faction::startUpgrade(const UpgradeType * ut) { + upgradeManager.startUpgrade(ut, index); + } + + void Faction::cancelUpgrade(const UpgradeType * ut) { + upgradeManager.cancelUpgrade(ut); + } + + void Faction::finishUpgrade(const UpgradeType * ut) { + upgradeManager.finishUpgrade(ut); + if (world->getThisFaction() != NULL + && this->getIndex() == world->getThisFaction()->getIndex()) { + Console *console = world->getGame()->getConsole(); + console->addStdMessage("UpgradeFinished", + ": " + formatString(ut->getName(true))); + } + for (int i = 0; i < getUnitCount(); ++i) { + getUnit(i)->applyUpgrade(ut); + } + } + + // ==================== reqs ==================== + + //checks if all required units and upgrades are present and maxUnitCount is within limit + bool Faction::reqsOk(const RequirableType * rt) const { + assert(rt != NULL); + //required units + for (int i = 0; i < rt->getUnitReqCount(); ++i) { + bool found = false; + for (int j = 0; j < getUnitCount(); ++j) { + Unit *unit = getUnit(j); + const UnitType *ut = unit->getType(); + if (rt->getUnitReq(i) == ut && unit->isOperative()) { + found = true; + break; + } + } + if (found == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + return false; + } + } + + //required upgrades + for (int i = 0; i < rt->getUpgradeReqCount(); ++i) { + if (upgradeManager.isUpgraded(rt->getUpgradeReq(i)) == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + return false; + } + } + + if (dynamic_cast (rt) != NULL) { + const UnitType *producedUnitType = + dynamic_cast (rt); + if (producedUnitType != NULL + && producedUnitType->getMaxUnitCount() > 0) { + if (producedUnitType->getMaxUnitCount() <= + getCountForMaxUnitCount(producedUnitType)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + return false; + } + } + + if (producedUnitType != NULL && isUnitLocked(producedUnitType)) { + return false; + } + } + + return true; + } + + int Faction::getCountForMaxUnitCount(const UnitType * unitType) const { + int count = 0; + //calculate current unit count + for (int j = 0; j < getUnitCount(); ++j) { + Unit *unit = getUnit(j); + const UnitType *currentUt = unit->getType(); + if (unitType == currentUt && unit->isOperative()) { + count++; + } + //check if there is any command active which already produces this unit + count = count + unit->getCountOfProducedUnits(unitType); + } + return count; + } + + + bool Faction::reqsOk(const CommandType * ct) const { + assert(ct != NULL); + if (ct == NULL) { + throw megaglest_runtime_error("In [Faction::reqsOk] ct == NULL"); + } + + if (ct->getProduced() != NULL && reqsOk(ct->getProduced()) == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] reqsOk FAILED\n", + __FILE__, __FUNCTION__, __LINE__); + return false; + } + + if (ct->getClass() == ccUpgrade) { + const UpgradeCommandType *uct = + static_cast (ct); + if (upgradeManager.isUpgradingOrUpgraded(uct->getProducedUpgrade())) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] upgrade check FAILED\n", + __FILE__, __FUNCTION__, __LINE__); + return false; + } + } + + return reqsOk(static_cast (ct)); + } + + // ================== cost application ================== + + //apply costs except static production (start building/production) + bool Faction::applyCosts(const ProducibleType * p, + const CommandType * ct) { + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + if (checkCosts(p, ct) == false) { + return false; + } + + assert(p != NULL); + //for each unit cost spend it + //pass 2, decrease resources, except negative static costs (ie: farms) + for (int i = 0; i < p->getCostCount(); ++i) { + const Resource *r = p->getCost(i); + if (r == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "cannot apply costs for p [%s] %d of %d costs resource is null", + p->getName(false).c_str(), i, p->getCostCount()); + throw megaglest_runtime_error(szBuf); + } + + const ResourceType *rt = r->getType(); + if (rt == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "cannot apply costs for p [%s] %d of %d costs resourcetype [%s] is null", + p->getName(false).c_str(), i, p->getCostCount(), + r->getDescription(false).c_str()); + throw megaglest_runtime_error(szBuf); + } + int cost = r->getAmount(); + if ((cost > 0 || (rt->getClass() != rcStatic)) + && rt->getClass() != rcConsumable) { + incResourceAmount(rt, -(cost)); + } + + } + } + return true; + } + + //apply discount (when a morph ends) + void Faction::applyDiscount(const ProducibleType * p, int discount) { + assert(p != NULL); + //increase resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + assert(rt != NULL); + int cost = p->getCost(i)->getAmount(); + if ((cost > 0 || (rt->getClass() != rcStatic)) + && rt->getClass() != rcConsumable) { + incResourceAmount(rt, cost * discount / 100); + } + } + } + + //apply static production (for starting units) + void Faction::applyStaticCosts(const ProducibleType * p, + const CommandType * ct) { + assert(p != NULL); + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + //decrease static resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + //assert(rt != NULL); + if (rt == NULL) { + throw megaglest_runtime_error(string(__FUNCTION__) + + " rt == NULL for ProducibleType [" + + p->getName(false) + + "] index: " + intToStr(i)); + } + if (rt->getClass() == rcStatic) { + int cost = p->getCost(i)->getAmount(); + if (cost > 0) { + incResourceAmount(rt, -cost); + } + } + } + } + } + + //apply static production (when a mana source is done) + void Faction::applyStaticProduction(const ProducibleType * p, + const CommandType * ct) { + assert(p != NULL); + + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + //decrease static resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + assert(rt != NULL); + if (rt->getClass() == rcStatic) { + int cost = p->getCost(i)->getAmount(); + if (cost < 0) { + incResourceAmount(rt, -cost); + } + } + } + } + } + + //deapply all costs except static production (usually when a building is cancelled) + void Faction::deApplyCosts(const ProducibleType * p, + const CommandType * ct) { + assert(p != NULL); + + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + //increase resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + assert(rt != NULL); + int cost = p->getCost(i)->getAmount(); + if ((cost > 0 || (rt->getClass() != rcStatic)) + && rt->getClass() != rcConsumable) { + incResourceAmount(rt, cost); + } + } + } + } + + //deapply static costs (usually when a unit dies) + void Faction::deApplyStaticCosts(const ProducibleType * p, + const CommandType * ct) { + assert(p != NULL); + + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + //decrease resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + assert(rt != NULL); + if (rt->getClass() == rcStatic) { + if (rt->getRecoup_cost() == true) { + int cost = p->getCost(i)->getAmount(); + incResourceAmount(rt, cost); + } + } + } + } + } + + //deapply static costs, but not negative costs, for when building gets killed + void Faction::deApplyStaticConsumption(const ProducibleType * p, + const CommandType * ct) { + assert(p != NULL); + + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + } + + if (ignoreResourceCosts == false) { + //decrease resources + for (int i = 0; i < p->getCostCount(); ++i) { + const ResourceType *rt = p->getCost(i)->getType(); + assert(rt != NULL); + if (rt->getClass() == rcStatic) { + int cost = p->getCost(i)->getAmount(); + if (cost > 0) { + incResourceAmount(rt, cost); + } + } + } + } + } + + //apply resource on interval (cosumable resouces) + void Faction::applyCostsOnInterval(const ResourceType * rtApply) { + + // For each Resource type we store in the int a total consumed value, then + // a vector of units that consume the resource type + std::map < const ResourceType *, std::pair < int, + std::vector > >resourceIntervalUsage; + + // count up consumables usage for the interval + for (int j = 0; j < getUnitCount(); ++j) { + Unit *unit = getUnit(j); + if (unit->isOperative() == true) { + for (int k = 0; k < unit->getType()->getCostCount(); ++k) { + const Resource *resource = unit->getType()->getCost(k); + if (resource->getType() == rtApply + && resource->getType()->getClass() == rcConsumable + && resource->getAmount() != 0) { + if (resourceIntervalUsage.find(resource->getType()) == + resourceIntervalUsage.end()) { + resourceIntervalUsage[resource->getType()] = + make_pair < int, std::vector >(0, + std::vector < + Unit * >()); + } + // Negative cost means accumulate the resource type + resourceIntervalUsage[resource->getType()].first += + -resource->getAmount(); + + // If the cost > 0 then the unit is a consumer + if (resource->getAmount() > 0) { + resourceIntervalUsage[resource->getType()].second. + push_back(unit); + } + } + } + } + } + + // Apply consumable resource usage + if (resourceIntervalUsage.empty() == false) { + for (std::map < const ResourceType *, std::pair < int, + std::vector < Unit * > > >::iterator iter = + resourceIntervalUsage.begin(); + iter != resourceIntervalUsage.end(); ++iter) { + // Apply resource type usage to faction resource store + const ResourceType *rt = iter->first; + int resourceTypeUsage = iter->second.first; + incResourceAmount(rt, resourceTypeUsage); + + // Check if we have any unit consumers + if (getResource(rt)->getAmount() < 0) { + resetResourceAmount(rt); + + // Apply consequences to consumer units of this resource type + std::vector < Unit * >&resourceConsumers = iter->second.second; + + for (int i = 0; i < (int) resourceConsumers.size(); ++i) { + Unit *unit = resourceConsumers[i]; + + //decrease unit hp + if (scriptManager->getPlayerModifiers(this->index)-> + getConsumeEnabled() == true) { + bool decHpResult = + unit->decHp(unit->getType()-> + getTotalMaxHp(unit->getTotalUpgrade()) / 3); + if (decHpResult) { + unit->setCauseOfDeath(ucodStarvedResource); + world->getStats()->die(unit->getFactionIndex(), + unit->getType()-> + getCountUnitDeathInStats()); + scriptManager->onUnitDied(unit); + } + StaticSound *sound = + static_cast < + const DieSkillType * + >(unit->getType()->getFirstStOfClass(scDie))->getSound(); + if (sound != NULL + && (thisFaction == true + || world->showWorldForPlayer(world-> + getThisTeamIndex()) == + true)) { + SoundRenderer::getInstance().playFx(sound); + } + } + } + } + } + } + } + + bool Faction::checkCosts(const ProducibleType * pt, + const CommandType * ct) { + assert(pt != NULL); + + bool ignoreResourceCosts = false; + if (ct != NULL && ct->getClass() == ccMorph) { + const MorphCommandType *mct = + dynamic_cast (ct); + if (mct != NULL) { + ignoreResourceCosts = mct->getIgnoreResourceRequirements(); + } + //printf("Checking costs = %d for commandtype:\n%s\n",ignoreResourceCosts,mct->getDesc(NULL).c_str()); + } + + if (ignoreResourceCosts == false) { + //for each unit cost check if enough resources + for (int i = 0; i < pt->getCostCount(); ++i) { + const ResourceType *rt = pt->getCost(i)->getType(); + int cost = pt->getCost(i)->getAmount(); + if (cost > 0) { + int available = getResource(rt)->getAmount(); + if (cost > available) { + return false; + } + } + } + } + + return true; + } + + // ================== diplomacy ================== + + bool Faction::isAlly(const Faction * faction) { + assert(faction != NULL); + return (teamIndex == faction->getTeam() || + faction->getTeam() == + GameConstants::maxPlayers - 1 + fpt_Observer); + } + + // ================== misc ================== + + void Faction::incResourceAmount(const ResourceType * rt, int amount) { + if (world != NULL && world->getGame() != NULL + && world->getGame()-> + isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true) { + for (int i = 0; i < (int) resources.size(); ++i) { + Resource *r = &resources[i]; + if (r->getType() == rt) { + r->setAmount(r->getAmount() + amount); + if (r->getType()->getClass() != rcStatic + && (getResource(rt, false)->getAmount() + amount) > + getStoreAmount(rt, false)) { + r->setAmount(getStoreAmount(rt, false) - + (getResource(rt, false)->getAmount() - + r->getAmount())); + } + return; + } + } + } else { + for (int i = 0; i < (int) resources.size(); ++i) { + Resource *r = &resources[i]; + if (r->getType() == rt) { + r->setAmount(r->getAmount() + amount); + if (r->getType()->getClass() != rcStatic + && r->getAmount() > getStoreAmount(rt)) { + r->setAmount(getStoreAmount(rt)); + } + return; + } + } + } + assert(false); + } + + void Faction::setResourceBalance(const ResourceType * rt, int balance) { + for (int i = 0; i < (int) resources.size(); ++i) { + Resource *r = &resources[i]; + if (r->getType() == rt) { + r->setBalance(balance); + return; + } + } + assert(false); + } + + Unit *Faction::findUnit(int id) const { + UnitMap::const_iterator itFound = unitMap.find(id); + if (itFound == unitMap.end()) { + return NULL; + } + return itFound->second; + } + + void Faction::addUnit(Unit * unit) { + MutexSafeWrapper safeMutex(unitsMutex, + string(__FILE__) + "_" + + intToStr(__LINE__)); + units.push_back(unit); + unitMap[unit->getId()] = unit; + } + + void Faction::removeUnit(Unit * unit) { + MutexSafeWrapper safeMutex(unitsMutex, + string(__FILE__) + "_" + + intToStr(__LINE__)); + + assert(units.size() == unitMap.size()); + + int unitId = unit->getId(); + for (int i = 0; i < (int) units.size(); ++i) { + if (units[i]->getId() == unitId) { + units.erase(units.begin() + i); + unitMap.erase(unitId); + assert(units.size() == unitMap.size()); + return; + } + } + + throw megaglest_runtime_error("Could not remove unit from faction!"); + //assert(false); + } + + void Faction::addStore(const UnitType * unitType) { + assert(unitType != NULL); + for (int newUnitStoredResourceIndex = 0; + newUnitStoredResourceIndex < unitType->getStoredResourceCount(); + ++newUnitStoredResourceIndex) { + const Resource *newUnitStoredResource = + unitType->getStoredResource(newUnitStoredResourceIndex); + + for (int currentStoredResourceIndex = 0; + currentStoredResourceIndex < (int) store.size(); + ++currentStoredResourceIndex) { + Resource *storedResource = &store[currentStoredResourceIndex]; + + if (storedResource->getType() == newUnitStoredResource->getType()) { + storedResource->setAmount(storedResource->getAmount() + + newUnitStoredResource->getAmount()); + } + } + } + } + + void Faction::removeStore(const UnitType * unitType) { + assert(unitType != NULL); + for (int i = 0; i < unitType->getStoredResourceCount(); ++i) { + const Resource *r = unitType->getStoredResource(i); + for (int j = 0; j < (int) store.size(); ++j) { + Resource *storedResource = &store[j]; + if (storedResource->getType() == r->getType()) { + storedResource->setAmount(storedResource->getAmount() - + r->getAmount()); + } + } + } + limitResourcesToStore(); + } + + void Faction::limitResourcesToStore() { + if (world != NULL && world->getGame() != NULL + && world->getGame()-> + isFlagType1BitEnabled(ft1_allow_shared_team_resources) == true) { + for (int i = 0; i < (int) resources.size(); ++i) { + Resource *r = &resources[i]; + const ResourceType *rt = r->getType(); + if (rt->getClass() != rcStatic + && (getResource(rt, false)->getAmount()) > getStoreAmount(rt, + false)) { + r->setAmount(getStoreAmount(rt, false) - + (getResource(rt, false)->getAmount() - + r->getAmount())); + } + } + } else { + for (int i = 0; i < (int) resources.size(); ++i) { + Resource *r = &resources[i]; + Resource *s = &store[i]; + if (r->getType()->getClass() != rcStatic + && r->getAmount() > s->getAmount()) { + r->setAmount(s->getAmount()); + } + } + } + } + + void Faction::resetResourceAmount(const ResourceType * rt) { + for (int i = 0; i < (int) resources.size(); ++i) { + if (resources[i].getType() == rt) { + resources[i].setAmount(0); + return; + } + } + assert(false); + } + + bool Faction::isResourceTargetInCache(const Vec2i & pos, + bool incrementUseCounter) { + bool result = false; + + if (cachingDisabled == false) { + if (cacheResourceTargetList.empty() == false) { + std::map < Vec2i, int >::iterator iter = + cacheResourceTargetList.find(pos); + + result = (iter != cacheResourceTargetList.end()); + if (result == true && incrementUseCounter == true) { + iter->second++; + } + } + } + + return result; + } + + void Faction::addResourceTargetToCache(const Vec2i & pos, + bool incrementUseCounter) { + if (cachingDisabled == false) { + + bool duplicateEntry = + isResourceTargetInCache(pos, incrementUseCounter); + //bool duplicateEntry = false; + + if (duplicateEntry == false) { + cacheResourceTargetList[pos] = 1; + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[addResourceTargetToCache] pos [%s]cacheResourceTargetList.size() [" + MG_SIZE_T_SPECIFIER "]", pos.getString().c_str(), + cacheResourceTargetList.size()); + + //unit->logSynchData(szBuf); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "----------------------------------- START [%d] ------------------------------------------------\n", + getFrameCount()); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "[%s::%d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, "%s\n", + szBuf); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "------------------------------------ END [%d] -------------------------------------------------\n", + getFrameCount()); + } + } + } + } + + void Faction::removeResourceTargetFromCache(const Vec2i & pos) { + if (cachingDisabled == false) { + if (cacheResourceTargetList.empty() == false) { + std::map < Vec2i, int >::iterator iter = + cacheResourceTargetList.find(pos); + + if (iter != cacheResourceTargetList.end()) { + cacheResourceTargetList.erase(pos); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[removeResourceTargetFromCache] pos [%s]cacheResourceTargetList.size() [" + MG_SIZE_T_SPECIFIER "]", pos.getString().c_str(), + cacheResourceTargetList.size()); + + //unit->logSynchData(szBuf); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "----------------------------------- START [%d] ------------------------------------------------\n", + getFrameCount()); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "[%s::%d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __LINE__); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, "%s\n", + szBuf); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "------------------------------------ END [%d] -------------------------------------------------\n", + getFrameCount()); + } + } + } + } + } + + void Faction::addCloseResourceTargetToCache(const Vec2i & pos) { + if (cachingDisabled == false) { + if (cachedCloseResourceTargetLookupList.find(pos) == + cachedCloseResourceTargetLookupList.end()) { + const Map *map = world->getMap(); + const int harvestDistance = 5; + + for (int j = -harvestDistance; j <= harvestDistance; ++j) { + for (int k = -harvestDistance; k <= harvestDistance; ++k) { + Vec2i newPos = pos + Vec2i(j, k); + if (isResourceTargetInCache(newPos) == false) { + if (map->isInside(newPos.x, newPos.y)) { + Resource *r = + map->getSurfaceCell(map->toSurfCoords(newPos))-> + getResource(); + if (r != NULL) { + addResourceTargetToCache(newPos); + //cacheResourceTargetList[newPos] = 1; + } + } + } + } + } + + cachedCloseResourceTargetLookupList[pos] = true; + } + } + } + + Vec2i Faction::getClosestResourceTypeTargetFromCache(Unit * unit, + const ResourceType * + type, + int frameIndex) { + Vec2i result(-1); + + if (cachingDisabled == false) { + if (cacheResourceTargetList.empty() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "cacheResourceTargetList.size() [" MG_SIZE_T_SPECIFIER + "]", cacheResourceTargetList.size()); + + if (frameIndex < 0) { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } + } + + + std::vector < Vec2i > deleteList; + + const int harvestDistance = 5; + const Map *map = world->getMap(); + Vec2i pos = unit->getPos(); + + bool foundCloseResource = false; + // First look immediately around the unit's position + + // 0 means start looking leftbottom to top right + // if(Thread::isCurrentThreadMainThread() == false) { + // throw megaglest_runtime_error("#1 Invalid access to Faction random from outside main thread current id = " + + // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); + // } + int tryRadius = random.randRange(0, 1); + //int tryRadius = unit->getRandom(true)->randRange(0,1); + //int tryRadius = 0; + if (tryRadius == 0) { + for (int j = -harvestDistance; + j <= harvestDistance && foundCloseResource == false; ++j) { + for (int k = -harvestDistance; + k <= harvestDistance && foundCloseResource == false; ++k) { + Vec2i newPos = pos + Vec2i(j, k); + if (map->isInside(newPos) == true + && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(newPos)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 + || unit->getPos().dist(newPos) < + unit->getPos().dist(result)) { + if (unit->isBadHarvestPos(newPos) == false) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + } else { + deleteList.push_back(newPos); + } + } + } + } + } + // start looking topright to leftbottom + else { + for (int j = harvestDistance; + j >= -harvestDistance && foundCloseResource == false; --j) { + for (int k = harvestDistance; + k >= -harvestDistance && foundCloseResource == false; --k) { + Vec2i newPos = pos + Vec2i(j, k); + if (map->isInside(newPos) == true + && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(newPos)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 + || unit->getPos().dist(newPos) < + unit->getPos().dist(result)) { + if (unit->isBadHarvestPos(newPos) == false) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + } else { + deleteList.push_back(newPos); + } + } + } + } + } + + if (foundCloseResource == false) { + // Now check the whole cache + for (std::map < Vec2i, int >::iterator iter = + cacheResourceTargetList.begin(); + iter != cacheResourceTargetList.end() + && foundCloseResource == false; ++iter) { + const Vec2i & cache = iter->first; + if (map->isInside(cache) == true) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(cache)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 + || unit->getPos().dist(cache) < + unit->getPos().dist(result)) { + if (unit->isBadHarvestPos(cache) == false) { + result = cache; + // Close enough to our position, no more looking + if (unit->getPos().dist(result) <= + (harvestDistance * 2)) { + foundCloseResource = true; + break; + } + } + } + } + } else { + deleteList.push_back(cache); + } + } else { + deleteList.push_back(cache); + } + } + } + + if (deleteList.empty() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == + true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[cleaning old resource targets] deleteList.size() [" + MG_SIZE_T_SPECIFIER + "] cacheResourceTargetList.size() [" + MG_SIZE_T_SPECIFIER "] result [%s]", + deleteList.size(), cacheResourceTargetList.size(), + result.getString().c_str()); + + if (frameIndex < 0) { + unit->logSynchData(__FILE__, __LINE__, szBuf); + } else { + unit->logSynchDataThreaded(__FILE__, __LINE__, szBuf); + } + } + + cleanupResourceTypeTargetCache(&deleteList, frameIndex); + } + } + } + + return result; + } + + // CANNOT MODIFY the cache here since the AI calls this method and the AI is only controlled + // by the server for network games and it would cause out of synch since clients do not call + // this method so DO NOT modify the cache here! + Vec2i Faction::getClosestResourceTypeTargetFromCache(const Vec2i & pos, + const ResourceType * + type) { + Vec2i result(-1); + if (cachingDisabled == false) { + if (cacheResourceTargetList.empty() == false) { + //std::vector deleteList; + + const int harvestDistance = 5; + const Map *map = world->getMap(); + + bool foundCloseResource = false; + + // 0 means start looking leftbottom to top right + // if(Thread::isCurrentThreadMainThread() == false) { + // throw megaglest_runtime_error("#2 Invalid access to Faction random from outside main thread current id = " + + // intToStr(Thread::getCurrentThreadId()) + " main = " + intToStr(Thread::getMainThreadId())); + // } + int tryRadius = random.randRange(0, 1); + if (tryRadius == 0) { + // First look immediately around the given position + for (int j = -harvestDistance; + j <= harvestDistance && foundCloseResource == false; ++j) { + for (int k = -harvestDistance; + k <= harvestDistance && foundCloseResource == false; ++k) { + Vec2i newPos = pos + Vec2i(j, k); + if (map->isInside(newPos) == true + && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(newPos)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 + || pos.dist(newPos) < pos.dist(result)) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + //else { + // deleteList.push_back(newPos); + //} + } + } + } + } else { + // First look immediately around the given position + for (int j = harvestDistance; + j >= -harvestDistance && foundCloseResource == false; --j) { + for (int k = harvestDistance; + k >= -harvestDistance && foundCloseResource == false; --k) { + Vec2i newPos = pos + Vec2i(j, k); + if (map->isInside(newPos) == true + && isResourceTargetInCache(newPos) == false) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(newPos)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 + || pos.dist(newPos) < pos.dist(result)) { + result = newPos; + foundCloseResource = true; + break; + } + } + } + //else { + // deleteList.push_back(newPos); + //} + } + } + } + } + + if (foundCloseResource == false) { + // Now check the whole cache + for (std::map < Vec2i, int >::iterator iter = + cacheResourceTargetList.begin(); + iter != cacheResourceTargetList.end() + && foundCloseResource == false; ++iter) { + const Vec2i & cache = iter->first; + if (map->isInside(cache) == true) { + const SurfaceCell *sc = + map->getSurfaceCell(map->toSurfCoords(cache)); + if (sc != NULL && sc->getResource() != NULL) { + const Resource *resource = sc->getResource(); + if (resource->getType() != NULL + && resource->getType() == type) { + if (result.x < 0 || pos.dist(cache) < pos.dist(result)) { + result = cache; + // Close enough to our position, no more looking + if (pos.dist(result) <= (harvestDistance * 2)) { + foundCloseResource = true; + break; + } + } + } + } + //else { + // deleteList.push_back(cache); + //} + } + //else { + // deleteList.push_back(cache); + //} + } + } + } + } + + return result; + } + + void Faction::cleanupResourceTypeTargetCache(std::vector < Vec2i > + *deleteListPtr, + int frameIndex) { + if (cachingDisabled == false) { + if (cacheResourceTargetList.empty() == false) { + const int cleanupInterval = (GameConstants::updateFps * 5); + bool needToCleanup = (getFrameCount() % cleanupInterval == 0); + + if (deleteListPtr != NULL || needToCleanup == true) { + std::vector < Vec2i > deleteList; + + if (deleteListPtr != NULL) { + deleteList = *deleteListPtr; + } else { + for (std::map < Vec2i, int >::iterator iter = + cacheResourceTargetList.begin(); + iter != cacheResourceTargetList.end(); ++iter) { + const Vec2i & cache = iter->first; + + if (world->getMap()-> + getSurfaceCell(world->getMap()->toSurfCoords(cache)) != + NULL) { + Resource *resource = + world->getMap()->getSurfaceCell(world->getMap()-> + toSurfCoords(cache))-> + getResource(); + if (resource == NULL) { + deleteList.push_back(cache); + } + } else { + deleteList.push_back(cache); + } + } + } + + if (deleteList.empty() == false) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[cleaning old resource targets] deleteList.size() [" + MG_SIZE_T_SPECIFIER + "] cacheResourceTargetList.size() [" + MG_SIZE_T_SPECIFIER "], needToCleanup [%d]", + deleteList.size(), cacheResourceTargetList.size(), + needToCleanup); + //unit->logSynchData(szBuf); + + char szBuf1[8096] = ""; + snprintf(szBuf1, 8096, + "----------------------------------- START [%d] ------------------------------------------------\n", + getFrameCount()); + string logDataText = szBuf1; + + snprintf(szBuf1, 8096, "[%s::%d]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + logDataText += szBuf1; + + snprintf(szBuf1, 8096, "%s\n", szBuf); + logDataText += szBuf1; + + snprintf(szBuf1, 8096, + "------------------------------------ END [%d] -------------------------------------------------\n", + getFrameCount()); + logDataText += szBuf1; + + if (frameIndex < 0) { + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + "%s", logDataText.c_str()); + } else { + addWorldSynchThreadedLogList(logDataText); + } + } + + for (int i = 0; i < (int) deleteList.size(); ++i) { + Vec2i & cache = deleteList[i]; + cacheResourceTargetList.erase(cache); + } + } + } + } + } + } + + //std::vector Faction::findCachedPath(const Vec2i &target, Unit *unit) { + // std::vector result; + // if(cachingDisabled == false) { + // if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) { + // // Lets find the shortest and most successful path already taken by a + // // similar sized unit + // + // bool foundCachedPath = false; + // std::vector &cacheList = successfulPathFinderTargetList[target]; + // int unitSize = unit->getType()->getSize(); + // for(int i = 0; i < cacheList.size(); ++i) { + // FactionPathSuccessCache &cache = cacheList[i]; + // if(cache.unitSize <= unitSize) { + // vector, int> > &pathQueue = cache.pathQueue; + // + // for(int j = 0; j < pathQueue.size(); ++j) { + // // Now start at the end of the path and see how many nodes + // // until we reach a cell near the unit's current position + // std::pair, int> &path = pathQueue[j]; + // + // for(int k = path.first.size() - 1; k >= 0; --k) { + // if(world->getMap()->canMove(unit, unit->getPos(), path.first[k]) == true) { + // if(foundCachedPath == false) { + // for(int l = k; l < path.first.size(); ++l) { + // result.push_back(path.first[l]); + // } + // } + // else { + // if(result.size() > (path.first.size() - k)) { + // for(int l = k; l < path.first.size(); ++l) { + // result.push_back(path.first[l]); + // } + // } + // } + // foundCachedPath = true; + // + // break; + // } + // } + // } + // } + // } + // } + // } + // + // return result; + //} + + //void Faction::addCachedPath(const Vec2i &target, Unit *unit) { + // if(cachingDisabled == false) { + // if(successfulPathFinderTargetList.find(target) == successfulPathFinderTargetList.end()) { + // FactionPathSuccessCache cache; + // cache.unitSize = unit->getType()->getSize(); + // cache.pathQueue.push_back(make_pair, int>(unit->getCurrentTargetPathTaken().second,1)); + // successfulPathFinderTargetList[target].push_back(cache); + // } + // else { + // bool finishedAdd = false; + // std::pair > currentTargetPathTaken = unit->getCurrentTargetPathTaken(); + // std::vector &cacheList = successfulPathFinderTargetList[target]; + // int unitSize = unit->getType()->getSize(); + // + // for(int i = 0; i < cacheList.size() && finishedAdd == false; ++i) { + // FactionPathSuccessCache &cache = cacheList[i]; + // if(cache.unitSize <= unitSize) { + // vector, int> > &pathQueue = cache.pathQueue; + // + // for(int j = 0; j < pathQueue.size() && finishedAdd == false; ++j) { + // // Now start at the end of the path and see how many nodes are the same + // std::pair, int> &path = pathQueue[j]; + // int minPathSize = std::min(path.first.size(),currentTargetPathTaken.second.size()); + // int intersectIndex = -1; + // + // for(int k = 0; k < minPathSize; ++k) { + // if(path.first[path.first.size() - k - 1] != currentTargetPathTaken.second[currentTargetPathTaken.second.size() - k - 1]) { + // intersectIndex = k; + // break; + // } + // } + // + // // New path is same or longer than old path so replace + // // old path with new + // if(intersectIndex + 1 == path.first.size()) { + // path.first = currentTargetPathTaken.second; + // path.second++; + // finishedAdd = true; + // } + // // Old path is same or longer than new path so + // // do nothing + // else if(intersectIndex + 1 == currentTargetPathTaken.second.size()) { + // path.second++; + // finishedAdd = true; + // } + // } + // + // // If new path is >= 10 cells add it + // if(finishedAdd == false && currentTargetPathTaken.second.size() >= 10) { + // pathQueue.push_back(make_pair, int>(currentTargetPathTaken.second,1)); + // } + // } + // } + // } + // } + //} + + void Faction::deletePixels() { + if (factionType != NULL) { + factionType->deletePixels(); + } + } + + //Unit * Faction::findClosestUnitWithSkillClass( const Vec2i &pos,const CommandClass &cmdClass, + // const std::vector &skillClassList, + // const UnitType *unitType) { + // Unit *result = NULL; + // + ///* + // std::map >::iterator iterFind = cacheUnitCommandClassList.find(cmdClass); + // if(iterFind != cacheUnitCommandClassList.end()) { + // for(std::map::iterator iter = iterFind->second.begin(); + // iter != iterFind->second.end(); ++iter) { + // Unit *curUnit = findUnit(iter->second); + // if(curUnit != NULL) { + // + // const CommandType *cmdType = curUnit->getType()->getFirstCtOfClass(cmdClass); + // bool isUnitPossibleCandidate = (cmdType != NULL); + // if(skillClassList.empty() == false) { + // isUnitPossibleCandidate = false; + // + // for(int j = 0; j < skillClassList.size(); ++j) { + // SkillClass skValue = skillClassList[j]; + // if(curUnit->getCurrSkill()->getClass() == skValue) { + // isUnitPossibleCandidate = true; + // break; + // } + // } + // } + // + // if(isUnitPossibleCandidate == true) { + // if(result == NULL || curUnit->getPos().dist(pos) < result->getPos().dist(pos)) { + // result = curUnit; + // } + // } + // } + // } + // } + //*/ + // + // //if(result == NULL) { + // for(int i = 0; i < getUnitCount(); ++i) { + // Unit *curUnit = getUnit(i); + // + // bool isUnitPossibleCandidate = false; + // + // const CommandType *cmdType = curUnit->getType()->getFirstCtOfClass(cmdClass); + // if(cmdType != NULL) { + // const RepairCommandType *rct = dynamic_cast(cmdType); + // if(rct != NULL && rct->isRepairableUnitType(unitType)) { + // isUnitPossibleCandidate = true; + // } + // } + // else { + // isUnitPossibleCandidate = false; + // } + // + // if(isUnitPossibleCandidate == true && skillClassList.empty() == false) { + // isUnitPossibleCandidate = false; + // + // for(int j = 0; j < (int)skillClassList.size(); ++j) { + // SkillClass skValue = skillClassList[j]; + // if(curUnit->getCurrSkill()->getClass() == skValue) { + // isUnitPossibleCandidate = true; + // break; + // } + // } + // } + // + // + // if(isUnitPossibleCandidate == true) { + // //cacheUnitCommandClassList[cmdClass][curUnit->getId()] = curUnit->getId(); + // + // if(result == NULL || curUnit->getPos().dist(pos) < result->getPos().dist(pos)) { + // result = curUnit; + // } + // } + // } + // //} + // return result; + //} + + int Faction::getFrameCount() { + int frameCount = 0; + const Game *game = Renderer::getInstance().getGame(); + if (game != NULL && game->getWorld() != NULL) { + frameCount = game->getWorld()->getFrameCount(); + } + + return frameCount; + } + + const SwitchTeamVote *Faction::getFirstSwitchTeamVote() const { + const SwitchTeamVote *vote = NULL; + if (switchTeamVotes.empty() == false) { + for (std::map < int, SwitchTeamVote >::const_iterator iterMap = + switchTeamVotes.begin(); iterMap != switchTeamVotes.end(); + ++iterMap) { + const SwitchTeamVote & curVote = iterMap->second; + if (curVote.voted == false) { + vote = &curVote; + break; + } + } + } + + return vote; + } + + SwitchTeamVote *Faction::getSwitchTeamVote(int factionIndex) { + SwitchTeamVote *vote = NULL; + if (switchTeamVotes.find(factionIndex) != switchTeamVotes.end()) { + vote = &switchTeamVotes[factionIndex]; + } + + return vote; + } + + void Faction::setSwitchTeamVote(SwitchTeamVote & vote) { + switchTeamVotes[vote.factionIndex] = vote; + } + + bool Faction::canCreateUnit(const UnitType * ut, bool checkBuild, + bool checkProduce, bool checkMorph) const { + // Now check that at least 1 other unit can produce, build or morph this unit + bool foundUnit = false; + for (int l = 0; l < this->getUnitCount() && foundUnit == false; ++l) { + const UnitType *unitType2 = this->getUnit(l)->getType(); + + for (int j = 0; + j < unitType2->getCommandTypeCount() && foundUnit == false; ++j) { + const CommandType *cmdType = unitType2->getCommandType(j); + if (cmdType != NULL) { + // Check if this is a produce command + if (checkProduce == true && cmdType->getClass() == ccProduce) { + const ProduceCommandType *produce = + dynamic_cast (cmdType); + if (produce != NULL) { + const UnitType *produceUnit = produce->getProducedUnit(); + + if (produceUnit != NULL && + ut->getId() != unitType2->getId() && + ut->getName(false) == produceUnit->getName(false)) { + foundUnit = true; + break; + } + } + } + // Check if this is a build command + else if (checkBuild == true && cmdType->getClass() == ccBuild) { + const BuildCommandType *build = + dynamic_cast (cmdType); + if (build != NULL) { + for (int k = 0; + k < build->getBuildingCount() && foundUnit == false; + ++k) { + const UnitType *buildUnit = build->getBuilding(k); + + if (buildUnit != NULL && + ut->getId() != unitType2->getId() && + ut->getName(false) == buildUnit->getName(false)) { + foundUnit = true; + break; + } + } + } + } + // Check if this is a morph command + else if (checkMorph == true && cmdType->getClass() == ccMorph) { + const MorphCommandType *morph = + dynamic_cast (cmdType); + if (morph != NULL) { + const UnitType *morphUnit = morph->getMorphUnit(); + + if (morphUnit != NULL && + ut->getId() != unitType2->getId() && + ut->getName(false) == morphUnit->getName(false)) { + foundUnit = true; + break; + } + } + } + } + } + } + + return foundUnit; + } + + void Faction::clearCaches() { + cacheResourceTargetList.clear(); + cachedCloseResourceTargetLookupList.clear(); + + //aliveUnitListCache.clear(); + //mobileUnitListCache.clear(); + //beingBuiltUnitListCache.clear(); + + unsigned int unitCount = this->getUnitCount(); + for (unsigned int i = 0; i < unitCount; ++i) { + Unit *unit = this->getUnit(i); + if (unit != NULL) { + unit->clearCaches(); + } + } + } + + uint64 Faction::getCacheKBytes(uint64 * cache1Size, uint64 * cache2Size, + uint64 * cache3Size, uint64 * cache4Size, + uint64 * cache5Size) { + uint64 cache1Count = 0; + uint64 cache2Count = 0; + uint64 cache3Count = 0; + uint64 cache4Count = 0; + uint64 cache5Count = 0; + + for (std::map < Vec2i, int >::iterator iterMap1 = + cacheResourceTargetList.begin(); + iterMap1 != cacheResourceTargetList.end(); ++iterMap1) { + cache1Count++; + } + for (std::map < Vec2i, bool >::iterator iterMap1 = + cachedCloseResourceTargetLookupList.begin(); + iterMap1 != cachedCloseResourceTargetLookupList.end(); ++iterMap1) { + cache2Count++; + } + for (std::map < int, const Unit * >::iterator iterMap1 = + aliveUnitListCache.begin(); iterMap1 != aliveUnitListCache.end(); + ++iterMap1) { + cache3Count++; + } + for (std::map < int, const Unit * >::iterator iterMap1 = + mobileUnitListCache.begin(); + iterMap1 != mobileUnitListCache.end(); ++iterMap1) { + cache4Count++; + } + for (std::map < int, const Unit * >::iterator iterMap1 = + beingBuiltUnitListCache.begin(); + iterMap1 != beingBuiltUnitListCache.end(); ++iterMap1) { + cache5Count++; + } + + if (cache1Size) { + *cache1Size = cache1Count; + } + if (cache2Size) { + *cache2Size = cache2Count; + } + if (cache3Size) { + *cache3Size = cache3Count; + } + if (cache4Size) { + *cache4Size = cache4Count; + } + if (cache5Size) { + *cache5Size = cache5Count; + } + + uint64 totalBytes = cache1Count * sizeof(int); + totalBytes += cache2Count * sizeof(bool); + totalBytes += cache3Count * (sizeof(int) + sizeof(const Unit *)); + totalBytes += cache4Count * (sizeof(int) + sizeof(const Unit *)); + totalBytes += cache5Count * (sizeof(int) + sizeof(const Unit *)); + + totalBytes /= 1000; + + return totalBytes; + } + + string Faction::getCacheStats() { + string result = ""; + + int cache1Count = 0; + int cache2Count = 0; + + for (std::map < Vec2i, int >::iterator iterMap1 = + cacheResourceTargetList.begin(); + iterMap1 != cacheResourceTargetList.end(); ++iterMap1) { + cache1Count++; + } + for (std::map < Vec2i, bool >::iterator iterMap1 = + cachedCloseResourceTargetLookupList.begin(); + iterMap1 != cachedCloseResourceTargetLookupList.end(); ++iterMap1) { + cache2Count++; + } + + uint64 totalBytes = cache1Count * sizeof(int); + totalBytes += cache2Count * sizeof(bool); + + totalBytes /= 1000; + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, "cache1Count [%d] cache2Count [%d] total KB: %s", + cache1Count, cache2Count, formatNumber(totalBytes).c_str()); + result = szBuf; + return result; + } + + std::string Faction::toString(bool crcMode) const { + std::string result = "FactionIndex = " + intToStr(this->index) + "\n"; + result += "teamIndex = " + intToStr(this->teamIndex) + "\n"; + result += + "startLocationIndex = " + intToStr(this->startLocationIndex) + "\n"; + if (crcMode == false) { + result += "thisFaction = " + intToStr(this->thisFaction) + "\n"; + result += "control = " + intToStr(this->control) + "\n"; + } + + if (this->factionType != NULL) { + result += this->factionType->toString() + "\n"; + } + + result += this->upgradeManager.toString() + "\n"; + + result += "ResourceCount = " + intToStr(resources.size()) + "\n"; + for (int idx = 0; idx < (int) resources.size(); idx++) { + result += + "index = " + intToStr(idx) + " " + resources[idx].toString() + + "\n"; + } + + result += "StoreCount = " + intToStr(store.size()) + "\n"; + for (int idx = 0; idx < (int) store.size(); idx++) { + result += + "index = " + intToStr(idx) + " " + store[idx].toString() + "\n"; + } + + result += "Allies = " + intToStr(allies.size()) + "\n"; + for (int idx = 0; idx < (int) allies.size(); idx++) { + result += + "index = " + intToStr(idx) + " name: " + + allies[idx]->factionType->getName(false) + " factionindex = " + + intToStr(allies[idx]->index) + "\n"; + } + + result += "Units = " + intToStr(units.size()) + "\n"; + for (int idx = 0; idx < (int) units.size(); idx++) { + result += units[idx]->toString(crcMode) + "\n"; + } + + return result; + } + + void Faction::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *factionNode = rootNode->addChild("Faction"); + + upgradeManager.saveGame(factionNode); + for (unsigned int i = 0; i < resources.size(); ++i) { + Resource & resource = resources[i]; + resource.saveGame(factionNode); + } + XmlNode *storeNode = factionNode->addChild("Store"); + for (unsigned int i = 0; i < store.size(); ++i) { + Resource & resource = store[i]; + resource.saveGame(storeNode); + } + + for (unsigned int i = 0; i < allies.size(); ++i) { + Faction *ally = allies[i]; + XmlNode *allyNode = factionNode->addChild("Ally"); + allyNode->addAttribute("allyFactionIndex", + intToStr(ally->getIndex()), + mapTagReplacements); + } + for (unsigned int i = 0; i < units.size(); ++i) { + Unit *unit = units[i]; + unit->saveGame(factionNode); + } + + factionNode->addAttribute("control", intToStr(control), + mapTagReplacements); + + factionNode->addAttribute("overridePersonalityType", + intToStr(overridePersonalityType), + mapTagReplacements); + factionNode->addAttribute("factiontype", factionType->getName(false), + mapTagReplacements); + factionNode->addAttribute("index", intToStr(index), + mapTagReplacements); + factionNode->addAttribute("teamIndex", intToStr(teamIndex), + mapTagReplacements); + factionNode->addAttribute("startLocationIndex", + intToStr(startLocationIndex), + mapTagReplacements); + factionNode->addAttribute("thisFaction", intToStr(thisFaction), + mapTagReplacements); + + for (std::map < Vec2i, int >::iterator iterMap = + cacheResourceTargetList.begin(); + iterMap != cacheResourceTargetList.end(); ++iterMap) { + XmlNode *cacheResourceTargetListNode = + factionNode->addChild("cacheResourceTargetList"); + + cacheResourceTargetListNode->addAttribute("key", + iterMap->first. + getString(), + mapTagReplacements); + cacheResourceTargetListNode->addAttribute("value", + intToStr(iterMap->second), + mapTagReplacements); + } + + for (std::map < Vec2i, bool >::iterator iterMap = + cachedCloseResourceTargetLookupList.begin(); + iterMap != cachedCloseResourceTargetLookupList.end(); ++iterMap) { + XmlNode *cachedCloseResourceTargetLookupListNode = + factionNode->addChild("cachedCloseResourceTargetLookupList"); + + cachedCloseResourceTargetLookupListNode->addAttribute("key", + iterMap->first. + getString(), + mapTagReplacements); + cachedCloseResourceTargetLookupListNode->addAttribute("value", + intToStr + (iterMap-> + second), + mapTagReplacements); + } + + factionNode->addAttribute("random", intToStr(random.getLastNumber()), + mapTagReplacements); + factionNode->addAttribute("currentSwitchTeamVoteFactionIndex", + intToStr(currentSwitchTeamVoteFactionIndex), + mapTagReplacements); + factionNode->addAttribute("allowSharedTeamUnits", + intToStr(allowSharedTeamUnits), + mapTagReplacements); + + for (std::set < const UnitType * >::iterator iterMap = + lockedUnits.begin(); iterMap != lockedUnits.end(); ++iterMap) { + XmlNode *lockedUnitsListNode = + factionNode->addChild("lockedUnitList"); + const UnitType *ut = *iterMap; + + lockedUnitsListNode->addAttribute("value", ut->getName(false), + mapTagReplacements); + } + + for (std::map < int, int >::iterator iterMap = unitsMovingList.begin(); + iterMap != unitsMovingList.end(); ++iterMap) { + XmlNode *unitsMovingListNode = + factionNode->addChild("unitsMovingList"); + + unitsMovingListNode->addAttribute("key", intToStr(iterMap->first), + mapTagReplacements); + unitsMovingListNode->addAttribute("value", + intToStr(iterMap->second), + mapTagReplacements); + } + + for (std::map < int, int >::iterator iterMap = + unitsPathfindingList.begin(); + iterMap != unitsPathfindingList.end(); ++iterMap) { + XmlNode *unitsPathfindingListNode = + factionNode->addChild("unitsPathfindingList"); + + unitsPathfindingListNode->addAttribute("key", + intToStr(iterMap->first), + mapTagReplacements); + unitsPathfindingListNode->addAttribute("value", + intToStr(iterMap->second), + mapTagReplacements); + } + } + + void Faction::loadGame(const XmlNode * rootNode, int factionIndex, + GameSettings * settings, World * world) { + XmlNode *factionNode = NULL; + vector < XmlNode * >factionNodeList = + rootNode->getChildList("Faction"); + for (unsigned int i = 0; i < factionNodeList.size(); ++i) { + XmlNode *node = factionNodeList[i]; + if (node->getAttribute("index")->getIntValue() == factionIndex) { + factionNode = node; + break; + } + } + + if (factionNode != NULL) { + + allies.clear(); + vector < XmlNode * >allyNodeList = factionNode->getChildList("Ally"); + for (unsigned int i = 0; i < allyNodeList.size(); ++i) { + XmlNode *allyNode = allyNodeList[i]; + + int allyFactionIndex = + allyNode->getAttribute("allyFactionIndex")->getIntValue(); + allies.push_back(world->getFaction(allyFactionIndex)); + } + + vector < XmlNode * >unitNodeList = factionNode->getChildList("Unit"); + for (unsigned int i = 0; i < unitNodeList.size(); ++i) { + XmlNode *unitNode = unitNodeList[i]; + Unit *unit = Unit::loadGame(unitNode, settings, this, world); + this->addUnit(unit); + } + + for (unsigned int i = 0; i < resources.size(); ++i) { + Resource & resource = resources[i]; + resource.loadGame(factionNode, i, techTree); + } + XmlNode *storeNode = factionNode->getChild("Store"); + for (unsigned int i = 0; i < store.size(); ++i) { + Resource & resource = store[i]; + resource.loadGame(storeNode, i, techTree); + } + + upgradeManager.loadGame(factionNode, this); + + control = + static_cast + (factionNode->getAttribute("control")->getIntValue()); + + if (factionNode->hasAttribute("overridePersonalityType") == true) { + overridePersonalityType = + static_cast + (factionNode->getAttribute("overridePersonalityType")-> + getIntValue()); + } + + teamIndex = factionNode->getAttribute("teamIndex")->getIntValue(); + + startLocationIndex = + factionNode->getAttribute("startLocationIndex")->getIntValue(); + + thisFaction = + factionNode->getAttribute("thisFaction")->getIntValue() != 0; + + if (factionNode->hasAttribute("allowSharedTeamUnits") == true) { + allowSharedTeamUnits = + factionNode->getAttribute("allowSharedTeamUnits")-> + getIntValue() != 0; + } + + vector < XmlNode * >cacheResourceTargetListNodeList = + factionNode->getChildList("cacheResourceTargetList"); + for (unsigned int i = 0; i < cacheResourceTargetListNodeList.size(); + ++i) { + XmlNode *cacheResourceTargetListNode = + cacheResourceTargetListNodeList[i]; + + Vec2i vec = + Vec2i::strToVec2(cacheResourceTargetListNode-> + getAttribute("key")->getValue()); + cacheResourceTargetList[vec] = + cacheResourceTargetListNode->getAttribute("value")-> + getIntValue(); + } + vector < XmlNode * >cachedCloseResourceTargetLookupListNodeList = + factionNode->getChildList("cachedCloseResourceTargetLookupList"); + for (unsigned int i = 0; + i < cachedCloseResourceTargetLookupListNodeList.size(); ++i) { + XmlNode *cachedCloseResourceTargetLookupListNode = + cachedCloseResourceTargetLookupListNodeList[i]; + + Vec2i vec = + Vec2i::strToVec2(cachedCloseResourceTargetLookupListNode-> + getAttribute("key")->getValue()); + cachedCloseResourceTargetLookupList[vec] = + cachedCloseResourceTargetLookupListNode->getAttribute("value")-> + getIntValue() != 0; + } + + random.setLastNumber(factionNode->getAttribute("random")-> + getIntValue()); + + vector < XmlNode * >lockedUnitsListNodeList = + factionNode->getChildList("lockedUnitList"); + for (unsigned int i = 0; i < lockedUnitsListNodeList.size(); ++i) { + XmlNode *lockedUnitsListNode = lockedUnitsListNodeList[i]; + + string unitName = + lockedUnitsListNode->getAttribute("value")->getValue(); + lockedUnits.insert(getType()->getUnitType(unitName)); + } + + vector < XmlNode * >unitsMovingListNodeList = + factionNode->getChildList("unitsMovingList"); + for (unsigned int i = 0; i < unitsMovingListNodeList.size(); ++i) { + XmlNode *unitsMovingListNode = unitsMovingListNodeList[i]; + + int unitId = + unitsMovingListNode->getAttribute("key")->getIntValue(); + unitsMovingList[unitId] = + unitsMovingListNode->getAttribute("value")->getIntValue(); + } + vector < XmlNode * >unitsPathfindingListNodeList = + factionNode->getChildList("unitsPathfindingList"); + for (unsigned int i = 0; i < unitsPathfindingListNodeList.size(); + ++i) { + XmlNode *unitsPathfindingListNode = unitsPathfindingListNodeList[i]; + + int unitId = + unitsPathfindingListNode->getAttribute("key")->getIntValue(); + unitsPathfindingList[unitId] = + unitsPathfindingListNode->getAttribute("value")->getIntValue(); + } + } + } + + Checksum Faction::getCRC() { + const bool consoleDebug = false; + + Checksum crcForFaction; + + // UpgradeManager upgradeManager; + + for (unsigned int i = 0; i < resources.size(); ++i) { + Resource & resource = resources[i]; + //crcForFaction.addSum(resource.getCRC().getSum()); + uint32 crc = resource.getCRC().getSum(); + crcForFaction.addBytes(&crc, sizeof(uint32)); + } + + if (consoleDebug) { + if (getWorld()->getFrameCount() % 40 == 0) { + printf("#1 Frame #: %d Faction: %d CRC: %u\n", + getWorld()->getFrameCount(), index, + crcForFaction.getSum()); + } + } + + for (unsigned int i = 0; i < store.size(); ++i) { + Resource & resource = store[i]; + //crcForFaction.addSum(resource.getCRC().getSum()); + uint32 crc = resource.getCRC().getSum(); + crcForFaction.addBytes(&crc, sizeof(uint32)); + } + + if (consoleDebug) { + if (getWorld()->getFrameCount() % 40 == 0) { + printf("#2 Frame #: %d Faction: %d CRC: %u\n", + getWorld()->getFrameCount(), index, + crcForFaction.getSum()); + } + } + + for (unsigned int i = 0; i < units.size(); ++i) { + Unit *unit = units[i]; + //crcForFaction.addSum(unit->getCRC().getSum()); + uint32 crc = unit->getCRC().getSum(); + crcForFaction.addBytes(&crc, sizeof(uint32)); + } + + if (consoleDebug) { + if (getWorld()->getFrameCount() % 40 == 0) { + printf("#3 Frame #: %d Faction: %d CRC: %u\n", + getWorld()->getFrameCount(), index, + crcForFaction.getSum()); + } + } + + return crcForFaction; + } + + void Faction::addCRC_DetailsForWorldFrame(int worldFrameCount, + bool isNetworkServer) { + unsigned int MAX_FRAME_CACHE = 250; + if (isNetworkServer == true) { + MAX_FRAME_CACHE += 250; + } + crcWorldFrameDetails[worldFrameCount] = this->toString(true); + //if(worldFrameCount <= 0) printf("Adding world frame: %d log entries: %lld\n",worldFrameCount,(long long int)crcWorldFrameDetails.size()); + + for (unsigned int i = 0; i < units.size(); ++i) { + Unit *unit = units[i]; + + unit->getRandom()->clearLastCaller(); + unit->clearNetworkCRCDecHpList(); + unit->clearParticleInfo(); + } + + if ((unsigned int) crcWorldFrameDetails.size() > MAX_FRAME_CACHE) { + //printf("===> Removing older world frame log entries: %lld\n",(long long int)crcWorldFrameDetails.size()); + + for (; + (unsigned int) crcWorldFrameDetails.size() - MAX_FRAME_CACHE > + 0;) { + crcWorldFrameDetails.erase(crcWorldFrameDetails.begin()); + } + } + } + + string Faction::getCRC_DetailsForWorldFrame(int worldFrameCount) { + if (crcWorldFrameDetails.empty()) { + return ""; + } + return crcWorldFrameDetails[worldFrameCount]; + } + + std::pair < int, + string > + Faction::getCRC_DetailsForWorldFrameIndex(int worldFrameIndex) const { + if (crcWorldFrameDetails.empty()) { + return make_pair < int, string >(0, ""); + } + std::map < int, string >::const_iterator iterMap = + crcWorldFrameDetails.begin(); + std::advance(iterMap, worldFrameIndex); + if (iterMap == crcWorldFrameDetails.end()) { + return make_pair < int, string >(0, ""); + } + return std::pair < int, string >(iterMap->first, iterMap->second); + } + + string Faction::getCRC_DetailsForWorldFrames() const { + string result = ""; + for (std::map < int, string >::const_iterator iterMap = + crcWorldFrameDetails.begin(); + iterMap != crcWorldFrameDetails.end(); ++iterMap) { + result += + string + ("============================================================================\n"); + result += + string("** world frame: ") + intToStr(iterMap->first) + + string(" detail: ") + iterMap->second; + } + return result; + } + + uint64 Faction::getCRC_DetailsForWorldFrameCount() const { + return crcWorldFrameDetails.size(); + } + + } } //end namespace diff --git a/source/glest_game/type_instances/faction.h b/source/glest_game/type_instances/faction.h index 46b29f0a1..a1e75a5f9 100644 --- a/source/glest_game/type_instances/faction.h +++ b/source/glest_game/type_instances/faction.h @@ -36,511 +36,458 @@ using std::set; using Shared::Graphics::Texture2D; using namespace Shared::PlatformCommon; -namespace Glest -{ - namespace Game - { - - class Unit; - class TechTree; - class FactionType; - class ProducibleType; - class RequirableType; - class CommandType; - class UnitType; - class Game; - class ScriptManager; - class World; - class Faction; - class GameSettings; - class SurfaceCell; - - class FowAlphaCellsLookupItem - { - public: - - std::map < Vec2i, float >surfPosAlphaList; - }; - - class ExploredCellsLookupItem - { - public: - - ExploredCellsLookupItem () - { - ExploredCellsLookupItemCacheTimerCountIndex = 0; - } - int ExploredCellsLookupItemCacheTimerCountIndex; - std::vector < SurfaceCell * >exploredCellList; - std::vector < SurfaceCell * >visibleCellList; - - static time_t lastDebug; - }; - -// ===================================================== -// class Faction -// -/// Each of the game players -// ===================================================== - - struct CommandGroupUnitSorter - { - bool operator () (const Unit * l, const Unit * r); - bool compare (const Unit * l, const Unit * r); - }; - - struct CommandGroupUnitSorterId - { - Faction *faction; - bool operator () (const int l, const int r); - }; - - class FactionThread:public BaseThread, - public SlaveThreadControllerInterface - { - protected: - - Faction * faction; - Semaphore semTaskSignalled; - Mutex *triggerIdMutex; - std::pair < int, bool > frameIndex; - MasterSlaveThreadController *masterController; - - virtual void setQuitStatus (bool value); - virtual void setTaskCompleted (int frameIndex); - virtual bool canShutdown (bool deleteSelfIfShutdownDelayed = false); - - public: - explicit FactionThread (Faction * faction); - virtual ~ FactionThread (); - virtual void execute (); - - virtual void setMasterController (MasterSlaveThreadController * master) - { - masterController = master; - } - virtual void signalSlave (void *userdata) - { - signalPathfinder (*((int *) (userdata))); - } - - void signalPathfinder (int frameIndex); - bool isSignalPathfinderCompleted (int frameIndex); - }; - - class SwitchTeamVote - { - public: - - int factionIndex; - int oldTeam; - int newTeam; - bool voted; - bool allowSwitchTeam; - }; - - class Faction - { - private: - typedef vector < Resource > Resources; - typedef vector < Resource > Store; - typedef vector < Faction * >Allies; - typedef vector < Unit * >Units; - typedef map < int, Unit * >UnitMap; - - private: - UpgradeManager upgradeManager; - - Resources resources; - Store store; - Allies allies; - - Mutex *unitsMutex; - Units units; - UnitMap unitMap; - World *world; - ScriptManager *scriptManager; - - FactionPersonalityType overridePersonalityType; - ControlType control; - - Texture2D *texture; - FactionType *factionType; - - int index; - int teamIndex; - int startLocationIndex; - - bool thisFaction; - - bool factionDisconnectHandled; - - bool cachingDisabled; - std::map < Vec2i, int >cacheResourceTargetList; - std::map < Vec2i, bool > cachedCloseResourceTargetLookupList; - - RandomGen random; - FactionThread *workerThread; - - std::map < int, SwitchTeamVote > switchTeamVotes; - int currentSwitchTeamVoteFactionIndex; - - bool allowSharedTeamUnits; - set < int >livingUnits; - set < Unit * >livingUnitsp; - - std::map < int, int >unitsMovingList; - std::map < int, int >unitsPathfindingList; - - std::set < const UnitType *>lockedUnits; - - TechTree *techTree; - const XmlNode *loadWorldNode; - - std::vector < string > worldSynchThreadedLogList; - - std::map < int, string > crcWorldFrameDetails; - - std::map < int, const Unit *>aliveUnitListCache; - std::map < int, const Unit *>mobileUnitListCache; - std::map < int, const Unit *>beingBuiltUnitListCache; - - std::map < std::string, bool > resourceTypeCostCache; - - public: - Faction (); - ~Faction (); - - Faction (const Faction & obj) - { - init (); - throw megaglest_runtime_error ("class Faction is NOT safe to copy!"); - } - Faction & operator= (const Faction & obj) - { - init (); - throw - megaglest_runtime_error ("class Faction is NOT safe to assign!"); - } - - void notifyUnitAliveStatusChange (const Unit * unit); - void notifyUnitTypeChange (const Unit * unit, const UnitType * newType); - void notifyUnitSkillTypeChange (const Unit * unit, - const SkillType * newType); - bool hasAliveUnits (bool filterMobileUnits, - bool filterBuiltUnits) const; - - inline void addWorldSynchThreadedLogList (const string & data) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - worldSynchThreadedLogList.push_back (data); - } - } - inline void clearWorldSynchThreadedLogList () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - worldSynchThreadedLogList.clear (); - } - } - inline void dumpWorldSynchThreadedLogList () - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugWorldSynch). - enabled == true) - { - if (worldSynchThreadedLogList.empty () == false) - { - for (unsigned int index = 0; - index < worldSynchThreadedLogList.size (); ++index) - { - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, - worldSynchThreadedLogList[index]. - c_str ()); - } - worldSynchThreadedLogList.clear (); - } - } - } - - inline void addLivingUnits (int id) - { - livingUnits.insert (id); - } - inline void addLivingUnitsp (Unit * unit) - { - livingUnitsp.insert (unit); - } - - inline bool isUnitInLivingUnitsp (Unit * unit) - { - return (livingUnitsp.find (unit) != livingUnitsp.end ()); - } - inline void deleteLivingUnits (int id) - { - livingUnits.erase (id); - } - inline void deleteLivingUnitsp (Unit * unit) - { - livingUnitsp.erase (unit); - } - - //std::map unitsMovingList; - void addUnitToMovingList (int unitId); - void removeUnitFromMovingList (int unitId); - //int getUnitMovingListCount(); - - void addUnitToPathfindingList (int unitId); - //void removeUnitFromPathfindingList(int unitId); - int getUnitPathfindingListCount (); - void clearUnitsPathfinding (); - bool canUnitsPathfind (); - - void setLockedUnitForFaction (const UnitType * ut, bool lock); - bool isUnitLocked (const UnitType * ut) const - { - return lockedUnits.find (ut) != lockedUnits.end (); - } - - void init (FactionType * factionType, ControlType control, - TechTree * techTree, Game * game, int factionIndex, - int teamIndex, int startLocationIndex, bool thisFaction, - bool giveResources, const XmlNode * loadWorldNode = NULL); - void end (); - - inline bool getFactionDisconnectHandled () const - { - return factionDisconnectHandled; - } - void setFactionDisconnectHandled (bool value) - { - factionDisconnectHandled = value; - } - - //get - const Resource *getResource (const ResourceType * rt, - bool localFactionOnly = false) const; - inline const Resource *getResource (int i) const - { - return &resources[i]; - } - int getStoreAmount (const ResourceType * rt, bool localFactionOnly = - false) const; - inline const FactionType *getType () const - { - return factionType; - } - inline int getIndex () const - { - return index; - } - - inline int getTeam () const - { - return teamIndex; - } - void setTeam (int team) - { - teamIndex = team; - } - - inline TechTree *getTechTree () const - { - return techTree; - } - const SwitchTeamVote *getFirstSwitchTeamVote () const; - SwitchTeamVote *getSwitchTeamVote (int factionIndex); - void setSwitchTeamVote (SwitchTeamVote & vote); - inline int getCurrentSwitchTeamVoteFactionIndex () const - { - return currentSwitchTeamVoteFactionIndex; - } - void setCurrentSwitchTeamVoteFactionIndex (int index) - { - currentSwitchTeamVoteFactionIndex = index; - } - - bool getCpuControl (bool enableServerControlledAI, bool isNetworkGame, - NetworkRole role) const; - bool getCpuControl () const; - inline bool getCpuEasyControl () const - { - return control == ctCpuEasy; - } - inline bool getCpuUltraControl () const - { - return control == ctCpuUltra; - } - inline bool getCpuZetaControl () const - { - return control == ctCpuZeta; - } - inline ControlType getControlType () const - { - return control; - } - - FactionPersonalityType getPersonalityType () const; - void setPersonalityType (FactionPersonalityType pType) - { - overridePersonalityType = pType; - } - int getAIBehaviorStaticOverideValue (AIBehaviorStaticValueCategory type) - const; - - inline Unit *getUnit (int i) const - { - Unit *result = units[i]; - return result; - } - inline int getUnitCount () const - { - int result = (int) units.size (); - return result; - } - inline Mutex *getUnitMutex () - { - return unitsMutex; - } - - inline const UpgradeManager *getUpgradeManager () const - { - return &upgradeManager; - } - inline const Texture2D *getTexture () const - { - return texture; - } - inline int getStartLocationIndex () const - { - return startLocationIndex; - } - inline bool getThisFaction () const - { - return thisFaction; - } - - //upgrades - void startUpgrade (const UpgradeType * ut); - void cancelUpgrade (const UpgradeType * ut); - void finishUpgrade (const UpgradeType * ut); - - //cost application - bool applyCosts (const ProducibleType * p, const CommandType * ct); - void applyDiscount (const ProducibleType * p, int discount); - void applyStaticCosts (const ProducibleType * p, - const CommandType * ct); - void applyStaticProduction (const ProducibleType * p, - const CommandType * ct); - void deApplyCosts (const ProducibleType * p, const CommandType * ct); - void deApplyStaticCosts (const ProducibleType * p, - const CommandType * ct); - void deApplyStaticConsumption (const ProducibleType * p, - const CommandType * ct); - void applyCostsOnInterval (const ResourceType * rtApply); - bool checkCosts (const ProducibleType * pt, const CommandType * ct); - - //reqs - bool reqsOk (const RequirableType * rt) const; - bool reqsOk (const CommandType * ct) const; - int getCountForMaxUnitCount (const UnitType * unitType) const; - - //diplomacy - bool isAlly (const Faction * faction); - - //other - Unit *findUnit (int id) const; - void addUnit (Unit * unit); - void removeUnit (Unit * unit); - void addStore (const UnitType * unitType); - void removeStore (const UnitType * unitType); - - //resources - void incResourceAmount (const ResourceType * rt, int amount); - void setResourceBalance (const ResourceType * rt, int balance); - - void setControlType (ControlType value) - { - control = value; - } - - bool isResourceTargetInCache (const Vec2i & pos, - bool incrementUseCounter = false); - void addResourceTargetToCache (const Vec2i & pos, - bool incrementUseCounter = true); - void removeResourceTargetFromCache (const Vec2i & pos); - void addCloseResourceTargetToCache (const Vec2i & pos); - Vec2i getClosestResourceTypeTargetFromCache (Unit * unit, - const ResourceType * type, - int frameIndex); - Vec2i getClosestResourceTypeTargetFromCache (const Vec2i & pos, - const ResourceType * type); - void cleanupResourceTypeTargetCache (std::vector < Vec2i > - *deleteListPtr, int frameIndex); - inline int getCacheResourceTargetListSize () const - { - return (int) cacheResourceTargetList.size (); - } - -// Unit * findClosestUnitWithSkillClass(const Vec2i &pos,const CommandClass &cmdClass, -// const std::vector &skillClassList, -// const UnitType *unitType); - - void deletePixels (); - - inline World *getWorld () - { - return world; - } - int getFrameCount (); - - void signalWorkerThread (int frameIndex); - bool isWorkerThreadSignalCompleted (int frameIndex); - FactionThread *getWorkerThread () - { - return workerThread; - } - - void limitResourcesToStore (); - - void sortUnitsByCommandGroups (); - - bool canCreateUnit (const UnitType * ut, bool checkBuild, - bool checkProduce, bool checkMorph) const; - - string getCacheStats (); - uint64 getCacheKBytes (uint64 * cache1Size, uint64 * cache2Size, - uint64 * cache3Size, uint64 * cache4Size, - uint64 * cache5Size); - - std::string toString (bool crcMode = false) const; - - void saveGame (XmlNode * rootNode); - void loadGame (const XmlNode * rootNode, int factionIndex, - GameSettings * settings, World * world); - - void clearCaches (); - - Checksum getCRC (); - void addCRC_DetailsForWorldFrame (int worldFrameCount, - bool isNetworkServer); - string getCRC_DetailsForWorldFrame (int worldFrameCount); - std::pair < int, - string > getCRC_DetailsForWorldFrameIndex (int worldFrameIndex) const; - string getCRC_DetailsForWorldFrames () const; - uint64 getCRC_DetailsForWorldFrameCount () const; - - void updateUnitTypeWithResourceCostCache (const ResourceType * rt); - bool hasUnitTypeWithResourceCostInCache (const ResourceType * rt) const; - - private: - void init (); - void resetResourceAmount (const ResourceType * rt); - bool hasUnitTypeWithResouceCost (const ResourceType * rt); - }; - -}} //end namespace +namespace Glest { + namespace Game { + + class Unit; + class TechTree; + class FactionType; + class ProducibleType; + class RequirableType; + class CommandType; + class UnitType; + class Game; + class ScriptManager; + class World; + class Faction; + class GameSettings; + class SurfaceCell; + + class FowAlphaCellsLookupItem { + public: + + std::map < Vec2i, float >surfPosAlphaList; + }; + + class ExploredCellsLookupItem { + public: + + ExploredCellsLookupItem() { + ExploredCellsLookupItemCacheTimerCountIndex = 0; + } + int ExploredCellsLookupItemCacheTimerCountIndex; + std::vector < SurfaceCell * >exploredCellList; + std::vector < SurfaceCell * >visibleCellList; + + static time_t lastDebug; + }; + + // ===================================================== + // class Faction + // + /// Each of the game players + // ===================================================== + + struct CommandGroupUnitSorter { + bool operator () (const Unit * l, const Unit * r); + bool compare(const Unit * l, const Unit * r); + }; + + struct CommandGroupUnitSorterId { + Faction *faction; + bool operator () (const int l, const int r); + }; + + class FactionThread :public BaseThread, + public SlaveThreadControllerInterface { + protected: + + Faction * faction; + Semaphore semTaskSignalled; + Mutex *triggerIdMutex; + std::pair < int, bool > frameIndex; + MasterSlaveThreadController *masterController; + + virtual void setQuitStatus(bool value); + virtual void setTaskCompleted(int frameIndex); + virtual bool canShutdown(bool deleteSelfIfShutdownDelayed = false); + + public: + explicit FactionThread(Faction * faction); + virtual ~FactionThread(); + virtual void execute(); + + virtual void setMasterController(MasterSlaveThreadController * master) { + masterController = master; + } + virtual void signalSlave(void *userdata) { + signalPathfinder(*((int *) (userdata))); + } + + void signalPathfinder(int frameIndex); + bool isSignalPathfinderCompleted(int frameIndex); + }; + + class SwitchTeamVote { + public: + + int factionIndex; + int oldTeam; + int newTeam; + bool voted; + bool allowSwitchTeam; + }; + + class Faction { + private: + typedef vector < Resource > Resources; + typedef vector < Resource > Store; + typedef vector < Faction * >Allies; + typedef vector < Unit * >Units; + typedef map < int, Unit * >UnitMap; + + private: + UpgradeManager upgradeManager; + + Resources resources; + Store store; + Allies allies; + + Mutex *unitsMutex; + Units units; + UnitMap unitMap; + World *world; + ScriptManager *scriptManager; + + FactionPersonalityType overridePersonalityType; + ControlType control; + + Texture2D *texture; + FactionType *factionType; + + int index; + int teamIndex; + int startLocationIndex; + + bool thisFaction; + + bool factionDisconnectHandled; + + bool cachingDisabled; + std::map < Vec2i, int >cacheResourceTargetList; + std::map < Vec2i, bool > cachedCloseResourceTargetLookupList; + + RandomGen random; + FactionThread *workerThread; + + std::map < int, SwitchTeamVote > switchTeamVotes; + int currentSwitchTeamVoteFactionIndex; + + bool allowSharedTeamUnits; + set < int >livingUnits; + set < Unit * >livingUnitsp; + + std::map < int, int >unitsMovingList; + std::map < int, int >unitsPathfindingList; + + std::set < const UnitType *>lockedUnits; + + TechTree *techTree; + const XmlNode *loadWorldNode; + + std::vector < string > worldSynchThreadedLogList; + + std::map < int, string > crcWorldFrameDetails; + + std::map < int, const Unit *>aliveUnitListCache; + std::map < int, const Unit *>mobileUnitListCache; + std::map < int, const Unit *>beingBuiltUnitListCache; + + std::map < std::string, bool > resourceTypeCostCache; + + public: + Faction(); + ~Faction(); + + Faction(const Faction & obj) { + init(); + throw megaglest_runtime_error("class Faction is NOT safe to copy!"); + } + Faction & operator= (const Faction & obj) { + init(); + throw + megaglest_runtime_error("class Faction is NOT safe to assign!"); + } + + void notifyUnitAliveStatusChange(const Unit * unit); + void notifyUnitTypeChange(const Unit * unit, const UnitType * newType); + void notifyUnitSkillTypeChange(const Unit * unit, + const SkillType * newType); + bool hasAliveUnits(bool filterMobileUnits, + bool filterBuiltUnits) const; + + inline void addWorldSynchThreadedLogList(const string & data) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + worldSynchThreadedLogList.push_back(data); + } + } + inline void clearWorldSynchThreadedLogList() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + worldSynchThreadedLogList.clear(); + } + } + inline void dumpWorldSynchThreadedLogList() { + if (SystemFlags::getSystemSettingType(SystemFlags::debugWorldSynch). + enabled == true) { + if (worldSynchThreadedLogList.empty() == false) { + for (unsigned int index = 0; + index < worldSynchThreadedLogList.size(); ++index) { + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, + worldSynchThreadedLogList[index]. + c_str()); + } + worldSynchThreadedLogList.clear(); + } + } + } + + inline void addLivingUnits(int id) { + livingUnits.insert(id); + } + inline void addLivingUnitsp(Unit * unit) { + livingUnitsp.insert(unit); + } + + inline bool isUnitInLivingUnitsp(Unit * unit) { + return (livingUnitsp.find(unit) != livingUnitsp.end()); + } + inline void deleteLivingUnits(int id) { + livingUnits.erase(id); + } + inline void deleteLivingUnitsp(Unit * unit) { + livingUnitsp.erase(unit); + } + + //std::map unitsMovingList; + void addUnitToMovingList(int unitId); + void removeUnitFromMovingList(int unitId); + //int getUnitMovingListCount(); + + void addUnitToPathfindingList(int unitId); + //void removeUnitFromPathfindingList(int unitId); + int getUnitPathfindingListCount(); + void clearUnitsPathfinding(); + bool canUnitsPathfind(); + + void setLockedUnitForFaction(const UnitType * ut, bool lock); + bool isUnitLocked(const UnitType * ut) const { + return lockedUnits.find(ut) != lockedUnits.end(); + } + + void init(FactionType * factionType, ControlType control, + TechTree * techTree, Game * game, int factionIndex, + int teamIndex, int startLocationIndex, bool thisFaction, + bool giveResources, const XmlNode * loadWorldNode = NULL); + void end(); + + inline bool getFactionDisconnectHandled() const { + return factionDisconnectHandled; + } + void setFactionDisconnectHandled(bool value) { + factionDisconnectHandled = value; + } + + //get + const Resource *getResource(const ResourceType * rt, + bool localFactionOnly = false) const; + inline const Resource *getResource(int i) const { + return &resources[i]; + } + int getStoreAmount(const ResourceType * rt, bool localFactionOnly = + false) const; + inline const FactionType *getType() const { + return factionType; + } + inline int getIndex() const { + return index; + } + + inline int getTeam() const { + return teamIndex; + } + void setTeam(int team) { + teamIndex = team; + } + + inline TechTree *getTechTree() const { + return techTree; + } + const SwitchTeamVote *getFirstSwitchTeamVote() const; + SwitchTeamVote *getSwitchTeamVote(int factionIndex); + void setSwitchTeamVote(SwitchTeamVote & vote); + inline int getCurrentSwitchTeamVoteFactionIndex() const { + return currentSwitchTeamVoteFactionIndex; + } + void setCurrentSwitchTeamVoteFactionIndex(int index) { + currentSwitchTeamVoteFactionIndex = index; + } + + bool getCpuControl(bool enableServerControlledAI, bool isNetworkGame, + NetworkRole role) const; + bool getCpuControl() const; + inline bool getCpuEasyControl() const { + return control == ctCpuEasy; + } + inline bool getCpuUltraControl() const { + return control == ctCpuUltra; + } + inline bool getCpuZetaControl() const { + return control == ctCpuZeta; + } + inline ControlType getControlType() const { + return control; + } + + FactionPersonalityType getPersonalityType() const; + void setPersonalityType(FactionPersonalityType pType) { + overridePersonalityType = pType; + } + int getAIBehaviorStaticOverideValue(AIBehaviorStaticValueCategory type) + const; + + inline Unit *getUnit(int i) const { + Unit *result = units[i]; + return result; + } + inline int getUnitCount() const { + int result = (int) units.size(); + return result; + } + inline Mutex *getUnitMutex() { + return unitsMutex; + } + + inline const UpgradeManager *getUpgradeManager() const { + return &upgradeManager; + } + inline const Texture2D *getTexture() const { + return texture; + } + inline int getStartLocationIndex() const { + return startLocationIndex; + } + inline bool getThisFaction() const { + return thisFaction; + } + + //upgrades + void startUpgrade(const UpgradeType * ut); + void cancelUpgrade(const UpgradeType * ut); + void finishUpgrade(const UpgradeType * ut); + + //cost application + bool applyCosts(const ProducibleType * p, const CommandType * ct); + void applyDiscount(const ProducibleType * p, int discount); + void applyStaticCosts(const ProducibleType * p, + const CommandType * ct); + void applyStaticProduction(const ProducibleType * p, + const CommandType * ct); + void deApplyCosts(const ProducibleType * p, const CommandType * ct); + void deApplyStaticCosts(const ProducibleType * p, + const CommandType * ct); + void deApplyStaticConsumption(const ProducibleType * p, + const CommandType * ct); + void applyCostsOnInterval(const ResourceType * rtApply); + bool checkCosts(const ProducibleType * pt, const CommandType * ct); + + //reqs + bool reqsOk(const RequirableType * rt) const; + bool reqsOk(const CommandType * ct) const; + int getCountForMaxUnitCount(const UnitType * unitType) const; + + //diplomacy + bool isAlly(const Faction * faction); + + //other + Unit *findUnit(int id) const; + void addUnit(Unit * unit); + void removeUnit(Unit * unit); + void addStore(const UnitType * unitType); + void removeStore(const UnitType * unitType); + + //resources + void incResourceAmount(const ResourceType * rt, int amount); + void setResourceBalance(const ResourceType * rt, int balance); + + void setControlType(ControlType value) { + control = value; + } + + bool isResourceTargetInCache(const Vec2i & pos, + bool incrementUseCounter = false); + void addResourceTargetToCache(const Vec2i & pos, + bool incrementUseCounter = true); + void removeResourceTargetFromCache(const Vec2i & pos); + void addCloseResourceTargetToCache(const Vec2i & pos); + Vec2i getClosestResourceTypeTargetFromCache(Unit * unit, + const ResourceType * type, + int frameIndex); + Vec2i getClosestResourceTypeTargetFromCache(const Vec2i & pos, + const ResourceType * type); + void cleanupResourceTypeTargetCache(std::vector < Vec2i > + *deleteListPtr, int frameIndex); + inline int getCacheResourceTargetListSize() const { + return (int) cacheResourceTargetList.size(); + } + + // Unit * findClosestUnitWithSkillClass(const Vec2i &pos,const CommandClass &cmdClass, + // const std::vector &skillClassList, + // const UnitType *unitType); + + void deletePixels(); + + inline World *getWorld() { + return world; + } + int getFrameCount(); + + void signalWorkerThread(int frameIndex); + bool isWorkerThreadSignalCompleted(int frameIndex); + FactionThread *getWorkerThread() { + return workerThread; + } + + void limitResourcesToStore(); + + void sortUnitsByCommandGroups(); + + bool canCreateUnit(const UnitType * ut, bool checkBuild, + bool checkProduce, bool checkMorph) const; + + string getCacheStats(); + uint64 getCacheKBytes(uint64 * cache1Size, uint64 * cache2Size, + uint64 * cache3Size, uint64 * cache4Size, + uint64 * cache5Size); + + std::string toString(bool crcMode = false) const; + + void saveGame(XmlNode * rootNode); + void loadGame(const XmlNode * rootNode, int factionIndex, + GameSettings * settings, World * world); + + void clearCaches(); + + Checksum getCRC(); + void addCRC_DetailsForWorldFrame(int worldFrameCount, + bool isNetworkServer); + string getCRC_DetailsForWorldFrame(int worldFrameCount); + std::pair < int, + string > getCRC_DetailsForWorldFrameIndex(int worldFrameIndex) const; + string getCRC_DetailsForWorldFrames() const; + uint64 getCRC_DetailsForWorldFrameCount() const; + + void updateUnitTypeWithResourceCostCache(const ResourceType * rt); + bool hasUnitTypeWithResourceCostInCache(const ResourceType * rt) const; + + private: + void init(); + void resetResourceAmount(const ResourceType * rt); + bool hasUnitTypeWithResouceCost(const ResourceType * rt); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/type_instances/object.cpp b/source/glest_game/type_instances/object.cpp index 54c2f4d59..592d5d6e4 100644 --- a/source/glest_game/type_instances/object.cpp +++ b/source/glest_game/type_instances/object.cpp @@ -26,382 +26,324 @@ using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - ObjectStateInterface *Object::stateCallback = NULL; + ObjectStateInterface *Object::stateCallback = NULL; -// ===================================================== -// class Object -// ===================================================== + // ===================================================== + // class Object + // ===================================================== - Object::Object (ObjectType * objectType, const Vec3f & pos, - const Vec2i & mapPos):BaseColorPickEntity () - { - RandomGen random; + Object::Object(ObjectType * objectType, const Vec3f & pos, + const Vec2i & mapPos) :BaseColorPickEntity() { + RandomGen random; - random.init (static_cast < int >(pos.x * pos.z)); - this->lastRenderFrame = 0; - this->objectType = objectType; - resource = NULL; - highlight = 0.f; - animated = false; - this->mapPos = mapPos; - this->pos = - pos + Vec3f (random.randRange (-0.6f, 0.6f), 0.0f, - random.randRange (-0.6f, 0.6f)); - rotation = random.randRange (0.f, 360.f); - if (objectType != NULL) - { - variation = random.randRange (0, objectType->getModelCount () - 1); - TilesetModelType *tmt = objectType->getTilesetModelType (variation); - if (tmt->getRotationAllowed () != true) - { - rotation = 0; - } - if (tmt->getRandomPositionEnabled () != true) - { - this->pos = pos; - } - animated = tmt->getAnimSpeed () > 0; - } - else - { - variation = 0; - } - visible = false; - animProgress = 0.0f; - } + random.init(static_cast (pos.x * pos.z)); + this->lastRenderFrame = 0; + this->objectType = objectType; + resource = NULL; + highlight = 0.f; + animated = false; + this->mapPos = mapPos; + this->pos = + pos + Vec3f(random.randRange(-0.6f, 0.6f), 0.0f, + random.randRange(-0.6f, 0.6f)); + rotation = random.randRange(0.f, 360.f); + if (objectType != NULL) { + variation = random.randRange(0, objectType->getModelCount() - 1); + TilesetModelType *tmt = objectType->getTilesetModelType(variation); + if (tmt->getRotationAllowed() != true) { + rotation = 0; + } + if (tmt->getRandomPositionEnabled() != true) { + this->pos = pos; + } + animated = tmt->getAnimSpeed() > 0; + } else { + variation = 0; + } + visible = false; + animProgress = 0.0f; + } - Object::~Object () - { - Renderer & renderer = Renderer::getInstance (); - // fade(and by this remove) all unit particle systems - while (unitParticleSystems.empty () == false) - { - bool particleValid = - renderer.validateParticleSystemStillExists (unitParticleSystems. - back (), rsGame); - if (particleValid == true) - { - unitParticleSystems.back ()->fade (); - } - unitParticleSystems.pop_back (); - } - Renderer::getInstance ().removeParticleSystemsForParticleOwner (this, - rsGame); - renderer.removeObjectFromQuadCache (this); - if (stateCallback) - { - stateCallback->removingObjectEvent (this); - } - delete resource; - resource = NULL; + Object::~Object() { + Renderer & renderer = Renderer::getInstance(); + // fade(and by this remove) all unit particle systems + while (unitParticleSystems.empty() == false) { + bool particleValid = + renderer.validateParticleSystemStillExists(unitParticleSystems. + back(), rsGame); + if (particleValid == true) { + unitParticleSystems.back()->fade(); + } + unitParticleSystems.pop_back(); + } + Renderer::getInstance().removeParticleSystemsForParticleOwner(this, + rsGame); + renderer.removeObjectFromQuadCache(this); + if (stateCallback) { + stateCallback->removingObjectEvent(this); + } + delete resource; + resource = NULL; - } + } - void Object::end () - { - // set Objects to fading and remove them from list. - // its needed because otherwise they will be accessed from the destructor - while (unitParticleSystems.empty () == false) - { - bool particleValid = - Renderer::getInstance (). - validateParticleSystemStillExists (unitParticleSystems.back (), - rsGame); - if (particleValid == true) - { - unitParticleSystems.back ()->fade (); - } - unitParticleSystems.pop_back (); - } - } + void Object::end() { + // set Objects to fading and remove them from list. + // its needed because otherwise they will be accessed from the destructor + while (unitParticleSystems.empty() == false) { + bool particleValid = + Renderer::getInstance(). + validateParticleSystemStillExists(unitParticleSystems.back(), + rsGame); + if (particleValid == true) { + unitParticleSystems.back()->fade(); + } + unitParticleSystems.pop_back(); + } + } - void Object::initParticles () - { - if (this->objectType == NULL) - { - return; - } - if (this->objectType->getTilesetModelType (variation)->hasParticles ()) - { - ModelParticleSystemTypes *particleTypes = - this->objectType->getTilesetModelType (variation)-> - getParticleTypes (); - initParticlesFromTypes (particleTypes); - } - } + void Object::initParticles() { + if (this->objectType == NULL) { + return; + } + if (this->objectType->getTilesetModelType(variation)->hasParticles()) { + ModelParticleSystemTypes *particleTypes = + this->objectType->getTilesetModelType(variation)-> + getParticleTypes(); + initParticlesFromTypes(particleTypes); + } + } - void Object::initParticlesFromTypes (const ModelParticleSystemTypes * - particleTypes) - { - bool showTilesetParticles = - Config::getInstance ().getBool ("TilesetParticles", "true"); - if (showTilesetParticles == true - && GlobalStaticFlags::getIsNonGraphicalModeEnabled () == false - && particleTypes->empty () == false - && unitParticleSystems.empty () == true) - { - for (ObjectParticleSystemTypes::const_iterator it = - particleTypes->begin (); it != particleTypes->end (); ++it) - { - UnitParticleSystem *ups = new UnitParticleSystem (200); - ups->setParticleOwner (this); - ups->setParticleType ((*it)); - (*it)->setValues (ups); - ups->setPos (this->pos); - ups->setRotation (this->rotation); - ups->setFactionColor (Vec3f (0, 0, 0)); - ups->setVisible (false); - this->unitParticleSystems.push_back (ups); - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - } - } - } + void Object::initParticlesFromTypes(const ModelParticleSystemTypes * + particleTypes) { + bool showTilesetParticles = + Config::getInstance().getBool("TilesetParticles", "true"); + if (showTilesetParticles == true + && GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false + && particleTypes->empty() == false + && unitParticleSystems.empty() == true) { + for (ObjectParticleSystemTypes::const_iterator it = + particleTypes->begin(); it != particleTypes->end(); ++it) { + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType((*it)); + (*it)->setValues(ups); + ups->setPos(this->pos); + ups->setRotation(this->rotation); + ups->setFactionColor(Vec3f(0, 0, 0)); + ups->setVisible(false); + this->unitParticleSystems.push_back(ups); + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } + } + } - void Object::end (ParticleSystem * particleSystem) - { - vector < UnitParticleSystem * >::iterator iterFind = - find (unitParticleSystems.begin (), unitParticleSystems.end (), - particleSystem); - if (iterFind != unitParticleSystems.end ()) - { - unitParticleSystems.erase (iterFind); - } - } + void Object::end(ParticleSystem * particleSystem) { + vector < UnitParticleSystem * >::iterator iterFind = + find(unitParticleSystems.begin(), unitParticleSystems.end(), + particleSystem); + if (iterFind != unitParticleSystems.end()) { + unitParticleSystems.erase(iterFind); + } + } - void Object::setHeight (float height) - { - pos.y = height; + void Object::setHeight(float height) { + pos.y = height; - for (UnitParticleSystems::iterator it = unitParticleSystems.begin (); - it != unitParticleSystems.end (); ++it) - { - bool particleValid = - Renderer::getInstance ().validateParticleSystemStillExists ((*it), - rsGame); - if (particleValid == true) - { - (*it)->setPos (this->pos); - } - } - } + for (UnitParticleSystems::iterator it = unitParticleSystems.begin(); + it != unitParticleSystems.end(); ++it) { + bool particleValid = + Renderer::getInstance().validateParticleSystemStillExists((*it), + rsGame); + if (particleValid == true) { + (*it)->setPos(this->pos); + } + } + } - void Object::updateHighlight () - { - //highlight - if (highlight > 0.f) - { - //const Game *game = Renderer::getInstance().getGame(); - //highlight -= 1.f / (Game::highlightTime * game->getWorld()->getUpdateFps(-1)); - highlight -= 1.f / (Game::highlightTime * GameConstants::updateFps); - } - } + void Object::updateHighlight() { + //highlight + if (highlight > 0.f) { + //const Game *game = Renderer::getInstance().getGame(); + //highlight -= 1.f / (Game::highlightTime * game->getWorld()->getUpdateFps(-1)); + highlight -= 1.f / (Game::highlightTime * GameConstants::updateFps); + } + } - void Object::update () - { - //if(objectType != NULL && objectType->getTilesetModelType(variation) != NULL && - // objectType->getTilesetModelType(variation)->getAnimSpeed() != 0.0) { - if (animated == true) - { -// printf("#1 Object updating [%s] Speed [%d] animProgress [%f]\n",this->objectType->getTilesetModelType(variation)->getModel()->getFileName().c_str(),objectType->getTilesetModelType(variation)->getAnimSpeed(),animProgress); + void Object::update() { + //if(objectType != NULL && objectType->getTilesetModelType(variation) != NULL && + // objectType->getTilesetModelType(variation)->getAnimSpeed() != 0.0) { + if (animated == true) { + // printf("#1 Object updating [%s] Speed [%d] animProgress [%f]\n",this->objectType->getTilesetModelType(variation)->getModel()->getFileName().c_str(),objectType->getTilesetModelType(variation)->getAnimSpeed(),animProgress); - if (objectType != NULL - && objectType->getTilesetModelType (variation) != NULL) - { - float heightFactor = 1.f; - const float speedDivider = 100.f; - float speedDenominator = (speedDivider * GameConstants::updateFps); + if (objectType != NULL + && objectType->getTilesetModelType(variation) != NULL) { + float heightFactor = 1.f; + const float speedDivider = 100.f; + float speedDenominator = (speedDivider * GameConstants::updateFps); - // smooth TwoFrameanimations - float f = 1.0f; - if (objectType->getTilesetModelType (variation)-> - getSmoothTwoFrameAnim () == true) - { - f = abs (std::sin (animProgress * 2 * 3.16)) + 0.4f; - } + // smooth TwoFrameanimations + float f = 1.0f; + if (objectType->getTilesetModelType(variation)-> + getSmoothTwoFrameAnim() == true) { + f = abs(std::sin(animProgress * 2 * 3.16)) + 0.4f; + } - float newAnimProgress = - animProgress + - f * - (((float) objectType->getTilesetModelType (variation)-> - getAnimSpeed () * heightFactor) / speedDenominator); + float newAnimProgress = + animProgress + + f * + (((float) objectType->getTilesetModelType(variation)-> + getAnimSpeed() * heightFactor) / speedDenominator); - animProgress = newAnimProgress; - if (animProgress > 1.f) - { - animProgress = 0.f; - } - } - } - } + animProgress = newAnimProgress; + if (animProgress > 1.f) { + animProgress = 0.f; + } + } + } + } - void Object::resetHighlight () - { - highlight = 1.f; - } + void Object::resetHighlight() { + highlight = 1.f; + } - Model *Object::getModelPtr () const - { - Model *result = NULL; - if (objectType == NULL) - { - if (resource != NULL && resource->getType () != NULL) - { - result = resource->getType ()->getModel (); - } - } - else - { - result = objectType->getTilesetModelType (variation)->getModel (); - } - return result; - } + Model *Object::getModelPtr() const { + Model *result = NULL; + if (objectType == NULL) { + if (resource != NULL && resource->getType() != NULL) { + result = resource->getType()->getModel(); + } + } else { + result = objectType->getTilesetModelType(variation)->getModel(); + } + return result; + } - const Model *Object::getModel () const - { - Model *result = NULL; - if (objectType == NULL) - { - if (resource != NULL && resource->getType () != NULL) - { - result = resource->getType ()->getModel (); - } - } - else - { - result = objectType->getTilesetModelType (variation)->getModel (); - } - return result; - } + const Model *Object::getModel() const { + Model *result = NULL; + if (objectType == NULL) { + if (resource != NULL && resource->getType() != NULL) { + result = resource->getType()->getModel(); + } + } else { + result = objectType->getTilesetModelType(variation)->getModel(); + } + return result; + } - bool Object::getWalkable () const - { - return objectType == NULL ? false : objectType->getWalkable (); - } + bool Object::getWalkable() const { + return objectType == NULL ? false : objectType->getWalkable(); + } - void Object::setResource (const ResourceType * resourceType, - const Vec2i & pos) - { - delete resource; - resource = new Resource (); - resource->init (resourceType, pos); - initParticlesFromTypes (resourceType->getObjectParticleSystemTypes ()); - } + void Object::setResource(const ResourceType * resourceType, + const Vec2i & pos) { + delete resource; + resource = new Resource(); + resource->init(resourceType, pos); + initParticlesFromTypes(resourceType->getObjectParticleSystemTypes()); + } - void Object::setVisible (bool visible) - { - this->visible = visible; - for (UnitParticleSystems::iterator it = unitParticleSystems.begin (); - it != unitParticleSystems.end (); ++it) - { - bool particleValid = - Renderer::getInstance ().validateParticleSystemStillExists ((*it), - rsGame); - if (particleValid == true) - { - (*it)->setVisible (visible); - } - } - } + void Object::setVisible(bool visible) { + this->visible = visible; + for (UnitParticleSystems::iterator it = unitParticleSystems.begin(); + it != unitParticleSystems.end(); ++it) { + bool particleValid = + Renderer::getInstance().validateParticleSystemStillExists((*it), + rsGame); + if (particleValid == true) { + (*it)->setVisible(visible); + } + } + } - string Object::getUniquePickName () const - { - string result = ""; - if (resource != NULL) - { - result += resource->getDescription (false) + " : "; - } - result += mapPos.getString (); - return result; - } + string Object::getUniquePickName() const { + string result = ""; + if (resource != NULL) { + result += resource->getDescription(false) + " : "; + } + result += mapPos.getString(); + return result; + } - void Object::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *objectNode = rootNode->addChild ("Object"); + void Object::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *objectNode = rootNode->addChild("Object"); -// ObjectType *objectType; - if (objectType != NULL) - { - objectNode->addAttribute ("objectType", - intToStr (objectType->getClass ()), - mapTagReplacements); - } -// vector unitParticleSystems; - for (unsigned int i = 0; i < unitParticleSystems.size (); ++i) - { - UnitParticleSystem *ptr = unitParticleSystems[i]; - if (ptr != NULL) - { - ptr->saveGame (objectNode); - } - } -// Resource *resource; - if (resource != NULL) - { - resource->saveGame (objectNode); - } -// Vec3f pos; - objectNode->addAttribute ("pos", pos.getString (), mapTagReplacements); -// float rotation; - objectNode->addAttribute ("rotation", floatToStr (rotation, 6), - mapTagReplacements); -// int variation; - objectNode->addAttribute ("variation", intToStr (variation), - mapTagReplacements); -// int lastRenderFrame; - objectNode->addAttribute ("lastRenderFrame", intToStr (lastRenderFrame), - mapTagReplacements); -// Vec2i mapPos; - objectNode->addAttribute ("mapPos", mapPos.getString (), - mapTagReplacements); -// bool visible; - objectNode->addAttribute ("visible", intToStr (visible), - mapTagReplacements); - } + // ObjectType *objectType; + if (objectType != NULL) { + objectNode->addAttribute("objectType", + intToStr(objectType->getClass()), + mapTagReplacements); + } + // vector unitParticleSystems; + for (unsigned int i = 0; i < unitParticleSystems.size(); ++i) { + UnitParticleSystem *ptr = unitParticleSystems[i]; + if (ptr != NULL) { + ptr->saveGame(objectNode); + } + } + // Resource *resource; + if (resource != NULL) { + resource->saveGame(objectNode); + } + // Vec3f pos; + objectNode->addAttribute("pos", pos.getString(), mapTagReplacements); + // float rotation; + objectNode->addAttribute("rotation", floatToStr(rotation, 6), + mapTagReplacements); + // int variation; + objectNode->addAttribute("variation", intToStr(variation), + mapTagReplacements); + // int lastRenderFrame; + objectNode->addAttribute("lastRenderFrame", intToStr(lastRenderFrame), + mapTagReplacements); + // Vec2i mapPos; + objectNode->addAttribute("mapPos", mapPos.getString(), + mapTagReplacements); + // bool visible; + objectNode->addAttribute("visible", intToStr(visible), + mapTagReplacements); + } - void Object::loadGame (const XmlNode * rootNode, - const TechTree * techTree) - { - const XmlNode *objectNode = rootNode->getChild ("Object"); + void Object::loadGame(const XmlNode * rootNode, + const TechTree * techTree) { + const XmlNode *objectNode = rootNode->getChild("Object"); - //description = objectNode->getAttribute("description")->getValue(); + //description = objectNode->getAttribute("description")->getValue(); - // ObjectType *objectType; -// if(objectType != NULL) { -// objectNode->addAttribute("objectType",intToStr(objectType->getClass()), mapTagReplacements); -// } -// // vector unitParticleSystems; -// for(unsigned int i = 0; i < unitParticleSystems.size(); ++i) { -// UnitParticleSystem *ptr= unitParticleSystems[i]; -// if(ptr != NULL) { -// ptr->saveGame(objectNode); -// } -// } - // Resource *resource; - if (resource != NULL) - { - resource->loadGame (objectNode, 0, techTree); - } - // Vec3f pos; - pos = Vec3f::strToVec3 (objectNode->getAttribute ("pos")->getValue ()); - // float rotation; - rotation = objectNode->getAttribute ("rotation")->getFloatValue (); - // int variation; - variation = objectNode->getAttribute ("variation")->getIntValue (); - // int lastRenderFrame; - lastRenderFrame = - objectNode->getAttribute ("lastRenderFrame")->getIntValue (); - // Vec2i mapPos; - mapPos = - Vec2i::strToVec2 (objectNode->getAttribute ("mapPos")->getValue ()); - // bool visible; - visible = objectNode->getAttribute ("visible")->getIntValue () != 0; - } + // ObjectType *objectType; + // if(objectType != NULL) { + // objectNode->addAttribute("objectType",intToStr(objectType->getClass()), mapTagReplacements); + // } + // // vector unitParticleSystems; + // for(unsigned int i = 0; i < unitParticleSystems.size(); ++i) { + // UnitParticleSystem *ptr= unitParticleSystems[i]; + // if(ptr != NULL) { + // ptr->saveGame(objectNode); + // } + // } + // Resource *resource; + if (resource != NULL) { + resource->loadGame(objectNode, 0, techTree); + } + // Vec3f pos; + pos = Vec3f::strToVec3(objectNode->getAttribute("pos")->getValue()); + // float rotation; + rotation = objectNode->getAttribute("rotation")->getFloatValue(); + // int variation; + variation = objectNode->getAttribute("variation")->getIntValue(); + // int lastRenderFrame; + lastRenderFrame = + objectNode->getAttribute("lastRenderFrame")->getIntValue(); + // Vec2i mapPos; + mapPos = + Vec2i::strToVec2(objectNode->getAttribute("mapPos")->getValue()); + // bool visible; + visible = objectNode->getAttribute("visible")->getIntValue() != 0; + } - } + } } //end namespace diff --git a/source/glest_game/type_instances/object.h b/source/glest_game/type_instances/object.h index 6e6ed1da8..a2c19536a 100644 --- a/source/glest_game/type_instances/object.h +++ b/source/glest_game/type_instances/object.h @@ -23,150 +23,131 @@ # include "object_type.h" # include "tileset_model_type.h" -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class ObjectType; - class ResourceType; - class Resource; - class TechTree; + class ObjectType; + class ResourceType; + class Resource; + class TechTree; - using Shared::Graphics::Model; - using Shared::Graphics::Vec2i; - using Shared::Graphics::Vec3f; - using Shared::Graphics::UnitParticleSystem; + using Shared::Graphics::Model; + using Shared::Graphics::Vec2i; + using Shared::Graphics::Vec3f; + using Shared::Graphics::UnitParticleSystem; -// ===================================================== -// class Object -// -/// A map object: tree, stone... -// ===================================================== + // ===================================================== + // class Object + // + /// A map object: tree, stone... + // ===================================================== - class Object; + class Object; - class ObjectStateInterface - { - public: - virtual void removingObjectEvent (Object * object) = 0; - virtual ~ ObjectStateInterface () - { - } - }; + class ObjectStateInterface { + public: + virtual void removingObjectEvent(Object * object) = 0; + virtual ~ObjectStateInterface() { + } + }; - class Object:public BaseColorPickEntity, public ParticleOwner - { - private: - typedef vector < UnitParticleSystem * >UnitParticleSystems; + class Object :public BaseColorPickEntity, public ParticleOwner { + private: + typedef vector < UnitParticleSystem * >UnitParticleSystems; - private: - ObjectType * objectType; - vector < UnitParticleSystem * >unitParticleSystems; - Resource *resource; - Vec3f pos; - float rotation; - int variation; - int lastRenderFrame; - Vec2i mapPos; - bool visible; - bool animated; - float animProgress; - float highlight; + private: + ObjectType * objectType; + vector < UnitParticleSystem * >unitParticleSystems; + Resource *resource; + Vec3f pos; + float rotation; + int variation; + int lastRenderFrame; + Vec2i mapPos; + bool visible; + bool animated; + float animProgress; + float highlight; - static ObjectStateInterface *stateCallback; + static ObjectStateInterface *stateCallback; - public: - Object (ObjectType * objectType, const Vec3f & pos, - const Vec2i & mapPos); - virtual ~ Object (); + public: + Object(ObjectType * objectType, const Vec3f & pos, + const Vec2i & mapPos); + virtual ~Object(); - virtual void end (); //to kill particles - virtual void logParticleInfo (string info) - { - }; - void initParticles (); - void initParticlesFromTypes (const ModelParticleSystemTypes * - particleTypes); - static void setStateCallback (ObjectStateInterface * value) - { - stateCallback = value; - } + virtual void end(); //to kill particles + virtual void logParticleInfo(string info) { + }; + void initParticles(); + void initParticlesFromTypes(const ModelParticleSystemTypes * + particleTypes); + static void setStateCallback(ObjectStateInterface * value) { + stateCallback = value; + } - const ObjectType *getType () const - { - return objectType; - } - Resource *getResource () const - { - return resource; - } - Vec3f getPos () const - { - return pos; - } - bool isVisible () const - { - return visible; - } - const Vec3f & getConstPos () const - { - return pos; - } - float getRotation () const - { - return rotation; - } - const Model *getModel () const; - Model *getModelPtr () const; - bool getWalkable () const; - bool isAnimated () const - { - return animated; - } + const ObjectType *getType() const { + return objectType; + } + Resource *getResource() const { + return resource; + } + Vec3f getPos() const { + return pos; + } + bool isVisible() const { + return visible; + } + const Vec3f & getConstPos() const { + return pos; + } + float getRotation() const { + return rotation; + } + const Model *getModel() const; + Model *getModelPtr() const; + bool getWalkable() const; + bool isAnimated() const { + return animated; + } - float getHightlight () const - { - return highlight; - } - bool isHighlighted () const - { - return highlight > 0.f; - } - void resetHighlight (); + float getHightlight() const { + return highlight; + } + bool isHighlighted() const { + return highlight > 0.f; + } + void resetHighlight(); - void setResource (const ResourceType * resourceType, const Vec2i & pos); - void setHeight (float height); - void setVisible (bool visible); + void setResource(const ResourceType * resourceType, const Vec2i & pos); + void setHeight(float height); + void setVisible(bool visible); - int getLastRenderFrame () const - { - return lastRenderFrame; - } - void setLastRenderFrame (int value) - { - lastRenderFrame = value; - } + int getLastRenderFrame() const { + return lastRenderFrame; + } + void setLastRenderFrame(int value) { + lastRenderFrame = value; + } - const Vec2i & getMapPos () const - { - return mapPos; - } + const Vec2i & getMapPos() const { + return mapPos; + } - void updateHighlight (); - void update (); - float getAnimProgress () const - { - return animProgress; - } + void updateHighlight(); + void update(); + float getAnimProgress() const { + return animProgress; + } - virtual string getUniquePickName () const; - void saveGame (XmlNode * rootNode); - void loadGame (const XmlNode * rootNode, const TechTree * techTree); + virtual string getUniquePickName() const; + void saveGame(XmlNode * rootNode); + void loadGame(const XmlNode * rootNode, const TechTree * techTree); - virtual void end (ParticleSystem * particleSystem); - }; + virtual void end(ParticleSystem * particleSystem); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/type_instances/resource.cpp b/source/glest_game/type_instances/resource.cpp index 3a607a556..691caeadd 100644 --- a/source/glest_game/type_instances/resource.cpp +++ b/source/glest_game/type_instances/resource.cpp @@ -22,165 +22,148 @@ using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Resource -// ===================================================== + // ===================================================== + // class Resource + // ===================================================== - Resource::Resource () - { - this->type = NULL; - this->amount = 0; - pos = Vec2i (0); - balance = 0; + Resource::Resource() { + this->type = NULL; + this->amount = 0; + pos = Vec2i(0); + balance = 0; - addItemToVault (&this->amount, this->amount); - addItemToVault (&this->balance, this->balance); - } + addItemToVault(&this->amount, this->amount); + addItemToVault(&this->balance, this->balance); + } - void Resource::init (const ResourceType * rt, int amount) - { - this->type = rt; - this->amount = amount; - pos = Vec2i (0); - balance = 0; + void Resource::init(const ResourceType * rt, int amount) { + this->type = rt; + this->amount = amount; + pos = Vec2i(0); + balance = 0; - addItemToVault (&this->amount, this->amount); - addItemToVault (&this->balance, this->balance); - } + addItemToVault(&this->amount, this->amount); + addItemToVault(&this->balance, this->balance); + } - void Resource::init (const ResourceType * rt, const Vec2i & pos) - { - this->type = rt; - amount = rt->getDefResPerPatch (); - this->pos = pos; + void Resource::init(const ResourceType * rt, const Vec2i & pos) { + this->type = rt; + amount = rt->getDefResPerPatch(); + this->pos = pos; - addItemToVault (&this->amount, this->amount); - addItemToVault (&this->balance, this->balance); - } + addItemToVault(&this->amount, this->amount); + addItemToVault(&this->balance, this->balance); + } - string Resource::getDescription (bool translatedValue) const - { - string str; + string Resource::getDescription(bool translatedValue) const { + string str; - str += type->getName (translatedValue); - str += "\n"; - str += intToStr (amount); - str += "/"; - str += intToStr (type->getDefResPerPatch ()); + str += type->getName(translatedValue); + str += "\n"; + str += intToStr(amount); + str += "/"; + str += intToStr(type->getDefResPerPatch()); - return str; - } + return str; + } - int Resource::getAmount () const - { - checkItemInVault (&this->amount, this->amount); - return amount; - } + int Resource::getAmount() const { + checkItemInVault(&this->amount, this->amount); + return amount; + } - int Resource::getBalance () const - { - checkItemInVault (&this->balance, this->balance); - return balance; - } + int Resource::getBalance() const { + checkItemInVault(&this->balance, this->balance); + return balance; + } - void Resource::setAmount (int amount) - { - checkItemInVault (&this->amount, this->amount); - this->amount = amount; - addItemToVault (&this->amount, this->amount); - } + void Resource::setAmount(int amount) { + checkItemInVault(&this->amount, this->amount); + this->amount = amount; + addItemToVault(&this->amount, this->amount); + } - void Resource::setBalance (int balance) - { - checkItemInVault (&this->balance, this->balance); - this->balance = balance; - addItemToVault (&this->balance, this->balance); - } + void Resource::setBalance(int balance) { + checkItemInVault(&this->balance, this->balance); + this->balance = balance; + addItemToVault(&this->balance, this->balance); + } - bool Resource::decAmount (int i) - { - checkItemInVault (&this->amount, this->amount); - amount -= i; - addItemToVault (&this->amount, this->amount); + bool Resource::decAmount(int i) { + checkItemInVault(&this->amount, this->amount); + amount -= i; + addItemToVault(&this->amount, this->amount); - if (amount > 0) - { - return false; - } - return true; - } + if (amount > 0) { + return false; + } + return true; + } - void Resource::saveGame (XmlNode * rootNode) const - { - std::map < string, string > mapTagReplacements; - XmlNode *resourceNode = rootNode->addChild ("Resource"); + void Resource::saveGame(XmlNode * rootNode) const { + std::map < string, string > mapTagReplacements; + XmlNode *resourceNode = rootNode->addChild("Resource"); -// int amount; - resourceNode->addAttribute ("amount", intToStr (amount), - mapTagReplacements); -// const ResourceType *type; - resourceNode->addAttribute ("type", type->getName (), - mapTagReplacements); -// Vec2i pos; - resourceNode->addAttribute ("pos", pos.getString (), - mapTagReplacements); -// int balance; - resourceNode->addAttribute ("balance", intToStr (balance), - mapTagReplacements); - } + // int amount; + resourceNode->addAttribute("amount", intToStr(amount), + mapTagReplacements); + // const ResourceType *type; + resourceNode->addAttribute("type", type->getName(), + mapTagReplacements); + // Vec2i pos; + resourceNode->addAttribute("pos", pos.getString(), + mapTagReplacements); + // int balance; + resourceNode->addAttribute("balance", intToStr(balance), + mapTagReplacements); + } - void Resource::loadGame (const XmlNode * rootNode, int index, - const TechTree * techTree) - { - vector < XmlNode * >resourceNodeList = - rootNode->getChildList ("Resource"); + void Resource::loadGame(const XmlNode * rootNode, int index, + const TechTree * techTree) { + vector < XmlNode * >resourceNodeList = + rootNode->getChildList("Resource"); - if (index < (int) resourceNodeList.size ()) - { - XmlNode *resourceNode = resourceNodeList[index]; + if (index < (int) resourceNodeList.size()) { + XmlNode *resourceNode = resourceNodeList[index]; - amount = resourceNode->getAttribute ("amount")->getIntValue (); - type = - techTree->getResourceType (resourceNode->getAttribute ("type")-> - getValue ()); - pos = - Vec2i::strToVec2 (resourceNode->getAttribute ("pos")->getValue ()); - balance = resourceNode->getAttribute ("balance")->getIntValue (); - } - } + amount = resourceNode->getAttribute("amount")->getIntValue(); + type = + techTree->getResourceType(resourceNode->getAttribute("type")-> + getValue()); + pos = + Vec2i::strToVec2(resourceNode->getAttribute("pos")->getValue()); + balance = resourceNode->getAttribute("balance")->getIntValue(); + } + } - std::string Resource::toString ()const - { - std::string result = - "resource name = " + this->getDescription (false) + "\n"; - result += "amount = " + intToStr (this->amount) + "\n"; - result += "type = " + this->type->getName (false) + "\n"; - result += - "type resources per patch = " + - intToStr (type->getDefResPerPatch ()) + "\n"; - result += "pos = " + this->pos.getString () + "\n"; - result += "balance = " + intToStr (this->balance) + "\n"; + std::string Resource::toString()const { + std::string result = + "resource name = " + this->getDescription(false) + "\n"; + result += "amount = " + intToStr(this->amount) + "\n"; + result += "type = " + this->type->getName(false) + "\n"; + result += + "type resources per patch = " + + intToStr(type->getDefResPerPatch()) + "\n"; + result += "pos = " + this->pos.getString() + "\n"; + result += "balance = " + intToStr(this->balance) + "\n"; - return result; - } + return result; + } - Checksum Resource::getCRC () - { - Checksum crcForResource; + Checksum Resource::getCRC() { + Checksum crcForResource; - crcForResource.addInt (amount); - crcForResource.addString (type->getName (false)); - crcForResource.addInt (pos.x); - crcForResource.addInt (pos.y); - crcForResource.addInt (balance); + crcForResource.addInt(amount); + crcForResource.addString(type->getName(false)); + crcForResource.addInt(pos.x); + crcForResource.addInt(pos.y); + crcForResource.addInt(balance); - return crcForResource; - } + return crcForResource; + } - } + } } //end namespace diff --git a/source/glest_game/type_instances/resource.h b/source/glest_game/type_instances/resource.h index e9a23fef2..de107ee9c 100644 --- a/source/glest_game/type_instances/resource.h +++ b/source/glest_game/type_instances/resource.h @@ -26,61 +26,57 @@ using std::string; using std::map; using Shared::Xml::XmlNode; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using Shared::Graphics::Vec2i; - using Shared::PlatformCommon::ValueCheckerVault; + using Shared::Graphics::Vec2i; + using Shared::PlatformCommon::ValueCheckerVault; - class ResourceType; - class TechTree; -// ===================================================== -// class Resource -// -/// Amount of a given ResourceType -// ===================================================== + class ResourceType; + class TechTree; + // ===================================================== + // class Resource + // + /// Amount of a given ResourceType + // ===================================================== - class Resource:public ValueCheckerVault - { - private: - int amount; - const ResourceType *type; - Vec2i pos; - int balance; + class Resource :public ValueCheckerVault { + private: + int amount; + const ResourceType *type; + Vec2i pos; + int balance; - public: - Resource (); - void init (const ResourceType * rt, int amount); - void init (const ResourceType * rt, const Vec2i & pos); + public: + Resource(); + void init(const ResourceType * rt, int amount); + void init(const ResourceType * rt, const Vec2i & pos); - const ResourceType *getType () const - { - return type; - } - Vec2i getPos () const - { - return pos; - } + const ResourceType *getType() const { + return type; + } + Vec2i getPos() const { + return pos; + } - int getAmount () const; - int getBalance () const; - string getDescription (bool translatedValue) const; + int getAmount() const; + int getBalance() const; + string getDescription(bool translatedValue) const; - void setAmount (int amount); - void setBalance (int balance); + void setAmount(int amount); + void setBalance(int balance); - bool decAmount (int i); + bool decAmount(int i); - void saveGame (XmlNode * rootNode) const; - void loadGame (const XmlNode * rootNode, int index, - const TechTree * techTree); + void saveGame(XmlNode * rootNode) const; + void loadGame(const XmlNode * rootNode, int index, + const TechTree * techTree); - std::string toString () const; - Checksum getCRC (); - }; + std::string toString() const; + Checksum getCRC(); + }; -}} // end namespace + } +} // end namespace #endif diff --git a/source/glest_game/type_instances/unit.cpp b/source/glest_game/type_instances/unit.cpp index 9de364203..1d17ed258 100644 --- a/source/glest_game/type_instances/unit.cpp +++ b/source/glest_game/type_instances/unit.cpp @@ -33,8194 +33,7218 @@ using namespace Shared::Graphics; using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - const int CHANGE_COMMAND_SPEED = 325; - const uint32 MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED = 160; + const int CHANGE_COMMAND_SPEED = 325; + const uint32 MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED = 160; -//Mutex Unit::mutexDeletedUnits; -//map Unit::deletedUnits; + //Mutex Unit::mutexDeletedUnits; + //map Unit::deletedUnits; - const int UnitPathBasic::maxBlockCount = GameConstants::updateFps / 2; + const int UnitPathBasic::maxBlockCount = GameConstants::updateFps / 2; #ifdef LEAK_CHECK_UNITS - std::map < UnitPathBasic *, bool > UnitPathBasic::mapMemoryList; - std::map < Unit *, bool > Unit::mapMemoryList; - std::map < UnitPathInterface *, int >Unit::mapMemoryList2; + std::map < UnitPathBasic *, bool > UnitPathBasic::mapMemoryList; + std::map < Unit *, bool > Unit::mapMemoryList; + std::map < UnitPathInterface *, int >Unit::mapMemoryList2; #endif - UnitPathBasic::UnitPathBasic ():UnitPathInterface () - { + UnitPathBasic::UnitPathBasic() :UnitPathInterface() { #ifdef LEAK_CHECK_UNITS - UnitPathBasic::mapMemoryList[this] = true; + UnitPathBasic::mapMemoryList[this] = true; #endif - this->blockCount = 0; - this->pathQueue.clear (); - this->map = NULL; - } + this->blockCount = 0; + this->pathQueue.clear(); + this->map = NULL; + } - UnitPathBasic::~UnitPathBasic () - { - this->blockCount = 0; - this->pathQueue.clear (); - this->map = NULL; + UnitPathBasic::~UnitPathBasic() { + this->blockCount = 0; + this->pathQueue.clear(); + this->map = NULL; #ifdef LEAK_CHECK_UNITS - UnitPathBasic::mapMemoryList.erase (this); + UnitPathBasic::mapMemoryList.erase(this); #endif - } + } #ifdef LEAK_CHECK_UNITS - void UnitPathBasic::dumpMemoryList () - { - printf ("===== START report of Unfreed UnitPathBasic pointers =====\n"); - for (std::map < UnitPathBasic *, bool >::iterator iterMap = - UnitPathBasic::mapMemoryList.begin (); - iterMap != UnitPathBasic::mapMemoryList.end (); ++iterMap) - { - printf ("************** ==> Unfreed UnitPathBasic pointer [%p]\n", - iterMap->first); - - if (Unit::mapMemoryList2.find (iterMap->first) != - Unit::mapMemoryList2.end ()) - { - printf ("Found owner unit id [%d]\n", - Unit::mapMemoryList2[iterMap->first]); - } - } - } -#endif - - void UnitPathBasic::clearCaches () - { - this->blockCount = 0; - this->pathQueue.clear (); - } - - bool UnitPathBasic::isEmpty () const - { - return pathQueue.empty (); - } - - bool UnitPathBasic::isBlocked () const - { - return blockCount >= maxBlockCount; - } - - bool UnitPathBasic::isStuck () const - { - return (isBlocked () == true && blockCount >= (maxBlockCount * 2)); - } - - void UnitPathBasic::clear () - { - pathQueue.clear (); - blockCount = 0; - } - - void UnitPathBasic::incBlockCount () - { - pathQueue.clear (); - blockCount++; - } - - void UnitPathBasic::add (const Vec2i & path) - { - if (this->map != NULL) - { - if (this->map->isInside (path) == false) - { - throw megaglest_runtime_error ("Invalid map path position = " + - path.getString () + " map w x h = " + - intToStr (map->getW ()) + " " + - intToStr (map->getH ())); - } - else if (this-> - map->isInsideSurface (this->map->toSurfCoords (path)) == - false) - { - throw - megaglest_runtime_error ("Invalid map surface path position = " + - path.getString () + - " map surface w x h = " + - intToStr (map->getSurfaceW ()) + " " + - intToStr (map->getSurfaceH ())); - } - } - - if (Thread::isCurrentThreadMainThread () == false) - { - throw - megaglest_runtime_error - ("Invalid access to UnitPathBasic add from outside main thread current id = " - + intToStr (Thread::getCurrentThreadId ()) + " main = " + - intToStr (Thread::getMainThreadId ())); - } - - pathQueue.push_back (path); - } - - Vec2i UnitPathBasic::pop (bool removeFrontPos) - { - if (pathQueue.empty () == true) - { - throw megaglest_runtime_error ("pathQueue.size() = " + - intToStr (pathQueue.size ())); - } - Vec2i p = pathQueue.front (); - if (removeFrontPos == true) - { - if (Thread::isCurrentThreadMainThread () == false) - { - throw - megaglest_runtime_error - ("Invalid access to UnitPathBasic delete from outside main thread current id = " - + intToStr (Thread::getCurrentThreadId ()) + " main = " + - intToStr (Thread::getMainThreadId ())); - } - - pathQueue.erase (pathQueue.begin ()); - } - return p; - } - std::string UnitPathBasic::toString ()const - { - std::string result = - "unit path blockCount = " + intToStr (blockCount) + - "\npathQueue size = " + intToStr (pathQueue.size ()); - for (int idx = 0; idx < (int) pathQueue.size (); ++idx) - { - result += - " index = " + intToStr (idx) + " value = " + - pathQueue[idx].getString (); - } - - return result; - } - - void UnitPathBasic::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *unitPathBasicNode = rootNode->addChild ("UnitPathBasic"); - -// int blockCount; - unitPathBasicNode->addAttribute ("blockCount", intToStr (blockCount), - mapTagReplacements); -// vector pathQueue; - for (unsigned int i = 0; i < pathQueue.size (); ++i) - { - Vec2i & vec = pathQueue[i]; - - XmlNode *pathQueueNode = unitPathBasicNode->addChild ("pathQueue"); - pathQueueNode->addAttribute ("vec", vec.getString (), - mapTagReplacements); - } - } - - void UnitPathBasic::loadGame (const XmlNode * rootNode) - { - const XmlNode *unitPathBasicNode = rootNode->getChild ("UnitPathBasic"); - - blockCount = - unitPathBasicNode->getAttribute ("blockCount")->getIntValue (); - - pathQueue.clear (); - vector < XmlNode * >pathqueueNodeList = - unitPathBasicNode->getChildList ("pathQueue"); - for (unsigned int i = 0; i < pathqueueNodeList.size (); ++i) - { - XmlNode *node = pathqueueNodeList[i]; - - Vec2i vec = - Vec2i::strToVec2 (node->getAttribute ("vec")->getValue ()); - pathQueue.push_back (vec); - } - } - - Checksum UnitPathBasic::getCRC () - { - Checksum crcForPath; - - crcForPath.addInt (blockCount); - crcForPath.addInt ((int) pathQueue.size ()); - - return crcForPath; - } - -// ===================================================== -// class UnitPath -// ===================================================== - -//void WaypointPath::condense() { -// if (size() < 2) { -// return; -// } -// iterator prev, curr; -// prev = curr = begin(); -// while (++curr != end()) { -// if (prev->dist(*curr) < 3.f) { -// prev = erase(prev); -// } else { -// ++prev; -// } -// } -//} - - std::string UnitPath::toString ()const - { - std::string result = - "unit path blockCount = " + intToStr (blockCount) + - " pathQueue size = " + intToStr (size ()); - result += " path = "; - for (const_iterator it = begin (); it != end (); ++it) - { - result += " [" + intToStr (it->x) + "," + intToStr (it->y) + "]"; - } - - return result; - } - -// ===================================================== -// class UnitReference -// ===================================================== - - UnitReference::UnitReference () - { - id = -1; - faction = NULL; - } - - UnitReference & UnitReference::operator= (const Unit * unit) - { - if (unit == NULL) - { - id = -1; - faction = NULL; - } - else - { - id = unit->getId (); - faction = unit->getFaction (); - } - - return *this; - } - - Unit *UnitReference::getUnit () const - { - if (faction != NULL) - { - return faction->findUnit (id); - } - return NULL; - } - - void UnitReference::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *unitRefNode = rootNode->addChild ("UnitReference"); - - unitRefNode->addAttribute ("id", intToStr (id), mapTagReplacements); - if (faction != NULL) - { - unitRefNode->addAttribute ("factionIndex", - intToStr (faction->getIndex ()), - mapTagReplacements); - } - } - - void UnitReference::loadGame (const XmlNode * rootNode, World * world) - { - const XmlNode *unitRefNode = rootNode->getChild ("UnitReference"); - - id = unitRefNode->getAttribute ("id")->getIntValue (); - if (unitRefNode->hasAttribute ("factionIndex") == true) - { - int factionIndex = - unitRefNode->getAttribute ("factionIndex")->getIntValue (); - if (factionIndex >= world->getFactionCount ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "factionIndex >= world->getFactionCount() [%d] : [%d]", - factionIndex, world->getFactionCount ()); - throw megaglest_runtime_error (szBuf); - } - faction = world->getFaction (factionIndex); - } - } - - const bool checkMemory = false; - static map < void *, int >memoryObjectList; - - UnitAttackBoostEffect::UnitAttackBoostEffect () - { - if (checkMemory) - { - printf ("++ Create UnitAttackBoostEffect [%p] before count = %d\n", - this, memoryObjectList[this]); - memoryObjectList[this]++; - printf ("++ Create UnitAttackBoostEffect [%p] after count = %d\n", - this, memoryObjectList[this]); - } - - unitId = -1; - unitPtr = NULL; - boost = NULL; - source = NULL; - ups = NULL; - upst = NULL; - } - - UnitAttackBoostEffect::~UnitAttackBoostEffect () - { - if (checkMemory) - { - printf ("-- Delete UnitAttackBoostEffect [%p] count = %d\n", this, - memoryObjectList[this]); - memoryObjectList[this]--; - assert (memoryObjectList[this] == 0); - } - - if (ups != NULL) - { - bool particleValid = - Renderer::getInstance ().validateParticleSystemStillExists (ups, - rsGame); - if (particleValid == true) - { - ups->fade (); - - vector < UnitParticleSystem * >particleSystemToRemove; - particleSystemToRemove.push_back (ups); - - Renderer:: - getInstance ().cleanupUnitParticleSystems (particleSystemToRemove, - rsGame); - ups = NULL; - } - } - - delete upst; - upst = NULL; - } - - const Unit *UnitAttackBoostEffect::getSource () - { - if (source == NULL && unitPtr != NULL && unitId > 0) - { - source = unitPtr->getFaction ()->findUnit (unitId); - - //printf("#1 Get source - boost effect unitId = %d unitPtr = %s source = %p\n",unitId,unitPtr->getFullName(false).c_str(),source); - } - //printf("#2 Get source - boost effect unitId = %d unitPtr = %s source = %p\n",unitId,unitPtr->getFullName(false).c_str(),source); - return source; - } - - void UnitAttackBoostEffect::setSource (const Unit * unit) - { - source = unit; - } - - void UnitAttackBoostEffect:: - applyLoadedAttackBoostParticles (UnitParticleSystemType * upstPtr, - const XmlNode * node, Unit * unit) - { - if (upstPtr != NULL) - { - bool showUnitParticles = - Config::getInstance ().getBool ("UnitParticles", "true"); - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - showUnitParticles = false; - } - if (showUnitParticles == true) - { - upst = new UnitParticleSystemType (); - *upst = *upstPtr; - upst->loadGame (node); - - ups = new UnitParticleSystem (200); - //ups->loadGame(node2); - ups->setParticleOwner (unit); - ups->setParticleType (upst); - - upst->setValues (ups); - ups->setPos (unit->getCurrVectorForParticlesystems ()); - ups->setRotation (unit->getRotation ()); - unit->setMeshPosInParticleSystem (ups); - if (unit->getFaction ()->getTexture ()) - { - ups->setFactionColor (unit->getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - //printf("Adding attack boost particle to deferred buffer: %p\n",ups); - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (ups, rsGame)); - } - } - } - - void UnitAttackBoostEffect::loadGame (const XmlNode * rootNode, - Unit * unit, World * world, - bool applyToOriginator) - { - const XmlNode *unitAttackBoostEffectNode = rootNode; - - if (unitAttackBoostEffectNode->hasAttribute ("source") == true) - { - unitId = - unitAttackBoostEffectNode->getAttribute ("source")->getIntValue (); - unitPtr = unit; - source = unit->getFaction ()->findUnit (unitId); - -// printf("#1 Loaded boost effect unitId = %d unitPtr = [%d - %s] source = %p\n", -// unitId,unitPtr->getId(),unitPtr->getType()->getName(false).c_str(),source); - } - - // Lets determine the originator unit's attack boost based on the skill used to trigger it - if (unitAttackBoostEffectNode->hasAttribute ("source-faction") == true) - { - string factionName = - unitAttackBoostEffectNode-> - getAttribute ("source-faction")->getValue (); - string unitTypeName = - unitAttackBoostEffectNode-> - getAttribute ("source-unit-type")->getValue (); - string skillTypeName = - unitAttackBoostEffectNode-> - getAttribute ("source-skill-type")->getValue (); - SkillClass skillClass = - static_cast < SkillClass > - (unitAttackBoostEffectNode-> - getAttribute ("source-skill-class")->getIntValue ()); - - const UnitType *unitType = - world->findUnitTypeByName (factionName, unitTypeName); - if (unitType != NULL) - { - const SkillType *skillType = - unitType->getSkillType (skillTypeName, skillClass); - if (skillType != NULL) - { - boost = skillType->getAttackBoost (); - -// printf("#2 boost effect unitId = %d unitPtr = [%d - %s] source = %p attackBoost src [%p] dest [%p]\n", -// unitId,unitPtr->getId(),unitPtr->getType()->getName(false).c_str(), -// source,boost->unitParticleSystemTypeForSourceUnit,boost->unitParticleSystemTypeForAffectedUnit); - } - } - } - - if (boost != NULL) - { -// printf("unit [%d - %s] applyToOriginator: %d src [%p] dest [%p] Boost attackBoost->enabled = %d:\n%s\n", -// unit->getId(),unit->getType()->getName(false).c_str(),applyToOriginator,boost->unitParticleSystemTypeForSourceUnit,boost->unitParticleSystemTypeForAffectedUnit,boost->enabled,boost->getDesc(false).c_str()); - - if (applyToOriginator == true) - { - applyLoadedAttackBoostParticles - (boost->unitParticleSystemTypeForSourceUnit, - unitAttackBoostEffectNode, unit); - } - else - { - applyLoadedAttackBoostParticles - (boost->unitParticleSystemTypeForAffectedUnit, - unitAttackBoostEffectNode, unit); - } - } - else - { - printf - ("******!!!! unit [%d - %s] applyToOriginator: %d NO BOOST FOUND!!!\n", - unit->getId (), unit->getType ()->getName (false).c_str (), - applyToOriginator); - } - } - - void UnitAttackBoostEffect::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *unitAttackBoostEffectNode = - rootNode->addChild ("UnitAttackBoostEffect"); - -// const AttackBoost *boost; - if (boost != NULL) - { - boost->saveGame (unitAttackBoostEffectNode); - } -// const Unit *source; - if (getSource () != NULL) - { - unitAttackBoostEffectNode->addAttribute ("source", - intToStr (getSource ()->getId - ()), - mapTagReplacements); - - unitAttackBoostEffectNode->addAttribute ("source-faction", - getSource ()-> - getFaction ()->getType ()-> - getName (false), - mapTagReplacements); - unitAttackBoostEffectNode->addAttribute ("source-unit-type", - getSource ()-> - getType ()->getName (false), - mapTagReplacements); - unitAttackBoostEffectNode->addAttribute ("source-skill-type", - getSource ()->getCurrSkill - ()->getName (), - mapTagReplacements); - unitAttackBoostEffectNode->addAttribute ("source-skill-class", - intToStr (getSource - ()->getCurrSkill - ()->getClass ()), - mapTagReplacements); - } -// UnitParticleSystem *ups; - if (ups != NULL - && Renderer::getInstance ().validateParticleSystemStillExists (ups, - rsGame) - == true) - { - ups->saveGame (unitAttackBoostEffectNode); - } - -// UnitParticleSystemType *upst; - if (upst != NULL) - { - upst->saveGame (unitAttackBoostEffectNode); - } - } - - UnitAttackBoostEffectOriginator::UnitAttackBoostEffectOriginator () - { - skillType = NULL; - currentAppliedEffect = NULL; - } - - UnitAttackBoostEffectOriginator::~UnitAttackBoostEffectOriginator () - { - delete currentAppliedEffect; - currentAppliedEffect = NULL; - } - - void UnitAttackBoostEffectOriginator::loadGame (const XmlNode * rootNode, - Unit * unit, - World * world) - { - const XmlNode *unitAttackBoostEffectOriginatorNode = - rootNode->getChild ("UnitAttackBoostEffectOriginator"); - - SkillClass skillClass = scStop; - string skillTypeName = - unitAttackBoostEffectOriginatorNode-> - getAttribute ("skillType")->getValue (); - if (unitAttackBoostEffectOriginatorNode->hasAttribute ("skillClass") == - false) - { - int skillCount = unit->getType ()->getSkillTypeCount (); - for (int index = 0; index < skillCount; ++index) - { - const SkillType *st = unit->getType ()->getSkillType (index); - if (st->getName () == skillTypeName) - { - skillClass = st->getClass (); - break; - } - } - } - else - { - skillClass = - static_cast < SkillClass > - (unitAttackBoostEffectOriginatorNode-> - getAttribute ("skillClass")->getIntValue ()); - } - - this->skillType = - unit->getType ()->getSkillType (skillTypeName, skillClass); - - if (unitAttackBoostEffectOriginatorNode->hasChild - ("currentAttackBoostUnits") == true) - { - vector < XmlNode * >currentAttackBoostUnitsNodeList = - unitAttackBoostEffectOriginatorNode->getChildList - ("currentAttackBoostUnits"); - for (unsigned int i = 0; i < currentAttackBoostUnitsNodeList.size (); - ++i) - { - XmlNode *node = currentAttackBoostUnitsNodeList[i]; - - int unitId = node->getAttribute ("value")->getIntValue (); - currentAttackBoostUnits.push_back (unitId); - } - } - - if (unitAttackBoostEffectOriginatorNode->hasChild - ("UnitAttackBoostEffect") == true) - { - currentAppliedEffect = new UnitAttackBoostEffect (); - currentAppliedEffect->loadGame (unitAttackBoostEffectOriginatorNode, - unit, world, true); - } - } - - void UnitAttackBoostEffectOriginator::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *unitAttackBoostEffectOriginatorNode = - rootNode->addChild ("UnitAttackBoostEffectOriginator"); - -// const SkillType *skillType; - if (skillType != NULL) - { - unitAttackBoostEffectOriginatorNode->addAttribute ("skillType", - skillType->getName - (), - mapTagReplacements); - unitAttackBoostEffectOriginatorNode->addAttribute ("skillClass", - intToStr - (skillType->getClass - ()), - mapTagReplacements); - } -// std::vector currentAttackBoostUnits; - for (unsigned int i = 0; i < currentAttackBoostUnits.size (); ++i) - { - XmlNode *currentAttackBoostUnitsNode = - unitAttackBoostEffectOriginatorNode->addChild - ("currentAttackBoostUnits"); - currentAttackBoostUnitsNode->addAttribute ("value", - intToStr - (currentAttackBoostUnits - [i]), mapTagReplacements); - } -// UnitAttackBoostEffect *currentAppliedEffect; - if (currentAppliedEffect != NULL) - { - currentAppliedEffect->saveGame (unitAttackBoostEffectOriginatorNode); - } - } - -// ===================================================== -// class Unit -// ===================================================== - - const float Unit::ANIMATION_SPEED_MULTIPLIER = 100000.f; -//const float Unit::PROGRESS_SPEED_MULTIPLIER = 100000.f; - const int64 Unit::PROGRESS_SPEED_MULTIPLIER = 100000; - - const int Unit::speedDivider = 100; - const int Unit::maxDeadCount = 1000; //time in until the corpse disapears - should be about 40 seconds - const int Unit::invalidId = -1; - -//set Unit::livingUnits; -//set Unit::livingUnitsp; - -// ============================ Constructor & destructor ============================= - - Game *Unit::game = NULL; - - Unit::Unit (int id, UnitPathInterface * unitpath, const Vec2i & pos, - const UnitType * type, Faction * faction, Map * map, - CardinalDir placeFacing):BaseColorPickEntity (), id (id) - { -#ifdef LEAK_CHECK_UNITS - Unit::mapMemoryList[this] = true; -#endif - - mutexCommands = new Mutex (CODE_AT_LINE); - changedActiveCommand = false; - lastChangedActiveCommandFrame = 0; - changedActiveCommandFrame = 0; - - lastSynchDataString = ""; - modelFacing = CardinalDir (CardinalDir::NORTH); - lastStuckFrame = 0; - lastStuckPos = Vec2i (0, 0); - lastPathfindFailedFrame = 0; - lastPathfindFailedPos = Vec2i (0, 0); - usePathfinderExtendedMaxNodes = false; - this->currentAttackBoostOriginatorEffect.skillType = NULL; - lastAttackerUnitId = -1; - lastAttackedUnitId = -1; - causeOfDeath = ucodNone; - pathfindFailedConsecutiveFrameCount = 0; - - lastSynchDataString = ""; - lastFile = ""; - lastLine = 0; - lastSource = ""; - - targetRotationZ = .0f; - targetRotationX = .0f; - rotationZ = .0f; - rotationX = .0f; - - this->fire = NULL; - this->unitPath = unitpath; - this->unitPath->setMap (map); - - //RandomGen random; - random.init (id); - random.setDisableLastCallerTracking (isNetworkCRCEnabled () == false); - pathFindRefreshCellCount = - random.randRange (10, 20, intToStr (__LINE__)); - - if (map->isInside (pos) == false - || map->isInsideSurface (map->toSurfCoords (pos)) == false) - { - throw megaglest_runtime_error ("#2 Invalid path position = " + - pos.getString ()); - } - - this->pos = pos; - - this->faction = faction; - this->preMorph_type = NULL; - this->type = type; - setType (this->type); - - this->map = map; - this->targetRef = NULL; - this->targetField = fLand; - this->targetVec = Vec3f (0.0); - this->targetPos = Vec2i (0); - this->lastRenderFrame = 0; - this->visible = true; - this->retryCurrCommandCount = 0; - this->screenPos = Vec3f (0.0); - this->ignoreCheckCommand = false; - this->inBailOutAttempt = false; - this->lastHarvestResourceTarget.first = Vec2i (0); - this->morphFieldsBlocked = false; - //this->lastBadHarvestListPurge = 0; - this->oldTotalSight = 0; - - level = NULL; - loadType = NULL; - - setModelFacing (placeFacing); - - Config & config = Config::getInstance (); - showUnitParticles = config.getBool ("UnitParticles", "true"); - maxQueuedCommandDisplayCount = - config.getInt ("MaxQueuedCommandDisplayCount", "15"); - - if (GlobalStaticFlags::getIsNonGraphicalModeEnabled () == true) - { - showUnitParticles = false; - } - - lastPos = pos; - progress = 0; - this->lastAnimProgress = 0; - this->animProgress = 0; - progress2 = 0; - kills = 0; - enemyKills = 0; - loadCount = 0; - ep = 0; - deadCount = 0; - hp = type->getMaxHp () / 20; - toBeUndertaken = false; - - highlight = 0.f; - meetingPos = pos; - setAlive (true); - - if (type->hasSkillClass (scBeBuilt) == false) - { - float rot = 0.f; - random.init (id); - rot += random.randRange (-5, 5, intToStr (__LINE__)); - rotation = rot; - lastRotation = rot; - targetRotation = rot; - } - // else it was set appropriately in setModelFacing() - - if (getType ()->getField (fAir)) - { - currField = fAir; - } - if (getType ()->getField (fLand)) - { - currField = fLand; - } - - computeTotalUpgrade (); - - //starting skill - this->lastModelIndexForCurrSkillType = -1; - this->animationRandomCycleCount = 0; - this->currSkill = getType ()->getFirstStOfClass (scStop); - this->setCurrSkill (this->currSkill); - this->currentAttackBoostOriginatorEffect.skillType = this->currSkill; - - this->faction->addLivingUnits (id); - this->faction->addLivingUnitsp (this); - - addItemToVault (&this->hp, this->hp); - addItemToVault (&this->ep, this->ep); - - calculateFogOfWarRadius (); - -// if(isUnitDeleted(this) == true) { -// MutexSafeWrapper safeMutex(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); -// deletedUnits.erase(this); -// } - - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - } - - Unit::~Unit () - { - badHarvestPosList.clear (); - - this->faction->deleteLivingUnits (id); - this->faction->deleteLivingUnitsp (this); - - //remove commands - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - changedActiveCommand = false; - while (commands.empty () == false) - { - delete commands.back (); - commands.pop_back (); - } - safeMutex.ReleaseLock (); - - cleanupAllParticlesystems (); - - while (currentAttackBoostEffects.empty () == false) - { - //UnitAttackBoostEffect &effect = currentAttackBoostEffects.back(); - UnitAttackBoostEffect *ab = currentAttackBoostEffects.back (); - delete ab; - currentAttackBoostEffects.pop_back (); - } - - delete currentAttackBoostOriginatorEffect.currentAppliedEffect; - currentAttackBoostOriginatorEffect.currentAppliedEffect = NULL; - - //Renderer::getInstance().cleanupParticleSystems(attackParticleSystems,rsGame); - Renderer::getInstance ().removeParticleSystemsForParticleOwner (this, - rsGame); - - -#ifdef LEAK_CHECK_UNITS - Unit::mapMemoryList2[this->unitPath] = this->getId (); -#endif - - delete this->unitPath; - this->unitPath = NULL; - - Renderer & renderer = Renderer::getInstance (); - renderer.removeUnitFromQuadCache (this); - if (game != NULL) - { - game->removeUnitFromSelection (this); - } - - //MutexSafeWrapper safeMutex1(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); - //deletedUnits[this]=true; - - delete mutexCommands; - mutexCommands = NULL; - -#ifdef LEAK_CHECK_UNITS - Unit::mapMemoryList.erase (this); -#endif - } - - void Unit::cleanupAllParticlesystems () - { - - Renderer:: - getInstance ().cleanupUnitParticleSystems (unitParticleSystems, - rsGame); - - Renderer::getInstance ().cleanupParticleSystems (fireParticleSystems, - rsGame); - // Must set this to null of it will be used below in stopDamageParticles() - - if (Renderer:: - getInstance ().validateParticleSystemStillExists (this->fire, - rsGame) == false) - { - this->fire = NULL; - } - - // fade(and by this remove) all unit particle systems - queuedUnitParticleSystemTypes.clear (); - while (unitParticleSystems.empty () == false) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists - (unitParticleSystems.back (), rsGame) == true) - { - unitParticleSystems.back ()->fade (); - } - unitParticleSystems.pop_back (); - } - stopDamageParticles (true); - - } - - ParticleSystem *Unit::getFire () const - { - if (this->fire != NULL && - Renderer::getInstance (). - validateParticleSystemStillExists (this->fire, rsGame) == false) - { - return NULL; - } - return this->fire; - } - - void Unit::setType (const UnitType * newType) - { - this->faction->notifyUnitTypeChange (this, newType); - this->type = newType; - } - - void Unit::setAlive (bool value) - { - this->alive = value; - this->faction->notifyUnitAliveStatusChange (this); - } - -#ifdef LEAK_CHECK_UNITS - void Unit::dumpMemoryList () - { - printf ("===== START report of Unfreed Unit pointers =====\n"); - for (std::map < Unit *, bool >::iterator iterMap = - Unit::mapMemoryList.begin (); - iterMap != Unit::mapMemoryList.end (); ++iterMap) - { - printf ("************** ==> Unfreed Unit pointer [%p]\n", - iterMap->first); - } - } -#endif - - bool Unit::isNetworkCRCEnabled () - { - bool isNetworkCRCEnabled = false; - - if (game != NULL) - { - if (game->isFlagType1BitEnabled (ft1_network_synch_checks_verbose) == - true) - { - isNetworkCRCEnabled = true; - } - else if (game->isFlagType1BitEnabled (ft1_network_synch_checks) == - true) - { - isNetworkCRCEnabled = true; - } - } - return isNetworkCRCEnabled; - } - - void Unit::clearNetworkCRCDecHpList () - { - if (networkCRCDecHpList.empty () == false) - { - networkCRCDecHpList.clear (); - } - } - void Unit::clearParticleInfo () - { - if (networkCRCParticleInfoList.empty () == false) - { - networkCRCParticleInfoList.clear (); - } - } - - void Unit::addNetworkCRCDecHp (string info) - { - if (isNetworkCRCEnabled () == true) - { - networkCRCDecHpList.push_back (info); - } - } - - void Unit::logParticleInfo (string info) - { - if (isNetworkCRCEnabled () == true) - { - networkCRCParticleInfoList.push_back (info); - } - } - string Unit::getParticleInfo () const - { - string result = ""; - if (networkCRCParticleInfoList.empty () == false) - { - for (unsigned int index = 0; - index < networkCRCParticleInfoList.size (); ++index) - { - result += networkCRCParticleInfoList[index] + "|"; - } - } - return result; - } - - void Unit::end (ParticleSystem * particleSystem) - { - if (particleSystem == fire) - { - fire = NULL; - } - - vector < ParticleSystem * >::iterator iterFind = - find (attackParticleSystems.begin (), attackParticleSystems.end (), - particleSystem); - if (iterFind != attackParticleSystems.end ()) - { - attackParticleSystems.erase (iterFind); - return; - } - vector < UnitParticleSystem * >::iterator iterFind1 = - find (smokeParticleSystems.begin (), smokeParticleSystems.end (), - particleSystem); - if (iterFind1 != smokeParticleSystems.end ()) - { - smokeParticleSystems.erase (iterFind1); - return; - } - iterFind = - find (fireParticleSystems.begin (), fireParticleSystems.end (), - particleSystem); - if (iterFind != fireParticleSystems.end ()) - { - fireParticleSystems.erase (iterFind); - return; - } - iterFind1 = - find (damageParticleSystems.begin (), damageParticleSystems.end (), - particleSystem); - if (iterFind1 != damageParticleSystems.end ()) - { - damageParticleSystems.erase (iterFind1); - return; - } - - iterFind1 = - find (unitParticleSystems.begin (), unitParticleSystems.end (), - particleSystem); - if (iterFind1 != unitParticleSystems.end ()) - { - unitParticleSystems.erase (iterFind1); - return; - } - } - -//bool Unit::isUnitDeleted(void *unit) { -// bool result = false; -// MutexSafeWrapper safeMutex(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); -// if(deletedUnits.find(unit) != deletedUnits.end()) { -// result = true; -// } -// return result; -//} - - void Unit::setModelFacing (CardinalDir value) - { - modelFacing = value; - lastRotation = targetRotation = rotation = value * 90.f; - } - -//void Unit::setCurrField(Field currField) { -// Field original_field = this->currField; -// -// this->currField = currField; -// -// if(original_field != this->currField) { -// //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); -// game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged); -// //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); -// } -//} -// ====================================== get ====================================== - - Vec2i Unit::getCenteredPos () const - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - return pos + Vec2i (type->getSize () / 2, type->getSize () / 2); - } - - Vec2f Unit::getFloatCenteredPos () const - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - return Vec2f (truncateDecimal < - float >(pos.x - 0.5f + type->getSize () / 2.f, 6), - truncateDecimal < - float >(pos.y - 0.5f + type->getSize () / 2.f, 6)); - } - - Vec2i Unit::getCellPos () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (type->hasCellMap ()) - { - if (type->hasEmptyCellMap () == false || - type->getAllowEmptyCellMap () == true) - { - - //find nearest pos to center that is free - Vec2i centeredPos = getCenteredPos (); - float nearestDist = -1.f; - Vec2i nearestPos = pos; - - for (int i = 0; i < type->getSize (); ++i) - { - for (int j = 0; j < type->getSize (); ++j) - { - if (type->getCellMapCell (i, j, modelFacing)) - { - Vec2i currPos = pos + Vec2i (i, j); - float dist = currPos.dist (centeredPos); - if (nearestDist == -1.f || dist < nearestDist) - { - nearestDist = dist; - nearestPos = currPos; - } - } - } - } - return nearestPos; - } - } - return pos; - } - - - - void Unit::calculateXZRotation () - { - //if(type->getProperty(UnitType::pRotatedClimb) && currSkill->getClass()==scMove){ - //if(currSkill->getClass()==scMove) - if (lastPos != pos) - { // targetPosCalc ( maybe also sometimes needed if no move ? terrain flatting... ) - SurfaceCell *sc = map->getSurfaceCell (Map::toSurfCoords (pos)); - const Vec3f normal = sc->getNormal (); - -#ifdef USE_STREFLOP - targetRotationZ = - radToDeg (streflop::atan2 - (static_cast < streflop::Simple > (abs (normal.x)), - static_cast < streflop::Simple > (abs (normal.y)))); -#else - targetRotationZ = radToDeg (atan2 (abs (normal.x), abs (normal.y))); -#endif - - if ((normal.y < 0 || normal.x < 0) && !(normal.y < 0 && normal.x < 0)) - { - targetRotationZ = targetRotationZ * -1; - } - targetRotationZ = targetRotationZ * -1; - -#ifdef USE_STREFLOP - targetRotationX = - radToDeg (streflop::atan2 - (static_cast < streflop::Simple > (abs (normal.z)), - static_cast < streflop::Simple > (abs (normal.y)))); -#else - targetRotationX = radToDeg (atan2 (abs (normal.z), abs (normal.y))); -#endif - - if ((normal.y < 0 || normal.z < 0) && !(normal.y < 0 && normal.z < 0)) - { - targetRotationX = targetRotationX * -1; - } - } - - //For smooth rotation we now softly adjust the angle - int adjustStep = 1; - if (rotationZ < targetRotationZ) - { - if (rotationZ + adjustStep > targetRotationZ) - { - rotationZ = targetRotationZ; - } - else - { - rotationZ = rotationZ + adjustStep; - } - } - else if (rotationZ > targetRotationZ) - { - if (rotationZ - adjustStep < targetRotationZ) - { - rotationZ = targetRotationZ; - } - else - { - rotationZ = rotationZ - adjustStep; - } - } - - if (rotationX < targetRotationX) - { - if (rotationX + adjustStep > targetRotationX) - { - rotationX = targetRotationX; - } - else - { - rotationX = rotationX + adjustStep; - } - } - else if (rotationX > targetRotationX) - { - if (rotationX - adjustStep < targetRotationX) - { - rotationX = targetRotationX; - } - else - { - rotationX = rotationX - adjustStep; - } - } - } - - float Unit::getRotationZ () const - { - return rotationZ; - } - - float Unit::getRotationX () const - { - return rotationX; - } - - int Unit::getProductionPercent () const - { - if (anyCommand ()) - { - const ProducibleType *produced = - commands.front ()->getCommandType ()->getProduced (); - if (produced != NULL) - { - if (produced->getProductionTime () == 0) - { - return 0; - } - return clamp (progress2 * 100 / produced->getProductionTime (), 0, - 100); - } - } - return -1; - } - - float Unit::getProgressRatio () const - { - if (anyCommand ()) - { - const ProducibleType *produced = - commands.front ()->getCommandType ()->getProduced (); - if (produced != NULL) - { - if (produced->getProductionTime () == 0) - { - return 0.f; - } - - float help = progress2; - return clamp (help / produced->getProductionTime (), 0.f, 1.f); - } - } - return -1; - } - - float Unit::getHpRatio () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - float maxHpAllowed = type->getTotalMaxHp (&totalUpgrade); - if (maxHpAllowed == 0.f) - { - return 0.f; - } - return clamp (static_cast < float >(hp) / maxHpAllowed, 0.f, 1.f); - } - - float Unit::getEpRatio () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (type->getTotalMaxHp (&totalUpgrade) == 0) - { - return 0.f; - } - else - { - float maxEpAllowed = type->getTotalMaxEp (&totalUpgrade); - if (maxEpAllowed == 0.f) - { - return 0.f; - } - return clamp (static_cast < float >(ep) / maxEpAllowed, 0.f, 1.f); - } - } - - const Level *Unit::getNextLevel () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (level == NULL && type->getLevelCount () > 0) - { - return type->getLevel (0); - } - else - { - for (int i = 1; i < type->getLevelCount (); ++i) - { - if (type->getLevel (i - 1) == level) - { - return type->getLevel (i); - } - } - } - return NULL; - } - - string Unit::getFullName (bool translatedValue) const - { - string str = ""; - if (level != NULL) - { - str += (level->getName (translatedValue) + " "); - } - if (type == NULL) - { - throw - megaglest_runtime_error ("type == NULL in Unit::getFullName()!"); - } - str += type->getName (translatedValue); - return str; - } - -// ====================================== is ====================================== - - bool Unit::isOperative () const - { - return isAlive () && isBuilt (); - } - - bool Unit::isAnimProgressBound () const - { - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - bool result = false; - if (currSkill->getClass () == scBeBuilt) - { - const BeBuiltSkillType *skill = - dynamic_cast < const BeBuiltSkillType * >(currSkill); - if (skill != NULL) - { - result = skill->getAnimProgressBound (); - } - } - else if (currSkill->getClass () == scProduce) - { - const ProduceSkillType *skill = - dynamic_cast < const ProduceSkillType * >(currSkill); - if (skill != NULL) - { - result = skill->getAnimProgressBound (); - } - } - else if (currSkill->getClass () == scUpgrade) - { - const UpgradeSkillType *skill = - dynamic_cast < const UpgradeSkillType * >(currSkill); - if (skill != NULL) - { - result = skill->getAnimProgressBound (); - } - } - else if (currSkill->getClass () == scMorph) - { - const MorphSkillType *skill = - dynamic_cast < const MorphSkillType * >(currSkill); - if (skill != NULL) - { - result = skill->getAnimProgressBound (); - } - } - return result; - } - - bool Unit::isBeingBuilt () const - { - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - return (currSkill->getClass () == scBeBuilt); - } - - bool Unit::isBuilt () const - { - return (isBeingBuilt () == false); - } - - bool Unit::isBuildCommandPending () const - { - bool result = false; - - Command *command = this->getCurrCommand (); - if (command != NULL) - { - const BuildCommandType *bct = - dynamic_cast < - const BuildCommandType * >(command->getCommandType ()); - if (bct != NULL) - { - if (this->getCurrSkill ()->getClass () != scBuild) - { - result = true; - } - } - } - - return result; - } - - UnitBuildInfo Unit::getBuildCommandPendingInfo () const - { - UnitBuildInfo result; - - Command *command = this->getCurrCommand (); - if (command != NULL) - { - const BuildCommandType *bct = - dynamic_cast < - const BuildCommandType * >(command->getCommandType ()); - if (bct != NULL) - { - result.pos = command->getOriginalPos (); - result.facing = command->getFacing (); - result.buildUnit = command->getUnitType (); - result.unit = this; - } - } - - return result; - } - - bool Unit::isAlly (const Unit * unit) const - { - if (unit == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: unit == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - return faction->isAlly (unit->getFaction ()); - } - - bool Unit::isDamaged () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - return hp < type->getTotalMaxHp (&totalUpgrade); - } - - bool Unit::isInteresting (InterestingUnitType iut) const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - switch (iut) - { - case iutIdleHarvester: - if (type->hasCommandClass (ccHarvest)) - { - if (commands.empty () == false) - { - const CommandType *ct = commands.front ()->getCommandType (); - if (ct != NULL) - { - return ct->getClass () == ccStop; - } - } - } - return false; - - case iutBuiltBuilding: - return type->hasSkillClass (scBeBuilt) && isBuilt (); - case iutProducer: - return type->hasSkillClass (scProduce); - case iutDamaged: - return isDamaged (); - case iutStore: - return type->getStoredResourceCount () > 0; - default: - return false; - } - } - -// ====================================== set ====================================== - - void Unit::setCurrSkill (const SkillType * currSkill) - { - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - if (this->currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: this->currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (this->currSkill->getClass () == scMove && - currSkill->getClass () != scMove) - { - faction->removeUnitFromMovingList (this->getId ()); - } - else if (this->currSkill->getClass () != scMove && - currSkill->getClass () == scMove) - { - faction->addUnitToMovingList (this->getId ()); - } - - changedActiveCommand = false; - if (currSkill->getClass () != this->currSkill->getClass () || - currSkill->getName () != this->currSkill->getName ()) - { - this->animProgress = 0; - this->lastAnimProgress = 0; - - queuedUnitParticleSystemTypes.clear (); - while (unitParticleSystems.empty () == false) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists - (unitParticleSystems.back (), rsGame) == true) - { - unitParticleSystems.back ()->fade (); - } - unitParticleSystems.pop_back (); - } - - Command *cmd = getCurrrentCommandThreadSafe (); - - // Set mew fog of war skill type if need be - if (cmd != NULL && cmd->getCommandType () != NULL && - cmd->getCommandType ()-> - hasFogOfWarSkillType (currSkill->getName ())) - { - const FogOfWarSkillType *fowst = - cmd->getCommandType ()->getFogOfWarSkillType (); - - // Remove old fog of war skill type if need be - game->getWorld ()->removeFogOfWarSkillTypeFromList (this); - - game->getWorld ()->addFogOfWarSkillType (this, fowst); - } - else - { - // Remove old fog of war skill type if need be - game->getWorld ()->removeFogOfWarSkillType (this); - } - } - if (showUnitParticles == true && - currSkill->unitParticleSystemTypes.empty () == false && - unitParticleSystems.empty () == true) - { - //printf("START - particle system type\n"); - - /* - for(UnitParticleSystemTypes::const_iterator it= currSkill->unitParticleSystemTypes.begin(); - it != currSkill->unitParticleSystemTypes.end(); ++it) { - if((*it)->getStartTime() == 0.0) { - //printf("Adding NON-queued particle system type [%s] [%f] [%f]\n",(*it)->getType().c_str(),(*it)->getStartTime(),(*it)->getEndTime()); - - UnitParticleSystem *ups = new UnitParticleSystem(200); - ups->setParticleOwner(this); - ups->setParticleType((*it)); - - (*it)->setValues(ups); - ups->setPos(getCurrVector()); - ups->setRotation(getRotation()); - ups->setUnitModel(getCurrentModelPtr()); - if(getFaction()->getTexture()) { - ups->setFactionColor(getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0)); - } - unitParticleSystems.push_back(ups); - Renderer::getInstance().manageParticleSystem(ups, rsGame); - } - else { - //printf("Adding queued particle system type [%s] [%f] [%f]\n",(*it)->getType().c_str(),(*it)->getStartTime(),(*it)->getEndTime()); - - queuedUnitParticleSystemTypes.push_back(*it); - } - } - */ - checkCustomizedUnitParticleListTriggers - (currSkill->unitParticleSystemTypes, true); - } - - if (this->currSkill != currSkill) - { - this->lastModelIndexForCurrSkillType = -1; - this->animationRandomCycleCount = 0; - } - - if (faction != NULL) - faction->notifyUnitSkillTypeChange (this, currSkill); - const SkillType *original_skill = this->currSkill; - this->currSkill = currSkill; - - if (original_skill != this->currSkill) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_SkillChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - } - - void Unit::setCurrSkill (SkillClass sc) - { - if (getType () == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: getType() == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - setCurrSkill (getType ()->getFirstStOfClass (sc)); - } - - void Unit::setTarget (const Unit * unit) - { - - if (unit == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: unit == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //find a free pos in cellmap - setTargetPos (unit->getCellPos ()); - - //ser field and vector - targetField = unit->getCurrField (); - targetVec = unit->getCurrVectorAsTarget (); - targetRef = unit; - } - - RandomGen *Unit::getRandom (bool threadAccessAllowed) - { - if (threadAccessAllowed == false - && Thread::isCurrentThreadMainThread () == false) - { - throw - megaglest_runtime_error - ("Invalid access to unit random from outside main thread current id = " - + intToStr (Thread::getCurrentThreadId ()) + " main = " + - intToStr (Thread::getMainThreadId ())); - } - return &random; - } - - void Unit::setPos (const Vec2i & pos, bool clearPathFinder, bool threaded) - { - if (map->isInside (pos) == false - || map->isInsideSurface (map->toSurfCoords (pos)) == false) - { - throw megaglest_runtime_error ("#3 Invalid path position = " + - pos.getString ()); - } - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - if (threaded) - { - logSynchDataThreaded (extractFileFromDirectoryPath (__FILE__).c_str - (), __LINE__); - } - else - { - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - } - - if (clearPathFinder == true && this->unitPath != NULL) - { - this->unitPath->clear (); - } - //Vec2i oldLastPos = this->lastPos; - this->lastPos = this->pos; - this->pos = pos; - - map->clampPos (this->pos); - this->meetingPos = pos - Vec2i (1); - map->clampPos (this->meetingPos); - - safeMutex.ReleaseLock (); - - refreshPos (); - - if (threaded) - { - logSynchDataThreaded (extractFileFromDirectoryPath (__FILE__).c_str - (), __LINE__); - } - else - { - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - } - } - - void Unit::refreshPos (bool forceRefresh) - { - // Attempt to improve performance - this->exploreCells (forceRefresh); - calculateFogOfWarRadius (forceRefresh); - } - - FowAlphaCellsLookupItem Unit::getFogOfWarRadius (bool useCache) const - { - if (useCache == true) - { - return cachedFow; - } - - //iterate through all cells - int sightRange = - this->getType ()->getTotalSight (this->getTotalUpgrade ()); - int radius = sightRange + World::indirectSightRange; - PosCircularIterator pci (map, this->getPosNotThreadSafe (), radius); - FowAlphaCellsLookupItem result; - while (pci.next ()) - { - const Vec2i sightpos = pci.getPos (); - Vec2i surfPos = Map::toSurfCoords (sightpos); - - //compute max alpha - float maxAlpha = 0.0f; - if (surfPos.x > 1 && surfPos.y > 1 && - surfPos.x < map->getSurfaceW () - 2 && - surfPos.y < map->getSurfaceH () - 2) - { - maxAlpha = 1.f; - } - else if (surfPos.x > 0 && surfPos.y > 0 && - surfPos.x < map->getSurfaceW () - 1 && - surfPos.y < map->getSurfaceH () - 1) - { - maxAlpha = 0.3f; - } - - //compute alpha - float alpha = maxAlpha; - float dist = this->getPosNotThreadSafe ().dist (sightpos); - if (dist > sightRange) - { - alpha = - clamp (1.f - (dist - sightRange) / (World::indirectSightRange), - 0.f, maxAlpha); - } - result.surfPosAlphaList[surfPos] = alpha; - } - return result; - } - - void Unit::calculateFogOfWarRadius (bool forceRefresh) - { - if (game->getWorld ()->getFogOfWar () == true) - { - if (forceRefresh || this->pos != this->cachedFowPos) - { - cachedFow = getFogOfWarRadius (false); - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - this->cachedFowPos = this->pos; - } - } - } - - void Unit::setTargetPos (const Vec2i & targetPos, bool threaded) - { - - if (map->isInside (targetPos) == false - || map->isInsideSurface (map->toSurfCoords (targetPos)) == false) - { - throw megaglest_runtime_error ("#4 Invalid path position = " + - targetPos.getString ()); - } - - Vec2i relPos = targetPos - pos; - //map->clampPos(relPos); - - Vec2f relPosf = Vec2f ((float) relPos.x, (float) relPos.y); -#ifdef USE_STREFLOP - targetRotation = - radToDeg (streflop::atan2 - (static_cast < streflop::Simple > (relPosf.x), - static_cast < streflop::Simple > (relPosf.y))); -#else - targetRotation = radToDeg (atan2 (relPosf.x, relPosf.y)); -#endif - targetRotation = truncateDecimal < float >(targetRotation, 6); - - targetRef = NULL; - - this->targetPos = targetPos; - map->clampPos (this->targetPos); - - if (threaded) - { - logSynchDataThreaded (extractFileFromDirectoryPath (__FILE__).c_str - (), __LINE__); - } - else - { - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - } - } - - void Unit::addAttackParticleSystem (ParticleSystem * ps) - { - attackParticleSystems.push_back (ps); - } - - void Unit::setVisible (const bool visible) - { - this->visible = visible; - - if (unitParticleSystems.empty () == false) - { - for (UnitParticleSystems::iterator it = unitParticleSystems.begin (); - it != unitParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == - true) - { - (*it)->setVisible (visible); - } - } - } - if (damageParticleSystems.empty () == false) - { - for (UnitParticleSystems::iterator it = - damageParticleSystems.begin (); - it != damageParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == - true) - { - (*it)->setVisible (visible); - } - } - } - if (smokeParticleSystems.empty () == false) - { - for (UnitParticleSystems::iterator it = smokeParticleSystems.begin (); - it != smokeParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == - true) - { - if ((*it)->getVisible () != visible) - { - //printf("Changing visibility for smoke particle system to: %d\n",visible); - (*it)->setVisible (visible); - } - } - } - } - - //if(attackParticleSystems.empty() == false) { - // for(vector::iterator it= attackParticleSystems.begin(); it != attackParticleSystems.end(); ++it) { - // if(Renderer::getInstance().validateParticleSystemStillExists((*it),rsGame) == true) { - // Not sure this is a good idea since the unit be not be visible but the attack particle might be. - // This means you won't see the attacking projectile until the unit moves into view. - //(*it)->setVisible(visible); - // } - // } - //} - - if (currentAttackBoostEffects.empty () == false) - { - for (unsigned int i = 0; i < currentAttackBoostEffects.size (); ++i) - { - UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; - if (effect != NULL && effect->ups != NULL) - { - bool particleValid = - Renderer:: - getInstance ().validateParticleSystemStillExists (effect->ups, - rsGame); - if (particleValid == true) - { - effect->ups->setVisible (visible); - } - } - } - } - if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) - { - if (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups != - NULL) - { - bool particleValid = - Renderer::getInstance ().validateParticleSystemStillExists - (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups, - rsGame); - if (particleValid == true) - { - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setVisible (visible); - } - } - } - } - -// =============================== Render related ================================== - - Model *Unit::getCurrentModelPtr () - { - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - int currentModelIndexForCurrSkillType = lastModelIndexForCurrSkillType; - Model *result = - currSkill->getAnimation (getAnimProgressAsFloat (), this, - &lastModelIndexForCurrSkillType, - &animationRandomCycleCount); - if (currentModelIndexForCurrSkillType != lastModelIndexForCurrSkillType) - { - animationRandomCycleCount++; - if (currSkill != NULL - && animationRandomCycleCount >= currSkill->getAnimationCount ()) - { - animationRandomCycleCount = 0; - } - } - return result; - } - -//const Model *Unit::getCurrentModel() { -// if(currSkill == NULL) { -// char szBuf[8096]=""; -// snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->toString().c_str()); -// throw megaglest_runtime_error(szBuf); -// } -// -// int currentModelIndexForCurrSkillType = lastModelIndexForCurrSkillType; -// const Model *result = currSkill->getAnimation(getAnimProgressAsFloat(),this,&lastModelIndexForCurrSkillType, &animationRandomCycleCount); -// if(currentModelIndexForCurrSkillType != lastModelIndexForCurrSkillType) { -// animationRandomCycleCount++; -// if(currSkill != NULL && animationRandomCycleCount >= currSkill->getAnimationCount()) { -// animationRandomCycleCount = 0; -// } -// } -// return result; -//} - - bool Unit::checkModelStateInfoForNewHpValue () - { - bool result = false; - if (currSkill != NULL && currSkill->getAnimationCount () > 1) - { - if (lastModelIndexForCurrSkillType >= 0) - { - const AnimationAttributes attributes = - currSkill->getAnimationAttribute (lastModelIndexForCurrSkillType); - if (attributes.fromHp != 0 || attributes.toHp != 0) - { - //printf("Check for RESET model state for [%d - %s] HP = %d [%d to %d]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp); - //if(this->getHp() >= attributes.fromHp && this->getHp() <= attributes.toHp) { - if (this->getHp () < attributes.fromHp - || this->getHp () > attributes.toHp) - { - //printf("RESET model state for [%d - %s] HP = %d [%d to %d]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp); - - lastModelIndexForCurrSkillType = -1; - animationRandomCycleCount = 0; - result = true; - } - } - else - { - //printf("Check for RESET #2 model state for [%d - %s] HP = %d [%d to %d] for skill [%s]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp,currSkill->getName().c_str()); - } - } - } - - return result; - } - - Vec3f Unit::getCurrVectorForParticlesystems () const - { - if (getFaction ()->getType ()->isFlatParticlePositions ()) - { - return getCurrVectorFlat (); - } - else - { - return getCurrMidHeightVector (); - } - } - - Vec3f Unit::getCurrMidHeightVector () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - Vec3f result = - getCurrVectorFlat () + Vec3f (0.f, type->getHeight () / 2.f, 0.f); - result.x = truncateDecimal < float >(result.x, 6); - result.y = truncateDecimal < float >(result.y, 6); - result.z = truncateDecimal < float >(result.z, 6); - - return result; - } - - Vec3f Unit::getCurrVectorAsTarget () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - Vec3f result = - getCurrVectorFlat () + Vec3f (0.f, type->getTargetHeight () / 2.f, - 0.f); - result.x = truncateDecimal < float >(result.x, 6); - result.y = truncateDecimal < float >(result.y, 6); - result.z = truncateDecimal < float >(result.z, 6); - - return result; - } - - Vec3f Unit::getCurrBurnVector () const - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - Vec3f result = - getCurrVectorFlat () + Vec3f (0.f, type->getBurnHeight () / 2.f, 0.f); - result.x = truncateDecimal < float >(result.x, 6); - result.y = truncateDecimal < float >(result.y, 6); - result.z = truncateDecimal < float >(result.z, 6); - - return result; - } - - Vec3f Unit::getCurrVectorFlat () const - { - return getVectorFlat (lastPos, pos); - } - - float Unit::getProgressAsFloat () const - { - float result = - (static_cast < float >(progress) / static_cast < - float >(PROGRESS_SPEED_MULTIPLIER)); - result = truncateDecimal < float >(result, 6); - return result; - } - - Vec3f Unit::getVectorFlat (const Vec2i & lastPosValue, - const Vec2i & curPosValue) const - { - Vec3f v; - - float y1 = computeHeight (lastPosValue); - float y2 = computeHeight (curPosValue); - - if (currSkill->getClass () == scMove) - { - float progressAsFloat = getProgressAsFloat (); - - v.x = - lastPosValue.x + progressAsFloat * (curPosValue.x - lastPosValue.x); - v.z = - lastPosValue.y + progressAsFloat * (curPosValue.y - lastPosValue.y); - v.y = y1 + progressAsFloat * (y2 - y1); - - v.x = truncateDecimal < float >(v.x, 6); - v.y = truncateDecimal < float >(v.y, 6); - v.z = truncateDecimal < float >(v.z, 6); - } - else - { - v.x = static_cast < float >(curPosValue.x); - v.z = static_cast < float >(curPosValue.y); - v.y = y2; - - v.x = truncateDecimal < float >(v.x, 6); - v.y = truncateDecimal < float >(v.y, 6); - v.z = truncateDecimal < float >(v.z, 6); - } - v.x += type->getSize () / 2.f - 0.5f; - v.z += type->getSize () / 2.f - 0.5f; - - v.x = truncateDecimal < float >(v.x, 6); - v.z = truncateDecimal < float >(v.z, 6); - - return v; - } - -// =================== Command list related =================== - -//any command - bool Unit::anyCommand (bool validateCommandtype) const - { - bool result = false; - if (validateCommandtype == false) - { - result = (commands.empty () == false); - } - else - { - for (Commands::const_iterator it = commands.begin (); - it != commands.end (); ++it) - { - const CommandType *ct = (*it)->getCommandType (); - if (ct != NULL && ct->getClass () != ccStop) - { - result = true; - break; - } - } - } - - return result; - } - -//return current command, assert that there is always one command - Command *Unit::getCurrrentCommandThreadSafe () - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - if (commands.empty () == false) - { - return commands.front (); - } - - return NULL; - } - - void Unit::replaceCurrCommand (Command * cmd) - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - assert (commands.empty () == false); - commands.front () = cmd; - this->setCurrentUnitTitle (""); - } - -//returns the size of the commands - unsigned int Unit::getCommandSize () const - { - return (unsigned int) commands.size (); - } - -//return current command, assert that there is always one command - int Unit::getCountOfProducedUnits (const UnitType * ut) const - { - int count = 0; - for (Commands::const_iterator it = commands.begin (); - it != commands.end (); ++it) - { - const CommandType *ct = (*it)->getCommandType (); - if (ct->getClass () == ccProduce || ct->getClass () == ccMorph) - { - const UnitType *producedUnitType = - static_cast < const UnitType * >(ct->getProduced ()); - if (producedUnitType == ut) - { - count++; - } - } - if (ct->getClass () == ccBuild) - { - const UnitType *builtUnitType = (*it)->getUnitType (); - if (builtUnitType == ut) - { - count++; - } - } - } - return count; - } - -//give one command (clear, and push back) - std::pair < CommandResult, string > Unit::giveCommand (Command * command, - bool tryQueue) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "\n======================\nUnit Command tryQueue = %d\nUnit Info:\n%s\nCommand Info:\n%s\n", - tryQueue, this->toString ().c_str (), - command->toString (false).c_str ()); - - std::pair < CommandResult, string > result (crFailUndefined, ""); - changedActiveCommand = false; - - Chrono chrono; - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled) - chrono.start (); - - if (command == NULL) - { - throw megaglest_runtime_error ("command == NULL"); - } - if (command->getCommandType () == NULL) - { - throw megaglest_runtime_error ("command->getCommandType() == NULL"); - } - - const int command_priority = command->getPriority (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - //printf("In [%s::%s] Line: %d unit [%d - %s] command [%s] tryQueue = %d command->getCommandType()->isQueuable(tryQueue) = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getId(),this->getType()->getName().c_str(), command->getCommandType()->getName().c_str(), tryQueue,command->getCommandType()->isQueuable(tryQueue)); - - - if (command->getCommandType ()->isQueuable (tryQueue)) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - if (SystemFlags::getSystemSettingType - (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "In [%s::%s Line: %d] Command is Queable\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - if (command->getCommandType ()->isQueuable () == qAlways && tryQueue) - { - // Its a produce or upgrade command called without queued key - // in this case we must NOT delete lower priority commands! - // we just queue it! - - } - else - { - //Delete all lower-prioirty commands - for (list < Command * >::iterator i = commands.begin (); - i != commands.end ();) - { - if ((*i)->getPriority () < command_priority) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "In [%s::%s Line: %d] Deleting lower priority command [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - (*i)->toString (false).c_str ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - deleteQueuedCommand (*i); - i = commands.erase (i); - - safeMutex.ReleaseLock (); - } - else - { - ++i; - } - } - } - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - - //cancel current command if it is not queuable - if (commands.empty () == false && - commands.back ()->getCommandType ()->isQueueAppendable () == - false) - { - if (SystemFlags::getSystemSettingType - (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "In [%s::%s Line: %d] Cancel command because last one is NOT queable [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, - commands.back ()-> - toString (false).c_str ()); - - cancelCommand (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - } - else - { - //empty command queue - if (SystemFlags::getSystemSettingType - (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "In [%s::%s Line: %d] Clear commands because current is NOT queable.\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - bool willChangedActiveCommand = (commands.empty () == false); - if (willChangedActiveCommand == true) - { - - CommandClass currCommandClass = - getCurrCommand ()->getCommandType ()->getClass (); - CommandClass commandClass = command->getCommandType ()->getClass (); - if (currCommandClass == commandClass) - { - - willChangedActiveCommand = false; - } - else if (currCommandClass == ccAttack || - currCommandClass == ccAttackStopped || - commandClass == ccAttack || - commandClass == ccAttackStopped) - { - - willChangedActiveCommand = true; - } - else - { - willChangedActiveCommand = false; - } - } - - if (willChangedActiveCommand == true) - { - - lastChangedActiveCommandFrame = changedActiveCommandFrame; - changedActiveCommandFrame = getFrameCount (); - - //printf("Line: %d getCurrCommand() [%s] command [%s]\n",__LINE__,getCurrCommand()->toString(false).c_str(),command->toString(false).c_str()); - } - - clearCommands (); - changedActiveCommand = willChangedActiveCommand; - - //printf("In [%s::%s] Line: %d cleared existing commands\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, chrono.getMillis ()); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - //check command - result = checkCommand (command); - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugUnitCommands).enabled) - SystemFlags::OutputDebug (SystemFlags::debugUnitCommands, - "In [%s::%s Line: %d] checkCommand returned: [%d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - result.first); - - //printf("In [%s::%s] Line: %d check command returned %d, commands.size() = %d\n[%s]\n\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,result,commands.size(),command->toString().c_str()); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - if (result.first == crSuccess) - { - applyCommand (command); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - //push back command - if (result.first == crSuccess) - { - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - commands.push_back (command); - - safeMutex.ReleaseLock (); - } - else - { - delete command; - changedActiveCommand = false; - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugPerformance).enabled - && chrono.getMillis () > 0) - SystemFlags::OutputDebug (SystemFlags::debugPerformance, - "In [%s::%s] Line: %d took msecs: %lld\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, __LINE__, - chrono.getMillis ()); - - return result; - } - -//pop front (used when order is done) - CommandResult Unit::finishCommand () - { - changedActiveCommand = false; - retryCurrCommandCount = 0; - // Reset the progress when task completed. - resetProgress2 (); - this->setCurrentUnitTitle (""); - //is empty? - if (commands.empty ()) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - return crFailUndefined; - } - - //pop front - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - delete commands.front (); - commands.erase (commands.begin ()); - - safeMutex.ReleaseLock (true); - - this->unitPath->clear (); - - while (commands.empty () == false) - { - if (commands.front ()->getUnit () != NULL - && this->faction->isUnitInLivingUnitsp (commands. - front ()->getUnit ()) == - false) - { - safeMutex.Lock (); - delete commands.front (); - commands.erase (commands.begin ()); - safeMutex.ReleaseLock (true); - } - else - { - break; - } - } - - return crSuccess; - } - -//to cancel a command - CommandResult Unit::cancelCommand () - { - changedActiveCommand = false; - retryCurrCommandCount = 0; - - this->setCurrentUnitTitle (""); - - //is empty? - if (commands.empty ()) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - return crFailUndefined; - } - - //undo command - undoCommand (commands.back ()); - - //delete ans pop command - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - delete commands.back (); - commands.pop_back (); - - // Reset the progress if the last command in the queue was cancelled. - // We don't want to reset the progress if we're not removing the last command, - // because then the progress of the actual command in progress. - /// TODO: extra if statement below needed adding make the reset function properly. Can this be avoided? - if (commands.empty()) resetProgress2(); - - safeMutex.ReleaseLock (); - - //clear routes - this->unitPath->clear (); - - - - return crSuccess; - } - -// =================== route stack =================== - - void Unit::create (bool startingUnit) - { - faction->addUnit (this); - map->putUnitCells (this, pos); - if (startingUnit) - { - faction->applyStaticCosts (type, NULL); - } - } - - void Unit::born (const CommandType * ct) - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - faction->addStore (type); - faction->applyStaticProduction (type, ct); - setCurrSkill (scStop); - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - - - //set hp from start hp - checkItemInVault (&this->ep, this->ep); - if (type->getStartHpType () == UnitType::stValue) - { - this->hp = type->getStartHpValue (); - } - else - { - this->hp = - type->getTotalMaxHp (&totalUpgrade) * - type->getStartHpPercentage () / 100; - } - - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - //set ep from start ep - checkItemInVault (&this->ep, this->ep); - if (type->getStartEpType () == UnitType::stValue) - { - this->ep = type->getStartEpValue (); - } - else - { - this->ep = - type->getTotalMaxEp (&totalUpgrade) * - type->getStartEpPercentage () / 100; - } - } - - void Unit::kill () - { - //no longer needs static resources - if (isBeingBuilt ()) - { - faction->deApplyStaticConsumption (type, - (getCurrCommand () != - NULL ? - getCurrCommand ()->getCommandType - () : NULL)); - } - else - { - faction->deApplyStaticCosts (type, - (getCurrCommand () != - NULL ? - getCurrCommand ()->getCommandType () : - NULL)); - } - - //do the cleaning - //clear commands ( and their blocking fields ) - clearCommands (); - - map->clearUnitCells (this, pos, true); - if (isBeingBuilt () == false) - { - faction->removeStore (type); - } - setCurrSkill (scDie); - - notifyObservers (UnitObserver::eKill); - - - UnitUpdater *unitUpdater = game->getWorld ()->getUnitUpdater (); - //unitUpdater->clearUnitPrecache(this); - unitUpdater->removeUnitPrecache (this); - } - - void Unit::undertake () - { - try - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] about to undertake unit id = %d [%s] [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, this->id, - this->getFullName (false).c_str (), - this->getDesc (false).c_str ()); - - // Remove any units that were previously in attack-boost range - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false - && currentAttackBoostOriginatorEffect.skillType != NULL) - { - for (unsigned int i = 0; - i < - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size (); ++i) - { - // Remove attack boost upgrades from unit - int findUnitId = - currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; - Unit *affectedUnit = game->getWorld ()->findUnitById (findUnitId); - if (affectedUnit != NULL) - { - affectedUnit->deapplyAttackBoost - (currentAttackBoostOriginatorEffect.skillType-> - getAttackBoost (), this); - } - - //printf("!!!! DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - currentAttackBoostOriginatorEffect.currentAttackBoostUnits.clear (); - } - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - UnitUpdater *unitUpdater = game->getWorld ()->getUnitUpdater (); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - //unitUpdater->clearUnitPrecache(this); - unitUpdater->removeUnitPrecache (this); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - this->faction->deleteLivingUnits (id); - this->faction->deleteLivingUnitsp (this); - - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugSystem).enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - - faction->removeUnit (this); - } - catch (const megaglest_runtime_error & ex) - { - string sErrBuf = ""; - if (ex.wantStackTrace () == true) - { - char szErrBuf[8096] = ""; - snprintf (szErrBuf, 8096, "In [%s::%s %d]", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - sErrBuf = - string (szErrBuf) + string ("\nerror [") + string (ex.what ()) + - string ("]\n"); - } - else - { - sErrBuf = ex.what (); - } - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - - throw; - } - catch (const std::exception & ex) - { - char szErrBuf[8096] = ""; - snprintf (szErrBuf, 8096, "In [%s::%s %d]", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__); - string sErrBuf = - string (szErrBuf) + string ("\nerror [") + string (ex.what ()) + - string ("]\n"); - SystemFlags::OutputDebug (SystemFlags::debugError, sErrBuf.c_str ()); - - throw; - } - } - -// =================== Referencers =================== - - void Unit::addObserver (UnitObserver * unitObserver) - { - observers.push_back (unitObserver); - } - -//void Unit::removeObserver(UnitObserver *unitObserver){ -// observers.remove(unitObserver); -//} - - void Unit::notifyObservers (UnitObserver::Event event) - { - for (Observers::iterator it = observers.begin (); - it != observers.end (); ++it) - { - (*it)->unitEvent (event, this); - } - } - -// =================== Other =================== - - void Unit::resetHighlight () - { - highlight = 1.f; - } - - const CommandType *Unit::computeCommandType (const Vec2i & pos, - const Unit * - targetUnit) const - { - const CommandType *commandType = NULL; - - if (map->isInside (pos) == false - || map->isInsideSurface (map->toSurfCoords (pos)) == false) - { - throw megaglest_runtime_error ("#6 Invalid path position = " + - pos.getString ()); - } - - SurfaceCell *sc = map->getSurfaceCell (Map::toSurfCoords (pos)); - - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //printf("Line: %d Unit::computeCommandType pos [%s] targetUnit [%s]\n",__LINE__,pos.getString().c_str(),(targetUnit != NULL ? targetUnit->getType()->getName().c_str() : "(null)")); - if (targetUnit != NULL) - { - //attack enemies - if (isAlly (targetUnit) == false) - { - commandType = - type->getFirstAttackCommand (targetUnit->getCurrField ()); - } - //repair allies - else - { - if (targetUnit->isBuilt () == false - || targetUnit->isDamaged () == true) - { - commandType = - type->getFirstRepairCommand (targetUnit->getType ()); - } - - //printf("Line: %d Unit::computeCommandType pos [%s] targetUnit [%s] commandType [%p]\n",__LINE__,pos.getString().c_str(),(targetUnit != NULL ? targetUnit->getType()->getName().c_str() : "(null)"),commandType); - - if (commandType == NULL && targetUnit != NULL) - { - //Command *command= this->getCurrCommand(); - //const HarvestCommandType *hct= dynamic_cast((command != NULL ? command->getCommandType() : NULL)); - - // Check if we can return whatever resources we have - if (targetUnit->getFactionIndex () == this->getFactionIndex () && - targetUnit->isOperative () == true && - this->getLoadType () != NULL && this->getLoadCount () != 0 && - targetUnit->getType () != NULL && - targetUnit->getType ()->getStore (this->getLoadType ()) > 0) - { - - commandType = type->getFirstHarvestEmergencyReturnCommand (); - } - } - } - } - else - { - //check harvest command - Resource *resource = sc->getResource (); - if (resource != NULL) - { - commandType = - type->getFirstHarvestCommand (resource->getType (), - this->getFaction ()); - } - } - - if (targetUnit == NULL && commandType == NULL) - { - const Vec2i unitTargetPos = pos; - Cell *cell = map->getCell (unitTargetPos); - if (cell != NULL && cell->getUnit (this->getCurrField ()) != NULL) - { - Unit *targetUnit = cell->getUnit (this->getCurrField ()); - if (targetUnit != NULL) - { - // Check if we want to help build (repair) any buildings instead of just moving - if (targetUnit->getFactionIndex () == this->getFactionIndex () && - (targetUnit->isBuilt () == false - || targetUnit->isDamaged () == true)) - { - const RepairCommandType *rct = - this->getType ()-> - getFirstRepairCommand (targetUnit->getType ()); - if (rct != NULL) - { - commandType = - type->getFirstRepairCommand (targetUnit->getType ()); - //printf("************ Unit will repair building built = %d, repair = %d\n",targetUnit->isBuilt(),targetUnit->isDamaged()); - } - } - } - } - } - - // Default command is the class of the unit (i.e. attack for attackers, walk for walkers). - // The default command is executed when a unit is produced and sent to a meeting point - // or when the unit is selected and right clicked to a position. - if (commandType == NULL) - { - // move - // commandType = type->getFirstCtOfClass (ccMove); - - // attack - // Is the unit class warrior? if yes, attack by default, else walk. - commandType = - type->getFirstCtOfClass (this->getType ()-> - isOfClass (ucWarrior) ? ccAttack : ccMove); - - // FIXME: I think a better solution would be to have a hotkey for this, - // the user can decide, and toggle in-game -andy5995 2018-02-03 - } - - return commandType; - } - - int64 Unit::getUpdateProgress () - { - if (progress > PROGRESS_SPEED_MULTIPLIER) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: progress > " MG_I64_SPECIFIER - ", progress = [" MG_I64_SPECIFIER "]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, PROGRESS_SPEED_MULTIPLIER, - progress); - throw megaglest_runtime_error (szBuf); - } - - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - int64 newProgress = progress; - if (currSkill->getClass () != scDie) - { - //speed - int speed = currSkill->getTotalSpeed (&totalUpgrade); - - if (changedActiveCommand == true) - { - if (changedActiveCommandFrame - lastChangedActiveCommandFrame >= - MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED) - { - //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] skill [%s] command [%s]\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,currSkill->toString(false).c_str(),getCurrCommand()->toString(false).c_str()); - //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] total = %u\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,(changedActiveCommandFrame - lastChangedActiveCommandFrame)); - speed = CHANGE_COMMAND_SPEED; - } - } - - //speed modifier - int64 diagonalFactor = getDiagonalFactor (); - int64 heightFactor = getHeightFactor (PROGRESS_SPEED_MULTIPLIER); - - //update progresses -// const Game *game = Renderer::getInstance().getGame(); -// if(game == NULL) { -// throw megaglest_runtime_error("game == NULL"); -// } -// if(game->getWorld() == NULL) { -// throw megaglest_runtime_error("game->getWorld() == NULL"); -// } - - newProgress = getUpdatedProgress (progress, GameConstants::updateFps, - speed, diagonalFactor, - heightFactor); - - } - return newProgress; - } - - bool Unit::needToUpdate () - { - bool return_value = false; - if (currSkill->getClass () != scDie) - { - int newProgress = getUpdateProgress (); - if (newProgress >= PROGRESS_SPEED_MULTIPLIER) - { - return_value = true; - } - } - return return_value; - } - - int64 Unit::getDiagonalFactor () - { - //speed modifier - int64 diagonalFactor = PROGRESS_SPEED_MULTIPLIER; - if (currSkill->getClass () == scMove) - { - //if moving in diagonal move slower - Vec2i dest = pos - lastPos; - if (abs (dest.x) + abs (dest.y) == 2) - { - //diagonalFactor = 0.71f * PROGRESS_SPEED_MULTIPLIER; - diagonalFactor = 71 * (PROGRESS_SPEED_MULTIPLIER / 100); - } - } - return diagonalFactor; - } - - int64 Unit::getHeightFactor (int64 speedMultiplier) - { - int64 heightFactor = speedMultiplier; - if (currSkill->getClass () == scMove) - { - //if moving to an higher cell move slower else move faster - Cell *unitCell = map->getCell (pos); - if (unitCell == NULL) - { - throw megaglest_runtime_error ("unitCell == NULL"); - } - - Cell *targetCell = map->getCell (targetPos); - if (targetCell == NULL) - { - throw megaglest_runtime_error ("targetCell == NULL"); - } - - int64 heightDiff = ((truncateDecimal < float >(unitCell->getHeight (), - 2) * - speedMultiplier) -(truncateDecimal < - float >(targetCell-> - getHeight (), - 2) * - speedMultiplier)); - //heightFactor= clamp(speedMultiplier + heightDiff / (5.f * speedMultiplier), 0.2f * speedMultiplier, 5.f * speedMultiplier); - heightFactor = - clamp (speedMultiplier + heightDiff / (5 * speedMultiplier), - (2 * (speedMultiplier / 10)), 5 * speedMultiplier); - } - - return heightFactor; - } - - int64 Unit::getSpeedDenominator (int64 updateFPS) - { - int64 speedDenominator = - (speedDivider * updateFPS) * PROGRESS_SPEED_MULTIPLIER; - return speedDenominator; - } - int64 Unit::getUpdatedProgress (int64 currentProgress, int64 updateFPS, - int64 speed, int64 diagonalFactor, - int64 heightFactor) - { - - int64 speedDenominator = getSpeedDenominator (updateFPS); - int64 newProgress = currentProgress; - int64 progressIncrease = - ((speed * diagonalFactor * heightFactor) / speedDenominator); - // Ensure we increment at least a value of 1 of the action will be stuck infinitely - if (speed > 0 && diagonalFactor > 0 && heightFactor > 0 - && progressIncrease == 0) - { - progressIncrease = 1; - } - - if (isNetworkCRCEnabled () == true) - { - float height = map->getCell (pos)->getHeight (); - float airHeight = game->getWorld ()->getTileset ()->getAirHeight (); - int cellUnitHeight = -1; - int cellObjectHeight = -1; - - Unit *unit = map->getCell (pos)->getUnit (fLand); - if (unit != NULL && unit->getType ()->getHeight () > airHeight) - { - cellUnitHeight = unit->getType ()->getHeight (); - } - else - { - SurfaceCell *sc = map->getSurfaceCell (map->toSurfCoords (pos)); - if (sc != NULL && sc->getObject () != NULL - && sc->getObject ()->getType () != NULL) - { - if (sc->getObject ()->getType ()->getHeight () > airHeight) - { - cellObjectHeight = sc->getObject ()->getType ()->getHeight (); - } - } - } - - - char szBuf[8096] = ""; - snprintf (szBuf, 8095, - "currentProgress = " MG_I64_SPECIFIER " updateFPS = " - MG_I64_SPECIFIER " speed = " MG_I64_SPECIFIER - " diagonalFactor = " MG_I64_SPECIFIER " heightFactor = " - MG_I64_SPECIFIER " speedDenominator = " MG_I64_SPECIFIER - " progressIncrease = " MG_I64_SPECIFIER " [" - MG_I64_SPECIFIER - "] height [%f] airHeight [%f] cellUnitHeight [%d] cellObjectHeight [%d] skill [%s] pos [%s] lastpos [%s]", - currentProgress, updateFPS, speed, diagonalFactor, - heightFactor, speedDenominator, progressIncrease, - ((speed * diagonalFactor * heightFactor) / - speedDenominator), height, airHeight, cellUnitHeight, - cellObjectHeight, - (currSkill != - NULL ? currSkill->getName ().c_str () : "none"), - pos.getString ().c_str (), lastPos.getString ().c_str ()); - networkCRCLogInfo = szBuf; - - //printf("%s\n",szBuf); - } - - newProgress += progressIncrease; - -// if(currSkill->getClass() == scMove || (currSkill->getClass() == scStop && this->loadCount > 0)) { -// printf("speedDenominator: " MG_I64_SPECIFIER " currentProgress: " MG_I64_SPECIFIER " speed: " MG_I64_SPECIFIER " diagonalFactor: " MG_I64_SPECIFIER " heightFactor: " MG_I64_SPECIFIER " progressIncrease: " MG_I64_SPECIFIER " newProgress: " MG_I64_SPECIFIER " TOP #: " MG_I64_SPECIFIER "\n",speedDenominator,currentProgress,speed,diagonalFactor,heightFactor,progressIncrease,newProgress,(speed * diagonalFactor * heightFactor)); -// } - - return newProgress; - } - - void Unit::updateAttackBoostProgress (const Game * game) - { - const bool debugBoost = false; - if (debugBoost) - printf - ("===================== START Unit [%d - %s] skill: %s affected unit size: " - MG_SIZE_T_SPECIFIER "\n", this->id, - this->getType ()->getName (false).c_str (), - currSkill->getBoostDesc (false).c_str (), - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size ()); - - if (currSkill != currentAttackBoostOriginatorEffect.skillType) - { - - if (debugBoost) - printf ("Line: %d new [%s]\n", __LINE__, - (currentAttackBoostOriginatorEffect.skillType != - NULL ? currentAttackBoostOriginatorEffect. - skillType->getBoostDesc (false).c_str () : "")); - - if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) - { - delete currentAttackBoostOriginatorEffect.currentAppliedEffect; - currentAttackBoostOriginatorEffect.currentAppliedEffect = NULL; - - //printf("- #1 DE-APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); - } - - // Remove any units that were previously in range - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false - && currentAttackBoostOriginatorEffect.skillType != NULL) - { - for (unsigned int i = 0; - i < - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size (); ++i) - { - // Remove attack boost upgrades from unit - - int findUnitId = - currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; - Unit *affectedUnit = game->getWorld ()->findUnitById (findUnitId); - if (affectedUnit != NULL) - { - affectedUnit->deapplyAttackBoost - (currentAttackBoostOriginatorEffect.skillType-> - getAttackBoost (), this); - } - - //printf("- #1 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - currentAttackBoostOriginatorEffect.currentAttackBoostUnits.clear (); - } - - if (debugBoost) - printf ("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", - __LINE__, - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size ()); - - currentAttackBoostOriginatorEffect.skillType = currSkill; - - if (currSkill->isAttackBoostEnabled () == true) - { - if (debugBoost) - printf ("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", - __LINE__, - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size ()); - - // Search for units in range of this unit which apply to the - // attack-boost and temporarily upgrade them - UnitUpdater *unitUpdater = - this->game->getWorld ()->getUnitUpdater (); - - const AttackBoost *attackBoost = currSkill->getAttackBoost (); - vector < Unit * >candidates = unitUpdater->findUnitsInRange (this, - attackBoost->radius); - - if (debugBoost) - printf ("Line: %d candidates unit size: " MG_SIZE_T_SPECIFIER - " attackBoost: %s\n", __LINE__, candidates.size (), - attackBoost->getDesc (false).c_str ()); - for (unsigned int i = 0; i < candidates.size (); ++i) - { - Unit *affectedUnit = candidates[i]; - if (attackBoost->isAffected (this, affectedUnit) == true) - { - if (affectedUnit->applyAttackBoost (attackBoost, this) == true) - { - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.push_back (affectedUnit->getId ()); - //printf("+ #1 APPLY ATTACK BOOST to unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - } - } - - if (debugBoost) - printf ("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", - __LINE__, - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size ()); - - if (showUnitParticles == true) - { - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false) - { - if (attackBoost->unitParticleSystemTypeForSourceUnit != NULL) - { - currentAttackBoostOriginatorEffect.currentAppliedEffect = - new UnitAttackBoostEffect (); - currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst = new UnitParticleSystemType (); - *currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst = - *attackBoost->unitParticleSystemTypeForSourceUnit; - //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; - - currentAttackBoostOriginatorEffect.currentAppliedEffect->ups = - new UnitParticleSystem (200); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setParticleOwner (this); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setParticleType - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - upst); - - currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst-> - setValues - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setPos (getCurrVectorForParticlesystems ()); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem - (currentAttackBoostOriginatorEffect. - currentAppliedEffect->ups); - - if (getFaction ()->getTexture ()) - { - currentAttackBoostOriginatorEffect. - currentAppliedEffect->ups-> - setFactionColor (getFaction ()->getTexture ()-> - getPixmapConst ()->getPixel3f (0, 0)); - } - Renderer:: - getInstance ().manageParticleSystem - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups, rsGame); - //printf("+ #1 APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); - } - } - } - } - } - else - { - if (currSkill->isAttackBoostEnabled () == true) - { - if (debugBoost) - printf ("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", - __LINE__, - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size ()); - - // Search for units in range of this unit which apply to the - // attack-boost and temporarily upgrade them - UnitUpdater *unitUpdater = - this->game->getWorld ()->getUnitUpdater (); - - const AttackBoost *attackBoost = currSkill->getAttackBoost (); - vector < Unit * >candidates = - unitUpdater->findUnitsInRange (this, attackBoost->radius); - vector < int >candidateValidIdList; - candidateValidIdList.reserve (candidates.size ()); - - if (debugBoost) - printf ("Line: %d candidates unit size: " MG_SIZE_T_SPECIFIER - " attackBoost: %s\n", __LINE__, candidates.size (), - attackBoost->getDesc (false).c_str ()); - - for (unsigned int i = 0; i < candidates.size (); ++i) - { - Unit *affectedUnit = candidates[i]; - candidateValidIdList.push_back (affectedUnit->getId ()); - - std::vector < int >::iterator iterFound = - std:: - find - (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - begin (), - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - end (), - affectedUnit->getId ()); - - if (attackBoost->isAffected (this, affectedUnit) == true) - { - if (iterFound == - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.end ()) - { - if (affectedUnit->applyAttackBoost (attackBoost, this) == - true) - { - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.push_back (affectedUnit-> - getId ()); - - //printf("+ #2 APPLY ATTACK BOOST to unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - } - } - else - { - if (iterFound != - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.end ()) - { - affectedUnit->deapplyAttackBoost - (currentAttackBoostOriginatorEffect.skillType-> - getAttackBoost (), this); - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.erase (iterFound); - - //printf("- #2 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - } - } - - // Now remove any units that were in the list of boosted units but - // are no longer in range - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false) - { - for (int i = - (int) - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size () - 1; i >= 0; --i) - { - int findUnitId = - currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; - - std::vector < int >::iterator iterFound = - std::find (candidateValidIdList.begin (), - candidateValidIdList.end (), - findUnitId); - if (iterFound == candidateValidIdList.end ()) - { - Unit *affectedUnit = - game->getWorld ()->findUnitById (findUnitId); - if (affectedUnit != NULL) - { - affectedUnit->deapplyAttackBoost - (currentAttackBoostOriginatorEffect.skillType-> - getAttackBoost (), this); - - if (debugBoost) - printf - ("Removed attack boost from Unit [%d - %s] since they are NO LONGER in range\n", - affectedUnit->id, - affectedUnit->getType ()->getName (false).c_str ()); - - } - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.erase - (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - begin () + i); - } - } - } - - if (debugBoost) - printf ("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", - __LINE__, - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size ()); - - if (showUnitParticles == true) - { - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false) - { - if (attackBoost->unitParticleSystemTypeForSourceUnit != NULL - && currentAttackBoostOriginatorEffect.currentAppliedEffect - == NULL) - { - - currentAttackBoostOriginatorEffect.currentAppliedEffect = - new UnitAttackBoostEffect (); - currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst = new UnitParticleSystemType (); - *currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst = - *attackBoost->unitParticleSystemTypeForSourceUnit; - //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; - - currentAttackBoostOriginatorEffect.currentAppliedEffect->ups = - new UnitParticleSystem (200); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setParticleOwner (this); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setParticleType - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - upst); - - currentAttackBoostOriginatorEffect. - currentAppliedEffect->upst-> - setValues - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setPos (getCurrVectorForParticlesystems ()); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem - (currentAttackBoostOriginatorEffect. - currentAppliedEffect->ups); - - if (getFaction ()->getTexture ()) - { - currentAttackBoostOriginatorEffect. - currentAppliedEffect->ups-> - setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - Renderer:: - getInstance ().manageParticleSystem - (currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups, rsGame); - - //printf("+ #2 APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); - } - } - else - if (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - empty () == true) - { - if (currentAttackBoostOriginatorEffect.currentAppliedEffect != - NULL) - { - delete - currentAttackBoostOriginatorEffect.currentAppliedEffect; - currentAttackBoostOriginatorEffect.currentAppliedEffect = - NULL; - - //printf("- #2 DE-APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); - } - } - } - } - } - - if (debugBoost) - { - if (currSkill->isAttackBoostEnabled () == true) - { - printf ("Unit [%d - %s] has attackboost enabled: %s\n", this->id, - this->getType ()->getName (false).c_str (), - currSkill->getBoostDesc (false).c_str ()); - - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false) - { - printf - ("Found affected units currentAttackBoostOriginatorEffect.skillType [%p]\n", - currentAttackBoostOriginatorEffect.skillType); - - for (unsigned int i = 0; - i < - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size (); ++i) - { - int unitnum = - currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; - printf ("affected unit #: %u - %d\n", i, unitnum); - } - } - } - } - } - - bool Unit::update () - { - assert (progress <= PROGRESS_SPEED_MULTIPLIER); - - //highlight - if (highlight > 0.f) - { - const Game *game = Renderer::getInstance ().getGame (); - highlight -= - 1.f / (Game::highlightTime * - game->getWorld ()->getUpdateFps (this->getFactionIndex ())); - } - - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //speed - int speed = currSkill->getTotalSpeed (&totalUpgrade); - - if (oldTotalSight != - getType ()->getTotalSight (this->getTotalUpgrade ())) - { - oldTotalSight = getType ()->getTotalSight (this->getTotalUpgrade ()); - // refresh FogOfWar and so on, because sight ha changed since last update - refreshPos (true); - } - - if (changedActiveCommand) - { - if (changedActiveCommandFrame - lastChangedActiveCommandFrame >= - MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED) - { - //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] skill [%s] command [%s]\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,currSkill->toString(false).c_str(),getCurrCommand()->toString(false).c_str()); - //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] total = %u\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,(changedActiveCommandFrame - lastChangedActiveCommandFrame)); - speed = CHANGE_COMMAND_SPEED; - } - } - - //speed modifier - int64 diagonalFactor = getDiagonalFactor (); - int64 heightFactor = getHeightFactor (PROGRESS_SPEED_MULTIPLIER); - - //update progresses - this->lastAnimProgress = this->animProgress; - const Game *game = Renderer::getInstance ().getGame (); - - if (animProgress == 0) - { - AnimCycleStarts (); - } - progress = getUpdatedProgress (progress, - GameConstants::updateFps, - speed, diagonalFactor, heightFactor); - - //printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str()); - - if (isAnimProgressBound () == true) - { - float targetProgress = 0; - if (currSkill->getClass () == scBeBuilt) - { - targetProgress = this->getHpRatio (); - } - if (currSkill->getClass () == scProduce) - { - targetProgress = this->getProgressRatio (); - } - if (currSkill->getClass () == scUpgrade) - { - targetProgress = this->getProgressRatio (); - } - if (currSkill->getClass () == scMorph) - { - targetProgress = this->getProgressRatio (); - } - - float targetProgressIntValue = - targetProgress * ANIMATION_SPEED_MULTIPLIER; - if (this->animProgress < targetProgressIntValue) - { - float diff = targetProgressIntValue - this->animProgress; - float progressIncrease = - static_cast < float >(this->animProgress) + diff / static_cast < - float >(GameConstants::updateFps); - // Ensure we increment at least a value of 1 of the action will be stuck infinitely - if (diff > 0.f && GameConstants::updateFps > 0 - && progressIncrease == 0.f) - { - progressIncrease = 1.f; - } - - //if(currSkill->getClass() == scBeBuilt) { - // printf("targetProgress: %.10f this->animProgress: %d diff: %.10f GameConstants::updateFps: %d progressIncrease: %.10f\n",targetProgress,this->animProgress,diff,GameConstants::updateFps,progressIncrease); - //} - - this->animProgress = progressIncrease; - - //if(currSkill->getClass() == scBeBuilt) { - // printf("Unit build progress: %d anim: %d\n",progress,this->animProgress); - //} - } - } - else - { - int64 heightFactor = getHeightFactor (ANIMATION_SPEED_MULTIPLIER); - int64 speedDenominator = speedDivider * - game->getWorld ()->getUpdateFps (this->getFactionIndex ()); - - // Override the animation speed for attacks that have upgraded the attack speed - int animSpeed = currSkill->getAnimSpeed (); - if (currSkill->getClass () == scAttack) - { - int animSpeedBoost = - ((AttackSkillType *) - currSkill)->getAnimSpeedBoost (&totalUpgrade); - animSpeed += animSpeedBoost; - } - - int64 progressIncrease = - (animSpeed * heightFactor) / speedDenominator; - // Ensure we increment at least a value of 1 of the action will be stuck infinitely - if (currSkill->getAnimSpeed () > 0 && heightFactor > 0 - && progressIncrease == 0) - { - progressIncrease = 1; - } - this->animProgress += progressIncrease; - //this->animProgress += (currSkill->getAnimSpeed() * heightFactor) / speedDenominator; - - //if(currSkill->getClass() == scDie) { - // printf("Unit died progress: %d anim: %d\n",progress,this->animProgress); - //} - } - //update target - updateTarget (); - - //rotation - if (currSkill->getClass () != scStop) - { - const int rotFactor = 2; - if (getProgressAsFloat () < 1.f / rotFactor) - { - if (type->getFirstStOfClass (scMove)) - { - if (abs ((int) (lastRotation - targetRotation)) < 180) - rotation = lastRotation + (targetRotation - lastRotation) * - getProgressAsFloat () * rotFactor; - else - { - float rotationTerm = - targetRotation > lastRotation ? -360.f : +360.f; - rotation = - lastRotation + (targetRotation - lastRotation + - rotationTerm) * getProgressAsFloat () * - rotFactor; - } - } - } - } - - if (type->getProperty (UnitType::pRotatedClimb)) - { - calculateXZRotation (); - } - else - { - rotationZ = .0f; - rotationX = .0f; - } - - if (Renderer:: - getInstance ().validateParticleSystemStillExists (this->fire, - rsGame) == false) - { - this->fire = NULL; - } - - if (this->fire != NULL) - { - this->fire->setPos (getCurrBurnVector ()); - } - for (UnitParticleSystems::iterator it = unitParticleSystems.begin (); - it != unitParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == true) - { - (*it)->setPos (getCurrVectorForParticlesystems ()); - (*it)->setRotation (getRotation ()); - setMeshPosInParticleSystem (*it); - } - } - for (UnitParticleSystems::iterator it = damageParticleSystems.begin (); - it != damageParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == true) - { - (*it)->setPos (getCurrVectorForParticlesystems ()); - (*it)->setRotation (getRotation ()); - setMeshPosInParticleSystem (*it); - } - } - - for (UnitParticleSystems::iterator it = smokeParticleSystems.begin (); - it != smokeParticleSystems.end (); ++it) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists ((*it), - rsGame) == true) - { - (*it)->setPos (getCurrMidHeightVector ()); - (*it)->setRotation (getRotation ()); - setMeshPosInParticleSystem (*it); - } - } - - //printf("Unit has attack boost? unit = [%d - %s] size = %d\n",this->getId(), this->getType()->getName(false).c_str(),(int)currentAttackBoostEffects.size()); - for (unsigned int i = 0; i < currentAttackBoostEffects.size (); ++i) - { - UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; - if (effect != NULL && effect->ups != NULL) - { - bool particleValid = - Renderer:: - getInstance ().validateParticleSystemStillExists (effect->ups, - rsGame); - if (particleValid == true) - { - effect->ups->setPos (getCurrVectorForParticlesystems ()); - effect->ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (effect->ups); - } - - //printf("i = %d particleValid = %d\n",i,particleValid); - } - //printf("i = %d effect = %p effect->ups = %p\n",i,effect,(effect ? effect->ups : NULL)); - } - - - if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) - { - if (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups != - NULL) - { - bool particleValid = - Renderer::getInstance ().validateParticleSystemStillExists - (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups, - rsGame); - if (particleValid == true) - { - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setPos (getCurrVectorForParticlesystems ()); - currentAttackBoostOriginatorEffect.currentAppliedEffect-> - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem - (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups); - } - } - } - - //checks - if (this->animProgress > ANIMATION_SPEED_MULTIPLIER) - { - bool canCycle = - currSkill->CanCycleNextRandomAnimation (&animationRandomCycleCount); - this->animProgress = - currSkill->getClass () == scDie ? ANIMATION_SPEED_MULTIPLIER : 0; - if (canCycle == true) - { - this->lastModelIndexForCurrSkillType = -1; - } - } - - bool return_value = false; - //checks - if (progress >= PROGRESS_SPEED_MULTIPLIER) - { - lastRotation = targetRotation; - if (currSkill->getClass () != scDie) - { - progress = 0; - return_value = true; - } - else - { - progress = PROGRESS_SPEED_MULTIPLIER; - deadCount++; - if (deadCount >= maxDeadCount) - { - toBeUndertaken = true; - return_value = false; - } - } - } - - updateAttackBoostProgress (game); - - if (return_value) - { - changedActiveCommand = false; - } - - return return_value; - } - - void Unit::updateTimedParticles () - { - //!!! - // Start new particle systems based on start time - if (queuedUnitParticleSystemTypes.empty () == false) - { - for (int i = (int)queuedUnitParticleSystemTypes.size () - 1; i >= 0; - i--) - { - UnitParticleSystemType *pst = queuedUnitParticleSystemTypes[i]; - if (pst != NULL) - { - if (truncateDecimal < float >(pst->getStartTime (), 6) <= - truncateDecimal < float >(getAnimProgressAsFloat (), 6)) - { - - UnitParticleSystem *ups = new UnitParticleSystem (200); - ups->setParticleOwner (this); - ups->setParticleType (pst); - - pst->setValues (ups); - ups->setPos (getCurrVectorForParticlesystems ()); - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (ups); - - if (getFaction ()->getTexture ()) - { - ups->setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - unitParticleSystems.push_back (ups); - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - - queuedUnitParticleSystemTypes.erase - (queuedUnitParticleSystemTypes.begin () + i); - } - } - else - { - queuedUnitParticleSystemTypes.erase - (queuedUnitParticleSystemTypes.begin () + i); - } - } - } - - // End existing systems based on end time - if (unitParticleSystems.empty () == false) - { - for (int i = (int)unitParticleSystems.size () - 1; i >= 0; i--) - { - UnitParticleSystem *ps = unitParticleSystems[i]; - if (ps != NULL) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - float pst = ps->getStartTime (); - float pet = ps->getEndTime (); - float particleStartTime = truncateDecimal < float >(pst, 6); - float particleEndTime = truncateDecimal < float >(pet, 6); - - if (particleStartTime != 0.0 || particleEndTime != 1.0) - { - float animProgressTime = - truncateDecimal < float >(getAnimProgressAsFloat (), 6); - if (animProgressTime >= 0.99 - || animProgressTime >= particleEndTime) - { - - ps->fade (); - unitParticleSystems.erase (unitParticleSystems.begin () + - i); - } - } - } - } - } - } - } - - bool Unit::unitHasAttackBoost (const AttackBoost * boost, - const Unit * source) - { - bool result = false; - for (unsigned int i = 0; i < currentAttackBoostEffects.size (); ++i) - { - UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; - if (effect != NULL && effect->boost->name == boost->name && - effect->getSource ()->getType ()->getId () == - source->getType ()->getId ()) - { - result = true; - break; - } - } - - //printf("Unit has attack boost? source = [%d - %s] [%p] boost [%s] result = %d\n",source->getId(), source->getType()->getName(false).c_str(),source,boost->name.c_str(),result); - - return result; - } - - bool Unit::applyAttackBoost (const AttackBoost * boost, - const Unit * source) - { - if (boost == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: boost == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //printf("APPLYING ATTACK BOOST to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); - - bool shouldApplyAttackBoost = true; - if (boost->allowMultipleBoosts == false) - { - // Check if we already have this boost from this unit type and multiples - // are not allowed - bool alreadyHasAttackBoost = this->unitHasAttackBoost (boost, source); - if (alreadyHasAttackBoost == true) - { - shouldApplyAttackBoost = false; - } - } - - if (shouldApplyAttackBoost == true) - { - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("=== APPLYING ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", - this->getType ()->getName (false).c_str (), this->getId (), - source->getType ()->getName (false).c_str (), source->getId (), - hp); - - UnitAttackBoostEffect *effect = new UnitAttackBoostEffect (); - effect->boost = boost; - effect->setSource (source); - - bool wasAlive = alive; - int originalHp = hp; - int prevMaxHp = totalUpgrade.getMaxHp (); - int prevMaxHpRegen = totalUpgrade.getMaxHpRegeneration (); - //printf("#1 wasAlive = %d hp = %d boosthp = %d\n",wasAlive,hp,boost->boostUpgrade.getMaxHp()); - - totalUpgrade.apply (source->getId (), &boost->boostUpgrade, this); - - checkItemInVault (&this->hp, this->hp); - //hp += boost->boostUpgrade.getMaxHp(); - int original_hp = this->hp; - this->hp += (totalUpgrade.getMaxHp () - prevMaxHp); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - //regenerate hp upgrade / or boost - if (totalUpgrade.getMaxHpRegeneration () != 0) - { - checkItemInVault (&this->hp, this->hp); - - //printf("BEFORE Apply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - int original_hp = this->hp; - this->hp += (totalUpgrade.getMaxHpRegeneration () - prevMaxHpRegen); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - //if(hp > type->getTotalMaxHp(&totalUpgrade)) { - // hp = type->getTotalMaxHp(&totalUpgrade); - //} - addItemToVault (&this->hp, this->hp); - - //printf("AFTER Apply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - } - - checkModelStateInfoForNewHpValue (); - - if (originalHp < hp) - { - this->setLastAttackerUnitId (source->getId ()); - } - //printf("#2 wasAlive = %d hp = %d boosthp = %d\n",wasAlive,hp,boost->boostUpgrade.getMaxHp()); - - if (showUnitParticles == true) - { - if (boost->unitParticleSystemTypeForAffectedUnit != NULL) - { - effect->upst = new UnitParticleSystemType (); - *effect->upst = *boost->unitParticleSystemTypeForAffectedUnit; - //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; - - effect->ups = new UnitParticleSystem (200); - effect->ups->setParticleOwner (this); - effect->ups->setParticleType (effect->upst); - - effect->upst->setValues (effect->ups); - effect->ups->setPos (getCurrVectorForParticlesystems ()); - effect->ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (effect->ups); - if (getFaction ()->getTexture ()) - { - effect->ups->setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - Renderer::getInstance ().manageParticleSystem (effect->ups, - rsGame); - } - } - currentAttackBoostEffects.push_back (effect); - - if (wasAlive == true) - { - //startDamageParticles - - if (originalHp > this->hp) - { - startDamageParticles (); - } - - //stop DamageParticles on death - if (this->hp <= 0) - { - setAlive (false); - this->hp = 0; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - addItemToVault (&this->hp, this->hp); - checkModelStateInfoForNewHpValue (); - - stopDamageParticles (true); - - this->setLastAttackerUnitId (source->getId ()); - this->setCauseOfDeath (ucodAttackBoost); - Unit::game->getWorld ()->getStats ()->die (getFactionIndex (), - getType - ()->getCountUnitDeathInStats - ()); - game->getScriptManager ()->onUnitDied (this); - - StaticSound *sound = - static_cast < - const DieSkillType * - >(this->getType ()->getFirstStOfClass (scDie))->getSound (); - if (sound != NULL - && (this->getFactionIndex () == - Unit::game->getWorld ()->getThisFactionIndex () - || (game-> - getWorld ()->showWorldForPlayer (game-> - getWorld - ()->getThisTeamIndex - ()) == true))) - { - SoundRenderer::getInstance ().playFx (sound); - } - - if (this->isDead () - && this->getCurrSkill ()->getClass () != scDie) - { - this->kill (); - } - } - } - - //printf("APPLYING ATTACK BOOST END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("APPLIED ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", - this->getType ()->getName (false).c_str (), this->getId (), - source->getType ()->getName (false).c_str (), source->getId (), - hp); - - return shouldApplyAttackBoost; - } - - void Unit::deapplyAttackBoost (const AttackBoost * boost, - const Unit * source) - { - if (boost == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: boost == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("=== DE-APPLYING ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", - this->getType ()->getName (false).c_str (), this->getId (), - source->getType ()->getName (false).c_str (), source->getId (), - hp); - - bool wasAlive = alive; - int originalHp = hp; - int prevMaxHp = totalUpgrade.getMaxHp (); - int prevMaxHpRegen = totalUpgrade.getMaxHpRegeneration (); - totalUpgrade.deapply (source->getId (), &boost->boostUpgrade, - this->getId ()); - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - //hp -= boost->boostUpgrade.getMaxHp(); - this->hp -= (prevMaxHp - totalUpgrade.getMaxHp ()); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - //regenerate hp upgrade / or boost - if (totalUpgrade.getMaxHpRegeneration () != 0) - { - checkItemInVault (&this->hp, this->hp); - - //printf("BEFORE DeApply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - int original_hp = this->hp; - this->hp -= (totalUpgrade.getMaxHpRegeneration () - prevMaxHpRegen); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - //if(hp > totalUpgrade.getMaxHp()) { - // hp = totalUpgrade.getMaxHp(); - //} - addItemToVault (&this->hp, this->hp); - - //printf("AFTER DeApply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); - } - - if (originalHp < hp) - { - this->setLastAttackerUnitId (source->getId ()); - } - - if (wasAlive == true) - { - //printf("DE-APPLYING ATTACK BOOST wasalive = true to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); - - //startDamageParticles - if (originalHp > this->hp) - { - startDamageParticles (); - } - - //stop DamageParticles on death - if (this->hp <= 0) - { - setAlive (false); - this->hp = 0; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - - stopDamageParticles (true); - - this->setLastAttackerUnitId (source->getId ()); - this->setCauseOfDeath (ucodAttackBoost); - - Unit::game->getWorld ()->getStats ()->die (getFactionIndex (), - getType - ()->getCountUnitDeathInStats - ()); - game->getScriptManager ()->onUnitDied (this); - - StaticSound *sound = - static_cast < - const DieSkillType * - >(this->getType ()->getFirstStOfClass (scDie))->getSound (); - if (sound != NULL - && (this->getFactionIndex () == - Unit::game->getWorld ()->getThisFactionIndex () - || (game-> - getWorld ()->showWorldForPlayer (game-> - getWorld - ()->getThisTeamIndex - ()) == true))) - { - SoundRenderer::getInstance ().playFx (sound); - } - - if (this->isDead () && this->getCurrSkill ()->getClass () != scDie) - { - this->kill (); - } - } - } - - checkModelStateInfoForNewHpValue (); - - //printf("DE-APPLYING ATTACK BOOST BEFORE END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); - - for (unsigned int i = 0; i < currentAttackBoostEffects.size (); ++i) - { - UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; - if (effect != NULL && effect->boost == boost - && effect->getSource () == source) - { - delete effect; - currentAttackBoostEffects. - erase (currentAttackBoostEffects.begin () + i); - break; - } - } - - if (SystemFlags::VERBOSE_MODE_ENABLED) - printf - ("DE-APPLIED ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", - this->getType ()->getName (false).c_str (), this->getId (), - source->getType ()->getName (false).c_str (), source->getId (), - hp); - - //printf("DE-APPLYING ATTACK BOOST END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); - } - - void Unit::tick () - { - - if (isAlive ()) - { - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //if(this->getType()->getName() == "spearman") printf("Unit [%d - %s] start tick hp = %d\n",this->getId(),this->getType()->getName().c_str(),hp); - - - //regenerate hp upgrade / or boost - if (type->getTotalMaxHpRegeneration (&totalUpgrade) != 0) - { - if (currSkill->getClass () != scBeBuilt) - { - if (type->getTotalMaxHpRegeneration (&totalUpgrade) >= 0) - { - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp += type->getTotalMaxHpRegeneration (&totalUpgrade); - if (this->hp > type->getTotalMaxHp (&totalUpgrade)) - { - this->hp = type->getTotalMaxHp (&totalUpgrade); - } - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - //if(this->getType()->getName() == "spearman") printf("tick hp#2 [type->getTotalMaxHpRegeneration(&totalUpgrade)] = %d type->getTotalMaxHp(&totalUpgrade) [%d] newhp = %d\n",type->getTotalMaxHpRegeneration(&totalUpgrade),type->getTotalMaxHp(&totalUpgrade),hp); - } - // If we have negative regeneration then check if the unit should die - else - { - bool decHpResult = - decHp (-type->getTotalMaxHpRegeneration (&totalUpgrade)); - if (decHpResult) - { - this->setCauseOfDeath (ucodStarvedRegeneration); - - Unit::game->getWorld ()->getStats ()->die (getFactionIndex (), - getType - ()->getCountUnitDeathInStats - ()); - game->getScriptManager ()->onUnitDied (this); - } - StaticSound *sound = - static_cast < - const DieSkillType * - >(this->getType ()->getFirstStOfClass (scDie))->getSound (); - if (sound != NULL - && (this->getFactionIndex () == - Unit::game->getWorld ()->getThisFactionIndex () - || (game-> - getWorld ()->showWorldForPlayer (game-> - getWorld - ()->getThisTeamIndex - ()) == true))) - { - SoundRenderer::getInstance ().playFx (sound); - } - } - } - } - //regenerate hp - else - { - if (type->getTotalMaxHpRegeneration (&totalUpgrade) >= 0) - { - if (currSkill->getClass () != scBeBuilt) - { - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp += type->getTotalMaxHpRegeneration (&totalUpgrade); - if (this->hp > type->getTotalMaxHp (&totalUpgrade)) - { - this->hp = type->getTotalMaxHp (&totalUpgrade); - } - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - //if(this->getType()->getName() == "spearman") printf("tick hp#1 [type->getHpRegeneration()] = %d type->getTotalMaxHp(&totalUpgrade) [%d] newhp = %d\n",type->getHpRegeneration(),type->getTotalMaxHp(&totalUpgrade),hp); - } - } - // If we have negative regeneration then check if the unit should die - else - { - bool decHpResult = - decHp (-type->getTotalMaxHpRegeneration (&totalUpgrade)); - if (decHpResult) - { - this->setCauseOfDeath (ucodStarvedRegeneration); - - Unit::game->getWorld ()->getStats ()->die (getFactionIndex (), - getType - ()->getCountUnitDeathInStats - ()); - game->getScriptManager ()->onUnitDied (this); - } - StaticSound *sound = - static_cast < - const DieSkillType * - >(this->getType ()->getFirstStOfClass (scDie))->getSound (); - if (sound != NULL - && (this->getFactionIndex () == - Unit::game->getWorld ()->getThisFactionIndex () - || (game-> - getWorld ()->showWorldForPlayer (game-> - getWorld - ()->getThisTeamIndex - ()) == true))) - { - SoundRenderer::getInstance ().playFx (sound); - } - } - } - - //stop DamageParticles - stopDamageParticles (false); - - checkItemInVault (&this->ep, this->ep); - //regenerate ep -// ep += type->getEpRegeneration(); -// if(ep > type->getTotalMaxEp(&totalUpgrade)){ -// ep = type->getTotalMaxEp(&totalUpgrade); -// } -// addItemToVault(&this->ep,this->ep); - - if (!isBeingBuilt ()) - { - //regenerate ep upgrade / or boost - checkItemInVault (&this->ep, this->ep); - //regenerate ep - int original_ep = this->ep; - this->ep += type->getTotalMaxEpRegeneration (&totalUpgrade); - if (this->ep > type->getTotalMaxEp (&totalUpgrade)) - { - this->ep = type->getTotalMaxEp (&totalUpgrade); - } - if (original_ep != this->ep) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_EPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->ep, this->ep); - } - } - } - - int Unit::update2 () - { - progress2++; - return progress2; - } - - bool Unit::computeEp () - { - - if (currSkill == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //if not enough ep - if (this->ep - currSkill->getEpCost () < 0) - { - return true; - } - - checkItemInVault (&this->ep, this->ep); - int original_ep = this->ep; - //decrease ep - this->ep -= currSkill->getEpCost (); - if (original_ep != this->ep) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_EPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->ep, this->ep); - - if (getType () == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: getType() == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (this->ep > getType ()->getTotalMaxEp (&totalUpgrade)) - { - int original_ep = this->ep; - this->ep = getType ()->getTotalMaxEp (&totalUpgrade); - if (original_ep != this->ep) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_EPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - } - addItemToVault (&this->ep, this->ep); - - return false; - } - - bool Unit::repair () - { - - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //increase hp - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - if (type->getProductionTime () + 1 == 0) - { - throw - megaglest_runtime_error - ("Detected divide by 0 condition: type->getProductionTime() + 1 == 0"); - } - this->hp += getType ()->getMaxHp () / type->getProductionTime () + 1; - if (this->hp > (getType ()->getTotalMaxHp (&totalUpgrade))) - { - this->hp = getType ()->getTotalMaxHp (&totalUpgrade); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - return true; - } - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - - //stop DamageParticles - stopDamageParticles (false); - - return false; - } - -//decrements HP and returns if dead - bool Unit::decHp (int decrementValue) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8095, "this->hp = %d, decrementValue = %d", this->hp, - decrementValue); - addNetworkCRCDecHp (szBuf); - - if (this->hp == 0) - { - return false; - } - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp -= decrementValue; - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - - if (type == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //startDamageParticles - startDamageParticles (); - - //stop DamageParticles on death - if (this->hp <= 0) - { - setAlive (false); - this->hp = 0; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - - stopDamageParticles (true); - return true; - } - return false; - } - - string Unit::getDescExtension (bool translatedValue) const - { - Lang & lang = Lang::getInstance (); - string str = "\n"; - - if (commands.empty () == false && commands.size () > 1) - { - Commands::const_iterator it = commands.begin (); - for (unsigned int i = 0; - i < min ((size_t) maxQueuedCommandDisplayCount, - commands.size ()); ++i) - { - const CommandType *ct = (*it)->getCommandType (); - if (i == 0) - { - str += "\n" + lang.getString ("OrdersOnQueue") + ": "; - } - str += - "\n#" + intToStr (i + 1) + " " + ct->getName (translatedValue); - ++it; - } - } - - return str; - } - - string Unit::getDesc (bool translatedValue) const - { - - Lang & lang = Lang::getInstance (); - - //hp - string str = "\n"; - - //maxUnitCount - if (type->getMaxUnitCount () > 0) - { - str += - lang.getString ("MaxUnitCount") + ": " + - intToStr (faction->getCountForMaxUnitCount (type)) + "/" + - intToStr (type->getMaxUnitCount ()); - } - - str += - "\n" + lang.getString ("Hp") + ": " + intToStr (hp) + "/" + - intToStr (type->getTotalMaxHp (&totalUpgrade)); - if (type->getHpRegeneration () != 0 - || totalUpgrade.getMaxHpRegeneration () != 0) - { - str += - " (" + lang.getString ("Regeneration") + ": " + - intToStr (type->getHpRegeneration ()); - if (totalUpgrade.getMaxHpRegeneration () != 0) - { - str += "+" + intToStr (totalUpgrade.getMaxHpRegeneration ()); - } - str += ")"; - } - - //ep - if (getType ()->getMaxEp () != 0) - { - str += - "\n" + lang.getString ("Ep") + ": " + intToStr (ep) + "/" + - intToStr (type->getTotalMaxEp (&totalUpgrade)); - } - if (type->getEpRegeneration () != 0 - || totalUpgrade.getMaxEpRegeneration () != 0) - { - str += - " (" + lang.getString ("Regeneration") + ": " + - intToStr (type->getEpRegeneration ()); - if (totalUpgrade.getMaxEpRegeneration () != 0) - { - str += "+" + intToStr (totalUpgrade.getMaxEpRegeneration ()); - } - str += ")"; - } - - //armor - str += - "\n" + lang.getString ("Armor") + ": " + - intToStr (getType ()->getArmor ()); - if (totalUpgrade.getArmor () != 0) - { - str += "+" + intToStr (totalUpgrade.getArmor ()); - } - str += - " (" + getType ()->getArmorType ()->getName (translatedValue) + ")"; - - //sight - str += - "\n" + lang.getString ("Sight") + ": " + - intToStr (getType ()->getSight ()); - if (totalUpgrade.getSight () != 0) - { - str += "+" + intToStr (totalUpgrade.getSight ()); - } - - //kills - const Level *nextLevel = getNextLevel (); - if (enemyKills > 0 || nextLevel != NULL) - { - str += "\n" + lang.getString ("Kills") + ": " + intToStr (enemyKills); - if (nextLevel != NULL) - { - str += - " (" + nextLevel->getName (translatedValue) + ": " + - intToStr (nextLevel->getKills ()) + ")"; - } - } - - //str+= "\nskl: "+scToStr(currSkill->getClass()); - - //load - if (loadCount != 0) - { - str += - "\n" + lang.getString ("Load") + ": " + intToStr (loadCount) + - " " + loadType->getName (translatedValue); - } - - //consumable production - for (int i = 0; i < getType ()->getCostCount (); ++i) - { - const Resource *r = getType ()->getCost (i); - if (r->getType ()->getClass () == rcConsumable) - { - str += "\n"; - str += - r->getAmount () < - 0 ? lang.getString ("Produce") + - ": " : lang.getString ("Consume") + ": "; - str += - intToStr (abs (r->getAmount ())) + " " + - r->getType ()->getName (translatedValue); - } - } - - //command info - if (commands.empty () == false) - { - str += - "\n" + - commands.front ()->getCommandType ()->getName (translatedValue); - if (commands.size () > 1) - { - str += - "\n" + lang.getString ("OrdersOnQueue") + ": " + - intToStr (commands.size ()); - } - } - else - { - //can store - if (getType ()->getStoredResourceCount () > 0) - { - for (int i = 0; i < getType ()->getStoredResourceCount (); ++i) - { - const Resource *r = getType ()->getStoredResource (i); - str += "\n" + lang.getString ("Store") + ": "; - str += - intToStr (r->getAmount ()) + " " + - r->getType ()->getName (translatedValue); - } - } - } - - return str; - } - - void Unit::applyUpgrade (const UpgradeType * upgradeType) - { - if (upgradeType == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: upgradeType == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (upgradeType->isAffected (type)) - { - totalUpgrade.sum (upgradeType, this); - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp += upgradeType->getMaxHp (); - this->hp = max (0, this->hp); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - } - } - - void Unit::computeTotalUpgrade () - { - faction->getUpgradeManager ()->computeTotalUpgrade (this, - &totalUpgrade); - } - - void Unit::incKills (int team) - { - ++kills; - if (team != this->getTeam ()) - { - ++enemyKills; - } - - checkUnitLevel (); - } - - void Unit::checkUnitLevel () - { - const Level *nextLevel = getNextLevel (); - if (nextLevel != NULL && this->enemyKills >= nextLevel->getKills ()) - { - this->level = nextLevel; - - int maxHp = this->totalUpgrade.getMaxHp (); - totalUpgrade.incLevel (type); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_LevelChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp += totalUpgrade.getMaxHp () - maxHp; - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - } - } - - void Unit::morphAttackBoosts (Unit * unit) - { - // Remove any units that were previously in range - if (currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.empty () == false - && currentAttackBoostOriginatorEffect.skillType != NULL) - { - for (int i = - (int)currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.size () - 1; i >= 0; --i) - { - // Remove attack boost upgrades from unit - - int findUnitId = - currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; - Unit *affectedUnit = game->getWorld ()->findUnitById (findUnitId); - if (affectedUnit != NULL - && affectedUnit->getId () == unit->getId ()) - { - affectedUnit->deapplyAttackBoost - (currentAttackBoostOriginatorEffect.skillType-> - getAttackBoost (), this); - - currentAttackBoostOriginatorEffect. - currentAttackBoostUnits.erase - (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - begin () + i); - } - - //printf("- #1 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); - } - } - } - - bool Unit::morph (const MorphCommandType * mct, int frameIndex) - { - - if (mct == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: mct == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - const UnitType *morphUnitType = mct->getMorphUnit (); - - if (morphUnitType == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: morphUnitType == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - Field morphUnitField = fLand; - if (morphUnitType->getField (fAir)) - { - morphUnitField = fAir; - } - if (morphUnitType->getField (fLand)) - { - morphUnitField = fLand; - } - - map->clearUnitCells (this, pos, false); - if (map->canMorph (pos, this, morphUnitType)) - { - map->clearUnitCells (this, pos, true); - faction->deApplyStaticCosts (type, mct); - - //printf("Now unapply attack-boost for unit [%d - %s]\n",this->getId(),this->getType()->getName().c_str()); - // De apply attack boosts for morphed unit - for (int i = (int)currentAttackBoostEffects.size () - 1; i >= 0; --i) - { - UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; - if (effect != NULL) - { - Unit *sourceUnit = - game->getWorld ()->findUnitById (effect-> - getSource ()->getId ()); - if (sourceUnit == NULL) - { - throw megaglest_runtime_error ("sourceUnit == NULL"); - } - sourceUnit->morphAttackBoosts (this); - } - } - - //stopDamageParticles(true); - cleanupAllParticlesystems (); - - checkItemInVault (&this->hp, this->hp); - int original_hp = this->hp; - this->hp += morphUnitType->getMaxHp () - type->getMaxHp (); - if (original_hp != this->hp) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_HPChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - addItemToVault (&this->hp, this->hp); - - checkModelStateInfoForNewHpValue (); - - this->preMorph_type = this->type; - this->setType (morphUnitType); - Field original_field = this->currField; - this->currField = morphUnitField; - computeTotalUpgrade (); - map->putUnitCells (this, this->pos, false, frameIndex < 0); - - this->faction->applyDiscount (morphUnitType, mct->getDiscount ()); - // add new storage - this->faction->addStore (this->type); - // remove former storage - this->faction->removeStore (this->preMorph_type); - this->faction->applyStaticProduction (morphUnitType, mct); - - this->level = NULL; - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_LevelChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - checkUnitLevel (); - - if (original_field != this->currField) - { - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - game->getScriptManager ()->onUnitTriggerEvent (this, - utet_FieldChanged); - //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); - } - - return true; - } - else - { - return false; - } - } - - -// ==================== PRIVATE ==================== - - float Unit::computeHeight (const Vec2i & pos) const - { - //printf("CRASHING FOR UNIT: %d alive = %d\n",this->getId(),this->isAlive()); - //printf("[%s]\n",this->getType()->getName().c_str()); - if (map->isInside (pos) == false - || map->isInsideSurface (map->toSurfCoords (pos)) == false) - { - //printf("CRASHING FOR UNIT: %d [%s] alive = %d\n",this->getId(),this->getType()->getName().c_str(),this->isAlive()); - //abort(); - throw megaglest_runtime_error ("#7 Invalid path position = " + - pos.getString ()); - } - - float height = map->getCell (pos)->getHeight (); - - if (currField == fAir) - { - float airHeight = game->getWorld ()->getTileset ()->getAirHeight (); - airHeight = truncateDecimal < float >(airHeight, 6); - - height += airHeight; - height = truncateDecimal < float >(height, 6); - - Unit *unit = map->getCell (pos)->getUnit (fLand); - if (unit != NULL && unit->getType ()->getHeight () > airHeight) - { - height += - (std::min ((float) unit->getType ()->getHeight (), - Tileset::standardAirHeight * 3) - airHeight); - height = truncateDecimal < float >(height, 6); - } - else - { - SurfaceCell *sc = map->getSurfaceCell (map->toSurfCoords (pos)); - if (sc != NULL && sc->getObject () != NULL - && sc->getObject ()->getType () != NULL) - { - if (sc->getObject ()->getType ()->getHeight () > airHeight) - { - height += - (std::min ((float) sc->getObject ()->getType ()->getHeight (), - Tileset::standardAirHeight * 3) - airHeight); - height = truncateDecimal < float >(height, 6); - } - } - } - } - - return height; - } - - void Unit::AnimCycleStarts () - { - // we need to queue timed particles if progress starts - queueTimedParticles (currSkill->unitParticleSystemTypes); - } - - void Unit::updateTarget () - { - Unit *target = targetRef.getUnit (); - if (target != NULL) - { - - //update target pos - targetPos = target->getCellPos (); - Vec2i relPos = targetPos - pos; - Vec2f relPosf = Vec2f ((float) relPos.x, (float) relPos.y); -#ifdef USE_STREFLOP - targetRotation = - radToDeg (streflop::atan2 - (static_cast < streflop::Simple > (relPosf.x), - static_cast < streflop::Simple > (relPosf.y))); -#else - targetRotation = radToDeg (atan2 (relPosf.x, relPosf.y)); -#endif - targetVec = target->getCurrVectorAsTarget (); - } - } - - void Unit::clearCommands () - { - - this->setCurrentUnitTitle (""); - this->unitPath->clear (); - while (commands.empty () == false) - { - undoCommand (commands.back ()); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - - delete commands.back (); - commands.pop_back (); - - safeMutex.ReleaseLock (); - } - changedActiveCommand = false; - } - - void Unit::deleteQueuedCommand (Command * command) - { - if (getCurrCommand () == command) - { - this->setCurrentUnitTitle (""); - this->unitPath->clear (); - } - undoCommand (command); - delete command; - } - - - std::pair < CommandResult, string > Unit::checkCommand (Command * command) const - { - std::pair < CommandResult, string > result (crSuccess, ""); - - if (command == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //if not operative or has not command type => fail - if (isOperative () == false || - command->getUnit () == this || - getType ()->hasCommandType (command->getCommandType ()) == false || - (ignoreCheckCommand == false - && this->getFaction ()->reqsOk (command->getCommandType ()) == - false)) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d] isOperative() = %d, command->getUnit() = %p, getType()->hasCommandType(command->getCommandType()) = %d, this->getFaction()->reqsOk(command->getCommandType()) = %d\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, isOperative (), - command->getUnit (), - getType ()-> - hasCommandType (command->getCommandType - ()), - this->getFaction ()-> - reqsOk (command->getCommandType ())); - - // Allow self healing if able to heal own unit type - if (command->getUnit () == this && - command->getCommandType ()->getClass () == ccRepair && - this->getType ()->getFirstRepairCommand (this->getType ()) != - NULL) - { - - } - else - { - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - - result.first = crFailUndefined; - return result; - } - } - - //if pos is not inside the world (if comand has not a pos, pos is (0, 0) and is inside world - if (map->isInside (command->getPos ()) == false) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - - result.first = crFailUndefined; - return result; - } - - //check produced - if (command->getCommandType () == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - const ProducibleType *produced = - command->getCommandType ()->getProduced (); - if (produced != NULL) - { - if (ignoreCheckCommand == false - && faction->reqsOk (produced) == false) - { - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - //printf("To produce this unit you need:\n%s\n",produced->getUnitAndUpgradeReqDesc().c_str()); - result.first = crFailReqs; - - Lang & lang = Lang::getInstance (); - result.second = - " - " + lang.getString ("Reqs") + " : " + - produced->getUnitAndUpgradeReqDesc (false, - this->showTranslatedTechTree - ()); - return result; - } - - if (ignoreCheckCommand == false && - faction->checkCosts (produced, - command->getCommandType ()) == false) - { - //printf("To produce this unit you need:\n%s\n",produced->getResourceReqDesc().c_str()); - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - result.first = crFailRes; - Lang & lang = Lang::getInstance (); - result.second = - " - " + lang.getString ("Reqs") + " : " + - produced->getResourceReqDesc (false, - this->showTranslatedTechTree ()); - return result; - } - } - - //build command specific, check resources and requirements for building - if (command->getCommandType ()->getClass () == ccBuild) - { - const UnitType *builtUnit = command->getUnitType (); - - if (builtUnit == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: builtUnit == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (faction->reqsOk (builtUnit) == false) - { - //printf("To build this unit you need:\n%s\n",builtUnit->getUnitAndUpgradeReqDesc().c_str()); - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - result.first = crFailReqs; - Lang & lang = Lang::getInstance (); - result.second = - " - " + lang.getString ("Reqs") + " : " + - builtUnit->getUnitAndUpgradeReqDesc (false, - this->showTranslatedTechTree - ()); - return result; - } - if (faction->checkCosts (builtUnit, NULL) == false) - { - //printf("To build this unit you need:\n%s\n",builtUnit->getResourceReqDesc().c_str()); - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - result.first = crFailRes; - Lang & lang = Lang::getInstance (); - result.second = - " - " + lang.getString ("Reqs") + " : " + - builtUnit->getResourceReqDesc (false, - this->showTranslatedTechTree ()); - return result; - } - } - //upgrade command specific, check that upgrade is not upgraded - else if (command->getCommandType ()->getClass () == ccUpgrade) - { - const UpgradeCommandType *uct = - static_cast < - const UpgradeCommandType * >(command->getCommandType ()); - - if (uct == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (faction-> - getUpgradeManager ()->isUpgradingOrUpgraded (uct-> - getProducedUpgrade - ())) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugLUA).enabled) - SystemFlags::OutputDebug (SystemFlags::debugLUA, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__); - //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); - result.first = crFailUndefined; - return result; - } - } - - return result; - } - - void Unit::applyCommand (Command * command) - { - if (command == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - else if (command->getCommandType () == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - //check produced - const ProducibleType *produced = - command->getCommandType ()->getProduced (); - if (produced != NULL) - { - faction->applyCosts (produced, command->getCommandType ()); - } - - //build command specific - if (command->getCommandType ()->getClass () == ccBuild) - { - faction->applyCosts (command->getUnitType (), - command->getCommandType ()); - } - //upgrade command specific - else if (command->getCommandType ()->getClass () == ccUpgrade) - { - const UpgradeCommandType *uct = - static_cast < - const UpgradeCommandType * >(command->getCommandType ()); - - if (uct == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - faction->startUpgrade (uct->getProducedUpgrade ()); - } - } - - CommandResult Unit::undoCommand (Command * command) - { - - if (command == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - else if (command->getCommandType () == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - if (getCurrCommand () == command - && command->getCommandType ()->getClass () == ccMorph - && this->currSkill->getClass () == scMorph) - { - // clear cells of morphed unit and set those of current unit! - map->clearUnitCells (this, this->getPos ()); - map->putUnitCells (this, this->getPos (), true); - } - //return cost - const ProducibleType *produced = - command->getCommandType ()->getProduced (); - if (produced != NULL) - { - faction->deApplyCosts (produced, command->getCommandType ()); - } - - //return building cost if not already building it or dead - if (command->getCommandType ()->getClass () == ccBuild) - { - if (currSkill->getClass () != scBuild - && currSkill->getClass () != scDie) - { - faction->deApplyCosts (command->getUnitType (), - command->getCommandType ()); - } - } - - //upgrade command cancel from list - if (command->getCommandType ()->getClass () == ccUpgrade) - { - const UpgradeCommandType *uct = - static_cast < - const UpgradeCommandType * >(command->getCommandType ()); - if (uct == NULL) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, this->toString ().c_str ()); - throw megaglest_runtime_error (szBuf); - } - - faction->cancelUpgrade (uct->getProducedUpgrade ()); - } - - retryCurrCommandCount = 0; - this->setCurrentUnitTitle (""); - - return crSuccess; - } - - void Unit::stopDamageParticles (bool force) - { - if (force == true || (hp > type->getTotalMaxHp (&totalUpgrade) / 2)) - { - //printf("Checking to stop damageparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); - - if (Renderer:: - getInstance ().validateParticleSystemStillExists (this->fire, - rsGame) == - false) - { - this->fire = NULL; - } - - // stop fire - if (this->fire != NULL) - { - this->fire->fade (); - this->fire = NULL; - } - // stop additional particles - - - if (smokeParticleSystems.empty () == false) - { - //printf("Checking to stop smokeparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); - - for (int i = (int)smokeParticleSystems.size () - 1; i >= 0; --i) - { - UnitParticleSystem *ps = smokeParticleSystems[i]; - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - ps->fade (); - } - smokeParticleSystems.pop_back (); - } - } - - if (damageParticleSystems.empty () == false) - { - //printf("Checking to stop damageparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); - - for (int i = (int)damageParticleSystems.size () - 1; i >= 0; --i) - { - UnitParticleSystem *ps = damageParticleSystems[i]; - UnitParticleSystemType *pst = NULL; - int foundParticleIndexType = -2; - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - for (std::map < int, UnitParticleSystem * >::iterator iterMap = - damageParticleSystemsInUse.begin (); - iterMap != damageParticleSystemsInUse.end (); ++iterMap) - { - if (iterMap->second == ps) - { - foundParticleIndexType = iterMap->first; - if (foundParticleIndexType >= 0) - { - pst = - type->damageParticleSystemTypes[foundParticleIndexType]; - break; - } - } - } - } - if (force == true - || (pst != NULL && pst->getMinmaxEnabled () == false)) - { - damageParticleSystemsInUse.erase (foundParticleIndexType); - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - ps->fade (); - } - damageParticleSystems.pop_back (); - } - } - } - } - - checkCustomizedParticleTriggers (force); - } - - void Unit:: - checkCustomizedUnitParticleListTriggers (const UnitParticleSystemTypes & - unitParticleSystemTypesList, - bool applySkillChangeParticles) - { - if (showUnitParticles == true) - { - vector < ParticleSystemTypeInterface * >systemTypesInUse; - - if (unitParticleSystems.empty () == false) - { - for (int index = (int)unitParticleSystems.size () - 1; index >= 0; - index--) - { - UnitParticleSystem *ps = unitParticleSystems[index]; - if (ps != NULL) - { - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - - bool stopParticle = false; - if ((ps->getParticleType () != NULL && - ps->getParticleType ()->getMinmaxEnabled ())) - { - - if (ps->getParticleType () != NULL) - { - if (ps->getParticleType ()->getMinmaxIsPercent () == - false) - { - if (hp < ps->getParticleType ()->getMinHp () - || hp > ps->getParticleType ()->getMaxHp ()) - { - stopParticle = true; - - //printf("STOP Particle line: %d\n",__LINE__); - } - } - else - { - int hpPercent = - (hp / type->getTotalMaxHp (&totalUpgrade) * 100); - if (hpPercent < ps->getParticleType ()->getMinHp () - || hpPercent > ps->getParticleType ()->getMaxHp ()) - { - stopParticle = true; - - //printf("STOP Particle line: %d\n",__LINE__); - } - } - } - - if (stopParticle == true) - { - ps->fade (); - unitParticleSystems.erase (unitParticleSystems.begin () + - index); - } - } - - if (ps->getParticleType () != NULL && stopParticle == false) - { - systemTypesInUse.push_back (ps->getParticleType ()); - } - } - } - } - } - - //printf("Check Particle start line: %d size: %d\n",__LINE__,(int)unitParticleSystemTypesList.size()); - - if (unitParticleSystemTypesList.empty () == false) - { - - //for(unsigned int index = 0; index < unitParticleSystemTypesList.size(); ++index) { - for (UnitParticleSystemTypes::const_iterator iterParticleType = - unitParticleSystemTypesList.begin (); - iterParticleType != unitParticleSystemTypesList.end (); - ++iterParticleType) - { - UnitParticleSystemType *pst = *iterParticleType; - - vector < ParticleSystemTypeInterface * >::iterator iterFind = - std::find (systemTypesInUse.begin (), systemTypesInUse.end (), - pst); - - //printf("Check Particle line: %d isenabled: %d already in use: %d\n",__LINE__,pst->getMinmaxEnabled(),(iterFind == systemTypesInUse.end())); - - bool showParticle = applySkillChangeParticles; - if (pst->getMinmaxEnabled () == true) - { - - //printf("Check Particle line: %d isenabled: %d already in use: %d\n",__LINE__,pst->getMinmaxEnabled(),(iterFind != systemTypesInUse.end())); - - showParticle = false; - if (iterFind == systemTypesInUse.end ()) - { - if (pst->getMinmaxIsPercent () == false) - { - if (hp >= pst->getMinHp () && hp <= pst->getMaxHp ()) - { - showParticle = true; - - //printf("START Particle line: %d\n",__LINE__); - } - } - else - { - int hpPercent = - (hp / type->getTotalMaxHp (&totalUpgrade) * 100); - if (hpPercent >= pst->getMinHp () - && hpPercent <= pst->getMaxHp ()) - { - showParticle = true; - - //printf("START Particle line: %d\n",__LINE__); - } - } - } - } - if (showParticle == true) - { - if (pst->getStartTime () == 0.0) - { - UnitParticleSystem *ups = new UnitParticleSystem (200); - ups->setParticleOwner (this); - ups->setParticleType (pst); - - pst->setValues (ups); - ups->setPos (getCurrVectorForParticlesystems ()); - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (ups); - if (getFaction ()->getTexture ()) - { - ups->setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - unitParticleSystems.push_back (ups); - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - } - else - { - // do nothing, timed particles are handled below in queueTimedParticles() - } - } - } - } - } - } - - void Unit:: - queueTimedParticles (const UnitParticleSystemTypes & - unitParticleSystemTypesList) - { - if (showUnitParticles == true) - { - for (UnitParticleSystemTypes::const_iterator iterParticleType = - unitParticleSystemTypesList.begin (); - iterParticleType != unitParticleSystemTypesList.end (); - ++iterParticleType) - { - UnitParticleSystemType *pst = *iterParticleType; - if (pst->getMinmaxEnabled () == false) - { - if (pst->getStartTime () != 0.0) - { - queuedUnitParticleSystemTypes.push_back (pst); - } - } - } - } - } - - void Unit::setMeshPosInParticleSystem (UnitParticleSystem * ups) - { - if (ups->getMeshName () != "") - { - string meshName = ups->getMeshName (); - Model *model = getCurrentModelPtr (); - - // as it can happen that anim progress is a bit out of range we correct it to get something valid for the particle positions. - float currentAnimProgress = getAnimProgressAsFloat (); - if (currentAnimProgress > 1.f || currentAnimProgress < 0.f) - { - currentAnimProgress = 0.f; - } - model->updateInterpolationVertices (currentAnimProgress, isAlive () - && !isAnimProgressBound ()); - - bool foundMesh = false; - for (unsigned int i = 0; i < model->getMeshCount (); i++) - { - //printf("meshName=%s\n",unitModel->getMesh(i)->getName().c_str()); - if (model->getMesh (i)->getName () == meshName) - { - const InterpolationData *data = - model->getMesh (i)->getInterpolationData (); - const Vec3f *verticepos = data->getVertices (); - ups->setMeshPos (Vec3f - (verticepos->x, verticepos->y, verticepos->z)); - foundMesh = true; - break; - } - } - if (foundMesh == false) - { - string meshesFound = model->getMesh (0)->getName (); - for (unsigned i = 1; i < model->getMeshCount (); i++) - { - meshesFound += ", " + model->getMesh (i)->getName (); - } - - string errorString = - "Warning: Particle system is trying to find mesh '" + meshName + - "', but just found:\n'" + meshesFound + "' in file:\n'" + - model->getFileName () + "'\n"; - //throw megaglest_runtime_error(errorString); - printf ("%s", errorString.c_str ()); - } - } - } - - void Unit::checkCustomizedUnitParticleTriggers () - { - if (currSkill != NULL) - { - checkCustomizedUnitParticleListTriggers - (currSkill->unitParticleSystemTypes, false); - } - } - - void Unit::checkCustomizedParticleTriggers (bool force) - { - // - // Now check if we have special pre-exisitng hp triggered particles and - // end those that should no longer display - // - // end s particles - if (damageParticleSystems.empty () == false) - { - for (int i = (int)damageParticleSystems.size () - 1; i >= 0; --i) - { - UnitParticleSystem *ps = damageParticleSystems[i]; - UnitParticleSystemType *pst = NULL; - int foundParticleIndexType = -2; - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - for (std::map < int, UnitParticleSystem * >::iterator iterMap = - damageParticleSystemsInUse.begin (); - iterMap != damageParticleSystemsInUse.end (); ++iterMap) - { - if (iterMap->second == ps) - { - foundParticleIndexType = iterMap->first; - if (foundParticleIndexType >= 0) - { - pst = - type->damageParticleSystemTypes[foundParticleIndexType]; - break; - } - } - } - } - - if (force == true - || (pst != NULL && pst->getMinmaxEnabled () == true)) - { - bool stopParticle = force; - if (force == false) - { - if (pst->getMinmaxIsPercent () == false) - { - if (hp < pst->getMinHp () || hp > pst->getMaxHp ()) - { - stopParticle = true; - } - } - else - { - int hpPercent = - (hp / type->getTotalMaxHp (&totalUpgrade) * 100); - if (hpPercent < pst->getMinHp () - || hpPercent > pst->getMaxHp ()) - { - stopParticle = true; - } - } - } - - //printf("CHECKING to STOP customized particle trigger by HP [%d to %d percentbased = %d] current hp = %d stopParticle = %d\n",pst->getMinHp(),pst->getMaxHp(),pst->getMinmaxIsPercent(),hp,stopParticle); - - if (stopParticle == true) - { - //printf("STOPPING customized particle trigger by HP [%d to %d] current hp = %d\n",pst->getMinHp(),pst->getMaxHp(),hp); - - damageParticleSystemsInUse.erase (foundParticleIndexType); - if (Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - ps->fade (); - } - damageParticleSystems.pop_back (); - } - } - } - } - - // - // Now check if we have new special hp triggered particles to display - // - //start additional particles - if (showUnitParticles && - type->damageParticleSystemTypes.empty () == false && - force == false && alive == true) - { - for (unsigned int i = 0; i < type->damageParticleSystemTypes.size (); - ++i) - { - UnitParticleSystemType *pst = type->damageParticleSystemTypes[i]; - - if (pst->getMinmaxEnabled () == true - && damageParticleSystemsInUse.find (i) == - damageParticleSystemsInUse.end ()) - { - bool showParticle = false; - if (pst->getMinmaxIsPercent () == false) - { - if (hp >= pst->getMinHp () && hp <= pst->getMaxHp ()) - { - showParticle = true; - } - } - else - { - int hpPercent = - (hp / type->getTotalMaxHp (&totalUpgrade) * 100); - if (hpPercent >= pst->getMinHp () - && hpPercent <= pst->getMaxHp ()) - { - showParticle = true; - } - } - - //printf("CHECKING to START customized particle trigger by HP [%d to %d percentbased = %d] current hp = %d showParticle = %d\n",pst->getMinHp(),pst->getMaxHp(),pst->getMinmaxIsPercent(),hp,showParticle); - - if (showParticle == true) - { - //printf("STARTING customized particle trigger by HP [%d to %d] current hp = %d\n",pst->getMinHp(),pst->getMaxHp(),hp); - - UnitParticleSystem *ups = new UnitParticleSystem (200); - ups->setParticleOwner (this); - ups->setParticleType (pst); - - pst->setValues (ups); - ups->setPos (getCurrVectorForParticlesystems ()); - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (ups); - if (getFaction ()->getTexture ()) - { - ups->setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - damageParticleSystems.push_back (ups); - damageParticleSystemsInUse[i] = ups; - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - } - } - } - } - - checkCustomizedUnitParticleTriggers (); - } - - void Unit::startDamageParticles () - { - if (hp < type->getTotalMaxHp (&totalUpgrade) / 2 && hp > 0 - && alive == true) - { - //start additional particles - if (showUnitParticles && - type->damageParticleSystemTypes.empty () == false) - { - for (unsigned int i = 0; - i < type->damageParticleSystemTypes.size (); ++i) - { - UnitParticleSystemType *pst = type->damageParticleSystemTypes[i]; - - if (pst->getMinmaxEnabled () == false - && damageParticleSystemsInUse.find (i) == - damageParticleSystemsInUse.end ()) - { - UnitParticleSystem *ups = new UnitParticleSystem (200); - ups->setParticleOwner (this); - ups->setParticleType (pst); - - pst->setValues (ups); - ups->setPos (getCurrVectorForParticlesystems ()); - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (ups); - if (getFaction ()->getTexture ()) - { - ups->setFactionColor (getFaction ()-> - getTexture ()->getPixmapConst ()-> - getPixel3f (0, 0)); - } - damageParticleSystems.push_back (ups); - damageParticleSystemsInUse[i] = ups; - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - } - } - } - - // start fire - if (type->getProperty (UnitType::pBurnable) && this->fire == NULL) - { - FireParticleSystem *fps = new FireParticleSystem (200); - fps->setParticleOwner (this); - const Game *game = Renderer::getInstance ().getGame (); - fps->setSpeed (2.5f / - game->getWorld ()-> - getUpdateFps (this->getFactionIndex ())); - fps->setPos (getCurrBurnVector ()); - fps->setRadius (type->getSize () / 3.f); - fps->setTexture (CoreData::getInstance ().getFireTexture ()); - fps->setParticleSize (type->getSize () / 3.f); - this->fire = fps; - fireParticleSystems.push_back (fps); - - Renderer::getInstance ().manageParticleSystem (fps, rsGame); - if (showUnitParticles == true) - { - // smoke - UnitParticleSystem *ups = new UnitParticleSystem (400); - ups->setParticleOwner (this); - ups->setColorNoEnergy (Vec4f (0.0f, 0.0f, 0.0f, 0.13f)); - ups->setColor (Vec4f (0.115f, 0.115f, 0.115f, 0.22f)); - ups->setPos (getCurrBurnVector ()); - ups->setRotation (getRotation ()); - setMeshPosInParticleSystem (ups); - ups->setBlendMode (ups->strToBlendMode ("black")); - ups->setOffset (Vec3f (0, 2, 0)); - ups->setDirection (Vec3f (0, 1, -0.2f)); - ups->setRadius (type->getSize () / 3.f); - ups->setShape (::Shared::Graphics::UnitParticleSystem::sLinear); - ups->setTexture (CoreData::getInstance ().getFireTexture ()); - const Game *game = Renderer::getInstance ().getGame (); - ups->setSpeed (2.0f / - game->getWorld ()-> - getUpdateFps (this->getFactionIndex ())); - ups->setGravity (0.0004f); - ups->setEmissionRate (1); - ups->setMaxParticleEnergy (150); - ups->setSizeNoEnergy (type->getSize () * 0.6f); - ups->setParticleSize (type->getSize () * 0.8f); - smokeParticleSystems.push_back (ups); - //damageParticleSystemsInUse[-1] = ups; - Renderer::getInstance ().manageParticleSystem (ups, rsGame); - } - } - } - - checkCustomizedParticleTriggers (false); - } - - void Unit::setMeetingPos (const Vec2i & meetingPos) - { - this->meetingPos = meetingPos; - map->clampPos (this->meetingPos); - - if (map->isInside (this->meetingPos) == false - || map->isInsideSurface (map->toSurfCoords (this->meetingPos)) == - false) - { - throw megaglest_runtime_error ("#8 Invalid path position = " + - this->meetingPos.getString ()); - } - - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__); - } - - bool Unit::isMeetingPointSettable () const - { - return (type != NULL ? type->getMeetingPoint () : false); - } - - uint32 Unit::getFrameCount () const - { - uint32 frameCount = 0; - //const Game *game = Renderer::getInstance().getGame(); - if (game != NULL && game->getWorld () != NULL) - { - int frameCountAsInt = game->getWorld ()->getFrameCount (); - if (frameCountAsInt >= 0) - { - frameCount = frameCountAsInt; - } - } - - return frameCount; - } - - void Unit::exploreCells (bool forceRefresh) - { - if (this->isOperative () == true) - { - const Vec2i & newPos = this->getCenteredPos (); - int sightRange = - this->getType ()->getTotalSight (this->getTotalUpgrade ()); - int teamIndex = this->getTeam (); - - if (game == NULL) - { - throw megaglest_runtime_error ("game == NULL"); - } - else if (game->getWorld () == NULL) - { - throw megaglest_runtime_error ("game->getWorld() == NULL"); - } - - // Try the local unit exploration cache - if (!forceRefresh && - cacheExploredCellsKey.first == newPos && - cacheExploredCellsKey.second == sightRange) - { - game->getWorld ()->exploreCells (teamIndex, cacheExploredCells); - } - else - { - // Try the world exploration scan or possible cache - cacheExploredCells = - game->getWorld ()->exploreCells (newPos, sightRange, teamIndex, - this); - - // Cache the result for this unit - cacheExploredCellsKey.first = newPos; - cacheExploredCellsKey.second = sightRange; - } - } - } - - void Unit::logSynchData (string file, int line, string source) - { - logSynchDataCommon (file, line, source, false); - } - void Unit::logSynchDataThreaded (string file, int line, string source) - { - logSynchDataCommon (file, line, source, true); - } - void Unit::logSynchDataCommon (string file, int line, string source, - bool threadedMode) - { - if (SystemFlags:: - getSystemSettingType (SystemFlags::debugWorldSynch).enabled == true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "FrameCount [%d] Unit = %d [%s][%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, progress [" - MG_I64_SPECIFIER - "], progress2 [%d] random [%d]\nUnit Path [%s]\n", - getFrameCount (), id, getFullName (false).c_str (), - faction->getType ()->getName (false).c_str (), - //getDesc().c_str(), - pos.getString ().c_str (), - lastPos.getString ().c_str (), - targetPos.getString ().c_str (), - targetVec.getString ().c_str (), - meetingPos.getString ().c_str (), -// lastRotation, -// targetRotation, -// rotation, - progress, - progress2, - random.getLastNumber (), - (unitPath != - NULL ? unitPath->toString ().c_str () : "NULL")); - - if (lastSynchDataString != string (szBuf) || - lastFile != file || lastLine != line || lastSource != source) - { - lastSynchDataString = string (szBuf); - lastFile = file; - lastSource = source; - - char szBufDataText[8096] = ""; - snprintf (szBufDataText, 8096, - "----------------------------------- START [FRAME %d UNIT: %d - %s] ------------------------------------------------\n", - getFrameCount (), this->id, - this->getType ()->getName (false).c_str ()); - string logDataText = szBufDataText; - - snprintf (szBufDataText, 8096, "[%s::%d]\n", - extractFileFromDirectoryPath (file).c_str (), line); - logDataText += szBufDataText; - - if (source != "") - { - snprintf (szBufDataText, 8096, "%s ", source.c_str ()); - logDataText += szBufDataText; - } - snprintf (szBufDataText, 8096, "%s\n", szBuf); - logDataText += szBufDataText; - snprintf (szBufDataText, 8096, - "------------------------------------ END [FRAME %d UNIT: %d - %s] ------------------------------------------------\n", - getFrameCount (), this->id, - this->getType ()->getName (false).c_str ()); - logDataText += szBufDataText; -/* - SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [FRAME %d UNIT: %d - %s] ------------------------------------------------\n",getFrameCount(),this->id,this->getType()->getName().c_str()); - SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"[%s::%d]\n",extractFileFromDirectoryPath(file).c_str(),line); - if(source != "") { - SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s ",source.c_str()); + void UnitPathBasic::dumpMemoryList() { + printf("===== START report of Unfreed UnitPathBasic pointers =====\n"); + for (std::map < UnitPathBasic *, bool >::iterator iterMap = + UnitPathBasic::mapMemoryList.begin(); + iterMap != UnitPathBasic::mapMemoryList.end(); ++iterMap) { + printf("************** ==> Unfreed UnitPathBasic pointer [%p]\n", + iterMap->first); + + if (Unit::mapMemoryList2.find(iterMap->first) != + Unit::mapMemoryList2.end()) { + printf("Found owner unit id [%d]\n", + Unit::mapMemoryList2[iterMap->first]); + } } - SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s\n",szBuf); - SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [FRAME %d UNIT: %d - %s] ------------------------------------------------\n",getFrameCount(),this->id,this->getType()->getName().c_str()); -*/ - if (threadedMode == false) - { - SystemFlags::OutputDebug (SystemFlags::debugWorldSynch, "%s", - logDataText.c_str ()); - } - else - { - this->faction->addWorldSynchThreadedLogList (logDataText); - } - } - } - } - - void Unit::addBadHarvestPos (const Vec2i & value) - { - //Chrono chron; - //chron.start(); - badHarvestPosList[value] = getFrameCount (); - cleanupOldBadHarvestPos (); - } - -//void Unit::removeBadHarvestPos(const Vec2i &value) { -// std::map::iterator iter = badHarvestPosList.find(value); -// if(iter != badHarvestPosList.end()) { -// badHarvestPosList.erase(value); -// } -// cleanupOldBadHarvestPos(); -//} - - void Unit::cleanupOldBadHarvestPos () - { - const unsigned int cleanupInterval = (GameConstants::updateFps * 5); - bool needToCleanup = (getFrameCount () % cleanupInterval == 0); - - //printf("========================> cleanupOldBadHarvestPos() [%d] badHarvestPosList.size [%ld] cleanupInterval [%d] getFrameCount() [%d] needToCleanup [%d]\n",getFrameCount(),badHarvestPosList.size(),cleanupInterval,getFrameCount(),needToCleanup); - - if (needToCleanup == true) - { - //printf("========================> cleanupOldBadHarvestPos() [%d] badHarvestPosList.size [%ld]\n",getFrameCount(),badHarvestPosList.size()); - - std::vector < Vec2i > purgeList; - for (std::map < Vec2i, int >::iterator iter = - badHarvestPosList.begin (); iter != badHarvestPosList.end (); - ++iter) - { - if (getFrameCount () - iter->second >= cleanupInterval) - { - //printf("cleanupOldBadHarvestPos() [%d][%d]\n",getFrameCount(),iter->second); - purgeList.push_back (iter->first); - } - } - - if (purgeList.empty () == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "[cleaning old bad harvest targets] purgeList.size() [" - MG_SIZE_T_SPECIFIER "]", purgeList.size ()); - logSynchData (extractFileFromDirectoryPath (__FILE__).c_str (), - __LINE__, szBuf); - - for (int i = 0; i < (int) purgeList.size (); ++i) - { - const Vec2i & item = purgeList[i]; - badHarvestPosList.erase (item); - } - } - } - } - - void Unit::setLastHarvestResourceTarget (const Vec2i * pos) - { - if (pos == NULL) - { - lastHarvestResourceTarget.first = Vec2i (0); - //lastHarvestResourceTarget.second = 0; - } - else - { - const Vec2i resourceLocation = *pos; - if (resourceLocation != lastHarvestResourceTarget.first) - { - lastHarvestResourceTarget.first = resourceLocation; - - //Chrono chron; - //chron.start(); - lastHarvestResourceTarget.second = getFrameCount (); - } - else - { - // If we cannot harvest for > 10 seconds tag the position - // as a bad one - const unsigned int addInterval = (GameConstants::updateFps * 5); - if (lastHarvestResourceTarget.second - getFrameCount () >= - addInterval) - { - //printf("-----------------------> setLastHarvestResourceTarget() [%d][%d]\n",getFrameCount(),lastHarvestResourceTarget.second); - addBadHarvestPos (resourceLocation); - } - } - } - } - -//void Unit::addCurrentTargetPathTakenCell(const Vec2i &target,const Vec2i &cell) { -// if(currentTargetPathTaken.first != target) { -// currentTargetPathTaken.second.clear(); -// } -// currentTargetPathTaken.first = target; -// currentTargetPathTaken.second.push_back(cell); -//} - - void Unit::setLastPathfindFailedFrameToCurrentFrame () - { - lastPathfindFailedFrame = getFrameCount (); - } - - bool Unit::isLastPathfindFailedFrameWithinCurrentFrameTolerance () const - { - //static const bool enablePathfinderEnlargeMaxNodes = Config::getInstance().getBool("EnablePathfinderEnlargeMaxNodes","false"); - static const bool enablePathfinderEnlargeMaxNodes = false; - bool result = enablePathfinderEnlargeMaxNodes; - if (enablePathfinderEnlargeMaxNodes) - { - const uint32 MIN_FRAME_ELAPSED_RETRY = 960; - result = - (getFrameCount () - lastPathfindFailedFrame >= - MIN_FRAME_ELAPSED_RETRY); - } - return result; - } - - void Unit::setLastStuckFrameToCurrentFrame () - { - lastStuckFrame = getFrameCount (); - } - - bool Unit::isLastStuckFrameWithinCurrentFrameTolerance (bool evalMode) - { - //const int MIN_FRAME_ELAPSED_RETRY = 300; - const uint32 MAX_BLOCKED_FRAME_THRESHOLD = 25000; - uint32 MIN_FRAME_ELAPSED_RETRY = 6; - if (lastStuckFrame < MAX_BLOCKED_FRAME_THRESHOLD) - { - if (evalMode == true) - { - MIN_FRAME_ELAPSED_RETRY = 4; - } - else - { - MIN_FRAME_ELAPSED_RETRY = - random.randRange (2, 6, intToStr (__LINE__)); - } - } - else - { - if (evalMode == true) - { - MIN_FRAME_ELAPSED_RETRY = 7; - } - else - { - MIN_FRAME_ELAPSED_RETRY = - random.randRange (6, 8, intToStr (__LINE__)); - } - } - bool result (getFrameCount () - lastStuckFrame <= - (MIN_FRAME_ELAPSED_RETRY * 100)); - return result; - } - - Vec2i Unit::getPosWithCellMapSet () const - { - Vec2i cellMapPos = - this->getType ()->getFirstOccupiedCellInCellMap (pos); - return cellMapPos; - } - - string Unit::getUniquePickName () const - { - string result = intToStr (id) + " - " + type->getName (false) + " : "; - result += pos.getString (); - return result; - } - - Vec2i Unit::getPos () - { - Vec2i result; - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (mutexCommands, mutexOwnerId); - result = this->pos; - safeMutex.ReleaseLock (); - - return result; - } - - void Unit::clearCaches () - { - cachedFow.surfPosAlphaList.clear (); - cachedFowPos = Vec2i (0, 0); - - cacheExploredCells.exploredCellList.clear (); - cacheExploredCells.visibleCellList.clear (); - cacheExploredCellsKey.first = Vec2i (-1, -1); - cacheExploredCellsKey.second = -1; - - if (unitPath != NULL) - { - unitPath->clearCaches (); - } - - lastHarvestedResourcePos = Vec2i (0, 0); - } - - bool Unit::showTranslatedTechTree () const - { - return (this->game != - NULL ? this->game->showTranslatedTechTree () : true); - } - - string Unit::getNetworkCRCDecHpList () const - { - string result = ""; - if (networkCRCDecHpList.empty () == false) - { - for (unsigned int index = 0; index < networkCRCDecHpList.size (); - ++index) - { - result += networkCRCDecHpList[index] + " "; - } - } - return result; - } - std::string Unit::toString (bool crcMode) const - { - std::string result = ""; - - result += "id = " + intToStr (this->id); - if (this->type != NULL) - { - result += - " name [" + this->type->getName (false) + "][" + - intToStr (this->type->getId ()) + "]"; - } - - if (this->faction != NULL) - { - result += - "\nFactionIndex = " + intToStr (this->faction->getIndex ()) + "\n"; - result += - "teamIndex = " + intToStr (this->faction->getTeam ()) + "\n"; - result += - "startLocationIndex = " + - intToStr (this->faction->getStartLocationIndex ()) + "\n"; - if (crcMode == false) - { - result += - "thisFaction = " + intToStr (this->faction->getThisFaction ()) + - "\n"; - result += - "control = " + intToStr (this->faction->getControlType ()) + "\n"; - } - if (this->faction->getType () != NULL) - { - result += - "factionName = " + this->faction->getType ()->getName (false) + - "\n"; - } - } - - result += " hp = " + intToStr (this->hp); - result += " ep = " + intToStr (this->ep); - result += " loadCount = " + intToStr (this->loadCount); - result += " deadCount = " + intToStr (this->deadCount); - result += " progress = " + intToStr (this->progress); - result += "\n"; - result += "networkCRCLogInfo = " + networkCRCLogInfo; - result += "\n"; - if (crcMode == false) - { - result += " lastAnimProgress = " + intToStr (this->lastAnimProgress); - result += " animProgress = " + intToStr (this->animProgress); - result += " highlight = " + floatToStr (this->highlight, 6); - } - result += " progress2 = " + intToStr (this->progress2); - result += " kills = " + intToStr (this->kills); - result += " enemyKills = " + intToStr (this->enemyKills); - result += "\n"; - - // WARNING!!! Don't access the Unit pointer in this->targetRef in this method or it causes - // a stack overflow - if (this->targetRef.getUnitId () >= 0) - { - //result += " targetRef = " + this->targetRef.getUnit()->toString(); - result += - " targetRef = " + intToStr (this->targetRef.getUnitId ()) + - " - factionIndex = " + - intToStr (this->targetRef.getUnitFaction ()->getIndex ()); - } - - result += " currField = " + intToStr (this->currField); - result += " targetField = " + intToStr (this->targetField); - if (level != NULL) - { - result += " level = " + level->getName (); - } - result += "\n"; - result += " pos = " + pos.getString (); - result += " lastPos = " + lastPos.getString (); - result += "\n"; - result += " targetPos = " + targetPos.getString (); - result += " targetVec = " + targetVec.getString (); - result += " meetingPos = " + meetingPos.getString (); - result += "\n"; - - if (crcMode == false) - { - result += " lastRotation = " + floatToStr (this->lastRotation, 6); - result += " targetRotation = " + floatToStr (this->targetRotation, 6); - result += " rotation = " + floatToStr (this->rotation, 6); - } - - if (loadType != NULL) - { - result += " loadType = " + loadType->getName (); - } - - if (currSkill != NULL) - { - result += " currSkill = " + currSkill->getName (); - } - result += "\n"; - - result += " toBeUndertaken = " + intToStr (this->toBeUndertaken); - result += " alive = " + intToStr (this->alive); - result += " showUnitParticles = " + intToStr (this->showUnitParticles); - - result += " totalUpgrade = " + totalUpgrade.toString (); - result += " " + this->unitPath->toString () + "\n"; - result += "\n"; - - result += "Command count = " + intToStr (commands.size ()) + "\n"; - - int cmdIdx = 0; - for (Commands::const_iterator iterList = commands.begin (); - iterList != commands.end (); ++iterList) - { - result += " index = " + intToStr (cmdIdx) + " "; - const Command *cmd = *iterList; - if (cmd != NULL) - { - result += cmd->toString (false) + "\n"; - } - cmdIdx++; - } - result += "\n"; - -// int obsIdx = 0; -// for(Observers::const_iterator iterList = observers.begin(); iterList != observers.end(); ++iterList) { -// const UnitObserver *observer = *iterList; -// if(observer != NULL) { -// } -// -// obsIdx++; -// } - - result += "\n"; - - result += "modelFacing = " + intToStr (modelFacing.asInt ()) + "\n"; - - result += - "retryCurrCommandCount = " + intToStr (retryCurrCommandCount) + "\n"; - - result += "screenPos = " + screenPos.getString () + "\n"; - - result += "currentUnitTitle = " + currentUnitTitle + "\n"; - - result += "inBailOutAttempt = " + intToStr (inBailOutAttempt) + "\n"; - - result += "random = " + intToStr (random.getLastNumber ()) + "\n"; - if (this->random.getLastCaller () != "") - { - result += "randomlastCaller = " + random.getLastCaller () + "\n"; - } - result += - "pathFindRefreshCellCount = " + intToStr (pathFindRefreshCellCount) + - "\n"; - - result += - "currentPathFinderDesiredFinalPos = " + - currentPathFinderDesiredFinalPos.getString () + "\n"; - - result += "lastStuckFrame = " + uIntToStr (lastStuckFrame) + "\n"; - result += "lastStuckPos = " + lastStuckPos.getString () + "\n"; - - if (attackParticleSystems.empty () == false) - { - result += - "attackParticleSystems count = " + - intToStr (attackParticleSystems.size ()) + "\n"; - } - if (networkCRCParticleLogInfo != "") - { - result += - "networkCRCParticleLogInfo = " + networkCRCParticleLogInfo + "\n"; - } - if (networkCRCDecHpList.empty () == false) - { - result += - "getNetworkCRCDecHpList() = " + getNetworkCRCDecHpList () + "\n"; - } - - if (getParticleInfo () != "") - { - result += "getParticleInfo() = " + getParticleInfo () + "\n"; - } - for (unsigned int index = 0; index < attackParticleSystems.size (); - ++index) - { - ParticleSystem *ps = attackParticleSystems[index]; - if (ps != NULL && - Renderer::getInstance ().validateParticleSystemStillExists (ps, - rsGame) - == true) - { - - result += - "attackParticleSystems #" + intToStr (index) + " = " + - ps->toString () + "\n"; - } - } - - return result; - } - - void Unit::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *unitNode = rootNode->addChild ("Unit"); - -// const int id; - unitNode->addAttribute ("id", intToStr (id), mapTagReplacements); - // For info purposes only - unitNode->addAttribute ("name", type->getName (false), - mapTagReplacements); - -// int hp; - unitNode->addAttribute ("hp", intToStr (hp), mapTagReplacements); -// int ep; - unitNode->addAttribute ("ep", intToStr (ep), mapTagReplacements); -// int loadCount; - unitNode->addAttribute ("loadCount", intToStr (loadCount), - mapTagReplacements); -// int deadCount; - unitNode->addAttribute ("deadCount", intToStr (deadCount), - mapTagReplacements); -// float progress; //between 0 and 1 - unitNode->addAttribute ("progress", intToStr (progress), - mapTagReplacements); -// float lastAnimProgress; //between 0 and 1 - unitNode->addAttribute ("lastAnimProgress", intToStr (lastAnimProgress), - mapTagReplacements); -// float animProgress; //between 0 and 1 - unitNode->addAttribute ("animProgress", intToStr (animProgress), - mapTagReplacements); -// float highlight; - unitNode->addAttribute ("highlight", floatToStr (highlight, 6), - mapTagReplacements); -// int progress2; - unitNode->addAttribute ("progress2", intToStr (progress2), - mapTagReplacements); -// int kills; - unitNode->addAttribute ("kills", intToStr (kills), mapTagReplacements); -// int enemyKills; - unitNode->addAttribute ("enemyKills", intToStr (enemyKills), - mapTagReplacements); -// UnitReference targetRef; - targetRef.saveGame (unitNode); -// -// Field currField; - unitNode->addAttribute ("currField", intToStr (currField), - mapTagReplacements); -// Field targetField; - unitNode->addAttribute ("targetField", intToStr (targetField), - mapTagReplacements); -// const Level *level; - if (level != NULL) - { - level->saveGame (unitNode); - } -// Vec2i pos; - unitNode->addAttribute ("pos", pos.getString (), mapTagReplacements); -// Vec2i lastPos; - unitNode->addAttribute ("lastPos", lastPos.getString (), - mapTagReplacements); -// Vec2i targetPos; //absolute target pos - unitNode->addAttribute ("targetPos", targetPos.getString (), - mapTagReplacements); -// Vec3f targetVec; - unitNode->addAttribute ("targetVec", targetVec.getString (), - mapTagReplacements); -// Vec2i meetingPos; - unitNode->addAttribute ("meetingPos", meetingPos.getString (), - mapTagReplacements); -// -// float lastRotation; //in degrees - unitNode->addAttribute ("lastRotation", floatToStr (lastRotation, 6), - mapTagReplacements); -// float targetRotation; - unitNode->addAttribute ("targetRotation", - floatToStr (targetRotation, 6), - mapTagReplacements); -// float rotation; - unitNode->addAttribute ("rotation", floatToStr (rotation, 6), - mapTagReplacements); -// float targetRotationZ; - unitNode->addAttribute ("targetRotationZ", - floatToStr (targetRotationZ, 6), - mapTagReplacements); -// float targetRotationX; - unitNode->addAttribute ("targetRotationX", - floatToStr (targetRotationX, 6), - mapTagReplacements); -// float rotationZ; - unitNode->addAttribute ("rotationZ", floatToStr (rotationZ, 6), - mapTagReplacements); -// float rotationX; - unitNode->addAttribute ("rotationX", floatToStr (rotationX, 6), - mapTagReplacements); -// const UnitType *type; - unitNode->addAttribute ("type", type->getName (false), - mapTagReplacements); - - unitNode->addAttribute ("preMorph_type", - (preMorph_type != - NULL ? preMorph_type->getName (false) : ""), - mapTagReplacements); - -// const ResourceType *loadType; - if (loadType != NULL) - { - unitNode->addAttribute ("loadType", loadType->getName (), - mapTagReplacements); - } -// const SkillType *currSkill; - if (currSkill != NULL) - { - unitNode->addAttribute ("currSkillName", currSkill->getName (), - mapTagReplacements); - unitNode->addAttribute ("currSkillClass", - intToStr (currSkill->getClass ()), - mapTagReplacements); - } -// int lastModelIndexForCurrSkillType; - unitNode->addAttribute ("lastModelIndexForCurrSkillType", - intToStr (lastModelIndexForCurrSkillType), - mapTagReplacements); -// int animationRandomCycleCount; - unitNode->addAttribute ("animationRandomCycleCount", - intToStr (animationRandomCycleCount), - mapTagReplacements); -// -// bool toBeUndertaken; - unitNode->addAttribute ("toBeUndertaken", intToStr (toBeUndertaken), - mapTagReplacements); -// bool alive; - unitNode->addAttribute ("alive", intToStr (alive), mapTagReplacements); -// bool showUnitParticles; - unitNode->addAttribute ("showUnitParticles", - intToStr (showUnitParticles), - mapTagReplacements); -// Faction *faction; -// ParticleSystem *fire; - int linkFireIndex = -1; - if (this->fire != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (this->fire, - rsGame) == true) - { - //fire->saveGame(unitNode); - bool fireInSystemList = false; - if (fireParticleSystems.empty () == false) - { - for (unsigned int i = 0; i < fireParticleSystems.size (); ++i) - { - ParticleSystem *ps = fireParticleSystems[i]; - if (ps == this->fire) - { - linkFireIndex = i; - fireInSystemList = true; - break; - } - } - } - if (fireInSystemList == false) - { - this->fire->saveGame (unitNode); - } - } -// TotalUpgrade totalUpgrade; - totalUpgrade.saveGame (unitNode); -// Map *map; -// -// UnitPathInterface *unitPath; - unitPath->saveGame (unitNode); -// WaypointPath waypointPath; -// -// Commands commands; - for (Commands::iterator it = commands.begin (); it != commands.end (); - ++it) - { - (*it)->saveGame (unitNode, faction); - } -// Observers observers; - //for(Observers::iterator it = observers.begin(); it != observers.end(); ++it) { - // (*it)->saveGame(unitNode); - //} - -// vector unitParticleSystems; - if (unitParticleSystems.empty () == false) - { - XmlNode *unitParticleSystemsNode = - unitNode->addChild ("unitParticleSystems"); - - for (unsigned int i = 0; i < unitParticleSystems.size (); ++i) - { - UnitParticleSystem *ups = unitParticleSystems[i]; - if (ups != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (ups, - rsGame) == - true) - { - ups->saveGame (unitParticleSystemsNode); - } - } - } -// vector queuedUnitParticleSystemTypes; - if (queuedUnitParticleSystemTypes.empty () == false) - { - XmlNode *queuedUnitParticleSystemTypesNode = - unitNode->addChild ("queuedUnitParticleSystemTypes"); - for (unsigned int i = 0; i < queuedUnitParticleSystemTypes.size (); - ++i) - { - UnitParticleSystemType *upst = queuedUnitParticleSystemTypes[i]; - if (upst != NULL) - { - upst->saveGame (queuedUnitParticleSystemTypesNode); - } - } - } -// UnitParticleSystems damageParticleSystems; - if (damageParticleSystems.empty () == false) - { - XmlNode *damageParticleSystemsNode = - unitNode->addChild ("damageParticleSystems"); - for (unsigned int i = 0; i < damageParticleSystems.size (); ++i) - { - UnitParticleSystem *ups = damageParticleSystems[i]; - if (ups != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (ups, - rsGame) == - true) - { - ups->saveGame (damageParticleSystemsNode); - } - } - } -// std::map damageParticleSystemsInUse; - if (damageParticleSystemsInUse.empty () == false) - { - XmlNode *damageParticleSystemsInUseNode = - unitNode->addChild ("damageParticleSystemsInUse"); - - for (std::map < int, UnitParticleSystem * >::const_iterator iterMap = - damageParticleSystemsInUse.begin (); - iterMap != damageParticleSystemsInUse.end (); ++iterMap) - { - if (iterMap->second != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (iterMap-> - second, - rsGame) == - true) - { - XmlNode *damageParticleSystemsInUseNode2 = - damageParticleSystemsInUseNode->addChild - ("damageParticleSystemsInUse"); - - damageParticleSystemsInUseNode2->addAttribute ("key", - intToStr - (iterMap->first), - mapTagReplacements); - iterMap->second->saveGame (damageParticleSystemsInUseNode2); - } - } - } - -// vector fireParticleSystems; - if (fireParticleSystems.empty () == false) - { - XmlNode *fireParticleSystemsNode = - unitNode->addChild ("fireParticleSystems"); - - if (linkFireIndex >= 0) - { - fireParticleSystemsNode->addAttribute ("fireParticleLink", - intToStr (linkFireIndex), - mapTagReplacements); - } - - for (unsigned int i = 0; i < fireParticleSystems.size (); ++i) - { - ParticleSystem *ps = fireParticleSystems[i]; - if (ps != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (ps, - rsGame) == - true) - { - ps->saveGame (fireParticleSystemsNode); - } - } - } - -// vector smokeParticleSystems; - if (smokeParticleSystems.empty () == false) - { - XmlNode *smokeParticleSystemsNode = - unitNode->addChild ("smokeParticleSystems"); - for (unsigned int i = 0; i < smokeParticleSystems.size (); ++i) - { - UnitParticleSystem *ups = smokeParticleSystems[i]; - if (ups != NULL - && Renderer:: - getInstance ().validateParticleSystemStillExists (ups, - rsGame) == - true) - { - ups->saveGame (smokeParticleSystemsNode); - //printf("Saving smoke particles:\n[%s]\n",ups->toString().c_str()); - } - } - } - -// CardinalDir modelFacing; - unitNode->addAttribute ("modelFacing", intToStr (modelFacing), - mapTagReplacements); - -// std::string lastSynchDataString; - unitNode->addAttribute ("lastSynchDataString", lastSynchDataString, - mapTagReplacements); -// std::string lastFile; - unitNode->addAttribute ("lastFile", lastFile, mapTagReplacements); -// int lastLine; - unitNode->addAttribute ("lastLine", intToStr (lastLine), - mapTagReplacements); -// std::string lastSource; - unitNode->addAttribute ("lastSource", lastSource, mapTagReplacements); -// int lastRenderFrame; - unitNode->addAttribute ("lastRenderFrame", intToStr (lastRenderFrame), - mapTagReplacements); -// bool visible; - unitNode->addAttribute ("visible", intToStr (visible), - mapTagReplacements); -// int retryCurrCommandCount; - unitNode->addAttribute ("retryCurrCommandCount", - intToStr (retryCurrCommandCount), - mapTagReplacements); -// Vec3f screenPos; - unitNode->addAttribute ("screenPos", screenPos.getString (), - mapTagReplacements); -// string currentUnitTitle; - unitNode->addAttribute ("currentUnitTitle", currentUnitTitle, - mapTagReplacements); -// -// bool inBailOutAttempt; - unitNode->addAttribute ("inBailOutAttempt", intToStr (inBailOutAttempt), - mapTagReplacements); -// //std::vector > badHarvestPosList; -// std::map badHarvestPosList; - for (std::map < Vec2i, int >::const_iterator iterMap = - badHarvestPosList.begin (); iterMap != badHarvestPosList.end (); - ++iterMap) - { - XmlNode *badHarvestPosListNode = - unitNode->addChild ("badHarvestPosList"); - - badHarvestPosListNode->addAttribute ("key", - iterMap->first.getString (), - mapTagReplacements); - badHarvestPosListNode->addAttribute ("value", - intToStr (iterMap->second), - mapTagReplacements); - } - -// //time_t lastBadHarvestListPurge; -// std::pair lastHarvestResourceTarget; - XmlNode *lastHarvestResourceTargetNode = - unitNode->addChild ("lastHarvestResourceTarget"); - lastHarvestResourceTargetNode->addAttribute ("key", - lastHarvestResourceTarget.first. - getString (), - mapTagReplacements); - lastHarvestResourceTargetNode->addAttribute ("value", - intToStr - (lastHarvestResourceTarget.second), - mapTagReplacements); - -// //std::pair > currentTargetPathTaken; -// static Game *game; -// -// bool ignoreCheckCommand; - unitNode->addAttribute ("ignoreCheckCommand", - intToStr (ignoreCheckCommand), - mapTagReplacements); -// uint32 lastStuckFrame; - unitNode->addAttribute ("lastStuckFrame", uIntToStr (lastStuckFrame), - mapTagReplacements); -// Vec2i lastStuckPos; - unitNode->addAttribute ("lastStuckPos", lastStuckPos.getString (), - mapTagReplacements); -// uint32 lastPathfindFailedFrame; - unitNode->addAttribute ("lastPathfindFailedFrame", - intToStr (lastPathfindFailedFrame), - mapTagReplacements); -// Vec2i lastPathfindFailedPos; - unitNode->addAttribute ("lastPathfindFailedPos", - lastPathfindFailedPos.getString (), - mapTagReplacements); -// bool usePathfinderExtendedMaxNodes; - unitNode->addAttribute ("usePathfinderExtendedMaxNodes", - intToStr (usePathfinderExtendedMaxNodes), - mapTagReplacements); -// int maxQueuedCommandDisplayCount; - unitNode->addAttribute ("maxQueuedCommandDisplayCount", - intToStr (maxQueuedCommandDisplayCount), - mapTagReplacements); -// UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; - currentAttackBoostOriginatorEffect.saveGame (unitNode); -// std::vector currentAttackBoostEffects; - for (unsigned int i = 0; i < currentAttackBoostEffects.size (); ++i) - { - UnitAttackBoostEffect *uabe = currentAttackBoostEffects[i]; - if (uabe != NULL) - { - uabe->saveGame (unitNode); - } - } - -// Mutex *mutexCommands; -// -// //static Mutex mutexDeletedUnits; -// //static std::map deletedUnits; -// -// bool changedActiveCommand; - unitNode->addAttribute ("changedActiveCommand", - intToStr (changedActiveCommand), - mapTagReplacements); -// int lastAttackerUnitId; - unitNode->addAttribute ("lastAttackerUnitId", - intToStr (lastAttackerUnitId), - mapTagReplacements); -// int lastAttackedUnitId; - unitNode->addAttribute ("lastAttackedUnitId", - intToStr (lastAttackedUnitId), - mapTagReplacements); -// CauseOfDeathType causeOfDeath; - unitNode->addAttribute ("causeOfDeath", intToStr (causeOfDeath), - mapTagReplacements); - - //pathfindFailedConsecutiveFrameCount - unitNode->addAttribute ("pathfindFailedConsecutiveFrameCount", - intToStr (pathfindFailedConsecutiveFrameCount), - mapTagReplacements); - - unitNode->addAttribute ("currentPathFinderDesiredFinalPos", - currentPathFinderDesiredFinalPos.getString (), - mapTagReplacements); - - unitNode->addAttribute ("random", intToStr (random.getLastNumber ()), - mapTagReplacements); - unitNode->addAttribute ("pathFindRefreshCellCount", - intToStr (pathFindRefreshCellCount), - mapTagReplacements); - } - - Unit *Unit::loadGame (const XmlNode * rootNode, GameSettings * settings, - Faction * faction, World * world) - { - const XmlNode *unitNode = rootNode; - - int newUnitId = unitNode->getAttribute ("id")->getIntValue (); - Vec2i newUnitPos = - Vec2i::strToVec2 (unitNode->getAttribute ("pos")->getValue ()); - string newUnitType = unitNode->getAttribute ("type")->getValue (); - const UnitType *ut = faction->getType ()->getUnitType (newUnitType); - CardinalDir newModelFacing = - static_cast < CardinalDir > - (unitNode->getAttribute ("modelFacing")->getIntValue ()); - -// Unit *result = new Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, -// const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing) : BaseColorPickEntity(), id(id) { - - UnitPathInterface *newpath = NULL; - switch (settings->getPathFinderType ()) - { - case pfBasic: - newpath = new UnitPathBasic (); - break; - default: - throw - megaglest_runtime_error ("detected unsupported pathfinder type!"); - } - - newpath->loadGame (unitNode); - //Unit *result = new Unit(getNextUnitId(f), newpath, Vec2i(0), ut, f, &map, CardinalDir::NORTH); - //Unit(int id, UnitPathInterface *path, const Vec2i &pos, const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing); - Unit *result = new Unit (newUnitId, newpath, newUnitPos, ut, faction, - world->getMapPtr (), newModelFacing); - - if (unitNode->hasAttribute ("preMorph_name") == true) - { - string newUnitType_preMorph = - unitNode->getAttribute ("preMorph_name")->getValue (); - const UnitType *ut_premorph = - faction->getType ()->getUnitType (newUnitType_preMorph); - result->preMorph_type = ut_premorph; - } - - result->lastRotation = - unitNode->getAttribute ("lastRotation")->getFloatValue (); - result->targetRotation = - unitNode->getAttribute ("targetRotation")->getFloatValue (); - result->rotation = - unitNode->getAttribute ("rotation")->getFloatValue (); - - //world->placeUnitAtLocation(newUnitPos, generationArea, unit, true); - //result->setPos(newUnitPos); - //Vec2i meetingPos = newUnitPos-Vec2i(1); - //result->setMeetingPos(meetingPos); - result->pos = newUnitPos; - result->lastPos = - Vec2i::strToVec2 (unitNode->getAttribute ("lastPos")->getValue ()); - result->meetingPos = - Vec2i::strToVec2 (unitNode->getAttribute ("meetingPos")->getValue ()); - // Attempt to improve performance - //result->exploreCells(); - //result->calculateFogOfWarRadius(); - // -------------------------- - - result->hp = unitNode->getAttribute ("hp")->getIntValue (); -// int ep; - result->ep = unitNode->getAttribute ("ep")->getIntValue (); -// int loadCount; - result->loadCount = - unitNode->getAttribute ("loadCount")->getIntValue (); -// int deadCount; - result->deadCount = - unitNode->getAttribute ("deadCount")->getIntValue (); -// float progress; //between 0 and 1 - try - { - result->progress = - unitNode->getAttribute ("progress")->getIntValue (); - } -#ifdef WIN32 - catch (const exception &) - { -#else - catch (const exception & ex) - { + } #endif - result->progress = - unitNode->getAttribute ("progress")->getFloatValue (); - } -// float lastAnimProgress; //between 0 and 1 - try - { - result->lastAnimProgress = - unitNode->getAttribute ("lastAnimProgress")->getIntValue (); - } -#ifdef WIN32 - catch (const exception &) - { -#else - catch (const exception & ex) - { + + void UnitPathBasic::clearCaches() { + this->blockCount = 0; + this->pathQueue.clear(); + } + + bool UnitPathBasic::isEmpty() const { + return pathQueue.empty(); + } + + bool UnitPathBasic::isBlocked() const { + return blockCount >= maxBlockCount; + } + + bool UnitPathBasic::isStuck() const { + return (isBlocked() == true && blockCount >= (maxBlockCount * 2)); + } + + void UnitPathBasic::clear() { + pathQueue.clear(); + blockCount = 0; + } + + void UnitPathBasic::incBlockCount() { + pathQueue.clear(); + blockCount++; + } + + void UnitPathBasic::add(const Vec2i & path) { + if (this->map != NULL) { + if (this->map->isInside(path) == false) { + throw megaglest_runtime_error("Invalid map path position = " + + path.getString() + " map w x h = " + + intToStr(map->getW()) + " " + + intToStr(map->getH())); + } else if (this-> + map->isInsideSurface(this->map->toSurfCoords(path)) == + false) { + throw + megaglest_runtime_error("Invalid map surface path position = " + + path.getString() + + " map surface w x h = " + + intToStr(map->getSurfaceW()) + " " + + intToStr(map->getSurfaceH())); + } + } + + if (Thread::isCurrentThreadMainThread() == false) { + throw + megaglest_runtime_error + ("Invalid access to UnitPathBasic add from outside main thread current id = " + + intToStr(Thread::getCurrentThreadId()) + " main = " + + intToStr(Thread::getMainThreadId())); + } + + pathQueue.push_back(path); + } + + Vec2i UnitPathBasic::pop(bool removeFrontPos) { + if (pathQueue.empty() == true) { + throw megaglest_runtime_error("pathQueue.size() = " + + intToStr(pathQueue.size())); + } + Vec2i p = pathQueue.front(); + if (removeFrontPos == true) { + if (Thread::isCurrentThreadMainThread() == false) { + throw + megaglest_runtime_error + ("Invalid access to UnitPathBasic delete from outside main thread current id = " + + intToStr(Thread::getCurrentThreadId()) + " main = " + + intToStr(Thread::getMainThreadId())); + } + + pathQueue.erase(pathQueue.begin()); + } + return p; + } + std::string UnitPathBasic::toString()const { + std::string result = + "unit path blockCount = " + intToStr(blockCount) + + "\npathQueue size = " + intToStr(pathQueue.size()); + for (int idx = 0; idx < (int) pathQueue.size(); ++idx) { + result += + " index = " + intToStr(idx) + " value = " + + pathQueue[idx].getString(); + } + + return result; + } + + void UnitPathBasic::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *unitPathBasicNode = rootNode->addChild("UnitPathBasic"); + + // int blockCount; + unitPathBasicNode->addAttribute("blockCount", intToStr(blockCount), + mapTagReplacements); + // vector pathQueue; + for (unsigned int i = 0; i < pathQueue.size(); ++i) { + Vec2i & vec = pathQueue[i]; + + XmlNode *pathQueueNode = unitPathBasicNode->addChild("pathQueue"); + pathQueueNode->addAttribute("vec", vec.getString(), + mapTagReplacements); + } + } + + void UnitPathBasic::loadGame(const XmlNode * rootNode) { + const XmlNode *unitPathBasicNode = rootNode->getChild("UnitPathBasic"); + + blockCount = + unitPathBasicNode->getAttribute("blockCount")->getIntValue(); + + pathQueue.clear(); + vector < XmlNode * >pathqueueNodeList = + unitPathBasicNode->getChildList("pathQueue"); + for (unsigned int i = 0; i < pathqueueNodeList.size(); ++i) { + XmlNode *node = pathqueueNodeList[i]; + + Vec2i vec = + Vec2i::strToVec2(node->getAttribute("vec")->getValue()); + pathQueue.push_back(vec); + } + } + + Checksum UnitPathBasic::getCRC() { + Checksum crcForPath; + + crcForPath.addInt(blockCount); + crcForPath.addInt((int) pathQueue.size()); + + return crcForPath; + } + + // ===================================================== + // class UnitPath + // ===================================================== + + //void WaypointPath::condense() { + // if (size() < 2) { + // return; + // } + // iterator prev, curr; + // prev = curr = begin(); + // while (++curr != end()) { + // if (prev->dist(*curr) < 3.f) { + // prev = erase(prev); + // } else { + // ++prev; + // } + // } + //} + + std::string UnitPath::toString()const { + std::string result = + "unit path blockCount = " + intToStr(blockCount) + + " pathQueue size = " + intToStr(size()); + result += " path = "; + for (const_iterator it = begin(); it != end(); ++it) { + result += " [" + intToStr(it->x) + "," + intToStr(it->y) + "]"; + } + + return result; + } + + // ===================================================== + // class UnitReference + // ===================================================== + + UnitReference::UnitReference() { + id = -1; + faction = NULL; + } + + UnitReference & UnitReference::operator= (const Unit * unit) { + if (unit == NULL) { + id = -1; + faction = NULL; + } else { + id = unit->getId(); + faction = unit->getFaction(); + } + + return *this; + } + + Unit *UnitReference::getUnit() const { + if (faction != NULL) { + return faction->findUnit(id); + } + return NULL; + } + + void UnitReference::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *unitRefNode = rootNode->addChild("UnitReference"); + + unitRefNode->addAttribute("id", intToStr(id), mapTagReplacements); + if (faction != NULL) { + unitRefNode->addAttribute("factionIndex", + intToStr(faction->getIndex()), + mapTagReplacements); + } + } + + void UnitReference::loadGame(const XmlNode * rootNode, World * world) { + const XmlNode *unitRefNode = rootNode->getChild("UnitReference"); + + id = unitRefNode->getAttribute("id")->getIntValue(); + if (unitRefNode->hasAttribute("factionIndex") == true) { + int factionIndex = + unitRefNode->getAttribute("factionIndex")->getIntValue(); + if (factionIndex >= world->getFactionCount()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "factionIndex >= world->getFactionCount() [%d] : [%d]", + factionIndex, world->getFactionCount()); + throw megaglest_runtime_error(szBuf); + } + faction = world->getFaction(factionIndex); + } + } + + const bool checkMemory = false; + static map < void *, int >memoryObjectList; + + UnitAttackBoostEffect::UnitAttackBoostEffect() { + if (checkMemory) { + printf("++ Create UnitAttackBoostEffect [%p] before count = %d\n", + this, memoryObjectList[this]); + memoryObjectList[this]++; + printf("++ Create UnitAttackBoostEffect [%p] after count = %d\n", + this, memoryObjectList[this]); + } + + unitId = -1; + unitPtr = NULL; + boost = NULL; + source = NULL; + ups = NULL; + upst = NULL; + } + + UnitAttackBoostEffect::~UnitAttackBoostEffect() { + if (checkMemory) { + printf("-- Delete UnitAttackBoostEffect [%p] count = %d\n", this, + memoryObjectList[this]); + memoryObjectList[this]--; + assert(memoryObjectList[this] == 0); + } + + if (ups != NULL) { + bool particleValid = + Renderer::getInstance().validateParticleSystemStillExists(ups, + rsGame); + if (particleValid == true) { + ups->fade(); + + vector < UnitParticleSystem * >particleSystemToRemove; + particleSystemToRemove.push_back(ups); + + Renderer:: + getInstance().cleanupUnitParticleSystems(particleSystemToRemove, + rsGame); + ups = NULL; + } + } + + delete upst; + upst = NULL; + } + + const Unit *UnitAttackBoostEffect::getSource() { + if (source == NULL && unitPtr != NULL && unitId > 0) { + source = unitPtr->getFaction()->findUnit(unitId); + + //printf("#1 Get source - boost effect unitId = %d unitPtr = %s source = %p\n",unitId,unitPtr->getFullName(false).c_str(),source); + } + //printf("#2 Get source - boost effect unitId = %d unitPtr = %s source = %p\n",unitId,unitPtr->getFullName(false).c_str(),source); + return source; + } + + void UnitAttackBoostEffect::setSource(const Unit * unit) { + source = unit; + } + + void UnitAttackBoostEffect:: + applyLoadedAttackBoostParticles(UnitParticleSystemType * upstPtr, + const XmlNode * node, Unit * unit) { + if (upstPtr != NULL) { + bool showUnitParticles = + Config::getInstance().getBool("UnitParticles", "true"); + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + showUnitParticles = false; + } + if (showUnitParticles == true) { + upst = new UnitParticleSystemType(); + *upst = *upstPtr; + upst->loadGame(node); + + ups = new UnitParticleSystem(200); + //ups->loadGame(node2); + ups->setParticleOwner(unit); + ups->setParticleType(upst); + + upst->setValues(ups); + ups->setPos(unit->getCurrVectorForParticlesystems()); + ups->setRotation(unit->getRotation()); + unit->setMeshPosInParticleSystem(ups); + if (unit->getFaction()->getTexture()) { + ups->setFactionColor(unit->getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + //printf("Adding attack boost particle to deferred buffer: %p\n",ups); + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (ups, rsGame)); + } + } + } + + void UnitAttackBoostEffect::loadGame(const XmlNode * rootNode, + Unit * unit, World * world, + bool applyToOriginator) { + const XmlNode *unitAttackBoostEffectNode = rootNode; + + if (unitAttackBoostEffectNode->hasAttribute("source") == true) { + unitId = + unitAttackBoostEffectNode->getAttribute("source")->getIntValue(); + unitPtr = unit; + source = unit->getFaction()->findUnit(unitId); + + // printf("#1 Loaded boost effect unitId = %d unitPtr = [%d - %s] source = %p\n", + // unitId,unitPtr->getId(),unitPtr->getType()->getName(false).c_str(),source); + } + + // Lets determine the originator unit's attack boost based on the skill used to trigger it + if (unitAttackBoostEffectNode->hasAttribute("source-faction") == true) { + string factionName = + unitAttackBoostEffectNode-> + getAttribute("source-faction")->getValue(); + string unitTypeName = + unitAttackBoostEffectNode-> + getAttribute("source-unit-type")->getValue(); + string skillTypeName = + unitAttackBoostEffectNode-> + getAttribute("source-skill-type")->getValue(); + SkillClass skillClass = + static_cast + (unitAttackBoostEffectNode-> + getAttribute("source-skill-class")->getIntValue()); + + const UnitType *unitType = + world->findUnitTypeByName(factionName, unitTypeName); + if (unitType != NULL) { + const SkillType *skillType = + unitType->getSkillType(skillTypeName, skillClass); + if (skillType != NULL) { + boost = skillType->getAttackBoost(); + + // printf("#2 boost effect unitId = %d unitPtr = [%d - %s] source = %p attackBoost src [%p] dest [%p]\n", + // unitId,unitPtr->getId(),unitPtr->getType()->getName(false).c_str(), + // source,boost->unitParticleSystemTypeForSourceUnit,boost->unitParticleSystemTypeForAffectedUnit); + } + } + } + + if (boost != NULL) { + // printf("unit [%d - %s] applyToOriginator: %d src [%p] dest [%p] Boost attackBoost->enabled = %d:\n%s\n", + // unit->getId(),unit->getType()->getName(false).c_str(),applyToOriginator,boost->unitParticleSystemTypeForSourceUnit,boost->unitParticleSystemTypeForAffectedUnit,boost->enabled,boost->getDesc(false).c_str()); + + if (applyToOriginator == true) { + applyLoadedAttackBoostParticles + (boost->unitParticleSystemTypeForSourceUnit, + unitAttackBoostEffectNode, unit); + } else { + applyLoadedAttackBoostParticles + (boost->unitParticleSystemTypeForAffectedUnit, + unitAttackBoostEffectNode, unit); + } + } else { + printf + ("******!!!! unit [%d - %s] applyToOriginator: %d NO BOOST FOUND!!!\n", + unit->getId(), unit->getType()->getName(false).c_str(), + applyToOriginator); + } + } + + void UnitAttackBoostEffect::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *unitAttackBoostEffectNode = + rootNode->addChild("UnitAttackBoostEffect"); + + // const AttackBoost *boost; + if (boost != NULL) { + boost->saveGame(unitAttackBoostEffectNode); + } + // const Unit *source; + if (getSource() != NULL) { + unitAttackBoostEffectNode->addAttribute("source", + intToStr(getSource()->getId + ()), + mapTagReplacements); + + unitAttackBoostEffectNode->addAttribute("source-faction", + getSource()-> + getFaction()->getType()-> + getName(false), + mapTagReplacements); + unitAttackBoostEffectNode->addAttribute("source-unit-type", + getSource()-> + getType()->getName(false), + mapTagReplacements); + unitAttackBoostEffectNode->addAttribute("source-skill-type", + getSource()->getCurrSkill + ()->getName(), + mapTagReplacements); + unitAttackBoostEffectNode->addAttribute("source-skill-class", + intToStr(getSource + ()->getCurrSkill + ()->getClass()), + mapTagReplacements); + } + // UnitParticleSystem *ups; + if (ups != NULL + && Renderer::getInstance().validateParticleSystemStillExists(ups, + rsGame) + == true) { + ups->saveGame(unitAttackBoostEffectNode); + } + + // UnitParticleSystemType *upst; + if (upst != NULL) { + upst->saveGame(unitAttackBoostEffectNode); + } + } + + UnitAttackBoostEffectOriginator::UnitAttackBoostEffectOriginator() { + skillType = NULL; + currentAppliedEffect = NULL; + } + + UnitAttackBoostEffectOriginator::~UnitAttackBoostEffectOriginator() { + delete currentAppliedEffect; + currentAppliedEffect = NULL; + } + + void UnitAttackBoostEffectOriginator::loadGame(const XmlNode * rootNode, + Unit * unit, + World * world) { + const XmlNode *unitAttackBoostEffectOriginatorNode = + rootNode->getChild("UnitAttackBoostEffectOriginator"); + + SkillClass skillClass = scStop; + string skillTypeName = + unitAttackBoostEffectOriginatorNode-> + getAttribute("skillType")->getValue(); + if (unitAttackBoostEffectOriginatorNode->hasAttribute("skillClass") == + false) { + int skillCount = unit->getType()->getSkillTypeCount(); + for (int index = 0; index < skillCount; ++index) { + const SkillType *st = unit->getType()->getSkillType(index); + if (st->getName() == skillTypeName) { + skillClass = st->getClass(); + break; + } + } + } else { + skillClass = + static_cast + (unitAttackBoostEffectOriginatorNode-> + getAttribute("skillClass")->getIntValue()); + } + + this->skillType = + unit->getType()->getSkillType(skillTypeName, skillClass); + + if (unitAttackBoostEffectOriginatorNode->hasChild + ("currentAttackBoostUnits") == true) { + vector < XmlNode * >currentAttackBoostUnitsNodeList = + unitAttackBoostEffectOriginatorNode->getChildList + ("currentAttackBoostUnits"); + for (unsigned int i = 0; i < currentAttackBoostUnitsNodeList.size(); + ++i) { + XmlNode *node = currentAttackBoostUnitsNodeList[i]; + + int unitId = node->getAttribute("value")->getIntValue(); + currentAttackBoostUnits.push_back(unitId); + } + } + + if (unitAttackBoostEffectOriginatorNode->hasChild + ("UnitAttackBoostEffect") == true) { + currentAppliedEffect = new UnitAttackBoostEffect(); + currentAppliedEffect->loadGame(unitAttackBoostEffectOriginatorNode, + unit, world, true); + } + } + + void UnitAttackBoostEffectOriginator::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *unitAttackBoostEffectOriginatorNode = + rootNode->addChild("UnitAttackBoostEffectOriginator"); + + // const SkillType *skillType; + if (skillType != NULL) { + unitAttackBoostEffectOriginatorNode->addAttribute("skillType", + skillType->getName + (), + mapTagReplacements); + unitAttackBoostEffectOriginatorNode->addAttribute("skillClass", + intToStr + (skillType->getClass + ()), + mapTagReplacements); + } + // std::vector currentAttackBoostUnits; + for (unsigned int i = 0; i < currentAttackBoostUnits.size(); ++i) { + XmlNode *currentAttackBoostUnitsNode = + unitAttackBoostEffectOriginatorNode->addChild + ("currentAttackBoostUnits"); + currentAttackBoostUnitsNode->addAttribute("value", + intToStr + (currentAttackBoostUnits + [i]), mapTagReplacements); + } + // UnitAttackBoostEffect *currentAppliedEffect; + if (currentAppliedEffect != NULL) { + currentAppliedEffect->saveGame(unitAttackBoostEffectOriginatorNode); + } + } + + // ===================================================== + // class Unit + // ===================================================== + + const float Unit::ANIMATION_SPEED_MULTIPLIER = 100000.f; + //const float Unit::PROGRESS_SPEED_MULTIPLIER = 100000.f; + const int64 Unit::PROGRESS_SPEED_MULTIPLIER = 100000; + + const int Unit::speedDivider = 100; + const int Unit::maxDeadCount = 1000; //time in until the corpse disapears - should be about 40 seconds + const int Unit::invalidId = -1; + + //set Unit::livingUnits; + //set Unit::livingUnitsp; + + // ============================ Constructor & destructor ============================= + + Game *Unit::game = NULL; + + Unit::Unit(int id, UnitPathInterface * unitpath, const Vec2i & pos, + const UnitType * type, Faction * faction, Map * map, + CardinalDir placeFacing) :BaseColorPickEntity(), id(id) { +#ifdef LEAK_CHECK_UNITS + Unit::mapMemoryList[this] = true; #endif - result->lastAnimProgress = - unitNode->getAttribute ("lastAnimProgress")->getFloatValue (); - } -// float animProgress; //between 0 and 1 - try - { - result->animProgress = - unitNode->getAttribute ("animProgress")->getIntValue (); - } -#ifdef WIN32 - catch (const exception &) - { -#else - catch (const exception & ex) - { + mutexCommands = new Mutex(CODE_AT_LINE); + changedActiveCommand = false; + lastChangedActiveCommandFrame = 0; + changedActiveCommandFrame = 0; + + lastSynchDataString = ""; + modelFacing = CardinalDir(CardinalDir::NORTH); + lastStuckFrame = 0; + lastStuckPos = Vec2i(0, 0); + lastPathfindFailedFrame = 0; + lastPathfindFailedPos = Vec2i(0, 0); + usePathfinderExtendedMaxNodes = false; + this->currentAttackBoostOriginatorEffect.skillType = NULL; + lastAttackerUnitId = -1; + lastAttackedUnitId = -1; + causeOfDeath = ucodNone; + pathfindFailedConsecutiveFrameCount = 0; + + lastSynchDataString = ""; + lastFile = ""; + lastLine = 0; + lastSource = ""; + + targetRotationZ = .0f; + targetRotationX = .0f; + rotationZ = .0f; + rotationX = .0f; + + this->fire = NULL; + this->unitPath = unitpath; + this->unitPath->setMap(map); + + //RandomGen random; + random.init(id); + random.setDisableLastCallerTracking(isNetworkCRCEnabled() == false); + pathFindRefreshCellCount = + random.randRange(10, 20, intToStr(__LINE__)); + + if (map->isInside(pos) == false + || map->isInsideSurface(map->toSurfCoords(pos)) == false) { + throw megaglest_runtime_error("#2 Invalid path position = " + + pos.getString()); + } + + this->pos = pos; + + this->faction = faction; + this->preMorph_type = NULL; + this->type = type; + setType(this->type); + + this->map = map; + this->targetRef = NULL; + this->targetField = fLand; + this->targetVec = Vec3f(0.0); + this->targetPos = Vec2i(0); + this->lastRenderFrame = 0; + this->visible = true; + this->retryCurrCommandCount = 0; + this->screenPos = Vec3f(0.0); + this->ignoreCheckCommand = false; + this->inBailOutAttempt = false; + this->lastHarvestResourceTarget.first = Vec2i(0); + this->morphFieldsBlocked = false; + //this->lastBadHarvestListPurge = 0; + this->oldTotalSight = 0; + + level = NULL; + loadType = NULL; + + setModelFacing(placeFacing); + + Config & config = Config::getInstance(); + showUnitParticles = config.getBool("UnitParticles", "true"); + maxQueuedCommandDisplayCount = + config.getInt("MaxQueuedCommandDisplayCount", "15"); + + if (GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + showUnitParticles = false; + } + + lastPos = pos; + progress = 0; + this->lastAnimProgress = 0; + this->animProgress = 0; + progress2 = 0; + kills = 0; + enemyKills = 0; + loadCount = 0; + ep = 0; + deadCount = 0; + hp = type->getMaxHp() / 20; + toBeUndertaken = false; + + highlight = 0.f; + meetingPos = pos; + setAlive(true); + + if (type->hasSkillClass(scBeBuilt) == false) { + float rot = 0.f; + random.init(id); + rot += random.randRange(-5, 5, intToStr(__LINE__)); + rotation = rot; + lastRotation = rot; + targetRotation = rot; + } + // else it was set appropriately in setModelFacing() + + if (getType()->getField(fAir)) { + currField = fAir; + } + if (getType()->getField(fLand)) { + currField = fLand; + } + + computeTotalUpgrade(); + + //starting skill + this->lastModelIndexForCurrSkillType = -1; + this->animationRandomCycleCount = 0; + this->currSkill = getType()->getFirstStOfClass(scStop); + this->setCurrSkill(this->currSkill); + this->currentAttackBoostOriginatorEffect.skillType = this->currSkill; + + this->faction->addLivingUnits(id); + this->faction->addLivingUnitsp(this); + + addItemToVault(&this->hp, this->hp); + addItemToVault(&this->ep, this->ep); + + calculateFogOfWarRadius(); + + // if(isUnitDeleted(this) == true) { + // MutexSafeWrapper safeMutex(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); + // deletedUnits.erase(this); + // } + + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + } + + Unit::~Unit() { + badHarvestPosList.clear(); + + this->faction->deleteLivingUnits(id); + this->faction->deleteLivingUnitsp(this); + + //remove commands + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + changedActiveCommand = false; + while (commands.empty() == false) { + delete commands.back(); + commands.pop_back(); + } + safeMutex.ReleaseLock(); + + cleanupAllParticlesystems(); + + while (currentAttackBoostEffects.empty() == false) { + //UnitAttackBoostEffect &effect = currentAttackBoostEffects.back(); + UnitAttackBoostEffect *ab = currentAttackBoostEffects.back(); + delete ab; + currentAttackBoostEffects.pop_back(); + } + + delete currentAttackBoostOriginatorEffect.currentAppliedEffect; + currentAttackBoostOriginatorEffect.currentAppliedEffect = NULL; + + //Renderer::getInstance().cleanupParticleSystems(attackParticleSystems,rsGame); + Renderer::getInstance().removeParticleSystemsForParticleOwner(this, + rsGame); + + +#ifdef LEAK_CHECK_UNITS + Unit::mapMemoryList2[this->unitPath] = this->getId(); #endif - result->animProgress = - unitNode->getAttribute ("animProgress")->getFloatValue (); - } - -// float highlight; - result->highlight = - unitNode->getAttribute ("highlight")->getFloatValue (); -// int progress2; - result->progress2 = - unitNode->getAttribute ("progress2")->getIntValue (); -// int kills; - result->kills = unitNode->getAttribute ("kills")->getIntValue (); -// int enemyKills; - result->enemyKills = - unitNode->getAttribute ("enemyKills")->getIntValue (); -// UnitReference targetRef; -// targetRef.saveGame(unitNode); - result->targetRef.loadGame (unitNode, world); -// -// Field currField; - result->currField = - static_cast < Field > - (unitNode->getAttribute ("currField")->getIntValue ()); -// Field targetField; - result->targetField = - static_cast < Field > - (unitNode->getAttribute ("targetField")->getIntValue ()); -// const Level *level; -// if(level != NULL) { -// level->saveGame(unitNode); -// } - result->level = Level::loadGame (unitNode, ut); -// Vec2i pos; - result->pos = - Vec2i::strToVec2 (unitNode->getAttribute ("pos")->getValue ()); -// Vec2i lastPos; - result->lastPos = - Vec2i::strToVec2 (unitNode->getAttribute ("lastPos")->getValue ()); -// Vec2i targetPos; //absolute target pos - result->targetPos = - Vec2i::strToVec2 (unitNode->getAttribute ("targetPos")->getValue ()); -// Vec3f targetVec; - result->targetVec = - Vec3f::strToVec3 (unitNode->getAttribute ("targetVec")->getValue ()); -// Vec2i meetingPos; - result->meetingPos = - Vec2i::strToVec2 (unitNode->getAttribute ("meetingPos")->getValue ()); -// -// float lastRotation; //in degrees - result->lastRotation = - unitNode->getAttribute ("lastRotation")->getFloatValue (); -// float targetRotation; - result->targetRotation = - unitNode->getAttribute ("targetRotation")->getFloatValue (); -// float rotation; - result->rotation = - unitNode->getAttribute ("rotation")->getFloatValue (); -// float targetRotationZ; - result->targetRotationZ = - unitNode->getAttribute ("targetRotationZ")->getFloatValue (); -// float targetRotationX; - result->targetRotationX = - unitNode->getAttribute ("targetRotationX")->getFloatValue (); -// float rotationZ; - result->rotationZ = - unitNode->getAttribute ("rotationZ")->getFloatValue (); -// float rotationX; - result->rotationX = - unitNode->getAttribute ("rotationX")->getFloatValue (); -// const UnitType *type; -// unitNode->addAttribute("type",type->getName(), mapTagReplacements); -// const ResourceType *loadType; -// if(loadType != NULL) { -// unitNode->addAttribute("loadType",loadType->getName(), mapTagReplacements); -// } - if (unitNode->hasAttribute ("loadType") == true) - { - string loadTypeName = - unitNode->getAttribute ("loadType")->getValue (); - result->loadType = - world->getTechTree ()->getResourceType (loadTypeName); - } -// const SkillType *currSkill; -// if(currSkill != NULL) { -// unitNode->addAttribute("currSkill",currSkill->getName(), mapTagReplacements); -// } - if (unitNode->hasAttribute ("currSkillName") == true) - { - string skillTypeName = - unitNode->getAttribute ("currSkillName")->getValue (); - SkillClass skillClass = - static_cast < SkillClass > - (unitNode->getAttribute ("currSkillClass")->getIntValue ()); - result->currSkill = ut->getSkillType (skillTypeName, skillClass); - result->setCurrSkill (result->currSkill); - } - -// int lastModelIndexForCurrSkillType; - result->lastModelIndexForCurrSkillType = - unitNode-> - getAttribute ("lastModelIndexForCurrSkillType")->getIntValue (); -// int animationRandomCycleCount; - result->animationRandomCycleCount = - unitNode->getAttribute ("animationRandomCycleCount")->getIntValue (); -// -// bool toBeUndertaken; - result->toBeUndertaken = - unitNode->getAttribute ("toBeUndertaken")->getIntValue () != 0; -// bool alive; - result->setAlive (unitNode->getAttribute ("alive")->getIntValue () != - 0); -// bool showUnitParticles; - result->showUnitParticles = - unitNode->getAttribute ("showUnitParticles")->getIntValue () != 0; -// Faction *faction; -// ParticleSystem *fire; -// if(fire != NULL) { -// fire->saveGame(unitNode); -// } - if (unitNode->hasChild ("FireParticleSystem") == true) - { - XmlNode *fireNode = unitNode->getChild ("FireParticleSystem"); - result->fire = new FireParticleSystem (); - result->fire->setParticleOwner (result); - result->fire->loadGame (fireNode); - //result->fire->setTexture(CoreData::getInstance().getFireTexture()); - result->fireParticleSystems.push_back (result->fire); - - //printf("Load MAIN fire particle result->fire = %p\n",result->fire); - - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (result->fire, - rsGame)); - } - -// TotalUpgrade totalUpgrade; - result->totalUpgrade.loadGame (unitNode); -// Map *map; -// -// UnitPathInterface *unitPath; -// unitPath->saveGame(unitNode); -// WaypointPath waypointPath; -// -// Commands commands; -// for(Commands::iterator it = commands.begin(); it != commands.end(); ++it) { -// (*it)->saveGame(unitNode); -// } - vector < XmlNode * >commandNodeList = - unitNode->getChildList ("Command"); - for (unsigned int i = 0; i < commandNodeList.size (); ++i) - { - XmlNode *node = commandNodeList[i]; - Command *command = Command::loadGame (node, ut, world); - - static string mutexOwnerId = - string (__FILE__) + string ("_") + intToStr (__LINE__); - MutexSafeWrapper safeMutex (result->mutexCommands, mutexOwnerId); - result->commands.push_back (command); - safeMutex.ReleaseLock (); - } -// Observers observers; - //for(Observers::iterator it = observers.begin(); it != observers.end(); ++it) { - // (*it)->saveGame(unitNode); - //} - -// vector unitParticleSystems; -// for(unsigned int i = 0; i < unitParticleSystems.size(); ++i) { -// UnitParticleSystem *ups= unitParticleSystems[i]; -// ups->saveGame(unitNode); -// } - if (unitNode->hasChild ("unitParticleSystems") == true) - { - XmlNode *unitParticleSystemsNode = - unitNode->getChild ("unitParticleSystems"); - vector < XmlNode * >unitParticleSystemNodeList = - unitParticleSystemsNode->getChildList ("UnitParticleSystem"); - for (unsigned int i = 0; i < unitParticleSystemNodeList.size (); ++i) - { - XmlNode *node = unitParticleSystemNodeList[i]; - - UnitParticleSystem *ups = new UnitParticleSystem (); - ups->setParticleOwner (result); - ups->loadGame (node); - result->unitParticleSystems.push_back (ups); - - //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (ups, rsGame)); - } - } - -// vector queuedUnitParticleSystemTypes; -// for(unsigned int i = 0; i < queuedUnitParticleSystemTypes.size(); ++i) { -// UnitParticleSystemType *upst= queuedUnitParticleSystemTypes[i]; -// upst->saveGame(unitNode); -// } - -// UnitParticleSystems damageParticleSystems; -// for(unsigned int i = 0; i < damageParticleSystems.size(); ++i) { -// UnitParticleSystem *ups= damageParticleSystems[i]; -// ups->saveGame(unitNode); -// } - if (unitNode->hasChild ("damageParticleSystems") == true) - { - XmlNode *damageParticleSystemsNode = - unitNode->getChild ("damageParticleSystems"); - vector < XmlNode * >unitParticleSystemNodeList = - damageParticleSystemsNode->getChildList ("UnitParticleSystem"); - for (unsigned int i = 0; i < unitParticleSystemNodeList.size (); ++i) - { - XmlNode *node = unitParticleSystemNodeList[i]; - - UnitParticleSystem *ups = new UnitParticleSystem (); - ups->setParticleOwner (result); - ups->loadGame (node); - result->damageParticleSystems.push_back (ups); - result->damageParticleSystemsInUse[i] = ups; - - //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (ups, rsGame)); - } - } - -// std::map damageParticleSystemsInUse; -// for(std::map::const_iterator iterMap = damageParticleSystemsInUse.begin(); -// iterMap != damageParticleSystemsInUse.end(); ++iterMap) { -// XmlNode *damageParticleSystemsInUseNode = unitNode->addChild("damageParticleSystemsInUse"); -// -// damageParticleSystemsInUseNode->addAttribute("key",intToStr(iterMap->first), mapTagReplacements); -// iterMap->second->saveGame(damageParticleSystemsInUseNode); -// } -// if(unitNode->hasChild("damageParticleSystemsInUse") == true) { -// XmlNode *damageParticleSystemsInUseNode = unitNode->getChild("damageParticleSystemsInUse"); -// vector damageParticleSystemsInUseNode2 = damageParticleSystemsInUseNode->getChildList("damageParticleSystemsInUse"); -// for(unsigned int i = 0; i < damageParticleSystemsInUseNode2.size(); ++i) { -// XmlNode *d2Node = damageParticleSystemsInUseNode2[i]; -// -// vector unitParticleSystemNodeList = damageParticleSystemsInUseNode->getChildList("UnitParticleSystem"); -// for(unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { -// XmlNode *node = unitParticleSystemNodeList[i]; -// -// UnitParticleSystem *ups = new UnitParticleSystem(); -// ups->loadGame(node); -// result->unitParticleSystems.push_back(ups); -// -// //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); -// Renderer::getInstance().addToDeferredParticleSystemList(make_pair(ups, rsGame)); -// } -// -// } - -// vector fireParticleSystems; -// for(unsigned int i = 0; i < fireParticleSystems.size(); ++i) { -// ParticleSystem *ps= fireParticleSystems[i]; -// ps->saveGame(unitNode); -// } - if (unitNode->hasChild ("fireParticleSystems") == true) - { - XmlNode *fireParticleSystemsNode = - unitNode->getChild ("fireParticleSystems"); - - int linkFireIndex = -1; - if (fireParticleSystemsNode->hasAttribute ("fireParticleLink") == - true) - { - linkFireIndex = - fireParticleSystemsNode-> - getAttribute ("fireParticleLink")->getIntValue (); - } - - vector < XmlNode * >unitParticleSystemNodeList = - fireParticleSystemsNode->getChildList ("FireParticleSystem"); - for (int i = 0; i < (int) unitParticleSystemNodeList.size (); ++i) - { - XmlNode *node = unitParticleSystemNodeList[i]; - - if (result->fire == NULL || linkFireIndex != i) - { - FireParticleSystem *ups = new FireParticleSystem (); - ups->setParticleOwner (result); - ups->loadGame (node); - //ups->setTexture(CoreData::getInstance().getFireTexture()); - result->fireParticleSystems.push_back (ups); - - //printf("Load fire particle i = %d linkFireIndex = %d result->fire = %p ups = %p\n",i,linkFireIndex,result->fire,ups); - - if (result->fire == NULL && linkFireIndex >= 0 - && linkFireIndex == i) - { - result->fire = ups; - } - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (ups, rsGame)); - } - } - } - -// vector smokeParticleSystems; -// for(unsigned int i = 0; i < smokeParticleSystems.size(); ++i) { -// UnitParticleSystem *ups= smokeParticleSystems[i]; -// ups->saveGame(unitNode); -// } - if (unitNode->hasChild ("smokeParticleSystems") == true) - { - XmlNode *smokeParticleSystemsNode = - unitNode->getChild ("smokeParticleSystems"); - vector < XmlNode * >unitParticleSystemNodeList = - smokeParticleSystemsNode->getChildList ("UnitParticleSystem"); - for (unsigned int i = 0; i < unitParticleSystemNodeList.size (); ++i) - { - XmlNode *node = unitParticleSystemNodeList[i]; - -// printf("Load Smoke particle i = %d\n",i); - UnitParticleSystem *ups = new UnitParticleSystem (); - ups->setParticleOwner (result); - ups->loadGame (node); - //ups->setTexture(CoreData::getInstance().getFireTexture()); - result->smokeParticleSystems.push_back (ups); - - Renderer:: - getInstance ().addToDeferredParticleSystemList (make_pair - (ups, rsGame)); - - //printf("Loading smoke particles:\n[%s]\n",ups->toString().c_str()); - } - } - -// CardinalDir modelFacing; -// unitNode->addAttribute("modelFacing",intToStr(modelFacing), mapTagReplacements); - -// std::string lastSynchDataString; -// unitNode->addAttribute("lastSynchDataString",lastSynchDataString, mapTagReplacements); -// std::string lastFile; -// unitNode->addAttribute("lastFile",lastFile, mapTagReplacements); -// int lastLine; -// unitNode->addAttribute("lastLine",intToStr(lastLine), mapTagReplacements); -// std::string lastSource; -// unitNode->addAttribute("lastSource",lastSource, mapTagReplacements); -// int lastRenderFrame; - result->lastRenderFrame = - unitNode->getAttribute ("lastRenderFrame")->getIntValue (); -// bool visible; - result->visible = - unitNode->getAttribute ("visible")->getIntValue () != 0; -// int retryCurrCommandCount; - result->retryCurrCommandCount = - unitNode->getAttribute ("retryCurrCommandCount")->getIntValue (); -// Vec3f screenPos; - result->screenPos = - Vec3f::strToVec3 (unitNode->getAttribute ("screenPos")->getValue ()); -// string currentUnitTitle; - result->currentUnitTitle = - unitNode->getAttribute ("currentUnitTitle")->getValue (); -// -// bool inBailOutAttempt; - result->inBailOutAttempt = - unitNode->getAttribute ("inBailOutAttempt")->getIntValue () != 0; -// //std::vector > badHarvestPosList; -// std::map badHarvestPosList; -// for(std::map::const_iterator iterMap = badHarvestPosList.begin(); -// iterMap != badHarvestPosList.end(); ++iterMap) { -// XmlNode *badHarvestPosListNode = unitNode->addChild("badHarvestPosList"); -// -// badHarvestPosListNode->addAttribute("key",iterMap->first.getString(), mapTagReplacements); -// badHarvestPosListNode->addAttribute("value",intToStr(iterMap->second), mapTagReplacements); -// } - -// //time_t lastBadHarvestListPurge; -// std::pair lastHarvestResourceTarget; - const XmlNode *lastHarvestResourceTargetNode = - unitNode->getChild ("lastHarvestResourceTarget"); -// lastHarvestResourceTargetNode->addAttribute("key",lastHarvestResourceTarget.first.getString(), mapTagReplacements); -// lastHarvestResourceTargetNode->addAttribute("value",intToStr(lastHarvestResourceTarget.second), mapTagReplacements); - - result->lastHarvestResourceTarget = - make_pair (Vec2i::strToVec2 - (lastHarvestResourceTargetNode->getAttribute ("key")-> - getValue ()), - lastHarvestResourceTargetNode-> - getAttribute ("value")->getIntValue ()); - -// //std::pair > currentTargetPathTaken; -// static Game *game; -// -// bool ignoreCheckCommand; - result->ignoreCheckCommand = - unitNode->getAttribute ("ignoreCheckCommand")->getIntValue () != 0; -// uint32 lastStuckFrame; - result->lastStuckFrame = - unitNode->getAttribute ("lastStuckFrame")->getIntValue (); -// Vec2i lastStuckPos; - result->lastStuckPos = - Vec2i::strToVec2 (unitNode-> - getAttribute ("lastStuckPos")->getValue ()); -// uint32 lastPathfindFailedFrame; - result->lastPathfindFailedFrame = - unitNode->getAttribute ("lastPathfindFailedFrame")->getIntValue (); -// Vec2i lastPathfindFailedPos; - result->lastPathfindFailedPos = - Vec2i::strToVec2 (unitNode-> - getAttribute ("lastPathfindFailedPos")->getValue - ()); -// bool usePathfinderExtendedMaxNodes; - result->usePathfinderExtendedMaxNodes = - unitNode-> - getAttribute ("usePathfinderExtendedMaxNodes")->getIntValue () != 0; -// int maxQueuedCommandDisplayCount; - result->maxQueuedCommandDisplayCount = - unitNode-> - getAttribute ("maxQueuedCommandDisplayCount")->getIntValue (); -// UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; - - // !!! TODO: Softcoder - in progress work to load attack boosts, not working properly yet - result->currentAttackBoostOriginatorEffect.loadGame (unitNode, result, - world); - -// std::vector currentAttackBoostEffects; -// for(unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { -// UnitAttackBoostEffect *uabe= currentAttackBoostEffects[i]; -// uabe->saveGame(unitNode); -// } - - // !!! TODO: Softcoder - in progress work to load attack boosts, not working properly yet - if (unitNode->hasChild ("UnitAttackBoostEffect") == true) - { - vector < XmlNode * >unitParticleSystemNodeList = - unitNode->getChildList ("UnitAttackBoostEffect"); - for (unsigned int i = 0; i < unitParticleSystemNodeList.size (); ++i) - { - XmlNode *node = unitParticleSystemNodeList[i]; - - UnitAttackBoostEffect *attackBoostEffect = - new UnitAttackBoostEffect (); - attackBoostEffect->loadGame (node, result, world, false); - - result->currentAttackBoostEffects.push_back (attackBoostEffect); - } - } - //printf("Unit [%d - %s] has currentAttackBoostEffects count: %d\n",result->getId(),result->getType()->getName(false).c_str(),(int)result->currentAttackBoostEffects.size()); - - -// Mutex *mutexCommands; -// -// //static Mutex mutexDeletedUnits; -// //static std::map deletedUnits; -// -// bool changedActiveCommand; - result->changedActiveCommand = - unitNode->getAttribute ("changedActiveCommand")->getIntValue () != 0; -// int lastAttackerUnitId; - result->lastAttackerUnitId = - unitNode->getAttribute ("lastAttackerUnitId")->getIntValue (); -// int lastAttackedUnitId; - result->lastAttackedUnitId = - unitNode->getAttribute ("lastAttackedUnitId")->getIntValue (); -// CauseOfDeathType causeOfDeath; - result->causeOfDeath = - static_cast < CauseOfDeathType > - (unitNode->getAttribute ("causeOfDeath")->getIntValue ()); - - result->pathfindFailedConsecutiveFrameCount = - unitNode-> - getAttribute ("pathfindFailedConsecutiveFrameCount")->getIntValue (); - - if (result->alive) - { - world->getMapPtr ()->putUnitCells (result, newUnitPos); - //result->born(); - } - - result->pos = newUnitPos; - result->lastPos = - Vec2i::strToVec2 (unitNode->getAttribute ("lastPos")->getValue ()); - result->meetingPos = - Vec2i::strToVec2 (unitNode->getAttribute ("meetingPos")->getValue ()); - - if (unitNode->hasAttribute ("currentPathFinderDesiredFinalPos")) - { - result->currentPathFinderDesiredFinalPos = - Vec2i::strToVec2 (unitNode->getAttribute - ("currentPathFinderDesiredFinalPos")->getValue - ()); - } - - if (unitNode->hasAttribute ("random")) - { - result->random.setLastNumber (unitNode-> - getAttribute ("random")->getIntValue - ()); - } - if (unitNode->hasAttribute ("pathFindRefreshCellCount")) - { - result->pathFindRefreshCellCount = - unitNode->getAttribute ("pathFindRefreshCellCount")->getIntValue (); - } - - //result->exploreCells(); - //result->calculateFogOfWarRadius(); - - return result; - } - - Checksum Unit::getCRC () - { - const bool consoleDebug = false; - - Checksum crcForUnit; - - crcForUnit.addInt (id); - crcForUnit.addInt (hp); - crcForUnit.addInt (ep); - crcForUnit.addInt (loadCount); - crcForUnit.addInt (deadCount); - - if (consoleDebug) - printf ("#1 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - crcForUnit.addInt64 (progress); - crcForUnit.addInt64 (lastAnimProgress); - crcForUnit.addInt64 (animProgress); - - if (consoleDebug) - printf ("#2 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //float highlight; - crcForUnit.addInt (progress2); - crcForUnit.addInt (kills); - crcForUnit.addInt (enemyKills); - crcForUnit.addInt (morphFieldsBlocked); - - if (consoleDebug) - printf ("#3 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //UnitReference targetRef; - - crcForUnit.addInt (currField); - crcForUnit.addInt (targetField); - - if (consoleDebug) - printf ("#4 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //const Level *level; - if (level != NULL) - { - crcForUnit.addString (level->getName (false)); - } - - if (consoleDebug) - printf ("#5 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - crcForUnit.addInt (pos.x); - crcForUnit.addInt (pos.y); - crcForUnit.addInt (lastPos.x); - crcForUnit.addInt (lastPos.y); - crcForUnit.addInt (targetPos.x); - crcForUnit.addInt (targetPos.y); - - if (consoleDebug) - printf ("#6 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //Vec3f targetVec; - - crcForUnit.addInt (meetingPos.x); - crcForUnit.addInt (meetingPos.y); - - if (consoleDebug) - printf ("#7 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //float lastRotation; - //float targetRotation; - //float rotation; - //float targetRotationZ; - //float targetRotationX; - //float rotationZ; - //float rotationX; - - //const UnitType *preMorph_type; - if (preMorph_type != NULL) - { - crcForUnit.addString (preMorph_type->getName (false)); - } - - if (consoleDebug) - printf ("#8 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //const UnitType *type; - if (type != NULL) - { - crcForUnit.addString (type->getName (false)); - } - - //const ResourceType *loadType; - if (loadType != NULL) - { - crcForUnit.addString (loadType->getName (false)); - } - - //const SkillType *currSkill; - if (currSkill != NULL) - { - crcForUnit.addString (currSkill->getName ()); - } - - //printf("#9 Unit: %d CRC: %u lastModelIndexForCurrSkillType: %d\n",id,crcForUnit.getSum(),lastModelIndexForCurrSkillType); - //printf("#9a Unit: %d CRC: %u\n",id,crcForUnit.getSum()); - //crcForUnit.addInt(lastModelIndexForCurrSkillType); - if (consoleDebug) - printf ("#9 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); + delete this->unitPath; + this->unitPath = NULL; - //crcForUnit.addInt(animationRandomCycleCount); - //printf("#9b Unit: %d CRC: %u\n",id,crcForUnit.getSum()); + Renderer & renderer = Renderer::getInstance(); + renderer.removeUnitFromQuadCache(this); + if (game != NULL) { + game->removeUnitFromSelection(this); + } - crcForUnit.addInt (toBeUndertaken); + //MutexSafeWrapper safeMutex1(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); + //deletedUnits[this]=true; - if (consoleDebug) - printf ("#9c Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - crcForUnit.addInt (alive); - //bool showUnitParticles; + delete mutexCommands; + mutexCommands = NULL; - if (consoleDebug) - printf ("#10 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); +#ifdef LEAK_CHECK_UNITS + Unit::mapMemoryList.erase(this); +#endif + } - //Faction *faction; - //ParticleSystem *fire; - if (fire != NULL) - { - crcForUnit.addInt (fire->getActive ()); - } - - //TotalUpgrade totalUpgrade; - uint32 crc = totalUpgrade.getCRC ().getSum (); - crcForUnit.addBytes (&crc, sizeof (uint32)); + void Unit::cleanupAllParticlesystems() { - //Map *map; - //UnitPathInterface *unitPath; - if (unitPath != NULL) - { - uint32 crc = unitPath->getCRC ().getSum (); - crcForUnit.addBytes (&crc, sizeof (uint32)); - } - //WaypointPath waypointPath; + Renderer:: + getInstance().cleanupUnitParticleSystems(unitParticleSystems, + rsGame); - if (consoleDebug) - printf ("#11 Unit: %d CRC: %u commands.size(): " MG_SIZE_T_SPECIFIER - "\n", id, crcForUnit.getSum (), commands.size ()); + Renderer::getInstance().cleanupParticleSystems(fireParticleSystems, + rsGame); + // Must set this to null of it will be used below in stopDamageParticles() - //Commands commands; - if (commands.empty () == false) - { - crcForUnit.addInt ((int) commands.size ()); - for (Commands::const_iterator it = commands.begin (); - it != commands.end (); ++it) - { - uint32 crc = (*it)->getCRC ().getSum (); - crcForUnit.addBytes (&crc, sizeof (uint32)); - } - } + if (Renderer:: + getInstance().validateParticleSystemStillExists(this->fire, + rsGame) == false) { + this->fire = NULL; + } - //printf("#11 Unit: %d CRC: %u observers.size(): %ld\n",id,crcForUnit.getSum(),observers.size()); + // fade(and by this remove) all unit particle systems + queuedUnitParticleSystemTypes.clear(); + while (unitParticleSystems.empty() == false) { + if (Renderer:: + getInstance().validateParticleSystemStillExists + (unitParticleSystems.back(), rsGame) == true) { + unitParticleSystems.back()->fade(); + } + unitParticleSystems.pop_back(); + } + stopDamageParticles(true); - //Observers observers; - //crcForUnit.addInt64((int64)observers.size()); + } - if (consoleDebug) - printf ("#11 Unit: %d CRC: %u damageParticleSystems.size(): " - MG_SIZE_T_SPECIFIER "\n", id, crcForUnit.getSum (), - damageParticleSystems.size ()); + ParticleSystem *Unit::getFire() const { + if (this->fire != NULL && + Renderer::getInstance(). + validateParticleSystemStillExists(this->fire, rsGame) == false) { + return NULL; + } + return this->fire; + } - //vector unitParticleSystems; - //vector queuedUnitParticleSystemTypes; + void Unit::setType(const UnitType * newType) { + this->faction->notifyUnitTypeChange(this, newType); + this->type = newType; + } - //UnitParticleSystems damageParticleSystems; - crcForUnit.addInt ((int) damageParticleSystems.size ()); + void Unit::setAlive(bool value) { + this->alive = value; + this->faction->notifyUnitAliveStatusChange(this); + } - if (consoleDebug) - printf ("#12 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); +#ifdef LEAK_CHECK_UNITS + void Unit::dumpMemoryList() { + printf("===== START report of Unfreed Unit pointers =====\n"); + for (std::map < Unit *, bool >::iterator iterMap = + Unit::mapMemoryList.begin(); + iterMap != Unit::mapMemoryList.end(); ++iterMap) { + printf("************** ==> Unfreed Unit pointer [%p]\n", + iterMap->first); + } + } +#endif - //std::map damageParticleSystemsInUse; + bool Unit::isNetworkCRCEnabled() { + bool isNetworkCRCEnabled = false; - //vector fireParticleSystems; - //vector smokeParticleSystems; - - //CardinalDir modelFacing; - crcForUnit.addInt (modelFacing); - - //std::string lastSynchDataString; - //std::string lastFile; - //int lastLine; - //std::string lastSource; - //int lastRenderFrame; - //bool visible; - - //int retryCurrCommandCount; - - //Vec3f screenPos; - //string currentUnitTitle; - - if (consoleDebug) - printf ("#13 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); + if (game != NULL) { + if (game->isFlagType1BitEnabled(ft1_network_synch_checks_verbose) == + true) { + isNetworkCRCEnabled = true; + } else if (game->isFlagType1BitEnabled(ft1_network_synch_checks) == + true) { + isNetworkCRCEnabled = true; + } + } + return isNetworkCRCEnabled; + } - crcForUnit.addInt (inBailOutAttempt); - - crcForUnit.addInt ((int) badHarvestPosList.size ()); - //crcForUnit.addInt(lastHarvestResourceTarget.first()); + void Unit::clearNetworkCRCDecHpList() { + if (networkCRCDecHpList.empty() == false) { + networkCRCDecHpList.clear(); + } + } + void Unit::clearParticleInfo() { + if (networkCRCParticleInfoList.empty() == false) { + networkCRCParticleInfoList.clear(); + } + } - if (consoleDebug) - printf ("#14 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); + void Unit::addNetworkCRCDecHp(string info) { + if (isNetworkCRCEnabled() == true) { + networkCRCDecHpList.push_back(info); + } + } - //static Game *game; - //bool ignoreCheckCommand; - - //uint32 lastStuckFrame; - crcForUnit.addUInt (lastStuckFrame); - //Vec2i lastStuckPos; - crcForUnit.addInt (lastStuckPos.x); - crcForUnit.addInt (lastStuckPos.y); - - //uint32 lastPathfindFailedFrame; - //Vec2i lastPathfindFailedPos; - //bool usePathfinderExtendedMaxNodes; - //int maxQueuedCommandDisplayCount; - - //UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; - crcForUnit. - addInt ((int) - currentAttackBoostOriginatorEffect.currentAttackBoostUnits. - size ()); - - if (consoleDebug) - printf ("#15 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); - - //std::vector currentAttackBoostEffects; - - //Mutex *mutexCommands; - - //bool changedActiveCommand; - - //int lastAttackerUnitId; - //int lastAttackedUnitId; - //CauseOfDeathType causeOfDeath; + void Unit::logParticleInfo(string info) { + if (isNetworkCRCEnabled() == true) { + networkCRCParticleInfoList.push_back(info); + } + } + string Unit::getParticleInfo() const { + string result = ""; + if (networkCRCParticleInfoList.empty() == false) { + for (unsigned int index = 0; + index < networkCRCParticleInfoList.size(); ++index) { + result += networkCRCParticleInfoList[index] + "|"; + } + } + return result; + } - //uint32 pathfindFailedConsecutiveFrameCount; - crcForUnit.addString (this-> - currentPathFinderDesiredFinalPos.getString ()); + void Unit::end(ParticleSystem * particleSystem) { + if (particleSystem == fire) { + fire = NULL; + } - crcForUnit.addInt (random.getLastNumber ()); - if (this->random.getLastCaller () != "") - { - crcForUnit.addString (this->random.getLastCaller ()); - } + vector < ParticleSystem * >::iterator iterFind = + find(attackParticleSystems.begin(), attackParticleSystems.end(), + particleSystem); + if (iterFind != attackParticleSystems.end()) { + attackParticleSystems.erase(iterFind); + return; + } + vector < UnitParticleSystem * >::iterator iterFind1 = + find(smokeParticleSystems.begin(), smokeParticleSystems.end(), + particleSystem); + if (iterFind1 != smokeParticleSystems.end()) { + smokeParticleSystems.erase(iterFind1); + return; + } + iterFind = + find(fireParticleSystems.begin(), fireParticleSystems.end(), + particleSystem); + if (iterFind != fireParticleSystems.end()) { + fireParticleSystems.erase(iterFind); + return; + } + iterFind1 = + find(damageParticleSystems.begin(), damageParticleSystems.end(), + particleSystem); + if (iterFind1 != damageParticleSystems.end()) { + damageParticleSystems.erase(iterFind1); + return; + } - if (consoleDebug) - printf ("#16 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); + iterFind1 = + find(unitParticleSystems.begin(), unitParticleSystems.end(), + particleSystem); + if (iterFind1 != unitParticleSystems.end()) { + unitParticleSystems.erase(iterFind1); + return; + } + } - //int pathFindRefreshCellCount; + //bool Unit::isUnitDeleted(void *unit) { + // bool result = false; + // MutexSafeWrapper safeMutex(&mutexDeletedUnits,string(__FILE__) + "_" + intToStr(__LINE__)); + // if(deletedUnits.find(unit) != deletedUnits.end()) { + // result = true; + // } + // return result; + //} - //FowAlphaCellsLookupItem cachedFow; - //Vec2i cachedFowPos; + void Unit::setModelFacing(CardinalDir value) { + modelFacing = value; + lastRotation = targetRotation = rotation = value * 90.f; + } - crcForUnit.addInt (lastHarvestedResourcePos.x); - crcForUnit.addInt (lastHarvestedResourcePos.y); + //void Unit::setCurrField(Field currField) { + // Field original_field = this->currField; + // + // this->currField = currField; + // + // if(original_field != this->currField) { + // //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + // game->getScriptManager()->onUnitTriggerEvent(this,utet_FieldChanged); + // //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + // } + //} + // ====================================== get ====================================== - if (consoleDebug) - printf ("#17 Unit: %d CRC: %u\n", id, crcForUnit.getSum ()); + Vec2i Unit::getCenteredPos() const { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); - if (this->getParticleInfo () != "") - { - crcForUnit.addString (this->getParticleInfo ()); - } + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } - crcForUnit.addInt ((int) attackParticleSystems.size ()); - if (isNetworkCRCEnabled () == true) - { - for (unsigned int index = 0; index < attackParticleSystems.size (); - ++index) - { - ParticleSystem *ps = attackParticleSystems[index]; - if (ps != NULL && - Renderer::getInstance ().validateParticleSystemStillExists (ps, - rsGame) - == true) - { - uint32 crc = ps->getCRC ().getSum (); - crcForUnit.addBytes (&crc, sizeof (uint32)); - } - } - } + return pos + Vec2i(type->getSize() / 2, type->getSize() / 2); + } - if (this->networkCRCParticleLogInfo != "") - { - crcForUnit.addString (this->networkCRCParticleLogInfo); - } + Vec2f Unit::getFloatCenteredPos() const { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); - return crcForUnit; - } + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } - } -} //end namespace + return Vec2f(truncateDecimal < + float >(pos.x - 0.5f + type->getSize() / 2.f, 6), + truncateDecimal < + float >(pos.y - 0.5f + type->getSize() / 2.f, 6)); + } + + Vec2i Unit::getCellPos() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (type->hasCellMap()) { + if (type->hasEmptyCellMap() == false || + type->getAllowEmptyCellMap() == true) { + + //find nearest pos to center that is free + Vec2i centeredPos = getCenteredPos(); + float nearestDist = -1.f; + Vec2i nearestPos = pos; + + for (int i = 0; i < type->getSize(); ++i) { + for (int j = 0; j < type->getSize(); ++j) { + if (type->getCellMapCell(i, j, modelFacing)) { + Vec2i currPos = pos + Vec2i(i, j); + float dist = currPos.dist(centeredPos); + if (nearestDist == -1.f || dist < nearestDist) { + nearestDist = dist; + nearestPos = currPos; + } + } + } + } + return nearestPos; + } + } + return pos; + } + + + + void Unit::calculateXZRotation() { + //if(type->getProperty(UnitType::pRotatedClimb) && currSkill->getClass()==scMove){ + //if(currSkill->getClass()==scMove) + if (lastPos != pos) { // targetPosCalc ( maybe also sometimes needed if no move ? terrain flatting... ) + SurfaceCell *sc = map->getSurfaceCell(Map::toSurfCoords(pos)); + const Vec3f normal = sc->getNormal(); + +#ifdef USE_STREFLOP + targetRotationZ = + radToDeg(streflop::atan2 + (static_cast (abs(normal.x)), + static_cast (abs(normal.y)))); +#else + targetRotationZ = radToDeg(atan2(abs(normal.x), abs(normal.y))); +#endif + + if ((normal.y < 0 || normal.x < 0) && !(normal.y < 0 && normal.x < 0)) { + targetRotationZ = targetRotationZ * -1; + } + targetRotationZ = targetRotationZ * -1; + +#ifdef USE_STREFLOP + targetRotationX = + radToDeg(streflop::atan2 + (static_cast (abs(normal.z)), + static_cast (abs(normal.y)))); +#else + targetRotationX = radToDeg(atan2(abs(normal.z), abs(normal.y))); +#endif + + if ((normal.y < 0 || normal.z < 0) && !(normal.y < 0 && normal.z < 0)) { + targetRotationX = targetRotationX * -1; + } + } + + //For smooth rotation we now softly adjust the angle + int adjustStep = 1; + if (rotationZ < targetRotationZ) { + if (rotationZ + adjustStep > targetRotationZ) { + rotationZ = targetRotationZ; + } else { + rotationZ = rotationZ + adjustStep; + } + } else if (rotationZ > targetRotationZ) { + if (rotationZ - adjustStep < targetRotationZ) { + rotationZ = targetRotationZ; + } else { + rotationZ = rotationZ - adjustStep; + } + } + + if (rotationX < targetRotationX) { + if (rotationX + adjustStep > targetRotationX) { + rotationX = targetRotationX; + } else { + rotationX = rotationX + adjustStep; + } + } else if (rotationX > targetRotationX) { + if (rotationX - adjustStep < targetRotationX) { + rotationX = targetRotationX; + } else { + rotationX = rotationX - adjustStep; + } + } + } + + float Unit::getRotationZ() const { + return rotationZ; + } + + float Unit::getRotationX() const { + return rotationX; + } + + int Unit::getProductionPercent() const { + if (anyCommand()) { + const ProducibleType *produced = + commands.front()->getCommandType()->getProduced(); + if (produced != NULL) { + if (produced->getProductionTime() == 0) { + return 0; + } + return clamp(progress2 * 100 / produced->getProductionTime(), 0, + 100); + } + } + return -1; + } + + float Unit::getProgressRatio() const { + if (anyCommand()) { + const ProducibleType *produced = + commands.front()->getCommandType()->getProduced(); + if (produced != NULL) { + if (produced->getProductionTime() == 0) { + return 0.f; + } + + float help = progress2; + return clamp(help / produced->getProductionTime(), 0.f, 1.f); + } + } + return -1; + } + + float Unit::getHpRatio() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + float maxHpAllowed = type->getTotalMaxHp(&totalUpgrade); + if (maxHpAllowed == 0.f) { + return 0.f; + } + return clamp(static_cast (hp) / maxHpAllowed, 0.f, 1.f); + } + + float Unit::getEpRatio() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (type->getTotalMaxHp(&totalUpgrade) == 0) { + return 0.f; + } else { + float maxEpAllowed = type->getTotalMaxEp(&totalUpgrade); + if (maxEpAllowed == 0.f) { + return 0.f; + } + return clamp(static_cast (ep) / maxEpAllowed, 0.f, 1.f); + } + } + + const Level *Unit::getNextLevel() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (level == NULL && type->getLevelCount() > 0) { + return type->getLevel(0); + } else { + for (int i = 1; i < type->getLevelCount(); ++i) { + if (type->getLevel(i - 1) == level) { + return type->getLevel(i); + } + } + } + return NULL; + } + + string Unit::getFullName(bool translatedValue) const { + string str = ""; + if (level != NULL) { + str += (level->getName(translatedValue) + " "); + } + if (type == NULL) { + throw + megaglest_runtime_error("type == NULL in Unit::getFullName()!"); + } + str += type->getName(translatedValue); + return str; + } + + // ====================================== is ====================================== + + bool Unit::isOperative() const { + return isAlive() && isBuilt(); + } + + bool Unit::isAnimProgressBound() const { + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + bool result = false; + if (currSkill->getClass() == scBeBuilt) { + const BeBuiltSkillType *skill = + dynamic_cast (currSkill); + if (skill != NULL) { + result = skill->getAnimProgressBound(); + } + } else if (currSkill->getClass() == scProduce) { + const ProduceSkillType *skill = + dynamic_cast (currSkill); + if (skill != NULL) { + result = skill->getAnimProgressBound(); + } + } else if (currSkill->getClass() == scUpgrade) { + const UpgradeSkillType *skill = + dynamic_cast (currSkill); + if (skill != NULL) { + result = skill->getAnimProgressBound(); + } + } else if (currSkill->getClass() == scMorph) { + const MorphSkillType *skill = + dynamic_cast (currSkill); + if (skill != NULL) { + result = skill->getAnimProgressBound(); + } + } + return result; + } + + bool Unit::isBeingBuilt() const { + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + return (currSkill->getClass() == scBeBuilt); + } + + bool Unit::isBuilt() const { + return (isBeingBuilt() == false); + } + + bool Unit::isBuildCommandPending() const { + bool result = false; + + Command *command = this->getCurrCommand(); + if (command != NULL) { + const BuildCommandType *bct = + dynamic_cast < + const BuildCommandType *>(command->getCommandType()); + if (bct != NULL) { + if (this->getCurrSkill()->getClass() != scBuild) { + result = true; + } + } + } + + return result; + } + + UnitBuildInfo Unit::getBuildCommandPendingInfo() const { + UnitBuildInfo result; + + Command *command = this->getCurrCommand(); + if (command != NULL) { + const BuildCommandType *bct = + dynamic_cast < + const BuildCommandType *>(command->getCommandType()); + if (bct != NULL) { + result.pos = command->getOriginalPos(); + result.facing = command->getFacing(); + result.buildUnit = command->getUnitType(); + result.unit = this; + } + } + + return result; + } + + bool Unit::isAlly(const Unit * unit) const { + if (unit == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: unit == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + return faction->isAlly(unit->getFaction()); + } + + bool Unit::isDamaged() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + return hp < type->getTotalMaxHp(&totalUpgrade); + } + + bool Unit::isInteresting(InterestingUnitType iut) const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + switch (iut) { + case iutIdleHarvester: + if (type->hasCommandClass(ccHarvest)) { + if (commands.empty() == false) { + const CommandType *ct = commands.front()->getCommandType(); + if (ct != NULL) { + return ct->getClass() == ccStop; + } + } + } + return false; + + case iutBuiltBuilding: + return type->hasSkillClass(scBeBuilt) && isBuilt(); + case iutProducer: + return type->hasSkillClass(scProduce); + case iutDamaged: + return isDamaged(); + case iutStore: + return type->getStoredResourceCount() > 0; + default: + return false; + } + } + + // ====================================== set ====================================== + + void Unit::setCurrSkill(const SkillType * currSkill) { + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + if (this->currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: this->currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (this->currSkill->getClass() == scMove && + currSkill->getClass() != scMove) { + faction->removeUnitFromMovingList(this->getId()); + } else if (this->currSkill->getClass() != scMove && + currSkill->getClass() == scMove) { + faction->addUnitToMovingList(this->getId()); + } + + changedActiveCommand = false; + if (currSkill->getClass() != this->currSkill->getClass() || + currSkill->getName() != this->currSkill->getName()) { + this->animProgress = 0; + this->lastAnimProgress = 0; + + queuedUnitParticleSystemTypes.clear(); + while (unitParticleSystems.empty() == false) { + if (Renderer:: + getInstance().validateParticleSystemStillExists + (unitParticleSystems.back(), rsGame) == true) { + unitParticleSystems.back()->fade(); + } + unitParticleSystems.pop_back(); + } + + Command *cmd = getCurrrentCommandThreadSafe(); + + // Set mew fog of war skill type if need be + if (cmd != NULL && cmd->getCommandType() != NULL && + cmd->getCommandType()-> + hasFogOfWarSkillType(currSkill->getName())) { + const FogOfWarSkillType *fowst = + cmd->getCommandType()->getFogOfWarSkillType(); + + // Remove old fog of war skill type if need be + game->getWorld()->removeFogOfWarSkillTypeFromList(this); + + game->getWorld()->addFogOfWarSkillType(this, fowst); + } else { + // Remove old fog of war skill type if need be + game->getWorld()->removeFogOfWarSkillType(this); + } + } + if (showUnitParticles == true && + currSkill->unitParticleSystemTypes.empty() == false && + unitParticleSystems.empty() == true) { + //printf("START - particle system type\n"); + + /* + for(UnitParticleSystemTypes::const_iterator it= currSkill->unitParticleSystemTypes.begin(); + it != currSkill->unitParticleSystemTypes.end(); ++it) { + if((*it)->getStartTime() == 0.0) { + //printf("Adding NON-queued particle system type [%s] [%f] [%f]\n",(*it)->getType().c_str(),(*it)->getStartTime(),(*it)->getEndTime()); + + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType((*it)); + + (*it)->setValues(ups); + ups->setPos(getCurrVector()); + ups->setRotation(getRotation()); + ups->setUnitModel(getCurrentModelPtr()); + if(getFaction()->getTexture()) { + ups->setFactionColor(getFaction()->getTexture()->getPixmapConst()->getPixel3f(0,0)); + } + unitParticleSystems.push_back(ups); + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } + else { + //printf("Adding queued particle system type [%s] [%f] [%f]\n",(*it)->getType().c_str(),(*it)->getStartTime(),(*it)->getEndTime()); + + queuedUnitParticleSystemTypes.push_back(*it); + } + } + */ + checkCustomizedUnitParticleListTriggers + (currSkill->unitParticleSystemTypes, true); + } + + if (this->currSkill != currSkill) { + this->lastModelIndexForCurrSkillType = -1; + this->animationRandomCycleCount = 0; + } + + if (faction != NULL) + faction->notifyUnitSkillTypeChange(this, currSkill); + const SkillType *original_skill = this->currSkill; + this->currSkill = currSkill; + + if (original_skill != this->currSkill) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_SkillChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + } + + void Unit::setCurrSkill(SkillClass sc) { + if (getType() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: getType() == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + setCurrSkill(getType()->getFirstStOfClass(sc)); + } + + void Unit::setTarget(const Unit * unit) { + + if (unit == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: unit == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //find a free pos in cellmap + setTargetPos(unit->getCellPos()); + + //ser field and vector + targetField = unit->getCurrField(); + targetVec = unit->getCurrVectorAsTarget(); + targetRef = unit; + } + + RandomGen *Unit::getRandom(bool threadAccessAllowed) { + if (threadAccessAllowed == false + && Thread::isCurrentThreadMainThread() == false) { + throw + megaglest_runtime_error + ("Invalid access to unit random from outside main thread current id = " + + intToStr(Thread::getCurrentThreadId()) + " main = " + + intToStr(Thread::getMainThreadId())); + } + return &random; + } + + void Unit::setPos(const Vec2i & pos, bool clearPathFinder, bool threaded) { + if (map->isInside(pos) == false + || map->isInsideSurface(map->toSurfCoords(pos)) == false) { + throw megaglest_runtime_error("#3 Invalid path position = " + + pos.getString()); + } + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + if (threaded) { + logSynchDataThreaded(extractFileFromDirectoryPath(__FILE__).c_str + (), __LINE__); + } else { + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + } + + if (clearPathFinder == true && this->unitPath != NULL) { + this->unitPath->clear(); + } + //Vec2i oldLastPos = this->lastPos; + this->lastPos = this->pos; + this->pos = pos; + + map->clampPos(this->pos); + this->meetingPos = pos - Vec2i(1); + map->clampPos(this->meetingPos); + + safeMutex.ReleaseLock(); + + refreshPos(); + + if (threaded) { + logSynchDataThreaded(extractFileFromDirectoryPath(__FILE__).c_str + (), __LINE__); + } else { + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + } + } + + void Unit::refreshPos(bool forceRefresh) { + // Attempt to improve performance + this->exploreCells(forceRefresh); + calculateFogOfWarRadius(forceRefresh); + } + + FowAlphaCellsLookupItem Unit::getFogOfWarRadius(bool useCache) const { + if (useCache == true) { + return cachedFow; + } + + //iterate through all cells + int sightRange = + this->getType()->getTotalSight(this->getTotalUpgrade()); + int radius = sightRange + World::indirectSightRange; + PosCircularIterator pci(map, this->getPosNotThreadSafe(), radius); + FowAlphaCellsLookupItem result; + while (pci.next()) { + const Vec2i sightpos = pci.getPos(); + Vec2i surfPos = Map::toSurfCoords(sightpos); + + //compute max alpha + float maxAlpha = 0.0f; + if (surfPos.x > 1 && surfPos.y > 1 && + surfPos.x < map->getSurfaceW() - 2 && + surfPos.y < map->getSurfaceH() - 2) { + maxAlpha = 1.f; + } else if (surfPos.x > 0 && surfPos.y > 0 && + surfPos.x < map->getSurfaceW() - 1 && + surfPos.y < map->getSurfaceH() - 1) { + maxAlpha = 0.3f; + } + + //compute alpha + float alpha = maxAlpha; + float dist = this->getPosNotThreadSafe().dist(sightpos); + if (dist > sightRange) { + alpha = + clamp(1.f - (dist - sightRange) / (World::indirectSightRange), + 0.f, maxAlpha); + } + result.surfPosAlphaList[surfPos] = alpha; + } + return result; + } + + void Unit::calculateFogOfWarRadius(bool forceRefresh) { + if (game->getWorld()->getFogOfWar() == true) { + if (forceRefresh || this->pos != this->cachedFowPos) { + cachedFow = getFogOfWarRadius(false); + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + this->cachedFowPos = this->pos; + } + } + } + + void Unit::setTargetPos(const Vec2i & targetPos, bool threaded) { + + if (map->isInside(targetPos) == false + || map->isInsideSurface(map->toSurfCoords(targetPos)) == false) { + throw megaglest_runtime_error("#4 Invalid path position = " + + targetPos.getString()); + } + + Vec2i relPos = targetPos - pos; + //map->clampPos(relPos); + + Vec2f relPosf = Vec2f((float) relPos.x, (float) relPos.y); +#ifdef USE_STREFLOP + targetRotation = + radToDeg(streflop::atan2 + (static_cast (relPosf.x), + static_cast (relPosf.y))); +#else + targetRotation = radToDeg(atan2(relPosf.x, relPosf.y)); +#endif + targetRotation = truncateDecimal < float >(targetRotation, 6); + + targetRef = NULL; + + this->targetPos = targetPos; + map->clampPos(this->targetPos); + + if (threaded) { + logSynchDataThreaded(extractFileFromDirectoryPath(__FILE__).c_str + (), __LINE__); + } else { + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + } + } + + void Unit::addAttackParticleSystem(ParticleSystem * ps) { + attackParticleSystems.push_back(ps); + } + + void Unit::setVisible(const bool visible) { + this->visible = visible; + + if (unitParticleSystems.empty() == false) { + for (UnitParticleSystems::iterator it = unitParticleSystems.begin(); + it != unitParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == + true) { + (*it)->setVisible(visible); + } + } + } + if (damageParticleSystems.empty() == false) { + for (UnitParticleSystems::iterator it = + damageParticleSystems.begin(); + it != damageParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == + true) { + (*it)->setVisible(visible); + } + } + } + if (smokeParticleSystems.empty() == false) { + for (UnitParticleSystems::iterator it = smokeParticleSystems.begin(); + it != smokeParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == + true) { + if ((*it)->getVisible() != visible) { + //printf("Changing visibility for smoke particle system to: %d\n",visible); + (*it)->setVisible(visible); + } + } + } + } + + //if(attackParticleSystems.empty() == false) { + // for(vector::iterator it= attackParticleSystems.begin(); it != attackParticleSystems.end(); ++it) { + // if(Renderer::getInstance().validateParticleSystemStillExists((*it),rsGame) == true) { + // Not sure this is a good idea since the unit be not be visible but the attack particle might be. + // This means you won't see the attacking projectile until the unit moves into view. + //(*it)->setVisible(visible); + // } + // } + //} + + if (currentAttackBoostEffects.empty() == false) { + for (unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; + if (effect != NULL && effect->ups != NULL) { + bool particleValid = + Renderer:: + getInstance().validateParticleSystemStillExists(effect->ups, + rsGame); + if (particleValid == true) { + effect->ups->setVisible(visible); + } + } + } + } + if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) { + if (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups != + NULL) { + bool particleValid = + Renderer::getInstance().validateParticleSystemStillExists + (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups, + rsGame); + if (particleValid == true) { + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setVisible(visible); + } + } + } + } + + // =============================== Render related ================================== + + Model *Unit::getCurrentModelPtr() { + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + int currentModelIndexForCurrSkillType = lastModelIndexForCurrSkillType; + Model *result = + currSkill->getAnimation(getAnimProgressAsFloat(), this, + &lastModelIndexForCurrSkillType, + &animationRandomCycleCount); + if (currentModelIndexForCurrSkillType != lastModelIndexForCurrSkillType) { + animationRandomCycleCount++; + if (currSkill != NULL + && animationRandomCycleCount >= currSkill->getAnimationCount()) { + animationRandomCycleCount = 0; + } + } + return result; + } + + //const Model *Unit::getCurrentModel() { + // if(currSkill == NULL) { + // char szBuf[8096]=""; + // snprintf(szBuf,8096,"In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->toString().c_str()); + // throw megaglest_runtime_error(szBuf); + // } + // + // int currentModelIndexForCurrSkillType = lastModelIndexForCurrSkillType; + // const Model *result = currSkill->getAnimation(getAnimProgressAsFloat(),this,&lastModelIndexForCurrSkillType, &animationRandomCycleCount); + // if(currentModelIndexForCurrSkillType != lastModelIndexForCurrSkillType) { + // animationRandomCycleCount++; + // if(currSkill != NULL && animationRandomCycleCount >= currSkill->getAnimationCount()) { + // animationRandomCycleCount = 0; + // } + // } + // return result; + //} + + bool Unit::checkModelStateInfoForNewHpValue() { + bool result = false; + if (currSkill != NULL && currSkill->getAnimationCount() > 1) { + if (lastModelIndexForCurrSkillType >= 0) { + const AnimationAttributes attributes = + currSkill->getAnimationAttribute(lastModelIndexForCurrSkillType); + if (attributes.fromHp != 0 || attributes.toHp != 0) { + //printf("Check for RESET model state for [%d - %s] HP = %d [%d to %d]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp); + //if(this->getHp() >= attributes.fromHp && this->getHp() <= attributes.toHp) { + if (this->getHp() < attributes.fromHp + || this->getHp() > attributes.toHp) { + //printf("RESET model state for [%d - %s] HP = %d [%d to %d]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp); + + lastModelIndexForCurrSkillType = -1; + animationRandomCycleCount = 0; + result = true; + } + } else { + //printf("Check for RESET #2 model state for [%d - %s] HP = %d [%d to %d] for skill [%s]\n",this->id,this->getType()->getName().c_str(),this->getHp(),attributes.fromHp,attributes.toHp,currSkill->getName().c_str()); + } + } + } + + return result; + } + + Vec3f Unit::getCurrVectorForParticlesystems() const { + if (getFaction()->getType()->isFlatParticlePositions()) { + return getCurrVectorFlat(); + } else { + return getCurrMidHeightVector(); + } + } + + Vec3f Unit::getCurrMidHeightVector() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + Vec3f result = + getCurrVectorFlat() + Vec3f(0.f, type->getHeight() / 2.f, 0.f); + result.x = truncateDecimal < float >(result.x, 6); + result.y = truncateDecimal < float >(result.y, 6); + result.z = truncateDecimal < float >(result.z, 6); + + return result; + } + + Vec3f Unit::getCurrVectorAsTarget() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + Vec3f result = + getCurrVectorFlat() + Vec3f(0.f, type->getTargetHeight() / 2.f, + 0.f); + result.x = truncateDecimal < float >(result.x, 6); + result.y = truncateDecimal < float >(result.y, 6); + result.z = truncateDecimal < float >(result.z, 6); + + return result; + } + + Vec3f Unit::getCurrBurnVector() const { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + Vec3f result = + getCurrVectorFlat() + Vec3f(0.f, type->getBurnHeight() / 2.f, 0.f); + result.x = truncateDecimal < float >(result.x, 6); + result.y = truncateDecimal < float >(result.y, 6); + result.z = truncateDecimal < float >(result.z, 6); + + return result; + } + + Vec3f Unit::getCurrVectorFlat() const { + return getVectorFlat(lastPos, pos); + } + + float Unit::getProgressAsFloat() const { + float result = + (static_cast (progress) / static_cast < + float>(PROGRESS_SPEED_MULTIPLIER)); + result = truncateDecimal < float >(result, 6); + return result; + } + + Vec3f Unit::getVectorFlat(const Vec2i & lastPosValue, + const Vec2i & curPosValue) const { + Vec3f v; + + float y1 = computeHeight(lastPosValue); + float y2 = computeHeight(curPosValue); + + if (currSkill->getClass() == scMove) { + float progressAsFloat = getProgressAsFloat(); + + v.x = + lastPosValue.x + progressAsFloat * (curPosValue.x - lastPosValue.x); + v.z = + lastPosValue.y + progressAsFloat * (curPosValue.y - lastPosValue.y); + v.y = y1 + progressAsFloat * (y2 - y1); + + v.x = truncateDecimal < float >(v.x, 6); + v.y = truncateDecimal < float >(v.y, 6); + v.z = truncateDecimal < float >(v.z, 6); + } else { + v.x = static_cast (curPosValue.x); + v.z = static_cast (curPosValue.y); + v.y = y2; + + v.x = truncateDecimal < float >(v.x, 6); + v.y = truncateDecimal < float >(v.y, 6); + v.z = truncateDecimal < float >(v.z, 6); + } + v.x += type->getSize() / 2.f - 0.5f; + v.z += type->getSize() / 2.f - 0.5f; + + v.x = truncateDecimal < float >(v.x, 6); + v.z = truncateDecimal < float >(v.z, 6); + + return v; + } + + // =================== Command list related =================== + + //any command + bool Unit::anyCommand(bool validateCommandtype) const { + bool result = false; + if (validateCommandtype == false) { + result = (commands.empty() == false); + } else { + for (Commands::const_iterator it = commands.begin(); + it != commands.end(); ++it) { + const CommandType *ct = (*it)->getCommandType(); + if (ct != NULL && ct->getClass() != ccStop) { + result = true; + break; + } + } + } + + return result; + } + + //return current command, assert that there is always one command + Command *Unit::getCurrrentCommandThreadSafe() { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + if (commands.empty() == false) { + return commands.front(); + } + + return NULL; + } + + void Unit::replaceCurrCommand(Command * cmd) { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + assert(commands.empty() == false); + commands.front() = cmd; + this->setCurrentUnitTitle(""); + } + + //returns the size of the commands + unsigned int Unit::getCommandSize() const { + return (unsigned int) commands.size(); + } + + //return current command, assert that there is always one command + int Unit::getCountOfProducedUnits(const UnitType * ut) const { + int count = 0; + for (Commands::const_iterator it = commands.begin(); + it != commands.end(); ++it) { + const CommandType *ct = (*it)->getCommandType(); + if (ct->getClass() == ccProduce || ct->getClass() == ccMorph) { + const UnitType *producedUnitType = + static_cast (ct->getProduced()); + if (producedUnitType == ut) { + count++; + } + } + if (ct->getClass() == ccBuild) { + const UnitType *builtUnitType = (*it)->getUnitType(); + if (builtUnitType == ut) { + count++; + } + } + } + return count; + } + + //give one command (clear, and push back) + std::pair < CommandResult, string > Unit::giveCommand(Command * command, + bool tryQueue) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "\n======================\nUnit Command tryQueue = %d\nUnit Info:\n%s\nCommand Info:\n%s\n", + tryQueue, this->toString().c_str(), + command->toString(false).c_str()); + + std::pair < CommandResult, string > result(crFailUndefined, ""); + changedActiveCommand = false; + + Chrono chrono; + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled) + chrono.start(); + + if (command == NULL) { + throw megaglest_runtime_error("command == NULL"); + } + if (command->getCommandType() == NULL) { + throw megaglest_runtime_error("command->getCommandType() == NULL"); + } + + const int command_priority = command->getPriority(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + //printf("In [%s::%s] Line: %d unit [%d - %s] command [%s] tryQueue = %d command->getCommandType()->isQueuable(tryQueue) = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,this->getId(),this->getType()->getName().c_str(), command->getCommandType()->getName().c_str(), tryQueue,command->getCommandType()->isQueuable(tryQueue)); + + + if (command->getCommandType()->isQueuable(tryQueue)) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + if (SystemFlags::getSystemSettingType + (SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "In [%s::%s Line: %d] Command is Queable\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + if (command->getCommandType()->isQueuable() == qAlways && tryQueue) { + // Its a produce or upgrade command called without queued key + // in this case we must NOT delete lower priority commands! + // we just queue it! + + } else { + //Delete all lower-prioirty commands + for (list < Command * >::iterator i = commands.begin(); + i != commands.end();) { + if ((*i)->getPriority() < command_priority) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "In [%s::%s Line: %d] Deleting lower priority command [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + (*i)->toString(false).c_str()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + deleteQueuedCommand(*i); + i = commands.erase(i); + + safeMutex.ReleaseLock(); + } else { + ++i; + } + } + } + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + + //cancel current command if it is not queuable + if (commands.empty() == false && + commands.back()->getCommandType()->isQueueAppendable() == + false) { + if (SystemFlags::getSystemSettingType + (SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "In [%s::%s Line: %d] Cancel command because last one is NOT queable [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, + commands.back()-> + toString(false).c_str()); + + cancelCommand(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + } else { + //empty command queue + if (SystemFlags::getSystemSettingType + (SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "In [%s::%s Line: %d] Clear commands because current is NOT queable.\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + bool willChangedActiveCommand = (commands.empty() == false); + if (willChangedActiveCommand == true) { + + CommandClass currCommandClass = + getCurrCommand()->getCommandType()->getClass(); + CommandClass commandClass = command->getCommandType()->getClass(); + if (currCommandClass == commandClass) { + + willChangedActiveCommand = false; + } else if (currCommandClass == ccAttack || + currCommandClass == ccAttackStopped || + commandClass == ccAttack || + commandClass == ccAttackStopped) { + + willChangedActiveCommand = true; + } else { + willChangedActiveCommand = false; + } + } + + if (willChangedActiveCommand == true) { + + lastChangedActiveCommandFrame = changedActiveCommandFrame; + changedActiveCommandFrame = getFrameCount(); + + //printf("Line: %d getCurrCommand() [%s] command [%s]\n",__LINE__,getCurrCommand()->toString(false).c_str(),command->toString(false).c_str()); + } + + clearCommands(); + changedActiveCommand = willChangedActiveCommand; + + //printf("In [%s::%s] Line: %d cleared existing commands\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, chrono.getMillis()); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + //check command + result = checkCommand(command); + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugUnitCommands).enabled) + SystemFlags::OutputDebug(SystemFlags::debugUnitCommands, + "In [%s::%s Line: %d] checkCommand returned: [%d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + result.first); + + //printf("In [%s::%s] Line: %d check command returned %d, commands.size() = %d\n[%s]\n\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,result,commands.size(),command->toString().c_str()); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + if (result.first == crSuccess) { + applyCommand(command); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + //push back command + if (result.first == crSuccess) { + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + commands.push_back(command); + + safeMutex.ReleaseLock(); + } else { + delete command; + changedActiveCommand = false; + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugPerformance).enabled + && chrono.getMillis() > 0) + SystemFlags::OutputDebug(SystemFlags::debugPerformance, + "In [%s::%s] Line: %d took msecs: %lld\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, __LINE__, + chrono.getMillis()); + + return result; + } + + //pop front (used when order is done) + CommandResult Unit::finishCommand() { + changedActiveCommand = false; + retryCurrCommandCount = 0; + // Reset the progress when task completed. + resetProgress2(); + this->setCurrentUnitTitle(""); + //is empty? + if (commands.empty()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + return crFailUndefined; + } + + //pop front + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + delete commands.front(); + commands.erase(commands.begin()); + + safeMutex.ReleaseLock(true); + + this->unitPath->clear(); + + while (commands.empty() == false) { + if (commands.front()->getUnit() != NULL + && this->faction->isUnitInLivingUnitsp(commands. + front()->getUnit()) == + false) { + safeMutex.Lock(); + delete commands.front(); + commands.erase(commands.begin()); + safeMutex.ReleaseLock(true); + } else { + break; + } + } + + return crSuccess; + } + + //to cancel a command + CommandResult Unit::cancelCommand() { + changedActiveCommand = false; + retryCurrCommandCount = 0; + + this->setCurrentUnitTitle(""); + + //is empty? + if (commands.empty()) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + return crFailUndefined; + } + + //undo command + undoCommand(commands.back()); + + //delete ans pop command + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + delete commands.back(); + commands.pop_back(); + + // Reset the progress if the last command in the queue was cancelled. + // We don't want to reset the progress if we're not removing the last command, + // because then the progress of the actual command in progress. + /// TODO: extra if statement below needed adding make the reset function properly. Can this be avoided? + if (commands.empty()) resetProgress2(); + + safeMutex.ReleaseLock(); + + //clear routes + this->unitPath->clear(); + + + + return crSuccess; + } + + // =================== route stack =================== + + void Unit::create(bool startingUnit) { + faction->addUnit(this); + map->putUnitCells(this, pos); + if (startingUnit) { + faction->applyStaticCosts(type, NULL); + } + } + + void Unit::born(const CommandType * ct) { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + faction->addStore(type); + faction->applyStaticProduction(type, ct); + setCurrSkill(scStop); + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + + + //set hp from start hp + checkItemInVault(&this->ep, this->ep); + if (type->getStartHpType() == UnitType::stValue) { + this->hp = type->getStartHpValue(); + } else { + this->hp = + type->getTotalMaxHp(&totalUpgrade) * + type->getStartHpPercentage() / 100; + } + + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + //set ep from start ep + checkItemInVault(&this->ep, this->ep); + if (type->getStartEpType() == UnitType::stValue) { + this->ep = type->getStartEpValue(); + } else { + this->ep = + type->getTotalMaxEp(&totalUpgrade) * + type->getStartEpPercentage() / 100; + } + } + + void Unit::kill() { + //no longer needs static resources + if (isBeingBuilt()) { + faction->deApplyStaticConsumption(type, + (getCurrCommand() != + NULL ? + getCurrCommand()->getCommandType + () : NULL)); + } else { + faction->deApplyStaticCosts(type, + (getCurrCommand() != + NULL ? + getCurrCommand()->getCommandType() : + NULL)); + } + + //do the cleaning + //clear commands ( and their blocking fields ) + clearCommands(); + + map->clearUnitCells(this, pos, true); + if (isBeingBuilt() == false) { + faction->removeStore(type); + } + setCurrSkill(scDie); + + notifyObservers(UnitObserver::eKill); + + + UnitUpdater *unitUpdater = game->getWorld()->getUnitUpdater(); + //unitUpdater->clearUnitPrecache(this); + unitUpdater->removeUnitPrecache(this); + } + + void Unit::undertake() { + try { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] about to undertake unit id = %d [%s] [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, this->id, + this->getFullName(false).c_str(), + this->getDesc(false).c_str()); + + // Remove any units that were previously in attack-boost range + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false + && currentAttackBoostOriginatorEffect.skillType != NULL) { + for (unsigned int i = 0; + i < + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size(); ++i) { + // Remove attack boost upgrades from unit + int findUnitId = + currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; + Unit *affectedUnit = game->getWorld()->findUnitById(findUnitId); + if (affectedUnit != NULL) { + affectedUnit->deapplyAttackBoost + (currentAttackBoostOriginatorEffect.skillType-> + getAttackBoost(), this); + } + + //printf("!!!! DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + currentAttackBoostOriginatorEffect.currentAttackBoostUnits.clear(); + } + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + UnitUpdater *unitUpdater = game->getWorld()->getUnitUpdater(); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + //unitUpdater->clearUnitPrecache(this); + unitUpdater->removeUnitPrecache(this); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + this->faction->deleteLivingUnits(id); + this->faction->deleteLivingUnitsp(this); + + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugSystem).enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + + faction->removeUnit(this); + } catch (const megaglest_runtime_error & ex) { + string sErrBuf = ""; + if (ex.wantStackTrace() == true) { + char szErrBuf[8096] = ""; + snprintf(szErrBuf, 8096, "In [%s::%s %d]", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + sErrBuf = + string(szErrBuf) + string("\nerror [") + string(ex.what()) + + string("]\n"); + } else { + sErrBuf = ex.what(); + } + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + + throw; + } catch (const std::exception & ex) { + char szErrBuf[8096] = ""; + snprintf(szErrBuf, 8096, "In [%s::%s %d]", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__); + string sErrBuf = + string(szErrBuf) + string("\nerror [") + string(ex.what()) + + string("]\n"); + SystemFlags::OutputDebug(SystemFlags::debugError, sErrBuf.c_str()); + + throw; + } + } + + // =================== Referencers =================== + + void Unit::addObserver(UnitObserver * unitObserver) { + observers.push_back(unitObserver); + } + + //void Unit::removeObserver(UnitObserver *unitObserver){ + // observers.remove(unitObserver); + //} + + void Unit::notifyObservers(UnitObserver::Event event) { + for (Observers::iterator it = observers.begin(); + it != observers.end(); ++it) { + (*it)->unitEvent(event, this); + } + } + + // =================== Other =================== + + void Unit::resetHighlight() { + highlight = 1.f; + } + + const CommandType *Unit::computeCommandType(const Vec2i & pos, + const Unit * + targetUnit) const { + const CommandType *commandType = NULL; + + if (map->isInside(pos) == false + || map->isInsideSurface(map->toSurfCoords(pos)) == false) { + throw megaglest_runtime_error("#6 Invalid path position = " + + pos.getString()); + } + + SurfaceCell *sc = map->getSurfaceCell(Map::toSurfCoords(pos)); + + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //printf("Line: %d Unit::computeCommandType pos [%s] targetUnit [%s]\n",__LINE__,pos.getString().c_str(),(targetUnit != NULL ? targetUnit->getType()->getName().c_str() : "(null)")); + if (targetUnit != NULL) { + //attack enemies + if (isAlly(targetUnit) == false) { + commandType = + type->getFirstAttackCommand(targetUnit->getCurrField()); + } + //repair allies + else { + if (targetUnit->isBuilt() == false + || targetUnit->isDamaged() == true) { + commandType = + type->getFirstRepairCommand(targetUnit->getType()); + } + + //printf("Line: %d Unit::computeCommandType pos [%s] targetUnit [%s] commandType [%p]\n",__LINE__,pos.getString().c_str(),(targetUnit != NULL ? targetUnit->getType()->getName().c_str() : "(null)"),commandType); + + if (commandType == NULL && targetUnit != NULL) { + //Command *command= this->getCurrCommand(); + //const HarvestCommandType *hct= dynamic_cast((command != NULL ? command->getCommandType() : NULL)); + + // Check if we can return whatever resources we have + if (targetUnit->getFactionIndex() == this->getFactionIndex() && + targetUnit->isOperative() == true && + this->getLoadType() != NULL && this->getLoadCount() != 0 && + targetUnit->getType() != NULL && + targetUnit->getType()->getStore(this->getLoadType()) > 0) { + + commandType = type->getFirstHarvestEmergencyReturnCommand(); + } + } + } + } else { + //check harvest command + Resource *resource = sc->getResource(); + if (resource != NULL) { + commandType = + type->getFirstHarvestCommand(resource->getType(), + this->getFaction()); + } + } + + if (targetUnit == NULL && commandType == NULL) { + const Vec2i unitTargetPos = pos; + Cell *cell = map->getCell(unitTargetPos); + if (cell != NULL && cell->getUnit(this->getCurrField()) != NULL) { + Unit *targetUnit = cell->getUnit(this->getCurrField()); + if (targetUnit != NULL) { + // Check if we want to help build (repair) any buildings instead of just moving + if (targetUnit->getFactionIndex() == this->getFactionIndex() && + (targetUnit->isBuilt() == false + || targetUnit->isDamaged() == true)) { + const RepairCommandType *rct = + this->getType()-> + getFirstRepairCommand(targetUnit->getType()); + if (rct != NULL) { + commandType = + type->getFirstRepairCommand(targetUnit->getType()); + //printf("************ Unit will repair building built = %d, repair = %d\n",targetUnit->isBuilt(),targetUnit->isDamaged()); + } + } + } + } + } + + // Default command is the class of the unit (i.e. attack for attackers, walk for walkers). + // The default command is executed when a unit is produced and sent to a meeting point + // or when the unit is selected and right clicked to a position. + if (commandType == NULL) { + // move + // commandType = type->getFirstCtOfClass (ccMove); + + // attack + // Is the unit class warrior? if yes, attack by default, else walk. + commandType = + type->getFirstCtOfClass(this->getType()-> + isOfClass(ucWarrior) ? ccAttack : ccMove); + + // FIXME: I think a better solution would be to have a hotkey for this, + // the user can decide, and toggle in-game -andy5995 2018-02-03 + } + + return commandType; + } + + int64 Unit::getUpdateProgress() { + if (progress > PROGRESS_SPEED_MULTIPLIER) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: progress > " MG_I64_SPECIFIER + ", progress = [" MG_I64_SPECIFIER "]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, PROGRESS_SPEED_MULTIPLIER, + progress); + throw megaglest_runtime_error(szBuf); + } + + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + int64 newProgress = progress; + if (currSkill->getClass() != scDie) { + //speed + int speed = currSkill->getTotalSpeed(&totalUpgrade); + + if (changedActiveCommand == true) { + if (changedActiveCommandFrame - lastChangedActiveCommandFrame >= + MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED) { + //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] skill [%s] command [%s]\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,currSkill->toString(false).c_str(),getCurrCommand()->toString(false).c_str()); + //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] total = %u\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,(changedActiveCommandFrame - lastChangedActiveCommandFrame)); + speed = CHANGE_COMMAND_SPEED; + } + } + + //speed modifier + int64 diagonalFactor = getDiagonalFactor(); + int64 heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER); + + //update progresses + // const Game *game = Renderer::getInstance().getGame(); + // if(game == NULL) { + // throw megaglest_runtime_error("game == NULL"); + // } + // if(game->getWorld() == NULL) { + // throw megaglest_runtime_error("game->getWorld() == NULL"); + // } + + newProgress = getUpdatedProgress(progress, GameConstants::updateFps, + speed, diagonalFactor, + heightFactor); + + } + return newProgress; + } + + bool Unit::needToUpdate() { + bool return_value = false; + if (currSkill->getClass() != scDie) { + int newProgress = getUpdateProgress(); + if (newProgress >= PROGRESS_SPEED_MULTIPLIER) { + return_value = true; + } + } + return return_value; + } + + int64 Unit::getDiagonalFactor() { + //speed modifier + int64 diagonalFactor = PROGRESS_SPEED_MULTIPLIER; + if (currSkill->getClass() == scMove) { + //if moving in diagonal move slower + Vec2i dest = pos - lastPos; + if (abs(dest.x) + abs(dest.y) == 2) { + //diagonalFactor = 0.71f * PROGRESS_SPEED_MULTIPLIER; + diagonalFactor = 71 * (PROGRESS_SPEED_MULTIPLIER / 100); + } + } + return diagonalFactor; + } + + int64 Unit::getHeightFactor(int64 speedMultiplier) { + int64 heightFactor = speedMultiplier; + if (currSkill->getClass() == scMove) { + //if moving to an higher cell move slower else move faster + Cell *unitCell = map->getCell(pos); + if (unitCell == NULL) { + throw megaglest_runtime_error("unitCell == NULL"); + } + + Cell *targetCell = map->getCell(targetPos); + if (targetCell == NULL) { + throw megaglest_runtime_error("targetCell == NULL"); + } + + int64 heightDiff = ((truncateDecimal < float >(unitCell->getHeight(), + 2) * + speedMultiplier) - (truncateDecimal < + float >(targetCell-> + getHeight(), + 2) * + speedMultiplier)); + //heightFactor= clamp(speedMultiplier + heightDiff / (5.f * speedMultiplier), 0.2f * speedMultiplier, 5.f * speedMultiplier); + heightFactor = + clamp(speedMultiplier + heightDiff / (5 * speedMultiplier), + (2 * (speedMultiplier / 10)), 5 * speedMultiplier); + } + + return heightFactor; + } + + int64 Unit::getSpeedDenominator(int64 updateFPS) { + int64 speedDenominator = + (speedDivider * updateFPS) * PROGRESS_SPEED_MULTIPLIER; + return speedDenominator; + } + int64 Unit::getUpdatedProgress(int64 currentProgress, int64 updateFPS, + int64 speed, int64 diagonalFactor, + int64 heightFactor) { + + int64 speedDenominator = getSpeedDenominator(updateFPS); + int64 newProgress = currentProgress; + int64 progressIncrease = + ((speed * diagonalFactor * heightFactor) / speedDenominator); + // Ensure we increment at least a value of 1 of the action will be stuck infinitely + if (speed > 0 && diagonalFactor > 0 && heightFactor > 0 + && progressIncrease == 0) { + progressIncrease = 1; + } + + if (isNetworkCRCEnabled() == true) { + float height = map->getCell(pos)->getHeight(); + float airHeight = game->getWorld()->getTileset()->getAirHeight(); + int cellUnitHeight = -1; + int cellObjectHeight = -1; + + Unit *unit = map->getCell(pos)->getUnit(fLand); + if (unit != NULL && unit->getType()->getHeight() > airHeight) { + cellUnitHeight = unit->getType()->getHeight(); + } else { + SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(pos)); + if (sc != NULL && sc->getObject() != NULL + && sc->getObject()->getType() != NULL) { + if (sc->getObject()->getType()->getHeight() > airHeight) { + cellObjectHeight = sc->getObject()->getType()->getHeight(); + } + } + } + + + char szBuf[8096] = ""; + snprintf(szBuf, 8095, + "currentProgress = " MG_I64_SPECIFIER " updateFPS = " + MG_I64_SPECIFIER " speed = " MG_I64_SPECIFIER + " diagonalFactor = " MG_I64_SPECIFIER " heightFactor = " + MG_I64_SPECIFIER " speedDenominator = " MG_I64_SPECIFIER + " progressIncrease = " MG_I64_SPECIFIER " [" + MG_I64_SPECIFIER + "] height [%f] airHeight [%f] cellUnitHeight [%d] cellObjectHeight [%d] skill [%s] pos [%s] lastpos [%s]", + currentProgress, updateFPS, speed, diagonalFactor, + heightFactor, speedDenominator, progressIncrease, + ((speed * diagonalFactor * heightFactor) / + speedDenominator), height, airHeight, cellUnitHeight, + cellObjectHeight, + (currSkill != + NULL ? currSkill->getName().c_str() : "none"), + pos.getString().c_str(), lastPos.getString().c_str()); + networkCRCLogInfo = szBuf; + + //printf("%s\n",szBuf); + } + + newProgress += progressIncrease; + + // if(currSkill->getClass() == scMove || (currSkill->getClass() == scStop && this->loadCount > 0)) { + // printf("speedDenominator: " MG_I64_SPECIFIER " currentProgress: " MG_I64_SPECIFIER " speed: " MG_I64_SPECIFIER " diagonalFactor: " MG_I64_SPECIFIER " heightFactor: " MG_I64_SPECIFIER " progressIncrease: " MG_I64_SPECIFIER " newProgress: " MG_I64_SPECIFIER " TOP #: " MG_I64_SPECIFIER "\n",speedDenominator,currentProgress,speed,diagonalFactor,heightFactor,progressIncrease,newProgress,(speed * diagonalFactor * heightFactor)); + // } + + return newProgress; + } + + void Unit::updateAttackBoostProgress(const Game * game) { + const bool debugBoost = false; + if (debugBoost) + printf + ("===================== START Unit [%d - %s] skill: %s affected unit size: " + MG_SIZE_T_SPECIFIER "\n", this->id, + this->getType()->getName(false).c_str(), + currSkill->getBoostDesc(false).c_str(), + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size()); + + if (currSkill != currentAttackBoostOriginatorEffect.skillType) { + + if (debugBoost) + printf("Line: %d new [%s]\n", __LINE__, + (currentAttackBoostOriginatorEffect.skillType != + NULL ? currentAttackBoostOriginatorEffect. + skillType->getBoostDesc(false).c_str() : "")); + + if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) { + delete currentAttackBoostOriginatorEffect.currentAppliedEffect; + currentAttackBoostOriginatorEffect.currentAppliedEffect = NULL; + + //printf("- #1 DE-APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); + } + + // Remove any units that were previously in range + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false + && currentAttackBoostOriginatorEffect.skillType != NULL) { + for (unsigned int i = 0; + i < + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size(); ++i) { + // Remove attack boost upgrades from unit + + int findUnitId = + currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; + Unit *affectedUnit = game->getWorld()->findUnitById(findUnitId); + if (affectedUnit != NULL) { + affectedUnit->deapplyAttackBoost + (currentAttackBoostOriginatorEffect.skillType-> + getAttackBoost(), this); + } + + //printf("- #1 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + currentAttackBoostOriginatorEffect.currentAttackBoostUnits.clear(); + } + + if (debugBoost) + printf("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", + __LINE__, + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size()); + + currentAttackBoostOriginatorEffect.skillType = currSkill; + + if (currSkill->isAttackBoostEnabled() == true) { + if (debugBoost) + printf("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", + __LINE__, + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size()); + + // Search for units in range of this unit which apply to the + // attack-boost and temporarily upgrade them + UnitUpdater *unitUpdater = + this->game->getWorld()->getUnitUpdater(); + + const AttackBoost *attackBoost = currSkill->getAttackBoost(); + vector < Unit * >candidates = unitUpdater->findUnitsInRange(this, + attackBoost->radius); + + if (debugBoost) + printf("Line: %d candidates unit size: " MG_SIZE_T_SPECIFIER + " attackBoost: %s\n", __LINE__, candidates.size(), + attackBoost->getDesc(false).c_str()); + for (unsigned int i = 0; i < candidates.size(); ++i) { + Unit *affectedUnit = candidates[i]; + if (attackBoost->isAffected(this, affectedUnit) == true) { + if (affectedUnit->applyAttackBoost(attackBoost, this) == true) { + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.push_back(affectedUnit->getId()); + //printf("+ #1 APPLY ATTACK BOOST to unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + } + } + + if (debugBoost) + printf("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", + __LINE__, + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size()); + + if (showUnitParticles == true) { + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false) { + if (attackBoost->unitParticleSystemTypeForSourceUnit != NULL) { + currentAttackBoostOriginatorEffect.currentAppliedEffect = + new UnitAttackBoostEffect(); + currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst = new UnitParticleSystemType(); + *currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst = + *attackBoost->unitParticleSystemTypeForSourceUnit; + //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; + + currentAttackBoostOriginatorEffect.currentAppliedEffect->ups = + new UnitParticleSystem(200); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setParticleOwner(this); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setParticleType + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + upst); + + currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst-> + setValues + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setPos(getCurrVectorForParticlesystems()); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setRotation(getRotation()); + setMeshPosInParticleSystem + (currentAttackBoostOriginatorEffect. + currentAppliedEffect->ups); + + if (getFaction()->getTexture()) { + currentAttackBoostOriginatorEffect. + currentAppliedEffect->ups-> + setFactionColor(getFaction()->getTexture()-> + getPixmapConst()->getPixel3f(0, 0)); + } + Renderer:: + getInstance().manageParticleSystem + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups, rsGame); + //printf("+ #1 APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); + } + } + } + } + } else { + if (currSkill->isAttackBoostEnabled() == true) { + if (debugBoost) + printf("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", + __LINE__, + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size()); + + // Search for units in range of this unit which apply to the + // attack-boost and temporarily upgrade them + UnitUpdater *unitUpdater = + this->game->getWorld()->getUnitUpdater(); + + const AttackBoost *attackBoost = currSkill->getAttackBoost(); + vector < Unit * >candidates = + unitUpdater->findUnitsInRange(this, attackBoost->radius); + vector < int >candidateValidIdList; + candidateValidIdList.reserve(candidates.size()); + + if (debugBoost) + printf("Line: %d candidates unit size: " MG_SIZE_T_SPECIFIER + " attackBoost: %s\n", __LINE__, candidates.size(), + attackBoost->getDesc(false).c_str()); + + for (unsigned int i = 0; i < candidates.size(); ++i) { + Unit *affectedUnit = candidates[i]; + candidateValidIdList.push_back(affectedUnit->getId()); + + std::vector < int >::iterator iterFound = + std:: + find + (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + begin(), + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + end(), + affectedUnit->getId()); + + if (attackBoost->isAffected(this, affectedUnit) == true) { + if (iterFound == + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.end()) { + if (affectedUnit->applyAttackBoost(attackBoost, this) == + true) { + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.push_back(affectedUnit-> + getId()); + + //printf("+ #2 APPLY ATTACK BOOST to unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + } + } else { + if (iterFound != + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.end()) { + affectedUnit->deapplyAttackBoost + (currentAttackBoostOriginatorEffect.skillType-> + getAttackBoost(), this); + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.erase(iterFound); + + //printf("- #2 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + } + } + + // Now remove any units that were in the list of boosted units but + // are no longer in range + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false) { + for (int i = + (int) + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size() - 1; i >= 0; --i) { + int findUnitId = + currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; + + std::vector < int >::iterator iterFound = + std::find(candidateValidIdList.begin(), + candidateValidIdList.end(), + findUnitId); + if (iterFound == candidateValidIdList.end()) { + Unit *affectedUnit = + game->getWorld()->findUnitById(findUnitId); + if (affectedUnit != NULL) { + affectedUnit->deapplyAttackBoost + (currentAttackBoostOriginatorEffect.skillType-> + getAttackBoost(), this); + + if (debugBoost) + printf + ("Removed attack boost from Unit [%d - %s] since they are NO LONGER in range\n", + affectedUnit->id, + affectedUnit->getType()->getName(false).c_str()); + + } + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.erase + (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + begin() + i); + } + } + } + + if (debugBoost) + printf("Line: %d affected unit size: " MG_SIZE_T_SPECIFIER "\n", + __LINE__, + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size()); + + if (showUnitParticles == true) { + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false) { + if (attackBoost->unitParticleSystemTypeForSourceUnit != NULL + && currentAttackBoostOriginatorEffect.currentAppliedEffect + == NULL) { + + currentAttackBoostOriginatorEffect.currentAppliedEffect = + new UnitAttackBoostEffect(); + currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst = new UnitParticleSystemType(); + *currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst = + *attackBoost->unitParticleSystemTypeForSourceUnit; + //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; + + currentAttackBoostOriginatorEffect.currentAppliedEffect->ups = + new UnitParticleSystem(200); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setParticleOwner(this); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setParticleType + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + upst); + + currentAttackBoostOriginatorEffect. + currentAppliedEffect->upst-> + setValues + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setPos(getCurrVectorForParticlesystems()); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setRotation(getRotation()); + setMeshPosInParticleSystem + (currentAttackBoostOriginatorEffect. + currentAppliedEffect->ups); + + if (getFaction()->getTexture()) { + currentAttackBoostOriginatorEffect. + currentAppliedEffect->ups-> + setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + Renderer:: + getInstance().manageParticleSystem + (currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups, rsGame); + + //printf("+ #2 APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); + } + } else + if (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + empty() == true) { + if (currentAttackBoostOriginatorEffect.currentAppliedEffect != + NULL) { + delete + currentAttackBoostOriginatorEffect.currentAppliedEffect; + currentAttackBoostOriginatorEffect.currentAppliedEffect = + NULL; + + //printf("- #2 DE-APPLY ATTACK BOOST SELF PARTICLE to unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId()); + } + } + } + } + } + + if (debugBoost) { + if (currSkill->isAttackBoostEnabled() == true) { + printf("Unit [%d - %s] has attackboost enabled: %s\n", this->id, + this->getType()->getName(false).c_str(), + currSkill->getBoostDesc(false).c_str()); + + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false) { + printf + ("Found affected units currentAttackBoostOriginatorEffect.skillType [%p]\n", + currentAttackBoostOriginatorEffect.skillType); + + for (unsigned int i = 0; + i < + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size(); ++i) { + int unitnum = + currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; + printf("affected unit #: %u - %d\n", i, unitnum); + } + } + } + } + } + + bool Unit::update() { + assert(progress <= PROGRESS_SPEED_MULTIPLIER); + + //highlight + if (highlight > 0.f) { + const Game *game = Renderer::getInstance().getGame(); + highlight -= + 1.f / (Game::highlightTime * + game->getWorld()->getUpdateFps(this->getFactionIndex())); + } + + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //speed + int speed = currSkill->getTotalSpeed(&totalUpgrade); + + if (oldTotalSight != + getType()->getTotalSight(this->getTotalUpgrade())) { + oldTotalSight = getType()->getTotalSight(this->getTotalUpgrade()); + // refresh FogOfWar and so on, because sight ha changed since last update + refreshPos(true); + } + + if (changedActiveCommand) { + if (changedActiveCommandFrame - lastChangedActiveCommandFrame >= + MIN_FRAMECOUNT_CHANGE_COMMAND_SPEED) { + //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] skill [%s] command [%s]\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,currSkill->toString(false).c_str(),getCurrCommand()->toString(false).c_str()); + //printf("Line: %d speed = %d changedActiveCommandFrame [%u] lastChangedActiveCommandFrame [%u] total = %u\n",__LINE__,speed,changedActiveCommandFrame,lastChangedActiveCommandFrame,(changedActiveCommandFrame - lastChangedActiveCommandFrame)); + speed = CHANGE_COMMAND_SPEED; + } + } + + //speed modifier + int64 diagonalFactor = getDiagonalFactor(); + int64 heightFactor = getHeightFactor(PROGRESS_SPEED_MULTIPLIER); + + //update progresses + this->lastAnimProgress = this->animProgress; + const Game *game = Renderer::getInstance().getGame(); + + if (animProgress == 0) { + AnimCycleStarts(); + } + progress = getUpdatedProgress(progress, + GameConstants::updateFps, + speed, diagonalFactor, heightFactor); + + //printf("Test progress = %d for unit [%d - %s]\n",progress,id,getType()->getName().c_str()); + + if (isAnimProgressBound() == true) { + float targetProgress = 0; + if (currSkill->getClass() == scBeBuilt) { + targetProgress = this->getHpRatio(); + } + if (currSkill->getClass() == scProduce) { + targetProgress = this->getProgressRatio(); + } + if (currSkill->getClass() == scUpgrade) { + targetProgress = this->getProgressRatio(); + } + if (currSkill->getClass() == scMorph) { + targetProgress = this->getProgressRatio(); + } + + float targetProgressIntValue = + targetProgress * ANIMATION_SPEED_MULTIPLIER; + if (this->animProgress < targetProgressIntValue) { + float diff = targetProgressIntValue - this->animProgress; + float progressIncrease = + static_cast (this->animProgress) + diff / static_cast < + float>(GameConstants::updateFps); + // Ensure we increment at least a value of 1 of the action will be stuck infinitely + if (diff > 0.f && GameConstants::updateFps > 0 + && progressIncrease == 0.f) { + progressIncrease = 1.f; + } + + //if(currSkill->getClass() == scBeBuilt) { + // printf("targetProgress: %.10f this->animProgress: %d diff: %.10f GameConstants::updateFps: %d progressIncrease: %.10f\n",targetProgress,this->animProgress,diff,GameConstants::updateFps,progressIncrease); + //} + + this->animProgress = progressIncrease; + + //if(currSkill->getClass() == scBeBuilt) { + // printf("Unit build progress: %d anim: %d\n",progress,this->animProgress); + //} + } + } else { + int64 heightFactor = getHeightFactor(ANIMATION_SPEED_MULTIPLIER); + int64 speedDenominator = speedDivider * + game->getWorld()->getUpdateFps(this->getFactionIndex()); + + // Override the animation speed for attacks that have upgraded the attack speed + int animSpeed = currSkill->getAnimSpeed(); + if (currSkill->getClass() == scAttack) { + int animSpeedBoost = + ((AttackSkillType *) + currSkill)->getAnimSpeedBoost(&totalUpgrade); + animSpeed += animSpeedBoost; + } + + int64 progressIncrease = + (animSpeed * heightFactor) / speedDenominator; + // Ensure we increment at least a value of 1 of the action will be stuck infinitely + if (currSkill->getAnimSpeed() > 0 && heightFactor > 0 + && progressIncrease == 0) { + progressIncrease = 1; + } + this->animProgress += progressIncrease; + //this->animProgress += (currSkill->getAnimSpeed() * heightFactor) / speedDenominator; + + //if(currSkill->getClass() == scDie) { + // printf("Unit died progress: %d anim: %d\n",progress,this->animProgress); + //} + } + //update target + updateTarget(); + + //rotation + if (currSkill->getClass() != scStop) { + const int rotFactor = 2; + if (getProgressAsFloat() < 1.f / rotFactor) { + if (type->getFirstStOfClass(scMove)) { + if (abs((int) (lastRotation - targetRotation)) < 180) + rotation = lastRotation + (targetRotation - lastRotation) * + getProgressAsFloat() * rotFactor; + else { + float rotationTerm = + targetRotation > lastRotation ? -360.f : +360.f; + rotation = + lastRotation + (targetRotation - lastRotation + + rotationTerm) * getProgressAsFloat() * + rotFactor; + } + } + } + } + + if (type->getProperty(UnitType::pRotatedClimb)) { + calculateXZRotation(); + } else { + rotationZ = .0f; + rotationX = .0f; + } + + if (Renderer:: + getInstance().validateParticleSystemStillExists(this->fire, + rsGame) == false) { + this->fire = NULL; + } + + if (this->fire != NULL) { + this->fire->setPos(getCurrBurnVector()); + } + for (UnitParticleSystems::iterator it = unitParticleSystems.begin(); + it != unitParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == true) { + (*it)->setPos(getCurrVectorForParticlesystems()); + (*it)->setRotation(getRotation()); + setMeshPosInParticleSystem(*it); + } + } + for (UnitParticleSystems::iterator it = damageParticleSystems.begin(); + it != damageParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == true) { + (*it)->setPos(getCurrVectorForParticlesystems()); + (*it)->setRotation(getRotation()); + setMeshPosInParticleSystem(*it); + } + } + + for (UnitParticleSystems::iterator it = smokeParticleSystems.begin(); + it != smokeParticleSystems.end(); ++it) { + if (Renderer:: + getInstance().validateParticleSystemStillExists((*it), + rsGame) == true) { + (*it)->setPos(getCurrMidHeightVector()); + (*it)->setRotation(getRotation()); + setMeshPosInParticleSystem(*it); + } + } + + //printf("Unit has attack boost? unit = [%d - %s] size = %d\n",this->getId(), this->getType()->getName(false).c_str(),(int)currentAttackBoostEffects.size()); + for (unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; + if (effect != NULL && effect->ups != NULL) { + bool particleValid = + Renderer:: + getInstance().validateParticleSystemStillExists(effect->ups, + rsGame); + if (particleValid == true) { + effect->ups->setPos(getCurrVectorForParticlesystems()); + effect->ups->setRotation(getRotation()); + setMeshPosInParticleSystem(effect->ups); + } + + //printf("i = %d particleValid = %d\n",i,particleValid); + } + //printf("i = %d effect = %p effect->ups = %p\n",i,effect,(effect ? effect->ups : NULL)); + } + + + if (currentAttackBoostOriginatorEffect.currentAppliedEffect != NULL) { + if (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups != + NULL) { + bool particleValid = + Renderer::getInstance().validateParticleSystemStillExists + (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups, + rsGame); + if (particleValid == true) { + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setPos(getCurrVectorForParticlesystems()); + currentAttackBoostOriginatorEffect.currentAppliedEffect-> + ups->setRotation(getRotation()); + setMeshPosInParticleSystem + (currentAttackBoostOriginatorEffect.currentAppliedEffect->ups); + } + } + } + + //checks + if (this->animProgress > ANIMATION_SPEED_MULTIPLIER) { + bool canCycle = + currSkill->CanCycleNextRandomAnimation(&animationRandomCycleCount); + this->animProgress = + currSkill->getClass() == scDie ? ANIMATION_SPEED_MULTIPLIER : 0; + if (canCycle == true) { + this->lastModelIndexForCurrSkillType = -1; + } + } + + bool return_value = false; + //checks + if (progress >= PROGRESS_SPEED_MULTIPLIER) { + lastRotation = targetRotation; + if (currSkill->getClass() != scDie) { + progress = 0; + return_value = true; + } else { + progress = PROGRESS_SPEED_MULTIPLIER; + deadCount++; + if (deadCount >= maxDeadCount) { + toBeUndertaken = true; + return_value = false; + } + } + } + + updateAttackBoostProgress(game); + + if (return_value) { + changedActiveCommand = false; + } + + return return_value; + } + + void Unit::updateTimedParticles() { + //!!! + // Start new particle systems based on start time + if (queuedUnitParticleSystemTypes.empty() == false) { + for (int i = (int) queuedUnitParticleSystemTypes.size() - 1; i >= 0; + i--) { + UnitParticleSystemType *pst = queuedUnitParticleSystemTypes[i]; + if (pst != NULL) { + if (truncateDecimal < float >(pst->getStartTime(), 6) <= + truncateDecimal < float >(getAnimProgressAsFloat(), 6)) { + + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType(pst); + + pst->setValues(ups); + ups->setPos(getCurrVectorForParticlesystems()); + ups->setRotation(getRotation()); + setMeshPosInParticleSystem(ups); + + if (getFaction()->getTexture()) { + ups->setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + unitParticleSystems.push_back(ups); + Renderer::getInstance().manageParticleSystem(ups, rsGame); + + queuedUnitParticleSystemTypes.erase + (queuedUnitParticleSystemTypes.begin() + i); + } + } else { + queuedUnitParticleSystemTypes.erase + (queuedUnitParticleSystemTypes.begin() + i); + } + } + } + + // End existing systems based on end time + if (unitParticleSystems.empty() == false) { + for (int i = (int) unitParticleSystems.size() - 1; i >= 0; i--) { + UnitParticleSystem *ps = unitParticleSystems[i]; + if (ps != NULL) { + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + float pst = ps->getStartTime(); + float pet = ps->getEndTime(); + float particleStartTime = truncateDecimal < float >(pst, 6); + float particleEndTime = truncateDecimal < float >(pet, 6); + + if (particleStartTime != 0.0 || particleEndTime != 1.0) { + float animProgressTime = + truncateDecimal < float >(getAnimProgressAsFloat(), 6); + if (animProgressTime >= 0.99 + || animProgressTime >= particleEndTime) { + + ps->fade(); + unitParticleSystems.erase(unitParticleSystems.begin() + + i); + } + } + } + } + } + } + } + + bool Unit::unitHasAttackBoost(const AttackBoost * boost, + const Unit * source) { + bool result = false; + for (unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; + if (effect != NULL && effect->boost->name == boost->name && + effect->getSource()->getType()->getId() == + source->getType()->getId()) { + result = true; + break; + } + } + + //printf("Unit has attack boost? source = [%d - %s] [%p] boost [%s] result = %d\n",source->getId(), source->getType()->getName(false).c_str(),source,boost->name.c_str(),result); + + return result; + } + + bool Unit::applyAttackBoost(const AttackBoost * boost, + const Unit * source) { + if (boost == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: boost == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //printf("APPLYING ATTACK BOOST to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); + + bool shouldApplyAttackBoost = true; + if (boost->allowMultipleBoosts == false) { + // Check if we already have this boost from this unit type and multiples + // are not allowed + bool alreadyHasAttackBoost = this->unitHasAttackBoost(boost, source); + if (alreadyHasAttackBoost == true) { + shouldApplyAttackBoost = false; + } + } + + if (shouldApplyAttackBoost == true) { + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("=== APPLYING ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", + this->getType()->getName(false).c_str(), this->getId(), + source->getType()->getName(false).c_str(), source->getId(), + hp); + + UnitAttackBoostEffect *effect = new UnitAttackBoostEffect(); + effect->boost = boost; + effect->setSource(source); + + bool wasAlive = alive; + int originalHp = hp; + int prevMaxHp = totalUpgrade.getMaxHp(); + int prevMaxHpRegen = totalUpgrade.getMaxHpRegeneration(); + //printf("#1 wasAlive = %d hp = %d boosthp = %d\n",wasAlive,hp,boost->boostUpgrade.getMaxHp()); + + totalUpgrade.apply(source->getId(), &boost->boostUpgrade, this); + + checkItemInVault(&this->hp, this->hp); + //hp += boost->boostUpgrade.getMaxHp(); + int original_hp = this->hp; + this->hp += (totalUpgrade.getMaxHp() - prevMaxHp); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + //regenerate hp upgrade / or boost + if (totalUpgrade.getMaxHpRegeneration() != 0) { + checkItemInVault(&this->hp, this->hp); + + //printf("BEFORE Apply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); + int original_hp = this->hp; + this->hp += (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + //if(hp > type->getTotalMaxHp(&totalUpgrade)) { + // hp = type->getTotalMaxHp(&totalUpgrade); + //} + addItemToVault(&this->hp, this->hp); + + //printf("AFTER Apply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); + } + + checkModelStateInfoForNewHpValue(); + + if (originalHp < hp) { + this->setLastAttackerUnitId(source->getId()); + } + //printf("#2 wasAlive = %d hp = %d boosthp = %d\n",wasAlive,hp,boost->boostUpgrade.getMaxHp()); + + if (showUnitParticles == true) { + if (boost->unitParticleSystemTypeForAffectedUnit != NULL) { + effect->upst = new UnitParticleSystemType(); + *effect->upst = *boost->unitParticleSystemTypeForAffectedUnit; + //effect.upst = boost->unitParticleSystemTypeForAffectedUnit; + + effect->ups = new UnitParticleSystem(200); + effect->ups->setParticleOwner(this); + effect->ups->setParticleType(effect->upst); + + effect->upst->setValues(effect->ups); + effect->ups->setPos(getCurrVectorForParticlesystems()); + effect->ups->setRotation(getRotation()); + setMeshPosInParticleSystem(effect->ups); + if (getFaction()->getTexture()) { + effect->ups->setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + Renderer::getInstance().manageParticleSystem(effect->ups, + rsGame); + } + } + currentAttackBoostEffects.push_back(effect); + + if (wasAlive == true) { + //startDamageParticles + + if (originalHp > this->hp) { + startDamageParticles(); + } + + //stop DamageParticles on death + if (this->hp <= 0) { + setAlive(false); + this->hp = 0; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + addItemToVault(&this->hp, this->hp); + checkModelStateInfoForNewHpValue(); + + stopDamageParticles(true); + + this->setLastAttackerUnitId(source->getId()); + this->setCauseOfDeath(ucodAttackBoost); + Unit::game->getWorld()->getStats()->die(getFactionIndex(), + getType + ()->getCountUnitDeathInStats + ()); + game->getScriptManager()->onUnitDied(this); + + StaticSound *sound = + static_cast < + const DieSkillType * + >(this->getType()->getFirstStOfClass(scDie))->getSound(); + if (sound != NULL + && (this->getFactionIndex() == + Unit::game->getWorld()->getThisFactionIndex() + || (game-> + getWorld()->showWorldForPlayer(game-> + getWorld + ()->getThisTeamIndex + ()) == true))) { + SoundRenderer::getInstance().playFx(sound); + } + + if (this->isDead() + && this->getCurrSkill()->getClass() != scDie) { + this->kill(); + } + } + } + + //printf("APPLYING ATTACK BOOST END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("APPLIED ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", + this->getType()->getName(false).c_str(), this->getId(), + source->getType()->getName(false).c_str(), source->getId(), + hp); + + return shouldApplyAttackBoost; + } + + void Unit::deapplyAttackBoost(const AttackBoost * boost, + const Unit * source) { + if (boost == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: boost == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("=== DE-APPLYING ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", + this->getType()->getName(false).c_str(), this->getId(), + source->getType()->getName(false).c_str(), source->getId(), + hp); + + bool wasAlive = alive; + int originalHp = hp; + int prevMaxHp = totalUpgrade.getMaxHp(); + int prevMaxHpRegen = totalUpgrade.getMaxHpRegeneration(); + totalUpgrade.deapply(source->getId(), &boost->boostUpgrade, + this->getId()); + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + //hp -= boost->boostUpgrade.getMaxHp(); + this->hp -= (prevMaxHp - totalUpgrade.getMaxHp()); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + //regenerate hp upgrade / or boost + if (totalUpgrade.getMaxHpRegeneration() != 0) { + checkItemInVault(&this->hp, this->hp); + + //printf("BEFORE DeApply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); + int original_hp = this->hp; + this->hp -= (totalUpgrade.getMaxHpRegeneration() - prevMaxHpRegen); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + //if(hp > totalUpgrade.getMaxHp()) { + // hp = totalUpgrade.getMaxHp(); + //} + addItemToVault(&this->hp, this->hp); + + //printf("AFTER DeApply Hp Regen max = %d, prev = %d, hp = %d\n",totalUpgrade.getMaxHpRegeneration(),prevMaxHpRegen,hp); + } + + if (originalHp < hp) { + this->setLastAttackerUnitId(source->getId()); + } + + if (wasAlive == true) { + //printf("DE-APPLYING ATTACK BOOST wasalive = true to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); + + //startDamageParticles + if (originalHp > this->hp) { + startDamageParticles(); + } + + //stop DamageParticles on death + if (this->hp <= 0) { + setAlive(false); + this->hp = 0; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + + stopDamageParticles(true); + + this->setLastAttackerUnitId(source->getId()); + this->setCauseOfDeath(ucodAttackBoost); + + Unit::game->getWorld()->getStats()->die(getFactionIndex(), + getType + ()->getCountUnitDeathInStats + ()); + game->getScriptManager()->onUnitDied(this); + + StaticSound *sound = + static_cast < + const DieSkillType * + >(this->getType()->getFirstStOfClass(scDie))->getSound(); + if (sound != NULL + && (this->getFactionIndex() == + Unit::game->getWorld()->getThisFactionIndex() + || (game-> + getWorld()->showWorldForPlayer(game-> + getWorld + ()->getThisTeamIndex + ()) == true))) { + SoundRenderer::getInstance().playFx(sound); + } + + if (this->isDead() && this->getCurrSkill()->getClass() != scDie) { + this->kill(); + } + } + } + + checkModelStateInfoForNewHpValue(); + + //printf("DE-APPLYING ATTACK BOOST BEFORE END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); + + for (unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; + if (effect != NULL && effect->boost == boost + && effect->getSource() == source) { + delete effect; + currentAttackBoostEffects. + erase(currentAttackBoostEffects.begin() + i); + break; + } + } + + if (SystemFlags::VERBOSE_MODE_ENABLED) + printf + ("DE-APPLIED ATTACK BOOST START to unit [%s - %d] from unit [%s - %d] hp: %d\n", + this->getType()->getName(false).c_str(), this->getId(), + source->getType()->getName(false).c_str(), source->getId(), + hp); + + //printf("DE-APPLYING ATTACK BOOST END to unit [%s - %d] from unit [%s - %d]\n",this->getType()->getName().c_str(),this->getId(),source->getType()->getName().c_str(),source->getId()); + } + + void Unit::tick() { + + if (isAlive()) { + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //if(this->getType()->getName() == "spearman") printf("Unit [%d - %s] start tick hp = %d\n",this->getId(),this->getType()->getName().c_str(),hp); + + + //regenerate hp upgrade / or boost + if (type->getTotalMaxHpRegeneration(&totalUpgrade) != 0) { + if (currSkill->getClass() != scBeBuilt) { + if (type->getTotalMaxHpRegeneration(&totalUpgrade) >= 0) { + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp += type->getTotalMaxHpRegeneration(&totalUpgrade); + if (this->hp > type->getTotalMaxHp(&totalUpgrade)) { + this->hp = type->getTotalMaxHp(&totalUpgrade); + } + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + //if(this->getType()->getName() == "spearman") printf("tick hp#2 [type->getTotalMaxHpRegeneration(&totalUpgrade)] = %d type->getTotalMaxHp(&totalUpgrade) [%d] newhp = %d\n",type->getTotalMaxHpRegeneration(&totalUpgrade),type->getTotalMaxHp(&totalUpgrade),hp); + } + // If we have negative regeneration then check if the unit should die + else { + bool decHpResult = + decHp(-type->getTotalMaxHpRegeneration(&totalUpgrade)); + if (decHpResult) { + this->setCauseOfDeath(ucodStarvedRegeneration); + + Unit::game->getWorld()->getStats()->die(getFactionIndex(), + getType + ()->getCountUnitDeathInStats + ()); + game->getScriptManager()->onUnitDied(this); + } + StaticSound *sound = + static_cast < + const DieSkillType * + >(this->getType()->getFirstStOfClass(scDie))->getSound(); + if (sound != NULL + && (this->getFactionIndex() == + Unit::game->getWorld()->getThisFactionIndex() + || (game-> + getWorld()->showWorldForPlayer(game-> + getWorld + ()->getThisTeamIndex + ()) == true))) { + SoundRenderer::getInstance().playFx(sound); + } + } + } + } + //regenerate hp + else { + if (type->getTotalMaxHpRegeneration(&totalUpgrade) >= 0) { + if (currSkill->getClass() != scBeBuilt) { + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp += type->getTotalMaxHpRegeneration(&totalUpgrade); + if (this->hp > type->getTotalMaxHp(&totalUpgrade)) { + this->hp = type->getTotalMaxHp(&totalUpgrade); + } + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + //if(this->getType()->getName() == "spearman") printf("tick hp#1 [type->getHpRegeneration()] = %d type->getTotalMaxHp(&totalUpgrade) [%d] newhp = %d\n",type->getHpRegeneration(),type->getTotalMaxHp(&totalUpgrade),hp); + } + } + // If we have negative regeneration then check if the unit should die + else { + bool decHpResult = + decHp(-type->getTotalMaxHpRegeneration(&totalUpgrade)); + if (decHpResult) { + this->setCauseOfDeath(ucodStarvedRegeneration); + + Unit::game->getWorld()->getStats()->die(getFactionIndex(), + getType + ()->getCountUnitDeathInStats + ()); + game->getScriptManager()->onUnitDied(this); + } + StaticSound *sound = + static_cast < + const DieSkillType * + >(this->getType()->getFirstStOfClass(scDie))->getSound(); + if (sound != NULL + && (this->getFactionIndex() == + Unit::game->getWorld()->getThisFactionIndex() + || (game-> + getWorld()->showWorldForPlayer(game-> + getWorld + ()->getThisTeamIndex + ()) == true))) { + SoundRenderer::getInstance().playFx(sound); + } + } + } + + //stop DamageParticles + stopDamageParticles(false); + + checkItemInVault(&this->ep, this->ep); + //regenerate ep + // ep += type->getEpRegeneration(); + // if(ep > type->getTotalMaxEp(&totalUpgrade)){ + // ep = type->getTotalMaxEp(&totalUpgrade); + // } + // addItemToVault(&this->ep,this->ep); + + if (!isBeingBuilt()) { + //regenerate ep upgrade / or boost + checkItemInVault(&this->ep, this->ep); + //regenerate ep + int original_ep = this->ep; + this->ep += type->getTotalMaxEpRegeneration(&totalUpgrade); + if (this->ep > type->getTotalMaxEp(&totalUpgrade)) { + this->ep = type->getTotalMaxEp(&totalUpgrade); + } + if (original_ep != this->ep) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_EPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->ep, this->ep); + } + } + } + + int Unit::update2() { + progress2++; + return progress2; + } + + bool Unit::computeEp() { + + if (currSkill == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: currSkill == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //if not enough ep + if (this->ep - currSkill->getEpCost() < 0) { + return true; + } + + checkItemInVault(&this->ep, this->ep); + int original_ep = this->ep; + //decrease ep + this->ep -= currSkill->getEpCost(); + if (original_ep != this->ep) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_EPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->ep, this->ep); + + if (getType() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: getType() == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (this->ep > getType()->getTotalMaxEp(&totalUpgrade)) { + int original_ep = this->ep; + this->ep = getType()->getTotalMaxEp(&totalUpgrade); + if (original_ep != this->ep) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_EPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + } + addItemToVault(&this->ep, this->ep); + + return false; + } + + bool Unit::repair() { + + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //increase hp + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + if (type->getProductionTime() + 1 == 0) { + throw + megaglest_runtime_error + ("Detected divide by 0 condition: type->getProductionTime() + 1 == 0"); + } + this->hp += getType()->getMaxHp() / type->getProductionTime() + 1; + if (this->hp > (getType()->getTotalMaxHp(&totalUpgrade))) { + this->hp = getType()->getTotalMaxHp(&totalUpgrade); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + return true; + } + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + + //stop DamageParticles + stopDamageParticles(false); + + return false; + } + + //decrements HP and returns if dead + bool Unit::decHp(int decrementValue) { + char szBuf[8096] = ""; + snprintf(szBuf, 8095, "this->hp = %d, decrementValue = %d", this->hp, + decrementValue); + addNetworkCRCDecHp(szBuf); + + if (this->hp == 0) { + return false; + } + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp -= decrementValue; + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + + if (type == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: type == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //startDamageParticles + startDamageParticles(); + + //stop DamageParticles on death + if (this->hp <= 0) { + setAlive(false); + this->hp = 0; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + + stopDamageParticles(true); + return true; + } + return false; + } + + string Unit::getDescExtension(bool translatedValue) const { + Lang & lang = Lang::getInstance(); + string str = "\n"; + + if (commands.empty() == false && commands.size() > 1) { + Commands::const_iterator it = commands.begin(); + for (unsigned int i = 0; + i < min((size_t) maxQueuedCommandDisplayCount, + commands.size()); ++i) { + const CommandType *ct = (*it)->getCommandType(); + if (i == 0) { + str += "\n" + lang.getString("OrdersOnQueue") + ": "; + } + str += + "\n#" + intToStr(i + 1) + " " + ct->getName(translatedValue); + ++it; + } + } + + return str; + } + + string Unit::getDesc(bool translatedValue) const { + + Lang & lang = Lang::getInstance(); + + //hp + string str = "\n"; + + //maxUnitCount + if (type->getMaxUnitCount() > 0) { + str += + lang.getString("MaxUnitCount") + ": " + + intToStr(faction->getCountForMaxUnitCount(type)) + "/" + + intToStr(type->getMaxUnitCount()); + } + + str += + "\n" + lang.getString("Hp") + ": " + intToStr(hp) + "/" + + intToStr(type->getTotalMaxHp(&totalUpgrade)); + if (type->getHpRegeneration() != 0 + || totalUpgrade.getMaxHpRegeneration() != 0) { + str += + " (" + lang.getString("Regeneration") + ": " + + intToStr(type->getHpRegeneration()); + if (totalUpgrade.getMaxHpRegeneration() != 0) { + str += "+" + intToStr(totalUpgrade.getMaxHpRegeneration()); + } + str += ")"; + } + + //ep + if (getType()->getMaxEp() != 0) { + str += + "\n" + lang.getString("Ep") + ": " + intToStr(ep) + "/" + + intToStr(type->getTotalMaxEp(&totalUpgrade)); + } + if (type->getEpRegeneration() != 0 + || totalUpgrade.getMaxEpRegeneration() != 0) { + str += + " (" + lang.getString("Regeneration") + ": " + + intToStr(type->getEpRegeneration()); + if (totalUpgrade.getMaxEpRegeneration() != 0) { + str += "+" + intToStr(totalUpgrade.getMaxEpRegeneration()); + } + str += ")"; + } + + //armor + str += + "\n" + lang.getString("Armor") + ": " + + intToStr(getType()->getArmor()); + if (totalUpgrade.getArmor() != 0) { + str += "+" + intToStr(totalUpgrade.getArmor()); + } + str += + " (" + getType()->getArmorType()->getName(translatedValue) + ")"; + + //sight + str += + "\n" + lang.getString("Sight") + ": " + + intToStr(getType()->getSight()); + if (totalUpgrade.getSight() != 0) { + str += "+" + intToStr(totalUpgrade.getSight()); + } + + //kills + const Level *nextLevel = getNextLevel(); + if (enemyKills > 0 || nextLevel != NULL) { + str += "\n" + lang.getString("Kills") + ": " + intToStr(enemyKills); + if (nextLevel != NULL) { + str += + " (" + nextLevel->getName(translatedValue) + ": " + + intToStr(nextLevel->getKills()) + ")"; + } + } + + //str+= "\nskl: "+scToStr(currSkill->getClass()); + + //load + if (loadCount != 0) { + str += + "\n" + lang.getString("Load") + ": " + intToStr(loadCount) + + " " + loadType->getName(translatedValue); + } + + //consumable production + for (int i = 0; i < getType()->getCostCount(); ++i) { + const Resource *r = getType()->getCost(i); + if (r->getType()->getClass() == rcConsumable) { + str += "\n"; + str += + r->getAmount() < + 0 ? lang.getString("Produce") + + ": " : lang.getString("Consume") + ": "; + str += + intToStr(abs(r->getAmount())) + " " + + r->getType()->getName(translatedValue); + } + } + + //command info + if (commands.empty() == false) { + str += + "\n" + + commands.front()->getCommandType()->getName(translatedValue); + if (commands.size() > 1) { + str += + "\n" + lang.getString("OrdersOnQueue") + ": " + + intToStr(commands.size()); + } + } else { + //can store + if (getType()->getStoredResourceCount() > 0) { + for (int i = 0; i < getType()->getStoredResourceCount(); ++i) { + const Resource *r = getType()->getStoredResource(i); + str += "\n" + lang.getString("Store") + ": "; + str += + intToStr(r->getAmount()) + " " + + r->getType()->getName(translatedValue); + } + } + } + + return str; + } + + void Unit::applyUpgrade(const UpgradeType * upgradeType) { + if (upgradeType == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: upgradeType == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (upgradeType->isAffected(type)) { + totalUpgrade.sum(upgradeType, this); + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp += upgradeType->getMaxHp(); + this->hp = max(0, this->hp); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + } + } + + void Unit::computeTotalUpgrade() { + faction->getUpgradeManager()->computeTotalUpgrade(this, + &totalUpgrade); + } + + void Unit::incKills(int team) { + ++kills; + if (team != this->getTeam()) { + ++enemyKills; + } + + checkUnitLevel(); + } + + void Unit::checkUnitLevel() { + const Level *nextLevel = getNextLevel(); + if (nextLevel != NULL && this->enemyKills >= nextLevel->getKills()) { + this->level = nextLevel; + + int maxHp = this->totalUpgrade.getMaxHp(); + totalUpgrade.incLevel(type); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_LevelChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp += totalUpgrade.getMaxHp() - maxHp; + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + } + } + + void Unit::morphAttackBoosts(Unit * unit) { + // Remove any units that were previously in range + if (currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.empty() == false + && currentAttackBoostOriginatorEffect.skillType != NULL) { + for (int i = + (int) currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.size() - 1; i >= 0; --i) { + // Remove attack boost upgrades from unit + + int findUnitId = + currentAttackBoostOriginatorEffect.currentAttackBoostUnits[i]; + Unit *affectedUnit = game->getWorld()->findUnitById(findUnitId); + if (affectedUnit != NULL + && affectedUnit->getId() == unit->getId()) { + affectedUnit->deapplyAttackBoost + (currentAttackBoostOriginatorEffect.skillType-> + getAttackBoost(), this); + + currentAttackBoostOriginatorEffect. + currentAttackBoostUnits.erase + (currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + begin() + i); + } + + //printf("- #1 DE-APPLY ATTACK BOOST from unit [%s - %d]\n",affectedUnit->getType()->getName().c_str(),affectedUnit->getId()); + } + } + } + + bool Unit::morph(const MorphCommandType * mct, int frameIndex) { + + if (mct == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: mct == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + const UnitType *morphUnitType = mct->getMorphUnit(); + + if (morphUnitType == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: morphUnitType == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + Field morphUnitField = fLand; + if (morphUnitType->getField(fAir)) { + morphUnitField = fAir; + } + if (morphUnitType->getField(fLand)) { + morphUnitField = fLand; + } + + map->clearUnitCells(this, pos, false); + if (map->canMorph(pos, this, morphUnitType)) { + map->clearUnitCells(this, pos, true); + faction->deApplyStaticCosts(type, mct); + + //printf("Now unapply attack-boost for unit [%d - %s]\n",this->getId(),this->getType()->getName().c_str()); + // De apply attack boosts for morphed unit + for (int i = (int) currentAttackBoostEffects.size() - 1; i >= 0; --i) { + UnitAttackBoostEffect *effect = currentAttackBoostEffects[i]; + if (effect != NULL) { + Unit *sourceUnit = + game->getWorld()->findUnitById(effect-> + getSource()->getId()); + if (sourceUnit == NULL) { + throw megaglest_runtime_error("sourceUnit == NULL"); + } + sourceUnit->morphAttackBoosts(this); + } + } + + //stopDamageParticles(true); + cleanupAllParticlesystems(); + + checkItemInVault(&this->hp, this->hp); + int original_hp = this->hp; + this->hp += morphUnitType->getMaxHp() - type->getMaxHp(); + if (original_hp != this->hp) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_HPChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + addItemToVault(&this->hp, this->hp); + + checkModelStateInfoForNewHpValue(); + + this->preMorph_type = this->type; + this->setType(morphUnitType); + Field original_field = this->currField; + this->currField = morphUnitField; + computeTotalUpgrade(); + map->putUnitCells(this, this->pos, false, frameIndex < 0); + + this->faction->applyDiscount(morphUnitType, mct->getDiscount()); + // add new storage + this->faction->addStore(this->type); + // remove former storage + this->faction->removeStore(this->preMorph_type); + this->faction->applyStaticProduction(morphUnitType, mct); + + this->level = NULL; + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_LevelChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + checkUnitLevel(); + + if (original_field != this->currField) { + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + game->getScriptManager()->onUnitTriggerEvent(this, + utet_FieldChanged); + //printf("File: %s line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__LINE__); + } + + return true; + } else { + return false; + } + } + + + // ==================== PRIVATE ==================== + + float Unit::computeHeight(const Vec2i & pos) const { + //printf("CRASHING FOR UNIT: %d alive = %d\n",this->getId(),this->isAlive()); + //printf("[%s]\n",this->getType()->getName().c_str()); + if (map->isInside(pos) == false + || map->isInsideSurface(map->toSurfCoords(pos)) == false) { + //printf("CRASHING FOR UNIT: %d [%s] alive = %d\n",this->getId(),this->getType()->getName().c_str(),this->isAlive()); + //abort(); + throw megaglest_runtime_error("#7 Invalid path position = " + + pos.getString()); + } + + float height = map->getCell(pos)->getHeight(); + + if (currField == fAir) { + float airHeight = game->getWorld()->getTileset()->getAirHeight(); + airHeight = truncateDecimal < float >(airHeight, 6); + + height += airHeight; + height = truncateDecimal < float >(height, 6); + + Unit *unit = map->getCell(pos)->getUnit(fLand); + if (unit != NULL && unit->getType()->getHeight() > airHeight) { + height += + (std::min((float) unit->getType()->getHeight(), + Tileset::standardAirHeight * 3) - airHeight); + height = truncateDecimal < float >(height, 6); + } else { + SurfaceCell *sc = map->getSurfaceCell(map->toSurfCoords(pos)); + if (sc != NULL && sc->getObject() != NULL + && sc->getObject()->getType() != NULL) { + if (sc->getObject()->getType()->getHeight() > airHeight) { + height += + (std::min((float) sc->getObject()->getType()->getHeight(), + Tileset::standardAirHeight * 3) - airHeight); + height = truncateDecimal < float >(height, 6); + } + } + } + } + + return height; + } + + void Unit::AnimCycleStarts() { + // we need to queue timed particles if progress starts + queueTimedParticles(currSkill->unitParticleSystemTypes); + } + + void Unit::updateTarget() { + Unit *target = targetRef.getUnit(); + if (target != NULL) { + + //update target pos + targetPos = target->getCellPos(); + Vec2i relPos = targetPos - pos; + Vec2f relPosf = Vec2f((float) relPos.x, (float) relPos.y); +#ifdef USE_STREFLOP + targetRotation = + radToDeg(streflop::atan2 + (static_cast (relPosf.x), + static_cast (relPosf.y))); +#else + targetRotation = radToDeg(atan2(relPosf.x, relPosf.y)); +#endif + targetVec = target->getCurrVectorAsTarget(); + } + } + + void Unit::clearCommands() { + + this->setCurrentUnitTitle(""); + this->unitPath->clear(); + while (commands.empty() == false) { + undoCommand(commands.back()); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + + delete commands.back(); + commands.pop_back(); + + safeMutex.ReleaseLock(); + } + changedActiveCommand = false; + } + + void Unit::deleteQueuedCommand(Command * command) { + if (getCurrCommand() == command) { + this->setCurrentUnitTitle(""); + this->unitPath->clear(); + } + undoCommand(command); + delete command; + } + + + std::pair < CommandResult, string > Unit::checkCommand(Command * command) const { + std::pair < CommandResult, string > result(crSuccess, ""); + + if (command == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //if not operative or has not command type => fail + if (isOperative() == false || + command->getUnit() == this || + getType()->hasCommandType(command->getCommandType()) == false || + (ignoreCheckCommand == false + && this->getFaction()->reqsOk(command->getCommandType()) == + false)) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d] isOperative() = %d, command->getUnit() = %p, getType()->hasCommandType(command->getCommandType()) = %d, this->getFaction()->reqsOk(command->getCommandType()) = %d\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, isOperative(), + command->getUnit(), + getType()-> + hasCommandType(command->getCommandType + ()), + this->getFaction()-> + reqsOk(command->getCommandType())); + + // Allow self healing if able to heal own unit type + if (command->getUnit() == this && + command->getCommandType()->getClass() == ccRepair && + this->getType()->getFirstRepairCommand(this->getType()) != + NULL) { + + } else { + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + + result.first = crFailUndefined; + return result; + } + } + + //if pos is not inside the world (if comand has not a pos, pos is (0, 0) and is inside world + if (map->isInside(command->getPos()) == false) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + + result.first = crFailUndefined; + return result; + } + + //check produced + if (command->getCommandType() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + const ProducibleType *produced = + command->getCommandType()->getProduced(); + if (produced != NULL) { + if (ignoreCheckCommand == false + && faction->reqsOk(produced) == false) { + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + //printf("To produce this unit you need:\n%s\n",produced->getUnitAndUpgradeReqDesc().c_str()); + result.first = crFailReqs; + + Lang & lang = Lang::getInstance(); + result.second = + " - " + lang.getString("Reqs") + " : " + + produced->getUnitAndUpgradeReqDesc(false, + this->showTranslatedTechTree + ()); + return result; + } + + if (ignoreCheckCommand == false && + faction->checkCosts(produced, + command->getCommandType()) == false) { + //printf("To produce this unit you need:\n%s\n",produced->getResourceReqDesc().c_str()); + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + result.first = crFailRes; + Lang & lang = Lang::getInstance(); + result.second = + " - " + lang.getString("Reqs") + " : " + + produced->getResourceReqDesc(false, + this->showTranslatedTechTree()); + return result; + } + } + + //build command specific, check resources and requirements for building + if (command->getCommandType()->getClass() == ccBuild) { + const UnitType *builtUnit = command->getUnitType(); + + if (builtUnit == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: builtUnit == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (faction->reqsOk(builtUnit) == false) { + //printf("To build this unit you need:\n%s\n",builtUnit->getUnitAndUpgradeReqDesc().c_str()); + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + result.first = crFailReqs; + Lang & lang = Lang::getInstance(); + result.second = + " - " + lang.getString("Reqs") + " : " + + builtUnit->getUnitAndUpgradeReqDesc(false, + this->showTranslatedTechTree + ()); + return result; + } + if (faction->checkCosts(builtUnit, NULL) == false) { + //printf("To build this unit you need:\n%s\n",builtUnit->getResourceReqDesc().c_str()); + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + result.first = crFailRes; + Lang & lang = Lang::getInstance(); + result.second = + " - " + lang.getString("Reqs") + " : " + + builtUnit->getResourceReqDesc(false, + this->showTranslatedTechTree()); + return result; + } + } + //upgrade command specific, check that upgrade is not upgraded + else if (command->getCommandType()->getClass() == ccUpgrade) { + const UpgradeCommandType *uct = + static_cast < + const UpgradeCommandType *>(command->getCommandType()); + + if (uct == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (faction-> + getUpgradeManager()->isUpgradingOrUpgraded(uct-> + getProducedUpgrade + ())) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugLUA).enabled) + SystemFlags::OutputDebug(SystemFlags::debugLUA, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__); + //printf("In [%s::%s Line: %d] command = %p\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,command); + result.first = crFailUndefined; + return result; + } + } + + return result; + } + + void Unit::applyCommand(Command * command) { + if (command == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } else if (command->getCommandType() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + //check produced + const ProducibleType *produced = + command->getCommandType()->getProduced(); + if (produced != NULL) { + faction->applyCosts(produced, command->getCommandType()); + } + + //build command specific + if (command->getCommandType()->getClass() == ccBuild) { + faction->applyCosts(command->getUnitType(), + command->getCommandType()); + } + //upgrade command specific + else if (command->getCommandType()->getClass() == ccUpgrade) { + const UpgradeCommandType *uct = + static_cast < + const UpgradeCommandType *>(command->getCommandType()); + + if (uct == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + faction->startUpgrade(uct->getProducedUpgrade()); + } + } + + CommandResult Unit::undoCommand(Command * command) { + + if (command == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } else if (command->getCommandType() == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: command->getCommandType() == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + if (getCurrCommand() == command + && command->getCommandType()->getClass() == ccMorph + && this->currSkill->getClass() == scMorph) { + // clear cells of morphed unit and set those of current unit! + map->clearUnitCells(this, this->getPos()); + map->putUnitCells(this, this->getPos(), true); + } + //return cost + const ProducibleType *produced = + command->getCommandType()->getProduced(); + if (produced != NULL) { + faction->deApplyCosts(produced, command->getCommandType()); + } + + //return building cost if not already building it or dead + if (command->getCommandType()->getClass() == ccBuild) { + if (currSkill->getClass() != scBuild + && currSkill->getClass() != scDie) { + faction->deApplyCosts(command->getUnitType(), + command->getCommandType()); + } + } + + //upgrade command cancel from list + if (command->getCommandType()->getClass() == ccUpgrade) { + const UpgradeCommandType *uct = + static_cast < + const UpgradeCommandType *>(command->getCommandType()); + if (uct == NULL) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "In [%s::%s Line: %d] ERROR: uct == NULL, Unit = [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, this->toString().c_str()); + throw megaglest_runtime_error(szBuf); + } + + faction->cancelUpgrade(uct->getProducedUpgrade()); + } + + retryCurrCommandCount = 0; + this->setCurrentUnitTitle(""); + + return crSuccess; + } + + void Unit::stopDamageParticles(bool force) { + if (force == true || (hp > type->getTotalMaxHp(&totalUpgrade) / 2)) { + //printf("Checking to stop damageparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); + + if (Renderer:: + getInstance().validateParticleSystemStillExists(this->fire, + rsGame) == + false) { + this->fire = NULL; + } + + // stop fire + if (this->fire != NULL) { + this->fire->fade(); + this->fire = NULL; + } + // stop additional particles + + + if (smokeParticleSystems.empty() == false) { + //printf("Checking to stop smokeparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); + + for (int i = (int) smokeParticleSystems.size() - 1; i >= 0; --i) { + UnitParticleSystem *ps = smokeParticleSystems[i]; + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + ps->fade(); + } + smokeParticleSystems.pop_back(); + } + } + + if (damageParticleSystems.empty() == false) { + //printf("Checking to stop damageparticles for unit [%s - %d] hp = %d\n",this->getType()->getName().c_str(),this->getId(),hp); + + for (int i = (int) damageParticleSystems.size() - 1; i >= 0; --i) { + UnitParticleSystem *ps = damageParticleSystems[i]; + UnitParticleSystemType *pst = NULL; + int foundParticleIndexType = -2; + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + for (std::map < int, UnitParticleSystem * >::iterator iterMap = + damageParticleSystemsInUse.begin(); + iterMap != damageParticleSystemsInUse.end(); ++iterMap) { + if (iterMap->second == ps) { + foundParticleIndexType = iterMap->first; + if (foundParticleIndexType >= 0) { + pst = + type->damageParticleSystemTypes[foundParticleIndexType]; + break; + } + } + } + } + if (force == true + || (pst != NULL && pst->getMinmaxEnabled() == false)) { + damageParticleSystemsInUse.erase(foundParticleIndexType); + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + ps->fade(); + } + damageParticleSystems.pop_back(); + } + } + } + } + + checkCustomizedParticleTriggers(force); + } + + void Unit:: + checkCustomizedUnitParticleListTriggers(const UnitParticleSystemTypes & + unitParticleSystemTypesList, + bool applySkillChangeParticles) { + if (showUnitParticles == true) { + vector < ParticleSystemTypeInterface * >systemTypesInUse; + + if (unitParticleSystems.empty() == false) { + for (int index = (int) unitParticleSystems.size() - 1; index >= 0; + index--) { + UnitParticleSystem *ps = unitParticleSystems[index]; + if (ps != NULL) { + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + + bool stopParticle = false; + if ((ps->getParticleType() != NULL && + ps->getParticleType()->getMinmaxEnabled())) { + + if (ps->getParticleType() != NULL) { + if (ps->getParticleType()->getMinmaxIsPercent() == + false) { + if (hp < ps->getParticleType()->getMinHp() + || hp > ps->getParticleType()->getMaxHp()) { + stopParticle = true; + + //printf("STOP Particle line: %d\n",__LINE__); + } + } else { + int hpPercent = + (hp / type->getTotalMaxHp(&totalUpgrade) * 100); + if (hpPercent < ps->getParticleType()->getMinHp() + || hpPercent > ps->getParticleType()->getMaxHp()) { + stopParticle = true; + + //printf("STOP Particle line: %d\n",__LINE__); + } + } + } + + if (stopParticle == true) { + ps->fade(); + unitParticleSystems.erase(unitParticleSystems.begin() + + index); + } + } + + if (ps->getParticleType() != NULL && stopParticle == false) { + systemTypesInUse.push_back(ps->getParticleType()); + } + } + } + } + } + + //printf("Check Particle start line: %d size: %d\n",__LINE__,(int)unitParticleSystemTypesList.size()); + + if (unitParticleSystemTypesList.empty() == false) { + + //for(unsigned int index = 0; index < unitParticleSystemTypesList.size(); ++index) { + for (UnitParticleSystemTypes::const_iterator iterParticleType = + unitParticleSystemTypesList.begin(); + iterParticleType != unitParticleSystemTypesList.end(); + ++iterParticleType) { + UnitParticleSystemType *pst = *iterParticleType; + + vector < ParticleSystemTypeInterface * >::iterator iterFind = + std::find(systemTypesInUse.begin(), systemTypesInUse.end(), + pst); + + //printf("Check Particle line: %d isenabled: %d already in use: %d\n",__LINE__,pst->getMinmaxEnabled(),(iterFind == systemTypesInUse.end())); + + bool showParticle = applySkillChangeParticles; + if (pst->getMinmaxEnabled() == true) { + + //printf("Check Particle line: %d isenabled: %d already in use: %d\n",__LINE__,pst->getMinmaxEnabled(),(iterFind != systemTypesInUse.end())); + + showParticle = false; + if (iterFind == systemTypesInUse.end()) { + if (pst->getMinmaxIsPercent() == false) { + if (hp >= pst->getMinHp() && hp <= pst->getMaxHp()) { + showParticle = true; + + //printf("START Particle line: %d\n",__LINE__); + } + } else { + int hpPercent = + (hp / type->getTotalMaxHp(&totalUpgrade) * 100); + if (hpPercent >= pst->getMinHp() + && hpPercent <= pst->getMaxHp()) { + showParticle = true; + + //printf("START Particle line: %d\n",__LINE__); + } + } + } + } + if (showParticle == true) { + if (pst->getStartTime() == 0.0) { + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType(pst); + + pst->setValues(ups); + ups->setPos(getCurrVectorForParticlesystems()); + ups->setRotation(getRotation()); + setMeshPosInParticleSystem(ups); + if (getFaction()->getTexture()) { + ups->setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + unitParticleSystems.push_back(ups); + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } else { + // do nothing, timed particles are handled below in queueTimedParticles() + } + } + } + } + } + } + + void Unit:: + queueTimedParticles(const UnitParticleSystemTypes & + unitParticleSystemTypesList) { + if (showUnitParticles == true) { + for (UnitParticleSystemTypes::const_iterator iterParticleType = + unitParticleSystemTypesList.begin(); + iterParticleType != unitParticleSystemTypesList.end(); + ++iterParticleType) { + UnitParticleSystemType *pst = *iterParticleType; + if (pst->getMinmaxEnabled() == false) { + if (pst->getStartTime() != 0.0) { + queuedUnitParticleSystemTypes.push_back(pst); + } + } + } + } + } + + void Unit::setMeshPosInParticleSystem(UnitParticleSystem * ups) { + if (ups->getMeshName() != "") { + string meshName = ups->getMeshName(); + Model *model = getCurrentModelPtr(); + + // as it can happen that anim progress is a bit out of range we correct it to get something valid for the particle positions. + float currentAnimProgress = getAnimProgressAsFloat(); + if (currentAnimProgress > 1.f || currentAnimProgress < 0.f) { + currentAnimProgress = 0.f; + } + model->updateInterpolationVertices(currentAnimProgress, isAlive() + && !isAnimProgressBound()); + + bool foundMesh = false; + for (unsigned int i = 0; i < model->getMeshCount(); i++) { + //printf("meshName=%s\n",unitModel->getMesh(i)->getName().c_str()); + if (model->getMesh(i)->getName() == meshName) { + const InterpolationData *data = + model->getMesh(i)->getInterpolationData(); + const Vec3f *verticepos = data->getVertices(); + ups->setMeshPos(Vec3f + (verticepos->x, verticepos->y, verticepos->z)); + foundMesh = true; + break; + } + } + if (foundMesh == false) { + string meshesFound = model->getMesh(0)->getName(); + for (unsigned i = 1; i < model->getMeshCount(); i++) { + meshesFound += ", " + model->getMesh(i)->getName(); + } + + string errorString = + "Warning: Particle system is trying to find mesh '" + meshName + + "', but just found:\n'" + meshesFound + "' in file:\n'" + + model->getFileName() + "'\n"; + //throw megaglest_runtime_error(errorString); + printf("%s", errorString.c_str()); + } + } + } + + void Unit::checkCustomizedUnitParticleTriggers() { + if (currSkill != NULL) { + checkCustomizedUnitParticleListTriggers + (currSkill->unitParticleSystemTypes, false); + } + } + + void Unit::checkCustomizedParticleTriggers(bool force) { + // + // Now check if we have special pre-exisitng hp triggered particles and + // end those that should no longer display + // + // end s particles + if (damageParticleSystems.empty() == false) { + for (int i = (int) damageParticleSystems.size() - 1; i >= 0; --i) { + UnitParticleSystem *ps = damageParticleSystems[i]; + UnitParticleSystemType *pst = NULL; + int foundParticleIndexType = -2; + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + for (std::map < int, UnitParticleSystem * >::iterator iterMap = + damageParticleSystemsInUse.begin(); + iterMap != damageParticleSystemsInUse.end(); ++iterMap) { + if (iterMap->second == ps) { + foundParticleIndexType = iterMap->first; + if (foundParticleIndexType >= 0) { + pst = + type->damageParticleSystemTypes[foundParticleIndexType]; + break; + } + } + } + } + + if (force == true + || (pst != NULL && pst->getMinmaxEnabled() == true)) { + bool stopParticle = force; + if (force == false) { + if (pst->getMinmaxIsPercent() == false) { + if (hp < pst->getMinHp() || hp > pst->getMaxHp()) { + stopParticle = true; + } + } else { + int hpPercent = + (hp / type->getTotalMaxHp(&totalUpgrade) * 100); + if (hpPercent < pst->getMinHp() + || hpPercent > pst->getMaxHp()) { + stopParticle = true; + } + } + } + + //printf("CHECKING to STOP customized particle trigger by HP [%d to %d percentbased = %d] current hp = %d stopParticle = %d\n",pst->getMinHp(),pst->getMaxHp(),pst->getMinmaxIsPercent(),hp,stopParticle); + + if (stopParticle == true) { + //printf("STOPPING customized particle trigger by HP [%d to %d] current hp = %d\n",pst->getMinHp(),pst->getMaxHp(),hp); + + damageParticleSystemsInUse.erase(foundParticleIndexType); + if (Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + ps->fade(); + } + damageParticleSystems.pop_back(); + } + } + } + } + + // + // Now check if we have new special hp triggered particles to display + // + //start additional particles + if (showUnitParticles && + type->damageParticleSystemTypes.empty() == false && + force == false && alive == true) { + for (unsigned int i = 0; i < type->damageParticleSystemTypes.size(); + ++i) { + UnitParticleSystemType *pst = type->damageParticleSystemTypes[i]; + + if (pst->getMinmaxEnabled() == true + && damageParticleSystemsInUse.find(i) == + damageParticleSystemsInUse.end()) { + bool showParticle = false; + if (pst->getMinmaxIsPercent() == false) { + if (hp >= pst->getMinHp() && hp <= pst->getMaxHp()) { + showParticle = true; + } + } else { + int hpPercent = + (hp / type->getTotalMaxHp(&totalUpgrade) * 100); + if (hpPercent >= pst->getMinHp() + && hpPercent <= pst->getMaxHp()) { + showParticle = true; + } + } + + //printf("CHECKING to START customized particle trigger by HP [%d to %d percentbased = %d] current hp = %d showParticle = %d\n",pst->getMinHp(),pst->getMaxHp(),pst->getMinmaxIsPercent(),hp,showParticle); + + if (showParticle == true) { + //printf("STARTING customized particle trigger by HP [%d to %d] current hp = %d\n",pst->getMinHp(),pst->getMaxHp(),hp); + + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType(pst); + + pst->setValues(ups); + ups->setPos(getCurrVectorForParticlesystems()); + ups->setRotation(getRotation()); + setMeshPosInParticleSystem(ups); + if (getFaction()->getTexture()) { + ups->setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + damageParticleSystems.push_back(ups); + damageParticleSystemsInUse[i] = ups; + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } + } + } + } + + checkCustomizedUnitParticleTriggers(); + } + + void Unit::startDamageParticles() { + if (hp < type->getTotalMaxHp(&totalUpgrade) / 2 && hp > 0 + && alive == true) { + //start additional particles + if (showUnitParticles && + type->damageParticleSystemTypes.empty() == false) { + for (unsigned int i = 0; + i < type->damageParticleSystemTypes.size(); ++i) { + UnitParticleSystemType *pst = type->damageParticleSystemTypes[i]; + + if (pst->getMinmaxEnabled() == false + && damageParticleSystemsInUse.find(i) == + damageParticleSystemsInUse.end()) { + UnitParticleSystem *ups = new UnitParticleSystem(200); + ups->setParticleOwner(this); + ups->setParticleType(pst); + + pst->setValues(ups); + ups->setPos(getCurrVectorForParticlesystems()); + ups->setRotation(getRotation()); + setMeshPosInParticleSystem(ups); + if (getFaction()->getTexture()) { + ups->setFactionColor(getFaction()-> + getTexture()->getPixmapConst()-> + getPixel3f(0, 0)); + } + damageParticleSystems.push_back(ups); + damageParticleSystemsInUse[i] = ups; + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } + } + } + + // start fire + if (type->getProperty(UnitType::pBurnable) && this->fire == NULL) { + FireParticleSystem *fps = new FireParticleSystem(200); + fps->setParticleOwner(this); + const Game *game = Renderer::getInstance().getGame(); + fps->setSpeed(2.5f / + game->getWorld()-> + getUpdateFps(this->getFactionIndex())); + fps->setPos(getCurrBurnVector()); + fps->setRadius(type->getSize() / 3.f); + fps->setTexture(CoreData::getInstance().getFireTexture()); + fps->setParticleSize(type->getSize() / 3.f); + this->fire = fps; + fireParticleSystems.push_back(fps); + + Renderer::getInstance().manageParticleSystem(fps, rsGame); + if (showUnitParticles == true) { + // smoke + UnitParticleSystem *ups = new UnitParticleSystem(400); + ups->setParticleOwner(this); + ups->setColorNoEnergy(Vec4f(0.0f, 0.0f, 0.0f, 0.13f)); + ups->setColor(Vec4f(0.115f, 0.115f, 0.115f, 0.22f)); + ups->setPos(getCurrBurnVector()); + ups->setRotation(getRotation()); + setMeshPosInParticleSystem(ups); + ups->setBlendMode(ups->strToBlendMode("black")); + ups->setOffset(Vec3f(0, 2, 0)); + ups->setDirection(Vec3f(0, 1, -0.2f)); + ups->setRadius(type->getSize() / 3.f); + ups->setShape(::Shared::Graphics::UnitParticleSystem::sLinear); + ups->setTexture(CoreData::getInstance().getFireTexture()); + const Game *game = Renderer::getInstance().getGame(); + ups->setSpeed(2.0f / + game->getWorld()-> + getUpdateFps(this->getFactionIndex())); + ups->setGravity(0.0004f); + ups->setEmissionRate(1); + ups->setMaxParticleEnergy(150); + ups->setSizeNoEnergy(type->getSize() * 0.6f); + ups->setParticleSize(type->getSize() * 0.8f); + smokeParticleSystems.push_back(ups); + //damageParticleSystemsInUse[-1] = ups; + Renderer::getInstance().manageParticleSystem(ups, rsGame); + } + } + } + + checkCustomizedParticleTriggers(false); + } + + void Unit::setMeetingPos(const Vec2i & meetingPos) { + this->meetingPos = meetingPos; + map->clampPos(this->meetingPos); + + if (map->isInside(this->meetingPos) == false + || map->isInsideSurface(map->toSurfCoords(this->meetingPos)) == + false) { + throw megaglest_runtime_error("#8 Invalid path position = " + + this->meetingPos.getString()); + } + + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__); + } + + bool Unit::isMeetingPointSettable() const { + return (type != NULL ? type->getMeetingPoint() : false); + } + + uint32 Unit::getFrameCount() const { + uint32 frameCount = 0; + //const Game *game = Renderer::getInstance().getGame(); + if (game != NULL && game->getWorld() != NULL) { + int frameCountAsInt = game->getWorld()->getFrameCount(); + if (frameCountAsInt >= 0) { + frameCount = frameCountAsInt; + } + } + + return frameCount; + } + + void Unit::exploreCells(bool forceRefresh) { + if (this->isOperative() == true) { + const Vec2i & newPos = this->getCenteredPos(); + int sightRange = + this->getType()->getTotalSight(this->getTotalUpgrade()); + int teamIndex = this->getTeam(); + + if (game == NULL) { + throw megaglest_runtime_error("game == NULL"); + } else if (game->getWorld() == NULL) { + throw megaglest_runtime_error("game->getWorld() == NULL"); + } + + // Try the local unit exploration cache + if (!forceRefresh && + cacheExploredCellsKey.first == newPos && + cacheExploredCellsKey.second == sightRange) { + game->getWorld()->exploreCells(teamIndex, cacheExploredCells); + } else { + // Try the world exploration scan or possible cache + cacheExploredCells = + game->getWorld()->exploreCells(newPos, sightRange, teamIndex, + this); + + // Cache the result for this unit + cacheExploredCellsKey.first = newPos; + cacheExploredCellsKey.second = sightRange; + } + } + } + + void Unit::logSynchData(string file, int line, string source) { + logSynchDataCommon(file, line, source, false); + } + void Unit::logSynchDataThreaded(string file, int line, string source) { + logSynchDataCommon(file, line, source, true); + } + void Unit::logSynchDataCommon(string file, int line, string source, + bool threadedMode) { + if (SystemFlags:: + getSystemSettingType(SystemFlags::debugWorldSynch).enabled == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "FrameCount [%d] Unit = %d [%s][%s] pos = %s, lastPos = %s, targetPos = %s, targetVec = %s, meetingPos = %s, progress [" + MG_I64_SPECIFIER + "], progress2 [%d] random [%d]\nUnit Path [%s]\n", + getFrameCount(), id, getFullName(false).c_str(), + faction->getType()->getName(false).c_str(), + //getDesc().c_str(), + pos.getString().c_str(), + lastPos.getString().c_str(), + targetPos.getString().c_str(), + targetVec.getString().c_str(), + meetingPos.getString().c_str(), + // lastRotation, + // targetRotation, + // rotation, + progress, + progress2, + random.getLastNumber(), + (unitPath != + NULL ? unitPath->toString().c_str() : "NULL")); + + if (lastSynchDataString != string(szBuf) || + lastFile != file || lastLine != line || lastSource != source) { + lastSynchDataString = string(szBuf); + lastFile = file; + lastSource = source; + + char szBufDataText[8096] = ""; + snprintf(szBufDataText, 8096, + "----------------------------------- START [FRAME %d UNIT: %d - %s] ------------------------------------------------\n", + getFrameCount(), this->id, + this->getType()->getName(false).c_str()); + string logDataText = szBufDataText; + + snprintf(szBufDataText, 8096, "[%s::%d]\n", + extractFileFromDirectoryPath(file).c_str(), line); + logDataText += szBufDataText; + + if (source != "") { + snprintf(szBufDataText, 8096, "%s ", source.c_str()); + logDataText += szBufDataText; + } + snprintf(szBufDataText, 8096, "%s\n", szBuf); + logDataText += szBufDataText; + snprintf(szBufDataText, 8096, + "------------------------------------ END [FRAME %d UNIT: %d - %s] ------------------------------------------------\n", + getFrameCount(), this->id, + this->getType()->getName(false).c_str()); + logDataText += szBufDataText; + /* + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"----------------------------------- START [FRAME %d UNIT: %d - %s] ------------------------------------------------\n",getFrameCount(),this->id,this->getType()->getName().c_str()); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"[%s::%d]\n",extractFileFromDirectoryPath(file).c_str(),line); + if(source != "") { + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s ",source.c_str()); + } + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"%s\n",szBuf); + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch,"------------------------------------ END [FRAME %d UNIT: %d - %s] ------------------------------------------------\n",getFrameCount(),this->id,this->getType()->getName().c_str()); + */ + if (threadedMode == false) { + SystemFlags::OutputDebug(SystemFlags::debugWorldSynch, "%s", + logDataText.c_str()); + } else { + this->faction->addWorldSynchThreadedLogList(logDataText); + } + } + } + } + + void Unit::addBadHarvestPos(const Vec2i & value) { + //Chrono chron; + //chron.start(); + badHarvestPosList[value] = getFrameCount(); + cleanupOldBadHarvestPos(); + } + + //void Unit::removeBadHarvestPos(const Vec2i &value) { + // std::map::iterator iter = badHarvestPosList.find(value); + // if(iter != badHarvestPosList.end()) { + // badHarvestPosList.erase(value); + // } + // cleanupOldBadHarvestPos(); + //} + + void Unit::cleanupOldBadHarvestPos() { + const unsigned int cleanupInterval = (GameConstants::updateFps * 5); + bool needToCleanup = (getFrameCount() % cleanupInterval == 0); + + //printf("========================> cleanupOldBadHarvestPos() [%d] badHarvestPosList.size [%ld] cleanupInterval [%d] getFrameCount() [%d] needToCleanup [%d]\n",getFrameCount(),badHarvestPosList.size(),cleanupInterval,getFrameCount(),needToCleanup); + + if (needToCleanup == true) { + //printf("========================> cleanupOldBadHarvestPos() [%d] badHarvestPosList.size [%ld]\n",getFrameCount(),badHarvestPosList.size()); + + std::vector < Vec2i > purgeList; + for (std::map < Vec2i, int >::iterator iter = + badHarvestPosList.begin(); iter != badHarvestPosList.end(); + ++iter) { + if (getFrameCount() - iter->second >= cleanupInterval) { + //printf("cleanupOldBadHarvestPos() [%d][%d]\n",getFrameCount(),iter->second); + purgeList.push_back(iter->first); + } + } + + if (purgeList.empty() == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "[cleaning old bad harvest targets] purgeList.size() [" + MG_SIZE_T_SPECIFIER "]", purgeList.size()); + logSynchData(extractFileFromDirectoryPath(__FILE__).c_str(), + __LINE__, szBuf); + + for (int i = 0; i < (int) purgeList.size(); ++i) { + const Vec2i & item = purgeList[i]; + badHarvestPosList.erase(item); + } + } + } + } + + void Unit::setLastHarvestResourceTarget(const Vec2i * pos) { + if (pos == NULL) { + lastHarvestResourceTarget.first = Vec2i(0); + //lastHarvestResourceTarget.second = 0; + } else { + const Vec2i resourceLocation = *pos; + if (resourceLocation != lastHarvestResourceTarget.first) { + lastHarvestResourceTarget.first = resourceLocation; + + //Chrono chron; + //chron.start(); + lastHarvestResourceTarget.second = getFrameCount(); + } else { + // If we cannot harvest for > 10 seconds tag the position + // as a bad one + const unsigned int addInterval = (GameConstants::updateFps * 5); + if (lastHarvestResourceTarget.second - getFrameCount() >= + addInterval) { + //printf("-----------------------> setLastHarvestResourceTarget() [%d][%d]\n",getFrameCount(),lastHarvestResourceTarget.second); + addBadHarvestPos(resourceLocation); + } + } + } + } + + //void Unit::addCurrentTargetPathTakenCell(const Vec2i &target,const Vec2i &cell) { + // if(currentTargetPathTaken.first != target) { + // currentTargetPathTaken.second.clear(); + // } + // currentTargetPathTaken.first = target; + // currentTargetPathTaken.second.push_back(cell); + //} + + void Unit::setLastPathfindFailedFrameToCurrentFrame() { + lastPathfindFailedFrame = getFrameCount(); + } + + bool Unit::isLastPathfindFailedFrameWithinCurrentFrameTolerance() const { + //static const bool enablePathfinderEnlargeMaxNodes = Config::getInstance().getBool("EnablePathfinderEnlargeMaxNodes","false"); + static const bool enablePathfinderEnlargeMaxNodes = false; + bool result = enablePathfinderEnlargeMaxNodes; + if (enablePathfinderEnlargeMaxNodes) { + const uint32 MIN_FRAME_ELAPSED_RETRY = 960; + result = + (getFrameCount() - lastPathfindFailedFrame >= + MIN_FRAME_ELAPSED_RETRY); + } + return result; + } + + void Unit::setLastStuckFrameToCurrentFrame() { + lastStuckFrame = getFrameCount(); + } + + bool Unit::isLastStuckFrameWithinCurrentFrameTolerance(bool evalMode) { + //const int MIN_FRAME_ELAPSED_RETRY = 300; + const uint32 MAX_BLOCKED_FRAME_THRESHOLD = 25000; + uint32 MIN_FRAME_ELAPSED_RETRY = 6; + if (lastStuckFrame < MAX_BLOCKED_FRAME_THRESHOLD) { + if (evalMode == true) { + MIN_FRAME_ELAPSED_RETRY = 4; + } else { + MIN_FRAME_ELAPSED_RETRY = + random.randRange(2, 6, intToStr(__LINE__)); + } + } else { + if (evalMode == true) { + MIN_FRAME_ELAPSED_RETRY = 7; + } else { + MIN_FRAME_ELAPSED_RETRY = + random.randRange(6, 8, intToStr(__LINE__)); + } + } + bool result(getFrameCount() - lastStuckFrame <= + (MIN_FRAME_ELAPSED_RETRY * 100)); + return result; + } + + Vec2i Unit::getPosWithCellMapSet() const { + Vec2i cellMapPos = + this->getType()->getFirstOccupiedCellInCellMap(pos); + return cellMapPos; + } + + string Unit::getUniquePickName() const { + string result = intToStr(id) + " - " + type->getName(false) + " : "; + result += pos.getString(); + return result; + } + + Vec2i Unit::getPos() { + Vec2i result; + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(mutexCommands, mutexOwnerId); + result = this->pos; + safeMutex.ReleaseLock(); + + return result; + } + + void Unit::clearCaches() { + cachedFow.surfPosAlphaList.clear(); + cachedFowPos = Vec2i(0, 0); + + cacheExploredCells.exploredCellList.clear(); + cacheExploredCells.visibleCellList.clear(); + cacheExploredCellsKey.first = Vec2i(-1, -1); + cacheExploredCellsKey.second = -1; + + if (unitPath != NULL) { + unitPath->clearCaches(); + } + + lastHarvestedResourcePos = Vec2i(0, 0); + } + + bool Unit::showTranslatedTechTree() const { + return (this->game != + NULL ? this->game->showTranslatedTechTree() : true); + } + + string Unit::getNetworkCRCDecHpList() const { + string result = ""; + if (networkCRCDecHpList.empty() == false) { + for (unsigned int index = 0; index < networkCRCDecHpList.size(); + ++index) { + result += networkCRCDecHpList[index] + " "; + } + } + return result; + } + std::string Unit::toString(bool crcMode) const { + std::string result = ""; + + result += "id = " + intToStr(this->id); + if (this->type != NULL) { + result += + " name [" + this->type->getName(false) + "][" + + intToStr(this->type->getId()) + "]"; + } + + if (this->faction != NULL) { + result += + "\nFactionIndex = " + intToStr(this->faction->getIndex()) + "\n"; + result += + "teamIndex = " + intToStr(this->faction->getTeam()) + "\n"; + result += + "startLocationIndex = " + + intToStr(this->faction->getStartLocationIndex()) + "\n"; + if (crcMode == false) { + result += + "thisFaction = " + intToStr(this->faction->getThisFaction()) + + "\n"; + result += + "control = " + intToStr(this->faction->getControlType()) + "\n"; + } + if (this->faction->getType() != NULL) { + result += + "factionName = " + this->faction->getType()->getName(false) + + "\n"; + } + } + + result += " hp = " + intToStr(this->hp); + result += " ep = " + intToStr(this->ep); + result += " loadCount = " + intToStr(this->loadCount); + result += " deadCount = " + intToStr(this->deadCount); + result += " progress = " + intToStr(this->progress); + result += "\n"; + result += "networkCRCLogInfo = " + networkCRCLogInfo; + result += "\n"; + if (crcMode == false) { + result += " lastAnimProgress = " + intToStr(this->lastAnimProgress); + result += " animProgress = " + intToStr(this->animProgress); + result += " highlight = " + floatToStr(this->highlight, 6); + } + result += " progress2 = " + intToStr(this->progress2); + result += " kills = " + intToStr(this->kills); + result += " enemyKills = " + intToStr(this->enemyKills); + result += "\n"; + + // WARNING!!! Don't access the Unit pointer in this->targetRef in this method or it causes + // a stack overflow + if (this->targetRef.getUnitId() >= 0) { + //result += " targetRef = " + this->targetRef.getUnit()->toString(); + result += + " targetRef = " + intToStr(this->targetRef.getUnitId()) + + " - factionIndex = " + + intToStr(this->targetRef.getUnitFaction()->getIndex()); + } + + result += " currField = " + intToStr(this->currField); + result += " targetField = " + intToStr(this->targetField); + if (level != NULL) { + result += " level = " + level->getName(); + } + result += "\n"; + result += " pos = " + pos.getString(); + result += " lastPos = " + lastPos.getString(); + result += "\n"; + result += " targetPos = " + targetPos.getString(); + result += " targetVec = " + targetVec.getString(); + result += " meetingPos = " + meetingPos.getString(); + result += "\n"; + + if (crcMode == false) { + result += " lastRotation = " + floatToStr(this->lastRotation, 6); + result += " targetRotation = " + floatToStr(this->targetRotation, 6); + result += " rotation = " + floatToStr(this->rotation, 6); + } + + if (loadType != NULL) { + result += " loadType = " + loadType->getName(); + } + + if (currSkill != NULL) { + result += " currSkill = " + currSkill->getName(); + } + result += "\n"; + + result += " toBeUndertaken = " + intToStr(this->toBeUndertaken); + result += " alive = " + intToStr(this->alive); + result += " showUnitParticles = " + intToStr(this->showUnitParticles); + + result += " totalUpgrade = " + totalUpgrade.toString(); + result += " " + this->unitPath->toString() + "\n"; + result += "\n"; + + result += "Command count = " + intToStr(commands.size()) + "\n"; + + int cmdIdx = 0; + for (Commands::const_iterator iterList = commands.begin(); + iterList != commands.end(); ++iterList) { + result += " index = " + intToStr(cmdIdx) + " "; + const Command *cmd = *iterList; + if (cmd != NULL) { + result += cmd->toString(false) + "\n"; + } + cmdIdx++; + } + result += "\n"; + + // int obsIdx = 0; + // for(Observers::const_iterator iterList = observers.begin(); iterList != observers.end(); ++iterList) { + // const UnitObserver *observer = *iterList; + // if(observer != NULL) { + // } + // + // obsIdx++; + // } + + result += "\n"; + + result += "modelFacing = " + intToStr(modelFacing.asInt()) + "\n"; + + result += + "retryCurrCommandCount = " + intToStr(retryCurrCommandCount) + "\n"; + + result += "screenPos = " + screenPos.getString() + "\n"; + + result += "currentUnitTitle = " + currentUnitTitle + "\n"; + + result += "inBailOutAttempt = " + intToStr(inBailOutAttempt) + "\n"; + + result += "random = " + intToStr(random.getLastNumber()) + "\n"; + if (this->random.getLastCaller() != "") { + result += "randomlastCaller = " + random.getLastCaller() + "\n"; + } + result += + "pathFindRefreshCellCount = " + intToStr(pathFindRefreshCellCount) + + "\n"; + + result += + "currentPathFinderDesiredFinalPos = " + + currentPathFinderDesiredFinalPos.getString() + "\n"; + + result += "lastStuckFrame = " + uIntToStr(lastStuckFrame) + "\n"; + result += "lastStuckPos = " + lastStuckPos.getString() + "\n"; + + if (attackParticleSystems.empty() == false) { + result += + "attackParticleSystems count = " + + intToStr(attackParticleSystems.size()) + "\n"; + } + if (networkCRCParticleLogInfo != "") { + result += + "networkCRCParticleLogInfo = " + networkCRCParticleLogInfo + "\n"; + } + if (networkCRCDecHpList.empty() == false) { + result += + "getNetworkCRCDecHpList() = " + getNetworkCRCDecHpList() + "\n"; + } + + if (getParticleInfo() != "") { + result += "getParticleInfo() = " + getParticleInfo() + "\n"; + } + for (unsigned int index = 0; index < attackParticleSystems.size(); + ++index) { + ParticleSystem *ps = attackParticleSystems[index]; + if (ps != NULL && + Renderer::getInstance().validateParticleSystemStillExists(ps, + rsGame) + == true) { + + result += + "attackParticleSystems #" + intToStr(index) + " = " + + ps->toString() + "\n"; + } + } + + return result; + } + + void Unit::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *unitNode = rootNode->addChild("Unit"); + + // const int id; + unitNode->addAttribute("id", intToStr(id), mapTagReplacements); + // For info purposes only + unitNode->addAttribute("name", type->getName(false), + mapTagReplacements); + + // int hp; + unitNode->addAttribute("hp", intToStr(hp), mapTagReplacements); + // int ep; + unitNode->addAttribute("ep", intToStr(ep), mapTagReplacements); + // int loadCount; + unitNode->addAttribute("loadCount", intToStr(loadCount), + mapTagReplacements); + // int deadCount; + unitNode->addAttribute("deadCount", intToStr(deadCount), + mapTagReplacements); + // float progress; //between 0 and 1 + unitNode->addAttribute("progress", intToStr(progress), + mapTagReplacements); + // float lastAnimProgress; //between 0 and 1 + unitNode->addAttribute("lastAnimProgress", intToStr(lastAnimProgress), + mapTagReplacements); + // float animProgress; //between 0 and 1 + unitNode->addAttribute("animProgress", intToStr(animProgress), + mapTagReplacements); + // float highlight; + unitNode->addAttribute("highlight", floatToStr(highlight, 6), + mapTagReplacements); + // int progress2; + unitNode->addAttribute("progress2", intToStr(progress2), + mapTagReplacements); + // int kills; + unitNode->addAttribute("kills", intToStr(kills), mapTagReplacements); + // int enemyKills; + unitNode->addAttribute("enemyKills", intToStr(enemyKills), + mapTagReplacements); + // UnitReference targetRef; + targetRef.saveGame(unitNode); + // + // Field currField; + unitNode->addAttribute("currField", intToStr(currField), + mapTagReplacements); + // Field targetField; + unitNode->addAttribute("targetField", intToStr(targetField), + mapTagReplacements); + // const Level *level; + if (level != NULL) { + level->saveGame(unitNode); + } + // Vec2i pos; + unitNode->addAttribute("pos", pos.getString(), mapTagReplacements); + // Vec2i lastPos; + unitNode->addAttribute("lastPos", lastPos.getString(), + mapTagReplacements); + // Vec2i targetPos; //absolute target pos + unitNode->addAttribute("targetPos", targetPos.getString(), + mapTagReplacements); + // Vec3f targetVec; + unitNode->addAttribute("targetVec", targetVec.getString(), + mapTagReplacements); + // Vec2i meetingPos; + unitNode->addAttribute("meetingPos", meetingPos.getString(), + mapTagReplacements); + // + // float lastRotation; //in degrees + unitNode->addAttribute("lastRotation", floatToStr(lastRotation, 6), + mapTagReplacements); + // float targetRotation; + unitNode->addAttribute("targetRotation", + floatToStr(targetRotation, 6), + mapTagReplacements); + // float rotation; + unitNode->addAttribute("rotation", floatToStr(rotation, 6), + mapTagReplacements); + // float targetRotationZ; + unitNode->addAttribute("targetRotationZ", + floatToStr(targetRotationZ, 6), + mapTagReplacements); + // float targetRotationX; + unitNode->addAttribute("targetRotationX", + floatToStr(targetRotationX, 6), + mapTagReplacements); + // float rotationZ; + unitNode->addAttribute("rotationZ", floatToStr(rotationZ, 6), + mapTagReplacements); + // float rotationX; + unitNode->addAttribute("rotationX", floatToStr(rotationX, 6), + mapTagReplacements); + // const UnitType *type; + unitNode->addAttribute("type", type->getName(false), + mapTagReplacements); + + unitNode->addAttribute("preMorph_type", + (preMorph_type != + NULL ? preMorph_type->getName(false) : ""), + mapTagReplacements); + + // const ResourceType *loadType; + if (loadType != NULL) { + unitNode->addAttribute("loadType", loadType->getName(), + mapTagReplacements); + } + // const SkillType *currSkill; + if (currSkill != NULL) { + unitNode->addAttribute("currSkillName", currSkill->getName(), + mapTagReplacements); + unitNode->addAttribute("currSkillClass", + intToStr(currSkill->getClass()), + mapTagReplacements); + } + // int lastModelIndexForCurrSkillType; + unitNode->addAttribute("lastModelIndexForCurrSkillType", + intToStr(lastModelIndexForCurrSkillType), + mapTagReplacements); + // int animationRandomCycleCount; + unitNode->addAttribute("animationRandomCycleCount", + intToStr(animationRandomCycleCount), + mapTagReplacements); + // + // bool toBeUndertaken; + unitNode->addAttribute("toBeUndertaken", intToStr(toBeUndertaken), + mapTagReplacements); + // bool alive; + unitNode->addAttribute("alive", intToStr(alive), mapTagReplacements); + // bool showUnitParticles; + unitNode->addAttribute("showUnitParticles", + intToStr(showUnitParticles), + mapTagReplacements); + // Faction *faction; + // ParticleSystem *fire; + int linkFireIndex = -1; + if (this->fire != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(this->fire, + rsGame) == true) { + //fire->saveGame(unitNode); + bool fireInSystemList = false; + if (fireParticleSystems.empty() == false) { + for (unsigned int i = 0; i < fireParticleSystems.size(); ++i) { + ParticleSystem *ps = fireParticleSystems[i]; + if (ps == this->fire) { + linkFireIndex = i; + fireInSystemList = true; + break; + } + } + } + if (fireInSystemList == false) { + this->fire->saveGame(unitNode); + } + } + // TotalUpgrade totalUpgrade; + totalUpgrade.saveGame(unitNode); + // Map *map; + // + // UnitPathInterface *unitPath; + unitPath->saveGame(unitNode); + // WaypointPath waypointPath; + // + // Commands commands; + for (Commands::iterator it = commands.begin(); it != commands.end(); + ++it) { + (*it)->saveGame(unitNode, faction); + } + // Observers observers; + //for(Observers::iterator it = observers.begin(); it != observers.end(); ++it) { + // (*it)->saveGame(unitNode); + //} + + // vector unitParticleSystems; + if (unitParticleSystems.empty() == false) { + XmlNode *unitParticleSystemsNode = + unitNode->addChild("unitParticleSystems"); + + for (unsigned int i = 0; i < unitParticleSystems.size(); ++i) { + UnitParticleSystem *ups = unitParticleSystems[i]; + if (ups != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(ups, + rsGame) == + true) { + ups->saveGame(unitParticleSystemsNode); + } + } + } + // vector queuedUnitParticleSystemTypes; + if (queuedUnitParticleSystemTypes.empty() == false) { + XmlNode *queuedUnitParticleSystemTypesNode = + unitNode->addChild("queuedUnitParticleSystemTypes"); + for (unsigned int i = 0; i < queuedUnitParticleSystemTypes.size(); + ++i) { + UnitParticleSystemType *upst = queuedUnitParticleSystemTypes[i]; + if (upst != NULL) { + upst->saveGame(queuedUnitParticleSystemTypesNode); + } + } + } + // UnitParticleSystems damageParticleSystems; + if (damageParticleSystems.empty() == false) { + XmlNode *damageParticleSystemsNode = + unitNode->addChild("damageParticleSystems"); + for (unsigned int i = 0; i < damageParticleSystems.size(); ++i) { + UnitParticleSystem *ups = damageParticleSystems[i]; + if (ups != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(ups, + rsGame) == + true) { + ups->saveGame(damageParticleSystemsNode); + } + } + } + // std::map damageParticleSystemsInUse; + if (damageParticleSystemsInUse.empty() == false) { + XmlNode *damageParticleSystemsInUseNode = + unitNode->addChild("damageParticleSystemsInUse"); + + for (std::map < int, UnitParticleSystem * >::const_iterator iterMap = + damageParticleSystemsInUse.begin(); + iterMap != damageParticleSystemsInUse.end(); ++iterMap) { + if (iterMap->second != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(iterMap-> + second, + rsGame) == + true) { + XmlNode *damageParticleSystemsInUseNode2 = + damageParticleSystemsInUseNode->addChild + ("damageParticleSystemsInUse"); + + damageParticleSystemsInUseNode2->addAttribute("key", + intToStr + (iterMap->first), + mapTagReplacements); + iterMap->second->saveGame(damageParticleSystemsInUseNode2); + } + } + } + + // vector fireParticleSystems; + if (fireParticleSystems.empty() == false) { + XmlNode *fireParticleSystemsNode = + unitNode->addChild("fireParticleSystems"); + + if (linkFireIndex >= 0) { + fireParticleSystemsNode->addAttribute("fireParticleLink", + intToStr(linkFireIndex), + mapTagReplacements); + } + + for (unsigned int i = 0; i < fireParticleSystems.size(); ++i) { + ParticleSystem *ps = fireParticleSystems[i]; + if (ps != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(ps, + rsGame) == + true) { + ps->saveGame(fireParticleSystemsNode); + } + } + } + + // vector smokeParticleSystems; + if (smokeParticleSystems.empty() == false) { + XmlNode *smokeParticleSystemsNode = + unitNode->addChild("smokeParticleSystems"); + for (unsigned int i = 0; i < smokeParticleSystems.size(); ++i) { + UnitParticleSystem *ups = smokeParticleSystems[i]; + if (ups != NULL + && Renderer:: + getInstance().validateParticleSystemStillExists(ups, + rsGame) == + true) { + ups->saveGame(smokeParticleSystemsNode); + //printf("Saving smoke particles:\n[%s]\n",ups->toString().c_str()); + } + } + } + + // CardinalDir modelFacing; + unitNode->addAttribute("modelFacing", intToStr(modelFacing), + mapTagReplacements); + + // std::string lastSynchDataString; + unitNode->addAttribute("lastSynchDataString", lastSynchDataString, + mapTagReplacements); + // std::string lastFile; + unitNode->addAttribute("lastFile", lastFile, mapTagReplacements); + // int lastLine; + unitNode->addAttribute("lastLine", intToStr(lastLine), + mapTagReplacements); + // std::string lastSource; + unitNode->addAttribute("lastSource", lastSource, mapTagReplacements); + // int lastRenderFrame; + unitNode->addAttribute("lastRenderFrame", intToStr(lastRenderFrame), + mapTagReplacements); + // bool visible; + unitNode->addAttribute("visible", intToStr(visible), + mapTagReplacements); + // int retryCurrCommandCount; + unitNode->addAttribute("retryCurrCommandCount", + intToStr(retryCurrCommandCount), + mapTagReplacements); + // Vec3f screenPos; + unitNode->addAttribute("screenPos", screenPos.getString(), + mapTagReplacements); + // string currentUnitTitle; + unitNode->addAttribute("currentUnitTitle", currentUnitTitle, + mapTagReplacements); + // + // bool inBailOutAttempt; + unitNode->addAttribute("inBailOutAttempt", intToStr(inBailOutAttempt), + mapTagReplacements); + // //std::vector > badHarvestPosList; + // std::map badHarvestPosList; + for (std::map < Vec2i, int >::const_iterator iterMap = + badHarvestPosList.begin(); iterMap != badHarvestPosList.end(); + ++iterMap) { + XmlNode *badHarvestPosListNode = + unitNode->addChild("badHarvestPosList"); + + badHarvestPosListNode->addAttribute("key", + iterMap->first.getString(), + mapTagReplacements); + badHarvestPosListNode->addAttribute("value", + intToStr(iterMap->second), + mapTagReplacements); + } + + // //time_t lastBadHarvestListPurge; + // std::pair lastHarvestResourceTarget; + XmlNode *lastHarvestResourceTargetNode = + unitNode->addChild("lastHarvestResourceTarget"); + lastHarvestResourceTargetNode->addAttribute("key", + lastHarvestResourceTarget.first. + getString(), + mapTagReplacements); + lastHarvestResourceTargetNode->addAttribute("value", + intToStr + (lastHarvestResourceTarget.second), + mapTagReplacements); + + // //std::pair > currentTargetPathTaken; + // static Game *game; + // + // bool ignoreCheckCommand; + unitNode->addAttribute("ignoreCheckCommand", + intToStr(ignoreCheckCommand), + mapTagReplacements); + // uint32 lastStuckFrame; + unitNode->addAttribute("lastStuckFrame", uIntToStr(lastStuckFrame), + mapTagReplacements); + // Vec2i lastStuckPos; + unitNode->addAttribute("lastStuckPos", lastStuckPos.getString(), + mapTagReplacements); + // uint32 lastPathfindFailedFrame; + unitNode->addAttribute("lastPathfindFailedFrame", + intToStr(lastPathfindFailedFrame), + mapTagReplacements); + // Vec2i lastPathfindFailedPos; + unitNode->addAttribute("lastPathfindFailedPos", + lastPathfindFailedPos.getString(), + mapTagReplacements); + // bool usePathfinderExtendedMaxNodes; + unitNode->addAttribute("usePathfinderExtendedMaxNodes", + intToStr(usePathfinderExtendedMaxNodes), + mapTagReplacements); + // int maxQueuedCommandDisplayCount; + unitNode->addAttribute("maxQueuedCommandDisplayCount", + intToStr(maxQueuedCommandDisplayCount), + mapTagReplacements); + // UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; + currentAttackBoostOriginatorEffect.saveGame(unitNode); + // std::vector currentAttackBoostEffects; + for (unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + UnitAttackBoostEffect *uabe = currentAttackBoostEffects[i]; + if (uabe != NULL) { + uabe->saveGame(unitNode); + } + } + + // Mutex *mutexCommands; + // + // //static Mutex mutexDeletedUnits; + // //static std::map deletedUnits; + // + // bool changedActiveCommand; + unitNode->addAttribute("changedActiveCommand", + intToStr(changedActiveCommand), + mapTagReplacements); + // int lastAttackerUnitId; + unitNode->addAttribute("lastAttackerUnitId", + intToStr(lastAttackerUnitId), + mapTagReplacements); + // int lastAttackedUnitId; + unitNode->addAttribute("lastAttackedUnitId", + intToStr(lastAttackedUnitId), + mapTagReplacements); + // CauseOfDeathType causeOfDeath; + unitNode->addAttribute("causeOfDeath", intToStr(causeOfDeath), + mapTagReplacements); + + //pathfindFailedConsecutiveFrameCount + unitNode->addAttribute("pathfindFailedConsecutiveFrameCount", + intToStr(pathfindFailedConsecutiveFrameCount), + mapTagReplacements); + + unitNode->addAttribute("currentPathFinderDesiredFinalPos", + currentPathFinderDesiredFinalPos.getString(), + mapTagReplacements); + + unitNode->addAttribute("random", intToStr(random.getLastNumber()), + mapTagReplacements); + unitNode->addAttribute("pathFindRefreshCellCount", + intToStr(pathFindRefreshCellCount), + mapTagReplacements); + } + + Unit *Unit::loadGame(const XmlNode * rootNode, GameSettings * settings, + Faction * faction, World * world) { + const XmlNode *unitNode = rootNode; + + int newUnitId = unitNode->getAttribute("id")->getIntValue(); + Vec2i newUnitPos = + Vec2i::strToVec2(unitNode->getAttribute("pos")->getValue()); + string newUnitType = unitNode->getAttribute("type")->getValue(); + const UnitType *ut = faction->getType()->getUnitType(newUnitType); + CardinalDir newModelFacing = + static_cast + (unitNode->getAttribute("modelFacing")->getIntValue()); + + // Unit *result = new Unit(int id, UnitPathInterface *unitpath, const Vec2i &pos, + // const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing) : BaseColorPickEntity(), id(id) { + + UnitPathInterface *newpath = NULL; + switch (settings->getPathFinderType()) { + case pfBasic: + newpath = new UnitPathBasic(); + break; + default: + throw + megaglest_runtime_error("detected unsupported pathfinder type!"); + } + + newpath->loadGame(unitNode); + //Unit *result = new Unit(getNextUnitId(f), newpath, Vec2i(0), ut, f, &map, CardinalDir::NORTH); + //Unit(int id, UnitPathInterface *path, const Vec2i &pos, const UnitType *type, Faction *faction, Map *map, CardinalDir placeFacing); + Unit *result = new Unit(newUnitId, newpath, newUnitPos, ut, faction, + world->getMapPtr(), newModelFacing); + + if (unitNode->hasAttribute("preMorph_name") == true) { + string newUnitType_preMorph = + unitNode->getAttribute("preMorph_name")->getValue(); + const UnitType *ut_premorph = + faction->getType()->getUnitType(newUnitType_preMorph); + result->preMorph_type = ut_premorph; + } + + result->lastRotation = + unitNode->getAttribute("lastRotation")->getFloatValue(); + result->targetRotation = + unitNode->getAttribute("targetRotation")->getFloatValue(); + result->rotation = + unitNode->getAttribute("rotation")->getFloatValue(); + + //world->placeUnitAtLocation(newUnitPos, generationArea, unit, true); + //result->setPos(newUnitPos); + //Vec2i meetingPos = newUnitPos-Vec2i(1); + //result->setMeetingPos(meetingPos); + result->pos = newUnitPos; + result->lastPos = + Vec2i::strToVec2(unitNode->getAttribute("lastPos")->getValue()); + result->meetingPos = + Vec2i::strToVec2(unitNode->getAttribute("meetingPos")->getValue()); + // Attempt to improve performance + //result->exploreCells(); + //result->calculateFogOfWarRadius(); + // -------------------------- + + result->hp = unitNode->getAttribute("hp")->getIntValue(); + // int ep; + result->ep = unitNode->getAttribute("ep")->getIntValue(); + // int loadCount; + result->loadCount = + unitNode->getAttribute("loadCount")->getIntValue(); + // int deadCount; + result->deadCount = + unitNode->getAttribute("deadCount")->getIntValue(); + // float progress; //between 0 and 1 + try { + result->progress = + unitNode->getAttribute("progress")->getIntValue(); + } +#ifdef WIN32 + catch (const exception &) { +#else + catch (const exception & ex) { +#endif + result->progress = + unitNode->getAttribute("progress")->getFloatValue(); + } + // float lastAnimProgress; //between 0 and 1 + try { + result->lastAnimProgress = + unitNode->getAttribute("lastAnimProgress")->getIntValue(); + } +#ifdef WIN32 + catch (const exception &) { +#else + catch (const exception & ex) { +#endif + result->lastAnimProgress = + unitNode->getAttribute("lastAnimProgress")->getFloatValue(); + } + + // float animProgress; //between 0 and 1 + try { + result->animProgress = + unitNode->getAttribute("animProgress")->getIntValue(); + } +#ifdef WIN32 + catch (const exception &) { +#else + catch (const exception & ex) { +#endif + result->animProgress = + unitNode->getAttribute("animProgress")->getFloatValue(); + } + + // float highlight; + result->highlight = + unitNode->getAttribute("highlight")->getFloatValue(); + // int progress2; + result->progress2 = + unitNode->getAttribute("progress2")->getIntValue(); + // int kills; + result->kills = unitNode->getAttribute("kills")->getIntValue(); + // int enemyKills; + result->enemyKills = + unitNode->getAttribute("enemyKills")->getIntValue(); + // UnitReference targetRef; + // targetRef.saveGame(unitNode); + result->targetRef.loadGame(unitNode, world); + // + // Field currField; + result->currField = + static_cast + (unitNode->getAttribute("currField")->getIntValue()); + // Field targetField; + result->targetField = + static_cast + (unitNode->getAttribute("targetField")->getIntValue()); + // const Level *level; + // if(level != NULL) { + // level->saveGame(unitNode); + // } + result->level = Level::loadGame(unitNode, ut); + // Vec2i pos; + result->pos = + Vec2i::strToVec2(unitNode->getAttribute("pos")->getValue()); + // Vec2i lastPos; + result->lastPos = + Vec2i::strToVec2(unitNode->getAttribute("lastPos")->getValue()); + // Vec2i targetPos; //absolute target pos + result->targetPos = + Vec2i::strToVec2(unitNode->getAttribute("targetPos")->getValue()); + // Vec3f targetVec; + result->targetVec = + Vec3f::strToVec3(unitNode->getAttribute("targetVec")->getValue()); + // Vec2i meetingPos; + result->meetingPos = + Vec2i::strToVec2(unitNode->getAttribute("meetingPos")->getValue()); + // + // float lastRotation; //in degrees + result->lastRotation = + unitNode->getAttribute("lastRotation")->getFloatValue(); + // float targetRotation; + result->targetRotation = + unitNode->getAttribute("targetRotation")->getFloatValue(); + // float rotation; + result->rotation = + unitNode->getAttribute("rotation")->getFloatValue(); + // float targetRotationZ; + result->targetRotationZ = + unitNode->getAttribute("targetRotationZ")->getFloatValue(); + // float targetRotationX; + result->targetRotationX = + unitNode->getAttribute("targetRotationX")->getFloatValue(); + // float rotationZ; + result->rotationZ = + unitNode->getAttribute("rotationZ")->getFloatValue(); + // float rotationX; + result->rotationX = + unitNode->getAttribute("rotationX")->getFloatValue(); + // const UnitType *type; + // unitNode->addAttribute("type",type->getName(), mapTagReplacements); + // const ResourceType *loadType; + // if(loadType != NULL) { + // unitNode->addAttribute("loadType",loadType->getName(), mapTagReplacements); + // } + if (unitNode->hasAttribute("loadType") == true) { + string loadTypeName = + unitNode->getAttribute("loadType")->getValue(); + result->loadType = + world->getTechTree()->getResourceType(loadTypeName); + } + // const SkillType *currSkill; + // if(currSkill != NULL) { + // unitNode->addAttribute("currSkill",currSkill->getName(), mapTagReplacements); + // } + if (unitNode->hasAttribute("currSkillName") == true) { + string skillTypeName = + unitNode->getAttribute("currSkillName")->getValue(); + SkillClass skillClass = + static_cast + (unitNode->getAttribute("currSkillClass")->getIntValue()); + result->currSkill = ut->getSkillType(skillTypeName, skillClass); + result->setCurrSkill(result->currSkill); + } + + // int lastModelIndexForCurrSkillType; + result->lastModelIndexForCurrSkillType = + unitNode-> + getAttribute("lastModelIndexForCurrSkillType")->getIntValue(); + // int animationRandomCycleCount; + result->animationRandomCycleCount = + unitNode->getAttribute("animationRandomCycleCount")->getIntValue(); + // + // bool toBeUndertaken; + result->toBeUndertaken = + unitNode->getAttribute("toBeUndertaken")->getIntValue() != 0; + // bool alive; + result->setAlive(unitNode->getAttribute("alive")->getIntValue() != + 0); + // bool showUnitParticles; + result->showUnitParticles = + unitNode->getAttribute("showUnitParticles")->getIntValue() != 0; + // Faction *faction; + // ParticleSystem *fire; + // if(fire != NULL) { + // fire->saveGame(unitNode); + // } + if (unitNode->hasChild("FireParticleSystem") == true) { + XmlNode *fireNode = unitNode->getChild("FireParticleSystem"); + result->fire = new FireParticleSystem(); + result->fire->setParticleOwner(result); + result->fire->loadGame(fireNode); + //result->fire->setTexture(CoreData::getInstance().getFireTexture()); + result->fireParticleSystems.push_back(result->fire); + + //printf("Load MAIN fire particle result->fire = %p\n",result->fire); + + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (result->fire, + rsGame)); + } + + // TotalUpgrade totalUpgrade; + result->totalUpgrade.loadGame(unitNode); + // Map *map; + // + // UnitPathInterface *unitPath; + // unitPath->saveGame(unitNode); + // WaypointPath waypointPath; + // + // Commands commands; + // for(Commands::iterator it = commands.begin(); it != commands.end(); ++it) { + // (*it)->saveGame(unitNode); + // } + vector < XmlNode * >commandNodeList = + unitNode->getChildList("Command"); + for (unsigned int i = 0; i < commandNodeList.size(); ++i) { + XmlNode *node = commandNodeList[i]; + Command *command = Command::loadGame(node, ut, world); + + static string mutexOwnerId = + string(__FILE__) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(result->mutexCommands, mutexOwnerId); + result->commands.push_back(command); + safeMutex.ReleaseLock(); + } + // Observers observers; + //for(Observers::iterator it = observers.begin(); it != observers.end(); ++it) { + // (*it)->saveGame(unitNode); + //} + + // vector unitParticleSystems; + // for(unsigned int i = 0; i < unitParticleSystems.size(); ++i) { + // UnitParticleSystem *ups= unitParticleSystems[i]; + // ups->saveGame(unitNode); + // } + if (unitNode->hasChild("unitParticleSystems") == true) { + XmlNode *unitParticleSystemsNode = + unitNode->getChild("unitParticleSystems"); + vector < XmlNode * >unitParticleSystemNodeList = + unitParticleSystemsNode->getChildList("UnitParticleSystem"); + for (unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { + XmlNode *node = unitParticleSystemNodeList[i]; + + UnitParticleSystem *ups = new UnitParticleSystem(); + ups->setParticleOwner(result); + ups->loadGame(node); + result->unitParticleSystems.push_back(ups); + + //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (ups, rsGame)); + } + } + + // vector queuedUnitParticleSystemTypes; + // for(unsigned int i = 0; i < queuedUnitParticleSystemTypes.size(); ++i) { + // UnitParticleSystemType *upst= queuedUnitParticleSystemTypes[i]; + // upst->saveGame(unitNode); + // } + + // UnitParticleSystems damageParticleSystems; + // for(unsigned int i = 0; i < damageParticleSystems.size(); ++i) { + // UnitParticleSystem *ups= damageParticleSystems[i]; + // ups->saveGame(unitNode); + // } + if (unitNode->hasChild("damageParticleSystems") == true) { + XmlNode *damageParticleSystemsNode = + unitNode->getChild("damageParticleSystems"); + vector < XmlNode * >unitParticleSystemNodeList = + damageParticleSystemsNode->getChildList("UnitParticleSystem"); + for (unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { + XmlNode *node = unitParticleSystemNodeList[i]; + + UnitParticleSystem *ups = new UnitParticleSystem(); + ups->setParticleOwner(result); + ups->loadGame(node); + result->damageParticleSystems.push_back(ups); + result->damageParticleSystemsInUse[i] = ups; + + //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (ups, rsGame)); + } + } + + // std::map damageParticleSystemsInUse; + // for(std::map::const_iterator iterMap = damageParticleSystemsInUse.begin(); + // iterMap != damageParticleSystemsInUse.end(); ++iterMap) { + // XmlNode *damageParticleSystemsInUseNode = unitNode->addChild("damageParticleSystemsInUse"); + // + // damageParticleSystemsInUseNode->addAttribute("key",intToStr(iterMap->first), mapTagReplacements); + // iterMap->second->saveGame(damageParticleSystemsInUseNode); + // } + // if(unitNode->hasChild("damageParticleSystemsInUse") == true) { + // XmlNode *damageParticleSystemsInUseNode = unitNode->getChild("damageParticleSystemsInUse"); + // vector damageParticleSystemsInUseNode2 = damageParticleSystemsInUseNode->getChildList("damageParticleSystemsInUse"); + // for(unsigned int i = 0; i < damageParticleSystemsInUseNode2.size(); ++i) { + // XmlNode *d2Node = damageParticleSystemsInUseNode2[i]; + // + // vector unitParticleSystemNodeList = damageParticleSystemsInUseNode->getChildList("UnitParticleSystem"); + // for(unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { + // XmlNode *node = unitParticleSystemNodeList[i]; + // + // UnitParticleSystem *ups = new UnitParticleSystem(); + // ups->loadGame(node); + // result->unitParticleSystems.push_back(ups); + // + // //Renderer::getInstance().manageParticleSystem(result->fire, rsGame); + // Renderer::getInstance().addToDeferredParticleSystemList(make_pair(ups, rsGame)); + // } + // + // } + + // vector fireParticleSystems; + // for(unsigned int i = 0; i < fireParticleSystems.size(); ++i) { + // ParticleSystem *ps= fireParticleSystems[i]; + // ps->saveGame(unitNode); + // } + if (unitNode->hasChild("fireParticleSystems") == true) { + XmlNode *fireParticleSystemsNode = + unitNode->getChild("fireParticleSystems"); + + int linkFireIndex = -1; + if (fireParticleSystemsNode->hasAttribute("fireParticleLink") == + true) { + linkFireIndex = + fireParticleSystemsNode-> + getAttribute("fireParticleLink")->getIntValue(); + } + + vector < XmlNode * >unitParticleSystemNodeList = + fireParticleSystemsNode->getChildList("FireParticleSystem"); + for (int i = 0; i < (int) unitParticleSystemNodeList.size(); ++i) { + XmlNode *node = unitParticleSystemNodeList[i]; + + if (result->fire == NULL || linkFireIndex != i) { + FireParticleSystem *ups = new FireParticleSystem(); + ups->setParticleOwner(result); + ups->loadGame(node); + //ups->setTexture(CoreData::getInstance().getFireTexture()); + result->fireParticleSystems.push_back(ups); + + //printf("Load fire particle i = %d linkFireIndex = %d result->fire = %p ups = %p\n",i,linkFireIndex,result->fire,ups); + + if (result->fire == NULL && linkFireIndex >= 0 + && linkFireIndex == i) { + result->fire = ups; + } + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (ups, rsGame)); + } + } + } + + // vector smokeParticleSystems; + // for(unsigned int i = 0; i < smokeParticleSystems.size(); ++i) { + // UnitParticleSystem *ups= smokeParticleSystems[i]; + // ups->saveGame(unitNode); + // } + if (unitNode->hasChild("smokeParticleSystems") == true) { + XmlNode *smokeParticleSystemsNode = + unitNode->getChild("smokeParticleSystems"); + vector < XmlNode * >unitParticleSystemNodeList = + smokeParticleSystemsNode->getChildList("UnitParticleSystem"); + for (unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { + XmlNode *node = unitParticleSystemNodeList[i]; + + // printf("Load Smoke particle i = %d\n",i); + UnitParticleSystem *ups = new UnitParticleSystem(); + ups->setParticleOwner(result); + ups->loadGame(node); + //ups->setTexture(CoreData::getInstance().getFireTexture()); + result->smokeParticleSystems.push_back(ups); + + Renderer:: + getInstance().addToDeferredParticleSystemList(make_pair + (ups, rsGame)); + + //printf("Loading smoke particles:\n[%s]\n",ups->toString().c_str()); + } + } + + // CardinalDir modelFacing; + // unitNode->addAttribute("modelFacing",intToStr(modelFacing), mapTagReplacements); + + // std::string lastSynchDataString; + // unitNode->addAttribute("lastSynchDataString",lastSynchDataString, mapTagReplacements); + // std::string lastFile; + // unitNode->addAttribute("lastFile",lastFile, mapTagReplacements); + // int lastLine; + // unitNode->addAttribute("lastLine",intToStr(lastLine), mapTagReplacements); + // std::string lastSource; + // unitNode->addAttribute("lastSource",lastSource, mapTagReplacements); + // int lastRenderFrame; + result->lastRenderFrame = + unitNode->getAttribute("lastRenderFrame")->getIntValue(); + // bool visible; + result->visible = + unitNode->getAttribute("visible")->getIntValue() != 0; + // int retryCurrCommandCount; + result->retryCurrCommandCount = + unitNode->getAttribute("retryCurrCommandCount")->getIntValue(); + // Vec3f screenPos; + result->screenPos = + Vec3f::strToVec3(unitNode->getAttribute("screenPos")->getValue()); + // string currentUnitTitle; + result->currentUnitTitle = + unitNode->getAttribute("currentUnitTitle")->getValue(); + // + // bool inBailOutAttempt; + result->inBailOutAttempt = + unitNode->getAttribute("inBailOutAttempt")->getIntValue() != 0; + // //std::vector > badHarvestPosList; + // std::map badHarvestPosList; + // for(std::map::const_iterator iterMap = badHarvestPosList.begin(); + // iterMap != badHarvestPosList.end(); ++iterMap) { + // XmlNode *badHarvestPosListNode = unitNode->addChild("badHarvestPosList"); + // + // badHarvestPosListNode->addAttribute("key",iterMap->first.getString(), mapTagReplacements); + // badHarvestPosListNode->addAttribute("value",intToStr(iterMap->second), mapTagReplacements); + // } + + // //time_t lastBadHarvestListPurge; + // std::pair lastHarvestResourceTarget; + const XmlNode *lastHarvestResourceTargetNode = + unitNode->getChild("lastHarvestResourceTarget"); + // lastHarvestResourceTargetNode->addAttribute("key",lastHarvestResourceTarget.first.getString(), mapTagReplacements); + // lastHarvestResourceTargetNode->addAttribute("value",intToStr(lastHarvestResourceTarget.second), mapTagReplacements); + + result->lastHarvestResourceTarget = + make_pair(Vec2i::strToVec2 + (lastHarvestResourceTargetNode->getAttribute("key")-> + getValue()), + lastHarvestResourceTargetNode-> + getAttribute("value")->getIntValue()); + + // //std::pair > currentTargetPathTaken; + // static Game *game; + // + // bool ignoreCheckCommand; + result->ignoreCheckCommand = + unitNode->getAttribute("ignoreCheckCommand")->getIntValue() != 0; + // uint32 lastStuckFrame; + result->lastStuckFrame = + unitNode->getAttribute("lastStuckFrame")->getIntValue(); + // Vec2i lastStuckPos; + result->lastStuckPos = + Vec2i::strToVec2(unitNode-> + getAttribute("lastStuckPos")->getValue()); + // uint32 lastPathfindFailedFrame; + result->lastPathfindFailedFrame = + unitNode->getAttribute("lastPathfindFailedFrame")->getIntValue(); + // Vec2i lastPathfindFailedPos; + result->lastPathfindFailedPos = + Vec2i::strToVec2(unitNode-> + getAttribute("lastPathfindFailedPos")->getValue + ()); + // bool usePathfinderExtendedMaxNodes; + result->usePathfinderExtendedMaxNodes = + unitNode-> + getAttribute("usePathfinderExtendedMaxNodes")->getIntValue() != 0; + // int maxQueuedCommandDisplayCount; + result->maxQueuedCommandDisplayCount = + unitNode-> + getAttribute("maxQueuedCommandDisplayCount")->getIntValue(); + // UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; + + // !!! TODO: Softcoder - in progress work to load attack boosts, not working properly yet + result->currentAttackBoostOriginatorEffect.loadGame(unitNode, result, + world); + + // std::vector currentAttackBoostEffects; + // for(unsigned int i = 0; i < currentAttackBoostEffects.size(); ++i) { + // UnitAttackBoostEffect *uabe= currentAttackBoostEffects[i]; + // uabe->saveGame(unitNode); + // } + + // !!! TODO: Softcoder - in progress work to load attack boosts, not working properly yet + if (unitNode->hasChild("UnitAttackBoostEffect") == true) { + vector < XmlNode * >unitParticleSystemNodeList = + unitNode->getChildList("UnitAttackBoostEffect"); + for (unsigned int i = 0; i < unitParticleSystemNodeList.size(); ++i) { + XmlNode *node = unitParticleSystemNodeList[i]; + + UnitAttackBoostEffect *attackBoostEffect = + new UnitAttackBoostEffect(); + attackBoostEffect->loadGame(node, result, world, false); + + result->currentAttackBoostEffects.push_back(attackBoostEffect); + } + } + //printf("Unit [%d - %s] has currentAttackBoostEffects count: %d\n",result->getId(),result->getType()->getName(false).c_str(),(int)result->currentAttackBoostEffects.size()); + + + // Mutex *mutexCommands; + // + // //static Mutex mutexDeletedUnits; + // //static std::map deletedUnits; + // + // bool changedActiveCommand; + result->changedActiveCommand = + unitNode->getAttribute("changedActiveCommand")->getIntValue() != 0; + // int lastAttackerUnitId; + result->lastAttackerUnitId = + unitNode->getAttribute("lastAttackerUnitId")->getIntValue(); + // int lastAttackedUnitId; + result->lastAttackedUnitId = + unitNode->getAttribute("lastAttackedUnitId")->getIntValue(); + // CauseOfDeathType causeOfDeath; + result->causeOfDeath = + static_cast + (unitNode->getAttribute("causeOfDeath")->getIntValue()); + + result->pathfindFailedConsecutiveFrameCount = + unitNode-> + getAttribute("pathfindFailedConsecutiveFrameCount")->getIntValue(); + + if (result->alive) { + world->getMapPtr()->putUnitCells(result, newUnitPos); + //result->born(); + } + + result->pos = newUnitPos; + result->lastPos = + Vec2i::strToVec2(unitNode->getAttribute("lastPos")->getValue()); + result->meetingPos = + Vec2i::strToVec2(unitNode->getAttribute("meetingPos")->getValue()); + + if (unitNode->hasAttribute("currentPathFinderDesiredFinalPos")) { + result->currentPathFinderDesiredFinalPos = + Vec2i::strToVec2(unitNode->getAttribute + ("currentPathFinderDesiredFinalPos")->getValue + ()); + } + + if (unitNode->hasAttribute("random")) { + result->random.setLastNumber(unitNode-> + getAttribute("random")->getIntValue + ()); + } + if (unitNode->hasAttribute("pathFindRefreshCellCount")) { + result->pathFindRefreshCellCount = + unitNode->getAttribute("pathFindRefreshCellCount")->getIntValue(); + } + + //result->exploreCells(); + //result->calculateFogOfWarRadius(); + + return result; + } + + Checksum Unit::getCRC() { + const bool consoleDebug = false; + + Checksum crcForUnit; + + crcForUnit.addInt(id); + crcForUnit.addInt(hp); + crcForUnit.addInt(ep); + crcForUnit.addInt(loadCount); + crcForUnit.addInt(deadCount); + + if (consoleDebug) + printf("#1 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + crcForUnit.addInt64(progress); + crcForUnit.addInt64(lastAnimProgress); + crcForUnit.addInt64(animProgress); + + if (consoleDebug) + printf("#2 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //float highlight; + crcForUnit.addInt(progress2); + crcForUnit.addInt(kills); + crcForUnit.addInt(enemyKills); + crcForUnit.addInt(morphFieldsBlocked); + + if (consoleDebug) + printf("#3 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //UnitReference targetRef; + + crcForUnit.addInt(currField); + crcForUnit.addInt(targetField); + + if (consoleDebug) + printf("#4 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //const Level *level; + if (level != NULL) { + crcForUnit.addString(level->getName(false)); + } + + if (consoleDebug) + printf("#5 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + crcForUnit.addInt(pos.x); + crcForUnit.addInt(pos.y); + crcForUnit.addInt(lastPos.x); + crcForUnit.addInt(lastPos.y); + crcForUnit.addInt(targetPos.x); + crcForUnit.addInt(targetPos.y); + + if (consoleDebug) + printf("#6 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //Vec3f targetVec; + + crcForUnit.addInt(meetingPos.x); + crcForUnit.addInt(meetingPos.y); + + if (consoleDebug) + printf("#7 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //float lastRotation; + //float targetRotation; + //float rotation; + //float targetRotationZ; + //float targetRotationX; + //float rotationZ; + //float rotationX; + + //const UnitType *preMorph_type; + if (preMorph_type != NULL) { + crcForUnit.addString(preMorph_type->getName(false)); + } + + if (consoleDebug) + printf("#8 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //const UnitType *type; + if (type != NULL) { + crcForUnit.addString(type->getName(false)); + } + + //const ResourceType *loadType; + if (loadType != NULL) { + crcForUnit.addString(loadType->getName(false)); + } + + //const SkillType *currSkill; + if (currSkill != NULL) { + crcForUnit.addString(currSkill->getName()); + } + + //printf("#9 Unit: %d CRC: %u lastModelIndexForCurrSkillType: %d\n",id,crcForUnit.getSum(),lastModelIndexForCurrSkillType); + //printf("#9a Unit: %d CRC: %u\n",id,crcForUnit.getSum()); + //crcForUnit.addInt(lastModelIndexForCurrSkillType); + + if (consoleDebug) + printf("#9 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //crcForUnit.addInt(animationRandomCycleCount); + //printf("#9b Unit: %d CRC: %u\n",id,crcForUnit.getSum()); + + crcForUnit.addInt(toBeUndertaken); + + if (consoleDebug) + printf("#9c Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + crcForUnit.addInt(alive); + //bool showUnitParticles; + + if (consoleDebug) + printf("#10 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //Faction *faction; + //ParticleSystem *fire; + if (fire != NULL) { + crcForUnit.addInt(fire->getActive()); + } + + //TotalUpgrade totalUpgrade; + uint32 crc = totalUpgrade.getCRC().getSum(); + crcForUnit.addBytes(&crc, sizeof(uint32)); + + //Map *map; + //UnitPathInterface *unitPath; + if (unitPath != NULL) { + uint32 crc = unitPath->getCRC().getSum(); + crcForUnit.addBytes(&crc, sizeof(uint32)); + } + //WaypointPath waypointPath; + + if (consoleDebug) + printf("#11 Unit: %d CRC: %u commands.size(): " MG_SIZE_T_SPECIFIER + "\n", id, crcForUnit.getSum(), commands.size()); + + //Commands commands; + if (commands.empty() == false) { + crcForUnit.addInt((int) commands.size()); + for (Commands::const_iterator it = commands.begin(); + it != commands.end(); ++it) { + uint32 crc = (*it)->getCRC().getSum(); + crcForUnit.addBytes(&crc, sizeof(uint32)); + } + } + + //printf("#11 Unit: %d CRC: %u observers.size(): %ld\n",id,crcForUnit.getSum(),observers.size()); + + //Observers observers; + //crcForUnit.addInt64((int64)observers.size()); + + if (consoleDebug) + printf("#11 Unit: %d CRC: %u damageParticleSystems.size(): " + MG_SIZE_T_SPECIFIER "\n", id, crcForUnit.getSum(), + damageParticleSystems.size()); + + //vector unitParticleSystems; + //vector queuedUnitParticleSystemTypes; + + //UnitParticleSystems damageParticleSystems; + crcForUnit.addInt((int) damageParticleSystems.size()); + + if (consoleDebug) + printf("#12 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //std::map damageParticleSystemsInUse; + + //vector fireParticleSystems; + //vector smokeParticleSystems; + + //CardinalDir modelFacing; + crcForUnit.addInt(modelFacing); + + //std::string lastSynchDataString; + //std::string lastFile; + //int lastLine; + //std::string lastSource; + //int lastRenderFrame; + //bool visible; + + //int retryCurrCommandCount; + + //Vec3f screenPos; + //string currentUnitTitle; + + if (consoleDebug) + printf("#13 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + crcForUnit.addInt(inBailOutAttempt); + + crcForUnit.addInt((int) badHarvestPosList.size()); + //crcForUnit.addInt(lastHarvestResourceTarget.first()); + + if (consoleDebug) + printf("#14 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //static Game *game; + //bool ignoreCheckCommand; + + //uint32 lastStuckFrame; + crcForUnit.addUInt(lastStuckFrame); + //Vec2i lastStuckPos; + crcForUnit.addInt(lastStuckPos.x); + crcForUnit.addInt(lastStuckPos.y); + + //uint32 lastPathfindFailedFrame; + //Vec2i lastPathfindFailedPos; + //bool usePathfinderExtendedMaxNodes; + //int maxQueuedCommandDisplayCount; + + //UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; + crcForUnit. + addInt((int) + currentAttackBoostOriginatorEffect.currentAttackBoostUnits. + size()); + + if (consoleDebug) + printf("#15 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //std::vector currentAttackBoostEffects; + + //Mutex *mutexCommands; + + //bool changedActiveCommand; + + //int lastAttackerUnitId; + //int lastAttackedUnitId; + //CauseOfDeathType causeOfDeath; + + //uint32 pathfindFailedConsecutiveFrameCount; + crcForUnit.addString(this-> + currentPathFinderDesiredFinalPos.getString()); + + crcForUnit.addInt(random.getLastNumber()); + if (this->random.getLastCaller() != "") { + crcForUnit.addString(this->random.getLastCaller()); + } + + if (consoleDebug) + printf("#16 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + //int pathFindRefreshCellCount; + + //FowAlphaCellsLookupItem cachedFow; + //Vec2i cachedFowPos; + + crcForUnit.addInt(lastHarvestedResourcePos.x); + crcForUnit.addInt(lastHarvestedResourcePos.y); + + if (consoleDebug) + printf("#17 Unit: %d CRC: %u\n", id, crcForUnit.getSum()); + + if (this->getParticleInfo() != "") { + crcForUnit.addString(this->getParticleInfo()); + } + + crcForUnit.addInt((int) attackParticleSystems.size()); + if (isNetworkCRCEnabled() == true) { + for (unsigned int index = 0; index < attackParticleSystems.size(); + ++index) { + ParticleSystem *ps = attackParticleSystems[index]; + if (ps != NULL && + Renderer::getInstance().validateParticleSystemStillExists(ps, + rsGame) + == true) { + uint32 crc = ps->getCRC().getSum(); + crcForUnit.addBytes(&crc, sizeof(uint32)); + } + } + } + + if (this->networkCRCParticleLogInfo != "") { + crcForUnit.addString(this->networkCRCParticleLogInfo); + } + + return crcForUnit; + } + + } + } //end namespace diff --git a/source/glest_game/type_instances/unit.h b/source/glest_game/type_instances/unit.h index 0e5840cad..da2846fbd 100644 --- a/source/glest_game/type_instances/unit.h +++ b/source/glest_game/type_instances/unit.h @@ -28,1237 +28,1088 @@ //#define LEAK_CHECK_UNITS -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - using Shared::Graphics::ParticleSystem; - using Shared::Graphics::UnitParticleSystem; - using Shared::Graphics::Vec4f; - using Shared::Graphics::Vec2f; - using Shared::Graphics::Vec3f; - using Shared::Graphics::Vec2i; - using Shared::Graphics::Model; - using Shared::PlatformCommon::Chrono; - using Shared::PlatformCommon::ValueCheckerVault; + using Shared::Graphics::ParticleSystem; + using Shared::Graphics::UnitParticleSystem; + using Shared::Graphics::Vec4f; + using Shared::Graphics::Vec2f; + using Shared::Graphics::Vec3f; + using Shared::Graphics::Vec2i; + using Shared::Graphics::Model; + using Shared::PlatformCommon::Chrono; + using Shared::PlatformCommon::ValueCheckerVault; - class Map; -//class Faction; - class Unit; - class Command; - class SkillType; - class ResourceType; - class CommandType; - class SkillType; - class UnitType; - class TotalUpgrade; - class UpgradeType; - class Level; - class MorphCommandType; - class Game; - class Unit; - class GameSettings; - class World; + class Map; + //class Faction; + class Unit; + class Command; + class SkillType; + class ResourceType; + class CommandType; + class SkillType; + class UnitType; + class TotalUpgrade; + class UpgradeType; + class Level; + class MorphCommandType; + class Game; + class Unit; + class GameSettings; + class World; - enum CommandResult - { - crSuccess, - crFailRes, - crFailReqs, - crFailUnitCount, - crFailUndefined, - crSomeFailed - }; + enum CommandResult { + crSuccess, + crFailRes, + crFailReqs, + crFailUnitCount, + crFailUndefined, + crSomeFailed + }; - enum InterestingUnitType - { - iutIdleHarvester, - iutBuiltBuilding, - iutProducer, - iutDamaged, - iutStore - }; + enum InterestingUnitType { + iutIdleHarvester, + iutBuiltBuilding, + iutProducer, + iutDamaged, + iutStore + }; - enum CauseOfDeathType - { - ucodNone, - ucodAttacked, - ucodAttackBoost, - ucodStarvedResource, - ucodStarvedRegeneration - }; + enum CauseOfDeathType { + ucodNone, + ucodAttacked, + ucodAttackBoost, + ucodStarvedResource, + ucodStarvedRegeneration + }; - class UnitBuildInfo - { - public: - UnitBuildInfo () - { - unit = NULL; - //pos; - buildUnit = NULL; - } - const Unit *unit; - CardinalDir facing; - Vec2i pos; - const UnitType *buildUnit; - }; + class UnitBuildInfo { + public: + UnitBuildInfo() { + unit = NULL; + //pos; + buildUnit = NULL; + } + const Unit *unit; + CardinalDir facing; + Vec2i pos; + const UnitType *buildUnit; + }; -// ===================================================== -// class UnitObserver -// ===================================================== + // ===================================================== + // class UnitObserver + // ===================================================== - class UnitObserver - { - public: - enum Event - { - eKill - }; + class UnitObserver { + public: + enum Event { + eKill + }; - public: - virtual ~ UnitObserver () - { - } - virtual void unitEvent (Event event, const Unit * unit) = 0; + public: + virtual ~UnitObserver() { + } + virtual void unitEvent(Event event, const Unit * unit) = 0; - virtual void saveGame (XmlNode * rootNode) const = 0; - }; + virtual void saveGame(XmlNode * rootNode) const = 0; + }; -// ===================================================== -// class UnitReference -// ===================================================== + // ===================================================== + // class UnitReference + // ===================================================== - class UnitReference - { - private: - int id; - Faction *faction; + class UnitReference { + private: + int id; + Faction *faction; - public: - UnitReference (); + public: + UnitReference(); - UnitReference & operator= (const Unit * unit); - Unit *getUnit () const; + UnitReference & operator= (const Unit * unit); + Unit *getUnit() const; - int getUnitId () const - { - return id; - } - Faction *getUnitFaction () const - { - return faction; - } + int getUnitId() const { + return id; + } + Faction *getUnitFaction() const { + return faction; + } - void saveGame (XmlNode * rootNode); - void loadGame (const XmlNode * rootNode, World * world); - }; + void saveGame(XmlNode * rootNode); + void loadGame(const XmlNode * rootNode, World * world); + }; - class UnitPathInterface - { + class UnitPathInterface { - public: - UnitPathInterface () - { - } - virtual ~ UnitPathInterface () - { - } + public: + UnitPathInterface() { + } + virtual ~UnitPathInterface() { + } - virtual bool isBlocked () const = 0; - virtual bool isEmpty () const = 0; - virtual bool isStuck () const = 0; + virtual bool isBlocked() const = 0; + virtual bool isEmpty() const = 0; + virtual bool isStuck() const = 0; - virtual void clear () = 0; - virtual void clearBlockCount () = 0; - virtual void incBlockCount () = 0; - virtual void add (const Vec2i & path) = 0; - //virtual Vec2i pop() = 0; - virtual int getBlockCount () const = 0; - virtual int getQueueCount () const = 0; + virtual void clear() = 0; + virtual void clearBlockCount() = 0; + virtual void incBlockCount() = 0; + virtual void add(const Vec2i & path) = 0; + //virtual Vec2i pop() = 0; + virtual int getBlockCount() const = 0; + virtual int getQueueCount() const = 0; - virtual vector < Vec2i > getQueue () const = 0; + virtual vector < Vec2i > getQueue() const = 0; - virtual std::string toString () const = 0; + virtual std::string toString() const = 0; - virtual void setMap (Map * value) = 0; - virtual Map *getMap () = 0; + virtual void setMap(Map * value) = 0; + virtual Map *getMap() = 0; - virtual void saveGame (XmlNode * rootNode) = 0; - virtual void loadGame (const XmlNode * rootNode) = 0; + virtual void saveGame(XmlNode * rootNode) = 0; + virtual void loadGame(const XmlNode * rootNode) = 0; - virtual void clearCaches () = 0; + virtual void clearCaches() = 0; - virtual Checksum getCRC () = 0; - }; + virtual Checksum getCRC() = 0; + }; - class UnitPathBasic:public UnitPathInterface - { - private: - static const int maxBlockCount; - Map *map; + class UnitPathBasic :public UnitPathInterface { + private: + static const int maxBlockCount; + Map *map; # ifdef LEAK_CHECK_UNITS - static std::map < UnitPathBasic *, bool > mapMemoryList; + static std::map < UnitPathBasic *, bool > mapMemoryList; # endif - private: - int blockCount; - vector < Vec2i > pathQueue; + private: + int blockCount; + vector < Vec2i > pathQueue; - public: - UnitPathBasic (); - virtual ~ UnitPathBasic (); + public: + UnitPathBasic(); + virtual ~UnitPathBasic(); # ifdef LEAK_CHECK_UNITS - static void dumpMemoryList (); + static void dumpMemoryList(); # endif - virtual bool isBlocked () const; - virtual bool isEmpty () const; - virtual bool isStuck () const; + virtual bool isBlocked() const; + virtual bool isEmpty() const; + virtual bool isStuck() const; - virtual void clear (); - virtual void clearBlockCount () - { - blockCount = 0; - } - virtual void incBlockCount (); - virtual void add (const Vec2i & path); - Vec2i pop (bool removeFrontPos = true); - virtual int getBlockCount () const - { - return blockCount; - } - virtual int getQueueCount () const - { - return (int) pathQueue.size (); - } + virtual void clear(); + virtual void clearBlockCount() { + blockCount = 0; + } + virtual void incBlockCount(); + virtual void add(const Vec2i & path); + Vec2i pop(bool removeFrontPos = true); + virtual int getBlockCount() const { + return blockCount; + } + virtual int getQueueCount() const { + return (int) pathQueue.size(); + } - virtual vector < Vec2i > getQueue () const - { - return pathQueue; - } + virtual vector < Vec2i > getQueue() const { + return pathQueue; + } - virtual void setMap (Map * value) - { - map = value; - } - virtual Map *getMap () - { - return map; - } + virtual void setMap(Map * value) { + map = value; + } + virtual Map *getMap() { + return map; + } - virtual std::string toString () const; + virtual std::string toString() const; - virtual void saveGame (XmlNode * rootNode); - virtual void loadGame (const XmlNode * rootNode); - virtual void clearCaches (); + virtual void saveGame(XmlNode * rootNode); + virtual void loadGame(const XmlNode * rootNode); + virtual void clearCaches(); - virtual Checksum getCRC (); - }; + virtual Checksum getCRC(); + }; -// ===================================================== -// class UnitPath -// ===================================================== -/** Holds the next cells of a Unit movement - * @extends std::list - */ - class UnitPath:public list < Vec2i >, public UnitPathInterface - { - private: - static const int maxBlockCount = 10; /**< number of command updates to wait on a blocked path */ + // ===================================================== + // class UnitPath + // ===================================================== + /** Holds the next cells of a Unit movement + * @extends std::list + */ + class UnitPath :public list < Vec2i >, public UnitPathInterface { + private: + static const int maxBlockCount = 10; /**< number of command updates to wait on a blocked path */ - private: - int blockCount; /**< number of command updates this path has been blocked */ - Map *map; + private: + int blockCount; /**< number of command updates this path has been blocked */ + Map *map; - public: - UnitPath ():UnitPathInterface (), blockCount (0), map (NULL) - { - } /**< Construct path object */ + public: + UnitPath() :UnitPathInterface(), blockCount(0), map(NULL) { + } /**< Construct path object */ - virtual bool isBlocked () const - { - return blockCount >= maxBlockCount; - } /**< is this path blocked */ - virtual bool isEmpty () const - { - return list < Vec2i >::empty (); - } /**< is path empty */ - virtual bool isStuck () const - { - return false; - } + virtual bool isBlocked() const { + return blockCount >= maxBlockCount; + } /**< is this path blocked */ + virtual bool isEmpty() const { + return list < Vec2i >::empty(); + } /**< is path empty */ + virtual bool isStuck() const { + return false; + } - int size () const - { - return (int) list < Vec2i >::size (); - } /**< size of path */ - virtual void clear () - { - list < Vec2i >::clear (); - blockCount = 0; - } /**< clear the path */ - virtual void clearBlockCount () - { - blockCount = 0; - } - virtual void incBlockCount () - { - ++blockCount; - } /**< increment block counter */ - virtual void push (Vec2i & pos) - { - push_front (pos); - } /**< push onto front of path */ - bool empty () const - { - return list < Vec2i >::empty (); - } /**< is path empty */ - virtual void add (const Vec2i & pos) - { - push_front (pos); - } /**< push onto front of path */ + int size() const { + return (int) list < Vec2i >::size(); + } /**< size of path */ + virtual void clear() { + list < Vec2i >::clear(); + blockCount = 0; + } /**< clear the path */ + virtual void clearBlockCount() { + blockCount = 0; + } + virtual void incBlockCount() { + ++blockCount; + } /**< increment block counter */ + virtual void push(Vec2i & pos) { + push_front(pos); + } /**< push onto front of path */ + bool empty() const { + return list < Vec2i >::empty(); + } /**< is path empty */ + virtual void add(const Vec2i & pos) { + push_front(pos); + } /**< push onto front of path */ # if 0 - // old style, to work with original PathFinder - Vec2i peek () - { - return back (); - } /**< peek at the next position */ - void pop () - { - this->pop_back (); - } /**< pop the next position off the path */ + // old style, to work with original PathFinder + Vec2i peek() { + return back(); + } /**< peek at the next position */ + void pop() { + this->pop_back(); + } /**< pop the next position off the path */ # else - // new style - Vec2i peek () - { - return front (); - } /**< peek at the next position */ - //virtual Vec2i pop() { Vec2i p= front(); erase(begin()); return p; } /**< pop the next position off the path */ - void pop () - { - erase (begin ()); - } /**< pop the next position off the path */ + // new style + Vec2i peek() { + return front(); + } /**< peek at the next position */ + //virtual Vec2i pop() { Vec2i p= front(); erase(begin()); return p; } /**< pop the next position off the path */ + void pop() { + erase(begin()); + } /**< pop the next position off the path */ # endif - virtual int getBlockCount () const - { - return blockCount; - } - virtual int getQueueCount () const - { - return this->size (); - } + virtual int getBlockCount() const { + return blockCount; + } + virtual int getQueueCount() const { + return this->size(); + } - virtual vector < Vec2i > getQueue () const - { - vector < Vec2i > result; - for (list < Vec2i >::const_iterator iter = this->begin (); - iter != this->end (); ++iter) - { - result.push_back (*iter); - } - return result; - } + virtual vector < Vec2i > getQueue() const { + vector < Vec2i > result; + for (list < Vec2i >::const_iterator iter = this->begin(); + iter != this->end(); ++iter) { + result.push_back(*iter); + } + return result; + } - virtual void setMap (Map * value) - { - map = value; - } - virtual Map *getMap () - { - return map; - } + virtual void setMap(Map * value) { + map = value; + } + virtual Map *getMap() { + return map; + } - virtual std::string toString () const; + virtual std::string toString() const; - virtual void saveGame (XmlNode * rootNode) - { - }; - virtual void loadGame (const XmlNode * rootNode) - { - }; - virtual void clearCaches () - { - }; + virtual void saveGame(XmlNode * rootNode) { + }; + virtual void loadGame(const XmlNode * rootNode) { + }; + virtual void clearCaches() { + }; - virtual Checksum getCRC () - { - return Checksum (); - }; - }; + virtual Checksum getCRC() { + return Checksum(); + }; + }; - class WaypointPath:public list < Vec2i > - { - public: - WaypointPath () - { - } - void push (const Vec2i & pos) - { - push_front (pos); - } - Vec2i peek () const - { - return front (); - } - void pop () - { - erase (begin ()); - } - //void condense(); - }; + class WaypointPath :public list < Vec2i > { + public: + WaypointPath() { + } + void push(const Vec2i & pos) { + push_front(pos); + } + Vec2i peek() const { + return front(); + } + void pop() { + erase(begin()); + } + //void condense(); + }; -// =============================== -// class Unit -// -/// A game unit or building -// =============================== + // =============================== + // class Unit + // + /// A game unit or building + // =============================== - class UnitAttackBoostEffect - { - private: - int unitId; - const Unit *unitPtr; + class UnitAttackBoostEffect { + private: + int unitId; + const Unit *unitPtr; - const Unit *source; + const Unit *source; - void applyLoadedAttackBoostParticles (UnitParticleSystemType * upstPtr, - const XmlNode * node, - Unit * unit); - public: + void applyLoadedAttackBoostParticles(UnitParticleSystemType * upstPtr, + const XmlNode * node, + Unit * unit); + public: - UnitAttackBoostEffect (); - virtual ~ UnitAttackBoostEffect (); + UnitAttackBoostEffect(); + virtual ~UnitAttackBoostEffect(); - const AttackBoost *boost; - //const Unit *source; - const Unit *getSource (); - void setSource (const Unit * unit); - UnitParticleSystem *ups; - UnitParticleSystemType *upst; + const AttackBoost *boost; + //const Unit *source; + const Unit *getSource(); + void setSource(const Unit * unit); + UnitParticleSystem *ups; + UnitParticleSystemType *upst; - virtual void saveGame (XmlNode * rootNode); - virtual void loadGame (const XmlNode * rootNode, Unit * unit, - World * world, bool applyToOriginator); - }; + virtual void saveGame(XmlNode * rootNode); + virtual void loadGame(const XmlNode * rootNode, Unit * unit, + World * world, bool applyToOriginator); + }; - class UnitAttackBoostEffectOriginator - { - public: + class UnitAttackBoostEffectOriginator { + public: - UnitAttackBoostEffectOriginator (); - virtual ~ UnitAttackBoostEffectOriginator (); + UnitAttackBoostEffectOriginator(); + virtual ~UnitAttackBoostEffectOriginator(); - const SkillType *skillType; - std::vector < int >currentAttackBoostUnits; - UnitAttackBoostEffect *currentAppliedEffect; + const SkillType *skillType; + std::vector < int >currentAttackBoostUnits; + UnitAttackBoostEffect *currentAppliedEffect; - virtual void saveGame (XmlNode * rootNode); - virtual void loadGame (const XmlNode * rootNode, Unit * unit, - World * world); - }; + virtual void saveGame(XmlNode * rootNode); + virtual void loadGame(const XmlNode * rootNode, Unit * unit, + World * world); + }; - class Unit:public BaseColorPickEntity, ValueCheckerVault, - public ParticleOwner - { - private: - typedef list < Command * >Commands; - typedef list < UnitObserver * >Observers; - typedef vector < UnitParticleSystem * >UnitParticleSystems; + class Unit :public BaseColorPickEntity, ValueCheckerVault, + public ParticleOwner { + private: + typedef list < Command * >Commands; + typedef list < UnitObserver * >Observers; + typedef vector < UnitParticleSystem * >UnitParticleSystems; # ifdef LEAK_CHECK_UNITS - static std::map < Unit *, bool > mapMemoryList; + static std::map < Unit *, bool > mapMemoryList; # endif - static const float ANIMATION_SPEED_MULTIPLIER; - static const int64 PROGRESS_SPEED_MULTIPLIER; + static const float ANIMATION_SPEED_MULTIPLIER; + static const int64 PROGRESS_SPEED_MULTIPLIER; - public: - static const int speedDivider; - static const int maxDeadCount; - static const int invalidId; + public: + static const int speedDivider; + static const int maxDeadCount; + static const int invalidId; # ifdef LEAK_CHECK_UNITS - static std::map < UnitPathInterface *, int >mapMemoryList2; - static void dumpMemoryList (); + static std::map < UnitPathInterface *, int >mapMemoryList2; + static void dumpMemoryList(); # endif - private: - const int32 id; - int32 hp; - int32 ep; - int32 loadCount; - int32 deadCount; - //float progress; //between 0 and 1 - int64 progress; //between 0 and 1 - int64 lastAnimProgress; //between 0 and 1 - int64 animProgress; //between 0 and 1 - float highlight; - // Progress2 keeps track of update/ upgrade progress of a unit. - int32 progress2; - int32 kills; - int32 enemyKills; - bool morphFieldsBlocked; - int oldTotalSight; - - UnitReference targetRef; - - Field currField; - Field targetField; - const Level *level; - - Vec2i pos; - Vec2i lastPos; - Vec2i targetPos; //absolute target pos - Vec3f targetVec; - Vec2i meetingPos; - - float lastRotation; //in degrees - float targetRotation; - float rotation; - float targetRotationZ; - float targetRotationX; - float rotationZ; - float rotationX; - - const UnitType *preMorph_type; - const UnitType *type; - const ResourceType *loadType; - const SkillType *currSkill; - int32 lastModelIndexForCurrSkillType; - int32 animationRandomCycleCount; - - bool toBeUndertaken; - bool alive; - bool showUnitParticles; - - Faction *faction; - ParticleSystem *fire; - TotalUpgrade totalUpgrade; - Map *map; - - UnitPathInterface *unitPath; - WaypointPath waypointPath; - - Commands commands; - Observers observers; - vector < UnitParticleSystem * >unitParticleSystems; - vector < UnitParticleSystemType * >queuedUnitParticleSystemTypes; - - UnitParticleSystems damageParticleSystems; - std::map < int, UnitParticleSystem * >damageParticleSystemsInUse; - - vector < ParticleSystem * >fireParticleSystems; - vector < UnitParticleSystem * >smokeParticleSystems; - vector < ParticleSystem * >attackParticleSystems; - - CardinalDir modelFacing; - - std::string lastSynchDataString; - std::string lastFile; - int32 lastLine; - std::string lastSource; - int32 lastRenderFrame; - bool visible; - - int retryCurrCommandCount; - - Vec3f screenPos; - string currentUnitTitle; - - bool inBailOutAttempt; - // This buffer stores a list of bad harvest cells, along with the start - // time of when it was detected. Typically this may be due to a unit - // constantly getting blocked from getting to the resource so this - // list may be used to tell areas of the game to ignore those cells for a - // period of time - //std::vector > badHarvestPosList; - std::map < Vec2i, int >badHarvestPosList; - //time_t lastBadHarvestListPurge; - std::pair < Vec2i, int >lastHarvestResourceTarget; - - //std::pair > currentTargetPathTaken; - - static Game *game; - - bool ignoreCheckCommand; - - uint32 lastStuckFrame; - Vec2i lastStuckPos; - - uint32 lastPathfindFailedFrame; - Vec2i lastPathfindFailedPos; - bool usePathfinderExtendedMaxNodes; - int32 maxQueuedCommandDisplayCount; - - UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; - - std::vector < UnitAttackBoostEffect * >currentAttackBoostEffects; - - Mutex *mutexCommands; - - //static Mutex mutexDeletedUnits; - //static std::map deletedUnits; - - bool changedActiveCommand; - uint32 lastChangedActiveCommandFrame; - uint32 changedActiveCommandFrame; - - int32 lastAttackerUnitId; - int32 lastAttackedUnitId; - CauseOfDeathType causeOfDeath; - - uint32 pathfindFailedConsecutiveFrameCount; - Vec2i currentPathFinderDesiredFinalPos; - - RandomGen random; - int32 pathFindRefreshCellCount; - - FowAlphaCellsLookupItem cachedFow; - Vec2i cachedFowPos; - - ExploredCellsLookupItem cacheExploredCells; - std::pair < Vec2i, int >cacheExploredCellsKey; - - Vec2i lastHarvestedResourcePos; - - string networkCRCLogInfo; - string networkCRCParticleLogInfo; - vector < string > networkCRCDecHpList; - vector < string > networkCRCParticleInfoList; - - public: - Unit (int id, UnitPathInterface * path, const Vec2i & pos, - const UnitType * type, Faction * faction, Map * map, - CardinalDir placeFacing); - virtual ~ Unit (); - - //static bool isUnitDeleted(void *unit); - - static void setGame (Game * value) - { - game = value; - } - - inline int getPathFindRefreshCellCount () const - { - return pathFindRefreshCellCount; - } - - void setCurrentPathFinderDesiredFinalPos (const Vec2i & finalPos) - { - currentPathFinderDesiredFinalPos = finalPos; - } - Vec2i getCurrentPathFinderDesiredFinalPos () const - { - return currentPathFinderDesiredFinalPos; - } - - const UnitAttackBoostEffectOriginator & - getAttackBoostOriginatorEffect () const - { - return currentAttackBoostOriginatorEffect; - } - bool unitHasAttackBoost (const AttackBoost * boost, - const Unit * source); - - inline uint32 getPathfindFailedConsecutiveFrameCount () const - { - return pathfindFailedConsecutiveFrameCount; - } - inline void incrementPathfindFailedConsecutiveFrameCount () - { - pathfindFailedConsecutiveFrameCount++; - } - inline void resetPathfindFailedConsecutiveFrameCount () - { - pathfindFailedConsecutiveFrameCount = 0; - } - - const FowAlphaCellsLookupItem & getCachedFow () const - { - return cachedFow; - } - FowAlphaCellsLookupItem getFogOfWarRadius (bool useCache) const; - void calculateFogOfWarRadius (bool forceRefresh = false); - - //queries - Command *getCurrrentCommandThreadSafe (); - void setIgnoreCheckCommand (bool value) - { - ignoreCheckCommand = value; - } - inline bool getIgnoreCheckCommand () const - { - return ignoreCheckCommand; - } - inline int getId () const - { - return id; - } - inline Field getCurrField () const - { - return currField; - } - inline int getLoadCount () const - { - return loadCount; - } - - //inline int getLastAnimProgress() const {return lastAnimProgress;} - //inline int getAnimProgress() const {return animProgress;} - inline float getLastAnimProgressAsFloat () const - { - return static_cast < - float >(lastAnimProgress) / ANIMATION_SPEED_MULTIPLIER; - } - inline float getAnimProgressAsFloat () const - { - return static_cast < - float >(animProgress) / ANIMATION_SPEED_MULTIPLIER; - } - - inline float getHightlight () const - { - return highlight; - } - inline int getProgress2 () const - { - return progress2; - } - inline int getFactionIndex () const - { - return faction->getIndex (); - } - inline int getTeam () const - { - return faction->getTeam (); - } - inline int getHp () const - { - return hp; - } - inline int getEp () const - { - return ep; - } - int getProductionPercent () const; - float getProgressRatio () const; - float getHpRatio () const; - float getEpRatio () const; - inline bool getToBeUndertaken () const - { - return toBeUndertaken; - } - inline Vec2i getTargetPos () const - { - return targetPos; - } - inline Vec3f getTargetVec () const - { - return targetVec; - } - inline Field getTargetField () const - { - return targetField; - } - inline Vec2i getMeetingPos () const - { - return meetingPos; - } - inline Faction *getFaction () const - { - return faction; - } - inline const ResourceType *getLoadType () const - { - return loadType; - } - - inline const UnitType *getType () const - { - return type; - } - void setType (const UnitType * newType); - inline const UnitType *getPreMorphType () const - { - return preMorph_type; - } - - inline const SkillType *getCurrSkill () const - { - return currSkill; - } - inline const TotalUpgrade *getTotalUpgrade () const - { - return &totalUpgrade; - } - inline float getRotation () const - { - return rotation; - } - float getRotationX () const; - float getRotationZ () const; - ParticleSystem *getFire () const; - inline int getKills () const - { - return kills; - } - inline int getEnemyKills () const - { - return enemyKills; - } - inline const Level *getLevel () const - { - return level; - } - const Level *getNextLevel () const; - string getFullName (bool translatedValue) const; - inline const UnitPathInterface *getPath () const - { - return unitPath; - } - inline UnitPathInterface *getPath () - { - return unitPath; - } - inline WaypointPath *getWaypointPath () - { - return &waypointPath; - } - - inline int getLastAttackerUnitId () const - { - return lastAttackerUnitId; - } - inline void setLastAttackerUnitId (int unitId) - { - lastAttackerUnitId = unitId; - } - - inline int getLastAttackedUnitId () const - { - return lastAttackedUnitId; - } - inline void setLastAttackedUnitId (int unitId) - { - lastAttackedUnitId = unitId; - } - - inline CauseOfDeathType getCauseOfDeath () const - { - return causeOfDeath; - } - inline void setCauseOfDeath (CauseOfDeathType cause) - { - causeOfDeath = cause; - } - - //pos - inline Vec2i getPosNotThreadSafe () const - { - return pos; - } - Vec2i getPos (); - Vec2i getPosWithCellMapSet () const; - inline Vec2i getLastPos () const - { - return lastPos; - } - Vec2i getCenteredPos () const; - Vec2f getFloatCenteredPos () const; - Vec2i getCellPos () const; - - //is - inline bool isHighlighted () const - { - return highlight > 0.f; - } - inline bool isDead () const - { - return !alive; - } - inline bool isAlive () const - { - return alive; - } - bool isOperative () const; - bool isBeingBuilt () const; - bool isBuilt () const; - bool isBuildCommandPending () const; - UnitBuildInfo getBuildCommandPendingInfo () const; - - bool isAnimProgressBound () const; - bool isPutrefacting () const - { - return deadCount != 0; - } - bool isAlly (const Unit * unit) const; - bool isDamaged () const; - bool isInteresting (InterestingUnitType iut) const; - - //set - //void setCurrField(Field currField); - void setCurrSkill (const SkillType * currSkill); - void setCurrSkill (SkillClass sc); - - void setMorphFieldsBlocked (bool value) - { - this->morphFieldsBlocked = value; - } - bool getMorphFieldsBlocked () const - { - return morphFieldsBlocked; - } - - inline void setLastHarvestedResourcePos (Vec2i pos) - { - this->lastHarvestedResourcePos = pos; - } - inline Vec2i getLastHarvestedResourcePos () const - { - return this->lastHarvestedResourcePos; - } - - inline void setLoadCount (int loadCount) - { - this->loadCount = loadCount; - } - inline void setLoadType (const ResourceType * loadType) - { - this->loadType = loadType; - } - // resetProgress2 resets produce and upgrade progress. - inline void resetProgress2 () - { - this->progress2 = 0; - } - void setPos (const Vec2i & pos, bool clearPathFinder = - false, bool threaded = false); - void refreshPos (bool forceRefresh = false); - void setTargetPos (const Vec2i & targetPos, bool threaded = false); - void setTarget (const Unit * unit); - //void setTargetVec(const Vec3f &targetVec); - void setMeetingPos (const Vec2i & meetingPos); - void setVisible (const bool visible); - inline bool getVisible () const - { - return visible; - } - - //render related - //const Model *getCurrentModel(); - Model *getCurrentModelPtr (); - Vec3f getCurrMidHeightVector () const; - Vec3f getCurrVectorForParticlesystems () const; - Vec3f getCurrVectorAsTarget () const; - Vec3f getCurrBurnVector () const; - Vec3f getCurrVectorFlat () const; - Vec3f getVectorFlat (const Vec2i & lastPosValue, - const Vec2i & curPosValue) const; - - //command related - bool anyCommand (bool validateCommandtype = false) const; - inline Command *getCurrCommand () const - { - if (commands.empty () == false) - { - return commands.front (); - } - return NULL; - } - void replaceCurrCommand (Command * cmd); - int getCountOfProducedUnits (const UnitType * ut) const; - unsigned int getCommandSize () const; - std::pair < CommandResult, string > giveCommand (Command * command, bool tryQueue = false); //give a command - CommandResult finishCommand (); //command finished - CommandResult cancelCommand (); //cancel canceled - - //lifecycle - void create (bool startingUnit = false); - void born (const CommandType * ct); - void kill (); - void undertake (); - - //observers - void addObserver (UnitObserver * unitObserver); - //void removeObserver(UnitObserver *unitObserver); - void notifyObservers (UnitObserver::Event event); - - //other - void resetHighlight (); - const CommandType *computeCommandType (const Vec2i & pos, - const Unit * targetUnit = - NULL) const; - string getDesc (bool translatedValue) const; - string getDescExtension (bool translatedValue) const; - bool computeEp (); - //bool computeHp(); - bool repair (); - bool decHp (int i); - int update2 (); - bool update (); - void tick (); - RandomGen *getRandom (bool threadAccessAllowed = false); - - bool applyAttackBoost (const AttackBoost * boost, const Unit * source); - void deapplyAttackBoost (const AttackBoost * boost, - const Unit * source); - - void applyUpgrade (const UpgradeType * upgradeType); - void computeTotalUpgrade (); - void incKills (int team); - bool morph (const MorphCommandType * mct, int frameIndex); - std::pair < CommandResult, - string > checkCommand (Command * command) const; - void applyCommand (Command * command); - - void setModelFacing (CardinalDir value); - inline CardinalDir getModelFacing () const - { - return modelFacing; - } - - bool isMeetingPointSettable () const; - - inline int getLastRenderFrame () const - { - return lastRenderFrame; - } - inline void setLastRenderFrame (int value) - { - lastRenderFrame = value; - } - - inline int getRetryCurrCommandCount () const - { - return retryCurrCommandCount; - } - inline void setRetryCurrCommandCount (int value) - { - retryCurrCommandCount = value; - } - - inline Vec3f getScreenPos () const - { - return screenPos; - } - void setScreenPos (Vec3f value) - { - screenPos = value; - } - - inline string getCurrentUnitTitle () const - { - return currentUnitTitle; - } - void setCurrentUnitTitle (string value) - { - currentUnitTitle = value; - } - - void exploreCells (bool forceRefresh = false); - - inline bool getInBailOutAttempt () const - { - return inBailOutAttempt; - } - inline void setInBailOutAttempt (bool value) - { - inBailOutAttempt = value; - } - - //std::vector > getBadHarvestPosList() const { return badHarvestPosList; } - //void setBadHarvestPosList(std::vector > value) { badHarvestPosList = value; } - void addBadHarvestPos (const Vec2i & value); - //void removeBadHarvestPos(const Vec2i &value); - inline bool isBadHarvestPos (const Vec2i & value, bool checkPeerUnits = - true) const - { - bool result = false; - if (badHarvestPosList.empty () == true) - { - return result; - } - - std::map < Vec2i, int >::const_iterator iter = - badHarvestPosList.find (value); - if (iter != badHarvestPosList.end ()) - { - result = true; - } - else if (checkPeerUnits == true) - { - // Check if any other units of similar type have this position tagged - // as bad? - for (int i = 0; i < this->getFaction ()->getUnitCount (); ++i) - { - Unit *peerUnit = this->getFaction ()->getUnit (i); - if (peerUnit != NULL && peerUnit->getId () != this->getId () && - peerUnit->getType ()->hasCommandClass (ccHarvest) == true && - peerUnit->getType ()->getSize () <= - this->getType ()->getSize ()) - { - if (peerUnit->isBadHarvestPos (value, false) == true) - { - result = true; - break; - } - } - } - } - - return result; - } - void cleanupOldBadHarvestPos (); - - void setLastHarvestResourceTarget (const Vec2i * pos); - inline std::pair < Vec2i, int >getLastHarvestResourceTarget () const - { - return lastHarvestResourceTarget; - } - - //std::pair > getCurrentTargetPathTaken() const { return currentTargetPathTaken; } - //void addCurrentTargetPathTakenCell(const Vec2i &target,const Vec2i &cell); - - void logSynchData (string file, int line, string source = ""); - void logSynchDataThreaded (string file, int line, string source = ""); - - std::string toString (bool crcMode = false) const; - bool needToUpdate (); - float getProgressAsFloat () const; - int64 getUpdateProgress (); - int64 getDiagonalFactor (); - int64 getHeightFactor (int64 speedMultiplier = - PROGRESS_SPEED_MULTIPLIER); - int64 getSpeedDenominator (int64 updateFPS); - bool isChangedActiveCommand () const - { - return changedActiveCommand; - } - - bool isLastStuckFrameWithinCurrentFrameTolerance (bool evalMode); - inline uint32 getLastStuckFrame () const - { - return lastStuckFrame; - } - //inline void setLastStuckFrame(uint32 value) { lastStuckFrame = value; } - void setLastStuckFrameToCurrentFrame (); - - inline Vec2i getLastStuckPos () const - { - return lastStuckPos; - } - inline void setLastStuckPos (Vec2i pos) - { - lastStuckPos = pos; - } - - bool isLastPathfindFailedFrameWithinCurrentFrameTolerance () const; - inline uint32 getLastPathfindFailedFrame () const - { - return lastPathfindFailedFrame; - } - inline void setLastPathfindFailedFrame (uint32 value) - { - lastPathfindFailedFrame = value; - } - void setLastPathfindFailedFrameToCurrentFrame (); - - inline Vec2i getLastPathfindFailedPos () const - { - return lastPathfindFailedPos; - } - inline void setLastPathfindFailedPos (Vec2i pos) - { - lastPathfindFailedPos = pos; - } - - inline bool getUsePathfinderExtendedMaxNodes () const - { - return usePathfinderExtendedMaxNodes; - } - inline void setUsePathfinderExtendedMaxNodes (bool value) - { - usePathfinderExtendedMaxNodes = value; - } - - void updateTimedParticles (); - void setMeshPosInParticleSystem (UnitParticleSystem * ups); - - virtual string getUniquePickName () const; - void saveGame (XmlNode * rootNode); - static Unit *loadGame (const XmlNode * rootNode, - GameSettings * settings, Faction * faction, - World * world); - - void clearCaches (); - bool showTranslatedTechTree () const; - - void addAttackParticleSystem (ParticleSystem * ps); - - Checksum getCRC (); - - virtual void end (ParticleSystem * particleSystem); - virtual void logParticleInfo (string info); - void setNetworkCRCParticleLogInfo (string networkCRCParticleLogInfo) - { - this->networkCRCParticleLogInfo = networkCRCParticleLogInfo; - } - void clearParticleInfo (); - void addNetworkCRCDecHp (string info); - void clearNetworkCRCDecHpList (); - - private: - - void cleanupAllParticlesystems (); - bool isNetworkCRCEnabled (); - string getNetworkCRCDecHpList () const; - string getParticleInfo () const; - - float computeHeight (const Vec2i & pos) const; - void calculateXZRotation (); - void AnimCycleStarts (); - void updateTarget (); - void clearCommands (); - void deleteQueuedCommand (Command * command); - CommandResult undoCommand (Command * command); - void stopDamageParticles (bool force); - void startDamageParticles (); - - uint32 getFrameCount () const; - - void checkCustomizedParticleTriggers (bool force); - void checkCustomizedUnitParticleTriggers (); - void checkCustomizedUnitParticleListTriggers (const - UnitParticleSystemTypes & - unitParticleSystemTypesList, - bool - applySkillChangeParticles); - void queueTimedParticles (const UnitParticleSystemTypes & - unitParticleSystemTypesList); - - bool checkModelStateInfoForNewHpValue (); - void checkUnitLevel (); - - void morphAttackBoosts (Unit * unit); - - int64 getUpdatedProgress (int64 currentProgress, int64 updateFPS, - int64 speed, int64 diagonalFactor, - int64 heightFactor); - - void logSynchDataCommon (string file, int line, string source = - "", bool threadedMode = false); - void updateAttackBoostProgress (const Game * game); - - void setAlive (bool value); - }; - -}} // end namespace + private: + const int32 id; + int32 hp; + int32 ep; + int32 loadCount; + int32 deadCount; + //float progress; //between 0 and 1 + int64 progress; //between 0 and 1 + int64 lastAnimProgress; //between 0 and 1 + int64 animProgress; //between 0 and 1 + float highlight; + // Progress2 keeps track of update/ upgrade progress of a unit. + int32 progress2; + int32 kills; + int32 enemyKills; + bool morphFieldsBlocked; + int oldTotalSight; + + UnitReference targetRef; + + Field currField; + Field targetField; + const Level *level; + + Vec2i pos; + Vec2i lastPos; + Vec2i targetPos; //absolute target pos + Vec3f targetVec; + Vec2i meetingPos; + + float lastRotation; //in degrees + float targetRotation; + float rotation; + float targetRotationZ; + float targetRotationX; + float rotationZ; + float rotationX; + + const UnitType *preMorph_type; + const UnitType *type; + const ResourceType *loadType; + const SkillType *currSkill; + int32 lastModelIndexForCurrSkillType; + int32 animationRandomCycleCount; + + bool toBeUndertaken; + bool alive; + bool showUnitParticles; + + Faction *faction; + ParticleSystem *fire; + TotalUpgrade totalUpgrade; + Map *map; + + UnitPathInterface *unitPath; + WaypointPath waypointPath; + + Commands commands; + Observers observers; + vector < UnitParticleSystem * >unitParticleSystems; + vector < UnitParticleSystemType * >queuedUnitParticleSystemTypes; + + UnitParticleSystems damageParticleSystems; + std::map < int, UnitParticleSystem * >damageParticleSystemsInUse; + + vector < ParticleSystem * >fireParticleSystems; + vector < UnitParticleSystem * >smokeParticleSystems; + vector < ParticleSystem * >attackParticleSystems; + + CardinalDir modelFacing; + + std::string lastSynchDataString; + std::string lastFile; + int32 lastLine; + std::string lastSource; + int32 lastRenderFrame; + bool visible; + + int retryCurrCommandCount; + + Vec3f screenPos; + string currentUnitTitle; + + bool inBailOutAttempt; + // This buffer stores a list of bad harvest cells, along with the start + // time of when it was detected. Typically this may be due to a unit + // constantly getting blocked from getting to the resource so this + // list may be used to tell areas of the game to ignore those cells for a + // period of time + //std::vector > badHarvestPosList; + std::map < Vec2i, int >badHarvestPosList; + //time_t lastBadHarvestListPurge; + std::pair < Vec2i, int >lastHarvestResourceTarget; + + //std::pair > currentTargetPathTaken; + + static Game *game; + + bool ignoreCheckCommand; + + uint32 lastStuckFrame; + Vec2i lastStuckPos; + + uint32 lastPathfindFailedFrame; + Vec2i lastPathfindFailedPos; + bool usePathfinderExtendedMaxNodes; + int32 maxQueuedCommandDisplayCount; + + UnitAttackBoostEffectOriginator currentAttackBoostOriginatorEffect; + + std::vector < UnitAttackBoostEffect * >currentAttackBoostEffects; + + Mutex *mutexCommands; + + //static Mutex mutexDeletedUnits; + //static std::map deletedUnits; + + bool changedActiveCommand; + uint32 lastChangedActiveCommandFrame; + uint32 changedActiveCommandFrame; + + int32 lastAttackerUnitId; + int32 lastAttackedUnitId; + CauseOfDeathType causeOfDeath; + + uint32 pathfindFailedConsecutiveFrameCount; + Vec2i currentPathFinderDesiredFinalPos; + + RandomGen random; + int32 pathFindRefreshCellCount; + + FowAlphaCellsLookupItem cachedFow; + Vec2i cachedFowPos; + + ExploredCellsLookupItem cacheExploredCells; + std::pair < Vec2i, int >cacheExploredCellsKey; + + Vec2i lastHarvestedResourcePos; + + string networkCRCLogInfo; + string networkCRCParticleLogInfo; + vector < string > networkCRCDecHpList; + vector < string > networkCRCParticleInfoList; + + public: + Unit(int id, UnitPathInterface * path, const Vec2i & pos, + const UnitType * type, Faction * faction, Map * map, + CardinalDir placeFacing); + virtual ~Unit(); + + //static bool isUnitDeleted(void *unit); + + static void setGame(Game * value) { + game = value; + } + + inline int getPathFindRefreshCellCount() const { + return pathFindRefreshCellCount; + } + + void setCurrentPathFinderDesiredFinalPos(const Vec2i & finalPos) { + currentPathFinderDesiredFinalPos = finalPos; + } + Vec2i getCurrentPathFinderDesiredFinalPos() const { + return currentPathFinderDesiredFinalPos; + } + + const UnitAttackBoostEffectOriginator & + getAttackBoostOriginatorEffect() const { + return currentAttackBoostOriginatorEffect; + } + bool unitHasAttackBoost(const AttackBoost * boost, + const Unit * source); + + inline uint32 getPathfindFailedConsecutiveFrameCount() const { + return pathfindFailedConsecutiveFrameCount; + } + inline void incrementPathfindFailedConsecutiveFrameCount() { + pathfindFailedConsecutiveFrameCount++; + } + inline void resetPathfindFailedConsecutiveFrameCount() { + pathfindFailedConsecutiveFrameCount = 0; + } + + const FowAlphaCellsLookupItem & getCachedFow() const { + return cachedFow; + } + FowAlphaCellsLookupItem getFogOfWarRadius(bool useCache) const; + void calculateFogOfWarRadius(bool forceRefresh = false); + + //queries + Command *getCurrrentCommandThreadSafe(); + void setIgnoreCheckCommand(bool value) { + ignoreCheckCommand = value; + } + inline bool getIgnoreCheckCommand() const { + return ignoreCheckCommand; + } + inline int getId() const { + return id; + } + inline Field getCurrField() const { + return currField; + } + inline int getLoadCount() const { + return loadCount; + } + + //inline int getLastAnimProgress() const {return lastAnimProgress;} + //inline int getAnimProgress() const {return animProgress;} + inline float getLastAnimProgressAsFloat() const { + return static_cast < + float>(lastAnimProgress) / ANIMATION_SPEED_MULTIPLIER; + } + inline float getAnimProgressAsFloat() const { + return static_cast < + float>(animProgress) / ANIMATION_SPEED_MULTIPLIER; + } + + inline float getHightlight() const { + return highlight; + } + inline int getProgress2() const { + return progress2; + } + inline int getFactionIndex() const { + return faction->getIndex(); + } + inline int getTeam() const { + return faction->getTeam(); + } + inline int getHp() const { + return hp; + } + inline int getEp() const { + return ep; + } + int getProductionPercent() const; + float getProgressRatio() const; + float getHpRatio() const; + float getEpRatio() const; + inline bool getToBeUndertaken() const { + return toBeUndertaken; + } + inline Vec2i getTargetPos() const { + return targetPos; + } + inline Vec3f getTargetVec() const { + return targetVec; + } + inline Field getTargetField() const { + return targetField; + } + inline Vec2i getMeetingPos() const { + return meetingPos; + } + inline Faction *getFaction() const { + return faction; + } + inline const ResourceType *getLoadType() const { + return loadType; + } + + inline const UnitType *getType() const { + return type; + } + void setType(const UnitType * newType); + inline const UnitType *getPreMorphType() const { + return preMorph_type; + } + + inline const SkillType *getCurrSkill() const { + return currSkill; + } + inline const TotalUpgrade *getTotalUpgrade() const { + return &totalUpgrade; + } + inline float getRotation() const { + return rotation; + } + float getRotationX() const; + float getRotationZ() const; + ParticleSystem *getFire() const; + inline int getKills() const { + return kills; + } + inline int getEnemyKills() const { + return enemyKills; + } + inline const Level *getLevel() const { + return level; + } + const Level *getNextLevel() const; + string getFullName(bool translatedValue) const; + inline const UnitPathInterface *getPath() const { + return unitPath; + } + inline UnitPathInterface *getPath() { + return unitPath; + } + inline WaypointPath *getWaypointPath() { + return &waypointPath; + } + + inline int getLastAttackerUnitId() const { + return lastAttackerUnitId; + } + inline void setLastAttackerUnitId(int unitId) { + lastAttackerUnitId = unitId; + } + + inline int getLastAttackedUnitId() const { + return lastAttackedUnitId; + } + inline void setLastAttackedUnitId(int unitId) { + lastAttackedUnitId = unitId; + } + + inline CauseOfDeathType getCauseOfDeath() const { + return causeOfDeath; + } + inline void setCauseOfDeath(CauseOfDeathType cause) { + causeOfDeath = cause; + } + + //pos + inline Vec2i getPosNotThreadSafe() const { + return pos; + } + Vec2i getPos(); + Vec2i getPosWithCellMapSet() const; + inline Vec2i getLastPos() const { + return lastPos; + } + Vec2i getCenteredPos() const; + Vec2f getFloatCenteredPos() const; + Vec2i getCellPos() const; + + //is + inline bool isHighlighted() const { + return highlight > 0.f; + } + inline bool isDead() const { + return !alive; + } + inline bool isAlive() const { + return alive; + } + bool isOperative() const; + bool isBeingBuilt() const; + bool isBuilt() const; + bool isBuildCommandPending() const; + UnitBuildInfo getBuildCommandPendingInfo() const; + + bool isAnimProgressBound() const; + bool isPutrefacting() const { + return deadCount != 0; + } + bool isAlly(const Unit * unit) const; + bool isDamaged() const; + bool isInteresting(InterestingUnitType iut) const; + + //set + //void setCurrField(Field currField); + void setCurrSkill(const SkillType * currSkill); + void setCurrSkill(SkillClass sc); + + void setMorphFieldsBlocked(bool value) { + this->morphFieldsBlocked = value; + } + bool getMorphFieldsBlocked() const { + return morphFieldsBlocked; + } + + inline void setLastHarvestedResourcePos(Vec2i pos) { + this->lastHarvestedResourcePos = pos; + } + inline Vec2i getLastHarvestedResourcePos() const { + return this->lastHarvestedResourcePos; + } + + inline void setLoadCount(int loadCount) { + this->loadCount = loadCount; + } + inline void setLoadType(const ResourceType * loadType) { + this->loadType = loadType; + } + // resetProgress2 resets produce and upgrade progress. + inline void resetProgress2() { + this->progress2 = 0; + } + void setPos(const Vec2i & pos, bool clearPathFinder = + false, bool threaded = false); + void refreshPos(bool forceRefresh = false); + void setTargetPos(const Vec2i & targetPos, bool threaded = false); + void setTarget(const Unit * unit); + //void setTargetVec(const Vec3f &targetVec); + void setMeetingPos(const Vec2i & meetingPos); + void setVisible(const bool visible); + inline bool getVisible() const { + return visible; + } + + //render related + //const Model *getCurrentModel(); + Model *getCurrentModelPtr(); + Vec3f getCurrMidHeightVector() const; + Vec3f getCurrVectorForParticlesystems() const; + Vec3f getCurrVectorAsTarget() const; + Vec3f getCurrBurnVector() const; + Vec3f getCurrVectorFlat() const; + Vec3f getVectorFlat(const Vec2i & lastPosValue, + const Vec2i & curPosValue) const; + + //command related + bool anyCommand(bool validateCommandtype = false) const; + inline Command *getCurrCommand() const { + if (commands.empty() == false) { + return commands.front(); + } + return NULL; + } + void replaceCurrCommand(Command * cmd); + int getCountOfProducedUnits(const UnitType * ut) const; + unsigned int getCommandSize() const; + std::pair < CommandResult, string > giveCommand(Command * command, bool tryQueue = false); //give a command + CommandResult finishCommand(); //command finished + CommandResult cancelCommand(); //cancel canceled + + //lifecycle + void create(bool startingUnit = false); + void born(const CommandType * ct); + void kill(); + void undertake(); + + //observers + void addObserver(UnitObserver * unitObserver); + //void removeObserver(UnitObserver *unitObserver); + void notifyObservers(UnitObserver::Event event); + + //other + void resetHighlight(); + const CommandType *computeCommandType(const Vec2i & pos, + const Unit * targetUnit = + NULL) const; + string getDesc(bool translatedValue) const; + string getDescExtension(bool translatedValue) const; + bool computeEp(); + //bool computeHp(); + bool repair(); + bool decHp(int i); + int update2(); + bool update(); + void tick(); + RandomGen *getRandom(bool threadAccessAllowed = false); + + bool applyAttackBoost(const AttackBoost * boost, const Unit * source); + void deapplyAttackBoost(const AttackBoost * boost, + const Unit * source); + + void applyUpgrade(const UpgradeType * upgradeType); + void computeTotalUpgrade(); + void incKills(int team); + bool morph(const MorphCommandType * mct, int frameIndex); + std::pair < CommandResult, + string > checkCommand(Command * command) const; + void applyCommand(Command * command); + + void setModelFacing(CardinalDir value); + inline CardinalDir getModelFacing() const { + return modelFacing; + } + + bool isMeetingPointSettable() const; + + inline int getLastRenderFrame() const { + return lastRenderFrame; + } + inline void setLastRenderFrame(int value) { + lastRenderFrame = value; + } + + inline int getRetryCurrCommandCount() const { + return retryCurrCommandCount; + } + inline void setRetryCurrCommandCount(int value) { + retryCurrCommandCount = value; + } + + inline Vec3f getScreenPos() const { + return screenPos; + } + void setScreenPos(Vec3f value) { + screenPos = value; + } + + inline string getCurrentUnitTitle() const { + return currentUnitTitle; + } + void setCurrentUnitTitle(string value) { + currentUnitTitle = value; + } + + void exploreCells(bool forceRefresh = false); + + inline bool getInBailOutAttempt() const { + return inBailOutAttempt; + } + inline void setInBailOutAttempt(bool value) { + inBailOutAttempt = value; + } + + //std::vector > getBadHarvestPosList() const { return badHarvestPosList; } + //void setBadHarvestPosList(std::vector > value) { badHarvestPosList = value; } + void addBadHarvestPos(const Vec2i & value); + //void removeBadHarvestPos(const Vec2i &value); + inline bool isBadHarvestPos(const Vec2i & value, bool checkPeerUnits = + true) const { + bool result = false; + if (badHarvestPosList.empty() == true) { + return result; + } + + std::map < Vec2i, int >::const_iterator iter = + badHarvestPosList.find(value); + if (iter != badHarvestPosList.end()) { + result = true; + } else if (checkPeerUnits == true) { + // Check if any other units of similar type have this position tagged + // as bad? + for (int i = 0; i < this->getFaction()->getUnitCount(); ++i) { + Unit *peerUnit = this->getFaction()->getUnit(i); + if (peerUnit != NULL && peerUnit->getId() != this->getId() && + peerUnit->getType()->hasCommandClass(ccHarvest) == true && + peerUnit->getType()->getSize() <= + this->getType()->getSize()) { + if (peerUnit->isBadHarvestPos(value, false) == true) { + result = true; + break; + } + } + } + } + + return result; + } + void cleanupOldBadHarvestPos(); + + void setLastHarvestResourceTarget(const Vec2i * pos); + inline std::pair < Vec2i, int >getLastHarvestResourceTarget() const { + return lastHarvestResourceTarget; + } + + //std::pair > getCurrentTargetPathTaken() const { return currentTargetPathTaken; } + //void addCurrentTargetPathTakenCell(const Vec2i &target,const Vec2i &cell); + + void logSynchData(string file, int line, string source = ""); + void logSynchDataThreaded(string file, int line, string source = ""); + + std::string toString(bool crcMode = false) const; + bool needToUpdate(); + float getProgressAsFloat() const; + int64 getUpdateProgress(); + int64 getDiagonalFactor(); + int64 getHeightFactor(int64 speedMultiplier = + PROGRESS_SPEED_MULTIPLIER); + int64 getSpeedDenominator(int64 updateFPS); + bool isChangedActiveCommand() const { + return changedActiveCommand; + } + + bool isLastStuckFrameWithinCurrentFrameTolerance(bool evalMode); + inline uint32 getLastStuckFrame() const { + return lastStuckFrame; + } + //inline void setLastStuckFrame(uint32 value) { lastStuckFrame = value; } + void setLastStuckFrameToCurrentFrame(); + + inline Vec2i getLastStuckPos() const { + return lastStuckPos; + } + inline void setLastStuckPos(Vec2i pos) { + lastStuckPos = pos; + } + + bool isLastPathfindFailedFrameWithinCurrentFrameTolerance() const; + inline uint32 getLastPathfindFailedFrame() const { + return lastPathfindFailedFrame; + } + inline void setLastPathfindFailedFrame(uint32 value) { + lastPathfindFailedFrame = value; + } + void setLastPathfindFailedFrameToCurrentFrame(); + + inline Vec2i getLastPathfindFailedPos() const { + return lastPathfindFailedPos; + } + inline void setLastPathfindFailedPos(Vec2i pos) { + lastPathfindFailedPos = pos; + } + + inline bool getUsePathfinderExtendedMaxNodes() const { + return usePathfinderExtendedMaxNodes; + } + inline void setUsePathfinderExtendedMaxNodes(bool value) { + usePathfinderExtendedMaxNodes = value; + } + + void updateTimedParticles(); + void setMeshPosInParticleSystem(UnitParticleSystem * ups); + + virtual string getUniquePickName() const; + void saveGame(XmlNode * rootNode); + static Unit *loadGame(const XmlNode * rootNode, + GameSettings * settings, Faction * faction, + World * world); + + void clearCaches(); + bool showTranslatedTechTree() const; + + void addAttackParticleSystem(ParticleSystem * ps); + + Checksum getCRC(); + + virtual void end(ParticleSystem * particleSystem); + virtual void logParticleInfo(string info); + void setNetworkCRCParticleLogInfo(string networkCRCParticleLogInfo) { + this->networkCRCParticleLogInfo = networkCRCParticleLogInfo; + } + void clearParticleInfo(); + void addNetworkCRCDecHp(string info); + void clearNetworkCRCDecHpList(); + + private: + + void cleanupAllParticlesystems(); + bool isNetworkCRCEnabled(); + string getNetworkCRCDecHpList() const; + string getParticleInfo() const; + + float computeHeight(const Vec2i & pos) const; + void calculateXZRotation(); + void AnimCycleStarts(); + void updateTarget(); + void clearCommands(); + void deleteQueuedCommand(Command * command); + CommandResult undoCommand(Command * command); + void stopDamageParticles(bool force); + void startDamageParticles(); + + uint32 getFrameCount() const; + + void checkCustomizedParticleTriggers(bool force); + void checkCustomizedUnitParticleTriggers(); + void checkCustomizedUnitParticleListTriggers(const + UnitParticleSystemTypes & + unitParticleSystemTypesList, + bool + applySkillChangeParticles); + void queueTimedParticles(const UnitParticleSystemTypes & + unitParticleSystemTypesList); + + bool checkModelStateInfoForNewHpValue(); + void checkUnitLevel(); + + void morphAttackBoosts(Unit * unit); + + int64 getUpdatedProgress(int64 currentProgress, int64 updateFPS, + int64 speed, int64 diagonalFactor, + int64 heightFactor); + + void logSynchDataCommon(string file, int line, string source = + "", bool threadedMode = false); + void updateAttackBoostProgress(const Game * game); + + void setAlive(bool value); + }; + + } +} // end namespace #endif diff --git a/source/glest_game/type_instances/upgrade.cpp b/source/glest_game/type_instances/upgrade.cpp index 090a02876..b9418b08a 100644 --- a/source/glest_game/type_instances/upgrade.cpp +++ b/source/glest_game/type_instances/upgrade.cpp @@ -24,332 +24,293 @@ using namespace std; using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class Upgrade -// ===================================================== - Upgrade::Upgrade () - { - state = usUpgrading; - this->factionIndex = -1; - this->type = NULL; - } - - Upgrade::Upgrade (const UpgradeType * type, int factionIndex) - { - state = usUpgrading; - this->factionIndex = factionIndex; - this->type = type; - } - -// ============== get ============== - - UpgradeState Upgrade::getState () const - { - return state; - } - - int Upgrade::getFactionIndex () const - { - return factionIndex; - } - - const UpgradeType *Upgrade::getType () const - { - return type; - } - -// ============== set ============== - - void Upgrade::setState (UpgradeState state) - { - this->state = state; - } - - std::string Upgrade::toString ()const - { - std::string result = ""; - - result += - " state = " + intToStr (state) + " factionIndex = " + - intToStr (factionIndex); - if (type != NULL) - { - result += " type = " + type->getReqDesc (false); - } - - return result; - } - - void Upgrade::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *upgradeNode = rootNode->addChild ("Upgrade"); - - upgradeNode->addAttribute ("state", intToStr (state), - mapTagReplacements); - upgradeNode->addAttribute ("factionIndex", intToStr (factionIndex), - mapTagReplacements); - upgradeNode->addAttribute ("type", type->getName (), - mapTagReplacements); - } - - Upgrade *Upgrade::loadGame (const XmlNode * rootNode, Faction * faction) - { - Upgrade *newUpgrade = new Upgrade (); - - const XmlNode *upgradeNode = rootNode; - - //description = upgrademanagerNode->getAttribute("description")->getValue(); - - newUpgrade->state = - static_cast < UpgradeState > - (upgradeNode->getAttribute ("state")->getIntValue ()); - newUpgrade->factionIndex = - upgradeNode->getAttribute ("factionIndex")->getIntValue (); - string unitTypeName = upgradeNode->getAttribute ("type")->getValue (); - newUpgrade->type = faction->getType ()->getUpgradeType (unitTypeName); - - return newUpgrade; - } - -// ===================================================== -// class UpgradeManager -// ===================================================== - - UpgradeManager::~UpgradeManager () - { - upgradesLookup.clear (); - deleteValues (upgrades.begin (), upgrades.end ()); - } - - void UpgradeManager::startUpgrade (const UpgradeType * upgradeType, - int factionIndex) - { - Upgrade *upgrade = new Upgrade (upgradeType, factionIndex); - upgrades.push_back (upgrade); - upgradesLookup[upgradeType] = (int) upgrades.size () - 1; - } - - void UpgradeManager::cancelUpgrade (const UpgradeType * upgradeType) - { - map < const UpgradeType *, int >::iterator iterFind = - upgradesLookup.find (upgradeType); - if (iterFind != upgradesLookup.end ()) - { - if (iterFind->second >= (int) upgrades.size ()) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "Error canceling upgrade, iterFind->second >= upgrades.size() - [%d] : [%d]", - iterFind->second, (int) upgrades.size ()); - throw - megaglest_runtime_error - ("Error canceling upgrade, upgrade not found in upgrade manager"); - } - int eraseIndex = iterFind->second; - upgrades.erase (upgrades.begin () + eraseIndex); - upgradesLookup.erase (upgradeType); - - for (map < const UpgradeType *, int >::iterator iterMap = - upgradesLookup.begin (); iterMap != upgradesLookup.end (); - ++iterMap) - { - if (iterMap->second >= (int) upgrades.size ()) - { - iterMap->second--; - } - if (iterMap->second < 0) - { - upgradesLookup.erase (iterMap->first); - } - } - } - else - { - throw - megaglest_runtime_error - ("Error canceling upgrade, upgrade not found in upgrade manager"); - } - -/* - Upgrades::iterator it; - - for(it=upgrades.begin(); it!=upgrades.end(); it++){ - if((*it)->getType()==upgradeType){ - break; + // ===================================================== + // class Upgrade + // ===================================================== + Upgrade::Upgrade() { + state = usUpgrading; + this->factionIndex = -1; + this->type = NULL; } - } - if(it!=upgrades.end()){ - upgrades.erase(it); - } - else{ - throw megaglest_runtime_error("Error canceling upgrade, upgrade not found in upgrade manager"); - } -*/ - } - - void UpgradeManager::finishUpgrade (const UpgradeType * upgradeType) - { - map < const UpgradeType *, int >::iterator iterFind = - upgradesLookup.find (upgradeType); - if (iterFind != upgradesLookup.end ()) - { - upgrades[iterFind->second]->setState (usUpgraded); - } - else - { - throw - megaglest_runtime_error - ("Error finishing upgrade, upgrade not found in upgrade manager"); - } - - -/* - Upgrades::iterator it; - - for(it=upgrades.begin(); it!=upgrades.end(); it++){ - if((*it)->getType()==upgradeType){ - break; + Upgrade::Upgrade(const UpgradeType * type, int factionIndex) { + state = usUpgrading; + this->factionIndex = factionIndex; + this->type = type; } - } - if(it!=upgrades.end()){ - (*it)->setState(usUpgraded); - } - else{ - throw megaglest_runtime_error("Error finishing upgrade, upgrade not found in upgrade manager"); - } -*/ - } + // ============== get ============== - bool UpgradeManager::isUpgradingOrUpgraded (const UpgradeType * - upgradeType) const - { - if (upgradesLookup.find (upgradeType) != upgradesLookup.end ()) - { - return true; - } - - return false; - -/* - Upgrades::const_iterator it; - - for(it= upgrades.begin(); it!=upgrades.end(); it++){ - if((*it)->getType()==upgradeType){ - return true; + UpgradeState Upgrade::getState() const { + return state; } - } - return false; -*/ - } - - bool UpgradeManager::isUpgraded (const UpgradeType * upgradeType) const - { - map < const UpgradeType *, int >::const_iterator iterFind = - upgradesLookup.find (upgradeType); - if (iterFind != upgradesLookup.end ()) - { - return (upgrades[iterFind->second]->getState () == usUpgraded); - } - return false; - -/* - for(Upgrades::const_iterator it= upgrades.begin(); it!=upgrades.end(); it++){ - if((*it)->getType()==upgradeType && (*it)->getState()==usUpgraded){ - return true; + int Upgrade::getFactionIndex() const { + return factionIndex; } - } - return false; -*/ - } - bool UpgradeManager::isUpgrading (const UpgradeType * upgradeType) const - { - map < const UpgradeType *, int >::const_iterator iterFind = - upgradesLookup.find (upgradeType); - if (iterFind != upgradesLookup.end ()) - { - return (upgrades[iterFind->second]->getState () == usUpgrading); - } - return false; - -/* - for(Upgrades::const_iterator it= upgrades.begin(); it!=upgrades.end(); it++){ - if((*it)->getType()==upgradeType && (*it)->getState()==usUpgrading){ - return true; + const UpgradeType *Upgrade::getType() const { + return type; } + + // ============== set ============== + + void Upgrade::setState(UpgradeState state) { + this->state = state; + } + + std::string Upgrade::toString()const { + std::string result = ""; + + result += + " state = " + intToStr(state) + " factionIndex = " + + intToStr(factionIndex); + if (type != NULL) { + result += " type = " + type->getReqDesc(false); + } + + return result; + } + + void Upgrade::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *upgradeNode = rootNode->addChild("Upgrade"); + + upgradeNode->addAttribute("state", intToStr(state), + mapTagReplacements); + upgradeNode->addAttribute("factionIndex", intToStr(factionIndex), + mapTagReplacements); + upgradeNode->addAttribute("type", type->getName(), + mapTagReplacements); + } + + Upgrade *Upgrade::loadGame(const XmlNode * rootNode, Faction * faction) { + Upgrade *newUpgrade = new Upgrade(); + + const XmlNode *upgradeNode = rootNode; + + //description = upgrademanagerNode->getAttribute("description")->getValue(); + + newUpgrade->state = + static_cast + (upgradeNode->getAttribute("state")->getIntValue()); + newUpgrade->factionIndex = + upgradeNode->getAttribute("factionIndex")->getIntValue(); + string unitTypeName = upgradeNode->getAttribute("type")->getValue(); + newUpgrade->type = faction->getType()->getUpgradeType(unitTypeName); + + return newUpgrade; + } + + // ===================================================== + // class UpgradeManager + // ===================================================== + + UpgradeManager::~UpgradeManager() { + upgradesLookup.clear(); + deleteValues(upgrades.begin(), upgrades.end()); + } + + void UpgradeManager::startUpgrade(const UpgradeType * upgradeType, + int factionIndex) { + Upgrade *upgrade = new Upgrade(upgradeType, factionIndex); + upgrades.push_back(upgrade); + upgradesLookup[upgradeType] = (int) upgrades.size() - 1; + } + + void UpgradeManager::cancelUpgrade(const UpgradeType * upgradeType) { + map < const UpgradeType *, int >::iterator iterFind = + upgradesLookup.find(upgradeType); + if (iterFind != upgradesLookup.end()) { + if (iterFind->second >= (int) upgrades.size()) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "Error canceling upgrade, iterFind->second >= upgrades.size() - [%d] : [%d]", + iterFind->second, (int) upgrades.size()); + throw + megaglest_runtime_error + ("Error canceling upgrade, upgrade not found in upgrade manager"); + } + int eraseIndex = iterFind->second; + upgrades.erase(upgrades.begin() + eraseIndex); + upgradesLookup.erase(upgradeType); + + for (map < const UpgradeType *, int >::iterator iterMap = + upgradesLookup.begin(); iterMap != upgradesLookup.end(); + ++iterMap) { + if (iterMap->second >= (int) upgrades.size()) { + iterMap->second--; + } + if (iterMap->second < 0) { + upgradesLookup.erase(iterMap->first); + } + } + } else { + throw + megaglest_runtime_error + ("Error canceling upgrade, upgrade not found in upgrade manager"); + } + + /* + Upgrades::iterator it; + + for(it=upgrades.begin(); it!=upgrades.end(); it++){ + if((*it)->getType()==upgradeType){ + break; + } + } + + if(it!=upgrades.end()){ + upgrades.erase(it); + } + else{ + throw megaglest_runtime_error("Error canceling upgrade, upgrade not found in upgrade manager"); + } + */ + } + + void UpgradeManager::finishUpgrade(const UpgradeType * upgradeType) { + map < const UpgradeType *, int >::iterator iterFind = + upgradesLookup.find(upgradeType); + if (iterFind != upgradesLookup.end()) { + upgrades[iterFind->second]->setState(usUpgraded); + } else { + throw + megaglest_runtime_error + ("Error finishing upgrade, upgrade not found in upgrade manager"); + } + + + /* + Upgrades::iterator it; + + for(it=upgrades.begin(); it!=upgrades.end(); it++){ + if((*it)->getType()==upgradeType){ + break; + } + } + + if(it!=upgrades.end()){ + (*it)->setState(usUpgraded); + } + else{ + throw megaglest_runtime_error("Error finishing upgrade, upgrade not found in upgrade manager"); + } + */ + } + + bool UpgradeManager::isUpgradingOrUpgraded(const UpgradeType * + upgradeType) const { + if (upgradesLookup.find(upgradeType) != upgradesLookup.end()) { + return true; + } + + return false; + + /* + Upgrades::const_iterator it; + + for(it= upgrades.begin(); it!=upgrades.end(); it++){ + if((*it)->getType()==upgradeType){ + return true; + } + } + + return false; + */ + } + + bool UpgradeManager::isUpgraded(const UpgradeType * upgradeType) const { + map < const UpgradeType *, int >::const_iterator iterFind = + upgradesLookup.find(upgradeType); + if (iterFind != upgradesLookup.end()) { + return (upgrades[iterFind->second]->getState() == usUpgraded); + } + return false; + + /* + for(Upgrades::const_iterator it= upgrades.begin(); it!=upgrades.end(); it++){ + if((*it)->getType()==upgradeType && (*it)->getState()==usUpgraded){ + return true; + } + } + return false; + */ + } + + bool UpgradeManager::isUpgrading(const UpgradeType * upgradeType) const { + map < const UpgradeType *, int >::const_iterator iterFind = + upgradesLookup.find(upgradeType); + if (iterFind != upgradesLookup.end()) { + return (upgrades[iterFind->second]->getState() == usUpgrading); + } + return false; + + /* + for(Upgrades::const_iterator it= upgrades.begin(); it!=upgrades.end(); it++){ + if((*it)->getType()==upgradeType && (*it)->getState()==usUpgrading){ + return true; + } + } + return false; + */ + } + + void UpgradeManager::computeTotalUpgrade(const Unit * unit, + TotalUpgrade * + totalUpgrade) const { + totalUpgrade->reset(); + for (Upgrades::const_iterator it = upgrades.begin(); + it != upgrades.end(); ++it) { + if ((*it)->getFactionIndex() == unit->getFactionIndex() + && (*it)->getType()->isAffected(unit->getType()) + && (*it)->getState() == usUpgraded) + totalUpgrade->sum((*it)->getType(), unit); + } + + } + + std::string UpgradeManager::toString() const { + std::string result = + "UpgradeCount: " + intToStr(this->getUpgradeCount()); + for (int idx = 0; idx < (int) upgrades.size(); idx++) { + result += + " index = " + intToStr(idx) + " " + upgrades[idx]->toString(); + } + return result; + } + + void UpgradeManager::saveGame(XmlNode * rootNode) { + //std::map mapTagReplacements; + XmlNode *upgrademanagerNode = rootNode->addChild("UpgradeManager"); + + for (unsigned int i = 0; i < upgrades.size(); ++i) { + upgrades[i]->saveGame(upgrademanagerNode); + } + + // Upgrades upgrades; + // UgradesLookup upgradesLookup; + } + + void UpgradeManager::loadGame(const XmlNode * rootNode, + Faction * faction) { + const XmlNode *upgrademanagerNode = + rootNode->getChild("UpgradeManager"); + + //description = upgrademanagerNode->getAttribute("description")->getValue(); + + vector < XmlNode * >upgradeNodeList = + upgrademanagerNode->getChildList("Upgrade"); + for (unsigned int i = 0; i < upgradeNodeList.size(); ++i) { + XmlNode *node = upgradeNodeList[i]; + Upgrade *newUpgrade = Upgrade::loadGame(node, faction); + upgrades.push_back(newUpgrade); + upgradesLookup[newUpgrade->getType()] = (int) upgrades.size() - 1; + } + } + } - return false; -*/ - } - - void UpgradeManager::computeTotalUpgrade (const Unit * unit, - TotalUpgrade * - totalUpgrade) const - { - totalUpgrade->reset (); - for (Upgrades::const_iterator it = upgrades.begin (); - it != upgrades.end (); ++it) - { - if ((*it)->getFactionIndex () == unit->getFactionIndex () - && (*it)->getType ()->isAffected (unit->getType ()) - && (*it)->getState () == usUpgraded) - totalUpgrade->sum ((*it)->getType (), unit); - } - - } - - std::string UpgradeManager::toString () const - { - std::string result = - "UpgradeCount: " + intToStr (this->getUpgradeCount ()); - for (int idx = 0; idx < (int) upgrades.size (); idx++) - { - result += - " index = " + intToStr (idx) + " " + upgrades[idx]->toString (); - } - return result; - } - - void UpgradeManager::saveGame (XmlNode * rootNode) - { - //std::map mapTagReplacements; - XmlNode *upgrademanagerNode = rootNode->addChild ("UpgradeManager"); - - for (unsigned int i = 0; i < upgrades.size (); ++i) - { - upgrades[i]->saveGame (upgrademanagerNode); - } - -// Upgrades upgrades; -// UgradesLookup upgradesLookup; - } - - void UpgradeManager::loadGame (const XmlNode * rootNode, - Faction * faction) - { - const XmlNode *upgrademanagerNode = - rootNode->getChild ("UpgradeManager"); - - //description = upgrademanagerNode->getAttribute("description")->getValue(); - - vector < XmlNode * >upgradeNodeList = - upgrademanagerNode->getChildList ("Upgrade"); - for (unsigned int i = 0; i < upgradeNodeList.size (); ++i) - { - XmlNode *node = upgradeNodeList[i]; - Upgrade *newUpgrade = Upgrade::loadGame (node, faction); - upgrades.push_back (newUpgrade); - upgradesLookup[newUpgrade->getType ()] = (int) upgrades.size () - 1; - } - } - -}} // end namespace +} // end namespace diff --git a/source/glest_game/type_instances/upgrade.h b/source/glest_game/type_instances/upgrade.h index 903817b30..8ba88c963 100644 --- a/source/glest_game/type_instances/upgrade.h +++ b/source/glest_game/type_instances/upgrade.h @@ -34,188 +34,183 @@ using std::vector; using std::map; using Shared::Xml::XmlNode; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class Unit; - class UpgradeType; - class Faction; + class Unit; + class UpgradeType; + class Faction; -/** - * Stores the state of the upgrade (whether or not the upgrading process is complete). - */ -// TODO: Don't make this global; move it inside Upgrade - enum UpgradeState - { - usUpgrading, /**< The upgrade is currently in progress. */ - usUpgraded, /**< The upgrade is completed. */ + /** + * Stores the state of the upgrade (whether or not the upgrading process is complete). + */ + // TODO: Don't make this global; move it inside Upgrade + enum UpgradeState { + usUpgrading, /**< The upgrade is currently in progress. */ + usUpgraded, /**< The upgrade is completed. */ - upgradeStateCount // TODO: This should be unnecessary -- there's no need to iterate over this enum - }; + upgradeStateCount // TODO: This should be unnecessary -- there's no need to iterate over this enum + }; - class UpgradeManager; - class TotalUpgrade; + class UpgradeManager; + class TotalUpgrade; -/** - * An instance of an upgrade. Factions will typically have one upgrade of each type. This object - * groups the type, faction, and upgrade state (ie, has the upgrade been obtained yet?). - */ - class Upgrade - { - private: - UpgradeState state; - // TODO: I believe this is unnecessary. As far as I can tell, it's only used for checking - // that the unit we're applying UpgradeManager::computeTotalUpgrade to is in this faction. However, - // I don't see an circumstances when it wouldn't be (since the UpgradeManager already an aggregate - // of a faction and Unit directly gets the UpgradeManager from the faction (so it must have the - // same faction as the upgrades in the UpgradeManager). - int factionIndex; - const UpgradeType *type; + /** + * An instance of an upgrade. Factions will typically have one upgrade of each type. This object + * groups the type, faction, and upgrade state (ie, has the upgrade been obtained yet?). + */ + class Upgrade { + private: + UpgradeState state; + // TODO: I believe this is unnecessary. As far as I can tell, it's only used for checking + // that the unit we're applying UpgradeManager::computeTotalUpgrade to is in this faction. However, + // I don't see an circumstances when it wouldn't be (since the UpgradeManager already an aggregate + // of a faction and Unit directly gets the UpgradeManager from the faction (so it must have the + // same faction as the upgrades in the UpgradeManager). + int factionIndex; + const UpgradeType *type; - friend class UpgradeManager; + friend class UpgradeManager; - Upgrade (); - public: - /** - * Creates an upgrade. The upgrade state will be set to UpgradeState::usUpgrading. - * @param upgradeType The type of the upgrade that this corresponds to. Upgrade types are - * essentially "classes" for upgrades. - * @param factionIndex The index of the faction that the upgrade belongs to. - */ - Upgrade (const UpgradeType * upgradeType, int factionIndex); + Upgrade(); + public: + /** + * Creates an upgrade. The upgrade state will be set to UpgradeState::usUpgrading. + * @param upgradeType The type of the upgrade that this corresponds to. Upgrade types are + * essentially "classes" for upgrades. + * @param factionIndex The index of the faction that the upgrade belongs to. + */ + Upgrade(const UpgradeType * upgradeType, int factionIndex); - private: - UpgradeState getState () const; - int getFactionIndex () const; - const UpgradeType *getType () const; + private: + UpgradeState getState() const; + int getFactionIndex() const; + const UpgradeType *getType() const; - void setState (UpgradeState state); + void setState(UpgradeState state); - /** - * Retrieves a string representation of the upgrade (detailing its state, type, and faction). - */ - std::string toString () const; + /** + * Retrieves a string representation of the upgrade (detailing its state, type, and faction). + */ + std::string toString() const; - /** - * Saves the object state into the given node. - * @param rootNode The UpgradeManager node to save object info to. - */ - void saveGame (XmlNode * rootNode); + /** + * Saves the object state into the given node. + * @param rootNode The UpgradeManager node to save object info to. + */ + void saveGame(XmlNode * rootNode); - /** - * Loads the object state from the given node. - * @param rootNode The UpgradeManager node to retrieve object info from. - * @param faction The faction that the upgrade belongs to. Used to convert the upgrade type from - * the XML string. - */ - static Upgrade *loadGame (const XmlNode * rootNode, Faction * faction); - }; + /** + * Loads the object state from the given node. + * @param rootNode The UpgradeManager node to retrieve object info from. + * @param faction The faction that the upgrade belongs to. Used to convert the upgrade type from + * the XML string. + */ + static Upgrade *loadGame(const XmlNode * rootNode, Faction * faction); + }; -/** - * Manages upgrades by starting, stopping, and finishing upgrades. Each faction has their own - * upgrade manager. - */ - class UpgradeManager - { - private: - typedef vector < Upgrade * >Upgrades; - typedef map < const UpgradeType *, int >UgradesLookup; + /** + * Manages upgrades by starting, stopping, and finishing upgrades. Each faction has their own + * upgrade manager. + */ + class UpgradeManager { + private: + typedef vector < Upgrade * >Upgrades; + typedef map < const UpgradeType *, int >UgradesLookup; - /** - * List of upgrades that the upgrade manager is working with (either in progress or finished). - */ - Upgrades upgrades; + /** + * List of upgrades that the upgrade manager is working with (either in progress or finished). + */ + Upgrades upgrades; - /** - * Maps UpgradeType to the index of the upgrade in UpgradeManager::upgrades. - */ - UgradesLookup upgradesLookup; - public: - ~UpgradeManager (); + /** + * Maps UpgradeType to the index of the upgrade in UpgradeManager::upgrades. + */ + UgradesLookup upgradesLookup; + public: + ~UpgradeManager(); - int getUpgradeCount () const - { - return (int) upgrades.size (); - } + int getUpgradeCount() const { + return (int) upgrades.size(); + } - /** - * Starts an upgrade. - * @param upgradeType The type of the upgrade to start. - * @param factionIndex Passed to the constructor of the Upgrade. - */ - void startUpgrade (const UpgradeType * upgradeType, int factionIndex); + /** + * Starts an upgrade. + * @param upgradeType The type of the upgrade to start. + * @param factionIndex Passed to the constructor of the Upgrade. + */ + void startUpgrade(const UpgradeType * upgradeType, int factionIndex); - /** - * Cancels an upgrade before it is finished. The upgrade is removed from the UpgradeManager. - * @param upgradeType The type of the upgrade to remove. - * @throws megaglest_runtime_error If there is no upgrade of the desired type in the UpgradeManager. - */ - void cancelUpgrade (const UpgradeType * upgradeType); + /** + * Cancels an upgrade before it is finished. The upgrade is removed from the UpgradeManager. + * @param upgradeType The type of the upgrade to remove. + * @throws megaglest_runtime_error If there is no upgrade of the desired type in the UpgradeManager. + */ + void cancelUpgrade(const UpgradeType * upgradeType); - /** - * Sets an Upgrade in the UpgradeManager as finished (ie, the state is UpgradeState::usUpgraded). - * @param upgradeType The type of the upgrade to complete. - * @throws megaglest_runtime_error If there is no upgrade of the desired type in the UpgradeManager. - */ - void finishUpgrade (const UpgradeType * upgradeType); + /** + * Sets an Upgrade in the UpgradeManager as finished (ie, the state is UpgradeState::usUpgraded). + * @param upgradeType The type of the upgrade to complete. + * @throws megaglest_runtime_error If there is no upgrade of the desired type in the UpgradeManager. + */ + void finishUpgrade(const UpgradeType * upgradeType); - /** - * Returns true if an Upgrade of the desired type has state UpgradeState::usUpgraded (ie, is - * finished upgrading). - * @param upgradeType The type of the upgrade in question. - */ - bool isUpgraded (const UpgradeType * upgradeType) const; + /** + * Returns true if an Upgrade of the desired type has state UpgradeState::usUpgraded (ie, is + * finished upgrading). + * @param upgradeType The type of the upgrade in question. + */ + bool isUpgraded(const UpgradeType * upgradeType) const; - /** - * Returns true if an Upgrade of the desired type has state UpgradeState::usUpgrading (ie, is - * currently in progress). - * @param upgradeType The type of the upgrade in question. - */ - bool isUpgrading (const UpgradeType * upgradeType) const; + /** + * Returns true if an Upgrade of the desired type has state UpgradeState::usUpgrading (ie, is + * currently in progress). + * @param upgradeType The type of the upgrade in question. + */ + bool isUpgrading(const UpgradeType * upgradeType) const; - /** - * Returns true if an Upgrade of the desired type exists in the UpgradeManager. - * @param upgradeType The type of the upgrade in question. - */ - bool isUpgradingOrUpgraded (const UpgradeType * upgradeType) const; + /** + * Returns true if an Upgrade of the desired type exists in the UpgradeManager. + * @param upgradeType The type of the upgrade in question. + */ + bool isUpgradingOrUpgraded(const UpgradeType * upgradeType) const; - /** - * [Sums up](@ref TotalUpgrade::sum) the effect of all upgrades for this faction as they apply - * to a particular unit. - * @param unit The unit that the TotalUpgrade applies to. This is necessary because some - * upgrades provide percentage boosts. - * @param totalUpgrade The TotalUpgrade object to modify. Note that it is cleared before values - * are calculated. - */ - void computeTotalUpgrade (const Unit * unit, - TotalUpgrade * totalUpgrade) const; + /** + * [Sums up](@ref TotalUpgrade::sum) the effect of all upgrades for this faction as they apply + * to a particular unit. + * @param unit The unit that the TotalUpgrade applies to. This is necessary because some + * upgrades provide percentage boosts. + * @param totalUpgrade The TotalUpgrade object to modify. Note that it is cleared before values + * are calculated. + */ + void computeTotalUpgrade(const Unit * unit, + TotalUpgrade * totalUpgrade) const; - /** - * Retrieves a string representation of the UpgradeManager. Contains the contents of - * Upgrade::toString for all upgrades in the UpgradeManager. - */ - std::string toString () const; + /** + * Retrieves a string representation of the UpgradeManager. Contains the contents of + * Upgrade::toString for all upgrades in the UpgradeManager. + */ + std::string toString() const; - /** - * Adds a node for the UpgradeManager that contains all the upgrade nodes, saving the object's - * state. - * @param rootNode The faction node to add the UpgradeManager node to. - * @see Upgrade::saveGame - */ - void saveGame (XmlNode * rootNode); + /** + * Adds a node for the UpgradeManager that contains all the upgrade nodes, saving the object's + * state. + * @param rootNode The faction node to add the UpgradeManager node to. + * @see Upgrade::saveGame + */ + void saveGame(XmlNode * rootNode); - /** - * Loads all the upgrades from the UpgradeManager node, effectively reloading the object's - * state. - * @param rootNode The faction node to get the UpgradeManager node from. - * @param faction Only passed to Upgrade::loadGame (which does the actual loading of each - * Upgrade object. - */ - void loadGame (const XmlNode * rootNode, Faction * faction); - }; + /** + * Loads all the upgrades from the UpgradeManager node, effectively reloading the object's + * state. + * @param rootNode The faction node to get the UpgradeManager node from. + * @param faction Only passed to Upgrade::loadGame (which does the actual loading of each + * Upgrade object. + */ + void loadGame(const XmlNode * rootNode, Faction * faction); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/types/command_type.cpp b/source/glest_game/types/command_type.cpp index b2026fa78..5529aaece 100644 --- a/source/glest_game/types/command_type.cpp +++ b/source/glest_game/types/command_type.cpp @@ -29,1580 +29,1441 @@ using namespace Shared::Util; -namespace Glest -{ - namespace Game - { - - -// ===================================================== -// class CommandType -// ===================================================== - -//get - CommandClass CommandType::getClass () const - { - assert (this != NULL); - return commandTypeClass; - } - - void CommandType::load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, string parentLoader) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - - this->id = id; - name = - n->getChild ("name")->getAttribute ("value")->getRestrictedValue (); - - //image - const XmlNode *imageNode = n->getChild ("image"); - image = Renderer::getInstance ().newTexture2D (rsGame); - - string currentPath = dir; - endPathWithSlash (currentPath); - if (image) - { - image->load (imageNode->getAttribute ("path")-> - getRestrictedValue (currentPath)); - } - loadedFileList[imageNode->getAttribute ("path")-> - getRestrictedValue (currentPath)]. - push_back (make_pair - (parentLoader, - imageNode->getAttribute ("path")->getRestrictedValue ())); - - //unit requirements - const XmlNode *unitRequirementsNode = n->getChild ("unit-requirements"); - for (int i = 0; i < (int) unitRequirementsNode->getChildCount (); ++i) - { - const XmlNode *unitNode = unitRequirementsNode->getChild ("unit", i); - string name = unitNode->getAttribute ("name")->getRestrictedValue (); - unitReqs.push_back (ft->getUnitType (name)); - } - - //upgrade requirements - const XmlNode *upgradeRequirementsNode = - n->getChild ("upgrade-requirements"); - for (int i = 0; i < (int) upgradeRequirementsNode->getChildCount (); - ++i) - { - const XmlNode *upgradeReqNode = - upgradeRequirementsNode->getChild ("upgrade", i); - string name = - upgradeReqNode->getAttribute ("name")->getRestrictedValue (); - upgradeReqs.push_back (ft->getUpgradeType (name)); - } - - //fog of war - if (n->hasChild ("fog-of-war-skill") == true) - { - string skillName = - n->getChild ("fog-of-war-skill")->getAttribute ("value")-> - getRestrictedValue (); - fogOfWarSkillType = - static_cast < - const FogOfWarSkillType *>(ut.getSkillType (skillName, scFogOfWar)); - - string skillAttachmentNames = - n->getChild ("fog-of-war-skill")-> - getAttribute ("skill-attachments")->getValue (); - - std::vector < std::string > skillList; - Tokenize (skillAttachmentNames, skillList, ","); - for (unsigned int i = 0; i < skillList.size (); ++i) - { - string skillAttachName = skillList[i]; - fogOfWarSkillAttachments[skillAttachName] = true; - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - bool CommandType::hasFogOfWarSkillType (string name) const - { - std::map < string, bool >::const_iterator iterFind = - fogOfWarSkillAttachments.find (name); - bool result = (iterFind != fogOfWarSkillAttachments.end ()); - return result; - } - -// ===================================================== -// class StopCommandType -// ===================================================== - -//varios - StopCommandType::StopCommandType () - { - commandTypeClass = ccStop; - clicks = cOne; - stopSkillType = NULL; - } - - void StopCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateStop (unit, frameIndex); - } - - string StopCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str; - Lang & lang = Lang::getInstance (); - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("ReactionSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (stopSkillType->getSpeed ()) + "\n"; - if (stopSkillType->getEpCost () != 0) - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (stopSkillType->getEpCost ()) + "\n"; - if (stopSkillType->getHpCost () != 0) - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (stopSkillType->getHpCost ()) + "\n"; - str += stopSkillType->getBoostDesc (translatedValue); - return str; - } - - string StopCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Stop"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Stop"); - } - - void StopCommandType::load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //stop - string skillName = - n->getChild ("stop-skill")->getAttribute ("value")-> - getRestrictedValue (); - stopSkillType = - static_cast < - const StopSkillType *>(ut.getSkillType (skillName, scStop)); - } - - -// ===================================================== -// class MoveCommandType -// ===================================================== - -//varios - MoveCommandType::MoveCommandType () - { - commandTypeClass = ccMove; - clicks = cTwo; - moveSkillType = NULL; - } - - void MoveCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateMove (unit, frameIndex); - } - - void MoveCommandType::load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //move - string skillName = - n->getChild ("move-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - } - - string MoveCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str; - Lang & lang = Lang::getInstance (); - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("WalkSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (moveSkillType->getSpeed ()); - if (totalUpgrade->getMoveSpeed (moveSkillType) != 0) - { - str += "+" + intToStr (totalUpgrade->getMoveSpeed (moveSkillType)); - } - str += "\n"; - if (moveSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (moveSkillType->getEpCost ()) + "\n"; - } - if (moveSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (moveSkillType->getHpCost ()) + "\n"; - } - str += moveSkillType->getBoostDesc (translatedValue); - return str; - } - - string MoveCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Move"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Move"); - } - -// ===================================================== -// class AttackCommandType -// ===================================================== - -//varios - AttackCommandType::AttackCommandType () - { - commandTypeClass = ccAttack; - clicks = cTwo; - moveSkillType = NULL; - attackSkillType = NULL; - } - - void AttackCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateAttack (unit, frameIndex); - } - - void AttackCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, const UnitType & ut, - std::map < string, vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //move - string skillName = - n->getChild ("move-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - - //attack - skillName = - n->getChild ("attack-skill")->getAttribute ("value")-> - getRestrictedValue (); - attackSkillType = - static_cast < - const AttackSkillType *>(ut.getSkillType (skillName, scAttack)); - } - - string AttackCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str; - Lang & lang = Lang::getInstance (); - - str = getName (translatedValue) + "\n"; - if (attackSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getEpCost ()) + "\n"; - } - if (attackSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getHpCost ()) + "\n"; - } - - //attack strength - str += - lang.getString ("AttackStrenght", - (translatedValue == true ? "" : "english")) + ": "; - str += - intToStr (attackSkillType->getAttackStrength () - - attackSkillType->getAttackVar ()); - str += "..."; - str += - intToStr (attackSkillType->getAttackStrength () + - attackSkillType->getAttackVar ()); - if (totalUpgrade->getAttackStrength (attackSkillType) != 0) - { - str += - "+" + intToStr (totalUpgrade->getAttackStrength (attackSkillType)); - } - str += - " (" + attackSkillType->getAttackType ()->getName (translatedValue) + - ")"; - str += "\n"; - - //splash radius - if (attackSkillType->getSplashRadius () != 0) - { - str += - lang.getString ("SplashRadius", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getSplashRadius ()) + "\n"; - } - - //attack distance - str += - lang.getString ("AttackDistance", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getAttackRange ()); - if (totalUpgrade->getAttackRange (attackSkillType) != 0) - { - str += - "+" + intToStr (totalUpgrade->getAttackRange (attackSkillType)); - } - str += "\n"; - - //attack fields - str += lang.getString ("Fields") + ": "; - for (int i = 0; i < fieldCount; i++) - { - Field field = static_cast < Field > (i); - if (attackSkillType->getAttackField (field)) - { - str += SkillType::fieldToStr (field) + " "; - } - } - str += "\n"; - - //movement speed - str += - lang.getString ("WalkSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (moveSkillType->getSpeed ()); - if (totalUpgrade->getMoveSpeed (moveSkillType) != 0) - { - str += "+" + intToStr (totalUpgrade->getMoveSpeed (moveSkillType)); - } - str += "\n"; - - //attack speed - str += - lang.getString ("AttackSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getSpeed ()) + "\n"; - if (totalUpgrade->getAttackSpeed (attackSkillType) != 0) - { - str += - "+" + intToStr (totalUpgrade->getAttackSpeed (attackSkillType)); - } - str += "\n"; - - str += attackSkillType->getBoostDesc (translatedValue); - return str; - } - - string AttackCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Attack"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Attack"); - } - - -// ===================================================== -// class AttackStoppedCommandType -// ===================================================== - -//varios - AttackStoppedCommandType::AttackStoppedCommandType () - { - commandTypeClass = ccAttackStopped; - clicks = cOne; - stopSkillType = NULL; - attackSkillType = NULL; - } - - void AttackStoppedCommandType::update (UnitUpdater * unitUpdater, - Unit * unit, int frameIndex) const - { - unitUpdater->updateAttackStopped (unit, frameIndex); - } - - void AttackStoppedCommandType::load (int id, const XmlNode * n, - const string & dir, - const TechTree * tt, - const FactionType * ft, - const UnitType & ut, - std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //stop - string skillName = - n->getChild ("stop-skill")->getAttribute ("value")-> - getRestrictedValue (); - stopSkillType = - static_cast < - const StopSkillType *>(ut.getSkillType (skillName, scStop)); - - //attack - skillName = - n->getChild ("attack-skill")->getAttribute ("value")-> - getRestrictedValue (); - attackSkillType = - static_cast < - const AttackSkillType *>(ut.getSkillType (skillName, scAttack)); - } - - string AttackStoppedCommandType::getDesc (const TotalUpgrade * - totalUpgrade, - bool translatedValue) const - { - Lang & lang = Lang::getInstance (); - string str; - - str = getName (translatedValue) + "\n"; - if (attackSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getEpCost ()) + "\n"; - } - if (attackSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getHpCost ()) + "\n"; - } - - //attack strength - str += - lang.getString ("AttackStrenght", - (translatedValue == true ? "" : "english")) + ": "; - str += - intToStr (attackSkillType->getAttackStrength () - - attackSkillType->getAttackVar ()); - str += "..."; - str += - intToStr (attackSkillType->getAttackStrength () + - attackSkillType->getAttackVar ()); - if (totalUpgrade->getAttackStrength (attackSkillType) != 0) - str += - "+" + intToStr (totalUpgrade->getAttackStrength (attackSkillType)); - str += - " (" + attackSkillType->getAttackType ()->getName (translatedValue) + - ")"; - str += "\n"; - - //splash radius - if (attackSkillType->getSplashRadius () != 0) - { - str += - lang.getString ("SplashRadius", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getSplashRadius ()) + "\n"; - } - - //attack distance - str += - lang.getString ("AttackDistance", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (attackSkillType->getAttackRange ()); - if (totalUpgrade->getAttackRange (attackSkillType) != 0) - { - str += - "+" + intToStr (totalUpgrade->getAttackRange (attackSkillType) != - 0); - } - str += "\n"; - - //attack fields - str += - lang.getString ("Fields", - (translatedValue == true ? "" : "english")) + ": "; - for (int i = 0; i < fieldCount; i++) - { - Field field = static_cast < Field > (i); - if (attackSkillType->getAttackField (field)) - { - str += SkillType::fieldToStr (field) + " "; - } - } - str += "\n"; - str += attackSkillType->getBoostDesc (translatedValue); - return str; - } - - string AttackStoppedCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "AttackStopped"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("AttackStopped"); - } - - -// ===================================================== -// class BuildCommandType -// ===================================================== - -//varios - BuildCommandType::BuildCommandType () - { - commandTypeClass = ccBuild; - clicks = cTwo; - moveSkillType = NULL; - buildSkillType = NULL; - } - - BuildCommandType::~BuildCommandType () - { - deleteValues (builtSounds.getSounds ().begin (), - builtSounds.getSounds ().end ()); - deleteValues (startSounds.getSounds ().begin (), - startSounds.getSounds ().end ()); - } - - void BuildCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateBuild (unit, frameIndex); - } - - void BuildCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, const UnitType & ut, - std::map < string, vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //move - string skillName = - n->getChild ("move-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - - //build - skillName = - n->getChild ("build-skill")->getAttribute ("value")-> - getRestrictedValue (); - buildSkillType = - static_cast < - const BuildSkillType *>(ut.getSkillType (skillName, scBuild)); - - //buildings built - const XmlNode *buildingsNode = n->getChild ("buildings"); - for (int i = 0; i < (int) buildingsNode->getChildCount (); ++i) - { - const XmlNode *buildingNode = buildingsNode->getChild ("building", i); - string name = - buildingNode->getAttribute ("name")->getRestrictedValue (); - buildings.push_back (ft->getUnitType (name)); - } - - //start sound - const XmlNode *startSoundNode = n->getChild ("start-sound"); - if (startSoundNode->getAttribute ("enabled")->getBoolValue ()) - { - startSounds.resize ((int) startSoundNode->getChildCount ()); - for (int i = 0; i < (int) startSoundNode->getChildCount (); ++i) - { - const XmlNode *soundFileNode = - startSoundNode->getChild ("sound-file", i); - string currentPath = dir; - endPathWithSlash (currentPath); - string path = - soundFileNode->getAttribute ("path")-> - getRestrictedValue (currentPath, true); - - StaticSound *sound = new StaticSound (); - - sound->load (path); - loadedFileList[path]. - push_back (make_pair - (parentLoader, - soundFileNode->getAttribute ("path")-> - getRestrictedValue ())); - startSounds[i] = sound; - } - } - - //built sound - const XmlNode *builtSoundNode = n->getChild ("built-sound"); - if (builtSoundNode->getAttribute ("enabled")->getBoolValue ()) - { - builtSounds.resize ((int) builtSoundNode->getChildCount ()); - for (int i = 0; i < (int) builtSoundNode->getChildCount (); ++i) - { - const XmlNode *soundFileNode = - builtSoundNode->getChild ("sound-file", i); - string currentPath = dir; - endPathWithSlash (currentPath); - string path = - soundFileNode->getAttribute ("path")-> - getRestrictedValue (currentPath, true); - - StaticSound *sound = new StaticSound (); - - sound->load (path); - loadedFileList[path]. - push_back (make_pair - (parentLoader, - soundFileNode->getAttribute ("path")-> - getRestrictedValue ())); - builtSounds[i] = sound; - } - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string BuildCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str; - Lang & lang = Lang::getInstance (); - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("BuildSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (buildSkillType->getSpeed ()) + "\n"; - if (buildSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (buildSkillType->getEpCost ()) + "\n"; - } - if (buildSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (buildSkillType->getHpCost ()) + "\n"; - } - str += buildSkillType->getBoostDesc (translatedValue); - return str; - } - - string BuildCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Build"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Build"); - } - -// ===================================================== -// class HarvestCommandType -// ===================================================== - -//varios - HarvestCommandType::HarvestCommandType () - { - commandTypeClass = ccHarvest; - clicks = cTwo; - moveSkillType = NULL; - moveLoadedSkillType = NULL; - harvestSkillType = NULL; - stopLoadedSkillType = NULL; - maxLoad = 0; - hitsPerUnit = 0; - } - - void HarvestCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateHarvest (unit, frameIndex); - } - - void HarvestCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //move - string skillName = - n->getChild ("move-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - - //harvest - skillName = - n->getChild ("harvest-skill")->getAttribute ("value")-> - getRestrictedValue (); - harvestSkillType = - static_cast < - const HarvestSkillType *>(ut.getSkillType (skillName, scHarvest)); - - //stop loaded - skillName = - n->getChild ("stop-loaded-skill")->getAttribute ("value")-> - getRestrictedValue (); - stopLoadedSkillType = - static_cast < - const StopSkillType *>(ut.getSkillType (skillName, scStop)); - - //move loaded - skillName = - n->getChild ("move-loaded-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveLoadedSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - - //resources can harvest - const XmlNode *resourcesNode = n->getChild ("harvested-resources"); - for (int i = 0; i < (int) resourcesNode->getChildCount (); ++i) - { - const XmlNode *resourceNode = resourcesNode->getChild ("resource", i); - harvestedResources.push_back (tt-> - getResourceType (resourceNode-> - getAttribute ("name")-> - getRestrictedValue - ())); - } - - maxLoad = - n->getChild ("max-load")->getAttribute ("value")->getIntValue (); - hitsPerUnit = - n->getChild ("hits-per-unit")->getAttribute ("value")->getIntValue (); - } - - string HarvestCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - - Lang & lang = Lang::getInstance (); - string str; - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("HarvestSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (harvestSkillType->getSpeed () / hitsPerUnit) + "\n"; - str += - lang.getString ("MaxLoad", - (translatedValue == - true ? "" : "english")) + ": " + intToStr (maxLoad) + - "\n"; - str += - lang.getString ("LoadedSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (moveLoadedSkillType->getSpeed ()) + "\n"; - if (harvestSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (harvestSkillType->getEpCost ()) + "\n"; - } - if (harvestSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (harvestSkillType->getHpCost ()) + "\n"; - } - str += - lang.getString ("Resources", - (translatedValue == true ? "" : "english")) + ":\n"; - for (int i = 0; i < getHarvestedResourceCount (); ++i) - { - str += getHarvestedResource (i)->getName (translatedValue) + "\n"; - } - str += harvestSkillType->getBoostDesc (translatedValue); - return str; - } - - string HarvestCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Harvest"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Harvest"); - } - - bool HarvestCommandType::canHarvest (const ResourceType * resourceType) const - { - return find (harvestedResources.begin (), harvestedResources.end (), - resourceType) != harvestedResources.end (); - } - - -// ===================================================== -// class HarvestCommandType -// ===================================================== - -//varios - HarvestEmergencyReturnCommandType::HarvestEmergencyReturnCommandType () - { - commandTypeClass = ccHarvestEmergencyReturn; - clicks = cTwo; - - this->id = -10; - } - - void HarvestEmergencyReturnCommandType::update (UnitUpdater * unitUpdater, - Unit * unit, - int frameIndex) const - { - unitUpdater->updateHarvestEmergencyReturn (unit, frameIndex); - } - - void HarvestEmergencyReturnCommandType::load (int id, const XmlNode * n, - const string & dir, - const TechTree * tt, - const FactionType * ft, - const UnitType & ut, - std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { -// CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, parentLoader); - } - - string HarvestEmergencyReturnCommandType::getDesc (const TotalUpgrade * - totalUpgrade, - bool translatedValue) - const - { - string str = getName (translatedValue) + "\n"; - return str; - } - - string HarvestEmergencyReturnCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "HarvestEmergencyReturn"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Harvest"); - } - -// ===================================================== -// class RepairCommandType -// ===================================================== - -//varios - RepairCommandType::RepairCommandType () - { - commandTypeClass = ccRepair; - clicks = cTwo; - moveSkillType = NULL; - repairSkillType = NULL; - } - - RepairCommandType::~RepairCommandType () - { - } - - void RepairCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateRepair (unit, frameIndex); - } - - void RepairCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, const UnitType & ut, - std::map < string, vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - // move skill (no longer required by means unit must already be beside unit to repair) - // for example a hospital - if (n->hasChild ("move-skill") == true) - { - string skillName = - n->getChild ("move-skill")->getAttribute ("value")-> - getRestrictedValue (); - moveSkillType = - static_cast < - const MoveSkillType *>(ut.getSkillType (skillName, scMove)); - } - - //repair - string skillName = - n->getChild ("repair-skill")->getAttribute ("value")-> - getRestrictedValue (); - repairSkillType = - static_cast < - const RepairSkillType *>(ut.getSkillType (skillName, scRepair)); - - //repaired units - const XmlNode *unitsNode = n->getChild ("repaired-units"); - for (int i = 0; i < (int) unitsNode->getChildCount (); ++i) - { - const XmlNode *unitNode = unitsNode->getChild ("unit", i); - repairableUnits.push_back (ft-> - getUnitType (unitNode-> - getAttribute ("name")-> - getRestrictedValue ())); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string RepairCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - Lang & lang = Lang::getInstance (); - string str; - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("RepairSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (repairSkillType->getSpeed ()) + "\n"; - if (repairSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (repairSkillType->getEpCost ()) + "\n"; - } - if (repairSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (repairSkillType->getHpCost ()) + "\n"; - } - str += - "\n" + lang.getString ("CanRepair", - (translatedValue == - true ? "" : "english")) + ":\n"; - for (int i = 0; i < (int) repairableUnits.size (); ++i) - { - str += - (static_cast < - const UnitType * - >(repairableUnits[i]))->getName (translatedValue) + "\n"; - } - str += repairSkillType->getBoostDesc (translatedValue); - return str; - } - - string RepairCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Repair"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Repair"); - } - -//get - bool RepairCommandType::isRepairableUnitType (const UnitType * unitType) const - { - for (int i = 0; i < (int) repairableUnits.size (); ++i) - { - const UnitType *curUnitType = - static_cast < const UnitType * >(repairableUnits[i]); - //printf("Lookup index = %d Can repair unittype [%s][%p] looking for [%s][%p] lookup found result = %d\n",i,curUnitType->getName().c_str(),curUnitType,unitType->getName().c_str(),unitType,(curUnitType == unitType)); - if (curUnitType == unitType) - { - return true; - } - } - return false; - } - -// ===================================================== -// class ProduceCommandType -// ===================================================== - -//varios - ProduceCommandType::ProduceCommandType () - { - commandTypeClass = ccProduce; - clicks = cOne; - produceSkillType = NULL; - producedUnit = NULL; - } - - void ProduceCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateProduce (unit, frameIndex); - } - - void ProduceCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //produce - string skillName = - n->getChild ("produce-skill")->getAttribute ("value")-> - getRestrictedValue (); - produceSkillType = - static_cast < - const ProduceSkillType *>(ut.getSkillType (skillName, scProduce)); - - string producedUnitName = - n->getChild ("produced-unit")->getAttribute ("name")-> - getRestrictedValue (); - producedUnit = ft->getUnitType (producedUnitName); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string ProduceCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str = getName (translatedValue) + "\n"; - Lang & lang = Lang::getInstance (); - - //prod speed - str += - lang.getString ("ProductionSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (produceSkillType->getSpeed ()); - if (totalUpgrade->getProdSpeed (produceSkillType) != 0) - { - str += "+" + intToStr (totalUpgrade->getProdSpeed (produceSkillType)); - } - str += "\n"; - str += - "" + Lang::getInstance ().getString ("TimeSteps", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (getProducedUnit ()->getProductionTime ()) + "\n"; - int64 speed = - produceSkillType->getSpeed () + - totalUpgrade->getProdSpeed (produceSkillType); - int64 time = getProducedUnit ()->getProductionTime (); - int64 seconds = time * 100 / speed; - str += - "" + Lang::getInstance ().getString ("Time", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (seconds); - str += "\n"; - - //mpcost - if (produceSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (produceSkillType->getEpCost ()) + "\n"; - } - if (produceSkillType->getHpCost () != 0) - { - str += - lang.getString ("hpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (produceSkillType->getHpCost ()) + "\n"; - } - str += "\n" + getProducedUnit ()->getReqDesc (translatedValue); - str += produceSkillType->getBoostDesc (translatedValue); - return str; - } - - string ProduceCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Produce"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Produce"); - } - - string ProduceCommandType::getReqDesc (bool translatedValue) const - { - return RequirableType::getReqDesc (translatedValue) + "\n" + - getProducedUnit ()->getReqDesc (translatedValue); - } - - const ProducibleType *ProduceCommandType::getProduced () const - { - return producedUnit; - } - -// ===================================================== -// class UpgradeCommandType -// ===================================================== - -//varios - UpgradeCommandType::UpgradeCommandType () - { - commandTypeClass = ccUpgrade; - clicks = cOne; - upgradeSkillType = NULL; - producedUpgrade = NULL; - } - - void UpgradeCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateUpgrade (unit, frameIndex); - } - - void UpgradeCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //upgrade - string skillName = - n->getChild ("upgrade-skill")->getAttribute ("value")-> - getRestrictedValue (); - upgradeSkillType = - static_cast < - const UpgradeSkillType *>(ut.getSkillType (skillName, scUpgrade)); - - string producedUpgradeName = - n->getChild ("produced-upgrade")->getAttribute ("name")-> - getRestrictedValue (); - producedUpgrade = ft->getUpgradeType (producedUpgradeName); - - } - - string UpgradeCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str; - Lang & lang = Lang::getInstance (); - - str = getName (translatedValue) + "\n"; - str += - lang.getString ("UpgradeSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (upgradeSkillType->getSpeed ()); - if (totalUpgrade->getProdSpeed (upgradeSkillType) != 0) - { - str += "+" + intToStr (totalUpgrade->getProdSpeed (upgradeSkillType)); - } - str += "\n"; - str += - "" + Lang::getInstance ().getString ("TimeSteps", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (getProducedUpgrade ()->getProductionTime ()) + "\n"; - int64 speed = - upgradeSkillType->getSpeed () + - totalUpgrade->getProdSpeed (upgradeSkillType); - int64 time = getProducedUpgrade ()->getProductionTime (); - int64 seconds = time * 100 / speed; - str += - "" + Lang::getInstance ().getString ("Time", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (seconds); - str += "\n"; - - if (upgradeSkillType->getEpCost () != 0) - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (upgradeSkillType->getEpCost ()) + "\n"; - if (upgradeSkillType->getHpCost () != 0) - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (upgradeSkillType->getHpCost ()) + "\n"; - str += "\n" + getProducedUpgrade ()->getReqDesc (translatedValue); - str += upgradeSkillType->getBoostDesc (translatedValue); - return str; - } - - string UpgradeCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Upgrade"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Upgrade"); - } - - string UpgradeCommandType::getReqDesc (bool translatedValue) const - { - return RequirableType::getReqDesc (translatedValue) + "\n" + - getProducedUpgrade ()->getReqDesc (translatedValue); - } - - const ProducibleType *UpgradeCommandType::getProduced () const - { - return producedUpgrade; - } - -// ===================================================== -// class MorphCommandType -// ===================================================== - -//varios - MorphCommandType::MorphCommandType () - { - commandTypeClass = ccMorph; - clicks = cOne; - morphSkillType = NULL; - morphUnit = NULL; - discount = 0; - ignoreResourceRequirements = false; - } - - void MorphCommandType::update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const - { - unitUpdater->updateMorph (unit, frameIndex); - } - - void MorphCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, const UnitType & ut, - std::map < string, vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //morph skill - string skillName = - n->getChild ("morph-skill")->getAttribute ("value")-> - getRestrictedValue (); - morphSkillType = - static_cast < - const MorphSkillType *>(ut.getSkillType (skillName, scMorph)); - - //morph unit - string morphUnitName = - n->getChild ("morph-unit")->getAttribute ("name")-> - getRestrictedValue (); - morphUnit = ft->getUnitType (morphUnitName); - - //discount - discount = - n->getChild ("discount")->getAttribute ("value")->getIntValue (); - - ignoreResourceRequirements = false; - if (n->hasChild ("ignore-resource-requirements") == true) - { - ignoreResourceRequirements = - n->getChild ("ignore-resource-requirements")-> - getAttribute ("value")->getBoolValue (); - - //printf("ignoreResourceRequirements = %d\n",ignoreResourceRequirements); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string MorphCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str = getName (translatedValue) + "\n"; - Lang & lang = Lang::getInstance (); - - //prod speed - str += - lang.getString ("MorphSpeed", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (morphSkillType->getSpeed ()); - if (totalUpgrade->getProdSpeed (morphSkillType) != 0) - { - str += "+" + intToStr (totalUpgrade->getProdSpeed (morphSkillType)); - } - str += "\n"; - str += - "" + Lang::getInstance ().getString ("TimeSteps", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (getMorphUnit ()->getProductionTime ()) + "\n"; - int64 speed = - morphSkillType->getSpeed () + - totalUpgrade->getProdSpeed (morphSkillType); - int64 time = getMorphUnit ()->getProductionTime (); - int64 seconds = time * 100 / speed; - str += - "" + Lang::getInstance ().getString ("Time", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (seconds); - str += "\n"; - - //mpcost - if (morphSkillType->getEpCost () != 0) - { - str += - lang.getString ("EpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (morphSkillType->getEpCost ()) + "\n"; - } - if (morphSkillType->getHpCost () != 0) - { - str += - lang.getString ("HpCost", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (morphSkillType->getHpCost ()) + "\n"; - } - - //discount - if (discount != 0) - { - str += - lang.getString ("Discount", - (translatedValue == - true ? "" : "english")) + ": " + - intToStr (discount) + "%\n"; - } - - str += - "\n" + getProduced ()->getReqDesc (ignoreResourceRequirements, - translatedValue); - - str += morphSkillType->getBoostDesc (translatedValue); - - return str; - } - - string MorphCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "Morph"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("Morph"); - } - - string MorphCommandType::getReqDesc (bool translatedValue) const - { - return RequirableType::getReqDesc (translatedValue) + "\n" + - getProduced ()->getReqDesc (translatedValue); - } - - const ProducibleType *MorphCommandType::getProduced () const - { - return morphUnit; - } - -// ===================================================== -// class SwitchTeamCommandType -// ===================================================== - -//varios - SwitchTeamCommandType::SwitchTeamCommandType () - { - commandTypeClass = ccSwitchTeam; - clicks = cOne; - } - - void SwitchTeamCommandType::update (UnitUpdater * unitUpdater, - Unit * unit, int frameIndex) const - { - unitUpdater->updateSwitchTeam (unit, frameIndex); - } - - void SwitchTeamCommandType::load (int id, const XmlNode * n, - const string & dir, const TechTree * tt, - const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, - string > > >&loadedFileList, - string parentLoader) - { - CommandType::load (id, n, dir, tt, ft, ut, loadedFileList, - parentLoader); - - //morph skill - //string skillName= n->getChild("morph-skill")->getAttribute("value")->getRestrictedValue(); - //morphSkillType= static_cast(ut.getSkillType(skillName, scMorph)); - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", __FILE__, - __FUNCTION__, __LINE__); - } - - string SwitchTeamCommandType::getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const - { - string str = getName (translatedValue) + "\n"; - return str; - } - - string SwitchTeamCommandType::toString (bool translatedValue) const - { - if (translatedValue == false) - { - return "SwitchTeam"; - } - Lang & lang = Lang::getInstance (); - return lang.getString ("SwitchTeam"); - } - -// ===================================================== -// class CommandFactory -// ===================================================== - - CommandTypeFactory::CommandTypeFactory () - { - registerClass < StopCommandType > ("stop"); - registerClass < MoveCommandType > ("move"); - registerClass < AttackCommandType > ("attack"); - registerClass < AttackStoppedCommandType > ("attack_stopped"); - registerClass < BuildCommandType > ("build"); - registerClass < HarvestCommandType > ("harvest"); - registerClass < RepairCommandType > ("repair"); - registerClass < ProduceCommandType > ("produce"); - registerClass < UpgradeCommandType > ("upgrade"); - registerClass < MorphCommandType > ("morph"); - registerClass < SwitchTeamCommandType > ("switch_team"); - registerClass < HarvestEmergencyReturnCommandType > ("harvest_return"); - } - - CommandTypeFactory & CommandTypeFactory::getInstance () - { - static CommandTypeFactory ctf; - return ctf; - } - - } +namespace Glest { + namespace Game { + + + // ===================================================== + // class CommandType + // ===================================================== + + //get + CommandClass CommandType::getClass() const { + assert(this != NULL); + return commandTypeClass; + } + + void CommandType::load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, string parentLoader) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + + this->id = id; + name = + n->getChild("name")->getAttribute("value")->getRestrictedValue(); + + //image + const XmlNode *imageNode = n->getChild("image"); + image = Renderer::getInstance().newTexture2D(rsGame); + + string currentPath = dir; + endPathWithSlash(currentPath); + if (image) { + image->load(imageNode->getAttribute("path")-> + getRestrictedValue(currentPath)); + } + loadedFileList[imageNode->getAttribute("path")-> + getRestrictedValue(currentPath)]. + push_back(make_pair + (parentLoader, + imageNode->getAttribute("path")->getRestrictedValue())); + + //unit requirements + const XmlNode *unitRequirementsNode = n->getChild("unit-requirements"); + for (int i = 0; i < (int) unitRequirementsNode->getChildCount(); ++i) { + const XmlNode *unitNode = unitRequirementsNode->getChild("unit", i); + string name = unitNode->getAttribute("name")->getRestrictedValue(); + unitReqs.push_back(ft->getUnitType(name)); + } + + //upgrade requirements + const XmlNode *upgradeRequirementsNode = + n->getChild("upgrade-requirements"); + for (int i = 0; i < (int) upgradeRequirementsNode->getChildCount(); + ++i) { + const XmlNode *upgradeReqNode = + upgradeRequirementsNode->getChild("upgrade", i); + string name = + upgradeReqNode->getAttribute("name")->getRestrictedValue(); + upgradeReqs.push_back(ft->getUpgradeType(name)); + } + + //fog of war + if (n->hasChild("fog-of-war-skill") == true) { + string skillName = + n->getChild("fog-of-war-skill")->getAttribute("value")-> + getRestrictedValue(); + fogOfWarSkillType = + static_cast < + const FogOfWarSkillType *>(ut.getSkillType(skillName, scFogOfWar)); + + string skillAttachmentNames = + n->getChild("fog-of-war-skill")-> + getAttribute("skill-attachments")->getValue(); + + std::vector < std::string > skillList; + Tokenize(skillAttachmentNames, skillList, ","); + for (unsigned int i = 0; i < skillList.size(); ++i) { + string skillAttachName = skillList[i]; + fogOfWarSkillAttachments[skillAttachName] = true; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + bool CommandType::hasFogOfWarSkillType(string name) const { + std::map < string, bool >::const_iterator iterFind = + fogOfWarSkillAttachments.find(name); + bool result = (iterFind != fogOfWarSkillAttachments.end()); + return result; + } + + // ===================================================== + // class StopCommandType + // ===================================================== + + //varios + StopCommandType::StopCommandType() { + commandTypeClass = ccStop; + clicks = cOne; + stopSkillType = NULL; + } + + void StopCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateStop(unit, frameIndex); + } + + string StopCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str; + Lang & lang = Lang::getInstance(); + + str = getName(translatedValue) + "\n"; + str += + lang.getString("ReactionSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(stopSkillType->getSpeed()) + "\n"; + if (stopSkillType->getEpCost() != 0) + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(stopSkillType->getEpCost()) + "\n"; + if (stopSkillType->getHpCost() != 0) + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(stopSkillType->getHpCost()) + "\n"; + str += stopSkillType->getBoostDesc(translatedValue); + return str; + } + + string StopCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Stop"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Stop"); + } + + void StopCommandType::load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //stop + string skillName = + n->getChild("stop-skill")->getAttribute("value")-> + getRestrictedValue(); + stopSkillType = + static_cast < + const StopSkillType *>(ut.getSkillType(skillName, scStop)); + } + + + // ===================================================== + // class MoveCommandType + // ===================================================== + + //varios + MoveCommandType::MoveCommandType() { + commandTypeClass = ccMove; + clicks = cTwo; + moveSkillType = NULL; + } + + void MoveCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateMove(unit, frameIndex); + } + + void MoveCommandType::load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //move + string skillName = + n->getChild("move-skill")->getAttribute("value")-> + getRestrictedValue(); + moveSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + } + + string MoveCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str; + Lang & lang = Lang::getInstance(); + + str = getName(translatedValue) + "\n"; + str += + lang.getString("WalkSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(moveSkillType->getSpeed()); + if (totalUpgrade->getMoveSpeed(moveSkillType) != 0) { + str += "+" + intToStr(totalUpgrade->getMoveSpeed(moveSkillType)); + } + str += "\n"; + if (moveSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(moveSkillType->getEpCost()) + "\n"; + } + if (moveSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(moveSkillType->getHpCost()) + "\n"; + } + str += moveSkillType->getBoostDesc(translatedValue); + return str; + } + + string MoveCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Move"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Move"); + } + + // ===================================================== + // class AttackCommandType + // ===================================================== + + //varios + AttackCommandType::AttackCommandType() { + commandTypeClass = ccAttack; + clicks = cTwo; + moveSkillType = NULL; + attackSkillType = NULL; + } + + void AttackCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateAttack(unit, frameIndex); + } + + void AttackCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, const UnitType & ut, + std::map < string, vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //move + string skillName = + n->getChild("move-skill")->getAttribute("value")-> + getRestrictedValue(); + moveSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + + //attack + skillName = + n->getChild("attack-skill")->getAttribute("value")-> + getRestrictedValue(); + attackSkillType = + static_cast < + const AttackSkillType *>(ut.getSkillType(skillName, scAttack)); + } + + string AttackCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str; + Lang & lang = Lang::getInstance(); + + str = getName(translatedValue) + "\n"; + if (attackSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getEpCost()) + "\n"; + } + if (attackSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getHpCost()) + "\n"; + } + + //attack strength + str += + lang.getString("AttackStrenght", + (translatedValue == true ? "" : "english")) + ": "; + str += + intToStr(attackSkillType->getAttackStrength() - + attackSkillType->getAttackVar()); + str += "..."; + str += + intToStr(attackSkillType->getAttackStrength() + + attackSkillType->getAttackVar()); + if (totalUpgrade->getAttackStrength(attackSkillType) != 0) { + str += + "+" + intToStr(totalUpgrade->getAttackStrength(attackSkillType)); + } + str += + " (" + attackSkillType->getAttackType()->getName(translatedValue) + + ")"; + str += "\n"; + + //splash radius + if (attackSkillType->getSplashRadius() != 0) { + str += + lang.getString("SplashRadius", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getSplashRadius()) + "\n"; + } + + //attack distance + str += + lang.getString("AttackDistance", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getAttackRange()); + if (totalUpgrade->getAttackRange(attackSkillType) != 0) { + str += + "+" + intToStr(totalUpgrade->getAttackRange(attackSkillType)); + } + str += "\n"; + + //attack fields + str += lang.getString("Fields") + ": "; + for (int i = 0; i < fieldCount; i++) { + Field field = static_cast (i); + if (attackSkillType->getAttackField(field)) { + str += SkillType::fieldToStr(field) + " "; + } + } + str += "\n"; + + //movement speed + str += + lang.getString("WalkSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(moveSkillType->getSpeed()); + if (totalUpgrade->getMoveSpeed(moveSkillType) != 0) { + str += "+" + intToStr(totalUpgrade->getMoveSpeed(moveSkillType)); + } + str += "\n"; + + //attack speed + str += + lang.getString("AttackSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getSpeed()) + "\n"; + if (totalUpgrade->getAttackSpeed(attackSkillType) != 0) { + str += + "+" + intToStr(totalUpgrade->getAttackSpeed(attackSkillType)); + } + str += "\n"; + + str += attackSkillType->getBoostDesc(translatedValue); + return str; + } + + string AttackCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Attack"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Attack"); + } + + + // ===================================================== + // class AttackStoppedCommandType + // ===================================================== + + //varios + AttackStoppedCommandType::AttackStoppedCommandType() { + commandTypeClass = ccAttackStopped; + clicks = cOne; + stopSkillType = NULL; + attackSkillType = NULL; + } + + void AttackStoppedCommandType::update(UnitUpdater * unitUpdater, + Unit * unit, int frameIndex) const { + unitUpdater->updateAttackStopped(unit, frameIndex); + } + + void AttackStoppedCommandType::load(int id, const XmlNode * n, + const string & dir, + const TechTree * tt, + const FactionType * ft, + const UnitType & ut, + std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //stop + string skillName = + n->getChild("stop-skill")->getAttribute("value")-> + getRestrictedValue(); + stopSkillType = + static_cast < + const StopSkillType *>(ut.getSkillType(skillName, scStop)); + + //attack + skillName = + n->getChild("attack-skill")->getAttribute("value")-> + getRestrictedValue(); + attackSkillType = + static_cast < + const AttackSkillType *>(ut.getSkillType(skillName, scAttack)); + } + + string AttackStoppedCommandType::getDesc(const TotalUpgrade * + totalUpgrade, + bool translatedValue) const { + Lang & lang = Lang::getInstance(); + string str; + + str = getName(translatedValue) + "\n"; + if (attackSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getEpCost()) + "\n"; + } + if (attackSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getHpCost()) + "\n"; + } + + //attack strength + str += + lang.getString("AttackStrenght", + (translatedValue == true ? "" : "english")) + ": "; + str += + intToStr(attackSkillType->getAttackStrength() - + attackSkillType->getAttackVar()); + str += "..."; + str += + intToStr(attackSkillType->getAttackStrength() + + attackSkillType->getAttackVar()); + if (totalUpgrade->getAttackStrength(attackSkillType) != 0) + str += + "+" + intToStr(totalUpgrade->getAttackStrength(attackSkillType)); + str += + " (" + attackSkillType->getAttackType()->getName(translatedValue) + + ")"; + str += "\n"; + + //splash radius + if (attackSkillType->getSplashRadius() != 0) { + str += + lang.getString("SplashRadius", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getSplashRadius()) + "\n"; + } + + //attack distance + str += + lang.getString("AttackDistance", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(attackSkillType->getAttackRange()); + if (totalUpgrade->getAttackRange(attackSkillType) != 0) { + str += + "+" + intToStr(totalUpgrade->getAttackRange(attackSkillType) != + 0); + } + str += "\n"; + + //attack fields + str += + lang.getString("Fields", + (translatedValue == true ? "" : "english")) + ": "; + for (int i = 0; i < fieldCount; i++) { + Field field = static_cast (i); + if (attackSkillType->getAttackField(field)) { + str += SkillType::fieldToStr(field) + " "; + } + } + str += "\n"; + str += attackSkillType->getBoostDesc(translatedValue); + return str; + } + + string AttackStoppedCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "AttackStopped"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("AttackStopped"); + } + + + // ===================================================== + // class BuildCommandType + // ===================================================== + + //varios + BuildCommandType::BuildCommandType() { + commandTypeClass = ccBuild; + clicks = cTwo; + moveSkillType = NULL; + buildSkillType = NULL; + } + + BuildCommandType::~BuildCommandType() { + deleteValues(builtSounds.getSounds().begin(), + builtSounds.getSounds().end()); + deleteValues(startSounds.getSounds().begin(), + startSounds.getSounds().end()); + } + + void BuildCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateBuild(unit, frameIndex); + } + + void BuildCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, const UnitType & ut, + std::map < string, vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //move + string skillName = + n->getChild("move-skill")->getAttribute("value")-> + getRestrictedValue(); + moveSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + + //build + skillName = + n->getChild("build-skill")->getAttribute("value")-> + getRestrictedValue(); + buildSkillType = + static_cast < + const BuildSkillType *>(ut.getSkillType(skillName, scBuild)); + + //buildings built + const XmlNode *buildingsNode = n->getChild("buildings"); + for (int i = 0; i < (int) buildingsNode->getChildCount(); ++i) { + const XmlNode *buildingNode = buildingsNode->getChild("building", i); + string name = + buildingNode->getAttribute("name")->getRestrictedValue(); + buildings.push_back(ft->getUnitType(name)); + } + + //start sound + const XmlNode *startSoundNode = n->getChild("start-sound"); + if (startSoundNode->getAttribute("enabled")->getBoolValue()) { + startSounds.resize((int) startSoundNode->getChildCount()); + for (int i = 0; i < (int) startSoundNode->getChildCount(); ++i) { + const XmlNode *soundFileNode = + startSoundNode->getChild("sound-file", i); + string currentPath = dir; + endPathWithSlash(currentPath); + string path = + soundFileNode->getAttribute("path")-> + getRestrictedValue(currentPath, true); + + StaticSound *sound = new StaticSound(); + + sound->load(path); + loadedFileList[path]. + push_back(make_pair + (parentLoader, + soundFileNode->getAttribute("path")-> + getRestrictedValue())); + startSounds[i] = sound; + } + } + + //built sound + const XmlNode *builtSoundNode = n->getChild("built-sound"); + if (builtSoundNode->getAttribute("enabled")->getBoolValue()) { + builtSounds.resize((int) builtSoundNode->getChildCount()); + for (int i = 0; i < (int) builtSoundNode->getChildCount(); ++i) { + const XmlNode *soundFileNode = + builtSoundNode->getChild("sound-file", i); + string currentPath = dir; + endPathWithSlash(currentPath); + string path = + soundFileNode->getAttribute("path")-> + getRestrictedValue(currentPath, true); + + StaticSound *sound = new StaticSound(); + + sound->load(path); + loadedFileList[path]. + push_back(make_pair + (parentLoader, + soundFileNode->getAttribute("path")-> + getRestrictedValue())); + builtSounds[i] = sound; + } + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string BuildCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str; + Lang & lang = Lang::getInstance(); + + str = getName(translatedValue) + "\n"; + str += + lang.getString("BuildSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(buildSkillType->getSpeed()) + "\n"; + if (buildSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(buildSkillType->getEpCost()) + "\n"; + } + if (buildSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(buildSkillType->getHpCost()) + "\n"; + } + str += buildSkillType->getBoostDesc(translatedValue); + return str; + } + + string BuildCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Build"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Build"); + } + + // ===================================================== + // class HarvestCommandType + // ===================================================== + + //varios + HarvestCommandType::HarvestCommandType() { + commandTypeClass = ccHarvest; + clicks = cTwo; + moveSkillType = NULL; + moveLoadedSkillType = NULL; + harvestSkillType = NULL; + stopLoadedSkillType = NULL; + maxLoad = 0; + hitsPerUnit = 0; + } + + void HarvestCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateHarvest(unit, frameIndex); + } + + void HarvestCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //move + string skillName = + n->getChild("move-skill")->getAttribute("value")-> + getRestrictedValue(); + moveSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + + //harvest + skillName = + n->getChild("harvest-skill")->getAttribute("value")-> + getRestrictedValue(); + harvestSkillType = + static_cast < + const HarvestSkillType *>(ut.getSkillType(skillName, scHarvest)); + + //stop loaded + skillName = + n->getChild("stop-loaded-skill")->getAttribute("value")-> + getRestrictedValue(); + stopLoadedSkillType = + static_cast < + const StopSkillType *>(ut.getSkillType(skillName, scStop)); + + //move loaded + skillName = + n->getChild("move-loaded-skill")->getAttribute("value")-> + getRestrictedValue(); + moveLoadedSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + + //resources can harvest + const XmlNode *resourcesNode = n->getChild("harvested-resources"); + for (int i = 0; i < (int) resourcesNode->getChildCount(); ++i) { + const XmlNode *resourceNode = resourcesNode->getChild("resource", i); + harvestedResources.push_back(tt-> + getResourceType(resourceNode-> + getAttribute("name")-> + getRestrictedValue + ())); + } + + maxLoad = + n->getChild("max-load")->getAttribute("value")->getIntValue(); + hitsPerUnit = + n->getChild("hits-per-unit")->getAttribute("value")->getIntValue(); + } + + string HarvestCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + + Lang & lang = Lang::getInstance(); + string str; + + str = getName(translatedValue) + "\n"; + str += + lang.getString("HarvestSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(harvestSkillType->getSpeed() / hitsPerUnit) + "\n"; + str += + lang.getString("MaxLoad", + (translatedValue == + true ? "" : "english")) + ": " + intToStr(maxLoad) + + "\n"; + str += + lang.getString("LoadedSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(moveLoadedSkillType->getSpeed()) + "\n"; + if (harvestSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(harvestSkillType->getEpCost()) + "\n"; + } + if (harvestSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(harvestSkillType->getHpCost()) + "\n"; + } + str += + lang.getString("Resources", + (translatedValue == true ? "" : "english")) + ":\n"; + for (int i = 0; i < getHarvestedResourceCount(); ++i) { + str += getHarvestedResource(i)->getName(translatedValue) + "\n"; + } + str += harvestSkillType->getBoostDesc(translatedValue); + return str; + } + + string HarvestCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Harvest"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Harvest"); + } + + bool HarvestCommandType::canHarvest(const ResourceType * resourceType) const { + return find(harvestedResources.begin(), harvestedResources.end(), + resourceType) != harvestedResources.end(); + } + + + // ===================================================== + // class HarvestCommandType + // ===================================================== + + //varios + HarvestEmergencyReturnCommandType::HarvestEmergencyReturnCommandType() { + commandTypeClass = ccHarvestEmergencyReturn; + clicks = cTwo; + + this->id = -10; + } + + void HarvestEmergencyReturnCommandType::update(UnitUpdater * unitUpdater, + Unit * unit, + int frameIndex) const { + unitUpdater->updateHarvestEmergencyReturn(unit, frameIndex); + } + + void HarvestEmergencyReturnCommandType::load(int id, const XmlNode * n, + const string & dir, + const TechTree * tt, + const FactionType * ft, + const UnitType & ut, + std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + // CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, parentLoader); + } + + string HarvestEmergencyReturnCommandType::getDesc(const TotalUpgrade * + totalUpgrade, + bool translatedValue) + const { + string str = getName(translatedValue) + "\n"; + return str; + } + + string HarvestEmergencyReturnCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "HarvestEmergencyReturn"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Harvest"); + } + + // ===================================================== + // class RepairCommandType + // ===================================================== + + //varios + RepairCommandType::RepairCommandType() { + commandTypeClass = ccRepair; + clicks = cTwo; + moveSkillType = NULL; + repairSkillType = NULL; + } + + RepairCommandType::~RepairCommandType() { + } + + void RepairCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateRepair(unit, frameIndex); + } + + void RepairCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, const UnitType & ut, + std::map < string, vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + // move skill (no longer required by means unit must already be beside unit to repair) + // for example a hospital + if (n->hasChild("move-skill") == true) { + string skillName = + n->getChild("move-skill")->getAttribute("value")-> + getRestrictedValue(); + moveSkillType = + static_cast < + const MoveSkillType *>(ut.getSkillType(skillName, scMove)); + } + + //repair + string skillName = + n->getChild("repair-skill")->getAttribute("value")-> + getRestrictedValue(); + repairSkillType = + static_cast < + const RepairSkillType *>(ut.getSkillType(skillName, scRepair)); + + //repaired units + const XmlNode *unitsNode = n->getChild("repaired-units"); + for (int i = 0; i < (int) unitsNode->getChildCount(); ++i) { + const XmlNode *unitNode = unitsNode->getChild("unit", i); + repairableUnits.push_back(ft-> + getUnitType(unitNode-> + getAttribute("name")-> + getRestrictedValue())); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string RepairCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + Lang & lang = Lang::getInstance(); + string str; + + str = getName(translatedValue) + "\n"; + str += + lang.getString("RepairSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(repairSkillType->getSpeed()) + "\n"; + if (repairSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(repairSkillType->getEpCost()) + "\n"; + } + if (repairSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(repairSkillType->getHpCost()) + "\n"; + } + str += + "\n" + lang.getString("CanRepair", + (translatedValue == + true ? "" : "english")) + ":\n"; + for (int i = 0; i < (int) repairableUnits.size(); ++i) { + str += + (static_cast < + const UnitType * + >(repairableUnits[i]))->getName(translatedValue) + "\n"; + } + str += repairSkillType->getBoostDesc(translatedValue); + return str; + } + + string RepairCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Repair"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Repair"); + } + + //get + bool RepairCommandType::isRepairableUnitType(const UnitType * unitType) const { + for (int i = 0; i < (int) repairableUnits.size(); ++i) { + const UnitType *curUnitType = + static_cast (repairableUnits[i]); + //printf("Lookup index = %d Can repair unittype [%s][%p] looking for [%s][%p] lookup found result = %d\n",i,curUnitType->getName().c_str(),curUnitType,unitType->getName().c_str(),unitType,(curUnitType == unitType)); + if (curUnitType == unitType) { + return true; + } + } + return false; + } + + // ===================================================== + // class ProduceCommandType + // ===================================================== + + //varios + ProduceCommandType::ProduceCommandType() { + commandTypeClass = ccProduce; + clicks = cOne; + produceSkillType = NULL; + producedUnit = NULL; + } + + void ProduceCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateProduce(unit, frameIndex); + } + + void ProduceCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //produce + string skillName = + n->getChild("produce-skill")->getAttribute("value")-> + getRestrictedValue(); + produceSkillType = + static_cast < + const ProduceSkillType *>(ut.getSkillType(skillName, scProduce)); + + string producedUnitName = + n->getChild("produced-unit")->getAttribute("name")-> + getRestrictedValue(); + producedUnit = ft->getUnitType(producedUnitName); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string ProduceCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str = getName(translatedValue) + "\n"; + Lang & lang = Lang::getInstance(); + + //prod speed + str += + lang.getString("ProductionSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(produceSkillType->getSpeed()); + if (totalUpgrade->getProdSpeed(produceSkillType) != 0) { + str += "+" + intToStr(totalUpgrade->getProdSpeed(produceSkillType)); + } + str += "\n"; + str += + "" + Lang::getInstance().getString("TimeSteps", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(getProducedUnit()->getProductionTime()) + "\n"; + int64 speed = + produceSkillType->getSpeed() + + totalUpgrade->getProdSpeed(produceSkillType); + int64 time = getProducedUnit()->getProductionTime(); + int64 seconds = time * 100 / speed; + str += + "" + Lang::getInstance().getString("Time", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(seconds); + str += "\n"; + + //mpcost + if (produceSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(produceSkillType->getEpCost()) + "\n"; + } + if (produceSkillType->getHpCost() != 0) { + str += + lang.getString("hpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(produceSkillType->getHpCost()) + "\n"; + } + str += "\n" + getProducedUnit()->getReqDesc(translatedValue); + str += produceSkillType->getBoostDesc(translatedValue); + return str; + } + + string ProduceCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Produce"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Produce"); + } + + string ProduceCommandType::getReqDesc(bool translatedValue) const { + return RequirableType::getReqDesc(translatedValue) + "\n" + + getProducedUnit()->getReqDesc(translatedValue); + } + + const ProducibleType *ProduceCommandType::getProduced() const { + return producedUnit; + } + + // ===================================================== + // class UpgradeCommandType + // ===================================================== + + //varios + UpgradeCommandType::UpgradeCommandType() { + commandTypeClass = ccUpgrade; + clicks = cOne; + upgradeSkillType = NULL; + producedUpgrade = NULL; + } + + void UpgradeCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateUpgrade(unit, frameIndex); + } + + void UpgradeCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //upgrade + string skillName = + n->getChild("upgrade-skill")->getAttribute("value")-> + getRestrictedValue(); + upgradeSkillType = + static_cast < + const UpgradeSkillType *>(ut.getSkillType(skillName, scUpgrade)); + + string producedUpgradeName = + n->getChild("produced-upgrade")->getAttribute("name")-> + getRestrictedValue(); + producedUpgrade = ft->getUpgradeType(producedUpgradeName); + + } + + string UpgradeCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str; + Lang & lang = Lang::getInstance(); + + str = getName(translatedValue) + "\n"; + str += + lang.getString("UpgradeSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(upgradeSkillType->getSpeed()); + if (totalUpgrade->getProdSpeed(upgradeSkillType) != 0) { + str += "+" + intToStr(totalUpgrade->getProdSpeed(upgradeSkillType)); + } + str += "\n"; + str += + "" + Lang::getInstance().getString("TimeSteps", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(getProducedUpgrade()->getProductionTime()) + "\n"; + int64 speed = + upgradeSkillType->getSpeed() + + totalUpgrade->getProdSpeed(upgradeSkillType); + int64 time = getProducedUpgrade()->getProductionTime(); + int64 seconds = time * 100 / speed; + str += + "" + Lang::getInstance().getString("Time", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(seconds); + str += "\n"; + + if (upgradeSkillType->getEpCost() != 0) + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(upgradeSkillType->getEpCost()) + "\n"; + if (upgradeSkillType->getHpCost() != 0) + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(upgradeSkillType->getHpCost()) + "\n"; + str += "\n" + getProducedUpgrade()->getReqDesc(translatedValue); + str += upgradeSkillType->getBoostDesc(translatedValue); + return str; + } + + string UpgradeCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Upgrade"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Upgrade"); + } + + string UpgradeCommandType::getReqDesc(bool translatedValue) const { + return RequirableType::getReqDesc(translatedValue) + "\n" + + getProducedUpgrade()->getReqDesc(translatedValue); + } + + const ProducibleType *UpgradeCommandType::getProduced() const { + return producedUpgrade; + } + + // ===================================================== + // class MorphCommandType + // ===================================================== + + //varios + MorphCommandType::MorphCommandType() { + commandTypeClass = ccMorph; + clicks = cOne; + morphSkillType = NULL; + morphUnit = NULL; + discount = 0; + ignoreResourceRequirements = false; + } + + void MorphCommandType::update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const { + unitUpdater->updateMorph(unit, frameIndex); + } + + void MorphCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, const UnitType & ut, + std::map < string, vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //morph skill + string skillName = + n->getChild("morph-skill")->getAttribute("value")-> + getRestrictedValue(); + morphSkillType = + static_cast < + const MorphSkillType *>(ut.getSkillType(skillName, scMorph)); + + //morph unit + string morphUnitName = + n->getChild("morph-unit")->getAttribute("name")-> + getRestrictedValue(); + morphUnit = ft->getUnitType(morphUnitName); + + //discount + discount = + n->getChild("discount")->getAttribute("value")->getIntValue(); + + ignoreResourceRequirements = false; + if (n->hasChild("ignore-resource-requirements") == true) { + ignoreResourceRequirements = + n->getChild("ignore-resource-requirements")-> + getAttribute("value")->getBoolValue(); + + //printf("ignoreResourceRequirements = %d\n",ignoreResourceRequirements); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string MorphCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str = getName(translatedValue) + "\n"; + Lang & lang = Lang::getInstance(); + + //prod speed + str += + lang.getString("MorphSpeed", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(morphSkillType->getSpeed()); + if (totalUpgrade->getProdSpeed(morphSkillType) != 0) { + str += "+" + intToStr(totalUpgrade->getProdSpeed(morphSkillType)); + } + str += "\n"; + str += + "" + Lang::getInstance().getString("TimeSteps", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(getMorphUnit()->getProductionTime()) + "\n"; + int64 speed = + morphSkillType->getSpeed() + + totalUpgrade->getProdSpeed(morphSkillType); + int64 time = getMorphUnit()->getProductionTime(); + int64 seconds = time * 100 / speed; + str += + "" + Lang::getInstance().getString("Time", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(seconds); + str += "\n"; + + //mpcost + if (morphSkillType->getEpCost() != 0) { + str += + lang.getString("EpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(morphSkillType->getEpCost()) + "\n"; + } + if (morphSkillType->getHpCost() != 0) { + str += + lang.getString("HpCost", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(morphSkillType->getHpCost()) + "\n"; + } + + //discount + if (discount != 0) { + str += + lang.getString("Discount", + (translatedValue == + true ? "" : "english")) + ": " + + intToStr(discount) + "%\n"; + } + + str += + "\n" + getProduced()->getReqDesc(ignoreResourceRequirements, + translatedValue); + + str += morphSkillType->getBoostDesc(translatedValue); + + return str; + } + + string MorphCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "Morph"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("Morph"); + } + + string MorphCommandType::getReqDesc(bool translatedValue) const { + return RequirableType::getReqDesc(translatedValue) + "\n" + + getProduced()->getReqDesc(translatedValue); + } + + const ProducibleType *MorphCommandType::getProduced() const { + return morphUnit; + } + + // ===================================================== + // class SwitchTeamCommandType + // ===================================================== + + //varios + SwitchTeamCommandType::SwitchTeamCommandType() { + commandTypeClass = ccSwitchTeam; + clicks = cOne; + } + + void SwitchTeamCommandType::update(UnitUpdater * unitUpdater, + Unit * unit, int frameIndex) const { + unitUpdater->updateSwitchTeam(unit, frameIndex); + } + + void SwitchTeamCommandType::load(int id, const XmlNode * n, + const string & dir, const TechTree * tt, + const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, + string > > >&loadedFileList, + string parentLoader) { + CommandType::load(id, n, dir, tt, ft, ut, loadedFileList, + parentLoader); + + //morph skill + //string skillName= n->getChild("morph-skill")->getAttribute("value")->getRestrictedValue(); + //morphSkillType= static_cast(ut.getSkillType(skillName, scMorph)); + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", __FILE__, + __FUNCTION__, __LINE__); + } + + string SwitchTeamCommandType::getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const { + string str = getName(translatedValue) + "\n"; + return str; + } + + string SwitchTeamCommandType::toString(bool translatedValue) const { + if (translatedValue == false) { + return "SwitchTeam"; + } + Lang & lang = Lang::getInstance(); + return lang.getString("SwitchTeam"); + } + + // ===================================================== + // class CommandFactory + // ===================================================== + + CommandTypeFactory::CommandTypeFactory() { + registerClass < StopCommandType >("stop"); + registerClass < MoveCommandType >("move"); + registerClass < AttackCommandType >("attack"); + registerClass < AttackStoppedCommandType >("attack_stopped"); + registerClass < BuildCommandType >("build"); + registerClass < HarvestCommandType >("harvest"); + registerClass < RepairCommandType >("repair"); + registerClass < ProduceCommandType >("produce"); + registerClass < UpgradeCommandType >("upgrade"); + registerClass < MorphCommandType >("morph"); + registerClass < SwitchTeamCommandType >("switch_team"); + registerClass < HarvestEmergencyReturnCommandType >("harvest_return"); + } + + CommandTypeFactory & CommandTypeFactory::getInstance() { + static CommandTypeFactory ctf; + return ctf; + } + + } } //end namespace diff --git a/source/glest_game/types/command_type.h b/source/glest_game/types/command_type.h index b5f12e6e5..84d606400 100644 --- a/source/glest_game/types/command_type.h +++ b/source/glest_game/types/command_type.h @@ -26,705 +26,625 @@ # include "sound_container.h" # include "leak_dumper.h" -namespace Glest -{ - namespace Game - { - - using Shared::Util::MultiFactory; - - class UnitUpdater; - class Unit; - class UnitType; - class TechTree; - class FactionType; - - enum CommandClass - { - ccStop, - ccMove, - ccAttack, - ccAttackStopped, - ccBuild, - ccHarvest, - ccRepair, - ccProduce, - ccUpgrade, - ccMorph, - ccSwitchTeam, - ccHarvestEmergencyReturn, - - ccCount, - ccNull - }; - - enum Clicks - { - cOne, - cTwo - }; - - enum Queueability - { - qNever, - qOnRequest, - qOnlyLast, - qAlways - }; - -// ===================================================== -// class CommandType -// -/// A complex action performed by a unit, composed by skills -// ===================================================== - - class CommandType:public RequirableType - { - protected: - Clicks clicks; - int id; - - std::map < string, bool > fogOfWarSkillAttachments; - const FogOfWarSkillType *fogOfWarSkillType; - - public: - static const int invalidId = -1; - CommandClass commandTypeClass; - - public: - CommandType () - { - commandTypeClass = ccNull; - clicks = cOne; - id = -1; - fogOfWarSkillType = NULL; - fogOfWarSkillAttachments.clear (); - } - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const = 0; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const = 0; - virtual string toString (bool translatedValue) const = 0; - virtual const ProducibleType *getProduced () const - { - return NULL; - } - virtual Queueability isQueuable () const - { - return qOnRequest; - } - bool isQueuable (bool tryQueue) const - { - Queueability q = isQueuable (); - return (q == qAlways) || ((q == qOnRequest || q == qOnlyLast) - && tryQueue); - } - bool isQueueAppendable () const - { - Queueability q = isQueuable (); - return (q != qNever) && (q != qOnlyLast); - } - //Priority: commands of higher priority will cancel commands of lower priority - virtual int getTypePriority () const - { - return 10; - } - virtual bool usesPathfinder () const = 0; - - //get - CommandClass getClass () const; - Clicks getClicks () const - { - return clicks; - } - int getId () const - { - return id; - } - - const FogOfWarSkillType *getFogOfWarSkillType () const - { - return fogOfWarSkillType; - }; - - bool hasFogOfWarSkillType (string name) const; - }; - -// =============================== -// class StopCommandType -// =============================== - - class StopCommandType:public CommandType - { - private: - const StopSkillType *stopSkillType; - - public: - StopCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual Queueability isQueuable () const - { - return qNever; - } - virtual int getTypePriority () const - { - return 100000; - } - //get - const StopSkillType *getStopSkillType () const - { - return stopSkillType; - }; - - virtual bool usesPathfinder () const - { - return false; - } - }; - - -// =============================== -// class MoveCommandType -// =============================== - - class MoveCommandType:public CommandType - { - private: - const MoveSkillType *moveSkillType; - - public: - MoveCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - //get - const MoveSkillType *getMoveSkillType () const - { - return moveSkillType; - }; - - virtual bool usesPathfinder () const - { - return true; - } - }; - - -// =============================== -// class AttackCommandType -// =============================== - - class AttackCommandType:public CommandType - { - private: - const MoveSkillType *moveSkillType; - const AttackSkillType *attackSkillType; - - public: - AttackCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - - //get - const MoveSkillType *getMoveSkillType () const - { - return moveSkillType; - } - const AttackSkillType *getAttackSkillType () const - { - return attackSkillType; - } - - virtual bool usesPathfinder () const - { - return true; - } - }; - -// ======================================= -// class AttackStoppedCommandType -// ======================================= - - class AttackStoppedCommandType:public CommandType - { - private: - const StopSkillType *stopSkillType; - const AttackSkillType *attackSkillType; - - public: - AttackStoppedCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - //get - const StopSkillType *getStopSkillType () const - { - return stopSkillType; - } - const AttackSkillType *getAttackSkillType () const - { - return attackSkillType; - } - - virtual bool usesPathfinder () const - { - return false; - } - }; - - -// =============================== -// class BuildCommandType -// =============================== - - class BuildCommandType:public CommandType - { - private: - const MoveSkillType *moveSkillType; - const BuildSkillType *buildSkillType; - vector < const UnitType *>buildings; - SoundContainer startSounds; - SoundContainer builtSounds; - - public: - BuildCommandType (); - ~BuildCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - //get - const MoveSkillType *getMoveSkillType () const - { - return moveSkillType; - } - const BuildSkillType *getBuildSkillType () const - { - return buildSkillType; - } - int getBuildingCount () const - { - return (int) buildings.size (); - } - const UnitType *getBuilding (int i) const - { - return buildings[i]; - } - StaticSound *getStartSound () const - { - return startSounds.getRandSound (); - } - StaticSound *getBuiltSound () const - { - return builtSounds.getRandSound (); - } - - virtual bool usesPathfinder () const - { - return true; - } - }; - - -// =============================== -// class HarvestCommandType -// =============================== - - class HarvestCommandType:public CommandType - { - private: - const MoveSkillType *moveSkillType; - const MoveSkillType *moveLoadedSkillType; - const HarvestSkillType *harvestSkillType; - const StopSkillType *stopLoadedSkillType; - vector < const ResourceType *>harvestedResources; - int maxLoad; - int hitsPerUnit; - - public: - HarvestCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual Queueability isQueuable () const - { - return qOnRequest; - } - - //get - const MoveSkillType *getMoveSkillType () const - { - return moveSkillType; - } - const MoveSkillType *getMoveLoadedSkillType () const - { - return moveLoadedSkillType; - } - const HarvestSkillType *getHarvestSkillType () const - { - return harvestSkillType; - } - const StopSkillType *getStopLoadedSkillType () const - { - return stopLoadedSkillType; - } - int getMaxLoad () const - { - return maxLoad; - } - int getHitsPerUnit () const - { - return hitsPerUnit; - } - int getHarvestedResourceCount () const - { - return (int) harvestedResources.size (); - } - const ResourceType *getHarvestedResource (int i) const - { - return harvestedResources[i]; - } - bool canHarvest (const ResourceType * resourceType) const; - - virtual bool usesPathfinder () const - { - return true; - } - }; - -// =============================== -// class HarvestEmergencyReturnCommandType -// =============================== - - class HarvestEmergencyReturnCommandType:public CommandType - { - private: - - public: - HarvestEmergencyReturnCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual Queueability isQueuable () const - { - return qOnRequest; - } - - //get - virtual bool usesPathfinder () const - { - return true; - } - }; - - -// =============================== -// class RepairCommandType -// =============================== - - class RepairCommandType:public CommandType - { - private: - const MoveSkillType *moveSkillType; - const RepairSkillType *repairSkillType; - vector < const UnitType *>repairableUnits; - - public: - RepairCommandType (); - ~RepairCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - //get - const MoveSkillType *getMoveSkillType () const - { - return moveSkillType; - }; - const RepairSkillType *getRepairSkillType () const - { - return repairSkillType; - }; - bool isRepairableUnitType (const UnitType * unitType) const; - - int getRepairCount () const - { - return (int) repairableUnits.size (); - } - const UnitType *getRepair (int i) const - { - return repairableUnits[i]; - } - - virtual bool usesPathfinder () const - { - return true; - } - }; - - -// =============================== -// class ProduceCommandType -// =============================== - - class ProduceCommandType:public CommandType - { - private: - const ProduceSkillType *produceSkillType; - const UnitType *producedUnit; - - public: - ProduceCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string getReqDesc (bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual const ProducibleType *getProduced () const; - virtual Queueability isQueuable () const - { - return qAlways; - } - virtual int getTypePriority () const - { - return 15; - } - - //get - const ProduceSkillType *getProduceSkillType () const - { - return produceSkillType; - } - const UnitType *getProducedUnit () const - { - return producedUnit; - } - - virtual bool usesPathfinder () const - { - return false; - } - }; - - -// =============================== -// class UpgradeCommandType -// =============================== - - class UpgradeCommandType:public CommandType - { - private: - const UpgradeSkillType *upgradeSkillType; - const UpgradeType *producedUpgrade; - - public: - UpgradeCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual string getReqDesc (bool translatedValue) const; - virtual const ProducibleType *getProduced () const; - virtual Queueability isQueuable () const - { - return qAlways; - } - virtual int getTypePriority () const - { - return 15; - } - - //get - const UpgradeSkillType *getUpgradeSkillType () const - { - return upgradeSkillType; - } - const UpgradeType *getProducedUpgrade () const - { - return producedUpgrade; - } - - virtual bool usesPathfinder () const - { - return false; - } - }; - -// =============================== -// class MorphCommandType -// =============================== - - class MorphCommandType:public CommandType - { - private: - const MorphSkillType *morphSkillType; - const UnitType *morphUnit; - int discount; - bool ignoreResourceRequirements; - - public: - MorphCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - virtual string getReqDesc (bool translatedValue) const; - virtual const ProducibleType *getProduced () const; - Queueability isQueuable () const - { - return qOnlyLast; - } //After morph anything can happen - - //get - const MorphSkillType *getMorphSkillType () const - { - return morphSkillType; - } - const UnitType *getMorphUnit () const - { - return morphUnit; - } - int getDiscount () const - { - return discount; - } - bool getIgnoreResourceRequirements () const - { - return ignoreResourceRequirements; - } - - virtual bool usesPathfinder () const - { - return false; - } - }; - -// =============================== -// class SwitchTeamCommandType -// =============================== - - class SwitchTeamCommandType:public CommandType - { - private: - - - public: - SwitchTeamCommandType (); - virtual void update (UnitUpdater * unitUpdater, Unit * unit, - int frameIndex) const; - virtual void load (int id, const XmlNode * n, const string & dir, - const TechTree * tt, const FactionType * ft, - const UnitType & ut, std::map < string, - vector < pair < string, string > > >&loadedFileList, - string parentLoader); - virtual string getDesc (const TotalUpgrade * totalUpgrade, - bool translatedValue) const; - virtual string toString (bool translatedValue) const; - - virtual bool usesPathfinder () const - { - return false; - } - }; - -// =============================== -// class CommandFactory -// =============================== - - class CommandTypeFactory:public MultiFactory < CommandType > - { - private: - CommandTypeFactory (); - - public: - static CommandTypeFactory & getInstance (); - }; - -}} //end namespace +namespace Glest { + namespace Game { + + using Shared::Util::MultiFactory; + + class UnitUpdater; + class Unit; + class UnitType; + class TechTree; + class FactionType; + + enum CommandClass { + ccStop, + ccMove, + ccAttack, + ccAttackStopped, + ccBuild, + ccHarvest, + ccRepair, + ccProduce, + ccUpgrade, + ccMorph, + ccSwitchTeam, + ccHarvestEmergencyReturn, + + ccCount, + ccNull + }; + + enum Clicks { + cOne, + cTwo + }; + + enum Queueability { + qNever, + qOnRequest, + qOnlyLast, + qAlways + }; + + // ===================================================== + // class CommandType + // + /// A complex action performed by a unit, composed by skills + // ===================================================== + + class CommandType :public RequirableType { + protected: + Clicks clicks; + int id; + + std::map < string, bool > fogOfWarSkillAttachments; + const FogOfWarSkillType *fogOfWarSkillType; + + public: + static const int invalidId = -1; + CommandClass commandTypeClass; + + public: + CommandType() { + commandTypeClass = ccNull; + clicks = cOne; + id = -1; + fogOfWarSkillType = NULL; + fogOfWarSkillAttachments.clear(); + } + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const = 0; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const = 0; + virtual string toString(bool translatedValue) const = 0; + virtual const ProducibleType *getProduced() const { + return NULL; + } + virtual Queueability isQueuable() const { + return qOnRequest; + } + bool isQueuable(bool tryQueue) const { + Queueability q = isQueuable(); + return (q == qAlways) || ((q == qOnRequest || q == qOnlyLast) + && tryQueue); + } + bool isQueueAppendable() const { + Queueability q = isQueuable(); + return (q != qNever) && (q != qOnlyLast); + } + //Priority: commands of higher priority will cancel commands of lower priority + virtual int getTypePriority() const { + return 10; + } + virtual bool usesPathfinder() const = 0; + + //get + CommandClass getClass() const; + Clicks getClicks() const { + return clicks; + } + int getId() const { + return id; + } + + const FogOfWarSkillType *getFogOfWarSkillType() const { + return fogOfWarSkillType; + }; + + bool hasFogOfWarSkillType(string name) const; + }; + + // =============================== + // class StopCommandType + // =============================== + + class StopCommandType :public CommandType { + private: + const StopSkillType *stopSkillType; + + public: + StopCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual Queueability isQueuable() const { + return qNever; + } + virtual int getTypePriority() const { + return 100000; + } + //get + const StopSkillType *getStopSkillType() const { + return stopSkillType; + }; + + virtual bool usesPathfinder() const { + return false; + } + }; + + + // =============================== + // class MoveCommandType + // =============================== + + class MoveCommandType :public CommandType { + private: + const MoveSkillType *moveSkillType; + + public: + MoveCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + //get + const MoveSkillType *getMoveSkillType() const { + return moveSkillType; + }; + + virtual bool usesPathfinder() const { + return true; + } + }; + + + // =============================== + // class AttackCommandType + // =============================== + + class AttackCommandType :public CommandType { + private: + const MoveSkillType *moveSkillType; + const AttackSkillType *attackSkillType; + + public: + AttackCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + + //get + const MoveSkillType *getMoveSkillType() const { + return moveSkillType; + } + const AttackSkillType *getAttackSkillType() const { + return attackSkillType; + } + + virtual bool usesPathfinder() const { + return true; + } + }; + + // ======================================= + // class AttackStoppedCommandType + // ======================================= + + class AttackStoppedCommandType :public CommandType { + private: + const StopSkillType *stopSkillType; + const AttackSkillType *attackSkillType; + + public: + AttackStoppedCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + //get + const StopSkillType *getStopSkillType() const { + return stopSkillType; + } + const AttackSkillType *getAttackSkillType() const { + return attackSkillType; + } + + virtual bool usesPathfinder() const { + return false; + } + }; + + + // =============================== + // class BuildCommandType + // =============================== + + class BuildCommandType :public CommandType { + private: + const MoveSkillType *moveSkillType; + const BuildSkillType *buildSkillType; + vector < const UnitType *>buildings; + SoundContainer startSounds; + SoundContainer builtSounds; + + public: + BuildCommandType(); + ~BuildCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + //get + const MoveSkillType *getMoveSkillType() const { + return moveSkillType; + } + const BuildSkillType *getBuildSkillType() const { + return buildSkillType; + } + int getBuildingCount() const { + return (int) buildings.size(); + } + const UnitType *getBuilding(int i) const { + return buildings[i]; + } + StaticSound *getStartSound() const { + return startSounds.getRandSound(); + } + StaticSound *getBuiltSound() const { + return builtSounds.getRandSound(); + } + + virtual bool usesPathfinder() const { + return true; + } + }; + + + // =============================== + // class HarvestCommandType + // =============================== + + class HarvestCommandType :public CommandType { + private: + const MoveSkillType *moveSkillType; + const MoveSkillType *moveLoadedSkillType; + const HarvestSkillType *harvestSkillType; + const StopSkillType *stopLoadedSkillType; + vector < const ResourceType *>harvestedResources; + int maxLoad; + int hitsPerUnit; + + public: + HarvestCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual Queueability isQueuable() const { + return qOnRequest; + } + + //get + const MoveSkillType *getMoveSkillType() const { + return moveSkillType; + } + const MoveSkillType *getMoveLoadedSkillType() const { + return moveLoadedSkillType; + } + const HarvestSkillType *getHarvestSkillType() const { + return harvestSkillType; + } + const StopSkillType *getStopLoadedSkillType() const { + return stopLoadedSkillType; + } + int getMaxLoad() const { + return maxLoad; + } + int getHitsPerUnit() const { + return hitsPerUnit; + } + int getHarvestedResourceCount() const { + return (int) harvestedResources.size(); + } + const ResourceType *getHarvestedResource(int i) const { + return harvestedResources[i]; + } + bool canHarvest(const ResourceType * resourceType) const; + + virtual bool usesPathfinder() const { + return true; + } + }; + + // =============================== + // class HarvestEmergencyReturnCommandType + // =============================== + + class HarvestEmergencyReturnCommandType :public CommandType { + private: + + public: + HarvestEmergencyReturnCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual Queueability isQueuable() const { + return qOnRequest; + } + + //get + virtual bool usesPathfinder() const { + return true; + } + }; + + + // =============================== + // class RepairCommandType + // =============================== + + class RepairCommandType :public CommandType { + private: + const MoveSkillType *moveSkillType; + const RepairSkillType *repairSkillType; + vector < const UnitType *>repairableUnits; + + public: + RepairCommandType(); + ~RepairCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + //get + const MoveSkillType *getMoveSkillType() const { + return moveSkillType; + }; + const RepairSkillType *getRepairSkillType() const { + return repairSkillType; + }; + bool isRepairableUnitType(const UnitType * unitType) const; + + int getRepairCount() const { + return (int) repairableUnits.size(); + } + const UnitType *getRepair(int i) const { + return repairableUnits[i]; + } + + virtual bool usesPathfinder() const { + return true; + } + }; + + + // =============================== + // class ProduceCommandType + // =============================== + + class ProduceCommandType :public CommandType { + private: + const ProduceSkillType *produceSkillType; + const UnitType *producedUnit; + + public: + ProduceCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string getReqDesc(bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual const ProducibleType *getProduced() const; + virtual Queueability isQueuable() const { + return qAlways; + } + virtual int getTypePriority() const { + return 15; + } + + //get + const ProduceSkillType *getProduceSkillType() const { + return produceSkillType; + } + const UnitType *getProducedUnit() const { + return producedUnit; + } + + virtual bool usesPathfinder() const { + return false; + } + }; + + + // =============================== + // class UpgradeCommandType + // =============================== + + class UpgradeCommandType :public CommandType { + private: + const UpgradeSkillType *upgradeSkillType; + const UpgradeType *producedUpgrade; + + public: + UpgradeCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual string getReqDesc(bool translatedValue) const; + virtual const ProducibleType *getProduced() const; + virtual Queueability isQueuable() const { + return qAlways; + } + virtual int getTypePriority() const { + return 15; + } + + //get + const UpgradeSkillType *getUpgradeSkillType() const { + return upgradeSkillType; + } + const UpgradeType *getProducedUpgrade() const { + return producedUpgrade; + } + + virtual bool usesPathfinder() const { + return false; + } + }; + + // =============================== + // class MorphCommandType + // =============================== + + class MorphCommandType :public CommandType { + private: + const MorphSkillType *morphSkillType; + const UnitType *morphUnit; + int discount; + bool ignoreResourceRequirements; + + public: + MorphCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + virtual string getReqDesc(bool translatedValue) const; + virtual const ProducibleType *getProduced() const; + Queueability isQueuable() const { + return qOnlyLast; + } //After morph anything can happen + + //get + const MorphSkillType *getMorphSkillType() const { + return morphSkillType; + } + const UnitType *getMorphUnit() const { + return morphUnit; + } + int getDiscount() const { + return discount; + } + bool getIgnoreResourceRequirements() const { + return ignoreResourceRequirements; + } + + virtual bool usesPathfinder() const { + return false; + } + }; + + // =============================== + // class SwitchTeamCommandType + // =============================== + + class SwitchTeamCommandType :public CommandType { + private: + + + public: + SwitchTeamCommandType(); + virtual void update(UnitUpdater * unitUpdater, Unit * unit, + int frameIndex) const; + virtual void load(int id, const XmlNode * n, const string & dir, + const TechTree * tt, const FactionType * ft, + const UnitType & ut, std::map < string, + vector < pair < string, string > > >&loadedFileList, + string parentLoader); + virtual string getDesc(const TotalUpgrade * totalUpgrade, + bool translatedValue) const; + virtual string toString(bool translatedValue) const; + + virtual bool usesPathfinder() const { + return false; + } + }; + + // =============================== + // class CommandFactory + // =============================== + + class CommandTypeFactory :public MultiFactory < CommandType > { + private: + CommandTypeFactory(); + + public: + static CommandTypeFactory & getInstance(); + }; + + } +} //end namespace #endif diff --git a/source/glest_game/types/damage_multiplier.cpp b/source/glest_game/types/damage_multiplier.cpp index 9fc93be5c..d045bd09d 100644 --- a/source/glest_game/types/damage_multiplier.cpp +++ b/source/glest_game/types/damage_multiplier.cpp @@ -16,117 +16,104 @@ using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - void AttackType::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *attackTypeNode = rootNode->addChild ("AttackType"); + void AttackType::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *attackTypeNode = rootNode->addChild("AttackType"); -// string name; - attackTypeNode->addAttribute ("name", name, mapTagReplacements); -// int id; - attackTypeNode->addAttribute ("id", intToStr (id), mapTagReplacements); - } + // string name; + attackTypeNode->addAttribute("name", name, mapTagReplacements); + // int id; + attackTypeNode->addAttribute("id", intToStr(id), mapTagReplacements); + } - string AttackType::getName (bool translatedValue) const - { - if (translatedValue == false) - return name; + string AttackType::getName(bool translatedValue) const { + if (translatedValue == false) + return name; - Lang & lang = Lang::getInstance (); - return lang.getTechTreeString ("AttackTypeName_" + name, name.c_str ()); - } + Lang & lang = Lang::getInstance(); + return lang.getTechTreeString("AttackTypeName_" + name, name.c_str()); + } - void ArmorType::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *armorTypeNode = rootNode->addChild ("ArmorType"); + void ArmorType::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *armorTypeNode = rootNode->addChild("ArmorType"); -// string name; - armorTypeNode->addAttribute ("name", name, mapTagReplacements); -// int id; - armorTypeNode->addAttribute ("id", intToStr (id), mapTagReplacements); - } + // string name; + armorTypeNode->addAttribute("name", name, mapTagReplacements); + // int id; + armorTypeNode->addAttribute("id", intToStr(id), mapTagReplacements); + } - string ArmorType::getName (bool translatedValue) const - { - if (translatedValue == false) - return name; + string ArmorType::getName(bool translatedValue) const { + if (translatedValue == false) + return name; - Lang & lang = Lang::getInstance (); - return lang.getTechTreeString ("ArmorTypeName_" + name, name.c_str ()); - } -// ===================================================== -// class DamageMultiplierTable -// ===================================================== + Lang & lang = Lang::getInstance(); + return lang.getTechTreeString("ArmorTypeName_" + name, name.c_str()); + } + // ===================================================== + // class DamageMultiplierTable + // ===================================================== - DamageMultiplierTable::DamageMultiplierTable () - { - values = NULL; - attackTypeCount = 0; - armorTypeCount = 0; - } + DamageMultiplierTable::DamageMultiplierTable() { + values = NULL; + attackTypeCount = 0; + armorTypeCount = 0; + } - DamageMultiplierTable::~DamageMultiplierTable () - { - delete[]values; - values = NULL; - } + DamageMultiplierTable::~DamageMultiplierTable() { + delete[]values; + values = NULL; + } - void DamageMultiplierTable::init (int attackTypeCount, int armorTypeCount) - { - this->attackTypeCount = attackTypeCount; - this->armorTypeCount = armorTypeCount; + void DamageMultiplierTable::init(int attackTypeCount, int armorTypeCount) { + this->attackTypeCount = attackTypeCount; + this->armorTypeCount = armorTypeCount; - int valueCount = attackTypeCount * armorTypeCount; - values = new double[valueCount]; - for (int i = 0; i < valueCount; ++i) - { - values[i] = 1.f; - } - } + int valueCount = attackTypeCount * armorTypeCount; + values = new double[valueCount]; + for (int i = 0; i < valueCount; ++i) { + values[i] = 1.f; + } + } - double DamageMultiplierTable::getDamageMultiplier (const AttackType * att, - const ArmorType * - art) const - { - return values[attackTypeCount * art->getId () + att->getId ()]; - } + double DamageMultiplierTable::getDamageMultiplier(const AttackType * att, + const ArmorType * + art) const { + return values[attackTypeCount * art->getId() + att->getId()]; + } - void DamageMultiplierTable::setDamageMultiplier (const AttackType * att, - const ArmorType * art, - double value) - { - values[attackTypeCount * art->getId () + att->getId ()] = value; - } + void DamageMultiplierTable::setDamageMultiplier(const AttackType * att, + const ArmorType * art, + double value) { + values[attackTypeCount * art->getId() + att->getId()] = value; + } - void DamageMultiplierTable::saveGame (XmlNode * rootNode) - { - std::map < string, string > mapTagReplacements; - XmlNode *damageMultiplierTableNode = - rootNode->addChild ("DamageMultiplierTable"); + void DamageMultiplierTable::saveGame(XmlNode * rootNode) { + std::map < string, string > mapTagReplacements; + XmlNode *damageMultiplierTableNode = + rootNode->addChild("DamageMultiplierTable"); -// float *values; -// int attackTypeCount; -// int armorTypeCount; - damageMultiplierTableNode->addAttribute ("attackTypeCount", - intToStr (attackTypeCount), - mapTagReplacements); - damageMultiplierTableNode->addAttribute ("armorTypeCount", - intToStr (armorTypeCount), - mapTagReplacements); + // float *values; + // int attackTypeCount; + // int armorTypeCount; + damageMultiplierTableNode->addAttribute("attackTypeCount", + intToStr(attackTypeCount), + mapTagReplacements); + damageMultiplierTableNode->addAttribute("armorTypeCount", + intToStr(armorTypeCount), + mapTagReplacements); - int valueCount = attackTypeCount * armorTypeCount; - for (int i = 0; i < valueCount; ++i) - { - XmlNode *valuesNode = damageMultiplierTableNode->addChild ("values"); - valuesNode->addAttribute ("value", doubleToStr (values[i]), - mapTagReplacements); - } - } + int valueCount = attackTypeCount * armorTypeCount; + for (int i = 0; i < valueCount; ++i) { + XmlNode *valuesNode = damageMultiplierTableNode->addChild("values"); + valuesNode->addAttribute("value", doubleToStr(values[i]), + mapTagReplacements); + } + } -}} //end namespaces + } +} //end namespaces diff --git a/source/glest_game/types/damage_multiplier.h b/source/glest_game/types/damage_multiplier.h index 8bfc257dd..b703e126c 100644 --- a/source/glest_game/types/damage_multiplier.h +++ b/source/glest_game/types/damage_multiplier.h @@ -24,104 +24,92 @@ using std::string; using Shared::Xml::XmlNode; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// =============================== -// class AttackType -// =============================== + // =============================== + // class AttackType + // =============================== - class AttackType - { - private: - string name; - int id; + class AttackType { + private: + string name; + int id; - public: - AttackType () - { - id = -1; - } - int getId () const - { - return id; - } - string getName (bool translatedValue = false) const; + public: + AttackType() { + id = -1; + } + int getId() const { + return id; + } + string getName(bool translatedValue = false) const; - void setName (const string & name) - { - this->name = name; - } - void setId (int id) - { - this->id = id; - } + void setName(const string & name) { + this->name = name; + } + void setId(int id) { + this->id = id; + } - void saveGame (XmlNode * rootNode); - }; + void saveGame(XmlNode * rootNode); + }; -// =============================== -// class ArmorType -// =============================== + // =============================== + // class ArmorType + // =============================== - class ArmorType - { - private: - string name; - int id; + class ArmorType { + private: + string name; + int id; - public: - ArmorType () - { - id = -1; - } - int getId () const - { - return id; - } - string getName (bool translatedValue = false) const; + public: + ArmorType() { + id = -1; + } + int getId() const { + return id; + } + string getName(bool translatedValue = false) const; - void setName (const string & name) - { - this->name = name; - } - void setId (int id) - { - this->id = id; - } + void setName(const string & name) { + this->name = name; + } + void setId(int id) { + this->id = id; + } - void saveGame (XmlNode * rootNode); - }; + void saveGame(XmlNode * rootNode); + }; -// ===================================================== -// class DamageMultiplierTable -// -/// Some attack types have bonuses against some -/// armor types and vice-versa -// ===================================================== + // ===================================================== + // class DamageMultiplierTable + // + /// Some attack types have bonuses against some + /// armor types and vice-versa + // ===================================================== - class DamageMultiplierTable - { - private: - double *values; - int attackTypeCount; - int armorTypeCount; + class DamageMultiplierTable { + private: + double *values; + int attackTypeCount; + int armorTypeCount; - public: - DamageMultiplierTable (); - ~DamageMultiplierTable (); + public: + DamageMultiplierTable(); + ~DamageMultiplierTable(); - void init (int attackTypeCount, int armorTypeCount); - double getDamageMultiplier (const AttackType * att, - const ArmorType * art) const; - void setDamageMultiplier (const AttackType * att, const ArmorType * art, - double value); + void init(int attackTypeCount, int armorTypeCount); + double getDamageMultiplier(const AttackType * att, + const ArmorType * art) const; + void setDamageMultiplier(const AttackType * att, const ArmorType * art, + double value); - void saveGame (XmlNode * rootNode); - }; + void saveGame(XmlNode * rootNode); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/types/element_type.cpp b/source/glest_game/types/element_type.cpp index facd89c5e..37a358a0c 100644 --- a/source/glest_game/types/element_type.cpp +++ b/source/glest_game/types/element_type.cpp @@ -25,232 +25,197 @@ using namespace Shared::Util; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { -// ===================================================== -// class DisplayableType -// ===================================================== + // ===================================================== + // class DisplayableType + // ===================================================== - DisplayableType::DisplayableType () - { - image = NULL; - } + DisplayableType::DisplayableType() { + image = NULL; + } - string DisplayableType::getName (bool translatedValue) const - { - if (translatedValue == false) - return name; + string DisplayableType::getName(bool translatedValue) const { + if (translatedValue == false) + return name; - Lang & lang = Lang::getInstance (); - return lang.getTechTreeString ("CommandName_" + name, name.c_str ()); - } + Lang & lang = Lang::getInstance(); + return lang.getTechTreeString("CommandName_" + name, name.c_str()); + } -//void DisplayableType::saveGame(XmlNode *rootNode) const { -// std::map mapTagReplacements; -// XmlNode *displayableTypeNode = rootNode->addChild("DisplayableType"); -// -// displayableTypeNode->addAttribute("name",name, mapTagReplacements); -//} + //void DisplayableType::saveGame(XmlNode *rootNode) const { + // std::map mapTagReplacements; + // XmlNode *displayableTypeNode = rootNode->addChild("DisplayableType"); + // + // displayableTypeNode->addAttribute("name",name, mapTagReplacements); + //} -// ===================================================== -// class RequirableType -// ===================================================== + // ===================================================== + // class RequirableType + // ===================================================== - string RequirableType::getReqDesc (bool translatedValue) const - { - bool anyReqs = false; + string RequirableType::getReqDesc(bool translatedValue) const { + bool anyReqs = false; - string reqString = ""; - for (int i = 0; i < getUnitReqCount (); ++i) - { - if (getUnitReq (i) == NULL) - { - throw megaglest_runtime_error ("getUnitReq(i) == NULL"); - } - reqString += getUnitReq (i)->getName (translatedValue); - reqString += "\n"; - anyReqs = true; - } + string reqString = ""; + for (int i = 0; i < getUnitReqCount(); ++i) { + if (getUnitReq(i) == NULL) { + throw megaglest_runtime_error("getUnitReq(i) == NULL"); + } + reqString += getUnitReq(i)->getName(translatedValue); + reqString += "\n"; + anyReqs = true; + } - for (int i = 0; i < getUpgradeReqCount (); ++i) - { - if (getUpgradeReq (i) == NULL) - { - throw megaglest_runtime_error ("getUpgradeReq(i) == NULL"); - } + for (int i = 0; i < getUpgradeReqCount(); ++i) { + if (getUpgradeReq(i) == NULL) { + throw megaglest_runtime_error("getUpgradeReq(i) == NULL"); + } - reqString += getUpgradeReq (i)->getName (translatedValue); - reqString += "\n"; - anyReqs = true; - } + reqString += getUpgradeReq(i)->getName(translatedValue); + reqString += "\n"; + anyReqs = true; + } - string str = getName (translatedValue); - if (anyReqs) - { - return str + " " + Lang::getInstance ().getString ("Reqs", - (translatedValue == - true ? "" : - "english")) + - ":\n" + reqString; - } - else - { - return str; - } - } + string str = getName(translatedValue); + if (anyReqs) { + return str + " " + Lang::getInstance().getString("Reqs", + (translatedValue == + true ? "" : + "english")) + + ":\n" + reqString; + } else { + return str; + } + } -//void RequirableType::saveGame(XmlNode *rootNode) const { -// DisplayableType::saveGame(rootNode); -// -// std::map mapTagReplacements; -// XmlNode *requirableTypeNode = rootNode->addChild("RequirableType"); -// -//// UnitReqs unitReqs; //needed units -// for(unsigned int i = 0; i < unitReqs.size(); ++i) { -// const UnitType *ut = unitReqs[i]; -// -// XmlNode *unitReqsNode = requirableTypeNode->addChild("unitReqs"); -// unitReqsNode->addAttribute("name",ut->getName(), mapTagReplacements); -// } -//// UpgradeReqs upgradeReqs; //needed upgrades -// for(unsigned int i = 0; i < upgradeReqs.size(); ++i) { -// const UpgradeType* ut = upgradeReqs[i]; -// -// ut->saveGame(requirableTypeNode); -// } -// -//} + //void RequirableType::saveGame(XmlNode *rootNode) const { + // DisplayableType::saveGame(rootNode); + // + // std::map mapTagReplacements; + // XmlNode *requirableTypeNode = rootNode->addChild("RequirableType"); + // + //// UnitReqs unitReqs; //needed units + // for(unsigned int i = 0; i < unitReqs.size(); ++i) { + // const UnitType *ut = unitReqs[i]; + // + // XmlNode *unitReqsNode = requirableTypeNode->addChild("unitReqs"); + // unitReqsNode->addAttribute("name",ut->getName(), mapTagReplacements); + // } + //// UpgradeReqs upgradeReqs; //needed upgrades + // for(unsigned int i = 0; i < upgradeReqs.size(); ++i) { + // const UpgradeType* ut = upgradeReqs[i]; + // + // ut->saveGame(requirableTypeNode); + // } + // + //} -// ===================================================== -// class ProducibleType -// ===================================================== + // ===================================================== + // class ProducibleType + // ===================================================== - ProducibleType::ProducibleType () - { - cancelImage = NULL; - productionTime = 0; - } + ProducibleType::ProducibleType() { + cancelImage = NULL; + productionTime = 0; + } - ProducibleType::~ProducibleType () - { - } + ProducibleType::~ProducibleType() { + } - const Resource *ProducibleType::getCost (const ResourceType * rt) const - { - for (int i = 0; i < (int) costs.size (); ++i) - { - if (costs[i].getType () == rt) - { - return &costs[i]; - } - } - return NULL; - } + const Resource *ProducibleType::getCost(const ResourceType * rt) const { + for (int i = 0; i < (int) costs.size(); ++i) { + if (costs[i].getType() == rt) { + return &costs[i]; + } + } + return NULL; + } - string ProducibleType::getReqDesc (bool translatedValue) const - { - return getReqDesc (false, translatedValue); - } + string ProducibleType::getReqDesc(bool translatedValue) const { + return getReqDesc(false, translatedValue); + } - string ProducibleType::getResourceReqDesc (bool lineBreaks, - bool translatedValue) const - { - string str = ""; - for (int i = 0; i < getCostCount (); ++i) - { - if (getCost (i)->getAmount () != 0) - { - str += getCost (i)->getType ()->getName (translatedValue); - str += ": " + intToStr (getCost (i)->getAmount ()); - if (lineBreaks == true) - { - str += "\n"; - } - else - { - str += " "; - } - } - } + string ProducibleType::getResourceReqDesc(bool lineBreaks, + bool translatedValue) const { + string str = ""; + for (int i = 0; i < getCostCount(); ++i) { + if (getCost(i)->getAmount() != 0) { + str += getCost(i)->getType()->getName(translatedValue); + str += ": " + intToStr(getCost(i)->getAmount()); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; + } + } + } - return str; - } + return str; + } - string ProducibleType::getUnitAndUpgradeReqDesc (bool lineBreaks, - bool translatedValue) - const - { - string str = ""; - for (int i = 0; i < getUnitReqCount (); ++i) - { - str += getUnitReq (i)->getName (translatedValue); - if (lineBreaks == true) - { - str += "\n"; - } - else - { - str += " "; - } - } + string ProducibleType::getUnitAndUpgradeReqDesc(bool lineBreaks, + bool translatedValue) + const { + string str = ""; + for (int i = 0; i < getUnitReqCount(); ++i) { + str += getUnitReq(i)->getName(translatedValue); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; + } + } - for (int i = 0; i < getUpgradeReqCount (); ++i) - { - str += getUpgradeReq (i)->getName (translatedValue); - if (lineBreaks == true) - { - str += "\n"; - } - else - { - str += " "; - } - } + for (int i = 0; i < getUpgradeReqCount(); ++i) { + str += getUpgradeReq(i)->getName(translatedValue); + if (lineBreaks == true) { + str += "\n"; + } else { + str += " "; + } + } - return str; - } + return str; + } - string ProducibleType::getReqDesc (bool ignoreResourceRequirements, - bool translatedValue) const - { - string str = - getName (translatedValue) + " " + - Lang::getInstance ().getString ("Reqs", - (translatedValue == - true ? "" : "english")) + ":\n"; - if (ignoreResourceRequirements == false) - { - str += getResourceReqDesc (true, translatedValue); - } + string ProducibleType::getReqDesc(bool ignoreResourceRequirements, + bool translatedValue) const { + string str = + getName(translatedValue) + " " + + Lang::getInstance().getString("Reqs", + (translatedValue == + true ? "" : "english")) + ":\n"; + if (ignoreResourceRequirements == false) { + str += getResourceReqDesc(true, translatedValue); + } - str += getUnitAndUpgradeReqDesc (true, translatedValue); - return str; - } + str += getUnitAndUpgradeReqDesc(true, translatedValue); + return str; + } -//void ProducibleType::saveGame(XmlNode *rootNode) const { -// RequirableType::saveGame(rootNode); -// -// std::map mapTagReplacements; -// XmlNode *producibleTypeNode = rootNode->addChild("ProducibleType"); -// -//// Costs costs; -// for(unsigned int i = 0; i < costs.size(); ++i) { -// const Resource &res = costs[i]; -// res.saveGame(producibleTypeNode); -// } -//// Texture2D *cancelImage; -//// int productionTime; -// producibleTypeNode->addAttribute("productionTime",intToStr(productionTime), mapTagReplacements); -//} + //void ProducibleType::saveGame(XmlNode *rootNode) const { + // RequirableType::saveGame(rootNode); + // + // std::map mapTagReplacements; + // XmlNode *producibleTypeNode = rootNode->addChild("ProducibleType"); + // + //// Costs costs; + // for(unsigned int i = 0; i < costs.size(); ++i) { + // const Resource &res = costs[i]; + // res.saveGame(producibleTypeNode); + // } + //// Texture2D *cancelImage; + //// int productionTime; + // producibleTypeNode->addAttribute("productionTime",intToStr(productionTime), mapTagReplacements); + //} -//void ProducibleType::loadGame(const XmlNode *rootNode) { -// const XmlNode *producibleTypeNode = rootNode->getChild("ProducibleType"); -// -// //int newUnitId = producibleTypeNode->getAttribute("id")->getIntValue(); -//} + //void ProducibleType::loadGame(const XmlNode *rootNode) { + // const XmlNode *producibleTypeNode = rootNode->getChild("ProducibleType"); + // + // //int newUnitId = producibleTypeNode->getAttribute("id")->getIntValue(); + //} - } + } } //end namespace diff --git a/source/glest_game/types/element_type.h b/source/glest_game/types/element_type.h index a3b0d2b65..f0b80b8d0 100644 --- a/source/glest_game/types/element_type.h +++ b/source/glest_game/types/element_type.h @@ -28,142 +28,128 @@ using std::string; using Shared::Graphics::Texture2D; -namespace Glest -{ - namespace Game - { +namespace Glest { + namespace Game { - class UpgradeType; - class TechTree; - class UnitType; - class UpgradeType; - class DisplayableType; - class ResourceType; + class UpgradeType; + class TechTree; + class UnitType; + class UpgradeType; + class DisplayableType; + class ResourceType; -// ===================================================== -// class DisplayableType -// -/// Base class for anything that has a name and a portrait -// ===================================================== + // ===================================================== + // class DisplayableType + // + /// Base class for anything that has a name and a portrait + // ===================================================== - class DisplayableType - { - protected: - string name; //name - Texture2D *image; //portrait + class DisplayableType { + protected: + string name; //name + Texture2D *image; //portrait - public: - DisplayableType (); - virtual ~ DisplayableType () - { - }; + public: + DisplayableType(); + virtual ~DisplayableType() { + }; - //get - virtual string getName (bool translatedValue = false) const; - virtual const Texture2D *getImage () const - { - return image; - } + //get + virtual string getName(bool translatedValue = false) const; + virtual const Texture2D *getImage() const { + return image; + } - //virtual void saveGame(XmlNode *rootNode) const; - }; + //virtual void saveGame(XmlNode *rootNode) const; + }; -// ===================================================== -// class RequirableType -// -/// Base class for anything that has requirements -// ===================================================== + // ===================================================== + // class RequirableType + // + /// Base class for anything that has requirements + // ===================================================== - class RequirableType:public DisplayableType - { - private: - typedef vector < const UnitType *>UnitReqs; - typedef vector < const UpgradeType *>UpgradeReqs; + class RequirableType :public DisplayableType { + private: + typedef vector < const UnitType *>UnitReqs; + typedef vector < const UpgradeType *>UpgradeReqs; - protected: - UnitReqs unitReqs; //needed units - UpgradeReqs upgradeReqs; //needed upgrades + protected: + UnitReqs unitReqs; //needed units + UpgradeReqs upgradeReqs; //needed upgrades - public: - //get - int getUpgradeReqCount () const - { - return (int) upgradeReqs.size (); - } - int getUnitReqCount () const - { - return (int) unitReqs.size (); - } - const UpgradeType *getUpgradeReq (int i) const - { - return upgradeReqs[i]; - } - const UnitType *getUnitReq (int i) const - { - return unitReqs[i]; - } + public: + //get + int getUpgradeReqCount() const { + return (int) upgradeReqs.size(); + } + int getUnitReqCount() const { + return (int) unitReqs.size(); + } + const UpgradeType *getUpgradeReq(int i) const { + return upgradeReqs[i]; + } + const UnitType *getUnitReq(int i) const { + return unitReqs[i]; + } - //other - virtual string getReqDesc (bool translatedValue) const; + //other + virtual string getReqDesc(bool translatedValue) const; - //virtual void saveGame(XmlNode *rootNode) const; - }; + //virtual void saveGame(XmlNode *rootNode) const; + }; -// ===================================================== -// class ProducibleType -// -/// Base class for anything that can be produced -// ===================================================== + // ===================================================== + // class ProducibleType + // + /// Base class for anything that can be produced + // ===================================================== - class ProducibleType:public RequirableType - { - private: - typedef vector < Resource > Costs; + class ProducibleType :public RequirableType { + private: + typedef vector < Resource > Costs; - protected: - Costs costs; - Texture2D *cancelImage; - int productionTime; + protected: + Costs costs; + Texture2D *cancelImage; + int productionTime; - public: - ProducibleType (); - virtual ~ ProducibleType (); + public: + ProducibleType(); + virtual ~ProducibleType(); - //get - int getCostCount () const - { - return (int) costs.size (); - } - const Resource *getCost (int i) const - { - return &costs[i]; - } - const Resource *getCost (const ResourceType * rt) const; - int getProductionTime () const - { - return productionTime; - } - const Texture2D *getCancelImage () const - { - return cancelImage; - } + //get + int getCostCount() const { + return (int) costs.size(); + } + const Resource *getCost(int i) const { + return &costs[i]; + } + const Resource *getCost(const ResourceType * rt) const; + int getProductionTime() const { + return productionTime; + } + const Texture2D *getCancelImage() const { + return cancelImage; + } - //varios - void checkCostStrings (TechTree * techTree); + //varios + void checkCostStrings(TechTree * techTree); - virtual string getReqDesc (bool translatedValue) const; - string getResourceReqDesc (bool lineBreaks, bool translatedValue) const; - string getUnitAndUpgradeReqDesc (bool lineBreaks, - bool translatedValue) const; - string getReqDesc (bool ignoreResourceRequirements, - bool translatedValue) const; + virtual string getReqDesc(bool translatedValue) const; + string getResourceReqDesc(bool lineBreaks, bool translatedValue) const; + string getUnitAndUpgradeReqDesc(bool lineBreaks, + bool translatedValue) const; + string getReqDesc(bool ignoreResourceRequirements, + bool translatedValue) const; -// virtual void saveGame(XmlNode *rootNode) const; -// void loadGame(const XmlNode *rootNode); - }; + // virtual void saveGame(XmlNode *rootNode) const; + // void loadGame(const XmlNode *rootNode); + }; -}} //end namespace + } +} //end namespace #endif diff --git a/source/glest_game/types/faction_type.cpp b/source/glest_game/types/faction_type.cpp index 5e232b5eb..4bdc03967 100644 --- a/source/glest_game/types/faction_type.cpp +++ b/source/glest_game/types/faction_type.cpp @@ -25,1565 +25,1369 @@ using namespace Shared::Util; using namespace Shared::Xml; -namespace Glest -{ - namespace Game - { - -// ====================================================== -// Class FactionType -// ====================================================== - - FactionType::FactionType () - { - music = NULL; - personalityType = fpt_Normal; - isLinked = false; - healthbarheight = -100.0f; - healthbarthickness = 0.11f; - healthbarVisible = hbvUndefined; - healthbarBorderTextureEnabled = false; - healthbarBackgroundTextureEnabled = false; - healthbarLineBorder = true; - healthbarTexture = NULL; - healthbarBackgroundTexture = NULL; - flatParticlePositions = false; - } - -//load a faction, given a directory - void FactionType::load (const string & factionName, - const TechTree * techTree, Checksum * checksum, - Checksum * techtreeChecksum, std::map < string, - vector < pair < string, - string > > >&loadedFileList, bool validationMode) - { - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - - string techTreePath = techTree->getPath (); - string techTreeName = techTree->getNameUntranslated (); - string currentPath = ""; - - //open xml file - string path = ""; - XmlTree xmlTree; - const XmlNode *factionNode; - - //printf("\n>>> factionname=%s\n",factionName.c_str()); - for (bool realFactionPathFound = false; realFactionPathFound == false;) - { - currentPath = techTreePath + "factions/" + factionName; - endPathWithSlash (currentPath); - - name = lastDir (currentPath); - - // Add special Observer Faction - //Lang &lang= Lang::getInstance(); - if (name == formatString (GameConstants::OBSERVER_SLOTNAME)) - { - personalityType = fpt_Observer; - } - - if (personalityType == fpt_Normal) - { - string tmppath = currentPath + factionName + ".xml"; - std::map < string, string > mapExtraTagReplacementValues; - // mapExtraTagReplacementValues["$COMMONDATAPATH"] = techTreePath + "/commondata/"; - //printf("current $COMMONDATAPATH = %s\n",mapExtraTagReplacementValues["$COMMONDATAPATH"].c_str()); - XmlTree xmlTree; - xmlTree.load (tmppath, - Properties:: - getTagReplacementValues - (&mapExtraTagReplacementValues)); - - - const XmlNode *rootNode = xmlTree.getRootNode (); - - if (rootNode->getName () == "link") - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "Faction [%s] is a linked faction\n", - name.c_str ()); - - isLinked = true; - const XmlNode *techTreeNode = rootNode->getChild ("techtree"); - const string linkedTechTreeName = - techTreeNode->getAttribute ("name")->getRestrictedValue (); - // const XmlNode *factionLinkNode= rootNode->getChild("faction"); - // string linkedFactionName=factionLinkNode->getAttribute("name")->getRestrictedValue(); - string linkedTechTreePath = - techTree->findPath (linkedTechTreeName); - techTreePath = linkedTechTreePath; - endPathWithSlash (techTreePath); - techTreeName = linkedTechTreeName; - - string linkedCurrentPath = - techTreePath + "factions/" + factionName; - endPathWithSlash (linkedCurrentPath); - string linkedTmppath = linkedCurrentPath + factionName + ".xml"; - - //printf("linkedTmppath [%s] linkedCurrentPath [%s]\n",linkedTmppath.c_str(),linkedCurrentPath.c_str()); - - loadedFileList[linkedTmppath]. - push_back (make_pair (linkedCurrentPath, linkedCurrentPath)); - loadedFileList[tmppath]. - push_back (make_pair (currentPath, currentPath)); - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "techTreePath [%s] techTreeName [%s]\n", - techTreePath.c_str (), - techTreeName.c_str ()); - } - else - { - // stop looking for new path, no more links ... - //xmlTree.load(tmppath, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); - - loadedFileList[tmppath]. - push_back (make_pair (currentPath, currentPath)); - - realFactionPathFound = true; - //printf("techPath found! %s\n",tmppath.c_str()); - - path = tmppath; - } - } - else - { - break; - } - } - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - Lang::getInstance (). - getString ("LogScreenGameLoadingFactionType", "", - true).c_str (), - formatString (this->getName ()).c_str ()); - Logger::getInstance ().add (szBuf, true); - - if (personalityType == fpt_Normal) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "Loading faction [%s] currentPath [%s]\n", - path.c_str (), currentPath.c_str ()); - - checksum->addFile (path); - techtreeChecksum->addFile (path); - - // a1) preload units - //string unitsPath= currentPath + "units/*."; - string unitsPath = currentPath + "units/"; - vector < string > unitFilenames; - //findAll(unitsPath, unitFilenames); - findDirs (unitsPath, unitFilenames, false, false); - - unitTypes.resize (unitFilenames.size ()); - - for (int i = 0; i < (int) unitTypes.size (); ++i) - { - string str = currentPath + "units/" + unitFilenames[i]; - unitTypes[i].preLoad (str); - - SDL_PumpEvents (); - } - - // a2) preload upgrades - //string upgradesPath= currentPath + "upgrades/*."; - string upgradesPath = currentPath + "upgrades/"; - vector < string > upgradeFilenames; - //findAll(upgradesPath, upgradeFilenames, false, false); - findDirs (upgradesPath, upgradeFilenames, false, false); - - upgradeTypes.resize (upgradeFilenames.size ()); - for (int i = 0; i < (int) upgradeTypes.size (); ++i) - { - string str = currentPath + "upgrades/" + upgradeFilenames[i]; - upgradeTypes[i].preLoad (str); - - SDL_PumpEvents (); - } - - // b1) load units - try - { - Logger & logger = Logger::getInstance (); - int progressBaseValue = logger.getProgress (); - for (int i = 0; i < (int) unitTypes.size (); ++i) - { - string str = currentPath + "units/" + unitTypes[i].getName (); - - try - { - unitTypes[i].loaddd (i, str, techTree, techTreePath, this, - checksum, techtreeChecksum, loadedFileList, - validationMode); - logger.setProgress (progressBaseValue + - (int) ((((double) i + - 1.0) / - (double) unitTypes.size ()) * - 100.0 / techTree->getTypeCount ())); - SDL_PumpEvents (); - } - catch (megaglest_runtime_error & ex) - { - if (validationMode == false) - { - throw; - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, ex.what ()); - } - } - } - } - catch (megaglest_runtime_error & ex) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - ex.what ()); - throw megaglest_runtime_error ("Error loading units: " + - currentPath + "\nMessage: " + - ex.what (), !ex.wantStackTrace ()); - } - catch (const exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - e.what ()); - throw megaglest_runtime_error ("Error loading units: " + - currentPath + "\nMessage: " + - e.what ()); - } - - // b2) load upgrades - try - { - for (int i = 0; i < (int) upgradeTypes.size (); ++i) - { - string str = - currentPath + "upgrades/" + upgradeTypes[i].getName (); - - try - { - upgradeTypes[i].load (str, techTree, this, checksum, - techtreeChecksum, loadedFileList, - validationMode); - } - catch (megaglest_runtime_error & ex) - { - if (validationMode == false) - { - throw; - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, ex.what ()); - } - } - - SDL_PumpEvents (); - } - } - catch (const exception & e) - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - e.what ()); - throw megaglest_runtime_error ("Error loading upgrades: " + - currentPath + "\n" + e.what ()); - } - - string tmppath = currentPath + factionName + ".xml"; - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "Loading faction xml [%s]\n", - tmppath.c_str ()); - - std::map < string, string > mapExtraTagReplacementValues; - mapExtraTagReplacementValues["$COMMONDATAPATH"] = - techTreePath + "/commondata/"; - //printf("current $COMMONDATAPATH = %s\n",mapExtraTagReplacementValues["$COMMONDATAPATH"].c_str()); - xmlTree.load (tmppath, - Properties:: - getTagReplacementValues - (&mapExtraTagReplacementValues)); - - - factionNode = xmlTree.getRootNode (); - //read starting resources - //printf("factionNode->getName()=%s",factionNode->getName().c_str()); - const XmlNode *startingResourcesNode = - factionNode->getChild ("starting-resources"); - - startingResources.resize (startingResourcesNode->getChildCount ()); - for (int i = 0; i < (int) startingResources.size (); ++i) - { - const XmlNode *resourceNode = - startingResourcesNode->getChild ("resource", i); - string name = - resourceNode->getAttribute ("name")->getRestrictedValue (); - int amount = resourceNode->getAttribute ("amount")->getIntValue (); - - try - { - startingResources[i].init (techTree->getResourceType (name), - amount); - } - catch (megaglest_runtime_error & ex) - { - if (validationMode == false) - { - throw; - } - else - { - SystemFlags::OutputDebug (SystemFlags::debugError, - "In [%s::%s Line: %d] Error [%s]\nFor FactionType: %s for StartResource: %s\n", - extractFileFromDirectoryPath - (__FILE__).c_str (), __FUNCTION__, - __LINE__, ex.what (), - this->name.c_str (), name.c_str ()); - } - } - - SDL_PumpEvents (); - } - - //read starting units - const XmlNode *startingUnitsNode = - factionNode->getChild ("starting-units"); - for (int i = 0; i < (int) startingUnitsNode->getChildCount (); ++i) - { - const XmlNode *unitNode = startingUnitsNode->getChild ("unit", i); - string name = - unitNode->getAttribute ("name")->getRestrictedValue (); - int amount = unitNode->getAttribute ("amount")->getIntValue (); - startingUnits. - push_back (PairPUnitTypeInt (getUnitType (name), amount)); - - SDL_PumpEvents (); - } - - //read music - const XmlNode *musicNode = factionNode->getChild ("music"); - bool value = musicNode->getAttribute ("value")->getBoolValue (); - if (value) - { - music = new StrSound (); - music->open (musicNode->getAttribute ("path")-> - getRestrictedValue (currentPath)); - loadedFileList[musicNode->getAttribute ("path")-> - getRestrictedValue (currentPath)]. - push_back (make_pair - (path, - musicNode->getAttribute ("path")-> - getRestrictedValue ())); - } - - if (factionNode->hasChild ("flat-particle-positions")) - { - const XmlNode *node = - factionNode->getChild ("flat-particle-positions"); - flatParticlePositions = - node->getAttribute ("value")->getBoolValue (); - } - - //healthbar - if (factionNode->hasChild ("healthbar")) - { - const XmlNode *healthbarNode = factionNode->getChild ("healthbar"); - if (healthbarNode->hasChild ("height")) - { - healthbarheight = - healthbarNode->getChild ("height")->getAttribute ("value")-> - getFloatValue (); - } - if (healthbarNode->hasChild ("thickness")) - { - healthbarthickness = - healthbarNode->getChild ("thickness")->getAttribute ("value")-> - getFloatValue (0.f, 1.f); - } - if (healthbarNode->hasChild ("visible")) - { - string healthbarVisibleString = - healthbarNode->getChild ("visible")->getAttribute ("value")-> - getValue (); - vector < string > v = split (healthbarVisibleString, "|"); - for (int i = 0; i < (int) v.size (); ++i) - { - string current = trim (v[i]); - if (current == "always") - { - healthbarVisible = healthbarVisible | hbvAlways; - } - else if (current == "selected") - { - healthbarVisible = healthbarVisible | hbvSelected; - } - else if (current == "ifNeeded") - { - healthbarVisible = healthbarVisible | hbvIfNeeded; - } - else if (current == "off") - { - healthbarVisible = healthbarVisible | hbvOff; - } - else - { - throw - megaglest_runtime_error - ("Unknown Healthbar Visible Option: " + current, true); - } - } - } - if (healthbarNode->hasChild ("borderTexture")) - { - healthbarBorderTextureEnabled = - healthbarNode->getChild ("borderTexture")-> - getAttribute ("enabled")->getBoolValue (); - if (healthbarBorderTextureEnabled - && healthbarNode->getChild ("borderTexture")-> - hasAttribute ("path")) - { - healthbarTexture = - Renderer::getInstance ().newTexture2D (rsGame); - if (healthbarTexture) - { - healthbarTexture->load (healthbarNode-> - getChild ("borderTexture")-> - getAttribute ("path")-> - getRestrictedValue (currentPath)); - } - loadedFileList[healthbarNode->getChild ("borderTexture")-> - getAttribute ("path")-> - getRestrictedValue (currentPath)]. - push_back (make_pair - (path, - healthbarNode->getChild ("borderTexture")-> - getAttribute ("path")->getRestrictedValue ())); - } - } - if (healthbarNode->hasChild ("backgroundTexture")) - { - healthbarBackgroundTextureEnabled = - healthbarNode->getChild ("backgroundTexture")-> - getAttribute ("enabled")->getBoolValue (); - if (healthbarBackgroundTextureEnabled - && healthbarNode->getChild ("backgroundTexture")-> - hasAttribute ("path")) - { - healthbarBackgroundTexture = - Renderer::getInstance ().newTexture2D (rsGame); - if (healthbarBackgroundTexture) - { - healthbarBackgroundTexture->load (healthbarNode-> - getChild - ("backgroundTexture")-> - getAttribute ("path")-> - getRestrictedValue - (currentPath)); - } - loadedFileList[healthbarNode->getChild ("backgroundTexture")-> - getAttribute ("path")-> - getRestrictedValue (currentPath)]. - push_back (make_pair - (path, - healthbarNode->getChild ("backgroundTexture")-> - getAttribute ("path")->getRestrictedValue ())); - } - } - if (healthbarNode->hasChild ("lineBorder")) - { - healthbarLineBorder = - healthbarNode->getChild ("lineBorder")-> - getAttribute ("enabled")->getBoolValue (); - } - - } - - //read ai behavior - if (factionNode->hasChild ("ai-behavior") == true) - { - const XmlNode *aiNode = factionNode->getChild ("ai-behavior"); - if (aiNode->hasAttribute ("min-static-resource-count") == true) - { - mapAIBehaviorStaticOverrideValues[aibsvcMinStaticResourceCount] = - aiNode->getAttribute ("min-static-resource-count")-> - getIntValue (); - } - - if (aiNode->hasChild ("static-values") == true) - { - const XmlNode *aiNodeUnits = aiNode->getChild ("static-values"); - for (int i = 0; i < (int) aiNodeUnits->getChildCount (); ++i) - { - const XmlNode *unitNode = aiNodeUnits->getChild ("static", i); - AIBehaviorStaticValueCategory type = aibsvcMaxBuildRadius; - if (unitNode->hasAttribute ("type") == true) - { - type = - static_cast < AIBehaviorStaticValueCategory > - (unitNode->getAttribute ("type")->getIntValue ()); - } - else - { - type = - EnumParser < - AIBehaviorStaticValueCategory >::getEnum (unitNode-> - getAttribute - ("type-name")-> - getValue ()); - //printf("Discovered overriden static value for AI, type = %d, value = %d\n",type,value); - } - - int value = unitNode->getAttribute ("value")->getIntValue (); - mapAIBehaviorStaticOverrideValues[type] = value; - //printf("Discovered overriden static value for AI, type = %d, value = %d\n",type,value); - } - } - - if (aiNode->hasChild ("worker-units") == true) - { - const XmlNode *aiNodeUnits = aiNode->getChild ("worker-units"); - for (int i = 0; i < (int) aiNodeUnits->getChildCount (); ++i) - { - const XmlNode *unitNode = aiNodeUnits->getChild ("unit", i); - string name = - unitNode->getAttribute ("name")->getRestrictedValue (); - int minimum = - unitNode->getAttribute ("minimum")->getIntValue (); - - mapAIBehaviorUnitCategories[aibcWorkerUnits]. - push_back (PairPUnitTypeInt (getUnitType (name), minimum)); - } - } - if (aiNode->hasChild ("warrior-units") == true) - { - const XmlNode *aiNodeUnits = aiNode->getChild ("warrior-units"); - for (int i = 0; i < (int) aiNodeUnits->getChildCount (); ++i) - { - const XmlNode *unitNode = aiNodeUnits->getChild ("unit", i); - string name = - unitNode->getAttribute ("name")->getRestrictedValue (); - int minimum = - unitNode->getAttribute ("minimum")->getIntValue (); - - mapAIBehaviorUnitCategories[aibcWarriorUnits]. - push_back (PairPUnitTypeInt (getUnitType (name), minimum)); - } - } - if (aiNode->hasChild ("resource-producer-units") == true) - { - const XmlNode *aiNodeUnits = - aiNode->getChild ("resource-producer-units"); - for (int i = 0; i < (int) aiNodeUnits->getChildCount (); ++i) - { - const XmlNode *unitNode = aiNodeUnits->getChild ("unit", i); - string name = - unitNode->getAttribute ("name")->getRestrictedValue (); - int minimum = - unitNode->getAttribute ("minimum")->getIntValue (); - - mapAIBehaviorUnitCategories[aibcResourceProducerUnits]. - push_back (PairPUnitTypeInt (getUnitType (name), minimum)); - } - } - if (aiNode->hasChild ("building-units") == true) - { - const XmlNode *aiNodeUnits = aiNode->getChild ("building-units"); - for (int i = 0; i < (int) aiNodeUnits->getChildCount (); ++i) - { - const XmlNode *unitNode = aiNodeUnits->getChild ("unit", i); - string name = - unitNode->getAttribute ("name")->getRestrictedValue (); - int minimum = - unitNode->getAttribute ("minimum")->getIntValue (); - - mapAIBehaviorUnitCategories[aibcBuildingUnits]. - push_back (PairPUnitTypeInt (getUnitType (name), minimum)); - } - } - - if (aiNode->hasChild ("upgrades") == true) - { - const XmlNode *aiNodeUpgrades = aiNode->getChild ("upgrades"); - for (int i = 0; i < (int) aiNodeUpgrades->getChildCount (); ++i) - { - const XmlNode *upgradeNode = - aiNodeUpgrades->getChild ("upgrade", i); - string name = - upgradeNode->getAttribute ("name")->getRestrictedValue (); - - vctAIBehaviorUpgrades.push_back (getUpgradeType (name)); - } - } - } - } - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__); - } - - int FactionType:: - getAIBehaviorStaticOverideValue (AIBehaviorStaticValueCategory type) - const - { - int result = INT_MAX; - std::map < AIBehaviorStaticValueCategory, - int >::const_iterator iterFind = - mapAIBehaviorStaticOverrideValues.find (type); - if (iterFind != mapAIBehaviorStaticOverrideValues.end ()) - { - result = iterFind->second; - } - return result; - } - - const std::vector < FactionType::PairPUnitTypeInt > - FactionType::getAIBehaviorUnits (AIBehaviorUnitCategory category) const - { - std::map < AIBehaviorUnitCategory, - std::vector < PairPUnitTypeInt > >::const_iterator iterFind = - mapAIBehaviorUnitCategories.find (category); - if (iterFind != mapAIBehaviorUnitCategories.end ()) - { - return iterFind->second; - } - return std::vector < FactionType::PairPUnitTypeInt > (); - } - - FactionType::~FactionType () - { - delete music; - music = NULL; - } - - std::vector < std::string > FactionType::validateFactionType () - { - std::vector < std::string > results; - - const uint32 MAX_BITRATE_WARNING = 200000; - StrSound *factionMusic = getMusic (); - if (factionMusic != NULL - && factionMusic->getInfo ()->getBitRate () > MAX_BITRATE_WARNING) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Faction [%s] has the music [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", - this->getName ().c_str (), - factionMusic->getFileName ().c_str (), - factionMusic->getInfo ()->getBitRate (), - MAX_BITRATE_WARNING); - results.push_back (szBuf); - } - - for (int i = 0; i < (int) unitTypes.size (); ++i) - { - UnitType & unitType = unitTypes[i]; - - for (int i = 0; - i < (int) unitType.getSelectionSounds ().getSounds ().size (); - ++i) - { - StaticSound *sound = unitType.getSelectionSounds ().getSounds ()[i]; - if (sound != NULL - && sound->getInfo ()->getBitRate () > MAX_BITRATE_WARNING) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the sound [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", - unitType.getName ().c_str (), this->getName ().c_str (), - sound->getFileName ().c_str (), - sound->getInfo ()->getBitRate (), MAX_BITRATE_WARNING); - results.push_back (szBuf); - } - } - for (int i = 0; - i < (int) unitType.getCommandSounds ().getSounds ().size (); ++i) - { - StaticSound *sound = unitType.getCommandSounds ().getSounds ()[i]; - if (sound != NULL - && sound->getInfo ()->getBitRate () > MAX_BITRATE_WARNING) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the sound [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", - unitType.getName ().c_str (), this->getName ().c_str (), - sound->getFileName ().c_str (), - sound->getInfo ()->getBitRate (), MAX_BITRATE_WARNING); - results.push_back (szBuf); - } - } - - int morphCommandCount = 0; - for (int j = 0; j < (int) unitType.getCommandTypeCount (); ++j) - { - const CommandType *cmdType = unitType.getCommandType (j); - if (cmdType != NULL) - { - // Check every unit's commands to validate that for every upgrade-requirements - // upgrade we have a unit that can do the upgrade in the faction. - for (int k = 0; k < cmdType->getUpgradeReqCount (); ++k) - { - const UpgradeType *upgradeType = cmdType->getUpgradeReq (k); - - if (upgradeType != NULL) - { - // Now lets find a unit that can produced-upgrade this upgrade - bool foundUpgraderUnit = false; - for (int l = 0; - l < (int) unitTypes.size () - && foundUpgraderUnit == false; ++l) - { - UnitType & unitType2 = unitTypes[l]; - for (int m = 0; - m < unitType2.getCommandTypeCount () - && foundUpgraderUnit == false; ++m) - { - const CommandType *cmdType2 = - unitType2.getCommandType (m); - if (cmdType2 != NULL - && dynamic_cast < - const UpgradeCommandType * >(cmdType2) != NULL) - { - const UpgradeCommandType *uct = - dynamic_cast < const UpgradeCommandType * >(cmdType2); - if (uct != NULL) - { - const UpgradeType *upgradeType2 = - uct->getProducedUpgrade (); - if (upgradeType2 != NULL - && upgradeType2->getName () == - upgradeType->getName ()) - { - foundUpgraderUnit = true; - break; - } - } - } - } - } - - if (foundUpgraderUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s]\nwhich has upgrade requirement [%s] but there are no units able to perform the upgrade!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - upgradeType->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - - // Ensure for each build type command that the build units - // exist in this faction - if (cmdType->getClass () == ccBuild) - { - const BuildCommandType *build = - dynamic_cast < const BuildCommandType * >(cmdType); - if (build == NULL) - { - throw megaglest_runtime_error ("build == NULL"); - } - for (int k = 0; k < build->getBuildingCount (); ++k) - { - const UnitType *buildUnit = build->getBuilding (k); - - // Now lets find the unit that we should be able to build - bool foundUnit = false; - for (int l = 0; - l < (int) unitTypes.size () && foundUnit == false; ++l) - { - UnitType & unitType2 = unitTypes[l]; - if (unitType2.getName () == buildUnit->getName ()) - { - foundUnit = true; - - // Now also validate the the unit to be built - // has a be_built_skill - if (buildUnit->hasSkillClass (scBeBuilt) == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can build the Unit [%s] but the Unit to be built\ndoes not have the skill class [be_built_skill] in this faction!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - buildUnit->getName ().c_str ()); - results.push_back (szBuf); - } - - break; - } - } - - if (foundUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can build the Unit [%s] but the Unit to be built does not exist in this faction!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - buildUnit->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - // Ensure for each repair type command that the repair units - // exist in this faction - if (cmdType->getClass () == ccRepair) - { - const RepairCommandType *repair = - dynamic_cast < const RepairCommandType * >(cmdType); - if (repair == NULL) - { - throw megaglest_runtime_error ("repair == NULL"); - } - for (int k = 0; k < repair->getRepairCount (); ++k) - { - const UnitType *repairUnit = repair->getRepair (k); - - // Now lets find the unit that we should be able to repair - bool foundUnit = false; - for (int l = 0; - l < (int) unitTypes.size () && foundUnit == false; ++l) - { - UnitType & unitType2 = unitTypes[l]; - if (unitType2.getName () == repairUnit->getName ()) - { - foundUnit = true; - break; - } - } - - if (foundUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can repair the Unit [%s] but the Unit to be repaired does not exist in this faction!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - repairUnit->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - // Ensure for each morph type command that the morph units - // exist in this faction - if (cmdType->getClass () == ccMorph) - { - const MorphCommandType *morph = - dynamic_cast < const MorphCommandType * >(cmdType); - if (morph != NULL) - { - morphCommandCount++; - const UnitType *morphUnit = morph->getMorphUnit (); - - // Now lets find the unit that we should be able to morph - // to - bool foundUnit = false; - for (int l = 0; - l < (int) unitTypes.size () && foundUnit == false; ++l) - { - UnitType & unitType2 = unitTypes[l]; - if (unitType2.getName () == morphUnit->getName ()) - { - foundUnit = true; - break; - } - } - - if (foundUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can morph into the Unit [%s] but the Unit to be morphed to does not exist in this faction!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - morphUnit->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - } - } - - const int maxMorphsAllowed = 6; - if (morphCommandCount > maxMorphsAllowed) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has more than %d morph commands which is too many to display in the UI!", - unitType.getName ().c_str (), this->getName ().c_str (), - maxMorphsAllowed); - results.push_back (szBuf); - } - - // Check every unit's unit requirements to validate that for every unit-requirements - // we have the units required in the faction. - for (int j = 0; j < unitType.getUnitReqCount (); ++j) - { - const UnitType *unitType2 = unitType.getUnitReq (j); - if (unitType2 != NULL) - { - // Now lets find the required unit - bool foundUnit = false; - for (int l = 0; l < (int) unitTypes.size () && foundUnit == false; - ++l) - { - UnitType & unitType3 = unitTypes[l]; - - if (unitType2->getName () == unitType3.getName ()) - { - foundUnit = true; - break; - } - } - - if (foundUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the required Unit [%s]\nbut the required unit does not exist in this faction!", - unitType.getName ().c_str (), - this->getName ().c_str (), - unitType2->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - - // Now check that at least 1 other unit can produce, build or morph this unit - bool foundUnit = false; - for (int l = 0; l < (int) unitTypes.size () && foundUnit == false; - ++l) - { - UnitType & unitType2 = unitTypes[l]; - - for (int j = 0; - j < unitType2.getCommandTypeCount () && foundUnit == false; - ++j) - { - const CommandType *cmdType = unitType2.getCommandType (j); - if (cmdType != NULL) - { - // Check if this is a produce command - if (cmdType->getClass () == ccProduce) - { - const ProduceCommandType *produce = - dynamic_cast < const ProduceCommandType * >(cmdType); - if (produce != NULL) - { - const UnitType *produceUnit = produce->getProducedUnit (); - - if (produceUnit != NULL && - unitType.getId () != unitType2.getId () && - unitType.getName () == produceUnit->getName ()) - { - foundUnit = true; - break; - } - } - } - // Check if this is a build command - if (cmdType->getClass () == ccBuild) - { - const BuildCommandType *build = - dynamic_cast < const BuildCommandType * >(cmdType); - if (build == NULL) - { - throw megaglest_runtime_error ("build == NULL"); - } - for (int k = 0; - k < build->getBuildingCount () && foundUnit == false; - ++k) - { - const UnitType *buildUnit = build->getBuilding (k); - - if (buildUnit != NULL && - unitType.getId () != unitType2.getId () && - unitType.getName () == buildUnit->getName ()) - { - foundUnit = true; - break; - } - } - if (foundUnit == true) - { - break; - } - } - // Check if this is a morph command - if (cmdType->getClass () == ccMorph) - { - const MorphCommandType *morph = - dynamic_cast < const MorphCommandType * >(cmdType); - if (morph == NULL) - { - throw megaglest_runtime_error ("morph == NULL"); - } - const UnitType *morphUnit = morph->getMorphUnit (); - - if (morphUnit != NULL && - unitType.getId () != unitType2.getId () && - unitType.getName () == morphUnit->getName ()) - { - foundUnit = true; - break; - } - } - - // Check if this is an attack command with spawned units on attack - if (cmdType->getClass () == ccAttack) - { - const AttackCommandType *act = - dynamic_cast < const AttackCommandType * >(cmdType); - if (act != NULL && act->getAttackSkillType () != NULL - && act->getAttackSkillType ()->getSpawnUnit () != "" - && act->getAttackSkillType ()->getSpawnUnitCount () > 0) - { - - if (unitType.getId () != unitType2.getId () && - unitType.getName () == - act->getAttackSkillType ()->getSpawnUnit ()) - { - foundUnit = true; - break; - } - } - } - } - } - } - - if (foundUnit == false) - { - //printf("Problem for unit [%s] unitTypes.size() = " MG_SIZE_T_SPECIFIER "\n",unitType.getName().c_str(),unitTypes.size()); - - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has no other units that can produce, build or morph into it in this faction!", - unitType.getName ().c_str (), this->getName ().c_str ()); - results.push_back (szBuf); - } - - // Ensure that all attack skill types have valid values - if (unitType.hasSkillClass (scAttack) == true) - { - for (int j = 0; j < unitType.getSkillTypeCount (); ++j) - { - const SkillType *st = unitType.getSkillType (j); - if (st != NULL - && dynamic_cast < const AttackSkillType * >(st) != NULL) - { - const AttackSkillType *ast = - dynamic_cast < const AttackSkillType * >(st); - if (ast != NULL && ast->getAttackVar () < 0) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the skill [%s] with an INVALID attack var value which is < 0 [%d]!", - unitType.getName ().c_str (), - this->getName ().c_str (), ast->getName ().c_str (), - ast->getAttackVar ()); - results.push_back (szBuf); - } - } - } - } - // end - - // Check if the unit has both be_built and harvest skills, this may cause issues - // with the AI - if (unitType.hasSkillClass (scBeBuilt) == true - && unitType.hasSkillClass (scHarvest) == true) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has both a bebuilt and harvest skill which will cause AI problems for CPU players!", - unitType.getName ().c_str (), this->getName ().c_str ()); - results.push_back (szBuf); - } - // end - - // Check if the unit has harvest skills but not move, meaning they cannot - // harvest the resource - if (unitType.hasSkillClass (scHarvest) == true - && unitType.hasSkillClass (scMove) == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has a harvest skill but no move skill so it cannot harvest!", - unitType.getName ().c_str (), this->getName ().c_str ()); - results.push_back (szBuf); - } - // end - - } - - return results; - } - - std::vector < std::string > - FactionType::validateFactionTypeResourceTypes (vector < ResourceType > - &resourceTypes) - { - std::vector < std::string > results; - - for (int i = 0; i < (int) unitTypes.size (); ++i) - { - UnitType & unitType = unitTypes[i]; - - // Check every unit's required resources to validate that for every resource-requirements - // we have a resource in the faction. - for (int j = 0; j < unitType.getCostCount (); ++j) - { - const Resource *r = unitType.getCost (j); - if (r != NULL && r->getType () != NULL) - { - bool foundResourceType = false; - // Now lets find a matching faction resource type for the unit - for (int k = 0; k < (int) resourceTypes.size (); ++k) - { - ResourceType & rt = resourceTypes[k]; - - if (r->getType ()->getName () == rt.getName ()) - { - foundResourceType = true; - break; - } - } - - if (foundResourceType == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the resource req [%s]\nbut there are no such resources in this tech!", - unitType.getName ().c_str (), - this->getName ().c_str (), - r->getType ()->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - - // Check every unit's stored resources to validate that for every resources-stored - // we have a resource in the faction. - for (int j = 0; j < unitType.getStoredResourceCount (); ++j) - { - const Resource *r = unitType.getStoredResource (j); - if (r != NULL && r->getType () != NULL) - { - bool foundResourceType = false; - // Now lets find a matching faction resource type for the unit - for (int k = 0; k < (int) resourceTypes.size (); ++k) - { - ResourceType & rt = resourceTypes[k]; - - if (r->getType ()->getName () == rt.getName ()) - { - foundResourceType = true; - break; - } - } - - if (foundResourceType == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the stored resource [%s]\nbut there are no such resources in this tech!", - unitType.getName ().c_str (), - this->getName ().c_str (), - r->getType ()->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - - for (int j = 0; j < unitType.getCommandTypeCount (); ++j) - { - const CommandType *cmdType = unitType.getCommandType (j); - if (cmdType != NULL) - { - // Ensure for each harvest type command that the resource - // exist in this faction - if (cmdType->getClass () == ccHarvest) - { - const HarvestCommandType *harvest = - dynamic_cast < const HarvestCommandType * >(cmdType); - if (harvest == NULL) - { - throw megaglest_runtime_error ("harvest == NULL"); - } - for (int k = 0; k < harvest->getHarvestedResourceCount (); ++k) - { - const ResourceType *harvestResource = - harvest->getHarvestedResource (k); - - bool foundResourceType = false; - // Now lets find a matching faction resource type for the unit - for (int k = 0; k < (int) resourceTypes.size (); ++k) - { - ResourceType & rt = resourceTypes[k]; - - if (harvestResource->getName () == rt.getName ()) - { - foundResourceType = true; - break; - } - } - - if (foundResourceType == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Unit [%s] in Faction [%s] has the command [%s] which can harvest the resource [%s]\nbut there are no such resources in this tech!", - unitType.getName ().c_str (), - this->getName ().c_str (), - cmdType->getName ().c_str (), - harvestResource->getName ().c_str ()); - results.push_back (szBuf); - } - } - } - } - } - } - - return results; - } - - std::vector < std::string > - FactionType::validateFactionTypeUpgradeTypes () - { - std::vector < std::string > results; - - // For each upgrade type make sure there is at least 1 unit that can produce - // the upgrade - for (int i = 0; i < (int) upgradeTypes.size (); ++i) - { - const UpgradeType & upgradeType = upgradeTypes[i]; - - // First find a unit with a command type to upgrade to this Upgrade type - bool foundUnit = false; - for (int j = 0; j < (int) unitTypes.size () && foundUnit == false; - ++j) - { - UnitType & unitType = unitTypes[j]; - for (int k = 0; - k < unitType.getCommandTypeCount () && foundUnit == false; ++k) - { - const CommandType *cmdType = unitType.getCommandType (k); - if (cmdType != NULL) - { - // Ensure for each build type command that the build units - // exist in this faction - if (cmdType->getClass () == ccUpgrade) - { - const UpgradeCommandType *upgrade = - dynamic_cast < const UpgradeCommandType * >(cmdType); - if (upgrade != NULL) - { - const UpgradeType *upgradeType2 = - upgrade->getProducedUpgrade (); - - if (upgradeType2 != NULL - && upgradeType.getName () == upgradeType2->getName ()) - { - foundUnit = true; - break; - } - } - } - } - } - } - - if (foundUnit == false) - { - char szBuf[8096] = ""; - snprintf (szBuf, 8096, - "The Upgrade Type [%s] in Faction [%s] has no Unit able to produce this upgrade in this faction!", - upgradeType.getName ().c_str (), - this->getName ().c_str ()); - results.push_back (szBuf); - } - } - - return results; - } - -// ==================== get ==================== - - const UnitType *FactionType::getUnitType (const string & name) const - { - for (int i = 0; i < (int) unitTypes.size (); i++) - { - if (unitTypes[i].getName (false) == name) - { - return &unitTypes[i]; - } - } - - printf ("In [%s::%s Line: %d] scanning [%s] size = " MG_SIZE_T_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, name.c_str (), unitTypes.size ()); - for (int i = 0; i < (int) unitTypes.size (); i++) - { - printf ("In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, name.c_str (), i, - unitTypes[i].getName (false).c_str ()); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] scanning [%s] size = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - name.c_str (), unitTypes.size ()); - for (int i = 0; i < (int) unitTypes.size (); i++) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - name.c_str (), i, - unitTypes[i].getName (false).c_str ()); - } - - throw megaglest_runtime_error ("Unit type not found: [" + name + - "] in faction type [" + this->name + "]", - true); - } - -//const UnitType *FactionType::getUnitTypeById(int id) const{ -// for(int i=0; i < (int)unitTypes.size();i++){ -// if(unitTypes[i].getId() == id) { -// return &unitTypes[i]; -// } -// } -// -// printf("In [%s::%s Line: %d] scanning [%d] size = " MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,id,unitTypes.size()); -// for(int i=0; i < (int)unitTypes.size();i++){ -// printf("In [%s::%s Line: %d] scanning [%s] idx = %d [%s][%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),i,unitTypes[i].getName(false).c_str(),unitTypes[i].getId()); -// } -// -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning [%s] size = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),unitTypes.size()); -// for(int i=0; i < (int)unitTypes.size();i++){ -// if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),i,unitTypes[i].getName(false).c_str()); -// } -// -// throw megaglest_runtime_error("Unit type not found: [" + intToStr(id) + "] in faction type [" + this->name + "]",true); -//} - - const UpgradeType *FactionType::getUpgradeType (const string & name) const - { - for (int i = 0; i < (int) upgradeTypes.size (); i++) - { - if (upgradeTypes[i].getName () == name) - { - return &upgradeTypes[i]; - } - } - - printf ("In [%s::%s Line: %d] scanning [%s] size = " MG_SIZE_T_SPECIFIER - "\n", extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, name.c_str (), unitTypes.size ()); - for (int i = 0; i < (int) upgradeTypes.size (); i++) - { - printf ("In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", - extractFileFromDirectoryPath (__FILE__).c_str (), - __FUNCTION__, __LINE__, name.c_str (), i, - upgradeTypes[i].getName ().c_str ()); - } - - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] scanning [%s] size = %d\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - name.c_str (), unitTypes.size ()); - for (int i = 0; i < (int) upgradeTypes.size (); i++) - { - if (SystemFlags::getSystemSettingType (SystemFlags::debugSystem). - enabled) - SystemFlags::OutputDebug (SystemFlags::debugSystem, - "In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", - extractFileFromDirectoryPath (__FILE__). - c_str (), __FUNCTION__, __LINE__, - name.c_str (), i, - upgradeTypes[i].getName ().c_str ()); - } - - throw megaglest_runtime_error ("Upgrade type not found: [" + name + - "] in faction type [" + this->name + "]", - true); - } - - int FactionType::getStartingResourceAmount (const ResourceType * - resourceType) const - { - for (int i = 0; i < (int) startingResources.size (); ++i) - { - if (startingResources[i].getType () == resourceType) - { - return startingResources[i].getAmount (); - } - } - return 0; - } - - void FactionType::deletePixels () - { - for (int i = 0; i < (int) unitTypes.size (); ++i) - { - UnitType & unitType = unitTypes[i]; - Texture2D *texture = unitType.getMeetingPointImage (); - if (texture != NULL) - { - texture->deletePixels (); - } - } - } - - bool FactionType::factionUsesResourceType (const ResourceType * rt) const - { - bool factionUsesResourceType = false; - if (rt != NULL) - { - for (unsigned int j = 0; - factionUsesResourceType == false - && j < (unsigned int) this->getUnitTypeCount (); ++j) - { - const UnitType *ut = this->getUnitType (j); - for (int k = 0; - factionUsesResourceType == false && k < ut->getCostCount (); - ++k) - { - const Resource *costResource = ut->getCost (k); - //printf("#1 factionUsesResourceType, unit [%s] resource [%s] cost [%s]\n",ut->getName().c_str(),rt->getName().c_str(),costResource->getType()->getName().c_str()); - - if (costResource != NULL && costResource->getType () != NULL && - costResource->getType ()->getName () == rt->getName ()) - { - factionUsesResourceType = true; - break; - } - } - if (factionUsesResourceType == false) - { - for (unsigned int k = 0; - factionUsesResourceType == false - && k < (unsigned int) ut->getCommandTypeCount (); ++k) - { - const CommandType *commandType = ut->getCommandType (k); - if (commandType != NULL - && commandType->getClass () == ccHarvest) - { - const HarvestCommandType *hct = - dynamic_cast < const HarvestCommandType * >(commandType); - if (hct != NULL && hct->getHarvestedResourceCount () > 0) - { - for (unsigned int l = 0; - factionUsesResourceType == false - && l < - (unsigned int) hct->getHarvestedResourceCount (); ++l) - { - //printf("#2 factionUsesResourceType, unit [%s] resource [%s] harvest [%s]\n",ut->getName().c_str(),rt->getName().c_str(),hct->getHarvestedResource(l)->getName().c_str()); - - if (hct->getHarvestedResource (l)->getName () == - rt->getName ()) - { - factionUsesResourceType = true; - break; - } - } - } - } - } - } - } - } - return factionUsesResourceType; - } - - std::string FactionType::toString ()const - { - std::string result = "Faction Name: " + name + "\n"; - - result += - "Unit Type List count = " + intToStr (this->getUnitTypeCount ()) + - "\n"; - for (int i = 0; i < (int) unitTypes.size (); i++) - { - result += unitTypes[i].toString () + "\n"; - } - - result += - "Upgrade Type List count = " + - intToStr (this->getUpgradeTypeCount ()) + "\n"; - for (int i = 0; i < (int) upgradeTypes.size (); i++) - { - result += - "index: " + intToStr (i) + " " + - upgradeTypes[i].getReqDesc (false) + "\n"; - } - - return result; - } - - string FactionType::getName (bool translatedValue) const - { - if (translatedValue == false) - return name; - - Lang & lang = Lang::getInstance (); - return lang.getTechTreeString ("FactionName_" + name, name.c_str ()); - } - - - } +namespace Glest { + namespace Game { + + // ====================================================== + // Class FactionType + // ====================================================== + + FactionType::FactionType() { + music = NULL; + personalityType = fpt_Normal; + isLinked = false; + healthbarheight = -100.0f; + healthbarthickness = 0.11f; + healthbarVisible = hbvUndefined; + healthbarBorderTextureEnabled = false; + healthbarBackgroundTextureEnabled = false; + healthbarLineBorder = true; + healthbarTexture = NULL; + healthbarBackgroundTexture = NULL; + flatParticlePositions = false; + } + + //load a faction, given a directory + void FactionType::load(const string & factionName, + const TechTree * techTree, Checksum * checksum, + Checksum * techtreeChecksum, std::map < string, + vector < pair < string, + string > > >&loadedFileList, bool validationMode) { + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + + string techTreePath = techTree->getPath(); + string techTreeName = techTree->getNameUntranslated(); + string currentPath = ""; + + //open xml file + string path = ""; + XmlTree xmlTree; + const XmlNode *factionNode; + + //printf("\n>>> factionname=%s\n",factionName.c_str()); + for (bool realFactionPathFound = false; realFactionPathFound == false;) { + currentPath = techTreePath + "factions/" + factionName; + endPathWithSlash(currentPath); + + name = lastDir(currentPath); + + // Add special Observer Faction + //Lang &lang= Lang::getInstance(); + if (name == formatString(GameConstants::OBSERVER_SLOTNAME)) { + personalityType = fpt_Observer; + } + + if (personalityType == fpt_Normal) { + string tmppath = currentPath + factionName + ".xml"; + std::map < string, string > mapExtraTagReplacementValues; + // mapExtraTagReplacementValues["$COMMONDATAPATH"] = techTreePath + "/commondata/"; + //printf("current $COMMONDATAPATH = %s\n",mapExtraTagReplacementValues["$COMMONDATAPATH"].c_str()); + XmlTree xmlTree; + xmlTree.load(tmppath, + Properties:: + getTagReplacementValues + (&mapExtraTagReplacementValues)); + + + const XmlNode *rootNode = xmlTree.getRootNode(); + + if (rootNode->getName() == "link") { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "Faction [%s] is a linked faction\n", + name.c_str()); + + isLinked = true; + const XmlNode *techTreeNode = rootNode->getChild("techtree"); + const string linkedTechTreeName = + techTreeNode->getAttribute("name")->getRestrictedValue(); + // const XmlNode *factionLinkNode= rootNode->getChild("faction"); + // string linkedFactionName=factionLinkNode->getAttribute("name")->getRestrictedValue(); + string linkedTechTreePath = + techTree->findPath(linkedTechTreeName); + techTreePath = linkedTechTreePath; + endPathWithSlash(techTreePath); + techTreeName = linkedTechTreeName; + + string linkedCurrentPath = + techTreePath + "factions/" + factionName; + endPathWithSlash(linkedCurrentPath); + string linkedTmppath = linkedCurrentPath + factionName + ".xml"; + + //printf("linkedTmppath [%s] linkedCurrentPath [%s]\n",linkedTmppath.c_str(),linkedCurrentPath.c_str()); + + loadedFileList[linkedTmppath]. + push_back(make_pair(linkedCurrentPath, linkedCurrentPath)); + loadedFileList[tmppath]. + push_back(make_pair(currentPath, currentPath)); + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "techTreePath [%s] techTreeName [%s]\n", + techTreePath.c_str(), + techTreeName.c_str()); + } else { + // stop looking for new path, no more links ... + //xmlTree.load(tmppath, Properties::getTagReplacementValues(&mapExtraTagReplacementValues)); + + loadedFileList[tmppath]. + push_back(make_pair(currentPath, currentPath)); + + realFactionPathFound = true; + //printf("techPath found! %s\n",tmppath.c_str()); + + path = tmppath; + } + } else { + break; + } + } + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + Lang::getInstance(). + getString("LogScreenGameLoadingFactionType", "", + true).c_str(), + formatString(this->getName()).c_str()); + Logger::getInstance().add(szBuf, true); + + if (personalityType == fpt_Normal) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "Loading faction [%s] currentPath [%s]\n", + path.c_str(), currentPath.c_str()); + + checksum->addFile(path); + techtreeChecksum->addFile(path); + + // a1) preload units + //string unitsPath= currentPath + "units/*."; + string unitsPath = currentPath + "units/"; + vector < string > unitFilenames; + //findAll(unitsPath, unitFilenames); + findDirs(unitsPath, unitFilenames, false, false); + + unitTypes.resize(unitFilenames.size()); + + for (int i = 0; i < (int) unitTypes.size(); ++i) { + string str = currentPath + "units/" + unitFilenames[i]; + unitTypes[i].preLoad(str); + + SDL_PumpEvents(); + } + + // a2) preload upgrades + //string upgradesPath= currentPath + "upgrades/*."; + string upgradesPath = currentPath + "upgrades/"; + vector < string > upgradeFilenames; + //findAll(upgradesPath, upgradeFilenames, false, false); + findDirs(upgradesPath, upgradeFilenames, false, false); + + upgradeTypes.resize(upgradeFilenames.size()); + for (int i = 0; i < (int) upgradeTypes.size(); ++i) { + string str = currentPath + "upgrades/" + upgradeFilenames[i]; + upgradeTypes[i].preLoad(str); + + SDL_PumpEvents(); + } + + // b1) load units + try { + Logger & logger = Logger::getInstance(); + int progressBaseValue = logger.getProgress(); + for (int i = 0; i < (int) unitTypes.size(); ++i) { + string str = currentPath + "units/" + unitTypes[i].getName(); + + try { + unitTypes[i].loaddd(i, str, techTree, techTreePath, this, + checksum, techtreeChecksum, loadedFileList, + validationMode); + logger.setProgress(progressBaseValue + + (int) ((((double) i + + 1.0) / + (double) unitTypes.size()) * + 100.0 / techTree->getTypeCount())); + SDL_PumpEvents(); + } catch (megaglest_runtime_error & ex) { + if (validationMode == false) { + throw; + } else { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, ex.what()); + } + } + } + } catch (megaglest_runtime_error & ex) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + ex.what()); + throw megaglest_runtime_error("Error loading units: " + + currentPath + "\nMessage: " + + ex.what(), !ex.wantStackTrace()); + } catch (const exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + e.what()); + throw megaglest_runtime_error("Error loading units: " + + currentPath + "\nMessage: " + + e.what()); + } + + // b2) load upgrades + try { + for (int i = 0; i < (int) upgradeTypes.size(); ++i) { + string str = + currentPath + "upgrades/" + upgradeTypes[i].getName(); + + try { + upgradeTypes[i].load(str, techTree, this, checksum, + techtreeChecksum, loadedFileList, + validationMode); + } catch (megaglest_runtime_error & ex) { + if (validationMode == false) { + throw; + } else { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, ex.what()); + } + } + + SDL_PumpEvents(); + } + } catch (const exception & e) { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + e.what()); + throw megaglest_runtime_error("Error loading upgrades: " + + currentPath + "\n" + e.what()); + } + + string tmppath = currentPath + factionName + ".xml"; + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "Loading faction xml [%s]\n", + tmppath.c_str()); + + std::map < string, string > mapExtraTagReplacementValues; + mapExtraTagReplacementValues["$COMMONDATAPATH"] = + techTreePath + "/commondata/"; + //printf("current $COMMONDATAPATH = %s\n",mapExtraTagReplacementValues["$COMMONDATAPATH"].c_str()); + xmlTree.load(tmppath, + Properties:: + getTagReplacementValues + (&mapExtraTagReplacementValues)); + + + factionNode = xmlTree.getRootNode(); + //read starting resources + //printf("factionNode->getName()=%s",factionNode->getName().c_str()); + const XmlNode *startingResourcesNode = + factionNode->getChild("starting-resources"); + + startingResources.resize(startingResourcesNode->getChildCount()); + for (int i = 0; i < (int) startingResources.size(); ++i) { + const XmlNode *resourceNode = + startingResourcesNode->getChild("resource", i); + string name = + resourceNode->getAttribute("name")->getRestrictedValue(); + int amount = resourceNode->getAttribute("amount")->getIntValue(); + + try { + startingResources[i].init(techTree->getResourceType(name), + amount); + } catch (megaglest_runtime_error & ex) { + if (validationMode == false) { + throw; + } else { + SystemFlags::OutputDebug(SystemFlags::debugError, + "In [%s::%s Line: %d] Error [%s]\nFor FactionType: %s for StartResource: %s\n", + extractFileFromDirectoryPath + (__FILE__).c_str(), __FUNCTION__, + __LINE__, ex.what(), + this->name.c_str(), name.c_str()); + } + } + + SDL_PumpEvents(); + } + + //read starting units + const XmlNode *startingUnitsNode = + factionNode->getChild("starting-units"); + for (int i = 0; i < (int) startingUnitsNode->getChildCount(); ++i) { + const XmlNode *unitNode = startingUnitsNode->getChild("unit", i); + string name = + unitNode->getAttribute("name")->getRestrictedValue(); + int amount = unitNode->getAttribute("amount")->getIntValue(); + startingUnits. + push_back(PairPUnitTypeInt(getUnitType(name), amount)); + + SDL_PumpEvents(); + } + + //read music + const XmlNode *musicNode = factionNode->getChild("music"); + bool value = musicNode->getAttribute("value")->getBoolValue(); + if (value) { + music = new StrSound(); + music->open(musicNode->getAttribute("path")-> + getRestrictedValue(currentPath)); + loadedFileList[musicNode->getAttribute("path")-> + getRestrictedValue(currentPath)]. + push_back(make_pair + (path, + musicNode->getAttribute("path")-> + getRestrictedValue())); + } + + if (factionNode->hasChild("flat-particle-positions")) { + const XmlNode *node = + factionNode->getChild("flat-particle-positions"); + flatParticlePositions = + node->getAttribute("value")->getBoolValue(); + } + + //healthbar + if (factionNode->hasChild("healthbar")) { + const XmlNode *healthbarNode = factionNode->getChild("healthbar"); + if (healthbarNode->hasChild("height")) { + healthbarheight = + healthbarNode->getChild("height")->getAttribute("value")-> + getFloatValue(); + } + if (healthbarNode->hasChild("thickness")) { + healthbarthickness = + healthbarNode->getChild("thickness")->getAttribute("value")-> + getFloatValue(0.f, 1.f); + } + if (healthbarNode->hasChild("visible")) { + string healthbarVisibleString = + healthbarNode->getChild("visible")->getAttribute("value")-> + getValue(); + vector < string > v = split(healthbarVisibleString, "|"); + for (int i = 0; i < (int) v.size(); ++i) { + string current = trim(v[i]); + if (current == "always") { + healthbarVisible = healthbarVisible | hbvAlways; + } else if (current == "selected") { + healthbarVisible = healthbarVisible | hbvSelected; + } else if (current == "ifNeeded") { + healthbarVisible = healthbarVisible | hbvIfNeeded; + } else if (current == "off") { + healthbarVisible = healthbarVisible | hbvOff; + } else { + throw + megaglest_runtime_error + ("Unknown Healthbar Visible Option: " + current, true); + } + } + } + if (healthbarNode->hasChild("borderTexture")) { + healthbarBorderTextureEnabled = + healthbarNode->getChild("borderTexture")-> + getAttribute("enabled")->getBoolValue(); + if (healthbarBorderTextureEnabled + && healthbarNode->getChild("borderTexture")-> + hasAttribute("path")) { + healthbarTexture = + Renderer::getInstance().newTexture2D(rsGame); + if (healthbarTexture) { + healthbarTexture->load(healthbarNode-> + getChild("borderTexture")-> + getAttribute("path")-> + getRestrictedValue(currentPath)); + } + loadedFileList[healthbarNode->getChild("borderTexture")-> + getAttribute("path")-> + getRestrictedValue(currentPath)]. + push_back(make_pair + (path, + healthbarNode->getChild("borderTexture")-> + getAttribute("path")->getRestrictedValue())); + } + } + if (healthbarNode->hasChild("backgroundTexture")) { + healthbarBackgroundTextureEnabled = + healthbarNode->getChild("backgroundTexture")-> + getAttribute("enabled")->getBoolValue(); + if (healthbarBackgroundTextureEnabled + && healthbarNode->getChild("backgroundTexture")-> + hasAttribute("path")) { + healthbarBackgroundTexture = + Renderer::getInstance().newTexture2D(rsGame); + if (healthbarBackgroundTexture) { + healthbarBackgroundTexture->load(healthbarNode-> + getChild + ("backgroundTexture")-> + getAttribute("path")-> + getRestrictedValue + (currentPath)); + } + loadedFileList[healthbarNode->getChild("backgroundTexture")-> + getAttribute("path")-> + getRestrictedValue(currentPath)]. + push_back(make_pair + (path, + healthbarNode->getChild("backgroundTexture")-> + getAttribute("path")->getRestrictedValue())); + } + } + if (healthbarNode->hasChild("lineBorder")) { + healthbarLineBorder = + healthbarNode->getChild("lineBorder")-> + getAttribute("enabled")->getBoolValue(); + } + + } + + if (factionNode->hasChild("scripts")) { + const XmlNode *scriptsNode = factionNode->getChild("scripts"); + + for (int i = 0; i < (int) scriptsNode->getChildCount(); ++i) { + const XmlNode *scriptNode = scriptsNode->getChild(i); + + scripts.push_back(Script(getFunctionName(scriptNode), scriptNode->getText())); + } + } + + //read ai behavior + if (factionNode->hasChild("ai-behavior") == true) { + const XmlNode *aiNode = factionNode->getChild("ai-behavior"); + if (aiNode->hasAttribute("min-static-resource-count") == true) { + mapAIBehaviorStaticOverrideValues[aibsvcMinStaticResourceCount] = + aiNode->getAttribute("min-static-resource-count")-> + getIntValue(); + } + + if (aiNode->hasChild("static-values") == true) { + const XmlNode *aiNodeUnits = aiNode->getChild("static-values"); + for (int i = 0; i < (int) aiNodeUnits->getChildCount(); ++i) { + const XmlNode *unitNode = aiNodeUnits->getChild("static", i); + AIBehaviorStaticValueCategory type = aibsvcMaxBuildRadius; + if (unitNode->hasAttribute("type") == true) { + type = + static_cast + (unitNode->getAttribute("type")->getIntValue()); + } else { + type = + EnumParser < + AIBehaviorStaticValueCategory >::getEnum(unitNode-> + getAttribute + ("type-name")-> + getValue()); + //printf("Discovered overriden static value for AI, type = %d, value = %d\n",type,value); + } + + int value = unitNode->getAttribute("value")->getIntValue(); + mapAIBehaviorStaticOverrideValues[type] = value; + //printf("Discovered overriden static value for AI, type = %d, value = %d\n",type,value); + } + } + + if (aiNode->hasChild("worker-units") == true) { + const XmlNode *aiNodeUnits = aiNode->getChild("worker-units"); + for (int i = 0; i < (int) aiNodeUnits->getChildCount(); ++i) { + const XmlNode *unitNode = aiNodeUnits->getChild("unit", i); + string name = + unitNode->getAttribute("name")->getRestrictedValue(); + int minimum = + unitNode->getAttribute("minimum")->getIntValue(); + + mapAIBehaviorUnitCategories[aibcWorkerUnits]. + push_back(PairPUnitTypeInt(getUnitType(name), minimum)); + } + } + if (aiNode->hasChild("warrior-units") == true) { + const XmlNode *aiNodeUnits = aiNode->getChild("warrior-units"); + for (int i = 0; i < (int) aiNodeUnits->getChildCount(); ++i) { + const XmlNode *unitNode = aiNodeUnits->getChild("unit", i); + string name = + unitNode->getAttribute("name")->getRestrictedValue(); + int minimum = + unitNode->getAttribute("minimum")->getIntValue(); + + mapAIBehaviorUnitCategories[aibcWarriorUnits]. + push_back(PairPUnitTypeInt(getUnitType(name), minimum)); + } + } + if (aiNode->hasChild("resource-producer-units") == true) { + const XmlNode *aiNodeUnits = + aiNode->getChild("resource-producer-units"); + for (int i = 0; i < (int) aiNodeUnits->getChildCount(); ++i) { + const XmlNode *unitNode = aiNodeUnits->getChild("unit", i); + string name = + unitNode->getAttribute("name")->getRestrictedValue(); + int minimum = + unitNode->getAttribute("minimum")->getIntValue(); + + mapAIBehaviorUnitCategories[aibcResourceProducerUnits]. + push_back(PairPUnitTypeInt(getUnitType(name), minimum)); + } + } + if (aiNode->hasChild("building-units") == true) { + const XmlNode *aiNodeUnits = aiNode->getChild("building-units"); + for (int i = 0; i < (int) aiNodeUnits->getChildCount(); ++i) { + const XmlNode *unitNode = aiNodeUnits->getChild("unit", i); + string name = + unitNode->getAttribute("name")->getRestrictedValue(); + int minimum = + unitNode->getAttribute("minimum")->getIntValue(); + + mapAIBehaviorUnitCategories[aibcBuildingUnits]. + push_back(PairPUnitTypeInt(getUnitType(name), minimum)); + } + } + + if (aiNode->hasChild("upgrades") == true) { + const XmlNode *aiNodeUpgrades = aiNode->getChild("upgrades"); + for (int i = 0; i < (int) aiNodeUpgrades->getChildCount(); ++i) { + const XmlNode *upgradeNode = + aiNodeUpgrades->getChild("upgrade", i); + string name = + upgradeNode->getAttribute("name")->getRestrictedValue(); + + vctAIBehaviorUpgrades.push_back(getUpgradeType(name)); + } + } + } + } + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__); + } + + string FactionType::getFunctionName(const XmlNode * scriptNode) { + string name = scriptNode->getName(); + + for (int i = 0; i < (int) scriptNode->getAttributeCount(); ++i) { + name += "_" + scriptNode->getAttribute(i)->getValue(); + } + return name; + } + + int FactionType:: + getAIBehaviorStaticOverideValue(AIBehaviorStaticValueCategory type) + const { + int result = INT_MAX; + std::map < AIBehaviorStaticValueCategory, + int >::const_iterator iterFind = + mapAIBehaviorStaticOverrideValues.find(type); + if (iterFind != mapAIBehaviorStaticOverrideValues.end()) { + result = iterFind->second; + } + return result; + } + + const std::vector < FactionType::PairPUnitTypeInt > + FactionType::getAIBehaviorUnits(AIBehaviorUnitCategory category) const { + std::map < AIBehaviorUnitCategory, + std::vector < PairPUnitTypeInt > >::const_iterator iterFind = + mapAIBehaviorUnitCategories.find(category); + if (iterFind != mapAIBehaviorUnitCategories.end()) { + return iterFind->second; + } + return std::vector < FactionType::PairPUnitTypeInt >(); + } + + FactionType::~FactionType() { + delete music; + music = NULL; + } + + std::vector < std::string > FactionType::validateFactionType() { + std::vector < std::string > results; + + const uint32 MAX_BITRATE_WARNING = 200000; + StrSound *factionMusic = getMusic(); + if (factionMusic != NULL + && factionMusic->getInfo()->getBitRate() > MAX_BITRATE_WARNING) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Faction [%s] has the music [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", + this->getName().c_str(), + factionMusic->getFileName().c_str(), + factionMusic->getInfo()->getBitRate(), + MAX_BITRATE_WARNING); + results.push_back(szBuf); + } + + for (int i = 0; i < (int) unitTypes.size(); ++i) { + UnitType & unitType = unitTypes[i]; + + for (int i = 0; + i < (int) unitType.getSelectionSounds().getSounds().size(); + ++i) { + StaticSound *sound = unitType.getSelectionSounds().getSounds()[i]; + if (sound != NULL + && sound->getInfo()->getBitRate() > MAX_BITRATE_WARNING) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the sound [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", + unitType.getName().c_str(), this->getName().c_str(), + sound->getFileName().c_str(), + sound->getInfo()->getBitRate(), MAX_BITRATE_WARNING); + results.push_back(szBuf); + } + } + for (int i = 0; + i < (int) unitType.getCommandSounds().getSounds().size(); ++i) { + StaticSound *sound = unitType.getCommandSounds().getSounds()[i]; + if (sound != NULL + && sound->getInfo()->getBitRate() > MAX_BITRATE_WARNING) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the sound [%s]\nwhich has a bitrate of [%u] which may cause some sound drivers to crash, please use a bitrate of %d or less!", + unitType.getName().c_str(), this->getName().c_str(), + sound->getFileName().c_str(), + sound->getInfo()->getBitRate(), MAX_BITRATE_WARNING); + results.push_back(szBuf); + } + } + + int morphCommandCount = 0; + for (int j = 0; j < (int) unitType.getCommandTypeCount(); ++j) { + const CommandType *cmdType = unitType.getCommandType(j); + if (cmdType != NULL) { + // Check every unit's commands to validate that for every upgrade-requirements + // upgrade we have a unit that can do the upgrade in the faction. + for (int k = 0; k < cmdType->getUpgradeReqCount(); ++k) { + const UpgradeType *upgradeType = cmdType->getUpgradeReq(k); + + if (upgradeType != NULL) { + // Now lets find a unit that can produced-upgrade this upgrade + bool foundUpgraderUnit = false; + for (int l = 0; + l < (int) unitTypes.size() + && foundUpgraderUnit == false; ++l) { + UnitType & unitType2 = unitTypes[l]; + for (int m = 0; + m < unitType2.getCommandTypeCount() + && foundUpgraderUnit == false; ++m) { + const CommandType *cmdType2 = + unitType2.getCommandType(m); + if (cmdType2 != NULL + && dynamic_cast < + const UpgradeCommandType *>(cmdType2) != NULL) { + const UpgradeCommandType *uct = + dynamic_cast (cmdType2); + if (uct != NULL) { + const UpgradeType *upgradeType2 = + uct->getProducedUpgrade(); + if (upgradeType2 != NULL + && upgradeType2->getName() == + upgradeType->getName()) { + foundUpgraderUnit = true; + break; + } + } + } + } + } + + if (foundUpgraderUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s]\nwhich has upgrade requirement [%s] but there are no units able to perform the upgrade!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + upgradeType->getName().c_str()); + results.push_back(szBuf); + } + } + } + + // Ensure for each build type command that the build units + // exist in this faction + if (cmdType->getClass() == ccBuild) { + const BuildCommandType *build = + dynamic_cast (cmdType); + if (build == NULL) { + throw megaglest_runtime_error("build == NULL"); + } + for (int k = 0; k < build->getBuildingCount(); ++k) { + const UnitType *buildUnit = build->getBuilding(k); + + // Now lets find the unit that we should be able to build + bool foundUnit = false; + for (int l = 0; + l < (int) unitTypes.size() && foundUnit == false; ++l) { + UnitType & unitType2 = unitTypes[l]; + if (unitType2.getName() == buildUnit->getName()) { + foundUnit = true; + + // Now also validate the the unit to be built + // has a be_built_skill + if (buildUnit->hasSkillClass(scBeBuilt) == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can build the Unit [%s] but the Unit to be built\ndoes not have the skill class [be_built_skill] in this faction!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + buildUnit->getName().c_str()); + results.push_back(szBuf); + } + + break; + } + } + + if (foundUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can build the Unit [%s] but the Unit to be built does not exist in this faction!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + buildUnit->getName().c_str()); + results.push_back(szBuf); + } + } + } + // Ensure for each repair type command that the repair units + // exist in this faction + if (cmdType->getClass() == ccRepair) { + const RepairCommandType *repair = + dynamic_cast (cmdType); + if (repair == NULL) { + throw megaglest_runtime_error("repair == NULL"); + } + for (int k = 0; k < repair->getRepairCount(); ++k) { + const UnitType *repairUnit = repair->getRepair(k); + + // Now lets find the unit that we should be able to repair + bool foundUnit = false; + for (int l = 0; + l < (int) unitTypes.size() && foundUnit == false; ++l) { + UnitType & unitType2 = unitTypes[l]; + if (unitType2.getName() == repairUnit->getName()) { + foundUnit = true; + break; + } + } + + if (foundUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can repair the Unit [%s] but the Unit to be repaired does not exist in this faction!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + repairUnit->getName().c_str()); + results.push_back(szBuf); + } + } + } + // Ensure for each morph type command that the morph units + // exist in this faction + if (cmdType->getClass() == ccMorph) { + const MorphCommandType *morph = + dynamic_cast (cmdType); + if (morph != NULL) { + morphCommandCount++; + const UnitType *morphUnit = morph->getMorphUnit(); + + // Now lets find the unit that we should be able to morph + // to + bool foundUnit = false; + for (int l = 0; + l < (int) unitTypes.size() && foundUnit == false; ++l) { + UnitType & unitType2 = unitTypes[l]; + if (unitType2.getName() == morphUnit->getName()) { + foundUnit = true; + break; + } + } + + if (foundUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s]\nwhich can morph into the Unit [%s] but the Unit to be morphed to does not exist in this faction!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + morphUnit->getName().c_str()); + results.push_back(szBuf); + } + } + } + } + } + + const int maxMorphsAllowed = 6; + if (morphCommandCount > maxMorphsAllowed) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has more than %d morph commands which is too many to display in the UI!", + unitType.getName().c_str(), this->getName().c_str(), + maxMorphsAllowed); + results.push_back(szBuf); + } + + // Check every unit's unit requirements to validate that for every unit-requirements + // we have the units required in the faction. + for (int j = 0; j < unitType.getUnitReqCount(); ++j) { + const UnitType *unitType2 = unitType.getUnitReq(j); + if (unitType2 != NULL) { + // Now lets find the required unit + bool foundUnit = false; + for (int l = 0; l < (int) unitTypes.size() && foundUnit == false; + ++l) { + UnitType & unitType3 = unitTypes[l]; + + if (unitType2->getName() == unitType3.getName()) { + foundUnit = true; + break; + } + } + + if (foundUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the required Unit [%s]\nbut the required unit does not exist in this faction!", + unitType.getName().c_str(), + this->getName().c_str(), + unitType2->getName().c_str()); + results.push_back(szBuf); + } + } + } + + // Now check that at least 1 other unit can produce, build or morph this unit + bool foundUnit = false; + for (int l = 0; l < (int) unitTypes.size() && foundUnit == false; + ++l) { + UnitType & unitType2 = unitTypes[l]; + + for (int j = 0; + j < unitType2.getCommandTypeCount() && foundUnit == false; + ++j) { + const CommandType *cmdType = unitType2.getCommandType(j); + if (cmdType != NULL) { + // Check if this is a produce command + if (cmdType->getClass() == ccProduce) { + const ProduceCommandType *produce = + dynamic_cast (cmdType); + if (produce != NULL) { + const UnitType *produceUnit = produce->getProducedUnit(); + + if (produceUnit != NULL && + unitType.getId() != unitType2.getId() && + unitType.getName() == produceUnit->getName()) { + foundUnit = true; + break; + } + } + } + // Check if this is a build command + if (cmdType->getClass() == ccBuild) { + const BuildCommandType *build = + dynamic_cast (cmdType); + if (build == NULL) { + throw megaglest_runtime_error("build == NULL"); + } + for (int k = 0; + k < build->getBuildingCount() && foundUnit == false; + ++k) { + const UnitType *buildUnit = build->getBuilding(k); + + if (buildUnit != NULL && + unitType.getId() != unitType2.getId() && + unitType.getName() == buildUnit->getName()) { + foundUnit = true; + break; + } + } + if (foundUnit == true) { + break; + } + } + // Check if this is a morph command + if (cmdType->getClass() == ccMorph) { + const MorphCommandType *morph = + dynamic_cast (cmdType); + if (morph == NULL) { + throw megaglest_runtime_error("morph == NULL"); + } + const UnitType *morphUnit = morph->getMorphUnit(); + + if (morphUnit != NULL && + unitType.getId() != unitType2.getId() && + unitType.getName() == morphUnit->getName()) { + foundUnit = true; + break; + } + } + + // Check if this is an attack command with spawned units on attack + if (cmdType->getClass() == ccAttack) { + const AttackCommandType *act = + dynamic_cast (cmdType); + if (act != NULL && act->getAttackSkillType() != NULL + && act->getAttackSkillType()->getSpawnUnit() != "" + && act->getAttackSkillType()->getSpawnUnitCount() > 0) { + + if (unitType.getId() != unitType2.getId() && + unitType.getName() == + act->getAttackSkillType()->getSpawnUnit()) { + foundUnit = true; + break; + } + } + } + } + } + } + + if (foundUnit == false) { + //printf("Problem for unit [%s] unitTypes.size() = " MG_SIZE_T_SPECIFIER "\n",unitType.getName().c_str(),unitTypes.size()); + + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has no other units that can produce, build or morph into it in this faction!", + unitType.getName().c_str(), this->getName().c_str()); + results.push_back(szBuf); + } + + // Ensure that all attack skill types have valid values + if (unitType.hasSkillClass(scAttack) == true) { + for (int j = 0; j < unitType.getSkillTypeCount(); ++j) { + const SkillType *st = unitType.getSkillType(j); + if (st != NULL + && dynamic_cast (st) != NULL) { + const AttackSkillType *ast = + dynamic_cast (st); + if (ast != NULL && ast->getAttackVar() < 0) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the skill [%s] with an INVALID attack var value which is < 0 [%d]!", + unitType.getName().c_str(), + this->getName().c_str(), ast->getName().c_str(), + ast->getAttackVar()); + results.push_back(szBuf); + } + } + } + } + // end + + // Check if the unit has both be_built and harvest skills, this may cause issues + // with the AI + if (unitType.hasSkillClass(scBeBuilt) == true + && unitType.hasSkillClass(scHarvest) == true) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has both a bebuilt and harvest skill which will cause AI problems for CPU players!", + unitType.getName().c_str(), this->getName().c_str()); + results.push_back(szBuf); + } + // end + + // Check if the unit has harvest skills but not move, meaning they cannot + // harvest the resource + if (unitType.hasSkillClass(scHarvest) == true + && unitType.hasSkillClass(scMove) == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has a harvest skill but no move skill so it cannot harvest!", + unitType.getName().c_str(), this->getName().c_str()); + results.push_back(szBuf); + } + // end + + } + + return results; + } + + std::vector < std::string > + FactionType::validateFactionTypeResourceTypes(vector < ResourceType > + &resourceTypes) { + std::vector < std::string > results; + + for (int i = 0; i < (int) unitTypes.size(); ++i) { + UnitType & unitType = unitTypes[i]; + + // Check every unit's required resources to validate that for every resource-requirements + // we have a resource in the faction. + for (int j = 0; j < unitType.getCostCount(); ++j) { + const Resource *r = unitType.getCost(j); + if (r != NULL && r->getType() != NULL) { + bool foundResourceType = false; + // Now lets find a matching faction resource type for the unit + for (int k = 0; k < (int) resourceTypes.size(); ++k) { + ResourceType & rt = resourceTypes[k]; + + if (r->getType()->getName() == rt.getName()) { + foundResourceType = true; + break; + } + } + + if (foundResourceType == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the resource req [%s]\nbut there are no such resources in this tech!", + unitType.getName().c_str(), + this->getName().c_str(), + r->getType()->getName().c_str()); + results.push_back(szBuf); + } + } + } + + // Check every unit's stored resources to validate that for every resources-stored + // we have a resource in the faction. + for (int j = 0; j < unitType.getStoredResourceCount(); ++j) { + const Resource *r = unitType.getStoredResource(j); + if (r != NULL && r->getType() != NULL) { + bool foundResourceType = false; + // Now lets find a matching faction resource type for the unit + for (int k = 0; k < (int) resourceTypes.size(); ++k) { + ResourceType & rt = resourceTypes[k]; + + if (r->getType()->getName() == rt.getName()) { + foundResourceType = true; + break; + } + } + + if (foundResourceType == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the stored resource [%s]\nbut there are no such resources in this tech!", + unitType.getName().c_str(), + this->getName().c_str(), + r->getType()->getName().c_str()); + results.push_back(szBuf); + } + } + } + + for (int j = 0; j < unitType.getCommandTypeCount(); ++j) { + const CommandType *cmdType = unitType.getCommandType(j); + if (cmdType != NULL) { + // Ensure for each harvest type command that the resource + // exist in this faction + if (cmdType->getClass() == ccHarvest) { + const HarvestCommandType *harvest = + dynamic_cast (cmdType); + if (harvest == NULL) { + throw megaglest_runtime_error("harvest == NULL"); + } + for (int k = 0; k < harvest->getHarvestedResourceCount(); ++k) { + const ResourceType *harvestResource = + harvest->getHarvestedResource(k); + + bool foundResourceType = false; + // Now lets find a matching faction resource type for the unit + for (int k = 0; k < (int) resourceTypes.size(); ++k) { + ResourceType & rt = resourceTypes[k]; + + if (harvestResource->getName() == rt.getName()) { + foundResourceType = true; + break; + } + } + + if (foundResourceType == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Unit [%s] in Faction [%s] has the command [%s] which can harvest the resource [%s]\nbut there are no such resources in this tech!", + unitType.getName().c_str(), + this->getName().c_str(), + cmdType->getName().c_str(), + harvestResource->getName().c_str()); + results.push_back(szBuf); + } + } + } + } + } + } + + return results; + } + + std::vector < std::string > + FactionType::validateFactionTypeUpgradeTypes() { + std::vector < std::string > results; + + // For each upgrade type make sure there is at least 1 unit that can produce + // the upgrade + for (int i = 0; i < (int) upgradeTypes.size(); ++i) { + const UpgradeType & upgradeType = upgradeTypes[i]; + + // First find a unit with a command type to upgrade to this Upgrade type + bool foundUnit = false; + for (int j = 0; j < (int) unitTypes.size() && foundUnit == false; + ++j) { + UnitType & unitType = unitTypes[j]; + for (int k = 0; + k < unitType.getCommandTypeCount() && foundUnit == false; ++k) { + const CommandType *cmdType = unitType.getCommandType(k); + if (cmdType != NULL) { + // Ensure for each build type command that the build units + // exist in this faction + if (cmdType->getClass() == ccUpgrade) { + const UpgradeCommandType *upgrade = + dynamic_cast (cmdType); + if (upgrade != NULL) { + const UpgradeType *upgradeType2 = + upgrade->getProducedUpgrade(); + + if (upgradeType2 != NULL + && upgradeType.getName() == upgradeType2->getName()) { + foundUnit = true; + break; + } + } + } + } + } + } + + if (foundUnit == false) { + char szBuf[8096] = ""; + snprintf(szBuf, 8096, + "The Upgrade Type [%s] in Faction [%s] has no Unit able to produce this upgrade in this faction!", + upgradeType.getName().c_str(), + this->getName().c_str()); + results.push_back(szBuf); + } + } + + return results; + } + + // ==================== get ==================== + + const UnitType *FactionType::getUnitType(const string & name) const { + for (int i = 0; i < (int) unitTypes.size(); i++) { + if (unitTypes[i].getName(false) == name) { + return &unitTypes[i]; + } + } + + printf("In [%s::%s Line: %d] scanning [%s] size = " MG_SIZE_T_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, name.c_str(), unitTypes.size()); + for (int i = 0; i < (int) unitTypes.size(); i++) { + printf("In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, name.c_str(), i, + unitTypes[i].getName(false).c_str()); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] scanning [%s] size = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + name.c_str(), unitTypes.size()); + for (int i = 0; i < (int) unitTypes.size(); i++) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + name.c_str(), i, + unitTypes[i].getName(false).c_str()); + } + + throw megaglest_runtime_error("Unit type not found: [" + name + + "] in faction type [" + this->name + "]", + true); + } + + //const UnitType *FactionType::getUnitTypeById(int id) const{ + // for(int i=0; i < (int)unitTypes.size();i++){ + // if(unitTypes[i].getId() == id) { + // return &unitTypes[i]; + // } + // } + // + // printf("In [%s::%s Line: %d] scanning [%d] size = " MG_SIZE_T_SPECIFIER "\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,id,unitTypes.size()); + // for(int i=0; i < (int)unitTypes.size();i++){ + // printf("In [%s::%s Line: %d] scanning [%s] idx = %d [%s][%d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),i,unitTypes[i].getName(false).c_str(),unitTypes[i].getId()); + // } + // + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning [%s] size = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),unitTypes.size()); + // for(int i=0; i < (int)unitTypes.size();i++){ + // if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,name.c_str(),i,unitTypes[i].getName(false).c_str()); + // } + // + // throw megaglest_runtime_error("Unit type not found: [" + intToStr(id) + "] in faction type [" + this->name + "]",true); + //} + + const UpgradeType *FactionType::getUpgradeType(const string & name) const { + for (int i = 0; i < (int) upgradeTypes.size(); i++) { + if (upgradeTypes[i].getName() == name) { + return &upgradeTypes[i]; + } + } + + printf("In [%s::%s Line: %d] scanning [%s] size = " MG_SIZE_T_SPECIFIER + "\n", extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, name.c_str(), unitTypes.size()); + for (int i = 0; i < (int) upgradeTypes.size(); i++) { + printf("In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", + extractFileFromDirectoryPath(__FILE__).c_str(), + __FUNCTION__, __LINE__, name.c_str(), i, + upgradeTypes[i].getName().c_str()); + } + + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] scanning [%s] size = %d\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + name.c_str(), unitTypes.size()); + for (int i = 0; i < (int) upgradeTypes.size(); i++) { + if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem). + enabled) + SystemFlags::OutputDebug(SystemFlags::debugSystem, + "In [%s::%s Line: %d] scanning [%s] idx = %d [%s]\n", + extractFileFromDirectoryPath(__FILE__). + c_str(), __FUNCTION__, __LINE__, + name.c_str(), i, + upgradeTypes[i].getName().c_str()); + } + + throw megaglest_runtime_error("Upgrade type not found: [" + name + + "] in faction type [" + this->name + "]", + true); + } + + int FactionType::getStartingResourceAmount(const ResourceType * + resourceType) const { + for (int i = 0; i < (int) startingResources.size(); ++i) { + if (startingResources[i].getType() == resourceType) { + return startingResources[i].getAmount(); + } + } + return 0; + } + + void FactionType::deletePixels() { + for (int i = 0; i < (int) unitTypes.size(); ++i) { + UnitType & unitType = unitTypes[i]; + Texture2D *texture = unitType.getMeetingPointImage(); + if (texture != NULL) { + texture->deletePixels(); + } + } + } + + bool FactionType::factionUsesResourceType(const ResourceType * rt) const { + bool factionUsesResourceType = false; + if (rt != NULL) { + for (unsigned int j = 0; + factionUsesResourceType == false + && j < (unsigned int) this->getUnitTypeCount(); ++j) { + const UnitType *ut = this->getUnitType(j); + for (int k = 0; + factionUsesResourceType == false && k < ut->getCostCount(); + ++k) { + const Resource *costResource = ut->getCost(k); + //printf("#1 factionUsesResourceType, unit [%s] resource [%s] cost [%s]\n",ut->getName().c_str(),rt->getName().c_str(),costResource->getType()->getName().c_str()); + + if (costResource != NULL && costResource->getType() != NULL && + costResource->getType()->getName() == rt->getName()) { + factionUsesResourceType = true; + break; + } + } + if (factionUsesResourceType == false) { + for (unsigned int k = 0; + factionUsesResourceType == false + && k < (unsigned int) ut->getCommandTypeCount(); ++k) { + const CommandType *commandType = ut->getCommandType(k); + if (commandType != NULL + && commandType->getClass() == ccHarvest) { + const HarvestCommandType *hct = + dynamic_cast (commandType); + if (hct != NULL && hct->getHarvestedResourceCount() > 0) { + for (unsigned int l = 0; + factionUsesResourceType == false + && l < + (unsigned int) hct->getHarvestedResourceCount(); ++l) { + //printf("#2 factionUsesResourceType, unit [%s] resource [%s] harvest [%s]\n",ut->getName().c_str(),rt->getName().c_str(),hct->getHarvestedResource(l)->getName().c_str()); + + if (hct->getHarvestedResource(l)->getName() == + rt->getName()) { + factionUsesResourceType = true; + break; + } + } + } + } + } + } + } + } + return factionUsesResourceType; + } + + std::string FactionType::toString()const { + std::string result = "Faction Name: " + name + "\n"; + + result += + "Unit Type List count = " + intToStr(this->getUnitTypeCount()) + + "\n"; + for (int i = 0; i < (int) unitTypes.size(); i++) { + result += unitTypes[i].toString() + "\n"; + } + + result += + "Upgrade Type List count = " + + intToStr(this->getUpgradeTypeCount()) + "\n"; + for (int i = 0; i < (int) upgradeTypes.size(); i++) { + result += + "index: " + intToStr(i) + " " + + upgradeTypes[i].getReqDesc(false) + "\n"; + } + + return result; + } + + string FactionType::getName(bool translatedValue) const { + if (translatedValue == false) + return name; + + Lang & lang = Lang::getInstance(); + return lang.getTechTreeString("FactionName_" + name, name.c_str()); + } + + + } } //end namespace diff --git a/source/glest_game/types/faction_type.h b/source/glest_game/types/faction_type.h index 8696a9de2..cf809e737 100644 --- a/source/glest_game/types/faction_type.h +++ b/source/glest_game/types/faction_type.h @@ -24,225 +24,208 @@ # include # include "util.h" # include "leak_dumper.h" +# include "scenario.h" using Shared::Sound::StrSound; -namespace Glest -{ - namespace Game - { -// ===================================================== -// class FactionType -// -/// Each of the possible factions the user can select -// ===================================================== +namespace Glest { + namespace Game { + // ===================================================== + // class FactionType + // + /// Each of the possible factions the user can select + // ===================================================== - enum AIBehaviorUnitCategory - { - aibcWorkerUnits, - aibcWarriorUnits, - aibcResourceProducerUnits, - aibcBuildingUnits - }; + enum AIBehaviorUnitCategory { + aibcWorkerUnits, + aibcWarriorUnits, + aibcResourceProducerUnits, + aibcBuildingUnits + }; - enum AIBehaviorStaticValueCategory - { - aibsvcMaxBuildRadius, - aibsvcMinMinWarriors, - aibsvcMinMinWarriorsExpandCpuEasy, - aibsvcMinMinWarriorsExpandCpuZeta, - aibsvcMinMinWarriorsExpandCpuUltra, - aibsvcMinMinWarriorsExpandCpuNormal, - aibsvcMaxMinWarriors, - aibsvcMaxExpansions, - aibsvcVillageRadius, - aibsvcMinStaticResourceCount, - aibsvcScoutResourceRange, - aibsvcMinWorkerAttackersHarvesting, - aibsvcMinBuildSpacing - }; - template <> - inline EnumParser < AIBehaviorStaticValueCategory >::EnumParser () - { - enumMap["MaxBuildRadius"] = aibsvcMaxBuildRadius; - enumMap["MinMinWarriors"] = aibsvcMinMinWarriors; - enumMap["MinMinWarriorsExpandCpuEasy"] = - aibsvcMinMinWarriorsExpandCpuEasy; - enumMap["MinMinWarriorsExpandCpuMega"] = - aibsvcMinMinWarriorsExpandCpuZeta; - enumMap["MinMinWarriorsExpandCpuZeta"] = - aibsvcMinMinWarriorsExpandCpuZeta; - enumMap["MinMinWarriorsExpandCpuUltra"] = - aibsvcMinMinWarriorsExpandCpuUltra; - enumMap["MinMinWarriorsExpandCpuNormal"] = - aibsvcMinMinWarriorsExpandCpuNormal; - enumMap["MaxMinWarriors"] = aibsvcMaxMinWarriors; - enumMap["MaxExpansions"] = aibsvcMaxExpansions; - enumMap["VillageRadius"] = aibsvcVillageRadius; - enumMap["MinStaticResourceCount"] = aibsvcMinStaticResourceCount; - enumMap["ScoutResourceRange"] = aibsvcScoutResourceRange; - enumMap["MinWorkerAttackersHarvesting"] = - aibsvcMinWorkerAttackersHarvesting; - enumMap["MinBuildSpacing"] = aibsvcMinBuildSpacing; - } + enum AIBehaviorStaticValueCategory { + aibsvcMaxBuildRadius, + aibsvcMinMinWarriors, + aibsvcMinMinWarriorsExpandCpuEasy, + aibsvcMinMinWarriorsExpandCpuZeta, + aibsvcMinMinWarriorsExpandCpuUltra, + aibsvcMinMinWarriorsExpandCpuNormal, + aibsvcMaxMinWarriors, + aibsvcMaxExpansions, + aibsvcVillageRadius, + aibsvcMinStaticResourceCount, + aibsvcScoutResourceRange, + aibsvcMinWorkerAttackersHarvesting, + aibsvcMinBuildSpacing + }; + template <> + inline EnumParser < AIBehaviorStaticValueCategory >::EnumParser() { + enumMap["MaxBuildRadius"] = aibsvcMaxBuildRadius; + enumMap["MinMinWarriors"] = aibsvcMinMinWarriors; + enumMap["MinMinWarriorsExpandCpuEasy"] = + aibsvcMinMinWarriorsExpandCpuEasy; + enumMap["MinMinWarriorsExpandCpuMega"] = + aibsvcMinMinWarriorsExpandCpuZeta; + enumMap["MinMinWarriorsExpandCpuZeta"] = + aibsvcMinMinWarriorsExpandCpuZeta; + enumMap["MinMinWarriorsExpandCpuUltra"] = + aibsvcMinMinWarriorsExpandCpuUltra; + enumMap["MinMinWarriorsExpandCpuNormal"] = + aibsvcMinMinWarriorsExpandCpuNormal; + enumMap["MaxMinWarriors"] = aibsvcMaxMinWarriors; + enumMap["MaxExpansions"] = aibsvcMaxExpansions; + enumMap["VillageRadius"] = aibsvcVillageRadius; + enumMap["MinStaticResourceCount"] = aibsvcMinStaticResourceCount; + enumMap["ScoutResourceRange"] = aibsvcScoutResourceRange; + enumMap["MinWorkerAttackersHarvesting"] = + aibsvcMinWorkerAttackersHarvesting; + enumMap["MinBuildSpacing"] = aibsvcMinBuildSpacing; + } - class FactionType - { - public: - typedef pair < const UnitType *, int >PairPUnitTypeInt; - typedef vector < UnitType > UnitTypes; - typedef vector < UpgradeType > UpgradeTypes; - typedef vector < PairPUnitTypeInt > StartingUnits; - typedef vector < Resource > Resources; + class FactionType { + public: + typedef pair < const UnitType *, int >PairPUnitTypeInt; + typedef vector < UnitType > UnitTypes; + typedef vector < UpgradeType > UpgradeTypes; + typedef vector < PairPUnitTypeInt > StartingUnits; + typedef vector < Resource > Resources; - private: - string name; - UnitTypes unitTypes; - UpgradeTypes upgradeTypes; - StartingUnits startingUnits; - Resources startingResources; - StrSound *music; - FactionPersonalityType personalityType; + private: + string name; + UnitTypes unitTypes; + UpgradeTypes upgradeTypes; + StartingUnits startingUnits; + Resources startingResources; + StrSound *music; + FactionPersonalityType personalityType; - std::map < AIBehaviorUnitCategory, - std::vector < PairPUnitTypeInt > >mapAIBehaviorUnitCategories; - std::vector < const UpgradeType *>vctAIBehaviorUpgrades; - std::map < AIBehaviorStaticValueCategory, - int >mapAIBehaviorStaticOverrideValues; + std::map < AIBehaviorUnitCategory, + std::vector < PairPUnitTypeInt > >mapAIBehaviorUnitCategories; + std::vector < const UpgradeType *>vctAIBehaviorUpgrades; + std::map < AIBehaviorStaticValueCategory, + int >mapAIBehaviorStaticOverrideValues; + std::vector