_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

fov.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 2020-2021 by Alyokhin
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
31 
32 #ifndef __FOV_H__
33 #define __FOV_H__
34 
35 #include <functional>
36 #include <queue>
37 #include <set>
38 #include "vec2i.h"
39 #include "map.h"
40 #include "tileset.h"
41 #include "settings.h"
42 
44 
46 {
47 public:
48  void Clean()
49  {
50  MarkedTilesCache.clear();
51  }
52 
54  void Refresh(const CPlayer &player, const CUnit &unit, const Vec2i &pos, const uint16_t width,
55  const uint16_t height, const uint16_t range, MapMarkerFunc *marker);
56 
57  bool SetType(const FieldOfViewTypes fov_type);
58  FieldOfViewTypes GetType() const;
59 
61  void SetOpaqueFields(const uint16_t flags);
62  uint16_t GetOpaqueFields() const;
65 
66 protected:
67 private:
69  struct SColumnPiece {
70  SColumnPiece(int16_t xValue, Vec2i top, Vec2i bottom) : col(xValue), TopVector(top), BottomVector(bottom) {}
71  int16_t col;
72  Vec2i TopVector;
73  Vec2i BottomVector;
74  };
75 
77  void ProceedSimpleRadial(const CPlayer &player, const Vec2i &pos, const int16_t w, const int16_t h,
78  int16_t range, MapMarkerFunc *marker) const;
80  void ProceedShadowCasting(const Vec2i &spectatorPos, const uint16_t width, const uint16_t height, const uint16_t range);
83  void ProceedRaysCast(const uint8_t octant, const Vec2i &origin, const uint16_t width, const uint16_t range);
85  void RefreshOctant(const uint8_t octant, const Vec2i &origin, const uint16_t range);
87  void CalcFoVForColumnPiece(const int16_t col, Vec2i &topVector, Vec2i &bottomVector,
88  const uint16_t range, std::queue<SColumnPiece> &wrkQueue);
90  int16_t CalcRow_ByVector(const bool isTop, const int16_t x, const Vec2i &vector) const;
91 
93  bool SetCurrentTile(const int16_t col, const int16_t row);
95  bool IsTileOpaque() const;
97  void MarkTile();
98 
100  void PrepareShadowCaster(const CPlayer &player, const CUnit &unit, const Vec2i &pos, MapMarkerFunc *marker);
101  void ResetShadowCaster();
102  void PrepareCache(const Vec2i pos, const uint16_t width, const uint16_t height, const uint16_t range);
103 
105  void SetEnvironment(const uint8_t octant, const Vec2i &origin);
106  void ResetEnvironment();
108  void ProjectCurrentTile(const int16_t col, const int16_t row);
109 
110 private:
111  struct FieldOfViewSettings
112  {
113  uint16_t OpaqueFields {MapFieldOpaque};
114  } Settings;
115 
116  Vec2i currTilePos {0, 0};
117  uint8_t currOctant {0};
118  Vec2i Origin {0, 0};
119  uint16_t OpaqueFields {0};
120 
121  const CPlayer *Player {nullptr};
122  const CUnit *Unit {nullptr};
123  MapMarkerFunc *map_setFoV {nullptr};
124 
125  std::vector<uint8_t> MarkedTilesCache;
126 };
129 
130 /*----------------------------------------------------------------------------
131 -- Variables
132 ----------------------------------------------------------------------------*/
133 
135 
136 /*----------------------------------------------------------------------------
137 -- Functions
138 ----------------------------------------------------------------------------*/
139 
140 inline bool CFieldOfView::SetCurrentTile(const int16_t col, const int16_t row)
141 {
142  ProjectCurrentTile(col, row);
143  if (Map.Info.IsPointOnMap(currTilePos.x, currTilePos.y)) {
144  return true;
145  } else {
146  currTilePos = {0, 0};
147  return false;
148  }
149 }
150 
151 inline bool CFieldOfView::IsTileOpaque() const
152 {
154  return (Map.Field(currTilePos.x, currTilePos.y)->Flags & OpaqueFields);
155 }
156 
157 inline void CFieldOfView::MarkTile()
158 {
159  const size_t index = Map.getIndex(currTilePos.x, currTilePos.y);
160  if (!MarkedTilesCache[index]) {
161  map_setFoV(*Player, index);
162  MarkedTilesCache[index] = 1;
163  }
164 }
165 
166 inline void CFieldOfView::ProjectCurrentTile(const int16_t col, const int16_t row)
167 {
168  switch (currOctant) {
169  case 1: currTilePos.x = row; currTilePos.y = col; break;
170  case 2: currTilePos.x = -row; currTilePos.y = col; break;
171  case 3: currTilePos.x = -col; currTilePos.y = row; break;
172  case 4: currTilePos.x = -col; currTilePos.y = -row; break;
173  case 5: currTilePos.x = -row; currTilePos.y = -col; break;
174  case 6: currTilePos.x = row; currTilePos.y = -col; break;
175  case 7: currTilePos.x = col; currTilePos.y = -row; break;
176  default: currTilePos.x = col; currTilePos.y = row;
177  }
178  currTilePos += Origin;
179 }
181 
182 #endif // !__FOV_H__
CPlayer
Diplomacy states for CommandDiplomacy.
Definition: player.h:83
CFieldOfView::SetOpaqueFields
void SetOpaqueFields(const uint16_t flags)
Set opaque map field flags (which fields will be opaque)
Definition: fov.cpp:87
FieldOfView
CFieldOfView FieldOfView
Definition: map_fog.cpp:64
settings.h
vec2i.h
CMapField::Flags
unsigned int Flags
graphic tile number
Definition: tile.h:248
Vec2T::y
T y
Definition: vec2i.h:43
MapMarkerFunc
void MapMarkerFunc(const CPlayer &player, const unsigned int index)
Function to (un)mark the vision table.
Definition: map.h:303
MapFieldOpaque
#define MapFieldOpaque
Definition: tileset.h:48
Vec2T
Definition: vec2i.h:36
FieldOfViewTypes
ENUM_CLASS FieldOfViewTypes
Definition: settings.h:216
tileset.h
CMap::Field
CMapField * Field(unsigned int index) const
Definition: map.h:171
CFieldOfView::SetType
bool SetType(const FieldOfViewTypes fov_type)
Definition: fov.cpp:56
CFieldOfView::Refresh
void Refresh(const CPlayer &player, const CUnit &unit, const Vec2i &pos, const uint16_t width, const uint16_t height, const uint16_t range, MapMarkerFunc *marker)
Refresh field of view.
Definition: fov.cpp:130
map.h
CFieldOfView::GetType
FieldOfViewTypes GetType() const
Definition: fov.cpp:77
update-images.w
w
Definition: update-images.py:53
Settings
Definition: settings.h:230
CFieldOfView::ResetAdditionalOpaqueFields
void ResetAdditionalOpaqueFields()
Reset opaque flags to default (MapFieldOpaque)
Definition: fov.cpp:114
CMap::getIndex
unsigned int getIndex(int x, int y) const
Definition: map.h:162
CFieldOfView::Clean
void Clean()
Definition: fov.h:48
Origin
Definition: sound.h:146
CMap::Info
CMapInfo Info
Definition: map.h:276
CMapInfo::IsPointOnMap
bool IsPointOnMap(int x, int y) const
Definition: map.h:125
CFieldOfView
Select algorithm for field of view.
Definition: fov.h:45
CFieldOfView::GetOpaqueFields
uint16_t GetOpaqueFields() const
Definition: fov.cpp:105
Vec2T::x
T x
Definition: vec2i.h:42
update-images.h
h
Definition: update-images.py:53
Map
CMap Map
Definition: map.cpp:55
CUnit
The big unit structure.
Definition: unit.h:135
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.