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.
28 MetaRoom::MetaRoom(int _x
, int _y
, int _width
, int _height
, const std::string
&back
, shared_ptr
<creaturesImage
> spr
, bool wrap
) {
29 xloc
= _x
; yloc
= _y
; wid
= _width
; hei
= _height
; wraps
= wrap
;
31 // if we were provided with a background, add it
34 addBackground(back
, spr
);
41 void MetaRoom::addBackground(std::string back
, shared_ptr
<creaturesImage
> spr
) {
42 shared_ptr
<creaturesImage
> backsprite
;
43 unsigned int totalwidth
, totalheight
;
45 caos_assert(!back
.empty());
46 // TODO: cadv adds backgrounds which have already been added as the default, look into this,
47 // should we preserve the default once extra backgrounds have been added and change this to
49 if (backgrounds
.find(back
) != backgrounds
.end()) return;
52 // we weren't passed a sprite, so we need to load one
53 backsprite
= world
.gallery
.getImage(back
, true);
54 blkImage
*background
= dynamic_cast<blkImage
*>(backsprite
.get());
55 if (!background
&& engine
.bmprenderer
) {
56 totalwidth
= backsprite
->width(0);
57 totalheight
= backsprite
->height(0);
59 caos_assert(background
);
61 totalwidth
= background
->totalwidth
;
62 totalheight
= background
->totalheight
;
65 // we were provided with a sprite, so use it
71 // store the background
72 backgrounds
[back
] = backsprite
;
74 // set the first background
75 firstback
= backsprite
;
77 fullhei
= totalheight
;
79 // make sure other backgrounds are the same size
80 if (world
.gametype
== "sm") return; // TODO: seamonkeys fails the background size checks :/
81 assert(totalwidth
== fullwid
);
82 assert(totalheight
== fullhei
);
86 std::vector
<std::string
> MetaRoom::backgroundList() {
87 // construct a temporary vector from our std::map
89 std::vector
<std::string
> b
;
90 for (std::map
<std::string
, shared_ptr
<creaturesImage
> >::iterator i
= backgrounds
.begin(); i
!= backgrounds
.end(); i
++)
91 b
.push_back(i
->first
);
95 shared_ptr
<creaturesImage
> MetaRoom::getBackground(std::string back
) {
96 // return the first background by default
101 // if this background name isn't found, return null
102 if (backgrounds
.find(back
) != backgrounds
.end()) return shared_ptr
<creaturesImage
>();
104 // otherwise, return the relevant background
105 return backgrounds
[back
];
108 MetaRoom::~MetaRoom() {
109 // we hold the only strong reference to our contained rooms, so they'll be auto-deleted
112 shared_ptr
<Room
> MetaRoom::nextFloorFromPoint(float x
, float y
) {
113 shared_ptr
<Room
> closest_up
, closest_down
;
114 float dist_down
= -1, dist_up
= -1;
115 for (std::vector
<shared_ptr
<Room
> >::iterator r
= rooms
.begin(); r
!= rooms
.end(); r
++) {
116 if (!(*r
)->bot
.containsX(x
)) continue;
117 float dist
= (*r
)->bot
.pointAtX(x
).y
- y
; // down is positive
118 float absdist
= fabs(dist
);
119 if (dist
>= 0 && (absdist
< dist_down
|| dist_down
< 0)) {
122 } else if (dist
< 0 && (absdist
< dist_up
|| dist_up
< 0)) {
127 if (closest_down
) return closest_down
;
128 if (closest_up
) return closest_up
;
129 return shared_ptr
<Room
>();
132 unsigned int MetaRoom::addRoom(shared_ptr
<Room
> r
) {
133 // add to both our local list and the global list
135 world
.map
.rooms
.push_back(r
);
137 // set the id and return
138 r
->id
= world
.map
.room_base
++;
142 shared_ptr
<Room
> MetaRoom::roomAt(float _x
, float _y
) {
144 if (_x
> (int)xloc
+ (int)wid
) _x
-= wid
;
145 else if (_x
< (int)xloc
) _x
+= wid
;
148 for (std::vector
<shared_ptr
<Room
> >::iterator i
= rooms
.begin(); i
!= rooms
.end(); i
++) {
149 shared_ptr
<Room
> r
= *i
;
150 if (r
->containsPoint(_x
, _y
)) return r
;
153 return shared_ptr
<Room
>();
156 std::vector
<shared_ptr
<Room
> > MetaRoom::roomsAt(float _x
, float _y
) {
158 if (_x
> (int)xloc
+ (int)wid
) _x
-= wid
;
159 else if (_x
< (int)xloc
) _x
+= wid
;
162 std::vector
<shared_ptr
<Room
> > ourlist
;
164 for (std::vector
<shared_ptr
<Room
> >::iterator i
= rooms
.begin(); i
!= rooms
.end(); i
++) {
165 shared_ptr
<Room
> r
= *i
;
166 if (r
->containsPoint(_x
, _y
)) ourlist
.push_back(r
);