This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 66
/
type2type.dm
554 lines (492 loc) · 13.4 KB
/
type2type.dm
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/*
* Holds procs designed to change one type of value, into another.
* Contains:
* hex2num & num2hex
* file2list
* angle2dir
* angle2text
* worldtime2text
* text2dir_extended & dir2text_short
*/
//Returns an integer given a hex input, supports negative values "-ff"
//skips preceding invalid characters
//breaks when hittin invalid characters thereafter
// If safe=TRUE, returns null on incorrect input strings instead of CRASHing
/proc/hex2num(hex, safe=FALSE)
. = 0
var/place = 1
for(var/i in length(hex) to 1 step -1)
var/num = text2ascii(hex, i)
switch(num)
if(48 to 57)
num -= 48 //0-9
if(97 to 102)
num -= 87 //a-f
if(65 to 70)
num -= 55 //A-F
if(45)
return . * -1 // -
else
if(safe)
return null
else
CRASH("Malformed hex number")
. += num * place
place *= 16
//Returns the hex value of a decimal number
//len == length of returned string
//if len < 0 then the returned string will be as long as it needs to be to contain the data
//Only supports positive numbers
//if an invalid number is provided, it assumes num==0
//Note, unlike previous versions, this one works from low to high <-- that way
/proc/num2hex(num, len=2)
if(!isnum(num))
num = 0
num = round(abs(num))
. = ""
var/i=0
while(1)
if(len<=0)
if(!num)
break
else
if(i>=len)
break
var/remainder = num/16
num = round(remainder)
remainder = (remainder - num) * 16
switch(remainder)
if(9,8,7,6,5,4,3,2,1)
. = "[remainder]" + .
if(10,11,12,13,14,15)
. = ascii2text(remainder+87) + .
else
. = "0" + .
i++
return .
//Splits the text of a file at seperator and returns them in a list.
//returns an empty list if the file doesn't exist
/world/proc/file2list(filename, seperator="\n", trim = TRUE)
if (trim)
return splittext(trim(file2text(filename)),seperator)
return splittext(file2text(filename),seperator)
//Turns a direction into text
/proc/dir2text(direction)
switch(direction)
if(NORTH)
return "north"
if(SOUTH)
return "south"
if(EAST)
return "east"
if(WEST)
return "west"
if(NORTHEAST)
return "northeast"
if(SOUTHEAST)
return "southeast"
if(NORTHWEST)
return "northwest"
if(SOUTHWEST)
return "southwest"
else
return
//Turns text into proper directions
/proc/text2dir(direction)
switch(uppertext(direction))
if("NORTH")
return NORTH
if("SOUTH")
return SOUTH
if("EAST")
return EAST
if("WEST")
return WEST
if("NORTHEAST")
return NORTHEAST
if("NORTHWEST")
return NORTHWEST
if("SOUTHEAST")
return SOUTHEAST
if("SOUTHWEST")
return SOUTHWEST
else
return
//Converts an angle (degrees) into an ss13 direction
GLOBAL_LIST_INIT(modulo_angle_to_dir, list(NORTH,NORTHEAST,EAST,SOUTHEAST,SOUTH,SOUTHWEST,WEST,NORTHWEST))
#define angle2dir(X) (GLOB.modulo_angle_to_dir[round((((X%360)+382.5)%360)/45)+1])
/proc/angle2dir_cardinal(degree)
degree = SIMPLIFY_DEGREES(degree)
switch(round(degree, 0.1))
if(315.5 to 360, 0 to 45.5)
return NORTH
if(45.6 to 135.5)
return EAST
if(135.6 to 225.5)
return SOUTH
if(225.6 to 315.5)
return WEST
//returns the north-zero clockwise angle in degrees, given a direction
/proc/dir2angle(D)
switch(D)
if(NORTH)
return 0
if(SOUTH)
return 180
if(EAST)
return 90
if(WEST)
return 270
if(NORTHEAST)
return 45
if(SOUTHEAST)
return 135
if(NORTHWEST)
return 315
if(SOUTHWEST)
return 225
else
return null
//Returns the angle in english
/proc/angle2text(degree)
return dir2text(angle2dir(degree))
//Converts a blend_mode constant to one acceptable to icon.Blend()
/proc/blendMode2iconMode(blend_mode)
switch(blend_mode)
if(BLEND_MULTIPLY)
return ICON_MULTIPLY
if(BLEND_ADD)
return ICON_ADD
if(BLEND_SUBTRACT)
return ICON_SUBTRACT
else
return ICON_OVERLAY
//Converts a rights bitfield into a string
/proc/rights2text(rights, seperator="", prefix = "+")
seperator += prefix
if(rights & R_BUILD)
. += "[seperator]BUILDMODE"
if(rights & R_ADMIN)
. += "[seperator]ADMIN"
if(rights & R_BAN)
. += "[seperator]BAN"
if(rights & R_FUN)
. += "[seperator]FUN"
if(rights & R_ROD) //WS edit: ROD
. += "[seperator]ROD"
if(rights & R_SERVER)
. += "[seperator]SERVER"
if(rights & R_DEBUG)
. += "[seperator]DEBUG"
if(rights & R_POSSESS)
. += "[seperator]POSSESS"
if(rights & R_PERMISSIONS)
. += "[seperator]PERMISSIONS"
if(rights & R_STEALTH)
. += "[seperator]STEALTH"
if(rights & R_POLL)
. += "[seperator]POLL"
if(rights & R_VAREDIT)
. += "[seperator]VAREDIT"
if(rights & R_SOUND)
. += "[seperator]SOUND"
if(rights & R_SPAWN)
. += "[seperator]SPAWN"
if(rights & R_AUTOADMIN)
. += "[seperator]AUTOLOGIN"
if(rights & R_DBRANKS)
. += "[seperator]DBRANKS"
if(!.)
. = "NONE"
return .
//colour formats
/proc/rgb2hsl(red, green, blue)
red /= 255;green /= 255;blue /= 255;
var/max = max(red,green,blue)
var/min = min(red,green,blue)
var/range = max-min
var/hue=0;var/saturation=0;var/lightness=0;
lightness = (max + min)/2
if(range != 0)
if(lightness < 0.5)
saturation = range/(max+min)
else
saturation = range/(2-max-min)
var/dred = ((max-red)/(6*max)) + 0.5
var/dgreen = ((max-green)/(6*max)) + 0.5
var/dblue = ((max-blue)/(6*max)) + 0.5
if(max==red)
hue = dblue - dgreen
else if(max==green)
hue = dred - dblue + (1/3)
else
hue = dgreen - dred + (2/3)
if(hue < 0)
hue++
else if(hue > 1)
hue--
return list(hue, saturation, lightness)
/proc/hsl2rgb(hue, saturation, lightness)
var/red;var/green;var/blue;
if(saturation == 0)
red = lightness * 255
green = red
blue = red
else
var/a;var/b;
if(lightness < 0.5)
b = lightness*(1+saturation)
else
b = (lightness+saturation) - (saturation*lightness)
a = 2*lightness - b
red = round(255 * hue2rgb(a, b, hue+(1/3)))
green = round(255 * hue2rgb(a, b, hue))
blue = round(255 * hue2rgb(a, b, hue-(1/3)))
return list(red, green, blue)
/proc/hue2rgb(a, b, hue)
if(hue < 0)
hue++
else if(hue > 1)
hue--
if(6*hue < 1)
return (a+(b-a)*6*hue)
if(2*hue < 1)
return b
if(3*hue < 2)
return (a+(b-a)*((2/3)-hue)*6)
return a
/// For finding out what body parts a body zone covers, the inverse of the below basically
/proc/zone2body_parts_covered(def_zone)
switch(def_zone)
if(BODY_ZONE_CHEST)
return list(CHEST, GROIN)
if(BODY_ZONE_HEAD)
return list(HEAD)
if(BODY_ZONE_L_ARM)
return list(ARM_LEFT, HAND_LEFT)
if(BODY_ZONE_R_ARM)
return list(ARM_RIGHT, HAND_RIGHT)
if(BODY_ZONE_L_LEG)
return list(LEG_LEFT, FOOT_LEFT)
if(BODY_ZONE_R_LEG)
return list(LEG_RIGHT, FOOT_RIGHT)
//Turns a Body_parts_covered bitfield into a list of organ/limb names.
//(I challenge you to find a use for this) -I found a use for it!!
/proc/body_parts_covered2organ_names(bpc)
var/list/covered_parts = list()
if(!bpc)
return 0
if(bpc & FULL_BODY)
covered_parts |= list(BODY_ZONE_L_ARM,BODY_ZONE_R_ARM,BODY_ZONE_HEAD,BODY_ZONE_CHEST,BODY_ZONE_L_LEG,BODY_ZONE_R_LEG)
else
if(bpc & HEAD)
covered_parts |= list(BODY_ZONE_HEAD)
if(bpc & CHEST)
covered_parts |= list(BODY_ZONE_CHEST)
if(bpc & GROIN)
covered_parts |= list(BODY_ZONE_CHEST)
if(bpc & ARMS)
covered_parts |= list(BODY_ZONE_L_ARM,BODY_ZONE_R_ARM)
else
if(bpc & ARM_LEFT)
covered_parts |= list(BODY_ZONE_L_ARM)
if(bpc & ARM_RIGHT)
covered_parts |= list(BODY_ZONE_R_ARM)
if(bpc & HANDS)
covered_parts |= list(BODY_ZONE_L_ARM,BODY_ZONE_R_ARM)
else
if(bpc & HAND_LEFT)
covered_parts |= list(BODY_ZONE_L_ARM)
if(bpc & HAND_RIGHT)
covered_parts |= list(BODY_ZONE_R_ARM)
if(bpc & LEGS)
covered_parts |= list(BODY_ZONE_L_LEG,BODY_ZONE_R_LEG)
else
if(bpc & LEG_LEFT)
covered_parts |= list(BODY_ZONE_L_LEG)
if(bpc & LEG_RIGHT)
covered_parts |= list(BODY_ZONE_R_LEG)
if(bpc & FEET)
covered_parts |= list(BODY_ZONE_L_LEG,BODY_ZONE_R_LEG)
else
if(bpc & FOOT_LEFT)
covered_parts |= list(BODY_ZONE_L_LEG)
if(bpc & FOOT_RIGHT)
covered_parts |= list(BODY_ZONE_R_LEG)
return covered_parts
/proc/slot2body_zone(slot)
switch(slot)
if(ITEM_SLOT_BACK, ITEM_SLOT_OCLOTHING, ITEM_SLOT_ICLOTHING, ITEM_SLOT_BELT, ITEM_SLOT_ID)
return BODY_ZONE_CHEST
if(ITEM_SLOT_GLOVES, ITEM_SLOT_HANDS, ITEM_SLOT_HANDCUFFED)
return pick(BODY_ZONE_PRECISE_L_HAND, BODY_ZONE_PRECISE_R_HAND)
if(ITEM_SLOT_HEAD, ITEM_SLOT_NECK, ITEM_SLOT_NECK, ITEM_SLOT_EARS)
return BODY_ZONE_HEAD
if(ITEM_SLOT_MASK)
return BODY_ZONE_PRECISE_MOUTH
if(ITEM_SLOT_EYES)
return BODY_ZONE_PRECISE_EYES
if(ITEM_SLOT_FEET)
return pick(BODY_ZONE_PRECISE_R_FOOT, BODY_ZONE_PRECISE_L_FOOT)
if(ITEM_SLOT_LEGCUFFED)
return pick(BODY_ZONE_L_LEG, BODY_ZONE_R_LEG)
//adapted from http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
/proc/heat2colour(temp)
return rgb(heat2colour_r(temp), heat2colour_g(temp), heat2colour_b(temp))
/proc/heat2colour_r(temp)
temp /= 100
if(temp <= 66)
. = 255
else
. = max(0, min(255, 329.698727446 * (temp - 60) ** -0.1332047592))
/proc/heat2colour_g(temp)
temp /= 100
if(temp <= 66)
. = max(0, min(255, 99.4708025861 * log(temp) - 161.1195681661))
else
. = max(0, min(255, 288.1221685293 * ((temp - 60) ** -0.075148492)))
/proc/heat2colour_b(temp)
temp /= 100
if(temp >= 66)
. = 255
else
if(temp <= 16)
. = 0
else
. = max(0, min(255, 138.5177312231 * log(temp - 10) - 305.0447927307))
/proc/color2hex(color) //web colors
if(!color)
return "#000000"
switch(color)
if("white")
return "#FFFFFF"
if("black")
return "#000000"
if("gray")
return "#808080"
if("brown")
return "#A52A2A"
if("red")
return "#FF0000"
if("darkred")
return "#8B0000"
if("crimson")
return "#DC143C"
if("orange")
return "#FFA500"
if("yellow")
return "#FFFF00"
if("green")
return "#008000"
if("lime")
return "#00FF00"
if("darkgreen")
return "#006400"
if("cyan")
return "#00FFFF"
if("blue")
return "#0000FF"
if("navy")
return "#000080"
if("teal")
return "#008080"
if("purple")
return "#800080"
if("indigo")
return "#4B0082"
else
return "#FFFFFF"
//This is a weird one:
//It returns a list of all var names found in the string
//These vars must be in the [var_name] format
//It's only a proc because it's used in more than one place
//Takes a string and a datum
//The string is well, obviously the string being checked
//The datum is used as a source for var names, to check validity
//Otherwise every single word could technically be a variable!
/proc/string2listofvars(t_string, datum/var_source)
if(!t_string || !var_source)
return list()
. = list()
var/var_found = findtext(t_string,"\[") //Not the actual variables, just a generic "should we even bother" check
if(var_found)
//Find var names
// "A dog said hi [name]!"
// splittext() --> list("A dog said hi ","name]!"
// jointext() --> "A dog said hi name]!"
// splittext() --> list("A","dog","said","hi","name]!")
t_string = replacetext(t_string,"\[","\[ ")//Necessary to resolve "word[var_name]" scenarios
var/list/list_value = splittext(t_string,"\[")
var/intermediate_stage = jointext(list_value, null)
list_value = splittext(intermediate_stage," ")
for(var/value in list_value)
if(findtext(value,"]"))
value = splittext(value,"]") //"name]!" --> list("name","!")
for(var/A in value)
if(var_source.vars.Find(A))
. += A
//assumes format #RRGGBB #rrggbb
/proc/color_hex2num(A)
if(!A || length(A) != length_char(A))
return 0
var/R = hex2num(copytext(A, 2, 4))
var/G = hex2num(copytext(A, 4, 6))
var/B = hex2num(copytext(A, 6, 8))
return R+G+B
//word of warning: using a matrix like this as a color value will simplify it back to a string after being set
/proc/color_hex2color_matrix(string)
var/length = length(string)
if((length != 7 && length != 9) || length != length_char(string))
return color_matrix_identity()
var/r = hex2num(copytext(string, 2, 4))/255
var/g = hex2num(copytext(string, 4, 6))/255
var/b = hex2num(copytext(string, 6, 8))/255
var/a = 1
if(length == 9)
a = hex2num(copytext(string, 8, 10))/255
if(!isnum(r) || !isnum(g) || !isnum(b) || !isnum(a))
return color_matrix_identity()
return list(r,0,0,0, 0,g,0,0, 0,0,b,0, 0,0,0,a, 0,0,0,0)
//will drop all values not on the diagonal
/proc/color_matrix2color_hex(list/the_matrix)
if(!istype(the_matrix) || the_matrix.len != 20)
return "#ffffffff"
return rgb(the_matrix[1]*255, the_matrix[6]*255, the_matrix[11]*255, the_matrix[16]*255)
/proc/type2parent(child)
var/string_type = "[child]"
var/last_slash = findlasttext(string_type, "/")
if(last_slash == 1)
switch(child)
if(/datum)
return null
if(/obj || /mob)
return /atom/movable
if(/area || /turf)
return /atom
else
return /datum
return text2path(copytext(string_type, 1, last_slash))
//returns a string the last bit of a type, without the preceeding '/'
/proc/type2top(the_type)
//handle the builtins manually
if(!ispath(the_type))
return
switch(the_type)
if(/datum)
return "datum"
if(/atom)
return "atom"
if(/obj)
return "obj"
if(/mob)
return "mob"
if(/area)
return "area"
if(/turf)
return "turf"
else //regex everything else (works for /proc too)
return lowertext(replacetext("[the_type]", "[type2parent(the_type)]/", ""))
/// Return html to load a url.
/// for use inside of browse() calls to html assets that might be loaded on a cdn.
/proc/url2htmlloader(url)
return {"<html><head><meta http-equiv="refresh" content="0;URL='[url]'"/></head><body onLoad="parent.location='[url]'"></body></html>"}