Snippet GetRandomId

Tom Jones

N/A
Reaction score
437
A snippet that'll return a random unit id. I ported it from a tower defence I've long given up on. I admit that it may be a little big for a snippet, but one can hardly call it a system.

Code:
vJass.
JASS:
library GetRandomId initializer Init
//##############################################################################
//#GetRandomId by Tom Jones.
//##############################################################################
//#Requirements:
//#1) NewGen Editor.
//#
//#Implementation:
//#1) Copy this GUI trigger from this map to your map.
//#
//#Usage:
//#¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
//#   call GetRandomId(A_UNIT_TYPE,ONCE)
//#¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
//#   Returns a random unit id.
//#
//#   A_UNIT_TYPE must be one of the following:
//#    a) UNIT_TYPE_GROUND
//#    b) UNIT_TYPE_FLYING
//#    c) UNIT_TYPE_HERO
//#    d) UNIT_TYPE_RANDOM
//#
//#   ONCE is a boolean and determines if a unit id can be randomized more then
//#   once. If ONCE is true a unit id can only be randomized once.
//#
//#Notes:
//#1) Enabling Debug mode will display errors, warnings, and possible fixes.
//#2) Typing -getlastindex will return the index of the last randomized
//#   id. This is usefull if for some reason the id doesn't work, etc.  units
//#   without movement. If you find unit id that doesn't work, please PM me on 
//#   <a href="http://www.TheHelper.net" target="_blank" class="link link--external" rel="nofollow ugc noopener">www.TheHelper.net</a>.
//##############################################################################

//##############################################################################
//#Options.
//##############################################################################
globals
    //If you randomize an entire array, the below will reset the array so it can be
    //randomized again.
    private constant boolean RESET_ARRAYS       = true
    
    //Enabling the below boolean will check randomized ids for invisiblity, 
    //invulnerbility, and locust, if Debug mode is enabled.
    private constant boolean CHECK_ABILITIES    = true
endglobals

//##############################################################################
//#GetRandomId Code - Do not modify below here unless you know what your doing.
//##############################################################################
globals    
    constant unittype        UNIT_TYPE_RANDOM = ConvertUnitType(26) //No idea if this messes with anthing, so far I haven&#039;t been able to produce an error.
    private constant integer NO_ID = 0
    private integer          LAST_INDEX
    private integer array    GROUND
    private integer          GROUND_INDEX
    private integer array    AIR
    private integer          AIR_INDEX
    private integer array    HERO
    private integer          HERO_INDEX
    private constant string  ERROR = &quot;|cffff0000GetRandomId() Error:|r &quot;
    private constant string  WARNING = &quot;|cffff0000GetRandomId() Warning:|r &quot;
    private constant string  FIX = &quot;|cff00ff00GetRandomId() Fix:|r &quot;
endglobals

private function Init_Heroes takes nothing returns nothing
    set HERO[0] = &#039;Hamg&#039;
    set HERO[1] = &#039;Hblm&#039;
    set HERO[2] = &#039;Hmkg&#039;
    set HERO[3] = &#039;Hpal&#039;
    set HERO[4] = &#039;Obla&#039;
    set HERO[5] = &#039;Ofar&#039;
    set HERO[6] = &#039;Oshd&#039;
    set HERO[7] = &#039;Otch&#039;
    set HERO[8] = &#039;Edem&#039;
    set HERO[9] = &#039;Edmm&#039;
    set HERO[10] = &#039;Ekee&#039;
    set HERO[11] = &#039;Emoo&#039;
    set HERO[12] = &#039;Ewar&#039;
    set HERO[13] = &#039;Ucrl&#039;
    set HERO[14] = &#039;Udea&#039;
    set HERO[15] = &#039;Udre&#039;
    set HERO[16] = &#039;Ulic&#039;
    set HERO[17] = &#039;Nal2&#039;
    set HERO[18] = &#039;Nal3&#039;
    set HERO[19] = &#039;Nalc&#039;
    set HERO[20] = &#039;Nalm&#039;
    set HERO[21] = &#039;Nbrn&#039;
    set HERO[22] = &#039;Nbst&#039;
    set HERO[23] = &#039;Nfir&#039;
    set HERO[24] = &#039;Nngs&#039;
    set HERO[25] = &#039;Npbm&#039;
    set HERO[26] = &#039;Nplh&#039;
    set HERO[27] = &#039;Nrob&#039;
    set HERO[28] = &#039;Ntin&#039;
    set HERO[29] = &#039;Ecen&#039;
    set HERO[30] = &#039;Eevi&#039;
    set HERO[31] = &#039;Eevm&#039;
    set HERO[32] = &#039;Efur&#039;
    set HERO[33] = &#039;Eidm&#039;
    set HERO[34] = &#039;Eill&#039;
    set HERO[35] = &#039;Eilm&#039;
    set HERO[36] = &#039;Ekgg&#039;
    set HERO[37] = &#039;Emfr&#039;
    set HERO[38] = &#039;Emns&#039;
    set HERO[39] = &#039;Etyr&#039;
    set HERO[40] = &#039;Ewrd&#039;
    set HERO[41] = &#039;Hant&#039;
    set HERO[42] = &#039;Hapm&#039;
    set HERO[43] = &#039;Harf&#039;
    set HERO[44] = &#039;Hart&#039;
    set HERO[45] = &#039;Hdgo&#039;
    set HERO[46] = &#039;Hgam&#039;
    set HERO[47] = &#039;Hhkl&#039;
    set HERO[48] = &#039;Hjai&#039;
    set HERO[49] = &#039;Hkal&#039;
    set HERO[50] = &#039;Hlgr&#039;
    set HERO[51] = &#039;Hmbr&#039;
    set HERO[52] = &#039;Hmgd&#039;
    set HERO[53] = &#039;Hpb1&#039;
    set HERO[54] = &#039;Hpb2&#039;
    set HERO[55] = &#039;Huth&#039;
    set HERO[56] = &#039;Hvsh&#039;
    set HERO[57] = &#039;Hvwd&#039;
    set HERO[58] = &#039;Naka&#039;
    set HERO[59] = &#039;Nbbc&#039;
    set HERO[60] = &#039;Nkjx&#039;
    set HERO[61] = &#039;Nklj&#039;
    set HERO[62] = &#039;Nmag&#039;
    set HERO[63] = &#039;Nman&#039;
    set HERO[64] = &#039;Npld&#039;
    set HERO[65] = &#039;Nsjs&#039;
    set HERO[66] = &#039;Ocb2&#039;
    set HERO[67] = &#039;Ocbh&#039;
    set HERO[68] = &#039;Odrt&#039;
    set HERO[69] = &#039;Ogld&#039;
    set HERO[70] = &#039;Ogrh&#039;
    set HERO[71] = &#039;Opgh&#039;
    set HERO[72] = &#039;Orex&#039;
    set HERO[73] = &#039;Orkn&#039;
    set HERO[74] = &#039;Osam&#039;
    set HERO[75] = &#039;Otcc&#039;
    set HERO[76] = &#039;Othr&#039;
    set HERO[77] = &#039;Uanb&#039;
    set HERO[78] = &#039;Ubal&#039;
    set HERO[79] = &#039;Uclc&#039;
    set HERO[80] = &#039;Udth&#039;
    set HERO[81] = &#039;Uear&#039;
    set HERO[82] = &#039;Uktl&#039;
    set HERO[83] = &#039;Umal&#039;
    set HERO[84] = &#039;Usyl&#039;
    set HERO[85] = &#039;Utic&#039;
    set HERO[86] = &#039;Uvar&#039;
    set HERO[87] = &#039;Uvng&#039;
    set HERO[88] = &#039;Uwar&#039;
    set HERO_INDEX = 88
