summaryrefslogtreecommitdiff
path: root/utils/tele.js
blob: 95da71b16cfb99d62af5a104da85e2f7b1a91d9b (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// this file deals with linked teleporters,
// not directional teleporters



/*
the corresponding tile is 0x9b


generate
[x]	make new entry in global door registry
[x]	set tile 0x9b
0x9b crackend
[x]	put item in inventory
place
[x]	set tile 0x9b
[x]	remove from inventory
inventory UI
[x]
[x]	unexpected inventory UI overhaul
0x9b onstep (first time)
[ ]	get new coord
[ ]	generate new chunk
[ ] 	generate new tele
0x9b onstep (routine)
[ ]
*/

const doorPath = "doors.txt";



let fs = require("fs");
let doors; {
	try {
		let data = fs.readFileSync(doorPath, 'utf8', function(e, data) {
			if (e) console.log('ladsjflajksfj');
			console.log(data);
		});
		doors = JSON.parse(data.toString());
	} catch (e) {
		doors = [];
	}
}
let dirty = false;

let doorUtils = {};
module.exports = doorUtils;

let Pos, createPosFromJson; {
	let t = require("models/pos");
	Pos = t.Pos;
	createPosFromJson = t.createPosFromJson;
}
let chunkUtils = require("utils/chunk.js");

doorUtils.moveDoorToPlayer = function(doorid, inventory, username) {
	let door = doors[doorid];
	door.postype = "player";
	door.pos = username;
	let m = inventory.tileCountMap[0x9b];
	if (!m) inventory.tileCountMap[0x9b] = [doorid];
	else m.push(doorid);
};

doorUtils.findDoorIdWithPos = function(pos) {
	for (let i = 0; i < doors.length; i++) {
		if (doors[i].postype != "coordinate") continue;
		if (pos.equals(doors[i].pos)) {
			return i;
		}
	}
	return null;
};
doorUtils.findDoorWithPos = function(pos) {
	let doorid = doorUtils.findDoorIdWithPos(pos);
	if (doorid === null) return null;
	return doors[doorid];
};

doorUtils.getDestCoord = function(doorid) {
	// needs to handle chunk generation
	if (doorid === null) console.log("ladkfjajsf");
	let door = doors[doorid];
	if (door.destId === null) { // generate new chunk
		const r = 100000 + 900000 * Math.random();
		for (let i = 0; i < 10; i++) {
			let th = Math.random() * 2 * Math.PI;
			let xoff = Math.cos(th) * r;
			let yoff = Math.sin(th) * r;
			let chunkSize = 128;
			let cx = Math.floor((door.pos.x + xoff) / chunkSize) * chunkSize;
			let cy = Math.floor((door.pos.y + yoff) / chunkSize) * chunkSize;
			let pos = new Pos(cx, cy);
			let chunkExist = chunkUtils.checkChunkExist(pos);
			if (chunkExist) continue;
			let chunk = chunkUtils.getChunk(pos);
			pos = new Pos(pos.x,pos.y);
			pos.add(new Pos(
				Math.floor(Math.random() * chunkSize),
				Math.floor(Math.random() * chunkSize),
			));
			chunk.generateAllTiles();
			chunk.setTile(pos, 0x9b);
			let destid = doorUtils.registerDoor(pos);
			doors[destid].destId = doorid;
			door.destId = destid;
			return pos;
		}
		console.warn("lkadsfald");
		return null;
	} else {
		let destdoor = doors[door.destId];
		if (destdoor.postype == "player") {
			// handle later...
			// if the player is online, teleport to player
			// otherwise null
		} else if (destdoor.postype == "coordinate") {
			return new Pos(destdoor.pos.x, destdoor.pos.y);
		} else {
			console.error("laksflsdf");
			return null;
		}
	}
};

doorUtils.removeDoorFromInventory = function(inv, doorid) {
	let m = inv.tileCountMap[0x9b];
	for (let i = 0; i < m.length; i++) {
		if (m[i] === doorid) {
			m.splice(i, 1);
			return true;
		}
	}
	return false;
};

doorUtils.placeDoor = function(doorid, pos) {
	chunkUtils.setTile(pos, 0x9b);
	doors[doorid].postype = "coordinate";
	doors[doorid].pos = pos;
};

doorUtils.registerDoor = function(pos) {
	let doorId = doors.length;
	doors.push({
		id: doorId,
		postype: "coordinate",
		pos: pos,
		destId: null
	});
	return doorId;
};



doorUtils.persistAllDoors = function() {
	fs.writeFileSync(doorPath, JSON.stringify(doors));
	dirty = false;
};