creaturesImage work: add mutableCopy and tint methods to the base class (and change...
[openc2e.git] / MetaRoom.cpp
blob92ad199c077c0ec896cdbd2afc10d5bbd3dba764
1 /*
2 * MetaRoom.cpp
3 * openc2e
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.
20 #include "MetaRoom.h"
21 #include "Room.h"
22 #include "World.h"
23 #include "blkImage.h"
24 #include <assert.h>
25 #include "Backend.h"
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
31 if (!back.empty()) {
32 if (spr) {
33 addBackground(back, spr);
34 } else {
35 addBackground(back);
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
47 // a caos_assert?
48 if (backgrounds.find(back) != backgrounds.end()) return;
50 if (!spr) {
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;
58 } else {
59 // we were provided with a sprite, so use it
60 backsprite = spr;
61 totalwidth = wid;
62 totalheight = hei;
65 // store the background
66 backgrounds[back] = backsprite;
67 if (!firstback) {
68 // set the first background
69 firstback = backsprite;
70 fullwid = totalwidth;
71 fullhei = totalheight;
72 } else {
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);
85 return b;
88 shared_ptr<creaturesImage> MetaRoom::getBackground(std::string back) {
89 // return the first background by default
90 if (back.empty()) {
91 return firstback;
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)) {
113 dist_down = absdist;
114 closest_down = *r;
115 } else if (dist < 0 && (absdist < dist_up || dist_up < 0)) {
116 dist_up = absdist;
117 closest_up = *r;
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
127 rooms.push_back(r);
128 world.map.rooms.push_back(r);
130 // set the id and return
131 r->id = world.map.room_base++;
132 return r->id;
135 shared_ptr<Room> MetaRoom::roomAt(float _x, float _y) {
136 if (wraps) {
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) {
150 if (wraps) {
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);
162 return ourlist;
165 /* vim: set noet: */