endfunction

private function Init_Ground takes nothing returns nothing
    set GROUND[0] = &#039;hfoo&#039;
    set GROUND[1] = &#039;hmil&#039;
    set GROUND[2] = &#039;hmpr&#039;
    set GROUND[3] = &#039;hmtm&#039;
    set GROUND[4] = &#039;hmtt&#039;
    set GROUND[5] = &#039;hpea&#039;
    set GROUND[6] = &#039;hpxe&#039;
    set GROUND[7] = &#039;hrif&#039;
    set GROUND[8] = &#039;hrtt&#039;
    set GROUND[9] = &#039;hspt&#039;
    set GROUND[10] = &#039;hwat&#039;
    set GROUND[11] = &#039;hwt2&#039;
    set GROUND[12] = &#039;hwt3&#039;
    set GROUND[13] = &#039;nlv1&#039;
    set GROUND[14] = &#039;nlv2&#039;
    set GROUND[15] = &#039;nlv3&#039;
    set GROUND[16] = &#039;hkni&#039;
    set GROUND[17] = &#039;hsor&#039;
    set GROUND[18] = &#039;ncat&#039;
    set GROUND[19] = &#039;nsw1&#039;
    set GROUND[20] = &#039;nsw2&#039;
    set GROUND[21] = &#039;nsw3&#039;
    set GROUND[22] = &#039;nwad&#039;
    set GROUND[23] = &#039;ocat&#039;
    set GROUND[24] = &#039;odoc&#039;
    set GROUND[25] = &#039;oeye&#039;
    set GROUND[26] = &#039;ogru&#039;
    set GROUND[27] = &#039;ohun&#039;
    set GROUND[28] = &#039;ohwd&#039;
    set GROUND[29] = &#039;opeo&#039;
    set GROUND[30] = &#039;oshm&#039;
    set GROUND[31] = &#039;osp1&#039;
    set GROUND[32] = &#039;osp2&#039;
    set GROUND[33] = &#039;osp3&#039;
    set GROUND[34] = &#039;osp4&#039;
    set GROUND[35] = &#039;ospm&#039;
    set GROUND[36] = &#039;ospw&#039;
    set GROUND[37] = &#039;osw1&#039;
    set GROUND[38] = &#039;osw2&#039;
    set GROUND[39] = &#039;osw3&#039;
    set GROUND[40] = &#039;otau&#039;
    set GROUND[41] = &#039;otbk&#039;
    set GROUND[42] = &#039;otot&#039;
    set GROUND[43] = &#039;okod&#039;
    set GROUND[44] = &#039;orai&#039;
    set GROUND[45] = &#039;earc&#039;
    set GROUND[46] = &#039;ebal&#039;
    set GROUND[47] = &#039;edcm&#039;
    set GROUND[48] = &#039;edoc&#039;
    set GROUND[49] = &#039;edot&#039;
    set GROUND[50] = &#039;edry&#039;
    set GROUND[51] = &#039;efon&#039;
    set GROUND[52] = &#039;emtg&#039;
    set GROUND[53] = &#039;esen&#039;
    set GROUND[54] = &#039;espv&#039;
    set GROUND[55] = &#039;even&#039;
    set GROUND[56] = &#039;ewsp&#039;
    set GROUND[57] = &#039;uabo&#039;
    set GROUND[58] = &#039;uaco&#039;
    set GROUND[59] = &#039;ucrm&#039;
    set GROUND[60] = &#039;ucry&#039;
    set GROUND[61] = &#039;ucs1&#039;
    set GROUND[62] = &#039;ucs2&#039;
    set GROUND[63] = &#039;ucs3&#039;
    set GROUND[64] = &#039;ucsB&#039;
    set GROUND[65] = &#039;ucsC&#039;
    set GROUND[66] = &#039;ugho&#039;
    set GROUND[67] = &#039;ugrm&#039;
    set GROUND[68] = &#039;umtw&#039;
    set GROUND[69] = &#039;unec&#039;
    set GROUND[70] = &#039;uobs&#039;
    set GROUND[71] = &#039;uske&#039;
    set GROUND[72] = &#039;uskm&#039;
    set GROUND[73] = &#039;uban&#039;
    set GROUND[74] = &#039;ushd&#039;
    set GROUND[75] = &#039;nanb&#039;
    set GROUND[76] = &#039;nanc&#039;
    set GROUND[77] = &#039;nane&#039;
    set GROUND[78] = &#039;nanm&#039;
    set GROUND[79] = &#039;nano&#039;
    set GROUND[80] = &#039;nanw&#039;
    set GROUND[81] = &#039;narg&#039;
    set GROUND[82] = &#039;nass&#039;
    set GROUND[83] = &#039;nba2&#039;
    set GROUND[84] = &#039;nbal&#039;
    set GROUND[85] = &#039;nban&#039;
    set GROUND[86] = &#039;nbda&#039;
    set GROUND[87] = &#039;nbdm&#039;
    set GROUND[88] = &#039;nbdo&#039;
    set GROUND[89] = &#039;nbds&#039;
    set GROUND[90] = &#039;nbdw&#039;
    set GROUND[91] = &#039;nbnb&#039;
    set GROUND[92] = &#039;nbrg&#039;
    set GROUND[93] = &#039;ncea&#039;
    set GROUND[94] = &#039;ncen&#039;
    set GROUND[95] = &#039;ncer&#039;
    set GROUND[96] = &#039;nchp&#039;
    set GROUND[97] = &#039;ncim&#039;
    set GROUND[98] = &#039;ncks&#039;
    set GROUND[99] = &#039;ncnk&#039;
    set GROUND[100] = &#039;ndqn&#039;
    set GROUND[101] = &#039;ndqp&#039;
    set GROUND[102] = &#039;ndqs&#039;
    set GROUND[103] = &#039;ndqt&#039;
    set GROUND[104] = &#039;ndqv&#039;
    set GROUND[105] = &#039;ndtb&#039;
    set GROUND[106] = &#039;ndth&#039;
    set GROUND[107] = &#039;ndtp&#039;
    set GROUND[108] = &#039;ndtr&#039;
    set GROUND[109] = &#039;ndtt&#039;
    set GROUND[110] = &#039;ndtw&#039;
    set GROUND[111] = &#039;nelb&#039;
    set GROUND[112] = &#039;nele&#039;
    set GROUND[113] = &#039;nenc&#039;
    set GROUND[114] = &#039;nenf&#039;
    set GROUND[115] = &#039;nenp&#039;
    set GROUND[116] = &#039;nepl&#039;
    set GROUND[117] = &#039;nerd&#039;
    set GROUND[118] = &#039;ners&#039;
    set GROUND[119] = &#039;nerw&#039;
    set GROUND[120] = &#039;nfel&#039;
    set GROUND[121] = &#039;nfgb&#039;
    set GROUND[122] = &#039;nfgt&#039;
    set GROUND[123] = &#039;nfgu&#039;
    set GROUND[124] = &#039;nfod&#039;
    set GROUND[125] = &#039;nfor&#039;
    set GROUND[126] = &#039;nfot&#039;
    set GROUND[127] = &#039;nfov&#039;
    set GROUND[128] = &#039;nfpc&#039;
    set GROUND[129] = &#039;nfpe&#039;
    set GROUND[130] = &#039;nfpl&#039;
    set GROUND[131] = &#039;nfps&#039;
    set GROUND[132] = &#039;nfpt&#039;
    set GROUND[133] = &#039;nfpu&#039;
    set GROUND[134] = &#039;nfra&#039;
    set GROUND[135] = &#039;nfrb&#039;
    set GROUND[136] = &#039;nfre&#039;
    set GROUND[137] = &#039;nfrg&#039;
    set GROUND[138] = &#039;nfrl&#039;
    set GROUND[139] = &#039;nfrp&#039;
    set GROUND[140] = &#039;nfrs&#039;
    set GROUND[141] = &#039;nfsh&#039;
    set GROUND[142] = &#039;nfsp&#039;
    set GROUND[143] = &#039;nftb&#039;
    set GROUND[144] = &#039;nftk&#039;
    set GROUND[145] = &#039;nftr&#039;
    set GROUND[146] = &#039;nftt&#039;
    set GROUND[147] = &#039;nggr&#039;
    set GROUND[148] = &#039;ngir&#039;
    set GROUND[149] = &#039;nglm&#039;
    set GROUND[150] = &#039;ngna&#039;
    set GROUND[151] = &#039;ngnb&#039;
    set GROUND[152] = &#039;ngno&#039;
    set GROUND[153] = &#039;ngns&#039;
    set GROUND[154] = &#039;ngnv&#039;
    set GROUND[155] = &#039;ngnw&#039;
    set GROUND[156] = &#039;ngrk&#039;
    set GROUND[157] = &#039;ngsp&#039;
    set GROUND[158] = &#039;ngst&#039;
    set GROUND[159] = &#039;ngza&#039;
    set GROUND[160] = &#039;nhdc&#039;
    set GROUND[161] = &#039;nhfp&#039;
    set GROUND[162] = &#039;nhhr&#039;
    set GROUND[163] = &#039;nhym&#039;
    set GROUND[164] = &#039;nina&#039;
    set GROUND[165] = &#039;ninc&#039;
    set GROUND[166] = &#039;ninf&#039;
    set GROUND[167] = &#039;ninm&#039;
    set GROUND[168] = &#039;nith&#039;
    set GROUND[169] = &#039;nitp&#039;
    set GROUND[170] = &#039;nitr&#039;
    set GROUND[171] = &#039;nits&#039;
    set GROUND[172] = &#039;nitt&#039;
    set GROUND[173] = &#039;nitw&#039;
    set GROUND[174] = &#039;njg1&#039;
    set GROUND[175] = &#039;njga&#039;
    set GROUND[176] = &#039;njgb&#039;
    set GROUND[177] = &#039;nkob&#039;
    set GROUND[178] = &#039;nkog&#039;
    set GROUND[179] = &#039;nkol&#039;
    set GROUND[180] = &#039;nkot&#039;
    set GROUND[181] = &#039;nlds&#039;
    set GROUND[182] = &#039;nlkl&#039;
    set GROUND[183] = &#039;nlpd&#039;
    set GROUND[184] = &#039;nlpr&#039;
    set GROUND[185] = &#039;nlps&#039;
    set GROUND[186] = &#039;nlsn&#039;
    set GROUND[187] = &#039;nltc&#039;
    set GROUND[188] = &#039;nltl&#039;
    set GROUND[189] = &#039;nlur&#039;
    set GROUND[190] = &#039;nmam&#039;
    set GROUND[191] = &#039;nmdr&#039;
    set GROUND[192] = &#039;nmfs&#039;
    set GROUND[193] = &#039;nmgd&#039;
    set GROUND[194] = &#039;nmgr&#039;
    set GROUND[195] = &#039;nmgw&#039;
    set GROUND[196] = &#039;nmit&#039;
    set GROUND[197] = &#039;nmmu&#039;
    set GROUND[198] = &#039;nmpg&#039;
    set GROUND[199] = &#039;nmrl&#039;
    set GROUND[200] = &#039;nmrm&#039;
    set GROUND[201] = &#039;nmrr&#039;
    set GROUND[202] = &#039;nnwa&#039;
    set GROUND[203] = &#039;nnwl&#039;
    set GROUND[204] = &#039;nnwq&#039;
    set GROUND[205] = &#039;nnwr&#039;
    set GROUND[206] = &#039;nnws&#039;
    set GROUND[207] = &#039;noga&#039;
    set GROUND[208] = &#039;nogl&#039;
    set GROUND[209] = &#039;nogm&#039;
    set GROUND[210] = &#039;nogn&#039;
    set GROUND[211] = &#039;nogo&#039;
    set GROUND[212] = &#039;nogr&#039;
    set GROUND[213] = &#039;nomg&#039;
    set GROUND[214] = &#039;nowb&#039;
    set GROUND[215] = &#039;nowe&#039;
    set GROUND[216] = &#039;nowk&#039;
    set GROUND[217] = &#039;npfl&#039;
    set GROUND[218] = &#039;npfm&#039;
    set GROUND[219] = &#039;nqbh&#039;
    set GROUND[220] = &#039;nrog&#039;
    set GROUND[221] = &#039;nrzb&#039;
    set GROUND[222] = &#039;nrzg&#039;
    set GROUND[223] = &#039;nrzm&#039;
    set GROUND[224] = &#039;nrzs&#039;
    set GROUND[225] = &#039;nrzt&#039;
    set GROUND[226] = &#039;nsat&#039;
    set GROUND[227] = &#039;nsbm&#039;
    set GROUND[228] = &#039;nsc2&#039;
    set GROUND[229] = &#039;nsc3&#039;
    set GROUND[230] = &#039;nsca&#039;
    set GROUND[231] = &#039;nscb&#039;
    set GROUND[232] = &#039;nsce&#039;
    set GROUND[233] = &#039;nsgg&#039;
    set GROUND[234] = &#039;nsgt&#039;
    set GROUND[235] = &#039;nska&#039;
    set GROUND[236] = &#039;nske&#039;
    set GROUND[237] = &#039;nskf&#039;
    set GROUND[238] = &#039;nskg&#039;
    set GROUND[239] = &#039;nskm&#039;
    set GROUND[240] = &#039;nsko&#039;
    set GROUND[241] = &#039;nslf&#039;
    set GROUND[242] = &#039;nslh&#039;
    set GROUND[243] = &#039;nsll&#039;
    set GROUND[244] = &#039;nslm&#039;
    set GROUND[245] = &#039;nsln&#039;
    set GROUND[246] = &#039;nslr&#039;
    set GROUND[247] = &#039;nslv&#039;
    set GROUND[248] = &#039;nsoc&#039;
    set GROUND[249] = &#039;nsog&#039;
    set GROUND[250] = &#039;nspb&#039;
    set GROUND[251] = &#039;nspd&#039;
    set GROUND[252] = &#039;nspg&#039;
    set GROUND[253] = &#039;nspp&#039;
    set GROUND[254] = &#039;nspr&#039;
    set GROUND[255] = &#039;nsqa&#039;
    set GROUND[256] = &#039;nsqe&#039;
    set GROUND[257] = &#039;nsqo&#039;
    set GROUND[258] = &#039;nsqt&#039;
    set GROUND[259] = &#039;nsra&#039;
    set GROUND[260] = &#039;nsrh&#039;
    set GROUND[261] = &#039;nsrn&#039;
    set GROUND[262] = &#039;nsrw&#039;
    set GROUND[263] = &#039;nssp&#039;
    set GROUND[264] = &#039;nsth&#039;
    set GROUND[265] = &#039;nstl&#039;
    set GROUND[266] = &#039;nsts&#039;
    set GROUND[267] = &#039;nstw&#039;
    set GROUND[268] = &#039;nsty&#039;
    set GROUND[269] = &#039;nthl&#039;
    set GROUND[270] = &#039;ntka&#039;
    set GROUND[271] = &#039;ntkc&#039;
    set GROUND[272] = &#039;ntkf&#039;
    set GROUND[273] = &#039;ntkh&#039;
    set GROUND[274] = &#039;ntks&#039;
    set GROUND[275] = &#039;ntkt&#039;
    set GROUND[276] = &#039;ntkw&#039;
    set GROUND[277] = &#039;ntor&#039;
    set GROUND[278] = &#039;nubk&#039;
    set GROUND[279] = &#039;nubr&#039;
    set GROUND[280] = &#039;nubw&#039;
    set GROUND[281] = &#039;nwen&#039;
    set GROUND[282] = &#039;nwiz&#039;
    set GROUND[283] = &#039;nwld&#039;
    set GROUND[284] = &#039;nwlg&#039;
    set GROUND[285] = &#039;nwlt&#039;
    set GROUND[286] = &#039;nwna&#039;
    set GROUND[287] = &#039;nwnr&#039;
    set GROUND[288] = &#039;nwns&#039;
    set GROUND[289] = &#039;nwrg&#039;
    set GROUND[290] = &#039;nwwd&#039;
    set GROUND[291] = &#039;nwwf&#039;
    set GROUND[292] = &#039;nwwg&#039;
    set GROUND[293] = &#039;nwzd&#039;
    set GROUND[294] = &#039;nwzg&#039;
    set GROUND[295] = &#039;nwzr&#039;
    set GROUND[296] = &#039;nzom&#039;
    set GROUND[297] = &#039;nbld&#039;
    set GROUND[298] = &#039;nahy&#039;
    set GROUND[299] = &#039;ncfs&#039;
    set GROUND[300] = &#039;nehy&#039;
    set GROUND[301] = &#039;nhyc&#039;
    set GROUND[302] = &#039;nhyd&#039;
    set GROUND[303] = &#039;nhyh&#039;
    set GROUND[304] = &#039;nmbg&#039;
    set GROUND[305] = &#039;nmcf&#039;
    set GROUND[306] = &#039;nmrv&#039;
    set GROUND[307] = &#039;nmsc&#039;
    set GROUND[308] = &#039;nmsn&#039;
    set GROUND[309] = &#039;nmtw&#039;
    set GROUND[310] = &#039;nmyr&#039;
    set GROUND[311] = &#039;nmys&#039;
    set GROUND[312] = &#039;nnmg&#039;
    set GROUND[313] = &#039;nnrg&#039;
    set GROUND[314] = &#039;nnrs&#039;
    set GROUND[315] = &#039;nnsu&#039;
    set GROUND[316] = &#039;nnsw&#039;
    set GROUND[317] = &#039;nplb&#039;
    set GROUND[318] = &#039;nplg&#039;
    set GROUND[319] = &#039;nrel&#039;
    set GROUND[320] = &#039;nsbs&#039;
    set GROUND[321] = &#039;nsel&#039;
    set GROUND[322] = &#039;nsgb&#039;
    set GROUND[323] = &#039;nsgh&#039;
    set GROUND[324] = &#039;nsgn&#039;
    set GROUND[325] = &#039;nsnp&#039;
    set GROUND[326] = &#039;nsns&#039;
    set GROUND[327] = &#039;ntrd&#039;
    set GROUND[328] = &#039;ntrg&#039;
    set GROUND[329] = &#039;ntrh&#039;
    set GROUND[330] = &#039;ntrs&#039;
    set GROUND[331] = &#039;ntrt&#039;
    set GROUND[332] = &#039;ntws&#039;
    set GROUND[333] = &#039;ndrv&#039;
    set GROUND[334] = &#039;ngh1&#039;
    set GROUND[335] = &#039;ngh2&#039;
    set GROUND[336] = &#039;nlrv&#039;
    set GROUND[337] = &#039;nrvd&#039;
    set GROUND[338] = &#039;nrvf&#039;
    set GROUND[339] = &#039;nrvi&#039;
    set GROUND[340] = &#039;nrvl&#039;
    set GROUND[341] = &#039;nrvs&#039;
    set GROUND[342] = &#039;nsrv&#039;
    set GROUND[343] = &#039;ntrv&#039;
    set GROUND[344] = &#039;nvde&#039;
    set GROUND[345] = &#039;nvdg&#039;
    set GROUND[346] = &#039;nvdl&#039;
    set GROUND[347] = &#039;nvdw&#039;
    set GROUND[348] = &#039;ncrb&#039;
    set GROUND[349] = &#039;nder&#039;
    set GROUND[350] = &#039;ndog&#039;
    set GROUND[351] = &#039;ndwm&#039;
    set GROUND[352] = &#039;nech&#039;
    set GROUND[353] = &#039;necr&#039;
    set GROUND[354] = &#039;nfbr&#039;
    set GROUND[355] = &#039;nfro&#039;
    set GROUND[356] = &#039;nhmc&#039;
    set GROUND[357] = &#039;npig&#039;
    set GROUND[358] = &#039;nrac&#039;
    set GROUND[359] = &#039;nrat&#039;
    set GROUND[360] = &#039;nsea&#039;
    set GROUND[361] = &#039;nshe&#039;
    set GROUND[362] = &#039;nskk&#039;
    set GROUND[363] = &#039;nvil&#039;
    set GROUND[364] = &#039;nvk2&#039;
    set GROUND[365] = &#039;nvl2&#039;
    set GROUND[366] = &#039;nvlk&#039;
    set GROUND[367] = &#039;nvlw&#039;
    set GROUND[368] = &#039;npng&#039;
    set GROUND[369] = &#039;nsha&#039;
    set GROUND[370] = &#039;eilw&#039;
    set GROUND[371] = &#039;enec&#039;
    set GROUND[372] = &#039;ensh&#039;
    set GROUND[373] = &#039;eshd&#039;
    set GROUND[374] = &#039;hbew&#039;
    set GROUND[375] = &#039;hcth&#039;
    set GROUND[376] = &#039;hhes&#039;
    set GROUND[377] = &#039;hprt&#039;
    set GROUND[378] = &#039;nbee&#039;
    set GROUND[379] = &#039;nbel&#039;
    set GROUND[380] = &#039;nbsp&#039;
    set GROUND[381] = &#039;nchg&#039;
    set GROUND[382] = &#039;nchw&#039;
    set GROUND[383] = &#039;ncpn&#039;
    set GROUND[384] = &#039;ndmu&#039;
    set GROUND[385] = &#039;ndrd&#039;
    set GROUND[386] = &#039;ndrf&#039;
    set GROUND[387] = &#039;ndrh&#039;
    set GROUND[388] = &#039;ndrj&#039;
    set GROUND[389] = &#039;ndrl&#039;
    set GROUND[390] = &#039;ndrm&#039;
    set GROUND[391] = &#039;ndrn&#039;
    set GROUND[392] = &#039;ndrp&#039;
    set GROUND[393] = &#039;ndrs&#039;
    set GROUND[394] = &#039;ndrt&#039;
    set GROUND[395] = &#039;ndrw&#039;
    set GROUND[396] = &#039;ndsa&#039;
    set GROUND[397] = &#039;negz&#039;
    set GROUND[398] = &#039;nemi&#039;
    set GROUND[399] = &#039;nfgl&#039;
    set GROUND[400] = &#039;ngbl&#039;
    set GROUND[401] = &#039;nhea&#039;
    set GROUND[402] = &#039;nhef&#039;
    set GROUND[403] = &#039;nhem&#039;
    set GROUND[404] = &#039;nhew&#039;
    set GROUND[405] = &#039;njks&#039;
    set GROUND[406] = &#039;nmed&#039;
    set GROUND[407] = &#039;nmsh&#039;
    set GROUND[408] = &#039;nspc&#039;
    set GROUND[409] = &#039;nssn&#039;
    set GROUND[410] = &#039;nw2w&#039;
    set GROUND[411] = &#039;nwat&#039;
    set GROUND[412] = &#039;nzlc&#039;
    set GROUND[413] = &#039;odkt&#039;
    set GROUND[414] = &#039;ogrk&#039;
    set GROUND[415] = &#039;omtg&#039;
    set GROUND[416] = &#039;oosc&#039;
    set GROUND[417] = &#039;ovlj&#039;
    set GROUND[418] = &#039;owar&#039;
    set GROUND[419] = &#039;uabc&#039;
    set GROUND[420] = &#039;uktg&#039;
    set GROUND[421] = &#039;uktn&#039;
    set GROUND[422] = &#039;hhdl&#039;
    set GROUND[423] = &#039;hrdh&#039;
    set GROUND[424] = &#039;nchr&#039;
    set GROUND[425] = &#039;nckb&#039;
    set GROUND[426] = &#039;onzg&#039;
    set GROUND[427] = &#039;nmpe&#039;
    set GROUND[428] = &#039;uswb&#039;
    set GROUND_INDEX = 428
