This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtmwchat-util.el
158 lines (141 loc) · 4.46 KB
/
tmwchat-util.el
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
(require 'tmwchat-log)
(defun tmwchat-decode-string (s)
(condition-case nil
(decode-coding-string s 'utf-8 t)
(error "{{STRING_DECODE_ERROR}}")))
(defun tmwchat--contains-302-202 (str)
"Check if string contains ManaPlus-specific messages with \302\202
that breaks utf8 decoding"
(string-match-p "#o302|#o202" str))
(defun vec-less (v1 v2)
(or (< (elt v1 3) (elt v2 3))
(and (= (elt v1 3) (elt v2 3))
(or (< (elt v1 2) (elt v2 2))
(and (= (elt v1 2) (elt v2 2))
(or (< (elt v1 1) (elt v2 1))
(and (= (elt v1 1) (elt v2 1))
(< (elt v1 0) (elt v2 0)))))))))
(make-variable-buffer-local 'vec-less)
(defun tmwchat-read-coordinates (v)
(let ((x) (y)
(v0 (elt v 0))
(v1 (elt v 1))
(v2 (elt v 2)))
(message "tmwchat-read-coordinates %s" v)
(setq x (lsh (logior (lsh v0 8)
(logand v1 #xc0))
-6))
(setq y (lsh (logior (lsh (logand v1 #x3f) 8)
(logand v2 #xf0))
-4))
(cons x y)))
(make-variable-buffer-local 'tmwchat-read-coordinates)
(defun tmwchat-read-coordinate-pair (v)
(let ((x1) (y1) (x2) (y2)
(v0 (elt v 0))
(v1 (elt v 1))
(v2 (elt v 2))
(v3 (elt v 3))
(v4 (elt v 4)))
(setq x1 (lsh (logior (lsh v0 8)
(logand v1 #xc0))
-6))
(setq y1 (lsh (logior (lsh (logand v1 #x3f) 8)
(logand v2 #xf0))
-4))
(setq x2 (lsh (logior (lsh (logand v2 #x0f) 8)
v3)
-2))
(setq y2 (logior (lsh (logand v3 #x03) 8) v4))
(list x1 y1 x2 y2)))
(make-variable-buffer-local 'tmwchat-read-coordinate-pair)
(defun tmwchat-coordinate-pair-to-vector (x1 y1 x2 y2)
(let
((v0 (lsh x1 -2))
(v1 (logior
(lsh (logand x1 #x03) 6)
(lsh (logand y1 #x3f0) -4)))
(v2 (logior
(lsh (logand y1 #x0f) 4)
(lsh (logand x2 #x3c0) -6)))
(v3 (logior
(lsh (logand x2 #x3f) 2)
(lsh (logand y2 #x300) -8)))
(v4 (logand y2 #xff)))
(vector v0 v1 v2 v3 v4)))
(defun tmwchat-coordinate-to-vector (x y)
(let
((v0 (lsh x -2))
(v1 (logior
(lsh (logand x #x03) 6)
(lsh (logand y #x3f0) -4)))
(v2 (lsh (logand y #x0f) 4)))
(vector v0 v1 v2)))
(defun tmwchat-escape-percent (str)
(let ((splt (split-string str "%")))
(mapconcat 'identity splt "%%")))
(defun tmwchat--remove-color (str)
(while (string-match "##[0-9bB]" str)
(setq str (replace-match "" nil nil str)))
(ignore-errors
(let ((beg) (code) (emote))
(while (setq beg (string-match "%%[^%]" str))
(setq code (- (elt str (+ beg 2)) 48)
emote (elt tmwchat-emotes-2 code)
str (replace-match emote nil nil str)))))
(setq str (tmwchat-escape-percent str))
str)
(defun tmwchat-make-read-only ()
"Make all the text in the current buffer read-only."
(add-text-properties (point-min) tmwchat--start-point
'(read-only t front-sticky t rear-nonsticky t)))
(defun tmwchat-find-player-id (name)
"Find player ID by name."
(let ((found-id -1))
(maphash
(lambda (id pname)
(when (string-equal name pname)
(setq found-id id)))
tmwchat-player-names)
found-id))
(defun tmwchat-read-itemdb (dbfile &optional dbhash)
(interactive "fPath to itemdb.txt: ")
(with-temp-buffer
(message "Loading itemdb from %s" dbfile)
(insert-file-contents dbfile)
(let ((itemsdb (or dbhash (make-hash-table :test 'equal))))
(while (not (eobp))
(let ((line (buffer-substring-no-properties
(line-beginning-position)
(line-end-position)))
(item-id) (item-name))
(when (string-match "^\\([0-9]+\\)[\t ]+\\(.+\\)" line)
(setq item-id
(string-to-int
(substring line (match-beginning 1) (match-end 1)))
item-name
(substring line (match-beginning 2) (match-end 2)))
(puthash item-id item-name itemsdb)))
(forward-line 1))
itemsdb)))
(defun tmwchat-item-name (id &optional link)
"Get item [name] from itemdb.
When link is non-nil, return [@@id|name@@]"
(if (= id 0) "GP"
(let ((name (gethash id tmwchat-itemdb "Unknown")))
(if link
(format "[@@%d|%s@@]" id name)
(format "[%s]" name)))))
(defun list-random-element (lst)
"Return random element of list.
If list is empty, return nil"
(let ((len (length lst)))
(nth (random len) lst)))
(defun tmwchat-item-id-help (widget)
"Function should be used as a :help-echo property
for customization options with item IDs."
(let* ((value (widget-value widget))
(id (cond ((stringp value) (string-to-int value))
((integerp value) value))))
(gethash id tmwchat-itemdb "Enter item ID")))
(provide 'tmwchat-util)