OpenClonk
C4ObjectVisibility.cpp
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Logic for C4Object: Light, audibility, visibility */
19 
20 #include "C4Include.h"
22 #include "object/C4Object.h"
23 
24 #include "game/C4Application.h"
25 #include "landscape/fow/C4FoW.h"
26 #include "player/C4PlayerList.h"
27 
28 
30 {
31  if (!lightRange && !lightFadeoutRange) return true;
32 
33  UpdateLight();
34  return true;
35 }
36 
37 bool C4Object::SetLightRange(int32_t iToRange, int32_t iToFadeoutRange)
38 {
39  // set new range
40  lightRange = iToRange;
41  lightFadeoutRange = iToFadeoutRange;
42  // resort into player's FoW-repeller-list
43  UpdateLight();
44  // success
45  return true;
46 }
47 
48 bool C4Object::SetLightColor(uint32_t iValue)
49 {
50  // set new color value
51  lightColor = iValue;
52  // resort into player's FoW-repeller-list
53  UpdateLight();
54  // success
55  return true;
56 }
57 
59 {
60  if (Landscape.HasFoW()) Landscape.GetFoW()->Add(this);
61 }
62 
63 void C4Object::SetAudibilityAt(C4TargetFacet &cgo, int32_t iX, int32_t iY, int32_t player)
64 {
65  // target pos (parallax)
66  float offX, offY, newzoom;
67  GetDrawPosition(cgo, iX, iY, cgo.Zoom, offX, offY, newzoom);
68  int32_t audible_at_pos = Clamp(100 - 100 * Distance(cgo.X + cgo.Wdt / 2, cgo.Y + cgo.Hgt / 2, offX, offY) / 700, 0, 100);
69  if (audible_at_pos > Audible)
70  {
71  Audible = audible_at_pos;
72  AudiblePan = Clamp<int>(200 * (offX - cgo.X - (cgo.Wdt / 2)) / cgo.Wdt, -100, 100);
74  }
75 }
76 
77 bool C4Object::IsVisible(int32_t iForPlr, bool fAsOverlay) const
78 {
79  bool fDraw;
80  C4Value vis;
81  if (!GetProperty(P_Visibility, &vis))
82  return true;
83 
84  int32_t Visibility;
85  C4ValueArray *parameters = vis.getArray();
86  if (parameters && parameters->GetSize())
87  {
88  Visibility = parameters->GetItem(0).getInt();
89  }
90  else
91  {
92  Visibility = vis.getInt();
93  }
94  // check layer
95  if (Layer && Layer != this && !fAsOverlay)
96  {
97  fDraw = Layer->IsVisible(iForPlr, false);
98  if (Layer->GetPropertyInt(P_Visibility) & VIS_LayerToggle) fDraw = !fDraw;
99  if (!fDraw) return false;
100  }
101  // no flags set?
102  if (!Visibility) return true;
103  // check overlay
104  if (Visibility & VIS_OverlayOnly)
105  {
106  if (!fAsOverlay) return false;
107  if (Visibility == VIS_OverlayOnly) return true;
108  }
109  // editor visibility
110  if (::Application.isEditor)
111  {
112  if (Visibility & VIS_Editor) return true;
113  }
114  // check visibility
115  fDraw=false;
116  if (Visibility & VIS_Owner) fDraw = fDraw || (iForPlr==Owner);
117  if (iForPlr!=NO_OWNER)
118  {
119  // check all
120  if (Visibility & VIS_Allies) fDraw = fDraw || (iForPlr!=Owner && !Hostile(iForPlr, Owner));
121  if (Visibility & VIS_Enemies) fDraw = fDraw || (iForPlr!=Owner && Hostile(iForPlr, Owner));
122  if (parameters)
123  {
124  if (Visibility & VIS_Select) fDraw = fDraw || parameters->GetItem(1+iForPlr).getBool();
125  }
126  }
127  else fDraw = fDraw || (Visibility & VIS_God);
128  return fDraw;
129 }
const int NO_OWNER
Definition: C4Constants.h:137
C4Application Application
Definition: C4Globals.cpp:44
C4Landscape Landscape
#define VIS_God
Definition: C4Object.h:68
#define VIS_Owner
Definition: C4Object.h:64
#define VIS_Select
Definition: C4Object.h:67
#define VIS_Enemies
Definition: C4Object.h:66
#define VIS_OverlayOnly
Definition: C4Object.h:70
#define VIS_LayerToggle
Definition: C4Object.h:69
#define VIS_Editor
Definition: C4Object.h:71
#define VIS_Allies
Definition: C4Object.h:65
int32_t Hostile(int32_t plr1, int32_t plr2)
@ P_Visibility
int32_t Distance(int32_t iX1, int32_t iY1, int32_t iX2, int32_t iY2)
Definition: Standard.cpp:25
T Clamp(T bval, T lbound, T rbound)
Definition: Standard.h:44
float Hgt
Definition: C4Facet.h:118
float Wdt
Definition: C4Facet.h:118
float Y
Definition: C4Facet.h:118
float X
Definition: C4Facet.h:118
void Add(C4Object *pObj)
Definition: C4FoW.cpp:165
bool HasFoW() const
class C4FoW * GetFoW()
bool GetDrawPosition(const C4TargetFacet &cgo, float &resultx, float &resulty, float &resultzoom) const
int32_t lightRange
Definition: C4Object.h:120
bool AssignLightRange()
int32_t Audible
Definition: C4Object.h:119
int32_t lightFadeoutRange
Definition: C4Object.h:121
void SetAudibilityAt(C4TargetFacet &cgo, int32_t iX, int32_t iY, int32_t player)
int32_t Owner
Definition: C4Object.h:108
bool IsVisible(int32_t iForPlr, bool fAsOverlay) const
int32_t AudiblePan
Definition: C4Object.h:119
uint32_t lightColor
Definition: C4Object.h:122
bool SetLightRange(int32_t iToRange, int32_t iToFadeoutRange)
bool SetLightColor(uint32_t iValue)
C4ObjectPtr Layer
Definition: C4Object.h:134
int32_t AudiblePlayer
Definition: C4Object.h:119
int32_t GetPropertyInt(C4PropertyName k, int32_t default_val=0) const
Definition: C4PropList.cpp:855
bool GetProperty(C4PropertyName k, C4Value *pResult) const
Definition: C4PropList.h:105
float Zoom
Definition: C4Facet.h:165
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t GetSize() const
Definition: C4ValueArray.h:36
C4ValueArray * getArray() const
Definition: C4Value.h:118
int32_t getInt() const
Definition: C4Value.h:112
bool getBool() const
Definition: C4Value.h:113