endfunction
    
private function Init_Air takes nothing returns nothing
    set AIR[0] = &#039;hdhw&#039;
    set AIR[1] = &#039;hgry&#039;
    set AIR[2] = &#039;hgyr&#039;
    set AIR[3] = &#039;hphx&#039;
    set AIR[4] = &#039;otbr&#039;
    set AIR[5] = &#039;owyv&#039;
    set AIR[6] = &#039;echm&#039;
    set AIR[7] = &#039;edtm&#039;
    set AIR[8] = &#039;efdr&#039;
    set AIR[9] = &#039;ehip&#039;
    set AIR[10] = &#039;ehpr&#039;
    set AIR[11] = &#039;ubsp&#039;
    set AIR[12] = &#039;ufro&#039;
    set AIR[13] = &#039;ugar&#039;
    set AIR[14] = &#039;uloc&#039;
    set AIR[15] = &#039;nadk&#039;
    set AIR[16] = &#039;nadr&#039;
    set AIR[17] = &#039;nadw&#039;
    set AIR[18] = &#039;nbdk&#039;
    set AIR[19] = &#039;nbdr&#039;
    set AIR[20] = &#039;nbwm&#039;
    set AIR[21] = &#039;nbzd&#039;
    set AIR[22] = &#039;nbzk&#039;
    set AIR[23] = &#039;nbzw&#039;
    set AIR[24] = &#039;ngdk&#039;
    set AIR[25] = &#039;ngrd&#039;
    set AIR[26] = &#039;ngrw&#039;
    set AIR[27] = &#039;nhar&#039;
    set AIR[28] = &#039;nhrh&#039;
    set AIR[29] = &#039;nhrq&#039;
    set AIR[30] = &#039;nhrr&#039;
    set AIR[31] = &#039;nhrw&#039;
    set AIR[32] = &#039;nndk&#039;
    set AIR[33] = &#039;nndr&#039;
    set AIR[34] = &#039;nnht&#039;
    set AIR[35] = &#039;nrdk&#039;
    set AIR[36] = &#039;nrdr&#039;
    set AIR[37] = &#039;nrwm&#039;
    set AIR[38] = &#039;nwgs&#039;
    set AIR[39] = &#039;nws1&#039;
    set AIR[40] = &#039;nzep&#039;
    set AIR[41] = &#039;nalb&#039;
    set AIR[42] = &#039;now2&#039;
    set AIR[43] = &#039;now3&#039;
    set AIR[44] = &#039;nowl&#039;
    set AIR[45] = &#039;nshf&#039;
    set AIR[46] = &#039;nsno&#039;
    set AIR[47] = &#039;nvul&#039;
    set AIR[48] = &#039;nmdm&#039;
    set AIR[49] = &#039;nser&#039;
    set AIR[50] = &#039;nthr&#039;
    set AIR[51] = &#039;oswy&#039;
    set AIR[52] = &#039;ownr&#039;
    set AIR[53] = &#039;uarb&#039;
    set AIR[54] = &#039;ubdd&#039;
    set AIR[55] = &#039;ubdr&#039;
    set AIR[56] = &#039;nwe1&#039;
    set AIR[57] = &#039;nwe2&#039;
    set AIR[58] = &#039;nwe3&#039;
    set AIR[59] = &#039;zjug&#039;
    set AIR_INDEX = 59
