-
Notifications
You must be signed in to change notification settings - Fork 2
/
room.erl
98 lines (71 loc) · 3.3 KB
/
room.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
%% @author M Wright <201176962@student.uj.ac.za>
%% @copyright Wright, MJ 2014-2015
%% @version 1.2
%% @title Room module
%% @doc All room based operations exist here.
%% Rooms are initialized, parsed and ordered here.
%% @end
-module(room).
-compile(export_all).
-include("recs.hrl").
%%basic start
st()->initrooms("room.conf").
%%load and setup rooms, parse items
initrooms(FilePath)->%%construct rooms
Result = file:consult(FilePath),
case Result of
{error, Error} -> io:fwrite("Failed to load rooms! ~p~n",[Error]);
{ok, Rooms} -> %%parse items into atoms here!
RoomList = [#room{title=Title,description=Descrpt,id=ID,doorEast=DE,doorWest=DW,doorNorth=DN,doorSouth=DS, item = parseRoomItems(ITEMS)} || {room, Title, Descrpt, ID, DE, DW, DN, DS, ITEMS}<-Rooms],
RoomList
end.
%%Get various information on rooms for players
getRoomDesc(Player, Rooms)->%%gets description of current room
[Descrpt || {room, _Title, Descrpt, ID, _DE, _DW, _DN, _DS, _ITEMS}<-Rooms,ID==Player#player.roomId].
getRoomInfo(Player, Rooms)->
["\r\n"++aCol:addcol({green,"Location:"})++aCol:addcol({cyan,getRoomTitle(Player, Rooms)})++"\r\n"++getRoomDesc(Player, Rooms)++getDoorDescript(Player, Rooms)].
getRoomTitle(Player, Rooms)->%%title of room
[Title || {room, Title, _Descrpt, ID, _DE, _DW, _DN, _DS, _ITEMS}<-Rooms,ID==Player#player.roomId].
getRoomTfromID(RID, Rooms)->[Title || {room, Title, _Descrpt, ID, _DE, _DW, _DN, _DS, _ITEMS}<-Rooms,ID==RID].
getRoomBasicInfo(Player, Rooms)->
["\r\n"++aCol:addcol({cyan,getRoomTitle(Player, Rooms)})++"\r\n"++getRoomDesc(Player, Rooms)++"\r\n"++getDoorDescript(Player, Rooms)++"\r\n"].
getDescript(Player, Rooms)->
["\r\n"++aCol:addcol({green,"Location:"})++aCol:addcol({cyan,getRoomTitle(Player, Rooms)})++"\r\n"++getDoorDescript(Player, Rooms)].
getDoorDescript(Player, Rooms)->%%Construct all moveable locations from current room
[{DE,DW,DN,DS}] = [{DE,DW,DN,DS} || {room, _Title, _Descrpt, ID, DE, DW, DN, DS, _ITEMS}<-Rooms, ID==Player#player.roomId],
["\r\n"++aCol:addcol({yellow,"Taking a look around you see that you can move: \r\n"})]++getDD("East", DE, Rooms)++getDD("West", DW, Rooms)++getDD("North", DN, Rooms)++getDD("South", DS, Rooms).
getDD(Dir, ID, Rooms)->%%helper
case ID of
-1->[];
_->[aCol:addcol({red,Dir})++" to the "++aCol:addcol({green,getRoomTfromID(ID, Rooms)})++"\r\n"]
end.
getValidDoors(Player, Rooms)->
[{DE,DW,DN,DS}] = [{DE,DW,DN,DS} || {room, _Title, _Descrpt, ID, DE, DW, DN, DS, _ITEMS}<-Rooms, ID==Player#player.roomId],
[getRoomTfromID(X, Rooms) || X <- [DE,DW,DN,DS], X > -1].
lookDir(Dir, DirID, Rooms)->
case DirID of
-1->["\r\nYou see nothing to the " ++ aCol:addcol({red,Dir})++"\r\n"];
_->["\r\nLooking to the " ++ aCol:addcol({red,Dir}) ++ " you see the " ++aCol:addcol({green,getRoomTfromID(DirID, Rooms)})++"\r\n"]
end.
%%Auxillary functions
getItemSearchSuccess(Player, Rooms)->
ITEMS = [ITEMS || {room, _Title, _Descrpt, ID, _DE, _DW, _DN, _DS, ITEMS}<-Rooms,ID==Player#player.roomId],
case ITEMS of
[empty]->-1;
_->random:uniform(3)
end.
parseRoomItems(ItemCodes)->
case ItemCodes of
[]->empty;
[1]->shitsword;
[2]->sword;
[3]->goodsword;
[4]->axe;
[5]->pike;
[6]->dagger;
[7]->knucklesandwitch;
_->empty
end.
getRoom(RID, Rooms)->
{value, Room} = lists:keysearch(RID, #room.id, Rooms),
Room.