1 /*
2 This file is part of "Avanor, the Land of Mystery" roguelike game
3 Home page: http://www.avanor.com/
4 Copyright (C) 2000-2003 Vadim Gaidukevich
5 
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20 
21 #include "shop.h"
22 #include "itemf.h"
23 #include "skeep_ai.h"
24 #include "xarchive.h"
25 
26 REGISTER_CLASS(XShop);
27 
XShop(XRect * _area,ITEM_MASK _im,XLocation * _loc,SHOP_DOOR sd)28 XShop::XShop(XRect * _area, ITEM_MASK _im, XLocation * _loc, SHOP_DOOR sd)
29 	:XAnyPlace(_area, _loc)
30 {
31 	shop_mask = _im;
32 
33 	int dx = 0;
34 	int dy = 0;
35 
36 	switch (sd)
37 	{
38 		case SHOP_DOOR_DOWN :
39 			dx = (area.left + area.right) / 2;
40 			dy = area.bottom - 1;
41 			break;
42 
43 		case SHOP_DOOR_UP :
44 			dx = (area.left + area.right) / 2;
45 			dy = area.top;
46 			break;
47 
48 		case SHOP_DOOR_LEFT:
49 			dx = area.left;
50 			dy = (area.top + area.bottom) / 2;
51 			break;
52 
53 		case SHOP_DOOR_RIGHT:
54 			dx = area.right - 1;
55 			dy = (area.top + area.bottom) / 2;
56 			break;
57 
58 		default: assert(0);
59 
60 	}
61 
62 	location->map->CreateRoom(area.left, area.top, area.Width(), area.Hight(),
63 		dx, dy, M_STONEFLOOR, M_STONEWALL);
64 
65 	for (int i = area.left + 1; i < area.right - 1; i++)
66 		for (int j = area.top + 1; j < area.bottom - 1; j++)
67 		{
68 			XItem * item = ICREATEA(shop_mask);
69 			item->Drop(location.get(), i, j);
70 		}
71 
72 	hero_in = 0;
73 }
74 
onCreaturePickItem(XCreature * cr,XItem * item)75 int XShop::onCreaturePickItem(XCreature * cr, XItem * item)
76 {
77 	if (owner)
78 	{
79 		return ((XShopKeeperAI *)(owner->xai))->onAnyonePickItem(cr, item);
80 	}
81 	return 1;
82 }
83 
84 
onCreatureEnter(XCreature * cr)85 int XShop::onCreatureEnter(XCreature * cr)
86 {
87 	if (cr->isHero())
88 	{
89 		for (int i = area.left + 1; i < area.right - 1; i++)
90 			for (int j = area.top + 1; j < area.bottom - 1; j++)
91 			{
92 				XItemList * ilist = location->map->GetItemList(i, j);
93 				it_iterator it;
94 				for(it = ilist->begin(); it != ilist->end(); it++)
95 				{
96 					XItem * tit = static_cast<XItem *>(static_cast<XObject *>(it));
97 					tit->Identify(1);
98 				}
99 			}
100 	}
101 
102 	if (owner)
103 	{
104 		((XShopKeeperAI *)(owner->xai))->onCreatureEnterShop(cr);
105 	}
106 	return 1;
107 };
108 
onCreatureLeave(XCreature * cr)109 int XShop::onCreatureLeave(XCreature * cr)
110 {
111 	if (owner)
112 	{
113 		((XShopKeeperAI *)(owner->xai))->onCreatureLeaveShop(cr);
114 	}
115 	return 1;
116 };
117 
onCreatureDropItem(XCreature * cr,XItem * item)118 int XShop::onCreatureDropItem(XCreature * cr, XItem * item)
119 {
120 //	cr->contain->Add(new XMoney(item->GetValue() * item->quantity));
121 	if (owner)
122 	{
123 		return ((XShopKeeperAI *)(owner->xai))->onAnyoneDropItem(cr, item);
124 	}
125 	return 1;
126 }
127 
128 
onCreatureMove(XCreature * cr)129 int XShop::onCreatureMove(XCreature * cr)
130 {
131 	return 1;
132 }
133 
Store(XFile * f)134 void XShop::Store(XFile * f)
135 {
136 	XAnyPlace::Store(f);
137 	f->Write(&hero_in);
138 	f->Write(&shop_mask, sizeof(ITEM_MASK));
139 }
140 
Restore(XFile * f)141 void XShop::Restore(XFile * f)
142 {
143 	XAnyPlace::Restore(f);
144 	f->Read(&hero_in);
145 	f->Read(&shop_mask, sizeof(ITEM_MASK));
146 }
147 
onShowItem(XItem * item,char * buf)148 void XShop::onShowItem(XItem * item, char * buf)
149 {
150 	item->toString(buf);
151 	if (owner)
152 	{
153 		char buf2[256];
154 		sprintf(buf2, "{%dgp}", item->quantity * item->GetValue());
155 		strcat(buf, buf2);
156 	}
157 }
158