endfunction

private function Display_Index takes nothing returns nothing
    call BJDebugMsg(&quot;|cffffd700GetRandomId() Index:|r &quot;+I2S(LAST_INDEX))
endfunction

private function Reset_Array takes nothing returns nothing
    if HERO_INDEX &lt; 0 then
        call Init_Heroes()
    elseif GROUND_INDEX &lt; 0 then
        call Init_Ground()
    elseif AIR_INDEX &lt; 0 then
        call Init_Air()
    endif
endfunction

private function Init takes nothing returns nothing
    local trigger trig 
    
    call Init_Heroes()
    call Init_Ground()
    call Init_Air()
    if bj_isSinglePlayer then
        set trig = CreateTrigger()
        call TriggerRegisterPlayerChatEvent(trig,Player(0),&quot;-getlastindex&quot;,true)
        call TriggerAddAction(trig,function Display_Index)
    endif
    if RESET_ARRAYS then
        set trig = CreateTrigger()
        call TriggerRegisterVariableEvent(trig,SCOPE_PRIVATE+&quot;HERO_INDEX&quot;,LESS_THAN,0)
        call TriggerRegisterVariableEvent(trig,SCOPE_PRIVATE+&quot;GROUND_INDEX&quot;,LESS_THAN,0)
        call TriggerRegisterVariableEvent(trig,SCOPE_PRIVATE+&quot;AIR_INDEX&quot;,LESS_THAN,0)
        call TriggerAddAction(trig,function Reset_Array)
    endif
