Skyscraper 2.0
lock.cpp
Go to the documentation of this file.
1/*
2 Scalable Building Simulator - Lock and DoorLock Classes
3 The Skyscraper Project - Version 2.0
4 Copyright (C)2004-2024 Ryan Thoryk
5 https://www.skyscrapersim.net
6 https://sourceforge.net/projects/skyscraper/
7 Contact - ryan@skyscrapersim.net
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22*/
23
24#include "globals.h"
25#include "sbs.h"
26#include "lock.h"
27
28namespace SBS {
29
31{
32 Locked = false;
33 KeyID = 0;
34 this->Parent = parent;
35}
36
37void Lock::SetLocked(bool value, int keyid)
38{
39 //set locked state
40 Locked = value;
41 KeyID = keyid;
42}
43
44bool Lock::ToggleLock(bool force)
45{
46 //toggle lock state
47 //if force is true, bypass key check
48
49 //quit if user doesn't have key, if force is false
50 if (KeyID != 0)
51 {
52 if (Parent->GetRoot()->CheckKey(KeyID) == false && force == false)
53 return Parent->ReportError("Need key " + ToString(KeyID) + " to lock/unlock");
54 }
55
56 Locked = !Locked;
57
58 if (Locked == true)
59 Parent->Report("Locked");
60 else
61 Parent->Report("Unlocked");
62
63 return true;
64}
65
67{
68 return Locked;
69}
70
72{
73 return KeyID;
74}
75
77{
78 //direction is false for left/right, true for front/back
79
80 Direction = false;
81 Locked = false;
82 KeyID = 0;
83 this->Parent = parent;
84}
85
86void DoorLock::SetDirection(bool direction)
87{
88 Direction = direction;
89}
90
91void DoorLock::SetLocked(int side, int keyid)
92{
93 //lock table:
94 //0 = unlocked
95 //1 = negative (left/front) side locked
96 //2 = positive (right/back) side locked
97 //3 = both sides locked
98
99 if (side < 0 || side > 3)
100 return;
101
102 Locked = side;
103 KeyID = keyid;
104}
105
106bool DoorLock::ToggleLock(const Vector3 &position, bool force)
107{
108 //toggle lock state of the related door side
109 //if force is true, bypass key check
110
111 bool replocked = false;
112
113 //quit if user doesn't have key, if force is false
114 if (KeyID != 0)
115 {
116 if (Parent->GetRoot()->CheckKey(KeyID) == false && force == false)
117 return Parent->ReportError("Need key " + ToString(KeyID) + " to lock/unlock");
118 }
119
120 if (GetSide(position) == false)
121 {
122 if (Locked == 0)
123 {
124 Locked = 1;
125 replocked = true;
126 }
127 else if (Locked == 1)
128 {
129 Locked = 0;
130 replocked = false;
131 }
132 else if (Locked == 2)
133 {
134 Locked = 3;
135 replocked = true;
136 }
137 }
138 else
139 {
140 if (Locked == 0)
141 {
142 Locked = 2;
143 replocked = true;
144 }
145 else if (Locked == 1)
146 {
147 Locked = 3;
148 replocked = true;
149 }
150 else if (Locked == 2)
151 {
152 Locked = 1;
153 replocked = false;
154 }
155 }
156
157 if (replocked == true)
158 Parent->Report("Locked");
159 else
160 Parent->Report("Unlocked");
161
162 return true;
163}
164
165bool DoorLock::GetSide(const Vector3 &position)
166{
167 //return which side of the door the position is (false for negative/left/front, true for positive/right/back)
168
169 if (Direction == false && position.x > Parent->GetPosition().x)
170 return true;
171
172 if (Direction == true && position.z > Parent->GetPosition().z)
173 return true;
174
175 return false;
176}
177
178bool DoorLock::IsLocked(const Vector3 &position)
179{
180 //returns if the door's side (in relation to the given position) is locked or not
181
182 if (Locked == 0)
183 return false;
184
185 if (Locked == 3)
186 return true;
187
188 bool side = GetSide(position);
189
190 if ((Locked == 1 && side == false) || (Locked == 2 && side == true))
191 return true;
192 return false;
193}
194
196{
197 return KeyID;
198}
199
200}
void SetLocked(int side, int keyid)
Definition lock.cpp:91
Object * Parent
Definition lock.h:61
bool GetSide(const Vector3 &position)
Definition lock.cpp:165
int KeyID
Definition lock.h:60
bool ToggleLock(const Vector3 &position, bool force=false)
Definition lock.cpp:106
int Locked
Definition lock.h:59
int GetKeyID()
Definition lock.cpp:195
void SetDirection(bool direction)
Definition lock.cpp:86
bool Direction
Definition lock.h:58
bool IsLocked(const Vector3 &position)
Definition lock.cpp:178
DoorLock(Object *parent)
Definition lock.cpp:76
Object * Parent
Definition lock.h:42
Lock(Object *parent)
Definition lock.cpp:30
int KeyID
Definition lock.h:41
bool Locked
Definition lock.h:40
bool IsLocked()
Definition lock.cpp:66
int GetKeyID()
Definition lock.cpp:71
void SetLocked(bool value, int keyid)
Definition lock.cpp:37
bool ToggleLock(bool force=false)
Definition lock.cpp:44
SBS * GetRoot()
Definition object.cpp:48
virtual bool ReportError(const std::string &message)
Definition object.cpp:84
virtual void Report(const std::string &message)
Definition object.cpp:78
virtual Vector3 GetPosition(bool relative=false)
Definition object.cpp:321
bool CheckKey(int keyid)
Definition sbs.cpp:3910
Ogre::Vector3 Vector3
Definition globals.h:58
std::string ToString(int number)
Definition globals.cpp:279