-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathTaskSystem.kt
More file actions
241 lines (217 loc) · 8.87 KB
/
TaskSystem.kt
File metadata and controls
241 lines (217 loc) · 8.87 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package content.achievement
import content.entity.player.modal.Tab
import content.entity.player.modal.tab
import content.quest.questCompleted
import world.gregs.voidps.engine.Script
import world.gregs.voidps.engine.client.message
import world.gregs.voidps.engine.client.ui.chat.plural
import world.gregs.voidps.engine.client.ui.open
import world.gregs.voidps.engine.data.definition.EnumDefinitions
import world.gregs.voidps.engine.data.definition.StructDefinitions
import world.gregs.voidps.engine.data.definition.VariableDefinitions
import world.gregs.voidps.engine.entity.World
import world.gregs.voidps.engine.entity.character.player.Player
import world.gregs.voidps.engine.event.AuditLog
class TaskSystem(
val variables: VariableDefinitions,
) : Script {
init {
interfaceOpened("task_system") {
sendVariable("task_pin_slot")
sendVariable("task_pinned")
sendVariable("introducing_explorer_jack_task")
refreshSlots(this)
if (contains("task_dont_show_again")) {
sendVariable("task_dont_show_again")
}
if (!questCompleted("unstable_foundations")) {
set("task_pinned", 3520) // Talk to explorer jack
set("task_pin_slot", 1)
set("task_slot_selected", 1)
set("unstable_foundations", "incomplete")
}
}
entered("lumbridge") {
set("task_area", "lumbridge_draynor")
}
exited("lumbridge") {
set("task_area", "dnd_activities")
}
entered("draynor") {
set("task_area", "lumbridge_draynor")
}
exited("draynor") {
set("task_area", "dnd_activities")
}
interfaceOption("Close", "task_system:close_hint") {
interfaces.sendVisibility("task_system", "message_overlay", false)
}
interfaceOption("Select Task", "task_system:task_*") {
val slot = it.component.removePrefix("task_").toInt()
set("task_slot_selected", slot)
}
interfaceOption("Toggle", "task_system:dont_show") {
set("task_dont_show_again", !get("task_dont_show_again", false))
}
interfaceOption("Open", "task_system:task_list") {
open("task_list")
}
interfaceOption("OK", "task_system:ok") {
interfaces.sendVisibility("task_system", "summary_overlay", false)
val slot = get("task_slot_selected", 0)
val selected = indexOfSlot(this, slot) ?: return@interfaceOption
if (selected == get("task_pinned", -1)) {
clear("task_pinned")
clear("task_pin_slot")
}
interfaces.sendVisibility("task_system", "ok", false)
refreshSlots(this)
}
interfaceOption("Pin/Unpin Task", "task_system:task_*") {
val index = it.component.removePrefix("task_").toInt()
pin(this, index)
}
interfaceOption("Set", "task_system:pin") {
val slot = get<Int>("task_slot_selected") ?: return@interfaceOption
pin(this, slot)
interfaces.sendVisibility("task_system", "summary_overlay", false)
}
interfaceOption("Details", "task_popup:details") {
if (questCompleted("unstable_foundations")) {
set("task_popup_summary", true)
interfaces.sendVisibility("task_system", "ok", true)
val index = get("task_popup", -1)
for (slot in 0 until 6) {
if (get("task_slot_$slot", -1) == index) {
set("task_slot_selected", slot)
break
}
}
}
tab(Tab.TaskSystem)
}
interfaceOption("Hint", "task_system:hint_*") {
val selected = get("task_slot_selected", 0)
val index = indexOfSlot(this, selected) ?: return@interfaceOption
val tile: Int = EnumDefinitions.getStructOrNull("task_structs", index, it.component.replace("hint_", "task_hint_tile_")) ?: return@interfaceOption
// TODO I expect the functionality is actually minimap highlights not world map
set("world_map_marker_1", tile)
set("world_map_marker_text_1", "")
open("world_map")
}
variableSet("task_pin_slot,task_area,*_task") { key, _, to ->
if (key == "task_pin_slot" || key == "task_area") {
refreshSlots(this)
} else if (key.endsWith("_task") && (to == true || to == "completed")) {
completeTask(this, key)
}
}
}
fun pin(player: Player, slot: Int) {
if (player["task_pin_slot", -1] == slot) {
player.clear("task_pinned")
player.clear("task_pin_slot")
} else {
player["task_pinned"] = indexOfSlot(player, slot) ?: return
player["task_pin_slot"] = slot
}
}
fun indexOfSlot(player: Player, slot: Int): Int? {
var count = 1
return Tasks.forEach(areaId(player)) {
val hideCompleted = Tasks.isCompleted(player, definition.stringId)
val hideMembers = definition["task_members", 0] == 1 && !World.members
if (hideCompleted || hideMembers) {
return@forEach null
}
if (count == player["task_pin_slot", -1]) {
val pinned = player["task_pinned", 4091]
if (count == slot) {
return@forEach pinned
}
skip = pinned != index
}
if (count++ == slot) {
return@forEach index
}
null
}
}
fun refreshSlots(player: Player) {
var slot = 1
var completed = 0
var total = 0
Tasks.forEach(areaId(player)) {
total++
val pinned = pinned(player, slot)
if (player["task_pinned", -1] == index && !pinned || !Tasks.hasRequirements(player, definition)) {
return@forEach null
}
if (Tasks.isCompleted(player, definition.stringId)) {
completed++
return@forEach null
}
if (pinned) {
player["task_slot_${slot++}"] = player["task_pinned", 4091]
total--
skip = true
} else if (slot < 7) {
player["task_slot_${slot++}"] = index
}
null
}
if (slot < 7) {
for (i in slot..6) {
player["task_slot_$i"] = 4091
}
}
player["task_progress_total"] = total
player["task_progress_current"] = completed
}
fun pinned(player: Player, slot: Int): Boolean {
val pinned = player["task_pin_slot", -1]
return pinned != -1 && slot == pinned
}
fun areaId(player: Player) = variables.get("task_area")!!.values.toInt(player["task_area", "empty"])
/*
Task completion
*/
fun completeTask(player: Player, id: String) {
val definition = StructDefinitions.get(id)
AuditLog.event(player, "task_completed", id)
val index = definition["task_index", -1]
player["task_popup"] = index
val difficulty = definition["task_difficulty", 0]
val area = definition["task_area", 61]
val areaName = EnumDefinitions.get("task_area_names").getString(area)
val difficultyName = EnumDefinitions.get("task_difficulties").getString(difficulty)
if (areaName.isNotBlank() && difficultyName.isNotBlank()) {
player.message("You have completed the Task '${definition["task_name", ""]}' in the $difficultyName $areaName set!")
} else {
player.message("You have completed the Task '${definition["task_name", ""]}'!")
}
val before = player["task_progress_current", 0]
refreshSlots(player)
val total = player.inc("task_progress_overall")
player.message("You have now completed $total ${"Task".plural(total)} in total.")
val after = player["task_progress_current", 0]
val maximum = player["task_progress_total", -1]
if (before != after && after == maximum) {
val prettyName = when (area) {
1 -> "Lumbridge and Draynor"
else -> areaName
}
player.message("Congratulations! You have completed all of the $difficultyName Tasks in the $prettyName")
val npc = when {
area == 1 && difficulty == 1 -> "Explorer Jack in Lumbridge"
area == 1 && difficulty == 2 -> "Bob in Bob's Axes in Lumbridge"
area == 1 && difficulty == 3 -> "Ned in Draynor Village"
else -> "someone somewhere"
}
player.message("set. Speak to $npc to claim your reward.")
}
}
/*
Hints
*/
}