TriggerHappy
...
- Reaction score
- 341
CodeGen
v0.4
v0.4
What is CodeGen?
CodeGen is used convert integers into strings. Those strings can later be typed and the integers can be loaded. These systems are commonly called 'Save and Load' Systems. They are generally popular in the RPG genre where a player can 'convert his hero into a string' and then next game type it, and his hero would be back.
How do I use this?
First open the attached demo map and copy the maps Custom Script code into your maps custom script.
Next copy the CodeGen folder into your map and you should be all set!
As for adding new values it's pretty easy. This system can only save integers, which most handles (variable-types) in warcraft can be converted to. Lets say you wanted to save the players lumber, you would add this to the save trigger;
And in the load trigger you would add;
But please remember, you must load everything in the order you saved them.
Next copy the CodeGen folder into your map and you should be all set!
As for adding new values it's pretty easy. This system can only save integers, which most handles (variable-types) in warcraft can be converted to. Lets say you wanted to save the players lumber, you would add this to the save trigger;
Code:
Set SaveCount = (SaveCount + 1)
Set Save[SaveCount] = ((Triggering player) Current lumber)
And in the load trigger you would add;
Code:
Set LoadCount = (LoadCount + 1)
Player - Set (Triggering player) Current lumber to Load[LoadCount]
But please remember, you must load everything in the order you saved them.
What does this save?
Well the demo map saves the heroes level, items, and gold as well as the players name. But the system can save anything which can be converted to an integer. So please I don't want to hear any 'Can this save XXX?'.
Frequently Asked QuestionsDoes this save the players name?
Yes, but you can disable it in the config trigger.
Is this secure?
I think so, if anyone cracks it please PM me or reply. But as for the general public who will be playing your game, they probably won't crack it. The chances are very low of guessing a code and probably will never happen.
How short is the code?
It will get longer with the more values you save. Also increasing the alphabet size decreases the code size.
Yes, but you can disable it in the config trigger.
Is this secure?
I think so, if anyone cracks it please PM me or reply. But as for the general public who will be playing your game, they probably won't crack it. The chances are very low of guessing a code and probably will never happen.
How short is the code?
It will get longer with the more values you save. Also increasing the alphabet size decreases the code size.
Code
Please note that you do not need to use any jass in order to use this system. This is the backend of the system.
JASS:
function CG_Init takes nothing returns nothing
set udg_Compress = SubString(udg_Alphabet, StringLength(udg_Alphabet)-1, 999)
set udg_Alphabet = SubString(udg_Alphabet, 0, StringLength(udg_Alphabet)-1)
endfunction
function CG_Encode takes integer i returns string
local integer b
local string s = ""
if i < udg_Base then
return SubString(udg_Alphabet, i, i + 1)
endif
loop
exitwhen i <= 0
set b = i - (i / udg_Base) * udg_Base
set s = SubString(udg_Alphabet, b, b + 1) + s
set i = i / udg_Base
endloop
return s
endfunction
function CG_SaveChar takes string c returns integer
local string low = "abcdefghijklmnopqrstuvwxyz"
local string high = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
local string int = "0123456789"
local integer i = S2I(c)
if i > 0 then
return i
endif
if c == "0" then
return 0
endif
set i = 0
loop
exitwhen i > 26
if SubString(low, i, i + 1) == c then
return i
elseif SubString(high, i, i + 1) == c then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function CG_SaveString takes string name returns string
local integer i = 0
local integer a = StringLength(name)
local integer c = 0
loop
exitwhen i > a
set c = c + CG_SaveChar(SubString(name, i, i + 1))
set i = i + 1
endloop
return CG_Encode(c)
endfunction
function CG_StrPos takes string s returns integer
local integer i = 0
loop
exitwhen i > udg_Base
if s == SubString(udg_Alphabet, i, i + 1) then
return i
endif
set i = i + 1
endloop
return -1
endfunction
function CG_Decode takes string s returns integer
local integer a = 0
loop
exitwhen StringLength(s) == 1
set a = a * udg_Base + udg_Base * CG_StrPos(SubString(s, 0, 1))
set s = SubString(s, 1, 50)
endloop
return a+CG_StrPos(s)
endfunction
function CG_SaveBoolean takes boolean flag returns integer
if flag then
return 1
endif
return 0
endfunction
function CG_SaveHero takes unit it returns integer
local integer i = 1
loop
exitwhen i > udg_HeroCount
if udg_SaveHero<i> == GetUnitTypeId(it) then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function CG_SaveItem takes item it returns integer
local integer i = 1
loop
exitwhen i > udg_ItemCount
if udg_SaveItem<i> == GetItemTypeId(it) then
return i
endif
set i = i + 1
endloop
return 0
endfunction
function CG_Seperate takes string s returns string
local integer i = 0
local integer b = StringLength(s)
local integer a = 0
loop
exitwhen i > b
if a == 4 then
set s = SubString(s, 0, i) + "-" + SubString(s, i, 999)
set a = 0
else
set a = a + 1
endif
set i = i + 1
endloop
return s
endfunction
function CG_Unseperate takes string s returns string
local integer i = 0
loop
exitwhen i > StringLength(s)
if SubString(s, i, i + 1) == "-" then
set s = SubString(s, 0, i) + SubString(s, i+1, 999)
endif
set i = i + 1
endloop
return s
endfunction
function CG_IsLower takes string s returns boolean
return s == "a" or s == "b" or s == "c" or s == "d" or s == "e" or s == "f" or s == "g" or s == "h" or s == "i" or s == "j" or s == "k" or s == "l" or s == "m" or s == "n" or s == "o" or s == "p" or s == "q" or s == "r" or s == "s" or s == "t" or s == "u" or s == "v" or s == "w" or s == "x" or s == "y" or s == "z"
endfunction
function CG_IsUpper takes string s returns boolean
return s == "W" or s == "A" or s == "B" or s == "C" or s == "D" or s == "E" or s == "F" or s == "G" or s == "H" or s == "I" or s == "J" or s == "K" or s == "L" or s == "M" or s == "N" or s == "O" or s == "P" or s == "Q" or s == "R" or s == "S" or s == "T" or s == "U" or s == "V" or s == "X" or s == "Y" or s == "Z"
endfunction
function CG_ColorChar takes string char returns string
local integer i = 0
local string s = udg_Alphabet + udg_Compress
loop
exitwhen i > udg_Base+1
if char == SubString(s, i, i + 1) then
if CG_IsLower(SubString(s, i, i + 1)) then
return udg_LOWER_CASE_COLOR + char + "|r"
elseif CG_IsUpper(SubString(s, i, i + 1)) then
return udg_UPPER_CASE_COLOR + char + "|r"
elseif SubString(s, i, i + 1) != "-" then
return udg_OTHER_CHARS_COLOR + char + "|r"
endif
endif
set i = i + 1
endloop
return char
endfunction
function CG_Color takes string s returns string
local integer i = 0
local string r = ""
loop
exitwhen i > StringLength(s)
set r = r + CG_ColorChar(SubString(s, i, i + 1))
set i = i + 1
endloop
return r
endfunction
function CG_ClearData takes nothing returns nothing
local integer i = 0
loop
exitwhen i > 50
set udg_Save<i> = 0
set i = i + 1
endloop
endfunction
function CG_SaveOnline takes nothing returns integer
if not ReloadGameCachesFromDisk() then
return 1
endif
return 0
endfunction
function CG_Execute takes nothing returns nothing
local integer i = 1
local integer t = 0
local string val = ""
set udg_Code = ""
loop
exitwhen i > udg_SaveCount
set val = CG_Encode(udg_Save<i>)
set t = StringLength(val)
if t > 1 then
set udg_Code = udg_Code+udg_Compress+I2S(t)+val
else
set udg_Code = udg_Code+val
endif
set i = i + 1
endloop
if udg_SavePlayerName then
set udg_Code = udg_Code+CG_SaveString(GetPlayerName(GetTriggerPlayer()))
endif
set udg_Code = CG_SaveString(udg_Code)+udg_Code
set udg_Code = CG_Seperate(udg_Code)
set udg_Code = CG_Color(udg_Code)
endfunction
function CG_Load takes string str returns nothing
local integer i = 0
local integer b = StringLength(CG_Unseperate(str))
local integer key = 1
local integer c = 1
local string tmp = ""
set str = CG_Unseperate(str)
set udg_LOAD_VALID = true
set tmp = CG_SaveString(SubString(str, 2, b))
if SubString(str, 0, StringLength(tmp)) != tmp then
set udg_LOAD_VALID = false
return
endif
set str = SubString(str, StringLength(tmp), 999)
if udg_SavePlayerName then
set tmp = CG_SaveString(GetPlayerName(GetTriggerPlayer()))
if StringLength(tmp) > b then
set udg_LOAD_VALID = false
else
if SubString(str, b-StringLength(tmp), b) != tmp then
set udg_LOAD_VALID = false
endif
endif
set b = b-1
set str = SubString(str, 0, b)
endif
loop
exitwhen i >= b
if SubString(str, i, i + 1) == udg_Compress then
set key = S2I(SubString(str, i + 1, i + 2))
set i = i + (key)
endif
set udg_Load[c] = CG_Decode(SubString(str, i, i + key))
set c = c + 1
set i = i + key
set key = 1
endloop
call CG_ClearData()
endfunction
</i></i></i></i>
Changelog
Code:
Version 0.4
-Fixed a bug where loading big values first would mess up.
-Code is more secure.
Version 0.3
-Rewrote Save/Load functions (fixing a common bug in which it would output wrong code).
-Player name saving is more accurate.
Version 0.2
-Fixed the multiline comment bug for vanilla WE users.
-Fixed an error that would give user wrong code.
-Made code slightly shorter, depending on circumstances.
-Demo now saves abilities
Version 0.1
-Initial release