1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996 |
- /*
- * Seven Kingdoms: Ancient Adversaries
- *
- * Copyright 1997,1998 Enlight Software Ltd.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- //Filename : OUNITAAT.CPP
- //Description : Object UnitArray - part 2
- #include <ALL.h>
- #include <OVGA.h>
- #include <OSYS.h>
- #include <OSTR.h>
- #include <OREMOTE.h>
- #include <OWORLD.h>
- #include <ONATION.h>
- #include <OFIRM.h>
- #include <OTOWN.h>
- #include <OTERRAIN.h>
- #include <OU_MARI.h>
- #ifdef NO_DEBUG_UNIT
- #undef err_when
- #undef err_here
- #undef err_if
- #undef err_else
- #undef err_now
- #define err_when(cond)
- #define err_here()
- #define err_if(cond)
- #define err_else
- #define err_now(msg)
- #undef DEBUG
- #endif
- //------ static variables for attacking ------//
- // ATTACK_DIR is defined in OUNIT.H
- #define MAX_TARGET_SIZE 4
- #define MAX_UNIT_SURROUND_SIZE MAX_TARGET_SIZE+2 // handle the case up to target size 4x4
- #define SHIFT_ADJUST 1 // since the upper left offset position is (-1, -1)
- static short *unit_processed_array; // store those processed unit sprite_recno
- static short unit_processed_count; // count the number processed units
- static short *dir_array_ptr[ATTACK_DIR]; // store units' sprite_recno in each direction
- static short dir_array_count[ATTACK_DIR]; // num of unit in each direction
- static char unreachable_table[MAX_UNIT_SURROUND_SIZE][MAX_UNIT_SURROUND_SIZE]; // table shared for all attackers
- //************* debug ***************//
- #ifdef DEBUG
- //--------------------------------------------------------------------------------//
- // <int> resultNum - the num of free space around the target
- // <int> width - width of target
- // <int> height - height of target
- //--------------------------------------------------------------------------------//
- static void debug_analyse_result_check(int resultNum, int width, int height)
- {
- int count=0, i, j;
- for(i=0; i<width+2; i++)
- {
- if(unreachable_table[i][0]==0)
- count++;
- }
- i = width+1;
- for(j=1; j<height+2; j++)
- {
- if(unreachable_table[i][j]==0)
- count++;
- }
- for(j=1; j<height+2; j++)
- {
- if(unreachable_table[0][j]==0)
- count++;
- }
- j = height+1;
- for(i=1; i<width+1; i++)
- {
- if(unreachable_table[i][j]==0)
- count++;
- }
- err_when(count!=resultNum);
- }
- #endif
- #ifdef DEBUG
- #define debug_result_check(resultNum, width, height) debug_analyse_result_check(resultNum, width, height)
- #else
- #define debug_result_check(resultNum, width, height)
- #endif
- //************* debug ***************//
- //--------- Begin of function UnitArray::get_target_surround_loc ---------//
- // <int> targetWidth - target width
- // <int> targetHeight - target height
- //
- char UnitArray::get_target_surround_loc(int targetWidth, int targetHeight)
- {
- static char surround_loc[MAX_TARGET_SIZE][MAX_TARGET_SIZE] // width, height
- = { { 8, 10, 12, 14}, {10, 12, 14, 16}, {12, 14, 16, 18}, {14, 16, 18, 20}};
- return surround_loc[targetWidth-1][targetHeight-1];
- }
- //----------- End of function UnitArray::get_target_surround_loc -----------//
- //--------- Begin of function UnitArray::update_unreachable_table ---------//
- // check target surroundig to drop those unreachable location
- //
- // <int> targetXLoc - target x location
- // <int> targetYLoc - target y location
- // <int> targetWidth - target width
- // <int> targetHeight - target height
- // <char> mobileType - mobile type of the attacker
- // <int&> analyseResult - reference for returning num of reachable location
- //
- void UnitArray::update_unreachable_table(int targetXLoc, int targetYLoc, int targetWidth, int targetHeight,
- char mobileType, int &analyseResult)
- {
- int xLoc1, yLoc1, xLoc2, yLoc2, i;
- //----------------------------------------------------------------------//
- // checking for left & right edges, calculate location and counter
- //----------------------------------------------------------------------//
- xLoc1 = targetXLoc-1;
- xLoc2 = targetXLoc+targetWidth;
-
- if(targetYLoc==0)
- {
- i = targetHeight+1;
- yLoc1=targetYLoc;
- }
- else
- {
- i = targetHeight+2;
- yLoc1=targetYLoc-1;
- }
- if(targetYLoc+targetHeight>=MAX_WORLD_Y_LOC)
- i--;
- for(yLoc2=yLoc1-targetYLoc+SHIFT_ADJUST; i>0; i--, yLoc1++, yLoc2++)
- {
- //---- left edge ----//
- if(xLoc1>=0 && !unreachable_table[0][yLoc2] && !world.get_loc(xLoc1, yLoc1)->can_move(mobileType))
- {
- unreachable_table[0][yLoc2] = 1;
- analyseResult--;
- }
- //----- right edge -----//
- if(xLoc2<MAX_WORLD_X_LOC && !unreachable_table[targetWidth+1][yLoc2] &&
- !world.get_loc(xLoc2, yLoc1)->can_move(mobileType))
- {
- unreachable_table[targetWidth+1][yLoc2] = 1;
- analyseResult--;
- }
- if(!analyseResult)
- return;
- }
- //----------------------------------------------------------------------//
- // checking for the top and bottom edges
- //----------------------------------------------------------------------//
- yLoc1 = targetYLoc-1;
- yLoc2 = targetYLoc+targetHeight;
- for(i=0, xLoc1=targetXLoc, xLoc2=xLoc1-targetXLoc+SHIFT_ADJUST; i<targetWidth; i++, xLoc1++, xLoc2++)
- {
- //---- top edge ----//
- if(yLoc1>=0 && !unreachable_table[xLoc2][0] && !world.get_loc(xLoc1, yLoc1)->can_move(mobileType))
- {
- unreachable_table[xLoc2][0] = 1;
- analyseResult--;
- }
- //----- bottom edge -----//
- if(yLoc2<MAX_WORLD_Y_LOC && !unreachable_table[xLoc2][targetHeight+1] &&
- !world.get_loc(xLoc1, yLoc2)->can_move(mobileType))
- {
- unreachable_table[xLoc2][targetHeight+1] = 1;
- analyseResult--;
- }
- if(!analyseResult)
- return;
- }
- debug_result_check(analyseResult, targetWidth, targetHeight);
- err_when(analyseResult<0);
- }
- //----------- End of function UnitArray::update_unreachable_table -----------//
- //--------- Begin of function UnitArray::attack ---------//
- //
- // Order the unit to attack a specific location.
- //
- // <int> targetXLoc, targetYLoc - target location
- // <int> divided - whether the units are divided
- // <short*> selectedUnitArray - an array of recno of selected units.
- // <int> selectedCount - no. of selected units.
- // [char] remoteAction - whether this is an action carried out by a remote machine or not.
- // (default: 0)
- // <int> targetUnitRecno - if it is unit, pass the unit recno, otherwise 0
- //
- void UnitArray::attack(int targetXLoc, int targetYLoc, int divided, short* selectedUnitArray, int selectedCount, char remoteAction, int targetUnitRecno)
- {
- err_when(targetXLoc<0 || targetYLoc<0 || targetXLoc>=MAX_WORLD_X_LOC || targetYLoc>=MAX_WORLD_Y_LOC);
- // ######## patch begin Gilbert 5/8 #########//
- int targetNationRecno = 0;
- if( targetUnitRecno == 0 )
- {
- // unit determined from the location
- int tmpMobileType;
- Location* locPtr = world.get_loc(targetXLoc, targetYLoc);
- targetUnitRecno = locPtr->get_any_unit(tmpMobileType) ;
- }
- else
- {
- // location determined by the unit
- Unit *unitPtr;
- if( !unit_array.is_deleted(targetUnitRecno)
- && (unitPtr = unit_array[targetUnitRecno])
- && unitPtr->is_visible() )
- {
- targetXLoc = unitPtr->next_x_loc();
- targetYLoc = unitPtr->next_y_loc();
- if( unitPtr->unit_id != UNIT_EXPLOSIVE_CART ) // attacking own porcupine is allowed
- targetNationRecno = unitPtr->nation_recno;
- }
- else
- targetUnitRecno = 0;
- }
-
- if( targetUnitRecno == 0 )
- {
- //--- set the target coordination to the top left position of the town/firm ---//
- Location* locPtr = world.get_loc(targetXLoc, targetYLoc);
- //---- if there is a firm on this location -----//
- if( locPtr->is_firm() )
- {
- Firm* firmPtr = firm_array[locPtr->firm_recno()];
- targetXLoc = firmPtr->loc_x1;
- targetYLoc = firmPtr->loc_y1;
- targetNationRecno = firmPtr->nation_recno;
- }
- //---- if there is a town on this location -----//
- else if( locPtr->is_town() )
- {
- Town* townPtr = town_array[locPtr->town_recno()];
- targetXLoc = townPtr->loc_x1;
- targetYLoc = townPtr->loc_y1;
- targetNationRecno = townPtr->nation_recno;
- }
- else
- return;
- }
- // ######## patch end Gilbert 5/8 #########//
- //--------- AI debug code ---------//
- //--- AI attacking a nation which its NationRelation::should_attack is 0 ---//
- Unit* attackUnit = unit_array[ selectedUnitArray[0] ];
- if( attackUnit->nation_recno && targetNationRecno )
- {
- if( nation_array[attackUnit->nation_recno]->get_relation(targetNationRecno)->should_attack==0 )
- return;
- }
- //-------- if it's a multiplayer game --------//
- if( !remoteAction && remote.is_enable() )
- {
- short* shortPtr = (short*) remote.new_send_queue_msg(MSG_UNIT_ATTACK, sizeof(short) * (5+selectedCount) );
- shortPtr[0] = targetXLoc;
- shortPtr[1] = targetYLoc;
- shortPtr[2] = targetUnitRecno;
- shortPtr[3] = selectedCount;
- shortPtr[4] = divided;
- memcpy( shortPtr+5, selectedUnitArray, sizeof(short) * selectedCount );
- }
- else
- {
- if(!divided)
- {
- divide_array(targetXLoc, targetYLoc, selectedUnitArray, selectedCount, 1); // 1 for excluding the recno in target location
- Location* locPtr = world.get_loc(targetXLoc, targetYLoc);
- int targetMobileType = locPtr->has_any_unit();
- if(selected_land_unit_count)
- attack_call(targetXLoc, targetYLoc, UNIT_LAND, targetMobileType, 1, selected_land_unit_array, selected_land_unit_count, targetUnitRecno);
-
- if(selected_sea_unit_count)
- attack_call(targetXLoc, targetYLoc, UNIT_SEA, targetMobileType, 1, selected_sea_unit_array, selected_sea_unit_count, targetUnitRecno);
- if(selected_air_unit_count)
- attack_call(targetXLoc, targetYLoc, UNIT_AIR, targetMobileType, 1, selected_air_unit_array, selected_air_unit_count, targetUnitRecno);
- selected_land_unit_count = selected_sea_unit_count = selected_air_unit_count = 0;
- mem_del(selected_land_unit_array);
- mem_del(selected_sea_unit_array);
- mem_del(selected_air_unit_array);
- return;
- }
- else
- err_here();
- }
- }
- //----------- End of function UnitArray::attack -----------//
- //--------- Begin of function UnitArray::attack_call ---------//
- // <int> targetXLoc - target x location
- // <int> targetYLoc - target y location
- // <char> mobileType - attacker's mobile type
- // <char> targetMobileType - target mobile type
- // <int> divided - whether the units are divided
- // <short*> selectedUnitArray - selected units' recno.
- // <int> selectedCount - num of selected units
- //
- void UnitArray::attack_call(int targetXLoc, int targetYLoc, char mobileType, char targetMobileType, int divided, short* selectedUnitArray, int selectedCount, int targetUnitRecno)
- {
- //------------- attack now -------------//
- err_when( selectedCount > 10000 );
- Location* locPtr = world.get_loc(targetXLoc, targetYLoc);
- err_when(!locPtr);
-
- // ##### patch begin Gilbert 5/8 ######//
- //if(targetMobileType)
- if( targetUnitRecno && !unit_array.is_deleted(targetUnitRecno) )
- {
- //---------------- attack unit --------------//
- //Unit *targetUnit = unit_array[locPtr->unit_recno(targetMobileType)];
- Unit *targetUnit = unit_array[targetUnitRecno];
- if(!targetUnit->is_visible() || targetUnit->hit_points<=0)
- return;
- // short targetUnitRecno = targetUnit->sprite_recno;
- attack_unit(targetXLoc, targetYLoc, targetUnitRecno, selectedUnitArray, selectedCount);
- }
- // ##### patch end Gilbert 5/8 ######//
- else if(locPtr->is_firm())
- {
- //------------------ attack firm -------------------//
- Firm *firmPtr = firm_array[locPtr->firm_recno()];
- err_when(!firmPtr);
- if(firmPtr->hit_points<=0)
- return;
- attack_firm(targetXLoc, targetYLoc, firmPtr->firm_recno, selectedUnitArray, selectedCount);
- }
- else if(locPtr->is_town())
- {
- //-------------------- attack town -------------------//
- Town *townPtr = town_array[locPtr->town_recno()];
- err_when(!townPtr);
- attack_town(targetXLoc, targetYLoc, townPtr->town_recno, selectedUnitArray, selectedCount);
- }
- else if(locPtr->is_wall())
- {
- //------------------ attack wall ---------------------//
- attack_wall(targetXLoc, targetYLoc, selectedUnitArray, selectedCount);
- }
- else
- return; // no target for attacking
- }
- //----------- End of function UnitArray::attack_call -----------//
- //--------- Begin of function UnitArray::attack_unit ---------//
- // <int> targetXLoc, targetYLoc - the unit upper left location
- // <short> targetunitRecno - the unit recno
- //
- // Note : this attack function only for attackers with size 1x1.
- //
- void UnitArray::attack_unit(int targetXLoc, int targetYLoc, short targetUnitRecno, short* selectedUnitArray, int selectedCount)
- {
- err_when(selectedCount<=0);
- if(selectedCount==1)
- {
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- unitPtr->unit_group_id = unit_array.cur_group_id++;
- unitPtr->attack_unit(targetXLoc, targetYLoc);
- return;
- }
-
- //********************** improve later begin **************************//
- //---------------------------------------------------------------------//
- // codes for differnt territory or different mobile_type attacking should
- // be added in the future.
- //---------------------------------------------------------------------//
- Unit *firstUnitPtr = unit_array[selectedUnitArray[0]];
- Unit *targetPtr2 = unit_array[targetUnitRecno];
- if( (world.get_loc(targetXLoc, targetYLoc)->region_id !=
- world.get_loc(firstUnitPtr->next_x_loc(), firstUnitPtr->next_y_loc())->region_id) ||
- (targetPtr2->mobile_type!=firstUnitPtr->mobile_type) )
- {
- Unit *unitPtr;
- set_group_id(selectedUnitArray, selectedCount);
- for(int i=0; i<selectedCount; i++)
- {
- unitPtr = (Unit*) get_ptr(selectedUnitArray[i]);
- unitPtr->attack_unit(targetXLoc, targetYLoc);
- }
- return;
- }
- //*********************** improve later end ***************************//
-
- //----------- initialize local parameters ------------//
- Unit *targetPtr = unit_array[targetUnitRecno];
- int targetWidth = targetPtr->sprite_info->loc_width;
- int targetHeight = targetPtr->sprite_info->loc_height;
- int targetXLoc2 = targetXLoc + targetWidth - 1;
- int targetYLoc2 = targetYLoc + targetHeight - 1;
- char surroundLoc = get_target_surround_loc(targetWidth, targetHeight);
- char *xOffsetPtr = get_target_x_offset(targetWidth, targetHeight, 0);
- char *yOffsetPtr = get_target_y_offset(targetWidth, targetHeight, 0);
- //---------------------------------------------------------------------//
- // construct data structure
- //---------------------------------------------------------------------//
- int tempVar = sizeof(short)*selectedCount;
-
- memset(dir_array_count, 0, sizeof(dir_array_count));
- int count;
- for(count=0; count<ATTACK_DIR; count++)
- {
- dir_array_ptr[count] = (short*) mem_add(tempVar);
- memset(dir_array_ptr[count], 0, tempVar);
- }
- unit_processed_array = (short *)mem_add(tempVar);
- unit_processed_count = 0;
- //---------------------------------------------------------------------//
- // divide the units into each region
- //---------------------------------------------------------------------//
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- err_when(!unitPtr);
- DWORD groupId = unit_array.cur_group_id++;
- arrange_units_in_group(targetXLoc, targetYLoc, targetXLoc2, targetYLoc2, selectedUnitArray, selectedCount, groupId, 1);
- //---------------------------------------------------------------------//
- // now the attackers are divided into 8 groups to attack the target
- //---------------------------------------------------------------------//
- int xLoc, yLoc; // actual target surrounding location to move to
- int xOffset, yOffset; // offset location of the target
- int unprocessed; // number of units in this group
- int dist, xDist, yDist, minDist;
- int destCount;
- short unitPos; // store the position of the unit with minDist
- short *curArrayPtr;
- int i;
- for(i=0; i<MAX_UNIT_SURROUND_SIZE; i++)
- memset(unreachable_table[i], 0, sizeof(char)*MAX_UNIT_SURROUND_SIZE);
- //---------------------------------------------------------------------//
- // anaylse the surrounding location of the target
- //---------------------------------------------------------------------//
- int analyseResult = analyse_surround_location(targetXLoc, targetYLoc, targetWidth, targetHeight, targetPtr->mobile_type);
- debug_result_check(analyseResult, targetWidth, targetHeight);
- err_when(analyseResult<0);
- if(!analyseResult) // 0 if all surround location is not accessible
- {
- //------------------------------------------------------------//
- // special handling for this case
- //------------------------------------------------------------//
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, targetUnitRecno, selectedUnitArray, selectedCount, 1);
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- //---------------------------------------------------------------------//
- // let the units move to the rest accessible location
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++) // for each array/group
- {
- //--------- initialize for each group --------//
- unprocessed = dir_array_count[count]; // get the number of units in this region
- if(!unprocessed)
- continue;
- destCount = surroundLoc-1;
- curArrayPtr = dir_array_ptr[count]; // get the recno of units in this region
- xOffsetPtr = get_target_x_offset(targetWidth, targetHeight, count);
- yOffsetPtr = get_target_y_offset(targetWidth, targetHeight, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- //-----------------------------------------------------------------//
- // determine a suitable location for the attacker to move to
- //-----------------------------------------------------------------//
- while(unprocessed)
- {
- //-----------------------------------------------------//
- // find a reachable location, or not searched location
- //-----------------------------------------------------//
- err_when(analyseResult<0);
- if(!analyseResult)
- {
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, targetUnitRecno, selectedUnitArray, selectedCount, 1);
-
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- else
- {
- for(i=0; i<surroundLoc; i++)
- {
- if((++destCount)>=surroundLoc)
- {
- destCount = 0;
- xOffsetPtr = get_target_x_offset(targetWidth, targetHeight, count);
- yOffsetPtr = get_target_y_offset(targetWidth, targetHeight, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- }
- else
- {
- xOffset = *(++xOffsetPtr);
- yOffset = *(++yOffsetPtr);
- }
- if(!unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST])
- break;
- }
- }
- err_when(unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST]);
- //------------------------------------------------------------//
- // find the closest attacker
- //------------------------------------------------------------//
- err_when(unprocessed>2000 || unprocessed <0);
- xLoc = targetXLoc + xOffset;
- yLoc = targetYLoc + yOffset;
- err_when(!world.get_loc(xLoc, yLoc)->can_move(targetPtr->mobile_type));
- for(i=0, minDist=0x7FFFFFF; i<unprocessed; i++)
- {
- unitPtr = (Unit*) get_ptr(curArrayPtr[i]);
- xDist = abs(xLoc-unitPtr->next_x_loc());
- yDist = abs(yLoc-unitPtr->next_y_loc());
- dist = (xDist >= yDist) ? xDist*10+yDist : yDist*10+xDist;
- if(dist < minDist)
- {
- minDist = dist;
- unitPos = i;
- }
- }
- unitPtr = (Unit*) get_ptr(curArrayPtr[unitPos]);
- curArrayPtr[unitPos] = curArrayPtr[--unprocessed]; // move the units in the back to the front
- err_when(unitPtr->action_mode2!=ACTION_ATTACK_WALL &&
- unitPtr->cur_action==SPRITE_ATTACK && unitPtr->action_para==0);
- seek_path.set_status(PATH_WAIT);
- err_when(seek_path.path_status==PATH_NODE_USED_UP);
- unitPtr->attack_unit(targetXLoc, targetYLoc, xOffset, yOffset);
- //------------------------------------------------------------//
- // store the unit sprite_recno in the array
- //------------------------------------------------------------//
- unit_processed_array[unit_processed_count++] = unitPtr->sprite_recno;
- //------------------------------------------------------------//
- // set the flag if unreachable
- //------------------------------------------------------------//
- if(seek_path.path_status==PATH_NODE_USED_UP)
- {
- unreachable_table[xLoc-targetXLoc+SHIFT_ADJUST][yLoc-targetYLoc+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, targetWidth, targetHeight);
- err_when(analyseResult<0);
- //------------------------------------------------------------//
- // the nearby location should also be unreachable
- //------------------------------------------------------------//
- check_nearby_location(targetXLoc, targetYLoc, xOffset, yOffset, targetWidth, targetHeight,
- targetPtr->mobile_type, analyseResult);
- }
- update_unreachable_table(targetXLoc, targetYLoc, targetWidth, targetHeight, unitPtr->mobile_type, analyseResult);
- #ifdef DEBUG
- for(int di=0; di<targetWidth+2; di++)
- {
- for(int dj=0; dj<targetHeight+2; dj++)
- {
- if(di>=1 && di<=targetWidth && dj>=1 && dj<=targetHeight)
- continue;
- int debugXLoc = targetXLoc+di-SHIFT_ADJUST;
- int debugYLoc = targetYLoc+dj-SHIFT_ADJUST;
- if(debugXLoc<0 || debugXLoc>=MAX_WORLD_X_LOC || debugYLoc<0 || debugYLoc>=MAX_WORLD_Y_LOC)
- continue;
- Location *dlPtr = world.get_loc(debugXLoc, debugYLoc);
-
- err_when(!dlPtr->can_move(targetPtr->mobile_type) && unreachable_table[di][dj]==0);
- }
- }
- #endif
- }
- }
- //---------------------------------------------------------------------//
- // set the unreachable flag for each units
- //---------------------------------------------------------------------//
- //-************** codes here ***************-//
- //---------------------------------------------------------------------//
- // destruct data structure
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- }
- //----------- End of function UnitArray::attack_unit -----------//
- //--------- Begin of function UnitArray::attack_firm ---------//
- // <int> targetXLoc, targetYLoc - the firm upper left location
- // <short> firmRecno - the firm recno
- //
- // try to calculate the best location for each unit to move to the
- // surrounding of the firm for attacking
- //
- void UnitArray::attack_firm(int targetXLoc, int targetYLoc, short firmRecno, short* selectedUnitArray, int selectedCount)
- {
- err_when(selectedCount<=0);
- if(selectedCount==1)
- {
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- unitPtr->unit_group_id = unit_array.cur_group_id++;
- unitPtr->attack_firm(targetXLoc, targetYLoc);
- return;
- }
- //********************** improve later begin **************************//
- //---------------------------------------------------------------------//
- // codes for differnt territory or different mobile_type attacking should
- // be added in the future.
- //---------------------------------------------------------------------//
- Unit *firstUnitPtr = unit_array[selectedUnitArray[0]];
- if(world.get_loc(targetXLoc, targetYLoc)->region_id !=
- world.get_loc(firstUnitPtr->next_x_loc(), firstUnitPtr->next_y_loc())->region_id)
- {
- Unit *unitPtr;
- set_group_id(selectedUnitArray, selectedCount);
- for(int i=0; i<selectedCount; i++)
- {
- unitPtr = (Unit*) get_ptr(selectedUnitArray[i]);
- unitPtr->attack_firm(targetXLoc, targetYLoc);
- }
- return;
- }
- //*********************** improve later end ***************************//
- //----------- initialize local parameters ------------//
- Firm *firmPtr = firm_array[firmRecno];
- FirmInfo *firmInfo = firm_res[firmPtr->firm_id];
- int firmWidth = firmInfo->loc_width;
- int firmHeight = firmInfo->loc_height;
- int targetXLoc2 = targetXLoc + firmWidth - 1; // the lower right corner of the firm
- int targetYLoc2 = targetYLoc + firmHeight -1;
- char *xOffsetPtr, *yOffsetPtr;
- //---------------------------------------------------------------------//
- // construct data structure
- //---------------------------------------------------------------------//
- int tempVar = sizeof(short)*selectedCount;
-
- memset(dir_array_count, 0, sizeof(dir_array_count));
- int count;
- for(count=0; count<ATTACK_DIR; count++)
- {
- dir_array_ptr[count] = (short*) mem_add(tempVar);
- memset(dir_array_ptr[count], 0, tempVar);
- }
- unit_processed_array = (short *)mem_add(tempVar);
- unit_processed_count = 0;
- //---------------------------------------------------------------------//
- // divide the units into each region
- //---------------------------------------------------------------------//
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- err_when(!unitPtr);
- DWORD groupId = unit_array.cur_group_id++;
- arrange_units_in_group(targetXLoc, targetYLoc, targetXLoc2, targetYLoc2, selectedUnitArray, selectedCount, groupId, 2);
- //---------------------------------------------------------------------//
- // now the attackers are divided into 8 groups to attack the target
- //---------------------------------------------------------------------//
- int xLoc, yLoc; // actual target surrounding location to move to
- int xOffset, yOffset; // offset location of the target
- int unprocessed; // number of units in this group
- int destCount;
- int dist, xDist, yDist, minDist;
- short unitPos; // store the position of the unit with minDist
- short *curArrayPtr;
- char surroundLoc = get_target_surround_loc(firmWidth, firmHeight);
- int i;
- for(i=0; i<MAX_UNIT_SURROUND_SIZE; i++)
- memset(unreachable_table[i], 0, sizeof(char)*MAX_UNIT_SURROUND_SIZE);
- //---------------------------------------------------------------------//
- // analyse the surrounding location of the target
- //---------------------------------------------------------------------//
- int analyseResult = analyse_surround_location(targetXLoc, targetYLoc, firmWidth, firmHeight, unitPtr->mobile_type);
- debug_result_check(analyseResult, firmWidth, firmHeight);
- err_when(analyseResult<0);
- if(!analyseResult) // 0 if all surround location is not accessible
- {
- //------------------------------------------------------------//
- // special handling for this case
- //------------------------------------------------------------//
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, firmRecno, selectedUnitArray, selectedCount, 2);
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- //---------------------------------------------------------------------//
- // let the units move to the rest accessible location
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++) // for each array/group
- {
- //--------- initialize for each group --------//
- unprocessed = dir_array_count[count]; // get the number of units in this region
- curArrayPtr = dir_array_ptr[count]; // get the recno of units in this region
- destCount = surroundLoc - 1;
- xOffsetPtr = get_target_x_offset(firmWidth, firmHeight, count);
- yOffsetPtr = get_target_y_offset(firmWidth, firmHeight, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- //-----------------------------------------------------------------//
- // determine a suitable location for the attacker to move to
- //-----------------------------------------------------------------//
- err_when(unprocessed>2000 || unprocessed <0);
- while(unprocessed)
- {
- //-----------------------------------------------------//
- // find a reachable location, or not searched location
- //-----------------------------------------------------//
- err_when(analyseResult<0);
- if(!analyseResult)
- {
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, firmRecno, selectedUnitArray, selectedCount, 2);
-
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- else
- {
- for(i=0; i<surroundLoc; i++)
- {
- if((++destCount)>=surroundLoc)
- {
- destCount = 0;
- xOffsetPtr = get_target_x_offset(firmWidth, firmHeight, count);
- yOffsetPtr = get_target_y_offset(firmWidth, firmHeight, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- }
- else
- {
- xOffset = *(++xOffsetPtr);
- yOffset = *(++yOffsetPtr);
- }
- if(!unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST])
- break;
- }
- }
- err_when(unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST]);
- //------------------------------------------------------------//
- // find the closest attacker
- //------------------------------------------------------------//
- err_when(unprocessed>2000 || unprocessed <0);
- xLoc = targetXLoc + xOffset;
- yLoc = targetYLoc + yOffset;
- err_when(!world.get_loc(xLoc, yLoc)->can_move(unitPtr->mobile_type));
- for(i=0, minDist=0x7FFFFFF; i<unprocessed; i++)
- {
- unitPtr = (Unit*) get_ptr(curArrayPtr[i]);
- xDist = abs(xLoc-unitPtr->next_x_loc());
- yDist = abs(yLoc-unitPtr->next_y_loc());
- dist = (xDist >= yDist) ? xDist*10+yDist : yDist*10+xDist;
- if(dist < minDist)
- {
- minDist = dist;
- unitPos = i;
- }
- }
- unitPtr = (Unit*) get_ptr(curArrayPtr[unitPos]);
- curArrayPtr[unitPos] = curArrayPtr[--unprocessed]; // move the units in the back to the front
- err_when(unitPtr->action_mode2!=ACTION_ATTACK_WALL &&
- unitPtr->cur_action==SPRITE_ATTACK && unitPtr->action_para==0);
- seek_path.set_status(PATH_WAIT);
- err_when(seek_path.path_status==PATH_NODE_USED_UP);
- unitPtr->attack_firm(targetXLoc, targetYLoc, xOffset, yOffset);
- //------------------------------------------------------------//
- // set the flag if unreachable
- //------------------------------------------------------------//
- if(seek_path.path_status==PATH_NODE_USED_UP)
- {
- unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, firmWidth, firmHeight);
- err_when(analyseResult<0);
- //------------------------------------------------------------//
- // the nearby location should also be unreachable
- //------------------------------------------------------------//
- check_nearby_location(targetXLoc, targetYLoc, xOffset, yOffset, firmWidth, firmHeight,
- unitPtr->mobile_type, analyseResult);
- }
- update_unreachable_table(targetXLoc, targetYLoc, firmWidth, firmHeight, unitPtr->mobile_type, analyseResult);
- #ifdef DEBUG
- for(int di=0; di<firmWidth+2; di++)
- {
- for(int dj=0; dj<firmHeight+2; dj++)
- {
- if(di>=1 && di<=firmWidth && dj>=1 && dj<=firmHeight)
- continue;
- int debugXLoc = targetXLoc+di-SHIFT_ADJUST;
- int debugYLoc = targetYLoc+dj-SHIFT_ADJUST;
- if(debugXLoc<0 || debugXLoc>=MAX_WORLD_X_LOC || debugYLoc<0 || debugYLoc>=MAX_WORLD_Y_LOC)
- continue;
- Location *dlPtr = world.get_loc(debugXLoc, debugYLoc);
-
- err_when(!dlPtr->can_move(unitPtr->mobile_type) && unreachable_table[di][dj]==0);
- }
- }
- #endif
- }
- }
- //---------------------------------------------------------------------//
- // set the unreachable flag for each units
- //---------------------------------------------------------------------//
- //-************** codes here ***************-//
- //---------------------------------------------------------------------//
- // destruct data structure
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- }
- //----------- End of function UnitArray::attack_firm -----------//
- //--------- Begin of function UnitArray::attack_town ---------//
- // <int> targetXLoc, targetYLoc - the town upper left location
- // <short> townRecno - the town recno
- //
- // try to calculate the best location for each unit to move to the
- // surrounding of the town for attacking
- //
- void UnitArray::attack_town(int targetXLoc, int targetYLoc, short townRecno, short* selectedUnitArray, int selectedCount)
- {
- err_when(selectedCount<=0);
- if(selectedCount==1)
- {
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- unitPtr->unit_group_id = unit_array.cur_group_id++;
- unitPtr->attack_town(targetXLoc, targetYLoc);
- return;
- }
- //********************** improve later begin **************************//
- //---------------------------------------------------------------------//
- // codes for differnt territory or different mobile_type attacking should
- // be added in the future.
- //---------------------------------------------------------------------//
- Unit *firstUnitPtr = unit_array[selectedUnitArray[0]];
- if(world.get_loc(targetXLoc, targetYLoc)->region_id !=
- world.get_loc(firstUnitPtr->next_x_loc(), firstUnitPtr->next_y_loc())->region_id)
- {
- Unit *unitPtr;
- set_group_id(selectedUnitArray, selectedCount);
- for(int i=0; i<selectedCount; i++)
- {
- unitPtr = (Unit*) get_ptr(selectedUnitArray[i]);
- unitPtr->attack_town(targetXLoc, targetYLoc);
- }
- return;
- }
- //*********************** improve later end ***************************//
- //----------- initialize local parameters ------------//
- int targetXLoc2 = targetXLoc + STD_TOWN_LOC_WIDTH - 1; // the lower right corner of the firm
- int targetYLoc2 = targetYLoc + STD_TOWN_LOC_HEIGHT - 1;
- char *xOffsetPtr, *yOffsetPtr;
- //---------------------------------------------------------------------//
- // construct data structure
- //---------------------------------------------------------------------//
- int tempVar = sizeof(short)*selectedCount;
-
- memset(dir_array_count, 0, sizeof(dir_array_count));
- int count;
- for(count=0; count<ATTACK_DIR; count++)
- {
- dir_array_ptr[count] = (short*) mem_add(tempVar);
- memset(dir_array_ptr[count], 0, tempVar);
- }
- unit_processed_array = (short *)mem_add(tempVar);
- unit_processed_count = 0;
- //---------------------------------------------------------------------//
- // divide the units into each region
- //---------------------------------------------------------------------//
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- err_when(!unitPtr);
- DWORD groupId = unit_array.cur_group_id++;
- arrange_units_in_group(targetXLoc, targetYLoc, targetXLoc2, targetYLoc2, selectedUnitArray, selectedCount, groupId, 3);
- //---------------------------------------------------------------------//
- // now the attackers are divided into 8 groups to attack the target
- //---------------------------------------------------------------------//
- int xLoc, yLoc; // actual target surrounding location to move to
- int xOffset, yOffset; // offset location of the target
- int unprocessed; // number of units in this group
- int dist, xDist, yDist, minDist;
- int destCount;
- short unitPos; // store the position of the unit with minDist
- short *curArrayPtr;
- char surroundLoc = get_target_surround_loc(STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT);
- int i;
- for(i=0; i<MAX_UNIT_SURROUND_SIZE; i++)
- memset(unreachable_table[i], 0, sizeof(char)*MAX_UNIT_SURROUND_SIZE);
- //---------------------------------------------------------------------//
- // analyse the surrounding location of the target
- //---------------------------------------------------------------------//
- int analyseResult = analyse_surround_location(targetXLoc, targetYLoc, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, unitPtr->mobile_type);
- debug_result_check(analyseResult, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT);
- err_when(analyseResult<0);
- if(!analyseResult) // 0 if all surround location is not accessible
- {
- //------------------------------------------------------------//
- // special handling for this case
- //------------------------------------------------------------//
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, townRecno, selectedUnitArray, selectedCount, 3);
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- //---------------------------------------------------------------------//
- // let the units move to the rest accessible location
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++) // for each array/group
- {
- //--------- initialize for each group --------//
- unprocessed = dir_array_count[count]; // get the number of units in this region
- curArrayPtr = dir_array_ptr[count]; // get the recno of units in this region
- destCount = surroundLoc-1;
- xOffsetPtr = get_target_x_offset(STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, count);
- yOffsetPtr = get_target_y_offset(STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- //-----------------------------------------------------------------//
- // determine a suitable location for the attacker to move to
- //-----------------------------------------------------------------//
- while(unprocessed)
- {
- //-----------------------------------------------------//
- // find a reachable location, or not searched location
- //-----------------------------------------------------//
- if(!analyseResult)
- {
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, townRecno, selectedUnitArray, selectedCount, 3);
-
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- else
- {
- for(i=0; i<surroundLoc; i++)
- {
- if((++destCount)>=surroundLoc)
- {
- destCount = 0;
- xOffsetPtr = get_target_x_offset(STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, count);
- yOffsetPtr = get_target_y_offset(STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- }
- else
- {
- xOffset = *(++xOffsetPtr);
- yOffset = *(++yOffsetPtr);
- }
- if(!unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST])
- break;
- }
- }
- err_when(unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST]);
- //------------------------------------------------------------//
- // find the closest attacker
- //------------------------------------------------------------//
- err_when(unprocessed>2000 || unprocessed <0);
- xLoc = targetXLoc + xOffset;
- yLoc = targetYLoc + yOffset;
- err_when(!world.get_loc(xLoc, yLoc)->can_move(unitPtr->mobile_type));
- for(i=0, minDist=0x7FFFFFF; i<unprocessed; i++)
- {
- unitPtr = (Unit*) get_ptr(curArrayPtr[i]);
- xDist = abs(xLoc-unitPtr->next_x_loc());
- yDist = abs(yLoc-unitPtr->next_y_loc());
- dist = (xDist >= yDist) ? xDist*10+yDist : yDist*10+xDist;
- if(dist < minDist)
- {
- minDist = dist;
- unitPos = i;
- }
- }
- unitPtr = (Unit*) get_ptr(curArrayPtr[unitPos]);
- curArrayPtr[unitPos] = curArrayPtr[--unprocessed]; // move the units in the back to the front
- err_when(unitPtr->action_mode2!=ACTION_ATTACK_WALL &&
- unitPtr->cur_action==SPRITE_ATTACK && unitPtr->action_para==0);
- seek_path.set_status(PATH_WAIT);
- err_when(seek_path.path_status==PATH_NODE_USED_UP);
- unitPtr->attack_town(targetXLoc, targetYLoc, xOffset, yOffset);
- //------------------------------------------------------------//
- // set the flag if unreachable
- //------------------------------------------------------------//
- if(seek_path.path_status==PATH_NODE_USED_UP)
- {
- unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT);
- err_when(analyseResult<0);
- //------------------------------------------------------------//
- // the nearby location should also be unreachable
- //------------------------------------------------------------//
- check_nearby_location(targetXLoc, targetYLoc, xOffset, yOffset, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT,
- unitPtr->mobile_type, analyseResult);
- }
- update_unreachable_table(targetXLoc, targetYLoc, STD_TOWN_LOC_WIDTH, STD_TOWN_LOC_HEIGHT, unitPtr->mobile_type, analyseResult);
- #ifdef DEBUG
- Location *dlPtr;
- for(int di=0; di<STD_TOWN_LOC_WIDTH+2; di++)
- {
- for(int dj=0; dj<STD_TOWN_LOC_HEIGHT+2; dj++)
- {
- if(di>=1 && di<=STD_TOWN_LOC_WIDTH && dj>=1 && dj<=STD_TOWN_LOC_HEIGHT)
- continue;
- int debugXLoc = targetXLoc+di-SHIFT_ADJUST;
- int debugYLoc = targetYLoc+dj-SHIFT_ADJUST;
- if(debugXLoc<0 || debugXLoc>=MAX_WORLD_X_LOC || debugYLoc<0 || debugYLoc>=MAX_WORLD_Y_LOC)
- continue;
- dlPtr = world.get_loc(debugXLoc, debugYLoc);
-
- err_when(!dlPtr->can_move(unitPtr->mobile_type) && unreachable_table[di][dj]==0);
- }
- }
- #endif
- }
- }
- //---------------------------------------------------------------------//
- // set the unreachable flag for each units
- //---------------------------------------------------------------------//
- //-************** codes here ***************-//
- //---------------------------------------------------------------------//
- // destruct data structure
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- }
- //----------- End of function UnitArray::attack_town -----------//
- //--------- Begin of function UnitArray::attack_wall ---------//
- // <int> targetXLoc - x location of the wall
- // <int> targetYLoc - y location of the wall
- //
- void UnitArray::attack_wall(int targetXLoc, int targetYLoc, short* selectedUnitArray, int selectedCount)
- {
- err_when(selectedCount<=0);
- if(selectedCount==1)
- {
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- unitPtr->unit_group_id = unit_array.cur_group_id++;
- unitPtr->attack_wall(targetXLoc, targetYLoc);
- return;
- }
- //********************** improve later begin **************************//
- //---------------------------------------------------------------------//
- // codes for differnt territory or different mobile_type attacking should
- // be added in the future.
- //---------------------------------------------------------------------//
- Unit *firstUnitPtr = unit_array[selectedUnitArray[0]];
- if(world.get_loc(targetXLoc, targetYLoc)->region_id !=
- world.get_loc(firstUnitPtr->next_x_loc(), firstUnitPtr->next_y_loc())->region_id)
- {
- Unit *unitPtr;
- set_group_id(selectedUnitArray, selectedCount);
- for(int i=0; i<selectedCount; i++)
- {
- unitPtr = (Unit*) get_ptr(selectedUnitArray[i]);
- unitPtr->attack_wall(targetXLoc, targetYLoc);
- }
- return;
- }
- //*********************** improve later end ***************************//
- //----------- initialize local parameters ------------//
- char *xOffsetPtr, *yOffsetPtr;
- //---------------------------------------------------------------------//
- // construct data structure
- //---------------------------------------------------------------------//
- int tempVar = sizeof(short)*selectedCount;
-
- memset(dir_array_count, 0, sizeof(dir_array_count));
- int count;
- for(count=0; count<ATTACK_DIR; count++)
- {
- dir_array_ptr[count] = (short*) mem_add(tempVar);
- memset(dir_array_ptr[count], 0, tempVar);
- }
- unit_processed_array = (short *)mem_add(tempVar);
- unit_processed_count = 0;
- //---------------------------------------------------------------------//
- // divide the units into each region
- //---------------------------------------------------------------------//
- Unit *unitPtr = (Unit*) get_ptr(selectedUnitArray[0]);
- err_when(!unitPtr);
- DWORD groupId = unit_array.cur_group_id++;
- arrange_units_in_group(targetXLoc, targetYLoc, targetXLoc, targetYLoc, selectedUnitArray, selectedCount, groupId, 0);
- //---------------------------------------------------------------------//
- // now the attackers are divided into 8 groups to attack the target
- //---------------------------------------------------------------------//
- int xLoc, yLoc; // actual target surrounding location to move to
- int xOffset, yOffset; // offset location of the target
- int unprocessed; // number of units in this group
- int dist, xDist, yDist, minDist;
- int destCount;
- short unitPos; // store the position of the unit with minDist
- short *curArrayPtr;
- char surroundLoc = get_target_surround_loc(1, 1);
- int i;
- for(i=0; i<MAX_UNIT_SURROUND_SIZE; i++)
- memset(unreachable_table[i], 0, sizeof(char)*MAX_UNIT_SURROUND_SIZE);
- //---------------------------------------------------------------------//
- // analyse the surrounding location of the target
- //---------------------------------------------------------------------//
- int analyseResult = analyse_surround_location(targetXLoc, targetYLoc, 1, 1, unitPtr->mobile_type);
- debug_result_check(analyseResult, 1, 1);
- err_when(analyseResult<0);
- if(!analyseResult) // 0 if all surround location is not accessible
- {
- //------------------------------------------------------------//
- // special handling for this case
- //------------------------------------------------------------//
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, 0, selectedUnitArray, selectedCount, 0);
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- //---------------------------------------------------------------------//
- // let the units move to the rest accessible location
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++) // for each array/group
- {
- //--------- initialize for each group --------//
- unprocessed = dir_array_count[count]; // get the number of units in this region
- curArrayPtr = dir_array_ptr[count]; // get the recno of units in this region
- destCount = surroundLoc-1;
- xOffsetPtr = get_target_x_offset(1, 1, count);
- yOffsetPtr = get_target_y_offset(1, 1, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- //-----------------------------------------------------------------//
- // determine a suitable location for the attacker to move to
- //-----------------------------------------------------------------//
- while(unprocessed)
- {
- //-----------------------------------------------------//
- // find a reachable location, or not searched location
- //-----------------------------------------------------//
- if(!analyseResult)
- {
- handle_attack_target_totally_blocked(targetXLoc, targetYLoc, 0, selectedUnitArray, selectedCount, 0);
-
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- return;
- }
- else
- {
- for(i=0; i<surroundLoc; i++)
- {
- if((++destCount)>=surroundLoc)
- {
- destCount = 0;
- xOffsetPtr = get_target_x_offset(1, 1, count);
- yOffsetPtr = get_target_y_offset(1, 1, count);
- xOffset = *xOffsetPtr;
- yOffset = *yOffsetPtr;
- }
- else
- {
- xOffset = *(++xOffsetPtr);
- yOffset = *(++yOffsetPtr);
- }
- if(!unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST])
- break;
- }
- }
- err_when(unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST]);
- //------------------------------------------------------------//
- // find the closest attacker
- //------------------------------------------------------------//
- err_when(unprocessed>2000 || unprocessed <0);
- xLoc = targetXLoc + xOffset;
- yLoc = targetYLoc + yOffset;
- err_when(!world.get_loc(xLoc, yLoc)->can_move(unitPtr->mobile_type));
- for(i=0, minDist=0x7FFFFFF; i<unprocessed; i++)
- {
- unitPtr = (Unit*) get_ptr(curArrayPtr[i]);
- xDist = abs(xLoc-unitPtr->next_x_loc());
- yDist = abs(yLoc-unitPtr->next_y_loc());
- dist = (xDist >= yDist) ? xDist*10+yDist : yDist*10+xDist;
- if(dist < minDist)
- {
- minDist = dist;
- unitPos = i;
- }
- }
- unitPtr = (Unit*) get_ptr(curArrayPtr[unitPos]);
- curArrayPtr[unitPos] = curArrayPtr[--unprocessed]; // move the units in the back to the front
- err_when(unitPtr->action_mode2!=ACTION_ATTACK_WALL &&
- unitPtr->cur_action==SPRITE_ATTACK && unitPtr->action_para==0);
- seek_path.set_status(PATH_WAIT);
- err_when(seek_path.path_status==PATH_NODE_USED_UP);
- unitPtr->attack_wall(targetXLoc, targetYLoc, xOffset, yOffset);
- //------------------------------------------------------------//
- // set the flag if unreachable
- //------------------------------------------------------------//
- if(seek_path.path_status==PATH_NODE_USED_UP)
- {
- unreachable_table[xOffset+SHIFT_ADJUST][yOffset+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, 1, 1);
- err_when(analyseResult<0);
- //------------------------------------------------------------//
- // the nearby location should also be unreachable
- //------------------------------------------------------------//
- check_nearby_location(targetXLoc, targetYLoc, xOffset, yOffset, 1, 1, unitPtr->mobile_type, analyseResult);
- }
- update_unreachable_table(targetXLoc, targetYLoc, 1, 1, unitPtr->mobile_type, analyseResult);
- #ifdef DEBUG
- for(int di=0; di<3; di++)
- {
- for(int dj=0; dj<3; dj++)
- {
- if(di==1 && dj==1)
- continue;
- int debugXLoc = targetXLoc+di-SHIFT_ADJUST;
- int debugYLoc = targetYLoc+dj-SHIFT_ADJUST;
- if(debugXLoc<0 || debugXLoc>=MAX_WORLD_X_LOC || debugYLoc<0 || debugYLoc>=MAX_WORLD_Y_LOC)
- continue;
- Location *dlPtr = world.get_loc(debugXLoc, debugYLoc);
-
- err_when(!dlPtr->can_move(unitPtr->mobile_type) && unreachable_table[di][dj]==0);
- }
- }
- #endif
- }
- }
- //---------------------------------------------------------------------//
- // set the unreachable flag for each units
- //---------------------------------------------------------------------//
- //-************** codes here ***************-//
- //---------------------------------------------------------------------//
- // destruct data structure
- //---------------------------------------------------------------------//
- for(count=0; count<ATTACK_DIR; count++)
- mem_del(dir_array_ptr[count]);
- mem_del(unit_processed_array);
- }
- //----------- End of function UnitArray::attack_wall -----------//
- //--------- Begin of function UnitArray::arrange_units_in_group ---------//
- // <int> xLoc1 - top left x location of the target
- // <int> yLoc1 - top left y location of the target
- // <int> xLoc2 - bottom right x location of the target
- // <int> yLoc2 - bottom right y location of the target
- // <short*> selectedUnitArray - recno. of selected units
- // <int> selectedCount - count of selected units
- // <DWORD> unitGroupId - group id for the selected units
- //
- // <int> targetType - 0 for wall, 1 for unit, 2 for firm, 3 for town
- //
- void UnitArray::arrange_units_in_group(int xLoc1, int yLoc1, int xLoc2, int yLoc2, short* selectedUnitArray,
- int selectedCount, DWORD unitGroupId, int targetType)
- {
- Unit *unitPtr;
- int curXLoc, curYLoc;
- for(int i=0; i<selectedCount; i++)
- {
- unitPtr = (Unit*) get_ptr(selectedUnitArray[i]);
- err_when(!unitPtr);
- unitPtr->unit_group_id = unitGroupId; // set unit_group_id
- err_when(unitPtr->cur_action==SPRITE_IDLE && (unitPtr->cur_x!=unitPtr->next_x || unitPtr->cur_y!=unitPtr->next_y));
- if(unitPtr->cur_action==SPRITE_IDLE)
- unitPtr->set_ready();
- curXLoc = unitPtr->next_x_loc();
- curYLoc = unitPtr->next_y_loc();
- if(curXLoc>=xLoc1-1 && curXLoc<=xLoc2+1 && curYLoc>=yLoc1-1 && curYLoc<=yLoc2+1)
- {
- //------------- already in the target surrounding ----------------//
- switch(targetType)
- {
- case 0: unitPtr->attack_wall(xLoc1, yLoc1);
- break;
- case 1: unitPtr->attack_unit(xLoc1, yLoc1);
- break;
- case 2: unitPtr->attack_firm(xLoc1, yLoc1);
- break;
- case 3: unitPtr->attack_town(xLoc1, yLoc1);
- break;
- }
- continue;
- }
- //---- the attacker need to call searching to reach the target ----//
- if(curXLoc < xLoc1)
- {
- if(curYLoc < yLoc1) // 8
- dir_array_ptr[7][dir_array_count[7]++] = selectedUnitArray[i];
- else if(curYLoc > yLoc2)// 2
- dir_array_ptr[1][dir_array_count[1]++] = selectedUnitArray[i];
- else // 1
- dir_array_ptr[0][dir_array_count[0]++] = selectedUnitArray[i];
- }
- else if(curXLoc > xLoc2)
- {
- if(curYLoc < yLoc1) // 6
- dir_array_ptr[5][dir_array_count[5]++] = selectedUnitArray[i];
- else if(curYLoc > yLoc2)// 4
- dir_array_ptr[3][dir_array_count[3]++] = selectedUnitArray[i];
- else // 5
- dir_array_ptr[4][dir_array_count[4]++] = selectedUnitArray[i];
- }
- else // curXLoc==targetXLoc2
- {
- if(curYLoc < yLoc1) // 7
- dir_array_ptr[6][dir_array_count[6]++] = selectedUnitArray[i];
- else if(curYLoc > yLoc2)// 3
- dir_array_ptr[2][dir_array_count[2]++] = selectedUnitArray[i];
- else // curXLoc==xLoc2 && curYLoc==yLoc2
- {
- // target is one of the selected unit, error
- err_here();
- }
- }
- }
- }
- //----------- End of function UnitArray::arrange_units_in_group -----------//
- //--------- Begin of function UnitArray::analyse_surround_location ---------//
- // return the number of accessible surrounding location of the target
- //
- // <int> targetXLoc - target x location
- // <int> targetYLoc - target y location
- // <int> targetWidth - target width
- // <int> targetHeight - target height
- // <char> mobileType - target mobile type
- //
- int UnitArray::analyse_surround_location(int targetXLoc, int targetYLoc, int targetWidth, int targetHeight, char mobileType)
- {
- static char xIncreTable[4] = { 1, 0, -1, 0};
- static char yIncreTable[4] = { 0, 1, 0, -1};
- err_when(targetWidth<1 || targetWidth>4);
- Location *locPtr;
- int xLoc = targetXLoc-1;
- int yLoc = targetYLoc-1;
- int targetXLoc2 = targetXLoc + targetWidth - 1;
- int targetYLoc2 = targetYLoc + targetHeight - 1;
- int bound = 2*(targetWidth + targetHeight) + 4; // (x+2)*(y+2) - xy
- int increCount=4, xIncre, yIncre, found=0;
- err_when(targetWidth==3 && targetHeight==3 && bound!=16);
- for(int i=0; i<bound; i++)
- {
- if(xLoc<0 || xLoc>=MAX_WORLD_X_LOC || yLoc<0 || yLoc>=MAX_WORLD_Y_LOC)
- unreachable_table[xLoc-targetXLoc+SHIFT_ADJUST][yLoc-targetYLoc+SHIFT_ADJUST] = 1;
- else
- {
- locPtr = world.get_loc(xLoc, yLoc);
- if(!locPtr->can_move(mobileType))
- unreachable_table[xLoc-targetXLoc+SHIFT_ADJUST][yLoc-targetYLoc+SHIFT_ADJUST] = 1;
- else
- found++;
- }
- if((xLoc==targetXLoc-1 || xLoc==targetXLoc2+1) && (yLoc==targetYLoc-1 || yLoc==targetYLoc2+1)) // at the corner
- {
- if((++increCount)>=4)
- increCount = 0;
- xIncre = xIncreTable[increCount];
- yIncre = yIncreTable[increCount];
- }
- xLoc += xIncre;
- yLoc += yIncre;
- }
- return found;
- }
- //----------- End of function UnitArray::analyse_surround_location -----------//
- //--------- Begin of function UnitArray::check_nearby_location ---------//
- // check the target location to find out how many of its surrounding location
- // is not blocked
- //
- // <int> targetXLoc - target x location
- // <int> targetYLoc - target y location
- // <char> xOffset - x offset from target x location
- // <char> yOffset - y offset from target y location
- // <int> targetWidth - target width
- // <int> targetHeight - target height
- // <char> targetMobileType - target mobile type
- // <int&> analyseResult - reference for returning
- //
- void UnitArray::check_nearby_location(int targetXLoc, int targetYLoc, char xOffset, char yOffset,
- int targetWidth, int targetHeight, char targetMobileType, int& analyseResult)
- {
- #ifdef DEBUG
- int backupAnalyseResult = analyseResult;
- char debugUnreachableTable[MAX_UNIT_SURROUND_SIZE][MAX_UNIT_SURROUND_SIZE];
- memcpy(debugUnreachableTable, unreachable_table, sizeof(char)*MAX_UNIT_SURROUND_SIZE*MAX_UNIT_SURROUND_SIZE);
- #endif
- debug_result_check(analyseResult, targetWidth, targetHeight);
- static char leftXIncreTable[4] = { 1, 0, -1, 0};
- static char leftYIncreTable[4] = { 0, 1, 0, -1};
- static char rightXIncreTable[4] = { -1, 0, 1, 0};
- static char rightYIncreTable[4] = { 0, 1, 0, -1};
- err_when(targetWidth<1 || targetWidth>4);
- Location *locPtr;
- int targetXLoc2 = targetXLoc + targetWidth - 1;
- int targetYLoc2 = targetYLoc + targetHeight - 1;
- int bound = 2*(targetWidth + targetHeight) + 4; // (x+2)*(y+2) - xy
-
- int leftXLoc ,leftYLoc, leftContinue=1;
- int leftXIncre, leftYIncre, leftIncreCount;
- int rightXLoc, rightYLoc, rightContinue=1;
- int rightXIncre, rightYIncre, rightIncreCount=1;
- //-------------------------------------------------------------------------------------//
- // determine the initial situation
- //-------------------------------------------------------------------------------------//
- if((xOffset==-1 || xOffset==targetWidth) && (yOffset==-1 || yOffset==targetHeight)) // at the corner
- {
- if(xOffset==-1)
- {
- if(yOffset==-1) // upper left corner
- {
- leftXIncre = 1;
- leftYIncre = 0;
- leftIncreCount = 0;
- rightXIncre = 0;
- rightYIncre = 1;
- rightIncreCount = 1;
- }
- else // lower left corner
- {
- leftXIncre = 0;
- leftYIncre = -1;
- leftIncreCount = 3;
- rightXIncre = 1;
- rightYIncre = 0;
- rightIncreCount = 2;
- }
- }
- else
- {
- if(yOffset==-1) // upper right corner
- {
- leftXIncre = 0;
- leftYIncre = 1;
- leftIncreCount = 1;
- rightXIncre = -1;
- rightYIncre = 0;
- rightIncreCount = 0;
- }
- else // lower right corner
- {
- leftXIncre = -1;
- leftYIncre = 0;
- leftIncreCount = 2;
- rightXIncre = 0;
- rightYIncre = -1;
- rightIncreCount = 3;
- }
- }
- }
- else // at the edge
- {
- if(xOffset==-1) // left edge
- {
- leftXIncre = 0;
- leftYIncre = -1;
- leftIncreCount = 3;
- rightXIncre = 0;
- rightYIncre = 1;
- rightIncreCount = 1;
- }
- else if(xOffset==targetWidth) // right edge
- {
- leftXIncre = 0;
- leftYIncre = 1;
- leftIncreCount = 1;
- rightXIncre = 0;
- rightYIncre = -1;
- rightIncreCount = 3;
- }
- else if(yOffset==-1) // upper edge
- {
- leftXIncre = 1;
- leftYIncre = 0;
- leftIncreCount = 0;
- rightXIncre = -1;
- rightYIncre = 0;
- rightIncreCount = 0;
- }
- else if(yOffset==targetHeight) // lower edge
- {
- leftXIncre = -1;
- leftYIncre = 0;
- leftIncreCount = 2;
- rightXIncre = 1;
- rightYIncre = 0;
- rightIncreCount = 2;
- }
- }
- leftXLoc = rightXLoc = targetXLoc + xOffset;
- leftYLoc = rightYLoc = targetYLoc + yOffset;
- int canReach;
- int outBoundary; // true if out of map boundary
- //-------------------------------------------------------------------------------------//
- // count the reachable location
- //-------------------------------------------------------------------------------------//
- for(int i=1; i<bound; i++) // exclude the starting location
- {
- debug_result_check(analyseResult, targetWidth, targetHeight);
- #ifdef DEBUG
- int debugLeftXLoc = leftXLoc;
- int debugLeftYloc = leftYLoc;
- int debugRightXLoc = rightXLoc;
- int debugRightYLoc = rightYLoc;
- int debugLeftIncreCount = leftIncreCount;
- int debugRightIncreCount = rightIncreCount;
- char debugUnreachableTable2[MAX_UNIT_SURROUND_SIZE][MAX_UNIT_SURROUND_SIZE];
- memcpy(debugUnreachableTable2, unreachable_table, sizeof(char)*MAX_UNIT_SURROUND_SIZE*MAX_UNIT_SURROUND_SIZE);
- for(int i=0; i<MAX_UNIT_SURROUND_SIZE; i++)
- {
- for(int j=0; j<MAX_UNIT_SURROUND_SIZE; j++)
- {
- if(debugUnreachableTable2[i][j] && debugUnreachableTable2[i][j]==1)
- debugUnreachableTable2[i][j] = 2; // plus 1 to distinguish the original table
- }
- }
- #endif
- //------------------------------------------------------------//
- // process left hand side checking
- //------------------------------------------------------------//
- if(leftContinue)
- {
- canReach = 0;
- outBoundary = 0;
- leftXLoc += leftXIncre;
- leftYLoc += leftYIncre;
- if((leftXLoc==targetXLoc-1 || leftXLoc==targetXLoc2+1) && (leftYLoc==targetYLoc-1 || leftYLoc==targetYLoc2+1))
- {
- if((++leftIncreCount)>=4)
- leftIncreCount = 0;
- leftXIncre = leftXIncreTable[leftIncreCount];
- leftYIncre = leftYIncreTable[leftIncreCount];
- }
-
- if(leftXLoc>=0 && leftXLoc<MAX_WORLD_X_LOC && leftYLoc>=0 && leftYLoc<MAX_WORLD_Y_LOC)
- {
- if(unreachable_table[leftXLoc-targetXLoc+SHIFT_ADJUST][leftYLoc-targetYLoc+SHIFT_ADJUST])
- canReach = 1; // concept incorrect, but it is used to terminate this part of checking
- else
- {
- locPtr = world.get_loc(leftXLoc, leftYLoc);
- if(locPtr->can_move(targetMobileType))
- canReach = 1;
- }
- }
- else
- outBoundary = 1;
- if(canReach)
- leftContinue = 0;
- else if(!outBoundary)
- {
- err_when(unreachable_table[leftXLoc-targetXLoc+SHIFT_ADJUST][leftYLoc-targetYLoc+SHIFT_ADJUST]);
- unreachable_table[leftXLoc-targetXLoc+SHIFT_ADJUST][leftYLoc-targetYLoc+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, targetWidth, targetHeight);
- err_when(analyseResult<0);
- }
- #ifdef DEBUG
- else
- err_when(!unreachable_table[leftXLoc-targetXLoc+SHIFT_ADJUST][leftYLoc-targetYLoc+SHIFT_ADJUST]);
- #endif
- i++;
- }
- //------------------------------------------------------------//
- // process right hand side checking
- //------------------------------------------------------------//
- if(rightContinue)
- {
- canReach = 0;
- outBoundary = 0;
- rightXLoc += rightXIncre;
- rightYLoc += rightYIncre;
- if((rightXLoc==targetXLoc-1 || rightXLoc==targetXLoc2+1) && (rightYLoc==targetYLoc-1 || rightYLoc==targetYLoc2+1))
- {
- if((++rightIncreCount)>=4)
- rightIncreCount = 0;
- rightXIncre = rightXIncreTable[rightIncreCount];
- rightYIncre = rightYIncreTable[rightIncreCount];
- }
-
- if(rightXLoc>=0 && rightXLoc<MAX_WORLD_X_LOC && rightYLoc>=0 && rightYLoc<MAX_WORLD_Y_LOC)
- {
- if(unreachable_table[rightXLoc-targetXLoc+SHIFT_ADJUST][rightYLoc-targetYLoc+SHIFT_ADJUST])
- canReach = 1; // concept incorrect, but it is used to terminate this part of checking
- else
- {
- locPtr = world.get_loc(rightXLoc, rightYLoc);
- if(locPtr->can_move(targetMobileType))
- canReach = 1;
- }
- }
- else
- outBoundary = 1;
- if(canReach)
- rightContinue = 0;
- else if(!outBoundary)
- {
- err_when(unreachable_table[rightXLoc-targetXLoc+SHIFT_ADJUST][rightYLoc-targetYLoc+SHIFT_ADJUST]);
- unreachable_table[rightXLoc-targetXLoc+SHIFT_ADJUST][rightYLoc-targetYLoc+SHIFT_ADJUST] = 1;
- analyseResult--;
- debug_result_check(analyseResult, targetWidth, targetHeight);
- err_when(analyseResult<0);
- }
- #ifdef DEBUG
- else
- err_when(!unreachable_table[rightXLoc-targetXLoc+SHIFT_ADJUST][rightYLoc-targetYLoc+SHIFT_ADJUST]);
- #endif
- }
- if(!leftContinue && !rightContinue)
- break;
- }
- }
- //----------- End of function UnitArray::check_nearby_location -----------//
- //--------- Begin of function UnitArray::handle_attack_target_totally_blocked ---------//
- // handle attacking while the target is totally blocked
- //
- // <int> targetXLoc - target x loc
- // <int> targetYLoc - target y loc
- // <short> targetRecno - target recno
- // <short*> selectedUnitArray - selected units' recno
- // <short> selectedCount - num of selected unit
- // <int> targetType - 0 for wall, 1 for unit, 2 for firm, 3 for town
- //
- void UnitArray::handle_attack_target_totally_blocked(int targetXLoc, int targetYLoc, short targetRecno,
- short *selectedUnitArray, short selectedCount, int targetType)
- {
- if(unit_processed_count>0) // some units can reach the target surrounding
- {
- Unit *processedPtr, *unitPtr;
- int proCount = unit_processed_count - 1;
- int unproCount = selectedCount - proCount - 1; // number of unprocessed
- int sCount = selectedCount-1;
- int found, i, recno;
- #ifdef DEBUG
- int debugCount;
- #endif
- //------------------------------------------------------------------------------------//
- // use the result of those processed units as a reference of those unprocessed units
- //------------------------------------------------------------------------------------//
- while(unproCount)
- {
- err_when(unit_array.is_deleted(unit_processed_array[proCount]));
- processedPtr = (Unit*) get_ptr(unit_processed_array[proCount]);
- #ifdef DEBUG
- debugCount = 0;
- #endif
-
- err_when(sCount<0);
- do
- {
- #ifdef DEBUG
- debugCount++;
- err_when(debugCount>1000);
- #endif
- found = 0;
- recno = selectedUnitArray[sCount];
- for(i=0; i<unit_processed_count; i++)
- {
- if(unit_processed_array[i]==recno)
- {
- found++;
- break;
- }
- }
-
- err_when(sCount<0 || sCount>selectedCount);
- sCount--;
- }while(found);
-
- unitPtr = (Unit *) get_ptr(recno);
- unitPtr->move_to(processedPtr->move_to_x_loc, processedPtr->move_to_y_loc);
- switch(targetType)
- {
- case 0: // wall
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_WALL;
- err_when(targetRecno);
- break;
- case 1: // unit
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_UNIT;
- err_when(!targetRecno);
- break;
- case 2: // firm
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_FIRM;
- err_when(!targetRecno);
- break;
- case 3: // town
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_TOWN;
- err_when(!targetRecno);
- break;
- }
- unitPtr->action_para = unitPtr->action_para2 = targetRecno;
- unitPtr->action_x_loc = unitPtr->action_x_loc2 = targetXLoc;
- unitPtr->action_y_loc = unitPtr->action_y_loc2 = targetYLoc;
- proCount--;
- if(proCount<0)
- proCount = unit_processed_count - 1;
-
- unproCount--;
- }
- }
- else // none of the units reaches the target surrounding
- {
- //----------------------------------------------------------------//
- // handle the case for 1x1 units now, no 2x2 units
- //----------------------------------------------------------------//
- //-*********** improve later ************-//
- int unprocessed = selectedCount;
- Unit *firstPtr = (Unit *) get_ptr(selectedUnitArray[unprocessed-1]);
- switch(targetType)
- {
- case 0: firstPtr->attack_wall(targetXLoc, targetYLoc);
- break;
- case 1: firstPtr->attack_unit(targetXLoc, targetYLoc);
- break;
- case 2: firstPtr->attack_firm(targetXLoc, targetYLoc);
- break;
- case 3: firstPtr->attack_town(targetXLoc, targetYLoc);
- break;
- }
- int moveToXLoc = firstPtr->move_to_x_loc;
- int moveToYLoc = firstPtr->move_to_y_loc;
- /*if(seek_path.path_status==PATH_NODE_USED_UP)
- {
- int debug = 0;
- }*/
- Unit *unitPtr;
- while(unprocessed)
- {
- unitPtr = (Unit *) get_ptr(selectedUnitArray[unprocessed-1]);
- unitPtr->move_to(moveToXLoc, moveToYLoc);
- switch(targetType)
- {
- case 0: // wall
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_WALL;
- unitPtr->action_para = unitPtr->action_para2 = 0;
- break;
- case 1: // unit
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_UNIT;
- unitPtr->action_para = unitPtr->action_para2 = targetRecno;
- break;
- case 2: // firm
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_FIRM;
- unitPtr->action_para = unitPtr->action_para2 = targetRecno;
- break;
- case 3: // town
- unitPtr->action_mode = unitPtr->action_mode2 = ACTION_ATTACK_TOWN;
- unitPtr->action_para = unitPtr->action_para2 = targetRecno;
- break;
- }
- unitPtr->action_x_loc = unitPtr->action_x_loc2 = targetXLoc;
- unitPtr->action_y_loc = unitPtr->action_y_loc2 = targetYLoc;
-
- unprocessed--;
- }
- }
- }
- //----------- End of function UnitArray::handle_attack_target_totally_blocked -----------//
|