5 * Created by Alyssa Milburn on Tue May 25 2004.
6 * Copyright (c) 2004 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
27 MetaRoom::MetaRoom(int _x
, int _y
, int _width
, int _height
, const std::string
&back
, shared_ptr
<creaturesImage
> spr
, bool wrap
) {
28 xloc
= _x
; yloc
= _y
; wid
= _width
; hei
= _height
; wraps
= wrap
;
30 // if we were provided with a background, add it
33 addBackground(back
, spr
);
40 void MetaRoom::addBackground(std::string back
, shared_ptr
<creaturesImage
> spr
) {
41 shared_ptr
<creaturesImage
> backsprite
;
42 unsigned int totalwidth
, totalheight
;
44 caos_assert(!back
.empty());
45 // TODO: cadv adds backgrounds which have already been added as the default, look into this,
46 // should we preserve the default once extra backgrounds have been added and change this to
48 if (backgrounds
.find(back
) != backgrounds
.end()) return;
51 // we weren't passed a sprite, so we need to load one
52 backsprite
= world
.gallery
.getImage(back
+ ".blk");
53 blkImage
*background
= dynamic_cast<blkImage
*>(backsprite
.get());
54 caos_assert(background
);
56 totalwidth
= background
->totalwidth
;
57 totalheight
= background
->totalheight
;
59 // we were provided with a sprite, so use it
65 // store the background
66 backgrounds
[back
] = backsprite
;
68 // set the first background
69 firstback
= backsprite
;
71 fullhei
= totalheight
;
73 // make sure other backgrounds are the same size
74 assert(totalwidth
== fullwid
);
75 assert(totalheight
== fullhei
);
79 std::vector
<std::string
> MetaRoom::backgroundList() {
80 // construct a temporary vector from our std::map
82 std::vector
<std::string
> b
;
83 for (std::map
<std::string
, shared_ptr
<creaturesImage
> >::iterator i
= backgrounds
.begin(); i
!= backgrounds
.end(); i
++)
84 b
.push_back(i
->first
);
88 shared_ptr
<creaturesImage
> MetaRoom::getBackground(std::string back
) {
89 // return the first background by default
94 // if this background name isn't found, return null
95 if (backgrounds
.find(back
) != backgrounds
.end()) return shared_ptr
<creaturesImage
>();
97 // otherwise, return the relevant background
98 return backgrounds
[back
];
101 MetaRoom::~MetaRoom() {
102 // we hold the only strong reference to our contained rooms, so they'll be auto-deleted
105 shared_ptr
<Room
> MetaRoom::nextFloorFromPoint(float x
, float y
) {
106 shared_ptr
<Room
> closest_up
, closest_down
;
107 float dist_down
= -1, dist_up
= -1;
108 for (std::vector
<shared_ptr
<Room
> >::iterator r
= rooms
.begin(); r
!= rooms
.end(); r
++) {
109 if (!(*r
)->bot
.containsX(x
)) continue;
110 float dist
= (*r
)->bot
.pointAtX(x
).y
- y
; // down is positive
111 float absdist
= fabs(dist
);
112 if (dist
>= 0 && (absdist
< dist_down
|| dist_down
< 0)) {
115 } else if (dist
< 0 && (absdist
< dist_up
|| dist_up
< 0)) {
120 if (closest_down
) return closest_down
;
121 if (closest_up
) return closest_up
;
122 return shared_ptr
<Room
>();
125 unsigned int MetaRoom::addRoom(shared_ptr
<Room
> r
) {
126 // add to both our local list and the global list
128 world
.map
.rooms
.push_back(r
);
130 // set the id and return
131 r
->id
= world
.map
.room_base
++;
135 shared_ptr
<Room
> MetaRoom::roomAt(float _x
, float _y
) {
137 if (_x
> (int)xloc
+ (int)wid
) _x
-= wid
;
138 else if (_x
< (int)xloc
) _x
+= wid
;
141 for (std::vector
<shared_ptr
<Room
> >::iterator i
= rooms
.begin(); i
!= rooms
.end(); i
++) {
142 shared_ptr
<Room
> r
= *i
;
143 if (r
->containsPoint(_x
, _y
)) return r
;
146 return shared_ptr
<Room
>();
149 std::vector
<shared_ptr
<Room
> > MetaRoom::roomsAt(float _x
, float _y
) {
151 if (_x
> (int)xloc
+ (int)wid
) _x
-= wid
;
152 else if (_x
< (int)xloc
) _x
+= wid
;
155 std::vector
<shared_ptr
<Room
> > ourlist
;
157 for (std::vector
<shared_ptr
<Room
> >::iterator i
= rooms
.begin(); i
!= rooms
.end(); i
++) {
158 shared_ptr
<Room
> r
= *i
;
159 if (r
->containsPoint(_x
, _y
)) ourlist
.push_back(r
);