endfunction

private function CheckAbilities takes integer id returns nothing
    local unit u = CreateUnit(Player(15),id,0,0,0)
    local boolean b = false
    
    if IsUnitInvisible(u,Player(0)) == true then
        set b = true
        call BJDebugMsg(WARNING+&quot;Unit type &quot;+UnitId2String(id)+&quot; is invisible.&quot;)
        if GetUnitAbilityLevel(u,&#039;Agho&#039;) &gt; 0 then
            call BJDebugMsg(FIX+&quot;Remove ability &#039;Agho&#039; from created units.&quot;)
        elseif GetUnitAbilityLevel(u,&#039;Ashm&#039;) &gt; 0 then
            call BJDebugMsg(FIX+&quot;Remove ability &#039;Ashm&#039; from created units.&quot;)
        elseif GetUnitAbilityLevel(u,&#039;Ahid&#039;) &gt; 0 then
            call BJDebugMsg(FIX+&quot;Remove ability &#039;Ahid&#039; from created units.&quot;)
        elseif GetUnitAbilityLevel(u,&#039;Sshm&#039;) &gt; 0 then
            call BJDebugMsg(FIX+&quot;Remove ability &#039;Sshm&#039; from created units.&quot;)
        endif
    endif
    if GetUnitAbilityLevel(u,&#039;Avul&#039;) &gt; 0 then
        set b = true
        call BJDebugMsg(WARNING+&quot;Unit type &quot;+UnitId2String(id)+&quot; is invulnerable.&quot;)
        call BJDebugMsg(FIX+&quot;Remove ability &#039;Avul&#039; from created units.&quot;)
    endif
    if GetUnitAbilityLevel(u,&#039;Aloc&#039;) &gt; 0 then
        set b = true
        call BJDebugMsg(WARNING+&quot;Unit type &quot;+UnitId2String(id)+&quot; is locusted.&quot;)
        call BJDebugMsg(FIX+&quot;Remove ability &#039;Aloc&#039; from created units.&quot;)
    endif
    if IssuePointOrder(u,&quot;move&quot;,0,0) == false then
        set b = true
        call BJDebugMsg(WARNING+&quot;Unit type &quot;+UnitId2String(id)+&quot; has no movement.&quot;)
    endif
    if b then
        if IsUnitType(u,UNIT_TYPE_HERO) == true then
            call BJDebugMsg(FIX+&quot;Alternatively set HERO[&quot;+I2S(LAST_INDEX)+&quot;] = NO_ID.&quot;)
        elseif IsUnitType(u,UNIT_TYPE_FLYING) == true then
            call BJDebugMsg(FIX+&quot;Alternatively set AIR[&quot;+I2S(LAST_INDEX)+&quot;] = NO_ID.&quot;)
        elseif IsUnitType(u,UNIT_TYPE_GROUND) == true then
            call BJDebugMsg(FIX+&quot;Alternatively set GROUND[&quot;+I2S(LAST_INDEX)+&quot;] = NO_ID.&quot;)
        endif
    endif
    call RemoveUnit(u)
    set u = null
endfunction

function GetRandomId takes unittype whichtype, boolean remove returns integer
    local integer id = NO_ID

    loop
        exitwhen id != NO_ID
        if GROUND_INDEX &gt;= 0 and (whichtype == UNIT_TYPE_GROUND or (whichtype == UNIT_TYPE_RANDOM and GetRandomInt(1,3) == 1)) then
            set LAST_INDEX = GetRandomInt(0,GROUND_INDEX)
            set id = GROUND[LAST_INDEX]
            if remove or id == NO_ID then
                set GROUND[LAST_INDEX] = GROUND[GROUND_INDEX]
                set GROUND_INDEX = GROUND_INDEX-1
            endif
        elseif AIR_INDEX &gt;= 0 and (whichtype == UNIT_TYPE_FLYING or (whichtype == UNIT_TYPE_RANDOM and GetRandomInt(1,2) == 1)) then
            set LAST_INDEX = GetRandomInt(0,AIR_INDEX)
            set id = AIR[LAST_INDEX]
            if remove or id == NO_ID then
                set AIR[LAST_INDEX] = AIR[AIR_INDEX]
                set AIR_INDEX = AIR_INDEX-1
            endif
        elseif HERO_INDEX &gt;= 0 and (whichtype == UNIT_TYPE_HERO or whichtype == UNIT_TYPE_RANDOM) then
            set LAST_INDEX = GetRandomInt(0,HERO_INDEX)
            set id = HERO[LAST_INDEX]
            if remove or id == NO_ID then
                set HERO[LAST_INDEX] = HERO[HERO_INDEX]
                set HERO_INDEX = HERO_INDEX-1
            endif
        else
            debug if HERO_INDEX &lt; 0 then
                debug call BJDebugMsg(ERROR+&quot;UNIT_TYPE_HERO array is empty.&quot;)
                debug call BJDebugMsg(FIX+&quot;Set RESET_ARRAYS = true.&quot;)
            debug elseif AIR_INDEX &lt; 0 then
                debug call BJDebugMsg(ERROR+&quot;UNIT_TYPE_FLYING array is empty.&quot;)
                debug call BJDebugMsg(FIX+&quot;Set RESET_ARRAYS = true.&quot;)
            debug elseif GROUND_INDEX &lt; 0 then
                debug call BJDebugMsg(ERROR+&quot;UNIT_TYPE_GROUND array is empty.&quot;)
                debug call BJDebugMsg(FIX+&quot;Set RESET_ARRAYS = true.&quot;)
            debug else
                debug call BJDebugMsg(ERROR+&quot;Unrecognized unit type passed to function.&quot;)
                debug call BJDebugMsg(FIX+&quot;Use UNIT_TYPE_GROUND, UNIT_TYPE_FLYING, UNIT_TYPE_HERO, or UNIT_TYPE_RANDOM.&quot;)
            debug endif
            return NO_ID
        endif
    endloop
    debug if CHECK_ABILITIES then
        debug call CheckAbilities(id)
    debug endif
    return id
endfunction
endlibrary


Usage:
JASS:
function ...
    local unit u_flying = CreateUnit(Player(0),GetRandomId(UNIT_TYPE_FLYING,true),0,0,0)
    local unit u_ground = CreateUnit(Player(0),GetRandomId(UNIT_TYPE_GROUND,true),0,0,0)
    local unit u_hero = CreateUnit(Player(0),GetRandomId(UNIT_TYPE_HERO,true),0,0,0)
    local unit u_random = CreateUnit(Player(0),GetRandomId(UNIT_TYPE_RANDOM,true),0,0,0)
    ...
endfunction
Also have a look at the example that comes with the map.
 

Attachments

  • GetRandomId.w3x
    36 KB · Views: 315

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
This is a nice snippet that could be used for an All-random purpose, but how long did it take to type that up. o_O
 

D.V.D

Make a wish
Reaction score
73
That's a lot of of ID's. So I think you should add a tutorial if people want to add their own ID's.
 

UndeadDragon

Super Moderator
Reaction score
447
That's a lot of of ID's. So I think you should add a tutorial if people want to add their own ID's.

It's not really worth a tutorial how simple it is...
 

Romek

Super Moderator
Reaction score
963
This could indeed be very useful for many maps. CheckAbilities() is a useful addition. :D

Approved.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top